af_volume.c 17 KB

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