vp9_decodeframe.c 74.2 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
#include <assert.h>
12
#include <stdlib.h>  // qsort()
John Koleszar's avatar
John Koleszar committed
13

14
#include "./vp9_rtcd.h"
15 16
#include "./vpx_scale_rtcd.h"

17
#include "vpx_mem/vpx_mem.h"
18
#include "vpx_ports/mem.h"
19
#include "vpx_ports/mem_ops.h"
20
#include "vpx_scale/vpx_scale.h"
Jingning Han's avatar
Jingning Han committed
21
#include "vpx_util/vpx_thread.h"
22

Dmitry Kovalev's avatar
Dmitry Kovalev committed
23
#include "vp9/common/vp9_alloccommon.h"
Ronald S. Bultje's avatar
Ronald S. Bultje committed
24
#include "vp9/common/vp9_common.h"
Yaowu Xu's avatar
Yaowu Xu committed
25
#include "vp9/common/vp9_entropy.h"
26
#include "vp9/common/vp9_entropymode.h"
27
#include "vp9/common/vp9_idct.h"
28
#include "vp9/common/vp9_thread_common.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
29
#include "vp9/common/vp9_pred_common.h"
30
#include "vp9/common/vp9_quant_common.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
31 32
#include "vp9/common/vp9_reconintra.h"
#include "vp9/common/vp9_reconinter.h"
33 34
#include "vp9/common/vp9_seg_common.h"
#include "vp9/common/vp9_tile_common.h"
35

Yaowu Xu's avatar
Yaowu Xu committed
36
#include "vp9/decoder/vp9_decodeframe.h"
37 38
#include "vp9/decoder/vp9_detokenize.h"
#include "vp9/decoder/vp9_decodemv.h"
39
#include "vp9/decoder/vp9_decoder.h"
40
#include "vp9/decoder/vp9_dsubexp.h"
41
#include "vp9/decoder/vp9_read_bit_buffer.h"
42
#include "vp9/decoder/vp9_reader.h"
43

44 45
#define MAX_VP9_HEADER_SIZE 80

46
static int is_compound_reference_allowed(const VP9_COMMON *cm) {
47
  int i;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
48
  for (i = 1; i < REFS_PER_FRAME; ++i)
49
    if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1])
50 51 52 53 54
      return 1;

  return 0;
}

55
static void setup_compound_reference_mode(VP9_COMMON *cm) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  if (cm->ref_frame_sign_bias[LAST_FRAME] ==
          cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
    cm->comp_fixed_ref = ALTREF_FRAME;
    cm->comp_var_ref[0] = LAST_FRAME;
    cm->comp_var_ref[1] = GOLDEN_FRAME;
  } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
                 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
    cm->comp_fixed_ref = GOLDEN_FRAME;
    cm->comp_var_ref[0] = LAST_FRAME;
    cm->comp_var_ref[1] = ALTREF_FRAME;
  } else {
    cm->comp_fixed_ref = LAST_FRAME;
    cm->comp_var_ref[0] = GOLDEN_FRAME;
    cm->comp_var_ref[1] = ALTREF_FRAME;
  }
}

73
static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
Johann's avatar
Johann committed
74
  return len != 0 && len <= (size_t)(end - start);
75 76
}

77 78 79 80 81
static int decode_unsigned_max(struct vp9_read_bit_buffer *rb, int max) {
  const int data = vp9_rb_read_literal(rb, get_unsigned_bits(max));
  return data > max ? max : data;
}

82 83 84 85 86
static TX_MODE read_tx_mode(vp9_reader *r) {
  TX_MODE tx_mode = vp9_read_literal(r, 2);
  if (tx_mode == ALLOW_32X32)
    tx_mode += vp9_read_bit(r);
  return tx_mode;
87 88
}

89
static void read_tx_mode_probs(struct tx_probs *tx_probs, vp9_reader *r) {
90 91 92
  int i, j;

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
93
    for (j = 0; j < TX_SIZES - 3; ++j)
94
      vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
95 96

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
97
    for (j = 0; j < TX_SIZES - 2; ++j)
98
      vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
99 100

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
101
    for (j = 0; j < TX_SIZES - 1; ++j)
102
      vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
John Koleszar's avatar
John Koleszar committed
103 104
}

105 106
static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vp9_reader *r) {
  int i, j;
107
  for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
108 109 110 111 112 113 114 115 116 117 118
    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
      vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
}

static void read_inter_mode_probs(FRAME_CONTEXT *fc, vp9_reader *r) {
  int i, j;
  for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
    for (j = 0; j < INTER_MODES - 1; ++j)
      vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
}

