af_volume.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 *precision_str[] = {
  38. "fixed", "float", "double"
  39. };
  40. #define OFFSET(x) offsetof(VolumeContext, x)
  41. #define A AV_OPT_FLAG_AUDIO_PARAM
  42. static const AVOption options[] = {
  43. { "volume", "Volume adjustment.",
  44. OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A },
  45. { "precision", "Mathematical precision.",
  46. OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A, "precision" },
  47. { "fixed", "8-bit fixed-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A, "precision" },
  48. { "float", "32-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A, "precision" },
  49. { "double", "64-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A, "precision" },
  50. { "replaygain", "Apply replaygain side data when present",
  51. OFFSET(replaygain), AV_OPT_TYPE_INT, { .i64 = REPLAYGAIN_DROP }, REPLAYGAIN_DROP, REPLAYGAIN_ALBUM, A, "replaygain" },
  52. { "drop", "replaygain side data is dropped", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_DROP }, 0, 0, A, "replaygain" },
  53. { "ignore", "replaygain side data is ignored", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_IGNORE }, 0, 0, A, "replaygain" },
  54. { "track", "track gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_TRACK }, 0, 0, A, "replaygain" },
  55. { "album", "album gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_ALBUM }, 0, 0, A, "replaygain" },
  56. { "replaygain_preamp", "Apply replaygain pre-amplification",
  57. OFFSET(replaygain_preamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, -15.0, 15.0, A },
  58. { "replaygain_noclip", "Apply replaygain clipping prevention",
  59. OFFSET(replaygain_noclip), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, A },
  60. { NULL },
  61. };
  62. static const AVClass volume_class = {
  63. .class_name = "volume filter",
  64. .item_name = av_default_item_name,
  65. .option = options,
  66. .version = LIBAVUTIL_VERSION_INT,
  67. };
  68. static av_cold int init(AVFilterContext *ctx)
  69. {
  70. VolumeContext *vol = ctx->priv;
  71. if (vol->precision == PRECISION_FIXED) {
  72. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  73. vol->volume = vol->volume_i / 256.0;
  74. av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
  75. vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
  76. } else {
  77. av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
  78. vol->volume, 20.0*log(vol->volume)/M_LN10,
  79. precision_str[vol->precision]);
  80. }
  81. return 0;
  82. }
  83. static int query_formats(AVFilterContext *ctx)
  84. {
  85. VolumeContext *vol = ctx->priv;
  86. AVFilterFormats *formats = NULL;
  87. AVFilterChannelLayouts *layouts;
  88. static const enum AVSampleFormat sample_fmts[][7] = {
  89. /* PRECISION_FIXED */
  90. {
  91. AV_SAMPLE_FMT_U8,
  92. AV_SAMPLE_FMT_U8P,
  93. AV_SAMPLE_FMT_S16,
  94. AV_SAMPLE_FMT_S16P,
  95. AV_SAMPLE_FMT_S32,
  96. AV_SAMPLE_FMT_S32P,
  97. AV_SAMPLE_FMT_NONE
  98. },
  99. /* PRECISION_FLOAT */
  100. {
  101. AV_SAMPLE_FMT_FLT,
  102. AV_SAMPLE_FMT_FLTP,
  103. AV_SAMPLE_FMT_NONE
  104. },
  105. /* PRECISION_DOUBLE */
  106. {
  107. AV_SAMPLE_FMT_DBL,
  108. AV_SAMPLE_FMT_DBLP,
  109. AV_SAMPLE_FMT_NONE
  110. }
  111. };
  112. layouts = ff_all_channel_layouts();
  113. if (!layouts)
  114. return AVERROR(ENOMEM);
  115. ff_set_common_channel_layouts(ctx, layouts);
  116. formats = ff_make_format_list(sample_fmts[vol->precision]);
  117. if (!formats)
  118. return AVERROR(ENOMEM);
  119. ff_set_common_formats(ctx, formats);
  120. formats = ff_all_samplerates();
  121. if (!formats)
  122. return AVERROR(ENOMEM);
  123. ff_set_common_samplerates(ctx, formats);
  124. return 0;
  125. }
  126. static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
  127. int nb_samples, int volume)
  128. {
  129. int i;
  130. for (i = 0; i < nb_samples; i++)
  131. dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
  132. }
  133. static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
  134. int nb_samples, int volume)
  135. {
  136. int i;
  137. for (i = 0; i < nb_samples; i++)
  138. dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
  139. }
  140. static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
  141. int nb_samples, int volume)
  142. {
  143. int i;
  144. int16_t *smp_dst = (int16_t *)dst;
  145. const int16_t *smp_src = (const int16_t *)src;
  146. for (i = 0; i < nb_samples; i++)
  147. smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
  148. }
  149. static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
  150. int nb_samples, int volume)
  151. {
  152. int i;
  153. int16_t *smp_dst = (int16_t *)dst;
  154. const int16_t *smp_src = (const int16_t *)src;
  155. for (i = 0; i < nb_samples; i++)
  156. smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
  157. }
  158. static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
  159. int nb_samples, int volume)
  160. {
  161. int i;
  162. int32_t *smp_dst = (int32_t *)dst;
  163. const int32_t *smp_src = (const int32_t *)src;
  164. for (i = 0; i < nb_samples; i++)
  165. smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
  166. }
  167. static av_cold void volume_init(VolumeContext *vol)
  168. {
  169. vol->samples_align = 1;
  170. switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
  171. case AV_SAMPLE_FMT_U8:
  172. if (vol->volume_i < 0x1000000)
  173. vol->scale_samples = scale_samples_u8_small;
  174. else
  175. vol->scale_samples = scale_samples_u8;
  176. break;
  177. case AV_SAMPLE_FMT_S16:
  178. if (vol->volume_i < 0x10000)
  179. vol->scale_samples = scale_samples_s16_small;
  180. else
  181. vol->scale_samples = scale_samples_s16;
  182. break;
  183. case AV_SAMPLE_FMT_S32:
  184. vol->scale_samples = scale_samples_s32;
  185. break;
  186. case AV_SAMPLE_FMT_FLT:
  187. avpriv_float_dsp_init(&vol->fdsp, 0);
  188. vol->samples_align = 4;
  189. break;
  190. case AV_SAMPLE_FMT_DBL:
  191. avpriv_float_dsp_init(&vol->fdsp, 0);
  192. vol->samples_align = 8;
  193. break;
  194. }
  195. if (ARCH_X86)
  196. ff_volume_init_x86(vol);
  197. }
  198. static int config_output(AVFilterLink *outlink)
  199. {
  200. AVFilterContext *ctx = outlink->src;
  201. VolumeContext *vol = ctx->priv;
  202. AVFilterLink *inlink = ctx->inputs[0];
  203. vol->sample_fmt = inlink->format;
  204. vol->channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  205. vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
  206. volume_init(vol);
  207. return 0;
  208. }
  209. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  210. {
  211. VolumeContext *vol = inlink->dst->priv;
  212. AVFilterLink *outlink = inlink->dst->outputs[0];
  213. int nb_samples = buf->nb_samples;
  214. AVFrame *out_buf;
  215. AVFrameSideData *sd = av_frame_get_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
  216. int ret;
  217. if (sd && vol->replaygain != REPLAYGAIN_IGNORE) {
  218. if (vol->replaygain != REPLAYGAIN_DROP) {
  219. AVReplayGain *replaygain = (AVReplayGain*)sd->data;
  220. int32_t gain = 100000;
  221. uint32_t peak = 100000;
  222. float g, p;
  223. if (vol->replaygain == REPLAYGAIN_TRACK &&
  224. replaygain->track_gain != INT32_MIN) {
  225. gain = replaygain->track_gain;
  226. if (replaygain->track_peak != 0)
  227. peak = replaygain->track_peak;
  228. } else if (replaygain->album_gain != INT32_MIN) {
  229. gain = replaygain->album_gain;
  230. if (replaygain->album_peak != 0)
  231. peak = replaygain->album_peak;
  232. } else {
  233. av_log(inlink->dst, AV_LOG_WARNING, "Both ReplayGain gain "
  234. "values are unknown.\n");
  235. }
  236. g = gain / 100000.0f;
  237. p = peak / 100000.0f;
  238. av_log(inlink->dst, AV_LOG_VERBOSE,
  239. "Using gain %f dB from replaygain side data.\n", g);
  240. vol->volume = pow(10, (g + vol->replaygain_preamp) / 20);
  241. if (vol->replaygain_noclip)
  242. vol->volume = FFMIN(vol->volume, 1.0 / p);
  243. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  244. volume_init(vol);
  245. }
  246. av_frame_remove_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
  247. }
  248. if (vol->volume == 1.0 || vol->volume_i == 256)
  249. return ff_filter_frame(outlink, buf);
  250. /* do volume scaling in-place if input buffer is writable */
  251. if (av_frame_is_writable(buf)) {
  252. out_buf = buf;
  253. } else {
  254. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  255. if (!out_buf)
  256. return AVERROR(ENOMEM);
  257. ret = av_frame_copy_props(out_buf, buf);
  258. if (ret < 0) {
  259. av_frame_free(&out_buf);
  260. av_frame_free(&buf);
  261. return ret;
  262. }
  263. }
  264. if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
  265. int p, plane_samples;
  266. if (av_sample_fmt_is_planar(buf->format))
  267. plane_samples = FFALIGN(nb_samples, vol->samples_align);
  268. else
  269. plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
  270. if (vol->precision == PRECISION_FIXED) {
  271. for (p = 0; p < vol->planes; p++) {
  272. vol->scale_samples(out_buf->extended_data[p],
  273. buf->extended_data[p], plane_samples,
  274. vol->volume_i);
  275. }
  276. } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
  277. for (p = 0; p < vol->planes; p++) {
  278. vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
  279. (const float *)buf->extended_data[p],
  280. vol->volume, plane_samples);
  281. }
  282. } else {
  283. for (p = 0; p < vol->planes; p++) {
  284. vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
  285. (const double *)buf->extended_data[p],
  286. vol->volume, plane_samples);
  287. }
  288. }
  289. }
  290. emms_c();
  291. if (buf != out_buf)
  292. av_frame_free(&buf);
  293. return ff_filter_frame(outlink, out_buf);
  294. }
  295. static const AVFilterPad avfilter_af_volume_inputs[] = {
  296. {
  297. .name = "default",
  298. .type = AVMEDIA_TYPE_AUDIO,
  299. .filter_frame = filter_frame,
  300. },
  301. { NULL }
  302. };
  303. static const AVFilterPad avfilter_af_volume_outputs[] = {
  304. {
  305. .name = "default",
  306. .type = AVMEDIA_TYPE_AUDIO,
  307. .config_props = config_output,
  308. },
  309. { NULL }
  310. };
  311. AVFilter ff_af_volume = {
  312. .name = "volume",
  313. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  314. .query_formats = query_formats,
  315. .priv_size = sizeof(VolumeContext),
  316. .priv_class = &volume_class,
  317. .init = init,
  318. .inputs = avfilter_af_volume_inputs,
  319. .outputs = avfilter_af_volume_outputs,
  320. };