LCOV - code coverage report
Current view: top level - third_party/aom/av1 - av1_cx_iface.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 603 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 62 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <stdlib.h>
      13             : #include <string.h>
      14             : 
      15             : #include "./aom_config.h"
      16             : #include "aom/aom_encoder.h"
      17             : #include "aom_ports/aom_once.h"
      18             : #include "aom_ports/system_state.h"
      19             : #include "aom/internal/aom_codec_internal.h"
      20             : #include "./aom_version.h"
      21             : #include "av1/encoder/encoder.h"
      22             : #include "aom/aomcx.h"
      23             : #include "av1/encoder/firstpass.h"
      24             : #include "av1/av1_iface_common.h"
      25             : 
      26             : struct av1_extracfg {
      27             :   int cpu_used;  // available cpu percentage in 1/16
      28             :   unsigned int enable_auto_alt_ref;
      29             : #if CONFIG_EXT_REFS
      30             :   unsigned int enable_auto_bwd_ref;
      31             : #endif  // CONFIG_EXT_REFS
      32             :   unsigned int noise_sensitivity;
      33             :   unsigned int sharpness;
      34             :   unsigned int static_thresh;
      35             :   unsigned int tile_columns;
      36             :   unsigned int tile_rows;
      37             : #if CONFIG_DEPENDENT_HORZTILES
      38             :   unsigned int dependent_horz_tiles;
      39             : #endif
      40             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
      41             :   unsigned int loop_filter_across_tiles_enabled;
      42             : #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
      43             :   unsigned int arnr_max_frames;
      44             :   unsigned int arnr_strength;
      45             :   unsigned int min_gf_interval;
      46             :   unsigned int max_gf_interval;
      47             :   aom_tune_metric tuning;
      48             :   unsigned int cq_level;  // constrained quality level
      49             :   unsigned int rc_max_intra_bitrate_pct;
      50             :   unsigned int rc_max_inter_bitrate_pct;
      51             :   unsigned int gf_cbr_boost_pct;
      52             :   unsigned int lossless;
      53             : #if CONFIG_AOM_QM
      54             :   unsigned int enable_qm;
      55             :   unsigned int qm_min;
      56             :   unsigned int qm_max;
      57             : #endif
      58             : #if CONFIG_TILE_GROUPS
      59             :   unsigned int num_tg;
      60             :   unsigned int mtu_size;
      61             : #endif
      62             : #if CONFIG_TEMPMV_SIGNALING
      63             :   unsigned int disable_tempmv;
      64             : #endif
      65             :   unsigned int frame_parallel_decoding_mode;
      66             :   AQ_MODE aq_mode;
      67             : #if CONFIG_EXT_DELTA_Q
      68             :   DELTAQ_MODE deltaq_mode;
      69             : #endif
      70             :   unsigned int frame_periodic_boost;
      71             :   aom_bit_depth_t bit_depth;
      72             :   aom_tune_content content;
      73             :   aom_color_space_t color_space;
      74             :   int color_range;
      75             :   int render_width;
      76             :   int render_height;
      77             :   aom_superblock_size_t superblock_size;
      78             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
      79             :   int ans_window_size_log2;
      80             : #endif
      81             : #if CONFIG_EXT_TILE
      82             :   unsigned int tile_encoding_mode;
      83             : #endif  // CONFIG_EXT_TILE
      84             : 
      85             :   unsigned int motion_vector_unit_test;
      86             : };
      87             : 
      88             : static struct av1_extracfg default_extra_cfg = {
      89             :   0,  // cpu_used
      90             :   1,  // enable_auto_alt_ref
      91             : #if CONFIG_EXT_REFS
      92             :   0,    // enable_auto_bwd_ref
      93             : #endif  // CONFIG_EXT_REFS
      94             :   0,    // noise_sensitivity
      95             :   0,    // sharpness
      96             :   0,    // static_thresh
      97             : #if CONFIG_EXT_TILE
      98             :   UINT_MAX,  // tile_columns
      99             :   UINT_MAX,  // tile_rows
     100             : #else
     101             :   0,  // tile_columns
     102             :   0,  // tile_rows
     103             : #endif  // CONFIG_EXT_TILE
     104             : #if CONFIG_DEPENDENT_HORZTILES
     105             :   0,  // Depdendent Horizontal tiles
     106             : #endif
     107             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
     108             :   1,              // loop_filter_across_tiles_enabled
     109             : #endif            // CONFIG_LOOPFILTERING_ACROSS_TILES
     110             :   7,              // arnr_max_frames
     111             :   5,              // arnr_strength
     112             :   0,              // min_gf_interval; 0 -> default decision
     113             :   0,              // max_gf_interval; 0 -> default decision
     114             :   AOM_TUNE_PSNR,  // tuning
     115             :   10,             // cq_level
     116             :   0,              // rc_max_intra_bitrate_pct
     117             :   0,              // rc_max_inter_bitrate_pct
     118             :   0,              // gf_cbr_boost_pct
     119             :   0,              // lossless
     120             : #if CONFIG_AOM_QM
     121             :   0,                 // enable_qm
     122             :   DEFAULT_QM_FIRST,  // qm_min
     123             :   DEFAULT_QM_LAST,   // qm_max
     124             : #endif
     125             : #if CONFIG_TILE_GROUPS
     126             :   1,  // max number of tile groups
     127             :   0,  // mtu_size
     128             : #endif
     129             : #if CONFIG_TEMPMV_SIGNALING
     130             :   0,  // disable temporal mv prediction
     131             : #endif
     132             :   1,      // frame_parallel_decoding_mode
     133             :   NO_AQ,  // aq_mode
     134             : #if CONFIG_EXT_DELTA_Q
     135             :   NO_DELTA_Q,  // deltaq_mode
     136             : #endif
     137             :   CONFIG_XIPHRC,                // frame_periodic_delta_q
     138             :   AOM_BITS_8,                   // Bit depth
     139             :   AOM_CONTENT_DEFAULT,          // content
     140             :   AOM_CS_UNKNOWN,               // color space
     141             :   0,                            // color range
     142             :   0,                            // render width
     143             :   0,                            // render height
     144             :   AOM_SUPERBLOCK_SIZE_DYNAMIC,  // superblock_size
     145             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
     146             :   23,  // ans_window_size_log2
     147             : #endif
     148             : #if CONFIG_EXT_TILE
     149             :   0,    // Tile encoding mode is TILE_NORMAL by default.
     150             : #endif  // CONFIG_EXT_TILE
     151             : 
     152             :   0,  // motion_vector_unit_test
     153             : };
     154             : 
     155             : struct aom_codec_alg_priv {
     156             :   aom_codec_priv_t base;
     157             :   aom_codec_enc_cfg_t cfg;
     158             :   struct av1_extracfg extra_cfg;
     159             :   AV1EncoderConfig oxcf;
     160             :   AV1_COMP *cpi;
     161             :   unsigned char *cx_data;
     162             :   size_t cx_data_sz;
     163             :   unsigned char *pending_cx_data;
     164             :   size_t pending_cx_data_sz;
     165             :   int pending_frame_count;
     166             :   size_t pending_frame_sizes[8];
     167             :   aom_image_t preview_img;
     168             :   aom_enc_frame_flags_t next_frame_flags;
     169             :   aom_postproc_cfg_t preview_ppcfg;
     170             :   aom_codec_pkt_list_decl(256) pkt_list;
     171             :   unsigned int fixed_kf_cntr;
     172             :   // BufferPool that holds all reference frames.
     173             :   BufferPool *buffer_pool;
     174             : };
     175             : 
     176           0 : static aom_codec_err_t update_error_state(
     177             :     aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
     178           0 :   const aom_codec_err_t res = error->error_code;
     179             : 
     180           0 :   if (res != AOM_CODEC_OK)
     181           0 :     ctx->base.err_detail = error->has_detail ? error->detail : NULL;
     182             : 
     183           0 :   return res;
     184             : }
     185             : 
     186             : #undef ERROR
     187             : #define ERROR(str)                  \
     188             :   do {                              \
     189             :     ctx->base.err_detail = str;     \
     190             :     return AOM_CODEC_INVALID_PARAM; \
     191             :   } while (0)
     192             : 
     193             : #define RANGE_CHECK(p, memb, lo, hi)                   \
     194             :   do {                                                 \
     195             :     if (!((p)->memb >= (lo) && (p)->memb <= (hi)))     \
     196             :       ERROR(#memb " out of range [" #lo ".." #hi "]"); \
     197             :   } while (0)
     198             : 
     199             : #define RANGE_CHECK_HI(p, memb, hi)                                     \
     200             :   do {                                                                  \
     201             :     if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
     202             :   } while (0)
     203             : 
     204             : #define RANGE_CHECK_LO(p, memb, lo)                                     \
     205             :   do {                                                                  \
     206             :     if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
     207             :   } while (0)
     208             : 
     209             : #define RANGE_CHECK_BOOL(p, memb)                                     \
     210             :   do {                                                                \
     211             :     if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
     212             :   } while (0)
     213             : 
     214           0 : static aom_codec_err_t validate_config(aom_codec_alg_priv_t *ctx,
     215             :                                        const aom_codec_enc_cfg_t *cfg,
     216             :                                        const struct av1_extracfg *extra_cfg) {
     217           0 :   RANGE_CHECK(cfg, g_w, 1, 65535);  // 16 bits available
     218           0 :   RANGE_CHECK(cfg, g_h, 1, 65535);  // 16 bits available
     219           0 :   RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
     220           0 :   RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
     221           0 :   RANGE_CHECK_HI(cfg, g_profile, 3);
     222             : 
     223           0 :   RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
     224           0 :   RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
     225           0 :   RANGE_CHECK_BOOL(extra_cfg, lossless);
     226           0 :   RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 1);
     227             : #if CONFIG_EXT_DELTA_Q
     228           0 :   RANGE_CHECK(extra_cfg, deltaq_mode, 0, DELTAQ_MODE_COUNT - 1);
     229             : #endif
     230           0 :   RANGE_CHECK_HI(extra_cfg, frame_periodic_boost, 1);
     231           0 :   RANGE_CHECK_HI(cfg, g_threads, 64);
     232           0 :   RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
     233           0 :   RANGE_CHECK(cfg, rc_end_usage, AOM_VBR, AOM_Q);
     234           0 :   RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
     235           0 :   RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
     236           0 :   RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
     237           0 :   RANGE_CHECK(cfg, kf_mode, AOM_KF_DISABLED, AOM_KF_AUTO);
     238           0 :   RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
     239           0 :   RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
     240           0 :   RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
     241           0 :   RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
     242           0 :   RANGE_CHECK(cfg, g_pass, AOM_RC_ONE_PASS, AOM_RC_LAST_PASS);
     243           0 :   RANGE_CHECK_HI(extra_cfg, min_gf_interval, MAX_LAG_BUFFERS - 1);
     244           0 :   RANGE_CHECK_HI(extra_cfg, max_gf_interval, MAX_LAG_BUFFERS - 1);
     245           0 :   if (extra_cfg->max_gf_interval > 0) {
     246           0 :     RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
     247             :   }
     248           0 :   if (extra_cfg->min_gf_interval > 0 && extra_cfg->max_gf_interval > 0) {
     249           0 :     RANGE_CHECK(extra_cfg, max_gf_interval, extra_cfg->min_gf_interval,
     250             :                 (MAX_LAG_BUFFERS - 1));
     251             :   }
     252             : 
     253           0 :   if (cfg->rc_resize_allowed == 1) {
     254           0 :     RANGE_CHECK_HI(cfg, rc_scaled_width, cfg->g_w);
     255           0 :     RANGE_CHECK_HI(cfg, rc_scaled_height, cfg->g_h);
     256             :   }
     257             : 
     258             :   // AV1 does not support a lower bound on the keyframe interval in
     259             :   // automatic keyframe placement mode.
     260           0 :   if (cfg->kf_mode != AOM_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
     261           0 :       cfg->kf_min_dist > 0)
     262           0 :     ERROR(
     263             :         "kf_min_dist not supported in auto mode, use 0 "
     264             :         "or kf_max_dist instead.");
     265             : 
     266           0 :   RANGE_CHECK_HI(extra_cfg, motion_vector_unit_test, 2);
     267           0 :   RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 2);
     268             : #if CONFIG_EXT_REFS
     269           0 :   RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
     270             : #endif  // CONFIG_EXT_REFS
     271           0 :   RANGE_CHECK(extra_cfg, cpu_used, 0, 8);
     272           0 :   RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
     273           0 :   RANGE_CHECK(extra_cfg, superblock_size, AOM_SUPERBLOCK_SIZE_64X64,
     274             :               AOM_SUPERBLOCK_SIZE_DYNAMIC);
     275             : #if CONFIG_EXT_TILE
     276             : // TODO(any): Waring. If CONFIG_EXT_TILE is true, tile_columns really
     277             : // means tile_width, and tile_rows really means tile_hight. The interface
     278             : // should be sanitized.
     279             : #if CONFIG_EXT_PARTITION
     280             :   if (extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
     281             :     if (extra_cfg->tile_columns != UINT_MAX)
     282             :       RANGE_CHECK(extra_cfg, tile_columns, 1, 32);
     283             :     if (extra_cfg->tile_rows != UINT_MAX)
     284             :       RANGE_CHECK(extra_cfg, tile_rows, 1, 32);
     285             :   } else
     286             : #endif  // CONFIG_EXT_PARTITION
     287             :   {
     288             :     if (extra_cfg->tile_columns != UINT_MAX)
     289             :       RANGE_CHECK(extra_cfg, tile_columns, 1, 64);
     290             :     if (extra_cfg->tile_rows != UINT_MAX)
     291             :       RANGE_CHECK(extra_cfg, tile_rows, 1, 64);
     292             :   }
     293             :   RANGE_CHECK_HI(extra_cfg, tile_encoding_mode, 1);
     294             : #else
     295           0 :   RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
     296           0 :   RANGE_CHECK_HI(extra_cfg, tile_rows, 2);
     297             : #endif  // CONFIG_EXT_TILE
     298             : #if CONFIG_DEPENDENT_HORZTILES
     299             :   RANGE_CHECK_HI(extra_cfg, dependent_horz_tiles, 1);
     300             : #endif
     301             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
     302           0 :   RANGE_CHECK_HI(extra_cfg, loop_filter_across_tiles_enabled, 1);
     303             : #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
     304           0 :   RANGE_CHECK_HI(extra_cfg, sharpness, 7);
     305           0 :   RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
     306           0 :   RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
     307           0 :   RANGE_CHECK_HI(extra_cfg, cq_level, 63);
     308           0 :   RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
     309           0 :   RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
     310           0 :   RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
     311             : 
     312             :   // TODO(yaowu): remove this when ssim tuning is implemented for av1
     313           0 :   if (extra_cfg->tuning == AOM_TUNE_SSIM)
     314           0 :     ERROR("Option --tune=ssim is not currently supported in AV1.");
     315             : 
     316           0 :   if (cfg->g_pass == AOM_RC_LAST_PASS) {
     317             : #if !CONFIG_XIPHRC
     318           0 :     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
     319           0 :     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
     320             :     const FIRSTPASS_STATS *stats;
     321             : #endif
     322             : 
     323           0 :     if (cfg->rc_twopass_stats_in.buf == NULL)
     324           0 :       ERROR("rc_twopass_stats_in.buf not set.");
     325             : 
     326             : #if !CONFIG_XIPHRC
     327           0 :     if (cfg->rc_twopass_stats_in.sz % packet_sz)
     328           0 :       ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
     329             : 
     330           0 :     if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
     331           0 :       ERROR("rc_twopass_stats_in requires at least two packets.");
     332             : 
     333           0 :     stats =
     334           0 :         (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
     335             : 
     336           0 :     if ((int)(stats->count + 0.5) != n_packets - 1)
     337           0 :       ERROR("rc_twopass_stats_in missing EOS stats packet");
     338             : #endif
     339             :   }
     340             : 
     341             : #if !CONFIG_HIGHBITDEPTH
     342             :   if (cfg->g_profile > (unsigned int)PROFILE_1) {
     343             :     ERROR("Profile > 1 not supported in this build configuration");
     344             :   }
     345             : #endif
     346           0 :   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
     347           0 :       cfg->g_bit_depth > AOM_BITS_8) {
     348           0 :     ERROR("Codec high bit-depth not supported in profile < 2");
     349             :   }
     350           0 :   if (cfg->g_profile <= (unsigned int)PROFILE_1 && cfg->g_input_bit_depth > 8) {
     351           0 :     ERROR("Source high bit-depth not supported in profile < 2");
     352             :   }
     353           0 :   if (cfg->g_profile > (unsigned int)PROFILE_1 &&
     354           0 :       cfg->g_bit_depth == AOM_BITS_8) {
     355           0 :     ERROR("Codec bit-depth 8 not supported in profile > 1");
     356             :   }
     357           0 :   RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_SRGB);
     358           0 :   RANGE_CHECK(extra_cfg, color_range, 0, 1);
     359             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
     360             :   RANGE_CHECK(extra_cfg, ans_window_size_log2, 8, 23);
     361             : #endif
     362           0 :   return AOM_CODEC_OK;
     363             : }
     364             : 
     365           0 : static aom_codec_err_t validate_img(aom_codec_alg_priv_t *ctx,
     366             :                                     const aom_image_t *img) {
     367           0 :   switch (img->fmt) {
     368             :     case AOM_IMG_FMT_YV12:
     369             :     case AOM_IMG_FMT_I420:
     370           0 :     case AOM_IMG_FMT_I42016: break;
     371             :     case AOM_IMG_FMT_I422:
     372             :     case AOM_IMG_FMT_I444:
     373             :     case AOM_IMG_FMT_I440:
     374           0 :       if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
     375           0 :         ERROR(
     376             :             "Invalid image format. I422, I444, I440 images are "
     377             :             "not supported in profile.");
     378             :       }
     379           0 :       break;
     380             :     case AOM_IMG_FMT_I42216:
     381             :     case AOM_IMG_FMT_I44416:
     382             :     case AOM_IMG_FMT_I44016:
     383           0 :       if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
     384           0 :           ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
     385           0 :         ERROR(
     386             :             "Invalid image format. 16-bit I422, I444, I440 images are "
     387             :             "not supported in profile.");
     388             :       }
     389           0 :       break;
     390             :     default:
     391           0 :       ERROR(
     392             :           "Invalid image format. Only YV12, I420, I422, I444 images are "
     393             :           "supported.");
     394             :       break;
     395             :   }
     396             : 
     397           0 :   if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
     398           0 :     ERROR("Image size must match encoder init configuration size");
     399             : 
     400           0 :   return AOM_CODEC_OK;
     401             : }
     402             : 
     403           0 : static int get_image_bps(const aom_image_t *img) {
     404           0 :   switch (img->fmt) {
     405             :     case AOM_IMG_FMT_YV12:
     406           0 :     case AOM_IMG_FMT_I420: return 12;
     407           0 :     case AOM_IMG_FMT_I422: return 16;
     408           0 :     case AOM_IMG_FMT_I444: return 24;
     409           0 :     case AOM_IMG_FMT_I440: return 16;
     410           0 :     case AOM_IMG_FMT_I42016: return 24;
     411           0 :     case AOM_IMG_FMT_I42216: return 32;
     412           0 :     case AOM_IMG_FMT_I44416: return 48;
     413           0 :     case AOM_IMG_FMT_I44016: return 32;
     414           0 :     default: assert(0 && "Invalid image format"); break;
     415             :   }
     416             :   return 0;
     417             : }
     418             : 
     419           0 : static aom_codec_err_t set_encoder_config(
     420             :     AV1EncoderConfig *oxcf, const aom_codec_enc_cfg_t *cfg,
     421             :     const struct av1_extracfg *extra_cfg) {
     422           0 :   const int is_vbr = cfg->rc_end_usage == AOM_VBR;
     423           0 :   oxcf->profile = cfg->g_profile;
     424           0 :   oxcf->max_threads = (int)cfg->g_threads;
     425           0 :   oxcf->width = cfg->g_w;
     426           0 :   oxcf->height = cfg->g_h;
     427           0 :   oxcf->bit_depth = cfg->g_bit_depth;
     428           0 :   oxcf->input_bit_depth = cfg->g_input_bit_depth;
     429             :   // guess a frame rate if out of whack, use 30
     430           0 :   oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
     431           0 :   if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
     432             : 
     433           0 :   oxcf->mode = GOOD;
     434             : 
     435           0 :   switch (cfg->g_pass) {
     436           0 :     case AOM_RC_ONE_PASS: oxcf->pass = 0; break;
     437           0 :     case AOM_RC_FIRST_PASS: oxcf->pass = 1; break;
     438           0 :     case AOM_RC_LAST_PASS: oxcf->pass = 2; break;
     439             :   }
     440             : 
     441           0 :   oxcf->lag_in_frames =
     442           0 :       cfg->g_pass == AOM_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
     443           0 :   oxcf->rc_mode = cfg->rc_end_usage;
     444             : 
     445             :   // Convert target bandwidth from Kbit/s to Bit/s
     446           0 :   oxcf->target_bandwidth = 1000 * cfg->rc_target_bitrate;
     447           0 :   oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
     448           0 :   oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
     449           0 :   oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
     450             : 
     451           0 :   oxcf->best_allowed_q =
     452           0 :       extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_min_quantizer);
     453           0 :   oxcf->worst_allowed_q =
     454           0 :       extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_max_quantizer);
     455           0 :   oxcf->cq_level = av1_quantizer_to_qindex(extra_cfg->cq_level);
     456           0 :   oxcf->fixed_q = -1;
     457             : 
     458             : #if CONFIG_AOM_QM
     459             :   oxcf->using_qm = extra_cfg->enable_qm;
     460             :   oxcf->qm_minlevel = extra_cfg->qm_min;
     461             :   oxcf->qm_maxlevel = extra_cfg->qm_max;
     462             : #endif
     463             : 
     464             : #if CONFIG_TILE_GROUPS
     465           0 :   oxcf->num_tile_groups = extra_cfg->num_tg;
     466           0 :   oxcf->mtu = extra_cfg->mtu_size;
     467             : #endif
     468             : 
     469             : #if CONFIG_TEMPMV_SIGNALING
     470           0 :   oxcf->disable_tempmv = extra_cfg->disable_tempmv;
     471             : #endif
     472           0 :   oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
     473           0 :   oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
     474             : 
     475           0 :   oxcf->scaled_frame_width = cfg->rc_scaled_width;
     476           0 :   oxcf->scaled_frame_height = cfg->rc_scaled_height;
     477           0 :   if (cfg->rc_resize_allowed == 1) {
     478           0 :     oxcf->resize_mode =
     479           0 :         (oxcf->scaled_frame_width == 0 || oxcf->scaled_frame_height == 0)
     480             :             ? RESIZE_DYNAMIC
     481           0 :             : RESIZE_FIXED;
     482             :   } else {
     483           0 :     oxcf->resize_mode = RESIZE_NONE;
     484             :   }
     485             : 
     486             :   // Initialize to input resolution if not specified.
     487           0 :   if (oxcf->resize_mode != RESIZE_FIXED) {
     488           0 :     oxcf->scaled_frame_width = oxcf->width;
     489           0 :     oxcf->scaled_frame_height = oxcf->height;
     490             :   }
     491             : 
     492             : #if CONFIG_FRAME_SUPERRES
     493             :   oxcf->superres_enabled = 1;  // TODO(afergs): Check the config
     494             : #endif                         // CONFIG_FRAME_SUPERRES
     495             : 
     496           0 :   oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
     497           0 :   oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
     498           0 :   oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
     499             : 
     500           0 :   oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
     501             : 
     502           0 :   oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
     503           0 :   oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
     504           0 :   oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
     505             : 
     506           0 :   oxcf->auto_key =
     507           0 :       cfg->kf_mode == AOM_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
     508             : 
     509           0 :   oxcf->key_freq = cfg->kf_max_dist;
     510             : 
     511           0 :   oxcf->speed = extra_cfg->cpu_used;
     512           0 :   oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
     513             : #if CONFIG_EXT_REFS
     514           0 :   oxcf->enable_auto_brf = extra_cfg->enable_auto_bwd_ref;
     515             : #endif  // CONFIG_EXT_REFS
     516           0 :   oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
     517           0 :   oxcf->sharpness = extra_cfg->sharpness;
     518             : 
     519           0 :   oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
     520             : 
     521             : #if CONFIG_FP_MB_STATS
     522             :   oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
     523             : #endif
     524             : 
     525           0 :   oxcf->color_space = extra_cfg->color_space;
     526           0 :   oxcf->color_range = extra_cfg->color_range;
     527           0 :   oxcf->render_width = extra_cfg->render_width;
     528           0 :   oxcf->render_height = extra_cfg->render_height;
     529           0 :   oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
     530           0 :   oxcf->arnr_strength = extra_cfg->arnr_strength;
     531           0 :   oxcf->min_gf_interval = extra_cfg->min_gf_interval;
     532           0 :   oxcf->max_gf_interval = extra_cfg->max_gf_interval;
     533             : 
     534           0 :   oxcf->tuning = extra_cfg->tuning;
     535           0 :   oxcf->content = extra_cfg->content;
     536             : 
     537             : #if CONFIG_EXT_PARTITION
     538             :   oxcf->superblock_size = extra_cfg->superblock_size;
     539             : #endif  // CONFIG_EXT_PARTITION
     540             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
     541             :   oxcf->ans_window_size_log2 = extra_cfg->ans_window_size_log2;
     542             : #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
     543             : 
     544             : #if CONFIG_EXT_TILE
     545             :   {
     546             : #if CONFIG_EXT_PARTITION
     547             :     const unsigned int max =
     548             :         extra_cfg->superblock_size == AOM_SUPERBLOCK_SIZE_64X64 ? 64 : 32;
     549             : #else
     550             :     const unsigned int max = 64;
     551             : #endif  // CONFIG_EXT_PARTITION
     552             :     oxcf->tile_columns = AOMMIN(extra_cfg->tile_columns, max);
     553             :     oxcf->tile_rows = AOMMIN(extra_cfg->tile_rows, max);
     554             :     oxcf->tile_encoding_mode = extra_cfg->tile_encoding_mode;
     555             :   }
     556             : #else
     557           0 :   oxcf->tile_columns = extra_cfg->tile_columns;
     558           0 :   oxcf->tile_rows = extra_cfg->tile_rows;
     559             : #endif  // CONFIG_EXT_TILE
     560             : #if CONFIG_DEPENDENT_HORZTILES
     561             :   oxcf->dependent_horz_tiles = extra_cfg->dependent_horz_tiles;
     562             : #endif
     563             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
     564           0 :   oxcf->loop_filter_across_tiles_enabled =
     565           0 :       extra_cfg->loop_filter_across_tiles_enabled;
     566             : #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
     567           0 :   oxcf->error_resilient_mode = cfg->g_error_resilient;
     568           0 :   oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
     569             : 
     570           0 :   oxcf->aq_mode = extra_cfg->aq_mode;
     571             : #if CONFIG_EXT_DELTA_Q
     572           0 :   oxcf->deltaq_mode = extra_cfg->deltaq_mode;
     573             : #endif
     574             : 
     575           0 :   oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
     576             : 
     577           0 :   oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
     578             :   /*
     579             :   printf("Current AV1 Settings: \n");
     580             :   printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
     581             :   printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
     582             :   printf("sharpness: %d\n",    oxcf->sharpness);
     583             :   printf("cpu_used: %d\n",  oxcf->cpu_used);
     584             :   printf("Mode: %d\n",     oxcf->mode);
     585             :   printf("auto_key: %d\n",  oxcf->auto_key);
     586             :   printf("key_freq: %d\n", oxcf->key_freq);
     587             :   printf("end_usage: %d\n", oxcf->end_usage);
     588             :   printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
     589             :   printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
     590             :   printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
     591             :   printf("optimal_buffer_level: %d\n",  oxcf->optimal_buffer_level);
     592             :   printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
     593             :   printf("fixed_q: %d\n",  oxcf->fixed_q);
     594             :   printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
     595             :   printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
     596             :   printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
     597             :   printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
     598             :   printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
     599             :   printf("two_pass_vbrbias: %d\n",  oxcf->two_pass_vbrbias);
     600             :   printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
     601             :   printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
     602             :   printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
     603             :   printf("enable_auto_arf: %d\n", oxcf->enable_auto_arf);
     604             :   printf("Version: %d\n", oxcf->Version);
     605             :   printf("error resilient: %d\n", oxcf->error_resilient_mode);
     606             :   printf("frame parallel detokenization: %d\n",
     607             :          oxcf->frame_parallel_decoding_mode);
     608             :   */
     609           0 :   return AOM_CODEC_OK;
     610             : }
     611             : 
     612           0 : static aom_codec_err_t encoder_set_config(aom_codec_alg_priv_t *ctx,
     613             :                                           const aom_codec_enc_cfg_t *cfg) {
     614             :   aom_codec_err_t res;
     615           0 :   int force_key = 0;
     616             : 
     617           0 :   if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
     618           0 :     if (cfg->g_lag_in_frames > 1 || cfg->g_pass != AOM_RC_ONE_PASS)
     619           0 :       ERROR("Cannot change width or height after initialization");
     620           0 :     if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
     621           0 :         (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
     622           0 :         (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
     623           0 :       force_key = 1;
     624             :   }
     625             : 
     626             :   // Prevent increasing lag_in_frames. This check is stricter than it needs
     627             :   // to be -- the limit is not increasing past the first lag_in_frames
     628             :   // value, but we don't track the initial config, only the last successful
     629             :   // config.
     630           0 :   if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
     631           0 :     ERROR("Cannot increase lag_in_frames");
     632             : 
     633           0 :   res = validate_config(ctx, cfg, &ctx->extra_cfg);
     634             : 
     635           0 :   if (res == AOM_CODEC_OK) {
     636           0 :     ctx->cfg = *cfg;
     637           0 :     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
     638             :     // On profile change, request a key frame
     639           0 :     force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
     640           0 :     av1_change_config(ctx->cpi, &ctx->oxcf);
     641             :   }
     642             : 
     643           0 :   if (force_key) ctx->next_frame_flags |= AOM_EFLAG_FORCE_KF;
     644             : 
     645           0 :   return res;
     646             : }
     647             : 
     648           0 : static aom_codec_err_t ctrl_get_quantizer(aom_codec_alg_priv_t *ctx,
     649             :                                           va_list args) {
     650           0 :   int *const arg = va_arg(args, int *);
     651           0 :   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
     652           0 :   *arg = av1_get_quantizer(ctx->cpi);
     653           0 :   return AOM_CODEC_OK;
     654             : }
     655             : 
     656           0 : static aom_codec_err_t ctrl_get_quantizer64(aom_codec_alg_priv_t *ctx,
     657             :                                             va_list args) {
     658           0 :   int *const arg = va_arg(args, int *);
     659           0 :   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
     660           0 :   *arg = av1_qindex_to_quantizer(av1_get_quantizer(ctx->cpi));
     661           0 :   return AOM_CODEC_OK;
     662             : }
     663             : 
     664           0 : static aom_codec_err_t update_extra_cfg(aom_codec_alg_priv_t *ctx,
     665             :                                         const struct av1_extracfg *extra_cfg) {
     666           0 :   const aom_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
     667           0 :   if (res == AOM_CODEC_OK) {
     668           0 :     ctx->extra_cfg = *extra_cfg;
     669           0 :     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
     670           0 :     av1_change_config(ctx->cpi, &ctx->oxcf);
     671             :   }
     672           0 :   return res;
     673             : }
     674             : 
     675           0 : static aom_codec_err_t ctrl_set_cpuused(aom_codec_alg_priv_t *ctx,
     676             :                                         va_list args) {
     677           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     678           0 :   extra_cfg.cpu_used = CAST(AOME_SET_CPUUSED, args);
     679           0 :   return update_extra_cfg(ctx, &extra_cfg);
     680             : }
     681             : 
     682           0 : static aom_codec_err_t ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t *ctx,
     683             :                                                     va_list args) {
     684           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     685           0 :   extra_cfg.enable_auto_alt_ref = CAST(AOME_SET_ENABLEAUTOALTREF, args);
     686           0 :   return update_extra_cfg(ctx, &extra_cfg);
     687             : }
     688             : 
     689             : #if CONFIG_EXT_REFS
     690           0 : static aom_codec_err_t ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t *ctx,
     691             :                                                     va_list args) {
     692           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     693           0 :   extra_cfg.enable_auto_bwd_ref = CAST(AOME_SET_ENABLEAUTOBWDREF, args);
     694           0 :   return update_extra_cfg(ctx, &extra_cfg);
     695             : }
     696             : #endif  // CONFIG_EXT_REFS
     697             : 
     698           0 : static aom_codec_err_t ctrl_set_noise_sensitivity(aom_codec_alg_priv_t *ctx,
     699             :                                                   va_list args) {
     700           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     701           0 :   extra_cfg.noise_sensitivity = CAST(AV1E_SET_NOISE_SENSITIVITY, args);
     702           0 :   return update_extra_cfg(ctx, &extra_cfg);
     703             : }
     704             : 
     705           0 : static aom_codec_err_t ctrl_set_sharpness(aom_codec_alg_priv_t *ctx,
     706             :                                           va_list args) {
     707           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     708           0 :   extra_cfg.sharpness = CAST(AOME_SET_SHARPNESS, args);
     709           0 :   return update_extra_cfg(ctx, &extra_cfg);
     710             : }
     711             : 
     712           0 : static aom_codec_err_t ctrl_set_static_thresh(aom_codec_alg_priv_t *ctx,
     713             :                                               va_list args) {
     714           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     715           0 :   extra_cfg.static_thresh = CAST(AOME_SET_STATIC_THRESHOLD, args);
     716           0 :   return update_extra_cfg(ctx, &extra_cfg);
     717             : }
     718             : 
     719           0 : static aom_codec_err_t ctrl_set_tile_columns(aom_codec_alg_priv_t *ctx,
     720             :                                              va_list args) {
     721           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     722           0 :   extra_cfg.tile_columns = CAST(AV1E_SET_TILE_COLUMNS, args);
     723           0 :   return update_extra_cfg(ctx, &extra_cfg);
     724             : }
     725             : 
     726           0 : static aom_codec_err_t ctrl_set_tile_rows(aom_codec_alg_priv_t *ctx,
     727             :                                           va_list args) {
     728           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     729           0 :   extra_cfg.tile_rows = CAST(AV1E_SET_TILE_ROWS, args);
     730           0 :   return update_extra_cfg(ctx, &extra_cfg);
     731             : }
     732             : #if CONFIG_DEPENDENT_HORZTILES
     733             : static aom_codec_err_t ctrl_set_tile_dependent_rows(aom_codec_alg_priv_t *ctx,
     734             :                                                     va_list args) {
     735             :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     736             :   extra_cfg.dependent_horz_tiles = CAST(AV1E_SET_TILE_DEPENDENT_ROWS, args);
     737             :   return update_extra_cfg(ctx, &extra_cfg);
     738             : }
     739             : #endif
     740             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
     741           0 : static aom_codec_err_t ctrl_set_tile_loopfilter(aom_codec_alg_priv_t *ctx,
     742             :                                                 va_list args) {
     743           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     744           0 :   extra_cfg.loop_filter_across_tiles_enabled =
     745           0 :       CAST(AV1E_SET_TILE_LOOPFILTER, args);
     746           0 :   return update_extra_cfg(ctx, &extra_cfg);
     747             : }
     748             : #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
     749             : 
     750           0 : static aom_codec_err_t ctrl_set_arnr_max_frames(aom_codec_alg_priv_t *ctx,
     751             :                                                 va_list args) {
     752           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     753           0 :   extra_cfg.arnr_max_frames = CAST(AOME_SET_ARNR_MAXFRAMES, args);
     754           0 :   return update_extra_cfg(ctx, &extra_cfg);
     755             : }
     756             : 
     757           0 : static aom_codec_err_t ctrl_set_arnr_strength(aom_codec_alg_priv_t *ctx,
     758             :                                               va_list args) {
     759           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     760           0 :   extra_cfg.arnr_strength = CAST(AOME_SET_ARNR_STRENGTH, args);
     761           0 :   return update_extra_cfg(ctx, &extra_cfg);
     762             : }
     763             : 
     764           0 : static aom_codec_err_t ctrl_set_tuning(aom_codec_alg_priv_t *ctx,
     765             :                                        va_list args) {
     766           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     767           0 :   extra_cfg.tuning = CAST(AOME_SET_TUNING, args);
     768           0 :   return update_extra_cfg(ctx, &extra_cfg);
     769             : }
     770             : 
     771           0 : static aom_codec_err_t ctrl_set_cq_level(aom_codec_alg_priv_t *ctx,
     772             :                                          va_list args) {
     773           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     774           0 :   extra_cfg.cq_level = CAST(AOME_SET_CQ_LEVEL, args);
     775           0 :   return update_extra_cfg(ctx, &extra_cfg);
     776             : }
     777             : 
     778           0 : static aom_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
     779             :     aom_codec_alg_priv_t *ctx, va_list args) {
     780           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     781           0 :   extra_cfg.rc_max_intra_bitrate_pct =
     782           0 :       CAST(AOME_SET_MAX_INTRA_BITRATE_PCT, args);
     783           0 :   return update_extra_cfg(ctx, &extra_cfg);
     784             : }
     785             : 
     786           0 : static aom_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
     787             :     aom_codec_alg_priv_t *ctx, va_list args) {
     788           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     789           0 :   extra_cfg.rc_max_inter_bitrate_pct =
     790           0 :       CAST(AOME_SET_MAX_INTER_BITRATE_PCT, args);
     791           0 :   return update_extra_cfg(ctx, &extra_cfg);
     792             : }
     793             : 
     794           0 : static aom_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t *ctx,
     795             :                                                     va_list args) {
     796           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     797           0 :   extra_cfg.gf_cbr_boost_pct = CAST(AV1E_SET_GF_CBR_BOOST_PCT, args);
     798           0 :   return update_extra_cfg(ctx, &extra_cfg);
     799             : }
     800             : 
     801           0 : static aom_codec_err_t ctrl_set_lossless(aom_codec_alg_priv_t *ctx,
     802             :                                          va_list args) {
     803           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     804           0 :   extra_cfg.lossless = CAST(AV1E_SET_LOSSLESS, args);
     805           0 :   return update_extra_cfg(ctx, &extra_cfg);
     806             : }
     807             : 
     808             : #if CONFIG_AOM_QM
     809             : static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
     810             :                                           va_list args) {
     811             :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     812             :   extra_cfg.enable_qm = CAST(AV1E_SET_ENABLE_QM, args);
     813             :   return update_extra_cfg(ctx, &extra_cfg);
     814             : }
     815             : 
     816             : static aom_codec_err_t ctrl_set_qm_min(aom_codec_alg_priv_t *ctx,
     817             :                                        va_list args) {
     818             :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     819             :   extra_cfg.qm_min = CAST(AV1E_SET_QM_MIN, args);
     820             :   return update_extra_cfg(ctx, &extra_cfg);
     821             : }
     822             : 
     823             : static aom_codec_err_t ctrl_set_qm_max(aom_codec_alg_priv_t *ctx,
     824             :                                        va_list args) {
     825             :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     826             :   extra_cfg.qm_max = CAST(AV1E_SET_QM_MAX, args);
     827             :   return update_extra_cfg(ctx, &extra_cfg);
     828             : }
     829             : #endif
     830             : 
     831             : #if CONFIG_TILE_GROUPS
     832           0 : static aom_codec_err_t ctrl_set_num_tg(aom_codec_alg_priv_t *ctx,
     833             :                                        va_list args) {
     834           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     835           0 :   extra_cfg.num_tg = CAST(AV1E_SET_NUM_TG, args);
     836           0 :   return update_extra_cfg(ctx, &extra_cfg);
     837             : }
     838             : 
     839           0 : static aom_codec_err_t ctrl_set_mtu(aom_codec_alg_priv_t *ctx, va_list args) {
     840           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     841           0 :   extra_cfg.mtu_size = CAST(AV1E_SET_MTU, args);
     842           0 :   return update_extra_cfg(ctx, &extra_cfg);
     843             : }
     844             : #endif
     845             : #if CONFIG_TEMPMV_SIGNALING
     846           0 : static aom_codec_err_t ctrl_set_disable_tempmv(aom_codec_alg_priv_t *ctx,
     847             :                                                va_list args) {
     848           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     849           0 :   extra_cfg.disable_tempmv = CAST(AV1E_SET_DISABLE_TEMPMV, args);
     850           0 :   return update_extra_cfg(ctx, &extra_cfg);
     851             : }
     852             : #endif
     853           0 : static aom_codec_err_t ctrl_set_frame_parallel_decoding_mode(
     854             :     aom_codec_alg_priv_t *ctx, va_list args) {
     855           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     856           0 :   extra_cfg.frame_parallel_decoding_mode =
     857           0 :       CAST(AV1E_SET_FRAME_PARALLEL_DECODING, args);
     858           0 :   return update_extra_cfg(ctx, &extra_cfg);
     859             : }
     860             : 
     861             : #if CONFIG_EXT_TILE
     862             : static aom_codec_err_t ctrl_set_tile_encoding_mode(aom_codec_alg_priv_t *ctx,
     863             :                                                    va_list args) {
     864             :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     865             :   extra_cfg.tile_encoding_mode = CAST(AV1E_SET_TILE_ENCODING_MODE, args);
     866             :   return update_extra_cfg(ctx, &extra_cfg);
     867             : }
     868             : #endif  // CONFIG_EXT_TILE
     869             : 
     870           0 : static aom_codec_err_t ctrl_set_aq_mode(aom_codec_alg_priv_t *ctx,
     871             :                                         va_list args) {
     872           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     873           0 :   extra_cfg.aq_mode = CAST(AV1E_SET_AQ_MODE, args);
     874           0 :   return update_extra_cfg(ctx, &extra_cfg);
     875             : }
     876             : 
     877             : #if CONFIG_EXT_DELTA_Q
     878           0 : static aom_codec_err_t ctrl_set_deltaq_mode(aom_codec_alg_priv_t *ctx,
     879             :                                             va_list args) {
     880           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     881           0 :   extra_cfg.deltaq_mode = CAST(AV1E_SET_DELTAQ_MODE, args);
     882           0 :   return update_extra_cfg(ctx, &extra_cfg);
     883             : }
     884             : #endif
     885           0 : static aom_codec_err_t ctrl_set_min_gf_interval(aom_codec_alg_priv_t *ctx,
     886             :                                                 va_list args) {
     887           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     888           0 :   extra_cfg.min_gf_interval = CAST(AV1E_SET_MIN_GF_INTERVAL, args);
     889           0 :   return update_extra_cfg(ctx, &extra_cfg);
     890             : }
     891             : 
     892           0 : static aom_codec_err_t ctrl_set_max_gf_interval(aom_codec_alg_priv_t *ctx,
     893             :                                                 va_list args) {
     894           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     895           0 :   extra_cfg.max_gf_interval = CAST(AV1E_SET_MAX_GF_INTERVAL, args);
     896           0 :   return update_extra_cfg(ctx, &extra_cfg);
     897             : }
     898             : 
     899           0 : static aom_codec_err_t ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t *ctx,
     900             :                                                      va_list args) {
     901           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     902           0 :   extra_cfg.frame_periodic_boost = CAST(AV1E_SET_FRAME_PERIODIC_BOOST, args);
     903           0 :   return update_extra_cfg(ctx, &extra_cfg);
     904             : }
     905             : 
     906           0 : static aom_codec_err_t ctrl_enable_motion_vector_unit_test(
     907             :     aom_codec_alg_priv_t *ctx, va_list args) {
     908           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
     909           0 :   extra_cfg.motion_vector_unit_test =
     910           0 :       CAST(AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
     911           0 :   return update_extra_cfg(ctx, &extra_cfg);
     912             : }
     913             : 
     914           0 : static aom_codec_err_t encoder_init(aom_codec_ctx_t *ctx,
     915             :                                     aom_codec_priv_enc_mr_cfg_t *data) {
     916           0 :   aom_codec_err_t res = AOM_CODEC_OK;
     917             :   (void)data;
     918             : 
     919           0 :   if (ctx->priv == NULL) {
     920           0 :     aom_codec_alg_priv_t *const priv = aom_calloc(1, sizeof(*priv));
     921           0 :     if (priv == NULL) return AOM_CODEC_MEM_ERROR;
     922             : 
     923           0 :     ctx->priv = (aom_codec_priv_t *)priv;
     924           0 :     ctx->priv->init_flags = ctx->init_flags;
     925           0 :     ctx->priv->enc.total_encoders = 1;
     926           0 :     priv->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
     927           0 :     if (priv->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
     928             : 
     929             : #if CONFIG_MULTITHREAD
     930           0 :     if (pthread_mutex_init(&priv->buffer_pool->pool_mutex, NULL)) {
     931           0 :       return AOM_CODEC_MEM_ERROR;
     932             :     }
     933             : #endif
     934             : 
     935           0 :     if (ctx->config.enc) {
     936             :       // Update the reference to the config structure to an internal copy.
     937           0 :       priv->cfg = *ctx->config.enc;
     938           0 :       ctx->config.enc = &priv->cfg;
     939             :     }
     940             : 
     941           0 :     priv->extra_cfg = default_extra_cfg;
     942           0 :     once(av1_initialize_enc);
     943             : 
     944           0 :     res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
     945             : 
     946           0 :     if (res == AOM_CODEC_OK) {
     947           0 :       set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
     948             : #if CONFIG_HIGHBITDEPTH
     949           0 :       priv->oxcf.use_highbitdepth =
     950           0 :           (ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
     951             : #endif
     952           0 :       priv->cpi = av1_create_compressor(&priv->oxcf, priv->buffer_pool);
     953           0 :       if (priv->cpi == NULL)
     954           0 :         res = AOM_CODEC_MEM_ERROR;
     955             :       else
     956           0 :         priv->cpi->output_pkt_list = &priv->pkt_list.head;
     957             :     }
     958             :   }
     959             : 
     960           0 :   return res;
     961             : }
     962             : 
     963           0 : static aom_codec_err_t encoder_destroy(aom_codec_alg_priv_t *ctx) {
     964           0 :   free(ctx->cx_data);
     965           0 :   av1_remove_compressor(ctx->cpi);
     966             : #if CONFIG_MULTITHREAD
     967           0 :   pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
     968             : #endif
     969           0 :   aom_free(ctx->buffer_pool);
     970           0 :   aom_free(ctx);
     971           0 :   return AOM_CODEC_OK;
     972             : }
     973             : 
     974           0 : static void pick_quickcompress_mode(aom_codec_alg_priv_t *ctx,
     975             :                                     unsigned long deadline) {
     976           0 :   MODE new_mode = GOOD;
     977             : 
     978           0 :   switch (ctx->cfg.g_pass) {
     979             :     case AOM_RC_ONE_PASS:
     980             :       switch (deadline) {
     981           0 :         default: new_mode = GOOD; break;
     982             :       }
     983           0 :       break;
     984           0 :     case AOM_RC_FIRST_PASS: break;
     985           0 :     case AOM_RC_LAST_PASS: new_mode = GOOD;
     986             :   }
     987             : 
     988           0 :   if (ctx->oxcf.mode != new_mode) {
     989           0 :     ctx->oxcf.mode = new_mode;
     990           0 :     av1_change_config(ctx->cpi, &ctx->oxcf);
     991             :   }
     992           0 : }
     993             : 
     994             : // Turn on to test if supplemental superframe data breaks decoding
     995             : // #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
     996           0 : static int write_superframe_index(aom_codec_alg_priv_t *ctx) {
     997           0 :   uint8_t marker = 0xc0;
     998             :   unsigned int mask;
     999             :   int mag, index_sz;
    1000             :   int i;
    1001           0 :   size_t max_frame_sz = 0;
    1002             : 
    1003           0 :   assert(ctx->pending_frame_count);
    1004           0 :   assert(ctx->pending_frame_count <= 8);
    1005             : 
    1006             :   // Add the number of frames to the marker byte
    1007           0 :   marker |= ctx->pending_frame_count - 1;
    1008           0 :   for (i = 0; i < ctx->pending_frame_count - 1; i++) {
    1009           0 :     const size_t frame_sz = (unsigned int)ctx->pending_frame_sizes[i] - 1;
    1010           0 :     max_frame_sz = frame_sz > max_frame_sz ? frame_sz : max_frame_sz;
    1011             :   }
    1012             : 
    1013             :   // Choose the magnitude
    1014           0 :   for (mag = 0, mask = 0xff; mag < 4; mag++) {
    1015           0 :     if (max_frame_sz <= mask) break;
    1016           0 :     mask <<= 8;
    1017           0 :     mask |= 0xff;
    1018             :   }
    1019           0 :   marker |= mag << 3;
    1020             : 
    1021             :   // Write the index
    1022           0 :   index_sz = 2 + (mag + 1) * (ctx->pending_frame_count - 1);
    1023           0 :   if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
    1024           0 :     uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
    1025             : #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
    1026             :     uint8_t marker_test = 0xc0;
    1027             :     int mag_test = 2;     // 1 - 4
    1028             :     int frames_test = 4;  // 1 - 8
    1029             :     int index_sz_test = 2 + mag_test * frames_test;
    1030             :     marker_test |= frames_test - 1;
    1031             :     marker_test |= (mag_test - 1) << 3;
    1032             :     *x++ = marker_test;
    1033             :     for (i = 0; i < mag_test * frames_test; ++i)
    1034             :       *x++ = 0;  // fill up with arbitrary data
    1035             :     *x++ = marker_test;
    1036             :     ctx->pending_cx_data_sz += index_sz_test;
    1037             :     printf("Added supplemental superframe data\n");
    1038             : #endif
    1039             : 
    1040           0 :     *x++ = marker;
    1041           0 :     for (i = 0; i < ctx->pending_frame_count - 1; i++) {
    1042             :       unsigned int this_sz;
    1043             :       int j;
    1044             : 
    1045           0 :       assert(ctx->pending_frame_sizes[i] > 0);
    1046           0 :       this_sz = (unsigned int)ctx->pending_frame_sizes[i] - 1;
    1047           0 :       for (j = 0; j <= mag; j++) {
    1048           0 :         *x++ = this_sz & 0xff;
    1049           0 :         this_sz >>= 8;
    1050             :       }
    1051             :     }
    1052           0 :     *x++ = marker;
    1053           0 :     ctx->pending_cx_data_sz += index_sz;
    1054             : #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
    1055             :     index_sz += index_sz_test;
    1056             : #endif
    1057             :   }
    1058           0 :   return index_sz;
    1059             : }
    1060             : 
    1061             : // av1 uses 10,000,000 ticks/second as time stamp
    1062             : #define TICKS_PER_SEC 10000000LL
    1063             : 
    1064           0 : static int64_t timebase_units_to_ticks(const aom_rational_t *timebase,
    1065             :                                        int64_t n) {
    1066           0 :   return n * TICKS_PER_SEC * timebase->num / timebase->den;
    1067             : }
    1068             : 
    1069           0 : static int64_t ticks_to_timebase_units(const aom_rational_t *timebase,
    1070             :                                        int64_t n) {
    1071           0 :   const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
    1072           0 :   return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
    1073             : }
    1074             : 
    1075           0 : static aom_codec_frame_flags_t get_frame_pkt_flags(const AV1_COMP *cpi,
    1076             :                                                    unsigned int lib_flags) {
    1077           0 :   aom_codec_frame_flags_t flags = lib_flags << 16;
    1078             : 
    1079           0 :   if (lib_flags & FRAMEFLAGS_KEY) flags |= AOM_FRAME_IS_KEY;
    1080             : 
    1081           0 :   if (cpi->droppable) flags |= AOM_FRAME_IS_DROPPABLE;
    1082             : 
    1083           0 :   return flags;
    1084             : }
    1085             : 
    1086           0 : static aom_codec_err_t encoder_encode(aom_codec_alg_priv_t *ctx,
    1087             :                                       const aom_image_t *img,
    1088             :                                       aom_codec_pts_t pts,
    1089             :                                       unsigned long duration,
    1090             :                                       aom_enc_frame_flags_t enc_flags,
    1091             :                                       unsigned long deadline) {
    1092           0 :   const size_t kMinCompressedSize = 8192;
    1093           0 :   volatile aom_codec_err_t res = AOM_CODEC_OK;
    1094           0 :   volatile aom_enc_frame_flags_t flags = enc_flags;
    1095           0 :   AV1_COMP *const cpi = ctx->cpi;
    1096           0 :   const aom_rational_t *const timebase = &ctx->cfg.g_timebase;
    1097             :   size_t data_sz;
    1098             : 
    1099           0 :   if (cpi == NULL) return AOM_CODEC_INVALID_PARAM;
    1100             : 
    1101           0 :   if (img != NULL) {
    1102           0 :     res = validate_img(ctx, img);
    1103             :     // TODO(jzern) the checks related to cpi's validity should be treated as a
    1104             :     // failure condition, encoder setup is done fully in init() currently.
    1105           0 :     if (res == AOM_CODEC_OK) {
    1106             : #if CONFIG_EXT_REFS
    1107           0 :       data_sz = ALIGN_POWER_OF_TWO(ctx->cfg.g_w, 5) *
    1108           0 :                 ALIGN_POWER_OF_TWO(ctx->cfg.g_h, 5) * get_image_bps(img);
    1109             : #else
    1110             :       // There's no codec control for multiple alt-refs so check the encoder
    1111             :       // instance for its status to determine the compressed data size.
    1112             :       data_sz = ALIGN_POWER_OF_TWO(ctx->cfg.g_w, 5) *
    1113             :                 ALIGN_POWER_OF_TWO(ctx->cfg.g_h, 5) * get_image_bps(img) / 8 *
    1114             :                 (cpi->multi_arf_allowed ? 8 : 2);
    1115             : #endif  // CONFIG_EXT_REFS
    1116           0 :       if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
    1117           0 :       if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
    1118           0 :         ctx->cx_data_sz = data_sz;
    1119           0 :         free(ctx->cx_data);
    1120           0 :         ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
    1121           0 :         if (ctx->cx_data == NULL) {
    1122           0 :           return AOM_CODEC_MEM_ERROR;
    1123             :         }
    1124             :       }
    1125             :     }
    1126             :   }
    1127             : 
    1128           0 :   pick_quickcompress_mode(ctx, deadline);
    1129           0 :   aom_codec_pkt_list_init(&ctx->pkt_list);
    1130             : 
    1131             :   // Handle Flags
    1132           0 :   if (((flags & AOM_EFLAG_NO_UPD_GF) && (flags & AOM_EFLAG_FORCE_GF)) ||
    1133           0 :       ((flags & AOM_EFLAG_NO_UPD_ARF) && (flags & AOM_EFLAG_FORCE_ARF))) {
    1134           0 :     ctx->base.err_detail = "Conflicting flags.";
    1135           0 :     return AOM_CODEC_INVALID_PARAM;
    1136             :   }
    1137             : 
    1138           0 :   if (setjmp(cpi->common.error.jmp)) {
    1139           0 :     cpi->common.error.setjmp = 0;
    1140           0 :     res = update_error_state(ctx, &cpi->common.error);
    1141           0 :     aom_clear_system_state();
    1142           0 :     return res;
    1143             :   }
    1144           0 :   cpi->common.error.setjmp = 1;
    1145             : 
    1146           0 :   av1_apply_encoding_flags(cpi, flags);
    1147             : 
    1148             :   // Handle fixed keyframe intervals
    1149           0 :   if (ctx->cfg.kf_mode == AOM_KF_AUTO &&
    1150           0 :       ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
    1151           0 :     if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
    1152           0 :       flags |= AOM_EFLAG_FORCE_KF;
    1153           0 :       ctx->fixed_kf_cntr = 1;
    1154             :     }
    1155             :   }
    1156             : 
    1157           0 :   if (res == AOM_CODEC_OK) {
    1158           0 :     unsigned int lib_flags = 0;
    1159             :     YV12_BUFFER_CONFIG sd;
    1160           0 :     int64_t dst_time_stamp = timebase_units_to_ticks(timebase, pts);
    1161           0 :     int64_t dst_end_time_stamp =
    1162           0 :         timebase_units_to_ticks(timebase, pts + duration);
    1163             :     size_t size, cx_data_sz;
    1164             :     unsigned char *cx_data;
    1165             : 
    1166             :     // Set up internal flags
    1167           0 :     if (ctx->base.init_flags & AOM_CODEC_USE_PSNR) cpi->b_calculate_psnr = 1;
    1168             : 
    1169           0 :     if (img != NULL) {
    1170           0 :       res = image2yuvconfig(img, &sd);
    1171             : 
    1172             :       // Store the original flags in to the frame buffer. Will extract the
    1173             :       // key frame flag when we actually encode this frame.
    1174           0 :       if (av1_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
    1175             :                                 dst_time_stamp, dst_end_time_stamp)) {
    1176           0 :         res = update_error_state(ctx, &cpi->common.error);
    1177             :       }
    1178           0 :       ctx->next_frame_flags = 0;
    1179             :     }
    1180             : 
    1181           0 :     cx_data = ctx->cx_data;
    1182           0 :     cx_data_sz = ctx->cx_data_sz;
    1183             : 
    1184             :     /* Any pending invisible frames? */
    1185           0 :     if (ctx->pending_cx_data) {
    1186           0 :       memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
    1187           0 :       ctx->pending_cx_data = cx_data;
    1188           0 :       cx_data += ctx->pending_cx_data_sz;
    1189           0 :       cx_data_sz -= ctx->pending_cx_data_sz;
    1190             : 
    1191             :       /* TODO: this is a minimal check, the underlying codec doesn't respect
    1192             :        * the buffer size anyway.
    1193             :        */
    1194           0 :       if (cx_data_sz < ctx->cx_data_sz / 2) {
    1195           0 :         aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR,
    1196             :                            "Compressed data buffer too small");
    1197           0 :         return AOM_CODEC_ERROR;
    1198             :       }
    1199             :     }
    1200             : 
    1201           0 :     while (cx_data_sz >= ctx->cx_data_sz / 2 &&
    1202           0 :            -1 != av1_get_compressed_data(cpi, &lib_flags, &size, cx_data,
    1203             :                                          &dst_time_stamp, &dst_end_time_stamp,
    1204             :                                          !img)) {
    1205             : #if CONFIG_REFERENCE_BUFFER
    1206           0 :       if (cpi->common.invalid_delta_frame_id_minus1) {
    1207           0 :         ctx->base.err_detail = "Invalid delta_frame_id_minus1";
    1208           0 :         return AOM_CODEC_ERROR;
    1209             :       }
    1210             : #endif
    1211           0 :       if (size) {
    1212             :         aom_codec_cx_pkt_t pkt;
    1213             : 
    1214             :         // Pack invisible frames with the next visible frame
    1215           0 :         if (!cpi->common.show_frame) {
    1216           0 :           if (ctx->pending_cx_data == 0) ctx->pending_cx_data = cx_data;
    1217           0 :           ctx->pending_cx_data_sz += size;
    1218           0 :           ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
    1219           0 :           cx_data += size;
    1220           0 :           cx_data_sz -= size;
    1221             : 
    1222           0 :           continue;
    1223             :         }
    1224             : 
    1225             :         // Add the frame packet to the list of returned packets.
    1226           0 :         pkt.kind = AOM_CODEC_CX_FRAME_PKT;
    1227           0 :         pkt.data.frame.pts = ticks_to_timebase_units(timebase, dst_time_stamp);
    1228           0 :         pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
    1229             :             timebase, dst_end_time_stamp - dst_time_stamp);
    1230           0 :         pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
    1231             : 
    1232           0 :         if (ctx->pending_cx_data) {
    1233           0 :           ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
    1234           0 :           ctx->pending_cx_data_sz += size;
    1235           0 :           size += write_superframe_index(ctx);
    1236           0 :           pkt.data.frame.buf = ctx->pending_cx_data;
    1237           0 :           pkt.data.frame.sz = ctx->pending_cx_data_sz;
    1238           0 :           ctx->pending_cx_data = NULL;
    1239           0 :           ctx->pending_cx_data_sz = 0;
    1240           0 :           ctx->pending_frame_count = 0;
    1241             :         } else {
    1242           0 :           pkt.data.frame.buf = cx_data;
    1243           0 :           pkt.data.frame.sz = size;
    1244             :         }
    1245           0 :         pkt.data.frame.partition_id = -1;
    1246             : 
    1247           0 :         aom_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
    1248             : 
    1249           0 :         cx_data += size;
    1250           0 :         cx_data_sz -= size;
    1251             :       }
    1252             :     }
    1253             :   }
    1254             : 
    1255           0 :   cpi->common.error.setjmp = 0;
    1256           0 :   return res;
    1257             : }
    1258             : 
    1259           0 : static const aom_codec_cx_pkt_t *encoder_get_cxdata(aom_codec_alg_priv_t *ctx,
    1260             :                                                     aom_codec_iter_t *iter) {
    1261           0 :   return aom_codec_pkt_list_get(&ctx->pkt_list.head, iter);
    1262             : }
    1263             : 
    1264           0 : static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
    1265             :                                           va_list args) {
    1266           0 :   aom_ref_frame_t *const frame = va_arg(args, aom_ref_frame_t *);
    1267             : 
    1268           0 :   if (frame != NULL) {
    1269             :     YV12_BUFFER_CONFIG sd;
    1270             : 
    1271           0 :     image2yuvconfig(&frame->img, &sd);
    1272           0 :     av1_set_reference_enc(ctx->cpi, ref_frame_to_av1_reframe(frame->frame_type),
    1273             :                           &sd);
    1274           0 :     return AOM_CODEC_OK;
    1275             :   } else {
    1276           0 :     return AOM_CODEC_INVALID_PARAM;
    1277             :   }
    1278             : }
    1279             : 
    1280           0 : static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
    1281             :                                            va_list args) {
    1282           0 :   aom_ref_frame_t *const frame = va_arg(args, aom_ref_frame_t *);
    1283             : 
    1284           0 :   if (frame != NULL) {
    1285             :     YV12_BUFFER_CONFIG sd;
    1286             : 
    1287           0 :     image2yuvconfig(&frame->img, &sd);
    1288           0 :     av1_copy_reference_enc(ctx->cpi,
    1289             :                            ref_frame_to_av1_reframe(frame->frame_type), &sd);
    1290           0 :     return AOM_CODEC_OK;
    1291             :   } else {
    1292           0 :     return AOM_CODEC_INVALID_PARAM;
    1293             :   }
    1294             : }
    1295             : 
    1296           0 : static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
    1297             :                                           va_list args) {
    1298           0 :   av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
    1299             : 
    1300           0 :   if (frame != NULL) {
    1301           0 :     YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->cpi->common, frame->idx);
    1302           0 :     if (fb == NULL) return AOM_CODEC_ERROR;
    1303             : 
    1304           0 :     yuvconfig2image(&frame->img, fb, NULL);
    1305           0 :     return AOM_CODEC_OK;
    1306             :   } else {
    1307           0 :     return AOM_CODEC_INVALID_PARAM;
    1308             :   }
    1309             : }
    1310             : 
    1311           0 : static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
    1312             :                                                 va_list args) {
    1313           0 :   aom_image_t *const new_img = va_arg(args, aom_image_t *);
    1314             : 
    1315           0 :   if (new_img != NULL) {
    1316             :     YV12_BUFFER_CONFIG new_frame;
    1317             : 
    1318           0 :     if (av1_get_last_show_frame(ctx->cpi, &new_frame) == 0) {
    1319           0 :       yuvconfig2image(new_img, &new_frame, NULL);
    1320           0 :       return AOM_CODEC_OK;
    1321             :     } else {
    1322           0 :       return AOM_CODEC_ERROR;
    1323             :     }
    1324             :   } else {
    1325           0 :     return AOM_CODEC_INVALID_PARAM;
    1326             :   }
    1327             : }
    1328             : 
    1329           0 : static aom_codec_err_t ctrl_set_previewpp(aom_codec_alg_priv_t *ctx,
    1330             :                                           va_list args) {
    1331             :   (void)ctx;
    1332             :   (void)args;
    1333           0 :   return AOM_CODEC_INCAPABLE;
    1334             : }
    1335             : 
    1336           0 : static aom_image_t *encoder_get_preview(aom_codec_alg_priv_t *ctx) {
    1337             :   YV12_BUFFER_CONFIG sd;
    1338             : 
    1339           0 :   if (av1_get_preview_raw_frame(ctx->cpi, &sd) == 0) {
    1340           0 :     yuvconfig2image(&ctx->preview_img, &sd, NULL);
    1341           0 :     return &ctx->preview_img;
    1342             :   } else {
    1343           0 :     return NULL;
    1344             :   }
    1345             : }
    1346             : 
    1347           0 : static aom_codec_err_t ctrl_use_reference(aom_codec_alg_priv_t *ctx,
    1348             :                                           va_list args) {
    1349           0 :   const int reference_flag = va_arg(args, int);
    1350             : 
    1351           0 :   av1_use_as_reference(ctx->cpi, reference_flag);
    1352           0 :   return AOM_CODEC_OK;
    1353             : }
    1354             : 
    1355           0 : static aom_codec_err_t ctrl_set_roi_map(aom_codec_alg_priv_t *ctx,
    1356             :                                         va_list args) {
    1357             :   (void)ctx;
    1358             :   (void)args;
    1359             : 
    1360             :   // TODO(yaowu): Need to re-implement and test for AV1.
    1361           0 :   return AOM_CODEC_INVALID_PARAM;
    1362             : }
    1363             : 
    1364           0 : static aom_codec_err_t ctrl_set_active_map(aom_codec_alg_priv_t *ctx,
    1365             :                                            va_list args) {
    1366           0 :   aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
    1367             : 
    1368           0 :   if (map) {
    1369           0 :     if (!av1_set_active_map(ctx->cpi, map->active_map, (int)map->rows,
    1370           0 :                             (int)map->cols))
    1371           0 :       return AOM_CODEC_OK;
    1372             :     else
    1373           0 :       return AOM_CODEC_INVALID_PARAM;
    1374             :   } else {
    1375           0 :     return AOM_CODEC_INVALID_PARAM;
    1376             :   }
    1377             : }
    1378             : 
    1379           0 : static aom_codec_err_t ctrl_get_active_map(aom_codec_alg_priv_t *ctx,
    1380             :                                            va_list args) {
    1381           0 :   aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
    1382             : 
    1383           0 :   if (map) {
    1384           0 :     if (!av1_get_active_map(ctx->cpi, map->active_map, (int)map->rows,
    1385           0 :                             (int)map->cols))
    1386           0 :       return AOM_CODEC_OK;
    1387             :     else
    1388           0 :       return AOM_CODEC_INVALID_PARAM;
    1389             :   } else {
    1390           0 :     return AOM_CODEC_INVALID_PARAM;
    1391             :   }
    1392             : }
    1393             : 
    1394           0 : static aom_codec_err_t ctrl_set_scale_mode(aom_codec_alg_priv_t *ctx,
    1395             :                                            va_list args) {
    1396           0 :   aom_scaling_mode_t *const mode = va_arg(args, aom_scaling_mode_t *);
    1397             : 
    1398           0 :   if (mode) {
    1399           0 :     const int res =
    1400           0 :         av1_set_internal_size(ctx->cpi, (AOM_SCALING)mode->h_scaling_mode,
    1401           0 :                               (AOM_SCALING)mode->v_scaling_mode);
    1402           0 :     return (res == 0) ? AOM_CODEC_OK : AOM_CODEC_INVALID_PARAM;
    1403             :   } else {
    1404           0 :     return AOM_CODEC_INVALID_PARAM;
    1405             :   }
    1406             : }
    1407             : 
    1408           0 : static aom_codec_err_t ctrl_set_tune_content(aom_codec_alg_priv_t *ctx,
    1409             :                                              va_list args) {
    1410           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
    1411           0 :   extra_cfg.content = CAST(AV1E_SET_TUNE_CONTENT, args);
    1412           0 :   return update_extra_cfg(ctx, &extra_cfg);
    1413             : }
    1414             : 
    1415           0 : static aom_codec_err_t ctrl_set_color_space(aom_codec_alg_priv_t *ctx,
    1416             :                                             va_list args) {
    1417           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
    1418           0 :   extra_cfg.color_space = CAST(AV1E_SET_COLOR_SPACE, args);
    1419           0 :   return update_extra_cfg(ctx, &extra_cfg);
    1420             : }
    1421             : 
    1422           0 : static aom_codec_err_t ctrl_set_color_range(aom_codec_alg_priv_t *ctx,
    1423             :                                             va_list args) {
    1424           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
    1425           0 :   extra_cfg.color_range = CAST(AV1E_SET_COLOR_RANGE, args);
    1426           0 :   return update_extra_cfg(ctx, &extra_cfg);
    1427             : }
    1428             : 
    1429           0 : static aom_codec_err_t ctrl_set_render_size(aom_codec_alg_priv_t *ctx,
    1430             :                                             va_list args) {
    1431           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
    1432           0 :   int *const render_size = va_arg(args, int *);
    1433           0 :   extra_cfg.render_width = render_size[0];
    1434           0 :   extra_cfg.render_height = render_size[1];
    1435           0 :   return update_extra_cfg(ctx, &extra_cfg);
    1436             : }
    1437             : 
    1438           0 : static aom_codec_err_t ctrl_set_superblock_size(aom_codec_alg_priv_t *ctx,
    1439             :                                                 va_list args) {
    1440           0 :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
    1441           0 :   extra_cfg.superblock_size = CAST(AV1E_SET_SUPERBLOCK_SIZE, args);
    1442           0 :   return update_extra_cfg(ctx, &extra_cfg);
    1443             : }
    1444             : 
    1445             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    1446             : static aom_codec_err_t ctrl_set_ans_window_size_log2(aom_codec_alg_priv_t *ctx,
    1447             :                                                      va_list args) {
    1448             :   struct av1_extracfg extra_cfg = ctx->extra_cfg;
    1449             :   extra_cfg.ans_window_size_log2 = CAST(AV1E_SET_ANS_WINDOW_SIZE_LOG2, args);
    1450             :   return update_extra_cfg(ctx, &extra_cfg);
    1451             : }
    1452             : #endif
    1453             : 
    1454             : static aom_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
    1455             :   { AOM_COPY_REFERENCE, ctrl_copy_reference },
    1456             :   { AOME_USE_REFERENCE, ctrl_use_reference },
    1457             : 
    1458             :   // Setters
    1459             :   { AOM_SET_REFERENCE, ctrl_set_reference },
    1460             :   { AOM_SET_POSTPROC, ctrl_set_previewpp },
    1461             :   { AOME_SET_ROI_MAP, ctrl_set_roi_map },
    1462             :   { AOME_SET_ACTIVEMAP, ctrl_set_active_map },
    1463             :   { AOME_SET_SCALEMODE, ctrl_set_scale_mode },
    1464             :   { AOME_SET_CPUUSED, ctrl_set_cpuused },
    1465             :   { AOME_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
    1466             : #if CONFIG_EXT_REFS
    1467             :   { AOME_SET_ENABLEAUTOBWDREF, ctrl_set_enable_auto_bwd_ref },
    1468             : #endif  // CONFIG_EXT_REFS
    1469             :   { AOME_SET_SHARPNESS, ctrl_set_sharpness },
    1470             :   { AOME_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
    1471             :   { AV1E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
    1472             :   { AV1E_SET_TILE_ROWS, ctrl_set_tile_rows },
    1473             : #if CONFIG_DEPENDENT_HORZTILES
    1474             :   { AV1E_SET_TILE_DEPENDENT_ROWS, ctrl_set_tile_dependent_rows },
    1475             : #endif
    1476             : #if CONFIG_LOOPFILTERING_ACROSS_TILES
    1477             :   { AV1E_SET_TILE_LOOPFILTER, ctrl_set_tile_loopfilter },
    1478             : #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
    1479             :   { AOME_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
    1480             :   { AOME_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
    1481             :   { AOME_SET_TUNING, ctrl_set_tuning },
    1482             :   { AOME_SET_CQ_LEVEL, ctrl_set_cq_level },
    1483             :   { AOME_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
    1484             :   { AV1E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
    1485             :   { AV1E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
    1486             :   { AV1E_SET_LOSSLESS, ctrl_set_lossless },
    1487             : #if CONFIG_AOM_QM
    1488             :   { AV1E_SET_ENABLE_QM, ctrl_set_enable_qm },
    1489             :   { AV1E_SET_QM_MIN, ctrl_set_qm_min },
    1490             :   { AV1E_SET_QM_MAX, ctrl_set_qm_max },
    1491             : #endif
    1492             : #if CONFIG_TILE_GROUPS
    1493             :   { AV1E_SET_NUM_TG, ctrl_set_num_tg },
    1494             :   { AV1E_SET_MTU, ctrl_set_mtu },
    1495             : #endif
    1496             : #if CONFIG_TEMPMV_SIGNALING
    1497             :   { AV1E_SET_DISABLE_TEMPMV, ctrl_set_disable_tempmv },
    1498             : #endif
    1499             :   { AV1E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
    1500             :   { AV1E_SET_AQ_MODE, ctrl_set_aq_mode },
    1501             : #if CONFIG_EXT_DELTA_Q
    1502             :   { AV1E_SET_DELTAQ_MODE, ctrl_set_deltaq_mode },
    1503             : #endif
    1504             :   { AV1E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
    1505             :   { AV1E_SET_TUNE_CONTENT, ctrl_set_tune_content },
    1506             :   { AV1E_SET_COLOR_SPACE, ctrl_set_color_space },
    1507             :   { AV1E_SET_COLOR_RANGE, ctrl_set_color_range },
    1508             :   { AV1E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
    1509             :   { AV1E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
    1510             :   { AV1E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
    1511             :   { AV1E_SET_RENDER_SIZE, ctrl_set_render_size },
    1512             :   { AV1E_SET_SUPERBLOCK_SIZE, ctrl_set_superblock_size },
    1513             : #if CONFIG_ANS && ANS_MAX_SYMBOLS
    1514             :   { AV1E_SET_ANS_WINDOW_SIZE_LOG2, ctrl_set_ans_window_size_log2 },
    1515             : #endif
    1516             : #if CONFIG_EXT_TILE
    1517             :   { AV1E_SET_TILE_ENCODING_MODE, ctrl_set_tile_encoding_mode },
    1518             : #endif  // CONFIG_EXT_TILE
    1519             :   { AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
    1520             : 
    1521             :   // Getters
    1522             :   { AOME_GET_LAST_QUANTIZER, ctrl_get_quantizer },
    1523             :   { AOME_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
    1524             :   { AV1_GET_REFERENCE, ctrl_get_reference },
    1525             :   { AV1E_GET_ACTIVEMAP, ctrl_get_active_map },
    1526             :   { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
    1527             : 
    1528             :   { -1, NULL },
    1529             : };
    1530             : 
    1531             : static aom_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
    1532             :   { 0,
    1533             :     {
    1534             :         // NOLINT
    1535             :         0,  // g_usage
    1536             :         8,  // g_threads
    1537             :         0,  // g_profile
    1538             : 
    1539             :         320,         // g_width
    1540             :         240,         // g_height
    1541             :         AOM_BITS_8,  // g_bit_depth
    1542             :         8,           // g_input_bit_depth
    1543             : 
    1544             :         { 1, 30 },  // g_timebase
    1545             : 
    1546             :         0,  // g_error_resilient
    1547             : 
    1548             :         AOM_RC_ONE_PASS,  // g_pass
    1549             : 
    1550             :         25,  // g_lag_in_frames
    1551             : 
    1552             :         0,   // rc_dropframe_thresh
    1553             :         0,   // rc_resize_allowed
    1554             :         0,   // rc_scaled_width
    1555             :         0,   // rc_scaled_height
    1556             :         60,  // rc_resize_down_thresold
    1557             :         30,  // rc_resize_up_thresold
    1558             : 
    1559             :         AOM_VBR,      // rc_end_usage
    1560             :         { NULL, 0 },  // rc_twopass_stats_in
    1561             :         { NULL, 0 },  // rc_firstpass_mb_stats_in
    1562             :         256,          // rc_target_bandwidth
    1563             :         0,            // rc_min_quantizer
    1564             :         63,           // rc_max_quantizer
    1565             :         25,           // rc_undershoot_pct
    1566             :         25,           // rc_overshoot_pct
    1567             : 
    1568             :         6000,  // rc_max_buffer_size
    1569             :         4000,  // rc_buffer_initial_size
    1570             :         5000,  // rc_buffer_optimal_size
    1571             : 
    1572             :         50,    // rc_two_pass_vbrbias
    1573             :         0,     // rc_two_pass_vbrmin_section
    1574             :         2000,  // rc_two_pass_vbrmax_section
    1575             : 
    1576             :         // keyframing settings (kf)
    1577             :         AOM_KF_AUTO,  // g_kfmode
    1578             :         0,            // kf_min_dist
    1579             :         9999,         // kf_max_dist
    1580             :     } },
    1581             : };
    1582             : 
    1583             : #ifndef VERSION_STRING
    1584             : #define VERSION_STRING
    1585             : #endif
    1586           0 : CODEC_INTERFACE(aom_codec_av1_cx) = {
    1587             :   "AOMedia Project AV1 Encoder" VERSION_STRING,
    1588             :   AOM_CODEC_INTERNAL_ABI_VERSION,
    1589             : #if CONFIG_HIGHBITDEPTH
    1590             :   AOM_CODEC_CAP_HIGHBITDEPTH |
    1591             : #endif
    1592             :       AOM_CODEC_CAP_ENCODER | AOM_CODEC_CAP_PSNR,  // aom_codec_caps_t
    1593             :   encoder_init,                                    // aom_codec_init_fn_t
    1594             :   encoder_destroy,                                 // aom_codec_destroy_fn_t
    1595             :   encoder_ctrl_maps,                               // aom_codec_ctrl_fn_map_t
    1596             :   {
    1597             :       // NOLINT
    1598             :       NULL,  // aom_codec_peek_si_fn_t
    1599             :       NULL,  // aom_codec_get_si_fn_t
    1600             :       NULL,  // aom_codec_decode_fn_t
    1601             :       NULL,  // aom_codec_frame_get_fn_t
    1602             :       NULL   // aom_codec_set_fb_fn_t
    1603             :   },
    1604             :   {
    1605             :       // NOLINT
    1606             :       1,                      // 1 cfg map
    1607             :       encoder_usage_cfg_map,  // aom_codec_enc_cfg_map_t
    1608             :       encoder_encode,         // aom_codec_encode_fn_t
    1609             :       encoder_get_cxdata,     // aom_codec_get_cx_data_fn_t
    1610             :       encoder_set_config,     // aom_codec_enc_config_set_fn_t
    1611             :       NULL,                   // aom_codec_get_global_headers_fn_t
    1612             :       encoder_get_preview,    // aom_codec_get_preview_frame_fn_t
    1613             :       NULL                    // aom_codec_enc_mr_get_mem_loc_fn_t
    1614             :   }
    1615             : };

Generated by: LCOV version 1.13