af_volume.c 10.0 KB

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