119 120
static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
                                                vp9_reader *r) {
121
  if (is_compound_reference_allowed(cm)) {
122 123 124
    return vp9_read_bit(r) ? (vp9_read_bit(r) ? REFERENCE_MODE_SELECT
                                              : COMPOUND_REFERENCE)
                           : SINGLE_REFERENCE;
125 126 127
  } else {
    return SINGLE_REFERENCE;
  }
128 129
}

130
static void read_frame_reference_mode_probs(VP9_COMMON *cm, vp9_reader *r) {
131
  FRAME_CONTEXT *const fc = cm->fc;
132
  int i;
133

134
  if (cm->reference_mode == REFERENCE_MODE_SELECT)
135 136
    for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
      vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
137

138
  if (cm->reference_mode != COMPOUND_REFERENCE)
139 140 141
    for (i = 0; i < REF_CONTEXTS; ++i) {
      vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
      vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
142 143
    }

144
  if (cm->reference_mode != SINGLE_REFERENCE)
145 146
    for (i = 0; i < REF_CONTEXTS; ++i)
      vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
147 148
}

149 150 151
static void update_mv_probs(vp9_prob *p, int n, vp9_reader *r) {
  int i;
  for (i = 0; i < n; ++i)
152
    if (vp9_read(r, MV_UPDATE_PROB))
Dmitry Kovalev's avatar
Dmitry Kovalev committed
153
      p[i] = (vp9_read_literal(r, 7) << 1) | 1;
154 155
}

156 157
static void read_mv_probs(nmv_context *ctx, int allow_hp, vp9_reader *r) {
  int i, j;
158

159
  update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
160 161

  for (i = 0; i < 2; ++i) {
162 163 164 165 166
    nmv_component *const comp_ctx = &ctx->comps[i];
    update_mv_probs(&comp_ctx->sign, 1, r);
    update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
    update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
    update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
167 168 169
  }

  for (i = 0; i < 2; ++i) {
170
    nmv_component *const comp_ctx = &ctx->comps[i];
171
    for (j = 0; j < CLASS0_SIZE; ++j)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
172
      update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
173
    update_mv_probs(comp_ctx->fp, 3, r);
174 175 176 177
  }

  if (allow_hp) {
    for (i = 0; i < 2; ++i) {
178 179 180
      nmv_component *const comp_ctx = &ctx->comps[i];
      update_mv_probs(&comp_ctx->class0_hp, 1, r);
      update_mv_probs(&comp_ctx->hp, 1, r);
181 182 183 184
    }
  }
}

185
static void inverse_transform_block(MACROBLOCKD* xd, int plane, int block,
186 187
                                    TX_SIZE tx_size, uint8_t *dst, int stride,
                                    int eob) {
188
  struct macroblockd_plane *const pd = &xd->plane[plane];
189
  if (eob > 0) {
190
    TX_TYPE tx_type = DCT_DCT;
191
    tran_low_t *const dqcoeff = pd->dqcoeff;
192 193 194 195
#if CONFIG_VP9_HIGHBITDEPTH
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      if (xd->lossless) {
        tx_type = DCT_DCT;
196
        vp9_highbd_iwht4x4_add(dqcoeff, dst, stride, eob, xd->bd);
197 198 199 200 201
      } else {
        const PLANE_TYPE plane_type = pd->plane_type;
        switch (tx_size) {
          case TX_4X4:
            tx_type = get_tx_type_4x4(plane_type, xd, block);
202
            vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
203 204 205
            break;
          case TX_8X8:
            tx_type = get_tx_type(plane_type, xd);
206
            vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
207 208 209
            break;
          case TX_16X16:
            tx_type = get_tx_type(plane_type, xd);
210
            vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
211 212 213
            break;
          case TX_32X32:
            tx_type = DCT_DCT;
214
            vp9_highbd_idct32x32_add(dqcoeff, dst, stride, eob, xd->bd);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            break;
          default:
            assert(0 && "Invalid transform size");
        }
      }
    } else {
      if (xd->lossless) {
        tx_type = DCT_DCT;
        vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
      } else {
        const PLANE_TYPE plane_type = pd->plane_type;
        switch (tx_size) {
          case TX_4X4:
            tx_type = get_tx_type_4x4(plane_type, xd, block);
            vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob);
            break;
          case TX_8X8:
            tx_type = get_tx_type(plane_type, xd);
            vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob);
            break;
          case TX_16X16:
            tx_type = get_tx_type(plane_type, xd);
            vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
            break;
          case TX_32X32:
            tx_type = DCT_DCT;
            vp9_idct32x32_add(dqcoeff, dst, stride, eob);
            break;
          default:
            assert(0 && "Invalid transform size");
            return;
        }
      }
    }
