1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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 */
10
11
12 #include "./vpx_config.h"
13 #include "vp8_rtcd.h"
14 #include "vpx/vpx_codec.h"
15 #include "vpx/internal/vpx_codec_internal.h"
16 #include "vpx_version.h"
17 #include "vp8/encoder/onyx_int.h"
18 #include "vpx/vp8cx.h"
19 #include "vp8/encoder/firstpass.h"
20 #include "vp8/common/onyx.h"
21 #include <stdlib.h>
22 #include <string.h>
23
24 struct vp8_extracfg
25 {
26 struct vpx_codec_pkt_list *pkt_list;
27 int cpu_used; /** available cpu percentage in 1/16*/
28 unsigned int enable_auto_alt_ref; /** if encoder decides to uses alternate reference frame */
29 unsigned int noise_sensitivity;
30 unsigned int Sharpness;
31 unsigned int static_thresh;
32 unsigned int token_partitions;
33 unsigned int arnr_max_frames; /* alt_ref Noise Reduction Max Frame Count */
34 unsigned int arnr_strength; /* alt_ref Noise Reduction Strength */
35 unsigned int arnr_type; /* alt_ref filter type */
36 vp8e_tuning tuning;
37 unsigned int cq_level; /* constrained quality level */
38 unsigned int rc_max_intra_bitrate_pct;
39
40 };
41
42 struct extraconfig_map
43 {
44 int usage;
45 struct vp8_extracfg cfg;
46 };
47
48 static const struct extraconfig_map extracfg_map[] =
49 {
50 {
51 0,
52 {
53 NULL,
54 #if !(CONFIG_REALTIME_ONLY)
55 0, /* cpu_used */
56 #else
57 4, /* cpu_used */
58 #endif
59 0, /* enable_auto_alt_ref */
60 0, /* noise_sensitivity */
61 0, /* Sharpness */
62 0, /* static_thresh */
63 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
64 VP8_EIGHT_TOKENPARTITION,
65 #else
66 VP8_ONE_TOKENPARTITION, /* token_partitions */
67 #endif
68 0, /* arnr_max_frames */
69 3, /* arnr_strength */
70 3, /* arnr_type*/
71 0, /* tuning*/
72 10, /* cq_level */
73 0, /* rc_max_intra_bitrate_pct */
74 }
75 }
76 };
77
78 struct vpx_codec_alg_priv
79 {
80 vpx_codec_priv_t base;
81 vpx_codec_enc_cfg_t cfg;
82 struct vp8_extracfg vp8_cfg;
83 VP8_CONFIG oxcf;
84 struct VP8_COMP *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 /* pkt_list size depends on the maximum number of lagged frames allowed. */
91 vpx_codec_pkt_list_decl(64) pkt_list;
92 unsigned int fixed_kf_cntr;
93 };
94
95
96 static vpx_codec_err_t
update_error_state(vpx_codec_alg_priv_t * ctx,const struct vpx_internal_error_info * error)97 update_error_state(vpx_codec_alg_priv_t *ctx,
98 const struct vpx_internal_error_info *error)
99 {
100 vpx_codec_err_t res;
101
102 if ((res = error->error_code))
103 ctx->base.err_detail = error->has_detail
104 ? error->detail
105 : NULL;
106
107 return res;
108 }
109
110
111 #undef ERROR
112 #define ERROR(str) do {\
113 ctx->base.err_detail = str;\
114 return VPX_CODEC_INVALID_PARAM;\
115 } while(0)
116
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)
121
122 #define RANGE_CHECK_HI(p,memb,hi) do {\
123 if(!((p)->memb <= (hi))) \
124 ERROR(#memb " out of range [.."#hi"]");\
125 } while(0)
126
127 #define RANGE_CHECK_LO(p,memb,lo) do {\
128 if(!((p)->memb >= (lo))) \
129 ERROR(#memb " out of range ["#lo"..]");\
130 } while(0)
131
132 #define RANGE_CHECK_BOOL(p,memb) do {\
133 if(!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
134 } while(0)
135
validate_config(vpx_codec_alg_priv_t * ctx,const vpx_codec_enc_cfg_t * cfg,const struct vp8_extracfg * vp8_cfg,int finalize)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,
139 int finalize)
140 {
141 RANGE_CHECK(cfg, g_w, 1, 16383); /* 14 bits available */
142 RANGE_CHECK(cfg, g_h, 1, 16383); /* 14 bits available */
143 RANGE_CHECK(cfg, g_timebase.den, 1, 1000000000);
144 RANGE_CHECK(cfg, g_timebase.num, 1, cfg->g_timebase.den);
145 RANGE_CHECK_HI(cfg, g_profile, 3);
146 RANGE_CHECK_HI(cfg, rc_max_quantizer, 63);
147 RANGE_CHECK_HI(cfg, rc_min_quantizer, cfg->rc_max_quantizer);
148 RANGE_CHECK_HI(cfg, g_threads, 64);
149 #if CONFIG_REALTIME_ONLY
150 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
151 #elif CONFIG_MULTI_RES_ENCODING
152 if (ctx->base.enc.total_encoders > 1)
153 RANGE_CHECK_HI(cfg, g_lag_in_frames, 0);
154 #else
155 RANGE_CHECK_HI(cfg, g_lag_in_frames, 25);
156 #endif
157 RANGE_CHECK(cfg, rc_end_usage, VPX_VBR, VPX_Q);
158 RANGE_CHECK_HI(cfg, rc_undershoot_pct, 1000);
159 RANGE_CHECK_HI(cfg, rc_overshoot_pct, 1000);
160 RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
161 RANGE_CHECK(cfg, kf_mode, VPX_KF_DISABLED, VPX_KF_AUTO);
162
163 /* TODO: add spatial re-sampling support and frame dropping in
164 * multi-res-encoder.*/
165 #if CONFIG_MULTI_RES_ENCODING
166 if (ctx->base.enc.total_encoders > 1)
167 RANGE_CHECK_HI(cfg, rc_resize_allowed, 0);
168 #else
169 RANGE_CHECK_BOOL(cfg, rc_resize_allowed);
170 #endif
171 RANGE_CHECK_HI(cfg, rc_dropframe_thresh, 100);
172 RANGE_CHECK_HI(cfg, rc_resize_up_thresh, 100);
173 RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
174
175 #if CONFIG_REALTIME_ONLY
176 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
177 #elif CONFIG_MULTI_RES_ENCODING
178 if (ctx->base.enc.total_encoders > 1)
179 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_ONE_PASS);
180 #else
181 RANGE_CHECK(cfg, g_pass, VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
182 #endif
183
184 /* VP8 does not support a lower bound on the keyframe interval in
185 * automatic keyframe placement mode.
186 */
187 if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
188 && cfg->kf_min_dist > 0)
189 ERROR("kf_min_dist not supported in auto mode, use 0 "
190 "or kf_max_dist instead.");
191
192 RANGE_CHECK_BOOL(vp8_cfg, enable_auto_alt_ref);
193 RANGE_CHECK(vp8_cfg, cpu_used, -16, 16);
194
195 #if CONFIG_REALTIME_ONLY && !CONFIG_TEMPORAL_DENOISING
196 RANGE_CHECK(vp8_cfg, noise_sensitivity, 0, 0);
197 #else
198 RANGE_CHECK_HI(vp8_cfg, noise_sensitivity, 6);
199 #endif
200
201 RANGE_CHECK(vp8_cfg, token_partitions, VP8_ONE_TOKENPARTITION,
202 VP8_EIGHT_TOKENPARTITION);
203 RANGE_CHECK_HI(vp8_cfg, Sharpness, 7);
204 RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
205 RANGE_CHECK_HI(vp8_cfg, arnr_strength, 6);
206 RANGE_CHECK(vp8_cfg, arnr_type, 1, 3);
207 RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
208 if (finalize && (cfg->rc_end_usage == VPX_CQ || cfg->rc_end_usage == VPX_Q))
209 RANGE_CHECK(vp8_cfg, cq_level,
210 cfg->rc_min_quantizer, cfg->rc_max_quantizer);
211
212 #if !(CONFIG_REALTIME_ONLY)
213 if (cfg->g_pass == VPX_RC_LAST_PASS)
214 {
215 size_t packet_sz = sizeof(FIRSTPASS_STATS);
216 int n_packets = (int)(cfg->rc_twopass_stats_in.sz /
217 packet_sz);
218 FIRSTPASS_STATS *stats;
219
220 if (!cfg->rc_twopass_stats_in.buf)
221 ERROR("rc_twopass_stats_in.buf not set.");
222
223 if (cfg->rc_twopass_stats_in.sz % packet_sz)
224 ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
225
226 if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
227 ERROR("rc_twopass_stats_in requires at least two packets.");
228
229 stats = (void*)((char *)cfg->rc_twopass_stats_in.buf
230 + (n_packets - 1) * packet_sz);
231
232 if ((int)(stats->count + 0.5) != n_packets - 1)
233 ERROR("rc_twopass_stats_in missing EOS stats packet");
234 }
235 #endif
236
237 RANGE_CHECK(cfg, ts_number_layers, 1, 5);
238
239 if (cfg->ts_number_layers > 1)
240 {
241 unsigned int i;
242 RANGE_CHECK_HI(cfg, ts_periodicity, 16);
243
244 for (i=1; i<cfg->ts_number_layers; i++)
245 if (cfg->ts_target_bitrate[i] <= cfg->ts_target_bitrate[i-1])
246 ERROR("ts_target_bitrate entries are not strictly increasing");
247
248 RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
249 for (i=cfg->ts_number_layers-2; i>0; i--)
250 if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i])
251 ERROR("ts_rate_decimator factors are not powers of 2");
252
253 RANGE_CHECK_HI(cfg, ts_layer_id[i], cfg->ts_number_layers-1);
254 }
255
256 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
257 if(cfg->g_threads > (1 << vp8_cfg->token_partitions))
258 ERROR("g_threads cannot be bigger than number of token partitions");
259 #endif
260
261 return VPX_CODEC_OK;
262 }
263
264
validate_img(vpx_codec_alg_priv_t * ctx,const vpx_image_t * img)265 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
266 const vpx_image_t *img)
267 {
268 switch (img->fmt)
269 {
270 case VPX_IMG_FMT_YV12:
271 case VPX_IMG_FMT_I420:
272 case VPX_IMG_FMT_VPXI420:
273 case VPX_IMG_FMT_VPXYV12:
274 break;
275 default:
276 ERROR("Invalid image format. Only YV12 and I420 images are supported");
277 }
278
279 if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
280 ERROR("Image size must match encoder init configuration size");
281
282 return VPX_CODEC_OK;
283 }
284
285
set_vp8e_config(VP8_CONFIG * oxcf,vpx_codec_enc_cfg_t cfg,struct vp8_extracfg vp8_cfg,vpx_codec_priv_enc_mr_cfg_t * mr_cfg)286 static vpx_codec_err_t set_vp8e_config(VP8_CONFIG *oxcf,
287 vpx_codec_enc_cfg_t cfg,
288 struct vp8_extracfg vp8_cfg,
289 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
290 {
291 oxcf->multi_threaded = cfg.g_threads;
292 oxcf->Version = cfg.g_profile;
293
294 oxcf->Width = cfg.g_w;
295 oxcf->Height = cfg.g_h;
296 oxcf->timebase = cfg.g_timebase;
297
298 oxcf->error_resilient_mode = cfg.g_error_resilient;
299
300 switch (cfg.g_pass)
301 {
302 case VPX_RC_ONE_PASS:
303 oxcf->Mode = MODE_BESTQUALITY;
304 break;
305 case VPX_RC_FIRST_PASS:
306 oxcf->Mode = MODE_FIRSTPASS;
307 break;
308 case VPX_RC_LAST_PASS:
309 oxcf->Mode = MODE_SECONDPASS_BEST;
310 break;
311 }
312
313 if (cfg.g_pass == VPX_RC_FIRST_PASS || cfg.g_pass == VPX_RC_ONE_PASS)
314 {
315 oxcf->allow_lag = 0;
316 oxcf->lag_in_frames = 0;
317 }
318 else
319 {
320 oxcf->allow_lag = (cfg.g_lag_in_frames) > 0;
321 oxcf->lag_in_frames = cfg.g_lag_in_frames;
322 }
323
324 oxcf->allow_df = (cfg.rc_dropframe_thresh > 0);
325 oxcf->drop_frames_water_mark = cfg.rc_dropframe_thresh;
326
327 oxcf->allow_spatial_resampling = cfg.rc_resize_allowed;
328 oxcf->resample_up_water_mark = cfg.rc_resize_up_thresh;
329 oxcf->resample_down_water_mark = cfg.rc_resize_down_thresh;
330
331 if (cfg.rc_end_usage == VPX_VBR) {
332 oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
333 } else if (cfg.rc_end_usage == VPX_CBR) {
334 oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
335 } else if (cfg.rc_end_usage == VPX_CQ) {
336 oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
337 } else if (cfg.rc_end_usage == VPX_Q) {
338 oxcf->end_usage = USAGE_CONSTANT_QUALITY;
339 }
340
341 oxcf->target_bandwidth = cfg.rc_target_bitrate;
342 oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
343
344 oxcf->best_allowed_q = cfg.rc_min_quantizer;
345 oxcf->worst_allowed_q = cfg.rc_max_quantizer;
346 oxcf->cq_level = vp8_cfg.cq_level;
347 oxcf->fixed_q = -1;
348
349 oxcf->under_shoot_pct = cfg.rc_undershoot_pct;
350 oxcf->over_shoot_pct = cfg.rc_overshoot_pct;
351
352 oxcf->maximum_buffer_size_in_ms = cfg.rc_buf_sz;
353 oxcf->starting_buffer_level_in_ms = cfg.rc_buf_initial_sz;
354 oxcf->optimal_buffer_level_in_ms = cfg.rc_buf_optimal_sz;
355
356 oxcf->maximum_buffer_size = cfg.rc_buf_sz;
357 oxcf->starting_buffer_level = cfg.rc_buf_initial_sz;
358 oxcf->optimal_buffer_level = cfg.rc_buf_optimal_sz;
359
360 oxcf->two_pass_vbrbias = cfg.rc_2pass_vbr_bias_pct;
361 oxcf->two_pass_vbrmin_section = cfg.rc_2pass_vbr_minsection_pct;
362 oxcf->two_pass_vbrmax_section = cfg.rc_2pass_vbr_maxsection_pct;
363
364 oxcf->auto_key = cfg.kf_mode == VPX_KF_AUTO
365 && cfg.kf_min_dist != cfg.kf_max_dist;
366 oxcf->key_freq = cfg.kf_max_dist;
367
368 oxcf->number_of_layers = cfg.ts_number_layers;
369 oxcf->periodicity = cfg.ts_periodicity;
370
371 if (oxcf->number_of_layers > 1)
372 {
373 memcpy (oxcf->target_bitrate, cfg.ts_target_bitrate,
374 sizeof(cfg.ts_target_bitrate));
375 memcpy (oxcf->rate_decimator, cfg.ts_rate_decimator,
376 sizeof(cfg.ts_rate_decimator));
377 memcpy (oxcf->layer_id, cfg.ts_layer_id, sizeof(cfg.ts_layer_id));
378 }
379
380 #if CONFIG_MULTI_RES_ENCODING
381 /* When mr_cfg is NULL, oxcf->mr_total_resolutions and oxcf->mr_encoder_id
382 * are both memset to 0, which ensures the correct logic under this
383 * situation.
384 */
385 if(mr_cfg)
386 {
387 oxcf->mr_total_resolutions = mr_cfg->mr_total_resolutions;
388 oxcf->mr_encoder_id = mr_cfg->mr_encoder_id;
389 oxcf->mr_down_sampling_factor.num = mr_cfg->mr_down_sampling_factor.num;
390 oxcf->mr_down_sampling_factor.den = mr_cfg->mr_down_sampling_factor.den;
391 oxcf->mr_low_res_mode_info = mr_cfg->mr_low_res_mode_info;
392 }
393 #endif
394
395 oxcf->cpu_used = vp8_cfg.cpu_used;
396 oxcf->encode_breakout = vp8_cfg.static_thresh;
397 oxcf->play_alternate = vp8_cfg.enable_auto_alt_ref;
398 oxcf->noise_sensitivity = vp8_cfg.noise_sensitivity;
399 oxcf->Sharpness = vp8_cfg.Sharpness;
400 oxcf->token_partitions = vp8_cfg.token_partitions;
401
402 oxcf->two_pass_stats_in = cfg.rc_twopass_stats_in;
403 oxcf->output_pkt_list = vp8_cfg.pkt_list;
404
405 oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
406 oxcf->arnr_strength = vp8_cfg.arnr_strength;
407 oxcf->arnr_type = vp8_cfg.arnr_type;
408
409 oxcf->tuning = vp8_cfg.tuning;
410
411 /*
412 printf("Current VP8 Settings: \n");
413 printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
414 printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
415 printf("Sharpness: %d\n", oxcf->Sharpness);
416 printf("cpu_used: %d\n", oxcf->cpu_used);
417 printf("Mode: %d\n", oxcf->Mode);
418 printf("auto_key: %d\n", oxcf->auto_key);
419 printf("key_freq: %d\n", oxcf->key_freq);
420 printf("end_usage: %d\n", oxcf->end_usage);
421 printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
422 printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
423 printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
424 printf("optimal_buffer_level: %d\n", oxcf->optimal_buffer_level);
425 printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
426 printf("fixed_q: %d\n", oxcf->fixed_q);
427 printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
428 printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
429 printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
430 printf("resample_down_water_mark: %d\n", oxcf->resample_down_water_mark);
431 printf("resample_up_water_mark: %d\n", oxcf->resample_up_water_mark);
432 printf("allow_df: %d\n", oxcf->allow_df);
433 printf("drop_frames_water_mark: %d\n", oxcf->drop_frames_water_mark);
434 printf("two_pass_vbrbias: %d\n", oxcf->two_pass_vbrbias);
435 printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
436 printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
437 printf("allow_lag: %d\n", oxcf->allow_lag);
438 printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
439 printf("play_alternate: %d\n", oxcf->play_alternate);
440 printf("Version: %d\n", oxcf->Version);
441 printf("multi_threaded: %d\n", oxcf->multi_threaded);
442 printf("encode_breakout: %d\n", oxcf->encode_breakout);
443 */
444 return VPX_CODEC_OK;
445 }
446
vp8e_set_config(vpx_codec_alg_priv_t * ctx,const vpx_codec_enc_cfg_t * cfg)447 static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx,
448 const vpx_codec_enc_cfg_t *cfg)
449 {
450 vpx_codec_err_t res;
451
452 if (((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h))
453 && (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS))
454 ERROR("Cannot change width or height after initialization");
455
456 /* Prevent increasing lag_in_frames. This check is stricter than it needs
457 * to be -- the limit is not increasing past the first lag_in_frames
458 * value, but we don't track the initial config, only the last successful
459 * config.
460 */
461 if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
462 ERROR("Cannot increase lag_in_frames");
463
464 res = validate_config(ctx, cfg, &ctx->vp8_cfg, 0);
465
466 if (!res)
467 {
468 ctx->cfg = *cfg;
469 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
470 vp8_change_config(ctx->cpi, &ctx->oxcf);
471 }
472
473 return res;
474 }
475
476 int vp8_reverse_trans(int);
477
get_quantizer(vpx_codec_alg_priv_t * ctx,va_list args)478 static vpx_codec_err_t get_quantizer(vpx_codec_alg_priv_t *ctx, va_list args)
479 {
480 int *const arg = va_arg(args, int *);
481 if (arg == NULL)
482 return VPX_CODEC_INVALID_PARAM;
483 *arg = vp8_get_quantizer(ctx->cpi);
484 return VPX_CODEC_OK;
485 }
486
get_quantizer64(vpx_codec_alg_priv_t * ctx,va_list args)487 static vpx_codec_err_t get_quantizer64(vpx_codec_alg_priv_t *ctx, va_list args)
488 {
489 int *const arg = va_arg(args, int *);
490 if (arg == NULL)
491 return VPX_CODEC_INVALID_PARAM;
492 *arg = vp8_reverse_trans(vp8_get_quantizer(ctx->cpi));
493 return VPX_CODEC_OK;
494 }
495
update_extracfg(vpx_codec_alg_priv_t * ctx,const struct vp8_extracfg * extra_cfg)496 static vpx_codec_err_t update_extracfg(vpx_codec_alg_priv_t *ctx,
497 const struct vp8_extracfg *extra_cfg)
498 {
499 const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg, 0);
500 if (res == VPX_CODEC_OK) {
501 ctx->vp8_cfg = *extra_cfg;
502 set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL);
503 vp8_change_config(ctx->cpi, &ctx->oxcf);
504 }
505 return res;
506 }
507
set_cpu_used(vpx_codec_alg_priv_t * ctx,va_list args)508 static vpx_codec_err_t set_cpu_used(vpx_codec_alg_priv_t *ctx, va_list args)
509 {
510 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
511 extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
512 return update_extracfg(ctx, &extra_cfg);
513 }
514
set_enable_auto_alt_ref(vpx_codec_alg_priv_t * ctx,va_list args)515 static vpx_codec_err_t set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
516 va_list args)
517 {
518 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
519 extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
520 return update_extracfg(ctx, &extra_cfg);
521 }
522
set_noise_sensitivity(vpx_codec_alg_priv_t * ctx,va_list args)523 static vpx_codec_err_t set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
524 va_list args)
525 {
526 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
527 extra_cfg.noise_sensitivity = CAST(VP8E_SET_NOISE_SENSITIVITY, args);
528 return update_extracfg(ctx, &extra_cfg);
529 }
530
set_sharpness(vpx_codec_alg_priv_t * ctx,va_list args)531 static vpx_codec_err_t set_sharpness(vpx_codec_alg_priv_t *ctx, va_list args)
532 {
533 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
534 extra_cfg.Sharpness = CAST(VP8E_SET_SHARPNESS, args);
535 return update_extracfg(ctx, &extra_cfg);
536 }
537
set_static_thresh(vpx_codec_alg_priv_t * ctx,va_list args)538 static vpx_codec_err_t set_static_thresh(vpx_codec_alg_priv_t *ctx,
539 va_list args)
540 {
541 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
542 extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
543 return update_extracfg(ctx, &extra_cfg);
544 }
545
set_token_partitions(vpx_codec_alg_priv_t * ctx,va_list args)546 static vpx_codec_err_t set_token_partitions(vpx_codec_alg_priv_t *ctx,
547 va_list args)
548 {
549 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
550 extra_cfg.token_partitions = CAST(VP8E_SET_TOKEN_PARTITIONS, args);
551 return update_extracfg(ctx, &extra_cfg);
552 }
553
set_arnr_max_frames(vpx_codec_alg_priv_t * ctx,va_list args)554 static vpx_codec_err_t set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
555 va_list args)
556 {
557 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
558 extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
559 return update_extracfg(ctx, &extra_cfg);
560 }
561
set_arnr_strength(vpx_codec_alg_priv_t * ctx,va_list args)562 static vpx_codec_err_t set_arnr_strength(vpx_codec_alg_priv_t *ctx,
563 va_list args)
564 {
565 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
566 extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
567 return update_extracfg(ctx, &extra_cfg);
568 }
569
set_arnr_type(vpx_codec_alg_priv_t * ctx,va_list args)570 static vpx_codec_err_t set_arnr_type(vpx_codec_alg_priv_t *ctx, va_list args)
571 {
572 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
573 extra_cfg.arnr_type = CAST(VP8E_SET_ARNR_TYPE, args);
574 return update_extracfg(ctx, &extra_cfg);
575 }
576
set_tuning(vpx_codec_alg_priv_t * ctx,va_list args)577 static vpx_codec_err_t set_tuning(vpx_codec_alg_priv_t *ctx, va_list args)
578 {
579 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
580 extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
581 return update_extracfg(ctx, &extra_cfg);
582 }
583
set_cq_level(vpx_codec_alg_priv_t * ctx,va_list args)584 static vpx_codec_err_t set_cq_level(vpx_codec_alg_priv_t *ctx, va_list args)
585 {
586 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
587 extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
588 return update_extracfg(ctx, &extra_cfg);
589 }
590
set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t * ctx,va_list args)591 static vpx_codec_err_t set_rc_max_intra_bitrate_pct(vpx_codec_alg_priv_t *ctx,
592 va_list args)
593 {
594 struct vp8_extracfg extra_cfg = ctx->vp8_cfg;
595 extra_cfg.rc_max_intra_bitrate_pct =
596 CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
597 return update_extracfg(ctx, &extra_cfg);
598 }
599
vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t * cfg,void ** mem_loc)600 static vpx_codec_err_t vp8e_mr_alloc_mem(const vpx_codec_enc_cfg_t *cfg,
601 void **mem_loc)
602 {
603 vpx_codec_err_t res = 0;
604
605 #if CONFIG_MULTI_RES_ENCODING
606 LOWER_RES_FRAME_INFO *shared_mem_loc;
607 int mb_rows = ((cfg->g_w + 15) >>4);
608 int mb_cols = ((cfg->g_h + 15) >>4);
609
610 shared_mem_loc = calloc(1, sizeof(LOWER_RES_FRAME_INFO));
611 if(!shared_mem_loc)
612 {
613 res = VPX_CODEC_MEM_ERROR;
614 }
615
616 shared_mem_loc->mb_info = calloc(mb_rows*mb_cols, sizeof(LOWER_RES_MB_INFO));
617 if(!(shared_mem_loc->mb_info))
618 {
619 res = VPX_CODEC_MEM_ERROR;
620 }
621 else
622 {
623 *mem_loc = (void *)shared_mem_loc;
624 res = VPX_CODEC_OK;
625 }
626 #endif
627 return res;
628 }
629
vp8e_init(vpx_codec_ctx_t * ctx,vpx_codec_priv_enc_mr_cfg_t * mr_cfg)630 static vpx_codec_err_t vp8e_init(vpx_codec_ctx_t *ctx,
631 vpx_codec_priv_enc_mr_cfg_t *mr_cfg)
632 {
633 vpx_codec_err_t res = VPX_CODEC_OK;
634 struct vpx_codec_alg_priv *priv;
635 vpx_codec_enc_cfg_t *cfg;
636 unsigned int i;
637
638 struct VP8_COMP *optr;
639
640 vp8_rtcd();
641
642 if (!ctx->priv)
643 {
644 priv = calloc(1, sizeof(struct vpx_codec_alg_priv));
645
646 if (!priv)
647 {
648 return VPX_CODEC_MEM_ERROR;
649 }
650
651 ctx->priv = &priv->base;
652 ctx->priv->sz = sizeof(*ctx->priv);
653 ctx->priv->iface = ctx->iface;
654 ctx->priv->alg_priv = priv;
655 ctx->priv->init_flags = ctx->init_flags;
656
657 if (ctx->config.enc)
658 {
659 /* Update the reference to the config structure to an
660 * internal copy.
661 */
662 ctx->priv->alg_priv->cfg = *ctx->config.enc;
663 ctx->config.enc = &ctx->priv->alg_priv->cfg;
664 }
665
666 cfg = &ctx->priv->alg_priv->cfg;
667
668 /* Select the extra vp8 configuration table based on the current
669 * usage value. If the current usage value isn't found, use the
670 * values for usage case 0.
671 */
672 for (i = 0;
673 extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage;
674 i++);
675
676 priv->vp8_cfg = extracfg_map[i].cfg;
677 priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
678
679 priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
680
681 if (priv->cx_data_sz < 32768) priv->cx_data_sz = 32768;
682
683 priv->cx_data = malloc(priv->cx_data_sz);
684
685 if (!priv->cx_data)
686 {
687 return VPX_CODEC_MEM_ERROR;
688 }
689
690 if(mr_cfg)
691 ctx->priv->enc.total_encoders = mr_cfg->mr_total_resolutions;
692 else
693 ctx->priv->enc.total_encoders = 1;
694
695 res = validate_config(priv, &priv->cfg, &priv->vp8_cfg, 0);
696
697 if (!res)
698 {
699 set_vp8e_config(&ctx->priv->alg_priv->oxcf,
700 ctx->priv->alg_priv->cfg,
701 ctx->priv->alg_priv->vp8_cfg,
702 mr_cfg);
703
704 optr = vp8_create_compressor(&ctx->priv->alg_priv->oxcf);
705
706 if (!optr)
707 res = VPX_CODEC_MEM_ERROR;
708 else
709 ctx->priv->alg_priv->cpi = optr;
710 }
711 }
712
713 return res;
714 }
715
vp8e_destroy(vpx_codec_alg_priv_t * ctx)716 static vpx_codec_err_t vp8e_destroy(vpx_codec_alg_priv_t *ctx)
717 {
718 #if CONFIG_MULTI_RES_ENCODING
719 /* Free multi-encoder shared memory */
720 if (ctx->oxcf.mr_total_resolutions > 0 && (ctx->oxcf.mr_encoder_id == ctx->oxcf.mr_total_resolutions-1))
721 {
722 LOWER_RES_FRAME_INFO *shared_mem_loc = (LOWER_RES_FRAME_INFO *)ctx->oxcf.mr_low_res_mode_info;
723 free(shared_mem_loc->mb_info);
724 free(ctx->oxcf.mr_low_res_mode_info);
725 }
726 #endif
727
728 free(ctx->cx_data);
729 vp8_remove_compressor(&ctx->cpi);
730 free(ctx);
731 return VPX_CODEC_OK;
732 }
733
image2yuvconfig(const vpx_image_t * img,YV12_BUFFER_CONFIG * yv12)734 static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
735 YV12_BUFFER_CONFIG *yv12)
736 {
737 vpx_codec_err_t res = VPX_CODEC_OK;
738 yv12->y_buffer = img->planes[VPX_PLANE_Y];
739 yv12->u_buffer = img->planes[VPX_PLANE_U];
740 yv12->v_buffer = img->planes[VPX_PLANE_V];
741
742 yv12->y_crop_width = img->d_w;
743 yv12->y_crop_height = img->d_h;
744 yv12->y_width = img->d_w;
745 yv12->y_height = img->d_h;
746 yv12->uv_width = (1 + yv12->y_width) / 2;
747 yv12->uv_height = (1 + yv12->y_height) / 2;
748
749 yv12->y_stride = img->stride[VPX_PLANE_Y];
750 yv12->uv_stride = img->stride[VPX_PLANE_U];
751
752 yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
753 return res;
754 }
755
pick_quickcompress_mode(vpx_codec_alg_priv_t * ctx,unsigned long duration,unsigned long deadline)756 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
757 unsigned long duration,
758 unsigned long deadline)
759 {
760 unsigned int new_qc;
761
762 #if !(CONFIG_REALTIME_ONLY)
763 /* Use best quality mode if no deadline is given. */
764 new_qc = MODE_BESTQUALITY;
765
766 if (deadline)
767 {
768 uint64_t duration_us;
769
770 /* Convert duration parameter from stream timebase to microseconds */
771 duration_us = (uint64_t)duration * 1000000
772 * (uint64_t)ctx->cfg.g_timebase.num
773 / (uint64_t)ctx->cfg.g_timebase.den;
774
775 /* If the deadline is more that the duration this frame is to be shown,
776 * use good quality mode. Otherwise use realtime mode.
777 */
778 new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
779 }
780
781 #else
782 new_qc = MODE_REALTIME;
783 #endif
784
785 if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
786 new_qc = MODE_FIRSTPASS;
787 else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
788 new_qc = (new_qc == MODE_BESTQUALITY)
789 ? MODE_SECONDPASS_BEST
790 : MODE_SECONDPASS;
791
792 if (ctx->oxcf.Mode != new_qc)
793 {
794 ctx->oxcf.Mode = new_qc;
795 vp8_change_config(ctx->cpi, &ctx->oxcf);
796 }
797 }
798
799
vp8e_encode(vpx_codec_alg_priv_t * ctx,const vpx_image_t * img,vpx_codec_pts_t pts,unsigned long duration,vpx_enc_frame_flags_t flags,unsigned long deadline)800 static vpx_codec_err_t vp8e_encode(vpx_codec_alg_priv_t *ctx,
801 const vpx_image_t *img,
802 vpx_codec_pts_t pts,
803 unsigned long duration,
804 vpx_enc_frame_flags_t flags,
805 unsigned long deadline)
806 {
807 vpx_codec_err_t res = VPX_CODEC_OK;
808
809 if (!ctx->cfg.rc_target_bitrate)
810 return res;
811
812 if (img)
813 res = validate_img(ctx, img);
814
815 if (!res)
816 res = validate_config(ctx, &ctx->cfg, &ctx->vp8_cfg, 1);
817
818 pick_quickcompress_mode(ctx, duration, deadline);
819 vpx_codec_pkt_list_init(&ctx->pkt_list);
820
821 /* Handle Flags */
822 if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
823 || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF)))
824 {
825 ctx->base.err_detail = "Conflicting flags.";
826 return VPX_CODEC_INVALID_PARAM;
827 }
828
829 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
830 | VP8_EFLAG_NO_REF_ARF))
831 {
832 int ref = 7;
833
834 if (flags & VP8_EFLAG_NO_REF_LAST)
835 ref ^= VP8_LAST_FRAME;
836
837 if (flags & VP8_EFLAG_NO_REF_GF)
838 ref ^= VP8_GOLD_FRAME;
839
840 if (flags & VP8_EFLAG_NO_REF_ARF)
841 ref ^= VP8_ALTR_FRAME;
842
843 vp8_use_as_reference(ctx->cpi, ref);
844 }
845
846 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
847 | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
848 | VP8_EFLAG_FORCE_ARF))
849 {
850 int upd = 7;
851
852 if (flags & VP8_EFLAG_NO_UPD_LAST)
853 upd ^= VP8_LAST_FRAME;
854
855 if (flags & VP8_EFLAG_NO_UPD_GF)
856 upd ^= VP8_GOLD_FRAME;
857
858 if (flags & VP8_EFLAG_NO_UPD_ARF)
859 upd ^= VP8_ALTR_FRAME;
860
861 vp8_update_reference(ctx->cpi, upd);
862 }
863
864 if (flags & VP8_EFLAG_NO_UPD_ENTROPY)
865 {
866 vp8_update_entropy(ctx->cpi, 0);
867 }
868
869 /* Handle fixed keyframe intervals */
870 if (ctx->cfg.kf_mode == VPX_KF_AUTO
871 && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist)
872 {
873 if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist)
874 {
875 flags |= VPX_EFLAG_FORCE_KF;
876 ctx->fixed_kf_cntr = 1;
877 }
878 }
879
880 /* Initialize the encoder instance on the first frame*/
881 if (!res && ctx->cpi)
882 {
883 unsigned int lib_flags;
884 YV12_BUFFER_CONFIG sd;
885 int64_t dst_time_stamp, dst_end_time_stamp;
886 unsigned long size, cx_data_sz;
887 unsigned char *cx_data;
888 unsigned char *cx_data_end;
889 int comp_data_state = 0;
890
891 /* Set up internal flags */
892 if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
893 ((VP8_COMP *)ctx->cpi)->b_calculate_psnr = 1;
894
895 if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
896 ((VP8_COMP *)ctx->cpi)->output_partition = 1;
897
898 /* Convert API flags to internal codec lib flags */
899 lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
900
901 /* vp8 use 10,000,000 ticks/second as time stamp */
902 dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
903 dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
904
905 if (img != NULL)
906 {
907 res = image2yuvconfig(img, &sd);
908
909 if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
910 &sd, dst_time_stamp, dst_end_time_stamp))
911 {
912 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
913 res = update_error_state(ctx, &cpi->common.error);
914 }
915
916 /* reset for next frame */
917 ctx->next_frame_flag = 0;
918 }
919
920 cx_data = ctx->cx_data;
921 cx_data_sz = ctx->cx_data_sz;
922 cx_data_end = ctx->cx_data + cx_data_sz;
923 lib_flags = 0;
924
925 while (cx_data_sz >= ctx->cx_data_sz / 2)
926 {
927 comp_data_state = vp8_get_compressed_data(ctx->cpi,
928 &lib_flags,
929 &size,
930 cx_data,
931 cx_data_end,
932 &dst_time_stamp,
933 &dst_end_time_stamp,
934 !img);
935
936 if(comp_data_state == VPX_CODEC_CORRUPT_FRAME)
937 return VPX_CODEC_CORRUPT_FRAME;
938 else if(comp_data_state == -1)
939 break;
940
941 if (size)
942 {
943 vpx_codec_pts_t round, delta;
944 vpx_codec_cx_pkt_t pkt;
945 VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
946
947 /* Add the frame packet to the list of returned packets. */
948 round = (vpx_codec_pts_t)10000000
949 * ctx->cfg.g_timebase.num / 2 - 1;
950 delta = (dst_end_time_stamp - dst_time_stamp);
951 pkt.kind = VPX_CODEC_CX_FRAME_PKT;
952 pkt.data.frame.pts =
953 (dst_time_stamp * ctx->cfg.g_timebase.den + round)
954 / ctx->cfg.g_timebase.num / 10000000;
955 pkt.data.frame.duration = (unsigned long)
956 ((delta * ctx->cfg.g_timebase.den + round)
957 / ctx->cfg.g_timebase.num / 10000000);
958 pkt.data.frame.flags = lib_flags << 16;
959
960 if (lib_flags & FRAMEFLAGS_KEY)
961 pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
962
963 if (!cpi->common.show_frame)
964 {
965 pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
966
967 /* This timestamp should be as close as possible to the
968 * prior PTS so that if a decoder uses pts to schedule when
969 * to do this, we start right after last frame was decoded.
970 * Invisible frames have no duration.
971 */
972 pkt.data.frame.pts = ((cpi->last_time_stamp_seen
973 * ctx->cfg.g_timebase.den + round)
974 / ctx->cfg.g_timebase.num / 10000000) + 1;
975 pkt.data.frame.duration = 0;
976 }
977
978 if (cpi->droppable)
979 pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
980
981 if (cpi->output_partition)
982 {
983 int i;
984 const int num_partitions =
985 (1 << cpi->common.multi_token_partition) + 1;
986
987 pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
988
989 for (i = 0; i < num_partitions; ++i)
990 {
991 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
992 pkt.data.frame.buf = cpi->partition_d[i];
993 #else
994 pkt.data.frame.buf = cx_data;
995 cx_data += cpi->partition_sz[i];
996 cx_data_sz -= cpi->partition_sz[i];
997 #endif
998 pkt.data.frame.sz = cpi->partition_sz[i];
999 pkt.data.frame.partition_id = i;
1000 /* don't set the fragment bit for the last partition */
1001 if (i == (num_partitions - 1))
1002 pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
1003 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1004 }
1005 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1006 /* In lagged mode the encoder can buffer multiple frames.
1007 * We don't want this in partitioned output because
1008 * partitions are spread all over the output buffer.
1009 * So, force an exit!
1010 */
1011 cx_data_sz -= ctx->cx_data_sz / 2;
1012 #endif
1013 }
1014 else
1015 {
1016 pkt.data.frame.buf = cx_data;
1017 pkt.data.frame.sz = size;
1018 pkt.data.frame.partition_id = -1;
1019 vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1020 cx_data += size;
1021 cx_data_sz -= size;
1022 }
1023 }
1024 }
1025 }
1026
1027 return res;
1028 }
1029
1030
vp8e_get_cxdata(vpx_codec_alg_priv_t * ctx,vpx_codec_iter_t * iter)1031 static const vpx_codec_cx_pkt_t *vp8e_get_cxdata(vpx_codec_alg_priv_t *ctx,
1032 vpx_codec_iter_t *iter)
1033 {
1034 return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1035 }
1036
vp8e_set_reference(vpx_codec_alg_priv_t * ctx,va_list args)1037 static vpx_codec_err_t vp8e_set_reference(vpx_codec_alg_priv_t *ctx,
1038 va_list args)
1039 {
1040 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1041
1042 if (data)
1043 {
1044 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1045 YV12_BUFFER_CONFIG sd;
1046
1047 image2yuvconfig(&frame->img, &sd);
1048 vp8_set_reference(ctx->cpi, frame->frame_type, &sd);
1049 return VPX_CODEC_OK;
1050 }
1051 else
1052 return VPX_CODEC_INVALID_PARAM;
1053
1054 }
1055
vp8e_get_reference(vpx_codec_alg_priv_t * ctx,va_list args)1056 static vpx_codec_err_t vp8e_get_reference(vpx_codec_alg_priv_t *ctx,
1057 va_list args)
1058 {
1059
1060 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
1061
1062 if (data)
1063 {
1064 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
1065 YV12_BUFFER_CONFIG sd;
1066
1067 image2yuvconfig(&frame->img, &sd);
1068 vp8_get_reference(ctx->cpi, frame->frame_type, &sd);
1069 return VPX_CODEC_OK;
1070 }
1071 else
1072 return VPX_CODEC_INVALID_PARAM;
1073 }
1074
vp8e_set_previewpp(vpx_codec_alg_priv_t * ctx,va_list args)1075 static vpx_codec_err_t vp8e_set_previewpp(vpx_codec_alg_priv_t *ctx,
1076 va_list args)
1077 {
1078 #if CONFIG_POSTPROC
1079 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
1080
1081 if (data)
1082 {
1083 ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
1084 return VPX_CODEC_OK;
1085 }
1086 else
1087 return VPX_CODEC_INVALID_PARAM;
1088 #else
1089 (void)ctx;
1090 (void)args;
1091 return VPX_CODEC_INCAPABLE;
1092 #endif
1093 }
1094
1095
vp8e_get_preview(vpx_codec_alg_priv_t * ctx)1096 static vpx_image_t *vp8e_get_preview(vpx_codec_alg_priv_t *ctx)
1097 {
1098
1099 YV12_BUFFER_CONFIG sd;
1100 vp8_ppflags_t flags = {0};
1101
1102 if (ctx->preview_ppcfg.post_proc_flag)
1103 {
1104 flags.post_proc_flag = ctx->preview_ppcfg.post_proc_flag;
1105 flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1106 flags.noise_level = ctx->preview_ppcfg.noise_level;
1107 }
1108
1109 if (0 == vp8_get_preview_raw_frame(ctx->cpi, &sd, &flags))
1110 {
1111
1112 /*
1113 vpx_img_wrap(&ctx->preview_img, VPX_IMG_FMT_YV12,
1114 sd.y_width + 2*VP8BORDERINPIXELS,
1115 sd.y_height + 2*VP8BORDERINPIXELS,
1116 1,
1117 sd.buffer_alloc);
1118 vpx_img_set_rect(&ctx->preview_img,
1119 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
1120 sd.y_width, sd.y_height);
1121 */
1122
1123 ctx->preview_img.bps = 12;
1124 ctx->preview_img.planes[VPX_PLANE_Y] = sd.y_buffer;
1125 ctx->preview_img.planes[VPX_PLANE_U] = sd.u_buffer;
1126 ctx->preview_img.planes[VPX_PLANE_V] = sd.v_buffer;
1127
1128 ctx->preview_img.fmt = VPX_IMG_FMT_I420;
1129 ctx->preview_img.x_chroma_shift = 1;
1130 ctx->preview_img.y_chroma_shift = 1;
1131
1132 ctx->preview_img.d_w = sd.y_width;
1133 ctx->preview_img.d_h = sd.y_height;
1134 ctx->preview_img.stride[VPX_PLANE_Y] = sd.y_stride;
1135 ctx->preview_img.stride[VPX_PLANE_U] = sd.uv_stride;
1136 ctx->preview_img.stride[VPX_PLANE_V] = sd.uv_stride;
1137 ctx->preview_img.w = sd.y_width;
1138 ctx->preview_img.h = sd.y_height;
1139
1140 return &ctx->preview_img;
1141 }
1142 else
1143 return NULL;
1144 }
1145
vp8e_update_entropy(vpx_codec_alg_priv_t * ctx,va_list args)1146 static vpx_codec_err_t vp8e_update_entropy(vpx_codec_alg_priv_t *ctx,
1147 va_list args)
1148 {
1149 int update = va_arg(args, int);
1150 vp8_update_entropy(ctx->cpi, update);
1151 return VPX_CODEC_OK;
1152
1153 }
1154
vp8e_update_reference(vpx_codec_alg_priv_t * ctx,va_list args)1155 static vpx_codec_err_t vp8e_update_reference(vpx_codec_alg_priv_t *ctx,
1156 va_list args)
1157 {
1158 int update = va_arg(args, int);
1159 vp8_update_reference(ctx->cpi, update);
1160 return VPX_CODEC_OK;
1161 }
1162
vp8e_use_reference(vpx_codec_alg_priv_t * ctx,va_list args)1163 static vpx_codec_err_t vp8e_use_reference(vpx_codec_alg_priv_t *ctx,
1164 va_list args)
1165 {
1166 int reference_flag = va_arg(args, int);
1167 vp8_use_as_reference(ctx->cpi, reference_flag);
1168 return VPX_CODEC_OK;
1169 }
1170
vp8e_set_roi_map(vpx_codec_alg_priv_t * ctx,va_list args)1171 static vpx_codec_err_t vp8e_set_roi_map(vpx_codec_alg_priv_t *ctx,
1172 va_list args)
1173 {
1174 vpx_roi_map_t *data = va_arg(args, vpx_roi_map_t *);
1175
1176 if (data)
1177 {
1178 vpx_roi_map_t *roi = (vpx_roi_map_t *)data;
1179
1180 if (!vp8_set_roimap(ctx->cpi, roi->roi_map, roi->rows, roi->cols, roi->delta_q, roi->delta_lf, roi->static_threshold))
1181 return VPX_CODEC_OK;
1182 else
1183 return VPX_CODEC_INVALID_PARAM;
1184 }
1185 else
1186 return VPX_CODEC_INVALID_PARAM;
1187 }
1188
1189
vp8e_set_activemap(vpx_codec_alg_priv_t * ctx,va_list args)1190 static vpx_codec_err_t vp8e_set_activemap(vpx_codec_alg_priv_t *ctx,
1191 va_list args)
1192 {
1193 vpx_active_map_t *data = va_arg(args, vpx_active_map_t *);
1194
1195 if (data)
1196 {
1197
1198 vpx_active_map_t *map = (vpx_active_map_t *)data;
1199
1200 if (!vp8_set_active_map(ctx->cpi, map->active_map, map->rows, map->cols))
1201 return VPX_CODEC_OK;
1202 else
1203 return VPX_CODEC_INVALID_PARAM;
1204 }
1205 else
1206 return VPX_CODEC_INVALID_PARAM;
1207 }
1208
vp8e_set_scalemode(vpx_codec_alg_priv_t * ctx,va_list args)1209 static vpx_codec_err_t vp8e_set_scalemode(vpx_codec_alg_priv_t *ctx,
1210 va_list args)
1211 {
1212
1213 vpx_scaling_mode_t *data = va_arg(args, vpx_scaling_mode_t *);
1214
1215 if (data)
1216 {
1217 int res;
1218 vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data ;
1219 res = vp8_set_internal_size(ctx->cpi,
1220 (VPX_SCALING)scalemode.h_scaling_mode,
1221 (VPX_SCALING)scalemode.v_scaling_mode);
1222
1223 if (!res)
1224 {
1225 /*force next frame a key frame to effect scaling mode */
1226 ctx->next_frame_flag |= FRAMEFLAGS_KEY;
1227 return VPX_CODEC_OK;
1228 }
1229 else
1230 return VPX_CODEC_INVALID_PARAM;
1231 }
1232 else
1233 return VPX_CODEC_INVALID_PARAM;
1234 }
1235
1236
1237 static vpx_codec_ctrl_fn_map_t vp8e_ctf_maps[] =
1238 {
1239 {VP8_SET_REFERENCE, vp8e_set_reference},
1240 {VP8_COPY_REFERENCE, vp8e_get_reference},
1241 {VP8_SET_POSTPROC, vp8e_set_previewpp},
1242 {VP8E_UPD_ENTROPY, vp8e_update_entropy},
1243 {VP8E_UPD_REFERENCE, vp8e_update_reference},
1244 {VP8E_USE_REFERENCE, vp8e_use_reference},
1245 {VP8E_SET_ROI_MAP, vp8e_set_roi_map},
1246 {VP8E_SET_ACTIVEMAP, vp8e_set_activemap},
1247 {VP8E_SET_SCALEMODE, vp8e_set_scalemode},
1248 {VP8E_SET_CPUUSED, set_cpu_used},
1249 {VP8E_SET_NOISE_SENSITIVITY, set_noise_sensitivity},
1250 {VP8E_SET_ENABLEAUTOALTREF, set_enable_auto_alt_ref},
1251 {VP8E_SET_SHARPNESS, set_sharpness},
1252 {VP8E_SET_STATIC_THRESHOLD, set_static_thresh},
1253 {VP8E_SET_TOKEN_PARTITIONS, set_token_partitions},
1254 {VP8E_GET_LAST_QUANTIZER, get_quantizer},
1255 {VP8E_GET_LAST_QUANTIZER_64, get_quantizer64},
1256 {VP8E_SET_ARNR_MAXFRAMES, set_arnr_max_frames},
1257 {VP8E_SET_ARNR_STRENGTH , set_arnr_strength},
1258 {VP8E_SET_ARNR_TYPE , set_arnr_type},
1259 {VP8E_SET_TUNING, set_tuning},
1260 {VP8E_SET_CQ_LEVEL, set_cq_level},
1261 {VP8E_SET_MAX_INTRA_BITRATE_PCT, set_rc_max_intra_bitrate_pct},
1262 { -1, NULL},
1263 };
1264
1265 static vpx_codec_enc_cfg_map_t vp8e_usage_cfg_map[] =
1266 {
1267 {
1268 0,
1269 {
1270 0, /* g_usage */
1271 0, /* g_threads */
1272 0, /* g_profile */
1273
1274 320, /* g_width */
1275 240, /* g_height */
1276 {1, 30}, /* g_timebase */
1277
1278 0, /* g_error_resilient */
1279
1280 VPX_RC_ONE_PASS, /* g_pass */
1281
1282 0, /* g_lag_in_frames */
1283
1284 0, /* rc_dropframe_thresh */
1285 0, /* rc_resize_allowed */
1286 1, /* rc_scaled_width */
1287 1, /* rc_scaled_height */
1288 60, /* rc_resize_down_thresold */
1289 30, /* rc_resize_up_thresold */
1290
1291 VPX_VBR, /* rc_end_usage */
1292 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1293 {0}, /* rc_twopass_stats_in */
1294 {0}, /* rc_firstpass_mb_stats_in */
1295 #endif
1296 256, /* rc_target_bandwidth */
1297 4, /* rc_min_quantizer */
1298 63, /* rc_max_quantizer */
1299 100, /* rc_undershoot_pct */
1300 100, /* rc_overshoot_pct */
1301
1302 6000, /* rc_max_buffer_size */
1303 4000, /* rc_buffer_initial_size; */
1304 5000, /* rc_buffer_optimal_size; */
1305
1306 50, /* rc_two_pass_vbrbias */
1307 0, /* rc_two_pass_vbrmin_section */
1308 400, /* rc_two_pass_vbrmax_section */
1309
1310 /* keyframing settings (kf) */
1311 VPX_KF_AUTO, /* g_kfmode*/
1312 0, /* kf_min_dist */
1313 128, /* kf_max_dist */
1314
1315 #if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
1316 "vp8.fpf" /* first pass filename */
1317 #endif
1318 VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
1319 {0},
1320 {0}, /* ss_target_bitrate */
1321 1, /* ts_number_layers */
1322 {0}, /* ts_target_bitrate */
1323 {0}, /* ts_rate_decimator */
1324 0, /* ts_periodicity */
1325 {0}, /* ts_layer_id */
1326 }},
1327 };
1328
1329
1330 #ifndef VERSION_STRING
1331 #define VERSION_STRING
1332 #endif
1333 CODEC_INTERFACE(vpx_codec_vp8_cx) =
1334 {
1335 "WebM Project VP8 Encoder" VERSION_STRING,
1336 VPX_CODEC_INTERNAL_ABI_VERSION,
1337 VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
1338 VPX_CODEC_CAP_OUTPUT_PARTITION,
1339 /* vpx_codec_caps_t caps; */
1340 vp8e_init, /* vpx_codec_init_fn_t init; */
1341 vp8e_destroy, /* vpx_codec_destroy_fn_t destroy; */
1342 vp8e_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
1343 {
1344 NOT_IMPLEMENTED, /* vpx_codec_peek_si_fn_t peek_si; */
1345 NOT_IMPLEMENTED, /* vpx_codec_get_si_fn_t get_si; */
1346 NOT_IMPLEMENTED, /* vpx_codec_decode_fn_t decode; */
1347 NOT_IMPLEMENTED, /* vpx_codec_frame_get_fn_t frame_get; */
1348 },
1349 {
1350 1, /* 1 cfg map */
1351 vp8e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t peek_si; */
1352 vp8e_encode, /* vpx_codec_encode_fn_t encode; */
1353 vp8e_get_cxdata, /* vpx_codec_get_cx_data_fn_t frame_get; */
1354 vp8e_set_config,
1355 NOT_IMPLEMENTED,
1356 vp8e_get_preview,
1357 vp8e_mr_alloc_mem,
1358 } /* encoder functions */
1359 };
1360