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.

191 lines
5.7KB

  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. typedef struct {
  30. double volume;
  31. int volume_i;
  32. } VolumeContext;
  33. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  34. {
  35. VolumeContext *vol = ctx->priv;
  36. char *tail;
  37. int ret = 0;
  38. vol->volume = 1.0;
  39. if (args) {
  40. /* parse the number as a decimal number */
  41. double d = strtod(args, &tail);
  42. if (*tail) {
  43. if (!strcmp(tail, "dB")) {
  44. /* consider the argument an adjustement in decibels */
  45. d = pow(10, d/20);
  46. } else {
  47. /* parse the argument as an expression */
  48. ret = av_expr_parse_and_eval(&d, args, NULL, NULL,
  49. NULL, NULL, NULL, NULL,
  50. NULL, 0, ctx);
  51. }
  52. }
  53. if (ret < 0) {
  54. av_log(ctx, AV_LOG_ERROR,
  55. "Invalid volume argument '%s'\n", args);
  56. return AVERROR(EINVAL);
  57. }
  58. if (d < 0 || d > 65536) { /* 65536 = INT_MIN / (128 * 256) */
  59. av_log(ctx, AV_LOG_ERROR,
  60. "Negative or too big volume value %f\n", d);
  61. return AVERROR(EINVAL);
  62. }
  63. vol->volume = d;
  64. }
  65. vol->volume_i = (int)(vol->volume * 256 + 0.5);
  66. av_log(ctx, AV_LOG_INFO, "volume=%f\n", vol->volume);
  67. return 0;
  68. }
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. AVFilterFormats *formats = NULL;
  72. enum AVSampleFormat sample_fmts[] = {
  73. AV_SAMPLE_FMT_U8,
  74. AV_SAMPLE_FMT_S16,
  75. AV_SAMPLE_FMT_S32,
  76. AV_SAMPLE_FMT_FLT,
  77. AV_SAMPLE_FMT_DBL,
  78. AV_SAMPLE_FMT_NONE
  79. };
  80. int packing_fmts[] = { AVFILTER_PACKED, -1 };
  81. formats = avfilter_make_all_channel_layouts();
  82. if (!formats)
  83. return AVERROR(ENOMEM);
  84. avfilter_set_common_channel_layouts(ctx, formats);
  85. formats = avfilter_make_format_list(sample_fmts);
  86. if (!formats)
  87. return AVERROR(ENOMEM);
  88. avfilter_set_common_sample_formats(ctx, formats);
  89. formats = avfilter_make_format_list(packing_fmts);
  90. if (!formats)
  91. return AVERROR(ENOMEM);
  92. avfilter_set_common_packing_formats(ctx, formats);
  93. return 0;
  94. }
  95. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  96. {
  97. VolumeContext *vol = inlink->dst->priv;
  98. AVFilterLink *outlink = inlink->dst->outputs[0];
  99. const int nb_samples = insamples->audio->nb_samples *
  100. av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
  101. const double volume = vol->volume;
  102. const int volume_i = vol->volume_i;
  103. int i;
  104. if (volume_i != 256) {
  105. switch (insamples->format) {
  106. case AV_SAMPLE_FMT_U8:
  107. {
  108. uint8_t *p = (void *)insamples->data[0];
  109. for (i = 0; i < nb_samples; i++) {
  110. int v = (((*p - 128) * volume_i + 128) >> 8) + 128;
  111. *p++ = av_clip_uint8(v);
  112. }
  113. break;
  114. }
  115. case AV_SAMPLE_FMT_S16:
  116. {
  117. int16_t *p = (void *)insamples->data[0];
  118. for (i = 0; i < nb_samples; i++) {
  119. int v = ((int64_t)*p * volume_i + 128) >> 8;
  120. *p++ = av_clip_int16(v);
  121. }
  122. break;
  123. }
  124. case AV_SAMPLE_FMT_S32:
  125. {
  126. int32_t *p = (void *)insamples->data[0];
  127. for (i = 0; i < nb_samples; i++) {
  128. int64_t v = (((int64_t)*p * volume_i + 128) >> 8);
  129. *p++ = av_clipl_int32(v);
  130. }
  131. break;
  132. }
  133. case AV_SAMPLE_FMT_FLT:
  134. {
  135. float *p = (void *)insamples->data[0];
  136. float scale = (float)volume;
  137. for (i = 0; i < nb_samples; i++) {
  138. *p++ *= scale;
  139. }
  140. break;
  141. }
  142. case AV_SAMPLE_FMT_DBL:
  143. {
  144. double *p = (void *)insamples->data[0];
  145. for (i = 0; i < nb_samples; i++) {
  146. *p *= volume;
  147. p++;
  148. }
  149. break;
  150. }
  151. }
  152. }
  153. ff_filter_samples(outlink, insamples);
  154. }
  155. AVFilter avfilter_af_volume = {
  156. .name = "volume",
  157. .description = NULL_IF_CONFIG_SMALL("Change input volume."),
  158. .query_formats = query_formats,
  159. .priv_size = sizeof(VolumeContext),
  160. .init = init,
  161. .inputs = (const AVFilterPad[]) {{ .name = "default",
  162. .type = AVMEDIA_TYPE_AUDIO,
  163. .filter_samples = filter_samples,
  164. .min_perms = AV_PERM_READ|AV_PERM_WRITE},
  165. { .name = NULL}},
  166. .outputs = (const AVFilterPad[]) {{ .name = "default",
  167. .type = AVMEDIA_TYPE_AUDIO, },
  168. { .name = NULL}},
  169. };