summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYunqing Wang <yunqingwang@google.com>2011-12-06 10:21:28 -0800
committerGerrit Code Review <gerrit@gerrit.golo.chromium.org>2011-12-06 10:21:28 -0800
commit254889cdfc64de8d042117c3dfeb9f83fee736bd (patch)
tree07e9c837d981eaa78991780f240e28380329212e
parentaa7335e610b961626f77130bc99b24de1031601d (diff)
parentbe5bbc96e8105ccea91f0dca940b7f8d67f8b0c1 (diff)
downloadlibvpx-254889cdfc64de8d042117c3dfeb9f83fee736bd.tar
libvpx-254889cdfc64de8d042117c3dfeb9f83fee736bd.tar.gz
libvpx-254889cdfc64de8d042117c3dfeb9f83fee736bd.tar.bz2
libvpx-254889cdfc64de8d042117c3dfeb9f83fee736bd.zip
Merge "Allow aligning the raw image buffer"
-rw-r--r--vpx/src/vpx_image.c53
-rw-r--r--vpx/vpx_image.h3
2 files changed, 50 insertions, 6 deletions
diff --git a/vpx/src/vpx_image.c b/vpx/src/vpx_image.c
index 7a4e27062..336b6e29d 100644
--- a/vpx/src/vpx_image.c
+++ b/vpx/src/vpx_image.c
@@ -13,10 +13,42 @@
#include <string.h>
#include "vpx/vpx_image.h"
+#define ADDRESS_STORAGE_SIZE sizeof(size_t)
+/*returns an addr aligned to the byte boundary specified by align*/
+#define align_addr(addr,align) (void*)(((size_t)(addr) + ((align) - 1)) & (size_t)-(align))
+
+/* Memalign code is copied from vpx_mem.c */
+static void *img_buf_memalign(size_t align, size_t size)
+{
+ void *addr,
+ * x = NULL;
+
+ addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
+
+ if (addr)
+ {
+ x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
+ /* save the actual malloc address */
+ ((size_t *)x)[-1] = (size_t)addr;
+ }
+
+ return x;
+}
+
+static void img_buf_free(void *memblk)
+{
+ if (memblk)
+ {
+ void *addr = (void *)(((size_t *)memblk)[-1]);
+ free(addr);
+ }
+}
+
static vpx_image_t *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)
{
@@ -25,6 +57,14 @@ static vpx_image_t *img_alloc_helper(vpx_image_t *img,
int align;
/* Treat align==0 like align==1 */
+ if (!buf_align)
+ buf_align = 1;
+
+ /* Validate alignment (must be power of 2) */
+ if (buf_align & (buf_align - 1))
+ goto fail;
+
+ /* Treat align==0 like align==1 */
if (!stride_align)
stride_align = 1;
@@ -119,7 +159,8 @@ static vpx_image_t *img_alloc_helper(vpx_image_t *img,
if (!img_data)
{
- img->img_data = malloc((fmt & VPX_IMG_FMT_PLANAR) ? h * w * bps / 8 : h * s);
+ img->img_data = img_buf_memalign(buf_align, ((fmt & VPX_IMG_FMT_PLANAR)?
+ h * s * bps / 8 : h * s));
img->img_data_owner = 1;
}
@@ -150,9 +191,9 @@ vpx_image_t *vpx_img_alloc(vpx_image_t *img,
vpx_img_fmt_t fmt,
unsigned int d_w,
unsigned int d_h,
- unsigned int stride_align)
+ unsigned int align)
{
- return img_alloc_helper(img, fmt, d_w, d_h, stride_align, NULL);
+ return img_alloc_helper(img, fmt, d_w, d_h, align, align, NULL);
}
vpx_image_t *vpx_img_wrap(vpx_image_t *img,
@@ -162,7 +203,9 @@ vpx_image_t *vpx_img_wrap(vpx_image_t *img,
unsigned int stride_align,
unsigned char *img_data)
{
- return img_alloc_helper(img, fmt, d_w, d_h, stride_align, img_data);
+ /* By setting buf_align = 1, we don't change buffer alignment in this
+ * function. */
+ return img_alloc_helper(img, fmt, d_w, d_h, 1, stride_align, img_data);
}
int vpx_img_set_rect(vpx_image_t *img,
@@ -254,7 +297,7 @@ void vpx_img_free(vpx_image_t *img)
if (img)
{
if (img->img_data && img->img_data_owner)
- free(img->img_data);
+ img_buf_free(img->img_data);
if (img->self_allocd)
free(img);
diff --git a/vpx/vpx_image.h b/vpx/vpx_image.h
index 8e08b3642..3e424470f 100644
--- a/vpx/vpx_image.h
+++ b/vpx/vpx_image.h
@@ -160,7 +160,8 @@ extern "C" {
* \param[in] fmt Format for the image
* \param[in] d_w Width of the image
* \param[in] d_h Height of the image
- * \param[in] align Alignment, in bytes, of each row in the image.
+ * \param[in] align Alignment, in bytes, of the image buffer and
+ * each row in the image(stride).
*
* \return Returns a pointer to the initialized image descriptor. If the img
* parameter is non-null, the value of the img parameter will be