Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/decoder/vp9_decoder.c
Line
Count
Source
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 <limits.h>
13
#include <stdio.h>
14
15
#include "./vp9_rtcd.h"
16
#include "./vpx_dsp_rtcd.h"
17
#include "./vpx_scale_rtcd.h"
18
19
#include "vpx/internal/vpx_codec_internal.h"
20
#include "vpx/vpx_codec.h"
21
#include "vpx_mem/vpx_mem.h"
22
#include "vpx_ports/system_state.h"
23
#include "vpx_ports/vpx_once.h"
24
#include "vpx_ports/vpx_timer.h"
25
#include "vpx_scale/vpx_scale.h"
26
#include "vpx_util/vpx_pthread.h"
27
#include "vpx_util/vpx_thread.h"
28
29
#include "vp9/common/vp9_alloccommon.h"
30
#include "vp9/common/vp9_loopfilter.h"
31
#include "vp9/common/vp9_onyxc_int.h"
32
#if CONFIG_VP9_POSTPROC
33
#include "vp9/common/vp9_postproc.h"
34
#endif
35
#include "vp9/common/vp9_quant_common.h"
36
#include "vp9/common/vp9_reconintra.h"
37
38
#include "vp9/decoder/vp9_decodeframe.h"
39
#include "vp9/decoder/vp9_decoder.h"
40
#include "vp9/decoder/vp9_detokenize.h"
41
42
1
static void initialize_dec(void) {
43
1
  static volatile int init_done = 0;
44
45
1
  if (!init_done) {
46
1
    vp9_rtcd();
47
1
    vpx_dsp_rtcd();
48
1
    vpx_scale_rtcd();
49
1
    vp9_init_intra_predictors();
50
1
    init_done = 1;
51
1
  }
52
1
}
53
54
76.5k
static void vp9_dec_setup_mi(VP9_COMMON *cm) {
55
76.5k
  cm->mi = cm->mip + cm->mi_stride + 1;
56
76.5k
  cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
57
76.5k
  memset(cm->mi_grid_base, 0,
58
76.5k
         cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
59
76.5k
}
60
61
void vp9_dec_alloc_row_mt_mem(RowMTWorkerData *row_mt_worker_data,
62
                              VP9_COMMON *cm, int num_sbs, int max_threads,
63
0
                              int num_jobs) {
64
0
  if ((size_t)num_sbs > SIZE_MAX / (sizeof(*row_mt_worker_data->dqcoeff[0])
65
0
                                    << DQCOEFFS_PER_SB_LOG2)) {
66
0
    vpx_internal_error(&cm->error, VPX_CODEC_ERROR, "num_sbs too big");
67
0
  }
68
0
  const size_t dqcoeff_size = ((size_t)num_sbs << DQCOEFFS_PER_SB_LOG2) *
69
0
                              sizeof(*row_mt_worker_data->dqcoeff[0]);
70
0
  row_mt_worker_data->num_jobs = num_jobs;
71
0
#if CONFIG_MULTITHREAD
72
0
  {
73
0
    int i;
74
0
    CHECK_MEM_ERROR(
75
0
        &cm->error, row_mt_worker_data->recon_sync_mutex,
76
0
        vpx_malloc(sizeof(*row_mt_worker_data->recon_sync_mutex) * num_jobs));
77
0
    if (row_mt_worker_data->recon_sync_mutex) {
78
0
      for (i = 0; i < num_jobs; ++i) {
79
0
        pthread_mutex_init(&row_mt_worker_data->recon_sync_mutex[i], NULL);
80
0
      }
81
0
    }
82
83
0
    CHECK_MEM_ERROR(
84
0
        &cm->error, row_mt_worker_data->recon_sync_cond,
85
0
        vpx_malloc(sizeof(*row_mt_worker_data->recon_sync_cond) * num_jobs));
86
0
    if (row_mt_worker_data->recon_sync_cond) {
87
0
      for (i = 0; i < num_jobs; ++i) {
88
0
        pthread_cond_init(&row_mt_worker_data->recon_sync_cond[i], NULL);
89
0
      }
90
0
    }
91
0
  }
92
0
#endif
93
0
  row_mt_worker_data->num_sbs = num_sbs;
94
0
  for (int plane = 0; plane < 3; ++plane) {
95
0
    CHECK_MEM_ERROR(&cm->error, row_mt_worker_data->dqcoeff[plane],
96
0
                    vpx_memalign(32, dqcoeff_size));
97
0
    memset(row_mt_worker_data->dqcoeff[plane], 0, dqcoeff_size);
98
0
    CHECK_MEM_ERROR(&cm->error, row_mt_worker_data->eob[plane],
99
0
                    vpx_calloc((size_t)num_sbs << EOBS_PER_SB_LOG2,
100
0
                               sizeof(*row_mt_worker_data->eob[plane])));
101
0
  }
102
0
  CHECK_MEM_ERROR(&cm->error, row_mt_worker_data->partition,
103
0
                  vpx_calloc((size_t)num_sbs * PARTITIONS_PER_SB,
104
0
                             sizeof(*row_mt_worker_data->partition)));
105
0
  CHECK_MEM_ERROR(&cm->error, row_mt_worker_data->recon_map,
106
0
                  vpx_calloc(num_sbs, sizeof(*row_mt_worker_data->recon_map)));
107
108
  // allocate memory for thread_data
109
0
  if (row_mt_worker_data->thread_data == NULL) {
110
0
    const size_t thread_size =
111
0
        max_threads * sizeof(*row_mt_worker_data->thread_data);
112
0
    CHECK_MEM_ERROR(&cm->error, row_mt_worker_data->thread_data,
113
0
                    vpx_memalign(32, thread_size));
114
0
  }
115
0
}
116
117
0
void vp9_dec_free_row_mt_mem(RowMTWorkerData *row_mt_worker_data) {
118
0
  if (row_mt_worker_data != NULL) {
119
0
    int plane;
120
0
#if CONFIG_MULTITHREAD
121
0
    int i;
122
0
    if (row_mt_worker_data->recon_sync_mutex != NULL) {
123
0
      for (i = 0; i < row_mt_worker_data->num_jobs; ++i) {
124
0
        pthread_mutex_destroy(&row_mt_worker_data->recon_sync_mutex[i]);
125
0
      }
126
0
      vpx_free(row_mt_worker_data->recon_sync_mutex);
127
0
      row_mt_worker_data->recon_sync_mutex = NULL;
128
0
    }
129
0
    if (row_mt_worker_data->recon_sync_cond != NULL) {
130
0
      for (i = 0; i < row_mt_worker_data->num_jobs; ++i) {
131
0
        pthread_cond_destroy(&row_mt_worker_data->recon_sync_cond[i]);
132
0
      }
133
0
      vpx_free(row_mt_worker_data->recon_sync_cond);
134
0
      row_mt_worker_data->recon_sync_cond = NULL;
135
0
    }
136
0
#endif
137
0
    for (plane = 0; plane < 3; ++plane) {
138
0
      vpx_free(row_mt_worker_data->eob[plane]);
139
0
      row_mt_worker_data->eob[plane] = NULL;
140
0
      vpx_free(row_mt_worker_data->dqcoeff[plane]);
141
0
      row_mt_worker_data->dqcoeff[plane] = NULL;
142
0
    }
143
0
    vpx_free(row_mt_worker_data->partition);
144
0
    row_mt_worker_data->partition = NULL;
145
0
    vpx_free(row_mt_worker_data->recon_map);
146
0
    row_mt_worker_data->recon_map = NULL;
147
0
    vpx_free(row_mt_worker_data->thread_data);
148
0
    row_mt_worker_data->thread_data = NULL;
149
0
  }
150
0
}
151
152
17.2k
static int vp9_dec_alloc_mi(VP9_COMMON *cm, int mi_size) {
153
17.2k
  cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
154
17.2k
  if (!cm->mip) return 1;
155
17.2k
  cm->mi_alloc_size = mi_size;
156
17.2k
  cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
157
17.2k
  if (!cm->mi_grid_base) return 1;
158
17.2k
  return 0;
159
17.2k
}
160
161
31.9k
static void vp9_dec_free_mi(VP9_COMMON *cm) {
162
#if CONFIG_VP9_POSTPROC
163
  // MFQE allocates an additional mip and swaps it with cm->mip.
164
  vpx_free(cm->postproc_state.prev_mip);
165
  cm->postproc_state.prev_mip = NULL;
166
#endif
167
31.9k
  vpx_free(cm->mip);
168
31.9k
  cm->mip = NULL;
169
31.9k
  vpx_free(cm->mi_grid_base);
170
31.9k
  cm->mi_grid_base = NULL;
171
31.9k
  cm->mi_alloc_size = 0;
172
31.9k
}
173
174
14.6k
VP9Decoder *vp9_decoder_create(BufferPool *const pool) {
175
14.6k
  VP9Decoder *volatile const pbi = vpx_memalign(32, sizeof(*pbi));
176
14.6k
  VP9_COMMON *volatile const cm = pbi ? &pbi->common : NULL;
177
178
14.6k
  if (!cm) return NULL;
179
180
14.6k
  vp9_zero(*pbi);
181
182
14.6k
  if (setjmp(cm->error.jmp)) {
183
0
    cm->error.setjmp = 0;
184
0
    vp9_decoder_remove(pbi);
185
0
    return NULL;
186
0
  }
187
188
14.6k
  cm->error.setjmp = 1;
189
190
14.6k
  CHECK_MEM_ERROR(&cm->error, cm->fc,
191
14.6k
                  (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
192
14.6k
  CHECK_MEM_ERROR(
193
14.6k
      &cm->error, cm->frame_contexts,
194
14.6k
      (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
195
196
14.6k
  pbi->need_resync = 1;
197
14.6k
  once(initialize_dec);
198
199
  // Initialize the references to not point to any frame buffers.
200
14.6k
  memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
201
14.6k
  memset(&cm->next_ref_frame_map, -1, sizeof(cm->next_ref_frame_map));
202
203
14.6k
  init_frame_indexes(cm);
204
14.6k
  pbi->ready_for_new_data = 1;
205
14.6k
  pbi->common.buffer_pool = pool;
206
207
14.6k
  cm->bit_depth = VPX_BITS_8;
208
14.6k
  cm->dequant_bit_depth = VPX_BITS_8;
209
210
14.6k
  cm->alloc_mi = vp9_dec_alloc_mi;
211
14.6k
  cm->free_mi = vp9_dec_free_mi;
212
14.6k
  cm->setup_mi = vp9_dec_setup_mi;
213
214
14.6k
  vp9_loop_filter_init(cm);
215
216
14.6k
  cm->error.setjmp = 0;
217
218
14.6k
  vpx_get_worker_interface()->init(&pbi->lf_worker);
219
14.6k
  pbi->lf_worker.thread_name = "vpx lf worker";
220
221
14.6k
  return pbi;
222
14.6k
}
223
224
14.6k
void vp9_decoder_remove(VP9Decoder *pbi) {
225
14.6k
  int i;
226
227
14.6k
  if (!pbi) return;
228
229
14.6k
  vpx_get_worker_interface()->end(&pbi->lf_worker);
230
14.6k
  vpx_free(pbi->lf_worker.data1);
231
232
14.6k
  for (i = 0; i < pbi->num_tile_workers; ++i) {
233
0
    VPxWorker *const worker = &pbi->tile_workers[i];
234
0
    vpx_get_worker_interface()->end(worker);
235
0
  }
236
237
14.6k
  vpx_free(pbi->tile_worker_data);
238
14.6k
  vpx_free(pbi->tile_workers);
239
240
14.6k
  if (pbi->num_tile_workers > 0) {
241
0
    vp9_loop_filter_dealloc(&pbi->lf_row_sync);
242
0
  }
243
244
14.6k
  if (pbi->row_mt == 1) {
245
0
    vp9_dec_free_row_mt_mem(pbi->row_mt_worker_data);
246
0
    if (pbi->row_mt_worker_data != NULL) {
247
0
      vp9_jobq_deinit(&pbi->row_mt_worker_data->jobq);
248
0
      vpx_free(pbi->row_mt_worker_data->jobq_buf);
249
0
#if CONFIG_MULTITHREAD
250
0
      pthread_mutex_destroy(&pbi->row_mt_worker_data->recon_done_mutex);
251
0
#endif
252
0
    }
253
0
    vpx_free(pbi->row_mt_worker_data);
254
0
  }
255
256
14.6k
  vp9_remove_common(&pbi->common);
257
14.6k
  vpx_free(pbi);
258
14.6k
}
259
260
static int equal_dimensions(const YV12_BUFFER_CONFIG *a,
261
0
                            const YV12_BUFFER_CONFIG *b) {
262
0
  return a->y_height == b->y_height && a->y_width == b->y_width &&
263
0
         a->uv_height == b->uv_height && a->uv_width == b->uv_width;
264
0
}
265
266
vpx_codec_err_t vp9_copy_reference_dec(VP9Decoder *pbi,
267
                                       VP9_REFFRAME ref_frame_flag,
268
0
                                       YV12_BUFFER_CONFIG *sd) {
269
0
  VP9_COMMON *cm = &pbi->common;
270
271
  /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the
272
   * encoder is using the frame buffers for. This is just a stub to keep the
273
   * vpxenc --test-decode functionality working, and will be replaced in a
274
   * later commit that adds VP9-specific controls for this functionality.
275
   */
276
0
  if (ref_frame_flag == VP9_LAST_FLAG) {
277
0
    const YV12_BUFFER_CONFIG *const cfg = get_ref_frame(cm, 0);
278
0
    if (cfg == NULL) {
279
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
280
0
                         "No 'last' reference frame");
281
0
      return VPX_CODEC_ERROR;
282
0
    }
283
0
    if (!equal_dimensions(cfg, sd))
284
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
285
0
                         "Incorrect buffer dimensions");
286
0
    else
287
0
      vpx_yv12_copy_frame(cfg, sd);
288
0
  } else {
289
0
    vpx_internal_error(&cm->error, VPX_CODEC_ERROR, "Invalid reference frame");
290
0
  }
291
292
0
  return cm->error.error_code;
293
0
}
294
295
vpx_codec_err_t vp9_set_reference_dec(VP9_COMMON *cm,
296
                                      VP9_REFFRAME ref_frame_flag,
297
0
                                      YV12_BUFFER_CONFIG *sd) {
298
0
  int idx;
299
0
  YV12_BUFFER_CONFIG *ref_buf = NULL;
300
301
  // TODO(jkoleszar): The decoder doesn't have any real knowledge of what the
302
  // encoder is using the frame buffers for. This is just a stub to keep the
303
  // vpxenc --test-decode functionality working, and will be replaced in a
304
  // later commit that adds VP9-specific controls for this functionality.
305
  // (Yunqing) The set_reference control depends on the following setting in
306
  // encoder.
307
  // cpi->lst_fb_idx = 0;
308
  // cpi->gld_fb_idx = 1;
309
  // cpi->alt_fb_idx = 2;
310
0
  if (ref_frame_flag == VP9_LAST_FLAG) {
311
0
    idx = cm->ref_frame_map[0];
312
0
  } else if (ref_frame_flag == VP9_GOLD_FLAG) {
313
0
    idx = cm->ref_frame_map[1];
314
0
  } else if (ref_frame_flag == VP9_ALT_FLAG) {
315
0
    idx = cm->ref_frame_map[2];
316
0
  } else {
317
0
    vpx_internal_error(&cm->error, VPX_CODEC_ERROR, "Invalid reference frame");
318
0
    return cm->error.error_code;
319
0
  }
320
321
0
  if (idx < 0 || idx >= FRAME_BUFFERS) {
322
0
    vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
323
0
                       "Invalid reference frame map");
324
0
    return cm->error.error_code;
325
0
  }
326
327
  // Get the destination reference buffer.
328
0
  ref_buf = &cm->buffer_pool->frame_bufs[idx].buf;
329
330
0
  if (!equal_dimensions(ref_buf, sd)) {
331
0
    vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
332
0
                       "Incorrect buffer dimensions");
333
0
  } else {
334
    // Overwrite the reference frame buffer.
335
0
    vpx_yv12_copy_frame(sd, ref_buf);
336
0
  }
337
338
0
  return cm->error.error_code;
339
0
}
340
341
/* If any buffer updating is signaled it should be done here. */
342
111k
static void swap_frame_buffers(VP9Decoder *pbi) {
343
111k
  int ref_index = 0, mask;
344
111k
  VP9_COMMON *const cm = &pbi->common;
345
111k
  BufferPool *const pool = cm->buffer_pool;
346
111k
  RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
347
348
631k
  for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
349
519k
    const int old_idx = cm->ref_frame_map[ref_index];
350
    // Current thread releases the holding of reference frame.
351
519k
    decrease_ref_count(old_idx, frame_bufs, pool);
352
353
    // Release the reference frame in reference map.
354
519k
    if (mask & 1) {
355
426k
      decrease_ref_count(old_idx, frame_bufs, pool);
356
426k
    }
357
519k
    cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index];
358
519k
    ++ref_index;
359
519k
  }
