Coverage Report

Created: 2024-02-11 06:14

/src/libvpx/vp9/decoder/vp9_decodeframe.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include <assert.h>
12
#include <stdlib.h>  // qsort()
13
14
#include "./vp9_rtcd.h"
15
#include "./vpx_dsp_rtcd.h"
16
#include "./vpx_scale_rtcd.h"
17
18
#include "vpx_dsp/bitreader_buffer.h"
19
#include "vpx_dsp/bitreader.h"
20
#include "vpx_dsp/vpx_dsp_common.h"
21
#include "vpx_mem/vpx_mem.h"
22
#include "vpx_ports/mem.h"
23
#include "vpx_ports/mem_ops.h"
24
#include "vpx_scale/vpx_scale.h"
25
#include "vpx_util/vpx_thread.h"
26
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
27
#include "vpx_util/vpx_debug_util.h"
28
#endif  // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
29
30
#include "vp9/common/vp9_alloccommon.h"
31
#include "vp9/common/vp9_common.h"
32
#include "vp9/common/vp9_entropy.h"
33
#include "vp9/common/vp9_entropymode.h"
34
#include "vp9/common/vp9_idct.h"
35
#include "vp9/common/vp9_thread_common.h"
36
#include "vp9/common/vp9_pred_common.h"
37
#include "vp9/common/vp9_quant_common.h"
38
#include "vp9/common/vp9_reconintra.h"
39
#include "vp9/common/vp9_reconinter.h"
40
#include "vp9/common/vp9_seg_common.h"
41
#include "vp9/common/vp9_tile_common.h"
42
43
#include "vp9/decoder/vp9_decodeframe.h"
44
#include "vp9/decoder/vp9_detokenize.h"
45
#include "vp9/decoder/vp9_decodemv.h"
46
#include "vp9/decoder/vp9_decoder.h"
47
#include "vp9/decoder/vp9_dsubexp.h"
48
#include "vp9/decoder/vp9_job_queue.h"
49
50
#define MAX_VP9_HEADER_SIZE 80
51
52
typedef int (*predict_recon_func)(TileWorkerData *twd, MODE_INFO *const mi,
53
                                  int plane, int row, int col, TX_SIZE tx_size);
54
55
typedef void (*intra_recon_func)(TileWorkerData *twd, MODE_INFO *const mi,
56
                                 int plane, int row, int col, TX_SIZE tx_size);
57
58
298k
static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
59
298k
  return len != 0 && len <= (size_t)(end - start);
60
298k
}
61
62
79.6k
static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) {
63
79.6k
  const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max));
64
79.6k
  return data > max ? max : data;
65
79.6k
}
66
67
87.3k
static TX_MODE read_tx_mode(vpx_reader *r) {
68
87.3k
  TX_MODE tx_mode = vpx_read_literal(r, 2);
69
87.3k
  if (tx_mode == ALLOW_32X32) tx_mode += vpx_read_bit(r);
70
87.3k
  return tx_mode;
71
87.3k
}
72
73
8.42k
static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) {
74
8.42k
  int i, j;
75
76
25.2k
  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
77
33.7k
    for (j = 0; j < TX_SIZES - 3; ++j)
78
16.8k
      vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
79
80
25.2k
  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
81
50.5k
    for (j = 0; j < TX_SIZES - 2; ++j)
82
33.7k
      vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
83
84
25.2k
  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
85
67.4k
    for (j = 0; j < TX_SIZES - 1; ++j)
86
50.5k
      vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
87
8.42k
}
88
89
13.1k
static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
90
13.1k
  int i, j;
91
65.9k
  for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
92
158k
    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
93
105k
      vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
94
13.1k
}
95
96
63.4k
static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
97
63.4k
  int i, j;
98
507k
  for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
99
1.77M
    for (j = 0; j < INTER_MODES - 1; ++j)
100
1.33M
      vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
101
63.4k
}
102
103
static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
104
63.4k
                                                vpx_reader *r) {
105
63.4k
  if (vp9_compound_reference_allowed(cm)) {
106
55.1k
    return vpx_read_bit(r)
107
55.1k
               ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT : COMPOUND_REFERENCE)
108
55.1k
               : SINGLE_REFERENCE;
109
55.1k
  } else {
110
8.35k
    return SINGLE_REFERENCE;
111
8.35k
  }
112
63.4k
}
113
114
63.4k
static void read_frame_reference_mode_probs(VP9_COMMON *cm, vpx_reader *r) {
115
63.4k
  FRAME_CONTEXT *const fc = cm->fc;
116
63.4k
  int i;
117
118
63.4k
  if (cm->reference_mode == REFERENCE_MODE_SELECT)
119
50.7k
    for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
120
42.2k
      vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
121
122
63.4k
  if (cm->reference_mode != COMPOUND_REFERENCE)
123
338k
    for (i = 0; i < REF_CONTEXTS; ++i) {
124
281k
      vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
125
281k
      vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
126
281k
    }
127
128
63.4k
  if (cm->reference_mode != SINGLE_REFERENCE)
129
93.1k
    for (i = 0; i < REF_CONTEXTS; ++i)
130
77.6k
      vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
131
63.4k
}
132
133
1.13M
static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) {
134
1.13M
  int i;
135
5.45M
  for (i = 0; i < n; ++i)
136
4.31M
    if (vpx_read(r, MV_UPDATE_PROB)) p[i] = (vpx_read_literal(r, 7) << 1) | 1;
137
1.13M
}
138
139
63.4k
static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) {
140
63.4k
  int i, j;
141
142
63.4k
  update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
143
144
190k
  for (i = 0; i < 2; ++i) {
145
126k
    nmv_component *const comp_ctx = &ctx->comps[i];
146
126k
    update_mv_probs(&comp_ctx->sign, 1, r);
147
126k
    update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
148
126k
    update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
149
126k
    update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
150
126k
  }
151
152
190k
  for (i = 0; i < 2; ++i) {
153
126k
    nmv_component *const comp_ctx = &ctx->comps[i];
154
380k
    for (j = 0; j < CLASS0_SIZE; ++j)
155
253k
      update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
156
126k
    update_mv_probs(comp_ctx->fp, 3, r);
157
126k
  }
158
159
63.4k
  if (allow_hp) {
160
140k
    for (i = 0; i < 2; ++i) {
161
93.6k
      nmv_component *const comp_ctx = &ctx->comps[i];
162
93.6k
      update_mv_probs(&comp_ctx->class0_hp, 1, r);
163
93.6k
      update_mv_probs(&comp_ctx->hp, 1, r);
164
93.6k
    }
165
46.8k
  }
166
63.4k
}
167
168
static void inverse_transform_block_inter(MACROBLOCKD *xd, int plane,
169
                                          const TX_SIZE tx_size, uint8_t *dst,
170
2.83M
                                          int stride, int eob) {
171
2.83M
  struct macroblockd_plane *const pd = &xd->plane[plane];
172
2.83M
  tran_low_t *const dqcoeff = pd->dqcoeff;
173
2.83M
  assert(eob > 0);
174
2.83M
#if CONFIG_VP9_HIGHBITDEPTH
175
2.83M
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
176
226k
    uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
177
226k
    if (xd->lossless) {
178
2.91k
      vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
179
223k
    } else {
180
223k
      switch (tx_size) {
181
136k
        case TX_4X4:
182
136k
          vp9_highbd_idct4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
183
136k
          break;
184
41.9k
        case TX_8X8:
185
41.9k
          vp9_highbd_idct8x8_add(dqcoeff, dst16, stride, eob, xd->bd);
186
41.9k
          break;
187
24.3k
        case TX_16X16:
188
24.3k
          vp9_highbd_idct16x16_add(dqcoeff, dst16, stride, eob, xd->bd);
189
24.3k
          break;
190
20.9k
        case TX_32X32:
191
20.9k
          vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd);
192
20.9k
          break;
193
0
        default: assert(0 && "Invalid transform size");
194
223k
      }
195
223k
    }
196
2.60M
  } else {
197
2.60M
    if (xd->lossless) {
198
3.02k
      vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
199
2.60M
    } else {
200
2.60M
      switch (tx_size) {
201
838k
        case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
202
800k
        case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
203
544k
        case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
204
423k
        case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
205
0
        default: assert(0 && "Invalid transform size"); return;
206
2.60M
      }
207
2.60M
    }
208
2.60M
  }
209
#else
210
  if (xd->lossless) {
211
    vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
212
  } else {
213
    switch (tx_size) {
214
      case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
215
      case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
216
      case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
217
      case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
218
      default: assert(0 && "Invalid transform size"); return;
219
    }
220
  }
221
#endif  // CONFIG_VP9_HIGHBITDEPTH
222
223
2.83M
  if (eob == 1) {
224
689k
    dqcoeff[0] = 0;
225
2.14M
  } else {
226
2.14M
    if (tx_size <= TX_16X16 && eob <= 10)
227
864k
      memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
228
1.28M
    else if (tx_size == TX_32X32 && eob <= 34)
229
173k
      memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
230
1.10M
    else
231
1.10M
      memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
232
2.14M
  }
233
2.83M
}
234
235
static void inverse_transform_block_intra(MACROBLOCKD *xd, int plane,
236
                                          const TX_TYPE tx_type,
237
                                          const TX_SIZE tx_size, uint8_t *dst,
238
3.33M
                                          int stride, int eob) {
239
3.33M
  struct macroblockd_plane *const pd = &xd->plane[plane];
240
3.33M
  tran_low_t *const dqcoeff = pd->dqcoeff;
241
3.33M
  assert(eob > 0);
242
3.33M
#if CONFIG_VP9_HIGHBITDEPTH
243
3.33M
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
244
1.90M
    uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
245
1.90M
    if (xd->lossless) {
246
456k
      vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
247
1.44M
    } else {
248
1.44M
      switch (tx_size) {
249
618k
        case TX_4X4:
250
618k
          vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
251
618k
          break;
252
297k
        case TX_8X8:
253
297k
          vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
254
297k
          break;
255
196k
        case TX_16X16:
256
196k
          vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
257
196k
          break;
258
334k
        case TX_32X32:
259
334k
          vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd);
260
334k
          break;
261
0
        default: assert(0 && "Invalid transform size");
262
1.44M
      }
263
1.44M
    }
264
1.90M
  } else {
265
1.43M
    if (xd->lossless) {
266
171k
      vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
267
1.25M
    } else {
268
1.25M
      switch (tx_size) {
269
397k
        case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
270
317k
        case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
271
238k
        case TX_16X16:
272
238k
          vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
273
238k
          break;
274
306k
        case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
275
0
        default: assert(0 && "Invalid transform size"); return;
276
1.25M
      }
277
1.25M
    }
278
1.43M
  }
279
#else
280
  if (xd->lossless) {
281
    vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
282
  } else {
283
    switch (tx_size) {
284
      case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
285
      case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
286
      case TX_16X16:
287
        vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
288
        break;
289
      case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
290
      default: assert(0 && "Invalid transform size"); return;
291
    }
292
  }
293
#endif  // CONFIG_VP9_HIGHBITDEPTH
294
295
3.32M
  if (eob == 1) {
296
697k
    dqcoeff[0] = 0;
297
2.63M
  } else {
298
2.63M
    if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
299
820k
      memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
300
1.81M
    else if (tx_size == TX_32X32 && eob <= 34)
301
345k
      memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
302
1.46M
    else
303
1.46M
      memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
304
2.63M
  }
305
3.32M
}
306
307
static void predict_and_reconstruct_intra_block(TileWorkerData *twd,
308
                                                MODE_INFO *const mi, int plane,
309
                                                int row, int col,
310
854M
                                                TX_SIZE tx_size) {
311
854M
  MACROBLOCKD *const xd = &twd->xd;
312
854M
  struct macroblockd_plane *const pd = &xd->plane[plane];
313
854M
  PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
314
854M
  uint8_t *dst;
315
854M
  dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
316
317
854M
  if (mi->sb_type < BLOCK_8X8)
318
8.01M
    if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
319
320
854M
  vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride,
321
854M
                          dst, pd->dst.stride, col, row, plane);
322
323
854M
  if (!mi->skip) {
324
178M
    const TX_TYPE tx_type =
325
178M
        (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
326
178M
    const ScanOrder *sc = (plane || xd->lossless)
327
178M
                              ? &vp9_default_scan_orders[tx_size]
328
178M
                              : &vp9_scan_orders[tx_size][tx_type];
329
178M
    const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
330
178M
                                            mi->segment_id);
331
178M
    if (eob > 0) {
332
3.33M
      inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
333
3.33M
                                    pd->dst.stride, eob);
334
3.33M
    }
335
178M
  }
336
854M
}
337
338
static void parse_intra_block_row_mt(TileWorkerData *twd, MODE_INFO *const mi,
339
                                     int plane, int row, int col,
340
0
                                     TX_SIZE tx_size) {
341
0
  MACROBLOCKD *const xd = &twd->xd;
342
0
  PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
343
344
0
  if (mi->sb_type < BLOCK_8X8)
345
0
    if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
346
347
0
  if (!mi->skip) {
348
0
    struct macroblockd_plane *const pd = &xd->plane[plane];
349
0
    const TX_TYPE tx_type =
350
0
        (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
351
0
    const ScanOrder *sc = (plane || xd->lossless)
352
0
                              ? &vp9_default_scan_orders[tx_size]
353
0
                              : &vp9_scan_orders[tx_size][tx_type];
354
0
    *pd->eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
355
0
                                       mi->segment_id);
356
    /* Keep the alignment to 16 */
357
0
    pd->dqcoeff += (16 << (tx_size << 1));
358
0
    pd->eob++;
359
0
  }
360
0
}
361
362
static void predict_and_reconstruct_intra_block_row_mt(TileWorkerData *twd,
363
                                                       MODE_INFO *const mi,
364
                                                       int plane, int row,
365
                                                       int col,
366
0
                                                       TX_SIZE tx_size) {
367
0
  MACROBLOCKD *const xd = &twd->xd;
368
0
  struct macroblockd_plane *const pd = &xd->plane[plane];
369
0
  PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
370
0
  uint8_t *dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
371
372
0
  if (mi->sb_type < BLOCK_8X8)
373
0
    if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
374
375
0
  vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride,
376
0
                          dst, pd->dst.stride, col, row, plane);
377
378
0
  if (!mi->skip) {
379
0
    const TX_TYPE tx_type =
380
0
        (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
381
0
    if (*pd->eob > 0) {
382
0
      inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
383
0
                                    pd->dst.stride, *pd->eob);
384
0
    }
385
    /* Keep the alignment to 16 */
386
0
    pd->dqcoeff += (16 << (tx_size << 1));
387
0
    pd->eob++;
388
0
  }
389
0
}
390
391
static int reconstruct_inter_block(TileWorkerData *twd, MODE_INFO *const mi,
392
                                   int plane, int row, int col, TX_SIZE tx_size,
393
7.97M
                                   int mi_row, int mi_col) {
394
7.97M
  MACROBLOCKD *const xd = &twd->xd;
395
7.97M
  struct macroblockd_plane *const pd = &xd->plane[plane];
396
7.97M
  const ScanOrder *sc = &vp9_default_scan_orders[tx_size];
397
7.97M
  const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
398
7.97M
                                          mi->segment_id);
399
7.97M
  uint8_t *dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
400
401
7.97M
  if (eob > 0) {
402
2.83M
    inverse_transform_block_inter(xd, plane, tx_size, dst, pd->dst.stride, eob);
403
2.83M
  }
404
#if CONFIG_MISMATCH_DEBUG
405
  {
406
    int pixel_c, pixel_r;
407
    int blk_w = 1 << (tx_size + TX_UNIT_SIZE_LOG2);
408
    int blk_h = 1 << (tx_size + TX_UNIT_SIZE_LOG2);
409
    mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, col, row,
410
                    pd->subsampling_x, pd->subsampling_y);
411
    mismatch_check_block_tx(dst, pd->dst.stride, plane, pixel_c, pixel_r, blk_w,
412
                            blk_h, xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
413
  }
414
#else
415
7.97M
  (void)mi_row;
416
7.97M
  (void)mi_col;
417
7.97M
#endif
418
7.97M
  return eob;
419
7.97M
}
420
421
static int parse_inter_block_row_mt(TileWorkerData *twd, MODE_INFO *const mi,
422
                                    int plane, int row, int col,
423
0
                                    TX_SIZE tx_size) {
424
0
  MACROBLOCKD *const xd = &twd->xd;
425
0
  struct macroblockd_plane *const pd = &xd->plane[plane];
426
0
  const ScanOrder *sc = &vp9_default_scan_orders[tx_size];
427
0
  const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
428
0
                                          mi->segment_id);
429
430
0
  *pd->eob = eob;
431
0
  pd->dqcoeff += (16 << (tx_size << 1));
432
0
  pd->eob++;
433
434
0
  return eob;
