Swap alt/gold/new/last frame buffer ptrs instead of copying.
[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
35 void vp8_de_alloc_frame_buffers(VP8_COMMON *oci)
36 {
37     int i;
38
39     for (i = 0; i < NUM_YV12_BUFFERS; i++)
40         vp8_yv12_de_alloc_frame_buffer(&oci->yv12_fb[i]);
41
42     vp8_yv12_de_alloc_frame_buffer(&oci->temp_scale_frame);
43     vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
44
45     vpx_free(oci->above_context[Y1CONTEXT]);
46     vpx_free(oci->above_context[UCONTEXT]);
47     vpx_free(oci->above_context[VCONTEXT]);
48     vpx_free(oci->above_context[Y2CONTEXT]);
49     vpx_free(oci->mip);
50
51     oci->above_context[Y1CONTEXT] = 0;
52     oci->above_context[UCONTEXT]  = 0;
53     oci->above_context[VCONTEXT]  = 0;
54     oci->above_context[Y2CONTEXT] = 0;
55     oci->mip = 0;
56
57     // Structure used to minitor GF useage
58     if (oci->gf_active_flags != 0)
59         vpx_free(oci->gf_active_flags);
60
61     oci->gf_active_flags = 0;
62 }
63
64 int vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height)
65 {
66     int i;
67
68     vp8_de_alloc_frame_buffers(oci);
69
70     // our internal buffers are always multiples of 16
71     if ((width & 0xf) != 0)
72         width += 16 - (width & 0xf);
73
74     if ((height & 0xf) != 0)
75         height += 16 - (height & 0xf);
76
77
78     for (i = 0; i < NUM_YV12_BUFFERS; i++)
79     {
80       oci->fb_idx_ref_cnt[0] = 0;
81
82       if (vp8_yv12_alloc_frame_buffer(&oci->yv12_fb[i],  width, height, VP8BORDERINPIXELS) < 0)
83         {
84             vp8_de_alloc_frame_buffers(oci);
85             return ALLOC_FAILURE;
86         }
87     }
88
89     oci->new_fb_idx = 0;
90     oci->lst_fb_idx = 1;
91     oci->gld_fb_idx = 2;
92     oci->alt_fb_idx = 3;
93
94     oci->fb_idx_ref_cnt[0] = 1;
95     oci->fb_idx_ref_cnt[1] = 1;
96     oci->fb_idx_ref_cnt[2] = 1;
97     oci->fb_idx_ref_cnt[3] = 1;
98
99     if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame,   width, 16, 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 }