2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
14 #include "./vpx_config.h"
15 #include "vpx/vpx_codec.h"
16 #include "vpx_ports/vpx_once.h"
17 #include "vpx/internal/vpx_codec_internal.h"
18 #include "./vpx_version.h"
19 #include "vp9/encoder/vp9_encoder.h"
20 #include "vpx/vp8cx.h"
21 #include "vp9/encoder/vp9_firstpass.h"
22 #include "vp9/vp9_iface_common.h"
25 int cpu_used
; // available cpu percentage in 1/16
26 unsigned int enable_auto_alt_ref
;
27 unsigned int noise_sensitivity
;
28 unsigned int sharpness
;
29 unsigned int static_thresh
;
30 unsigned int tile_columns
;
31 unsigned int tile_rows
;
32 unsigned int arnr_max_frames
;
33 unsigned int arnr_strength
;
35 unsigned int cq_level
; // constrained quality level
36 unsigned int rc_max_intra_bitrate_pct
;
37 unsigned int rc_max_inter_bitrate_pct
;
38 unsigned int gf_cbr_boost_pct
;
39 unsigned int lossless
;
40 unsigned int frame_parallel_decoding_mode
;
42 unsigned int frame_periodic_boost
;
43 vpx_bit_depth_t bit_depth
;
44 vp9e_tune_content content
;
45 vpx_color_space_t color_space
;
48 static struct vp9_extracfg default_extra_cfg
= {
50 1, // enable_auto_alt_ref
51 0, // noise_sensitivity
58 VP8_TUNE_PSNR
, // tuning
60 0, // rc_max_intra_bitrate_pct
61 0, // rc_max_inter_bitrate_pct
62 0, // gf_cbr_boost_pct
64 0, // frame_parallel_decoding_mode
66 0, // frame_periodic_delta_q
67 VPX_BITS_8
, // Bit depth
68 VP9E_CONTENT_DEFAULT
, // content
69 VPX_CS_UNKNOWN
, // color space
72 struct vpx_codec_alg_priv
{
73 vpx_codec_priv_t base
;
74 vpx_codec_enc_cfg_t cfg
;
75 struct vp9_extracfg extra_cfg
;
76 VP9EncoderConfig oxcf
;
78 unsigned char *cx_data
;
80 unsigned char *pending_cx_data
;
81 size_t pending_cx_data_sz
;
82 int pending_frame_count
;
83 size_t pending_frame_sizes
[8];
84 size_t pending_frame_magnitude
;
85 vpx_image_t preview_img
;
86 vpx_enc_frame_flags_t next_frame_flags
;
87 vp8_postproc_cfg_t preview_ppcfg
;
88 vpx_codec_pkt_list_decl(256) pkt_list
;
89 unsigned int fixed_kf_cntr
;
90 vpx_codec_priv_output_cx_pkt_cb_pair_t output_cx_pkt_cb
;
91 // BufferPool that holds all reference frames.
92 BufferPool
*buffer_pool
;
95 static VP9_REFFRAME
ref_frame_to_vp9_reframe(vpx_ref_frame_type_t frame
) {
100 return VP9_GOLD_FLAG
;
104 assert(0 && "Invalid Reference Frame");
105 return VP9_LAST_FLAG
;
108 static vpx_codec_err_t
update_error_state(vpx_codec_alg_priv_t
*ctx
,
109 const struct vpx_internal_error_info
*error
) {
110 const vpx_codec_err_t res
= error
->error_code
;
112 if (res
!= VPX_CODEC_OK
)
113 ctx
->base
.err_detail
= error
->has_detail
? error
->detail
: NULL
;
120 #define ERROR(str) do {\
121 ctx->base.err_detail = str;\
122 return VPX_CODEC_INVALID_PARAM;\
125 #define RANGE_CHECK(p, memb, lo, hi) do {\
126 if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
127 ERROR(#memb " out of range ["#lo".."#hi"]");\
130 #define RANGE_CHECK_HI(p, memb, hi) do {\
131 if (!((p)->memb <= (hi))) \
132 ERROR(#memb " out of range [.."#hi"]");\
135 #define RANGE_CHECK_LO(p, memb, lo) do {\
136 if (!((p)->memb >= (lo))) \
137 ERROR(#memb " out of range ["#lo"..]");\
140 #define RANGE_CHECK_BOOL(p, memb) do {\
141 if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
144 static vpx_codec_err_t
validate_config(vpx_codec_alg_priv_t
*ctx
,
145 const vpx_codec_enc_cfg_t
*cfg
,
146 const struct vp9_extracfg
*extra_cfg
) {
147 RANGE_CHECK(cfg
, g_w
, 1, 65535); // 16 bits available
148 RANGE_CHECK(cfg
, g_h
, 1, 65535); // 16 bits available
149 RANGE_CHECK(cfg
, g_timebase
.den
, 1, 1000000000);
150 RANGE_CHECK(cfg
, g_timebase
.num
, 1, cfg
->g_timebase
.den
);
151 RANGE_CHECK_HI(cfg
, g_profile
, 3);
153 RANGE_CHECK_HI(cfg
, rc_max_quantizer
, 63);
154 RANGE_CHECK_HI(cfg
, rc_min_quantizer
, cfg
->rc_max_quantizer
);
155 RANGE_CHECK_BOOL(extra_cfg
, lossless
);
156 RANGE_CHECK(extra_cfg
, aq_mode
, 0, AQ_MODE_COUNT
- 1);
157 RANGE_CHECK(extra_cfg
, frame_periodic_boost
, 0, 1);
158 RANGE_CHECK_HI(cfg
, g_threads
, 64);
159 RANGE_CHECK_HI(cfg
, g_lag_in_frames
, MAX_LAG_BUFFERS
);
160 RANGE_CHECK(cfg
, rc_end_usage
, VPX_VBR
, VPX_Q
);
161 RANGE_CHECK_HI(cfg
, rc_undershoot_pct
, 1000);
162 RANGE_CHECK_HI(cfg
, rc_overshoot_pct
, 1000);
163 RANGE_CHECK_HI(cfg
, rc_2pass_vbr_bias_pct
, 100);
164 RANGE_CHECK(cfg
, kf_mode
, VPX_KF_DISABLED
, VPX_KF_AUTO
);
165 RANGE_CHECK_BOOL(cfg
, rc_resize_allowed
);
166 RANGE_CHECK_HI(cfg
, rc_dropframe_thresh
, 100);
167 RANGE_CHECK_HI(cfg
, rc_resize_up_thresh
, 100);
168 RANGE_CHECK_HI(cfg
, rc_resize_down_thresh
, 100);
169 RANGE_CHECK(cfg
, g_pass
, VPX_RC_ONE_PASS
, VPX_RC_LAST_PASS
);
171 if (cfg
->rc_resize_allowed
== 1) {
172 RANGE_CHECK(cfg
, rc_scaled_width
, 0, cfg
->g_w
);
173 RANGE_CHECK(cfg
, rc_scaled_height
, 0, cfg
->g_h
);
176 RANGE_CHECK(cfg
, ss_number_layers
, 1, VPX_SS_MAX_LAYERS
);
177 RANGE_CHECK(cfg
, ts_number_layers
, 1, VPX_TS_MAX_LAYERS
);
179 if (cfg
->ts_number_layers
> 1) {
181 for (i
= 1; i
< cfg
->ts_number_layers
; ++i
)
182 if (cfg
->ts_target_bitrate
[i
] < cfg
->ts_target_bitrate
[i
- 1])
183 ERROR("ts_target_bitrate entries are not increasing");
185 RANGE_CHECK(cfg
, ts_rate_decimator
[cfg
->ts_number_layers
- 1], 1, 1);
186 for (i
= cfg
->ts_number_layers
- 2; i
> 0; --i
)
187 if (cfg
->ts_rate_decimator
[i
- 1] != 2 * cfg
->ts_rate_decimator
[i
])
188 ERROR("ts_rate_decimator factors are not powers of 2");
191 #if CONFIG_SPATIAL_SVC
193 if ((cfg
->ss_number_layers
> 1 || cfg
->ts_number_layers
> 1) &&
194 cfg
->g_pass
== VPX_RC_LAST_PASS
) {
195 unsigned int i
, alt_ref_sum
= 0;
196 for (i
= 0; i
< cfg
->ss_number_layers
; ++i
) {
197 if (cfg
->ss_enable_auto_alt_ref
[i
])
200 if (alt_ref_sum
> REF_FRAMES
- cfg
->ss_number_layers
)
201 ERROR("Not enough ref buffers for svc alt ref frames");
202 if (cfg
->ss_number_layers
* cfg
->ts_number_layers
> 3 &&
203 cfg
->g_error_resilient
== 0)
204 ERROR("Multiple frame context are not supported for more than 3 layers");
208 // VP9 does not support a lower bound on the keyframe interval in
209 // automatic keyframe placement mode.
210 if (cfg
->kf_mode
!= VPX_KF_DISABLED
&&
211 cfg
->kf_min_dist
!= cfg
->kf_max_dist
&&
212 cfg
->kf_min_dist
> 0)
213 ERROR("kf_min_dist not supported in auto mode, use 0 "
214 "or kf_max_dist instead.");
216 RANGE_CHECK(extra_cfg
, enable_auto_alt_ref
, 0, 2);
217 RANGE_CHECK(extra_cfg
, cpu_used
, -8, 8);
218 RANGE_CHECK_HI(extra_cfg
, noise_sensitivity
, 6);
219 RANGE_CHECK(extra_cfg
, tile_columns
, 0, 6);
220 RANGE_CHECK(extra_cfg
, tile_rows
, 0, 2);
221 RANGE_CHECK_HI(extra_cfg
, sharpness
, 7);
222 RANGE_CHECK(extra_cfg
, arnr_max_frames
, 0, 15);
223 RANGE_CHECK_HI(extra_cfg
, arnr_strength
, 6);
224 RANGE_CHECK(extra_cfg
, cq_level
, 0, 63);
225 RANGE_CHECK(cfg
, g_bit_depth
, VPX_BITS_8
, VPX_BITS_12
);
226 RANGE_CHECK(cfg
, g_input_bit_depth
, 8, 12);
227 RANGE_CHECK(extra_cfg
, content
,
228 VP9E_CONTENT_DEFAULT
, VP9E_CONTENT_INVALID
- 1);
230 // TODO(yaowu): remove this when ssim tuning is implemented for vp9
231 if (extra_cfg
->tuning
== VP8_TUNE_SSIM
)
232 ERROR("Option --tune=ssim is not currently supported in VP9.");
234 if (cfg
->g_pass
== VPX_RC_LAST_PASS
) {
235 const size_t packet_sz
= sizeof(FIRSTPASS_STATS
);
236 const int n_packets
= (int)(cfg
->rc_twopass_stats_in
.sz
/ packet_sz
);
237 const FIRSTPASS_STATS
*stats
;
239 if (cfg
->rc_twopass_stats_in
.buf
== NULL
)
240 ERROR("rc_twopass_stats_in.buf not set.");
242 if (cfg
->rc_twopass_stats_in
.sz
% packet_sz
)
243 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
245 if (cfg
->ss_number_layers
> 1 || cfg
->ts_number_layers
> 1) {
247 unsigned int n_packets_per_layer
[VPX_SS_MAX_LAYERS
] = {0};
249 stats
= cfg
->rc_twopass_stats_in
.buf
;
250 for (i
= 0; i
< n_packets
; ++i
) {
251 const int layer_id
= (int)stats
[i
].spatial_layer_id
;
252 if (layer_id
>= 0 && layer_id
< (int)cfg
->ss_number_layers
) {
253 ++n_packets_per_layer
[layer_id
];
257 for (i
= 0; i
< (int)cfg
->ss_number_layers
; ++i
) {
258 unsigned int layer_id
;
259 if (n_packets_per_layer
[i
] < 2) {
260 ERROR("rc_twopass_stats_in requires at least two packets for each "
264 stats
= (const FIRSTPASS_STATS
*)cfg
->rc_twopass_stats_in
.buf
+
265 n_packets
- cfg
->ss_number_layers
+ i
;
266 layer_id
= (int)stats
->spatial_layer_id
;
268 if (layer_id
>= cfg
->ss_number_layers
269 ||(unsigned int)(stats
->count
+ 0.5) !=
270 n_packets_per_layer
[layer_id
] - 1)
271 ERROR("rc_twopass_stats_in missing EOS stats packet");
274 if (cfg
->rc_twopass_stats_in
.sz
< 2 * packet_sz
)
275 ERROR("rc_twopass_stats_in requires at least two packets.");
278 (const FIRSTPASS_STATS
*)cfg
->rc_twopass_stats_in
.buf
+ n_packets
- 1;
280 if ((int)(stats
->count
+ 0.5) != n_packets
- 1)
281 ERROR("rc_twopass_stats_in missing EOS stats packet");
285 #if !CONFIG_VP9_HIGHBITDEPTH
286 if (cfg
->g_profile
> (unsigned int)PROFILE_1
) {
287 ERROR("Profile > 1 not supported in this build configuration");
290 if (cfg
->g_profile
<= (unsigned int)PROFILE_1
&&
291 cfg
->g_bit_depth
> VPX_BITS_8
) {
292 ERROR("Codec high bit-depth not supported in profile < 2");
294 if (cfg
->g_profile
<= (unsigned int)PROFILE_1
&&
295 cfg
->g_input_bit_depth
> 8) {
296 ERROR("Source high bit-depth not supported in profile < 2");
298 if (cfg
->g_profile
> (unsigned int)PROFILE_1
&&
299 cfg
->g_bit_depth
== VPX_BITS_8
) {
300 ERROR("Codec bit-depth 8 not supported in profile > 1");
302 RANGE_CHECK(extra_cfg
, color_space
, VPX_CS_UNKNOWN
, VPX_CS_SRGB
);
306 static vpx_codec_err_t
validate_img(vpx_codec_alg_priv_t
*ctx
,
307 const vpx_image_t
*img
) {
309 case VPX_IMG_FMT_YV12
:
310 case VPX_IMG_FMT_I420
:
311 case VPX_IMG_FMT_I42016
:
313 case VPX_IMG_FMT_I422
:
314 case VPX_IMG_FMT_I444
:
315 case VPX_IMG_FMT_I440
:
316 if (ctx
->cfg
.g_profile
!= (unsigned int)PROFILE_1
) {
317 ERROR("Invalid image format. I422, I444, I440 images are "
318 "not supported in profile.");
321 case VPX_IMG_FMT_I42216
:
322 case VPX_IMG_FMT_I44416
:
323 case VPX_IMG_FMT_I44016
:
324 if (ctx
->cfg
.g_profile
!= (unsigned int)PROFILE_1
&&
325 ctx
->cfg
.g_profile
!= (unsigned int)PROFILE_3
) {
326 ERROR("Invalid image format. 16-bit I422, I444, I440 images are "
327 "not supported in profile.");
331 ERROR("Invalid image format. Only YV12, I420, I422, I444 images are "
336 if (img
->d_w
!= ctx
->cfg
.g_w
|| img
->d_h
!= ctx
->cfg
.g_h
)
337 ERROR("Image size must match encoder init configuration size");
342 static int get_image_bps(const vpx_image_t
*img
) {
344 case VPX_IMG_FMT_YV12
:
345 case VPX_IMG_FMT_I420
: return 12;
346 case VPX_IMG_FMT_I422
: return 16;
347 case VPX_IMG_FMT_I444
: return 24;
348 case VPX_IMG_FMT_I440
: return 16;
349 case VPX_IMG_FMT_I42016
: return 24;
350 case VPX_IMG_FMT_I42216
: return 32;
351 case VPX_IMG_FMT_I44416
: return 48;
352 case VPX_IMG_FMT_I44016
: return 32;
353 default: assert(0 && "Invalid image format"); break;
358 static vpx_codec_err_t
set_encoder_config(
359 VP9EncoderConfig
*oxcf
,
360 const vpx_codec_enc_cfg_t
*cfg
,
361 const struct vp9_extracfg
*extra_cfg
) {
362 const int is_vbr
= cfg
->rc_end_usage
== VPX_VBR
;
363 oxcf
->profile
= cfg
->g_profile
;
364 oxcf
->max_threads
= (int)cfg
->g_threads
;
365 oxcf
->width
= cfg
->g_w
;
366 oxcf
->height
= cfg
->g_h
;
367 oxcf
->bit_depth
= cfg
->g_bit_depth
;
368 oxcf
->input_bit_depth
= cfg
->g_input_bit_depth
;
369 // guess a frame rate if out of whack, use 30
370 oxcf
->init_framerate
= (double)cfg
->g_timebase
.den
/ cfg
->g_timebase
.num
;
371 if (oxcf
->init_framerate
> 180)
372 oxcf
->init_framerate
= 30;
376 switch (cfg
->g_pass
) {
377 case VPX_RC_ONE_PASS
:
380 case VPX_RC_FIRST_PASS
:
383 case VPX_RC_LAST_PASS
:
388 oxcf
->lag_in_frames
= cfg
->g_pass
== VPX_RC_FIRST_PASS
? 0
389 : cfg
->g_lag_in_frames
;
390 oxcf
->rc_mode
= cfg
->rc_end_usage
;
392 // Convert target bandwidth from Kbit/s to Bit/s
393 oxcf
->target_bandwidth
= 1000 * cfg
->rc_target_bitrate
;
394 oxcf
->rc_max_intra_bitrate_pct
= extra_cfg
->rc_max_intra_bitrate_pct
;
395 oxcf
->rc_max_inter_bitrate_pct
= extra_cfg
->rc_max_inter_bitrate_pct
;
396 oxcf
->gf_cbr_boost_pct
= extra_cfg
->gf_cbr_boost_pct
;
398 oxcf
->best_allowed_q
=
399 extra_cfg
->lossless
? 0 : vp9_quantizer_to_qindex(cfg
->rc_min_quantizer
);
400 oxcf
->worst_allowed_q
=
401 extra_cfg
->lossless
? 0 : vp9_quantizer_to_qindex(cfg
->rc_max_quantizer
);
402 oxcf
->cq_level
= vp9_quantizer_to_qindex(extra_cfg
->cq_level
);
405 oxcf
->under_shoot_pct
= cfg
->rc_undershoot_pct
;
406 oxcf
->over_shoot_pct
= cfg
->rc_overshoot_pct
;
408 oxcf
->scaled_frame_width
= cfg
->rc_scaled_width
;
409 oxcf
->scaled_frame_height
= cfg
->rc_scaled_height
;
410 if (cfg
->rc_resize_allowed
== 1) {
412 (oxcf
->scaled_frame_width
== 0 || oxcf
->scaled_frame_height
== 0) ?
413 RESIZE_DYNAMIC
: RESIZE_FIXED
;
415 oxcf
->resize_mode
= RESIZE_NONE
;
418 oxcf
->maximum_buffer_size_ms
= is_vbr
? 240000 : cfg
->rc_buf_sz
;
419 oxcf
->starting_buffer_level_ms
= is_vbr
? 60000 : cfg
->rc_buf_initial_sz
;
420 oxcf
->optimal_buffer_level_ms
= is_vbr
? 60000 : cfg
->rc_buf_optimal_sz
;
422 oxcf
->drop_frames_water_mark
= cfg
->rc_dropframe_thresh
;
424 oxcf
->two_pass_vbrbias
= cfg
->rc_2pass_vbr_bias_pct
;
425 oxcf
->two_pass_vbrmin_section
= cfg
->rc_2pass_vbr_minsection_pct
;
426 oxcf
->two_pass_vbrmax_section
= cfg
->rc_2pass_vbr_maxsection_pct
;
428 oxcf
->auto_key
= cfg
->kf_mode
== VPX_KF_AUTO
&&
429 cfg
->kf_min_dist
!= cfg
->kf_max_dist
;
431 oxcf
->key_freq
= cfg
->kf_max_dist
;
433 oxcf
->speed
= abs(extra_cfg
->cpu_used
);
434 oxcf
->encode_breakout
= extra_cfg
->static_thresh
;
435 oxcf
->enable_auto_arf
= extra_cfg
->enable_auto_alt_ref
;
436 oxcf
->noise_sensitivity
= extra_cfg
->noise_sensitivity
;
437 oxcf
->sharpness
= extra_cfg
->sharpness
;
439 oxcf
->two_pass_stats_in
= cfg
->rc_twopass_stats_in
;
441 #if CONFIG_FP_MB_STATS
442 oxcf
->firstpass_mb_stats_in
= cfg
->rc_firstpass_mb_stats_in
;
445 oxcf
->color_space
= extra_cfg
->color_space
;
446 oxcf
->arnr_max_frames
= extra_cfg
->arnr_max_frames
;
447 oxcf
->arnr_strength
= extra_cfg
->arnr_strength
;
449 oxcf
->tuning
= extra_cfg
->tuning
;
450 oxcf
->content
= extra_cfg
->content
;
452 oxcf
->tile_columns
= extra_cfg
->tile_columns
;
453 oxcf
->tile_rows
= extra_cfg
->tile_rows
;
455 oxcf
->error_resilient_mode
= cfg
->g_error_resilient
;
456 oxcf
->frame_parallel_decoding_mode
= extra_cfg
->frame_parallel_decoding_mode
;
458 oxcf
->aq_mode
= extra_cfg
->aq_mode
;
460 oxcf
->frame_periodic_boost
= extra_cfg
->frame_periodic_boost
;
462 oxcf
->ss_number_layers
= cfg
->ss_number_layers
;
464 if (oxcf
->ss_number_layers
> 1) {
466 for (i
= 0; i
< VPX_SS_MAX_LAYERS
; ++i
) {
467 oxcf
->ss_target_bitrate
[i
] = 1000 * cfg
->ss_target_bitrate
[i
];
468 #if CONFIG_SPATIAL_SVC
469 oxcf
->ss_enable_auto_arf
[i
] = cfg
->ss_enable_auto_alt_ref
[i
];
472 } else if (oxcf
->ss_number_layers
== 1) {
473 oxcf
->ss_target_bitrate
[0] = (int)oxcf
->target_bandwidth
;
474 #if CONFIG_SPATIAL_SVC
475 oxcf
->ss_enable_auto_arf
[0] = extra_cfg
->enable_auto_alt_ref
;
479 oxcf
->ts_number_layers
= cfg
->ts_number_layers
;
481 if (oxcf
->ts_number_layers
> 1) {
483 for (i
= 0; i
< VPX_TS_MAX_LAYERS
; ++i
) {
484 oxcf
->ts_target_bitrate
[i
] = 1000 * cfg
->ts_target_bitrate
[i
];
485 oxcf
->ts_rate_decimator
[i
] = cfg
->ts_rate_decimator
[i
];
487 } else if (oxcf
->ts_number_layers
== 1) {
488 oxcf
->ts_target_bitrate
[0] = (int)oxcf
->target_bandwidth
;
489 oxcf
->ts_rate_decimator
[0] = 1;
493 printf("Current VP9 Settings: \n");
494 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
495 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
496 printf("sharpness: %d\n", oxcf->sharpness);
497 printf("cpu_used: %d\n", oxcf->cpu_used);
498 printf("Mode: %d\n", oxcf->mode);
499 printf("auto_key: %d\n", oxcf->auto_key);
500 printf("key_freq: %d\n", oxcf->key_freq);
501 printf("end_usage: %d\n", oxcf->end_usage);
502 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
503 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
504 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
505 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
506 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
507 printf("fixed_q: %d\n", oxcf->fixed_q);
508 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
509 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
510 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
511 printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
512 printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
513 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
514 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
515 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
516 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
517 printf("enable_auto_arf: %d\n", oxcf->enable_auto_arf);
518 printf("Version: %d\n", oxcf->Version);
519 printf("encode_breakout: %d\n", oxcf->encode_breakout);
520 printf("error resilient: %d\n", oxcf->error_resilient_mode);
521 printf("frame parallel detokenization: %d\n",
522 oxcf->frame_parallel_decoding_mode);
527 static vpx_codec_err_t
encoder_set_config(vpx_codec_alg_priv_t
*ctx
,
528 const vpx_codec_enc_cfg_t
*cfg
) {
532 if (cfg
->g_w
!= ctx
->cfg
.g_w
|| cfg
->g_h
!= ctx
->cfg
.g_h
) {
533 if (cfg
->g_lag_in_frames
> 1 || cfg
->g_pass
!= VPX_RC_ONE_PASS
)
534 ERROR("Cannot change width or height after initialization");
535 if (!valid_ref_frame_size(ctx
->cfg
.g_w
, ctx
->cfg
.g_h
, cfg
->g_w
, cfg
->g_h
) ||
536 (ctx
->cpi
->initial_width
&& (int)cfg
->g_w
> ctx
->cpi
->initial_width
) ||
537 (ctx
->cpi
->initial_height
&& (int)cfg
->g_h
> ctx
->cpi
->initial_height
))
541 // Prevent increasing lag_in_frames. This check is stricter than it needs
542 // to be -- the limit is not increasing past the first lag_in_frames
543 // value, but we don't track the initial config, only the last successful
545 if (cfg
->g_lag_in_frames
> ctx
->cfg
.g_lag_in_frames
)
546 ERROR("Cannot increase lag_in_frames");
548 res
= validate_config(ctx
, cfg
, &ctx
->extra_cfg
);
550 if (res
== VPX_CODEC_OK
) {
552 set_encoder_config(&ctx
->oxcf
, &ctx
->cfg
, &ctx
->extra_cfg
);
553 vp9_change_config(ctx
->cpi
, &ctx
->oxcf
);
557 ctx
->next_frame_flags
|= VPX_EFLAG_FORCE_KF
;
562 static vpx_codec_err_t
ctrl_get_quantizer(vpx_codec_alg_priv_t
*ctx
,
564 int *const arg
= va_arg(args
, int *);
566 return VPX_CODEC_INVALID_PARAM
;
567 *arg
= vp9_get_quantizer(ctx
->cpi
);
571 static vpx_codec_err_t
ctrl_get_quantizer64(vpx_codec_alg_priv_t
*ctx
,
573 int *const arg
= va_arg(args
, int *);
575 return VPX_CODEC_INVALID_PARAM
;
576 *arg
= vp9_qindex_to_quantizer(vp9_get_quantizer(ctx
->cpi
));
580 static vpx_codec_err_t
update_extra_cfg(vpx_codec_alg_priv_t
*ctx
,
581 const struct vp9_extracfg
*extra_cfg
) {
582 const vpx_codec_err_t res
= validate_config(ctx
, &ctx
->cfg
, extra_cfg
);
583 if (res
== VPX_CODEC_OK
) {
584 ctx
->extra_cfg
= *extra_cfg
;
585 set_encoder_config(&ctx
->oxcf
, &ctx
->cfg
, &ctx
->extra_cfg
);
586 vp9_change_config(ctx
->cpi
, &ctx
->oxcf
);
591 static vpx_codec_err_t
ctrl_set_cpuused(vpx_codec_alg_priv_t
*ctx
,
593 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
594 extra_cfg
.cpu_used
= CAST(VP8E_SET_CPUUSED
, args
);
595 return update_extra_cfg(ctx
, &extra_cfg
);
598 static vpx_codec_err_t
ctrl_set_enable_auto_alt_ref(vpx_codec_alg_priv_t
*ctx
,
600 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
601 extra_cfg
.enable_auto_alt_ref
= CAST(VP8E_SET_ENABLEAUTOALTREF
, args
);
602 return update_extra_cfg(ctx
, &extra_cfg
);
605 static vpx_codec_err_t
ctrl_set_noise_sensitivity(vpx_codec_alg_priv_t
*ctx
,
607 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
608 extra_cfg
.noise_sensitivity
= CAST(VP9E_SET_NOISE_SENSITIVITY
, args
);
609 return update_extra_cfg(ctx
, &extra_cfg
);
612 static vpx_codec_err_t
ctrl_set_sharpness(vpx_codec_alg_priv_t
*ctx
,
614 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
615 extra_cfg
.sharpness
= CAST(VP8E_SET_SHARPNESS
, args
);
616 return update_extra_cfg(ctx
, &extra_cfg
);
619 static vpx_codec_err_t
ctrl_set_static_thresh(vpx_codec_alg_priv_t
*ctx
,
621 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
622 extra_cfg
.static_thresh
= CAST(VP8E_SET_STATIC_THRESHOLD
, args
);
623 return update_extra_cfg(ctx
, &extra_cfg
);
626 static vpx_codec_err_t
ctrl_set_tile_columns(vpx_codec_alg_priv_t
*ctx
,
628 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
629 extra_cfg
.tile_columns
= CAST(VP9E_SET_TILE_COLUMNS
, args
);
630 return update_extra_cfg(ctx
, &extra_cfg
);
633 static vpx_codec_err_t
ctrl_set_tile_rows(vpx_codec_alg_priv_t
*ctx
,
635 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
636 extra_cfg
.tile_rows
= CAST(VP9E_SET_TILE_ROWS
, args
);
637 return update_extra_cfg(ctx
, &extra_cfg
);
640 static vpx_codec_err_t
ctrl_set_arnr_max_frames(vpx_codec_alg_priv_t
*ctx
,
642 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
643 extra_cfg
.arnr_max_frames
= CAST(VP8E_SET_ARNR_MAXFRAMES
, args
);
644 return update_extra_cfg(ctx
, &extra_cfg
);
647 static vpx_codec_err_t
ctrl_set_arnr_strength(vpx_codec_alg_priv_t
*ctx
,
649 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
650 extra_cfg
.arnr_strength
= CAST(VP8E_SET_ARNR_STRENGTH
, args
);
651 return update_extra_cfg(ctx
, &extra_cfg
);
654 static vpx_codec_err_t
ctrl_set_arnr_type(vpx_codec_alg_priv_t
*ctx
,
661 static vpx_codec_err_t
ctrl_set_tuning(vpx_codec_alg_priv_t
*ctx
,
663 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
664 extra_cfg
.tuning
= CAST(VP8E_SET_TUNING
, args
);
665 return update_extra_cfg(ctx
, &extra_cfg
);
668 static vpx_codec_err_t
ctrl_set_cq_level(vpx_codec_alg_priv_t
*ctx
,
670 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
671 extra_cfg
.cq_level
= CAST(VP8E_SET_CQ_LEVEL
, args
);
672 return update_extra_cfg(ctx
, &extra_cfg
);
675 static vpx_codec_err_t
ctrl_set_rc_max_intra_bitrate_pct(
676 vpx_codec_alg_priv_t
*ctx
, va_list args
) {
677 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
678 extra_cfg
.rc_max_intra_bitrate_pct
=
679 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT
, args
);
680 return update_extra_cfg(ctx
, &extra_cfg
);
683 static vpx_codec_err_t
ctrl_set_rc_max_inter_bitrate_pct(
684 vpx_codec_alg_priv_t
*ctx
, va_list args
) {
685 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
686 extra_cfg
.rc_max_inter_bitrate_pct
=
687 CAST(VP8E_SET_MAX_INTER_BITRATE_PCT
, args
);
688 return update_extra_cfg(ctx
, &extra_cfg
);
691 static vpx_codec_err_t
ctrl_set_rc_gf_cbr_boost_pct(
692 vpx_codec_alg_priv_t
*ctx
, va_list args
) {
693 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
694 extra_cfg
.gf_cbr_boost_pct
=
695 CAST(VP8E_SET_GF_CBR_BOOST_PCT
, args
);
696 return update_extra_cfg(ctx
, &extra_cfg
);
699 static vpx_codec_err_t
ctrl_set_lossless(vpx_codec_alg_priv_t
*ctx
,
701 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
702 extra_cfg
.lossless
= CAST(VP9E_SET_LOSSLESS
, args
);
703 return update_extra_cfg(ctx
, &extra_cfg
);
706 static vpx_codec_err_t
ctrl_set_frame_parallel_decoding_mode(
707 vpx_codec_alg_priv_t
*ctx
, va_list args
) {
708 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
709 extra_cfg
.frame_parallel_decoding_mode
=
710 CAST(VP9E_SET_FRAME_PARALLEL_DECODING
, args
);
711 return update_extra_cfg(ctx
, &extra_cfg
);
714 static vpx_codec_err_t
ctrl_set_aq_mode(vpx_codec_alg_priv_t
*ctx
,
716 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
717 extra_cfg
.aq_mode
= CAST(VP9E_SET_AQ_MODE
, args
);
718 return update_extra_cfg(ctx
, &extra_cfg
);
721 static vpx_codec_err_t
ctrl_set_frame_periodic_boost(vpx_codec_alg_priv_t
*ctx
,
723 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
724 extra_cfg
.frame_periodic_boost
= CAST(VP9E_SET_FRAME_PERIODIC_BOOST
, args
);
725 return update_extra_cfg(ctx
, &extra_cfg
);
728 static vpx_codec_err_t
encoder_init(vpx_codec_ctx_t
*ctx
,
729 vpx_codec_priv_enc_mr_cfg_t
*data
) {
730 vpx_codec_err_t res
= VPX_CODEC_OK
;
733 if (ctx
->priv
== NULL
) {
734 vpx_codec_alg_priv_t
*const priv
= vpx_calloc(1, sizeof(*priv
));
736 return VPX_CODEC_MEM_ERROR
;
738 ctx
->priv
= (vpx_codec_priv_t
*)priv
;
739 ctx
->priv
->init_flags
= ctx
->init_flags
;
740 ctx
->priv
->enc
.total_encoders
= 1;
742 (BufferPool
*)vpx_calloc(1, sizeof(BufferPool
));
743 if (priv
->buffer_pool
== NULL
)
744 return VPX_CODEC_MEM_ERROR
;
746 #if CONFIG_MULTITHREAD
747 if (pthread_mutex_init(&priv
->buffer_pool
->pool_mutex
, NULL
)) {
748 return VPX_CODEC_MEM_ERROR
;
752 if (ctx
->config
.enc
) {
753 // Update the reference to the config structure to an internal copy.
754 priv
->cfg
= *ctx
->config
.enc
;
755 ctx
->config
.enc
= &priv
->cfg
;
758 priv
->extra_cfg
= default_extra_cfg
;
759 once(vp9_initialize_enc
);
761 res
= validate_config(priv
, &priv
->cfg
, &priv
->extra_cfg
);
763 if (res
== VPX_CODEC_OK
) {
764 set_encoder_config(&priv
->oxcf
, &priv
->cfg
, &priv
->extra_cfg
);
765 #if CONFIG_VP9_HIGHBITDEPTH
766 priv
->oxcf
.use_highbitdepth
=
767 (ctx
->init_flags
& VPX_CODEC_USE_HIGHBITDEPTH
) ? 1 : 0;
769 priv
->cpi
= vp9_create_compressor(&priv
->oxcf
, priv
->buffer_pool
);
770 if (priv
->cpi
== NULL
)
771 res
= VPX_CODEC_MEM_ERROR
;
773 priv
->cpi
->output_pkt_list
= &priv
->pkt_list
.head
;
780 static vpx_codec_err_t
encoder_destroy(vpx_codec_alg_priv_t
*ctx
) {
782 vp9_remove_compressor(ctx
->cpi
);
783 #if CONFIG_MULTITHREAD
784 pthread_mutex_destroy(&ctx
->buffer_pool
->pool_mutex
);
786 vpx_free(ctx
->buffer_pool
);
791 static void pick_quickcompress_mode(vpx_codec_alg_priv_t
*ctx
,
792 unsigned long duration
,
793 unsigned long deadline
) {
794 MODE new_mode
= BEST
;
796 switch (ctx
->cfg
.g_pass
) {
797 case VPX_RC_ONE_PASS
:
799 const vpx_codec_enc_cfg_t
*const cfg
= &ctx
->cfg
;
801 // Convert duration parameter from stream timebase to microseconds.
802 const uint64_t duration_us
= (uint64_t)duration
* 1000000 *
803 (uint64_t)cfg
->g_timebase
.num
/(uint64_t)cfg
->g_timebase
.den
;
805 // If the deadline is more that the duration this frame is to be shown,
806 // use good quality mode. Otherwise use realtime mode.
807 new_mode
= (deadline
> duration_us
) ? GOOD
: REALTIME
;
812 case VPX_RC_FIRST_PASS
:
814 case VPX_RC_LAST_PASS
:
815 new_mode
= deadline
> 0 ? GOOD
: BEST
;
819 if (ctx
->oxcf
.mode
!= new_mode
) {
820 ctx
->oxcf
.mode
= new_mode
;
821 vp9_change_config(ctx
->cpi
, &ctx
->oxcf
);
825 // Turn on to test if supplemental superframe data breaks decoding
826 // #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
827 static int write_superframe_index(vpx_codec_alg_priv_t
*ctx
) {
828 uint8_t marker
= 0xc0;
832 assert(ctx
->pending_frame_count
);
833 assert(ctx
->pending_frame_count
<= 8);
835 // Add the number of frames to the marker byte
836 marker
|= ctx
->pending_frame_count
- 1;
838 // Choose the magnitude
839 for (mag
= 0, mask
= 0xff; mag
< 4; mag
++) {
840 if (ctx
->pending_frame_magnitude
< mask
)
848 index_sz
= 2 + (mag
+ 1) * ctx
->pending_frame_count
;
849 if (ctx
->pending_cx_data_sz
+ index_sz
< ctx
->cx_data_sz
) {
850 uint8_t *x
= ctx
->pending_cx_data
+ ctx
->pending_cx_data_sz
;
852 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
853 uint8_t marker_test
= 0xc0;
854 int mag_test
= 2; // 1 - 4
855 int frames_test
= 4; // 1 - 8
856 int index_sz_test
= 2 + mag_test
* frames_test
;
857 marker_test
|= frames_test
- 1;
858 marker_test
|= (mag_test
- 1) << 3;
860 for (i
= 0; i
< mag_test
* frames_test
; ++i
)
861 *x
++ = 0; // fill up with arbitrary data
863 ctx
->pending_cx_data_sz
+= index_sz_test
;
864 printf("Added supplemental superframe data\n");
868 for (i
= 0; i
< ctx
->pending_frame_count
; i
++) {
869 unsigned int this_sz
= (unsigned int)ctx
->pending_frame_sizes
[i
];
871 for (j
= 0; j
<= mag
; j
++) {
872 *x
++ = this_sz
& 0xff;
877 ctx
->pending_cx_data_sz
+= index_sz
;
878 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
879 index_sz
+= index_sz_test
;
885 // vp9 uses 10,000,000 ticks/second as time stamp
886 #define TICKS_PER_SEC 10000000LL
888 static int64_t timebase_units_to_ticks(const vpx_rational_t
*timebase
,
890 return n
* TICKS_PER_SEC
* timebase
->num
/ timebase
->den
;
893 static int64_t ticks_to_timebase_units(const vpx_rational_t
*timebase
,
895 const int64_t round
= TICKS_PER_SEC
* timebase
->num
/ 2 - 1;
896 return (n
* timebase
->den
+ round
) / timebase
->num
/ TICKS_PER_SEC
;
899 static vpx_codec_frame_flags_t
get_frame_pkt_flags(const VP9_COMP
*cpi
,
900 unsigned int lib_flags
) {
901 vpx_codec_frame_flags_t flags
= lib_flags
<< 16;
903 if (lib_flags
& FRAMEFLAGS_KEY
904 #if CONFIG_SPATIAL_SVC
905 || (is_two_pass_svc(cpi
) && cpi
->svc
.layer_context
[0].is_key_frame
)
908 flags
|= VPX_FRAME_IS_KEY
;
911 flags
|= VPX_FRAME_IS_DROPPABLE
;
916 static vpx_codec_err_t
encoder_encode(vpx_codec_alg_priv_t
*ctx
,
917 const vpx_image_t
*img
,
919 unsigned long duration
,
920 vpx_enc_frame_flags_t flags
,
921 unsigned long deadline
) {
922 vpx_codec_err_t res
= VPX_CODEC_OK
;
923 VP9_COMP
*const cpi
= ctx
->cpi
;
924 const vpx_rational_t
*const timebase
= &ctx
->cfg
.g_timebase
;
927 res
= validate_img(ctx
, img
);
928 // TODO(jzern) the checks related to cpi's validity should be treated as a
929 // failure condition, encoder setup is done fully in init() currently.
930 if (res
== VPX_CODEC_OK
&& cpi
!= NULL
&& ctx
->cx_data
== NULL
) {
931 // There's no codec control for multiple alt-refs so check the encoder
932 // instance for its status to determine the compressed data size.
933 ctx
->cx_data_sz
= ctx
->cfg
.g_w
* ctx
->cfg
.g_h
*
934 get_image_bps(img
) / 8 *
935 (cpi
->multi_arf_allowed
? 8 : 2);
936 if (ctx
->cx_data_sz
< 4096) ctx
->cx_data_sz
= 4096;
938 ctx
->cx_data
= (unsigned char *)malloc(ctx
->cx_data_sz
);
939 if (ctx
->cx_data
== NULL
) {
940 return VPX_CODEC_MEM_ERROR
;
945 pick_quickcompress_mode(ctx
, duration
, deadline
);
946 vpx_codec_pkt_list_init(&ctx
->pkt_list
);
949 if (((flags
& VP8_EFLAG_NO_UPD_GF
) && (flags
& VP8_EFLAG_FORCE_GF
)) ||
950 ((flags
& VP8_EFLAG_NO_UPD_ARF
) && (flags
& VP8_EFLAG_FORCE_ARF
))) {
951 ctx
->base
.err_detail
= "Conflicting flags.";
952 return VPX_CODEC_INVALID_PARAM
;
955 vp9_apply_encoding_flags(cpi
, flags
);
957 // Handle fixed keyframe intervals
958 if (ctx
->cfg
.kf_mode
== VPX_KF_AUTO
&&
959 ctx
->cfg
.kf_min_dist
== ctx
->cfg
.kf_max_dist
) {
960 if (++ctx
->fixed_kf_cntr
> ctx
->cfg
.kf_min_dist
) {
961 flags
|= VPX_EFLAG_FORCE_KF
;
962 ctx
->fixed_kf_cntr
= 1;
966 // Initialize the encoder instance on the first frame.
967 if (res
== VPX_CODEC_OK
&& cpi
!= NULL
) {
968 unsigned int lib_flags
= 0;
969 YV12_BUFFER_CONFIG sd
;
970 int64_t dst_time_stamp
= timebase_units_to_ticks(timebase
, pts
);
971 int64_t dst_end_time_stamp
=
972 timebase_units_to_ticks(timebase
, pts
+ duration
);
973 size_t size
, cx_data_sz
;
974 unsigned char *cx_data
;
976 // Set up internal flags
977 if (ctx
->base
.init_flags
& VPX_CODEC_USE_PSNR
)
978 cpi
->b_calculate_psnr
= 1;
981 res
= image2yuvconfig(img
, &sd
);
983 // Store the original flags in to the frame buffer. Will extract the
984 // key frame flag when we actually encode this frame.
985 if (vp9_receive_raw_frame(cpi
, flags
| ctx
->next_frame_flags
,
986 &sd
, dst_time_stamp
, dst_end_time_stamp
)) {
987 res
= update_error_state(ctx
, &cpi
->common
.error
);
989 ctx
->next_frame_flags
= 0;
992 cx_data
= ctx
->cx_data
;
993 cx_data_sz
= ctx
->cx_data_sz
;
995 /* Any pending invisible frames? */
996 if (ctx
->pending_cx_data
) {
997 memmove(cx_data
, ctx
->pending_cx_data
, ctx
->pending_cx_data_sz
);
998 ctx
->pending_cx_data
= cx_data
;
999 cx_data
+= ctx
->pending_cx_data_sz
;
1000 cx_data_sz
-= ctx
->pending_cx_data_sz
;
1002 /* TODO: this is a minimal check, the underlying codec doesn't respect
1003 * the buffer size anyway.
1005 if (cx_data_sz
< ctx
->cx_data_sz
/ 2) {
1006 ctx
->base
.err_detail
= "Compressed data buffer too small";
1007 return VPX_CODEC_ERROR
;
1011 while (cx_data_sz
>= ctx
->cx_data_sz
/ 2 &&
1012 -1 != vp9_get_compressed_data(cpi
, &lib_flags
, &size
,
1013 cx_data
, &dst_time_stamp
,
1014 &dst_end_time_stamp
, !img
)) {
1016 vpx_codec_cx_pkt_t pkt
;
1018 #if CONFIG_SPATIAL_SVC
1019 if (is_two_pass_svc(cpi
))
1020 cpi
->svc
.layer_context
[cpi
->svc
.spatial_layer_id
].layer_size
+= size
;
1023 // Pack invisible frames with the next visible frame
1024 if (!cpi
->common
.show_frame
1025 #if CONFIG_SPATIAL_SVC
1026 || (is_two_pass_svc(cpi
) &&
1027 cpi
->svc
.spatial_layer_id
< cpi
->svc
.number_spatial_layers
- 1)
1030 if (ctx
->pending_cx_data
== 0)
1031 ctx
->pending_cx_data
= cx_data
;
1032 ctx
->pending_cx_data_sz
+= size
;
1033 ctx
->pending_frame_sizes
[ctx
->pending_frame_count
++] = size
;
1034 ctx
->pending_frame_magnitude
|= size
;
1038 if (ctx
->output_cx_pkt_cb
.output_cx_pkt
) {
1039 pkt
.kind
= VPX_CODEC_CX_FRAME_PKT
;
1040 pkt
.data
.frame
.pts
= ticks_to_timebase_units(timebase
,
1042 pkt
.data
.frame
.duration
=
1043 (unsigned long)ticks_to_timebase_units(timebase
,
1044 dst_end_time_stamp
- dst_time_stamp
);
1045 pkt
.data
.frame
.flags
= get_frame_pkt_flags(cpi
, lib_flags
);
1046 pkt
.data
.frame
.buf
= ctx
->pending_cx_data
;
1047 pkt
.data
.frame
.sz
= size
;
1048 ctx
->pending_cx_data
= NULL
;
1049 ctx
->pending_cx_data_sz
= 0;
1050 ctx
->pending_frame_count
= 0;
1051 ctx
->pending_frame_magnitude
= 0;
1052 ctx
->output_cx_pkt_cb
.output_cx_pkt(
1053 &pkt
, ctx
->output_cx_pkt_cb
.user_priv
);
1058 // Add the frame packet to the list of returned packets.
1059 pkt
.kind
= VPX_CODEC_CX_FRAME_PKT
;
1060 pkt
.data
.frame
.pts
= ticks_to_timebase_units(timebase
, dst_time_stamp
);
1061 pkt
.data
.frame
.duration
=
1062 (unsigned long)ticks_to_timebase_units(timebase
,
1063 dst_end_time_stamp
- dst_time_stamp
);
1064 pkt
.data
.frame
.flags
= get_frame_pkt_flags(cpi
, lib_flags
);
1066 if (ctx
->pending_cx_data
) {
1067 ctx
->pending_frame_sizes
[ctx
->pending_frame_count
++] = size
;
1068 ctx
->pending_frame_magnitude
|= size
;
1069 ctx
->pending_cx_data_sz
+= size
;
1070 // write the superframe only for the case when
1071 if (!ctx
->output_cx_pkt_cb
.output_cx_pkt
)
1072 size
+= write_superframe_index(ctx
);
1073 pkt
.data
.frame
.buf
= ctx
->pending_cx_data
;
1074 pkt
.data
.frame
.sz
= ctx
->pending_cx_data_sz
;
1075 ctx
->pending_cx_data
= NULL
;
1076 ctx
->pending_cx_data_sz
= 0;
1077 ctx
->pending_frame_count
= 0;
1078 ctx
->pending_frame_magnitude
= 0;
1080 pkt
.data
.frame
.buf
= cx_data
;
1081 pkt
.data
.frame
.sz
= size
;
1083 pkt
.data
.frame
.partition_id
= -1;
1085 if(ctx
->output_cx_pkt_cb
.output_cx_pkt
)
1086 ctx
->output_cx_pkt_cb
.output_cx_pkt(&pkt
, ctx
->output_cx_pkt_cb
.user_priv
);
1088 vpx_codec_pkt_list_add(&ctx
->pkt_list
.head
, &pkt
);
1092 #if CONFIG_SPATIAL_SVC
1093 if (is_two_pass_svc(cpi
) && !ctx
->output_cx_pkt_cb
.output_cx_pkt
) {
1094 vpx_codec_cx_pkt_t pkt_sizes
, pkt_psnr
;
1096 vp9_zero(pkt_sizes
);
1098 pkt_sizes
.kind
= VPX_CODEC_SPATIAL_SVC_LAYER_SIZES
;
1099 pkt_psnr
.kind
= VPX_CODEC_SPATIAL_SVC_LAYER_PSNR
;
1100 for (i
= 0; i
< cpi
->svc
.number_spatial_layers
; ++i
) {
1101 LAYER_CONTEXT
*lc
= &cpi
->svc
.layer_context
[i
];
1102 pkt_sizes
.data
.layer_sizes
[i
] = lc
->layer_size
;
1103 pkt_psnr
.data
.layer_psnr
[i
] = lc
->psnr_pkt
;
1107 vpx_codec_pkt_list_add(&ctx
->pkt_list
.head
, &pkt_sizes
);
1109 vpx_codec_pkt_list_add(&ctx
->pkt_list
.head
, &pkt_psnr
);
1119 static const vpx_codec_cx_pkt_t
*encoder_get_cxdata(vpx_codec_alg_priv_t
*ctx
,
1120 vpx_codec_iter_t
*iter
) {
1121 return vpx_codec_pkt_list_get(&ctx
->pkt_list
.head
, iter
);
1124 static vpx_codec_err_t
ctrl_set_reference(vpx_codec_alg_priv_t
*ctx
,
1126 vpx_ref_frame_t
*const frame
= va_arg(args
, vpx_ref_frame_t
*);
1128 if (frame
!= NULL
) {
1129 YV12_BUFFER_CONFIG sd
;
1131 image2yuvconfig(&frame
->img
, &sd
);
1132 vp9_set_reference_enc(ctx
->cpi
, ref_frame_to_vp9_reframe(frame
->frame_type
),
1134 return VPX_CODEC_OK
;
1136 return VPX_CODEC_INVALID_PARAM
;
1140 static vpx_codec_err_t
ctrl_copy_reference(vpx_codec_alg_priv_t
*ctx
,
1142 vpx_ref_frame_t
*const frame
= va_arg(args
, vpx_ref_frame_t
*);
1144 if (frame
!= NULL
) {
1145 YV12_BUFFER_CONFIG sd
;
1147 image2yuvconfig(&frame
->img
, &sd
);
1148 vp9_copy_reference_enc(ctx
->cpi
,
1149 ref_frame_to_vp9_reframe(frame
->frame_type
), &sd
);
1150 return VPX_CODEC_OK
;
1152 return VPX_CODEC_INVALID_PARAM
;
1156 static vpx_codec_err_t
ctrl_get_reference(vpx_codec_alg_priv_t
*ctx
,
1158 vp9_ref_frame_t
*const frame
= va_arg(args
, vp9_ref_frame_t
*);
1160 if (frame
!= NULL
) {
1161 YV12_BUFFER_CONFIG
*fb
= get_ref_frame(&ctx
->cpi
->common
, frame
->idx
);
1162 if (fb
== NULL
) return VPX_CODEC_ERROR
;
1164 yuvconfig2image(&frame
->img
, fb
, NULL
);
1165 return VPX_CODEC_OK
;
1167 return VPX_CODEC_INVALID_PARAM
;
1171 static vpx_codec_err_t
ctrl_set_previewpp(vpx_codec_alg_priv_t
*ctx
,
1173 #if CONFIG_VP9_POSTPROC
1174 vp8_postproc_cfg_t
*config
= va_arg(args
, vp8_postproc_cfg_t
*);
1175 if (config
!= NULL
) {
1176 ctx
->preview_ppcfg
= *config
;
1177 return VPX_CODEC_OK
;
1179 return VPX_CODEC_INVALID_PARAM
;
1184 return VPX_CODEC_INCAPABLE
;
1189 static vpx_image_t
*encoder_get_preview(vpx_codec_alg_priv_t
*ctx
) {
1190 YV12_BUFFER_CONFIG sd
;
1191 vp9_ppflags_t flags
;
1194 if (ctx
->preview_ppcfg
.post_proc_flag
) {
1195 flags
.post_proc_flag
= ctx
->preview_ppcfg
.post_proc_flag
;
1196 flags
.deblocking_level
= ctx
->preview_ppcfg
.deblocking_level
;
1197 flags
.noise_level
= ctx
->preview_ppcfg
.noise_level
;
1200 if (vp9_get_preview_raw_frame(ctx
->cpi
, &sd
, &flags
) == 0) {
1201 yuvconfig2image(&ctx
->preview_img
, &sd
, NULL
);
1202 return &ctx
->preview_img
;
1208 static vpx_codec_err_t
ctrl_update_entropy(vpx_codec_alg_priv_t
*ctx
,
1210 const int update
= va_arg(args
, int);
1212 vp9_update_entropy(ctx
->cpi
, update
);
1213 return VPX_CODEC_OK
;
1216 static vpx_codec_err_t
ctrl_update_reference(vpx_codec_alg_priv_t
*ctx
,
1218 const int ref_frame_flags
= va_arg(args
, int);
1220 vp9_update_reference(ctx
->cpi
, ref_frame_flags
);
1221 return VPX_CODEC_OK
;
1224 static vpx_codec_err_t
ctrl_use_reference(vpx_codec_alg_priv_t
*ctx
,
1226 const int reference_flag
= va_arg(args
, int);
1228 vp9_use_as_reference(ctx
->cpi
, reference_flag
);
1229 return VPX_CODEC_OK
;
1232 static vpx_codec_err_t
ctrl_set_roi_map(vpx_codec_alg_priv_t
*ctx
,
1237 // TODO(yaowu): Need to re-implement and test for VP9.
1238 return VPX_CODEC_INVALID_PARAM
;
1242 static vpx_codec_err_t
ctrl_set_active_map(vpx_codec_alg_priv_t
*ctx
,
1244 vpx_active_map_t
*const map
= va_arg(args
, vpx_active_map_t
*);
1247 if (!vp9_set_active_map(ctx
->cpi
, map
->active_map
,
1248 (int)map
->rows
, (int)map
->cols
))
1249 return VPX_CODEC_OK
;
1251 return VPX_CODEC_INVALID_PARAM
;
1253 return VPX_CODEC_INVALID_PARAM
;
1257 static vpx_codec_err_t
ctrl_set_scale_mode(vpx_codec_alg_priv_t
*ctx
,
1259 vpx_scaling_mode_t
*const mode
= va_arg(args
, vpx_scaling_mode_t
*);
1262 const int res
= vp9_set_internal_size(ctx
->cpi
,
1263 (VPX_SCALING
)mode
->h_scaling_mode
,
1264 (VPX_SCALING
)mode
->v_scaling_mode
);
1265 return (res
== 0) ? VPX_CODEC_OK
: VPX_CODEC_INVALID_PARAM
;
1267 return VPX_CODEC_INVALID_PARAM
;
1271 static vpx_codec_err_t
ctrl_set_svc(vpx_codec_alg_priv_t
*ctx
, va_list args
) {
1272 int data
= va_arg(args
, int);
1273 const vpx_codec_enc_cfg_t
*cfg
= &ctx
->cfg
;
1275 vp9_set_svc(ctx
->cpi
, data
);
1276 // CBR or two pass mode for SVC with both temporal and spatial layers
1277 // not yet supported.
1279 (cfg
->rc_end_usage
== VPX_CBR
||
1280 cfg
->g_pass
== VPX_RC_FIRST_PASS
||
1281 cfg
->g_pass
== VPX_RC_LAST_PASS
) &&
1282 cfg
->ss_number_layers
> 1 &&
1283 cfg
->ts_number_layers
> 1) {
1284 return VPX_CODEC_INVALID_PARAM
;
1286 return VPX_CODEC_OK
;
1289 static vpx_codec_err_t
ctrl_set_svc_layer_id(vpx_codec_alg_priv_t
*ctx
,
1291 vpx_svc_layer_id_t
*const data
= va_arg(args
, vpx_svc_layer_id_t
*);
1292 VP9_COMP
*const cpi
= (VP9_COMP
*)ctx
->cpi
;
1293 SVC
*const svc
= &cpi
->svc
;
1295 svc
->spatial_layer_id
= data
->spatial_layer_id
;
1296 svc
->temporal_layer_id
= data
->temporal_layer_id
;
1297 // Checks on valid layer_id input.
1298 if (svc
->temporal_layer_id
< 0 ||
1299 svc
->temporal_layer_id
>= (int)ctx
->cfg
.ts_number_layers
) {
1300 return VPX_CODEC_INVALID_PARAM
;
1302 if (svc
->spatial_layer_id
< 0 ||
1303 svc
->spatial_layer_id
>= (int)ctx
->cfg
.ss_number_layers
) {
1304 return VPX_CODEC_INVALID_PARAM
;
1306 return VPX_CODEC_OK
;
1309 static vpx_codec_err_t
ctrl_get_svc_layer_id(vpx_codec_alg_priv_t
*ctx
,
1311 vpx_svc_layer_id_t
*data
= va_arg(args
, vpx_svc_layer_id_t
*);
1312 VP9_COMP
*const cpi
= (VP9_COMP
*)ctx
->cpi
;
1313 SVC
*const svc
= &cpi
->svc
;
1315 data
->spatial_layer_id
= svc
->spatial_layer_id
;
1316 data
->temporal_layer_id
= svc
->temporal_layer_id
;
1318 return VPX_CODEC_OK
;
1321 static vpx_codec_err_t
ctrl_set_svc_parameters(vpx_codec_alg_priv_t
*ctx
,
1323 VP9_COMP
*const cpi
= ctx
->cpi
;
1324 vpx_svc_extra_cfg_t
*const params
= va_arg(args
, vpx_svc_extra_cfg_t
*);
1327 for (i
= 0; i
< cpi
->svc
.number_spatial_layers
; ++i
) {
1328 LAYER_CONTEXT
*lc
= &cpi
->svc
.layer_context
[i
];
1330 lc
->max_q
= params
->max_quantizers
[i
];
1331 lc
->min_q
= params
->min_quantizers
[i
];
1332 lc
->scaling_factor_num
= params
->scaling_factor_num
[i
];
1333 lc
->scaling_factor_den
= params
->scaling_factor_den
[i
];
1336 return VPX_CODEC_OK
;
1339 static vpx_codec_err_t
ctrl_register_cx_callback(vpx_codec_alg_priv_t
*ctx
,
1341 vpx_codec_priv_output_cx_pkt_cb_pair_t
*cbp
=
1342 (vpx_codec_priv_output_cx_pkt_cb_pair_t
*)va_arg(args
, void *);
1343 ctx
->output_cx_pkt_cb
.output_cx_pkt
= cbp
->output_cx_pkt
;
1344 ctx
->output_cx_pkt_cb
.user_priv
= cbp
->user_priv
;
1346 return VPX_CODEC_OK
;
1349 static vpx_codec_err_t
ctrl_set_tune_content(vpx_codec_alg_priv_t
*ctx
,
1351 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
1352 extra_cfg
.content
= CAST(VP9E_SET_TUNE_CONTENT
, args
);
1353 return update_extra_cfg(ctx
, &extra_cfg
);
1356 static vpx_codec_err_t
ctrl_set_color_space(vpx_codec_alg_priv_t
*ctx
,
1358 struct vp9_extracfg extra_cfg
= ctx
->extra_cfg
;
1359 extra_cfg
.color_space
= CAST(VP9E_SET_COLOR_SPACE
, args
);
1360 return update_extra_cfg(ctx
, &extra_cfg
);
1363 static vpx_codec_ctrl_fn_map_t encoder_ctrl_maps
[] = {
1364 {VP8_COPY_REFERENCE
, ctrl_copy_reference
},
1365 {VP8E_UPD_ENTROPY
, ctrl_update_entropy
},
1366 {VP8E_UPD_REFERENCE
, ctrl_update_reference
},
1367 {VP8E_USE_REFERENCE
, ctrl_use_reference
},
1370 {VP8_SET_REFERENCE
, ctrl_set_reference
},
1371 {VP8_SET_POSTPROC
, ctrl_set_previewpp
},
1372 {VP8E_SET_ROI_MAP
, ctrl_set_roi_map
},
1373 {VP8E_SET_ACTIVEMAP
, ctrl_set_active_map
},
1374 {VP8E_SET_SCALEMODE
, ctrl_set_scale_mode
},
1375 {VP8E_SET_CPUUSED
, ctrl_set_cpuused
},
1376 {VP8E_SET_ENABLEAUTOALTREF
, ctrl_set_enable_auto_alt_ref
},
1377 {VP8E_SET_SHARPNESS
, ctrl_set_sharpness
},
1378 {VP8E_SET_STATIC_THRESHOLD
, ctrl_set_static_thresh
},
1379 {VP9E_SET_TILE_COLUMNS
, ctrl_set_tile_columns
},
1380 {VP9E_SET_TILE_ROWS
, ctrl_set_tile_rows
},
1381 {VP8E_SET_ARNR_MAXFRAMES
, ctrl_set_arnr_max_frames
},
1382 {VP8E_SET_ARNR_STRENGTH
, ctrl_set_arnr_strength
},
1383 {VP8E_SET_ARNR_TYPE
, ctrl_set_arnr_type
},
1384 {VP8E_SET_TUNING
, ctrl_set_tuning
},
1385 {VP8E_SET_CQ_LEVEL
, ctrl_set_cq_level
},
1386 {VP8E_SET_MAX_INTRA_BITRATE_PCT
, ctrl_set_rc_max_intra_bitrate_pct
},
1387 {VP8E_SET_MAX_INTER_BITRATE_PCT
, ctrl_set_rc_max_inter_bitrate_pct
},
1388 {VP8E_SET_GF_CBR_BOOST_PCT
, ctrl_set_rc_gf_cbr_boost_pct
},
1389 {VP9E_SET_LOSSLESS
, ctrl_set_lossless
},
1390 {VP9E_SET_FRAME_PARALLEL_DECODING
, ctrl_set_frame_parallel_decoding_mode
},
1391 {VP9E_SET_AQ_MODE
, ctrl_set_aq_mode
},
1392 {VP9E_SET_FRAME_PERIODIC_BOOST
, ctrl_set_frame_periodic_boost
},
1393 {VP9E_SET_SVC
, ctrl_set_svc
},
1394 {VP9E_SET_SVC_PARAMETERS
, ctrl_set_svc_parameters
},
1395 {VP9E_REGISTER_CX_CALLBACK
, ctrl_register_cx_callback
},
1396 {VP9E_SET_SVC_LAYER_ID
, ctrl_set_svc_layer_id
},
1397 {VP9E_SET_TUNE_CONTENT
, ctrl_set_tune_content
},
1398 {VP9E_SET_COLOR_SPACE
, ctrl_set_color_space
},
1399 {VP9E_SET_NOISE_SENSITIVITY
, ctrl_set_noise_sensitivity
},
1402 {VP8E_GET_LAST_QUANTIZER
, ctrl_get_quantizer
},
1403 {VP8E_GET_LAST_QUANTIZER_64
, ctrl_get_quantizer64
},
1404 {VP9_GET_REFERENCE
, ctrl_get_reference
},
1405 {VP9E_GET_SVC_LAYER_ID
, ctrl_get_svc_layer_id
},
1410 static vpx_codec_enc_cfg_map_t encoder_usage_cfg_map
[] = {
1420 VPX_BITS_8
, // g_bit_depth
1421 8, // g_input_bit_depth
1423 {1, 30}, // g_timebase
1425 0, // g_error_resilient
1427 VPX_RC_ONE_PASS
, // g_pass
1429 25, // g_lag_in_frames
1431 0, // rc_dropframe_thresh
1432 0, // rc_resize_allowed
1433 0, // rc_scaled_width
1434 0, // rc_scaled_height
1435 60, // rc_resize_down_thresold
1436 30, // rc_resize_up_thresold
1438 VPX_VBR
, // rc_end_usage
1439 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1440 {NULL
, 0}, // rc_twopass_stats_in
1441 {NULL
, 0}, // rc_firstpass_mb_stats_in
1443 256, // rc_target_bandwidth
1444 0, // rc_min_quantizer
1445 63, // rc_max_quantizer
1446 100, // rc_undershoot_pct
1447 100, // rc_overshoot_pct
1449 6000, // rc_max_buffer_size
1450 4000, // rc_buffer_initial_size
1451 5000, // rc_buffer_optimal_size
1453 50, // rc_two_pass_vbrbias
1454 0, // rc_two_pass_vbrmin_section
1455 2000, // rc_two_pass_vbrmax_section
1457 // keyframing settings (kf)
1458 VPX_KF_AUTO
, // g_kfmode
1460 9999, // kf_max_dist
1462 VPX_SS_DEFAULT_LAYERS
, // ss_number_layers
1464 {0}, // ss_target_bitrate
1465 1, // ts_number_layers
1466 {0}, // ts_target_bitrate
1467 {0}, // ts_rate_decimator
1468 0, // ts_periodicity
1470 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
1471 "vp8.fpf" // first pass filename
1477 #ifndef VERSION_STRING
1478 #define VERSION_STRING
1480 CODEC_INTERFACE(vpx_codec_vp9_cx
) = {
1481 "WebM Project VP9 Encoder" VERSION_STRING
,
1482 VPX_CODEC_INTERNAL_ABI_VERSION
,
1483 #if CONFIG_VP9_HIGHBITDEPTH
1484 VPX_CODEC_CAP_HIGHBITDEPTH
|
1486 VPX_CODEC_CAP_ENCODER
| VPX_CODEC_CAP_PSNR
, // vpx_codec_caps_t
1487 encoder_init
, // vpx_codec_init_fn_t
1488 encoder_destroy
, // vpx_codec_destroy_fn_t
1489 encoder_ctrl_maps
, // vpx_codec_ctrl_fn_map_t
1491 NULL
, // vpx_codec_peek_si_fn_t
1492 NULL
, // vpx_codec_get_si_fn_t
1493 NULL
, // vpx_codec_decode_fn_t
1494 NULL
, // vpx_codec_frame_get_fn_t
1495 NULL
// vpx_codec_set_fb_fn_t
1499 encoder_usage_cfg_map
, // vpx_codec_enc_cfg_map_t
1500 encoder_encode
, // vpx_codec_encode_fn_t
1501 encoder_get_cxdata
, // vpx_codec_get_cx_data_fn_t
1502 encoder_set_config
, // vpx_codec_enc_config_set_fn_t
1503 NULL
, // vpx_codec_get_global_headers_fn_t
1504 encoder_get_preview
, // vpx_codec_get_preview_frame_fn_t
1505 NULL
// vpx_codec_enc_mr_get_mem_loc_fn_t