2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
11 #include "vpx_codec/vpx_codec.h"
12 #include "vpx_codec/internal/vpx_codec_internal.h"
13 #include "vpx_version.h"
20 /* This value is a sentinel for determining whether the user has set a mode
21 * directly through the deprecated VP8E_SET_ENCODING_MODE control.
23 #define NO_MODE_SET 255
27 struct vpx_codec_pkt_list *pkt_list;
28 vp8e_encoding_mode encoding_mode; /** best, good, realtime */
29 int cpu_used; /** available cpu percentage in 1/16*/
30 unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */
31 unsigned int noise_sensitivity;
32 unsigned int Sharpness;
33 unsigned int static_thresh;
34 unsigned int token_partitions;
35 unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
36 unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
37 unsigned int arnr_type; /* alt_ref filter type */
41 struct extraconfig_map
44 struct vp8_extracfg cfg;
47 static const struct extraconfig_map extracfg_map[] =
53 #if !(CONFIG_REALTIME_ONLY)
54 VP8_BEST_QUALITY_ENCODING, /* Encoding Mode */
57 VP8_REAL_TIME_ENCODING, /* Encoding Mode */
60 0, /* enable_auto_alt_ref */
61 0, /* noise_sensitivity */
63 800, /* static_thresh */
64 VP8_ONE_TOKENPARTITION, /* token_partitions */
65 0, /* arnr_max_frames */
66 0, /* arnr_strength */
72 struct vpx_codec_alg_priv
74 vpx_codec_priv_t base;
75 vpx_codec_enc_cfg_t cfg;
76 struct vp8_extracfg vp8_cfg;
79 unsigned char *cx_data;
80 unsigned int cx_data_sz;
81 vpx_image_t preview_img;
82 unsigned int next_frame_flag;
83 vp8_postproc_cfg_t preview_ppcfg;
84 vpx_codec_pkt_list_decl(26) pkt_list; // changed to accomendate the maximum number of lagged frames allowed
86 unsigned int fixed_kf_cntr;
90 static vpx_codec_err_t
91 update_error_state(vpx_codec_alg_priv_t *ctx,
92 const struct vpx_internal_error_info *error)
96 if ((res = error->error_code))
97 ctx->base.err_detail = error->has_detail
105 #define ERROR(str) do {\
106 ctx->base.err_detail = str;\
107 return VPX_CODEC_INVALID_PARAM;\
110 #define RANGE_CHECK(p,memb,lo,hi) do {\
111 if(!((p)->memb >= (lo) && (p)->memb <= hi)) \
112 ERROR(#memb " out of range ["#lo".."#hi"]");\
115 #define RANGE_CHECK_LO(p,memb,lo) do {\
116 if(!((p)->memb >= (lo))) \
117 ERROR(#memb " out of range ["#lo"..]");\
120 #define RANGE_CHECK_BOOL(p,memb) do {\
121 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
124 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
125 const vpx_codec_enc_cfg_t *cfg,
126 const struct vp8_extracfg *vp8_cfg)
128 RANGE_CHECK(cfg, g_w, 2, 16384);
129 RANGE_CHECK(cfg, g_h, 2, 16384);
130 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
131 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
132 RANGE_CHECK(cfg, g_profile, 0, 3);
133 RANGE_CHECK(cfg, rc_min_quantizer, 0, 63);
134 RANGE_CHECK(cfg, rc_max_quantizer, 0, 63);
135 RANGE_CHECK(cfg, g_threads, 0, 64);
136 #if !(CONFIG_REALTIME_ONLY)
137 RANGE_CHECK(cfg, g_lag_in_frames, 0, 25);
139 RANGE_CHECK(cfg, g_lag_in_frames, 0, 0);
141 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_CBR);
142 RANGE_CHECK(cfg, rc_undershoot_pct, 0, 100);
143 RANGE_CHECK(cfg, rc_2pass_vbr_bias_pct, 0, 100);
144 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
145 //RANGE_CHECK_BOOL(cfg, g_delete_firstpassfile);
146 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
147 RANGE_CHECK(cfg, rc_dropframe_thresh, 0, 100);
148 RANGE_CHECK(cfg, rc_resize_up_thresh, 0, 100);
149 RANGE_CHECK(cfg, rc_resize_down_thresh, 0, 100);
150 #if !(CONFIG_REALTIME_ONLY)
151 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
153 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
156 /* VP8 does not support a lower bound on the keyframe interval in
157 * automatic keyframe placement mode.
159 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
160 && cfg->kf_min_dist > 0)
161 ERROR("kf_min_dist not supported in auto mode, use 0 "
162 "or kf_max_dist instead.");
164 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
165 #if !(CONFIG_REALTIME_ONLY)
166 RANGE_CHECK(vp8_cfg, encoding_mode, VP8_BEST_QUALITY_ENCODING, VP8_REAL_TIME_ENCODING);
167 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
168 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 6);
170 RANGE_CHECK(vp8_cfg, encoding_mode, VP8_REAL_TIME_ENCODING, VP8_REAL_TIME_ENCODING);
172 if (!((vp8_cfg->cpu_used >= -16 && vp8_cfg->cpu_used <= -4) || (vp8_cfg->cpu_used >= 4 && vp8_cfg->cpu_used <= 16)))
173 ERROR("cpu_used out of range [-16..-4] or [4..16]");
175 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
178 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION, VP8_EIGHT_TOKENPARTITION);
179 RANGE_CHECK(vp8_cfg, Sharpness, 0, 7);
180 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 25);
181 RANGE_CHECK(vp8_cfg, arnr_strength, 0, 6);
182 RANGE_CHECK(vp8_cfg, arnr_type, 0, 0xffffffff);
184 if (cfg->g_pass == VPX_RC_LAST_PASS)
186 int n_doubles = cfg->rc_twopass_stats_in.sz / sizeof(double);
187 int n_packets = cfg->rc_twopass_stats_in.sz / sizeof(FIRSTPASS_STATS);
190 if (!cfg->rc_twopass_stats_in.buf)
191 ERROR("rc_twopass_stats_in.buf not set.");
193 if (cfg->rc_twopass_stats_in.sz % sizeof(FIRSTPASS_STATS))
194 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
196 if (cfg->rc_twopass_stats_in.sz < 2 * sizeof(FIRSTPASS_STATS))
197 ERROR("rc_twopass_stats_in requires at least two packets.");
199 frames = ((double *)cfg->rc_twopass_stats_in.buf)[n_doubles - 1];
201 if ((int)(frames + 0.5) != n_packets - 1)
202 ERROR("rc_twopass_stats_in missing EOS stats packet");
209 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
210 const vpx_image_t *img)
214 case VPX_IMG_FMT_YV12:
215 case VPX_IMG_FMT_I420:
216 case VPX_IMG_FMT_VPXI420:
217 case VPX_IMG_FMT_VPXYV12:
220 ERROR("Invalid image format. Only YV12 and I420 images are supported");
223 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
224 ERROR("Image size must match encoder init configuration size");
230 static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
231 vpx_codec_enc_cfg_t cfg,
232 struct vp8_extracfg vp8_cfg)
234 oxcf->multi_threaded = cfg.g_threads;
235 oxcf->Version = cfg.g_profile;
237 oxcf->Width = cfg.g_w;
238 oxcf->Height = cfg.g_h;
239 /* guess a frame rate if out of whack, use 30 */
240 oxcf->frame_rate = (double)(cfg.g_timebase.den) / (double)(cfg.g_timebase.num);
242 if (oxcf->frame_rate > 180)
244 oxcf->frame_rate = 30;
247 oxcf->error_resilient_mode = cfg.g_error_resilient;
251 case VPX_RC_ONE_PASS:
252 oxcf->Mode = MODE_BESTQUALITY;
254 case VPX_RC_FIRST_PASS:
255 oxcf->Mode = MODE_FIRSTPASS;
257 case VPX_RC_LAST_PASS:
258 oxcf->Mode = MODE_SECONDPASS_BEST;
262 if (cfg.g_pass == VPX_RC_FIRST_PASS)
265 oxcf->lag_in_frames = 0;
269 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
270 oxcf->lag_in_frames = cfg.g_lag_in_frames;
273 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
274 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
276 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
277 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
278 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
280 if (cfg.rc_end_usage == VPX_VBR)
282 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
284 else if (cfg.rc_end_usage == VPX_CBR)
286 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
289 oxcf->target_bandwidth = cfg.rc_target_bitrate;
291 oxcf->best_allowed_q = cfg.rc_min_quantizer;
292 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
295 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
296 //oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
298 oxcf->maximum_buffer_size = cfg.rc_buf_sz / 1000;
299 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz / 1000;
300 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz / 1000;
302 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
303 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
304 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
306 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
307 && cfg.kf_min_dist != cfg.kf_max_dist;
308 //oxcf->kf_min_dist = cfg.kf_min_dis;
309 oxcf->key_freq = cfg.kf_max_dist;
311 //oxcf->delete_first_pass_file = cfg.g_delete_firstpassfile;
312 //strcpy(oxcf->first_pass_file, cfg.g_firstpass_file);
314 oxcf->cpu_used = vp8_cfg.cpu_used;
315 oxcf->encode_breakout = vp8_cfg.static_thresh;
316 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
317 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
318 oxcf->Sharpness = vp8_cfg.Sharpness;
319 oxcf->token_partitions = vp8_cfg.token_partitions;
321 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
322 oxcf->output_pkt_list = vp8_cfg.pkt_list;
324 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
325 oxcf->arnr_strength = vp8_cfg.arnr_strength;
326 oxcf->arnr_type = vp8_cfg.arnr_type;
330 printf("Current VP8 Settings: \n");
331 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
332 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
333 printf("Sharpness: %d\n", oxcf->Sharpness);
334 printf("cpu_used: %d\n", oxcf->cpu_used);
335 printf("Mode: %d\n", oxcf->Mode);
336 printf("delete_first_pass_file: %d\n", oxcf->delete_first_pass_file);
337 printf("auto_key: %d\n", oxcf->auto_key);
338 printf("key_freq: %d\n", oxcf->key_freq);
339 printf("end_usage: %d\n", oxcf->end_usage);
340 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
341 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
342 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
343 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
344 printf("fixed_q: %d\n", oxcf->fixed_q);
345 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
346 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
347 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
348 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
349 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
350 printf("allow_df: %d\n", oxcf->allow_df);
351 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
352 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
353 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
354 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
355 printf("allow_lag: %d\n", oxcf->allow_lag);
356 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
357 printf("play_alternate: %d\n", oxcf->play_alternate);
358 printf("Version: %d\n", oxcf->Version);
359 printf("multi_threaded: %d\n", oxcf->multi_threaded);
360 printf("encode_breakout: %d\n", oxcf->encode_breakout);
365 static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
366 const vpx_codec_enc_cfg_t *cfg)
370 if ((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h))
371 ERROR("Cannot change width or height after initialization");
373 /* Prevent increasing lag_in_frames. This check is stricter than it needs
374 * to be -- the limit is not increasing past the first lag_in_frames
375 * value, but we don't track the initial config, only the last successful
378 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
379 ERROR("Cannot increase lag_in_frames");
381 res = validate_config(ctx, cfg, &ctx->vp8_cfg);
386 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
387 vp8_change_config(ctx->cpi, &ctx->oxcf);
394 int vp8_reverse_trans(int);
397 static vpx_codec_err_t get_param(vpx_codec_alg_priv_t *ctx,
401 void *arg = va_arg(args, void *);
403 #define MAP(id, var) case id: *(RECAST(id, arg)) = var; break
406 return VPX_CODEC_INVALID_PARAM;
410 MAP(VP8E_GET_LAST_QUANTIZER, vp8_get_quantizer(ctx->cpi));
411 MAP(VP8E_GET_LAST_QUANTIZER_64, vp8_reverse_trans(vp8_get_quantizer(ctx->cpi)));
419 static vpx_codec_err_t set_param(vpx_codec_alg_priv_t *ctx,
423 vpx_codec_err_t res = VPX_CODEC_OK;
424 struct vp8_extracfg xcfg = ctx->vp8_cfg;
426 #define MAP(id, var) case id: var = CAST(id, args); break;
430 MAP(VP8E_SET_ENCODING_MODE, ctx->deprecated_mode);
431 MAP(VP8E_SET_CPUUSED, xcfg.cpu_used);
432 MAP(VP8E_SET_ENABLEAUTOALTREF, xcfg.enable_auto_alt_ref);
433 MAP(VP8E_SET_NOISE_SENSITIVITY, xcfg.noise_sensitivity);
434 MAP(VP8E_SET_SHARPNESS, xcfg.Sharpness);
435 MAP(VP8E_SET_STATIC_THRESHOLD, xcfg.static_thresh);
436 MAP(VP8E_SET_TOKEN_PARTITIONS, xcfg.token_partitions);
438 MAP(VP8E_SET_ARNR_MAXFRAMES, xcfg.arnr_max_frames);
439 MAP(VP8E_SET_ARNR_STRENGTH , xcfg.arnr_strength);
440 MAP(VP8E_SET_ARNR_TYPE , xcfg.arnr_type);
444 res = validate_config(ctx, &ctx->cfg, &xcfg);
449 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
450 vp8_change_config(ctx->cpi, &ctx->oxcf);
456 static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx)
458 vpx_codec_err_t res = VPX_DEC_OK;
459 struct vpx_codec_alg_priv *priv;
460 vpx_codec_enc_cfg_t *cfg;
467 priv = calloc(1, sizeof(struct vpx_codec_alg_priv));
471 ctx->priv = &priv->base;
472 ctx->priv->sz = sizeof(*ctx->priv);
473 ctx->priv->iface = ctx->iface;
474 ctx->priv->alg_priv = priv;
475 ctx->priv->init_flags = ctx->init_flags;
479 /* Update the reference to the config structure to an
482 ctx->priv->alg_priv->cfg = *ctx->config.enc;
483 ctx->config.enc = &ctx->priv->alg_priv->cfg;
486 cfg = &ctx->priv->alg_priv->cfg;
488 /* Select the extra vp6 configuration table based on the current
489 * usage value. If the current usage value isn't found, use the
490 * values for usage case 0.
493 extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage;
496 priv->vp8_cfg = extracfg_map[i].cfg;
497 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
499 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
501 if (priv->cx_data_sz < 4096) priv->cx_data_sz = 4096;
503 priv->cx_data = malloc(priv->cx_data_sz);
504 priv->deprecated_mode = NO_MODE_SET;
508 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg);
512 set_vp8e_config(&ctx->priv->alg_priv->oxcf, ctx->priv->alg_priv->cfg, ctx->priv->alg_priv->vp8_cfg);
513 optr = vp8_create_compressor(&ctx->priv->alg_priv->oxcf);
516 res = VPX_CODEC_MEM_ERROR;
518 ctx->priv->alg_priv->cpi = optr;
526 static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
530 vp8_remove_compressor(&ctx->cpi);
535 static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
536 YV12_BUFFER_CONFIG *yv12)
538 vpx_codec_err_t res = VPX_CODEC_OK;
539 yv12->y_buffer = img->planes[PLANE_Y];
540 yv12->u_buffer = img->planes[PLANE_U];
541 yv12->v_buffer = img->planes[PLANE_V];
543 yv12->y_width = img->d_w;
544 yv12->y_height = img->d_h;
545 yv12->uv_width = (1 + yv12->y_width) / 2;
546 yv12->uv_height = (1 + yv12->y_height) / 2;
548 yv12->y_stride = img->stride[PLANE_Y];
549 yv12->uv_stride = img->stride[PLANE_U];
551 yv12->border = (img->stride[PLANE_Y] - img->w) / 2;
552 yv12->clrtype = (img->fmt == VPX_IMG_FMT_VPXI420 || img->fmt == VPX_IMG_FMT_VPXYV12); //REG_YUV = 0
556 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
557 unsigned long duration,
558 unsigned long deadline)
562 #if !(CONFIG_REALTIME_ONLY)
563 /* Use best quality mode if no deadline is given. */
564 new_qc = MODE_BESTQUALITY;
568 uint64_t duration_us;
570 /* Convert duration parameter from stream timebase to microseconds */
571 duration_us = (uint64_t)duration * 1000000
572 * (uint64_t)ctx->cfg.g_timebase.num
573 / (uint64_t)ctx->cfg.g_timebase.den;
575 /* If the deadline is more that the duration this frame is to be shown,
576 * use good quality mode. Otherwise use realtime mode.
578 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
582 new_qc = MODE_REALTIME;
585 switch (ctx->deprecated_mode)
587 case VP8_BEST_QUALITY_ENCODING:
588 new_qc = MODE_BESTQUALITY;
590 case VP8_GOOD_QUALITY_ENCODING:
591 new_qc = MODE_GOODQUALITY;
593 case VP8_REAL_TIME_ENCODING:
594 new_qc = MODE_REALTIME;
598 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
599 new_qc = MODE_FIRSTPASS;
600 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
601 new_qc = (new_qc == MODE_BESTQUALITY)
602 ? MODE_SECONDPASS_BEST
605 if (ctx->oxcf.Mode != new_qc)
607 ctx->oxcf.Mode = new_qc;
608 vp8_change_config(ctx->cpi, &ctx->oxcf);
613 static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
614 const vpx_image_t *img,
616 unsigned long duration,
617 vpx_enc_frame_flags_t flags,
618 unsigned long deadline)
620 vpx_codec_err_t res = VPX_CODEC_OK;
623 res = validate_img(ctx, img);
625 pick_quickcompress_mode(ctx, duration, deadline);
626 vpx_codec_pkt_list_init(&ctx->pkt_list);
629 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
630 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
632 ctx->base.err_detail = "Conflicting flags.";
633 return VPX_CODEC_INVALID_PARAM;
636 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
637 | VP8_EFLAG_NO_REF_ARF))
641 if (flags & VP8_EFLAG_NO_REF_LAST)
642 ref ^= VP8_LAST_FLAG;
644 if (flags & VP8_EFLAG_NO_REF_GF)
645 ref ^= VP8_GOLD_FLAG;
647 if (flags & VP8_EFLAG_NO_REF_ARF)
650 vp8_use_as_reference(ctx->cpi, ref);
653 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
654 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
655 | VP8_EFLAG_FORCE_ARF))
659 if (flags & VP8_EFLAG_NO_UPD_LAST)
660 upd ^= VP8_LAST_FLAG;
662 if (flags & VP8_EFLAG_NO_UPD_GF)
663 upd ^= VP8_GOLD_FLAG;
665 if (flags & VP8_EFLAG_NO_UPD_ARF)
668 vp8_update_reference(ctx->cpi, upd);
671 if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
673 vp8_update_entropy(ctx->cpi, 0);
676 /* Handle fixed keyframe intervals */
677 if (ctx->cfg.kf_mode == VPX_KF_AUTO
678 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
680 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
682 flags |= VPX_EFLAG_FORCE_KF;
683 ctx->fixed_kf_cntr = 0;
687 /* Initialize the encoder instance on the first frame*/
688 if (!res && ctx->cpi)
690 unsigned int lib_flags;
691 YV12_BUFFER_CONFIG sd;
692 INT64 dst_time_stamp, dst_end_time_stamp;
693 unsigned long size, cx_data_sz;
694 unsigned char *cx_data;
696 /* Set up internal flags */
697 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
698 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
700 /* Convert API flags to internal codec lib flags */
701 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
703 /* vp8 use 10,000,000 ticks/second as time stamp */
704 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
705 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
709 res = image2yuvconfig(img, &sd);
711 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
712 &sd, dst_time_stamp, dst_end_time_stamp))
714 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
715 res = update_error_state(ctx, &cpi->common.error);
718 /* reset for next frame */
719 ctx->next_frame_flag = 0;
722 cx_data = ctx->cx_data;
723 cx_data_sz = ctx->cx_data_sz;
726 while (cx_data_sz >= ctx->cx_data_sz / 2
727 && -1 != vp8_get_compressed_data(ctx->cpi, &lib_flags, &size, cx_data, &dst_time_stamp, &dst_end_time_stamp, !img))
731 vpx_codec_pts_t round, delta;
732 vpx_codec_cx_pkt_t pkt;
733 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
735 /* Add the frame packet to the list of returned packets. */
736 round = 1000000 * ctx->cfg.g_timebase.num / 2 - 1;
737 delta = (dst_end_time_stamp - dst_time_stamp);
738 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
739 pkt.data.frame.buf = cx_data;
740 pkt.data.frame.sz = size;
742 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
743 / ctx->cfg.g_timebase.num / 10000000;
744 pkt.data.frame.duration =
745 (delta * ctx->cfg.g_timebase.den + round)
746 / ctx->cfg.g_timebase.num / 10000000;
747 pkt.data.frame.flags = lib_flags << 16;
749 if (lib_flags & FRAMEFLAGS_KEY)
750 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
752 if (!cpi->common.show_frame)
754 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
756 // TODO: ideally this timestamp should be as close as
757 // possible to the prior PTS so that if a decoder uses
758 // pts to schedule when to do this, we start right after
759 // last frame was decoded. Maybe should be set to
760 // last time stamp. Invisible frames have no duration..
761 pkt.data.frame.pts --;
762 pkt.data.frame.duration = 0;
765 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
767 //printf("timestamp: %lld, duration: %d\n", pkt->data.frame.pts, pkt->data.frame.duration);
778 static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
779 vpx_codec_iter_t *iter)
781 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
784 static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
788 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
792 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
793 YV12_BUFFER_CONFIG sd;
795 image2yuvconfig(&frame->img, &sd);
796 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
800 return VPX_CODEC_INVALID_PARAM;
804 static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
809 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
813 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
814 YV12_BUFFER_CONFIG sd;
816 image2yuvconfig(&frame->img, &sd);
817 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
821 return VPX_CODEC_INVALID_PARAM;
824 static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
828 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
832 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
836 return VPX_CODEC_INVALID_PARAM;
840 static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
843 YV12_BUFFER_CONFIG sd;
845 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, ctx->preview_ppcfg.deblocking_level, ctx->preview_ppcfg.noise_level, ctx->preview_ppcfg.post_proc_flag))
849 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
850 sd.y_width + 2*VP8BORDERINPIXELS,
851 sd.y_height + 2*VP8BORDERINPIXELS,
854 vpx_img_set_rect(&ctx->preview_img,
855 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
856 sd.y_width, sd.y_height);
859 ctx->preview_img.bps = 12;
860 ctx->preview_img.planes[PLANE_Y] = sd.y_buffer;
861 ctx->preview_img.planes[PLANE_U] = sd.u_buffer;
862 ctx->preview_img.planes[PLANE_V] = sd.v_buffer;
864 if (sd.clrtype == REG_YUV)
865 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
867 ctx->preview_img.fmt = VPX_IMG_FMT_VPXI420;
869 ctx->preview_img.x_chroma_shift = 1;
870 ctx->preview_img.y_chroma_shift = 1;
872 ctx->preview_img.d_w = ctx->cfg.g_w;
873 ctx->preview_img.d_h = ctx->cfg.g_h;
874 ctx->preview_img.stride[PLANE_Y] = sd.y_stride;
875 ctx->preview_img.stride[PLANE_U] = sd.uv_stride;
876 ctx->preview_img.stride[PLANE_V] = sd.uv_stride;
877 ctx->preview_img.w = sd.y_width;
878 ctx->preview_img.h = sd.y_height;
880 return &ctx->preview_img;
886 static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx,
890 int update = va_arg(args, int);
891 vp8_update_entropy(ctx->cpi, update);
896 static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
900 int update = va_arg(args, int);
901 vp8_update_reference(ctx->cpi, update);
905 static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
909 int reference_flag = va_arg(args, int);
910 vp8_use_as_reference(ctx->cpi, reference_flag);
914 static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
918 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
922 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
924 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
927 return VPX_CODEC_INVALID_PARAM;
930 return VPX_CODEC_INVALID_PARAM;
934 static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
938 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
943 vpx_active_map_t *map = (vpx_active_map_t *)data;
945 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
948 return VPX_CODEC_INVALID_PARAM;
951 return VPX_CODEC_INVALID_PARAM;
954 static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
959 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
964 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
965 res = vp8_set_internal_size(ctx->cpi, scalemode.h_scaling_mode, scalemode.v_scaling_mode);
969 /*force next frame a key frame to effect scaling mode */
970 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
974 return VPX_CODEC_INVALID_PARAM;
977 return VPX_CODEC_INVALID_PARAM;
981 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
983 {VP8_SET_REFERENCE, vp8e_set_reference},
984 {VP8_COPY_REFERENCE, vp8e_get_reference},
985 {VP8_SET_POSTPROC, vp8e_set_previewpp},
986 {VP8E_UPD_ENTROPY, vp8e_update_entropy},
987 {VP8E_UPD_REFERENCE, vp8e_update_reference},
988 {VP8E_USE_REFERENCE, vp8e_use_reference},
989 {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
990 {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
991 {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
992 {VP8E_SET_ENCODING_MODE, set_param},
993 {VP8E_SET_CPUUSED, set_param},
994 {VP8E_SET_NOISE_SENSITIVITY, set_param},
995 {VP8E_SET_ENABLEAUTOALTREF, set_param},
996 {VP8E_SET_SHARPNESS, set_param},
997 {VP8E_SET_STATIC_THRESHOLD, set_param},
998 {VP8E_SET_TOKEN_PARTITIONS, set_param},
999 {VP8E_GET_LAST_QUANTIZER, get_param},
1000 {VP8E_GET_LAST_QUANTIZER_64, get_param},
1001 {VP8E_SET_ARNR_MAXFRAMES, set_param},
1002 {VP8E_SET_ARNR_STRENGTH , set_param},
1003 {VP8E_SET_ARNR_TYPE , set_param},
1007 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
1018 {1, 30}, /* g_timebase */
1020 0, /* g_error_resilient */
1022 VPX_RC_ONE_PASS, /* g_pass */
1024 0, /* g_lag_in_frames */
1026 70, /* rc_dropframe_thresh */
1027 0, /* rc_resize_allowed */
1028 60, /* rc_resize_down_thresold */
1029 30, /* rc_resize_up_thresold */
1031 VPX_VBR, /* rc_end_usage */
1032 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1033 {0}, /* rc_twopass_stats_in */
1035 256, /* rc_target_bandwidth */
1037 4, /* rc_min_quantizer */
1038 63, /* rc_max_quantizer */
1040 95, /* rc_undershoot_pct */
1041 200, /* rc_overshoot_pct */
1043 6000, /* rc_max_buffer_size */
1044 4000, /* rc_buffer_initial_size; */
1045 5000, /* rc_buffer_optimal_size; */
1047 50, /* rc_two_pass_vbrbias */
1048 0, /* rc_two_pass_vbrmin_section */
1049 400, /* rc_two_pass_vbrmax_section */
1051 /* keyframing settings (kf) */
1052 VPX_KF_AUTO, /* g_kfmode*/
1053 0, /* kf_min_dist */
1054 9999, /* kf_max_dist */
1056 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
1057 1, /* g_delete_first_pass_file */
1058 "vp8.fpf" /* first pass filename */
1061 { -1, {NOT_IMPLEMENTED}}
1065 #ifndef VERSION_STRING
1066 #define VERSION_STRING
1068 vpx_codec_iface_t vpx_codec_vp8_cx_algo =
1070 "vpx Technologies VP8 Encoder" VERSION_STRING,
1071 VPX_CODEC_INTERNAL_ABI_VERSION,
1072 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR,
1073 /* vpx_codec_caps_t caps; */
1074 vp8e_init, /* vpx_codec_init_fn_t init; */
1075 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1076 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1077 NOT_IMPLEMENTED, /* vpx_codec_get_mmap_fn_t get_mmap; */
1078 NOT_IMPLEMENTED, /* vpx_codec_set_mmap_fn_t set_mmap; */
1080 NOT_IMPLEMENTED, /* vpx_codec_peek_si_fn_t peek_si; */
1081 NOT_IMPLEMENTED, /* vpx_codec_get_si_fn_t get_si; */
1082 NOT_IMPLEMENTED, /* vpx_codec_decode_fn_t decode; */
1083 NOT_IMPLEMENTED, /* vpx_codec_frame_get_fn_t frame_get; */
1086 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
1087 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
1088 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
1092 } /* encoder functions */
1097 * BEGIN BACKWARDS COMPATIBILITY SHIM.
1100 static vpx_codec_err_t api1_control(vpx_codec_alg_priv_t *ctx,
1104 vpx_codec_ctrl_fn_map_t *entry;
1108 case VP8E_SET_FLUSHFLAG:
1109 /* VP8 sample code did VP8E_SET_FLUSHFLAG followed by
1110 * vpx_codec_get_cx_data() rather than vpx_codec_encode().
1112 return vp8e_encode(ctx, NULL, 0, 0, 0, 0);
1113 case VP8E_SET_FRAMETYPE:
1114 ctx->base.enc.tbd |= FORCE_KEY;
1115 return VPX_CODEC_OK;
1118 for (entry = vp8e_ctf_maps; entry && entry->fn; entry++)
1120 if (!entry->ctrl_id || entry->ctrl_id == ctrl_id)
1122 return entry->fn(ctx, ctrl_id, args);
1126 return VPX_CODEC_ERROR;
1130 static vpx_codec_ctrl_fn_map_t api1_ctrl_maps[] =
1137 static vpx_codec_err_t api1_encode(vpx_codec_alg_priv_t *ctx,
1138 const vpx_image_t *img,
1139 vpx_codec_pts_t pts,
1140 unsigned long duration,
1141 vpx_enc_frame_flags_t flags,
1142 unsigned long deadline)
1144 int force = ctx->base.enc.tbd;
1146 ctx->base.enc.tbd = 0;
1152 flags | ((force & FORCE_KEY) ? VPX_EFLAG_FORCE_KF : 0),
1157 vpx_codec_iface_t vpx_enc_vp8_algo =
1159 "vpx Technologies VP8 Encoder (Deprecated API)" VERSION_STRING,
1160 VPX_CODEC_INTERNAL_ABI_VERSION,
1161 VPX_CODEC_CAP_ENCODER,
1162 /* vpx_codec_caps_t caps; */
1163 vp8e_init, /* vpx_codec_init_fn_t init; */
1164 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1165 api1_ctrl_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1166 NOT_IMPLEMENTED, /* vpx_codec_get_mmap_fn_t get_mmap; */
1167 NOT_IMPLEMENTED, /* vpx_codec_set_mmap_fn_t set_mmap; */
1168 {NOT_IMPLEMENTED}, /* decoder functions */
1170 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
1171 api1_encode, /* vpx_codec_encode_fn_t encode; */
1172 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
1176 } /* encoder functions */