af_volume.c 17 KB

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