2012-05-04 16:09:17 +03:00
|
|
|
/*
|
2013-08-16 00:12:51 +03:00
|
|
|
* This file is part of FFmpeg.
|
2012-05-04 16:09:17 +03:00
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-05-04 16:09:17 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-05-04 16:09:17 +03:00
|
|
|
* 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
|
2013-08-16 00:12:51 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-05-04 16:09:17 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* sample format and channel layout conversion audio filter
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2012-12-19 05:27:35 +03:00
|
|
|
#include "libavutil/dict.h"
|
2012-05-04 16:09:17 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "libavresample/avresample.h"
|
|
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
2012-05-06 08:00:22 +03:00
|
|
|
#include "formats.h"
|
2012-05-04 16:09:17 +03:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct ResampleContext {
|
2013-02-25 23:21:29 +03:00
|
|
|
const AVClass *class;
|
2012-05-04 16:09:17 +03:00
|
|
|
AVAudioResampleContext *avr;
|
2012-12-19 05:27:35 +03:00
|
|
|
AVDictionary *options;
|
2012-05-04 16:09:17 +03:00
|
|
|
|
2015-07-16 19:26:36 +02:00
|
|
|
int resampling;
|
2012-05-04 16:09:17 +03:00
|
|
|
int64_t next_pts;
|
2014-10-14 03:46:00 +03:00
|
|
|
int64_t next_in_pts;
|
2012-07-04 17:46:17 +03:00
|
|
|
|
2012-11-27 09:49:45 +03:00
|
|
|
/* set by filter_frame() to signal an output frame to request_frame() */
|
2012-07-04 17:46:17 +03:00
|
|
|
int got_output;
|
2012-05-04 16:09:17 +03:00
|
|
|
} ResampleContext;
|
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx, AVDictionary **opts)
|
2012-12-19 05:27:35 +03:00
|
|
|
{
|
|
|
|
ResampleContext *s = ctx->priv;
|
2013-02-25 23:21:29 +03:00
|
|
|
const AVClass *avr_class = avresample_get_class();
|
|
|
|
AVDictionaryEntry *e = NULL;
|
2012-12-19 05:27:35 +03:00
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
|
|
|
|
if (av_opt_find(&avr_class, e->key, NULL, 0,
|
|
|
|
AV_OPT_SEARCH_FAKE_OBJ | AV_OPT_SEARCH_CHILDREN))
|
|
|
|
av_dict_set(&s->options, e->key, e->value, 0);
|
2012-12-19 05:27:35 +03:00
|
|
|
}
|
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
e = NULL;
|
|
|
|
while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
|
|
|
|
av_dict_set(opts, e->key, NULL, 0);
|
|
|
|
|
|
|
|
/* do not allow the user to override basic format options */
|
|
|
|
av_dict_set(&s->options, "in_channel_layout", NULL, 0);
|
|
|
|
av_dict_set(&s->options, "out_channel_layout", NULL, 0);
|
|
|
|
av_dict_set(&s->options, "in_sample_fmt", NULL, 0);
|
|
|
|
av_dict_set(&s->options, "out_sample_fmt", NULL, 0);
|
|
|
|
av_dict_set(&s->options, "in_sample_rate", NULL, 0);
|
|
|
|
av_dict_set(&s->options, "out_sample_rate", NULL, 0);
|
|
|
|
|
2012-12-19 05:27:35 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-04 16:09:17 +03:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
ResampleContext *s = ctx->priv;
|
|
|
|
|
|
|
|
if (s->avr) {
|
|
|
|
avresample_close(s->avr);
|
|
|
|
avresample_free(&s->avr);
|
|
|
|
}
|
2012-12-19 05:27:35 +03:00
|
|
|
av_dict_free(&s->options);
|
2012-05-04 16:09:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2015-10-05 05:39:25 +02:00
|
|
|
AVFilterFormats *in_formats, *out_formats, *in_samplerates, *out_samplerates;
|
|
|
|
AVFilterChannelLayouts *in_layouts, *out_layouts;
|
|
|
|
int ret;
|
2012-05-04 16:09:17 +03:00
|
|
|
|
2015-10-05 05:39:25 +02:00
|
|
|
if (!(in_formats = ff_all_formats (AVMEDIA_TYPE_AUDIO)) ||
|
|
|
|
!(out_formats = ff_all_formats (AVMEDIA_TYPE_AUDIO)) ||
|
|
|
|
!(in_samplerates = ff_all_samplerates ( )) ||
|
|
|
|
!(out_samplerates = ff_all_samplerates ( )) ||
|
|
|
|
!(in_layouts = ff_all_channel_layouts ( )) ||
|
|
|
|
!(out_layouts = ff_all_channel_layouts ( )))
|
|
|
|
return AVERROR(ENOMEM);
|
2012-05-06 08:00:22 +03:00
|
|
|
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_formats_ref (in_formats, &inlink->out_formats )) < 0 ||
|
|
|
|
(ret = ff_formats_ref (out_formats, &outlink->in_formats )) < 0 ||
|
|
|
|
(ret = ff_formats_ref (in_samplerates, &inlink->out_samplerates )) < 0 ||
|
|
|
|
(ret = ff_formats_ref (out_samplerates, &outlink->in_samplerates )) < 0 ||
|
|
|
|
(ret = ff_channel_layouts_ref (in_layouts, &inlink->out_channel_layouts)) < 0 ||
|
|
|
|
(ret = ff_channel_layouts_ref (out_layouts, &outlink->in_channel_layouts)) < 0)
|
|
|
|
return ret;
|
2012-05-06 08:00:22 +03:00
|
|
|
|
2012-05-04 16:09:17 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
ResampleContext *s = ctx->priv;
|
|
|
|
char buf1[64], buf2[64];
|
|
|
|
int ret;
|
|
|
|
|
2015-07-16 19:26:36 +02:00
|
|
|
int64_t resampling_forced;
|
|
|
|
|
2012-05-04 16:09:17 +03:00
|
|
|
if (s->avr) {
|
|
|
|
avresample_close(s->avr);
|
|
|
|
avresample_free(&s->avr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inlink->channel_layout == outlink->channel_layout &&
|
|
|
|
inlink->sample_rate == outlink->sample_rate &&
|
2012-09-20 20:28:34 +03:00
|
|
|
(inlink->format == outlink->format ||
|
|
|
|
(av_get_channel_layout_nb_channels(inlink->channel_layout) == 1 &&
|
|
|
|
av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
|
|
|
|
av_get_planar_sample_fmt(inlink->format) ==
|
|
|
|
av_get_planar_sample_fmt(outlink->format))))
|
2012-05-04 16:09:17 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!(s->avr = avresample_alloc_context()))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-12-19 05:27:35 +03:00
|
|
|
if (s->options) {
|
2014-10-17 12:07:11 +03:00
|
|
|
int ret;
|
2012-12-19 05:27:35 +03:00
|
|
|
AVDictionaryEntry *e = NULL;
|
|
|
|
while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
|
|
|
|
|
2014-10-17 12:07:11 +03:00
|
|
|
ret = av_opt_set_dict(s->avr, &s->options);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-12-19 05:27:35 +03:00
|
|
|
}
|
|
|
|
|
2012-05-04 16:09:17 +03:00
|
|
|
av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
|
|
|
|
av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
|
|
|
|
av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
|
|
|
|
av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
|
|
|
|
av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
|
|
|
|
av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
|
|
|
|
|
|
|
|
if ((ret = avresample_open(s->avr)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2015-07-16 19:26:36 +02:00
|
|
|
av_opt_get_int(s->avr, "force_resampling", 0, &resampling_forced);
|
|
|
|
s->resampling = resampling_forced || (inlink->sample_rate != outlink->sample_rate);
|
|
|
|
|
|
|
|
if (s->resampling) {
|
|
|
|
outlink->time_base = (AVRational){ 1, outlink->sample_rate };
|
|
|
|
s->next_pts = AV_NOPTS_VALUE;
|
|
|
|
s->next_in_pts = AV_NOPTS_VALUE;
|
|
|
|
} else
|
|
|
|
outlink->time_base = inlink->time_base;
|
2012-05-04 16:09:17 +03:00
|
|
|
|
|
|
|
av_get_channel_layout_string(buf1, sizeof(buf1),
|
|
|
|
-1, inlink ->channel_layout);
|
|
|
|
av_get_channel_layout_string(buf2, sizeof(buf2),
|
|
|
|
-1, outlink->channel_layout);
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
2012-05-21 21:59:56 +03:00
|
|
|
"fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
|
2012-05-04 16:09:17 +03:00
|
|
|
av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
|
|
|
|
av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
ResampleContext *s = ctx->priv;
|
2012-07-04 17:46:17 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
s->got_output = 0;
|
|
|
|
while (ret >= 0 && !s->got_output)
|
|
|
|
ret = ff_request_frame(ctx->inputs[0]);
|
2012-05-04 16:09:17 +03:00
|
|
|
|
|
|
|
/* flush the lavr delay buffer */
|
|
|
|
if (ret == AVERROR_EOF && s->avr) {
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *frame;
|
2014-05-26 22:30:30 +03:00
|
|
|
int nb_samples = avresample_get_out_samples(s->avr, 0);
|
2012-05-04 16:09:17 +03:00
|
|
|
|
|
|
|
if (!nb_samples)
|
|
|
|
return ret;
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
frame = ff_get_audio_buffer(outlink, nb_samples);
|
|
|
|
if (!frame)
|
2012-05-04 16:09:17 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
ret = avresample_convert(s->avr, frame->extended_data,
|
|
|
|
frame->linesize[0], nb_samples,
|
2012-05-04 16:09:17 +03:00
|
|
|
NULL, 0, 0);
|
|
|
|
if (ret <= 0) {
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&frame);
|
2012-05-04 16:09:17 +03:00
|
|
|
return (ret == 0) ? AVERROR_EOF : ret;
|
|
|
|
}
|
|
|
|
|
2014-08-02 02:58:07 +03:00
|
|
|
frame->nb_samples = ret;
|
2012-11-28 10:41:07 +03:00
|
|
|
frame->pts = s->next_pts;
|
|
|
|
return ff_filter_frame(outlink, frame);
|
2012-05-04 16:09:17 +03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
2012-05-04 16:09:17 +03:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
ResampleContext *s = ctx->priv;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2012-07-02 21:13:40 +03:00
|
|
|
int ret;
|
2012-05-04 16:09:17 +03:00
|
|
|
|
|
|
|
if (s->avr) {
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *out;
|
2012-07-02 21:13:40 +03:00
|
|
|
int delay, nb_samples;
|
2012-05-04 16:09:17 +03:00
|
|
|
|
|
|
|
/* maximum possible samples lavr can output */
|
|
|
|
delay = avresample_get_delay(s->avr);
|
2014-05-26 22:30:30 +03:00
|
|
|
nb_samples = avresample_get_out_samples(s->avr, in->nb_samples);
|
2012-05-04 16:09:17 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
out = ff_get_audio_buffer(outlink, nb_samples);
|
|
|
|
if (!out) {
|
2012-07-02 21:13:40 +03:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
|
|
|
|
nb_samples, in->extended_data, in->linesize[0],
|
|
|
|
in->nb_samples);
|
2012-10-09 17:28:30 +03:00
|
|
|
if (ret <= 0) {
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&out);
|
2012-10-09 17:28:30 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2012-07-02 21:13:40 +03:00
|
|
|
}
|
2012-05-04 16:09:17 +03:00
|
|
|
|
|
|
|
av_assert0(!avresample_available(s->avr));
|
|
|
|
|
2015-07-16 19:26:36 +02:00
|
|
|
if (s->resampling && s->next_pts == AV_NOPTS_VALUE) {
|
2012-11-28 10:41:07 +03:00
|
|
|
if (in->pts == AV_NOPTS_VALUE) {
|
2012-05-04 16:09:17 +03:00
|
|
|
av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
|
|
|
|
"assuming 0.\n");
|
|
|
|
s->next_pts = 0;
|
|
|
|
} else
|
2012-11-28 10:41:07 +03:00
|
|
|
s->next_pts = av_rescale_q(in->pts, inlink->time_base,
|
2012-05-04 16:09:17 +03:00
|
|
|
outlink->time_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret > 0) {
|
2012-11-28 10:41:07 +03:00
|
|
|
out->nb_samples = ret;
|
2014-02-19 22:42:39 +03:00
|
|
|
|
|
|
|
ret = av_frame_copy_props(out, in);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&out);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-07-16 19:26:36 +02:00
|
|
|
if (s->resampling) {
|
|
|
|
out->sample_rate = outlink->sample_rate;
|
|
|
|
/* Only convert in->pts if there is a discontinuous jump.
|
|
|
|
This ensures that out->pts tracks the number of samples actually
|
|
|
|
output by the resampler in the absence of such a jump.
|
|
|
|
Otherwise, the rounding in av_rescale_q() and av_rescale()
|
|
|
|
causes off-by-1 errors. */
|
|
|
|
if (in->pts != AV_NOPTS_VALUE && in->pts != s->next_in_pts) {
|
|
|
|
out->pts = av_rescale_q(in->pts, inlink->time_base,
|
|
|
|
outlink->time_base) -
|
|
|
|
av_rescale(delay, outlink->sample_rate,
|
|
|
|
inlink->sample_rate);
|
|
|
|
} else
|
|
|
|
out->pts = s->next_pts;
|
|
|
|
|
|
|
|
s->next_pts = out->pts + out->nb_samples;
|
|
|
|
s->next_in_pts = in->pts + in->nb_samples;
|
2012-05-04 16:09:17 +03:00
|
|
|
} else
|
2015-07-16 19:26:36 +02:00
|
|
|
out->pts = in->pts;
|
2012-05-04 16:09:17 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
ret = ff_filter_frame(outlink, out);
|
2012-07-04 17:46:17 +03:00
|
|
|
s->got_output = 1;
|
2012-05-04 16:09:17 +03:00
|
|
|
}
|
2012-07-02 21:13:40 +03:00
|
|
|
|
|
|
|
fail:
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&in);
|
2012-07-04 17:46:17 +03:00
|
|
|
} else {
|
2012-11-28 10:41:07 +03:00
|
|
|
in->format = outlink->format;
|
|
|
|
ret = ff_filter_frame(outlink, in);
|
2012-07-04 17:46:17 +03:00
|
|
|
s->got_output = 1;
|
|
|
|
}
|
2012-07-02 21:13:40 +03:00
|
|
|
|
|
|
|
return ret;
|
2012-05-04 16:09:17 +03:00
|
|
|
}
|
|
|
|
|
2020-05-27 15:34:07 +02:00
|
|
|
#if FF_API_CHILD_CLASS_NEXT
|
2013-02-25 23:21:29 +03:00
|
|
|
static const AVClass *resample_child_class_next(const AVClass *prev)
|
|
|
|
{
|
|
|
|
return prev ? NULL : avresample_get_class();
|
|
|
|
}
|
2020-05-27 15:34:07 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static const AVClass *resample_child_class_iterate(void **iter)
|
|
|
|
{
|
|
|
|
const AVClass *c = *iter ? NULL : avresample_get_class();
|
|
|
|
*iter = (void*)(uintptr_t)c;
|
|
|
|
return c;
|
|
|
|
}
|
2013-02-25 23:21:29 +03:00
|
|
|
|
|
|
|
static void *resample_child_next(void *obj, void *prev)
|
|
|
|
{
|
|
|
|
ResampleContext *s = obj;
|
|
|
|
return prev ? NULL : s->avr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass resample_class = {
|
|
|
|
.class_name = "resample",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2020-05-27 15:34:07 +02:00
|
|
|
#if FF_API_CHILD_CLASS_NEXT
|
2013-02-25 23:21:29 +03:00
|
|
|
.child_class_next = resample_child_class_next,
|
2020-05-27 15:34:07 +02:00
|
|
|
#endif
|
|
|
|
.child_class_iterate = resample_child_class_iterate,
|
2013-02-25 23:21:29 +03:00
|
|
|
.child_next = resample_child_next,
|
|
|
|
};
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_af_resample_inputs[] = {
|
|
|
|
{
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 16:14:01 +03:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_resample_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
.request_frame = request_frame
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-28 09:44:24 +03:00
|
|
|
AVFilter ff_af_resample = {
|
2012-05-04 16:09:17 +03:00
|
|
|
.name = "resample",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
|
|
|
|
.priv_size = sizeof(ResampleContext),
|
2013-02-25 23:21:29 +03:00
|
|
|
.priv_class = &resample_class,
|
2013-09-07 15:13:50 +03:00
|
|
|
.init_dict = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.inputs = avfilter_af_resample_inputs,
|
|
|
|
.outputs = avfilter_af_resample_outputs,
|
2012-05-04 16:09:17 +03:00
|
|
|
};
|