2 * Copyright (c) 2010 Nicolas George
3 * Copyright (c) 2011 Stefano Sabatini
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * API example for decoding and filtering
29 #define _XOPEN_SOURCE 600 /* for usleep */
31 #include <libavcodec/avcodec.h>
32 #include <libavformat/avformat.h>
33 #include <libavfilter/avfiltergraph.h>
34 #include <libavfilter/vsink_buffer.h>
35 #include <libavfilter/vsrc_buffer.h>
37 const char *filter_descr = "scale=78:24";
39 static AVFormatContext *fmt_ctx;
40 static AVCodecContext *dec_ctx;
41 AVFilterContext *buffersink_ctx;
42 AVFilterContext *buffersrc_ctx;
43 AVFilterGraph *filter_graph;
44 static int video_stream_index = -1;
45 static int64_t last_pts = AV_NOPTS_VALUE;
47 static int open_input_file(const char *filename)
52 if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
53 av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
57 if ((ret = av_find_stream_info(fmt_ctx)) < 0) {
58 av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
62 /* select the video stream */
63 ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
65 av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
68 video_stream_index = ret;
69 dec_ctx = fmt_ctx->streams[video_stream_index]->codec;
71 /* init the video decoder */
72 if ((ret = avcodec_open(dec_ctx, dec)) < 0) {
73 av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
80 static int init_filters(const char *filters_descr)
84 AVFilter *buffersrc = avfilter_get_by_name("buffer");
85 AVFilter *buffersink = avfilter_get_by_name("buffersink");
86 AVFilterInOut *outputs = avfilter_inout_alloc();
87 AVFilterInOut *inputs = avfilter_inout_alloc();
88 enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, PIX_FMT_NONE };
89 filter_graph = avfilter_graph_alloc();
91 /* buffer video source: the decoded frames from the decoder will be inserted here. */
92 snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d",
93 dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
94 dec_ctx->time_base.num, dec_ctx->time_base.den,
95 dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
96 ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
97 args, NULL, filter_graph);
99 av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
103 /* buffer video sink: to terminate the filter chain. */
104 ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
105 NULL, pix_fmts, filter_graph);
107 av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
111 /* Endpoints for the filter graph. */
112 outputs->name = av_strdup("in");
113 outputs->filter_ctx = buffersrc_ctx;
114 outputs->pad_idx = 0;
115 outputs->next = NULL;
117 inputs->name = av_strdup("out");
118 inputs->filter_ctx = buffersink_ctx;
122 if ((ret = avfilter_graph_parse(filter_graph, filter_descr,
123 &inputs, &outputs, NULL)) < 0)
126 if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
130 static void display_picref(AVFilterBufferRef *picref, AVRational time_base)
136 if (picref->pts != AV_NOPTS_VALUE) {
137 if (last_pts != AV_NOPTS_VALUE) {
138 /* sleep roughly the right amount of time;
139 * usleep is in microseconds, just like AV_TIME_BASE. */
140 delay = av_rescale_q(picref->pts - last_pts,
141 time_base, AV_TIME_BASE_Q);
142 if (delay > 0 && delay < 1000000)
145 last_pts = picref->pts;
148 /* Trivial ASCII grayscale display. */
149 p0 = picref->data[0];
151 for (y = 0; y < picref->video->h; y++) {
153 for (x = 0; x < picref->video->w; x++)
154 putchar(" .-+#"[*(p++) / 52]);
156 p0 += picref->linesize[0];
161 int main(int argc, char **argv)
169 fprintf(stderr, "Usage: %s file\n", argv[0]);
173 avcodec_register_all();
175 avfilter_register_all();
177 if ((ret = open_input_file(argv[1]) < 0))
179 if ((ret = init_filters(filter_descr)) < 0)
182 /* read all packets */
184 AVFilterBufferRef *picref;
185 if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
188 if (packet.stream_index == video_stream_index) {
189 avcodec_get_frame_defaults(&frame);
191 ret = avcodec_decode_video2(dec_ctx, &frame, &got_frame, &packet);
192 av_free_packet(&packet);
194 av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
199 if (frame.pts == AV_NOPTS_VALUE)
200 frame.pts = frame.pkt_dts == AV_NOPTS_VALUE ?
201 frame.pkt_dts : frame.pkt_pts;
202 /* push the decoded frame into the filtergraph */
203 av_vsrc_buffer_add_frame(buffersrc_ctx, &frame);
205 /* pull filtered pictures from the filtergraph */
206 while (avfilter_poll_frame(buffersink_ctx->inputs[0])) {
207 av_vsink_buffer_get_video_buffer_ref(buffersink_ctx, &picref, 0);
209 display_picref(picref, buffersink_ctx->inputs[0]->time_base);
210 avfilter_unref_buffer(picref);
217 avfilter_graph_free(&filter_graph);
219 avcodec_close(dec_ctx);
220 av_close_input_file(fmt_ctx);
222 if (ret < 0 && ret != AVERROR_EOF) {
224 av_strerror(ret, buf, sizeof(buf));
225 fprintf(stderr, "Error occurred: %s\n", buf);