Merge remote branch 'internal/upstream' into HEAD
[platform/upstream/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 - 2, 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);
46     vpx_free(oci->mip);
47
48     oci->above_context = 0;
49     oci->mip = 0;
50
51 }
52
53 int vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height)
54 {
55     int i;
56
57     vp8_de_alloc_frame_buffers(oci);
58
59     // our internal buffers are always multiples of 16
60     if ((width & 0xf) != 0)
61         width += 16 - (width & 0xf);
62
63     if ((height & 0xf) != 0)
64         height += 16 - (height & 0xf);
65
66
67     for (i = 0; i < NUM_YV12_BUFFERS; i++)
68     {
69       oci->fb_idx_ref_cnt[0] = 0;
70
71       if (vp8_yv12_alloc_frame_buffer(&oci->yv12_fb[i],  width, height, VP8BORDERINPIXELS) < 0)
72         {
73             vp8_de_alloc_frame_buffers(oci);
74             return ALLOC_FAILURE;
75         }
76     }
77
78     oci->new_fb_idx = 0;
79     oci->lst_fb_idx = 1;
80     oci->gld_fb_idx = 2;
81     oci->alt_fb_idx = 3;
82
83     oci->fb_idx_ref_cnt[0] = 1;
84     oci->fb_idx_ref_cnt[1] = 1;
85     oci->fb_idx_ref_cnt[2] = 1;
86     oci->fb_idx_ref_cnt[3] = 1;
87
88     if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame,   width, 16, VP8BORDERINPIXELS) < 0)
89     {
90         vp8_de_alloc_frame_buffers(oci);
91         return ALLOC_FAILURE;
92     }
93
94     if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height, VP8BORDERINPIXELS) < 0)
95     {
96         vp8_de_alloc_frame_buffers(oci);
97         return ALLOC_FAILURE;
98     }
99
100     oci->mb_rows = height >> 4;
101     oci->mb_cols = width >> 4;
102     oci->MBs = oci->mb_rows * oci->mb_cols;
103     oci->mode_info_stride = oci->mb_cols + 1;
104     oci->mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
105
106     if (!oci->mip)
107     {
108         vp8_de_alloc_frame_buffers(oci);
109         return ALLOC_FAILURE;
110     }
111
112     oci->mi = oci->mip + oci->mode_info_stride + 1;
113
114
115     oci->above_context = vpx_calloc(sizeof(ENTROPY_CONTEXT_PLANES) * oci->mb_cols, 1);
116
117     if (!oci->above_context)
118     {
119         vp8_de_alloc_frame_buffers(oci);
120         return ALLOC_FAILURE;
121     }
122
123     vp8_update_mode_info_border(oci->mi, oci->mb_rows, oci->mb_cols);
124
125     return 0;
126 }
127 void vp8_setup_version(VP8_COMMON *cm)
128 {
129     if (cm->version & 0x4)
130     {
131         if (!CONFIG_EXPERIMENTAL)
132             vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
133                                "Bitstream was created by an experimental "
134                                "encoder");        
135         cm->experimental = 1;
136     }
137     
138     switch (cm->version & 0x3)
139     {
140     case 0:
141         cm->no_lpf = 0;
142         cm->simpler_lpf = 0;
143         cm->use_bilinear_mc_filter = 0;
144         cm->full_pixel = 0;
145         break;
146     case 1:
147         cm->no_lpf = 0;
148         cm->simpler_lpf = 1;
149         cm->use_bilinear_mc_filter = 1;
150         cm->full_pixel = 0;
151         break;
152     case 2:
153         cm->no_lpf = 1;
154         cm->simpler_lpf = 0;
155         cm->use_bilinear_mc_filter = 1;
156         cm->full_pixel = 0;
157         break;
158     case 3:
159         cm->no_lpf = 1;
160         cm->simpler_lpf = 1;
161         cm->use_bilinear_mc_filter = 1;
162         cm->full_pixel = 1;
163         break;
164     }
165 }
166 void vp8_create_common(VP8_COMMON *oci)
167 {
168     vp8_machine_specific_config(oci);
169     vp8_default_coef_probs(oci);
170     vp8_init_mbmode_probs(oci);
171     vp8_default_bmode_probs(oci->fc.bmode_prob);
172
173     oci->mb_no_coeff_skip = 1;
174     oci->no_lpf = 0;
175     oci->simpler_lpf = 0;
176     oci->use_bilinear_mc_filter = 0;
177     oci->full_pixel = 0;
178     oci->multi_token_partition = ONE_PARTITION;
179     oci->clr_type = REG_YUV;
180     oci->clamp_type = RECON_CLAMP_REQUIRED;
181
182     // Initialise reference frame sign bias structure to defaults
183     vpx_memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
184
185     // Default disable buffer to buffer copying
186     oci->copy_buffer_to_gf = 0;
187     oci->copy_buffer_to_arf = 0;
188 }
189
190 void vp8_remove_common(VP8_COMMON *oci)
191 {
192     vp8_de_alloc_frame_buffers(oci);
193 }
194
195 void vp8_initialize_common()
196 {
197     vp8_coef_tree_initialize();
198
199     vp8_entropy_mode_init();
200
201     vp8_init_scan_order_mask();
202
203 }