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