[PATCH 4/9] 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   |   27 +++++++++++++++++++++++++++
 libavcore/imgutils.h   |   11 +++++++++++
 libavfilter/defaults.c |   17 +++--------------
 3 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/libavcore/imgutils.c b/libavcore/imgutils.c
index 554639f..a2c3c7d 100644
--- a/libavcore/imgutils.c
+++ b/libavcore/imgutils.c
@@ -161,6 +161,33 @@ 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_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..15ba3ce 100644
--- a/libavcore/imgutils.h
+++ b/libavcore/imgutils.h
@@ -78,6 +78,17 @@ 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.
+ *
+ * @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 5462b1a..61f1e51 100644
--- a/libavfilter/defaults.c
+++ b/libavfilter/defaults.c
@@ -38,8 +38,6 @@ AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int per
 {
     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
     AVFilterBufferRef *ref = NULL;
-    int i, tempsize;
-    char *buf = NULL;
 
     if (!pic || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
         goto fail;
@@ -55,25 +53,16 @@ AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int per
     pic->refcount = 1;
     ref->format   = link->format;
     pic->free     = avfilter_default_free_buffer;
-    av_image_fill_linesizes(pic->linesize, ref->format, ref->video->w);
-
-    for (i = 0; i < 4; i++)
-        pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
-
-    tempsize = av_image_fill_pointers(pic->data, ref->format, ref->video->h, NULL, pic->linesize);
-    buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
-                                    // SIMD-friendly
-    if (!buf)
-        goto fail;
-    av_image_fill_pointers(pic->data, ref->format, ref->video->h, buf, pic->linesize);
 
+    // +2 is needed for swscaler, +16 to be SIMD-friendly
+    if (av_image_alloc(pic->data, pic->linesize, w, h, ref->format, 16) < 0)
+        return NULL;
     memcpy(ref->data,     pic->data,     sizeof(ref->data));
     memcpy(ref->linesize, pic->linesize, sizeof(ref->linesize));
 
     return ref;
 
 fail:
-    av_free(buf);
     if (ref && ref->video)
         av_free(ref->video);
     av_free(ref);
-- 
1.7.1


--aT9PWwzfKXlsBJM1
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment; filename="0005-Use-av_image_alloc-in-avpicture_alloc-simplify.patch"




More information about the ffmpeg-devel mailing list