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.

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