blob: 5258db488ceb72437f130d661f856a6d87cef201 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <stdlib.h>
13#include <string.h>
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
16#include "aom/aom_encoder.h"
17#include "aom_ports/aom_once.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "aom/internal/aom_codec_internal.h"
20#include "./aom_version.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "av1/encoder/encoder.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070022#include "aom/aomcx.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070023#include "av1/encoder/firstpass.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070024#include "av1/av1_iface_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025
Yaowu Xuf883b422016-08-30 14:01:10 -070026struct av1_extracfg {
Yaowu Xuc27fc142016-08-22 16:08:15 -070027 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;
Fangwen Fu7b9f2b32017-01-17 14:01:52 -080037#if CONFIG_DEPENDENT_HORZTILES
38 unsigned int dependent_horz_tiles;
39#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -080040#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -080041 unsigned int loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -080042#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -070043 unsigned int arnr_max_frames;
44 unsigned int arnr_strength;
45 unsigned int min_gf_interval;
46 unsigned int max_gf_interval;
Yaowu Xuf883b422016-08-30 14:01:10 -070047 aom_tune_metric tuning;
Yaowu Xuc27fc142016-08-22 16:08:15 -070048 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
Thomas Daviesaf6df172016-11-09 14:04:18 +000058#if CONFIG_TILE_GROUPS
59 unsigned int num_tg;
60 unsigned int mtu_size;
61#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -080062#if CONFIG_TEMPMV_SIGNALING
63 unsigned int disable_tempmv;
64#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070065 unsigned int frame_parallel_decoding_mode;
66 AQ_MODE aq_mode;
Fangwen Fu6160df22017-04-24 09:45:51 -070067#if CONFIG_EXT_DELTA_Q
68 DELTAQ_MODE deltaq_mode;
69#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070070 unsigned int frame_periodic_boost;
Yaowu Xuf883b422016-08-30 14:01:10 -070071 aom_bit_depth_t bit_depth;
72 aom_tune_content content;
73 aom_color_space_t color_space;
anorkin76fb1262017-03-22 15:12:12 -070074#if CONFIG_COLORSPACE_HEADERS
75 aom_transfer_function_t transfer_function;
76 aom_chroma_sample_position_t chroma_sample_position;
77#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070078 int color_range;
79 int render_width;
80 int render_height;
Yaowu Xuf883b422016-08-30 14:01:10 -070081 aom_superblock_size_t superblock_size;
Alex Converseeb780e72016-12-13 12:46:41 -080082#if CONFIG_ANS && ANS_MAX_SYMBOLS
83 int ans_window_size_log2;
84#endif
Yunqing Wangd8cd55f2017-02-27 12:16:00 -080085#if CONFIG_EXT_TILE
86 unsigned int tile_encoding_mode;
87#endif // CONFIG_EXT_TILE
Yunqing Wangff4fa062017-04-21 10:56:08 -070088
89 unsigned int motion_vector_unit_test;
Yaowu Xuc27fc142016-08-22 16:08:15 -070090};
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static struct av1_extracfg default_extra_cfg = {
Yaowu Xuc27fc142016-08-22 16:08:15 -070093 0, // cpu_used
94 1, // enable_auto_alt_ref
95#if CONFIG_EXT_REFS
96 0, // enable_auto_bwd_ref
97#endif // CONFIG_EXT_REFS
98 0, // noise_sensitivity
99 0, // sharpness
100 0, // static_thresh
101#if CONFIG_EXT_TILE
102 UINT_MAX, // tile_columns
103 UINT_MAX, // tile_rows
104#else
105 0, // tile_columns
106 0, // tile_rows
Ryan Lei7386eda2016-12-08 21:08:31 -0800107#endif // CONFIG_EXT_TILE
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800108#if CONFIG_DEPENDENT_HORZTILES
anorkin76fb1262017-03-22 15:12:12 -0700109 0, // Dependent Horizontal tiles
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800110#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800111#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Leiad67d792017-02-02 17:53:19 -0800112 1, // loop_filter_across_tiles_enabled
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800113#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114 7, // arnr_max_frames
115 5, // arnr_strength
116 0, // min_gf_interval; 0 -> default decision
117 0, // max_gf_interval; 0 -> default decision
Yaowu Xuf883b422016-08-30 14:01:10 -0700118 AOM_TUNE_PSNR, // tuning
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119 10, // cq_level
120 0, // rc_max_intra_bitrate_pct
121 0, // rc_max_inter_bitrate_pct
122 0, // gf_cbr_boost_pct
123 0, // lossless
124#if CONFIG_AOM_QM
125 0, // enable_qm
126 DEFAULT_QM_FIRST, // qm_min
127 DEFAULT_QM_LAST, // qm_max
128#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000129#if CONFIG_TILE_GROUPS
130 1, // max number of tile groups
131 0, // mtu_size
132#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800133#if CONFIG_TEMPMV_SIGNALING
134 0, // disable temporal mv prediction
135#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700136 1, // frame_parallel_decoding_mode
137 NO_AQ, // aq_mode
138#if CONFIG_EXT_DELTA_Q
139 NO_DELTA_Q, // deltaq_mode
140#endif
anorkin76fb1262017-03-22 15:12:12 -0700141 CONFIG_XIPHRC, // frame_periodic_delta_q
142 AOM_BITS_8, // Bit depth
143 AOM_CONTENT_DEFAULT, // content
144 AOM_CS_UNKNOWN, // color space
145#if CONFIG_COLORSPACE_HEADERS
146 AOM_TF_UNKNOWN, // transfer function
147 AOM_CSP_UNKNOWN, // chroma sample position
148#endif
Alex Converseeb780e72016-12-13 12:46:41 -0800149 0, // color range
150 0, // render width
151 0, // render height
152 AOM_SUPERBLOCK_SIZE_DYNAMIC, // superblock_size
153#if CONFIG_ANS && ANS_MAX_SYMBOLS
154 23, // ans_window_size_log2
155#endif
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800156#if CONFIG_EXT_TILE
157 0, // Tile encoding mode is TILE_NORMAL by default.
158#endif // CONFIG_EXT_TILE
Yunqing Wangff4fa062017-04-21 10:56:08 -0700159
160 0, // motion_vector_unit_test
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161};
162
Yaowu Xuf883b422016-08-30 14:01:10 -0700163struct aom_codec_alg_priv {
164 aom_codec_priv_t base;
165 aom_codec_enc_cfg_t cfg;
166 struct av1_extracfg extra_cfg;
167 AV1EncoderConfig oxcf;
168 AV1_COMP *cpi;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169 unsigned char *cx_data;
170 size_t cx_data_sz;
171 unsigned char *pending_cx_data;
172 size_t pending_cx_data_sz;
173 int pending_frame_count;
174 size_t pending_frame_sizes[8];
Yaowu Xuf883b422016-08-30 14:01:10 -0700175 aom_image_t preview_img;
176 aom_enc_frame_flags_t next_frame_flags;
177 aom_postproc_cfg_t preview_ppcfg;
178 aom_codec_pkt_list_decl(256) pkt_list;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179 unsigned int fixed_kf_cntr;
180 // BufferPool that holds all reference frames.
181 BufferPool *buffer_pool;
182};
183
Yaowu Xuf883b422016-08-30 14:01:10 -0700184static aom_codec_err_t update_error_state(
185 aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
186 const aom_codec_err_t res = error->error_code;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187
Yaowu Xuf883b422016-08-30 14:01:10 -0700188 if (res != AOM_CODEC_OK)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 ctx->base.err_detail = error->has_detail ? error->detail : NULL;
190
191 return res;
192}
193
194#undef ERROR
195#define ERROR(str) \
196 do { \
197 ctx->base.err_detail = str; \
Yaowu Xuf883b422016-08-30 14:01:10 -0700198 return AOM_CODEC_INVALID_PARAM; \
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199 } while (0)
200
Urvang Joshicd8ab902016-10-28 12:32:06 -0700201#define RANGE_CHECK(p, memb, lo, hi) \
202 do { \
203 if (!((p)->memb >= (lo) && (p)->memb <= (hi))) \
204 ERROR(#memb " out of range [" #lo ".." #hi "]"); \
Yaowu Xuc27fc142016-08-22 16:08:15 -0700205 } while (0)
206
207#define RANGE_CHECK_HI(p, memb, hi) \
208 do { \
209 if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
210 } while (0)
211
212#define RANGE_CHECK_LO(p, memb, lo) \
213 do { \
214 if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
215 } while (0)
216
217#define RANGE_CHECK_BOOL(p, memb) \
218 do { \
219 if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
220 } while (0)
221
Yaowu Xuf883b422016-08-30 14:01:10 -0700222static aom_codec_err_t validate_config(aom_codec_alg_priv_t *ctx,
223 const aom_codec_enc_cfg_t *cfg,
224 const struct av1_extracfg *extra_cfg) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225 RANGE_CHECK(cfg, g_w, 1, 65535); // 16 bits available
226 RANGE_CHECK(cfg, g_h, 1, 65535); // 16 bits available
227 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
228 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
229 RANGE_CHECK_HI(cfg, g_profile, 3);
230
231 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
232 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
233 RANGE_CHECK_BOOL(extra_cfg, lossless);
234 RANGE_CHECK(extra_cfg, aq_mode, 0, AQ_MODE_COUNT - 1);
Fangwen Fu6160df22017-04-24 09:45:51 -0700235#if CONFIG_EXT_DELTA_Q
236 RANGE_CHECK(extra_cfg, deltaq_mode, 0, DELTAQ_MODE_COUNT - 1);
237#endif
Urvang Joshicd8ab902016-10-28 12:32:06 -0700238 RANGE_CHECK_HI(extra_cfg, frame_periodic_boost, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700239 RANGE_CHECK_HI(cfg, g_threads, 64);
240 RANGE_CHECK_HI(cfg, g_lag_in_frames, MAX_LAG_BUFFERS);
Yaowu Xuf883b422016-08-30 14:01:10 -0700241 RANGE_CHECK(cfg, rc_end_usage, AOM_VBR, AOM_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
243 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
244 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
Yaowu Xuf883b422016-08-30 14:01:10 -0700245 RANGE_CHECK(cfg, kf_mode, AOM_KF_DISABLED, AOM_KF_AUTO);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
247 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
248 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
249 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
Yaowu Xuf883b422016-08-30 14:01:10 -0700250 RANGE_CHECK(cfg, g_pass, AOM_RC_ONE_PASS, AOM_RC_LAST_PASS);
Urvang Joshicd8ab902016-10-28 12:32:06 -0700251 RANGE_CHECK_HI(extra_cfg, min_gf_interval, MAX_LAG_BUFFERS - 1);
252 RANGE_CHECK_HI(extra_cfg, max_gf_interval, MAX_LAG_BUFFERS - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253 if (extra_cfg->max_gf_interval > 0) {
254 RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
255 }
256 if (extra_cfg->min_gf_interval > 0 && extra_cfg->max_gf_interval > 0) {
257 RANGE_CHECK(extra_cfg, max_gf_interval, extra_cfg->min_gf_interval,
258 (MAX_LAG_BUFFERS - 1));
259 }
260
261 if (cfg->rc_resize_allowed == 1) {
Urvang Joshicd8ab902016-10-28 12:32:06 -0700262 RANGE_CHECK_HI(cfg, rc_scaled_width, cfg->g_w);
263 RANGE_CHECK_HI(cfg, rc_scaled_height, cfg->g_h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264 }
265
Yaowu Xuf883b422016-08-30 14:01:10 -0700266 // AV1 does not support a lower bound on the keyframe interval in
Yaowu Xuc27fc142016-08-22 16:08:15 -0700267 // automatic keyframe placement mode.
Yaowu Xuf883b422016-08-30 14:01:10 -0700268 if (cfg->kf_mode != AOM_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 cfg->kf_min_dist > 0)
270 ERROR(
271 "kf_min_dist not supported in auto mode, use 0 "
272 "or kf_max_dist instead.");
273
Yunqing Wangff4fa062017-04-21 10:56:08 -0700274 RANGE_CHECK_HI(extra_cfg, motion_vector_unit_test, 2);
Urvang Joshicd8ab902016-10-28 12:32:06 -0700275 RANGE_CHECK_HI(extra_cfg, enable_auto_alt_ref, 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276#if CONFIG_EXT_REFS
Urvang Joshicd8ab902016-10-28 12:32:06 -0700277 RANGE_CHECK_HI(extra_cfg, enable_auto_bwd_ref, 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278#endif // CONFIG_EXT_REFS
Alex Converse561d0af2017-03-23 12:58:04 -0700279 RANGE_CHECK(extra_cfg, cpu_used, 0, 8);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280 RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
Yaowu Xuf883b422016-08-30 14:01:10 -0700281 RANGE_CHECK(extra_cfg, superblock_size, AOM_SUPERBLOCK_SIZE_64X64,
282 AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283#if CONFIG_EXT_TILE
284// TODO(any): Waring. If CONFIG_EXT_TILE is true, tile_columns really
285// means tile_width, and tile_rows really means tile_hight. The interface
286// should be sanitized.
287#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700288 if (extra_cfg->superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 if (extra_cfg->tile_columns != UINT_MAX)
290 RANGE_CHECK(extra_cfg, tile_columns, 1, 32);
291 if (extra_cfg->tile_rows != UINT_MAX)
292 RANGE_CHECK(extra_cfg, tile_rows, 1, 32);
293 } else
294#endif // CONFIG_EXT_PARTITION
295 {
296 if (extra_cfg->tile_columns != UINT_MAX)
297 RANGE_CHECK(extra_cfg, tile_columns, 1, 64);
298 if (extra_cfg->tile_rows != UINT_MAX)
299 RANGE_CHECK(extra_cfg, tile_rows, 1, 64);
300 }
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800301 RANGE_CHECK_HI(extra_cfg, tile_encoding_mode, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700302#else
Urvang Joshicd8ab902016-10-28 12:32:06 -0700303 RANGE_CHECK_HI(extra_cfg, tile_columns, 6);
304 RANGE_CHECK_HI(extra_cfg, tile_rows, 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700305#endif // CONFIG_EXT_TILE
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800306#if CONFIG_DEPENDENT_HORZTILES
307 RANGE_CHECK_HI(extra_cfg, dependent_horz_tiles, 1);
308#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800309#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800310 RANGE_CHECK_HI(extra_cfg, loop_filter_across_tiles_enabled, 1);
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800311#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -0700312 RANGE_CHECK_HI(extra_cfg, sharpness, 7);
Urvang Joshicd8ab902016-10-28 12:32:06 -0700313 RANGE_CHECK_HI(extra_cfg, arnr_max_frames, 15);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700314 RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
Urvang Joshicd8ab902016-10-28 12:32:06 -0700315 RANGE_CHECK_HI(extra_cfg, cq_level, 63);
Yaowu Xuf883b422016-08-30 14:01:10 -0700316 RANGE_CHECK(cfg, g_bit_depth, AOM_BITS_8, AOM_BITS_12);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700317 RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
Yaowu Xuf883b422016-08-30 14:01:10 -0700318 RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319
Yaowu Xuf883b422016-08-30 14:01:10 -0700320 // TODO(yaowu): remove this when ssim tuning is implemented for av1
321 if (extra_cfg->tuning == AOM_TUNE_SSIM)
322 ERROR("Option --tune=ssim is not currently supported in AV1.");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323
Yaowu Xuf883b422016-08-30 14:01:10 -0700324 if (cfg->g_pass == AOM_RC_LAST_PASS) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +0000325#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
327 const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
328 const FIRSTPASS_STATS *stats;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +0000329#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700330
331 if (cfg->rc_twopass_stats_in.buf == NULL)
332 ERROR("rc_twopass_stats_in.buf not set.");
333
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +0000334#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335 if (cfg->rc_twopass_stats_in.sz % packet_sz)
336 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
337
338 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
339 ERROR("rc_twopass_stats_in requires at least two packets.");
340
341 stats =
342 (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
343
344 if ((int)(stats->count + 0.5) != n_packets - 1)
345 ERROR("rc_twopass_stats_in missing EOS stats packet");
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +0000346#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347 }
348
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200349#if !CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700350 if (cfg->g_profile > (unsigned int)PROFILE_1) {
351 ERROR("Profile > 1 not supported in this build configuration");
352 }
353#endif
354 if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700355 cfg->g_bit_depth > AOM_BITS_8) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700356 ERROR("Codec high bit-depth not supported in profile < 2");
357 }
358 if (cfg->g_profile <= (unsigned int)PROFILE_1 && cfg->g_input_bit_depth > 8) {
359 ERROR("Source high bit-depth not supported in profile < 2");
360 }
361 if (cfg->g_profile > (unsigned int)PROFILE_1 &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700362 cfg->g_bit_depth == AOM_BITS_8) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700363 ERROR("Codec bit-depth 8 not supported in profile > 1");
364 }
anorkin76fb1262017-03-22 15:12:12 -0700365#if CONFIG_COLORSPACE_HEADERS
366 RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_ICTCP);
367 RANGE_CHECK(extra_cfg, transfer_function, AOM_TF_UNKNOWN, AOM_TF_HLG);
368 RANGE_CHECK(extra_cfg, chroma_sample_position, AOM_CSP_UNKNOWN,
369 AOM_CSP_COLOCATED);
370#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700371 RANGE_CHECK(extra_cfg, color_space, AOM_CS_UNKNOWN, AOM_CS_SRGB);
anorkin76fb1262017-03-22 15:12:12 -0700372#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700373 RANGE_CHECK(extra_cfg, color_range, 0, 1);
Alex Converseeb780e72016-12-13 12:46:41 -0800374#if CONFIG_ANS && ANS_MAX_SYMBOLS
375 RANGE_CHECK(extra_cfg, ans_window_size_log2, 8, 23);
376#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700377 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700378}
379
Yaowu Xuf883b422016-08-30 14:01:10 -0700380static aom_codec_err_t validate_img(aom_codec_alg_priv_t *ctx,
381 const aom_image_t *img) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700382 switch (img->fmt) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700383 case AOM_IMG_FMT_YV12:
384 case AOM_IMG_FMT_I420:
385 case AOM_IMG_FMT_I42016: break;
386 case AOM_IMG_FMT_I422:
387 case AOM_IMG_FMT_I444:
388 case AOM_IMG_FMT_I440:
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389 if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
390 ERROR(
391 "Invalid image format. I422, I444, I440 images are "
392 "not supported in profile.");
393 }
394 break;
Yaowu Xuf883b422016-08-30 14:01:10 -0700395 case AOM_IMG_FMT_I42216:
396 case AOM_IMG_FMT_I44416:
397 case AOM_IMG_FMT_I44016:
Yaowu Xuc27fc142016-08-22 16:08:15 -0700398 if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
399 ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
400 ERROR(
401 "Invalid image format. 16-bit I422, I444, I440 images are "
402 "not supported in profile.");
403 }
404 break;
405 default:
406 ERROR(
407 "Invalid image format. Only YV12, I420, I422, I444 images are "
408 "supported.");
409 break;
410 }
411
412 if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
413 ERROR("Image size must match encoder init configuration size");
414
Yaowu Xuf883b422016-08-30 14:01:10 -0700415 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700416}
417
Yaowu Xuf883b422016-08-30 14:01:10 -0700418static int get_image_bps(const aom_image_t *img) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700419 switch (img->fmt) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700420 case AOM_IMG_FMT_YV12:
421 case AOM_IMG_FMT_I420: return 12;
422 case AOM_IMG_FMT_I422: return 16;
423 case AOM_IMG_FMT_I444: return 24;
424 case AOM_IMG_FMT_I440: return 16;
425 case AOM_IMG_FMT_I42016: return 24;
426 case AOM_IMG_FMT_I42216: return 32;
427 case AOM_IMG_FMT_I44416: return 48;
428 case AOM_IMG_FMT_I44016: return 32;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700429 default: assert(0 && "Invalid image format"); break;
430 }
431 return 0;
432}
433
Yaowu Xuf883b422016-08-30 14:01:10 -0700434static aom_codec_err_t set_encoder_config(
435 AV1EncoderConfig *oxcf, const aom_codec_enc_cfg_t *cfg,
436 const struct av1_extracfg *extra_cfg) {
437 const int is_vbr = cfg->rc_end_usage == AOM_VBR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700438 oxcf->profile = cfg->g_profile;
439 oxcf->max_threads = (int)cfg->g_threads;
440 oxcf->width = cfg->g_w;
441 oxcf->height = cfg->g_h;
442 oxcf->bit_depth = cfg->g_bit_depth;
443 oxcf->input_bit_depth = cfg->g_input_bit_depth;
444 // guess a frame rate if out of whack, use 30
445 oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
446 if (oxcf->init_framerate > 180) oxcf->init_framerate = 30;
447
448 oxcf->mode = GOOD;
449
450 switch (cfg->g_pass) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700451 case AOM_RC_ONE_PASS: oxcf->pass = 0; break;
452 case AOM_RC_FIRST_PASS: oxcf->pass = 1; break;
453 case AOM_RC_LAST_PASS: oxcf->pass = 2; break;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700454 }
455
456 oxcf->lag_in_frames =
Yaowu Xuf883b422016-08-30 14:01:10 -0700457 cfg->g_pass == AOM_RC_FIRST_PASS ? 0 : cfg->g_lag_in_frames;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700458 oxcf->rc_mode = cfg->rc_end_usage;
459
460 // Convert target bandwidth from Kbit/s to Bit/s
461 oxcf->target_bandwidth = 1000 * cfg->rc_target_bitrate;
462 oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
463 oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
464 oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
465
466 oxcf->best_allowed_q =
Yaowu Xuf883b422016-08-30 14:01:10 -0700467 extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_min_quantizer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700468 oxcf->worst_allowed_q =
Yaowu Xuf883b422016-08-30 14:01:10 -0700469 extra_cfg->lossless ? 0 : av1_quantizer_to_qindex(cfg->rc_max_quantizer);
470 oxcf->cq_level = av1_quantizer_to_qindex(extra_cfg->cq_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700471 oxcf->fixed_q = -1;
472
473#if CONFIG_AOM_QM
474 oxcf->using_qm = extra_cfg->enable_qm;
475 oxcf->qm_minlevel = extra_cfg->qm_min;
476 oxcf->qm_maxlevel = extra_cfg->qm_max;
477#endif
478
Thomas Daviesaf6df172016-11-09 14:04:18 +0000479#if CONFIG_TILE_GROUPS
480 oxcf->num_tile_groups = extra_cfg->num_tg;
481 oxcf->mtu = extra_cfg->mtu_size;
482#endif
483
Fangwen Fu8d164de2016-12-14 13:40:54 -0800484#if CONFIG_TEMPMV_SIGNALING
485 oxcf->disable_tempmv = extra_cfg->disable_tempmv;
486#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487 oxcf->under_shoot_pct = cfg->rc_undershoot_pct;
488 oxcf->over_shoot_pct = cfg->rc_overshoot_pct;
489
490 oxcf->scaled_frame_width = cfg->rc_scaled_width;
491 oxcf->scaled_frame_height = cfg->rc_scaled_height;
492 if (cfg->rc_resize_allowed == 1) {
493 oxcf->resize_mode =
494 (oxcf->scaled_frame_width == 0 || oxcf->scaled_frame_height == 0)
495 ? RESIZE_DYNAMIC
496 : RESIZE_FIXED;
497 } else {
498 oxcf->resize_mode = RESIZE_NONE;
499 }
500
Fergus Simpson3502d082017-04-10 12:25:07 -0700501 // Initialize to input resolution if not specified.
502 if (oxcf->resize_mode != RESIZE_FIXED) {
503 oxcf->scaled_frame_width = oxcf->width;
504 oxcf->scaled_frame_height = oxcf->height;
505 }
506
507#if CONFIG_FRAME_SUPERRES
508 oxcf->superres_enabled = 1; // TODO(afergs): Check the config
509#endif // CONFIG_FRAME_SUPERRES
510
Yaowu Xuc27fc142016-08-22 16:08:15 -0700511 oxcf->maximum_buffer_size_ms = is_vbr ? 240000 : cfg->rc_buf_sz;
512 oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
513 oxcf->optimal_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
514
515 oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
516
517 oxcf->two_pass_vbrbias = cfg->rc_2pass_vbr_bias_pct;
518 oxcf->two_pass_vbrmin_section = cfg->rc_2pass_vbr_minsection_pct;
519 oxcf->two_pass_vbrmax_section = cfg->rc_2pass_vbr_maxsection_pct;
520
521 oxcf->auto_key =
Yaowu Xuf883b422016-08-30 14:01:10 -0700522 cfg->kf_mode == AOM_KF_AUTO && cfg->kf_min_dist != cfg->kf_max_dist;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700523
524 oxcf->key_freq = cfg->kf_max_dist;
525
Alex Converse561d0af2017-03-23 12:58:04 -0700526 oxcf->speed = extra_cfg->cpu_used;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700527 oxcf->enable_auto_arf = extra_cfg->enable_auto_alt_ref;
528#if CONFIG_EXT_REFS
529 oxcf->enable_auto_brf = extra_cfg->enable_auto_bwd_ref;
530#endif // CONFIG_EXT_REFS
531 oxcf->noise_sensitivity = extra_cfg->noise_sensitivity;
532 oxcf->sharpness = extra_cfg->sharpness;
533
534 oxcf->two_pass_stats_in = cfg->rc_twopass_stats_in;
535
536#if CONFIG_FP_MB_STATS
537 oxcf->firstpass_mb_stats_in = cfg->rc_firstpass_mb_stats_in;
538#endif
539
540 oxcf->color_space = extra_cfg->color_space;
anorkin76fb1262017-03-22 15:12:12 -0700541#if CONFIG_COLORSPACE_HEADERS
542 oxcf->transfer_function = extra_cfg->transfer_function;
543 oxcf->chroma_sample_position = extra_cfg->chroma_sample_position;
544#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700545 oxcf->color_range = extra_cfg->color_range;
546 oxcf->render_width = extra_cfg->render_width;
547 oxcf->render_height = extra_cfg->render_height;
548 oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
549 oxcf->arnr_strength = extra_cfg->arnr_strength;
550 oxcf->min_gf_interval = extra_cfg->min_gf_interval;
551 oxcf->max_gf_interval = extra_cfg->max_gf_interval;
552
553 oxcf->tuning = extra_cfg->tuning;
554 oxcf->content = extra_cfg->content;
555
556#if CONFIG_EXT_PARTITION
557 oxcf->superblock_size = extra_cfg->superblock_size;
558#endif // CONFIG_EXT_PARTITION
Alex Converseeb780e72016-12-13 12:46:41 -0800559#if CONFIG_ANS && ANS_MAX_SYMBOLS
560 oxcf->ans_window_size_log2 = extra_cfg->ans_window_size_log2;
561#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562
563#if CONFIG_EXT_TILE
564 {
565#if CONFIG_EXT_PARTITION
566 const unsigned int max =
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 extra_cfg->superblock_size == AOM_SUPERBLOCK_SIZE_64X64 ? 64 : 32;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568#else
569 const unsigned int max = 64;
570#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700571 oxcf->tile_columns = AOMMIN(extra_cfg->tile_columns, max);
572 oxcf->tile_rows = AOMMIN(extra_cfg->tile_rows, max);
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800573 oxcf->tile_encoding_mode = extra_cfg->tile_encoding_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700574 }
575#else
576 oxcf->tile_columns = extra_cfg->tile_columns;
577 oxcf->tile_rows = extra_cfg->tile_rows;
578#endif // CONFIG_EXT_TILE
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800579#if CONFIG_DEPENDENT_HORZTILES
580 oxcf->dependent_horz_tiles = extra_cfg->dependent_horz_tiles;
581#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800582#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800583 oxcf->loop_filter_across_tiles_enabled =
584 extra_cfg->loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800585#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -0700586 oxcf->error_resilient_mode = cfg->g_error_resilient;
587 oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
588
589 oxcf->aq_mode = extra_cfg->aq_mode;
Fangwen Fu6160df22017-04-24 09:45:51 -0700590#if CONFIG_EXT_DELTA_Q
591 oxcf->deltaq_mode = extra_cfg->deltaq_mode;
592#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593
594 oxcf->frame_periodic_boost = extra_cfg->frame_periodic_boost;
595
Yunqing Wangff4fa062017-04-21 10:56:08 -0700596 oxcf->motion_vector_unit_test = extra_cfg->motion_vector_unit_test;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597 /*
Yaowu Xuf883b422016-08-30 14:01:10 -0700598 printf("Current AV1 Settings: \n");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700599 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
600 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
601 printf("sharpness: %d\n", oxcf->sharpness);
602 printf("cpu_used: %d\n", oxcf->cpu_used);
603 printf("Mode: %d\n", oxcf->mode);
604 printf("auto_key: %d\n", oxcf->auto_key);
605 printf("key_freq: %d\n", oxcf->key_freq);
606 printf("end_usage: %d\n", oxcf->end_usage);
607 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
608 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
609 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
610 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
611 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
612 printf("fixed_q: %d\n", oxcf->fixed_q);
613 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
614 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
615 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
616 printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
617 printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
618 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
619 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
620 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
621 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
622 printf("enable_auto_arf: %d\n", oxcf->enable_auto_arf);
623 printf("Version: %d\n", oxcf->Version);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700624 printf("error resilient: %d\n", oxcf->error_resilient_mode);
625 printf("frame parallel detokenization: %d\n",
626 oxcf->frame_parallel_decoding_mode);
627 */
Yaowu Xuf883b422016-08-30 14:01:10 -0700628 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629}
630
Yaowu Xuf883b422016-08-30 14:01:10 -0700631static aom_codec_err_t encoder_set_config(aom_codec_alg_priv_t *ctx,
632 const aom_codec_enc_cfg_t *cfg) {
633 aom_codec_err_t res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634 int force_key = 0;
635
636 if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700637 if (cfg->g_lag_in_frames > 1 || cfg->g_pass != AOM_RC_ONE_PASS)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638 ERROR("Cannot change width or height after initialization");
639 if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
640 (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
641 (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
642 force_key = 1;
643 }
644
645 // Prevent increasing lag_in_frames. This check is stricter than it needs
646 // to be -- the limit is not increasing past the first lag_in_frames
647 // value, but we don't track the initial config, only the last successful
648 // config.
649 if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
650 ERROR("Cannot increase lag_in_frames");
651
652 res = validate_config(ctx, cfg, &ctx->extra_cfg);
653
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 if (res == AOM_CODEC_OK) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 ctx->cfg = *cfg;
656 set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
657 // On profile change, request a key frame
658 force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
Yaowu Xuf883b422016-08-30 14:01:10 -0700659 av1_change_config(ctx->cpi, &ctx->oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660 }
661
Yaowu Xuf883b422016-08-30 14:01:10 -0700662 if (force_key) ctx->next_frame_flags |= AOM_EFLAG_FORCE_KF;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700663
664 return res;
665}
666
Yaowu Xuf883b422016-08-30 14:01:10 -0700667static aom_codec_err_t ctrl_get_quantizer(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668 va_list args) {
669 int *const arg = va_arg(args, int *);
Yaowu Xuf883b422016-08-30 14:01:10 -0700670 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
671 *arg = av1_get_quantizer(ctx->cpi);
672 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673}
674
Yaowu Xuf883b422016-08-30 14:01:10 -0700675static aom_codec_err_t ctrl_get_quantizer64(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676 va_list args) {
677 int *const arg = va_arg(args, int *);
Yaowu Xuf883b422016-08-30 14:01:10 -0700678 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
679 *arg = av1_qindex_to_quantizer(av1_get_quantizer(ctx->cpi));
680 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700681}
682
Yaowu Xuf883b422016-08-30 14:01:10 -0700683static aom_codec_err_t update_extra_cfg(aom_codec_alg_priv_t *ctx,
684 const struct av1_extracfg *extra_cfg) {
685 const aom_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
686 if (res == AOM_CODEC_OK) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 ctx->extra_cfg = *extra_cfg;
688 set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
Yaowu Xuf883b422016-08-30 14:01:10 -0700689 av1_change_config(ctx->cpi, &ctx->oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690 }
691 return res;
692}
693
Yaowu Xuf883b422016-08-30 14:01:10 -0700694static aom_codec_err_t ctrl_set_cpuused(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700695 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700696 struct av1_extracfg extra_cfg = ctx->extra_cfg;
697 extra_cfg.cpu_used = CAST(AOME_SET_CPUUSED, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698 return update_extra_cfg(ctx, &extra_cfg);
699}
700
Yaowu Xuf883b422016-08-30 14:01:10 -0700701static aom_codec_err_t ctrl_set_enable_auto_alt_ref(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700703 struct av1_extracfg extra_cfg = ctx->extra_cfg;
704 extra_cfg.enable_auto_alt_ref = CAST(AOME_SET_ENABLEAUTOALTREF, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700705 return update_extra_cfg(ctx, &extra_cfg);
706}
707
708#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -0700709static aom_codec_err_t ctrl_set_enable_auto_bwd_ref(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700711 struct av1_extracfg extra_cfg = ctx->extra_cfg;
712 extra_cfg.enable_auto_bwd_ref = CAST(AOME_SET_ENABLEAUTOBWDREF, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713 return update_extra_cfg(ctx, &extra_cfg);
714}
715#endif // CONFIG_EXT_REFS
716
Yaowu Xuf883b422016-08-30 14:01:10 -0700717static aom_codec_err_t ctrl_set_noise_sensitivity(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 struct av1_extracfg extra_cfg = ctx->extra_cfg;
720 extra_cfg.noise_sensitivity = CAST(AV1E_SET_NOISE_SENSITIVITY, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721 return update_extra_cfg(ctx, &extra_cfg);
722}
723
Yaowu Xuf883b422016-08-30 14:01:10 -0700724static aom_codec_err_t ctrl_set_sharpness(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700725 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 struct av1_extracfg extra_cfg = ctx->extra_cfg;
727 extra_cfg.sharpness = CAST(AOME_SET_SHARPNESS, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728 return update_extra_cfg(ctx, &extra_cfg);
729}
730
Yaowu Xuf883b422016-08-30 14:01:10 -0700731static aom_codec_err_t ctrl_set_static_thresh(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700733 struct av1_extracfg extra_cfg = ctx->extra_cfg;
734 extra_cfg.static_thresh = CAST(AOME_SET_STATIC_THRESHOLD, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 return update_extra_cfg(ctx, &extra_cfg);
736}
737
Yaowu Xuf883b422016-08-30 14:01:10 -0700738static aom_codec_err_t ctrl_set_tile_columns(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700740 struct av1_extracfg extra_cfg = ctx->extra_cfg;
741 extra_cfg.tile_columns = CAST(AV1E_SET_TILE_COLUMNS, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700742 return update_extra_cfg(ctx, &extra_cfg);
743}
744
Yaowu Xuf883b422016-08-30 14:01:10 -0700745static aom_codec_err_t ctrl_set_tile_rows(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700746 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700747 struct av1_extracfg extra_cfg = ctx->extra_cfg;
748 extra_cfg.tile_rows = CAST(AV1E_SET_TILE_ROWS, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749 return update_extra_cfg(ctx, &extra_cfg);
750}
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800751#if CONFIG_DEPENDENT_HORZTILES
752static aom_codec_err_t ctrl_set_tile_dependent_rows(aom_codec_alg_priv_t *ctx,
753 va_list args) {
754 struct av1_extracfg extra_cfg = ctx->extra_cfg;
755 extra_cfg.dependent_horz_tiles = CAST(AV1E_SET_TILE_DEPENDENT_ROWS, args);
756 return update_extra_cfg(ctx, &extra_cfg);
757}
758#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800759#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800760static aom_codec_err_t ctrl_set_tile_loopfilter(aom_codec_alg_priv_t *ctx,
761 va_list args) {
762 struct av1_extracfg extra_cfg = ctx->extra_cfg;
763 extra_cfg.loop_filter_across_tiles_enabled =
764 CAST(AV1E_SET_TILE_LOOPFILTER, args);
765 return update_extra_cfg(ctx, &extra_cfg);
766}
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800767#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800768
Yaowu Xuf883b422016-08-30 14:01:10 -0700769static aom_codec_err_t ctrl_set_arnr_max_frames(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700771 struct av1_extracfg extra_cfg = ctx->extra_cfg;
772 extra_cfg.arnr_max_frames = CAST(AOME_SET_ARNR_MAXFRAMES, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773 return update_extra_cfg(ctx, &extra_cfg);
774}
775
Yaowu Xuf883b422016-08-30 14:01:10 -0700776static aom_codec_err_t ctrl_set_arnr_strength(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700778 struct av1_extracfg extra_cfg = ctx->extra_cfg;
779 extra_cfg.arnr_strength = CAST(AOME_SET_ARNR_STRENGTH, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 return update_extra_cfg(ctx, &extra_cfg);
781}
782
Yaowu Xuf883b422016-08-30 14:01:10 -0700783static aom_codec_err_t ctrl_set_tuning(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 struct av1_extracfg extra_cfg = ctx->extra_cfg;
786 extra_cfg.tuning = CAST(AOME_SET_TUNING, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787 return update_extra_cfg(ctx, &extra_cfg);
788}
789
Yaowu Xuf883b422016-08-30 14:01:10 -0700790static aom_codec_err_t ctrl_set_cq_level(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 struct av1_extracfg extra_cfg = ctx->extra_cfg;
793 extra_cfg.cq_level = CAST(AOME_SET_CQ_LEVEL, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794 return update_extra_cfg(ctx, &extra_cfg);
795}
796
Yaowu Xuf883b422016-08-30 14:01:10 -0700797static aom_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
798 aom_codec_alg_priv_t *ctx, va_list args) {
799 struct av1_extracfg extra_cfg = ctx->extra_cfg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 extra_cfg.rc_max_intra_bitrate_pct =
Yaowu Xuf883b422016-08-30 14:01:10 -0700801 CAST(AOME_SET_MAX_INTRA_BITRATE_PCT, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 return update_extra_cfg(ctx, &extra_cfg);
803}
804
Yaowu Xuf883b422016-08-30 14:01:10 -0700805static aom_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
806 aom_codec_alg_priv_t *ctx, va_list args) {
807 struct av1_extracfg extra_cfg = ctx->extra_cfg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808 extra_cfg.rc_max_inter_bitrate_pct =
Yaowu Xuf883b422016-08-30 14:01:10 -0700809 CAST(AOME_SET_MAX_INTER_BITRATE_PCT, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810 return update_extra_cfg(ctx, &extra_cfg);
811}
812
Yaowu Xuf883b422016-08-30 14:01:10 -0700813static aom_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 struct av1_extracfg extra_cfg = ctx->extra_cfg;
816 extra_cfg.gf_cbr_boost_pct = CAST(AV1E_SET_GF_CBR_BOOST_PCT, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817 return update_extra_cfg(ctx, &extra_cfg);
818}
819
Yaowu Xuf883b422016-08-30 14:01:10 -0700820static aom_codec_err_t ctrl_set_lossless(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700821 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700822 struct av1_extracfg extra_cfg = ctx->extra_cfg;
823 extra_cfg.lossless = CAST(AV1E_SET_LOSSLESS, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824 return update_extra_cfg(ctx, &extra_cfg);
825}
826
827#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -0700828static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700830 struct av1_extracfg extra_cfg = ctx->extra_cfg;
831 extra_cfg.enable_qm = CAST(AV1E_SET_ENABLE_QM, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832 return update_extra_cfg(ctx, &extra_cfg);
833}
834
Yaowu Xuf883b422016-08-30 14:01:10 -0700835static aom_codec_err_t ctrl_set_qm_min(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700836 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700837 struct av1_extracfg extra_cfg = ctx->extra_cfg;
838 extra_cfg.qm_min = CAST(AV1E_SET_QM_MIN, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 return update_extra_cfg(ctx, &extra_cfg);
840}
841
Yaowu Xuf883b422016-08-30 14:01:10 -0700842static aom_codec_err_t ctrl_set_qm_max(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700844 struct av1_extracfg extra_cfg = ctx->extra_cfg;
845 extra_cfg.qm_max = CAST(AV1E_SET_QM_MAX, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846 return update_extra_cfg(ctx, &extra_cfg);
847}
848#endif
849
Thomas Daviesaf6df172016-11-09 14:04:18 +0000850#if CONFIG_TILE_GROUPS
851static aom_codec_err_t ctrl_set_num_tg(aom_codec_alg_priv_t *ctx,
852 va_list args) {
853 struct av1_extracfg extra_cfg = ctx->extra_cfg;
854 extra_cfg.num_tg = CAST(AV1E_SET_NUM_TG, args);
855 return update_extra_cfg(ctx, &extra_cfg);
856}
857
858static aom_codec_err_t ctrl_set_mtu(aom_codec_alg_priv_t *ctx, va_list args) {
859 struct av1_extracfg extra_cfg = ctx->extra_cfg;
860 extra_cfg.mtu_size = CAST(AV1E_SET_MTU, args);
861 return update_extra_cfg(ctx, &extra_cfg);
862}
863#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800864#if CONFIG_TEMPMV_SIGNALING
865static aom_codec_err_t ctrl_set_disable_tempmv(aom_codec_alg_priv_t *ctx,
866 va_list args) {
867 struct av1_extracfg extra_cfg = ctx->extra_cfg;
868 extra_cfg.disable_tempmv = CAST(AV1E_SET_DISABLE_TEMPMV, args);
869 return update_extra_cfg(ctx, &extra_cfg);
870}
871#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700872static aom_codec_err_t ctrl_set_frame_parallel_decoding_mode(
873 aom_codec_alg_priv_t *ctx, va_list args) {
874 struct av1_extracfg extra_cfg = ctx->extra_cfg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 extra_cfg.frame_parallel_decoding_mode =
Yaowu Xuf883b422016-08-30 14:01:10 -0700876 CAST(AV1E_SET_FRAME_PARALLEL_DECODING, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700877 return update_extra_cfg(ctx, &extra_cfg);
878}
879
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800880#if CONFIG_EXT_TILE
881static aom_codec_err_t ctrl_set_tile_encoding_mode(aom_codec_alg_priv_t *ctx,
882 va_list args) {
883 struct av1_extracfg extra_cfg = ctx->extra_cfg;
884 extra_cfg.tile_encoding_mode = CAST(AV1E_SET_TILE_ENCODING_MODE, args);
885 return update_extra_cfg(ctx, &extra_cfg);
886}
887#endif // CONFIG_EXT_TILE
888
Yaowu Xuf883b422016-08-30 14:01:10 -0700889static aom_codec_err_t ctrl_set_aq_mode(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700890 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700891 struct av1_extracfg extra_cfg = ctx->extra_cfg;
892 extra_cfg.aq_mode = CAST(AV1E_SET_AQ_MODE, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 return update_extra_cfg(ctx, &extra_cfg);
894}
895
Fangwen Fu6160df22017-04-24 09:45:51 -0700896#if CONFIG_EXT_DELTA_Q
897static aom_codec_err_t ctrl_set_deltaq_mode(aom_codec_alg_priv_t *ctx,
898 va_list args) {
899 struct av1_extracfg extra_cfg = ctx->extra_cfg;
900 extra_cfg.deltaq_mode = CAST(AV1E_SET_DELTAQ_MODE, args);
901 return update_extra_cfg(ctx, &extra_cfg);
902}
903#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700904static aom_codec_err_t ctrl_set_min_gf_interval(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700906 struct av1_extracfg extra_cfg = ctx->extra_cfg;
907 extra_cfg.min_gf_interval = CAST(AV1E_SET_MIN_GF_INTERVAL, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908 return update_extra_cfg(ctx, &extra_cfg);
909}
910
Yaowu Xuf883b422016-08-30 14:01:10 -0700911static aom_codec_err_t ctrl_set_max_gf_interval(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700912 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700913 struct av1_extracfg extra_cfg = ctx->extra_cfg;
914 extra_cfg.max_gf_interval = CAST(AV1E_SET_MAX_GF_INTERVAL, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700915 return update_extra_cfg(ctx, &extra_cfg);
916}
917
Yaowu Xuf883b422016-08-30 14:01:10 -0700918static aom_codec_err_t ctrl_set_frame_periodic_boost(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700920 struct av1_extracfg extra_cfg = ctx->extra_cfg;
921 extra_cfg.frame_periodic_boost = CAST(AV1E_SET_FRAME_PERIODIC_BOOST, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 return update_extra_cfg(ctx, &extra_cfg);
923}
924
Yunqing Wangff4fa062017-04-21 10:56:08 -0700925static aom_codec_err_t ctrl_enable_motion_vector_unit_test(
926 aom_codec_alg_priv_t *ctx, va_list args) {
927 struct av1_extracfg extra_cfg = ctx->extra_cfg;
928 extra_cfg.motion_vector_unit_test =
929 CAST(AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, args);
930 return update_extra_cfg(ctx, &extra_cfg);
931}
932
Yaowu Xuf883b422016-08-30 14:01:10 -0700933static aom_codec_err_t encoder_init(aom_codec_ctx_t *ctx,
934 aom_codec_priv_enc_mr_cfg_t *data) {
935 aom_codec_err_t res = AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936 (void)data;
937
938 if (ctx->priv == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700939 aom_codec_alg_priv_t *const priv = aom_calloc(1, sizeof(*priv));
940 if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941
Yaowu Xuf883b422016-08-30 14:01:10 -0700942 ctx->priv = (aom_codec_priv_t *)priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700943 ctx->priv->init_flags = ctx->init_flags;
944 ctx->priv->enc.total_encoders = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700945 priv->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
946 if (priv->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700947
948#if CONFIG_MULTITHREAD
949 if (pthread_mutex_init(&priv->buffer_pool->pool_mutex, NULL)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700950 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 }
952#endif
953
954 if (ctx->config.enc) {
955 // Update the reference to the config structure to an internal copy.
956 priv->cfg = *ctx->config.enc;
957 ctx->config.enc = &priv->cfg;
958 }
959
960 priv->extra_cfg = default_extra_cfg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700961 once(av1_initialize_enc);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962
963 res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
964
Yaowu Xuf883b422016-08-30 14:01:10 -0700965 if (res == AOM_CODEC_OK) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700966 set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200967#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700968 priv->oxcf.use_highbitdepth =
Yaowu Xuf883b422016-08-30 14:01:10 -0700969 (ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700970#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700971 priv->cpi = av1_create_compressor(&priv->oxcf, priv->buffer_pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972 if (priv->cpi == NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -0700973 res = AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700974 else
975 priv->cpi->output_pkt_list = &priv->pkt_list.head;
976 }
977 }
978
979 return res;
980}
981
Yaowu Xuf883b422016-08-30 14:01:10 -0700982static aom_codec_err_t encoder_destroy(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700983 free(ctx->cx_data);
Yaowu Xuf883b422016-08-30 14:01:10 -0700984 av1_remove_compressor(ctx->cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700985#if CONFIG_MULTITHREAD
986 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
987#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700988 aom_free(ctx->buffer_pool);
989 aom_free(ctx);
990 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700991}
992
Yaowu Xuf883b422016-08-30 14:01:10 -0700993static void pick_quickcompress_mode(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700994 unsigned long deadline) {
Thomas Daede6eca8352017-03-17 14:14:12 -0700995 MODE new_mode = GOOD;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700996
997 switch (ctx->cfg.g_pass) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700998 case AOM_RC_ONE_PASS:
Thomas Daedeac0a3802016-03-01 14:29:47 -0800999 switch (deadline) {
Thomas Daedeac0a3802016-03-01 14:29:47 -08001000 default: new_mode = GOOD; break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001001 }
1002 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001003 case AOM_RC_FIRST_PASS: break;
Thomas Daede6eca8352017-03-17 14:14:12 -07001004 case AOM_RC_LAST_PASS: new_mode = GOOD;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001005 }
1006
1007 if (ctx->oxcf.mode != new_mode) {
1008 ctx->oxcf.mode = new_mode;
Yaowu Xuf883b422016-08-30 14:01:10 -07001009 av1_change_config(ctx->cpi, &ctx->oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001010 }
1011}
1012
1013// Turn on to test if supplemental superframe data breaks decoding
1014// #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
Yaowu Xuf883b422016-08-30 14:01:10 -07001015static int write_superframe_index(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016 uint8_t marker = 0xc0;
1017 unsigned int mask;
1018 int mag, index_sz;
1019 int i;
1020 size_t max_frame_sz = 0;
1021
1022 assert(ctx->pending_frame_count);
1023 assert(ctx->pending_frame_count <= 8);
1024
1025 // Add the number of frames to the marker byte
1026 marker |= ctx->pending_frame_count - 1;
1027 for (i = 0; i < ctx->pending_frame_count - 1; i++) {
1028 const size_t frame_sz = (unsigned int)ctx->pending_frame_sizes[i] - 1;
1029 max_frame_sz = frame_sz > max_frame_sz ? frame_sz : max_frame_sz;
1030 }
1031
1032 // Choose the magnitude
1033 for (mag = 0, mask = 0xff; mag < 4; mag++) {
1034 if (max_frame_sz <= mask) break;
1035 mask <<= 8;
1036 mask |= 0xff;
1037 }
1038 marker |= mag << 3;
1039
1040 // Write the index
1041 index_sz = 2 + (mag + 1) * (ctx->pending_frame_count - 1);
1042 if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
1043 uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001044#ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1045 uint8_t marker_test = 0xc0;
1046 int mag_test = 2; // 1 - 4
1047 int frames_test = 4; // 1 - 8
1048 int index_sz_test = 2 + mag_test * frames_test;
1049 marker_test |= frames_test - 1;
1050 marker_test |= (mag_test - 1) << 3;
1051 *x++ = marker_test;
1052 for (i = 0; i < mag_test * frames_test; ++i)
1053 *x++ = 0; // fill up with arbitrary data
1054 *x++ = marker_test;
1055 ctx->pending_cx_data_sz += index_sz_test;
1056 printf("Added supplemental superframe data\n");
1057#endif
1058
1059 *x++ = marker;
1060 for (i = 0; i < ctx->pending_frame_count - 1; i++) {
1061 unsigned int this_sz;
Urvang Joshi368fbc92016-10-17 16:31:34 -07001062 int j;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001063
1064 assert(ctx->pending_frame_sizes[i] > 0);
1065 this_sz = (unsigned int)ctx->pending_frame_sizes[i] - 1;
1066 for (j = 0; j <= mag; j++) {
1067 *x++ = this_sz & 0xff;
1068 this_sz >>= 8;
1069 }
1070 }
1071 *x++ = marker;
1072 ctx->pending_cx_data_sz += index_sz;
1073#ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
1074 index_sz += index_sz_test;
1075#endif
1076 }
1077 return index_sz;
1078}
1079
Yaowu Xuf883b422016-08-30 14:01:10 -07001080// av1 uses 10,000,000 ticks/second as time stamp
Yaowu Xuc27fc142016-08-22 16:08:15 -07001081#define TICKS_PER_SEC 10000000LL
1082
Yaowu Xuf883b422016-08-30 14:01:10 -07001083static int64_t timebase_units_to_ticks(const aom_rational_t *timebase,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001084 int64_t n) {
1085 return n * TICKS_PER_SEC * timebase->num / timebase->den;
1086}
1087
Yaowu Xuf883b422016-08-30 14:01:10 -07001088static int64_t ticks_to_timebase_units(const aom_rational_t *timebase,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001089 int64_t n) {
1090 const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
1091 return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
1092}
1093
Yaowu Xuf883b422016-08-30 14:01:10 -07001094static aom_codec_frame_flags_t get_frame_pkt_flags(const AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001095 unsigned int lib_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001096 aom_codec_frame_flags_t flags = lib_flags << 16;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001097
Yaowu Xuf883b422016-08-30 14:01:10 -07001098 if (lib_flags & FRAMEFLAGS_KEY) flags |= AOM_FRAME_IS_KEY;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001099
Yaowu Xuf883b422016-08-30 14:01:10 -07001100 if (cpi->droppable) flags |= AOM_FRAME_IS_DROPPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001101
1102 return flags;
1103}
1104
Yaowu Xuf883b422016-08-30 14:01:10 -07001105static aom_codec_err_t encoder_encode(aom_codec_alg_priv_t *ctx,
1106 const aom_image_t *img,
1107 aom_codec_pts_t pts,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001108 unsigned long duration,
Yaowu Xuf883b422016-08-30 14:01:10 -07001109 aom_enc_frame_flags_t enc_flags,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001110 unsigned long deadline) {
Ralph Giles19944292017-04-18 13:46:56 -07001111 const size_t kMinCompressedSize = 8192;
Yaowu Xuf883b422016-08-30 14:01:10 -07001112 volatile aom_codec_err_t res = AOM_CODEC_OK;
1113 volatile aom_enc_frame_flags_t flags = enc_flags;
1114 AV1_COMP *const cpi = ctx->cpi;
1115 const aom_rational_t *const timebase = &ctx->cfg.g_timebase;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001116 size_t data_sz;
1117
Yaowu Xuf883b422016-08-30 14:01:10 -07001118 if (cpi == NULL) return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001119
1120 if (img != NULL) {
1121 res = validate_img(ctx, img);
1122 // TODO(jzern) the checks related to cpi's validity should be treated as a
1123 // failure condition, encoder setup is done fully in init() currently.
Yaowu Xuf883b422016-08-30 14:01:10 -07001124 if (res == AOM_CODEC_OK) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001125#if CONFIG_EXT_REFS
Yaowu Xuabc7d812016-07-12 11:57:51 -07001126 data_sz = ALIGN_POWER_OF_TWO(ctx->cfg.g_w, 5) *
1127 ALIGN_POWER_OF_TWO(ctx->cfg.g_h, 5) * get_image_bps(img);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001128#else
1129 // There's no codec control for multiple alt-refs so check the encoder
1130 // instance for its status to determine the compressed data size.
Yaowu Xuabc7d812016-07-12 11:57:51 -07001131 data_sz = ALIGN_POWER_OF_TWO(ctx->cfg.g_w, 5) *
1132 ALIGN_POWER_OF_TWO(ctx->cfg.g_h, 5) * get_image_bps(img) / 8 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07001133 (cpi->multi_arf_allowed ? 8 : 2);
1134#endif // CONFIG_EXT_REFS
Yaowu Xu416b0d92016-07-11 11:38:53 -07001135 if (data_sz < kMinCompressedSize) data_sz = kMinCompressedSize;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001136 if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
1137 ctx->cx_data_sz = data_sz;
1138 free(ctx->cx_data);
1139 ctx->cx_data = (unsigned char *)malloc(ctx->cx_data_sz);
1140 if (ctx->cx_data == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001141 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001142 }
1143 }
1144 }
1145 }
1146
Thomas Daedeac0a3802016-03-01 14:29:47 -08001147 pick_quickcompress_mode(ctx, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001148 aom_codec_pkt_list_init(&ctx->pkt_list);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001149
1150 // Handle Flags
Yaowu Xuf883b422016-08-30 14:01:10 -07001151 if (((flags & AOM_EFLAG_NO_UPD_GF) && (flags & AOM_EFLAG_FORCE_GF)) ||
1152 ((flags & AOM_EFLAG_NO_UPD_ARF) && (flags & AOM_EFLAG_FORCE_ARF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001153 ctx->base.err_detail = "Conflicting flags.";
Yaowu Xuf883b422016-08-30 14:01:10 -07001154 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001155 }
1156
1157 if (setjmp(cpi->common.error.jmp)) {
1158 cpi->common.error.setjmp = 0;
1159 res = update_error_state(ctx, &cpi->common.error);
Yaowu Xuf883b422016-08-30 14:01:10 -07001160 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07001161 return res;
1162 }
1163 cpi->common.error.setjmp = 1;
1164
Yaowu Xuf883b422016-08-30 14:01:10 -07001165 av1_apply_encoding_flags(cpi, flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166
1167 // Handle fixed keyframe intervals
Yaowu Xuf883b422016-08-30 14:01:10 -07001168 if (ctx->cfg.kf_mode == AOM_KF_AUTO &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07001169 ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
1170 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001171 flags |= AOM_EFLAG_FORCE_KF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001172 ctx->fixed_kf_cntr = 1;
1173 }
1174 }
1175
Yaowu Xuf883b422016-08-30 14:01:10 -07001176 if (res == AOM_CODEC_OK) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001177 unsigned int lib_flags = 0;
1178 YV12_BUFFER_CONFIG sd;
1179 int64_t dst_time_stamp = timebase_units_to_ticks(timebase, pts);
1180 int64_t dst_end_time_stamp =
1181 timebase_units_to_ticks(timebase, pts + duration);
1182 size_t size, cx_data_sz;
1183 unsigned char *cx_data;
1184
1185 // Set up internal flags
Yaowu Xuf883b422016-08-30 14:01:10 -07001186 if (ctx->base.init_flags & AOM_CODEC_USE_PSNR) cpi->b_calculate_psnr = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001187
1188 if (img != NULL) {
1189 res = image2yuvconfig(img, &sd);
1190
1191 // Store the original flags in to the frame buffer. Will extract the
1192 // key frame flag when we actually encode this frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07001193 if (av1_receive_raw_frame(cpi, flags | ctx->next_frame_flags, &sd,
1194 dst_time_stamp, dst_end_time_stamp)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001195 res = update_error_state(ctx, &cpi->common.error);
1196 }
1197 ctx->next_frame_flags = 0;
1198 }
1199
1200 cx_data = ctx->cx_data;
1201 cx_data_sz = ctx->cx_data_sz;
1202
1203 /* Any pending invisible frames? */
1204 if (ctx->pending_cx_data) {
1205 memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
1206 ctx->pending_cx_data = cx_data;
1207 cx_data += ctx->pending_cx_data_sz;
1208 cx_data_sz -= ctx->pending_cx_data_sz;
1209
1210 /* TODO: this is a minimal check, the underlying codec doesn't respect
1211 * the buffer size anyway.
1212 */
1213 if (cx_data_sz < ctx->cx_data_sz / 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001214 aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001215 "Compressed data buffer too small");
Yaowu Xuf883b422016-08-30 14:01:10 -07001216 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001217 }
1218 }
1219
1220 while (cx_data_sz >= ctx->cx_data_sz / 2 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07001221 -1 != av1_get_compressed_data(cpi, &lib_flags, &size, cx_data,
1222 &dst_time_stamp, &dst_end_time_stamp,
1223 !img)) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01001224#if CONFIG_REFERENCE_BUFFER
1225 if (cpi->common.invalid_delta_frame_id_minus1) {
1226 ctx->base.err_detail = "Invalid delta_frame_id_minus1";
1227 return AOM_CODEC_ERROR;
1228 }
1229#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001230 if (size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001231 aom_codec_cx_pkt_t pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001232
1233 // Pack invisible frames with the next visible frame
1234 if (!cpi->common.show_frame) {
1235 if (ctx->pending_cx_data == 0) ctx->pending_cx_data = cx_data;
1236 ctx->pending_cx_data_sz += size;
1237 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1238 cx_data += size;
1239 cx_data_sz -= size;
1240
1241 continue;
1242 }
1243
1244 // Add the frame packet to the list of returned packets.
Yaowu Xuf883b422016-08-30 14:01:10 -07001245 pkt.kind = AOM_CODEC_CX_FRAME_PKT;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001246 pkt.data.frame.pts = ticks_to_timebase_units(timebase, dst_time_stamp);
1247 pkt.data.frame.duration = (unsigned long)ticks_to_timebase_units(
1248 timebase, dst_end_time_stamp - dst_time_stamp);
1249 pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1250
1251 if (ctx->pending_cx_data) {
1252 ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1253 ctx->pending_cx_data_sz += size;
1254 size += write_superframe_index(ctx);
1255 pkt.data.frame.buf = ctx->pending_cx_data;
1256 pkt.data.frame.sz = ctx->pending_cx_data_sz;
1257 ctx->pending_cx_data = NULL;
1258 ctx->pending_cx_data_sz = 0;
1259 ctx->pending_frame_count = 0;
1260 } else {
1261 pkt.data.frame.buf = cx_data;
1262 pkt.data.frame.sz = size;
1263 }
1264 pkt.data.frame.partition_id = -1;
1265
Yaowu Xuf883b422016-08-30 14:01:10 -07001266 aom_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001267
1268 cx_data += size;
1269 cx_data_sz -= size;
1270 }
1271 }
1272 }
1273
1274 cpi->common.error.setjmp = 0;
1275 return res;
1276}
1277
Yaowu Xuf883b422016-08-30 14:01:10 -07001278static const aom_codec_cx_pkt_t *encoder_get_cxdata(aom_codec_alg_priv_t *ctx,
1279 aom_codec_iter_t *iter) {
1280 return aom_codec_pkt_list_get(&ctx->pkt_list.head, iter);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001281}
1282
Yaowu Xuf883b422016-08-30 14:01:10 -07001283static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001284 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001285 aom_ref_frame_t *const frame = va_arg(args, aom_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001286
1287 if (frame != NULL) {
1288 YV12_BUFFER_CONFIG sd;
1289
1290 image2yuvconfig(&frame->img, &sd);
Yaowu Xuf883b422016-08-30 14:01:10 -07001291 av1_set_reference_enc(ctx->cpi, ref_frame_to_av1_reframe(frame->frame_type),
1292 &sd);
1293 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001294 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001295 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001296 }
1297}
1298
Yaowu Xuf883b422016-08-30 14:01:10 -07001299static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001300 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001301 aom_ref_frame_t *const frame = va_arg(args, aom_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001302
1303 if (frame != NULL) {
1304 YV12_BUFFER_CONFIG sd;
1305
1306 image2yuvconfig(&frame->img, &sd);
Yaowu Xuf883b422016-08-30 14:01:10 -07001307 av1_copy_reference_enc(ctx->cpi,
1308 ref_frame_to_av1_reframe(frame->frame_type), &sd);
1309 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001310 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001311 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312 }
1313}
1314
Yaowu Xuf883b422016-08-30 14:01:10 -07001315static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001316 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001317 av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001318
1319 if (frame != NULL) {
1320 YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->cpi->common, frame->idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07001321 if (fb == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001322
1323 yuvconfig2image(&frame->img, fb, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07001324 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001325 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001326 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001327 }
1328}
1329
Yaowu Xuf883b422016-08-30 14:01:10 -07001330static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001331 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001332 aom_image_t *const new_img = va_arg(args, aom_image_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001333
1334 if (new_img != NULL) {
1335 YV12_BUFFER_CONFIG new_frame;
1336
Yaowu Xuf883b422016-08-30 14:01:10 -07001337 if (av1_get_last_show_frame(ctx->cpi, &new_frame) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001338 yuvconfig2image(new_img, &new_frame, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07001339 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001340 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001341 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001342 }
1343 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001344 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001345 }
1346}
1347
Yaowu Xuf883b422016-08-30 14:01:10 -07001348static aom_codec_err_t ctrl_set_previewpp(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001349 va_list args) {
1350 (void)ctx;
1351 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -07001352 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001353}
1354
Yaowu Xuf883b422016-08-30 14:01:10 -07001355static aom_image_t *encoder_get_preview(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001356 YV12_BUFFER_CONFIG sd;
1357
Yaowu Xuf883b422016-08-30 14:01:10 -07001358 if (av1_get_preview_raw_frame(ctx->cpi, &sd) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001359 yuvconfig2image(&ctx->preview_img, &sd, NULL);
1360 return &ctx->preview_img;
1361 } else {
1362 return NULL;
1363 }
1364}
1365
Yaowu Xuf883b422016-08-30 14:01:10 -07001366static aom_codec_err_t ctrl_use_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001367 va_list args) {
1368 const int reference_flag = va_arg(args, int);
1369
Yaowu Xuf883b422016-08-30 14:01:10 -07001370 av1_use_as_reference(ctx->cpi, reference_flag);
1371 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001372}
1373
Yaowu Xuf883b422016-08-30 14:01:10 -07001374static aom_codec_err_t ctrl_set_roi_map(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001375 va_list args) {
1376 (void)ctx;
1377 (void)args;
1378
Yaowu Xuf883b422016-08-30 14:01:10 -07001379 // TODO(yaowu): Need to re-implement and test for AV1.
1380 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001381}
1382
Yaowu Xuf883b422016-08-30 14:01:10 -07001383static aom_codec_err_t ctrl_set_active_map(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001384 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001385 aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386
1387 if (map) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001388 if (!av1_set_active_map(ctx->cpi, map->active_map, (int)map->rows,
1389 (int)map->cols))
1390 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001391 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001392 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001393 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001394 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001395 }
1396}
1397
Yaowu Xuf883b422016-08-30 14:01:10 -07001398static aom_codec_err_t ctrl_get_active_map(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001399 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001400 aom_active_map_t *const map = va_arg(args, aom_active_map_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001401
1402 if (map) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001403 if (!av1_get_active_map(ctx->cpi, map->active_map, (int)map->rows,
1404 (int)map->cols))
1405 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001406 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001407 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001408 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001409 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001410 }
1411}
1412
Yaowu Xuf883b422016-08-30 14:01:10 -07001413static aom_codec_err_t ctrl_set_scale_mode(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001415 aom_scaling_mode_t *const mode = va_arg(args, aom_scaling_mode_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001416
1417 if (mode) {
1418 const int res =
Yaowu Xuf883b422016-08-30 14:01:10 -07001419 av1_set_internal_size(ctx->cpi, (AOM_SCALING)mode->h_scaling_mode,
1420 (AOM_SCALING)mode->v_scaling_mode);
1421 return (res == 0) ? AOM_CODEC_OK : AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001422 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001423 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001424 }
1425}
1426
Yaowu Xuf883b422016-08-30 14:01:10 -07001427static aom_codec_err_t ctrl_set_tune_content(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001428 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001429 struct av1_extracfg extra_cfg = ctx->extra_cfg;
1430 extra_cfg.content = CAST(AV1E_SET_TUNE_CONTENT, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001431 return update_extra_cfg(ctx, &extra_cfg);
1432}
1433
Yaowu Xuf883b422016-08-30 14:01:10 -07001434static aom_codec_err_t ctrl_set_color_space(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001435 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001436 struct av1_extracfg extra_cfg = ctx->extra_cfg;
1437 extra_cfg.color_space = CAST(AV1E_SET_COLOR_SPACE, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001438 return update_extra_cfg(ctx, &extra_cfg);
1439}
1440
anorkin76fb1262017-03-22 15:12:12 -07001441#if CONFIG_COLORSPACE_HEADERS
1442static aom_codec_err_t ctrl_set_transfer_function(aom_codec_alg_priv_t *ctx,
1443 va_list args) {
1444 struct av1_extracfg extra_cfg = ctx->extra_cfg;
1445 extra_cfg.transfer_function = CAST(AV1E_SET_TRANSFER_FUNCTION, args);
1446 return update_extra_cfg(ctx, &extra_cfg);
1447}
1448
1449static aom_codec_err_t ctrl_set_chroma_sample_position(
1450 aom_codec_alg_priv_t *ctx, va_list args) {
1451 struct av1_extracfg extra_cfg = ctx->extra_cfg;
1452 extra_cfg.chroma_sample_position =
1453 CAST(AV1E_SET_CHROMA_SAMPLE_POSITION, args);
1454 return update_extra_cfg(ctx, &extra_cfg);
1455}
1456#endif
1457
Yaowu Xuf883b422016-08-30 14:01:10 -07001458static aom_codec_err_t ctrl_set_color_range(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001459 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001460 struct av1_extracfg extra_cfg = ctx->extra_cfg;
1461 extra_cfg.color_range = CAST(AV1E_SET_COLOR_RANGE, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001462 return update_extra_cfg(ctx, &extra_cfg);
1463}
1464
Yaowu Xuf883b422016-08-30 14:01:10 -07001465static aom_codec_err_t ctrl_set_render_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001467 struct av1_extracfg extra_cfg = ctx->extra_cfg;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001468 int *const render_size = va_arg(args, int *);
1469 extra_cfg.render_width = render_size[0];
1470 extra_cfg.render_height = render_size[1];
1471 return update_extra_cfg(ctx, &extra_cfg);
1472}
1473
Yaowu Xuf883b422016-08-30 14:01:10 -07001474static aom_codec_err_t ctrl_set_superblock_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001475 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001476 struct av1_extracfg extra_cfg = ctx->extra_cfg;
1477 extra_cfg.superblock_size = CAST(AV1E_SET_SUPERBLOCK_SIZE, args);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001478 return update_extra_cfg(ctx, &extra_cfg);
1479}
1480
Alex Converseeb780e72016-12-13 12:46:41 -08001481#if CONFIG_ANS && ANS_MAX_SYMBOLS
1482static aom_codec_err_t ctrl_set_ans_window_size_log2(aom_codec_alg_priv_t *ctx,
1483 va_list args) {
1484 struct av1_extracfg extra_cfg = ctx->extra_cfg;
1485 extra_cfg.ans_window_size_log2 = CAST(AV1E_SET_ANS_WINDOW_SIZE_LOG2, args);
1486 return update_extra_cfg(ctx, &extra_cfg);
1487}
1488#endif
1489
Yaowu Xuf883b422016-08-30 14:01:10 -07001490static aom_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
1491 { AOM_COPY_REFERENCE, ctrl_copy_reference },
1492 { AOME_USE_REFERENCE, ctrl_use_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001493
1494 // Setters
Yaowu Xuf883b422016-08-30 14:01:10 -07001495 { AOM_SET_REFERENCE, ctrl_set_reference },
1496 { AOM_SET_POSTPROC, ctrl_set_previewpp },
1497 { AOME_SET_ROI_MAP, ctrl_set_roi_map },
1498 { AOME_SET_ACTIVEMAP, ctrl_set_active_map },
1499 { AOME_SET_SCALEMODE, ctrl_set_scale_mode },
1500 { AOME_SET_CPUUSED, ctrl_set_cpuused },
1501 { AOME_SET_ENABLEAUTOALTREF, ctrl_set_enable_auto_alt_ref },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001502#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001503 { AOME_SET_ENABLEAUTOBWDREF, ctrl_set_enable_auto_bwd_ref },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001504#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07001505 { AOME_SET_SHARPNESS, ctrl_set_sharpness },
1506 { AOME_SET_STATIC_THRESHOLD, ctrl_set_static_thresh },
1507 { AV1E_SET_TILE_COLUMNS, ctrl_set_tile_columns },
1508 { AV1E_SET_TILE_ROWS, ctrl_set_tile_rows },
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08001509#if CONFIG_DEPENDENT_HORZTILES
1510 { AV1E_SET_TILE_DEPENDENT_ROWS, ctrl_set_tile_dependent_rows },
1511#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001512#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08001513 { AV1E_SET_TILE_LOOPFILTER, ctrl_set_tile_loopfilter },
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001514#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -07001515 { AOME_SET_ARNR_MAXFRAMES, ctrl_set_arnr_max_frames },
1516 { AOME_SET_ARNR_STRENGTH, ctrl_set_arnr_strength },
Yaowu Xuf883b422016-08-30 14:01:10 -07001517 { AOME_SET_TUNING, ctrl_set_tuning },
1518 { AOME_SET_CQ_LEVEL, ctrl_set_cq_level },
1519 { AOME_SET_MAX_INTRA_BITRATE_PCT, ctrl_set_rc_max_intra_bitrate_pct },
1520 { AV1E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
1521 { AV1E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
1522 { AV1E_SET_LOSSLESS, ctrl_set_lossless },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001523#if CONFIG_AOM_QM
Yaowu Xuf883b422016-08-30 14:01:10 -07001524 { AV1E_SET_ENABLE_QM, ctrl_set_enable_qm },
1525 { AV1E_SET_QM_MIN, ctrl_set_qm_min },
1526 { AV1E_SET_QM_MAX, ctrl_set_qm_max },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001527#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +00001528#if CONFIG_TILE_GROUPS
1529 { AV1E_SET_NUM_TG, ctrl_set_num_tg },
1530 { AV1E_SET_MTU, ctrl_set_mtu },
1531#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -08001532#if CONFIG_TEMPMV_SIGNALING
1533 { AV1E_SET_DISABLE_TEMPMV, ctrl_set_disable_tempmv },
1534#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001535 { AV1E_SET_FRAME_PARALLEL_DECODING, ctrl_set_frame_parallel_decoding_mode },
1536 { AV1E_SET_AQ_MODE, ctrl_set_aq_mode },
Fangwen Fu6160df22017-04-24 09:45:51 -07001537#if CONFIG_EXT_DELTA_Q
1538 { AV1E_SET_DELTAQ_MODE, ctrl_set_deltaq_mode },
1539#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001540 { AV1E_SET_FRAME_PERIODIC_BOOST, ctrl_set_frame_periodic_boost },
1541 { AV1E_SET_TUNE_CONTENT, ctrl_set_tune_content },
1542 { AV1E_SET_COLOR_SPACE, ctrl_set_color_space },
anorkin76fb1262017-03-22 15:12:12 -07001543#if CONFIG_COLORSPACE_HEADERS
1544 { AV1E_SET_TRANSFER_FUNCTION, ctrl_set_transfer_function },
1545 { AV1E_SET_CHROMA_SAMPLE_POSITION, ctrl_set_chroma_sample_position },
1546#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001547 { AV1E_SET_COLOR_RANGE, ctrl_set_color_range },
1548 { AV1E_SET_NOISE_SENSITIVITY, ctrl_set_noise_sensitivity },
1549 { AV1E_SET_MIN_GF_INTERVAL, ctrl_set_min_gf_interval },
1550 { AV1E_SET_MAX_GF_INTERVAL, ctrl_set_max_gf_interval },
1551 { AV1E_SET_RENDER_SIZE, ctrl_set_render_size },
1552 { AV1E_SET_SUPERBLOCK_SIZE, ctrl_set_superblock_size },
Alex Converseeb780e72016-12-13 12:46:41 -08001553#if CONFIG_ANS && ANS_MAX_SYMBOLS
1554 { AV1E_SET_ANS_WINDOW_SIZE_LOG2, ctrl_set_ans_window_size_log2 },
1555#endif
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08001556#if CONFIG_EXT_TILE
1557 { AV1E_SET_TILE_ENCODING_MODE, ctrl_set_tile_encoding_mode },
1558#endif // CONFIG_EXT_TILE
Yunqing Wangff4fa062017-04-21 10:56:08 -07001559 { AV1E_ENABLE_MOTION_VECTOR_UNIT_TEST, ctrl_enable_motion_vector_unit_test },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001560
1561 // Getters
Yaowu Xuf883b422016-08-30 14:01:10 -07001562 { AOME_GET_LAST_QUANTIZER, ctrl_get_quantizer },
1563 { AOME_GET_LAST_QUANTIZER_64, ctrl_get_quantizer64 },
1564 { AV1_GET_REFERENCE, ctrl_get_reference },
1565 { AV1E_GET_ACTIVEMAP, ctrl_get_active_map },
1566 { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001567
1568 { -1, NULL },
1569};
1570
Yaowu Xuf883b422016-08-30 14:01:10 -07001571static aom_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001572 { 0,
1573 {
1574 // NOLINT
1575 0, // g_usage
1576 8, // g_threads
1577 0, // g_profile
1578
1579 320, // g_width
1580 240, // g_height
Yaowu Xuf883b422016-08-30 14:01:10 -07001581 AOM_BITS_8, // g_bit_depth
Yaowu Xuc27fc142016-08-22 16:08:15 -07001582 8, // g_input_bit_depth
1583
1584 { 1, 30 }, // g_timebase
1585
1586 0, // g_error_resilient
1587
Yaowu Xuf883b422016-08-30 14:01:10 -07001588 AOM_RC_ONE_PASS, // g_pass
Yaowu Xuc27fc142016-08-22 16:08:15 -07001589
1590 25, // g_lag_in_frames
1591
1592 0, // rc_dropframe_thresh
1593 0, // rc_resize_allowed
1594 0, // rc_scaled_width
1595 0, // rc_scaled_height
1596 60, // rc_resize_down_thresold
1597 30, // rc_resize_up_thresold
1598
Yaowu Xuf883b422016-08-30 14:01:10 -07001599 AOM_VBR, // rc_end_usage
Yaowu Xuc27fc142016-08-22 16:08:15 -07001600 { NULL, 0 }, // rc_twopass_stats_in
1601 { NULL, 0 }, // rc_firstpass_mb_stats_in
1602 256, // rc_target_bandwidth
1603 0, // rc_min_quantizer
1604 63, // rc_max_quantizer
1605 25, // rc_undershoot_pct
1606 25, // rc_overshoot_pct
1607
1608 6000, // rc_max_buffer_size
1609 4000, // rc_buffer_initial_size
1610 5000, // rc_buffer_optimal_size
1611
1612 50, // rc_two_pass_vbrbias
1613 0, // rc_two_pass_vbrmin_section
1614 2000, // rc_two_pass_vbrmax_section
1615
1616 // keyframing settings (kf)
Yaowu Xuf883b422016-08-30 14:01:10 -07001617 AOM_KF_AUTO, // g_kfmode
Yaowu Xuc27fc142016-08-22 16:08:15 -07001618 0, // kf_min_dist
1619 9999, // kf_max_dist
1620 } },
1621};
1622
1623#ifndef VERSION_STRING
1624#define VERSION_STRING
1625#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001626CODEC_INTERFACE(aom_codec_av1_cx) = {
1627 "AOMedia Project AV1 Encoder" VERSION_STRING,
1628 AOM_CODEC_INTERNAL_ABI_VERSION,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001629#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001630 AOM_CODEC_CAP_HIGHBITDEPTH |
Yaowu Xuc27fc142016-08-22 16:08:15 -07001631#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001632 AOM_CODEC_CAP_ENCODER | AOM_CODEC_CAP_PSNR, // aom_codec_caps_t
1633 encoder_init, // aom_codec_init_fn_t
1634 encoder_destroy, // aom_codec_destroy_fn_t
1635 encoder_ctrl_maps, // aom_codec_ctrl_fn_map_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001636 {
1637 // NOLINT
Yaowu Xuf883b422016-08-30 14:01:10 -07001638 NULL, // aom_codec_peek_si_fn_t
1639 NULL, // aom_codec_get_si_fn_t
1640 NULL, // aom_codec_decode_fn_t
1641 NULL, // aom_codec_frame_get_fn_t
1642 NULL // aom_codec_set_fb_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001643 },
1644 {
1645 // NOLINT
1646 1, // 1 cfg map
Yaowu Xuf883b422016-08-30 14:01:10 -07001647 encoder_usage_cfg_map, // aom_codec_enc_cfg_map_t
1648 encoder_encode, // aom_codec_encode_fn_t
1649 encoder_get_cxdata, // aom_codec_get_cx_data_fn_t
1650 encoder_set_config, // aom_codec_enc_config_set_fn_t
1651 NULL, // aom_codec_get_global_headers_fn_t
1652 encoder_get_preview, // aom_codec_get_preview_frame_fn_t
1653 NULL // aom_codec_enc_mr_get_mem_loc_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001654 }
1655};