435
0
}
436
437
static int reconstruct_inter_block_row_mt(TileWorkerData *twd,
438
                                          MODE_INFO *const mi, int plane,
439
0
                                          int row, int col, TX_SIZE tx_size) {
440
0
  MACROBLOCKD *const xd = &twd->xd;
441
0
  struct macroblockd_plane *const pd = &xd->plane[plane];
442
0
  const int eob = *pd->eob;
443
444
0
  (void)mi;
445
0
  if (eob > 0) {
446
0
    inverse_transform_block_inter(
447
0
        xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
448
0
        pd->dst.stride, eob);
449
0
  }
450
0
  pd->dqcoeff += (16 << (tx_size << 1));
451
0
  pd->eob++;
452
453
0
  return eob;
454
0
}
455
456
static void build_mc_border(const uint8_t *src, int src_stride, uint8_t *dst,
457
                            int dst_stride, int x, int y, int b_w, int b_h,
458
3.43M
                            int w, int h) {
459
  // Get a pointer to the start of the real data for this row.
460
3.43M
  const uint8_t *ref_row = src - x - y * src_stride;
461
462
3.43M
  if (y >= h)
463
128k
    ref_row += (h - 1) * src_stride;
464
3.30M
  else if (y > 0)
465
2.33M
    ref_row += y * src_stride;
466
467
51.4M
  do {
468
51.4M
    int right = 0, copy;
469
51.4M
    int left = x < 0 ? -x : 0;
470
471
51.4M
    if (left > b_w) left = b_w;
472
473
51.4M
    if (x + b_w > w) right = x + b_w - w;
474
475
51.4M
    if (right > b_w) right = b_w;
476
477
51.4M
    copy = b_w - left - right;
478
479
51.4M
    if (left) memset(dst, ref_row[0], left);
480
481
51.4M
    if (copy) memcpy(dst + left, ref_row + x + left, copy);
482
483
51.4M
    if (right) memset(dst + left + copy, ref_row[w - 1], right);
484
485
51.4M
    dst += dst_stride;
486
51.4M
    ++y;
487
488
51.4M
    if (y > 0 && y < h) ref_row += src_stride;
489
51.4M
  } while (--b_h);
490
3.43M
}
491
492
#if CONFIG_VP9_HIGHBITDEPTH
493
static void high_build_mc_border(const uint8_t *src8, int src_stride,
494
                                 uint16_t *dst, int dst_stride, int x, int y,
495
1.47M
                                 int b_w, int b_h, int w, int h) {
496
  // Get a pointer to the start of the real data for this row.
497
1.47M
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
498
1.47M
  const uint16_t *ref_row = src - x - y * src_stride;
499
500
1.47M
  if (y >= h)
501
149k
    ref_row += (h - 1) * src_stride;
502
1.32M
  else if (y > 0)
503
564k
    ref_row += y * src_stride;
504
505
25.7M
  do {
506
25.7M
    int right = 0, copy;
507
25.7M
    int left = x < 0 ? -x : 0;
508
509
25.7M
    if (left > b_w) left = b_w;
510
511
25.7M
    if (x + b_w > w) right = x + b_w - w;
512
513
25.7M
    if (right > b_w) right = b_w;
514
515
25.7M
    copy = b_w - left - right;
516
517
25.7M
    if (left) vpx_memset16(dst, ref_row[0], left);
518
519
25.7M
    if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
520
521
25.7M
    if (right) vpx_memset16(dst + left + copy, ref_row[w - 1], right);
522
523
25.7M
    dst += dst_stride;
524
25.7M
    ++y;
525
526
25.7M
    if (y > 0 && y < h) ref_row += src_stride;
527
25.7M
  } while (--b_h);
528
1.47M
}
529
#endif  // CONFIG_VP9_HIGHBITDEPTH
530
531
#if CONFIG_VP9_HIGHBITDEPTH
532
static void extend_and_predict(TileWorkerData *twd, const uint8_t *buf_ptr1,
533
                               int pre_buf_stride, int x0, int y0, int b_w,
534
                               int b_h, int frame_width, int frame_height,
535
                               int border_offset, uint8_t *const dst,
536
                               int dst_buf_stride, int subpel_x, int subpel_y,
537
                               const InterpKernel *kernel,
538
                               const struct scale_factors *sf, MACROBLOCKD *xd,
539
4.91M
                               int w, int h, int ref, int xs, int ys) {
540
4.91M
  uint16_t *mc_buf_high = twd->extend_and_predict_buf;
541
4.91M
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
542
1.47M
    high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w, x0, y0,
543
1.47M
                         b_w, b_h, frame_width, frame_height);
544
1.47M
    highbd_inter_predictor(mc_buf_high + border_offset, b_w,
545
1.47M
                           CONVERT_TO_SHORTPTR(dst), dst_buf_stride, subpel_x,
546
1.47M
                           subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
547
3.43M
  } else {
548
3.43M
    build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w, x0,
549
3.43M
                    y0, b_w, b_h, frame_width, frame_height);
550
3.43M
    inter_predictor(((uint8_t *)mc_buf_high) + border_offset, b_w, dst,
551
3.43M
                    dst_buf_stride, subpel_x, subpel_y, sf, w, h, ref, kernel,
552
3.43M
                    xs, ys);
553
3.43M
  }
554
4.91M
}
555
#else
556
static void extend_and_predict(TileWorkerData *twd, const uint8_t *buf_ptr1,
557
                               int pre_buf_stride, int x0, int y0, int b_w,
558
                               int b_h, int frame_width, int frame_height,
559
                               int border_offset, uint8_t *const dst,
560
                               int dst_buf_stride, int subpel_x, int subpel_y,
561
                               const InterpKernel *kernel,
562
                               const struct scale_factors *sf, int w, int h,
563
                               int ref, int xs, int ys) {
564
  uint8_t *mc_buf = (uint8_t *)twd->extend_and_predict_buf;
565
  const uint8_t *buf_ptr;
566
567
  build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w, x0, y0, b_w, b_h,
568
                  frame_width, frame_height);
569
  buf_ptr = mc_buf + border_offset;
570
571
  inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x, subpel_y, sf, w,
572
                  h, ref, kernel, xs, ys);
573
}
574
#endif  // CONFIG_VP9_HIGHBITDEPTH
575
576
static void dec_build_inter_predictors(
577
    TileWorkerData *twd, MACROBLOCKD *xd, int plane, int bw, int bh, int x,
578
    int y, int w, int h, int mi_x, int mi_y, const InterpKernel *kernel,
579
    const struct scale_factors *sf, struct buf_2d *pre_buf,
580
    struct buf_2d *dst_buf, const MV *mv, RefCntBuffer *ref_frame_buf,
581
53.9M
    int is_scaled, int ref) {
582
53.9M
  struct macroblockd_plane *const pd = &xd->plane[plane];
583
53.9M
  uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
584
53.9M
  MV32 scaled_mv;
585
53.9M
  int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
586
53.9M
      subpel_x, subpel_y;
587
53.9M
  uint8_t *ref_frame, *buf_ptr;
588
589
  // Get reference frame pointer, width and height.
590
53.9M
  if (plane == 0) {
591
21.7M
    frame_width = ref_frame_buf->buf.y_crop_width;
592
21.7M
    frame_height = ref_frame_buf->buf.y_crop_height;
593
21.7M
    ref_frame = ref_frame_buf->buf.y_buffer;
594
32.2M
  } else {
595
32.2M
    frame_width = ref_frame_buf->buf.uv_crop_width;
596
32.2M
    frame_height = ref_frame_buf->buf.uv_crop_height;
597
32.2M
    ref_frame =
598
32.2M
        plane == 1 ? ref_frame_buf->buf.u_buffer : ref_frame_buf->buf.v_buffer;
599
32.2M
  }
600
601
53.9M
  if (is_scaled) {
602
6.50M
    const MV mv_q4 = clamp_mv_to_umv_border_sb(
603
6.50M
        xd, mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
604
    // Co-ordinate of containing block to pixel precision.
605
6.50M
    int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
606
6.50M
    int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
607
#if 0  // CONFIG_BETTER_HW_COMPATIBILITY
608
    assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
609
           xd->mi[0]->sb_type != BLOCK_8X4);
610
    assert(mv_q4.row == mv->row * (1 << (1 - pd->subsampling_y)) &&
611
           mv_q4.col == mv->col * (1 << (1 - pd->subsampling_x)));
612
#endif
613
    // Co-ordinate of the block to 1/16th pixel precision.
614
6.50M
    x0_16 = (x_start + x) << SUBPEL_BITS;
615
6.50M
    y0_16 = (y_start + y) << SUBPEL_BITS;
616
617
    // Co-ordinate of current block in reference frame
618
    // to 1/16th pixel precision.
619
6.50M
    x0_16 = sf->scale_value_x(x0_16, sf);
620
6.50M
    y0_16 = sf->scale_value_y(y0_16, sf);
621
622
    // Map the top left corner of the block into the reference frame.
623
6.50M
    x0 = sf->scale_value_x(x_start + x, sf);
624
6.50M
    y0 = sf->scale_value_y(y_start + y, sf);
625
626
    // Scale the MV and incorporate the sub-pixel offset of the block
627
    // in the reference frame.
628
6.50M
    scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
629
6.50M
    xs = sf->x_step_q4;
630
6.50M
    ys = sf->y_step_q4;
631
47.4M
  } else {
632
    // Co-ordinate of containing block to pixel precision.
633
47.4M
    x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
634
47.4M
    y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
635
636
    // Co-ordinate of the block to 1/16th pixel precision.
637
47.4M
    x0_16 = x0 << SUBPEL_BITS;
638
47.4M
    y0_16 = y0 << SUBPEL_BITS;
639
640
47.4M
    scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
641
47.4M
    scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
642
47.4M
    xs = ys = 16;
643
47.4M
  }
644
53.9M
  subpel_x = scaled_mv.col & SUBPEL_MASK;
645
53.9M
  subpel_y = scaled_mv.row & SUBPEL_MASK;
646
647
  // Calculate the top left corner of the best matching block in the
648
  // reference frame.
649
53.9M
  x0 += scaled_mv.col >> SUBPEL_BITS;
650
53.9M
  y0 += scaled_mv.row >> SUBPEL_BITS;
651
53.9M
  x0_16 += scaled_mv.col;
652
53.9M
  y0_16 += scaled_mv.row;
653
654
  // Get reference block pointer.
655
53.9M
  buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
656
53.9M
  buf_stride = pre_buf->stride;
657
658
  // Do border extension if there is motion or the
659
  // width/height is not a multiple of 8 pixels.
660
53.9M
  if (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
661
53.9M
      (frame_height & 0x7)) {
662
52.2M
    int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
663
664
    // Get reference block bottom right horizontal coordinate.
665
52.2M
    int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
666
52.2M
    int x_pad = 0, y_pad = 0;
667
668
52.2M
    if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
669
34.1M
      x0 -= VP9_INTERP_EXTEND - 1;
670
34.1M
      x1 += VP9_INTERP_EXTEND;
671
34.1M
      x_pad = 1;
672
34.1M
    }
673
674
52.2M
    if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
675
39.7M
      y0 -= VP9_INTERP_EXTEND - 1;
676
39.7M
      y1 += VP9_INTERP_EXTEND;
677
39.7M
      y_pad = 1;
678
39.7M
    }
679
680
    // Skip border extension if block is inside the frame.
681
52.2M
    if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
682
52.2M
        y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
683
      // Extend the border.
684
4.91M
      const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
685
4.91M
      const int b_w = x1 - x0 + 1;
686
4.91M
      const int b_h = y1 - y0 + 1;
687
4.91M
      const int border_offset = y_pad * 3 * b_w + x_pad * 3;
688
689
4.91M
      extend_and_predict(twd, buf_ptr1, buf_stride, x0, y0, b_w, b_h,
690
4.91M
                         frame_width, frame_height, border_offset, dst,
691
4.91M
                         dst_buf->stride, subpel_x, subpel_y, kernel, sf,
692
4.91M
#if CONFIG_VP9_HIGHBITDEPTH
693
4.91M
                         xd,
694
4.91M
#endif
695
4.91M
                         w, h, ref, xs, ys);
696
4.91M
      return;
697
4.91M
    }
698
52.2M
  }
699
49.0M
#if CONFIG_VP9_HIGHBITDEPTH
700
49.0M
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
701
2.30M
    highbd_inter_predictor(CONVERT_TO_SHORTPTR(buf_ptr), buf_stride,
702
2.30M
                           CONVERT_TO_SHORTPTR(dst), dst_buf->stride, subpel_x,
703
2.30M
                           subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
704
46.7M
  } else {
705
46.7M
    inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
706
46.7M
                    subpel_y, sf, w, h, ref, kernel, xs, ys);
707
46.7M
  }
708
#else
709
  inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x, subpel_y,
710
                  sf, w, h, ref, kernel, xs, ys);
711
#endif  // CONFIG_VP9_HIGHBITDEPTH
712
49.0M
}
713
714
static void dec_build_inter_predictors_sb(TileWorkerData *twd,
715
                                          VP9Decoder *const pbi,
716
                                          MACROBLOCKD *xd, int mi_row,
717
11.0M
                                          int mi_col) {
718
11.0M
  int plane;
719
11.0M
  const int mi_x = mi_col * MI_SIZE;
720
11.0M
  const int mi_y = mi_row * MI_SIZE;
721
11.0M
  const MODE_INFO *mi = xd->mi[0];
722
11.0M
  const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
723
11.0M
  const BLOCK_SIZE sb_type = mi->sb_type;
724
11.0M
  const int is_compound = has_second_ref(mi);
725
11.0M
  int ref;
726
11.0M
  int is_scaled;
727
728
27.0M
  for (ref = 0; ref < 1 + is_compound; ++ref) {
729
15.9M
    const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
730
15.9M
    RefBuffer *ref_buf = &pbi->common.frame_refs[frame - LAST_FRAME];
731
15.9M
    const struct scale_factors *const sf = &ref_buf->sf;
732
15.9M
    const int idx = ref_buf->idx;
733
15.9M
    BufferPool *const pool = pbi->common.buffer_pool;
734
15.9M
    RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
735
736
15.9M
    if (!vp9_is_valid_scale(sf))
737
16
      vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
738
16
                         "Reference frame has invalid dimensions");
739
740
15.9M
    is_scaled = vp9_is_scaled(sf);
741
15.9M
    vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
742
15.9M
                         is_scaled ? sf : NULL);
743
15.9M
    xd->block_refs[ref] = ref_buf;
744
745
15.9M
    if (sb_type < BLOCK_8X8) {
746
7.72M
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
747
5.79M
        struct macroblockd_plane *const pd = &xd->plane[plane];
748
5.79M
        struct buf_2d *const dst_buf = &pd->dst;
749
5.79M
        const int num_4x4_w = pd->n4_w;
750
5.79M
        const int num_4x4_h = pd->n4_h;
751
5.79M
        const int n4w_x4 = 4 * num_4x4_w;
752
5.79M
        const int n4h_x4 = 4 * num_4x4_h;
753
5.79M
        struct buf_2d *const pre_buf = &pd->pre[ref];
754
5.79M
        int i = 0, x, y;
755
13.6M
        for (y = 0; y < num_4x4_h; ++y) {
756
19.8M
          for (x = 0; x < num_4x4_w; ++x) {
757
11.9M
            const MV mv = average_split_mvs(pd, mi, ref, i++);
758
11.9M
            dec_build_inter_predictors(twd, xd, plane, n4w_x4, n4h_x4, 4 * x,
759
11.9M
                                       4 * y, 4, 4, mi_x, mi_y, kernel, sf,
760
11.9M
                                       pre_buf, dst_buf, &mv, ref_frame_buf,
761
11.9M
                                       is_scaled, ref);
762
11.9M
          }
763
7.85M
        }
764
5.79M
      }
765
14.0M
    } else {
766
14.0M
      const MV mv = mi->mv[ref].as_mv;
767
56.0M
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
768
42.0M
        struct macroblockd_plane *const pd = &xd->plane[plane];
769
42.0M
        struct buf_2d *const dst_buf = &pd->dst;
770
42.0M
        const int num_4x4_w = pd->n4_w;
771
42.0M
        const int num_4x4_h = pd->n4_h;
772
42.0M
        const int n4w_x4 = 4 * num_4x4_w;
773
42.0M
        const int n4h_x4 = 4 * num_4x4_h;
774
42.0M
        struct buf_2d *const pre_buf = &pd->pre[ref];
775
42.0M
        dec_build_inter_predictors(twd, xd, plane, n4w_x4, n4h_x4, 0, 0, n4w_x4,
776
42.0M
                                   n4h_x4, mi_x, mi_y, kernel, sf, pre_buf,
777
42.0M
                                   dst_buf, &mv, ref_frame_buf, is_scaled, ref);
778
42.0M
      }
779
14.0M
    }
780
15.9M
  }
781
11.0M
}
782
783
17.4M
static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
784
17.4M
  int i;
785
69.6M
  for (i = 0; i < MAX_MB_PLANE; i++) {
786
52.2M
    struct macroblockd_plane *const pd = &xd->plane[i];
787
52.2M
    memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
788
52.2M
    memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
789
52.2M
  }
790
17.4M
}
791
792
static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
793
22.1M
                         int bhl) {
794
22.1M
  int i;
795
88.6M
  for (i = 0; i < MAX_MB_PLANE; i++) {
796
66.4M
    xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
797
66.4M
    xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
798
66.4M
    xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
799
66.4M
    xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
800
66.4M
  }
801
22.1M
}
802
803
static MODE_INFO *set_offsets_recon(VP9_COMMON *const cm, MACROBLOCKD *const xd,
804
                                    int mi_row, int mi_col, int bw, int bh,
805
0
                                    int bwl, int bhl) {
806
0
  const int offset = mi_row * cm->mi_stride + mi_col;
807
0
  const TileInfo *const tile = &xd->tile;
808
0
  xd->mi = cm->mi_grid_visible + offset;
809
810
0
  set_plane_n4(xd, bw, bh, bwl, bhl);
811
812
0
  set_skip_context(xd, mi_row, mi_col);
813
814
  // Distance of Mb to the various image edges. These are specified to 8th pel
815
  // as they are always compared to values that are in 1/8th pel units
816
0
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
817
818
0
  vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
819
0
  return xd->mi[0];
820
0
}
821
822
static MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
823
                              BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
