2011-08-17 13:00:20 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* resampling audio filter
|
|
|
|
*/
|
|
|
|
|
2012-01-21 20:26:31 +01:00
|
|
|
#include "libswresample/swresample.h"
|
2011-08-17 13:00:20 +02:00
|
|
|
#include "avfilter.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int out_rate;
|
|
|
|
double ratio;
|
2012-01-21 20:26:31 +01:00
|
|
|
struct SwrContext *swr;
|
2011-08-17 13:00:20 +02:00
|
|
|
} AResampleContext;
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
|
|
|
|
{
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (args) {
|
|
|
|
if ((ret = ff_parse_sample_rate(&aresample->out_rate, args, ctx)) < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
aresample->out_rate = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-01-21 20:26:31 +01:00
|
|
|
swr_free(&aresample->swr);
|
2011-08-17 13:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
2012-01-21 20:26:31 +01:00
|
|
|
int ret;
|
2011-08-17 13:00:20 +02:00
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
|
|
|
|
|
|
|
if (aresample->out_rate == -1)
|
|
|
|
aresample->out_rate = outlink->sample_rate;
|
|
|
|
else
|
|
|
|
outlink->sample_rate = aresample->out_rate;
|
2011-09-17 01:30:15 +02:00
|
|
|
outlink->time_base = (AVRational) {1, aresample->out_rate};
|
2011-08-17 13:00:20 +02:00
|
|
|
|
2012-01-21 20:26:31 +01:00
|
|
|
//TODO: make the resampling parameters (filter size, phrase shift, linear, cutoff) configurable
|
|
|
|
aresample->swr = swr_alloc_set_opts(aresample->swr,
|
|
|
|
inlink->channel_layout, inlink->format, aresample->out_rate,
|
|
|
|
inlink->channel_layout, inlink->format, inlink->sample_rate,
|
|
|
|
0, ctx);
|
|
|
|
if (!aresample->swr)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = swr_init(aresample->swr);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-08-17 13:00:20 +02:00
|
|
|
|
|
|
|
aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_INFO, "r:%"PRId64"Hz -> r:%"PRId64"Hz\n",
|
|
|
|
inlink->sample_rate, outlink->sample_rate);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
|
|
|
|
{
|
2012-01-21 20:26:31 +01:00
|
|
|
AResampleContext *aresample = inlink->dst->priv;
|
|
|
|
const int n_in = insamplesref->audio->nb_samples;
|
|
|
|
int n_out = n_in * aresample->ratio;
|
|
|
|
AVFilterLink *const outlink = inlink->dst->outputs[0];
|
|
|
|
AVFilterBufferRef *outsamplesref = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
|
|
|
|
|
|
|
|
n_out = swr_convert(aresample->swr, outsamplesref->data, n_out,
|
|
|
|
(void *)insamplesref->data, n_in);
|
|
|
|
|
|
|
|
avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
|
|
|
|
outsamplesref->audio->sample_rate = outlink->sample_rate;
|
|
|
|
outsamplesref->audio->nb_samples = n_out;
|
2012-04-16 01:08:00 +02:00
|
|
|
outsamplesref->pts = insamplesref->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
|
|
|
|
av_rescale(outlink->sample_rate, insamplesref->pts, inlink ->sample_rate);
|
2012-01-21 20:26:31 +01:00
|
|
|
|
|
|
|
avfilter_filter_samples(outlink, outsamplesref);
|
2011-08-17 13:00:20 +02:00
|
|
|
avfilter_unref_buffer(insamplesref);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_af_aresample = {
|
|
|
|
.name = "aresample",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Resample audio data."),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(AResampleContext),
|
|
|
|
|
2011-11-05 14:39:24 +01:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-08-17 13:00:20 +02:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_samples = filter_samples,
|
|
|
|
.min_perms = AV_PERM_READ, },
|
|
|
|
{ .name = NULL}},
|
2011-11-05 14:39:24 +01:00
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-08-17 13:00:20 +02:00
|
|
|
.config_props = config_output,
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO, },
|
|
|
|
{ .name = NULL}},
|
|
|
|
};
|