2012-12-06 17:18:59 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Stefano Sabatini
|
|
|
|
* Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* audio volume filter
|
|
|
|
*/
|
|
|
|
|
2013-03-07 10:16:07 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-12-06 17:18:59 +03:00
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/eval.h"
|
2016-03-15 03:28:56 +02:00
|
|
|
#include "libavutil/ffmath.h"
|
2012-12-06 17:18:59 +03:00
|
|
|
#include "libavutil/float_dsp.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2012-12-06 17:18:59 +03:00
|
|
|
#include "libavutil/opt.h"
|
2014-02-19 23:01:37 +03:00
|
|
|
#include "libavutil/replaygain.h"
|
|
|
|
|
2012-12-06 17:18:59 +03:00
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
2024-08-06 08:50:21 +02:00
|
|
|
#include "filters.h"
|
2012-12-06 17:18:59 +03:00
|
|
|
#include "formats.h"
|
|
|
|
#include "af_volume.h"
|
|
|
|
|
2014-08-29 01:32:32 +03:00
|
|
|
static const char * const precision_str[] = {
|
2012-12-06 17:18:59 +03:00
|
|
|
"fixed", "float", "double"
|
|
|
|
};
|
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
static const char *const var_names[] = {
|
|
|
|
"n", ///< frame number (starting at zero)
|
|
|
|
"nb_channels", ///< number of channels
|
|
|
|
"nb_consumed_samples", ///< number of samples consumed by the filter
|
|
|
|
"nb_samples", ///< number of samples in the current frame
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
#if FF_API_FRAME_PKT
|
2013-02-23 02:17:17 +03:00
|
|
|
"pos", ///< position in the file of the frame
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
#endif
|
2013-02-23 02:17:17 +03:00
|
|
|
"pts", ///< frame presentation timestamp
|
|
|
|
"sample_rate", ///< sample rate
|
|
|
|
"startpts", ///< PTS at start of stream
|
|
|
|
"startt", ///< time at start of stream
|
|
|
|
"t", ///< time in the file of the frame
|
|
|
|
"tb", ///< timebase
|
|
|
|
"volume", ///< last set value
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-12-06 17:18:59 +03:00
|
|
|
#define OFFSET(x) offsetof(VolumeContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM
|
2012-12-08 13:58:52 +03:00
|
|
|
#define F AV_OPT_FLAG_FILTERING_PARAM
|
2020-01-11 03:28:33 +02:00
|
|
|
#define T AV_OPT_FLAG_RUNTIME_PARAM
|
2012-12-06 17:18:59 +03:00
|
|
|
|
2012-12-08 13:58:52 +03:00
|
|
|
static const AVOption volume_options[] = {
|
2013-02-23 02:17:17 +03:00
|
|
|
{ "volume", "set volume adjustment expression",
|
2020-01-11 03:28:33 +02:00
|
|
|
OFFSET(volume_expr), AV_OPT_TYPE_STRING, { .str = "1.0" }, .flags = A|F|T },
|
2012-12-08 13:54:43 +03:00
|
|
|
{ "precision", "select mathematical precision",
|
2024-02-11 16:41:05 +02:00
|
|
|
OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A|F, .unit = "precision" },
|
|
|
|
{ "fixed", "select 8-bit fixed-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A|F, .unit = "precision" },
|
|
|
|
{ "float", "select 32-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A|F, .unit = "precision" },
|
|
|
|
{ "double", "select 64-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A|F, .unit = "precision" },
|
|
|
|
{ "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_ONCE}, 0, EVAL_MODE_NB-1, .flags = A|F, .unit = "eval" },
|
2013-02-23 02:17:17 +03:00
|
|
|
{ "once", "eval volume expression once", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_ONCE}, .flags = A|F, .unit = "eval" },
|
|
|
|
{ "frame", "eval volume expression per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = A|F, .unit = "eval" },
|
2014-02-19 23:01:37 +03:00
|
|
|
{ "replaygain", "Apply replaygain side data when present",
|
2024-02-11 16:41:05 +02:00
|
|
|
OFFSET(replaygain), AV_OPT_TYPE_INT, { .i64 = REPLAYGAIN_DROP }, REPLAYGAIN_DROP, REPLAYGAIN_ALBUM, A|F, .unit = "replaygain" },
|
|
|
|
{ "drop", "replaygain side data is dropped", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_DROP }, 0, 0, A|F, .unit = "replaygain" },
|
|
|
|
{ "ignore", "replaygain side data is ignored", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_IGNORE }, 0, 0, A|F, .unit = "replaygain" },
|
|
|
|
{ "track", "track gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_TRACK }, 0, 0, A|F, .unit = "replaygain" },
|
|
|
|
{ "album", "album gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_ALBUM }, 0, 0, A|F, .unit = "replaygain" },
|
2014-04-04 19:42:09 +03:00
|
|
|
{ "replaygain_preamp", "Apply replaygain pre-amplification",
|
2015-09-08 23:20:02 +02:00
|
|
|
OFFSET(replaygain_preamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, -15.0, 15.0, A|F },
|
2014-04-06 17:25:08 +03:00
|
|
|
{ "replaygain_noclip", "Apply replaygain clipping prevention",
|
2015-09-08 23:20:02 +02:00
|
|
|
OFFSET(replaygain_noclip), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, A|F },
|
2015-09-08 23:21:15 +02:00
|
|
|
{ NULL }
|
2012-12-06 17:18:59 +03:00
|
|
|
};
|
|
|
|
|
2012-12-08 13:58:52 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(volume);
|
2012-12-06 17:18:59 +03:00
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
static int set_expr(AVExpr **pexpr, const char *expr, void *log_ctx)
|
2012-12-06 17:18:59 +03:00
|
|
|
{
|
2013-02-23 02:17:17 +03:00
|
|
|
int ret;
|
|
|
|
AVExpr *old = NULL;
|
|
|
|
|
|
|
|
if (*pexpr)
|
|
|
|
old = *pexpr;
|
|
|
|
ret = av_expr_parse(pexpr, expr, var_names,
|
|
|
|
NULL, NULL, NULL, NULL, 0, log_ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
|
|
|
"Error when evaluating the volume expression '%s'\n", expr);
|
|
|
|
*pexpr = old;
|
|
|
|
return ret;
|
2012-12-06 17:18:59 +03:00
|
|
|
}
|
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
av_expr_free(old);
|
2013-03-16 22:54:57 +03:00
|
|
|
return 0;
|
2012-12-06 17:18:59 +03:00
|
|
|
}
|
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
VolumeContext *vol = ctx->priv;
|
2014-11-18 13:24:41 +02:00
|
|
|
|
|
|
|
vol->fdsp = avpriv_float_dsp_alloc(0);
|
|
|
|
if (!vol->fdsp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
return set_expr(&vol->volume_pexpr, vol->volume_expr, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
VolumeContext *vol = ctx->priv;
|
|
|
|
av_expr_free(vol->volume_pexpr);
|
2014-11-18 13:24:41 +02:00
|
|
|
av_freep(&vol->fdsp);
|
2013-02-23 02:17:17 +03:00
|
|
|
}
|
|
|
|
|
2012-12-06 17:18:59 +03:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
VolumeContext *vol = ctx->priv;
|
|
|
|
static const enum AVSampleFormat sample_fmts[][7] = {
|
2013-03-29 14:34:15 +03:00
|
|
|
[PRECISION_FIXED] = {
|
2012-12-06 17:18:59 +03:00
|
|
|
AV_SAMPLE_FMT_U8,
|
|
|
|
AV_SAMPLE_FMT_U8P,
|
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
AV_SAMPLE_FMT_S16P,
|
|
|
|
AV_SAMPLE_FMT_S32,
|
|
|
|
AV_SAMPLE_FMT_S32P,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
},
|
2013-03-29 14:34:15 +03:00
|
|
|
[PRECISION_FLOAT] = {
|
2012-12-06 17:18:59 +03:00
|
|
|
AV_SAMPLE_FMT_FLT,
|
|
|
|
AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
},
|
2013-03-29 14:34:15 +03:00
|
|
|
[PRECISION_DOUBLE] = {
|
2012-12-06 17:18:59 +03:00
|
|
|
AV_SAMPLE_FMT_DBL,
|
|
|
|
AV_SAMPLE_FMT_DBLP,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
}
|
|
|
|
};
|
2021-08-10 01:25:31 +02:00
|
|
|
int ret = ff_set_common_all_channel_counts(ctx);
|
2015-04-03 19:55:18 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-12-06 17:18:59 +03:00
|
|
|
|
2021-08-10 01:25:31 +02:00
|
|
|
ret = ff_set_common_formats_from_list(ctx, sample_fmts[vol->precision]);
|
2015-04-03 19:55:18 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-12-06 17:18:59 +03:00
|
|
|
|
2021-08-10 01:25:31 +02:00
|
|
|
return ff_set_common_all_samplerates(ctx);
|
2012-12-06 17:18:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
|
|
|
|
int nb_samples, int volume)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nb_samples; i++)
|
|
|
|
dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
|
|
|
|
int nb_samples, int volume)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nb_samples; i++)
|
|
|
|
dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
|
|
|
|
int nb_samples, int volume)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int16_t *smp_dst = (int16_t *)dst;
|
|
|
|
const int16_t *smp_src = (const int16_t *)src;
|
|
|
|
for (i = 0; i < nb_samples; i++)
|
|
|
|
smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
|
|
|
|
int nb_samples, int volume)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int16_t *smp_dst = (int16_t *)dst;
|
|
|
|
const int16_t *smp_src = (const int16_t *)src;
|
|
|
|
for (i = 0; i < nb_samples; i++)
|
|
|
|
smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
|
|
|
|
int nb_samples, int volume)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int32_t *smp_dst = (int32_t *)dst;
|
|
|
|
const int32_t *smp_src = (const int32_t *)src;
|
|
|
|
for (i = 0; i < nb_samples; i++)
|
|
|
|
smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
|
|
|
|
}
|
|
|
|
|
2013-04-30 19:30:02 +03:00
|
|
|
static av_cold void volume_init(VolumeContext *vol)
|
2012-12-06 17:18:59 +03:00
|
|
|
{
|
|
|
|
vol->samples_align = 1;
|
|
|
|
|
|
|
|
switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
|
|
|
|
case AV_SAMPLE_FMT_U8:
|
|
|
|
if (vol->volume_i < 0x1000000)
|
|
|
|
vol->scale_samples = scale_samples_u8_small;
|
|
|
|
else
|
|
|
|
vol->scale_samples = scale_samples_u8;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S16:
|
|
|
|
if (vol->volume_i < 0x10000)
|
|
|
|
vol->scale_samples = scale_samples_s16_small;
|
|
|
|
else
|
|
|
|
vol->scale_samples = scale_samples_s16;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S32:
|
|
|
|
vol->scale_samples = scale_samples_s32;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLT:
|
|
|
|
vol->samples_align = 4;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_DBL:
|
|
|
|
vol->samples_align = 8;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-23 21:49:26 +03:00
|
|
|
|
2022-06-12 05:51:12 +02:00
|
|
|
#if ARCH_X86
|
|
|
|
ff_volume_init_x86(vol);
|
|
|
|
#endif
|
2012-12-06 17:18:59 +03:00
|
|
|
}
|
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
static int set_volume(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
VolumeContext *vol = ctx->priv;
|
|
|
|
|
|
|
|
vol->volume = av_expr_eval(vol->volume_pexpr, vol->var_values, NULL);
|
|
|
|
if (isnan(vol->volume)) {
|
|
|
|
if (vol->eval_mode == EVAL_MODE_ONCE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid value NaN for volume\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "Invalid value NaN for volume, setting to 0\n");
|
|
|
|
vol->volume = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vol->var_values[VAR_VOLUME] = vol->volume;
|
|
|
|
|
2013-12-25 19:55:16 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "n:%f t:%f pts:%f precision:%s ",
|
|
|
|
vol->var_values[VAR_N], vol->var_values[VAR_T], vol->var_values[VAR_PTS],
|
|
|
|
precision_str[vol->precision]);
|
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
if (vol->precision == PRECISION_FIXED) {
|
|
|
|
vol->volume_i = (int)(vol->volume * 256 + 0.5);
|
|
|
|
vol->volume = vol->volume_i / 256.0;
|
2013-12-25 19:55:16 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "volume_i:%d/255 ", vol->volume_i);
|
2013-02-23 02:17:17 +03:00
|
|
|
}
|
2013-12-25 19:55:16 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "volume:%f volume_dB:%f\n",
|
2015-10-29 06:11:44 +02:00
|
|
|
vol->volume, 20.0*log10(vol->volume));
|
2013-02-23 02:17:17 +03:00
|
|
|
|
|
|
|
volume_init(vol);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-06 17:18:59 +03:00
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
VolumeContext *vol = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
|
|
|
|
vol->sample_fmt = inlink->format;
|
2021-08-31 16:03:14 +02:00
|
|
|
vol->channels = inlink->ch_layout.nb_channels;
|
2012-12-06 17:18:59 +03:00
|
|
|
vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
|
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
vol->var_values[VAR_N] =
|
|
|
|
vol->var_values[VAR_NB_CONSUMED_SAMPLES] =
|
|
|
|
vol->var_values[VAR_NB_SAMPLES] =
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
#if FF_API_FRAME_PKT
|
2013-02-23 02:17:17 +03:00
|
|
|
vol->var_values[VAR_POS] =
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
#endif
|
2013-02-23 02:17:17 +03:00
|
|
|
vol->var_values[VAR_PTS] =
|
|
|
|
vol->var_values[VAR_STARTPTS] =
|
|
|
|
vol->var_values[VAR_STARTT] =
|
|
|
|
vol->var_values[VAR_T] =
|
|
|
|
vol->var_values[VAR_VOLUME] = NAN;
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
vol->var_values[VAR_NB_CHANNELS] = inlink->ch_layout.nb_channels;
|
2013-02-23 02:17:17 +03:00
|
|
|
vol->var_values[VAR_TB] = av_q2d(inlink->time_base);
|
|
|
|
vol->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
|
|
|
|
|
|
|
|
av_log(inlink->src, AV_LOG_VERBOSE, "tb:%f sample_rate:%f nb_channels:%f\n",
|
|
|
|
vol->var_values[VAR_TB],
|
|
|
|
vol->var_values[VAR_SAMPLE_RATE],
|
|
|
|
vol->var_values[VAR_NB_CHANNELS]);
|
|
|
|
|
|
|
|
return set_volume(ctx);
|
2012-12-06 17:18:59 +03:00
|
|
|
}
|
|
|
|
|
2013-12-23 20:24:57 +03:00
|
|
|
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
|
|
|
char *res, int res_len, int flags)
|
|
|
|
{
|
|
|
|
VolumeContext *vol = ctx->priv;
|
|
|
|
int ret = AVERROR(ENOSYS);
|
|
|
|
|
|
|
|
if (!strcmp(cmd, "volume")) {
|
|
|
|
if ((ret = set_expr(&vol->volume_pexpr, args, ctx)) < 0)
|
|
|
|
return ret;
|
|
|
|
if (vol->eval_mode == EVAL_MODE_ONCE)
|
|
|
|
set_volume(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
|
2012-12-06 17:18:59 +03:00
|
|
|
{
|
2024-08-06 08:50:21 +02:00
|
|
|
FilterLink *inl = ff_filter_link(inlink);
|
2013-02-23 02:17:17 +03:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2012-12-06 17:18:59 +03:00
|
|
|
VolumeContext *vol = inlink->dst->priv;
|
|
|
|
AVFilterLink *outlink = inlink->dst->outputs[0];
|
2012-11-28 10:41:07 +03:00
|
|
|
int nb_samples = buf->nb_samples;
|
|
|
|
AVFrame *out_buf;
|
2014-02-19 23:01:37 +03:00
|
|
|
AVFrameSideData *sd = av_frame_get_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
|
2014-02-19 22:55:27 +03:00
|
|
|
int ret;
|
2013-02-23 02:17:17 +03:00
|
|
|
|
2014-02-19 23:01:37 +03:00
|
|
|
if (sd && vol->replaygain != REPLAYGAIN_IGNORE) {
|
|
|
|
if (vol->replaygain != REPLAYGAIN_DROP) {
|
|
|
|
AVReplayGain *replaygain = (AVReplayGain*)sd->data;
|
2014-04-06 17:25:08 +03:00
|
|
|
int32_t gain = 100000;
|
|
|
|
uint32_t peak = 100000;
|
|
|
|
float g, p;
|
2014-02-19 23:01:37 +03:00
|
|
|
|
|
|
|
if (vol->replaygain == REPLAYGAIN_TRACK &&
|
2014-04-06 17:25:08 +03:00
|
|
|
replaygain->track_gain != INT32_MIN) {
|
2014-02-19 23:01:37 +03:00
|
|
|
gain = replaygain->track_gain;
|
2014-04-06 17:25:08 +03:00
|
|
|
|
|
|
|
if (replaygain->track_peak != 0)
|
|
|
|
peak = replaygain->track_peak;
|
|
|
|
} else if (replaygain->album_gain != INT32_MIN) {
|
2014-02-19 23:01:37 +03:00
|
|
|
gain = replaygain->album_gain;
|
2014-04-06 17:25:08 +03:00
|
|
|
|
|
|
|
if (replaygain->album_peak != 0)
|
|
|
|
peak = replaygain->album_peak;
|
|
|
|
} else {
|
2014-02-19 23:01:37 +03:00
|
|
|
av_log(inlink->dst, AV_LOG_WARNING, "Both ReplayGain gain "
|
|
|
|
"values are unknown.\n");
|
|
|
|
}
|
|
|
|
g = gain / 100000.0f;
|
2014-04-06 17:25:08 +03:00
|
|
|
p = peak / 100000.0f;
|
2014-02-19 23:01:37 +03:00
|
|
|
|
|
|
|
av_log(inlink->dst, AV_LOG_VERBOSE,
|
|
|
|
"Using gain %f dB from replaygain side data.\n", g);
|
|
|
|
|
2015-12-23 20:05:34 +02:00
|
|
|
vol->volume = ff_exp10((g + vol->replaygain_preamp) / 20);
|
2014-04-06 17:25:08 +03:00
|
|
|
if (vol->replaygain_noclip)
|
|
|
|
vol->volume = FFMIN(vol->volume, 1.0 / p);
|
2014-02-19 23:01:37 +03:00
|
|
|
vol->volume_i = (int)(vol->volume * 256 + 0.5);
|
|
|
|
|
|
|
|
volume_init(vol);
|
|
|
|
}
|
|
|
|
av_frame_remove_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
|
|
|
|
}
|
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
if (isnan(vol->var_values[VAR_STARTPTS])) {
|
|
|
|
vol->var_values[VAR_STARTPTS] = TS2D(buf->pts);
|
|
|
|
vol->var_values[VAR_STARTT ] = TS2T(buf->pts, inlink->time_base);
|
|
|
|
}
|
|
|
|
vol->var_values[VAR_PTS] = TS2D(buf->pts);
|
|
|
|
vol->var_values[VAR_T ] = TS2T(buf->pts, inlink->time_base);
|
2024-08-06 08:50:21 +02:00
|
|
|
vol->var_values[VAR_N ] = inl->frame_count_out;
|
2012-12-06 17:18:59 +03:00
|
|
|
|
lavu/frame: deprecate AVFrame.pkt_{pos,size}
These fields are supposed to store information about the packet the
frame was decoded from, specifically the byte offset it was stored at
and its size.
However,
- the fields are highly ad-hoc - there is no strong reason why
specifically those (and not any other) packet properties should have a
dedicated field in AVFrame; unlike e.g. the timestamps, there is no
fundamental link between coded packet offset/size and decoded frames
- they only make sense for frames produced by decoding demuxed packets,
and even then it is not always the case that the encoded data was
stored in the file as a contiguous sequence of bytes (in order for pos
to be well-defined)
- pkt_pos was added without much explanation, apparently to allow
passthrough of this information through lavfi in order to handle byte
seeking in ffplay. That is now implemented using arbitrary user data
passthrough in AVFrame.opaque_ref.
- several filters use pkt_pos as a variable available to user-supplied
expressions, but there seems to be no established motivation for using them.
- pkt_size was added for use in ffprobe, but that too is now handled
without using this field. Additonally, the values of this field
produced by libavcodec are flawed, as described in the previous
ffprobe conversion commit.
In summary - these fields are ill-defined and insufficiently motivated,
so deprecate them.
2023-03-10 11:48:34 +02:00
|
|
|
#if FF_API_FRAME_PKT
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
{
|
|
|
|
int64_t pos;
|
|
|
|
pos = buf->pkt_pos;
|
|
|
|
vol->var_values[VAR_POS] = pos == -1 ? NAN : pos;
|
|
|
|
}
|
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2013-02-23 02:17:17 +03:00
|
|
|
if (vol->eval_mode == EVAL_MODE_FRAME)
|
|
|
|
set_volume(ctx);
|
|
|
|
|
|
|
|
if (vol->volume == 1.0 || vol->volume_i == 256) {
|
|
|
|
out_buf = buf;
|
|
|
|
goto end;
|
|
|
|
}
|
2012-12-06 17:18:59 +03:00
|
|
|
|
|
|
|
/* do volume scaling in-place if input buffer is writable */
|
2015-03-01 00:15:39 +02:00
|
|
|
if (av_frame_is_writable(buf)
|
|
|
|
&& (vol->precision != PRECISION_FIXED || vol->volume_i > 0)) {
|
2012-12-06 17:18:59 +03:00
|
|
|
out_buf = buf;
|
|
|
|
} else {
|
2018-01-03 23:47:40 +02:00
|
|
|
out_buf = ff_get_audio_buffer(outlink, nb_samples);
|
2017-06-24 18:36:46 +02:00
|
|
|
if (!out_buf) {
|
|
|
|
av_frame_free(&buf);
|
2012-12-06 17:18:59 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2017-06-24 18:36:46 +02:00
|
|
|
}
|
2014-02-19 22:55:27 +03:00
|
|
|
ret = av_frame_copy_props(out_buf, buf);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&out_buf);
|
|
|
|
av_frame_free(&buf);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-12-06 17:18:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
|
|
|
|
int p, plane_samples;
|
|
|
|
|
|
|
|
if (av_sample_fmt_is_planar(buf->format))
|
|
|
|
plane_samples = FFALIGN(nb_samples, vol->samples_align);
|
|
|
|
else
|
|
|
|
plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
|
|
|
|
|
|
|
|
if (vol->precision == PRECISION_FIXED) {
|
|
|
|
for (p = 0; p < vol->planes; p++) {
|
|
|
|
vol->scale_samples(out_buf->extended_data[p],
|
|
|
|
buf->extended_data[p], plane_samples,
|
|
|
|
vol->volume_i);
|
|
|
|
}
|
|
|
|
} else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
|
|
|
|
for (p = 0; p < vol->planes; p++) {
|
2014-11-18 13:24:41 +02:00
|
|
|
vol->fdsp->vector_fmul_scalar((float *)out_buf->extended_data[p],
|
2012-12-06 17:18:59 +03:00
|
|
|
(const float *)buf->extended_data[p],
|
|
|
|
vol->volume, plane_samples);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (p = 0; p < vol->planes; p++) {
|
2014-11-18 13:24:41 +02:00
|
|
|
vol->fdsp->vector_dmul_scalar((double *)out_buf->extended_data[p],
|
2012-12-06 17:18:59 +03:00
|
|
|
(const double *)buf->extended_data[p],
|
|
|
|
vol->volume, plane_samples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf != out_buf)
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&buf);
|
2012-12-06 17:18:59 +03:00
|
|
|
|
2013-02-23 02:17:17 +03:00
|
|
|
end:
|
2013-12-25 21:13:23 +03:00
|
|
|
vol->var_values[VAR_NB_CONSUMED_SAMPLES] += out_buf->nb_samples;
|
2012-12-06 17:18:59 +03:00
|
|
|
return ff_filter_frame(outlink, out_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_volume_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_volume_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_volume = {
|
2012-12-08 14:07:03 +03:00
|
|
|
.name = "volume",
|
2012-12-06 17:18:59 +03:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Change input volume."),
|
|
|
|
.priv_size = sizeof(VolumeContext),
|
2013-02-25 23:21:29 +03:00
|
|
|
.priv_class = &volume_class,
|
2012-12-06 17:18:59 +03:00
|
|
|
.init = init,
|
2013-02-23 02:17:17 +03:00
|
|
|
.uninit = uninit,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(avfilter_af_volume_inputs),
|
|
|
|
FILTER_OUTPUTS(avfilter_af_volume_outputs),
|
2021-09-27 12:07:35 +02:00
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2013-05-09 02:04:41 +03:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
|
2013-12-23 20:24:57 +03:00
|
|
|
.process_command = process_command,
|
2012-12-06 17:18:59 +03:00
|
|
|
};
|