vp9_cx_iface.c 41.8 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11 12
#include <stdlib.h>
#include <string.h>
John Koleszar's avatar
John Koleszar committed
13

14 15
#include "vpx/vpx_codec.h"
#include "vpx/internal/vpx_codec_internal.h"
16
#include "./vpx_version.h"
17
#include "vp9/encoder/vp9_onyx_int.h"
John Koleszar's avatar
John Koleszar committed
18
#include "vpx/vp8cx.h"
19
#include "vp9/encoder/vp9_firstpass.h"
John Koleszar's avatar
John Koleszar committed
20
#include "vp9/vp9_iface_common.h"
John Koleszar's avatar
John Koleszar committed
21

22
struct vp9_extracfg {
John Koleszar's avatar
John Koleszar committed
23
  struct vpx_codec_pkt_list *pkt_list;
24
  int                         cpu_used;  // available cpu percentage in 1/16
25
  unsigned int                enable_auto_alt_ref;
John Koleszar's avatar
John Koleszar committed
26
  unsigned int                noise_sensitivity;
27
  unsigned int                sharpness;
John Koleszar's avatar
John Koleszar committed
28
  unsigned int                static_thresh;
29
  unsigned int                tile_columns;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
30
  unsigned int                tile_rows;
31 32 33
  unsigned int                arnr_max_frames;
  unsigned int                arnr_strength;
  unsigned int                arnr_type;
John Koleszar's avatar
John Koleszar committed
34
  vp8e_tuning                 tuning;
35
  unsigned int                cq_level;  // constrained quality level
John Koleszar's avatar
John Koleszar committed
36
  unsigned int                rc_max_intra_bitrate_pct;
John Koleszar's avatar
John Koleszar committed
37
  unsigned int                lossless;
38
  unsigned int                frame_parallel_decoding_mode;
39
  AQ_MODE                     aq_mode;
40
  unsigned int                frame_periodic_boost;
41
  BIT_DEPTH                   bit_depth;
John Koleszar's avatar
John Koleszar committed
42 43
};

John Koleszar's avatar
John Koleszar committed
44
struct extraconfig_map {
45
  int usage;
46
  struct vp9_extracfg cfg;
John Koleszar's avatar
John Koleszar committed
47 48
};

John Koleszar's avatar
John Koleszar committed
49 50 51
static const struct extraconfig_map extracfg_map[] = {
  {
    0,
52
    { // NOLINT
John Koleszar's avatar
John Koleszar committed
53
      NULL,
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      0,                          // cpu_used
      1,                          // enable_auto_alt_ref
      0,                          // noise_sensitivity
      0,                          // sharpness
      0,                          // static_thresh
      0,                          // tile_columns
      0,                          // tile_rows
      7,                          // arnr_max_frames
      5,                          // arnr_strength
      3,                          // arnr_type
      VP8_TUNE_PSNR,              // tuning
      10,                         // cq_level
      0,                          // rc_max_intra_bitrate_pct
      0,                          // lossless
      0,                          // frame_parallel_decoding_mode
      NO_AQ,                      // aq_mode
70
      0,                          // frame_periodic_delta_q
71
      BITS_8,                     // Bit depth
John Koleszar's avatar
John Koleszar committed
72
    }
John Koleszar's avatar
John Koleszar committed
73
  }
John Koleszar's avatar
John Koleszar committed
74 75
};

John Koleszar's avatar
John Koleszar committed
76 77 78
struct vpx_codec_alg_priv {
  vpx_codec_priv_t        base;
  vpx_codec_enc_cfg_t     cfg;
79
  struct vp9_extracfg     extra_cfg;
80
  VP9_CONFIG              oxcf;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
81
  VP9_COMP               *cpi;
John Koleszar's avatar
John Koleszar committed
82
  unsigned char          *cx_data;
83
  size_t                  cx_data_sz;
84
  unsigned char          *pending_cx_data;
85
  size_t                  pending_cx_data_sz;
John Koleszar's avatar
John Koleszar committed
86
  int                     pending_frame_count;
87 88
  size_t                  pending_frame_sizes[8];
  size_t                  pending_frame_magnitude;
John Koleszar's avatar
John Koleszar committed
89 90
  vpx_image_t             preview_img;
  vp8_postproc_cfg_t      preview_ppcfg;
91
  vpx_codec_pkt_list_decl(64) pkt_list;
John Koleszar's avatar
John Koleszar committed
92
  unsigned int                fixed_kf_cntr;
John Koleszar's avatar
John Koleszar committed
93 94
};

95
static VP9_REFFRAME ref_frame_to_vp9_reframe(vpx_ref_frame_type_t frame) {
96 97 98 99 100 101 102 103
  switch (frame) {
    case VP8_LAST_FRAME:
      return VP9_LAST_FLAG;
    case VP8_GOLD_FRAME:
      return VP9_GOLD_FLAG;
    case VP8_ALTR_FRAME:
      return VP9_ALT_FLAG;
  }
James Zern's avatar
James Zern committed
104
  assert(0 && "Invalid Reference Frame");
105 106
  return VP9_LAST_FLAG;
}
John Koleszar's avatar
John Koleszar committed
107

108 109 110
static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
    const struct vpx_internal_error_info *error) {
  const vpx_codec_err_t res = error->error_code;
John Koleszar's avatar
John Koleszar committed
111

112 113
  if (res != VPX_CODEC_OK)
    ctx->base.err_detail = error->has_detail ? error->detail : NULL;
John Koleszar's avatar
John Koleszar committed
114

John Koleszar's avatar
John Koleszar committed
115
  return res;
John Koleszar's avatar
John Koleszar committed
116 117 118
}


119
#undef ERROR
John Koleszar's avatar
John Koleszar committed
120
#define ERROR(str) do {\
John Koleszar's avatar
John Koleszar committed
121 122
    ctx->base.err_detail = str;\
    return VPX_CODEC_INVALID_PARAM;\
123
  } while (0)
