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.

301 lines
9.7KB

  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)
  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. AV_SAMPLE_FMT_U8,
  75. AV_SAMPLE_FMT_U8P,
  76. AV_SAMPLE_FMT_S16,
  77. AV_SAMPLE_FMT_S16P,
  78. AV_SAMPLE_FMT_S32,
  79. AV_SAMPLE_FMT_S32P,
  80. AV_SAMPLE_FMT_NONE
  81. },
  82. [PRECISION_FLOAT] = {
  83. AV_SAMPLE_FMT_FLT,
  84. AV_SAMPLE_FMT_FLTP,
  85. AV_SAMPLE_FMT_NONE
  86. },
  87. [PRECISION_DOUBLE] = {
  88. AV_SAMPLE_FMT_DBL,
  89. AV_SAMPLE_FMT_DBLP,
  90. AV_SAMPLE_FMT_NONE
  91. }
  92. };
  93. layouts = ff_all_channel_layouts();
  94. if (!layouts)
  95. return AVERROR(ENOMEM);
  96. ff_set_common_channel_layouts(ctx, layouts);
  97. formats = ff_make_format_list(sample_fmts[vol->precision]);
  98. if (!formats)
  99. return AVERROR(ENOMEM);
  100. ff_set_common_formats(ctx, formats);
  101. formats = ff_all_samplerates();
  102. if (!formats)
  103. return AVERROR(ENOMEM);
  104. ff_set_common_samplerates(ctx, formats);
  105. return 0;
  106. }
  107. static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
  108. int nb_samples, int volume)
  109. {
  110. int i;
  111. for (i = 0; i < nb_samples; i++)
  112. dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
  113. }
  114. static inline void scale_samples_u8_small(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((((src[i] - 128) * volume + 128) >> 8) + 128);
  120. }
  121. static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
  122. int nb_samples, int volume)
  123. {
  124. int i;
  125. int16_t *smp_dst = (int16_t *)dst;
  126. const int16_t *smp_src = (const int16_t *)src;
  127. for (i = 0; i < nb_samples; i++)
  128. smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
  129. }
  130. static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
  131. int nb_samples, int volume)
  132. {
  133. int i;
  134. int16_t *smp_dst = (int16_t *)dst;
  135. const int16_t *smp_src = (const int16_t *)src;
  136. for (i = 0; i < nb_samples; i++)
  137. smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
  138. }
  139. static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
  140. int nb_samples, int volume)
  141. {
  142. int i;
  143. int32_t *smp_dst = (int32_t *)dst;
  144. const int32_t *smp_src = (const int32_t *)src;
  145. for (i = 0; i < nb_samples; i++)
  146. smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
  147. }
  148. static av_cold void volume_init(VolumeContext *vol)
  149. {
  150. vol->samples_align = 1;
  151. switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
  152. case AV_SAMPLE_FMT_U8:
  153. if (vol->volume_i < 0x1000000)
  154. vol->scale_samples = scale_samples_u8_small;
  155. else
  156. vol->scale_samples = scale_samples_u8;
  157. break;
  158. case AV_SAMPLE_FMT_S16:
  159. if (vol->volume_i < 0x10000)
  160. vol->scale_samples = scale_samples_s16_small;
  161. else
  162. vol->scale_samples = scale_samples_s16;
  163. break;
  164. case AV_SAMPLE_FMT_S32:
  165. vol->scale_samples = scale_samples_s32;
  166. break;
  167. case AV_SAMPLE_FMT_FLT:
  168. avpriv_float_dsp_init(&vol->fdsp, 0);
  169. vol->samples_align = 4;
  170. break;
  171. case AV_SAMPLE_FMT_DBL:
  172. avpriv_float_dsp_init(&vol->fdsp, 0);
  173. vol->samples_align = 8;
  174. break;
  175. }
  176. if (ARCH_X86)
  177. ff_volume_init_x86(vol);
  178. }
  179. static int config_output(AVFilterLink *outlink)
  180. {
  181. AVFilterContext *ctx = outlink->src;
  182. VolumeContext *vol = ctx->priv;
  183. AVFilterLink *inlink = ctx->inputs[0];
  184. vol->sample_fmt = inlink->format;
  185. vol->channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
  186. vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
  187. volume_init(vol);
  188. return 0;
  189. }
  190. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  191. {
  192. VolumeContext *vol = inlink->dst->priv;
  193. AVFilterLink *outlink = inlink->dst->outputs[0];
  194. int nb_samples = buf->nb_samples;
  195. AVFrame *out_buf;
  196. if (vol->volume == 1.0 || vol->volume_i == 256)
  197. return ff_filter_frame(outlink, buf);
  198. /* do volume scaling in-place if input buffer is writable */
  199. if (av_frame_is_writable(buf)) {
  200. out_buf = buf;
  201. } else {
  202. out_buf = ff_get_audio_buffer(inlink, nb_samples);
  203. if (!out_buf)
  204. return AVERROR(ENOMEM);
  205. av_frame_copy_props(out_buf, buf);
  206. }
  207. if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
  208. int p, plane_samples;
  209. if (av_sample_fmt_is_planar(buf->format))
  210. plane_samples = FFALIGN(nb_samples, vol->samples_align);
  211. else
  212. plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
  213. if (vol->precision == PRECISION_FIXED) {
  214. for (p = 0; p < vol->planes; p++) {
  215. vol->scale_samples(out_buf->extended_data[p],
  216. buf->extended_data[p], plane_samples,
  217. vol->volume_i);
  218. }
  219. } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
  220. for (p = 0; p < vol->planes; p++) {
  221. vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
  222. (const float *)buf->extended_data[p],
  223. vol->volume, plane_samples);
  224. }
  225. } else {
  226. for (p = 0; p < vol->planes; p++) {
  227. vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
  228. (const double *)buf->extended_data[p],
  229. vol->volume, plane_samples);
  230. }
  231. }
  232. }
  233. if (buf != out_buf)
  234. av_frame_free(&buf);
  235. return ff_filter_frame(outlink, out_buf);
  236. }
  237. static const AVFilterPad avfilter_af_volume_inputs[] = {
  238. {
  239. .name = "default",
  240. .type = AVMEDIA_TYPE_AUDIO,
  241. .filter_frame = filter_frame,
  242. },
  243. { NULL }
  244. };
  245. static const AVFilterPad avfilter_af_volume_outputs[] = {
  246. {
  247. .name = "default",
  248. .type = AVMEDIA_TYPE_AUDIO,
  249. .config_props = config_output,
  250. },
  251. { NULL }
  252. };
  253. AVFilter ff_af_volume = {
  254. .name = "volume",
  255. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  256. .query_formats = query_formats,
  257. .priv_size = sizeof(VolumeContext),
  258. .priv_class = &volume_class,
  259. .init = init,
  260. .inputs = avfilter_af_volume_inputs,
  261. .outputs = avfilter_af_volume_outputs,
  262. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  263. };