1 /*
2  *  Copyright (c) 2019, Alliance for Open Media. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 //  This is an example demonstrating how to implement a multi-layer AOM
12 //  encoding scheme for RTC video applications.
13 
14 #include <assert.h>
15 #include <math.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #include "aom/aom_encoder.h"
21 #include "aom/aomcx.h"
22 #include "av1/common/enums.h"
23 #include "av1/encoder/encoder.h"
24 #include "common/args.h"
25 #include "common/tools_common.h"
26 #include "common/video_writer.h"
27 #include "examples/encoder_util.h"
28 #include "aom_ports/aom_timer.h"
29 
30 #define OPTION_BUFFER_SIZE 1024
31 
32 typedef struct {
33   const char *output_filename;
34   char options[OPTION_BUFFER_SIZE];
35   struct AvxInputContext input_ctx;
36   int speed;
37   int aq_mode;
38   int layering_mode;
39   int output_obu;
40 } AppInput;
41 
42 typedef enum {
43   QUANTIZER = 0,
44   BITRATE,
45   SCALE_FACTOR,
46   AUTO_ALT_REF,
47   ALL_OPTION_TYPES
48 } LAYER_OPTION_TYPE;
49 
50 static const arg_def_t outputfile =
51     ARG_DEF("o", "output", 1, "Output filename");
52 static const arg_def_t frames_arg =
53     ARG_DEF("f", "frames", 1, "Number of frames to encode");
54 static const arg_def_t threads_arg =
55     ARG_DEF("th", "threads", 1, "Number of threads to use");
56 static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "Source width");
57 static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "Source height");
58 static const arg_def_t timebase_arg =
59     ARG_DEF("t", "timebase", 1, "Timebase (num/den)");
60 static const arg_def_t bitrate_arg = ARG_DEF(
61     "b", "target-bitrate", 1, "Encoding bitrate, in kilobits per second");
62 static const arg_def_t spatial_layers_arg =
63     ARG_DEF("sl", "spatial-layers", 1, "Number of spatial SVC layers");
64 static const arg_def_t temporal_layers_arg =
65     ARG_DEF("tl", "temporal-layers", 1, "Number of temporal SVC layers");
66 static const arg_def_t layering_mode_arg =
67     ARG_DEF("lm", "layering-mode", 1, "Temporal layering scheme.");
68 static const arg_def_t kf_dist_arg =
69     ARG_DEF("k", "kf-dist", 1, "Number of frames between keyframes");
70 static const arg_def_t scale_factors_arg =
71     ARG_DEF("r", "scale-factors", 1, "Scale factors (lowest to highest layer)");
72 static const arg_def_t min_q_arg =
73     ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
74 static const arg_def_t max_q_arg =
75     ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
76 static const arg_def_t speed_arg =
77     ARG_DEF("sp", "speed", 1, "Speed configuration");
78 static const arg_def_t aqmode_arg =
79     ARG_DEF("aq", "aqmode", 1, "AQ mode off/on");
80 static const arg_def_t bitrates_arg =
81     ARG_DEF("bl", "bitrates", 1,
82             "Bitrates[spatial_layer * num_temporal_layer + temporal_layer]");
83 static const arg_def_t dropframe_thresh_arg =
84     ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
85 static const arg_def_t error_resilient_arg =
86     ARG_DEF(NULL, "error-resilient", 1, "Error resilient flag");
87 static const arg_def_t output_obu_arg =
88     ARG_DEF(NULL, "output-obu", 1,
89             "Write OBUs when set to 1. Otherwise write IVF files.");
90 
91 #if CONFIG_AV1_HIGHBITDEPTH
92 static const struct arg_enum_list bitdepth_enum[] = {
93   { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
94 };
95 
96 static const arg_def_t bitdepth_arg = ARG_DEF_ENUM(
97     "d", "bit-depth", 1, "Bit depth for codec 8, 10 or 12. ", bitdepth_enum);
98 #endif  // CONFIG_AV1_HIGHBITDEPTH
99 
100 static const arg_def_t *svc_args[] = {
101   &frames_arg,          &outputfile,     &width_arg,
102   &height_arg,          &timebase_arg,   &bitrate_arg,
103   &spatial_layers_arg,  &kf_dist_arg,    &scale_factors_arg,
104   &min_q_arg,           &max_q_arg,      &temporal_layers_arg,
105   &layering_mode_arg,   &threads_arg,    &aqmode_arg,
106 #if CONFIG_AV1_HIGHBITDEPTH
107   &bitdepth_arg,
108 #endif
109   &speed_arg,           &bitrates_arg,   &dropframe_thresh_arg,
110   &error_resilient_arg, &output_obu_arg, NULL
111 };
112 
113 #define zero(Dest) memset(&(Dest), 0, sizeof(Dest));
114 
115 static const char *exec_name;
116 
usage_exit(void)117 void usage_exit(void) {
118   fprintf(stderr, "Usage: %s <options> input_filename -o output_filename\n",
119           exec_name);
120   fprintf(stderr, "Options:\n");
121   arg_show_usage(stderr, svc_args);
122   exit(EXIT_FAILURE);
123 }
124 
file_is_y4m(const char detect[4])125 static int file_is_y4m(const char detect[4]) {
126   return memcmp(detect, "YUV4", 4) == 0;
127 }
128 
fourcc_is_ivf(const char detect[4])129 static int fourcc_is_ivf(const char detect[4]) {
130   if (memcmp(detect, "DKIF", 4) == 0) {
131     return 1;
132   }
133   return 0;
134 }
135 
136 static const int option_max_values[ALL_OPTION_TYPES] = { 63, INT_MAX, INT_MAX,
137                                                          1 };
138 
139 static const int option_min_values[ALL_OPTION_TYPES] = { 0, 0, 1, 0 };
140 
open_input_file(struct AvxInputContext * input,aom_chroma_sample_position_t csp)141 static void open_input_file(struct AvxInputContext *input,
142                             aom_chroma_sample_position_t csp) {
143   /* Parse certain options from the input file, if possible */
144   input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
145                                              : set_binary_mode(stdin);
146 
147   if (!input->file) fatal("Failed to open input file");
148 
149   if (!fseeko(input->file, 0, SEEK_END)) {
150     /* Input file is seekable. Figure out how long it is, so we can get
151      * progress info.
152      */
153     input->length = ftello(input->file);
154     rewind(input->file);
155   }
156 
157   /* Default to 1:1 pixel aspect ratio. */
158   input->pixel_aspect_ratio.numerator = 1;
159   input->pixel_aspect_ratio.denominator = 1;
160 
161   /* For RAW input sources, these bytes will applied on the first frame
162    *  in read_frame().
163    */
164   input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
165   input->detect.position = 0;
166 
167   if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
168     if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
169                        input->only_i420) >= 0) {
170       input->file_type = FILE_TYPE_Y4M;
171       input->width = input->y4m.pic_w;
172       input->height = input->y4m.pic_h;
173       input->pixel_aspect_ratio.numerator = input->y4m.par_n;
174       input->pixel_aspect_ratio.denominator = input->y4m.par_d;
175       input->framerate.numerator = input->y4m.fps_n;
176       input->framerate.denominator = input->y4m.fps_d;
177       input->fmt = input->y4m.aom_fmt;
178       input->bit_depth = input->y4m.bit_depth;
179     } else {
180       fatal("Unsupported Y4M stream.");
181     }
182   } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
183     fatal("IVF is not supported as input.");
184   } else {
185     input->file_type = FILE_TYPE_RAW;
186   }
187 }
188 
extract_option(LAYER_OPTION_TYPE type,char * input,int * value0,int * value1)189 static aom_codec_err_t extract_option(LAYER_OPTION_TYPE type, char *input,
190                                       int *value0, int *value1) {
191   if (type == SCALE_FACTOR) {
192     *value0 = (int)strtol(input, &input, 10);
193     if (*input++ != '/') return AOM_CODEC_INVALID_PARAM;
194     *value1 = (int)strtol(input, &input, 10);
195 
196     if (*value0 < option_min_values[SCALE_FACTOR] ||
197         *value1 < option_min_values[SCALE_FACTOR] ||
198         *value0 > option_max_values[SCALE_FACTOR] ||
199         *value1 > option_max_values[SCALE_FACTOR] ||
200         *value0 > *value1)  // num shouldn't be greater than den
201       return AOM_CODEC_INVALID_PARAM;
202   } else {
203     *value0 = atoi(input);
204     if (*value0 < option_min_values[type] || *value0 > option_max_values[type])
205       return AOM_CODEC_INVALID_PARAM;
206   }
207   return AOM_CODEC_OK;
208 }
209 
parse_layer_options_from_string(aom_svc_params_t * svc_params,LAYER_OPTION_TYPE type,const char * input,int * option0,int * option1)210 static aom_codec_err_t parse_layer_options_from_string(
211     aom_svc_params_t *svc_params, LAYER_OPTION_TYPE type, const char *input,
212     int *option0, int *option1) {
213   aom_codec_err_t res = AOM_CODEC_OK;
214   char *input_string;
215   char *token;
216   const char *delim = ",";
217   int num_layers = svc_params->number_spatial_layers;
218   int i = 0;
219 
220   if (type == BITRATE)
221     num_layers =
222         svc_params->number_spatial_layers * svc_params->number_temporal_layers;
223 
224   if (input == NULL || option0 == NULL ||
225       (option1 == NULL && type == SCALE_FACTOR))
226     return AOM_CODEC_INVALID_PARAM;
227 
228   input_string = malloc(strlen(input));
229   memcpy(input_string, input, strlen(input));
230   if (input_string == NULL) return AOM_CODEC_MEM_ERROR;
231   token = strtok(input_string, delim);  // NOLINT
232   for (i = 0; i < num_layers; ++i) {
233     if (token != NULL) {
234       res = extract_option(type, token, option0 + i, option1 + i);
235       if (res != AOM_CODEC_OK) break;
236       token = strtok(NULL, delim);  // NOLINT
237     } else {
238       break;
239     }
240   }
241   if (res == AOM_CODEC_OK && i != num_layers) {
242     res = AOM_CODEC_INVALID_PARAM;
243   }
244   free(input_string);
245   return res;
246 }
247 
parse_command_line(int argc,const char ** argv_,AppInput * app_input,aom_svc_params_t * svc_params,aom_codec_enc_cfg_t * enc_cfg)248 static void parse_command_line(int argc, const char **argv_,
249                                AppInput *app_input,
250                                aom_svc_params_t *svc_params,
251                                aom_codec_enc_cfg_t *enc_cfg) {
252   struct arg arg;
253   char **argv = NULL;
254   char **argi = NULL;
255   char **argj = NULL;
256   char string_options[1024] = { 0 };
257 
258   // Default settings
259   svc_params->number_spatial_layers = 1;
260   svc_params->number_temporal_layers = 1;
261   app_input->layering_mode = 0;
262   app_input->output_obu = 0;
263   enc_cfg->g_threads = 1;
264   enc_cfg->rc_end_usage = AOM_CBR;
265 
266   // process command line options
267   argv = argv_dup(argc - 1, argv_ + 1);
268   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
269     arg.argv_step = 1;
270 
271     if (arg_match(&arg, &outputfile, argi)) {
272       app_input->output_filename = arg.val;
273     } else if (arg_match(&arg, &width_arg, argi)) {
274       enc_cfg->g_w = arg_parse_uint(&arg);
275     } else if (arg_match(&arg, &height_arg, argi)) {
276       enc_cfg->g_h = arg_parse_uint(&arg);
277     } else if (arg_match(&arg, &timebase_arg, argi)) {
278       enc_cfg->g_timebase = arg_parse_rational(&arg);
279     } else if (arg_match(&arg, &bitrate_arg, argi)) {
280       enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
281     } else if (arg_match(&arg, &spatial_layers_arg, argi)) {
282       svc_params->number_spatial_layers = arg_parse_uint(&arg);
283     } else if (arg_match(&arg, &temporal_layers_arg, argi)) {
284       svc_params->number_temporal_layers = arg_parse_uint(&arg);
285     } else if (arg_match(&arg, &speed_arg, argi)) {
286       app_input->speed = arg_parse_uint(&arg);
287       if (app_input->speed > 9) {
288         aom_tools_warn("Mapping speed %d to speed 9.\n", app_input->speed);
289       }
290     } else if (arg_match(&arg, &aqmode_arg, argi)) {
291       app_input->aq_mode = arg_parse_uint(&arg);
292     } else if (arg_match(&arg, &threads_arg, argi)) {
293       enc_cfg->g_threads = arg_parse_uint(&arg);
294     } else if (arg_match(&arg, &layering_mode_arg, argi)) {
295       app_input->layering_mode = arg_parse_int(&arg);
296     } else if (arg_match(&arg, &kf_dist_arg, argi)) {
297       enc_cfg->kf_min_dist = arg_parse_uint(&arg);
298       enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
299     } else if (arg_match(&arg, &scale_factors_arg, argi)) {
300       parse_layer_options_from_string(svc_params, SCALE_FACTOR, arg.val,
301                                       svc_params->scaling_factor_num,
302                                       svc_params->scaling_factor_den);
303     } else if (arg_match(&arg, &min_q_arg, argi)) {
304       enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
305     } else if (arg_match(&arg, &max_q_arg, argi)) {
306       enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
307 #if CONFIG_AV1_HIGHBITDEPTH
308     } else if (arg_match(&arg, &bitdepth_arg, argi)) {
309       enc_cfg->g_bit_depth = arg_parse_enum_or_int(&arg);
310       switch (enc_cfg->g_bit_depth) {
311         case AOM_BITS_8:
312           enc_cfg->g_input_bit_depth = 8;
313           enc_cfg->g_profile = 0;
314           break;
315         case AOM_BITS_10:
316           enc_cfg->g_input_bit_depth = 10;
317           enc_cfg->g_profile = 2;
318           break;
319         case AOM_BITS_12:
320           enc_cfg->g_input_bit_depth = 12;
321           enc_cfg->g_profile = 2;
322           break;
323         default:
324           die("Error: Invalid bit depth selected (%d)\n", enc_cfg->g_bit_depth);
325           break;
326       }
327 #endif  // CONFIG_VP9_HIGHBITDEPTH
328     } else if (arg_match(&arg, &dropframe_thresh_arg, argi)) {
329       enc_cfg->rc_dropframe_thresh = arg_parse_uint(&arg);
330     } else if (arg_match(&arg, &error_resilient_arg, argi)) {
331       enc_cfg->g_error_resilient = arg_parse_uint(&arg);
332       if (enc_cfg->g_error_resilient != 0 && enc_cfg->g_error_resilient != 1)
333         die("Invalid value for error resilient (0, 1): %d.",
334             enc_cfg->g_error_resilient);
335     } else if (arg_match(&arg, &output_obu_arg, argi)) {
336       app_input->output_obu = arg_parse_uint(&arg);
337       if (app_input->output_obu != 0 && app_input->output_obu != 1)
338         die("Invalid value for obu output flag (0, 1): %d.",
339             app_input->output_obu);
340     } else {
341       ++argj;
342     }
343   }
344 
345   // Total bitrate needs to be parsed after the number of layers.
346   for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
347     arg.argv_step = 1;
348     if (arg_match(&arg, &bitrates_arg, argi)) {
349       parse_layer_options_from_string(svc_params, BITRATE, arg.val,
350                                       svc_params->layer_target_bitrate, NULL);
351     } else {
352       ++argj;
353     }
354   }
355 
356   // There will be a space in front of the string options
357   if (strlen(string_options) > 0)
358     strncpy(app_input->options, string_options, OPTION_BUFFER_SIZE);
359 
360   // Check for unrecognized options
361   for (argi = argv; *argi; ++argi)
362     if (argi[0][0] == '-' && strlen(argi[0]) > 1)
363       die("Error: Unrecognized option %s\n", *argi);
364 
365   if (argv[0] == NULL) {
366     usage_exit();
367   }
368 
369   app_input->input_ctx.filename = argv[0];
370   free(argv);
371 
372   open_input_file(&app_input->input_ctx, 0);
373   if (app_input->input_ctx.file_type == FILE_TYPE_Y4M) {
374     enc_cfg->g_w = app_input->input_ctx.width;
375     enc_cfg->g_h = app_input->input_ctx.height;
376   }
377 
378   if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
379       enc_cfg->g_h % 2)
380     die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
381 
382   printf(
383       "Codec %s\n"
384       "layers: %d\n"
385       "width %u, height: %u\n"
386       "num: %d, den: %d, bitrate: %u\n"
387       "gop size: %u\n",
388       aom_codec_iface_name(aom_codec_av1_cx()),
389       svc_params->number_spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
390       enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
391       enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
392 }
393 
394 static unsigned int mode_to_num_temporal_layers[11] = { 1, 2, 3, 3, 2, 1,
395                                                         1, 3, 3, 3, 3 };
396 static unsigned int mode_to_num_spatial_layers[11] = { 1, 1, 1, 1, 1, 2,
397                                                        3, 2, 3, 3, 3 };
398 
399 // For rate control encoding stats.
400 struct RateControlMetrics {
401   // Number of input frames per layer.
402   int layer_input_frames[AOM_MAX_TS_LAYERS];
403   // Number of encoded non-key frames per layer.
404   int layer_enc_frames[AOM_MAX_TS_LAYERS];
405   // Framerate per layer layer (cumulative).
406   double layer_framerate[AOM_MAX_TS_LAYERS];
407   // Target average frame size per layer (per-frame-bandwidth per layer).
408   double layer_pfb[AOM_MAX_LAYERS];
409   // Actual average frame size per layer.
410   double layer_avg_frame_size[AOM_MAX_LAYERS];
411   // Average rate mismatch per layer (|target - actual| / target).
412   double layer_avg_rate_mismatch[AOM_MAX_LAYERS];
413   // Actual encoding bitrate per layer (cumulative across temporal layers).
414   double layer_encoding_bitrate[AOM_MAX_LAYERS];
415   // Average of the short-time encoder actual bitrate.
416   // TODO(marpan): Should we add these short-time stats for each layer?
417   double avg_st_encoding_bitrate;
418   // Variance of the short-time encoder actual bitrate.
419   double variance_st_encoding_bitrate;
420   // Window (number of frames) for computing short-timee encoding bitrate.
421   int window_size;
422   // Number of window measurements.
423   int window_count;
424   int layer_target_bitrate[AOM_MAX_LAYERS];
425 };
426 
427 // Reference frames used in this example encoder.
428 enum {
429   SVC_LAST_FRAME = 0,
430   SVC_LAST2_FRAME,
431   SVC_LAST3_FRAME,
432   SVC_GOLDEN_FRAME,
433   SVC_BWDREF_FRAME,
434   SVC_ALTREF2_FRAME,
435   SVC_ALTREF_FRAME
436 };
437 
read_frame(struct AvxInputContext * input_ctx,aom_image_t * img)438 static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
439   FILE *f = input_ctx->file;
440   y4m_input *y4m = &input_ctx->y4m;
441   int shortread = 0;
442 
443   if (input_ctx->file_type == FILE_TYPE_Y4M) {
444     if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
445   } else {
446     shortread = read_yuv_frame(input_ctx, img);
447   }
448 
449   return !shortread;
450 }
451 
close_input_file(struct AvxInputContext * input)452 static void close_input_file(struct AvxInputContext *input) {
453   fclose(input->file);
454   if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
455 }
456 
457 // Note: these rate control metrics assume only 1 key frame in the
458 // sequence (i.e., first frame only). So for temporal pattern# 7
459 // (which has key frame for every frame on base layer), the metrics
460 // computation will be off/wrong.
461 // TODO(marpan): Update these metrics to account for multiple key frames
462 // in the stream.
set_rate_control_metrics(struct RateControlMetrics * rc,double framerate,unsigned int ss_number_layers,unsigned int ts_number_layers)463 static void set_rate_control_metrics(struct RateControlMetrics *rc,
464                                      double framerate,
465                                      unsigned int ss_number_layers,
466                                      unsigned int ts_number_layers) {
467   int ts_rate_decimator[AOM_MAX_TS_LAYERS] = { 1 };
468   ts_rate_decimator[0] = 1;
469   if (ts_number_layers == 2) {
470     ts_rate_decimator[0] = 2;
471     ts_rate_decimator[1] = 1;
472   }
473   if (ts_number_layers == 3) {
474     ts_rate_decimator[0] = 4;
475     ts_rate_decimator[1] = 2;
476     ts_rate_decimator[2] = 1;
477   }
478   // Set the layer (cumulative) framerate and the target layer (non-cumulative)
479   // per-frame-bandwidth, for the rate control encoding stats below.
480   for (unsigned int sl = 0; sl < ss_number_layers; ++sl) {
481     unsigned int i = sl * ts_number_layers;
482     rc->layer_framerate[0] = framerate / ts_rate_decimator[0];
483     rc->layer_pfb[i] =
484         1000.0 * rc->layer_target_bitrate[i] / rc->layer_framerate[0];
485     for (unsigned int tl = 0; tl < ts_number_layers; ++tl) {
486       i = sl * ts_number_layers + tl;
487       if (tl > 0) {
488         rc->layer_framerate[tl] = framerate / ts_rate_decimator[tl];
489         rc->layer_pfb[i] =
490             1000.0 *
491             (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
492             (rc->layer_framerate[tl] - rc->layer_framerate[tl - 1]);
493       }
494       rc->layer_input_frames[tl] = 0;
495       rc->layer_enc_frames[tl] = 0;
496       rc->layer_encoding_bitrate[i] = 0.0;
497       rc->layer_avg_frame_size[i] = 0.0;
498       rc->layer_avg_rate_mismatch[i] = 0.0;
499     }
500   }
501   rc->window_count = 0;
502   rc->window_size = 15;
503   rc->avg_st_encoding_bitrate = 0.0;
504   rc->variance_st_encoding_bitrate = 0.0;
505 }
506 
printout_rate_control_summary(struct RateControlMetrics * rc,int frame_cnt,unsigned int ss_number_layers,unsigned int ts_number_layers)507 static void printout_rate_control_summary(struct RateControlMetrics *rc,
508                                           int frame_cnt,
509                                           unsigned int ss_number_layers,
510                                           unsigned int ts_number_layers) {
511   int tot_num_frames = 0;
512   double perc_fluctuation = 0.0;
513   printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
514   printf("Rate control layer stats for %u layer(s):\n\n", ts_number_layers);
515   for (unsigned int sl = 0; sl < ss_number_layers; ++sl) {
516     tot_num_frames = 0;
517     for (unsigned int tl = 0; tl < ts_number_layers; ++tl) {
518       unsigned int i = sl * ts_number_layers + tl;
519       const int num_dropped =
520           tl > 0 ? rc->layer_input_frames[tl] - rc->layer_enc_frames[tl]
521                  : rc->layer_input_frames[tl] - rc->layer_enc_frames[tl] - 1;
522       tot_num_frames += rc->layer_input_frames[tl];
523       rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[tl] *
524                                       rc->layer_encoding_bitrate[i] /
525                                       tot_num_frames;
526       rc->layer_avg_frame_size[i] =
527           rc->layer_avg_frame_size[i] / rc->layer_enc_frames[tl];
528       rc->layer_avg_rate_mismatch[i] =
529           100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[tl];
530       printf("For layer#: %u %u \n", sl, tl);
531       printf("Bitrate (target vs actual): %d %f\n", rc->layer_target_bitrate[i],
532              rc->layer_encoding_bitrate[i]);
533       printf("Average frame size (target vs actual): %f %f\n", rc->layer_pfb[i],
534              rc->layer_avg_frame_size[i]);
535       printf("Average rate_mismatch: %f\n", rc->layer_avg_rate_mismatch[i]);
536       printf(
537           "Number of input frames, encoded (non-key) frames, "
538           "and perc dropped frames: %d %d %f\n",
539           rc->layer_input_frames[tl], rc->layer_enc_frames[tl],
540           100.0 * num_dropped / rc->layer_input_frames[tl]);
541       printf("\n");
542     }
543   }
544   rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
545   rc->variance_st_encoding_bitrate =
546       rc->variance_st_encoding_bitrate / rc->window_count -
547       (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
548   perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
549                      rc->avg_st_encoding_bitrate;
550   printf("Short-time stats, for window of %d frames:\n", rc->window_size);
551   printf("Average, rms-variance, and percent-fluct: %f %f %f\n",
552          rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
553          perc_fluctuation);
554   if (frame_cnt - 1 != tot_num_frames)
555     die("Error: Number of input frames not equal to output!\n");
556 }
557 
558 // 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)559 static void set_layer_pattern(
560     int layering_mode, int superframe_cnt, aom_svc_layer_id_t *layer_id,
561     aom_svc_ref_frame_config_t *ref_frame_config,
562     aom_svc_ref_frame_comp_pred_t *ref_frame_comp_pred, int *use_svc_control,
563     int spatial_layer_id, int is_key_frame, int ksvc_mode) {
564   int i;
565   int enable_longterm_temporal_ref = 1;
566   int shift = (layering_mode == 8) ? 2 : 0;
567   *use_svc_control = 1;
568   layer_id->spatial_layer_id = spatial_layer_id;
569   int lag_index = 0;
570   int base_count = superframe_cnt >> 2;
571   ref_frame_comp_pred->use_comp_pred[0] = 0;  // GOLDEN_LAST
572   ref_frame_comp_pred->use_comp_pred[1] = 0;  // LAST2_LAST
573   ref_frame_comp_pred->use_comp_pred[2] = 0;  // ALTREF_LAST
574   // Set the reference map buffer idx for the 7 references:
575   // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
576   // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
577   for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->ref_idx[i] = i;
578   for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->reference[i] = 0;
579   for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
580 
581   if (ksvc_mode) {
582     // Same pattern as case 9.
583     layering_mode = 9;
584     if (!is_key_frame)
585       // No inter-layer prediction on inter-frames.
586       ref_frame_config->reference[SVC_LAST_FRAME] = 1;
587   }
588   switch (layering_mode) {
589     case 0:
590       // 1-layer: update LAST on every frame, reference LAST.
591       layer_id->temporal_layer_id = 0;
592       ref_frame_config->refresh[0] = 1;
593       ref_frame_config->reference[SVC_LAST_FRAME] = 1;
594       break;
595     case 1:
596       // 2-temporal layer.
597       //    1    3    5
598       //  0    2    4
599       if (superframe_cnt % 2 == 0) {
600         layer_id->temporal_layer_id = 0;
601         // Update LAST on layer 0, reference LAST.
602         ref_frame_config->refresh[0] = 1;
603         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
604       } else {
605         layer_id->temporal_layer_id = 1;
606         // No updates on layer 1, only reference LAST (TL0).
607         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
608       }
609       break;
610     case 2:
611       // 3-temporal layer:
612       //   1    3   5    7
613       //     2        6
614       // 0        4        8
615       if (superframe_cnt % 4 == 0) {
616         // Base layer.
617         layer_id->temporal_layer_id = 0;
618         // Update LAST on layer 0, reference LAST.
619         ref_frame_config->refresh[0] = 1;
620         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
621       } else if ((superframe_cnt - 1) % 4 == 0) {
622         layer_id->temporal_layer_id = 2;
623         // First top layer: no updates, only reference LAST (TL0).
624         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
625       } else if ((superframe_cnt - 2) % 4 == 0) {
626         layer_id->temporal_layer_id = 1;
627         // Middle layer (TL1): update LAST2, only reference LAST (TL0).
628         ref_frame_config->refresh[1] = 1;
629         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
630       } else if ((superframe_cnt - 3) % 4 == 0) {
631         layer_id->temporal_layer_id = 2;
632         // Second top layer: no updates, only reference LAST.
633         // Set buffer idx for LAST to slot 1, since that was the slot
634         // updated in previous frame. So LAST is TL1 frame.
635         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
636         ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
637         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
638       }
639       break;
640     case 3:
641       // 3 TL, same as above, except allow for predicting
642       // off 2 more references (GOLDEN and ALTREF), with
643       // GOLDEN updated periodically, and ALTREF lagging from
644       // LAST from ~4 frames. Both GOLDEN and ALTREF
645       // can only be updated on base temporal layer.
646 
647       // Keep golden fixed at slot 3.
648       ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
649       // Cyclically refresh slots 4, 5, 6, 7, for lag altref.
650       lag_index = 4 + (base_count % 4);
651       // Set the altref slot to lag_index.
652       ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = lag_index;
653       if (superframe_cnt % 4 == 0) {
654         // Base layer.
655         layer_id->temporal_layer_id = 0;
656         // Update LAST on layer 0, reference LAST.
657         ref_frame_config->refresh[0] = 1;
658         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
659         // Refresh GOLDEN every x ~10 base layer frames.
660         if (base_count % 10 == 0) ref_frame_config->refresh[3] = 1;
661         // Refresh lag_index slot, needed for lagging altref.
662         ref_frame_config->refresh[lag_index] = 1;
663       } else if ((superframe_cnt - 1) % 4 == 0) {
664         layer_id->temporal_layer_id = 2;
665         // First top layer: no updates, only reference LAST (TL0).
666         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
667       } else if ((superframe_cnt - 2) % 4 == 0) {
668         layer_id->temporal_layer_id = 1;
669         // Middle layer (TL1): update LAST2, only reference LAST (TL0).
670         ref_frame_config->refresh[1] = 1;
671         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
672       } else if ((superframe_cnt - 3) % 4 == 0) {
673         layer_id->temporal_layer_id = 2;
674         // Second top layer: no updates, only reference LAST.
675         // Set buffer idx for LAST to slot 1, since that was the slot
676         // updated in previous frame. So LAST is TL1 frame.
677         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
678         ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
679         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
680       }
681       // Every frame can reference GOLDEN AND ALTREF.
682       ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
683       ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
684       break;
685     case 4:
686       // 3-temporal layer: but middle layer updates GF, so 2nd TL2 will
687       // only reference GF (not LAST). Other frames only reference LAST.
688       //   1    3   5    7
689       //     2        6
690       // 0        4        8
691       if (superframe_cnt % 4 == 0) {
692         // Base layer.
693         layer_id->temporal_layer_id = 0;
694         // Update LAST on layer 0, only reference LAST.
695         ref_frame_config->refresh[0] = 1;
696         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
697       } else if ((superframe_cnt - 1) % 4 == 0) {
698         layer_id->temporal_layer_id = 2;
699         // First top layer: no updates, only reference LAST (TL0).
700         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
701       } else if ((superframe_cnt - 2) % 4 == 0) {
702         layer_id->temporal_layer_id = 1;
703         // Middle layer (TL1): update GF, only reference LAST (TL0).
704         ref_frame_config->refresh[3] = 1;
705         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
706       } else if ((superframe_cnt - 3) % 4 == 0) {
707         layer_id->temporal_layer_id = 2;
708         // Second top layer: no updates, only reference GF.
709         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
710       }
711       break;
712     case 5:
713       // 2 spatial layers, 1 temporal.
714       layer_id->temporal_layer_id = 0;
715       if (layer_id->spatial_layer_id == 0) {
716         // Reference LAST, update LAST.
717         ref_frame_config->refresh[0] = 1;
718         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
719       } else if (layer_id->spatial_layer_id == 1) {
720         // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
721         // and GOLDEN to slot 0. Update slot 1 (LAST).
722         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
723         ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 0;
724         ref_frame_config->refresh[1] = 1;
725         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
726         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
727       }
728       break;
729     case 6:
730       // 3 spatial layers, 1 temporal.
731       // Note for this case, we set the buffer idx for all references to be
732       // either LAST or GOLDEN, which are always valid references, since decoder
733       // will check if any of the 7 references is valid scale in
734       // valid_ref_frame_size().
735       layer_id->temporal_layer_id = 0;
736       if (layer_id->spatial_layer_id == 0) {
737         // Reference LAST, update LAST. Set all buffer_idx to 0.
738         for (i = 0; i < INTER_REFS_PER_FRAME; i++)
739           ref_frame_config->ref_idx[i] = 0;
740         ref_frame_config->refresh[0] = 1;
741         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
742       } else if (layer_id->spatial_layer_id == 1) {
743         // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
744         // and GOLDEN (and all other refs) to slot 0.
745         // Update slot 1 (LAST).
746         for (i = 0; i < INTER_REFS_PER_FRAME; i++)
747           ref_frame_config->ref_idx[i] = 0;
748         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
749         ref_frame_config->refresh[1] = 1;
750         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
751         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
752       } else if (layer_id->spatial_layer_id == 2) {
753         // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2
754         // and GOLDEN (and all other refs) to slot 1.
755         // Update slot 2 (LAST).
756         for (i = 0; i < INTER_REFS_PER_FRAME; i++)
757           ref_frame_config->ref_idx[i] = 1;
758         ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
759         ref_frame_config->refresh[2] = 1;
760         ref_frame_config->reference[SVC_LAST_FRAME] = 1;
761         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
762         // For 3 spatial layer case: allow for top spatial layer to use
763         // additional temporal reference. Update every 10 frames.
764         if (enable_longterm_temporal_ref) {
765           ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
766           ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
767           if (base_count % 10 == 0)
768             ref_frame_config->refresh[REF_FRAMES - 1] = 1;
769         }
770       }
771       break;
772     case 7:
773       // 2 spatial and 3 temporal layer.
774       ref_frame_config->reference[SVC_LAST_FRAME] = 1;
775       if (superframe_cnt % 4 == 0) {
776         // Base temporal layer
777         layer_id->temporal_layer_id = 0;
778         if (layer_id->spatial_layer_id == 0) {
779           // Reference LAST, update LAST
780           // Set all buffer_idx to 0
781           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
782             ref_frame_config->ref_idx[i] = 0;
783           ref_frame_config->refresh[0] = 1;
784         } else if (layer_id->spatial_layer_id == 1) {
785           // Reference LAST and GOLDEN.
786           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
787             ref_frame_config->ref_idx[i] = 0;
788           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
789           ref_frame_config->refresh[1] = 1;
790         }
791       } else if ((superframe_cnt - 1) % 4 == 0) {
792         // First top temporal enhancement layer.
793         layer_id->temporal_layer_id = 2;
794         if (layer_id->spatial_layer_id == 0) {
795           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
796             ref_frame_config->ref_idx[i] = 0;
797           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
798           ref_frame_config->refresh[3] = 1;
799         } else if (layer_id->spatial_layer_id == 1) {
800           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
801           // GOLDEN (and all other refs) to slot 3.
802           // No update.
803           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
804             ref_frame_config->ref_idx[i] = 3;
805           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
806         }
807       } else if ((superframe_cnt - 2) % 4 == 0) {
808         // Middle temporal enhancement layer.
809         layer_id->temporal_layer_id = 1;
810         if (layer_id->spatial_layer_id == 0) {
811           // Reference LAST.
812           // Set all buffer_idx to 0.
813           // Set GOLDEN to slot 5 and update slot 5.
814           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
815             ref_frame_config->ref_idx[i] = 0;
816           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
817           ref_frame_config->refresh[5 - shift] = 1;
818         } else if (layer_id->spatial_layer_id == 1) {
819           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
820           // GOLDEN (and all other refs) to slot 5.
821           // Set LAST3 to slot 6 and update slot 6.
822           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
823             ref_frame_config->ref_idx[i] = 5 - shift;
824           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
825           ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
826           ref_frame_config->refresh[6 - shift] = 1;
827         }
828       } else if ((superframe_cnt - 3) % 4 == 0) {
829         // Second top temporal enhancement layer.
830         layer_id->temporal_layer_id = 2;
831         if (layer_id->spatial_layer_id == 0) {
832           // Set LAST to slot 5 and reference LAST.
833           // Set GOLDEN to slot 3 and update slot 3.
834           // Set all other buffer_idx to 0.
835           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
836             ref_frame_config->ref_idx[i] = 0;
837           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
838           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
839           ref_frame_config->refresh[3] = 1;
840         } else if (layer_id->spatial_layer_id == 1) {
841           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
842           // GOLDEN to slot 3. No update.
843           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
844             ref_frame_config->ref_idx[i] = 0;
845           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
846           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
847         }
848       }
849       if (layer_id->spatial_layer_id > 0 && !ksvc_mode) {
850         // Reference GOLDEN.
851         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
852       }
853       break;
854     case 8:
855       // 3 spatial and 3 temporal layer.
856       // Same as case 9 but overalap in the buffer slot updates.
857       // (shift = 2). The slots 3 and 4 updated by first TL2 are
858       // reused for update in TL1 superframe.
859       // Note for this case, frame order hint must be disabled for
860       // lower resolutios (operating points > 0) to be decoedable.
861     case 9:
862       // 3 spatial and 3 temporal layer.
863       // No overlap in buffer updates between TL2 and TL1.
864       // TL2 updates slot 3 and 4, TL1 updates 5, 6, 7.
865       // Set the references via the svc_ref_frame_config control.
866       // Always reference LAST.
867       ref_frame_config->reference[SVC_LAST_FRAME] = 1;
868       if (superframe_cnt % 4 == 0) {
869         // Base temporal layer.
870         layer_id->temporal_layer_id = 0;
871         if (layer_id->spatial_layer_id == 0) {
872           // Reference LAST, update LAST.
873           // Set all buffer_idx to 0.
874           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
875             ref_frame_config->ref_idx[i] = 0;
876           ref_frame_config->refresh[0] = 1;
877         } else if (layer_id->spatial_layer_id == 1) {
878           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
879           // GOLDEN (and all other refs) to slot 0.
880           // Update slot 1 (LAST).
881           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
882             ref_frame_config->ref_idx[i] = 0;
883           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
884           ref_frame_config->refresh[1] = 1;
885         } else if (layer_id->spatial_layer_id == 2) {
886           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
887           // GOLDEN (and all other refs) to slot 1.
888           // Update slot 2 (LAST).
889           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
890             ref_frame_config->ref_idx[i] = 1;
891           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
892           ref_frame_config->refresh[2] = 1;
893         }
894       } else if ((superframe_cnt - 1) % 4 == 0) {
895         // First top temporal enhancement layer.
896         layer_id->temporal_layer_id = 2;
897         if (layer_id->spatial_layer_id == 0) {
898           // Reference LAST (slot 0).
899           // Set GOLDEN to slot 3 and update slot 3.
900           // Set all other buffer_idx to slot 0.
901           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
902             ref_frame_config->ref_idx[i] = 0;
903           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
904           ref_frame_config->refresh[3] = 1;
905         } else if (layer_id->spatial_layer_id == 1) {
906           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
907           // GOLDEN (and all other refs) to slot 3.
908           // Set LAST2 to slot 4 and Update slot 4.
909           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
910             ref_frame_config->ref_idx[i] = 3;
911           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
912           ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
913           ref_frame_config->refresh[4] = 1;
914         } else if (layer_id->spatial_layer_id == 2) {
915           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
916           // GOLDEN (and all other refs) to slot 4.
917           // No update.
918           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
919             ref_frame_config->ref_idx[i] = 4;
920           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
921         }
922       } else if ((superframe_cnt - 2) % 4 == 0) {
923         // Middle temporal enhancement layer.
924         layer_id->temporal_layer_id = 1;
925         if (layer_id->spatial_layer_id == 0) {
926           // Reference LAST.
927           // Set all buffer_idx to 0.
928           // Set GOLDEN to slot 5 and update slot 5.
929           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
930             ref_frame_config->ref_idx[i] = 0;
931           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
932           ref_frame_config->refresh[5 - shift] = 1;
933         } else if (layer_id->spatial_layer_id == 1) {
934           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
935           // GOLDEN (and all other refs) to slot 5.
936           // Set LAST3 to slot 6 and update slot 6.
937           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
938             ref_frame_config->ref_idx[i] = 5 - shift;
939           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
940           ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
941           ref_frame_config->refresh[6 - shift] = 1;
942         } else if (layer_id->spatial_layer_id == 2) {
943           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
944           // GOLDEN (and all other refs) to slot 6.
945           // Set LAST3 to slot 7 and update slot 7.
946           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
947             ref_frame_config->ref_idx[i] = 6 - shift;
948           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
949           ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 7 - shift;
950           ref_frame_config->refresh[7 - shift] = 1;
951         }
952       } else if ((superframe_cnt - 3) % 4 == 0) {
953         // Second top temporal enhancement layer.
954         layer_id->temporal_layer_id = 2;
955         if (layer_id->spatial_layer_id == 0) {
956           // Set LAST to slot 5 and reference LAST.
957           // Set GOLDEN to slot 3 and update slot 3.
958           // Set all other buffer_idx to 0.
959           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
960             ref_frame_config->ref_idx[i] = 0;
961           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
962           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
963           ref_frame_config->refresh[3] = 1;
964         } else if (layer_id->spatial_layer_id == 1) {
965           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
966           // GOLDEN to slot 3. Set LAST2 to slot 4 and update slot 4.
967           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
968             ref_frame_config->ref_idx[i] = 0;
969           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
970           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
971           ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
972           ref_frame_config->refresh[4] = 1;
973         } else if (layer_id->spatial_layer_id == 2) {
974           // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 7,
975           // GOLDEN to slot 4. No update.
976           for (i = 0; i < INTER_REFS_PER_FRAME; i++)
977             ref_frame_config->ref_idx[i] = 0;
978           ref_frame_config->ref_idx[SVC_LAST_FRAME] = 7 - shift;
979           ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 4;
980         }
981       }
982       if (layer_id->spatial_layer_id > 0 && !ksvc_mode)
983         // Reference GOLDEN.
984         ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
985       // For 3 spatial layer case 8 (where there is free buffer slot):
986       // allow for top spatial layer to use additional temporal reference.
987       // Additional reference is only updated on base temporal layer, every
988       // 10 TL0 frames here.
989       if (enable_longterm_temporal_ref && layer_id->spatial_layer_id == 2 &&
990           layering_mode == 8) {
991         ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
992         ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
993         if (base_count % 10 == 0 && layer_id->temporal_layer_id == 0)
994           ref_frame_config->refresh[REF_FRAMES - 1] = 1;
995       }
996       break;
997     default: assert(0); die("Error: Unsupported temporal layering mode!\n");
998   }
999 }
1000 
1001 #if CONFIG_AV1_DECODER
test_decode(aom_codec_ctx_t * encoder,aom_codec_ctx_t * decoder,const int frames_out,int * mismatch_seen)1002 static void test_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
1003                         const int frames_out, int *mismatch_seen) {
1004   aom_image_t enc_img, dec_img;
1005 
1006   if (*mismatch_seen) return;
1007 
1008   /* Get the internal reference frame */
1009   AOM_CODEC_CONTROL_TYPECHECKED(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1010   AOM_CODEC_CONTROL_TYPECHECKED(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
1011 
1012 #if CONFIG_AV1_HIGHBITDEPTH
1013   if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1014       (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1015     if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1016       aom_image_t enc_hbd_img;
1017       aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1018                     enc_img.d_w, enc_img.d_h, 16);
1019       aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1020       enc_img = enc_hbd_img;
1021     }
1022     if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1023       aom_image_t dec_hbd_img;
1024       aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1025                     dec_img.d_w, dec_img.d_h, 16);
1026       aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1027       dec_img = dec_hbd_img;
1028     }
1029   }
1030 #endif
1031 
1032   if (!aom_compare_img(&enc_img, &dec_img)) {
1033     int y[4], u[4], v[4];
1034 #if CONFIG_AV1_HIGHBITDEPTH
1035     if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1036       aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
1037     } else {
1038       aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1039     }
1040 #else
1041     aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1042 #endif
1043     decoder->err = 1;
1044     printf(
1045         "Encode/decode mismatch on frame %d at"
1046         " Y[%d, %d] {%d/%d},"
1047         " U[%d, %d] {%d/%d},"
1048         " V[%d, %d] {%d/%d}",
1049         frames_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
1050         v[2], v[3]);
1051     *mismatch_seen = frames_out;
1052   }
1053 
1054   aom_img_free(&enc_img);
1055   aom_img_free(&dec_img);
1056 }
1057 #endif  // CONFIG_AV1_DECODER
1058 
main(int argc,const char ** argv)1059 int main(int argc, const char **argv) {
1060   AppInput app_input;
1061   AvxVideoWriter *outfile[AOM_MAX_LAYERS] = { NULL };
1062   FILE *obu_files[AOM_MAX_LAYERS] = { NULL };
1063   AvxVideoWriter *total_layer_file = NULL;
1064   FILE *total_layer_obu_file = NULL;
1065   aom_codec_enc_cfg_t cfg;
1066   int frame_cnt = 0;
1067   aom_image_t raw;
1068   int frame_avail;
1069   int got_data = 0;
1070   int flags = 0;
1071   unsigned i;
1072   int pts = 0;             // PTS starts at 0.
1073   int frame_duration = 1;  // 1 timebase tick per frame.
1074   aom_svc_layer_id_t layer_id;
1075   aom_svc_params_t svc_params;
1076   aom_svc_ref_frame_config_t ref_frame_config;
1077   aom_svc_ref_frame_comp_pred_t ref_frame_comp_pred;
1078 
1079 #if CONFIG_INTERNAL_STATS
1080   FILE *stats_file = fopen("opsnr.stt", "a");
1081   if (stats_file == NULL) {
1082     die("Cannot open opsnr.stt\n");
1083   }
1084 #endif
1085 #if CONFIG_AV1_DECODER
1086   int mismatch_seen = 0;
1087   aom_codec_ctx_t decoder;
1088 #endif
1089 
1090   struct RateControlMetrics rc;
1091   int64_t cx_time = 0;
1092   int64_t cx_time_sl[3];  // max number of spatial layers.
1093   double sum_bitrate = 0.0;
1094   double sum_bitrate2 = 0.0;
1095   double framerate = 30.0;
1096   int use_svc_control = 1;
1097   int set_err_resil_frame = 0;
1098   zero(rc.layer_target_bitrate);
1099   memset(&layer_id, 0, sizeof(aom_svc_layer_id_t));
1100   memset(&app_input, 0, sizeof(AppInput));
1101   memset(&svc_params, 0, sizeof(svc_params));
1102 
1103   // Flag to test dynamic scaling of source frames for single
1104   // spatial stream, using the scaling_mode control.
1105   const int test_dynamic_scaling_single_layer = 0;
1106 
1107   /* Setup default input stream settings */
1108   app_input.input_ctx.framerate.numerator = 30;
1109   app_input.input_ctx.framerate.denominator = 1;
1110   app_input.input_ctx.only_i420 = 1;
1111   app_input.input_ctx.bit_depth = 0;
1112   app_input.speed = 7;
1113   exec_name = argv[0];
1114 
1115   // start with default encoder configuration
1116   aom_codec_err_t res = aom_codec_enc_config_default(aom_codec_av1_cx(), &cfg,
1117                                                      AOM_USAGE_REALTIME);
1118   if (res) {
1119     die("Failed to get config: %s\n", aom_codec_err_to_string(res));
1120   }
1121 
1122   // Real time parameters.
1123   cfg.g_usage = AOM_USAGE_REALTIME;
1124 
1125   cfg.rc_end_usage = AOM_CBR;
1126   cfg.rc_min_quantizer = 2;
1127   cfg.rc_max_quantizer = 52;
1128   cfg.rc_undershoot_pct = 50;
1129   cfg.rc_overshoot_pct = 50;
1130   cfg.rc_buf_initial_sz = 600;
1131   cfg.rc_buf_optimal_sz = 600;
1132   cfg.rc_buf_sz = 1000;
1133   cfg.rc_resize_mode = 0;  // Set to RESIZE_DYNAMIC for dynamic resize.
1134   cfg.g_lag_in_frames = 0;
1135   cfg.kf_mode = AOM_KF_AUTO;
1136 
1137   parse_command_line(argc, argv, &app_input, &svc_params, &cfg);
1138 
1139   unsigned int ts_number_layers = svc_params.number_temporal_layers;
1140   unsigned int ss_number_layers = svc_params.number_spatial_layers;
1141 
1142   unsigned int width = cfg.g_w;
1143   unsigned int height = cfg.g_h;
1144 
1145   if (app_input.layering_mode >= 0) {
1146     if (ts_number_layers !=
1147             mode_to_num_temporal_layers[app_input.layering_mode] ||
1148         ss_number_layers !=
1149             mode_to_num_spatial_layers[app_input.layering_mode]) {
1150       die("Number of layers doesn't match layering mode.");
1151     }
1152   }
1153 
1154   // Y4M reader has its own allocation.
1155   if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
1156     if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, width, height, 32)) {
1157       die("Failed to allocate image", width, height);
1158     }
1159   }
1160 
1161   aom_codec_iface_t *encoder = get_aom_encoder_by_short_name("av1");
1162 
1163   memcpy(&rc.layer_target_bitrate[0], &svc_params.layer_target_bitrate[0],
1164          sizeof(svc_params.layer_target_bitrate));
1165 
1166   unsigned int total_rate = 0;
1167   for (i = 0; i < ss_number_layers; i++) {
1168     total_rate +=
1169         svc_params
1170             .layer_target_bitrate[i * ts_number_layers + ts_number_layers - 1];
1171   }
1172   if (total_rate != cfg.rc_target_bitrate) {
1173     die("Incorrect total target bitrate");
1174   }
1175 
1176   svc_params.framerate_factor[0] = 1;
1177   if (ts_number_layers == 2) {
1178     svc_params.framerate_factor[0] = 2;
1179     svc_params.framerate_factor[1] = 1;
1180   } else if (ts_number_layers == 3) {
1181     svc_params.framerate_factor[0] = 4;
1182     svc_params.framerate_factor[1] = 2;
1183     svc_params.framerate_factor[2] = 1;
1184   }
1185 
1186   if (app_input.input_ctx.file_type == FILE_TYPE_Y4M) {
1187     // Override these settings with the info from Y4M file.
1188     cfg.g_w = app_input.input_ctx.width;
1189     cfg.g_h = app_input.input_ctx.height;
1190     // g_timebase is the reciprocal of frame rate.
1191     cfg.g_timebase.num = app_input.input_ctx.framerate.denominator;
1192     cfg.g_timebase.den = app_input.input_ctx.framerate.numerator;
1193   }
1194   framerate = cfg.g_timebase.den / cfg.g_timebase.num;
1195   set_rate_control_metrics(&rc, framerate, ss_number_layers, ts_number_layers);
1196 
1197   AvxVideoInfo info;
1198   info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
1199   info.frame_width = cfg.g_w;
1200   info.frame_height = cfg.g_h;
1201   info.time_base.numerator = cfg.g_timebase.num;
1202   info.time_base.denominator = cfg.g_timebase.den;
1203   // Open an output file for each stream.
1204   for (unsigned int sl = 0; sl < ss_number_layers; ++sl) {
1205     for (unsigned tl = 0; tl < ts_number_layers; ++tl) {
1206       i = sl * ts_number_layers + tl;
1207       char file_name[PATH_MAX];
1208       snprintf(file_name, sizeof(file_name), "%s_%u.av1",
1209                app_input.output_filename, i);
1210       if (app_input.output_obu) {
1211         obu_files[i] = fopen(file_name, "wb");
1212         if (!obu_files[i]) die("Failed to open %s for writing", file_name);
1213       } else {
1214         outfile[i] = aom_video_writer_open(file_name, kContainerIVF, &info);
1215         if (!outfile[i]) die("Failed to open %s for writing", file_name);
1216       }
1217     }
1218   }
1219   if (app_input.output_obu) {
1220     total_layer_obu_file = fopen(app_input.output_filename, "wb");
1221     if (!total_layer_obu_file)
1222       die("Failed to open %s for writing", app_input.output_filename);
1223   } else {
1224     total_layer_file =
1225         aom_video_writer_open(app_input.output_filename, kContainerIVF, &info);
1226     if (!total_layer_file)
1227       die("Failed to open %s for writing", app_input.output_filename);
1228   }
1229 
1230   // Initialize codec.
1231   aom_codec_ctx_t codec;
1232   if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
1233     die("Failed to initialize encoder");
1234 
1235 #if CONFIG_AV1_DECODER
1236   if (aom_codec_dec_init(&decoder, get_aom_decoder_by_index(0), NULL, 0)) {
1237     die("Failed to initialize decoder");
1238   }
1239 #endif
1240 
1241   aom_codec_control(&codec, AOME_SET_CPUUSED, app_input.speed);
1242   aom_codec_control(&codec, AV1E_SET_AQ_MODE, app_input.aq_mode ? 3 : 0);
1243   aom_codec_control(&codec, AV1E_SET_GF_CBR_BOOST_PCT, 0);
1244   aom_codec_control(&codec, AV1E_SET_ENABLE_CDEF, 1);
1245   aom_codec_control(&codec, AV1E_SET_ENABLE_WARPED_MOTION, 0);
1246   aom_codec_control(&codec, AV1E_SET_ENABLE_OBMC, 0);
1247   aom_codec_control(&codec, AV1E_SET_ENABLE_GLOBAL_MOTION, 0);
1248   aom_codec_control(&codec, AV1E_SET_ENABLE_ORDER_HINT, 0);
1249   aom_codec_control(&codec, AV1E_SET_ENABLE_TPL_MODEL, 0);
1250   aom_codec_control(&codec, AV1E_SET_DELTAQ_MODE, 0);
1251   aom_codec_control(&codec, AV1E_SET_COEFF_COST_UPD_FREQ, 3);
1252   aom_codec_control(&codec, AV1E_SET_MODE_COST_UPD_FREQ, 3);
1253   aom_codec_control(&codec, AV1E_SET_MV_COST_UPD_FREQ, 3);
1254   aom_codec_control(&codec, AV1E_SET_DV_COST_UPD_FREQ, 3);
1255   aom_codec_control(&codec, AV1E_SET_CDF_UPDATE_MODE, 1);
1256   aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS,
1257                     cfg.g_threads ? get_msb(cfg.g_threads) : 0);
1258   if (cfg.g_threads > 1) aom_codec_control(&codec, AV1E_SET_ROW_MT, 1);
1259 
1260   svc_params.number_spatial_layers = ss_number_layers;
1261   svc_params.number_temporal_layers = ts_number_layers;
1262   for (i = 0; i < ss_number_layers * ts_number_layers; ++i) {
1263     svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
1264     svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
1265   }
1266   for (i = 0; i < ss_number_layers; ++i) {
1267     svc_params.scaling_factor_num[i] = 1;
1268     svc_params.scaling_factor_den[i] = 1;
1269   }
1270   if (ss_number_layers == 2) {
1271     svc_params.scaling_factor_num[0] = 1;
1272     svc_params.scaling_factor_den[0] = 2;
1273   } else if (ss_number_layers == 3) {
1274     svc_params.scaling_factor_num[0] = 1;
1275     svc_params.scaling_factor_den[0] = 4;
1276     svc_params.scaling_factor_num[1] = 1;
1277     svc_params.scaling_factor_den[1] = 2;
1278   }
1279   aom_codec_control(&codec, AV1E_SET_SVC_PARAMS, &svc_params);
1280   // TODO(aomedia:3032): Configure KSVC in fixed mode.
1281 
1282   // This controls the maximum target size of the key frame.
1283   // For generating smaller key frames, use a smaller max_intra_size_pct
1284   // value, like 100 or 200.
1285   {
1286     const int max_intra_size_pct = 300;
1287     aom_codec_control(&codec, AOME_SET_MAX_INTRA_BITRATE_PCT,
1288                       max_intra_size_pct);
1289   }
1290 
1291   for (unsigned int slx = 0; slx < ss_number_layers; slx++) cx_time_sl[slx] = 0;
1292   frame_avail = 1;
1293   while (frame_avail || got_data) {
1294     struct aom_usec_timer timer;
1295     frame_avail = read_frame(&(app_input.input_ctx), &raw);
1296     int is_key_frame = (frame_cnt % cfg.kf_max_dist) == 0;
1297     // Loop over spatial layers.
1298     for (unsigned int slx = 0; slx < ss_number_layers; slx++) {
1299       aom_codec_iter_t iter = NULL;
1300       const aom_codec_cx_pkt_t *pkt;
1301       int layer = 0;
1302 
1303       // For flexible mode:
1304       if (app_input.layering_mode >= 0) {
1305         // Set the reference/update flags, layer_id, and reference_map
1306         // buffer index.
1307         set_layer_pattern(app_input.layering_mode, frame_cnt, &layer_id,
1308                           &ref_frame_config, &ref_frame_comp_pred,
1309                           &use_svc_control, slx, is_key_frame,
1310                           (app_input.layering_mode == 10));
1311         aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
1312         if (use_svc_control) {
1313           aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_CONFIG,
1314                             &ref_frame_config);
1315           aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_COMP_PRED,
1316                             &ref_frame_comp_pred);
1317         }
1318       } else {
1319         // Only up to 3 temporal layers supported in fixed mode.
1320         // Only need to set spatial and temporal layer_id: reference
1321         // prediction, refresh, and buffer_idx are set internally.
1322         layer_id.spatial_layer_id = slx;
1323         layer_id.temporal_layer_id = 0;
1324         if (ts_number_layers == 2) {
1325           layer_id.temporal_layer_id = (frame_cnt % 2) != 0;
1326         } else if (ts_number_layers == 3) {
1327           if (frame_cnt % 2 != 0)
1328             layer_id.temporal_layer_id = 2;
1329           else if ((frame_cnt > 1) && ((frame_cnt - 2) % 4 == 0))
1330             layer_id.temporal_layer_id = 1;
1331         }
1332         aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
1333       }
1334 
1335       if (set_err_resil_frame) {
1336         // Set error_resilient per frame: off/0 for base layer and
1337         // on/1 for enhancement layer frames.
1338         int err_resil_mode =
1339             (layer_id.spatial_layer_id > 0 || layer_id.temporal_layer_id > 0);
1340         aom_codec_control(&codec, AV1E_SET_ERROR_RESILIENT_MODE,
1341                           err_resil_mode);
1342       }
1343 
1344       layer = slx * ts_number_layers + layer_id.temporal_layer_id;
1345       if (frame_avail && slx == 0) ++rc.layer_input_frames[layer];
1346 
1347       if (test_dynamic_scaling_single_layer) {
1348         if (frame_cnt >= 200 && frame_cnt <= 400) {
1349           // Scale source down by 2x2.
1350           struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
1351           aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
1352         } else {
1353           // Source back up to original resolution (no scaling).
1354           struct aom_scaling_mode mode = { AOME_NORMAL, AOME_NORMAL };
1355           aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
1356         }
1357       }
1358 
1359       // Do the layer encode.
1360       aom_usec_timer_start(&timer);
1361       if (aom_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags))
1362         die_codec(&codec, "Failed to encode frame");
1363       aom_usec_timer_mark(&timer);
1364       cx_time += aom_usec_timer_elapsed(&timer);
1365       cx_time_sl[slx] += aom_usec_timer_elapsed(&timer);
1366 
1367       got_data = 0;
1368       while ((pkt = aom_codec_get_cx_data(&codec, &iter))) {
1369         got_data = 1;
1370         switch (pkt->kind) {
1371           case AOM_CODEC_CX_FRAME_PKT:
1372             for (unsigned int sl = layer_id.spatial_layer_id;
1373                  sl < ss_number_layers; ++sl) {
1374               for (unsigned tl = layer_id.temporal_layer_id;
1375                    tl < ts_number_layers; ++tl) {
1376                 unsigned int j = sl * ts_number_layers + tl;
1377                 if (app_input.output_obu) {
1378                   fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1379                          obu_files[j]);
1380                 } else {
1381                   aom_video_writer_write_frame(outfile[j], pkt->data.frame.buf,
1382                                                pkt->data.frame.sz, pts);
1383                 }
1384                 if (sl == (unsigned int)layer_id.spatial_layer_id)
1385                   rc.layer_encoding_bitrate[j] += 8.0 * pkt->data.frame.sz;
1386               }
1387             }
1388             // Write everything into the top layer.
1389             if (app_input.output_obu) {
1390               fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1391                      total_layer_obu_file);
1392             } else {
1393               aom_video_writer_write_frame(total_layer_file,
1394                                            pkt->data.frame.buf,
1395                                            pkt->data.frame.sz, pts);
1396             }
1397             // Keep count of rate control stats per layer (for non-key).
1398             if (!(pkt->data.frame.flags & AOM_FRAME_IS_KEY)) {
1399               unsigned int j = layer_id.spatial_layer_id * ts_number_layers +
1400                                layer_id.temporal_layer_id;
1401               rc.layer_avg_frame_size[j] += 8.0 * pkt->data.frame.sz;
1402               rc.layer_avg_rate_mismatch[j] +=
1403                   fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[j]) /
1404                   rc.layer_pfb[j];
1405               if (slx == 0) ++rc.layer_enc_frames[layer_id.temporal_layer_id];
1406             }
1407 
1408             // Update for short-time encoding bitrate states, for moving window
1409             // of size rc->window, shifted by rc->window / 2.
1410             // Ignore first window segment, due to key frame.
1411             // For spatial layers: only do this for top/highest SL.
1412             if (frame_cnt > rc.window_size && slx == ss_number_layers - 1) {
1413               sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
1414               rc.window_size = (rc.window_size <= 0) ? 1 : rc.window_size;
1415               if (frame_cnt % rc.window_size == 0) {
1416                 rc.window_count += 1;
1417                 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
1418                 rc.variance_st_encoding_bitrate +=
1419                     (sum_bitrate / rc.window_size) *
1420                     (sum_bitrate / rc.window_size);
1421                 sum_bitrate = 0.0;
1422               }
1423             }
1424             // Second shifted window.
1425             if (frame_cnt > rc.window_size + rc.window_size / 2 &&
1426                 slx == ss_number_layers - 1) {
1427               sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
1428               if (frame_cnt > 2 * rc.window_size &&
1429                   frame_cnt % rc.window_size == 0) {
1430                 rc.window_count += 1;
1431                 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
1432                 rc.variance_st_encoding_bitrate +=
1433                     (sum_bitrate2 / rc.window_size) *
1434                     (sum_bitrate2 / rc.window_size);
1435                 sum_bitrate2 = 0.0;
1436               }
1437             }
1438 
1439 #if CONFIG_AV1_DECODER
1440             if (aom_codec_decode(&decoder, pkt->data.frame.buf,
1441                                  (unsigned int)pkt->data.frame.sz, NULL))
1442               die_codec(&decoder, "Failed to decode frame.");
1443 #endif
1444 
1445             break;
1446           default: break;
1447         }
1448       }
1449 #if CONFIG_AV1_DECODER
1450       // Don't look for mismatch on top spatial and top temporal layers as they
1451       // are non reference frames.
1452       if ((ss_number_layers > 1 || ts_number_layers > 1) &&
1453           !(layer_id.temporal_layer_id > 0 &&
1454             layer_id.temporal_layer_id == (int)ts_number_layers - 1)) {
1455         test_decode(&codec, &decoder, frame_cnt, &mismatch_seen);
1456       }
1457 #endif
1458     }  // loop over spatial layers
1459     ++frame_cnt;
1460     pts += frame_duration;
1461   }
1462 
1463   close_input_file(&(app_input.input_ctx));
1464   printout_rate_control_summary(&rc, frame_cnt, ss_number_layers,
1465                                 ts_number_layers);
1466   printf("\n");
1467   printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f\n",
1468          frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
1469          1000000 * (double)frame_cnt / (double)cx_time);
1470 
1471   if (ss_number_layers > 1) {
1472     printf("Per spatial layer: \n");
1473     for (unsigned int slx = 0; slx < ss_number_layers; slx++)
1474       printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f\n",
1475              frame_cnt, (float)cx_time_sl[slx] / (double)(frame_cnt * 1000),
1476              1000000 * (double)frame_cnt / (double)cx_time_sl[slx]);
1477   }
1478 
1479   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
1480 
1481 #if CONFIG_INTERNAL_STATS
1482   if (mismatch_seen) {
1483     fprintf(stats_file, "First mismatch occurred in frame %d\n", mismatch_seen);
1484   } else {
1485     fprintf(stats_file, "No mismatch detected in recon buffers\n");
1486   }
1487   fclose(stats_file);
1488 #endif
1489 
1490   // Try to rewrite the output file headers with the actual frame count.
1491   for (i = 0; i < ss_number_layers * ts_number_layers; ++i)
1492     aom_video_writer_close(outfile[i]);
1493   aom_video_writer_close(total_layer_file);
1494 
1495   if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
1496     aom_img_free(&raw);
1497   }
1498   return EXIT_SUCCESS;
1499 }
1500