1 /*
2 * Copyright (c) 2016 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 // VP9 Set Reference Frame
12 // ============================
13 //
14 // This is an example demonstrating how to overwrite the VP9 encoder's
15 // internal reference frame. In the sample we set the last frame to the
16 // current frame. This technique could be used to bounce between two cameras.
17 //
18 // The decoder would also have to set the reference frame to the same value
19 // on the same frame, or the video will become corrupt. The 'test_decode'
20 // variable is set to 1 in this example that tests if the encoder and decoder
21 // results are matching.
22 //
23 // Usage
24 // -----
25 // This example encodes a raw video. And the last argument passed in specifies
26 // the frame number to update the reference frame on. For example, run
27 // examples/vp9cx_set_ref 352 288 in.yuv out.ivf 4 30
28 // The parameter is parsed as follows:
29 //
30 //
31 // Extra Variables
32 // ---------------
33 // This example maintains the frame number passed on the command line
34 // in the `update_frame_num` variable.
35 //
36 //
37 // Configuration
38 // -------------
39 //
40 // The reference frame is updated on the frame specified on the command
41 // line.
42 //
43 // Observing The Effects
44 // ---------------------
45 // The encoder and decoder results should be matching when the same reference
46 // frame setting operation is done in both encoder and decoder. Otherwise,
47 // the encoder/decoder mismatch would be seen.
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "vpx/vp8cx.h"
54 #include "vpx/vpx_decoder.h"
55 #include "vpx/vpx_encoder.h"
56 #include "vp9/common/vp9_common.h"
57
58 #include "./tools_common.h"
59 #include "./video_writer.h"
60
61 static const char *exec_name;
62
usage_exit()63 void usage_exit() {
64 fprintf(stderr,
65 "Usage: %s <width> <height> <infile> <outfile> "
66 "<frame> <limit(optional)>\n",
67 exec_name);
68 exit(EXIT_FAILURE);
69 }
70
testing_decode(vpx_codec_ctx_t * encoder,vpx_codec_ctx_t * decoder,unsigned int frame_out,int * mismatch_seen)71 static void testing_decode(vpx_codec_ctx_t *encoder, vpx_codec_ctx_t *decoder,
72 unsigned int frame_out, int *mismatch_seen) {
73 vpx_image_t enc_img, dec_img;
74 struct vp9_ref_frame ref_enc, ref_dec;
75
76 if (*mismatch_seen) return;
77
78 ref_enc.idx = 0;
79 ref_dec.idx = 0;
80 if (vpx_codec_control(encoder, VP9_GET_REFERENCE, &ref_enc))
81 die_codec(encoder, "Failed to get encoder reference frame");
82 enc_img = ref_enc.img;
83 if (vpx_codec_control(decoder, VP9_GET_REFERENCE, &ref_dec))
84 die_codec(decoder, "Failed to get decoder reference frame");
85 dec_img = ref_dec.img;
86
87 if (!compare_img(&enc_img, &dec_img)) {
88 int y[4], u[4], v[4];
89
90 *mismatch_seen = 1;
91
92 find_mismatch(&enc_img, &dec_img, y, u, v);
93 printf(
94 "Encode/decode mismatch on frame %d at"
95 " Y[%d, %d] {%d/%d},"
96 " U[%d, %d] {%d/%d},"
97 " V[%d, %d] {%d/%d}",
98 frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
99 v[2], v[3]);
100 }
101
102 vpx_img_free(&enc_img);
103 vpx_img_free(&dec_img);
104 }
105
encode_frame(vpx_codec_ctx_t * ecodec,vpx_image_t * img,unsigned int frame_in,VpxVideoWriter * writer,int test_decode,vpx_codec_ctx_t * dcodec,unsigned int * frame_out,int * mismatch_seen)106 static int encode_frame(vpx_codec_ctx_t *ecodec, vpx_image_t *img,
107 unsigned int frame_in, VpxVideoWriter *writer,
108 int test_decode, vpx_codec_ctx_t *dcodec,
109 unsigned int *frame_out, int *mismatch_seen) {
110 int got_pkts = 0;
111 vpx_codec_iter_t iter = NULL;
112 const vpx_codec_cx_pkt_t *pkt = NULL;
113 int got_data;
114 const vpx_codec_err_t res =
115 vpx_codec_encode(ecodec, img, frame_in, 1, 0, VPX_DL_GOOD_QUALITY);
116 if (res != VPX_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
117
118 got_data = 0;
119
120 while ((pkt = vpx_codec_get_cx_data(ecodec, &iter)) != NULL) {
121 got_pkts = 1;
122
123 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
124 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
125
126 if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
127 *frame_out += 1;
128 }
129
130 if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
131 pkt->data.frame.sz,
132 pkt->data.frame.pts)) {
133 die_codec(ecodec, "Failed to write compressed frame");
134 }
135 printf(keyframe ? "K" : ".");
136 fflush(stdout);
137 got_data = 1;
138
139 // Decode 1 frame.
140 if (test_decode) {
141 if (vpx_codec_decode(dcodec, pkt->data.frame.buf,
142 (unsigned int)pkt->data.frame.sz, NULL, 0))
143 die_codec(dcodec, "Failed to decode frame.");
144 }
145 }
146 }
147
148 // Mismatch checking
149 if (got_data && test_decode) {
150 testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
151 }
152
153 return got_pkts;
154 }
155
main(int argc,char ** argv)156 int main(int argc, char **argv) {
157 FILE *infile = NULL;
158 // Encoder
159 vpx_codec_ctx_t ecodec;
160 vpx_codec_enc_cfg_t cfg;
161 unsigned int frame_in = 0;
162 vpx_image_t raw;
163 vpx_codec_err_t res;
164 VpxVideoInfo info;
165 VpxVideoWriter *writer = NULL;
166 const VpxInterface *encoder = NULL;
167
168 // Test encoder/decoder mismatch.
169 int test_decode = 1;
170 // Decoder
171 vpx_codec_ctx_t dcodec;
172 unsigned int frame_out = 0;
173
174 // The frame number to set reference frame on
175 unsigned int update_frame_num = 0;
176 int mismatch_seen = 0;
177
178 const int fps = 30;
179 const int bitrate = 500;
180
181 const char *width_arg = NULL;
182 const char *height_arg = NULL;
183 const char *infile_arg = NULL;
184 const char *outfile_arg = NULL;
185 const char *update_frame_num_arg = NULL;
186 unsigned int limit = 0;
187
188 vp9_zero(ecodec);
189 vp9_zero(cfg);
190 vp9_zero(info);
191
192 exec_name = argv[0];
193
194 if (argc < 6) die("Invalid number of arguments");
195
196 width_arg = argv[1];
197 height_arg = argv[2];
198 infile_arg = argv[3];
199 outfile_arg = argv[4];
200 update_frame_num_arg = argv[5];
201
202 encoder = get_vpx_encoder_by_name("vp9");
203 if (!encoder) die("Unsupported codec.");
204
205 update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
206 // In VP9, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
207 // allocated while calling vpx_codec_encode(), thus, setting reference for
208 // 1st frame isn't supported.
209 if (update_frame_num <= 1) {
210 die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
211 }
212
213 if (argc > 6) {
214 limit = (unsigned int)strtoul(argv[6], NULL, 0);
215 if (update_frame_num > limit)
216 die("Update frame number couldn't larger than limit\n");
217 }
218
219 info.codec_fourcc = encoder->fourcc;
220 info.frame_width = (int)strtol(width_arg, NULL, 0);
221 info.frame_height = (int)strtol(height_arg, NULL, 0);
222 info.time_base.numerator = 1;
223 info.time_base.denominator = fps;
224
225 if (info.frame_width <= 0 || info.frame_height <= 0 ||
226 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
227 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
228 }
229
230 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
231 info.frame_height, 1)) {
232 die("Failed to allocate image.");
233 }
234
235 printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));
236
237 res = vpx_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
238 if (res) die_codec(&ecodec, "Failed to get default codec config.");
239
240 cfg.g_w = info.frame_width;
241 cfg.g_h = info.frame_height;
242 cfg.g_timebase.num = info.time_base.numerator;
243 cfg.g_timebase.den = info.time_base.denominator;
244 cfg.rc_target_bitrate = bitrate;
245 cfg.g_lag_in_frames = 3;
246
247 writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
248 if (!writer) die("Failed to open %s for writing.", outfile_arg);
249
250 if (!(infile = fopen(infile_arg, "rb")))
251 die("Failed to open %s for reading.", infile_arg);
252
253 if (vpx_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0))
254 die("Failed to initialize encoder");
255
256 // Disable alt_ref.
257 if (vpx_codec_control(&ecodec, VP8E_SET_ENABLEAUTOALTREF, 0))
258 die_codec(&ecodec, "Failed to set enable auto alt ref");
259
260 if (test_decode) {
261 const VpxInterface *decoder = get_vpx_decoder_by_name("vp9");
262 if (vpx_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
263 die_codec(&dcodec, "Failed to initialize decoder.");
264 }
265
266 // Encode frames.
267 while (vpx_img_read(&raw, infile)) {
268 if (limit && frame_in >= limit) break;
269 if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
270 vpx_ref_frame_t ref;
271 ref.frame_type = VP8_LAST_FRAME;
272 ref.img = raw;
273 // Set reference frame in encoder.
274 if (vpx_codec_control(&ecodec, VP8_SET_REFERENCE, &ref))
275 die_codec(&ecodec, "Failed to set reference frame");
276 printf(" <SET_REF>");
277
278 // If set_reference in decoder is commented out, the enc/dec mismatch
279 // would be seen.
280 if (test_decode) {
281 if (vpx_codec_control(&dcodec, VP8_SET_REFERENCE, &ref))
282 die_codec(&dcodec, "Failed to set reference frame");
283 }
284 }
285
286 encode_frame(&ecodec, &raw, frame_in, writer, test_decode, &dcodec,
287 &frame_out, &mismatch_seen);
288 frame_in++;
289 if (mismatch_seen) break;
290 }
291
292 // Flush encoder.
293 if (!mismatch_seen)
294 while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
295 &frame_out, &mismatch_seen)) {
296 }
297
298 printf("\n");
299 fclose(infile);
300 printf("Processed %d frames.\n", frame_out);
301
302 if (test_decode) {
303 if (!mismatch_seen)
304 printf("Encoder/decoder results are matching.\n");
305 else
306 printf("Encoder/decoder results are NOT matching.\n");
307 }
308
309 if (test_decode)
310 if (vpx_codec_destroy(&dcodec))
311 die_codec(&dcodec, "Failed to destroy decoder");
312
313 vpx_img_free(&raw);
314 if (vpx_codec_destroy(&ecodec))
315 die_codec(&ecodec, "Failed to destroy encoder.");
316
317 vpx_video_writer_close(writer);
318
319 return EXIT_SUCCESS;
320 }
321