John Koleszar's avatar
John Koleszar committed
124

125 126
#define RANGE_CHECK(p, memb, lo, hi) do {\
    if (!(((p)->memb == lo || (p)->memb > (lo)) && (p)->memb <= hi)) \
John Koleszar's avatar
John Koleszar committed
127
      ERROR(#memb " out of range ["#lo".."#hi"]");\
128
  } while (0)
John Koleszar's avatar
John Koleszar committed
129

130 131
#define RANGE_CHECK_HI(p, memb, hi) do {\
    if (!((p)->memb <= (hi))) \
John Koleszar's avatar
John Koleszar committed
132
      ERROR(#memb " out of range [.."#hi"]");\
133
  } while (0)
134

135 136
#define RANGE_CHECK_LO(p, memb, lo) do {\
    if (!((p)->memb >= (lo))) \
John Koleszar's avatar
John Koleszar committed
137
      ERROR(#memb " out of range ["#lo"..]");\
138
  } while (0)
John Koleszar's avatar
John Koleszar committed
139

140 141 142
#define RANGE_CHECK_BOOL(p, memb) do {\
    if (!!((p)->memb) != (p)->memb) ERROR(#memb " expected boolean");\
  } while (0)
John Koleszar's avatar
John Koleszar committed
143

144
static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
John Koleszar's avatar
John Koleszar committed
145
                                       const vpx_codec_enc_cfg_t *cfg,
146
                                       const struct vp9_extracfg *extra_cfg) {
147 148
  RANGE_CHECK(cfg, g_w,                   1, 65535);  // 16 bits available
  RANGE_CHECK(cfg, g_h,                   1, 65535);  // 16 bits available
John Koleszar's avatar
John Koleszar committed
149 150 151
  RANGE_CHECK(cfg, g_timebase.den,        1, 1000000000);
  RANGE_CHECK(cfg, g_timebase.num,        1, cfg->g_timebase.den);
  RANGE_CHECK_HI(cfg, g_profile,          3);
John Koleszar's avatar
John Koleszar committed
152

John Koleszar's avatar
John Koleszar committed
153 154
  RANGE_CHECK_HI(cfg, rc_max_quantizer,   63);
  RANGE_CHECK_HI(cfg, rc_min_quantizer,   cfg->rc_max_quantizer);
155 156
  RANGE_CHECK_BOOL(extra_cfg, lossless);
  RANGE_CHECK(extra_cfg, aq_mode,           0, AQ_MODE_COUNT - 1);
157
  RANGE_CHECK(extra_cfg, frame_periodic_boost, 0, 1);
John Koleszar's avatar
John Koleszar committed
158 159
  RANGE_CHECK_HI(cfg, g_threads,          64);
  RANGE_CHECK_HI(cfg, g_lag_in_frames,    MAX_LAG_BUFFERS);
160
  RANGE_CHECK(cfg, rc_end_usage,          VPX_VBR, VPX_Q);
John Koleszar's avatar
John Koleszar committed
161 162 163 164 165 166 167 168 169 170
  RANGE_CHECK_HI(cfg, rc_undershoot_pct,  1000);
  RANGE_CHECK_HI(cfg, rc_overshoot_pct,   1000);
  RANGE_CHECK_HI(cfg, rc_2pass_vbr_bias_pct, 100);
  RANGE_CHECK(cfg, kf_mode,               VPX_KF_DISABLED, VPX_KF_AUTO);
  RANGE_CHECK_BOOL(cfg,                   rc_resize_allowed);
  RANGE_CHECK_HI(cfg, rc_dropframe_thresh,   100);
  RANGE_CHECK_HI(cfg, rc_resize_up_thresh,   100);
  RANGE_CHECK_HI(cfg, rc_resize_down_thresh, 100);
  RANGE_CHECK(cfg,        g_pass,         VPX_RC_ONE_PASS, VPX_RC_LAST_PASS);

171 172 173 174 175
  if (cfg->rc_resize_allowed == 1) {
    RANGE_CHECK(cfg, rc_scaled_width, 1, cfg->g_w);
    RANGE_CHECK(cfg, rc_scaled_height, 1, cfg->g_h);
  }

176
  RANGE_CHECK(cfg, ss_number_layers, 1, VPX_SS_MAX_LAYERS);
177 178
  RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
  if (cfg->ts_number_layers > 1) {
179
    unsigned int i;
180 181
    for (i = 1; i < cfg->ts_number_layers; ++i)
      if (cfg->ts_target_bitrate[i] < cfg->ts_target_bitrate[i - 1])
182
        ERROR("ts_target_bitrate entries are not increasing");
183 184 185 186

    RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers - 1], 1, 1);
    for (i = cfg->ts_number_layers - 2; i > 0; --i)
      if (cfg->ts_rate_decimator[i - 1] != 2 * cfg->ts_rate_decimator[i])
187 188 189
        ERROR("ts_rate_decimator factors are not powers of 2");
  }

190 191 192 193 194
  // VP8 does not support a lower bound on the keyframe interval in
  // automatic keyframe placement mode.
  if (cfg->kf_mode != VPX_KF_DISABLED &&
      cfg->kf_min_dist != cfg->kf_max_dist &&
      cfg->kf_min_dist > 0)
John Koleszar's avatar
John Koleszar committed
195 196 197
    ERROR("kf_min_dist not supported in auto mode, use 0 "
          "or kf_max_dist instead.");

198 199 200 201 202 203 204 205 206 207
  RANGE_CHECK_BOOL(extra_cfg,  enable_auto_alt_ref);
  RANGE_CHECK(extra_cfg, cpu_used, -16, 16);
  RANGE_CHECK_HI(extra_cfg, noise_sensitivity, 6);
  RANGE_CHECK(extra_cfg, tile_columns, 0, 6);
  RANGE_CHECK(extra_cfg, tile_rows, 0, 2);
  RANGE_CHECK_HI(extra_cfg, sharpness, 7);
  RANGE_CHECK(extra_cfg, arnr_max_frames, 0, 15);
  RANGE_CHECK_HI(extra_cfg, arnr_strength, 6);
  RANGE_CHECK(extra_cfg, arnr_type, 1, 3);
  RANGE_CHECK(extra_cfg, cq_level, 0, 63);
