Coverage Report

Created: 2025-07-18 06:30

/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
300k
static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
60
300k
  return len != 0 && len <= (size_t)(end - start);
61
300k
}
62
63
106k
static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) {
64
106k
  const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max));
65
106k
  return data > max ? max : data;
66
106k
}
67
68
89.9k
static TX_MODE read_tx_mode(vpx_reader *r) {
69
89.9k
  TX_MODE tx_mode = vpx_read_literal(r, 2);
70
89.9k
  if (tx_mode == ALLOW_32X32) tx_mode += vpx_read_bit(r);
71
89.9k
  assert(tx_mode < TX_MODES);
72
89.9k
  return tx_mode;
73
89.9k
}
74
75
11.8k
static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) {
76
11.8k
  int i, j;
77
78
35.5k
  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
79
47.4k
    for (j = 0; j < TX_SIZES - 3; ++j)
80
23.7k
      vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
81
82
35.5k
  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
83
71.1k
    for (j = 0; j < TX_SIZES - 2; ++j)
84
47.4k
      vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
85
86
35.5k
  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
87
94.8k
    for (j = 0; j < TX_SIZES - 1; ++j)
88
71.1k
      vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
89
11.8k
}
90
91
19.3k
static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
92
19.3k
  int i, j;
93
96.8k
  for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
94
232k
    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
95
154k
      vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
96
19.3k
}
97
98
64.4k
static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
99
64.4k
  int i, j;
100
515k
  for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
101
1.80M
    for (j = 0; j < INTER_MODES - 1; ++j)
102
1.35M
      vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
103
64.4k
}
104
105
static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
106
64.4k
                                                vpx_reader *r) {
107
64.4k
  if (vp9_compound_reference_allowed(cm)) {
108
56.3k
    return vpx_read_bit(r)
109
56.3k
               ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT : COMPOUND_REFERENCE)
110
56.3k
               : SINGLE_REFERENCE;
111
56.3k
  } else {
112
8.10k
    return SINGLE_REFERENCE;
113
8.10k
  }
114
64.4k
}
115
116
64.4k
static void read_frame_reference_mode_probs(VP9_COMMON *cm, vpx_reader *r) {
117
64.4k
  FRAME_CONTEXT *const fc = cm->fc;
118
64.4k
  int i;
119
120
64.4k
  if (cm->reference_mode == REFERENCE_MODE_SELECT)
121
61.8k
    for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
122
51.5k
      vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
123
124
64.4k
  if (cm->reference_mode != COMPOUND_REFERENCE)
125
329k
    for (i = 0; i < REF_CONTEXTS; ++i) {
126
274k
      vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
127
274k
      vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
128
274k
    }
129
130
64.4k
  if (cm->reference_mode != SINGLE_REFERENCE)
131
119k
    for (i = 0; i < REF_CONTEXTS; ++i)
132
99.4k
      vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
133
64.4k
}
134
135
1.13M
static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) {
136
1.13M
  int i;
137
5.49M
  for (i = 0; i < n; ++i)
138
4.35M
    if (vpx_read(r, MV_UPDATE_PROB)) p[i] = (vpx_read_literal(r, 7) << 1) | 1;
139
1.13M
}
140
141
64.4k
static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) {
142
64.4k
  int i, j;
143
144
64.4k
  update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
145
146
193k
  for (i = 0; i < 2; ++i) {
147
128k
    nmv_component *const comp_ctx = &ctx->comps[i];
148
128k
    update_mv_probs(&comp_ctx->sign, 1, r);
149
128k
    update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
150
128k
    update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
151
128k
    update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
152
128k
  }
153
154
193k
  for (i = 0; i < 2; ++i) {
155
128k
    nmv_component *const comp_ctx = &ctx->comps[i];
156
386k
    for (j = 0; j < CLASS0_SIZE; ++j)
157
257k
      update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
158
128k
    update_mv_probs(comp_ctx->fp, 3, r);
159
128k
  }
160
161
64.4k
  if (allow_hp) {
162
127k
    for (i = 0; i < 2; ++i) {
163
84.9k
      nmv_component *const comp_ctx = &ctx->comps[i];
164
84.9k
      update_mv_probs(&comp_ctx->class0_hp, 1, r);
165
84.9k
      update_mv_probs(&comp_ctx->hp, 1, r);
166
84.9k
    }
167
42.4k
  }
168
64.4k
}
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
90.1k
    uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
179
90.1k
    if (xd->lossless) {
180
8.36k
      vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
181
81.8k
    } else {
182
81.8k
      switch (tx_size) {
183
41.1k
        case TX_4X4:
184
41.1k
          vp9_highbd_idct4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
185
41.1k
          break;
186
15.9k
        case TX_8X8:
187
15.9k
          vp9_highbd_idct8x8_add(dqcoeff, dst16, stride, eob, xd->bd);
188
15.9k
          break;
189
12.6k
        case TX_16X16:
190
12.6k
          vp9_highbd_idct16x16_add(dqcoeff, dst16, stride, eob, xd->bd);
191
12.6k
          break;
192
12.0k
        case TX_32X32:
193
12.0k
          vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd);
194
12.0k
          break;
195
0
        default: assert(0 && "Invalid transform size");
196
81.8k
      }
197
81.8k
    }
198
1.87M
  } else {
199
1.87M
    if (xd->lossless) {
200
3.79k
      vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
201
1.86M
    } else {
202
1.86M
      switch (tx_size) {
203
665k
        case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
204
577k
        case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
205
351k
        case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
206
274k
        case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
207
0
        default: assert(0 && "Invalid transform size"); return;
208
1.86M
      }
209
1.86M
    }
210
1.87M
  }
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
468k
    dqcoeff[0] = 0;
227
1.49M
  } else {
228
1.49M
    if (tx_size <= TX_16X16 && eob <= 10)
229
612k
      memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
230
881k
    else if (tx_size == TX_32X32 && eob <= 34)
231
114k
      memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
232
766k
    else
233
766k
      memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
234
1.49M
  }
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.21M
                                          int stride, int eob) {
241
2.21M
  struct macroblockd_plane *const pd = &xd->plane[plane];
242
2.21M
  tran_low_t *const dqcoeff = pd->dqcoeff;
243
2.21M
  assert(eob > 0);
244
2.21M
#if CONFIG_VP9_HIGHBITDEPTH
245
2.21M
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
246
910k
    uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
247
910k
    if (xd->lossless) {
248
276k
      vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
249
634k
    } else {
250
634k
      switch (tx_size) {
251
186k
        case TX_4X4:
252
186k
          vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
253
186k
          break;
254
154k
        case TX_8X8:
255
154k
          vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
256
154k
          break;
257
126k
        case TX_16X16:
258
126k
          vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
259
126k
          break;
260
179k
        case TX_32X32:
261
179k
          vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd);
262
179k
          break;
263
0
        default: assert(0 && "Invalid transform size");
264
634k
      }
265
634k
    }
266
1.30M
  } else {
267
1.30M
    if (xd->lossless) {
268
78.0k
      vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
269
1.22M
    } else {
270
1.22M
      switch (tx_size) {
271
545k
        case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
272
300k
        case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
273
177k
        case TX_16X16:
274
177k
          vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
275
177k
          break;
276
223k
        case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
277
0
        default: assert(0 && "Invalid transform size"); return;
278
1.22M
      }
279
1.22M
    }
280
1.30M
  }
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.23M
  if (eob == 1) {
298
586k
    dqcoeff[0] = 0;
299
1.64M
  } else {
300
1.64M
    if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
301
525k
      memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
302
1.11M
    else if (tx_size == TX_32X32 && eob <= 34)
303
220k
      memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
304
898k
    else
305
898k
      memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
306
1.64M
  }
307
2.23M
}
308
309
static void predict_and_reconstruct_intra_block(TileWorkerData *twd,
310
                                                MODE_INFO *const mi, int plane,
311
                                                int row, int col,
312
623M
                                                TX_SIZE tx_size) {
313
623M
  MACROBLOCKD *const xd = &twd->xd;
314
623M
  struct macroblockd_plane *const pd = &xd->plane[plane];
315
623M
  PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
316
623M
  uint8_t *dst;
317
623M
  dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
318
319
623M
  if (mi->sb_type < BLOCK_8X8)
320
5.32M
    if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
321
322
623M
  vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride,
323
623M
                          dst, pd->dst.stride, col, row, plane);
324
325
623M
  if (!mi->skip) {
326
197M
    const TX_TYPE tx_type =
327
197M
        (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
328
197M
    const ScanOrder *sc = (plane || xd->lossless)
329
197M
                              ? &vp9_default_scan_orders[tx_size]
330
197M
                              : &vp9_scan_orders[tx_size][tx_type];
331
197M
    const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
332
197M
                                            mi->segment_id);
333
197M
    if (eob > 0) {
334
2.21M
      inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
335
2.21M
                                    pd->dst.stride, eob);
336
2.21M
    }
337
197M
  }
338
623M
}
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
6.23M
                                   int mi_row, int mi_col) {
396
6.23M
  MACROBLOCKD *const xd = &twd->xd;
397
6.23M
  struct macroblockd_plane *const pd = &xd->plane[plane];
398
6.23M
  const ScanOrder *sc = &vp9_default_scan_orders[tx_size];
399
6.23M
  const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
400
6.23M
                                          mi->segment_id);
401
6.23M
  uint8_t *dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
402
403
6.23M
  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
6.23M
  (void)mi_row;
418
6.23M
  (void)mi_col;
419
6.23M
#endif
420
6.23M
  return eob;
421
6.23M
}
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.19M
                            int w, int h) {
461
  // Get a pointer to the start of the real data for this row.
462
4.19M
  const uint8_t *ref_row = src - x - y * src_stride;
463
464
4.19M
  if (y >= h)
465
226k
    ref_row += (h - 1) * src_stride;
466
3.96M
  else if (y > 0)
467
2.21M
    ref_row += y * src_stride;
468
469
59.4M
  do {
470
59.4M
    int right = 0, copy;
471
59.4M
    int left = x < 0 ? -x : 0;
472
473
59.4M
    if (left > b_w) left = b_w;
474
475
59.4M
    if (x + b_w > w) right = x + b_w - w;
476
477
59.4M
    if (right > b_w) right = b_w;
478
479
59.4M
    copy = b_w - left - right;
480
481
59.4M
    if (left) memset(dst, ref_row[0], left);
482
483
59.4M
    if (copy) memcpy(dst + left, ref_row + x + left, copy);
484
485
59.4M
    if (right) memset(dst + left + copy, ref_row[w - 1], right);
486
487
59.4M
    dst += dst_stride;
488
59.4M
    ++y;
489
490
59.4M
    if (y > 0 && y < h) ref_row += src_stride;
491
59.4M
  } while (--b_h);
492
4.19M
}
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.37M
                                 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.37M
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
500
1.37M
  const uint16_t *ref_row = src - x - y * src_stride;
