2011-08-17 14:00:20 +03: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-05-17 14:10:14 +03:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/opt.h"
|
2012-05-17 17:12:54 +03:00
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
#include "libavutil/avassert.h"
|
2012-01-21 22:26:31 +03:00
|
|
|
#include "libswresample/swresample.h"
|
2011-08-17 14:00:20 +03:00
|
|
|
#include "avfilter.h"
|
2012-05-12 18:58:14 +03:00
|
|
|
#include "audio.h"
|
2011-08-17 14:00:20 +03:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
double ratio;
|
2012-01-21 22:26:31 +03:00
|
|
|
struct SwrContext *swr;
|
2012-05-17 23:39:02 +03:00
|
|
|
int64_t next_pts;
|
2012-05-26 03:28:40 +03:00
|
|
|
int req_fullfilled;
|
2011-08-17 14:00:20 +03:00
|
|
|
} AResampleContext;
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
|
|
|
|
{
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-05-17 14:10:14 +03:00
|
|
|
int ret = 0;
|
|
|
|
char *argd = av_strdup(args);
|
|
|
|
|
2012-05-17 23:39:02 +03:00
|
|
|
aresample->next_pts = AV_NOPTS_VALUE;
|
2012-05-17 14:10:14 +03:00
|
|
|
aresample->swr = swr_alloc();
|
|
|
|
if (!aresample->swr)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-08-17 14:00:20 +03:00
|
|
|
|
|
|
|
if (args) {
|
2012-05-17 14:10:14 +03:00
|
|
|
char *ptr=argd, *token;
|
|
|
|
|
|
|
|
while(token = av_strtok(ptr, ":", &ptr)) {
|
|
|
|
char *value;
|
|
|
|
av_strtok(token, "=", &value);
|
|
|
|
|
|
|
|
if(value) {
|
|
|
|
if((ret=av_opt_set(aresample->swr, token, value, 0)) < 0)
|
|
|
|
goto end;
|
2012-05-17 17:12:54 +03:00
|
|
|
} else {
|
|
|
|
int out_rate;
|
|
|
|
if ((ret = ff_parse_sample_rate(&out_rate, token, ctx)) < 0)
|
|
|
|
goto end;
|
|
|
|
if((ret = av_opt_set_int(aresample->swr, "osr", out_rate, 0)) < 0)
|
|
|
|
goto end;
|
|
|
|
}
|
2012-05-17 14:10:14 +03:00
|
|
|
}
|
2011-08-17 14:00:20 +03:00
|
|
|
}
|
2012-05-17 14:10:14 +03:00
|
|
|
end:
|
|
|
|
av_free(argd);
|
|
|
|
return ret;
|
2011-08-17 14:00:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-01-21 22:26:31 +03:00
|
|
|
swr_free(&aresample->swr);
|
2011-08-17 14:00:20 +03:00
|
|
|
}
|
|
|
|
|
2012-05-16 03:27:31 +03:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-05-17 17:12:54 +03:00
|
|
|
int out_rate = av_get_int(aresample->swr, "osr", NULL);
|
|
|
|
uint64_t out_layout = av_get_int(aresample->swr, "ocl", NULL);
|
|
|
|
enum AVSampleFormat out_format = av_get_int(aresample->swr, "osf", NULL);
|
2012-05-16 03:27:31 +03:00
|
|
|
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
|
|
|
|
AVFilterFormats *in_formats = avfilter_all_formats(AVMEDIA_TYPE_AUDIO);
|
2012-05-17 17:12:54 +03:00
|
|
|
AVFilterFormats *out_formats;
|
2012-05-16 03:27:31 +03:00
|
|
|
AVFilterFormats *in_samplerates = ff_all_samplerates();
|
|
|
|
AVFilterFormats *out_samplerates;
|
|
|
|
AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
|
2012-05-17 17:12:54 +03:00
|
|
|
AVFilterChannelLayouts *out_layouts;
|
2012-05-16 03:27:31 +03:00
|
|
|
|
2012-05-17 17:12:54 +03:00
|
|
|
avfilter_formats_ref (in_formats, &inlink->out_formats);
|
|
|
|
avfilter_formats_ref (in_samplerates, &inlink->out_samplerates);
|
|
|
|
ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
|
2012-05-16 03:27:31 +03:00
|
|
|
|
2012-05-17 17:12:54 +03:00
|
|
|
if(out_rate > 0) {
|
|
|
|
out_samplerates = avfilter_make_format_list((int[]){ out_rate, -1 });
|
2012-05-16 03:27:31 +03:00
|
|
|
} else {
|
|
|
|
out_samplerates = ff_all_samplerates();
|
|
|
|
}
|
2012-05-17 17:12:54 +03:00
|
|
|
avfilter_formats_ref(out_samplerates, &outlink->in_samplerates);
|
|
|
|
|
|
|
|
if(out_format != AV_SAMPLE_FMT_NONE) {
|
|
|
|
out_formats = avfilter_make_format_list((int[]){ out_format, -1 });
|
|
|
|
} else
|
|
|
|
out_formats = avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO);
|
|
|
|
avfilter_formats_ref(out_formats, &outlink->in_formats);
|
|
|
|
|
|
|
|
if(out_layout) {
|
|
|
|
out_layouts = avfilter_make_format64_list((int64_t[]){ out_layout, -1 });
|
|
|
|
} else
|
|
|
|
out_layouts = ff_all_channel_layouts();
|
|
|
|
ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
|
2012-05-16 03:27:31 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-17 14:00:20 +03:00
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
2012-01-21 22:26:31 +03:00
|
|
|
int ret;
|
2011-08-17 14:00:20 +03:00
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-05-17 17:12:54 +03:00
|
|
|
int out_rate;
|
|
|
|
uint64_t out_layout;
|
|
|
|
enum AVSampleFormat out_format;
|
2012-05-25 01:07:40 +03:00
|
|
|
char inchl_buf[128], outchl_buf[128];
|
2011-08-17 14:00:20 +03:00
|
|
|
|
2012-01-21 22:26:31 +03:00
|
|
|
aresample->swr = swr_alloc_set_opts(aresample->swr,
|
2012-05-16 04:53:29 +03:00
|
|
|
outlink->channel_layout, outlink->format, outlink->sample_rate,
|
2012-01-21 22:26:31 +03:00
|
|
|
inlink->channel_layout, inlink->format, inlink->sample_rate,
|
|
|
|
0, ctx);
|
|
|
|
if (!aresample->swr)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-05-17 17:12:54 +03:00
|
|
|
|
2012-01-21 22:26:31 +03:00
|
|
|
ret = swr_init(aresample->swr);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-08-17 14:00:20 +03:00
|
|
|
|
2012-05-17 17:12:54 +03:00
|
|
|
out_rate = av_get_int(aresample->swr, "osr", NULL);
|
|
|
|
out_layout = av_get_int(aresample->swr, "ocl", NULL);
|
|
|
|
out_format = av_get_int(aresample->swr, "osf", NULL);
|
|
|
|
outlink->time_base = (AVRational) {1, out_rate};
|
|
|
|
|
|
|
|
av_assert0(outlink->sample_rate == out_rate);
|
|
|
|
av_assert0(outlink->channel_layout == out_layout);
|
|
|
|
av_assert0(outlink->format == out_format);
|
|
|
|
|
2011-08-17 14:00:20 +03:00
|
|
|
aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
|
|
|
|
|
2012-05-25 01:07:40 +03:00
|
|
|
av_get_channel_layout_string(inchl_buf, sizeof(inchl_buf), -1, inlink ->channel_layout);
|
|
|
|
av_get_channel_layout_string(outchl_buf, sizeof(outchl_buf), -1, outlink->channel_layout);
|
|
|
|
av_log(ctx, AV_LOG_INFO, "chl:%s fmt:%s r:%"PRId64"Hz -> chl:%s fmt:%s r:%"PRId64"Hz\n",
|
|
|
|
inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
|
|
|
|
outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
|
2011-08-17 14:00:20 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
|
|
|
|
{
|
2012-01-21 22:26:31 +03:00
|
|
|
AResampleContext *aresample = inlink->dst->priv;
|
|
|
|
const int n_in = insamplesref->audio->nb_samples;
|
2012-05-19 19:48:05 +03:00
|
|
|
int n_out = n_in * aresample->ratio * 2 ;
|
2012-01-21 22:26:31 +03:00
|
|
|
AVFilterLink *const outlink = inlink->dst->outputs[0];
|
2012-05-10 23:41:29 +03:00
|
|
|
AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
|
2012-01-21 22:26:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
|
2012-05-17 23:39:02 +03:00
|
|
|
|
2012-05-19 19:48:53 +03:00
|
|
|
if(insamplesref->pts != AV_NOPTS_VALUE) {
|
|
|
|
int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
|
|
|
|
int64_t outpts= swr_next_pts(aresample->swr, inpts);
|
|
|
|
aresample->next_pts =
|
|
|
|
outsamplesref->pts = (outpts + inlink->sample_rate/2) / inlink->sample_rate;
|
|
|
|
} else {
|
|
|
|
outsamplesref->pts = AV_NOPTS_VALUE;
|
|
|
|
}
|
2012-05-26 17:17:47 +03:00
|
|
|
|
|
|
|
n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
|
|
|
|
(void *)insamplesref->extended_data, n_in);
|
|
|
|
if (n_out <= 0) {
|
|
|
|
avfilter_unref_buffer(outsamplesref);
|
|
|
|
avfilter_unref_buffer(insamplesref);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
outsamplesref->audio->sample_rate = outlink->sample_rate;
|
|
|
|
outsamplesref->audio->nb_samples = n_out;
|
|
|
|
|
2012-05-10 23:41:29 +03:00
|
|
|
ff_filter_samples(outlink, outsamplesref);
|
2012-05-26 03:28:40 +03:00
|
|
|
aresample->req_fullfilled= 1;
|
2011-08-17 14:00:20 +03:00
|
|
|
avfilter_unref_buffer(insamplesref);
|
|
|
|
}
|
|
|
|
|
2012-05-17 23:39:02 +03:00
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-05-19 19:48:53 +03:00
|
|
|
AVFilterLink *const inlink = outlink->src->inputs[0];
|
2012-05-26 03:28:40 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
aresample->req_fullfilled = 0;
|
|
|
|
do{
|
|
|
|
ret = avfilter_request_frame(ctx->inputs[0]);
|
|
|
|
}while(!aresample->req_fullfilled && ret>=0);
|
2012-05-17 23:39:02 +03:00
|
|
|
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
AVFilterBufferRef *outsamplesref;
|
|
|
|
int n_out = 4096;
|
|
|
|
|
|
|
|
outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
|
|
|
|
if (!outsamplesref)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-05-19 19:46:25 +03:00
|
|
|
n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
|
2012-05-17 23:39:02 +03:00
|
|
|
if (n_out <= 0) {
|
|
|
|
avfilter_unref_buffer(outsamplesref);
|
|
|
|
return (n_out == 0) ? AVERROR_EOF : n_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
outsamplesref->audio->sample_rate = outlink->sample_rate;
|
|
|
|
outsamplesref->audio->nb_samples = n_out;
|
2012-05-19 19:48:53 +03:00
|
|
|
#if 0
|
2012-05-17 23:39:02 +03:00
|
|
|
outsamplesref->pts = aresample->next_pts;
|
|
|
|
if(aresample->next_pts != AV_NOPTS_VALUE)
|
|
|
|
aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
|
2012-05-19 19:48:53 +03:00
|
|
|
#else
|
|
|
|
outsamplesref->pts = (swr_next_pts(aresample->swr, INT64_MIN) + inlink->sample_rate/2) / inlink->sample_rate;
|
|
|
|
#endif
|
2012-05-17 23:39:02 +03:00
|
|
|
|
|
|
|
ff_filter_samples(outlink, outsamplesref);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-17 14:00:20 +03:00
|
|
|
AVFilter avfilter_af_aresample = {
|
|
|
|
.name = "aresample",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Resample audio data."),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2012-05-16 03:27:31 +03:00
|
|
|
.query_formats = query_formats,
|
2011-08-17 14:00:20 +03:00
|
|
|
.priv_size = sizeof(AResampleContext),
|
|
|
|
|
2011-11-05 16:39:24 +03:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-08-17 14:00:20 +03:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_samples = filter_samples,
|
|
|
|
.min_perms = AV_PERM_READ, },
|
|
|
|
{ .name = NULL}},
|
2011-11-05 16:39:24 +03:00
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-08-17 14:00:20 +03:00
|
|
|
.config_props = config_output,
|
2012-05-17 23:39:02 +03:00
|
|
|
.request_frame = request_frame,
|
2011-08-17 14:00:20 +03:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO, },
|
|
|
|
{ .name = NULL}},
|
|
|
|
};
|