| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <stdlib.h> | 11 #include <stdlib.h> |
| 12 #include <string.h> | 12 #include <string.h> |
| 13 | 13 |
| 14 #include "vpx/vpx_image.h" | 14 #include "vpx/vpx_image.h" |
| 15 #include "vpx/vpx_integer.h" | 15 #include "vpx/vpx_integer.h" |
| 16 #include "vpx_mem/vpx_mem.h" | 16 #include "vpx_mem/vpx_mem.h" |
| 17 | 17 |
| 18 static vpx_image_t *img_alloc_helper(vpx_image_t *img, | 18 static vpx_image_t *img_alloc_helper(vpx_image_t *img, |
| 19 vpx_img_fmt_t fmt, | 19 vpx_img_fmt_t fmt, |
| 20 unsigned int d_w, | 20 unsigned int d_w, |
| 21 unsigned int d_h, | 21 unsigned int d_h, |
| 22 unsigned int buf_align, | 22 unsigned int buf_align, |
| 23 unsigned int stride_align, | 23 unsigned int stride_align, |
| 24 unsigned char *img_data) { | 24 unsigned char *img_data) { |
| 25 | 25 unsigned int h, w, s, xcs, ycs, bps; |
| 26 unsigned int h, w, s, xcs, ycs, bps; | 26 unsigned int stride_in_bytes; |
| 27 int align; | 27 int align; |
| 28 | 28 |
| 29 /* Treat align==0 like align==1 */ | 29 /* Treat align==0 like align==1 */ |
| 30 if (!buf_align) | 30 if (!buf_align) |
| 31 buf_align = 1; | 31 buf_align = 1; |
| 32 | 32 |
| 33 /* Validate alignment (must be power of 2) */ | 33 /* Validate alignment (must be power of 2) */ |
| 34 if (buf_align & (buf_align - 1)) | 34 if (buf_align & (buf_align - 1)) |
| 35 goto fail; | 35 goto fail; |
| 36 | 36 |
| 37 /* Treat align==0 like align==1 */ | 37 /* Treat align==0 like align==1 */ |
| (...skipping 25 matching lines...) Expand all Loading... |
| 63 case VPX_IMG_FMT_YVYU: | 63 case VPX_IMG_FMT_YVYU: |
| 64 bps = 16; | 64 bps = 16; |
| 65 break; | 65 break; |
| 66 case VPX_IMG_FMT_I420: | 66 case VPX_IMG_FMT_I420: |
| 67 case VPX_IMG_FMT_YV12: | 67 case VPX_IMG_FMT_YV12: |
| 68 case VPX_IMG_FMT_VPXI420: | 68 case VPX_IMG_FMT_VPXI420: |
| 69 case VPX_IMG_FMT_VPXYV12: | 69 case VPX_IMG_FMT_VPXYV12: |
| 70 bps = 12; | 70 bps = 12; |
| 71 break; | 71 break; |
| 72 case VPX_IMG_FMT_I422: | 72 case VPX_IMG_FMT_I422: |
| 73 case VPX_IMG_FMT_I440: |
| 73 bps = 16; | 74 bps = 16; |
| 74 break; | 75 break; |
| 75 case VPX_IMG_FMT_I444: | 76 case VPX_IMG_FMT_I444: |
| 76 bps = 24; | 77 bps = 24; |
| 77 break; | 78 break; |
| 78 case VPX_IMG_FMT_I42016: | 79 case VPX_IMG_FMT_I42016: |
| 79 bps = 24; | 80 bps = 24; |
| 80 break; | 81 break; |
| 81 case VPX_IMG_FMT_I42216: | 82 case VPX_IMG_FMT_I42216: |
| 83 case VPX_IMG_FMT_I44016: |
| 82 bps = 32; | 84 bps = 32; |
| 83 break; | 85 break; |
| 84 case VPX_IMG_FMT_I44416: | 86 case VPX_IMG_FMT_I44416: |
| 85 bps = 48; | 87 bps = 48; |
| 86 break; | 88 break; |
| 87 default: | 89 default: |
| 88 bps = 16; | 90 bps = 16; |
| 89 break; | 91 break; |
| 90 } | 92 } |
| 91 | 93 |
| 92 /* Get chroma shift values for this format */ | 94 /* Get chroma shift values for this format */ |
| 93 switch (fmt) { | 95 switch (fmt) { |
| 94 case VPX_IMG_FMT_I420: | 96 case VPX_IMG_FMT_I420: |
| 95 case VPX_IMG_FMT_YV12: | 97 case VPX_IMG_FMT_YV12: |
| 96 case VPX_IMG_FMT_VPXI420: | 98 case VPX_IMG_FMT_VPXI420: |
| 97 case VPX_IMG_FMT_VPXYV12: | 99 case VPX_IMG_FMT_VPXYV12: |
| 98 case VPX_IMG_FMT_I422: | 100 case VPX_IMG_FMT_I422: |
| 99 case VPX_IMG_FMT_I42016: | 101 case VPX_IMG_FMT_I42016: |
| 100 case VPX_IMG_FMT_I42216: | 102 case VPX_IMG_FMT_I42216: |
| 101 xcs = 1; | 103 xcs = 1; |
| 102 break; | 104 break; |
| 103 default: | 105 default: |
| 104 xcs = 0; | 106 xcs = 0; |
| 105 break; | 107 break; |
| 106 } | 108 } |
| 107 | 109 |
| 108 switch (fmt) { | 110 switch (fmt) { |
| 109 case VPX_IMG_FMT_I420: | 111 case VPX_IMG_FMT_I420: |
| 112 case VPX_IMG_FMT_I440: |
| 110 case VPX_IMG_FMT_YV12: | 113 case VPX_IMG_FMT_YV12: |
| 111 case VPX_IMG_FMT_VPXI420: | 114 case VPX_IMG_FMT_VPXI420: |
| 112 case VPX_IMG_FMT_VPXYV12: | 115 case VPX_IMG_FMT_VPXYV12: |
| 116 case VPX_IMG_FMT_I42016: |
| 117 case VPX_IMG_FMT_I44016: |
| 113 ycs = 1; | 118 ycs = 1; |
| 114 break; | 119 break; |
| 115 default: | 120 default: |
| 116 ycs = 0; | 121 ycs = 0; |
| 117 break; | 122 break; |
| 118 } | 123 } |
| 119 | 124 |
| 120 /* Calculate storage sizes given the chroma subsampling */ | 125 /* Calculate storage sizes given the chroma subsampling */ |
| 121 align = (1 << xcs) - 1; | 126 align = (1 << xcs) - 1; |
| 122 w = (d_w + align) & ~align; | 127 w = (d_w + align) & ~align; |
| 123 align = (1 << ycs) - 1; | 128 align = (1 << ycs) - 1; |
| 124 h = (d_h + align) & ~align; | 129 h = (d_h + align) & ~align; |
| 125 s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8; | 130 s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8; |
| 126 s = (s + stride_align - 1) & ~(stride_align - 1); | 131 s = (s + stride_align - 1) & ~(stride_align - 1); |
| 132 stride_in_bytes = (fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? s * 2 : s; |
| 127 | 133 |
| 128 /* Allocate the new image */ | 134 /* Allocate the new image */ |
| 129 if (!img) { | 135 if (!img) { |
| 130 img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t)); | 136 img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t)); |
| 131 | 137 |
| 132 if (!img) | 138 if (!img) |
| 133 goto fail; | 139 goto fail; |
| 134 | 140 |
| 135 img->self_allocd = 1; | 141 img->self_allocd = 1; |
| 136 } else { | 142 } else { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 155 | 161 |
| 156 img->fmt = fmt; | 162 img->fmt = fmt; |
| 157 img->bit_depth = (fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 16 : 8; | 163 img->bit_depth = (fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 16 : 8; |
| 158 img->w = w; | 164 img->w = w; |
| 159 img->h = h; | 165 img->h = h; |
| 160 img->x_chroma_shift = xcs; | 166 img->x_chroma_shift = xcs; |
| 161 img->y_chroma_shift = ycs; | 167 img->y_chroma_shift = ycs; |
| 162 img->bps = bps; | 168 img->bps = bps; |
| 163 | 169 |
| 164 /* Calculate strides */ | 170 /* Calculate strides */ |
| 165 img->stride[VPX_PLANE_Y] = img->stride[VPX_PLANE_ALPHA] = s; | 171 img->stride[VPX_PLANE_Y] = img->stride[VPX_PLANE_ALPHA] = stride_in_bytes; |
| 166 img->stride[VPX_PLANE_U] = img->stride[VPX_PLANE_V] = s >> xcs; | 172 img->stride[VPX_PLANE_U] = img->stride[VPX_PLANE_V] = stride_in_bytes >> xcs; |
| 167 | 173 |
| 168 /* Default viewport to entire image */ | 174 /* Default viewport to entire image */ |
| 169 if (!vpx_img_set_rect(img, 0, 0, d_w, d_h)) | 175 if (!vpx_img_set_rect(img, 0, 0, d_w, d_h)) |
| 170 return img; | 176 return img; |
| 171 | 177 |
| 172 fail: | 178 fail: |
| 173 vpx_img_free(img); | 179 vpx_img_free(img); |
| 174 return NULL; | 180 return NULL; |
| 175 } | 181 } |
| 176 | 182 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 202 | 208 |
| 203 if (x + w <= img->w && y + h <= img->h) { | 209 if (x + w <= img->w && y + h <= img->h) { |
| 204 img->d_w = w; | 210 img->d_w = w; |
| 205 img->d_h = h; | 211 img->d_h = h; |
| 206 | 212 |
| 207 /* Calculate plane pointers */ | 213 /* Calculate plane pointers */ |
| 208 if (!(img->fmt & VPX_IMG_FMT_PLANAR)) { | 214 if (!(img->fmt & VPX_IMG_FMT_PLANAR)) { |
| 209 img->planes[VPX_PLANE_PACKED] = | 215 img->planes[VPX_PLANE_PACKED] = |
| 210 img->img_data + x * img->bps / 8 + y * img->stride[VPX_PLANE_PACKED]; | 216 img->img_data + x * img->bps / 8 + y * img->stride[VPX_PLANE_PACKED]; |
| 211 } else { | 217 } else { |
| 218 const int bytes_per_sample = |
| 219 (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1; |
| 212 data = img->img_data; | 220 data = img->img_data; |
| 213 | 221 |
| 214 if (img->fmt & VPX_IMG_FMT_HAS_ALPHA) { | 222 if (img->fmt & VPX_IMG_FMT_HAS_ALPHA) { |
| 215 img->planes[VPX_PLANE_ALPHA] = | 223 img->planes[VPX_PLANE_ALPHA] = |
| 216 data + x + y * img->stride[VPX_PLANE_ALPHA]; | 224 data + x * bytes_per_sample + y * img->stride[VPX_PLANE_ALPHA]; |
| 217 data += img->h * img->stride[VPX_PLANE_ALPHA]; | 225 data += img->h * img->stride[VPX_PLANE_ALPHA]; |
| 218 } | 226 } |
| 219 | 227 |
| 220 img->planes[VPX_PLANE_Y] = data + x + y * img->stride[VPX_PLANE_Y]; | 228 img->planes[VPX_PLANE_Y] = data + x * bytes_per_sample + |
| 229 y * img->stride[VPX_PLANE_Y]; |
| 221 data += img->h * img->stride[VPX_PLANE_Y]; | 230 data += img->h * img->stride[VPX_PLANE_Y]; |
| 222 | 231 |
| 223 if (!(img->fmt & VPX_IMG_FMT_UV_FLIP)) { | 232 if (!(img->fmt & VPX_IMG_FMT_UV_FLIP)) { |
| 224 img->planes[VPX_PLANE_U] = data | 233 img->planes[VPX_PLANE_U] = |
| 225 + (x >> img->x_chroma_shift) | 234 data + (x >> img->x_chroma_shift) * bytes_per_sample + |
| 226 + (y >> img->y_chroma_shift) * img->stride[VP
X_PLANE_U]; | 235 (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; |
| 227 data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; | 236 data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; |
| 228 img->planes[VPX_PLANE_V] = data | 237 img->planes[VPX_PLANE_V] = |
| 229 + (x >> img->x_chroma_shift) | 238 data + (x >> img->x_chroma_shift) * bytes_per_sample + |
| 230 + (y >> img->y_chroma_shift) * img->stride[VP
X_PLANE_V]; | 239 (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; |
| 231 } else { | 240 } else { |
| 232 img->planes[VPX_PLANE_V] = data | 241 img->planes[VPX_PLANE_V] = |
| 233 + (x >> img->x_chroma_shift) | 242 data + (x >> img->x_chroma_shift) * bytes_per_sample + |
| 234 + (y >> img->y_chroma_shift) * img->stride[VP
X_PLANE_V]; | 243 (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; |
| 235 data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; | 244 data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; |
| 236 img->planes[VPX_PLANE_U] = data | 245 img->planes[VPX_PLANE_U] = |
| 237 + (x >> img->x_chroma_shift) | 246 data + (x >> img->x_chroma_shift) * bytes_per_sample + |
| 238 + (y >> img->y_chroma_shift) * img->stride[VP
X_PLANE_U]; | 247 (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; |
| 239 } | 248 } |
| 240 } | 249 } |
| 241 | |
| 242 return 0; | 250 return 0; |
| 243 } | 251 } |
| 244 | |
| 245 return -1; | 252 return -1; |
| 246 } | 253 } |
| 247 | 254 |
| 248 void vpx_img_flip(vpx_image_t *img) { | 255 void vpx_img_flip(vpx_image_t *img) { |
| 249 /* Note: In the calculation pointer adjustment calculation, we want the | 256 /* Note: In the calculation pointer adjustment calculation, we want the |
| 250 * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99 | 257 * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99 |
| 251 * standard indicates that if the adjustment parameter is unsigned, the | 258 * standard indicates that if the adjustment parameter is unsigned, the |
| 252 * stride parameter will be promoted to unsigned, causing errors when | 259 * stride parameter will be promoted to unsigned, causing errors when |
| 253 * the lhs is a larger type than the rhs. | 260 * the lhs is a larger type than the rhs. |
| 254 */ | 261 */ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 269 | 276 |
| 270 void vpx_img_free(vpx_image_t *img) { | 277 void vpx_img_free(vpx_image_t *img) { |
| 271 if (img) { | 278 if (img) { |
| 272 if (img->img_data && img->img_data_owner) | 279 if (img->img_data && img->img_data_owner) |
| 273 vpx_free(img->img_data); | 280 vpx_free(img->img_data); |
| 274 | 281 |
| 275 if (img->self_allocd) | 282 if (img->self_allocd) |
| 276 free(img); | 283 free(img); |
| 277 } | 284 } |
| 278 } | 285 } |
| OLD | NEW |