vp9_dx_iface.c 22 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>
13

14
#include "./vpx_config.h"
15
#include "./vpx_version.h"
16 17 18 19

#include "vpx/internal/vpx_codec_internal.h"
#include "vpx/vp8dx.h"
#include "vpx/vpx_decoder.h"
20
#include "vpx_dsp/bitreader_buffer.h"
21
#include "vpx_dsp/vpx_dsp_common.h"
22
#include "vpx_util/vpx_thread.h"
23

24
#include "vp9/common/vp9_alloccommon.h"
25
#include "vp9/common/vp9_frame_buffers.h"
26

27
#include "vp9/decoder/vp9_decodeframe.h"
28

29
#include "vp9/vp9_dx_iface.h"
30
#include "vp9/vp9_iface_common.h"
John Koleszar's avatar
John Koleszar committed
31

32
#define VP9_CAP_POSTPROC (CONFIG_VP9_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
33 34

static vpx_codec_err_t decoder_init(vpx_codec_ctx_t *ctx,
35
                                    vpx_codec_priv_enc_mr_cfg_t *data) {
36 37 38
  // This function only allocates space for the vpx_codec_alg_priv_t
  // structure. More memory may be required at the time the stream
  // information becomes known.
39 40
  (void)data;

John Koleszar's avatar
John Koleszar committed
41
  if (!ctx->priv) {
42 43
    vpx_codec_alg_priv_t *const priv =
        (vpx_codec_alg_priv_t *)vpx_calloc(1, sizeof(*priv));
44
    if (priv == NULL) return VPX_CODEC_MEM_ERROR;
45

46
    ctx->priv = (vpx_codec_priv_t *)priv;
47
    ctx->priv->init_flags = ctx->init_flags;
48 49
    priv->si.sz = sizeof(priv->si);
    priv->flushed = 0;
50
    if (ctx->config.dec) {
51 52
      priv->cfg = *ctx->config.dec;
      ctx->config.dec = &priv->cfg;
John Koleszar's avatar
John Koleszar committed
53
    }
John Koleszar's avatar
John Koleszar committed
54
  }
John Koleszar's avatar
John Koleszar committed
55

56
  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
57 58
}

59
static vpx_codec_err_t decoder_destroy(vpx_codec_alg_priv_t *ctx) {
60 61
  if (ctx->pbi != NULL) {
    vp9_decoder_remove(ctx->pbi);
62
  }
John Koleszar's avatar
John Koleszar committed
63

64 65
  if (ctx->buffer_pool) {
    vp9_free_ref_frame_buffers(ctx->buffer_pool);
66
    vp9_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
67
  }
68

69 70
  vpx_free(ctx->buffer_pool);
  vpx_free(ctx);
John Koleszar's avatar
John Koleszar committed
71
  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
72 73
}

74 75
static int parse_bitdepth_colorspace_sampling(BITSTREAM_PROFILE profile,
                                              struct vpx_read_bit_buffer *rb) {
76
  vpx_color_space_t color_space;
77
  if (profile >= PROFILE_2) rb->bit_offset += 1;  // Bit-depth 10 or 12.
78
  color_space = (vpx_color_space_t)vpx_rb_read_literal(rb, 3);
79
  if (color_space != VPX_CS_SRGB) {
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    rb->bit_offset += 1;  // [16,235] (including xvycc) vs [0,255] range.
    if (profile == PROFILE_1 || profile == PROFILE_3) {
      rb->bit_offset += 2;  // subsampling x/y.
      rb->bit_offset += 1;  // unused.
    }
  } else {
    if (profile == PROFILE_1 || profile == PROFILE_3) {
      rb->bit_offset += 1;  // unused
    } else {
      // RGB is only available in version 1.
      return 0;
    }
  }
  return 1;
}

