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 |
20 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) { | 38 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) { |
21 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2); | 39 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2); |
22 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2); | 40 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2); |
23 | 41 |
24 cm->mi_cols = aligned_width >> MI_SIZE_LOG2; | 42 cm->mi_cols = aligned_width >> MI_SIZE_LOG2; |
25 cm->mi_rows = aligned_height >> MI_SIZE_LOG2; | 43 cm->mi_rows = aligned_height >> MI_SIZE_LOG2; |
26 cm->mi_stride = calc_mi_size(cm->mi_cols); | 44 cm->mi_stride = calc_mi_size(cm->mi_cols); |
27 | 45 |
28 cm->mb_cols = (cm->mi_cols + 1) >> 1; | 46 cm->mb_cols = (cm->mi_cols + 1) >> 1; |
29 cm->mb_rows = (cm->mi_rows + 1) >> 1; | 47 cm->mb_rows = (cm->mi_rows + 1) >> 1; |
30 cm->MBs = cm->mb_rows * cm->mb_cols; | 48 cm->MBs = cm->mb_rows * cm->mb_cols; |
31 } | 49 } |
32 | 50 |
| 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 |
33 void vp9_free_ref_frame_buffers(VP9_COMMON *cm) { | 86 void vp9_free_ref_frame_buffers(VP9_COMMON *cm) { |
| 87 BufferPool *const pool = cm->buffer_pool; |
34 int i; | 88 int i; |
35 | 89 |
36 for (i = 0; i < FRAME_BUFFERS; ++i) { | 90 for (i = 0; i < FRAME_BUFFERS; ++i) { |
37 if (cm->frame_bufs[i].ref_count > 0 && | 91 if (pool->frame_bufs[i].ref_count > 0 && |
38 cm->frame_bufs[i].raw_frame_buffer.data != NULL) { | 92 pool->frame_bufs[i].raw_frame_buffer.data != NULL) { |
39 cm->release_fb_cb(cm->cb_priv, &cm->frame_bufs[i].raw_frame_buffer); | 93 pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer); |
40 cm->frame_bufs[i].ref_count = 0; | 94 pool->frame_bufs[i].ref_count = 0; |
41 } | 95 } |
42 vpx_free(cm->frame_bufs[i].mvs); | 96 vpx_free(pool->frame_bufs[i].mvs); |
43 cm->frame_bufs[i].mvs = NULL; | 97 pool->frame_bufs[i].mvs = NULL; |
44 vp9_free_frame_buffer(&cm->frame_bufs[i].buf); | 98 vp9_free_frame_buffer(&pool->frame_bufs[i].buf); |
45 } | 99 } |
46 | 100 |
47 #if CONFIG_VP9_POSTPROC | 101 #if CONFIG_VP9_POSTPROC |
48 vp9_free_frame_buffer(&cm->post_proc_buffer); | 102 vp9_free_frame_buffer(&cm->post_proc_buffer); |
49 vp9_free_frame_buffer(&cm->post_proc_buffer_int); | 103 vp9_free_frame_buffer(&cm->post_proc_buffer_int); |
50 #endif | 104 #endif |
51 } | 105 } |
52 | 106 |
53 void vp9_free_context_buffers(VP9_COMMON *cm) { | 107 void vp9_free_context_buffers(VP9_COMMON *cm) { |
54 cm->free_mi(cm); | 108 cm->free_mi(cm); |
55 vpx_free(cm->last_frame_seg_map); | 109 free_seg_map(cm); |
56 cm->last_frame_seg_map = NULL; | |
57 vpx_free(cm->above_context); | 110 vpx_free(cm->above_context); |
58 cm->above_context = NULL; | 111 cm->above_context = NULL; |
59 vpx_free(cm->above_seg_context); | 112 vpx_free(cm->above_seg_context); |
60 cm->above_seg_context = NULL; | 113 cm->above_seg_context = NULL; |
61 } | 114 } |
62 | 115 |
63 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) { | 116 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) { |
64 vp9_free_context_buffers(cm); | 117 vp9_free_context_buffers(cm); |
65 | 118 |
66 vp9_set_mb_mi(cm, width, height); | 119 vp9_set_mb_mi(cm, width, height); |
67 if (cm->alloc_mi(cm, cm->mi_stride * calc_mi_size(cm->mi_rows))) | 120 if (cm->alloc_mi(cm, cm->mi_stride * calc_mi_size(cm->mi_rows))) |
68 goto fail; | 121 goto fail; |
69 | 122 |
70 cm->last_frame_seg_map = (uint8_t *)vpx_calloc(cm->mi_rows * cm->mi_cols, 1); | 123 // Create the segmentation map structure and set to 0. |
71 if (!cm->last_frame_seg_map) goto fail; | 124 free_seg_map(cm); |
| 125 if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) |
| 126 goto fail; |
72 | 127 |
73 cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc( | 128 cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc( |
74 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE, | 129 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE, |
75 sizeof(*cm->above_context)); | 130 sizeof(*cm->above_context)); |
76 if (!cm->above_context) goto fail; | 131 if (!cm->above_context) goto fail; |
77 | 132 |
78 cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc( | 133 cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc( |
79 mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context)); | 134 mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context)); |
80 if (!cm->above_seg_context) goto fail; | 135 if (!cm->above_seg_context) goto fail; |
81 | 136 |
82 return 0; | 137 return 0; |
83 | 138 |
84 fail: | 139 fail: |
85 vp9_free_context_buffers(cm); | 140 vp9_free_context_buffers(cm); |
86 return 1; | 141 return 1; |
87 } | 142 } |
88 | 143 |
89 static void init_frame_bufs(VP9_COMMON *cm) { | 144 static void init_frame_bufs(VP9_COMMON *cm) { |
| 145 BufferPool *const pool = cm->buffer_pool; |
90 int i; | 146 int i; |
91 | 147 |
92 cm->new_fb_idx = FRAME_BUFFERS - 1; | 148 cm->new_fb_idx = FRAME_BUFFERS - 1; |
93 cm->frame_bufs[cm->new_fb_idx].ref_count = 1; | 149 pool->frame_bufs[cm->new_fb_idx].ref_count = 1; |
94 | 150 |
95 for (i = 0; i < REF_FRAMES; ++i) { | 151 for (i = 0; i < REF_FRAMES; ++i) { |
96 cm->ref_frame_map[i] = i; | 152 cm->ref_frame_map[i] = i; |
97 cm->frame_bufs[i].ref_count = 1; | 153 pool->frame_bufs[i].ref_count = 1; |
98 } | 154 } |
99 } | 155 } |
100 | 156 |
101 int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) { | 157 int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) { |
102 int i; | 158 int i; |
103 const int ss_x = cm->subsampling_x; | 159 const int ss_x = cm->subsampling_x; |
104 const int ss_y = cm->subsampling_y; | 160 const int ss_y = cm->subsampling_y; |
105 | 161 |
106 vp9_free_ref_frame_buffers(cm); | 162 vp9_free_ref_frame_buffers(cm); |
107 | 163 |
108 for (i = 0; i < FRAME_BUFFERS; ++i) { | 164 for (i = 0; i < FRAME_BUFFERS; ++i) { |
109 cm->frame_bufs[i].ref_count = 0; | 165 BufferPool *const pool = cm->buffer_pool; |
110 if (vp9_alloc_frame_buffer(&cm->frame_bufs[i].buf, width, height, | 166 pool->frame_bufs[i].ref_count = 0; |
| 167 if (vp9_alloc_frame_buffer(&pool->frame_bufs[i].buf, width, height, |
111 ss_x, ss_y, | 168 ss_x, ss_y, |
112 #if CONFIG_VP9_HIGHBITDEPTH | 169 #if CONFIG_VP9_HIGHBITDEPTH |
113 cm->use_highbitdepth, | 170 cm->use_highbitdepth, |
114 #endif | 171 #endif |
115 VP9_ENC_BORDER_IN_PIXELS, | 172 VP9_ENC_BORDER_IN_PIXELS, |
116 cm->byte_alignment) < 0) | 173 cm->byte_alignment) < 0) |
117 goto fail; | 174 goto fail; |
118 if (cm->frame_bufs[i].mvs == NULL) { | 175 if (pool->frame_bufs[i].mvs == NULL) { |
119 cm->frame_bufs[i].mvs = | 176 pool->frame_bufs[i].mvs = |
120 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols, | 177 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols, |
121 sizeof(*cm->frame_bufs[i].mvs)); | 178 sizeof(*pool->frame_bufs[i].mvs)); |
122 if (cm->frame_bufs[i].mvs == NULL) | 179 if (pool->frame_bufs[i].mvs == NULL) |
123 goto fail; | 180 goto fail; |
124 | 181 |
125 cm->frame_bufs[i].mi_rows = cm->mi_rows; | 182 pool->frame_bufs[i].mi_rows = cm->mi_rows; |
126 cm->frame_bufs[i].mi_cols = cm->mi_cols; | 183 pool->frame_bufs[i].mi_cols = cm->mi_cols; |
127 } | 184 } |
128 } | 185 } |
129 | 186 |
130 init_frame_bufs(cm); | 187 init_frame_bufs(cm); |
131 | 188 |
132 #if CONFIG_VP9_POSTPROC | 189 #if CONFIG_VP9_POSTPROC |
133 if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y, | 190 if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y, |
134 #if CONFIG_VP9_HIGHBITDEPTH | 191 #if CONFIG_VP9_HIGHBITDEPTH |
135 cm->use_highbitdepth, | 192 cm->use_highbitdepth, |
136 #endif | 193 #endif |
137 VP9_ENC_BORDER_IN_PIXELS, | 194 VP9_ENC_BORDER_IN_PIXELS, |
138 cm->byte_alignment) < 0) | 195 cm->byte_alignment) < 0) |
139 goto fail; | 196 goto fail; |
140 #endif | 197 #endif |
141 | 198 |
142 return 0; | 199 return 0; |
143 | 200 |
144 fail: | 201 fail: |
145 vp9_free_ref_frame_buffers(cm); | 202 vp9_free_ref_frame_buffers(cm); |
146 return 1; | 203 return 1; |
147 } | 204 } |
148 | 205 |
149 void vp9_remove_common(VP9_COMMON *cm) { | 206 void vp9_remove_common(VP9_COMMON *cm) { |
150 vp9_free_ref_frame_buffers(cm); | 207 vp9_free_ref_frame_buffers(cm); |
151 vp9_free_context_buffers(cm); | 208 vp9_free_context_buffers(cm); |
152 vp9_free_internal_frame_buffers(&cm->int_frame_buffers); | |
153 | 209 |
154 vpx_free(cm->fc); | 210 vpx_free(cm->fc); |
155 cm->fc = NULL; | 211 cm->fc = NULL; |
156 vpx_free(cm->frame_contexts); | 212 vpx_free(cm->frame_contexts); |
157 cm->frame_contexts = NULL; | 213 cm->frame_contexts = NULL; |
158 } | 214 } |
159 | 215 |
160 void vp9_init_context_buffers(VP9_COMMON *cm) { | 216 void vp9_init_context_buffers(VP9_COMMON *cm) { |
161 cm->setup_mi(cm); | 217 cm->setup_mi(cm); |
162 if (cm->last_frame_seg_map) | 218 if (cm->last_frame_seg_map && !cm->frame_parallel_decode) |
163 vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols); | 219 vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols); |
164 } | 220 } |
| 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 |