501
502
1.37M
  if (y >= h)
503
130k
    ref_row += (h - 1) * src_stride;
504
1.24M
  else if (y > 0)
505
783k
    ref_row += y * src_stride;
506
507
26.4M
  do {
508
26.4M
    int right = 0, copy;
509
26.4M
    int left = x < 0 ? -x : 0;
510
511
26.4M
    if (left > b_w) left = b_w;
512
513
26.4M
    if (x + b_w > w) right = x + b_w - w;
514
515
26.4M
    if (right > b_w) right = b_w;
516
517
26.4M
    copy = b_w - left - right;
518
519
26.4M
    if (left) vpx_memset16(dst, ref_row[0], left);
520
521
26.4M
    if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
522
523
26.4M
    if (right) vpx_memset16(dst + left + copy, ref_row[w - 1], right);
524
525
26.4M
    dst += dst_stride;
526
26.4M
    ++y;
527
528
26.4M
    if (y > 0 && y < h) ref_row += src_stride;
529
26.4M
  } while (--b_h);
530
1.37M
}
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
5.56M
                               int w, int h, int ref, int xs, int ys) {
542
5.56M
  uint16_t *mc_buf_high = twd->extend_and_predict_buf;
543
5.56M
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
544
1.37M
    high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w, x0, y0,
545
1.37M
                         b_w, b_h, frame_width, frame_height);
546
1.37M
    highbd_inter_predictor(mc_buf_high + border_offset, b_w,
547
1.37M
                           CONVERT_TO_SHORTPTR(dst), dst_buf_stride, subpel_x,
548
1.37M
                           subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
549
4.19M
  } else {
550
4.19M
    build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w, x0,
551
4.19M
                    y0, b_w, b_h, frame_width, frame_height);
552
4.19M
    inter_predictor(((uint8_t *)mc_buf_high) + border_offset, b_w, dst,
553
4.19M
                    dst_buf_stride, subpel_x, subpel_y, sf, w, h, ref, kernel,
554
4.19M
                    xs, ys);
555
4.19M
  }
556
5.56M
}
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
45.9M
    int is_scaled, int ref) {
584
45.9M
  struct macroblockd_plane *const pd = &xd->plane[plane];
585
45.9M
  uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
586
45.9M
  MV32 scaled_mv;
587
45.9M
  int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
588
45.9M
      subpel_x, subpel_y;
589
45.9M
  uint8_t *ref_frame, *buf_ptr;
590
591
  // Get reference frame pointer, width and height.
592
45.9M
  if (plane == 0) {
593
19.0M
    frame_width = ref_frame_buf->buf.y_crop_width;
594
19.0M
    frame_height = ref_frame_buf->buf.y_crop_height;
595
19.0M
    ref_frame = ref_frame_buf->buf.y_buffer;
596
26.8M
  } else {
597
26.8M
    frame_width = ref_frame_buf->buf.uv_crop_width;
598
26.8M
    frame_height = ref_frame_buf->buf.uv_crop_height;
599
26.8M
    ref_frame =
600
26.8M
        plane == 1 ? ref_frame_buf->buf.u_buffer : ref_frame_buf->buf.v_buffer;
601
26.8M
  }
602
603
45.9M
  if (is_scaled) {
604
12.7M
    const MV mv_q4 = clamp_mv_to_umv_border_sb(
605
12.7M
        xd, mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
606
    // Co-ordinate of containing block to pixel precision.
607
12.7M
    int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
608
12.7M
    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
12.7M
    x0_16 = (x_start + x) << SUBPEL_BITS;
617
12.7M
    y0_16 = (y_start + y) << SUBPEL_BITS;
618
619
    // Co-ordinate of current block in reference frame
620
    // to 1/16th pixel precision.
621
12.7M
    x0_16 = sf->scale_value_x(x0_16, sf);
622
12.7M
    y0_16 = sf->scale_value_y(y0_16, sf);
623
624
    // Map the top left corner of the block into the reference frame.
625
12.7M
    x0 = sf->scale_value_x(x_start + x, sf);
626
12.7M
    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
12.7M
    scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
631
12.7M
    xs = sf->x_step_q4;
632
12.7M
    ys = sf->y_step_q4;
633
33.1M
  } else {
634
    // Co-ordinate of containing block to pixel precision.
635
33.1M
    x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
636
33.1M
    y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
637
638
    // Co-ordinate of the block to 1/16th pixel precision.
639
33.1M
    x0_16 = x0 << SUBPEL_BITS;
640
33.1M
    y0_16 = y0 << SUBPEL_BITS;
641
642
33.1M
    scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
643
33.1M
    scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
644
33.1M
    xs = ys = 16;
645
33.1M
  }
646
45.9M
  subpel_x = scaled_mv.col & SUBPEL_MASK;
647
45.9M
  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
45.9M
  x0 += scaled_mv.col >> SUBPEL_BITS;
652
45.9M
  y0 += scaled_mv.row >> SUBPEL_BITS;
653
45.9M
  x0_16 += scaled_mv.col;
654
45.9M
  y0_16 += scaled_mv.row;
655
656
  // Get reference block pointer.
657
45.9M
  buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
658
45.9M
  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
45.9M
  if (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
663
45.9M
      (frame_height & 0x7)) {
664
44.7M
    int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
665
666
    // Get reference block bottom right horizontal coordinate.
667
44.7M
    int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
668
44.7M
    int x_pad = 0, y_pad = 0;
669
670
44.7M
    if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
671
29.9M
      x0 -= VP9_INTERP_EXTEND - 1;
672
29.9M
      x1 += VP9_INTERP_EXTEND;
673
29.9M
      x_pad = 1;
674
29.9M
    }
675
676
44.7M
    if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
677
35.0M
      y0 -= VP9_INTERP_EXTEND - 1;
678
35.0M
      y1 += VP9_INTERP_EXTEND;
679
35.0M
      y_pad = 1;
680
35.0M
    }
681
682
    // Skip border extension if block is inside the frame.
683
44.7M
    if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
684
44.7M
        y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
685
      // Extend the border.
686
5.56M
      const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
687
5.56M
      const int b_w = x1 - x0 + 1;
688
5.56M
      const int b_h = y1 - y0 + 1;
689
5.56M
      const int border_offset = y_pad * 3 * b_w + x_pad * 3;
690
691
5.56M
      extend_and_predict(twd, buf_ptr1, buf_stride, x0, y0, b_w, b_h,
692
5.56M
                         frame_width, frame_height, border_offset, dst,
693
5.56M
                         dst_buf->stride, subpel_x, subpel_y, kernel, sf,
694
5.56M
#if CONFIG_VP9_HIGHBITDEPTH
695
5.56M
                         xd,
696
5.56M
#endif
697
5.56M
                         w, h, ref, xs, ys);
698
5.56M
      return;
699
5.56M
    }
700
44.7M
  }
701
40.3M
#if CONFIG_VP9_HIGHBITDEPTH
702
40.3M
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
703
2.69M
    highbd_inter_predictor(CONVERT_TO_SHORTPTR(buf_ptr), buf_stride,
704
2.69M
                           CONVERT_TO_SHORTPTR(dst), dst_buf->stride, subpel_x,
705
2.69M
                           subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
706
37.6M
  } else {
707
37.6M
    inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
708
37.6M
                    subpel_y, sf, w, h, ref, kernel, xs, ys);
709
37.6M
  }
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
40.3M
}
715
716
static void dec_build_inter_predictors_sb(TileWorkerData *twd,
717
                                          VP9Decoder *const pbi,
718
                                          MACROBLOCKD *xd, int mi_row,
719
8.81M
                                          int mi_col) {
720
8.81M
  int plane;
721
8.81M
  const int mi_x = mi_col * MI_SIZE;
722
8.81M
  const int mi_y = mi_row * MI_SIZE;
723
8.81M
  const MODE_INFO *mi = xd->mi[0];
724
8.81M
  const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
725
8.81M
  const BLOCK_SIZE sb_type = mi->sb_type;
726
8.81M
  const int is_compound = has_second_ref(mi);
727
8.81M
  int ref;
728
8.81M
  int is_scaled;
729
730
22.2M
  for (ref = 0; ref < 1 + is_compound; ++ref) {
731
13.3M
    const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
732
13.3M
    RefBuffer *ref_buf = &pbi->common.frame_refs[frame - LAST_FRAME];
733
13.3M
    const struct scale_factors *const sf = &ref_buf->sf;
734
13.3M
    const int idx = ref_buf->idx;
735
13.3M
    BufferPool *const pool = pbi->common.buffer_pool;
736
13.3M
    RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
737
738
13.3M
    if (!vp9_is_valid_scale(sf))
739
17
      vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
740
17
                         "Reference frame has invalid dimensions");
741
742
13.3M
    is_scaled = vp9_is_scaled(sf);
743
13.3M
    vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
744
13.3M
                         is_scaled ? sf : NULL);
745
13.3M
    xd->block_refs[ref] = ref_buf;
746
747
13.3M
    if (sb_type < BLOCK_8X8) {
748
7.60M
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
749
5.70M
        struct macroblockd_plane *const pd = &xd->plane[plane];
750
5.70M
        struct buf_2d *const dst_buf = &pd->dst;
751
5.70M
        const int num_4x4_w = pd->n4_w;
752
5.70M
        const int num_4x4_h = pd->n4_h;
753
5.70M
        const int n4w_x4 = 4 * num_4x4_w;
754
5.70M
        const int n4h_x4 = 4 * num_4x4_h;
755
5.70M
        struct buf_2d *const pre_buf = &pd->pre[ref];
756
5.70M
        int i = 0, x, y;
757
13.3M
        for (y = 0; y < num_4x4_h; ++y) {
758
19.2M
          for (x = 0; x < num_4x4_w; ++x) {
759
11.5M
            const MV mv = average_split_mvs(pd, mi, ref, i++);
760
11.5M
            dec_build_inter_predictors(twd, xd, plane, n4w_x4, n4h_x4, 4 * x,
761
11.5M
                                       4 * y, 4, 4, mi_x, mi_y, kernel, sf,
762
11.5M
                                       pre_buf, dst_buf, &mv, ref_frame_buf,
763
11.5M
                                       is_scaled, ref);
764
11.5M
          }
765
7.66M
        }
766
5.70M
      }
767
11.4M
    } else {
768
11.4M
      const MV mv = mi->mv[ref].as_mv;
769
45.8M
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
770
34.3M
        struct macroblockd_plane *const pd = &xd->plane[plane];
771
34.3M
        struct buf_2d *const dst_buf = &pd->dst;
772
34.3M
        const int num_4x4_w = pd->n4_w;
773
34.3M
        const int num_4x4_h = pd->n4_h;
774
34.3M
        const int n4w_x4 = 4 * num_4x4_w;
775
34.3M
        const int n4h_x4 = 4 * num_4x4_h;
776
34.3M
        struct buf_2d *const pre_buf = &pd->pre[ref];
777
34.3M
        dec_build_inter_predictors(twd, xd, plane, n4w_x4, n4h_x4, 0, 0, n4w_x4,
778
34.3M
                                   n4h_x4, mi_x, mi_y, kernel, sf, pre_buf,
779
34.3M
                                   dst_buf, &mv, ref_frame_buf, is_scaled, ref);
780
34.3M
      }