360
361
  // Current thread releases the holding of reference frame.
362
353k
  for (; ref_index < REF_FRAMES && !cm->show_existing_frame; ++ref_index) {
363
241k
    const int old_idx = cm->ref_frame_map[ref_index];
364
241k
    decrease_ref_count(old_idx, frame_bufs, pool);
365
241k
    cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index];
366
241k
  }
367
111k
  pbi->hold_ref_buf = 0;
368
111k
  cm->frame_to_show = get_frame_new_buffer(cm);
369
370
111k
  --frame_bufs[cm->new_fb_idx].ref_count;
371
372
  // Invalidate these references until the next frame starts.
373
447k
  for (ref_index = 0; ref_index < 3; ref_index++)
374
335k
    cm->frame_refs[ref_index].idx = -1;
375
111k
}
376
377
246k
static void release_fb_on_decoder_exit(VP9Decoder *pbi) {
378
246k
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
379
246k
  VP9_COMMON *volatile const cm = &pbi->common;
380
246k
  BufferPool *volatile const pool = cm->buffer_pool;
381
246k
  RefCntBuffer *volatile const frame_bufs = cm->buffer_pool->frame_bufs;
382
246k
  int i;
383
384
  // Synchronize all threads immediately as a subsequent decode call may
385
  // cause a resize invalidating some allocations.
386
246k
  winterface->sync(&pbi->lf_worker);
387
246k
  for (i = 0; i < pbi->num_tile_workers; ++i) {
388
0
    winterface->sync(&pbi->tile_workers[i]);
389
0
  }
390
391
  // Release all the reference buffers if worker thread is holding them.
392
246k
  if (pbi->hold_ref_buf == 1) {
393
100k
    int ref_index = 0, mask;
394
739k
    for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
395
639k
      const int old_idx = cm->ref_frame_map[ref_index];
396
      // Current thread releases the holding of reference frame.
397
639k
      decrease_ref_count(old_idx, frame_bufs, pool);
398
399
      // Release the reference frame in reference map.
400
639k
      if (mask & 1) {
401
475k
        decrease_ref_count(old_idx, frame_bufs, pool);
402
475k
      }
403
639k
      ++ref_index;
404
639k
    }
405
406
    // Current thread releases the holding of reference frame.
407
260k
    for (; ref_index < REF_FRAMES && !cm->show_existing_frame; ++ref_index) {
408
160k
      const int old_idx = cm->ref_frame_map[ref_index];
409
160k
      decrease_ref_count(old_idx, frame_bufs, pool);
410
160k
    }
411
100k
    pbi->hold_ref_buf = 0;
412
100k
  }
