[FFmpeg-devel] [PATCH] vsrc_buffer: simplify interface

Stefano Sabatini stefano.sabatini-lala at poste.it
Mon May 2 02:18:21 CEST 2011


---
 ffmpeg.c                  |   18 ++++++++---
 libavfilter/vsrc_buffer.c |   68 ++++++++++++---------------------------------
 libavfilter/vsrc_buffer.h |    7 +---
 3 files changed, 33 insertions(+), 60 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index e36b3e5..98fdbae 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -42,6 +42,7 @@
 #include "libavutil/colorspace.h"
 #include "libavutil/fifo.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/imgutils.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/avstring.h"
 #include "libavutil/libm.h"
@@ -1632,13 +1633,20 @@ static int output_packet(AVInputStream *ist, int ist_index,
             for(i=0;i<nb_ostreams;i++) {
                 ost = ost_table[i];
                 if (ost->input_video_filter && ost->source_index == ist_index) {
+                    // fetch a frame from the filterchain, and buffer it
+                    AVFilterLink *inlink = ost->input_video_filter->outputs[0];
+                    AVFilterBufferRef *picref =
+                        avfilter_get_video_buffer(inlink, AV_PERM_WRITE, picture.width, picture.height);
+
                     if (!picture.sample_aspect_ratio.num)
                         picture.sample_aspect_ratio = ist->st->sample_aspect_ratio;
-                    // add it to be filtered
-                    av_vsrc_buffer_add_frame2(ost->input_video_filter, &picture,
-                                             ist->pts,
-                                             ist->st->codec->width, ist->st->codec->height,
-                                             ist->st->codec->pix_fmt, ""); //TODO user setable params
+                    av_image_copy(picref->data, picref->linesize,
+                                  picture.data, picture.linesize,
+                                  picref->format, picref->video->w, picref->video->h);
+                    avfilter_copy_frame_props(picref, &picture);
+                    picref->pts = ist->pts;
+
+                    av_vsrc_buffer_add_frame2(ost->input_video_filter, picref, "");  //TODO user setable params
                 }
             }
         }
diff --git a/libavfilter/vsrc_buffer.c b/libavfilter/vsrc_buffer.c
index 7eb291a..fe6b4e1 100644
--- a/libavfilter/vsrc_buffer.c
+++ b/libavfilter/vsrc_buffer.c
@@ -29,25 +29,21 @@
 #include "libavutil/imgutils.h"
 
 typedef struct {
-    int64_t           pts;
-    AVFrame           frame;
-    int               has_frame;
     int               h, w;
     enum PixelFormat  pix_fmt;
     AVRational        time_base;     ///< time_base to set in the output link
     AVRational        sample_aspect_ratio;
     char              sws_param[256];
+    AVFilterBufferRef *picref;
 } BufferSourceContext;
 
-int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
-                              int64_t pts, int width,
-                              int height, enum PixelFormat  pix_fmt,
+int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFilterBufferRef *picref,
                               const char *sws_param)
 {
     BufferSourceContext *c = buffer_filter->priv;
     int ret;
 
-    if (c->has_frame) {
+    if (c->picref) {
         av_log(buffer_filter, AV_LOG_ERROR,
                "Buffering several frames is not supported. "
                "Please consume all available frames before adding a new one.\n"
@@ -59,12 +55,14 @@ int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
         snprintf(c->sws_param, 255, "%d:%d:%s", c->w, c->h, sws_param);
     }
 
-    if(width != c->w || height != c->h || pix_fmt != c->pix_fmt){
+    if (picref->video->w != c->w || picref->video->h != c->h ||
+        picref->format != c->pix_fmt) {
         AVFilterContext *scale= buffer_filter->outputs[0]->dst;
         AVFilterLink *link;
 
         av_log(buffer_filter, AV_LOG_INFO, "Changing filter graph input to accept %dx%d %d (%d %d)\n",
-               width,height,pix_fmt, c->pix_fmt, scale && scale->outputs ? scale->outputs[0]->format : -123);
+               picref->video->w, picref->video->h, picref->format,
+               c->pix_fmt, scale && scale->outputs ? scale->outputs[0]->format : -123);
 
         if(!scale || strcmp(scale->filter->name,"scale")){
             AVFilter *f= avfilter_get_by_name("scale");
@@ -90,39 +88,23 @@ int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
             scale->filter->init(scale, c->sws_param, NULL);
         }
 
-        c->pix_fmt= scale->inputs[0]->format= pix_fmt;
-        c->w= scale->inputs[0]->w= width;
-        c->h= scale->inputs[0]->h= height;
+        c->pix_fmt = scale->inputs[0]->format = picref->format;
+        c->w = scale->inputs[0]->w = picref->video->w;
+        c->h = scale->inputs[0]->h = picref->video->h;
 
         link= scale->outputs[0];
         if ((ret =  link->srcpad->config_props(link)) < 0)
             return ret;
     }
 
-    memcpy(c->frame.data    , frame->data    , sizeof(frame->data));
-    memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
-    c->frame.width  = frame->width;
-    c->frame.height = frame->height;
-    c->frame.format = frame->format;
-    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->frame.sample_aspect_ratio = frame->sample_aspect_ratio;
-    c->pts = pts;
-    c->has_frame = 1;
+    c->picref = picref;
 
     return 0;
 }
 
-int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
-                             int64_t pts)
+int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter,  AVFilterBufferRef *picref)
 {
-    BufferSourceContext *c = buffer_filter->priv;
-
-    return av_vsrc_buffer_add_frame2(buffer_filter, frame,
-                              pts, c->w,
-                              c->h, c->pix_fmt, "");
+    return av_vsrc_buffer_add_frame2(buffer_filter, picref, "");
 }
 
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
@@ -175,32 +157,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->picref) {
         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;
-
-    avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
+    avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~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->picref);
+    c->picref = NULL;
 
     return 0;
 }
@@ -208,7 +176,7 @@ static int request_frame(AVFilterLink *link)
 static int poll_frame(AVFilterLink *link)
 {
     BufferSourceContext *c = link->src->priv;
-    return !!(c->has_frame);
+    return !!(c->picref);
 }
 
 AVFilter avfilter_vsrc_buffer = {
diff --git a/libavfilter/vsrc_buffer.h b/libavfilter/vsrc_buffer.h
index 68f7847..59adf73 100644
--- a/libavfilter/vsrc_buffer.h
+++ b/libavfilter/vsrc_buffer.h
@@ -30,12 +30,9 @@
 #include "libavcodec/avcodec.h" /* AVFrame */
 #include "avfilter.h"
 
-int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
-                             int64_t pts);
+int av_vsrc_buffer_add_frame(AVFilterContext *bufsrc, AVFilterBufferRef *picref);
 
-int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
-                              int64_t pts, int width,
-                              int height, enum PixelFormat  pix_fmt,
+int av_vsrc_buffer_add_frame2(AVFilterContext *bufsrc,  AVFilterBufferRef *picref,
                               const char *sws_param);
 
 #endif /* AVFILTER_VSRC_BUFFER_H */
-- 
1.7.2.3



More information about the ffmpeg-devel mailing list