af_volume.c 5.7 KB

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