lavfi: add a new function av_buffersrc_buffer().
[ffmpeg.git] / libavfilter / vsrc_buffer.c
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 = {