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.

315 lines
9.9KB

  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/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. #define OFFSET(x) offsetof(VolumeContext, x)
  39. #define A AV_OPT_FLAG_AUDIO_PARAM
  40. static const AVOption options[] = {
  41. { "volume", "Volume adjustment.",
  42. OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A },
  43. { "precision", "Mathematical precision.",
  44. OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A, "precision" },
  45. { "fixed", "8-bit fixed-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A, "precision" },
  46. { "float", "32-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A, "precision" },
  47. { "double", "64-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A, "precision" },
  48. { NULL },
  49. };
  50. static const AVClass volume_class = {
  51. .class_name = "volume filter",
  52. .item_name = av_default_item_name,
  53. .option = options,
  54. .version = LIBAVUTIL_VERSION_INT,
  55. };
  56. static av_cold int init(AVFilterContext *ctx)
  57. {
  58. VolumeContext *vol = ctx->priv;
  59. if (vol->precision == PRECISION_FIXED) {
  60. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  61. vol->volume = vol->volume_i / 256.0;
  62. av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
  63. vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
  64. } else {
  65. av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
  66. vol->volume, 20.0*log(vol->volume)/M_LN10,
  67. precision_str[vol->precision]);
  68. }
  69. return 0;
  70. }
  71. static int query_formats(AVFilterContext *ctx)
  72. {
  73. VolumeContext *vol = ctx->priv;
  74. AVFilterFormats *formats = NULL;
  75. AVFilterChannelLayouts *layouts;
  76. static const enum AVSampleFormat sample_fmts[][7] = {
  77. /* PRECISION_FIXED */
  78. {
  79. AV_SAMPLE_FMT_U8,
  80. AV_SAMPLE_FMT_U8P,
  81. AV_SAMPLE_FMT_S16,
  82. AV_SAMPLE_FMT_S16P,
  83. AV_SAMPLE_FMT_S32,
  84. AV_SAMPLE_FMT_S32P,
  85. AV_SAMPLE_FMT_NONE
  86. },
  87. /* PRECISION_FLOAT */
  88. {
  89. AV_SAMPLE_FMT_FLT,
  90. AV_SAMPLE_FMT_FLTP,
  91. AV_SAMPLE_FMT_NONE
  92. },
  93. /* PRECISION_DOUBLE */
  94. {
  95. AV_SAMPLE_FMT_DBL,
  96. AV_SAMPLE_FMT_DBLP,
  97. AV_SAMPLE_FMT_NONE
  98. }
  99. };
  100. layouts = ff_all_channel_layouts();
  101. if (!layouts)
  102. return AVERROR(ENOMEM);
  103. ff_set_common_channel_layouts(ctx, layouts);
  104. formats = ff_make_format_list(sample_fmts[vol->precision]);
  105. if (!formats)
  106. return AVERROR(ENOMEM);
  107. ff_set_common_formats(ctx, formats);
  108. formats = ff_all_samplerates();
  109. if (!formats)
  110. return AVERROR(ENOMEM);
  111. ff_set_common_samplerates(ctx, formats);
  112. return 0;
  113. }
  114. static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
  115. int nb_samples, int volume)
  116. {
  117. int i;
  118. for (i = 0; i < nb_samples; i++)
  119. dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
  120. }
  121. static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
  122. int nb_samples, int volume)
  123. {
  124. int i;
  125. for (i = 0; i < nb_samples; i++)
  126. dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
  127. }
  128. static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
  129. int nb_samples, int volume)
  130. {
  131. int i;
  132. int16_t *smp_dst = (int16_t *)dst;
  133. const int16_t *smp_src = (const int16_t *)src;
  134. for (i = 0; i < nb_samples; i++)
  135. smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
  136. }
  137. static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
  138. int nb_samples, int volume)
  139. {
  140. int i;
  141. int16_t *smp_dst = (int16_t *)dst;
  142. const int16_t *smp_src = (const int16_t *)src;
  143. for (i = 0; i < nb_samples; i++)
  144. smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
  145. }
  146. static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
  147. int nb_samples, int volume)
  148. {
  149. int i;
  150. int32_t *smp_dst = (int32_t *)dst;
  151. const int32_t *smp_src = (const int32_t *)src;
  152. for (i = 0; i < nb_samples; i++)
  153. smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
  154. }
  155. static av_cold void volume_init(VolumeContext *vol)
  156. {
  157. vol->samples_align = 1;
  158. switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
  159. case AV_SAMPLE_FMT_U8:
  160. if (vol->volume_i < 0x1000000)
  161. vol->scale_samples = scale_samples_u8_small;
  162. else
  163. vol->scale_samples = scale_samples_u8;
  164. break;
  165. case AV_SAMPLE_FMT_S16:
  166. if (vol->volume_i < 0x10000)
  167. vol->scale_samples = scale_samples_s16_small;
  168. else
  169. vol->scale_samples = scale_samples_s16;
  170. break;
  171. case AV_SAMPLE_FMT_S32:
  172. vol->scale_samples = scale_samples_s32;
  173. break;
  174. case AV_SAMPLE_FMT_FLT:
  175. avpriv_float_dsp_init(&vol->fdsp, 0);
  176. vol->samples_align = 4;
  177. break;
  178. case AV_SAMPLE_FMT_DBL:
  179. avpriv_float_dsp_init(&vol->fdsp, 0);
  180. vol->samples_align = 8;
  181. break;
  182. }
  183. if (ARCH_X86)
  184. ff_volume_init_x86(vol);
  185. }
  186. static int config_output(AVFilterLink *outlink)
  187. {
  188. AVFilterContext *ctx = outlink->src;
  189. VolumeContext *vol = ctx->priv;
  190. AVFilterLink *inlink = ctx->inputs[0];
  191. vol->sample_fmt = inlink->format;
  192. vol->channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  193. vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
  194. volume_init(vol);
  195. return 0;
  196. }
  197. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  198. {
  199. VolumeContext *vol = inlink->dst->priv;
  200. AVFilterLink *outlink = inlink->dst->outputs[0];
  201. int nb_samples = buf->nb_samples;
  202. AVFrame *out_buf;
  203. int ret;
  204. if (vol->volume == 1.0 || vol->volume_i == 256)
  205. return ff_filter_frame(outlink, buf);
  206. /* do volume scaling in-place if input buffer is writable */
  207. if (av_frame_is_writable(buf)) {
  208. out_buf = buf;
  209. } else {
  210. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  211. if (!out_buf)
  212. return AVERROR(ENOMEM);
  213. ret = av_frame_copy_props(out_buf, buf);
  214. if (ret < 0) {
  215. av_frame_free(&out_buf);
  216. av_frame_free(&buf);
  217. return ret;
  218. }
  219. }
  220. if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
  221. int p, plane_samples;
  222. if (av_sample_fmt_is_planar(buf->format))
  223. plane_samples = FFALIGN(nb_samples, vol->samples_align);
  224. else
  225. plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
  226. if (vol->precision == PRECISION_FIXED) {
  227. for (p = 0; p < vol->planes; p++) {
  228. vol->scale_samples(out_buf->extended_data[p],
  229. buf->extended_data[p], plane_samples,
  230. vol->volume_i);
  231. }
  232. } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
  233. for (p = 0; p < vol->planes; p++) {
  234. vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
  235. (const float *)buf->extended_data[p],
  236. vol->volume, plane_samples);
  237. }
  238. } else {
  239. for (p = 0; p < vol->planes; p++) {
  240. vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
  241. (const double *)buf->extended_data[p],
  242. vol->volume, plane_samples);
  243. }
  244. }
  245. }
  246. if (buf != out_buf)
  247. av_frame_free(&buf);
  248. return ff_filter_frame(outlink, out_buf);
  249. }
  250. static const AVFilterPad avfilter_af_volume_inputs[] = {
  251. {
  252. .name = "default",
  253. .type = AVMEDIA_TYPE_AUDIO,
  254. .filter_frame = filter_frame,
  255. },
  256. { NULL }
  257. };
  258. static const AVFilterPad avfilter_af_volume_outputs[] = {
  259. {
  260. .name = "default",
  261. .type = AVMEDIA_TYPE_AUDIO,
  262. .config_props = config_output,
  263. },
  264. { NULL }
  265. };
  266. AVFilter ff_af_volume = {
  267. .name = "volume",
  268. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  269. .query_formats = query_formats,
  270. .priv_size = sizeof(VolumeContext),
  271. .priv_class = &volume_class,
  272. .init = init,
  273. .inputs = avfilter_af_volume_inputs,
  274. .outputs = avfilter_af_volume_outputs,
  275. };