| 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 <assert.h> |
| 12 |
| 11 #include "./vpx_config.h" | 13 #include "./vpx_config.h" |
| 12 #include "vpx_scale/yv12config.h" | 14 #include "vpx_scale/yv12config.h" |
| 13 #include "vpx_mem/vpx_mem.h" | 15 #include "vpx_mem/vpx_mem.h" |
| 14 | 16 |
| 15 /**************************************************************************** | 17 /**************************************************************************** |
| 16 * Exports | 18 * Exports |
| 17 ****************************************************************************/ | 19 ****************************************************************************/ |
| 18 | 20 |
| 19 /**************************************************************************** | 21 /**************************************************************************** |
| 20 * | 22 * |
| 21 ****************************************************************************/ | 23 ****************************************************************************/ |
| 24 #define yv12_align_addr(addr, align) \ |
| 25 (void*)(((size_t)(addr) + ((align) - 1)) & (size_t)-(align)) |
| 26 |
| 22 int | 27 int |
| 23 vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) { | 28 vp8_yv12_de_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf) { |
| 24 if (ybf) { | 29 if (ybf) { |
| 25 vpx_free(ybf->buffer_alloc); | 30 // If libvpx is using frame buffer callbacks then buffer_alloc_sz must |
| 31 // not be set. |
| 32 if (ybf->buffer_alloc_sz > 0) { |
| 33 vpx_free(ybf->buffer_alloc); |
| 34 } |
| 26 | 35 |
| 27 /* buffer_alloc isn't accessed by most functions. Rather y_buffer, | 36 /* buffer_alloc isn't accessed by most functions. Rather y_buffer, |
| 28 u_buffer and v_buffer point to buffer_alloc and are used. Clear out | 37 u_buffer and v_buffer point to buffer_alloc and are used. Clear out |
| 29 all of this so that a freed pointer isn't inadvertently used */ | 38 all of this so that a freed pointer isn't inadvertently used */ |
| 30 vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG)); | 39 vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG)); |
| 31 } else { | 40 } else { |
| 32 return -1; | 41 return -1; |
| 33 } | 42 } |
| 34 | 43 |
| 35 return 0; | 44 return 0; |
| 36 } | 45 } |
| 37 | 46 |
| 38 int vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, | 47 int vp8_yv12_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, |
| 39 int width, int height, int border) { | 48 int width, int height, int border) { |
| 40 if (ybf) { | 49 if (ybf) { |
| 41 int aligned_width = (width + 15) & ~15; | 50 int aligned_width = (width + 15) & ~15; |
| 42 int aligned_height = (height + 15) & ~15; | 51 int aligned_height = (height + 15) & ~15; |
| 43 int y_stride = ((aligned_width + 2 * border) + 31) & ~31; | 52 int y_stride = ((aligned_width + 2 * border) + 31) & ~31; |
| 44 int yplane_size = (aligned_height + 2 * border) * y_stride; | 53 int yplane_size = (aligned_height + 2 * border) * y_stride; |
| 45 int uv_width = aligned_width >> 1; | 54 int uv_width = aligned_width >> 1; |
| 46 int uv_height = aligned_height >> 1; | 55 int uv_height = aligned_height >> 1; |
| 47 /** There is currently a bunch of code which assumes | 56 /** There is currently a bunch of code which assumes |
| 48 * uv_stride == y_stride/2, so enforce this here. */ | 57 * uv_stride == y_stride/2, so enforce this here. */ |
| 49 int uv_stride = y_stride >> 1; | 58 int uv_stride = y_stride >> 1; |
| 50 int uvplane_size = (uv_height + border) * uv_stride; | 59 int uvplane_size = (uv_height + border) * uv_stride; |
| 51 const int frame_size = yplane_size + 2 * uvplane_size; | 60 const int frame_size = yplane_size + 2 * uvplane_size; |
| 52 | 61 |
| 53 if (!ybf->buffer_alloc) { | 62 if (!ybf->buffer_alloc) { |
| 54 ybf->buffer_alloc = vpx_memalign(32, frame_size); | 63 ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size); |
| 55 ybf->buffer_alloc_sz = frame_size; | 64 ybf->buffer_alloc_sz = frame_size; |
| 56 } | 65 } |
| 57 | 66 |
| 58 if (!ybf->buffer_alloc || ybf->buffer_alloc_sz < frame_size) | 67 if (!ybf->buffer_alloc || ybf->buffer_alloc_sz < frame_size) |
| 59 return -1; | 68 return -1; |
| 60 | 69 |
| 61 /* Only support allocating buffers that have a border that's a multiple | 70 /* Only support allocating buffers that have a border that's a multiple |
| 62 * of 32. The border restriction is required to get 16-byte alignment of | 71 * of 32. The border restriction is required to get 16-byte alignment of |
| 63 * the start of the chroma rows without introducing an arbitrary gap | 72 * the start of the chroma rows without introducing an arbitrary gap |
| 64 * between planes, which would break the semantics of things like | 73 * between planes, which would break the semantics of things like |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 return vp8_yv12_realloc_frame_buffer(ybf, width, height, border); | 110 return vp8_yv12_realloc_frame_buffer(ybf, width, height, border); |
| 102 } | 111 } |
| 103 return -2; | 112 return -2; |
| 104 } | 113 } |
| 105 | 114 |
| 106 #if CONFIG_VP9 | 115 #if CONFIG_VP9 |
| 107 // TODO(jkoleszar): Maybe replace this with struct vpx_image | 116 // TODO(jkoleszar): Maybe replace this with struct vpx_image |
| 108 | 117 |
| 109 int vp9_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) { | 118 int vp9_free_frame_buffer(YV12_BUFFER_CONFIG *ybf) { |
| 110 if (ybf) { | 119 if (ybf) { |
| 111 vpx_free(ybf->buffer_alloc); | 120 if (ybf->buffer_alloc_sz > 0) { |
| 121 vpx_free(ybf->buffer_alloc); |
| 122 } |
| 112 | 123 |
| 113 /* buffer_alloc isn't accessed by most functions. Rather y_buffer, | 124 /* buffer_alloc isn't accessed by most functions. Rather y_buffer, |
| 114 u_buffer and v_buffer point to buffer_alloc and are used. Clear out | 125 u_buffer and v_buffer point to buffer_alloc and are used. Clear out |
| 115 all of this so that a freed pointer isn't inadvertently used */ | 126 all of this so that a freed pointer isn't inadvertently used */ |
| 116 vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG)); | 127 vpx_memset(ybf, 0, sizeof(YV12_BUFFER_CONFIG)); |
| 117 } else { | 128 } else { |
| 118 return -1; | 129 return -1; |
| 119 } | 130 } |
| 120 | 131 |
| 121 return 0; | 132 return 0; |
| 122 } | 133 } |
| 123 | 134 |
| 124 int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, | 135 int vp9_realloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, |
| 125 int width, int height, | 136 int width, int height, |
| 126 int ss_x, int ss_y, int border) { | 137 int ss_x, int ss_y, int border, |
| 138 vpx_codec_frame_buffer_t *fb, |
| 139 vpx_get_frame_buffer_cb_fn_t cb, |
| 140 void *cb_priv) { |
| 127 if (ybf) { | 141 if (ybf) { |
| 128 const int aligned_width = (width + 7) & ~7; | 142 const int aligned_width = (width + 7) & ~7; |
| 129 const int aligned_height = (height + 7) & ~7; | 143 const int aligned_height = (height + 7) & ~7; |
| 130 const int y_stride = ((aligned_width + 2 * border) + 31) & ~31; | 144 const int y_stride = ((aligned_width + 2 * border) + 31) & ~31; |
| 131 const int yplane_size = (aligned_height + 2 * border) * y_stride; | 145 const int yplane_size = (aligned_height + 2 * border) * y_stride; |
| 132 const int uv_width = aligned_width >> ss_x; | 146 const int uv_width = aligned_width >> ss_x; |
| 133 const int uv_height = aligned_height >> ss_y; | 147 const int uv_height = aligned_height >> ss_y; |
| 134 const int uv_stride = y_stride >> ss_x; | 148 const int uv_stride = y_stride >> ss_x; |
| 135 const int uv_border_w = border >> ss_x; | 149 const int uv_border_w = border >> ss_x; |
| 136 const int uv_border_h = border >> ss_y; | 150 const int uv_border_h = border >> ss_y; |
| 137 const int uvplane_size = (uv_height + 2 * uv_border_h) * uv_stride; | 151 const int uvplane_size = (uv_height + 2 * uv_border_h) * uv_stride; |
| 138 #if CONFIG_ALPHA | 152 #if CONFIG_ALPHA |
| 139 const int alpha_width = aligned_width; | 153 const int alpha_width = aligned_width; |
| 140 const int alpha_height = aligned_height; | 154 const int alpha_height = aligned_height; |
| 141 const int alpha_stride = y_stride; | 155 const int alpha_stride = y_stride; |
| 142 const int alpha_border_w = border; | 156 const int alpha_border_w = border; |
| 143 const int alpha_border_h = border; | 157 const int alpha_border_h = border; |
| 144 const int alpha_plane_size = (alpha_height + 2 * alpha_border_h) * | 158 const int alpha_plane_size = (alpha_height + 2 * alpha_border_h) * |
| 145 alpha_stride; | 159 alpha_stride; |
| 146 const int frame_size = yplane_size + 2 * uvplane_size + | 160 const int frame_size = yplane_size + 2 * uvplane_size + |
| 147 alpha_plane_size; | 161 alpha_plane_size; |
| 148 #else | 162 #else |
| 149 const int frame_size = yplane_size + 2 * uvplane_size; | 163 const int frame_size = yplane_size + 2 * uvplane_size; |
| 150 #endif | 164 #endif |
| 151 if (frame_size > ybf->buffer_alloc_sz) { | 165 if (cb != NULL) { |
| 166 const int align_addr_extra_size = 31; |
| 167 const size_t external_frame_size = frame_size + align_addr_extra_size; |
| 168 |
| 169 assert(fb != NULL); |
| 170 |
| 171 // Allocation to hold larger frame, or first allocation. |
| 172 if (cb(cb_priv, external_frame_size, fb) < 0) |
| 173 return -1; |
| 174 |
| 175 if (fb->data == NULL || fb->size < external_frame_size) |
| 176 return -1; |
| 177 |
| 178 // This memset is needed for fixing valgrind error from C loop filter |
| 179 // due to access uninitialized memory in frame border. It could be |
| 180 // removed if border is totally removed. |
| 181 vpx_memset(fb->data, 0, fb->size); |
| 182 |
| 183 ybf->buffer_alloc = (uint8_t *)yv12_align_addr(fb->data, 32); |
| 184 } else if (frame_size > ybf->buffer_alloc_sz) { |
| 152 // Allocation to hold larger frame, or first allocation. | 185 // Allocation to hold larger frame, or first allocation. |
| 153 if (ybf->buffer_alloc) | 186 if (ybf->buffer_alloc) |
| 154 vpx_free(ybf->buffer_alloc); | 187 vpx_free(ybf->buffer_alloc); |
| 155 ybf->buffer_alloc = vpx_memalign(32, frame_size); | 188 ybf->buffer_alloc = (uint8_t *)vpx_memalign(32, frame_size); |
| 156 if (!ybf->buffer_alloc) | 189 if (!ybf->buffer_alloc) |
| 157 return -1; | 190 return -1; |
| 158 | 191 |
| 159 ybf->buffer_alloc_sz = frame_size; | 192 ybf->buffer_alloc_sz = frame_size; |
| 160 | 193 |
| 161 // This memset is needed for fixing valgrind error from C loop filter | 194 // This memset is needed for fixing valgrind error from C loop filter |
| 162 // due to access uninitialized memory in frame boarder. It could be | 195 // due to access uninitialized memory in frame border. It could be |
| 163 // removed if border is totally removed. | 196 // removed if border is totally removed. |
| 164 vpx_memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz); | 197 vpx_memset(ybf->buffer_alloc, 0, ybf->buffer_alloc_sz); |
| 165 } | 198 } |
| 166 | 199 |
| 167 if (ybf->buffer_alloc_sz < frame_size) | |
| 168 return -1; | |
| 169 | |
| 170 /* Only support allocating buffers that have a border that's a multiple | 200 /* Only support allocating buffers that have a border that's a multiple |
| 171 * of 32. The border restriction is required to get 16-byte alignment of | 201 * of 32. The border restriction is required to get 16-byte alignment of |
| 172 * the start of the chroma rows without introducing an arbitrary gap | 202 * the start of the chroma rows without introducing an arbitrary gap |
| 173 * between planes, which would break the semantics of things like | 203 * between planes, which would break the semantics of things like |
| 174 * vpx_img_set_rect(). */ | 204 * vpx_img_set_rect(). */ |
| 175 if (border & 0x1f) | 205 if (border & 0x1f) |
| 176 return -3; | 206 return -3; |
| 177 | 207 |
| 178 ybf->y_crop_width = width; | 208 ybf->y_crop_width = width; |
| 179 ybf->y_crop_height = height; | 209 ybf->y_crop_height = height; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 207 return 0; | 237 return 0; |
| 208 } | 238 } |
| 209 return -2; | 239 return -2; |
| 210 } | 240 } |
| 211 | 241 |
| 212 int vp9_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, | 242 int vp9_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, |
| 213 int width, int height, | 243 int width, int height, |
| 214 int ss_x, int ss_y, int border) { | 244 int ss_x, int ss_y, int border) { |
| 215 if (ybf) { | 245 if (ybf) { |
| 216 vp9_free_frame_buffer(ybf); | 246 vp9_free_frame_buffer(ybf); |
| 217 return vp9_realloc_frame_buffer(ybf, width, height, ss_x, ss_y, border); | 247 return vp9_realloc_frame_buffer(ybf, width, height, ss_x, ss_y, border, |
| 248 NULL, NULL, NULL); |
| 218 } | 249 } |
| 219 return -2; | 250 return -2; |
| 220 } | 251 } |
| 221 #endif | 252 #endif |
| OLD | NEW |