af_bs2b.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 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. static const AVClass bs2b_class = {
  53. .class_name = "bs2b filter",
  54. .item_name = av_default_item_name,
  55. .option = options,
  56. .version = LIBAVUTIL_VERSION_INT,
  57. };
  58. static av_cold int init(AVFilterContext *ctx)
  59. {
  60. Bs2bContext *bs2b = ctx->priv;
  61. if (!(bs2b->bs2bp = bs2b_open()))
  62. return AVERROR(ENOMEM);
  63. bs2b_set_level(bs2b->bs2bp, bs2b->profile);
  64. if (bs2b->fcut)
  65. bs2b_set_level_fcut(bs2b->bs2bp, bs2b->fcut);
  66. if (bs2b->feed)
  67. bs2b_set_level_feed(bs2b->bs2bp, bs2b->feed);
  68. return 0;
  69. }
  70. static av_cold void uninit(AVFilterContext *ctx)
  71. {
  72. Bs2bContext *bs2b = ctx->priv;
  73. if (bs2b->bs2bp)
  74. bs2b_close(bs2b->bs2bp);
  75. }
  76. static int query_formats(AVFilterContext *ctx)
  77. {
  78. AVFilterFormats *formats = NULL;
  79. AVFilterChannelLayouts *layouts = NULL;
  80. static const enum AVSampleFormat sample_fmts[] = {
  81. AV_SAMPLE_FMT_U8,
  82. AV_SAMPLE_FMT_S16,
  83. AV_SAMPLE_FMT_S32,
  84. AV_SAMPLE_FMT_FLT,
  85. AV_SAMPLE_FMT_DBL,
  86. AV_SAMPLE_FMT_NONE,
  87. };
  88. if (ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO) != 0)
  89. return AVERROR(ENOMEM);
  90. ff_set_common_channel_layouts(ctx, layouts);
  91. formats = ff_make_format_list(sample_fmts);
  92. if (!formats)
  93. return AVERROR(ENOMEM);
  94. ff_set_common_formats(ctx, formats);
  95. formats = ff_all_samplerates();
  96. if (!formats)
  97. return AVERROR(ENOMEM);
  98. ff_set_common_samplerates(ctx, formats);
  99. return 0;
  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(inlink, frame->nb_samples);
  111. if (!out_frame)
  112. return AVERROR(ENOMEM);
  113. av_frame_copy(out_frame, frame);
  114. ret = av_frame_copy_props(out_frame, frame);
  115. if (ret < 0) {
  116. av_frame_free(&out_frame);
  117. av_frame_free(&frame);
  118. return ret;
  119. }
  120. }
  121. bs2b->filter(bs2b->bs2bp, out_frame->extended_data[0], out_frame->nb_samples);
  122. if (frame != out_frame)
  123. av_frame_free(&frame);
  124. return ff_filter_frame(outlink, out_frame);
  125. }
  126. static int config_output(AVFilterLink *outlink)
  127. {
  128. AVFilterContext *ctx = outlink->src;
  129. Bs2bContext *bs2b = ctx->priv;
  130. AVFilterLink *inlink = ctx->inputs[0];
  131. int srate = inlink->sample_rate;
  132. switch (inlink->format) {
  133. case AV_SAMPLE_FMT_U8:
  134. bs2b->filter = bs2b_cross_feed_u8;
  135. break;
  136. case AV_SAMPLE_FMT_S16:
  137. bs2b->filter = (void*)bs2b_cross_feed_s16;
  138. break;
  139. case AV_SAMPLE_FMT_S32:
  140. bs2b->filter = (void*)bs2b_cross_feed_s32;
  141. break;
  142. case AV_SAMPLE_FMT_FLT:
  143. bs2b->filter = (void*)bs2b_cross_feed_f;
  144. break;
  145. case AV_SAMPLE_FMT_DBL:
  146. bs2b->filter = (void*)bs2b_cross_feed_d;
  147. break;
  148. default:
  149. return AVERROR_BUG;
  150. }
  151. if ((srate < BS2B_MINSRATE) || (srate > BS2B_MAXSRATE))
  152. return AVERROR(ENOSYS);
  153. bs2b_set_srate(bs2b->bs2bp, srate);
  154. return 0;
  155. }
  156. static const AVFilterPad bs2b_inputs[] = {
  157. {
  158. .name = "default",
  159. .type = AVMEDIA_TYPE_AUDIO,
  160. .filter_frame = filter_frame,
  161. },
  162. { NULL }
  163. };
  164. static const AVFilterPad bs2b_outputs[] = {
  165. {
  166. .name = "default",
  167. .type = AVMEDIA_TYPE_AUDIO,
  168. .config_props = config_output,
  169. },
  170. { NULL }
  171. };
  172. AVFilter ff_af_bs2b = {
  173. .name = "bs2b",
  174. .description = NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
  175. .query_formats = query_formats,
  176. .priv_size = sizeof(Bs2bContext),
  177. .priv_class = &bs2b_class,
  178. .init = init,
  179. .uninit = uninit,
  180. .inputs = bs2b_inputs,
  181. .outputs = bs2b_outputs,
  182. };