af_bs2b.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Bauer stereo-to-binaural filter
  21. */
  22. #include <bs2b.h>
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/opt.h"
  26. #include "audio.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. typedef struct Bs2bContext {
  31. const AVClass *class;
  32. int profile;
  33. int fcut;
  34. int feed;
  35. t_bs2bdp bs2bp;
  36. void (*filter)(t_bs2bdp bs2bdp, uint8_t *sample, int n);
  37. } Bs2bContext;
  38. #define OFFSET(x) offsetof(Bs2bContext, x)
  39. #define A AV_OPT_FLAG_AUDIO_PARAM
  40. static const AVOption bs2b_options[] = {
  41. { "profile", "Apply a pre-defined crossfeed level",
  42. OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, INT_MAX, A, "profile" },
  43. { "default", "default profile", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, 0, A, "profile" },
  44. { "cmoy", "Chu Moy circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_CMOY_CLEVEL }, 0, 0, A, "profile" },
  45. { "jmeier", "Jan Meier circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_JMEIER_CLEVEL }, 0, 0, A, "profile" },
  46. { "fcut", "Set cut frequency (in Hz)",
  47. OFFSET(fcut), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFCUT, A },
  48. { "feed", "Set feed level (in Hz)",
  49. OFFSET(feed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFEED, A },
  50. { NULL },
  51. };
  52. AVFILTER_DEFINE_CLASS(bs2b);
  53. static av_cold int init(AVFilterContext *ctx)
  54. {
  55. Bs2bContext *bs2b = ctx->priv;
  56. if (!(bs2b->bs2bp = bs2b_open()))
  57. return AVERROR(ENOMEM);
  58. bs2b_set_level(bs2b->bs2bp, bs2b->profile);
  59. if (bs2b->fcut)
  60. bs2b_set_level_fcut(bs2b->bs2bp, bs2b->fcut);
  61. if (bs2b->feed)
  62. bs2b_set_level_feed(bs2b->bs2bp, bs2b->feed);
  63. return 0;
  64. }
  65. static av_cold void uninit(AVFilterContext *ctx)
  66. {
  67. Bs2bContext *bs2b = ctx->priv;
  68. if (bs2b->bs2bp)
  69. bs2b_close(bs2b->bs2bp);
  70. }
  71. static int query_formats(AVFilterContext *ctx)
  72. {
  73. AVFilterFormats *formats = NULL;
  74. AVFilterChannelLayouts *layouts = NULL;
  75. static const enum AVSampleFormat sample_fmts[] = {
  76. AV_SAMPLE_FMT_U8,
  77. AV_SAMPLE_FMT_S16,
  78. AV_SAMPLE_FMT_S32,
  79. AV_SAMPLE_FMT_FLT,
  80. AV_SAMPLE_FMT_DBL,
  81. AV_SAMPLE_FMT_NONE,
  82. };
  83. int ret;
  84. if (ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO) != 0)
  85. return AVERROR(ENOMEM);
  86. ret = ff_set_common_channel_layouts(ctx, layouts);
  87. if (ret < 0)
  88. return ret;
  89. formats = ff_make_format_list(sample_fmts);
  90. if (!formats)
  91. return AVERROR(ENOMEM);
  92. ret = ff_set_common_formats(ctx, formats);
  93. if (ret < 0)
  94. return ret;
  95. formats = ff_all_samplerates();
  96. if (!formats)
  97. return AVERROR(ENOMEM);
  98. return ff_set_common_samplerates(ctx, formats);
  99. }
  100. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  101. {
  102. int ret;
  103. AVFrame *out_frame;
  104. Bs2bContext *bs2b = inlink->dst->priv;
  105. AVFilterLink *outlink = inlink->dst->outputs[0];
  106. if (av_frame_is_writable(frame)) {
  107. out_frame = frame;
  108. } else {
  109. out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
  110. if (!out_frame)
  111. return AVERROR(ENOMEM);
  112. av_frame_copy(out_frame, frame);
  113. ret = av_frame_copy_props(out_frame, frame);
  114. if (ret < 0) {
  115. av_frame_free(&out_frame);
  116. av_frame_free(&frame);
  117. return ret;
  118. }
  119. }
  120. bs2b->filter(bs2b->bs2bp, out_frame->extended_data[0], out_frame->nb_samples);
  121. if (frame != out_frame)
  122. av_frame_free(&frame);
  123. return ff_filter_frame(outlink, out_frame);
  124. }
  125. static int config_output(AVFilterLink *outlink)
  126. {
  127. AVFilterContext *ctx = outlink->src;
  128. Bs2bContext *bs2b = ctx->priv;
  129. AVFilterLink *inlink = ctx->inputs[0];
  130. int srate = inlink->sample_rate;
  131. switch (inlink->format) {
  132. case AV_SAMPLE_FMT_U8:
  133. bs2b->filter = bs2b_cross_feed_u8;
  134. break;
  135. case AV_SAMPLE_FMT_S16:
  136. bs2b->filter = (void*)bs2b_cross_feed_s16;
  137. break;
  138. case AV_SAMPLE_FMT_S32:
  139. bs2b->filter = (void*)bs2b_cross_feed_s32;
  140. break;
  141. case AV_SAMPLE_FMT_FLT:
  142. bs2b->filter = (void*)bs2b_cross_feed_f;
  143. break;
  144. case AV_SAMPLE_FMT_DBL:
  145. bs2b->filter = (void*)bs2b_cross_feed_d;
  146. break;
  147. default:
  148. return AVERROR_BUG;
  149. }
  150. if ((srate < BS2B_MINSRATE) || (srate > BS2B_MAXSRATE))
  151. return AVERROR(ENOSYS);
  152. bs2b_set_srate(bs2b->bs2bp, srate);
  153. return 0;
  154. }
  155. static const AVFilterPad bs2b_inputs[] = {
  156. {
  157. .name = "default",
  158. .type = AVMEDIA_TYPE_AUDIO,
  159. .filter_frame = filter_frame,
  160. },
  161. { NULL }
  162. };
  163. static const AVFilterPad bs2b_outputs[] = {
  164. {
  165. .name = "default",
  166. .type = AVMEDIA_TYPE_AUDIO,
  167. .config_props = config_output,
  168. },
  169. { NULL }
  170. };
  171. AVFilter ff_af_bs2b = {
  172. .name = "bs2b",
  173. .description = NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
  174. .query_formats = query_formats,
  175. .priv_size = sizeof(Bs2bContext),
  176. .priv_class = &bs2b_class,
  177. .init = init,
  178. .uninit = uninit,
  179. .inputs = bs2b_inputs,
  180. .outputs = bs2b_outputs,
  181. };