[PATCH] Implement av_image_alloc() and use it in avfilter_default_get_video_buffer().

Stefano Sabatini stefano.sabatini-lala
Sun Nov 7 16:51:50 CET 2010


---
 libavcore/imgutils.c   |   29 +++++++++++++++++++++++++++++
 libavcore/imgutils.h   |   13 +++++++++++++
 libavfilter/defaults.c |   16 ++++------------
 3 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/libavcore/imgutils.c b/libavcore/imgutils.c
index a2adaa6..c096e00 100644
--- a/libavcore/imgutils.c
+++ b/libavcore/imgutils.c
@@ -176,6 +176,35 @@ int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
     return 0;
 }
 
+int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
+                   int w, int h, enum PixelFormat pix_fmt, int align)
+{
+    int i, ret;
+    uint8_t *buf;
+
+    if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
+        return ret;
+    if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
+        return ret;
+
+    for (i = 0; i < 4; i++)
+        linesizes[i] = FFALIGN(linesizes[i], align);
+
+    if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
+        return ret;
+    buf = av_malloc(ret + align);
+    if (!buf)
+        return AVERROR(ENOMEM);
+    if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
+        av_free(buf);
+        return ret;
+    }
+    if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL)
+        ff_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
+
+    return ret;
+}
+
 typedef struct ImgUtils {
     const AVClass *class;
     int   log_offset;
diff --git a/libavcore/imgutils.h b/libavcore/imgutils.h
index 8458fc6..6c39d53 100644
--- a/libavcore/imgutils.h
+++ b/libavcore/imgutils.h
@@ -78,6 +78,19 @@ int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int heigh
                            uint8_t *ptr, const int linesizes[4]);
 
 /**
+ * Allocate an image with size w and h and pixel format pix_fmt, and
+ * fill pointers and linesizes accordingly.
+ * The allocated image buffer has to be freed by using
+ * av_freep(&pointers[0]).
+ *
+ * @param align the value to use for buffer size alignment
+ * @return the size in bytes required for the image buffer, a negative
+ * error code in case of failure
+ */
+int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
+                   int w, int h, enum PixelFormat pix_fmt, int align);
+
+/**
  * Copy image plane from src to dst.
  * That is, copy "height" number of lines of "bytewidth" bytes each.
  * The first byte of each successive line is separated by *_linesize
diff --git a/libavfilter/defaults.c b/libavfilter/defaults.c
index 44262ca..401bcb2 100644
--- a/libavfilter/defaults.c
+++ b/libavfilter/defaults.c
@@ -37,26 +37,18 @@ void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
  * alloc & free cycle currently implemented. */
 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
 {
-    char *buf = NULL;
-    int linesize[4], i, tempsize;
+    int linesize[4];
     uint8_t *data[4];
     AVFilterBufferRef *picref = NULL;
 
-    av_image_fill_linesizes(linesize, link->format, w);
-    for (i = 0; i < 4; i++)
-        linesize[i] = FFALIGN(linesize[i], 16);
-    tempsize = av_image_fill_pointers(data, link->format, h, NULL, linesize);
-    buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
-                                    // SIMD-friendly
-    if (!buf)
+    // +2 is needed for swscaler, +16 to be SIMD-friendly
+    if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
         return NULL;
 
-    av_image_fill_pointers(data, link->format, h, buf, linesize);
-
     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
                                                        perms, w, h, link->format);
     if (!picref) {
-        av_free(buf);
+        av_free(data[0]);
         return NULL;
     }
 
-- 
1.7.1


--7JfCtLOvnd9MIVvH--



More information about the ffmpeg-devel mailing list