blob: c10f37d63c2d5846c09f682a0e5bddc61266684f [file] [log] [blame]
Yunqing Wang9aaa3c92016-03-25 11:57:20 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yunqing Wang9aaa3c92016-03-25 11:57:20 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012// AV1 Set Reference Frame
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070013// ============================
14//
Yaowu Xuf883b422016-08-30 14:01:10 -070015// This is an example demonstrating how to overwrite the AV1 encoder's
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070016// internal reference frame. In the sample we set the last frame to the
17// current frame. This technique could be used to bounce between two cameras.
18//
19// The decoder would also have to set the reference frame to the same value
20// on the same frame, or the video will become corrupt. The 'test_decode'
21// variable is set to 1 in this example that tests if the encoder and decoder
22// results are matching.
23//
24// Usage
25// -----
26// This example encodes a raw video. And the last argument passed in specifies
27// the frame number to update the reference frame on. For example, run
Yaowu Xuf883b422016-08-30 14:01:10 -070028// examples/aom_cx_set_ref av1 352 288 in.yuv out.ivf 4 30
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070029// The parameter is parsed as follows:
30//
31//
32// Extra Variables
33// ---------------
34// This example maintains the frame number passed on the command line
35// in the `update_frame_num` variable.
36//
37//
38// Configuration
39// -------------
40//
41// The reference frame is updated on the frame specified on the command
42// line.
43//
44// Observing The Effects
45// ---------------------
46// The encoder and decoder results should be matching when the same reference
47// frame setting operation is done in both encoder and decoder. Otherwise,
48// the encoder/decoder mismatch would be seen.
49
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53
Yaowu Xuf883b422016-08-30 14:01:10 -070054#include "aom/aomcx.h"
55#include "aom/aom_decoder.h"
56#include "aom/aom_encoder.h"
Urvang Joshi09c293e2017-04-20 17:56:27 -070057#include "examples/encoder_util.h"
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070058#include "./tools_common.h"
59#include "./video_writer.h"
60
Yunqing Wangff07a122018-04-17 16:02:34 -070061#define AOM_BORDER_IN_PIXELS 288
62
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070063static const char *exec_name;
64
65void usage_exit() {
clang-format397d9642016-08-08 18:51:16 -070066 fprintf(stderr,
67 "Usage: %s <codec> <width> <height> <infile> <outfile> "
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070068 "<frame> <limit(optional)>\n",
69 exec_name);
70 exit(EXIT_FAILURE);
71}
72
Yaowu Xuf883b422016-08-30 14:01:10 -070073static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
Imdad Sardharwallab5def022017-12-14 11:20:24 +000074 unsigned int frame_out, int *mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -070075 aom_image_t enc_img, dec_img;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070076
clang-format397d9642016-08-08 18:51:16 -070077 if (*mismatch_seen) return;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070078
Yunqing Wang4450c762018-04-24 13:49:25 -070079 /* Get the internal reference frame */
80 if (aom_codec_control(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img))
clang-format397d9642016-08-08 18:51:16 -070081 die_codec(encoder, "Failed to get encoder reference frame");
Yunqing Wang4450c762018-04-24 13:49:25 -070082 if (aom_codec_control(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070083 die_codec(decoder, "Failed to get decoder reference frame");
Yunqing Wang4450c762018-04-24 13:49:25 -070084
85 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
86 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
87 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
88 aom_image_t enc_hbd_img;
89 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
90 enc_img.d_w, enc_img.d_h, 16);
91 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
92 enc_img = enc_hbd_img;
93 }
94 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
95 aom_image_t dec_hbd_img;
96 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
97 dec_img.d_w, dec_img.d_h, 16);
98 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
99 dec_img = dec_hbd_img;
100 }
101 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700102
Imdad Sardharwallab5def022017-12-14 11:20:24 +0000103 if (!aom_compare_img(&enc_img, &dec_img)) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700104 int y[4], u[4], v[4];
Yunqing Wang4450c762018-04-24 13:49:25 -0700105 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
106 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
107 } else {
108 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
109 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700110
clang-format397d9642016-08-08 18:51:16 -0700111 printf(
112 "Encode/decode mismatch on frame %d at"
113 " Y[%d, %d] {%d/%d},"
114 " U[%d, %d] {%d/%d},"
115 " V[%d, %d] {%d/%d}",
116 frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
117 v[2], v[3]);
Yunqing Wang4450c762018-04-24 13:49:25 -0700118 *mismatch_seen = 1;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700119 }
120
Yaowu Xuf883b422016-08-30 14:01:10 -0700121 aom_img_free(&enc_img);
122 aom_img_free(&dec_img);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700123}
124
Urvang Joshid71a2312016-07-14 12:33:48 -0700125static int encode_frame(aom_codec_ctx_t *ecodec, aom_image_t *img,
126 unsigned int frame_in, AvxVideoWriter *writer,
127 int test_decode, aom_codec_ctx_t *dcodec,
Imdad Sardharwallab5def022017-12-14 11:20:24 +0000128 unsigned int *frame_out, int *mismatch_seen) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700129 int got_pkts = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700130 aom_codec_iter_t iter = NULL;
131 const aom_codec_cx_pkt_t *pkt = NULL;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700132 int got_data;
Sean DuBois47cc2552018-01-23 07:44:16 +0000133 const aom_codec_err_t res = aom_codec_encode(ecodec, img, frame_in, 1, 0);
Yaowu Xuf883b422016-08-30 14:01:10 -0700134 if (res != AOM_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700135
136 got_data = 0;
137
Yaowu Xuf883b422016-08-30 14:01:10 -0700138 while ((pkt = aom_codec_get_cx_data(ecodec, &iter)) != NULL) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700139 got_pkts = 1;
140
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
142 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700143
Yaowu Xuf883b422016-08-30 14:01:10 -0700144 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
clang-format397d9642016-08-08 18:51:16 -0700145 *frame_out += 1;
146 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700147
Yaowu Xuf883b422016-08-30 14:01:10 -0700148 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700149 pkt->data.frame.sz,
150 pkt->data.frame.pts)) {
151 die_codec(ecodec, "Failed to write compressed frame");
152 }
153 printf(keyframe ? "K" : ".");
154 fflush(stdout);
155 got_data = 1;
156
157 // Decode 1 frame.
158 if (test_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700159 if (aom_codec_decode(dcodec, pkt->data.frame.buf,
Sean DuBois47cc2552018-01-23 07:44:16 +0000160 (unsigned int)pkt->data.frame.sz, NULL))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700161 die_codec(dcodec, "Failed to decode frame.");
162 }
163 }
164 }
165
166 // Mismatch checking
167 if (got_data && test_decode) {
Imdad Sardharwallab5def022017-12-14 11:20:24 +0000168 testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700169 }
170
171 return got_pkts;
172}
173
174int main(int argc, char **argv) {
175 FILE *infile = NULL;
176 // Encoder
Urvang Joshid71a2312016-07-14 12:33:48 -0700177 aom_codec_ctx_t ecodec;
178 aom_codec_enc_cfg_t cfg;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700179 unsigned int frame_in = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700180 aom_image_t raw;
Yunqing Wang4450c762018-04-24 13:49:25 -0700181 aom_image_t ext_ref;
Yaowu Xuf883b422016-08-30 14:01:10 -0700182 aom_codec_err_t res;
Urvang Joshid71a2312016-07-14 12:33:48 -0700183 AvxVideoInfo info;
Yaowu Xuf883b422016-08-30 14:01:10 -0700184 AvxVideoWriter *writer = NULL;
185 const AvxInterface *encoder = NULL;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700186
187 // Test encoder/decoder mismatch.
188 int test_decode = 1;
189 // Decoder
Yaowu Xuf883b422016-08-30 14:01:10 -0700190 aom_codec_ctx_t dcodec;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700191 unsigned int frame_out = 0;
192
193 // The frame number to set reference frame on
Yunqing Wang2a5a3f62016-07-22 17:14:22 -0700194 unsigned int update_frame_num = 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700195 int mismatch_seen = 0;
196
197 const int fps = 30;
198 const int bitrate = 500;
199
200 const char *codec_arg = NULL;
201 const char *width_arg = NULL;
202 const char *height_arg = NULL;
203 const char *infile_arg = NULL;
204 const char *outfile_arg = NULL;
Urvang Joshid3a75762016-07-08 16:09:36 -0700205 const char *update_frame_num_arg = NULL;
Yunqing Wang2a5a3f62016-07-22 17:14:22 -0700206 unsigned int limit = 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700207 exec_name = argv[0];
208
Urvang Joshid71a2312016-07-14 12:33:48 -0700209 // Clear explicitly, as simply assigning "{ 0 }" generates
210 // "missing-field-initializers" warning in some compilers.
211 memset(&ecodec, 0, sizeof(ecodec));
212 memset(&cfg, 0, sizeof(cfg));
213 memset(&info, 0, sizeof(info));
214
clang-format397d9642016-08-08 18:51:16 -0700215 if (argc < 7) die("Invalid number of arguments");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700216
217 codec_arg = argv[1];
218 width_arg = argv[2];
219 height_arg = argv[3];
220 infile_arg = argv[4];
221 outfile_arg = argv[5];
Urvang Joshid3a75762016-07-08 16:09:36 -0700222 update_frame_num_arg = argv[6];
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700223
Yaowu Xuf883b422016-08-30 14:01:10 -0700224 encoder = get_aom_encoder_by_name(codec_arg);
clang-format397d9642016-08-08 18:51:16 -0700225 if (!encoder) die("Unsupported codec.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700226
Urvang Joshid3a75762016-07-08 16:09:36 -0700227 update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
Yaowu Xuf883b422016-08-30 14:01:10 -0700228 // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
229 // allocated while calling aom_codec_encode(), thus, setting reference for
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700230 // 1st frame isn't supported.
Urvang Joshid3a75762016-07-08 16:09:36 -0700231 if (update_frame_num <= 1) {
232 die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
233 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700234
235 if (argc > 7) {
Urvang Joshid3a75762016-07-08 16:09:36 -0700236 limit = (unsigned int)strtoul(argv[7], NULL, 0);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700237 if (update_frame_num > limit)
238 die("Update frame number couldn't larger than limit\n");
239 }
240
241 info.codec_fourcc = encoder->fourcc;
James Zern097fef92017-03-23 00:25:55 -0700242 info.frame_width = (int)strtol(width_arg, NULL, 0);
243 info.frame_height = (int)strtol(height_arg, NULL, 0);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700244 info.time_base.numerator = 1;
245 info.time_base.denominator = fps;
246
Yunqing Wangff07a122018-04-17 16:02:34 -0700247 if (info.frame_width <= 0 || info.frame_height <= 0) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700248 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
249 }
250
Yunqing Wangff07a122018-04-17 16:02:34 -0700251 if (!aom_img_alloc_with_border(&raw, AOM_IMG_FMT_I420, info.frame_width,
Yunqing Wang4450c762018-04-24 13:49:25 -0700252 info.frame_height, 1, 8, 0)) {
253 die("Failed to allocate image.");
254 }
255 // Allocate memory with the border so that it can be used as a reference.
256 if (!aom_img_alloc_with_border(&ext_ref, AOM_IMG_FMT_I420, info.frame_width,
Yunqing Wangff07a122018-04-17 16:02:34 -0700257 info.frame_height, 32, 8,
258 AOM_BORDER_IN_PIXELS)) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700259 die("Failed to allocate image.");
260 }
261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700263
Yaowu Xuf883b422016-08-30 14:01:10 -0700264 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
clang-format397d9642016-08-08 18:51:16 -0700265 if (res) die_codec(&ecodec, "Failed to get default codec config.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700266
267 cfg.g_w = info.frame_width;
268 cfg.g_h = info.frame_height;
269 cfg.g_timebase.num = info.time_base.numerator;
270 cfg.g_timebase.den = info.time_base.denominator;
271 cfg.rc_target_bitrate = bitrate;
272 cfg.g_lag_in_frames = 3;
273
Yaowu Xuf883b422016-08-30 14:01:10 -0700274 writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
clang-format397d9642016-08-08 18:51:16 -0700275 if (!writer) die("Failed to open %s for writing.", outfile_arg);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700276
277 if (!(infile = fopen(infile_arg, "rb")))
278 die("Failed to open %s for reading.", infile_arg);
279
Yaowu Xuf883b422016-08-30 14:01:10 -0700280 if (aom_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700281 die_codec(&ecodec, "Failed to initialize encoder");
282
283 // Disable alt_ref.
Yaowu Xuf883b422016-08-30 14:01:10 -0700284 if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700285 die_codec(&ecodec, "Failed to set enable auto alt ref");
286
287 if (test_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700288 const AvxInterface *decoder = get_aom_decoder_by_name(codec_arg);
289 if (aom_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
clang-format397d9642016-08-08 18:51:16 -0700290 die_codec(&dcodec, "Failed to initialize decoder.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700291 }
292
293 // Encode frames.
Yunqing Wang4450c762018-04-24 13:49:25 -0700294 aom_image_t *img_frm = &ext_ref;
295 while (aom_img_read(img_frm, infile)) {
clang-format397d9642016-08-08 18:51:16 -0700296 if (limit && frame_in >= limit) break;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700297 if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
Thomas Daede497d1952017-08-08 17:33:06 -0700298 av1_ref_frame_t ref;
299 ref.idx = 0;
Yunqing Wang4450c762018-04-24 13:49:25 -0700300 ref.use_external_ref = 0;
301 ref.img = *img_frm;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700302 // Set reference frame in encoder.
Thomas Daede497d1952017-08-08 17:33:06 -0700303 if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700304 die_codec(&ecodec, "Failed to set reference frame");
305 printf(" <SET_REF>");
306
307 // If set_reference in decoder is commented out, the enc/dec mismatch
308 // would be seen.
309 if (test_decode) {
Yunqing Wang4450c762018-04-24 13:49:25 -0700310 ref.use_external_ref = 1;
Thomas Daede497d1952017-08-08 17:33:06 -0700311 if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700312 die_codec(&dcodec, "Failed to set reference frame");
313 }
314 }
315
Yunqing Wang4450c762018-04-24 13:49:25 -0700316 encode_frame(&ecodec, img_frm, frame_in, writer, test_decode, &dcodec,
Imdad Sardharwallab5def022017-12-14 11:20:24 +0000317 &frame_out, &mismatch_seen);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700318 frame_in++;
clang-format397d9642016-08-08 18:51:16 -0700319 if (mismatch_seen) break;
Yunqing Wang4450c762018-04-24 13:49:25 -0700320 if (frame_out >= update_frame_num) img_frm = &raw;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700321 }
322
323 // Flush encoder.
324 if (!mismatch_seen)
Urvang Joshid71a2312016-07-14 12:33:48 -0700325 while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
Imdad Sardharwallab5def022017-12-14 11:20:24 +0000326 &frame_out, &mismatch_seen)) {
clang-format397d9642016-08-08 18:51:16 -0700327 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700328
329 printf("\n");
330 fclose(infile);
331 printf("Processed %d frames.\n", frame_out);
332
333 if (test_decode) {
334 if (!mismatch_seen)
335 printf("Encoder/decoder results are matching.\n");
336 else
337 printf("Encoder/decoder results are NOT matching.\n");
338 }
339
340 if (test_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -0700341 if (aom_codec_destroy(&dcodec))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700342 die_codec(&dcodec, "Failed to destroy decoder");
343
Yaowu Xuf883b422016-08-30 14:01:10 -0700344 aom_img_free(&raw);
345 if (aom_codec_destroy(&ecodec))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700346 die_codec(&ecodec, "Failed to destroy encoder.");
347
Yaowu Xuf883b422016-08-30 14:01:10 -0700348 aom_video_writer_close(writer);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700349
350 return EXIT_SUCCESS;
351}