824
22.1M
                              int bh, int x_mis, int y_mis, int bwl, int bhl) {
825
22.1M
  const int offset = mi_row * cm->mi_stride + mi_col;
826
22.1M
  int x, y;
827
22.1M
  const TileInfo *const tile = &xd->tile;
828
829
22.1M
  xd->mi = cm->mi_grid_visible + offset;
830
22.1M
  xd->mi[0] = &cm->mi[offset];
831
  // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
832
  // passing bsize from decode_partition().
833
22.1M
  xd->mi[0]->sb_type = bsize;
834
90.5M
  for (y = 0; y < y_mis; ++y)
835
422M
    for (x = !y; x < x_mis; ++x) {
836
354M
      xd->mi[y * cm->mi_stride + x] = xd->mi[0];
837
354M
    }
838
839
22.1M
  set_plane_n4(xd, bw, bh, bwl, bhl);
840
841
22.1M
  set_skip_context(xd, mi_row, mi_col);
842
843
  // Distance of Mb to the various image edges. These are specified to 8th pel
844
  // as they are always compared to values that are in 1/8th pel units
845
22.1M
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
846
847
22.1M
  vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
848
22.1M
  return xd->mi[0];
849
22.1M
}
850
851
static INLINE int predict_recon_inter(MACROBLOCKD *xd, MODE_INFO *mi,
852
                                      TileWorkerData *twd,
853
0
                                      predict_recon_func func) {
854
0
  int eobtotal = 0;
855
0
  int plane;
856
0
  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
857
0
    const struct macroblockd_plane *const pd = &xd->plane[plane];
858
0
    const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
859
0
    const int num_4x4_w = pd->n4_w;
860
0
    const int num_4x4_h = pd->n4_h;
861
0
    const int step = (1 << tx_size);
862
0
    int row, col;
863
0
    const int max_blocks_wide =
864
0
        num_4x4_w + (xd->mb_to_right_edge >= 0
865
0
                         ? 0
866
0
                         : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
867
0
    const int max_blocks_high =
868
0
        num_4x4_h + (xd->mb_to_bottom_edge >= 0
869
0
                         ? 0
870
0
                         : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
871
872
0
    xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
873
0
    xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
874
875
0
    for (row = 0; row < max_blocks_high; row += step)
876
0
      for (col = 0; col < max_blocks_wide; col += step)
877
0
        eobtotal += func(twd, mi, plane, row, col, tx_size);
878
0
  }
879
0
  return eobtotal;
880
0
}
881
882
static INLINE void predict_recon_intra(MACROBLOCKD *xd, MODE_INFO *mi,
883
                                       TileWorkerData *twd,
884
0
                                       intra_recon_func func) {
885
0
  int plane;
886
0
  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
887
0
    const struct macroblockd_plane *const pd = &xd->plane[plane];
888
0
    const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
889
0
    const int num_4x4_w = pd->n4_w;
890
0
    const int num_4x4_h = pd->n4_h;
891
0
    const int step = (1 << tx_size);
892
0
    int row, col;
893
0
    const int max_blocks_wide =
894
0
        num_4x4_w + (xd->mb_to_right_edge >= 0
895
0
                         ? 0
896
0
                         : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
897
0
    const int max_blocks_high =
898
0
        num_4x4_h + (xd->mb_to_bottom_edge >= 0
899
0
                         ? 0
900
0
                         : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
901
902
0
    xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
903
0
    xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
904
905
0
    for (row = 0; row < max_blocks_high; row += step)
906
0
      for (col = 0; col < max_blocks_wide; col += step)
907
0
        func(twd, mi, plane, row, col, tx_size);
908
0
  }
909
0
}
910
911
static void decode_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
912
22.1M
                         int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
913
22.1M
  VP9_COMMON *const cm = &pbi->common;
914
22.1M
  const int less8x8 = bsize < BLOCK_8X8;
915
22.1M
  const int bw = 1 << (bwl - 1);
916
22.1M
  const int bh = 1 << (bhl - 1);
917
22.1M
  const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
918
22.1M
  const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
919
22.1M
  vpx_reader *r = &twd->bit_reader;
920
22.1M
  MACROBLOCKD *const xd = &twd->xd;
921
922
22.1M
  MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
923
22.1M
                              y_mis, bwl, bhl);
924
925
22.1M
  if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
926
19.0M
    const BLOCK_SIZE uv_subsize =
927
19.0M
        ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
928
19.0M
    if (uv_subsize == BLOCK_INVALID)
929
277
      vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
930
277
                         "Invalid block size.");
931
19.0M
  }
932
933
22.1M
  vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis);
934
935
22.1M
  if (mi->skip) {
936
17.4M
    dec_reset_skip_context(xd);
937
17.4M
  }
938
939
22.1M
  if (!is_inter_block(mi)) {
940
11.1M
    int plane;
941
44.4M
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
942
33.3M
      const struct macroblockd_plane *const pd = &xd->plane[plane];
943
33.3M
      const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
944
33.3M
      const int num_4x4_w = pd->n4_w;
945
33.3M
      const int num_4x4_h = pd->n4_h;
946
33.3M
      const int step = (1 << tx_size);
947
33.3M
      int row, col;
948
33.3M
      const int max_blocks_wide =
949
33.3M
          num_4x4_w + (xd->mb_to_right_edge >= 0
950
33.3M
                           ? 0
951
33.3M
                           : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
952
33.3M
      const int max_blocks_high =
953
33.3M
          num_4x4_h + (xd->mb_to_bottom_edge >= 0
954
33.3M
                           ? 0
955
33.3M
                           : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
956
957
33.3M
      xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
958
33.3M
      xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
959
960
141M
      for (row = 0; row < max_blocks_high; row += step)
961
962M
        for (col = 0; col < max_blocks_wide; col += step)
962
854M
          predict_and_reconstruct_intra_block(twd, mi, plane, row, col,
963
854M
                                              tx_size);
964
33.3M
    }
965
11.1M
  } else {
966
    // Prediction
967
11.0M
    dec_build_inter_predictors_sb(twd, pbi, xd, mi_row, mi_col);
968
#if CONFIG_MISMATCH_DEBUG
969
    {
970
      int plane;
971
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
972
        const struct macroblockd_plane *pd = &xd->plane[plane];
973
        int pixel_c, pixel_r;
974
        const BLOCK_SIZE plane_bsize =
975
            get_plane_block_size(VPXMAX(bsize, BLOCK_8X8), &xd->plane[plane]);
976
        const int bw = get_block_width(plane_bsize);
977
        const int bh = get_block_height(plane_bsize);
978
        mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, 0, 0,
979
                        pd->subsampling_x, pd->subsampling_y);
980
        mismatch_check_block_pre(pd->dst.buf, pd->dst.stride, plane, pixel_c,
981
                                 pixel_r, bw, bh,
982
                                 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
983
      }
984
    }
985
#endif
986
987
    // Reconstruction
988
11.0M
    if (!mi->skip) {
989
1.65M
      int eobtotal = 0;
990
1.65M
      int plane;
991
992
6.63M
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
993
4.97M
        const struct macroblockd_plane *const pd = &xd->plane[plane];
994
4.97M
        const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
995
4.97M
        const int num_4x4_w = pd->n4_w;
996
4.97M
        const int num_4x4_h = pd->n4_h;
997
4.97M
        const int step = (1 << tx_size);
998
4.97M
        int row, col;
999
4.97M
        const int max_blocks_wide =
1000
4.97M
            num_4x4_w + (xd->mb_to_right_edge >= 0
1001
4.97M
                             ? 0
1002
4.97M
                             : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
1003
4.97M
        const int max_blocks_high =
1004
4.97M
            num_4x4_h +
1005
4.97M
            (xd->mb_to_bottom_edge >= 0
1006
4.97M
                 ? 0
1007
4.97M
                 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
1008
1009
4.97M
        xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
1010
4.97M
        xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
1011
1012
11.0M
        for (row = 0; row < max_blocks_high; row += step)
1013
14.0M
          for (col = 0; col < max_blocks_wide; col += step)
1014
7.97M
            eobtotal += reconstruct_inter_block(twd, mi, plane, row, col,
1015
7.97M
                                                tx_size, mi_row, mi_col);
1016
4.97M
      }
1017
1018
1.65M
      if (!less8x8 && eobtotal == 0) mi->skip = 1;  // skip loopfilter
1019
1.65M
    }
1020
11.0M
  }
1021
1022
22.1M
  xd->corrupted |= vpx_reader_has_error(r);
1023
1024
22.1M
  if (cm->lf.filter_level) {
1025
18.4M
    vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
1026
18.4M
  }
1027
22.1M
}
1028
1029
static void recon_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
1030
0
                        int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
1031
0
  VP9_COMMON *const cm = &pbi->common;
1032
0
  const int bw = 1 << (bwl - 1);
1033
0
  const int bh = 1 << (bhl - 1);
1034
0
  MACROBLOCKD *const xd = &twd->xd;
1035
1036
0
  MODE_INFO *mi = set_offsets_recon(cm, xd, mi_row, mi_col, bw, bh, bwl, bhl);
1037
1038
0
  if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1039
0
    const BLOCK_SIZE uv_subsize =
1040
0
        ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1041
0
    if (uv_subsize == BLOCK_INVALID)
1042
0
      vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
1043
0
                         "Invalid block size.");
1044
0
  }
1045
1046
0
  if (!is_inter_block(mi)) {
1047
0
    predict_recon_intra(xd, mi, twd,
1048
0
                        predict_and_reconstruct_intra_block_row_mt);
1049
0
  } else {
1050
    // Prediction
1051
0
    dec_build_inter_predictors_sb(twd, pbi, xd, mi_row, mi_col);
1052
1053
    // Reconstruction
1054
0
    if (!mi->skip) {
1055
0
      predict_recon_inter(xd, mi, twd, reconstruct_inter_block_row_mt);
1056
0
    }
1057
0
  }
1058
1059
0
  vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
1060
0
}
1061
1062
static void parse_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
1063
0
                        int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
1064
0
  VP9_COMMON *const cm = &pbi->common;
1065
0
  const int bw = 1 << (bwl - 1);
1066
0
  const int bh = 1 << (bhl - 1);
1067
0
  const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
1068
0
  const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
1069
0
  vpx_reader *r = &twd->bit_reader;
1070
0
  MACROBLOCKD *const xd = &twd->xd;
1071
1072
0
  MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
1073
0
                              y_mis, bwl, bhl);
1074
1075
0
  if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
1076
0
    const BLOCK_SIZE uv_subsize =
1077
0
        ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
1078
0
    if (uv_subsize == BLOCK_INVALID)
1079
0
      vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
1080
0
                         "Invalid block size.");
1081
0
  }
1082
1083
0
  vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis);
1084
1085
0
  if (mi->skip) {
1086
0
    dec_reset_skip_context(xd);
1087
0
  }
1088
1089
0
  if (!is_inter_block(mi)) {
1090
0
    predict_recon_intra(xd, mi, twd, parse_intra_block_row_mt);
1091
0
  } else {
1092
0
    if (!mi->skip) {
1093
0
      tran_low_t *dqcoeff[MAX_MB_PLANE];
1094
0
      int *eob[MAX_MB_PLANE];
1095
0
      int plane;
1096
0
      int eobtotal;
1097
      // Based on eobtotal and bsize, this may be mi->skip may be set to true
1098
      // In that case dqcoeff and eob need to be backed up and restored as
1099
      // recon_block will not increment these pointers for skip cases
1100
0
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1101
0
        const struct macroblockd_plane *const pd = &xd->plane[plane];
1102
0
        dqcoeff[plane] = pd->dqcoeff;
1103
0
        eob[plane] = pd->eob;
1104
0
      }
1105
0
      eobtotal = predict_recon_inter(xd, mi, twd, parse_inter_block_row_mt);
1106
1107
0
      if (bsize >= BLOCK_8X8 && eobtotal == 0) {
1108
0
        mi->skip = 1;  // skip loopfilter
1109
0
        for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1110
0
          struct macroblockd_plane *pd = &xd->plane[plane];
1111
0
          pd->dqcoeff = dqcoeff[plane];
1112
0
          pd->eob = eob[plane];
1113
0
        }
1114
0
      }
1115
0
    }
1116
0
  }
1117
1118
0
  xd->corrupted |= vpx_reader_has_error(r);
1119
0
}
1120
1121
static INLINE int dec_partition_plane_context(TileWorkerData *twd, int mi_row,
1122
25.1M
                                              int mi_col, int bsl) {
1123
25.1M
  const PARTITION_CONTEXT *above_ctx = twd->xd.above_seg_context + mi_col;
1124
25.1M
  const PARTITION_CONTEXT *left_ctx =
1125
25.1M
      twd->xd.left_seg_context + (mi_row & MI_MASK);
1126
25.1M
  int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1127
1128
  //  assert(bsl >= 0);
1129
1130
25.1M
  return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1131
25.1M
}
1132
1133
static INLINE void dec_update_partition_context(TileWorkerData *twd, int mi_row,
1134
                                                int mi_col, BLOCK_SIZE subsize,
1135
20.1M
                                                int bw) {
1136
20.1M
  PARTITION_CONTEXT *const above_ctx = twd->xd.above_seg_context + mi_col;
1137
20.1M
  PARTITION_CONTEXT *const left_ctx =
1138
20.1M
      twd->xd.left_seg_context + (mi_row & MI_MASK);
1139
1140
  // update the partition context at the end notes. set partition bits
1141
  // of block sizes larger than the current one to be one, and partition
1142
  // bits of smaller block sizes to be zero.
1143
20.1M
  memset(above_ctx, partition_context_lookup[subsize].above, bw);
1144
20.1M
  memset(left_ctx, partition_context_lookup[subsize].left, bw);
1145
20.1M
}
1146
1147
static PARTITION_TYPE read_partition(TileWorkerData *twd, int mi_row,
1148
                                     int mi_col, int has_rows, int has_cols,
1149
25.1M
                                     int bsl) {
1150
25.1M
  const int ctx = dec_partition_plane_context(twd, mi_row, mi_col, bsl);
1151
25.1M
  const vpx_prob *const probs = twd->xd.partition_probs[ctx];
1152
25.1M
  FRAME_COUNTS *counts = twd->xd.counts;
1153
25.1M
  PARTITION_TYPE p;
1154
25.1M
  vpx_reader *r = &twd->bit_reader;
1155
1156
25.1M
  if (has_rows && has_cols)
1157
24.0M
    p = (PARTITION_TYPE)vpx_read_tree(r, vp9_partition_tree, probs);
1158
1.06M
  else if (!has_rows && has_cols)
1159
644k
    p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
1160
417k
  else if (has_rows && !has_cols)
1161
368k
    p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
1162
49.1k
  else
1163
49.1k
    p = PARTITION_SPLIT;
1164
1165
25.1M
  if (counts) ++counts->partition[ctx][p];
1166
1167
25.1M
  return p;
1168
25.1M
}
1169
1170
// TODO(slavarnway): eliminate bsize and subsize in future commits
1171
static void decode_partition(TileWorkerData *twd, VP9Decoder *const pbi,
1172
                             int mi_row, int mi_col, BLOCK_SIZE bsize,
1173
26.2M
                             int n4x4_l2) {
1174
26.2M
  VP9_COMMON *const cm = &pbi->common;
1175
26.2M
  const int n8x8_l2 = n4x4_l2 - 1;
1176
26.2M
  const int num_8x8_wh = 1 << n8x8_l2;
1177
26.2M
  const int hbs = num_8x8_wh >> 1;
1178
26.2M
  PARTITION_TYPE partition;
1179
26.2M
  BLOCK_SIZE subsize;
1180
26.2M
  const int has_rows = (mi_row + hbs) < cm->mi_rows;
1181
26.2M
  const int has_cols = (mi_col + hbs) < cm->mi_cols;
1182
26.2M
  MACROBLOCKD *const xd = &twd->xd;
1183
1184
26.2M
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1185
1186
25.1M
  partition = read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2);
1187
25.1M
  subsize = subsize_lookup[partition][bsize];  // get_subsize(bsize, partition);
1188
25.1M
  if (!hbs) {
1189
    // calculate bmode block dimensions (log 2)
1190
8.07M
    xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1191
8.07M
    xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1192
8.07M
    decode_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
1193
17.0M
  } else {
1194
17.0M
    switch (partition) {
1195
9.54M
      case PARTITION_NONE:
1196
9.54M
        decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
1197
9.54M
        break;
1198
1.54M
      case PARTITION_HORZ:
1199
1.54M
        decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
1200
1.54M
        if (has_rows)
1201
1.20M
          decode_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
1202
1.20M
                       n8x8_l2);
1203
1.54M
        break;
1204
990k
      case PARTITION_VERT:
1205
990k
        decode_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
1206
990k
        if (has_cols)
1207
813k
          decode_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
1208
813k
                       n4x4_l2);
1209
990k
        break;
1210
4.95M
      case PARTITION_SPLIT:
1211
4.95M
        decode_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2);
1212
4.95M
        decode_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2);
1213
4.95M
        decode_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2);
1214
4.95M
        decode_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize,
1215
4.95M
                         n8x8_l2);
1216
4.95M
        break;
1217
0
      default: assert(0 && "Invalid partition type");
1218
17.0M
    }