413
246k
}
414
415
int vp9_receive_compressed_data(VP9Decoder *pbi, size_t size,
416
358k
                                const uint8_t **psource) {
417
358k
  VP9_COMMON *volatile const cm = &pbi->common;
418
358k
  BufferPool *volatile const pool = cm->buffer_pool;
419
358k
  RefCntBuffer *volatile const frame_bufs = cm->buffer_pool->frame_bufs;
420
358k
  const uint8_t *source = *psource;
421
358k
  int retcode = 0;
422
358k
  cm->error.error_code = VPX_CODEC_OK;
423
424
358k
  if (size == 0) {
425
    // This is used to signal that we are missing frames.
426
    // We do not know if the missing frame(s) was supposed to update
427
    // any of the reference buffers, but we act conservative and
428
    // mark only the last buffer as corrupted.
429
    //
430
    // TODO(jkoleszar): Error concealment is undefined and non-normative
431
    // at this point, but if it becomes so, [0] may not always be the correct
432
    // thing to do here.
433
488
    if (cm->frame_refs[0].idx > 0) {
434
195
      assert(cm->frame_refs[0].buf != NULL);
435
195
      cm->frame_refs[0].buf->corrupted = 1;
436
195
    }
437
488
  }
438
439
358k
  pbi->ready_for_new_data = 0;
440
441
  // Check if the previous frame was a frame without any references to it.
442
358k
  if (cm->new_fb_idx >= 0 && frame_bufs[cm->new_fb_idx].ref_count == 0 &&
443
172k
      !frame_bufs[cm->new_fb_idx].released) {
444
23.5k
    pool->release_fb_cb(pool->cb_priv,
445
23.5k
                        &frame_bufs[cm->new_fb_idx].raw_frame_buffer);
446
23.5k
    frame_bufs[cm->new_fb_idx].released = 1;
447
23.5k
  }
448
449
  // Find a free frame buffer. Return error if can not find any.
450
358k
  cm->new_fb_idx = get_free_fb(cm);
451
358k
  if (cm->new_fb_idx == INVALID_IDX) {
452
1.38k
    pbi->ready_for_new_data = 1;
453
1.38k
    release_fb_on_decoder_exit(pbi);
454
1.38k
    vpx_clear_system_state();
455
1.38k
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
456
1.38k
                       "Unable to find free frame buffer");
457
1.38k
    return cm->error.error_code;
458
1.38k
  }
