1/*
2 * Copyright (c) 2010 Nicolas George
3 * Copyright (c) 2011 Stefano Sabatini
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * THE SOFTWARE.
22 */
23
24/**
25 * @file
26 * API example for decoding and filtering
27 * @example filtering_video.c
28 */
29
30#define _XOPEN_SOURCE 600 /* for usleep */
31#include <unistd.h>
32
33#include <libavcodec/avcodec.h>
34#include <libavformat/avformat.h>
35#include <libavfilter/avfiltergraph.h>
36#include <libavfilter/avcodec.h>
37#include <libavfilter/buffersink.h>
38#include <libavfilter/buffersrc.h>
39#include <libavutil/opt.h>
40
41const char *filter_descr = "scale=78:24";
42
43static AVFormatContext *fmt_ctx;
44static AVCodecContext *dec_ctx;
45AVFilterContext *buffersink_ctx;
46AVFilterContext *buffersrc_ctx;
47AVFilterGraph *filter_graph;
48static int video_stream_index = -1;
49static int64_t last_pts = AV_NOPTS_VALUE;
50
51static int open_input_file(const char *filename)
52{
53    int ret;
54    AVCodec *dec;
55
56    if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
57        av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
58        return ret;
59    }
60
61    if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
62        av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
63        return ret;
64    }
65
66    /* select the video stream */
67    ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
68    if (ret < 0) {
69        av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
70        return ret;
71    }
72    video_stream_index = ret;
73    dec_ctx = fmt_ctx->streams[video_stream_index]->codec;
74    av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);
75
76    /* init the video decoder */
77    if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
78        av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
79        return ret;
80    }
81
82    return 0;
83}
84
85static int init_filters(const char *filters_descr)
86{
87    char args[512];
88    int ret = 0;
89    AVFilter *buffersrc  = avfilter_get_by_name("buffer");
90    AVFilter *buffersink = avfilter_get_by_name("buffersink");
91    AVFilterInOut *outputs = avfilter_inout_alloc();
92    AVFilterInOut *inputs  = avfilter_inout_alloc();
93    enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
94
95    filter_graph = avfilter_graph_alloc();
96    if (!outputs || !inputs || !filter_graph) {
97        ret = AVERROR(ENOMEM);
98        goto end;
99    }
100
101    /* buffer video source: the decoded frames from the decoder will be inserted here. */
102    snprintf(args, sizeof(args),
103            "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
104            dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
105            dec_ctx->time_base.num, dec_ctx->time_base.den,
106            dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
107
108    ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
109                                       args, NULL, filter_graph);
110    if (ret < 0) {
111        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
112        goto end;
113    }
114
115    /* buffer video sink: to terminate the filter chain. */
116    ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
117                                       NULL, NULL, filter_graph);
118    if (ret < 0) {
119        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
120        goto end;
121    }
122
123    ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,
124                              AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
125    if (ret < 0) {
126        av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
127        goto end;
128    }
129
130    /* Endpoints for the filter graph. */
131    outputs->name       = av_strdup("in");
132    outputs->filter_ctx = buffersrc_ctx;
133    outputs->pad_idx    = 0;
134    outputs->next       = NULL;
135
136    inputs->name       = av_strdup("out");
137    inputs->filter_ctx = buffersink_ctx;
138    inputs->pad_idx    = 0;
139    inputs->next       = NULL;
140
141    if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
142                                    &inputs, &outputs, NULL)) < 0)
143        goto end;
144
145    if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
146        goto end;
147
148end:
149    avfilter_inout_free(&inputs);
150    avfilter_inout_free(&outputs);
151
152    return ret;
153}
154
155static void display_frame(const AVFrame *frame, AVRational time_base)
156{
157    int x, y;
158    uint8_t *p0, *p;
159    int64_t delay;
160
161    if (frame->pts != AV_NOPTS_VALUE) {
162        if (last_pts != AV_NOPTS_VALUE) {
163            /* sleep roughly the right amount of time;
164             * usleep is in microseconds, just like AV_TIME_BASE. */
165            delay = av_rescale_q(frame->pts - last_pts,
166                                 time_base, AV_TIME_BASE_Q);
167            if (delay > 0 && delay < 1000000)
168                usleep(delay);
169        }
170        last_pts = frame->pts;
171    }
172
173    /* Trivial ASCII grayscale display. */
174    p0 = frame->data[0];
175    puts("\033c");
176    for (y = 0; y < frame->height; y++) {
177        p = p0;
178        for (x = 0; x < frame->width; x++)
179            putchar(" .-+#"[*(p++) / 52]);
180        putchar('\n');
181        p0 += frame->linesize[0];
182    }
183    fflush(stdout);
184}
185
186int main(int argc, char **argv)
187{
188    int ret;
189    AVPacket packet;
190    AVFrame *frame = av_frame_alloc();
191    AVFrame *filt_frame = av_frame_alloc();
192    int got_frame;
193
194    if (!frame || !filt_frame) {
195        perror("Could not allocate frame");
196        exit(1);
197    }
198    if (argc != 2) {
199        fprintf(stderr, "Usage: %s file\n", argv[0]);
200        exit(1);
201    }
202
203    av_register_all();
204    avfilter_register_all();
205
206    if ((ret = open_input_file(argv[1])) < 0)
207        goto end;
208    if ((ret = init_filters(filter_descr)) < 0)
209        goto end;
210
211    /* read all packets */
212    while (1) {
213        if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
214            break;
215
216        if (packet.stream_index == video_stream_index) {
217            got_frame = 0;
218            ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);
219            if (ret < 0) {
220                av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
221                break;
222            }
223
224            if (got_frame) {
225                frame->pts = av_frame_get_best_effort_timestamp(frame);
226
227                /* push the decoded frame into the filtergraph */
228                if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
229                    av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
230                    break;
231                }
232
233                /* pull filtered frames from the filtergraph */
234                while (1) {
235                    ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
236                    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
237                        break;
238                    if (ret < 0)
239                        goto end;
240                    display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);
241                    av_frame_unref(filt_frame);
242                }
243                av_frame_unref(frame);
244            }
245        }
246        av_free_packet(&packet);
247    }
248end:
249    avfilter_graph_free(&filter_graph);
250    avcodec_close(dec_ctx);
251    avformat_close_input(&fmt_ctx);
252    av_frame_free(&frame);
253    av_frame_free(&filt_frame);
254
255    if (ret < 0 && ret != AVERROR_EOF) {
256        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
257        exit(1);
258    }
259
260    exit(0);
261}
262