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