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 "./vpx_scale_rtcd.h"
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_ports/vpx_timer.h"
19 #include "vpx_scale/vpx_scale.h"
21 #include "vp9/common/vp9_alloccommon.h"
22 #include "vp9/common/vp9_loopfilter.h"
23 #include "vp9/common/vp9_onyxc_int.h"
24 #if CONFIG_VP9_POSTPROC
25 #include "vp9/common/vp9_postproc.h"
27 #include "vp9/common/vp9_quant_common.h"
28 #include "vp9/common/vp9_systemdependent.h"
30 #include "vp9/decoder/vp9_decodeframe.h"
31 #include "vp9/decoder/vp9_decoder.h"
32 #include "vp9/decoder/vp9_detokenize.h"
33 #include "vp9/decoder/vp9_dthread.h"
35 static void initialize_dec() {
36 static int init_done = 0;
44 VP9Decoder *vp9_decoder_create() {
45 VP9Decoder *const pbi = vpx_memalign(32, sizeof(*pbi));
46 VP9_COMMON *const cm = pbi ? &pbi->common : NULL;
53 if (setjmp(cm->error.jmp)) {
55 vp9_decoder_remove(pbi);
64 // Initialize the references to not point to any frame buffers.
65 vpx_memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
67 cm->current_video_frame = 0;
68 pbi->ready_for_new_data = 1;
70 // vp9_init_dequantizer() is first called here. Add check in
71 // frame_init_dequantizer() to avoid unnecessary calling of
72 // vp9_init_dequantizer() for every frame.
73 vp9_init_dequantizer(cm);
75 vp9_loop_filter_init(cm);
79 vp9_get_worker_interface()->init(&pbi->lf_worker);
84 void vp9_decoder_remove(VP9Decoder *pbi) {
85 VP9_COMMON *const cm = &pbi->common;
88 vp9_get_worker_interface()->end(&pbi->lf_worker);
89 vpx_free(pbi->lf_worker.data1);
90 vpx_free(pbi->tile_data);
91 for (i = 0; i < pbi->num_tile_workers; ++i) {
92 VP9Worker *const worker = &pbi->tile_workers[i];
93 vp9_get_worker_interface()->end(worker);
94 vpx_free(worker->data1);
95 vpx_free(worker->data2);
97 vpx_free(pbi->tile_workers);
99 if (pbi->num_tile_workers) {
101 mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
102 vp9_loop_filter_dealloc(&pbi->lf_row_sync, sb_rows);
105 vp9_remove_common(cm);
109 static int equal_dimensions(const YV12_BUFFER_CONFIG *a,
110 const YV12_BUFFER_CONFIG *b) {
111 return a->y_height == b->y_height && a->y_width == b->y_width &&
112 a->uv_height == b->uv_height && a->uv_width == b->uv_width;
115 vpx_codec_err_t vp9_copy_reference_dec(VP9Decoder *pbi,
116 VP9_REFFRAME ref_frame_flag,
117 YV12_BUFFER_CONFIG *sd) {
118 VP9_COMMON *cm = &pbi->common;
120 /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the
121 * encoder is using the frame buffers for. This is just a stub to keep the
122 * vpxenc --test-decode functionality working, and will be replaced in a
123 * later commit that adds VP9-specific controls for this functionality.
125 if (ref_frame_flag == VP9_LAST_FLAG) {
126 const YV12_BUFFER_CONFIG *const cfg =
127 &cm->frame_bufs[cm->ref_frame_map[0]].buf;
128 if (!equal_dimensions(cfg, sd))
129 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
130 "Incorrect buffer dimensions");
132 vp8_yv12_copy_frame(cfg, sd);
134 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
135 "Invalid reference frame");
138 return cm->error.error_code;
142 vpx_codec_err_t vp9_set_reference_dec(VP9_COMMON *cm,
143 VP9_REFFRAME ref_frame_flag,
144 YV12_BUFFER_CONFIG *sd) {
145 RefBuffer *ref_buf = NULL;
147 // TODO(jkoleszar): The decoder doesn't have any real knowledge of what the
148 // encoder is using the frame buffers for. This is just a stub to keep the
149 // vpxenc --test-decode functionality working, and will be replaced in a
150 // later commit that adds VP9-specific controls for this functionality.
151 if (ref_frame_flag == VP9_LAST_FLAG) {
152 ref_buf = &cm->frame_refs[0];
153 } else if (ref_frame_flag == VP9_GOLD_FLAG) {
154 ref_buf = &cm->frame_refs[1];
155 } else if (ref_frame_flag == VP9_ALT_FLAG) {
156 ref_buf = &cm->frame_refs[2];
158 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
159 "Invalid reference frame");
160 return cm->error.error_code;
163 if (!equal_dimensions(ref_buf->buf, sd)) {
164 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
165 "Incorrect buffer dimensions");
167 int *ref_fb_ptr = &ref_buf->idx;
169 // Find an empty frame buffer.
170 const int free_fb = get_free_fb(cm);
171 // Decrease ref_count since it will be increased again in
172 // ref_cnt_fb() below.
173 cm->frame_bufs[free_fb].ref_count--;
175 // Manage the reference counters and copy image.
176 ref_cnt_fb(cm->frame_bufs, ref_fb_ptr, free_fb);
177 ref_buf->buf = &cm->frame_bufs[*ref_fb_ptr].buf;
178 vp8_yv12_copy_frame(sd, ref_buf->buf);
181 return cm->error.error_code;
184 /* If any buffer updating is signaled it should be done here. */
185 static void swap_frame_buffers(VP9Decoder *pbi) {
186 int ref_index = 0, mask;
187 VP9_COMMON *const cm = &pbi->common;
189 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
191 const int old_idx = cm->ref_frame_map[ref_index];
192 ref_cnt_fb(cm->frame_bufs, &cm->ref_frame_map[ref_index],
194 if (old_idx >= 0 && cm->frame_bufs[old_idx].ref_count == 0)
195 cm->release_fb_cb(cm->cb_priv,
196 &cm->frame_bufs[old_idx].raw_frame_buffer);
201 cm->frame_to_show = get_frame_new_buffer(cm);
202 cm->frame_bufs[cm->new_fb_idx].ref_count--;
204 // Invalidate these references until the next frame starts.
205 for (ref_index = 0; ref_index < 3; ref_index++)
206 cm->frame_refs[ref_index].idx = INT_MAX;
209 int vp9_receive_compressed_data(VP9Decoder *pbi,
210 size_t size, const uint8_t **psource) {
211 VP9_COMMON *const cm = &pbi->common;
212 const uint8_t *source = *psource;
215 cm->error.error_code = VPX_CODEC_OK;
218 // This is used to signal that we are missing frames.
219 // We do not know if the missing frame(s) was supposed to update
220 // any of the reference buffers, but we act conservative and
221 // mark only the last buffer as corrupted.
223 // TODO(jkoleszar): Error concealment is undefined and non-normative
224 // at this point, but if it becomes so, [0] may not always be the correct
226 if (cm->frame_refs[0].idx != INT_MAX)
227 cm->frame_refs[0].buf->corrupted = 1;
230 // Check if the previous frame was a frame without any references to it.
231 if (cm->new_fb_idx >= 0 && cm->frame_bufs[cm->new_fb_idx].ref_count == 0)
232 cm->release_fb_cb(cm->cb_priv,
233 &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer);
234 cm->new_fb_idx = get_free_fb(cm);
236 if (setjmp(cm->error.jmp)) {
237 cm->error.setjmp = 0;
238 vp9_clear_system_state();
240 // We do not know if the missing frame(s) was supposed to update
241 // any of the reference buffers, but we act conservative and
242 // mark only the last buffer as corrupted.
244 // TODO(jkoleszar): Error concealment is undefined and non-normative
245 // at this point, but if it becomes so, [0] may not always be the correct
247 if (cm->frame_refs[0].idx != INT_MAX && cm->frame_refs[0].buf != NULL)
248 cm->frame_refs[0].buf->corrupted = 1;
250 if (cm->new_fb_idx > 0 && cm->frame_bufs[cm->new_fb_idx].ref_count > 0)
251 cm->frame_bufs[cm->new_fb_idx].ref_count--;
256 cm->error.setjmp = 1;
258 vp9_decode_frame(pbi, source, source + size, psource);
260 swap_frame_buffers(pbi);
262 vp9_clear_system_state();
264 cm->last_width = cm->width;
265 cm->last_height = cm->height;
267 if (!cm->show_existing_frame)
268 cm->last_show_frame = cm->show_frame;
269 if (cm->show_frame) {
270 if (!cm->show_existing_frame)
271 vp9_swap_mi_and_prev_mi(cm);
273 cm->current_video_frame++;
276 pbi->ready_for_new_data = 0;
278 cm->error.setjmp = 0;
282 int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
283 vp9_ppflags_t *flags) {
284 VP9_COMMON *const cm = &pbi->common;
286 #if !CONFIG_VP9_POSTPROC
290 if (pbi->ready_for_new_data == 1)
293 /* no raw frame to show!!! */
297 pbi->ready_for_new_data = 1;
299 #if CONFIG_VP9_POSTPROC
300 if (!cm->show_existing_frame) {
301 ret = vp9_post_proc_frame(cm, sd, flags);
303 *sd = *cm->frame_to_show;
307 *sd = *cm->frame_to_show;
309 #endif /*!CONFIG_POSTPROC*/
310 vp9_clear_system_state();
314 vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data,
316 uint32_t sizes[8], int *count,
317 vpx_decrypt_cb decrypt_cb,
318 void *decrypt_state) {
319 // A chunk ending with a byte matching 0xc0 is an invalid chunk unless
320 // it is a super frame index. If the last byte of real video compression
321 // data is 0xc0 the encoder must add a 0 byte. If we have the marker but
322 // not the associated matching marker byte at the front of the index we have
323 // an invalid bitstream and need to return an error.
328 marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
331 if ((marker & 0xe0) == 0xc0) {
332 const uint32_t frames = (marker & 0x7) + 1;
333 const uint32_t mag = ((marker >> 3) & 0x3) + 1;
334 const size_t index_sz = 2 + mag * frames;
336 // This chunk is marked as having a superframe index but doesn't have
337 // enough data for it, thus it's an invalid superframe index.
338 if (data_sz < index_sz)
339 return VPX_CODEC_CORRUPT_FRAME;
342 const uint8_t marker2 = read_marker(decrypt_cb, decrypt_state,
343 data + data_sz - index_sz);
345 // This chunk is marked as having a superframe index but doesn't have
346 // the matching marker byte at the front of the index therefore it's an
348 if (marker != marker2)
349 return VPX_CODEC_CORRUPT_FRAME;
353 // Found a valid superframe index.
355 const uint8_t *x = &data[data_sz - index_sz + 1];
357 // Frames has a maximum of 8 and mag has a maximum of 4.
358 uint8_t clear_buffer[32];
359 assert(sizeof(clear_buffer) >= frames * mag);
361 decrypt_cb(decrypt_state, x, clear_buffer, frames * mag);
365 for (i = 0; i < frames; ++i) {
366 uint32_t this_sz = 0;
368 for (j = 0; j < mag; ++j)
369 this_sz |= (*x++) << (j * 8);