af_dcshift.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2000 Chris Ausbrooks <weed@bucket.pp.ualr.edu>
  3. * Copyright (c) 2000 Fabien COELHO <fabien@coelho.net>
  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/opt.h"
  22. #include "libavutil/samplefmt.h"
  23. #include "avfilter.h"
  24. #include "audio.h"
  25. #include "internal.h"
  26. typedef struct DCShiftContext {
  27. const AVClass *class;
  28. double dcshift;
  29. double limiterthreshold;
  30. double limitergain;
  31. } DCShiftContext;
  32. #define OFFSET(x) offsetof(DCShiftContext, x)
  33. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  34. static const AVOption dcshift_options[] = {
  35. { "shift", "set DC shift", OFFSET(dcshift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
  36. { "limitergain", "set limiter gain", OFFSET(limitergain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, A },
  37. { NULL }
  38. };
  39. AVFILTER_DEFINE_CLASS(dcshift);
  40. static av_cold int init(AVFilterContext *ctx)
  41. {
  42. DCShiftContext *s = ctx->priv;
  43. s->limiterthreshold = INT32_MAX * (1.0 - (fabs(s->dcshift) - s->limitergain));
  44. return 0;
  45. }
  46. static int query_formats(AVFilterContext *ctx)
  47. {
  48. AVFilterChannelLayouts *layouts;
  49. AVFilterFormats *formats;
  50. static const enum AVSampleFormat sample_fmts[] = {
  51. AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
  52. };
  53. int ret;
  54. layouts = ff_all_channel_counts();
  55. if (!layouts)
  56. return AVERROR(ENOMEM);
  57. ret = ff_set_common_channel_layouts(ctx, layouts);
  58. if (ret < 0)
  59. return ret;
  60. formats = ff_make_format_list(sample_fmts);
  61. if (!formats)
  62. return AVERROR(ENOMEM);
  63. ret = ff_set_common_formats(ctx, formats);
  64. if (ret < 0)
  65. return ret;
  66. formats = ff_all_samplerates();
  67. if (!formats)
  68. return AVERROR(ENOMEM);
  69. return ff_set_common_samplerates(ctx, formats);
  70. }
  71. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  72. {
  73. AVFilterContext *ctx = inlink->dst;
  74. AVFilterLink *outlink = ctx->outputs[0];
  75. AVFrame *out;
  76. DCShiftContext *s = ctx->priv;
  77. int i, j;
  78. double dcshift = s->dcshift;
  79. if (av_frame_is_writable(in)) {
  80. out = in;
  81. } else {
  82. out = ff_get_audio_buffer(outlink, in->nb_samples);
  83. if (!out) {
  84. av_frame_free(&in);
  85. return AVERROR(ENOMEM);
  86. }
  87. av_frame_copy_props(out, in);
  88. }
  89. if (s->limitergain > 0) {
  90. for (i = 0; i < inlink->channels; i++) {
  91. const int32_t *src = (int32_t *)in->extended_data[i];
  92. int32_t *dst = (int32_t *)out->extended_data[i];
  93. for (j = 0; j < in->nb_samples; j++) {
  94. double d;
  95. d = src[j];
  96. if (d > s->limiterthreshold && dcshift > 0) {
  97. d = (d - s->limiterthreshold) * s->limitergain /
  98. (INT32_MAX - s->limiterthreshold) +
  99. s->limiterthreshold + dcshift;
  100. } else if (d < -s->limiterthreshold && dcshift < 0) {
  101. d = (d + s->limiterthreshold) * s->limitergain /
  102. (INT32_MAX - s->limiterthreshold) -
  103. s->limiterthreshold + dcshift;
  104. } else {
  105. d = dcshift * INT32_MAX + d;
  106. }
  107. dst[j] = av_clipl_int32(d);
  108. }
  109. }
  110. } else {
  111. for (i = 0; i < inlink->channels; i++) {
  112. const int32_t *src = (int32_t *)in->extended_data[i];
  113. int32_t *dst = (int32_t *)out->extended_data[i];
  114. for (j = 0; j < in->nb_samples; j++) {
  115. double d = dcshift * (INT32_MAX + 1.) + src[j];
  116. dst[j] = av_clipl_int32(d);
  117. }
  118. }
  119. }
  120. if (out != in)
  121. av_frame_free(&in);
  122. return ff_filter_frame(outlink, out);
  123. }
  124. static const AVFilterPad dcshift_inputs[] = {
  125. {
  126. .name = "default",
  127. .type = AVMEDIA_TYPE_AUDIO,
  128. .filter_frame = filter_frame,
  129. },
  130. { NULL }
  131. };
  132. static const AVFilterPad dcshift_outputs[] = {
  133. {
  134. .name = "default",
  135. .type = AVMEDIA_TYPE_AUDIO,
  136. },
  137. { NULL }
  138. };
  139. AVFilter ff_af_dcshift = {
  140. .name = "dcshift",
  141. .description = NULL_IF_CONFIG_SMALL("Apply a DC shift to the audio."),
  142. .query_formats = query_formats,
  143. .priv_size = sizeof(DCShiftContext),
  144. .priv_class = &dcshift_class,
  145. .init = init,
  146. .inputs = dcshift_inputs,
  147. .outputs = dcshift_outputs,
  148. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  149. };