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.

198 lines
5.9KB

  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, void *opaque)
  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_INFO, "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. int packing_fmts[] = { AVFILTER_PACKED, -1 };
  83. layouts = ff_all_channel_layouts();
  84. if (!layouts)
  85. return AVERROR(ENOMEM);
  86. ff_set_common_channel_layouts(ctx, layouts);
  87. formats = avfilter_make_format_list(sample_fmts);
  88. if (!formats)
  89. return AVERROR(ENOMEM);
  90. avfilter_set_common_sample_formats(ctx, formats);
  91. formats = avfilter_make_format_list(packing_fmts);
  92. if (!formats)
  93. return AVERROR(ENOMEM);
  94. avfilter_set_common_packing_formats(ctx, formats);
  95. formats = ff_all_samplerates();
  96. if (!formats)
  97. return AVERROR(ENOMEM);
  98. ff_set_common_samplerates(ctx, formats);
  99. return 0;
  100. }
  101. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  102. {
  103. VolumeContext *vol = inlink->dst->priv;
  104. AVFilterLink *outlink = inlink->dst->outputs[0];
  105. const int nb_samples = insamples->audio->nb_samples *
  106. av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
  107. const double volume = vol->volume;
  108. const int volume_i = vol->volume_i;
  109. int i;
  110. if (volume_i != 256) {
  111. switch (insamples->format) {
  112. case AV_SAMPLE_FMT_U8:
  113. {
  114. uint8_t *p = (void *)insamples->data[0];
  115. for (i = 0; i < nb_samples; i++) {
  116. int v = (((*p - 128) * volume_i + 128) >> 8) + 128;
  117. *p++ = av_clip_uint8(v);
  118. }
  119. break;
  120. }
  121. case AV_SAMPLE_FMT_S16:
  122. {
  123. int16_t *p = (void *)insamples->data[0];
  124. for (i = 0; i < nb_samples; i++) {
  125. int v = ((int64_t)*p * volume_i + 128) >> 8;
  126. *p++ = av_clip_int16(v);
  127. }
  128. break;
  129. }
  130. case AV_SAMPLE_FMT_S32:
  131. {
  132. int32_t *p = (void *)insamples->data[0];
  133. for (i = 0; i < nb_samples; i++) {
  134. int64_t v = (((int64_t)*p * volume_i + 128) >> 8);
  135. *p++ = av_clipl_int32(v);
  136. }
  137. break;
  138. }
  139. case AV_SAMPLE_FMT_FLT:
  140. {
  141. float *p = (void *)insamples->data[0];
  142. float scale = (float)volume;
  143. for (i = 0; i < nb_samples; i++) {
  144. *p++ *= scale;
  145. }
  146. break;
  147. }
  148. case AV_SAMPLE_FMT_DBL:
  149. {
  150. double *p = (void *)insamples->data[0];
  151. for (i = 0; i < nb_samples; i++) {
  152. *p *= volume;
  153. p++;
  154. }
  155. break;
  156. }
  157. }
  158. }
  159. ff_filter_samples(outlink, insamples);
  160. }
  161. AVFilter avfilter_af_volume = {
  162. .name = "volume",
  163. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  164. .query_formats = query_formats,
  165. .priv_size = sizeof(VolumeContext),
  166. .init = init,
  167. .inputs = (const AVFilterPad[]) {{ .name = "default",
  168. .type = AVMEDIA_TYPE_AUDIO,
  169. .filter_samples = filter_samples,
  170. .min_perms = AV_PERM_READ|AV_PERM_WRITE},
  171. { .name = NULL}},
  172. .outputs = (const AVFilterPad[]) {{ .name = "default",
  173. .type = AVMEDIA_TYPE_AUDIO, },
  174. { .name = NULL}},
  175. };