781
11.4M
    }
782
13.3M
  }
783
8.81M
}
784
785
13.0M
static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
786
13.0M
  int i;
787
51.9M
  for (i = 0; i < MAX_MB_PLANE; i++) {
788
38.9M
    struct macroblockd_plane *const pd = &xd->plane[i];
789
38.9M
    memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
790
38.9M
    memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
791
38.9M
  }
792
13.0M
}
793
794
static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
795
16.4M
                         int bhl) {
796
16.4M
  int i;
797
65.6M
  for (i = 0; i < MAX_MB_PLANE; i++) {
798
49.2M
    xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
799
49.2M
    xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
800
49.2M
    xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
801
49.2M
    xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
802
49.2M
  }
803
16.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
16.4M
                              int bh, int x_mis, int y_mis, int bwl, int bhl) {
827
16.4M
  const int offset = mi_row * cm->mi_stride + mi_col;
828
16.4M
  int x, y;
829
16.4M
  const TileInfo *const tile = &xd->tile;
830
831
16.4M
  xd->mi = cm->mi_grid_visible + offset;
832
16.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
16.4M
  xd->mi[0]->sb_type = bsize;
836
67.4M
  for (y = 0; y < y_mis; ++y)
837
316M
    for (x = !y; x < x_mis; ++x) {
838
265M
      xd->mi[y * cm->mi_stride + x] = xd->mi[0];
839
265M
    }
840
841
16.4M
  set_plane_n4(xd, bw, bh, bwl, bhl);
842
843
16.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
16.4M
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
848
849
16.4M
  vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
850
16.4M
  return xd->mi[0];
851
16.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
16.4M
                         int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
915
16.4M
  VP9_COMMON *const cm = &pbi->common;
916
16.4M
  const int less8x8 = bsize < BLOCK_8X8;
917
16.4M
  const int bw = 1 << (bwl - 1);
918
16.4M
  const int bh = 1 << (bhl - 1);
919
16.4M
  const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
920
16.4M
  const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
921
16.4M
  vpx_reader *r = &twd->bit_reader;
922
16.4M
  MACROBLOCKD *const xd = &twd->xd;
923
924
16.4M
  MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
925
16.4M
                              y_mis, bwl, bhl);
926
927
16.4M
  if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
928
13.7M
    const BLOCK_SIZE uv_subsize =
929
13.7M
        ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
930
13.7M
    if (uv_subsize == BLOCK_INVALID)
931
242
      vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
932
242
                         "Invalid block size.");
933
13.7M
  }
934
935
16.4M
  vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis);
936
937
16.4M
  if (mi->skip) {
938
13.0M
    dec_reset_skip_context(xd);
939
13.0M
  }
940
941
16.4M
  if (!is_inter_block(mi)) {
942
7.58M
    int plane;
943
30.2M
    for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
944
22.6M
      const struct macroblockd_plane *const pd = &xd->plane[plane];
945
22.6M
      const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
946
22.6M
      const int num_4x4_w = pd->n4_w;
947
22.6M
      const int num_4x4_h = pd->n4_h;
948
22.6M
      const int step = (1 << tx_size);
949
22.6M
      int row, col;
950
22.6M
      const int max_blocks_wide =
951
22.6M
          num_4x4_w + (xd->mb_to_right_edge >= 0
952
22.6M
                           ? 0
953
22.6M
                           : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
954
22.6M
      const int max_blocks_high =
955
22.6M
          num_4x4_h + (xd->mb_to_bottom_edge >= 0
956
22.6M
                           ? 0
957
22.6M
                           : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
958
959
22.6M
      xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
960
22.6M
      xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
961
962
101M
      for (row = 0; row < max_blocks_high; row += step)
963
701M
        for (col = 0; col < max_blocks_wide; col += step)
964
622M
          predict_and_reconstruct_intra_block(twd, mi, plane, row, col,
965
622M
                                              tx_size);
966
22.6M
    }
967
8.82M
  } else {
968
    // Prediction
969
8.82M
    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
8.82M
    if (!mi->skip) {
991
1.10M
      int eobtotal = 0;
992
1.10M
      int plane;
993
994
4.43M
      for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
995
3.32M
        const struct macroblockd_plane *const pd = &xd->plane[plane];
996
3.32M
        const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
997
3.32M
        const int num_4x4_w = pd->n4_w;
998
3.32M
        const int num_4x4_h = pd->n4_h;
999
3.32M
        const int step = (1 << tx_size);
1000
3.32M
        int row, col;
1001
3.32M
        const int max_blocks_wide =
1002
3.32M
            num_4x4_w + (xd->mb_to_right_edge >= 0
1003
3.32M
                             ? 0
1004
3.32M
                             : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
1005
3.32M
        const int max_blocks_high =
1006
3.32M
            num_4x4_h +
1007
3.32M
            (xd->mb_to_bottom_edge >= 0
1008
3.32M
                 ? 0
1009
3.32M
                 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
1010
1011
3.32M
        xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
1012
3.32M
        xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
1013
1014
7.46M
        for (row = 0; row < max_blocks_high; row += step)
1015
10.3M
          for (col = 0; col < max_blocks_wide; col += step)
1016
6.23M
            eobtotal += reconstruct_inter_block(twd, mi, plane, row, col,
1017
6.23M
                                                tx_size, mi_row, mi_col);
1018
3.32M
      }
1019
1020
1.10M
      if (!less8x8 && eobtotal == 0) mi->skip = 1;  // skip loopfilter
1021
1.10M
    }
1022
8.82M
  }
1023
1024
16.4M
  xd->corrupted |= vpx_reader_has_error(r);
1025
1026
16.4M
  if (cm->lf.filter_level) {
1027
14.4M
    vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
1028
14.4M
  }
1029
16.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
18.5M
                                              int mi_col, int bsl) {
1125
18.5M
  const PARTITION_CONTEXT *above_ctx = twd->xd.above_seg_context + mi_col;
1126
18.5M
  const PARTITION_CONTEXT *left_ctx =
1127
18.5M
      twd->xd.left_seg_context + (mi_row & MI_MASK);
1128
18.5M
  int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
1129
1130
  //  assert(bsl >= 0);
1131
1132
18.5M
  return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
1133
18.5M
}
1134
1135
static INLINE void dec_update_partition_context(TileWorkerData *twd, int mi_row,
1136
                                                int mi_col, BLOCK_SIZE subsize,
1137
14.8M
                                                int bw) {
1138
14.8M
  PARTITION_CONTEXT *const above_ctx = twd->xd.above_seg_context + mi_col;
1139
14.8M
  PARTITION_CONTEXT *const left_ctx =
1140
14.8M
      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
14.8M
  memset(above_ctx, partition_context_lookup[subsize].above, bw);
1146
14.8M
  memset(left_ctx, partition_context_lookup[subsize].left, bw);
1147
14.8M
}
1148
1149
static PARTITION_TYPE read_partition(TileWorkerData *twd, int mi_row,
1150
                                     int mi_col, int has_rows, int has_cols,
1151
18.5M
                                     int bsl) {
1152
18.5M
  const int ctx = dec_partition_plane_context(twd, mi_row, mi_col, bsl);
1153
18.5M
  const vpx_prob *const probs = twd->xd.partition_probs[ctx];
1154
18.5M
  FRAME_COUNTS *counts = twd->xd.counts;
1155
18.5M
  PARTITION_TYPE p;
1156
18.5M
  vpx_reader *r = &twd->bit_reader;
1157
1158
18.5M
  if (has_rows && has_cols)
1159
17.4M
    p = (PARTITION_TYPE)vpx_read_tree(r, vp9_partition_tree, probs);
1160
1.07M
  else if (!has_rows && has_cols)
1161
695k
    p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
1162
374k
  else if (has_rows && !has_cols)
1163
314k
    p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
1164
60.1k
  else
1165
60.1k
    p = PARTITION_SPLIT;
1166
1167
18.5M
  if (counts) ++counts->partition[ctx][p];
1168
1169
18.5M
  return p;
1170
18.5M
}
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
19.6M
                             int n4x4_l2) {
1176
19.6M
  VP9_COMMON *const cm = &pbi->common;
1177
19.6M
  const int n8x8_l2 = n4x4_l2 - 1;
1178
19.6M
  const int num_8x8_wh = 1 << n8x8_l2;
1179
19.6M
  const int hbs = num_8x8_wh >> 1;
1180
19.6M
  PARTITION_TYPE partition;
1181
19.6M
  BLOCK_SIZE subsize;
1182
19.6M
  const int has_rows = (mi_row + hbs) < cm->mi_rows;
1183
19.6M
  const int has_cols = (mi_col + hbs) < cm->mi_cols;
1184
19.6M
  MACROBLOCKD *const xd = &twd->xd;
1185
1186
19.6M
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1187
1188
18.5M
  partition = read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2);
1189
18.5M
  subsize = subsize_lookup[partition][bsize];  // get_subsize(bsize, partition);
1190
18.5M
  if (!hbs) {
1191
    // calculate bmode block dimensions (log 2)
1192
5.64M
    xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
1193
5.64M
    xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
1194
5.64M
    decode_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
1195
12.8M
  } else {
1196
12.8M
    switch (partition) {
1197
7.09M
      case PARTITION_NONE:
1198
7.09M
        decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
1199
7.09M
        break;
1200
1.36M
      case PARTITION_HORZ:
1201
1.36M
        decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
1202
1.36M
        if (has_rows)
1203
980k
          decode_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
1204
980k
                       n8x8_l2);
1205
1.36M
        break;
1206
735k
      case PARTITION_VERT:
1207
735k
        decode_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
1208
735k
        if (has_cols)
1209
604k
          decode_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
1210
604k
                       n4x4_l2);
1211
735k
        break;
1212
3.69M
      case PARTITION_SPLIT:
1213
3.69M
        decode_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2);
1214
3.69M
        decode_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2);
1215
3.69M
        decode_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2);
1216
3.69M
        decode_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize,
1217
3.69M
                         n8x8_l2);
1218
3.69M
        break;
1219
0
      default: assert(0 && "Invalid partition type");
1220
12.8M
    }
1221
12.8M
  }
1222
1223
  // update partition context
1224
18.5M
  if (bsize >= BLOCK_8X8 &&
1225
18.5M
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
1226
14.8M
    dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh);
1227
18.5M
}
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
132k
                                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
132k
  if (!read_is_valid(data, read_size, data_end))
