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 void vp9_initialize_dec() {
36 static int init_done = 0;
40 vp9_init_quant_tables();
45 VP9Decoder *vp9_decoder_create(const VP9DecoderConfig *oxcf) {
46 VP9Decoder *const pbi = vpx_memalign(32, sizeof(*pbi));
47 VP9_COMMON *const cm = pbi ? &pbi->common : NULL;
54 if (setjmp(cm->error.jmp)) {
56 vp9_decoder_remove(pbi);
65 // Initialize the references to not point to any frame buffers.
66 vpx_memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
68 cm->current_video_frame = 0;
70 pbi->ready_for_new_data = 1;
71 pbi->decoded_key_frame = 0;
73 // vp9_init_dequantizer() is first called here. Add check in
74 // frame_init_dequantizer() to avoid unnecessary calling of
75 // vp9_init_dequantizer() for every frame.
76 vp9_init_dequantizer(cm);
78 vp9_loop_filter_init(cm);
82 vp9_worker_init(&pbi->lf_worker);
87 void vp9_decoder_remove(VP9Decoder *pbi) {
88 VP9_COMMON *const cm = &pbi->common;
91 vp9_remove_common(cm);
92 vp9_worker_end(&pbi->lf_worker);
93 vpx_free(pbi->lf_worker.data1);
94 for (i = 0; i < pbi->num_tile_workers; ++i) {
95 VP9Worker *const worker = &pbi->tile_workers[i];
96 vp9_worker_end(worker);
97 vpx_free(worker->data1);
98 vpx_free(worker->data2);
100 vpx_free(pbi->tile_workers);
102 if (pbi->num_tile_workers) {
104 mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
105 vp9_loop_filter_dealloc(&pbi->lf_row_sync, sb_rows);
111 static int equal_dimensions(const YV12_BUFFER_CONFIG *a,
112 const YV12_BUFFER_CONFIG *b) {
113 return a->y_height == b->y_height && a->y_width == b->y_width &&
114 a->uv_height == b->uv_height && a->uv_width == b->uv_width;
117 vpx_codec_err_t vp9_copy_reference_dec(VP9Decoder *pbi,
118 VP9_REFFRAME ref_frame_flag,
119 YV12_BUFFER_CONFIG *sd) {
120 VP9_COMMON *cm = &pbi->common;
122 /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the
123 * encoder is using the frame buffers for. This is just a stub to keep the
124 * vpxenc --test-decode functionality working, and will be replaced in a
125 * later commit that adds VP9-specific controls for this functionality.
127 if (ref_frame_flag == VP9_LAST_FLAG) {
128 const YV12_BUFFER_CONFIG *const cfg =
129 &cm->frame_bufs[cm->ref_frame_map[0]].buf;
130 if (!equal_dimensions(cfg, sd))
131 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
132 "Incorrect buffer dimensions");
134 vp8_yv12_copy_frame(cfg, sd);
136 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
137 "Invalid reference frame");
140 return cm->error.error_code;
144 vpx_codec_err_t vp9_set_reference_dec(VP9_COMMON *cm,
145 VP9_REFFRAME ref_frame_flag,
146 YV12_BUFFER_CONFIG *sd) {
147 RefBuffer *ref_buf = NULL;
149 // TODO(jkoleszar): The decoder doesn't have any real knowledge of what the
150 // encoder is using the frame buffers for. This is just a stub to keep the
151 // vpxenc --test-decode functionality working, and will be replaced in a
152 // later commit that adds VP9-specific controls for this functionality.
153 if (ref_frame_flag == VP9_LAST_FLAG) {
154 ref_buf = &cm->frame_refs[0];
155 } else if (ref_frame_flag == VP9_GOLD_FLAG) {
156 ref_buf = &cm->frame_refs[1];
157 } else if (ref_frame_flag == VP9_ALT_FLAG) {
158 ref_buf = &cm->frame_refs[2];
160 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
161 "Invalid reference frame");
162 return cm->error.error_code;
165 if (!equal_dimensions(ref_buf->buf, sd)) {
166 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
167 "Incorrect buffer dimensions");
169 int *ref_fb_ptr = &ref_buf->idx;
171 // Find an empty frame buffer.
172 const int free_fb = get_free_fb(cm);
173 // Decrease ref_count since it will be increased again in
174 // ref_cnt_fb() below.
175 cm->frame_bufs[free_fb].ref_count--;
177 // Manage the reference counters and copy image.
178 ref_cnt_fb(cm->frame_bufs, ref_fb_ptr, free_fb);
179 ref_buf->buf = &cm->frame_bufs[*ref_fb_ptr].buf;
180 vp8_yv12_copy_frame(sd, ref_buf->buf);
183 return cm->error.error_code;
187 int vp9_get_reference_dec(VP9Decoder *pbi, int index, YV12_BUFFER_CONFIG **fb) {
188 VP9_COMMON *cm = &pbi->common;
190 if (index < 0 || index >= REF_FRAMES)
193 *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
197 /* If any buffer updating is signaled it should be done here. */
198 static void swap_frame_buffers(VP9Decoder *pbi) {
199 int ref_index = 0, mask;
200 VP9_COMMON *const cm = &pbi->common;
202 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
204 const int old_idx = cm->ref_frame_map[ref_index];
205 ref_cnt_fb(cm->frame_bufs, &cm->ref_frame_map[ref_index],
207 if (old_idx >= 0 && cm->frame_bufs[old_idx].ref_count == 0)
208 cm->release_fb_cb(cm->cb_priv,
209 &cm->frame_bufs[old_idx].raw_frame_buffer);
214 cm->frame_to_show = get_frame_new_buffer(cm);
215 cm->frame_bufs[cm->new_fb_idx].ref_count--;
217 // Invalidate these references until the next frame starts.
218 for (ref_index = 0; ref_index < 3; ref_index++)
219 cm->frame_refs[ref_index].idx = INT_MAX;
222 int vp9_receive_compressed_data(VP9Decoder *pbi,
223 size_t size, const uint8_t **psource,
224 int64_t time_stamp) {
225 VP9_COMMON *const cm = &pbi->common;
226 const uint8_t *source = *psource;
229 cm->error.error_code = VPX_CODEC_OK;
232 // This is used to signal that we are missing frames.
233 // We do not know if the missing frame(s) was supposed to update
234 // any of the reference buffers, but we act conservative and
235 // mark only the last buffer as corrupted.
237 // TODO(jkoleszar): Error concealment is undefined and non-normative
238 // at this point, but if it becomes so, [0] may not always be the correct
240 if (cm->frame_refs[0].idx != INT_MAX)
241 cm->frame_refs[0].buf->corrupted = 1;
244 // Check if the previous frame was a frame without any references to it.
245 if (cm->new_fb_idx >= 0 && cm->frame_bufs[cm->new_fb_idx].ref_count == 0)
246 cm->release_fb_cb(cm->cb_priv,
247 &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer);
248 cm->new_fb_idx = get_free_fb(cm);
250 if (setjmp(cm->error.jmp)) {
251 cm->error.setjmp = 0;
253 // We do not know if the missing frame(s) was supposed to update
254 // any of the reference buffers, but we act conservative and
255 // mark only the last buffer as corrupted.
257 // TODO(jkoleszar): Error concealment is undefined and non-normative
258 // at this point, but if it becomes so, [0] may not always be the correct
260 if (cm->frame_refs[0].idx != INT_MAX)
261 cm->frame_refs[0].buf->corrupted = 1;
263 if (cm->frame_bufs[cm->new_fb_idx].ref_count > 0)
264 cm->frame_bufs[cm->new_fb_idx].ref_count--;
269 cm->error.setjmp = 1;
271 retcode = vp9_decode_frame(pbi, source, source + size, psource);
274 cm->error.error_code = VPX_CODEC_ERROR;
275 cm->error.setjmp = 0;
276 if (cm->frame_bufs[cm->new_fb_idx].ref_count > 0)
277 cm->frame_bufs[cm->new_fb_idx].ref_count--;
281 swap_frame_buffers(pbi);
283 if (!pbi->do_loopfilter_inline) {
284 // If multiple threads are used to decode tiles, then we use those threads
285 // to do parallel loopfiltering.
286 if (pbi->num_tile_workers) {
287 vp9_loop_filter_frame_mt(pbi, cm, cm->lf.filter_level, 0, 0);
289 vp9_loop_filter_frame(cm, &pbi->mb, cm->lf.filter_level, 0, 0);
293 vp9_clear_system_state();
295 cm->last_width = cm->width;
296 cm->last_height = cm->height;
298 if (!cm->show_existing_frame)
299 cm->last_show_frame = cm->show_frame;
300 if (cm->show_frame) {
301 if (!cm->show_existing_frame)
302 vp9_swap_mi_and_prev_mi(cm);
304 cm->current_video_frame++;
307 pbi->ready_for_new_data = 0;
308 pbi->last_time_stamp = time_stamp;
310 cm->error.setjmp = 0;
314 int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
315 int64_t *time_stamp, int64_t *time_end_stamp,
316 vp9_ppflags_t *flags) {
319 if (pbi->ready_for_new_data == 1)
322 /* ie no raw frame to show!!! */
323 if (pbi->common.show_frame == 0)
326 pbi->ready_for_new_data = 1;
327 *time_stamp = pbi->last_time_stamp;
330 #if CONFIG_VP9_POSTPROC
331 ret = vp9_post_proc_frame(&pbi->common, sd, flags);
333 *sd = *pbi->common.frame_to_show;
335 #endif /*!CONFIG_POSTPROC*/
336 vp9_clear_system_state();