blob: 2f4f6586f4981caa0cc851944793d4302bb7d9cd [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/aom_decoder.h"
55#include "aom/aom_encoder.h"
Tom Finegan77902132018-05-21 10:19:15 -070056#include "aom/aomcx.h"
Urvang Joshi6077b182018-08-21 11:49:38 -070057#include "aom_scale/yv12config.h"
Tom Finegan77902132018-05-21 10:19:15 -070058#include "common/tools_common.h"
59#include "common/video_writer.h"
Urvang Joshi09c293e2017-04-20 17:56:27 -070060#include "examples/encoder_util.h"
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070061
62static const char *exec_name;
63
64void usage_exit() {
clang-format397d9642016-08-08 18:51:16 -070065 fprintf(stderr,
66 "Usage: %s <codec> <width> <height> <infile> <outfile> "
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070067 "<frame> <limit(optional)>\n",
68 exec_name);
69 exit(EXIT_FAILURE);
70}
71
Yaowu Xuf883b422016-08-30 14:01:10 -070072static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
Imdad Sardharwallab5def022017-12-14 11:20:24 +000073 unsigned int frame_out, int *mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -070074 aom_image_t enc_img, dec_img;
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
Yunqing Wang4450c762018-04-24 13:49:25 -070078 /* Get the internal reference frame */
79 if (aom_codec_control(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img))
clang-format397d9642016-08-08 18:51:16 -070080 die_codec(encoder, "Failed to get encoder reference frame");
Yunqing Wang4450c762018-04-24 13:49:25 -070081 if (aom_codec_control(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -070082 die_codec(decoder, "Failed to get decoder reference frame");
Yunqing Wang4450c762018-04-24 13:49:25 -070083
84 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
85 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
86 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
87 aom_image_t enc_hbd_img;
88 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
89 enc_img.d_w, enc_img.d_h, 16);
90 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
91 enc_img = enc_hbd_img;
92 }
93 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
94 aom_image_t dec_hbd_img;
95 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
96 dec_img.d_w, dec_img.d_h, 16);
97 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
98 dec_img = dec_hbd_img;
99 }
100 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700101
Imdad Sardharwallab5def022017-12-14 11:20:24 +0000102 if (!aom_compare_img(&enc_img, &dec_img)) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700103 int y[4], u[4], v[4];
Yunqing Wang4450c762018-04-24 13:49:25 -0700104 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
105 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
106 } else {
107 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
108 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700109
clang-format397d9642016-08-08 18:51:16 -0700110 printf(
111 "Encode/decode mismatch on frame %d at"
112 " Y[%d, %d] {%d/%d},"
113 " U[%d, %d] {%d/%d},"
114 " V[%d, %d] {%d/%d}",
115 frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
116 v[2], v[3]);
Yunqing Wang4450c762018-04-24 13:49:25 -0700117 *mismatch_seen = 1;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700118 }
119
Yaowu Xuf883b422016-08-30 14:01:10 -0700120 aom_img_free(&enc_img);
121 aom_img_free(&dec_img);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700122}
123
Urvang Joshid71a2312016-07-14 12:33:48 -0700124static int encode_frame(aom_codec_ctx_t *ecodec, aom_image_t *img,
125 unsigned int frame_in, AvxVideoWriter *writer,
126 int test_decode, aom_codec_ctx_t *dcodec,
Yunqing Wang20a336d2018-04-25 10:19:53 -0700127 unsigned int *frame_out, int *mismatch_seen,
128 aom_image_t *ext_ref) {
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
Urvang Joshi22043ef2018-09-27 12:41:17 -0700144 ++*frame_out;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700145
Yaowu Xuf883b422016-08-30 14:01:10 -0700146 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700147 pkt->data.frame.sz,
148 pkt->data.frame.pts)) {
149 die_codec(ecodec, "Failed to write compressed frame");
150 }
151 printf(keyframe ? "K" : ".");
152 fflush(stdout);
153 got_data = 1;
154
155 // Decode 1 frame.
156 if (test_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700157 if (aom_codec_decode(dcodec, pkt->data.frame.buf,
Sean DuBois47cc2552018-01-23 07:44:16 +0000158 (unsigned int)pkt->data.frame.sz, NULL))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700159 die_codec(dcodec, "Failed to decode frame.");
Yunqing Wang20a336d2018-04-25 10:19:53 -0700160
161 // Copy out first decoded frame, and use it as reference later.
162 if (*frame_out == 1 && ext_ref != NULL)
Yunqing Wang55ceb3c2018-07-03 11:23:05 -0700163 if (aom_codec_control(dcodec, AV1_COPY_NEW_FRAME_IMAGE, ext_ref))
Yunqing Wang20a336d2018-04-25 10:19:53 -0700164 die_codec(dcodec, "Failed to get decoder new frame");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700165 }
166 }
167 }
168
169 // Mismatch checking
170 if (got_data && test_decode) {
Imdad Sardharwallab5def022017-12-14 11:20:24 +0000171 testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700172 }
173
174 return got_pkts;
175}
176
177int main(int argc, char **argv) {
178 FILE *infile = NULL;
179 // Encoder
Urvang Joshid71a2312016-07-14 12:33:48 -0700180 aom_codec_ctx_t ecodec;
181 aom_codec_enc_cfg_t cfg;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700182 unsigned int frame_in = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700183 aom_image_t raw;
Yunqing Wang750cba02018-05-02 12:32:38 -0700184 aom_image_t raw_shift;
Yunqing Wang4450c762018-04-24 13:49:25 -0700185 aom_image_t ext_ref;
Yaowu Xuf883b422016-08-30 14:01:10 -0700186 aom_codec_err_t res;
Urvang Joshid71a2312016-07-14 12:33:48 -0700187 AvxVideoInfo info;
Yaowu Xuf883b422016-08-30 14:01:10 -0700188 AvxVideoWriter *writer = NULL;
189 const AvxInterface *encoder = NULL;
Yunqing Wang750cba02018-05-02 12:32:38 -0700190 int flags = 0;
191 int allocated_raw_shift = 0;
192 aom_img_fmt_t raw_fmt = AOM_IMG_FMT_I420;
193 aom_img_fmt_t ref_fmt = AOM_IMG_FMT_I420;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700194
195 // Test encoder/decoder mismatch.
196 int test_decode = 1;
197 // Decoder
Yaowu Xuf883b422016-08-30 14:01:10 -0700198 aom_codec_ctx_t dcodec;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700199 unsigned int frame_out = 0;
200
201 // The frame number to set reference frame on
Yunqing Wang2a5a3f62016-07-22 17:14:22 -0700202 unsigned int update_frame_num = 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700203 int mismatch_seen = 0;
204
205 const int fps = 30;
206 const int bitrate = 500;
207
208 const char *codec_arg = NULL;
209 const char *width_arg = NULL;
210 const char *height_arg = NULL;
211 const char *infile_arg = NULL;
212 const char *outfile_arg = NULL;
Urvang Joshid3a75762016-07-08 16:09:36 -0700213 const char *update_frame_num_arg = NULL;
Yunqing Wang2a5a3f62016-07-22 17:14:22 -0700214 unsigned int limit = 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700215 exec_name = argv[0];
216
Urvang Joshid71a2312016-07-14 12:33:48 -0700217 // Clear explicitly, as simply assigning "{ 0 }" generates
218 // "missing-field-initializers" warning in some compilers.
219 memset(&ecodec, 0, sizeof(ecodec));
220 memset(&cfg, 0, sizeof(cfg));
221 memset(&info, 0, sizeof(info));
222
clang-format397d9642016-08-08 18:51:16 -0700223 if (argc < 7) die("Invalid number of arguments");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700224
225 codec_arg = argv[1];
226 width_arg = argv[2];
227 height_arg = argv[3];
228 infile_arg = argv[4];
229 outfile_arg = argv[5];
Urvang Joshid3a75762016-07-08 16:09:36 -0700230 update_frame_num_arg = argv[6];
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700231
Yaowu Xuf883b422016-08-30 14:01:10 -0700232 encoder = get_aom_encoder_by_name(codec_arg);
clang-format397d9642016-08-08 18:51:16 -0700233 if (!encoder) die("Unsupported codec.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700234
Urvang Joshid3a75762016-07-08 16:09:36 -0700235 update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
Yaowu Xuf883b422016-08-30 14:01:10 -0700236 // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
237 // allocated while calling aom_codec_encode(), thus, setting reference for
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700238 // 1st frame isn't supported.
Urvang Joshid3a75762016-07-08 16:09:36 -0700239 if (update_frame_num <= 1) {
240 die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
241 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700242
243 if (argc > 7) {
Urvang Joshid3a75762016-07-08 16:09:36 -0700244 limit = (unsigned int)strtoul(argv[7], NULL, 0);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700245 if (update_frame_num > limit)
246 die("Update frame number couldn't larger than limit\n");
247 }
248
249 info.codec_fourcc = encoder->fourcc;
James Zern097fef92017-03-23 00:25:55 -0700250 info.frame_width = (int)strtol(width_arg, NULL, 0);
251 info.frame_height = (int)strtol(height_arg, NULL, 0);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700252 info.time_base.numerator = 1;
253 info.time_base.denominator = fps;
254
Yunqing Wangff07a122018-04-17 16:02:34 -0700255 if (info.frame_width <= 0 || info.frame_height <= 0) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700256 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
257 }
258
Yunqing Wang750cba02018-05-02 12:32:38 -0700259 // In this test, the bit depth of input video is 8-bit, and the input format
260 // is AOM_IMG_FMT_I420.
261 if (!aom_img_alloc(&raw, raw_fmt, info.frame_width, info.frame_height, 32)) {
Yunqing Wang4450c762018-04-24 13:49:25 -0700262 die("Failed to allocate image.");
263 }
Yunqing Wang750cba02018-05-02 12:32:38 -0700264
Jerome Jiang5fad3fb2019-08-14 10:16:12 -0700265 if (FORCE_HIGHBITDEPTH_DECODING) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Yunqing Wang4450c762018-04-24 13:49:25 -0700266 // Allocate memory with the border so that it can be used as a reference.
Yunqing Wang750cba02018-05-02 12:32:38 -0700267 if (!aom_img_alloc_with_border(&ext_ref, ref_fmt, info.frame_width,
Yunqing Wangff07a122018-04-17 16:02:34 -0700268 info.frame_height, 32, 8,
269 AOM_BORDER_IN_PIXELS)) {
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700270 die("Failed to allocate image.");
271 }
272
Yaowu Xuf883b422016-08-30 14:01:10 -0700273 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700274
Yaowu Xuf883b422016-08-30 14:01:10 -0700275 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
clang-format397d9642016-08-08 18:51:16 -0700276 if (res) die_codec(&ecodec, "Failed to get default codec config.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700277
278 cfg.g_w = info.frame_width;
279 cfg.g_h = info.frame_height;
280 cfg.g_timebase.num = info.time_base.numerator;
281 cfg.g_timebase.den = info.time_base.denominator;
282 cfg.rc_target_bitrate = bitrate;
283 cfg.g_lag_in_frames = 3;
Yunqing Wang750cba02018-05-02 12:32:38 -0700284 cfg.g_bit_depth = AOM_BITS_8;
285
Jerome Jiang5fad3fb2019-08-14 10:16:12 -0700286 flags |= (cfg.g_bit_depth > AOM_BITS_8 || FORCE_HIGHBITDEPTH_DECODING)
Yunqing Wang750cba02018-05-02 12:32:38 -0700287 ? AOM_CODEC_USE_HIGHBITDEPTH
288 : 0;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700289
Yaowu Xuf883b422016-08-30 14:01:10 -0700290 writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
clang-format397d9642016-08-08 18:51:16 -0700291 if (!writer) die("Failed to open %s for writing.", outfile_arg);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700292
293 if (!(infile = fopen(infile_arg, "rb")))
294 die("Failed to open %s for reading.", infile_arg);
295
Yunqing Wang750cba02018-05-02 12:32:38 -0700296 if (aom_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, flags))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700297 die_codec(&ecodec, "Failed to initialize encoder");
298
299 // Disable alt_ref.
Yaowu Xuf883b422016-08-30 14:01:10 -0700300 if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700301 die_codec(&ecodec, "Failed to set enable auto alt ref");
302
303 if (test_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700304 const AvxInterface *decoder = get_aom_decoder_by_name(codec_arg);
305 if (aom_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
clang-format397d9642016-08-08 18:51:16 -0700306 die_codec(&dcodec, "Failed to initialize decoder.");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700307 }
308
309 // Encode frames.
Yunqing Wang20a336d2018-04-25 10:19:53 -0700310 while (aom_img_read(&raw, infile)) {
clang-format397d9642016-08-08 18:51:16 -0700311 if (limit && frame_in >= limit) break;
Yunqing Wang750cba02018-05-02 12:32:38 -0700312 aom_image_t *frame_to_encode;
313
Jerome Jiang5fad3fb2019-08-14 10:16:12 -0700314 if (FORCE_HIGHBITDEPTH_DECODING) {
Yunqing Wang750cba02018-05-02 12:32:38 -0700315 // Need to allocate larger buffer to use hbd internal.
316 int input_shift = 0;
317 if (!allocated_raw_shift) {
318 aom_img_alloc(&raw_shift, raw_fmt | AOM_IMG_FMT_HIGHBITDEPTH,
319 info.frame_width, info.frame_height, 32);
320 allocated_raw_shift = 1;
321 }
322 aom_img_upshift(&raw_shift, &raw, input_shift);
323 frame_to_encode = &raw_shift;
324 } else {
325 frame_to_encode = &raw;
326 }
327
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700328 if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
Thomas Daede497d1952017-08-08 17:33:06 -0700329 av1_ref_frame_t ref;
330 ref.idx = 0;
Yunqing Wang4450c762018-04-24 13:49:25 -0700331 ref.use_external_ref = 0;
Yunqing Wang20a336d2018-04-25 10:19:53 -0700332 ref.img = ext_ref;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700333 // Set reference frame in encoder.
Thomas Daede497d1952017-08-08 17:33:06 -0700334 if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref))
Yunqing Wang20a336d2018-04-25 10:19:53 -0700335 die_codec(&ecodec, "Failed to set encoder reference frame");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700336 printf(" <SET_REF>");
337
338 // If set_reference in decoder is commented out, the enc/dec mismatch
339 // would be seen.
340 if (test_decode) {
Yunqing Wang4450c762018-04-24 13:49:25 -0700341 ref.use_external_ref = 1;
Thomas Daede497d1952017-08-08 17:33:06 -0700342 if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref))
Yunqing Wang20a336d2018-04-25 10:19:53 -0700343 die_codec(&dcodec, "Failed to set decoder reference frame");
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700344 }
345 }
346
Yunqing Wang750cba02018-05-02 12:32:38 -0700347 encode_frame(&ecodec, frame_to_encode, frame_in, writer, test_decode,
348 &dcodec, &frame_out, &mismatch_seen, &ext_ref);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700349 frame_in++;
clang-format397d9642016-08-08 18:51:16 -0700350 if (mismatch_seen) break;
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700351 }
352
353 // Flush encoder.
354 if (!mismatch_seen)
Urvang Joshid71a2312016-07-14 12:33:48 -0700355 while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
Yunqing Wang20a336d2018-04-25 10:19:53 -0700356 &frame_out, &mismatch_seen, NULL)) {
clang-format397d9642016-08-08 18:51:16 -0700357 }
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700358
359 printf("\n");
360 fclose(infile);
361 printf("Processed %d frames.\n", frame_out);
362
363 if (test_decode) {
364 if (!mismatch_seen)
365 printf("Encoder/decoder results are matching.\n");
366 else
367 printf("Encoder/decoder results are NOT matching.\n");
368 }
369
370 if (test_decode)
Yaowu Xuf883b422016-08-30 14:01:10 -0700371 if (aom_codec_destroy(&dcodec))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700372 die_codec(&dcodec, "Failed to destroy decoder");
373
Yunqing Wang750cba02018-05-02 12:32:38 -0700374 if (allocated_raw_shift) aom_img_free(&raw_shift);
375 aom_img_free(&ext_ref);
Yaowu Xuf883b422016-08-30 14:01:10 -0700376 aom_img_free(&raw);
377 if (aom_codec_destroy(&ecodec))
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700378 die_codec(&ecodec, "Failed to destroy encoder.");
379
Yaowu Xuf883b422016-08-30 14:01:10 -0700380 aom_video_writer_close(writer);
Yunqing Wang9aaa3c92016-03-25 11:57:20 -0700381
382 return EXIT_SUCCESS;
383}