2012-02-20 12:49:18 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2014-02-17 05:59:59 +00:00
|
|
|
* @example filtering_audio.c
|
2012-02-20 12:49:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavfilter/avfiltergraph.h>
|
|
|
|
#include <libavfilter/buffersink.h>
|
2012-05-09 21:47:08 +00:00
|
|
|
#include <libavfilter/buffersrc.h>
|
2013-04-17 06:46:49 +00:00
|
|
|
#include <libavutil/opt.h>
|
2012-02-20 12:49:18 +00:00
|
|
|
|
2013-08-03 20:22:03 +00:00
|
|
|
static const char *filter_descr = "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono";
|
|
|
|
static const char *player = "ffplay -f s16le -ar 8000 -ac 1 -";
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
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;
|
2013-04-17 08:46:16 +00:00
|
|
|
av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
/* 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];
|
2013-12-02 12:18:11 +00:00
|
|
|
int ret = 0;
|
2012-02-20 12:49:18 +00:00
|
|
|
AVFilter *abuffersrc = avfilter_get_by_name("abuffer");
|
2013-03-11 20:49:04 +00:00
|
|
|
AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
|
2012-02-20 12:49:18 +00:00
|
|
|
AVFilterInOut *outputs = avfilter_inout_alloc();
|
|
|
|
AVFilterInOut *inputs = avfilter_inout_alloc();
|
2013-08-03 20:22:03 +00:00
|
|
|
static const enum AVSampleFormat out_sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
|
|
|
|
static const int64_t out_channel_layouts[] = { AV_CH_LAYOUT_MONO, -1 };
|
|
|
|
static const int out_sample_rates[] = { 8000, -1 };
|
2012-02-20 12:49:18 +00:00
|
|
|
const AVFilterLink *outlink;
|
2012-06-04 18:07:15 +00:00
|
|
|
AVRational time_base = fmt_ctx->streams[audio_stream_index]->time_base;
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
filter_graph = avfilter_graph_alloc();
|
2013-12-02 12:18:11 +00:00
|
|
|
if (!outputs || !inputs || !filter_graph) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
/* 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);
|
2012-06-04 18:07:15 +00:00
|
|
|
snprintf(args, sizeof(args),
|
|
|
|
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
|
|
|
|
time_base.num, time_base.den, dec_ctx->sample_rate,
|
|
|
|
av_get_sample_fmt_name(dec_ctx->sample_fmt), dec_ctx->channel_layout);
|
2012-02-20 12:49:18 +00:00
|
|
|
ret = avfilter_graph_create_filter(&buffersrc_ctx, abuffersrc, "in",
|
|
|
|
args, NULL, filter_graph);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
|
2013-12-02 12:18:11 +00:00
|
|
|
goto end;
|
2012-02-20 12:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* buffer audio sink: to terminate the filter chain. */
|
|
|
|
ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out",
|
2013-04-17 06:46:49 +00:00
|
|
|
NULL, NULL, filter_graph);
|
2012-02-20 12:49:18 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
|
2013-12-02 12:18:11 +00:00
|
|
|
goto end;
|
2012-02-20 12:49:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 06:46:49 +00:00
|
|
|
ret = av_opt_set_int_list(buffersink_ctx, "sample_fmts", out_sample_fmts, -1,
|
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
|
2013-12-02 12:18:11 +00:00
|
|
|
goto end;
|
2013-04-17 06:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_opt_set_int_list(buffersink_ctx, "channel_layouts", out_channel_layouts, -1,
|
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
|
2013-12-02 12:18:11 +00:00
|
|
|
goto end;
|
2013-04-17 06:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_opt_set_int_list(buffersink_ctx, "sample_rates", out_sample_rates, -1,
|
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
|
2013-12-02 12:18:11 +00:00
|
|
|
goto end;
|
2013-04-17 06:46:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 12:22:48 +00:00
|
|
|
/*
|
|
|
|
* Set the endpoints for the filter graph. The filter_graph will
|
|
|
|
* be linked to the graph described by filters_descr.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The buffer source output must be connected to the input pad of
|
|
|
|
* the first filter described by filters_descr; since the first
|
|
|
|
* filter input label is not specified, it is set to "in" by
|
|
|
|
* default.
|
|
|
|
*/
|
2012-02-20 12:49:18 +00:00
|
|
|
outputs->name = av_strdup("in");
|
|
|
|
outputs->filter_ctx = buffersrc_ctx;
|
|
|
|
outputs->pad_idx = 0;
|
|
|
|
outputs->next = NULL;
|
|
|
|
|
2015-01-27 12:22:48 +00:00
|
|
|
/*
|
|
|
|
* The buffer sink input must be connected to the output pad of
|
|
|
|
* the last filter described by filters_descr; since the last
|
|
|
|
* filter output label is not specified, it is set to "out" by
|
|
|
|
* default.
|
|
|
|
*/
|
2012-02-20 12:49:18 +00:00
|
|
|
inputs->name = av_strdup("out");
|
|
|
|
inputs->filter_ctx = buffersink_ctx;
|
|
|
|
inputs->pad_idx = 0;
|
|
|
|
inputs->next = NULL;
|
|
|
|
|
2013-07-01 23:39:14 +00:00
|
|
|
if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
|
2013-12-02 12:18:11 +00:00
|
|
|
&inputs, &outputs, NULL)) < 0)
|
|
|
|
goto end;
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
|
2013-12-02 12:18:11 +00:00
|
|
|
goto end;
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
/* Print summary of the sink buffer
|
|
|
|
* Note: args buffer is reused to store channel layout string */
|
|
|
|
outlink = buffersink_ctx->inputs[0];
|
|
|
|
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);
|
|
|
|
|
2013-12-02 12:18:11 +00:00
|
|
|
end:
|
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
avfilter_inout_free(&outputs);
|
|
|
|
|
|
|
|
return ret;
|
2012-02-20 12:49:18 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 20:38:07 +00:00
|
|
|
static void print_frame(const AVFrame *frame)
|
2012-02-20 12:49:18 +00:00
|
|
|
{
|
2013-03-13 21:08:33 +00:00
|
|
|
const int n = frame->nb_samples * av_get_channel_layout_nb_channels(av_frame_get_channel_layout(frame));
|
|
|
|
const uint16_t *p = (uint16_t*)frame->data[0];
|
2012-02-20 12:49:18 +00:00
|
|
|
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;
|
2013-12-02 14:21:52 +00:00
|
|
|
AVPacket packet0, packet;
|
2013-03-13 21:08:33 +00:00
|
|
|
AVFrame *frame = av_frame_alloc();
|
|
|
|
AVFrame *filt_frame = av_frame_alloc();
|
2012-02-20 12:49:18 +00:00
|
|
|
int got_frame;
|
|
|
|
|
2013-03-13 21:08:33 +00:00
|
|
|
if (!frame || !filt_frame) {
|
2013-02-16 10:36:32 +00:00
|
|
|
perror("Could not allocate frame");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-02-20 12:49:18 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s file | %s\n", argv[0], player);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2013-12-02 23:00:04 +00:00
|
|
|
packet0.data = NULL;
|
2013-12-02 14:21:52 +00:00
|
|
|
packet.data = NULL;
|
2012-02-20 12:49:18 +00:00
|
|
|
while (1) {
|
2013-12-02 14:21:52 +00:00
|
|
|
if (!packet0.data) {
|
|
|
|
if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
|
|
|
|
break;
|
|
|
|
packet0 = packet;
|
|
|
|
}
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
if (packet.stream_index == audio_stream_index) {
|
|
|
|
got_frame = 0;
|
2013-02-16 10:36:32 +00:00
|
|
|
ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, &packet);
|
2012-02-20 12:49:18 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n");
|
2012-04-27 21:46:09 +00:00
|
|
|
continue;
|
2012-02-20 12:49:18 +00:00
|
|
|
}
|
2013-12-02 14:21:52 +00:00
|
|
|
packet.size -= ret;
|
|
|
|
packet.data += ret;
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
if (got_frame) {
|
|
|
|
/* push the audio data from decoded frame into the filtergraph */
|
2013-04-18 13:15:22 +00:00
|
|
|
if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, 0) < 0) {
|
2012-02-20 12:49:18 +00:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pull filtered audio from the filtergraph */
|
2012-06-24 12:15:53 +00:00
|
|
|
while (1) {
|
2013-03-13 21:08:33 +00:00
|
|
|
ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
|
2013-12-02 13:43:40 +00:00
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
|
2012-06-24 12:15:53 +00:00
|
|
|
break;
|
2013-12-02 13:43:40 +00:00
|
|
|
if (ret < 0)
|
2012-06-24 12:15:53 +00:00
|
|
|
goto end;
|
2013-03-13 21:08:33 +00:00
|
|
|
print_frame(filt_frame);
|
|
|
|
av_frame_unref(filt_frame);
|
2012-02-20 12:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-02 14:21:52 +00:00
|
|
|
|
|
|
|
if (packet.size <= 0)
|
2015-10-27 13:35:30 +00:00
|
|
|
av_packet_unref(&packet0);
|
2013-12-02 14:21:52 +00:00
|
|
|
} else {
|
|
|
|
/* discard non-wanted packets */
|
2015-10-27 13:35:30 +00:00
|
|
|
av_packet_unref(&packet0);
|
2012-02-20 12:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
avfilter_graph_free(&filter_graph);
|
2013-11-21 22:29:56 +00:00
|
|
|
avcodec_close(dec_ctx);
|
2012-02-20 12:49:18 +00:00
|
|
|
avformat_close_input(&fmt_ctx);
|
2013-03-13 21:08:33 +00:00
|
|
|
av_frame_free(&frame);
|
|
|
|
av_frame_free(&filt_frame);
|
2012-02-20 12:49:18 +00:00
|
|
|
|
|
|
|
if (ret < 0 && ret != AVERROR_EOF) {
|
2013-12-02 12:08:00 +00:00
|
|
|
fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
|
2012-02-20 12:49:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|