Merge "Detect toolchain based on gcc -dumpmachine"
[profile/ivi/libvpx.git] / vp8 / common / alloccommon.c
1 /*
2  *  Copyright (c) 2010 The VP8 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_ports/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 #include "vpxerrors.h"
20
21
22 extern  void vp8_init_scan_order_mask();
23
24 void vp8_update_mode_info_border(MODE_INFO *mi, int rows, int cols)
25 {
26     int i;
27     vpx_memset(mi - cols - 1, 0, sizeof(MODE_INFO) * cols + 1);
28
29     for (i = 0; i < rows; i++)
30     {
31         vpx_memset(&mi[i*cols-1], 0, sizeof(MODE_INFO));
32     }
33 }
34 void vp8_de_alloc_frame_buffers(VP8_COMMON *oci)
35 {
36     vp8_yv12_de_alloc_frame_buffer(&oci->temp_scale_frame);
37     vp8_yv12_de_alloc_frame_buffer(&oci->new_frame);
38     vp8_yv12_de_alloc_frame_buffer(&oci->last_frame);
39     vp8_yv12_de_alloc_frame_buffer(&oci->golden_frame);
40     vp8_yv12_de_alloc_frame_buffer(&oci->alt_ref_frame);
41     vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
42
43     vpx_free(oci->above_context[Y1CONTEXT]);
44     vpx_free(oci->above_context[UCONTEXT]);
45     vpx_free(oci->above_context[VCONTEXT]);
46     vpx_free(oci->above_context[Y2CONTEXT]);
47     vpx_free(oci->mip);
48
49     oci->above_context[Y1CONTEXT] = 0;
50     oci->above_context[UCONTEXT]  = 0;
51     oci->above_context[VCONTEXT]  = 0;
52     oci->above_context[Y2CONTEXT] = 0;
53     oci->mip = 0;
54
55     // Structure used to minitor GF useage
56     if (oci->gf_active_flags != 0)
57         vpx_free(oci->gf_active_flags);
58
59     oci->gf_active_flags = 0;
60 }
61
62 int vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height)
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     if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame,   width, 16, VP8BORDERINPIXELS) < 0)
75     {
76         vp8_de_alloc_frame_buffers(oci);
77         return ALLOC_FAILURE;
78     }
79
80
81     if (vp8_yv12_alloc_frame_buffer(&oci->new_frame,   width, height, VP8BORDERINPIXELS) < 0)
82     {
83         vp8_de_alloc_frame_buffers(oci);
84         return ALLOC_FAILURE;
85     }
86
87     if (vp8_yv12_alloc_frame_buffer(&oci->last_frame,  width, height, VP8BORDERINPIXELS) < 0)
88     {
89         vp8_de_alloc_frame_buffers(oci);
90         return ALLOC_FAILURE;
91     }
92
93     if (vp8_yv12_alloc_frame_buffer(&oci->golden_frame, width, height, VP8BORDERINPIXELS) < 0)
94     {
95         vp8_de_alloc_frame_buffers(oci);
96         return ALLOC_FAILURE;
97     }
98
99     if (vp8_yv12_alloc_frame_buffer(&oci->alt_ref_frame, width, height, VP8BORDERINPIXELS) < 0)
100     {
101         vp8_de_alloc_frame_buffers(oci);
102         return ALLOC_FAILURE;
103     }
104
105     if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height, VP8BORDERINPIXELS) < 0)
106     {
107         vp8_de_alloc_frame_buffers(oci);
108         return ALLOC_FAILURE;
109     }
110
111     oci->mb_rows = height >> 4;
112     oci->mb_cols = width >> 4;
113     oci->MBs = oci->mb_rows * oci->mb_cols;
114     oci->mode_info_stride = oci->mb_cols + 1;
115     oci->mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
116
117     if (!oci->mip)
118     {
119         vp8_de_alloc_frame_buffers(oci);
120         return ALLOC_FAILURE;
121     }
122
123     oci->mi = oci->mip + oci->mode_info_stride + 1;
124
125
126     oci->above_context[Y1CONTEXT] = vpx_calloc(sizeof(ENTROPY_CONTEXT) * oci->mb_cols * 4 , 1);
127
128     if (!oci->above_context[Y1CONTEXT])
129     {
130         vp8_de_alloc_frame_buffers(oci);
131         return ALLOC_FAILURE;
132     }
133
134     oci->above_context[UCONTEXT]  = vpx_calloc(sizeof(ENTROPY_CONTEXT) * oci->mb_cols * 2 , 1);
135
136     if (!oci->above_context[UCONTEXT])
137     {
138         vp8_de_alloc_frame_buffers(oci);
139         return ALLOC_FAILURE;
140     }
141
142     oci->above_context[VCONTEXT]  = vpx_calloc(sizeof(ENTROPY_CONTEXT) * oci->mb_cols * 2 , 1);
143
144     if (!oci->above_context[VCONTEXT])
145     {
146         vp8_de_alloc_frame_buffers(oci);
147         return ALLOC_FAILURE;
148     }
149
150     oci->above_context[Y2CONTEXT] = vpx_calloc(sizeof(ENTROPY_CONTEXT) * oci->mb_cols     , 1);
151
152     if (!oci->above_context[Y2CONTEXT])
153     {
154         vp8_de_alloc_frame_buffers(oci);
155         return ALLOC_FAILURE;
156     }
157
158     vp8_update_mode_info_border(oci->mi, oci->mb_rows, oci->mb_cols);
159
160     // Structures used to minitor GF usage
161     if (oci->gf_active_flags != 0)
162         vpx_free(oci->gf_active_flags);
163
164     oci->gf_active_flags = (unsigned char *)vpx_calloc(oci->mb_rows * oci->mb_cols, 1);
165
166     if (!oci->gf_active_flags)
167     {
168         vp8_de_alloc_frame_buffers(oci);
169         return ALLOC_FAILURE;
170     }
171
172     oci->gf_active_count = oci->mb_rows * oci->mb_cols;
173
174     return 0;
175 }
176 void vp8_setup_version(VP8_COMMON *cm)
177 {
178     switch (cm->version)
179     {
180     case 0:
181         cm->no_lpf = 0;
182         cm->simpler_lpf = 0;
183         cm->use_bilinear_mc_filter = 0;
184         cm->full_pixel = 0;
185         break;
186     case 1:
187         cm->no_lpf = 0;
188         cm->simpler_lpf = 1;
189         cm->use_bilinear_mc_filter = 1;
190         cm->full_pixel = 0;
191         break;
192     case 2:
193         cm->no_lpf = 1;
194         cm->simpler_lpf = 0;
195         cm->use_bilinear_mc_filter = 1;
196         cm->full_pixel = 0;
197         break;
198     case 3:
199         cm->no_lpf = 1;
200         cm->simpler_lpf = 1;
201         cm->use_bilinear_mc_filter = 1;
202         cm->full_pixel = 1;
203         break;
204     default:
205         //4,5,6,7 are reserved for future use
206         cm->no_lpf = 0;
207         cm->simpler_lpf = 0;
208         cm->use_bilinear_mc_filter = 0;
209         cm->full_pixel = 0;
210         break;
211     }
212 }
213 void vp8_create_common(VP8_COMMON *oci)
214 {
215     vp8_machine_specific_config(oci);
216     vp8_default_coef_probs(oci);
217     vp8_init_mbmode_probs(oci);
218     vp8_default_bmode_probs(oci->fc.bmode_prob);
219
220     oci->mb_no_coeff_skip = 1;
221     oci->no_lpf = 0;
222     oci->simpler_lpf = 0;
223     oci->use_bilinear_mc_filter = 0;
224     oci->full_pixel = 0;
225     oci->multi_token_partition = ONE_PARTITION;
226     oci->clr_type = REG_YUV;
227     oci->clamp_type = RECON_CLAMP_REQUIRED;
228
229     // Initialise reference frame sign bias structure to defaults
230     vpx_memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
231
232     // Default disable buffer to buffer copying
233     oci->copy_buffer_to_gf = 0;
234     oci->copy_buffer_to_arf = 0;
235 }
236
237 void vp8_remove_common(VP8_COMMON *oci)
238 {
239     vp8_de_alloc_frame_buffers(oci);
240 }
241
242 void vp8_initialize_common()
243 {
244     vp8_coef_tree_initialize();
245
246     vp8_entropy_mode_init();
247
248     vp8_init_scan_order_mask();
249
250 }