af_crystalizer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. void (*filter)(void **dst, void **prv, const void **src,
  31. int nb_samples, int channels, float mult, int clip);
  32. } CrystalizerContext;
  33. #define OFFSET(x) offsetof(CrystalizerContext, x)
  34. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  35. static const AVOption crystalizer_options[] = {
  36. { "i", "set intensity", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0}, 0, 10, A },
  37. { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A },
  38. { NULL }
  39. };
  40. AVFILTER_DEFINE_CLASS(crystalizer);
  41. static int query_formats(AVFilterContext *ctx)
  42. {
  43. AVFilterFormats *formats = NULL;
  44. AVFilterChannelLayouts *layouts = NULL;
  45. static const enum AVSampleFormat sample_fmts[] = {
  46. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  47. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  48. AV_SAMPLE_FMT_NONE
  49. };
  50. int ret;
  51. formats = ff_make_format_list(sample_fmts);
  52. if (!formats)
  53. return AVERROR(ENOMEM);
  54. ret = ff_set_common_formats(ctx, formats);
  55. if (ret < 0)
  56. return ret;
  57. layouts = ff_all_channel_counts();
  58. if (!layouts)
  59. return AVERROR(ENOMEM);
  60. ret = ff_set_common_channel_layouts(ctx, layouts);
  61. if (ret < 0)
  62. return ret;
  63. formats = ff_all_samplerates();
  64. return ff_set_common_samplerates(ctx, formats);
  65. }
  66. static void filter_flt(void **d, void **p, const void **s,
  67. int nb_samples, int channels,
  68. float mult, int clip)
  69. {
  70. const float *src = s[0];
  71. float *dst = d[0];
  72. float *prv = p[0];
  73. int n, c;
  74. for (n = 0; n < nb_samples; n++) {
  75. for (c = 0; c < channels; c++) {
  76. float current = src[c];
  77. dst[c] = current + (current - prv[c]) * mult;
  78. prv[c] = current;
  79. if (clip) {
  80. dst[c] = av_clipf(dst[c], -1, 1);
  81. }
  82. }
  83. dst += c;
  84. src += c;
  85. }
  86. }
  87. static void filter_dbl(void **d, void **p, const void **s,
  88. int nb_samples, int channels,
  89. float mult, int clip)
  90. {
  91. const double *src = s[0];
  92. double *dst = d[0];
  93. double *prv = p[0];
  94. int n, c;
  95. for (n = 0; n < nb_samples; n++) {
  96. for (c = 0; c < channels; c++) {
  97. double current = src[c];
  98. dst[c] = current + (current - prv[c]) * mult;
  99. prv[c] = current;
  100. if (clip) {
  101. dst[c] = av_clipd(dst[c], -1, 1);
  102. }
  103. }
  104. dst += c;
  105. src += c;
  106. }
  107. }
  108. static void filter_fltp(void **d, void **p, const void **s,
  109. int nb_samples, int channels,
  110. float mult, int clip)
  111. {
  112. int n, c;
  113. for (c = 0; c < channels; c++) {
  114. const float *src = s[c];
  115. float *dst = d[c];
  116. float *prv = p[c];
  117. for (n = 0; n < nb_samples; n++) {
  118. float current = src[n];
  119. dst[n] = current + (current - prv[0]) * mult;
  120. prv[0] = current;
  121. if (clip) {
  122. dst[n] = av_clipf(dst[n], -1, 1);
  123. }
  124. }
  125. }
  126. }
  127. static void filter_dblp(void **d, void **p, const void **s,
  128. int nb_samples, int channels,
  129. float mult, int clip)
  130. {
  131. int n, c;
  132. for (c = 0; c < channels; c++) {
  133. const double *src = s[c];
  134. double *dst = d[c];
  135. double *prv = p[c];
  136. for (n = 0; n < nb_samples; n++) {
  137. double current = src[n];
  138. dst[n] = current + (current - prv[0]) * mult;
  139. prv[0] = current;
  140. if (clip) {
  141. dst[n] = av_clipd(dst[n], -1, 1);
  142. }
  143. }
  144. }
  145. }
  146. static int config_input(AVFilterLink *inlink)
  147. {
  148. AVFilterContext *ctx = inlink->dst;
  149. CrystalizerContext *s = ctx->priv;
  150. switch (inlink->format) {
  151. case AV_SAMPLE_FMT_FLT: s->filter = filter_flt; break;
  152. case AV_SAMPLE_FMT_DBL: s->filter = filter_dbl; break;
  153. case AV_SAMPLE_FMT_FLTP: s->filter = filter_fltp; break;
  154. case AV_SAMPLE_FMT_DBLP: s->filter = filter_dblp; break;
  155. }
  156. return 0;
  157. }
  158. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  159. {
  160. AVFilterContext *ctx = inlink->dst;
  161. AVFilterLink *outlink = ctx->outputs[0];
  162. CrystalizerContext *s = ctx->priv;
  163. AVFrame *out;
  164. if (!s->prev) {
  165. s->prev = ff_get_audio_buffer(inlink, 1);
  166. if (!s->prev) {
  167. av_frame_free(&in);
  168. return AVERROR(ENOMEM);
  169. }
  170. }
  171. if (av_frame_is_writable(in)) {
  172. out = in;
  173. } else {
  174. out = ff_get_audio_buffer(outlink, in->nb_samples);
  175. if (!out) {
  176. av_frame_free(&in);
  177. return AVERROR(ENOMEM);
  178. }
  179. av_frame_copy_props(out, in);
  180. }
  181. s->filter((void **)out->extended_data, (void **)s->prev->extended_data, (const void **)in->extended_data,
  182. in->nb_samples, in->channels, s->mult, s->clip);
  183. if (out != in)
  184. av_frame_free(&in);
  185. return ff_filter_frame(outlink, out);
  186. }
  187. static av_cold void uninit(AVFilterContext *ctx)
  188. {
  189. CrystalizerContext *s = ctx->priv;
  190. av_frame_free(&s->prev);
  191. }
  192. static const AVFilterPad inputs[] = {
  193. {
  194. .name = "default",
  195. .type = AVMEDIA_TYPE_AUDIO,
  196. .filter_frame = filter_frame,
  197. .config_props = config_input,
  198. },
  199. { NULL }
  200. };
  201. static const AVFilterPad outputs[] = {
  202. {
  203. .name = "default",
  204. .type = AVMEDIA_TYPE_AUDIO,
  205. },
  206. { NULL }
  207. };
  208. AVFilter ff_af_crystalizer = {
  209. .name = "crystalizer",
  210. .description = NULL_IF_CONFIG_SMALL("Simple expand audio dynamic range filter."),
  211. .query_formats = query_formats,
  212. .priv_size = sizeof(CrystalizerContext),
  213. .priv_class = &crystalizer_class,
  214. .uninit = uninit,
  215. .inputs = inputs,
  216. .outputs = outputs,
  217. };