libavfilter: example audio filtering program
[libav.git] / doc / examples / filter_audio.c
1 /*
2 * copyright (c) 2013 Andrew Kelley
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * libavfilter API usage example.
24 *
25 * @example filter_audio.c
26 * This example will generate a sine wave audio,
27 * pass it through a simple filter chain, and then compute the MD5 checksum of
28 * the output data.
29 *
30 * The filter chain it uses is:
31 * (input) -> abuffer -> volume -> aformat -> abuffersink -> (output)
32 *
33 * abuffer: This provides the endpoint where you can feed the decoded samples.
34 * volume: In this example we hardcode it to 0.90.
35 * aformat: This converts the samples to the samplefreq, channel layout,
36 * and sample format required by the audio device.
37 * abuffersink: This provides the endpoint where you can read the samples after
38 * they have passed through the filter chain.
39 */
40
41 #include <inttypes.h>
42 #include <math.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45
46 #include "libavutil/channel_layout.h"
47 #include "libavutil/md5.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/samplefmt.h"
50
51 #include "libavfilter/avfilter.h"
52 #include "libavfilter/buffersink.h"
53 #include "libavfilter/buffersrc.h"
54
55 #define INPUT_SAMPLERATE 48000
56 #define INPUT_FORMAT AV_SAMPLE_FMT_FLTP
57 #define INPUT_CHANNEL_LAYOUT AV_CH_LAYOUT_5POINT0
58
59 #define VOLUME_VAL 0.90
60
61 static int init_filter_graph(AVFilterGraph **graph, AVFilterContext **src,
62 AVFilterContext **sink)
63 {
64 AVFilterGraph *filter_graph;
65 AVFilterContext *abuffer_ctx;
66 AVFilter *abuffer;
67 AVFilterContext *volume_ctx;
68 AVFilter *volume;
69 AVFilterContext *aformat_ctx;
70 AVFilter *aformat;
71 AVFilterContext *abuffersink_ctx;
72 AVFilter *abuffersink;
73
74 AVDictionary *options_dict = NULL;
75 uint8_t options_str[1024];
76 uint8_t ch_layout[64];
77
78 int err;
79
80 /* Create a new filtergraph, which will contain all the filters. */
81 filter_graph = avfilter_graph_alloc();
82 if (!filter_graph) {
83 fprintf(stderr, "Unable to create filter graph.\n");
84 return AVERROR(ENOMEM);
85 }
86
87 /* Create the abuffer filter;
88 * it will be used for feeding the data into the graph. */
89 abuffer = avfilter_get_by_name("abuffer");
90 if (!abuffer) {
91 fprintf(stderr, "Could not find the abuffer filter.\n");
92 return AVERROR_FILTER_NOT_FOUND;
93 }
94
95 abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, "src");
96 if (!abuffer_ctx) {
97 fprintf(stderr, "Could not allocate the abuffer instance.\n");
98 return AVERROR(ENOMEM);
99 }
100
101 /* Set the filter options through the AVOptions API. */
102 av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, INPUT_CHANNEL_LAYOUT);
103 av_opt_set (abuffer_ctx, "channel_layout", ch_layout, AV_OPT_SEARCH_CHILDREN);
104 av_opt_set (abuffer_ctx, "sample_fmt", av_get_sample_fmt_name(INPUT_FORMAT), AV_OPT_SEARCH_CHILDREN);
105 av_opt_set_q (abuffer_ctx, "time_base", (AVRational){ 1, INPUT_SAMPLERATE }, AV_OPT_SEARCH_CHILDREN);
106 av_opt_set_int(abuffer_ctx, "sample_rate", INPUT_SAMPLERATE, AV_OPT_SEARCH_CHILDREN);
107
108 /* Now initialize the filter; we pass NULL options, since we have already
109 * set all the options above. */
110 err = avfilter_init_str(abuffer_ctx, NULL);
111 if (err < 0) {
112 fprintf(stderr, "Could not initialize the abuffer filter.\n");
113 return err;
114 }
115
116 /* Create volume filter. */
117 volume = avfilter_get_by_name("volume");
118 if (!volume) {
119 fprintf(stderr, "Could not find the volume filter.\n");
120 return AVERROR_FILTER_NOT_FOUND;
121 }
122
123 volume_ctx = avfilter_graph_alloc_filter(filter_graph, volume, "volume");
124 if (!volume_ctx) {
125 fprintf(stderr, "Could not allocate the volume instance.\n");
126 return AVERROR(ENOMEM);
127 }
128
129 /* A different way of passing the options is as key/value pairs in a
130 * dictionary. */
131 av_dict_set(&options_dict, "volume", AV_STRINGIFY(VOLUME_VAL), 0);
132 err = avfilter_init_dict(volume_ctx, &options_dict);
133 av_dict_free(&options_dict);
134 if (err < 0) {
135 fprintf(stderr, "Could not initialize the volume filter.\n");
136 return err;
137 }
138
139 /* Create the aformat filter;
140 * it ensures that the output is of the format we want. */
141 aformat = avfilter_get_by_name("aformat");
142 if (!aformat) {
143 fprintf(stderr, "Could not find the aformat filter.\n");
144 return AVERROR_FILTER_NOT_FOUND;
145 }
146
147 aformat_ctx = avfilter_graph_alloc_filter(filter_graph, aformat, "aformat");
148 if (!aformat_ctx) {
149 fprintf(stderr, "Could not allocate the aformat instance.\n");
150 return AVERROR(ENOMEM);
151 }
152
153 /* A third way of passing the options is in a string of the form
154 * key1=value1:key2=value2.... */
155 snprintf(options_str, sizeof(options_str),
156 "sample_fmts=%s:sample_rates=%d:channel_layouts=0x%"PRIx64,
157 av_get_sample_fmt_name(AV_SAMPLE_FMT_S16), 44100,
158 (uint64_t)AV_CH_LAYOUT_STEREO);
159 err = avfilter_init_str(aformat_ctx, options_str);
160 if (err < 0) {
161 av_log(NULL, AV_LOG_ERROR, "Could not initialize the aformat filter.\n");
162 return err;
163 }
164
165 /* Finally create the abuffersink filter;
166 * it will be used to get the filtered data out of the graph. */
167 abuffersink = avfilter_get_by_name("abuffersink");
168 if (!abuffersink) {
169 fprintf(stderr, "Could not find the abuffersink filter.\n");
170 return AVERROR_FILTER_NOT_FOUND;
171 }
172
173 abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
174 if (!abuffersink_ctx) {
175 fprintf(stderr, "Could not allocate the abuffersink instance.\n");
176 return AVERROR(ENOMEM);
177 }
178
179 /* This filter takes no options. */
180 err = avfilter_init_str(abuffersink_ctx, NULL);
181 if (err < 0) {
182 fprintf(stderr, "Could not initialize the abuffersink instance.\n");
183 return err;
184 }
185
186 /* Connect the filters;
187 * in this simple case the filters just form a linear chain. */
188 err = avfilter_link(abuffer_ctx, 0, volume_ctx, 0);
189 if (err >= 0)
190 err = avfilter_link(volume_ctx, 0, aformat_ctx, 0);
191 if (err >= 0)
192 err = avfilter_link(aformat_ctx, 0, abuffersink_ctx, 0);
193 if (err < 0) {
194 fprintf(stderr, "Error connecting filters\n");
195 return err;
196 }
197
198 /* Configure the graph. */
199 err = avfilter_graph_config(filter_graph, NULL);
200 if (err < 0) {
201 av_log(NULL, AV_LOG_ERROR, "Error configuring the filter graph\n");
202 return err;
203 }
204
205 *graph = filter_graph;
206 *src = abuffer_ctx;
207 *sink = abuffersink_ctx;
208
209 return 0;
210 }
211
212 /* Do something useful with the filtered data: this simple
213 * example just prints the MD5 checksum of each plane to stdout. */
214 static int process_output(struct AVMD5 *md5, AVFrame *frame)
215 {
216 int planar = av_sample_fmt_is_planar(frame->format);
217 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
218 int planes = planar ? channels : 1;
219 int bps = av_get_bytes_per_sample(frame->format);
220 int plane_size = bps * frame->nb_samples * (planar ? 1 : channels);
221 int i, j;
222
223 for (i = 0; i < planes; i++) {
224 uint8_t checksum[16];
225
226 av_md5_init(md5);
227 av_md5_sum(checksum, frame->extended_data[i], plane_size);
228
229 fprintf(stdout, "plane %d: 0x", i);
230 for (j = 0; j < sizeof(checksum); j++)
231 fprintf(stdout, "%02X", checksum[j]);
232 fprintf(stdout, "\n");
233 }
234 fprintf(stdout, "\n");
235
236 return 0;
237 }
238
239 /* Construct a frame of audio data to be filtered;
240 * this simple example just synthesizes a sine wave. */
241 static int get_input(AVFrame *frame, int frame_num)
242 {
243 int err, i, j;
244
245 #define FRAME_SIZE 1024
246
247 /* Set up the frame properties and allocate the buffer for the data. */
248 frame->sample_rate = INPUT_SAMPLERATE;
249 frame->format = INPUT_FORMAT;
250 frame->channel_layout = INPUT_CHANNEL_LAYOUT;
251 frame->nb_samples = FRAME_SIZE;
252 frame->pts = frame_num * FRAME_SIZE;
253
254 err = av_frame_get_buffer(frame, 0);
255 if (err < 0)
256 return err;
257
258 /* Fill the data for each channel. */
259 for (i = 0; i < 5; i++) {
260 float *data = (float*)frame->extended_data[i];
261
262 for (j = 0; j < frame->nb_samples; j++)
263 data[j] = sin(2 * M_PI * (frame_num + j) * (i + 1) / FRAME_SIZE);
264 }
265
266 return 0;
267 }
268
269 int main(int argc, char *argv[])
270 {
271 struct AVMD5 *md5;
272 AVFilterGraph *graph;
273 AVFilterContext *src, *sink;
274 AVFrame *frame;
275 uint8_t errstr[1024];
276 float duration;
277 int err, nb_frames, i;
278
279 if (argc < 2) {
280 fprintf(stderr, "Usage: %s <duration>\n", argv[0]);
281 return 1;
282 }
283
284 duration = atof(argv[1]);
285 nb_frames = duration * INPUT_SAMPLERATE / FRAME_SIZE;
286 if (nb_frames <= 0) {
287 fprintf(stderr, "Invalid duration: %s\n", argv[1]);
288 return 1;
289 }
290
291 avfilter_register_all();
292
293 /* Allocate the frame we will be using to store the data. */
294 frame = av_frame_alloc();
295 if (!frame) {
296 fprintf(stderr, "Error allocating the frame\n");
297 return 1;
298 }
299
300 md5 = av_md5_alloc();
301 if (!md5) {
302 fprintf(stderr, "Error allocating the MD5 context\n");
303 return 1;
304 }
305
306 /* Set up the filtergraph. */
307 err = init_filter_graph(&graph, &src, &sink);
308 if (err < 0) {
309 fprintf(stderr, "Unable to init filter graph:");
310 goto fail;
311 }
312
313 /* the main filtering loop */
314 for (i = 0; i < nb_frames; i++) {
315 /* get an input frame to be filtered */
316 err = get_input(frame, i);
317 if (err < 0) {
318 fprintf(stderr, "Error generating input frame:");
319 goto fail;
320 }
321
322 /* Send the frame to the input of the filtergraph. */
323 err = av_buffersrc_add_frame(src, frame);
324 if (err < 0) {
325 av_frame_unref(frame);
326 fprintf(stderr, "Error submitting the frame to the filtergraph:");
327 goto fail;
328 }
329
330 /* Get all the filtered output that is available. */
331 while ((err = av_buffersink_get_frame(sink, frame)) >= 0) {
332 /* now do something with our filtered frame */
333 err = process_output(md5, frame);
334 if (err < 0) {
335 fprintf(stderr, "Error processing the filtered frame:");
336 goto fail;
337 }
338 av_frame_unref(frame);
339 }
340
341 if (err == AVERROR(EAGAIN)) {
342 /* Need to feed more frames in. */
343 continue;
344 } else if (err == AVERROR_EOF) {
345 /* Nothing more to do, finish. */
346 break;
347 } else if (err < 0) {
348 /* An error occurred. */
349 fprintf(stderr, "Error filtering the data:");
350 goto fail;
351 }
352 }
353
354 avfilter_graph_free(&graph);
355 av_frame_free(&frame);
356 av_freep(&md5);
357
358 return 0;
359
360 fail:
361 av_strerror(err, errstr, sizeof(errstr));
362 fprintf(stderr, "%s\n", errstr);
363 return 1;
364 }