lavfi: add a new function av_buffersrc_buffer().
authorAnton Khirnov <anton@khirnov.net>
Wed, 21 Dec 2011 19:39:18 +0000 (20:39 +0100)
committerAnton Khirnov <anton@khirnov.net>
Sun, 25 Dec 2011 15:18:57 +0000 (16:18 +0100)
It can be used to directly pass a AVFilterBufferRef to lavfi, avoiding a
memcpy.

libavfilter/Makefile
libavfilter/buffersrc.h [new file with mode: 0644]
libavfilter/vsrc_buffer.c

index c269db5..78c4692 100644 (file)
@@ -3,7 +3,7 @@ FFLIBS = avutil
 FFLIBS-$(CONFIG_MOVIE_FILTER) += avformat avcodec
 FFLIBS-$(CONFIG_SCALE_FILTER) += swscale
 
-HEADERS = avfilter.h avfiltergraph.h vsrc_buffer.h
+HEADERS = avfilter.h avfiltergraph.h buffersrc.h vsrc_buffer.h
 
 OBJS = allfilters.o                                                     \
        avfilter.o                                                       \
diff --git a/libavfilter/buffersrc.h b/libavfilter/buffersrc.h
new file mode 100644 (file)
index 0000000..bd82c06
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVFILTER_BUFFERSRC_H
+#define AVFILTER_BUFFERSRC_H
+
+/**
+ * @file
+ * Memory buffer source API.
+ */
+
+#include "avfilter.h"
+
+/**
+ * Add a buffer to the filtergraph s.
+ *
+ * @param buf buffer containing frame data to be passed down the filtergraph.
+ * This function will take ownership of buf, the user must not free it.
+ */
+int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf);
+
+#endif /* AVFILTER_BUFFERSRC_H */
index 982fed5..7ef19a1 100644 (file)
  */
 
 #include "avfilter.h"
+#include "buffersrc.h"
 #include "vsrc_buffer.h"
 #include "libavutil/imgutils.h"
 
 typedef struct {
-    int64_t           pts;
-    AVFrame           frame;
-    int               has_frame;
+    AVFilterBufferRef *buf;
     int               h, w;
     enum PixelFormat  pix_fmt;
     AVRational        time_base;     ///< time_base to set in the output link
@@ -42,7 +41,7 @@ int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
 {
     BufferSourceContext *c = buffer_filter->priv;
 
-    if (c->has_frame) {
+    if (c->buf) {
         av_log(buffer_filter, AV_LOG_ERROR,
                "Buffering several frames is not supported. "
                "Please consume all available frames before adding a new one.\n"
@@ -50,15 +49,31 @@ int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
         //return -1;
     }
 
-    memcpy(c->frame.data    , frame->data    , sizeof(frame->data));
-    memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
-    c->frame.interlaced_frame= frame->interlaced_frame;
-    c->frame.top_field_first = frame->top_field_first;
-    c->frame.key_frame = frame->key_frame;
-    c->frame.pict_type = frame->pict_type;
-    c->pts = pts;
-    c->pixel_aspect = pixel_aspect;
-    c->has_frame = 1;
+    c->buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
+                                       c->w, c->h);
+    av_image_copy(c->buf->data, c->buf->linesize, frame->data, frame->linesize,
+                  c->pix_fmt, c->w, c->h);
+
+    avfilter_copy_frame_props(c->buf, frame);
+    c->buf->pts                    = pts;
+    c->buf->video->pixel_aspect    = pixel_aspect;
+
+    return 0;
+}
+
+int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
+{
+    BufferSourceContext *c = s->priv;
+
+    if (c->buf) {
+        av_log(s, AV_LOG_ERROR,
+               "Buffering several frames is not supported. "
+               "Please consume all available frames before adding a new one.\n"
+            );
+        return AVERROR(EINVAL);
+    }
+
+    c->buf = buf;
 
     return 0;
 }
@@ -113,33 +128,18 @@ static int config_props(AVFilterLink *link)
 static int request_frame(AVFilterLink *link)
 {
     BufferSourceContext *c = link->src->priv;
-    AVFilterBufferRef *picref;
 
-    if (!c->has_frame) {
+    if (!c->buf) {
         av_log(link->src, AV_LOG_ERROR,
                "request_frame() called with no available frame!\n");
         //return -1;
     }
 
-    /* This picture will be needed unmodified later for decoding the next
-     * frame */
-    picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
-                                       AV_PERM_REUSE2,
-                                       link->w, link->h);
-
-    av_image_copy(picref->data, picref->linesize,
-                  c->frame.data, c->frame.linesize,
-                  picref->format, link->w, link->h);
-
-    avfilter_copy_frame_props(picref, &c->frame);
-    picref->pts                    = c->pts;
-    picref->video->pixel_aspect    = c->pixel_aspect;
-    avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
+    avfilter_start_frame(link, avfilter_ref_buffer(c->buf, ~0));
     avfilter_draw_slice(link, 0, link->h, 1);
     avfilter_end_frame(link);
-    avfilter_unref_buffer(picref);
-
-    c->has_frame = 0;
+    avfilter_unref_buffer(c->buf);
+    c->buf = NULL;
 
     return 0;
 }
@@ -147,7 +147,7 @@ static int request_frame(AVFilterLink *link)
 static int poll_frame(AVFilterLink *link)
 {
     BufferSourceContext *c = link->src->priv;
-    return !!(c->has_frame);
+    return !!(c->buf);
 }
 
 AVFilter avfilter_vsrc_buffer = {