WebM VP8 Codec SDK
vp8cx_set_ref
1 /*
2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  * Use of this source code is governed by a BSD-style license
5  * that can be found in the LICENSE file in the root of the source
6  * tree. An additional intellectual property rights grant can be found
7  * in the file PATENTS. All contributing project authors may
8  * be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 
12 // VP8 Set Reference Frame
13 // =======================
14 //
15 // This is an example demonstrating how to overwrite the VP8 encoder's
16 // internal reference frame. In the sample we set the last frame to the
17 // current frame. If this is done at a cut scene it will avoid a keyframe.
18 // This technique could be used to bounce between two cameras.
19 //
20 // Note that the decoder would also have to set the reference frame to the
21 // same value on the same frame, or the video will become corrupt.
22 //
23 // Usage
24 // -----
25 // This example adds a single argument to the `simple_encoder` example,
26 // which specifies the frame number to update the reference frame on.
27 // The parameter is parsed as follows:
28 //
29 //
30 // Extra Variables
31 // ---------------
32 // This example maintains the frame number passed on the command line
33 // in the `update_frame_num` variable.
34 //
35 //
36 // Configuration
37 // -------------
38 //
39 // The reference frame is updated on the frame specified on the command
40 // line.
41 //
42 // Observing The Effects
43 // ---------------------
44 // Use the `simple_encoder` example to encode a sample with a cut scene.
45 // Determine the frame number of the cut scene by looking for a generated
46 // key-frame (indicated by a 'K'). Supply that frame number as an argument
47 // to this example, and observe that no key-frame is generated.
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #define VPX_CODEC_DISABLE_COMPAT 1
54 #include "vpx/vp8cx.h"
55 #include "vpx/vpx_encoder.h"
56 
57 #include "./tools_common.h"
58 #include "./video_writer.h"
59 
60 static const char *exec_name;
61 
62 void usage_exit() {
63  fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile> <frame>\n",
64  exec_name);
65  exit(EXIT_FAILURE);
66 }
67 
68 static void encode_frame(vpx_codec_ctx_t *codec,
69  vpx_image_t *img,
70  int frame_index,
71  VpxVideoWriter *writer) {
72  vpx_codec_iter_t iter = NULL;
73  const vpx_codec_cx_pkt_t *pkt = NULL;
74  const vpx_codec_err_t res = vpx_codec_encode(codec, img, frame_index, 1, 0,
76  if (res != VPX_CODEC_OK)
77  die_codec(codec, "Failed to encode frame");
78 
79  while ((pkt = vpx_codec_get_cx_data(codec, &iter)) != NULL) {
80  if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
81  const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
82  if (!vpx_video_writer_write_frame(writer,
83  pkt->data.frame.buf,
84  pkt->data.frame.sz,
85  pkt->data.frame.pts)) {
86  die_codec(codec, "Failed to write compressed frame");
87  }
88 
89  printf(keyframe ? "K" : ".");
90  fflush(stdout);
91  }
92  }
93 }
94 
95 int main(int argc, char **argv) {
96  FILE *infile = NULL;
97  vpx_codec_ctx_t codec = {0};
98  vpx_codec_enc_cfg_t cfg = {0};
99  int frame_count = 0;
100  vpx_image_t raw;
101  vpx_codec_err_t res;
102  VpxVideoInfo info = {0};
103  VpxVideoWriter *writer = NULL;
104  const VpxInterface *encoder = NULL;
105  int update_frame_num = 0;
106  const int fps = 30; // TODO(dkovalev) add command line argument
107  const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
108 
109  exec_name = argv[0];
110 
111  if (argc != 6)
112  die("Invalid number of arguments");
113 
114  // TODO(dkovalev): add vp9 support and rename the file accordingly
115  encoder = get_vpx_encoder_by_name("vp8");
116  if (!encoder)
117  die("Unsupported codec.");
118 
119  update_frame_num = atoi(argv[5]);
120  if (!update_frame_num)
121  die("Couldn't parse frame number '%s'\n", argv[5]);
122 
123  info.codec_fourcc = encoder->fourcc;
124  info.frame_width = strtol(argv[1], NULL, 0);
125  info.frame_height = strtol(argv[2], NULL, 0);
126  info.time_base.numerator = 1;
127  info.time_base.denominator = fps;
128 
129  if (info.frame_width <= 0 ||
130  info.frame_height <= 0 ||
131  (info.frame_width % 2) != 0 ||
132  (info.frame_height % 2) != 0) {
133  die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
134  }
135 
136  if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
137  info.frame_height, 1)) {
138  die("Failed to allocate image.");
139  }
140 
141  printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
142 
143  res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
144  if (res)
145  die_codec(&codec, "Failed to get default codec config.");
146 
147  cfg.g_w = info.frame_width;
148  cfg.g_h = info.frame_height;
149  cfg.g_timebase.num = info.time_base.numerator;
150  cfg.g_timebase.den = info.time_base.denominator;
151  cfg.rc_target_bitrate = bitrate;
152 
153  writer = vpx_video_writer_open(argv[4], kContainerIVF, &info);
154  if (!writer)
155  die("Failed to open %s for writing.", argv[4]);
156 
157  if (!(infile = fopen(argv[3], "rb")))
158  die("Failed to open %s for reading.", argv[3]);
159 
160  if (vpx_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
161  die_codec(&codec, "Failed to initialize encoder");
162 
163  while (vpx_img_read(&raw, infile)) {
164  if (frame_count + 1 == update_frame_num) {
165  vpx_ref_frame_t ref;
166  ref.frame_type = VP8_LAST_FRAME;
167  ref.img = raw;
168  if (vpx_codec_control(&codec, VP8_SET_REFERENCE, &ref))
169  die_codec(&codec, "Failed to set reference frame");
170  }
171 
172  encode_frame(&codec, &raw, frame_count++, writer);
173  }
174  encode_frame(&codec, NULL, -1, writer);
175 
176  printf("\n");
177  fclose(infile);
178  printf("Processed %d frames.\n", frame_count);
179 
180  vpx_img_free(&raw);
181  if (vpx_codec_destroy(&codec))
182  die_codec(&codec, "Failed to destroy codec.");
183 
184  vpx_video_writer_close(writer);
185 
186  return EXIT_SUCCESS;
187 }
Image Descriptor.
Definition: vpx_image.h:102
Describes the encoder algorithm interface to applications.
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:353
int den
Definition: vpx_encoder.h:232
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, unsigned long deadline)
Encode a frame.
Encoder configuration structure.
Definition: vpx_encoder.h:285
Encoder output packet.
Definition: vpx_encoder.h:185
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
Definition: vpx_image.h:56
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:328
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:338
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:186
Operation completed without error.
Definition: vpx_codec.h:91
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:481
int num
Definition: vpx_encoder.h:231
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int usage)
Get a default configuration.
#define VPX_DL_GOOD_QUALITY
Definition: vpx_encoder.h:841
vpx_image_t img
Definition: vp8.h:107
Provides definitions for using the VP8 encoder algorithm within the vpx Codec Interface.
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:740
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:89
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
union vpx_codec_cx_pkt::@1 data
reference frame data struct
Definition: vp8.h:105
vpx_ref_frame_type_t frame_type
Definition: vp8.h:106
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:405
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:123
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
Definition: vpx_encoder.h:169
Definition: vp8.h:45
Codec context structure.
Definition: vpx_codec.h:199