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 "vpx_config.h"
13#include "alloccommon.h"
14#include "blockd.h"
15#include "vpx_mem/vpx_mem.h"
16#include "onyxc_int.h"
17#include "findnearmv.h"
18#include "entropymode.h"
19#include "systemdependent.h"
20
21void vp8_de_alloc_frame_buffers(VP8_COMMON *oci)
22{
23    int i;
24    for (i = 0; i < NUM_YV12_BUFFERS; i++)
25        vp8_yv12_de_alloc_frame_buffer(&oci->yv12_fb[i]);
26
27    vp8_yv12_de_alloc_frame_buffer(&oci->temp_scale_frame);
28#if CONFIG_POSTPROC
29    vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
30    if (oci->post_proc_buffer_int_used)
31        vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer_int);
32
33    vpx_free(oci->pp_limits_buffer);
34    oci->pp_limits_buffer = NULL;
35#endif
36
37    vpx_free(oci->above_context);
38    vpx_free(oci->mip);
39#if CONFIG_ERROR_CONCEALMENT
40    vpx_free(oci->prev_mip);
41    oci->prev_mip = NULL;
42#endif
43
44    oci->above_context = NULL;
45    oci->mip = NULL;
46}
47
48int vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height)
49{
50    int i;
51
52    vp8_de_alloc_frame_buffers(oci);
53
54    /* our internal buffers are always multiples of 16 */
55    if ((width & 0xf) != 0)
56        width += 16 - (width & 0xf);
57
58    if ((height & 0xf) != 0)
59        height += 16 - (height & 0xf);
60
61
62    for (i = 0; i < NUM_YV12_BUFFERS; i++)
63    {
64        oci->fb_idx_ref_cnt[i] = 0;
65        oci->yv12_fb[i].flags = 0;
66        if (vp8_yv12_alloc_frame_buffer(&oci->yv12_fb[i], width, height, VP8BORDERINPIXELS) < 0)
67            goto allocation_fail;
68    }
69
70    oci->new_fb_idx = 0;
71    oci->lst_fb_idx = 1;
72    oci->gld_fb_idx = 2;
73    oci->alt_fb_idx = 3;
74
75    oci->fb_idx_ref_cnt[0] = 1;
76    oci->fb_idx_ref_cnt[1] = 1;
77    oci->fb_idx_ref_cnt[2] = 1;
78    oci->fb_idx_ref_cnt[3] = 1;
79
80    if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame,   width, 16, VP8BORDERINPIXELS) < 0)
81        goto allocation_fail;
82
83    oci->mb_rows = height >> 4;
84    oci->mb_cols = width >> 4;
85    oci->MBs = oci->mb_rows * oci->mb_cols;
86    oci->mode_info_stride = oci->mb_cols + 1;
87    oci->mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
88
89    if (!oci->mip)
90        goto allocation_fail;
91
92    oci->mi = oci->mip + oci->mode_info_stride + 1;
93
94    /* Allocation of previous mode info will be done in vp8_decode_frame()
95     * as it is a decoder only data */
96
97    oci->above_context = vpx_calloc(sizeof(ENTROPY_CONTEXT_PLANES) * oci->mb_cols, 1);
98
99    if (!oci->above_context)
100        goto allocation_fail;
101
102#if CONFIG_POSTPROC
103    if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height, VP8BORDERINPIXELS) < 0)
104        goto allocation_fail;
105
106    oci->post_proc_buffer_int_used = 0;
107    memset(&oci->postproc_state, 0, sizeof(oci->postproc_state));
108    memset(oci->post_proc_buffer.buffer_alloc, 128,
109           oci->post_proc_buffer.frame_size);
110
111    /* Allocate buffer to store post-processing filter coefficients.
112     *
113     * Note: Round up mb_cols to support SIMD reads
114     */
115    oci->pp_limits_buffer = vpx_memalign(16, 24 * ((oci->mb_cols + 1) & ~1));
116    if (!oci->pp_limits_buffer)
117        goto allocation_fail;
118#endif
119
120    return 0;
121
122allocation_fail:
123    vp8_de_alloc_frame_buffers(oci);
124    return 1;
125}
126
127void vp8_setup_version(VP8_COMMON *cm)
128{
129    switch (cm->version)
130    {
131    case 0:
132        cm->no_lpf = 0;
133        cm->filter_type = NORMAL_LOOPFILTER;
134        cm->use_bilinear_mc_filter = 0;
135        cm->full_pixel = 0;
136        break;
137    case 1:
138        cm->no_lpf = 0;
139        cm->filter_type = SIMPLE_LOOPFILTER;
140        cm->use_bilinear_mc_filter = 1;
141        cm->full_pixel = 0;
142        break;
143    case 2:
144        cm->no_lpf = 1;
145        cm->filter_type = NORMAL_LOOPFILTER;
146        cm->use_bilinear_mc_filter = 1;
147        cm->full_pixel = 0;
148        break;
149    case 3:
150        cm->no_lpf = 1;
151        cm->filter_type = SIMPLE_LOOPFILTER;
152        cm->use_bilinear_mc_filter = 1;
153        cm->full_pixel = 1;
154        break;
155    default:
156        /*4,5,6,7 are reserved for future use*/
157        cm->no_lpf = 0;
158        cm->filter_type = NORMAL_LOOPFILTER;
159        cm->use_bilinear_mc_filter = 0;
160        cm->full_pixel = 0;
161        break;
162    }
163}
164void vp8_create_common(VP8_COMMON *oci)
165{
166    vp8_machine_specific_config(oci);
167
168    vp8_init_mbmode_probs(oci);
169    vp8_default_bmode_probs(oci->fc.bmode_prob);
170
171    oci->mb_no_coeff_skip = 1;
172    oci->no_lpf = 0;
173    oci->filter_type = NORMAL_LOOPFILTER;
174    oci->use_bilinear_mc_filter = 0;
175    oci->full_pixel = 0;
176    oci->multi_token_partition = ONE_PARTITION;
177    oci->clamp_type = RECON_CLAMP_REQUIRED;
178
179    /* Initialize reference frame sign bias structure to defaults */
180    memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
181
182    /* Default disable buffer to buffer copying */
183    oci->copy_buffer_to_gf = 0;
184    oci->copy_buffer_to_arf = 0;
185}
186
187void vp8_remove_common(VP8_COMMON *oci)
188{
189    vp8_de_alloc_frame_buffers(oci);
190}
191