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