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 aom_img_set_rect(img, 0, 0, d_w, d_h, border);
186 return img;
187
188 fail:
189 aom_img_free(img);
190 return NULL;
191 }
192
aom_img_alloc(aom_image_t * img,aom_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align)193 aom_image_t *aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt,
194 unsigned int d_w, unsigned int d_h,
195 unsigned int align) {
196 return img_alloc_helper(img, fmt, d_w, d_h, align, align, 1, 0, NULL, NULL,
197 NULL);
198 }
199
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)200 aom_image_t *aom_img_alloc_with_cb(aom_image_t *img, aom_img_fmt_t fmt,
201 unsigned int d_w, unsigned int d_h,
202 unsigned int align,
203 aom_alloc_img_data_cb_fn_t alloc_cb,
204 void *cb_priv) {
205 return img_alloc_helper(img, fmt, d_w, d_h, align, align, 1, 0, NULL,
206 alloc_cb, cb_priv);
207 }
208
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)209 aom_image_t *aom_img_wrap(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w,
210 unsigned int d_h, unsigned int stride_align,
211 unsigned char *img_data) {
212 /* Set buf_align = 1. It is ignored by img_alloc_helper because img_data is
213 * not NULL. */
214 return img_alloc_helper(img, fmt, d_w, d_h, 1, stride_align, 1, 0, img_data,
215 NULL, NULL);
216 }
217
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)218 aom_image_t *aom_img_alloc_with_border(aom_image_t *img, aom_img_fmt_t fmt,
219 unsigned int d_w, unsigned int d_h,
220 unsigned int align,
221 unsigned int size_align,
222 unsigned int border) {
223 return img_alloc_helper(img, fmt, d_w, d_h, align, align, size_align, border,
224 NULL, NULL, NULL);
225 }
226
aom_img_set_rect(aom_image_t * img,unsigned int x,unsigned int y,unsigned int w,unsigned int h,unsigned int border)227 int aom_img_set_rect(aom_image_t *img, unsigned int x, unsigned int y,
228 unsigned int w, unsigned int h, unsigned int border) {
229 if (x <= UINT_MAX - w && x + w <= img->w && y <= UINT_MAX - h &&
230 y + h <= img->h) {
231 img->d_w = w;
232 img->d_h = h;
233
234 x += border;
235 y += border;
236
237 /* Calculate plane pointers */
238 if (!(img->fmt & AOM_IMG_FMT_PLANAR)) {
239 img->planes[AOM_PLANE_PACKED] =
240 img->img_data + x * img->bps / 8 + y * img->stride[AOM_PLANE_PACKED];
241 } else {
242 const int bytes_per_sample =
243 (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
244 unsigned char *data = img->img_data;
245
246 img->planes[AOM_PLANE_Y] =
247 data + x * bytes_per_sample + y * img->stride[AOM_PLANE_Y];
248 data += ((size_t)img->h + 2 * border) * img->stride[AOM_PLANE_Y];
249
250 unsigned int uv_border_h = border >> img->y_chroma_shift;
251 unsigned int uv_x = x >> img->x_chroma_shift;
252 unsigned int uv_y = y >> img->y_chroma_shift;
253 if (img->fmt == AOM_IMG_FMT_NV12) {
254 img->planes[AOM_PLANE_U] = data + uv_x * bytes_per_sample * 2 +
255 uv_y * img->stride[AOM_PLANE_U];
256 img->planes[AOM_PLANE_V] = NULL;
257 } else if (!(img->fmt & AOM_IMG_FMT_UV_FLIP)) {
258 img->planes[AOM_PLANE_U] =
259 data + uv_x * bytes_per_sample + uv_y * img->stride[AOM_PLANE_U];
260 data += ((size_t)(img->h >> img->y_chroma_shift) + 2 * uv_border_h) *
261 img->stride[AOM_PLANE_U];
262 img->planes[AOM_PLANE_V] =
263 data + uv_x * bytes_per_sample + uv_y * img->stride[AOM_PLANE_V];
264 } else {
265 img->planes[AOM_PLANE_V] =
266 data + uv_x * bytes_per_sample + uv_y * img->stride[AOM_PLANE_V];
267 data += ((size_t)(img->h >> img->y_chroma_shift) + 2 * uv_border_h) *
268 img->stride[AOM_PLANE_V];
269 img->planes[AOM_PLANE_U] =
270 data + uv_x * bytes_per_sample + uv_y * img->stride[AOM_PLANE_U];
271 }
272 }
273 return 0;
274 }
275 return -1;
276 }
277
aom_img_flip(aom_image_t * img)278 void aom_img_flip(aom_image_t *img) {
279 /* Note: In the calculation pointer adjustment calculation, we want the
280 * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99
281 * standard indicates that if the adjustment parameter is unsigned, the
282 * stride parameter will be promoted to unsigned, causing errors when
283 * the lhs is a larger type than the rhs.
284 */
285 img->planes[AOM_PLANE_Y] += (signed)(img->d_h - 1) * img->stride[AOM_PLANE_Y];
286 img->stride[AOM_PLANE_Y] = -img->stride[AOM_PLANE_Y];
287
288 img->planes[AOM_PLANE_U] += (signed)((img->d_h >> img->y_chroma_shift) - 1) *
289 img->stride[AOM_PLANE_U];
290 img->stride[AOM_PLANE_U] = -img->stride[AOM_PLANE_U];
291
292 img->planes[AOM_PLANE_V] += (signed)((img->d_h >> img->y_chroma_shift) - 1) *
293 img->stride[AOM_PLANE_V];
294 img->stride[AOM_PLANE_V] = -img->stride[AOM_PLANE_V];
295 }
296
aom_img_free(aom_image_t * img)297 void aom_img_free(aom_image_t *img) {
298 if (img) {
299 aom_img_remove_metadata(img);
300 if (img->img_data && img->img_data_owner) aom_free(img->img_data);
301
302 if (img->self_allocd) free(img);
303 }
304 }
305
aom_img_plane_width(const aom_image_t * img,int plane)306 int aom_img_plane_width(const aom_image_t *img, int plane) {
307 if (plane > 0)
308 return (img->d_w + img->x_chroma_shift) >> img->x_chroma_shift;
309 else
310 return img->d_w;
311 }
312
aom_img_plane_height(const aom_image_t * img,int plane)313 int aom_img_plane_height(const aom_image_t *img, int plane) {
314 if (plane > 0)
315 return (img->d_h + img->y_chroma_shift) >> img->y_chroma_shift;
316 else
317 return img->d_h;
318 }
319
aom_img_metadata_alloc(uint32_t type,const uint8_t * data,size_t sz,aom_metadata_insert_flags_t insert_flag)320 aom_metadata_t *aom_img_metadata_alloc(
321 uint32_t type, const uint8_t *data, size_t sz,
322 aom_metadata_insert_flags_t insert_flag) {
323 if (!data || sz == 0) return NULL;
324 aom_metadata_t *metadata = (aom_metadata_t *)malloc(sizeof(aom_metadata_t));
325 if (!metadata) return NULL;
326 metadata->type = type;
327 metadata->payload = (uint8_t *)malloc(sz);
328 if (!metadata->payload) {
329 free(metadata);
330 return NULL;
331 }
332 memcpy(metadata->payload, data, sz);
333 metadata->sz = sz;
334 metadata->insert_flag = insert_flag;
335 return metadata;
336 }
337
aom_img_metadata_free(aom_metadata_t * metadata)338 void aom_img_metadata_free(aom_metadata_t *metadata) {
339 if (metadata) {
340 if (metadata->payload) free(metadata->payload);
341 free(metadata);
342 }
343 }
344
aom_img_metadata_array_alloc(size_t sz)345 aom_metadata_array_t *aom_img_metadata_array_alloc(size_t sz) {
346 aom_metadata_array_t *arr =
347 (aom_metadata_array_t *)calloc(1, sizeof(aom_metadata_array_t));
348 if (!arr) return NULL;
349 if (sz > 0) {
350 arr->metadata_array =
351 (aom_metadata_t **)calloc(sz, sizeof(aom_metadata_t *));
352 if (!arr->metadata_array) {
353 aom_img_metadata_array_free(arr);
354 return NULL;
355 }
356 arr->sz = sz;
357 }
358 return arr;
359 }
360
aom_img_metadata_array_free(aom_metadata_array_t * arr)361 void aom_img_metadata_array_free(aom_metadata_array_t *arr) {
362 if (arr) {
363 if (arr->metadata_array) {
364 for (size_t i = 0; i < arr->sz; i++) {
365 aom_img_metadata_free(arr->metadata_array[i]);
366 }
367 free(arr->metadata_array);
368 }
369 free(arr);
370 }
371 }
372
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)373 int aom_img_add_metadata(aom_image_t *img, uint32_t type, const uint8_t *data,
374 size_t sz, aom_metadata_insert_flags_t insert_flag) {
375 if (!img) return -1;
376 if (!img->metadata) {
377 img->metadata = aom_img_metadata_array_alloc(0);
378 if (!img->metadata) return -1;
379 }
380 aom_metadata_t *metadata =
381 aom_img_metadata_alloc(type, data, sz, insert_flag);
382 if (!metadata) return -1;
383 aom_metadata_t **metadata_array =
384 (aom_metadata_t **)realloc(img->metadata->metadata_array,
385 (img->metadata->sz + 1) * sizeof(metadata));
386 if (!metadata_array) {
387 aom_img_metadata_free(metadata);
388 return -1;
389 }
390 img->metadata->metadata_array = metadata_array;
391 img->metadata->metadata_array[img->metadata->sz] = metadata;
392 img->metadata->sz++;
393 return 0;
394 }
395
aom_img_remove_metadata(aom_image_t * img)396 void aom_img_remove_metadata(aom_image_t *img) {
397 if (img && img->metadata) {
398 aom_img_metadata_array_free(img->metadata);
399 img->metadata = NULL;
400 }
401 }
402
aom_img_get_metadata(const aom_image_t * img,size_t index)403 const aom_metadata_t *aom_img_get_metadata(const aom_image_t *img,
404 size_t index) {
405 if (!img) return NULL;
406 const aom_metadata_array_t *array = img->metadata;
407 if (array && index < array->sz) {
408 return array->metadata_array[index];
409 }
410 return NULL;
411 }
412
aom_img_num_metadata(const aom_image_t * img)413 size_t aom_img_num_metadata(const aom_image_t *img) {
414 if (!img || !img->metadata) return 0;
415 return img->metadata->sz;
416 }
417