FFmpeg 7.1.1
Loading...
Searching...
No Matches
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 libavfilter audio filtering API usage example
23 * @example filter_audio.c
24 *
25 * This example will generate a sine wave audio, pass it through a simple filter
26 * chain, and then compute the MD5 checksum of the output data.
27 *
28 * The filter chain it uses is:
29 * (input) -> abuffer -> volume -> aformat -> abuffersink -> (output)
30 *
31 * abuffer: This provides the endpoint where you can feed the decoded samples.
32 * volume: In this example we hardcode it to 0.90.
33 * aformat: This converts the samples to the samplefreq, channel layout,
34 * and sample format required by the audio device.
35 * abuffersink: This provides the endpoint where you can read the samples after
36 * they have passed through the filter chain.
37 */
38
39#include <inttypes.h>
40#include <math.h>
41#include <stdio.h>
42#include <stdlib.h>
43
45#include <libavutil/md5.h>
46#include <libavutil/mem.h>
47#include <libavutil/opt.h>
48#include <libavutil/samplefmt.h>
49
53
54#define INPUT_SAMPLERATE 48000
55#define INPUT_FORMAT AV_SAMPLE_FMT_FLTP
56#define INPUT_CHANNEL_LAYOUT (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0
57
58#define VOLUME_VAL 0.90
59
61 AVFilterContext **sink)
62{
64 AVFilterContext *abuffer_ctx;
65 const AVFilter *abuffer;
66 AVFilterContext *volume_ctx;
67 const AVFilter *volume;
68 AVFilterContext *aformat_ctx;
69 const AVFilter *aformat;
70 AVFilterContext *abuffersink_ctx;
71 const AVFilter *abuffersink;
72
73 AVDictionary *options_dict = NULL;
74 uint8_t options_str[1024];
75 uint8_t ch_layout[64];
76
77 int err;
78
79 /* Create a new filtergraph, which will contain all the filters. */
81 if (!filter_graph) {
82 fprintf(stderr, "Unable to create filter graph.\n");
83 return AVERROR(ENOMEM);
84 }
85
86 /* Create the abuffer filter;
87 * it will be used for feeding the data into the graph. */
88 abuffer = avfilter_get_by_name("abuffer");
89 if (!abuffer) {
90 fprintf(stderr, "Could not find the abuffer filter.\n");
92 }
93
94 abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, "src");
95 if (!abuffer_ctx) {
96 fprintf(stderr, "Could not allocate the abuffer instance.\n");
97 return AVERROR(ENOMEM);
98 }
99
100 /* Set the filter options through the AVOptions API. */
101 av_channel_layout_describe(&INPUT_CHANNEL_LAYOUT, ch_layout, sizeof(ch_layout));
102 av_opt_set (abuffer_ctx, "channel_layout", ch_layout, AV_OPT_SEARCH_CHILDREN);
104 av_opt_set_q (abuffer_ctx, "time_base", (AVRational){ 1, INPUT_SAMPLERATE }, AV_OPT_SEARCH_CHILDREN);
105 av_opt_set_int(abuffer_ctx, "sample_rate", INPUT_SAMPLERATE, AV_OPT_SEARCH_CHILDREN);
106
107 /* Now initialize the filter; we pass NULL options, since we have already
108 * set all the options above. */
109 err = avfilter_init_str(abuffer_ctx, NULL);
110 if (err < 0) {
111 fprintf(stderr, "Could not initialize the abuffer filter.\n");
112 return err;
113 }
114
115 /* Create volume filter. */
116 volume = avfilter_get_by_name("volume");
117 if (!volume) {
118 fprintf(stderr, "Could not find the volume filter.\n");
120 }
121
122 volume_ctx = avfilter_graph_alloc_filter(filter_graph, volume, "volume");
123 if (!volume_ctx) {
124 fprintf(stderr, "Could not allocate the volume instance.\n");
125 return AVERROR(ENOMEM);
126 }
127
128 /* A different way of passing the options is as key/value pairs in a
129 * dictionary. */
130 av_dict_set(&options_dict, "volume", AV_STRINGIFY(VOLUME_VAL), 0);
131 err = avfilter_init_dict(volume_ctx, &options_dict);
132 av_dict_free(&options_dict);
133 if (err < 0) {
134 fprintf(stderr, "Could not initialize the volume filter.\n");
135 return err;
136 }
137
138 /* Create the aformat filter;
139 * it ensures that the output is of the format we want. */
140 aformat = avfilter_get_by_name("aformat");
141 if (!aformat) {
142 fprintf(stderr, "Could not find the aformat filter.\n");
144 }
145
146 aformat_ctx = avfilter_graph_alloc_filter(filter_graph, aformat, "aformat");
147 if (!aformat_ctx) {
148 fprintf(stderr, "Could not allocate the aformat instance.\n");
149 return AVERROR(ENOMEM);
150 }
151
152 /* A third way of passing the options is in a string of the form
153 * key1=value1:key2=value2.... */
154 snprintf(options_str, sizeof(options_str),
155 "sample_fmts=%s:sample_rates=%d:channel_layouts=stereo",
157 err = avfilter_init_str(aformat_ctx, options_str);
158 if (err < 0) {
159 av_log(NULL, AV_LOG_ERROR, "Could not initialize the aformat filter.\n");
160 return err;
161 }
162
163 /* Finally create the abuffersink filter;
164 * it will be used to get the filtered data out of the graph. */
165 abuffersink = avfilter_get_by_name("abuffersink");
166 if (!abuffersink) {
167 fprintf(stderr, "Could not find the abuffersink filter.\n");
169 }
170
171 abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
172 if (!abuffersink_ctx) {
173 fprintf(stderr, "Could not allocate the abuffersink instance.\n");
174 return AVERROR(ENOMEM);
175 }
176
177 /* This filter takes no options. */
178 err = avfilter_init_str(abuffersink_ctx, NULL);
179 if (err < 0) {
180 fprintf(stderr, "Could not initialize the abuffersink instance.\n");
181 return err;
182 }
183
184 /* Connect the filters;
185 * in this simple case the filters just form a linear chain. */
186 err = avfilter_link(abuffer_ctx, 0, volume_ctx, 0);
187 if (err >= 0)
188 err = avfilter_link(volume_ctx, 0, aformat_ctx, 0);
189 if (err >= 0)
190 err = avfilter_link(aformat_ctx, 0, abuffersink_ctx, 0);
191 if (err < 0) {
192 fprintf(stderr, "Error connecting filters\n");
193 return err;
194 }
195
196 /* Configure the graph. */
198 if (err < 0) {
199 av_log(NULL, AV_LOG_ERROR, "Error configuring the filter graph\n");
200 return err;
201 }
202
203 *graph = filter_graph;
204 *src = abuffer_ctx;
205 *sink = abuffersink_ctx;
206
207 return 0;
208}
209
210/* Do something useful with the filtered data: this simple
211 * example just prints the MD5 checksum of each plane to stdout. */
212static int process_output(struct AVMD5 *md5, AVFrame *frame)
213{
214 int planar = av_sample_fmt_is_planar(frame->format);
215 int channels = frame->ch_layout.nb_channels;
216 int planes = planar ? channels : 1;
218 int plane_size = bps * frame->nb_samples * (planar ? 1 : channels);
219 int i, j;
220
221 for (i = 0; i < planes; i++) {
222 uint8_t checksum[16];
223
224 av_md5_init(md5);
225 av_md5_sum(checksum, frame->extended_data[i], plane_size);
226
227 fprintf(stdout, "plane %d: 0x", i);
228 for (j = 0; j < sizeof(checksum); j++)
229 fprintf(stdout, "%02X", checksum[j]);
230 fprintf(stdout, "\n");
231 }
232 fprintf(stdout, "\n");
233
234 return 0;
235}
236
237/* Construct a frame of audio data to be filtered;
238 * this simple example just synthesizes a sine wave. */
239static int get_input(AVFrame *frame, int frame_num)
240{
241 int err, i, j;
242
243#define FRAME_SIZE 1024
244
245 /* Set up the frame properties and allocate the buffer for the data. */
250 frame->pts = frame_num * FRAME_SIZE;
251
252 err = av_frame_get_buffer(frame, 0);
253 if (err < 0)
254 return err;
255
256 /* Fill the data for each channel. */
257 for (i = 0; i < 5; i++) {
258 float *data = (float*)frame->extended_data[i];
259
260 for (j = 0; j < frame->nb_samples; j++)
261 data[j] = sin(2 * M_PI * (frame_num + j) * (i + 1) / FRAME_SIZE);
262 }
263
264 return 0;
265}
266
267int main(int argc, char *argv[])
268{
269 struct AVMD5 *md5;
270 AVFilterGraph *graph;
271 AVFilterContext *src, *sink;
272 AVFrame *frame;
273 uint8_t errstr[1024];
274 float duration;
275 int err, nb_frames, i;
276
277 if (argc < 2) {
278 fprintf(stderr, "Usage: %s <duration>\n", argv[0]);
279 return 1;
280 }
281
282 duration = atof(argv[1]);
283 nb_frames = duration * INPUT_SAMPLERATE / FRAME_SIZE;
284 if (nb_frames <= 0) {
285 fprintf(stderr, "Invalid duration: %s\n", argv[1]);
286 return 1;
287 }
288
289 /* Allocate the frame we will be using to store the data. */
291 if (!frame) {
292 fprintf(stderr, "Error allocating the frame\n");
293 return 1;
294 }
295
296 md5 = av_md5_alloc();
297 if (!md5) {
298 fprintf(stderr, "Error allocating the MD5 context\n");
299 return 1;
300 }
301
302 /* Set up the filtergraph. */
303 err = init_filter_graph(&graph, &src, &sink);
304 if (err < 0) {
305 fprintf(stderr, "Unable to init filter graph:");
306 goto fail;
307 }
308
309 /* the main filtering loop */
310 for (i = 0; i < nb_frames; i++) {
311 /* get an input frame to be filtered */
312 err = get_input(frame, i);
313 if (err < 0) {
314 fprintf(stderr, "Error generating input frame:");
315 goto fail;
316 }
317
318 /* Send the frame to the input of the filtergraph. */
319 err = av_buffersrc_add_frame(src, frame);
320 if (err < 0) {
322 fprintf(stderr, "Error submitting the frame to the filtergraph:");
323 goto fail;
324 }
325
326 /* Get all the filtered output that is available. */
327 while ((err = av_buffersink_get_frame(sink, frame)) >= 0) {
328 /* now do something with our filtered frame */
329 err = process_output(md5, frame);
330 if (err < 0) {
331 fprintf(stderr, "Error processing the filtered frame:");
332 goto fail;
333 }
335 }
336
337 if (err == AVERROR(EAGAIN)) {
338 /* Need to feed more frames in. */
339 continue;
340 } else if (err == AVERROR_EOF) {
341 /* Nothing more to do, finish. */
342 break;
343 } else if (err < 0) {
344 /* An error occurred. */
345 fprintf(stderr, "Error filtering the data:");
346 goto fail;
347 }
348 }
349
350 avfilter_graph_free(&graph);
352 av_freep(&md5);
353
354 return 0;
355
356fail:
357 av_strerror(err, errstr, sizeof(errstr));
358 fprintf(stderr, "%s\n", errstr);
359 return 1;
360}
Main libavfilter public API header.
int main(int argc, char *argv[])
memory buffer sink API for audio and video
Memory buffer source API.
Public libavutil channel layout APIs header.
AVFilterGraph * filter_graph
static AVFrame * frame
#define INPUT_SAMPLERATE
static int get_input(AVFrame *frame, int frame_num)
static int process_output(struct AVMD5 *md5, AVFrame *frame)
static int init_filter_graph(AVFilterGraph **graph, AVFilterContext **src, AVFilterContext **sink)
#define INPUT_CHANNEL_LAYOUT
#define VOLUME_VAL
#define INPUT_FORMAT
#define FRAME_SIZE
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.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
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:94
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:60
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:57
#define AVERROR(e)
Definition error.h:45
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:180
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.
void av_md5_sum(uint8_t *dst, const uint8_t *src, size_t len)
Hash an array of data.
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
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:58
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition opt.h:605
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:66
#define M_PI
Definition mathematics.h:67
Public header for MD5 hash function implementation.
Memory handling functions.
AVOptions.
int nb_channels
Number of channels in this layout.
An instance of a filter.
Definition avfilter.h:457
Filter definition.
Definition avfilter.h:201
This structure describes decoded (raw) audio or video data.
Definition frame.h:389
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:469
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:501
int sample_rate
Sample rate of the audio data.
Definition frame.h:588
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition frame.h:790
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:476
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:450
Rational number (pair of numerator and denominator).
Definition rational.h:58