AOMedia AV1 Codec
aomenc
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "apps/aomenc.h"
13 
14 #include "config/aom_config.h"
15 
16 #include <assert.h>
17 #include <limits.h>
18 #include <math.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #if CONFIG_AV1_DECODER
25 #include "aom/aom_decoder.h"
26 #include "aom/aomdx.h"
27 #endif
28 
29 #include "aom/aom_encoder.h"
30 #include "aom/aom_integer.h"
31 #include "aom/aomcx.h"
32 #include "aom_dsp/aom_dsp_common.h"
33 #include "aom_ports/aom_timer.h"
34 #include "aom_ports/mem_ops.h"
35 #include "common/args.h"
36 #include "common/ivfenc.h"
37 #include "common/tools_common.h"
38 #include "common/warnings.h"
39 
40 #if CONFIG_WEBM_IO
41 #include "common/webmenc.h"
42 #endif
43 
44 #include "common/y4minput.h"
45 #include "examples/encoder_util.h"
46 #include "stats/aomstats.h"
47 #include "stats/rate_hist.h"
48 
49 #if CONFIG_LIBYUV
50 #include "third_party/libyuv/include/libyuv/scale.h"
51 #endif
52 
53 /* Swallow warnings about unused results of fread/fwrite */
54 static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
55  return fread(ptr, size, nmemb, stream);
56 }
57 #define fread wrap_fread
58 
59 static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
60  FILE *stream) {
61  return fwrite(ptr, size, nmemb, stream);
62 }
63 #define fwrite wrap_fwrite
64 
65 static const char *exec_name;
66 
67 static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
68  const char *s, va_list ap) {
69  if (ctx->err) {
70  const char *detail = aom_codec_error_detail(ctx);
71 
72  vfprintf(stderr, s, ap);
73  fprintf(stderr, ": %s\n", aom_codec_error(ctx));
74 
75  if (detail) fprintf(stderr, " %s\n", detail);
76 
77  if (fatal) exit(EXIT_FAILURE);
78  }
79 }
80 
81 static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
82  va_list ap;
83 
84  va_start(ap, s);
85  warn_or_exit_on_errorv(ctx, 1, s, ap);
86  va_end(ap);
87 }
88 
89 static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
90  const char *s, ...) {
91  va_list ap;
92 
93  va_start(ap, s);
94  warn_or_exit_on_errorv(ctx, fatal, s, ap);
95  va_end(ap);
96 }
97 
98 static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
99  FILE *f = input_ctx->file;
100  y4m_input *y4m = &input_ctx->y4m;
101  int shortread = 0;
102 
103  if (input_ctx->file_type == FILE_TYPE_Y4M) {
104  if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
105  } else {
106  shortread = read_yuv_frame(input_ctx, img);
107  }
108 
109  return !shortread;
110 }
111 
112 static int file_is_y4m(const char detect[4]) {
113  if (memcmp(detect, "YUV4", 4) == 0) {
114  return 1;
115  }
116  return 0;
117 }
118 
119 static int fourcc_is_ivf(const char detect[4]) {
120  if (memcmp(detect, "DKIF", 4) == 0) {
121  return 1;
122  }
123  return 0;
124 }
125 
126 static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
209  AV1E_SET_MTU,
213 #if CONFIG_DENOISE
216  AV1E_SET_ENABLE_DNL_DENOISING,
217 #endif // CONFIG_DENOISE
227 #if CONFIG_TUNE_VMAF
229 #endif
234  0 };
235 
236 const arg_def_t *main_args[] = { &g_av1_codec_arg_defs.help,
237  &g_av1_codec_arg_defs.use_cfg,
238  &g_av1_codec_arg_defs.debugmode,
239  &g_av1_codec_arg_defs.outputfile,
240  &g_av1_codec_arg_defs.codecarg,
241  &g_av1_codec_arg_defs.passes,
242  &g_av1_codec_arg_defs.pass_arg,
243  &g_av1_codec_arg_defs.fpf_name,
244  &g_av1_codec_arg_defs.limit,
245  &g_av1_codec_arg_defs.skip,
246  &g_av1_codec_arg_defs.good_dl,
247  &g_av1_codec_arg_defs.rt_dl,
248  &g_av1_codec_arg_defs.ai_dl,
249  &g_av1_codec_arg_defs.quietarg,
250  &g_av1_codec_arg_defs.verbosearg,
251  &g_av1_codec_arg_defs.psnrarg,
252  &g_av1_codec_arg_defs.use_webm,
253  &g_av1_codec_arg_defs.use_ivf,
254  &g_av1_codec_arg_defs.use_obu,
255  &g_av1_codec_arg_defs.q_hist_n,
256  &g_av1_codec_arg_defs.rate_hist_n,
257  &g_av1_codec_arg_defs.disable_warnings,
258  &g_av1_codec_arg_defs.disable_warning_prompt,
259  &g_av1_codec_arg_defs.recontest,
260  NULL };
261 
262 const arg_def_t *global_args[] = {
263  &g_av1_codec_arg_defs.use_yv12,
264  &g_av1_codec_arg_defs.use_i420,
265  &g_av1_codec_arg_defs.use_i422,
266  &g_av1_codec_arg_defs.use_i444,
267  &g_av1_codec_arg_defs.usage,
268  &g_av1_codec_arg_defs.threads,
269  &g_av1_codec_arg_defs.profile,
270  &g_av1_codec_arg_defs.width,
271  &g_av1_codec_arg_defs.height,
272  &g_av1_codec_arg_defs.forced_max_frame_width,
273  &g_av1_codec_arg_defs.forced_max_frame_height,
274 #if CONFIG_WEBM_IO
275  &g_av1_codec_arg_defs.stereo_mode,
276 #endif
277  &g_av1_codec_arg_defs.timebase,
278  &g_av1_codec_arg_defs.framerate,
279  &g_av1_codec_arg_defs.global_error_resilient,
280  &g_av1_codec_arg_defs.bitdeptharg,
281  &g_av1_codec_arg_defs.inbitdeptharg,
282  &g_av1_codec_arg_defs.lag_in_frames,
283  &g_av1_codec_arg_defs.large_scale_tile,
284  &g_av1_codec_arg_defs.monochrome,
285  &g_av1_codec_arg_defs.full_still_picture_hdr,
286  &g_av1_codec_arg_defs.use_16bit_internal,
287  &g_av1_codec_arg_defs.save_as_annexb,
288  NULL
289 };
290 
291 const arg_def_t *rc_args[] = { &g_av1_codec_arg_defs.dropframe_thresh,
292  &g_av1_codec_arg_defs.resize_mode,
293  &g_av1_codec_arg_defs.resize_denominator,
294  &g_av1_codec_arg_defs.resize_kf_denominator,
295  &g_av1_codec_arg_defs.superres_mode,
296  &g_av1_codec_arg_defs.superres_denominator,
297  &g_av1_codec_arg_defs.superres_kf_denominator,
298  &g_av1_codec_arg_defs.superres_qthresh,
299  &g_av1_codec_arg_defs.superres_kf_qthresh,
300  &g_av1_codec_arg_defs.end_usage,
301  &g_av1_codec_arg_defs.target_bitrate,
302  &g_av1_codec_arg_defs.min_quantizer,
303  &g_av1_codec_arg_defs.max_quantizer,
304  &g_av1_codec_arg_defs.undershoot_pct,
305  &g_av1_codec_arg_defs.overshoot_pct,
306  &g_av1_codec_arg_defs.buf_sz,
307  &g_av1_codec_arg_defs.buf_initial_sz,
308  &g_av1_codec_arg_defs.buf_optimal_sz,
309  &g_av1_codec_arg_defs.bias_pct,
310  &g_av1_codec_arg_defs.minsection_pct,
311  &g_av1_codec_arg_defs.maxsection_pct,
312  NULL };
313 
314 const arg_def_t *kf_args[] = { &g_av1_codec_arg_defs.fwd_kf_enabled,
315  &g_av1_codec_arg_defs.kf_min_dist,
316  &g_av1_codec_arg_defs.kf_max_dist,
317  &g_av1_codec_arg_defs.kf_disabled,
318  &g_av1_codec_arg_defs.sframe_dist,
319  &g_av1_codec_arg_defs.sframe_mode,
320  NULL };
321 
322 // TODO(bohanli): Currently all options are supported by the key & value API.
323 // Consider removing the control ID usages?
324 const arg_def_t *av1_ctrl_args[] = {
325  &g_av1_codec_arg_defs.cpu_used_av1,
326  &g_av1_codec_arg_defs.auto_altref,
327  &g_av1_codec_arg_defs.sharpness,
328  &g_av1_codec_arg_defs.static_thresh,
329  &g_av1_codec_arg_defs.rowmtarg,
330  &g_av1_codec_arg_defs.tile_cols,
331  &g_av1_codec_arg_defs.tile_rows,
332  &g_av1_codec_arg_defs.enable_tpl_model,
333  &g_av1_codec_arg_defs.enable_keyframe_filtering,
334  &g_av1_codec_arg_defs.arnr_maxframes,
335  &g_av1_codec_arg_defs.arnr_strength,
336  &g_av1_codec_arg_defs.tune_metric,
337  &g_av1_codec_arg_defs.cq_level,
338  &g_av1_codec_arg_defs.max_intra_rate_pct,
339  &g_av1_codec_arg_defs.max_inter_rate_pct,
340  &g_av1_codec_arg_defs.gf_cbr_boost_pct,
341  &g_av1_codec_arg_defs.lossless,
342  &g_av1_codec_arg_defs.enable_cdef,
343  &g_av1_codec_arg_defs.enable_restoration,
344  &g_av1_codec_arg_defs.enable_rect_partitions,
345  &g_av1_codec_arg_defs.enable_ab_partitions,
346  &g_av1_codec_arg_defs.enable_1to4_partitions,
347  &g_av1_codec_arg_defs.min_partition_size,
348  &g_av1_codec_arg_defs.max_partition_size,
349  &g_av1_codec_arg_defs.enable_dual_filter,
350  &g_av1_codec_arg_defs.enable_chroma_deltaq,
351  &g_av1_codec_arg_defs.enable_intra_edge_filter,
352  &g_av1_codec_arg_defs.enable_order_hint,
353  &g_av1_codec_arg_defs.enable_tx64,
354  &g_av1_codec_arg_defs.enable_flip_idtx,
355  &g_av1_codec_arg_defs.enable_rect_tx,
356  &g_av1_codec_arg_defs.enable_dist_wtd_comp,
357  &g_av1_codec_arg_defs.enable_masked_comp,
358  &g_av1_codec_arg_defs.enable_onesided_comp,
359  &g_av1_codec_arg_defs.enable_interintra_comp,
360  &g_av1_codec_arg_defs.enable_smooth_interintra,
361  &g_av1_codec_arg_defs.enable_diff_wtd_comp,
362  &g_av1_codec_arg_defs.enable_interinter_wedge,
363  &g_av1_codec_arg_defs.enable_interintra_wedge,
364  &g_av1_codec_arg_defs.enable_global_motion,
365  &g_av1_codec_arg_defs.enable_warped_motion,
366  &g_av1_codec_arg_defs.enable_filter_intra,
367  &g_av1_codec_arg_defs.enable_smooth_intra,
368  &g_av1_codec_arg_defs.enable_paeth_intra,
369  &g_av1_codec_arg_defs.enable_cfl_intra,
370  &g_av1_codec_arg_defs.enable_diagonal_intra,
371  &g_av1_codec_arg_defs.force_video_mode,
372  &g_av1_codec_arg_defs.enable_obmc,
373  &g_av1_codec_arg_defs.enable_overlay,
374  &g_av1_codec_arg_defs.enable_palette,
375  &g_av1_codec_arg_defs.enable_intrabc,
376  &g_av1_codec_arg_defs.enable_angle_delta,
377  &g_av1_codec_arg_defs.disable_trellis_quant,
378  &g_av1_codec_arg_defs.enable_qm,
379  &g_av1_codec_arg_defs.qm_min,
380  &g_av1_codec_arg_defs.qm_max,
381  &g_av1_codec_arg_defs.reduced_tx_type_set,
382  &g_av1_codec_arg_defs.use_intra_dct_only,
383  &g_av1_codec_arg_defs.use_inter_dct_only,
384  &g_av1_codec_arg_defs.use_intra_default_tx_only,
385  &g_av1_codec_arg_defs.quant_b_adapt,
386  &g_av1_codec_arg_defs.coeff_cost_upd_freq,
387  &g_av1_codec_arg_defs.mode_cost_upd_freq,
388  &g_av1_codec_arg_defs.mv_cost_upd_freq,
389  &g_av1_codec_arg_defs.frame_parallel_decoding,
390  &g_av1_codec_arg_defs.error_resilient_mode,
391  &g_av1_codec_arg_defs.aq_mode,
392  &g_av1_codec_arg_defs.deltaq_mode,
393  &g_av1_codec_arg_defs.deltalf_mode,
394  &g_av1_codec_arg_defs.frame_periodic_boost,
395  &g_av1_codec_arg_defs.noise_sens,
396  &g_av1_codec_arg_defs.tune_content,
397  &g_av1_codec_arg_defs.cdf_update_mode,
398  &g_av1_codec_arg_defs.input_color_primaries,
399  &g_av1_codec_arg_defs.input_transfer_characteristics,
400  &g_av1_codec_arg_defs.input_matrix_coefficients,
401  &g_av1_codec_arg_defs.input_chroma_sample_position,
402  &g_av1_codec_arg_defs.min_gf_interval,
403  &g_av1_codec_arg_defs.max_gf_interval,
404  &g_av1_codec_arg_defs.gf_min_pyr_height,
405  &g_av1_codec_arg_defs.gf_max_pyr_height,
406  &g_av1_codec_arg_defs.superblock_size,
407  &g_av1_codec_arg_defs.num_tg,
408  &g_av1_codec_arg_defs.mtu_size,
409  &g_av1_codec_arg_defs.timing_info,
410  &g_av1_codec_arg_defs.film_grain_test,
411  &g_av1_codec_arg_defs.film_grain_table,
412 #if CONFIG_DENOISE
413  &g_av1_codec_arg_defs.denoise_noise_level,
414  &g_av1_codec_arg_defs.denoise_block_size,
415  &g_av1_codec_arg_defs.enable_dnl_denoising,
416 #endif // CONFIG_DENOISE
417  &g_av1_codec_arg_defs.max_reference_frames,
418  &g_av1_codec_arg_defs.reduced_reference_set,
419  &g_av1_codec_arg_defs.enable_ref_frame_mvs,
420  &g_av1_codec_arg_defs.target_seq_level_idx,
421  &g_av1_codec_arg_defs.set_tier_mask,
422  &g_av1_codec_arg_defs.set_min_cr,
423  &g_av1_codec_arg_defs.vbr_corpus_complexity_lap,
424  &g_av1_codec_arg_defs.input_chroma_subsampling_x,
425  &g_av1_codec_arg_defs.input_chroma_subsampling_y,
426 #if CONFIG_TUNE_VMAF
427  &g_av1_codec_arg_defs.vmaf_model_path,
428 #endif
429  &g_av1_codec_arg_defs.dv_cost_upd_freq,
430  &g_av1_codec_arg_defs.partition_info_path,
431  &g_av1_codec_arg_defs.enable_directional_intra,
432  &g_av1_codec_arg_defs.enable_tx_size_search,
433  NULL,
434 };
435 
436 const arg_def_t *av1_key_val_args[] = {
437  &g_av1_codec_arg_defs.passes,
438  &g_av1_codec_arg_defs.two_pass_output,
439  &g_av1_codec_arg_defs.fwd_kf_dist,
440  NULL,
441 };
442 
443 static const arg_def_t *no_args[] = { NULL };
444 
445 static void show_help(FILE *fout, int shorthelp) {
446  fprintf(fout, "Usage: %s <options> -o dst_filename src_filename\n",
447  exec_name);
448 
449  if (shorthelp) {
450  fprintf(fout, "Use --help to see the full list of options.\n");
451  return;
452  }
453 
454  fprintf(fout, "\nOptions:\n");
455  arg_show_usage(fout, main_args);
456  fprintf(fout, "\nEncoder Global Options:\n");
457  arg_show_usage(fout, global_args);
458  fprintf(fout, "\nRate Control Options:\n");
459  arg_show_usage(fout, rc_args);
460  fprintf(fout, "\nKeyframe Placement Options:\n");
461  arg_show_usage(fout, kf_args);
462 #if CONFIG_AV1_ENCODER
463  fprintf(fout, "\nAV1 Specific Options:\n");
464  arg_show_usage(fout, av1_ctrl_args);
465  arg_show_usage(fout, av1_key_val_args);
466 #endif
467  fprintf(fout,
468  "\nStream timebase (--timebase):\n"
469  " The desired precision of timestamps in the output, expressed\n"
470  " in fractional seconds. Default is 1/1000.\n");
471  fprintf(fout, "\nIncluded encoders:\n\n");
472 
473  const int num_encoder = get_aom_encoder_count();
474  for (int i = 0; i < num_encoder; ++i) {
475  aom_codec_iface_t *encoder = get_aom_encoder_by_index(i);
476  const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
477  fprintf(fout, " %-6s - %s %s\n", get_short_name_by_aom_encoder(encoder),
478  aom_codec_iface_name(encoder), defstr);
479  }
480  fprintf(fout, "\n ");
481  fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
482 }
483 
484 void usage_exit(void) {
485  show_help(stderr, 1);
486  exit(EXIT_FAILURE);
487 }
488 
489 #if CONFIG_AV1_ENCODER
490 #define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
491 #define ARG_KEY_VAL_CNT_MAX NELEMENTS(av1_key_val_args)
492 #endif
493 
494 #if !CONFIG_WEBM_IO
495 typedef int stereo_format_t;
496 struct WebmOutputContext {
497  int debug;
498 };
499 #endif
500 
501 /* Per-stream configuration */
502 struct stream_config {
503  struct aom_codec_enc_cfg cfg;
504  const char *out_fn;
505  const char *stats_fn;
506  stereo_format_t stereo_fmt;
507  int arg_ctrls[ARG_CTRL_CNT_MAX][2];
508  int arg_ctrl_cnt;
509  const char *arg_key_vals[ARG_KEY_VAL_CNT_MAX][2];
510  int arg_key_val_cnt;
511  int write_webm;
512  const char *film_grain_filename;
513  int write_ivf;
514  // whether to use 16bit internal buffers
515  int use_16bit_internal;
516 #if CONFIG_TUNE_VMAF
517  const char *vmaf_model_path;
518 #endif
519  const char *partition_info_path;
520  aom_color_range_t color_range;
521  const char *two_pass_input;
522  const char *two_pass_output;
523  int two_pass_width;
524  int two_pass_height;
525 };
526 
527 struct stream_state {
528  int index;
529  struct stream_state *next;
530  struct stream_config config;
531  FILE *file;
532  struct rate_hist *rate_hist;
533  struct WebmOutputContext webm_ctx;
534  uint64_t psnr_sse_total[2];
535  uint64_t psnr_samples_total[2];
536  double psnr_totals[2][4];
537  int psnr_count[2];
538  int counts[64];
539  aom_codec_ctx_t encoder;
540  unsigned int frames_out;
541  uint64_t cx_time;
542  size_t nbytes;
543  stats_io_t stats;
544  struct aom_image *img;
545  aom_codec_ctx_t decoder;
546  int mismatch_seen;
547  unsigned int chroma_subsampling_x;
548  unsigned int chroma_subsampling_y;
549  const char *orig_out_fn;
550  unsigned int orig_width;
551  unsigned int orig_height;
552  int orig_write_webm;
553  int orig_write_ivf;
554  char tmp_out_fn[40];
555 };
556 
557 static void validate_positive_rational(const char *msg,
558  struct aom_rational *rat) {
559  if (rat->den < 0) {
560  rat->num *= -1;
561  rat->den *= -1;
562  }
563 
564  if (rat->num < 0) die("Error: %s must be positive\n", msg);
565 
566  if (!rat->den) die("Error: %s has zero denominator\n", msg);
567 }
568 
569 static void init_config(cfg_options_t *config) {
570  memset(config, 0, sizeof(cfg_options_t));
571  config->super_block_size = 0; // Dynamic
572  config->max_partition_size = 128;
573  config->min_partition_size = 4;
574  config->disable_trellis_quant = 3;
575 }
576 
577 /* Parses global config arguments into the AvxEncoderConfig. Note that
578  * argv is modified and overwrites all parsed arguments.
579  */
580 static void parse_global_config(struct AvxEncoderConfig *global, char ***argv) {
581  char **argi, **argj;
582  struct arg arg;
583  const int num_encoder = get_aom_encoder_count();
584  char **argv_local = (char **)*argv;
585  if (num_encoder < 1) die("Error: no valid encoder available\n");
586 
587  /* Initialize default parameters */
588  memset(global, 0, sizeof(*global));
589  global->codec = get_aom_encoder_by_index(num_encoder - 1);
590  global->passes = 0;
591  global->color_type = I420;
592  global->csp = AOM_CSP_UNKNOWN;
593  global->show_psnr = 0;
594 
595  int cfg_included = 0;
596  init_config(&global->encoder_config);
597 
598  for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
599  arg.argv_step = 1;
600 
601  if (arg_match(&arg, &g_av1_codec_arg_defs.use_cfg, argi)) {
602  if (!cfg_included) {
603  parse_cfg(arg.val, &global->encoder_config);
604  cfg_included = 1;
605  }
606  } else if (arg_match(&arg, &g_av1_codec_arg_defs.help, argi)) {
607  show_help(stdout, 0);
608  exit(EXIT_SUCCESS);
609  } else if (arg_match(&arg, &g_av1_codec_arg_defs.codecarg, argi)) {
610  global->codec = get_aom_encoder_by_short_name(arg.val);
611  if (!global->codec)
612  die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
613  } else if (arg_match(&arg, &g_av1_codec_arg_defs.passes, argi)) {
614  global->passes = arg_parse_uint(&arg);
615 
616  if (global->passes < 1 || global->passes > 3)
617  die("Error: Invalid number of passes (%d)\n", global->passes);
618  } else if (arg_match(&arg, &g_av1_codec_arg_defs.pass_arg, argi)) {
619  global->pass = arg_parse_uint(&arg);
620 
621  if (global->pass < 1 || global->pass > 3)
622  die("Error: Invalid pass selected (%d)\n", global->pass);
623  } else if (arg_match(&arg,
624  &g_av1_codec_arg_defs.input_chroma_sample_position,
625  argi)) {
626  global->csp = arg_parse_enum(&arg);
627  /* Flag is used by later code as well, preserve it. */
628  argj++;
629  } else if (arg_match(&arg, &g_av1_codec_arg_defs.usage, argi)) {
630  global->usage = arg_parse_uint(&arg);
631  } else if (arg_match(&arg, &g_av1_codec_arg_defs.good_dl, argi)) {
632  global->usage = AOM_USAGE_GOOD_QUALITY; // Good quality usage
633  } else if (arg_match(&arg, &g_av1_codec_arg_defs.rt_dl, argi)) {
634  global->usage = AOM_USAGE_REALTIME; // Real-time usage
635  } else if (arg_match(&arg, &g_av1_codec_arg_defs.ai_dl, argi)) {
636  global->usage = AOM_USAGE_ALL_INTRA; // All intra usage
637  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_yv12, argi)) {
638  global->color_type = YV12;
639  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i420, argi)) {
640  global->color_type = I420;
641  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i422, argi)) {
642  global->color_type = I422;
643  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i444, argi)) {
644  global->color_type = I444;
645  } else if (arg_match(&arg, &g_av1_codec_arg_defs.quietarg, argi)) {
646  global->quiet = 1;
647  } else if (arg_match(&arg, &g_av1_codec_arg_defs.verbosearg, argi)) {
648  global->verbose = 1;
649  } else if (arg_match(&arg, &g_av1_codec_arg_defs.limit, argi)) {
650  global->limit = arg_parse_uint(&arg);
651  } else if (arg_match(&arg, &g_av1_codec_arg_defs.skip, argi)) {
652  global->skip_frames = arg_parse_uint(&arg);
653  } else if (arg_match(&arg, &g_av1_codec_arg_defs.psnrarg, argi)) {
654  if (arg.val)
655  global->show_psnr = arg_parse_int(&arg);
656  else
657  global->show_psnr = 1;
658  } else if (arg_match(&arg, &g_av1_codec_arg_defs.recontest, argi)) {
659  global->test_decode = arg_parse_enum_or_int(&arg);
660  } else if (arg_match(&arg, &g_av1_codec_arg_defs.framerate, argi)) {
661  global->framerate = arg_parse_rational(&arg);
662  validate_positive_rational(arg.name, &global->framerate);
663  global->have_framerate = 1;
664  } else if (arg_match(&arg, &g_av1_codec_arg_defs.debugmode, argi)) {
665  global->debug = 1;
666  } else if (arg_match(&arg, &g_av1_codec_arg_defs.q_hist_n, argi)) {
667  global->show_q_hist_buckets = arg_parse_uint(&arg);
668  } else if (arg_match(&arg, &g_av1_codec_arg_defs.rate_hist_n, argi)) {
669  global->show_rate_hist_buckets = arg_parse_uint(&arg);
670  } else if (arg_match(&arg, &g_av1_codec_arg_defs.disable_warnings, argi)) {
671  global->disable_warnings = 1;
672  } else if (arg_match(&arg, &g_av1_codec_arg_defs.disable_warning_prompt,
673  argi)) {
674  global->disable_warning_prompt = 1;
675  } else {
676  argj++;
677  }
678  }
679 
680  if (global->pass) {
681  /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
682  if (global->pass > global->passes) {
683  aom_tools_warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
684  global->pass);
685  global->passes = global->pass;
686  }
687  }
688  /* Validate global config */
689  if (global->passes == 0) {
690 #if CONFIG_AV1_ENCODER
691  // Make default AV1 passes = 2 until there is a better quality 1-pass
692  // encoder
693  if (global->codec != NULL)
694  global->passes =
695  (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0 &&
696  global->usage != AOM_USAGE_REALTIME)
697  ? 2
698  : 1;
699 #else
700  global->passes = 1;
701 #endif
702  }
703 
704  if (global->usage == AOM_USAGE_REALTIME && global->passes > 1) {
705  aom_tools_warn("Enforcing one-pass encoding in realtime mode\n");
706  if (global->pass > 1)
707  die("Error: Invalid --pass=%d for one-pass encoding\n", global->pass);
708  global->passes = 1;
709  }
710 
711  if (global->usage == AOM_USAGE_ALL_INTRA && global->passes > 1) {
712  aom_tools_warn("Enforcing one-pass encoding in all intra mode\n");
713  global->passes = 1;
714  }
715 }
716 
717 static void open_input_file(struct AvxInputContext *input,
719  /* Parse certain options from the input file, if possible */
720  input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
721  : set_binary_mode(stdin);
722 
723  if (!input->file) fatal("Failed to open input file");
724 
725  if (!fseeko(input->file, 0, SEEK_END)) {
726  /* Input file is seekable. Figure out how long it is, so we can get
727  * progress info.
728  */
729  input->length = ftello(input->file);
730  rewind(input->file);
731  }
732 
733  /* Default to 1:1 pixel aspect ratio. */
734  input->pixel_aspect_ratio.numerator = 1;
735  input->pixel_aspect_ratio.denominator = 1;
736 
737  /* For RAW input sources, these bytes will applied on the first frame
738  * in read_frame().
739  */
740  input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
741  input->detect.position = 0;
742 
743  if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
744  if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
745  input->only_i420) >= 0) {
746  input->file_type = FILE_TYPE_Y4M;
747  input->width = input->y4m.pic_w;
748  input->height = input->y4m.pic_h;
749  input->pixel_aspect_ratio.numerator = input->y4m.par_n;
750  input->pixel_aspect_ratio.denominator = input->y4m.par_d;
751  input->framerate.numerator = input->y4m.fps_n;
752  input->framerate.denominator = input->y4m.fps_d;
753  input->fmt = input->y4m.aom_fmt;
754  input->bit_depth = input->y4m.bit_depth;
755  input->color_range = input->y4m.color_range;
756  } else
757  fatal("Unsupported Y4M stream.");
758  } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
759  fatal("IVF is not supported as input.");
760  } else {
761  input->file_type = FILE_TYPE_RAW;
762  }
763 }
764 
765 static void close_input_file(struct AvxInputContext *input) {
766  fclose(input->file);
767  if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
768 }
769 
770 static struct stream_state *new_stream(struct AvxEncoderConfig *global,
771  struct stream_state *prev) {
772  struct stream_state *stream;
773 
774  stream = calloc(1, sizeof(*stream));
775  if (stream == NULL) {
776  fatal("Failed to allocate new stream.");
777  }
778 
779  if (prev) {
780  memcpy(stream, prev, sizeof(*stream));
781  stream->index++;
782  prev->next = stream;
783  } else {
784  aom_codec_err_t res;
785 
786  /* Populate encoder configuration */
787  res = aom_codec_enc_config_default(global->codec, &stream->config.cfg,
788  global->usage);
789  if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
790 
791  /* Change the default timebase to a high enough value so that the
792  * encoder will always create strictly increasing timestamps.
793  */
794  stream->config.cfg.g_timebase.den = 1000;
795 
796  /* Never use the library's default resolution, require it be parsed
797  * from the file or set on the command line.
798  */
799  stream->config.cfg.g_w = 0;
800  stream->config.cfg.g_h = 0;
801 
802  /* Initialize remaining stream parameters */
803  stream->config.write_webm = 1;
804  stream->config.write_ivf = 0;
805 
806 #if CONFIG_WEBM_IO
807  stream->config.stereo_fmt = STEREO_FORMAT_MONO;
808  stream->webm_ctx.last_pts_ns = -1;
809  stream->webm_ctx.writer = NULL;
810  stream->webm_ctx.segment = NULL;
811 #endif
812 
813  /* Allows removal of the application version from the EBML tags */
814  stream->webm_ctx.debug = global->debug;
815  memcpy(&stream->config.cfg.encoder_cfg, &global->encoder_config,
816  sizeof(stream->config.cfg.encoder_cfg));
817  }
818 
819  /* Output files must be specified for each stream */
820  stream->config.out_fn = NULL;
821  stream->config.two_pass_input = NULL;
822  stream->config.two_pass_output = NULL;
823  stream->config.two_pass_width = 0;
824  stream->config.two_pass_height = 0;
825 
826  stream->next = NULL;
827  return stream;
828 }
829 
830 static void set_config_arg_ctrls(struct stream_config *config, int key,
831  const struct arg *arg) {
832  int j;
833  if (key == AV1E_SET_FILM_GRAIN_TABLE) {
834  config->film_grain_filename = arg->val;
835  return;
836  }
837 
838  // For target level, the settings should accumulate rather than overwrite,
839  // so we simply append it.
840  if (key == AV1E_SET_TARGET_SEQ_LEVEL_IDX) {
841  j = config->arg_ctrl_cnt;
842  assert(j < ARG_CTRL_CNT_MAX);
843  config->arg_ctrls[j][0] = key;
844  config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
845  ++config->arg_ctrl_cnt;
846  return;
847  }
848 
849  /* Point either to the next free element or the first instance of this
850  * control.
851  */
852  for (j = 0; j < config->arg_ctrl_cnt; j++)
853  if (config->arg_ctrls[j][0] == key) break;
854 
855  /* Update/insert */
856  assert(j < ARG_CTRL_CNT_MAX);
857  config->arg_ctrls[j][0] = key;
858  config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
859 
860  if (key == AOME_SET_ENABLEAUTOALTREF && config->arg_ctrls[j][1] > 1) {
861  aom_tools_warn(
862  "auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
863  config->arg_ctrls[j][1] = 1;
864  }
865 
866  if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
867 }
868 
869 static void set_config_arg_key_vals(struct stream_config *config,
870  const char *name, const struct arg *arg) {
871  int j;
872  const char *val = arg->val;
873  // For target level, the settings should accumulate rather than overwrite,
874  // so we simply append it.
875  if (strcmp(name, "target-seq-level-idx") == 0) {
876  j = config->arg_key_val_cnt;
877  assert(j < ARG_KEY_VAL_CNT_MAX);
878  config->arg_key_vals[j][0] = name;
879  config->arg_key_vals[j][1] = val;
880  ++config->arg_key_val_cnt;
881  return;
882  }
883 
884  /* Point either to the next free element or the first instance of this
885  * option.
886  */
887  for (j = 0; j < config->arg_key_val_cnt; j++)
888  if (strcmp(name, config->arg_key_vals[j][0]) == 0) break;
889 
890  /* Update/insert */
891  assert(j < ARG_KEY_VAL_CNT_MAX);
892  config->arg_key_vals[j][0] = name;
893  config->arg_key_vals[j][1] = val;
894 
895  if (strcmp(name, g_av1_codec_arg_defs.auto_altref.long_name) == 0) {
896  int auto_altref = arg_parse_int(arg);
897  if (auto_altref > 1) {
898  aom_tools_warn(
899  "auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
900  config->arg_key_vals[j][1] = "1";
901  }
902  }
903 
904  if (j == config->arg_key_val_cnt) config->arg_key_val_cnt++;
905 }
906 
907 static int parse_stream_params(struct AvxEncoderConfig *global,
908  struct stream_state *stream, char **argv) {
909  char **argi, **argj;
910  struct arg arg;
911  static const arg_def_t **ctrl_args = no_args;
912  static const arg_def_t **key_val_args = no_args;
913  static const int *ctrl_args_map = NULL;
914  struct stream_config *config = &stream->config;
915  int eos_mark_found = 0;
916  int webm_forced = 0;
917 
918  // Handle codec specific options
919  if (0) {
920 #if CONFIG_AV1_ENCODER
921  } else if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
922  // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
923  // Consider to expand this set for AV1 encoder control.
924  ctrl_args = av1_ctrl_args;
925  ctrl_args_map = av1_arg_ctrl_map;
926  key_val_args = av1_key_val_args;
927 #endif
928  }
929 
930  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
931  arg.argv_step = 1;
932 
933  /* Once we've found an end-of-stream marker (--) we want to continue
934  * shifting arguments but not consuming them.
935  */
936  if (eos_mark_found) {
937  argj++;
938  continue;
939  } else if (!strcmp(*argj, "--")) {
940  eos_mark_found = 1;
941  continue;
942  }
943 
944  if (arg_match(&arg, &g_av1_codec_arg_defs.outputfile, argi)) {
945  config->out_fn = arg.val;
946  if (!webm_forced) {
947  const size_t out_fn_len = strlen(config->out_fn);
948  if (out_fn_len >= 4 &&
949  !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
950  config->write_webm = 0;
951  config->write_ivf = 1;
952  } else if (out_fn_len >= 4 &&
953  !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
954  config->write_webm = 0;
955  config->write_ivf = 0;
956  }
957  }
958  } else if (arg_match(&arg, &g_av1_codec_arg_defs.fpf_name, argi)) {
959  config->stats_fn = arg.val;
960  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_webm, argi)) {
961 #if CONFIG_WEBM_IO
962  config->write_webm = 1;
963  webm_forced = 1;
964 #else
965  die("Error: --webm specified but webm is disabled.");
966 #endif
967  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_ivf, argi)) {
968  config->write_webm = 0;
969  config->write_ivf = 1;
970  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_obu, argi)) {
971  config->write_webm = 0;
972  config->write_ivf = 0;
973  } else if (arg_match(&arg, &g_av1_codec_arg_defs.threads, argi)) {
974  config->cfg.g_threads = arg_parse_uint(&arg);
975  } else if (arg_match(&arg, &g_av1_codec_arg_defs.profile, argi)) {
976  config->cfg.g_profile = arg_parse_uint(&arg);
977  } else if (arg_match(&arg, &g_av1_codec_arg_defs.width, argi)) {
978  config->cfg.g_w = arg_parse_uint(&arg);
979  } else if (arg_match(&arg, &g_av1_codec_arg_defs.height, argi)) {
980  config->cfg.g_h = arg_parse_uint(&arg);
981  } else if (arg_match(&arg, &g_av1_codec_arg_defs.forced_max_frame_width,
982  argi)) {
983  config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
984  } else if (arg_match(&arg, &g_av1_codec_arg_defs.forced_max_frame_height,
985  argi)) {
986  config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
987  } else if (arg_match(&arg, &g_av1_codec_arg_defs.bitdeptharg, argi)) {
988  config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
989  } else if (arg_match(&arg, &g_av1_codec_arg_defs.inbitdeptharg, argi)) {
990  config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
991  } else if (arg_match(&arg, &g_av1_codec_arg_defs.input_chroma_subsampling_x,
992  argi)) {
993  stream->chroma_subsampling_x = arg_parse_uint(&arg);
994  } else if (arg_match(&arg, &g_av1_codec_arg_defs.input_chroma_subsampling_y,
995  argi)) {
996  stream->chroma_subsampling_y = arg_parse_uint(&arg);
997 #if CONFIG_WEBM_IO
998  } else if (arg_match(&arg, &g_av1_codec_arg_defs.stereo_mode, argi)) {
999  config->stereo_fmt = arg_parse_enum_or_int(&arg);
1000 #endif
1001  } else if (arg_match(&arg, &g_av1_codec_arg_defs.timebase, argi)) {
1002  config->cfg.g_timebase = arg_parse_rational(&arg);
1003  validate_positive_rational(arg.name, &config->cfg.g_timebase);
1004  } else if (arg_match(&arg, &g_av1_codec_arg_defs.global_error_resilient,
1005  argi)) {
1006  config->cfg.g_error_resilient = arg_parse_uint(&arg);
1007  } else if (arg_match(&arg, &g_av1_codec_arg_defs.lag_in_frames, argi)) {
1008  config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1009  } else if (arg_match(&arg, &g_av1_codec_arg_defs.large_scale_tile, argi)) {
1010  config->cfg.large_scale_tile = arg_parse_uint(&arg);
1011  if (config->cfg.large_scale_tile) {
1012  global->codec = get_aom_encoder_by_short_name("av1");
1013  }
1014  } else if (arg_match(&arg, &g_av1_codec_arg_defs.monochrome, argi)) {
1015  config->cfg.monochrome = 1;
1016  } else if (arg_match(&arg, &g_av1_codec_arg_defs.full_still_picture_hdr,
1017  argi)) {
1018  config->cfg.full_still_picture_hdr = 1;
1019  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_16bit_internal,
1020  argi)) {
1021  config->use_16bit_internal = CONFIG_AV1_HIGHBITDEPTH;
1022  if (!config->use_16bit_internal) {
1023  aom_tools_warn("%s option ignored with CONFIG_AV1_HIGHBITDEPTH=0.\n",
1024  arg.name);
1025  }
1026  } else if (arg_match(&arg, &g_av1_codec_arg_defs.dropframe_thresh, argi)) {
1027  config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1028  } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_mode, argi)) {
1029  config->cfg.rc_resize_mode = arg_parse_uint(&arg);
1030  } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_denominator,
1031  argi)) {
1032  config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1033  } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_kf_denominator,
1034  argi)) {
1035  config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
1036  } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_mode, argi)) {
1037  config->cfg.rc_superres_mode = arg_parse_uint(&arg);
1038  } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_denominator,
1039  argi)) {
1040  config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1041  } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_kf_denominator,
1042  argi)) {
1043  config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
1044  } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_qthresh, argi)) {
1045  config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1046  } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_kf_qthresh,
1047  argi)) {
1048  config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
1049  } else if (arg_match(&arg, &g_av1_codec_arg_defs.end_usage, argi)) {
1050  config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1051  } else if (arg_match(&arg, &g_av1_codec_arg_defs.target_bitrate, argi)) {
1052  config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1053  } else if (arg_match(&arg, &g_av1_codec_arg_defs.min_quantizer, argi)) {
1054  config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1055  } else if (arg_match(&arg, &g_av1_codec_arg_defs.max_quantizer, argi)) {
1056  config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1057  } else if (arg_match(&arg, &g_av1_codec_arg_defs.undershoot_pct, argi)) {
1058  config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1059  } else if (arg_match(&arg, &g_av1_codec_arg_defs.overshoot_pct, argi)) {
1060  config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1061  } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_sz, argi)) {
1062  config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1063  } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_initial_sz, argi)) {
1064  config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1065  } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_optimal_sz, argi)) {
1066  config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1067  } else if (arg_match(&arg, &g_av1_codec_arg_defs.bias_pct, argi)) {
1068  config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1069  if (global->passes < 2)
1070  aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1071  } else if (arg_match(&arg, &g_av1_codec_arg_defs.minsection_pct, argi)) {
1072  config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1073 
1074  if (global->passes < 2)
1075  aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1076  } else if (arg_match(&arg, &g_av1_codec_arg_defs.maxsection_pct, argi)) {
1077  config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1078 
1079  if (global->passes < 2)
1080  aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1081  } else if (arg_match(&arg, &g_av1_codec_arg_defs.fwd_kf_enabled, argi)) {
1082  config->cfg.fwd_kf_enabled = arg_parse_uint(&arg);
1083  } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_min_dist, argi)) {
1084  config->cfg.kf_min_dist = arg_parse_uint(&arg);
1085  } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_max_dist, argi)) {
1086  config->cfg.kf_max_dist = arg_parse_uint(&arg);
1087  } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_disabled, argi)) {
1088  config->cfg.kf_mode = AOM_KF_DISABLED;
1089  } else if (arg_match(&arg, &g_av1_codec_arg_defs.sframe_dist, argi)) {
1090  config->cfg.sframe_dist = arg_parse_uint(&arg);
1091  } else if (arg_match(&arg, &g_av1_codec_arg_defs.sframe_mode, argi)) {
1092  config->cfg.sframe_mode = arg_parse_uint(&arg);
1093  } else if (arg_match(&arg, &g_av1_codec_arg_defs.save_as_annexb, argi)) {
1094  config->cfg.save_as_annexb = arg_parse_uint(&arg);
1095  } else if (arg_match(&arg, &g_av1_codec_arg_defs.tile_width, argi)) {
1096  config->cfg.tile_width_count =
1097  arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1098  } else if (arg_match(&arg, &g_av1_codec_arg_defs.tile_height, argi)) {
1099  config->cfg.tile_height_count =
1100  arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1101 #if CONFIG_TUNE_VMAF
1102  } else if (arg_match(&arg, &g_av1_codec_arg_defs.vmaf_model_path, argi)) {
1103  config->vmaf_model_path = arg.val;
1104 #endif
1105  } else if (arg_match(&arg, &g_av1_codec_arg_defs.partition_info_path,
1106  argi)) {
1107  config->partition_info_path = arg.val;
1108  } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_fixed_qp_offsets,
1109  argi)) {
1110  config->cfg.use_fixed_qp_offsets = arg_parse_uint(&arg);
1111  } else if (arg_match(&arg, &g_av1_codec_arg_defs.fixed_qp_offsets, argi)) {
1112  const int fixed_qp_offset_count = arg_parse_list(
1113  &arg, config->cfg.fixed_qp_offsets, FIXED_QP_OFFSET_COUNT);
1114  if (fixed_qp_offset_count < FIXED_QP_OFFSET_COUNT) {
1115  die("Option --fixed_qp_offsets requires %d comma-separated values, but "
1116  "only %d values were provided.\n",
1117  FIXED_QP_OFFSET_COUNT, fixed_qp_offset_count);
1118  }
1119  config->cfg.use_fixed_qp_offsets = 1;
1120  } else if (global->usage == AOM_USAGE_REALTIME &&
1121  arg_match(&arg, &g_av1_codec_arg_defs.enable_restoration,
1122  argi)) {
1123  if (arg_parse_uint(&arg) == 1) {
1124  aom_tools_warn("non-zero %s option ignored in realtime mode.\n",
1125  arg.name);
1126  }
1127  } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_input, argi)) {
1128  config->two_pass_input = arg.val;
1129  } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_output, argi)) {
1130  config->two_pass_output = arg.val;
1131  } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_width, argi)) {
1132  config->two_pass_width = arg_parse_int(&arg);
1133  } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_height, argi)) {
1134  config->two_pass_height = arg_parse_int(&arg);
1135  } else {
1136  int i, match = 0;
1137  // check if the control ID API supports this arg
1138  if (ctrl_args_map) {
1139  for (i = 0; ctrl_args[i]; i++) {
1140  if (arg_match(&arg, ctrl_args[i], argi)) {
1141  match = 1;
1142  set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
1143  break;
1144  }
1145  }
1146  }
1147  if (!match) {
1148  // check if the key & value API supports this arg
1149  for (i = 0; key_val_args[i]; i++) {
1150  if (arg_match(&arg, key_val_args[i], argi)) {
1151  match = 1;
1152  set_config_arg_key_vals(config, key_val_args[i]->long_name, &arg);
1153  break;
1154  }
1155  }
1156  }
1157  if (!match) argj++;
1158  }
1159  }
1160  config->use_16bit_internal |= config->cfg.g_bit_depth > AOM_BITS_8;
1161 
1162  if (global->usage == AOM_USAGE_REALTIME && config->cfg.g_lag_in_frames != 0) {
1163  aom_tools_warn("non-zero lag-in-frames option ignored in realtime mode.\n");
1164  config->cfg.g_lag_in_frames = 0;
1165  }
1166 
1167  if (global->usage == AOM_USAGE_ALL_INTRA) {
1168  if (config->cfg.g_lag_in_frames != 0) {
1169  aom_tools_warn(
1170  "non-zero lag-in-frames option ignored in all intra mode.\n");
1171  config->cfg.g_lag_in_frames = 0;
1172  }
1173  if (config->cfg.kf_max_dist != 0) {
1174  aom_tools_warn(
1175  "non-zero max key frame distance option ignored in all intra "
1176  "mode.\n");
1177  config->cfg.kf_max_dist = 0;
1178  }
1179  }
1180 
1181  // set the passes field using key & val API
1182  if (config->arg_key_val_cnt >= ARG_KEY_VAL_CNT_MAX) {
1183  die("Not enough buffer for the key & value API.");
1184  }
1185  config->arg_key_vals[config->arg_key_val_cnt][0] = "passes";
1186  switch (global->passes) {
1187  case 0: config->arg_key_vals[config->arg_key_val_cnt][1] = "0"; break;
1188  case 1: config->arg_key_vals[config->arg_key_val_cnt][1] = "1"; break;
1189  case 2: config->arg_key_vals[config->arg_key_val_cnt][1] = "2"; break;
1190  case 3: config->arg_key_vals[config->arg_key_val_cnt][1] = "3"; break;
1191  default: die("Invalid value of --passes.");
1192  }
1193  config->arg_key_val_cnt++;
1194 
1195  // set the two_pass_output field
1196  if (!config->two_pass_output && global->passes == 3) {
1197  snprintf(stream->tmp_out_fn, sizeof(stream->tmp_out_fn),
1198  "tmp_2pass_output_%d.ivf", stream->index);
1199  stream->config.two_pass_output = stream->tmp_out_fn;
1200  }
1201  if (config->two_pass_output) {
1202  config->arg_key_vals[config->arg_key_val_cnt][0] = "two-pass-output";
1203  config->arg_key_vals[config->arg_key_val_cnt][1] = config->two_pass_output;
1204  config->arg_key_val_cnt++;
1205  }
1206 
1207  return eos_mark_found;
1208 }
1209 
1210 #define FOREACH_STREAM(iterator, list) \
1211  for (struct stream_state *iterator = list; iterator; \
1212  iterator = iterator->next)
1213 
1214 static void validate_stream_config(const struct stream_state *stream,
1215  const struct AvxEncoderConfig *global) {
1216  const struct stream_state *streami;
1217  (void)global;
1218 
1219  if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1220  fatal(
1221  "Stream %d: Specify stream dimensions with --width (-w) "
1222  " and --height (-h)",
1223  stream->index);
1224 
1225  /* Even if bit depth is set on the command line flag to be lower,
1226  * it is upgraded to at least match the input bit depth.
1227  */
1228  assert(stream->config.cfg.g_input_bit_depth <=
1229  (unsigned int)stream->config.cfg.g_bit_depth);
1230 
1231  for (streami = stream; streami; streami = streami->next) {
1232  /* All streams require output files */
1233  if (!streami->config.out_fn)
1234  fatal("Stream %d: Output file is required (specify with -o)",
1235  streami->index);
1236 
1237  /* Check for two streams outputting to the same file */
1238  if (streami != stream) {
1239  const char *a = stream->config.out_fn;
1240  const char *b = streami->config.out_fn;
1241  if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1242  fatal("Stream %d: duplicate output file (from stream %d)",
1243  streami->index, stream->index);
1244  }
1245 
1246  /* Check for two streams sharing a stats file. */
1247  if (streami != stream) {
1248  const char *a = stream->config.stats_fn;
1249  const char *b = streami->config.stats_fn;
1250  if (a && b && !strcmp(a, b))
1251  fatal("Stream %d: duplicate stats file (from stream %d)",
1252  streami->index, stream->index);
1253  }
1254  }
1255 }
1256 
1257 static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
1258  unsigned int h) {
1259  if (!stream->config.cfg.g_w) {
1260  if (!stream->config.cfg.g_h)
1261  stream->config.cfg.g_w = w;
1262  else
1263  stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1264  }
1265  if (!stream->config.cfg.g_h) {
1266  stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1267  }
1268 }
1269 
1270 static const char *file_type_to_string(enum VideoFileType t) {
1271  switch (t) {
1272  case FILE_TYPE_RAW: return "RAW";
1273  case FILE_TYPE_Y4M: return "Y4M";
1274  default: return "Other";
1275  }
1276 }
1277 
1278 static const char *image_format_to_string(aom_img_fmt_t f) {
1279  switch (f) {
1280  case AOM_IMG_FMT_I420: return "I420";
1281  case AOM_IMG_FMT_I422: return "I422";
1282  case AOM_IMG_FMT_I444: return "I444";
1283  case AOM_IMG_FMT_YV12: return "YV12";
1284  case AOM_IMG_FMT_YV1216: return "YV1216";
1285  case AOM_IMG_FMT_I42016: return "I42016";
1286  case AOM_IMG_FMT_I42216: return "I42216";
1287  case AOM_IMG_FMT_I44416: return "I44416";
1288  default: return "Other";
1289  }
1290 }
1291 
1292 static void show_stream_config(struct stream_state *stream,
1293  struct AvxEncoderConfig *global,
1294  struct AvxInputContext *input) {
1295 #define SHOW(field) \
1296  fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1297 
1298  if (stream->index == 0) {
1299  fprintf(stderr, "Codec: %s\n", aom_codec_iface_name(global->codec));
1300  fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
1301  input->filename, file_type_to_string(input->file_type),
1302  image_format_to_string(input->fmt));
1303  }
1304  if (stream->next || stream->index)
1305  fprintf(stderr, "\nStream Index: %d\n", stream->index);
1306  fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1307  fprintf(stderr, "Coding path: %s\n",
1308  stream->config.use_16bit_internal ? "HBD" : "LBD");
1309  fprintf(stderr, "Encoder parameters:\n");
1310 
1311  SHOW(g_usage);
1312  SHOW(g_threads);
1313  SHOW(g_profile);
1314  SHOW(g_w);
1315  SHOW(g_h);
1316  SHOW(g_bit_depth);
1317  SHOW(g_input_bit_depth);
1318  SHOW(g_timebase.num);
1319  SHOW(g_timebase.den);
1320  SHOW(g_error_resilient);
1321  SHOW(g_pass);
1322  SHOW(g_lag_in_frames);
1323  SHOW(large_scale_tile);
1324  SHOW(rc_dropframe_thresh);
1325  SHOW(rc_resize_mode);
1326  SHOW(rc_resize_denominator);
1327  SHOW(rc_resize_kf_denominator);
1328  SHOW(rc_superres_mode);
1329  SHOW(rc_superres_denominator);
1330  SHOW(rc_superres_kf_denominator);
1331  SHOW(rc_superres_qthresh);
1332  SHOW(rc_superres_kf_qthresh);
1333  SHOW(rc_end_usage);
1334  SHOW(rc_target_bitrate);
1335  SHOW(rc_min_quantizer);
1336  SHOW(rc_max_quantizer);
1337  SHOW(rc_undershoot_pct);
1338  SHOW(rc_overshoot_pct);
1339  SHOW(rc_buf_sz);
1340  SHOW(rc_buf_initial_sz);
1341  SHOW(rc_buf_optimal_sz);
1342  SHOW(rc_2pass_vbr_bias_pct);
1343  SHOW(rc_2pass_vbr_minsection_pct);
1344  SHOW(rc_2pass_vbr_maxsection_pct);
1345  SHOW(fwd_kf_enabled);
1346  SHOW(kf_mode);
1347  SHOW(kf_min_dist);
1348  SHOW(kf_max_dist);
1349 
1350 #define SHOW_PARAMS(field) \
1351  fprintf(stderr, " %-28s = %d\n", #field, \
1352  stream->config.cfg.encoder_cfg.field)
1353  if (global->encoder_config.init_by_cfg_file) {
1354  SHOW_PARAMS(super_block_size);
1355  SHOW_PARAMS(max_partition_size);
1356  SHOW_PARAMS(min_partition_size);
1357  SHOW_PARAMS(disable_ab_partition_type);
1358  SHOW_PARAMS(disable_rect_partition_type);
1359  SHOW_PARAMS(disable_1to4_partition_type);
1360  SHOW_PARAMS(disable_flip_idtx);
1361  SHOW_PARAMS(disable_cdef);
1362  SHOW_PARAMS(disable_lr);
1363  SHOW_PARAMS(disable_obmc);
1364  SHOW_PARAMS(disable_warp_motion);
1365  SHOW_PARAMS(disable_global_motion);
1366  SHOW_PARAMS(disable_dist_wtd_comp);
1367  SHOW_PARAMS(disable_diff_wtd_comp);
1368  SHOW_PARAMS(disable_inter_intra_comp);
1369  SHOW_PARAMS(disable_masked_comp);
1370  SHOW_PARAMS(disable_one_sided_comp);
1371  SHOW_PARAMS(disable_palette);
1372  SHOW_PARAMS(disable_intrabc);
1373  SHOW_PARAMS(disable_cfl);
1374  SHOW_PARAMS(disable_smooth_intra);
1375  SHOW_PARAMS(disable_filter_intra);
1376  SHOW_PARAMS(disable_dual_filter);
1377  SHOW_PARAMS(disable_intra_angle_delta);
1378  SHOW_PARAMS(disable_intra_edge_filter);
1379  SHOW_PARAMS(disable_tx_64x64);
1380  SHOW_PARAMS(disable_smooth_inter_intra);
1381  SHOW_PARAMS(disable_inter_inter_wedge);
1382  SHOW_PARAMS(disable_inter_intra_wedge);
1383  SHOW_PARAMS(disable_paeth_intra);
1384  SHOW_PARAMS(disable_trellis_quant);
1385  SHOW_PARAMS(disable_ref_frame_mv);
1386  SHOW_PARAMS(reduced_reference_set);
1387  SHOW_PARAMS(reduced_tx_type_set);
1388  }
1389 }
1390 
1391 static void open_output_file(struct stream_state *stream,
1392  struct AvxEncoderConfig *global,
1393  const struct AvxRational *pixel_aspect_ratio,
1394  const char *encoder_settings) {
1395  const char *fn = stream->config.out_fn;
1396  const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1397 
1398  if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1399 
1400  stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
1401 
1402  if (!stream->file) fatal("Failed to open output file");
1403 
1404  if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1405  fatal("WebM output to pipes not supported.");
1406 
1407 #if CONFIG_WEBM_IO
1408  if (stream->config.write_webm) {
1409  stream->webm_ctx.stream = stream->file;
1410  if (write_webm_file_header(&stream->webm_ctx, &stream->encoder, cfg,
1411  stream->config.stereo_fmt,
1412  get_fourcc_by_aom_encoder(global->codec),
1413  pixel_aspect_ratio, encoder_settings) != 0) {
1414  fatal("WebM writer initialization failed.");
1415  }
1416  }
1417 #else
1418  (void)pixel_aspect_ratio;
1419  (void)encoder_settings;
1420 #endif
1421 
1422  if (!stream->config.write_webm && stream->config.write_ivf) {
1423  ivf_write_file_header(stream->file, cfg,
1424  get_fourcc_by_aom_encoder(global->codec), 0);
1425  }
1426 }
1427 
1428 static void close_output_file(struct stream_state *stream,
1429  unsigned int fourcc) {
1430  const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1431 
1432  if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1433 
1434 #if CONFIG_WEBM_IO
1435  if (stream->config.write_webm) {
1436  if (write_webm_file_footer(&stream->webm_ctx) != 0) {
1437  fatal("WebM writer finalization failed.");
1438  }
1439  }
1440 #endif
1441 
1442  if (!stream->config.write_webm && stream->config.write_ivf) {
1443  if (!fseek(stream->file, 0, SEEK_SET))
1444  ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1445  stream->frames_out);
1446  }
1447 
1448  fclose(stream->file);
1449 }
1450 
1451 static void setup_pass(struct stream_state *stream,
1452  struct AvxEncoderConfig *global, int pass) {
1453  if (stream->config.stats_fn) {
1454  if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1455  fatal("Failed to open statistics store");
1456  } else {
1457  if (!stats_open_mem(&stream->stats, pass))
1458  fatal("Failed to open statistics store");
1459  }
1460 
1461  if (global->passes == 1) {
1462  stream->config.cfg.g_pass = AOM_RC_ONE_PASS;
1463  } else {
1464  switch (pass) {
1465  case 0: stream->config.cfg.g_pass = AOM_RC_FIRST_PASS; break;
1466  case 1: stream->config.cfg.g_pass = AOM_RC_SECOND_PASS; break;
1467  case 2: stream->config.cfg.g_pass = AOM_RC_THIRD_PASS; break;
1468  default: fatal("Failed to set pass");
1469  }
1470  }
1471 
1472  if (pass) {
1473  stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1474  }
1475 
1476  stream->cx_time = 0;
1477  stream->nbytes = 0;
1478  stream->frames_out = 0;
1479 }
1480 
1481 static void initialize_encoder(struct stream_state *stream,
1482  struct AvxEncoderConfig *global) {
1483  int i;
1484  int flags = 0;
1485 
1486  flags |= (global->show_psnr >= 1) ? AOM_CODEC_USE_PSNR : 0;
1487  flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
1488 
1489  /* Construct Encoder Context */
1490  aom_codec_enc_init(&stream->encoder, global->codec, &stream->config.cfg,
1491  flags);
1492  ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
1493 
1494  for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1495  int ctrl = stream->config.arg_ctrls[i][0];
1496  int value = stream->config.arg_ctrls[i][1];
1497  if (aom_codec_control(&stream->encoder, ctrl, value))
1498  fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
1499 
1500  ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1501  }
1502 
1503  for (i = 0; i < stream->config.arg_key_val_cnt; i++) {
1504  const char *name = stream->config.arg_key_vals[i][0];
1505  const char *val = stream->config.arg_key_vals[i][1];
1506  if (aom_codec_set_option(&stream->encoder, name, val))
1507  fprintf(stderr, "Error: Tried to set option %s = %s\n", name, val);
1508 
1509  ctx_exit_on_error(&stream->encoder, "Failed to set codec option");
1510  }
1511 
1512 #if CONFIG_TUNE_VMAF
1513  if (stream->config.vmaf_model_path) {
1515  stream->config.vmaf_model_path);
1516  }
1517 #endif
1518  if (stream->config.partition_info_path) {
1519  AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1521  stream->config.partition_info_path);
1522  }
1523 
1524  if (stream->config.film_grain_filename) {
1526  stream->config.film_grain_filename);
1527  }
1529  stream->config.color_range);
1530 
1531 #if CONFIG_AV1_DECODER
1532  if (global->test_decode != TEST_DECODE_OFF) {
1533  aom_codec_iface_t *decoder = get_aom_decoder_by_short_name(
1534  get_short_name_by_aom_encoder(global->codec));
1535  aom_codec_dec_cfg_t cfg = { 0, 0, 0, !stream->config.use_16bit_internal };
1536  aom_codec_dec_init(&stream->decoder, decoder, &cfg, 0);
1537 
1538  if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
1540  stream->config.cfg.large_scale_tile);
1541  ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1542 
1544  stream->config.cfg.save_as_annexb);
1545  ctx_exit_on_error(&stream->decoder, "Failed to set is_annexb");
1546 
1548  -1);
1549  ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1550 
1551  AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_SET_DECODE_TILE_COL,
1552  -1);
1553  ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1554  }
1555  }
1556 #endif
1557 }
1558 
1559 // Convert the input image 'img' to a monochrome image. The Y plane of the
1560 // output image is a shallow copy of the Y plane of the input image, therefore
1561 // the input image must remain valid for the lifetime of the output image. The U
1562 // and V planes of the output image are set to null pointers. The output image
1563 // format is AOM_IMG_FMT_I420 because libaom does not have AOM_IMG_FMT_I400.
1564 static void convert_image_to_monochrome(const struct aom_image *img,
1565  struct aom_image *monochrome_img) {
1566  *monochrome_img = *img;
1567  monochrome_img->fmt = AOM_IMG_FMT_I420;
1568  if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1569  monochrome_img->fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
1570  }
1571  monochrome_img->monochrome = 1;
1572  monochrome_img->csp = AOM_CSP_UNKNOWN;
1573  monochrome_img->x_chroma_shift = 1;
1574  monochrome_img->y_chroma_shift = 1;
1575  monochrome_img->planes[AOM_PLANE_U] = NULL;
1576  monochrome_img->planes[AOM_PLANE_V] = NULL;
1577  monochrome_img->stride[AOM_PLANE_U] = 0;
1578  monochrome_img->stride[AOM_PLANE_V] = 0;
1579  monochrome_img->sz = 0;
1580  monochrome_img->bps = (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
1581  monochrome_img->img_data = NULL;
1582  monochrome_img->img_data_owner = 0;
1583  monochrome_img->self_allocd = 0;
1584 }
1585 
1586 static void encode_frame(struct stream_state *stream,
1587  struct AvxEncoderConfig *global, struct aom_image *img,
1588  unsigned int frames_in) {
1589  aom_codec_pts_t frame_start, next_frame_start;
1590  struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1591  struct aom_usec_timer timer;
1592 
1593  frame_start =
1594  (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1595  cfg->g_timebase.num / global->framerate.num;
1596  next_frame_start =
1597  (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1598  cfg->g_timebase.num / global->framerate.num;
1599 
1600  /* Scale if necessary */
1601  if (img) {
1602  if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
1603  (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1604  if (img->fmt != AOM_IMG_FMT_I42016) {
1605  fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1606  exit(EXIT_FAILURE);
1607  }
1608 #if CONFIG_LIBYUV
1609  if (!stream->img) {
1610  stream->img =
1611  aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
1612  }
1613  I420Scale_16(
1614  (uint16_t *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1615  (uint16_t *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1616  (uint16_t *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1617  img->d_w, img->d_h, (uint16_t *)stream->img->planes[AOM_PLANE_Y],
1618  stream->img->stride[AOM_PLANE_Y] / 2,
1619  (uint16_t *)stream->img->planes[AOM_PLANE_U],
1620  stream->img->stride[AOM_PLANE_U] / 2,
1621  (uint16_t *)stream->img->planes[AOM_PLANE_V],
1622  stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
1623  stream->img->d_h, kFilterBox);
1624  img = stream->img;
1625 #else
1626  stream->encoder.err = 1;
1627  ctx_exit_on_error(&stream->encoder,
1628  "Stream %d: Failed to encode frame.\n"
1629  "libyuv is required for scaling but is currently "
1630  "disabled.\n"
1631  "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1632  "cmake.\n",
1633  stream->index);
1634 #endif
1635  }
1636  }
1637  if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1638  if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
1639  fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1640  exit(EXIT_FAILURE);
1641  }
1642 #if CONFIG_LIBYUV
1643  if (!stream->img)
1644  stream->img =
1645  aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
1646  I420Scale(
1647  img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1648  img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1649  img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1650  stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1651  stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1652  stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
1653  stream->img->d_w, stream->img->d_h, kFilterBox);
1654  img = stream->img;
1655 #else
1656  stream->encoder.err = 1;
1657  ctx_exit_on_error(&stream->encoder,
1658  "Stream %d: Failed to encode frame.\n"
1659  "Scaling disabled in this configuration. \n"
1660  "To enable, configure with --enable-libyuv\n",
1661  stream->index);
1662 #endif
1663  }
1664 
1665  struct aom_image monochrome_img;
1666  if (img && cfg->monochrome) {
1667  convert_image_to_monochrome(img, &monochrome_img);
1668  img = &monochrome_img;
1669  }
1670 
1671  aom_usec_timer_start(&timer);
1672  aom_codec_encode(&stream->encoder, img, frame_start,
1673  (uint32_t)(next_frame_start - frame_start), 0);
1674  aom_usec_timer_mark(&timer);
1675  stream->cx_time += aom_usec_timer_elapsed(&timer);
1676  ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1677  stream->index);
1678 }
1679 
1680 static void update_quantizer_histogram(struct stream_state *stream) {
1681  if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
1682  int q;
1683 
1685  &q);
1686  ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1687  stream->counts[q]++;
1688  }
1689 }
1690 
1691 static void get_cx_data(struct stream_state *stream,
1692  struct AvxEncoderConfig *global, int *got_data) {
1693  const aom_codec_cx_pkt_t *pkt;
1694  const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1695  aom_codec_iter_t iter = NULL;
1696 
1697  *got_data = 0;
1698  while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
1699  static size_t fsize = 0;
1700  static FileOffset ivf_header_pos = 0;
1701 
1702  switch (pkt->kind) {
1704  ++stream->frames_out;
1705  if (!global->quiet)
1706  fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
1707 
1708  update_rate_histogram(stream->rate_hist, cfg, pkt);
1709 #if CONFIG_WEBM_IO
1710  if (stream->config.write_webm) {
1711  if (write_webm_block(&stream->webm_ctx, cfg, pkt) != 0) {
1712  fatal("WebM writer failed.");
1713  }
1714  }
1715 #endif
1716  if (!stream->config.write_webm) {
1717  if (stream->config.write_ivf) {
1718  if (pkt->data.frame.partition_id <= 0) {
1719  ivf_header_pos = ftello(stream->file);
1720  fsize = pkt->data.frame.sz;
1721 
1722  ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1723  } else {
1724  fsize += pkt->data.frame.sz;
1725 
1726  const FileOffset currpos = ftello(stream->file);
1727  fseeko(stream->file, ivf_header_pos, SEEK_SET);
1728  ivf_write_frame_size(stream->file, fsize);
1729  fseeko(stream->file, currpos, SEEK_SET);
1730  }
1731  }
1732 
1733  (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1734  stream->file);
1735  }
1736  stream->nbytes += pkt->data.raw.sz;
1737 
1738  *got_data = 1;
1739 #if CONFIG_AV1_DECODER
1740  if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1741  aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
1742  pkt->data.frame.sz, NULL);
1743  if (stream->decoder.err) {
1744  warn_or_exit_on_error(&stream->decoder,
1745  global->test_decode == TEST_DECODE_FATAL,
1746  "Failed to decode frame %d in stream %d",
1747  stream->frames_out + 1, stream->index);
1748  stream->mismatch_seen = stream->frames_out + 1;
1749  }
1750  }
1751 #endif
1752  break;
1753  case AOM_CODEC_STATS_PKT:
1754  stream->frames_out++;
1755  stats_write(&stream->stats, pkt->data.twopass_stats.buf,
1756  pkt->data.twopass_stats.sz);
1757  stream->nbytes += pkt->data.raw.sz;
1758  break;
1759  case AOM_CODEC_PSNR_PKT:
1760 
1761  if (global->show_psnr >= 1) {
1762  int i;
1763 
1764  stream->psnr_sse_total[0] += pkt->data.psnr.sse[0];
1765  stream->psnr_samples_total[0] += pkt->data.psnr.samples[0];
1766  for (i = 0; i < 4; i++) {
1767  if (!global->quiet)
1768  fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
1769  stream->psnr_totals[0][i] += pkt->data.psnr.psnr[i];
1770  }
1771  stream->psnr_count[0]++;
1772 
1773 #if CONFIG_AV1_HIGHBITDEPTH
1774  if (stream->config.cfg.g_input_bit_depth <
1775  (unsigned int)stream->config.cfg.g_bit_depth) {
1776  stream->psnr_sse_total[1] += pkt->data.psnr.sse_hbd[0];
1777  stream->psnr_samples_total[1] += pkt->data.psnr.samples_hbd[0];
1778  for (i = 0; i < 4; i++) {
1779  if (!global->quiet)
1780  fprintf(stderr, "%.3f ", pkt->data.psnr.psnr_hbd[i]);
1781  stream->psnr_totals[1][i] += pkt->data.psnr.psnr_hbd[i];
1782  }
1783  stream->psnr_count[1]++;
1784  }
1785 #endif
1786  }
1787 
1788  break;
1789  default: break;
1790  }
1791  }
1792 }
1793 
1794 static void show_psnr(struct stream_state *stream, double peak, int64_t bps) {
1795  int i;
1796  double ovpsnr;
1797 
1798  if (!stream->psnr_count[0]) return;
1799 
1800  fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1801  ovpsnr = sse_to_psnr((double)stream->psnr_samples_total[0], peak,
1802  (double)stream->psnr_sse_total[0]);
1803  fprintf(stderr, " %.3f", ovpsnr);
1804 
1805  for (i = 0; i < 4; i++) {
1806  fprintf(stderr, " %.3f", stream->psnr_totals[0][i] / stream->psnr_count[0]);
1807  }
1808  if (bps > 0) {
1809  fprintf(stderr, " %7" PRId64 " bps", bps);
1810  }
1811  fprintf(stderr, " %7" PRId64 " ms", stream->cx_time / 1000);
1812  fprintf(stderr, "\n");
1813 }
1814 
1815 #if CONFIG_AV1_HIGHBITDEPTH
1816 static void show_psnr_hbd(struct stream_state *stream, double peak,
1817  int64_t bps) {
1818  int i;
1819  double ovpsnr;
1820  // Compute PSNR based on stream bit depth
1821  if (!stream->psnr_count[1]) return;
1822 
1823  fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1824  ovpsnr = sse_to_psnr((double)stream->psnr_samples_total[1], peak,
1825  (double)stream->psnr_sse_total[1]);
1826  fprintf(stderr, " %.3f", ovpsnr);
1827 
1828  for (i = 0; i < 4; i++) {
1829  fprintf(stderr, " %.3f", stream->psnr_totals[1][i] / stream->psnr_count[1]);
1830  }
1831  if (bps > 0) {
1832  fprintf(stderr, " %7" PRId64 " bps", bps);
1833  }
1834  fprintf(stderr, " %7" PRId64 " ms", stream->cx_time / 1000);
1835  fprintf(stderr, "\n");
1836 }
1837 #endif
1838 
1839 static float usec_to_fps(uint64_t usec, unsigned int frames) {
1840  return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1841 }
1842 
1843 static void test_decode(struct stream_state *stream,
1844  enum TestDecodeFatality fatal) {
1845  aom_image_t enc_img, dec_img;
1846 
1847  if (stream->mismatch_seen) return;
1848 
1849  /* Get the internal reference frame */
1851  &enc_img);
1853  &dec_img);
1854 
1855  if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1856  (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1857  if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1858  aom_image_t enc_hbd_img;
1859  aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1860  enc_img.d_w, enc_img.d_h, 16);
1861  aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1862  enc_img = enc_hbd_img;
1863  }
1864  if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1865  aom_image_t dec_hbd_img;
1866  aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1867  dec_img.d_w, dec_img.d_h, 16);
1868  aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1869  dec_img = dec_hbd_img;
1870  }
1871  }
1872 
1873  ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
1874  ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
1875 
1876  if (!aom_compare_img(&enc_img, &dec_img)) {
1877  int y[4], u[4], v[4];
1878  if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1879  aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
1880  } else {
1881  aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1882  }
1883  stream->decoder.err = 1;
1884  warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1885  "Stream %d: Encode/decode mismatch on frame %d at"
1886  " Y[%d, %d] {%d/%d},"
1887  " U[%d, %d] {%d/%d},"
1888  " V[%d, %d] {%d/%d}",
1889  stream->index, stream->frames_out, y[0], y[1], y[2],
1890  y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1891  stream->mismatch_seen = stream->frames_out;
1892  }
1893 
1894  aom_img_free(&enc_img);
1895  aom_img_free(&dec_img);
1896 }
1897 
1898 static void print_time(const char *label, int64_t etl) {
1899  int64_t hours;
1900  int64_t mins;
1901  int64_t secs;
1902 
1903  if (etl >= 0) {
1904  hours = etl / 3600;
1905  etl -= hours * 3600;
1906  mins = etl / 60;
1907  etl -= mins * 60;
1908  secs = etl;
1909 
1910  fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1911  hours, mins, secs);
1912  } else {
1913  fprintf(stderr, "[%3s unknown] ", label);
1914  }
1915 }
1916 
1917 static void clear_stream_count_state(struct stream_state *stream) {
1918  // PSNR counters
1919  for (int k = 0; k < 2; k++) {
1920  stream->psnr_sse_total[k] = 0;
1921  stream->psnr_samples_total[k] = 0;
1922  for (int i = 0; i < 4; i++) {
1923  stream->psnr_totals[k][i] = 0;
1924  }
1925  stream->psnr_count[k] = 0;
1926  }
1927  // q hist
1928  memset(stream->counts, 0, sizeof(stream->counts));
1929 }
1930 
1931 // aomenc will downscale the second pass if:
1932 // 1. the specific pass is not given by commandline (aomenc will perform all
1933 // passes)
1934 // 2. there are more than 2 passes in total
1935 // 3. current pass is the second pass (the parameter pass starts with 0 so
1936 // pass == 1)
1937 static int pass_need_downscale(int global_pass, int global_passes, int pass) {
1938  return !global_pass && global_passes > 2 && pass == 1;
1939 }
1940 
1941 int main(int argc, const char **argv_) {
1942  int pass;
1943  aom_image_t raw;
1944  aom_image_t raw_shift;
1945  int allocated_raw_shift = 0;
1946  int do_16bit_internal = 0;
1947  int input_shift = 0;
1948  int frame_avail, got_data;
1949 
1950  struct AvxInputContext input;
1951  struct AvxEncoderConfig global;
1952  struct stream_state *streams = NULL;
1953  char **argv, **argi;
1954  uint64_t cx_time = 0;
1955  int stream_cnt = 0;
1956  int res = 0;
1957  int profile_updated = 0;
1958 
1959  memset(&input, 0, sizeof(input));
1960  memset(&raw, 0, sizeof(raw));
1961  exec_name = argv_[0];
1962 
1963  /* Setup default input stream settings */
1964  input.framerate.numerator = 30;
1965  input.framerate.denominator = 1;
1966  input.only_i420 = 1;
1967  input.bit_depth = 0;
1968 
1969  /* First parse the global configuration values, because we want to apply
1970  * other parameters on top of the default configuration provided by the
1971  * codec.
1972  */
1973  argv = argv_dup(argc - 1, argv_ + 1);
1974  parse_global_config(&global, &argv);
1975 
1976  if (argc < 2) usage_exit();
1977 
1978  switch (global.color_type) {
1979  case I420: input.fmt = AOM_IMG_FMT_I420; break;
1980  case I422: input.fmt = AOM_IMG_FMT_I422; break;
1981  case I444: input.fmt = AOM_IMG_FMT_I444; break;
1982  case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
1983  }
1984 
1985  {
1986  /* Now parse each stream's parameters. Using a local scope here
1987  * due to the use of 'stream' as loop variable in FOREACH_STREAM
1988  * loops
1989  */
1990  struct stream_state *stream = NULL;
1991 
1992  do {
1993  stream = new_stream(&global, stream);
1994  stream_cnt++;
1995  if (!streams) streams = stream;
1996  } while (parse_stream_params(&global, stream, argv));
1997  }
1998 
1999  /* Check for unrecognized options */
2000  for (argi = argv; *argi; argi++)
2001  if (argi[0][0] == '-' && argi[0][1])
2002  die("Error: Unrecognized option %s\n", *argi);
2003 
2004  FOREACH_STREAM(stream, streams) {
2005  check_encoder_config(global.disable_warning_prompt, &global,
2006  &stream->config.cfg);
2007 
2008  // If large_scale_tile = 1, only support to output to ivf format.
2009  if (stream->config.cfg.large_scale_tile && !stream->config.write_ivf)
2010  die("only support ivf output format while large-scale-tile=1\n");
2011  }
2012 
2013  /* Handle non-option arguments */
2014  input.filename = argv[0];
2015  const char *orig_input_filename = input.filename;
2016  FOREACH_STREAM(stream, streams) {
2017  stream->orig_out_fn = stream->config.out_fn;
2018  stream->orig_width = stream->config.cfg.g_w;
2019  stream->orig_height = stream->config.cfg.g_h;
2020  stream->orig_write_ivf = stream->config.write_ivf;
2021  stream->orig_write_webm = stream->config.write_webm;
2022  }
2023 
2024  if (!input.filename) {
2025  fprintf(stderr, "No input file specified!\n");
2026  usage_exit();
2027  }
2028 
2029  /* Decide if other chroma subsamplings than 4:2:0 are supported */
2030  if (get_fourcc_by_aom_encoder(global.codec) == AV1_FOURCC)
2031  input.only_i420 = 0;
2032 
2033  for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
2034  if (pass > 1) {
2035  FOREACH_STREAM(stream, streams) { clear_stream_count_state(stream); }
2036  }
2037 
2038  int frames_in = 0, seen_frames = 0;
2039  int64_t estimated_time_left = -1;
2040  int64_t average_rate = -1;
2041  int64_t lagged_count = 0;
2042  const int need_downscale =
2043  pass_need_downscale(global.pass, global.passes, pass);
2044 
2045  // Set the output to the specified two-pass output file, and
2046  // restore the width and height to the original values.
2047  FOREACH_STREAM(stream, streams) {
2048  if (need_downscale) {
2049  stream->config.out_fn = stream->config.two_pass_output;
2050  // Libaom currently only supports the ivf format for the third pass.
2051  stream->config.write_ivf = 1;
2052  stream->config.write_webm = 0;
2053  } else {
2054  stream->config.out_fn = stream->orig_out_fn;
2055  stream->config.write_ivf = stream->orig_write_ivf;
2056  stream->config.write_webm = stream->orig_write_webm;
2057  }
2058  stream->config.cfg.g_w = stream->orig_width;
2059  stream->config.cfg.g_h = stream->orig_height;
2060  }
2061 
2062  // For second pass in three-pass encoding, set the input to
2063  // the given two-pass-input file if available. If the scaled input is not
2064  // given, we will attempt to re-scale the original input.
2065  input.filename = orig_input_filename;
2066  const char *two_pass_input = NULL;
2067  if (need_downscale) {
2068  FOREACH_STREAM(stream, streams) {
2069  if (stream->config.two_pass_input) {
2070  two_pass_input = stream->config.two_pass_input;
2071  input.filename = two_pass_input;
2072  break;
2073  }
2074  }
2075  }
2076 
2077  open_input_file(&input, global.csp);
2078 
2079  /* If the input file doesn't specify its w/h (raw files), try to get
2080  * the data from the first stream's configuration.
2081  */
2082  if (!input.width || !input.height) {
2083  if (two_pass_input) {
2084  FOREACH_STREAM(stream, streams) {
2085  if (stream->config.two_pass_width && stream->config.two_pass_height) {
2086  input.width = stream->config.two_pass_width;
2087  input.height = stream->config.two_pass_height;
2088  break;
2089  }
2090  }
2091  } else {
2092  FOREACH_STREAM(stream, streams) {
2093  if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2094  input.width = stream->config.cfg.g_w;
2095  input.height = stream->config.cfg.g_h;
2096  break;
2097  }
2098  }
2099  }
2100  }
2101 
2102  /* Update stream configurations from the input file's parameters */
2103  if (!input.width || !input.height) {
2104  if (two_pass_input) {
2105  fatal(
2106  "Specify downscaled stream dimensions with --two-pass-width "
2107  " and --two-pass-height");
2108  } else {
2109  fatal(
2110  "Specify stream dimensions with --width (-w) "
2111  " and --height (-h)");
2112  }
2113  }
2114 
2115  if (need_downscale) {
2116  FOREACH_STREAM(stream, streams) {
2117  if (stream->config.two_pass_width && stream->config.two_pass_height) {
2118  stream->config.cfg.g_w = stream->config.two_pass_width;
2119  stream->config.cfg.g_h = stream->config.two_pass_height;
2120  } else if (two_pass_input) {
2121  stream->config.cfg.g_w = input.width;
2122  stream->config.cfg.g_h = input.height;
2123  } else if (stream->orig_width && stream->orig_height) {
2124  stream->config.cfg.g_w = (stream->orig_width + 1) / 2;
2125  stream->config.cfg.g_h = (stream->orig_height + 1) / 2;
2126  } else {
2127  stream->config.cfg.g_w = (input.width + 1) / 2;
2128  stream->config.cfg.g_h = (input.height + 1) / 2;
2129  }
2130  }
2131  }
2132 
2133  /* If input file does not specify bit-depth but input-bit-depth parameter
2134  * exists, assume that to be the input bit-depth. However, if the
2135  * input-bit-depth paramter does not exist, assume the input bit-depth
2136  * to be the same as the codec bit-depth.
2137  */
2138  if (!input.bit_depth) {
2139  FOREACH_STREAM(stream, streams) {
2140  if (stream->config.cfg.g_input_bit_depth)
2141  input.bit_depth = stream->config.cfg.g_input_bit_depth;
2142  else
2143  input.bit_depth = stream->config.cfg.g_input_bit_depth =
2144  (int)stream->config.cfg.g_bit_depth;
2145  }
2146  if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
2147  } else {
2148  FOREACH_STREAM(stream, streams) {
2149  stream->config.cfg.g_input_bit_depth = input.bit_depth;
2150  }
2151  }
2152 
2153  FOREACH_STREAM(stream, streams) {
2154  if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2155  /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2156  was selected. */
2157  switch (stream->config.cfg.g_profile) {
2158  case 0:
2159  if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2160  input.fmt == AOM_IMG_FMT_I44416)) {
2161  if (!stream->config.cfg.monochrome) {
2162  stream->config.cfg.g_profile = 1;
2163  profile_updated = 1;
2164  }
2165  } else if (input.bit_depth == 12 ||
2166  ((input.fmt == AOM_IMG_FMT_I422 ||
2167  input.fmt == AOM_IMG_FMT_I42216) &&
2168  !stream->config.cfg.monochrome)) {
2169  stream->config.cfg.g_profile = 2;
2170  profile_updated = 1;
2171  }
2172  break;
2173  case 1:
2174  if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2175  input.fmt == AOM_IMG_FMT_I42216) {
2176  stream->config.cfg.g_profile = 2;
2177  profile_updated = 1;
2178  } else if (input.bit_depth < 12 &&
2179  (input.fmt == AOM_IMG_FMT_I420 ||
2180  input.fmt == AOM_IMG_FMT_I42016)) {
2181  stream->config.cfg.g_profile = 0;
2182  profile_updated = 1;
2183  }
2184  break;
2185  case 2:
2186  if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2187  input.fmt == AOM_IMG_FMT_I44416)) {
2188  stream->config.cfg.g_profile = 1;
2189  profile_updated = 1;
2190  } else if (input.bit_depth < 12 &&
2191  (input.fmt == AOM_IMG_FMT_I420 ||
2192  input.fmt == AOM_IMG_FMT_I42016)) {
2193  stream->config.cfg.g_profile = 0;
2194  profile_updated = 1;
2195  } else if (input.bit_depth == 12 &&
2196  input.file_type == FILE_TYPE_Y4M) {
2197  // Note that here the input file values for chroma subsampling
2198  // are used instead of those from the command line.
2199  AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2201  input.y4m.dst_c_dec_h >> 1);
2202  AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2204  input.y4m.dst_c_dec_v >> 1);
2205  } else if (input.bit_depth == 12 &&
2206  input.file_type == FILE_TYPE_RAW) {
2207  AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2209  stream->chroma_subsampling_x);
2210  AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2212  stream->chroma_subsampling_y);
2213  }
2214  break;
2215  default: break;
2216  }
2217  }
2218  /* Automatically set the codec bit depth to match the input bit depth.
2219  * Upgrade the profile if required. */
2220  if (stream->config.cfg.g_input_bit_depth >
2221  (unsigned int)stream->config.cfg.g_bit_depth) {
2222  stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2223  if (!global.quiet) {
2224  fprintf(stderr,
2225  "Warning: automatically updating bit depth to %d to "
2226  "match input format.\n",
2227  stream->config.cfg.g_input_bit_depth);
2228  }
2229  }
2230 #if !CONFIG_AV1_HIGHBITDEPTH
2231  if (stream->config.cfg.g_bit_depth > 8) {
2232  fatal("Unsupported bit-depth with CONFIG_AV1_HIGHBITDEPTH=0\n");
2233  }
2234 #endif // CONFIG_AV1_HIGHBITDEPTH
2235  if (stream->config.cfg.g_bit_depth > 10) {
2236  switch (stream->config.cfg.g_profile) {
2237  case 0:
2238  case 1:
2239  stream->config.cfg.g_profile = 2;
2240  profile_updated = 1;
2241  break;
2242  default: break;
2243  }
2244  }
2245  if (stream->config.cfg.g_bit_depth > 8) {
2246  stream->config.use_16bit_internal = 1;
2247  }
2248  if (profile_updated && !global.quiet) {
2249  fprintf(stderr,
2250  "Warning: automatically updating to profile %d to "
2251  "match input format.\n",
2252  stream->config.cfg.g_profile);
2253  }
2254  if ((global.show_psnr == 2) && (stream->config.cfg.g_input_bit_depth ==
2255  stream->config.cfg.g_bit_depth)) {
2256  fprintf(stderr,
2257  "Warning: --psnr==2 and --psnr==1 will provide same "
2258  "results when input bit-depth == stream bit-depth, "
2259  "falling back to default psnr value\n");
2260  global.show_psnr = 1;
2261  }
2262  if (global.show_psnr < 0 || global.show_psnr > 2) {
2263  fprintf(stderr,
2264  "Warning: --psnr can take only 0,1,2 as values,"
2265  "falling back to default psnr value\n");
2266  global.show_psnr = 1;
2267  }
2268  /* Set limit */
2269  stream->config.cfg.g_limit = global.limit;
2270  }
2271 
2272  FOREACH_STREAM(stream, streams) {
2273  set_stream_dimensions(stream, input.width, input.height);
2274  stream->config.color_range = input.color_range;
2275  }
2276  FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
2277 
2278  /* Ensure that --passes and --pass are consistent. If --pass is set and
2279  * --passes >= 2, ensure --fpf was set.
2280  */
2281  if (global.pass > 0 && global.pass <= 3 && global.passes >= 2) {
2282  FOREACH_STREAM(stream, streams) {
2283  if (!stream->config.stats_fn)
2284  die("Stream %d: Must specify --fpf when --pass=%d"
2285  " and --passes=%d\n",
2286  stream->index, global.pass, global.passes);
2287  }
2288  }
2289 
2290 #if !CONFIG_WEBM_IO
2291  FOREACH_STREAM(stream, streams) {
2292  if (stream->config.write_webm) {
2293  stream->config.write_webm = 0;
2294  stream->config.write_ivf = 0;
2295  aom_tools_warn("aomenc compiled w/o WebM support. Writing OBU stream.");
2296  }
2297  }
2298 #endif
2299 
2300  /* Use the frame rate from the file only if none was specified
2301  * on the command-line.
2302  */
2303  if (!global.have_framerate) {
2304  global.framerate.num = input.framerate.numerator;
2305  global.framerate.den = input.framerate.denominator;
2306  }
2307  FOREACH_STREAM(stream, streams) {
2308  stream->config.cfg.g_timebase.den = global.framerate.num;
2309  stream->config.cfg.g_timebase.num = global.framerate.den;
2310  }
2311  /* Show configuration */
2312  if (global.verbose && pass == 0) {
2313  FOREACH_STREAM(stream, streams) {
2314  show_stream_config(stream, &global, &input);
2315  }
2316  }
2317 
2318  if (pass == (global.pass ? global.pass - 1 : 0)) {
2319  // The Y4M reader does its own allocation.
2320  if (input.file_type != FILE_TYPE_Y4M) {
2321  aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
2322  }
2323  FOREACH_STREAM(stream, streams) {
2324  stream->rate_hist =
2325  init_rate_histogram(&stream->config.cfg, &global.framerate);
2326  }
2327  }
2328 
2329  FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2330  FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
2331  FOREACH_STREAM(stream, streams) {
2332  char *encoder_settings = NULL;
2333 #if CONFIG_WEBM_IO
2334  // Test frameworks may compare outputs from different versions, but only
2335  // wish to check for bitstream changes. The encoder-settings tag, however,
2336  // can vary if the version is updated, even if no encoder algorithm
2337  // changes were made. To work around this issue, do not output
2338  // the encoder-settings tag when --debug is enabled (which is the flag
2339  // that test frameworks should use, when they want deterministic output
2340  // from the container format).
2341  if (stream->config.write_webm && !stream->webm_ctx.debug) {
2342  encoder_settings = extract_encoder_settings(
2343  aom_codec_version_str(), argv_, argc, input.filename);
2344  if (encoder_settings == NULL) {
2345  fprintf(
2346  stderr,
2347  "Warning: unable to extract encoder settings. Continuing...\n");
2348  }
2349  }
2350 #endif
2351  open_output_file(stream, &global, &input.pixel_aspect_ratio,
2352  encoder_settings);
2353  free(encoder_settings);
2354  }
2355 
2356  if (strcmp(get_short_name_by_aom_encoder(global.codec), "av1") == 0) {
2357  // Check to see if at least one stream uses 16 bit internal.
2358  // Currently assume that the bit_depths for all streams using
2359  // highbitdepth are the same.
2360  FOREACH_STREAM(stream, streams) {
2361  if (stream->config.use_16bit_internal) {
2362  do_16bit_internal = 1;
2363  }
2364  input_shift = (int)stream->config.cfg.g_bit_depth -
2365  stream->config.cfg.g_input_bit_depth;
2366  };
2367  }
2368 
2369  frame_avail = 1;
2370  got_data = 0;
2371 
2372  while (frame_avail || got_data) {
2373  struct aom_usec_timer timer;
2374 
2375  if (!global.limit || frames_in < global.limit) {
2376  frame_avail = read_frame(&input, &raw);
2377 
2378  if (frame_avail) frames_in++;
2379  seen_frames =
2380  frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
2381 
2382  if (!global.quiet) {
2383  float fps = usec_to_fps(cx_time, seen_frames);
2384  fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2385 
2386  if (stream_cnt == 1)
2387  fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2388  streams->frames_out, (int64_t)streams->nbytes);
2389  else
2390  fprintf(stderr, "frame %4d ", frames_in);
2391 
2392  fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
2393  cx_time > 9999999 ? cx_time / 1000 : cx_time,
2394  cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
2395  fps >= 1.0 ? "fps" : "fpm");
2396  print_time("ETA", estimated_time_left);
2397  // mingw-w64 gcc does not match msvc for stderr buffering behavior
2398  // and uses line buffering, thus the progress output is not
2399  // real-time. The fflush() is here to make sure the progress output
2400  // is sent out while the clip is being processed.
2401  fflush(stderr);
2402  }
2403 
2404  } else {
2405  frame_avail = 0;
2406  }
2407 
2408  if (frames_in > global.skip_frames) {
2409  aom_image_t *frame_to_encode;
2410  if (input_shift || (do_16bit_internal && input.bit_depth == 8)) {
2411  assert(do_16bit_internal);
2412  // Input bit depth and stream bit depth do not match, so up
2413  // shift frame to stream bit depth
2414  if (!allocated_raw_shift) {
2415  aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
2416  input.width, input.height, 32);
2417  allocated_raw_shift = 1;
2418  }
2419  aom_img_upshift(&raw_shift, &raw, input_shift);
2420  frame_to_encode = &raw_shift;
2421  } else {
2422  frame_to_encode = &raw;
2423  }
2424  aom_usec_timer_start(&timer);
2425  if (do_16bit_internal) {
2426  assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
2427  FOREACH_STREAM(stream, streams) {
2428  if (stream->config.use_16bit_internal)
2429  encode_frame(stream, &global,
2430  frame_avail ? frame_to_encode : NULL, frames_in);
2431  else
2432  assert(0);
2433  };
2434  } else {
2435  assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
2436  FOREACH_STREAM(stream, streams) {
2437  encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2438  frames_in);
2439  }
2440  }
2441  aom_usec_timer_mark(&timer);
2442  cx_time += aom_usec_timer_elapsed(&timer);
2443 
2444  FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
2445 
2446  got_data = 0;
2447  FOREACH_STREAM(stream, streams) {
2448  get_cx_data(stream, &global, &got_data);
2449  }
2450 
2451  if (!got_data && input.length && streams != NULL &&
2452  !streams->frames_out) {
2453  lagged_count = global.limit ? seen_frames : ftello(input.file);
2454  } else if (input.length) {
2455  int64_t remaining;
2456  int64_t rate;
2457 
2458  if (global.limit) {
2459  const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
2460 
2461  rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
2462  remaining = 1000 * (global.limit - global.skip_frames -
2463  seen_frames + lagged_count);
2464  } else {
2465  const int64_t input_pos = ftello(input.file);
2466  const int64_t input_pos_lagged = input_pos - lagged_count;
2467  const int64_t input_limit = input.length;
2468 
2469  rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
2470  remaining = input_limit - input_pos + lagged_count;
2471  }
2472 
2473  average_rate =
2474  (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
2475  estimated_time_left = average_rate ? remaining / average_rate : -1;
2476  }
2477 
2478  if (got_data && global.test_decode != TEST_DECODE_OFF) {
2479  FOREACH_STREAM(stream, streams) {
2480  test_decode(stream, global.test_decode);
2481  }
2482  }
2483  }
2484 
2485  fflush(stdout);
2486  if (!global.quiet) fprintf(stderr, "\033[K");
2487  }
2488 
2489  if (stream_cnt > 1) fprintf(stderr, "\n");
2490 
2491  if (!global.quiet) {
2492  FOREACH_STREAM(stream, streams) {
2493  const int64_t bpf =
2494  seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0;
2495  const int64_t bps = bpf * global.framerate.num / global.framerate.den;
2496  fprintf(stderr,
2497  "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2498  "b/f %7" PRId64
2499  "b/s"
2500  " %7" PRId64 " %s (%.2f fps)\033[K\n",
2501  pass + 1, global.passes, frames_in, stream->frames_out,
2502  (int64_t)stream->nbytes, bpf, bps,
2503  stream->cx_time > 9999999 ? stream->cx_time / 1000
2504  : stream->cx_time,
2505  stream->cx_time > 9999999 ? "ms" : "us",
2506  usec_to_fps(stream->cx_time, seen_frames));
2507  // This instance of cr does not need fflush as it is followed by a
2508  // newline in the same string.
2509  }
2510  }
2511 
2512  if (global.show_psnr >= 1) {
2513  if (get_fourcc_by_aom_encoder(global.codec) == AV1_FOURCC) {
2514  FOREACH_STREAM(stream, streams) {
2515  int64_t bps = 0;
2516  if (global.show_psnr == 1) {
2517  if (stream->psnr_count[0] && seen_frames && global.framerate.den) {
2518  bps = (int64_t)stream->nbytes * 8 *
2519  (int64_t)global.framerate.num / global.framerate.den /
2520  seen_frames;
2521  }
2522  show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1,
2523  bps);
2524  }
2525  if (global.show_psnr == 2) {
2526 #if CONFIG_AV1_HIGHBITDEPTH
2527  if (stream->config.cfg.g_input_bit_depth <
2528  (unsigned int)stream->config.cfg.g_bit_depth)
2529  show_psnr_hbd(stream, (1 << stream->config.cfg.g_bit_depth) - 1,
2530  bps);
2531 #endif
2532  }
2533  }
2534  } else {
2535  FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0, 0); }
2536  }
2537  }
2538 
2539  FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
2540 
2541  if (global.test_decode != TEST_DECODE_OFF) {
2542  FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
2543  }
2544 
2545  close_input_file(&input);
2546 
2547  if (global.test_decode == TEST_DECODE_FATAL) {
2548  FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
2549  }
2550  FOREACH_STREAM(stream, streams) {
2551  close_output_file(stream, get_fourcc_by_aom_encoder(global.codec));
2552  }
2553 
2554  FOREACH_STREAM(stream, streams) {
2555  stats_close(&stream->stats, global.passes - 1);
2556  }
2557 
2558  if (global.pass) break;
2559  }
2560 
2561  if (global.show_q_hist_buckets) {
2562  FOREACH_STREAM(stream, streams) {
2563  show_q_histogram(stream->counts, global.show_q_hist_buckets);
2564  }
2565  }
2566 
2567  if (global.show_rate_hist_buckets) {
2568  FOREACH_STREAM(stream, streams) {
2569  show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2570  global.show_rate_hist_buckets);
2571  }
2572  }
2573  FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
2574 
2575 #if CONFIG_INTERNAL_STATS
2576  /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2577  * to match some existing utilities.
2578  */
2579  if (!(global.pass == 1 && global.passes == 2)) {
2580  FOREACH_STREAM(stream, streams) {
2581  FILE *f = fopen("opsnr.stt", "a");
2582  if (stream->mismatch_seen) {
2583  fprintf(f, "First mismatch occurred in frame %d\n",
2584  stream->mismatch_seen);
2585  } else {
2586  fprintf(f, "No mismatch detected in recon buffers\n");
2587  }
2588  fclose(f);
2589  }
2590  }
2591 #endif
2592 
2593  if (allocated_raw_shift) aom_img_free(&raw_shift);
2594  aom_img_free(&raw);
2595  free(argv);
2596  free(streams);
2597  return res ? EXIT_FAILURE : EXIT_SUCCESS;
2598 }
#define AOM_PLANE_U
Definition: aom_image.h:200
void * buf
Definition: aom_encoder.h:78
Control to use dct only for intra modes, int parameter.
Definition: aomcx.h:1185
Codec control function to set max data rate for intra frames, unsigned int parameter.
Definition: aomcx.h:300
unsigned char * img_data
Definition: aom_image.h:217
unsigned int d_h
Definition: aom_image.h:187
#define AOM_PLANE_V
Definition: aom_image.h:201
Codec control function to turn on / off Paeth intra mode usage, int parameter.
Definition: aomcx.h:1067
Codec control function to set an MTU size for a tile group, unsigned int parameter.
Definition: aomcx.h:789
Codec control function to encode with CDEF, unsigned int parameter.
Definition: aomcx.h:659
unsigned int min_partition_size
min partition size 8, 16, 32, 64, 128
Definition: aom_encoder.h:232
unsigned int g_w
Width of the frame.
Definition: aom_encoder.h:415
Codec control function to turn on / off directional intra mode usage, int parameter.
Definition: aomcx.h:1366
#define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data)
aom_codec_control wrapper macro (adds type-checking, less flexible)
Definition: aom_codec.h:520
Codec control function to turn on / off frame order hint (int parameter). Affects: joint compound mod...
Definition: aomcx.h:854
int64_t aom_codec_pts_t
Time Stamp Type.
Definition: aom_codec.h:235
Describes the encoder algorithm interface to applications.
Definition: aom_image.h:133
Codec control function to set max partition size, int parameter.
Definition: aomcx.h:837
Codec control function to set noise sensitivity, unsigned int parameter.
Definition: aomcx.h:482
Codec control function to encode with quantisation matrices, unsigned int parameter.
Definition: aomcx.h:707
Codec control function to signal picture timing info in the bitstream, aom_timing_info_type_t paramet...
Definition: aomcx.h:1155
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition: aom_encoder.h:940
Encoder configuration structure.
Definition: aom_encoder.h:376
Codec control function to turn on / off interintra wedge compound, int parameter. ...
Definition: aomcx.h:1007
Control to use default tx type only for intra modes, int parameter.
Definition: aomcx.h:1192
aom_codec_err_t err
Definition: aom_codec.h:301
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
Codec control function to set chroma 4:2:0 sample position info, aom_chroma_sample_position_t paramet...
Definition: aomcx.h:574
Codec control function to turn on / off rectangular transforms, int parameter.
Definition: aomcx.h:901
#define AOM_CODEC_USE_PSNR
Initialization-time Feature Enabling.
Definition: aom_encoder.h:69
Codec control function to set min partition size, int parameter.
Definition: aomcx.h:826
unsigned char * planes[3]
Definition: aom_image.h:202
Codec control function to encode without trellis quantization, unsigned int parameter.
Definition: aomcx.h:696
Codec control function to set the max no of frames to create arf, unsigned int parameter.
Definition: aomcx.h:262
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.
Codec control function to set constrained / constant quality level, unsigned int parameter.
Definition: aomcx.h:286
Control to set target sequence level index for a certain operating point(OP), int parameter Possible ...
Definition: aomcx.h:626
Codec control function to indicate whether bitstream is in Annex-B format, unsigned int parameter...
Definition: aomdx.h:345
Control to set average complexity of the corpus in the case of single pass vbr based on LAP...
Definition: aomcx.h:1314
Definition: aom_image.h:50
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
Rational Number.
Definition: aom_encoder.h:153
unsigned int super_block_size
Superblock size 0, 64 or 128.
Definition: aom_encoder.h:224
Codec context structure.
Definition: aom_codec.h:298
Codec control function to turn on/off intra block copy mode, int parameter.
Definition: aomcx.h:1102
Codec control function to turn on / off warped motion usage at sequence level, int parameter...
Definition: aomcx.h:1027
Codec control function to turn on / off interinter wedge compound, int parameter. ...
Definition: aomcx.h:999
Codec control function to set the min quant matrix flatness, unsigned int parameter.
Definition: aomcx.h:720
Codec control function to enable frame parallel decoding feature, unsigned int parameter.
Definition: aomcx.h:425
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
Control to use dct only for inter modes, int parameter.
Definition: aomcx.h:1188
Codec control function to enable/disable rectangular partitions, int parameter.
Definition: aomcx.h:799
Codec control function to set transfer function info, int parameter.
Definition: aomcx.h:567
unsigned int y_chroma_shift
Definition: aom_image.h:195
Codec control function to set color range bit, int parameter.
Definition: aomcx.h:600
Describes the decoder algorithm interface to applications.
#define AOM_USAGE_GOOD_QUALITY
usage parameter analogous to AV1 GOOD QUALITY mode.
Definition: aom_encoder.h:1011
Codec control function to enable/disable AB partitions, int parameter.
Definition: aomcx.h:807
Codec control function to add film grain parameters (one of several preset types) info in the bitstre...
Definition: aomcx.h:1162
Codec control function to set max data rate for inter frames, unsigned int parameter.
Definition: aomcx.h:319
Definition: aom_image.h:49
Control to select maximum height for the GF group pyramid structure, unsigned int parameter...
Definition: aomcx.h:1202
Control to set minimum compression ratio, unsigned int parameter Take integer values. If non-zero, encoder will try to keep the compression ratio of each frame to be higher than the given value divided by 100. E.g. 850 means minimum compression ratio of 8.5.
Definition: aomcx.h:1258
int monochrome
Definition: aom_image.h:176
Image Descriptor.
Definition: aom_image.h:171
double psnr[4]
Definition: aom_encoder.h:134
Codec control function to set number of tile columns. unsigned int parameter.
Definition: aomcx.h:374
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_fixed_buf_t raw
Definition: aom_encoder.h:145
Definition: aom_image.h:54
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.
const aom_codec_cx_pkt_t * aom_codec_get_cx_data(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Encoded data iterator.
Control to select maximum reference frames allowed per frame, int parameter.
Definition: aomcx.h:1209
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.
unsigned int max_partition_size
max partition size 8, 16, 32, 64, 128
Definition: aom_encoder.h:228
#define AOM_USAGE_REALTIME
usage parameter analogous to AV1 REALTIME mode.
Definition: aom_encoder.h:1013
Sets the denoisers block size, unsigned int parameter.
Definition: aomcx.h:1173
Definition: aom_codec.h:319
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition: aom_codec.h:254
Codec control function to set the sharpness parameter, unsigned int parameter.
Definition: aomcx.h:235
Sets the noise level, int parameter.
Definition: aomcx.h:1170
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:129
Codec control function to turn on / off smooth inter-intra mode for a sequence, int parameter...
Definition: aomcx.h:983
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
struct aom_rational g_timebase
Stream timebase units.
Definition: aom_encoder.h:473
Definition: aom_encoder.h:102
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
Definition: aom_encoder.h:100
Encoder Config Options.
Definition: aom_encoder.h:216
const char * aom_codec_err_to_string(aom_codec_err_t err)
Convert error number to printable string.
Codec control function to enable temporal filtering on key frame, unsigned int parameter.
Definition: aomcx.h:411
Codec control function to turn on/off loopfilter modulation when delta q modulation is enabled...
Definition: aomcx.h:1128
Codec control function to set minimum interval between GF/ARF frames, unsigned int parameter...
Definition: aomcx.h:581
Definition: aom_image.h:53
Codec control function to turn on / off CFL uv intra mode usage, int parameter.
Definition: aomcx.h:1077
Codec control function to turn on / off flip and identity transforms, int parameter.
Definition: aomcx.h:889
Control to turn on / off transform size search.
Definition: aomcx.h:1373
enum aom_codec_cx_pkt_kind kind
Definition: aom_encoder.h:112
Codec control function to enable error_resilient_mode, int parameter.
Definition: aomcx.h:436
Codec control function to encode with Loop Restoration Filter, unsigned int parameter.
Definition: aomcx.h:669
Codec control function to set visual tuning, aom_tune_metric (int) parameter.
Definition: aomcx.h:276
Definition: aom_image.h:52
Codec control function to set the path to the film grain parameters, const char* parameter.
Definition: aomcx.h:1167
void aom_img_free(aom_image_t *img)
Close an image descriptor.
#define FIXED_QP_OFFSET_COUNT
Number of fixed QP offsets.
Definition: aom_encoder.h:885
Codec control function to turn on / off global motion usage for a sequence, int parameter.
Definition: aomcx.h:1017
Codec control function to turn on / off masked compound usage (wedge and diff-wtd compound modes) for...
Definition: aomcx.h:959
Codec control function to set minimum interval between GF/ARF frames, unsigned int parameter...
Definition: aomcx.h:588
unsigned int disable_trellis_quant
disable trellis quantization
Definition: aom_encoder.h:344
Sets the chroma subsampling y value, unsigned int parameter.
Definition: aomcx.h:1179
Codec control function to turn on / off filter intra usage at sequence level, int parameter...
Definition: aomcx.h:1048
struct aom_codec_cx_pkt::@1::@2 frame
Definition: aom_encoder.h:166
Definition: aom_encoder.h:99
Codec control function to turn on / off interintra compound for a sequence, int parameter.
Definition: aomcx.h:975
Sets the chroma subsampling x value, unsigned int parameter.
Definition: aomcx.h:1176
Codec control function to turn on / off ref frame mvs (mfmv) usage at sequence level, int parameter.
Definition: aomcx.h:924
int self_allocd
Definition: aom_image.h:219
Initialization Configurations.
Definition: aom_decoder.h:91
Codec control function to get last quantizer chosen by the encoder, int* parameter.
Definition: aomcx.h:257
#define MAX_TILE_WIDTHS
Maximum number of tile widths in tile widths array.
Definition: aom_encoder.h:845
Control to set frequency of the cost updates for intrabc motion vectors, unsigned int parameter...
Definition: aomcx.h:1347
#define MAX_TILE_HEIGHTS
Maximum number of tile heights in tile heights array.
Definition: aom_encoder.h:858
#define AOM_CODEC_USE_HIGHBITDEPTH
Make the encoder output one partition at a time.
Definition: aom_encoder.h:71
Control to set frequency of the cost updates for motion vectors, unsigned int parameter.
Definition: aomcx.h:1243
Control to set bit mask that specifies which tier each of the 32 possible operating points conforms t...
Definition: aomcx.h:1251
Codec control function to set lossless encoding mode, unsigned int parameter.
Definition: aomcx.h:347
Codec control function to turn on/off palette mode, int parameter.
Definition: aomcx.h:1098
Control to set frequency of the cost updates for mode, unsigned int parameter.
Definition: aomcx.h:1233
Codec control function to get a pointer to the new frame.
Definition: aom.h:70
Codec control function to set encoder internal speed settings, int parameter.
Definition: aomcx.h:214
Codec control function to enable RDO modulated by frame temporal dependency, unsigned int parameter...
Definition: aomcx.h:402
Codec control function to enable the row based multi-threading of the encoder, unsigned int parameter...
Definition: aomcx.h:355
Codec control function to turn on / off dual interpolation filter for a sequence, int parameter...
Definition: aomcx.h:943
aom_chroma_sample_position_t csp
Definition: aom_image.h:177
const char * aom_codec_version_str(void)
Return the version information (as a string)
#define AOM_USAGE_ALL_INTRA
usage parameter analogous to AV1 all intra mode.
Definition: aom_encoder.h:1015
Codec control function to set the delta q mode, unsigned int parameter.
Definition: aomcx.h:1120
Codec control function to force video mode, unsigned int parameter.
Definition: aomcx.h:676
Codec control function to turn on / off dist-wtd compound mode at sequence level, int parameter...
Definition: aomcx.h:913
Codec control function to turn on / off 64-length transforms, int parameter.
Definition: aomcx.h:865
Codec control function to turn on / off one sided compound usage for a sequence, int parameter...
Definition: aomcx.h:967
unsigned int monochrome
Monochrome mode.
Definition: aom_encoder.h:806
aom_codec_err_t aom_codec_set_option(aom_codec_ctx_t *ctx, const char *name, const char *value)
Key & Value API.
Codec control function to set the filter strength for the arf, unsigned int parameter.
Definition: aomcx.h:267
Control to use adaptive quantize_b, int parameter.
Definition: aomcx.h:1195
Codec control function to enable automatic set and use alf frames, unsigned int parameter.
Definition: aomcx.h:222
Codec control function to turn on / off intra edge filter at sequence level, int parameter.
Definition: aomcx.h:845
Codec control function to set the range of tile decoding, int parameter.
Definition: aomdx.h:301
Control to use a reduced tx type set, int parameter.
Definition: aomcx.h:1182
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:288
unsigned int x_chroma_shift
Definition: aom_image.h:194
Codec control function to set the tile coding mode, int parameter.
Definition: aomdx.h:309
size_t sz
Definition: aom_image.h:204
Codec control function to set the path to the VMAF model used when tuning the encoder for VMAF...
Definition: aomcx.h:1281
Codec control function to turn on / off difference weighted compound, int parameter.
Definition: aomcx.h:991
enum aom_color_range aom_color_range_t
List of supported color range.
Codec control function to turn on / off smooth intra modes usage, int parameter.
Definition: aomcx.h:1059
Codec control function to turn on / off overlay frames for filtered ALTREF frames, int parameter.
Definition: aomcx.h:1095
int bps
Definition: aom_image.h:206
Codec control function to set number of tile rows, unsigned int parameter.
Definition: aomcx.h:392
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:155
const char * aom_codec_error(aom_codec_ctx_t *ctx)
Retrieve error synopsis for codec context.
Definition: aom_image.h:51
Control to set frequency of the cost updates for coefficients, unsigned int parameter.
Definition: aomcx.h:1223
enum aom_chroma_sample_position aom_chroma_sample_position_t
List of chroma sample positions.
Provides definitions for using AOM or AV1 within the aom Decoder interface.
int stride[3]
Definition: aom_image.h:203
aom_fixed_buf_t twopass_stats
Definition: aom_encoder.h:129
int den
Definition: aom_encoder.h:155
Codec control function to set content type, aom_tune_content parameter.
Definition: aomcx.h:491
Codec control function to set the max quant matrix flatness, unsigned int parameter.
Definition: aomcx.h:732
Codec control function to set color space info, int parameter.
Definition: aomcx.h:521
Codec control function to set adaptive quantization mode, unsigned int parameter. ...
Definition: aomcx.h:462
Encoder output packet.
Definition: aom_encoder.h:111
Definition: aom_encoder.h:169
Control to select minimum height for the GF group pyramid structure, unsigned int parameter...
Definition: aomcx.h:1309
Codec control function to predict with OBMC mode, unsigned int parameter.
Definition: aomcx.h:686
int num
Definition: aom_encoder.h:154
Codec control to set the path for partition stats read and write. const char * parameter.
Definition: aomcx.h:1352
Boost percentage for Golden Frame in CBR mode, unsigned int parameter.
Definition: aomcx.h:333
Definition: aom_image.h:45
Codec control function to turn on / off D45 to D203 intra mode usage, int parameter.
Definition: aomcx.h:1337
Codec control function to enable/disable 1:4 and 4:1 partitions, int parameter.
Definition: aomcx.h:815
Codec control function to set CDF update mode, unsigned int parameter.
Definition: aomcx.h:500
Codec control function to set transfer function info, int parameter.
Definition: aomcx.h:546
const char * aom_codec_error_detail(aom_codec_ctx_t *ctx)
Retrieve detailed error information for codec context.
Codec control function to turn on/off intra angle delta, int parameter.
Definition: aomcx.h:1106
union aom_codec_cx_pkt::@1 data
Codec control function to set the threshold for MBs treated static, unsigned int parameter.
Definition: aomcx.h:240
size_t sz
Definition: aom_encoder.h:79
Codec control function to set intended superblock size, unsigned int parameter.
Definition: aomcx.h:641
unsigned int d_w
Definition: aom_image.h:186
Codec control function to enable/disable periodic Q boost, unsigned int parameter.
Definition: aomcx.h:474
Definition: aom_encoder.h:167
Codec control function to set a maximum number of tile groups, unsigned int parameter.
Definition: aomcx.h:778
Definition: aom_encoder.h:192
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: aom_encoder.h:488
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
unsigned int g_h
Height of the frame.
Definition: aom_encoder.h:424
Definition: aom_image.h:43
aom_img_fmt_t fmt
Definition: aom_image.h:172
Codec control function to turn on / off delta quantization in chroma planes for a sequence...
Definition: aomcx.h:951
Definition: aom_encoder.h:168
#define AOM_PLANE_Y
Definition: aom_image.h:199
Control to use reduced set of single and compound references, int parameter.
Definition: aomcx.h:1213
int img_data_owner
Definition: aom_image.h:218