michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include michael@0: #include michael@0: #include "vpx/vpx_image.h" michael@0: michael@0: #define ADDRESS_STORAGE_SIZE sizeof(size_t) michael@0: /*returns an addr aligned to the byte boundary specified by align*/ michael@0: #define align_addr(addr,align) (void*)(((size_t)(addr) + ((align) - 1)) & (size_t)-(align)) michael@0: michael@0: /* Memalign code is copied from vpx_mem.c */ michael@0: static void *img_buf_memalign(size_t align, size_t size) { michael@0: void *addr, michael@0: * x = NULL; michael@0: michael@0: addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE); michael@0: michael@0: if (addr) { michael@0: x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align); michael@0: /* save the actual malloc address */ michael@0: ((size_t *)x)[-1] = (size_t)addr; michael@0: } michael@0: michael@0: return x; michael@0: } michael@0: michael@0: static void img_buf_free(void *memblk) { michael@0: if (memblk) { michael@0: void *addr = (void *)(((size_t *)memblk)[-1]); michael@0: free(addr); michael@0: } michael@0: } michael@0: michael@0: static vpx_image_t *img_alloc_helper(vpx_image_t *img, michael@0: vpx_img_fmt_t fmt, michael@0: unsigned int d_w, michael@0: unsigned int d_h, michael@0: unsigned int buf_align, michael@0: unsigned int stride_align, michael@0: unsigned char *img_data) { michael@0: michael@0: unsigned int h, w, s, xcs, ycs, bps; michael@0: int align; michael@0: michael@0: /* Treat align==0 like align==1 */ michael@0: if (!buf_align) michael@0: buf_align = 1; michael@0: michael@0: /* Validate alignment (must be power of 2) */ michael@0: if (buf_align & (buf_align - 1)) michael@0: goto fail; michael@0: michael@0: /* Treat align==0 like align==1 */ michael@0: if (!stride_align) michael@0: stride_align = 1; michael@0: michael@0: /* Validate alignment (must be power of 2) */ michael@0: if (stride_align & (stride_align - 1)) michael@0: goto fail; michael@0: michael@0: /* Get sample size for this format */ michael@0: switch (fmt) { michael@0: case VPX_IMG_FMT_RGB32: michael@0: case VPX_IMG_FMT_RGB32_LE: michael@0: case VPX_IMG_FMT_ARGB: michael@0: case VPX_IMG_FMT_ARGB_LE: michael@0: bps = 32; michael@0: break; michael@0: case VPX_IMG_FMT_RGB24: michael@0: case VPX_IMG_FMT_BGR24: michael@0: bps = 24; michael@0: break; michael@0: case VPX_IMG_FMT_RGB565: michael@0: case VPX_IMG_FMT_RGB565_LE: michael@0: case VPX_IMG_FMT_RGB555: michael@0: case VPX_IMG_FMT_RGB555_LE: michael@0: case VPX_IMG_FMT_UYVY: michael@0: case VPX_IMG_FMT_YUY2: michael@0: case VPX_IMG_FMT_YVYU: michael@0: bps = 16; michael@0: break; michael@0: case VPX_IMG_FMT_I420: michael@0: case VPX_IMG_FMT_YV12: michael@0: case VPX_IMG_FMT_VPXI420: michael@0: case VPX_IMG_FMT_VPXYV12: michael@0: bps = 12; michael@0: break; michael@0: default: michael@0: bps = 16; michael@0: break; michael@0: } michael@0: michael@0: /* Get chroma shift values for this format */ michael@0: switch (fmt) { michael@0: case VPX_IMG_FMT_I420: michael@0: case VPX_IMG_FMT_YV12: michael@0: case VPX_IMG_FMT_VPXI420: michael@0: case VPX_IMG_FMT_VPXYV12: michael@0: xcs = 1; michael@0: break; michael@0: default: michael@0: xcs = 0; michael@0: break; michael@0: } michael@0: michael@0: switch (fmt) { michael@0: case VPX_IMG_FMT_I420: michael@0: case VPX_IMG_FMT_YV12: michael@0: case VPX_IMG_FMT_VPXI420: michael@0: case VPX_IMG_FMT_VPXYV12: michael@0: ycs = 1; michael@0: break; michael@0: default: michael@0: ycs = 0; michael@0: break; michael@0: } michael@0: michael@0: /* Calculate storage sizes given the chroma subsampling */ michael@0: align = (1 << xcs) - 1; michael@0: w = (d_w + align) & ~align; michael@0: align = (1 << ycs) - 1; michael@0: h = (d_h + align) & ~align; michael@0: s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8; michael@0: s = (s + stride_align - 1) & ~(stride_align - 1); michael@0: michael@0: /* Allocate the new image */ michael@0: if (!img) { michael@0: img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t)); michael@0: michael@0: if (!img) michael@0: goto fail; michael@0: michael@0: img->self_allocd = 1; michael@0: } else { michael@0: memset(img, 0, sizeof(vpx_image_t)); michael@0: } michael@0: michael@0: img->img_data = img_data; michael@0: michael@0: if (!img_data) { michael@0: img->img_data = img_buf_memalign(buf_align, ((fmt & VPX_IMG_FMT_PLANAR) ? michael@0: h * s * bps / 8 : h * s)); michael@0: img->img_data_owner = 1; michael@0: } michael@0: michael@0: if (!img->img_data) michael@0: goto fail; michael@0: michael@0: img->fmt = fmt; michael@0: img->w = w; michael@0: img->h = h; michael@0: img->x_chroma_shift = xcs; michael@0: img->y_chroma_shift = ycs; michael@0: img->bps = bps; michael@0: michael@0: /* Calculate strides */ michael@0: img->stride[VPX_PLANE_Y] = img->stride[VPX_PLANE_ALPHA] = s; michael@0: img->stride[VPX_PLANE_U] = img->stride[VPX_PLANE_V] = s >> xcs; michael@0: michael@0: /* Default viewport to entire image */ michael@0: if (!vpx_img_set_rect(img, 0, 0, d_w, d_h)) michael@0: return img; michael@0: michael@0: fail: michael@0: vpx_img_free(img); michael@0: return NULL; michael@0: } michael@0: michael@0: vpx_image_t *vpx_img_alloc(vpx_image_t *img, michael@0: vpx_img_fmt_t fmt, michael@0: unsigned int d_w, michael@0: unsigned int d_h, michael@0: unsigned int align) { michael@0: return img_alloc_helper(img, fmt, d_w, d_h, align, align, NULL); michael@0: } michael@0: michael@0: vpx_image_t *vpx_img_wrap(vpx_image_t *img, michael@0: vpx_img_fmt_t fmt, michael@0: unsigned int d_w, michael@0: unsigned int d_h, michael@0: unsigned int stride_align, michael@0: unsigned char *img_data) { michael@0: /* By setting buf_align = 1, we don't change buffer alignment in this michael@0: * function. */ michael@0: return img_alloc_helper(img, fmt, d_w, d_h, 1, stride_align, img_data); michael@0: } michael@0: michael@0: int vpx_img_set_rect(vpx_image_t *img, michael@0: unsigned int x, michael@0: unsigned int y, michael@0: unsigned int w, michael@0: unsigned int h) { michael@0: unsigned char *data; michael@0: michael@0: if (x + w <= img->w && y + h <= img->h) { michael@0: img->d_w = w; michael@0: img->d_h = h; michael@0: michael@0: /* Calculate plane pointers */ michael@0: if (!(img->fmt & VPX_IMG_FMT_PLANAR)) { michael@0: img->planes[VPX_PLANE_PACKED] = michael@0: img->img_data + x * img->bps / 8 + y * img->stride[VPX_PLANE_PACKED]; michael@0: } else { michael@0: data = img->img_data; michael@0: michael@0: if (img->fmt & VPX_IMG_FMT_HAS_ALPHA) { michael@0: img->planes[VPX_PLANE_ALPHA] = michael@0: data + x + y * img->stride[VPX_PLANE_ALPHA]; michael@0: data += img->h * img->stride[VPX_PLANE_ALPHA]; michael@0: } michael@0: michael@0: img->planes[VPX_PLANE_Y] = data + x + y * img->stride[VPX_PLANE_Y]; michael@0: data += img->h * img->stride[VPX_PLANE_Y]; michael@0: michael@0: if (!(img->fmt & VPX_IMG_FMT_UV_FLIP)) { michael@0: img->planes[VPX_PLANE_U] = data michael@0: + (x >> img->x_chroma_shift) michael@0: + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; michael@0: data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; michael@0: img->planes[VPX_PLANE_V] = data michael@0: + (x >> img->x_chroma_shift) michael@0: + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; michael@0: } else { michael@0: img->planes[VPX_PLANE_V] = data michael@0: + (x >> img->x_chroma_shift) michael@0: + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; michael@0: data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; michael@0: img->planes[VPX_PLANE_U] = data michael@0: + (x >> img->x_chroma_shift) michael@0: + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: void vpx_img_flip(vpx_image_t *img) { michael@0: /* Note: In the calculation pointer adjustment calculation, we want the michael@0: * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99 michael@0: * standard indicates that if the adjustment parameter is unsigned, the michael@0: * stride parameter will be promoted to unsigned, causing errors when michael@0: * the lhs is a larger type than the rhs. michael@0: */ michael@0: img->planes[VPX_PLANE_Y] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE_Y]; michael@0: img->stride[VPX_PLANE_Y] = -img->stride[VPX_PLANE_Y]; michael@0: michael@0: img->planes[VPX_PLANE_U] += (signed)((img->d_h >> img->y_chroma_shift) - 1) michael@0: * img->stride[VPX_PLANE_U]; michael@0: img->stride[VPX_PLANE_U] = -img->stride[VPX_PLANE_U]; michael@0: michael@0: img->planes[VPX_PLANE_V] += (signed)((img->d_h >> img->y_chroma_shift) - 1) michael@0: * img->stride[VPX_PLANE_V]; michael@0: img->stride[VPX_PLANE_V] = -img->stride[VPX_PLANE_V]; michael@0: michael@0: img->planes[VPX_PLANE_ALPHA] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE_ALPHA]; michael@0: img->stride[VPX_PLANE_ALPHA] = -img->stride[VPX_PLANE_ALPHA]; michael@0: } michael@0: michael@0: void vpx_img_free(vpx_image_t *img) { michael@0: if (img) { michael@0: if (img->img_data && img->img_data_owner) michael@0: img_buf_free(img->img_data); michael@0: michael@0: if (img->self_allocd) michael@0: free(img); michael@0: } michael@0: }