2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
15 #include "./vp9_rtcd.h"
16 #include "./vpx_dsp_rtcd.h"
17 #include "./vpx_scale_rtcd.h"
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"
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"
32 #include "vp9/common/vp9_quant_common.h"
33 #include "vp9/common/vp9_reconintra.h"
35 #include "vp9/decoder/vp9_decodeframe.h"
36 #include "vp9/decoder/vp9_decoder.h"
37 #include "vp9/decoder/vp9_detokenize.h"
39 static void initialize_dec(void) {
40 static volatile int init_done = 0;
46 vp9_init_intra_predictors();
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));
58 void vp9_dec_alloc_row_mt_mem(RowMTWorkerData *row_mt_worker_data,
59 VP9_COMMON *cm, int num_sbs, int max_threads,
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
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);
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);
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])));
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)));
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));
111 void vp9_dec_free_row_mt_mem(RowMTWorkerData *row_mt_worker_data) {
112 if (row_mt_worker_data != NULL) {
114 #if CONFIG_MULTITHREAD
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]);
120 vpx_free(row_mt_worker_data->recon_sync_mutex);
121 row_mt_worker_data->recon_sync_mutex = NULL;
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]);
127 vpx_free(row_mt_worker_data->recon_sync_cond);
128 row_mt_worker_data->recon_sync_cond = NULL;
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;
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;
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;
155 static void vp9_dec_free_mi(VP9_COMMON *cm) {
156 #if CONFIG_VP9_POSTPROC
157 // MFQE allocates an additional mip and swaps it with cm->mip.
158 vpx_free(cm->postproc_state.prev_mip);
159 cm->postproc_state.prev_mip = NULL;
163 vpx_free(cm->mi_grid_base);
164 cm->mi_grid_base = NULL;
165 cm->mi_alloc_size = 0;
168 VP9Decoder *vp9_decoder_create(BufferPool *const pool) {
169 VP9Decoder *volatile const pbi = vpx_memalign(32, sizeof(*pbi));
170 VP9_COMMON *volatile const cm = pbi ? &pbi->common : NULL;
172 if (!cm) return NULL;
176 if (setjmp(cm->error.jmp)) {
177 cm->error.setjmp = 0;
178 vp9_decoder_remove(pbi);
182 cm->error.setjmp = 1;
184 CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
186 cm, cm->frame_contexts,
187 (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
189 pbi->need_resync = 1;
190 once(initialize_dec);
192 // Initialize the references to not point to any frame buffers.
193 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
194 memset(&cm->next_ref_frame_map, -1, sizeof(cm->next_ref_frame_map));
196 init_frame_indexes(cm);
197 pbi->ready_for_new_data = 1;
198 pbi->common.buffer_pool = pool;
200 cm->bit_depth = VPX_BITS_8;
201 cm->dequant_bit_depth = VPX_BITS_8;
203 cm->alloc_mi = vp9_dec_alloc_mi;
204 cm->free_mi = vp9_dec_free_mi;
205 cm->setup_mi = vp9_dec_setup_mi;
207 vp9_loop_filter_init(cm);
209 cm->error.setjmp = 0;
211 vpx_get_worker_interface()->init(&pbi->lf_worker);
216 void vp9_decoder_remove(VP9Decoder *pbi) {
221 vpx_get_worker_interface()->end(&pbi->lf_worker);
222 vpx_free(pbi->lf_worker.data1);
224 for (i = 0; i < pbi->num_tile_workers; ++i) {
225 VPxWorker *const worker = &pbi->tile_workers[i];
226 vpx_get_worker_interface()->end(worker);
229 vpx_free(pbi->tile_worker_data);
230 vpx_free(pbi->tile_workers);
232 if (pbi->num_tile_workers > 0) {
233 vp9_loop_filter_dealloc(&pbi->lf_row_sync);
236 if (pbi->row_mt == 1) {
237 vp9_dec_free_row_mt_mem(pbi->row_mt_worker_data);
238 if (pbi->row_mt_worker_data != NULL) {
239 vp9_jobq_deinit(&pbi->row_mt_worker_data->jobq);
240 vpx_free(pbi->row_mt_worker_data->jobq_buf);
241 #if CONFIG_MULTITHREAD
242 pthread_mutex_destroy(&pbi->row_mt_worker_data->recon_done_mutex);
245 vpx_free(pbi->row_mt_worker_data);
248 vp9_remove_common(&pbi->common);
252 static int equal_dimensions(const YV12_BUFFER_CONFIG *a,
253 const YV12_BUFFER_CONFIG *b) {
254 return a->y_height == b->y_height && a->y_width == b->y_width &&
255 a->uv_height == b->uv_height && a->uv_width == b->uv_width;
258 vpx_codec_err_t vp9_copy_reference_dec(VP9Decoder *pbi,
259 VP9_REFFRAME ref_frame_flag,
260 YV12_BUFFER_CONFIG *sd) {
261 VP9_COMMON *cm = &pbi->common;
263 /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the
264 * encoder is using the frame buffers for. This is just a stub to keep the
265 * vpxenc --test-decode functionality working, and will be replaced in a
266 * later commit that adds VP9-specific controls for this functionality.
268 if (ref_frame_flag == VP9_LAST_FLAG) {
269 const YV12_BUFFER_CONFIG *const cfg = get_ref_frame(cm, 0);
271 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
272 "No 'last' reference frame");
273 return VPX_CODEC_ERROR;
275 if (!equal_dimensions(cfg, sd))
276 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
277 "Incorrect buffer dimensions");
279 vpx_yv12_copy_frame(cfg, sd);
281 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, "Invalid reference frame");
284 return cm->error.error_code;
287 vpx_codec_err_t vp9_set_reference_dec(VP9_COMMON *cm,
288 VP9_REFFRAME ref_frame_flag,
289 YV12_BUFFER_CONFIG *sd) {
291 YV12_BUFFER_CONFIG *ref_buf = NULL;
293 // TODO(jkoleszar): The decoder doesn't have any real knowledge of what the
294 // encoder is using the frame buffers for. This is just a stub to keep the
295 // vpxenc --test-decode functionality working, and will be replaced in a
296 // later commit that adds VP9-specific controls for this functionality.
297 // (Yunqing) The set_reference control depends on the following setting in
299 // cpi->lst_fb_idx = 0;
300 // cpi->gld_fb_idx = 1;
301 // cpi->alt_fb_idx = 2;
302 if (ref_frame_flag == VP9_LAST_FLAG) {
303 idx = cm->ref_frame_map[0];
304 } else if (ref_frame_flag == VP9_GOLD_FLAG) {
305 idx = cm->ref_frame_map[1];
306 } else if (ref_frame_flag == VP9_ALT_FLAG) {
307 idx = cm->ref_frame_map[2];
309 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, "Invalid reference frame");
310 return cm->error.error_code;
313 if (idx < 0 || idx >= FRAME_BUFFERS) {
314 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
315 "Invalid reference frame map");
316 return cm->error.error_code;
319 // Get the destination reference buffer.
320 ref_buf = &cm->buffer_pool->frame_bufs[idx].buf;
322 if (!equal_dimensions(ref_buf, sd)) {
323 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
324 "Incorrect buffer dimensions");
326 // Overwrite the reference frame buffer.
327 vpx_yv12_copy_frame(sd, ref_buf);
330 return cm->error.error_code;
333 /* If any buffer updating is signaled it should be done here. */
334 static void swap_frame_buffers(VP9Decoder *pbi) {
335 int ref_index = 0, mask;
336 VP9_COMMON *const cm = &pbi->common;
337 BufferPool *const pool = cm->buffer_pool;
338 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
340 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
341 const int old_idx = cm->ref_frame_map[ref_index];
342 // Current thread releases the holding of reference frame.
343 decrease_ref_count(old_idx, frame_bufs, pool);
345 // Release the reference frame in reference map.
347 decrease_ref_count(old_idx, frame_bufs, pool);
349 cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index];
353 // Current thread releases the holding of reference frame.
354 for (; ref_index < REF_FRAMES && !cm->show_existing_frame; ++ref_index) {
355 const int old_idx = cm->ref_frame_map[ref_index];
356 decrease_ref_count(old_idx, frame_bufs, pool);
357 cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index];
359 pbi->hold_ref_buf = 0;
360 cm->frame_to_show = get_frame_new_buffer(cm);
362 --frame_bufs[cm->new_fb_idx].ref_count;
364 // Invalidate these references until the next frame starts.
365 for (ref_index = 0; ref_index < 3; ref_index++)
366 cm->frame_refs[ref_index].idx = -1;
369 static void release_fb_on_decoder_exit(VP9Decoder *pbi) {
370 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
371 VP9_COMMON *volatile const cm = &pbi->common;
372 BufferPool *volatile const pool = cm->buffer_pool;
373 RefCntBuffer *volatile const frame_bufs = cm->buffer_pool->frame_bufs;
376 // Synchronize all threads immediately as a subsequent decode call may
377 // cause a resize invalidating some allocations.
378 winterface->sync(&pbi->lf_worker);
379 for (i = 0; i < pbi->num_tile_workers; ++i) {
380 winterface->sync(&pbi->tile_workers[i]);
383 // Release all the reference buffers if worker thread is holding them.
384 if (pbi->hold_ref_buf == 1) {
385 int ref_index = 0, mask;
386 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
387 const int old_idx = cm->ref_frame_map[ref_index];
388 // Current thread releases the holding of reference frame.
389 decrease_ref_count(old_idx, frame_bufs, pool);
391 // Release the reference frame in reference map.
393 decrease_ref_count(old_idx, frame_bufs, pool);
398 // Current thread releases the holding of reference frame.
399 for (; ref_index < REF_FRAMES && !cm->show_existing_frame; ++ref_index) {
400 const int old_idx = cm->ref_frame_map[ref_index];
401 decrease_ref_count(old_idx, frame_bufs, pool);
403 pbi->hold_ref_buf = 0;
407 int vp9_receive_compressed_data(VP9Decoder *pbi, size_t size,
408 const uint8_t **psource) {
409 VP9_COMMON *volatile const cm = &pbi->common;
410 BufferPool *volatile const pool = cm->buffer_pool;
411 RefCntBuffer *volatile const frame_bufs = cm->buffer_pool->frame_bufs;
412 const uint8_t *source = *psource;
414 cm->error.error_code = VPX_CODEC_OK;
417 // This is used to signal that we are missing frames.
418 // We do not know if the missing frame(s) was supposed to update
419 // any of the reference buffers, but we act conservative and
420 // mark only the last buffer as corrupted.
422 // TODO(jkoleszar): Error concealment is undefined and non-normative
423 // at this point, but if it becomes so, [0] may not always be the correct
425 if (cm->frame_refs[0].idx > 0) {
426 assert(cm->frame_refs[0].buf != NULL);
427 cm->frame_refs[0].buf->corrupted = 1;
431 pbi->ready_for_new_data = 0;
433 // Check if the previous frame was a frame without any references to it.
434 if (cm->new_fb_idx >= 0 && frame_bufs[cm->new_fb_idx].ref_count == 0 &&
435 !frame_bufs[cm->new_fb_idx].released) {
436 pool->release_fb_cb(pool->cb_priv,
437 &frame_bufs[cm->new_fb_idx].raw_frame_buffer);
438 frame_bufs[cm->new_fb_idx].released = 1;
441 // Find a free frame buffer. Return error if can not find any.
442 cm->new_fb_idx = get_free_fb(cm);
443 if (cm->new_fb_idx == INVALID_IDX) {
444 pbi->ready_for_new_data = 1;
445 release_fb_on_decoder_exit(pbi);
446 vpx_clear_system_state();
447 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
448 "Unable to find free frame buffer");
449 return cm->error.error_code;
452 // Assign a MV array to the frame buffer.
453 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
455 pbi->hold_ref_buf = 0;
456 pbi->cur_buf = &frame_bufs[cm->new_fb_idx];
458 if (setjmp(cm->error.jmp)) {
459 cm->error.setjmp = 0;
460 pbi->ready_for_new_data = 1;
461 release_fb_on_decoder_exit(pbi);
462 // Release current frame.
463 decrease_ref_count(cm->new_fb_idx, frame_bufs, pool);
464 vpx_clear_system_state();
468 cm->error.setjmp = 1;
469 vp9_decode_frame(pbi, source, source + size, psource);
471 swap_frame_buffers(pbi);
473 vpx_clear_system_state();
475 if (!cm->show_existing_frame) {
476 cm->last_show_frame = cm->show_frame;
477 cm->prev_frame = cm->cur_frame;
478 if (cm->seg.enabled) vp9_swap_current_and_last_seg_map(cm);
481 if (cm->show_frame) cm->cur_show_frame_fb_idx = cm->new_fb_idx;
483 // Update progress in frame parallel decode.
484 cm->last_width = cm->width;
485 cm->last_height = cm->height;
486 if (cm->show_frame) {
487 cm->current_video_frame++;
490 cm->error.setjmp = 0;
494 int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
495 vp9_ppflags_t *flags) {
496 VP9_COMMON *const cm = &pbi->common;
498 #if !CONFIG_VP9_POSTPROC
502 if (pbi->ready_for_new_data == 1) return ret;
504 pbi->ready_for_new_data = 1;
506 /* no raw frame to show!!! */
507 if (!cm->show_frame) return ret;
509 pbi->ready_for_new_data = 1;
511 #if CONFIG_VP9_POSTPROC
512 if (!cm->show_existing_frame) {
513 ret = vp9_post_proc_frame(cm, sd, flags, cm->width);
515 *sd = *cm->frame_to_show;
519 *sd = *cm->frame_to_show;
521 #endif /*!CONFIG_POSTPROC*/
522 vpx_clear_system_state();
526 vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data, size_t data_sz,
527 uint32_t sizes[8], int *count,
528 vpx_decrypt_cb decrypt_cb,
529 void *decrypt_state) {
530 // A chunk ending with a byte matching 0xc0 is an invalid chunk unless
531 // it is a super frame index. If the last byte of real video compression
532 // data is 0xc0 the encoder must add a 0 byte. If we have the marker but
533 // not the associated matching marker byte at the front of the index we have
534 // an invalid bitstream and need to return an error.
539 marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
542 if ((marker & 0xe0) == 0xc0) {
543 const uint32_t frames = (marker & 0x7) + 1;
544 const uint32_t mag = ((marker >> 3) & 0x3) + 1;
545 const size_t index_sz = 2 + mag * frames;
547 // This chunk is marked as having a superframe index but doesn't have
548 // enough data for it, thus it's an invalid superframe index.
549 if (data_sz < index_sz) return VPX_CODEC_CORRUPT_FRAME;
552 const uint8_t marker2 =
553 read_marker(decrypt_cb, decrypt_state, data + data_sz - index_sz);
555 // This chunk is marked as having a superframe index but doesn't have
556 // the matching marker byte at the front of the index therefore it's an
558 if (marker != marker2) return VPX_CODEC_CORRUPT_FRAME;
562 // Found a valid superframe index.
564 const uint8_t *x = &data[data_sz - index_sz + 1];
566 // Frames has a maximum of 8 and mag has a maximum of 4.
567 uint8_t clear_buffer[32];
568 assert(sizeof(clear_buffer) >= frames * mag);
570 decrypt_cb(decrypt_state, x, clear_buffer, frames * mag);
574 for (i = 0; i < frames; ++i) {
575 uint32_t this_sz = 0;
577 for (j = 0; j < mag; ++j) this_sz |= ((uint32_t)(*x++)) << (j * 8);