#else
250 251 252 253 254 255 256 257
    if (xd->lossless) {
      tx_type = DCT_DCT;
      vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
    } else {
      const PLANE_TYPE plane_type = pd->plane_type;
      switch (tx_size) {
        case TX_4X4:
          tx_type = get_tx_type_4x4(plane_type, xd, block);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
258
          vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
          break;
        case TX_8X8:
          tx_type = get_tx_type(plane_type, xd);
          vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob);
          break;
        case TX_16X16:
          tx_type = get_tx_type(plane_type, xd);
          vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
          break;
        case TX_32X32:
          tx_type = DCT_DCT;
          vp9_idct32x32_add(dqcoeff, dst, stride, eob);
          break;
        default:
          assert(0 && "Invalid transform size");
274
          return;
275
      }
276
    }
277
#endif  // CONFIG_VP9_HIGHBITDEPTH
278 279

    if (eob == 1) {
James Zern's avatar
James Zern committed
280
      memset(dqcoeff, 0, 2 * sizeof(dqcoeff[0]));
281
    } else {
282
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
James Zern's avatar
James Zern committed
283
        memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
284
      else if (tx_size == TX_32X32 && eob <= 34)
James Zern's avatar
James Zern committed
285
        memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
286
      else
James Zern's avatar
James Zern committed
287
        memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
288
    }
289 290 291
  }
}

292 293 294
struct intra_args {
  MACROBLOCKD *xd;
  vp9_reader *r;
295
  int seg_id;
296 297 298 299 300
};

static void predict_and_reconstruct_intra_block(int plane, int block,
                                                BLOCK_SIZE plane_bsize,
                                                TX_SIZE tx_size, void *arg) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
301
  struct intra_args *const args = (struct intra_args *)arg;
302
  MACROBLOCKD *const xd = args->xd;
303
  struct macroblockd_plane *const pd = &xd->plane[plane];
304
  MODE_INFO *const mi = xd->mi[0];
305 306
  const PREDICTION_MODE mode = (plane == 0) ? get_y_mode(mi, block)
                                            : mi->mbmi.uv_mode;
307 308 309 310
  int x, y;
  uint8_t *dst;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &x, &y);
  dst = &pd->dst.buf[4 * y * pd->dst.stride + 4 * x];
311

312
  vp9_predict_intra_block(xd, block >> (tx_size << 1),
313
                          b_width_log2_lookup[plane_bsize], tx_size, mode,
314 315
                          dst, pd->dst.stride, dst, pd->dst.stride,
                          x, y, plane);
316

317
  if (!mi->mbmi.skip) {
318
    const int eob = vp9_decode_block_tokens(xd, plane, block,
319
                                            plane_bsize, x, y, tx_size,
320
                                            args->r, args->seg_id);
321 322
    inverse_transform_block(xd, plane, block, tx_size, dst, pd->dst.stride,
                            eob);
323
  }
324 325
}

326 327 328 329
struct inter_args {
  MACROBLOCKD *xd;
  vp9_reader *r;
  int *eobtotal;
330
  int seg_id;
331 332 333 334 335
};

static void reconstruct_inter_block(int plane, int block,
                                    BLOCK_SIZE plane_bsize,
                                    TX_SIZE tx_size, void *arg) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
336
  struct inter_args *args = (struct inter_args *)arg;
337
  MACROBLOCKD *const xd = args->xd;
338
  struct macroblockd_plane *const pd = &xd->plane[plane];
339
  int x, y, eob;
340
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &x, &y);
341
  eob = vp9_decode_block_tokens(xd, plane, block, plane_bsize,
342
                                x, y, tx_size, args->r, args->seg_id);
343 344
  inverse_transform_block(xd, plane, block, tx_size,
                          &pd->dst.buf[4 * y * pd->dst.stride + 4 * x],
345 346
                          pd->dst.stride, eob);
  *args->eobtotal += eob;
347 348
}

349 350 351 352 353
static void build_mc_border(const uint8_t *src, int src_stride,
                            uint8_t *dst, int dst_stride,
                            int x, int y, int b_w, int b_h, int w, int h) {
  // Get a pointer to the start of the real data for this row.
  const uint8_t *ref_row = src - x - y * src_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
354

355 356 357 358
  if (y >= h)
    ref_row += (h - 1) * src_stride;
  else if (y > 0)
    ref_row += y * src_stride;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
359

360 361 362
  do {
    int right = 0, copy;
    int left = x < 0 ? -x : 0;
John Koleszar's avatar
John Koleszar committed
363

364 365
    if (left > b_w)
      left = b_w;
366

367 368
    if (x + b_w > w)
      right = x + b_w - w;
369

370 371
    if (right > b_w)
      right = b_w;
372

373
    copy = b_w - left - right;
374

375 376
    if (left)
      memset(dst, ref_row[0], left);
377

378 379
    if (copy)
      memcpy(dst + left, ref_row + x + left, copy);
380

381 382
    if (right)
      memset(dst + left + copy, ref_row[w - 1], right);
383

384 385
    dst += dst_stride;
    ++y;
386

387 388 389
    if (y > 0 && y < h)
      ref_row += src_stride;
  } while (--b_h);
390 391
}

