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.

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