af_dcshift.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 = ff_get_audio_buffer(inlink, in->nb_samples);
  76. DCShiftContext *s = ctx->priv;
  77. int i, j;
  78. double dcshift = s->dcshift;
  79. if (!out) {
  80. av_frame_free(&in);
  81. return AVERROR(ENOMEM);
  82. }
  83. av_frame_copy_props(out, in);
  84. if (s->limitergain > 0) {
  85. for (i = 0; i < inlink->channels; i++) {
  86. const int32_t *src = (int32_t *)in->extended_data[i];
  87. int32_t *dst = (int32_t *)out->extended_data[i];
  88. for (j = 0; j < in->nb_samples; j++) {
  89. double d;
  90. d = src[j];
  91. if (d > s->limiterthreshold && dcshift > 0) {
  92. d = (d - s->limiterthreshold) * s->limitergain /
  93. (INT32_MAX - s->limiterthreshold) +
  94. s->limiterthreshold + dcshift;
  95. } else if (d < -s->limiterthreshold && dcshift < 0) {
  96. d = (d + s->limiterthreshold) * s->limitergain /
  97. (INT32_MAX - s->limiterthreshold) -
  98. s->limiterthreshold + dcshift;
  99. } else {
  100. d = dcshift * INT32_MAX + d;
  101. }
  102. dst[j] = av_clipl_int32(d);
  103. }
  104. }
  105. } else {
  106. for (i = 0; i < inlink->channels; i++) {
  107. const int32_t *src = (int32_t *)in->extended_data[i];
  108. int32_t *dst = (int32_t *)out->extended_data[i];
  109. for (j = 0; j < in->nb_samples; j++) {
  110. double d = dcshift * (INT32_MAX + 1.) + src[j];
  111. dst[j] = av_clipl_int32(d);
  112. }
  113. }
  114. }
  115. av_frame_free(&in);
  116. return ff_filter_frame(outlink, out);
  117. }
  118. static const AVFilterPad dcshift_inputs[] = {
  119. {
  120. .name = "default",
  121. .type = AVMEDIA_TYPE_AUDIO,
  122. .filter_frame = filter_frame,
  123. },
  124. { NULL }
  125. };
  126. static const AVFilterPad dcshift_outputs[] = {
  127. {
  128. .name = "default",
  129. .type = AVMEDIA_TYPE_AUDIO,
  130. },
  131. { NULL }
  132. };
  133. AVFilter ff_af_dcshift = {
  134. .name = "dcshift",
  135. .description = NULL_IF_CONFIG_SMALL("Apply a DC shift to the audio."),
  136. .query_formats = query_formats,
  137. .priv_size = sizeof(DCShiftContext),
  138. .priv_class = &dcshift_class,
  139. .init = init,
  140. .inputs = dcshift_inputs,
  141. .outputs = dcshift_outputs,
  142. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  143. };