[FFmpeg-devel] [PATCH] lavfi: add vsink_buffer, and use it in ff* tools
Stefano Sabatini
stefano.sabatini-lala at poste.it
Sat Jun 11 19:39:30 CEST 2011
---
cmdutils.c | 68 ------------------------------
cmdutils.h | 6 ---
ffmpeg.c | 20 +++++----
ffplay.c | 14 ++++---
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vsink_buffer.c | 100 ++++++++++++++++++++++++++++++++++++++++++++
libavfilter/vsink_buffer.h | 49 +++++++++++++++++++++
8 files changed, 171 insertions(+), 88 deletions(-)
create mode 100644 libavfilter/vsink_buffer.c
create mode 100644 libavfilter/vsink_buffer.h
diff --git a/cmdutils.c b/cmdutils.c
index 4f27f50..c6e1b6b 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -880,71 +880,3 @@ FILE *get_preset_file(char *filename, size_t filename_size,
return f;
}
-
-#if CONFIG_AVFILTER
-
-static int ffsink_init(AVFilterContext *ctx, const char *args, void *opaque)
-{
- FFSinkContext *priv = ctx->priv;
-
- if (!opaque)
- return AVERROR(EINVAL);
- *priv = *(FFSinkContext *)opaque;
-
- return 0;
-}
-
-static void null_end_frame(AVFilterLink *inlink) { }
-
-static int ffsink_query_formats(AVFilterContext *ctx)
-{
- FFSinkContext *priv = ctx->priv;
- enum PixelFormat pix_fmts[] = { priv->pix_fmt, PIX_FMT_NONE };
-
- avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
- return 0;
-}
-
-AVFilter ffsink = {
- .name = "ffsink",
- .priv_size = sizeof(FFSinkContext),
- .init = ffsink_init,
-
- .query_formats = ffsink_query_formats,
-
- .inputs = (AVFilterPad[]) {{ .name = "default",
- .type = AVMEDIA_TYPE_VIDEO,
- .end_frame = null_end_frame,
- .min_perms = AV_PERM_READ, },
- { .name = NULL }},
- .outputs = (AVFilterPad[]) {{ .name = NULL }},
-};
-
-int get_filtered_video_frame(AVFilterContext *ctx, AVFrame *frame,
- AVFilterBufferRef **picref_ptr, AVRational *tb)
-{
- int ret;
- AVFilterBufferRef *picref;
- *picref_ptr = NULL;
-
- if ((ret = avfilter_request_frame(ctx->inputs[0])) < 0)
- return ret;
- if (!(picref = ctx->inputs[0]->cur_buf))
- return AVERROR(ENOENT);
- *picref_ptr = picref;
- ctx->inputs[0]->cur_buf = NULL;
- *tb = ctx->inputs[0]->time_base;
-
- memcpy(frame->data, picref->data, sizeof(frame->data));
- memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
- frame->pkt_pos = picref->pos;
- frame->interlaced_frame = picref->video->interlaced;
- frame->top_field_first = picref->video->top_field_first;
- frame->key_frame = picref->video->key_frame;
- frame->pict_type = picref->video->pict_type;
- frame->sample_aspect_ratio = picref->video->sample_aspect_ratio;
-
- return 1;
-}
-
-#endif /* CONFIG_AVFILTER */
diff --git a/cmdutils.h b/cmdutils.h
index 5fd398d..ee1e7fd 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -259,12 +259,6 @@ int read_file(const char *filename, char **bufptr, size_t *size);
FILE *get_preset_file(char *filename, size_t filename_size,
const char *preset_name, int is_path, const char *codec_name);
-typedef struct {
- enum PixelFormat pix_fmt;
-} FFSinkContext;
-
-extern AVFilter ffsink;
-
/**
* Extract a frame from sink.
*
diff --git a/ffmpeg.c b/ffmpeg.c
index c51f371..a2e4428 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -51,6 +51,7 @@
# include "libavfilter/avcodec.h"
# include "libavfilter/avfilter.h"
# include "libavfilter/avfiltergraph.h"
+# include "libavfilter/vsink_buffer.h"
# include "libavfilter/vsrc_buffer.h"
#endif
@@ -362,7 +363,7 @@ static int configure_video_filters(AVInputStream *ist, AVOutputStream *ost)
/** filter graph containing all filters including input & output */
AVCodecContext *codec = ost->st->codec;
AVCodecContext *icodec = ist->st->codec;
- FFSinkContext ffsink_ctx = { .pix_fmt = codec->pix_fmt };
+ enum PixelFormat pix_fmts[] = { codec->pix_fmt, PIX_FMT_NONE };
AVRational sample_aspect_ratio;
char args[255];
int ret;
@@ -382,8 +383,8 @@ static int configure_video_filters(AVInputStream *ist, AVOutputStream *ost)
"src", args, NULL, ost->graph);
if (ret < 0)
return ret;
- ret = avfilter_graph_create_filter(&ost->output_video_filter, &ffsink,
- "out", NULL, &ffsink_ctx, ost->graph);
+ ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
+ "out", NULL, pix_fmts, ost->graph);
if (ret < 0)
return ret;
last_filter = ost->input_video_filter;
@@ -1706,12 +1707,15 @@ static int output_packet(AVInputStream *ist, int ist_index,
frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
!ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]);
while (frame_available) {
- AVRational ist_pts_tb;
- if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter)
- if (get_filtered_video_frame(ost->output_video_filter, &picture, &ost->picref, &ist_pts_tb) < 0)
+ if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter) {
+ AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base;
+ if (av_vsink_buffer_get_video_buffer_ref(ost->output_video_filter, &ost->picref, 0) < 0)
goto cont;
- if (ost->picref)
- ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
+ if (ost->picref) {
+ avfilter_fill_frame_from_video_buffer_ref(&picture, ost->picref);
+ ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
+ }
+ }
#endif
os = output_files[ost->file_index];
diff --git a/ffplay.c b/ffplay.c
index 4145f9c..2411863 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -41,6 +41,7 @@
# include "libavfilter/avcodec.h"
# include "libavfilter/avfilter.h"
# include "libavfilter/avfiltergraph.h"
+# include "libavfilter/vsink_buffer.h"
#endif
#include <SDL.h>
@@ -1682,7 +1683,7 @@ static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const c
{
char sws_flags_str[128];
int ret;
- FFSinkContext ffsink_ctx = { .pix_fmt = PIX_FMT_YUV420P };
+ enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
AVFilterContext *filt_src = NULL, *filt_out = NULL;
snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags);
graph->scale_sws_opts = av_strdup(sws_flags_str);
@@ -1690,8 +1691,8 @@ static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const c
if ((ret = avfilter_graph_create_filter(&filt_src, &input_filter, "src",
NULL, is, graph)) < 0)
goto the_end;
- if ((ret = avfilter_graph_create_filter(&filt_out, &ffsink, "out",
- NULL, &ffsink_ctx, graph)) < 0)
+ if ((ret = avfilter_graph_create_filter(&filt_out, avfilter_get_by_name("buffersink"), "out",
+ NULL, pix_fmts, graph)) < 0)
goto the_end;
if(vfilters) {
@@ -1748,13 +1749,14 @@ static int video_thread(void *arg)
AVPacket pkt;
#else
AVFilterBufferRef *picref;
- AVRational tb;
+ AVRational tb = filt_out->inputs[0]->time_base;
#endif
while (is->paused && !is->videoq.abort_request)
SDL_Delay(10);
#if CONFIG_AVFILTER
- ret = get_filtered_video_frame(filt_out, frame, &picref, &tb);
+ ret = av_vsink_buffer_get_video_buffer_ref(filt_out, &picref, 0);
if (picref) {
+ avfilter_fill_frame_from_video_buffer_ref(frame, picref);
pts_int = picref->pts;
pos = picref->pos;
frame->opaque = picref;
@@ -1776,7 +1778,7 @@ static int video_thread(void *arg)
if (ret < 0) goto the_end;
- if (!ret)
+ if (!picref)
continue;
pts = pts_int*av_q2d(is->video_st->time_base);
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 2324fb9..69b4f35 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -65,6 +65,7 @@ OBJS-$(CONFIG_FREI0R_SRC_FILTER) += vf_frei0r.o
OBJS-$(CONFIG_MOVIE_FILTER) += vsrc_movie.o
OBJS-$(CONFIG_NULLSRC_FILTER) += vsrc_nullsrc.o
+OBJS-$(CONFIG_BUFFERSINK_FILTER) += vsink_buffer.o
OBJS-$(CONFIG_NULLSINK_FILTER) += vsink_nullsink.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 5f1065f..708dc17 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -81,5 +81,6 @@ void avfilter_register_all(void)
REGISTER_FILTER (MOVIE, movie, vsrc);
REGISTER_FILTER (NULLSRC, nullsrc, vsrc);
+ REGISTER_FILTER (BUFFER, buffersink, vsink);
REGISTER_FILTER (NULLSINK, nullsink, vsink);
}
diff --git a/libavfilter/vsink_buffer.c b/libavfilter/vsink_buffer.c
new file mode 100644
index 0000000..484f91d
--- /dev/null
+++ b/libavfilter/vsink_buffer.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2011 Stefano Sabatini
+
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * simple video sink
+ */
+
+#include "avfilter.h"
+#include "vsink_buffer.h"
+
+typedef struct {
+ enum PixelFormat *pix_fmts; ///< accepted pixel formats, must be terminated with -1
+ AVFilterBufferRef *picref;
+} BufferSinkContext;
+
+static int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ BufferSinkContext *buf = ctx->priv;
+
+ if (!opaque) {
+ av_log(ctx, AV_LOG_ERROR, "No opaque field provided, which is required.\n");
+ return AVERROR(EINVAL);
+ }
+
+ buf->pix_fmts = opaque;
+ return 0;
+}
+
+static void null_end_frame(AVFilterLink *inlink) { }
+
+static int poll_frame(AVFilterLink *outlink)
+{
+ AVFilterLink *inlink = outlink->src->inputs[0];
+
+ return inlink->cur_buf ? 1 : avfilter_poll_frame(inlink);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ BufferSinkContext *buf = ctx->priv;
+
+ avfilter_set_common_formats(ctx, avfilter_make_format_list(buf->pix_fmts));
+ return 0;
+}
+
+int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *ctx,
+ AVFilterBufferRef **picref, int flags)
+{
+ AVFilterLink *inlink = ctx->inputs[0];
+ int ret;
+ *picref = NULL;
+
+ /* no picref available, fetch it from the filterchain */
+ if (!inlink->cur_buf) {
+ if ((ret = avfilter_request_frame(inlink)) < 0)
+ return ret;
+ }
+
+ if (!(*picref = inlink->cur_buf))
+ return AVERROR(EINVAL);
+
+ if (!(flags & AV_VSINK_BUF_FLAG_PEEK))
+ inlink->cur_buf = NULL;
+
+ return 0;
+}
+
+AVFilter avfilter_vsink_buffersink = {
+ .name = "buffersink",
+ .priv_size = sizeof(BufferSinkContext),
+ .init = init,
+
+ .query_formats = query_formats,
+
+ .inputs = (AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .poll_frame = poll_frame,
+ .end_frame = null_end_frame,
+ .min_perms = AV_PERM_READ, },
+ { .name = NULL }},
+ .outputs = (AVFilterPad[]) {{ .name = NULL }},
+};
diff --git a/libavfilter/vsink_buffer.h b/libavfilter/vsink_buffer.h
new file mode 100644
index 0000000..9e6333b
--- /dev/null
+++ b/libavfilter/vsink_buffer.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2011 Stefano Sabatini
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVFILTER_VSINK_BUFFER_H
+#define AVFILTER_VSINK_BUFFER_H
+
+/**
+ * @file
+ * memory buffer sink API for video
+ */
+
+#include "avfilter.h"
+
+/**
+ * Tell av_vsink_buffer_get_video_buffer_ref() to read the picref, but not
+ * remove it from the buffer. This is useful if you need only to read
+ * the picref, without to fetch it.
+ */
+#define AV_VSINK_BUF_FLAG_PEEK 1
+
+/**
+ * Get a video buffer data from buffer_sink and put it in picref.
+ *
+ * @param buffer_sink pointer to a buffer sink context
+ * @param flags a combination of AV_VSINK_BUF_FLAG_* flags
+ * @return >= 0 in case of success, a negative AVERROR code in case of
+ * failure
+ */
+int av_vsink_buffer_get_video_buffer_ref(AVFilterContext *buffer_sink,
+ AVFilterBufferRef **picref, int flags);
+
+#endif /* AVFILTER_VSINK_BUFFER_H */
--
1.7.2.5
More information about the ffmpeg-devel
mailing list