You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

497 lines
17KB

  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * audio volume filter
  24. */
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/eval.h"
  28. #include "libavutil/ffmath.h"
  29. #include "libavutil/float_dsp.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/replaygain.h"
  33. #include "audio.h"
  34. #include "avfilter.h"
  35. #include "formats.h"
  36. #include "internal.h"
  37. #include "af_volume.h"
  38. static const char * const precision_str[] = {
  39. "fixed", "float", "double"
  40. };
  41. static const char *const var_names[] = {
  42. "n", ///< frame number (starting at zero)
  43. "nb_channels", ///< number of channels
  44. "nb_consumed_samples", ///< number of samples consumed by the filter
  45. "nb_samples", ///< number of samples in the current frame
  46. "pos", ///< position in the file of the frame
  47. "pts", ///< frame presentation timestamp
  48. "sample_rate", ///< sample rate
  49. "startpts", ///< PTS at start of stream
  50. "startt", ///< time at start of stream
  51. "t", ///< time in the file of the frame
  52. "tb", ///< timebase
  53. "volume", ///< last set value
  54. NULL
  55. };
  56. #define OFFSET(x) offsetof(VolumeContext, x)
  57. #define A AV_OPT_FLAG_AUDIO_PARAM
  58. #define F AV_OPT_FLAG_FILTERING_PARAM
  59. #define T AV_OPT_FLAG_RUNTIME_PARAM
  60. static const AVOption volume_options[] = {
  61. { "volume", "set volume adjustment expression",
  62. OFFSET(volume_expr), AV_OPT_TYPE_STRING, { .str = "1.0" }, .flags = A|F|T },
  63. { "precision", "select mathematical precision",
  64. OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A|F, "precision" },
  65. { "fixed", "select 8-bit fixed-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A|F, "precision" },
  66. { "float", "select 32-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A|F, "precision" },
  67. { "double", "select 64-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A|F, "precision" },
  68. { "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, "eval" },
  69. { "once", "eval volume expression once", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_ONCE}, .flags = A|F, .unit = "eval" },
  70. { "frame", "eval volume expression per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = A|F, .unit = "eval" },
  71. { "replaygain", "Apply replaygain side data when present",
  72. OFFSET(replaygain), AV_OPT_TYPE_INT, { .i64 = REPLAYGAIN_DROP }, REPLAYGAIN_DROP, REPLAYGAIN_ALBUM, A|F, "replaygain" },
  73. { "drop", "replaygain side data is dropped", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_DROP }, 0, 0, A|F, "replaygain" },
  74. { "ignore", "replaygain side data is ignored", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_IGNORE }, 0, 0, A|F, "replaygain" },
  75. { "track", "track gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_TRACK }, 0, 0, A|F, "replaygain" },
  76. { "album", "album gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_ALBUM }, 0, 0, A|F, "replaygain" },
  77. { "replaygain_preamp", "Apply replaygain pre-amplification",
  78. OFFSET(replaygain_preamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, -15.0, 15.0, A|F },
  79. { "replaygain_noclip", "Apply replaygain clipping prevention",
  80. OFFSET(replaygain_noclip), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, A|F },
  81. { NULL }
  82. };
  83. AVFILTER_DEFINE_CLASS(volume);
  84. static int set_expr(AVExpr **pexpr, const char *expr, void *log_ctx)
  85. {
  86. int ret;
  87. AVExpr *old = NULL;
  88. if (*pexpr)
  89. old = *pexpr;
  90. ret = av_expr_parse(pexpr, expr, var_names,
  91. NULL, NULL, NULL, NULL, 0, log_ctx);
  92. if (ret < 0) {
  93. av_log(log_ctx, AV_LOG_ERROR,
  94. "Error when evaluating the volume expression '%s'\n", expr);
  95. *pexpr = old;
  96. return ret;
  97. }
  98. av_expr_free(old);
  99. return 0;
  100. }
  101. static av_cold int init(AVFilterContext *ctx)
  102. {
  103. VolumeContext *vol = ctx->priv;
  104. vol->fdsp = avpriv_float_dsp_alloc(0);
  105. if (!vol->fdsp)
  106. return AVERROR(ENOMEM);
  107. return set_expr(&vol->volume_pexpr, vol->volume_expr, ctx);
  108. }
  109. static av_cold void uninit(AVFilterContext *ctx)
  110. {
  111. VolumeContext *vol = ctx->priv;
  112. av_expr_free(vol->volume_pexpr);
  113. av_opt_free(vol);
  114. av_freep(&vol->fdsp);
  115. }
  116. static int query_formats(AVFilterContext *ctx)
  117. {
  118. VolumeContext *vol = ctx->priv;
  119. AVFilterFormats *formats = NULL;
  120. AVFilterChannelLayouts *layouts;
  121. static const enum AVSampleFormat sample_fmts[][7] = {
  122. [PRECISION_FIXED] = {
  123. AV_SAMPLE_FMT_U8,
  124. AV_SAMPLE_FMT_U8P,
  125. AV_SAMPLE_FMT_S16,
  126. AV_SAMPLE_FMT_S16P,
  127. AV_SAMPLE_FMT_S32,
  128. AV_SAMPLE_FMT_S32P,
  129. AV_SAMPLE_FMT_NONE
  130. },
  131. [PRECISION_FLOAT] = {
  132. AV_SAMPLE_FMT_FLT,
  133. AV_SAMPLE_FMT_FLTP,
  134. AV_SAMPLE_FMT_NONE
  135. },
  136. [PRECISION_DOUBLE] = {
  137. AV_SAMPLE_FMT_DBL,
  138. AV_SAMPLE_FMT_DBLP,
  139. AV_SAMPLE_FMT_NONE
  140. }
  141. };
  142. int ret;
  143. layouts = ff_all_channel_counts();
  144. if (!layouts)
  145. return AVERROR(ENOMEM);
  146. ret = ff_set_common_channel_layouts(ctx, layouts);
  147. if (ret < 0)
  148. return ret;
  149. formats = ff_make_format_list(sample_fmts[vol->precision]);
  150. if (!formats)
  151. return AVERROR(ENOMEM);
  152. ret = ff_set_common_formats(ctx, formats);
  153. if (ret < 0)
  154. return ret;
  155. formats = ff_all_samplerates();
  156. if (!formats)
  157. return AVERROR(ENOMEM);
  158. return ff_set_common_samplerates(ctx, formats);
  159. }
  160. static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
  161. int nb_samples, int volume)
  162. {
  163. int i;
  164. for (i = 0; i < nb_samples; i++)
  165. dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
  166. }
  167. static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
  168. int nb_samples, int volume)
  169. {
  170. int i;
  171. for (i = 0; i < nb_samples; i++)
  172. dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
  173. }
  174. static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
  175. int nb_samples, int volume)
  176. {
  177. int i;
  178. int16_t *smp_dst = (int16_t *)dst;
  179. const int16_t *smp_src = (const int16_t *)src;
  180. for (i = 0; i < nb_samples; i++)
  181. smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
  182. }
  183. static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
  184. int nb_samples, int volume)
  185. {
  186. int i;
  187. int16_t *smp_dst = (int16_t *)dst;
  188. const int16_t *smp_src = (const int16_t *)src;
  189. for (i = 0; i < nb_samples; i++)
  190. smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
  191. }
  192. static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
  193. int nb_samples, int volume)
  194. {
  195. int i;
  196. int32_t *smp_dst = (int32_t *)dst;
  197. const int32_t *smp_src = (const int32_t *)src;
  198. for (i = 0; i < nb_samples; i++)
  199. smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
  200. }
  201. static av_cold void volume_init(VolumeContext *vol)
  202. {
  203. vol->samples_align = 1;
  204. switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
  205. case AV_SAMPLE_FMT_U8:
  206. if (vol->volume_i < 0x1000000)
  207. vol->scale_samples = scale_samples_u8_small;
  208. else
  209. vol->scale_samples = scale_samples_u8;
  210. break;
  211. case AV_SAMPLE_FMT_S16:
  212. if (vol->volume_i < 0x10000)
  213. vol->scale_samples = scale_samples_s16_small;
  214. else
  215. vol->scale_samples = scale_samples_s16;
  216. break;
  217. case AV_SAMPLE_FMT_S32:
  218. vol->scale_samples = scale_samples_s32;
  219. break;
  220. case AV_SAMPLE_FMT_FLT:
  221. vol->samples_align = 4;
  222. break;
  223. case AV_SAMPLE_FMT_DBL:
  224. vol->samples_align = 8;
  225. break;
  226. }
  227. if (ARCH_X86)
  228. ff_volume_init_x86(vol);
  229. }
  230. static int set_volume(AVFilterContext *ctx)
  231. {
  232. VolumeContext *vol = ctx->priv;
  233. vol->volume = av_expr_eval(vol->volume_pexpr, vol->var_values, NULL);
  234. if (isnan(vol->volume)) {
  235. if (vol->eval_mode == EVAL_MODE_ONCE) {
  236. av_log(ctx, AV_LOG_ERROR, "Invalid value NaN for volume\n");
  237. return AVERROR(EINVAL);
  238. } else {
  239. av_log(ctx, AV_LOG_WARNING, "Invalid value NaN for volume, setting to 0\n");
  240. vol->volume = 0;
  241. }
  242. }
  243. vol->var_values[VAR_VOLUME] = vol->volume;
  244. av_log(ctx, AV_LOG_VERBOSE, "n:%f t:%f pts:%f precision:%s ",
  245. vol->var_values[VAR_N], vol->var_values[VAR_T], vol->var_values[VAR_PTS],
  246. precision_str[vol->precision]);
  247. if (vol->precision == PRECISION_FIXED) {
  248. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  249. vol->volume = vol->volume_i / 256.0;
  250. av_log(ctx, AV_LOG_VERBOSE, "volume_i:%d/255 ", vol->volume_i);
  251. }
  252. av_log(ctx, AV_LOG_VERBOSE, "volume:%f volume_dB:%f\n",
  253. vol->volume, 20.0*log10(vol->volume));
  254. volume_init(vol);
  255. return 0;
  256. }
  257. static int config_output(AVFilterLink *outlink)
  258. {
  259. AVFilterContext *ctx = outlink->src;
  260. VolumeContext *vol = ctx->priv;
  261. AVFilterLink *inlink = ctx->inputs[0];
  262. vol->sample_fmt = inlink->format;
  263. vol->channels = inlink->channels;
  264. vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
  265. vol->var_values[VAR_N] =
  266. vol->var_values[VAR_NB_CONSUMED_SAMPLES] =
  267. vol->var_values[VAR_NB_SAMPLES] =
  268. vol->var_values[VAR_POS] =
  269. vol->var_values[VAR_PTS] =
  270. vol->var_values[VAR_STARTPTS] =
  271. vol->var_values[VAR_STARTT] =
  272. vol->var_values[VAR_T] =
  273. vol->var_values[VAR_VOLUME] = NAN;
  274. vol->var_values[VAR_NB_CHANNELS] = inlink->channels;
  275. vol->var_values[VAR_TB] = av_q2d(inlink->time_base);
  276. vol->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
  277. av_log(inlink->src, AV_LOG_VERBOSE, "tb:%f sample_rate:%f nb_channels:%f\n",
  278. vol->var_values[VAR_TB],
  279. vol->var_values[VAR_SAMPLE_RATE],
  280. vol->var_values[VAR_NB_CHANNELS]);
  281. return set_volume(ctx);
  282. }
  283. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  284. char *res, int res_len, int flags)
  285. {
  286. VolumeContext *vol = ctx->priv;
  287. int ret = AVERROR(ENOSYS);
  288. if (!strcmp(cmd, "volume")) {
  289. if ((ret = set_expr(&vol->volume_pexpr, args, ctx)) < 0)
  290. return ret;
  291. if (vol->eval_mode == EVAL_MODE_ONCE)
  292. set_volume(ctx);
  293. }
  294. return ret;
  295. }
  296. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  297. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  298. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  299. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  300. {
  301. AVFilterContext *ctx = inlink->dst;
  302. VolumeContext *vol = inlink->dst->priv;
  303. AVFilterLink *outlink = inlink->dst->outputs[0];
  304. int nb_samples = buf->nb_samples;
  305. AVFrame *out_buf;
  306. int64_t pos;
  307. AVFrameSideData *sd = av_frame_get_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
  308. int ret;
  309. if (sd && vol->replaygain != REPLAYGAIN_IGNORE) {
  310. if (vol->replaygain != REPLAYGAIN_DROP) {
  311. AVReplayGain *replaygain = (AVReplayGain*)sd->data;
  312. int32_t gain = 100000;
  313. uint32_t peak = 100000;
  314. float g, p;
  315. if (vol->replaygain == REPLAYGAIN_TRACK &&
  316. replaygain->track_gain != INT32_MIN) {
  317. gain = replaygain->track_gain;
  318. if (replaygain->track_peak != 0)
  319. peak = replaygain->track_peak;
  320. } else if (replaygain->album_gain != INT32_MIN) {
  321. gain = replaygain->album_gain;
  322. if (replaygain->album_peak != 0)
  323. peak = replaygain->album_peak;
  324. } else {
  325. av_log(inlink->dst, AV_LOG_WARNING, "Both ReplayGain gain "
  326. "values are unknown.\n");
  327. }
  328. g = gain / 100000.0f;
  329. p = peak / 100000.0f;
  330. av_log(inlink->dst, AV_LOG_VERBOSE,
  331. "Using gain %f dB from replaygain side data.\n", g);
  332. vol->volume = ff_exp10((g + vol->replaygain_preamp) / 20);
  333. if (vol->replaygain_noclip)
  334. vol->volume = FFMIN(vol->volume, 1.0 / p);
  335. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  336. volume_init(vol);
  337. }
  338. av_frame_remove_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
  339. }
  340. if (isnan(vol->var_values[VAR_STARTPTS])) {
  341. vol->var_values[VAR_STARTPTS] = TS2D(buf->pts);
  342. vol->var_values[VAR_STARTT ] = TS2T(buf->pts, inlink->time_base);
  343. }
  344. vol->var_values[VAR_PTS] = TS2D(buf->pts);
  345. vol->var_values[VAR_T ] = TS2T(buf->pts, inlink->time_base);
  346. vol->var_values[VAR_N ] = inlink->frame_count_out;
  347. pos = buf->pkt_pos;
  348. vol->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  349. if (vol->eval_mode == EVAL_MODE_FRAME)
  350. set_volume(ctx);
  351. if (vol->volume == 1.0 || vol->volume_i == 256) {
  352. out_buf = buf;
  353. goto end;
  354. }
  355. /* do volume scaling in-place if input buffer is writable */
  356. if (av_frame_is_writable(buf)
  357. && (vol->precision != PRECISION_FIXED || vol->volume_i > 0)) {
  358. out_buf = buf;
  359. } else {
  360. out_buf = ff_get_audio_buffer(outlink, nb_samples);
  361. if (!out_buf) {
  362. av_frame_free(&buf);
  363. return AVERROR(ENOMEM);
  364. }
  365. ret = av_frame_copy_props(out_buf, buf);
  366. if (ret < 0) {
  367. av_frame_free(&out_buf);
  368. av_frame_free(&buf);
  369. return ret;
  370. }
  371. }
  372. if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
  373. int p, plane_samples;
  374. if (av_sample_fmt_is_planar(buf->format))
  375. plane_samples = FFALIGN(nb_samples, vol->samples_align);
  376. else
  377. plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
  378. if (vol->precision == PRECISION_FIXED) {
  379. for (p = 0; p < vol->planes; p++) {
  380. vol->scale_samples(out_buf->extended_data[p],
  381. buf->extended_data[p], plane_samples,
  382. vol->volume_i);
  383. }
  384. } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
  385. for (p = 0; p < vol->planes; p++) {
  386. vol->fdsp->vector_fmul_scalar((float *)out_buf->extended_data[p],
  387. (const float *)buf->extended_data[p],
  388. vol->volume, plane_samples);
  389. }
  390. } else {
  391. for (p = 0; p < vol->planes; p++) {
  392. vol->fdsp->vector_dmul_scalar((double *)out_buf->extended_data[p],
  393. (const double *)buf->extended_data[p],
  394. vol->volume, plane_samples);
  395. }
  396. }
  397. }
  398. emms_c();
  399. if (buf != out_buf)
  400. av_frame_free(&buf);
  401. end:
  402. vol->var_values[VAR_NB_CONSUMED_SAMPLES] += out_buf->nb_samples;
  403. return ff_filter_frame(outlink, out_buf);
  404. }
  405. static const AVFilterPad avfilter_af_volume_inputs[] = {
  406. {
  407. .name = "default",
  408. .type = AVMEDIA_TYPE_AUDIO,
  409. .filter_frame = filter_frame,
  410. },
  411. { NULL }
  412. };
  413. static const AVFilterPad avfilter_af_volume_outputs[] = {
  414. {
  415. .name = "default",
  416. .type = AVMEDIA_TYPE_AUDIO,
  417. .config_props = config_output,
  418. },
  419. { NULL }
  420. };
  421. AVFilter ff_af_volume = {
  422. .name = "volume",
  423. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  424. .query_formats = query_formats,
  425. .priv_size = sizeof(VolumeContext),
  426. .priv_class = &volume_class,
  427. .init = init,
  428. .uninit = uninit,
  429. .inputs = avfilter_af_volume_inputs,
  430. .outputs = avfilter_af_volume_outputs,
  431. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  432. .process_command = process_command,
  433. };