459
460
  // Assign a MV array to the frame buffer.
461
356k
  cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
462
463
356k
  pbi->hold_ref_buf = 0;
464
356k
  pbi->cur_buf = &frame_bufs[cm->new_fb_idx];
465
466
356k
  if (setjmp(cm->error.jmp)) {
467
244k
    cm->error.setjmp = 0;
468
244k
    pbi->ready_for_new_data = 1;
469
244k
    release_fb_on_decoder_exit(pbi);
470
    // Release current frame.
471
244k
    decrease_ref_count(cm->new_fb_idx, frame_bufs, pool);
472
244k
    vpx_clear_system_state();
473
244k
    return -1;
474
244k
  }
475
476
111k
  cm->error.setjmp = 1;
477
111k
  vp9_decode_frame(pbi, source, source + size, psource);
478
479
111k
  swap_frame_buffers(pbi);
480
481
111k
  vpx_clear_system_state();
482
483
111k
  if (!cm->show_existing_frame) {
484
95.1k
    cm->last_show_frame = cm->show_frame;
485
95.1k
    cm->prev_frame = cm->cur_frame;
486
95.1k
    if (cm->seg.enabled) vp9_swap_current_and_last_seg_map(cm);
487
95.1k
  }
488
489
111k
  if (cm->show_frame) cm->cur_show_frame_fb_idx = cm->new_fb_idx;