1219
17.0M
  }
1220
1221
  // update partition context
1222
25.1M
  if (bsize >= BLOCK_8X8 &&
1223
25.1M
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
1224
20.1M
    dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh);
1225
25.1M
}
1226
1227
static void process_partition(TileWorkerData *twd, VP9Decoder *const pbi,
1228
                              int mi_row, int mi_col, BLOCK_SIZE bsize,
1229
                              int n4x4_l2, int parse_recon_flag,
1230
0
                              process_block_fn_t process_block) {
1231
0
  VP9_COMMON *const cm = &pbi->common;
1232
0
  const int n8x8_l2 = n4x4_l2 - 1;
1233
0
  const int num_8x8_wh = 1 << n8x8_l2;
1234
0
  const int hbs = num_8x8_wh >> 1;
1235
0
  PARTITION_TYPE partition;
1236
0
  BLOCK_SIZE subsize;
1237
0
  const int has_rows = (mi_row + hbs) < cm->mi_rows;
1238
0
  const int has_cols = (mi_col + hbs) < cm->mi_cols;
1239
0
  MACROBLOCKD *const xd = &twd->xd;
1240
1241
0
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1242
1243
0
  if (parse_recon_flag & PARSE) {
1244
0
    *xd->partition =
1245
0
        read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2);
1246
0
  }
1247
1248
0
  partition = *xd->partition;
1249
0
  xd->partition++;
1250
1251
0
  subsize = get_subsize(bsize, partition);
1252
0
  if (!hbs) {
1253
    // calculate bmode block dimensions (log 2)
1254
0
    xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1255
0
    xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1256
0
    process_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
1257
0
  } else {
1258
0
    switch (partition) {
1259
0
      case PARTITION_NONE:
1260
0
        process_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
1261
0
        break;
1262
0
      case PARTITION_HORZ:
1263
0
        process_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
1264
0
        if (has_rows)
1265
0
          process_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
1266
0
                        n8x8_l2);
1267
0
        break;
1268
0
      case PARTITION_VERT:
1269
0
        process_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
1270
0
        if (has_cols)
1271
0
          process_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
1272
0
                        n4x4_l2);
1273
0
        break;
1274
0
      case PARTITION_SPLIT:
1275
0
        process_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2,
1276
0
                          parse_recon_flag, process_block);
1277
0
        process_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
1278
0
                          parse_recon_flag, process_block);
1279
0
        process_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2,
1280
0
                          parse_recon_flag, process_block);
1281
0
        process_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize,
1282
0
                          n8x8_l2, parse_recon_flag, process_block);
1283
0
        break;
1284
0
      default: assert(0 && "Invalid partition type");
1285
0
    }
1286
0
  }
1287
1288
0
  if (parse_recon_flag & PARSE) {
1289
    // update partition context
1290
0
    if ((bsize == BLOCK_8X8 || partition != PARTITION_SPLIT) &&
1291
0
        bsize >= BLOCK_8X8)
1292
0
      dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh);
1293
0
  }
1294
0
}
1295
1296
static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
1297
                                size_t read_size,
1298
                                struct vpx_internal_error_info *error_info,
1299
                                vpx_reader *r, vpx_decrypt_cb decrypt_cb,
1300
125k
                                void *decrypt_state) {
1301
  // Validate the calculated partition length. If the buffer described by the
1302
  // partition can't be fully read then throw an error.
1303
125k
  if (!read_is_valid(data, read_size, data_end))
1304
17.9k
    vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1305
17.9k
                       "Truncated packet or corrupt tile length");
1306
1307
125k
  if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
1308
4.49k
    vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
1309
4.49k
                       "Failed to allocate bool decoder %d", 1);
1310
125k
}
1311
1312
static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
1313
239k
                                   vpx_reader *r) {
1314
239k
  int i, j, k, l, m;
1315
1316
239k
  if (vpx_read_bit(r))
1317
112k
    for (i = 0; i < PLANE_TYPES; ++i)
1318
224k
      for (j = 0; j < REF_TYPES; ++j)
1319
1.04M
        for (k = 0; k < COEF_BANDS; ++k)
1320
5.82M
          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1321
19.7M
            for (m = 0; m < UNCONSTRAINED_NODES; ++m)
1322
14.7M
              vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
1323
239k
}
1324
1325
98.6k
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, vpx_reader *r) {
1326
98.6k
  const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
1327
98.6k
  TX_SIZE tx_size;
1328
337k
  for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
1329
239k
    read_coef_probs_common(fc->coef_probs[tx_size], r);
1330
98.6k
}
1331
1332
static void setup_segmentation(struct segmentation *seg,
1333
160k
                               struct vpx_read_bit_buffer *rb) {
1334
160k
  int i, j;
1335
1336
160k
  seg->update_map = 0;
1337
160k
  seg->update_data = 0;
1338
1339
160k
  seg->enabled = vpx_rb_read_bit(rb);
1340
160k
  if (!seg->enabled) return;
1341
1342
  // Segmentation map update
1343
32.9k
  seg->update_map = vpx_rb_read_bit(rb);
1344
32.9k
  if (seg->update_map) {
1345
150k
    for (i = 0; i < SEG_TREE_PROBS; i++)
1346
130k
      seg->tree_probs[i] =
1347
130k
          vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
1348
1349
20.0k
    seg->temporal_update = vpx_rb_read_bit(rb);
1350
20.0k
    if (seg->temporal_update) {
1351
16.4k
      for (i = 0; i < PREDICTION_PROBS; i++)
1352
12.3k
        seg->pred_probs[i] =
1353
12.3k
            vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
1354
15.9k
    } else {
1355
49.3k
      for (i = 0; i < PREDICTION_PROBS; i++) seg->pred_probs[i] = MAX_PROB;
1356
15.9k
    }
1357
20.0k
  }
1358
1359
  // Segmentation data update
1360
32.9k
  seg->update_data = vpx_rb_read_bit(rb);
1361
32.9k
  if (seg->update_data) {
1362
12.3k
    seg->abs_delta = vpx_rb_read_bit(rb);
1363
1364
12.3k
    vp9_clearall_segfeatures(seg);
1365
1366
96.4k
    for (i = 0; i < MAX_SEGMENTS; i++) {
1367
413k
      for (j = 0; j < SEG_LVL_MAX; j++) {
1368
329k
        int data = 0;
1369
329k
        const int feature_enabled = vpx_rb_read_bit(rb);
1370
329k
        if (feature_enabled) {
1371
79.6k
          vp9_enable_segfeature(seg, i, j);
1372
79.6k
          data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
1373
79.6k
          if (vp9_is_segfeature_signed(j))
1374
37.3k
            data = vpx_rb_read_bit(rb) ? -data : data;
1375
79.6k
        }
1376
329k
        vp9_set_segdata(seg, i, j, data);
1377
329k
      }
1378
84.0k
    }
1379
12.3k
  }
1380
32.9k
}
1381
1382
static void setup_loopfilter(struct loopfilter *lf,
1383
164k
                             struct vpx_read_bit_buffer *rb) {
1384
164k
  lf->filter_level = vpx_rb_read_literal(rb, 6);
1385
164k
  lf->sharpness_level = vpx_rb_read_literal(rb, 3);
1386
1387
  // Read in loop filter deltas applied at the MB level based on mode or ref
1388
  // frame.
1389
164k
  lf->mode_ref_delta_update = 0;
1390
1391
164k
  lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb);
1392
164k
  if (lf->mode_ref_delta_enabled) {
1393
64.1k
    lf->mode_ref_delta_update = vpx_rb_read_bit(rb);
1394
64.1k
    if (lf->mode_ref_delta_update) {
1395
18.5k
      int i;
1396
1397
92.6k
      for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1398
74.1k
        if (vpx_rb_read_bit(rb))
1399
30.1k
          lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1400
1401
55.3k
      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1402
36.7k
        if (vpx_rb_read_bit(rb))
1403
14.2k
          lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1404
18.5k
    }
1405
64.1k
  }
1406
164k
}
1407
1408
486k
static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) {
1409
486k
  return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0;
1410
486k
}
1411
1412
static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
1413
163k
                               struct vpx_read_bit_buffer *rb) {
1414
163k
  cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS);
1415
163k
  cm->y_dc_delta_q = read_delta_q(rb);
1416
163k
  cm->uv_dc_delta_q = read_delta_q(rb);
1417
163k
  cm->uv_ac_delta_q = read_delta_q(rb);
1418
163k
  cm->dequant_bit_depth = cm->bit_depth;
1419
163k
  xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
1420
163k
                 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
1421
1422
163k
#if CONFIG_VP9_HIGHBITDEPTH
1423
163k
  xd->bd = (int)cm->bit_depth;
1424
163k
#endif
1425
163k
}
1426
1427
152k
static void setup_segmentation_dequant(VP9_COMMON *const cm) {
1428
  // Build y/uv dequant values based on segmentation.
1429
152k
  if (cm->seg.enabled) {
1430
24.6k
    int i;
1431
221k
    for (i = 0; i < MAX_SEGMENTS; ++i) {
1432
196k
      const int qindex = vp9_get_qindex(&cm->seg, i, cm->base_qindex);
1433
196k
      cm->y_dequant[i][0] =
1434
196k
          vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1435
196k
      cm->y_dequant[i][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1436
196k
      cm->uv_dequant[i][0] =
1437
196k
          vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
1438
196k
      cm->uv_dequant[i][1] =
1439
196k
          vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
1440
196k
    }
1441
128k
  } else {
1442
128k
    const int qindex = cm->base_qindex;
1443
    // When segmentation is disabled, only the first value is used.  The
1444
    // remaining are don't cares.
1445
128k
    cm->y_dequant[0][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1446
128k
    cm->y_dequant[0][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1447
128k
    cm->uv_dequant[0][0] =
1448
128k
        vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
1449
128k
    cm->uv_dequant[0][1] =
1450
128k
        vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
1451
128k
  }
1452
152k
}
1453
1454
67.1k
static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) {
1455
67.1k
  const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH, EIGHTTAP,
1456
67.1k
                                              EIGHTTAP_SHARP, BILINEAR };
1457
67.1k
  return vpx_rb_read_bit(rb) ? SWITCHABLE
1458
67.1k
                             : literal_to_filter[vpx_rb_read_literal(rb, 2)];
1459
67.1k
}
1460
1461
165k
static void setup_render_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1462
165k
  cm->render_width = cm->width;
1463
165k
  cm->render_height = cm->height;
1464
165k
  if (vpx_rb_read_bit(rb))
1465
6.83k
    vp9_read_frame_size(rb, &cm->render_width, &cm->render_height);
1466
165k
}
1467
1468
54.0k
static void resize_mv_buffer(VP9_COMMON *cm) {
1469
54.0k
  vpx_free(cm->cur_frame->mvs);
1470
54.0k
  cm->cur_frame->mi_rows = cm->mi_rows;
1471
54.0k
  cm->cur_frame->mi_cols = cm->mi_cols;
1472
54.0k
  CHECK_MEM_ERROR(&cm->error, cm->cur_frame->mvs,
1473
54.0k
                  (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
1474
54.0k
                                       sizeof(*cm->cur_frame->mvs)));
1475
54.0k
}
1476
1477
166k
static void resize_context_buffers(VP9_COMMON *cm, int width, int height) {
1478
166k
#if CONFIG_SIZE_LIMIT
1479
166k
  if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1480
763
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1481
763
                       "Dimensions of %dx%d beyond allowed size of %dx%d.",
1482
763
                       width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1483
166k
#endif
1484
166k
  if (cm->width != width || cm->height != height) {
1485
93.1k
    const int new_mi_rows =
1486
93.1k
        ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1487
93.1k
    const int new_mi_cols =
1488
93.1k
        ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1489
1490
    // Allocations in vp9_alloc_context_buffers() depend on individual
1491
    // dimensions as well as the overall size.
1492
93.1k
    if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
1493
69.4k
      if (vp9_alloc_context_buffers(cm, width, height)) {
1494
        // The cm->mi_* values have been cleared and any existing context
1495
        // buffers have been freed. Clear cm->width and cm->height to be
1496
        // consistent and to force a realloc next time.
1497
0
        cm->width = 0;
1498
0
        cm->height = 0;
1499
0
        vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1500
0
                           "Failed to allocate context buffers");
1501
0
      }
1502
69.4k
    } else {
1503
23.6k
      vp9_set_mb_mi(cm, width, height);
1504
23.6k
    }
1505
93.1k
    vp9_init_context_buffers(cm);
1506
93.1k
    cm->width = width;
1507
93.1k
    cm->height = height;
1508
93.1k
  }
1509
166k
  if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
1510
166k
      cm->mi_cols > cm->cur_frame->mi_cols) {
1511
54.0k
    resize_mv_buffer(cm);
1512
54.0k
  }
1513
166k
}
1514
1515
99.2k
static void setup_frame_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1516
99.2k
  int width, height;
1517
99.2k
  BufferPool *const pool = cm->buffer_pool;
1518
99.2k
  vp9_read_frame_size(rb, &width, &height);
1519
99.2k
  resize_context_buffers(cm, width, height);
1520
99.2k
  setup_render_size(cm, rb);
1521
1522
99.2k
  if (vpx_realloc_frame_buffer(
1523
99.2k
          get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1524
99.2k
          cm->subsampling_y,
1525
99.2k
#if CONFIG_VP9_HIGHBITDEPTH
1526
99.2k
          cm->use_highbitdepth,
1527
99.2k
#endif
1528
99.2k
          VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
1529
99.2k
          &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1530
99.2k
          pool->cb_priv)) {
1531
165
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1532
165
                       "Failed to allocate frame buffer");
1533
165
  }
1534
1535
99.2k
  pool->frame_bufs[cm->new_fb_idx].released = 0;
1536
99.2k
  pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1537
99.2k
  pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1538
99.2k
  pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1539
99.2k
  pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1540
99.2k
  pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1541
99.2k
  pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1542
99.2k
  pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1543
99.2k
}
1544
1545
static INLINE int valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth,
1546
                                          int ref_xss, int ref_yss,
1547
                                          vpx_bit_depth_t this_bit_depth,
1548
201k
                                          int this_xss, int this_yss) {
1549
201k
  return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
1550
201k
         ref_yss == this_yss;
1551
201k
}
1552
1553
static void setup_frame_size_with_refs(VP9_COMMON *cm,
1554
67.7k
                                       struct vpx_read_bit_buffer *rb) {
1555
67.7k
  int width, height;
1556
67.7k
  int found = 0, i;
1557
67.7k
  int has_valid_ref_frame = 0;
1558
67.7k
  BufferPool *const pool = cm->buffer_pool;
1559
112k
  for (i = 0; i < REFS_PER_FRAME; ++i) {
1560
105k
    if (vpx_rb_read_bit(rb)) {
1561
61.5k
      if (cm->frame_refs[i].idx != INVALID_IDX) {
1562
61.4k
        YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
1563
61.4k
        width = buf->y_crop_width;
1564
61.4k
        height = buf->y_crop_height;
1565
61.4k
        found = 1;
1566
61.4k
        break;
1567
61.4k
      } else {
1568
49
        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1569
49
                           "Failed to decode frame size");
1570
49
      }
1571
61.5k
    }
1572
105k
  }
1573
1574
67.7k
  if (!found) vp9_read_frame_size(rb, &width, &height);
1575
1576
67.7k
  if (width <= 0 || height <= 0)
1577
0
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1578
0
                       "Invalid frame size");
1579
1580
  // Check to make sure at least one of frames that this frame references
1581
  // has valid dimensions.
1582
270k
  for (i = 0; i < REFS_PER_FRAME; ++i) {
1583
203k
    RefBuffer *const ref_frame = &cm->frame_refs[i];
1584
203k
    has_valid_ref_frame |=
1585
203k
        (ref_frame->idx != INVALID_IDX &&
1586
203k
         valid_ref_frame_size(ref_frame->buf->y_crop_width,
1587
202k
                              ref_frame->buf->y_crop_height, width, height));
1588
203k
  }
1589
67.7k
  if (!has_valid_ref_frame)
1590
321
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1591
321
                       "Referenced frame has invalid size");
1592
269k
  for (i = 0; i < REFS_PER_FRAME; ++i) {
1593
201k
    RefBuffer *const ref_frame = &cm->frame_refs[i];
1594
201k
    if (ref_frame->idx == INVALID_IDX ||
1595
201k
        !valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
1596
201k
                                 ref_frame->buf->subsampling_x,
1597
201k
                                 ref_frame->buf->subsampling_y, cm->bit_depth,
1598
201k
                                 cm->subsampling_x, cm->subsampling_y))
1599
177
      vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1600
177
                         "Referenced frame has incompatible color format");
1601
201k
  }
1602
1603
67.7k
  resize_context_buffers(cm, width, height);
1604
67.7k
  setup_render_size(cm, rb);
1605
1606
67.7k
  if (vpx_realloc_frame_buffer(
1607
67.7k
          get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1608
67.7k
          cm->subsampling_y,
1609
67.7k
#if CONFIG_VP9_HIGHBITDEPTH
1610
67.7k
          cm->use_highbitdepth,
1611
67.7k
#endif
1612
67.7k
          VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
1613
67.7k
          &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1614
67.7k
          pool->cb_priv)) {
1615
1
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1616
1
                       "Failed to allocate frame buffer");
1617
1
  }
1618
1619
67.7k
  pool->frame_bufs[cm->new_fb_idx].released = 0;
1620
67.7k
  pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1621
67.7k
  pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1622
67.7k
  pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1623
