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
12#include "vp8/common/onyxc_int.h"
13#if CONFIG_POSTPROC
14#include "vp8/common/postproc.h"
15#endif
16#include "vp8/common/onyxd.h"
17#include "onyxd_int.h"
18#include "vpx_mem/vpx_mem.h"
19#include "vp8/common/alloccommon.h"
20#include "vp8/common/loopfilter.h"
21#include "vp8/common/swapyv12buffer.h"
22#include "vp8/common/threading.h"
23#include "decoderthreading.h"
24#include <stdio.h>
25#include <assert.h>
26
27#include "vp8/common/quant_common.h"
28#include "vp8/common/reconintra.h"
29#include "./vpx_dsp_rtcd.h"
30#include "./vpx_scale_rtcd.h"
31#include "vpx_scale/vpx_scale.h"
32#include "vp8/common/systemdependent.h"
33#include "vpx_ports/vpx_once.h"
34#include "vpx_ports/vpx_timer.h"
35#include "detokenize.h"
36#if CONFIG_ERROR_CONCEALMENT
37#include "error_concealment.h"
38#endif
39#if ARCH_ARM
40#include "vpx_ports/arm.h"
41#endif
42
43extern void vp8_init_loop_filter(VP8_COMMON *cm);
44extern void vp8cx_init_de_quantizer(VP8D_COMP *pbi);
45static int get_free_fb (VP8_COMMON *cm);
46static void ref_cnt_fb (int *buf, int *idx, int new_idx);
47
48static void initialize_dec(void) {
49    static volatile int init_done = 0;
50
51    if (!init_done)
52    {
53        vpx_dsp_rtcd();
54        vp8_init_intra_predictors();
55        init_done = 1;
56    }
57}
58
59static void remove_decompressor(VP8D_COMP *pbi)
60{
61#if CONFIG_ERROR_CONCEALMENT
62    vp8_de_alloc_overlap_lists(pbi);
63#endif
64    vp8_remove_common(&pbi->common);
65    vpx_free(pbi);
66}
67
68static struct VP8D_COMP * create_decompressor(VP8D_CONFIG *oxcf)
69{
70    VP8D_COMP *pbi = vpx_memalign(32, sizeof(VP8D_COMP));
71
72    if (!pbi)
73        return NULL;
74
75    memset(pbi, 0, sizeof(VP8D_COMP));
76
77    if (setjmp(pbi->common.error.jmp))
78    {
79        pbi->common.error.setjmp = 0;
80        remove_decompressor(pbi);
81        return 0;
82    }
83
84    pbi->common.error.setjmp = 1;
85
86    vp8_create_common(&pbi->common);
87
88    pbi->common.current_video_frame = 0;
89    pbi->ready_for_new_data = 1;
90
91    /* vp8cx_init_de_quantizer() is first called here. Add check in frame_init_dequantizer() to avoid
92     *  unnecessary calling of vp8cx_init_de_quantizer() for every frame.
93     */
94    vp8cx_init_de_quantizer(pbi);
95
96    vp8_loop_filter_init(&pbi->common);
97
98    pbi->common.error.setjmp = 0;
99
100#if CONFIG_ERROR_CONCEALMENT
101    pbi->ec_enabled = oxcf->error_concealment;
102    pbi->overlaps = NULL;
103#else
104    (void)oxcf;
105    pbi->ec_enabled = 0;
106#endif
107    /* Error concealment is activated after a key frame has been
108     * decoded without errors when error concealment is enabled.
109     */
110    pbi->ec_active = 0;
111
112    pbi->decoded_key_frame = 0;
113
114    /* Independent partitions is activated when a frame updates the
115     * token probability table to have equal probabilities over the
116     * PREV_COEF context.
117     */
118    pbi->independent_partitions = 0;
119
120    vp8_setup_block_dptrs(&pbi->mb);
121
122    once(initialize_dec);
123
124    return pbi;
125}
126
127vpx_codec_err_t vp8dx_get_reference(VP8D_COMP *pbi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
128{
129    VP8_COMMON *cm = &pbi->common;
130    int ref_fb_idx;
131
132    if (ref_frame_flag == VP8_LAST_FRAME)
133        ref_fb_idx = cm->lst_fb_idx;
134    else if (ref_frame_flag == VP8_GOLD_FRAME)
135        ref_fb_idx = cm->gld_fb_idx;
136    else if (ref_frame_flag == VP8_ALTR_FRAME)
137        ref_fb_idx = cm->alt_fb_idx;
138    else{
139        vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
140            "Invalid reference frame");
141        return pbi->common.error.error_code;
142    }
143
144    if(cm->yv12_fb[ref_fb_idx].y_height != sd->y_height ||
145        cm->yv12_fb[ref_fb_idx].y_width != sd->y_width ||
146        cm->yv12_fb[ref_fb_idx].uv_height != sd->uv_height ||
147        cm->yv12_fb[ref_fb_idx].uv_width != sd->uv_width){
148        vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
149            "Incorrect buffer dimensions");
150    }
151    else
152        vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
153
154    return pbi->common.error.error_code;
155}
156
157
158vpx_codec_err_t vp8dx_set_reference(VP8D_COMP *pbi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
159{
160    VP8_COMMON *cm = &pbi->common;
161    int *ref_fb_ptr = NULL;
162    int free_fb;
163
164    if (ref_frame_flag == VP8_LAST_FRAME)
165        ref_fb_ptr = &cm->lst_fb_idx;
166    else if (ref_frame_flag == VP8_GOLD_FRAME)
167        ref_fb_ptr = &cm->gld_fb_idx;
168    else if (ref_frame_flag == VP8_ALTR_FRAME)
169        ref_fb_ptr = &cm->alt_fb_idx;
170    else{
171        vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
172            "Invalid reference frame");
173        return pbi->common.error.error_code;
174    }
175
176    if(cm->yv12_fb[*ref_fb_ptr].y_height != sd->y_height ||
177        cm->yv12_fb[*ref_fb_ptr].y_width != sd->y_width ||
178        cm->yv12_fb[*ref_fb_ptr].uv_height != sd->uv_height ||
179        cm->yv12_fb[*ref_fb_ptr].uv_width != sd->uv_width){
180        vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
181            "Incorrect buffer dimensions");
182    }
183    else{
184        /* Find an empty frame buffer. */
185        free_fb = get_free_fb(cm);
186        /* Decrease fb_idx_ref_cnt since it will be increased again in
187         * ref_cnt_fb() below. */
188        cm->fb_idx_ref_cnt[free_fb]--;
189
190        /* Manage the reference counters and copy image. */
191        ref_cnt_fb (cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb);
192        vp8_yv12_copy_frame(sd, &cm->yv12_fb[*ref_fb_ptr]);
193    }
194
195   return pbi->common.error.error_code;
196}
197
198static int get_free_fb (VP8_COMMON *cm)
199{
200    int i;
201    for (i = 0; i < NUM_YV12_BUFFERS; i++)
202        if (cm->fb_idx_ref_cnt[i] == 0)
203            break;
204
205    assert(i < NUM_YV12_BUFFERS);
206    cm->fb_idx_ref_cnt[i] = 1;
207    return i;
208}
209
210static void ref_cnt_fb (int *buf, int *idx, int new_idx)
211{
212    if (buf[*idx] > 0)
213        buf[*idx]--;
214
215    *idx = new_idx;
216
217    buf[new_idx]++;
218}
219
220/* If any buffer copy / swapping is signalled it should be done here. */
221static int swap_frame_buffers (VP8_COMMON *cm)
222{
223    int err = 0;
224
225    /* The alternate reference frame or golden frame can be updated
226     *  using the new, last, or golden/alt ref frame.  If it
227     *  is updated using the newly decoded frame it is a refresh.
228     *  An update using the last or golden/alt ref frame is a copy.
229     */
230    if (cm->copy_buffer_to_arf)
231    {
232        int new_fb = 0;
233
234        if (cm->copy_buffer_to_arf == 1)
235            new_fb = cm->lst_fb_idx;
236        else if (cm->copy_buffer_to_arf == 2)
237            new_fb = cm->gld_fb_idx;
238        else
239            err = -1;
240
241        ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
242    }
243
244    if (cm->copy_buffer_to_gf)
245    {
246        int new_fb = 0;
247
248        if (cm->copy_buffer_to_gf == 1)
249            new_fb = cm->lst_fb_idx;
250        else if (cm->copy_buffer_to_gf == 2)
251            new_fb = cm->alt_fb_idx;
252        else
253            err = -1;
254
255        ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
256    }
257
258    if (cm->refresh_golden_frame)
259        ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
260
261    if (cm->refresh_alt_ref_frame)
262        ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
263
264    if (cm->refresh_last_frame)
265    {
266        ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
267
268        cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
269    }
270    else
271        cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
272
273    cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
274
275    return err;
276}
277
278static int check_fragments_for_errors(VP8D_COMP *pbi)
279{
280    if (!pbi->ec_active &&
281        pbi->fragments.count <= 1 && pbi->fragments.sizes[0] == 0)
282    {
283        VP8_COMMON *cm = &pbi->common;
284
285        /* If error concealment is disabled we won't signal missing frames
286         * to the decoder.
287         */
288        if (cm->fb_idx_ref_cnt[cm->lst_fb_idx] > 1)
289        {
290            /* The last reference shares buffer with another reference
291             * buffer. Move it to its own buffer before setting it as
292             * corrupt, otherwise we will make multiple buffers corrupt.
293             */
294            const int prev_idx = cm->lst_fb_idx;
295            cm->fb_idx_ref_cnt[prev_idx]--;
296            cm->lst_fb_idx = get_free_fb(cm);
297            vp8_yv12_copy_frame(&cm->yv12_fb[prev_idx],
298                                    &cm->yv12_fb[cm->lst_fb_idx]);
299        }
300        /* This is used to signal that we are missing frames.
301         * We do not know if the missing frame(s) was supposed to update
302         * any of the reference buffers, but we act conservative and
303         * mark only the last buffer as corrupted.
304         */
305        cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
306
307        /* Signal that we have no frame to show. */
308        cm->show_frame = 0;
309
310        /* Nothing more to do. */
311        return 0;
312    }
313
314    return 1;
315}
316
317int vp8dx_receive_compressed_data(VP8D_COMP *pbi, size_t size,
318                                  const uint8_t *source,
319                                  int64_t time_stamp)
320{
321    VP8_COMMON *cm = &pbi->common;
322    int retcode = -1;
323    (void)size;
324    (void)source;
325
326    pbi->common.error.error_code = VPX_CODEC_OK;
327
328    retcode = check_fragments_for_errors(pbi);
329    if(retcode <= 0)
330        return retcode;
331
332    cm->new_fb_idx = get_free_fb (cm);
333
334    /* setup reference frames for vp8_decode_frame */
335    pbi->dec_fb_ref[INTRA_FRAME]  = &cm->yv12_fb[cm->new_fb_idx];
336    pbi->dec_fb_ref[LAST_FRAME]   = &cm->yv12_fb[cm->lst_fb_idx];
337    pbi->dec_fb_ref[GOLDEN_FRAME] = &cm->yv12_fb[cm->gld_fb_idx];
338    pbi->dec_fb_ref[ALTREF_FRAME] = &cm->yv12_fb[cm->alt_fb_idx];
339
340    if (setjmp(pbi->common.error.jmp))
341    {
342       /* We do not know if the missing frame(s) was supposed to update
343        * any of the reference buffers, but we act conservative and
344        * mark only the last buffer as corrupted.
345        */
346        cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
347
348        if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
349          cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
350
351        goto decode_exit;
352    }
353
354    pbi->common.error.setjmp = 1;
355
356    retcode = vp8_decode_frame(pbi);
357
358    if (retcode < 0)
359    {
360        if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
361          cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
362
363        pbi->common.error.error_code = VPX_CODEC_ERROR;
364        goto decode_exit;
365    }
366
367    if (swap_frame_buffers (cm))
368    {
369        pbi->common.error.error_code = VPX_CODEC_ERROR;
370        goto decode_exit;
371    }
372
373    vp8_clear_system_state();
374
375    if (cm->show_frame)
376    {
377        cm->current_video_frame++;
378        cm->show_frame_mi = cm->mi;
379    }
380
381    #if CONFIG_ERROR_CONCEALMENT
382    /* swap the mode infos to storage for future error concealment */
383    if (pbi->ec_enabled && pbi->common.prev_mi)
384    {
385        MODE_INFO* tmp = pbi->common.prev_mi;
386        int row, col;
387        pbi->common.prev_mi = pbi->common.mi;
388        pbi->common.mi = tmp;
389
390        /* Propagate the segment_ids to the next frame */
391        for (row = 0; row < pbi->common.mb_rows; ++row)
392        {
393            for (col = 0; col < pbi->common.mb_cols; ++col)
394            {
395                const int i = row*pbi->common.mode_info_stride + col;
396                pbi->common.mi[i].mbmi.segment_id =
397                        pbi->common.prev_mi[i].mbmi.segment_id;
398            }
399        }
400    }
401#endif
402
403    pbi->ready_for_new_data = 0;
404    pbi->last_time_stamp = time_stamp;
405
406decode_exit:
407    pbi->common.error.setjmp = 0;
408    vp8_clear_system_state();
409    return retcode;
410}
411int vp8dx_get_raw_frame(VP8D_COMP *pbi, YV12_BUFFER_CONFIG *sd, int64_t *time_stamp, int64_t *time_end_stamp, vp8_ppflags_t *flags)
412{
413    int ret = -1;
414
415    if (pbi->ready_for_new_data == 1)
416        return ret;
417
418    /* ie no raw frame to show!!! */
419    if (pbi->common.show_frame == 0)
420        return ret;
421
422    pbi->ready_for_new_data = 1;
423    *time_stamp = pbi->last_time_stamp;
424    *time_end_stamp = 0;
425
426#if CONFIG_POSTPROC
427    ret = vp8_post_proc_frame(&pbi->common, sd, flags);
428#else
429    (void)flags;
430
431    if (pbi->common.frame_to_show)
432    {
433        *sd = *pbi->common.frame_to_show;
434        sd->y_width = pbi->common.Width;
435        sd->y_height = pbi->common.Height;
436        sd->uv_height = pbi->common.Height / 2;
437        ret = 0;
438    }
439    else
440    {
441        ret = -1;
442    }
443
444#endif /*!CONFIG_POSTPROC*/
445    vp8_clear_system_state();
446    return ret;
447}
448
449
450/* This function as written isn't decoder specific, but the encoder has
451 * much faster ways of computing this, so it's ok for it to live in a
452 * decode specific file.
453 */
454int vp8dx_references_buffer( VP8_COMMON *oci, int ref_frame )
455{
456    const MODE_INFO *mi = oci->mi;
457    int mb_row, mb_col;
458
459    for (mb_row = 0; mb_row < oci->mb_rows; mb_row++)
460    {
461        for (mb_col = 0; mb_col < oci->mb_cols; mb_col++,mi++)
462        {
463            if( mi->mbmi.ref_frame == ref_frame)
464              return 1;
465        }
466        mi++;
467    }
468    return 0;
469
470}
471
472int vp8_create_decoder_instances(struct frame_buffers *fb, VP8D_CONFIG *oxcf)
473{
474    if(!fb->use_frame_threads)
475    {
476        /* decoder instance for single thread mode */
477        fb->pbi[0] = create_decompressor(oxcf);
478        if(!fb->pbi[0])
479            return VPX_CODEC_ERROR;
480
481#if CONFIG_MULTITHREAD
482        /* enable row-based threading only when use_frame_threads
483         * is disabled */
484        fb->pbi[0]->max_threads = oxcf->max_threads;
485        vp8_decoder_create_threads(fb->pbi[0]);
486#endif
487    }
488    else
489    {
490        /* TODO : create frame threads and decoder instances for each
491         * thread here */
492    }
493
494    return VPX_CODEC_OK;
495}
496
497int vp8_remove_decoder_instances(struct frame_buffers *fb)
498{
499    if(!fb->use_frame_threads)
500    {
501        VP8D_COMP *pbi = fb->pbi[0];
502
503        if (!pbi)
504            return VPX_CODEC_ERROR;
505#if CONFIG_MULTITHREAD
506        if (pbi->b_multithreaded_rd)
507            vp8mt_de_alloc_temp_buffers(pbi, pbi->common.mb_rows);
508        vp8_decoder_remove_threads(pbi);
509#endif
510
511        /* decoder instance for single thread mode */
512        remove_decompressor(pbi);
513    }
514    else
515    {
516        /* TODO : remove frame threads and decoder instances for each
517         * thread here */
518    }
519
520    return VPX_CODEC_OK;
521}
522