Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / decoder / vp9_decoder.c
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 "./vpx_scale_rtcd.h"
16
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_ports/vpx_timer.h"
19 #include "vpx_scale/vpx_scale.h"
20
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"
26 #endif
27 #include "vp9/common/vp9_quant_common.h"
28 #include "vp9/common/vp9_systemdependent.h"
29
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"
34
35 void vp9_initialize_dec() {
36   static int init_done = 0;
37
38   if (!init_done) {
39     vp9_init_neighbors();
40     vp9_init_quant_tables();
41     init_done = 1;
42   }
43 }
44
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;
48
49   if (!cm)
50     return NULL;
51
52   vp9_zero(*pbi);
53
54   if (setjmp(cm->error.jmp)) {
55     cm->error.setjmp = 0;
56     vp9_decoder_remove(pbi);
57     return NULL;
58   }
59
60   cm->error.setjmp = 1;
61   vp9_initialize_dec();
62
63   vp9_rtcd();
64
65   // Initialize the references to not point to any frame buffers.
66   vpx_memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
67
68   cm->current_video_frame = 0;
69   pbi->oxcf = *oxcf;
70   pbi->ready_for_new_data = 1;
71   pbi->decoded_key_frame = 0;
72
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);
77
78   vp9_loop_filter_init(cm);
79
80   cm->error.setjmp = 0;
81
82   vp9_worker_init(&pbi->lf_worker);
83
84   return pbi;
85 }
86
87 void vp9_decoder_remove(VP9Decoder *pbi) {
88   VP9_COMMON *const cm = &pbi->common;
89   int i;
90
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);
99   }
100   vpx_free(pbi->tile_workers);
101
102   if (pbi->num_tile_workers) {
103     const int sb_rows =
104         mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
105     vp9_loop_filter_dealloc(&pbi->lf_row_sync, sb_rows);
106   }
107
108   vpx_free(pbi);
109 }
110
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;
115 }
116
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;
121
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.
126    */
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");
133     else
134       vp8_yv12_copy_frame(cfg, sd);
135   } else {
136     vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
137                        "Invalid reference frame");
138   }
139
140   return cm->error.error_code;
141 }
142
143
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;
148
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];
159   } else {
160     vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
161                        "Invalid reference frame");
162     return cm->error.error_code;
163   }
164
165   if (!equal_dimensions(ref_buf->buf, sd)) {
166     vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
167                        "Incorrect buffer dimensions");
168   } else {
169     int *ref_fb_ptr = &ref_buf->idx;
170
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--;
176
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);
181   }
182
183   return cm->error.error_code;
184 }
185
186
187 int vp9_get_reference_dec(VP9Decoder *pbi, int index, YV12_BUFFER_CONFIG **fb) {
188   VP9_COMMON *cm = &pbi->common;
189
190   if (index < 0 || index >= REF_FRAMES)
191     return -1;
192
193   *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
194   return 0;
195 }
196
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;
201
202   for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
203     if (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],
206                  cm->new_fb_idx);
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);
210     }
211     ++ref_index;
212   }
213
214   cm->frame_to_show = get_frame_new_buffer(cm);
215   cm->frame_bufs[cm->new_fb_idx].ref_count--;
216
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;
220 }
221
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;
227   int retcode = 0;
228
229   cm->error.error_code = VPX_CODEC_OK;
230
231   if (size == 0) {
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.
236     //
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
239     // thing to do here.
240     if (cm->frame_refs[0].idx != INT_MAX)
241       cm->frame_refs[0].buf->corrupted = 1;
242   }
243
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);
249
250   if (setjmp(cm->error.jmp)) {
251     cm->error.setjmp = 0;
252
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.
256     //
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
259     // thing to do here.
260     if (cm->frame_refs[0].idx != INT_MAX)
261       cm->frame_refs[0].buf->corrupted = 1;
262
263     if (cm->frame_bufs[cm->new_fb_idx].ref_count > 0)
264       cm->frame_bufs[cm->new_fb_idx].ref_count--;
265
266     return -1;
267   }
268
269   cm->error.setjmp = 1;
270
271   retcode = vp9_decode_frame(pbi, source, source + size, psource);
272
273   if (retcode < 0) {
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--;
278     return retcode;
279   }
280
281   swap_frame_buffers(pbi);
282
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);
288     } else {
289       vp9_loop_filter_frame(cm, &pbi->mb, cm->lf.filter_level, 0, 0);
290     }
291   }
292
293   vp9_clear_system_state();
294
295   cm->last_width = cm->width;
296   cm->last_height = cm->height;
297
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);
303
304     cm->current_video_frame++;
305   }
306
307   pbi->ready_for_new_data = 0;
308   pbi->last_time_stamp = time_stamp;
309
310   cm->error.setjmp = 0;
311   return retcode;
312 }
313
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) {
317   int ret = -1;
318
319   if (pbi->ready_for_new_data == 1)
320     return ret;
321
322   /* ie no raw frame to show!!! */
323   if (pbi->common.show_frame == 0)
324     return ret;
325
326   pbi->ready_for_new_data = 1;
327   *time_stamp = pbi->last_time_stamp;
328   *time_end_stamp = 0;
329
330 #if CONFIG_VP9_POSTPROC
331   ret = vp9_post_proc_frame(&pbi->common, sd, flags);
332 #else
333     *sd = *pbi->common.frame_to_show;
334     ret = 0;
335 #endif /*!CONFIG_POSTPROC*/
336   vp9_clear_system_state();
337   return ret;
338 }