FFmpeg 4.2.2
filter_audio.c
Go to the documentation of this file.
1/*
2 * copyright (c) 2013 Andrew Kelley
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
47#include "libavutil/md5.h"
48#include "libavutil/mem.h"
49#include "libavutil/opt.h"
50#include "libavutil/samplefmt.h"
51
55
56#define INPUT_SAMPLERATE 48000
57#define INPUT_FORMAT AV_SAMPLE_FMT_FLTP
58#define INPUT_CHANNEL_LAYOUT AV_CH_LAYOUT_5POINT0
59
60#define VOLUME_VAL 0.90
61
63 AVFilterContext **sink)
64{
66 AVFilterContext *abuffer_ctx;
67 const AVFilter *abuffer;
68 AVFilterContext *volume_ctx;
69 const AVFilter *volume;
70 AVFilterContext *aformat_ctx;
71 const AVFilter *aformat;
72 AVFilterContext *abuffersink_ctx;
73 const AVFilter *abuffersink;
74
75 AVDictionary *options_dict = NULL;
76 uint8_t options_str[1024];
77 uint8_t ch_layout[64];
78
79 int err;
80
81 /* Create a new filtergraph, which will contain all the filters. */
83 if (!filter_graph) {
84 fprintf(stderr, "Unable to create filter graph.\n");
85 return AVERROR(ENOMEM);
86 }
87
88 /* Create the abuffer filter;
89 * it will be used for feeding the data into the graph. */
90 abuffer = avfilter_get_by_name("abuffer");
91 if (!abuffer) {
92 fprintf(stderr, "Could not find the abuffer filter.\n");
94 }
95
96 abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, "src");
97 if (!abuffer_ctx) {
98 fprintf(stderr, "Could not allocate the abuffer instance.\n");
99 return AVERROR(ENOMEM);
100 }
101
102 /* Set the filter options through the AVOptions API. */
103 av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, INPUT_CHANNEL_LAYOUT);
104 av_opt_set (abuffer_ctx, "channel_layout", ch_layout, AV_OPT_SEARCH_CHILDREN);
106 av_opt_set_q (abuffer_ctx, "time_base", (AVRational){ 1, INPUT_SAMPLERATE }, AV_OPT_SEARCH_CHILDREN);
107 av_opt_set_int(abuffer_ctx, "sample_rate", INPUT_SAMPLERATE, AV_OPT_SEARCH_CHILDREN);
108
109 /* Now initialize the filter; we pass NULL options, since we have already
110 * set all the options above. */
111 err = avfilter_init_str(abuffer_ctx, NULL);
112 if (err < 0) {
113 fprintf(stderr, "Could not initialize the abuffer filter.\n");
114 return err;
115 }
116
117 /* Create volume filter. */
118 volume = avfilter_get_by_name("volume");
119 if (!volume) {
120 fprintf(stderr, "Could not find the volume filter.\n");
122 }
123
124 volume_ctx = avfilter_graph_alloc_filter(filter_graph, volume, "volume");
125 if (!volume_ctx) {
126 fprintf(stderr, "Could not allocate the volume instance.\n");
127 return AVERROR(ENOMEM);
128 }
129
130 /* A different way of passing the options is as key/value pairs in a
131 * dictionary. */
132 av_dict_set(&options_dict, "volume", AV_STRINGIFY(VOLUME_VAL), 0);
133 err = avfilter_init_dict(volume_ctx, &options_dict);
134 av_dict_free(&options_dict);
135 if (err < 0) {
136 fprintf(stderr, "Could not initialize the volume filter.\n");
137 return err;
138 }
139
140 /* Create the aformat filter;
141 * it ensures that the output is of the format we want. */
142 aformat = avfilter_get_by_name("aformat");
143 if (!aformat) {
144 fprintf(stderr, "Could not find the aformat filter.\n");
146 }
147
148 aformat_ctx = avfilter_graph_alloc_filter(filter_graph, aformat, "aformat");
149 if (!aformat_ctx) {
150 fprintf(stderr, "Could not allocate the aformat instance.\n");
151 return AVERROR(ENOMEM);
152 }
153
154 /* A third way of passing the options is in a string of the form
155 * key1=value1:key2=value2.... */
156 snprintf(options_str, sizeof(options_str),
157 "sample_fmts=%s:sample_rates=%d:channel_layouts=0x%"PRIx64,
159 (uint64_t)AV_CH_LAYOUT_STEREO);
160 err = avfilter_init_str(aformat_ctx, options_str);
161 if (err < 0) {
162 av_log(NULL, AV_LOG_ERROR, "Could not initialize the aformat filter.\n");
163 return err;
164 }
165
166 /* Finally create the abuffersink filter;
167 * it will be used to get the filtered data out of the graph. */
168 abuffersink = avfilter_get_by_name("abuffersink");
169 if (!abuffersink) {
170 fprintf(stderr, "Could not find the abuffersink filter.\n");
172 }
173
174 abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
175 if (!abuffersink_ctx) {
176 fprintf(stderr, "Could not allocate the abuffersink instance.\n");
177 return AVERROR(ENOMEM);
178 }
179
180 /* This filter takes no options. */
181 err = avfilter_init_str(abuffersink_ctx, NULL);
182 if (err < 0) {
183 fprintf(stderr, "Could not initialize the abuffersink instance.\n");
184 return err;
185 }
186
187 /* Connect the filters;
188 * in this simple case the filters just form a linear chain. */
189 err = avfilter_link(abuffer_ctx, 0, volume_ctx, 0);
190 if (err >= 0)
191 err = avfilter_link(volume_ctx, 0, aformat_ctx, 0);
192 if (err >= 0)
193 err = avfilter_link(aformat_ctx, 0, abuffersink_ctx, 0);
194 if (err < 0) {
195 fprintf(stderr, "Error connecting filters\n");
196 return err;
197 }
198
199 /* Configure the graph. */
201 if (err < 0) {
202 av_log(NULL, AV_LOG_ERROR, "Error configuring the filter graph\n");
203 return err;
204 }
205
206 *graph = filter_graph;
207 *src = abuffer_ctx;
208 *sink = abuffersink_ctx;
209
210 return 0;
211}
212
213/* Do something useful with the filtered data: this simple
214 * example just prints the MD5 checksum of each plane to stdout. */
215static int process_output(struct AVMD5 *md5, AVFrame *frame)
216{
217 int planar = av_sample_fmt_is_planar(frame->format);
219 int planes = planar ? channels : 1;
221 int plane_size = bps * frame->nb_samples * (planar ? 1 : channels);
222 int i, j;
223
224 for (i = 0; i < planes; i++) {
225 uint8_t checksum[16];
226
227 av_md5_init(md5);
228 av_md5_sum(checksum, frame->extended_data[i], plane_size);
229
230 fprintf(stdout, "plane %d: 0x", i);
231 for (j = 0; j < sizeof(checksum); j++)
232 fprintf(stdout, "%02X", checksum[j]);
233 fprintf(stdout, "\n");
234 }
235 fprintf(stdout, "\n");
236
237 return 0;
238}
239
240/* Construct a frame of audio data to be filtered;
241 * this simple example just synthesizes a sine wave. */
242static int get_input(AVFrame *frame, int frame_num)
243{
244 int err, i, j;
245
246#define FRAME_SIZE 1024
247
248 /* Set up the frame properties and allocate the buffer for the data. */
253 frame->pts = frame_num * FRAME_SIZE;
254
255 err = av_frame_get_buffer(frame, 0);
256 if (err < 0)
257 return err;
258
259 /* Fill the data for each channel. */
260 for (i = 0; i < 5; i++) {
261 float *data = (float*)frame->extended_data[i];
262
263 for (j = 0; j < frame->nb_samples; j++)
264 data[j] = sin(2 * M_PI * (frame_num + j) * (i + 1) / FRAME_SIZE);
265 }
266
267 return 0;
268}
269
270int main(int argc, char *argv[])
271{
272 struct AVMD5 *md5;
273 AVFilterGraph *graph;
274 AVFilterContext *src, *sink;
275 AVFrame *frame;
276 uint8_t errstr[1024];
277 float duration;
278 int err, nb_frames, i;
279
280 if (argc < 2) {
281 fprintf(stderr, "Usage: %s <duration>\n", argv[0]);
282 return 1;
283 }
284
285 duration = atof(argv[1]);
286 nb_frames = duration * INPUT_SAMPLERATE / FRAME_SIZE;
287 if (nb_frames <= 0) {
288 fprintf(stderr, "Invalid duration: %s\n", argv[1]);
289 return 1;
290 }
291
292 /* Allocate the frame we will be using to store the data. */
294 if (!frame) {
295 fprintf(stderr, "Error allocating the frame\n");
296 return 1;
297 }
298
299 md5 = av_md5_alloc();
300 if (!md5) {
301 fprintf(stderr, "Error allocating the MD5 context\n");
302 return 1;
303 }
304
305 /* Set up the filtergraph. */
306 err = init_filter_graph(&graph, &src, &sink);
307 if (err < 0) {
308 fprintf(stderr, "Unable to init filter graph:");
309 goto fail;
310 }
311
312 /* the main filtering loop */
313 for (i = 0; i < nb_frames; i++) {
314 /* get an input frame to be filtered */
315 err = get_input(frame, i);
316 if (err < 0) {
317 fprintf(stderr, "Error generating input frame:");
318 goto fail;
319 }
320
321 /* Send the frame to the input of the filtergraph. */
322 err = av_buffersrc_add_frame(src, frame);
323 if (err < 0) {
325 fprintf(stderr, "Error submitting the frame to the filtergraph:");
326 goto fail;
327 }
328
329 /* Get all the filtered output that is available. */
330 while ((err = av_buffersink_get_frame(sink, frame)) >= 0) {
331 /* now do something with our filtered frame */
332 err = process_output(md5, frame);
333 if (err < 0) {
334 fprintf(stderr, "Error processing the filtered frame:");
335 goto fail;
336 }
338 }
339
340 if (err == AVERROR(EAGAIN)) {
341 /* Need to feed more frames in. */
342 continue;
343 } else if (err == AVERROR_EOF) {
344 /* Nothing more to do, finish. */
345 break;
346 } else if (err < 0) {
347 /* An error occurred. */
348 fprintf(stderr, "Error filtering the data:");
349 goto fail;
350 }
351 }
352
353 avfilter_graph_free(&graph);
355 av_freep(&md5);
356
357 return 0;
358
359fail:
360 av_strerror(err, errstr, sizeof(errstr));
361 fprintf(stderr, "%s\n", errstr);
362 return 1;
363}
Main libavfilter public API header.
memory buffer sink API for audio and video
Memory buffer source API.
audio channel layout utility functions
static AVFrame * frame
int main(int argc, char *argv[])
Definition: filter_audio.c:270
#define INPUT_SAMPLERATE
Definition: filter_audio.c:56
static int get_input(AVFrame *frame, int frame_num)
Definition: filter_audio.c:242
static int process_output(struct AVMD5 *md5, AVFrame *frame)
Definition: filter_audio.c:215
static int init_filter_graph(AVFilterGraph **graph, AVFilterContext **src, AVFilterContext **sink)
Definition: filter_audio.c:62
#define INPUT_CHANNEL_LAYOUT
Definition: filter_audio.c:58
#define VOLUME_VAL
Definition: filter_audio.c:60
#define INPUT_FORMAT
Definition: filter_audio.c:57
#define FRAME_SIZE
AVFilterGraph * filter_graph
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:556
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
#define AV_CH_LAYOUT_STEREO
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
Get a frame with filtered data from sink and put it in frame.
av_warn_unused_result int av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
Add a frame to the buffer source.
int avfilter_init_str(AVFilterContext *ctx, const char *args)
Initialize a filter with the supplied parameters.
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
AVFilterContext * avfilter_graph_alloc_filter(AVFilterGraph *graph, const AVFilter *filter, const char *name)
Create a new filter instance in a filter graph.
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
Initialize a filter with the supplied dictionary of options.
int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)
Link two filters together.
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values.
struct AVDictionary AVDictionary
Definition: dict.h:86
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AVERROR_FILTER_NOT_FOUND
Filter not found.
Definition: error.h:58
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level.
void av_md5_init(struct AVMD5 *ctx)
Initialize MD5 hashing.
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
void av_md5_sum(uint8_t *dst, const uint8_t *src, const int len)
Hash an array of data.
void av_freep(void *ptr)
Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family,...
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
int av_opt_set_q(void *obj, const char *name, AVRational val, int search_flags)
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
#define AV_STRINGIFY(s)
Definition: macros.h:36
#define M_PI
Definition: mathematics.h:52
Public header for MD5 hash function implementation.
Memory handling functions.
AVOptions.
An instance of a filter.
Definition: avfilter.h:338
Filter definition.
Definition: avfilter.h:144
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:361
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:472
int sample_rate
Sample rate of the audio data.
Definition: frame.h:467
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:368
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:342
Rational number (pair of numerator and denominator).
Definition: rational.h:58