392 393 394 395 396 397 398 399
#if CONFIG_VP9_HIGHBITDEPTH
static void high_build_mc_border(const uint8_t *src8, int src_stride,
                                 uint16_t *dst, int dst_stride,
                                 int x, int y, int b_w, int b_h,
                                 int w, int h) {
  // Get a pointer to the start of the real data for this row.
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  const uint16_t *ref_row = src - x - y * src_stride;
400

401 402 403 404
  if (y >= h)
    ref_row += (h - 1) * src_stride;
  else if (y > 0)
    ref_row += y * src_stride;
405

406 407 408
  do {
    int right = 0, copy;
    int left = x < 0 ? -x : 0;
409

410 411
    if (left > b_w)
      left = b_w;
412

413 414
    if (x + b_w > w)
      right = x + b_w - w;
415

416 417
    if (right > b_w)
      right = b_w;
418

419
    copy = b_w - left - right;
420

421 422
    if (left)
      vpx_memset16(dst, ref_row[0], left);
423

424 425
    if (copy)
      memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
John Koleszar's avatar
John Koleszar committed
426

427 428
    if (right)
      vpx_memset16(dst + left + copy, ref_row[w - 1], right);
John Koleszar's avatar
John Koleszar committed
429

430 431
    dst += dst_stride;
    ++y;
432

433 434 435
    if (y > 0 && y < h)
      ref_row += src_stride;
  } while (--b_h);
436
}
437
#endif  // CONFIG_VP9_HIGHBITDEPTH
438

439 440 441 442 443 444 445 446 447 448 449 450 451
#if CONFIG_VP9_HIGHBITDEPTH
static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
                               int x0, int y0, int b_w, int b_h,
                               int frame_width, int frame_height,
                               int border_offset,
                               uint8_t *const dst, int dst_buf_stride,
                               int subpel_x, int subpel_y,
                               const InterpKernel *kernel,
                               const struct scale_factors *sf,
                               MACROBLOCKD *xd,
                               int w, int h, int ref, int xs, int ys) {
  DECLARE_ALIGNED(16, uint16_t, mc_buf_high[80 * 2 * 80 * 2]);
  const uint8_t *buf_ptr;
452

453 454 455 456 457 458 459 460 461
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w,
                         x0, y0, b_w, b_h, frame_width, frame_height);
    buf_ptr = CONVERT_TO_BYTEPTR(mc_buf_high) + border_offset;
  } else {
    build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w,
                    x0, y0, b_w, b_h, frame_width, frame_height);
    buf_ptr = ((uint8_t *)mc_buf_high) + border_offset;
  }
462

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    high_inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
                         subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
  } else {
    inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
                    subpel_y, sf, w, h, ref, kernel, xs, ys);
  }
}
#else
static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
                               int x0, int y0, int b_w, int b_h,
                               int frame_width, int frame_height,
                               int border_offset,
                               uint8_t *const dst, int dst_buf_stride,
                               int subpel_x, int subpel_y,
                               const InterpKernel *kernel,
                               const struct scale_factors *sf,
                               int w, int h, int ref, int xs, int ys) {
  DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
  const uint8_t *buf_ptr;
483

484 485 486
  build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w,
                  x0, y0, b_w, b_h, frame_width, frame_height);
  buf_ptr = mc_buf + border_offset;
487