1306
15.5k
    vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1307
15.5k
                       "Truncated packet or corrupt tile length");
1308
1309
132k
  if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
1310
5.05k
    vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
1311
5.05k
                       "Failed to allocate bool decoder %d", 1);
1312
132k
}
1313
1314
static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
1315
245k
                                   vpx_reader *r) {
1316
245k
  int i, j, k, l, m;
1317
1318
245k
  if (vpx_read_bit(r))
1319
111k
    for (i = 0; i < PLANE_TYPES; ++i)
1320
222k
      for (j = 0; j < REF_TYPES; ++j)
1321
1.03M
        for (k = 0; k < COEF_BANDS; ++k)
1322
5.78M
          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1323
19.5M
            for (m = 0; m < UNCONSTRAINED_NODES; ++m)
1324
14.6M
              vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
1325
245k
}
1326
1327
103k
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, vpx_reader *r) {
1328
103k
  const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
1329
103k
  TX_SIZE tx_size;
1330
348k
  for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
1331
245k
    read_coef_probs_common(fc->coef_probs[tx_size], r);
1332
103k
}
1333
1334
static void setup_segmentation(struct segmentation *seg,
1335
153k
                               struct vpx_read_bit_buffer *rb) {
1336
153k
  int i, j;
1337
1338
153k
  seg->update_map = 0;
1339
153k
  seg->update_data = 0;
1340
1341
153k
  seg->enabled = vpx_rb_read_bit(rb);
1342
153k
  if (!seg->enabled) return;
1343
1344
  // Segmentation map update
1345
46.0k
  seg->update_map = vpx_rb_read_bit(rb);
1346
46.0k
  if (seg->update_map) {
1347
180k
    for (i = 0; i < SEG_TREE_PROBS; i++)
1348
156k
      seg->tree_probs[i] =
1349
156k
          vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
1350
1351
23.6k
    seg->temporal_update = vpx_rb_read_bit(rb);
1352
23.6k
    if (seg->temporal_update) {
1353
18.0k
      for (i = 0; i < PREDICTION_PROBS; i++)
1354
13.5k
        seg->pred_probs[i] =
1355
13.5k
            vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
1356
19.1k
    } else {
1357
63.8k
      for (i = 0; i < PREDICTION_PROBS; i++) seg->pred_probs[i] = MAX_PROB;
1358
19.1k
    }
1359
23.6k
  }
1360
1361
  // Segmentation data update
1362
46.0k
  seg->update_data = vpx_rb_read_bit(rb);
1363
46.0k
  if (seg->update_data) {
1364
19.7k
    seg->abs_delta = vpx_rb_read_bit(rb);
1365
1366
19.7k
    vp9_clearall_segfeatures(seg);
1367
1368
142k
    for (i = 0; i < MAX_SEGMENTS; i++) {
1369
605k
      for (j = 0; j < SEG_LVL_MAX; j++) {
1370
481k
        int data = 0;
1371
481k
        const int feature_enabled = vpx_rb_read_bit(rb);
1372
481k
        if (feature_enabled) {
1373
106k
          vp9_enable_segfeature(seg, i, j);
1374
106k
          data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
1375
106k
          if (vp9_is_segfeature_signed(j))
1376
46.8k
            data = vpx_rb_read_bit(rb) ? -data : data;
1377
106k
        }
1378
481k
        vp9_set_segdata(seg, i, j, data);
1379
481k
      }
1380
123k
    }
1381
19.7k
  }
1382
46.0k
}
1383
1384
static void setup_loopfilter(struct loopfilter *lf,
1385
158k
                             struct vpx_read_bit_buffer *rb) {
1386
158k
  lf->filter_level = vpx_rb_read_literal(rb, 6);
1387
158k
  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
158k
  lf->mode_ref_delta_update = 0;
1392
1393
158k
  lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb);
1394
158k
  if (lf->mode_ref_delta_enabled) {
1395
52.1k
    lf->mode_ref_delta_update = vpx_rb_read_bit(rb);
1396
52.1k
    if (lf->mode_ref_delta_update) {
1397
15.4k
      int i;
1398
1399
74.8k
      for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1400
59.4k
        if (vpx_rb_read_bit(rb))
1401
24.0k
          lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1402
1403
43.6k
      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1404
28.1k
        if (vpx_rb_read_bit(rb))
1405
9.87k
          lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1406
15.4k
    }
1407
52.1k
  }
1408
158k
}
1409
1410
464k
static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) {
1411
464k
  return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0;
1412
464k
}
1413
1414
static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
1415
156k
                               struct vpx_read_bit_buffer *rb) {
1416
156k
  cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS);
1417
156k
  cm->y_dc_delta_q = read_delta_q(rb);
1418
156k
  cm->uv_dc_delta_q = read_delta_q(rb);
1419
156k
  cm->uv_ac_delta_q = read_delta_q(rb);
1420
156k
  cm->dequant_bit_depth = cm->bit_depth;
1421
156k
  xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
1422
156k
                 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
1423
1424
156k
#if CONFIG_VP9_HIGHBITDEPTH
1425
156k
  xd->bd = (int)cm->bit_depth;
1426
156k
#endif
1427
156k
}
1428
1429
141k
static void setup_segmentation_dequant(VP9_COMMON *const cm) {
1430
  // Build y/uv dequant values based on segmentation.
1431
141k
  if (cm->seg.enabled) {
1432
34.0k
    int i;
1433
306k
    for (i = 0; i < MAX_SEGMENTS; ++i) {
1434
272k
      const int qindex = vp9_get_qindex(&cm->seg, i, cm->base_qindex);
1435
272k
      cm->y_dequant[i][0] =
1436
272k
          vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1437
272k
      cm->y_dequant[i][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1438
272k
      cm->uv_dequant[i][0] =
1439
272k
          vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
1440
272k
      cm->uv_dequant[i][1] =
1441
272k
          vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
1442
272k
    }
1443
107k
  } else {
1444
107k
    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
107k
    cm->y_dequant[0][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1448
107k
    cm->y_dequant[0][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1449
107k
    cm->uv_dequant[0][0] =
1450
107k
        vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
1451
107k
    cm->uv_dequant[0][1] =
1452
107k
        vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
1453
107k
  }
1454
141k
}
1455
1456
67.5k
static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) {
1457
67.5k
  const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH, EIGHTTAP,
1458
67.5k
                                              EIGHTTAP_SHARP, BILINEAR };
1459
67.5k
  return vpx_rb_read_bit(rb) ? SWITCHABLE
1460
67.5k
                             : literal_to_filter[vpx_rb_read_literal(rb, 2)];
1461
67.5k
}
1462
1463
159k
static void setup_render_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1464
159k
  cm->render_width = cm->width;
1465
159k
  cm->render_height = cm->height;
1466
159k
  if (vpx_rb_read_bit(rb))
1467
7.19k
    vp9_read_frame_size(rb, &cm->render_width, &cm->render_height);
1468
159k
}
1469
1470
46.2k
static void resize_mv_buffer(VP9_COMMON *cm) {
1471
46.2k
  vpx_free(cm->cur_frame->mvs);
1472
46.2k
  cm->cur_frame->mi_rows = cm->mi_rows;
1473
46.2k
  cm->cur_frame->mi_cols = cm->mi_cols;
1474
46.2k
  CHECK_MEM_ERROR(&cm->error, cm->cur_frame->mvs,
1475
46.2k
                  (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
1476
46.2k
                                       sizeof(*cm->cur_frame->mvs)));
1477
46.2k
}
1478
1479
160k
static void resize_context_buffers(VP9_COMMON *cm, int width, int height) {
1480
160k
#if CONFIG_SIZE_LIMIT
1481
160k
  if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1482
844
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1483
844
                       "Dimensions of %dx%d beyond allowed size of %dx%d.",
1484
844
                       width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1485
160k
#endif
1486
160k
  if (cm->width != width || cm->height != height) {
1487
88.9k
    const int new_mi_rows =
1488
88.9k
        ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1489
88.9k
    const int new_mi_cols =
1490
88.9k
        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
88.9k
    if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
1495
63.3k
      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
63.3k
    } else {
1505
25.5k
      vp9_set_mb_mi(cm, width, height);
1506
25.5k
    }
1507
88.9k
    vp9_init_context_buffers(cm);
1508
88.9k
    cm->width = width;
1509
88.9k
    cm->height = height;
1510
88.9k
  }
1511
160k
  if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
1512
160k
      cm->mi_cols > cm->cur_frame->mi_cols) {
1513
46.2k
    resize_mv_buffer(cm);
1514
46.2k
  }
1515
160k
}
1516
1517
92.9k
static void setup_frame_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1518
92.9k
  int width, height;
1519
92.9k
  BufferPool *const pool = cm->buffer_pool;
1520
92.9k
  vp9_read_frame_size(rb, &width, &height);
1521
92.9k
  resize_context_buffers(cm, width, height);
1522
92.9k
  setup_render_size(cm, rb);
1523
1524
92.9k
  if (vpx_realloc_frame_buffer(
1525
92.9k
          get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1526
92.9k
          cm->subsampling_y,
1527
92.9k
#if CONFIG_VP9_HIGHBITDEPTH
1528
92.9k
          cm->use_highbitdepth,
1529
92.9k
#endif
1530
92.9k
          VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
1531
92.9k
          &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1532
92.9k
          pool->cb_priv)) {
1533
221
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1534
221
                       "Failed to allocate frame buffer");
1535
221
  }
1536
1537
92.9k
  pool->frame_bufs[cm->new_fb_idx].released = 0;
1538
92.9k
  pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1539
92.9k
  pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1540
92.9k
  pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1541
92.9k
  pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1542
92.9k
  pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1543
92.9k
  pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1544
92.9k
  pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1545
92.9k
}
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
202k
                                          int this_xss, int this_yss) {
1551
202k
  return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
1552
202k
         ref_yss == this_yss;
1553
202k
}
1554
1555
static void setup_frame_size_with_refs(VP9_COMMON *cm,
1556
68.2k
                                       struct vpx_read_bit_buffer *rb) {
1557
68.2k
  int width, height;
1558
68.2k
  int found = 0, i;
1559
68.2k
  int has_valid_ref_frame = 0;
1560
68.2k
  BufferPool *const pool = cm->buffer_pool;
1561
126k
  for (i = 0; i < REFS_PER_FRAME; ++i) {
1562
119k
    if (vpx_rb_read_bit(rb)) {
1563
60.4k
      if (cm->frame_refs[i].idx != INVALID_IDX) {
1564
60.3k
        YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
1565
60.3k
        width = buf->y_crop_width;
1566
60.3k
        height = buf->y_crop_height;
1567
60.3k
        found = 1;
1568
60.3k
        break;
1569
60.3k
      } else {
1570
40
        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1571
40
                           "Failed to decode frame size");
1572
40
      }
1573
60.4k
    }
1574
119k
  }