67.7k
  pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1624
67.7k
  pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1625
67.7k
  pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1626
67.7k
  pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1627
67.7k
}
1628
1629
152k
static void setup_tile_info(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1630
152k
  int min_log2_tile_cols, max_log2_tile_cols, max_ones;
1631
152k
  vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1632
1633
  // columns
1634
152k
  max_ones = max_log2_tile_cols - min_log2_tile_cols;
1635
152k
  cm->log2_tile_cols = min_log2_tile_cols;
1636
166k
  while (max_ones-- && vpx_rb_read_bit(rb)) cm->log2_tile_cols++;
1637
1638
152k
  if (cm->log2_tile_cols > 6)
1639
0
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1640
0
                       "Invalid number of tile columns");
1641
1642
  // rows
1643
152k
  cm->log2_tile_rows = vpx_rb_read_bit(rb);
1644
152k
  if (cm->log2_tile_rows) cm->log2_tile_rows += vpx_rb_read_bit(rb);
1645
152k
}
1646
1647
// Reads the next tile returning its size and adjusting '*data' accordingly
1648
// based on 'is_last'.
1649
static void get_tile_buffer(const uint8_t *const data_end, int is_last,
1650
                            struct vpx_internal_error_info *error_info,
1651
                            const uint8_t **data, vpx_decrypt_cb decrypt_cb,
1652
132k
                            void *decrypt_state, TileBuffer *buf) {
1653
132k
  size_t size;
1654
1655
132k
  if (!is_last) {
1656
35.5k
    if (!read_is_valid(*data, 4, data_end))
1657
256
      vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1658
256
                         "Truncated packet or corrupt tile length");
1659
1660
35.5k
    if (decrypt_cb) {
1661
0
      uint8_t be_data[4];
1662
0
      decrypt_cb(decrypt_state, *data, be_data, 4);
1663
0
      size = mem_get_be32(be_data);
1664
35.5k
    } else {
1665
35.5k
      size = mem_get_be32(*data);
1666
35.5k
    }
1667
35.5k
    *data += 4;
1668
1669
35.5k
    if (size > (size_t)(data_end - *data))
1670
479
      vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1671
479
                         "Truncated packet or corrupt tile size");
1672
97.1k
  } else {
1673
97.1k
    size = data_end - *data;
1674
97.1k
  }
1675
1676
132k
  buf->data = *data;
1677
132k
  buf->size = size;
1678
1679
132k
  *data += size;
1680
132k
}
1681
1682
static void get_tile_buffers(VP9Decoder *pbi, const uint8_t *data,
1683
                             const uint8_t *data_end, int tile_cols,
1684
                             int tile_rows,
1685
97.8k
                             TileBuffer (*tile_buffers)[1 << 6]) {
1686
97.8k
  int r, c;
1687
1688
196k
  for (r = 0; r < tile_rows; ++r) {
1689
231k
    for (c = 0; c < tile_cols; ++c) {
1690
132k
      const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
1691
132k
      TileBuffer *const buf = &tile_buffers[r][c];
1692
132k
      buf->col = c;
1693
132k
      get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
1694
132k
                      pbi->decrypt_cb, pbi->decrypt_state, buf);
1695
132k
    }
1696
98.4k
  }
1697
97.8k
}
1698
1699
static void map_write(RowMTWorkerData *const row_mt_worker_data, int map_idx,
1700
0
                      int sync_idx) {
1701
0
#if CONFIG_MULTITHREAD
1702
0
  pthread_mutex_lock(&row_mt_worker_data->recon_sync_mutex[sync_idx]);
1703
0
  row_mt_worker_data->recon_map[map_idx] = 1;
1704
0
  pthread_cond_signal(&row_mt_worker_data->recon_sync_cond[sync_idx]);
1705
0
  pthread_mutex_unlock(&row_mt_worker_data->recon_sync_mutex[sync_idx]);
1706
#else
1707
  (void)row_mt_worker_data;
1708
  (void)map_idx;
1709
  (void)sync_idx;
1710
#endif  // CONFIG_MULTITHREAD
1711
0
}
1712
1713
static void map_read(RowMTWorkerData *const row_mt_worker_data, int map_idx,
1714
0
                     int sync_idx) {
1715
0
#if CONFIG_MULTITHREAD
1716
0
  volatile int8_t *map = row_mt_worker_data->recon_map + map_idx;
1717
0
  pthread_mutex_t *const mutex =
1718
0
      &row_mt_worker_data->recon_sync_mutex[sync_idx];
1719
0
  pthread_mutex_lock(mutex);
1720
0
  while (!(*map)) {
1721
0
    pthread_cond_wait(&row_mt_worker_data->recon_sync_cond[sync_idx], mutex);
1722
0
  }
1723
0
  pthread_mutex_unlock(mutex);
1724
#else
1725
  (void)row_mt_worker_data;
1726
  (void)map_idx;
1727
  (void)sync_idx;
1728
#endif  // CONFIG_MULTITHREAD
1729
0
}
1730
1731
0
static int lpf_map_write_check(VP9LfSync *lf_sync, int row, int num_tile_cols) {
1732
0
  int return_val = 0;
1733
0
#if CONFIG_MULTITHREAD
1734
0
  int corrupted;
1735
0
  pthread_mutex_lock(lf_sync->lf_mutex);
1736
0
  corrupted = lf_sync->corrupted;
1737
0
  pthread_mutex_unlock(lf_sync->lf_mutex);
1738
0
  if (!corrupted) {
1739
0
    pthread_mutex_lock(&lf_sync->recon_done_mutex[row]);
1740
0
    lf_sync->num_tiles_done[row] += 1;
1741
0
    if (num_tile_cols == lf_sync->num_tiles_done[row]) return_val = 1;
1742
0
    pthread_mutex_unlock(&lf_sync->recon_done_mutex[row]);
1743
0
  }
1744
#else
1745
  (void)lf_sync;
1746
  (void)row;
1747
  (void)num_tile_cols;
1748
#endif
1749
0
  return return_val;
1750
0
}
1751
1752
0
static void vp9_tile_done(VP9Decoder *pbi) {
1753
0
#if CONFIG_MULTITHREAD
1754
0
  int terminate;
1755
0
  RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
1756
0
  const int all_parse_done = 1 << pbi->common.log2_tile_cols;
1757
0
  pthread_mutex_lock(&row_mt_worker_data->recon_done_mutex);
1758
0
  row_mt_worker_data->num_tiles_done++;
1759
0
  terminate = all_parse_done == row_mt_worker_data->num_tiles_done;
1760
0
  pthread_mutex_unlock(&row_mt_worker_data->recon_done_mutex);
1761
0
  if (terminate) {
1762
0
    vp9_jobq_terminate(&row_mt_worker_data->jobq);
1763
0
  }
1764
#else
1765
  (void)pbi;
1766
#endif
1767
0
}
1768
1769
0
static void vp9_jobq_alloc(VP9Decoder *pbi) {
1770
0
  VP9_COMMON *const cm = &pbi->common;
1771
0
  RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
1772
0
  const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
1773
0
  const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2;
1774
0
  const int tile_cols = 1 << cm->log2_tile_cols;
1775
0
  const size_t jobq_size = (tile_cols * sb_rows * 2 + sb_rows) * sizeof(Job);
1776
1777
0
  if (jobq_size > row_mt_worker_data->jobq_size) {
1778
0
    vpx_free(row_mt_worker_data->jobq_buf);
1779
0
    CHECK_MEM_ERROR(&cm->error, row_mt_worker_data->jobq_buf,
1780
0
                    vpx_calloc(1, jobq_size));
1781
0
    vp9_jobq_init(&row_mt_worker_data->jobq, row_mt_worker_data->jobq_buf,
1782
0
                  jobq_size);
1783
0
    row_mt_worker_data->jobq_size = jobq_size;
1784
0
  }
1785
0
}
1786
1787
static void recon_tile_row(TileWorkerData *tile_data, VP9Decoder *pbi,
1788
                           int mi_row, int is_last_row, VP9LfSync *lf_sync,
1789
0
                           int cur_tile_col) {
1790
0
  VP9_COMMON *const cm = &pbi->common;
1791
0
  RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
1792
0
  const int tile_cols = 1 << cm->log2_tile_cols;
1793
0
  const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1794
0
  const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2;
1795
0
  const int cur_sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
1796
0
  int mi_col_start = tile_data->xd.tile.mi_col_start;
1797
0
  int mi_col_end = tile_data->xd.tile.mi_col_end;
1798
0
  int mi_col;
1799
1800
0
  vp9_zero(tile_data->xd.left_context);
1801
0
  vp9_zero(tile_data->xd.left_seg_context);
1802
0
  for (mi_col = mi_col_start; mi_col < mi_col_end; mi_col += MI_BLOCK_SIZE) {
1803
0
    const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
1804
0
    int plane;
1805
0
    const int sb_num = (cur_sb_row * (aligned_cols >> MI_BLOCK_SIZE_LOG2) + c);
1806
1807
    // Top Dependency
1808
0
    if (cur_sb_row) {
1809
0
      map_read(row_mt_worker_data, ((cur_sb_row - 1) * sb_cols) + c,
1810
0
               ((cur_sb_row - 1) * tile_cols) + cur_tile_col);
1811
0
    }
1812
1813
0
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1814
0
      tile_data->xd.plane[plane].eob =
1815
0
          row_mt_worker_data->eob[plane] + (sb_num << EOBS_PER_SB_LOG2);
1816
0
      tile_data->xd.plane[plane].dqcoeff =
1817
0
          row_mt_worker_data->dqcoeff[plane] + (sb_num << DQCOEFFS_PER_SB_LOG2);
1818
0
    }
1819
0
    tile_data->xd.partition =
1820
0
        row_mt_worker_data->partition + (sb_num * PARTITIONS_PER_SB);
1821
0
    process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4, RECON,
1822
0
                      recon_block);
1823
0
    if (cm->lf.filter_level && !cm->skip_loop_filter) {
1824
      // Queue LPF_JOB
1825
0
      int is_lpf_job_ready = 0;
1826
1827
0
      if (mi_col + MI_BLOCK_SIZE >= mi_col_end) {
1828
        // Checks if this row has been decoded in all tiles
1829
0
        is_lpf_job_ready = lpf_map_write_check(lf_sync, cur_sb_row, tile_cols);
1830
1831
0
        if (is_lpf_job_ready) {
1832
0
          Job lpf_job;
1833
0
          lpf_job.job_type = LPF_JOB;
1834
0
          if (cur_sb_row > 0) {
1835
0
            lpf_job.row_num = mi_row - MI_BLOCK_SIZE;
1836
0
            vp9_jobq_queue(&row_mt_worker_data->jobq, &lpf_job,
1837
0
                           sizeof(lpf_job));
1838
0
          }
1839
0
          if (is_last_row) {
1840
0
            lpf_job.row_num = mi_row;
1841
0
            vp9_jobq_queue(&row_mt_worker_data->jobq, &lpf_job,
1842
0
                           sizeof(lpf_job));
1843
0
          }
1844
0
        }
1845
0
      }
1846
0
    }
1847
0
    map_write(row_mt_worker_data, (cur_sb_row * sb_cols) + c,
1848
0
              (cur_sb_row * tile_cols) + cur_tile_col);
1849
0
  }
1850
0
}
1851
1852
static void parse_tile_row(TileWorkerData *tile_data, VP9Decoder *pbi,
1853
0
                           int mi_row, int cur_tile_col, uint8_t **data_end) {
1854
0
  int mi_col;
1855
0
  VP9_COMMON *const cm = &pbi->common;
1856
0
  RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
1857
0
  TileInfo *tile = &tile_data->xd.tile;
1858
0
  TileBuffer *const buf = &pbi->tile_buffers[cur_tile_col];
1859
0
  const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1860
1861
0
  vp9_zero(tile_data->dqcoeff);
1862
0
  vp9_tile_init(tile, cm, 0, cur_tile_col);
1863
1864
  /* Update reader only at the beginning of each row in a tile */
1865
0
  if (mi_row == 0) {
1866
0
    setup_token_decoder(buf->data, *data_end, buf->size, &tile_data->error_info,
1867
0
                        &tile_data->bit_reader, pbi->decrypt_cb,
1868
0
                        pbi->decrypt_state);
1869
0
  }
1870
0
  vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
1871
0
  tile_data->xd.error_info = &tile_data->error_info;
1872
1873
0
  vp9_zero(tile_data->xd.left_context);
1874
0
  vp9_zero(tile_data->xd.left_seg_context);
1875
0
  for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
1876
0
       mi_col += MI_BLOCK_SIZE) {
1877
0
    const int r = mi_row >> MI_BLOCK_SIZE_LOG2;
1878
0
    const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
1879
0
    int plane;
1880
0
    const int sb_num = (r * (aligned_cols >> MI_BLOCK_SIZE_LOG2) + c);
1881
0
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
1882
0
      tile_data->xd.plane[plane].eob =
1883
0
          row_mt_worker_data->eob[plane] + (sb_num << EOBS_PER_SB_LOG2);
1884
0
      tile_data->xd.plane[plane].dqcoeff =
1885
0
          row_mt_worker_data->dqcoeff[plane] + (sb_num << DQCOEFFS_PER_SB_LOG2);
1886
0
    }
1887
0
    tile_data->xd.partition =
1888
0
        row_mt_worker_data->partition + sb_num * PARTITIONS_PER_SB;
1889
0
    process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4, PARSE,
1890
0
                      parse_block);
1891
0
  }
1892
0
}
1893
1894
0
static int row_decode_worker_hook(void *arg1, void *arg2) {
1895
0
  ThreadData *const thread_data = (ThreadData *)arg1;
1896
0
  uint8_t **data_end = (uint8_t **)arg2;
1897
0
  VP9Decoder *const pbi = thread_data->pbi;
1898
0
  VP9_COMMON *const cm = &pbi->common;
1899
0
  RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
1900
0
  const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1901
0
  const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
1902
0
  const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2;
1903
0
  const int tile_cols = 1 << cm->log2_tile_cols;
1904
0
  Job job;
1905
0
  LFWorkerData *lf_data = thread_data->lf_data;
1906
0
  VP9LfSync *lf_sync = thread_data->lf_sync;
1907
0
  volatile int corrupted = 0;
1908
0
  TileWorkerData *volatile tile_data_recon = NULL;
1909
1910
0
  while (!vp9_jobq_dequeue(&row_mt_worker_data->jobq, &job, sizeof(job), 1)) {
1911
0
    int mi_col;
1912
0
    const int mi_row = job.row_num;
1913
1914
0
    if (job.job_type == LPF_JOB) {
1915
0
      lf_data->start = mi_row;
1916
0
      lf_data->stop = lf_data->start + MI_BLOCK_SIZE;
1917
1918
0
      if (cm->lf.filter_level && !cm->skip_loop_filter &&
1919
0
          mi_row < cm->mi_rows) {
1920
0
        vp9_loopfilter_job(lf_data, lf_sync);
1921
0
      }
1922
0
    } else if (job.job_type == RECON_JOB) {
1923
0
      const int cur_sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
1924
0
      const int is_last_row = sb_rows - 1 == cur_sb_row;
1925
0
      int mi_col_start, mi_col_end;
1926
0
      if (!tile_data_recon)
1927
0
        CHECK_MEM_ERROR(&cm->error, tile_data_recon,
1928
0
                        vpx_memalign(32, sizeof(TileWorkerData)));
1929
1930
0
      tile_data_recon->xd = pbi->mb;
1931
0
      vp9_tile_init(&tile_data_recon->xd.tile, cm, 0, job.tile_col);
1932
0
      vp9_init_macroblockd(cm, &tile_data_recon->xd, tile_data_recon->dqcoeff);
1933
0
      mi_col_start = tile_data_recon->xd.tile.mi_col_start;
1934
0
      mi_col_end = tile_data_recon->xd.tile.mi_col_end;
1935
1936
0
      if (setjmp(tile_data_recon->error_info.jmp)) {
1937
0
        const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2;
1938
0
        tile_data_recon->error_info.setjmp = 0;
1939
0
        corrupted = 1;
1940
0
        for (mi_col = mi_col_start; mi_col < mi_col_end;
1941
0
             mi_col += MI_BLOCK_SIZE) {
1942
0
          const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
1943
0
          map_write(row_mt_worker_data, (cur_sb_row * sb_cols) + c,
1944
0
                    (cur_sb_row * tile_cols) + job.tile_col);
1945
0
        }
1946
0
        if (is_last_row) {
1947
0
          vp9_tile_done(pbi);
1948
0
        }
1949
0
        continue;
1950
0
      }
1951
1952
0
      tile_data_recon->error_info.setjmp = 1;
1953
0
      tile_data_recon->xd.error_info = &tile_data_recon->error_info;
1954
1955
0
      recon_tile_row(tile_data_recon, pbi, mi_row, is_last_row, lf_sync,
1956
0
                     job.tile_col);
1957
1958
0
      if (corrupted)
1959
0
        vpx_internal_error(&tile_data_recon->error_info,
1960
0
                           VPX_CODEC_CORRUPT_FRAME,
1961
0
                           "Failed to decode tile data");
1962
1963
0
      if (is_last_row) {
1964
0
        vp9_tile_done(pbi);
1965
0
      }
1966
0
    } else if (job.job_type == PARSE_JOB) {
1967
0
      TileWorkerData *const tile_data = &pbi->tile_worker_data[job.tile_col];
1968
1969
0
      if (setjmp(tile_data->error_info.jmp)) {
1970
0
        tile_data->error_info.setjmp = 0;
1971
0
        corrupted = 1;
1972
0
        vp9_tile_done(pbi);
1973
0
        continue;
1974
0
      }
1975
1976
0
      tile_data->xd = pbi->mb;
1977
0
      tile_data->xd.counts =
1978
0
          cm->frame_parallel_decoding_mode ? 0 : &tile_data->counts;
1979
1980
0
      tile_data->error_info.setjmp = 1;
1981
1982
0
      parse_tile_row(tile_data, pbi, mi_row, job.tile_col, data_end);
1983
1984
0
      corrupted |= tile_data->xd.corrupted;
1985
0
      if (corrupted)
1986
0
        vpx_internal_error(&tile_data->error_info, VPX_CODEC_CORRUPT_FRAME,
1987
0
                           "Failed to decode tile data");
1988
1989
      /* Queue in the recon_job for this row */
1990
0
      {
1991
0
        Job recon_job;
1992
0
        recon_job.row_num = mi_row;
1993
0
        recon_job.tile_col = job.tile_col;
1994
0
        recon_job.job_type = RECON_JOB;
1995
0
        vp9_jobq_queue(&row_mt_worker_data->jobq, &recon_job,
1996
0
                       sizeof(recon_job));
1997
0
      }
1998
1999
      /* Queue next parse job */
2000
0
      if (mi_row + MI_BLOCK_SIZE < cm->mi_rows) {
2001
0
        Job parse_job;
2002
0
        parse_job.row_num = mi_row + MI_BLOCK_SIZE;
2003
0
        parse_job.tile_col = job.tile_col;
2004
0
        parse_job.job_type = PARSE_JOB;
2005
0
        vp9_jobq_queue(&row_mt_worker_data->jobq, &parse_job,
2006
0
                       sizeof(parse_job));
2007
0
      }
2008
0
    }
2009
0
  }
2010
2011
0
  vpx_free(tile_data_recon);
2012
0
  return !corrupted;
2013
0
}
2014
2015
static const uint8_t *decode_tiles(VP9Decoder *pbi, const uint8_t *data,
2016
86.2k
                                   const uint8_t *data_end) {
2017
86.2k
  VP9_COMMON *const cm = &pbi->common;
2018
86.2k
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
2019
86.2k
  const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2020
86.2k
  const int tile_cols = 1 << cm->log2_tile_cols;
2021
86.2k
  const int tile_rows = 1 << cm->log2_tile_rows;
2022
86.2k
  TileBuffer tile_buffers[4][1 << 6];
2023
86.2k
  int tile_row, tile_col;
2024
86.2k
  int mi_row, mi_col;
2025
86.2k
  TileWorkerData *tile_data = NULL;
2026
2027
86.2k
  if (cm->lf.filter_level && !cm->skip_loop_filter &&
2028
86.2k
      pbi->lf_worker.data1 == NULL) {
2029
10.1k
    CHECK_MEM_ERROR(&cm->error, pbi->lf_worker.data1,
2030
10.1k
                    vpx_memalign(32, sizeof(LFWorkerData)));
2031
10.1k
    pbi->lf_worker.hook = vp9_loop_filter_worker;
2032
10.1k
    if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
2033
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
2034
0
                         "Loop filter thread creation failed");
2035
0
    }
