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.

145 lines
4.4KB

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