1575
1576
68.2k
  if (!found) vp9_read_frame_size(rb, &width, &height);
1577
1578
68.2k
  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
272k
  for (i = 0; i < REFS_PER_FRAME; ++i) {
1585
204k
    RefBuffer *const ref_frame = &cm->frame_refs[i];
1586
204k
    has_valid_ref_frame |=
1587
204k
        (ref_frame->idx != INVALID_IDX &&
1588
204k
         valid_ref_frame_size(ref_frame->buf->y_crop_width,
1589
204k
                              ref_frame->buf->y_crop_height, width, height));
1590
204k
  }
1591
68.2k
  if (!has_valid_ref_frame)
1592
512
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1593
512
                       "Referenced frame has invalid size");
1594
271k
  for (i = 0; i < REFS_PER_FRAME; ++i) {
1595
202k
    RefBuffer *const ref_frame = &cm->frame_refs[i];
1596
202k
    if (ref_frame->idx == INVALID_IDX ||
1597
202k
        !valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
1598
202k
                                 ref_frame->buf->subsampling_x,
1599
202k
                                 ref_frame->buf->subsampling_y, cm->bit_depth,
1600
202k
                                 cm->subsampling_x, cm->subsampling_y))
1601
75
      vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1602
75
                         "Referenced frame has incompatible color format");
1603
202k
  }
1604
1605
68.2k
  resize_context_buffers(cm, width, height);
1606
68.2k
  setup_render_size(cm, rb);
1607
1608
68.2k
  if (vpx_realloc_frame_buffer(
1609
68.2k
          get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1610
68.2k
          cm->subsampling_y,
1611
68.2k
#if CONFIG_VP9_HIGHBITDEPTH
1612
68.2k
          cm->use_highbitdepth,
1613
68.2k
#endif
1614
68.2k
          VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
1615
68.2k
          &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1616
68.2k
          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
68.2k
  pool->frame_bufs[cm->new_fb_idx].released = 0;
1622
68.2k
  pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1623
68.2k
  pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1624
68.2k
  pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1625
68.2k
  pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1626
68.2k
  pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1627
68.2k
  pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1628
68.2k
  pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1629
68.2k
}
1630
1631
141k
static void setup_tile_info(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1632
141k
  int min_log2_tile_cols, max_log2_tile_cols, max_ones;
1633
141k
  vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1634
1635
  // columns
1636
141k
  max_ones = max_log2_tile_cols - min_log2_tile_cols;
1637
141k
  cm->log2_tile_cols = min_log2_tile_cols;
1638
159k
  while (max_ones-- && vpx_rb_read_bit(rb)) cm->log2_tile_cols++;
1639
1640
141k
  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
141k
  cm->log2_tile_rows = vpx_rb_read_bit(rb);
1646
141k
  if (cm->log2_tile_rows) cm->log2_tile_rows += vpx_rb_read_bit(rb);
1647
141k
}
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
141k
                            void *decrypt_state, TileBuffer *buf) {
1655
141k
  size_t size;
1656
1657
141k
  if (!is_last) {
1658
39.8k
    if (!read_is_valid(*data, 4, data_end))
1659
202
      vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1660
202
                         "Truncated packet or corrupt tile length");
1661
1662
39.8k
    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
39.8k
    } else {
1667
39.8k
      size = mem_get_be32(*data);
1668
39.8k
    }
1669
39.8k
    *data += 4;
1670
1671
39.8k
    if (size > (size_t)(data_end - *data))
1672
959
      vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1673
959
                         "Truncated packet or corrupt tile size");
1674
101k
  } else {
1675
101k
    size = data_end - *data;
1676
101k
  }
1677
1678
141k
  buf->data = *data;
1679
141k
  buf->size = size;
1680
1681
141k
  *data += size;
1682
141k
}
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
102k
                             TileBuffer (*tile_buffers)[1 << 6]) {
1688
102k
  int r, c;
1689
1690
205k
  for (r = 0; r < tile_rows; ++r) {
1691
244k
    for (c = 0; c < tile_cols; ++c) {
1692
141k
      const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
1693
141k
      TileBuffer *const buf = &tile_buffers[r][c];
1694
141k
      buf->col = c;
1695
141k
      get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
1696
141k
                      pbi->decrypt_cb, pbi->decrypt_state, buf);
1697
141k
    }
1698
103k
  }
1699
102k
}
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
86.0k
                                   const uint8_t *data_end) {
2019
86.0k
  VP9_COMMON *const cm = &pbi->common;
2020
86.0k
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
2021
86.0k
  const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2022
86.0k
  const int tile_cols = 1 << cm->log2_tile_cols;
2023
86.0k
  const int tile_rows = 1 << cm->log2_tile_rows;
2024
86.0k
  TileBuffer tile_buffers[4][1 << 6];
2025
86.0k
  int tile_row, tile_col;
2026
86.0k
  int mi_row, mi_col;
2027
86.0k
  TileWorkerData *tile_data = NULL;
2028
2029
86.0k
  if (cm->lf.filter_level && !cm->skip_loop_filter &&
2030
86.0k
      pbi->lf_worker.data1 == NULL) {
2031
11.9k
    CHECK_MEM_ERROR(&cm->error, pbi->lf_worker.data1,
2032
11.9k
                    vpx_memalign(32, sizeof(LFWorkerData)));
2033
11.9k
    pbi->lf_worker.hook = vp9_loop_filter_worker;
2034
11.9k
    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
11.9k
  }
2039
2040
86.0k
  if (cm->lf.filter_level && !cm->skip_loop_filter) {
2041
59.9k
    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
59.9k
    winterface->sync(&pbi->lf_worker);
2044
59.9k
    vp9_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
2045
59.9k
                               pbi->mb.plane);
2046
59.9k
  }
2047
2048
86.0k
  assert(tile_rows <= 4);
2049
86.0k
  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
86.0k
  memset(cm->above_context, 0,
2054
86.0k
         sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
2055
2056
86.0k
  memset(cm->above_seg_context, 0,
2057
86.0k
         sizeof(*cm->above_seg_context) * aligned_cols);
2058
2059
86.0k
  vp9_reset_lfm(cm);
2060
2061
86.0k
  get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
2062
2063
  // Load all tile information into tile_data.
2064
172k
  for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
2065
173k
    for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
2066
86.9k
      const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
2067
86.9k
      tile_data = pbi->tile_worker_data + tile_cols * tile_row + tile_col;
2068
86.9k
      tile_data->xd = pbi->mb;
2069
86.9k
      tile_data->xd.corrupted = 0;
2070
86.9k
      tile_data->xd.counts =
2071
86.9k
          cm->frame_parallel_decoding_mode ? NULL : &cm->counts;
2072
86.9k
      vp9_zero(tile_data->dqcoeff);
2073
86.9k
      vp9_tile_init(&tile_data->xd.tile, cm, tile_row, tile_col);
2074
86.9k
      setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
2075
86.9k
                          &tile_data->bit_reader, pbi->decrypt_cb,
2076
86.9k
                          pbi->decrypt_state);
2077
86.9k
      vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
2078
86.9k
    }
2079
86.3k
  }
2080
2081
167k
  for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
2082
81.2k
    TileInfo tile;
2083
81.2k
    vp9_tile_set_row(&tile, cm, tile_row);
2084
421k
    for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
2085
340k
         mi_row += MI_BLOCK_SIZE) {
2086
682k
      for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
2087
341k
        const int col =
2088
341k
            pbi->inv_tile_order ? tile_cols - tile_col - 1 : tile_col;
2089
341k
        tile_data = pbi->tile_worker_data + tile_cols * tile_row + col;
2090
341k
        vp9_tile_set_col(&tile, cm, col);
2091
341k
        vp9_zero(tile_data->xd.left_context);
2092
341k
        vp9_zero(tile_data->xd.left_seg_context);
2093
3.22M
        for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
2094
2.87M
             mi_col += MI_BLOCK_SIZE) {
2095
2.87M
          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
2.87M
          } else {
2116
2.87M
            decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
2117
2.87M
          }
2118
2.87M
        }
2119
341k
        pbi->mb.corrupted |= tile_data->xd.corrupted;
2120
341k
        if (pbi->mb.corrupted)
2121
11.2k
          vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2122
11.2k
                             "Failed to decode tile data");
2123
341k
      }
2124
      // Loopfilter one row.
2125
340k
      if (cm->lf.filter_level && !cm->skip_loop_filter) {
2126
250k
        const int lf_start = mi_row - MI_BLOCK_SIZE;
2127
250k
        LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
2128
2129
        // delay the loopfilter by 1 macroblock row.
2130
250k
        if (lf_start < 0) continue;
2131
2132
        // decoding has completed: finish up the loop filter in this thread.
2133
197k
        if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue;
2134
2135
183k
        winterface->sync(&pbi->lf_worker);
2136
183k
        lf_data->start = lf_start;
2137
183k
        lf_data->stop = mi_row;
2138
183k
        if (pbi->max_threads > 1) {
2139
105k
          winterface->launch(&pbi->lf_worker);
2140
105k
        } else {
2141
77.8k
          winterface->execute(&pbi->lf_worker);
2142
77.8k
        }
2143
183k
      }
2144
340k
    }
2145
81.2k
  }
2146
2147
  // Loopfilter remaining rows in the frame.
2148
86.0k
  if (cm->lf.filter_level && !cm->skip_loop_filter) {
2149
50.9k
    LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
2150
50.9k
    winterface->sync(&pbi->lf_worker);
2151
50.9k
    lf_data->start = lf_data->stop;
2152
50.9k
    lf_data->stop = cm->mi_rows;
2153
50.9k
    winterface->execute(&pbi->lf_worker);
2154
50.9k
  }
2155
2156
  // Get last tile data.
2157
86.0k
  tile_data = pbi->tile_worker_data + tile_cols * tile_rows - 1;
2158
2159
86.0k
  return vpx_reader_find_end(&tile_data->bit_reader);
2160
86.0k
}
2161
2162
static void set_rows_after_error(VP9LfSync *lf_sync, int start_row, int mi_rows,
2163
2.17k
                                 int num_tiles_left, int total_num_tiles) {
2164
2.41k
  do {
2165
2.41k
    int mi_row;
2166
2.41k
    const int aligned_rows = mi_cols_aligned_to_sb(mi_rows);
2167
2.41k
    const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2);
2168
2.41k
    const int corrupted = 1;
2169
40.6k
    for (mi_row = start_row; mi_row < mi_rows; mi_row += MI_BLOCK_SIZE) {
2170
38.2k
      const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2);
2171
38.2k
      vp9_set_row(lf_sync, total_num_tiles, mi_row >> MI_BLOCK_SIZE_LOG2,
2172
38.2k
                  is_last_row, corrupted);
2173
38.2k
    }
2174
    /* If there are multiple tiles, the second tile should start marking row
2175
     * progress from row 0.
2176
     */
