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, vpx_img_fmt_t fmt,
19                                      unsigned int d_w, unsigned int d_h,
20                                      unsigned int buf_align,
21                                      unsigned int stride_align,
22                                      unsigned char *img_data) {
23   unsigned int h, w, s, xcs, ycs, bps;
24   unsigned int stride_in_bytes;
25   int align;
26 
27   /* Treat align==0 like align==1 */
28   if (!buf_align) buf_align = 1;
29 
30   /* Validate alignment (must be power of 2) */
31   if (buf_align & (buf_align - 1)) goto fail;
32 
33   /* Treat align==0 like align==1 */
34   if (!stride_align) stride_align = 1;
35 
36   /* Validate alignment (must be power of 2) */
37   if (stride_align & (stride_align - 1)) goto fail;
38 
39   /* Get sample size for this format */
40   switch (fmt) {
41     case VPX_IMG_FMT_I420:
42     case VPX_IMG_FMT_YV12: bps = 12; break;
43     case VPX_IMG_FMT_I422:
44     case VPX_IMG_FMT_I440: bps = 16; break;
45     case VPX_IMG_FMT_I444: bps = 24; break;
46     case VPX_IMG_FMT_I42016: bps = 24; break;
47     case VPX_IMG_FMT_I42216:
48     case VPX_IMG_FMT_I44016: bps = 32; break;
49     case VPX_IMG_FMT_I44416: bps = 48; break;
50     default: bps = 16; break;
51   }
52 
53   /* Get chroma shift values for this format */
54   switch (fmt) {
55     case VPX_IMG_FMT_I420:
56     case VPX_IMG_FMT_YV12:
57     case VPX_IMG_FMT_I422:
58     case VPX_IMG_FMT_I42016:
59     case VPX_IMG_FMT_I42216: xcs = 1; break;
60     default: xcs = 0; break;
61   }
62 
63   switch (fmt) {
64     case VPX_IMG_FMT_I420:
65     case VPX_IMG_FMT_I440:
66     case VPX_IMG_FMT_YV12:
67     case VPX_IMG_FMT_I42016:
68     case VPX_IMG_FMT_I44016: ycs = 1; break;
69     default: ycs = 0; break;
70   }
71 
72   /* Calculate storage sizes. If the buffer was allocated externally, the width
73    * and height shouldn't be adjusted. */
74   w = d_w;
75   h = d_h;
76   s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8;
77   s = (s + stride_align - 1) & ~(stride_align - 1);
78   stride_in_bytes = (fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? s * 2 : s;
79 
80   /* Allocate the new image */
81   if (!img) {
82     img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t));
83 
84     if (!img) goto fail;
85 
86     img->self_allocd = 1;
87   } else {
88     memset(img, 0, sizeof(vpx_image_t));
89   }
90 
91   img->img_data = img_data;
92 
93   if (!img_data) {
94     uint64_t alloc_size;
95     /* Calculate storage sizes given the chroma subsampling */
96     align = (1 << xcs) - 1;
97     w = (d_w + align) & ~align;
98     align = (1 << ycs) - 1;
99     h = (d_h + align) & ~align;
100 
101     s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8;
102     s = (s + stride_align - 1) & ~(stride_align - 1);
103     stride_in_bytes = (fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? s * 2 : s;
104     alloc_size = (fmt & VPX_IMG_FMT_PLANAR) ? (uint64_t)h * s * bps / 8
105                                             : (uint64_t)h * s;
106 
107     if (alloc_size != (size_t)alloc_size) goto fail;
108 
109     img->img_data = (uint8_t *)vpx_memalign(buf_align, (size_t)alloc_size);
110     img->img_data_owner = 1;
111   }
112 
113   if (!img->img_data) goto fail;
114 
115   img->fmt = fmt;
116   img->bit_depth = (fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
117   img->w = w;
118   img->h = h;
119   img->x_chroma_shift = xcs;
120   img->y_chroma_shift = ycs;
121   img->bps = bps;
122 
123   /* Calculate strides */
124   img->stride[VPX_PLANE_Y] = img->stride[VPX_PLANE_ALPHA] = stride_in_bytes;
125   img->stride[VPX_PLANE_U] = img->stride[VPX_PLANE_V] = stride_in_bytes >> xcs;
126 
127   /* Default viewport to entire image */
128   if (!vpx_img_set_rect(img, 0, 0, d_w, d_h)) return img;
129 
130 fail:
131   vpx_img_free(img);
132   return NULL;
133 }
134 
vpx_img_alloc(vpx_image_t * img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align)135 vpx_image_t *vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt,
136                            unsigned int d_w, unsigned int d_h,
137                            unsigned int align) {
138   return img_alloc_helper(img, fmt, d_w, d_h, align, align, NULL);
139 }
140 
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)141 vpx_image_t *vpx_img_wrap(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w,
142                           unsigned int d_h, unsigned int stride_align,
143                           unsigned char *img_data) {
144   /* By setting buf_align = 1, we don't change buffer alignment in this
145    * function. */
146   return img_alloc_helper(img, fmt, d_w, d_h, 1, stride_align, img_data);
147 }
148 
vpx_img_set_rect(vpx_image_t * img,unsigned int x,unsigned int y,unsigned int w,unsigned int h)149 int vpx_img_set_rect(vpx_image_t *img, unsigned int x, unsigned int y,
150                      unsigned int w, unsigned int h) {
151   unsigned char *data;
152 
153   if (x + w <= img->w && y + h <= img->h) {
154     img->d_w = w;
155     img->d_h = h;
156 
157     /* Calculate plane pointers */
158     if (!(img->fmt & VPX_IMG_FMT_PLANAR)) {
159       img->planes[VPX_PLANE_PACKED] =
160           img->img_data + x * img->bps / 8 + y * img->stride[VPX_PLANE_PACKED];
161     } else {
162       const int bytes_per_sample =
163           (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
164       data = img->img_data;
165 
166       if (img->fmt & VPX_IMG_FMT_HAS_ALPHA) {
167         img->planes[VPX_PLANE_ALPHA] =
168             data + x * bytes_per_sample + y * img->stride[VPX_PLANE_ALPHA];
169         data += img->h * img->stride[VPX_PLANE_ALPHA];
170       }
171 
172       img->planes[VPX_PLANE_Y] =
173           data + x * bytes_per_sample + y * img->stride[VPX_PLANE_Y];
174       data += img->h * img->stride[VPX_PLANE_Y];
175 
176       if (!(img->fmt & VPX_IMG_FMT_UV_FLIP)) {
177         img->planes[VPX_PLANE_U] =
178             data + (x >> img->x_chroma_shift) * bytes_per_sample +
179             (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U];
180         data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_U];
181         img->planes[VPX_PLANE_V] =
182             data + (x >> img->x_chroma_shift) * bytes_per_sample +
183             (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V];
184       } else {
185         img->planes[VPX_PLANE_V] =
186             data + (x >> img->x_chroma_shift) * bytes_per_sample +
187             (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V];
188         data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_V];
189         img->planes[VPX_PLANE_U] =
190             data + (x >> img->x_chroma_shift) * bytes_per_sample +
191             (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U];
192       }
193     }
194     return 0;
195   }
196   return -1;
197 }
198 
vpx_img_flip(vpx_image_t * img)199 void vpx_img_flip(vpx_image_t *img) {
200   /* Note: In the calculation pointer adjustment calculation, we want the
201    * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99
202    * standard indicates that if the adjustment parameter is unsigned, the
203    * stride parameter will be promoted to unsigned, causing errors when
204    * the lhs is a larger type than the rhs.
205    */
206   img->planes[VPX_PLANE_Y] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE_Y];
207   img->stride[VPX_PLANE_Y] = -img->stride[VPX_PLANE_Y];
208 
209   img->planes[VPX_PLANE_U] += (signed)((img->d_h >> img->y_chroma_shift) - 1) *
210                               img->stride[VPX_PLANE_U];
211   img->stride[VPX_PLANE_U] = -img->stride[VPX_PLANE_U];
212 
213   img->planes[VPX_PLANE_V] += (signed)((img->d_h >> img->y_chroma_shift) - 1) *
214                               img->stride[VPX_PLANE_V];
215   img->stride[VPX_PLANE_V] = -img->stride[VPX_PLANE_V];
216 
217   img->planes[VPX_PLANE_ALPHA] +=
218       (signed)(img->d_h - 1) * img->stride[VPX_PLANE_ALPHA];
219   img->stride[VPX_PLANE_ALPHA] = -img->stride[VPX_PLANE_ALPHA];
220 }
221 
vpx_img_free(vpx_image_t * img)222 void vpx_img_free(vpx_image_t *img) {
223   if (img) {
224     if (img->img_data && img->img_data_owner) vpx_free(img->img_data);
225 
226     if (img->self_allocd) free(img);
227   }
228 }
229