96 97 98
static vpx_codec_err_t decoder_peek_si_internal(
    const uint8_t *data, unsigned int data_sz, vpx_codec_stream_info_t *si,
    int *is_intra_only, vpx_decrypt_cb decrypt_cb, void *decrypt_state) {
99
  int intra_only_flag = 0;
100
  uint8_t clear_buffer[10];
101

102
  if (data + data_sz <= data) return VPX_CODEC_INVALID_PARAM;
103 104 105

  si->is_kf = 0;
  si->w = si->h = 0;
John Koleszar's avatar
John Koleszar committed
106

107
  if (decrypt_cb) {
108
    data_sz = VPXMIN(sizeof(clear_buffer), data_sz);
109 110 111 112
    decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
    data = clear_buffer;
  }

113 114
  // A maximum of 6 bits are needed to read the frame marker, profile and
  // show_existing_frame.
115
  if (data_sz < 1) return VPX_CODEC_UNSUP_BITSTREAM;
116

117
  {
118 119
    int show_frame;
    int error_resilient;
120
    struct vpx_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
121
    const int frame_marker = vpx_rb_read_literal(&rb, 2);
122
    const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
123

124
    if (frame_marker != VP9_FRAME_MARKER) return VPX_CODEC_UNSUP_BITSTREAM;
125

126
    if (profile >= MAX_PROFILES) return VPX_CODEC_UNSUP_BITSTREAM;
Yaowu Xu's avatar
Yaowu Xu committed
127

128
    if (vpx_rb_read_bit(&rb)) {  // show an existing frame
129 130
      // If profile is > 2 and show_existing_frame is true, then at least 1 more
      // byte (6+3=9 bits) is needed.
131
      if (profile > 2 && data_sz < 2) return VPX_CODEC_UNSUP_BITSTREAM;
132
      vpx_rb_read_literal(&rb, 3);  // Frame buffer to show.
133 134
      return VPX_CODEC_OK;
    }
135

136 137 138
    // For the rest of the function, a maximum of 9 more bytes are needed
    // (computed by taking the maximum possible bits needed in each case). Note
    // that this has to be updated if we read any more bits in this function.
139
    if (data_sz < 10) return VPX_CODEC_UNSUP_BITSTREAM;
140

141 142 143
    si->is_kf = !vpx_rb_read_bit(&rb);
    show_frame = vpx_rb_read_bit(&rb);
    error_resilient = vpx_rb_read_bit(&rb);
144

145
    if (si->is_kf) {
146
      if (!vp9_read_sync_code(&rb)) return VPX_CODEC_UNSUP_BITSTREAM;
147

148 149
      if (!parse_bitdepth_colorspace_sampling(profile, &rb))
        return VPX_CODEC_UNSUP_BITSTREAM;
150 151
      vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
    } else {
152
      intra_only_flag = show_frame ? 0 : vpx_rb_read_bit(&rb);
153

154
      rb.bit_offset += error_resilient ? 0 : 2;  // reset_frame_context
155

156
      if (intra_only_flag) {
157
        if (!vp9_read_sync_code(&rb)) return VPX_CODEC_UNSUP_BITSTREAM;
158 159 160 161
        if (profile > PROFILE_0) {
          if (!parse_bitdepth_colorspace_sampling(profile, &rb))
            return VPX_CODEC_UNSUP_BITSTREAM;
        }
162 163 164
        rb.bit_offset += REF_FRAMES;  // refresh_frame_flags
        vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
      }
165
    }
John Koleszar's avatar
John Koleszar committed
166
  }
167
  if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
168
  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
169 170
}

171 172 173
static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
                                       unsigned int data_sz,
                                       vpx_codec_stream_info_t *si) {
174
  return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
175 176
}

177 178
static vpx_codec_err_t decoder_get_si(vpx_codec_alg_priv_t *ctx,
                                      vpx_codec_stream_info_t *si) {
179
  const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
180 181
                        ? sizeof(vp9_stream_info_t)
                        : sizeof(vpx_codec_stream_info_t);
John Koleszar's avatar
John Koleszar committed
182
  memcpy(si, &ctx->si, sz);
183
  si->sz = (unsigned int)sz;
John Koleszar's avatar
John Koleszar committed
184

John Koleszar's avatar
John Koleszar committed
185
  return VPX_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
186 187
}

188 189 190 191 192
static void set_error_detail(vpx_codec_alg_priv_t *ctx,
                             const char *const error) {
  ctx->base.err_detail = error;
}

