vp9_decodframe.c 38.5 KB
Newer Older
John Koleszar's avatar
John Koleszar committed

    data += 3;

Yaowu Xu's avatar
Yaowu Xu committed

John Koleszar's avatar
John Koleszar committed
    if (pc->frame_type == KEY_FRAME) {
      // When error concealment is enabled we should only check the sync
      // code if we have enough bits available
John Koleszar's avatar
John Koleszar committed
      if (data + 3 < data_end) {
        if (data[0] != 0x9d || data[1] != 0x01 || data[2] != 0x2a)
          vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
                             "Invalid frame sync code");
      }
    data = setup_frame_size(pbi, scaling_active, data, data_end);
John Koleszar's avatar
John Koleszar committed
  }
John Koleszar's avatar
John Koleszar committed
  if ((!pbi->decoded_key_frame && pc->frame_type != KEY_FRAME) ||
      pc->width == 0 || pc->height == 0) {
John Koleszar's avatar
John Koleszar committed
    return -1;
  }
John Koleszar's avatar
John Koleszar committed
  init_frame(pbi);
  // Reset the frame pointers to the current frame size
  vp8_yv12_realloc_frame_buffer(new_fb, pc->width, pc->height,
                                VP9BORDERINPIXELS);

  if (vp9_reader_init(&header_bc, data, first_partition_size))
John Koleszar's avatar
John Koleszar committed
    vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate bool decoder 0");
  pc->clr_type = (YUV_TYPE)vp9_read_bit(&header_bc);
  pc->clamp_type = (CLAMP_TYPE)vp9_read_bit(&header_bc);
  pc->error_resilient_mode = vp9_read_bit(&header_bc);
  xd->lossless = vp9_read_bit(&header_bc);
  if (xd->lossless) {
    xd->inv_txm4x4_1      = vp9_short_iwalsh4x4_1;
    xd->inv_txm4x4        = vp9_short_iwalsh4x4;
    xd->itxm_add          = vp9_idct_add_lossless_c;
    xd->itxm_add_y_block  = vp9_idct_add_y_block_lossless_c;
    xd->itxm_add_uv_block = vp9_idct_add_uv_block_lossless_c;
  } else {
    xd->inv_txm4x4_1      = vp9_short_idct4x4_1;
    xd->inv_txm4x4        = vp9_short_idct4x4;
    xd->itxm_add          = vp9_idct_add;
    xd->itxm_add_y_block  = vp9_idct_add_y_block;
    xd->itxm_add_uv_block = vp9_idct_add_uv_block;
  }
  setup_loopfilter(pc, xd, &header_bc);
John Koleszar's avatar
John Koleszar committed

  setup_quantization(pbi, &header_bc);
John Koleszar's avatar
John Koleszar committed

  // Determine if the golden frame or ARF buffer should be updated and how.
  // For all non key frames the GF and ARF refresh flags and sign bias
  // flags must be set explicitly.
  if (pc->frame_type == KEY_FRAME) {
    for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i)
      pc->active_ref_idx[i] = pc->new_fb_idx;
    // Should the GF or ARF be updated from the current frame
    pbi->refresh_frame_flags = vp9_read_literal(&header_bc, NUM_REF_FRAMES);
John Koleszar's avatar
John Koleszar committed

    // Select active reference frames and calculate scaling factors
    for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
      const int ref = vp9_read_literal(&header_bc, NUM_REF_FRAMES_LG2);
      const int mapped_ref = pc->ref_frame_map[ref];
      YV12_BUFFER_CONFIG *const fb = &pc->yv12_fb[mapped_ref];
      struct scale_factors *const sf = &pc->active_ref_scale[i];

      pc->active_ref_idx[i] = mapped_ref;
      if (mapped_ref >= NUM_YV12_BUFFERS)
        memset(sf, 0, sizeof(*sf));
      else
        vp9_setup_scale_factors_for_frame(sf, fb, pc->width, pc->height);
    // Read the sign bias for each reference frame buffer.
    for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
      pc->ref_frame_sign_bias[i + 1] = vp9_read_bit(&header_bc);
    }

    xd->allow_high_precision_mv = vp9_read_bit(&header_bc);
    pc->mcomp_filter_type = read_mcomp_filter_type(&header_bc);
    // To enable choice of different interpolation filters
    vp9_setup_interp_filters(xd, pc->mcomp_filter_type, pc);
