store quant_shift as an unsigned char
[libvpx.git] / vp8 / vp8_cx_iface.c
blob973265c2e1bfd1bdaea1c1ac7ab618a745fd4242
1 /*
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.
9 */
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"
16 #include "vpx/vp8e.h"
17 #include "vp8/encoder/firstpass.h"
18 #include "vp8/common/onyx.h"
19 #include <stdlib.h>
20 #include <string.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
27 struct vp8_extracfg
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 */
40 vp8e_tuning tuning;
41 unsigned int cq_level; /* constrained quality level */
45 struct extraconfig_map
47 int usage;
48 struct vp8_extracfg cfg;
51 static const struct extraconfig_map extracfg_map[] =
56 NULL,
57 #if !(CONFIG_REALTIME_ONLY)
58 VP8_BEST_QUALITY_ENCODING, /* Encoding Mode */
59 0, /* cpu_used */
60 #else
61 VP8_REAL_TIME_ENCODING, /* Encoding Mode */
62 4, /* cpu_used */
63 #endif
64 0, /* enable_auto_alt_ref */
65 0, /* noise_sensitivity */
66 0, /* Sharpness */
67 0, /* static_thresh */
68 VP8_ONE_TOKENPARTITION, /* token_partitions */
69 0, /* arnr_max_frames */
70 3, /* arnr_strength */
71 3, /* arnr_type*/
72 0, /* tuning*/
73 10, /* cq_level */
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;
83 VP8_CONFIG oxcf;
84 VP8_PTR cpi;
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
91 int deprecated_mode;
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)
100 vpx_codec_err_t res;
102 if ((res = error->error_code))
103 ctx->base.err_detail = error->has_detail
104 ? error->detail
105 : NULL;
107 return res;
111 #undef ERROR
112 #define ERROR(str) do {\
113 ctx->base.err_detail = str;\
114 return VPX_CODEC_INVALID_PARAM;\
115 } while(0)
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"]");\
120 } while(0)
122 #define RANGE_CHECK_HI(p,memb,hi) do {\
123 if(!((p)->memb <= (hi))) \
124 ERROR(#memb " out of range [.."#hi"]");\
125 } while(0)
127 #define RANGE_CHECK_LO(p,memb,lo) do {\
128 if(!((p)->memb >= (lo))) \
129 ERROR(#memb " out of range ["#lo"..]");\
130 } while(0)
132 #define RANGE_CHECK_BOOL(p,memb) do {\
133 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
134 } while(0)
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, 16383); /* 14 bits available */
141 RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
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);
150 #else
151 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
152 #endif
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);
164 #else
165 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
166 #endif
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 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
179 #if !(CONFIG_REALTIME_ONLY)
180 RANGE_CHECK(vp8_cfg, encoding_mode, VP8_BEST_QUALITY_ENCODING, VP8_REAL_TIME_ENCODING);
181 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
182 #else
183 RANGE_CHECK(vp8_cfg, encoding_mode, VP8_REAL_TIME_ENCODING, VP8_REAL_TIME_ENCODING);
184 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
185 #endif
187 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION, VP8_EIGHT_TOKENPARTITION);
188 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
189 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
190 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
191 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
192 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
194 #if !(CONFIG_REALTIME_ONLY)
195 if (cfg->g_pass == VPX_RC_LAST_PASS)
197 size_t packet_sz = sizeof(FIRSTPASS_STATS);
198 int n_packets = cfg->rc_twopass_stats_in.sz / packet_sz;
199 FIRSTPASS_STATS *stats;
201 if (!cfg->rc_twopass_stats_in.buf)
202 ERROR("rc_twopass_stats_in.buf not set.");
204 if (cfg->rc_twopass_stats_in.sz % packet_sz)
205 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
207 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
208 ERROR("rc_twopass_stats_in requires at least two packets.");
210 stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
211 + (n_packets - 1) * packet_sz);
213 if ((int)(stats->count + 0.5) != n_packets - 1)
214 ERROR("rc_twopass_stats_in missing EOS stats packet");
216 #endif
218 return VPX_CODEC_OK;
222 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
223 const vpx_image_t *img)
225 switch (img->fmt)
227 case VPX_IMG_FMT_YV12:
228 case VPX_IMG_FMT_I420:
229 case VPX_IMG_FMT_VPXI420:
230 case VPX_IMG_FMT_VPXYV12:
231 break;
232 default:
233 ERROR("Invalid image format. Only YV12 and I420 images are supported");
236 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
237 ERROR("Image size must match encoder init configuration size");
239 return VPX_CODEC_OK;
243 static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
244 vpx_codec_enc_cfg_t cfg,
245 struct vp8_extracfg vp8_cfg)
247 oxcf->multi_threaded = cfg.g_threads;
248 oxcf->Version = cfg.g_profile;
250 oxcf->Width = cfg.g_w;
251 oxcf->Height = cfg.g_h;
252 /* guess a frame rate if out of whack, use 30 */
253 oxcf->frame_rate = (double)(cfg.g_timebase.den) / (double)(cfg.g_timebase.num);
255 if (oxcf->frame_rate > 180)
257 oxcf->frame_rate = 30;
260 oxcf->error_resilient_mode = cfg.g_error_resilient;
262 switch (cfg.g_pass)
264 case VPX_RC_ONE_PASS:
265 oxcf->Mode = MODE_BESTQUALITY;
266 break;
267 case VPX_RC_FIRST_PASS:
268 oxcf->Mode = MODE_FIRSTPASS;
269 break;
270 case VPX_RC_LAST_PASS:
271 oxcf->Mode = MODE_SECONDPASS_BEST;
272 break;
275 if (cfg.g_pass == VPX_RC_FIRST_PASS)
277 oxcf->allow_lag = 0;
278 oxcf->lag_in_frames = 0;
280 else
282 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
283 oxcf->lag_in_frames = cfg.g_lag_in_frames;
286 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
287 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
289 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
290 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
291 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
293 if (cfg.rc_end_usage == VPX_VBR)
295 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
297 else if (cfg.rc_end_usage == VPX_CBR)
299 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
301 else if (cfg.rc_end_usage == VPX_CQ)
303 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
306 oxcf->target_bandwidth = cfg.rc_target_bitrate;
308 oxcf->best_allowed_q = cfg.rc_min_quantizer;
309 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
310 oxcf->cq_level = vp8_cfg.cq_level;
311 oxcf->fixed_q = -1;
313 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
314 //oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
316 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
317 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
318 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
320 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
321 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
322 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
324 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
325 && cfg.kf_min_dist != cfg.kf_max_dist;
326 //oxcf->kf_min_dist = cfg.kf_min_dis;
327 oxcf->key_freq = cfg.kf_max_dist;
329 //oxcf->delete_first_pass_file = cfg.g_delete_firstpassfile;
330 //strcpy(oxcf->first_pass_file, cfg.g_firstpass_file);
332 oxcf->cpu_used = vp8_cfg.cpu_used;
333 oxcf->encode_breakout = vp8_cfg.static_thresh;
334 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
335 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
336 oxcf->Sharpness = vp8_cfg.Sharpness;
337 oxcf->token_partitions = vp8_cfg.token_partitions;
339 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
340 oxcf->output_pkt_list = vp8_cfg.pkt_list;
342 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
343 oxcf->arnr_strength = vp8_cfg.arnr_strength;
344 oxcf->arnr_type = vp8_cfg.arnr_type;
346 oxcf->tuning = vp8_cfg.tuning;
349 printf("Current VP8 Settings: \n");
350 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
351 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
352 printf("Sharpness: %d\n", oxcf->Sharpness);
353 printf("cpu_used: %d\n", oxcf->cpu_used);
354 printf("Mode: %d\n", oxcf->Mode);
355 printf("delete_first_pass_file: %d\n", oxcf->delete_first_pass_file);
356 printf("auto_key: %d\n", oxcf->auto_key);
357 printf("key_freq: %d\n", oxcf->key_freq);
358 printf("end_usage: %d\n", oxcf->end_usage);
359 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
360 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
361 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
362 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
363 printf("fixed_q: %d\n", oxcf->fixed_q);
364 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
365 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
366 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
367 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
368 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
369 printf("allow_df: %d\n", oxcf->allow_df);
370 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
371 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
372 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
373 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
374 printf("allow_lag: %d\n", oxcf->allow_lag);
375 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
376 printf("play_alternate: %d\n", oxcf->play_alternate);
377 printf("Version: %d\n", oxcf->Version);
378 printf("multi_threaded: %d\n", oxcf->multi_threaded);
379 printf("encode_breakout: %d\n", oxcf->encode_breakout);
381 return VPX_CODEC_OK;
384 static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
385 const vpx_codec_enc_cfg_t *cfg)
387 vpx_codec_err_t res;
389 if ((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h))
390 ERROR("Cannot change width or height after initialization");
392 /* Prevent increasing lag_in_frames. This check is stricter than it needs
393 * to be -- the limit is not increasing past the first lag_in_frames
394 * value, but we don't track the initial config, only the last successful
395 * config.
397 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
398 ERROR("Cannot increase lag_in_frames");
400 res = validate_config(ctx, cfg, &ctx->vp8_cfg);
402 if (!res)
404 ctx->cfg = *cfg;
405 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
406 vp8_change_config(ctx->cpi, &ctx->oxcf);
409 return res;
413 int vp8_reverse_trans(int);
416 static vpx_codec_err_t get_param(vpx_codec_alg_priv_t *ctx,
417 int ctrl_id,
418 va_list args)
420 void *arg = va_arg(args, void *);
422 #define MAP(id, var) case id: *(RECAST(id, arg)) = var; break
424 if (!arg)
425 return VPX_CODEC_INVALID_PARAM;
427 switch (ctrl_id)
429 MAP(VP8E_GET_LAST_QUANTIZER, vp8_get_quantizer(ctx->cpi));
430 MAP(VP8E_GET_LAST_QUANTIZER_64, vp8_reverse_trans(vp8_get_quantizer(ctx->cpi)));
433 return VPX_CODEC_OK;
434 #undef MAP
438 static vpx_codec_err_t set_param(vpx_codec_alg_priv_t *ctx,
439 int ctrl_id,
440 va_list args)
442 vpx_codec_err_t res = VPX_CODEC_OK;
443 struct vp8_extracfg xcfg = ctx->vp8_cfg;
445 #define MAP(id, var) case id: var = CAST(id, args); break;
447 switch (ctrl_id)
449 MAP(VP8E_SET_ENCODING_MODE, ctx->deprecated_mode);
450 MAP(VP8E_SET_CPUUSED, xcfg.cpu_used);
451 MAP(VP8E_SET_ENABLEAUTOALTREF, xcfg.enable_auto_alt_ref);
452 MAP(VP8E_SET_NOISE_SENSITIVITY, xcfg.noise_sensitivity);
453 MAP(VP8E_SET_SHARPNESS, xcfg.Sharpness);
454 MAP(VP8E_SET_STATIC_THRESHOLD, xcfg.static_thresh);
455 MAP(VP8E_SET_TOKEN_PARTITIONS, xcfg.token_partitions);
457 MAP(VP8E_SET_ARNR_MAXFRAMES, xcfg.arnr_max_frames);
458 MAP(VP8E_SET_ARNR_STRENGTH , xcfg.arnr_strength);
459 MAP(VP8E_SET_ARNR_TYPE , xcfg.arnr_type);
460 MAP(VP8E_SET_TUNING, xcfg.tuning);
461 MAP(VP8E_SET_CQ_LEVEL, xcfg.cq_level);
465 res = validate_config(ctx, &ctx->cfg, &xcfg);
467 if (!res)
469 ctx->vp8_cfg = xcfg;
470 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
471 vp8_change_config(ctx->cpi, &ctx->oxcf);
474 return res;
475 #undef MAP
477 static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx)
479 vpx_codec_err_t res = VPX_DEC_OK;
480 struct vpx_codec_alg_priv *priv;
481 vpx_codec_enc_cfg_t *cfg;
482 unsigned int i;
484 VP8_PTR optr;
486 if (!ctx->priv)
488 priv = calloc(1, sizeof(struct vpx_codec_alg_priv));
490 if (!priv)
492 return VPX_CODEC_MEM_ERROR;
495 ctx->priv = &priv->base;
496 ctx->priv->sz = sizeof(*ctx->priv);
497 ctx->priv->iface = ctx->iface;
498 ctx->priv->alg_priv = priv;
499 ctx->priv->init_flags = ctx->init_flags;
501 if (ctx->config.enc)
503 /* Update the reference to the config structure to an
504 * internal copy.
506 ctx->priv->alg_priv->cfg = *ctx->config.enc;
507 ctx->config.enc = &ctx->priv->alg_priv->cfg;
510 cfg = &ctx->priv->alg_priv->cfg;
512 /* Select the extra vp6 configuration table based on the current
513 * usage value. If the current usage value isn't found, use the
514 * values for usage case 0.
516 for (i = 0;
517 extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage;
518 i++);
520 priv->vp8_cfg = extracfg_map[i].cfg;
521 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
523 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
525 if (priv->cx_data_sz < 4096) priv->cx_data_sz = 4096;
527 priv->cx_data = malloc(priv->cx_data_sz);
529 if (!priv->cx_data)
531 return VPX_CODEC_MEM_ERROR;
534 priv->deprecated_mode = NO_MODE_SET;
536 vp8_initialize();
538 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg);
540 if (!res)
542 set_vp8e_config(&ctx->priv->alg_priv->oxcf,
543 ctx->priv->alg_priv->cfg,
544 ctx->priv->alg_priv->vp8_cfg);
545 optr = vp8_create_compressor(&ctx->priv->alg_priv->oxcf);
547 if (!optr)
548 res = VPX_CODEC_MEM_ERROR;
549 else
550 ctx->priv->alg_priv->cpi = optr;
554 return res;
557 static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
560 free(ctx->cx_data);
561 vp8_remove_compressor(&ctx->cpi);
562 free(ctx);
563 return VPX_CODEC_OK;
566 static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
567 YV12_BUFFER_CONFIG *yv12)
569 vpx_codec_err_t res = VPX_CODEC_OK;
570 yv12->y_buffer = img->planes[VPX_PLANE_Y];
571 yv12->u_buffer = img->planes[VPX_PLANE_U];
572 yv12->v_buffer = img->planes[VPX_PLANE_V];
574 yv12->y_width = img->d_w;
575 yv12->y_height = img->d_h;
576 yv12->uv_width = (1 + yv12->y_width) / 2;
577 yv12->uv_height = (1 + yv12->y_height) / 2;
579 yv12->y_stride = img->stride[VPX_PLANE_Y];
580 yv12->uv_stride = img->stride[VPX_PLANE_U];
582 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
583 yv12->clrtype = (img->fmt == VPX_IMG_FMT_VPXI420 || img->fmt == VPX_IMG_FMT_VPXYV12); //REG_YUV = 0
584 return res;
587 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
588 unsigned long duration,
589 unsigned long deadline)
591 unsigned int new_qc;
593 #if !(CONFIG_REALTIME_ONLY)
594 /* Use best quality mode if no deadline is given. */
595 new_qc = MODE_BESTQUALITY;
597 if (deadline)
599 uint64_t duration_us;
601 /* Convert duration parameter from stream timebase to microseconds */
602 duration_us = (uint64_t)duration * 1000000
603 * (uint64_t)ctx->cfg.g_timebase.num
604 / (uint64_t)ctx->cfg.g_timebase.den;
606 /* If the deadline is more that the duration this frame is to be shown,
607 * use good quality mode. Otherwise use realtime mode.
609 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
612 #else
613 new_qc = MODE_REALTIME;
614 #endif
616 switch (ctx->deprecated_mode)
618 case VP8_BEST_QUALITY_ENCODING:
619 new_qc = MODE_BESTQUALITY;
620 break;
621 case VP8_GOOD_QUALITY_ENCODING:
622 new_qc = MODE_GOODQUALITY;
623 break;
624 case VP8_REAL_TIME_ENCODING:
625 new_qc = MODE_REALTIME;
626 break;
629 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
630 new_qc = MODE_FIRSTPASS;
631 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
632 new_qc = (new_qc == MODE_BESTQUALITY)
633 ? MODE_SECONDPASS_BEST
634 : MODE_SECONDPASS;
636 if (ctx->oxcf.Mode != new_qc)
638 ctx->oxcf.Mode = new_qc;
639 vp8_change_config(ctx->cpi, &ctx->oxcf);
644 static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
645 const vpx_image_t *img,
646 vpx_codec_pts_t pts,
647 unsigned long duration,
648 vpx_enc_frame_flags_t flags,
649 unsigned long deadline)
651 vpx_codec_err_t res = VPX_CODEC_OK;
653 if (img)
654 res = validate_img(ctx, img);
656 pick_quickcompress_mode(ctx, duration, deadline);
657 vpx_codec_pkt_list_init(&ctx->pkt_list);
659 /* Handle Flags */
660 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
661 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
663 ctx->base.err_detail = "Conflicting flags.";
664 return VPX_CODEC_INVALID_PARAM;
667 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
668 | VP8_EFLAG_NO_REF_ARF))
670 int ref = 7;
672 if (flags & VP8_EFLAG_NO_REF_LAST)
673 ref ^= VP8_LAST_FLAG;
675 if (flags & VP8_EFLAG_NO_REF_GF)
676 ref ^= VP8_GOLD_FLAG;
678 if (flags & VP8_EFLAG_NO_REF_ARF)
679 ref ^= VP8_ALT_FLAG;
681 vp8_use_as_reference(ctx->cpi, ref);
684 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
685 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
686 | VP8_EFLAG_FORCE_ARF))
688 int upd = 7;
690 if (flags & VP8_EFLAG_NO_UPD_LAST)
691 upd ^= VP8_LAST_FLAG;
693 if (flags & VP8_EFLAG_NO_UPD_GF)
694 upd ^= VP8_GOLD_FLAG;
696 if (flags & VP8_EFLAG_NO_UPD_ARF)
697 upd ^= VP8_ALT_FLAG;
699 vp8_update_reference(ctx->cpi, upd);
702 if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
704 vp8_update_entropy(ctx->cpi, 0);
707 /* Handle fixed keyframe intervals */
708 if (ctx->cfg.kf_mode == VPX_KF_AUTO
709 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
711 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
713 flags |= VPX_EFLAG_FORCE_KF;
714 ctx->fixed_kf_cntr = 1;
718 /* Initialize the encoder instance on the first frame*/
719 if (!res && ctx->cpi)
721 unsigned int lib_flags;
722 YV12_BUFFER_CONFIG sd;
723 INT64 dst_time_stamp, dst_end_time_stamp;
724 unsigned long size, cx_data_sz;
725 unsigned char *cx_data;
727 /* Set up internal flags */
728 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
729 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
731 /* Convert API flags to internal codec lib flags */
732 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
734 /* vp8 use 10,000,000 ticks/second as time stamp */
735 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
736 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
738 if (img != NULL)
740 res = image2yuvconfig(img, &sd);
742 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
743 &sd, dst_time_stamp, dst_end_time_stamp))
745 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
746 res = update_error_state(ctx, &cpi->common.error);
749 /* reset for next frame */
750 ctx->next_frame_flag = 0;
753 cx_data = ctx->cx_data;
754 cx_data_sz = ctx->cx_data_sz;
755 lib_flags = 0;
757 while (cx_data_sz >= ctx->cx_data_sz / 2
758 && -1 != vp8_get_compressed_data(ctx->cpi, &lib_flags, &size, cx_data, &dst_time_stamp, &dst_end_time_stamp, !img))
760 if (size)
762 vpx_codec_pts_t round, delta;
763 vpx_codec_cx_pkt_t pkt;
764 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
766 /* Add the frame packet to the list of returned packets. */
767 round = 1000000 * ctx->cfg.g_timebase.num / 2 - 1;
768 delta = (dst_end_time_stamp - dst_time_stamp);
769 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
770 pkt.data.frame.buf = cx_data;
771 pkt.data.frame.sz = size;
772 pkt.data.frame.pts =
773 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
774 / ctx->cfg.g_timebase.num / 10000000;
775 pkt.data.frame.duration =
776 (delta * ctx->cfg.g_timebase.den + round)
777 / ctx->cfg.g_timebase.num / 10000000;
778 pkt.data.frame.flags = lib_flags << 16;
780 if (lib_flags & FRAMEFLAGS_KEY)
781 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
783 if (!cpi->common.show_frame)
785 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
787 // This timestamp should be as close as possible to the
788 // prior PTS so that if a decoder uses pts to schedule when
789 // to do this, we start right after last frame was decoded.
790 // Invisible frames have no duration.
791 pkt.data.frame.pts = ((cpi->last_time_stamp_seen
792 * ctx->cfg.g_timebase.den + round)
793 / ctx->cfg.g_timebase.num / 10000000) + 1;
794 pkt.data.frame.duration = 0;
797 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
799 //printf("timestamp: %lld, duration: %d\n", pkt->data.frame.pts, pkt->data.frame.duration);
800 cx_data += size;
801 cx_data_sz -= size;
806 return res;
810 static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
811 vpx_codec_iter_t *iter)
813 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
816 static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
817 int ctr_id,
818 va_list args)
820 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
822 if (data)
824 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
825 YV12_BUFFER_CONFIG sd;
827 image2yuvconfig(&frame->img, &sd);
828 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
829 return VPX_CODEC_OK;
831 else
832 return VPX_CODEC_INVALID_PARAM;
836 static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
837 int ctr_id,
838 va_list args)
841 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
843 if (data)
845 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
846 YV12_BUFFER_CONFIG sd;
848 image2yuvconfig(&frame->img, &sd);
849 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
850 return VPX_CODEC_OK;
852 else
853 return VPX_CODEC_INVALID_PARAM;
856 static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
857 int ctr_id,
858 va_list args)
860 #if CONFIG_POSTPROC
861 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
862 (void)ctr_id;
864 if (data)
866 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
867 return VPX_CODEC_OK;
869 else
870 return VPX_CODEC_INVALID_PARAM;
871 #else
872 (void)ctx;
873 (void)ctr_id;
874 (void)args;
875 return VPX_CODEC_INCAPABLE;
876 #endif
880 static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
883 YV12_BUFFER_CONFIG sd;
884 vp8_ppflags_t flags = {0};
886 if (ctx->preview_ppcfg.post_proc_flag)
888 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
889 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
890 flags.noise_level = ctx->preview_ppcfg.noise_level;
893 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
897 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
898 sd.y_width + 2*VP8BORDERINPIXELS,
899 sd.y_height + 2*VP8BORDERINPIXELS,
901 sd.buffer_alloc);
902 vpx_img_set_rect(&ctx->preview_img,
903 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
904 sd.y_width, sd.y_height);
907 ctx->preview_img.bps = 12;
908 ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
909 ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
910 ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
912 if (sd.clrtype == REG_YUV)
913 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
914 else
915 ctx->preview_img.fmt = VPX_IMG_FMT_VPXI420;
917 ctx->preview_img.x_chroma_shift = 1;
918 ctx->preview_img.y_chroma_shift = 1;
920 ctx->preview_img.d_w = sd.y_width;
921 ctx->preview_img.d_h = sd.y_height;
922 ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
923 ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
924 ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
925 ctx->preview_img.w = sd.y_width;
926 ctx->preview_img.h = sd.y_height;
928 return &ctx->preview_img;
930 else
931 return NULL;
934 static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx,
935 int ctr_id,
936 va_list args)
938 int update = va_arg(args, int);
939 vp8_update_entropy(ctx->cpi, update);
940 return VPX_CODEC_OK;
944 static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
945 int ctr_id,
946 va_list args)
948 int update = va_arg(args, int);
949 vp8_update_reference(ctx->cpi, update);
950 return VPX_CODEC_OK;
953 static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
954 int ctr_id,
955 va_list args)
957 int reference_flag = va_arg(args, int);
958 vp8_use_as_reference(ctx->cpi, reference_flag);
959 return VPX_CODEC_OK;
962 static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
963 int ctr_id,
964 va_list args)
966 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
968 if (data)
970 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
972 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
973 return VPX_CODEC_OK;
974 else
975 return VPX_CODEC_INVALID_PARAM;
977 else
978 return VPX_CODEC_INVALID_PARAM;
982 static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
983 int ctr_id,
984 va_list args)
986 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
988 if (data)
991 vpx_active_map_t *map = (vpx_active_map_t *)data;
993 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
994 return VPX_CODEC_OK;
995 else
996 return VPX_CODEC_INVALID_PARAM;
998 else
999 return VPX_CODEC_INVALID_PARAM;
1002 static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
1003 int ctr_id,
1004 va_list args)
1007 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1009 if (data)
1011 int res;
1012 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
1013 res = vp8_set_internal_size(ctx->cpi, scalemode.h_scaling_mode, scalemode.v_scaling_mode);
1015 if (!res)
1017 /*force next frame a key frame to effect scaling mode */
1018 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1019 return VPX_CODEC_OK;
1021 else
1022 return VPX_CODEC_INVALID_PARAM;
1024 else
1025 return VPX_CODEC_INVALID_PARAM;
1029 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
1031 {VP8_SET_REFERENCE, vp8e_set_reference},
1032 {VP8_COPY_REFERENCE, vp8e_get_reference},
1033 {VP8_SET_POSTPROC, vp8e_set_previewpp},
1034 {VP8E_UPD_ENTROPY, vp8e_update_entropy},
1035 {VP8E_UPD_REFERENCE, vp8e_update_reference},
1036 {VP8E_USE_REFERENCE, vp8e_use_reference},
1037 {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
1038 {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
1039 {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
1040 {VP8E_SET_ENCODING_MODE, set_param},
1041 {VP8E_SET_CPUUSED, set_param},
1042 {VP8E_SET_NOISE_SENSITIVITY, set_param},
1043 {VP8E_SET_ENABLEAUTOALTREF, set_param},
1044 {VP8E_SET_SHARPNESS, set_param},
1045 {VP8E_SET_STATIC_THRESHOLD, set_param},
1046 {VP8E_SET_TOKEN_PARTITIONS, set_param},
1047 {VP8E_GET_LAST_QUANTIZER, get_param},
1048 {VP8E_GET_LAST_QUANTIZER_64, get_param},
1049 {VP8E_SET_ARNR_MAXFRAMES, set_param},
1050 {VP8E_SET_ARNR_STRENGTH , set_param},
1051 {VP8E_SET_ARNR_TYPE , set_param},
1052 {VP8E_SET_TUNING, set_param},
1053 {VP8E_SET_CQ_LEVEL, set_param},
1054 { -1, NULL},
1057 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
1062 0, /* g_usage */
1063 0, /* g_threads */
1064 0, /* g_profile */
1066 320, /* g_width */
1067 240, /* g_height */
1068 {1, 30}, /* g_timebase */
1070 0, /* g_error_resilient */
1072 VPX_RC_ONE_PASS, /* g_pass */
1074 0, /* g_lag_in_frames */
1076 0, /* rc_dropframe_thresh */
1077 0, /* rc_resize_allowed */
1078 60, /* rc_resize_down_thresold */
1079 30, /* rc_resize_up_thresold */
1081 VPX_VBR, /* rc_end_usage */
1082 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1083 {0}, /* rc_twopass_stats_in */
1084 #endif
1085 256, /* rc_target_bandwidth */
1087 4, /* rc_min_quantizer */
1088 63, /* rc_max_quantizer */
1089 95, /* rc_undershoot_pct */
1090 200, /* rc_overshoot_pct */
1092 6000, /* rc_max_buffer_size */
1093 4000, /* rc_buffer_initial_size; */
1094 5000, /* rc_buffer_optimal_size; */
1096 50, /* rc_two_pass_vbrbias */
1097 0, /* rc_two_pass_vbrmin_section */
1098 400, /* rc_two_pass_vbrmax_section */
1100 /* keyframing settings (kf) */
1101 VPX_KF_AUTO, /* g_kfmode*/
1102 0, /* kf_min_dist */
1103 9999, /* kf_max_dist */
1105 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
1106 1, /* g_delete_first_pass_file */
1107 "vp8.fpf" /* first pass filename */
1108 #endif
1110 { -1, {NOT_IMPLEMENTED}}
1114 #ifndef VERSION_STRING
1115 #define VERSION_STRING
1116 #endif
1117 CODEC_INTERFACE(vpx_codec_vp8_cx) =
1119 "WebM Project VP8 Encoder" VERSION_STRING,
1120 VPX_CODEC_INTERNAL_ABI_VERSION,
1121 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR,
1122 /* vpx_codec_caps_t caps; */
1123 vp8e_init, /* vpx_codec_init_fn_t init; */
1124 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1125 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1126 NOT_IMPLEMENTED, /* vpx_codec_get_mmap_fn_t get_mmap; */
1127 NOT_IMPLEMENTED, /* vpx_codec_set_mmap_fn_t set_mmap; */
1129 NOT_IMPLEMENTED, /* vpx_codec_peek_si_fn_t peek_si; */
1130 NOT_IMPLEMENTED, /* vpx_codec_get_si_fn_t get_si; */
1131 NOT_IMPLEMENTED, /* vpx_codec_decode_fn_t decode; */
1132 NOT_IMPLEMENTED, /* vpx_codec_frame_get_fn_t frame_get; */
1135 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
1136 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
1137 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
1138 vp8e_set_config,
1139 NOT_IMPLEMENTED,
1140 vp8e_get_preview,
1141 } /* encoder functions */
1146 * BEGIN BACKWARDS COMPATIBILITY SHIM.
1148 #define FORCE_KEY 2
1149 static vpx_codec_err_t api1_control(vpx_codec_alg_priv_t *ctx,
1150 int ctrl_id,
1151 va_list args)
1153 vpx_codec_ctrl_fn_map_t *entry;
1155 switch (ctrl_id)
1157 case VP8E_SET_FLUSHFLAG:
1158 /* VP8 sample code did VP8E_SET_FLUSHFLAG followed by
1159 * vpx_codec_get_cx_data() rather than vpx_codec_encode().
1161 return vp8e_encode(ctx, NULL, 0, 0, 0, 0);
1162 case VP8E_SET_FRAMETYPE:
1163 ctx->base.enc.tbd |= FORCE_KEY;
1164 return VPX_CODEC_OK;
1167 for (entry = vp8e_ctf_maps; entry && entry->fn; entry++)
1169 if (!entry->ctrl_id || entry->ctrl_id == ctrl_id)
1171 return entry->fn(ctx, ctrl_id, args);
1175 return VPX_CODEC_ERROR;
1179 static vpx_codec_ctrl_fn_map_t api1_ctrl_maps[] =
1181 {0, api1_control},
1182 { -1, NULL}
1186 static vpx_codec_err_t api1_encode(vpx_codec_alg_priv_t *ctx,
1187 const vpx_image_t *img,
1188 vpx_codec_pts_t pts,
1189 unsigned long duration,
1190 vpx_enc_frame_flags_t flags,
1191 unsigned long deadline)
1193 int force = ctx->base.enc.tbd;
1195 ctx->base.enc.tbd = 0;
1196 return vp8e_encode
1197 (ctx,
1198 img,
1199 pts,
1200 duration,
1201 flags | ((force & FORCE_KEY) ? VPX_EFLAG_FORCE_KF : 0),
1202 deadline);
1206 vpx_codec_iface_t vpx_enc_vp8_algo =
1208 "WebM Project VP8 Encoder (Deprecated API)" VERSION_STRING,
1209 VPX_CODEC_INTERNAL_ABI_VERSION,
1210 VPX_CODEC_CAP_ENCODER,
1211 /* vpx_codec_caps_t caps; */
1212 vp8e_init, /* vpx_codec_init_fn_t init; */
1213 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1214 api1_ctrl_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1215 NOT_IMPLEMENTED, /* vpx_codec_get_mmap_fn_t get_mmap; */
1216 NOT_IMPLEMENTED, /* vpx_codec_set_mmap_fn_t set_mmap; */
1217 {NOT_IMPLEMENTED}, /* decoder functions */
1219 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
1220 api1_encode, /* vpx_codec_encode_fn_t encode; */
1221 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
1222 vp8e_set_config,
1223 NOT_IMPLEMENTED,
1224 vp8e_get_preview,
1225 } /* encoder functions */