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