[FFmpeg-devel] [PATCH] Add example reencoding_filtered_video.c

Andrey Utkin andrey.krieger.utkin at gmail.com
Thu Jan 30 20:55:56 CET 2014


---
 configure                                |   2 +
 doc/Makefile                             |   1 +
 doc/examples/Makefile                    |   1 +
 doc/examples/reencoding_filtered_video.c | 423 +++++++++++++++++++++++++++++++
 4 files changed, 427 insertions(+)
 create mode 100644 doc/examples/reencoding_filtered_video.c

diff --git a/configure b/configure
index 76723fc..3bab96c 100755
--- a/configure
+++ b/configure
@@ -1247,6 +1247,7 @@ EXAMPLE_LIST="
     filtering_video_example
     metadata_example
     muxing_example
+    reencoding_filtered_video_example
     remuxing_example
     resampling_audio_example
     scaling_video_example
@@ -2397,6 +2398,7 @@ filtering_audio_example_deps="avfilter avcodec avformat avutil"
 filtering_video_example_deps="avfilter avcodec avformat avutil"
 metadata_example_deps="avformat avutil"
 muxing_example_deps="avcodec avformat avutil swscale"
+reencoding_filtered_video_example_deps="avfilter avcodec avformat avutil"
 remuxing_example_deps="avcodec avformat avutil"
 resampling_audio_example_deps="avutil swresample"
 scaling_video_example_deps="avutil swscale"
diff --git a/doc/Makefile b/doc/Makefile
index f75a401..d1d4048 100644
--- a/doc/Makefile
+++ b/doc/Makefile
@@ -42,6 +42,7 @@ DOC_EXAMPLES-$(CONFIG_FILTERING_AUDIO_EXAMPLE)   += filtering_audio
 DOC_EXAMPLES-$(CONFIG_FILTERING_VIDEO_EXAMPLE)   += filtering_video
 DOC_EXAMPLES-$(CONFIG_METADATA_EXAMPLE)          += metadata
 DOC_EXAMPLES-$(CONFIG_MUXING_EXAMPLE)            += muxing
+DOC_EXAMPLES-$(CONFIG_REENCODING_FILTERED_VIDEO_EXAMPLE) += reencoding_filtered_video
 DOC_EXAMPLES-$(CONFIG_REMUXING_EXAMPLE)          += remuxing
 DOC_EXAMPLES-$(CONFIG_RESAMPLING_AUDIO_EXAMPLE)  += resampling_audio
 DOC_EXAMPLES-$(CONFIG_SCALING_VIDEO_EXAMPLE)     += scaling_video
diff --git a/doc/examples/Makefile b/doc/examples/Makefile
index 52d9c52..369e49b 100644
--- a/doc/examples/Makefile
+++ b/doc/examples/Makefile
@@ -17,6 +17,7 @@ EXAMPLES=       decoding_encoding                  \
                 filtering_audio                    \
                 metadata                           \
                 muxing                             \
+                reencoding_filtered_video          \
                 remuxing                           \
                 resampling_audio                   \
                 scaling_video                      \