2036
10.1k
  }
2037
2038
86.2k
  if (cm->lf.filter_level && !cm->skip_loop_filter) {
2039
46.8k
    LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
2040
    // Be sure to sync as we might be resuming after a failed frame decode.
2041
46.8k
    winterface->sync(&pbi->lf_worker);
2042
46.8k
    vp9_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
2043
46.8k
                               pbi->mb.plane);
2044
46.8k
  }
2045
2046
86.2k
  assert(tile_rows <= 4);
2047
86.2k
  assert(tile_cols <= (1 << 6));
2048
2049
  // Note: this memset assumes above_context[0], [1] and [2]
2050
  // are allocated as part of the same buffer.
2051
86.2k
  memset(cm->above_context, 0,
2052
86.2k
         sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
2053
2054
86.2k
  memset(cm->above_seg_context, 0,
2055
86.2k
         sizeof(*cm->above_seg_context) * aligned_cols);
2056
2057
86.2k
  vp9_reset_lfm(cm);
2058
2059
86.2k
  get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
2060
2061
  // Load all tile information into tile_data.
2062
172k
  for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
2063
173k
    for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
2064
86.9k
      const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
2065
86.9k
      tile_data = pbi->tile_worker_data + tile_cols * tile_row + tile_col;
2066
86.9k
      tile_data->xd = pbi->mb;
2067
86.9k
      tile_data->xd.corrupted = 0;
2068
86.9k
      tile_data->xd.counts =
2069
86.9k
          cm->frame_parallel_decoding_mode ? NULL : &cm->counts;
2070
86.9k
      vp9_zero(tile_data->dqcoeff);
2071
86.9k
      vp9_tile_init(&tile_data->xd.tile, cm, tile_row, tile_col);
2072
86.9k
      setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
2073
86.9k
                          &tile_data->bit_reader, pbi->decrypt_cb,
2074
86.9k
                          pbi->decrypt_state);
2075
86.9k
      vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
2076
86.9k
    }
2077
86.5k
  }
2078
2079
167k
  for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
2080
81.1k
    TileInfo tile;
2081
81.1k
    vp9_tile_set_row(&tile, cm, tile_row);
2082
431k
    for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
2083
350k
         mi_row += MI_BLOCK_SIZE) {
2084
705k
      for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
2085
355k
        const int col =
2086
355k
            pbi->inv_tile_order ? tile_cols - tile_col - 1 : tile_col;
2087
355k
        tile_data = pbi->tile_worker_data + tile_cols * tile_row + col;
2088
355k
        vp9_tile_set_col(&tile, cm, col);
2089
355k
        vp9_zero(tile_data->xd.left_context);
2090
355k
        vp9_zero(tile_data->xd.left_seg_context);
2091
4.37M
        for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
2092
4.01M
             mi_col += MI_BLOCK_SIZE) {
2093
4.01M
          if (pbi->row_mt == 1) {
2094
0
            int plane;
2095
0
            RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
2096
0
            for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
2097
0
              tile_data->xd.plane[plane].eob = row_mt_worker_data->eob[plane];
2098
0
              tile_data->xd.plane[plane].dqcoeff =
2099
0
                  row_mt_worker_data->dqcoeff[plane];
2100
0
            }
2101
0
            tile_data->xd.partition = row_mt_worker_data->partition;
2102
0
            process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4,
2103
0
                              PARSE, parse_block);
2104
2105
0
            for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
2106
0
              tile_data->xd.plane[plane].eob = row_mt_worker_data->eob[plane];
2107
0
              tile_data->xd.plane[plane].dqcoeff =
2108
0
                  row_mt_worker_data->dqcoeff[plane];
2109
0
            }
2110
0
            tile_data->xd.partition = row_mt_worker_data->partition;
2111
0
            process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4,
2112
0
                              RECON, recon_block);
2113
4.01M
          } else {
2114
4.01M
            decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
2115
4.01M
          }
2116
4.01M
        }
2117
355k
        pbi->mb.corrupted |= tile_data->xd.corrupted;
2118
355k
        if (pbi->mb.corrupted)
2119
11.4k
          vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2120
11.4k
                             "Failed to decode tile data");
2121
355k
      }
2122
      // Loopfilter one row.
2123
350k
      if (cm->lf.filter_level && !cm->skip_loop_filter) {
2124
213k
        const int lf_start = mi_row - MI_BLOCK_SIZE;
2125
213k
        LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
2126
2127
        // delay the loopfilter by 1 macroblock row.
2128
213k
        if (lf_start < 0) continue;
2129
2130
        // decoding has completed: finish up the loop filter in this thread.
2131
172k
        if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue;
2132
2133
161k
        winterface->sync(&pbi->lf_worker);
2134
161k
        lf_data->start = lf_start;
2135
161k
        lf_data->stop = mi_row;
2136
161k
        if (pbi->max_threads > 1) {
2137
109k
          winterface->launch(&pbi->lf_worker);
2138
109k
        } else {
2139
52.2k
          winterface->execute(&pbi->lf_worker);
2140
52.2k
        }
2141
161k
      }
2142
350k
    }
2143
81.1k
  }
2144
2145
  // Loopfilter remaining rows in the frame.
2146
86.2k
  if (cm->lf.filter_level && !cm->skip_loop_filter) {
2147
39.0k
    LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
2148
39.0k
    winterface->sync(&pbi->lf_worker);
2149
39.0k
    lf_data->start = lf_data->stop;
2150
39.0k
    lf_data->stop = cm->mi_rows;
2151
39.0k
    winterface->execute(&pbi->lf_worker);
2152
39.0k
  }
2153
2154
  // Get last tile data.
2155
86.2k
  tile_data = pbi->tile_worker_data + tile_cols * tile_rows - 1;
2156
2157
86.2k
  return vpx_reader_find_end(&tile_data->bit_reader);
2158
86.2k
}
2159
2160
static void set_rows_after_error(VP9LfSync *lf_sync, int start_row, int mi_rows,
2161
10.8k
                                 int num_tiles_left, int total_num_tiles) {
2162
10.9k
  do {
2163
10.9k
    int mi_row;
2164
10.9k
    const int aligned_rows = mi_cols_aligned_to_sb(mi_rows);
2165
10.9k
    const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2);
2166
10.9k
    const int corrupted = 1;
2167
219k
    for (mi_row = start_row; mi_row < mi_rows; mi_row += MI_BLOCK_SIZE) {
2168
208k
      const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2);
2169
208k
      vp9_set_row(lf_sync, total_num_tiles, mi_row >> MI_BLOCK_SIZE_LOG2,
2170
208k
                  is_last_row, corrupted);
2171
208k
    }
2172
    /* If there are multiple tiles, the second tile should start marking row
2173
     * progress from row 0.
2174
     */
2175
10.9k
    start_row = 0;
2176
10.9k
  } while (num_tiles_left--);
2177
10.8k
}
2178
2179
// On entry 'tile_data->data_end' points to the end of the input frame, on exit
2180
// it is updated to reflect the bitreader position of the final tile column if
2181
// present in the tile buffer group or NULL otherwise.
2182
38.0k
static int tile_worker_hook(void *arg1, void *arg2) {
2183
38.0k
  TileWorkerData *const tile_data = (TileWorkerData *)arg1;
2184
38.0k
  VP9Decoder *const pbi = (VP9Decoder *)arg2;
2185
2186
38.0k
  TileInfo *volatile tile = &tile_data->xd.tile;
2187
38.0k
  const int final_col = (1 << pbi->common.log2_tile_cols) - 1;
2188
38.0k
  const uint8_t *volatile bit_reader_end = NULL;
2189
38.0k
  VP9_COMMON *cm = &pbi->common;
2190
2191
38.0k
  LFWorkerData *lf_data = tile_data->lf_data;
2192
38.0k
  VP9LfSync *lf_sync = tile_data->lf_sync;
2193
2194
38.0k
  volatile int mi_row = 0;
2195
38.0k
  volatile int n = tile_data->buf_start;
2196
38.0k
  if (setjmp(tile_data->error_info.jmp)) {
2197
17.1k
    tile_data->error_info.setjmp = 0;
2198
17.1k
    tile_data->xd.corrupted = 1;
2199
17.1k
    tile_data->data_end = NULL;
2200
17.1k
    if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
2201
9.46k
      const int num_tiles_left = tile_data->buf_end - n;
2202
9.46k
      const int mi_row_start = mi_row;
2203
9.46k
      set_rows_after_error(lf_sync, mi_row_start, cm->mi_rows, num_tiles_left,
2204
9.46k
                           1 << cm->log2_tile_cols);
2205
9.46k
    }
2206
17.1k
    return 0;
2207
17.1k
  }
2208
20.9k
  tile_data->error_info.setjmp = 1;
2209
2210
20.9k
  tile_data->xd.corrupted = 0;
2211
2212
21.3k
  do {
2213
21.3k
    int mi_col;
2214
21.3k
    const TileBuffer *const buf = pbi->tile_buffers + n;
2215
2216
    /* Initialize to 0 is safe since we do not deal with streams that have
2217
     * more than one row of tiles. (So tile->mi_row_start will be 0)
2218
     */
2219
21.3k
    assert(cm->log2_tile_rows == 0);
2220
38.3k
    mi_row = 0;
2221
38.3k
    vp9_zero(tile_data->dqcoeff);
2222
38.3k
    vp9_tile_init(tile, &pbi->common, 0, buf->col);
2223
38.3k
    setup_token_decoder(buf->data, tile_data->data_end, buf->size,
2224
38.3k
                        &tile_data->error_info, &tile_data->bit_reader,
2225
38.3k
                        pbi->decrypt_cb, pbi->decrypt_state);
2226
38.3k
    vp9_init_macroblockd(&pbi->common, &tile_data->xd, tile_data->dqcoeff);
2227
    // init resets xd.error_info
2228
38.3k
    tile_data->xd.error_info = &tile_data->error_info;
2229
2230
228k
    for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
2231
189k
         mi_row += MI_BLOCK_SIZE) {
2232
189k
      vp9_zero(tile_data->xd.left_context);
2233
189k
      vp9_zero(tile_data->xd.left_seg_context);
2234
2.59M
      for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
2235
2.40M
           mi_col += MI_BLOCK_SIZE) {
2236
2.40M
        decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
2237
2.40M
      }
2238
189k
      if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
2239
111k
        const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
2240
111k
        const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2);
2241
111k
        const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2);
2242
111k
        vp9_set_row(lf_sync, 1 << cm->log2_tile_cols,
2243
111k
                    mi_row >> MI_BLOCK_SIZE_LOG2, is_last_row,
2244
111k
                    tile_data->xd.corrupted);
2245
111k
      }
2246
189k
    }
2247
2248
38.3k
    if (buf->col == final_col) {
2249
10.6k
      bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader);
2250
10.6k
    }
2251
38.3k
  } while (!tile_data->xd.corrupted && ++n <= tile_data->buf_end);
2252
2253
37.9k
  if (pbi->lpf_mt_opt && n < tile_data->buf_end && cm->lf.filter_level &&
2254
37.9k
      !cm->skip_loop_filter) {
2255
    /* This was not incremented in the tile loop, so increment before tiles left
2256
     * calculation
2257
     */
2258
1.41k
    ++n;
2259
1.41k
    set_rows_after_error(lf_sync, 0, cm->mi_rows, tile_data->buf_end - n,
2260
1.41k
                         1 << cm->log2_tile_cols);
2261
1.41k
  }
2262
2263
37.9k
  if (pbi->lpf_mt_opt && !tile_data->xd.corrupted && cm->lf.filter_level &&
2264
37.9k
      !cm->skip_loop_filter) {
2265
7.37k
    vp9_loopfilter_rows(lf_data, lf_sync);
2266
7.37k
  }
2267
2268
37.9k
  tile_data->data_end = bit_reader_end;
2269
37.9k
  return !tile_data->xd.corrupted;
