[FFmpeg-devel] [PATCH] doc/examples/filtering_video: switch to new decoding API

wm4 nfxjfg at googlemail.com
Thu Mar 30 15:23:11 EEST 2017


On Thu, 30 Mar 2017 13:35:47 +0200
Matthieu Bouron <matthieu.bouron at gmail.com> wrote:

> From e9e5b3e679a12fdd9495c53177ade51c3dee7ba2 Mon Sep 17 00:00:00 2001
> From: Matthieu Bouron <matthieu.bouron at gmail.com>
> Date: Wed, 29 Mar 2017 14:58:01 +0200
> Subject: [PATCH] doc/examples/filtering_video: switch to new decoding API
> 
> ---
>  doc/examples/filtering_video.c | 46 +++++++++++++++++++++++++-----------------
>  1 file changed, 27 insertions(+), 19 deletions(-)
> 
> diff --git a/doc/examples/filtering_video.c b/doc/examples/filtering_video.c
> index 15116d3881..4d973024f1 100644
> --- a/doc/examples/filtering_video.c
> +++ b/doc/examples/filtering_video.c
> @@ -213,7 +213,6 @@ int main(int argc, char **argv)
>      AVPacket packet;
>      AVFrame *frame = av_frame_alloc();
>      AVFrame *filt_frame = av_frame_alloc();
> -    int got_frame;
>  
>      if (!frame || !filt_frame) {
>          perror("Could not allocate frame");
> @@ -238,33 +237,42 @@ int main(int argc, char **argv)
>              break;
>  
>          if (packet.stream_index == video_stream_index) {
> -            got_frame = 0;
> -            ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);
> +            ret = avcodec_send_packet(dec_ctx, &packet);

I assume the "flush" packet makes it to this code as well...

>              if (ret < 0) {
> -                av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
> +                av_log(NULL, AV_LOG_ERROR, "Error while sending a packet to the decoder\n");
>                  break;
>              }
>  
> -            if (got_frame) {
> -                frame->pts = av_frame_get_best_effort_timestamp(frame);
> -
> -                /* push the decoded frame into the filtergraph */
> -                if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
> -                    av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
> +            while (ret >= 0) {
> +                ret = avcodec_receive_frame(dec_ctx, frame);
> +                if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
>                      break;

On EOF this assumes the demuxer takes care of it, and if the demuxer is
unexpectedly not EOF, the send call will error. So LGTM.

> +                } else if (ret < 0) {
> +                    av_log(NULL, AV_LOG_ERROR, "Error while receiving a frame from the decoder\n");
> +                    goto end;
>                  }
>  
> -                /* pull filtered frames from the filtergraph */
> -                while (1) {
> -                    ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
> -                    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
> +                if (ret >= 0) {
> +                    frame->pts = av_frame_get_best_effort_timestamp(frame);
> +
> +                    /* push the decoded frame into the filtergraph */
> +                    if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
> +                        av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
>                          break;
> -                    if (ret < 0)
> -                        goto end;
> -                    display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);
> -                    av_frame_unref(filt_frame);
> +                    }
> +
> +                    /* pull filtered frames from the filtergraph */
> +                    while (1) {
> +                        ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
> +                        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
> +                            break;
> +                        if (ret < 0)
> +                            goto end;
> +                        display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);
> +                        av_frame_unref(filt_frame);
> +                    }
> +                    av_frame_unref(frame);
>                  }
> -                av_frame_unref(frame);
>              }
>          }
>          av_packet_unref(&packet);

LGTM.


More information about the ffmpeg-devel mailing list