3a9e227f |
/*
* 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
*/
|
e3c6b612 |
#include "libavutil/avstring.h" |
1acd2f6b |
#include "libavutil/channel_layout.h" |
e3c6b612 |
#include "libavutil/opt.h" |
b13d39bd |
#include "libavutil/samplefmt.h"
#include "libavutil/avassert.h" |
e96be840 |
#include "libswresample/swresample.h" |
3a9e227f |
#include "avfilter.h" |
4522df52 |
#include "audio.h" |
3a9e227f |
#include "internal.h"
typedef struct {
double ratio; |
e96be840 |
struct SwrContext *swr; |
847943bc |
int64_t next_pts; |
411689b5 |
int req_fullfilled; |
3a9e227f |
} AResampleContext;
|
052f4f85 |
static av_cold int init(AVFilterContext *ctx, const char *args) |
3a9e227f |
{
AResampleContext *aresample = ctx->priv; |
e3c6b612 |
int ret = 0;
char *argd = av_strdup(args);
|
847943bc |
aresample->next_pts = AV_NOPTS_VALUE; |
e3c6b612 |
aresample->swr = swr_alloc(); |
9ba2484e |
if (!aresample->swr) {
ret = AVERROR(ENOMEM);
goto end;
} |
3a9e227f |
if (args) { |
e06c1475 |
char *ptr = argd, *token; |
e3c6b612 |
|
e06c1475 |
while (token = av_strtok(ptr, ":", &ptr)) { |
e3c6b612 |
char *value;
av_strtok(token, "=", &value);
|
e06c1475 |
if (value) {
if ((ret = av_opt_set(aresample->swr, token, value, 0)) < 0) |
e3c6b612 |
goto end; |
b13d39bd |
} else {
int out_rate;
if ((ret = ff_parse_sample_rate(&out_rate, token, ctx)) < 0)
goto end; |
e06c1475 |
if ((ret = av_opt_set_int(aresample->swr, "osr", out_rate, 0)) < 0) |
b13d39bd |
goto end;
} |
e3c6b612 |
} |
3a9e227f |
} |
e3c6b612 |
end:
av_free(argd);
return ret; |
3a9e227f |
}
static av_cold void uninit(AVFilterContext *ctx)
{
AResampleContext *aresample = ctx->priv; |
e96be840 |
swr_free(&aresample->swr); |
3a9e227f |
}
|
1cbf7fb4 |
static int query_formats(AVFilterContext *ctx)
{
AResampleContext *aresample = ctx->priv; |
b13d39bd |
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); |
1cbf7fb4 |
AVFilterLink *inlink = ctx->inputs[0];
AVFilterLink *outlink = ctx->outputs[0];
|
c9e183b4 |
AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO); |
b13d39bd |
AVFilterFormats *out_formats; |
1cbf7fb4 |
AVFilterFormats *in_samplerates = ff_all_samplerates();
AVFilterFormats *out_samplerates;
AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts(); |
b13d39bd |
AVFilterChannelLayouts *out_layouts; |
1cbf7fb4 |
|
c9e183b4 |
ff_formats_ref (in_formats, &inlink->out_formats);
ff_formats_ref (in_samplerates, &inlink->out_samplerates); |
b13d39bd |
ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts); |
1cbf7fb4 |
|
b13d39bd |
if(out_rate > 0) { |
c9e183b4 |
out_samplerates = ff_make_format_list((int[]){ out_rate, -1 }); |
1cbf7fb4 |
} else {
out_samplerates = ff_all_samplerates();
} |
c9e183b4 |
ff_formats_ref(out_samplerates, &outlink->in_samplerates); |
b13d39bd |
if(out_format != AV_SAMPLE_FMT_NONE) { |
c9e183b4 |
out_formats = ff_make_format_list((int[]){ out_format, -1 }); |
b13d39bd |
} else |
1c600888 |
out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO); |
c9e183b4 |
ff_formats_ref(out_formats, &outlink->in_formats); |
b13d39bd |
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); |
1cbf7fb4 |
return 0;
}
|
3a9e227f |
static int config_output(AVFilterLink *outlink)
{ |
e96be840 |
int ret; |
3a9e227f |
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = ctx->inputs[0];
AResampleContext *aresample = ctx->priv; |
b13d39bd |
int out_rate;
uint64_t out_layout;
enum AVSampleFormat out_format; |
b0b7a89b |
char inchl_buf[128], outchl_buf[128]; |
3a9e227f |
|
e96be840 |
aresample->swr = swr_alloc_set_opts(aresample->swr, |
75492cce |
outlink->channel_layout, outlink->format, outlink->sample_rate, |
e96be840 |
inlink->channel_layout, inlink->format, inlink->sample_rate,
0, ctx);
if (!aresample->swr)
return AVERROR(ENOMEM); |
b13d39bd |
|
e96be840 |
ret = swr_init(aresample->swr);
if (ret < 0)
return ret; |
3a9e227f |
|
b13d39bd |
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);
|
3a9e227f |
aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
|
b0b7a89b |
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); |
62975afa |
|
fda968aa |
av_log(ctx, AV_LOG_VERBOSE, "chl:%s fmt:%s r:%dHz -> chl:%s fmt:%s r:%dHz\n", |
b0b7a89b |
inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate); |
3a9e227f |
return 0;
}
|
cd7febd3 |
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamplesref) |
3a9e227f |
{ |
e96be840 |
AResampleContext *aresample = inlink->dst->priv;
const int n_in = insamplesref->audio->nb_samples; |
616e6ffa |
int n_out = n_in * aresample->ratio * 2 + 256; |
e96be840 |
AVFilterLink *const outlink = inlink->dst->outputs[0]; |
01590329 |
AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out); |
f8911b98 |
int ret; |
e96be840 |
|
014058ab |
if(!outsamplesref)
return AVERROR(ENOMEM);
|
e96be840 |
avfilter_copy_buffer_ref_props(outsamplesref, insamplesref); |
b5bea2f9 |
outsamplesref->format = outlink->format; |
238edd2f |
outsamplesref->audio->channels = outlink->channels; |
b5bea2f9 |
outsamplesref->audio->channel_layout = outlink->channel_layout;
outsamplesref->audio->sample_rate = outlink->sample_rate; |
847943bc |
|
21d8a80e |
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 = |
4b5a12a2 |
outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate); |
21d8a80e |
} else {
outsamplesref->pts = AV_NOPTS_VALUE;
} |
be97675e |
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); |
f8911b98 |
return 0; |
be97675e |
}
outsamplesref->audio->nb_samples = n_out;
|
cd7febd3 |
ret = ff_filter_frame(outlink, outsamplesref); |
411689b5 |
aresample->req_fullfilled= 1; |
3a9e227f |
avfilter_unref_buffer(insamplesref); |
f8911b98 |
return ret; |
3a9e227f |
}
|
847943bc |
static int request_frame(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AResampleContext *aresample = ctx->priv; |
21d8a80e |
AVFilterLink *const inlink = outlink->src->inputs[0]; |
411689b5 |
int ret;
aresample->req_fullfilled = 0;
do{ |
1c600888 |
ret = ff_request_frame(ctx->inputs[0]); |
411689b5 |
}while(!aresample->req_fullfilled && ret>=0); |
847943bc |
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); |
e17a0396 |
n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0); |
847943bc |
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; |
21d8a80e |
#if 0 |
847943bc |
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); |
21d8a80e |
#else |
8d231e37 |
outsamplesref->pts = swr_next_pts(aresample->swr, INT64_MIN);
outsamplesref->pts = ROUNDED_DIV(outsamplesref->pts, inlink->sample_rate); |
21d8a80e |
#endif |
847943bc |
|
cd7febd3 |
ff_filter_frame(outlink, outsamplesref); |
847943bc |
return 0;
}
return ret;
}
|
2d9d4440 |
static const AVFilterPad aresample_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_frame = filter_frame,
.min_perms = AV_PERM_READ,
},
{ NULL },
};
static const AVFilterPad aresample_outputs[] = {
{
.name = "default",
.config_props = config_output,
.request_frame = request_frame,
.type = AVMEDIA_TYPE_AUDIO,
},
{ NULL },
};
|
3a9e227f |
AVFilter avfilter_af_aresample = {
.name = "aresample",
.description = NULL_IF_CONFIG_SMALL("Resample audio data."),
.init = init,
.uninit = uninit, |
1cbf7fb4 |
.query_formats = query_formats, |
3a9e227f |
.priv_size = sizeof(AResampleContext), |
2d9d4440 |
.inputs = aresample_inputs,
.outputs = aresample_outputs, |
3a9e227f |
}; |