shithub: libvpx

Download patch

ref: b088998e5d538e99d5d9f461482b0a87c95d5931
parent: 119decdee7f3052b5f0a6bc3f3a6e84d157054c9
author: James Zern <jzern@google.com>
date: Thu Jul 11 19:01:26 EDT 2013

vp[89]_dx_iface: factorize vp8_mmap_*()

s/vp8/vpx/ -> vpx_codec_internal.h / vpx_codec.c

Change-Id: If4192b40206276a761b01d44e334fe15bcb81128

--- a/vp8/vp8_dx_iface.c
+++ b/vp8/vp8_dx_iface.c
@@ -41,15 +41,6 @@
 
 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t);
 
-typedef struct
-{
-    unsigned int   id;
-    unsigned long  sz;
-    unsigned int   align;
-    unsigned int   flags;
-    unsigned long(*calc_sz)(const vpx_codec_dec_cfg_t *, vpx_codec_flags_t);
-} mem_req_t;
-
 static const mem_req_t vp8_mem_req_segs[] =
 {
     {VP8_SEG_ALG_PRIV,    0, 8, VPX_CODEC_MEM_ZERO, vp8_priv_sz},
@@ -93,65 +84,6 @@
     return sizeof(vpx_codec_alg_priv_t);
 }
 
-
-static void vp8_mmap_dtor(vpx_codec_mmap_t *mmap)
-{
-    free(mmap->priv);
-}
-
-static vpx_codec_err_t vp8_mmap_alloc(vpx_codec_mmap_t *mmap)
-{
-    vpx_codec_err_t  res;
-    unsigned int   align;
-
-    align = mmap->align ? mmap->align - 1 : 0;
-
-    if (mmap->flags & VPX_CODEC_MEM_ZERO)
-        mmap->priv = calloc(1, mmap->sz + align);
-    else
-        mmap->priv = malloc(mmap->sz + align);
-
-    res = (mmap->priv) ? VPX_CODEC_OK : VPX_CODEC_MEM_ERROR;
-    mmap->base = (void *)((((uintptr_t)mmap->priv) + align) & ~(uintptr_t)align);
-    mmap->dtor = vp8_mmap_dtor;
-    return res;
-}
-
-static vpx_codec_err_t vp8_validate_mmaps(const vp8_stream_info_t *si,
-        const vpx_codec_mmap_t        *mmaps,
-        vpx_codec_flags_t              init_flags)
-{
-    int i;
-    vpx_codec_err_t res = VPX_CODEC_OK;
-
-    for (i = 0; i < NELEMENTS(vp8_mem_req_segs) - 1; i++)
-    {
-        /* Ensure the segment has been allocated */
-        if (!mmaps[i].base)
-        {
-            res = VPX_CODEC_MEM_ERROR;
-            break;
-        }
-
-        /* Verify variable size segment is big enough for the current si. */
-        if (vp8_mem_req_segs[i].calc_sz)
-        {
-            vpx_codec_dec_cfg_t cfg;
-
-            cfg.w = si->w;
-            cfg.h = si->h;
-
-            if (mmaps[i].sz < vp8_mem_req_segs[i].calc_sz(&cfg, init_flags))
-            {
-                res = VPX_CODEC_MEM_ERROR;
-                break;
-            }
-        }
-    }
-
-    return res;
-}
-
 static void vp8_init_ctx(vpx_codec_ctx_t *ctx, const vpx_codec_mmap_t *mmap)
 {
     int i;
@@ -214,7 +146,7 @@
         mmap.align = vp8_mem_req_segs[0].align;
         mmap.flags = vp8_mem_req_segs[0].flags;
 
-        res = vp8_mmap_alloc(&mmap);
+        res = vpx_mmap_alloc(&mmap);
         if (res != VPX_CODEC_OK) return res;
 
         vp8_init_ctx(ctx, &mmap);
@@ -488,7 +420,7 @@
                 ctx->mmaps[i].sz = vp8_mem_req_segs[i].calc_sz(&cfg,
                                    ctx->base.init_flags);
 
-            res = vp8_mmap_alloc(&ctx->mmaps[i]);
+            res = vpx_mmap_alloc(&ctx->mmaps[i]);
         }
 
         if (!res)
