AOMedia AV1 Codec
svc_encoder_rtc
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 "aom_ports/aom_timer.h"
28 
29 #define OPTION_BUFFER_SIZE 1024
30 
31 typedef struct {
32  const char *output_filename;
33  char options[OPTION_BUFFER_SIZE];
34  struct AvxInputContext input_ctx;
35  int speed;
36  int aq_mode;
37  int layering_mode;
38 } AppInput;
39 
40 typedef enum {
41  QUANTIZER = 0,
42  BITRATE,
43  SCALE_FACTOR,
44  AUTO_ALT_REF,
45  ALL_OPTION_TYPES
46 } LAYER_OPTION_TYPE;
47 
48 static const arg_def_t outputfile =
49  ARG_DEF("o", "output", 1, "Output filename");
50 static const arg_def_t frames_arg =
51  ARG_DEF("f", "frames", 1, "Number of frames to encode");
52 static const arg_def_t threads_arg =
53  ARG_DEF("th", "threads", 1, "Number of threads to use");
54 static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "Source width");
55 static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "Source height");
56 static const arg_def_t timebase_arg =
57  ARG_DEF("t", "timebase", 1, "Timebase (num/den)");
58 static const arg_def_t bitrate_arg = ARG_DEF(
59  "b", "target-bitrate", 1, "Encoding bitrate, in kilobits per second");
60 static const arg_def_t spatial_layers_arg =
61  ARG_DEF("sl", "spatial-layers", 1, "Number of spatial SVC layers");
62 static const arg_def_t temporal_layers_arg =
63  ARG_DEF("tl", "temporal-layers", 1, "Number of temporal SVC layers");
64 static const arg_def_t layering_mode_arg =
65  ARG_DEF("lm", "layering-mode", 1, "Temporal layering scheme.");
66 static const arg_def_t kf_dist_arg =
67  ARG_DEF("k", "kf-dist", 1, "Number of frames between keyframes");
68 static const arg_def_t scale_factors_arg =
69  ARG_DEF("r", "scale-factors", 1, "Scale factors (lowest to highest layer)");
70 static const arg_def_t min_q_arg =
71  ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
72 static const arg_def_t max_q_arg =
73  ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
74 static const arg_def_t speed_arg =
75  ARG_DEF("sp", "speed", 1, "Speed configuration");
76 static const arg_def_t aqmode_arg =
77  ARG_DEF("aq", "aqmode", 1, "AQ mode off/on");
78 static const arg_def_t bitrates_arg =
79  ARG_DEF("bl", "bitrates", 1,
80  "Bitrates[spatial_layer * num_temporal_layer + temporal_layer]");
81 static const arg_def_t dropframe_thresh_arg =
82  ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
83 static const arg_def_t error_resilient_arg =
84  ARG_DEF(NULL, "error-resilient", 1, "Error resilient flag");
85 
86 #if CONFIG_AV1_HIGHBITDEPTH
87 static const struct arg_enum_list bitdepth_enum[] = {
88  { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
89 };
90 
91 static const arg_def_t bitdepth_arg = ARG_DEF_ENUM(
92  "d", "bit-depth", 1, "Bit depth for codec 8, 10 or 12. ", bitdepth_enum);
93 #endif // CONFIG_AV1_HIGHBITDEPTH
94 
95 static const arg_def_t *svc_args[] = {
96  &frames_arg, &outputfile, &width_arg,
97  &height_arg, &timebase_arg, &bitrate_arg,
98  &spatial_layers_arg, &kf_dist_arg, &scale_factors_arg,
99  &min_q_arg, &max_q_arg, &temporal_layers_arg,
100  &layering_mode_arg, &threads_arg, &aqmode_arg,
101 #if CONFIG_AV1_HIGHBITDEPTH
102  &bitdepth_arg,
103 #endif
104  &speed_arg, &bitrates_arg, &dropframe_thresh_arg,
105  &error_resilient_arg, NULL
106 };
107 
108 #define zero(Dest) memset(&(Dest), 0, sizeof(Dest));
109 
110 static const char *exec_name;
111 
112 void usage_exit(void) {
113  fprintf(stderr, "Usage: %s <options> input_filename -o output_filename\n",
114  exec_name);
115  fprintf(stderr, "Options:\n");
116  arg_show_usage(stderr, svc_args);
117  exit(EXIT_FAILURE);
118 }
119 
120 static int file_is_y4m(const char detect[4]) {
121  return memcmp(detect, "YUV4", 4) == 0;
122 }
123 
124 static int fourcc_is_ivf(const char detect[4]) {
125  if (memcmp(detect, "DKIF", 4) == 0) {
126  return 1;
127  }
128  return 0;
129 }
130 
131 static const int option_max_values[ALL_OPTION_TYPES] = { 63, INT_MAX, INT_MAX,
132  1 };
133 
134 static const int option_min_values[ALL_OPTION_TYPES] = { 0, 0, 1, 0 };
135 
136 static void open_input_file(struct AvxInputContext *input,
138  /* Parse certain options from the input file, if possible */
139  input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
140  : set_binary_mode(stdin);
141 
142  if (!input->file) fatal("Failed to open input file");
143 
144  if (!fseeko(input->file, 0, SEEK_END)) {
145  /* Input file is seekable. Figure out how long it is, so we can get
146  * progress info.
147  */
148  input->length = ftello(input->file);
149  rewind(input->file);
150  }
151 
152  /* Default to 1:1 pixel aspect ratio. */
153  input->pixel_aspect_ratio.numerator = 1;
154  input->pixel_aspect_ratio.denominator = 1;
155 
156  /* For RAW input sources, these bytes will applied on the first frame
157  * in read_frame().
158  */
159  input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
160  input->detect.position = 0;
161 
162  if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
163  if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
164  input->only_i420) >= 0) {
165  input->file_type = FILE_TYPE_Y4M;
166  input->width = input->y4m.pic_w;
167  input->height = input->y4m.pic_h;
168  input->pixel_aspect_ratio.numerator = input->y4m.par_n;
169  input->pixel_aspect_ratio.denominator = input->y4m.par_d;
170  input->framerate.numerator = input->y4m.fps_n;
171  input->framerate.denominator = input->y4m.fps_d;
172  input->fmt = input->y4m.aom_fmt;
173  input->bit_depth = input->y4m.bit_depth;
174  } else {
175  fatal("Unsupported Y4M stream.");
176  }
177  } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
178  fatal("IVF is not supported as input.");
179  } else {
180  input->file_type = FILE_TYPE_RAW;
181  }
182 }
183 
184 static aom_codec_err_t extract_option(LAYER_OPTION_TYPE type, char *input,
185  int *value0, int *value1) {
186  if (type == SCALE_FACTOR) {
187  *value0 = (int)strtol(input, &input, 10);
188  if (*input++ != '/') return AOM_CODEC_INVALID_PARAM;
189  *value1 = (int)strtol(input, &input, 10);
190 
191  if (*value0 < option_min_values[SCALE_FACTOR] ||
192  *value1 < option_min_values[SCALE_FACTOR] ||
193  *value0 > option_max_values[SCALE_FACTOR] ||
194  *value1 > option_max_values[SCALE_FACTOR] ||
195  *value0 > *value1) // num shouldn't be greater than den
197  } else {
198  *value0 = atoi(input);
199  if (*value0 < option_min_values[type] || *value0 > option_max_values[type])
201  }
202  return AOM_CODEC_OK;
203 }
204 
205 static aom_codec_err_t parse_layer_options_from_string(
206  aom_svc_params_t *svc_params, LAYER_OPTION_TYPE type, const char *input,
207  int *option0, int *option1) {
209  char *input_string;
210  char *token;
211  const char *delim = ",";
212  int num_layers = svc_params->number_spatial_layers;
213  int i = 0;
214 
215  if (type == BITRATE)
216  num_layers =
217  svc_params->number_spatial_layers * svc_params->number_temporal_layers;
218 
219  if (input == NULL || option0 == NULL ||
220  (option1 == NULL && type == SCALE_FACTOR))
222 
223  input_string = malloc(strlen(input));
224  memcpy(input_string, input, strlen(input));
225  if (input_string == NULL) return AOM_CODEC_MEM_ERROR;
226  token = strtok(input_string, delim); // NOLINT
227  for (i = 0; i < num_layers; ++i) {
228  if (token != NULL) {
229  res = extract_option(type, token, option0 + i, option1 + i);
230  if (res != AOM_CODEC_OK) break;
231  token = strtok(NULL, delim); // NOLINT
232  } else {
233  break;
234  }
235  }
236  if (res == AOM_CODEC_OK && i != num_layers) {
238  }
239  free(input_string);
240  return res;
241 }
242 
243 static void parse_command_line(int argc, const char **argv_,
244  AppInput *app_input,
245  aom_svc_params_t *svc_params,
246  aom_codec_enc_cfg_t *enc_cfg) {
247  struct arg arg;
248  char **argv = NULL;
249  char **argi = NULL;
250  char **argj = NULL;
251  char string_options[1024] = { 0 };
252 
253  // Default settings
254  svc_params->number_spatial_layers = 1;
255  svc_params->number_temporal_layers = 1;
256  app_input->layering_mode = 0;
257  enc_cfg->g_threads = 1;
258  enc_cfg->rc_end_usage = AOM_CBR;
259 
260  // process command line options
261  argv = argv_dup(argc - 1, argv_ + 1);
262  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
263  arg.argv_step = 1;
264 
265  if (arg_match(&arg, &outputfile, argi)) {
266  app_input->output_filename = arg.val;
267  } else if (arg_match(&arg, &width_arg, argi)) {
268  enc_cfg->g_w = arg_parse_uint(&arg);
269  } else if (arg_match(&arg, &height_arg, argi)) {
270  enc_cfg->g_h = arg_parse_uint(&arg);
271  } else if (arg_match(&arg, &timebase_arg, argi)) {
272  enc_cfg->g_timebase = arg_parse_rational(&arg);
273  } else if (arg_match(&arg, &bitrate_arg, argi)) {
274  enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
275  } else if (arg_match(&arg, &spatial_layers_arg, argi)) {
276  svc_params->number_spatial_layers = arg_parse_uint(&arg);
277  } else if (arg_match(&arg, &temporal_layers_arg, argi)) {
278  svc_params->number_temporal_layers = arg_parse_uint(&arg);
279  } else if (arg_match(&arg, &speed_arg, argi)) {
280  app_input->speed = arg_parse_uint(&arg);
281  if (app_input->speed > 9) {
282  warn("Mapping speed %d to speed 9.\n", app_input->speed);
283  }
284  } else if (arg_match(&arg, &aqmode_arg, argi)) {
285  app_input->aq_mode = arg_parse_uint(&arg);
286  } else if (arg_match(&arg, &threads_arg, argi)) {
287  enc_cfg->g_threads = arg_parse_uint(&arg);
288  } else if (arg_match(&arg, &layering_mode_arg, argi)) {
289  app_input->layering_mode = arg_parse_int(&arg);
290  } else if (arg_match(&arg, &kf_dist_arg, argi)) {
291  enc_cfg->kf_min_dist = arg_parse_uint(&arg);
292  enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
293  } else if (arg_match(&arg, &scale_factors_arg, argi)) {
294  parse_layer_options_from_string(svc_params, SCALE_FACTOR, arg.val,
295  svc_params->scaling_factor_num,
296  svc_params->scaling_factor_den);
297  } else if (arg_match(&arg, &min_q_arg, argi)) {
298  enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
299  } else if (arg_match(&arg, &max_q_arg, argi)) {
300  enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
301 #if CONFIG_AV1_HIGHBITDEPTH
302  } else if (arg_match(&arg, &bitdepth_arg, argi)) {
303  enc_cfg->g_bit_depth = arg_parse_enum_or_int(&arg);
304  switch (enc_cfg->g_bit_depth) {
305  case AOM_BITS_8:
306  enc_cfg->g_input_bit_depth = 8;
307  enc_cfg->g_profile = 0;
308  break;
309  case AOM_BITS_10:
310  enc_cfg->g_input_bit_depth = 10;
311  enc_cfg->g_profile = 2;
312  break;
313  case AOM_BITS_12:
314  enc_cfg->g_input_bit_depth = 12;
315  enc_cfg->g_profile = 2;
316  break;
317  default:
318  die("Error: Invalid bit depth selected (%d)\n", enc_cfg->g_bit_depth);
319  break;
320  }
321 #endif // CONFIG_VP9_HIGHBITDEPTH
322  } else if (arg_match(&arg, &dropframe_thresh_arg, argi)) {
323  enc_cfg->rc_dropframe_thresh = arg_parse_uint(&arg);
324  } else if (arg_match(&arg, &error_resilient_arg, argi)) {
325  enc_cfg->g_error_resilient = arg_parse_uint(&arg);
326  if (enc_cfg->g_error_resilient != 0 && enc_cfg->g_error_resilient != 1)
327  die("Invalid value for error resilient (0, 1): %d.",
328  enc_cfg->g_error_resilient);
329  } else {
330  ++argj;
331  }
332  }
333 
334  // Total bitrate needs to be parsed after the number of layers.
335  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
336  arg.argv_step = 1;
337  if (arg_match(&arg, &bitrates_arg, argi)) {
338  parse_layer_options_from_string(svc_params, BITRATE, arg.val,
339  svc_params->layer_target_bitrate, NULL);
340  } else {
341  ++argj;
342  }
343  }
344 
345  // There will be a space in front of the string options
346  if (strlen(string_options) > 0)
347  strncpy(app_input->options, string_options, OPTION_BUFFER_SIZE);
348 
349  // Check for unrecognized options
350  for (argi = argv; *argi; ++argi)
351  if (argi[0][0] == '-' && strlen(argi[0]) > 1)
352  die("Error: Unrecognized option %s\n", *argi);
353 
354  if (argv[0] == NULL) {
355  usage_exit();
356  }
357 
358  app_input->input_ctx.filename = argv[0];
359  free(argv);
360 
361  open_input_file(&app_input->input_ctx, 0);
362  if (app_input->input_ctx.file_type == FILE_TYPE_Y4M) {
363  enc_cfg->g_w = app_input->input_ctx.width;
364  enc_cfg->g_h = app_input->input_ctx.height;
365  }
366 
367  if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
368  enc_cfg->g_h % 2)
369  die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
370 
371  printf(
372  "Codec %s\n"
373  "layers: %d\n"
374  "width %u, height: %u\n"
375  "num: %d, den: %d, bitrate: %u\n"
376  "gop size: %u\n",
378  svc_params->number_spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
379  enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
380  enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
381 }
382 
383 static unsigned int mode_to_num_temporal_layers[10] = { 1, 2, 3, 3, 2,
384  1, 1, 3, 3, 3 };
385 static unsigned int mode_to_num_spatial_layers[10] = { 1, 1, 1, 1, 1,
386  2, 3, 3, 3, 3 };
387 
388 // For rate control encoding stats.
389 struct RateControlMetrics {
390  // Number of input frames per layer.
391  int layer_input_frames[AOM_MAX_TS_LAYERS];
392  // Number of encoded non-key frames per layer.
393  int layer_enc_frames[AOM_MAX_TS_LAYERS];
394  // Framerate per layer layer (cumulative).
395  double layer_framerate[AOM_MAX_TS_LAYERS];
396  // Target average frame size per layer (per-frame-bandwidth per layer).
397  double layer_pfb[AOM_MAX_LAYERS];
398  // Actual average frame size per layer.
399  double layer_avg_frame_size[AOM_MAX_LAYERS];
400  // Average rate mismatch per layer (|target - actual| / target).
401  double layer_avg_rate_mismatch[AOM_MAX_LAYERS];
402  // Actual encoding bitrate per layer (cumulative across temporal layers).
403  double layer_encoding_bitrate[AOM_MAX_LAYERS];
404  // Average of the short-time encoder actual bitrate.
405  // TODO(marpan): Should we add these short-time stats for each layer?
406  double avg_st_encoding_bitrate;
407  // Variance of the short-time encoder actual bitrate.
408  double variance_st_encoding_bitrate;
409  // Window (number of frames) for computing short-timee encoding bitrate.
410  int window_size;
411  // Number of window measurements.
412  int window_count;
413  int layer_target_bitrate[AOM_MAX_LAYERS];
414 };
415 
416 // Reference frames used in this example encoder.
417 enum {
418  SVC_LAST_FRAME = 0,
419  SVC_LAST2_FRAME,
420  SVC_LAST3_FRAME,
421  SVC_GOLDEN_FRAME,
422  SVC_BWDREF_FRAME,
423  SVC_ALTREF2_FRAME,
424  SVC_ALTREF_FRAME
425 };
426 
427 static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
428  FILE *f = input_ctx->file;
429  y4m_input *y4m = &input_ctx->y4m;
430  int shortread = 0;
431 
432  if (input_ctx->file_type == FILE_TYPE_Y4M) {
433  if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
434  } else {
435  shortread = read_yuv_frame(input_ctx, img);
436  }
437 
438  return !shortread;
439 }
440 
441 static void close_input_file(struct AvxInputContext *input) {
442  fclose(input->file);
443  if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
444 }
445 
446 // Note: these rate control metrics assume only 1 key frame in the
447 // sequence (i.e., first frame only). So for temporal pattern# 7
448 // (which has key frame for every frame on base layer), the metrics
449 // computation will be off/wrong.
450 // TODO(marpan): Update these metrics to account for multiple key frames
451 // in the stream.
452 static void set_rate_control_metrics(struct RateControlMetrics *rc,
453  double framerate,
454  unsigned int ss_number_layers,
455  unsigned int ts_number_layers) {
456  int ts_rate_decimator[AOM_MAX_TS_LAYERS] = { 1 };
457  ts_rate_decimator[0] = 1;
458  if (ts_number_layers == 2) {
459  ts_rate_decimator[0] = 2;
460  ts_rate_decimator[1] = 1;
461  }
462  if (ts_number_layers == 3) {
463  ts_rate_decimator[0] = 4;
464  ts_rate_decimator[1] = 2;
465  ts_rate_decimator[2] = 1;
466  }
467  // Set the layer (cumulative) framerate and the target layer (non-cumulative)
468  // per-frame-bandwidth, for the rate control encoding stats below.
469  for (unsigned int sl = 0; sl < ss_number_layers; ++sl) {
470  unsigned int i = sl * ts_number_layers;
471  rc->layer_framerate[0] = framerate / ts_rate_decimator[0];
472  rc->layer_pfb[i] =
473  1000.0 * rc->layer_target_bitrate[i] / rc->layer_framerate[0];
474  for (unsigned int tl = 0; tl < ts_number_layers; ++tl) {
475  i = sl * ts_number_layers + tl;
476  if (tl > 0) {
477  rc->layer_framerate[tl] = framerate / ts_rate_decimator[tl];
478  rc->layer_pfb[i] =
479  1000.0 *
480  (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
481  (rc->layer_framerate[tl] - rc->layer_framerate[tl - 1]);
482  }
483  rc->layer_input_frames[tl] = 0;
484  rc->layer_enc_frames[tl] = 0;
485  rc->layer_encoding_bitrate[i] = 0.0;
486  rc->layer_avg_frame_size[i] = 0.0;
487  rc->layer_avg_rate_mismatch[i] = 0.0;
488  }
489  }
490  rc->window_count = 0;
491  rc->window_size = 15;
492  rc->avg_st_encoding_bitrate = 0.0;
493  rc->variance_st_encoding_bitrate = 0.0;
494 }
495 
496 static void printout_rate_control_summary(struct RateControlMetrics *rc,
497  int frame_cnt,
498  unsigned int ss_number_layers,
499  unsigned int ts_number_layers) {
500  int tot_num_frames = 0;
501  double perc_fluctuation = 0.0;
502  printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
503  printf("Rate control layer stats for %u layer(s):\n\n", ts_number_layers);
504  for (unsigned int sl = 0; sl < ss_number_layers; ++sl) {
505  tot_num_frames = 0;
506  for (unsigned int tl = 0; tl < ts_number_layers; ++tl) {
507  unsigned int i = sl * ts_number_layers + tl;
508  const int num_dropped =
509  tl > 0 ? rc->layer_input_frames[tl] - rc->layer_enc_frames[tl]
510  : rc->layer_input_frames[tl] - rc->layer_enc_frames[tl] - 1;
511  tot_num_frames += rc->layer_input_frames[tl];
512  rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[tl] *
513  rc->layer_encoding_bitrate[i] /
514  tot_num_frames;
515  rc->layer_avg_frame_size[i] =
516  rc->layer_avg_frame_size[i] / rc->layer_enc_frames[tl];
517  rc->layer_avg_rate_mismatch[i] =
518  100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[tl];
519  printf("For layer#: %u %u \n", sl, tl);
520  printf("Bitrate (target vs actual): %d %f\n", rc->layer_target_bitrate[i],
521  rc->layer_encoding_bitrate[i]);
522  printf("Average frame size (target vs actual): %f %f\n", rc->layer_pfb[i],
523  rc->layer_avg_frame_size[i]);
524  printf("Average rate_mismatch: %f\n", rc->layer_avg_rate_mismatch[i]);
525  printf(
526  "Number of input frames, encoded (non-key) frames, "
527  "and perc dropped frames: %d %d %f\n",
528  rc->layer_input_frames[tl], rc->layer_enc_frames[tl],
529  100.0 * num_dropped / rc->layer_input_frames[tl]);
530  printf("\n");
531  }
532  }
533  rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
534  rc->variance_st_encoding_bitrate =
535  rc->variance_st_encoding_bitrate / rc->window_count -
536  (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
537  perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
538  rc->avg_st_encoding_bitrate;
539  printf("Short-time stats, for window of %d frames:\n", rc->window_size);
540  printf("Average, rms-variance, and percent-fluct: %f %f %f\n",
541  rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
542  perc_fluctuation);
543  if (frame_cnt - 1 != tot_num_frames)
544  die("Error: Number of input frames not equal to output!\n");
545 }
546 
547 // Layer pattern configuration.
548 static void set_layer_pattern(int layering_mode, int superframe_cnt,
549  aom_svc_layer_id_t *layer_id,
550  aom_svc_ref_frame_config_t *ref_frame_config,
551  int *use_svc_control, int spatial_layer_id,
552  int is_key_frame, int ksvc_mode) {
553  int i;
554  int enable_longterm_temporal_ref = 1;
555  int shift = (layering_mode == 7) ? 2 : 0;
556  *use_svc_control = 1;
557  layer_id->spatial_layer_id = spatial_layer_id;
558  int lag_index = 0;
559  int base_count = superframe_cnt >> 2;
560  // Set the referende map buffer idx for the 7 references:
561  // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
562  // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
563  for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->ref_idx[i] = i;
564  for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->reference[i] = 0;
565  for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
566 
567  if (ksvc_mode) {
568  // Same pattern as case 8.
569  layering_mode = 8;
570  if (!is_key_frame)
571  // No inter-layer prediction on inter-frames.
572  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
573  }
574  switch (layering_mode) {
575  case 0:
576  // 1-layer: update LAST on every frame, reference LAST.
577  layer_id->temporal_layer_id = 0;
578  ref_frame_config->refresh[0] = 1;
579  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
580  break;
581  case 1:
582  // 2-temporal layer.
583  // 1 3 5
584  // 0 2 4
585  if (superframe_cnt % 2 == 0) {
586  layer_id->temporal_layer_id = 0;
587  // Update LAST on layer 0, reference LAST.
588  ref_frame_config->refresh[0] = 1;
589  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
590  } else {
591  layer_id->temporal_layer_id = 1;
592  // No updates on layer 1, only reference LAST (TL0).
593  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
594  }
595  break;
596  case 2:
597  // 3-temporal layer:
598  // 1 3 5 7
599  // 2 6
600  // 0 4 8
601  if (superframe_cnt % 4 == 0) {
602  // Base layer.
603  layer_id->temporal_layer_id = 0;
604  // Update LAST on layer 0, reference LAST.
605  ref_frame_config->refresh[0] = 1;
606  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
607  } else if ((superframe_cnt - 1) % 4 == 0) {
608  layer_id->temporal_layer_id = 2;
609  // First top layer: no updates, only reference LAST (TL0).
610  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
611  } else if ((superframe_cnt - 2) % 4 == 0) {
612  layer_id->temporal_layer_id = 1;
613  // Middle layer (TL1): update LAST2, only reference LAST (TL0).
614  ref_frame_config->refresh[1] = 1;
615  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
616  } else if ((superframe_cnt - 3) % 4 == 0) {
617  layer_id->temporal_layer_id = 2;
618  // Second top layer: no updates, only reference LAST.
619  // Set buffer idx for LAST to slot 1, since that was the slot
620  // updated in previous frame. So LAST is TL1 frame.
621  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
622  ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
623  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
624  }
625  break;
626  case 3:
627  // 3 TL, same as above, except allow for predicting
628  // off 2 more references (GOLDEN and ALTREF), with
629  // GOLDEN updated periodically, and ALTREF lagging from
630  // LAST from ~4 frames. Both GOLDEN and ALTREF
631  // can only be updated on base temporal layer.
632 
633  // Keep golden fixed at slot 3.
634  ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
635  // Cyclically refresh slots 4, 5, 6, 7, for lag altref.
636  lag_index = 4 + (base_count % 4);
637  // Set the altref slot to lag_index.
638  ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = lag_index;
639  if (superframe_cnt % 4 == 0) {
640  // Base layer.
641  layer_id->temporal_layer_id = 0;
642  // Update LAST on layer 0, reference LAST.
643  ref_frame_config->refresh[0] = 1;
644  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
645  // Refresh GOLDEN every x ~10 base layer frames.
646  if (base_count % 10 == 0) ref_frame_config->refresh[3] = 1;
647  // Refresh lag_index slot, needed for lagging altref.
648  ref_frame_config->refresh[lag_index] = 1;
649  } else if ((superframe_cnt - 1) % 4 == 0) {
650  layer_id->temporal_layer_id = 2;
651  // First top layer: no updates, only reference LAST (TL0).
652  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
653  } else if ((superframe_cnt - 2) % 4 == 0) {
654  layer_id->temporal_layer_id = 1;
655  // Middle layer (TL1): update LAST2, only reference LAST (TL0).
656  ref_frame_config->refresh[1] = 1;
657  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
658  } else if ((superframe_cnt - 3) % 4 == 0) {
659  layer_id->temporal_layer_id = 2;
660  // Second top layer: no updates, only reference LAST.
661  // Set buffer idx for LAST to slot 1, since that was the slot
662  // updated in previous frame. So LAST is TL1 frame.
663  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
664  ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
665  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
666  }
667  // Every frame can reference GOLDEN AND ALTREF.
668  ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
669  ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
670  break;
671  case 4:
672  // 3-temporal layer: but middle layer updates GF, so 2nd TL2 will
673  // only reference GF (not LAST). Other frames only reference LAST.
674  // 1 3 5 7
675  // 2 6
676  // 0 4 8
677  if (superframe_cnt % 4 == 0) {
678  // Base layer.
679  layer_id->temporal_layer_id = 0;
680  // Update LAST on layer 0, only reference LAST.
681  ref_frame_config->refresh[0] = 1;
682  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
683  } else if ((superframe_cnt - 1) % 4 == 0) {
684  layer_id->temporal_layer_id = 2;
685  // First top layer: no updates, only reference LAST (TL0).
686  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
687  } else if ((superframe_cnt - 2) % 4 == 0) {
688  layer_id->temporal_layer_id = 1;
689  // Middle layer (TL1): update GF, only reference LAST (TL0).
690  ref_frame_config->refresh[3] = 1;
691  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
692  } else if ((superframe_cnt - 3) % 4 == 0) {
693  layer_id->temporal_layer_id = 2;
694  // Second top layer: no updates, only reference GF.
695  ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
696  }
697  break;
698  case 5:
699  // 2 spatial layers, 1 temporal.
700  layer_id->temporal_layer_id = 0;
701  if (layer_id->spatial_layer_id == 0) {
702  // Reference LAST, update LAST.
703  ref_frame_config->refresh[0] = 1;
704  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
705  } else if (layer_id->spatial_layer_id == 1) {
706  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
707  // and GOLDEN to slot 0. Update slot 1 (LAST).
708  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
709  ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 0;
710  ref_frame_config->refresh[1] = 1;
711  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
712  ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
713  }
714  break;
715  case 6:
716  // 3 spatial layers, 1 temporal.
717  // Note for this case, we set the buffer idx for all references to be
718  // either LAST or GOLDEN, which are always valid references, since decoder
719  // will check if any of the 7 references is valid scale in
720  // valid_ref_frame_size().
721  layer_id->temporal_layer_id = 0;
722  if (layer_id->spatial_layer_id == 0) {
723  // Reference LAST, update LAST. Set all buffer_idx to 0.
724  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
725  ref_frame_config->ref_idx[i] = 0;
726  ref_frame_config->refresh[0] = 1;
727  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
728  } else if (layer_id->spatial_layer_id == 1) {
729  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
730  // and GOLDEN (and all other refs) to slot 0.
731  // Update slot 1 (LAST).
732  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
733  ref_frame_config->ref_idx[i] = 0;
734  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
735  ref_frame_config->refresh[1] = 1;
736  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
737  ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
738  } else if (layer_id->spatial_layer_id == 2) {
739  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2
740  // and GOLDEN (and all other refs) to slot 1.
741  // Update slot 2 (LAST).
742  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
743  ref_frame_config->ref_idx[i] = 1;
744  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
745  ref_frame_config->refresh[2] = 1;
746  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
747  ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
748  // For 3 spatial layer case: allow for top spatial layer to use
749  // additional temporal reference. Update every 10 frames.
750  if (enable_longterm_temporal_ref) {
751  ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
752  ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
753  if (base_count % 10 == 0)
754  ref_frame_config->refresh[REF_FRAMES - 1] = 1;
755  }
756  }
757  break;
758  case 7:
759  // 3 spatial and 3 temporal layer.
760  // Same as case 8 but overalap in the buffer slot updates.
761  // (shift = 2). The slots 3 and 4 updated by first TL2 are
762  // reused for update in TL1 superframe.
763  // Note for this case, frame order hint must be disabled for
764  // lower resolutios (operating points > 0) to be decoedable.
765  case 8:
766  // 3 spatial and 3 temporal layer.
767  // No overlap in buffer updates between TL2 and TL1.
768  // TL2 updates slot 3 and 4, TL1 updates 5, 6, 7.
769  // Set the references via the svc_ref_frame_config control.
770  // Always reference LAST.
771  ref_frame_config->reference[SVC_LAST_FRAME] = 1;
772  if (superframe_cnt % 4 == 0) {
773  // Base temporal layer.
774  layer_id->temporal_layer_id = 0;
775  if (layer_id->spatial_layer_id == 0) {
776  // Reference LAST, update LAST.
777  // Set all buffer_idx to 0.
778  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
779  ref_frame_config->ref_idx[i] = 0;
780  ref_frame_config->refresh[0] = 1;
781  } else if (layer_id->spatial_layer_id == 1) {
782  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
783  // GOLDEN (and all other refs) to slot 0.
784  // Update slot 1 (LAST).
785  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
786  ref_frame_config->ref_idx[i] = 0;
787  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
788  ref_frame_config->refresh[1] = 1;
789  } else if (layer_id->spatial_layer_id == 2) {
790  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
791  // GOLDEN (and all other refs) to slot 1.
792  // Update slot 2 (LAST).
793  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
794  ref_frame_config->ref_idx[i] = 1;
795  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
796  ref_frame_config->refresh[2] = 1;
797  }
798  } else if ((superframe_cnt - 1) % 4 == 0) {
799  // First top temporal enhancement layer.
800  layer_id->temporal_layer_id = 2;
801  if (layer_id->spatial_layer_id == 0) {
802  // Reference LAST (slot 0).
803  // Set GOLDEN to slot 3 and update slot 3.
804  // Set all other buffer_idx to slot 0.
805  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
806  ref_frame_config->ref_idx[i] = 0;
807  ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
808  ref_frame_config->refresh[3] = 1;
809  } else if (layer_id->spatial_layer_id == 1) {
810  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
811  // GOLDEN (and all other refs) to slot 3.
812  // Set LAST2 to slot 4 and Update slot 4.
813  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
814  ref_frame_config->ref_idx[i] = 3;
815  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
816  ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
817  ref_frame_config->refresh[4] = 1;
818  } else if (layer_id->spatial_layer_id == 2) {
819  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
820  // GOLDEN (and all other refs) to slot 4.
821  // No update.
822  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
823  ref_frame_config->ref_idx[i] = 4;
824  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
825  }
826  } else if ((superframe_cnt - 2) % 4 == 0) {
827  // Middle temporal enhancement layer.
828  layer_id->temporal_layer_id = 1;
829  if (layer_id->spatial_layer_id == 0) {
830  // Reference LAST.
831  // Set all buffer_idx to 0.
832  // Set GOLDEN to slot 5 and update slot 5.
833  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
834  ref_frame_config->ref_idx[i] = 0;
835  ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
836  ref_frame_config->refresh[5 - shift] = 1;
837  } else if (layer_id->spatial_layer_id == 1) {
838  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
839  // GOLDEN (and all other refs) to slot 5.
840  // Set LAST3 to slot 6 and update slot 6.
841  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
842  ref_frame_config->ref_idx[i] = 5 - shift;
843  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
844  ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
845  ref_frame_config->refresh[6 - shift] = 1;
846  } else if (layer_id->spatial_layer_id == 2) {
847  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
848  // GOLDEN (and all other refs) to slot 6.
849  // Set LAST3 to slot 7 and update slot 7.
850  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
851  ref_frame_config->ref_idx[i] = 6 - shift;
852  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
853  ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 7 - shift;
854  ref_frame_config->refresh[7 - shift] = 1;
855  }
856  } else if ((superframe_cnt - 3) % 4 == 0) {
857  // Second top temporal enhancement layer.
858  layer_id->temporal_layer_id = 2;
859  if (layer_id->spatial_layer_id == 0) {
860  // Set LAST to slot 5 and reference LAST.
861  // Set GOLDEN to slot 3 and update slot 3.
862  // Set all other buffer_idx to 0.
863  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
864  ref_frame_config->ref_idx[i] = 0;
865  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
866  ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
867  ref_frame_config->refresh[3] = 1;
868  } else if (layer_id->spatial_layer_id == 1) {
869  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
870  // GOLDEN to slot 3. Set LAST2 to slot 4 and update slot 4.
871  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
872  ref_frame_config->ref_idx[i] = 0;
873  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
874  ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
875  ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
876  ref_frame_config->refresh[4] = 1;
877  } else if (layer_id->spatial_layer_id == 2) {
878  // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 7,
879  // GOLDEN to slot 4. No update.
880  for (i = 0; i < INTER_REFS_PER_FRAME; i++)
881  ref_frame_config->ref_idx[i] = 0;
882  ref_frame_config->ref_idx[SVC_LAST_FRAME] = 7 - shift;
883  ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 4;
884  }
885  }
886  if (layer_id->spatial_layer_id > 0 && !ksvc_mode)
887  // Reference GOLDEN.
888  ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
889  // For 3 spatial layer case 7 (where there is free buffer slot):
890  // allow for top spatial layer to use additional temporal reference.
891  // Additional reference is only updated on base temporal layer, every
892  // 10 TL0 frames here.
893  if (enable_longterm_temporal_ref && layer_id->spatial_layer_id == 2 &&
894  layering_mode == 7) {
895  ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
896  ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
897  if (base_count % 10 == 0 && layer_id->temporal_layer_id == 0)
898  ref_frame_config->refresh[REF_FRAMES - 1] = 1;
899  }
900  break;
901  default: assert(0); die("Error: Unsupported temporal layering mode!\n");
902  }
903 }
904 
905 int main(int argc, const char **argv) {
906  AppInput app_input;
907  AvxVideoWriter *outfile[AOM_MAX_LAYERS] = { NULL };
908  AvxVideoWriter *total_layer_file = NULL;
910  int frame_cnt = 0;
911  aom_image_t raw;
912  int frame_avail;
913  int got_data = 0;
914  int flags = 0;
915  unsigned i;
916  int pts = 0; // PTS starts at 0.
917  int frame_duration = 1; // 1 timebase tick per frame.
918  aom_svc_layer_id_t layer_id;
919  aom_svc_params_t svc_params;
920  aom_svc_ref_frame_config_t ref_frame_config;
921 
922  struct RateControlMetrics rc;
923  int64_t cx_time = 0;
924  int64_t cx_time_sl[3]; // max number of spatial layers.
925  double sum_bitrate = 0.0;
926  double sum_bitrate2 = 0.0;
927  double framerate = 30.0;
928  int use_svc_control = 1;
929  int set_err_resil_frame = 0;
930  zero(rc.layer_target_bitrate);
931  memset(&layer_id, 0, sizeof(aom_svc_layer_id_t));
932  memset(&app_input, 0, sizeof(AppInput));
933  memset(&svc_params, 0, sizeof(svc_params));
934 
935  // Flag to test dynamic scaling of source frames for single
936  // spatial stream, using the scaling_mode control.
937  const int test_dynamic_scaling_single_layer = 0;
938 
939  /* Setup default input stream settings */
940  app_input.input_ctx.framerate.numerator = 30;
941  app_input.input_ctx.framerate.denominator = 1;
942  app_input.input_ctx.only_i420 = 1;
943  app_input.input_ctx.bit_depth = 0;
944  exec_name = argv[0];
945 
946  // start with default encoder configuration
947  aom_codec_err_t res =
949  if (res) {
950  die("Failed to get config: %s\n", aom_codec_err_to_string(res));
951  }
952 
953  // Real time parameters.
955 
956  cfg.rc_end_usage = AOM_CBR;
957  cfg.rc_min_quantizer = 2;
958  cfg.rc_max_quantizer = 52;
959  cfg.rc_undershoot_pct = 50;
960  cfg.rc_overshoot_pct = 50;
961  cfg.rc_buf_initial_sz = 600;
962  cfg.rc_buf_optimal_sz = 600;
963  cfg.rc_buf_sz = 1000;
964  cfg.rc_resize_mode = 0; // Set to RESIZE_DYNAMIC for dynamic resize.
965  cfg.g_lag_in_frames = 0;
966  cfg.kf_mode = AOM_KF_AUTO;
967 
968  parse_command_line(argc, argv, &app_input, &svc_params, &cfg);
969 
970  unsigned int ts_number_layers = svc_params.number_temporal_layers;
971  unsigned int ss_number_layers = svc_params.number_spatial_layers;
972 
973  unsigned int width = cfg.g_w;
974  unsigned int height = cfg.g_h;
975 
976  if (ts_number_layers !=
977  mode_to_num_temporal_layers[app_input.layering_mode] ||
978  ss_number_layers != mode_to_num_spatial_layers[app_input.layering_mode]) {
979  die("Number of layers doesn't match layering mode.");
980  }
981 
982  // Y4M reader has its own allocation.
983  if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
984  if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, width, height, 32)) {
985  die("Failed to allocate image", width, height);
986  }
987  }
988 
989  aom_codec_iface_t *encoder = get_aom_encoder_by_short_name("av1");
990 
991  memcpy(&rc.layer_target_bitrate[0], &svc_params.layer_target_bitrate[0],
992  sizeof(svc_params.layer_target_bitrate));
993 
994  unsigned int total_rate = 0;
995  for (i = 0; i < ss_number_layers; i++) {
996  total_rate +=
997  svc_params
998  .layer_target_bitrate[i * ts_number_layers + ts_number_layers - 1];
999  }
1000  if (total_rate != cfg.rc_target_bitrate) {
1001  die("Incorrect total target bitrate");
1002  }
1003 
1004  svc_params.framerate_factor[0] = 1;
1005  if (ts_number_layers == 2) {
1006  svc_params.framerate_factor[0] = 2;
1007  svc_params.framerate_factor[1] = 1;
1008  } else if (ts_number_layers == 3) {
1009  svc_params.framerate_factor[0] = 4;
1010  svc_params.framerate_factor[1] = 2;
1011  svc_params.framerate_factor[2] = 1;
1012  }
1013 
1014  framerate = cfg.g_timebase.den / cfg.g_timebase.num;
1015  set_rate_control_metrics(&rc, framerate, ss_number_layers, ts_number_layers);
1016 
1017  if (app_input.input_ctx.file_type == FILE_TYPE_Y4M) {
1018  if (app_input.input_ctx.width != cfg.g_w ||
1019  app_input.input_ctx.height != cfg.g_h) {
1020  die("Incorrect width or height: %d x %d", cfg.g_w, cfg.g_h);
1021  }
1022  if (app_input.input_ctx.framerate.numerator != cfg.g_timebase.den ||
1023  app_input.input_ctx.framerate.denominator != cfg.g_timebase.num) {
1024  die("Incorrect framerate: numerator %d denominator %d",
1025  cfg.g_timebase.num, cfg.g_timebase.den);
1026  }
1027  }
1028 
1029  AvxVideoInfo info;
1030  info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
1031  info.frame_width = cfg.g_w;
1032  info.frame_height = cfg.g_h;
1033  info.time_base.numerator = cfg.g_timebase.num;
1034  info.time_base.denominator = cfg.g_timebase.den;
1035  // Open an output file for each stream.
1036  for (unsigned int sl = 0; sl < ss_number_layers; ++sl) {
1037  for (unsigned tl = 0; tl < ts_number_layers; ++tl) {
1038  i = sl * ts_number_layers + tl;
1039  char file_name[PATH_MAX];
1040 
1041  snprintf(file_name, sizeof(file_name), "%s_%u.av1",
1042  app_input.output_filename, i);
1043  outfile[i] = aom_video_writer_open(file_name, kContainerIVF, &info);
1044  if (!outfile[i]) die("Failed to open %s for writing", file_name);
1045  }
1046  }
1047  total_layer_file =
1048  aom_video_writer_open(app_input.output_filename, kContainerIVF, &info);
1049  if (!total_layer_file)
1050  die("Failed to open %s for writing", app_input.output_filename);
1051 
1052  // Initialize codec.
1053  aom_codec_ctx_t codec;
1054  if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
1055  die("Failed to initialize encoder");
1056 
1057  aom_codec_control(&codec, AOME_SET_CPUUSED, app_input.speed);
1058  aom_codec_control(&codec, AV1E_SET_AQ_MODE, app_input.aq_mode ? 3 : 0);
1069  cfg.g_threads ? get_msb(cfg.g_threads) : 0);
1070  if (cfg.g_threads > 1) aom_codec_control(&codec, AV1E_SET_ROW_MT, 1);
1071 
1072  svc_params.number_spatial_layers = ss_number_layers;
1073  svc_params.number_temporal_layers = ts_number_layers;
1074  for (i = 0; i < ss_number_layers * ts_number_layers; ++i) {
1075  svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
1076  svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
1077  }
1078  for (i = 0; i < ss_number_layers; ++i) {
1079  svc_params.scaling_factor_num[i] = 1;
1080  svc_params.scaling_factor_den[i] = 1;
1081  }
1082  if (ss_number_layers == 2) {
1083  svc_params.scaling_factor_num[0] = 1;
1084  svc_params.scaling_factor_den[0] = 2;
1085  } else if (ss_number_layers == 3) {
1086  svc_params.scaling_factor_num[0] = 1;
1087  svc_params.scaling_factor_den[0] = 4;
1088  svc_params.scaling_factor_num[1] = 1;
1089  svc_params.scaling_factor_den[1] = 2;
1090  }
1091 
1092  aom_codec_control(&codec, AV1E_SET_SVC_PARAMS, &svc_params);
1093 
1094  // This controls the maximum target size of the key frame.
1095  // For generating smaller key frames, use a smaller max_intra_size_pct
1096  // value, like 100 or 200.
1097  {
1098  const int max_intra_size_pct = 300;
1100  max_intra_size_pct);
1101  }
1102 
1103  for (unsigned int slx = 0; slx < ss_number_layers; slx++) cx_time_sl[slx] = 0;
1104  frame_avail = 1;
1105  while (frame_avail || got_data) {
1106  struct aom_usec_timer timer;
1107  frame_avail = read_frame(&(app_input.input_ctx), &raw);
1108  int is_key_frame = (frame_cnt % cfg.kf_max_dist) == 0;
1109  // Loop over spatial layers.
1110  for (unsigned int slx = 0; slx < ss_number_layers; slx++) {
1111  aom_codec_iter_t iter = NULL;
1112  const aom_codec_cx_pkt_t *pkt;
1113  int layer = 0;
1114 
1115  // Set the reference/update flags, layer_id, and reference_map
1116  // buffer index.
1117  set_layer_pattern(app_input.layering_mode, frame_cnt, &layer_id,
1118  &ref_frame_config, &use_svc_control, slx, is_key_frame,
1119  (app_input.layering_mode == 9));
1120  aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
1121  if (use_svc_control)
1123  &ref_frame_config);
1124  if (set_err_resil_frame) {
1125  // Set error_resilient per frame: off/0 for base layer and
1126  // on/1 for enhancement layer frames.
1127  int err_resil_mode =
1128  (layer_id.spatial_layer_id > 0 || layer_id.temporal_layer_id > 0);
1130  err_resil_mode);
1131  }
1132 
1133  layer = slx * ts_number_layers + layer_id.temporal_layer_id;
1134  if (frame_avail && slx == 0) ++rc.layer_input_frames[layer];
1135 
1136  if (test_dynamic_scaling_single_layer) {
1137  if (frame_cnt >= 200 && frame_cnt <= 400) {
1138  // Scale source down by 2x2.
1139  struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
1140  aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
1141  } else {
1142  // Source back up to original resolution (no scaling).
1143  struct aom_scaling_mode mode = { AOME_NORMAL, AOME_NORMAL };
1144  aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
1145  }
1146  }
1147 
1148  // Do the layer encode.
1149  aom_usec_timer_start(&timer);
1150  if (aom_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags))
1151  die_codec(&codec, "Failed to encode frame");
1152  aom_usec_timer_mark(&timer);
1153  cx_time += aom_usec_timer_elapsed(&timer);
1154  cx_time_sl[slx] += aom_usec_timer_elapsed(&timer);
1155 
1156  got_data = 0;
1157  while ((pkt = aom_codec_get_cx_data(&codec, &iter))) {
1158  got_data = 1;
1159  switch (pkt->kind) {
1161  for (unsigned int sl = layer_id.spatial_layer_id;
1162  sl < ss_number_layers; ++sl) {
1163  for (unsigned tl = layer_id.temporal_layer_id;
1164  tl < ts_number_layers; ++tl) {
1165  unsigned int j = sl * ts_number_layers + tl;
1166  aom_video_writer_write_frame(outfile[j], pkt->data.frame.buf,
1167  pkt->data.frame.sz, pts);
1168  if (sl == (unsigned int)layer_id.spatial_layer_id)
1169  rc.layer_encoding_bitrate[j] += 8.0 * pkt->data.frame.sz;
1170  }
1171  }
1172  // Write everything into the top layer.
1173  aom_video_writer_write_frame(total_layer_file, pkt->data.frame.buf,
1174  pkt->data.frame.sz, pts);
1175  // Keep count of rate control stats per layer (for non-key).
1176  if (!(pkt->data.frame.flags & AOM_FRAME_IS_KEY)) {
1177  unsigned int j = layer_id.spatial_layer_id * ts_number_layers +
1178  layer_id.temporal_layer_id;
1179  rc.layer_avg_frame_size[j] += 8.0 * pkt->data.frame.sz;
1180  rc.layer_avg_rate_mismatch[j] +=
1181  fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[j]) /
1182  rc.layer_pfb[j];
1183  if (slx == 0) ++rc.layer_enc_frames[layer_id.temporal_layer_id];
1184  }
1185 
1186  // Update for short-time encoding bitrate states, for moving window
1187  // of size rc->window, shifted by rc->window / 2.
1188  // Ignore first window segment, due to key frame.
1189  // For spatial layers: only do this for top/highest SL.
1190  if (frame_cnt > rc.window_size && slx == ss_number_layers - 1) {
1191  sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
1192  rc.window_size = (rc.window_size <= 0) ? 1 : rc.window_size;
1193  if (frame_cnt % rc.window_size == 0) {
1194  rc.window_count += 1;
1195  rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
1196  rc.variance_st_encoding_bitrate +=
1197  (sum_bitrate / rc.window_size) *
1198  (sum_bitrate / rc.window_size);
1199  sum_bitrate = 0.0;
1200  }
1201  }
1202  // Second shifted window.
1203  if (frame_cnt > rc.window_size + rc.window_size / 2 &&
1204  slx == ss_number_layers - 1) {
1205  sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
1206  if (frame_cnt > 2 * rc.window_size &&
1207  frame_cnt % rc.window_size == 0) {
1208  rc.window_count += 1;
1209  rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
1210  rc.variance_st_encoding_bitrate +=
1211  (sum_bitrate2 / rc.window_size) *
1212  (sum_bitrate2 / rc.window_size);
1213  sum_bitrate2 = 0.0;
1214  }
1215  }
1216  break;
1217  default: break;
1218  }
1219  }
1220  } // loop over spatial layers
1221  ++frame_cnt;
1222  pts += frame_duration;
1223  }
1224  close_input_file(&(app_input.input_ctx));
1225  printout_rate_control_summary(&rc, frame_cnt, ss_number_layers,
1226  ts_number_layers);
1227  printf("\n");
1228  printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f\n",
1229  frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
1230  1000000 * (double)frame_cnt / (double)cx_time);
1231 
1232  if (ss_number_layers > 1) {
1233  printf("Per spatial layer: \n");
1234  for (unsigned int slx = 0; slx < ss_number_layers; slx++)
1235  printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f\n",
1236  frame_cnt, (float)cx_time_sl[slx] / (double)(frame_cnt * 1000),
1237  1000000 * (double)frame_cnt / (double)cx_time_sl[slx]);
1238  }
1239 
1240  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
1241 
1242  // Try to rewrite the output file headers with the actual frame count.
1243  for (i = 0; i < ss_number_layers * ts_number_layers; ++i)
1244  aom_video_writer_close(outfile[i]);
1245  aom_video_writer_close(total_layer_file);
1246 
1247  if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
1248  aom_img_free(&raw);
1249  }
1250  return EXIT_SUCCESS;
1251 }
Describes the encoder algorithm interface to applications.
enum aom_chroma_sample_position aom_chroma_sample_position_t
List of chroma sample positions.
aom_image_t * aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
@ AOM_IMG_FMT_I420
Definition: aom_image.h:45
void aom_img_free(aom_image_t *img)
Close an image descriptor.
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
Declares top-level encoder structures and functions.
#define AOM_MAX_LAYERS
Definition: aomcx.h:1417
aom_codec_iface_t * aom_codec_av1_cx(void)
The interface to the AV1 encoder.
#define AOM_MAX_TS_LAYERS
Definition: aomcx.h:1419
@ AV1E_SET_ROW_MT
Codec control function to enable the row based multi-threading of the encoder, unsigned int parameter...
Definition: aomcx.h:348
@ AV1E_SET_ENABLE_TPL_MODEL
Codec control function to enable RDO modulated by frame temporal dependency, unsigned int parameter.
Definition: aomcx.h:395
@ AV1E_SET_AQ_MODE
Codec control function to set adaptive quantization mode, unsigned int parameter.
Definition: aomcx.h:455
@ AV1E_SET_SVC_LAYER_ID
Codec control function to set the layer id, aom_svc_layer_id_t* parameter.
Definition: aomcx.h:1256
@ AV1E_SET_SVC_REF_FRAME_CONFIG
Codec control function to set reference frame config: the ref_idx and the refresh flags for each buff...
Definition: aomcx.h:1267
@ AV1E_SET_CDF_UPDATE_MODE
Codec control function to set CDF update mode, unsigned int parameter.
Definition: aomcx.h:493
@ AV1E_SET_MV_COST_UPD_FREQ
Control to set frequency of the cost updates for motion vectors, unsigned int parameter.
Definition: aomcx.h:1234
@ AV1E_SET_COEFF_COST_UPD_FREQ
Control to set frequency of the cost updates for coefficients, unsigned int parameter.
Definition: aomcx.h:1214
@ AV1E_SET_ENABLE_CDEF
Codec control function to encode with CDEF, unsigned int parameter.
Definition: aomcx.h:652
@ AV1E_SET_SVC_PARAMS
Codec control function to set SVC paramaeters, aom_svc_params_t* parameter.
Definition: aomcx.h:1261
@ AOME_SET_MAX_INTRA_BITRATE_PCT
Codec control function to set max data rate for intra frames, unsigned int parameter.
Definition: aomcx.h:293
@ AV1E_SET_ERROR_RESILIENT_MODE
Codec control function to enable error_resilient_mode, int parameter.
Definition: aomcx.h:429
@ AOME_SET_SCALEMODE
Codec control function to set encoder scaling mode, aom_scaling_mode_t* parameter.
Definition: aomcx.h:195
@ AV1E_SET_TILE_COLUMNS
Codec control function to set number of tile columns. unsigned int parameter.
Definition: aomcx.h:367
@ AV1E_SET_ENABLE_ORDER_HINT
Codec control function to turn on / off frame order hint (int parameter). Affects: joint compound mod...
Definition: aomcx.h:847
@ AV1E_SET_DELTAQ_MODE
Codec control function to set the delta q mode, unsigned int parameter.
Definition: aomcx.h:1111
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings, int parameter.
Definition: aomcx.h:213
@ AV1E_SET_GF_CBR_BOOST_PCT
Boost percentage for Golden Frame in CBR mode, unsigned int parameter.
Definition: aomcx.h:326
@ AV1E_SET_MODE_COST_UPD_FREQ
Control to set frequency of the cost updates for mode, unsigned int parameter.
Definition: aomcx.h:1224
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition: aom_codec.h:254
const char * aom_codec_err_to_string(aom_codec_err_t err)
Convert error number to printable string.
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:155
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:288
#define AOM_FRAME_IS_KEY
Definition: aom_codec.h:271
@ AOM_BITS_12
Definition: aom_codec.h:321
@ AOM_BITS_8
Definition: aom_codec.h:319
@ AOM_BITS_10
Definition: aom_codec.h:320
@ AOM_CODEC_INVALID_PARAM
An application-supplied parameter is not valid.
Definition: aom_codec.h:200
@ AOM_CODEC_MEM_ERROR
Memory operation failed.
Definition: aom_codec.h:163
@ AOM_CODEC_OK
Operation completed without error.
Definition: aom_codec.h:157
aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned long duration, aom_enc_frame_flags_t flags)
Encode a frame.
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition: aom_encoder.h:931
aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface, aom_codec_enc_cfg_t *cfg, unsigned int usage)
Get the default configuration for a usage.
#define AOM_USAGE_REALTIME
usage parameter analogous to AV1 REALTIME mode.
Definition: aom_encoder.h:1004
const aom_codec_cx_pkt_t * aom_codec_get_cx_data(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Encoded data iterator.
@ AOM_CBR
Definition: aom_encoder.h:167
@ AOM_KF_AUTO
Definition: aom_encoder.h:182
@ AOM_CODEC_CX_FRAME_PKT
Definition: aom_encoder.h:98
Codec context structure.
Definition: aom_codec.h:298
Encoder output packet.
Definition: aom_encoder.h:110
enum aom_codec_cx_pkt_kind kind
Definition: aom_encoder.h:111
union aom_codec_cx_pkt::@1 data
struct aom_codec_cx_pkt::@1::@2 frame
Encoder configuration structure.
Definition: aom_encoder.h:367
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition: aom_encoder.h:450
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: aom_encoder.h:515
struct aom_rational g_timebase
Stream timebase units.
Definition: aom_encoder.h:464
unsigned int g_usage
Algorithm specific "usage" value.
Definition: aom_encoder.h:379
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: aom_encoder.h:679
unsigned int g_h
Height of the frame.
Definition: aom_encoder.h:415
enum aom_kf_mode kf_mode
Keyframe placement mode.
Definition: aom_encoder.h:742
enum aom_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: aom_encoder.h:598
unsigned int g_threads
Maximum number of threads to use.
Definition: aom_encoder.h:387
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: aom_encoder.h:751
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: aom_encoder.h:493
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: aom_encoder.h:688
unsigned int g_profile
Bitstream profile to use.
Definition: aom_encoder.h:397
aom_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: aom_encoder.h:442
unsigned int g_w
Width of the frame.
Definition: aom_encoder.h:406
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: aom_encoder.h:655
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: aom_encoder.h:760
aom_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: aom_encoder.h:472
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition: aom_encoder.h:642
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: aom_encoder.h:697
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: aom_encoder.h:632
unsigned int rc_target_bitrate
Target data rate.
Definition: aom_encoder.h:618
unsigned int rc_resize_mode
Mode for spatial resampling, if supported by the codec.
Definition: aom_encoder.h:524
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: aom_encoder.h:664
Image Descriptor.
Definition: aom_image.h:171
int num
Definition: aom_encoder.h:153
int den
Definition: aom_encoder.h:154
aom image scaling mode
Definition: aomcx.h:1382
Definition: aomcx.h:1422
int temporal_layer_id
Definition: aomcx.h:1424
int spatial_layer_id
Definition: aomcx.h:1423
Definition: aomcx.h:1428
int max_quantizers[32]
Definition: aomcx.h:1431
int number_spatial_layers
Definition: aomcx.h:1429
int layer_target_bitrate[32]
Definition: aomcx.h:1436
int framerate_factor[8]
Definition: aomcx.h:1438
int min_quantizers[32]
Definition: aomcx.h:1432
int scaling_factor_den[4]
Definition: aomcx.h:1434
int number_temporal_layers
Definition: aomcx.h:1430
int scaling_factor_num[4]
Definition: aomcx.h:1433
Definition: aomcx.h:1442
int reference[7]
Definition: aomcx.h:1445
int refresh[8]
Definition: aomcx.h:1448
int ref_idx[7]
Definition: aomcx.h:1447