1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <limits.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "./vpx_config.h"
17 #include "./vp8_rtcd.h"
18 #include "./vpx_dsp_rtcd.h"
19 #include "./vpx_scale_rtcd.h"
20 #include "vpx/vpx_encoder.h"
21 #include "vpx/internal/vpx_codec_internal.h"
22 #include "vpx_version.h"
23 #include "vpx_mem/vpx_mem.h"
24 #include "vpx_ports/static_assert.h"
25 #include "vpx_ports/system_state.h"
26 #include "vpx_util/vpx_timestamp.h"
27 #if CONFIG_MULTITHREAD
28 #include "vp8/encoder/ethreading.h"
29 #endif
30 #include "vp8/encoder/onyx_int.h"
31 #include "vpx/vp8cx.h"
32 #include "vp8/encoder/firstpass.h"
33 #include "vp8/common/onyx.h"
34 #include "vp8/common/common.h"
35
36 struct vp8_extracfg {
37 struct vpx_codec_pkt_list *pkt_list;
38 int cpu_used; /** available cpu percentage in 1/16*/
39 /** if encoder decides to uses alternate reference frame */
40 unsigned int enable_auto_alt_ref;
41 unsigned int noise_sensitivity;
42 unsigned int Sharpness;
43 unsigned int static_thresh;
44 unsigned int token_partitions;
45 unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
46 unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
47 unsigned int arnr_type; /* alt_ref filter type */
48 vp8e_tuning tuning;
49 unsigned int cq_level; /* constrained quality level */
50 unsigned int rc_max_intra_bitrate_pct;
51 unsigned int gf_cbr_boost_pct;
52 unsigned int screen_content_mode;
53 };
54
55 static struct vp8_extracfg default_extracfg = {
56 NULL,
57 #if !(CONFIG_REALTIME_ONLY)
58 0, /* cpu_used */
59 #else
60 4, /* cpu_used */
61 #endif
62 0, /* enable_auto_alt_ref */
63 0, /* noise_sensitivity */
64 0, /* Sharpness */
65 0, /* static_thresh */
66 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
67 VP8_EIGHT_TOKENPARTITION,
68 #else
69 VP8_ONE_TOKENPARTITION, /* token_partitions */
70 #endif
71 0, /* arnr_max_frames */
72 3, /* arnr_strength */
73 3, /* arnr_type*/
74 0, /* tuning*/
75 10, /* cq_level */
76 0, /* rc_max_intra_bitrate_pct */
77 0, /* gf_cbr_boost_pct */
78 0, /* screen_content_mode */
79 };
80
81 struct vpx_codec_alg_priv {
82 vpx_codec_priv_t base;
83 vpx_codec_enc_cfg_t cfg;
84 struct vp8_extracfg vp8_cfg;
85 vpx_rational64_t timestamp_ratio;
86 vpx_codec_pts_t pts_offset;
87 unsigned char pts_offset_initialized;
88 VP8_CONFIG oxcf;
89 struct VP8_COMP *cpi;
90 unsigned char *cx_data;
91 unsigned int cx_data_sz;
92 vpx_image_t preview_img;
93 unsigned int next_frame_flag;
94 vp8_postproc_cfg_t preview_ppcfg;
95 /* pkt_list size depends on the maximum number of lagged frames allowed. */
96 vpx_codec_pkt_list_decl(64) pkt_list;
97 unsigned int fixed_kf_cntr;
98 vpx_enc_frame_flags_t control_frame_flags;
99 };
100
101 // Called by vp8e_set_config() and vp8e_encode() only. Must not be called
102 // by vp8e_init() because the `error` paramerer (cpi->common.error) will be
103 // destroyed by vpx_codec_enc_init_ver() after vp8e_init() returns an error.
104 // See the "IMPORTANT" comment in vpx_codec_enc_init_ver().
update_error_state(vpx_codec_alg_priv_t * ctx,const struct vpx_internal_error_info * error)105 static vpx_codec_err_t update_error_state(
106 vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
107 const vpx_codec_err_t res = error->error_code;
108
109 if (res != VPX_CODEC_OK)
110 ctx->base.err_detail = error->has_detail ? error->detail : NULL;
111
112 return res;
113 }
114
115 #undef ERROR
116 #define ERROR(str) \
117 do { \
118 ctx->base.err_detail = str; \
119 return VPX_CODEC_INVALID_PARAM; \
120 } while (0)
121
122 #define RANGE_CHECK(p, memb, lo, hi) \
123 do { \
124 if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
125 ERROR(#memb " out of range [" #lo ".." #hi "]"); \
126 } while (0)
127
128 #define RANGE_CHECK_HI(p, memb, hi) \
129 do { \
130 if (!((p)->memb <= (hi))) ERROR(#memb " out of range [.." #hi "]"); \
131 } while (0)
132
133 #define RANGE_CHECK_LO(p, memb, lo) \
134 do { \
135 if (!((p)->memb >= (lo))) ERROR(#memb " out of range [" #lo "..]"); \
136 } while (0)
137
138 #define RANGE_CHECK_BOOL(p, memb) \
139 do { \
140 if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean"); \
141 } while (0)
142
validate_config(vpx_codec_alg_priv_t * ctx,const vpx_codec_enc_cfg_t * cfg,const struct vp8_extracfg * vp8_cfg,int finalize)143 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
144 const vpx_codec_enc_cfg_t *cfg,
145 const struct vp8_extracfg *vp8_cfg,
146 int finalize) {
147 RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
148 RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
149 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
150 RANGE_CHECK(cfg, g_timebase.num, 1, 1000000000);
151 RANGE_CHECK_HI(cfg, g_profile, 3);
152 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
153 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
154 RANGE_CHECK_HI(cfg, g_threads, 64);
155 #if CONFIG_REALTIME_ONLY
156 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
157 #elif CONFIG_MULTI_RES_ENCODING
158 if (ctx->base.enc.total_encoders > 1) RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
159 #else
160 RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
161 #endif
162 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
163 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 100);
164 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 100);
165 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
166 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
167
168 /* TODO: add spatial re-sampling support and frame dropping in
169 * multi-res-encoder.*/
170 #if CONFIG_MULTI_RES_ENCODING
171 if (ctx->base.enc.total_encoders > 1)
172 RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
173 #else
174 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
175 #endif
176 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
177 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
178 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
179
180 #if CONFIG_REALTIME_ONLY
181 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
182 #elif CONFIG_MULTI_RES_ENCODING
183 if (ctx->base.enc.total_encoders > 1)
184 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
185 #else
186 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
187 #endif
188
189 /* VP8 does not support a lower bound on the keyframe interval in
190 * automatic keyframe placement mode.
191 */
192 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist &&
193 cfg->kf_min_dist > 0)
194 ERROR(
195 "kf_min_dist not supported in auto mode, use 0 "
196 "or kf_max_dist instead.");
197
198 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
199 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
200
201 #if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
202 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
203 #else
204 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
205 #endif
206
207 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
208 VP8_EIGHT_TOKENPARTITION);
209 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
210 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
211 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
212 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
213 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
214 RANGE_CHECK_HI(vp8_cfg, screen_content_mode, 2);
215 if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
216 RANGE_CHECK(vp8_cfg, cq_level, cfg->rc_min_quantizer,
217 cfg->rc_max_quantizer);
218
219 #if !(CONFIG_REALTIME_ONLY)
220 if (cfg->g_pass == VPX_RC_LAST_PASS) {
221 size_t packet_sz = sizeof(FIRSTPASS_STATS);
222 int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
223 FIRSTPASS_STATS *stats;
224
225 if (!cfg->rc_twopass_stats_in.buf)
226 ERROR("rc_twopass_stats_in.buf not set.");
227
228 if (cfg->rc_twopass_stats_in.sz % packet_sz)
229 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
230
231 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
232 ERROR("rc_twopass_stats_in requires at least two packets.");
233
234 stats = (void *)((char *)cfg->rc_twopass_stats_in.buf +
235 (n_packets - 1) * packet_sz);
236
237 if ((int)(stats->count + 0.5) != n_packets - 1)
238 ERROR("rc_twopass_stats_in missing EOS stats packet");
239 }
240 #endif
241
242 RANGE_CHECK(cfg, ts_number_layers, 1, 5);
243
244 if (cfg->ts_number_layers > 1) {
245 unsigned int i;
246 RANGE_CHECK_HI(cfg, ts_periodicity, 16);
247
248 for (i = 1; i < cfg->ts_number_layers; ++i) {
249 if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i - 1] &&
250 cfg->rc_target_bitrate > 0)
251 ERROR("ts_target_bitrate entries are not strictly increasing");
252 }
253
254 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
255 for (i = cfg->ts_number_layers - 2; i > 0; i--) {
256 if (cfg->ts_rate_decimator[i - 1] != 2 * cfg->ts_rate_decimator[i])
257 ERROR("ts_rate_decimator factors are not powers of 2");
258 }
259
260 RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers - 1);
261 }
262
263 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
264 if (cfg->g_threads > (1 << vp8_cfg->token_partitions))
265 ERROR("g_threads cannot be bigger than number of token partitions");
266 #endif
267
268 // The range below shall be further tuned.
269 RANGE_CHECK(cfg, use_vizier_rc_params, 0, 1);
270 RANGE_CHECK(cfg, active_wq_factor.den, 1, 1000);
271 RANGE_CHECK(cfg, err_per_mb_factor.den, 1, 1000);
272 RANGE_CHECK(cfg, sr_default_decay_limit.den, 1, 1000);
273 RANGE_CHECK(cfg, sr_diff_factor.den, 1, 1000);
274 RANGE_CHECK(cfg, kf_err_per_mb_factor.den, 1, 1000);
275 RANGE_CHECK(cfg, kf_frame_min_boost_factor.den, 1, 1000);
276 RANGE_CHECK(cfg, kf_frame_max_boost_subs_factor.den, 1, 1000);
277 RANGE_CHECK(cfg, kf_max_total_boost_factor.den, 1, 1000);
278 RANGE_CHECK(cfg, gf_max_total_boost_factor.den, 1, 1000);
279 RANGE_CHECK(cfg, gf_frame_max_boost_factor.den, 1, 1000);
280 RANGE_CHECK(cfg, zm_factor.den, 1, 1000);
281 RANGE_CHECK(cfg, rd_mult_inter_qp_fac.den, 1, 1000);
282 RANGE_CHECK(cfg, rd_mult_arf_qp_fac.den, 1, 1000);
283 RANGE_CHECK(cfg, rd_mult_key_qp_fac.den, 1, 1000);
284
285 return VPX_CODEC_OK;
286 }
287
validate_img(vpx_codec_alg_priv_t * ctx,const vpx_image_t * img)288 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
289 const vpx_image_t *img) {
290 switch (img->fmt) {
291 case VPX_IMG_FMT_YV12:
292 case VPX_IMG_FMT_I420:
293 case VPX_IMG_FMT_NV12: break;
294 default:
295 ERROR(
296 "Invalid image format. Only YV12, I420 and NV12 images are "
297 "supported");
298 }
299
300 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
301 ERROR("Image size must match encoder init configuration size");
302
303 return VPX_CODEC_OK;
304 }
305
set_vp8e_config(VP8_CONFIG * oxcf,vpx_codec_enc_cfg_t cfg,struct vp8_extracfg vp8_cfg,vpx_codec_priv_enc_mr_cfg_t * mr_cfg)306 static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
307 vpx_codec_enc_cfg_t cfg,
308 struct vp8_extracfg vp8_cfg,
309 vpx_codec_priv_enc_mr_cfg_t *mr_cfg) {
310 oxcf->multi_threaded = cfg.g_threads;
311 oxcf->Version = cfg.g_profile;
312
313 oxcf->Width = cfg.g_w;
314 oxcf->Height = cfg.g_h;
315 oxcf->timebase = cfg.g_timebase;
316
317 oxcf->error_resilient_mode = cfg.g_error_resilient;
318
319 switch (cfg.g_pass) {
320 case VPX_RC_ONE_PASS: oxcf->Mode = MODE_BESTQUALITY; break;
321 case VPX_RC_FIRST_PASS: oxcf->Mode = MODE_FIRSTPASS; break;
322 case VPX_RC_LAST_PASS: oxcf->Mode = MODE_SECONDPASS_BEST; break;
323 }
324
325 if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS) {
326 oxcf->allow_lag = 0;
327 oxcf->lag_in_frames = 0;
328 } else {
329 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
330 oxcf->lag_in_frames = cfg.g_lag_in_frames;
331 }
332
333 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
334 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
335
336 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
337 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
338 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
339
340 if (cfg.rc_end_usage == VPX_VBR) {
341 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
342 } else if (cfg.rc_end_usage == VPX_CBR) {
343 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
344 } else if (cfg.rc_end_usage == VPX_CQ) {
345 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
346 } else if (cfg.rc_end_usage == VPX_Q) {
347 oxcf->end_usage = USAGE_CONSTANT_QUALITY;
348 }
349
350 // Cap the target rate to 1000 Mbps to avoid some integer overflows in
351 // target bandwidth calculations.
352 oxcf->target_bandwidth = VPXMIN(cfg.rc_target_bitrate, 1000000);
353 oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
354 oxcf->gf_cbr_boost_pct = vp8_cfg.gf_cbr_boost_pct;
355
356 oxcf->best_allowed_q = cfg.rc_min_quantizer;
357 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
358 oxcf->cq_level = vp8_cfg.cq_level;
359 oxcf->fixed_q = -1;
360
361 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
362 oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
363
364 oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
365 oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
366 oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
367
368 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
369 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
370 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
371
372 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
373 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
374 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
375
376 oxcf->auto_key =
377 cfg.kf_mode == VPX_KF_AUTO && cfg.kf_min_dist != cfg.kf_max_dist;
378 oxcf->key_freq = cfg.kf_max_dist;
379
380 oxcf->number_of_layers = cfg.ts_number_layers;
381 oxcf->periodicity = cfg.ts_periodicity;
382
383 if (oxcf->number_of_layers > 1) {
384 memcpy(oxcf->target_bitrate, cfg.ts_target_bitrate,
385 sizeof(cfg.ts_target_bitrate));
386 memcpy(oxcf->rate_decimator, cfg.ts_rate_decimator,
387 sizeof(cfg.ts_rate_decimator));
388 memcpy(oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
389 }
390
391 #if CONFIG_MULTI_RES_ENCODING
392 /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
393 * are both memset to 0, which ensures the correct logic under this
394 * situation.
395 */
396 if (mr_cfg) {
397 oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
398 oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
399 oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
400 oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
401 oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
402 }
403 #else
404 (void)mr_cfg;
405 #endif
406
407 oxcf->cpu_used = vp8_cfg.cpu_used;
408 if (cfg.g_pass == VPX_RC_FIRST_PASS) {
409 oxcf->cpu_used = VPXMAX(4, oxcf->cpu_used);
410 }
411 oxcf->encode_breakout = vp8_cfg.static_thresh;
412 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
413 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
414 oxcf->Sharpness = vp8_cfg.Sharpness;
415 oxcf->token_partitions = vp8_cfg.token_partitions;
416
417 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
418 oxcf->output_pkt_list = vp8_cfg.pkt_list;
419
420 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
421 oxcf->arnr_strength = vp8_cfg.arnr_strength;
422 oxcf->arnr_type = vp8_cfg.arnr_type;
423
424 oxcf->tuning = vp8_cfg.tuning;
425
426 oxcf->screen_content_mode = vp8_cfg.screen_content_mode;
427
428 /*
429 printf("Current VP8 Settings: \n");
430 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
431 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
432 printf("Sharpness: %d\n", oxcf->Sharpness);
433 printf("cpu_used: %d\n", oxcf->cpu_used);
434 printf("Mode: %d\n", oxcf->Mode);
435 printf("auto_key: %d\n", oxcf->auto_key);
436 printf("key_freq: %d\n", oxcf->key_freq);
437 printf("end_usage: %d\n", oxcf->end_usage);
438 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
439 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
440 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
441 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
442 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
443 printf("fixed_q: %d\n", oxcf->fixed_q);
444 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
445 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
446 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
447 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
448 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
449 printf("allow_df: %d\n", oxcf->allow_df);
450 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
451 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
452 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
453 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
454 printf("allow_lag: %d\n", oxcf->allow_lag);
455 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
456 printf("play_alternate: %d\n", oxcf->play_alternate);
457 printf("Version: %d\n", oxcf->Version);
458 printf("multi_threaded: %d\n", oxcf->multi_threaded);
459 printf("encode_breakout: %d\n", oxcf->encode_breakout);
460 */
461 return VPX_CODEC_OK;
462 }
463
vp8e_set_config(vpx_codec_alg_priv_t * ctx,const vpx_codec_enc_cfg_t * cfg)464 static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
465 const vpx_codec_enc_cfg_t *cfg) {
466 vpx_codec_err_t res;
467
468 if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
469 if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
470 ERROR("Cannot change width or height after initialization");
471 if ((ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
472 (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
473 ERROR("Cannot increase width or height larger than their initial values");
474 }
475
476 /* Prevent increasing lag_in_frames. This check is stricter than it needs
477 * to be -- the limit is not increasing past the first lag_in_frames
478 * value, but we don't track the initial config, only the last successful
479 * config.
480 */
481 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
482 ERROR("Cannot increase lag_in_frames");
483
484 res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
485 if (res != VPX_CODEC_OK) return res;
486
487 if (setjmp(ctx->cpi->common.error.jmp)) {
488 const vpx_codec_err_t codec_err =
489 update_error_state(ctx, &ctx->cpi->common.error);
490 ctx->cpi->common.error.setjmp = 0;
491 vpx_clear_system_state();
492 assert(codec_err != VPX_CODEC_OK);
493 return codec_err;
494 }
495
496 ctx->cpi->common.error.setjmp = 1;
497 ctx->cfg = *cfg;
498 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
499 vp8_change_config(ctx->cpi, &ctx->oxcf);
500 #if CONFIG_MULTITHREAD
501 if (vp8cx_create_encoder_threads(ctx->cpi)) {
502 ctx->cpi->common.error.setjmp = 0;
503 return VPX_CODEC_ERROR;
504 }
505 #endif
506 ctx->cpi->common.error.setjmp = 0;
507 return VPX_CODEC_OK;
508 }
509
get_quantizer(vpx_codec_alg_priv_t * ctx,va_list args)510 static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args) {
511 int *const arg = va_arg(args, int *);
512 if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
513 *arg = vp8_get_quantizer(ctx->cpi);
514 return VPX_CODEC_OK;
515 }
516
get_quantizer64(vpx_codec_alg_priv_t * ctx,va_list args)517 static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx,
518 va_list args) {
519 int *const arg = va_arg(args, int *);
520 if (arg == NULL) return VPX_CODEC_INVALID_PARAM;
521 *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi));
522 return VPX_CODEC_OK;
523 }
524
update_extracfg(vpx_codec_alg_priv_t * ctx,const struct vp8_extracfg * extra_cfg)525 static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx,
526 const struct vp8_extracfg *extra_cfg) {
527 const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0);
528 if (res == VPX_CODEC_OK) {
529 ctx->vp8_cfg = *extra_cfg;
530 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
531 vp8_change_config(ctx->cpi, &ctx->oxcf);
532 }
533 return res;
534 }
535
set_cpu_used(vpx_codec_alg_priv_t * ctx,va_list args)536 static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args) {
537 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
538 extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
539 // Use fastest speed setting (speed 16 or -16) if it's set beyond the range.
540 extra_cfg.cpu_used = VPXMIN(16, extra_cfg.cpu_used);
541 extra_cfg.cpu_used = VPXMAX(-16, extra_cfg.cpu_used);
542 return update_extracfg(ctx, &extra_cfg);
543 }
544
set_enable_auto_alt_ref(vpx_codec_alg_priv_t * ctx,va_list args)545 static vpx_codec_err_t set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
546 va_list args) {
547 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
548 extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
549 return update_extracfg(ctx, &extra_cfg);
550 }
551
set_noise_sensitivity(vpx_codec_alg_priv_t * ctx,va_list args)552 static vpx_codec_err_t set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
553 va_list args) {
554 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
555 extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args);
556 return update_extracfg(ctx, &extra_cfg);
557 }
558
set_sharpness(vpx_codec_alg_priv_t * ctx,va_list args)559 static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args) {
560 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
561 extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, args);
562 return update_extracfg(ctx, &extra_cfg);
563 }
564
set_static_thresh(vpx_codec_alg_priv_t * ctx,va_list args)565 static vpx_codec_err_t set_static_thresh(vpx_codec_alg_priv_t *ctx,
566 va_list args) {
567 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
568 extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
569 return update_extracfg(ctx, &extra_cfg);
570 }
571
set_token_partitions(vpx_codec_alg_priv_t * ctx,va_list args)572 static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx,
573 va_list args) {
574 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
575 extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args);
576 return update_extracfg(ctx, &extra_cfg);
577 }
578
set_arnr_max_frames(vpx_codec_alg_priv_t * ctx,va_list args)579 static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
580 va_list args) {
581 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
582 extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
583 return update_extracfg(ctx, &extra_cfg);
584 }
585
set_arnr_strength(vpx_codec_alg_priv_t * ctx,va_list args)586 static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx,
587 va_list args) {
588 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
589 extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
590 return update_extracfg(ctx, &extra_cfg);
591 }
592
set_arnr_type(vpx_codec_alg_priv_t * ctx,va_list args)593 static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args) {
594 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
595 extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args);
596 return update_extracfg(ctx, &extra_cfg);
597 }
598
set_tuning(vpx_codec_alg_priv_t * ctx,va_list args)599 static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args) {
600 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
601 extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
602 return update_extracfg(ctx, &extra_cfg);
603 }
604
set_cq_level(vpx_codec_alg_priv_t * ctx,va_list args)605 static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args) {
606 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
607 extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
608 return update_extracfg(ctx, &extra_cfg);
609 }
610
set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t * ctx,va_list args)611 static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx,
612 va_list args) {
613 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
614 extra_cfg.rc_max_intra_bitrate_pct =
615 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
616 return update_extracfg(ctx, &extra_cfg);
617 }
618
ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t * ctx,va_list args)619 static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(vpx_codec_alg_priv_t *ctx,
620 va_list args) {
621 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
622 extra_cfg.gf_cbr_boost_pct = CAST(VP8E_SET_GF_CBR_BOOST_PCT, args);
623 return update_extracfg(ctx, &extra_cfg);
624 }
625
set_screen_content_mode(vpx_codec_alg_priv_t * ctx,va_list args)626 static vpx_codec_err_t set_screen_content_mode(vpx_codec_alg_priv_t *ctx,
627 va_list args) {
628 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
629 extra_cfg.screen_content_mode = CAST(VP8E_SET_SCREEN_CONTENT_MODE, args);
630 return update_extracfg(ctx, &extra_cfg);
631 }
632
ctrl_set_rtc_external_ratectrl(vpx_codec_alg_priv_t * ctx,va_list args)633 static vpx_codec_err_t ctrl_set_rtc_external_ratectrl(vpx_codec_alg_priv_t *ctx,
634 va_list args) {
635 VP8_COMP *cpi = ctx->cpi;
636 const unsigned int data = CAST(VP8E_SET_RTC_EXTERNAL_RATECTRL, args);
637 if (data) {
638 cpi->cyclic_refresh_mode_enabled = 0;
639 cpi->rt_always_update_correction_factor = 1;
640 cpi->rt_drop_recode_on_overshoot = 0;
641 }
642 return VPX_CODEC_OK;
643 }
644
vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t * cfg,void ** mem_loc)645 static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
646 void **mem_loc) {
647 vpx_codec_err_t res = VPX_CODEC_OK;
648
649 #if CONFIG_MULTI_RES_ENCODING
650 LOWER_RES_FRAME_INFO *shared_mem_loc;
651 int mb_rows = ((cfg->g_w + 15) >> 4);
652 int mb_cols = ((cfg->g_h + 15) >> 4);
653
654 shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
655 if (!shared_mem_loc) {
656 return VPX_CODEC_MEM_ERROR;
657 }
658
659 shared_mem_loc->mb_info =
660 calloc(mb_rows * mb_cols, sizeof(LOWER_RES_MB_INFO));
661 if (!(shared_mem_loc->mb_info)) {
662 free(shared_mem_loc);
663 res = VPX_CODEC_MEM_ERROR;
664 } else {
665 *mem_loc = (void *)shared_mem_loc;
666 res = VPX_CODEC_OK;
667 }
668 #else
669 (void)cfg;
670 (void)mem_loc;
671 #endif
672 return res;
673 }
674
vp8e_init(vpx_codec_ctx_t * ctx,vpx_codec_priv_enc_mr_cfg_t * mr_cfg)675 static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
676 vpx_codec_priv_enc_mr_cfg_t *mr_cfg) {
677 vpx_codec_err_t res = VPX_CODEC_OK;
678
679 vp8_rtcd();
680 vpx_dsp_rtcd();
681 vpx_scale_rtcd();
682
683 if (!ctx->priv) {
684 struct vpx_codec_alg_priv *priv =
685 (struct vpx_codec_alg_priv *)vpx_calloc(1, sizeof(*priv));
686
687 if (!priv) {
688 return VPX_CODEC_MEM_ERROR;
689 }
690
691 ctx->priv = (vpx_codec_priv_t *)priv;
692 ctx->priv->init_flags = ctx->init_flags;
693
694 if (ctx->config.enc) {
695 /* Update the reference to the config structure to an
696 * internal copy.
697 */
698 priv->cfg = *ctx->config.enc;
699 ctx->config.enc = &priv->cfg;
700 }
701
702 priv->vp8_cfg = default_extracfg;
703 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
704
705 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
706
707 if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
708
709 priv->cx_data = malloc(priv->cx_data_sz);
710
711 if (!priv->cx_data) {
712 priv->cx_data_sz = 0;
713 return VPX_CODEC_MEM_ERROR;
714 }
715
716 if (mr_cfg) {
717 ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
718 } else {
719 ctx->priv->enc.total_encoders = 1;
720 }
721
722 vp8_initialize_enc();
723
724 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
725
726 if (!res) {
727 priv->pts_offset_initialized = 0;
728 priv->timestamp_ratio.den = priv->cfg.g_timebase.den;
729 priv->timestamp_ratio.num = (int64_t)priv->cfg.g_timebase.num;
730 priv->timestamp_ratio.num *= TICKS_PER_SEC;
731 reduce_ratio(&priv->timestamp_ratio);
732
733 set_vp8e_config(&priv->oxcf, priv->cfg, priv->vp8_cfg, mr_cfg);
734 priv->cpi = vp8_create_compressor(&priv->oxcf);
735 if (!priv->cpi) res = VPX_CODEC_MEM_ERROR;
736 }
737 }
738
739 return res;
740 }
741
vp8e_destroy(vpx_codec_alg_priv_t * ctx)742 static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx) {
743 #if CONFIG_MULTI_RES_ENCODING
744 /* Free multi-encoder shared memory */
745 if (ctx->oxcf.mr_total_resolutions > 0 &&
746 (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions - 1)) {
747 LOWER_RES_FRAME_INFO *shared_mem_loc =
748 (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
749 free(shared_mem_loc->mb_info);
750 free(ctx->oxcf.mr_low_res_mode_info);
751 }
752 #endif
753
754 free(ctx->cx_data);
755 vp8_remove_compressor(&ctx->cpi);
756 vpx_free(ctx);
757 return VPX_CODEC_OK;
758 }
759
image2yuvconfig(const vpx_image_t * img,YV12_BUFFER_CONFIG * yv12)760 static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
761 YV12_BUFFER_CONFIG *yv12) {
762 const int y_w = img->d_w;
763 const int y_h = img->d_h;
764 const int uv_w = (img->d_w + 1) / 2;
765 const int uv_h = (img->d_h + 1) / 2;
766 vpx_codec_err_t res = VPX_CODEC_OK;
767 yv12->y_buffer = img->planes[VPX_PLANE_Y];
768 yv12->u_buffer = img->planes[VPX_PLANE_U];
769 yv12->v_buffer = img->planes[VPX_PLANE_V];
770
771 yv12->y_crop_width = y_w;
772 yv12->y_crop_height = y_h;
773 yv12->y_width = y_w;
774 yv12->y_height = y_h;
775 yv12->uv_crop_width = uv_w;
776 yv12->uv_crop_height = uv_h;
777 yv12->uv_width = uv_w;
778 yv12->uv_height = uv_h;
779
780 yv12->y_stride = img->stride[VPX_PLANE_Y];
781 yv12->uv_stride = img->stride[VPX_PLANE_U];
782
783 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
784 return res;
785 }
786
pick_quickcompress_mode(vpx_codec_alg_priv_t * ctx,unsigned long duration,vpx_enc_deadline_t deadline)787 static vpx_codec_err_t pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
788 unsigned long duration,
789 vpx_enc_deadline_t deadline) {
790 int new_qc;
791
792 #if !(CONFIG_REALTIME_ONLY)
793 /* Use best quality mode if no deadline is given. */
794 new_qc = MODE_BESTQUALITY;
795
796 if (deadline) {
797 /* Convert duration parameter from stream timebase to microseconds */
798 VPX_STATIC_ASSERT(TICKS_PER_SEC > 1000000 &&
799 (TICKS_PER_SEC % 1000000) == 0);
800
801 if (duration > UINT64_MAX / (uint64_t)ctx->timestamp_ratio.num) {
802 ERROR("duration is too big");
803 }
804 uint64_t duration_us =
805 duration * (uint64_t)ctx->timestamp_ratio.num /
806 ((uint64_t)ctx->timestamp_ratio.den * (TICKS_PER_SEC / 1000000));
807
808 /* If the deadline is more that the duration this frame is to be shown,
809 * use good quality mode. Otherwise use realtime mode.
810 */
811 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
812 }
813
814 #else
815 (void)duration;
816 new_qc = MODE_REALTIME;
817 #endif
818
819 if (deadline == VPX_DL_REALTIME) {
820 new_qc = MODE_REALTIME;
821 } else if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS) {
822 new_qc = MODE_FIRSTPASS;
823 } else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS) {
824 new_qc =
825 (new_qc == MODE_BESTQUALITY) ? MODE_SECONDPASS_BEST : MODE_SECONDPASS;
826 }
827
828 if (ctx->oxcf.Mode != new_qc) {
829 ctx->oxcf.Mode = new_qc;
830 vp8_change_config(ctx->cpi, &ctx->oxcf);
831 }
832 return VPX_CODEC_OK;
833 }
834
set_reference_and_update(vpx_codec_alg_priv_t * ctx,vpx_enc_frame_flags_t flags)835 static vpx_codec_err_t set_reference_and_update(vpx_codec_alg_priv_t *ctx,
836 vpx_enc_frame_flags_t flags) {
837 /* Handle Flags */
838 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
839 ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
840 ctx->base.err_detail = "Conflicting flags.";
841 return VPX_CODEC_INVALID_PARAM;
842 }
843
844 if (flags &
845 (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
846 int ref = 7;
847
848 if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP8_LAST_FRAME;
849
850 if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP8_GOLD_FRAME;
851
852 if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP8_ALTR_FRAME;
853
854 vp8_use_as_reference(ctx->cpi, ref);
855 }
856
857 if (flags &
858 (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
859 VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
860 int upd = 7;
861
862 if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP8_LAST_FRAME;
863
864 if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP8_GOLD_FRAME;
865
866 if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP8_ALTR_FRAME;
867
868 vp8_update_reference(ctx->cpi, upd);
869 }
870
871 if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
872 vp8_update_entropy(ctx->cpi, 0);
873 }
874
875 return VPX_CODEC_OK;
876 }
877
vp8e_encode(vpx_codec_alg_priv_t * ctx,const vpx_image_t * img,vpx_codec_pts_t pts,unsigned long duration,vpx_enc_frame_flags_t enc_flags,vpx_enc_deadline_t deadline)878 static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
879 const vpx_image_t *img, vpx_codec_pts_t pts,
880 unsigned long duration,
881 vpx_enc_frame_flags_t enc_flags,
882 vpx_enc_deadline_t deadline) {
883 volatile vpx_codec_err_t res = VPX_CODEC_OK;
884 // Make a copy as volatile to avoid -Wclobbered with longjmp.
885 volatile vpx_enc_frame_flags_t flags = enc_flags;
886 volatile vpx_codec_pts_t pts_val = pts;
887
888 if (!ctx->cfg.rc_target_bitrate) {
889 #if CONFIG_MULTI_RES_ENCODING
890 if (!ctx->cpi) return VPX_CODEC_ERROR;
891 if (ctx->cpi->oxcf.mr_total_resolutions > 1) {
892 LOWER_RES_FRAME_INFO *low_res_frame_info =
893 (LOWER_RES_FRAME_INFO *)ctx->cpi->oxcf.mr_low_res_mode_info;
894 if (!low_res_frame_info) return VPX_CODEC_ERROR;
895 low_res_frame_info->skip_encoding_prev_stream = 1;
896 if (ctx->cpi->oxcf.mr_encoder_id == 0)
897 low_res_frame_info->skip_encoding_base_stream = 1;
898 }
899 #endif
900 return res;
901 }
902
903 if (img) res = validate_img(ctx, img);
904
905 if (!res) res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
906
907 if (!res) res = pick_quickcompress_mode(ctx, duration, deadline);
908 vpx_codec_pkt_list_init(&ctx->pkt_list);
909
910 // If no flags are set in the encode call, then use the frame flags as
911 // defined via the control function: vp8e_set_frame_flags.
912 if (!flags) {
913 flags = ctx->control_frame_flags;
914 }
915 ctx->control_frame_flags = 0;
916
917 if (!res) res = set_reference_and_update(ctx, flags);
918
919 /* Handle fixed keyframe intervals */
920 if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
921 ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
922 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
923 flags |= VPX_EFLAG_FORCE_KF;
924 ctx->fixed_kf_cntr = 1;
925 }
926 }
927
928 /* Initialize the encoder instance on the first frame*/
929 if (!res && ctx->cpi) {
930 unsigned int lib_flags;
931 int64_t dst_time_stamp, dst_end_time_stamp;
932 size_t size, cx_data_sz;
933 unsigned char *cx_data;
934 unsigned char *cx_data_end;
935 int comp_data_state = 0;
936
937 if (setjmp(ctx->cpi->common.error.jmp)) {
938 ctx->cpi->common.error.setjmp = 0;
939 res = update_error_state(ctx, &ctx->cpi->common.error);
940 vpx_clear_system_state();
941 return res;
942 }
943 ctx->cpi->common.error.setjmp = 1;
944
945 /* Set up internal flags */
946 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR) {
947 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
948 }
949
950 if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION) {
951 ((VP8_COMP *)ctx->cpi)->output_partition = 1;
952 }
953
954 /* Convert API flags to internal codec lib flags */
955 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
956
957 if (img != NULL) {
958 YV12_BUFFER_CONFIG sd;
959
960 if (!ctx->pts_offset_initialized) {
961 ctx->pts_offset = pts_val;
962 ctx->pts_offset_initialized = 1;
963 }
964 if (pts_val < ctx->pts_offset) {
965 vpx_internal_error(&ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
966 "pts is smaller than initial pts");
967 }
968 pts_val -= ctx->pts_offset;
969 if (pts_val > INT64_MAX / ctx->timestamp_ratio.num) {
970 vpx_internal_error(
971 &ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
972 "conversion of relative pts to ticks would overflow");
973 }
974 dst_time_stamp =
975 pts_val * ctx->timestamp_ratio.num / ctx->timestamp_ratio.den;
976 #if ULONG_MAX > INT64_MAX
977 if (duration > INT64_MAX) {
978 vpx_internal_error(&ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
979 "duration is too big");
980 }
981 #endif
982 if (pts_val > INT64_MAX - (int64_t)duration) {
983 vpx_internal_error(&ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
984 "relative pts + duration is too big");
985 }
986 vpx_codec_pts_t pts_end = pts_val + (int64_t)duration;
987 if (pts_end > INT64_MAX / ctx->timestamp_ratio.num) {
988 vpx_internal_error(
989 &ctx->cpi->common.error, VPX_CODEC_INVALID_PARAM,
990 "conversion of relative pts + duration to ticks would overflow");
991 }
992 dst_end_time_stamp =
993 pts_end * ctx->timestamp_ratio.num / ctx->timestamp_ratio.den;
994
995 res = image2yuvconfig(img, &sd);
996
997 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags, &sd,
998 dst_time_stamp, dst_end_time_stamp)) {
999 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
1000 res = update_error_state(ctx, &cpi->common.error);
1001 }
1002
1003 /* reset for next frame */
1004 ctx->next_frame_flag = 0;
1005 }
1006
1007 cx_data = ctx->cx_data;
1008 cx_data_sz = ctx->cx_data_sz;
1009 cx_data_end = ctx->cx_data + cx_data_sz;
1010 lib_flags = 0;
1011
1012 while (cx_data_sz >= ctx->cx_data_sz / 2) {
1013 comp_data_state = vp8_get_compressed_data(
1014 ctx->cpi, &lib_flags, &size, cx_data, cx_data_end, &dst_time_stamp,
1015 &dst_end_time_stamp, !img);
1016
1017 if (comp_data_state == VPX_CODEC_CORRUPT_FRAME) {
1018 ctx->cpi->common.error.setjmp = 0;
1019 return VPX_CODEC_CORRUPT_FRAME;
1020 } else if (comp_data_state == -1) {
1021 break;
1022 }
1023
1024 if (size) {
1025 vpx_codec_pts_t round, delta;
1026 vpx_codec_cx_pkt_t pkt;
1027 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
1028
1029 /* Add the frame packet to the list of returned packets. */
1030 round = (vpx_codec_pts_t)ctx->timestamp_ratio.num / 2;
1031 if (round > 0) --round;
1032 delta = (dst_end_time_stamp - dst_time_stamp);
1033 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1034 pkt.data.frame.pts =
1035 (dst_time_stamp * ctx->timestamp_ratio.den + round) /
1036 ctx->timestamp_ratio.num +
1037 ctx->pts_offset;
1038 pkt.data.frame.duration =
1039 (unsigned long)((delta * ctx->timestamp_ratio.den + round) /
1040 ctx->timestamp_ratio.num);
1041 pkt.data.frame.flags = lib_flags << 16;
1042 pkt.data.frame.width[0] = cpi->common.Width;
1043 pkt.data.frame.height[0] = cpi->common.Height;
1044 pkt.data.frame.spatial_layer_encoded[0] = 1;
1045
1046 if (lib_flags & FRAMEFLAGS_KEY) {
1047 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
1048 }
1049
1050 if (!cpi->common.show_frame) {
1051 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
1052
1053 /* This timestamp should be as close as possible to the
1054 * prior PTS so that if a decoder uses pts to schedule when
1055 * to do this, we start right after last frame was decoded.
1056 * Invisible frames have no duration.
1057 */
1058 pkt.data.frame.pts =
1059 ((cpi->last_time_stamp_seen * ctx->timestamp_ratio.den + round) /
1060 ctx->timestamp_ratio.num) +
1061 ctx->pts_offset + 1;
1062 pkt.data.frame.duration = 0;
1063 }
1064
1065 if (cpi->droppable) pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
1066
1067 if (cpi->output_partition) {
1068 int i;
1069 const int num_partitions =
1070 (1 << cpi->common.multi_token_partition) + 1;
1071
1072 pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
1073
1074 for (i = 0; i < num_partitions; ++i) {
1075 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1076 pkt.data.frame.buf = cpi->partition_d[i];
1077 #else
1078 pkt.data.frame.buf = cx_data;
1079 cx_data += cpi->partition_sz[i];
1080 cx_data_sz -= cpi->partition_sz[i];
1081 #endif
1082 pkt.data.frame.sz = cpi->partition_sz[i];
1083 pkt.data.frame.partition_id = i;
1084 /* don't set the fragment bit for the last partition */
1085 if (i == (num_partitions - 1)) {
1086 pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
1087 }
1088 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1089 }
1090 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1091 /* In lagged mode the encoder can buffer multiple frames.
1092 * We don't want this in partitioned output because
1093 * partitions are spread all over the output buffer.
1094 * So, force an exit!
1095 */
1096 cx_data_sz -= ctx->cx_data_sz / 2;
1097 #endif
1098 } else {
1099 pkt.data.frame.buf = cx_data;
1100 pkt.data.frame.sz = size;
1101 pkt.data.frame.partition_id = -1;
1102 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1103 cx_data += size;
1104 cx_data_sz -= size;
1105 }
1106 }
1107 }
1108 ctx->cpi->common.error.setjmp = 0;
1109 }
1110
1111 return res;
1112 }
1113
vp8e_get_cxdata(vpx_codec_alg_priv_t * ctx,vpx_codec_iter_t * iter)1114 static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
1115 vpx_codec_iter_t *iter) {
1116 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1117 }
1118
vp8e_set_reference(vpx_codec_alg_priv_t * ctx,va_list args)1119 static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
1120 va_list args) {
1121 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1122
1123 if (data) {
1124 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1125 YV12_BUFFER_CONFIG sd;
1126
1127 image2yuvconfig(&frame->img, &sd);
1128 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
1129 return VPX_CODEC_OK;
1130 } else {
1131 return VPX_CODEC_INVALID_PARAM;
1132 }
1133 }
1134
vp8e_get_reference(vpx_codec_alg_priv_t * ctx,va_list args)1135 static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
1136 va_list args) {
1137 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1138
1139 if (data) {
1140 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1141 YV12_BUFFER_CONFIG sd;
1142
1143 image2yuvconfig(&frame->img, &sd);
1144 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
1145 return VPX_CODEC_OK;
1146 } else {
1147 return VPX_CODEC_INVALID_PARAM;
1148 }
1149 }
1150
vp8e_set_previewpp(vpx_codec_alg_priv_t * ctx,va_list args)1151 static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
1152 va_list args) {
1153 #if CONFIG_POSTPROC
1154 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
1155
1156 if (data) {
1157 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
1158 return VPX_CODEC_OK;
1159 } else {
1160 return VPX_CODEC_INVALID_PARAM;
1161 }
1162 #else
1163 (void)ctx;
1164 (void)args;
1165 return VPX_CODEC_INCAPABLE;
1166 #endif
1167 }
1168
vp8e_get_preview(vpx_codec_alg_priv_t * ctx)1169 static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx) {
1170 YV12_BUFFER_CONFIG sd;
1171 vp8_ppflags_t flags;
1172 vp8_zero(flags);
1173
1174 if (ctx->preview_ppcfg.post_proc_flag) {
1175 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1176 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1177 flags.noise_level = ctx->preview_ppcfg.noise_level;
1178 }
1179
1180 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags)) {
1181 /*
1182 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
1183 sd.y_width + 2*VP8BORDERINPIXELS,
1184 sd.y_height + 2*VP8BORDERINPIXELS,
1185 1,
1186 sd.buffer_alloc);
1187 vpx_img_set_rect(&ctx->preview_img,
1188 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
1189 sd.y_width, sd.y_height);
1190 */
1191
1192 ctx->preview_img.bps = 12;
1193 ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
1194 ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
1195 ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
1196
1197 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
1198 ctx->preview_img.x_chroma_shift = 1;
1199 ctx->preview_img.y_chroma_shift = 1;
1200
1201 ctx->preview_img.d_w = sd.y_width;
1202 ctx->preview_img.d_h = sd.y_height;
1203 ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
1204 ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
1205 ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
1206 ctx->preview_img.w = sd.y_width;
1207 ctx->preview_img.h = sd.y_height;
1208
1209 return &ctx->preview_img;
1210 } else {
1211 return NULL;
1212 }
1213 }
1214
vp8e_set_frame_flags(vpx_codec_alg_priv_t * ctx,va_list args)1215 static vpx_codec_err_t vp8e_set_frame_flags(vpx_codec_alg_priv_t *ctx,
1216 va_list args) {
1217 int frame_flags = va_arg(args, int);
1218 ctx->control_frame_flags = frame_flags;
1219 return set_reference_and_update(ctx, frame_flags);
1220 }
1221
vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t * ctx,va_list args)1222 static vpx_codec_err_t vp8e_set_temporal_layer_id(vpx_codec_alg_priv_t *ctx,
1223 va_list args) {
1224 int layer_id = va_arg(args, int);
1225 if (layer_id < 0 || layer_id >= (int)ctx->cfg.ts_number_layers) {
1226 return VPX_CODEC_INVALID_PARAM;
1227 }
1228 ctx->cpi->temporal_layer_id = layer_id;
1229 return VPX_CODEC_OK;
1230 }
1231
vp8e_set_roi_map(vpx_codec_alg_priv_t * ctx,va_list args)1232 static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
1233 va_list args) {
1234 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1235
1236 if (data) {
1237 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1238
1239 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols,
1240 roi->delta_q, roi->delta_lf, roi->static_threshold)) {
1241 return VPX_CODEC_OK;
1242 } else {
1243 return VPX_CODEC_INVALID_PARAM;
1244 }
1245 } else {
1246 return VPX_CODEC_INVALID_PARAM;
1247 }
1248 }
1249
vp8e_set_activemap(vpx_codec_alg_priv_t * ctx,va_list args)1250 static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
1251 va_list args) {
1252 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1253
1254 if (data) {
1255 vpx_active_map_t *map = (vpx_active_map_t *)data;
1256
1257 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols)) {
1258 return VPX_CODEC_OK;
1259 } else {
1260 return VPX_CODEC_INVALID_PARAM;
1261 }
1262 } else {
1263 return VPX_CODEC_INVALID_PARAM;
1264 }
1265 }
1266
vp8e_set_scalemode(vpx_codec_alg_priv_t * ctx,va_list args)1267 static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
1268 va_list args) {
1269 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1270
1271 if (data) {
1272 int res;
1273 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data;
1274 res = vp8_set_internal_size(ctx->cpi, scalemode.h_scaling_mode,
1275 scalemode.v_scaling_mode);
1276
1277 if (!res) {
1278 /*force next frame a key frame to effect scaling mode */
1279 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1280 return VPX_CODEC_OK;
1281 } else {
1282 return VPX_CODEC_INVALID_PARAM;
1283 }
1284 } else {
1285 return VPX_CODEC_INVALID_PARAM;
1286 }
1287 }
1288
1289 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] = {
1290 { VP8_SET_REFERENCE, vp8e_set_reference },
1291 { VP8_COPY_REFERENCE, vp8e_get_reference },
1292 { VP8_SET_POSTPROC, vp8e_set_previewpp },
1293 { VP8E_SET_FRAME_FLAGS, vp8e_set_frame_flags },
1294 { VP8E_SET_TEMPORAL_LAYER_ID, vp8e_set_temporal_layer_id },
1295 { VP8E_SET_ROI_MAP, vp8e_set_roi_map },
1296 { VP8E_SET_ACTIVEMAP, vp8e_set_activemap },
1297 { VP8E_SET_SCALEMODE, vp8e_set_scalemode },
1298 { VP8E_SET_CPUUSED, set_cpu_used },
1299 { VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity },
1300 { VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref },
1301 { VP8E_SET_SHARPNESS, set_sharpness },
1302 { VP8E_SET_STATIC_THRESHOLD, set_static_thresh },
1303 { VP8E_SET_TOKEN_PARTITIONS, set_token_partitions },
1304 { VP8E_GET_LAST_QUANTIZER, get_quantizer },
1305 { VP8E_GET_LAST_QUANTIZER_64, get_quantizer64 },
1306 { VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames },
1307 { VP8E_SET_ARNR_STRENGTH, set_arnr_strength },
1308 { VP8E_SET_ARNR_TYPE, set_arnr_type },
1309 { VP8E_SET_TUNING, set_tuning },
1310 { VP8E_SET_CQ_LEVEL, set_cq_level },
1311 { VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct },
1312 { VP8E_SET_SCREEN_CONTENT_MODE, set_screen_content_mode },
1313 { VP8E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
1314 { VP8E_SET_RTC_EXTERNAL_RATECTRL, ctrl_set_rtc_external_ratectrl },
1315 { -1, NULL },
1316 };
1317
1318 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] = {
1319 { 0,
1320 {
1321 0, /* g_usage (unused) */
1322 0, /* g_threads */
1323 0, /* g_profile */
1324
1325 320, /* g_width */
1326 240, /* g_height */
1327 VPX_BITS_8, /* g_bit_depth */
1328 8, /* g_input_bit_depth */
1329
1330 { 1, 30 }, /* g_timebase */
1331
1332 0, /* g_error_resilient */
1333
1334 VPX_RC_ONE_PASS, /* g_pass */
1335
1336 0, /* g_lag_in_frames */
1337
1338 0, /* rc_dropframe_thresh */
1339 0, /* rc_resize_allowed */
1340 1, /* rc_scaled_width */
1341 1, /* rc_scaled_height */
1342 60, /* rc_resize_down_thresh */
1343 30, /* rc_resize_up_thresh */
1344
1345 VPX_VBR, /* rc_end_usage */
1346 { NULL, 0 }, /* rc_twopass_stats_in */
1347 { NULL, 0 }, /* rc_firstpass_mb_stats_in */
1348 256, /* rc_target_bitrate */
1349 4, /* rc_min_quantizer */
1350 63, /* rc_max_quantizer */
1351 100, /* rc_undershoot_pct */
1352 100, /* rc_overshoot_pct */
1353
1354 6000, /* rc_max_buffer_size */
1355 4000, /* rc_buffer_initial_size; */
1356 5000, /* rc_buffer_optimal_size; */
1357
1358 50, /* rc_two_pass_vbrbias */
1359 0, /* rc_two_pass_vbrmin_section */
1360 400, /* rc_two_pass_vbrmax_section */
1361 0, // rc_2pass_vbr_corpus_complexity (only has meaningfull for VP9)
1362
1363 /* keyframing settings (kf) */
1364 VPX_KF_AUTO, /* g_kfmode*/
1365 0, /* kf_min_dist */
1366 128, /* kf_max_dist */
1367
1368 VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
1369 { 0 },
1370 { 0 }, /* ss_target_bitrate */
1371 1, /* ts_number_layers */
1372 { 0 }, /* ts_target_bitrate */
1373 { 0 }, /* ts_rate_decimator */
1374 0, /* ts_periodicity */
1375 { 0 }, /* ts_layer_id */
1376 { 0 }, /* layer_target_bitrate */
1377 0, /* temporal_layering_mode */
1378 0, /* use_vizier_rc_params */
1379 { 1, 1 }, /* active_wq_factor */
1380 { 1, 1 }, /* err_per_mb_factor */
1381 { 1, 1 }, /* sr_default_decay_limit */
1382 { 1, 1 }, /* sr_diff_factor */
1383 { 1, 1 }, /* kf_err_per_mb_factor */
1384 { 1, 1 }, /* kf_frame_min_boost_factor */
1385 { 1, 1 }, /* kf_frame_max_boost_first_factor */
1386 { 1, 1 }, /* kf_frame_max_boost_subs_factor */
1387 { 1, 1 }, /* kf_max_total_boost_factor */
1388 { 1, 1 }, /* gf_max_total_boost_factor */
1389 { 1, 1 }, /* gf_frame_max_boost_factor */
1390 { 1, 1 }, /* zm_factor */
1391 { 1, 1 }, /* rd_mult_inter_qp_fac */
1392 { 1, 1 }, /* rd_mult_arf_qp_fac */
1393 { 1, 1 }, /* rd_mult_key_qp_fac */
1394 } },
1395 };
1396
1397 #ifndef VERSION_STRING
1398 #define VERSION_STRING
1399 #endif
1400 CODEC_INTERFACE(vpx_codec_vp8_cx) = {
1401 "WebM Project VP8 Encoder" VERSION_STRING,
1402 VPX_CODEC_INTERNAL_ABI_VERSION,
1403 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR | VPX_CODEC_CAP_OUTPUT_PARTITION,
1404 /* vpx_codec_caps_t caps; */
1405 vp8e_init, /* vpx_codec_init_fn_t init; */
1406 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1407 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1408 {
1409 NULL, /* vpx_codec_peek_si_fn_t peek_si; */
1410 NULL, /* vpx_codec_get_si_fn_t get_si; */
1411 NULL, /* vpx_codec_decode_fn_t decode; */
1412 NULL, /* vpx_codec_frame_get_fn_t frame_get; */
1413 NULL, /* vpx_codec_set_fb_fn_t set_fb_fn; */
1414 },
1415 {
1416 1, /* 1 cfg map */
1417 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t cfg_maps; */
1418 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
1419 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t get_cx_data; */
1420 vp8e_set_config,
1421 NULL,
1422 vp8e_get_preview,
1423 vp8e_mr_alloc_mem,
1424 } /* encoder functions */
1425 };
1426