vp10: allow MV refs to point outside visible image.
[platform/upstream/libvpx.git] / vp9 / vp9_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 <stdlib.h>
12 #include <string.h>
13
14 #include "./vpx_config.h"
15 #include "vpx/vpx_encoder.h"
16 #include "vpx_ports/vpx_once.h"
17 #include "vpx/internal/vpx_codec_internal.h"
18 #include "./vpx_version.h"
19 #include "vp9/encoder/vp9_encoder.h"
20 #include "vpx/vp8cx.h"
21 #include "vp9/encoder/vp9_firstpass.h"
22 #include "vp9/vp9_iface_common.h"
23
24 struct vp9_extracfg {
25   int                         cpu_used;  // available cpu percentage in 1/16
26   unsigned int                enable_auto_alt_ref;
27   unsigned int                noise_sensitivity;
28   unsigned int                sharpness;
29   unsigned int                static_thresh;
30   unsigned int                tile_columns;
31   unsigned int                tile_rows;
32   unsigned int                arnr_max_frames;
33   unsigned int                arnr_strength;
34   unsigned int                min_gf_interval;
35   unsigned int                max_gf_interval;
36   vp8e_tuning                 tuning;
37   unsigned int                cq_level;  // constrained quality level
38   unsigned int                rc_max_intra_bitrate_pct;
39   unsigned int                rc_max_inter_bitrate_pct;
40   unsigned int                gf_cbr_boost_pct;
41   unsigned int                lossless;
42   unsigned int                frame_parallel_decoding_mode;
43   AQ_MODE                     aq_mode;
44   unsigned int                frame_periodic_boost;
45   vpx_bit_depth_t             bit_depth;
46   vp9e_tune_content           content;
47   vpx_color_space_t           color_space;
48   int                         color_range;
49   int                         render_width;
50   int                         render_height;
51 };
52
53 static struct vp9_extracfg default_extra_cfg = {
54   0,                          // cpu_used
55   1,                          // enable_auto_alt_ref
56   0,                          // noise_sensitivity
57   0,                          // sharpness
58   0,                          // static_thresh
59   6,                          // tile_columns
60   0,                          // tile_rows
61   7,                          // arnr_max_frames
62   5,                          // arnr_strength
63   0,                          // min_gf_interval; 0 -> default decision
64   0,                          // max_gf_interval; 0 -> default decision
65   VP8_TUNE_PSNR,              // tuning
66   10,                         // cq_level
67   0,                          // rc_max_intra_bitrate_pct
68   0,                          // rc_max_inter_bitrate_pct
69   0,                          // gf_cbr_boost_pct
70   0,                          // lossless
71   1,                          // frame_parallel_decoding_mode
72   NO_AQ,                      // aq_mode
73   0,                          // frame_periodic_delta_q
74   VPX_BITS_8,                 // Bit depth
75   VP9E_CONTENT_DEFAULT,       // content
76   VPX_CS_UNKNOWN,             // color space
77   0,                          // color range
78   0,                          // render width
79   0,                          // render height
80 };
81
82 struct vpx_codec_alg_priv {
83   vpx_codec_priv_t        base;
84   vpx_codec_enc_cfg_t     cfg;
85   struct vp9_extracfg     extra_cfg;
86   VP9EncoderConfig        oxcf;
87   VP9_COMP               *cpi;
88   unsigned char          *cx_data;
89   size_t                  cx_data_sz;
90   unsigned char          *pending_cx_data;
91   size_t                  pending_cx_data_sz;
92   int                     pending_frame_count;
93   size_t                  pending_frame_sizes[8];
94   size_t                  pending_frame_magnitude;
95   vpx_image_t             preview_img;
96   vpx_enc_frame_flags_t   next_frame_flags;
97   vp8_postproc_cfg_t      preview_ppcfg;
98   vpx_codec_pkt_list_decl(256) pkt_list;
99   unsigned int                 fixed_kf_cntr;
100   vpx_codec_priv_output_cx_pkt_cb_pair_t output_cx_pkt_cb;
101   // BufferPool that holds all reference frames.
102   BufferPool              *buffer_pool;
103 };
104
105 static VP9_REFFRAME ref_frame_to_vp9_reframe(vpx_ref_frame_type_t frame) {
106   switch (frame) {
107     case VP8_LAST_FRAME:
108       return VP9_LAST_FLAG;
109     case VP8_GOLD_FRAME:
110       return VP9_GOLD_FLAG;
111     case VP8_ALTR_FRAME:
112       return VP9_ALT_FLAG;
113   }
114   assert(0 && "Invalid Reference Frame");
115   return VP9_LAST_FLAG;
116 }
117
118 static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
119     const struct vpx_internal_error_info *error) {
120   const vpx_codec_err_t res = error->error_code;
121
122   if (res != VPX_CODEC_OK)
123     ctx->base.err_detail = error->has_detail ? error->detail : NULL;
124
125   return res;
126 }
127
128
129 #undef ERROR
130 #define ERROR(str) do {\
131     ctx->base.err_detail = str;\
132     return VPX_CODEC_INVALID_PARAM;\
133   } while (0)
134
135 #define RANGE_CHECK(p, memb, lo, hi) do {\
136     if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
137       ERROR(#memb " out of range ["#lo".."#hi"]");\
138   } while (0)
139
140 #define RANGE_CHECK_HI(p, memb, hi) do {\
141     if (!((p)->memb <= (hi))) \
142       ERROR(#memb " out of range [.."#hi"]");\
143   } while (0)
144
145 #define RANGE_CHECK_LO(p, memb, lo) do {\
146     if (!((p)->memb >= (lo))) \
147       ERROR(#memb " out of range ["#lo"..]");\
148   } while (0)
149
150 #define RANGE_CHECK_BOOL(p, memb) do {\
151     if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
152   } while (0)
153
154 static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
155                                        const vpx_codec_enc_cfg_t *cfg,
156                                        const struct vp9_extracfg *extra_cfg) {
157   RANGE_CHECK(cfg, g_w,                   1, 65535);  // 16 bits available
158   RANGE_CHECK(cfg, g_h,                   1, 65535);  // 16 bits available
159   RANGE_CHECK(cfg, g_timebase.den,        1, 1000000000);
160   RANGE_CHECK(cfg, g_timebase.num,        1, cfg->g_timebase.den);
161   RANGE_CHECK_HI(cfg, g_profile,          3);
162
163   RANGE_CHECK_HI(cfg, rc_max_quantizer,   63);
164   RANGE_CHECK_HI(cfg, rc_min_quantizer,   cfg->rc_max_quantizer);
165   RANGE_CHECK_BOOL(extra_cfg, lossless);
166   RANGE_CHECK(extra_cfg, aq_mode,           0, AQ_MODE_COUNT - 1);
167   RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
168   RANGE_CHECK_HI(cfg, g_threads,          64);
169   RANGE_CHECK_HI(cfg, g_lag_in_frames,    MAX_LAG_BUFFERS);
170   RANGE_CHECK(cfg, rc_end_usage,          VPX_VBR, VPX_Q);
171   RANGE_CHECK_HI(cfg, rc_undershoot_pct,  100);
172   RANGE_CHECK_HI(cfg, rc_overshoot_pct,   100);
173   RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
174   RANGE_CHECK(cfg, kf_mode,               VPX_KF_DISABLED, VPX_KF_AUTO);
175   RANGE_CHECK_BOOL(cfg,                   rc_resize_allowed);
176   RANGE_CHECK_HI(cfg, rc_dropframe_thresh,   100);
177   RANGE_CHECK_HI(cfg, rc_resize_up_thresh,   100);
178   RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
179   RANGE_CHECK(cfg,        g_pass,         VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
180   RANGE_CHECK(extra_cfg, min_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
181   RANGE_CHECK(extra_cfg, max_gf_interval, 0, (MAX_LAG_BUFFERS - 1));
182   if (extra_cfg->max_gf_interval > 0) {
183     RANGE_CHECK(extra_cfg, max_gf_interval, 2, (MAX_LAG_BUFFERS - 1));
184   }
185   if (extra_cfg->min_gf_interval > 0 && extra_cfg->max_gf_interval > 0) {
186     RANGE_CHECK(extra_cfg, max_gf_interval, extra_cfg->min_gf_interval,
187       (MAX_LAG_BUFFERS - 1));
188   }
189
190   if (cfg->rc_resize_allowed == 1) {
191     RANGE_CHECK(cfg, rc_scaled_width, 0, cfg->g_w);
192     RANGE_CHECK(cfg, rc_scaled_height, 0, cfg->g_h);
193   }
194
195   RANGE_CHECK(cfg, ss_number_layers, 1, VPX_SS_MAX_LAYERS);
196   RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
197
198   if (cfg->ss_number_layers * cfg->ts_number_layers > VPX_MAX_LAYERS)
199     ERROR("ss_number_layers * ts_number_layers is out of range");
200   if (cfg->ts_number_layers > 1) {
201     unsigned int sl, tl;
202     for (sl = 1; sl < cfg->ss_number_layers; ++sl) {
203       for (tl = 1; tl < cfg->ts_number_layers; ++tl) {
204         const int layer =
205             LAYER_IDS_TO_IDX(sl, tl, cfg->ts_number_layers);
206         if (cfg->layer_target_bitrate[layer] <
207             cfg->layer_target_bitrate[layer - 1])
208         ERROR("ts_target_bitrate entries are not increasing");
209       }
210     }
211
212     RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
213     for (tl = cfg->ts_number_layers - 2; tl > 0; --tl)
214       if (cfg->ts_rate_decimator[tl - 1] != 2 * cfg->ts_rate_decimator[tl])
215         ERROR("ts_rate_decimator factors are not powers of 2");
216   }
217
218 #if CONFIG_SPATIAL_SVC
219
220   if ((cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) &&
221       cfg->g_pass == VPX_RC_LAST_PASS) {
222     unsigned int i, alt_ref_sum = 0;
223     for (i = 0; i < cfg->ss_number_layers; ++i) {
224       if (cfg->ss_enable_auto_alt_ref[i])
225         ++alt_ref_sum;
226     }
227     if (alt_ref_sum > REF_FRAMES - cfg->ss_number_layers)
228       ERROR("Not enough ref buffers for svc alt ref frames");
229     if (cfg->ss_number_layers * cfg->ts_number_layers > 3 &&
230         cfg->g_error_resilient == 0)
231     ERROR("Multiple frame context are not supported for more than 3 layers");
232   }
233 #endif
234
235   // VP9 does not support a lower bound on the keyframe interval in
236   // automatic keyframe placement mode.
237   if (cfg->kf_mode != VPX_KF_DISABLED &&
238       cfg->kf_min_dist != cfg->kf_max_dist &&
239       cfg->kf_min_dist > 0)
240     ERROR("kf_min_dist not supported in auto mode, use 0 "
241           "or kf_max_dist instead.");
242
243   RANGE_CHECK(extra_cfg, enable_auto_alt_ref, 0, 2);
244   RANGE_CHECK(extra_cfg, cpu_used, -8, 8);
245   RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
246   RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
247   RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
248   RANGE_CHECK_HI(extra_cfg, sharpness, 7);
249   RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
250   RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
251   RANGE_CHECK(extra_cfg, cq_level, 0, 63);
252   RANGE_CHECK(cfg, g_bit_depth, VPX_BITS_8, VPX_BITS_12);
253   RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
254   RANGE_CHECK(extra_cfg, content,
255               VP9E_CONTENT_DEFAULT, VP9E_CONTENT_INVALID - 1);
256
257   // TODO(yaowu): remove this when ssim tuning is implemented for vp9
258   if (extra_cfg->tuning == VP8_TUNE_SSIM)
259       ERROR("Option --tune=ssim is not currently supported in VP9.");
260
261   if (cfg->g_pass == VPX_RC_LAST_PASS) {
262     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
263     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
264     const FIRSTPASS_STATS *stats;
265
266     if (cfg->rc_twopass_stats_in.buf == NULL)
267       ERROR("rc_twopass_stats_in.buf not set.");
268
269     if (cfg->rc_twopass_stats_in.sz % packet_sz)
270       ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
271
272     if (cfg->ss_number_layers > 1 || cfg->ts_number_layers > 1) {
273       int i;
274       unsigned int n_packets_per_layer[VPX_SS_MAX_LAYERS] = {0};
275
276       stats = cfg->rc_twopass_stats_in.buf;
277       for (i = 0; i < n_packets; ++i) {
278         const int layer_id = (int)stats[i].spatial_layer_id;
279         if (layer_id >= 0 && layer_id < (int)cfg->ss_number_layers) {
280           ++n_packets_per_layer[layer_id];
281         }
282       }
283
284       for (i = 0; i < (int)cfg->ss_number_layers; ++i) {
285         unsigned int layer_id;
286         if (n_packets_per_layer[i] < 2) {
287           ERROR("rc_twopass_stats_in requires at least two packets for each "
288                 "layer.");
289         }
290
291         stats = (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf +
292                 n_packets - cfg->ss_number_layers + i;
293         layer_id = (int)stats->spatial_layer_id;
294
295         if (layer_id >= cfg->ss_number_layers
296             ||(unsigned int)(stats->count + 0.5) !=
297                n_packets_per_layer[layer_id] - 1)
298           ERROR("rc_twopass_stats_in missing EOS stats packet");
299       }
300     } else {
301       if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
302         ERROR("rc_twopass_stats_in requires at least two packets.");
303
304       stats =
305           (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;
306
307       if ((int)(stats->count + 0.5) != n_packets - 1)
308         ERROR("rc_twopass_stats_in missing EOS stats packet");
309     }
310   }
311
312 #if !CONFIG_VP9_HIGHBITDEPTH
313   if (cfg->g_profile > (unsigned int)PROFILE_1) {
314     ERROR("Profile > 1 not supported in this build configuration");
315   }
316 #endif
317   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
318       cfg->g_bit_depth > VPX_BITS_8) {
319     ERROR("Codec high bit-depth not supported in profile < 2");
320   }
321   if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
322       cfg->g_input_bit_depth > 8) {
323     ERROR("Source high bit-depth not supported in profile < 2");
324   }
325   if (cfg->g_profile > (unsigned int)PROFILE_1 &&
326       cfg->g_bit_depth == VPX_BITS_8) {
327     ERROR("Codec bit-depth 8 not supported in profile > 1");
328   }
329   RANGE_CHECK(extra_cfg, color_space, VPX_CS_UNKNOWN, VPX_CS_SRGB);
330   RANGE_CHECK(extra_cfg, color_range, 0, 2);
331   return VPX_CODEC_OK;
332 }
333
334 static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
335                                     const vpx_image_t *img) {
336   switch (img->fmt) {
337     case VPX_IMG_FMT_YV12:
338     case VPX_IMG_FMT_I420:
339     case VPX_IMG_FMT_I42016:
340       break;
341     case VPX_IMG_FMT_I422:
342     case VPX_IMG_FMT_I444:
343     case VPX_IMG_FMT_I440:
344       if (ctx->cfg.g_profile != (unsigned int)PROFILE_1) {
345         ERROR("Invalid image format. I422, I444, I440 images are "
346               "not supported in profile.");
347       }
348       break;
349     case VPX_IMG_FMT_I42216:
350     case VPX_IMG_FMT_I44416:
351     case VPX_IMG_FMT_I44016:
352       if (ctx->cfg.g_profile != (unsigned int)PROFILE_1 &&
353           ctx->cfg.g_profile != (unsigned int)PROFILE_3) {
354         ERROR("Invalid image format. 16-bit I422, I444, I440 images are "
355               "not supported in profile.");
356       }
357       break;
358     default:
359       ERROR("Invalid image format. Only YV12, I420, I422, I444 images are "
360             "supported.");
361       break;
362   }
363
364   if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
365     ERROR("Image size must match encoder init configuration size");
366
367   return VPX_CODEC_OK;
368 }
369
370 static int get_image_bps(const vpx_image_t *img) {
371   switch (img->fmt) {
372     case VPX_IMG_FMT_YV12:
373     case VPX_IMG_FMT_I420: return 12;
374     case VPX_IMG_FMT_I422: return 16;
375     case VPX_IMG_FMT_I444: return 24;
376     case VPX_IMG_FMT_I440: return 16;
377     case VPX_IMG_FMT_I42016: return 24;
378     case VPX_IMG_FMT_I42216: return 32;
379     case VPX_IMG_FMT_I44416: return 48;
380     case VPX_IMG_FMT_I44016: return 32;
381     default: assert(0 && "Invalid image format"); break;
382   }
383   return 0;
384 }
385
386 static vpx_codec_err_t set_encoder_config(
387   VP9EncoderConfig *oxcf,
388   const vpx_codec_enc_cfg_t *cfg,
389   const struct vp9_extracfg *extra_cfg) {
390   const int is_vbr = cfg->rc_end_usage == VPX_VBR;
391   int sl, tl;
392   oxcf->profile = cfg->g_profile;
393   oxcf->max_threads = (int)cfg->g_threads;
394   oxcf->width   = cfg->g_w;
395   oxcf->height  = cfg->g_h;
396   oxcf->bit_depth = cfg->g_bit_depth;
397   oxcf->input_bit_depth = cfg->g_input_bit_depth;
398   // guess a frame rate if out of whack, use 30
399   oxcf->init_framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
400   if (oxcf->init_framerate > 180)
401     oxcf->init_framerate = 30;
402
403   oxcf->mode = GOOD;
404
405   switch (cfg->g_pass) {
406     case VPX_RC_ONE_PASS:
407       oxcf->pass = 0;
408       break;
409     case VPX_RC_FIRST_PASS:
410       oxcf->pass = 1;
411       break;
412     case VPX_RC_LAST_PASS:
413       oxcf->pass = 2;
414       break;
415   }
416
417   oxcf->lag_in_frames = cfg->g_pass == VPX_RC_FIRST_PASS ? 0
418                                                          : cfg->g_lag_in_frames;
419   oxcf->rc_mode = cfg->rc_end_usage;
420
421   // Convert target bandwidth from Kbit/s to Bit/s
422   oxcf->target_bandwidth = 1000 * cfg->rc_target_bitrate;
423   oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
424   oxcf->rc_max_inter_bitrate_pct = extra_cfg->rc_max_inter_bitrate_pct;
425   oxcf->gf_cbr_boost_pct = extra_cfg->gf_cbr_boost_pct;
426
427   oxcf->best_allowed_q =
428       extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_min_quantizer);
429   oxcf->worst_allowed_q =
430       extra_cfg->lossless ? 0 : vp9_quantizer_to_qindex(cfg->rc_max_quantizer);
431   oxcf->cq_level        = vp9_quantizer_to_qindex(extra_cfg->cq_level);
432   oxcf->fixed_q = -1;
433
434   oxcf->under_shoot_pct         = cfg->rc_undershoot_pct;
435   oxcf->over_shoot_pct          = cfg->rc_overshoot_pct;
436
437   oxcf->scaled_frame_width  = cfg->rc_scaled_width;
438   oxcf->scaled_frame_height = cfg->rc_scaled_height;
439   if (cfg->rc_resize_allowed == 1) {
440     oxcf->resize_mode =
441         (oxcf->scaled_frame_width == 0 || oxcf->scaled_frame_height == 0) ?
442             RESIZE_DYNAMIC : RESIZE_FIXED;
443   } else {
444     oxcf->resize_mode = RESIZE_NONE;
445   }
446
447   oxcf->maximum_buffer_size_ms   = is_vbr ? 240000 : cfg->rc_buf_sz;
448   oxcf->starting_buffer_level_ms = is_vbr ? 60000 : cfg->rc_buf_initial_sz;
449   oxcf->optimal_buffer_level_ms  = is_vbr ? 60000 : cfg->rc_buf_optimal_sz;
450
451   oxcf->drop_frames_water_mark   = cfg->rc_dropframe_thresh;
452
453   oxcf->two_pass_vbrbias         = cfg->rc_2pass_vbr_bias_pct;
454   oxcf->two_pass_vbrmin_section  = cfg->rc_2pass_vbr_minsection_pct;
455   oxcf->two_pass_vbrmax_section  = cfg->rc_2pass_vbr_maxsection_pct;
456
457   oxcf->auto_key               = cfg->kf_mode == VPX_KF_AUTO &&
458                                  cfg->kf_min_dist != cfg->kf_max_dist;
459
460   oxcf->key_freq               = cfg->kf_max_dist;
461
462   oxcf->speed                  =  abs(extra_cfg->cpu_used);
463   oxcf->encode_breakout        =  extra_cfg->static_thresh;
464   oxcf->enable_auto_arf        =  extra_cfg->enable_auto_alt_ref;
465   oxcf->noise_sensitivity      =  extra_cfg->noise_sensitivity;
466   oxcf->sharpness              =  extra_cfg->sharpness;
467
468   oxcf->two_pass_stats_in      =  cfg->rc_twopass_stats_in;
469
470 #if CONFIG_FP_MB_STATS
471   oxcf->firstpass_mb_stats_in  = cfg->rc_firstpass_mb_stats_in;
472 #endif
473
474   oxcf->color_space = extra_cfg->color_space;
475   oxcf->color_range = extra_cfg->color_range;
476   oxcf->render_width  = extra_cfg->render_width;
477   oxcf->render_height = extra_cfg->render_height;
478   oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
479   oxcf->arnr_strength   = extra_cfg->arnr_strength;
480   oxcf->min_gf_interval = extra_cfg->min_gf_interval;
481   oxcf->max_gf_interval = extra_cfg->max_gf_interval;
482
483   oxcf->tuning = extra_cfg->tuning;
484   oxcf->content = extra_cfg->content;
485
486   oxcf->tile_columns = extra_cfg->tile_columns;
487   oxcf->tile_rows    = extra_cfg->tile_rows;
488
489   oxcf->error_resilient_mode         = cfg->g_error_resilient;
490   oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
491
492   oxcf->aq_mode = extra_cfg->aq_mode;
493
494   oxcf->frame_periodic_boost =  extra_cfg->frame_periodic_boost;
495
496   oxcf->ss_number_layers = cfg->ss_number_layers;
497   oxcf->ts_number_layers = cfg->ts_number_layers;
498   oxcf->temporal_layering_mode = (enum vp9e_temporal_layering_mode)
499       cfg->temporal_layering_mode;
500
501   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
502 #if CONFIG_SPATIAL_SVC
503     oxcf->ss_enable_auto_arf[sl] = cfg->ss_enable_auto_alt_ref[sl];
504 #endif
505     for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
506       oxcf->layer_target_bitrate[sl * oxcf->ts_number_layers + tl] =
507           1000 * cfg->layer_target_bitrate[sl * oxcf->ts_number_layers + tl];
508     }
509   }
510   if (oxcf->ss_number_layers == 1 && oxcf->pass != 0) {
511     oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
512 #if CONFIG_SPATIAL_SVC
513     oxcf->ss_enable_auto_arf[0] = extra_cfg->enable_auto_alt_ref;
514 #endif
515   }
516   if (oxcf->ts_number_layers > 1) {
517     for (tl = 0; tl < VPX_TS_MAX_LAYERS; ++tl) {
518       oxcf->ts_rate_decimator[tl] = cfg->ts_rate_decimator[tl] ?
519           cfg->ts_rate_decimator[tl] : 1;
520     }
521   } else if (oxcf->ts_number_layers == 1) {
522     oxcf->ts_rate_decimator[0] = 1;
523   }
524   /*
525   printf("Current VP9 Settings: \n");
526   printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
527   printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
528   printf("sharpness: %d\n",    oxcf->sharpness);
529   printf("cpu_used: %d\n",  oxcf->cpu_used);
530   printf("Mode: %d\n",     oxcf->mode);
531   printf("auto_key: %d\n",  oxcf->auto_key);
532   printf("key_freq: %d\n", oxcf->key_freq);
533   printf("end_usage: %d\n", oxcf->end_usage);
534   printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
535   printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
536   printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
537   printf("optimal_buffer_level: %d\n",  oxcf->optimal_buffer_level);
538   printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
539   printf("fixed_q: %d\n",  oxcf->fixed_q);
540   printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
541   printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
542   printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
543   printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
544   printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
545   printf("two_pass_vbrbias: %d\n",  oxcf->two_pass_vbrbias);
546   printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
547   printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
548   printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
549   printf("enable_auto_arf: %d\n", oxcf->enable_auto_arf);
550   printf("Version: %d\n", oxcf->Version);
551   printf("encode_breakout: %d\n", oxcf->encode_breakout);
552   printf("error resilient: %d\n", oxcf->error_resilient_mode);
553   printf("frame parallel detokenization: %d\n",
554          oxcf->frame_parallel_decoding_mode);
555   */
556   return VPX_CODEC_OK;
557 }
558
559 static vpx_codec_err_t encoder_set_config(vpx_codec_alg_priv_t *ctx,
560                                           const vpx_codec_enc_cfg_t  *cfg) {
561   vpx_codec_err_t res;
562   int force_key = 0;
563
564   if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h) {
565     if (cfg->g_lag_in_frames > 1 || cfg->g_pass != VPX_RC_ONE_PASS)
566       ERROR("Cannot change width or height after initialization");
567     if (!valid_ref_frame_size(ctx->cfg.g_w, ctx->cfg.g_h, cfg->g_w, cfg->g_h) ||
568         (ctx->cpi->initial_width && (int)cfg->g_w > ctx->cpi->initial_width) ||
569         (ctx->cpi->initial_height && (int)cfg->g_h > ctx->cpi->initial_height))
570       force_key = 1;
571   }
572
573   // Prevent increasing lag_in_frames. This check is stricter than it needs
574   // to be -- the limit is not increasing past the first lag_in_frames
575   // value, but we don't track the initial config, only the last successful
576   // config.
577   if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
578     ERROR("Cannot increase lag_in_frames");
579
580   res = validate_config(ctx, cfg, &ctx->extra_cfg);
581
582   if (res == VPX_CODEC_OK) {
583     ctx->cfg = *cfg;
584     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
585     // On profile change, request a key frame
586     force_key |= ctx->cpi->common.profile != ctx->oxcf.profile;
587     vp9_change_config(ctx->cpi, &ctx->oxcf);
588   }
589
590   if (force_key)
591     ctx->next_frame_flags |= VPX_EFLAG_FORCE_KF;
592
593   return res;
594 }
595
596 static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
597                                           va_list args) {
598   int *const arg = va_arg(args, int *);
599   if (arg == NULL)
600     return VPX_CODEC_INVALID_PARAM;
601   *arg = vp9_get_quantizer(ctx->cpi);
602   return VPX_CODEC_OK;
603 }
604
605 static vpx_codec_err_t ctrl_get_quantizer64(vpx_codec_alg_priv_t *ctx,
606                                             va_list args) {
607   int *const arg = va_arg(args, int *);
608   if (arg == NULL)
609     return VPX_CODEC_INVALID_PARAM;
610   *arg = vp9_qindex_to_quantizer(vp9_get_quantizer(ctx->cpi));
611   return VPX_CODEC_OK;
612 }
613
614 static vpx_codec_err_t update_extra_cfg(vpx_codec_alg_priv_t *ctx,
615                                         const struct vp9_extracfg *extra_cfg) {
616   const vpx_codec_err_t res = validate_config(ctx, &ctx->cfg, extra_cfg);
617   if (res == VPX_CODEC_OK) {
618     ctx->extra_cfg = *extra_cfg;
619     set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
620     vp9_change_config(ctx->cpi, &ctx->oxcf);
621   }
622   return res;
623 }
624
625 static vpx_codec_err_t ctrl_set_cpuused(vpx_codec_alg_priv_t *ctx,
626                                         va_list args) {
627   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
628   extra_cfg.cpu_used = CAST(VP8E_SET_CPUUSED, args);
629   return update_extra_cfg(ctx, &extra_cfg);
630 }
631
632 static vpx_codec_err_t ctrl_set_enable_auto_alt_ref(vpx_codec_alg_priv_t *ctx,
633                                                     va_list args) {
634   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
635   extra_cfg.enable_auto_alt_ref = CAST(VP8E_SET_ENABLEAUTOALTREF, args);
636   return update_extra_cfg(ctx, &extra_cfg);
637 }
638
639 static vpx_codec_err_t ctrl_set_noise_sensitivity(vpx_codec_alg_priv_t *ctx,
640                                                   va_list args) {
641   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
642   extra_cfg.noise_sensitivity = CAST(VP9E_SET_NOISE_SENSITIVITY, args);
643   return update_extra_cfg(ctx, &extra_cfg);
644 }
645
646 static vpx_codec_err_t ctrl_set_sharpness(vpx_codec_alg_priv_t *ctx,
647                                           va_list args) {
648   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
649   extra_cfg.sharpness = CAST(VP8E_SET_SHARPNESS, args);
650   return update_extra_cfg(ctx, &extra_cfg);
651 }
652
653 static vpx_codec_err_t ctrl_set_static_thresh(vpx_codec_alg_priv_t *ctx,
654                                               va_list args) {
655   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
656   extra_cfg.static_thresh = CAST(VP8E_SET_STATIC_THRESHOLD, args);
657   return update_extra_cfg(ctx, &extra_cfg);
658 }
659
660 static vpx_codec_err_t ctrl_set_tile_columns(vpx_codec_alg_priv_t *ctx,
661                                              va_list args) {
662   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
663   extra_cfg.tile_columns = CAST(VP9E_SET_TILE_COLUMNS, args);
664   return update_extra_cfg(ctx, &extra_cfg);
665 }
666
667 static vpx_codec_err_t ctrl_set_tile_rows(vpx_codec_alg_priv_t *ctx,
668                                           va_list args) {
669   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
670   extra_cfg.tile_rows = CAST(VP9E_SET_TILE_ROWS, args);
671   return update_extra_cfg(ctx, &extra_cfg);
672 }
673
674 static vpx_codec_err_t ctrl_set_arnr_max_frames(vpx_codec_alg_priv_t *ctx,
675                                                 va_list args) {
676   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
677   extra_cfg.arnr_max_frames = CAST(VP8E_SET_ARNR_MAXFRAMES, args);
678   return update_extra_cfg(ctx, &extra_cfg);
679 }
680
681 static vpx_codec_err_t ctrl_set_arnr_strength(vpx_codec_alg_priv_t *ctx,
682                                               va_list args) {
683   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
684   extra_cfg.arnr_strength = CAST(VP8E_SET_ARNR_STRENGTH, args);
685   return update_extra_cfg(ctx, &extra_cfg);
686 }
687
688 static vpx_codec_err_t ctrl_set_arnr_type(vpx_codec_alg_priv_t *ctx,
689                                           va_list args) {
690   (void)ctx;
691   (void)args;
692   return VPX_CODEC_OK;
693 }
694
695 static vpx_codec_err_t ctrl_set_tuning(vpx_codec_alg_priv_t *ctx,
696                                        va_list args) {
697   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
698   extra_cfg.tuning = CAST(VP8E_SET_TUNING, args);
699   return update_extra_cfg(ctx, &extra_cfg);
700 }
701
702 static vpx_codec_err_t ctrl_set_cq_level(vpx_codec_alg_priv_t *ctx,
703                                          va_list args) {
704   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
705   extra_cfg.cq_level = CAST(VP8E_SET_CQ_LEVEL, args);
706   return update_extra_cfg(ctx, &extra_cfg);
707 }
708
709 static vpx_codec_err_t ctrl_set_rc_max_intra_bitrate_pct(
710     vpx_codec_alg_priv_t *ctx, va_list args) {
711   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
712   extra_cfg.rc_max_intra_bitrate_pct =
713       CAST(VP8E_SET_MAX_INTRA_BITRATE_PCT, args);
714   return update_extra_cfg(ctx, &extra_cfg);
715 }
716
717 static vpx_codec_err_t ctrl_set_rc_max_inter_bitrate_pct(
718     vpx_codec_alg_priv_t *ctx, va_list args) {
719   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
720   extra_cfg.rc_max_inter_bitrate_pct =
721       CAST(VP8E_SET_MAX_INTER_BITRATE_PCT, args);
722   return update_extra_cfg(ctx, &extra_cfg);
723 }
724
725 static vpx_codec_err_t ctrl_set_rc_gf_cbr_boost_pct(
726     vpx_codec_alg_priv_t *ctx, va_list args) {
727   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
728   extra_cfg.gf_cbr_boost_pct =
729       CAST(VP9E_SET_GF_CBR_BOOST_PCT, args);
730   return update_extra_cfg(ctx, &extra_cfg);
731 }
732
733 static vpx_codec_err_t ctrl_set_lossless(vpx_codec_alg_priv_t *ctx,
734                                          va_list args) {
735   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
736   extra_cfg.lossless = CAST(VP9E_SET_LOSSLESS, args);
737   return update_extra_cfg(ctx, &extra_cfg);
738 }
739
740 static vpx_codec_err_t ctrl_set_frame_parallel_decoding_mode(
741     vpx_codec_alg_priv_t *ctx, va_list args) {
742   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
743   extra_cfg.frame_parallel_decoding_mode =
744       CAST(VP9E_SET_FRAME_PARALLEL_DECODING, args);
745   return update_extra_cfg(ctx, &extra_cfg);
746 }
747
748 static vpx_codec_err_t ctrl_set_aq_mode(vpx_codec_alg_priv_t *ctx,
749                                         va_list args) {
750   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
751   extra_cfg.aq_mode = CAST(VP9E_SET_AQ_MODE, args);
752   return update_extra_cfg(ctx, &extra_cfg);
753 }
754
755 static vpx_codec_err_t ctrl_set_min_gf_interval(vpx_codec_alg_priv_t *ctx,
756                                                 va_list args) {
757   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
758   extra_cfg.min_gf_interval = CAST(VP9E_SET_MIN_GF_INTERVAL, args);
759   return update_extra_cfg(ctx, &extra_cfg);
760 }
761
762 static vpx_codec_err_t ctrl_set_max_gf_interval(vpx_codec_alg_priv_t *ctx,
763                                                 va_list args) {
764   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
765   extra_cfg.max_gf_interval = CAST(VP9E_SET_MAX_GF_INTERVAL, args);
766   return update_extra_cfg(ctx, &extra_cfg);
767 }
768
769 static vpx_codec_err_t ctrl_set_frame_periodic_boost(vpx_codec_alg_priv_t *ctx,
770                                                      va_list args) {
771   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
772   extra_cfg.frame_periodic_boost = CAST(VP9E_SET_FRAME_PERIODIC_BOOST, args);
773   return update_extra_cfg(ctx, &extra_cfg);
774 }
775
776 static vpx_codec_err_t encoder_init(vpx_codec_ctx_t *ctx,
777                                     vpx_codec_priv_enc_mr_cfg_t *data) {
778   vpx_codec_err_t res = VPX_CODEC_OK;
779   (void)data;
780
781   if (ctx->priv == NULL) {
782     vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
783     if (priv == NULL)
784       return VPX_CODEC_MEM_ERROR;
785
786     ctx->priv = (vpx_codec_priv_t *)priv;
787     ctx->priv->init_flags = ctx->init_flags;
788     ctx->priv->enc.total_encoders = 1;
789     priv->buffer_pool =
790         (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
791     if (priv->buffer_pool == NULL)
792       return VPX_CODEC_MEM_ERROR;
793
794 #if CONFIG_MULTITHREAD
795     if (pthread_mutex_init(&priv->buffer_pool->pool_mutex, NULL)) {
796       return VPX_CODEC_MEM_ERROR;
797     }
798 #endif
799
800     if (ctx->config.enc) {
801       // Update the reference to the config structure to an internal copy.
802       priv->cfg = *ctx->config.enc;
803       ctx->config.enc = &priv->cfg;
804     }
805
806     priv->extra_cfg = default_extra_cfg;
807     once(vp9_initialize_enc);
808
809     res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
810
811     if (res == VPX_CODEC_OK) {
812       set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
813 #if CONFIG_VP9_HIGHBITDEPTH
814       priv->oxcf.use_highbitdepth =
815           (ctx->init_flags & VPX_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
816 #endif
817       priv->cpi = vp9_create_compressor(&priv->oxcf, priv->buffer_pool);
818       if (priv->cpi == NULL)
819         res = VPX_CODEC_MEM_ERROR;
820       else
821         priv->cpi->output_pkt_list = &priv->pkt_list.head;
822     }
823   }
824
825   return res;
826 }
827
828 static vpx_codec_err_t encoder_destroy(vpx_codec_alg_priv_t *ctx) {
829   free(ctx->cx_data);
830   vp9_remove_compressor(ctx->cpi);
831 #if CONFIG_MULTITHREAD
832   pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
833 #endif
834   vpx_free(ctx->buffer_pool);
835   vpx_free(ctx);
836   return VPX_CODEC_OK;
837 }
838
839 static void pick_quickcompress_mode(vpx_codec_alg_priv_t *ctx,
840                                     unsigned long duration,
841                                     unsigned long deadline) {
842   MODE new_mode = BEST;
843
844   switch (ctx->cfg.g_pass) {
845     case VPX_RC_ONE_PASS:
846       if (deadline > 0) {
847         const vpx_codec_enc_cfg_t *const cfg = &ctx->cfg;
848
849         // Convert duration parameter from stream timebase to microseconds.
850         const uint64_t duration_us = (uint64_t)duration * 1000000 *
851            (uint64_t)cfg->g_timebase.num /(uint64_t)cfg->g_timebase.den;
852
853         // If the deadline is more that the duration this frame is to be shown,
854         // use good quality mode. Otherwise use realtime mode.
855         new_mode = (deadline > duration_us) ? GOOD : REALTIME;
856       } else {
857         new_mode = BEST;
858       }
859       break;
860     case VPX_RC_FIRST_PASS:
861       break;
862     case VPX_RC_LAST_PASS:
863       new_mode = deadline > 0 ? GOOD : BEST;
864       break;
865   }
866
867   if (ctx->oxcf.mode != new_mode) {
868     ctx->oxcf.mode = new_mode;
869     vp9_change_config(ctx->cpi, &ctx->oxcf);
870   }
871 }
872
873 // Turn on to test if supplemental superframe data breaks decoding
874 // #define TEST_SUPPLEMENTAL_SUPERFRAME_DATA
875 static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
876   uint8_t marker = 0xc0;
877   unsigned int mask;
878   int mag, index_sz;
879
880   assert(ctx->pending_frame_count);
881   assert(ctx->pending_frame_count <= 8);
882
883   // Add the number of frames to the marker byte
884   marker |= ctx->pending_frame_count - 1;
885
886   // Choose the magnitude
887   for (mag = 0, mask = 0xff; mag < 4; mag++) {
888     if (ctx->pending_frame_magnitude < mask)
889       break;
890     mask <<= 8;
891     mask |= 0xff;
892   }
893   marker |= mag << 3;
894
895   // Write the index
896   index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
897   if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
898     uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
899     int i, j;
900 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
901     uint8_t marker_test = 0xc0;
902     int mag_test = 2;     // 1 - 4
903     int frames_test = 4;  // 1 - 8
904     int index_sz_test = 2 + mag_test * frames_test;
905     marker_test |= frames_test - 1;
906     marker_test |= (mag_test - 1) << 3;
907     *x++ = marker_test;
908     for (i = 0; i < mag_test * frames_test; ++i)
909       *x++ = 0;  // fill up with arbitrary data
910     *x++ = marker_test;
911     ctx->pending_cx_data_sz += index_sz_test;
912     printf("Added supplemental superframe data\n");
913 #endif
914
915     *x++ = marker;
916     for (i = 0; i < ctx->pending_frame_count; i++) {
917       unsigned int this_sz = (unsigned int)ctx->pending_frame_sizes[i];
918
919       for (j = 0; j <= mag; j++) {
920         *x++ = this_sz & 0xff;
921         this_sz >>= 8;
922       }
923     }
924     *x++ = marker;
925     ctx->pending_cx_data_sz += index_sz;
926 #ifdef TEST_SUPPLEMENTAL_SUPERFRAME_DATA
927     index_sz += index_sz_test;
928 #endif
929   }
930   return index_sz;
931 }
932
933 // vp9 uses 10,000,000 ticks/second as time stamp
934 #define TICKS_PER_SEC 10000000LL
935
936 static int64_t timebase_units_to_ticks(const vpx_rational_t *timebase,
937                                        int64_t n) {
938   return n * TICKS_PER_SEC * timebase->num / timebase->den;
939 }
940
941 static int64_t ticks_to_timebase_units(const vpx_rational_t *timebase,
942                                        int64_t n) {
943   const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
944   return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
945 }
946
947 static vpx_codec_frame_flags_t get_frame_pkt_flags(const VP9_COMP *cpi,
948                                                    unsigned int lib_flags) {
949   vpx_codec_frame_flags_t flags = lib_flags << 16;
950
951   if (lib_flags & FRAMEFLAGS_KEY ||
952       (cpi->use_svc &&
953           cpi->svc.layer_context[cpi->svc.spatial_layer_id *
954               cpi->svc.number_temporal_layers +
955               cpi->svc.temporal_layer_id].is_key_frame)
956      )
957     flags |= VPX_FRAME_IS_KEY;
958
959   if (cpi->droppable)
960     flags |= VPX_FRAME_IS_DROPPABLE;
961
962   return flags;
963 }
964
965 static vpx_codec_err_t encoder_encode(vpx_codec_alg_priv_t  *ctx,
966                                       const vpx_image_t *img,
967                                       vpx_codec_pts_t pts,
968                                       unsigned long duration,
969                                       vpx_enc_frame_flags_t flags,
970                                       unsigned long deadline) {
971   vpx_codec_err_t res = VPX_CODEC_OK;
972   VP9_COMP *const cpi = ctx->cpi;
973   const vpx_rational_t *const timebase = &ctx->cfg.g_timebase;
974   size_t data_sz;
975
976   if (img != NULL) {
977     res = validate_img(ctx, img);
978     // TODO(jzern) the checks related to cpi's validity should be treated as a
979     // failure condition, encoder setup is done fully in init() currently.
980     if (res == VPX_CODEC_OK && cpi != NULL) {
981       // There's no codec control for multiple alt-refs so check the encoder
982       // instance for its status to determine the compressed data size.
983       data_sz = ctx->cfg.g_w * ctx->cfg.g_h * get_image_bps(img) / 8 *
984                 (cpi->multi_arf_allowed ? 8 : 2);
985       if (data_sz < 4096)
986         data_sz = 4096;
987       if (ctx->cx_data == NULL || ctx->cx_data_sz < data_sz) {
988         ctx->cx_data_sz = data_sz;
989         free(ctx->cx_data);
990         ctx->cx_data = (unsigned char*)malloc(ctx->cx_data_sz);
991         if (ctx->cx_data == NULL) {
992           return VPX_CODEC_MEM_ERROR;
993         }
994       }
995     }
996   }
997
998   pick_quickcompress_mode(ctx, duration, deadline);
999   vpx_codec_pkt_list_init(&ctx->pkt_list);
1000
1001   // Handle Flags
1002   if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
1003        ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
1004     ctx->base.err_detail = "Conflicting flags.";
1005     return VPX_CODEC_INVALID_PARAM;
1006   }
1007
1008   vp9_apply_encoding_flags(cpi, flags);
1009
1010   // Handle fixed keyframe intervals
1011   if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
1012       ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
1013     if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
1014       flags |= VPX_EFLAG_FORCE_KF;
1015       ctx->fixed_kf_cntr = 1;
1016     }
1017   }
1018
1019   // Initialize the encoder instance on the first frame.
1020   if (res == VPX_CODEC_OK && cpi != NULL) {
1021     unsigned int lib_flags = 0;
1022     YV12_BUFFER_CONFIG sd;
1023     int64_t dst_time_stamp = timebase_units_to_ticks(timebase, pts);
1024     int64_t dst_end_time_stamp =
1025         timebase_units_to_ticks(timebase, pts + duration);
1026     size_t size, cx_data_sz;
1027     unsigned char *cx_data;
1028
1029     // Set up internal flags
1030     if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
1031       cpi->b_calculate_psnr = 1;
1032
1033     if (img != NULL) {
1034       res = image2yuvconfig(img, &sd);
1035
1036       // Store the original flags in to the frame buffer. Will extract the
1037       // key frame flag when we actually encode this frame.
1038       if (vp9_receive_raw_frame(cpi, flags | ctx->next_frame_flags,
1039                                 &sd, dst_time_stamp, dst_end_time_stamp)) {
1040         res = update_error_state(ctx, &cpi->common.error);
1041       }
1042       ctx->next_frame_flags = 0;
1043     }
1044
1045     cx_data = ctx->cx_data;
1046     cx_data_sz = ctx->cx_data_sz;
1047
1048     /* Any pending invisible frames? */
1049     if (ctx->pending_cx_data) {
1050       memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
1051       ctx->pending_cx_data = cx_data;
1052       cx_data += ctx->pending_cx_data_sz;
1053       cx_data_sz -= ctx->pending_cx_data_sz;
1054
1055       /* TODO: this is a minimal check, the underlying codec doesn't respect
1056        * the buffer size anyway.
1057        */
1058       if (cx_data_sz < ctx->cx_data_sz / 2) {
1059         ctx->base.err_detail = "Compressed data buffer too small";
1060         return VPX_CODEC_ERROR;
1061       }
1062     }
1063
1064     while (cx_data_sz >= ctx->cx_data_sz / 2 &&
1065            -1 != vp9_get_compressed_data(cpi, &lib_flags, &size,
1066                                          cx_data, &dst_time_stamp,
1067                                          &dst_end_time_stamp, !img)) {
1068       if (size) {
1069         vpx_codec_cx_pkt_t pkt;
1070
1071 #if CONFIG_SPATIAL_SVC
1072         if (cpi->use_svc)
1073           cpi->svc.layer_context[cpi->svc.spatial_layer_id *
1074               cpi->svc.number_temporal_layers].layer_size += size;
1075 #endif
1076
1077         // Pack invisible frames with the next visible frame
1078         if (!cpi->common.show_frame ||
1079             (cpi->use_svc &&
1080              cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1)
1081             ) {
1082           if (ctx->pending_cx_data == 0)
1083             ctx->pending_cx_data = cx_data;
1084           ctx->pending_cx_data_sz += size;
1085           ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1086           ctx->pending_frame_magnitude |= size;
1087           cx_data += size;
1088           cx_data_sz -= size;
1089
1090           if (ctx->output_cx_pkt_cb.output_cx_pkt) {
1091             pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1092             pkt.data.frame.pts = ticks_to_timebase_units(timebase,
1093                                                          dst_time_stamp);
1094             pkt.data.frame.duration =
1095                (unsigned long)ticks_to_timebase_units(timebase,
1096                    dst_end_time_stamp - dst_time_stamp);
1097             pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1098             pkt.data.frame.buf = ctx->pending_cx_data;
1099             pkt.data.frame.sz  = size;
1100             ctx->pending_cx_data = NULL;
1101             ctx->pending_cx_data_sz = 0;
1102             ctx->pending_frame_count = 0;
1103             ctx->pending_frame_magnitude = 0;
1104             ctx->output_cx_pkt_cb.output_cx_pkt(
1105                 &pkt, ctx->output_cx_pkt_cb.user_priv);
1106           }
1107           continue;
1108         }
1109
1110         // Add the frame packet to the list of returned packets.
1111         pkt.kind = VPX_CODEC_CX_FRAME_PKT;
1112         pkt.data.frame.pts = ticks_to_timebase_units(timebase, dst_time_stamp);
1113         pkt.data.frame.duration =
1114            (unsigned long)ticks_to_timebase_units(timebase,
1115                dst_end_time_stamp - dst_time_stamp);
1116         pkt.data.frame.flags = get_frame_pkt_flags(cpi, lib_flags);
1117
1118         if (ctx->pending_cx_data) {
1119           ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
1120           ctx->pending_frame_magnitude |= size;
1121           ctx->pending_cx_data_sz += size;
1122           // write the superframe only for the case when
1123           if (!ctx->output_cx_pkt_cb.output_cx_pkt)
1124             size += write_superframe_index(ctx);
1125           pkt.data.frame.buf = ctx->pending_cx_data;
1126           pkt.data.frame.sz  = ctx->pending_cx_data_sz;
1127           ctx->pending_cx_data = NULL;
1128           ctx->pending_cx_data_sz = 0;
1129           ctx->pending_frame_count = 0;
1130           ctx->pending_frame_magnitude = 0;
1131         } else {
1132           pkt.data.frame.buf = cx_data;
1133           pkt.data.frame.sz  = size;
1134         }
1135         pkt.data.frame.partition_id = -1;
1136
1137         if(ctx->output_cx_pkt_cb.output_cx_pkt)
1138           ctx->output_cx_pkt_cb.output_cx_pkt(&pkt,
1139                                               ctx->output_cx_pkt_cb.user_priv);
1140         else
1141           vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
1142
1143         cx_data += size;
1144         cx_data_sz -= size;
1145 #if VPX_ENCODER_ABI_VERSION > (5 + VPX_CODEC_ABI_VERSION)
1146 #if CONFIG_SPATIAL_SVC
1147         if (cpi->use_svc && !ctx->output_cx_pkt_cb.output_cx_pkt) {
1148           vpx_codec_cx_pkt_t pkt_sizes, pkt_psnr;
1149           int sl;
1150           vp9_zero(pkt_sizes);
1151           vp9_zero(pkt_psnr);
1152           pkt_sizes.kind = VPX_CODEC_SPATIAL_SVC_LAYER_SIZES;
1153           pkt_psnr.kind = VPX_CODEC_SPATIAL_SVC_LAYER_PSNR;
1154           for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1155             LAYER_CONTEXT *lc =
1156                 &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers];
1157             pkt_sizes.data.layer_sizes[sl] = lc->layer_size;
1158             pkt_psnr.data.layer_psnr[sl] = lc->psnr_pkt;
1159             lc->layer_size = 0;
1160           }
1161
1162           vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt_sizes);
1163
1164           vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt_psnr);
1165         }
1166 #endif
1167 #endif
1168         if (is_one_pass_cbr_svc(cpi) &&
1169             (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) {
1170           // Encoded all spatial layers; exit loop.
1171           break;
1172         }
1173       }
1174     }
1175   }
1176
1177   return res;
1178 }
1179
1180 static const vpx_codec_cx_pkt_t *encoder_get_cxdata(vpx_codec_alg_priv_t *ctx,
1181                                                     vpx_codec_iter_t *iter) {
1182   return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
1183 }
1184
1185 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
1186                                           va_list args) {
1187   vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1188
1189   if (frame != NULL) {
1190     YV12_BUFFER_CONFIG sd;
1191
1192     image2yuvconfig(&frame->img, &sd);
1193     vp9_set_reference_enc(ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type),
1194                           &sd);
1195     return VPX_CODEC_OK;
1196   } else {
1197     return VPX_CODEC_INVALID_PARAM;
1198   }
1199 }
1200
1201 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
1202                                            va_list args) {
1203   vpx_ref_frame_t *const frame = va_arg(args, vpx_ref_frame_t *);
1204
1205   if (frame != NULL) {
1206     YV12_BUFFER_CONFIG sd;
1207
1208     image2yuvconfig(&frame->img, &sd);
1209     vp9_copy_reference_enc(ctx->cpi,
1210                            ref_frame_to_vp9_reframe(frame->frame_type), &sd);
1211     return VPX_CODEC_OK;
1212   } else {
1213     return VPX_CODEC_INVALID_PARAM;
1214   }
1215 }
1216
1217 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
1218                                           va_list args) {
1219   vp9_ref_frame_t *const frame = va_arg(args, vp9_ref_frame_t *);
1220
1221   if (frame != NULL) {
1222     YV12_BUFFER_CONFIG *fb = get_ref_frame(&ctx->cpi->common, frame->idx);
1223     if (fb == NULL) return VPX_CODEC_ERROR;
1224
1225     yuvconfig2image(&frame->img, fb, NULL);
1226     return VPX_CODEC_OK;
1227   } else {
1228     return VPX_CODEC_INVALID_PARAM;
1229   }
1230 }
1231
1232 static vpx_codec_err_t ctrl_set_previewpp(vpx_codec_alg_priv_t *ctx,
1233                                           va_list args) {
1234 #if CONFIG_VP9_POSTPROC
1235   vp8_postproc_cfg_t *config = va_arg(args, vp8_postproc_cfg_t *);
1236   if (config != NULL) {
1237     ctx->preview_ppcfg = *config;
1238     return VPX_CODEC_OK;
1239   } else {
1240     return VPX_CODEC_INVALID_PARAM;
1241   }
1242 #else
1243   (void)ctx;
1244   (void)args;
1245   return VPX_CODEC_INCAPABLE;
1246 #endif
1247 }
1248
1249
1250 static vpx_image_t *encoder_get_preview(vpx_codec_alg_priv_t *ctx) {
1251   YV12_BUFFER_CONFIG sd;
1252   vp9_ppflags_t flags;
1253   vp9_zero(flags);
1254
1255   if (ctx->preview_ppcfg.post_proc_flag) {
1256     flags.post_proc_flag   = ctx->preview_ppcfg.post_proc_flag;
1257     flags.deblocking_level = ctx->preview_ppcfg.deblocking_level;
1258     flags.noise_level      = ctx->preview_ppcfg.noise_level;
1259   }
1260
1261   if (vp9_get_preview_raw_frame(ctx->cpi, &sd, &flags) == 0) {
1262     yuvconfig2image(&ctx->preview_img, &sd, NULL);
1263     return &ctx->preview_img;
1264   } else {
1265     return NULL;
1266   }
1267 }
1268
1269 static vpx_codec_err_t ctrl_update_entropy(vpx_codec_alg_priv_t *ctx,
1270                                            va_list args) {
1271   const int update = va_arg(args, int);
1272
1273   vp9_update_entropy(ctx->cpi, update);
1274   return VPX_CODEC_OK;
1275 }
1276
1277 static vpx_codec_err_t ctrl_update_reference(vpx_codec_alg_priv_t *ctx,
1278                                              va_list args) {
1279   const int ref_frame_flags = va_arg(args, int);
1280
1281   vp9_update_reference(ctx->cpi, ref_frame_flags);
1282   return VPX_CODEC_OK;
1283 }
1284
1285 static vpx_codec_err_t ctrl_use_reference(vpx_codec_alg_priv_t *ctx,
1286                                           va_list args) {
1287   const int reference_flag = va_arg(args, int);
1288
1289   vp9_use_as_reference(ctx->cpi, reference_flag);
1290   return VPX_CODEC_OK;
1291 }
1292
1293 static vpx_codec_err_t ctrl_set_roi_map(vpx_codec_alg_priv_t *ctx,
1294                                         va_list args) {
1295   (void)ctx;
1296   (void)args;
1297
1298   // TODO(yaowu): Need to re-implement and test for VP9.
1299   return VPX_CODEC_INVALID_PARAM;
1300 }
1301
1302
1303 static vpx_codec_err_t ctrl_set_active_map(vpx_codec_alg_priv_t *ctx,
1304                                            va_list args) {
1305   vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1306
1307   if (map) {
1308     if (!vp9_set_active_map(ctx->cpi, map->active_map,
1309                             (int)map->rows, (int)map->cols))
1310       return VPX_CODEC_OK;
1311     else
1312       return VPX_CODEC_INVALID_PARAM;
1313   } else {
1314     return VPX_CODEC_INVALID_PARAM;
1315   }
1316 }
1317
1318 static vpx_codec_err_t ctrl_get_active_map(vpx_codec_alg_priv_t *ctx,
1319                                            va_list args) {
1320   vpx_active_map_t *const map = va_arg(args, vpx_active_map_t *);
1321
1322   if (map) {
1323     if (!vp9_get_active_map(ctx->cpi, map->active_map,
1324                             (int)map->rows, (int)map->cols))
1325       return VPX_CODEC_OK;
1326     else
1327       return VPX_CODEC_INVALID_PARAM;
1328   } else {
1329     return VPX_CODEC_INVALID_PARAM;
1330   }
1331 }
1332
1333 static vpx_codec_err_t ctrl_set_scale_mode(vpx_codec_alg_priv_t *ctx,
1334                                            va_list args) {
1335   vpx_scaling_mode_t *const mode = va_arg(args, vpx_scaling_mode_t *);
1336
1337   if (mode) {
1338     const int res = vp9_set_internal_size(ctx->cpi,
1339                                           (VPX_SCALING)mode->h_scaling_mode,
1340                                           (VPX_SCALING)mode->v_scaling_mode);
1341     return (res == 0) ? VPX_CODEC_OK : VPX_CODEC_INVALID_PARAM;
1342   } else {
1343     return VPX_CODEC_INVALID_PARAM;
1344   }
1345 }
1346
1347 static vpx_codec_err_t ctrl_set_svc(vpx_codec_alg_priv_t *ctx, va_list args) {
1348   int data = va_arg(args, int);
1349   const vpx_codec_enc_cfg_t *cfg = &ctx->cfg;
1350   // Both one-pass and two-pass RC are supported now.
1351   // User setting this has to make sure of the following.
1352   // In two-pass setting: either (but not both)
1353   //      cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
1354   // In one-pass setting:
1355   //      either or both cfg->ss_number_layers > 1, or cfg->ts_number_layers > 1
1356
1357   vp9_set_svc(ctx->cpi, data);
1358
1359   if (data == 1 &&
1360       (cfg->g_pass == VPX_RC_FIRST_PASS ||
1361        cfg->g_pass == VPX_RC_LAST_PASS) &&
1362        cfg->ss_number_layers > 1 &&
1363        cfg->ts_number_layers > 1) {
1364     return VPX_CODEC_INVALID_PARAM;
1365   }
1366   return VPX_CODEC_OK;
1367 }
1368
1369 static vpx_codec_err_t ctrl_set_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1370                                              va_list args) {
1371   vpx_svc_layer_id_t *const data = va_arg(args, vpx_svc_layer_id_t *);
1372   VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1373   SVC *const svc = &cpi->svc;
1374
1375   svc->first_spatial_layer_to_encode = data->spatial_layer_id;
1376   svc->spatial_layer_to_encode = data->spatial_layer_id;
1377   svc->temporal_layer_id = data->temporal_layer_id;
1378   // Checks on valid layer_id input.
1379   if (svc->temporal_layer_id < 0 ||
1380       svc->temporal_layer_id >= (int)ctx->cfg.ts_number_layers) {
1381     return VPX_CODEC_INVALID_PARAM;
1382   }
1383   if (svc->first_spatial_layer_to_encode < 0 ||
1384       svc->first_spatial_layer_to_encode >= (int)ctx->cfg.ss_number_layers) {
1385     return VPX_CODEC_INVALID_PARAM;
1386   }
1387   // First spatial layer to encode not implemented for two-pass.
1388   if (is_two_pass_svc(cpi) && svc->first_spatial_layer_to_encode > 0)
1389     return VPX_CODEC_INVALID_PARAM;
1390   return VPX_CODEC_OK;
1391 }
1392
1393 static vpx_codec_err_t ctrl_get_svc_layer_id(vpx_codec_alg_priv_t *ctx,
1394                                              va_list args) {
1395   vpx_svc_layer_id_t *data = va_arg(args, vpx_svc_layer_id_t *);
1396   VP9_COMP *const cpi = (VP9_COMP *)ctx->cpi;
1397   SVC *const svc = &cpi->svc;
1398
1399   data->spatial_layer_id = svc->spatial_layer_id;
1400   data->temporal_layer_id = svc->temporal_layer_id;
1401
1402   return VPX_CODEC_OK;
1403 }
1404
1405 static vpx_codec_err_t ctrl_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
1406                                                va_list args) {
1407   VP9_COMP *const cpi = ctx->cpi;
1408   vpx_svc_extra_cfg_t *const params = va_arg(args, vpx_svc_extra_cfg_t *);
1409   int sl, tl;
1410
1411   // Number of temporal layers and number of spatial layers have to be set
1412   // properly before calling this control function.
1413   for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1414     for (tl = 0; tl < cpi->svc.number_temporal_layers; ++tl) {
1415       const int layer =
1416           LAYER_IDS_TO_IDX(sl, tl, cpi->svc.number_temporal_layers);
1417       LAYER_CONTEXT *lc =
1418           &cpi->svc.layer_context[layer];
1419       lc->max_q = params->max_quantizers[sl];
1420       lc->min_q = params->min_quantizers[sl];
1421       lc->scaling_factor_num = params->scaling_factor_num[sl];
1422       lc->scaling_factor_den = params->scaling_factor_den[sl];
1423     }
1424   }
1425
1426   return VPX_CODEC_OK;
1427 }
1428
1429 static vpx_codec_err_t ctrl_set_svc_ref_frame_config(vpx_codec_alg_priv_t *ctx,
1430                                                      va_list args) {
1431   VP9_COMP *const cpi = ctx->cpi;
1432   vpx_svc_ref_frame_config_t *data = va_arg(args, vpx_svc_ref_frame_config_t *);
1433   int sl;
1434   for (sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
1435     cpi->svc.ext_frame_flags[sl] = data->frame_flags[sl];
1436     cpi->svc.ext_lst_fb_idx[sl] = data->lst_fb_idx[sl];
1437     cpi->svc.ext_gld_fb_idx[sl] = data->gld_fb_idx[sl];
1438     cpi->svc.ext_alt_fb_idx[sl] = data->alt_fb_idx[sl];
1439   }
1440   return VPX_CODEC_OK;
1441 }
1442
1443 static vpx_codec_err_t ctrl_register_cx_callback(vpx_codec_alg_priv_t *ctx,
1444                                                  va_list args) {
1445   vpx_codec_priv_output_cx_pkt_cb_pair_t *cbp =
1446       (vpx_codec_priv_output_cx_pkt_cb_pair_t *)va_arg(args, void *);
1447   ctx->output_cx_pkt_cb.output_cx_pkt = cbp->output_cx_pkt;
1448   ctx->output_cx_pkt_cb.user_priv = cbp->user_priv;
1449
1450   return VPX_CODEC_OK;
1451 }
1452
1453 static vpx_codec_err_t ctrl_set_tune_content(vpx_codec_alg_priv_t *ctx,
1454                                              va_list args) {
1455   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1456   extra_cfg.content = CAST(VP9E_SET_TUNE_CONTENT, args);
1457   return update_extra_cfg(ctx, &extra_cfg);
1458 }
1459
1460 static vpx_codec_err_t ctrl_set_color_space(vpx_codec_alg_priv_t *ctx,
1461                                             va_list args) {
1462   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1463   extra_cfg.color_space = CAST(VP9E_SET_COLOR_SPACE, args);
1464   return update_extra_cfg(ctx, &extra_cfg);
1465 }
1466
1467 static vpx_codec_err_t ctrl_set_color_range(vpx_codec_alg_priv_t *ctx,
1468                                             va_list args) {
1469   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1470   extra_cfg.color_range = CAST(VP9E_SET_COLOR_RANGE, args);
1471   return update_extra_cfg(ctx, &extra_cfg);
1472 }
1473
1474 static vpx_codec_err_t ctrl_set_render_size(vpx_codec_alg_priv_t *ctx,
1475                                             va_list args) {
1476   struct vp9_extracfg extra_cfg = ctx->extra_cfg;
1477   int *const render_size = va_arg(args, int *);
1478   extra_cfg.render_width  = render_size[0];
1479   extra_cfg.render_height = render_size[1];
1480   return update_extra_cfg(ctx, &extra_cfg);
1481 }
1482
1483 static vpx_codec_ctrl_fn_map_t encoder_ctrl_maps[] = {
1484   {VP8_COPY_REFERENCE,                ctrl_copy_reference},
1485   {VP8E_UPD_ENTROPY,                  ctrl_update_entropy},
1486   {VP8E_UPD_REFERENCE,                ctrl_update_reference},
1487   {VP8E_USE_REFERENCE,                ctrl_use_reference},
1488
1489   // Setters
1490   {VP8_SET_REFERENCE,                 ctrl_set_reference},
1491   {VP8_SET_POSTPROC,                  ctrl_set_previewpp},
1492   {VP8E_SET_ROI_MAP,                  ctrl_set_roi_map},
1493   {VP8E_SET_ACTIVEMAP,                ctrl_set_active_map},
1494   {VP8E_SET_SCALEMODE,                ctrl_set_scale_mode},
1495   {VP8E_SET_CPUUSED,                  ctrl_set_cpuused},
1496   {VP8E_SET_ENABLEAUTOALTREF,         ctrl_set_enable_auto_alt_ref},
1497   {VP8E_SET_SHARPNESS,                ctrl_set_sharpness},
1498   {VP8E_SET_STATIC_THRESHOLD,         ctrl_set_static_thresh},
1499   {VP9E_SET_TILE_COLUMNS,             ctrl_set_tile_columns},
1500   {VP9E_SET_TILE_ROWS,                ctrl_set_tile_rows},
1501   {VP8E_SET_ARNR_MAXFRAMES,           ctrl_set_arnr_max_frames},
1502   {VP8E_SET_ARNR_STRENGTH,            ctrl_set_arnr_strength},
1503   {VP8E_SET_ARNR_TYPE,                ctrl_set_arnr_type},
1504   {VP8E_SET_TUNING,                   ctrl_set_tuning},
1505   {VP8E_SET_CQ_LEVEL,                 ctrl_set_cq_level},
1506   {VP8E_SET_MAX_INTRA_BITRATE_PCT,    ctrl_set_rc_max_intra_bitrate_pct},
1507   {VP9E_SET_MAX_INTER_BITRATE_PCT,    ctrl_set_rc_max_inter_bitrate_pct},
1508   {VP9E_SET_GF_CBR_BOOST_PCT,         ctrl_set_rc_gf_cbr_boost_pct},
1509   {VP9E_SET_LOSSLESS,                 ctrl_set_lossless},
1510   {VP9E_SET_FRAME_PARALLEL_DECODING,  ctrl_set_frame_parallel_decoding_mode},
1511   {VP9E_SET_AQ_MODE,                  ctrl_set_aq_mode},
1512   {VP9E_SET_FRAME_PERIODIC_BOOST,     ctrl_set_frame_periodic_boost},
1513   {VP9E_SET_SVC,                      ctrl_set_svc},
1514   {VP9E_SET_SVC_PARAMETERS,           ctrl_set_svc_parameters},
1515   {VP9E_REGISTER_CX_CALLBACK,         ctrl_register_cx_callback},
1516   {VP9E_SET_SVC_LAYER_ID,             ctrl_set_svc_layer_id},
1517   {VP9E_SET_TUNE_CONTENT,             ctrl_set_tune_content},
1518   {VP9E_SET_COLOR_SPACE,              ctrl_set_color_space},
1519   {VP9E_SET_COLOR_RANGE,              ctrl_set_color_range},
1520   {VP9E_SET_NOISE_SENSITIVITY,        ctrl_set_noise_sensitivity},
1521   {VP9E_SET_MIN_GF_INTERVAL,          ctrl_set_min_gf_interval},
1522   {VP9E_SET_MAX_GF_INTERVAL,          ctrl_set_max_gf_interval},
1523   {VP9E_SET_SVC_REF_FRAME_CONFIG,     ctrl_set_svc_ref_frame_config},
1524   {VP9E_SET_RENDER_SIZE,              ctrl_set_render_size},
1525
1526   // Getters
1527   {VP8E_GET_LAST_QUANTIZER,           ctrl_get_quantizer},
1528   {VP8E_GET_LAST_QUANTIZER_64,        ctrl_get_quantizer64},
1529   {VP9_GET_REFERENCE,                 ctrl_get_reference},
1530   {VP9E_GET_SVC_LAYER_ID,             ctrl_get_svc_layer_id},
1531   {VP9E_GET_ACTIVEMAP,                ctrl_get_active_map},
1532
1533   { -1, NULL},
1534 };
1535
1536 static vpx_codec_enc_cfg_map_t encoder_usage_cfg_map[] = {
1537   {
1538     0,
1539     {  // NOLINT
1540       0,                  // g_usage
1541       8,                  // g_threads
1542       0,                  // g_profile
1543
1544       320,                // g_width
1545       240,                // g_height
1546       VPX_BITS_8,         // g_bit_depth
1547       8,                  // g_input_bit_depth
1548
1549       {1, 30},            // g_timebase
1550
1551       0,                  // g_error_resilient
1552
1553       VPX_RC_ONE_PASS,    // g_pass
1554
1555       25,                 // g_lag_in_frames
1556
1557       0,                  // rc_dropframe_thresh
1558       0,                  // rc_resize_allowed
1559       0,                  // rc_scaled_width
1560       0,                  // rc_scaled_height
1561       60,                 // rc_resize_down_thresold
1562       30,                 // rc_resize_up_thresold
1563
1564       VPX_VBR,            // rc_end_usage
1565       {NULL, 0},          // rc_twopass_stats_in
1566       {NULL, 0},          // rc_firstpass_mb_stats_in
1567       256,                // rc_target_bandwidth
1568       0,                  // rc_min_quantizer
1569       63,                 // rc_max_quantizer
1570       25,                 // rc_undershoot_pct
1571       25,                 // rc_overshoot_pct
1572
1573       6000,               // rc_max_buffer_size
1574       4000,               // rc_buffer_initial_size
1575       5000,               // rc_buffer_optimal_size
1576
1577       50,                 // rc_two_pass_vbrbias
1578       0,                  // rc_two_pass_vbrmin_section
1579       2000,               // rc_two_pass_vbrmax_section
1580
1581       // keyframing settings (kf)
1582       VPX_KF_AUTO,        // g_kfmode
1583       0,                  // kf_min_dist
1584       9999,               // kf_max_dist
1585
1586       VPX_SS_DEFAULT_LAYERS,  // ss_number_layers
1587       {0},
1588       {0},                    // ss_target_bitrate
1589       1,                      // ts_number_layers
1590       {0},                    // ts_target_bitrate
1591       {0},                    // ts_rate_decimator
1592       0,                      // ts_periodicity
1593       {0},                    // ts_layer_id
1594       {0},                  // layer_taget_bitrate
1595       0                     // temporal_layering_mode
1596     }
1597   },
1598 };
1599
1600 #ifndef VERSION_STRING
1601 #define VERSION_STRING
1602 #endif
1603 CODEC_INTERFACE(vpx_codec_vp9_cx) = {
1604   "WebM Project VP9 Encoder" VERSION_STRING,
1605   VPX_CODEC_INTERNAL_ABI_VERSION,
1606 #if CONFIG_VP9_HIGHBITDEPTH
1607   VPX_CODEC_CAP_HIGHBITDEPTH |
1608 #endif
1609   VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR,  // vpx_codec_caps_t
1610   encoder_init,       // vpx_codec_init_fn_t
1611   encoder_destroy,    // vpx_codec_destroy_fn_t
1612   encoder_ctrl_maps,  // vpx_codec_ctrl_fn_map_t
1613   {  // NOLINT
1614     NULL,  // vpx_codec_peek_si_fn_t
1615     NULL,  // vpx_codec_get_si_fn_t
1616     NULL,  // vpx_codec_decode_fn_t
1617     NULL,  // vpx_codec_frame_get_fn_t
1618     NULL   // vpx_codec_set_fb_fn_t
1619   },
1620   {  // NOLINT
1621     1,                      // 1 cfg map
1622     encoder_usage_cfg_map,  // vpx_codec_enc_cfg_map_t
1623     encoder_encode,         // vpx_codec_encode_fn_t
1624     encoder_get_cxdata,     // vpx_codec_get_cx_data_fn_t
1625     encoder_set_config,     // vpx_codec_enc_config_set_fn_t
1626     NULL,        // vpx_codec_get_global_headers_fn_t
1627     encoder_get_preview,    // vpx_codec_get_preview_frame_fn_t
1628     NULL         // vpx_codec_enc_mr_get_mem_loc_fn_t
1629   }
1630 };