193 194
static vpx_codec_err_t update_error_state(
    vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
195
  if (error->error_code)
196
    set_error_detail(ctx, error->has_detail ? error->detail : NULL);
John Koleszar's avatar
John Koleszar committed
197

198
  return error->error_code;
John Koleszar's avatar
John Koleszar committed
199 200
}

201
static void init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
202 203
  VP9_COMMON *const cm = &ctx->pbi->common;
  BufferPool *const pool = cm->buffer_pool;
204

205 206 207
  cm->new_fb_idx = INVALID_IDX;
  cm->byte_alignment = ctx->byte_alignment;
  cm->skip_loop_filter = ctx->skip_loop_filter;
208

209 210 211 212 213 214 215 216 217 218 219 220 221
  if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
    pool->get_fb_cb = ctx->get_ext_fb_cb;
    pool->release_fb_cb = ctx->release_ext_fb_cb;
    pool->cb_priv = ctx->ext_priv;
  } else {
    pool->get_fb_cb = vp9_get_frame_buffer;
    pool->release_fb_cb = vp9_release_frame_buffer;

    if (vp9_alloc_internal_frame_buffers(&pool->int_frame_buffers))
      vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
                         "Failed to initialize internal frame buffers");

    pool->cb_priv = &pool->int_frame_buffers;
222 223 224 225 226 227 228 229 230
  }
}

static void set_default_ppflags(vp8_postproc_cfg_t *cfg) {
  cfg->post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
  cfg->deblocking_level = 4;
  cfg->noise_level = 0;
}

231 232
static void set_ppflags(const vpx_codec_alg_priv_t *ctx, vp9_ppflags_t *flags) {
  flags->post_proc_flag = ctx->postproc_cfg.post_proc_flag;
233 234 235 236 237

  flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
  flags->noise_level = ctx->postproc_cfg.noise_level;
}

238 239
static vpx_codec_err_t init_decoder(vpx_codec_alg_priv_t *ctx) {
  ctx->last_show_frame = -1;
240
  ctx->need_resync = 1;
241 242 243
  ctx->flushed = 0;

  ctx->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
244
  if (ctx->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
245

246 247 248
  ctx->pbi = vp9_decoder_create(ctx->buffer_pool);
  if (ctx->pbi == NULL) {
    set_error_detail(ctx, "Failed to allocate decoder");
249 250
    return VPX_CODEC_MEM_ERROR;
  }
251 252
  ctx->pbi->max_threads = ctx->cfg.threads;
  ctx->pbi->inv_tile_order = ctx->invert_tile_order;
253

254 255
  // If postprocessing was enabled by the application and a
  // configuration has not been provided, default it.
256
  if (!ctx->postproc_cfg_set && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
257 258 259
    set_default_ppflags(&ctx->postproc_cfg);

  init_buffer_callbacks(ctx);
260 261

  return VPX_CODEC_OK;
262 263
}

264 265
static INLINE void check_resync(vpx_codec_alg_priv_t *const ctx,
                                const VP9Decoder *const pbi) {
266
  // Clear resync flag if the decoder got a key frame or intra only frame.
267 268 269 270 271
  if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
      (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
    ctx->need_resync = 0;
}

272 273 274
static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
                                  const uint8_t **data, unsigned int data_sz,
                                  void *user_priv, int64_t deadline) {
275 276
  (void)deadline;

277 278 279
  // Determine the stream parameters. Note that we rely on peek_si to
  // validate that we have a buffer that does not wrap around the top
  // of the heap.
280
  if (!ctx->si.h) {
281
    int is_intra_only = 0;
282
    const vpx_codec_err_t res =
283 284
        decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
                                 ctx->decrypt_cb, ctx->decrypt_state);
285
    if (res != VPX_CODEC_OK) return res;
286

287
    if (!ctx->si.is_kf && !is_intra_only) return VPX_CODEC_ERROR;
288
  }
289

290
  ctx->user_priv = user_priv;
291

292 293 294 295
  // Set these even if already initialized.  The caller may have changed the
  // decrypt config between frames.
  ctx->pbi->decrypt_cb = ctx->decrypt_cb;
  ctx->pbi->decrypt_state = ctx->decrypt_state;
296

297 298 299 300 301
  if (vp9_receive_compressed_data(ctx->pbi, data_sz, data)) {
    ctx->pbi->cur_buf->buf.corrupted = 1;
    ctx->pbi->need_resync = 1;
    ctx->need_resync = 1;
    return update_error_state(ctx, &ctx->pbi->common.error);
302
  }
303

304 305
  check_resync(ctx, ctx->pbi);

306
  return VPX_CODEC_OK;
307 308
}

309 310 311
static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
                                      const uint8_t *data, unsigned int data_sz,
                                      void *user_priv, long deadline) {
312
  const uint8_t *data_start = data;
313
  const uint8_t *const data_end = data + data_sz;
314
  vpx_codec_err_t res;
315 316
  uint32_t frame_sizes[8];
  int frame_count;
John Koleszar's avatar
John Koleszar committed
317

318 319 320 321 322 323 324
  if (data == NULL && data_sz == 0) {
    ctx->flushed = 1;
    return VPX_CODEC_OK;
  }

  // Reset flushed when receiving a valid frame.
  ctx->flushed = 0;
325

326 327
  // Initialize the decoder on the first frame.
  if (ctx->pbi == NULL) {
328
    const vpx_codec_err_t res = init_decoder(ctx);
329
    if (res != VPX_CODEC_OK) return res;
330 331
  }

332 333
  res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
                                   ctx->decrypt_cb, ctx->decrypt_state);
