af_volume.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * audio volume filter
  23. * based on ffmpeg.c code
  24. */
  25. #include "libavutil/audioconvert.h"
  26. #include "libavutil/eval.h"
  27. #include "audio.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. typedef struct {
  31. double volume;
  32. int volume_i;
  33. } VolumeContext;
  34. static av_cold int init(AVFilterContext *ctx, const char *args)
  35. {
  36. VolumeContext *vol = ctx->priv;
  37. char *tail;
  38. int ret = 0;
  39. vol->volume = 1.0;
  40. if (args) {
  41. /* parse the number as a decimal number */
  42. double d = strtod(args, &tail);
  43. if (*tail) {
  44. if (!strcmp(tail, "dB")) {
  45. /* consider the argument an adjustement in decibels */
  46. d = pow(10, d/20);
  47. } else {
  48. /* parse the argument as an expression */
  49. ret = av_expr_parse_and_eval(&d, args, NULL, NULL,
  50. NULL, NULL, NULL, NULL,
  51. NULL, 0, ctx);
  52. }
  53. }
  54. if (ret < 0) {
  55. av_log(ctx, AV_LOG_ERROR,
  56. "Invalid volume argument '%s'\n", args);
  57. return AVERROR(EINVAL);
  58. }
  59. if (d < 0 || d > 65536) { /* 65536 = INT_MIN / (128 * 256) */
  60. av_log(ctx, AV_LOG_ERROR,
  61. "Negative or too big volume value %f\n", d);
  62. return AVERROR(EINVAL);
  63. }
  64. vol->volume = d;
  65. }
  66. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  67. av_log(ctx, AV_LOG_VERBOSE, "volume=%f\n", vol->volume);
  68. return 0;
  69. }
  70. static int query_formats(AVFilterContext *ctx)
  71. {
  72. AVFilterFormats *formats = NULL;
  73. AVFilterChannelLayouts *layouts;
  74. enum AVSampleFormat sample_fmts[] = {
  75. AV_SAMPLE_FMT_U8,
  76. AV_SAMPLE_FMT_S16,
  77. AV_SAMPLE_FMT_S32,
  78. AV_SAMPLE_FMT_FLT,
  79. AV_SAMPLE_FMT_DBL,
  80. AV_SAMPLE_FMT_NONE
  81. };
  82. layouts = ff_all_channel_layouts();
  83. if (!layouts)
  84. return AVERROR(ENOMEM);
  85. ff_set_common_channel_layouts(ctx, layouts);
  86. formats = ff_make_format_list(sample_fmts);
  87. if (!formats)
  88. return AVERROR(ENOMEM);
  89. ff_set_common_formats(ctx, formats);
  90. formats = ff_all_samplerates();
  91. if (!formats)
  92. return AVERROR(ENOMEM);
  93. ff_set_common_samplerates(ctx, formats);
  94. return 0;
  95. }
  96. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  97. {
  98. VolumeContext *vol = inlink->dst->priv;
  99. AVFilterLink *outlink = inlink->dst->outputs[0];
  100. const int nb_samples = insamples->audio->nb_samples *
  101. av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
  102. const double volume = vol->volume;
  103. const int volume_i = vol->volume_i;
  104. int i;
  105. if (volume_i != 256) {
  106. switch (insamples->format) {
  107. case AV_SAMPLE_FMT_U8:
  108. {
  109. uint8_t *p = (void *)insamples->data[0];
  110. for (i = 0; i < nb_samples; i++) {
  111. int v = (((*p - 128) * volume_i + 128) >> 8) + 128;
  112. *p++ = av_clip_uint8(v);
  113. }
  114. break;
  115. }
  116. case AV_SAMPLE_FMT_S16:
  117. {
  118. int16_t *p = (void *)insamples->data[0];
  119. for (i = 0; i < nb_samples; i++) {
  120. int v = ((int64_t)*p * volume_i + 128) >> 8;
  121. *p++ = av_clip_int16(v);
  122. }
  123. break;
  124. }
  125. case AV_SAMPLE_FMT_S32:
  126. {
  127. int32_t *p = (void *)insamples->data[0];
  128. for (i = 0; i < nb_samples; i++) {
  129. int64_t v = (((int64_t)*p * volume_i + 128) >> 8);
  130. *p++ = av_clipl_int32(v);
  131. }
  132. break;
  133. }
  134. case AV_SAMPLE_FMT_FLT:
  135. {
  136. float *p = (void *)insamples->data[0];
  137. float scale = (float)volume;
  138. for (i = 0; i < nb_samples; i++) {
  139. *p++ *= scale;
  140. }
  141. break;
  142. }
  143. case AV_SAMPLE_FMT_DBL:
  144. {
  145. double *p = (void *)insamples->data[0];
  146. for (i = 0; i < nb_samples; i++) {
  147. *p *= volume;
  148. p++;
  149. }
  150. break;
  151. }
  152. }
  153. }
  154. return ff_filter_samples(outlink, insamples);
  155. }
  156. AVFilter avfilter_af_volume = {
  157. .name = "volume",
  158. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  159. .query_formats = query_formats,
  160. .priv_size = sizeof(VolumeContext),
  161. .init = init,
  162. .inputs = (const AVFilterPad[]) {{ .name = "default",
  163. .type = AVMEDIA_TYPE_AUDIO,
  164. .filter_samples = filter_samples,
  165. .min_perms = AV_PERM_READ|AV_PERM_WRITE},
  166. { .name = NULL}},
  167. .outputs = (const AVFilterPad[]) {{ .name = "default",
  168. .type = AVMEDIA_TYPE_AUDIO, },
  169. { .name = NULL}},
  170. };