488 489 490 491
  inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
                  subpel_y, sf, w, h, ref, kernel, xs, ys);
}
#endif  // CONFIG_VP9_HIGHBITDEPTH
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
static void dec_build_inter_predictors(VP9Decoder *const pbi, MACROBLOCKD *xd,
                                       int plane, int bw, int bh, int x,
                                       int y, int w, int h, int mi_x, int mi_y,
                                       const InterpKernel *kernel,
                                       const struct scale_factors *sf,
                                       struct buf_2d *pre_buf,
                                       struct buf_2d *dst_buf, const MV* mv,
                                       RefCntBuffer *ref_frame_buf,
                                       int is_scaled, int ref) {
  struct macroblockd_plane *const pd = &xd->plane[plane];
  uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
  MV32 scaled_mv;
  int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height,
      buf_stride, subpel_x, subpel_y;
  uint8_t *ref_frame, *buf_ptr;

  // Get reference frame pointer, width and height.
  if (plane == 0) {
    frame_width = ref_frame_buf->buf.y_crop_width;
    frame_height = ref_frame_buf->buf.y_crop_height;
    ref_frame = ref_frame_buf->buf.y_buffer;
  } else {
    frame_width = ref_frame_buf->buf.uv_crop_width;
    frame_height = ref_frame_buf->buf.uv_crop_height;
    ref_frame = plane == 1 ? ref_frame_buf->buf.u_buffer
                         : ref_frame_buf->buf.v_buffer;
519
  }
520

521 522 523 524 525 526 527
  if (is_scaled) {
    const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, mv, bw, bh,
                                               pd->subsampling_x,
                                               pd->subsampling_y);
    // Co-ordinate of containing block to pixel precision.
    int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
    int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
528

529 530 531
    // Co-ordinate of the block to 1/16th pixel precision.
    x0_16 = (x_start + x) << SUBPEL_BITS;
    y0_16 = (y_start + y) << SUBPEL_BITS;
532

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    // Co-ordinate of current block in reference frame
    // to 1/16th pixel precision.
    x0_16 = sf->scale_value_x(x0_16, sf);
    y0_16 = sf->scale_value_y(y0_16, sf);

    // Map the top left corner of the block into the reference frame.
    x0 = sf->scale_value_x(x_start + x, sf);
    y0 = sf->scale_value_y(y_start + y, sf);

    // Scale the MV and incorporate the sub-pixel offset of the block
    // in the reference frame.
    scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
    xs = sf->x_step_q4;
    ys = sf->y_step_q4;
  } else {
    // Co-ordinate of containing block to pixel precision.
    x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
    y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;

    // Co-ordinate of the block to 1/16th pixel precision.
    x0_16 = x0 << SUBPEL_BITS;
    y0_16 = y0 << SUBPEL_BITS;

    scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
    scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
    xs = ys = 16;
559
  }
560 561
  subpel_x = scaled_mv.col & SUBPEL_MASK;
  subpel_y = scaled_mv.row & SUBPEL_MASK;
562

563 564 565 566 567 568
  // Calculate the top left corner of the best matching block in the
  // reference frame.
  x0 += scaled_mv.col >> SUBPEL_BITS;
  y0 += scaled_mv.row >> SUBPEL_BITS;
  x0_16 += scaled_mv.col;
  y0_16 += scaled_mv.row;
569

570 571 572
  // Get reference block pointer.
  buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
  buf_stride = pre_buf->stride;
573

574 575 576 577
  // Do border extension if there is motion or the
  // width/height is not a multiple of 8 pixels.
  if (is_scaled || scaled_mv.col || scaled_mv.row ||
      (frame_width & 0x7) || (frame_height & 0x7)) {
578
    int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
579

580
    // Get reference block bottom right horizontal coordinate.
581
    int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
582
    int x_pad = 0, y_pad = 0;
583

584 585 586 587
    if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
      x0 -= VP9_INTERP_EXTEND - 1;
      x1 += VP9_INTERP_EXTEND;
      x_pad = 1;
588 589
    }

590 591 592 593 594
    if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
      y0 -= VP9_INTERP_EXTEND - 1;
      y1 += VP9_INTERP_EXTEND;
      y_pad = 1;
    }
595

596 597 598 599 600 601 602 603 604 605 606 607 608 609
    // Wait until reference block is ready. Pad 7 more pixels as last 7
    // pixels of each superblock row can be changed by next superblock row.
    if (pbi->frame_parallel_decode)
      vp9_frameworker_wait(pbi->frame_worker_owner, ref_frame_buf,
                           MAX(0, (y1 + 7)) << (plane == 0 ? 0 : 1));

    // Skip border extension if block is inside the frame.
    if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
        y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
      // Extend the border.
      const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
      const int b_w = x1 - x0 + 1;
      const int b_h = y1 - y0 + 1;
      const int border_offset = y_pad * 3 * b_w + x_pad * 3;
610

611 612 613 614 615
      extend_and_predict(buf_ptr1, buf_stride, x0, y0, b_w, b_h,
                         frame_width, frame_height, border_offset,
                         dst, dst_buf->stride,
                         subpel_x, subpel_y,
                         kernel, sf,
616
#if CONFIG_VP9_HIGHBITDEPTH
617
                         xd,
618
#endif
619 620
                         w, h, ref, xs, ys);
      return;
621 622
    }
  } else {
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    // Wait until reference block is ready. Pad 7 more pixels as last 7
    // pixels of each superblock row can be changed by next superblock row.
     if (pbi->frame_parallel_decode) {
       const int y1 = (y0_16 + (h - 1) * ys) >> SUBPEL_BITS;
       vp9_frameworker_wait(pbi->frame_worker_owner, ref_frame_buf,
                            MAX(0, (y1 + 7)) << (plane == 0 ? 0 : 1));
     }
  }