490
491
  // Update progress in frame parallel decode.
492
111k
  cm->last_width = cm->width;
493
111k
  cm->last_height = cm->height;
494
111k
  if (cm->show_frame) {
495
72.1k
    cm->current_video_frame++;
496
72.1k
  }
497
498
111k
  cm->error.setjmp = 0;
499
111k
  return retcode;
500
356k
}
501
502
int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
503
14.4k
                      vp9_ppflags_t *flags) {
504
14.4k
  VP9_COMMON *const cm = &pbi->common;
505
14.4k
  int ret = -1;
506
14.4k
#if !CONFIG_VP9_POSTPROC
507
14.4k
  (void)*flags;
508
14.4k
#endif
509
510
14.4k
  if (pbi->ready_for_new_data == 1) return ret;
511
512
14.4k
  pbi->ready_for_new_data = 1;
513
514
  /* no raw frame to show!!! */
515
14.4k
  if (!cm->show_frame) return ret;
516
517
12.3k
  pbi->ready_for_new_data = 1;
518
519
#if CONFIG_VP9_POSTPROC
520
  if (setjmp(cm->error.jmp)) {
521
    cm->error.setjmp = 0;
522
    vpx_clear_system_state();
523
    return -1;
524
  }
525
526
  cm->error.setjmp = 1;
527
528
  if (!cm->show_existing_frame) {
529
    ret = vp9_post_proc_frame(cm, sd, flags, cm->width);
530
  } else {
531
    *sd = *cm->frame_to_show;
532
    ret = 0;
533
  }
534
535
  cm->error.setjmp = 0;
536
#else
537
12.3k
  *sd = *cm->frame_to_show;
538
12.3k
  ret = 0;
539
12.3k
#endif /*!CONFIG_POSTPROC*/
540
541
12.3k
  vpx_clear_system_state();
542
12.3k
  return ret;
543
14.4k
}
544
545
vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data, size_t data_sz,
546
                                           uint32_t sizes[8], int *count,
