2011-07-04 10:35:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 S.N. Hemanth Meenakshisundaram <smeenaks@ucsd.edu>
|
|
|
|
* Copyright (c) 2011 Stefano Sabatini
|
|
|
|
* Copyright (c) 2011 Mina Nagy Zaki
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* sample format and channel layout conversion audio filter
|
|
|
|
*/
|
|
|
|
|
2011-10-15 00:14:37 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2012-11-13 14:16:48 +01:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-01-26 08:49:15 +01:00
|
|
|
#include "libswresample/swresample.h"
|
2011-07-04 10:35:39 +02:00
|
|
|
#include "avfilter.h"
|
2012-05-12 17:58:14 +02:00
|
|
|
#include "audio.h"
|
2011-07-04 10:35:39 +02:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2012-01-26 08:49:15 +01:00
|
|
|
enum AVSampleFormat out_sample_fmt;
|
|
|
|
int64_t out_chlayout;
|
|
|
|
struct SwrContext *swr;
|
2011-07-04 10:35:39 +02:00
|
|
|
} AConvertContext;
|
|
|
|
|
2012-06-26 23:27:59 +02:00
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args0)
|
2011-07-04 10:35:39 +02:00
|
|
|
{
|
|
|
|
AConvertContext *aconvert = ctx->priv;
|
|
|
|
char *arg, *ptr = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
char *args = av_strdup(args0);
|
|
|
|
|
|
|
|
aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
|
|
|
|
aconvert->out_chlayout = 0;
|
|
|
|
|
2011-10-15 00:14:37 +02:00
|
|
|
if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
|
2011-07-04 10:35:39 +02:00
|
|
|
if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
|
|
|
|
goto end;
|
|
|
|
}
|
2011-10-15 00:14:37 +02:00
|
|
|
if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
|
2011-07-04 10:35:39 +02:00
|
|
|
if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
av_freep(&args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AConvertContext *aconvert = ctx->priv;
|
2012-01-26 08:49:15 +01:00
|
|
|
swr_free(&aconvert->swr);
|
2011-07-04 10:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats = NULL;
|
|
|
|
AConvertContext *aconvert = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2012-05-16 02:27:31 +02:00
|
|
|
AVFilterChannelLayouts *layouts;
|
2011-07-04 10:35:39 +02:00
|
|
|
|
2012-06-24 02:09:53 +02:00
|
|
|
ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
|
2011-07-04 10:35:39 +02:00
|
|
|
&inlink->out_formats);
|
|
|
|
if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
|
|
|
|
formats = NULL;
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_add_format(&formats, aconvert->out_sample_fmt);
|
|
|
|
ff_formats_ref(formats, &outlink->in_formats);
|
2011-07-04 10:35:39 +02:00
|
|
|
} else
|
2012-06-24 02:09:53 +02:00
|
|
|
ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
|
2011-07-04 10:35:39 +02:00
|
|
|
&outlink->in_formats);
|
|
|
|
|
2012-05-16 02:27:31 +02:00
|
|
|
ff_channel_layouts_ref(ff_all_channel_layouts(),
|
|
|
|
&inlink->out_channel_layouts);
|
2011-07-04 10:35:39 +02:00
|
|
|
if (aconvert->out_chlayout != 0) {
|
2012-05-16 02:27:31 +02:00
|
|
|
layouts = NULL;
|
|
|
|
ff_add_channel_layout(&layouts, aconvert->out_chlayout);
|
|
|
|
ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
|
2011-07-04 10:35:39 +02:00
|
|
|
} else
|
2012-05-16 02:27:31 +02:00
|
|
|
ff_channel_layouts_ref(ff_all_channel_layouts(),
|
|
|
|
&outlink->in_channel_layouts);
|
2011-07-04 10:35:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
2012-01-26 08:49:15 +01:00
|
|
|
int ret;
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AConvertContext *aconvert = ctx->priv;
|
2011-07-04 10:35:39 +02:00
|
|
|
char buf1[64], buf2[64];
|
|
|
|
|
|
|
|
/* if not specified in args, use the format and layout of the output */
|
|
|
|
if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
|
|
|
|
aconvert->out_sample_fmt = outlink->format;
|
|
|
|
if (aconvert->out_chlayout == 0)
|
|
|
|
aconvert->out_chlayout = outlink->channel_layout;
|
2012-01-26 08:49:15 +01:00
|
|
|
|
|
|
|
aconvert->swr = swr_alloc_set_opts(aconvert->swr,
|
|
|
|
aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
|
|
|
|
inlink->channel_layout, inlink->format, inlink->sample_rate,
|
|
|
|
0, ctx);
|
|
|
|
if (!aconvert->swr)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = swr_init(aconvert->swr);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-07-04 10:35:39 +02:00
|
|
|
|
|
|
|
av_get_channel_layout_string(buf1, sizeof(buf1),
|
|
|
|
-1, inlink ->channel_layout);
|
|
|
|
av_get_channel_layout_string(buf2, sizeof(buf2),
|
|
|
|
-1, outlink->channel_layout);
|
2012-07-14 18:26:04 +02:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
2012-05-12 17:38:47 +02:00
|
|
|
"fmt:%s cl:%s -> fmt:%s cl:%s\n",
|
|
|
|
av_get_sample_fmt_name(inlink ->format), buf1,
|
|
|
|
av_get_sample_fmt_name(outlink->format), buf2);
|
2011-07-04 10:35:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 13:53:48 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
|
2011-07-04 10:35:39 +02:00
|
|
|
{
|
|
|
|
AConvertContext *aconvert = inlink->dst->priv;
|
2012-01-26 08:49:15 +01:00
|
|
|
const int n = insamplesref->audio->nb_samples;
|
|
|
|
AVFilterLink *const outlink = inlink->dst->outputs[0];
|
2012-05-10 22:41:29 +02:00
|
|
|
AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
|
2012-07-09 22:10:38 +02:00
|
|
|
int ret;
|
2011-07-04 10:35:39 +02:00
|
|
|
|
2012-01-26 08:49:15 +01:00
|
|
|
swr_convert(aconvert->swr, outsamplesref->data, n,
|
|
|
|
(void *)insamplesref->data, n);
|
2011-07-04 10:35:39 +02:00
|
|
|
|
2012-01-26 08:49:15 +01:00
|
|
|
avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
|
2012-12-20 16:38:53 +01:00
|
|
|
outsamplesref->audio->channels = outlink->channels;
|
2012-01-26 08:49:15 +01:00
|
|
|
outsamplesref->audio->channel_layout = outlink->channel_layout;
|
2011-07-04 10:35:39 +02:00
|
|
|
|
2012-11-28 13:53:48 +01:00
|
|
|
ret = ff_filter_frame(outlink, outsamplesref);
|
2011-07-04 10:35:39 +02:00
|
|
|
avfilter_unref_buffer(insamplesref);
|
2012-07-09 22:10:38 +02:00
|
|
|
return ret;
|
2011-07-04 10:35:39 +02:00
|
|
|
}
|
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad aconvert_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.min_perms = AV_PERM_READ,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad aconvert_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2011-07-04 10:35:39 +02:00
|
|
|
AVFilter avfilter_af_aconvert = {
|
|
|
|
.name = "aconvert",
|
2012-05-12 17:38:47 +02:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
|
2011-07-04 10:35:39 +02:00
|
|
|
.priv_size = sizeof(AConvertContext),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = aconvert_inputs,
|
|
|
|
.outputs = aconvert_outputs,
|
2011-07-04 10:35:39 +02:00
|
|
|
};
|