#if CONFIG_VP9_HIGHBITDEPTH
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    high_inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
                         subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
  } else {
    inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
                    subpel_y, sf, w, h, ref, kernel, xs, ys);
638
  }
639 640 641 642
#else
  inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
                  subpel_y, sf, w, h, ref, kernel, xs, ys);
#endif  // CONFIG_VP9_HIGHBITDEPTH
643 644
}

645 646 647 648 649 650 651 652
static void dec_build_inter_predictors_sb(VP9Decoder *const pbi,
                                          MACROBLOCKD *xd,
                                          int mi_row, int mi_col,
                                          BLOCK_SIZE bsize) {
  int plane;
  const int mi_x = mi_col * MI_SIZE;
  const int mi_y = mi_row * MI_SIZE;
  const MODE_INFO *mi = xd->mi[0];
James Zern's avatar
James Zern committed
653
  const InterpKernel *kernel = vp9_filter_kernels[mi->mbmi.interp_filter];
654 655
  const BLOCK_SIZE sb_type = mi->mbmi.sb_type;
  const int is_compound = has_second_ref(&mi->mbmi);
656

657 658 659 660 661 662 663
  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize,
                                                        &xd->plane[plane]);
    struct macroblockd_plane *const pd = &xd->plane[plane];
    struct buf_2d *const dst_buf = &pd->dst;
    const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
    const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
664

665 666 667
    const int bw = 4 * num_4x4_w;
    const int bh = 4 * num_4x4_h;
    int ref;
668

669 670 671 672 673 674 675
    for (ref = 0; ref < 1 + is_compound; ++ref) {
      const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
      struct buf_2d *const pre_buf = &pd->pre[ref];
      const int idx = xd->block_refs[ref]->idx;
      BufferPool *const pool = pbi->common.buffer_pool;
      RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
      const int is_scaled = vp9_is_scaled(sf);
676

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
      if (sb_type < BLOCK_8X8) {
        int i = 0, x, y;
        assert(bsize == BLOCK_8X8);
        for (y = 0; y < num_4x4_h; ++y) {
          for (x = 0; x < num_4x4_w; ++x) {
            const MV mv = average_split_mvs(pd, mi, ref, i++);
            dec_build_inter_predictors(pbi, xd, plane, bw, bh,
                                       4 * x, 4 * y, 4, 4, mi_x, mi_y, kernel,
                                       sf, pre_buf, dst_buf, &mv,
                                       ref_frame_buf, is_scaled, ref);
          }
        }
      } else {
        const MV mv = mi->mbmi.mv[ref].as_mv;
        dec_build_inter_predictors(pbi, xd, plane, bw, bh,
                                   0, 0, bw, bh, mi_x, mi_y, kernel,
                                   sf, pre_buf, dst_buf, &mv, ref_frame_buf,
                                   is_scaled, ref);
      }
696
    }
697
  }
Adrian Grange's avatar
Adrian Grange committed
698 699
}

700 701 702 703 704 705 706 707
static MB_MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
                                 BLOCK_SIZE bsize, int mi_row, int mi_col) {
  const int bw = num_8x8_blocks_wide_lookup[bsize];
  const int bh = num_8x8_blocks_high_lookup[bsize];
  const int x_mis = MIN(bw, cm->mi_cols - mi_col);
  const int y_mis = MIN(bh, cm->mi_rows - mi_row);
  const int offset = mi_row * cm->mi_stride + mi_col;
  int x, y;
Scott LaVarnway's avatar
Scott LaVarnway committed
708
  const TileInfo *const tile = &xd->tile;
709

710 711 712 713 714 715 716
  xd->mi = cm->mi_grid_visible + offset;
  xd->mi[0] = &cm->mi[offset];
  xd->mi[0]->mbmi.sb_type = bsize;
  for (y = 0; y < y_mis; ++y)
    for (x = !y; x < x_mis; ++x) {
      xd->mi[y * cm->mi_stride + x] = xd->mi[0];
    }
717

718
  set_skip_context(xd, mi_row, mi_col);
719

720 721 722 723 724 725
  // Distance of Mb to the various image edges. These are specified to 8th pel
  // as they are always compared to values that are in 1/8th pel units
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);

  vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
  return &xd->mi[0]->mbmi;
726 727
}

728 729 730 731 732
static void decode_block(VP9Decoder *const pbi, MACROBLOCKD *const xd,
                         int mi_row, int mi_col,
                         vp9_reader *r, BLOCK_SIZE bsize) {
  VP9_COMMON *const cm = &pbi->common;
  const int less8x8 = bsize < BLOCK_8X8;
Scott LaVarnway's avatar
Scott LaVarnway committed
733
  MB_MODE_INFO *mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col);
