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.

152 lines
4.3KB

  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. * Channel split filter
  21. *
  22. * Split an audio stream into per-channel streams.
  23. */
  24. #include "libavutil/channel_layout.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/opt.h"
  27. #include "audio.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. typedef struct ChannelSplitContext {
  32. const AVClass *class;
  33. uint64_t channel_layout;
  34. char *channel_layout_str;
  35. } ChannelSplitContext;
  36. #define OFFSET(x) offsetof(ChannelSplitContext, x)
  37. #define A AV_OPT_FLAG_AUDIO_PARAM
  38. #define F AV_OPT_FLAG_FILTERING_PARAM
  39. static const AVOption channelsplit_options[] = {
  40. { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A|F },
  41. { NULL },
  42. };
  43. AVFILTER_DEFINE_CLASS(channelsplit);
  44. static int init(AVFilterContext *ctx)
  45. {
  46. ChannelSplitContext *s = ctx->priv;
  47. int nb_channels;
  48. int ret = 0, i;
  49. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  50. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  51. s->channel_layout_str);
  52. ret = AVERROR(EINVAL);
  53. goto fail;
  54. }
  55. nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  56. for (i = 0; i < nb_channels; i++) {
  57. uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
  58. AVFilterPad pad = { 0 };
  59. pad.type = AVMEDIA_TYPE_AUDIO;
  60. pad.name = av_get_channel_name(channel);
  61. ff_insert_outpad(ctx, i, &pad);
  62. }
  63. fail:
  64. return ret;
  65. }
  66. static int query_formats(AVFilterContext *ctx)
  67. {
  68. ChannelSplitContext *s = ctx->priv;
  69. AVFilterChannelLayouts *in_layouts = NULL;
  70. int i;
  71. ff_set_common_formats (ctx, ff_planar_sample_fmts());
  72. ff_set_common_samplerates(ctx, ff_all_samplerates());
  73. ff_add_channel_layout(&in_layouts, s->channel_layout);
  74. ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts);
  75. for (i = 0; i < ctx->nb_outputs; i++) {
  76. AVFilterChannelLayouts *out_layouts = NULL;
  77. uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
  78. ff_add_channel_layout(&out_layouts, channel);
  79. ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts);
  80. }
  81. return 0;
  82. }
  83. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  84. {
  85. AVFilterContext *ctx = inlink->dst;
  86. int i, ret = 0;
  87. for (i = 0; i < ctx->nb_outputs; i++) {
  88. AVFrame *buf_out = av_frame_clone(buf);
  89. if (!buf_out) {
  90. ret = AVERROR(ENOMEM);
  91. break;
  92. }
  93. buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
  94. buf_out->channel_layout =
  95. av_channel_layout_extract_channel(buf->channel_layout, i);
  96. av_frame_set_channels(buf_out, 1);
  97. ret = ff_filter_frame(ctx->outputs[i], buf_out);
  98. if (ret < 0)
  99. break;
  100. }
  101. av_frame_free(&buf);
  102. return ret;
  103. }
  104. static const AVFilterPad avfilter_af_channelsplit_inputs[] = {
  105. {
  106. .name = "default",
  107. .type = AVMEDIA_TYPE_AUDIO,
  108. .filter_frame = filter_frame,
  109. },
  110. { NULL }
  111. };
  112. AVFilter avfilter_af_channelsplit = {
  113. .name = "channelsplit",
  114. .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams"),
  115. .priv_size = sizeof(ChannelSplitContext),
  116. .priv_class = &channelsplit_class,
  117. .init = init,
  118. .query_formats = query_formats,
  119. .inputs = avfilter_af_channelsplit_inputs,
  120. .outputs = NULL,
  121. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  122. };