OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "./vpx_config.h" | 11 #include "./vpx_config.h" |
12 #include "vpx_mem/vpx_mem.h" | 12 #include "vpx_mem/vpx_mem.h" |
13 | 13 |
14 #include "vp9/common/vp9_blockd.h" | 14 #include "vp9/common/vp9_blockd.h" |
15 #include "vp9/common/vp9_entropymode.h" | 15 #include "vp9/common/vp9_entropymode.h" |
16 #include "vp9/common/vp9_entropymv.h" | 16 #include "vp9/common/vp9_entropymv.h" |
17 #include "vp9/common/vp9_onyxc_int.h" | 17 #include "vp9/common/vp9_onyxc_int.h" |
18 #include "vp9/common/vp9_systemdependent.h" | 18 #include "vp9/common/vp9_systemdependent.h" |
19 | 19 |
20 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic | |
21 // frame reference count. | |
22 void lock_buffer_pool(BufferPool *const pool) { | |
23 #if CONFIG_MULTITHREAD | |
24 pthread_mutex_lock(&pool->pool_mutex); | |
25 #else | |
26 (void)pool; | |
27 #endif | |
28 } | |
29 | |
30 void unlock_buffer_pool(BufferPool *const pool) { | |
31 #if CONFIG_MULTITHREAD | |
32 pthread_mutex_unlock(&pool->pool_mutex); | |
33 #else | |
34 (void)pool; | |
35 #endif | |
36 } | |
37 | |
38 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) { | 20 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) { |
39 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2); | 21 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2); |
40 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2); | 22 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2); |
41 | 23 |
42 cm->mi_cols = aligned_width >> MI_SIZE_LOG2; | 24 cm->mi_cols = aligned_width >> MI_SIZE_LOG2; |
43 cm->mi_rows = aligned_height >> MI_SIZE_LOG2; | 25 cm->mi_rows = aligned_height >> MI_SIZE_LOG2; |
44 cm->mi_stride = calc_mi_size(cm->mi_cols); | 26 cm->mi_stride = calc_mi_size(cm->mi_cols); |
45 | 27 |
46 cm->mb_cols = (cm->mi_cols + 1) >> 1; | 28 cm->mb_cols = (cm->mi_cols + 1) >> 1; |
47 cm->mb_rows = (cm->mi_rows + 1) >> 1; | 29 cm->mb_rows = (cm->mi_rows + 1) >> 1; |
48 cm->MBs = cm->mb_rows * cm->mb_cols; | 30 cm->MBs = cm->mb_rows * cm->mb_cols; |
49 } | 31 } |
50 | 32 |
51 static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) { | |
52 int i; | |
53 | |
54 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) { | |
55 cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1); | |
56 if (cm->seg_map_array[i] == NULL) | |
57 return 1; | |
58 } | |
59 | |
60 // Init the index. | |
61 cm->seg_map_idx = 0; | |
62 cm->prev_seg_map_idx = 1; | |
63 | |
64 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx]; | |
65 if (!cm->frame_parallel_decode) | |
66 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx]; | |
67 | |
68 return 0; | |
69 } | |
70 | |
71 static void free_seg_map(VP9_COMMON *cm) { | |
72 int i; | |
73 | |
74 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) { | |
75 vpx_free(cm->seg_map_array[i]); | |
76 cm->seg_map_array[i] = NULL; | |
77 } | |
78 | |
79 cm->current_frame_seg_map = NULL; | |
80 | |
81 if (!cm->frame_parallel_decode) { | |
82 cm->last_frame_seg_map = NULL; | |
83 } | |
84 } | |
85 | |
86 void vp9_free_ref_frame_buffers(VP9_COMMON *cm) { | 33 void vp9_free_ref_frame_buffers(VP9_COMMON *cm) { |
87 BufferPool *const pool = cm->buffer_pool; | |
88 int i; | 34 int i; |
89 | 35 |
90 for (i = 0; i < FRAME_BUFFERS; ++i) { | 36 for (i = 0; i < FRAME_BUFFERS; ++i) { |
91 if (pool->frame_bufs[i].ref_count > 0 && | 37 if (cm->frame_bufs[i].ref_count > 0 && |
92 pool->frame_bufs[i].raw_frame_buffer.data != NULL) { | 38 cm->frame_bufs[i].raw_frame_buffer.data != NULL) { |
93 pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer); | 39 cm->release_fb_cb(cm->cb_priv, &cm->frame_bufs[i].raw_frame_buffer); |
94 pool->frame_bufs[i].ref_count = 0; | 40 cm->frame_bufs[i].ref_count = 0; |
95 } | 41 } |
96 vpx_free(pool->frame_bufs[i].mvs); | 42 vpx_free(cm->frame_bufs[i].mvs); |
97 pool->frame_bufs[i].mvs = NULL; | 43 cm->frame_bufs[i].mvs = NULL; |
98 vp9_free_frame_buffer(&pool->frame_bufs[i].buf); | 44 vp9_free_frame_buffer(&cm->frame_bufs[i].buf); |
99 } | 45 } |
100 | 46 |
101 #if CONFIG_VP9_POSTPROC | 47 #if CONFIG_VP9_POSTPROC |
102 vp9_free_frame_buffer(&cm->post_proc_buffer); | 48 vp9_free_frame_buffer(&cm->post_proc_buffer); |
103 vp9_free_frame_buffer(&cm->post_proc_buffer_int); | 49 vp9_free_frame_buffer(&cm->post_proc_buffer_int); |
104 #endif | 50 #endif |
105 } | 51 } |
106 | 52 |
107 void vp9_free_context_buffers(VP9_COMMON *cm) { | 53 void vp9_free_context_buffers(VP9_COMMON *cm) { |
108 cm->free_mi(cm); | 54 cm->free_mi(cm); |
109 free_seg_map(cm); | 55 vpx_free(cm->last_frame_seg_map); |
| 56 cm->last_frame_seg_map = NULL; |
110 vpx_free(cm->above_context); | 57 vpx_free(cm->above_context); |
111 cm->above_context = NULL; | 58 cm->above_context = NULL; |
112 vpx_free(cm->above_seg_context); | 59 vpx_free(cm->above_seg_context); |
113 cm->above_seg_context = NULL; | 60 cm->above_seg_context = NULL; |
114 } | 61 } |
115 | 62 |
116 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) { | 63 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) { |
117 vp9_free_context_buffers(cm); | 64 vp9_free_context_buffers(cm); |
118 | 65 |
119 vp9_set_mb_mi(cm, width, height); | 66 vp9_set_mb_mi(cm, width, height); |
120 if (cm->alloc_mi(cm, cm->mi_stride * calc_mi_size(cm->mi_rows))) | 67 if (cm->alloc_mi(cm, cm->mi_stride * calc_mi_size(cm->mi_rows))) |
121 goto fail; | 68 goto fail; |
122 | 69 |
123 // Create the segmentation map structure and set to 0. | 70 cm->last_frame_seg_map = (uint8_t *)vpx_calloc(cm->mi_rows * cm->mi_cols, 1); |
124 free_seg_map(cm); | 71 if (!cm->last_frame_seg_map) goto fail; |
125 if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) | |
126 goto fail; | |
127 | 72 |
128 cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc( | 73 cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc( |
129 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE, | 74 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE, |
130 sizeof(*cm->above_context)); | 75 sizeof(*cm->above_context)); |
131 if (!cm->above_context) goto fail; | 76 if (!cm->above_context) goto fail; |
132 | 77 |
133 cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc( | 78 cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc( |
134 mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context)); | 79 mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context)); |
135 if (!cm->above_seg_context) goto fail; | 80 if (!cm->above_seg_context) goto fail; |
136 | 81 |
137 return 0; | 82 return 0; |
138 | 83 |
139 fail: | 84 fail: |
140 vp9_free_context_buffers(cm); | 85 vp9_free_context_buffers(cm); |
141 return 1; | 86 return 1; |
142 } | 87 } |
143 | 88 |
144 static void init_frame_bufs(VP9_COMMON *cm) { | 89 static void init_frame_bufs(VP9_COMMON *cm) { |
145 BufferPool *const pool = cm->buffer_pool; | |
146 int i; | 90 int i; |
147 | 91 |
148 cm->new_fb_idx = FRAME_BUFFERS - 1; | 92 cm->new_fb_idx = FRAME_BUFFERS - 1; |
149 pool->frame_bufs[cm->new_fb_idx].ref_count = 1; | 93 cm->frame_bufs[cm->new_fb_idx].ref_count = 1; |
150 | 94 |
151 for (i = 0; i < REF_FRAMES; ++i) { | 95 for (i = 0; i < REF_FRAMES; ++i) { |
152 cm->ref_frame_map[i] = i; | 96 cm->ref_frame_map[i] = i; |
153 pool->frame_bufs[i].ref_count = 1; | 97 cm->frame_bufs[i].ref_count = 1; |
154 } | 98 } |
155 } | 99 } |
156 | 100 |
157 int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) { | 101 int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) { |
158 int i; | 102 int i; |
159 const int ss_x = cm->subsampling_x; | 103 const int ss_x = cm->subsampling_x; |
160 const int ss_y = cm->subsampling_y; | 104 const int ss_y = cm->subsampling_y; |
161 | 105 |
162 vp9_free_ref_frame_buffers(cm); | 106 vp9_free_ref_frame_buffers(cm); |
163 | 107 |
164 for (i = 0; i < FRAME_BUFFERS; ++i) { | 108 for (i = 0; i < FRAME_BUFFERS; ++i) { |
165 BufferPool *const pool = cm->buffer_pool; | 109 cm->frame_bufs[i].ref_count = 0; |
166 pool->frame_bufs[i].ref_count = 0; | 110 if (vp9_alloc_frame_buffer(&cm->frame_bufs[i].buf, width, height, |
167 if (vp9_alloc_frame_buffer(&pool->frame_bufs[i].buf, width, height, | |
168 ss_x, ss_y, | 111 ss_x, ss_y, |
169 #if CONFIG_VP9_HIGHBITDEPTH | 112 #if CONFIG_VP9_HIGHBITDEPTH |
170 cm->use_highbitdepth, | 113 cm->use_highbitdepth, |
171 #endif | 114 #endif |
172 VP9_ENC_BORDER_IN_PIXELS, | 115 VP9_ENC_BORDER_IN_PIXELS, |
173 cm->byte_alignment) < 0) | 116 cm->byte_alignment) < 0) |
174 goto fail; | 117 goto fail; |
175 if (pool->frame_bufs[i].mvs == NULL) { | 118 if (cm->frame_bufs[i].mvs == NULL) { |
176 pool->frame_bufs[i].mvs = | 119 cm->frame_bufs[i].mvs = |
177 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols, | 120 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols, |
178 sizeof(*pool->frame_bufs[i].mvs)); | 121 sizeof(*cm->frame_bufs[i].mvs)); |
179 if (pool->frame_bufs[i].mvs == NULL) | 122 if (cm->frame_bufs[i].mvs == NULL) |
180 goto fail; | 123 goto fail; |
181 | 124 |
182 pool->frame_bufs[i].mi_rows = cm->mi_rows; | 125 cm->frame_bufs[i].mi_rows = cm->mi_rows; |
183 pool->frame_bufs[i].mi_cols = cm->mi_cols; | 126 cm->frame_bufs[i].mi_cols = cm->mi_cols; |
184 } | 127 } |
185 } | 128 } |
186 | 129 |
187 init_frame_bufs(cm); | 130 init_frame_bufs(cm); |
188 | 131 |
189 #if CONFIG_VP9_POSTPROC | 132 #if CONFIG_VP9_POSTPROC |
190 if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y, | 133 if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y, |
191 #if CONFIG_VP9_HIGHBITDEPTH | 134 #if CONFIG_VP9_HIGHBITDEPTH |
192 cm->use_highbitdepth, | 135 cm->use_highbitdepth, |
193 #endif | 136 #endif |
194 VP9_ENC_BORDER_IN_PIXELS, | 137 VP9_ENC_BORDER_IN_PIXELS, |
195 cm->byte_alignment) < 0) | 138 cm->byte_alignment) < 0) |
196 goto fail; | 139 goto fail; |
197 #endif | 140 #endif |
198 | 141 |
199 return 0; | 142 return 0; |
200 | 143 |
201 fail: | 144 fail: |
202 vp9_free_ref_frame_buffers(cm); | 145 vp9_free_ref_frame_buffers(cm); |
203 return 1; | 146 return 1; |
204 } | 147 } |
205 | 148 |
206 void vp9_remove_common(VP9_COMMON *cm) { | 149 void vp9_remove_common(VP9_COMMON *cm) { |
207 vp9_free_ref_frame_buffers(cm); | 150 vp9_free_ref_frame_buffers(cm); |
208 vp9_free_context_buffers(cm); | 151 vp9_free_context_buffers(cm); |
| 152 vp9_free_internal_frame_buffers(&cm->int_frame_buffers); |
209 | 153 |
210 vpx_free(cm->fc); | 154 vpx_free(cm->fc); |
211 cm->fc = NULL; | 155 cm->fc = NULL; |
212 vpx_free(cm->frame_contexts); | 156 vpx_free(cm->frame_contexts); |
213 cm->frame_contexts = NULL; | 157 cm->frame_contexts = NULL; |
214 } | 158 } |
215 | 159 |
216 void vp9_init_context_buffers(VP9_COMMON *cm) { | 160 void vp9_init_context_buffers(VP9_COMMON *cm) { |
217 cm->setup_mi(cm); | 161 cm->setup_mi(cm); |
218 if (cm->last_frame_seg_map && !cm->frame_parallel_decode) | 162 if (cm->last_frame_seg_map) |
219 vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols); | 163 vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols); |
220 } | 164 } |
221 | |
222 void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) { | |
223 // Swap indices. | |
224 const int tmp = cm->seg_map_idx; | |
225 cm->seg_map_idx = cm->prev_seg_map_idx; | |
226 cm->prev_seg_map_idx = tmp; | |
227 | |
228 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx]; | |
229 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx]; | |
230 } | |
OLD | NEW |