Upstream version 9.38.198.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 static void initialize_dec() {
36   static int init_done = 0;
37
38   if (!init_done) {
39     vp9_init_neighbors();
40     init_done = 1;
41   }
42 }
43
44 VP9Decoder *vp9_decoder_create() {
45   VP9Decoder *const pbi = vpx_memalign(32, sizeof(*pbi));
46   VP9_COMMON *const cm = pbi ? &pbi->common : NULL;
47
48   if (!cm)
49     return NULL;
50
51   vp9_zero(*pbi);
52
53   if (setjmp(cm->error.jmp)) {
54     cm->error.setjmp = 0;
55     vp9_decoder_remove(pbi);
56     return NULL;
57   }
58
59   cm->error.setjmp = 1;
60   initialize_dec();
61
62   vp9_rtcd();
63
64   // Initialize the references to not point to any frame buffers.
65   vpx_memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
66
67   cm->current_video_frame = 0;
68   pbi->ready_for_new_data = 1;
69
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);
74
75   vp9_loop_filter_init(cm);
76
77   cm->error.setjmp = 0;
78
79   vp9_get_worker_interface()->init(&pbi->lf_worker);
80
81   return pbi;
82 }
83
84 void vp9_decoder_remove(VP9Decoder *pbi) {
85   VP9_COMMON *const cm = &pbi->common;
86   int i;
87
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);
96   }
97   vpx_free(pbi->tile_workers);
98
99   if (pbi->num_tile_workers) {
100     const int sb_rows =
101         mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
102     vp9_loop_filter_dealloc(&pbi->lf_row_sync, sb_rows);
103   }
104
105   vp9_remove_common(cm);
106   vpx_free(pbi);
107 }
108
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;
113 }
114
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;
119
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.
124    */
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");
131     else
132       vp8_yv12_copy_frame(cfg, sd);
133   } else {
134     vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
135                        "Invalid reference frame");
136   }
137
138   return cm->error.error_code;
139 }
140
141
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;
146
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];
157   } else {
158     vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
159                        "Invalid reference frame");
160     return cm->error.error_code;
161   }
162
163   if (!equal_dimensions(ref_buf->buf, sd)) {
164     vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
165                        "Incorrect buffer dimensions");
166   } else {
167     int *ref_fb_ptr = &ref_buf->idx;
168
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--;
174
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);
179   }
180
181   return cm->error.error_code;
182 }
183
184
185 int vp9_get_reference_dec(VP9Decoder *pbi, int index, YV12_BUFFER_CONFIG **fb) {
186   VP9_COMMON *cm = &pbi->common;
187
188   if (index < 0 || index >= REF_FRAMES)
189     return -1;
190
191   *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
192   return 0;
193 }
194
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;
199
200   for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
201     if (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],
204                  cm->new_fb_idx);
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);
208     }
209     ++ref_index;
210   }
211
212   cm->frame_to_show = get_frame_new_buffer(cm);
213   cm->frame_bufs[cm->new_fb_idx].ref_count--;
214
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;
218 }
219
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;
224   int retcode = 0;
225
226   cm->error.error_code = VPX_CODEC_OK;
227
228   if (size == 0) {
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.
233     //
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
236     // thing to do here.
237     if (cm->frame_refs[0].idx != INT_MAX)
238       cm->frame_refs[0].buf->corrupted = 1;
239   }
240
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);
246
247   if (setjmp(cm->error.jmp)) {
248     cm->error.setjmp = 0;
249     vp9_clear_system_state();
250
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.
254     //
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
257     // thing to do here.
258     if (cm->frame_refs[0].idx != INT_MAX && cm->frame_refs[0].buf != NULL)
259       cm->frame_refs[0].buf->corrupted = 1;
260
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--;
263
264     return -1;
265   }
266
267   cm->error.setjmp = 1;
268
269   vp9_decode_frame(pbi, source, source + size, psource);
270
271   swap_frame_buffers(pbi);
272
273   vp9_clear_system_state();
274
275   cm->last_width = cm->width;
276   cm->last_height = cm->height;
277
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);
283
284     cm->current_video_frame++;
285   }
286
287   pbi->ready_for_new_data = 0;
288
289   cm->error.setjmp = 0;
290   return retcode;
291 }
292
293 int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
294                       vp9_ppflags_t *flags) {
295   VP9_COMMON *const cm = &pbi->common;
296   int ret = -1;
297 #if !CONFIG_VP9_POSTPROC
298   (void)*flags;
299 #endif
300
301   if (pbi->ready_for_new_data == 1)
302     return ret;
303
304   /* no raw frame to show!!! */
305   if (!cm->show_frame)
306     return ret;
307
308   pbi->ready_for_new_data = 1;
309
310 #if CONFIG_VP9_POSTPROC
311   if (!cm->show_existing_frame) {
312     ret = vp9_post_proc_frame(cm, sd, flags);
313   } else {
314     *sd = *cm->frame_to_show;
315     ret = 0;
316   }
317 #else
318   *sd = *cm->frame_to_show;
319   ret = 0;
320 #endif /*!CONFIG_POSTPROC*/
321   vp9_clear_system_state();
322   return ret;
323 }