af_volume.c 17 KB

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