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-05-17 13:10:14 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2012-11-13 14:16:48 +01:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-05-17 13:10:14 +02:00
|
|
|
#include "libavutil/opt.h"
|
2012-05-17 16:12:54 +02:00
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
#include "libavutil/avassert.h"
|
2012-01-21 20:26:31 +01:00
|
|
|
#include "libswresample/swresample.h"
|
2011-08-17 13:00:20 +02:00
|
|
|
#include "avfilter.h"
|
2012-05-12 17:58:14 +02:00
|
|
|
#include "audio.h"
|
2011-08-17 13:00:20 +02:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2013-04-11 20:23:52 +02:00
|
|
|
const AVClass *class;
|
|
|
|
int sample_rate_arg;
|
2011-08-17 13:00:20 +02:00
|
|
|
double ratio;
|
2012-01-21 20:26:31 +01:00
|
|
|
struct SwrContext *swr;
|
2012-05-17 22:39:02 +02:00
|
|
|
int64_t next_pts;
|
2012-05-26 02:28:40 +02:00
|
|
|
int req_fullfilled;
|
2011-08-17 13:00:20 +02:00
|
|
|
} AResampleContext;
|
|
|
|
|
2013-04-11 20:23:52 +02:00
|
|
|
static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
|
2011-08-17 13:00:20 +02:00
|
|
|
{
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-05-17 13:10:14 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
2012-05-17 22:39:02 +02:00
|
|
|
aresample->next_pts = AV_NOPTS_VALUE;
|
2012-05-17 13:10:14 +02:00
|
|
|
aresample->swr = swr_alloc();
|
2012-10-11 16:48:25 +02:00
|
|
|
if (!aresample->swr) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
2011-08-17 13:00:20 +02:00
|
|
|
|
2013-04-11 20:23:52 +02:00
|
|
|
if (opts) {
|
|
|
|
AVDictionaryEntry *e = NULL;
|
|
|
|
|
|
|
|
while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
|
2013-08-16 04:25:39 +02:00
|
|
|
if ((ret = av_opt_set(aresample->swr, e->key, e->value, 0)) < 0)
|
2013-04-11 20:23:52 +02:00
|
|
|
goto end;
|
2012-05-17 13:10:14 +02:00
|
|
|
}
|
2013-04-11 20:23:52 +02:00
|
|
|
av_dict_free(opts);
|
2011-08-17 13:00:20 +02:00
|
|
|
}
|
2013-04-11 20:23:52 +02:00
|
|
|
if (aresample->sample_rate_arg > 0)
|
|
|
|
av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
|
2012-05-17 13:10:14 +02:00
|
|
|
end:
|
|
|
|
return ret;
|
2011-08-17 13:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-05-16 02:27:31 +02:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-05-17 16:12:54 +02: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 02:27:31 +02:00
|
|
|
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
|
2012-06-16 11:47:46 +02:00
|
|
|
AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
|
2012-05-17 16:12:54 +02:00
|
|
|
AVFilterFormats *out_formats;
|
2012-05-16 02:27:31 +02:00
|
|
|
AVFilterFormats *in_samplerates = ff_all_samplerates();
|
|
|
|
AVFilterFormats *out_samplerates;
|
2012-12-26 16:51:50 +01:00
|
|
|
AVFilterChannelLayouts *in_layouts = ff_all_channel_counts();
|
2012-05-17 16:12:54 +02:00
|
|
|
AVFilterChannelLayouts *out_layouts;
|
2012-05-16 02:27:31 +02:00
|
|
|
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_formats_ref (in_formats, &inlink->out_formats);
|
|
|
|
ff_formats_ref (in_samplerates, &inlink->out_samplerates);
|
2012-05-17 16:12:54 +02:00
|
|
|
ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
|
2012-05-16 02:27:31 +02:00
|
|
|
|
2012-05-17 16:12:54 +02:00
|
|
|
if(out_rate > 0) {
|
2012-06-16 11:47:46 +02:00
|
|
|
out_samplerates = ff_make_format_list((int[]){ out_rate, -1 });
|
2012-05-16 02:27:31 +02:00
|
|
|
} else {
|
|
|
|
out_samplerates = ff_all_samplerates();
|
|
|
|
}
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_formats_ref(out_samplerates, &outlink->in_samplerates);
|
2012-05-17 16:12:54 +02:00
|
|
|
|
|
|
|
if(out_format != AV_SAMPLE_FMT_NONE) {
|
2012-06-16 11:47:46 +02:00
|
|
|
out_formats = ff_make_format_list((int[]){ out_format, -1 });
|
2012-05-17 16:12:54 +02:00
|
|
|
} else
|
2012-06-24 02:09:53 +02:00
|
|
|
out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
|
2012-06-16 11:47:46 +02:00
|
|
|
ff_formats_ref(out_formats, &outlink->in_formats);
|
2012-05-17 16:12:54 +02:00
|
|
|
|
|
|
|
if(out_layout) {
|
|
|
|
out_layouts = avfilter_make_format64_list((int64_t[]){ out_layout, -1 });
|
|
|
|
} else
|
2012-12-26 16:51:50 +01:00
|
|
|
out_layouts = ff_all_channel_counts();
|
2012-05-17 16:12:54 +02:00
|
|
|
ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
|
2012-05-16 02:27:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2012-05-17 16:12:54 +02:00
|
|
|
int out_rate;
|
|
|
|
uint64_t out_layout;
|
|
|
|
enum AVSampleFormat out_format;
|
2012-05-25 00:07:40 +02:00
|
|
|
char inchl_buf[128], outchl_buf[128];
|
2011-08-17 13:00:20 +02:00
|
|
|
|
2012-01-21 20:26:31 +01:00
|
|
|
aresample->swr = swr_alloc_set_opts(aresample->swr,
|
2012-05-16 03:53:29 +02:00
|
|
|
outlink->channel_layout, outlink->format, outlink->sample_rate,
|
2012-01-21 20:26:31 +01:00
|
|
|
inlink->channel_layout, inlink->format, inlink->sample_rate,
|
|
|
|
0, ctx);
|
|
|
|
if (!aresample->swr)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-12-26 16:51:50 +01:00
|
|
|
if (!inlink->channel_layout)
|
|
|
|
av_opt_set_int(aresample->swr, "ich", inlink->channels, 0);
|
|
|
|
if (!outlink->channel_layout)
|
|
|
|
av_opt_set_int(aresample->swr, "och", outlink->channels, 0);
|
2012-05-17 16:12:54 +02:00
|
|
|
|
2012-01-21 20:26:31 +01:00
|
|
|
ret = swr_init(aresample->swr);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-08-17 13:00:20 +02:00
|
|
|
|
2012-05-17 16:12:54 +02: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);
|
2013-02-26 20:11:47 +01:00
|
|
|
av_assert0(outlink->channel_layout == out_layout || !outlink->channel_layout);
|
2012-05-17 16:12:54 +02:00
|
|
|
av_assert0(outlink->format == out_format);
|
|
|
|
|
2011-08-17 13:00:20 +02:00
|
|
|
aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
|
|
|
|
|
2013-02-26 20:13:57 +01:00
|
|
|
av_get_channel_layout_string(inchl_buf, sizeof(inchl_buf), inlink ->channels, inlink ->channel_layout);
|
|
|
|
av_get_channel_layout_string(outchl_buf, sizeof(outchl_buf), outlink->channels, outlink->channel_layout);
|
2012-06-24 15:13:39 +02:00
|
|
|
|
2012-12-26 16:51:50 +01:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n",
|
|
|
|
inlink ->channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
|
|
|
|
outlink->channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
|
2011-08-17 13:00:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
|
2011-08-17 13:00:20 +02:00
|
|
|
{
|
2012-01-21 20:26:31 +01:00
|
|
|
AResampleContext *aresample = inlink->dst->priv;
|
2013-03-10 01:30:30 +01:00
|
|
|
const int n_in = insamplesref->nb_samples;
|
2012-12-13 18:17:48 +01:00
|
|
|
int n_out = n_in * aresample->ratio * 2 + 256;
|
2012-01-21 20:26:31 +01:00
|
|
|
AVFilterLink *const outlink = inlink->dst->outputs[0];
|
2013-03-10 01:30:30 +01:00
|
|
|
AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n_out);
|
2012-07-09 22:10:38 +02:00
|
|
|
int ret;
|
2012-01-21 20:26:31 +01:00
|
|
|
|
2012-11-24 17:33:32 +01:00
|
|
|
if(!outsamplesref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
av_frame_copy_props(outsamplesref, insamplesref);
|
2012-09-12 19:34:21 +02:00
|
|
|
outsamplesref->format = outlink->format;
|
2013-03-15 16:13:53 +01:00
|
|
|
av_frame_set_channels(outsamplesref, outlink->channels);
|
2013-03-10 01:30:30 +01:00
|
|
|
outsamplesref->channel_layout = outlink->channel_layout;
|
|
|
|
outsamplesref->sample_rate = outlink->sample_rate;
|
2012-05-17 22:39:02 +02:00
|
|
|
|
2012-05-19 18:48:53 +02: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 =
|
2012-11-26 16:25:47 +01:00
|
|
|
outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate);
|
2012-05-19 18:48:53 +02:00
|
|
|
} else {
|
|
|
|
outsamplesref->pts = AV_NOPTS_VALUE;
|
|
|
|
}
|
2012-05-26 16:17:47 +02:00
|
|
|
n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
|
|
|
|
(void *)insamplesref->extended_data, n_in);
|
|
|
|
if (n_out <= 0) {
|
2013-03-10 01:30:30 +01:00
|
|
|
av_frame_free(&outsamplesref);
|
|
|
|
av_frame_free(&insamplesref);
|
2012-07-09 22:10:38 +02:00
|
|
|
return 0;
|
2012-05-26 16:17:47 +02:00
|
|
|
}
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
outsamplesref->nb_samples = n_out;
|
2012-05-26 16:17:47 +02:00
|
|
|
|
2012-11-28 13:53:48 +01:00
|
|
|
ret = ff_filter_frame(outlink, outsamplesref);
|
2012-05-26 02:28:40 +02:00
|
|
|
aresample->req_fullfilled= 1;
|
2013-03-10 01:30:30 +01:00
|
|
|
av_frame_free(&insamplesref);
|
2012-07-09 22:10:38 +02:00
|
|
|
return ret;
|
2011-08-17 13:00:20 +02:00
|
|
|
}
|
|
|
|
|
2012-05-17 22:39:02 +02:00
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AResampleContext *aresample = ctx->priv;
|
2012-05-19 18:48:53 +02:00
|
|
|
AVFilterLink *const inlink = outlink->src->inputs[0];
|
2012-05-26 02:28:40 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
aresample->req_fullfilled = 0;
|
|
|
|
do{
|
2012-06-24 02:09:53 +02:00
|
|
|
ret = ff_request_frame(ctx->inputs[0]);
|
2012-05-26 02:28:40 +02:00
|
|
|
}while(!aresample->req_fullfilled && ret>=0);
|
2012-05-17 22:39:02 +02:00
|
|
|
|
|
|
|
if (ret == AVERROR_EOF) {
|
2013-03-10 01:30:30 +01:00
|
|
|
AVFrame *outsamplesref;
|
2012-05-17 22:39:02 +02:00
|
|
|
int n_out = 4096;
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
outsamplesref = ff_get_audio_buffer(outlink, n_out);
|
2012-05-17 22:39:02 +02:00
|
|
|
if (!outsamplesref)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-05-19 18:46:25 +02:00
|
|
|
n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
|
2012-05-17 22:39:02 +02:00
|
|
|
if (n_out <= 0) {
|
2013-03-10 01:30:30 +01:00
|
|
|
av_frame_free(&outsamplesref);
|
2012-05-17 22:39:02 +02:00
|
|
|
return (n_out == 0) ? AVERROR_EOF : n_out;
|
|
|
|
}
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
outsamplesref->sample_rate = outlink->sample_rate;
|
|
|
|
outsamplesref->nb_samples = n_out;
|
2012-05-19 18:48:53 +02:00
|
|
|
#if 0
|
2012-05-17 22:39:02 +02: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 18:48:53 +02:00
|
|
|
#else
|
2012-11-26 20:17:16 +01:00
|
|
|
outsamplesref->pts = swr_next_pts(aresample->swr, INT64_MIN);
|
|
|
|
outsamplesref->pts = ROUNDED_DIV(outsamplesref->pts, inlink->sample_rate);
|
2012-05-19 18:48:53 +02:00
|
|
|
#endif
|
2012-05-17 22:39:02 +02:00
|
|
|
|
2013-03-13 09:55:18 +01:00
|
|
|
return ff_filter_frame(outlink, outsamplesref);
|
2012-05-17 22:39:02 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-11 20:23:52 +02:00
|
|
|
static const AVClass *resample_child_class_next(const AVClass *prev)
|
|
|
|
{
|
|
|
|
return prev ? NULL : swr_get_class();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *resample_child_next(void *obj, void *prev)
|
|
|
|
{
|
|
|
|
AResampleContext *s = obj;
|
|
|
|
return prev ? NULL : s->swr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(AResampleContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{"sample_rate", NULL, OFFSET(sample_rate_arg), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass aresample_class = {
|
|
|
|
.class_name = "aresample",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.child_class_next = resample_child_class_next,
|
|
|
|
.child_next = resample_child_next,
|
|
|
|
};
|
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad aresample_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
2013-09-07 14:13:50 +02:00
|
|
|
{ NULL }
|
2012-11-28 20:01:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad aresample_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.config_props = config_output,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
2013-09-07 14:13:50 +02:00
|
|
|
{ NULL }
|
2012-11-28 20:01:59 +01:00
|
|
|
};
|
|
|
|
|
2011-08-17 13:00:20 +02:00
|
|
|
AVFilter avfilter_af_aresample = {
|
|
|
|
.name = "aresample",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Resample audio data."),
|
2013-04-11 20:23:52 +02:00
|
|
|
.init_dict = init_dict,
|
2011-08-17 13:00:20 +02:00
|
|
|
.uninit = uninit,
|
2012-05-16 02:27:31 +02:00
|
|
|
.query_formats = query_formats,
|
2011-08-17 13:00:20 +02:00
|
|
|
.priv_size = sizeof(AResampleContext),
|
2013-04-11 20:23:52 +02:00
|
|
|
.priv_class = &aresample_class,
|
2012-11-28 20:01:59 +01:00
|
|
|
.inputs = aresample_inputs,
|
|
|
|
.outputs = aresample_outputs,
|
2011-08-17 13:00:20 +02:00
|
|
|
};
|