2177
2.41k
    start_row = 0;
2178
2.41k
  } while (num_tiles_left--);
2179
2.17k
}
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
45.0k
static int tile_worker_hook(void *arg1, void *arg2) {
2185
45.0k
  TileWorkerData *const tile_data = (TileWorkerData *)arg1;
2186
45.0k
  VP9Decoder *const pbi = (VP9Decoder *)arg2;
2187
2188
45.0k
  TileInfo *volatile tile = &tile_data->xd.tile;
2189
45.0k
  const int final_col = (1 << pbi->common.log2_tile_cols) - 1;
2190
45.0k
  const uint8_t *volatile bit_reader_end = NULL;
2191
45.0k
  VP9_COMMON *cm = &pbi->common;
2192
2193
45.0k
  LFWorkerData *lf_data = tile_data->lf_data;
2194
45.0k
  VP9LfSync *lf_sync = tile_data->lf_sync;
2195
2196
45.0k
  volatile int mi_row = 0;
2197
45.0k
  volatile int n = tile_data->buf_start;
2198
45.0k
  if (setjmp(tile_data->error_info.jmp)) {
2199
15.7k
    tile_data->error_info.setjmp = 0;
2200
15.7k
    tile_data->xd.corrupted = 1;
2201
15.7k
    tile_data->data_end = NULL;
2202
15.7k
    if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
2203
1.93k
      const int num_tiles_left = tile_data->buf_end - n;
2204
1.93k
      const int mi_row_start = mi_row;
2205
1.93k
      set_rows_after_error(lf_sync, mi_row_start, cm->mi_rows, num_tiles_left,
2206
1.93k
                           1 << cm->log2_tile_cols);
2207
1.93k
    }
2208
15.7k
    return 0;
2209
15.7k
  }
2210
29.2k
  tile_data->error_info.setjmp = 1;
2211
2212
29.2k
  tile_data->xd.corrupted = 0;
2213
2214
29.6k
  do {
2215
29.6k
    int mi_col;
2216
29.6k
    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
29.6k
    assert(cm->log2_tile_rows == 0);
2222
45.2k
    mi_row = 0;
2223
45.2k
    vp9_zero(tile_data->dqcoeff);
2224
45.2k
    vp9_tile_init(tile, &pbi->common, 0, buf->col);
2225
45.2k
    setup_token_decoder(buf->data, tile_data->data_end, buf->size,
2226
45.2k
                        &tile_data->error_info, &tile_data->bit_reader,
2227
45.2k
                        pbi->decrypt_cb, pbi->decrypt_state);
2228
45.2k
    vp9_init_macroblockd(&pbi->common, &tile_data->xd, tile_data->dqcoeff);
2229
    // init resets xd.error_info
2230
45.2k
    tile_data->xd.error_info = &tile_data->error_info;
2231
2232
249k
    for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
2233
203k
         mi_row += MI_BLOCK_SIZE) {
2234
203k
      vp9_zero(tile_data->xd.left_context);
2235
203k
      vp9_zero(tile_data->xd.left_seg_context);
2236
2.27M
      for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
2237
2.07M
           mi_col += MI_BLOCK_SIZE) {
2238
2.07M
        decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
2239
2.07M
      }
2240
203k
      if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
2241
122k
        const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
2242
122k
        const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2);
2243
122k
        const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2);
2244
122k
        vp9_set_row(lf_sync, 1 << cm->log2_tile_cols,
2245
122k
                    mi_row >> MI_BLOCK_SIZE_LOG2, is_last_row,
2246
122k
                    tile_data->xd.corrupted);
2247
122k
      }
2248
203k
    }
2249
2250
45.2k
    if (buf->col == final_col) {
2251
14.3k
      bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader);
2252
14.3k
    }
2253
45.2k
  } while (!tile_data->xd.corrupted && ++n <= tile_data->buf_end);
2254
2255
44.9k
  if (pbi->lpf_mt_opt && n < tile_data->buf_end && cm->lf.filter_level &&
2256
44.9k
      !cm->skip_loop_filter) {
2257
    /* This was not incremented in the tile loop, so increment before tiles left
2258
     * calculation
2259
     */
2260
238
    ++n;
2261
238
    set_rows_after_error(lf_sync, 0, cm->mi_rows, tile_data->buf_end - n,
2262
238
                         1 << cm->log2_tile_cols);
2263
238
  }
2264
2265
44.9k
  if (pbi->lpf_mt_opt && !tile_data->xd.corrupted && cm->lf.filter_level &&
2266
44.9k
      !cm->skip_loop_filter) {
2267
14.7k
    vp9_loopfilter_rows(lf_data, lf_sync);
2268
14.7k
  }
2269
2270
44.9k
  tile_data->data_end = bit_reader_end;
2271
44.9k
  return !tile_data->xd.corrupted;
2272
29.2k
}
2273
2274
// sorts in descending order
2275
56.8k
static int compare_tile_buffers(const void *a, const void *b) {
2276
56.8k
  const TileBuffer *const buf_a = (const TileBuffer *)a;
2277
56.8k
  const TileBuffer *const buf_b = (const TileBuffer *)b;
2278
56.8k
  return (buf_a->size < buf_b->size) - (buf_a->size > buf_b->size);
2279
56.8k
}
2280
2281
16.5k
static INLINE void init_mt(VP9Decoder *pbi) {
2282
16.5k
  int n;
2283
16.5k
  VP9_COMMON *const cm = &pbi->common;
2284
16.5k
  VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
2285
16.5k
  const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2286
16.5k
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
2287
2288
16.5k
  if (pbi->num_tile_workers == 0) {
2289
1.20k
    const int num_threads = pbi->max_threads;
2290
1.20k
    CHECK_MEM_ERROR(&cm->error, pbi->tile_workers,
2291
1.20k
                    vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
2292
36.5k
    for (n = 0; n < num_threads; ++n) {
2293
35.3k
      VPxWorker *const worker = &pbi->tile_workers[n];
2294
35.3k
      ++pbi->num_tile_workers;
2295
2296
35.3k
      winterface->init(worker);
2297
35.3k
      worker->thread_name = "vpx tile worker";
2298
35.3k
      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
35.3k
    }
2308
1.20k
  }
2309
2310
  // Initialize LPF
2311
16.5k
  if ((pbi->lpf_mt_opt || pbi->row_mt) && cm->lf.filter_level &&
2312
16.5k
      !cm->skip_loop_filter) {
2313
7.55k
    vp9_lpf_mt_init(lf_row_sync, cm, cm->lf.filter_level,
2314
7.55k
                    pbi->num_tile_workers);
2315
7.55k
  }
2316
2317
  // Note: this memset assumes above_context[0], [1] and [2]
2318
  // are allocated as part of the same buffer.
2319
16.5k
  memset(cm->above_context, 0,
2320
16.5k
         sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
2321
2322
16.5k
  memset(cm->above_seg_context, 0,
2323
16.5k
         sizeof(*cm->above_seg_context) * aligned_mi_cols);
2324
2325
16.5k
  vp9_reset_lfm(cm);
2326
16.5k
}
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
16.5k
                                      const uint8_t *data_end) {
2447
16.5k
  VP9_COMMON *const cm = &pbi->common;
2448
16.5k
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
2449
16.5k
  const uint8_t *bit_reader_end = NULL;
2450
16.5k
  VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
2451
16.5k
  YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
2452
16.5k
  const int tile_cols = 1 << cm->log2_tile_cols;
2453
16.5k
  const int tile_rows = 1 << cm->log2_tile_rows;
2454
16.5k
  const int num_workers = VPXMIN(pbi->max_threads, tile_cols);
2455
16.5k
  int n;
2456
2457
16.5k
  assert(tile_cols <= (1 << 6));
2458
16.5k
  assert(tile_rows == 1);
2459
16.5k
  (void)tile_rows;
2460
2461
16.5k
  init_mt(pbi);
2462
2463
  // Reset tile decoding hook
2464
66.6k
  for (n = 0; n < num_workers; ++n) {
2465
50.1k
    VPxWorker *const worker = &pbi->tile_workers[n];
2466
50.1k
    TileWorkerData *const tile_data =
2467
50.1k
        &pbi->tile_worker_data[n + pbi->total_tiles];
2468
50.1k
    winterface->sync(worker);
2469
2470
50.1k
    if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
2471
18.3k
      tile_data->lf_sync = lf_row_sync;
2472
18.3k
      tile_data->lf_data = &tile_data->lf_sync->lfdata[n];
2473
18.3k
      vp9_loop_filter_data_reset(tile_data->lf_data, new_fb, cm, pbi->mb.plane);
2474
18.3k
      tile_data->lf_data->y_only = 0;
2475
18.3k
    }
2476
2477
50.1k
    tile_data->xd = pbi->mb;
2478
50.1k
    tile_data->xd.counts =
2479
50.1k
        cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
2480
50.1k
    worker->hook = tile_worker_hook;
2481
50.1k
    worker->data1 = tile_data;
2482
50.1k
    worker->data2 = pbi;
2483
50.1k
  }
2484
2485
  // Load tile data into tile_buffers
2486
16.5k
  get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
2487
16.5k
                   &pbi->tile_buffers);
2488
2489
  // Sort the buffers based on size in descending order.
2490
16.5k
  qsort(pbi->tile_buffers, tile_cols, sizeof(pbi->tile_buffers[0]),
2491
16.5k
        compare_tile_buffers);
2492
2493
16.5k
  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
13.5k
    const TileBuffer largest = pbi->tile_buffers[0];
2499
13.5k
    memmove(pbi->tile_buffers, pbi->tile_buffers + 1,
2500
13.5k
            (tile_cols - 1) * sizeof(pbi->tile_buffers[0]));
2501
13.5k
    pbi->tile_buffers[tile_cols - 1] = largest;
2502
13.5k
  } else {
2503
2.94k
    int start = 0, end = tile_cols - 2;
2504
2.94k
    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
6.48k
    while (start < end) {
2509
3.54k
      tmp = pbi->tile_buffers[start];
2510
3.54k
      pbi->tile_buffers[start] = pbi->tile_buffers[end];
2511
3.54k
      pbi->tile_buffers[end] = tmp;
2512
3.54k
      start += 2;
2513
3.54k
      end -= 2;
2514
3.54k
    }
2515
2.94k
  }
2516
2517
  // Initialize thread frame counts.
2518
16.5k
  if (!cm->frame_parallel_decoding_mode) {
2519
38.0k
    for (n = 0; n < num_workers; ++n) {
2520
29.4k
      TileWorkerData *const tile_data =
2521
29.4k
          (TileWorkerData *)pbi->tile_workers[n].data1;
2522
29.4k
      vp9_zero(tile_data->counts);
2523
29.4k
    }
2524
8.57k
  }