334
  if (res != VPX_CODEC_OK) return res;
335

336 337 338
  if (ctx->svc_decoding && ctx->svc_spatial_layer < frame_count - 1)
    frame_count = ctx->svc_spatial_layer + 1;

339 340 341
  // Decode in serial mode.
  if (frame_count > 0) {
    int i;
342

343 344 345 346 347 348 349
    for (i = 0; i < frame_count; ++i) {
      const uint8_t *data_start_copy = data_start;
      const uint32_t frame_size = frame_sizes[i];
      vpx_codec_err_t res;
      if (data_start < data || frame_size > (uint32_t)(data_end - data_start)) {
        set_error_detail(ctx, "Invalid frame size in index");
        return VPX_CODEC_CORRUPT_FRAME;
350 351
      }

352
      res = decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
353
      if (res != VPX_CODEC_OK) return res;
354 355

      data_start += frame_size;
John Koleszar's avatar
John Koleszar committed
356
    }
357
  } else {
358 359 360 361 362
    while (data_start < data_end) {
      const uint32_t frame_size = (uint32_t)(data_end - data_start);
      const vpx_codec_err_t res =
          decode_one(ctx, &data_start, frame_size, user_priv, deadline);
      if (res != VPX_CODEC_OK) return res;
363

364
      // Account for suboptimal termination by the encoder.
365
      while (data_start < data_end) {
366 367 368 369
        const uint8_t marker =
            read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
        if (marker) break;
        ++data_start;
370
      }
371
    }
372
  }
373

374 375 376
  return res;
}

377 378
static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
                                      vpx_codec_iter_t *iter) {
John Koleszar's avatar
John Koleszar committed
379 380
  vpx_image_t *img = NULL;

381 382 383 384 385
  // Legacy parameter carried over from VP8. Has no effect for VP9 since we
  // always return only 1 frame per decode call.
  (void)iter;

  if (ctx->pbi != NULL) {
386 387 388
    YV12_BUFFER_CONFIG sd;
    vp9_ppflags_t flags = { 0, 0, 0 };
    if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC) set_ppflags(ctx, &flags);
389 390 391 392 393 394 395 396 397
    if (vp9_get_raw_frame(ctx->pbi, &sd, &flags) == 0) {
      VP9_COMMON *const cm = &ctx->pbi->common;
      RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
      ctx->last_show_frame = ctx->pbi->common.new_fb_idx;
      if (ctx->need_resync) return NULL;
      yuvconfig2image(&ctx->img, &sd, ctx->user_priv);
      ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
      img = &ctx->img;
      return img;
398
    }
399
  }
