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.

306 lines
9.8KB

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