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