diff --git a/doc/examples/reencoding_filtered_video.c b/doc/examples/reencoding_filtered_video.c
new file mode 100644
index 0000000..997f830
--- /dev/null
+++ b/doc/examples/reencoding_filtered_video.c
@@ -0,0 +1,423 @@
+/*
+ * Copyright (c) 2010 Nicolas George
+ * Copyright (c) 2011 Stefano Sabatini
+ * Copyright (c) 2014 Andrey Utkin
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * @file
+ * API example for demuxing, decoding, filtering, encoding and muxing
+ * @example doc/examples/reencoding_filtered_video.c
+ */
+
+#include <libavcodec/avcodec.h>
+#include <libavformat/avformat.h>
+#include <libavfilter/avfiltergraph.h>
+#include <libavfilter/avcodec.h>
+#include <libavfilter/buffersink.h>
+#include <libavfilter/buffersrc.h>
+#include <libavutil/opt.h>
+#include <libavutil/pixdesc.h>
+
+/* Filtergraph string is a good place to set up resize, pixel/sample format or
+ * timebase conversion, constant output fps etc.
+ */
+const char *filter_descr;
+
+static AVFormatContext *ifmt_ctx;
+static AVFormatContext *ofmt_ctx;
+static AVCodecContext *dec_ctx;
+static AVCodecContext *enc_ctx;
+AVFilterContext *buffersink_ctx;
+AVFilterContext *buffersrc_ctx;
+AVFilterGraph *filter_graph;
+static int video_stream_index = -1;
+
+static int open_input_file(const char *filename)
+{
+    int ret;
+
+    ifmt_ctx = NULL;
+    if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
+        return ret;
+    }
+
+    if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
+        return ret;
+    }
+
+    /* select the video stream */
+    ret = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
+        return ret;
+    }
+    video_stream_index = ret;
+    dec_ctx = ifmt_ctx->streams[video_stream_index]->codec;
+    av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);
+
+    av_dump_format(ifmt_ctx, 0, filename, 0);
+    return 0;
+}
+
+static int open_output_file(const char *filename)
+{
+    AVStream *out_stream;
+    AVCodec *enc;
+    AVDictionary *encoder_opts = NULL;
+    int ret;
+
+    ofmt_ctx = NULL;
+    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
+    if (!ofmt_ctx) {
+        av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
+        return AVERROR_UNKNOWN;
+    }
+
+    /* Open just one elementary stream for transcoded video */
+    enc = avcodec_find_encoder(AV_CODEC_ID_H264);
+
+    out_stream = avformat_new_stream(ofmt_ctx, enc);
+    if (!out_stream) {
+        av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
+        return AVERROR_UNKNOWN;
+    }
+
+    if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
+        out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
+
+    /* rewrite output codec setting according to our needs */
+    /* in this example, we choose transcoding to H264 */
+
+    /* init the video decoder */
+    if ((ret = avcodec_open2(dec_ctx, avcodec_find_decoder(dec_ctx->codec_id), NULL)) < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
+        return ret;
+    }
+
+    /* init the video encoder */
+    enc_ctx = out_stream->codec;
+    enc_ctx->height = dec_ctx->height;
+    enc_ctx->width = dec_ctx->width;
+    /* yuv420p is just common in use and known to be supported by libx264
+     * pix_fmt can be also taken from enc->pix_fmts[]
+     */
+    enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
+    enc_ctx->time_base = out_stream->time_base;
+    av_dict_set(&encoder_opts, "preset", "medium", 0); /* performance/quality high-level configuration */
+    av_dict_set(&encoder_opts, "profile", "baseline", 0); /* H264 specific profile */
+    if ((ret = avcodec_open2(enc_ctx, enc, &encoder_opts)) < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder\n");
+        return ret;
+    }
+    av_dump_format(ofmt_ctx, 0, filename, 1);
+
+    if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
+        ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
+        if (ret < 0) {
+            av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
+            return ret;
+        }
+    }
+
+    /* init muxer, write output file header */
+    ret = avformat_write_header(ofmt_ctx, NULL);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
+        return ret;
+    }
+
+    return 0;
+}
+
+static int init_filters(const char *filters_descr)
+{
+    char args[512];
+    int ret = 0;
+    AVFilter *buffersrc  = avfilter_get_by_name("buffer");
+    AVFilter *buffersink = avfilter_get_by_name("buffersink");
+    AVFilterInOut *outputs = avfilter_inout_alloc();
+    AVFilterInOut *inputs  = avfilter_inout_alloc();
+
+    filter_graph = avfilter_graph_alloc();
+
+    if (!buffersrc || !buffersink) {
+        av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
+        ret = AVERROR_UNKNOWN;
+        goto end;
+    }
+
+    if (!outputs || !inputs || !filter_graph) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    /* buffer video source: the decoded frames from the decoder will be inserted here. */
+    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) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
+        goto end;
+    }
+
+    /* buffer video sink: to terminate the filter chain. */
+    ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
+                                       NULL, NULL, filter_graph);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
+        goto end;
+    }
+
+    ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", enc_ctx->codec->pix_fmts,
+                              AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
+        goto end;
+    }
+
+    /* Endpoints for the filter graph. */
+    outputs->name       = av_strdup("in");
+    outputs->filter_ctx = buffersrc_ctx;
+    outputs->pad_idx    = 0;
+    outputs->next       = NULL;
+
+    inputs->name       = av_strdup("out");
+    inputs->filter_ctx = buffersink_ctx;
+    inputs->pad_idx    = 0;
+    inputs->next       = NULL;
+
+    if (!outputs->name || !inputs->name) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
+                    &inputs, &outputs, NULL)) < 0)
+        goto end;
+
+    if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
+        goto end;
+
+end:
+    avfilter_inout_free(&inputs);
+    avfilter_inout_free(&outputs);
+
+    return ret;
+}
+
+static int encode_write_frame(AVFrame *filt_frame, int *got_frame) {
+    int ret;
+    int got_frame_local;
+    AVPacket enc_pkt;
+
+    if (!got_frame)
+        got_frame = &got_frame_local;
+
+    av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
+    /* encode filtered frame */
+    enc_pkt.data = NULL;
+    enc_pkt.size = 0;
+    av_init_packet(&enc_pkt);
+    ret = avcodec_encode_video2(enc_ctx, &enc_pkt, filt_frame, got_frame);
+    av_frame_free(&filt_frame);
+    if (ret < 0)
+        return ret;
+    if (!(*got_frame))
+        return 0;
+
+    /* prepare packet for muxing */
+    enc_pkt.stream_index = 0;  /* we have only one stream in output */
+    enc_pkt.dts = av_rescale_q_rnd(enc_pkt.dts, enc_ctx->time_base, ofmt_ctx->streams[0]->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
+    enc_pkt.pts = av_rescale_q_rnd(enc_pkt.pts, enc_ctx->time_base, ofmt_ctx->streams[0]->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
+    enc_pkt.duration = av_rescale_q(enc_pkt.duration, enc_ctx->time_base, ofmt_ctx->streams[0]->time_base);
+
+    av_log(NULL, AV_LOG_INFO, "Muxing frame\n");
+    /* mux encoded frame */
+    ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
+    return ret;
+}
+
+static int filter_encode_write_frame(AVFrame *frame) {
+    int ret;
+    AVFrame *filt_frame;
+
+    av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
+    /* push the decoded frame into the filtergraph */
+    ret = av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
+        return ret;
+    }
+
+    /* pull filtered frames from the filtergraph */
+    while (1) {
+        filt_frame = av_frame_alloc();
+        if (!filt_frame) {
+            ret = AVERROR(ENOMEM);
+            break;
+        }
+        av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
+        ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
+        if (ret < 0) {
+            /* if no more frames for output - returns AVERROR(EAGAIN)
+             * if flushed and no more frames for output - returns AVERROR_EOF
+             * rewrite retcode to 0 to show it as normal procedure completion
+             */
+            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+                ret = 0;
+            av_frame_free(&filt_frame);
+            break;
+        }
+
+        filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
+        ret = encode_write_frame(filt_frame, NULL);
+        if (ret < 0)
+            break;
+    }
+
+    return ret;
+}
+
+static int flush_encoder(void) {
+    int ret;
+    int got_frame;
+
+    if (!(enc_ctx->codec->capabilities & CODEC_CAP_DELAY))
+        return 0;
+
+    while (1) {
+        av_log(NULL, AV_LOG_INFO, "Flushing encoder\n");
+        ret = encode_write_frame(NULL, &got_frame);
+        if (ret < 0)
+            break;
+        if (!got_frame)
+            return 0;
+    }
+    return ret;
+}
+
+int main(int argc, char **argv)
+{
+    int ret;
+    AVPacket packet;
+    AVFrame *frame;
+    int got_frame;
+    char filter_spec_buf[1000];
+
+    if (argc != 3) {
+        av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
+        return 1;
+    }
+
+    av_register_all();
+    avfilter_register_all();
+
+    if ((ret = open_input_file(argv[1])) < 0)
+        goto end;
+    if ((ret = open_output_file(argv[2])) < 0)
+        goto end;
+
+    /* This filtering specification saves us special actions for pixel format
+     * conversion and conversion of timebase before feeding frames to encoder.
+     * Arbitrary filters can be prepended.
+     */
+    snprintf(filter_spec_buf, sizeof(filter_spec_buf), "format=pix_fmts=%s,settb=%d/%d", av_get_pix_fmt_name(enc_ctx->pix_fmt), enc_ctx->time_base.num, enc_ctx->time_base.den);
+    filter_descr = filter_spec_buf;
+    av_log(NULL, AV_LOG_INFO, "filtering specification: %s\n", filter_descr);
+    if ((ret = init_filters(filter_descr)) < 0)
+        goto end;
+
+    /* read all packets */
+    while (1) {
+        if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
+            break;
+
+        if (packet.stream_index == video_stream_index) {
+            frame = av_frame_alloc();
+            if (!frame) {
+                ret = AVERROR(ENOMEM);
+                break;
+            }
+            av_log(NULL, AV_LOG_INFO, "Decoding frame\n");
+            packet.dts = av_rescale_q_rnd(packet.dts, ifmt_ctx->streams[packet.stream_index]->time_base, dec_ctx->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
+            packet.pts = av_rescale_q_rnd(packet.pts, ifmt_ctx->streams[packet.stream_index]->time_base, dec_ctx->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
+            ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);
+            if (ret < 0) {
+                av_frame_free(&frame);
+                av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
+                break;
+            }
+
+            if (got_frame) {
+                frame->pts = av_frame_get_best_effort_timestamp(frame);
+
+                ret = filter_encode_write_frame(frame);
+                if (ret < 0)
+                    goto end;
+
+            }
+            av_frame_free(&frame);
+        } else {
+            /* Other elementary streams are discarded in this example.
+             * They may be otherwise also reencoded, or remuxed.
+             */
+            ;
+        }
+        av_free_packet(&packet);
+    }
+
+    /* flush filters */
+    ret = filter_encode_write_frame(NULL);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Flushing filters failed\n");
+        goto end;
+    }
+
+    /* flush encoder */
+    ret = flush_encoder();
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
+        goto end;
+    }
+
+    av_write_trailer(ofmt_ctx);
+end:
+    avfilter_graph_free(&filter_graph);
+    avcodec_close(dec_ctx);
+    avcodec_close(enc_ctx);
+    avformat_close_input(&ifmt_ctx);
+    if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
+        avio_close(ofmt_ctx->pb);
+    avformat_free_context(ofmt_ctx);
+
+    if (ret < 0)
+        av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
+
+    return ret ? 1 : 0;
+}
-- 
1.8.1.5



More information about the ffmpeg-devel mailing list