AOMedia AV1 Codec: svc_encoder_rtc

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