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