Coverage Report

Created: 2024-05-20 06:40

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