400
  return NULL;
John Koleszar's avatar
John Koleszar committed
401 402
}

403
static vpx_codec_err_t decoder_set_fb_fn(
404
    vpx_codec_alg_priv_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get,
405 406 407
    vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
  if (cb_get == NULL || cb_release == NULL) {
    return VPX_CODEC_INVALID_PARAM;
408
  } else if (ctx->pbi == NULL) {
409 410 411 412 413 414 415 416 417 418 419
    // If the decoder has already been initialized, do not accept changes to
    // the frame buffer functions.
    ctx->get_ext_fb_cb = cb_get;
    ctx->release_ext_fb_cb = cb_release;
    ctx->ext_priv = cb_priv;
    return VPX_CODEC_OK;
  }

  return VPX_CODEC_ERROR;
}

420
static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
421
                                          va_list args) {
422
  vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
John Koleszar's avatar
John Koleszar committed
423

John Koleszar's avatar
John Koleszar committed
424
  if (data) {
425
    vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
John Koleszar's avatar
John Koleszar committed
426 427
    YV12_BUFFER_CONFIG sd;
    image2yuvconfig(&frame->img, &sd);
428 429
    return vp9_set_reference_dec(
        &ctx->pbi->common, ref_frame_to_vp9_reframe(frame->frame_type), &sd);
430
  } else {
John Koleszar's avatar
John Koleszar committed
431
    return VPX_CODEC_INVALID_PARAM;
432
  }
John Koleszar's avatar
John Koleszar committed
433 434
}

435
static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
436
                                           va_list args) {
John Koleszar's avatar
John Koleszar committed
437
  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
John Koleszar's avatar
John Koleszar committed
438

John Koleszar's avatar
John Koleszar committed
439
  if (data) {
440
    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
John Koleszar's avatar
John Koleszar committed
441 442
    YV12_BUFFER_CONFIG sd;
    image2yuvconfig(&frame->img, &sd);
443 444
    return vp9_copy_reference_dec(ctx->pbi, (VP9_REFFRAME)frame->frame_type,
                                  &sd);
445
  } else {
John Koleszar's avatar
John Koleszar committed
446
    return VPX_CODEC_INVALID_PARAM;
447
  }
John Koleszar's avatar
John Koleszar committed
448 449
}

450
static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
451
                                          va_list args) {
452 453 454
  vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);

  if (data) {
455
    YV12_BUFFER_CONFIG *fb;
456
    fb = get_ref_frame(&ctx->pbi->common, data->idx);
457
    if (fb == NULL) return VPX_CODEC_ERROR;
458
    yuvconfig2image(&data->img, fb, NULL);
459 460 461 462 463 464
    return VPX_CODEC_OK;
  } else {
    return VPX_CODEC_INVALID_PARAM;
  }
}

465
static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
466
                                         va_list args) {
467
#if CONFIG_VP9_POSTPROC
John Koleszar's avatar
John Koleszar committed
468
  vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
John Koleszar's avatar
John Koleszar committed
469

John Koleszar's avatar
John Koleszar committed
470 471 472 473
  if (data) {
    ctx->postproc_cfg_set = 1;
    ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
    return VPX_CODEC_OK;
474
  } else {
John Koleszar's avatar
John Koleszar committed
475
    return VPX_CODEC_INVALID_PARAM;
476
  }
John Koleszar's avatar
John Koleszar committed
477
#else
478 479
  (void)ctx;
  (void)args;
John Koleszar's avatar
John Koleszar committed
480
  return VPX_CODEC_INCAPABLE;
John Koleszar's avatar
John Koleszar committed
481 482 483
#endif
}

484 485 486
static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
                                          va_list args) {
  int *const arg = va_arg(args, int *);
487 488
  if (arg == NULL || ctx->pbi == NULL) return VPX_CODEC_INVALID_PARAM;
  *arg = ctx->pbi->common.base_qindex;
489 490 491
  return VPX_CODEC_OK;
}

