You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
12KB

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