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.
12 #include "vpx/vpx_codec.h"
13 #include "vpx/internal/vpx_codec_internal.h"
14 #include "vpx_version.h"
15 #include "vp8/encoder/onyx_int.h"
17 #include "vp8/encoder/firstpass.h"
18 #include "vp8/common/onyx.h"
22 /* This value is a sentinel for determining whether the user has set a mode
23 * directly through the deprecated VP8E_SET_ENCODING_MODE control.
25 #define NO_MODE_SET 255
29 struct vpx_codec_pkt_list
*pkt_list
;
30 vp8e_encoding_mode encoding_mode
; /** best, good, realtime */
31 int cpu_used
; /** available cpu percentage in 1/16*/
32 unsigned int enable_auto_alt_ref
; /** if encoder decides to uses alternate reference frame */
33 unsigned int noise_sensitivity
;
34 unsigned int Sharpness
;
35 unsigned int static_thresh
;
36 unsigned int token_partitions
;
37 unsigned int arnr_max_frames
; /* alt_ref Noise Reduction Max Frame Count */
38 unsigned int arnr_strength
; /* alt_ref Noise Reduction Strength */
39 unsigned int arnr_type
; /* alt_ref filter type */
41 unsigned int cq_level
; /* constrained quality level */
45 struct extraconfig_map
48 struct vp8_extracfg cfg
;
51 static const struct extraconfig_map extracfg_map
[] =
57 #if !(CONFIG_REALTIME_ONLY)
58 VP8_BEST_QUALITY_ENCODING
, /* Encoding Mode */
61 VP8_REAL_TIME_ENCODING
, /* Encoding Mode */
64 0, /* enable_auto_alt_ref */
65 0, /* noise_sensitivity */
67 0, /* static_thresh */
68 VP8_ONE_TOKENPARTITION
, /* token_partitions */
69 0, /* arnr_max_frames */
70 3, /* arnr_strength */
78 struct vpx_codec_alg_priv
80 vpx_codec_priv_t base
;
81 vpx_codec_enc_cfg_t cfg
;
82 struct vp8_extracfg vp8_cfg
;
85 unsigned char *cx_data
;
86 unsigned int cx_data_sz
;
87 vpx_image_t preview_img
;
88 unsigned int next_frame_flag
;
89 vp8_postproc_cfg_t preview_ppcfg
;
90 vpx_codec_pkt_list_decl(64) pkt_list
; // changed to accomendate the maximum number of lagged frames allowed
92 unsigned int fixed_kf_cntr
;
96 static vpx_codec_err_t
97 update_error_state(vpx_codec_alg_priv_t
*ctx
,
98 const struct vpx_internal_error_info
*error
)
102 if ((res
= error
->error_code
))
103 ctx
->base
.err_detail
= error
->has_detail
112 #define ERROR(str) do {\
113 ctx->base.err_detail = str;\
114 return VPX_CODEC_INVALID_PARAM;\
117 #define RANGE_CHECK(p,memb,lo,hi) do {\
118 if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
119 ERROR(#memb " out of range ["#lo".."#hi"]");\
122 #define RANGE_CHECK_HI(p,memb,hi) do {\
123 if(!((p)->memb <= (hi))) \
124 ERROR(#memb " out of range [.."#hi"]");\
127 #define RANGE_CHECK_LO(p,memb,lo) do {\
128 if(!((p)->memb >= (lo))) \
129 ERROR(#memb " out of range ["#lo"..]");\
132 #define RANGE_CHECK_BOOL(p,memb) do {\
133 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
136 static vpx_codec_err_t
validate_config(vpx_codec_alg_priv_t
*ctx
,
137 const vpx_codec_enc_cfg_t
*cfg
,
138 const struct vp8_extracfg
*vp8_cfg
)
140 RANGE_CHECK(cfg
, g_w
, 1, 16384);
141 RANGE_CHECK(cfg
, g_h
, 1, 16384);
142 RANGE_CHECK(cfg
, g_timebase
.den
, 1, 1000000000);
143 RANGE_CHECK(cfg
, g_timebase
.num
, 1, cfg
->g_timebase
.den
);
144 RANGE_CHECK_HI(cfg
, g_profile
, 3);
145 RANGE_CHECK_HI(cfg
, rc_max_quantizer
, 63);
146 RANGE_CHECK_HI(cfg
, rc_min_quantizer
, cfg
->rc_max_quantizer
);
147 RANGE_CHECK_HI(cfg
, g_threads
, 64);
148 #if !(CONFIG_REALTIME_ONLY)
149 RANGE_CHECK_HI(cfg
, g_lag_in_frames
, 25);
151 RANGE_CHECK_HI(cfg
, g_lag_in_frames
, 0);
153 RANGE_CHECK(cfg
, rc_end_usage
, VPX_VBR
, VPX_CQ
);
154 RANGE_CHECK_HI(cfg
, rc_undershoot_pct
, 100);
155 RANGE_CHECK_HI(cfg
, rc_2pass_vbr_bias_pct
, 100);
156 RANGE_CHECK(cfg
, kf_mode
, VPX_KF_DISABLED
, VPX_KF_AUTO
);
157 //RANGE_CHECK_BOOL(cfg, g_delete_firstpassfile);
158 RANGE_CHECK_BOOL(cfg
, rc_resize_allowed
);
159 RANGE_CHECK_HI(cfg
, rc_dropframe_thresh
, 100);
160 RANGE_CHECK_HI(cfg
, rc_resize_up_thresh
, 100);
161 RANGE_CHECK_HI(cfg
, rc_resize_down_thresh
, 100);
162 #if !(CONFIG_REALTIME_ONLY)
163 RANGE_CHECK(cfg
, g_pass
, VPX_RC_ONE_PASS
, VPX_RC_LAST_PASS
);
165 RANGE_CHECK(cfg
, g_pass
, VPX_RC_ONE_PASS
, VPX_RC_ONE_PASS
);
168 /* VP8 does not support a lower bound on the keyframe interval in
169 * automatic keyframe placement mode.
171 if (cfg
->kf_mode
!= VPX_KF_DISABLED
&& cfg
->kf_min_dist
!= cfg
->kf_max_dist
172 && cfg
->kf_min_dist
> 0)
173 ERROR("kf_min_dist not supported in auto mode, use 0 "
174 "or kf_max_dist instead.");
176 RANGE_CHECK_BOOL(vp8_cfg
, enable_auto_alt_ref
);
177 #if !(CONFIG_REALTIME_ONLY)
178 RANGE_CHECK(vp8_cfg
, encoding_mode
, VP8_BEST_QUALITY_ENCODING
, VP8_REAL_TIME_ENCODING
);
179 RANGE_CHECK(vp8_cfg
, cpu_used
, -16, 16);
180 RANGE_CHECK_HI(vp8_cfg
, noise_sensitivity
, 6);
182 RANGE_CHECK(vp8_cfg
, encoding_mode
, VP8_REAL_TIME_ENCODING
, VP8_REAL_TIME_ENCODING
);
184 if (!((vp8_cfg
->cpu_used
>= -16 && vp8_cfg
->cpu_used
<= -4) || (vp8_cfg
->cpu_used
>= 4 && vp8_cfg
->cpu_used
<= 16)))
185 ERROR("cpu_used out of range [-16..-4] or [4..16]");
187 RANGE_CHECK(vp8_cfg
, noise_sensitivity
, 0, 0);
190 RANGE_CHECK(vp8_cfg
, token_partitions
, VP8_ONE_TOKENPARTITION
, VP8_EIGHT_TOKENPARTITION
);
191 RANGE_CHECK_HI(vp8_cfg
, Sharpness
, 7);
192 RANGE_CHECK(vp8_cfg
, arnr_max_frames
, 0, 15);
193 RANGE_CHECK_HI(vp8_cfg
, arnr_strength
, 6);
194 RANGE_CHECK(vp8_cfg
, arnr_type
, 1, 3);
195 RANGE_CHECK(vp8_cfg
, cq_level
, 0, 63);
197 #if !(CONFIG_REALTIME_ONLY)
198 if (cfg
->g_pass
== VPX_RC_LAST_PASS
)
200 int mb_r
= (cfg
->g_h
+ 15) / 16;
201 int mb_c
= (cfg
->g_w
+ 15) / 16;
202 size_t packet_sz
= sizeof(FIRSTPASS_STATS
);
203 int n_packets
= cfg
->rc_twopass_stats_in
.sz
/ packet_sz
;
204 FIRSTPASS_STATS
*stats
;
206 if (!cfg
->rc_twopass_stats_in
.buf
)
207 ERROR("rc_twopass_stats_in.buf not set.");
209 if (cfg
->rc_twopass_stats_in
.sz
% packet_sz
)
210 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
212 if (cfg
->rc_twopass_stats_in
.sz
< 2 * packet_sz
)
213 ERROR("rc_twopass_stats_in requires at least two packets.");
215 stats
= (void*)((char *)cfg
->rc_twopass_stats_in
.buf
216 + (n_packets
- 1) * packet_sz
);
218 if ((int)(stats
->count
+ 0.5) != n_packets
- 1)
219 ERROR("rc_twopass_stats_in missing EOS stats packet");
227 static vpx_codec_err_t
validate_img(vpx_codec_alg_priv_t
*ctx
,
228 const vpx_image_t
*img
)
232 case VPX_IMG_FMT_YV12
:
233 case VPX_IMG_FMT_I420
:
234 case VPX_IMG_FMT_VPXI420
:
235 case VPX_IMG_FMT_VPXYV12
:
238 ERROR("Invalid image format. Only YV12 and I420 images are supported");
241 if ((img
->d_w
!= ctx
->cfg
.g_w
) || (img
->d_h
!= ctx
->cfg
.g_h
))
242 ERROR("Image size must match encoder init configuration size");
248 static vpx_codec_err_t
set_vp8e_config(VP8_CONFIG
*oxcf
,
249 vpx_codec_enc_cfg_t cfg
,
250 struct vp8_extracfg vp8_cfg
)
252 oxcf
->multi_threaded
= cfg
.g_threads
;
253 oxcf
->Version
= cfg
.g_profile
;
255 oxcf
->Width
= cfg
.g_w
;
256 oxcf
->Height
= cfg
.g_h
;
257 /* guess a frame rate if out of whack, use 30 */
258 oxcf
->frame_rate
= (double)(cfg
.g_timebase
.den
) / (double)(cfg
.g_timebase
.num
);
260 if (oxcf
->frame_rate
> 180)
262 oxcf
->frame_rate
= 30;
265 oxcf
->error_resilient_mode
= cfg
.g_error_resilient
;
269 case VPX_RC_ONE_PASS
:
270 oxcf
->Mode
= MODE_BESTQUALITY
;
272 case VPX_RC_FIRST_PASS
:
273 oxcf
->Mode
= MODE_FIRSTPASS
;
275 case VPX_RC_LAST_PASS
:
276 oxcf
->Mode
= MODE_SECONDPASS_BEST
;
280 if (cfg
.g_pass
== VPX_RC_FIRST_PASS
)
283 oxcf
->lag_in_frames
= 0;
287 oxcf
->allow_lag
= (cfg
.g_lag_in_frames
) > 0;
288 oxcf
->lag_in_frames
= cfg
.g_lag_in_frames
;
291 oxcf
->allow_df
= (cfg
.rc_dropframe_thresh
> 0);
292 oxcf
->drop_frames_water_mark
= cfg
.rc_dropframe_thresh
;
294 oxcf
->allow_spatial_resampling
= cfg
.rc_resize_allowed
;
295 oxcf
->resample_up_water_mark
= cfg
.rc_resize_up_thresh
;
296 oxcf
->resample_down_water_mark
= cfg
.rc_resize_down_thresh
;
298 if (cfg
.rc_end_usage
== VPX_VBR
)
300 oxcf
->end_usage
= USAGE_LOCAL_FILE_PLAYBACK
;
302 else if (cfg
.rc_end_usage
== VPX_CBR
)
304 oxcf
->end_usage
= USAGE_STREAM_FROM_SERVER
;
306 else if (cfg
.rc_end_usage
== VPX_CQ
)
308 oxcf
->end_usage
= USAGE_CONSTRAINED_QUALITY
;
311 oxcf
->target_bandwidth
= cfg
.rc_target_bitrate
;
313 oxcf
->best_allowed_q
= cfg
.rc_min_quantizer
;
314 oxcf
->worst_allowed_q
= cfg
.rc_max_quantizer
;
315 oxcf
->cq_level
= vp8_cfg
.cq_level
;
318 oxcf
->under_shoot_pct
= cfg
.rc_undershoot_pct
;
319 //oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
321 oxcf
->maximum_buffer_size
= cfg
.rc_buf_sz
;
322 oxcf
->starting_buffer_level
= cfg
.rc_buf_initial_sz
;
323 oxcf
->optimal_buffer_level
= cfg
.rc_buf_optimal_sz
;
325 oxcf
->two_pass_vbrbias
= cfg
.rc_2pass_vbr_bias_pct
;
326 oxcf
->two_pass_vbrmin_section
= cfg
.rc_2pass_vbr_minsection_pct
;
327 oxcf
->two_pass_vbrmax_section
= cfg
.rc_2pass_vbr_maxsection_pct
;
329 oxcf
->auto_key
= cfg
.kf_mode
== VPX_KF_AUTO
330 && cfg
.kf_min_dist
!= cfg
.kf_max_dist
;
331 //oxcf->kf_min_dist = cfg.kf_min_dis;
332 oxcf
->key_freq
= cfg
.kf_max_dist
;
334 //oxcf->delete_first_pass_file = cfg.g_delete_firstpassfile;
335 //strcpy(oxcf->first_pass_file, cfg.g_firstpass_file);
337 oxcf
->cpu_used
= vp8_cfg
.cpu_used
;
338 oxcf
->encode_breakout
= vp8_cfg
.static_thresh
;
339 oxcf
->play_alternate
= vp8_cfg
.enable_auto_alt_ref
;
340 oxcf
->noise_sensitivity
= vp8_cfg
.noise_sensitivity
;
341 oxcf
->Sharpness
= vp8_cfg
.Sharpness
;
342 oxcf
->token_partitions
= vp8_cfg
.token_partitions
;
344 oxcf
->two_pass_stats_in
= cfg
.rc_twopass_stats_in
;
345 oxcf
->output_pkt_list
= vp8_cfg
.pkt_list
;
347 oxcf
->arnr_max_frames
= vp8_cfg
.arnr_max_frames
;
348 oxcf
->arnr_strength
= vp8_cfg
.arnr_strength
;
349 oxcf
->arnr_type
= vp8_cfg
.arnr_type
;
351 oxcf
->tuning
= vp8_cfg
.tuning
;
354 printf("Current VP8 Settings: \n");
355 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
356 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
357 printf("Sharpness: %d\n", oxcf->Sharpness);
358 printf("cpu_used: %d\n", oxcf->cpu_used);
359 printf("Mode: %d\n", oxcf->Mode);
360 printf("delete_first_pass_file: %d\n", oxcf->delete_first_pass_file);
361 printf("auto_key: %d\n", oxcf->auto_key);
362 printf("key_freq: %d\n", oxcf->key_freq);
363 printf("end_usage: %d\n", oxcf->end_usage);
364 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
365 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
366 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
367 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
368 printf("fixed_q: %d\n", oxcf->fixed_q);
369 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
370 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
371 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
372 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
373 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
374 printf("allow_df: %d\n", oxcf->allow_df);
375 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
376 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
377 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
378 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
379 printf("allow_lag: %d\n", oxcf->allow_lag);
380 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
381 printf("play_alternate: %d\n", oxcf->play_alternate);
382 printf("Version: %d\n", oxcf->Version);
383 printf("multi_threaded: %d\n", oxcf->multi_threaded);
384 printf("encode_breakout: %d\n", oxcf->encode_breakout);
389 static vpx_codec_err_t
vp8e_set_config(vpx_codec_alg_priv_t
*ctx
,
390 const vpx_codec_enc_cfg_t
*cfg
)
394 if ((cfg
->g_w
!= ctx
->cfg
.g_w
) || (cfg
->g_h
!= ctx
->cfg
.g_h
))
395 ERROR("Cannot change width or height after initialization");
397 /* Prevent increasing lag_in_frames. This check is stricter than it needs
398 * to be -- the limit is not increasing past the first lag_in_frames
399 * value, but we don't track the initial config, only the last successful
402 if ((cfg
->g_lag_in_frames
> ctx
->cfg
.g_lag_in_frames
))
403 ERROR("Cannot increase lag_in_frames");
405 res
= validate_config(ctx
, cfg
, &ctx
->vp8_cfg
);
410 set_vp8e_config(&ctx
->oxcf
, ctx
->cfg
, ctx
->vp8_cfg
);
411 vp8_change_config(ctx
->cpi
, &ctx
->oxcf
);
418 int vp8_reverse_trans(int);
421 static vpx_codec_err_t
get_param(vpx_codec_alg_priv_t
*ctx
,
425 void *arg
= va_arg(args
, void *);
427 #define MAP(id, var) case id: *(RECAST(id, arg)) = var; break
430 return VPX_CODEC_INVALID_PARAM
;
434 MAP(VP8E_GET_LAST_QUANTIZER
, vp8_get_quantizer(ctx
->cpi
));
435 MAP(VP8E_GET_LAST_QUANTIZER_64
, vp8_reverse_trans(vp8_get_quantizer(ctx
->cpi
)));
443 static vpx_codec_err_t
set_param(vpx_codec_alg_priv_t
*ctx
,
447 vpx_codec_err_t res
= VPX_CODEC_OK
;
448 struct vp8_extracfg xcfg
= ctx
->vp8_cfg
;
450 #define MAP(id, var) case id: var = CAST(id, args); break;
454 MAP(VP8E_SET_ENCODING_MODE
, ctx
->deprecated_mode
);
455 MAP(VP8E_SET_CPUUSED
, xcfg
.cpu_used
);
456 MAP(VP8E_SET_ENABLEAUTOALTREF
, xcfg
.enable_auto_alt_ref
);
457 MAP(VP8E_SET_NOISE_SENSITIVITY
, xcfg
.noise_sensitivity
);
458 MAP(VP8E_SET_SHARPNESS
, xcfg
.Sharpness
);
459 MAP(VP8E_SET_STATIC_THRESHOLD
, xcfg
.static_thresh
);
460 MAP(VP8E_SET_TOKEN_PARTITIONS
, xcfg
.token_partitions
);
462 MAP(VP8E_SET_ARNR_MAXFRAMES
, xcfg
.arnr_max_frames
);
463 MAP(VP8E_SET_ARNR_STRENGTH
, xcfg
.arnr_strength
);
464 MAP(VP8E_SET_ARNR_TYPE
, xcfg
.arnr_type
);
465 MAP(VP8E_SET_TUNING
, xcfg
.tuning
);
466 MAP(VP8E_SET_CQ_LEVEL
, xcfg
.cq_level
);
470 res
= validate_config(ctx
, &ctx
->cfg
, &xcfg
);
475 set_vp8e_config(&ctx
->oxcf
, ctx
->cfg
, ctx
->vp8_cfg
);
476 vp8_change_config(ctx
->cpi
, &ctx
->oxcf
);
482 static vpx_codec_err_t
vp8e_init(vpx_codec_ctx_t
*ctx
)
484 vpx_codec_err_t res
= VPX_DEC_OK
;
485 struct vpx_codec_alg_priv
*priv
;
486 vpx_codec_enc_cfg_t
*cfg
;
493 priv
= calloc(1, sizeof(struct vpx_codec_alg_priv
));
497 return VPX_CODEC_MEM_ERROR
;
500 ctx
->priv
= &priv
->base
;
501 ctx
->priv
->sz
= sizeof(*ctx
->priv
);
502 ctx
->priv
->iface
= ctx
->iface
;
503 ctx
->priv
->alg_priv
= priv
;
504 ctx
->priv
->init_flags
= ctx
->init_flags
;
508 /* Update the reference to the config structure to an
511 ctx
->priv
->alg_priv
->cfg
= *ctx
->config
.enc
;
512 ctx
->config
.enc
= &ctx
->priv
->alg_priv
->cfg
;
515 cfg
= &ctx
->priv
->alg_priv
->cfg
;
517 /* Select the extra vp6 configuration table based on the current
518 * usage value. If the current usage value isn't found, use the
519 * values for usage case 0.
522 extracfg_map
[i
].usage
&& extracfg_map
[i
].usage
!= cfg
->g_usage
;
525 priv
->vp8_cfg
= extracfg_map
[i
].cfg
;
526 priv
->vp8_cfg
.pkt_list
= &priv
->pkt_list
.head
;
528 priv
->cx_data_sz
= priv
->cfg
.g_w
* priv
->cfg
.g_h
* 3 / 2 * 2;
530 if (priv
->cx_data_sz
< 4096) priv
->cx_data_sz
= 4096;
532 priv
->cx_data
= malloc(priv
->cx_data_sz
);
536 return VPX_CODEC_MEM_ERROR
;
539 priv
->deprecated_mode
= NO_MODE_SET
;
543 res
= validate_config(priv
, &priv
->cfg
, &priv
->vp8_cfg
);
547 set_vp8e_config(&ctx
->priv
->alg_priv
->oxcf
,
548 ctx
->priv
->alg_priv
->cfg
,
549 ctx
->priv
->alg_priv
->vp8_cfg
);
550 optr
= vp8_create_compressor(&ctx
->priv
->alg_priv
->oxcf
);
553 res
= VPX_CODEC_MEM_ERROR
;
555 ctx
->priv
->alg_priv
->cpi
= optr
;
562 static vpx_codec_err_t
vp8e_destroy(vpx_codec_alg_priv_t
*ctx
)
566 vp8_remove_compressor(&ctx
->cpi
);
571 static vpx_codec_err_t
image2yuvconfig(const vpx_image_t
*img
,
572 YV12_BUFFER_CONFIG
*yv12
)
574 vpx_codec_err_t res
= VPX_CODEC_OK
;
575 yv12
->y_buffer
= img
->planes
[VPX_PLANE_Y
];
576 yv12
->u_buffer
= img
->planes
[VPX_PLANE_U
];
577 yv12
->v_buffer
= img
->planes
[VPX_PLANE_V
];
579 yv12
->y_width
= img
->d_w
;
580 yv12
->y_height
= img
->d_h
;
581 yv12
->uv_width
= (1 + yv12
->y_width
) / 2;
582 yv12
->uv_height
= (1 + yv12
->y_height
) / 2;
584 yv12
->y_stride
= img
->stride
[VPX_PLANE_Y
];
585 yv12
->uv_stride
= img
->stride
[VPX_PLANE_U
];
587 yv12
->border
= (img
->stride
[VPX_PLANE_Y
] - img
->w
) / 2;
588 yv12
->clrtype
= (img
->fmt
== VPX_IMG_FMT_VPXI420
|| img
->fmt
== VPX_IMG_FMT_VPXYV12
); //REG_YUV = 0
592 static void pick_quickcompress_mode(vpx_codec_alg_priv_t
*ctx
,
593 unsigned long duration
,
594 unsigned long deadline
)
598 #if !(CONFIG_REALTIME_ONLY)
599 /* Use best quality mode if no deadline is given. */
600 new_qc
= MODE_BESTQUALITY
;
604 uint64_t duration_us
;
606 /* Convert duration parameter from stream timebase to microseconds */
607 duration_us
= (uint64_t)duration
* 1000000
608 * (uint64_t)ctx
->cfg
.g_timebase
.num
609 / (uint64_t)ctx
->cfg
.g_timebase
.den
;
611 /* If the deadline is more that the duration this frame is to be shown,
612 * use good quality mode. Otherwise use realtime mode.
614 new_qc
= (deadline
> duration_us
) ? MODE_GOODQUALITY
: MODE_REALTIME
;
618 new_qc
= MODE_REALTIME
;
621 switch (ctx
->deprecated_mode
)
623 case VP8_BEST_QUALITY_ENCODING
:
624 new_qc
= MODE_BESTQUALITY
;
626 case VP8_GOOD_QUALITY_ENCODING
:
627 new_qc
= MODE_GOODQUALITY
;
629 case VP8_REAL_TIME_ENCODING
:
630 new_qc
= MODE_REALTIME
;
634 if (ctx
->cfg
.g_pass
== VPX_RC_FIRST_PASS
)
635 new_qc
= MODE_FIRSTPASS
;
636 else if (ctx
->cfg
.g_pass
== VPX_RC_LAST_PASS
)
637 new_qc
= (new_qc
== MODE_BESTQUALITY
)
638 ? MODE_SECONDPASS_BEST
641 if (ctx
->oxcf
.Mode
!= new_qc
)
643 ctx
->oxcf
.Mode
= new_qc
;
644 vp8_change_config(ctx
->cpi
, &ctx
->oxcf
);
649 static vpx_codec_err_t
vp8e_encode(vpx_codec_alg_priv_t
*ctx
,
650 const vpx_image_t
*img
,
652 unsigned long duration
,
653 vpx_enc_frame_flags_t flags
,
654 unsigned long deadline
)
656 vpx_codec_err_t res
= VPX_CODEC_OK
;
659 res
= validate_img(ctx
, img
);
661 pick_quickcompress_mode(ctx
, duration
, deadline
);
662 vpx_codec_pkt_list_init(&ctx
->pkt_list
);
665 if (((flags
& VP8_EFLAG_NO_UPD_GF
) && (flags
& VP8_EFLAG_FORCE_GF
))
666 || ((flags
& VP8_EFLAG_NO_UPD_ARF
) && (flags
& VP8_EFLAG_FORCE_ARF
)))
668 ctx
->base
.err_detail
= "Conflicting flags.";
669 return VPX_CODEC_INVALID_PARAM
;
672 if (flags
& (VP8_EFLAG_NO_REF_LAST
| VP8_EFLAG_NO_REF_GF
673 | VP8_EFLAG_NO_REF_ARF
))
677 if (flags
& VP8_EFLAG_NO_REF_LAST
)
678 ref
^= VP8_LAST_FLAG
;
680 if (flags
& VP8_EFLAG_NO_REF_GF
)
681 ref
^= VP8_GOLD_FLAG
;
683 if (flags
& VP8_EFLAG_NO_REF_ARF
)
686 vp8_use_as_reference(ctx
->cpi
, ref
);
689 if (flags
& (VP8_EFLAG_NO_UPD_LAST
| VP8_EFLAG_NO_UPD_GF
690 | VP8_EFLAG_NO_UPD_ARF
| VP8_EFLAG_FORCE_GF
691 | VP8_EFLAG_FORCE_ARF
))
695 if (flags
& VP8_EFLAG_NO_UPD_LAST
)
696 upd
^= VP8_LAST_FLAG
;
698 if (flags
& VP8_EFLAG_NO_UPD_GF
)
699 upd
^= VP8_GOLD_FLAG
;
701 if (flags
& VP8_EFLAG_NO_UPD_ARF
)
704 vp8_update_reference(ctx
->cpi
, upd
);
707 if (flags
& VP8_EFLAG_NO_UPD_ENTROPY
)
709 vp8_update_entropy(ctx
->cpi
, 0);
712 /* Handle fixed keyframe intervals */
713 if (ctx
->cfg
.kf_mode
== VPX_KF_AUTO
714 && ctx
->cfg
.kf_min_dist
== ctx
->cfg
.kf_max_dist
)
716 if (++ctx
->fixed_kf_cntr
> ctx
->cfg
.kf_min_dist
)
718 flags
|= VPX_EFLAG_FORCE_KF
;
719 ctx
->fixed_kf_cntr
= 1;
723 /* Initialize the encoder instance on the first frame*/
724 if (!res
&& ctx
->cpi
)
726 unsigned int lib_flags
;
727 YV12_BUFFER_CONFIG sd
;
728 INT64 dst_time_stamp
, dst_end_time_stamp
;
729 unsigned long size
, cx_data_sz
;
730 unsigned char *cx_data
;
732 /* Set up internal flags */
733 if (ctx
->base
.init_flags
& VPX_CODEC_USE_PSNR
)
734 ((VP8_COMP
*)ctx
->cpi
)->b_calculate_psnr
= 1;
736 /* Convert API flags to internal codec lib flags */
737 lib_flags
= (flags
& VPX_EFLAG_FORCE_KF
) ? FRAMEFLAGS_KEY
: 0;
739 /* vp8 use 10,000,000 ticks/second as time stamp */
740 dst_time_stamp
= pts
* 10000000 * ctx
->cfg
.g_timebase
.num
/ ctx
->cfg
.g_timebase
.den
;
741 dst_end_time_stamp
= (pts
+ duration
) * 10000000 * ctx
->cfg
.g_timebase
.num
/ ctx
->cfg
.g_timebase
.den
;
745 res
= image2yuvconfig(img
, &sd
);
747 if (vp8_receive_raw_frame(ctx
->cpi
, ctx
->next_frame_flag
| lib_flags
,
748 &sd
, dst_time_stamp
, dst_end_time_stamp
))
750 VP8_COMP
*cpi
= (VP8_COMP
*)ctx
->cpi
;
751 res
= update_error_state(ctx
, &cpi
->common
.error
);
754 /* reset for next frame */
755 ctx
->next_frame_flag
= 0;
758 cx_data
= ctx
->cx_data
;
759 cx_data_sz
= ctx
->cx_data_sz
;
762 while (cx_data_sz
>= ctx
->cx_data_sz
/ 2
763 && -1 != vp8_get_compressed_data(ctx
->cpi
, &lib_flags
, &size
, cx_data
, &dst_time_stamp
, &dst_end_time_stamp
, !img
))
767 vpx_codec_pts_t round
, delta
;
768 vpx_codec_cx_pkt_t pkt
;
769 VP8_COMP
*cpi
= (VP8_COMP
*)ctx
->cpi
;
771 /* Add the frame packet to the list of returned packets. */
772 round
= 1000000 * ctx
->cfg
.g_timebase
.num
/ 2 - 1;
773 delta
= (dst_end_time_stamp
- dst_time_stamp
);
774 pkt
.kind
= VPX_CODEC_CX_FRAME_PKT
;
775 pkt
.data
.frame
.buf
= cx_data
;
776 pkt
.data
.frame
.sz
= size
;
778 (dst_time_stamp
* ctx
->cfg
.g_timebase
.den
+ round
)
779 / ctx
->cfg
.g_timebase
.num
/ 10000000;
780 pkt
.data
.frame
.duration
=
781 (delta
* ctx
->cfg
.g_timebase
.den
+ round
)
782 / ctx
->cfg
.g_timebase
.num
/ 10000000;
783 pkt
.data
.frame
.flags
= lib_flags
<< 16;
785 if (lib_flags
& FRAMEFLAGS_KEY
)
786 pkt
.data
.frame
.flags
|= VPX_FRAME_IS_KEY
;
788 if (!cpi
->common
.show_frame
)
790 pkt
.data
.frame
.flags
|= VPX_FRAME_IS_INVISIBLE
;
792 // This timestamp should be as close as possible to the
793 // prior PTS so that if a decoder uses pts to schedule when
794 // to do this, we start right after last frame was decoded.
795 // Invisible frames have no duration.
796 pkt
.data
.frame
.pts
= ((cpi
->last_time_stamp_seen
797 * ctx
->cfg
.g_timebase
.den
+ round
)
798 / ctx
->cfg
.g_timebase
.num
/ 10000000) + 1;
799 pkt
.data
.frame
.duration
= 0;
802 vpx_codec_pkt_list_add(&ctx
->pkt_list
.head
, &pkt
);
804 //printf("timestamp: %lld, duration: %d\n", pkt->data.frame.pts, pkt->data.frame.duration);
815 static const vpx_codec_cx_pkt_t
*vp8e_get_cxdata(vpx_codec_alg_priv_t
*ctx
,
816 vpx_codec_iter_t
*iter
)
818 return vpx_codec_pkt_list_get(&ctx
->pkt_list
.head
, iter
);
821 static vpx_codec_err_t
vp8e_set_reference(vpx_codec_alg_priv_t
*ctx
,
825 vpx_ref_frame_t
*data
= va_arg(args
, vpx_ref_frame_t
*);
829 vpx_ref_frame_t
*frame
= (vpx_ref_frame_t
*)data
;
830 YV12_BUFFER_CONFIG sd
;
832 image2yuvconfig(&frame
->img
, &sd
);
833 vp8_set_reference(ctx
->cpi
, frame
->frame_type
, &sd
);
837 return VPX_CODEC_INVALID_PARAM
;
841 static vpx_codec_err_t
vp8e_get_reference(vpx_codec_alg_priv_t
*ctx
,
846 vpx_ref_frame_t
*data
= va_arg(args
, vpx_ref_frame_t
*);
850 vpx_ref_frame_t
*frame
= (vpx_ref_frame_t
*)data
;
851 YV12_BUFFER_CONFIG sd
;
853 image2yuvconfig(&frame
->img
, &sd
);
854 vp8_get_reference(ctx
->cpi
, frame
->frame_type
, &sd
);
858 return VPX_CODEC_INVALID_PARAM
;
861 static vpx_codec_err_t
vp8e_set_previewpp(vpx_codec_alg_priv_t
*ctx
,
866 vp8_postproc_cfg_t
*data
= va_arg(args
, vp8_postproc_cfg_t
*);
871 ctx
->preview_ppcfg
= *((vp8_postproc_cfg_t
*)data
);
875 return VPX_CODEC_INVALID_PARAM
;
880 return VPX_CODEC_INCAPABLE
;
885 static vpx_image_t
*vp8e_get_preview(vpx_codec_alg_priv_t
*ctx
)
888 YV12_BUFFER_CONFIG sd
;
889 vp8_ppflags_t flags
= {0};
891 if (ctx
->preview_ppcfg
.post_proc_flag
)
893 flags
.post_proc_flag
= ctx
->preview_ppcfg
.post_proc_flag
;
894 flags
.deblocking_level
= ctx
->preview_ppcfg
.deblocking_level
;
895 flags
.noise_level
= ctx
->preview_ppcfg
.noise_level
;
898 if (0 == vp8_get_preview_raw_frame(ctx
->cpi
, &sd
, &flags
))
902 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
903 sd.y_width + 2*VP8BORDERINPIXELS,
904 sd.y_height + 2*VP8BORDERINPIXELS,
907 vpx_img_set_rect(&ctx->preview_img,
908 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
909 sd.y_width, sd.y_height);
912 ctx
->preview_img
.bps
= 12;
913 ctx
->preview_img
.planes
[VPX_PLANE_Y
] = sd
.y_buffer
;
914 ctx
->preview_img
.planes
[VPX_PLANE_U
] = sd
.u_buffer
;
915 ctx
->preview_img
.planes
[VPX_PLANE_V
] = sd
.v_buffer
;
917 if (sd
.clrtype
== REG_YUV
)
918 ctx
->preview_img
.fmt
= VPX_IMG_FMT_I420
;
920 ctx
->preview_img
.fmt
= VPX_IMG_FMT_VPXI420
;
922 ctx
->preview_img
.x_chroma_shift
= 1;
923 ctx
->preview_img
.y_chroma_shift
= 1;
925 ctx
->preview_img
.d_w
= sd
.y_width
;
926 ctx
->preview_img
.d_h
= sd
.y_height
;
927 ctx
->preview_img
.stride
[VPX_PLANE_Y
] = sd
.y_stride
;
928 ctx
->preview_img
.stride
[VPX_PLANE_U
] = sd
.uv_stride
;
929 ctx
->preview_img
.stride
[VPX_PLANE_V
] = sd
.uv_stride
;
930 ctx
->preview_img
.w
= sd
.y_width
;
931 ctx
->preview_img
.h
= sd
.y_height
;
933 return &ctx
->preview_img
;
939 static vpx_codec_err_t
vp8e_update_entropy(vpx_codec_alg_priv_t
*ctx
,
943 int update
= va_arg(args
, int);
944 vp8_update_entropy(ctx
->cpi
, update
);
949 static vpx_codec_err_t
vp8e_update_reference(vpx_codec_alg_priv_t
*ctx
,
953 int update
= va_arg(args
, int);
954 vp8_update_reference(ctx
->cpi
, update
);
958 static vpx_codec_err_t
vp8e_use_reference(vpx_codec_alg_priv_t
*ctx
,
962 int reference_flag
= va_arg(args
, int);
963 vp8_use_as_reference(ctx
->cpi
, reference_flag
);
967 static vpx_codec_err_t
vp8e_set_roi_map(vpx_codec_alg_priv_t
*ctx
,
971 vpx_roi_map_t
*data
= va_arg(args
, vpx_roi_map_t
*);
975 vpx_roi_map_t
*roi
= (vpx_roi_map_t
*)data
;
977 if (!vp8_set_roimap(ctx
->cpi
, roi
->roi_map
, roi
->rows
, roi
->cols
, roi
->delta_q
, roi
->delta_lf
, roi
->static_threshold
))
980 return VPX_CODEC_INVALID_PARAM
;
983 return VPX_CODEC_INVALID_PARAM
;
987 static vpx_codec_err_t
vp8e_set_activemap(vpx_codec_alg_priv_t
*ctx
,
991 vpx_active_map_t
*data
= va_arg(args
, vpx_active_map_t
*);
996 vpx_active_map_t
*map
= (vpx_active_map_t
*)data
;
998 if (!vp8_set_active_map(ctx
->cpi
, map
->active_map
, map
->rows
, map
->cols
))
1001 return VPX_CODEC_INVALID_PARAM
;
1004 return VPX_CODEC_INVALID_PARAM
;
1007 static vpx_codec_err_t
vp8e_set_scalemode(vpx_codec_alg_priv_t
*ctx
,
1012 vpx_scaling_mode_t
*data
= va_arg(args
, vpx_scaling_mode_t
*);
1017 vpx_scaling_mode_t scalemode
= *(vpx_scaling_mode_t
*)data
;
1018 res
= vp8_set_internal_size(ctx
->cpi
, scalemode
.h_scaling_mode
, scalemode
.v_scaling_mode
);
1022 /*force next frame a key frame to effect scaling mode */
1023 ctx
->next_frame_flag
|= FRAMEFLAGS_KEY
;
1024 return VPX_CODEC_OK
;
1027 return VPX_CODEC_INVALID_PARAM
;
1030 return VPX_CODEC_INVALID_PARAM
;
1034 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps
[] =
1036 {VP8_SET_REFERENCE
, vp8e_set_reference
},
1037 {VP8_COPY_REFERENCE
, vp8e_get_reference
},
1038 {VP8_SET_POSTPROC
, vp8e_set_previewpp
},
1039 {VP8E_UPD_ENTROPY
, vp8e_update_entropy
},
1040 {VP8E_UPD_REFERENCE
, vp8e_update_reference
},
1041 {VP8E_USE_REFERENCE
, vp8e_use_reference
},
1042 {VP8E_SET_ROI_MAP
, vp8e_set_roi_map
},
1043 {VP8E_SET_ACTIVEMAP
, vp8e_set_activemap
},
1044 {VP8E_SET_SCALEMODE
, vp8e_set_scalemode
},
1045 {VP8E_SET_ENCODING_MODE
, set_param
},
1046 {VP8E_SET_CPUUSED
, set_param
},
1047 {VP8E_SET_NOISE_SENSITIVITY
, set_param
},
1048 {VP8E_SET_ENABLEAUTOALTREF
, set_param
},
1049 {VP8E_SET_SHARPNESS
, set_param
},
1050 {VP8E_SET_STATIC_THRESHOLD
, set_param
},
1051 {VP8E_SET_TOKEN_PARTITIONS
, set_param
},
1052 {VP8E_GET_LAST_QUANTIZER
, get_param
},
1053 {VP8E_GET_LAST_QUANTIZER_64
, get_param
},
1054 {VP8E_SET_ARNR_MAXFRAMES
, set_param
},
1055 {VP8E_SET_ARNR_STRENGTH
, set_param
},
1056 {VP8E_SET_ARNR_TYPE
, set_param
},
1057 {VP8E_SET_TUNING
, set_param
},
1058 {VP8E_SET_CQ_LEVEL
, set_param
},
1062 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map
[] =
1073 {1, 30}, /* g_timebase */
1075 0, /* g_error_resilient */
1077 VPX_RC_ONE_PASS
, /* g_pass */
1079 0, /* g_lag_in_frames */
1081 0, /* rc_dropframe_thresh */
1082 0, /* rc_resize_allowed */
1083 60, /* rc_resize_down_thresold */
1084 30, /* rc_resize_up_thresold */
1086 VPX_VBR
, /* rc_end_usage */
1087 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1088 {0}, /* rc_twopass_stats_in */
1090 256, /* rc_target_bandwidth */
1092 4, /* rc_min_quantizer */
1093 63, /* rc_max_quantizer */
1094 95, /* rc_undershoot_pct */
1095 200, /* rc_overshoot_pct */
1097 6000, /* rc_max_buffer_size */
1098 4000, /* rc_buffer_initial_size; */
1099 5000, /* rc_buffer_optimal_size; */
1101 50, /* rc_two_pass_vbrbias */
1102 0, /* rc_two_pass_vbrmin_section */
1103 400, /* rc_two_pass_vbrmax_section */
1105 /* keyframing settings (kf) */
1106 VPX_KF_AUTO
, /* g_kfmode*/
1107 0, /* kf_min_dist */
1108 9999, /* kf_max_dist */
1110 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
1111 1, /* g_delete_first_pass_file */
1112 "vp8.fpf" /* first pass filename */
1115 { -1, {NOT_IMPLEMENTED
}}
1119 #ifndef VERSION_STRING
1120 #define VERSION_STRING
1122 CODEC_INTERFACE(vpx_codec_vp8_cx
) =
1124 "WebM Project VP8 Encoder" VERSION_STRING
,
1125 VPX_CODEC_INTERNAL_ABI_VERSION
,
1126 VPX_CODEC_CAP_ENCODER
| VPX_CODEC_CAP_PSNR
,
1127 /* vpx_codec_caps_t caps; */
1128 vp8e_init
, /* vpx_codec_init_fn_t init; */
1129 vp8e_destroy
, /* vpx_codec_destroy_fn_t destroy; */
1130 vp8e_ctf_maps
, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1131 NOT_IMPLEMENTED
, /* vpx_codec_get_mmap_fn_t get_mmap; */
1132 NOT_IMPLEMENTED
, /* vpx_codec_set_mmap_fn_t set_mmap; */
1134 NOT_IMPLEMENTED
, /* vpx_codec_peek_si_fn_t peek_si; */
1135 NOT_IMPLEMENTED
, /* vpx_codec_get_si_fn_t get_si; */
1136 NOT_IMPLEMENTED
, /* vpx_codec_decode_fn_t decode; */
1137 NOT_IMPLEMENTED
, /* vpx_codec_frame_get_fn_t frame_get; */
1140 vp8e_usage_cfg_map
, /* vpx_codec_enc_cfg_map_t peek_si; */
1141 vp8e_encode
, /* vpx_codec_encode_fn_t encode; */
1142 vp8e_get_cxdata
, /* vpx_codec_get_cx_data_fn_t frame_get; */
1146 } /* encoder functions */
1151 * BEGIN BACKWARDS COMPATIBILITY SHIM.
1154 static vpx_codec_err_t
api1_control(vpx_codec_alg_priv_t
*ctx
,
1158 vpx_codec_ctrl_fn_map_t
*entry
;
1162 case VP8E_SET_FLUSHFLAG
:
1163 /* VP8 sample code did VP8E_SET_FLUSHFLAG followed by
1164 * vpx_codec_get_cx_data() rather than vpx_codec_encode().
1166 return vp8e_encode(ctx
, NULL
, 0, 0, 0, 0);
1167 case VP8E_SET_FRAMETYPE
:
1168 ctx
->base
.enc
.tbd
|= FORCE_KEY
;
1169 return VPX_CODEC_OK
;
1172 for (entry
= vp8e_ctf_maps
; entry
&& entry
->fn
; entry
++)
1174 if (!entry
->ctrl_id
|| entry
->ctrl_id
== ctrl_id
)
1176 return entry
->fn(ctx
, ctrl_id
, args
);
1180 return VPX_CODEC_ERROR
;
1184 static vpx_codec_ctrl_fn_map_t api1_ctrl_maps
[] =
1191 static vpx_codec_err_t
api1_encode(vpx_codec_alg_priv_t
*ctx
,
1192 const vpx_image_t
*img
,
1193 vpx_codec_pts_t pts
,
1194 unsigned long duration
,
1195 vpx_enc_frame_flags_t flags
,
1196 unsigned long deadline
)
1198 int force
= ctx
->base
.enc
.tbd
;
1200 ctx
->base
.enc
.tbd
= 0;
1206 flags
| ((force
& FORCE_KEY
) ? VPX_EFLAG_FORCE_KF
: 0),
1211 vpx_codec_iface_t vpx_enc_vp8_algo
=
1213 "WebM Project VP8 Encoder (Deprecated API)" VERSION_STRING
,
1214 VPX_CODEC_INTERNAL_ABI_VERSION
,
1215 VPX_CODEC_CAP_ENCODER
,
1216 /* vpx_codec_caps_t caps; */
1217 vp8e_init
, /* vpx_codec_init_fn_t init; */
1218 vp8e_destroy
, /* vpx_codec_destroy_fn_t destroy; */
1219 api1_ctrl_maps
, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1220 NOT_IMPLEMENTED
, /* vpx_codec_get_mmap_fn_t get_mmap; */
1221 NOT_IMPLEMENTED
, /* vpx_codec_set_mmap_fn_t set_mmap; */
1222 {NOT_IMPLEMENTED
}, /* decoder functions */
1224 vp8e_usage_cfg_map
, /* vpx_codec_enc_cfg_map_t peek_si; */
1225 api1_encode
, /* vpx_codec_encode_fn_t encode; */
1226 vp8e_get_cxdata
, /* vpx_codec_get_cx_data_fn_t frame_get; */
1230 } /* encoder functions */