AOMedia Codec SDK
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 "common/tools_common.h"
24 #include "common/video_writer.h"
25 #include "aom_ports/aom_timer.h"
26 
27 #define zero(Dest) memset(&(Dest), 0, sizeof(Dest));
28 
29 static const char *exec_name;
30 
31 void usage_exit(void) { exit(EXIT_FAILURE); }
32 
33 static int mode_to_num_layers[4] = { 1, 2, 3, 3 };
34 
35 // For rate control encoding stats.
36 struct RateControlMetrics {
37  // Number of input frames per layer.
38  int layer_input_frames[AOM_MAX_TS_LAYERS];
39  // Total (cumulative) number of encoded frames per layer.
40  int layer_tot_enc_frames[AOM_MAX_TS_LAYERS];
41  // Number of encoded non-key frames per layer.
42  int layer_enc_frames[AOM_MAX_TS_LAYERS];
43  // Framerate per layer layer (cumulative).
44  double layer_framerate[AOM_MAX_TS_LAYERS];
45  // Target average frame size per layer (per-frame-bandwidth per layer).
46  double layer_pfb[AOM_MAX_TS_LAYERS];
47  // Actual average frame size per layer.
48  double layer_avg_frame_size[AOM_MAX_TS_LAYERS];
49  // Average rate mismatch per layer (|target - actual| / target).
50  double layer_avg_rate_mismatch[AOM_MAX_TS_LAYERS];
51  // Actual encoding bitrate per layer (cumulative).
52  double layer_encoding_bitrate[AOM_MAX_TS_LAYERS];
53  // Average of the short-time encoder actual bitrate.
54  // TODO(marpan): Should we add these short-time stats for each layer?
55  double avg_st_encoding_bitrate;
56  // Variance of the short-time encoder actual bitrate.
57  double variance_st_encoding_bitrate;
58  // Window (number of frames) for computing short-timee encoding bitrate.
59  int window_size;
60  // Number of window measurements.
61  int window_count;
62  int layer_target_bitrate[AOM_MAX_TS_LAYERS];
63 };
64 
65 static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
66  FILE *f = input_ctx->file;
67  y4m_input *y4m = &input_ctx->y4m;
68  int shortread = 0;
69 
70  if (input_ctx->file_type == FILE_TYPE_Y4M) {
71  if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
72  } else {
73  shortread = read_yuv_frame(input_ctx, img);
74  }
75 
76  return !shortread;
77 }
78 
79 static int file_is_y4m(const char detect[4]) {
80  if (memcmp(detect, "YUV4", 4) == 0) {
81  return 1;
82  }
83  return 0;
84 }
85 
86 static int fourcc_is_ivf(const char detect[4]) {
87  if (memcmp(detect, "DKIF", 4) == 0) {
88  return 1;
89  }
90  return 0;
91 }
92 
93 static void close_input_file(struct AvxInputContext *input) {
94  fclose(input->file);
95  if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
96 }
97 
98 static void open_input_file(struct AvxInputContext *input,
100  /* Parse certain options from the input file, if possible */
101  input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
102  : set_binary_mode(stdin);
103 
104  if (!input->file) fatal("Failed to open input file");
105 
106  if (!fseeko(input->file, 0, SEEK_END)) {
107  /* Input file is seekable. Figure out how long it is, so we can get
108  * progress info.
109  */
110  input->length = ftello(input->file);
111  rewind(input->file);
112  }
113 
114  /* Default to 1:1 pixel aspect ratio. */
115  input->pixel_aspect_ratio.numerator = 1;
116  input->pixel_aspect_ratio.denominator = 1;
117 
118  /* For RAW input sources, these bytes will applied on the first frame
119  * in read_frame().
120  */
121  input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
122  input->detect.position = 0;
123 
124  if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
125  if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
126  input->only_i420) >= 0) {
127  input->file_type = FILE_TYPE_Y4M;
128  input->width = input->y4m.pic_w;
129  input->height = input->y4m.pic_h;
130  input->pixel_aspect_ratio.numerator = input->y4m.par_n;
131  input->pixel_aspect_ratio.denominator = input->y4m.par_d;
132  input->framerate.numerator = input->y4m.fps_n;
133  input->framerate.denominator = input->y4m.fps_d;
134  input->fmt = input->y4m.aom_fmt;
135  input->bit_depth = input->y4m.bit_depth;
136  } else {
137  fatal("Unsupported Y4M stream.");
138  }
139  } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
140  fatal("IVF is not supported as input.");
141  } else {
142  input->file_type = FILE_TYPE_RAW;
143  }
144 }
145 
146 // Note: these rate control metrics assume only 1 key frame in the
147 // sequence (i.e., first frame only). So for temporal pattern# 7
148 // (which has key frame for every frame on base layer), the metrics
149 // computation will be off/wrong.
150 // TODO(marpan): Update these metrics to account for multiple key frames
151 // in the stream.
152 static void set_rate_control_metrics(struct RateControlMetrics *rc,
153  double framerate,
154  unsigned int ts_number_layers) {
155  int ts_rate_decimator[AOM_MAX_TS_LAYERS] = { 1 };
156  ts_rate_decimator[0] = 1;
157  if (ts_number_layers == 2) {
158  ts_rate_decimator[0] = 2;
159  ts_rate_decimator[1] = 1;
160  }
161  if (ts_number_layers == 3) {
162  ts_rate_decimator[0] = 4;
163  ts_rate_decimator[1] = 2;
164  ts_rate_decimator[2] = 1;
165  }
166  // Set the layer (cumulative) framerate and the target layer (non-cumulative)
167  // per-frame-bandwidth, for the rate control encoding stats below.
168  rc->layer_framerate[0] = framerate / ts_rate_decimator[0];
169  rc->layer_pfb[0] =
170  1000.0 * rc->layer_target_bitrate[0] / rc->layer_framerate[0];
171  for (unsigned int i = 0; i < ts_number_layers; ++i) {
172  if (i > 0) {
173  rc->layer_framerate[i] = framerate / ts_rate_decimator[i];
174  rc->layer_pfb[i] =
175  1000.0 *
176  (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
177  (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
178  }
179  rc->layer_input_frames[i] = 0;
180  rc->layer_enc_frames[i] = 0;
181  rc->layer_tot_enc_frames[i] = 0;
182  rc->layer_encoding_bitrate[i] = 0.0;
183  rc->layer_avg_frame_size[i] = 0.0;
184  rc->layer_avg_rate_mismatch[i] = 0.0;
185  }
186  rc->window_count = 0;
187  rc->window_size = 15;
188  rc->avg_st_encoding_bitrate = 0.0;
189  rc->variance_st_encoding_bitrate = 0.0;
190 }
191 
192 static void printout_rate_control_summary(struct RateControlMetrics *rc,
193  int frame_cnt,
194  unsigned int ts_number_layers) {
195  int tot_num_frames = 0;
196  double perc_fluctuation = 0.0;
197  printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
198  printf("Rate control layer stats for %d layer(s):\n\n", ts_number_layers);
199  for (unsigned int i = 0; i < ts_number_layers; ++i) {
200  const int num_dropped =
201  i > 0 ? rc->layer_input_frames[i] - rc->layer_enc_frames[i]
202  : rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1;
203  tot_num_frames += rc->layer_input_frames[i];
204  rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
205  rc->layer_encoding_bitrate[i] /
206  tot_num_frames;
207  rc->layer_avg_frame_size[i] =
208  rc->layer_avg_frame_size[i] / rc->layer_enc_frames[i];
209  rc->layer_avg_rate_mismatch[i] =
210  100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[i];
211  printf("For layer#: %d\n", i);
212  printf("Bitrate (target vs actual): %d %f\n", rc->layer_target_bitrate[i],
213  rc->layer_encoding_bitrate[i]);
214  printf("Average frame size (target vs actual): %f %f\n", rc->layer_pfb[i],
215  rc->layer_avg_frame_size[i]);
216  printf("Average rate_mismatch: %f\n", rc->layer_avg_rate_mismatch[i]);
217  printf(
218  "Number of input frames, encoded (non-key) frames, "
219  "and perc dropped frames: %d %d %f\n",
220  rc->layer_input_frames[i], rc->layer_enc_frames[i],
221  100.0 * num_dropped / rc->layer_input_frames[i]);
222  printf("\n");
223  }
224  rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
225  rc->variance_st_encoding_bitrate =
226  rc->variance_st_encoding_bitrate / rc->window_count -
227  (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
228  perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
229  rc->avg_st_encoding_bitrate;
230  printf("Short-time stats, for window of %d frames:\n", rc->window_size);
231  printf("Average, rms-variance, and percent-fluct: %f %f %f\n",
232  rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
233  perc_fluctuation);
234  if (frame_cnt - 1 != tot_num_frames)
235  die("Error: Number of input frames not equal to output!\n");
236 }
237 
238 // Layer pattern configuration.
239 static int set_layer_pattern(int layering_mode, int frame_cnt,
240  aom_svc_layer_id_t *layer_id,
241  aom_svc_ref_frame_config_t *ref_frame_config) {
242  int i;
243  // No spatial layers in this test.
244  layer_id->spatial_layer_id = 0;
245  // Set the referende map buffer idx for the 7 references:
246  // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
247  // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
248  for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->ref_idx[i] = i;
249  for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
250  // Note only use LAST and GF for prediction in non-rd mode (speed 8).
251  int layer_flags = AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
254  switch (layering_mode) {
255  case 0:
256  // 1-layer: update LAST on every frame, reference LAST and GF.
257  layer_id->temporal_layer_id = 0;
258  ref_frame_config->refresh[0] = 1;
259  break;
260  case 1:
261  // 2-layer.
262  // 1 3 5
263  // 0 2 4
264  if (frame_cnt % 2 == 0) {
265  layer_id->temporal_layer_id = 0;
266  // Update LAST on layer 0, reference LAST and GF.
267  ref_frame_config->refresh[0] = 1;
268  } else {
269  layer_id->temporal_layer_id = 1;
270  // No updates on layer 1, only reference LAST (TL0).
271  layer_flags |= AOM_EFLAG_NO_REF_GF;
272  }
273  break;
274  case 2:
275  // 3-layer:
276  // 1 3 5 7
277  // 2 6
278  // 0 4 8
279  if (frame_cnt % 4 == 0) {
280  // Base layer.
281  layer_id->temporal_layer_id = 0;
282  // Update LAST on layer 0, reference LAST and GF.
283  ref_frame_config->refresh[0] = 1;
284  } else if ((frame_cnt - 1) % 4 == 0) {
285  layer_id->temporal_layer_id = 2;
286  // First top layer: no updates, only reference LAST (TL0).
287  layer_flags |= AOM_EFLAG_NO_REF_GF;
288  } else if ((frame_cnt - 2) % 4 == 0) {
289  layer_id->temporal_layer_id = 1;
290  // Middle layer (TL1): update LAST2, only reference LAST (TL0).
291  ref_frame_config->refresh[1] = 1;
292  layer_flags |= AOM_EFLAG_NO_REF_GF;
293  } else if ((frame_cnt - 3) % 4 == 0) {
294  layer_id->temporal_layer_id = 2;
295  // Second top layer: no updates, only reference LAST.
296  // Set buffer idx for LAST to slot 1, since that was the slot
297  // updated in previous frame. So LAST is TL1 frame.
298  ref_frame_config->ref_idx[0] = 1;
299  ref_frame_config->ref_idx[1] = 0;
300  layer_flags |= AOM_EFLAG_NO_REF_GF;
301  }
302  break;
303  case 3:
304  // 3-layer: but middle layer updates GF, so 2nd TL2 will only
305  // reference GF (not LAST). Other frames only reference LAST.
306  // 1 3 5 7
307  // 2 6
308  // 0 4 8
309  if (frame_cnt % 4 == 0) {
310  // Base layer.
311  layer_id->temporal_layer_id = 0;
312  // Update LAST on layer 0, only reference LAST.
313  ref_frame_config->refresh[0] = 1;
314  layer_flags |= AOM_EFLAG_NO_REF_GF;
315  } else if ((frame_cnt - 1) % 4 == 0) {
316  layer_id->temporal_layer_id = 2;
317  // First top layer: no updates, only reference LAST (TL0).
318  layer_flags |= AOM_EFLAG_NO_REF_GF;
319  } else if ((frame_cnt - 2) % 4 == 0) {
320  layer_id->temporal_layer_id = 1;
321  // Middle layer (TL1): update GF, only reference LAST (TL0).
322  ref_frame_config->refresh[3] = 1;
323  layer_flags |= AOM_EFLAG_NO_REF_GF;
324  } else if ((frame_cnt - 3) % 4 == 0) {
325  layer_id->temporal_layer_id = 2;
326  // Second top layer: no updates, only reference GF.
327  layer_flags |= AOM_EFLAG_NO_REF_LAST;
328  }
329  break;
330  default: assert(0); die("Error: Unsupported temporal layering mode!\n");
331  }
332  return layer_flags;
333 }
334 
335 int main(int argc, char **argv) {
336  AvxVideoWriter *outfile[AOM_MAX_TS_LAYERS] = { NULL };
337  aom_codec_ctx_t codec;
339  int frame_cnt = 0;
340  aom_image_t raw;
341  aom_codec_err_t res;
342  unsigned int width;
343  unsigned int height;
344  uint32_t error_resilient = 0;
345  int speed;
346  int frame_avail;
347  int got_data;
348  int flags = 0;
349  unsigned int i;
350  int pts = 0; // PTS starts at 0.
351  int frame_duration = 1; // 1 timebase tick per frame.
352  int layering_mode = 0;
353  aom_svc_layer_id_t layer_id;
354  aom_svc_params_t svc_params;
355  aom_svc_ref_frame_config_t ref_frame_config;
356  const AvxInterface *encoder = NULL;
357  struct AvxInputContext input_ctx;
358  struct RateControlMetrics rc;
359  int64_t cx_time = 0;
360  const int min_args_base = 13;
361  const int min_args = min_args_base;
362  double sum_bitrate = 0.0;
363  double sum_bitrate2 = 0.0;
364  double framerate = 30.0;
365  zero(rc.layer_target_bitrate);
366  memset(&layer_id, 0, sizeof(aom_svc_layer_id_t));
367  memset(&input_ctx, 0, sizeof(input_ctx));
368  memset(&svc_params, 0, sizeof(svc_params));
369 
370  /* Setup default input stream settings */
371  input_ctx.framerate.numerator = 30;
372  input_ctx.framerate.denominator = 1;
373  input_ctx.only_i420 = 1;
374  input_ctx.bit_depth = 0;
375  unsigned int ts_number_layers = 1;
376  unsigned int ss_number_layers = 1;
377  exec_name = argv[0];
378  // Check usage and arguments.
379  if (argc < min_args) {
380  die("Usage: %s <infile> <outfile> <codec_type(av1)> <width> <height> "
381  "<rate_num> <rate_den> <speed> <frame_drop_threshold> "
382  "<error_resilient> <threads> <mode> "
383  "<Rate_0> ... <Rate_nlayers-1>\n",
384  argv[0]);
385  }
386 
387  encoder = get_aom_encoder_by_name(argv[3]);
388 
389  width = (unsigned int)strtoul(argv[4], NULL, 0);
390  height = (unsigned int)strtoul(argv[5], NULL, 0);
391  if (width < 16 || width % 2 || height < 16 || height % 2) {
392  die("Invalid resolution: %d x %d", width, height);
393  }
394 
395  layering_mode = (int)strtol(argv[12], NULL, 0);
396  if (layering_mode < 0 || layering_mode > 13) {
397  die("Invalid layering mode (0..12) %s", argv[12]);
398  }
399 
400  if (argc != min_args + mode_to_num_layers[layering_mode]) {
401  die("Invalid number of arguments");
402  }
403 
404  ts_number_layers = mode_to_num_layers[layering_mode];
405 
406  input_ctx.filename = argv[1];
407  open_input_file(&input_ctx, 0);
408 
409  // Y4M reader has its own allocation.
410  if (input_ctx.file_type != FILE_TYPE_Y4M) {
411  if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, width, height, 32)) {
412  die("Failed to allocate image", width, height);
413  }
414  }
415 
416  // Populate encoder configuration.
417  res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
418  if (res) {
419  printf("Failed to get config: %s\n", aom_codec_err_to_string(res));
420  return EXIT_FAILURE;
421  }
422 
423  // Update the default configuration with our settings.
424  cfg.g_w = width;
425  cfg.g_h = height;
426 
427  // Timebase format e.g. 30fps: numerator=1, demoninator = 30.
428  cfg.g_timebase.num = (int)strtol(argv[6], NULL, 0);
429  cfg.g_timebase.den = (int)strtol(argv[7], NULL, 0);
430 
431  speed = (int)strtol(argv[8], NULL, 0);
432  if (speed < 0 || speed > 8) {
433  die("Invalid speed setting: must be positive");
434  }
435 
436  for (i = min_args_base;
437  (int)i < min_args_base + mode_to_num_layers[layering_mode]; ++i) {
438  rc.layer_target_bitrate[i - 13] = (int)strtol(argv[i], NULL, 0);
439  svc_params.layer_target_bitrate[i - 13] = rc.layer_target_bitrate[i - 13];
440  }
441 
442  cfg.rc_target_bitrate = svc_params.layer_target_bitrate[ts_number_layers - 1];
443 
444  svc_params.framerate_factor[0] = 1;
445  if (ts_number_layers == 2) {
446  svc_params.framerate_factor[0] = 2;
447  svc_params.framerate_factor[1] = 1;
448  } else if (ts_number_layers == 3) {
449  svc_params.framerate_factor[0] = 4;
450  svc_params.framerate_factor[1] = 2;
451  svc_params.framerate_factor[2] = 1;
452  }
453 
454  // Real time parameters.
456 
457  cfg.rc_dropframe_thresh = (unsigned int)strtoul(argv[9], NULL, 0);
458  cfg.rc_end_usage = AOM_CBR;
459  cfg.rc_min_quantizer = 2;
460  cfg.rc_max_quantizer = 52;
461  cfg.rc_undershoot_pct = 50;
462  cfg.rc_overshoot_pct = 50;
463  cfg.rc_buf_initial_sz = 600;
464  cfg.rc_buf_optimal_sz = 600;
465  cfg.rc_buf_sz = 1000;
466 
467  // Use 1 thread as default.
468  cfg.g_threads = (unsigned int)strtoul(argv[11], NULL, 0);
469 
470  error_resilient = (uint32_t)strtoul(argv[10], NULL, 0);
471  if (error_resilient != 0 && error_resilient != 1) {
472  die("Invalid value for error resilient (0, 1): %d.", error_resilient);
473  }
474  // Enable error resilient mode.
475  cfg.g_error_resilient = error_resilient;
476  cfg.g_lag_in_frames = 0;
477  cfg.kf_mode = AOM_KF_AUTO;
478 
479  // Disable automatic keyframe placement.
480  cfg.kf_min_dist = cfg.kf_max_dist = 3000;
481 
482  framerate = cfg.g_timebase.den / cfg.g_timebase.num;
483  set_rate_control_metrics(&rc, framerate, ts_number_layers);
484 
485  if (input_ctx.file_type == FILE_TYPE_Y4M) {
486  if (input_ctx.width != cfg.g_w || input_ctx.height != cfg.g_h) {
487  die("Incorrect width or height: %d x %d", cfg.g_w, cfg.g_h);
488  }
489  if (input_ctx.framerate.numerator != cfg.g_timebase.den ||
490  input_ctx.framerate.denominator != cfg.g_timebase.num) {
491  die("Incorrect framerate: numerator %d denominator %d",
492  cfg.g_timebase.num, cfg.g_timebase.den);
493  }
494  }
495 
496  // Open an output file for each stream.
497  for (i = 0; i < ts_number_layers; ++i) {
498  char file_name[PATH_MAX];
499  AvxVideoInfo info;
500  info.codec_fourcc = encoder->fourcc;
501  info.frame_width = cfg.g_w;
502  info.frame_height = cfg.g_h;
503  info.time_base.numerator = cfg.g_timebase.num;
504  info.time_base.denominator = cfg.g_timebase.den;
505 
506  snprintf(file_name, sizeof(file_name), "%s_%d.av1", argv[2], i);
507  outfile[i] = aom_video_writer_open(file_name, kContainerIVF, &info);
508  if (!outfile[i]) die("Failed to open %s for writing", file_name);
509 
510  assert(outfile[i] != NULL);
511  }
512 
513  // Initialize codec.
514  if (aom_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
515  die_codec(&codec, "Failed to initialize encoder");
516 
517  aom_codec_control(&codec, AOME_SET_CPUUSED, speed);
521 
522  svc_params.number_spatial_layers = ss_number_layers;
523  svc_params.number_temporal_layers = ts_number_layers;
524  for (i = 0; i < ts_number_layers; ++i) {
525  svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
526  svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
527  }
528  for (i = 0; i < ss_number_layers; ++i) {
529  svc_params.scaling_factor_num[i] = 1;
530  svc_params.scaling_factor_den[i] = 1;
531  }
532  aom_codec_control(&codec, AV1E_SET_SVC_PARAMS, &svc_params);
533 
534  // This controls the maximum target size of the key frame.
535  // For generating smaller key frames, use a smaller max_intra_size_pct
536  // value, like 100 or 200.
537  {
538  const int max_intra_size_pct = 300;
540  max_intra_size_pct);
541  }
542 
543  frame_avail = 1;
544  while (frame_avail || got_data) {
545  struct aom_usec_timer timer;
546  aom_codec_iter_t iter = NULL;
547  const aom_codec_cx_pkt_t *pkt;
548 
549  // Set the reference/update flags, layer_id, and reference_map
550  // buffer index.
551  flags = set_layer_pattern(layering_mode, frame_cnt, &layer_id,
552  &ref_frame_config);
553  aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
554  aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_CONFIG, &ref_frame_config);
555 
556  frame_avail = read_frame(&input_ctx, &raw);
557  if (frame_avail) ++rc.layer_input_frames[layer_id.temporal_layer_id];
558  aom_usec_timer_start(&timer);
559  if (aom_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags)) {
560  die_codec(&codec, "Failed to encode frame");
561  }
562  aom_usec_timer_mark(&timer);
563  cx_time += aom_usec_timer_elapsed(&timer);
564  got_data = 0;
565  while ((pkt = aom_codec_get_cx_data(&codec, &iter))) {
566  got_data = 1;
567  switch (pkt->kind) {
569  for (i = layer_id.temporal_layer_id; i < ts_number_layers; ++i) {
570  aom_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
571  pkt->data.frame.sz, pts);
572  ++rc.layer_tot_enc_frames[i];
573  rc.layer_encoding_bitrate[i] += 8.0 * pkt->data.frame.sz;
574  // Keep count of rate control stats per layer (for non-key frames).
575  if (i == (unsigned int)layer_id.temporal_layer_id &&
576  !(pkt->data.frame.flags & AOM_FRAME_IS_KEY)) {
577  rc.layer_avg_frame_size[i] += 8.0 * pkt->data.frame.sz;
578  rc.layer_avg_rate_mismatch[i] +=
579  fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[i]) /
580  rc.layer_pfb[i];
581  ++rc.layer_enc_frames[i];
582  }
583  }
584  // Update for short-time encoding bitrate states, for moving window
585  // of size rc->window, shifted by rc->window / 2.
586  // Ignore first window segment, due to key frame.
587  if (frame_cnt > rc.window_size) {
588  sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
589  rc.window_size = (rc.window_size <= 0) ? 1 : rc.window_size;
590  if (frame_cnt % rc.window_size == 0) {
591  rc.window_count += 1;
592  rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
593  rc.variance_st_encoding_bitrate +=
594  (sum_bitrate / rc.window_size) *
595  (sum_bitrate / rc.window_size);
596  sum_bitrate = 0.0;
597  }
598  }
599  // Second shifted window.
600  if (frame_cnt > rc.window_size + rc.window_size / 2) {
601  sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
602  if (frame_cnt > 2 * rc.window_size &&
603  frame_cnt % rc.window_size == 0) {
604  rc.window_count += 1;
605  rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
606  rc.variance_st_encoding_bitrate +=
607  (sum_bitrate2 / rc.window_size) *
608  (sum_bitrate2 / rc.window_size);
609  sum_bitrate2 = 0.0;
610  }
611  }
612  break;
613  default: break;
614  }
615  }
616  ++frame_cnt;
617  pts += frame_duration;
618  }
619  close_input_file(&input_ctx);
620  printout_rate_control_summary(&rc, frame_cnt, ts_number_layers);
621  printf("\n");
622  printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f\n",
623  frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
624  1000000 * (double)frame_cnt / (double)cx_time);
625 
626  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
627 
628  // Try to rewrite the output file headers with the actual frame count.
629  for (i = 0; i < ts_number_layers; ++i) aom_video_writer_close(outfile[i]);
630 
631  if (input_ctx.file_type != FILE_TYPE_Y4M) {
632  aom_img_free(&raw);
633  }
634  return EXIT_SUCCESS;
635 }
AOM_IMG_FMT_I420
@ AOM_IMG_FMT_I420
Definition: aom_image.h:45
AOM_USAGE_REALTIME
#define AOM_USAGE_REALTIME
usage parameter analogous to AV1 REALTIME mode.
Definition: aom_encoder.h:867
aom_codec_enc_cfg::rc_dropframe_thresh
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: aom_encoder.h:376
aom_codec_enc_cfg
Encoder configuration structure.
Definition: aom_encoder.h:228
AOM_EFLAG_NO_REF_LAST
#define AOM_EFLAG_NO_REF_LAST
Don't reference the last frame.
Definition: aomcx.h:50
AOME_SET_CPUUSED
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings.
Definition: aomcx.h:182
aom_svc_params::framerate_factor
int framerate_factor[8]
Definition: aomcx.h:1254
AOM_EFLAG_NO_REF_BWD
#define AOM_EFLAG_NO_REF_BWD
Don't reference the bwd reference frame.
Definition: aomcx.h:86
aom_svc_params::number_spatial_layers
int number_spatial_layers
Definition: aomcx.h:1245
aom_chroma_sample_position_t
enum aom_chroma_sample_position aom_chroma_sample_position_t
List of chroma sample positions.
AV1E_SET_AQ_MODE
@ AV1E_SET_AQ_MODE
Codec control function to set adaptive quantization mode.
Definition: aomcx.h:372
aom_codec_enc_cfg::g_lag_in_frames
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: aom_encoder.h:354
AV1E_SET_GF_CBR_BOOST_PCT
@ AV1E_SET_GF_CBR_BOOST_PCT
Boost percentage for Golden Frame in CBR mode.
Definition: aomcx.h:270
AOM_EFLAG_NO_REF_LAST3
#define AOM_EFLAG_NO_REF_LAST3
Don't reference the last3 frame.
Definition: aomcx.h:64
aom_svc_ref_frame_config::refresh
int refresh[8]
Definition: aomcx.h:1263
aom_codec_ctx
Codec context structure.
Definition: aom_codec.h:204
aom_codec_enc_cfg::rc_buf_sz
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: aom_encoder.h:548
aom_codec_iter_t
const typedef void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
aomcx.h
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
aom_codec_err_to_string
const char * aom_codec_err_to_string(aom_codec_err_t err)
Convert error number to printable string.
aom_codec_enc_cfg::rc_target_bitrate
unsigned int rc_target_bitrate
Target data rate.
Definition: aom_encoder.h:481
aom_img_free
void aom_img_free(aom_image_t *img)
Close an image descriptor.
aom_svc_params::max_quantizers
int max_quantizers[32]
Definition: aomcx.h:1247
aom_codec_enc_cfg::rc_min_quantizer
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: aom_encoder.h:495
AOM_CBR
@ AOM_CBR
Definition: aom_encoder.h:193
aom_rational::den
int den
Definition: aom_encoder.h:180
aom_codec_enc_cfg::rc_undershoot_pct
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: aom_encoder.h:521
aom_svc_params::number_temporal_layers
int number_temporal_layers
Definition: aomcx.h:1246
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_svc_layer_id
Definition: aomcx.h:1238
aom_img_alloc
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_codec_control
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:414
aom_codec_enc_cfg::rc_overshoot_pct
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: aom_encoder.h:533
aom_codec_enc_cfg::kf_mode
enum aom_kf_mode kf_mode
Keyframe placement mode.
Definition: aom_encoder.h:611
aom_codec_enc_cfg::g_usage
unsigned int g_usage
Algorithm specific "usage" value.
Definition: aom_encoder.h:240
AV1E_SET_SVC_LAYER_ID
@ AV1E_SET_SVC_LAYER_ID
Codec control function to set the layer id.
Definition: aomcx.h:1134
aom_svc_ref_frame_config
Definition: aomcx.h:1258
aom_codec_enc_cfg::rc_end_usage
enum aom_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: aom_encoder.h:461
aom_svc_params::scaling_factor_num
int scaling_factor_num[4]
Definition: aomcx.h:1249
aom_svc_params::layer_target_bitrate
int layer_target_bitrate[32]
Definition: aomcx.h:1252
aom_svc_params::min_quantizers
int min_quantizers[32]
Definition: aomcx.h:1248
aom_codec_enc_cfg::g_w
unsigned int g_w
Width of the frame.
Definition: aom_encoder.h:267
AOM_MAX_TS_LAYERS
#define AOM_MAX_TS_LAYERS
Definition: aomcx.h:1235
aom_svc_params
Definition: aomcx.h:1244
aom_codec_enc_cfg::g_error_resilient
aom_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: aom_encoder.h:333
aom_codec_err_t
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
AOM_EFLAG_NO_REF_GF
#define AOM_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: aomcx.h:71
aom_encoder.h
Describes the encoder algorithm interface to applications.
aom_codec_get_cx_data
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_svc_params::scaling_factor_den
int scaling_factor_den[4]
Definition: aomcx.h:1250
aom_codec_encode
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.
aom_codec_enc_cfg::rc_buf_initial_sz
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: aom_encoder.h:557
AOME_SET_MAX_INTRA_BITRATE_PCT
@ AOME_SET_MAX_INTRA_BITRATE_PCT
Codec control function to set Max data rate for Intra frames.
Definition: aomcx.h:240
aom_codec_enc_cfg::rc_max_quantizer
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition: aom_encoder.h:505
aom_codec_cx_pkt::data
union aom_codec_cx_pkt::@1 data
AV1E_SET_ENABLE_CDEF
@ AV1E_SET_ENABLE_CDEF
Codec control function to encode with CDEF.
Definition: aomcx.h:563
aom_codec_cx_pkt::frame
struct aom_codec_cx_pkt::@1::@2 frame
aom_codec_cx_pkt
Encoder output packet.
Definition: aom_encoder.h:138
AV1E_SET_SVC_PARAMS
@ AV1E_SET_SVC_PARAMS
Codec control function to set SVC paramaeters.
Definition: aomcx.h:1138
AV1E_SET_SVC_REF_FRAME_CONFIG
@ 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:1143
AOM_EFLAG_NO_REF_LAST2
#define AOM_EFLAG_NO_REF_LAST2
Don't reference the last2 frame.
Definition: aomcx.h:57
AOM_KF_AUTO
@ AOM_KF_AUTO
Definition: aom_encoder.h:208
aom_codec_enc_cfg::g_h
unsigned int g_h
Height of the frame.
Definition: aom_encoder.h:276
aom_svc_layer_id::spatial_layer_id
int spatial_layer_id
Definition: aomcx.h:1239
AOM_CODEC_CX_FRAME_PKT
@ AOM_CODEC_CX_FRAME_PKT
Definition: aom_encoder.h:126
AOM_EFLAG_NO_REF_ARF2
#define AOM_EFLAG_NO_REF_ARF2
Don't reference the alt2 reference frame.
Definition: aomcx.h:93
aom_codec_enc_init
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition: aom_encoder.h:764
aom_image
Image Descriptor.
Definition: aom_image.h:141
aom_codec_cx_pkt::kind
enum aom_codec_cx_pkt_kind kind
Definition: aom_encoder.h:139
aom_codec_enc_cfg::g_timebase
struct aom_rational g_timebase
Stream timebase units.
Definition: aom_encoder.h:325
aom_codec_enc_cfg::g_threads
unsigned int g_threads
Maximum number of threads to use.
Definition: aom_encoder.h:248
AOM_FRAME_IS_KEY
#define AOM_FRAME_IS_KEY
Definition: aom_encoder.h:96
aom_svc_ref_frame_config::ref_idx
int ref_idx[7]
Definition: aomcx.h:1262
aom_codec_enc_cfg::rc_buf_optimal_sz
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: aom_encoder.h:566
aom_codec_enc_cfg::kf_max_dist
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: aom_encoder.h:629
AOM_EFLAG_NO_REF_ARF
#define AOM_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: aomcx.h:79
aom_codec_enc_config_default
aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface, aom_codec_enc_cfg_t *cfg, unsigned int reserved)
Get a default configuration.
aom_svc_layer_id::temporal_layer_id
int temporal_layer_id
Definition: aomcx.h:1240
aom_codec_enc_cfg::kf_min_dist
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: aom_encoder.h:620
aom_rational::num
int num
Definition: aom_encoder.h:179