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.

155 lines
4.6KB

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