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"
17 #include "vp8/encoder/firstpass.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 */
43 struct extraconfig_map
46 struct vp8_extracfg cfg
;
49 static const struct extraconfig_map extracfg_map
[] =
55 #if !(CONFIG_REALTIME_ONLY)
56 VP8_BEST_QUALITY_ENCODING
, /* Encoding Mode */
59 VP8_REAL_TIME_ENCODING
, /* Encoding Mode */
62 0, /* enable_auto_alt_ref */
63 0, /* noise_sensitivity */
65 0, /* static_thresh */
66 VP8_ONE_TOKENPARTITION
, /* token_partitions */
67 0, /* arnr_max_frames */
68 3, /* arnr_strength */
74 struct vpx_codec_alg_priv
76 vpx_codec_priv_t base
;
77 vpx_codec_enc_cfg_t cfg
;
78 struct vp8_extracfg vp8_cfg
;
81 unsigned char *cx_data
;
82 unsigned int cx_data_sz
;
83 vpx_image_t preview_img
;
84 unsigned int next_frame_flag
;
85 vp8_postproc_cfg_t preview_ppcfg
;
86 vpx_codec_pkt_list_decl(64) pkt_list
; // changed to accomendate the maximum number of lagged frames allowed
88 unsigned int fixed_kf_cntr
;
92 static vpx_codec_err_t
93 update_error_state(vpx_codec_alg_priv_t
*ctx
,
94 const struct vpx_internal_error_info
*error
)
98 if ((res
= error
->error_code
))
99 ctx
->base
.err_detail
= error
->has_detail
108 #define ERROR(str) do {\
109 ctx->base.err_detail = str;\
110 return VPX_CODEC_INVALID_PARAM;\
113 #define RANGE_CHECK(p,memb,lo,hi) do {\
114 if(!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
115 ERROR(#memb " out of range ["#lo".."#hi"]");\
118 #define RANGE_CHECK_HI(p,memb,hi) do {\
119 if(!((p)->memb <= (hi))) \
120 ERROR(#memb " out of range [.."#hi"]");\
123 #define RANGE_CHECK_LO(p,memb,lo) do {\
124 if(!((p)->memb >= (lo))) \
125 ERROR(#memb " out of range ["#lo"..]");\
128 #define RANGE_CHECK_BOOL(p,memb) do {\
129 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
132 static vpx_codec_err_t
validate_config(vpx_codec_alg_priv_t
*ctx
,
133 const vpx_codec_enc_cfg_t
*cfg
,
134 const struct vp8_extracfg
*vp8_cfg
)
136 RANGE_CHECK(cfg
, g_w
, 1, 16384);
137 RANGE_CHECK(cfg
, g_h
, 1, 16384);
138 RANGE_CHECK(cfg
, g_timebase
.den
, 1, 1000000000);
139 RANGE_CHECK(cfg
, g_timebase
.num
, 1, cfg
->g_timebase
.den
);
140 RANGE_CHECK_HI(cfg
, g_profile
, 3);
141 RANGE_CHECK_HI(cfg
, rc_min_quantizer
, 63);
142 RANGE_CHECK_HI(cfg
, rc_max_quantizer
, 63);
143 RANGE_CHECK_HI(cfg
, g_threads
, 64);
144 #if !(CONFIG_REALTIME_ONLY)
145 RANGE_CHECK_HI(cfg
, g_lag_in_frames
, 25);
147 RANGE_CHECK_HI(cfg
, g_lag_in_frames
, 0);
149 RANGE_CHECK(cfg
, rc_end_usage
, VPX_VBR
, VPX_CBR
);
150 RANGE_CHECK_HI(cfg
, rc_undershoot_pct
, 100);
151 RANGE_CHECK_HI(cfg
, rc_2pass_vbr_bias_pct
, 100);
152 RANGE_CHECK(cfg
, kf_mode
, VPX_KF_DISABLED
, VPX_KF_AUTO
);
153 //RANGE_CHECK_BOOL(cfg, g_delete_firstpassfile);
154 RANGE_CHECK_BOOL(cfg
, rc_resize_allowed
);
155 RANGE_CHECK_HI(cfg
, rc_dropframe_thresh
, 100);
156 RANGE_CHECK_HI(cfg
, rc_resize_up_thresh
, 100);
157 RANGE_CHECK_HI(cfg
, rc_resize_down_thresh
, 100);
158 #if !(CONFIG_REALTIME_ONLY)
159 RANGE_CHECK(cfg
, g_pass
, VPX_RC_ONE_PASS
, VPX_RC_LAST_PASS
);
161 RANGE_CHECK(cfg
, g_pass
, VPX_RC_ONE_PASS
, VPX_RC_ONE_PASS
);
164 /* VP8 does not support a lower bound on the keyframe interval in
165 * automatic keyframe placement mode.
167 if (cfg
->kf_mode
!= VPX_KF_DISABLED
&& cfg
->kf_min_dist
!= cfg
->kf_max_dist
168 && cfg
->kf_min_dist
> 0)
169 ERROR("kf_min_dist not supported in auto mode, use 0 "
170 "or kf_max_dist instead.");
172 RANGE_CHECK_BOOL(vp8_cfg
, enable_auto_alt_ref
);
173 #if !(CONFIG_REALTIME_ONLY)
174 RANGE_CHECK(vp8_cfg
, encoding_mode
, VP8_BEST_QUALITY_ENCODING
, VP8_REAL_TIME_ENCODING
);
175 RANGE_CHECK(vp8_cfg
, cpu_used
, -16, 16);
176 RANGE_CHECK_HI(vp8_cfg
, noise_sensitivity
, 6);
178 RANGE_CHECK(vp8_cfg
, encoding_mode
, VP8_REAL_TIME_ENCODING
, VP8_REAL_TIME_ENCODING
);
180 if (!((vp8_cfg
->cpu_used
>= -16 && vp8_cfg
->cpu_used
<= -4) || (vp8_cfg
->cpu_used
>= 4 && vp8_cfg
->cpu_used
<= 16)))
181 ERROR("cpu_used out of range [-16..-4] or [4..16]");
183 RANGE_CHECK(vp8_cfg
, noise_sensitivity
, 0, 0);
186 RANGE_CHECK(vp8_cfg
, token_partitions
, VP8_ONE_TOKENPARTITION
, VP8_EIGHT_TOKENPARTITION
);
187 RANGE_CHECK_HI(vp8_cfg
, Sharpness
, 7);
188 RANGE_CHECK(vp8_cfg
, arnr_max_frames
, 0, 15);
189 RANGE_CHECK_HI(vp8_cfg
, arnr_strength
, 6);
190 RANGE_CHECK(vp8_cfg
, arnr_type
, 1, 3);
192 if (cfg
->g_pass
== VPX_RC_LAST_PASS
)
194 int mb_r
= (cfg
->g_h
+ 15) / 16;
195 int mb_c
= (cfg
->g_w
+ 15) / 16;
196 size_t packet_sz
= vp8_firstpass_stats_sz(mb_r
* mb_c
);
197 int n_packets
= cfg
->rc_twopass_stats_in
.sz
/ packet_sz
;
198 FIRSTPASS_STATS
*stats
;
200 if (!cfg
->rc_twopass_stats_in
.buf
)
201 ERROR("rc_twopass_stats_in.buf not set.");
203 if (cfg
->rc_twopass_stats_in
.sz
% packet_sz
)
204 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
206 if (cfg
->rc_twopass_stats_in
.sz
< 2 * packet_sz
)
207 ERROR("rc_twopass_stats_in requires at least two packets.");
209 stats
= (void*)((char *)cfg
->rc_twopass_stats_in
.buf
210 + (n_packets
- 1) * packet_sz
);
212 if ((int)(stats
->count
+ 0.5) != n_packets
- 1)
213 ERROR("rc_twopass_stats_in missing EOS stats packet");
220 static vpx_codec_err_t
validate_img(vpx_codec_alg_priv_t
*ctx
,
221 const vpx_image_t
*img
)
225 case VPX_IMG_FMT_YV12
:
226 case VPX_IMG_FMT_I420
:
227 case VPX_IMG_FMT_VPXI420
:
228 case VPX_IMG_FMT_VPXYV12
:
231 ERROR("Invalid image format. Only YV12 and I420 images are supported");
234 if ((img
->d_w
!= ctx
->cfg
.g_w
) || (img
->d_h
!= ctx
->cfg
.g_h
))
235 ERROR("Image size must match encoder init configuration size");
241 static vpx_codec_err_t
set_vp8e_config(VP8_CONFIG
*oxcf
,
242 vpx_codec_enc_cfg_t cfg
,
243 struct vp8_extracfg vp8_cfg
)
245 oxcf
->multi_threaded
= cfg
.g_threads
;
246 oxcf
->Version
= cfg
.g_profile
;
248 oxcf
->Width
= cfg
.g_w
;
249 oxcf
->Height
= cfg
.g_h
;
250 /* guess a frame rate if out of whack, use 30 */
251 oxcf
->frame_rate
= (double)(cfg
.g_timebase
.den
) / (double)(cfg
.g_timebase
.num
);
253 if (oxcf
->frame_rate
> 180)
255 oxcf
->frame_rate
= 30;
258 oxcf
->error_resilient_mode
= cfg
.g_error_resilient
;
262 case VPX_RC_ONE_PASS
:
263 oxcf
->Mode
= MODE_BESTQUALITY
;
265 case VPX_RC_FIRST_PASS
:
266 oxcf
->Mode
= MODE_FIRSTPASS
;
268 case VPX_RC_LAST_PASS
:
269 oxcf
->Mode
= MODE_SECONDPASS_BEST
;
273 if (cfg
.g_pass
== VPX_RC_FIRST_PASS
)
276 oxcf
->lag_in_frames
= 0;
280 oxcf
->allow_lag
= (cfg
.g_lag_in_frames
) > 0;
281 oxcf
->lag_in_frames
= cfg
.g_lag_in_frames
;
284 oxcf
->allow_df
= (cfg
.rc_dropframe_thresh
> 0);
285 oxcf
->drop_frames_water_mark
= cfg
.rc_dropframe_thresh
;
287 oxcf
->allow_spatial_resampling
= cfg
.rc_resize_allowed
;
288 oxcf
->resample_up_water_mark
= cfg
.rc_resize_up_thresh
;
289 oxcf
->resample_down_water_mark
= cfg
.rc_resize_down_thresh
;
291 if (cfg
.rc_end_usage
== VPX_VBR
)
293 oxcf
->end_usage
= USAGE_LOCAL_FILE_PLAYBACK
;
295 else if (cfg
.rc_end_usage
== VPX_CBR
)
297 oxcf
->end_usage
= USAGE_STREAM_FROM_SERVER
;
300 oxcf
->target_bandwidth
= cfg
.rc_target_bitrate
;
302 oxcf
->best_allowed_q
= cfg
.rc_min_quantizer
;
303 oxcf
->worst_allowed_q
= cfg
.rc_max_quantizer
;
306 oxcf
->under_shoot_pct
= cfg
.rc_undershoot_pct
;
307 //oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
309 oxcf
->maximum_buffer_size
= cfg
.rc_buf_sz
;
310 oxcf
->starting_buffer_level
= cfg
.rc_buf_initial_sz
;
311 oxcf
->optimal_buffer_level
= cfg
.rc_buf_optimal_sz
;
313 oxcf
->two_pass_vbrbias
= cfg
.rc_2pass_vbr_bias_pct
;
314 oxcf
->two_pass_vbrmin_section
= cfg
.rc_2pass_vbr_minsection_pct
;
315 oxcf
->two_pass_vbrmax_section
= cfg
.rc_2pass_vbr_maxsection_pct
;
317 oxcf
->auto_key
= cfg
.kf_mode
== VPX_KF_AUTO
318 && cfg
.kf_min_dist
!= cfg
.kf_max_dist
;
319 //oxcf->kf_min_dist = cfg.kf_min_dis;
320 oxcf
->key_freq
= cfg
.kf_max_dist
;
322 //oxcf->delete_first_pass_file = cfg.g_delete_firstpassfile;
323 //strcpy(oxcf->first_pass_file, cfg.g_firstpass_file);
325 oxcf
->cpu_used
= vp8_cfg
.cpu_used
;
326 oxcf
->encode_breakout
= vp8_cfg
.static_thresh
;
327 oxcf
->play_alternate
= vp8_cfg
.enable_auto_alt_ref
;
328 oxcf
->noise_sensitivity
= vp8_cfg
.noise_sensitivity
;
329 oxcf
->Sharpness
= vp8_cfg
.Sharpness
;
330 oxcf
->token_partitions
= vp8_cfg
.token_partitions
;
332 oxcf
->two_pass_stats_in
= cfg
.rc_twopass_stats_in
;
333 oxcf
->output_pkt_list
= vp8_cfg
.pkt_list
;
335 oxcf
->arnr_max_frames
= vp8_cfg
.arnr_max_frames
;
336 oxcf
->arnr_strength
= vp8_cfg
.arnr_strength
;
337 oxcf
->arnr_type
= vp8_cfg
.arnr_type
;
341 printf("Current VP8 Settings: \n");
342 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
343 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
344 printf("Sharpness: %d\n", oxcf->Sharpness);
345 printf("cpu_used: %d\n", oxcf->cpu_used);
346 printf("Mode: %d\n", oxcf->Mode);
347 printf("delete_first_pass_file: %d\n", oxcf->delete_first_pass_file);
348 printf("auto_key: %d\n", oxcf->auto_key);
349 printf("key_freq: %d\n", oxcf->key_freq);
350 printf("end_usage: %d\n", oxcf->end_usage);
351 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
352 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
353 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
354 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
355 printf("fixed_q: %d\n", oxcf->fixed_q);
356 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
357 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
358 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
359 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
360 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
361 printf("allow_df: %d\n", oxcf->allow_df);
362 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
363 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
364 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
365 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
366 printf("allow_lag: %d\n", oxcf->allow_lag);
367 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
368 printf("play_alternate: %d\n", oxcf->play_alternate);
369 printf("Version: %d\n", oxcf->Version);
370 printf("multi_threaded: %d\n", oxcf->multi_threaded);
371 printf("encode_breakout: %d\n", oxcf->encode_breakout);
376 static vpx_codec_err_t
vp8e_set_config(vpx_codec_alg_priv_t
*ctx
,
377 const vpx_codec_enc_cfg_t
*cfg
)
381 if ((cfg
->g_w
!= ctx
->cfg
.g_w
) || (cfg
->g_h
!= ctx
->cfg
.g_h
))
382 ERROR("Cannot change width or height after initialization");
384 /* Prevent increasing lag_in_frames. This check is stricter than it needs
385 * to be -- the limit is not increasing past the first lag_in_frames
386 * value, but we don't track the initial config, only the last successful
389 if ((cfg
->g_lag_in_frames
> ctx
->cfg
.g_lag_in_frames
))
390 ERROR("Cannot increase lag_in_frames");
392 res
= validate_config(ctx
, cfg
, &ctx
->vp8_cfg
);
397 set_vp8e_config(&ctx
->oxcf
, ctx
->cfg
, ctx
->vp8_cfg
);
398 vp8_change_config(ctx
->cpi
, &ctx
->oxcf
);
405 int vp8_reverse_trans(int);
408 static vpx_codec_err_t
get_param(vpx_codec_alg_priv_t
*ctx
,
412 void *arg
= va_arg(args
, void *);
414 #define MAP(id, var) case id: *(RECAST(id, arg)) = var; break
417 return VPX_CODEC_INVALID_PARAM
;
421 MAP(VP8E_GET_LAST_QUANTIZER
, vp8_get_quantizer(ctx
->cpi
));
422 MAP(VP8E_GET_LAST_QUANTIZER_64
, vp8_reverse_trans(vp8_get_quantizer(ctx
->cpi
)));
430 static vpx_codec_err_t
set_param(vpx_codec_alg_priv_t
*ctx
,
434 vpx_codec_err_t res
= VPX_CODEC_OK
;
435 struct vp8_extracfg xcfg
= ctx
->vp8_cfg
;
437 #define MAP(id, var) case id: var = CAST(id, args); break;
441 MAP(VP8E_SET_ENCODING_MODE
, ctx
->deprecated_mode
);
442 MAP(VP8E_SET_CPUUSED
, xcfg
.cpu_used
);
443 MAP(VP8E_SET_ENABLEAUTOALTREF
, xcfg
.enable_auto_alt_ref
);
444 MAP(VP8E_SET_NOISE_SENSITIVITY
, xcfg
.noise_sensitivity
);
445 MAP(VP8E_SET_SHARPNESS
, xcfg
.Sharpness
);
446 MAP(VP8E_SET_STATIC_THRESHOLD
, xcfg
.static_thresh
);
447 MAP(VP8E_SET_TOKEN_PARTITIONS
, xcfg
.token_partitions
);
449 MAP(VP8E_SET_ARNR_MAXFRAMES
, xcfg
.arnr_max_frames
);
450 MAP(VP8E_SET_ARNR_STRENGTH
, xcfg
.arnr_strength
);
451 MAP(VP8E_SET_ARNR_TYPE
, xcfg
.arnr_type
);
455 res
= validate_config(ctx
, &ctx
->cfg
, &xcfg
);
460 set_vp8e_config(&ctx
->oxcf
, ctx
->cfg
, ctx
->vp8_cfg
);
461 vp8_change_config(ctx
->cpi
, &ctx
->oxcf
);
467 static vpx_codec_err_t
vp8e_init(vpx_codec_ctx_t
*ctx
)
469 vpx_codec_err_t res
= VPX_DEC_OK
;
470 struct vpx_codec_alg_priv
*priv
;
471 vpx_codec_enc_cfg_t
*cfg
;
478 priv
= calloc(1, sizeof(struct vpx_codec_alg_priv
));
482 ctx
->priv
= &priv
->base
;
483 ctx
->priv
->sz
= sizeof(*ctx
->priv
);
484 ctx
->priv
->iface
= ctx
->iface
;
485 ctx
->priv
->alg_priv
= priv
;
486 ctx
->priv
->init_flags
= ctx
->init_flags
;
490 /* Update the reference to the config structure to an
493 ctx
->priv
->alg_priv
->cfg
= *ctx
->config
.enc
;
494 ctx
->config
.enc
= &ctx
->priv
->alg_priv
->cfg
;
497 cfg
= &ctx
->priv
->alg_priv
->cfg
;
499 /* Select the extra vp6 configuration table based on the current
500 * usage value. If the current usage value isn't found, use the
501 * values for usage case 0.
504 extracfg_map
[i
].usage
&& extracfg_map
[i
].usage
!= cfg
->g_usage
;
507 priv
->vp8_cfg
= extracfg_map
[i
].cfg
;
508 priv
->vp8_cfg
.pkt_list
= &priv
->pkt_list
.head
;
510 priv
->cx_data_sz
= priv
->cfg
.g_w
* priv
->cfg
.g_h
* 3 / 2 * 2;
512 if (priv
->cx_data_sz
< 4096) priv
->cx_data_sz
= 4096;
514 priv
->cx_data
= malloc(priv
->cx_data_sz
);
515 priv
->deprecated_mode
= NO_MODE_SET
;
519 res
= validate_config(priv
, &priv
->cfg
, &priv
->vp8_cfg
);
523 set_vp8e_config(&ctx
->priv
->alg_priv
->oxcf
, ctx
->priv
->alg_priv
->cfg
, ctx
->priv
->alg_priv
->vp8_cfg
);
524 optr
= vp8_create_compressor(&ctx
->priv
->alg_priv
->oxcf
);
527 res
= VPX_CODEC_MEM_ERROR
;
529 ctx
->priv
->alg_priv
->cpi
= optr
;
537 static vpx_codec_err_t
vp8e_destroy(vpx_codec_alg_priv_t
*ctx
)
541 vp8_remove_compressor(&ctx
->cpi
);
546 static vpx_codec_err_t
image2yuvconfig(const vpx_image_t
*img
,
547 YV12_BUFFER_CONFIG
*yv12
)
549 vpx_codec_err_t res
= VPX_CODEC_OK
;
550 yv12
->y_buffer
= img
->planes
[VPX_PLANE_Y
];
551 yv12
->u_buffer
= img
->planes
[VPX_PLANE_U
];
552 yv12
->v_buffer
= img
->planes
[VPX_PLANE_V
];
554 yv12
->y_width
= img
->d_w
;
555 yv12
->y_height
= img
->d_h
;
556 yv12
->uv_width
= (1 + yv12
->y_width
) / 2;
557 yv12
->uv_height
= (1 + yv12
->y_height
) / 2;
559 yv12
->y_stride
= img
->stride
[VPX_PLANE_Y
];
560 yv12
->uv_stride
= img
->stride
[VPX_PLANE_U
];
562 yv12
->border
= (img
->stride
[VPX_PLANE_Y
] - img
->w
) / 2;
563 yv12
->clrtype
= (img
->fmt
== VPX_IMG_FMT_VPXI420
|| img
->fmt
== VPX_IMG_FMT_VPXYV12
); //REG_YUV = 0
567 static void pick_quickcompress_mode(vpx_codec_alg_priv_t
*ctx
,
568 unsigned long duration
,
569 unsigned long deadline
)
573 #if !(CONFIG_REALTIME_ONLY)
574 /* Use best quality mode if no deadline is given. */
575 new_qc
= MODE_BESTQUALITY
;
579 uint64_t duration_us
;
581 /* Convert duration parameter from stream timebase to microseconds */
582 duration_us
= (uint64_t)duration
* 1000000
583 * (uint64_t)ctx
->cfg
.g_timebase
.num
584 / (uint64_t)ctx
->cfg
.g_timebase
.den
;
586 /* If the deadline is more that the duration this frame is to be shown,
587 * use good quality mode. Otherwise use realtime mode.
589 new_qc
= (deadline
> duration_us
) ? MODE_GOODQUALITY
: MODE_REALTIME
;
593 new_qc
= MODE_REALTIME
;
596 switch (ctx
->deprecated_mode
)
598 case VP8_BEST_QUALITY_ENCODING
:
599 new_qc
= MODE_BESTQUALITY
;
601 case VP8_GOOD_QUALITY_ENCODING
:
602 new_qc
= MODE_GOODQUALITY
;
604 case VP8_REAL_TIME_ENCODING
:
605 new_qc
= MODE_REALTIME
;
609 if (ctx
->cfg
.g_pass
== VPX_RC_FIRST_PASS
)
610 new_qc
= MODE_FIRSTPASS
;
611 else if (ctx
->cfg
.g_pass
== VPX_RC_LAST_PASS
)
612 new_qc
= (new_qc
== MODE_BESTQUALITY
)
613 ? MODE_SECONDPASS_BEST
616 if (ctx
->oxcf
.Mode
!= new_qc
)
618 ctx
->oxcf
.Mode
= new_qc
;
619 vp8_change_config(ctx
->cpi
, &ctx
->oxcf
);
624 static vpx_codec_err_t
vp8e_encode(vpx_codec_alg_priv_t
*ctx
,
625 const vpx_image_t
*img
,
627 unsigned long duration
,
628 vpx_enc_frame_flags_t flags
,
629 unsigned long deadline
)
631 vpx_codec_err_t res
= VPX_CODEC_OK
;
634 res
= validate_img(ctx
, img
);
636 pick_quickcompress_mode(ctx
, duration
, deadline
);
637 vpx_codec_pkt_list_init(&ctx
->pkt_list
);
640 if (((flags
& VP8_EFLAG_NO_UPD_GF
) && (flags
& VP8_EFLAG_FORCE_GF
))
641 || ((flags
& VP8_EFLAG_NO_UPD_ARF
) && (flags
& VP8_EFLAG_FORCE_ARF
)))
643 ctx
->base
.err_detail
= "Conflicting flags.";
644 return VPX_CODEC_INVALID_PARAM
;
647 if (flags
& (VP8_EFLAG_NO_REF_LAST
| VP8_EFLAG_NO_REF_GF
648 | VP8_EFLAG_NO_REF_ARF
))
652 if (flags
& VP8_EFLAG_NO_REF_LAST
)
653 ref
^= VP8_LAST_FLAG
;
655 if (flags
& VP8_EFLAG_NO_REF_GF
)
656 ref
^= VP8_GOLD_FLAG
;
658 if (flags
& VP8_EFLAG_NO_REF_ARF
)
661 vp8_use_as_reference(ctx
->cpi
, ref
);
664 if (flags
& (VP8_EFLAG_NO_UPD_LAST
| VP8_EFLAG_NO_UPD_GF
665 | VP8_EFLAG_NO_UPD_ARF
| VP8_EFLAG_FORCE_GF
666 | VP8_EFLAG_FORCE_ARF
))
670 if (flags
& VP8_EFLAG_NO_UPD_LAST
)
671 upd
^= VP8_LAST_FLAG
;
673 if (flags
& VP8_EFLAG_NO_UPD_GF
)
674 upd
^= VP8_GOLD_FLAG
;
676 if (flags
& VP8_EFLAG_NO_UPD_ARF
)
679 vp8_update_reference(ctx
->cpi
, upd
);
682 if (flags
& VP8_EFLAG_NO_UPD_ENTROPY
)
684 vp8_update_entropy(ctx
->cpi
, 0);
687 /* Handle fixed keyframe intervals */
688 if (ctx
->cfg
.kf_mode
== VPX_KF_AUTO
689 && ctx
->cfg
.kf_min_dist
== ctx
->cfg
.kf_max_dist
)
691 if (++ctx
->fixed_kf_cntr
> ctx
->cfg
.kf_min_dist
)
693 flags
|= VPX_EFLAG_FORCE_KF
;
694 ctx
->fixed_kf_cntr
= 0;
698 /* Initialize the encoder instance on the first frame*/
699 if (!res
&& ctx
->cpi
)
701 unsigned int lib_flags
;
702 YV12_BUFFER_CONFIG sd
;
703 INT64 dst_time_stamp
, dst_end_time_stamp
;
704 unsigned long size
, cx_data_sz
;
705 unsigned char *cx_data
;
707 /* Set up internal flags */
708 if (ctx
->base
.init_flags
& VPX_CODEC_USE_PSNR
)
709 ((VP8_COMP
*)ctx
->cpi
)->b_calculate_psnr
= 1;
711 /* Convert API flags to internal codec lib flags */
712 lib_flags
= (flags
& VPX_EFLAG_FORCE_KF
) ? FRAMEFLAGS_KEY
: 0;
714 /* vp8 use 10,000,000 ticks/second as time stamp */
715 dst_time_stamp
= pts
* 10000000 * ctx
->cfg
.g_timebase
.num
/ ctx
->cfg
.g_timebase
.den
;
716 dst_end_time_stamp
= (pts
+ duration
) * 10000000 * ctx
->cfg
.g_timebase
.num
/ ctx
->cfg
.g_timebase
.den
;
720 res
= image2yuvconfig(img
, &sd
);
722 if (vp8_receive_raw_frame(ctx
->cpi
, ctx
->next_frame_flag
| lib_flags
,
723 &sd
, dst_time_stamp
, dst_end_time_stamp
))
725 VP8_COMP
*cpi
= (VP8_COMP
*)ctx
->cpi
;
726 res
= update_error_state(ctx
, &cpi
->common
.error
);
729 /* reset for next frame */
730 ctx
->next_frame_flag
= 0;
733 cx_data
= ctx
->cx_data
;
734 cx_data_sz
= ctx
->cx_data_sz
;
737 while (cx_data_sz
>= ctx
->cx_data_sz
/ 2
738 && -1 != vp8_get_compressed_data(ctx
->cpi
, &lib_flags
, &size
, cx_data
, &dst_time_stamp
, &dst_end_time_stamp
, !img
))
742 vpx_codec_pts_t round
, delta
;
743 vpx_codec_cx_pkt_t pkt
;
744 VP8_COMP
*cpi
= (VP8_COMP
*)ctx
->cpi
;
746 /* Add the frame packet to the list of returned packets. */
747 round
= 1000000 * ctx
->cfg
.g_timebase
.num
/ 2 - 1;
748 delta
= (dst_end_time_stamp
- dst_time_stamp
);
749 pkt
.kind
= VPX_CODEC_CX_FRAME_PKT
;
750 pkt
.data
.frame
.buf
= cx_data
;
751 pkt
.data
.frame
.sz
= size
;
753 (dst_time_stamp
* ctx
->cfg
.g_timebase
.den
+ round
)
754 / ctx
->cfg
.g_timebase
.num
/ 10000000;
755 pkt
.data
.frame
.duration
=
756 (delta
* ctx
->cfg
.g_timebase
.den
+ round
)
757 / ctx
->cfg
.g_timebase
.num
/ 10000000;
758 pkt
.data
.frame
.flags
= lib_flags
<< 16;
760 if (lib_flags
& FRAMEFLAGS_KEY
)
761 pkt
.data
.frame
.flags
|= VPX_FRAME_IS_KEY
;
763 if (!cpi
->common
.show_frame
)
765 pkt
.data
.frame
.flags
|= VPX_FRAME_IS_INVISIBLE
;
767 // This timestamp should be as close as possible to the
768 // prior PTS so that if a decoder uses pts to schedule when
769 // to do this, we start right after last frame was decoded.
770 // Invisible frames have no duration.
771 pkt
.data
.frame
.pts
= ((cpi
->last_time_stamp_seen
772 * ctx
->cfg
.g_timebase
.den
+ round
)
773 / ctx
->cfg
.g_timebase
.num
/ 10000000) + 1;
774 pkt
.data
.frame
.duration
= 0;
777 vpx_codec_pkt_list_add(&ctx
->pkt_list
.head
, &pkt
);
779 //printf("timestamp: %lld, duration: %d\n", pkt->data.frame.pts, pkt->data.frame.duration);
790 static const vpx_codec_cx_pkt_t
*vp8e_get_cxdata(vpx_codec_alg_priv_t
*ctx
,
791 vpx_codec_iter_t
*iter
)
793 return vpx_codec_pkt_list_get(&ctx
->pkt_list
.head
, iter
);
796 static vpx_codec_err_t
vp8e_set_reference(vpx_codec_alg_priv_t
*ctx
,
800 vpx_ref_frame_t
*data
= va_arg(args
, vpx_ref_frame_t
*);
804 vpx_ref_frame_t
*frame
= (vpx_ref_frame_t
*)data
;
805 YV12_BUFFER_CONFIG sd
;
807 image2yuvconfig(&frame
->img
, &sd
);
808 vp8_set_reference(ctx
->cpi
, frame
->frame_type
, &sd
);
812 return VPX_CODEC_INVALID_PARAM
;
816 static vpx_codec_err_t
vp8e_get_reference(vpx_codec_alg_priv_t
*ctx
,
821 vpx_ref_frame_t
*data
= va_arg(args
, vpx_ref_frame_t
*);
825 vpx_ref_frame_t
*frame
= (vpx_ref_frame_t
*)data
;
826 YV12_BUFFER_CONFIG sd
;
828 image2yuvconfig(&frame
->img
, &sd
);
829 vp8_get_reference(ctx
->cpi
, frame
->frame_type
, &sd
);
833 return VPX_CODEC_INVALID_PARAM
;
836 static vpx_codec_err_t
vp8e_set_previewpp(vpx_codec_alg_priv_t
*ctx
,
841 vp8_postproc_cfg_t
*data
= va_arg(args
, vp8_postproc_cfg_t
*);
846 ctx
->preview_ppcfg
= *((vp8_postproc_cfg_t
*)data
);
850 return VPX_CODEC_INVALID_PARAM
;
855 return VPX_CODEC_INCAPABLE
;
860 static vpx_image_t
*vp8e_get_preview(vpx_codec_alg_priv_t
*ctx
)
863 YV12_BUFFER_CONFIG sd
;
864 vp8_ppflags_t flags
= {0};
866 if (ctx
->preview_ppcfg
.post_proc_flag
)
868 flags
.post_proc_flag
= ctx
->preview_ppcfg
.post_proc_flag
;
869 flags
.deblocking_level
= ctx
->preview_ppcfg
.deblocking_level
;
870 flags
.noise_level
= ctx
->preview_ppcfg
.noise_level
;
873 if (0 == vp8_get_preview_raw_frame(ctx
->cpi
, &sd
, &flags
))
877 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
878 sd.y_width + 2*VP8BORDERINPIXELS,
879 sd.y_height + 2*VP8BORDERINPIXELS,
882 vpx_img_set_rect(&ctx->preview_img,
883 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
884 sd.y_width, sd.y_height);
887 ctx
->preview_img
.bps
= 12;
888 ctx
->preview_img
.planes
[VPX_PLANE_Y
] = sd
.y_buffer
;
889 ctx
->preview_img
.planes
[VPX_PLANE_U
] = sd
.u_buffer
;
890 ctx
->preview_img
.planes
[VPX_PLANE_V
] = sd
.v_buffer
;
892 if (sd
.clrtype
== REG_YUV
)
893 ctx
->preview_img
.fmt
= VPX_IMG_FMT_I420
;
895 ctx
->preview_img
.fmt
= VPX_IMG_FMT_VPXI420
;
897 ctx
->preview_img
.x_chroma_shift
= 1;
898 ctx
->preview_img
.y_chroma_shift
= 1;
900 ctx
->preview_img
.d_w
= ctx
->cfg
.g_w
;
901 ctx
->preview_img
.d_h
= ctx
->cfg
.g_h
;
902 ctx
->preview_img
.stride
[VPX_PLANE_Y
] = sd
.y_stride
;
903 ctx
->preview_img
.stride
[VPX_PLANE_U
] = sd
.uv_stride
;
904 ctx
->preview_img
.stride
[VPX_PLANE_V
] = sd
.uv_stride
;
905 ctx
->preview_img
.w
= sd
.y_width
;
906 ctx
->preview_img
.h
= sd
.y_height
;
908 return &ctx
->preview_img
;
914 static vpx_codec_err_t
vp8e_update_entropy(vpx_codec_alg_priv_t
*ctx
,
918 int update
= va_arg(args
, int);
919 vp8_update_entropy(ctx
->cpi
, update
);
924 static vpx_codec_err_t
vp8e_update_reference(vpx_codec_alg_priv_t
*ctx
,
928 int update
= va_arg(args
, int);
929 vp8_update_reference(ctx
->cpi
, update
);
933 static vpx_codec_err_t
vp8e_use_reference(vpx_codec_alg_priv_t
*ctx
,
937 int reference_flag
= va_arg(args
, int);
938 vp8_use_as_reference(ctx
->cpi
, reference_flag
);
942 static vpx_codec_err_t
vp8e_set_roi_map(vpx_codec_alg_priv_t
*ctx
,
946 vpx_roi_map_t
*data
= va_arg(args
, vpx_roi_map_t
*);
950 vpx_roi_map_t
*roi
= (vpx_roi_map_t
*)data
;
952 if (!vp8_set_roimap(ctx
->cpi
, roi
->roi_map
, roi
->rows
, roi
->cols
, roi
->delta_q
, roi
->delta_lf
, roi
->static_threshold
))
955 return VPX_CODEC_INVALID_PARAM
;
958 return VPX_CODEC_INVALID_PARAM
;
962 static vpx_codec_err_t
vp8e_set_activemap(vpx_codec_alg_priv_t
*ctx
,
966 vpx_active_map_t
*data
= va_arg(args
, vpx_active_map_t
*);
971 vpx_active_map_t
*map
= (vpx_active_map_t
*)data
;
973 if (!vp8_set_active_map(ctx
->cpi
, map
->active_map
, map
->rows
, map
->cols
))
976 return VPX_CODEC_INVALID_PARAM
;
979 return VPX_CODEC_INVALID_PARAM
;
982 static vpx_codec_err_t
vp8e_set_scalemode(vpx_codec_alg_priv_t
*ctx
,
987 vpx_scaling_mode_t
*data
= va_arg(args
, vpx_scaling_mode_t
*);
992 vpx_scaling_mode_t scalemode
= *(vpx_scaling_mode_t
*)data
;
993 res
= vp8_set_internal_size(ctx
->cpi
, scalemode
.h_scaling_mode
, scalemode
.v_scaling_mode
);
997 /*force next frame a key frame to effect scaling mode */
998 ctx
->next_frame_flag
|= FRAMEFLAGS_KEY
;
1002 return VPX_CODEC_INVALID_PARAM
;
1005 return VPX_CODEC_INVALID_PARAM
;
1009 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps
[] =
1011 {VP8_SET_REFERENCE
, vp8e_set_reference
},
1012 {VP8_COPY_REFERENCE
, vp8e_get_reference
},
1013 {VP8_SET_POSTPROC
, vp8e_set_previewpp
},
1014 {VP8E_UPD_ENTROPY
, vp8e_update_entropy
},
1015 {VP8E_UPD_REFERENCE
, vp8e_update_reference
},
1016 {VP8E_USE_REFERENCE
, vp8e_use_reference
},
1017 {VP8E_SET_ROI_MAP
, vp8e_set_roi_map
},
1018 {VP8E_SET_ACTIVEMAP
, vp8e_set_activemap
},
1019 {VP8E_SET_SCALEMODE
, vp8e_set_scalemode
},
1020 {VP8E_SET_ENCODING_MODE
, set_param
},
1021 {VP8E_SET_CPUUSED
, set_param
},
1022 {VP8E_SET_NOISE_SENSITIVITY
, set_param
},
1023 {VP8E_SET_ENABLEAUTOALTREF
, set_param
},
1024 {VP8E_SET_SHARPNESS
, set_param
},
1025 {VP8E_SET_STATIC_THRESHOLD
, set_param
},
1026 {VP8E_SET_TOKEN_PARTITIONS
, set_param
},
1027 {VP8E_GET_LAST_QUANTIZER
, get_param
},
1028 {VP8E_GET_LAST_QUANTIZER_64
, get_param
},
1029 {VP8E_SET_ARNR_MAXFRAMES
, set_param
},
1030 {VP8E_SET_ARNR_STRENGTH
, set_param
},
1031 {VP8E_SET_ARNR_TYPE
, set_param
},
1035 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map
[] =
1046 {1, 30}, /* g_timebase */
1048 0, /* g_error_resilient */
1050 VPX_RC_ONE_PASS
, /* g_pass */
1052 0, /* g_lag_in_frames */
1054 0, /* rc_dropframe_thresh */
1055 0, /* rc_resize_allowed */
1056 60, /* rc_resize_down_thresold */
1057 30, /* rc_resize_up_thresold */
1059 VPX_VBR
, /* rc_end_usage */
1060 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1061 {0}, /* rc_twopass_stats_in */
1063 256, /* rc_target_bandwidth */
1065 4, /* rc_min_quantizer */
1066 63, /* rc_max_quantizer */
1068 95, /* rc_undershoot_pct */
1069 200, /* rc_overshoot_pct */
1071 6000, /* rc_max_buffer_size */
1072 4000, /* rc_buffer_initial_size; */
1073 5000, /* rc_buffer_optimal_size; */
1075 50, /* rc_two_pass_vbrbias */
1076 0, /* rc_two_pass_vbrmin_section */
1077 400, /* rc_two_pass_vbrmax_section */
1079 /* keyframing settings (kf) */
1080 VPX_KF_AUTO
, /* g_kfmode*/
1081 0, /* kf_min_dist */
1082 9999, /* kf_max_dist */
1084 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
1085 1, /* g_delete_first_pass_file */
1086 "vp8.fpf" /* first pass filename */
1089 { -1, {NOT_IMPLEMENTED
}}
1093 #ifndef VERSION_STRING
1094 #define VERSION_STRING
1096 CODEC_INTERFACE(vpx_codec_vp8_cx
) =
1098 "WebM Project VP8 Encoder" VERSION_STRING
,
1099 VPX_CODEC_INTERNAL_ABI_VERSION
,
1100 VPX_CODEC_CAP_ENCODER
| VPX_CODEC_CAP_PSNR
,
1101 /* vpx_codec_caps_t caps; */
1102 vp8e_init
, /* vpx_codec_init_fn_t init; */
1103 vp8e_destroy
, /* vpx_codec_destroy_fn_t destroy; */
1104 vp8e_ctf_maps
, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1105 NOT_IMPLEMENTED
, /* vpx_codec_get_mmap_fn_t get_mmap; */
1106 NOT_IMPLEMENTED
, /* vpx_codec_set_mmap_fn_t set_mmap; */
1108 NOT_IMPLEMENTED
, /* vpx_codec_peek_si_fn_t peek_si; */
1109 NOT_IMPLEMENTED
, /* vpx_codec_get_si_fn_t get_si; */
1110 NOT_IMPLEMENTED
, /* vpx_codec_decode_fn_t decode; */
1111 NOT_IMPLEMENTED
, /* vpx_codec_frame_get_fn_t frame_get; */
1114 vp8e_usage_cfg_map
, /* vpx_codec_enc_cfg_map_t peek_si; */
1115 vp8e_encode
, /* vpx_codec_encode_fn_t encode; */
1116 vp8e_get_cxdata
, /* vpx_codec_get_cx_data_fn_t frame_get; */
1120 } /* encoder functions */
1125 * BEGIN BACKWARDS COMPATIBILITY SHIM.
1128 static vpx_codec_err_t
api1_control(vpx_codec_alg_priv_t
*ctx
,
1132 vpx_codec_ctrl_fn_map_t
*entry
;
1136 case VP8E_SET_FLUSHFLAG
:
1137 /* VP8 sample code did VP8E_SET_FLUSHFLAG followed by
1138 * vpx_codec_get_cx_data() rather than vpx_codec_encode().
1140 return vp8e_encode(ctx
, NULL
, 0, 0, 0, 0);
1141 case VP8E_SET_FRAMETYPE
:
1142 ctx
->base
.enc
.tbd
|= FORCE_KEY
;
1143 return VPX_CODEC_OK
;
1146 for (entry
= vp8e_ctf_maps
; entry
&& entry
->fn
; entry
++)
1148 if (!entry
->ctrl_id
|| entry
->ctrl_id
== ctrl_id
)
1150 return entry
->fn(ctx
, ctrl_id
, args
);
1154 return VPX_CODEC_ERROR
;
1158 static vpx_codec_ctrl_fn_map_t api1_ctrl_maps
[] =
1165 static vpx_codec_err_t
api1_encode(vpx_codec_alg_priv_t
*ctx
,
1166 const vpx_image_t
*img
,
1167 vpx_codec_pts_t pts
,
1168 unsigned long duration
,
1169 vpx_enc_frame_flags_t flags
,
1170 unsigned long deadline
)
1172 int force
= ctx
->base
.enc
.tbd
;
1174 ctx
->base
.enc
.tbd
= 0;
1180 flags
| ((force
& FORCE_KEY
) ? VPX_EFLAG_FORCE_KF
: 0),
1185 vpx_codec_iface_t vpx_enc_vp8_algo
=
1187 "WebM Project VP8 Encoder (Deprecated API)" VERSION_STRING
,
1188 VPX_CODEC_INTERNAL_ABI_VERSION
,
1189 VPX_CODEC_CAP_ENCODER
,
1190 /* vpx_codec_caps_t caps; */
1191 vp8e_init
, /* vpx_codec_init_fn_t init; */
1192 vp8e_destroy
, /* vpx_codec_destroy_fn_t destroy; */
1193 api1_ctrl_maps
, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1194 NOT_IMPLEMENTED
, /* vpx_codec_get_mmap_fn_t get_mmap; */
1195 NOT_IMPLEMENTED
, /* vpx_codec_set_mmap_fn_t set_mmap; */
1196 {NOT_IMPLEMENTED
}, /* decoder functions */
1198 vp8e_usage_cfg_map
, /* vpx_codec_enc_cfg_map_t peek_si; */
1199 api1_encode
, /* vpx_codec_encode_fn_t encode; */
1200 vp8e_get_cxdata
, /* vpx_codec_get_cx_data_fn_t frame_get; */
1204 } /* encoder functions */