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;
185 int vp9_get_reference_dec(VP9Decoder *pbi, int index, YV12_BUFFER_CONFIG **fb) {
186 VP9_COMMON *cm = &pbi->common;
188 if (index < 0 || index >= REF_FRAMES)
191 *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
195 /* If any buffer updating is signaled it should be done here. */
196 static void swap_frame_buffers(VP9Decoder *pbi) {
197 int ref_index = 0, mask;
198 VP9_COMMON *const cm = &pbi->common;
200 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
202 const int old_idx = cm->ref_frame_map[ref_index];
203 ref_cnt_fb(cm->frame_bufs, &cm->ref_frame_map[ref_index],
205 if (old_idx >= 0 && cm->frame_bufs[old_idx].ref_count == 0)
206 cm->release_fb_cb(cm->cb_priv,
207 &cm->frame_bufs[old_idx].raw_frame_buffer);
212 cm->frame_to_show = get_frame_new_buffer(cm);
213 cm->frame_bufs[cm->new_fb_idx].ref_count--;
215 // Invalidate these references until the next frame starts.
216 for (ref_index = 0; ref_index < 3; ref_index++)
217 cm->frame_refs[ref_index].idx = INT_MAX;
220 int vp9_receive_compressed_data(VP9Decoder *pbi,
221 size_t size, const uint8_t **psource) {
222 VP9_COMMON *const cm = &pbi->common;
223 const uint8_t *source = *psource;
226 cm->error.error_code = VPX_CODEC_OK;
229 // This is used to signal that we are missing frames.
230 // We do not know if the missing frame(s) was supposed to update
231 // any of the reference buffers, but we act conservative and
232 // mark only the last buffer as corrupted.
234 // TODO(jkoleszar): Error concealment is undefined and non-normative
235 // at this point, but if it becomes so, [0] may not always be the correct
237 if (cm->frame_refs[0].idx != INT_MAX)
238 cm->frame_refs[0].buf->corrupted = 1;
241 // Check if the previous frame was a frame without any references to it.
242 if (cm->new_fb_idx >= 0 && cm->frame_bufs[cm->new_fb_idx].ref_count == 0)
243 cm->release_fb_cb(cm->cb_priv,
244 &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer);
245 cm->new_fb_idx = get_free_fb(cm);
247 if (setjmp(cm->error.jmp)) {
248 cm->error.setjmp = 0;
249 vp9_clear_system_state();
251 // We do not know if the missing frame(s) was supposed to update
252 // any of the reference buffers, but we act conservative and
253 // mark only the last buffer as corrupted.
255 // TODO(jkoleszar): Error concealment is undefined and non-normative
256 // at this point, but if it becomes so, [0] may not always be the correct
258 if (cm->frame_refs[0].idx != INT_MAX && cm->frame_refs[0].buf != NULL)
259 cm->frame_refs[0].buf->corrupted = 1;
261 if (cm->new_fb_idx > 0 && cm->frame_bufs[cm->new_fb_idx].ref_count > 0)
262 cm->frame_bufs[cm->new_fb_idx].ref_count--;
267 cm->error.setjmp = 1;
269 vp9_decode_frame(pbi, source, source + size, psource);
271 swap_frame_buffers(pbi);
273 vp9_clear_system_state();
275 cm->last_width = cm->width;
276 cm->last_height = cm->height;
278 if (!cm->show_existing_frame)
279 cm->last_show_frame = cm->show_frame;
280 if (cm->show_frame) {
281 if (!cm->show_existing_frame)
282 vp9_swap_mi_and_prev_mi(cm);
284 cm->current_video_frame++;
287 pbi->ready_for_new_data = 0;
289 cm->error.setjmp = 0;
293 int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
294 vp9_ppflags_t *flags) {
295 VP9_COMMON *const cm = &pbi->common;
297 #if !CONFIG_VP9_POSTPROC
301 if (pbi->ready_for_new_data == 1)
304 /* no raw frame to show!!! */
308 pbi->ready_for_new_data = 1;
310 #if CONFIG_VP9_POSTPROC
311 if (!cm->show_existing_frame) {
312 ret = vp9_post_proc_frame(cm, sd, flags);
314 *sd = *cm->frame_to_show;
318 *sd = *cm->frame_to_show;
320 #endif /*!CONFIG_POSTPROC*/
321 vp9_clear_system_state();