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