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 | |
12 #include <stdlib.h> | 11 #include <stdlib.h> |
13 #include <string.h> | 12 #include <string.h> |
| 13 |
14 #include "vpx/vpx_image.h" | 14 #include "vpx/vpx_image.h" |
15 #include "vpx/vpx_integer.h" | 15 #include "vpx/vpx_integer.h" |
16 | 16 #include "vpx_mem/vpx_mem.h" |
17 #define ADDRESS_STORAGE_SIZE sizeof(size_t) | |
18 /*returns an addr aligned to the byte boundary specified by align*/ | |
19 #define align_addr(addr,align) (void*)(((size_t)(addr) + ((align) - 1)) & (size_
t)-(align)) | |
20 | |
21 /* Memalign code is copied from vpx_mem.c */ | |
22 static void *img_buf_memalign(size_t align, size_t size) { | |
23 void *addr, | |
24 * x = NULL; | |
25 | |
26 addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE); | |
27 | |
28 if (addr) { | |
29 x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align); | |
30 /* save the actual malloc address */ | |
31 ((size_t *)x)[-1] = (size_t)addr; | |
32 } | |
33 | |
34 return x; | |
35 } | |
36 | |
37 static void img_buf_free(void *memblk) { | |
38 if (memblk) { | |
39 void *addr = (void *)(((size_t *)memblk)[-1]); | |
40 free(addr); | |
41 } | |
42 } | |
43 | 17 |
44 static vpx_image_t *img_alloc_helper(vpx_image_t *img, | 18 static vpx_image_t *img_alloc_helper(vpx_image_t *img, |
45 vpx_img_fmt_t fmt, | 19 vpx_img_fmt_t fmt, |
46 unsigned int d_w, | 20 unsigned int d_w, |
47 unsigned int d_h, | 21 unsigned int d_h, |
48 unsigned int buf_align, | 22 unsigned int buf_align, |
49 unsigned int stride_align, | 23 unsigned int stride_align, |
50 unsigned char *img_data) { | 24 unsigned char *img_data) { |
51 | 25 |
52 unsigned int h, w, s, xcs, ycs, bps; | 26 unsigned int h, w, s, xcs, ycs, bps; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 139 |
166 img->img_data = img_data; | 140 img->img_data = img_data; |
167 | 141 |
168 if (!img_data) { | 142 if (!img_data) { |
169 const uint64_t alloc_size = (fmt & VPX_IMG_FMT_PLANAR) ? | 143 const uint64_t alloc_size = (fmt & VPX_IMG_FMT_PLANAR) ? |
170 (uint64_t)h * s * bps / 8 : (uint64_t)h * s; | 144 (uint64_t)h * s * bps / 8 : (uint64_t)h * s; |
171 | 145 |
172 if (alloc_size != (size_t)alloc_size) | 146 if (alloc_size != (size_t)alloc_size) |
173 goto fail; | 147 goto fail; |
174 | 148 |
175 img->img_data = img_buf_memalign(buf_align, (size_t)alloc_size); | 149 img->img_data = (uint8_t *)vpx_memalign(buf_align, (size_t)alloc_size); |
176 img->img_data_owner = 1; | 150 img->img_data_owner = 1; |
177 } | 151 } |
178 | 152 |
179 if (!img->img_data) | 153 if (!img->img_data) |
180 goto fail; | 154 goto fail; |
181 | 155 |
182 img->fmt = fmt; | 156 img->fmt = fmt; |
183 img->bit_depth = (fmt & VPX_IMG_FMT_HIGH) ? 16 : 8; | 157 img->bit_depth = (fmt & VPX_IMG_FMT_HIGH) ? 16 : 8; |
184 img->w = w; | 158 img->w = w; |
185 img->h = h; | 159 img->h = h; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 * img->stride[VPX_PLANE_V]; | 263 * img->stride[VPX_PLANE_V]; |
290 img->stride[VPX_PLANE_V] = -img->stride[VPX_PLANE_V]; | 264 img->stride[VPX_PLANE_V] = -img->stride[VPX_PLANE_V]; |
291 | 265 |
292 img->planes[VPX_PLANE_ALPHA] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE
_ALPHA]; | 266 img->planes[VPX_PLANE_ALPHA] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE
_ALPHA]; |
293 img->stride[VPX_PLANE_ALPHA] = -img->stride[VPX_PLANE_ALPHA]; | 267 img->stride[VPX_PLANE_ALPHA] = -img->stride[VPX_PLANE_ALPHA]; |
294 } | 268 } |
295 | 269 |
296 void vpx_img_free(vpx_image_t *img) { | 270 void vpx_img_free(vpx_image_t *img) { |
297 if (img) { | 271 if (img) { |
298 if (img->img_data && img->img_data_owner) | 272 if (img->img_data && img->img_data_owner) |
299 img_buf_free(img->img_data); | 273 vpx_free(img->img_data); |
300 | 274 |
301 if (img->self_allocd) | 275 if (img->self_allocd) |
302 free(img); | 276 free(img); |
303 } | 277 } |
304 } | 278 } |
OLD | NEW |