1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker // AV1 Set Reference Frame
13*77c1e3ccSAndroid Build Coastguard Worker // ============================
14*77c1e3ccSAndroid Build Coastguard Worker //
15*77c1e3ccSAndroid Build Coastguard Worker // This is an example demonstrating how to overwrite the AV1 encoder's
16*77c1e3ccSAndroid Build Coastguard Worker // internal reference frame. In the sample we set the last frame to the
17*77c1e3ccSAndroid Build Coastguard Worker // current frame. This technique could be used to bounce between two cameras.
18*77c1e3ccSAndroid Build Coastguard Worker //
19*77c1e3ccSAndroid Build Coastguard Worker // The decoder would also have to set the reference frame to the same value
20*77c1e3ccSAndroid Build Coastguard Worker // on the same frame, or the video will become corrupt. The 'test_decode'
21*77c1e3ccSAndroid Build Coastguard Worker // variable is set to 1 in this example that tests if the encoder and decoder
22*77c1e3ccSAndroid Build Coastguard Worker // results are matching.
23*77c1e3ccSAndroid Build Coastguard Worker //
24*77c1e3ccSAndroid Build Coastguard Worker // Usage
25*77c1e3ccSAndroid Build Coastguard Worker // -----
26*77c1e3ccSAndroid Build Coastguard Worker // This example encodes a raw video. And the last argument passed in specifies
27*77c1e3ccSAndroid Build Coastguard Worker // the frame number to update the reference frame on. For example, run
28*77c1e3ccSAndroid Build Coastguard Worker // examples/aom_cx_set_ref av1 352 288 in.yuv out.ivf 4 30
29*77c1e3ccSAndroid Build Coastguard Worker // The parameter is parsed as follows:
30*77c1e3ccSAndroid Build Coastguard Worker //
31*77c1e3ccSAndroid Build Coastguard Worker //
32*77c1e3ccSAndroid Build Coastguard Worker // Extra Variables
33*77c1e3ccSAndroid Build Coastguard Worker // ---------------
34*77c1e3ccSAndroid Build Coastguard Worker // This example maintains the frame number passed on the command line
35*77c1e3ccSAndroid Build Coastguard Worker // in the `update_frame_num` variable.
36*77c1e3ccSAndroid Build Coastguard Worker //
37*77c1e3ccSAndroid Build Coastguard Worker //
38*77c1e3ccSAndroid Build Coastguard Worker // Configuration
39*77c1e3ccSAndroid Build Coastguard Worker // -------------
40*77c1e3ccSAndroid Build Coastguard Worker //
41*77c1e3ccSAndroid Build Coastguard Worker // The reference frame is updated on the frame specified on the command
42*77c1e3ccSAndroid Build Coastguard Worker // line.
43*77c1e3ccSAndroid Build Coastguard Worker //
44*77c1e3ccSAndroid Build Coastguard Worker // Observing The Effects
45*77c1e3ccSAndroid Build Coastguard Worker // ---------------------
46*77c1e3ccSAndroid Build Coastguard Worker // The encoder and decoder results should be matching when the same reference
47*77c1e3ccSAndroid Build Coastguard Worker // frame setting operation is done in both encoder and decoder. Otherwise,
48*77c1e3ccSAndroid Build Coastguard Worker // the encoder/decoder mismatch would be seen.
49*77c1e3ccSAndroid Build Coastguard Worker
50*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
51*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
52*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
53*77c1e3ccSAndroid Build Coastguard Worker
54*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_decoder.h"
55*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
56*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomcx.h"
57*77c1e3ccSAndroid Build Coastguard Worker #include "aom_scale/yv12config.h"
58*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
59*77c1e3ccSAndroid Build Coastguard Worker #include "common/video_writer.h"
60*77c1e3ccSAndroid Build Coastguard Worker #include "examples/encoder_util.h"
61*77c1e3ccSAndroid Build Coastguard Worker
62*77c1e3ccSAndroid Build Coastguard Worker static const char *exec_name;
63*77c1e3ccSAndroid Build Coastguard Worker
usage_exit(void)64*77c1e3ccSAndroid Build Coastguard Worker void usage_exit(void) {
65*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr,
66*77c1e3ccSAndroid Build Coastguard Worker "Usage: %s <codec> <width> <height> <infile> <outfile> "
67*77c1e3ccSAndroid Build Coastguard Worker "<frame> <limit(optional)>\n",
68*77c1e3ccSAndroid Build Coastguard Worker exec_name);
69*77c1e3ccSAndroid Build Coastguard Worker exit(EXIT_FAILURE);
70*77c1e3ccSAndroid Build Coastguard Worker }
71*77c1e3ccSAndroid Build Coastguard Worker
testing_decode(aom_codec_ctx_t * encoder,aom_codec_ctx_t * decoder,unsigned int frame_out,int * mismatch_seen)72*77c1e3ccSAndroid Build Coastguard Worker static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
73*77c1e3ccSAndroid Build Coastguard Worker unsigned int frame_out, int *mismatch_seen) {
74*77c1e3ccSAndroid Build Coastguard Worker aom_image_t enc_img, dec_img;
75*77c1e3ccSAndroid Build Coastguard Worker
76*77c1e3ccSAndroid Build Coastguard Worker if (*mismatch_seen) return;
77*77c1e3ccSAndroid Build Coastguard Worker
78*77c1e3ccSAndroid Build Coastguard Worker /* Get the internal reference frame */
79*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_control(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img))
80*77c1e3ccSAndroid Build Coastguard Worker die_codec(encoder, "Failed to get encoder reference frame");
81*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_control(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img))
82*77c1e3ccSAndroid Build Coastguard Worker die_codec(decoder, "Failed to get decoder reference frame");
83*77c1e3ccSAndroid Build Coastguard Worker
84*77c1e3ccSAndroid Build Coastguard Worker if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
85*77c1e3ccSAndroid Build Coastguard Worker (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
86*77c1e3ccSAndroid Build Coastguard Worker if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
87*77c1e3ccSAndroid Build Coastguard Worker aom_image_t enc_hbd_img;
88*77c1e3ccSAndroid Build Coastguard Worker aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
89*77c1e3ccSAndroid Build Coastguard Worker enc_img.d_w, enc_img.d_h, 16);
90*77c1e3ccSAndroid Build Coastguard Worker aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
91*77c1e3ccSAndroid Build Coastguard Worker enc_img = enc_hbd_img;
92*77c1e3ccSAndroid Build Coastguard Worker }
93*77c1e3ccSAndroid Build Coastguard Worker if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
94*77c1e3ccSAndroid Build Coastguard Worker aom_image_t dec_hbd_img;
95*77c1e3ccSAndroid Build Coastguard Worker aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
96*77c1e3ccSAndroid Build Coastguard Worker dec_img.d_w, dec_img.d_h, 16);
97*77c1e3ccSAndroid Build Coastguard Worker aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
98*77c1e3ccSAndroid Build Coastguard Worker dec_img = dec_hbd_img;
99*77c1e3ccSAndroid Build Coastguard Worker }
100*77c1e3ccSAndroid Build Coastguard Worker }
101*77c1e3ccSAndroid Build Coastguard Worker
102*77c1e3ccSAndroid Build Coastguard Worker if (!aom_compare_img(&enc_img, &dec_img)) {
103*77c1e3ccSAndroid Build Coastguard Worker int y[4], u[4], v[4];
104*77c1e3ccSAndroid Build Coastguard Worker if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
105*77c1e3ccSAndroid Build Coastguard Worker aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
106*77c1e3ccSAndroid Build Coastguard Worker } else {
107*77c1e3ccSAndroid Build Coastguard Worker aom_find_mismatch(&enc_img, &dec_img, y, u, v);
108*77c1e3ccSAndroid Build Coastguard Worker }
109*77c1e3ccSAndroid Build Coastguard Worker
110*77c1e3ccSAndroid Build Coastguard Worker printf(
111*77c1e3ccSAndroid Build Coastguard Worker "Encode/decode mismatch on frame %u at"
112*77c1e3ccSAndroid Build Coastguard Worker " Y[%d, %d] {%d/%d},"
113*77c1e3ccSAndroid Build Coastguard Worker " U[%d, %d] {%d/%d},"
114*77c1e3ccSAndroid Build Coastguard Worker " V[%d, %d] {%d/%d}",
115*77c1e3ccSAndroid Build Coastguard Worker frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
116*77c1e3ccSAndroid Build Coastguard Worker v[2], v[3]);
117*77c1e3ccSAndroid Build Coastguard Worker *mismatch_seen = 1;
118*77c1e3ccSAndroid Build Coastguard Worker }
119*77c1e3ccSAndroid Build Coastguard Worker
120*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(&enc_img);
121*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(&dec_img);
122*77c1e3ccSAndroid Build Coastguard Worker }
123*77c1e3ccSAndroid Build Coastguard Worker
encode_frame(aom_codec_ctx_t * ecodec,aom_image_t * img,unsigned int frame_in,AvxVideoWriter * writer,int test_decode,aom_codec_ctx_t * dcodec,unsigned int * frame_out,int * mismatch_seen,aom_image_t * ext_ref)124*77c1e3ccSAndroid Build Coastguard Worker static int encode_frame(aom_codec_ctx_t *ecodec, aom_image_t *img,
125*77c1e3ccSAndroid Build Coastguard Worker unsigned int frame_in, AvxVideoWriter *writer,
126*77c1e3ccSAndroid Build Coastguard Worker int test_decode, aom_codec_ctx_t *dcodec,
127*77c1e3ccSAndroid Build Coastguard Worker unsigned int *frame_out, int *mismatch_seen,
128*77c1e3ccSAndroid Build Coastguard Worker aom_image_t *ext_ref) {
129*77c1e3ccSAndroid Build Coastguard Worker int got_pkts = 0;
130*77c1e3ccSAndroid Build Coastguard Worker aom_codec_iter_t iter = NULL;
131*77c1e3ccSAndroid Build Coastguard Worker const aom_codec_cx_pkt_t *pkt = NULL;
132*77c1e3ccSAndroid Build Coastguard Worker int got_data;
133*77c1e3ccSAndroid Build Coastguard Worker const aom_codec_err_t res = aom_codec_encode(ecodec, img, frame_in, 1, 0);
134*77c1e3ccSAndroid Build Coastguard Worker if (res != AOM_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
135*77c1e3ccSAndroid Build Coastguard Worker
136*77c1e3ccSAndroid Build Coastguard Worker got_data = 0;
137*77c1e3ccSAndroid Build Coastguard Worker
138*77c1e3ccSAndroid Build Coastguard Worker while ((pkt = aom_codec_get_cx_data(ecodec, &iter)) != NULL) {
139*77c1e3ccSAndroid Build Coastguard Worker got_pkts = 1;
140*77c1e3ccSAndroid Build Coastguard Worker
141*77c1e3ccSAndroid Build Coastguard Worker if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
142*77c1e3ccSAndroid Build Coastguard Worker const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
143*77c1e3ccSAndroid Build Coastguard Worker
144*77c1e3ccSAndroid Build Coastguard Worker ++*frame_out;
145*77c1e3ccSAndroid Build Coastguard Worker
146*77c1e3ccSAndroid Build Coastguard Worker if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
147*77c1e3ccSAndroid Build Coastguard Worker pkt->data.frame.sz,
148*77c1e3ccSAndroid Build Coastguard Worker pkt->data.frame.pts)) {
149*77c1e3ccSAndroid Build Coastguard Worker die_codec(ecodec, "Failed to write compressed frame");
150*77c1e3ccSAndroid Build Coastguard Worker }
151*77c1e3ccSAndroid Build Coastguard Worker printf(keyframe ? "K" : ".");
152*77c1e3ccSAndroid Build Coastguard Worker fflush(stdout);
153*77c1e3ccSAndroid Build Coastguard Worker got_data = 1;
154*77c1e3ccSAndroid Build Coastguard Worker
155*77c1e3ccSAndroid Build Coastguard Worker // Decode 1 frame.
156*77c1e3ccSAndroid Build Coastguard Worker if (test_decode) {
157*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_decode(dcodec, pkt->data.frame.buf,
158*77c1e3ccSAndroid Build Coastguard Worker (unsigned int)pkt->data.frame.sz, NULL))
159*77c1e3ccSAndroid Build Coastguard Worker die_codec(dcodec, "Failed to decode frame.");
160*77c1e3ccSAndroid Build Coastguard Worker
161*77c1e3ccSAndroid Build Coastguard Worker // Copy out first decoded frame, and use it as reference later.
162*77c1e3ccSAndroid Build Coastguard Worker if (*frame_out == 1 && ext_ref != NULL)
163*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_control(dcodec, AV1_COPY_NEW_FRAME_IMAGE, ext_ref))
164*77c1e3ccSAndroid Build Coastguard Worker die_codec(dcodec, "Failed to get decoder new frame");
165*77c1e3ccSAndroid Build Coastguard Worker }
166*77c1e3ccSAndroid Build Coastguard Worker }
167*77c1e3ccSAndroid Build Coastguard Worker }
168*77c1e3ccSAndroid Build Coastguard Worker
169*77c1e3ccSAndroid Build Coastguard Worker // Mismatch checking
170*77c1e3ccSAndroid Build Coastguard Worker if (got_data && test_decode) {
171*77c1e3ccSAndroid Build Coastguard Worker testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
172*77c1e3ccSAndroid Build Coastguard Worker }
173*77c1e3ccSAndroid Build Coastguard Worker
174*77c1e3ccSAndroid Build Coastguard Worker return got_pkts;
175*77c1e3ccSAndroid Build Coastguard Worker }
176*77c1e3ccSAndroid Build Coastguard Worker
main(int argc,char ** argv)177*77c1e3ccSAndroid Build Coastguard Worker int main(int argc, char **argv) {
178*77c1e3ccSAndroid Build Coastguard Worker FILE *infile = NULL;
179*77c1e3ccSAndroid Build Coastguard Worker // Encoder
180*77c1e3ccSAndroid Build Coastguard Worker aom_codec_ctx_t ecodec;
181*77c1e3ccSAndroid Build Coastguard Worker aom_codec_enc_cfg_t cfg;
182*77c1e3ccSAndroid Build Coastguard Worker unsigned int frame_in = 0;
183*77c1e3ccSAndroid Build Coastguard Worker aom_image_t raw;
184*77c1e3ccSAndroid Build Coastguard Worker aom_image_t raw_shift;
185*77c1e3ccSAndroid Build Coastguard Worker aom_image_t ext_ref;
186*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t res;
187*77c1e3ccSAndroid Build Coastguard Worker AvxVideoInfo info;
188*77c1e3ccSAndroid Build Coastguard Worker AvxVideoWriter *writer = NULL;
189*77c1e3ccSAndroid Build Coastguard Worker int flags = 0;
190*77c1e3ccSAndroid Build Coastguard Worker int allocated_raw_shift = 0;
191*77c1e3ccSAndroid Build Coastguard Worker aom_img_fmt_t raw_fmt = AOM_IMG_FMT_I420;
192*77c1e3ccSAndroid Build Coastguard Worker aom_img_fmt_t ref_fmt = AOM_IMG_FMT_I420;
193*77c1e3ccSAndroid Build Coastguard Worker
194*77c1e3ccSAndroid Build Coastguard Worker // Test encoder/decoder mismatch.
195*77c1e3ccSAndroid Build Coastguard Worker int test_decode = 1;
196*77c1e3ccSAndroid Build Coastguard Worker // Decoder
197*77c1e3ccSAndroid Build Coastguard Worker aom_codec_ctx_t dcodec;
198*77c1e3ccSAndroid Build Coastguard Worker unsigned int frame_out = 0;
199*77c1e3ccSAndroid Build Coastguard Worker
200*77c1e3ccSAndroid Build Coastguard Worker // The frame number to set reference frame on
201*77c1e3ccSAndroid Build Coastguard Worker unsigned int update_frame_num = 0;
202*77c1e3ccSAndroid Build Coastguard Worker int mismatch_seen = 0;
203*77c1e3ccSAndroid Build Coastguard Worker
204*77c1e3ccSAndroid Build Coastguard Worker const int fps = 30;
205*77c1e3ccSAndroid Build Coastguard Worker const int bitrate = 500;
206*77c1e3ccSAndroid Build Coastguard Worker
207*77c1e3ccSAndroid Build Coastguard Worker const char *codec_arg = NULL;
208*77c1e3ccSAndroid Build Coastguard Worker const char *width_arg = NULL;
209*77c1e3ccSAndroid Build Coastguard Worker const char *height_arg = NULL;
210*77c1e3ccSAndroid Build Coastguard Worker const char *infile_arg = NULL;
211*77c1e3ccSAndroid Build Coastguard Worker const char *outfile_arg = NULL;
212*77c1e3ccSAndroid Build Coastguard Worker const char *update_frame_num_arg = NULL;
213*77c1e3ccSAndroid Build Coastguard Worker unsigned int limit = 0;
214*77c1e3ccSAndroid Build Coastguard Worker exec_name = argv[0];
215*77c1e3ccSAndroid Build Coastguard Worker
216*77c1e3ccSAndroid Build Coastguard Worker // Clear explicitly, as simply assigning "{ 0 }" generates
217*77c1e3ccSAndroid Build Coastguard Worker // "missing-field-initializers" warning in some compilers.
218*77c1e3ccSAndroid Build Coastguard Worker memset(&ecodec, 0, sizeof(ecodec));
219*77c1e3ccSAndroid Build Coastguard Worker memset(&cfg, 0, sizeof(cfg));
220*77c1e3ccSAndroid Build Coastguard Worker memset(&info, 0, sizeof(info));
221*77c1e3ccSAndroid Build Coastguard Worker
222*77c1e3ccSAndroid Build Coastguard Worker if (argc < 7) die("Invalid number of arguments");
223*77c1e3ccSAndroid Build Coastguard Worker
224*77c1e3ccSAndroid Build Coastguard Worker codec_arg = argv[1];
225*77c1e3ccSAndroid Build Coastguard Worker width_arg = argv[2];
226*77c1e3ccSAndroid Build Coastguard Worker height_arg = argv[3];
227*77c1e3ccSAndroid Build Coastguard Worker infile_arg = argv[4];
228*77c1e3ccSAndroid Build Coastguard Worker outfile_arg = argv[5];
229*77c1e3ccSAndroid Build Coastguard Worker update_frame_num_arg = argv[6];
230*77c1e3ccSAndroid Build Coastguard Worker
231*77c1e3ccSAndroid Build Coastguard Worker aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg);
232*77c1e3ccSAndroid Build Coastguard Worker if (!encoder) die("Unsupported codec.");
233*77c1e3ccSAndroid Build Coastguard Worker
234*77c1e3ccSAndroid Build Coastguard Worker update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
235*77c1e3ccSAndroid Build Coastguard Worker // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
236*77c1e3ccSAndroid Build Coastguard Worker // allocated while calling aom_codec_encode(), thus, setting reference for
237*77c1e3ccSAndroid Build Coastguard Worker // 1st frame isn't supported.
238*77c1e3ccSAndroid Build Coastguard Worker if (update_frame_num <= 1) {
239*77c1e3ccSAndroid Build Coastguard Worker die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
240*77c1e3ccSAndroid Build Coastguard Worker }
241*77c1e3ccSAndroid Build Coastguard Worker
242*77c1e3ccSAndroid Build Coastguard Worker if (argc > 7) {
243*77c1e3ccSAndroid Build Coastguard Worker limit = (unsigned int)strtoul(argv[7], NULL, 0);
244*77c1e3ccSAndroid Build Coastguard Worker if (update_frame_num > limit)
245*77c1e3ccSAndroid Build Coastguard Worker die("Update frame number couldn't larger than limit\n");
246*77c1e3ccSAndroid Build Coastguard Worker }
247*77c1e3ccSAndroid Build Coastguard Worker
248*77c1e3ccSAndroid Build Coastguard Worker info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
249*77c1e3ccSAndroid Build Coastguard Worker info.frame_width = (int)strtol(width_arg, NULL, 0);
250*77c1e3ccSAndroid Build Coastguard Worker info.frame_height = (int)strtol(height_arg, NULL, 0);
251*77c1e3ccSAndroid Build Coastguard Worker info.time_base.numerator = 1;
252*77c1e3ccSAndroid Build Coastguard Worker info.time_base.denominator = fps;
253*77c1e3ccSAndroid Build Coastguard Worker
254*77c1e3ccSAndroid Build Coastguard Worker if (info.frame_width <= 0 || info.frame_height <= 0) {
255*77c1e3ccSAndroid Build Coastguard Worker die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
256*77c1e3ccSAndroid Build Coastguard Worker }
257*77c1e3ccSAndroid Build Coastguard Worker
258*77c1e3ccSAndroid Build Coastguard Worker // In this test, the bit depth of input video is 8-bit, and the input format
259*77c1e3ccSAndroid Build Coastguard Worker // is AOM_IMG_FMT_I420.
260*77c1e3ccSAndroid Build Coastguard Worker if (!aom_img_alloc(&raw, raw_fmt, info.frame_width, info.frame_height, 32)) {
261*77c1e3ccSAndroid Build Coastguard Worker die("Failed to allocate image.");
262*77c1e3ccSAndroid Build Coastguard Worker }
263*77c1e3ccSAndroid Build Coastguard Worker
264*77c1e3ccSAndroid Build Coastguard Worker if (FORCE_HIGHBITDEPTH_DECODING) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
265*77c1e3ccSAndroid Build Coastguard Worker // Allocate memory with the border so that it can be used as a reference.
266*77c1e3ccSAndroid Build Coastguard Worker if (!aom_img_alloc_with_border(&ext_ref, ref_fmt, info.frame_width,
267*77c1e3ccSAndroid Build Coastguard Worker info.frame_height, 32, 8,
268*77c1e3ccSAndroid Build Coastguard Worker AOM_DEC_BORDER_IN_PIXELS)) {
269*77c1e3ccSAndroid Build Coastguard Worker die("Failed to allocate image.");
270*77c1e3ccSAndroid Build Coastguard Worker }
271*77c1e3ccSAndroid Build Coastguard Worker
272*77c1e3ccSAndroid Build Coastguard Worker printf("Using %s\n", aom_codec_iface_name(encoder));
273*77c1e3ccSAndroid Build Coastguard Worker
274*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
275*77c1e3ccSAndroid Build Coastguard Worker res = aom_codec_enc_config_default(encoder, &cfg, 1);
276*77c1e3ccSAndroid Build Coastguard Worker #else
277*77c1e3ccSAndroid Build Coastguard Worker res = aom_codec_enc_config_default(encoder, &cfg, 0);
278*77c1e3ccSAndroid Build Coastguard Worker #endif
279*77c1e3ccSAndroid Build Coastguard Worker if (res) die_codec(&ecodec, "Failed to get default codec config.");
280*77c1e3ccSAndroid Build Coastguard Worker
281*77c1e3ccSAndroid Build Coastguard Worker cfg.g_w = info.frame_width;
282*77c1e3ccSAndroid Build Coastguard Worker cfg.g_h = info.frame_height;
283*77c1e3ccSAndroid Build Coastguard Worker cfg.g_timebase.num = info.time_base.numerator;
284*77c1e3ccSAndroid Build Coastguard Worker cfg.g_timebase.den = info.time_base.denominator;
285*77c1e3ccSAndroid Build Coastguard Worker cfg.rc_target_bitrate = bitrate;
286*77c1e3ccSAndroid Build Coastguard Worker cfg.g_lag_in_frames = 3;
287*77c1e3ccSAndroid Build Coastguard Worker cfg.g_bit_depth = AOM_BITS_8;
288*77c1e3ccSAndroid Build Coastguard Worker
289*77c1e3ccSAndroid Build Coastguard Worker flags |= (cfg.g_bit_depth > AOM_BITS_8 || FORCE_HIGHBITDEPTH_DECODING)
290*77c1e3ccSAndroid Build Coastguard Worker ? AOM_CODEC_USE_HIGHBITDEPTH
291*77c1e3ccSAndroid Build Coastguard Worker : 0;
292*77c1e3ccSAndroid Build Coastguard Worker
293*77c1e3ccSAndroid Build Coastguard Worker writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
294*77c1e3ccSAndroid Build Coastguard Worker if (!writer) die("Failed to open %s for writing.", outfile_arg);
295*77c1e3ccSAndroid Build Coastguard Worker
296*77c1e3ccSAndroid Build Coastguard Worker if (!(infile = fopen(infile_arg, "rb")))
297*77c1e3ccSAndroid Build Coastguard Worker die("Failed to open %s for reading.", infile_arg);
298*77c1e3ccSAndroid Build Coastguard Worker
299*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_enc_init(&ecodec, encoder, &cfg, flags))
300*77c1e3ccSAndroid Build Coastguard Worker die("Failed to initialize encoder");
301*77c1e3ccSAndroid Build Coastguard Worker
302*77c1e3ccSAndroid Build Coastguard Worker // Disable alt_ref.
303*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0))
304*77c1e3ccSAndroid Build Coastguard Worker die_codec(&ecodec, "Failed to set enable auto alt ref");
305*77c1e3ccSAndroid Build Coastguard Worker
306*77c1e3ccSAndroid Build Coastguard Worker if (test_decode) {
307*77c1e3ccSAndroid Build Coastguard Worker aom_codec_iface_t *decoder = get_aom_decoder_by_short_name(codec_arg);
308*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_dec_init(&dcodec, decoder, NULL, 0))
309*77c1e3ccSAndroid Build Coastguard Worker die("Failed to initialize decoder.");
310*77c1e3ccSAndroid Build Coastguard Worker }
311*77c1e3ccSAndroid Build Coastguard Worker
312*77c1e3ccSAndroid Build Coastguard Worker // Encode frames.
313*77c1e3ccSAndroid Build Coastguard Worker while (aom_img_read(&raw, infile)) {
314*77c1e3ccSAndroid Build Coastguard Worker if (limit && frame_in >= limit) break;
315*77c1e3ccSAndroid Build Coastguard Worker aom_image_t *frame_to_encode;
316*77c1e3ccSAndroid Build Coastguard Worker
317*77c1e3ccSAndroid Build Coastguard Worker if (FORCE_HIGHBITDEPTH_DECODING) {
318*77c1e3ccSAndroid Build Coastguard Worker // Need to allocate larger buffer to use hbd internal.
319*77c1e3ccSAndroid Build Coastguard Worker int input_shift = 0;
320*77c1e3ccSAndroid Build Coastguard Worker if (!allocated_raw_shift) {
321*77c1e3ccSAndroid Build Coastguard Worker aom_img_alloc(&raw_shift, raw_fmt | AOM_IMG_FMT_HIGHBITDEPTH,
322*77c1e3ccSAndroid Build Coastguard Worker info.frame_width, info.frame_height, 32);
323*77c1e3ccSAndroid Build Coastguard Worker allocated_raw_shift = 1;
324*77c1e3ccSAndroid Build Coastguard Worker }
325*77c1e3ccSAndroid Build Coastguard Worker aom_img_upshift(&raw_shift, &raw, input_shift);
326*77c1e3ccSAndroid Build Coastguard Worker frame_to_encode = &raw_shift;
327*77c1e3ccSAndroid Build Coastguard Worker } else {
328*77c1e3ccSAndroid Build Coastguard Worker frame_to_encode = &raw;
329*77c1e3ccSAndroid Build Coastguard Worker }
330*77c1e3ccSAndroid Build Coastguard Worker
331*77c1e3ccSAndroid Build Coastguard Worker if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
332*77c1e3ccSAndroid Build Coastguard Worker av1_ref_frame_t ref;
333*77c1e3ccSAndroid Build Coastguard Worker ref.idx = 0;
334*77c1e3ccSAndroid Build Coastguard Worker ref.use_external_ref = 0;
335*77c1e3ccSAndroid Build Coastguard Worker ref.img = ext_ref;
336*77c1e3ccSAndroid Build Coastguard Worker // Set reference frame in encoder.
337*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref))
338*77c1e3ccSAndroid Build Coastguard Worker die_codec(&ecodec, "Failed to set encoder reference frame");
339*77c1e3ccSAndroid Build Coastguard Worker printf(" <SET_REF>");
340*77c1e3ccSAndroid Build Coastguard Worker
341*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
342*77c1e3ccSAndroid Build Coastguard Worker // Set cpu speed in encoder.
343*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_control(&ecodec, AOME_SET_CPUUSED, 7))
344*77c1e3ccSAndroid Build Coastguard Worker die_codec(&ecodec, "Failed to set cpu speed");
345*77c1e3ccSAndroid Build Coastguard Worker #endif
346*77c1e3ccSAndroid Build Coastguard Worker
347*77c1e3ccSAndroid Build Coastguard Worker // If set_reference in decoder is commented out, the enc/dec mismatch
348*77c1e3ccSAndroid Build Coastguard Worker // would be seen.
349*77c1e3ccSAndroid Build Coastguard Worker if (test_decode) {
350*77c1e3ccSAndroid Build Coastguard Worker ref.use_external_ref = 1;
351*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref))
352*77c1e3ccSAndroid Build Coastguard Worker die_codec(&dcodec, "Failed to set decoder reference frame");
353*77c1e3ccSAndroid Build Coastguard Worker }
354*77c1e3ccSAndroid Build Coastguard Worker }
355*77c1e3ccSAndroid Build Coastguard Worker
356*77c1e3ccSAndroid Build Coastguard Worker encode_frame(&ecodec, frame_to_encode, frame_in, writer, test_decode,
357*77c1e3ccSAndroid Build Coastguard Worker &dcodec, &frame_out, &mismatch_seen, &ext_ref);
358*77c1e3ccSAndroid Build Coastguard Worker frame_in++;
359*77c1e3ccSAndroid Build Coastguard Worker if (mismatch_seen) break;
360*77c1e3ccSAndroid Build Coastguard Worker }
361*77c1e3ccSAndroid Build Coastguard Worker
362*77c1e3ccSAndroid Build Coastguard Worker // Flush encoder.
363*77c1e3ccSAndroid Build Coastguard Worker if (!mismatch_seen)
364*77c1e3ccSAndroid Build Coastguard Worker while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
365*77c1e3ccSAndroid Build Coastguard Worker &frame_out, &mismatch_seen, NULL)) {
366*77c1e3ccSAndroid Build Coastguard Worker }
367*77c1e3ccSAndroid Build Coastguard Worker
368*77c1e3ccSAndroid Build Coastguard Worker printf("\n");
369*77c1e3ccSAndroid Build Coastguard Worker fclose(infile);
370*77c1e3ccSAndroid Build Coastguard Worker printf("Processed %u frames.\n", frame_out);
371*77c1e3ccSAndroid Build Coastguard Worker
372*77c1e3ccSAndroid Build Coastguard Worker if (test_decode) {
373*77c1e3ccSAndroid Build Coastguard Worker if (!mismatch_seen)
374*77c1e3ccSAndroid Build Coastguard Worker printf("Encoder/decoder results are matching.\n");
375*77c1e3ccSAndroid Build Coastguard Worker else
376*77c1e3ccSAndroid Build Coastguard Worker printf("Encoder/decoder results are NOT matching.\n");
377*77c1e3ccSAndroid Build Coastguard Worker }
378*77c1e3ccSAndroid Build Coastguard Worker
379*77c1e3ccSAndroid Build Coastguard Worker if (test_decode)
380*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_destroy(&dcodec))
381*77c1e3ccSAndroid Build Coastguard Worker die_codec(&dcodec, "Failed to destroy decoder");
382*77c1e3ccSAndroid Build Coastguard Worker
383*77c1e3ccSAndroid Build Coastguard Worker if (allocated_raw_shift) aom_img_free(&raw_shift);
384*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(&ext_ref);
385*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(&raw);
386*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_destroy(&ecodec))
387*77c1e3ccSAndroid Build Coastguard Worker die_codec(&ecodec, "Failed to destroy encoder.");
388*77c1e3ccSAndroid Build Coastguard Worker
389*77c1e3ccSAndroid Build Coastguard Worker aom_video_writer_close(writer);
390*77c1e3ccSAndroid Build Coastguard Worker
391*77c1e3ccSAndroid Build Coastguard Worker return EXIT_SUCCESS;
392*77c1e3ccSAndroid Build Coastguard Worker }
393