John Koleszar's avatar
John Koleszar committed
  }

  if (!pc->error_resilient_mode) {
    pc->refresh_frame_context = vp9_read_bit(&header_bc);
    pc->frame_parallel_decoding_mode = vp9_read_bit(&header_bc);
  } else {
    pc->refresh_frame_context = 0;
    pc->frame_parallel_decoding_mode = 1;
  }
  pc->frame_context_idx = vp9_read_literal(&header_bc, NUM_FRAME_CONTEXTS_LG2);
  pc->fc = pc->frame_contexts[pc->frame_context_idx];
John Koleszar's avatar
John Koleszar committed

  setup_segmentation(pc, xd, &header_bc);

  setup_pred_probs(pc, &header_bc);

  setup_txfm_mode(pc, xd->lossless, &header_bc);
  // Read inter mode probability context updates
  if (pc->frame_type != KEY_FRAME) {
    int i, j;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
    for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
      for (j = 0; j < 4; ++j)
        if (vp9_read(&header_bc, 252))
          pc->fc.vp9_mode_contexts[i][j] = vp9_read_prob(&header_bc);
#if CONFIG_MODELCOEFPROB
  if (pc->frame_type == KEY_FRAME)
    vp9_default_coef_probs(pc);
  update_frame_context(&pc->fc);
#if CONFIG_CODE_ZEROGROUP
  read_zpc_probs(pc, &header_bc);
Yaowu Xu's avatar
Yaowu Xu committed

  // Initialize xd pointers. Any reference should do for xd->pre, so use 0.
  setup_pre_planes(xd, &pc->yv12_fb[pc->active_ref_idx[0]], NULL,
                   0, 0, NULL, NULL);
  setup_dst_planes(xd, new_fb, 0, 0);
John Koleszar's avatar
John Koleszar committed
  // Create the segmentation map structure and set to 0
  if (!pc->last_frame_seg_map)
    CHECK_MEM_ERROR(pc->last_frame_seg_map,
                    vpx_calloc((pc->mi_rows * pc->mi_cols), 1));
  vp9_setup_block_dptrs(xd);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
  // clear out the coeff buffer
  for (i = 0; i < MAX_MB_PLANE; ++i)
    vp9_zero(xd->plane[i].qcoeff);
  vp9_decode_mode_mvs_init(pbi, &header_bc);
  decode_tiles(pbi, data, first_partition_size, &header_bc, &residual_bc);
John Koleszar's avatar
John Koleszar committed

  pc->last_width = pc->width;
  pc->last_height = pc->height;
  new_fb->corrupted = vp9_reader_has_error(&header_bc) | xd->corrupted;
John Koleszar's avatar
John Koleszar committed

  if (!pbi->decoded_key_frame) {
    if (pc->frame_type == KEY_FRAME && !new_fb->corrupted)
John Koleszar's avatar
John Koleszar committed
      pbi->decoded_key_frame = 1;
    else
      vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
John Koleszar's avatar
John Koleszar committed
                         "A stream must start with a complete key frame");
  }
  if (!pc->error_resilient_mode && !pc->frame_parallel_decoding_mode) {
    vp9_adapt_coef_probs(pc);
#if CONFIG_CODE_ZEROGROUP
    vp9_adapt_zpc_probs(pc);
    if (pc->frame_type != KEY_FRAME) {
      vp9_adapt_mode_probs(pc);
      vp9_adapt_nmv_probs(pc, xd->allow_high_precision_mv);
John Koleszar's avatar
John Koleszar committed
  }
#if CONFIG_IMPLICIT_SEGMENTATION
  // If signalled at the frame level apply implicit updates to the segment map.
  if (!pc->error_resilient_mode && xd->allow_implicit_segment_update) {
    vp9_implicit_segment_map_update(pc);
  }
#endif

  if (pc->refresh_frame_context)
    pc->frame_contexts[pc->frame_context_idx] = pc->fc;
  *p_data_end = vp9_reader_find_end(&residual_bc);
John Koleszar's avatar
John Koleszar committed
  return 0;
John Koleszar's avatar
John Koleszar committed
}