[FFmpeg-devel] [PATCH 10/10] Add av_image_alloc() and use it in avfilter_default_get_video_buffer().
Stefano Sabatini
stefano.sabatini-lala
Fri Nov 5 15:17:41 CET 2010
---
libavcore/imgutils.c | 25 +++++++++++++++++++++++++
libavcore/imgutils.h | 11 +++++++++++
libavfilter/defaults.c | 16 ++--------------
3 files changed, 38 insertions(+), 14 deletions(-)
diff --git a/libavcore/imgutils.c b/libavcore/imgutils.c
index 0a21f6d..8ef3dc1 100644
--- a/libavcore/imgutils.c
+++ b/libavcore/imgutils.c
@@ -120,6 +120,31 @@ int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int heigh
return total_size;
}
+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 + 16); // +2 is needed for swscaler, +16 to be SIMD-friendly
+ if (!buf)
+ return AVERROR(ENOMEM);
+ if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
+ av_free(buf);
+ return ret;
+ }
+
+ return ret;
+}
+
typedef struct ImgUtils {
const AVClass *class;
int log_offset;
diff --git a/libavcore/imgutils.h b/libavcore/imgutils.h
index 8458fc6..ab7b861 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 in pointers an image with size w and h and pixel format
+ * pix_fmt, and fill the linesizes in linesizes.
+ *
+ * @param align the value to use for aligning linesizes
+ * @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..929118f 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,15 @@ 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);
+ 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
More information about the ffmpeg-devel
mailing list