af_acontrast.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2008 Rob Sykes
  3. * Copyright (c) 2017 Paul B Mahol
  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. #include "libavutil/channel_layout.h"
  22. #include "libavutil/opt.h"
  23. #include "avfilter.h"
  24. #include "audio.h"
  25. #include "formats.h"
  26. typedef struct AudioContrastContext {
  27. const AVClass *class;
  28. float contrast;
  29. void (*filter)(void **dst, const void **src,
  30. int nb_samples, int channels, float contrast);
  31. } AudioContrastContext;
  32. #define OFFSET(x) offsetof(AudioContrastContext, x)
  33. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  34. static const AVOption acontrast_options[] = {
  35. { "contrast", "set contrast", OFFSET(contrast), AV_OPT_TYPE_FLOAT, {.dbl=33}, 0, 100, A },
  36. { NULL }
  37. };
  38. AVFILTER_DEFINE_CLASS(acontrast);
  39. static int query_formats(AVFilterContext *ctx)
  40. {
  41. AVFilterFormats *formats = NULL;
  42. AVFilterChannelLayouts *layouts = NULL;
  43. static const enum AVSampleFormat sample_fmts[] = {
  44. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  45. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  46. AV_SAMPLE_FMT_NONE
  47. };
  48. int ret;
  49. formats = ff_make_format_list(sample_fmts);
  50. if (!formats)
  51. return AVERROR(ENOMEM);
  52. ret = ff_set_common_formats(ctx, formats);
  53. if (ret < 0)
  54. return ret;
  55. layouts = ff_all_channel_counts();
  56. if (!layouts)
  57. return AVERROR(ENOMEM);
  58. ret = ff_set_common_channel_layouts(ctx, layouts);
  59. if (ret < 0)
  60. return ret;
  61. formats = ff_all_samplerates();
  62. return ff_set_common_samplerates(ctx, formats);
  63. }
  64. static void filter_flt(void **d, const void **s,
  65. int nb_samples, int channels,
  66. float contrast)
  67. {
  68. const float *src = s[0];
  69. float *dst = d[0];
  70. int n, c;
  71. for (n = 0; n < nb_samples; n++) {
  72. for (c = 0; c < channels; c++) {
  73. float d = src[c] * M_PI_2;
  74. dst[c] = sinf(d + contrast * sinf(d * 4));
  75. }
  76. dst += c;
  77. src += c;
  78. }
  79. }
  80. static void filter_dbl(void **d, const void **s,
  81. int nb_samples, int channels,
  82. float contrast)
  83. {
  84. const double *src = s[0];
  85. double *dst = d[0];
  86. int n, c;
  87. for (n = 0; n < nb_samples; n++) {
  88. for (c = 0; c < channels; c++) {
  89. double d = src[c] * M_PI_2;
  90. dst[c] = sin(d + contrast * sin(d * 4));
  91. }
  92. dst += c;
  93. src += c;
  94. }
  95. }
  96. static void filter_fltp(void **d, const void **s,
  97. int nb_samples, int channels,
  98. float contrast)
  99. {
  100. int n, c;
  101. for (c = 0; c < channels; c++) {
  102. const float *src = s[c];
  103. float *dst = d[c];
  104. for (n = 0; n < nb_samples; n++) {
  105. float d = src[n] * M_PI_2;
  106. dst[n] = sinf(d + contrast * sinf(d * 4));
  107. }
  108. }
  109. }
  110. static void filter_dblp(void **d, const void **s,
  111. int nb_samples, int channels,
  112. float contrast)
  113. {
  114. int n, c;
  115. for (c = 0; c < channels; c++) {
  116. const double *src = s[c];
  117. double *dst = d[c];
  118. for (n = 0; n < nb_samples; n++) {
  119. double d = src[n] * M_PI_2;
  120. dst[n] = sin(d + contrast * sin(d * 4));
  121. }
  122. }
  123. }
  124. static int config_input(AVFilterLink *inlink)
  125. {
  126. AVFilterContext *ctx = inlink->dst;
  127. AudioContrastContext *s = ctx->priv;
  128. switch (inlink->format) {
  129. case AV_SAMPLE_FMT_FLT: s->filter = filter_flt; break;
  130. case AV_SAMPLE_FMT_DBL: s->filter = filter_dbl; break;
  131. case AV_SAMPLE_FMT_FLTP: s->filter = filter_fltp; break;
  132. case AV_SAMPLE_FMT_DBLP: s->filter = filter_dblp; break;
  133. }
  134. return 0;
  135. }
  136. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  137. {
  138. AVFilterContext *ctx = inlink->dst;
  139. AVFilterLink *outlink = ctx->outputs[0];
  140. AudioContrastContext *s = ctx->priv;
  141. AVFrame *out;
  142. if (av_frame_is_writable(in)) {
  143. out = in;
  144. } else {
  145. out = ff_get_audio_buffer(outlink, in->nb_samples);
  146. if (!out) {
  147. av_frame_free(&in);
  148. return AVERROR(ENOMEM);
  149. }
  150. av_frame_copy_props(out, in);
  151. }
  152. s->filter((void **)out->extended_data, (const void **)in->extended_data,
  153. in->nb_samples, in->channels, s->contrast / 750);
  154. if (out != in)
  155. av_frame_free(&in);
  156. return ff_filter_frame(outlink, out);
  157. }
  158. static const AVFilterPad inputs[] = {
  159. {
  160. .name = "default",
  161. .type = AVMEDIA_TYPE_AUDIO,
  162. .filter_frame = filter_frame,
  163. .config_props = config_input,
  164. },
  165. { NULL }
  166. };
  167. static const AVFilterPad outputs[] = {
  168. {
  169. .name = "default",
  170. .type = AVMEDIA_TYPE_AUDIO,
  171. },
  172. { NULL }
  173. };
  174. AVFilter ff_af_acontrast = {
  175. .name = "acontrast",
  176. .description = NULL_IF_CONFIG_SMALL("Simple audio dynamic range compression/expansion filter."),
  177. .query_formats = query_formats,
  178. .priv_size = sizeof(AudioContrastContext),
  179. .priv_class = &acontrast_class,
  180. .inputs = inputs,
  181. .outputs = outputs,
  182. };