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