John Koleszar's avatar
John Koleszar committed
208

Yaowu Xu's avatar
Yaowu Xu committed
209
  // TODO(yaowu): remove this when ssim tuning is implemented for vp9
210
  if (extra_cfg->tuning == VP8_TUNE_SSIM)
Yaowu Xu's avatar
Yaowu Xu committed
211 212
      ERROR("Option --tune=ssim is not currently supported in VP9.");

John Koleszar's avatar
John Koleszar committed
213 214
  if (cfg->g_pass == VPX_RC_LAST_PASS) {
    size_t           packet_sz = sizeof(FIRSTPASS_STATS);
215
    int              n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
216
    const FIRSTPASS_STATS *stats;
John Koleszar's avatar
John Koleszar committed
217

Adrian Grange's avatar
Adrian Grange committed
218
    if (cfg->rc_twopass_stats_in.buf == NULL)
John Koleszar's avatar
John Koleszar committed
219 220 221 222 223
      ERROR("rc_twopass_stats_in.buf not set.");

    if (cfg->rc_twopass_stats_in.sz % packet_sz)
      ERROR("rc_twopass_stats_in.sz indicates truncated packet.");

224 225 226 227 228 229
    if (cfg->ss_number_layers > 1) {
      int i;
      unsigned int n_packets_per_layer[VPX_SS_MAX_LAYERS] = {0};

      stats = cfg->rc_twopass_stats_in.buf;
      for (i = 0; i < n_packets; ++i) {
230
        const int layer_id = (int)stats[i].spatial_layer_id;
231 232 233 234 235 236 237 238 239 240 241
        if (layer_id >= 0 && layer_id < (int)cfg->ss_number_layers) {
          ++n_packets_per_layer[layer_id];
        }
      }

      for (i = 0; i < (int)cfg->ss_number_layers; ++i) {
        unsigned int layer_id;
        if (n_packets_per_layer[i] < 2) {
          ERROR("rc_twopass_stats_in requires at least two packets for each "
                "layer.");
        }
John Koleszar's avatar
John Koleszar committed
242

243 244
        stats = (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf +
                n_packets - cfg->ss_number_layers + i;
245
        layer_id = (int)stats->spatial_layer_id;
John Koleszar's avatar
John Koleszar committed
246

247 248 249 250 251 252 253 254 255 256 257 258 259 260
        if (layer_id >= cfg->ss_number_layers
            ||(int)(stats->count + 0.5) != n_packets_per_layer[layer_id] - 1)
          ERROR("rc_twopass_stats_in missing EOS stats packet");
      }
    } else {
      if (cfg->rc_twopass_stats_in.sz < 2 * packet_sz)
        ERROR("rc_twopass_stats_in requires at least two packets.");

      stats =
          (const FIRSTPASS_STATS *)cfg->rc_twopass_stats_in.buf + n_packets - 1;

      if ((int)(stats->count + 0.5) != n_packets - 1)
        ERROR("rc_twopass_stats_in missing EOS stats packet");
    }
John Koleszar's avatar
John Koleszar committed
261
  }
262 263 264 265 266 267
  if (cfg->g_profile <= (unsigned int)PROFILE_1 &&
      extra_cfg->bit_depth > BITS_8)
    ERROR("High bit-depth not supported in profile < 2");
  if (cfg->g_profile > (unsigned int)PROFILE_1 &&
      extra_cfg->bit_depth == BITS_8)
    ERROR("Bit-depth 8 not supported in profile > 1");
John Koleszar's avatar
John Koleszar committed
268 269

  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
270 271 272 273
}


static vpx_codec_err_t validate_img(vpx_codec_alg_priv_t *ctx,
274
                                    const vpx_image_t *img) {
John Koleszar's avatar
John Koleszar committed
275
  switch (img->fmt) {
James Zern's avatar
James Zern committed
276 277
    case VPX_IMG_FMT_YV12:
    case VPX_IMG_FMT_I420:
278 279
    case VPX_IMG_FMT_I422:
    case VPX_IMG_FMT_I444:
John Koleszar's avatar
John Koleszar committed
280
      break;
John Koleszar's avatar
John Koleszar committed
281
    default:
282 283
      ERROR("Invalid image format. Only YV12, I420, I422, I444 images are "
            "supported.");
John Koleszar's avatar
John Koleszar committed
284
  }
John Koleszar's avatar
John Koleszar committed
285

286
  if (img->d_w != ctx->cfg.g_w || img->d_h != ctx->cfg.g_h)
John Koleszar's avatar
John Koleszar committed
287
    ERROR("Image size must match encoder init configuration size");
John Koleszar's avatar
John Koleszar committed
288

John Koleszar's avatar
John Koleszar committed
289
  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
290 291 292
}


