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.

474 lines
16KB

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