1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "vpx/vpx_image.h"
15 #include "vpx/vpx_integer.h"
16 #include "vpx_mem/vpx_mem.h"
17
img_alloc_helper(vpx_image_t * img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int buf_align,unsigned int stride_align,unsigned char * img_data)18 static vpx_image_t *img_alloc_helper(vpx_image_t *img,
19 vpx_img_fmt_t fmt,
20 unsigned int d_w,
21 unsigned int d_h,
22 unsigned int buf_align,
23 unsigned int stride_align,
24 unsigned char *img_data) {
25
26 unsigned int h, w, s, xcs, ycs, bps;
27 int align;
28
29 /* Treat align==0 like align==1 */
30 if (!buf_align)
31 buf_align = 1;
32
33 /* Validate alignment (must be power of 2) */
34 if (buf_align & (buf_align - 1))
35 goto fail;
36
37 /* Treat align==0 like align==1 */
38 if (!stride_align)
39 stride_align = 1;
40
41 /* Validate alignment (must be power of 2) */
42 if (stride_align & (stride_align - 1))
43 goto fail;
44
45 /* Get sample size for this format */
46 switch (fmt) {
47 case VPX_IMG_FMT_RGB32:
48 case VPX_IMG_FMT_RGB32_LE:
49 case VPX_IMG_FMT_ARGB:
50 case VPX_IMG_FMT_ARGB_LE:
51 bps = 32;
52 break;
53 case VPX_IMG_FMT_RGB24:
54 case VPX_IMG_FMT_BGR24:
55 bps = 24;
56 break;
57 case VPX_IMG_FMT_RGB565:
58 case VPX_IMG_FMT_RGB565_LE:
59 case VPX_IMG_FMT_RGB555:
60 case VPX_IMG_FMT_RGB555_LE:
61 case VPX_IMG_FMT_UYVY:
62 case VPX_IMG_FMT_YUY2:
63 case VPX_IMG_FMT_YVYU:
64 bps = 16;
65 break;
66 case VPX_IMG_FMT_I420:
67 case VPX_IMG_FMT_YV12:
68 case VPX_IMG_FMT_VPXI420:
69 case VPX_IMG_FMT_VPXYV12:
70 bps = 12;
71 break;
72 case VPX_IMG_FMT_I422:
73 bps = 16;
74 break;
75 case VPX_IMG_FMT_I444:
76 bps = 24;
77 break;
78 case VPX_IMG_FMT_I42016:
79 bps = 24;
80 break;
81 case VPX_IMG_FMT_I42216:
82 bps = 32;
83 break;
84 case VPX_IMG_FMT_I44416:
85 bps = 48;
86 break;
87 default:
88 bps = 16;
89 break;
90 }
91
92 /* Get chroma shift values for this format */
93 switch (fmt) {
94 case VPX_IMG_FMT_I420:
95 case VPX_IMG_FMT_YV12:
96 case VPX_IMG_FMT_VPXI420:
97 case VPX_IMG_FMT_VPXYV12:
98 case VPX_IMG_FMT_I422:
99 case VPX_IMG_FMT_I42016:
100 case VPX_IMG_FMT_I42216:
101 xcs = 1;
102 break;
103 default:
104 xcs = 0;
105 break;
106 }
107
108 switch (fmt) {
109 case VPX_IMG_FMT_I420:
110 case VPX_IMG_FMT_YV12:
111 case VPX_IMG_FMT_VPXI420:
112 case VPX_IMG_FMT_VPXYV12:
113 ycs = 1;
114 break;
115 default:
116 ycs = 0;
117 break;
118 }
119
120 /* Calculate storage sizes given the chroma subsampling */
121 align = (1 << xcs) - 1;
122 w = (d_w + align) & ~align;
123 align = (1 << ycs) - 1;
124 h = (d_h + align) & ~align;
125 s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8;
126 s = (s + stride_align - 1) & ~(stride_align - 1);
127
128 /* Allocate the new image */
129 if (!img) {
130 img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t));
131
132 if (!img)
133 goto fail;
134
135 img->self_allocd = 1;
136 } else {
137 memset(img, 0, sizeof(vpx_image_t));
138 }
139
140 img->img_data = img_data;
141
142 if (!img_data) {
143 const uint64_t alloc_size = (fmt & VPX_IMG_FMT_PLANAR) ?
144 (uint64_t)h * s * bps / 8 : (uint64_t)h * s;
145
146 if (alloc_size != (size_t)alloc_size)
147 goto fail;
148
149 img->img_data = (uint8_t *)vpx_memalign(buf_align, (size_t)alloc_size);
150 img->img_data_owner = 1;
151 }
152
153 if (!img->img_data)
154 goto fail;
155
156 img->fmt = fmt;
157 img->bit_depth = (fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
158 img->w = w;
159 img->h = h;
160 img->x_chroma_shift = xcs;
161 img->y_chroma_shift = ycs;
162 img->bps = bps;
163
164 /* Calculate strides */
165 img->stride[VPX_PLANE_Y] = img->stride[VPX_PLANE_ALPHA] = s;
166 img->stride[VPX_PLANE_U] = img->stride[VPX_PLANE_V] = s >> xcs;
167
168 /* Default viewport to entire image */
169 if (!vpx_img_set_rect(img, 0, 0, d_w, d_h))
170 return img;
171
172 fail:
173 vpx_img_free(img);
174 return NULL;
175 }
176
vpx_img_alloc(vpx_image_t * img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align)177 vpx_image_t *vpx_img_alloc(vpx_image_t *img,
178 vpx_img_fmt_t fmt,
179 unsigned int d_w,
180 unsigned int d_h,
181 unsigned int align) {
182 return img_alloc_helper(img, fmt, d_w, d_h, align, align, NULL);
183 }
184
vpx_img_wrap(vpx_image_t * img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int stride_align,unsigned char * img_data)185 vpx_image_t *vpx_img_wrap(vpx_image_t *img,
186 vpx_img_fmt_t fmt,
187 unsigned int d_w,
188 unsigned int d_h,
189 unsigned int stride_align,
190 unsigned char *img_data) {
191 /* By setting buf_align = 1, we don't change buffer alignment in this
192 * function. */
193 return img_alloc_helper(img, fmt, d_w, d_h, 1, stride_align, img_data);
194 }
195
vpx_img_set_rect(vpx_image_t * img,unsigned int x,unsigned int y,unsigned int w,unsigned int h)196 int vpx_img_set_rect(vpx_image_t *img,
197 unsigned int x,
198 unsigned int y,
199 unsigned int w,
200 unsigned int h) {
201 unsigned char *data;
202
203 if (x + w <= img->w && y + h <= img->h) {
204 img->d_w = w;
205 img->d_h = h;
206
207 /* Calculate plane pointers */
208 if (!(img->fmt & VPX_IMG_FMT_PLANAR)) {
209 img->planes[VPX_PLANE_PACKED] =
210 img->img_data + x * img->bps / 8 + y * img->stride[VPX_PLANE_PACKED];
211 } else {
212 data = img->img_data;
213
214 if (img->fmt & VPX_IMG_FMT_HAS_ALPHA) {
215 img->planes[VPX_PLANE_ALPHA] =
216 data + x + y * img->stride[VPX_PLANE_ALPHA];
217 data += img->h * img->stride[VPX_PLANE_ALPHA];
218 }
219
220 img->planes[VPX_PLANE_Y] = data + x + y * img->stride[VPX_PLANE_Y];
221 data += img->h * img->stride[VPX_PLANE_Y];
222
223 if (!(img->fmt & VPX_IMG_FMT_UV_FLIP)) {
224 img->planes[VPX_PLANE_U] = data
225 + (x >> img->x_chroma_shift)
226 + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U];
227 data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_U];
228 img->planes[VPX_PLANE_V] = data
229 + (x >> img->x_chroma_shift)
230 + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V];
231 } else {
232 img->planes[VPX_PLANE_V] = data
233 + (x >> img->x_chroma_shift)
234 + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V];
235 data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_V];
236 img->planes[VPX_PLANE_U] = data
237 + (x >> img->x_chroma_shift)
238 + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U];
239 }
240 }
241
242 return 0;
243 }
244
245 return -1;
246 }
247
vpx_img_flip(vpx_image_t * img)248 void vpx_img_flip(vpx_image_t *img) {
249 /* Note: In the calculation pointer adjustment calculation, we want the
250 * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99
251 * standard indicates that if the adjustment parameter is unsigned, the
252 * stride parameter will be promoted to unsigned, causing errors when
253 * the lhs is a larger type than the rhs.
254 */
255 img->planes[VPX_PLANE_Y] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE_Y];
256 img->stride[VPX_PLANE_Y] = -img->stride[VPX_PLANE_Y];
257
258 img->planes[VPX_PLANE_U] += (signed)((img->d_h >> img->y_chroma_shift) - 1)
259 * img->stride[VPX_PLANE_U];
260 img->stride[VPX_PLANE_U] = -img->stride[VPX_PLANE_U];
261
262 img->planes[VPX_PLANE_V] += (signed)((img->d_h >> img->y_chroma_shift) - 1)
263 * img->stride[VPX_PLANE_V];
264 img->stride[VPX_PLANE_V] = -img->stride[VPX_PLANE_V];
265
266 img->planes[VPX_PLANE_ALPHA] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE_ALPHA];
267 img->stride[VPX_PLANE_ALPHA] = -img->stride[VPX_PLANE_ALPHA];
268 }
269
vpx_img_free(vpx_image_t * img)270 void vpx_img_free(vpx_image_t *img) {
271 if (img) {
272 if (img->img_data && img->img_data_owner)
273 vpx_free(img->img_data);
274
275 if (img->self_allocd)
276 free(img);
277 }
278 }
279