492
static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
493
                                                 va_list args) {
494
  int *const update_info = va_arg(args, int *);
495

John Koleszar's avatar
John Koleszar committed
496
  if (update_info) {
497 498
    if (ctx->pbi != NULL) {
      *update_info = ctx->pbi->refresh_frame_flags;
499
      return VPX_CODEC_OK;
500
    } else {
501
      return VPX_CODEC_ERROR;
502
    }
503
  }
504 505

  return VPX_CODEC_INVALID_PARAM;
506 507
}

508
static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
509
                                                va_list args) {
John Koleszar's avatar
John Koleszar committed
510
  int *corrupted = va_arg(args, int *);
511

512
  if (corrupted) {
513 514 515
    if (ctx->pbi != NULL) {
      RefCntBuffer *const frame_bufs = ctx->pbi->common.buffer_pool->frame_bufs;
      if (ctx->pbi->common.frame_to_show == NULL) return VPX_CODEC_ERROR;
516 517
      if (ctx->last_show_frame >= 0)
        *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
518
      return VPX_CODEC_OK;
519 520 521
    } else {
      return VPX_CODEC_ERROR;
    }
522
  }
523 524

  return VPX_CODEC_INVALID_PARAM;
525 526
}

527 528 529 530 531
static vpx_codec_err_t ctrl_get_frame_size(vpx_codec_alg_priv_t *ctx,
                                           va_list args) {
  int *const frame_size = va_arg(args, int *);

  if (frame_size) {
532 533
    if (ctx->pbi != NULL) {
      const VP9_COMMON *const cm = &ctx->pbi->common;
534 535 536 537 538 539 540
      frame_size[0] = cm->width;
      frame_size[1] = cm->height;
      return VPX_CODEC_OK;
    } else {
      return VPX_CODEC_ERROR;
    }
  }
541 542

  return VPX_CODEC_INVALID_PARAM;
543 544
}

545 546 547
static vpx_codec_err_t ctrl_get_render_size(vpx_codec_alg_priv_t *ctx,
                                            va_list args) {
  int *const render_size = va_arg(args, int *);
548

549
  if (render_size) {
550 551
    if (ctx->pbi != NULL) {
      const VP9_COMMON *const cm = &ctx->pbi->common;
552 553
      render_size[0] = cm->render_width;
      render_size[1] = cm->render_height;
554
      return VPX_CODEC_OK;
555 556 557 558
    } else {
      return VPX_CODEC_ERROR;
    }
  }
559 560

  return VPX_CODEC_INVALID_PARAM;
561 562
}

563 564 565 566 567
static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
                                          va_list args) {
  unsigned int *const bit_depth = va_arg(args, unsigned int *);

  if (bit_depth) {
568 569
    if (ctx->pbi != NULL) {
      const VP9_COMMON *const cm = &ctx->pbi->common;
570 571 572 573 574 575
      *bit_depth = cm->bit_depth;
      return VPX_CODEC_OK;
    } else {
      return VPX_CODEC_ERROR;
    }
  }
576 577

  return VPX_CODEC_INVALID_PARAM;
578 579
}

580
static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
581
                                                  va_list args) {
582 583 584 585
  ctx->invert_tile_order = va_arg(args, int);
  return VPX_CODEC_OK;
}

586 587 588 589 590 591 592 593
static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
                                          va_list args) {
  vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
  ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
  ctx->decrypt_state = init ? init->decrypt_state : NULL;
  return VPX_CODEC_OK;
}

594 595 596 597 598 599 600 601 602 603 604 605 606 607
static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
                                               va_list args) {
  const int legacy_byte_alignment = 0;
  const int min_byte_alignment = 32;
  const int max_byte_alignment = 1024;
  const int byte_alignment = va_arg(args, int);

  if (byte_alignment != legacy_byte_alignment &&
      (byte_alignment < min_byte_alignment ||
       byte_alignment > max_byte_alignment ||
       (byte_alignment & (byte_alignment - 1)) != 0))
    return VPX_CODEC_INVALID_PARAM;

  ctx->byte_alignment = byte_alignment;
608 609
  if (ctx->pbi != NULL) {
    ctx->pbi->common.byte_alignment = byte_alignment;
610 611 612 613
  }
  return VPX_CODEC_OK;
}

