You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.0KB

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