af_volume.c 17 KB

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