293 294 295 296 297
static vpx_codec_err_t set_encoder_config(
    VP9_CONFIG *oxcf,
    const vpx_codec_enc_cfg_t *cfg,
    const struct vp9_extracfg *extra_cfg) {
  oxcf->profile = cfg->g_profile;
298 299
  oxcf->width   = cfg->g_w;
  oxcf->height  = cfg->g_h;
300
  oxcf->bit_depth = extra_cfg->bit_depth;
301 302 303
  // guess a frame rate if out of whack, use 30
  oxcf->framerate = (double)cfg->g_timebase.den / cfg->g_timebase.num;
  if (oxcf->framerate > 180)
James Zern's avatar
James Zern committed
304
    oxcf->framerate = 30;
John Koleszar's avatar
John Koleszar committed
305

306
  switch (cfg->g_pass) {
John Koleszar's avatar
John Koleszar committed
307
    case VPX_RC_ONE_PASS:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
308
      oxcf->mode = MODE_GOODQUALITY;
John Koleszar's avatar
John Koleszar committed
309
      break;
John Koleszar's avatar
John Koleszar committed
310
    case VPX_RC_FIRST_PASS:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
311
      oxcf->mode = MODE_FIRSTPASS;
John Koleszar's avatar
John Koleszar committed
312
      break;
John Koleszar's avatar
John Koleszar committed
313
    case VPX_RC_LAST_PASS:
Dmitry Kovalev's avatar
Dmitry Kovalev committed
314
      oxcf->mode = MODE_SECONDPASS_BEST;
John Koleszar's avatar
John Koleszar committed
315 316 317
      break;
  }

318 319
  oxcf->lag_in_frames = cfg->g_pass == VPX_RC_FIRST_PASS ? 0
                                                         : cfg->g_lag_in_frames;
John Koleszar's avatar
John Koleszar committed
320

321 322
  oxcf->end_usage = USAGE_LOCAL_FILE_PLAYBACK;
  if (cfg->rc_end_usage == VPX_CQ)
Adrian Grange's avatar
Adrian Grange committed
323
    oxcf->end_usage = USAGE_CONSTRAINED_QUALITY;
324
  else if (cfg->rc_end_usage == VPX_Q)
Adrian Grange's avatar
Adrian Grange committed
325
    oxcf->end_usage = USAGE_CONSTANT_QUALITY;
326
  else if (cfg->rc_end_usage == VPX_CBR)
Marco Paniconi's avatar
Marco Paniconi committed
327
    oxcf->end_usage = USAGE_STREAM_FROM_SERVER;
328

329 330
  oxcf->target_bandwidth         = cfg->rc_target_bitrate;
  oxcf->rc_max_intra_bitrate_pct = extra_cfg->rc_max_intra_bitrate_pct;
John Koleszar's avatar
John Koleszar committed
331

332 333 334
  oxcf->best_allowed_q  = vp9_quantizer_to_qindex(cfg->rc_min_quantizer);
  oxcf->worst_allowed_q = vp9_quantizer_to_qindex(cfg->rc_max_quantizer);
  oxcf->cq_level        = vp9_quantizer_to_qindex(extra_cfg->cq_level);
John Koleszar's avatar
John Koleszar committed
335 336
  oxcf->fixed_q = -1;

337 338 339
  oxcf->under_shoot_pct         = cfg->rc_undershoot_pct;
  oxcf->over_shoot_pct          = cfg->rc_overshoot_pct;

340 341 342 343
  oxcf->allow_spatial_resampling = cfg->rc_resize_allowed;
  oxcf->scaled_frame_width       = cfg->rc_scaled_width;
  oxcf->scaled_frame_height      = cfg->rc_scaled_height;

344 345 346
  oxcf->maximum_buffer_size     = cfg->rc_buf_sz;
  oxcf->starting_buffer_level   = cfg->rc_buf_initial_sz;
  oxcf->optimal_buffer_level    = cfg->rc_buf_optimal_sz;
John Koleszar's avatar
John Koleszar committed
347

348
  oxcf->drop_frames_water_mark   = cfg->rc_dropframe_thresh;
John Koleszar's avatar
John Koleszar committed
349

350 351 352
  oxcf->two_pass_vbrbias         = cfg->rc_2pass_vbr_bias_pct;
  oxcf->two_pass_vbrmin_section  = cfg->rc_2pass_vbr_minsection_pct;
  oxcf->two_pass_vbrmax_section  = cfg->rc_2pass_vbr_maxsection_pct;
353

354 355
  oxcf->auto_key               = cfg->kf_mode == VPX_KF_AUTO &&
                                 cfg->kf_min_dist != cfg->kf_max_dist;
John Koleszar's avatar
John Koleszar committed
356

357
  oxcf->key_freq               = cfg->kf_max_dist;
John Koleszar's avatar
John Koleszar committed
358

359 360 361 362 363
  oxcf->cpu_used               =  extra_cfg->cpu_used;
  oxcf->encode_breakout        =  extra_cfg->static_thresh;
  oxcf->play_alternate         =  extra_cfg->enable_auto_alt_ref;
  oxcf->noise_sensitivity      =  extra_cfg->noise_sensitivity;
  oxcf->sharpness              =  extra_cfg->sharpness;
John Koleszar's avatar
John Koleszar committed
364

365 366
  oxcf->two_pass_stats_in      =  cfg->rc_twopass_stats_in;
  oxcf->output_pkt_list        =  extra_cfg->pkt_list;
John Koleszar's avatar
John Koleszar committed
367

368 369 370
  oxcf->arnr_max_frames = extra_cfg->arnr_max_frames;
  oxcf->arnr_strength   = extra_cfg->arnr_strength;
  oxcf->arnr_type       = extra_cfg->arnr_type;
John Koleszar's avatar
John Koleszar committed
371

372
  oxcf->tuning = extra_cfg->tuning;
John Koleszar's avatar
John Koleszar committed
373

374 375
  oxcf->tile_columns = extra_cfg->tile_columns;
  oxcf->tile_rows    = extra_cfg->tile_rows;
376

377
  oxcf->lossless = extra_cfg->lossless;
Hui Su's avatar
Hui Su committed
378

379 380
  oxcf->error_resilient_mode         = cfg->g_error_resilient;
  oxcf->frame_parallel_decoding_mode = extra_cfg->frame_parallel_decoding_mode;
381

382
  oxcf->aq_mode = extra_cfg->aq_mode;
383

384 385
  oxcf->frame_periodic_boost =  extra_cfg->frame_periodic_boost;

386
  oxcf->ss_number_layers = cfg->ss_number_layers;
387

388
  if (oxcf->ss_number_layers > 1) {
389
    vp9_copy(oxcf->ss_target_bitrate, cfg->ss_target_bitrate);
390
  } else if (oxcf->ss_number_layers == 1) {
Paul Wilkins's avatar
Paul Wilkins committed
391
    oxcf->ss_target_bitrate[0] = (int)oxcf->target_bandwidth;
392 393
  }

394
  oxcf->ts_number_layers = cfg->ts_number_layers;
395 396

  if (oxcf->ts_number_layers > 1) {
397 398
    vp9_copy(oxcf->ts_target_bitrate, cfg->ts_target_bitrate);
    vp9_copy(oxcf->ts_rate_decimator, cfg->ts_rate_decimator);
399
  } else if (oxcf->ts_number_layers == 1) {
400
    oxcf->ts_target_bitrate[0] = (int)oxcf->target_bandwidth;
401 402 403
    oxcf->ts_rate_decimator[0] = 1;
  }

John Koleszar's avatar
John Koleszar committed
404
  /*
405 406 407
  printf("Current VP9 Settings: \n");
  printf("target_bandwidth: %d\n", oxcf->target_bandwidth);
  printf("noise_sensitivity: %d\n", oxcf->noise_sensitivity);
408
  printf("sharpness: %d\n",    oxcf->sharpness);
409
  printf("cpu_used: %d\n",  oxcf->cpu_used);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
410
  printf("Mode: %d\n",     oxcf->mode);
411 412 413 414 415 416 417 418 419 420 421
  printf("auto_key: %d\n",  oxcf->auto_key);
  printf("key_freq: %d\n", oxcf->key_freq);
  printf("end_usage: %d\n", oxcf->end_usage);
  printf("under_shoot_pct: %d\n", oxcf->under_shoot_pct);
  printf("over_shoot_pct: %d\n", oxcf->over_shoot_pct);
  printf("starting_buffer_level: %d\n", oxcf->starting_buffer_level);
  printf("optimal_buffer_level: %d\n",  oxcf->optimal_buffer_level);
  printf("maximum_buffer_size: %d\n", oxcf->maximum_buffer_size);
  printf("fixed_q: %d\n",  oxcf->fixed_q);
  printf("worst_allowed_q: %d\n", oxcf->worst_allowed_q);
  printf("best_allowed_q: %d\n", oxcf->best_allowed_q);
422 423 424
  printf("allow_spatial_resampling: %d\n", oxcf->allow_spatial_resampling);
  printf("scaled_frame_width: %d\n", oxcf->scaled_frame_width);
  printf("scaled_frame_height: %d\n", oxcf->scaled_frame_height);
425 426 427 428 429 430 431 432
  printf("two_pass_vbrbias: %d\n",  oxcf->two_pass_vbrbias);
  printf("two_pass_vbrmin_section: %d\n", oxcf->two_pass_vbrmin_section);
  printf("two_pass_vbrmax_section: %d\n", oxcf->two_pass_vbrmax_section);
  printf("lag_in_frames: %d\n", oxcf->lag_in_frames);
  printf("play_alternate: %d\n", oxcf->play_alternate);
  printf("Version: %d\n", oxcf->Version);
  printf("encode_breakout: %d\n", oxcf->encode_breakout);
  printf("error resilient: %d\n", oxcf->error_resilient_mode);
433 434
  printf("frame parallel detokenization: %d\n",
         oxcf->frame_parallel_decoding_mode);
John Koleszar's avatar
John Koleszar committed
435 436
  */
  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
437 438
}