2525
2526
16.5k
  {
2527
16.5k
    const int base = tile_cols / num_workers;
2528
16.5k
    const int remain = tile_cols % num_workers;
2529
16.5k
    int buf_start = 0;
2530
2531
61.6k
    for (n = 0; n < num_workers; ++n) {
2532
45.1k
      const int count = base + (remain + n) / num_workers;
2533
45.1k
      VPxWorker *const worker = &pbi->tile_workers[n];
2534
45.1k
      TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
2535
2536
45.1k
      tile_data->buf_start = buf_start;
2537
45.1k
      tile_data->buf_end = buf_start + count - 1;
2538
45.1k
      tile_data->data_end = data_end;
2539
45.1k
      buf_start += count;
2540
2541
45.1k
      worker->had_error = 0;
2542
45.1k
      if (n == num_workers - 1) {
2543
15.5k
        assert(tile_data->buf_end == tile_cols - 1);
2544
15.5k
        winterface->execute(worker);
2545
29.6k
      } else {
2546
29.6k
        winterface->launch(worker);
2547
29.6k
      }
2548
45.1k
    }
2549
2550
61.6k
    for (; n > 0; --n) {
2551
45.1k
      VPxWorker *const worker = &pbi->tile_workers[n - 1];
2552
45.1k
      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
45.1k
      pbi->mb.corrupted |= !winterface->sync(worker);
2558
45.1k
      if (!bit_reader_end) bit_reader_end = tile_data->data_end;
2559
45.1k
    }
2560
16.5k
  }
2561
2562
  // Accumulate thread frame counts.
2563
16.5k
  if (!cm->frame_parallel_decoding_mode) {
2564
38.0k
    for (n = 0; n < num_workers; ++n) {
2565
29.4k
      TileWorkerData *const tile_data =
2566
29.4k
          (TileWorkerData *)pbi->tile_workers[n].data1;
2567
29.4k
      vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
2568
29.4k
    }
2569
8.57k
  }
2570
2571
16.5k
  assert(bit_reader_end || pbi->mb.corrupted);
2572
15.5k
  return bit_reader_end;
2573
16.5k
}
2574
2575
20.1k
static void error_handler(void *data) {
2576
20.1k
  VP9_COMMON *const cm = (VP9_COMMON *)data;
2577
20.1k
  vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
2578
20.1k
}
2579
2580
static void read_bitdepth_colorspace_sampling(VP9_COMMON *cm,
2581
48.9k
                                              struct vpx_read_bit_buffer *rb) {
2582
48.9k
  if (cm->profile >= PROFILE_2) {
2583
13.6k
    cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10;
2584
13.6k
#if CONFIG_VP9_HIGHBITDEPTH
2585
13.6k
    cm->use_highbitdepth = 1;
2586
13.6k
#endif
2587
35.2k
  } else {
2588
35.2k
    cm->bit_depth = VPX_BITS_8;
2589
35.2k
#if CONFIG_VP9_HIGHBITDEPTH
2590
35.2k
    cm->use_highbitdepth = 0;
2591
35.2k
#endif
2592
35.2k
  }
2593
48.9k
  cm->color_space = vpx_rb_read_literal(rb, 3);
2594
48.9k
  if (cm->color_space != VPX_CS_SRGB) {
2595
48.6k
    cm->color_range = (vpx_color_range_t)vpx_rb_read_bit(rb);
2596
48.6k
    if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
2597
11.9k
      cm->subsampling_x = vpx_rb_read_bit(rb);
2598
11.9k
      cm->subsampling_y = vpx_rb_read_bit(rb);
2599
11.9k
      if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
2600
66
        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2601
66
                           "4:2:0 color not supported in profile 1 or 3");
2602
11.9k
      if (vpx_rb_read_bit(rb))
2603
130
        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2604
130
                           "Reserved bit set");
2605
36.7k
    } else {
2606
36.7k
      cm->subsampling_y = cm->subsampling_x = 1;
2607
36.7k
    }
2608
48.6k
  } else {
2609
281
    cm->color_range = VPX_CR_FULL_RANGE;
2610
281
    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
174
      cm->subsampling_y = cm->subsampling_x = 0;
2614
174
      if (vpx_rb_read_bit(rb))
2615
76
        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2616
76
                           "Reserved bit set");
2617
174
    } else {
2618
107
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2619
107
                         "4:4:4 color not supported in profile 0 or 2");
2620
107
    }
2621
281
  }
2622
48.9k
}
2623
2624
37.3k
static INLINE void flush_all_fb_on_key(VP9_COMMON *cm) {
2625
37.3k
  if (cm->frame_type == KEY_FRAME && cm->current_video_frame > 0) {
2626
20.8k
    RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
2627
20.8k
    BufferPool *const pool = cm->buffer_pool;
2628
20.8k
    int i;
2629
270k
    for (i = 0; i < FRAME_BUFFERS; ++i) {
2630
250k
      if (i == cm->new_fb_idx) continue;
2631
229k
      frame_bufs[i].ref_count = 0;
2632
229k
      if (!frame_bufs[i].released) {
2633
66.6k
        pool->release_fb_cb(pool->cb_priv, &frame_bufs[i].raw_frame_buffer);
2634
66.6k
        frame_bufs[i].released = 1;
2635
66.6k
      }
2636
229k
    }
2637
20.8k
  }
2638
37.3k
}
2639
2640
static size_t read_uncompressed_header(VP9Decoder *pbi,
2641
206k
                                       struct vpx_read_bit_buffer *rb) {
2642
206k
  VP9_COMMON *const cm = &pbi->common;
2643
206k
  BufferPool *const pool = cm->buffer_pool;
2644
206k
  RefCntBuffer *const frame_bufs = pool->frame_bufs;
2645
206k
  int i, mask, ref_index = 0;
2646
206k
  size_t sz;
2647
2648
206k
  cm->last_frame_type = cm->frame_type;
2649
206k
  cm->last_intra_only = cm->intra_only;
2650
2651
206k
  if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
2652
16.6k
    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2653
16.6k
                       "Invalid frame marker");
2654
2655
206k
  cm->profile = vp9_read_profile(rb);
2656
206k
#if CONFIG_VP9_HIGHBITDEPTH
2657
206k
  if (cm->profile >= MAX_PROFILES)
2658
104
    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2659
104
                       "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
206k
  cm->show_existing_frame = vpx_rb_read_bit(rb);
2667
206k
  if (cm->show_existing_frame) {
2668
    // Show an existing frame directly.
2669
7.37k
    const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)];
2670
7.37k
    if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
2671
806
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2672
806
                         "Buffer %d does not contain a decoded frame",
2673
806
                         frame_to_show);
2674
806
    }
2675
2676
7.37k
    ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
2677
7.37k
    pbi->refresh_frame_flags = 0;
2678
7.37k
    cm->lf.filter_level = 0;
2679
7.37k
    cm->show_frame = 1;
2680
2681
7.37k
    return 0;
2682
7.37k
  }
2683
2684
198k
  cm->frame_type = (FRAME_TYPE)vpx_rb_read_bit(rb);
2685
198k
  cm->show_frame = vpx_rb_read_bit(rb);
2686
198k
  cm->error_resilient_mode = vpx_rb_read_bit(rb);
2687
2688
198k
  if (cm->frame_type == KEY_FRAME) {
2689
50.5k
    if (!vp9_read_sync_code(rb))
2690
1.78k
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2691
1.78k
                         "Invalid frame sync code");
2692
2693
50.5k
    read_bitdepth_colorspace_sampling(cm, rb);
2694
50.5k
    pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
2695
2696
195k
    for (i = 0; i < REFS_PER_FRAME; ++i) {
2697
145k
      cm->frame_refs[i].idx = INVALID_IDX;
2698
145k
      cm->frame_refs[i].buf = NULL;
2699
145k
    }
2700
2701
50.5k
    setup_frame_size(cm, rb);
2702
50.5k
    if (pbi->need_resync) {
2703
37.3k
      memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2704
37.3k
      flush_all_fb_on_key(cm);
2705
37.3k
      pbi->need_resync = 0;
2706
37.3k
    }
2707
148k
  } else {
2708
148k
    cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
2709
2710
148k
    cm->reset_frame_context =
2711
148k
        cm->error_resilient_mode ? 0 : vpx_rb_read_literal(rb, 2);
2712
2713
148k
    if (cm->intra_only) {
2714
47.8k
      if (!vp9_read_sync_code(rb))
2715
3.24k
        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
2716
3.24k
                           "Invalid frame sync code");
2717
47.8k
      if (cm->profile > PROFILE_0) {
2718
150
        read_bitdepth_colorspace_sampling(cm, rb);
2719
47.6k
      } 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
47.6k
        cm->color_space = VPX_CS_BT_601;
2725
47.6k
        cm->color_range = VPX_CR_STUDIO_RANGE;
2726
47.6k
        cm->subsampling_y = cm->subsampling_x = 1;
2727
47.6k
        cm->bit_depth = VPX_BITS_8;
2728
47.6k
#if CONFIG_VP9_HIGHBITDEPTH
2729
47.6k
        cm->use_highbitdepth = 0;
2730
47.6k
#endif
2731
47.6k
      }
2732
2733
47.8k
      pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
2734
47.8k
      setup_frame_size(cm, rb);
2735
47.8k
      if (pbi->need_resync) {
2736
44.0k
        memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2737
44.0k
        pbi->need_resync = 0;
2738
44.0k
      }
2739
100k
    } else if (pbi->need_resync != 1) { /* Skip if need resync */
2740
68.2k
      pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
2741
272k
      for (i = 0; i < REFS_PER_FRAME; ++i) {
2742
204k
        const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2);
2743
204k
        const int idx = cm->ref_frame_map[ref];
2744
204k
        RefBuffer *const ref_frame = &cm->frame_refs[i];
2745
204k
        ref_frame->idx = idx;
2746
204k
        ref_frame->buf = &frame_bufs[idx].buf;
2747
204k
        cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb);
2748
204k
      }
2749
2750
68.2k
      setup_frame_size_with_refs(cm, rb);
2751
2752
68.2k
      cm->allow_high_precision_mv = vpx_rb_read_bit(rb);
2753
68.2k
      cm->interp_filter = read_interp_filter(rb);
2754
2755
270k
      for (i = 0; i < REFS_PER_FRAME; ++i) {
2756
202k
        RefBuffer *const ref_buf = &cm->frame_refs[i];
2757
202k
#if CONFIG_VP9_HIGHBITDEPTH
2758
202k
        vp9_setup_scale_factors_for_frame(
2759
202k
            &ref_buf->sf, ref_buf->buf->y_crop_width,
2760
202k
            ref_buf->buf->y_crop_height, cm->width, cm->height,
2761
202k
            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
202k
      }
2768
68.2k
    }
2769
148k
  }
2770
198k
#if CONFIG_VP9_HIGHBITDEPTH
2771
198k
  get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
