2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
13 #include "vpx_codec/vpx_image.h"
15 static vpx_image_t *img_alloc_helper(vpx_image_t *img,
19 unsigned int stride_align,
20 unsigned char *img_data)
23 unsigned int h, w, s, xcs, ycs, bps;
26 /* Treat align==0 like align==1 */
30 /* Validate alignment (must be power of 2) */
31 if (stride_align & (stride_align - 1))
34 /* Get sample size for this format */
37 case VPX_IMG_FMT_RGB32:
38 case VPX_IMG_FMT_RGB32_LE:
39 case VPX_IMG_FMT_ARGB:
40 case VPX_IMG_FMT_ARGB_LE:
43 case VPX_IMG_FMT_RGB24:
44 case VPX_IMG_FMT_BGR24:
47 case VPX_IMG_FMT_RGB565:
48 case VPX_IMG_FMT_RGB565_LE:
49 case VPX_IMG_FMT_RGB555:
50 case VPX_IMG_FMT_RGB555_LE:
51 case VPX_IMG_FMT_UYVY:
52 case VPX_IMG_FMT_YUY2:
53 case VPX_IMG_FMT_YVYU:
56 case VPX_IMG_FMT_I420:
57 case VPX_IMG_FMT_YV12:
58 case VPX_IMG_FMT_VPXI420:
59 case VPX_IMG_FMT_VPXYV12:
67 /* Get chroma shift values for this format */
70 case VPX_IMG_FMT_I420:
71 case VPX_IMG_FMT_YV12:
72 case VPX_IMG_FMT_VPXI420:
73 case VPX_IMG_FMT_VPXYV12:
83 case VPX_IMG_FMT_I420:
84 case VPX_IMG_FMT_YV12:
85 case VPX_IMG_FMT_VPXI420:
86 case VPX_IMG_FMT_VPXYV12:
94 /* Calculate storage sizes given the chroma subsampling */
95 align = (1 << xcs) - 1;
96 w = (d_w + align) & ~align;
97 align = (1 << ycs) - 1;
98 h = (d_h + align) & ~align;
99 s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8;
100 s = (s + stride_align - 1) & ~(stride_align - 1);
102 /* Allocate the new image */
105 img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t));
110 img->self_allocd = 1;
114 memset(img, 0, sizeof(vpx_image_t));
117 img->img_data = img_data;
121 img->img_data = malloc((fmt & VPX_IMG_FMT_PLANAR) ? h * w * bps / 8 : h * s);
122 img->img_data_owner = 1;
131 img->x_chroma_shift = xcs;
132 img->y_chroma_shift = ycs;
135 /* Calculate strides */
136 img->stride[PLANE_Y] = img->stride[PLANE_ALPHA] = s;
137 img->stride[PLANE_U] = img->stride[PLANE_V] = s >> xcs;
139 /* Default viewport to entire image */
140 if (!vpx_img_set_rect(img, 0, 0, d_w, d_h))
148 vpx_image_t *vpx_img_alloc(vpx_image_t *img,
152 unsigned int stride_align)
154 return img_alloc_helper(img, fmt, d_w, d_h, stride_align, NULL);
157 vpx_image_t *vpx_img_wrap(vpx_image_t *img,
161 unsigned int stride_align,
162 unsigned char *img_data)
164 return img_alloc_helper(img, fmt, d_w, d_h, stride_align, img_data);
167 int vpx_img_set_rect(vpx_image_t *img,
175 if (x + w <= img->w && y + h <= img->h)
180 /* Calculate plane pointers */
181 if (!(img->fmt & VPX_IMG_FMT_PLANAR))
183 img->planes[PLANE_PACKED] =
184 img->img_data + x * img->bps / 8 + y * img->stride[PLANE_PACKED];
188 data = img->img_data;
190 if (img->fmt & VPX_IMG_FMT_HAS_ALPHA)
192 img->planes[PLANE_ALPHA] =
193 data + x + y * img->stride[PLANE_ALPHA];
194 data += img->h * img->stride[PLANE_ALPHA];
197 img->planes[PLANE_Y] = data + x + y * img->stride[PLANE_Y];
198 data += img->h * img->stride[PLANE_Y];
200 if (!(img->fmt & VPX_IMG_FMT_UV_FLIP))
202 img->planes[PLANE_U] = data
203 + (x >> img->x_chroma_shift)
204 + (y >> img->y_chroma_shift) * img->stride[PLANE_U];
205 data += (img->h >> img->y_chroma_shift) * img->stride[PLANE_U];
206 img->planes[PLANE_V] = data
207 + (x >> img->x_chroma_shift)
208 + (y >> img->y_chroma_shift) * img->stride[PLANE_V];
212 img->planes[PLANE_V] = data
213 + (x >> img->x_chroma_shift)
214 + (y >> img->y_chroma_shift) * img->stride[PLANE_V];
215 data += (img->h >> img->y_chroma_shift) * img->stride[PLANE_V];
216 img->planes[PLANE_U] = data
217 + (x >> img->x_chroma_shift)
218 + (y >> img->y_chroma_shift) * img->stride[PLANE_U];
228 void vpx_img_flip(vpx_image_t *img)
230 /* Note: In the calculation pointer adjustment calculation, we want the
231 * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99
232 * standard indicates that if the adjustment parameter is unsigned, the
233 * stride parameter will be promoted to unsigned, causing errors when
234 * the lhs is a larger type than the rhs.
236 img->planes[PLANE_Y] += (signed)(img->d_h - 1) * img->stride[PLANE_Y];
237 img->stride[PLANE_Y] = -img->stride[PLANE_Y];
239 img->planes[PLANE_U] += (signed)((img->d_h >> img->y_chroma_shift) - 1)
240 * img->stride[PLANE_U];
241 img->stride[PLANE_U] = -img->stride[PLANE_U];
243 img->planes[PLANE_V] += (signed)((img->d_h >> img->y_chroma_shift) - 1)
244 * img->stride[PLANE_V];
245 img->stride[PLANE_V] = -img->stride[PLANE_V];
247 img->planes[PLANE_ALPHA] += (signed)(img->d_h - 1) * img->stride[PLANE_ALPHA];
248 img->stride[PLANE_ALPHA] = -img->stride[PLANE_ALPHA];
251 void vpx_img_free(vpx_image_t *img)
255 if (img->img_data && img->img_data_owner)
258 if (img->self_allocd)