af_bs2b.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 void (*filter_func)(t_bs2bdp bs2bdp, uint8_t *sample, int n);
  31. typedef struct Bs2bContext {
  32. const AVClass *class;
  33. int profile;
  34. int fcut;
  35. int feed;
  36. t_bs2bdp bs2bp;
  37. filter_func filter;
  38. } Bs2bContext;
  39. #define OFFSET(x) offsetof(Bs2bContext, x)
  40. #define A AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  41. static const AVOption bs2b_options[] = {
  42. { "profile", "Apply a pre-defined crossfeed level",
  43. OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, INT_MAX, A, "profile" },
  44. { "default", "default profile", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_DEFAULT_CLEVEL }, 0, 0, A, "profile" },
  45. { "cmoy", "Chu Moy circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_CMOY_CLEVEL }, 0, 0, A, "profile" },
  46. { "jmeier", "Jan Meier circuit", 0, AV_OPT_TYPE_CONST, { .i64 = BS2B_JMEIER_CLEVEL }, 0, 0, A, "profile" },
  47. { "fcut", "Set cut frequency (in Hz)",
  48. OFFSET(fcut), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFCUT, A },
  49. { "feed", "Set feed level (in Hz)",
  50. OFFSET(feed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BS2B_MAXFEED, A },
  51. { NULL },
  52. };
  53. AVFILTER_DEFINE_CLASS(bs2b);
  54. static av_cold int init(AVFilterContext *ctx)
  55. {
  56. Bs2bContext *bs2b = ctx->priv;
  57. if (!(bs2b->bs2bp = bs2b_open()))
  58. return AVERROR(ENOMEM);
  59. bs2b_set_level(bs2b->bs2bp, bs2b->profile);
  60. if (bs2b->fcut)
  61. bs2b_set_level_fcut(bs2b->bs2bp, bs2b->fcut);
  62. if (bs2b->feed)
  63. bs2b_set_level_feed(bs2b->bs2bp, bs2b->feed);
  64. return 0;
  65. }
  66. static av_cold void uninit(AVFilterContext *ctx)
  67. {
  68. Bs2bContext *bs2b = ctx->priv;
  69. if (bs2b->bs2bp)
  70. bs2b_close(bs2b->bs2bp);
  71. }
  72. static int query_formats(AVFilterContext *ctx)
  73. {
  74. AVFilterFormats *formats = NULL;
  75. AVFilterChannelLayouts *layouts = NULL;
  76. static const enum AVSampleFormat sample_fmts[] = {
  77. AV_SAMPLE_FMT_U8,
  78. AV_SAMPLE_FMT_S16,
  79. AV_SAMPLE_FMT_S32,
  80. AV_SAMPLE_FMT_FLT,
  81. AV_SAMPLE_FMT_DBL,
  82. AV_SAMPLE_FMT_NONE,
  83. };
  84. int ret;
  85. if (ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO) != 0)
  86. return AVERROR(ENOMEM);
  87. ret = ff_set_common_channel_layouts(ctx, layouts);
  88. if (ret < 0)
  89. return ret;
  90. formats = ff_make_format_list(sample_fmts);
  91. if (!formats)
  92. return AVERROR(ENOMEM);
  93. ret = ff_set_common_formats(ctx, formats);
  94. if (ret < 0)
  95. return ret;
  96. formats = ff_all_samplerates();
  97. if (!formats)
  98. return AVERROR(ENOMEM);
  99. return ff_set_common_samplerates(ctx, formats);
  100. }
  101. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  102. {
  103. int ret;
  104. AVFrame *out_frame;
  105. Bs2bContext *bs2b = inlink->dst->priv;
  106. AVFilterLink *outlink = inlink->dst->outputs[0];
  107. if (av_frame_is_writable(frame)) {
  108. out_frame = frame;
  109. } else {
  110. out_frame = ff_get_audio_buffer(outlink, frame->nb_samples);
  111. if (!out_frame) {
  112. av_frame_free(&frame);
  113. return AVERROR(ENOMEM);
  114. }
  115. av_frame_copy(out_frame, frame);
  116. ret = av_frame_copy_props(out_frame, frame);
  117. if (ret < 0) {
  118. av_frame_free(&out_frame);
  119. av_frame_free(&frame);
  120. return ret;
  121. }
  122. }
  123. bs2b->filter(bs2b->bs2bp, out_frame->extended_data[0], out_frame->nb_samples);
  124. if (frame != out_frame)
  125. av_frame_free(&frame);
  126. return ff_filter_frame(outlink, out_frame);
  127. }
  128. static int config_output(AVFilterLink *outlink)
  129. {
  130. AVFilterContext *ctx = outlink->src;
  131. Bs2bContext *bs2b = ctx->priv;
  132. AVFilterLink *inlink = ctx->inputs[0];
  133. int srate = inlink->sample_rate;
  134. switch (inlink->format) {
  135. case AV_SAMPLE_FMT_U8:
  136. bs2b->filter = (filter_func) bs2b_cross_feed_u8;
  137. break;
  138. case AV_SAMPLE_FMT_S16:
  139. bs2b->filter = (filter_func) bs2b_cross_feed_s16;
  140. break;
  141. case AV_SAMPLE_FMT_S32:
  142. bs2b->filter = (filter_func) bs2b_cross_feed_s32;
  143. break;
  144. case AV_SAMPLE_FMT_FLT:
  145. bs2b->filter = (filter_func) bs2b_cross_feed_f;
  146. break;
  147. case AV_SAMPLE_FMT_DBL:
  148. bs2b->filter = (filter_func) bs2b_cross_feed_d;
  149. break;
  150. default:
  151. return AVERROR_BUG;
  152. }
  153. if ((srate < BS2B_MINSRATE) || (srate > BS2B_MAXSRATE))
  154. return AVERROR(ENOSYS);
  155. bs2b_set_srate(bs2b->bs2bp, srate);
  156. return 0;
  157. }
  158. static const AVFilterPad bs2b_inputs[] = {
  159. {
  160. .name = "default",
  161. .type = AVMEDIA_TYPE_AUDIO,
  162. .filter_frame = filter_frame,
  163. },
  164. { NULL }
  165. };
  166. static const AVFilterPad bs2b_outputs[] = {
  167. {
  168. .name = "default",
  169. .type = AVMEDIA_TYPE_AUDIO,
  170. .config_props = config_output,
  171. },
  172. { NULL }
  173. };
  174. AVFilter ff_af_bs2b = {
  175. .name = "bs2b",
  176. .description = NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
  177. .query_formats = query_formats,
  178. .priv_size = sizeof(Bs2bContext),
  179. .priv_class = &bs2b_class,
  180. .init = init,
  181. .uninit = uninit,
  182. .inputs = bs2b_inputs,
  183. .outputs = bs2b_outputs,
  184. };