[FFmpeg-devel] [PATCH] Updated video filtering example
Stefano Sabatini
stefasab at gmail.com
Tue Jun 26 13:47:45 CEST 2012
On date Tuesday 2012-06-26 09:18:33 +0100, Gavin Kinsey encoded:
> Updated the video filtering example program based on the audio one.
[...]
> From b6bbe1a6eca3e822fca45b8678dde951c76cd293 Mon Sep 17 00:00:00 2001
> From: Gavin Kinsey <gkinsey at ad-holdings.co.uk>
> Date: Tue, 26 Jun 2012 09:16:16 +0100
> Subject: [PATCH] Updated video filtering example
>
> ---
> doc/examples/filtering_video.c | 36 ++++++++++++++++++++++++++----------
> 1 files changed, 26 insertions(+), 10 deletions(-)
>
> diff --git a/doc/examples/filtering_video.c b/doc/examples/filtering_video.c
> index 29fb77b..dd9f798 100644
> --- a/doc/examples/filtering_video.c
> +++ b/doc/examples/filtering_video.c
> @@ -34,6 +34,7 @@
> #include <libavfilter/avfiltergraph.h>
> #include <libavfilter/avcodec.h>
> #include <libavfilter/buffersink.h>
> +#include <libavfilter/buffersrc.h>
>
> const char *filter_descr = "scale=78:24";
>
> @@ -87,13 +88,17 @@ static int init_filters(const char *filters_descr)
> AVFilterInOut *outputs = avfilter_inout_alloc();
> AVFilterInOut *inputs = avfilter_inout_alloc();
> enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, PIX_FMT_NONE };
> + AVBufferSinkParams *buffersink_params;
> +
> filter_graph = avfilter_graph_alloc();
>
> /* buffer video source: the decoded frames from the decoder will be inserted here. */
> - snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d",
> - dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
> - dec_ctx->time_base.num, dec_ctx->time_base.den,
> - dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
> + snprintf(args, sizeof(args),
> + "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
> + dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
> + dec_ctx->time_base.num, dec_ctx->time_base.den,
> + dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
> +
> ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
> args, NULL, filter_graph);
> if (ret < 0) {
> @@ -102,8 +107,11 @@ static int init_filters(const char *filters_descr)
> }
>
> /* buffer video sink: to terminate the filter chain. */
> + buffersink_params = av_buffersink_params_alloc();
> + buffersink_params->pixel_fmts = pix_fmts;
> ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
> - NULL, pix_fmts, filter_graph);
> + NULL, buffersink_params, filter_graph);
> + av_free(buffersink_params);
> if (ret < 0) {
> av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
> return ret;
> @@ -191,7 +199,6 @@ int main(int argc, char **argv)
> avcodec_get_frame_defaults(&frame);
> got_frame = 0;
> ret = avcodec_decode_video2(dec_ctx, &frame, &got_frame, &packet);
> - av_free_packet(&packet);
Any reason for which you moved this to the end of the loop?
> if (ret < 0) {
> av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
> break;
> @@ -201,18 +208,27 @@ int main(int argc, char **argv)
> frame.pts = av_frame_get_best_effort_timestamp(&frame);
>
> /* push the decoded frame into the filtergraph */
> - av_vsrc_buffer_add_frame(buffersrc_ctx, &frame, 0);
> + if (av_buffersrc_add_frame(buffersrc_ctx, &frame, 0) < 0) {
> + av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
> + break;
> + }
>
> /* pull filtered pictures from the filtergraph */
> - while (avfilter_poll_frame(buffersink_ctx->inputs[0])) {
> - av_buffersink_get_buffer_ref(buffersink_ctx, &picref, 0);
> + while (1) {
> + ret = av_buffersink_get_buffer_ref(buffersink_ctx, &picref, 0);
> + if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
> + break;
> + if(ret < 0)
> + goto end;
> +
> if (picref) {
> display_picref(picref, buffersink_ctx->inputs[0]->time_base);
> - avfilter_unref_buffer(picref);
> + avfilter_unref_bufferp(&picref);
> }
> }
> }
> }
> + av_free_packet(&packet);
And patch looks good, thanks.
--
FFmpeg = Fundamentalist and Fancy Mind-dumbing Powered Exxagerate Geek
More information about the ffmpeg-devel
mailing list