xref: /aosp_15_r20/external/libaom/examples/svc_encoder_rtc.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2019, 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 //  This is an example demonstrating how to implement a multi-layer AOM
13*77c1e3ccSAndroid Build Coastguard Worker //  encoding scheme for RTC video applications.
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
18*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
19*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
20*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker #include <memory>
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
27*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_decoder.h"
28*77c1e3ccSAndroid Build Coastguard Worker #endif
29*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
30*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomcx.h"
31*77c1e3ccSAndroid Build Coastguard Worker #include "common/args.h"
32*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
33*77c1e3ccSAndroid Build Coastguard Worker #include "common/video_writer.h"
34*77c1e3ccSAndroid Build Coastguard Worker #include "examples/encoder_util.h"
35*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/aom_timer.h"
36*77c1e3ccSAndroid Build Coastguard Worker #include "av1/ratectrl_rtc.h"
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker #define OPTION_BUFFER_SIZE 1024
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
41*77c1e3ccSAndroid Build Coastguard Worker   const char *output_filename;
42*77c1e3ccSAndroid Build Coastguard Worker   char options[OPTION_BUFFER_SIZE];
43*77c1e3ccSAndroid Build Coastguard Worker   struct AvxInputContext input_ctx;
44*77c1e3ccSAndroid Build Coastguard Worker   int speed;
45*77c1e3ccSAndroid Build Coastguard Worker   int aq_mode;
46*77c1e3ccSAndroid Build Coastguard Worker   int layering_mode;
47*77c1e3ccSAndroid Build Coastguard Worker   int output_obu;
48*77c1e3ccSAndroid Build Coastguard Worker   int decode;
49*77c1e3ccSAndroid Build Coastguard Worker   int tune_content;
50*77c1e3ccSAndroid Build Coastguard Worker   int show_psnr;
51*77c1e3ccSAndroid Build Coastguard Worker   bool use_external_rc;
52*77c1e3ccSAndroid Build Coastguard Worker } AppInput;
53*77c1e3ccSAndroid Build Coastguard Worker 
54*77c1e3ccSAndroid Build Coastguard Worker typedef enum {
55*77c1e3ccSAndroid Build Coastguard Worker   QUANTIZER = 0,
56*77c1e3ccSAndroid Build Coastguard Worker   BITRATE,
57*77c1e3ccSAndroid Build Coastguard Worker   SCALE_FACTOR,
58*77c1e3ccSAndroid Build Coastguard Worker   AUTO_ALT_REF,
59*77c1e3ccSAndroid Build Coastguard Worker   ALL_OPTION_TYPES
60*77c1e3ccSAndroid Build Coastguard Worker } LAYER_OPTION_TYPE;
61*77c1e3ccSAndroid Build Coastguard Worker 
62*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t outputfile =
63*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("o", "output", 1, "Output filename");
64*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t frames_arg =
65*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("f", "frames", 1, "Number of frames to encode");
66*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t threads_arg =
67*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("th", "threads", 1, "Number of threads to use");
68*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "Source width");
69*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "Source height");
70*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t timebase_arg =
71*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("t", "timebase", 1, "Timebase (num/den)");
72*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t bitrate_arg = ARG_DEF(
73*77c1e3ccSAndroid Build Coastguard Worker     "b", "target-bitrate", 1, "Encoding bitrate, in kilobits per second");
74*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t spatial_layers_arg =
75*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("sl", "spatial-layers", 1, "Number of spatial SVC layers");
76*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t temporal_layers_arg =
77*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("tl", "temporal-layers", 1, "Number of temporal SVC layers");
78*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t layering_mode_arg =
79*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("lm", "layering-mode", 1, "Temporal layering scheme.");
80*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t kf_dist_arg =
81*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("k", "kf-dist", 1, "Number of frames between keyframes");
82*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t scale_factors_arg =
83*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("r", "scale-factors", 1, "Scale factors (lowest to highest layer)");
84*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t min_q_arg =
85*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
86*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t max_q_arg =
87*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
88*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t speed_arg =
89*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("sp", "speed", 1, "Speed configuration");
90*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t aqmode_arg =
91*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("aq", "aqmode", 1, "AQ mode off/on");
92*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t bitrates_arg =
93*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF("bl", "bitrates", 1,
94*77c1e3ccSAndroid Build Coastguard Worker             "Bitrates[spatial_layer * num_temporal_layer + temporal_layer]");
95*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t dropframe_thresh_arg =
96*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
97*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t error_resilient_arg =
98*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF(NULL, "error-resilient", 1, "Error resilient flag");
99*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t output_obu_arg =
100*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF(NULL, "output-obu", 1,
101*77c1e3ccSAndroid Build Coastguard Worker             "Write OBUs when set to 1. Otherwise write IVF files.");
102*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t test_decode_arg =
103*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF(NULL, "test-decode", 1,
104*77c1e3ccSAndroid Build Coastguard Worker             "Attempt to test decoding the output when set to 1. Default is 1.");
105*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t psnr_arg =
106*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF(NULL, "psnr", -1, "Show PSNR in status line.");
107*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t ext_rc_arg =
108*77c1e3ccSAndroid Build Coastguard Worker     ARG_DEF(NULL, "use-ext-rc", 0, "Use external rate control.");
109*77c1e3ccSAndroid Build Coastguard Worker static const struct arg_enum_list tune_content_enum[] = {
110*77c1e3ccSAndroid Build Coastguard Worker   { "default", AOM_CONTENT_DEFAULT },
111*77c1e3ccSAndroid Build Coastguard Worker   { "screen", AOM_CONTENT_SCREEN },
112*77c1e3ccSAndroid Build Coastguard Worker   { "film", AOM_CONTENT_FILM },
113*77c1e3ccSAndroid Build Coastguard Worker   { NULL, 0 }
114*77c1e3ccSAndroid Build Coastguard Worker };
115*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t tune_content_arg = ARG_DEF_ENUM(
116*77c1e3ccSAndroid Build Coastguard Worker     NULL, "tune-content", 1, "Tune content type", tune_content_enum);
117*77c1e3ccSAndroid Build Coastguard Worker 
118*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
119*77c1e3ccSAndroid Build Coastguard Worker static const struct arg_enum_list bitdepth_enum[] = { { "8", AOM_BITS_8 },
120*77c1e3ccSAndroid Build Coastguard Worker                                                       { "10", AOM_BITS_10 },
121*77c1e3ccSAndroid Build Coastguard Worker                                                       { NULL, 0 } };
122*77c1e3ccSAndroid Build Coastguard Worker 
123*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t bitdepth_arg = ARG_DEF_ENUM(
124*77c1e3ccSAndroid Build Coastguard Worker     "d", "bit-depth", 1, "Bit depth for codec 8 or 10. ", bitdepth_enum);
125*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_HIGHBITDEPTH
126*77c1e3ccSAndroid Build Coastguard Worker 
127*77c1e3ccSAndroid Build Coastguard Worker static const arg_def_t *svc_args[] = {
128*77c1e3ccSAndroid Build Coastguard Worker   &frames_arg,          &outputfile,     &width_arg,
129*77c1e3ccSAndroid Build Coastguard Worker   &height_arg,          &timebase_arg,   &bitrate_arg,
130*77c1e3ccSAndroid Build Coastguard Worker   &spatial_layers_arg,  &kf_dist_arg,    &scale_factors_arg,
131*77c1e3ccSAndroid Build Coastguard Worker   &min_q_arg,           &max_q_arg,      &temporal_layers_arg,
132*77c1e3ccSAndroid Build Coastguard Worker   &layering_mode_arg,   &threads_arg,    &aqmode_arg,
133*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
134*77c1e3ccSAndroid Build Coastguard Worker   &bitdepth_arg,
135*77c1e3ccSAndroid Build Coastguard Worker #endif
136*77c1e3ccSAndroid Build Coastguard Worker   &speed_arg,           &bitrates_arg,   &dropframe_thresh_arg,
137*77c1e3ccSAndroid Build Coastguard Worker   &error_resilient_arg, &output_obu_arg, &test_decode_arg,
138*77c1e3ccSAndroid Build Coastguard Worker   &tune_content_arg,    &psnr_arg,       NULL,
139*77c1e3ccSAndroid Build Coastguard Worker };
140*77c1e3ccSAndroid Build Coastguard Worker 
141*77c1e3ccSAndroid Build Coastguard Worker #define zero(Dest) memset(&(Dest), 0, sizeof(Dest))
142*77c1e3ccSAndroid Build Coastguard Worker 
143*77c1e3ccSAndroid Build Coastguard Worker static const char *exec_name;
144*77c1e3ccSAndroid Build Coastguard Worker 
usage_exit(void)145*77c1e3ccSAndroid Build Coastguard Worker void usage_exit(void) {
146*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "Usage: %s <options> input_filename -o output_filename\n",
147*77c1e3ccSAndroid Build Coastguard Worker           exec_name);
148*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "Options:\n");
149*77c1e3ccSAndroid Build Coastguard Worker   arg_show_usage(stderr, svc_args);
150*77c1e3ccSAndroid Build Coastguard Worker   exit(EXIT_FAILURE);
151*77c1e3ccSAndroid Build Coastguard Worker }
152*77c1e3ccSAndroid Build Coastguard Worker 
file_is_y4m(const char detect[4])153*77c1e3ccSAndroid Build Coastguard Worker static int file_is_y4m(const char detect[4]) {
154*77c1e3ccSAndroid Build Coastguard Worker   return memcmp(detect, "YUV4", 4) == 0;
155*77c1e3ccSAndroid Build Coastguard Worker }
156*77c1e3ccSAndroid Build Coastguard Worker 
fourcc_is_ivf(const char detect[4])157*77c1e3ccSAndroid Build Coastguard Worker static int fourcc_is_ivf(const char detect[4]) {
158*77c1e3ccSAndroid Build Coastguard Worker   if (memcmp(detect, "DKIF", 4) == 0) {
159*77c1e3ccSAndroid Build Coastguard Worker     return 1;
160*77c1e3ccSAndroid Build Coastguard Worker   }
161*77c1e3ccSAndroid Build Coastguard Worker   return 0;
162*77c1e3ccSAndroid Build Coastguard Worker }
163*77c1e3ccSAndroid Build Coastguard Worker 
164*77c1e3ccSAndroid Build Coastguard Worker static const int option_max_values[ALL_OPTION_TYPES] = { 63, INT_MAX, INT_MAX,
165*77c1e3ccSAndroid Build Coastguard Worker                                                          1 };
166*77c1e3ccSAndroid Build Coastguard Worker 
167*77c1e3ccSAndroid Build Coastguard Worker static const int option_min_values[ALL_OPTION_TYPES] = { 0, 0, 1, 0 };
168*77c1e3ccSAndroid Build Coastguard Worker 
open_input_file(struct AvxInputContext * input,aom_chroma_sample_position_t csp)169*77c1e3ccSAndroid Build Coastguard Worker static void open_input_file(struct AvxInputContext *input,
170*77c1e3ccSAndroid Build Coastguard Worker                             aom_chroma_sample_position_t csp) {
171*77c1e3ccSAndroid Build Coastguard Worker   /* Parse certain options from the input file, if possible */
172*77c1e3ccSAndroid Build Coastguard Worker   input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
173*77c1e3ccSAndroid Build Coastguard Worker                                              : set_binary_mode(stdin);
174*77c1e3ccSAndroid Build Coastguard Worker 
175*77c1e3ccSAndroid Build Coastguard Worker   if (!input->file) fatal("Failed to open input file");
176*77c1e3ccSAndroid Build Coastguard Worker 
177*77c1e3ccSAndroid Build Coastguard Worker   if (!fseeko(input->file, 0, SEEK_END)) {
178*77c1e3ccSAndroid Build Coastguard Worker     /* Input file is seekable. Figure out how long it is, so we can get
179*77c1e3ccSAndroid Build Coastguard Worker      * progress info.
180*77c1e3ccSAndroid Build Coastguard Worker      */
181*77c1e3ccSAndroid Build Coastguard Worker     input->length = ftello(input->file);
182*77c1e3ccSAndroid Build Coastguard Worker     rewind(input->file);
183*77c1e3ccSAndroid Build Coastguard Worker   }
184*77c1e3ccSAndroid Build Coastguard Worker 
185*77c1e3ccSAndroid Build Coastguard Worker   /* Default to 1:1 pixel aspect ratio. */
186*77c1e3ccSAndroid Build Coastguard Worker   input->pixel_aspect_ratio.numerator = 1;
187*77c1e3ccSAndroid Build Coastguard Worker   input->pixel_aspect_ratio.denominator = 1;
188*77c1e3ccSAndroid Build Coastguard Worker 
189*77c1e3ccSAndroid Build Coastguard Worker   /* For RAW input sources, these bytes will applied on the first frame
190*77c1e3ccSAndroid Build Coastguard Worker    *  in read_frame().
191*77c1e3ccSAndroid Build Coastguard Worker    */
192*77c1e3ccSAndroid Build Coastguard Worker   input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
193*77c1e3ccSAndroid Build Coastguard Worker   input->detect.position = 0;
194*77c1e3ccSAndroid Build Coastguard Worker 
195*77c1e3ccSAndroid Build Coastguard Worker   if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
196*77c1e3ccSAndroid Build Coastguard Worker     if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
197*77c1e3ccSAndroid Build Coastguard Worker                        input->only_i420) >= 0) {
198*77c1e3ccSAndroid Build Coastguard Worker       input->file_type = FILE_TYPE_Y4M;
199*77c1e3ccSAndroid Build Coastguard Worker       input->width = input->y4m.pic_w;
200*77c1e3ccSAndroid Build Coastguard Worker       input->height = input->y4m.pic_h;
201*77c1e3ccSAndroid Build Coastguard Worker       input->pixel_aspect_ratio.numerator = input->y4m.par_n;
202*77c1e3ccSAndroid Build Coastguard Worker       input->pixel_aspect_ratio.denominator = input->y4m.par_d;
203*77c1e3ccSAndroid Build Coastguard Worker       input->framerate.numerator = input->y4m.fps_n;
204*77c1e3ccSAndroid Build Coastguard Worker       input->framerate.denominator = input->y4m.fps_d;
205*77c1e3ccSAndroid Build Coastguard Worker       input->fmt = input->y4m.aom_fmt;
206*77c1e3ccSAndroid Build Coastguard Worker       input->bit_depth = static_cast<aom_bit_depth_t>(input->y4m.bit_depth);
207*77c1e3ccSAndroid Build Coastguard Worker     } else {
208*77c1e3ccSAndroid Build Coastguard Worker       fatal("Unsupported Y4M stream.");
209*77c1e3ccSAndroid Build Coastguard Worker     }
210*77c1e3ccSAndroid Build Coastguard Worker   } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
211*77c1e3ccSAndroid Build Coastguard Worker     fatal("IVF is not supported as input.");
212*77c1e3ccSAndroid Build Coastguard Worker   } else {
213*77c1e3ccSAndroid Build Coastguard Worker     input->file_type = FILE_TYPE_RAW;
214*77c1e3ccSAndroid Build Coastguard Worker   }
215*77c1e3ccSAndroid Build Coastguard Worker }
216*77c1e3ccSAndroid Build Coastguard Worker 
extract_option(LAYER_OPTION_TYPE type,char * input,int * value0,int * value1)217*77c1e3ccSAndroid Build Coastguard Worker static aom_codec_err_t extract_option(LAYER_OPTION_TYPE type, char *input,
218*77c1e3ccSAndroid Build Coastguard Worker                                       int *value0, int *value1) {
219*77c1e3ccSAndroid Build Coastguard Worker   if (type == SCALE_FACTOR) {
220*77c1e3ccSAndroid Build Coastguard Worker     *value0 = (int)strtol(input, &input, 10);
221*77c1e3ccSAndroid Build Coastguard Worker     if (*input++ != '/') return AOM_CODEC_INVALID_PARAM;
222*77c1e3ccSAndroid Build Coastguard Worker     *value1 = (int)strtol(input, &input, 10);
223*77c1e3ccSAndroid Build Coastguard Worker 
224*77c1e3ccSAndroid Build Coastguard Worker     if (*value0 < option_min_values[SCALE_FACTOR] ||
225*77c1e3ccSAndroid Build Coastguard Worker         *value1 < option_min_values[SCALE_FACTOR] ||
226*77c1e3ccSAndroid Build Coastguard Worker         *value0 > option_max_values[SCALE_FACTOR] ||
227*77c1e3ccSAndroid Build Coastguard Worker         *value1 > option_max_values[SCALE_FACTOR] ||
228*77c1e3ccSAndroid Build Coastguard Worker         *value0 > *value1)  // num shouldn't be greater than den
229*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_INVALID_PARAM;
230*77c1e3ccSAndroid Build Coastguard Worker   } else {
231*77c1e3ccSAndroid Build Coastguard Worker     *value0 = atoi(input);
232*77c1e3ccSAndroid Build Coastguard Worker     if (*value0 < option_min_values[type] || *value0 > option_max_values[type])
233*77c1e3ccSAndroid Build Coastguard Worker       return AOM_CODEC_INVALID_PARAM;
234*77c1e3ccSAndroid Build Coastguard Worker   }
235*77c1e3ccSAndroid Build Coastguard Worker   return AOM_CODEC_OK;
236*77c1e3ccSAndroid Build Coastguard Worker }
237*77c1e3ccSAndroid Build Coastguard Worker 
parse_layer_options_from_string(aom_svc_params_t * svc_params,LAYER_OPTION_TYPE type,const char * input,int * option0,int * option1)238*77c1e3ccSAndroid Build Coastguard Worker static aom_codec_err_t parse_layer_options_from_string(
239*77c1e3ccSAndroid Build Coastguard Worker     aom_svc_params_t *svc_params, LAYER_OPTION_TYPE type, const char *input,
240*77c1e3ccSAndroid Build Coastguard Worker     int *option0, int *option1) {
241*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res = AOM_CODEC_OK;
242*77c1e3ccSAndroid Build Coastguard Worker   char *input_string;
243*77c1e3ccSAndroid Build Coastguard Worker   char *token;
244*77c1e3ccSAndroid Build Coastguard Worker   const char *delim = ",";
245*77c1e3ccSAndroid Build Coastguard Worker   int num_layers = svc_params->number_spatial_layers;
246*77c1e3ccSAndroid Build Coastguard Worker   int i = 0;
247*77c1e3ccSAndroid Build Coastguard Worker 
248*77c1e3ccSAndroid Build Coastguard Worker   if (type == BITRATE)
249*77c1e3ccSAndroid Build Coastguard Worker     num_layers =
250*77c1e3ccSAndroid Build Coastguard Worker         svc_params->number_spatial_layers * svc_params->number_temporal_layers;
251*77c1e3ccSAndroid Build Coastguard Worker 
252*77c1e3ccSAndroid Build Coastguard Worker   if (input == NULL || option0 == NULL ||
253*77c1e3ccSAndroid Build Coastguard Worker       (option1 == NULL && type == SCALE_FACTOR))
254*77c1e3ccSAndroid Build Coastguard Worker     return AOM_CODEC_INVALID_PARAM;
255*77c1e3ccSAndroid Build Coastguard Worker 
256*77c1e3ccSAndroid Build Coastguard Worker   const size_t input_length = strlen(input);
257*77c1e3ccSAndroid Build Coastguard Worker   input_string = reinterpret_cast<char *>(malloc(input_length + 1));
258*77c1e3ccSAndroid Build Coastguard Worker   if (input_string == NULL) return AOM_CODEC_MEM_ERROR;
259*77c1e3ccSAndroid Build Coastguard Worker   memcpy(input_string, input, input_length + 1);
260*77c1e3ccSAndroid Build Coastguard Worker   token = strtok(input_string, delim);  // NOLINT
261*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < num_layers; ++i) {
262*77c1e3ccSAndroid Build Coastguard Worker     if (token != NULL) {
263*77c1e3ccSAndroid Build Coastguard Worker       res = extract_option(type, token, option0 + i, option1 + i);
264*77c1e3ccSAndroid Build Coastguard Worker       if (res != AOM_CODEC_OK) break;
265*77c1e3ccSAndroid Build Coastguard Worker       token = strtok(NULL, delim);  // NOLINT
266*77c1e3ccSAndroid Build Coastguard Worker     } else {
267*77c1e3ccSAndroid Build Coastguard Worker       res = AOM_CODEC_INVALID_PARAM;
268*77c1e3ccSAndroid Build Coastguard Worker       break;
269*77c1e3ccSAndroid Build Coastguard Worker     }
270*77c1e3ccSAndroid Build Coastguard Worker   }
271*77c1e3ccSAndroid Build Coastguard Worker   free(input_string);
272*77c1e3ccSAndroid Build Coastguard Worker   return res;
273*77c1e3ccSAndroid Build Coastguard Worker }
274*77c1e3ccSAndroid Build Coastguard Worker 
parse_command_line(int argc,const char ** argv_,AppInput * app_input,aom_svc_params_t * svc_params,aom_codec_enc_cfg_t * enc_cfg)275*77c1e3ccSAndroid Build Coastguard Worker static void parse_command_line(int argc, const char **argv_,
276*77c1e3ccSAndroid Build Coastguard Worker                                AppInput *app_input,
277*77c1e3ccSAndroid Build Coastguard Worker                                aom_svc_params_t *svc_params,
278*77c1e3ccSAndroid Build Coastguard Worker                                aom_codec_enc_cfg_t *enc_cfg) {
279*77c1e3ccSAndroid Build Coastguard Worker   struct arg arg;
280*77c1e3ccSAndroid Build Coastguard Worker   char **argv = NULL;
281*77c1e3ccSAndroid Build Coastguard Worker   char **argi = NULL;
282*77c1e3ccSAndroid Build Coastguard Worker   char **argj = NULL;
283*77c1e3ccSAndroid Build Coastguard Worker   char string_options[1024] = { 0 };
284*77c1e3ccSAndroid Build Coastguard Worker 
285*77c1e3ccSAndroid Build Coastguard Worker   // Default settings
286*77c1e3ccSAndroid Build Coastguard Worker   svc_params->number_spatial_layers = 1;
287*77c1e3ccSAndroid Build Coastguard Worker   svc_params->number_temporal_layers = 1;
288*77c1e3ccSAndroid Build Coastguard Worker   app_input->layering_mode = 0;
289*77c1e3ccSAndroid Build Coastguard Worker   app_input->output_obu = 0;
290*77c1e3ccSAndroid Build Coastguard Worker   app_input->decode = 1;
291*77c1e3ccSAndroid Build Coastguard Worker   enc_cfg->g_threads = 1;
292*77c1e3ccSAndroid Build Coastguard Worker   enc_cfg->rc_end_usage = AOM_CBR;
293*77c1e3ccSAndroid Build Coastguard Worker 
294*77c1e3ccSAndroid Build Coastguard Worker   // process command line options
295*77c1e3ccSAndroid Build Coastguard Worker   argv = argv_dup(argc - 1, argv_ + 1);
296*77c1e3ccSAndroid Build Coastguard Worker   if (!argv) {
297*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, "Error allocating argument list\n");
298*77c1e3ccSAndroid Build Coastguard Worker     exit(EXIT_FAILURE);
299*77c1e3ccSAndroid Build Coastguard Worker   }
300*77c1e3ccSAndroid Build Coastguard Worker   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
301*77c1e3ccSAndroid Build Coastguard Worker     arg.argv_step = 1;
302*77c1e3ccSAndroid Build Coastguard Worker 
303*77c1e3ccSAndroid Build Coastguard Worker     if (arg_match(&arg, &outputfile, argi)) {
304*77c1e3ccSAndroid Build Coastguard Worker       app_input->output_filename = arg.val;
305*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &width_arg, argi)) {
306*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->g_w = arg_parse_uint(&arg);
307*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &height_arg, argi)) {
308*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->g_h = arg_parse_uint(&arg);
309*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &timebase_arg, argi)) {
310*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->g_timebase = arg_parse_rational(&arg);
311*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &bitrate_arg, argi)) {
312*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
313*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &spatial_layers_arg, argi)) {
314*77c1e3ccSAndroid Build Coastguard Worker       svc_params->number_spatial_layers = arg_parse_uint(&arg);
315*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &temporal_layers_arg, argi)) {
316*77c1e3ccSAndroid Build Coastguard Worker       svc_params->number_temporal_layers = arg_parse_uint(&arg);
317*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &speed_arg, argi)) {
318*77c1e3ccSAndroid Build Coastguard Worker       app_input->speed = arg_parse_uint(&arg);
319*77c1e3ccSAndroid Build Coastguard Worker       if (app_input->speed > 11) {
320*77c1e3ccSAndroid Build Coastguard Worker         aom_tools_warn("Mapping speed %d to speed 11.\n", app_input->speed);
321*77c1e3ccSAndroid Build Coastguard Worker       }
322*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &aqmode_arg, argi)) {
323*77c1e3ccSAndroid Build Coastguard Worker       app_input->aq_mode = arg_parse_uint(&arg);
324*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &threads_arg, argi)) {
325*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->g_threads = arg_parse_uint(&arg);
326*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &layering_mode_arg, argi)) {
327*77c1e3ccSAndroid Build Coastguard Worker       app_input->layering_mode = arg_parse_int(&arg);
328*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &kf_dist_arg, argi)) {
329*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->kf_min_dist = arg_parse_uint(&arg);
330*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
331*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &scale_factors_arg, argi)) {
332*77c1e3ccSAndroid Build Coastguard Worker       aom_codec_err_t res = parse_layer_options_from_string(
333*77c1e3ccSAndroid Build Coastguard Worker           svc_params, SCALE_FACTOR, arg.val, svc_params->scaling_factor_num,
334*77c1e3ccSAndroid Build Coastguard Worker           svc_params->scaling_factor_den);
335*77c1e3ccSAndroid Build Coastguard Worker       if (res != AOM_CODEC_OK) {
336*77c1e3ccSAndroid Build Coastguard Worker         die("Failed to parse scale factors: %s\n",
337*77c1e3ccSAndroid Build Coastguard Worker             aom_codec_err_to_string(res));
338*77c1e3ccSAndroid Build Coastguard Worker       }
339*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &min_q_arg, argi)) {
340*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
341*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &max_q_arg, argi)) {
342*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
343*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
344*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &bitdepth_arg, argi)) {
345*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->g_bit_depth =
346*77c1e3ccSAndroid Build Coastguard Worker           static_cast<aom_bit_depth_t>(arg_parse_enum_or_int(&arg));
347*77c1e3ccSAndroid Build Coastguard Worker       switch (enc_cfg->g_bit_depth) {
348*77c1e3ccSAndroid Build Coastguard Worker         case AOM_BITS_8:
349*77c1e3ccSAndroid Build Coastguard Worker           enc_cfg->g_input_bit_depth = 8;
350*77c1e3ccSAndroid Build Coastguard Worker           enc_cfg->g_profile = 0;
351*77c1e3ccSAndroid Build Coastguard Worker           break;
352*77c1e3ccSAndroid Build Coastguard Worker         case AOM_BITS_10:
353*77c1e3ccSAndroid Build Coastguard Worker           enc_cfg->g_input_bit_depth = 10;
354*77c1e3ccSAndroid Build Coastguard Worker           enc_cfg->g_profile = 0;
355*77c1e3ccSAndroid Build Coastguard Worker           break;
356*77c1e3ccSAndroid Build Coastguard Worker         default:
357*77c1e3ccSAndroid Build Coastguard Worker           die("Error: Invalid bit depth selected (%d)\n", enc_cfg->g_bit_depth);
358*77c1e3ccSAndroid Build Coastguard Worker       }
359*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_VP9_HIGHBITDEPTH
360*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &dropframe_thresh_arg, argi)) {
361*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->rc_dropframe_thresh = arg_parse_uint(&arg);
362*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &error_resilient_arg, argi)) {
363*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->g_error_resilient = arg_parse_uint(&arg);
364*77c1e3ccSAndroid Build Coastguard Worker       if (enc_cfg->g_error_resilient != 0 && enc_cfg->g_error_resilient != 1)
365*77c1e3ccSAndroid Build Coastguard Worker         die("Invalid value for error resilient (0, 1): %d.",
366*77c1e3ccSAndroid Build Coastguard Worker             enc_cfg->g_error_resilient);
367*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &output_obu_arg, argi)) {
368*77c1e3ccSAndroid Build Coastguard Worker       app_input->output_obu = arg_parse_uint(&arg);
369*77c1e3ccSAndroid Build Coastguard Worker       if (app_input->output_obu != 0 && app_input->output_obu != 1)
370*77c1e3ccSAndroid Build Coastguard Worker         die("Invalid value for obu output flag (0, 1): %d.",
371*77c1e3ccSAndroid Build Coastguard Worker             app_input->output_obu);
372*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &test_decode_arg, argi)) {
373*77c1e3ccSAndroid Build Coastguard Worker       app_input->decode = arg_parse_uint(&arg);
374*77c1e3ccSAndroid Build Coastguard Worker       if (app_input->decode != 0 && app_input->decode != 1)
375*77c1e3ccSAndroid Build Coastguard Worker         die("Invalid value for test decode flag (0, 1): %d.",
376*77c1e3ccSAndroid Build Coastguard Worker             app_input->decode);
377*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &tune_content_arg, argi)) {
378*77c1e3ccSAndroid Build Coastguard Worker       app_input->tune_content = arg_parse_enum_or_int(&arg);
379*77c1e3ccSAndroid Build Coastguard Worker       printf("tune content %d\n", app_input->tune_content);
380*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &psnr_arg, argi)) {
381*77c1e3ccSAndroid Build Coastguard Worker       app_input->show_psnr = 1;
382*77c1e3ccSAndroid Build Coastguard Worker     } else if (arg_match(&arg, &ext_rc_arg, argi)) {
383*77c1e3ccSAndroid Build Coastguard Worker       app_input->use_external_rc = true;
384*77c1e3ccSAndroid Build Coastguard Worker     } else {
385*77c1e3ccSAndroid Build Coastguard Worker       ++argj;
386*77c1e3ccSAndroid Build Coastguard Worker     }
387*77c1e3ccSAndroid Build Coastguard Worker   }
388*77c1e3ccSAndroid Build Coastguard Worker 
389*77c1e3ccSAndroid Build Coastguard Worker   // Total bitrate needs to be parsed after the number of layers.
390*77c1e3ccSAndroid Build Coastguard Worker   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
391*77c1e3ccSAndroid Build Coastguard Worker     arg.argv_step = 1;
392*77c1e3ccSAndroid Build Coastguard Worker     if (arg_match(&arg, &bitrates_arg, argi)) {
393*77c1e3ccSAndroid Build Coastguard Worker       aom_codec_err_t res = parse_layer_options_from_string(
394*77c1e3ccSAndroid Build Coastguard Worker           svc_params, BITRATE, arg.val, svc_params->layer_target_bitrate, NULL);
395*77c1e3ccSAndroid Build Coastguard Worker       if (res != AOM_CODEC_OK) {
396*77c1e3ccSAndroid Build Coastguard Worker         die("Failed to parse bitrates: %s\n", aom_codec_err_to_string(res));
397*77c1e3ccSAndroid Build Coastguard Worker       }
398*77c1e3ccSAndroid Build Coastguard Worker     } else {
399*77c1e3ccSAndroid Build Coastguard Worker       ++argj;
400*77c1e3ccSAndroid Build Coastguard Worker     }
401*77c1e3ccSAndroid Build Coastguard Worker   }
402*77c1e3ccSAndroid Build Coastguard Worker 
403*77c1e3ccSAndroid Build Coastguard Worker   // There will be a space in front of the string options
404*77c1e3ccSAndroid Build Coastguard Worker   if (strlen(string_options) > 0)
405*77c1e3ccSAndroid Build Coastguard Worker     strncpy(app_input->options, string_options, OPTION_BUFFER_SIZE);
406*77c1e3ccSAndroid Build Coastguard Worker 
407*77c1e3ccSAndroid Build Coastguard Worker   // Check for unrecognized options
408*77c1e3ccSAndroid Build Coastguard Worker   for (argi = argv; *argi; ++argi)
409*77c1e3ccSAndroid Build Coastguard Worker     if (argi[0][0] == '-' && strlen(argi[0]) > 1)
410*77c1e3ccSAndroid Build Coastguard Worker       die("Error: Unrecognized option %s\n", *argi);
411*77c1e3ccSAndroid Build Coastguard Worker 
412*77c1e3ccSAndroid Build Coastguard Worker   if (argv[0] == NULL) {
413*77c1e3ccSAndroid Build Coastguard Worker     usage_exit();
414*77c1e3ccSAndroid Build Coastguard Worker   }
415*77c1e3ccSAndroid Build Coastguard Worker 
416*77c1e3ccSAndroid Build Coastguard Worker   app_input->input_ctx.filename = argv[0];
417*77c1e3ccSAndroid Build Coastguard Worker   free(argv);
418*77c1e3ccSAndroid Build Coastguard Worker 
419*77c1e3ccSAndroid Build Coastguard Worker   open_input_file(&app_input->input_ctx, AOM_CSP_UNKNOWN);
420*77c1e3ccSAndroid Build Coastguard Worker   if (app_input->input_ctx.file_type == FILE_TYPE_Y4M) {
421*77c1e3ccSAndroid Build Coastguard Worker     enc_cfg->g_w = app_input->input_ctx.width;
422*77c1e3ccSAndroid Build Coastguard Worker     enc_cfg->g_h = app_input->input_ctx.height;
423*77c1e3ccSAndroid Build Coastguard Worker   }
424*77c1e3ccSAndroid Build Coastguard Worker 
425*77c1e3ccSAndroid Build Coastguard Worker   if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
426*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->g_h % 2)
427*77c1e3ccSAndroid Build Coastguard Worker     die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
428*77c1e3ccSAndroid Build Coastguard Worker 
429*77c1e3ccSAndroid Build Coastguard Worker   printf(
430*77c1e3ccSAndroid Build Coastguard Worker       "Codec %s\n"
431*77c1e3ccSAndroid Build Coastguard Worker       "layers: %d\n"
432*77c1e3ccSAndroid Build Coastguard Worker       "width %u, height: %u\n"
433*77c1e3ccSAndroid Build Coastguard Worker       "num: %d, den: %d, bitrate: %u\n"
434*77c1e3ccSAndroid Build Coastguard Worker       "gop size: %u\n",
435*77c1e3ccSAndroid Build Coastguard Worker       aom_codec_iface_name(aom_codec_av1_cx()),
436*77c1e3ccSAndroid Build Coastguard Worker       svc_params->number_spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
437*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
438*77c1e3ccSAndroid Build Coastguard Worker       enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
439*77c1e3ccSAndroid Build Coastguard Worker }
440*77c1e3ccSAndroid Build Coastguard Worker 
441*77c1e3ccSAndroid Build Coastguard Worker static int mode_to_num_temporal_layers[12] = {
442*77c1e3ccSAndroid Build Coastguard Worker   1, 2, 3, 3, 2, 1, 1, 3, 3, 3, 3, 3,
443*77c1e3ccSAndroid Build Coastguard Worker };
444*77c1e3ccSAndroid Build Coastguard Worker static int mode_to_num_spatial_layers[12] = {
445*77c1e3ccSAndroid Build Coastguard Worker   1, 1, 1, 1, 1, 2, 3, 2, 3, 3, 3, 3,
446*77c1e3ccSAndroid Build Coastguard Worker };
447*77c1e3ccSAndroid Build Coastguard Worker 
448*77c1e3ccSAndroid Build Coastguard Worker // For rate control encoding stats.
449*77c1e3ccSAndroid Build Coastguard Worker struct RateControlMetrics {
450*77c1e3ccSAndroid Build Coastguard Worker   // Number of input frames per layer.
451*77c1e3ccSAndroid Build Coastguard Worker   int layer_input_frames[AOM_MAX_TS_LAYERS];
452*77c1e3ccSAndroid Build Coastguard Worker   // Number of encoded non-key frames per layer.
453*77c1e3ccSAndroid Build Coastguard Worker   int layer_enc_frames[AOM_MAX_TS_LAYERS];
454*77c1e3ccSAndroid Build Coastguard Worker   // Framerate per layer layer (cumulative).
455*77c1e3ccSAndroid Build Coastguard Worker   double layer_framerate[AOM_MAX_TS_LAYERS];
456*77c1e3ccSAndroid Build Coastguard Worker   // Target average frame size per layer (per-frame-bandwidth per layer).
457*77c1e3ccSAndroid Build Coastguard Worker   double layer_pfb[AOM_MAX_LAYERS];
458*77c1e3ccSAndroid Build Coastguard Worker   // Actual average frame size per layer.
459*77c1e3ccSAndroid Build Coastguard Worker   double layer_avg_frame_size[AOM_MAX_LAYERS];
460*77c1e3ccSAndroid Build Coastguard Worker   // Average rate mismatch per layer (|target - actual| / target).
461*77c1e3ccSAndroid Build Coastguard Worker   double layer_avg_rate_mismatch[AOM_MAX_LAYERS];
462*77c1e3ccSAndroid Build Coastguard Worker   // Actual encoding bitrate per layer (cumulative across temporal layers).
463*77c1e3ccSAndroid Build Coastguard Worker   double layer_encoding_bitrate[AOM_MAX_LAYERS];
464*77c1e3ccSAndroid Build Coastguard Worker   // Average of the short-time encoder actual bitrate.
465*77c1e3ccSAndroid Build Coastguard Worker   // TODO(marpan): Should we add these short-time stats for each layer?
466*77c1e3ccSAndroid Build Coastguard Worker   double avg_st_encoding_bitrate;
467*77c1e3ccSAndroid Build Coastguard Worker   // Variance of the short-time encoder actual bitrate.
468*77c1e3ccSAndroid Build Coastguard Worker   double variance_st_encoding_bitrate;
469*77c1e3ccSAndroid Build Coastguard Worker   // Window (number of frames) for computing short-timee encoding bitrate.
470*77c1e3ccSAndroid Build Coastguard Worker   int window_size;
471*77c1e3ccSAndroid Build Coastguard Worker   // Number of window measurements.
472*77c1e3ccSAndroid Build Coastguard Worker   int window_count;
473*77c1e3ccSAndroid Build Coastguard Worker   int layer_target_bitrate[AOM_MAX_LAYERS];
474*77c1e3ccSAndroid Build Coastguard Worker };
475*77c1e3ccSAndroid Build Coastguard Worker 
476*77c1e3ccSAndroid Build Coastguard Worker static const int REF_FRAMES = 8;
477*77c1e3ccSAndroid Build Coastguard Worker 
478*77c1e3ccSAndroid Build Coastguard Worker static const int INTER_REFS_PER_FRAME = 7;
479*77c1e3ccSAndroid Build Coastguard Worker 
480*77c1e3ccSAndroid Build Coastguard Worker // Reference frames used in this example encoder.
481*77c1e3ccSAndroid Build Coastguard Worker enum {
482*77c1e3ccSAndroid Build Coastguard Worker   SVC_LAST_FRAME = 0,
483*77c1e3ccSAndroid Build Coastguard Worker   SVC_LAST2_FRAME,
484*77c1e3ccSAndroid Build Coastguard Worker   SVC_LAST3_FRAME,
485*77c1e3ccSAndroid Build Coastguard Worker   SVC_GOLDEN_FRAME,
486*77c1e3ccSAndroid Build Coastguard Worker   SVC_BWDREF_FRAME,
487*77c1e3ccSAndroid Build Coastguard Worker   SVC_ALTREF2_FRAME,
488*77c1e3ccSAndroid Build Coastguard Worker   SVC_ALTREF_FRAME
489*77c1e3ccSAndroid Build Coastguard Worker };
490*77c1e3ccSAndroid Build Coastguard Worker 
read_frame(struct AvxInputContext * input_ctx,aom_image_t * img)491*77c1e3ccSAndroid Build Coastguard Worker static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
492*77c1e3ccSAndroid Build Coastguard Worker   FILE *f = input_ctx->file;
493*77c1e3ccSAndroid Build Coastguard Worker   y4m_input *y4m = &input_ctx->y4m;
494*77c1e3ccSAndroid Build Coastguard Worker   int shortread = 0;
495*77c1e3ccSAndroid Build Coastguard Worker 
496*77c1e3ccSAndroid Build Coastguard Worker   if (input_ctx->file_type == FILE_TYPE_Y4M) {
497*77c1e3ccSAndroid Build Coastguard Worker     if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
498*77c1e3ccSAndroid Build Coastguard Worker   } else {
499*77c1e3ccSAndroid Build Coastguard Worker     shortread = read_yuv_frame(input_ctx, img);
500*77c1e3ccSAndroid Build Coastguard Worker   }
501*77c1e3ccSAndroid Build Coastguard Worker 
502*77c1e3ccSAndroid Build Coastguard Worker   return !shortread;
503*77c1e3ccSAndroid Build Coastguard Worker }
504*77c1e3ccSAndroid Build Coastguard Worker 
close_input_file(struct AvxInputContext * input)505*77c1e3ccSAndroid Build Coastguard Worker static void close_input_file(struct AvxInputContext *input) {
506*77c1e3ccSAndroid Build Coastguard Worker   fclose(input->file);
507*77c1e3ccSAndroid Build Coastguard Worker   if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
508*77c1e3ccSAndroid Build Coastguard Worker }
509*77c1e3ccSAndroid Build Coastguard Worker 
510*77c1e3ccSAndroid Build Coastguard Worker // Note: these rate control metrics assume only 1 key frame in the
511*77c1e3ccSAndroid Build Coastguard Worker // sequence (i.e., first frame only). So for temporal pattern# 7
512*77c1e3ccSAndroid Build Coastguard Worker // (which has key frame for every frame on base layer), the metrics
513*77c1e3ccSAndroid Build Coastguard Worker // computation will be off/wrong.
514*77c1e3ccSAndroid Build Coastguard Worker // TODO(marpan): Update these metrics to account for multiple key frames
515*77c1e3ccSAndroid Build Coastguard Worker // in the stream.
set_rate_control_metrics(struct RateControlMetrics * rc,double framerate,int ss_number_layers,int ts_number_layers)516*77c1e3ccSAndroid Build Coastguard Worker static void set_rate_control_metrics(struct RateControlMetrics *rc,
517*77c1e3ccSAndroid Build Coastguard Worker                                      double framerate, int ss_number_layers,
518*77c1e3ccSAndroid Build Coastguard Worker                                      int ts_number_layers) {
519*77c1e3ccSAndroid Build Coastguard Worker   int ts_rate_decimator[AOM_MAX_TS_LAYERS] = { 1 };
520*77c1e3ccSAndroid Build Coastguard Worker   ts_rate_decimator[0] = 1;
521*77c1e3ccSAndroid Build Coastguard Worker   if (ts_number_layers == 2) {
522*77c1e3ccSAndroid Build Coastguard Worker     ts_rate_decimator[0] = 2;
523*77c1e3ccSAndroid Build Coastguard Worker     ts_rate_decimator[1] = 1;
524*77c1e3ccSAndroid Build Coastguard Worker   }
525*77c1e3ccSAndroid Build Coastguard Worker   if (ts_number_layers == 3) {
526*77c1e3ccSAndroid Build Coastguard Worker     ts_rate_decimator[0] = 4;
527*77c1e3ccSAndroid Build Coastguard Worker     ts_rate_decimator[1] = 2;
528*77c1e3ccSAndroid Build Coastguard Worker     ts_rate_decimator[2] = 1;
529*77c1e3ccSAndroid Build Coastguard Worker   }
530*77c1e3ccSAndroid Build Coastguard Worker   // Set the layer (cumulative) framerate and the target layer (non-cumulative)
531*77c1e3ccSAndroid Build Coastguard Worker   // per-frame-bandwidth, for the rate control encoding stats below.
532*77c1e3ccSAndroid Build Coastguard Worker   for (int sl = 0; sl < ss_number_layers; ++sl) {
533*77c1e3ccSAndroid Build Coastguard Worker     int i = sl * ts_number_layers;
534*77c1e3ccSAndroid Build Coastguard Worker     rc->layer_framerate[0] = framerate / ts_rate_decimator[0];
535*77c1e3ccSAndroid Build Coastguard Worker     rc->layer_pfb[i] =
536*77c1e3ccSAndroid Build Coastguard Worker         1000.0 * rc->layer_target_bitrate[i] / rc->layer_framerate[0];
537*77c1e3ccSAndroid Build Coastguard Worker     for (int tl = 0; tl < ts_number_layers; ++tl) {
538*77c1e3ccSAndroid Build Coastguard Worker       i = sl * ts_number_layers + tl;
539*77c1e3ccSAndroid Build Coastguard Worker       if (tl > 0) {
540*77c1e3ccSAndroid Build Coastguard Worker         rc->layer_framerate[tl] = framerate / ts_rate_decimator[tl];
541*77c1e3ccSAndroid Build Coastguard Worker         rc->layer_pfb[i] =
542*77c1e3ccSAndroid Build Coastguard Worker             1000.0 *
543*77c1e3ccSAndroid Build Coastguard Worker             (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
544*77c1e3ccSAndroid Build Coastguard Worker             (rc->layer_framerate[tl] - rc->layer_framerate[tl - 1]);
545*77c1e3ccSAndroid Build Coastguard Worker       }
546*77c1e3ccSAndroid Build Coastguard Worker       rc->layer_input_frames[tl] = 0;
547*77c1e3ccSAndroid Build Coastguard Worker       rc->layer_enc_frames[tl] = 0;
548*77c1e3ccSAndroid Build Coastguard Worker       rc->layer_encoding_bitrate[i] = 0.0;
549*77c1e3ccSAndroid Build Coastguard Worker       rc->layer_avg_frame_size[i] = 0.0;
550*77c1e3ccSAndroid Build Coastguard Worker       rc->layer_avg_rate_mismatch[i] = 0.0;
551*77c1e3ccSAndroid Build Coastguard Worker     }
552*77c1e3ccSAndroid Build Coastguard Worker   }
553*77c1e3ccSAndroid Build Coastguard Worker   rc->window_count = 0;
554*77c1e3ccSAndroid Build Coastguard Worker   rc->window_size = 15;
555*77c1e3ccSAndroid Build Coastguard Worker   rc->avg_st_encoding_bitrate = 0.0;
556*77c1e3ccSAndroid Build Coastguard Worker   rc->variance_st_encoding_bitrate = 0.0;
557*77c1e3ccSAndroid Build Coastguard Worker }
558*77c1e3ccSAndroid Build Coastguard Worker 
printout_rate_control_summary(struct RateControlMetrics * rc,int frame_cnt,int ss_number_layers,int ts_number_layers)559*77c1e3ccSAndroid Build Coastguard Worker static void printout_rate_control_summary(struct RateControlMetrics *rc,
560*77c1e3ccSAndroid Build Coastguard Worker                                           int frame_cnt, int ss_number_layers,
561*77c1e3ccSAndroid Build Coastguard Worker                                           int ts_number_layers) {
562*77c1e3ccSAndroid Build Coastguard Worker   int tot_num_frames = 0;
563*77c1e3ccSAndroid Build Coastguard Worker   double perc_fluctuation = 0.0;
564*77c1e3ccSAndroid Build Coastguard Worker   printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
565*77c1e3ccSAndroid Build Coastguard Worker   printf("Rate control layer stats for %d layer(s):\n\n", ts_number_layers);
566*77c1e3ccSAndroid Build Coastguard Worker   for (int sl = 0; sl < ss_number_layers; ++sl) {
567*77c1e3ccSAndroid Build Coastguard Worker     tot_num_frames = 0;
568*77c1e3ccSAndroid Build Coastguard Worker     for (int tl = 0; tl < ts_number_layers; ++tl) {
569*77c1e3ccSAndroid Build Coastguard Worker       int i = sl * ts_number_layers + tl;
570*77c1e3ccSAndroid Build Coastguard Worker       const int num_dropped =
571*77c1e3ccSAndroid Build Coastguard Worker           tl > 0 ? rc->layer_input_frames[tl] - rc->layer_enc_frames[tl]
572*77c1e3ccSAndroid Build Coastguard Worker                  : rc->layer_input_frames[tl] - rc->layer_enc_frames[tl] - 1;
573*77c1e3ccSAndroid Build Coastguard Worker       tot_num_frames += rc->layer_input_frames[tl];
574*77c1e3ccSAndroid Build Coastguard Worker       rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[tl] *
575*77c1e3ccSAndroid Build Coastguard Worker                                       rc->layer_encoding_bitrate[i] /
576*77c1e3ccSAndroid Build Coastguard Worker                                       tot_num_frames;
577*77c1e3ccSAndroid Build Coastguard Worker       rc->layer_avg_frame_size[i] =
578*77c1e3ccSAndroid Build Coastguard Worker           rc->layer_avg_frame_size[i] / rc->layer_enc_frames[tl];
579*77c1e3ccSAndroid Build Coastguard Worker       rc->layer_avg_rate_mismatch[i] =
580*77c1e3ccSAndroid Build Coastguard Worker           100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[tl];
581*77c1e3ccSAndroid Build Coastguard Worker       printf("For layer#: %d %d \n", sl, tl);
582*77c1e3ccSAndroid Build Coastguard Worker       printf("Bitrate (target vs actual): %d %f\n", rc->layer_target_bitrate[i],
583*77c1e3ccSAndroid Build Coastguard Worker              rc->layer_encoding_bitrate[i]);
584*77c1e3ccSAndroid Build Coastguard Worker       printf("Average frame size (target vs actual): %f %f\n", rc->layer_pfb[i],
585*77c1e3ccSAndroid Build Coastguard Worker              rc->layer_avg_frame_size[i]);
586*77c1e3ccSAndroid Build Coastguard Worker       printf("Average rate_mismatch: %f\n", rc->layer_avg_rate_mismatch[i]);
587*77c1e3ccSAndroid Build Coastguard Worker       printf(
588*77c1e3ccSAndroid Build Coastguard Worker           "Number of input frames, encoded (non-key) frames, "
589*77c1e3ccSAndroid Build Coastguard Worker           "and perc dropped frames: %d %d %f\n",
590*77c1e3ccSAndroid Build Coastguard Worker           rc->layer_input_frames[tl], rc->layer_enc_frames[tl],
591*77c1e3ccSAndroid Build Coastguard Worker           100.0 * num_dropped / rc->layer_input_frames[tl]);
592*77c1e3ccSAndroid Build Coastguard Worker       printf("\n");
593*77c1e3ccSAndroid Build Coastguard Worker     }
594*77c1e3ccSAndroid Build Coastguard Worker   }
595*77c1e3ccSAndroid Build Coastguard Worker   rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
596*77c1e3ccSAndroid Build Coastguard Worker   rc->variance_st_encoding_bitrate =
597*77c1e3ccSAndroid Build Coastguard Worker       rc->variance_st_encoding_bitrate / rc->window_count -
598*77c1e3ccSAndroid Build Coastguard Worker       (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
599*77c1e3ccSAndroid Build Coastguard Worker   perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
600*77c1e3ccSAndroid Build Coastguard Worker                      rc->avg_st_encoding_bitrate;
601*77c1e3ccSAndroid Build Coastguard Worker   printf("Short-time stats, for window of %d frames:\n", rc->window_size);
602*77c1e3ccSAndroid Build Coastguard Worker   printf("Average, rms-variance, and percent-fluct: %f %f %f\n",
603*77c1e3ccSAndroid Build Coastguard Worker          rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
604*77c1e3ccSAndroid Build Coastguard Worker          perc_fluctuation);
605*77c1e3ccSAndroid Build Coastguard Worker   if (frame_cnt - 1 != tot_num_frames)
606*77c1e3ccSAndroid Build Coastguard Worker     die("Error: Number of input frames not equal to output!\n");
607*77c1e3ccSAndroid Build Coastguard Worker }
608*77c1e3ccSAndroid Build Coastguard Worker 
609*77c1e3ccSAndroid Build Coastguard Worker // Layer pattern configuration.
set_layer_pattern(int layering_mode,int superframe_cnt,aom_svc_layer_id_t * layer_id,aom_svc_ref_frame_config_t * ref_frame_config,aom_svc_ref_frame_comp_pred_t * ref_frame_comp_pred,int * use_svc_control,int spatial_layer_id,int is_key_frame,int ksvc_mode,int speed)610*77c1e3ccSAndroid Build Coastguard Worker static void set_layer_pattern(
611*77c1e3ccSAndroid Build Coastguard Worker     int layering_mode, int superframe_cnt, aom_svc_layer_id_t *layer_id,
612*77c1e3ccSAndroid Build Coastguard Worker     aom_svc_ref_frame_config_t *ref_frame_config,
613*77c1e3ccSAndroid Build Coastguard Worker     aom_svc_ref_frame_comp_pred_t *ref_frame_comp_pred, int *use_svc_control,
614*77c1e3ccSAndroid Build Coastguard Worker     int spatial_layer_id, int is_key_frame, int ksvc_mode, int speed) {
615*77c1e3ccSAndroid Build Coastguard Worker   // Setting this flag to 1 enables simplex example of
616*77c1e3ccSAndroid Build Coastguard Worker   // RPS (Reference Picture Selection) for 1 layer.
617*77c1e3ccSAndroid Build Coastguard Worker   int use_rps_example = 0;
618*77c1e3ccSAndroid Build Coastguard Worker   int i;
619*77c1e3ccSAndroid Build Coastguard Worker   int enable_longterm_temporal_ref = 1;
620*77c1e3ccSAndroid Build Coastguard Worker   int shift = (layering_mode == 8) ? 2 : 0;
621*77c1e3ccSAndroid Build Coastguard Worker   int simulcast_mode = (layering_mode == 11);
622*77c1e3ccSAndroid Build Coastguard Worker   *use_svc_control = 1;
623*77c1e3ccSAndroid Build Coastguard Worker   layer_id->spatial_layer_id = spatial_layer_id;
624*77c1e3ccSAndroid Build Coastguard Worker   int lag_index = 0;
625*77c1e3ccSAndroid Build Coastguard Worker   int base_count = superframe_cnt >> 2;
626*77c1e3ccSAndroid Build Coastguard Worker   ref_frame_comp_pred->use_comp_pred[0] = 0;  // GOLDEN_LAST
627*77c1e3ccSAndroid Build Coastguard Worker   ref_frame_comp_pred->use_comp_pred[1] = 0;  // LAST2_LAST
628*77c1e3ccSAndroid Build Coastguard Worker   ref_frame_comp_pred->use_comp_pred[2] = 0;  // ALTREF_LAST
629*77c1e3ccSAndroid Build Coastguard Worker   // Set the reference map buffer idx for the 7 references:
630*77c1e3ccSAndroid Build Coastguard Worker   // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
631*77c1e3ccSAndroid Build Coastguard Worker   // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
632*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->ref_idx[i] = i;
633*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->reference[i] = 0;
634*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
635*77c1e3ccSAndroid Build Coastguard Worker 
636*77c1e3ccSAndroid Build Coastguard Worker   if (ksvc_mode) {
637*77c1e3ccSAndroid Build Coastguard Worker     // Same pattern as case 9, but the reference strucutre will be constrained
638*77c1e3ccSAndroid Build Coastguard Worker     // below.
639*77c1e3ccSAndroid Build Coastguard Worker     layering_mode = 9;
640*77c1e3ccSAndroid Build Coastguard Worker   }
641*77c1e3ccSAndroid Build Coastguard Worker   switch (layering_mode) {
642*77c1e3ccSAndroid Build Coastguard Worker     case 0:
643*77c1e3ccSAndroid Build Coastguard Worker       if (use_rps_example == 0) {
644*77c1e3ccSAndroid Build Coastguard Worker         // 1-layer: update LAST on every frame, reference LAST.
645*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
646*77c1e3ccSAndroid Build Coastguard Worker         layer_id->spatial_layer_id = 0;
647*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[0] = 1;
648*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
649*77c1e3ccSAndroid Build Coastguard Worker       } else {
650*77c1e3ccSAndroid Build Coastguard Worker         // Pattern of 2 references (ALTREF and GOLDEN) trailing
651*77c1e3ccSAndroid Build Coastguard Worker         // LAST by 4 and 8 frames, with some switching logic to
652*77c1e3ccSAndroid Build Coastguard Worker         // sometimes only predict from the longer-term reference
653*77c1e3ccSAndroid Build Coastguard Worker         //(golden here). This is simple example to test RPS
654*77c1e3ccSAndroid Build Coastguard Worker         // (reference picture selection).
655*77c1e3ccSAndroid Build Coastguard Worker         int last_idx = 0;
656*77c1e3ccSAndroid Build Coastguard Worker         int last_idx_refresh = 0;
657*77c1e3ccSAndroid Build Coastguard Worker         int gld_idx = 0;
658*77c1e3ccSAndroid Build Coastguard Worker         int alt_ref_idx = 0;
659*77c1e3ccSAndroid Build Coastguard Worker         int lag_alt = 4;
660*77c1e3ccSAndroid Build Coastguard Worker         int lag_gld = 8;
661*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
662*77c1e3ccSAndroid Build Coastguard Worker         layer_id->spatial_layer_id = 0;
663*77c1e3ccSAndroid Build Coastguard Worker         int sh = 8;  // slots 0 - 7.
664*77c1e3ccSAndroid Build Coastguard Worker         // Moving index slot for last: 0 - (sh - 1)
665*77c1e3ccSAndroid Build Coastguard Worker         if (superframe_cnt > 1) last_idx = (superframe_cnt - 1) % sh;
666*77c1e3ccSAndroid Build Coastguard Worker         // Moving index for refresh of last: one ahead for next frame.
667*77c1e3ccSAndroid Build Coastguard Worker         last_idx_refresh = superframe_cnt % sh;
668*77c1e3ccSAndroid Build Coastguard Worker         // Moving index for gld_ref, lag behind current by lag_gld
669*77c1e3ccSAndroid Build Coastguard Worker         if (superframe_cnt > lag_gld) gld_idx = (superframe_cnt - lag_gld) % sh;
670*77c1e3ccSAndroid Build Coastguard Worker         // Moving index for alt_ref, lag behind LAST by lag_alt frames.
671*77c1e3ccSAndroid Build Coastguard Worker         if (superframe_cnt > lag_alt)
672*77c1e3ccSAndroid Build Coastguard Worker           alt_ref_idx = (superframe_cnt - lag_alt) % sh;
673*77c1e3ccSAndroid Build Coastguard Worker         // Set the ref_idx.
674*77c1e3ccSAndroid Build Coastguard Worker         // Default all references to slot for last.
675*77c1e3ccSAndroid Build Coastguard Worker         for (i = 0; i < INTER_REFS_PER_FRAME; i++)
676*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[i] = last_idx;
677*77c1e3ccSAndroid Build Coastguard Worker         // Set the ref_idx for the relevant references.
678*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST_FRAME] = last_idx;
679*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST2_FRAME] = last_idx_refresh;
680*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = gld_idx;
681*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = alt_ref_idx;
682*77c1e3ccSAndroid Build Coastguard Worker         // Refresh this slot, which will become LAST on next frame.
683*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[last_idx_refresh] = 1;
684*77c1e3ccSAndroid Build Coastguard Worker         // Reference LAST, ALTREF, and GOLDEN
685*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
686*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
687*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
688*77c1e3ccSAndroid Build Coastguard Worker         // Switch to only GOLDEN every 300 frames.
689*77c1e3ccSAndroid Build Coastguard Worker         if (superframe_cnt % 200 == 0 && superframe_cnt > 0) {
690*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 0;
691*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_ALTREF_FRAME] = 0;
692*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
693*77c1e3ccSAndroid Build Coastguard Worker           // Test if the long-term is LAST instead, this is just a renaming
694*77c1e3ccSAndroid Build Coastguard Worker           // but its tests if encoder behaves the same, whether its
695*77c1e3ccSAndroid Build Coastguard Worker           // LAST or GOLDEN.
696*77c1e3ccSAndroid Build Coastguard Worker           if (superframe_cnt % 400 == 0 && superframe_cnt > 0) {
697*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[SVC_LAST_FRAME] = gld_idx;
698*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->reference[SVC_LAST_FRAME] = 1;
699*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->reference[SVC_ALTREF_FRAME] = 0;
700*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->reference[SVC_GOLDEN_FRAME] = 0;
701*77c1e3ccSAndroid Build Coastguard Worker           }
702*77c1e3ccSAndroid Build Coastguard Worker         }
703*77c1e3ccSAndroid Build Coastguard Worker       }
704*77c1e3ccSAndroid Build Coastguard Worker       break;
705*77c1e3ccSAndroid Build Coastguard Worker     case 1:
706*77c1e3ccSAndroid Build Coastguard Worker       // 2-temporal layer.
707*77c1e3ccSAndroid Build Coastguard Worker       //    1    3    5
708*77c1e3ccSAndroid Build Coastguard Worker       //  0    2    4
709*77c1e3ccSAndroid Build Coastguard Worker       // Keep golden fixed at slot 3.
710*77c1e3ccSAndroid Build Coastguard Worker       base_count = superframe_cnt >> 1;
711*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
712*77c1e3ccSAndroid Build Coastguard Worker       // Cyclically refresh slots 5, 6, 7, for lag alt ref.
713*77c1e3ccSAndroid Build Coastguard Worker       lag_index = 5;
714*77c1e3ccSAndroid Build Coastguard Worker       if (base_count > 0) {
715*77c1e3ccSAndroid Build Coastguard Worker         lag_index = 5 + (base_count % 3);
716*77c1e3ccSAndroid Build Coastguard Worker         if (superframe_cnt % 2 != 0) lag_index = 5 + ((base_count + 1) % 3);
717*77c1e3ccSAndroid Build Coastguard Worker       }
718*77c1e3ccSAndroid Build Coastguard Worker       // Set the altref slot to lag_index.
719*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = lag_index;
720*77c1e3ccSAndroid Build Coastguard Worker       if (superframe_cnt % 2 == 0) {
721*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
722*77c1e3ccSAndroid Build Coastguard Worker         // Update LAST on layer 0, reference LAST.
723*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[0] = 1;
724*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
725*77c1e3ccSAndroid Build Coastguard Worker         // Refresh lag_index slot, needed for lagging golen.
726*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[lag_index] = 1;
727*77c1e3ccSAndroid Build Coastguard Worker         // Refresh GOLDEN every x base layer frames.
728*77c1e3ccSAndroid Build Coastguard Worker         if (base_count % 32 == 0) ref_frame_config->refresh[3] = 1;
729*77c1e3ccSAndroid Build Coastguard Worker       } else {
730*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 1;
731*77c1e3ccSAndroid Build Coastguard Worker         // No updates on layer 1, reference LAST (TL0).
732*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
733*77c1e3ccSAndroid Build Coastguard Worker       }
734*77c1e3ccSAndroid Build Coastguard Worker       // Always reference golden and altref on TL0.
735*77c1e3ccSAndroid Build Coastguard Worker       if (layer_id->temporal_layer_id == 0) {
736*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
737*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
738*77c1e3ccSAndroid Build Coastguard Worker       }
739*77c1e3ccSAndroid Build Coastguard Worker       break;
740*77c1e3ccSAndroid Build Coastguard Worker     case 2:
741*77c1e3ccSAndroid Build Coastguard Worker       // 3-temporal layer:
742*77c1e3ccSAndroid Build Coastguard Worker       //   1    3   5    7
743*77c1e3ccSAndroid Build Coastguard Worker       //     2        6
744*77c1e3ccSAndroid Build Coastguard Worker       // 0        4        8
745*77c1e3ccSAndroid Build Coastguard Worker       if (superframe_cnt % 4 == 0) {
746*77c1e3ccSAndroid Build Coastguard Worker         // Base layer.
747*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
748*77c1e3ccSAndroid Build Coastguard Worker         // Update LAST on layer 0, reference LAST.
749*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[0] = 1;
750*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
751*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 1) % 4 == 0) {
752*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
753*77c1e3ccSAndroid Build Coastguard Worker         // First top layer: no updates, only reference LAST (TL0).
754*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
755*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 2) % 4 == 0) {
756*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 1;
757*77c1e3ccSAndroid Build Coastguard Worker         // Middle layer (TL1): update LAST2, only reference LAST (TL0).
758*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[1] = 1;
759*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
760*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 3) % 4 == 0) {
761*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
762*77c1e3ccSAndroid Build Coastguard Worker         // Second top layer: no updates, only reference LAST.
763*77c1e3ccSAndroid Build Coastguard Worker         // Set buffer idx for LAST to slot 1, since that was the slot
764*77c1e3ccSAndroid Build Coastguard Worker         // updated in previous frame. So LAST is TL1 frame.
765*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
766*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
767*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
768*77c1e3ccSAndroid Build Coastguard Worker       }
769*77c1e3ccSAndroid Build Coastguard Worker       break;
770*77c1e3ccSAndroid Build Coastguard Worker     case 3:
771*77c1e3ccSAndroid Build Coastguard Worker       // 3 TL, same as above, except allow for predicting
772*77c1e3ccSAndroid Build Coastguard Worker       // off 2 more references (GOLDEN and ALTREF), with
773*77c1e3ccSAndroid Build Coastguard Worker       // GOLDEN updated periodically, and ALTREF lagging from
774*77c1e3ccSAndroid Build Coastguard Worker       // LAST from ~4 frames. Both GOLDEN and ALTREF
775*77c1e3ccSAndroid Build Coastguard Worker       // can only be updated on base temporal layer.
776*77c1e3ccSAndroid Build Coastguard Worker 
777*77c1e3ccSAndroid Build Coastguard Worker       // Keep golden fixed at slot 3.
778*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
779*77c1e3ccSAndroid Build Coastguard Worker       // Cyclically refresh slots 5, 6, 7, for lag altref.
780*77c1e3ccSAndroid Build Coastguard Worker       lag_index = 5;
781*77c1e3ccSAndroid Build Coastguard Worker       if (base_count > 0) {
782*77c1e3ccSAndroid Build Coastguard Worker         lag_index = 5 + (base_count % 3);
783*77c1e3ccSAndroid Build Coastguard Worker         if (superframe_cnt % 4 != 0) lag_index = 5 + ((base_count + 1) % 3);
784*77c1e3ccSAndroid Build Coastguard Worker       }
785*77c1e3ccSAndroid Build Coastguard Worker       // Set the altref slot to lag_index.
786*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = lag_index;
787*77c1e3ccSAndroid Build Coastguard Worker       if (superframe_cnt % 4 == 0) {
788*77c1e3ccSAndroid Build Coastguard Worker         // Base layer.
789*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
790*77c1e3ccSAndroid Build Coastguard Worker         // Update LAST on layer 0, reference LAST.
791*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[0] = 1;
792*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
793*77c1e3ccSAndroid Build Coastguard Worker         // Refresh GOLDEN every x ~10 base layer frames.
794*77c1e3ccSAndroid Build Coastguard Worker         if (base_count % 10 == 0) ref_frame_config->refresh[3] = 1;
795*77c1e3ccSAndroid Build Coastguard Worker         // Refresh lag_index slot, needed for lagging altref.
796*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[lag_index] = 1;
797*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 1) % 4 == 0) {
798*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
799*77c1e3ccSAndroid Build Coastguard Worker         // First top layer: no updates, only reference LAST (TL0).
800*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
801*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 2) % 4 == 0) {
802*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 1;
803*77c1e3ccSAndroid Build Coastguard Worker         // Middle layer (TL1): update LAST2, only reference LAST (TL0).
804*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[1] = 1;
805*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
806*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 3) % 4 == 0) {
807*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
808*77c1e3ccSAndroid Build Coastguard Worker         // Second top layer: no updates, only reference LAST.
809*77c1e3ccSAndroid Build Coastguard Worker         // Set buffer idx for LAST to slot 1, since that was the slot
810*77c1e3ccSAndroid Build Coastguard Worker         // updated in previous frame. So LAST is TL1 frame.
811*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
812*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
813*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
814*77c1e3ccSAndroid Build Coastguard Worker       }
815*77c1e3ccSAndroid Build Coastguard Worker       // Every frame can reference GOLDEN AND ALTREF.
816*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
817*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
818*77c1e3ccSAndroid Build Coastguard Worker       // Allow for compound prediction for LAST-ALTREF and LAST-GOLDEN.
819*77c1e3ccSAndroid Build Coastguard Worker       if (speed >= 7) {
820*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_comp_pred->use_comp_pred[2] = 1;
821*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_comp_pred->use_comp_pred[0] = 1;
822*77c1e3ccSAndroid Build Coastguard Worker       }
823*77c1e3ccSAndroid Build Coastguard Worker       break;
824*77c1e3ccSAndroid Build Coastguard Worker     case 4:
825*77c1e3ccSAndroid Build Coastguard Worker       // 3-temporal layer: but middle layer updates GF, so 2nd TL2 will
826*77c1e3ccSAndroid Build Coastguard Worker       // only reference GF (not LAST). Other frames only reference LAST.
827*77c1e3ccSAndroid Build Coastguard Worker       //   1    3   5    7
828*77c1e3ccSAndroid Build Coastguard Worker       //     2        6
829*77c1e3ccSAndroid Build Coastguard Worker       // 0        4        8
830*77c1e3ccSAndroid Build Coastguard Worker       if (superframe_cnt % 4 == 0) {
831*77c1e3ccSAndroid Build Coastguard Worker         // Base layer.
832*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
833*77c1e3ccSAndroid Build Coastguard Worker         // Update LAST on layer 0, only reference LAST.
834*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[0] = 1;
835*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
836*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 1) % 4 == 0) {
837*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
838*77c1e3ccSAndroid Build Coastguard Worker         // First top layer: no updates, only reference LAST (TL0).
839*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
840*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 2) % 4 == 0) {
841*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 1;
842*77c1e3ccSAndroid Build Coastguard Worker         // Middle layer (TL1): update GF, only reference LAST (TL0).
843*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[3] = 1;
844*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
845*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 3) % 4 == 0) {
846*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
847*77c1e3ccSAndroid Build Coastguard Worker         // Second top layer: no updates, only reference GF.
848*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
849*77c1e3ccSAndroid Build Coastguard Worker       }
850*77c1e3ccSAndroid Build Coastguard Worker       break;
851*77c1e3ccSAndroid Build Coastguard Worker     case 5:
852*77c1e3ccSAndroid Build Coastguard Worker       // 2 spatial layers, 1 temporal.
853*77c1e3ccSAndroid Build Coastguard Worker       layer_id->temporal_layer_id = 0;
854*77c1e3ccSAndroid Build Coastguard Worker       if (layer_id->spatial_layer_id == 0) {
855*77c1e3ccSAndroid Build Coastguard Worker         // Reference LAST, update LAST.
856*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[0] = 1;
857*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
858*77c1e3ccSAndroid Build Coastguard Worker       } else if (layer_id->spatial_layer_id == 1) {
859*77c1e3ccSAndroid Build Coastguard Worker         // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
860*77c1e3ccSAndroid Build Coastguard Worker         // and GOLDEN to slot 0. Update slot 1 (LAST).
861*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
862*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 0;
863*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[1] = 1;
864*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
865*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
866*77c1e3ccSAndroid Build Coastguard Worker       }
867*77c1e3ccSAndroid Build Coastguard Worker       break;
868*77c1e3ccSAndroid Build Coastguard Worker     case 6:
869*77c1e3ccSAndroid Build Coastguard Worker       // 3 spatial layers, 1 temporal.
870*77c1e3ccSAndroid Build Coastguard Worker       // Note for this case, we set the buffer idx for all references to be
871*77c1e3ccSAndroid Build Coastguard Worker       // either LAST or GOLDEN, which are always valid references, since decoder
872*77c1e3ccSAndroid Build Coastguard Worker       // will check if any of the 7 references is valid scale in
873*77c1e3ccSAndroid Build Coastguard Worker       // valid_ref_frame_size().
874*77c1e3ccSAndroid Build Coastguard Worker       layer_id->temporal_layer_id = 0;
875*77c1e3ccSAndroid Build Coastguard Worker       if (layer_id->spatial_layer_id == 0) {
876*77c1e3ccSAndroid Build Coastguard Worker         // Reference LAST, update LAST. Set all buffer_idx to 0.
877*77c1e3ccSAndroid Build Coastguard Worker         for (i = 0; i < INTER_REFS_PER_FRAME; i++)
878*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[i] = 0;
879*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[0] = 1;
880*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
881*77c1e3ccSAndroid Build Coastguard Worker       } else if (layer_id->spatial_layer_id == 1) {
882*77c1e3ccSAndroid Build Coastguard Worker         // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
883*77c1e3ccSAndroid Build Coastguard Worker         // and GOLDEN (and all other refs) to slot 0.
884*77c1e3ccSAndroid Build Coastguard Worker         // Update slot 1 (LAST).
885*77c1e3ccSAndroid Build Coastguard Worker         for (i = 0; i < INTER_REFS_PER_FRAME; i++)
886*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[i] = 0;
887*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
888*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[1] = 1;
889*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
890*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
891*77c1e3ccSAndroid Build Coastguard Worker       } else if (layer_id->spatial_layer_id == 2) {
892*77c1e3ccSAndroid Build Coastguard Worker         // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2
893*77c1e3ccSAndroid Build Coastguard Worker         // and GOLDEN (and all other refs) to slot 1.
894*77c1e3ccSAndroid Build Coastguard Worker         // Update slot 2 (LAST).
895*77c1e3ccSAndroid Build Coastguard Worker         for (i = 0; i < INTER_REFS_PER_FRAME; i++)
896*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[i] = 1;
897*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
898*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->refresh[2] = 1;
899*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
900*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
901*77c1e3ccSAndroid Build Coastguard Worker         // For 3 spatial layer case: allow for top spatial layer to use
902*77c1e3ccSAndroid Build Coastguard Worker         // additional temporal reference. Update every 10 frames.
903*77c1e3ccSAndroid Build Coastguard Worker         if (enable_longterm_temporal_ref) {
904*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
905*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
906*77c1e3ccSAndroid Build Coastguard Worker           if (base_count % 10 == 0)
907*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->refresh[REF_FRAMES - 1] = 1;
908*77c1e3ccSAndroid Build Coastguard Worker         }
909*77c1e3ccSAndroid Build Coastguard Worker       }
910*77c1e3ccSAndroid Build Coastguard Worker       break;
911*77c1e3ccSAndroid Build Coastguard Worker     case 7:
912*77c1e3ccSAndroid Build Coastguard Worker       // 2 spatial and 3 temporal layer.
913*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_config->reference[SVC_LAST_FRAME] = 1;
914*77c1e3ccSAndroid Build Coastguard Worker       if (superframe_cnt % 4 == 0) {
915*77c1e3ccSAndroid Build Coastguard Worker         // Base temporal layer
916*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
917*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
918*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST, update LAST
919*77c1e3ccSAndroid Build Coastguard Worker           // Set all buffer_idx to 0
920*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
921*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
922*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[0] = 1;
923*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
924*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN.
925*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
926*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
927*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
928*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[1] = 1;
929*77c1e3ccSAndroid Build Coastguard Worker         }
930*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 1) % 4 == 0) {
931*77c1e3ccSAndroid Build Coastguard Worker         // First top temporal enhancement layer.
932*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
933*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
934*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
935*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
936*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
937*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[3] = 1;
938*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
939*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
940*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN (and all other refs) to slot 3.
941*77c1e3ccSAndroid Build Coastguard Worker           // No update.
942*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
943*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 3;
944*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
945*77c1e3ccSAndroid Build Coastguard Worker         }
946*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 2) % 4 == 0) {
947*77c1e3ccSAndroid Build Coastguard Worker         // Middle temporal enhancement layer.
948*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 1;
949*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
950*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST.
951*77c1e3ccSAndroid Build Coastguard Worker           // Set all buffer_idx to 0.
952*77c1e3ccSAndroid Build Coastguard Worker           // Set GOLDEN to slot 5 and update slot 5.
953*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
954*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
955*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
956*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[5 - shift] = 1;
957*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
958*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
959*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN (and all other refs) to slot 5.
960*77c1e3ccSAndroid Build Coastguard Worker           // Set LAST3 to slot 6 and update slot 6.
961*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
962*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 5 - shift;
963*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
964*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
965*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[6 - shift] = 1;
966*77c1e3ccSAndroid Build Coastguard Worker         }
967*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 3) % 4 == 0) {
968*77c1e3ccSAndroid Build Coastguard Worker         // Second top temporal enhancement layer.
969*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
970*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
971*77c1e3ccSAndroid Build Coastguard Worker           // Set LAST to slot 5 and reference LAST.
972*77c1e3ccSAndroid Build Coastguard Worker           // Set GOLDEN to slot 3 and update slot 3.
973*77c1e3ccSAndroid Build Coastguard Worker           // Set all other buffer_idx to 0.
974*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
975*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
976*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
977*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
978*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[3] = 1;
979*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
980*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
981*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN to slot 3. No update.
982*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
983*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
984*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
985*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
986*77c1e3ccSAndroid Build Coastguard Worker         }
987*77c1e3ccSAndroid Build Coastguard Worker       }
988*77c1e3ccSAndroid Build Coastguard Worker       break;
989*77c1e3ccSAndroid Build Coastguard Worker     case 8:
990*77c1e3ccSAndroid Build Coastguard Worker       // 3 spatial and 3 temporal layer.
991*77c1e3ccSAndroid Build Coastguard Worker       // Same as case 9 but overalap in the buffer slot updates.
992*77c1e3ccSAndroid Build Coastguard Worker       // (shift = 2). The slots 3 and 4 updated by first TL2 are
993*77c1e3ccSAndroid Build Coastguard Worker       // reused for update in TL1 superframe.
994*77c1e3ccSAndroid Build Coastguard Worker       // Note for this case, frame order hint must be disabled for
995*77c1e3ccSAndroid Build Coastguard Worker       // lower resolutios (operating points > 0) to be decoedable.
996*77c1e3ccSAndroid Build Coastguard Worker     case 9:
997*77c1e3ccSAndroid Build Coastguard Worker       // 3 spatial and 3 temporal layer.
998*77c1e3ccSAndroid Build Coastguard Worker       // No overlap in buffer updates between TL2 and TL1.
999*77c1e3ccSAndroid Build Coastguard Worker       // TL2 updates slot 3 and 4, TL1 updates 5, 6, 7.
1000*77c1e3ccSAndroid Build Coastguard Worker       // Set the references via the svc_ref_frame_config control.
1001*77c1e3ccSAndroid Build Coastguard Worker       // Always reference LAST.
1002*77c1e3ccSAndroid Build Coastguard Worker       ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1003*77c1e3ccSAndroid Build Coastguard Worker       if (superframe_cnt % 4 == 0) {
1004*77c1e3ccSAndroid Build Coastguard Worker         // Base temporal layer.
1005*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
1006*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
1007*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST, update LAST.
1008*77c1e3ccSAndroid Build Coastguard Worker           // Set all buffer_idx to 0.
1009*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1010*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
1011*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[0] = 1;
1012*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
1013*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
1014*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN (and all other refs) to slot 0.
1015*77c1e3ccSAndroid Build Coastguard Worker           // Update slot 1 (LAST).
1016*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1017*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
1018*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1019*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[1] = 1;
1020*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {
1021*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
1022*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN (and all other refs) to slot 1.
1023*77c1e3ccSAndroid Build Coastguard Worker           // Update slot 2 (LAST).
1024*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1025*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 1;
1026*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1027*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[2] = 1;
1028*77c1e3ccSAndroid Build Coastguard Worker         }
1029*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 1) % 4 == 0) {
1030*77c1e3ccSAndroid Build Coastguard Worker         // First top temporal enhancement layer.
1031*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
1032*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
1033*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 0).
1034*77c1e3ccSAndroid Build Coastguard Worker           // Set GOLDEN to slot 3 and update slot 3.
1035*77c1e3ccSAndroid Build Coastguard Worker           // Set all other buffer_idx to slot 0.
1036*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1037*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
1038*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1039*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[3] = 1;
1040*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
1041*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
1042*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN (and all other refs) to slot 3.
1043*77c1e3ccSAndroid Build Coastguard Worker           // Set LAST2 to slot 4 and Update slot 4.
1044*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1045*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 3;
1046*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1047*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
1048*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[4] = 1;
1049*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {
1050*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
1051*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN (and all other refs) to slot 4.
1052*77c1e3ccSAndroid Build Coastguard Worker           // No update.
1053*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1054*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 4;
1055*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1056*77c1e3ccSAndroid Build Coastguard Worker         }
1057*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 2) % 4 == 0) {
1058*77c1e3ccSAndroid Build Coastguard Worker         // Middle temporal enhancement layer.
1059*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 1;
1060*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
1061*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST.
1062*77c1e3ccSAndroid Build Coastguard Worker           // Set all buffer_idx to 0.
1063*77c1e3ccSAndroid Build Coastguard Worker           // Set GOLDEN to slot 5 and update slot 5.
1064*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1065*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
1066*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
1067*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[5 - shift] = 1;
1068*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
1069*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
1070*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN (and all other refs) to slot 5.
1071*77c1e3ccSAndroid Build Coastguard Worker           // Set LAST3 to slot 6 and update slot 6.
1072*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1073*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 5 - shift;
1074*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1075*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
1076*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[6 - shift] = 1;
1077*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {
1078*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
1079*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN (and all other refs) to slot 6.
1080*77c1e3ccSAndroid Build Coastguard Worker           // Set LAST3 to slot 7 and update slot 7.
1081*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1082*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 6 - shift;
1083*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1084*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 7 - shift;
1085*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[7 - shift] = 1;
1086*77c1e3ccSAndroid Build Coastguard Worker         }
1087*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 3) % 4 == 0) {
1088*77c1e3ccSAndroid Build Coastguard Worker         // Second top temporal enhancement layer.
1089*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
1090*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
1091*77c1e3ccSAndroid Build Coastguard Worker           // Set LAST to slot 5 and reference LAST.
1092*77c1e3ccSAndroid Build Coastguard Worker           // Set GOLDEN to slot 3 and update slot 3.
1093*77c1e3ccSAndroid Build Coastguard Worker           // Set all other buffer_idx to 0.
1094*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1095*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
1096*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
1097*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1098*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[3] = 1;
1099*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
1100*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
1101*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN to slot 3. Set LAST2 to slot 4 and update slot 4.
1102*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1103*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
1104*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
1105*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1106*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
1107*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[4] = 1;
1108*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {
1109*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 7,
1110*77c1e3ccSAndroid Build Coastguard Worker           // GOLDEN to slot 4. No update.
1111*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1112*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
1113*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 7 - shift;
1114*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 4;
1115*77c1e3ccSAndroid Build Coastguard Worker         }
1116*77c1e3ccSAndroid Build Coastguard Worker       }
1117*77c1e3ccSAndroid Build Coastguard Worker       break;
1118*77c1e3ccSAndroid Build Coastguard Worker     case 11:
1119*77c1e3ccSAndroid Build Coastguard Worker       // Simulcast mode for 3 spatial and 3 temporal layers.
1120*77c1e3ccSAndroid Build Coastguard Worker       // No inter-layer predicton, only prediction is temporal and single
1121*77c1e3ccSAndroid Build Coastguard Worker       // reference (LAST).
1122*77c1e3ccSAndroid Build Coastguard Worker       // No overlap in buffer slots between spatial layers. So for example,
1123*77c1e3ccSAndroid Build Coastguard Worker       // SL0 only uses slots 0 and 1.
1124*77c1e3ccSAndroid Build Coastguard Worker       // SL1 only uses slots 2 and 3.
1125*77c1e3ccSAndroid Build Coastguard Worker       // SL2 only uses slots 4 and 5.
1126*77c1e3ccSAndroid Build Coastguard Worker       // All 7 references for each inter-frame must only access buffer slots
1127*77c1e3ccSAndroid Build Coastguard Worker       // for that spatial layer.
1128*77c1e3ccSAndroid Build Coastguard Worker       // On key (super)frames: SL1 and SL2 must have no references set
1129*77c1e3ccSAndroid Build Coastguard Worker       // and must refresh all the slots for that layer only (so 2 and 3
1130*77c1e3ccSAndroid Build Coastguard Worker       // for SL1, 4 and 5 for SL2). The base SL0 will be labelled internally
1131*77c1e3ccSAndroid Build Coastguard Worker       // as a Key frame (refresh all slots). SL1/SL2 will be labelled
1132*77c1e3ccSAndroid Build Coastguard Worker       // internally as Intra-only frames that allow that stream to be decoded.
1133*77c1e3ccSAndroid Build Coastguard Worker       // These conditions will allow for each spatial stream to be
1134*77c1e3ccSAndroid Build Coastguard Worker       // independently decodeable.
1135*77c1e3ccSAndroid Build Coastguard Worker 
1136*77c1e3ccSAndroid Build Coastguard Worker       // Initialize all references to 0 (don't use reference).
1137*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1138*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[i] = 0;
1139*77c1e3ccSAndroid Build Coastguard Worker       // Initialize as no refresh/update for all slots.
1140*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
1141*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1142*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[i] = 0;
1143*77c1e3ccSAndroid Build Coastguard Worker 
1144*77c1e3ccSAndroid Build Coastguard Worker       if (is_key_frame) {
1145*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {
1146*77c1e3ccSAndroid Build Coastguard Worker           // Assign LAST/GOLDEN to slot 0/1.
1147*77c1e3ccSAndroid Build Coastguard Worker           // Refesh slots 0 and 1 for SL0.
1148*77c1e3ccSAndroid Build Coastguard Worker           // SL0: this will get set to KEY frame internally.
1149*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
1150*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 1;
1151*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[0] = 1;
1152*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[1] = 1;
1153*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {
1154*77c1e3ccSAndroid Build Coastguard Worker           // Assign LAST/GOLDEN to slot 2/3.
1155*77c1e3ccSAndroid Build Coastguard Worker           // Refesh slots 2 and 3 for SL1.
1156*77c1e3ccSAndroid Build Coastguard Worker           // This will get set to Intra-only frame internally.
1157*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1158*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1159*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[2] = 1;
1160*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[3] = 1;
1161*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {
1162*77c1e3ccSAndroid Build Coastguard Worker           // Assign LAST/GOLDEN to slot 4/5.
1163*77c1e3ccSAndroid Build Coastguard Worker           // Refresh slots 4 and 5 for SL2.
1164*77c1e3ccSAndroid Build Coastguard Worker           // This will get set to Intra-only frame internally.
1165*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
1166*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5;
1167*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[4] = 1;
1168*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[5] = 1;
1169*77c1e3ccSAndroid Build Coastguard Worker         }
1170*77c1e3ccSAndroid Build Coastguard Worker       } else if (superframe_cnt % 4 == 0) {
1171*77c1e3ccSAndroid Build Coastguard Worker         // Base temporal layer: TL0
1172*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 0;
1173*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {  // SL0
1174*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST. Assign all references to either slot
1175*77c1e3ccSAndroid Build Coastguard Worker           // 0 or 1. Here we assign LAST to slot 0, all others to 1.
1176*77c1e3ccSAndroid Build Coastguard Worker           // Update slot 0 (LAST).
1177*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1178*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1179*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 1;
1180*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
1181*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[0] = 1;
1182*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {  // SL1
1183*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST. Assign all references to either slot
1184*77c1e3ccSAndroid Build Coastguard Worker           // 2 or 3. Here we assign LAST to slot 2, all others to 3.
1185*77c1e3ccSAndroid Build Coastguard Worker           // Update slot 2 (LAST).
1186*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1187*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1188*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 3;
1189*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1190*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[2] = 1;
1191*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {  // SL2
1192*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST. Assign all references to either slot
1193*77c1e3ccSAndroid Build Coastguard Worker           // 4 or 5. Here we assign LAST to slot 4, all others to 5.
1194*77c1e3ccSAndroid Build Coastguard Worker           // Update slot 4 (LAST).
1195*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1196*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1197*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 5;
1198*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
1199*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[4] = 1;
1200*77c1e3ccSAndroid Build Coastguard Worker         }
1201*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 1) % 4 == 0) {
1202*77c1e3ccSAndroid Build Coastguard Worker         // First top temporal enhancement layer: TL2
1203*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
1204*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {  // SL0
1205*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 0). Assign other references to slot 1.
1206*77c1e3ccSAndroid Build Coastguard Worker           // No update/refresh on any slots.
1207*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1208*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1209*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 1;
1210*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
1211*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {  // SL1
1212*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 2). Assign other references to slot 3.
1213*77c1e3ccSAndroid Build Coastguard Worker           // No update/refresh on any slots.
1214*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1215*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1216*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 3;
1217*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1218*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {  // SL2
1219*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 4). Assign other references to slot 4.
1220*77c1e3ccSAndroid Build Coastguard Worker           // No update/refresh on any slots.
1221*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1222*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1223*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 5;
1224*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
1225*77c1e3ccSAndroid Build Coastguard Worker         }
1226*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 2) % 4 == 0) {
1227*77c1e3ccSAndroid Build Coastguard Worker         // Middle temporal enhancement layer: TL1
1228*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 1;
1229*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {  // SL0
1230*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 0).
1231*77c1e3ccSAndroid Build Coastguard Worker           // Set GOLDEN to slot 1 and update slot 1.
1232*77c1e3ccSAndroid Build Coastguard Worker           // This will be used as reference for next TL2.
1233*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1234*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1235*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 1;
1236*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
1237*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[1] = 1;
1238*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {  // SL1
1239*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 2).
1240*77c1e3ccSAndroid Build Coastguard Worker           // Set GOLDEN to slot 3 and update slot 3.
1241*77c1e3ccSAndroid Build Coastguard Worker           // This will be used as reference for next TL2.
1242*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1243*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1244*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 3;
1245*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1246*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[3] = 1;
1247*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {  // SL2
1248*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 4).
1249*77c1e3ccSAndroid Build Coastguard Worker           // Set GOLDEN to slot 5 and update slot 5.
1250*77c1e3ccSAndroid Build Coastguard Worker           // This will be used as reference for next TL2.
1251*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1252*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1253*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 5;
1254*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
1255*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[5] = 1;
1256*77c1e3ccSAndroid Build Coastguard Worker         }
1257*77c1e3ccSAndroid Build Coastguard Worker       } else if ((superframe_cnt - 3) % 4 == 0) {
1258*77c1e3ccSAndroid Build Coastguard Worker         // Second top temporal enhancement layer: TL2
1259*77c1e3ccSAndroid Build Coastguard Worker         layer_id->temporal_layer_id = 2;
1260*77c1e3ccSAndroid Build Coastguard Worker         if (layer_id->spatial_layer_id == 0) {  // SL0
1261*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 1). Assign other references to slot 0.
1262*77c1e3ccSAndroid Build Coastguard Worker           // No update/refresh on any slots.
1263*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1264*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1265*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 0;
1266*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1267*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 1) {  // SL1
1268*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 3). Assign other references to slot 2.
1269*77c1e3ccSAndroid Build Coastguard Worker           // No update/refresh on any slots.
1270*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1271*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1272*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 2;
1273*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 3;
1274*77c1e3ccSAndroid Build Coastguard Worker         } else if (layer_id->spatial_layer_id == 2) {  // SL2
1275*77c1e3ccSAndroid Build Coastguard Worker           // Reference LAST (slot 5). Assign other references to slot 4.
1276*77c1e3ccSAndroid Build Coastguard Worker           // No update/refresh on any slots.
1277*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1278*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1279*77c1e3ccSAndroid Build Coastguard Worker             ref_frame_config->ref_idx[i] = 4;
1280*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5;
1281*77c1e3ccSAndroid Build Coastguard Worker         }
1282*77c1e3ccSAndroid Build Coastguard Worker       }
1283*77c1e3ccSAndroid Build Coastguard Worker       if (!simulcast_mode && layer_id->spatial_layer_id > 0) {
1284*77c1e3ccSAndroid Build Coastguard Worker         // Always reference GOLDEN (inter-layer prediction).
1285*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
1286*77c1e3ccSAndroid Build Coastguard Worker         if (ksvc_mode) {
1287*77c1e3ccSAndroid Build Coastguard Worker           // KSVC: only keep the inter-layer reference (GOLDEN) for
1288*77c1e3ccSAndroid Build Coastguard Worker           // superframes whose base is key.
1289*77c1e3ccSAndroid Build Coastguard Worker           if (!is_key_frame) ref_frame_config->reference[SVC_GOLDEN_FRAME] = 0;
1290*77c1e3ccSAndroid Build Coastguard Worker         }
1291*77c1e3ccSAndroid Build Coastguard Worker         if (is_key_frame && layer_id->spatial_layer_id > 1) {
1292*77c1e3ccSAndroid Build Coastguard Worker           // On superframes whose base is key: remove LAST to avoid prediction
1293*77c1e3ccSAndroid Build Coastguard Worker           // off layer two levels below.
1294*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->reference[SVC_LAST_FRAME] = 0;
1295*77c1e3ccSAndroid Build Coastguard Worker         }
1296*77c1e3ccSAndroid Build Coastguard Worker       }
1297*77c1e3ccSAndroid Build Coastguard Worker       // For 3 spatial layer case 8 (where there is free buffer slot):
1298*77c1e3ccSAndroid Build Coastguard Worker       // allow for top spatial layer to use additional temporal reference.
1299*77c1e3ccSAndroid Build Coastguard Worker       // Additional reference is only updated on base temporal layer, every
1300*77c1e3ccSAndroid Build Coastguard Worker       // 10 TL0 frames here.
1301*77c1e3ccSAndroid Build Coastguard Worker       if (!simulcast_mode && enable_longterm_temporal_ref &&
1302*77c1e3ccSAndroid Build Coastguard Worker           layer_id->spatial_layer_id == 2 && layering_mode == 8) {
1303*77c1e3ccSAndroid Build Coastguard Worker         ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
1304*77c1e3ccSAndroid Build Coastguard Worker         if (!is_key_frame) ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
1305*77c1e3ccSAndroid Build Coastguard Worker         if (base_count % 10 == 0 && layer_id->temporal_layer_id == 0)
1306*77c1e3ccSAndroid Build Coastguard Worker           ref_frame_config->refresh[REF_FRAMES - 1] = 1;
1307*77c1e3ccSAndroid Build Coastguard Worker       }
1308*77c1e3ccSAndroid Build Coastguard Worker       break;
1309*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0); die("Error: Unsupported temporal layering mode!\n");
1310*77c1e3ccSAndroid Build Coastguard Worker   }
1311*77c1e3ccSAndroid Build Coastguard Worker }
1312*77c1e3ccSAndroid Build Coastguard Worker 
1313*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
1314*77c1e3ccSAndroid Build Coastguard Worker // Returns whether there is a mismatch between the encoder's new frame and the
1315*77c1e3ccSAndroid Build Coastguard Worker // decoder's new frame.
test_decode(aom_codec_ctx_t * encoder,aom_codec_ctx_t * decoder,const int frames_out)1316*77c1e3ccSAndroid Build Coastguard Worker static int test_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
1317*77c1e3ccSAndroid Build Coastguard Worker                        const int frames_out) {
1318*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t enc_img, dec_img;
1319*77c1e3ccSAndroid Build Coastguard Worker   int mismatch = 0;
1320*77c1e3ccSAndroid Build Coastguard Worker 
1321*77c1e3ccSAndroid Build Coastguard Worker   /* Get the internal new frame */
1322*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1323*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
1324*77c1e3ccSAndroid Build Coastguard Worker 
1325*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
1326*77c1e3ccSAndroid Build Coastguard Worker   if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1327*77c1e3ccSAndroid Build Coastguard Worker       (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1328*77c1e3ccSAndroid Build Coastguard Worker     if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1329*77c1e3ccSAndroid Build Coastguard Worker       aom_image_t enc_hbd_img;
1330*77c1e3ccSAndroid Build Coastguard Worker       aom_img_alloc(
1331*77c1e3ccSAndroid Build Coastguard Worker           &enc_hbd_img,
1332*77c1e3ccSAndroid Build Coastguard Worker           static_cast<aom_img_fmt_t>(enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH),
1333*77c1e3ccSAndroid Build Coastguard Worker           enc_img.d_w, enc_img.d_h, 16);
1334*77c1e3ccSAndroid Build Coastguard Worker       aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1335*77c1e3ccSAndroid Build Coastguard Worker       enc_img = enc_hbd_img;
1336*77c1e3ccSAndroid Build Coastguard Worker     }
1337*77c1e3ccSAndroid Build Coastguard Worker     if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1338*77c1e3ccSAndroid Build Coastguard Worker       aom_image_t dec_hbd_img;
1339*77c1e3ccSAndroid Build Coastguard Worker       aom_img_alloc(
1340*77c1e3ccSAndroid Build Coastguard Worker           &dec_hbd_img,
1341*77c1e3ccSAndroid Build Coastguard Worker           static_cast<aom_img_fmt_t>(dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH),
1342*77c1e3ccSAndroid Build Coastguard Worker           dec_img.d_w, dec_img.d_h, 16);
1343*77c1e3ccSAndroid Build Coastguard Worker       aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1344*77c1e3ccSAndroid Build Coastguard Worker       dec_img = dec_hbd_img;
1345*77c1e3ccSAndroid Build Coastguard Worker     }
1346*77c1e3ccSAndroid Build Coastguard Worker   }
1347*77c1e3ccSAndroid Build Coastguard Worker #endif
1348*77c1e3ccSAndroid Build Coastguard Worker 
1349*77c1e3ccSAndroid Build Coastguard Worker   if (!aom_compare_img(&enc_img, &dec_img)) {
1350*77c1e3ccSAndroid Build Coastguard Worker     int y[4], u[4], v[4];
1351*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
1352*77c1e3ccSAndroid Build Coastguard Worker     if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1353*77c1e3ccSAndroid Build Coastguard Worker       aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
1354*77c1e3ccSAndroid Build Coastguard Worker     } else {
1355*77c1e3ccSAndroid Build Coastguard Worker       aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1356*77c1e3ccSAndroid Build Coastguard Worker     }
1357*77c1e3ccSAndroid Build Coastguard Worker #else
1358*77c1e3ccSAndroid Build Coastguard Worker     aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1359*77c1e3ccSAndroid Build Coastguard Worker #endif
1360*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr,
1361*77c1e3ccSAndroid Build Coastguard Worker             "Encode/decode mismatch on frame %d at"
1362*77c1e3ccSAndroid Build Coastguard Worker             " Y[%d, %d] {%d/%d},"
1363*77c1e3ccSAndroid Build Coastguard Worker             " U[%d, %d] {%d/%d},"
1364*77c1e3ccSAndroid Build Coastguard Worker             " V[%d, %d] {%d/%d}\n",
1365*77c1e3ccSAndroid Build Coastguard Worker             frames_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0],
1366*77c1e3ccSAndroid Build Coastguard Worker             v[1], v[2], v[3]);
1367*77c1e3ccSAndroid Build Coastguard Worker     mismatch = 1;
1368*77c1e3ccSAndroid Build Coastguard Worker   }
1369*77c1e3ccSAndroid Build Coastguard Worker 
1370*77c1e3ccSAndroid Build Coastguard Worker   aom_img_free(&enc_img);
1371*77c1e3ccSAndroid Build Coastguard Worker   aom_img_free(&dec_img);
1372*77c1e3ccSAndroid Build Coastguard Worker   return mismatch;
1373*77c1e3ccSAndroid Build Coastguard Worker }
1374*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_DECODER
1375*77c1e3ccSAndroid Build Coastguard Worker 
1376*77c1e3ccSAndroid Build Coastguard Worker struct psnr_stats {
1377*77c1e3ccSAndroid Build Coastguard Worker   // The second element of these arrays is reserved for high bitdepth.
1378*77c1e3ccSAndroid Build Coastguard Worker   uint64_t psnr_sse_total[2];
1379*77c1e3ccSAndroid Build Coastguard Worker   uint64_t psnr_samples_total[2];
1380*77c1e3ccSAndroid Build Coastguard Worker   double psnr_totals[2][4];
1381*77c1e3ccSAndroid Build Coastguard Worker   int psnr_count[2];
1382*77c1e3ccSAndroid Build Coastguard Worker };
1383*77c1e3ccSAndroid Build Coastguard Worker 
show_psnr(struct psnr_stats * psnr_stream,double peak)1384*77c1e3ccSAndroid Build Coastguard Worker static void show_psnr(struct psnr_stats *psnr_stream, double peak) {
1385*77c1e3ccSAndroid Build Coastguard Worker   double ovpsnr;
1386*77c1e3ccSAndroid Build Coastguard Worker 
1387*77c1e3ccSAndroid Build Coastguard Worker   if (!psnr_stream->psnr_count[0]) return;
1388*77c1e3ccSAndroid Build Coastguard Worker 
1389*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "\nPSNR (Overall/Avg/Y/U/V)");
1390*77c1e3ccSAndroid Build Coastguard Worker   ovpsnr = sse_to_psnr((double)psnr_stream->psnr_samples_total[0], peak,
1391*77c1e3ccSAndroid Build Coastguard Worker                        (double)psnr_stream->psnr_sse_total[0]);
1392*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, " %.3f", ovpsnr);
1393*77c1e3ccSAndroid Build Coastguard Worker 
1394*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 4; i++) {
1395*77c1e3ccSAndroid Build Coastguard Worker     fprintf(stderr, " %.3f",
1396*77c1e3ccSAndroid Build Coastguard Worker             psnr_stream->psnr_totals[0][i] / psnr_stream->psnr_count[0]);
1397*77c1e3ccSAndroid Build Coastguard Worker   }
1398*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "\n");
1399*77c1e3ccSAndroid Build Coastguard Worker }
1400*77c1e3ccSAndroid Build Coastguard Worker 
create_rtc_rc_config(const aom_codec_enc_cfg_t & cfg,const AppInput & app_input)1401*77c1e3ccSAndroid Build Coastguard Worker static aom::AV1RateControlRtcConfig create_rtc_rc_config(
1402*77c1e3ccSAndroid Build Coastguard Worker     const aom_codec_enc_cfg_t &cfg, const AppInput &app_input) {
1403*77c1e3ccSAndroid Build Coastguard Worker   aom::AV1RateControlRtcConfig rc_cfg;
1404*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.width = cfg.g_w;
1405*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.height = cfg.g_h;
1406*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.max_quantizer = cfg.rc_max_quantizer;
1407*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.min_quantizer = cfg.rc_min_quantizer;
1408*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.target_bandwidth = cfg.rc_target_bitrate;
1409*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.buf_initial_sz = cfg.rc_buf_initial_sz;
1410*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.buf_optimal_sz = cfg.rc_buf_optimal_sz;
1411*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.buf_sz = cfg.rc_buf_sz;
1412*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.overshoot_pct = cfg.rc_overshoot_pct;
1413*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.undershoot_pct = cfg.rc_undershoot_pct;
1414*77c1e3ccSAndroid Build Coastguard Worker   // This is hardcoded as AOME_SET_MAX_INTRA_BITRATE_PCT
1415*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.max_intra_bitrate_pct = 300;
1416*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.framerate = cfg.g_timebase.den;
1417*77c1e3ccSAndroid Build Coastguard Worker   // TODO(jianj): Add suppor for SVC.
1418*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.ss_number_layers = 1;
1419*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.ts_number_layers = 1;
1420*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.scaling_factor_num[0] = 1;
1421*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.scaling_factor_den[0] = 1;
1422*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.layer_target_bitrate[0] = static_cast<int>(rc_cfg.target_bandwidth);
1423*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.max_quantizers[0] = rc_cfg.max_quantizer;
1424*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.min_quantizers[0] = rc_cfg.min_quantizer;
1425*77c1e3ccSAndroid Build Coastguard Worker   rc_cfg.aq_mode = app_input.aq_mode;
1426*77c1e3ccSAndroid Build Coastguard Worker 
1427*77c1e3ccSAndroid Build Coastguard Worker   return rc_cfg;
1428*77c1e3ccSAndroid Build Coastguard Worker }
1429*77c1e3ccSAndroid Build Coastguard Worker 
qindex_to_quantizer(int qindex)1430*77c1e3ccSAndroid Build Coastguard Worker static int qindex_to_quantizer(int qindex) {
1431*77c1e3ccSAndroid Build Coastguard Worker   // Table that converts 0-63 range Q values passed in outside to the 0-255
1432*77c1e3ccSAndroid Build Coastguard Worker   // range Qindex used internally.
1433*77c1e3ccSAndroid Build Coastguard Worker   static const int quantizer_to_qindex[] = {
1434*77c1e3ccSAndroid Build Coastguard Worker     0,   4,   8,   12,  16,  20,  24,  28,  32,  36,  40,  44,  48,
1435*77c1e3ccSAndroid Build Coastguard Worker     52,  56,  60,  64,  68,  72,  76,  80,  84,  88,  92,  96,  100,
1436*77c1e3ccSAndroid Build Coastguard Worker     104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
1437*77c1e3ccSAndroid Build Coastguard Worker     156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
1438*77c1e3ccSAndroid Build Coastguard Worker     208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
1439*77c1e3ccSAndroid Build Coastguard Worker   };
1440*77c1e3ccSAndroid Build Coastguard Worker   for (int quantizer = 0; quantizer < 64; ++quantizer)
1441*77c1e3ccSAndroid Build Coastguard Worker     if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
1442*77c1e3ccSAndroid Build Coastguard Worker 
1443*77c1e3ccSAndroid Build Coastguard Worker   return 63;
1444*77c1e3ccSAndroid Build Coastguard Worker }
1445*77c1e3ccSAndroid Build Coastguard Worker 
set_active_map(const aom_codec_enc_cfg_t * cfg,aom_codec_ctx_t * codec,int frame_cnt)1446*77c1e3ccSAndroid Build Coastguard Worker static void set_active_map(const aom_codec_enc_cfg_t *cfg,
1447*77c1e3ccSAndroid Build Coastguard Worker                            aom_codec_ctx_t *codec, int frame_cnt) {
1448*77c1e3ccSAndroid Build Coastguard Worker   aom_active_map_t map = { 0, 0, 0 };
1449*77c1e3ccSAndroid Build Coastguard Worker 
1450*77c1e3ccSAndroid Build Coastguard Worker   map.rows = (cfg->g_h + 15) / 16;
1451*77c1e3ccSAndroid Build Coastguard Worker   map.cols = (cfg->g_w + 15) / 16;
1452*77c1e3ccSAndroid Build Coastguard Worker 
1453*77c1e3ccSAndroid Build Coastguard Worker   map.active_map = (uint8_t *)malloc(map.rows * map.cols);
1454*77c1e3ccSAndroid Build Coastguard Worker   if (!map.active_map) die("Failed to allocate active map");
1455*77c1e3ccSAndroid Build Coastguard Worker 
1456*77c1e3ccSAndroid Build Coastguard Worker   // Example map for testing.
1457*77c1e3ccSAndroid Build Coastguard Worker   for (unsigned int i = 0; i < map.rows; ++i) {
1458*77c1e3ccSAndroid Build Coastguard Worker     for (unsigned int j = 0; j < map.cols; ++j) {
1459*77c1e3ccSAndroid Build Coastguard Worker       int index = map.cols * i + j;
1460*77c1e3ccSAndroid Build Coastguard Worker       map.active_map[index] = 1;
1461*77c1e3ccSAndroid Build Coastguard Worker       if (frame_cnt < 300) {
1462*77c1e3ccSAndroid Build Coastguard Worker         if (i < map.rows / 2 && j < map.cols / 2) map.active_map[index] = 0;
1463*77c1e3ccSAndroid Build Coastguard Worker       } else if (frame_cnt >= 300) {
1464*77c1e3ccSAndroid Build Coastguard Worker         if (i < map.rows / 2 && j >= map.cols / 2) map.active_map[index] = 0;
1465*77c1e3ccSAndroid Build Coastguard Worker       }
1466*77c1e3ccSAndroid Build Coastguard Worker     }
1467*77c1e3ccSAndroid Build Coastguard Worker   }
1468*77c1e3ccSAndroid Build Coastguard Worker 
1469*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(codec, AOME_SET_ACTIVEMAP, &map))
1470*77c1e3ccSAndroid Build Coastguard Worker     die_codec(codec, "Failed to set active map");
1471*77c1e3ccSAndroid Build Coastguard Worker 
1472*77c1e3ccSAndroid Build Coastguard Worker   free(map.active_map);
1473*77c1e3ccSAndroid Build Coastguard Worker }
1474*77c1e3ccSAndroid Build Coastguard Worker 
main(int argc,const char ** argv)1475*77c1e3ccSAndroid Build Coastguard Worker int main(int argc, const char **argv) {
1476*77c1e3ccSAndroid Build Coastguard Worker   AppInput app_input;
1477*77c1e3ccSAndroid Build Coastguard Worker   AvxVideoWriter *outfile[AOM_MAX_LAYERS] = { NULL };
1478*77c1e3ccSAndroid Build Coastguard Worker   FILE *obu_files[AOM_MAX_LAYERS] = { NULL };
1479*77c1e3ccSAndroid Build Coastguard Worker   AvxVideoWriter *total_layer_file = NULL;
1480*77c1e3ccSAndroid Build Coastguard Worker   FILE *total_layer_obu_file = NULL;
1481*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_enc_cfg_t cfg;
1482*77c1e3ccSAndroid Build Coastguard Worker   int frame_cnt = 0;
1483*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t raw;
1484*77c1e3ccSAndroid Build Coastguard Worker   int frame_avail;
1485*77c1e3ccSAndroid Build Coastguard Worker   int got_data = 0;
1486*77c1e3ccSAndroid Build Coastguard Worker   int flags = 0;
1487*77c1e3ccSAndroid Build Coastguard Worker   int i;
1488*77c1e3ccSAndroid Build Coastguard Worker   int pts = 0;             // PTS starts at 0.
1489*77c1e3ccSAndroid Build Coastguard Worker   int frame_duration = 1;  // 1 timebase tick per frame.
1490*77c1e3ccSAndroid Build Coastguard Worker   aom_svc_layer_id_t layer_id;
1491*77c1e3ccSAndroid Build Coastguard Worker   aom_svc_params_t svc_params;
1492*77c1e3ccSAndroid Build Coastguard Worker   aom_svc_ref_frame_config_t ref_frame_config;
1493*77c1e3ccSAndroid Build Coastguard Worker   aom_svc_ref_frame_comp_pred_t ref_frame_comp_pred;
1494*77c1e3ccSAndroid Build Coastguard Worker 
1495*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
1496*77c1e3ccSAndroid Build Coastguard Worker   FILE *stats_file = fopen("opsnr.stt", "a");
1497*77c1e3ccSAndroid Build Coastguard Worker   if (stats_file == NULL) {
1498*77c1e3ccSAndroid Build Coastguard Worker     die("Cannot open opsnr.stt\n");
1499*77c1e3ccSAndroid Build Coastguard Worker   }
1500*77c1e3ccSAndroid Build Coastguard Worker #endif
1501*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
1502*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_ctx_t decoder;
1503*77c1e3ccSAndroid Build Coastguard Worker #endif
1504*77c1e3ccSAndroid Build Coastguard Worker 
1505*77c1e3ccSAndroid Build Coastguard Worker   struct RateControlMetrics rc;
1506*77c1e3ccSAndroid Build Coastguard Worker   int64_t cx_time = 0;
1507*77c1e3ccSAndroid Build Coastguard Worker   int64_t cx_time_layer[AOM_MAX_LAYERS];  // max number of layers.
1508*77c1e3ccSAndroid Build Coastguard Worker   int frame_cnt_layer[AOM_MAX_LAYERS];
1509*77c1e3ccSAndroid Build Coastguard Worker   double sum_bitrate = 0.0;
1510*77c1e3ccSAndroid Build Coastguard Worker   double sum_bitrate2 = 0.0;
1511*77c1e3ccSAndroid Build Coastguard Worker   double framerate = 30.0;
1512*77c1e3ccSAndroid Build Coastguard Worker   int use_svc_control = 1;
1513*77c1e3ccSAndroid Build Coastguard Worker   int set_err_resil_frame = 0;
1514*77c1e3ccSAndroid Build Coastguard Worker   int test_changing_bitrate = 0;
1515*77c1e3ccSAndroid Build Coastguard Worker   zero(rc.layer_target_bitrate);
1516*77c1e3ccSAndroid Build Coastguard Worker   memset(&layer_id, 0, sizeof(aom_svc_layer_id_t));
1517*77c1e3ccSAndroid Build Coastguard Worker   memset(&app_input, 0, sizeof(AppInput));
1518*77c1e3ccSAndroid Build Coastguard Worker   memset(&svc_params, 0, sizeof(svc_params));
1519*77c1e3ccSAndroid Build Coastguard Worker 
1520*77c1e3ccSAndroid Build Coastguard Worker   // Flag to test dynamic scaling of source frames for single
1521*77c1e3ccSAndroid Build Coastguard Worker   // spatial stream, using the scaling_mode control.
1522*77c1e3ccSAndroid Build Coastguard Worker   const int test_dynamic_scaling_single_layer = 0;
1523*77c1e3ccSAndroid Build Coastguard Worker 
1524*77c1e3ccSAndroid Build Coastguard Worker   // Flag to test setting speed per layer.
1525*77c1e3ccSAndroid Build Coastguard Worker   const int test_speed_per_layer = 0;
1526*77c1e3ccSAndroid Build Coastguard Worker 
1527*77c1e3ccSAndroid Build Coastguard Worker   // Flag for testing active maps.
1528*77c1e3ccSAndroid Build Coastguard Worker   const int test_active_maps = 0;
1529*77c1e3ccSAndroid Build Coastguard Worker 
1530*77c1e3ccSAndroid Build Coastguard Worker   /* Setup default input stream settings */
1531*77c1e3ccSAndroid Build Coastguard Worker   app_input.input_ctx.framerate.numerator = 30;
1532*77c1e3ccSAndroid Build Coastguard Worker   app_input.input_ctx.framerate.denominator = 1;
1533*77c1e3ccSAndroid Build Coastguard Worker   app_input.input_ctx.only_i420 = 0;
1534*77c1e3ccSAndroid Build Coastguard Worker   app_input.input_ctx.bit_depth = AOM_BITS_8;
1535*77c1e3ccSAndroid Build Coastguard Worker   app_input.speed = 7;
1536*77c1e3ccSAndroid Build Coastguard Worker   exec_name = argv[0];
1537*77c1e3ccSAndroid Build Coastguard Worker 
1538*77c1e3ccSAndroid Build Coastguard Worker   // start with default encoder configuration
1539*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res = aom_codec_enc_config_default(aom_codec_av1_cx(), &cfg,
1540*77c1e3ccSAndroid Build Coastguard Worker                                                      AOM_USAGE_REALTIME);
1541*77c1e3ccSAndroid Build Coastguard Worker   if (res != AOM_CODEC_OK) {
1542*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to get config: %s\n", aom_codec_err_to_string(res));
1543*77c1e3ccSAndroid Build Coastguard Worker   }
1544*77c1e3ccSAndroid Build Coastguard Worker 
1545*77c1e3ccSAndroid Build Coastguard Worker   // Real time parameters.
1546*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_usage = AOM_USAGE_REALTIME;
1547*77c1e3ccSAndroid Build Coastguard Worker 
1548*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_end_usage = AOM_CBR;
1549*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_min_quantizer = 2;
1550*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_max_quantizer = 52;
1551*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_undershoot_pct = 50;
1552*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_overshoot_pct = 50;
1553*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_buf_initial_sz = 600;
1554*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_buf_optimal_sz = 600;
1555*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_buf_sz = 1000;
1556*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_resize_mode = 0;  // Set to RESIZE_DYNAMIC for dynamic resize.
1557*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_lag_in_frames = 0;
1558*77c1e3ccSAndroid Build Coastguard Worker   cfg.kf_mode = AOM_KF_AUTO;
1559*77c1e3ccSAndroid Build Coastguard Worker 
1560*77c1e3ccSAndroid Build Coastguard Worker   parse_command_line(argc, argv, &app_input, &svc_params, &cfg);
1561*77c1e3ccSAndroid Build Coastguard Worker 
1562*77c1e3ccSAndroid Build Coastguard Worker   int ts_number_layers = svc_params.number_temporal_layers;
1563*77c1e3ccSAndroid Build Coastguard Worker   int ss_number_layers = svc_params.number_spatial_layers;
1564*77c1e3ccSAndroid Build Coastguard Worker 
1565*77c1e3ccSAndroid Build Coastguard Worker   unsigned int width = cfg.g_w;
1566*77c1e3ccSAndroid Build Coastguard Worker   unsigned int height = cfg.g_h;
1567*77c1e3ccSAndroid Build Coastguard Worker 
1568*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.layering_mode >= 0) {
1569*77c1e3ccSAndroid Build Coastguard Worker     if (ts_number_layers !=
1570*77c1e3ccSAndroid Build Coastguard Worker             mode_to_num_temporal_layers[app_input.layering_mode] ||
1571*77c1e3ccSAndroid Build Coastguard Worker         ss_number_layers !=
1572*77c1e3ccSAndroid Build Coastguard Worker             mode_to_num_spatial_layers[app_input.layering_mode]) {
1573*77c1e3ccSAndroid Build Coastguard Worker       die("Number of layers doesn't match layering mode.");
1574*77c1e3ccSAndroid Build Coastguard Worker     }
1575*77c1e3ccSAndroid Build Coastguard Worker   }
1576*77c1e3ccSAndroid Build Coastguard Worker 
1577*77c1e3ccSAndroid Build Coastguard Worker   // Y4M reader has its own allocation.
1578*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
1579*77c1e3ccSAndroid Build Coastguard Worker     if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, width, height, 32)) {
1580*77c1e3ccSAndroid Build Coastguard Worker       die("Failed to allocate image (%dx%d)", width, height);
1581*77c1e3ccSAndroid Build Coastguard Worker     }
1582*77c1e3ccSAndroid Build Coastguard Worker   }
1583*77c1e3ccSAndroid Build Coastguard Worker 
1584*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iface_t *encoder = aom_codec_av1_cx();
1585*77c1e3ccSAndroid Build Coastguard Worker 
1586*77c1e3ccSAndroid Build Coastguard Worker   memcpy(&rc.layer_target_bitrate[0], &svc_params.layer_target_bitrate[0],
1587*77c1e3ccSAndroid Build Coastguard Worker          sizeof(svc_params.layer_target_bitrate));
1588*77c1e3ccSAndroid Build Coastguard Worker 
1589*77c1e3ccSAndroid Build Coastguard Worker   unsigned int total_rate = 0;
1590*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < ss_number_layers; i++) {
1591*77c1e3ccSAndroid Build Coastguard Worker     total_rate +=
1592*77c1e3ccSAndroid Build Coastguard Worker         svc_params
1593*77c1e3ccSAndroid Build Coastguard Worker             .layer_target_bitrate[i * ts_number_layers + ts_number_layers - 1];
1594*77c1e3ccSAndroid Build Coastguard Worker   }
1595*77c1e3ccSAndroid Build Coastguard Worker   if (total_rate != cfg.rc_target_bitrate) {
1596*77c1e3ccSAndroid Build Coastguard Worker     die("Incorrect total target bitrate");
1597*77c1e3ccSAndroid Build Coastguard Worker   }
1598*77c1e3ccSAndroid Build Coastguard Worker 
1599*77c1e3ccSAndroid Build Coastguard Worker   svc_params.framerate_factor[0] = 1;
1600*77c1e3ccSAndroid Build Coastguard Worker   if (ts_number_layers == 2) {
1601*77c1e3ccSAndroid Build Coastguard Worker     svc_params.framerate_factor[0] = 2;
1602*77c1e3ccSAndroid Build Coastguard Worker     svc_params.framerate_factor[1] = 1;
1603*77c1e3ccSAndroid Build Coastguard Worker   } else if (ts_number_layers == 3) {
1604*77c1e3ccSAndroid Build Coastguard Worker     svc_params.framerate_factor[0] = 4;
1605*77c1e3ccSAndroid Build Coastguard Worker     svc_params.framerate_factor[1] = 2;
1606*77c1e3ccSAndroid Build Coastguard Worker     svc_params.framerate_factor[2] = 1;
1607*77c1e3ccSAndroid Build Coastguard Worker   }
1608*77c1e3ccSAndroid Build Coastguard Worker 
1609*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.input_ctx.file_type == FILE_TYPE_Y4M) {
1610*77c1e3ccSAndroid Build Coastguard Worker     // Override these settings with the info from Y4M file.
1611*77c1e3ccSAndroid Build Coastguard Worker     cfg.g_w = app_input.input_ctx.width;
1612*77c1e3ccSAndroid Build Coastguard Worker     cfg.g_h = app_input.input_ctx.height;
1613*77c1e3ccSAndroid Build Coastguard Worker     // g_timebase is the reciprocal of frame rate.
1614*77c1e3ccSAndroid Build Coastguard Worker     cfg.g_timebase.num = app_input.input_ctx.framerate.denominator;
1615*77c1e3ccSAndroid Build Coastguard Worker     cfg.g_timebase.den = app_input.input_ctx.framerate.numerator;
1616*77c1e3ccSAndroid Build Coastguard Worker   }
1617*77c1e3ccSAndroid Build Coastguard Worker   framerate = cfg.g_timebase.den / cfg.g_timebase.num;
1618*77c1e3ccSAndroid Build Coastguard Worker   set_rate_control_metrics(&rc, framerate, ss_number_layers, ts_number_layers);
1619*77c1e3ccSAndroid Build Coastguard Worker 
1620*77c1e3ccSAndroid Build Coastguard Worker   AvxVideoInfo info;
1621*77c1e3ccSAndroid Build Coastguard Worker   info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
1622*77c1e3ccSAndroid Build Coastguard Worker   info.frame_width = cfg.g_w;
1623*77c1e3ccSAndroid Build Coastguard Worker   info.frame_height = cfg.g_h;
1624*77c1e3ccSAndroid Build Coastguard Worker   info.time_base.numerator = cfg.g_timebase.num;
1625*77c1e3ccSAndroid Build Coastguard Worker   info.time_base.denominator = cfg.g_timebase.den;
1626*77c1e3ccSAndroid Build Coastguard Worker   // Open an output file for each stream.
1627*77c1e3ccSAndroid Build Coastguard Worker   for (int sl = 0; sl < ss_number_layers; ++sl) {
1628*77c1e3ccSAndroid Build Coastguard Worker     for (int tl = 0; tl < ts_number_layers; ++tl) {
1629*77c1e3ccSAndroid Build Coastguard Worker       i = sl * ts_number_layers + tl;
1630*77c1e3ccSAndroid Build Coastguard Worker       char file_name[PATH_MAX];
1631*77c1e3ccSAndroid Build Coastguard Worker       snprintf(file_name, sizeof(file_name), "%s_%d.av1",
1632*77c1e3ccSAndroid Build Coastguard Worker                app_input.output_filename, i);
1633*77c1e3ccSAndroid Build Coastguard Worker       if (app_input.output_obu) {
1634*77c1e3ccSAndroid Build Coastguard Worker         obu_files[i] = fopen(file_name, "wb");
1635*77c1e3ccSAndroid Build Coastguard Worker         if (!obu_files[i]) die("Failed to open %s for writing", file_name);
1636*77c1e3ccSAndroid Build Coastguard Worker       } else {
1637*77c1e3ccSAndroid Build Coastguard Worker         outfile[i] = aom_video_writer_open(file_name, kContainerIVF, &info);
1638*77c1e3ccSAndroid Build Coastguard Worker         if (!outfile[i]) die("Failed to open %s for writing", file_name);
1639*77c1e3ccSAndroid Build Coastguard Worker       }
1640*77c1e3ccSAndroid Build Coastguard Worker     }
1641*77c1e3ccSAndroid Build Coastguard Worker   }
1642*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.output_obu) {
1643*77c1e3ccSAndroid Build Coastguard Worker     total_layer_obu_file = fopen(app_input.output_filename, "wb");
1644*77c1e3ccSAndroid Build Coastguard Worker     if (!total_layer_obu_file)
1645*77c1e3ccSAndroid Build Coastguard Worker       die("Failed to open %s for writing", app_input.output_filename);
1646*77c1e3ccSAndroid Build Coastguard Worker   } else {
1647*77c1e3ccSAndroid Build Coastguard Worker     total_layer_file =
1648*77c1e3ccSAndroid Build Coastguard Worker         aom_video_writer_open(app_input.output_filename, kContainerIVF, &info);
1649*77c1e3ccSAndroid Build Coastguard Worker     if (!total_layer_file)
1650*77c1e3ccSAndroid Build Coastguard Worker       die("Failed to open %s for writing", app_input.output_filename);
1651*77c1e3ccSAndroid Build Coastguard Worker   }
1652*77c1e3ccSAndroid Build Coastguard Worker 
1653*77c1e3ccSAndroid Build Coastguard Worker   // Initialize codec.
1654*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_ctx_t codec;
1655*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_flags_t flag = 0;
1656*77c1e3ccSAndroid Build Coastguard Worker   flag |= cfg.g_input_bit_depth == AOM_BITS_8 ? 0 : AOM_CODEC_USE_HIGHBITDEPTH;
1657*77c1e3ccSAndroid Build Coastguard Worker   flag |= app_input.show_psnr ? AOM_CODEC_USE_PSNR : 0;
1658*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_enc_init(&codec, encoder, &cfg, flag))
1659*77c1e3ccSAndroid Build Coastguard Worker     die_codec(&codec, "Failed to initialize encoder");
1660*77c1e3ccSAndroid Build Coastguard Worker 
1661*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
1662*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.decode) {
1663*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_dec_init(&decoder, get_aom_decoder_by_index(0), NULL, 0))
1664*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&decoder, "Failed to initialize decoder");
1665*77c1e3ccSAndroid Build Coastguard Worker   }
1666*77c1e3ccSAndroid Build Coastguard Worker #endif
1667*77c1e3ccSAndroid Build Coastguard Worker 
1668*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AOME_SET_CPUUSED, app_input.speed);
1669*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_AQ_MODE, app_input.aq_mode ? 3 : 0);
1670*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_GF_CBR_BOOST_PCT, 0);
1671*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_CDEF, 1);
1672*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_LOOPFILTER_CONTROL, 1);
1673*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_WARPED_MOTION, 0);
1674*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_OBMC, 0);
1675*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_GLOBAL_MOTION, 0);
1676*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_ORDER_HINT, 0);
1677*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_TPL_MODEL, 0);
1678*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_DELTAQ_MODE, 0);
1679*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_COEFF_COST_UPD_FREQ, 3);
1680*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_MODE_COST_UPD_FREQ, 3);
1681*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_MV_COST_UPD_FREQ, 3);
1682*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_DV_COST_UPD_FREQ, 3);
1683*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_CDF_UPDATE_MODE, 1);
1684*77c1e3ccSAndroid Build Coastguard Worker 
1685*77c1e3ccSAndroid Build Coastguard Worker   // Settings to reduce key frame encoding time.
1686*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_CFL_INTRA, 0);
1687*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_SMOOTH_INTRA, 0);
1688*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_ANGLE_DELTA, 0);
1689*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_ENABLE_FILTER_INTRA, 0);
1690*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_INTRA_DEFAULT_TX_ONLY, 1);
1691*77c1e3ccSAndroid Build Coastguard Worker 
1692*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_AUTO_TILES, 1);
1693*77c1e3ccSAndroid Build Coastguard Worker 
1694*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_TUNE_CONTENT, app_input.tune_content);
1695*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.tune_content == AOM_CONTENT_SCREEN) {
1696*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_control(&codec, AV1E_SET_ENABLE_PALETTE, 1);
1697*77c1e3ccSAndroid Build Coastguard Worker     // INTRABC is currently disabled for rt mode, as it's too slow.
1698*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_control(&codec, AV1E_SET_ENABLE_INTRABC, 0);
1699*77c1e3ccSAndroid Build Coastguard Worker   }
1700*77c1e3ccSAndroid Build Coastguard Worker 
1701*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.use_external_rc) {
1702*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_control(&codec, AV1E_SET_RTC_EXTERNAL_RC, 1);
1703*77c1e3ccSAndroid Build Coastguard Worker   }
1704*77c1e3ccSAndroid Build Coastguard Worker 
1705*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_MAX_CONSEC_FRAME_DROP_MS_CBR, INT_MAX);
1706*77c1e3ccSAndroid Build Coastguard Worker 
1707*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_SVC_FRAME_DROP_MODE,
1708*77c1e3ccSAndroid Build Coastguard Worker                     AOM_FULL_SUPERFRAME_DROP);
1709*77c1e3ccSAndroid Build Coastguard Worker 
1710*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_POSTENCODE_DROP_RTC, 1);
1711*77c1e3ccSAndroid Build Coastguard Worker 
1712*77c1e3ccSAndroid Build Coastguard Worker   svc_params.number_spatial_layers = ss_number_layers;
1713*77c1e3ccSAndroid Build Coastguard Worker   svc_params.number_temporal_layers = ts_number_layers;
1714*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < ss_number_layers * ts_number_layers; ++i) {
1715*77c1e3ccSAndroid Build Coastguard Worker     svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
1716*77c1e3ccSAndroid Build Coastguard Worker     svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
1717*77c1e3ccSAndroid Build Coastguard Worker   }
1718*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < ss_number_layers; ++i) {
1719*77c1e3ccSAndroid Build Coastguard Worker     svc_params.scaling_factor_num[i] = 1;
1720*77c1e3ccSAndroid Build Coastguard Worker     svc_params.scaling_factor_den[i] = 1;
1721*77c1e3ccSAndroid Build Coastguard Worker   }
1722*77c1e3ccSAndroid Build Coastguard Worker   if (ss_number_layers == 2) {
1723*77c1e3ccSAndroid Build Coastguard Worker     svc_params.scaling_factor_num[0] = 1;
1724*77c1e3ccSAndroid Build Coastguard Worker     svc_params.scaling_factor_den[0] = 2;
1725*77c1e3ccSAndroid Build Coastguard Worker   } else if (ss_number_layers == 3) {
1726*77c1e3ccSAndroid Build Coastguard Worker     svc_params.scaling_factor_num[0] = 1;
1727*77c1e3ccSAndroid Build Coastguard Worker     svc_params.scaling_factor_den[0] = 4;
1728*77c1e3ccSAndroid Build Coastguard Worker     svc_params.scaling_factor_num[1] = 1;
1729*77c1e3ccSAndroid Build Coastguard Worker     svc_params.scaling_factor_den[1] = 2;
1730*77c1e3ccSAndroid Build Coastguard Worker   }
1731*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_control(&codec, AV1E_SET_SVC_PARAMS, &svc_params);
1732*77c1e3ccSAndroid Build Coastguard Worker   // TODO(aomedia:3032): Configure KSVC in fixed mode.
1733*77c1e3ccSAndroid Build Coastguard Worker 
1734*77c1e3ccSAndroid Build Coastguard Worker   // This controls the maximum target size of the key frame.
1735*77c1e3ccSAndroid Build Coastguard Worker   // For generating smaller key frames, use a smaller max_intra_size_pct
1736*77c1e3ccSAndroid Build Coastguard Worker   // value, like 100 or 200.
1737*77c1e3ccSAndroid Build Coastguard Worker   {
1738*77c1e3ccSAndroid Build Coastguard Worker     const int max_intra_size_pct = 300;
1739*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_control(&codec, AOME_SET_MAX_INTRA_BITRATE_PCT,
1740*77c1e3ccSAndroid Build Coastguard Worker                       max_intra_size_pct);
1741*77c1e3ccSAndroid Build Coastguard Worker   }
1742*77c1e3ccSAndroid Build Coastguard Worker 
1743*77c1e3ccSAndroid Build Coastguard Worker   for (int lx = 0; lx < ts_number_layers * ss_number_layers; lx++) {
1744*77c1e3ccSAndroid Build Coastguard Worker     cx_time_layer[lx] = 0;
1745*77c1e3ccSAndroid Build Coastguard Worker     frame_cnt_layer[lx] = 0;
1746*77c1e3ccSAndroid Build Coastguard Worker   }
1747*77c1e3ccSAndroid Build Coastguard Worker 
1748*77c1e3ccSAndroid Build Coastguard Worker   std::unique_ptr<aom::AV1RateControlRTC> rc_api;
1749*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.use_external_rc) {
1750*77c1e3ccSAndroid Build Coastguard Worker     const aom::AV1RateControlRtcConfig rc_cfg =
1751*77c1e3ccSAndroid Build Coastguard Worker         create_rtc_rc_config(cfg, app_input);
1752*77c1e3ccSAndroid Build Coastguard Worker     rc_api = aom::AV1RateControlRTC::Create(rc_cfg);
1753*77c1e3ccSAndroid Build Coastguard Worker   }
1754*77c1e3ccSAndroid Build Coastguard Worker 
1755*77c1e3ccSAndroid Build Coastguard Worker   frame_avail = 1;
1756*77c1e3ccSAndroid Build Coastguard Worker   struct psnr_stats psnr_stream;
1757*77c1e3ccSAndroid Build Coastguard Worker   memset(&psnr_stream, 0, sizeof(psnr_stream));
1758*77c1e3ccSAndroid Build Coastguard Worker   while (frame_avail || got_data) {
1759*77c1e3ccSAndroid Build Coastguard Worker     struct aom_usec_timer timer;
1760*77c1e3ccSAndroid Build Coastguard Worker     frame_avail = read_frame(&(app_input.input_ctx), &raw);
1761*77c1e3ccSAndroid Build Coastguard Worker     // Loop over spatial layers.
1762*77c1e3ccSAndroid Build Coastguard Worker     for (int slx = 0; slx < ss_number_layers; slx++) {
1763*77c1e3ccSAndroid Build Coastguard Worker       aom_codec_iter_t iter = NULL;
1764*77c1e3ccSAndroid Build Coastguard Worker       const aom_codec_cx_pkt_t *pkt;
1765*77c1e3ccSAndroid Build Coastguard Worker       int layer = 0;
1766*77c1e3ccSAndroid Build Coastguard Worker       // Flag for superframe whose base is key.
1767*77c1e3ccSAndroid Build Coastguard Worker       int is_key_frame = (frame_cnt % cfg.kf_max_dist) == 0;
1768*77c1e3ccSAndroid Build Coastguard Worker       // For flexible mode:
1769*77c1e3ccSAndroid Build Coastguard Worker       if (app_input.layering_mode >= 0) {
1770*77c1e3ccSAndroid Build Coastguard Worker         // Set the reference/update flags, layer_id, and reference_map
1771*77c1e3ccSAndroid Build Coastguard Worker         // buffer index.
1772*77c1e3ccSAndroid Build Coastguard Worker         set_layer_pattern(app_input.layering_mode, frame_cnt, &layer_id,
1773*77c1e3ccSAndroid Build Coastguard Worker                           &ref_frame_config, &ref_frame_comp_pred,
1774*77c1e3ccSAndroid Build Coastguard Worker                           &use_svc_control, slx, is_key_frame,
1775*77c1e3ccSAndroid Build Coastguard Worker                           (app_input.layering_mode == 10), app_input.speed);
1776*77c1e3ccSAndroid Build Coastguard Worker         aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
1777*77c1e3ccSAndroid Build Coastguard Worker         if (use_svc_control) {
1778*77c1e3ccSAndroid Build Coastguard Worker           aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_CONFIG,
1779*77c1e3ccSAndroid Build Coastguard Worker                             &ref_frame_config);
1780*77c1e3ccSAndroid Build Coastguard Worker           aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_COMP_PRED,
1781*77c1e3ccSAndroid Build Coastguard Worker                             &ref_frame_comp_pred);
1782*77c1e3ccSAndroid Build Coastguard Worker         }
1783*77c1e3ccSAndroid Build Coastguard Worker         // Set the speed per layer.
1784*77c1e3ccSAndroid Build Coastguard Worker         if (test_speed_per_layer) {
1785*77c1e3ccSAndroid Build Coastguard Worker           int speed_per_layer = 10;
1786*77c1e3ccSAndroid Build Coastguard Worker           if (layer_id.spatial_layer_id == 0) {
1787*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 0) speed_per_layer = 6;
1788*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 1) speed_per_layer = 7;
1789*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 2) speed_per_layer = 8;
1790*77c1e3ccSAndroid Build Coastguard Worker           } else if (layer_id.spatial_layer_id == 1) {
1791*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 0) speed_per_layer = 7;
1792*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 1) speed_per_layer = 8;
1793*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 2) speed_per_layer = 9;
1794*77c1e3ccSAndroid Build Coastguard Worker           } else if (layer_id.spatial_layer_id == 2) {
1795*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 0) speed_per_layer = 8;
1796*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 1) speed_per_layer = 9;
1797*77c1e3ccSAndroid Build Coastguard Worker             if (layer_id.temporal_layer_id == 2) speed_per_layer = 10;
1798*77c1e3ccSAndroid Build Coastguard Worker           }
1799*77c1e3ccSAndroid Build Coastguard Worker           aom_codec_control(&codec, AOME_SET_CPUUSED, speed_per_layer);
1800*77c1e3ccSAndroid Build Coastguard Worker         }
1801*77c1e3ccSAndroid Build Coastguard Worker       } else {
1802*77c1e3ccSAndroid Build Coastguard Worker         // Only up to 3 temporal layers supported in fixed mode.
1803*77c1e3ccSAndroid Build Coastguard Worker         // Only need to set spatial and temporal layer_id: reference
1804*77c1e3ccSAndroid Build Coastguard Worker         // prediction, refresh, and buffer_idx are set internally.
1805*77c1e3ccSAndroid Build Coastguard Worker         layer_id.spatial_layer_id = slx;
1806*77c1e3ccSAndroid Build Coastguard Worker         layer_id.temporal_layer_id = 0;
1807*77c1e3ccSAndroid Build Coastguard Worker         if (ts_number_layers == 2) {
1808*77c1e3ccSAndroid Build Coastguard Worker           layer_id.temporal_layer_id = (frame_cnt % 2) != 0;
1809*77c1e3ccSAndroid Build Coastguard Worker         } else if (ts_number_layers == 3) {
1810*77c1e3ccSAndroid Build Coastguard Worker           if (frame_cnt % 2 != 0)
1811*77c1e3ccSAndroid Build Coastguard Worker             layer_id.temporal_layer_id = 2;
1812*77c1e3ccSAndroid Build Coastguard Worker           else if ((frame_cnt > 1) && ((frame_cnt - 2) % 4 == 0))
1813*77c1e3ccSAndroid Build Coastguard Worker             layer_id.temporal_layer_id = 1;
1814*77c1e3ccSAndroid Build Coastguard Worker         }
1815*77c1e3ccSAndroid Build Coastguard Worker         aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
1816*77c1e3ccSAndroid Build Coastguard Worker       }
1817*77c1e3ccSAndroid Build Coastguard Worker 
1818*77c1e3ccSAndroid Build Coastguard Worker       if (set_err_resil_frame && cfg.g_error_resilient == 0) {
1819*77c1e3ccSAndroid Build Coastguard Worker         // Set error_resilient per frame: off/0 for base layer and
1820*77c1e3ccSAndroid Build Coastguard Worker         // on/1 for enhancement layer frames.
1821*77c1e3ccSAndroid Build Coastguard Worker         // Note that this is can only be done on the fly/per-frame/layer
1822*77c1e3ccSAndroid Build Coastguard Worker         // if the config error_resilience is off/0. See the logic for updating
1823*77c1e3ccSAndroid Build Coastguard Worker         // in set_encoder_config():
1824*77c1e3ccSAndroid Build Coastguard Worker         // tool_cfg->error_resilient_mode =
1825*77c1e3ccSAndroid Build Coastguard Worker         //     cfg->g_error_resilient | extra_cfg->error_resilient_mode;
1826*77c1e3ccSAndroid Build Coastguard Worker         const int err_resil_mode =
1827*77c1e3ccSAndroid Build Coastguard Worker             layer_id.spatial_layer_id > 0 || layer_id.temporal_layer_id > 0;
1828*77c1e3ccSAndroid Build Coastguard Worker         aom_codec_control(&codec, AV1E_SET_ERROR_RESILIENT_MODE,
1829*77c1e3ccSAndroid Build Coastguard Worker                           err_resil_mode);
1830*77c1e3ccSAndroid Build Coastguard Worker       }
1831*77c1e3ccSAndroid Build Coastguard Worker 
1832*77c1e3ccSAndroid Build Coastguard Worker       layer = slx * ts_number_layers + layer_id.temporal_layer_id;
1833*77c1e3ccSAndroid Build Coastguard Worker       if (frame_avail && slx == 0) ++rc.layer_input_frames[layer];
1834*77c1e3ccSAndroid Build Coastguard Worker 
1835*77c1e3ccSAndroid Build Coastguard Worker       if (test_dynamic_scaling_single_layer) {
1836*77c1e3ccSAndroid Build Coastguard Worker         // Example to scale source down by 2x2, then 4x4, and then back up to
1837*77c1e3ccSAndroid Build Coastguard Worker         // 2x2, and then back to original.
1838*77c1e3ccSAndroid Build Coastguard Worker         int frame_2x2 = 200;
1839*77c1e3ccSAndroid Build Coastguard Worker         int frame_4x4 = 400;
1840*77c1e3ccSAndroid Build Coastguard Worker         int frame_2x2up = 600;
1841*77c1e3ccSAndroid Build Coastguard Worker         int frame_orig = 800;
1842*77c1e3ccSAndroid Build Coastguard Worker         if (frame_cnt >= frame_2x2 && frame_cnt < frame_4x4) {
1843*77c1e3ccSAndroid Build Coastguard Worker           // Scale source down by 2x2.
1844*77c1e3ccSAndroid Build Coastguard Worker           struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
1845*77c1e3ccSAndroid Build Coastguard Worker           aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
1846*77c1e3ccSAndroid Build Coastguard Worker         } else if (frame_cnt >= frame_4x4 && frame_cnt < frame_2x2up) {
1847*77c1e3ccSAndroid Build Coastguard Worker           // Scale source down by 4x4.
1848*77c1e3ccSAndroid Build Coastguard Worker           struct aom_scaling_mode mode = { AOME_ONEFOUR, AOME_ONEFOUR };
1849*77c1e3ccSAndroid Build Coastguard Worker           aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
1850*77c1e3ccSAndroid Build Coastguard Worker         } else if (frame_cnt >= frame_2x2up && frame_cnt < frame_orig) {
1851*77c1e3ccSAndroid Build Coastguard Worker           // Source back up to 2x2.
1852*77c1e3ccSAndroid Build Coastguard Worker           struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
1853*77c1e3ccSAndroid Build Coastguard Worker           aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
1854*77c1e3ccSAndroid Build Coastguard Worker         } else if (frame_cnt >= frame_orig) {
1855*77c1e3ccSAndroid Build Coastguard Worker           // Source back up to original resolution (no scaling).
1856*77c1e3ccSAndroid Build Coastguard Worker           struct aom_scaling_mode mode = { AOME_NORMAL, AOME_NORMAL };
1857*77c1e3ccSAndroid Build Coastguard Worker           aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
1858*77c1e3ccSAndroid Build Coastguard Worker         }
1859*77c1e3ccSAndroid Build Coastguard Worker         if (frame_cnt == frame_2x2 || frame_cnt == frame_4x4 ||
1860*77c1e3ccSAndroid Build Coastguard Worker             frame_cnt == frame_2x2up || frame_cnt == frame_orig) {
1861*77c1e3ccSAndroid Build Coastguard Worker           // For dynamic resize testing on single layer: refresh all references
1862*77c1e3ccSAndroid Build Coastguard Worker           // on the resized frame: this is to avoid decode error:
1863*77c1e3ccSAndroid Build Coastguard Worker           // if resize goes down by >= 4x4 then libaom decoder will throw an
1864*77c1e3ccSAndroid Build Coastguard Worker           // error that some reference (even though not used) is beyond the
1865*77c1e3ccSAndroid Build Coastguard Worker           // limit size (must be smaller than 4x4).
1866*77c1e3ccSAndroid Build Coastguard Worker           for (i = 0; i < REF_FRAMES; i++) ref_frame_config.refresh[i] = 1;
1867*77c1e3ccSAndroid Build Coastguard Worker           if (use_svc_control) {
1868*77c1e3ccSAndroid Build Coastguard Worker             aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_CONFIG,
1869*77c1e3ccSAndroid Build Coastguard Worker                               &ref_frame_config);
1870*77c1e3ccSAndroid Build Coastguard Worker             aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_COMP_PRED,
1871*77c1e3ccSAndroid Build Coastguard Worker                               &ref_frame_comp_pred);
1872*77c1e3ccSAndroid Build Coastguard Worker           }
1873*77c1e3ccSAndroid Build Coastguard Worker         }
1874*77c1e3ccSAndroid Build Coastguard Worker       }
1875*77c1e3ccSAndroid Build Coastguard Worker 
1876*77c1e3ccSAndroid Build Coastguard Worker       // Change target_bitrate every other frame.
1877*77c1e3ccSAndroid Build Coastguard Worker       if (test_changing_bitrate && frame_cnt % 2 == 0) {
1878*77c1e3ccSAndroid Build Coastguard Worker         if (frame_cnt < 500)
1879*77c1e3ccSAndroid Build Coastguard Worker           cfg.rc_target_bitrate += 10;
1880*77c1e3ccSAndroid Build Coastguard Worker         else
1881*77c1e3ccSAndroid Build Coastguard Worker           cfg.rc_target_bitrate -= 10;
1882*77c1e3ccSAndroid Build Coastguard Worker         // Do big increase and decrease.
1883*77c1e3ccSAndroid Build Coastguard Worker         if (frame_cnt == 100) cfg.rc_target_bitrate <<= 1;
1884*77c1e3ccSAndroid Build Coastguard Worker         if (frame_cnt == 600) cfg.rc_target_bitrate >>= 1;
1885*77c1e3ccSAndroid Build Coastguard Worker         if (cfg.rc_target_bitrate < 100) cfg.rc_target_bitrate = 100;
1886*77c1e3ccSAndroid Build Coastguard Worker         // Call change_config, or bypass with new control.
1887*77c1e3ccSAndroid Build Coastguard Worker         // res = aom_codec_enc_config_set(&codec, &cfg);
1888*77c1e3ccSAndroid Build Coastguard Worker         if (aom_codec_control(&codec, AV1E_SET_BITRATE_ONE_PASS_CBR,
1889*77c1e3ccSAndroid Build Coastguard Worker                               cfg.rc_target_bitrate))
1890*77c1e3ccSAndroid Build Coastguard Worker           die_codec(&codec, "Failed to SET_BITRATE_ONE_PASS_CBR");
1891*77c1e3ccSAndroid Build Coastguard Worker       }
1892*77c1e3ccSAndroid Build Coastguard Worker 
1893*77c1e3ccSAndroid Build Coastguard Worker       if (rc_api) {
1894*77c1e3ccSAndroid Build Coastguard Worker         aom::AV1FrameParamsRTC frame_params;
1895*77c1e3ccSAndroid Build Coastguard Worker         // TODO(jianj): Add support for SVC.
1896*77c1e3ccSAndroid Build Coastguard Worker         frame_params.spatial_layer_id = 0;
1897*77c1e3ccSAndroid Build Coastguard Worker         frame_params.temporal_layer_id = 0;
1898*77c1e3ccSAndroid Build Coastguard Worker         frame_params.frame_type =
1899*77c1e3ccSAndroid Build Coastguard Worker             is_key_frame ? aom::kKeyFrame : aom::kInterFrame;
1900*77c1e3ccSAndroid Build Coastguard Worker         rc_api->ComputeQP(frame_params);
1901*77c1e3ccSAndroid Build Coastguard Worker         const int current_qp = rc_api->GetQP();
1902*77c1e3ccSAndroid Build Coastguard Worker         if (aom_codec_control(&codec, AV1E_SET_QUANTIZER_ONE_PASS,
1903*77c1e3ccSAndroid Build Coastguard Worker                               qindex_to_quantizer(current_qp))) {
1904*77c1e3ccSAndroid Build Coastguard Worker           die_codec(&codec, "Failed to SET_QUANTIZER_ONE_PASS");
1905*77c1e3ccSAndroid Build Coastguard Worker         }
1906*77c1e3ccSAndroid Build Coastguard Worker       }
1907*77c1e3ccSAndroid Build Coastguard Worker 
1908*77c1e3ccSAndroid Build Coastguard Worker       if (test_active_maps) set_active_map(&cfg, &codec, frame_cnt);
1909*77c1e3ccSAndroid Build Coastguard Worker 
1910*77c1e3ccSAndroid Build Coastguard Worker       // Do the layer encode.
1911*77c1e3ccSAndroid Build Coastguard Worker       aom_usec_timer_start(&timer);
1912*77c1e3ccSAndroid Build Coastguard Worker       if (aom_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags))
1913*77c1e3ccSAndroid Build Coastguard Worker         die_codec(&codec, "Failed to encode frame");
1914*77c1e3ccSAndroid Build Coastguard Worker       aom_usec_timer_mark(&timer);
1915*77c1e3ccSAndroid Build Coastguard Worker       cx_time += aom_usec_timer_elapsed(&timer);
1916*77c1e3ccSAndroid Build Coastguard Worker       cx_time_layer[layer] += aom_usec_timer_elapsed(&timer);
1917*77c1e3ccSAndroid Build Coastguard Worker       frame_cnt_layer[layer] += 1;
1918*77c1e3ccSAndroid Build Coastguard Worker 
1919*77c1e3ccSAndroid Build Coastguard Worker       // Get the high motion content flag.
1920*77c1e3ccSAndroid Build Coastguard Worker       int content_flag = 0;
1921*77c1e3ccSAndroid Build Coastguard Worker       if (aom_codec_control(&codec, AV1E_GET_HIGH_MOTION_CONTENT_SCREEN_RTC,
1922*77c1e3ccSAndroid Build Coastguard Worker                             &content_flag)) {
1923*77c1e3ccSAndroid Build Coastguard Worker         die_codec(&codec, "Failed to GET_HIGH_MOTION_CONTENT_SCREEN_RTC");
1924*77c1e3ccSAndroid Build Coastguard Worker       }
1925*77c1e3ccSAndroid Build Coastguard Worker 
1926*77c1e3ccSAndroid Build Coastguard Worker       got_data = 0;
1927*77c1e3ccSAndroid Build Coastguard Worker       // For simulcast (mode 11): write out each spatial layer to the file.
1928*77c1e3ccSAndroid Build Coastguard Worker       int ss_layers_write = (app_input.layering_mode == 11)
1929*77c1e3ccSAndroid Build Coastguard Worker                                 ? layer_id.spatial_layer_id + 1
1930*77c1e3ccSAndroid Build Coastguard Worker                                 : ss_number_layers;
1931*77c1e3ccSAndroid Build Coastguard Worker       while ((pkt = aom_codec_get_cx_data(&codec, &iter))) {
1932*77c1e3ccSAndroid Build Coastguard Worker         switch (pkt->kind) {
1933*77c1e3ccSAndroid Build Coastguard Worker           case AOM_CODEC_CX_FRAME_PKT:
1934*77c1e3ccSAndroid Build Coastguard Worker             for (int sl = layer_id.spatial_layer_id; sl < ss_layers_write;
1935*77c1e3ccSAndroid Build Coastguard Worker                  ++sl) {
1936*77c1e3ccSAndroid Build Coastguard Worker               for (int tl = layer_id.temporal_layer_id; tl < ts_number_layers;
1937*77c1e3ccSAndroid Build Coastguard Worker                    ++tl) {
1938*77c1e3ccSAndroid Build Coastguard Worker                 int j = sl * ts_number_layers + tl;
1939*77c1e3ccSAndroid Build Coastguard Worker                 if (app_input.output_obu) {
1940*77c1e3ccSAndroid Build Coastguard Worker                   fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1941*77c1e3ccSAndroid Build Coastguard Worker                          obu_files[j]);
1942*77c1e3ccSAndroid Build Coastguard Worker                 } else {
1943*77c1e3ccSAndroid Build Coastguard Worker                   aom_video_writer_write_frame(
1944*77c1e3ccSAndroid Build Coastguard Worker                       outfile[j],
1945*77c1e3ccSAndroid Build Coastguard Worker                       reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
1946*77c1e3ccSAndroid Build Coastguard Worker                       pkt->data.frame.sz, pts);
1947*77c1e3ccSAndroid Build Coastguard Worker                 }
1948*77c1e3ccSAndroid Build Coastguard Worker                 if (sl == layer_id.spatial_layer_id)
1949*77c1e3ccSAndroid Build Coastguard Worker                   rc.layer_encoding_bitrate[j] += 8.0 * pkt->data.frame.sz;
1950*77c1e3ccSAndroid Build Coastguard Worker               }
1951*77c1e3ccSAndroid Build Coastguard Worker             }
1952*77c1e3ccSAndroid Build Coastguard Worker             got_data = 1;
1953*77c1e3ccSAndroid Build Coastguard Worker             // Write everything into the top layer.
1954*77c1e3ccSAndroid Build Coastguard Worker             if (app_input.output_obu) {
1955*77c1e3ccSAndroid Build Coastguard Worker               fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1956*77c1e3ccSAndroid Build Coastguard Worker                      total_layer_obu_file);
1957*77c1e3ccSAndroid Build Coastguard Worker             } else {
1958*77c1e3ccSAndroid Build Coastguard Worker               aom_video_writer_write_frame(
1959*77c1e3ccSAndroid Build Coastguard Worker                   total_layer_file,
1960*77c1e3ccSAndroid Build Coastguard Worker                   reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
1961*77c1e3ccSAndroid Build Coastguard Worker                   pkt->data.frame.sz, pts);
1962*77c1e3ccSAndroid Build Coastguard Worker             }
1963*77c1e3ccSAndroid Build Coastguard Worker             // Keep count of rate control stats per layer (for non-key).
1964*77c1e3ccSAndroid Build Coastguard Worker             if (!(pkt->data.frame.flags & AOM_FRAME_IS_KEY)) {
1965*77c1e3ccSAndroid Build Coastguard Worker               int j = layer_id.spatial_layer_id * ts_number_layers +
1966*77c1e3ccSAndroid Build Coastguard Worker                       layer_id.temporal_layer_id;
1967*77c1e3ccSAndroid Build Coastguard Worker               assert(j >= 0);
1968*77c1e3ccSAndroid Build Coastguard Worker               rc.layer_avg_frame_size[j] += 8.0 * pkt->data.frame.sz;
1969*77c1e3ccSAndroid Build Coastguard Worker               rc.layer_avg_rate_mismatch[j] +=
1970*77c1e3ccSAndroid Build Coastguard Worker                   fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[j]) /
1971*77c1e3ccSAndroid Build Coastguard Worker                   rc.layer_pfb[j];
1972*77c1e3ccSAndroid Build Coastguard Worker               if (slx == 0) ++rc.layer_enc_frames[layer_id.temporal_layer_id];
1973*77c1e3ccSAndroid Build Coastguard Worker             }
1974*77c1e3ccSAndroid Build Coastguard Worker 
1975*77c1e3ccSAndroid Build Coastguard Worker             if (rc_api) {
1976*77c1e3ccSAndroid Build Coastguard Worker               rc_api->PostEncodeUpdate(pkt->data.frame.sz);
1977*77c1e3ccSAndroid Build Coastguard Worker             }
1978*77c1e3ccSAndroid Build Coastguard Worker             // Update for short-time encoding bitrate states, for moving window
1979*77c1e3ccSAndroid Build Coastguard Worker             // of size rc->window, shifted by rc->window / 2.
1980*77c1e3ccSAndroid Build Coastguard Worker             // Ignore first window segment, due to key frame.
1981*77c1e3ccSAndroid Build Coastguard Worker             // For spatial layers: only do this for top/highest SL.
1982*77c1e3ccSAndroid Build Coastguard Worker             if (frame_cnt > rc.window_size && slx == ss_number_layers - 1) {
1983*77c1e3ccSAndroid Build Coastguard Worker               sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
1984*77c1e3ccSAndroid Build Coastguard Worker               rc.window_size = (rc.window_size <= 0) ? 1 : rc.window_size;
1985*77c1e3ccSAndroid Build Coastguard Worker               if (frame_cnt % rc.window_size == 0) {
1986*77c1e3ccSAndroid Build Coastguard Worker                 rc.window_count += 1;
1987*77c1e3ccSAndroid Build Coastguard Worker                 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
1988*77c1e3ccSAndroid Build Coastguard Worker                 rc.variance_st_encoding_bitrate +=
1989*77c1e3ccSAndroid Build Coastguard Worker                     (sum_bitrate / rc.window_size) *
1990*77c1e3ccSAndroid Build Coastguard Worker                     (sum_bitrate / rc.window_size);
1991*77c1e3ccSAndroid Build Coastguard Worker                 sum_bitrate = 0.0;
1992*77c1e3ccSAndroid Build Coastguard Worker               }
1993*77c1e3ccSAndroid Build Coastguard Worker             }
1994*77c1e3ccSAndroid Build Coastguard Worker             // Second shifted window.
1995*77c1e3ccSAndroid Build Coastguard Worker             if (frame_cnt > rc.window_size + rc.window_size / 2 &&
1996*77c1e3ccSAndroid Build Coastguard Worker                 slx == ss_number_layers - 1) {
1997*77c1e3ccSAndroid Build Coastguard Worker               sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
1998*77c1e3ccSAndroid Build Coastguard Worker               if (frame_cnt > 2 * rc.window_size &&
1999*77c1e3ccSAndroid Build Coastguard Worker                   frame_cnt % rc.window_size == 0) {
2000*77c1e3ccSAndroid Build Coastguard Worker                 rc.window_count += 1;
2001*77c1e3ccSAndroid Build Coastguard Worker                 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
2002*77c1e3ccSAndroid Build Coastguard Worker                 rc.variance_st_encoding_bitrate +=
2003*77c1e3ccSAndroid Build Coastguard Worker                     (sum_bitrate2 / rc.window_size) *
2004*77c1e3ccSAndroid Build Coastguard Worker                     (sum_bitrate2 / rc.window_size);
2005*77c1e3ccSAndroid Build Coastguard Worker                 sum_bitrate2 = 0.0;
2006*77c1e3ccSAndroid Build Coastguard Worker               }
2007*77c1e3ccSAndroid Build Coastguard Worker             }
2008*77c1e3ccSAndroid Build Coastguard Worker 
2009*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
2010*77c1e3ccSAndroid Build Coastguard Worker             if (app_input.decode) {
2011*77c1e3ccSAndroid Build Coastguard Worker               if (aom_codec_decode(
2012*77c1e3ccSAndroid Build Coastguard Worker                       &decoder,
2013*77c1e3ccSAndroid Build Coastguard Worker                       reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
2014*77c1e3ccSAndroid Build Coastguard Worker                       pkt->data.frame.sz, NULL))
2015*77c1e3ccSAndroid Build Coastguard Worker                 die_codec(&decoder, "Failed to decode frame");
2016*77c1e3ccSAndroid Build Coastguard Worker             }
2017*77c1e3ccSAndroid Build Coastguard Worker #endif
2018*77c1e3ccSAndroid Build Coastguard Worker 
2019*77c1e3ccSAndroid Build Coastguard Worker             break;
2020*77c1e3ccSAndroid Build Coastguard Worker           case AOM_CODEC_PSNR_PKT:
2021*77c1e3ccSAndroid Build Coastguard Worker             if (app_input.show_psnr) {
2022*77c1e3ccSAndroid Build Coastguard Worker               psnr_stream.psnr_sse_total[0] += pkt->data.psnr.sse[0];
2023*77c1e3ccSAndroid Build Coastguard Worker               psnr_stream.psnr_samples_total[0] += pkt->data.psnr.samples[0];
2024*77c1e3ccSAndroid Build Coastguard Worker               for (int plane = 0; plane < 4; plane++) {
2025*77c1e3ccSAndroid Build Coastguard Worker                 psnr_stream.psnr_totals[0][plane] += pkt->data.psnr.psnr[plane];
2026*77c1e3ccSAndroid Build Coastguard Worker               }
2027*77c1e3ccSAndroid Build Coastguard Worker               psnr_stream.psnr_count[0]++;
2028*77c1e3ccSAndroid Build Coastguard Worker             }
2029*77c1e3ccSAndroid Build Coastguard Worker             break;
2030*77c1e3ccSAndroid Build Coastguard Worker           default: break;
2031*77c1e3ccSAndroid Build Coastguard Worker         }
2032*77c1e3ccSAndroid Build Coastguard Worker       }
2033*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
2034*77c1e3ccSAndroid Build Coastguard Worker       if (got_data && app_input.decode) {
2035*77c1e3ccSAndroid Build Coastguard Worker         // Don't look for mismatch on top spatial and top temporal layers as
2036*77c1e3ccSAndroid Build Coastguard Worker         // they are non reference frames.
2037*77c1e3ccSAndroid Build Coastguard Worker         if ((ss_number_layers > 1 || ts_number_layers > 1) &&
2038*77c1e3ccSAndroid Build Coastguard Worker             !(layer_id.temporal_layer_id > 0 &&
2039*77c1e3ccSAndroid Build Coastguard Worker               layer_id.temporal_layer_id == ts_number_layers - 1)) {
2040*77c1e3ccSAndroid Build Coastguard Worker           if (test_decode(&codec, &decoder, frame_cnt)) {
2041*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
2042*77c1e3ccSAndroid Build Coastguard Worker             fprintf(stats_file, "First mismatch occurred in frame %d\n",
2043*77c1e3ccSAndroid Build Coastguard Worker                     frame_cnt);
2044*77c1e3ccSAndroid Build Coastguard Worker             fclose(stats_file);
2045*77c1e3ccSAndroid Build Coastguard Worker #endif
2046*77c1e3ccSAndroid Build Coastguard Worker             fatal("Mismatch seen");
2047*77c1e3ccSAndroid Build Coastguard Worker           }
2048*77c1e3ccSAndroid Build Coastguard Worker         }
2049*77c1e3ccSAndroid Build Coastguard Worker       }
2050*77c1e3ccSAndroid Build Coastguard Worker #endif
2051*77c1e3ccSAndroid Build Coastguard Worker     }  // loop over spatial layers
2052*77c1e3ccSAndroid Build Coastguard Worker     ++frame_cnt;
2053*77c1e3ccSAndroid Build Coastguard Worker     pts += frame_duration;
2054*77c1e3ccSAndroid Build Coastguard Worker   }
2055*77c1e3ccSAndroid Build Coastguard Worker 
2056*77c1e3ccSAndroid Build Coastguard Worker   close_input_file(&(app_input.input_ctx));
2057*77c1e3ccSAndroid Build Coastguard Worker   printout_rate_control_summary(&rc, frame_cnt, ss_number_layers,
2058*77c1e3ccSAndroid Build Coastguard Worker                                 ts_number_layers);
2059*77c1e3ccSAndroid Build Coastguard Worker 
2060*77c1e3ccSAndroid Build Coastguard Worker   printf("\n");
2061*77c1e3ccSAndroid Build Coastguard Worker   for (int slx = 0; slx < ss_number_layers; slx++)
2062*77c1e3ccSAndroid Build Coastguard Worker     for (int tlx = 0; tlx < ts_number_layers; tlx++) {
2063*77c1e3ccSAndroid Build Coastguard Worker       int lx = slx * ts_number_layers + tlx;
2064*77c1e3ccSAndroid Build Coastguard Worker       printf("Per layer encoding time/FPS stats for encoder: %d %d %d %f %f \n",
2065*77c1e3ccSAndroid Build Coastguard Worker              slx, tlx, frame_cnt_layer[lx],
2066*77c1e3ccSAndroid Build Coastguard Worker              (float)cx_time_layer[lx] / (double)(frame_cnt_layer[lx] * 1000),
2067*77c1e3ccSAndroid Build Coastguard Worker              1000000 * (double)frame_cnt_layer[lx] / (double)cx_time_layer[lx]);
2068*77c1e3ccSAndroid Build Coastguard Worker     }
2069*77c1e3ccSAndroid Build Coastguard Worker 
2070*77c1e3ccSAndroid Build Coastguard Worker   printf("\n");
2071*77c1e3ccSAndroid Build Coastguard Worker   printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f\n",
2072*77c1e3ccSAndroid Build Coastguard Worker          frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
2073*77c1e3ccSAndroid Build Coastguard Worker          1000000 * (double)frame_cnt / (double)cx_time);
2074*77c1e3ccSAndroid Build Coastguard Worker 
2075*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.show_psnr) {
2076*77c1e3ccSAndroid Build Coastguard Worker     show_psnr(&psnr_stream, 255.0);
2077*77c1e3ccSAndroid Build Coastguard Worker   }
2078*77c1e3ccSAndroid Build Coastguard Worker 
2079*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy encoder");
2080*77c1e3ccSAndroid Build Coastguard Worker 
2081*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_DECODER
2082*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.decode) {
2083*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_destroy(&decoder))
2084*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&decoder, "Failed to destroy decoder");
2085*77c1e3ccSAndroid Build Coastguard Worker   }
2086*77c1e3ccSAndroid Build Coastguard Worker #endif
2087*77c1e3ccSAndroid Build Coastguard Worker 
2088*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
2089*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stats_file, "No mismatch detected in recon buffers\n");
2090*77c1e3ccSAndroid Build Coastguard Worker   fclose(stats_file);
2091*77c1e3ccSAndroid Build Coastguard Worker #endif
2092*77c1e3ccSAndroid Build Coastguard Worker 
2093*77c1e3ccSAndroid Build Coastguard Worker   // Try to rewrite the output file headers with the actual frame count.
2094*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < ss_number_layers * ts_number_layers; ++i)
2095*77c1e3ccSAndroid Build Coastguard Worker     aom_video_writer_close(outfile[i]);
2096*77c1e3ccSAndroid Build Coastguard Worker   aom_video_writer_close(total_layer_file);
2097*77c1e3ccSAndroid Build Coastguard Worker 
2098*77c1e3ccSAndroid Build Coastguard Worker   if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
2099*77c1e3ccSAndroid Build Coastguard Worker     aom_img_free(&raw);
2100*77c1e3ccSAndroid Build Coastguard Worker   }
2101*77c1e3ccSAndroid Build Coastguard Worker   return EXIT_SUCCESS;
2102*77c1e3ccSAndroid Build Coastguard Worker }
2103