[FFmpeg-devel] [PATCH] lavfi: add avfilter_copy_video_buffer_ref_props_from_frame()

Stefano Sabatini stefano.sabatini-lala at poste.it
Tue Apr 26 16:41:31 CEST 2011


Allow to factorize code, also more robust.

Signed-off-by: Stefano Sabatini <stefano.sabatini-lala at poste.it>
---
 ffmpeg.c                 |   15 ++++++---------
 ffplay.c                 |    5 +++--
 libavfilter/avfilter.h   |   20 ++++++++++++++++++++
 libavfilter/vsrc_movie.c |   16 +++++-----------
 4 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 62a2e74..51e392c 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -1632,21 +1632,18 @@ 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) {
-                    AVRational sar;
                     AVFilterLink *inlink = ost->input_video_filter->outputs[0];
-                    AVFilterBufferRef *picref;
-                    if (ist->st->sample_aspect_ratio.num) sar = ist->st->sample_aspect_ratio;
-                    else                                  sar = ist->st->codec->sample_aspect_ratio;
-
                     // fetch a frame from the filterchain, and buffer it
-                    picref = avfilter_get_video_buffer(inlink, AV_PERM_WRITE, inlink->w, inlink->h);
+                    AVFilterBufferRef *picref =
+                        avfilter_get_video_buffer(inlink, AV_PERM_WRITE, inlink->w, inlink->h);
                     av_image_copy(picref->data, picref->linesize,
                                   picture.data, picture.linesize,
                                   picref->format, inlink->w, inlink->h);
+                    avfilter_copy_video_buffer_ref_props_from_frame(
+                        picref, &picture, pkt->pos,
+                        ist->st->sample_aspect_ratio.num ?
+                        ist->st->sample_aspect_ratio : ist->st->codec->sample_aspect_ratio);
                     picref->pts = ist->pts;
-                    picref->video->interlaced      = picture.interlaced_frame;
-                    picref->video->top_field_first = picture.top_field_first;
-                    picref->video->pixel_aspect    = sar;
                     av_vsrc_buffer_add_frame(ost->input_video_filter, picref);
                 }
             }
diff --git a/ffplay.c b/ffplay.c
index ee0fb0e..65c3a1b 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -1699,9 +1699,10 @@ static int input_request_frame(AVFilterLink *link)
     }
     av_free_packet(&pkt);
 
+    avfilter_copy_video_buffer_ref_props_from_frame(
+        picref, priv->frame, pkt.pos,
+        priv->is->video_st->codec->sample_aspect_ratio);
     picref->pts = pts;
-    picref->pos = pkt.pos;
-    picref->video->pixel_aspect = priv->is->video_st->codec->sample_aspect_ratio;
     avfilter_start_frame(link, picref);
     avfilter_draw_slice(link, 0, link->h, 1);
     avfilter_end_frame(link);
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index 6525f57..abb4c60 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -163,6 +163,26 @@ static inline void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilt
     }
 }
 
+#if CONFIG_AVCODEC
+#include "libavcodec/avcodec.h" // AVFrame
+
+static inline
+void avfilter_copy_video_buffer_ref_props_from_frame(AVFilterBufferRef *picref,
+                                                     AVFrame *frame,
+                                                     int64_t pos, AVRational sar)
+{
+    /* use pkt_dts if pkt_pts is not available */
+    picref->pts = frame->pkt_pts == AV_NOPTS_VALUE ? frame->pkt_dts : frame->pkt_pts;
+    picref->pos = pos;
+
+    picref->video->pixel_aspect    = sar;
+    picref->video->interlaced      = frame->interlaced_frame;
+    picref->video->top_field_first = frame->top_field_first;
+    picref->video->key_frame       = frame->key_frame;
+    picref->video->pict_type       = frame->pict_type;
+}
+#endif
+
 /**
  * Add a new reference to a buffer.
  *
diff --git a/libavfilter/vsrc_movie.c b/libavfilter/vsrc_movie.c
index da601bb..b7ff49d 100644
--- a/libavfilter/vsrc_movie.c
+++ b/libavfilter/vsrc_movie.c
@@ -243,17 +243,11 @@ static int movie_get_frame(AVFilterLink *outlink)
 
                 /* FIXME: use a PTS correction mechanism as that in
                  * ffplay.c when some API will be available for that */
-                /* use pkt_dts if pkt_pts is not available */
-                movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
-                    movie->frame->pkt_dts : movie->frame->pkt_pts;
-
-                movie->picref->pos                    = movie->frame->reordered_opaque;
-                movie->picref->video->pixel_aspect = st->sample_aspect_ratio.num ?
-                    st->sample_aspect_ratio : movie->codec_ctx->sample_aspect_ratio;
-                movie->picref->video->interlaced      = movie->frame->interlaced_frame;
-                movie->picref->video->top_field_first = movie->frame->top_field_first;
-                movie->picref->video->key_frame       = movie->frame->key_frame;
-                movie->picref->video->pict_type       = movie->frame->pict_type;
+                avfilter_copy_video_buffer_ref_props_from_frame(
+                    movie->picref, movie->frame,
+                    movie->frame->reordered_opaque,
+                    st->sample_aspect_ratio.num ? st->sample_aspect_ratio : movie->codec_ctx->sample_aspect_ratio);
+
                 av_dlog(outlink->src,
                         "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
                         movie->file_name, movie->picref->pts,
-- 
1.7.2.3



More information about the ffmpeg-devel mailing list