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.

226 lines
6.0KB

  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. int ret;
  89. if (ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO) != 0)
  90. return AVERROR(ENOMEM);
  91. ret = ff_set_common_channel_layouts(ctx, layouts);
  92. if (ret < 0)
  93. return ret;
  94. formats = ff_make_format_list(sample_fmts);
  95. if (!formats)
  96. return AVERROR(ENOMEM);
  97. ret = ff_set_common_formats(ctx, formats);
  98. if (ret < 0)
  99. return ret;
  100. formats = ff_all_samplerates();
  101. if (!formats)
  102. return AVERROR(ENOMEM);
  103. return ff_set_common_samplerates(ctx, formats);
  104. }
  105. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  106. {
  107. int ret;
  108. AVFrame *out_frame;
  109. Bs2bContext *bs2b = inlink->dst->priv;
  110. AVFilterLink *outlink = inlink->dst->outputs[0];
  111. if (av_frame_is_writable(frame)) {
  112. out_frame = frame;
  113. } else {
  114. out_frame = ff_get_audio_buffer(inlink, frame->nb_samples);
  115. if (!out_frame)
  116. return AVERROR(ENOMEM);
  117. av_frame_copy(out_frame, frame);
  118. ret = av_frame_copy_props(out_frame, frame);
  119. if (ret < 0) {
  120. av_frame_free(&out_frame);
  121. av_frame_free(&frame);
  122. return ret;
  123. }
  124. }
  125. bs2b->filter(bs2b->bs2bp, out_frame->extended_data[0], out_frame->nb_samples);
  126. if (frame != out_frame)
  127. av_frame_free(&frame);
  128. return ff_filter_frame(outlink, out_frame);
  129. }
  130. static int config_output(AVFilterLink *outlink)
  131. {
  132. AVFilterContext *ctx = outlink->src;
  133. Bs2bContext *bs2b = ctx->priv;
  134. AVFilterLink *inlink = ctx->inputs[0];
  135. int srate = inlink->sample_rate;
  136. switch (inlink->format) {
  137. case AV_SAMPLE_FMT_U8:
  138. bs2b->filter = bs2b_cross_feed_u8;
  139. break;
  140. case AV_SAMPLE_FMT_S16:
  141. bs2b->filter = (void*)bs2b_cross_feed_s16;
  142. break;
  143. case AV_SAMPLE_FMT_S32:
  144. bs2b->filter = (void*)bs2b_cross_feed_s32;
  145. break;
  146. case AV_SAMPLE_FMT_FLT:
  147. bs2b->filter = (void*)bs2b_cross_feed_f;
  148. break;
  149. case AV_SAMPLE_FMT_DBL:
  150. bs2b->filter = (void*)bs2b_cross_feed_d;
  151. break;
  152. default:
  153. return AVERROR_BUG;
  154. }
  155. if ((srate < BS2B_MINSRATE) || (srate > BS2B_MAXSRATE))
  156. return AVERROR(ENOSYS);
  157. bs2b_set_srate(bs2b->bs2bp, srate);
  158. return 0;
  159. }
  160. static const AVFilterPad bs2b_inputs[] = {
  161. {
  162. .name = "default",
  163. .type = AVMEDIA_TYPE_AUDIO,
  164. .filter_frame = filter_frame,
  165. },
  166. { NULL }
  167. };
  168. static const AVFilterPad bs2b_outputs[] = {
  169. {
  170. .name = "default",
  171. .type = AVMEDIA_TYPE_AUDIO,
  172. .config_props = config_output,
  173. },
  174. { NULL }
  175. };
  176. AVFilter ff_af_bs2b = {
  177. .name = "bs2b",
  178. .description = NULL_IF_CONFIG_SMALL("Bauer stereo-to-binaural filter."),
  179. .query_formats = query_formats,
  180. .priv_size = sizeof(Bs2bContext),
  181. .priv_class = &bs2b_class,
  182. .init = init,
  183. .uninit = uninit,
  184. .inputs = bs2b_inputs,
  185. .outputs = bs2b_outputs,
  186. };