547
                                           vpx_decrypt_cb decrypt_cb,
548
325k
                                           void *decrypt_state) {
549
  // A chunk ending with a byte matching 0xc0 is an invalid chunk unless
550
  // it is a super frame index. If the last byte of real video compression
551
  // data is 0xc0 the encoder must add a 0 byte. If we have the marker but
552
  // not the associated matching marker byte at the front of the index we have
553
  // an invalid bitstream and need to return an error.
554
555
325k
  uint8_t marker;
556
557
325k
  assert(data_sz);
558
325k
  marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
559
325k
  *count = 0;
560
561
325k
  if ((marker & 0xe0) == 0xc0) {
562
9.79k
    const uint32_t frames = (marker & 0x7) + 1;
563
9.79k
    const uint32_t mag = ((marker >> 3) & 0x3) + 1;
564
9.79k
    const size_t index_sz = 2 + mag * frames;
565
566
    // This chunk is marked as having a superframe index but doesn't have
567
    // enough data for it, thus it's an invalid superframe index.
568
9.79k
    if (data_sz < index_sz) return VPX_CODEC_CORRUPT_FRAME;
569
570
8.42k
    {
571
8.42k
      const uint8_t marker2 =
572
8.42k
          read_marker(decrypt_cb, decrypt_state, data + data_sz - index_sz);
573
574
      // This chunk is marked as having a superframe index but doesn't have
575
      // the matching marker byte at the front of the index therefore it's an
576
      // invalid chunk.
577
8.42k
      if (marker != marker2) return VPX_CODEC_CORRUPT_FRAME;
578
8.42k
    }
579
580
5.95k
    {
581
      // Found a valid superframe index.
582
5.95k
      uint32_t i, j;
583
5.95k
      const uint8_t *x = &data[data_sz - index_sz + 1];
584
585
      // Frames has a maximum of 8 and mag has a maximum of 4.
586
5.95k
      uint8_t clear_buffer[32];
587
5.95k
      assert(sizeof(clear_buffer) >= frames * mag);
588
5.95k
      if (decrypt_cb) {
589
0
        decrypt_cb(decrypt_state, x, clear_buffer, frames * mag);
590
0
        x = clear_buffer;
591
0
      }
592
593
28.4k
      for (i = 0; i < frames; ++i) {
594
22.4k
        uint32_t this_sz = 0;
595
596
80.0k
        for (j = 0; j < mag; ++j) this_sz |= ((uint32_t)(*x++)) << (j * 8);
597
22.4k
        sizes[i] = this_sz;
598
22.4k
      }
599
5.95k
      *count = frames;
600
5.95k
    }
601
5.95k
  }
602
321k
  return VPX_CODEC_OK;
603
325k
}