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.

154 lines
4.1KB

  1. /*
  2. * Copyright (c) 2016 The FFmpeg Project
  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. #include "libavutil/channel_layout.h"
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "audio.h"
  24. #include "formats.h"
  25. typedef struct CrystalizerContext {
  26. const AVClass *class;
  27. float mult;
  28. int clip;
  29. AVFrame *prev;
  30. } CrystalizerContext;
  31. #define OFFSET(x) offsetof(CrystalizerContext, x)
  32. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  33. static const AVOption crystalizer_options[] = {
  34. { "i", "set intensity", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0}, 0, 10, A },
  35. { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
  36. { NULL }
  37. };
  38. AVFILTER_DEFINE_CLASS(crystalizer);
  39. static int query_formats(AVFilterContext *ctx)
  40. {
  41. AVFilterFormats *formats = NULL;
  42. AVFilterChannelLayouts *layouts = NULL;
  43. int ret;
  44. if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_FLT )) < 0 ||
  45. (ret = ff_set_common_formats(ctx , formats )) < 0)
  46. return ret;
  47. layouts = ff_all_channel_counts();
  48. if (!layouts)
  49. return AVERROR(ENOMEM);
  50. ret = ff_set_common_channel_layouts(ctx, layouts);
  51. if (ret < 0)
  52. return ret;
  53. formats = ff_all_samplerates();
  54. return ff_set_common_samplerates(ctx, formats);
  55. }
  56. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  57. {
  58. AVFilterContext *ctx = inlink->dst;
  59. AVFilterLink *outlink = ctx->outputs[0];
  60. CrystalizerContext *s = ctx->priv;
  61. const float *src = (const float *)in->data[0];
  62. const float mult = s->mult;
  63. AVFrame *out;
  64. float *dst, *prv;
  65. int n, c;
  66. if (!s->prev) {
  67. s->prev = ff_get_audio_buffer(inlink, 1);
  68. if (!s->prev) {
  69. av_frame_free(&in);
  70. return AVERROR(ENOMEM);
  71. }
  72. }
  73. if (av_frame_is_writable(in)) {
  74. out = in;
  75. } else {
  76. out = ff_get_audio_buffer(inlink, in->nb_samples);
  77. if (!out) {
  78. av_frame_free(&in);
  79. return AVERROR(ENOMEM);
  80. }
  81. av_frame_copy_props(out, in);
  82. }
  83. dst = (float *)out->data[0];
  84. prv = (float *)s->prev->data[0];
  85. for (n = 0; n < in->nb_samples; n++) {
  86. for (c = 0; c < in->channels; c++) {
  87. float current = src[c];
  88. dst[c] = current + (current - prv[c]) * mult;
  89. prv[c] = current;
  90. if (s->clip) {
  91. dst[c] = av_clipf(dst[c], -1, 1);
  92. }
  93. }
  94. dst += c;
  95. src += c;
  96. }
  97. if (out != in)
  98. av_frame_free(&in);
  99. return ff_filter_frame(outlink, out);
  100. }
  101. static av_cold void uninit(AVFilterContext *ctx)
  102. {
  103. CrystalizerContext *s = ctx->priv;
  104. av_frame_free(&s->prev);
  105. }
  106. static const AVFilterPad inputs[] = {
  107. {
  108. .name = "default",
  109. .type = AVMEDIA_TYPE_AUDIO,
  110. .filter_frame = filter_frame,
  111. },
  112. { NULL }
  113. };
  114. static const AVFilterPad outputs[] = {
  115. {
  116. .name = "default",
  117. .type = AVMEDIA_TYPE_AUDIO,
  118. },
  119. { NULL }
  120. };
  121. AVFilter ff_af_crystalizer = {
  122. .name = "crystalizer",
  123. .description = NULL_IF_CONFIG_SMALL("Simple expand audio dynamic range filter."),
  124. .query_formats = query_formats,
  125. .priv_size = sizeof(CrystalizerContext),
  126. .priv_class = &crystalizer_class,
  127. .uninit = uninit,
  128. .inputs = inputs,
  129. .outputs = outputs,
  130. };