439 440
static vpx_codec_err_t encoder_set_config(vpx_codec_alg_priv_t *ctx,
                                          const vpx_codec_enc_cfg_t  *cfg) {
John Koleszar's avatar
John Koleszar committed
441
  vpx_codec_err_t res;
John Koleszar's avatar
John Koleszar committed
442

443
  if (cfg->g_w != ctx->cfg.g_w || cfg->g_h != ctx->cfg.g_h)
John Koleszar's avatar
John Koleszar committed
444
    ERROR("Cannot change width or height after initialization");
John Koleszar's avatar
John Koleszar committed
445

446 447 448 449 450
  // Prevent increasing lag_in_frames. This check is stricter than it needs
  // to be -- the limit is not increasing past the first lag_in_frames
  // value, but we don't track the initial config, only the last successful
  // config.
  if (cfg->g_lag_in_frames > ctx->cfg.g_lag_in_frames)
John Koleszar's avatar
John Koleszar committed
451
    ERROR("Cannot increase lag_in_frames");
John Koleszar's avatar
John Koleszar committed
452

453
  res = validate_config(ctx, cfg, &ctx->extra_cfg);
John Koleszar's avatar
John Koleszar committed
454

Adrian Grange's avatar
Adrian Grange committed
455
  if (res == VPX_CODEC_OK) {
John Koleszar's avatar
John Koleszar committed
456
    ctx->cfg = *cfg;
457
    set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
458
    vp9_change_config(ctx->cpi, &ctx->oxcf);
John Koleszar's avatar
John Koleszar committed
459
  }
John Koleszar's avatar
John Koleszar committed
460

John Koleszar's avatar
John Koleszar committed
461
  return res;
John Koleszar's avatar
John Koleszar committed
462 463
}

464
static vpx_codec_err_t ctrl_get_param(vpx_codec_alg_priv_t *ctx, int ctrl_id,
465
                                 va_list args) {
John Koleszar's avatar
John Koleszar committed
466
  void *arg = va_arg(args, void *);
John Koleszar's avatar
John Koleszar committed
467 468 469

#define MAP(id, var) case id: *(RECAST(id, arg)) = var; break

470 471
  if (arg == NULL)
    return VPX_CODEC_INVALID_PARAM;
John Koleszar's avatar
John Koleszar committed
472

John Koleszar's avatar
John Koleszar committed
473
  switch (ctrl_id) {
474 475
    MAP(VP8E_GET_LAST_QUANTIZER, vp9_get_quantizer(ctx->cpi));
    MAP(VP8E_GET_LAST_QUANTIZER_64,
476
        vp9_qindex_to_quantizer(vp9_get_quantizer(ctx->cpi)));
John Koleszar's avatar
John Koleszar committed
477
  }
John Koleszar's avatar
John Koleszar committed
478

John Koleszar's avatar
John Koleszar committed
479
  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
480 481 482 483
#undef MAP
}


