1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <assert.h>
13 #include <limits.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "aom/aom_image.h"
18 #include "aom/aom_integer.h"
19 #include "aom/internal/aom_image_internal.h"
20 #include "aom_mem/aom_mem.h"
21
align_image_dimension(unsigned int d,unsigned int subsampling,unsigned int size_align)22 static inline unsigned int align_image_dimension(unsigned int d,
23 unsigned int subsampling,
24 unsigned int size_align) {
25 unsigned int align;
26
27 align = (1 << subsampling) - 1;
28 align = (size_align - 1 > align) ? (size_align - 1) : align;
29 return ((d + align) & ~align);
30 }
31
img_alloc_helper(aom_image_t * img,aom_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int buf_align,unsigned int stride_align,unsigned int size_align,unsigned int border,unsigned char * img_data,aom_alloc_img_data_cb_fn_t alloc_cb,void * cb_priv)32 static aom_image_t *img_alloc_helper(
33 aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h,
34 unsigned int buf_align, unsigned int stride_align, unsigned int size_align,
35 unsigned int border, unsigned char *img_data,
36 aom_alloc_img_data_cb_fn_t alloc_cb, void *cb_priv) {
37 /* NOTE: In this function, bit_depth is either 8 or 16 (if
38 * AOM_IMG_FMT_HIGHBITDEPTH is set), never 10 or 12.
39 */
40 unsigned int xcs, ycs, bps, bit_depth;
41
42 if (img != NULL) memset(img, 0, sizeof(aom_image_t));
43
44 if (fmt == AOM_IMG_FMT_NONE) goto fail;
45
46 /* Impose maximum values on input parameters so that this function can
47 * perform arithmetic operations without worrying about overflows.
48 */
49 if (d_w > 0x08000000 || d_h > 0x08000000 || buf_align > 65536 ||
50 stride_align > 65536 || size_align > 65536 || border > 65536) {
51 goto fail;
52 }
53
54 /* Treat align==0 like align==1 */
55 if (!buf_align) buf_align = 1;
56
57 /* Validate alignment (must be power of 2) */
58 if (buf_align & (buf_align - 1)) goto fail;
59
60 /* Treat align==0 like align==1 */
61 if (!stride_align) stride_align = 1;
62
63 /* Validate alignment (must be power of 2) */
64 if (stride_align & (stride_align - 1)) goto fail;
65
66 /* Treat align==0 like align==1 */
67 if (!size_align) size_align = 1;
68
69 /* Validate alignment (must be power of 2) */
70 if (size_align & (size_align - 1)) goto fail;
71
72 /* Get sample size for this format */
73 switch (fmt) {
74 case AOM_IMG_FMT_I420:
75 case AOM_IMG_FMT_YV12:
76 case AOM_IMG_FMT_NV12:
77 case AOM_IMG_FMT_AOMI420:
78 case AOM_IMG_FMT_AOMYV12: bps = 12; break;
79 case AOM_IMG_FMT_I422: bps = 16; break;
80 case AOM_IMG_FMT_I444: bps = 24; break;
81 case AOM_IMG_FMT_YV1216:
82 case AOM_IMG_FMT_I42016: bps = 24; break;
83 case AOM_IMG_FMT_I42216: bps = 32; break;
84 case AOM_IMG_FMT_I44416: bps = 48; break;
85 default: bps = 16; break;
86 }
87
88 bit_depth = (fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
89
90 /* Get chroma shift values for this format */
91 switch (fmt) {
92 case AOM_IMG_FMT_I420:
93 case AOM_IMG_FMT_YV12:
94 case AOM_IMG_FMT_NV12:
95 case AOM_IMG_FMT_AOMI420:
96 case AOM_IMG_FMT_AOMYV12:
97 case AOM_IMG_FMT_I422:
98 case AOM_IMG_FMT_I42016:
99 case AOM_IMG_FMT_YV1216:
100 case AOM_IMG_FMT_I42216: xcs = 1; break;
101 default: xcs = 0; break;
102 }
103
104 switch (fmt) {
105 case AOM_IMG_FMT_I420:
106 case AOM_IMG_FMT_YV12:
107 case AOM_IMG_FMT_NV12:
108 case AOM_IMG_FMT_AOMI420:
109 case AOM_IMG_FMT_AOMYV12:
110 case AOM_IMG_FMT_YV1216:
111 case AOM_IMG_FMT_I42016: ycs = 1; break;
112 default: ycs = 0; break;
113 }
114
115 /* Calculate storage sizes given the chroma subsampling */
116 const unsigned int w = align_image_dimension(d_w, xcs, size_align);
117 assert(d_w <= w);
118 const unsigned int h = align_image_dimension(d_h, ycs, size_align);
119 assert(d_h <= h);
120
121 uint64_t s = (uint64_t)w + 2 * border;
122 s = (fmt & AOM_IMG_FMT_PLANAR) ? s : s * bps / bit_depth;
123 s = s * bit_depth / 8;
124 s = (s + stride_align - 1) & ~((uint64_t)stride_align - 1);
125 if (s > INT_MAX) goto fail;
126 const int stride_in_bytes = (int)s;
127
128 /* Allocate the new image */
129 if (!img) {
130 img = (aom_image_t *)calloc(1, sizeof(aom_image_t));
131
132 if (!img) goto fail;
133
134 img->self_allocd = 1;
135 }
136
137 img->img_data = img_data;
138
139 if (!img_data) {
140 const uint64_t alloc_size =
141 (fmt & AOM_IMG_FMT_PLANAR)
142 ? (uint64_t)(h + 2 * border) * stride_in_bytes * bps / bit_depth
143 : (uint64_t)(h + 2 * border) * stride_in_bytes;
144
145 if (alloc_size != (size_t)alloc_size) goto fail;
146
147 if (alloc_cb) {
148 const size_t padded_alloc_size = (size_t)alloc_size + buf_align - 1;
149 img->img_data = (uint8_t *)alloc_cb(cb_priv, padded_alloc_size);
150 if (img->img_data) {
151 img->img_data = (uint8_t *)aom_align_addr(img->img_data, buf_align);
152 }
153 img->img_data_owner = 0;
154 } else {
155 img->img_data = (uint8_t *)aom_memalign(buf_align, (size_t)alloc_size);
156 img->img_data_owner = 1;
157 }
158 img->sz = (size_t)alloc_size;
159 }
160
161 if (!img->img_data) goto fail;
162
163 img->fmt = fmt;
164 img->bit_depth = bit_depth;
165 // aligned width and aligned height
166 img->w = w;
167 img->h = h;
168 img->x_chroma_shift = xcs;
169 img->y_chroma_shift = ycs;
170 img->bps = bps;
171
172 /* Calculate strides */
173 img->stride[AOM_PLANE_Y] = stride_in_bytes;
174 img->stride[AOM_PLANE_U] = img->stride[AOM_PLANE_V] = stride_in_bytes >> xcs;
175
176 if (fmt == AOM_IMG_FMT_NV12) {
177 // Each row is a row of U and a row of V interleaved, so the stride is twice
178 // as long.
179 img->stride[AOM_PLANE_U] *= 2;
180 img->stride[AOM_PLANE_V] = 0;
181 }
182
183 /* Default viewport to entire image. (This aom_img_set_rect call always
184 * succeeds.) */
185 int ret = aom_img_set_rect(img, 0, 0, d_w, d_h, border);
186 assert(ret == 0);
187 (void)ret;
188 return img;
189
190 fail:
191 aom_img_free(img);
192 return NULL;
193 }
194
aom_img_alloc(aom_image_t * img,aom_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align)195 aom_image_t *aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt,
196 unsigned int d_w, unsigned int d_h,
197 unsigned int align) {
198 return img_alloc_helper(img, fmt, d_w, d_h, align, align, 1, 0, NULL, NULL,
199 NULL);
200 }
201
aom_img_alloc_with_cb(aom_image_t * img,aom_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align,aom_alloc_img_data_cb_fn_t alloc_cb,void * cb_priv)202 aom_image_t *aom_img_alloc_with_cb(aom_image_t *img, aom_img_fmt_t fmt,
203 unsigned int d_w, unsigned int d_h,
204 unsigned int align,
205 aom_alloc_img_data_cb_fn_t alloc_cb,
206 void *cb_priv) {
207 return img_alloc_helper(img, fmt, d_w, d_h, align, align, 1, 0, NULL,
208 alloc_cb, cb_priv);
209 }
210
aom_img_wrap(aom_image_t * img,aom_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int stride_align,unsigned char * img_data)211 aom_image_t *aom_img_wrap(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w,
212 unsigned int d_h, unsigned int stride_align,
213 unsigned char *img_data) {
214 /* Set buf_align = 1. It is ignored by img_alloc_helper because img_data is
215 * not NULL. */
216 return img_alloc_helper(img, fmt, d_w, d_h, 1, stride_align, 1, 0, img_data,
217 NULL, NULL);
218 }
219
aom_img_alloc_with_border(aom_image_t * img,aom_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align,unsigned int size_align,unsigned int border)220 aom_image_t *aom_img_alloc_with_border(aom_image_t *img, aom_img_fmt_t fmt,
221 unsigned int d_w, unsigned int d_h,
222 unsigned int align,
223 unsigned int size_align,
224 unsigned int border) {
225 return img_alloc_helper(img, fmt, d_w, d_h, align, align, size_align, border,
226 NULL, NULL, NULL);
227 }
228
aom_img_set_rect(aom_image_t * img,unsigned int x,unsigned int y,unsigned int w,unsigned int h,unsigned int border)229 int aom_img_set_rect(aom_image_t *img, unsigned int x, unsigned int y,
230 unsigned int w, unsigned int h, unsigned int border) {
231 if (x <= UINT_MAX - w && x + w <= img->w && y <= UINT_MAX - h &&
232 y + h <= img->h) {
233 img->d_w = w;
234 img->d_h = h;
235
236 x += border;
237 y += border;
238
239 /* Calculate plane pointers */
240 if (!(img->fmt & AOM_IMG_FMT_PLANAR)) {
241 img->planes[AOM_PLANE_PACKED] =
242 img->img_data + x * img->bps / 8 + y * img->stride[AOM_PLANE_PACKED];
243 } else {
244 const int bytes_per_sample =
245 (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
246 unsigned char *data = img->img_data;
247
248 img->planes[AOM_PLANE_Y] =
249 data + x * bytes_per_sample + y * img->stride[AOM_PLANE_Y];
250 data += ((size_t)img->h + 2 * border) * img->stride[AOM_PLANE_Y];
251
252 unsigned int uv_border_h = border >> img->y_chroma_shift;
253 unsigned int uv_x = x >> img->x_chroma_shift;
254 unsigned int uv_y = y >> img->y_chroma_shift;
255 if (img->fmt == AOM_IMG_FMT_NV12) {
256 img->planes[AOM_PLANE_U] = data + uv_x * bytes_per_sample * 2 +
257 uv_y * img->stride[AOM_PLANE_U];
258 img->planes[AOM_PLANE_V] = NULL;
259 } else if (!(img->fmt & AOM_IMG_FMT_UV_FLIP)) {
260 img->planes[AOM_PLANE_U] =
261 data + uv_x * bytes_per_sample + uv_y * img->stride[AOM_PLANE_U];
262 data += ((size_t)(img->h >> img->y_chroma_shift) + 2 * uv_border_h) *
263 img->stride[AOM_PLANE_U];
264 img->planes[AOM_PLANE_V] =
265 data + uv_x * bytes_per_sample + uv_y * img->stride[AOM_PLANE_V];
266 } else {
267 img->planes[AOM_PLANE_V] =
268 data + uv_x * bytes_per_sample + uv_y * img->stride[AOM_PLANE_V];
269 data += ((size_t)(img->h >> img->y_chroma_shift) + 2 * uv_border_h) *
270 img->stride[AOM_PLANE_V];
271 img->planes[AOM_PLANE_U] =
272 data + uv_x * bytes_per_sample + uv_y * img->stride[AOM_PLANE_U];
273 }
274 }
275 return 0;
276 }
277 return -1;
278 }
279
aom_img_flip(aom_image_t * img)280 void aom_img_flip(aom_image_t *img) {
281 /* Note: In the calculation pointer adjustment calculation, we want the
282 * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99
283 * standard indicates that if the adjustment parameter is unsigned, the
284 * stride parameter will be promoted to unsigned, causing errors when
285 * the lhs is a larger type than the rhs.
286 */
287 img->planes[AOM_PLANE_Y] += (signed)(img->d_h - 1) * img->stride[AOM_PLANE_Y];
288 img->stride[AOM_PLANE_Y] = -img->stride[AOM_PLANE_Y];
289
290 img->planes[AOM_PLANE_U] += (signed)((img->d_h >> img->y_chroma_shift) - 1) *
291 img->stride[AOM_PLANE_U];
292 img->stride[AOM_PLANE_U] = -img->stride[AOM_PLANE_U];
293
294 img->planes[AOM_PLANE_V] += (signed)((img->d_h >> img->y_chroma_shift) - 1) *
295 img->stride[AOM_PLANE_V];
296 img->stride[AOM_PLANE_V] = -img->stride[AOM_PLANE_V];
297 }
298
aom_img_free(aom_image_t * img)299 void aom_img_free(aom_image_t *img) {
300 if (img) {
301 aom_img_remove_metadata(img);
302 if (img->img_data && img->img_data_owner) aom_free(img->img_data);
303
304 if (img->self_allocd) free(img);
305 }
306 }
307
aom_img_plane_width(const aom_image_t * img,int plane)308 int aom_img_plane_width(const aom_image_t *img, int plane) {
309 if (plane > 0)
310 return (img->d_w + img->x_chroma_shift) >> img->x_chroma_shift;
311 else
312 return img->d_w;
313 }
314
aom_img_plane_height(const aom_image_t * img,int plane)315 int aom_img_plane_height(const aom_image_t *img, int plane) {
316 if (plane > 0)
317 return (img->d_h + img->y_chroma_shift) >> img->y_chroma_shift;
318 else
319 return img->d_h;
320 }
321
aom_img_metadata_alloc(uint32_t type,const uint8_t * data,size_t sz,aom_metadata_insert_flags_t insert_flag)322 aom_metadata_t *aom_img_metadata_alloc(
323 uint32_t type, const uint8_t *data, size_t sz,
324 aom_metadata_insert_flags_t insert_flag) {
325 if (!data || sz == 0) return NULL;
326 aom_metadata_t *metadata = (aom_metadata_t *)malloc(sizeof(aom_metadata_t));
327 if (!metadata) return NULL;
328 metadata->type = type;
329 metadata->payload = (uint8_t *)malloc(sz);
330 if (!metadata->payload) {
331 free(metadata);
332 return NULL;
333 }
334 memcpy(metadata->payload, data, sz);
335 metadata->sz = sz;
336 metadata->insert_flag = insert_flag;
337 return metadata;
338 }
339
aom_img_metadata_free(aom_metadata_t * metadata)340 void aom_img_metadata_free(aom_metadata_t *metadata) {
341 if (metadata) {
342 if (metadata->payload) free(metadata->payload);
343 free(metadata);
344 }
345 }
346
aom_img_metadata_array_alloc(size_t sz)347 aom_metadata_array_t *aom_img_metadata_array_alloc(size_t sz) {
348 aom_metadata_array_t *arr =
349 (aom_metadata_array_t *)calloc(1, sizeof(aom_metadata_array_t));
350 if (!arr) return NULL;
351 if (sz > 0) {
352 arr->metadata_array =
353 (aom_metadata_t **)calloc(sz, sizeof(aom_metadata_t *));
354 if (!arr->metadata_array) {
355 aom_img_metadata_array_free(arr);
356 return NULL;
357 }
358 arr->sz = sz;
359 }
360 return arr;
361 }
362
aom_img_metadata_array_free(aom_metadata_array_t * arr)363 void aom_img_metadata_array_free(aom_metadata_array_t *arr) {
364 if (arr) {
365 if (arr->metadata_array) {
366 for (size_t i = 0; i < arr->sz; i++) {
367 aom_img_metadata_free(arr->metadata_array[i]);
368 }
369 free(arr->metadata_array);
370 }
371 free(arr);
372 }
373 }
374
aom_img_add_metadata(aom_image_t * img,uint32_t type,const uint8_t * data,size_t sz,aom_metadata_insert_flags_t insert_flag)375 int aom_img_add_metadata(aom_image_t *img, uint32_t type, const uint8_t *data,
376 size_t sz, aom_metadata_insert_flags_t insert_flag) {
377 if (!img) return -1;
378 if (!img->metadata) {
379 img->metadata = aom_img_metadata_array_alloc(0);
380 if (!img->metadata) return -1;
381 }
382 aom_metadata_t *metadata =
383 aom_img_metadata_alloc(type, data, sz, insert_flag);
384 if (!metadata) return -1;
385 aom_metadata_t **metadata_array =
386 (aom_metadata_t **)realloc(img->metadata->metadata_array,
387 (img->metadata->sz + 1) * sizeof(metadata));
388 if (!metadata_array) {
389 aom_img_metadata_free(metadata);
390 return -1;
391 }
392 img->metadata->metadata_array = metadata_array;
393 img->metadata->metadata_array[img->metadata->sz] = metadata;
394 img->metadata->sz++;
395 return 0;
396 }
397
aom_img_remove_metadata(aom_image_t * img)398 void aom_img_remove_metadata(aom_image_t *img) {
399 if (img && img->metadata) {
400 aom_img_metadata_array_free(img->metadata);
401 img->metadata = NULL;
402 }
403 }
404
aom_img_get_metadata(const aom_image_t * img,size_t index)405 const aom_metadata_t *aom_img_get_metadata(const aom_image_t *img,
406 size_t index) {
407 if (!img) return NULL;
408 const aom_metadata_array_t *array = img->metadata;
409 if (array && index < array->sz) {
410 return array->metadata_array[index];
411 }
412 return NULL;
413 }
414
aom_img_num_metadata(const aom_image_t * img)415 size_t aom_img_num_metadata(const aom_image_t *img) {
416 if (!img || !img->metadata) return 0;
417 return img->metadata->sz;
418 }
419