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