blob: ff24fa14a38448bcc49485e880e7c2f2d2652513 [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
61static const char *exec_name;
62
63void usage_exit() {
clang-format397d9642016-08-08 18:51:16 -070064 fprintf(stderr,
65 "Usage: %s <codec> <width> <height> <infile> <outfile> "
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070066 "<frame> <limit(optional)>\n",
67 exec_name);
68 exit(EXIT_FAILURE);
69}
70
Yaowu Xuf883b422016-08-30 14:01:10 -070071static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
Urvang Joshid71a2312016-07-14 12:33:48 -070072 unsigned int frame_out, int *mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -070073 aom_image_t enc_img, dec_img;
74 struct av1_ref_frame ref_enc, ref_dec;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070075
clang-format397d9642016-08-08 18:51:16 -070076 if (*mismatch_seen) return;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070077
78 ref_enc.idx = 0;
79 ref_dec.idx = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070080 if (aom_codec_control(encoder, AV1_GET_REFERENCE, &ref_enc))
clang-format397d9642016-08-08 18:51:16 -070081 die_codec(encoder, "Failed to get encoder reference frame");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070082 enc_img = ref_enc.img;
Yaowu Xuf883b422016-08-30 14:01:10 -070083 if (aom_codec_control(decoder, AV1_GET_REFERENCE, &ref_dec))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070084 die_codec(decoder, "Failed to get decoder reference frame");
85 dec_img = ref_dec.img;
86
Urvang Joshi09c293e2017-04-20 17:56:27 -070087 if (!aom_compare_img(&enc_img, &dec_img)) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070088 int y[4], u[4], v[4];
89
90 *mismatch_seen = 1;
91
Urvang Joshi09c293e2017-04-20 17:56:27 -070092 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
clang-format397d9642016-08-08 18:51:16 -070093 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]);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700100 }
101
Yaowu Xuf883b422016-08-30 14:01:10 -0700102 aom_img_free(&enc_img);
103 aom_img_free(&dec_img);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700104}
105
Urvang Joshid71a2312016-07-14 12:33:48 -0700106static int encode_frame(aom_codec_ctx_t *ecodec, aom_image_t *img,
107 unsigned int frame_in, AvxVideoWriter *writer,
108 int test_decode, aom_codec_ctx_t *dcodec,
109 unsigned int *frame_out, int *mismatch_seen) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700110 int got_pkts = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700111 aom_codec_iter_t iter = NULL;
112 const aom_codec_cx_pkt_t *pkt = NULL;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700113 int got_data;
Yaowu Xuf883b422016-08-30 14:01:10 -0700114 const aom_codec_err_t res =
115 aom_codec_encode(ecodec, img, frame_in, 1, 0, AOM_DL_GOOD_QUALITY);
116 if (res != AOM_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700117
118 got_data = 0;
119
Yaowu Xuf883b422016-08-30 14:01:10 -0700120 while ((pkt = aom_codec_get_cx_data(ecodec, &iter)) != NULL) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700121 got_pkts = 1;
122
Yaowu Xuf883b422016-08-30 14:01:10 -0700123 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
124 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700125
Yaowu Xuf883b422016-08-30 14:01:10 -0700126 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
clang-format397d9642016-08-08 18:51:16 -0700127 *frame_out += 1;
128 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700129
Yaowu Xuf883b422016-08-30 14:01:10 -0700130 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700131 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) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 if (aom_codec_decode(dcodec, pkt->data.frame.buf,
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700142 (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) {
Urvang Joshid71a2312016-07-14 12:33:48 -0700150 testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700151 }
152
153 return got_pkts;
154}
155
156int main(int argc, char **argv) {
157 FILE *infile = NULL;
158 // Encoder
Urvang Joshid71a2312016-07-14 12:33:48 -0700159 aom_codec_ctx_t ecodec;
160 aom_codec_enc_cfg_t cfg;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700161 unsigned int frame_in = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700162 aom_image_t raw;
163 aom_codec_err_t res;
Urvang Joshid71a2312016-07-14 12:33:48 -0700164 AvxVideoInfo info;
Yaowu Xuf883b422016-08-30 14:01:10 -0700165 AvxVideoWriter *writer = NULL;
166 const AvxInterface *encoder = NULL;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700167
168 // Test encoder/decoder mismatch.
169 int test_decode = 1;
170 // Decoder
Yaowu Xuf883b422016-08-30 14:01:10 -0700171 aom_codec_ctx_t dcodec;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700172 unsigned int frame_out = 0;
173
174 // The frame number to set reference frame on
Yunqing Wang2a5a3f62016-07-22 17:14:22 -0700175 unsigned int update_frame_num = 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700176 int mismatch_seen = 0;
177
178 const int fps = 30;
179 const int bitrate = 500;
180
181 const char *codec_arg = NULL;
182 const char *width_arg = NULL;
183 const char *height_arg = NULL;
184 const char *infile_arg = NULL;
185 const char *outfile_arg = NULL;
Urvang Joshid3a75762016-07-08 16:09:36 -0700186 const char *update_frame_num_arg = NULL;
Yunqing Wang2a5a3f62016-07-22 17:14:22 -0700187 unsigned int limit = 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700188 exec_name = argv[0];
189
Urvang Joshid71a2312016-07-14 12:33:48 -0700190 // Clear explicitly, as simply assigning "{ 0 }" generates
191 // "missing-field-initializers" warning in some compilers.
192 memset(&ecodec, 0, sizeof(ecodec));
193 memset(&cfg, 0, sizeof(cfg));
194 memset(&info, 0, sizeof(info));
195
clang-format397d9642016-08-08 18:51:16 -0700196 if (argc < 7) die("Invalid number of arguments");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700197
198 codec_arg = argv[1];
199 width_arg = argv[2];
200 height_arg = argv[3];
201 infile_arg = argv[4];
202 outfile_arg = argv[5];
Urvang Joshid3a75762016-07-08 16:09:36 -0700203 update_frame_num_arg = argv[6];
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700204
Yaowu Xuf883b422016-08-30 14:01:10 -0700205 encoder = get_aom_encoder_by_name(codec_arg);
clang-format397d9642016-08-08 18:51:16 -0700206 if (!encoder) die("Unsupported codec.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700207
Urvang Joshid3a75762016-07-08 16:09:36 -0700208 update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
Yaowu Xuf883b422016-08-30 14:01:10 -0700209 // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
210 // allocated while calling aom_codec_encode(), thus, setting reference for
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700211 // 1st frame isn't supported.
Urvang Joshid3a75762016-07-08 16:09:36 -0700212 if (update_frame_num <= 1) {
213 die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
214 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700215
216 if (argc > 7) {
Urvang Joshid3a75762016-07-08 16:09:36 -0700217 limit = (unsigned int)strtoul(argv[7], NULL, 0);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700218 if (update_frame_num > limit)
219 die("Update frame number couldn't larger than limit\n");
220 }
221
222 info.codec_fourcc = encoder->fourcc;
James Zern097fef92017-03-23 00:25:55 -0700223 info.frame_width = (int)strtol(width_arg, NULL, 0);
224 info.frame_height = (int)strtol(height_arg, NULL, 0);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700225 info.time_base.numerator = 1;
226 info.time_base.denominator = fps;
227
clang-format397d9642016-08-08 18:51:16 -0700228 if (info.frame_width <= 0 || info.frame_height <= 0 ||
229 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700230 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
231 }
232
Yaowu Xuf883b422016-08-30 14:01:10 -0700233 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
clang-format397d9642016-08-08 18:51:16 -0700234 info.frame_height, 1)) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700235 die("Failed to allocate image.");
236 }
237
Yaowu Xuf883b422016-08-30 14:01:10 -0700238 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700239
Yaowu Xuf883b422016-08-30 14:01:10 -0700240 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
clang-format397d9642016-08-08 18:51:16 -0700241 if (res) die_codec(&ecodec, "Failed to get default codec config.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700242
243 cfg.g_w = info.frame_width;
244 cfg.g_h = info.frame_height;
245 cfg.g_timebase.num = info.time_base.numerator;
246 cfg.g_timebase.den = info.time_base.denominator;
247 cfg.rc_target_bitrate = bitrate;
248 cfg.g_lag_in_frames = 3;
249
Yaowu Xuf883b422016-08-30 14:01:10 -0700250 writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
clang-format397d9642016-08-08 18:51:16 -0700251 if (!writer) die("Failed to open %s for writing.", outfile_arg);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700252
253 if (!(infile = fopen(infile_arg, "rb")))
254 die("Failed to open %s for reading.", infile_arg);
255
Yaowu Xuf883b422016-08-30 14:01:10 -0700256 if (aom_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700257 die_codec(&ecodec, "Failed to initialize encoder");
258
259 // Disable alt_ref.
Yaowu Xuf883b422016-08-30 14:01:10 -0700260 if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700261 die_codec(&ecodec, "Failed to set enable auto alt ref");
262
263 if (test_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700264 const AvxInterface *decoder = get_aom_decoder_by_name(codec_arg);
265 if (aom_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
clang-format397d9642016-08-08 18:51:16 -0700266 die_codec(&dcodec, "Failed to initialize decoder.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700267 }
268
269 // Encode frames.
Yaowu Xuf883b422016-08-30 14:01:10 -0700270 while (aom_img_read(&raw, infile)) {
clang-format397d9642016-08-08 18:51:16 -0700271 if (limit && frame_in >= limit) break;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700272 if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700273 aom_ref_frame_t ref;
274 ref.frame_type = AOM_LAST_FRAME;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700275 ref.img = raw;
276 // Set reference frame in encoder.
Yaowu Xuf883b422016-08-30 14:01:10 -0700277 if (aom_codec_control(&ecodec, AOM_SET_REFERENCE, &ref))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700278 die_codec(&ecodec, "Failed to set reference frame");
279 printf(" <SET_REF>");
280
281 // If set_reference in decoder is commented out, the enc/dec mismatch
282 // would be seen.
283 if (test_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700284 if (aom_codec_control(&dcodec, AOM_SET_REFERENCE, &ref))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700285 die_codec(&dcodec, "Failed to set reference frame");
286 }
287 }
288
Urvang Joshid71a2312016-07-14 12:33:48 -0700289 encode_frame(&ecodec, &raw, frame_in, writer, test_decode, &dcodec,
clang-format397d9642016-08-08 18:51:16 -0700290 &frame_out, &mismatch_seen);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700291 frame_in++;
clang-format397d9642016-08-08 18:51:16 -0700292 if (mismatch_seen) break;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700293 }
294
295 // Flush encoder.
296 if (!mismatch_seen)
Urvang Joshid71a2312016-07-14 12:33:48 -0700297 while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
298 &frame_out, &mismatch_seen)) {
clang-format397d9642016-08-08 18:51:16 -0700299 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700300
301 printf("\n");
302 fclose(infile);
303 printf("Processed %d frames.\n", frame_out);
304
305 if (test_decode) {
306 if (!mismatch_seen)
307 printf("Encoder/decoder results are matching.\n");
308 else
309 printf("Encoder/decoder results are NOT matching.\n");
310 }
311
312 if (test_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -0700313 if (aom_codec_destroy(&dcodec))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700314 die_codec(&dcodec, "Failed to destroy decoder");
315
Yaowu Xuf883b422016-08-30 14:01:10 -0700316 aom_img_free(&raw);
317 if (aom_codec_destroy(&ecodec))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700318 die_codec(&ecodec, "Failed to destroy encoder.");
319
Yaowu Xuf883b422016-08-30 14:01:10 -0700320 aom_video_writer_close(writer);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700321
322 return EXIT_SUCCESS;
323}