2270
20.9k
}
2271
2272
// sorts in descending order
2273
54.5k
static int compare_tile_buffers(const void *a, const void *b) {
2274
54.5k
  const TileBuffer *const buf_a = (const TileBuffer *)a;
2275
54.5k
  const TileBuffer *const buf_b = (const TileBuffer *)b;
2276
54.5k
  return (buf_a->size < buf_b->size) - (buf_a->size > buf_b->size);
2277
54.5k
}
2278
2279
11.6k
static INLINE void init_mt(VP9Decoder *pbi) {
2280
11.6k
  int n;
2281
11.6k
  VP9_COMMON *const cm = &pbi->common;
2282
11.6k
  VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
2283
11.6k
  const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2284
11.6k
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
2285
2286
11.6k
  if (pbi->num_tile_workers == 0) {
2287
964
    const int num_threads = pbi->max_threads;
2288
964
    CHECK_MEM_ERROR(&cm->error, pbi->tile_workers,
2289
964
                    vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
2290
30.6k
    for (n = 0; n < num_threads; ++n) {
2291
29.7k
      VPxWorker *const worker = &pbi->tile_workers[n];
2292
29.7k
      ++pbi->num_tile_workers;
2293
2294
29.7k
      winterface->init(worker);
2295
29.7k
      if (n < num_threads - 1 && !winterface->reset(worker)) {
2296
0
        do {
2297
0
          winterface->end(&pbi->tile_workers[pbi->num_tile_workers - 1]);
2298
0
        } while (--pbi->num_tile_workers != 0);
2299
0
        vpx_free(pbi->tile_workers);
2300
0
        pbi->tile_workers = NULL;
2301
0
        vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
2302
0
                           "Tile decoder thread creation failed");
2303
0
      }
2304
29.7k
    }
2305
964
  }
2306
2307
  // Initialize LPF
2308
11.6k
  if ((pbi->lpf_mt_opt || pbi->row_mt) && cm->lf.filter_level &&
2309
11.6k
      !cm->skip_loop_filter) {
2310
4.92k
    vp9_lpf_mt_init(lf_row_sync, cm, cm->lf.filter_level,
2311
4.92k
                    pbi->num_tile_workers);
2312
4.92k
  }
2313
2314
  // Note: this memset assumes above_context[0], [1] and [2]
2315
  // are allocated as part of the same buffer.
2316
11.6k
  memset(cm->above_context, 0,
2317
11.6k
         sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
2318
2319
11.6k
  memset(cm->above_seg_context, 0,
2320
11.6k
         sizeof(*cm->above_seg_context) * aligned_mi_cols);
2321
2322
11.6k
  vp9_reset_lfm(cm);
2323
11.6k
}
2324
2325
static const uint8_t *decode_tiles_row_wise_mt(VP9Decoder *pbi,
2326
                                               const uint8_t *data,
2327
0
                                               const uint8_t *data_end) {
2328
0
  VP9_COMMON *const cm = &pbi->common;
2329
0
  RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
2330
0
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
2331
0
  const int tile_cols = 1 << cm->log2_tile_cols;
2332
0
  const int tile_rows = 1 << cm->log2_tile_rows;
2333
0
  const int num_workers = pbi->max_threads;
2334
0
  int i, n;
2335
0
  int col;
2336
0
  int corrupted = 0;
2337
0
  const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
2338
0
  const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2;
2339
0
  VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
2340
0
  YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
2341
2342
0
  assert(tile_cols <= (1 << 6));
2343
0
  assert(tile_rows == 1);
2344
0
  (void)tile_rows;
2345
2346
0
  memset(row_mt_worker_data->recon_map, 0,
2347
0
         sb_rows * sb_cols * sizeof(*row_mt_worker_data->recon_map));
2348
2349
0
  init_mt(pbi);
2350
2351
  // Reset tile decoding hook
2352
0
  for (n = 0; n < num_workers; ++n) {
2353
0
    VPxWorker *const worker = &pbi->tile_workers[n];
2354
0
    ThreadData *const thread_data = &pbi->row_mt_worker_data->thread_data[n];
2355
0
    winterface->sync(worker);
2356
2357
0
    if (cm->lf.filter_level && !cm->skip_loop_filter) {
2358
0
      thread_data->lf_sync = lf_row_sync;
2359
0
      thread_data->lf_data = &thread_data->lf_sync->lfdata[n];
2360
0
      vp9_loop_filter_data_reset(thread_data->lf_data, new_fb, cm,
2361
0
                                 pbi->mb.plane);
2362
0
    }
2363
2364
0
    thread_data->pbi = pbi;
2365
2366
0
    worker->hook = row_decode_worker_hook;
2367
0
    worker->data1 = thread_data;
2368
0
    worker->data2 = (void *)&row_mt_worker_data->data_end;
2369
0
  }
2370
2371
0
  for (col = 0; col < tile_cols; ++col) {
2372
0
    TileWorkerData *const tile_data = &pbi->tile_worker_data[col];
2373
0
    tile_data->xd = pbi->mb;
2374
0
    tile_data->xd.counts =
2375
0
        cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
2376
0
  }
2377
2378
  /* Reset the jobq to start of the jobq buffer */
2379
0
  vp9_jobq_reset(&row_mt_worker_data->jobq);
2380
0
  row_mt_worker_data->num_tiles_done = 0;
2381
0
  row_mt_worker_data->data_end = NULL;
2382
2383
  // Load tile data into tile_buffers
2384
0
  get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
2385
0
                   &pbi->tile_buffers);
2386
2387
  // Initialize thread frame counts.
2388
0
  if (!cm->frame_parallel_decoding_mode) {
2389
0
    for (col = 0; col < tile_cols; ++col) {
2390
0
      TileWorkerData *const tile_data = &pbi->tile_worker_data[col];
2391
0
      vp9_zero(tile_data->counts);
2392
0
    }
2393
0
  }
2394
2395
  // queue parse jobs for 0th row of every tile
2396
0
  for (col = 0; col < tile_cols; ++col) {
2397
0
    Job parse_job;
2398
0
    parse_job.row_num = 0;
2399
0
    parse_job.tile_col = col;
2400
0
    parse_job.job_type = PARSE_JOB;
2401
0
    vp9_jobq_queue(&row_mt_worker_data->jobq, &parse_job, sizeof(parse_job));
2402
0
  }
2403
2404
0
  for (i = 0; i < num_workers; ++i) {
2405
0
    VPxWorker *const worker = &pbi->tile_workers[i];
2406
0
    worker->had_error = 0;
2407
0
    if (i == num_workers - 1) {
2408
0
      winterface->execute(worker);
2409
0
    } else {
2410
0
      winterface->launch(worker);
2411
0
    }
2412
0
  }
2413
2414
0
  for (; n > 0; --n) {
2415
0
    VPxWorker *const worker = &pbi->tile_workers[n - 1];
2416
    // TODO(jzern): The tile may have specific error data associated with
2417
    // its vpx_internal_error_info which could be propagated to the main info
2418
    // in cm. Additionally once the threads have been synced and an error is
2419
    // detected, there's no point in continuing to decode tiles.
2420
0
    corrupted |= !winterface->sync(worker);
2421
0
  }
2422
2423
0
  pbi->mb.corrupted = corrupted;
2424
2425
0
  {
2426
    /* Set data end */
2427
0
    TileWorkerData *const tile_data = &pbi->tile_worker_data[tile_cols - 1];
2428
0
    row_mt_worker_data->data_end = vpx_reader_find_end(&tile_data->bit_reader);
2429
0
  }
2430
2431
  // Accumulate thread frame counts.
2432
0
  if (!cm->frame_parallel_decoding_mode) {
2433
0
    for (i = 0; i < tile_cols; ++i) {
2434
0
      TileWorkerData *const tile_data = &pbi->tile_worker_data[i];
2435
0
      vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
2436
0
    }
2437
0
  }
2438
2439
0
  return row_mt_worker_data->data_end;
2440
0
}
2441
2442
static const uint8_t *decode_tiles_mt(VP9Decoder *pbi, const uint8_t *data,
2443
11.6k
                                      const uint8_t *data_end) {
2444
11.6k
  VP9_COMMON *const cm = &pbi->common;
2445
11.6k
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
2446
11.6k
  const uint8_t *bit_reader_end = NULL;
2447
11.6k
  VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
2448
11.6k
  YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
2449
11.6k
  const int tile_cols = 1 << cm->log2_tile_cols;
2450
11.6k
  const int tile_rows = 1 << cm->log2_tile_rows;
2451
11.6k
  const int num_workers = VPXMIN(pbi->max_threads, tile_cols);
2452
11.6k
  int n;
2453
2454
11.6k
  assert(tile_cols <= (1 << 6));
2455
11.6k
  assert(tile_rows == 1);
2456
11.6k
  (void)tile_rows;
2457
2458
11.6k
  init_mt(pbi);
2459
2460
  // Reset tile decoding hook
2461
51.9k
  for (n = 0; n < num_workers; ++n) {
2462
40.3k
    VPxWorker *const worker = &pbi->tile_workers[n];
2463
40.3k
    TileWorkerData *const tile_data =
2464
40.3k
        &pbi->tile_worker_data[n + pbi->total_tiles];
2465
40.3k
    winterface->sync(worker);
2466
2467
40.3k
    if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
2468
19.6k
      tile_data->lf_sync = lf_row_sync;
2469
19.6k
      tile_data->lf_data = &tile_data->lf_sync->lfdata[n];
2470
19.6k
      vp9_loop_filter_data_reset(tile_data->lf_data, new_fb, cm, pbi->mb.plane);
2471
19.6k
      tile_data->lf_data->y_only = 0;
2472
19.6k
    }
2473
2474
40.3k
    tile_data->xd = pbi->mb;
2475
40.3k
    tile_data->xd.counts =
2476
40.3k
        cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
2477
40.3k
    worker->hook = tile_worker_hook;
2478
40.3k
    worker->data1 = tile_data;
2479
40.3k
    worker->data2 = pbi;
2480
40.3k
  }
2481
2482
  // Load tile data into tile_buffers
2483
11.6k
  get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
2484
11.6k
                   &pbi->tile_buffers);
2485
2486
  // Sort the buffers based on size in descending order.
2487
11.6k
  qsort(pbi->tile_buffers, tile_cols, sizeof(pbi->tile_buffers[0]),
2488
11.6k
        compare_tile_buffers);
2489
2490
11.6k
  if (num_workers == tile_cols) {
2491
    // Rearrange the tile buffers such that the largest, and
2492
    // presumably the most difficult, tile will be decoded in the main thread.
2493
    // This should help minimize the number of instances where the main thread
2494
    // is waiting for a worker to complete.
2495
8.40k
    const TileBuffer largest = pbi->tile_buffers[0];
2496
8.40k
    memmove(pbi->tile_buffers, pbi->tile_buffers + 1,
2497
8.40k
            (tile_cols - 1) * sizeof(pbi->tile_buffers[0]));
2498
8.40k
    pbi->tile_buffers[tile_cols - 1] = largest;
2499
8.40k
  } else {
2500
3.23k
    int start = 0, end = tile_cols - 2;
2501
3.23k
    TileBuffer tmp;
2502
2503
    // Interleave the tiles to distribute the load between threads, assuming a
2504
    // larger tile implies it is more difficult to decode.
2505
8.61k
    while (start < end) {
2506
5.37k
      tmp = pbi->tile_buffers[start];
2507
5.37k
      pbi->tile_buffers[start] = pbi->tile_buffers[end];
2508
5.37k
      pbi->tile_buffers[end] = tmp;
2509
5.37k
      start += 2;
2510
5.37k
      end -= 2;
2511
5.37k
    }
2512
3.23k
  }
2513
2514
  // Initialize thread frame counts.
2515
11.6k
  if (!cm->frame_parallel_decoding_mode) {
2516
29.1k
    for (n = 0; n < num_workers; ++n) {
2517
23.9k
      TileWorkerData *const tile_data =
2518
23.9k
          (TileWorkerData *)pbi->tile_workers[n].data1;
2519
23.9k
      vp9_zero(tile_data->counts);
2520
23.9k
    }
2521
5.17k
  }
2522
2523
11.6k
  {
2524
11.6k
    const int base = tile_cols / num_workers;
2525
11.6k
    const int remain = tile_cols % num_workers;
2526
11.6k
    int buf_start = 0;
2527
2528
49.7k
    for (n = 0; n < num_workers; ++n) {
2529
38.0k
      const int count = base + (remain + n) / num_workers;
2530
38.0k
      VPxWorker *const worker = &pbi->tile_workers[n];
2531
38.0k
      TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
2532
2533
38.0k
      tile_data->buf_start = buf_start;
2534
38.0k
      tile_data->buf_end = buf_start + count - 1;
2535
38.0k
      tile_data->data_end = data_end;
2536
38.0k
      buf_start += count;
2537
2538
38.0k
      worker->had_error = 0;
2539
38.0k
      if (n == num_workers - 1) {
2540
11.1k
        assert(tile_data->buf_end == tile_cols - 1);
2541
11.1k
        winterface->execute(worker);
2542
26.9k
      } else {
2543
26.9k
        winterface->launch(worker);
2544
26.9k
      }
2545
38.0k
    }
2546
2547
49.7k
    for (; n > 0; --n) {
2548
38.0k
      VPxWorker *const worker = &pbi->tile_workers[n - 1];
2549
38.0k
      TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
2550
      // TODO(jzern): The tile may have specific error data associated with
2551
      // its vpx_internal_error_info which could be propagated to the main info
2552
      // in cm. Additionally once the threads have been synced and an error is
2553
      // detected, there's no point in continuing to decode tiles.
2554
38.0k
      pbi->mb.corrupted |= !winterface->sync(worker);
2555
38.0k
      if (!bit_reader_end) bit_reader_end = tile_data->data_end;
2556
38.0k
    }
2557
11.6k
  }
2558
2559
  // Accumulate thread frame counts.
2560
11.6k
  if (!cm->frame_parallel_decoding_mode) {
2561
29.1k
    for (n = 0; n < num_workers; ++n) {
2562
23.9k
      TileWorkerData *const tile_data =
2563
23.9k
          (TileWorkerData *)pbi->tile_workers[n].data1;
2564
23.9k
      vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
2565
23.9k
    }
2566
5.17k
  }
2567
2568
11.6k
  assert(bit_reader_end || pbi->mb.corrupted);
2569
11.1k
  return bit_reader_end;
2570
11.6k
}
2571
2572
13.8k
static void error_handler(void *data) {
2573
13.8k
  VP9_COMMON *const cm = (VP9_COMMON *)data;
2574
13.8k
  vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
2575
13.8k
}
2576
2577
static void read_bitdepth_colorspace_sampling(VP9_COMMON *cm,
2578
43.1k
                                              struct vpx_read_bit_buffer *rb) {
2579
43.1k
  if (cm->profile >= PROFILE_2) {
2580
9.40k
    cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10;
2581
9.40k
#if CONFIG_VP9_HIGHBITDEPTH
2582
9.40k
    cm->use_highbitdepth = 1;
2583
9.40k
#endif
2584
33.7k
  } else {
2585
33.7k
    cm->bit_depth = VPX_BITS_8;
2586
33.7k
#if CONFIG_VP9_HIGHBITDEPTH
2587
33.7k
    cm->use_highbitdepth = 0;
2588
33.7k
#endif
2589
33.7k
  }
2590
43.1k
  cm->color_space = vpx_rb_read_literal(rb, 3);
2591
43.1k
  if (cm->color_space != VPX_CS_SRGB) {
2592
42.5k
    cm->color_range = (vpx_color_range_t)vpx_rb_read_bit(rb);
2593
42.5k
    if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
2594
6.98k
      cm->subsampling_x = vpx_rb_read_bit(rb);
2595
6.98k
      cm->subsampling_y = vpx_rb_read_bit(rb);
2596
6.98k
      if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
2597
217
        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2598
217
                           "4:2:0 color not supported in profile 1 or 3");
2599
6.98k
      if (vpx_rb_read_bit(rb))
2600
78
        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2601
78
                           "Reserved bit set");
2602
35.5k
    } else {
2603
35.5k
      cm->subsampling_y = cm->subsampling_x = 1;
2604
35.5k
    }
2605
42.5k
  } else {
2606
606
    cm->color_range = VPX_CR_FULL_RANGE;
2607
606
    if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
2608
      // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
2609
      // 4:2:2 or 4:4:0 chroma sampling is not allowed.
2610
394
      cm->subsampling_y = cm->subsampling_x = 0;
2611
394
      if (vpx_rb_read_bit(rb))
2612
90
        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2613
90
                           "Reserved bit set");
2614
394
    } else {
2615
212
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2616
212
                         "4:4:4 color not supported in profile 0 or 2");
2617
212
    }
2618
606
  }
2619
43.1k
}
2620
2621
34.2k
static INLINE void flush_all_fb_on_key(VP9_COMMON *cm) {
2622
34.2k
  if (cm->frame_type == KEY_FRAME && cm->current_video_frame > 0) {
2623
20.2k
    RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
2624
20.2k
    BufferPool *const pool = cm->buffer_pool;
2625
20.2k
    int i;
2626
262k
    for (i = 0; i < FRAME_BUFFERS; ++i) {
2627
242k
      if (i == cm->new_fb_idx) continue;
2628
222k
      frame_bufs[i].ref_count = 0;
2629
222k
      if (!frame_bufs[i].released) {
2630
76.0k
        pool->release_fb_cb(pool->cb_priv, &frame_bufs[i].raw_frame_buffer);
2631
76.0k
        frame_bufs[i].released = 1;
2632
76.0k
      }
2633
222k
    }
2634
20.2k
  }
2635
34.2k
}
2636
2637
static size_t read_uncompressed_header(VP9Decoder *pbi,
2638
225k
                                       struct vpx_read_bit_buffer *rb) {
2639
225k
  VP9_COMMON *const cm = &pbi->common;
2640
225k
  BufferPool *const pool = cm->buffer_pool;
2641
225k
  RefCntBuffer *const frame_bufs = pool->frame_bufs;
2642
225k
  int i, mask, ref_index = 0;
2643
225k
  size_t sz;
2644
2645
225k
  cm->last_frame_type = cm->frame_type;
2646
225k
  cm->last_intra_only = cm->intra_only;
2647
2648
225k
  if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
2649
25.7k
    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2650
25.7k
                       "Invalid frame marker");
2651
2652
225k
  cm->profile = vp9_read_profile(rb);
2653
225k
#if CONFIG_VP9_HIGHBITDEPTH
2654
225k
  if (cm->profile >= MAX_PROFILES)
2655
83
    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2656
83
                       "Unsupported bitstream profile");
2657
#else
2658
  if (cm->profile >= PROFILE_2)
2659
    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2660
                       "Unsupported bitstream profile");
2661
#endif
2662
2663
225k
  cm->show_existing_frame = vpx_rb_read_bit(rb);
2664
225k
  if (cm->show_existing_frame) {
2665
    // Show an existing frame directly.
2666
5.40k
    const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)];
2667
5.40k
    if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
2668
587
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2669
587
                         "Buffer %d does not contain a decoded frame",
2670
587
                         frame_to_show);
2671
587
    }
2672
2673
5.40k
    ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
2674
5.40k
    pbi->refresh_frame_flags = 0;
2675
5.40k
    cm->lf.filter_level = 0;
2676
5.40k
    cm->show_frame = 1;
2677
2678
5.40k
    return 0;
2679
5.40k
  }
2680
2681
219k
  cm->frame_type = (FRAME_TYPE)vpx_rb_read_bit(rb);
2682
219k
  cm->show_frame = vpx_rb_read_bit(rb);
2683
219k
  cm->error_resilient_mode = vpx_rb_read_bit(rb);
2684
2685
219k
  if (cm->frame_type == KEY_FRAME) {
2686
43.9k
    if (!vp9_read_sync_code(rb))
2687
1.09k
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2688
1.09k
                         "Invalid frame sync code");