2772
198k
#endif
2773
198k
  get_frame_new_buffer(cm)->color_space = cm->color_space;
2774
198k
  get_frame_new_buffer(cm)->color_range = cm->color_range;
2775
198k
  get_frame_new_buffer(cm)->render_width = cm->render_width;
2776
198k
  get_frame_new_buffer(cm)->render_height = cm->render_height;
2777
2778
198k
  if (pbi->need_resync) {
2779
15.1k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2780
15.1k
                       "Keyframe / intra-only frame required to reset decoder"
2781
15.1k
                       " state");
2782
15.1k
  }
2783
2784
198k
  if (!cm->error_resilient_mode) {
2785
106k
    cm->refresh_frame_context = vpx_rb_read_bit(rb);
2786
106k
    cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb);
2787
106k
    if (!cm->frame_parallel_decoding_mode) vp9_zero(cm->counts);
2788
106k
  } else {
2789
91.9k
    cm->refresh_frame_context = 0;
2790
91.9k
    cm->frame_parallel_decoding_mode = 1;
2791
91.9k
  }
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
198k
  cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
2796
2797
  // Generate next_ref_frame_map.
2798
960k
  for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
2799
762k
    if (mask & 1) {
2800
557k
      cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
2801
557k
      ++frame_bufs[cm->new_fb_idx].ref_count;
2802
557k
    } else {
2803
204k
      cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
2804
204k
    }
2805
    // Current thread holds the reference frame.
2806
762k
    if (cm->ref_frame_map[ref_index] >= 0)
2807
375k
      ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2808
762k
    ++ref_index;
2809
762k
  }
2810
2811
702k
  for (; ref_index < REF_FRAMES; ++ref_index) {
2812
503k
    cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
2813
    // Current thread holds the reference frame.
2814
503k
    if (cm->ref_frame_map[ref_index] >= 0)
2815
236k
      ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2816
503k
  }
2817
198k
  pbi->hold_ref_buf = 1;
2818
2819
198k
  if (frame_is_intra_only(cm) || cm->error_resilient_mode)
2820
91.2k
    vp9_setup_past_independence(cm);
2821
2822
198k
  setup_loopfilter(&cm->lf, rb);
2823
198k
  setup_quantization(cm, &pbi->mb, rb);
2824
198k
  setup_segmentation(&cm->seg, rb);
2825
198k
  setup_segmentation_dequant(cm);
2826
2827
198k
  setup_tile_info(cm, rb);
2828
198k
  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
198k
  sz = vpx_rb_read_literal(rb, 16);
2858
2859
198k
  if (sz == 0)
2860
11.4k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2861
11.4k
                       "Invalid header size");
2862
2863
198k
  return sz;
2864
198k
}
2865
2866
static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
2867
104k
                                  size_t partition_size) {
2868
104k
  VP9_COMMON *const cm = &pbi->common;
2869
104k
  MACROBLOCKD *const xd = &pbi->mb;
2870
104k
  FRAME_CONTEXT *const fc = cm->fc;
2871
104k
  vpx_reader r;
2872
104k
  int k;
2873
2874
104k
  if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb,
2875
104k
                      pbi->decrypt_state))
2876
1.15k
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2877
1.15k
                       "Failed to allocate bool decoder 0");
2878
2879
104k
  cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
2880
104k
  if (cm->tx_mode == TX_MODE_SELECT) read_tx_mode_probs(&fc->tx_probs, &r);
2881
104k
  read_coef_probs(fc, cm->tx_mode, &r);
2882
2883
415k
  for (k = 0; k < SKIP_CONTEXTS; ++k)
2884
310k
    vp9_diff_update_prob(&r, &fc->skip_probs[k]);
2885
2886
104k
  if (!frame_is_intra_only(cm)) {
2887
64.4k
    nmv_context *const nmvc = &fc->nmvc;
2888
64.4k
    int i, j;
2889
2890
64.4k
    read_inter_mode_probs(fc, &r);
2891
2892
64.4k
    if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
2893
2894
322k
    for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
2895
257k
      vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
2896
2897
64.4k
    cm->reference_mode = read_frame_reference_mode(cm, &r);
2898
64.4k
    if (cm->reference_mode != SINGLE_REFERENCE)
2899
19.8k
      vp9_setup_compound_reference_mode(cm);
2900
64.4k
    read_frame_reference_mode_probs(cm, &r);
2901
2902
322k
    for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
2903
2.57M
      for (i = 0; i < INTRA_MODES - 1; ++i)
2904
2.32M
        vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
2905
2906
1.09M
    for (j = 0; j < PARTITION_CONTEXTS; ++j)
2907
4.12M
      for (i = 0; i < PARTITION_TYPES - 1; ++i)
2908
3.09M
        vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
2909
2910
64.4k
    read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
2911
64.4k
  }
2912
2913
104k
  return vpx_reader_has_error(&r);
2914
104k
}
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
206k
    const uint8_t *data_end, uint8_t clear_data[MAX_VP9_HEADER_SIZE]) {
2919
206k
  rb->bit_offset = 0;
2920
206k
  rb->error_handler = error_handler;
2921
206k
  rb->error_handler_data = &pbi->common;
2922
206k
  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
206k
  } else {
2928
206k
    rb->bit_buffer = data;
2929
206k
    rb->bit_buffer_end = data_end;
2930
206k
  }
2931
206k
  return rb;
2932
206k
}
2933
2934
//------------------------------------------------------------------------------
2935
2936
204k
int vp9_read_sync_code(struct vpx_read_bit_buffer *const rb) {
2937
204k
  return vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 &&
2938
204k
         vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 &&
2939
204k
         vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2;
2940
204k
}
2941
2942
void vp9_read_frame_size(struct vpx_read_bit_buffer *rb, int *width,
2943
208k
                         int *height) {
2944
208k
  *width = vpx_rb_read_literal(rb, 16) + 1;
2945
208k
  *height = vpx_rb_read_literal(rb, 16) + 1;
2946
208k
}
2947
2948
354k
BITSTREAM_PROFILE vp9_read_profile(struct vpx_read_bit_buffer *rb) {
2949
354k
  int profile = vpx_rb_read_bit(rb);
2950
354k
  profile |= vpx_rb_read_bit(rb) << 1;
2951
354k
  if (profile > 2) profile += vpx_rb_read_bit(rb);
2952
354k
  return (BITSTREAM_PROFILE)profile;
2953
354k
}
2954
2955
void vp9_decode_frame(VP9Decoder *pbi, const uint8_t *data,
2956
206k
                      const uint8_t *data_end, const uint8_t **p_data_end) {
2957
206k
  VP9_COMMON *const cm = &pbi->common;
2958
206k
  MACROBLOCKD *const xd = &pbi->mb;
2959
206k
  struct vpx_read_bit_buffer rb;
2960
206k
  int context_updated = 0;
2961
206k
  uint8_t clear_data[MAX_VP9_HEADER_SIZE];
2962
206k
  const size_t first_partition_size = read_uncompressed_header(
2963
206k
      pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
2964
206k
  const int tile_rows = 1 << cm->log2_tile_rows;
2965
206k
  const int tile_cols = 1 << cm->log2_tile_cols;
2966
206k
  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
206k
  xd->cur_buf = new_fb;
2974
2975
206k
  if (!first_partition_size) {
2976
    // showing a frame directly
2977
6.57k
    *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
2978
6.57k
    return;
2979
6.57k
  }
2980
2981
199k
  data += vpx_rb_bytes_read(&rb);
2982
199k
  if (!read_is_valid(data, first_partition_size, data_end))
2983
23.4k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2984
23.4k
                       "Truncated packet or corrupt header length");
2985
2986
199k
  cm->use_prev_frame_mvs =
2987
199k
      !cm->error_resilient_mode && cm->width == cm->last_width &&
2988
199k
      cm->height == cm->last_height && !cm->last_intra_only &&
2989
199k
      cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
2990
2991
199k
  vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
2992
2993
199k
  *cm->fc = cm->frame_contexts[cm->frame_context_idx];
2994
199k
  if (!cm->fc->initialized)
2995
1
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2996
1
                       "Uninitialized entropy context.");
2997
2998
199k
  xd->corrupted = 0;
2999
199k
  new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
3000
199k
  if (new_fb->corrupted)
3001
1.09k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
3002
1.09k
                       "Decode failed. Frame data header is corrupted.");
3003
3004
199k
  if (cm->lf.filter_level && !cm->skip_loop_filter) {
3005
71.5k
    vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
3006
71.5k
  }
3007
3008
199k
  if (pbi->tile_worker_data == NULL ||
3009
199k
      (tile_cols * tile_rows) != pbi->total_tiles) {
3010
16.9k
    const int num_tile_workers =
3011
16.9k
        tile_cols * tile_rows + ((pbi->max_threads > 1) ? pbi->max_threads : 0);
3012
16.9k
    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
16.9k
    assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
3016
16.9k
    vpx_free(pbi->tile_worker_data);
3017
16.9k
    CHECK_MEM_ERROR(&cm->error, pbi->tile_worker_data,
3018
16.9k
                    vpx_memalign(32, twd_size));
3019
16.9k
    pbi->total_tiles = tile_rows * tile_cols;
3020
16.9k
  }
3021
3022
199k
  if (pbi->max_threads > 1 && tile_rows == 1 &&
3023
199k
      (tile_cols > 1 || pbi->row_mt == 1)) {
3024
16.5k
    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
16.5k
    } else {
3028
      // Multi-threaded tile decoder
3029
16.5k
      *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
3030
16.5k
      if (!pbi->lpf_mt_opt) {
3031
5.35k
        if (!xd->corrupted) {
3032
4.31k
          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.31k
            vp9_loop_filter_frame_mt(
3036
4.31k
                new_fb, cm, pbi->mb.plane, cm->lf.filter_level, 0, 0,
3037
4.31k
                pbi->tile_workers, pbi->num_tile_workers, &pbi->lf_row_sync);
3038
4.31k
          }
3039
4.31k
        } else {
3040
1.04k
          vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
3041
1.04k
                             "Decode failed. Frame data is corrupted.");
3042
1.04k
        }
3043
5.35k
      }
3044
16.5k
    }
3045
183k
  } else {
3046
183k
    *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
3047
183k
  }
3048
3049
199k
  if (!xd->corrupted) {
3050
80.9k
    if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
3051
48.5k
      vp9_adapt_coef_probs(cm);
3052
3053
48.5k
      if (!frame_is_intra_only(cm)) {
3054
32.7k
        vp9_adapt_mode_probs(cm);
3055
32.7k
        vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
3056
32.7k
      }
3057
48.5k
    }
3058
118k
  } else {
3059
118k
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
3060
118k
                       "Decode failed. Frame data is corrupted.");
3061
118k
  }
3062
3063
  // Non frame parallel update frame context here.
3064
199k
  if (cm->refresh_frame_context && !context_updated)
3065
32.0k
    cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3066
199k
}