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.

318 lines
10KB

  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/audioconvert.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, const char *args)
  57. {
  58. VolumeContext *vol = ctx->priv;
  59. int ret;
  60. vol->class = &volume_class;
  61. av_opt_set_defaults(vol);
  62. if ((ret = av_set_options_string(vol, args, "=", ":")) < 0) {
  63. av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
  64. return ret;
  65. }
  66. if (vol->precision == PRECISION_FIXED) {
  67. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  68. vol->volume = vol->volume_i / 256.0;
  69. av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
  70. vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
  71. } else {
  72. av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
  73. vol->volume, 20.0*log(vol->volume)/M_LN10,
  74. precision_str[vol->precision]);
  75. }
  76. av_opt_free(vol);
  77. return ret;
  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 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, AVFilterBufferRef *buf)
  206. {
  207. VolumeContext *vol = inlink->dst->priv;
  208. AVFilterLink *outlink = inlink->dst->outputs[0];
  209. int nb_samples = buf->audio->nb_samples;
  210. AVFilterBufferRef *out_buf;
  211. if (vol->volume == 1.0 || vol->volume_i == 256)
  212. return ff_filter_frame(outlink, buf);
  213. /* do volume scaling in-place if input buffer is writable */
  214. if (buf->perms & AV_PERM_WRITE) {
  215. out_buf = buf;
  216. } else {
  217. out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
  218. if (!out_buf)
  219. return AVERROR(ENOMEM);
  220. out_buf->pts = buf->pts;
  221. }
  222. if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
  223. int p, plane_samples;
  224. if (av_sample_fmt_is_planar(buf->format))
  225. plane_samples = FFALIGN(nb_samples, vol->samples_align);
  226. else
  227. plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
  228. if (vol->precision == PRECISION_FIXED) {
  229. for (p = 0; p < vol->planes; p++) {
  230. vol->scale_samples(out_buf->extended_data[p],
  231. buf->extended_data[p], plane_samples,
  232. vol->volume_i);
  233. }
  234. } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
  235. for (p = 0; p < vol->planes; p++) {
  236. vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
  237. (const float *)buf->extended_data[p],
  238. vol->volume, plane_samples);
  239. }
  240. } else {
  241. for (p = 0; p < vol->planes; p++) {
  242. vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
  243. (const double *)buf->extended_data[p],
  244. vol->volume, plane_samples);
  245. }
  246. }
  247. }
  248. if (buf != out_buf)
  249. avfilter_unref_buffer(buf);
  250. return ff_filter_frame(outlink, out_buf);
  251. }
  252. static const AVFilterPad avfilter_af_volume_inputs[] = {
  253. {
  254. .name = "default",
  255. .type = AVMEDIA_TYPE_AUDIO,
  256. .filter_frame = filter_frame,
  257. },
  258. { NULL }
  259. };
  260. static const AVFilterPad avfilter_af_volume_outputs[] = {
  261. {
  262. .name = "default",
  263. .type = AVMEDIA_TYPE_AUDIO,
  264. .config_props = config_output,
  265. },
  266. { NULL }
  267. };
  268. AVFilter avfilter_af_volume = {
  269. .name = "volume",
  270. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  271. .query_formats = query_formats,
  272. .priv_size = sizeof(VolumeContext),
  273. .init = init,
  274. .inputs = avfilter_af_volume_inputs,
  275. .outputs = avfilter_af_volume_outputs,
  276. };