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