[PATCH 1/2] lavfi/abuffer: init the data planes with NULL pointers.
From: Clément Bœsch <clement.boesch@smartjog.com> Samples buffer ref is allocated and loaded with the uninitialized data pointers: av_asrc_buffer_add_buffer() -> av_asrc_buffer_add_samples() -> avfilter_get_audio_buffer_ref_from_arrays(data, ...) ...which leads to a crash with at least lavfi/ashowinfo in case of !NULL (see the for loop while samplesref->data[plane]). --- libavfilter/asrc_abuffer.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/libavfilter/asrc_abuffer.c b/libavfilter/asrc_abuffer.c index ad71e12..5eca785 100644 --- a/libavfilter/asrc_abuffer.c +++ b/libavfilter/asrc_abuffer.c @@ -234,7 +234,7 @@ int av_asrc_buffer_add_buffer(AVFilterContext *ctx, int sample_fmt, int64_t channel_layout, int planar, int64_t pts, int av_unused flags) { - uint8_t *data[8]; + uint8_t *data[8] = {0}; int linesize[8]; int nb_channels = av_get_channel_layout_nb_channels(channel_layout), nb_samples = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt); -- 1.7.9
From: Clément Bœsch <clement.boesch@smartjog.com> Mostly based on doc/examples/filtering.c. lavfi API is still limited to "buffer feeding" instead of "frame feeding" at the moment, so this example code sticks with it. --- doc/examples/Makefile | 2 +- doc/examples/filtering-audio.c | 244 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+), 1 deletions(-) create mode 100644 doc/examples/filtering-audio.c diff --git a/doc/examples/Makefile b/doc/examples/Makefile index b4d299f..135fa95 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -3,7 +3,7 @@ FFMPEG_LIBS=libavdevice libavformat libavfilter libavcodec libswscale libavutil CFLAGS+=-Wall $(shell pkg-config --cflags $(FFMPEG_LIBS)) LDFLAGS+=$(shell pkg-config --libs $(FFMPEG_LIBS)) -EXAMPLES=decoding_encoding filtering metadata muxing +EXAMPLES=decoding_encoding filtering filtering-audio metadata muxing OBJS=$(addsuffix .o,$(EXAMPLES)) diff --git a/doc/examples/filtering-audio.c b/doc/examples/filtering-audio.c new file mode 100644 index 0000000..738b186 --- /dev/null +++ b/doc/examples/filtering-audio.c @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2010 Nicolas George + * Copyright (c) 2011 Stefano Sabatini + * Copyright (c) 2012 Clément Bœsch + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @file + * API example for audio decoding and filtering + */ + +#include <unistd.h> + +#include <libavcodec/avcodec.h> +#include <libavformat/avformat.h> +#include <libavfilter/asrc_abuffer.h> +#include <libavfilter/avfiltergraph.h> +#include <libavfilter/avcodec.h> +#include <libavfilter/buffersink.h> + +const char *filter_descr = "aresample=8000,aconvert=s16:mono"; +const char *player = "ffplay -f s16le -ar 8000 -ac 1 -i /dev/stdin"; + +static AVFormatContext *fmt_ctx; +static AVCodecContext *dec_ctx; +AVFilterContext *buffersink_ctx; +AVFilterContext *buffersrc_ctx; +AVFilterGraph *filter_graph; +static int audio_stream_index = -1; + +static int open_input_file(const char *filename) +{ + int ret; + AVCodec *dec; + + if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n"); + return ret; + } + + if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n"); + return ret; + } + + /* select the audio stream */ + ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot find a audio stream in the input file\n"); + return ret; + } + audio_stream_index = ret; + dec_ctx = fmt_ctx->streams[audio_stream_index]->codec; + + /* init the audio decoder */ + if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot open audio decoder\n"); + return ret; + } + + return 0; +} + +static int init_filters(const char *filters_descr) +{ + char args[512]; + int ret; + AVFilter *buffersrc = avfilter_get_by_name("abuffer"); + AVFilter *buffersink = avfilter_get_by_name("abuffersink"); + AVFilterInOut *outputs = avfilter_inout_alloc(); + AVFilterInOut *inputs = avfilter_inout_alloc(); + const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 }; + const int packing_fmts[] = { AVFILTER_PACKED, -1 }; + const int64_t *chlayouts = avfilter_all_channel_layouts; + AVABufferSinkParams *abuffersink_params; + const AVFilterLink *outlink; + + filter_graph = avfilter_graph_alloc(); + + /* buffer audio source: the decoded frames from the decoder will be inserted here. */ + if (!dec_ctx->channel_layout) + dec_ctx->channel_layout = av_get_default_channel_layout(dec_ctx->channels); + snprintf(args, sizeof(args), "%d:%d:0x%"PRIx64":packed", + dec_ctx->sample_rate, dec_ctx->sample_fmt, dec_ctx->channel_layout); + ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", + args, NULL, filter_graph); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n"); + return ret; + } + + /* buffer audio sink: to terminate the filter chain. */ + abuffersink_params = av_abuffersink_params_alloc(); + abuffersink_params->sample_fmts = sample_fmts; + abuffersink_params->channel_layouts = chlayouts; + abuffersink_params->packing_fmts = packing_fmts; + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, abuffersink_params, filter_graph); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n"); + return ret; + } + + /* Endpoints for the filter graph. */ + outputs->name = av_strdup("in"); + outputs->filter_ctx = buffersrc_ctx; + outputs->pad_idx = 0; + outputs->next = NULL; + + inputs->name = av_strdup("out"); + inputs->filter_ctx = buffersink_ctx; + inputs->pad_idx = 0; + inputs->next = NULL; + + if ((ret = avfilter_graph_parse(filter_graph, filter_descr, + &inputs, &outputs, NULL)) < 0) + return ret; + + if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) + return ret; + + outlink = buffersink_ctx->inputs[0]; + // abuse args buffer to store channel layout string + av_get_channel_layout_string(args, sizeof(args), -1, outlink->channel_layout); + av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n", + (int)outlink->sample_rate, + (char *)av_x_if_null(av_get_sample_fmt_name(outlink->format), "?"), + args); + + return 0; +} + +static void print_samplesref(AVFilterBufferRef *samplesref) +{ + const AVFilterBufferRefAudioProps *props = samplesref->audio; + const int n = props->nb_samples * av_get_channel_layout_nb_channels(props->channel_layout); + const uint16_t *p = (uint16_t*)samplesref->data[0]; + const uint16_t *p_end = p + n; + + while (p < p_end) { + fputc(*p & 0xff, stdout); + fputc(*p>>8 & 0xff, stdout); + p++; + } + fflush(stdout); +} + +int main(int argc, char **argv) +{ + int ret; + AVPacket packet; + AVFrame frame; + int got_frame; + + if (argc != 2) { + fprintf(stderr, "Usage: %s file | %s\n", argv[0], player); + exit(1); + } + + avcodec_register_all(); + av_register_all(); + avfilter_register_all(); + + if ((ret = open_input_file(argv[1])) < 0) + goto end; + if ((ret = init_filters(filter_descr)) < 0) + goto end; + + /* read all packets */ + while (1) { + AVFilterBufferRef *samplesref; + if ((ret = av_read_frame(fmt_ctx, &packet)) < 0) + break; + + if (packet.stream_index == audio_stream_index) { + avcodec_get_frame_defaults(&frame); + got_frame = 0; + ret = avcodec_decode_audio4(dec_ctx, &frame, &got_frame, &packet); + av_free_packet(&packet); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n"); + break; + } + + if (got_frame) { + const int bps = av_get_bytes_per_sample(dec_ctx->sample_fmt); + const int decoded_data_size = frame.nb_samples * dec_ctx->channels * bps; + + /* push the audio data from decoded frame into the filtergraph */ + if (av_asrc_buffer_add_buffer(buffersrc_ctx, + frame.data[0], + decoded_data_size, + dec_ctx->sample_rate, + dec_ctx->sample_fmt, + dec_ctx->channel_layout, + 0, frame.pts, 0) < 0) { + av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n"); + exit(1); + } + + /* pull filtered pictures from the filtergraph */ + while (avfilter_poll_frame(buffersink_ctx->inputs[0])) { + av_buffersink_get_buffer_ref(buffersink_ctx, &samplesref, 0); + if (samplesref) { + print_samplesref(samplesref); + avfilter_unref_buffer(samplesref); + } + } + } + } + } +end: + avfilter_graph_free(&filter_graph); + if (dec_ctx) + avcodec_close(dec_ctx); + avformat_close_input(&fmt_ctx); + + if (ret < 0 && ret != AVERROR_EOF) { + char buf[1024]; + av_strerror(ret, buf, sizeof(buf)); + fprintf(stderr, "Error occurred: %s\n", buf); + exit(1); + } + + exit(0); +} -- 1.7.9
2012/2/20 Clément Bœsch <ubitux@gmail.com>:
From: Clément Bœsch <clement.boesch@smartjog.com>
Mostly based on doc/examples/filtering.c. lavfi API is still limited to "buffer feeding" instead of "frame feeding" at the moment, so this example code sticks with it.
Thanks a lot for this example!
+ /* pull filtered pictures from the filtergraph */
That's not "pictures". -- Andrey Utkin
On Mon, Feb 20, 2012 at 06:49:45PM +0200, Andrey Utkin wrote:
2012/2/20 Clément Bœsch <ubitux@gmail.com>:
From: Clément Bœsch <clement.boesch@smartjog.com>
Mostly based on doc/examples/filtering.c. lavfi API is still limited to "buffer feeding" instead of "frame feeding" at the moment, so this example code sticks with it.
Thanks a lot for this example!
To be honest, it was mostly to help myself more than kindness for API users :)
+ /* pull filtered pictures from the filtergraph */
That's not "pictures".
Heh, indeed. Fixed locally, thank you. -- Clément B.
It would be even more useful if you show how to get AVFrame from AVFilterBufferRef in a right way. -- Andrey Utkin
On Mon, Feb 20, 2012 at 07:05:55PM +0200, Andrey Utkin wrote:
It would be even more useful if you show how to get AVFrame from AVFilterBufferRef in a right way.
For consistency with the simple filtering.c example (which just makes use of the picref), I'd better not. Of course, more small code excerpts are welcome, so feel free to propose some. BTW, not related, I'll rename filtering.c into filtering_video.c after these two patches are pushed. -- Clément B.
Le duodi 2 ventôse, an CCXX, Clément Bœsch a écrit :
doc/examples/Makefile | 2 +- doc/examples/filtering-audio.c | 244 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+), 1 deletions(-) create mode 100644 doc/examples/filtering-audio.c
Good idea.
diff --git a/doc/examples/Makefile b/doc/examples/Makefile index b4d299f..135fa95 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -3,7 +3,7 @@ FFMPEG_LIBS=libavdevice libavformat libavfilter libavcodec libswscale libavutil CFLAGS+=-Wall $(shell pkg-config --cflags $(FFMPEG_LIBS)) LDFLAGS+=$(shell pkg-config --libs $(FFMPEG_LIBS))
-EXAMPLES=decoding_encoding filtering metadata muxing +EXAMPLES=decoding_encoding filtering filtering-audio metadata muxing
Aren't we being inconsistent with dashes and underscores?
OBJS=$(addsuffix .o,$(EXAMPLES))
diff --git a/doc/examples/filtering-audio.c b/doc/examples/filtering-audio.c new file mode 100644 index 0000000..738b186 --- /dev/null +++ b/doc/examples/filtering-audio.c @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2010 Nicolas George + * Copyright (c) 2011 Stefano Sabatini + * Copyright (c) 2012 Clément Bœsch + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @file + * API example for audio decoding and filtering + */ + +#include <unistd.h> + +#include <libavcodec/avcodec.h> +#include <libavformat/avformat.h> +#include <libavfilter/asrc_abuffer.h> +#include <libavfilter/avfiltergraph.h> +#include <libavfilter/avcodec.h> +#include <libavfilter/buffersink.h> + +const char *filter_descr = "aresample=8000,aconvert=s16:mono"; +const char *player = "ffplay -f s16le -ar 8000 -ac 1 -i /dev/stdin";
For ffplay, I believe "-" works too, and may be more portable. And the -i is not necessary.
+ +static AVFormatContext *fmt_ctx; +static AVCodecContext *dec_ctx; +AVFilterContext *buffersink_ctx; +AVFilterContext *buffersrc_ctx; +AVFilterGraph *filter_graph; +static int audio_stream_index = -1; + +static int open_input_file(const char *filename) +{ + int ret; + AVCodec *dec; + + if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n"); + return ret; + } + + if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n"); + return ret; + } + + /* select the audio stream */ + ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot find a audio stream in the input file\n"); + return ret; + } + audio_stream_index = ret; + dec_ctx = fmt_ctx->streams[audio_stream_index]->codec; + + /* init the audio decoder */ + if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot open audio decoder\n"); + return ret; + } + + return 0; +} + +static int init_filters(const char *filters_descr) +{ + char args[512]; + int ret; + AVFilter *buffersrc = avfilter_get_by_name("abuffer"); + AVFilter *buffersink = avfilter_get_by_name("abuffersink"); + AVFilterInOut *outputs = avfilter_inout_alloc(); + AVFilterInOut *inputs = avfilter_inout_alloc(); + const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 }; + const int packing_fmts[] = { AVFILTER_PACKED, -1 }; + const int64_t *chlayouts = avfilter_all_channel_layouts; + AVABufferSinkParams *abuffersink_params;
Some of the variables use the exact name "abuffer...", some use only "buffer", this is slightly inconsistent.
+ const AVFilterLink *outlink; + + filter_graph = avfilter_graph_alloc(); + + /* buffer audio source: the decoded frames from the decoder will be inserted here. */ + if (!dec_ctx->channel_layout) + dec_ctx->channel_layout = av_get_default_channel_layout(dec_ctx->channels); + snprintf(args, sizeof(args), "%d:%d:0x%"PRIx64":packed", + dec_ctx->sample_rate, dec_ctx->sample_fmt, dec_ctx->channel_layout); + ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", + args, NULL, filter_graph); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n"); + return ret; + } + + /* buffer audio sink: to terminate the filter chain. */ + abuffersink_params = av_abuffersink_params_alloc(); + abuffersink_params->sample_fmts = sample_fmts; + abuffersink_params->channel_layouts = chlayouts; + abuffersink_params->packing_fmts = packing_fmts; + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, abuffersink_params, filter_graph);
As a matter of curiosity, is there a reason to use a string argument for abuffersrc and a structured parameter for abuffersink?
+ if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n"); + return ret; + } + + /* Endpoints for the filter graph. */ + outputs->name = av_strdup("in"); + outputs->filter_ctx = buffersrc_ctx; + outputs->pad_idx = 0; + outputs->next = NULL; + + inputs->name = av_strdup("out"); + inputs->filter_ctx = buffersink_ctx; + inputs->pad_idx = 0; + inputs->next = NULL; + + if ((ret = avfilter_graph_parse(filter_graph, filter_descr, + &inputs, &outputs, NULL)) < 0) + return ret; + + if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) + return ret; + + outlink = buffersink_ctx->inputs[0]; + // abuse args buffer to store channel layout string
"reuse", maybe? And while I am nitpicking, the comment style is inconsistent.
+ av_get_channel_layout_string(args, sizeof(args), -1, outlink->channel_layout); + av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n", + (int)outlink->sample_rate, + (char *)av_x_if_null(av_get_sample_fmt_name(outlink->format), "?"), + args); + + return 0; +} + +static void print_samplesref(AVFilterBufferRef *samplesref) +{ + const AVFilterBufferRefAudioProps *props = samplesref->audio; + const int n = props->nb_samples * av_get_channel_layout_nb_channels(props->channel_layout); + const uint16_t *p = (uint16_t*)samplesref->data[0]; + const uint16_t *p_end = p + n; + + while (p < p_end) { + fputc(*p & 0xff, stdout); + fputc(*p>>8 & 0xff, stdout); + p++; + } + fflush(stdout); +} + +int main(int argc, char **argv) +{ + int ret; + AVPacket packet; + AVFrame frame; + int got_frame; + + if (argc != 2) { + fprintf(stderr, "Usage: %s file | %s\n", argv[0], player); + exit(1); + } + + avcodec_register_all(); + av_register_all(); + avfilter_register_all(); + + if ((ret = open_input_file(argv[1])) < 0) + goto end; + if ((ret = init_filters(filter_descr)) < 0) + goto end; + + /* read all packets */ + while (1) { + AVFilterBufferRef *samplesref; + if ((ret = av_read_frame(fmt_ctx, &packet)) < 0) + break; + + if (packet.stream_index == audio_stream_index) { + avcodec_get_frame_defaults(&frame); + got_frame = 0; + ret = avcodec_decode_audio4(dec_ctx, &frame, &got_frame, &packet); + av_free_packet(&packet); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n"); + break; + } + + if (got_frame) { + const int bps = av_get_bytes_per_sample(dec_ctx->sample_fmt); + const int decoded_data_size = frame.nb_samples * dec_ctx->channels * bps; + + /* push the audio data from decoded frame into the filtergraph */ + if (av_asrc_buffer_add_buffer(buffersrc_ctx, + frame.data[0], + decoded_data_size, + dec_ctx->sample_rate, + dec_ctx->sample_fmt, + dec_ctx->channel_layout, + 0, frame.pts, 0) < 0) { + av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n"); + exit(1);
Is there a reason to use exit here while the other places use break or goto?
+ } + + /* pull filtered pictures from the filtergraph */ + while (avfilter_poll_frame(buffersink_ctx->inputs[0])) { + av_buffersink_get_buffer_ref(buffersink_ctx, &samplesref, 0); + if (samplesref) { + print_samplesref(samplesref); + avfilter_unref_buffer(samplesref); + } + } + } + } + } +end: + avfilter_graph_free(&filter_graph); + if (dec_ctx) + avcodec_close(dec_ctx); + avformat_close_input(&fmt_ctx); + + if (ret < 0 && ret != AVERROR_EOF) { + char buf[1024]; + av_strerror(ret, buf, sizeof(buf)); + fprintf(stderr, "Error occurred: %s\n", buf); + exit(1); + } + + exit(0); +}
Apart from these little details, it seems really useful. Regards, -- Nicolas George
On Mon, Feb 20, 2012 at 06:45:27PM +0100, Nicolas George wrote:
Le duodi 2 ventôse, an CCXX, Clément Bœsch a écrit :
doc/examples/Makefile | 2 +- doc/examples/filtering-audio.c | 244 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+), 1 deletions(-) create mode 100644 doc/examples/filtering-audio.c
Good idea.
diff --git a/doc/examples/Makefile b/doc/examples/Makefile index b4d299f..135fa95 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -3,7 +3,7 @@ FFMPEG_LIBS=libavdevice libavformat libavfilter libavcodec libswscale libavutil CFLAGS+=-Wall $(shell pkg-config --cflags $(FFMPEG_LIBS)) LDFLAGS+=$(shell pkg-config --libs $(FFMPEG_LIBS))
-EXAMPLES=decoding_encoding filtering metadata muxing +EXAMPLES=decoding_encoding filtering filtering-audio metadata muxing
Aren't we being inconsistent with dashes and underscores?
Indeed, renamed.
OBJS=$(addsuffix .o,$(EXAMPLES))
diff --git a/doc/examples/filtering-audio.c b/doc/examples/filtering-audio.c new file mode 100644 index 0000000..738b186 --- /dev/null +++ b/doc/examples/filtering-audio.c @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2010 Nicolas George + * Copyright (c) 2011 Stefano Sabatini + * Copyright (c) 2012 Clément Bœsch + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @file + * API example for audio decoding and filtering + */ + +#include <unistd.h> + +#include <libavcodec/avcodec.h> +#include <libavformat/avformat.h> +#include <libavfilter/asrc_abuffer.h> +#include <libavfilter/avfiltergraph.h> +#include <libavfilter/avcodec.h> +#include <libavfilter/buffersink.h> + +const char *filter_descr = "aresample=8000,aconvert=s16:mono"; +const char *player = "ffplay -f s16le -ar 8000 -ac 1 -i /dev/stdin";
For ffplay, I believe "-" works too, and may be more portable. And the -i is not necessary.
That's right, simplified.
+ +static AVFormatContext *fmt_ctx; +static AVCodecContext *dec_ctx; +AVFilterContext *buffersink_ctx; +AVFilterContext *buffersrc_ctx; +AVFilterGraph *filter_graph; +static int audio_stream_index = -1; + +static int open_input_file(const char *filename) +{ + int ret; + AVCodec *dec; + + if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n"); + return ret; + } + + if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n"); + return ret; + } + + /* select the audio stream */ + ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot find a audio stream in the input file\n"); + return ret; + } + audio_stream_index = ret; + dec_ctx = fmt_ctx->streams[audio_stream_index]->codec; + + /* init the audio decoder */ + if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot open audio decoder\n"); + return ret; + } + + return 0; +} + +static int init_filters(const char *filters_descr) +{ + char args[512]; + int ret; + AVFilter *buffersrc = avfilter_get_by_name("abuffer"); + AVFilter *buffersink = avfilter_get_by_name("abuffersink"); + AVFilterInOut *outputs = avfilter_inout_alloc(); + AVFilterInOut *inputs = avfilter_inout_alloc(); + const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 }; + const int packing_fmts[] = { AVFILTER_PACKED, -1 }; + const int64_t *chlayouts = avfilter_all_channel_layouts; + AVABufferSinkParams *abuffersink_params;
Some of the variables use the exact name "abuffer...", some use only "buffer", this is slightly inconsistent.
buffersrc and buffsersink respectively renamed into abuffersrc and abuffersink.
+ const AVFilterLink *outlink; + + filter_graph = avfilter_graph_alloc(); + + /* buffer audio source: the decoded frames from the decoder will be inserted here. */ + if (!dec_ctx->channel_layout) + dec_ctx->channel_layout = av_get_default_channel_layout(dec_ctx->channels); + snprintf(args, sizeof(args), "%d:%d:0x%"PRIx64":packed", + dec_ctx->sample_rate, dec_ctx->sample_fmt, dec_ctx->channel_layout); + ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", + args, NULL, filter_graph); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n"); + return ret; + } + + /* buffer audio sink: to terminate the filter chain. */ + abuffersink_params = av_abuffersink_params_alloc(); + abuffersink_params->sample_fmts = sample_fmts; + abuffersink_params->channel_layouts = chlayouts; + abuffersink_params->packing_fmts = packing_fmts; + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, abuffersink_params, filter_graph);
As a matter of curiosity, is there a reason to use a string argument for abuffersrc and a structured parameter for abuffersink?
None that I know of, except that abuffersink (lavfi/sink_buffer.c:asink_init) only accepts a struct parameter while abuffer (lavfi/asrc_abuffer.c:init) only accepts a string argument. Maybe the two filters could accept two kind of inputs for consistency.
+ if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n"); + return ret; + } + + /* Endpoints for the filter graph. */ + outputs->name = av_strdup("in"); + outputs->filter_ctx = buffersrc_ctx; + outputs->pad_idx = 0; + outputs->next = NULL; + + inputs->name = av_strdup("out"); + inputs->filter_ctx = buffersink_ctx; + inputs->pad_idx = 0; + inputs->next = NULL; + + if ((ret = avfilter_graph_parse(filter_graph, filter_descr, + &inputs, &outputs, NULL)) < 0) + return ret; + + if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) + return ret; + + outlink = buffersink_ctx->inputs[0]; + // abuse args buffer to store channel layout string
"reuse", maybe? And while I am nitpicking, the comment style is inconsistent.
Replaced "abuse" with "reuse". About the comment style, it was relative to a single line and not an explanation of the following block; since the line was already too long, I just put it above. The comment is reworked, hopefully in a better way.
+ av_get_channel_layout_string(args, sizeof(args), -1, outlink->channel_layout); + av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n", + (int)outlink->sample_rate, + (char *)av_x_if_null(av_get_sample_fmt_name(outlink->format), "?"), + args); + + return 0; +} + +static void print_samplesref(AVFilterBufferRef *samplesref) +{ + const AVFilterBufferRefAudioProps *props = samplesref->audio; + const int n = props->nb_samples * av_get_channel_layout_nb_channels(props->channel_layout); + const uint16_t *p = (uint16_t*)samplesref->data[0]; + const uint16_t *p_end = p + n; + + while (p < p_end) { + fputc(*p & 0xff, stdout); + fputc(*p>>8 & 0xff, stdout); + p++; + } + fflush(stdout); +} + +int main(int argc, char **argv) +{ + int ret; + AVPacket packet; + AVFrame frame; + int got_frame; + + if (argc != 2) { + fprintf(stderr, "Usage: %s file | %s\n", argv[0], player); + exit(1); + } + + avcodec_register_all(); + av_register_all(); + avfilter_register_all(); + + if ((ret = open_input_file(argv[1])) < 0) + goto end; + if ((ret = init_filters(filter_descr)) < 0) + goto end; + + /* read all packets */ + while (1) { + AVFilterBufferRef *samplesref; + if ((ret = av_read_frame(fmt_ctx, &packet)) < 0) + break; + + if (packet.stream_index == audio_stream_index) { + avcodec_get_frame_defaults(&frame); + got_frame = 0; + ret = avcodec_decode_audio4(dec_ctx, &frame, &got_frame, &packet); + av_free_packet(&packet); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n"); + break; + } + + if (got_frame) { + const int bps = av_get_bytes_per_sample(dec_ctx->sample_fmt); + const int decoded_data_size = frame.nb_samples * dec_ctx->channels * bps; + + /* push the audio data from decoded frame into the filtergraph */ + if (av_asrc_buffer_add_buffer(buffersrc_ctx, + frame.data[0], + decoded_data_size, + dec_ctx->sample_rate, + dec_ctx->sample_fmt, + dec_ctx->channel_layout, + 0, frame.pts, 0) < 0) { + av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n"); + exit(1);
Is there a reason to use exit here while the other places use break or goto?
Not at all, my eyes just catch a few exit() and I didn't though much about this; obviously, a break is more appropriate here, so replaced.
+ } + + /* pull filtered pictures from the filtergraph */ + while (avfilter_poll_frame(buffersink_ctx->inputs[0])) { + av_buffersink_get_buffer_ref(buffersink_ctx, &samplesref, 0); + if (samplesref) { + print_samplesref(samplesref); + avfilter_unref_buffer(samplesref); + } + } + } + } + } +end: + avfilter_graph_free(&filter_graph); + if (dec_ctx) + avcodec_close(dec_ctx); + avformat_close_input(&fmt_ctx); + + if (ret < 0 && ret != AVERROR_EOF) { + char buf[1024]; + av_strerror(ret, buf, sizeof(buf)); + fprintf(stderr, "Error occurred: %s\n", buf); + exit(1); + } + + exit(0); +}
Apart from these little details, it seems really useful.
Great, then I'll push the attached version soon. -- Clément B.
On Tue, Feb 21, 2012 at 09:15:15AM +0100, Clément Bœsch wrote: [...]
Apart from these little details, it seems really useful.
Great, then I'll push the attached version soon.
-- Clément B.
From 6d6f41aafcfb855bf89977ad055fa524235c732e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <clement.boesch@smartjog.com> Date: Mon, 20 Feb 2012 13:49:18 +0100 Subject: [PATCH 2/2] doc/examples: add audio decoding/filtering example.
Mostly based on doc/examples/filtering.c. lavfi API is still limited to "buffer feeding" instead of "frame feeding" at the moment, so this example code sticks with it. --- doc/examples/Makefile | 2 +- doc/examples/filtering_audio.c | 245 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+), 1 deletions(-) create mode 100644 doc/examples/filtering_audio.c
Pushed. -- Clément B.
2012/2/20 Clément Bœsch <ubitux@gmail.com>:
+ snprintf(args, sizeof(args), "%d:%d:0x%"PRIx64":packed", + dec_ctx->sample_rate, dec_ctx->sample_fmt, dec_ctx->channel_layout);
Why not just %"PRIu64"? -- Andrey Utkin
On Tue, Feb 21, 2012 at 04:46:42PM +0200, Andrey Utkin wrote:
2012/2/20 Clément Bœsch <ubitux@gmail.com>:
+ snprintf(args, sizeof(args), "%d:%d:0x%"PRIx64":packed", + dec_ctx->sample_rate, dec_ctx->sample_fmt, dec_ctx->channel_layout);
Why not just %"PRIu64"?
See http://source.ffmpeg.org/?p=ffmpeg.git;a=commitdiff;h=6e6bdeb137e1b89598f1be... This is due to the av_get_channel_layout() recent change. -- Clément B.
2012/2/20 Clément Bœsch <ubitux@gmail.com>:
+ /* buffer audio sink: to terminate the filter chain. */ + abuffersink_params = av_abuffersink_params_alloc(); + abuffersink_params->sample_fmts = sample_fmts; + abuffersink_params->channel_layouts = chlayouts; + abuffersink_params->packing_fmts = packing_fmts; + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, abuffersink_params, filter_graph);
Looking at libavfilter/avfiltergraph.c:68 libavfilter/avfilter.c:876 and libavfilter/asink_anullsink.c i find that abuffersink_params is not treated in any way, and is not saved anywhere. Is that right, or i miss sth? -- Andrey Utkin
On Tue, Feb 21, 2012 at 04:59:07PM +0200, Andrey Utkin wrote:
2012/2/20 Clément Bœsch <ubitux@gmail.com>:
+ /* buffer audio sink: to terminate the filter chain. */ + abuffersink_params = av_abuffersink_params_alloc(); + abuffersink_params->sample_fmts = sample_fmts; + abuffersink_params->channel_layouts = chlayouts; + abuffersink_params->packing_fmts = packing_fmts; + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, abuffersink_params, filter_graph);
Looking at libavfilter/avfiltergraph.c:68 libavfilter/avfilter.c:876
Why do you think this...
and libavfilter/asink_anullsink.c
...will call this callback?
i find that abuffersink_params is not treated in any way, and is not saved anywhere. Is that right, or i miss sth?
The correct callback is asink_init() in lavfi/sink_buffer.c, where the opaque pointer is used. -- Clément B.
2012/2/21 Clément Bœsch <ubitux@gmail.com>:
The correct callback is asink_init() in lavfi/sink_buffer.c, where the opaque pointer is used.
Sorry for noise. Confused abuffersink with anullsink. -- Andrey Utkin
2012/2/20 Clément Bœsch <ubitux@gmail.com>:
+ AVABufferSinkParams *abuffersink_params; ... + /* buffer audio sink: to terminate the filter chain. */ + abuffersink_params = av_abuffersink_params_alloc(); + abuffersink_params->sample_fmts = sample_fmts; + abuffersink_params->channel_layouts = chlayouts; + abuffersink_params->packing_fmts = packing_fmts; + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, abuffersink_params, filter_graph);
As i see, abuffersink_params in the asink_init is not being referenced or so. Its contents are just read and copied to internal context. I think we should use automatic allocation - for sake of simplicity, to avoid API users think they must also allocate AVABufferSinkParams on a heap (using special function) and pass the pointer. -- Andrey Utkin
On Tue, Feb 21, 2012 at 05:59:24PM +0200, Andrey Utkin wrote:
2012/2/20 Clément Bœsch <ubitux@gmail.com>:
+ AVABufferSinkParams *abuffersink_params; ... + /* buffer audio sink: to terminate the filter chain. */ + abuffersink_params = av_abuffersink_params_alloc(); + abuffersink_params->sample_fmts = sample_fmts; + abuffersink_params->channel_layouts = chlayouts; + abuffersink_params->packing_fmts = packing_fmts; + ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", + NULL, abuffersink_params, filter_graph);
As i see, abuffersink_params in the asink_init is not being referenced or so. Its contents are just read and copied to internal context. I think we should use automatic allocation - for sake of simplicity, to avoid API users think they must also allocate AVABufferSinkParams on a heap (using special function) and pass the pointer.
What do you propose to achieve the "automatic allocation"? And why "think"? They actually have to do that allocation. -- Clément B.
2012/2/21 Clément Bœsch <ubitux@gmail.com>:
On Tue, Feb 21, 2012 at 05:59:24PM +0200, Andrey Utkin wrote:
As i see, abuffersink_params in the asink_init is not being referenced or so. Its contents are just read and copied to internal context. I think we should use automatic allocation - for sake of simplicity, to avoid API users think they must also allocate AVABufferSinkParams on a heap (using special function) and pass the pointer.
What do you propose to achieve the "automatic allocation"?
Allocation on stack. -- Andrey Utkin
On Wed, Feb 22, 2012 at 11:58:53AM +0200, Andrey Utkin wrote:
2012/2/21 Clément Bœsch <ubitux@gmail.com>:
On Tue, Feb 21, 2012 at 05:59:24PM +0200, Andrey Utkin wrote:
As i see, abuffersink_params in the asink_init is not being referenced or so. Its contents are just read and copied to internal context. I think we should use automatic allocation - for sake of simplicity, to avoid API users think they must also allocate AVABufferSinkParams on a heap (using special function) and pass the pointer.
What do you propose to achieve the "automatic allocation"?
Allocation on stack.
I wouldn't mind a av_[a]buffersink_get_defaults_params() or something, but maybe Stefano wants to comment on this. -- Clément B.
On date Wednesday 2012-02-22 13:01:30 +0100, Clément Bœsch encoded:
On Wed, Feb 22, 2012 at 11:58:53AM +0200, Andrey Utkin wrote:
2012/2/21 Clément Bœsch <ubitux@gmail.com>:
On Tue, Feb 21, 2012 at 05:59:24PM +0200, Andrey Utkin wrote:
As i see, abuffersink_params in the asink_init is not being referenced or so. Its contents are just read and copied to internal context. I think we should use automatic allocation - for sake of simplicity, to avoid API users think they must also allocate AVABufferSinkParams on a heap (using special function) and pass the pointer.
What do you propose to achieve the "automatic allocation"?
Allocation on stack.
I wouldn't mind a av_[a]buffersink_get_defaults_params() or something, but maybe Stefano wants to comment on this.
This would break in case of ABI changes. The size of the struct may change, so an application may allocate on the stack a struct with the wrong size if not compiled against a not updated library version, that's why we require the call of a library function. -- FFmpeg = Fierce and Fiendish Miracolous Puristic Ecumenical Ghost
On 2/20/12, Clement Boesch <ubitux@gmail.com> wrote:
From: Clement Boesch <clement.boesch@smartjog.com>
Samples buffer ref is allocated and loaded with the uninitialized data pointers: av_asrc_buffer_add_buffer() -> av_asrc_buffer_add_samples() -> avfilter_get_audio_buffer_ref_from_arrays(data, ...)
...which leads to a crash with at least lavfi/ashowinfo in case of !NULL (see the for loop while samplesref->data[plane]). --- libavfilter/asrc_abuffer.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
LGTM
On Mon, Feb 20, 2012 at 06:06:54PM +0000, Paul B Mahol wrote:
On 2/20/12, Clement Boesch <ubitux@gmail.com> wrote:
From: Clement Boesch <clement.boesch@smartjog.com>
Samples buffer ref is allocated and loaded with the uninitialized data pointers: av_asrc_buffer_add_buffer() -> av_asrc_buffer_add_samples() -> avfilter_get_audio_buffer_ref_from_arrays(data, ...)
...which leads to a crash with at least lavfi/ashowinfo in case of !NULL (see the for loop while samplesref->data[plane]). --- libavfilter/asrc_abuffer.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
LGTM
Applied. -- Clément B.
participants (5)
-
Andrey Utkin -
Clément Bœsch -
Nicolas George -
Paul B Mahol -
Stefano Sabatini