@@ -500,7 +432,9 @@
     /* Initialize the decoder instance on the first frame*/
     if (!res && !ctx->decoder_init)
     {
-        res = vp8_validate_mmaps(&ctx->si, ctx->mmaps, ctx->base.init_flags);
+        res = vpx_validate_mmaps(&ctx->si, ctx->mmaps,
+                                 vp8_mem_req_segs, NELEMENTS(vp8_mem_req_segs),
+                                 ctx->base.init_flags);
 
         if (!res)
         {
--- a/vp9/vp9_dx_iface.c
+++ b/vp9/vp9_dx_iface.c
@@ -31,14 +31,6 @@
 
 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t);
 
-typedef struct {
-  unsigned int   id;
-  unsigned long  sz;
-  unsigned int   align;
-  unsigned int   flags;
-  unsigned long(*calc_sz)(const vpx_codec_dec_cfg_t *, vpx_codec_flags_t);
-} mem_req_t;
-
 static const mem_req_t vp8_mem_req_segs[] = {
   {VP8_SEG_ALG_PRIV,    0, 8, VPX_CODEC_MEM_ZERO, vp8_priv_sz},
   {VP8_SEG_MAX, 0, 0, 0, NULL}
@@ -78,58 +70,6 @@
   return sizeof(vpx_codec_alg_priv_t);
 }
 
-
-static void vp8_mmap_dtor(vpx_codec_mmap_t *mmap) {
-  free(mmap->priv);
-}
-
-static vpx_codec_err_t vp8_mmap_alloc(vpx_codec_mmap_t *mmap) {
-  vpx_codec_err_t  res;
-  unsigned int   align;
-
-  align = mmap->align ? mmap->align - 1 : 0;
-
-  if (mmap->flags & VPX_CODEC_MEM_ZERO)
-    mmap->priv = calloc(1, mmap->sz + align);
-  else
-    mmap->priv = malloc(mmap->sz + align);
-
-  res = (mmap->priv) ? VPX_CODEC_OK : VPX_CODEC_MEM_ERROR;
-  mmap->base = (void *)((((uintptr_t)mmap->priv) + align) & ~(uintptr_t)align);
-  mmap->dtor = vp8_mmap_dtor;
-  return res;
-}
-
-static vpx_codec_err_t vp8_validate_mmaps(const vp8_stream_info_t *si,
-                                          const vpx_codec_mmap_t *mmaps,
-                                          vpx_codec_flags_t init_flags) {
-  int i;
-  vpx_codec_err_t res = VPX_CODEC_OK;
-
-  for (i = 0; i < NELEMENTS(vp8_mem_req_segs) - 1; i++) {
-    /* Ensure the segment has been allocated */
-    if (!mmaps[i].base) {
-      res = VPX_CODEC_MEM_ERROR;
-      break;
-    }
-
-    /* Verify variable size segment is big enough for the current si. */
-    if (vp8_mem_req_segs[i].calc_sz) {
-      vpx_codec_dec_cfg_t cfg;
-
-      cfg.w = si->w;
-      cfg.h = si->h;
-
-      if (mmaps[i].sz < vp8_mem_req_segs[i].calc_sz(&cfg, init_flags)) {
-        res = VPX_CODEC_MEM_ERROR;
-        break;
-      }
-    }
-  }
-
-  return res;
-}
-
 static void vp8_init_ctx(vpx_codec_ctx_t *ctx, const vpx_codec_mmap_t *mmap) {
   int i;
 
@@ -181,7 +121,7 @@
     mmap.align = vp8_mem_req_segs[0].align;
     mmap.flags = vp8_mem_req_segs[0].flags;
 
-    res = vp8_mmap_alloc(&mmap);
+    res = vpx_mmap_alloc(&mmap);
 
     if (!res) {
       vp8_init_ctx(ctx, &mmap);
@@ -303,7 +243,7 @@
         ctx->mmaps[i].sz = vp8_mem_req_segs[i].calc_sz(&cfg,
                                                        ctx->base.init_flags);
 
-      res = vp8_mmap_alloc(&ctx->mmaps[i]);
+      res = vpx_mmap_alloc(&ctx->mmaps[i]);
     }
 
     if (!res)
@@ -314,7 +254,9 @@
 
   /* Initialize the decoder instance on the first frame*/
   if (!res && !ctx->decoder_init) {
-    res = vp8_validate_mmaps(&ctx->si, ctx->mmaps, ctx->base.init_flags);
+    res = vpx_validate_mmaps(&ctx->si, ctx->mmaps,
+                             vp8_mem_req_segs, NELEMENTS(vp8_mem_req_segs),
+                             ctx->base.init_flags);
 
     if (!res) {
       VP9D_CONFIG oxcf;
--- a/vpx/internal/vpx_codec_internal.h
+++ b/vpx/internal/vpx_codec_internal.h
@@ -474,4 +474,30 @@
   if (info->setjmp)
     longjmp(info->jmp, info->error_code);
 }
+
+//------------------------------------------------------------------------------
+// mmap interface
+
+typedef struct {
+  unsigned int   id;
+  unsigned long  sz;
+  unsigned int   align;
+  unsigned int   flags;
+  unsigned long (*calc_sz)(const vpx_codec_dec_cfg_t *, vpx_codec_flags_t);
+} mem_req_t;
+
+// Allocates mmap.priv and sets mmap.base based on mmap.sz/align/flags
+// requirements.
+// Returns #VPX_CODEC_OK on success, #VPX_CODEC_MEM_ERROR otherwise.
+vpx_codec_err_t vpx_mmap_alloc(vpx_codec_mmap_t *mmap);
+
+// Frees mmap.base allocated by a call to vpx_mmap_alloc().
+void vpx_mmap_dtor(vpx_codec_mmap_t *mmap);
+
+// Checks each mmap has the size requirement specificied by mem_reqs.
+// Returns #VPX_CODEC_OK on success, #VPX_CODEC_MEM_ERROR otherwise.
+vpx_codec_err_t vpx_validate_mmaps(const vpx_codec_stream_info_t *si,
+                                   const vpx_codec_mmap_t *mmaps,
+                                   const mem_req_t *mem_reqs, int nreqs,
+                                   vpx_codec_flags_t init_flags);
 #endif
--- a/vpx/src/vpx_codec.c
+++ b/vpx/src/vpx_codec.c
@@ -14,6 +14,7 @@
  *
  */
 #include <stdarg.h>
+#include <stdlib.h>
 #include "vpx/vpx_integer.h"
 #include "vpx/internal/vpx_codec_internal.h"
 #include "vpx_version.h"
@@ -132,4 +133,52 @@
   }
 
   return SAVE_STATUS(ctx, res);
+}
+
+//------------------------------------------------------------------------------
+// mmap interface
+
+vpx_codec_err_t vpx_mmap_alloc(vpx_codec_mmap_t *mmap) {
+  unsigned int align = mmap->align ? mmap->align - 1 : 0;
+
+  if (mmap->flags & VPX_CODEC_MEM_ZERO)
+    mmap->priv = calloc(1, mmap->sz + align);
+  else
+    mmap->priv = malloc(mmap->sz + align);
+
+  if (mmap->priv == NULL) return VPX_CODEC_MEM_ERROR;
+  mmap->base = (void *)((((uintptr_t)mmap->priv) + align) & ~(uintptr_t)align);
+  mmap->dtor = vpx_mmap_dtor;
+  return VPX_CODEC_OK;
+}
+
+void vpx_mmap_dtor(vpx_codec_mmap_t *mmap) {
+  free(mmap->priv);
+}
+
+vpx_codec_err_t vpx_validate_mmaps(const vpx_codec_stream_info_t *si,
+                                   const vpx_codec_mmap_t *mmaps,
+                                   const mem_req_t *mem_reqs, int nreqs,
+                                   vpx_codec_flags_t init_flags) {
+  int i;
+
+  for (i = 0; i < nreqs - 1; ++i) {
+    /* Ensure the segment has been allocated */
+    if (mmaps[i].base == NULL) {
+      return VPX_CODEC_MEM_ERROR;
+    }
+
+    /* Verify variable size segment is big enough for the current si. */
+    if (mem_reqs[i].calc_sz != NULL) {
+      vpx_codec_dec_cfg_t cfg;
+
+      cfg.w = si->w;
+      cfg.h = si->h;
+
+      if (mmaps[i].sz < mem_reqs[i].calc_sz(&cfg, init_flags)) {
+        return VPX_CODEC_MEM_ERROR;
+      }
+    }
+  }
+  return VPX_CODEC_OK;
 }