614 615 616 617
static vpx_codec_err_t ctrl_set_skip_loop_filter(vpx_codec_alg_priv_t *ctx,
                                                 va_list args) {
  ctx->skip_loop_filter = va_arg(args, int);

618 619
  if (ctx->pbi != NULL) {
    ctx->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
620 621 622 623 624
  }

  return VPX_CODEC_OK;
}

625 626 627 628 629 630 631 632 633 634
static vpx_codec_err_t ctrl_set_spatial_layer_svc(vpx_codec_alg_priv_t *ctx,
                                                  va_list args) {
  ctx->svc_decoding = 1;
  ctx->svc_spatial_layer = va_arg(args, int);
  if (ctx->svc_spatial_layer < 0)
    return VPX_CODEC_INVALID_PARAM;
  else
    return VPX_CODEC_OK;
}

635
static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
636
  { VP8_COPY_REFERENCE, ctrl_copy_reference },
637 638

  // Setters
639 640 641 642 643 644
  { VP8_SET_REFERENCE, ctrl_set_reference },
  { VP8_SET_POSTPROC, ctrl_set_postproc },
  { VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
  { VPXD_SET_DECRYPTOR, ctrl_set_decryptor },
  { VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
  { VP9_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
645
  { VP9_DECODE_SVC_SPATIAL_LAYER, ctrl_set_spatial_layer_svc },
646 647

  // Getters
648
  { VPXD_GET_LAST_QUANTIZER, ctrl_get_quantizer },
649 650 651 652 653 654 655 656
  { VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
  { VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
  { VP9_GET_REFERENCE, ctrl_get_reference },
  { VP9D_GET_DISPLAY_SIZE, ctrl_get_render_size },
  { VP9D_GET_BIT_DEPTH, ctrl_get_bit_depth },
  { VP9D_GET_FRAME_SIZE, ctrl_get_frame_size },

  { -1, NULL },
John Koleszar's avatar
John Koleszar committed
657 658 659 660 661
};

#ifndef VERSION_STRING
#define VERSION_STRING
#endif
662 663
CODEC_INTERFACE(vpx_codec_vp9_dx) = {
  "WebM Project VP9 Decoder" VERSION_STRING,
John Koleszar's avatar
John Koleszar committed
664
  VPX_CODEC_INTERNAL_ABI_VERSION,
665 666 667 668
#if CONFIG_VP9_HIGHBITDEPTH
  VPX_CODEC_CAP_HIGHBITDEPTH |
#endif
      VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
669
      VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER,  // vpx_codec_caps_t
670 671 672 673 674 675 676 677 678 679
  decoder_init,                             // vpx_codec_init_fn_t
  decoder_destroy,                          // vpx_codec_destroy_fn_t
  decoder_ctrl_maps,                        // vpx_codec_ctrl_fn_map_t
  {
      // NOLINT
      decoder_peek_si,    // vpx_codec_peek_si_fn_t
      decoder_get_si,     // vpx_codec_get_si_fn_t
      decoder_decode,     // vpx_codec_decode_fn_t
      decoder_get_frame,  // vpx_codec_frame_get_fn_t
      decoder_set_fb_fn,  // vpx_codec_set_fb_fn_t
John Koleszar's avatar
John Koleszar committed
680
  },
681 682 683 684 685 686 687 688 689 690
  {
      // NOLINT
      0,
      NULL,  // vpx_codec_enc_cfg_map_t
      NULL,  // vpx_codec_encode_fn_t
      NULL,  // vpx_codec_get_cx_data_fn_t
      NULL,  // vpx_codec_enc_config_set_fn_t
      NULL,  // vpx_codec_get_global_headers_fn_t
      NULL,  // vpx_codec_get_preview_frame_fn_t
      NULL   // vpx_codec_enc_mr_get_mem_loc_fn_t
John Koleszar's avatar
John Koleszar committed
691
  }
John Koleszar's avatar
John Koleszar committed
692
};