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.

159 lines
4.6KB

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