d58e49c34a9f562ce5f6da55cc03d362575d093c
[profile/ivi/libvpx.git] / vp8 / common / alloccommon.c
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
20
21 extern  void vp8_init_scan_order_mask();
22
23 static void update_mode_info_border(MODE_INFO *mi, int rows, int cols)
24 {
25     int i;
26     vpx_memset(mi - cols - 2, 0, sizeof(MODE_INFO) * (cols + 1));
27
28     for (i = 0; i < rows; i++)
29     {
30         /* TODO(holmer): Bug? This updates the last element of each row
31          * rather than the border element!
32          */
33         vpx_memset(&mi[i*cols-1], 0, sizeof(MODE_INFO));
34     }
35 }
36
37 void vp8_de_alloc_frame_buffers(VP8_COMMON *oci)
38 {
39     int i;
40     for (i = 0; i < NUM_YV12_BUFFERS; i++)
41         vp8_yv12_de_alloc_frame_buffer(&oci->yv12_fb[i]);
42
43     vp8_yv12_de_alloc_frame_buffer(&oci->temp_scale_frame);
44 #if CONFIG_POSTPROC
45     vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
46     if (oci->post_proc_buffer_int_used)
47         vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer_int);
48 #endif
49
50     vpx_free(oci->above_context);
51     vpx_free(oci->mip);
52     vpx_free(oci->prev_mip);
53
54     oci->above_context = 0;
55     oci->mip = 0;
56     oci->prev_mip = 0;
57
58 }
59
60 int vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height)
61 {
62     int i;
63
64     vp8_de_alloc_frame_buffers(oci);
65
66     /* our internal buffers are always multiples of 16 */
67     if ((width & 0xf) != 0)
68         width += 16 - (width & 0xf);
69
70     if ((height & 0xf) != 0)
71         height += 16 - (height & 0xf);
72
73
74     for (i = 0; i < NUM_YV12_BUFFERS; i++)
75     {
76         oci->fb_idx_ref_cnt[i] = 0;
77         oci->yv12_fb[i].flags = 0;
78         if (vp8_yv12_alloc_frame_buffer(&oci->yv12_fb[i], width, height, VP8BORDERINPIXELS) < 0)
79         {
80             vp8_de_alloc_frame_buffers(oci);
81             return 1;
82         }
83     }
84
85     oci->new_fb_idx = 0;
86     oci->lst_fb_idx = 1;
87     oci->gld_fb_idx = 2;
88     oci->alt_fb_idx = 3;
89
90     oci->fb_idx_ref_cnt[0] = 1;
91     oci->fb_idx_ref_cnt[1] = 1;
92     oci->fb_idx_ref_cnt[2] = 1;
93     oci->fb_idx_ref_cnt[3] = 1;
94
95     if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame,   width, 16, VP8BORDERINPIXELS) < 0)
96     {
97         vp8_de_alloc_frame_buffers(oci);
98         return 1;
99     }
100
101 #if CONFIG_POSTPROC
102     if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height, VP8BORDERINPIXELS) < 0)
103     {
104         vp8_de_alloc_frame_buffers(oci);
105         return 1;
106     }
107
108     oci->post_proc_buffer_int_used = 0;
109     vpx_memset(&oci->postproc_state, 0, sizeof(oci->postproc_state));
110     vpx_memset((&oci->post_proc_buffer)->buffer_alloc,128,(&oci->post_proc_buffer)->frame_size);
111 #endif
112
113     oci->mb_rows = height >> 4;
114     oci->mb_cols = width >> 4;
115     oci->MBs = oci->mb_rows * oci->mb_cols;
116     oci->mode_info_stride = oci->mb_cols + 1;
117     oci->mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
118
119     if (!oci->mip)
120     {
121         vp8_de_alloc_frame_buffers(oci);
122         return 1;
123     }
124
125     oci->mi = oci->mip + oci->mode_info_stride + 1;
126
127     /* allocate memory for last frame MODE_INFO array */
128 #if CONFIG_ERROR_CONCEALMENT
129     oci->prev_mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
130
131     if (!oci->prev_mip)
132     {
133         vp8_de_alloc_frame_buffers(oci);
134         return 1;
135     }
136
137     oci->prev_mi = oci->prev_mip + oci->mode_info_stride + 1;
138 #else
139     oci->prev_mip = NULL;
140     oci->prev_mi = NULL;
141 #endif
142
143     oci->above_context = vpx_calloc(sizeof(ENTROPY_CONTEXT_PLANES) * oci->mb_cols, 1);
144
145     if (!oci->above_context)
146     {
147         vp8_de_alloc_frame_buffers(oci);
148         return 1;
149     }
150
151     update_mode_info_border(oci->mi, oci->mb_rows, oci->mb_cols);
152 #if CONFIG_ERROR_CONCEALMENT
153     update_mode_info_border(oci->prev_mi, oci->mb_rows, oci->mb_cols);
154 #endif
155
156     return 0;
157 }
158 void vp8_setup_version(VP8_COMMON *cm)
159 {
160     switch (cm->version)
161     {
162     case 0:
163         cm->no_lpf = 0;
164         cm->filter_type = NORMAL_LOOPFILTER;
165         cm->use_bilinear_mc_filter = 0;
166         cm->full_pixel = 0;
167         break;
168     case 1:
169         cm->no_lpf = 0;
170         cm->filter_type = SIMPLE_LOOPFILTER;
171         cm->use_bilinear_mc_filter = 1;
172         cm->full_pixel = 0;
173         break;
174     case 2:
175         cm->no_lpf = 1;
176         cm->filter_type = NORMAL_LOOPFILTER;
177         cm->use_bilinear_mc_filter = 1;
178         cm->full_pixel = 0;
179         break;
180     case 3:
181         cm->no_lpf = 1;
182         cm->filter_type = SIMPLE_LOOPFILTER;
183         cm->use_bilinear_mc_filter = 1;
184         cm->full_pixel = 1;
185         break;
186     default:
187         /*4,5,6,7 are reserved for future use*/
188         cm->no_lpf = 0;
189         cm->filter_type = NORMAL_LOOPFILTER;
190         cm->use_bilinear_mc_filter = 0;
191         cm->full_pixel = 0;
192         break;
193     }
194 }
195 void vp8_create_common(VP8_COMMON *oci)
196 {
197     vp8_machine_specific_config(oci);
198
199     vp8_init_mbmode_probs(oci);
200     vp8_default_bmode_probs(oci->fc.bmode_prob);
201
202     oci->mb_no_coeff_skip = 1;
203     oci->no_lpf = 0;
204     oci->filter_type = NORMAL_LOOPFILTER;
205     oci->use_bilinear_mc_filter = 0;
206     oci->full_pixel = 0;
207     oci->multi_token_partition = ONE_PARTITION;
208     oci->clr_type = REG_YUV;
209     oci->clamp_type = RECON_CLAMP_REQUIRED;
210
211     /* Initialize reference frame sign bias structure to defaults */
212     vpx_memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
213
214     /* Default disable buffer to buffer copying */
215     oci->copy_buffer_to_gf = 0;
216     oci->copy_buffer_to_arf = 0;
217 }
218
219 void vp8_remove_common(VP8_COMMON *oci)
220 {
221     vp8_de_alloc_frame_buffers(oci);
222 }