734 735 736 737 738 739 740

  if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
    const BLOCK_SIZE uv_subsize =
        ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
    if (uv_subsize == BLOCK_INVALID)
      vpx_internal_error(xd->error_info,
                         VPX_CODEC_CORRUPT_FRAME, "Invalid block size.");
741 742
  }

Scott LaVarnway's avatar
Scott LaVarnway committed
743
  vp9_read_mode_info(pbi, xd, mi_row, mi_col, r);
744

745 746
  if (less8x8)
    bsize = BLOCK_8X8;
747

748 749
  if (mbmi->skip) {
    reset_skip_context(xd, bsize);
750
  }
751

752 753 754 755 756 757 758
  if (!is_inter_block(mbmi)) {
    struct intra_args arg = {xd, r, mbmi->segment_id};
    vp9_foreach_transformed_block(xd, bsize,
                                  predict_and_reconstruct_intra_block, &arg);
  } else {
    // Prediction
    dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col, bsize);
Adrian Grange's avatar
Adrian Grange committed
759

760 761 762 763 764 765 766 767
    // Reconstruction
    if (!mbmi->skip) {
      int eobtotal = 0;
      struct inter_args arg = {xd, r, &eobtotal, mbmi->segment_id};
      vp9_foreach_transformed_block(xd, bsize, reconstruct_inter_block, &arg);
      if (!less8x8 && eobtotal == 0)
        mbmi->skip = 1;  // skip loopfilter
    }
Adrian Grange's avatar
Adrian Grange committed
768
  }
769

770
  xd->corrupted |= vp9_reader_has_error(r);
771 772
}

773 774 775
static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
                                     BLOCK_SIZE bsize, vp9_reader *r,
                                     int has_rows, int has_cols) {
776
  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
777
  const vp9_prob *const probs = get_partition_probs(xd, ctx);
778 779
  FRAME_COUNTS *counts = xd->counts;
  PARTITION_TYPE p;
James Zern's avatar
James Zern committed
780

781 782 783 784 785 786 787 788
  if (has_rows && has_cols)
    p = (PARTITION_TYPE)vp9_read_tree(r, vp9_partition_tree, probs);
  else if (!has_rows && has_cols)
    p = vp9_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
  else if (has_rows && !has_cols)
    p = vp9_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
  else
    p = PARTITION_SPLIT;
789

790 791
  if (counts)
    ++counts->partition[ctx][p];
792

793 794
  return p;
}
James Zern's avatar
James Zern committed
795

796 797 798 799 800 801 802
static void decode_partition(VP9Decoder *const pbi, MACROBLOCKD *const xd,
                             int mi_row, int mi_col,
                             vp9_reader* r, BLOCK_SIZE bsize) {
  VP9_COMMON *const cm = &pbi->common;
  const int hbs = num_8x8_blocks_wide_lookup[bsize] / 2;
  PARTITION_TYPE partition;
  BLOCK_SIZE subsize;
803 804
  const int has_rows = (mi_row + hbs) < cm->mi_rows;
  const int has_cols = (mi_col + hbs) < cm->mi_cols;
James Zern's avatar
James Zern committed
805

806 807
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
    return;
Johann's avatar
Johann committed
808

809
  partition = read_partition(xd, mi_row, mi_col, bsize, r, has_rows, has_cols);
810 811
  subsize = get_subsize(bsize, partition);
  if (bsize == BLOCK_8X8) {
Scott LaVarnway's avatar
Scott LaVarnway committed
812
    decode_block(pbi, xd, mi_row, mi_col, r, subsize);
James Zern's avatar
James Zern committed
813
  } else {
814 815
    switch (partition) {
      case PARTITION_NONE:
Scott LaVarnway's avatar
Scott LaVarnway committed
816
        decode_block(pbi, xd, mi_row, mi_col, r, subsize);
817 818
        break;
      case PARTITION_HORZ:
Scott LaVarnway's avatar
Scott LaVarnway committed
819
        decode_block(pbi, xd, mi_row, mi_col, r, subsize);
820
        if (has_rows)
Scott LaVarnway's avatar
Scott LaVarnway committed
821
          decode_block(pbi, xd, mi_row + hbs, mi_col, r, subsize);
822 823
        break;
      case PARTITION_VERT:
Scott LaVarnway's avatar
Scott LaVarnway committed
824
        decode_block(pbi, xd, mi_row, mi_col, r, subsize);
825
        if (has_cols)
Scott LaVarnway's avatar
Scott LaVarnway committed
826
          decode_block(pbi, xd, mi_row, mi_col + hbs, r, subsize);
827 828
        break;
      case PARTITION_SPLIT:
Scott LaVarnway's avatar
Scott LaVarnway committed
829 830 831 832
        decode_partition(pbi, xd, mi_row, mi_col, r, subsize);
        decode_partition(pbi, xd, mi_row, mi_col + hbs, r, subsize);
        decode_partition(pbi, xd, mi_row + hbs, mi_col, r, subsize);
        decode_partition(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize);
833 834 835 836
        break;
      default:
        assert(0 && "Invalid partition type");
    }
James Zern's avatar
James Zern committed
837
  }
838

839 840 841 842 843
  // update partition context
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
    update_partition_context(xd, mi_row, mi_col, subsize, bsize);
}
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
static void setup_token_decoder(const uint8_t *data,
                                const uint8_t *data_end,
                                size_t read_size,
                                struct vpx_internal_error_info *error_info,
                                vp9_reader *r,
                                vpx_decrypt_cb decrypt_cb,
                                void *decrypt_state) {
  // Validate the calculated partition length. If the buffer
  // described by the partition can't be fully read, then restrict
  // it to the portion that can be (for EC mode) or throw an error.
  if (!read_is_valid(data, read_size, data_end))
    vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
                       "Truncated packet or corrupt tile length");

  if (vp9_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
    vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate bool decoder %d", 1);
James Zern's avatar
James Zern committed
862 863
}

