af_volume.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/opt.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "af_volume.h"
  35. static const char *precision_str[] = {
  36. "fixed", "float", "double"
  37. };
  38. static const char *const var_names[] = {
  39. "n", ///< frame number (starting at zero)
  40. "nb_channels", ///< number of channels
  41. "nb_consumed_samples", ///< number of samples consumed by the filter
  42. "nb_samples", ///< number of samples in the current frame
  43. "pos", ///< position in the file of the frame
  44. "pts", ///< frame presentation timestamp
  45. "sample_rate", ///< sample rate
  46. "startpts", ///< PTS at start of stream
  47. "startt", ///< time at start of stream
  48. "t", ///< time in the file of the frame
  49. "tb", ///< timebase
  50. "volume", ///< last set value
  51. NULL
  52. };
  53. #define OFFSET(x) offsetof(VolumeContext, x)
  54. #define A AV_OPT_FLAG_AUDIO_PARAM
  55. #define F AV_OPT_FLAG_FILTERING_PARAM
  56. static const AVOption volume_options[] = {
  57. { "volume", "set volume adjustment expression",
  58. OFFSET(volume_expr), AV_OPT_TYPE_STRING, { .str = "1.0" }, .flags = A|F },
  59. { "precision", "select mathematical precision",
  60. OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A|F, "precision" },
  61. { "fixed", "select 8-bit fixed-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A|F, "precision" },
  62. { "float", "select 32-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A|F, "precision" },
  63. { "double", "select 64-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A|F, "precision" },
  64. { "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" },
  65. { "once", "eval volume expression once", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_ONCE}, .flags = A|F, .unit = "eval" },
  66. { "frame", "eval volume expression per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = A|F, .unit = "eval" },
  67. { NULL }
  68. };
  69. AVFILTER_DEFINE_CLASS(volume);
  70. static int set_expr(AVExpr **pexpr, const char *expr, void *log_ctx)
  71. {
  72. int ret;
  73. AVExpr *old = NULL;
  74. if (*pexpr)
  75. old = *pexpr;
  76. ret = av_expr_parse(pexpr, expr, var_names,
  77. NULL, NULL, NULL, NULL, 0, log_ctx);
  78. if (ret < 0) {
  79. av_log(log_ctx, AV_LOG_ERROR,
  80. "Error when evaluating the volume expression '%s'\n", expr);
  81. *pexpr = old;
  82. return ret;
  83. }
  84. av_expr_free(old);
  85. return 0;
  86. }
  87. static av_cold int init(AVFilterContext *ctx)
  88. {
  89. VolumeContext *vol = ctx->priv;
  90. return set_expr(&vol->volume_pexpr, vol->volume_expr, ctx);
  91. }
  92. static av_cold void uninit(AVFilterContext *ctx)
  93. {
  94. VolumeContext *vol = ctx->priv;
  95. av_expr_free(vol->volume_pexpr);
  96. av_opt_free(vol);
  97. }
  98. static int query_formats(AVFilterContext *ctx)
  99. {
  100. VolumeContext *vol = ctx->priv;
  101. AVFilterFormats *formats = NULL;
  102. AVFilterChannelLayouts *layouts;
  103. static const enum AVSampleFormat sample_fmts[][7] = {
  104. [PRECISION_FIXED] = {
  105. AV_SAMPLE_FMT_U8,
  106. AV_SAMPLE_FMT_U8P,
  107. AV_SAMPLE_FMT_S16,
  108. AV_SAMPLE_FMT_S16P,
  109. AV_SAMPLE_FMT_S32,
  110. AV_SAMPLE_FMT_S32P,
  111. AV_SAMPLE_FMT_NONE
  112. },
  113. [PRECISION_FLOAT] = {
  114. AV_SAMPLE_FMT_FLT,
  115. AV_SAMPLE_FMT_FLTP,
  116. AV_SAMPLE_FMT_NONE
  117. },
  118. [PRECISION_DOUBLE] = {
  119. AV_SAMPLE_FMT_DBL,
  120. AV_SAMPLE_FMT_DBLP,
  121. AV_SAMPLE_FMT_NONE
  122. }
  123. };
  124. layouts = ff_all_channel_counts();
  125. if (!layouts)
  126. return AVERROR(ENOMEM);
  127. ff_set_common_channel_layouts(ctx, layouts);
  128. formats = ff_make_format_list(sample_fmts[vol->precision]);
  129. if (!formats)
  130. return AVERROR(ENOMEM);
  131. ff_set_common_formats(ctx, formats);
  132. formats = ff_all_samplerates();
  133. if (!formats)
  134. return AVERROR(ENOMEM);
  135. ff_set_common_samplerates(ctx, formats);
  136. return 0;
  137. }
  138. static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
  139. int nb_samples, int volume)
  140. {
  141. int i;
  142. for (i = 0; i < nb_samples; i++)
  143. dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
  144. }
  145. static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
  146. int nb_samples, int volume)
  147. {
  148. int i;
  149. for (i = 0; i < nb_samples; i++)
  150. dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
  151. }
  152. static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
  153. int nb_samples, int volume)
  154. {
  155. int i;
  156. int16_t *smp_dst = (int16_t *)dst;
  157. const int16_t *smp_src = (const int16_t *)src;
  158. for (i = 0; i < nb_samples; i++)
  159. smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
  160. }
  161. static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
  162. int nb_samples, int volume)
  163. {
  164. int i;
  165. int16_t *smp_dst = (int16_t *)dst;
  166. const int16_t *smp_src = (const int16_t *)src;
  167. for (i = 0; i < nb_samples; i++)
  168. smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
  169. }
  170. static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
  171. int nb_samples, int volume)
  172. {
  173. int i;
  174. int32_t *smp_dst = (int32_t *)dst;
  175. const int32_t *smp_src = (const int32_t *)src;
  176. for (i = 0; i < nb_samples; i++)
  177. smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
  178. }
  179. static av_cold void volume_init(VolumeContext *vol)
  180. {
  181. vol->samples_align = 1;
  182. switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
  183. case AV_SAMPLE_FMT_U8:
  184. if (vol->volume_i < 0x1000000)
  185. vol->scale_samples = scale_samples_u8_small;
  186. else
  187. vol->scale_samples = scale_samples_u8;
  188. break;
  189. case AV_SAMPLE_FMT_S16:
  190. if (vol->volume_i < 0x10000)
  191. vol->scale_samples = scale_samples_s16_small;
  192. else
  193. vol->scale_samples = scale_samples_s16;
  194. break;
  195. case AV_SAMPLE_FMT_S32:
  196. vol->scale_samples = scale_samples_s32;
  197. break;
  198. case AV_SAMPLE_FMT_FLT:
  199. avpriv_float_dsp_init(&vol->fdsp, 0);
  200. vol->samples_align = 4;
  201. break;
  202. case AV_SAMPLE_FMT_DBL:
  203. avpriv_float_dsp_init(&vol->fdsp, 0);
  204. vol->samples_align = 8;
  205. break;
  206. }
  207. if (ARCH_X86)
  208. ff_volume_init_x86(vol);
  209. }
  210. static int set_volume(AVFilterContext *ctx)
  211. {
  212. VolumeContext *vol = ctx->priv;
  213. vol->volume = av_expr_eval(vol->volume_pexpr, vol->var_values, NULL);
  214. if (isnan(vol->volume)) {
  215. if (vol->eval_mode == EVAL_MODE_ONCE) {
  216. av_log(ctx, AV_LOG_ERROR, "Invalid value NaN for volume\n");
  217. return AVERROR(EINVAL);
  218. } else {
  219. av_log(ctx, AV_LOG_WARNING, "Invalid value NaN for volume, setting to 0\n");
  220. vol->volume = 0;
  221. }
  222. }
  223. vol->var_values[VAR_VOLUME] = vol->volume;
  224. av_log(ctx, AV_LOG_VERBOSE, "n:%f t:%f pts:%f precision:%s ",
  225. vol->var_values[VAR_N], vol->var_values[VAR_T], vol->var_values[VAR_PTS],
  226. precision_str[vol->precision]);
  227. if (vol->precision == PRECISION_FIXED) {
  228. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  229. vol->volume = vol->volume_i / 256.0;
  230. av_log(ctx, AV_LOG_VERBOSE, "volume_i:%d/255 ", vol->volume_i);
  231. }
  232. av_log(ctx, AV_LOG_VERBOSE, "volume:%f volume_dB:%f\n",
  233. vol->volume, 20.0*log(vol->volume)/M_LN10);
  234. volume_init(vol);
  235. return 0;
  236. }
  237. static int config_output(AVFilterLink *outlink)
  238. {
  239. AVFilterContext *ctx = outlink->src;
  240. VolumeContext *vol = ctx->priv;
  241. AVFilterLink *inlink = ctx->inputs[0];
  242. vol->sample_fmt = inlink->format;
  243. vol->channels = inlink->channels;
  244. vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
  245. vol->var_values[VAR_N] =
  246. vol->var_values[VAR_NB_CONSUMED_SAMPLES] =
  247. vol->var_values[VAR_NB_SAMPLES] =
  248. vol->var_values[VAR_POS] =
  249. vol->var_values[VAR_PTS] =
  250. vol->var_values[VAR_STARTPTS] =
  251. vol->var_values[VAR_STARTT] =
  252. vol->var_values[VAR_T] =
  253. vol->var_values[VAR_VOLUME] = NAN;
  254. vol->var_values[VAR_NB_CHANNELS] = inlink->channels;
  255. vol->var_values[VAR_TB] = av_q2d(inlink->time_base);
  256. vol->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
  257. av_log(inlink->src, AV_LOG_VERBOSE, "tb:%f sample_rate:%f nb_channels:%f\n",
  258. vol->var_values[VAR_TB],
  259. vol->var_values[VAR_SAMPLE_RATE],
  260. vol->var_values[VAR_NB_CHANNELS]);
  261. return set_volume(ctx);
  262. }
  263. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  264. char *res, int res_len, int flags)
  265. {
  266. VolumeContext *vol = ctx->priv;
  267. int ret = AVERROR(ENOSYS);
  268. if (!strcmp(cmd, "volume")) {
  269. if ((ret = set_expr(&vol->volume_pexpr, args, ctx)) < 0)
  270. return ret;
  271. if (vol->eval_mode == EVAL_MODE_ONCE)
  272. set_volume(ctx);
  273. }
  274. return ret;
  275. }
  276. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  277. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  278. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  279. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  280. {
  281. AVFilterContext *ctx = inlink->dst;
  282. VolumeContext *vol = inlink->dst->priv;
  283. AVFilterLink *outlink = inlink->dst->outputs[0];
  284. int nb_samples = buf->nb_samples;
  285. AVFrame *out_buf;
  286. int64_t pos;
  287. int ret;
  288. if (isnan(vol->var_values[VAR_STARTPTS])) {
  289. vol->var_values[VAR_STARTPTS] = TS2D(buf->pts);
  290. vol->var_values[VAR_STARTT ] = TS2T(buf->pts, inlink->time_base);
  291. }
  292. vol->var_values[VAR_PTS] = TS2D(buf->pts);
  293. vol->var_values[VAR_T ] = TS2T(buf->pts, inlink->time_base);
  294. vol->var_values[VAR_N ] = inlink->frame_count;
  295. pos = av_frame_get_pkt_pos(buf);
  296. vol->var_values[VAR_POS] = pos == -1 ? NAN : pos;
  297. if (vol->eval_mode == EVAL_MODE_FRAME)
  298. set_volume(ctx);
  299. if (vol->volume == 1.0 || vol->volume_i == 256) {
  300. out_buf = buf;
  301. goto end;
  302. }
  303. /* do volume scaling in-place if input buffer is writable */
  304. if (av_frame_is_writable(buf)) {
  305. out_buf = buf;
  306. } else {
  307. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  308. if (!out_buf)
  309. return AVERROR(ENOMEM);
  310. ret = av_frame_copy_props(out_buf, buf);
  311. if (ret < 0) {
  312. av_frame_free(&out_buf);
  313. av_frame_free(&buf);
  314. return ret;
  315. }
  316. }
  317. if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
  318. int p, plane_samples;
  319. if (av_sample_fmt_is_planar(buf->format))
  320. plane_samples = FFALIGN(nb_samples, vol->samples_align);
  321. else
  322. plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
  323. if (vol->precision == PRECISION_FIXED) {
  324. for (p = 0; p < vol->planes; p++) {
  325. vol->scale_samples(out_buf->extended_data[p],
  326. buf->extended_data[p], plane_samples,
  327. vol->volume_i);
  328. }
  329. } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
  330. for (p = 0; p < vol->planes; p++) {
  331. vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
  332. (const float *)buf->extended_data[p],
  333. vol->volume, plane_samples);
  334. }
  335. } else {
  336. for (p = 0; p < vol->planes; p++) {
  337. vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
  338. (const double *)buf->extended_data[p],
  339. vol->volume, plane_samples);
  340. }
  341. }
  342. }
  343. emms_c();
  344. if (buf != out_buf)
  345. av_frame_free(&buf);
  346. end:
  347. vol->var_values[VAR_NB_CONSUMED_SAMPLES] += out_buf->nb_samples;
  348. return ff_filter_frame(outlink, out_buf);
  349. }
  350. static const AVFilterPad avfilter_af_volume_inputs[] = {
  351. {
  352. .name = "default",
  353. .type = AVMEDIA_TYPE_AUDIO,
  354. .filter_frame = filter_frame,
  355. },
  356. { NULL }
  357. };
  358. static const AVFilterPad avfilter_af_volume_outputs[] = {
  359. {
  360. .name = "default",
  361. .type = AVMEDIA_TYPE_AUDIO,
  362. .config_props = config_output,
  363. },
  364. { NULL }
  365. };
  366. AVFilter ff_af_volume = {
  367. .name = "volume",
  368. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  369. .query_formats = query_formats,
  370. .priv_size = sizeof(VolumeContext),
  371. .priv_class = &volume_class,
  372. .init = init,
  373. .uninit = uninit,
  374. .inputs = avfilter_af_volume_inputs,
  375. .outputs = avfilter_af_volume_outputs,
  376. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  377. .process_command = process_command,
  378. };