1/*
2 * Copyright (c) 2010 Nicolas George
3 * Copyright (c) 2011 Stefano Sabatini
4 * Copyright (c) 2012 Cl��ment B��sch
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/**
26 * @file
27 * API example for audio decoding and filtering
28 * @example filtering_audio.c
29 */
30
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
41static const char *filter_descr = "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono";
42static const char *player       = "ffplay -f s16le -ar 8000 -ac 1 -";
43
44static AVFormatContext *fmt_ctx;
45static AVCodecContext *dec_ctx;
46AVFilterContext *buffersink_ctx;
47AVFilterContext *buffersrc_ctx;
48AVFilterGraph *filter_graph;
49static int audio_stream_index = -1;
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 audio stream */
67    ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
68    if (ret < 0) {
69        av_log(NULL, AV_LOG_ERROR, "Cannot find a audio stream in the input file\n");
70        return ret;
71    }
72    audio_stream_index = ret;
73    dec_ctx = fmt_ctx->streams[audio_stream_index]->codec;
74    av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);
75
76    /* init the audio decoder */
77    if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
78        av_log(NULL, AV_LOG_ERROR, "Cannot open audio 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 *abuffersrc  = avfilter_get_by_name("abuffer");
90    AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
91    AVFilterInOut *outputs = avfilter_inout_alloc();
92    AVFilterInOut *inputs  = avfilter_inout_alloc();
93    static const enum AVSampleFormat out_sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
94    static const int64_t out_channel_layouts[] = { AV_CH_LAYOUT_MONO, -1 };
95    static const int out_sample_rates[] = { 8000, -1 };
96    const AVFilterLink *outlink;
97    AVRational time_base = fmt_ctx->streams[audio_stream_index]->time_base;
98
99    filter_graph = avfilter_graph_alloc();
100    if (!outputs || !inputs || !filter_graph) {
101        ret = AVERROR(ENOMEM);
102        goto end;
103    }
104
105    /* buffer audio source: the decoded frames from the decoder will be inserted here. */
106    if (!dec_ctx->channel_layout)
107        dec_ctx->channel_layout = av_get_default_channel_layout(dec_ctx->channels);
108    snprintf(args, sizeof(args),
109            "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
110             time_base.num, time_base.den, dec_ctx->sample_rate,
111             av_get_sample_fmt_name(dec_ctx->sample_fmt), dec_ctx->channel_layout);
112    ret = avfilter_graph_create_filter(&buffersrc_ctx, abuffersrc, "in",
113                                       args, NULL, filter_graph);
114    if (ret < 0) {
115        av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
116        goto end;
117    }
118
119    /* buffer audio sink: to terminate the filter chain. */
120    ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out",
121                                       NULL, NULL, filter_graph);
122    if (ret < 0) {
123        av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
124        goto end;
125    }
126
127    ret = av_opt_set_int_list(buffersink_ctx, "sample_fmts", out_sample_fmts, -1,
128                              AV_OPT_SEARCH_CHILDREN);
129    if (ret < 0) {
130        av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
131        goto end;
132    }
133
134    ret = av_opt_set_int_list(buffersink_ctx, "channel_layouts", out_channel_layouts, -1,
135                              AV_OPT_SEARCH_CHILDREN);
136    if (ret < 0) {
137        av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
138        goto end;
139    }
140
141    ret = av_opt_set_int_list(buffersink_ctx, "sample_rates", out_sample_rates, -1,
142                              AV_OPT_SEARCH_CHILDREN);
143    if (ret < 0) {
144        av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
145        goto end;
146    }
147
148    /* Endpoints for the filter graph. */
149    outputs->name       = av_strdup("in");
150    outputs->filter_ctx = buffersrc_ctx;
151    outputs->pad_idx    = 0;
152    outputs->next       = NULL;
153
154    inputs->name       = av_strdup("out");
155    inputs->filter_ctx = buffersink_ctx;
156    inputs->pad_idx    = 0;
157    inputs->next       = NULL;
158
159    if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
160                                        &inputs, &outputs, NULL)) < 0)
161        goto end;
162
163    if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
164        goto end;
165
166    /* Print summary of the sink buffer
167     * Note: args buffer is reused to store channel layout string */
168    outlink = buffersink_ctx->inputs[0];
169    av_get_channel_layout_string(args, sizeof(args), -1, outlink->channel_layout);
170    av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n",
171           (int)outlink->sample_rate,
172           (char *)av_x_if_null(av_get_sample_fmt_name(outlink->format), "?"),
173           args);
174
175end:
176    avfilter_inout_free(&inputs);
177    avfilter_inout_free(&outputs);
178
179    return ret;
180}
181
182static void print_frame(const AVFrame *frame)
183{
184    const int n = frame->nb_samples * av_get_channel_layout_nb_channels(av_frame_get_channel_layout(frame));
185    const uint16_t *p     = (uint16_t*)frame->data[0];
186    const uint16_t *p_end = p + n;
187
188    while (p < p_end) {
189        fputc(*p    & 0xff, stdout);
190        fputc(*p>>8 & 0xff, stdout);
191        p++;
192    }
193    fflush(stdout);
194}
195
196int main(int argc, char **argv)
197{
198    int ret;
199    AVPacket packet0, packet;
200    AVFrame *frame = av_frame_alloc();
201    AVFrame *filt_frame = av_frame_alloc();
202    int got_frame;
203
204    if (!frame || !filt_frame) {
205        perror("Could not allocate frame");
206        exit(1);
207    }
208    if (argc != 2) {
209        fprintf(stderr, "Usage: %s file | %s\n", argv[0], player);
210        exit(1);
211    }
212
213    av_register_all();
214    avfilter_register_all();
215
216    if ((ret = open_input_file(argv[1])) < 0)
217        goto end;
218    if ((ret = init_filters(filter_descr)) < 0)
219        goto end;
220
221    /* read all packets */
222    packet0.data = NULL;
223    packet.data = NULL;
224    while (1) {
225        if (!packet0.data) {
226            if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
227                break;
228            packet0 = packet;
229        }
230
231        if (packet.stream_index == audio_stream_index) {
232            got_frame = 0;
233            ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, &packet);
234            if (ret < 0) {
235                av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n");
236                continue;
237            }
238            packet.size -= ret;
239            packet.data += ret;
240
241            if (got_frame) {
242                /* push the audio data from decoded frame into the filtergraph */
243                if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, 0) < 0) {
244                    av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n");
245                    break;
246                }
247
248                /* pull filtered audio from the filtergraph */
249                while (1) {
250                    ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
251                    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
252                        break;
253                    if (ret < 0)
254                        goto end;
255                    print_frame(filt_frame);
256                    av_frame_unref(filt_frame);
257                }
258            }
259
260            if (packet.size <= 0)
261                av_free_packet(&packet0);
262        } else {
263            /* discard non-wanted packets */
264            av_free_packet(&packet0);
265        }
266    }
267end:
268    avfilter_graph_free(&filter_graph);
269    avcodec_close(dec_ctx);
270    avformat_close_input(&fmt_ctx);
271    av_frame_free(&frame);
272    av_frame_free(&filt_frame);
273
274    if (ret < 0 && ret != AVERROR_EOF) {
275        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
276        exit(1);
277    }
278
279    exit(0);
280}
281