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