484 485
static vpx_codec_err_t ctrl_set_param(vpx_codec_alg_priv_t *ctx, int ctrl_id,
                                      va_list args) {
486
  vpx_codec_err_t res = VPX_CODEC_OK;
487
  struct vp9_extracfg extra_cfg = ctx->extra_cfg;
John Koleszar's avatar
John Koleszar committed
488 489 490

#define MAP(id, var) case id: var = CAST(id, args); break;

John Koleszar's avatar
John Koleszar committed
491
  switch (ctrl_id) {
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    MAP(VP8E_SET_CPUUSED,                 extra_cfg.cpu_used);
    MAP(VP8E_SET_ENABLEAUTOALTREF,        extra_cfg.enable_auto_alt_ref);
    MAP(VP8E_SET_NOISE_SENSITIVITY,       extra_cfg.noise_sensitivity);
    MAP(VP8E_SET_SHARPNESS,               extra_cfg.sharpness);
    MAP(VP8E_SET_STATIC_THRESHOLD,        extra_cfg.static_thresh);
    MAP(VP9E_SET_TILE_COLUMNS,            extra_cfg.tile_columns);
    MAP(VP9E_SET_TILE_ROWS,               extra_cfg.tile_rows);
    MAP(VP8E_SET_ARNR_MAXFRAMES,          extra_cfg.arnr_max_frames);
    MAP(VP8E_SET_ARNR_STRENGTH,           extra_cfg.arnr_strength);
    MAP(VP8E_SET_ARNR_TYPE,               extra_cfg.arnr_type);
    MAP(VP8E_SET_TUNING,                  extra_cfg.tuning);
    MAP(VP8E_SET_CQ_LEVEL,                extra_cfg.cq_level);
    MAP(VP8E_SET_MAX_INTRA_BITRATE_PCT,   extra_cfg.rc_max_intra_bitrate_pct);
    MAP(VP9E_SET_LOSSLESS,                extra_cfg.lossless);
    MAP(VP9E_SET_FRAME_PARALLEL_DECODING,
        extra_cfg.frame_parallel_decoding_mode);
    MAP(VP9E_SET_AQ_MODE,                 extra_cfg.aq_mode);
509
    MAP(VP9E_SET_FRAME_PERIODIC_BOOST,   extra_cfg.frame_periodic_boost);
John Koleszar's avatar
John Koleszar committed
510 511
  }

512
  res = validate_config(ctx, &ctx->cfg, &extra_cfg);
John Koleszar's avatar
John Koleszar committed
513

Adrian Grange's avatar
Adrian Grange committed
514
  if (res == VPX_CODEC_OK) {
515
    ctx->extra_cfg = extra_cfg;
516
    set_encoder_config(&ctx->oxcf, &ctx->cfg, &ctx->extra_cfg);
517
    vp9_change_config(ctx->cpi, &ctx->oxcf);
John Koleszar's avatar
John Koleszar committed
518 519 520
  }

  return res;
John Koleszar's avatar
John Koleszar committed
521 522
#undef MAP
}
523

524
static vpx_codec_err_t encoder_common_init(vpx_codec_ctx_t *ctx) {
525
  vpx_codec_err_t res = VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
526

Adrian Grange's avatar
Adrian Grange committed
527
  if (ctx->priv == NULL) {
528 529 530
    int i;
    vpx_codec_enc_cfg_t *cfg;
    struct vpx_codec_alg_priv *priv = calloc(1, sizeof(*priv));
John Koleszar's avatar
John Koleszar committed
531

Adrian Grange's avatar
Adrian Grange committed
532
    if (priv == NULL) return VPX_CODEC_MEM_ERROR;
533

John Koleszar's avatar
John Koleszar committed
534 535 536 537 538
    ctx->priv = &priv->base;
    ctx->priv->sz = sizeof(*ctx->priv);
    ctx->priv->iface = ctx->iface;
    ctx->priv->alg_priv = priv;
    ctx->priv->init_flags = ctx->init_flags;
John Koleszar's avatar
John Koleszar committed
539
    ctx->priv->enc.total_encoders = 1;
John Koleszar's avatar
John Koleszar committed
540 541

    if (ctx->config.enc) {
542 543
      // Update the reference to the config structure to an
      // internal copy.
John Koleszar's avatar
John Koleszar committed
544 545 546
      ctx->priv->alg_priv->cfg = *ctx->config.enc;
      ctx->config.enc = &ctx->priv->alg_priv->cfg;
    }
547

548
    cfg = &ctx->priv->alg_priv->cfg;
John Koleszar's avatar
John Koleszar committed
549

550 551 552
    // Select the extra vp6 configuration table based on the current
    // usage value. If the current usage value isn't found, use the
    // values for usage case 0.
John Koleszar's avatar
John Koleszar committed
553 554
    for (i = 0;
         extracfg_map[i].usage && extracfg_map[i].usage != cfg->g_usage;
555
         ++i) {}
John Koleszar's avatar
John Koleszar committed
556

557 558
    priv->extra_cfg = extracfg_map[i].cfg;
    priv->extra_cfg.pkt_list = &priv->pkt_list.head;
John Koleszar's avatar
John Koleszar committed
559

560
    // Maximum buffer size approximated based on having multiple ARF.
561
    priv->cx_data_sz = priv->cfg.g_w * priv->cfg.g_h * 3 / 2 * 8;
John Koleszar's avatar
John Koleszar committed
562

John Koleszar's avatar
John Koleszar committed
563
    if (priv->cx_data_sz < 4096) priv->cx_data_sz = 4096;
John Koleszar's avatar
John Koleszar committed
564

565
    priv->cx_data = (unsigned char *)malloc(priv->cx_data_sz);
566 567
    if (priv->cx_data == NULL)
      return VPX_CODEC_MEM_ERROR;
John Koleszar's avatar
John Koleszar committed
568

569
    vp9_initialize_enc();
John Koleszar's avatar
John Koleszar committed
570

571
    res = validate_config(priv, &priv->cfg, &priv->extra_cfg);
John Koleszar's avatar
John Koleszar committed
572

Adrian Grange's avatar
Adrian Grange committed
573
    if (res == VPX_CODEC_OK) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
574
      VP9_COMP *cpi;
575
      set_encoder_config(&ctx->priv->alg_priv->oxcf,
576 577
                      &ctx->priv->alg_priv->cfg,
                      &ctx->priv->alg_priv->extra_cfg);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
578 579
      cpi = vp9_create_compressor(&ctx->priv->alg_priv->oxcf);
      if (cpi == NULL)
John Koleszar's avatar
John Koleszar committed
580 581
        res = VPX_CODEC_MEM_ERROR;
      else
Dmitry Kovalev's avatar
Dmitry Kovalev committed
582
        ctx->priv->alg_priv->cpi = cpi;
John Koleszar's avatar
John Koleszar committed
583
    }
John Koleszar's avatar
John Koleszar committed
584
  }
