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.

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