xref: /aosp_15_r20/external/libaom/examples/scalable_encoder.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2018, 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 // Scalable Encoder
13*77c1e3ccSAndroid Build Coastguard Worker // ==============
14*77c1e3ccSAndroid Build Coastguard Worker //
15*77c1e3ccSAndroid Build Coastguard Worker // This is an example of a scalable encoder loop. It takes two input files in
16*77c1e3ccSAndroid Build Coastguard Worker // YV12 format, passes it through the encoder, and writes the compressed
17*77c1e3ccSAndroid Build Coastguard Worker // frames to disk in OBU format.
18*77c1e3ccSAndroid Build Coastguard Worker //
19*77c1e3ccSAndroid Build Coastguard Worker // Getting The Default Configuration
20*77c1e3ccSAndroid Build Coastguard Worker // ---------------------------------
21*77c1e3ccSAndroid Build Coastguard Worker // Encoders have the notion of "usage profiles." For example, an encoder
22*77c1e3ccSAndroid Build Coastguard Worker // may want to publish default configurations for both a video
23*77c1e3ccSAndroid Build Coastguard Worker // conferencing application and a best quality offline encoder. These
24*77c1e3ccSAndroid Build Coastguard Worker // obviously have very different default settings. Consult the
25*77c1e3ccSAndroid Build Coastguard Worker // documentation for your codec to see if it provides any default
26*77c1e3ccSAndroid Build Coastguard Worker // configurations. All codecs provide a default configuration, number 0,
27*77c1e3ccSAndroid Build Coastguard Worker // which is valid for material in the vacinity of QCIF/QVGA.
28*77c1e3ccSAndroid Build Coastguard Worker //
29*77c1e3ccSAndroid Build Coastguard Worker // Updating The Configuration
30*77c1e3ccSAndroid Build Coastguard Worker // ---------------------------------
31*77c1e3ccSAndroid Build Coastguard Worker // Almost all applications will want to update the default configuration
32*77c1e3ccSAndroid Build Coastguard Worker // with settings specific to their usage. Here we set the width and height
33*77c1e3ccSAndroid Build Coastguard Worker // of the video file to that specified on the command line. We also scale
34*77c1e3ccSAndroid Build Coastguard Worker // the default bitrate based on the ratio between the default resolution
35*77c1e3ccSAndroid Build Coastguard Worker // and the resolution specified on the command line.
36*77c1e3ccSAndroid Build Coastguard Worker //
37*77c1e3ccSAndroid Build Coastguard Worker // Encoding A Frame
38*77c1e3ccSAndroid Build Coastguard Worker // ----------------
39*77c1e3ccSAndroid Build Coastguard Worker // The frame is read as a continuous block (size = width * height * 3 / 2)
40*77c1e3ccSAndroid Build Coastguard Worker // from the input file. If a frame was read (the input file has not hit
41*77c1e3ccSAndroid Build Coastguard Worker // EOF) then the frame is passed to the encoder. Otherwise, a NULL
42*77c1e3ccSAndroid Build Coastguard Worker // is passed, indicating the End-Of-Stream condition to the encoder. The
43*77c1e3ccSAndroid Build Coastguard Worker // `frame_cnt` is reused as the presentation time stamp (PTS) and each
44*77c1e3ccSAndroid Build Coastguard Worker // frame is shown for one frame-time in duration. The flags parameter is
45*77c1e3ccSAndroid Build Coastguard Worker // unused in this example.
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker // Forced Keyframes
48*77c1e3ccSAndroid Build Coastguard Worker // ----------------
49*77c1e3ccSAndroid Build Coastguard Worker // Keyframes can be forced by setting the AOM_EFLAG_FORCE_KF bit of the
50*77c1e3ccSAndroid Build Coastguard Worker // flags passed to `aom_codec_control()`. In this example, we force a
51*77c1e3ccSAndroid Build Coastguard Worker // keyframe every <keyframe-interval> frames. Note, the output stream can
52*77c1e3ccSAndroid Build Coastguard Worker // contain additional keyframes beyond those that have been forced using the
53*77c1e3ccSAndroid Build Coastguard Worker // AOM_EFLAG_FORCE_KF flag because of automatic keyframe placement by the
54*77c1e3ccSAndroid Build Coastguard Worker // encoder.
55*77c1e3ccSAndroid Build Coastguard Worker //
56*77c1e3ccSAndroid Build Coastguard Worker // Processing The Encoded Data
57*77c1e3ccSAndroid Build Coastguard Worker // ---------------------------
58*77c1e3ccSAndroid Build Coastguard Worker // Each packet of type `AOM_CODEC_CX_FRAME_PKT` contains the encoded data
59*77c1e3ccSAndroid Build Coastguard Worker // for this frame. We write a IVF frame header, followed by the raw data.
60*77c1e3ccSAndroid Build Coastguard Worker //
61*77c1e3ccSAndroid Build Coastguard Worker // Cleanup
62*77c1e3ccSAndroid Build Coastguard Worker // -------
63*77c1e3ccSAndroid Build Coastguard Worker // The `aom_codec_destroy` call frees any memory allocated by the codec.
64*77c1e3ccSAndroid Build Coastguard Worker //
65*77c1e3ccSAndroid Build Coastguard Worker // Error Handling
66*77c1e3ccSAndroid Build Coastguard Worker // --------------
67*77c1e3ccSAndroid Build Coastguard Worker // This example does not special case any error return codes. If there was
68*77c1e3ccSAndroid Build Coastguard Worker // an error, a descriptive message is printed and the program exits. With
69*77c1e3ccSAndroid Build Coastguard Worker // few exeptions, aom_codec functions return an enumerated error status,
70*77c1e3ccSAndroid Build Coastguard Worker // with the value `0` indicating success.
71*77c1e3ccSAndroid Build Coastguard Worker 
72*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
73*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
74*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
77*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomcx.h"
78*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/enums.h"
79*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
80*77c1e3ccSAndroid Build Coastguard Worker #include "common/video_writer.h"
81*77c1e3ccSAndroid Build Coastguard Worker 
82*77c1e3ccSAndroid Build Coastguard Worker static const char *exec_name;
83*77c1e3ccSAndroid Build Coastguard Worker 
usage_exit(void)84*77c1e3ccSAndroid Build Coastguard Worker void usage_exit(void) {
85*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr,
86*77c1e3ccSAndroid Build Coastguard Worker           "Usage: %s <codec> <width> <height> <infile0> <infile1> "
87*77c1e3ccSAndroid Build Coastguard Worker           "<outfile> <frames to encode>\n"
88*77c1e3ccSAndroid Build Coastguard Worker           "See comments in scalable_encoder.c for more information.\n",
89*77c1e3ccSAndroid Build Coastguard Worker           exec_name);
90*77c1e3ccSAndroid Build Coastguard Worker   exit(EXIT_FAILURE);
91*77c1e3ccSAndroid Build Coastguard Worker }
92*77c1e3ccSAndroid Build Coastguard Worker 
encode_frame(aom_codec_ctx_t * codec,aom_image_t * img,int frame_index,int flags,FILE * outfile)93*77c1e3ccSAndroid Build Coastguard Worker static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
94*77c1e3ccSAndroid Build Coastguard Worker                         int frame_index, int flags, FILE *outfile) {
95*77c1e3ccSAndroid Build Coastguard Worker   int got_pkts = 0;
96*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iter_t iter = NULL;
97*77c1e3ccSAndroid Build Coastguard Worker   const aom_codec_cx_pkt_t *pkt = NULL;
98*77c1e3ccSAndroid Build Coastguard Worker   const aom_codec_err_t res =
99*77c1e3ccSAndroid Build Coastguard Worker       aom_codec_encode(codec, img, frame_index, 1, flags);
100*77c1e3ccSAndroid Build Coastguard Worker   if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
101*77c1e3ccSAndroid Build Coastguard Worker 
102*77c1e3ccSAndroid Build Coastguard Worker   while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
103*77c1e3ccSAndroid Build Coastguard Worker     got_pkts = 1;
104*77c1e3ccSAndroid Build Coastguard Worker 
105*77c1e3ccSAndroid Build Coastguard Worker     if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
106*77c1e3ccSAndroid Build Coastguard Worker       const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
107*77c1e3ccSAndroid Build Coastguard Worker       if (fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile) !=
108*77c1e3ccSAndroid Build Coastguard Worker           pkt->data.frame.sz) {
109*77c1e3ccSAndroid Build Coastguard Worker         die_codec(codec, "Failed to write compressed frame");
110*77c1e3ccSAndroid Build Coastguard Worker       }
111*77c1e3ccSAndroid Build Coastguard Worker       printf(keyframe ? "K" : ".");
112*77c1e3ccSAndroid Build Coastguard Worker       printf(" %6d\n", (int)pkt->data.frame.sz);
113*77c1e3ccSAndroid Build Coastguard Worker       fflush(stdout);
114*77c1e3ccSAndroid Build Coastguard Worker     }
115*77c1e3ccSAndroid Build Coastguard Worker   }
116*77c1e3ccSAndroid Build Coastguard Worker 
117*77c1e3ccSAndroid Build Coastguard Worker   return got_pkts;
118*77c1e3ccSAndroid Build Coastguard Worker }
119*77c1e3ccSAndroid Build Coastguard Worker 
main(int argc,char ** argv)120*77c1e3ccSAndroid Build Coastguard Worker int main(int argc, char **argv) {
121*77c1e3ccSAndroid Build Coastguard Worker   FILE *infile0 = NULL;
122*77c1e3ccSAndroid Build Coastguard Worker   FILE *infile1 = NULL;
123*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_enc_cfg_t cfg;
124*77c1e3ccSAndroid Build Coastguard Worker   int frame_count = 0;
125*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t raw0, raw1;
126*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res;
127*77c1e3ccSAndroid Build Coastguard Worker   AvxVideoInfo info;
128*77c1e3ccSAndroid Build Coastguard Worker   const int fps = 30;
129*77c1e3ccSAndroid Build Coastguard Worker   const int bitrate = 200;
130*77c1e3ccSAndroid Build Coastguard Worker   int keyframe_interval = 0;
131*77c1e3ccSAndroid Build Coastguard Worker   int max_frames = 0;
132*77c1e3ccSAndroid Build Coastguard Worker   int frames_encoded = 0;
133*77c1e3ccSAndroid Build Coastguard Worker   const char *codec_arg = NULL;
134*77c1e3ccSAndroid Build Coastguard Worker   const char *width_arg = NULL;
135*77c1e3ccSAndroid Build Coastguard Worker   const char *height_arg = NULL;
136*77c1e3ccSAndroid Build Coastguard Worker   const char *infile0_arg = NULL;
137*77c1e3ccSAndroid Build Coastguard Worker   const char *infile1_arg = NULL;
138*77c1e3ccSAndroid Build Coastguard Worker   const char *outfile_arg = NULL;
139*77c1e3ccSAndroid Build Coastguard Worker   //  const char *keyframe_interval_arg = NULL;
140*77c1e3ccSAndroid Build Coastguard Worker   FILE *outfile = NULL;
141*77c1e3ccSAndroid Build Coastguard Worker 
142*77c1e3ccSAndroid Build Coastguard Worker   exec_name = argv[0];
143*77c1e3ccSAndroid Build Coastguard Worker 
144*77c1e3ccSAndroid Build Coastguard Worker   // Clear explicitly, as simply assigning "{ 0 }" generates
145*77c1e3ccSAndroid Build Coastguard Worker   // "missing-field-initializers" warning in some compilers.
146*77c1e3ccSAndroid Build Coastguard Worker   memset(&info, 0, sizeof(info));
147*77c1e3ccSAndroid Build Coastguard Worker 
148*77c1e3ccSAndroid Build Coastguard Worker   if (argc != 8) die("Invalid number of arguments");
149*77c1e3ccSAndroid Build Coastguard Worker 
150*77c1e3ccSAndroid Build Coastguard Worker   codec_arg = argv[1];
151*77c1e3ccSAndroid Build Coastguard Worker   width_arg = argv[2];
152*77c1e3ccSAndroid Build Coastguard Worker   height_arg = argv[3];
153*77c1e3ccSAndroid Build Coastguard Worker   infile0_arg = argv[4];
154*77c1e3ccSAndroid Build Coastguard Worker   infile1_arg = argv[5];
155*77c1e3ccSAndroid Build Coastguard Worker   outfile_arg = argv[6];
156*77c1e3ccSAndroid Build Coastguard Worker   max_frames = (int)strtol(argv[7], NULL, 0);
157*77c1e3ccSAndroid Build Coastguard Worker 
158*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg);
159*77c1e3ccSAndroid Build Coastguard Worker   if (!encoder) die("Unsupported codec.");
160*77c1e3ccSAndroid Build Coastguard Worker 
161*77c1e3ccSAndroid Build Coastguard Worker   info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
162*77c1e3ccSAndroid Build Coastguard Worker   info.frame_width = (int)strtol(width_arg, NULL, 0);
163*77c1e3ccSAndroid Build Coastguard Worker   info.frame_height = (int)strtol(height_arg, NULL, 0);
164*77c1e3ccSAndroid Build Coastguard Worker   info.time_base.numerator = 1;
165*77c1e3ccSAndroid Build Coastguard Worker   info.time_base.denominator = fps;
166*77c1e3ccSAndroid Build Coastguard Worker 
167*77c1e3ccSAndroid Build Coastguard Worker   if (info.frame_width <= 0 || info.frame_height <= 0 ||
168*77c1e3ccSAndroid Build Coastguard Worker       (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
169*77c1e3ccSAndroid Build Coastguard Worker     die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
170*77c1e3ccSAndroid Build Coastguard Worker   }
171*77c1e3ccSAndroid Build Coastguard Worker 
172*77c1e3ccSAndroid Build Coastguard Worker   if (!aom_img_alloc(&raw0, AOM_IMG_FMT_I420, info.frame_width,
173*77c1e3ccSAndroid Build Coastguard Worker                      info.frame_height, 1)) {
174*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to allocate image for layer 0.");
175*77c1e3ccSAndroid Build Coastguard Worker   }
176*77c1e3ccSAndroid Build Coastguard Worker   if (!aom_img_alloc(&raw1, AOM_IMG_FMT_I420, info.frame_width,
177*77c1e3ccSAndroid Build Coastguard Worker                      info.frame_height, 1)) {
178*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to allocate image for layer 1.");
179*77c1e3ccSAndroid Build Coastguard Worker   }
180*77c1e3ccSAndroid Build Coastguard Worker 
181*77c1e3ccSAndroid Build Coastguard Worker   //  keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0);
182*77c1e3ccSAndroid Build Coastguard Worker   keyframe_interval = 100;
183*77c1e3ccSAndroid Build Coastguard Worker   if (keyframe_interval < 0) die("Invalid keyframe interval value.");
184*77c1e3ccSAndroid Build Coastguard Worker 
185*77c1e3ccSAndroid Build Coastguard Worker   printf("Using %s\n", aom_codec_iface_name(encoder));
186*77c1e3ccSAndroid Build Coastguard Worker 
187*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_ctx_t codec;
188*77c1e3ccSAndroid Build Coastguard Worker   res = aom_codec_enc_config_default(encoder, &cfg, 0);
189*77c1e3ccSAndroid Build Coastguard Worker   if (res) die_codec(&codec, "Failed to get default codec config.");
190*77c1e3ccSAndroid Build Coastguard Worker 
191*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_w = info.frame_width;
192*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_h = info.frame_height;
193*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_timebase.num = info.time_base.numerator;
194*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_timebase.den = info.time_base.denominator;
195*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_target_bitrate = bitrate;
196*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_error_resilient = 0;
197*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_lag_in_frames = 0;
198*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_end_usage = AOM_Q;
199*77c1e3ccSAndroid Build Coastguard Worker   cfg.save_as_annexb = 0;
200*77c1e3ccSAndroid Build Coastguard Worker 
201*77c1e3ccSAndroid Build Coastguard Worker   outfile = fopen(outfile_arg, "wb");
202*77c1e3ccSAndroid Build Coastguard Worker   if (!outfile) die("Failed to open %s for writing.", outfile_arg);
203*77c1e3ccSAndroid Build Coastguard Worker 
204*77c1e3ccSAndroid Build Coastguard Worker   if (!(infile0 = fopen(infile0_arg, "rb")))
205*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to open %s for reading.", infile0_arg);
206*77c1e3ccSAndroid Build Coastguard Worker   if (!(infile1 = fopen(infile1_arg, "rb")))
207*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to open %s for reading.", infile0_arg);
208*77c1e3ccSAndroid Build Coastguard Worker 
209*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
210*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to initialize encoder");
211*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&codec, AOME_SET_CPUUSED, 8))
212*77c1e3ccSAndroid Build Coastguard Worker     die_codec(&codec, "Failed to set cpu to 8");
213*77c1e3ccSAndroid Build Coastguard Worker 
214*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, 2))
215*77c1e3ccSAndroid Build Coastguard Worker     die_codec(&codec, "Failed to set tile columns to 2");
216*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&codec, AV1E_SET_NUM_TG, 3))
217*77c1e3ccSAndroid Build Coastguard Worker     die_codec(&codec, "Failed to set num of tile groups to 3");
218*77c1e3ccSAndroid Build Coastguard Worker 
219*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&codec, AOME_SET_NUMBER_SPATIAL_LAYERS, 2))
220*77c1e3ccSAndroid Build Coastguard Worker     die_codec(&codec, "Failed to set number of spatial layers to 2");
221*77c1e3ccSAndroid Build Coastguard Worker 
222*77c1e3ccSAndroid Build Coastguard Worker   // Encode frames.
223*77c1e3ccSAndroid Build Coastguard Worker   while (aom_img_read(&raw0, infile0)) {
224*77c1e3ccSAndroid Build Coastguard Worker     int flags = 0;
225*77c1e3ccSAndroid Build Coastguard Worker 
226*77c1e3ccSAndroid Build Coastguard Worker     // configure and encode base layer
227*77c1e3ccSAndroid Build Coastguard Worker 
228*77c1e3ccSAndroid Build Coastguard Worker     if (keyframe_interval > 0 && frames_encoded % keyframe_interval == 0)
229*77c1e3ccSAndroid Build Coastguard Worker       flags |= AOM_EFLAG_FORCE_KF;
230*77c1e3ccSAndroid Build Coastguard Worker     else
231*77c1e3ccSAndroid Build Coastguard Worker       // use previous base layer (LAST) as sole reference
232*77c1e3ccSAndroid Build Coastguard Worker       // save this frame as LAST to be used as reference by enhanmcent layer
233*77c1e3ccSAndroid Build Coastguard Worker       // and next base layer
234*77c1e3ccSAndroid Build Coastguard Worker       flags |= AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
235*77c1e3ccSAndroid Build Coastguard Worker                AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
236*77c1e3ccSAndroid Build Coastguard Worker                AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 |
237*77c1e3ccSAndroid Build Coastguard Worker                AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
238*77c1e3ccSAndroid Build Coastguard Worker                AOM_EFLAG_NO_UPD_ENTROPY;
239*77c1e3ccSAndroid Build Coastguard Worker     cfg.g_w = info.frame_width;
240*77c1e3ccSAndroid Build Coastguard Worker     cfg.g_h = info.frame_height;
241*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_enc_config_set(&codec, &cfg))
242*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&codec, "Failed to set enc cfg for layer 0");
243*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_control(&codec, AOME_SET_SPATIAL_LAYER_ID, 0))
244*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&codec, "Failed to set layer id to 0");
245*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_control(&codec, AOME_SET_CQ_LEVEL, 62))
246*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&codec, "Failed to set cq level");
247*77c1e3ccSAndroid Build Coastguard Worker     encode_frame(&codec, &raw0, frame_count++, flags, outfile);
248*77c1e3ccSAndroid Build Coastguard Worker 
249*77c1e3ccSAndroid Build Coastguard Worker     // configure and encode enhancement layer
250*77c1e3ccSAndroid Build Coastguard Worker 
251*77c1e3ccSAndroid Build Coastguard Worker     //  use LAST (base layer) as sole reference
252*77c1e3ccSAndroid Build Coastguard Worker     flags = AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
253*77c1e3ccSAndroid Build Coastguard Worker             AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
254*77c1e3ccSAndroid Build Coastguard Worker             AOM_EFLAG_NO_REF_ARF2 | AOM_EFLAG_NO_UPD_LAST |
255*77c1e3ccSAndroid Build Coastguard Worker             AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
256*77c1e3ccSAndroid Build Coastguard Worker             AOM_EFLAG_NO_UPD_ENTROPY;
257*77c1e3ccSAndroid Build Coastguard Worker     cfg.g_w = info.frame_width;
258*77c1e3ccSAndroid Build Coastguard Worker     cfg.g_h = info.frame_height;
259*77c1e3ccSAndroid Build Coastguard Worker     aom_img_read(&raw1, infile1);
260*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_enc_config_set(&codec, &cfg))
261*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&codec, "Failed to set enc cfg for layer 1");
262*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_control(&codec, AOME_SET_SPATIAL_LAYER_ID, 1))
263*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&codec, "Failed to set layer id to 1");
264*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_control(&codec, AOME_SET_CQ_LEVEL, 10))
265*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&codec, "Failed to set cq level");
266*77c1e3ccSAndroid Build Coastguard Worker     encode_frame(&codec, &raw1, frame_count++, flags, outfile);
267*77c1e3ccSAndroid Build Coastguard Worker 
268*77c1e3ccSAndroid Build Coastguard Worker     frames_encoded++;
269*77c1e3ccSAndroid Build Coastguard Worker 
270*77c1e3ccSAndroid Build Coastguard Worker     if (max_frames > 0 && frames_encoded >= max_frames) break;
271*77c1e3ccSAndroid Build Coastguard Worker   }
272*77c1e3ccSAndroid Build Coastguard Worker 
273*77c1e3ccSAndroid Build Coastguard Worker   // Flush encoder.
274*77c1e3ccSAndroid Build Coastguard Worker   while (encode_frame(&codec, NULL, -1, 0, outfile)) continue;
275*77c1e3ccSAndroid Build Coastguard Worker 
276*77c1e3ccSAndroid Build Coastguard Worker   printf("\n");
277*77c1e3ccSAndroid Build Coastguard Worker   fclose(infile0);
278*77c1e3ccSAndroid Build Coastguard Worker   fclose(infile1);
279*77c1e3ccSAndroid Build Coastguard Worker   printf("Processed %d frames.\n", frame_count / 2);
280*77c1e3ccSAndroid Build Coastguard Worker 
281*77c1e3ccSAndroid Build Coastguard Worker   aom_img_free(&raw0);
282*77c1e3ccSAndroid Build Coastguard Worker   aom_img_free(&raw1);
283*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
284*77c1e3ccSAndroid Build Coastguard Worker 
285*77c1e3ccSAndroid Build Coastguard Worker   fclose(outfile);
286*77c1e3ccSAndroid Build Coastguard Worker 
287*77c1e3ccSAndroid Build Coastguard Worker   return EXIT_SUCCESS;
288*77c1e3ccSAndroid Build Coastguard Worker }
289