John Koleszar's avatar
John Koleszar committed
585

John Koleszar's avatar
John Koleszar committed
586
  return res;
John Koleszar's avatar
John Koleszar committed
587 588
}

589

590 591 592
static vpx_codec_err_t encoder_init(vpx_codec_ctx_t *ctx,
                                    vpx_codec_priv_enc_mr_cfg_t *data) {
  return encoder_common_init(ctx);
593 594
}

595
static vpx_codec_err_t encoder_destroy(vpx_codec_alg_priv_t *ctx) {
John Koleszar's avatar
John Koleszar committed
596
  free(ctx->cx_data);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
597
  vp9_remove_compressor(ctx->cpi);
John Koleszar's avatar
John Koleszar committed
598 599
  free(ctx);
  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
600 601 602
}

static void pick_quickcompress_mode(vpx_codec_alg_priv_t  *ctx,
603 604 605 606
                                    unsigned long duration,
                                    unsigned long deadline) {
  // Use best quality mode if no deadline is given.
  MODE new_qc = MODE_BESTQUALITY;
Jim Bankoski's avatar
Jim Bankoski committed
607 608

  if (deadline) {
609 610 611 612 613 614 615 616
    // Convert duration parameter from stream timebase to microseconds
    const uint64_t duration_us = (uint64_t)duration * 1000000 *
                               (uint64_t)ctx->cfg.g_timebase.num /
                               (uint64_t)ctx->cfg.g_timebase.den;

    // If the deadline is more that the duration this frame is to be shown,
    // use good quality mode. Otherwise use realtime mode.
    new_qc = (deadline > duration_us) ? MODE_GOODQUALITY : MODE_REALTIME;
Jim Bankoski's avatar
Jim Bankoski committed
617
  }
John Koleszar's avatar
John Koleszar committed
618 619 620 621

  if (ctx->cfg.g_pass == VPX_RC_FIRST_PASS)
    new_qc = MODE_FIRSTPASS;
  else if (ctx->cfg.g_pass == VPX_RC_LAST_PASS)
622 623
    new_qc = (new_qc == MODE_BESTQUALITY) ? MODE_SECONDPASS_BEST
                                          : MODE_SECONDPASS;
John Koleszar's avatar
John Koleszar committed
624

Dmitry Kovalev's avatar
Dmitry Kovalev committed
625 626
  if (ctx->oxcf.mode != new_qc) {
    ctx->oxcf.mode = new_qc;
627
    vp9_change_config(ctx->cpi, &ctx->oxcf);
John Koleszar's avatar
John Koleszar committed
628
  }
John Koleszar's avatar
John Koleszar committed
629 630 631
}


632
static int write_superframe_index(vpx_codec_alg_priv_t *ctx) {
John Koleszar's avatar
John Koleszar committed
633
  uint8_t marker = 0xc0;
Yaowu Xu's avatar
Yaowu Xu committed
634 635
  unsigned int mask;
  int mag, index_sz;
John Koleszar's avatar
John Koleszar committed
636 637 638 639

  assert(ctx->pending_frame_count);
  assert(ctx->pending_frame_count <= 8);

640
  // Add the number of frames to the marker byte
John Koleszar's avatar
John Koleszar committed
641 642
  marker |= ctx->pending_frame_count - 1;

643
  // Choose the magnitude
John Koleszar's avatar
John Koleszar committed
644 645 646 647 648 649 650 651
  for (mag = 0, mask = 0xff; mag < 4; mag++) {
    if (ctx->pending_frame_magnitude < mask)
      break;
    mask <<= 8;
    mask |= 0xff;
  }
  marker |= mag << 3;

652
  // Write the index
John Koleszar's avatar
John Koleszar committed
653 654 655 656 657 658 659
  index_sz = 2 + (mag + 1) * ctx->pending_frame_count;
  if (ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz) {
    uint8_t *x = ctx->pending_cx_data + ctx->pending_cx_data_sz;
    int i, j;

    *x++ = marker;
    for (i = 0; i < ctx->pending_frame_count; i++) {
660
      unsigned int this_sz = (unsigned int)ctx->pending_frame_sizes[i];
John Koleszar's avatar
John Koleszar committed
661 662 663 664 665 666 667 668 669

      for (j = 0; j <= mag; j++) {
        *x++ = this_sz & 0xff;
        this_sz >>= 8;
      }
    }
    *x++ = marker;
    ctx->pending_cx_data_sz += index_sz;
  }
670
  return index_sz;
John Koleszar's avatar
John Koleszar committed
671 672
}