2689
2690
43.9k
    read_bitdepth_colorspace_sampling(cm, rb);
2691
43.9k
    pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
2692
2693
171k
    for (i = 0; i < REFS_PER_FRAME; ++i) {
2694
127k
      cm->frame_refs[i].idx = INVALID_IDX;
2695
127k
      cm->frame_refs[i].buf = NULL;
2696
127k
    }
2697
2698
43.9k
    setup_frame_size(cm, rb);
2699
43.9k
    if (pbi->need_resync) {
2700
34.2k
      memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2701
34.2k
      flush_all_fb_on_key(cm);
2702
34.2k
      pbi->need_resync = 0;
2703
34.2k
    }
2704
175k
  } else {
2705
175k
    cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
2706
2707
175k
    cm->reset_frame_context =
2708
175k
        cm->error_resilient_mode ? 0 : vpx_rb_read_literal(rb, 2);
2709
2710
175k
    if (cm->intra_only) {
2711
60.0k
      if (!vp9_read_sync_code(rb))
2712
3.14k
        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2713
3.14k
                           "Invalid frame sync code");
2714
60.0k
      if (cm->profile > PROFILE_0) {
2715
297
        read_bitdepth_colorspace_sampling(cm, rb);
2716
59.7k
      } else {
2717
        // NOTE: The intra-only frame header does not include the specification
2718
        // of either the color format or color sub-sampling in profile 0. VP9
2719
        // specifies that the default color format should be YUV 4:2:0 in this
2720
        // case (normative).
2721
59.7k
        cm->color_space = VPX_CS_BT_601;
2722
59.7k
        cm->color_range = VPX_CR_STUDIO_RANGE;
2723
59.7k
        cm->subsampling_y = cm->subsampling_x = 1;
2724
59.7k
        cm->bit_depth = VPX_BITS_8;
2725
59.7k
#if CONFIG_VP9_HIGHBITDEPTH
2726
59.7k
        cm->use_highbitdepth = 0;
2727
59.7k
#endif
2728
59.7k
      }
2729
2730
60.0k
      pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
2731
60.0k
      setup_frame_size(cm, rb);
2732
60.0k
      if (pbi->need_resync) {
2733
56.3k
        memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2734
56.3k
        pbi->need_resync = 0;
2735
56.3k
      }
2736
115k
    } else if (pbi->need_resync != 1) { /* Skip if need resync */
2737
67.7k
      pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
2738
271k
      for (i = 0; i < REFS_PER_FRAME; ++i) {
2739
203k
        const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2);
2740
203k
        const int idx = cm->ref_frame_map[ref];
2741
203k
        RefBuffer *const ref_frame = &cm->frame_refs[i];
2742
203k
        ref_frame->idx = idx;
2743
203k
        ref_frame->buf = &frame_bufs[idx].buf;
2744
203k
        cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb);
2745
203k
      }
2746
2747
67.7k
      setup_frame_size_with_refs(cm, rb);
2748
2749
67.7k
      cm->allow_high_precision_mv = vpx_rb_read_bit(rb);
2750
67.7k
      cm->interp_filter = read_interp_filter(rb);
2751
2752
269k
      for (i = 0; i < REFS_PER_FRAME; ++i) {
2753
201k
        RefBuffer *const ref_buf = &cm->frame_refs[i];
2754
201k
#if CONFIG_VP9_HIGHBITDEPTH
2755
201k
        vp9_setup_scale_factors_for_frame(
2756
201k
            &ref_buf->sf, ref_buf->buf->y_crop_width,
2757
201k
            ref_buf->buf->y_crop_height, cm->width, cm->height,
2758
201k
            cm->use_highbitdepth);
2759
#else
2760
        vp9_setup_scale_factors_for_frame(
2761
            &ref_buf->sf, ref_buf->buf->y_crop_width,
2762
            ref_buf->buf->y_crop_height, cm->width, cm->height);
2763
#endif
2764
201k
      }
2765
67.7k
    }
2766
175k
  }
2767
219k
#if CONFIG_VP9_HIGHBITDEPTH
2768
219k
  get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
2769
219k
#endif
2770
219k
  get_frame_new_buffer(cm)->color_space = cm->color_space;
2771
219k
  get_frame_new_buffer(cm)->color_range = cm->color_range;
2772
219k
  get_frame_new_buffer(cm)->render_width = cm->render_width;
2773
219k
  get_frame_new_buffer(cm)->render_height = cm->render_height;
2774
2775
219k
  if (pbi->need_resync) {
2776
21.8k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2777
21.8k
                       "Keyframe / intra-only frame required to reset decoder"
2778
21.8k
                       " state");
2779
21.8k
  }
2780
2781
219k
  if (!cm->error_resilient_mode) {
2782
103k
    cm->refresh_frame_context = vpx_rb_read_bit(rb);
2783
103k
    cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb);
2784
103k
    if (!cm->frame_parallel_decoding_mode) vp9_zero(cm->counts);
2785
116k
  } else {
2786
116k
    cm->refresh_frame_context = 0;
2787
116k
    cm->frame_parallel_decoding_mode = 1;
2788
116k
  }
2789
2790
  // This flag will be overridden by the call to vp9_setup_past_independence
2791
  // below, forcing the use of context 0 for those frame types.
2792
219k
  cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
2793
2794
  // Generate next_ref_frame_map.
2795
936k
  for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
2796
716k
    if (mask & 1) {
2797
511k
      cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
2798
511k
      ++frame_bufs[cm->new_fb_idx].ref_count;
2799
511k
    } else {
2800
204k
      cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
2801
204k
    }
2802
    // Current thread holds the reference frame.
2803
716k
    if (cm->ref_frame_map[ref_index] >= 0)
2804
338k
      ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2805
716k
    ++ref_index;
2806
716k
  }
2807
2808
816k
  for (; ref_index < REF_FRAMES; ++ref_index) {
2809
596k
    cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
2810
    // Current thread holds the reference frame.
2811
596k
    if (cm->ref_frame_map[ref_index] >= 0)
2812
249k
      ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2813
596k
  }
2814
219k
  pbi->hold_ref_buf = 1;
2815
2816
219k
  if (frame_is_intra_only(cm) || cm->error_resilient_mode)
2817
97.4k
    vp9_setup_past_independence(cm);
2818
2819
219k
  setup_loopfilter(&cm->lf, rb);
2820
219k
  setup_quantization(cm, &pbi->mb, rb);
2821
219k
  setup_segmentation(&cm->seg, rb);
2822
219k
  setup_segmentation_dequant(cm);
2823
2824
219k
  setup_tile_info(cm, rb);
2825
219k
  if (pbi->row_mt == 1) {
2826
0
    int num_sbs = 1;
2827
0
    const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
2828
0
    const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2;
2829
0
    const int num_jobs = sb_rows << cm->log2_tile_cols;
2830
2831
0
    if (pbi->row_mt_worker_data == NULL) {
2832
0
      CHECK_MEM_ERROR(&cm->error, pbi->row_mt_worker_data,
2833
0
                      vpx_calloc(1, sizeof(*pbi->row_mt_worker_data)));
2834
0
#if CONFIG_MULTITHREAD
2835
0
      pthread_mutex_init(&pbi->row_mt_worker_data->recon_done_mutex, NULL);
2836
0
#endif
2837
0
    }
2838
2839
0
    if (pbi->max_threads > 1) {
2840
0
      const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2841
0
      const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2;
2842
2843
0
      num_sbs = sb_cols * sb_rows;
2844
0
    }
2845
2846
0
    if (num_sbs > pbi->row_mt_worker_data->num_sbs ||
2847
0
        num_jobs > pbi->row_mt_worker_data->num_jobs) {
2848
0
      vp9_dec_free_row_mt_mem(pbi->row_mt_worker_data);
2849
0
      vp9_dec_alloc_row_mt_mem(pbi->row_mt_worker_data, cm, num_sbs,
2850
0
                               pbi->max_threads, num_jobs);
2851
0
    }
2852
0
    vp9_jobq_alloc(pbi);
2853
0
  }
2854
219k
  sz = vpx_rb_read_literal(rb, 16);
2855
2856
219k
  if (sz == 0)
2857
14.7k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2858
14.7k
                       "Invalid header size");
2859
2860
219k
  return sz;
2861
219k
}
2862
2863
static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
2864
100k
                                  size_t partition_size) {
2865
100k
  VP9_COMMON *const cm = &pbi->common;
2866
100k
  MACROBLOCKD *const xd = &pbi->mb;
2867
100k
  FRAME_CONTEXT *const fc = cm->fc;
2868
100k
  vpx_reader r;
2869
100k
  int k;
2870
2871
100k
  if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb,
2872
100k
                      pbi->decrypt_state))
2873
1.69k
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2874
1.69k
                       "Failed to allocate bool decoder 0");
2875
2876
100k
  cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
2877
100k
  if (cm->tx_mode == TX_MODE_SELECT) read_tx_mode_probs(&fc->tx_probs, &r);
2878
100k
  read_coef_probs(fc, cm->tx_mode, &r);
2879
2880
396k
  for (k = 0; k < SKIP_CONTEXTS; ++k)
2881
296k
    vp9_diff_update_prob(&r, &fc->skip_probs[k]);
2882
2883
100k
  if (!frame_is_intra_only(cm)) {
2884
63.4k
    nmv_context *const nmvc = &fc->nmvc;
2885
63.4k
    int i, j;
2886
2887
63.4k
    read_inter_mode_probs(fc, &r);
2888
2889
63.4k
    if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
2890
2891
317k
    for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
2892
253k
      vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
2893
2894
63.4k
    cm->reference_mode = read_frame_reference_mode(cm, &r);
2895
63.4k
    if (cm->reference_mode != SINGLE_REFERENCE)
2896
15.5k
      vp9_setup_compound_reference_mode(cm);
2897
63.4k
    read_frame_reference_mode_probs(cm, &r);
2898
2899
317k
    for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
2900
2.53M
      for (i = 0; i < INTRA_MODES - 1; ++i)
2901
2.28M
        vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
2902
2903
1.07M
    for (j = 0; j < PARTITION_CONTEXTS; ++j)
2904
4.06M
      for (i = 0; i < PARTITION_TYPES - 1; ++i)
2905
3.04M
        vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
2906
2907
63.4k
    read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
2908
63.4k
  }
2909
2910
100k
  return vpx_reader_has_error(&r);
2911
100k
}
2912
2913
static struct vpx_read_bit_buffer *init_read_bit_buffer(
2914
    VP9Decoder *pbi, struct vpx_read_bit_buffer *rb, const uint8_t *data,
2915
225k
    const uint8_t *data_end, uint8_t clear_data[MAX_VP9_HEADER_SIZE]) {
2916
225k
  rb->bit_offset = 0;
2917
225k
  rb->error_handler = error_handler;
2918
225k
  rb->error_handler_data = &pbi->common;
2919
225k
  if (pbi->decrypt_cb) {
2920
0
    const int n = (int)VPXMIN(MAX_VP9_HEADER_SIZE, data_end - data);
2921
0
    pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
2922
0
    rb->bit_buffer = clear_data;
2923
0
    rb->bit_buffer_end = clear_data + n;
2924
225k
  } else {
2925
225k
    rb->bit_buffer = data;
2926
225k
    rb->bit_buffer_end = data_end;
2927
225k
  }
2928
225k
  return rb;
2929
225k
}
2930
2931
//------------------------------------------------------------------------------
2932
2933
119k
int vp9_read_sync_code(struct vpx_read_bit_buffer *const rb) {
2934
119k
  return vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 &&
2935
119k
         vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 &&
2936
119k
         vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2;
2937
119k
}
2938
2939
void vp9_read_frame_size(struct vpx_read_bit_buffer *rb, int *width,
2940
126k
                         int *height) {
2941
126k
  *width = vpx_rb_read_literal(rb, 16) + 1;
2942
126k
  *height = vpx_rb_read_literal(rb, 16) + 1;
2943
126k
}
2944
2945
216k
BITSTREAM_PROFILE vp9_read_profile(struct vpx_read_bit_buffer *rb) {
2946
216k
  int profile = vpx_rb_read_bit(rb);
2947
216k
  profile |= vpx_rb_read_bit(rb) << 1;
2948
216k
  if (profile > 2) profile += vpx_rb_read_bit(rb);
2949
216k
  return (BITSTREAM_PROFILE)profile;
2950
216k
}
2951
2952
void vp9_decode_frame(VP9Decoder *pbi, const uint8_t *data,
2953
225k
                      const uint8_t *data_end, const uint8_t **p_data_end) {
2954
225k
  VP9_COMMON *const cm = &pbi->common;
2955
225k
  MACROBLOCKD *const xd = &pbi->mb;
2956
225k
  struct vpx_read_bit_buffer rb;
2957
225k
  int context_updated = 0;
2958
225k
  uint8_t clear_data[MAX_VP9_HEADER_SIZE];
2959
225k
  const size_t first_partition_size = read_uncompressed_header(
2960
225k
      pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
2961
225k
  const int tile_rows = 1 << cm->log2_tile_rows;
2962
225k
  const int tile_cols = 1 << cm->log2_tile_cols;
2963
225k
  YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
2964
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
2965
  bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
2966
#endif
2967
#if CONFIG_MISMATCH_DEBUG
2968
  mismatch_move_frame_idx_r();
2969
#endif
2970
225k
  xd->cur_buf = new_fb;
2971
2972
225k
  if (!first_partition_size) {
2973
    // showing a frame directly
2974
4.79k
    *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
2975
4.79k
    return;
2976
4.79k
  }
2977
2978
220k
  data += vpx_rb_bytes_read(&rb);
2979
220k
  if (!read_is_valid(data, first_partition_size, data_end))
2980
36.7k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2981
36.7k
                       "Truncated packet or corrupt header length");
2982
2983
220k
  cm->use_prev_frame_mvs =
2984
220k
      !cm->error_resilient_mode && cm->width == cm->last_width &&
2985
220k
      cm->height == cm->last_height && !cm->last_intra_only &&
2986
220k
      cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
2987
2988
220k
  vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
2989
2990
220k
  *cm->fc = cm->frame_contexts[cm->frame_context_idx];
2991
220k
  if (!cm->fc->initialized)
2992
0
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2993
0
                       "Uninitialized entropy context.");
2994
2995
220k
  xd->corrupted = 0;
2996
220k
  new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
2997
220k
  if (new_fb->corrupted)
2998
797
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2999
797
                       "Decode failed. Frame data header is corrupted.");
3000
3001
220k
  if (cm->lf.filter_level && !cm->skip_loop_filter) {
3002
55.7k
    vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
3003
55.7k
  }
3004
3005
220k
  if (pbi->tile_worker_data == NULL ||
3006
220k
      (tile_cols * tile_rows) != pbi->total_tiles) {
3007
14.9k
    const int num_tile_workers =
3008
14.9k
        tile_cols * tile_rows + ((pbi->max_threads > 1) ? pbi->max_threads : 0);
3009
14.9k
    const size_t twd_size = num_tile_workers * sizeof(*pbi->tile_worker_data);
3010
    // Ensure tile data offsets will be properly aligned. This may fail on
3011
    // platforms without DECLARE_ALIGNED().
3012
14.9k
    assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3013
14.9k
    vpx_free(pbi->tile_worker_data);
3014
14.9k
    CHECK_MEM_ERROR(&cm->error, pbi->tile_worker_data,
3015
14.9k
                    vpx_memalign(32, twd_size));
3016
14.9k
    pbi->total_tiles = tile_rows * tile_cols;
3017
14.9k
  }
3018
3019
220k
  if (pbi->max_threads > 1 && tile_rows == 1 &&
3020
220k
      (tile_cols > 1 || pbi->row_mt == 1)) {
3021
11.6k
    if (pbi->row_mt == 1) {
3022
0
      *p_data_end =
3023
0
          decode_tiles_row_wise_mt(pbi, data + first_partition_size, data_end);
3024
11.6k
    } else {
3025
      // Multi-threaded tile decoder
3026
11.6k
      *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
3027
11.6k
      if (!pbi->lpf_mt_opt) {
3028
4.77k
        if (!xd->corrupted) {
3029
4.20k
          if (!cm->skip_loop_filter) {
3030
            // If multiple threads are used to decode tiles, then we use those
3031
            // threads to do parallel loopfiltering.
3032
4.20k
            vp9_loop_filter_frame_mt(
3033
4.20k
                new_fb, cm, pbi->mb.plane, cm->lf.filter_level, 0, 0,
3034
4.20k
                pbi->tile_workers, pbi->num_tile_workers, &pbi->lf_row_sync);
3035
4.20k
          }
3036
4.20k
        } else {
3037
565
          vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
3038
565
                             "Decode failed. Frame data is corrupted.");
3039
565
        }
3040
4.77k
      }
3041
11.6k
    }
3042
208k
  } else {
3043
208k
    *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
3044
208k
  }
3045
3046
220k
  if (!xd->corrupted) {
3047
76.6k
    if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
3048
35.3k
      vp9_adapt_coef_probs(cm);
3049
3050
35.3k
      if (!frame_is_intra_only(cm)) {
3051
23.7k
        vp9_adapt_mode_probs(cm);
3052
23.7k
        vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
3053
23.7k
      }
3054
35.3k
    }
3055
143k
  } else {
3056
143k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
3057
143k
                       "Decode failed. Frame data is corrupted.");
3058
143k
  }
3059
3060
  // Non frame parallel update frame context here.
3061
220k
  if (cm->refresh_frame_context && !context_updated)
3062
41.9k
    cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3063
220k
}