af_dcshift.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 limiterthreshhold;
  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->limiterthreshhold = 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. layouts = ff_all_channel_layouts();
  54. if (!layouts)
  55. return AVERROR(ENOMEM);
  56. ff_set_common_channel_layouts(ctx, layouts);
  57. formats = ff_make_format_list(sample_fmts);
  58. if (!formats)
  59. return AVERROR(ENOMEM);
  60. ff_set_common_formats(ctx, formats);
  61. formats = ff_all_samplerates();
  62. if (!formats)
  63. return AVERROR(ENOMEM);
  64. ff_set_common_samplerates(ctx, formats);
  65. return 0;
  66. }
  67. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  68. {
  69. AVFilterContext *ctx = inlink->dst;
  70. AVFilterLink *outlink = ctx->outputs[0];
  71. AVFrame *out = ff_get_audio_buffer(inlink, in->nb_samples);
  72. DCShiftContext *s = ctx->priv;
  73. int i, j;
  74. double dcshift = s->dcshift;
  75. if (!out) {
  76. av_frame_free(&in);
  77. return AVERROR(ENOMEM);
  78. }
  79. av_frame_copy_props(out, in);
  80. if (s->limitergain > 0) {
  81. for (i = 0; i < inlink->channels; i++) {
  82. const int32_t *src = (int32_t *)in->extended_data[i];
  83. int32_t *dst = (int32_t *)out->extended_data[i];
  84. for (j = 0; j < in->nb_samples; j++) {
  85. double d;
  86. d = src[j];
  87. if (d > s->limiterthreshhold && dcshift > 0) {
  88. d = (d - s->limiterthreshhold) * s->limitergain /
  89. (INT32_MAX - s->limiterthreshhold) +
  90. s->limiterthreshhold + dcshift;
  91. } else if (d < -s->limiterthreshhold && dcshift < 0) {
  92. d = (d + s->limiterthreshhold) * s->limitergain /
  93. (INT32_MAX - s->limiterthreshhold) -
  94. s->limiterthreshhold + dcshift;
  95. } else {
  96. d = dcshift * INT32_MAX + d;
  97. }
  98. dst[j] = av_clipl_int32(d);
  99. }
  100. }
  101. } else {
  102. for (i = 0; i < inlink->channels; i++) {
  103. const int32_t *src = (int32_t *)in->extended_data[i];
  104. int32_t *dst = (int32_t *)out->extended_data[i];
  105. for (j = 0; j < in->nb_samples; j++) {
  106. double d = dcshift * (INT32_MAX + 1.) + src[j];
  107. dst[j] = av_clipl_int32(d);
  108. }
  109. }
  110. }
  111. av_frame_free(&in);
  112. return ff_filter_frame(outlink, out);
  113. }
  114. static const AVFilterPad dcshift_inputs[] = {
  115. {
  116. .name = "default",
  117. .type = AVMEDIA_TYPE_AUDIO,
  118. .filter_frame = filter_frame,
  119. },
  120. { NULL }
  121. };
  122. static const AVFilterPad dcshift_outputs[] = {
  123. {
  124. .name = "default",
  125. .type = AVMEDIA_TYPE_AUDIO,
  126. },
  127. { NULL }
  128. };
  129. AVFilter ff_af_dcshift = {
  130. .name = "dcshift",
  131. .description = NULL_IF_CONFIG_SMALL("Apply a DC shift to the audio."),
  132. .query_formats = query_formats,
  133. .priv_size = sizeof(DCShiftContext),
  134. .priv_class = &dcshift_class,
  135. .init = init,
  136. .inputs = dcshift_inputs,
  137. .outputs = dcshift_outputs,
  138. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  139. };