673 674 675 676 677 678
static vpx_codec_err_t encoder_encode(vpx_codec_alg_priv_t  *ctx,
                                      const vpx_image_t *img,
                                      vpx_codec_pts_t pts,
                                      unsigned long duration,
                                      vpx_enc_frame_flags_t flags,
                                      unsigned long deadline) {
John Koleszar's avatar
John Koleszar committed
679
  vpx_codec_err_t res = VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
680

John Koleszar's avatar
John Koleszar committed
681 682
  if (img)
    res = validate_img(ctx, img);
John Koleszar's avatar
John Koleszar committed
683

John Koleszar's avatar
John Koleszar committed
684 685
  pick_quickcompress_mode(ctx, duration, deadline);
  vpx_codec_pkt_list_init(&ctx->pkt_list);
John Koleszar's avatar
John Koleszar committed
686

687 688 689
  // Handle Flags
  if (((flags & VP8_EFLAG_NO_UPD_GF) && (flags & VP8_EFLAG_FORCE_GF)) ||
       ((flags & VP8_EFLAG_NO_UPD_ARF) && (flags & VP8_EFLAG_FORCE_ARF))) {
John Koleszar's avatar
John Koleszar committed
690 691 692
    ctx->base.err_detail = "Conflicting flags.";
    return VPX_CODEC_INVALID_PARAM;
  }
John Koleszar's avatar
John Koleszar committed
693

694 695
  if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
               VP8_EFLAG_NO_REF_ARF)) {
John Koleszar's avatar
John Koleszar committed
696
    int ref = 7;
John Koleszar's avatar
John Koleszar committed
697

John Koleszar's avatar
John Koleszar committed
698
    if (flags & VP8_EFLAG_NO_REF_LAST)
699
      ref ^= VP9_LAST_FLAG;
John Koleszar's avatar
John Koleszar committed
700

John Koleszar's avatar
John Koleszar committed
701
    if (flags & VP8_EFLAG_NO_REF_GF)
702
      ref ^= VP9_GOLD_FLAG;
John Koleszar's avatar
John Koleszar committed
703

John Koleszar's avatar
John Koleszar committed
704
    if (flags & VP8_EFLAG_NO_REF_ARF)
705
      ref ^= VP9_ALT_FLAG;
John Koleszar's avatar
John Koleszar committed
706

707
    vp9_use_as_reference(ctx->cpi, ref);
John Koleszar's avatar
John Koleszar committed
708
  }
John Koleszar's avatar
John Koleszar committed
709

710 711 712
  if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
               VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
               VP8_EFLAG_FORCE_ARF)) {
John Koleszar's avatar
John Koleszar committed
713
    int upd = 7;
John Koleszar's avatar
John Koleszar committed
714

John Koleszar's avatar
John Koleszar committed
715
    if (flags & VP8_EFLAG_NO_UPD_LAST)
716
      upd ^= VP9_LAST_FLAG;
John Koleszar's avatar
John Koleszar committed
717

John Koleszar's avatar
John Koleszar committed
718
    if (flags & VP8_EFLAG_NO_UPD_GF)
719
      upd ^= VP9_GOLD_FLAG;
John Koleszar's avatar
John Koleszar committed
720

John Koleszar's avatar
John Koleszar committed
721
    if (flags & VP8_EFLAG_NO_UPD_ARF)
722
      upd ^= VP9_ALT_FLAG;
John Koleszar's avatar
John Koleszar committed
723

724
    vp9_update_reference(ctx->cpi, upd);
John Koleszar's avatar
John Koleszar committed
725
  }
John Koleszar's avatar
John Koleszar committed
726

John Koleszar's avatar
John Koleszar committed
727
  if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
728
    vp9_update_entropy(ctx->cpi, 0);
John Koleszar's avatar
John Koleszar committed
729
  }
John Koleszar's avatar
John Koleszar committed
730

731 732 733
  // Handle fixed keyframe intervals
  if (ctx->cfg.kf_mode == VPX_KF_AUTO &&
      ctx->cfg.kf_min_dist == ctx->cfg.kf_max_dist) {
John Koleszar's avatar
John Koleszar committed
734 735 736
    if (++ctx->fixed_kf_cntr > ctx->cfg.kf_min_dist) {
      flags |= VPX_EFLAG_FORCE_KF;
      ctx->fixed_kf_cntr = 1;
John Koleszar's avatar
John Koleszar committed
737
    }
John Koleszar's avatar
John Koleszar committed
738
  }
John Koleszar's avatar
John Koleszar committed
739

740
  // Initialize the encoder instance on the first frame.
Adrian Grange's avatar
Adrian Grange committed
741
  if (res == VPX_CODEC_OK && ctx->cpi != NULL) {
John Koleszar's avatar
John Koleszar committed
742 743 744
    unsigned int lib_flags;
    YV12_BUFFER_CONFIG sd;
    int64_t dst_time_stamp, dst_end_time_stamp;
745
    size_t size, cx_data_sz;
John Koleszar's avatar
John Koleszar committed
746
    unsigned char *cx_data;
John Koleszar's avatar
John Koleszar committed
747

748
    // Set up internal flags
John Koleszar's avatar
John Koleszar committed
749
    if (ctx->base.init_flags & VPX_CODEC_USE_PSNR)
750
      ((VP9_COMP *)ctx->cpi)->b_calculate_psnr = 1;
John Koleszar's avatar
John Koleszar committed
751

752
    // Convert API flags to internal codec lib flags
John Koleszar's avatar
John Koleszar committed
753
    lib_flags = (flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
John Koleszar's avatar
John Koleszar committed
754

755 756
    /* vp9 use 10,000,000 ticks/second as time stamp */
    dst_time_stamp = (pts * 10000000 * ctx->cfg.g_timebase.num)
757 758 759
                     / ctx->cfg.g_timebase.den;
    dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num /
                         ctx->cfg.g_timebase.den;
John Koleszar's avatar
John Koleszar committed
760

John Koleszar's avatar
John Koleszar committed
761 762
    if (img != NULL) {
      res = image2yuvconfig(img, &sd);
John Koleszar's avatar
John Koleszar committed
763

764