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/vpx_codec.h"
15#include "vpx/internal/vpx_codec_internal.h"
16#include "./vpx_version.h"
17#include "vp9/encoder/vp9_onyx_int.h"
18#include "vpx/vp8cx.h"
19#include "vp9/encoder/vp9_firstpass.h"
20#include "vp9/common/vp9_onyx.h"
21#include "vp9/vp9_iface_common.h"
22
23struct vp9_extracfg {
24  struct vpx_codec_pkt_list *pkt_list;
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                arnr_type;
35  unsigned int                experimental;
36  vp8e_tuning                 tuning;
37  unsigned int                cq_level;         /* constrained quality level */
38  unsigned int                rc_max_intra_bitrate_pct;
39  unsigned int                lossless;
40  unsigned int                frame_parallel_decoding_mode;
41};
42
43struct extraconfig_map {
44  int                 usage;
45  struct vp9_extracfg cfg;
46};
47
48static const struct extraconfig_map extracfg_map[] = {
49  {
50    0,
51    { // NOLINT
52      NULL,
53      0,                          /* cpu_used      */
54      1,                          /* enable_auto_alt_ref */
55      0,                          /* noise_sensitivity */
56      0,                          /* Sharpness */
57      0,                          /* static_thresh */
58      0,                          /* tile_columns */
59      0,                          /* tile_rows */
60      7,                          /* arnr_max_frames */
61      5,                          /* arnr_strength */
62      3,                          /* arnr_type*/
63      0,                          /* experimental mode */
64      0,                          /* tuning*/
65      10,                         /* cq_level */
66      0,                          /* rc_max_intra_bitrate_pct */
67      0,                          /* lossless */
68      0,                          /* frame_parallel_decoding_mode */
69    }
70  }
71};
72
73struct vpx_codec_alg_priv {
74  vpx_codec_priv_t        base;
75  vpx_codec_enc_cfg_t     cfg;
76  struct vp9_extracfg     vp8_cfg;
77  VP9_CONFIG              oxcf;
78  VP9_PTR             cpi;
79  unsigned char          *cx_data;
80  unsigned int            cx_data_sz;
81  unsigned char          *pending_cx_data;
82  unsigned int            pending_cx_data_sz;
83  int                     pending_frame_count;
84  uint32_t                pending_frame_sizes[8];
85  uint32_t                pending_frame_magnitude;
86  vpx_image_t             preview_img;
87  vp8_postproc_cfg_t      preview_ppcfg;
88  vpx_codec_pkt_list_decl(64) pkt_list;
89  unsigned int                fixed_kf_cntr;
90};
91
92static VP9_REFFRAME ref_frame_to_vp9_reframe(vpx_ref_frame_type_t frame) {
93  switch (frame) {
94    case VP8_LAST_FRAME:
95      return VP9_LAST_FLAG;
96    case VP8_GOLD_FRAME:
97      return VP9_GOLD_FLAG;
98    case VP8_ALTR_FRAME:
99      return VP9_ALT_FLAG;
100  }
101  assert(!"Invalid Reference Frame");
102  return VP9_LAST_FLAG;
103}
104
105static vpx_codec_err_t
106update_error_state(vpx_codec_alg_priv_t                 *ctx,
107                   const struct vpx_internal_error_info *error) {
108  vpx_codec_err_t res;
109
110  if ((res = error->error_code))
111    ctx->base.err_detail = error->has_detail
112                           ? error->detail
113                           : NULL;
114
115  return res;
116}
117
118
119#undef ERROR
120#define ERROR(str) do {\
121    ctx->base.err_detail = str;\
122    return VPX_CODEC_INVALID_PARAM;\
123  } while (0)
124
125#define RANGE_CHECK(p, memb, lo, hi) do {\
126    if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
127      ERROR(#memb " out of range ["#lo".."#hi"]");\
128  } while (0)
129
130#define RANGE_CHECK_HI(p, memb, hi) do {\
131    if (!((p)->memb <= (hi))) \
132      ERROR(#memb " out of range [.."#hi"]");\
133  } while (0)
134
135#define RANGE_CHECK_LO(p, memb, lo) do {\
136    if (!((p)->memb >= (lo))) \
137      ERROR(#memb " out of range ["#lo"..]");\
138  } while (0)
139
140#define RANGE_CHECK_BOOL(p, memb) do {\
141    if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
142  } while (0)
143
144static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t      *ctx,
145                                       const vpx_codec_enc_cfg_t *cfg,
146                                       const struct vp9_extracfg *vp8_cfg) {
147  RANGE_CHECK(cfg, g_w,                   1, 65535); /* 16 bits available */
148  RANGE_CHECK(cfg, g_h,                   1, 65535); /* 16 bits available */
149  RANGE_CHECK(cfg, g_timebase.den,        1, 1000000000);
150  RANGE_CHECK(cfg, g_timebase.num,        1, cfg->g_timebase.den);
151  RANGE_CHECK_HI(cfg, g_profile,          3);
152
153  RANGE_CHECK_HI(cfg, rc_max_quantizer,   63);
154  RANGE_CHECK_HI(cfg, rc_min_quantizer,   cfg->rc_max_quantizer);
155  RANGE_CHECK_BOOL(vp8_cfg, lossless);
156  if (vp8_cfg->lossless) {
157    RANGE_CHECK_HI(cfg, rc_max_quantizer, 0);
158    RANGE_CHECK_HI(cfg, rc_min_quantizer, 0);
159  }
160
161  RANGE_CHECK_HI(cfg, g_threads,          64);
162  RANGE_CHECK_HI(cfg, g_lag_in_frames,    MAX_LAG_BUFFERS);
163  RANGE_CHECK(cfg, rc_end_usage,          VPX_VBR, VPX_Q);
164  RANGE_CHECK_HI(cfg, rc_undershoot_pct,  1000);
165  RANGE_CHECK_HI(cfg, rc_overshoot_pct,   1000);
166  RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
167  RANGE_CHECK(cfg, kf_mode,               VPX_KF_DISABLED, VPX_KF_AUTO);
168  // RANGE_CHECK_BOOL(cfg,                 g_delete_firstpassfile);
169  RANGE_CHECK_BOOL(cfg,                   rc_resize_allowed);
170  RANGE_CHECK_HI(cfg, rc_dropframe_thresh,   100);
171  RANGE_CHECK_HI(cfg, rc_resize_up_thresh,   100);
172  RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
173  RANGE_CHECK(cfg,        g_pass,         VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);
174
175  RANGE_CHECK(cfg, ss_number_layers,      1,
176              VPX_SS_MAX_LAYERS); /*Spatial layers max */
177  /* VP8 does not support a lower bound on the keyframe interval in
178   * automatic keyframe placement mode.
179   */
180  if (cfg->kf_mode != VPX_KF_DISABLED && cfg->kf_min_dist != cfg->kf_max_dist
181      && cfg->kf_min_dist > 0)
182    ERROR("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  RANGE_CHECK_HI(vp8_cfg, noise_sensitivity,  6);
189
190  RANGE_CHECK(vp8_cfg, tile_columns, 0, 6);
191  RANGE_CHECK(vp8_cfg, tile_rows, 0, 2);
192  RANGE_CHECK_HI(vp8_cfg, Sharpness,       7);
193  RANGE_CHECK(vp8_cfg, arnr_max_frames, 0, 15);
194  RANGE_CHECK_HI(vp8_cfg, arnr_strength,   6);
195  RANGE_CHECK(vp8_cfg, arnr_type,       1, 3);
196  RANGE_CHECK(vp8_cfg, cq_level, 0, 63);
197
198  if (cfg->g_pass == VPX_RC_LAST_PASS) {
199    size_t           packet_sz = sizeof(FIRSTPASS_STATS);
200    int              n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
201    FIRSTPASS_STATS *stats;
202
203    if (!cfg->rc_twopass_stats_in.buf)
204      ERROR("rc_twopass_stats_in.buf not set.");
205
206    if (cfg->rc_twopass_stats_in.sz % packet_sz)
207      ERROR("rc_twopass_stats_in.sz indicates truncated packet.");
208
209    if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
210      ERROR("rc_twopass_stats_in requires at least two packets.");
211
212    stats = (void *)((char *)cfg->rc_twopass_stats_in.buf
213                     + (n_packets - 1) * packet_sz);
214
215    if ((int)(stats->count + 0.5) != n_packets - 1)
216      ERROR("rc_twopass_stats_in missing EOS stats packet");
217  }
218
219  return VPX_CODEC_OK;
220}
221
222
223static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
224                                    const vpx_image_t    *img) {
225  switch (img->fmt) {
226    case VPX_IMG_FMT_YV12:
227    case VPX_IMG_FMT_I420:
228    case VPX_IMG_FMT_I422:
229    case VPX_IMG_FMT_I444:
230      break;
231    default:
232      ERROR("Invalid image format. Only YV12, I420, I422, I444 images are "
233            "supported.");
234  }
235
236  if ((img->d_w != ctx->cfg.g_w) || (img->d_h != ctx->cfg.g_h))
237    ERROR("Image size must match encoder init configuration size");
238
239  return VPX_CODEC_OK;
240}
241
242
243static vpx_codec_err_t set_vp9e_config(VP9_CONFIG *oxcf,
244                                       vpx_codec_enc_cfg_t cfg,
245                                       struct vp9_extracfg vp8_cfg) {
246  oxcf->version = cfg.g_profile | (vp8_cfg.experimental ? 0x4 : 0);
247  oxcf->width   = cfg.g_w;
248  oxcf->height  = cfg.g_h;
249  /* guess a frame rate if out of whack, use 30 */
250  oxcf->framerate = (double)(cfg.g_timebase.den)
251                    / (double)(cfg.g_timebase.num);
252
253  if (oxcf->framerate > 180) {
254    oxcf->framerate = 30;
255  }
256
257  switch (cfg.g_pass) {
258    case VPX_RC_ONE_PASS:
259      oxcf->Mode = MODE_GOODQUALITY;
260      break;
261    case VPX_RC_FIRST_PASS:
262      oxcf->Mode = MODE_FIRSTPASS;
263      break;
264    case VPX_RC_LAST_PASS:
265      oxcf->Mode = MODE_SECONDPASS_BEST;
266      break;
267  }
268
269  if (cfg.g_pass == VPX_RC_FIRST_PASS) {
270    oxcf->allow_lag     = 0;
271    oxcf->lag_in_frames = 0;
272  } else {
273    oxcf->allow_lag     = (cfg.g_lag_in_frames) > 0;
274    oxcf->lag_in_frames = cfg.g_lag_in_frames;
275  }
276
277  // VBR only supported for now.
278  // CBR code has been deprectated for experimental phase.
279  // CQ mode not yet tested
280  oxcf->end_usage        = USAGE_LOCAL_FILE_PLAYBACK;
281  if (cfg.rc_end_usage == VPX_CQ)
282    oxcf->end_usage      = USAGE_CONSTRAINED_QUALITY;
283  else if (cfg.rc_end_usage == VPX_Q)
284    oxcf->end_usage      = USAGE_CONSTANT_QUALITY;
285  else if (cfg.rc_end_usage == VPX_CBR)
286    oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
287
288  oxcf->target_bandwidth         = cfg.rc_target_bitrate;
289  oxcf->rc_max_intra_bitrate_pct = vp8_cfg.rc_max_intra_bitrate_pct;
290
291  oxcf->best_allowed_q          = cfg.rc_min_quantizer;
292  oxcf->worst_allowed_q         = cfg.rc_max_quantizer;
293  oxcf->cq_level                = vp8_cfg.cq_level;
294  oxcf->fixed_q = -1;
295
296  oxcf->under_shoot_pct         = cfg.rc_undershoot_pct;
297  oxcf->over_shoot_pct          = cfg.rc_overshoot_pct;
298
299  oxcf->maximum_buffer_size     = cfg.rc_buf_sz;
300  oxcf->starting_buffer_level   = cfg.rc_buf_initial_sz;
301  oxcf->optimal_buffer_level    = cfg.rc_buf_optimal_sz;
302
303  oxcf->two_pass_vbrbias         = cfg.rc_2pass_vbr_bias_pct;
304  oxcf->two_pass_vbrmin_section  = cfg.rc_2pass_vbr_minsection_pct;
305  oxcf->two_pass_vbrmax_section  = cfg.rc_2pass_vbr_maxsection_pct;
306
307  oxcf->auto_key               = cfg.kf_mode == VPX_KF_AUTO
308                                 && cfg.kf_min_dist != cfg.kf_max_dist;
309  // oxcf->kf_min_dist         = cfg.kf_min_dis;
310  oxcf->key_freq               = cfg.kf_max_dist;
311
312  // oxcf->delete_first_pass_file = cfg.g_delete_firstpassfile;
313  // strcpy(oxcf->first_pass_file, cfg.g_firstpass_file);
314
315  oxcf->cpu_used               =  vp8_cfg.cpu_used;
316  oxcf->encode_breakout        =  vp8_cfg.static_thresh;
317  oxcf->play_alternate         =  vp8_cfg.enable_auto_alt_ref;
318  oxcf->noise_sensitivity      =  vp8_cfg.noise_sensitivity;
319  oxcf->Sharpness              =  vp8_cfg.Sharpness;
320
321  oxcf->two_pass_stats_in      =  cfg.rc_twopass_stats_in;
322  oxcf->output_pkt_list        =  vp8_cfg.pkt_list;
323
324  oxcf->arnr_max_frames = vp8_cfg.arnr_max_frames;
325  oxcf->arnr_strength   = vp8_cfg.arnr_strength;
326  oxcf->arnr_type       = vp8_cfg.arnr_type;
327
328  oxcf->tuning = vp8_cfg.tuning;
329
330  oxcf->tile_columns = vp8_cfg.tile_columns;
331  oxcf->tile_rows    = vp8_cfg.tile_rows;
332
333  oxcf->lossless = vp8_cfg.lossless;
334
335  oxcf->error_resilient_mode         = cfg.g_error_resilient;
336  oxcf->frame_parallel_decoding_mode = vp8_cfg.frame_parallel_decoding_mode;
337
338  oxcf->ss_number_layers = cfg.ss_number_layers;
339  /*
340  printf("Current VP9 Settings: \n");
341  printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
342  printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
343  printf("Sharpness: %d\n",    oxcf->Sharpness);
344  printf("cpu_used: %d\n",  oxcf->cpu_used);
345  printf("Mode: %d\n",     oxcf->Mode);
346  // printf("delete_first_pass_file: %d\n",  oxcf->delete_first_pass_file);
347  printf("auto_key: %d\n",  oxcf->auto_key);
348  printf("key_freq: %d\n", oxcf->key_freq);
349  printf("end_usage: %d\n", oxcf->end_usage);
350  printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
351  printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
352  printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
353  printf("optimal_buffer_level: %d\n",  oxcf->optimal_buffer_level);
354  printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
355  printf("fixed_q: %d\n",  oxcf->fixed_q);
356  printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
357  printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
358  printf("two_pass_vbrbias: %d\n",  oxcf->two_pass_vbrbias);
359  printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
360  printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
361  printf("allow_lag: %d\n", oxcf->allow_lag);
362  printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
363  printf("play_alternate: %d\n", oxcf->play_alternate);
364  printf("Version: %d\n", oxcf->Version);
365  printf("encode_breakout: %d\n", oxcf->encode_breakout);
366  printf("error resilient: %d\n", oxcf->error_resilient_mode);
367  printf("frame parallel detokenization: %d\n",
368         oxcf->frame_parallel_decoding_mode);
369  */
370  return VPX_CODEC_OK;
371}
372
373static vpx_codec_err_t vp9e_set_config(vpx_codec_alg_priv_t       *ctx,
374                                       const vpx_codec_enc_cfg_t  *cfg) {
375  vpx_codec_err_t res;
376
377  if ((cfg->g_w != ctx->cfg.g_w) || (cfg->g_h != ctx->cfg.g_h))
378    ERROR("Cannot change width or height after initialization");
379
380  /* Prevent increasing lag_in_frames. This check is stricter than it needs
381   * to be -- the limit is not increasing past the first lag_in_frames
382   * value, but we don't track the initial config, only the last successful
383   * config.
384   */
385  if ((cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames))
386    ERROR("Cannot increase lag_in_frames");
387
388  res = validate_config(ctx, cfg, &ctx->vp8_cfg);
389
390  if (!res) {
391    ctx->cfg = *cfg;
392    set_vp9e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
393    vp9_change_config(ctx->cpi, &ctx->oxcf);
394  }
395
396  return res;
397}
398
399
400int vp9_reverse_trans(int q);
401
402
403static vpx_codec_err_t get_param(vpx_codec_alg_priv_t *ctx,
404                                 int                   ctrl_id,
405                                 va_list               args) {
406  void *arg = va_arg(args, void *);
407
408#define MAP(id, var) case id: *(RECAST(id, arg)) = var; break
409
410  if (!arg)
411    return VPX_CODEC_INVALID_PARAM;
412
413  switch (ctrl_id) {
414      MAP(VP8E_GET_LAST_QUANTIZER, vp9_get_quantizer(ctx->cpi));
415      MAP(VP8E_GET_LAST_QUANTIZER_64,
416          vp9_reverse_trans(vp9_get_quantizer(ctx->cpi)));
417  }
418
419  return VPX_CODEC_OK;
420#undef MAP
421}
422
423
424static vpx_codec_err_t set_param(vpx_codec_alg_priv_t *ctx,
425                                 int                   ctrl_id,
426                                 va_list               args) {
427  vpx_codec_err_t     res  = VPX_CODEC_OK;
428  struct vp9_extracfg xcfg = ctx->vp8_cfg;
429
430#define MAP(id, var) case id: var = CAST(id, args); break;
431
432  switch (ctrl_id) {
433      MAP(VP8E_SET_CPUUSED,                 xcfg.cpu_used);
434      MAP(VP8E_SET_ENABLEAUTOALTREF,        xcfg.enable_auto_alt_ref);
435      MAP(VP8E_SET_NOISE_SENSITIVITY,       xcfg.noise_sensitivity);
436      MAP(VP8E_SET_SHARPNESS,               xcfg.Sharpness);
437      MAP(VP8E_SET_STATIC_THRESHOLD,        xcfg.static_thresh);
438      MAP(VP9E_SET_TILE_COLUMNS,            xcfg.tile_columns);
439      MAP(VP9E_SET_TILE_ROWS,               xcfg.tile_rows);
440      MAP(VP8E_SET_ARNR_MAXFRAMES,          xcfg.arnr_max_frames);
441      MAP(VP8E_SET_ARNR_STRENGTH,           xcfg.arnr_strength);
442      MAP(VP8E_SET_ARNR_TYPE,               xcfg.arnr_type);
443      MAP(VP8E_SET_TUNING,                  xcfg.tuning);
444      MAP(VP8E_SET_CQ_LEVEL,                xcfg.cq_level);
445      MAP(VP8E_SET_MAX_INTRA_BITRATE_PCT,   xcfg.rc_max_intra_bitrate_pct);
446      MAP(VP9E_SET_LOSSLESS,                xcfg.lossless);
447      MAP(VP9E_SET_FRAME_PARALLEL_DECODING, xcfg.frame_parallel_decoding_mode);
448  }
449
450  res = validate_config(ctx, &ctx->cfg, &xcfg);
451
452  if (!res) {
453    ctx->vp8_cfg = xcfg;
454    set_vp9e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
455    vp9_change_config(ctx->cpi, &ctx->oxcf);
456  }
457
458  return res;
459#undef MAP
460}
461
462
463static vpx_codec_err_t vp9e_common_init(vpx_codec_ctx_t *ctx,
464                                        int              experimental) {
465  vpx_codec_err_t            res = VPX_CODEC_OK;
466  struct vpx_codec_alg_priv *priv;
467  vpx_codec_enc_cfg_t       *cfg;
468  unsigned int               i;
469
470  VP9_PTR optr;
471
472  if (!ctx->priv) {
473    priv = calloc(1, sizeof(struct vpx_codec_alg_priv));
474
475    if (!priv) {
476      return VPX_CODEC_MEM_ERROR;
477    }
478
479    ctx->priv = &priv->base;
480    ctx->priv->sz = sizeof(*ctx->priv);
481    ctx->priv->iface = ctx->iface;
482    ctx->priv->alg_priv = priv;
483    ctx->priv->init_flags = ctx->init_flags;
484    ctx->priv->enc.total_encoders = 1;
485
486    if (ctx->config.enc) {
487      /* Update the reference to the config structure to an
488       * internal copy.
489       */
490      ctx->priv->alg_priv->cfg = *ctx->config.enc;
491      ctx->config.enc = &ctx->priv->alg_priv->cfg;
492    }
493
494    cfg =  &ctx->priv->alg_priv->cfg;
495
496    /* Select the extra vp6 configuration table based on the current
497     * usage value. If the current usage value isn't found, use the
498     * values for usage case 0.
499     */
500    for (i = 0;
501         extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage;
502         i++) {}
503
504    priv->vp8_cfg = extracfg_map[i].cfg;
505    priv->vp8_cfg.pkt_list = &priv->pkt_list.head;
506    priv->vp8_cfg.experimental = experimental;
507
508    // TODO(agrange) Check the limits set on this buffer, or the check that is
509    // applied in vp9e_encode.
510    priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 8;
511//    priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 2;
512
513    if (priv->cx_data_sz < 4096) priv->cx_data_sz = 4096;
514
515    priv->cx_data = malloc(priv->cx_data_sz);
516
517    if (!priv->cx_data) {
518      return VPX_CODEC_MEM_ERROR;
519    }
520
521    vp9_initialize_enc();
522
523    res = validate_config(priv, &priv->cfg, &priv->vp8_cfg);
524
525    if (!res) {
526      set_vp9e_config(&ctx->priv->alg_priv->oxcf,
527                      ctx->priv->alg_priv->cfg,
528                      ctx->priv->alg_priv->vp8_cfg);
529      optr = vp9_create_compressor(&ctx->priv->alg_priv->oxcf);
530
531      if (!optr)
532        res = VPX_CODEC_MEM_ERROR;
533      else
534        ctx->priv->alg_priv->cpi = optr;
535    }
536  }
537
538  return res;
539}
540
541
542static vpx_codec_err_t vp9e_init(vpx_codec_ctx_t *ctx,
543                                 vpx_codec_priv_enc_mr_cfg_t *data) {
544  return vp9e_common_init(ctx, 0);
545}
546
547
548#if CONFIG_EXPERIMENTAL
549static vpx_codec_err_t vp9e_exp_init(vpx_codec_ctx_t *ctx,
550                                     vpx_codec_priv_enc_mr_cfg_t *data) {
551  return vp9e_common_init(ctx, 1);
552}
553#endif
554
555
556static vpx_codec_err_t vp9e_destroy(vpx_codec_alg_priv_t *ctx) {
557  free(ctx->cx_data);
558  vp9_remove_compressor(&ctx->cpi);
559  free(ctx);
560  return VPX_CODEC_OK;
561}
562
563static void pick_quickcompress_mode(vpx_codec_alg_priv_t  *ctx,
564                                    unsigned long          duration,
565                                    unsigned long          deadline) {
566  unsigned int new_qc;
567
568  /* Use best quality mode if no deadline is given. */
569  if (deadline)
570    new_qc = MODE_GOODQUALITY;
571  else
572    new_qc = MODE_BESTQUALITY;
573
574  if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
575    new_qc = MODE_FIRSTPASS;
576  else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
577    new_qc = (new_qc == MODE_BESTQUALITY)
578             ? MODE_SECONDPASS_BEST
579             : MODE_SECONDPASS;
580
581  if (ctx->oxcf.Mode != new_qc) {
582    ctx->oxcf.Mode = new_qc;
583    vp9_change_config(ctx->cpi, &ctx->oxcf);
584  }
585}
586
587
588static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
589  uint8_t marker = 0xc0;
590  unsigned int mask;
591  int mag, index_sz;
592
593  assert(ctx->pending_frame_count);
594  assert(ctx->pending_frame_count <= 8);
595
596  /* Add the number of frames to the marker byte */
597  marker |= ctx->pending_frame_count - 1;
598
599  /* Choose the magnitude */
600  for (mag = 0, mask = 0xff; mag < 4; mag++) {
601    if (ctx->pending_frame_magnitude < mask)
602      break;
603    mask <<= 8;
604    mask |= 0xff;
605  }
606  marker |= mag << 3;
607
608  /* Write the index */
609  index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
610  if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
611    uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
612    int i, j;
613
614    *x++ = marker;
615    for (i = 0; i < ctx->pending_frame_count; i++) {
616      int this_sz = ctx->pending_frame_sizes[i];
617
618      for (j = 0; j <= mag; j++) {
619        *x++ = this_sz & 0xff;
620        this_sz >>= 8;
621      }
622    }
623    *x++ = marker;
624    ctx->pending_cx_data_sz += index_sz;
625  }
626  return index_sz;
627}
628
629static vpx_codec_err_t vp9e_encode(vpx_codec_alg_priv_t  *ctx,
630                                   const vpx_image_t     *img,
631                                   vpx_codec_pts_t        pts,
632                                   unsigned long          duration,
633                                   vpx_enc_frame_flags_t  flags,
634                                   unsigned long          deadline) {
635  vpx_codec_err_t res = VPX_CODEC_OK;
636
637  if (img)
638    res = validate_img(ctx, img);
639
640  pick_quickcompress_mode(ctx, duration, deadline);
641  vpx_codec_pkt_list_init(&ctx->pkt_list);
642
643  /* Handle Flags */
644  if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF))
645      || ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
646    ctx->base.err_detail = "Conflicting flags.";
647    return VPX_CODEC_INVALID_PARAM;
648  }
649
650  if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF
651               | VP8_EFLAG_NO_REF_ARF)) {
652    int ref = 7;
653
654    if (flags & VP8_EFLAG_NO_REF_LAST)
655      ref ^= VP9_LAST_FLAG;
656
657    if (flags & VP8_EFLAG_NO_REF_GF)
658      ref ^= VP9_GOLD_FLAG;
659
660    if (flags & VP8_EFLAG_NO_REF_ARF)
661      ref ^= VP9_ALT_FLAG;
662
663    vp9_use_as_reference(ctx->cpi, ref);
664  }
665
666  if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF
667               | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF
668               | VP8_EFLAG_FORCE_ARF)) {
669    int upd = 7;
670
671    if (flags & VP8_EFLAG_NO_UPD_LAST)
672      upd ^= VP9_LAST_FLAG;
673
674    if (flags & VP8_EFLAG_NO_UPD_GF)
675      upd ^= VP9_GOLD_FLAG;
676
677    if (flags & VP8_EFLAG_NO_UPD_ARF)
678      upd ^= VP9_ALT_FLAG;
679
680    vp9_update_reference(ctx->cpi, upd);
681  }
682
683  if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
684    vp9_update_entropy(ctx->cpi, 0);
685  }
686
687  /* Handle fixed keyframe intervals */
688  if (ctx->cfg.kf_mode == VPX_KF_AUTO
689      && ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
690    if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
691      flags |= VPX_EFLAG_FORCE_KF;
692      ctx->fixed_kf_cntr = 1;
693    }
694  }
695
696  /* Initialize the encoder instance on the first frame*/
697  if (!res && ctx->cpi) {
698    unsigned int lib_flags;
699    YV12_BUFFER_CONFIG sd;
700    int64_t dst_time_stamp, dst_end_time_stamp;
701    unsigned long size, cx_data_sz;
702    unsigned char *cx_data;
703
704    /* Set up internal flags */
705    if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
706      ((VP9_COMP *)ctx->cpi)->b_calculate_psnr = 1;
707
708    // if (ctx->base.init_flags & VPX_CODEC_USE_OUTPUT_PARTITION)
709    //    ((VP9_COMP *)ctx->cpi)->output_partition = 1;
710
711    /* Convert API flags to internal codec lib flags */
712    lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
713
714    /* vp8 use 10,000,000 ticks/second as time stamp */
715    dst_time_stamp = pts * 10000000 * ctx->cfg.g_timebase.num
716                     / ctx->cfg.g_timebase.den;
717    dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num /
718                         ctx->cfg.g_timebase.den;
719
720    if (img != NULL) {
721      res = image2yuvconfig(img, &sd);
722
723      if (vp9_receive_raw_frame(ctx->cpi, lib_flags,
724                                &sd, dst_time_stamp, dst_end_time_stamp)) {
725        VP9_COMP *cpi = (VP9_COMP *)ctx->cpi;
726        res = update_error_state(ctx, &cpi->common.error);
727      }
728    }
729
730    cx_data = ctx->cx_data;
731    cx_data_sz = ctx->cx_data_sz;
732    lib_flags = 0;
733
734    /* Any pending invisible frames? */
735    if (ctx->pending_cx_data) {
736      memmove(cx_data, ctx->pending_cx_data, ctx->pending_cx_data_sz);
737      ctx->pending_cx_data = cx_data;
738      cx_data += ctx->pending_cx_data_sz;
739      cx_data_sz -= ctx->pending_cx_data_sz;
740
741      /* TODO: this is a minimal check, the underlying codec doesn't respect
742       * the buffer size anyway.
743       */
744      if (cx_data_sz < ctx->cx_data_sz / 2) {
745        ctx->base.err_detail = "Compressed data buffer too small";
746        return VPX_CODEC_ERROR;
747      }
748    }
749
750    while (cx_data_sz >= ctx->cx_data_sz / 2 &&
751           -1 != vp9_get_compressed_data(ctx->cpi, &lib_flags, &size,
752                                         cx_data, &dst_time_stamp,
753                                         &dst_end_time_stamp, !img)) {
754      if (size) {
755        vpx_codec_pts_t    round, delta;
756        vpx_codec_cx_pkt_t pkt;
757        VP9_COMP *cpi = (VP9_COMP *)ctx->cpi;
758
759        /* Pack invisible frames with the next visible frame */
760        if (!cpi->common.show_frame) {
761          if (!ctx->pending_cx_data)
762            ctx->pending_cx_data = cx_data;
763          ctx->pending_cx_data_sz += size;
764          ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
765          ctx->pending_frame_magnitude |= size;
766          cx_data += size;
767          cx_data_sz -= size;
768          continue;
769        }
770
771        /* Add the frame packet to the list of returned packets. */
772        round = (vpx_codec_pts_t)1000000 * ctx->cfg.g_timebase.num / 2 - 1;
773        delta = (dst_end_time_stamp - dst_time_stamp);
774        pkt.kind = VPX_CODEC_CX_FRAME_PKT;
775        pkt.data.frame.pts =
776          (dst_time_stamp * ctx->cfg.g_timebase.den + round)
777          / ctx->cfg.g_timebase.num / 10000000;
778        pkt.data.frame.duration = (unsigned long)
779          ((delta * ctx->cfg.g_timebase.den + round)
780          / ctx->cfg.g_timebase.num / 10000000);
781        pkt.data.frame.flags = lib_flags << 16;
782
783        if (lib_flags & FRAMEFLAGS_KEY)
784          pkt.data.frame.flags |= VPX_FRAME_IS_KEY;
785
786        if (!cpi->common.show_frame) {
787          pkt.data.frame.flags |= VPX_FRAME_IS_INVISIBLE;
788
789          // This timestamp should be as close as possible to the
790          // prior PTS so that if a decoder uses pts to schedule when
791          // to do this, we start right after last frame was decoded.
792          // Invisible frames have no duration.
793          pkt.data.frame.pts = ((cpi->last_time_stamp_seen
794                                 * ctx->cfg.g_timebase.den + round)
795                                / ctx->cfg.g_timebase.num / 10000000) + 1;
796          pkt.data.frame.duration = 0;
797        }
798
799        if (cpi->droppable)
800          pkt.data.frame.flags |= VPX_FRAME_IS_DROPPABLE;
801
802        /*if (cpi->output_partition)
803        {
804            int i;
805            const int num_partitions = 1;
806
807            pkt.data.frame.flags |= VPX_FRAME_IS_FRAGMENT;
808
809            for (i = 0; i < num_partitions; ++i)
810            {
811                pkt.data.frame.buf = cx_data;
812                pkt.data.frame.sz = cpi->partition_sz[i];
813                pkt.data.frame.partition_id = i;
814                // don't set the fragment bit for the last partition
815                if (i == (num_partitions - 1))
816                    pkt.data.frame.flags &= ~VPX_FRAME_IS_FRAGMENT;
817                vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
818                cx_data += cpi->partition_sz[i];
819                cx_data_sz -= cpi->partition_sz[i];
820            }
821        }
822        else*/
823        {
824          if (ctx->pending_cx_data) {
825            ctx->pending_frame_sizes[ctx->pending_frame_count++] = size;
826            ctx->pending_frame_magnitude |= size;
827            ctx->pending_cx_data_sz += size;
828            size += write_superframe_index(ctx);
829            pkt.data.frame.buf = ctx->pending_cx_data;
830            pkt.data.frame.sz  = ctx->pending_cx_data_sz;
831            ctx->pending_cx_data = NULL;
832            ctx->pending_cx_data_sz = 0;
833            ctx->pending_frame_count = 0;
834            ctx->pending_frame_magnitude = 0;
835          } else {
836            pkt.data.frame.buf = cx_data;
837            pkt.data.frame.sz  = size;
838          }
839          pkt.data.frame.partition_id = -1;
840          vpx_codec_pkt_list_add(&ctx->pkt_list.head, &pkt);
841          cx_data += size;
842          cx_data_sz -= size;
843        }
844      }
845    }
846  }
847
848  return res;
849}
850
851
852static const vpx_codec_cx_pkt_t *vp9e_get_cxdata(vpx_codec_alg_priv_t  *ctx,
853                                                 vpx_codec_iter_t      *iter) {
854  return vpx_codec_pkt_list_get(&ctx->pkt_list.head, iter);
855}
856
857static vpx_codec_err_t vp9e_set_reference(vpx_codec_alg_priv_t *ctx,
858                                          int ctr_id,
859                                          va_list args) {
860  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
861
862  if (data) {
863    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
864    YV12_BUFFER_CONFIG sd;
865
866    image2yuvconfig(&frame->img, &sd);
867    vp9_set_reference_enc(ctx->cpi, ref_frame_to_vp9_reframe(frame->frame_type),
868                          &sd);
869    return VPX_CODEC_OK;
870  } else {
871    return VPX_CODEC_INVALID_PARAM;
872  }
873}
874
875static vpx_codec_err_t vp9e_copy_reference(vpx_codec_alg_priv_t *ctx,
876                                           int ctr_id,
877                                           va_list args) {
878  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
879
880  if (data) {
881    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
882    YV12_BUFFER_CONFIG sd;
883
884    image2yuvconfig(&frame->img, &sd);
885    vp9_copy_reference_enc(ctx->cpi,
886                           ref_frame_to_vp9_reframe(frame->frame_type), &sd);
887    return VPX_CODEC_OK;
888  } else {
889    return VPX_CODEC_INVALID_PARAM;
890  }
891}
892
893static vpx_codec_err_t get_reference(vpx_codec_alg_priv_t *ctx,
894                                     int ctr_id,
895                                     va_list args) {
896  vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
897
898  if (data) {
899    YV12_BUFFER_CONFIG* fb;
900
901    vp9_get_reference_enc(ctx->cpi, data->idx, &fb);
902    yuvconfig2image(&data->img, fb, NULL);
903    return VPX_CODEC_OK;
904  } else {
905    return VPX_CODEC_INVALID_PARAM;
906  }
907}
908
909static vpx_codec_err_t vp9e_set_previewpp(vpx_codec_alg_priv_t *ctx,
910                                          int ctr_id,
911                                          va_list args) {
912#if CONFIG_VP9_POSTPROC
913  vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
914  (void)ctr_id;
915
916  if (data) {
917    ctx->preview_ppcfg = *((vp8_postproc_cfg_t *)data);
918    return VPX_CODEC_OK;
919  } else {
920    return VPX_CODEC_INVALID_PARAM;
921  }
922#else
923  (void)ctx;
924  (void)ctr_id;
925  (void)args;
926  return VPX_CODEC_INCAPABLE;
927#endif
928}
929
930
931static vpx_image_t *vp9e_get_preview(vpx_codec_alg_priv_t *ctx) {
932  YV12_BUFFER_CONFIG sd;
933  vp9_ppflags_t flags = {0};
934
935  if (ctx->preview_ppcfg.post_proc_flag) {
936    flags.post_proc_flag        = ctx->preview_ppcfg.post_proc_flag;
937    flags.deblocking_level      = ctx->preview_ppcfg.deblocking_level;
938    flags.noise_level           = ctx->preview_ppcfg.noise_level;
939  }
940
941  if (0 == vp9_get_preview_raw_frame(ctx->cpi, &sd, &flags)) {
942    yuvconfig2image(&ctx->preview_img, &sd, NULL);
943    return &ctx->preview_img;
944  } else {
945    return NULL;
946  }
947}
948
949static vpx_codec_err_t vp9e_update_entropy(vpx_codec_alg_priv_t *ctx,
950                                           int ctr_id,
951                                           va_list args) {
952  int update = va_arg(args, int);
953  vp9_update_entropy(ctx->cpi, update);
954  return VPX_CODEC_OK;
955}
956
957static vpx_codec_err_t vp9e_update_reference(vpx_codec_alg_priv_t *ctx,
958                                             int ctr_id,
959                                             va_list args) {
960  int update = va_arg(args, int);
961  vp9_update_reference(ctx->cpi, update);
962  return VPX_CODEC_OK;
963}
964
965static vpx_codec_err_t vp9e_use_reference(vpx_codec_alg_priv_t *ctx,
966                                          int ctr_id,
967                                          va_list args) {
968  int reference_flag = va_arg(args, int);
969  vp9_use_as_reference(ctx->cpi, reference_flag);
970  return VPX_CODEC_OK;
971}
972
973static vpx_codec_err_t vp9e_set_roi_map(vpx_codec_alg_priv_t *ctx,
974                                        int ctr_id,
975                                        va_list args) {
976  // TODO(yaowu): Need to re-implement and test for VP9.
977  return VPX_CODEC_INVALID_PARAM;
978}
979
980
981static vpx_codec_err_t vp9e_set_activemap(vpx_codec_alg_priv_t *ctx,
982                                          int ctr_id,
983                                          va_list args) {
984  // TODO(yaowu): Need to re-implement and test for VP9.
985  return VPX_CODEC_INVALID_PARAM;
986}
987
988static vpx_codec_err_t vp9e_set_scalemode(vpx_codec_alg_priv_t *ctx,
989                                          int ctr_id,
990                                          va_list args) {
991  vpx_scaling_mode_t *data =  va_arg(args, vpx_scaling_mode_t *);
992
993  if (data) {
994    int res;
995    vpx_scaling_mode_t scalemode = *(vpx_scaling_mode_t *)data;
996    res = vp9_set_internal_size(ctx->cpi,
997                                (VPX_SCALING)scalemode.h_scaling_mode,
998                                (VPX_SCALING)scalemode.v_scaling_mode);
999
1000    if (!res) {
1001      return VPX_CODEC_OK;
1002    } else {
1003      return VPX_CODEC_INVALID_PARAM;
1004    }
1005  } else {
1006    return VPX_CODEC_INVALID_PARAM;
1007  }
1008}
1009
1010static vpx_codec_err_t vp9e_set_svc(vpx_codec_alg_priv_t *ctx, int ctr_id,
1011                                    va_list args) {
1012  int data = va_arg(args, int);
1013  vp9_set_svc(ctx->cpi, data);
1014  return VPX_CODEC_OK;
1015}
1016
1017static vpx_codec_err_t vp9e_set_svc_parameters(vpx_codec_alg_priv_t *ctx,
1018                                               int ctr_id, va_list args) {
1019  vpx_svc_parameters_t *data = va_arg(args, vpx_svc_parameters_t *);
1020  VP9_COMP *cpi = (VP9_COMP *)ctx->cpi;
1021  vpx_svc_parameters_t params;
1022
1023  if (data == NULL) {
1024    return VPX_CODEC_INVALID_PARAM;
1025  }
1026
1027  params = *(vpx_svc_parameters_t *)data;
1028
1029  cpi->current_layer = params.layer;
1030  cpi->lst_fb_idx = params.lst_fb_idx;
1031  cpi->gld_fb_idx = params.gld_fb_idx;
1032  cpi->alt_fb_idx = params.alt_fb_idx;
1033
1034  if (vp9_set_size_literal(ctx->cpi, params.width, params.height) != 0) {
1035    return VPX_CODEC_INVALID_PARAM;
1036  }
1037
1038  ctx->cfg.rc_max_quantizer = params.max_quantizer;
1039  ctx->cfg.rc_min_quantizer = params.min_quantizer;
1040
1041  set_vp9e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg);
1042  vp9_change_config(ctx->cpi, &ctx->oxcf);
1043
1044  return VPX_CODEC_OK;
1045}
1046
1047static vpx_codec_ctrl_fn_map_t vp9e_ctf_maps[] = {
1048  {VP8_SET_REFERENCE,                 vp9e_set_reference},
1049  {VP8_COPY_REFERENCE,                vp9e_copy_reference},
1050  {VP8_SET_POSTPROC,                  vp9e_set_previewpp},
1051  {VP8E_UPD_ENTROPY,                  vp9e_update_entropy},
1052  {VP8E_UPD_REFERENCE,                vp9e_update_reference},
1053  {VP8E_USE_REFERENCE,                vp9e_use_reference},
1054  {VP8E_SET_ROI_MAP,                  vp9e_set_roi_map},
1055  {VP8E_SET_ACTIVEMAP,                vp9e_set_activemap},
1056  {VP8E_SET_SCALEMODE,                vp9e_set_scalemode},
1057  {VP8E_SET_CPUUSED,                  set_param},
1058  {VP8E_SET_NOISE_SENSITIVITY,        set_param},
1059  {VP8E_SET_ENABLEAUTOALTREF,         set_param},
1060  {VP8E_SET_SHARPNESS,                set_param},
1061  {VP8E_SET_STATIC_THRESHOLD,         set_param},
1062  {VP9E_SET_TILE_COLUMNS,             set_param},
1063  {VP9E_SET_TILE_ROWS,                set_param},
1064  {VP8E_GET_LAST_QUANTIZER,           get_param},
1065  {VP8E_GET_LAST_QUANTIZER_64,        get_param},
1066  {VP8E_SET_ARNR_MAXFRAMES,           set_param},
1067  {VP8E_SET_ARNR_STRENGTH,            set_param},
1068  {VP8E_SET_ARNR_TYPE,                set_param},
1069  {VP8E_SET_TUNING,                   set_param},
1070  {VP8E_SET_CQ_LEVEL,                 set_param},
1071  {VP8E_SET_MAX_INTRA_BITRATE_PCT,    set_param},
1072  {VP9E_SET_LOSSLESS,                 set_param},
1073  {VP9E_SET_FRAME_PARALLEL_DECODING,  set_param},
1074  {VP9_GET_REFERENCE,                 get_reference},
1075  {VP9E_SET_SVC,                      vp9e_set_svc},
1076  {VP9E_SET_SVC_PARAMETERS,           vp9e_set_svc_parameters},
1077  { -1, NULL},
1078};
1079
1080static vpx_codec_enc_cfg_map_t vp9e_usage_cfg_map[] = {
1081  {
1082    0,
1083    {  // NOLINT
1084      0,                  /* g_usage */
1085      0,                  /* g_threads */
1086      0,                  /* g_profile */
1087
1088      320,                /* g_width */
1089      240,                /* g_height */
1090      {1, 30},            /* g_timebase */
1091
1092      0,                  /* g_error_resilient */
1093
1094      VPX_RC_ONE_PASS,    /* g_pass */
1095
1096      25,                 /* g_lag_in_frames */
1097
1098      0,                  /* rc_dropframe_thresh */
1099      0,                  /* rc_resize_allowed */
1100      60,                 /* rc_resize_down_thresold */
1101      30,                 /* rc_resize_up_thresold */
1102
1103      VPX_VBR,            /* rc_end_usage */
1104#if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1105      {0},                /* rc_twopass_stats_in */
1106#endif
1107      256,                /* rc_target_bandwidth */
1108      0,                  /* rc_min_quantizer */
1109      63,                 /* rc_max_quantizer */
1110      100,                /* rc_undershoot_pct */
1111      100,                /* rc_overshoot_pct */
1112
1113      6000,               /* rc_max_buffer_size */
1114      4000,               /* rc_buffer_initial_size; */
1115      5000,               /* rc_buffer_optimal_size; */
1116
1117      50,                 /* rc_two_pass_vbrbias  */
1118      0,                  /* rc_two_pass_vbrmin_section */
1119      2000,               /* rc_two_pass_vbrmax_section */
1120
1121      /* keyframing settings (kf) */
1122      VPX_KF_AUTO,        /* g_kfmode*/
1123      0,                  /* kf_min_dist */
1124      9999,               /* kf_max_dist */
1125
1126      VPX_SS_DEFAULT_LAYERS, /* ss_number_layers */
1127
1128#if VPX_ENCODER_ABI_VERSION == (1 + VPX_CODEC_ABI_VERSION)
1129      1,                  /* g_delete_first_pass_file */
1130      "vp8.fpf"           /* first pass filename */
1131#endif
1132    }
1133  },
1134  { -1, {NOT_IMPLEMENTED}}
1135};
1136
1137
1138#ifndef VERSION_STRING
1139#define VERSION_STRING
1140#endif
1141CODEC_INTERFACE(vpx_codec_vp9_cx) = {
1142  "WebM Project VP9 Encoder" VERSION_STRING,
1143  VPX_CODEC_INTERNAL_ABI_VERSION,
1144  VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR |
1145  VPX_CODEC_CAP_OUTPUT_PARTITION,
1146  /* vpx_codec_caps_t          caps; */
1147  vp9e_init,          /* vpx_codec_init_fn_t       init; */
1148  vp9e_destroy,       /* vpx_codec_destroy_fn_t    destroy; */
1149  vp9e_ctf_maps,      /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
1150  NOT_IMPLEMENTED,    /* vpx_codec_get_mmap_fn_t   get_mmap; */
1151  NOT_IMPLEMENTED,    /* vpx_codec_set_mmap_fn_t   set_mmap; */
1152  {  // NOLINT
1153    NOT_IMPLEMENTED,    /* vpx_codec_peek_si_fn_t    peek_si; */
1154    NOT_IMPLEMENTED,    /* vpx_codec_get_si_fn_t     get_si; */
1155    NOT_IMPLEMENTED,    /* vpx_codec_decode_fn_t     decode; */
1156    NOT_IMPLEMENTED,    /* vpx_codec_frame_get_fn_t  frame_get; */
1157  },
1158  {  // NOLINT
1159    vp9e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t    peek_si; */
1160    vp9e_encode,        /* vpx_codec_encode_fn_t      encode; */
1161    vp9e_get_cxdata,    /* vpx_codec_get_cx_data_fn_t   frame_get; */
1162    vp9e_set_config,
1163    NOT_IMPLEMENTED,
1164    vp9e_get_preview,
1165  } /* encoder functions */
1166};
1167
1168
1169#if CONFIG_EXPERIMENTAL
1170
1171CODEC_INTERFACE(vpx_codec_vp9x_cx) = {
1172  "VP8 Experimental Encoder" VERSION_STRING,
1173  VPX_CODEC_INTERNAL_ABI_VERSION,
1174  VPX_CODEC_CAP_ENCODER | VPX_CODEC_CAP_PSNR,
1175  /* vpx_codec_caps_t          caps; */
1176  vp9e_exp_init,      /* vpx_codec_init_fn_t       init; */
1177  vp9e_destroy,       /* vpx_codec_destroy_fn_t    destroy; */
1178  vp9e_ctf_maps,      /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
1179  NOT_IMPLEMENTED,    /* vpx_codec_get_mmap_fn_t   get_mmap; */
1180  NOT_IMPLEMENTED,    /* vpx_codec_set_mmap_fn_t   set_mmap; */
1181  {  // NOLINT
1182    NOT_IMPLEMENTED,    /* vpx_codec_peek_si_fn_t    peek_si; */
1183    NOT_IMPLEMENTED,    /* vpx_codec_get_si_fn_t     get_si; */
1184    NOT_IMPLEMENTED,    /* vpx_codec_decode_fn_t     decode; */
1185    NOT_IMPLEMENTED,    /* vpx_codec_frame_get_fn_t  frame_get; */
1186  },
1187  {  // NOLINT
1188    vp9e_usage_cfg_map, /* vpx_codec_enc_cfg_map_t    peek_si; */
1189    vp9e_encode,        /* vpx_codec_encode_fn_t      encode; */
1190    vp9e_get_cxdata,    /* vpx_codec_get_cx_data_fn_t   frame_get; */
1191    vp9e_set_config,
1192    NOT_IMPLEMENTED,
1193    vp9e_get_preview,
1194  } /* encoder functions */
1195};
1196#endif
1197