864 865 866
static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
                                   vp9_reader *r) {
  int i, j, k, l, m;
867

868 869 870 871 872 873 874
  if (vp9_read_bit(r))
    for (i = 0; i < PLANE_TYPES; ++i)
      for (j = 0; j < REF_TYPES; ++j)
        for (k = 0; k < COEF_BANDS; ++k)
          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
            for (m = 0; m < UNCONSTRAINED_NODES; ++m)
              vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
875
}
876

877 878 879 880 881 882 883
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
                            vp9_reader *r) {
    const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
    TX_SIZE tx_size;
    for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
      read_coef_probs_common(fc->coef_probs[tx_size], r);
}
884

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
static void setup_segmentation(struct segmentation *seg,
                               struct vp9_read_bit_buffer *rb) {
  int i, j;

  seg->update_map = 0;
  seg->update_data = 0;

  seg->enabled = vp9_rb_read_bit(rb);
  if (!seg->enabled)
    return;

  // Segmentation map update
  seg->update_map = vp9_rb_read_bit(rb);
  if (seg->update_map) {
    for (i = 0; i < SEG_TREE_PROBS; i++)
      seg->tree_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
                                               : MAX_PROB;

    seg->temporal_update = vp9_rb_read_bit(rb);
    if (seg->temporal_update) {
      for (i = 0; i < PREDICTION_PROBS; i++)
        seg->pred_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
                                                 : MAX_PROB;
    } else {
      for (i = 0; i < PREDICTION_PROBS; i++)
        seg->pred_probs[i] = MAX_PROB;
911 912 913
    }
  }

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  // Segmentation data update
  seg->update_data = vp9_rb_read_bit(rb);
  if (seg->update_data) {
    seg->abs_delta = vp9_rb_read_bit(rb);

    vp9_clearall_segfeatures(seg);

    for (i = 0; i < MAX_SEGMENTS; i++) {
      for (j = 0; j < SEG_LVL_MAX; j++) {
        int data = 0;
        const int feature_enabled = vp9_rb_read_bit(rb);
        if (feature_enabled) {
          vp9_enable_segfeature(seg, i, j);
          data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
          if (vp9_is_segfeature_signed(j))
            data = vp9_rb_read_bit(rb) ? -data : data;
        }
        vp9_set_segdata(seg, i, j, data);
      }
    }
934
  }
935
}
936

937 938 939 940
static void setup_loopfilter(struct loopfilter *lf,
                             struct vp9_read_bit_buffer *rb) {
  lf->filter_level = vp9_rb_read_literal(rb, 6);
  lf->sharpness_level = vp9_rb_read_literal(rb, 3);
941

942 943 944
  // Read in loop filter deltas applied at the MB level based on mode or ref
  // frame.
  lf->mode_ref_delta_update = 0;
945

946 947 948 949 950
  lf->mode_ref_delta_enabled = vp9_rb_read_bit(rb);
  if (lf->mode_ref_delta_enabled) {
    lf->mode_ref_delta_update = vp9_rb_read_bit(rb);
    if (lf->mode_ref_delta_update) {
      int i;
951

952 953 954
      for (i = 0; i < MAX_REF_LF_DELTAS; i++)
        if (vp9_rb_read_bit(rb))
          lf->ref_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
955

956 957 958
      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
        if (vp9_rb_read_bit(rb))
          lf->mode_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
959 960
    }
  }
961
}
962

963 964 965
static INLINE int read_delta_q(struct vp9_read_bit_buffer *rb) {
  return vp9_rb_read_bit(rb) ? vp9_rb_read_signed_literal(rb, 4) : 0;
}
966