|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "libavutil/opt.h" |
|
#include "avfilter.h" |
|
#include "formats.h" |
|
#include "internal.h" |
|
|
|
typedef struct ASetRateContext { |
|
const AVClass *class; |
|
int sample_rate; |
|
int rescale_pts; |
|
} ASetRateContext; |
|
|
|
#define CONTEXT ASetRateContext |
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
|
|
#define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \ |
|
{ name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \ |
|
{ .deffield = def }, min, max, FLAGS, __VA_ARGS__ } |
|
|
|
#define OPT_INT(name, field, def, min, max, descr, ...) \ |
|
OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__) |
|
|
|
static const AVOption asetrate_options[] = { |
|
OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate",), |
|
OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate",), |
|
{NULL}, |
|
}; |
|
|
|
AVFILTER_DEFINE_CLASS(asetrate); |
|
|
|
static av_cold int query_formats(AVFilterContext *ctx) |
|
{ |
|
ASetRateContext *sr = ctx->priv; |
|
int ret, sample_rates[] = { sr->sample_rate, -1 }; |
|
|
|
if ((ret = ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO))) < 0) |
|
return ret; |
|
|
|
if ((ret = ff_set_common_all_channel_counts(ctx)) < 0) |
|
return ret; |
|
|
|
if ((ret = ff_formats_ref(ff_all_samplerates(), |
|
&ctx->inputs[0]->outcfg.samplerates)) < 0) |
|
return ret; |
|
|
|
return ff_formats_ref(ff_make_format_list(sample_rates), |
|
&ctx->outputs[0]->incfg.samplerates); |
|
} |
|
|
|
static av_cold int config_props(AVFilterLink *outlink) |
|
{ |
|
AVFilterContext *ctx = outlink->src; |
|
ASetRateContext *sr = ctx->priv; |
|
AVFilterLink *inlink = ctx->inputs[0]; |
|
AVRational intb = ctx->inputs[0]->time_base; |
|
int inrate = inlink->sample_rate; |
|
|
|
if (intb.num == 1 && intb.den == inrate) { |
|
outlink->time_base.num = 1; |
|
outlink->time_base.den = outlink->sample_rate; |
|
} else { |
|
outlink->time_base = intb; |
|
sr->rescale_pts = 1; |
|
if (av_q2d(intb) > 1.0 / FFMAX(inrate, outlink->sample_rate)) |
|
av_log(ctx, AV_LOG_WARNING, "Time base is inaccurate\n"); |
|
} |
|
return 0; |
|
} |
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame) |
|
{ |
|
AVFilterContext *ctx = inlink->dst; |
|
ASetRateContext *sr = ctx->priv; |
|
AVFilterLink *outlink = ctx->outputs[0]; |
|
|
|
frame->sample_rate = outlink->sample_rate; |
|
if (sr->rescale_pts) |
|
frame->pts = av_rescale(frame->pts, inlink->sample_rate, |
|
outlink->sample_rate); |
|
return ff_filter_frame(outlink, frame); |
|
} |
|
|
|
static const AVFilterPad asetrate_inputs[] = { |
|
{ |
|
.name = "default", |
|
.type = AVMEDIA_TYPE_AUDIO, |
|
.filter_frame = filter_frame, |
|
}, |
|
}; |
|
|
|
static const AVFilterPad asetrate_outputs[] = { |
|
{ |
|
.name = "default", |
|
.type = AVMEDIA_TYPE_AUDIO, |
|
.config_props = config_props, |
|
}, |
|
}; |
|
|
|
const AVFilter ff_af_asetrate = { |
|
.name = "asetrate", |
|
.description = NULL_IF_CONFIG_SMALL("Change the sample rate without " |
|
"altering the data."), |
|
.priv_size = sizeof(ASetRateContext), |
|
FILTER_INPUTS(asetrate_inputs), |
|
FILTER_OUTPUTS(asetrate_outputs), |
|
FILTER_QUERY_FUNC(query_formats), |
|
.priv_class = &asetrate_class, |
|
.flags = AVFILTER_FLAG_METADATA_ONLY, |
|
}; |
|
|