af_channelsplit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. char *channels_str;
  37. int map[64];
  38. } ChannelSplitContext;
  39. #define OFFSET(x) offsetof(ChannelSplitContext, x)
  40. #define A AV_OPT_FLAG_AUDIO_PARAM
  41. #define F AV_OPT_FLAG_FILTERING_PARAM
  42. static const AVOption channelsplit_options[] = {
  43. { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A|F },
  44. { "channels", "Channels to extract.", OFFSET(channels_str), AV_OPT_TYPE_STRING, { .str = "all" }, .flags = A|F },
  45. { NULL }
  46. };
  47. AVFILTER_DEFINE_CLASS(channelsplit);
  48. static av_cold int init(AVFilterContext *ctx)
  49. {
  50. ChannelSplitContext *s = ctx->priv;
  51. uint64_t channel_layout;
  52. int nb_channels;
  53. int all = 0, ret = 0, i;
  54. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  55. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  56. s->channel_layout_str);
  57. ret = AVERROR(EINVAL);
  58. goto fail;
  59. }
  60. if (!strcmp(s->channels_str, "all")) {
  61. nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  62. channel_layout = s->channel_layout;
  63. all = 1;
  64. } else {
  65. if ((ret = av_get_extended_channel_layout(s->channels_str, &channel_layout, &nb_channels)) < 0)
  66. return ret;
  67. }
  68. for (i = 0; i < nb_channels; i++) {
  69. uint64_t channel = av_channel_layout_extract_channel(channel_layout, i);
  70. AVFilterPad pad = { 0 };
  71. pad.type = AVMEDIA_TYPE_AUDIO;
  72. pad.name = av_get_channel_name(channel);
  73. if (all) {
  74. s->map[i] = i;
  75. } else {
  76. if ((ret = av_get_channel_layout_channel_index(s->channel_layout, channel)) < 0) {
  77. av_log(ctx, AV_LOG_ERROR, "Channel name '%s' not present in channel layout '%s'.\n",
  78. av_get_channel_name(channel), s->channel_layout_str);
  79. return ret;
  80. }
  81. s->map[i] = ret;
  82. }
  83. if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
  84. return ret;
  85. }
  86. }
  87. fail:
  88. return ret;
  89. }
  90. static int query_formats(AVFilterContext *ctx)
  91. {
  92. ChannelSplitContext *s = ctx->priv;
  93. AVFilterChannelLayouts *in_layouts = NULL;
  94. int i, ret;
  95. if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
  96. (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
  97. return ret;
  98. if ((ret = ff_add_channel_layout(&in_layouts, s->channel_layout)) < 0 ||
  99. (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts)) < 0)
  100. return ret;
  101. for (i = 0; i < ctx->nb_outputs; i++) {
  102. AVFilterChannelLayouts *out_layouts = NULL;
  103. uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, s->map[i]);
  104. if ((ret = ff_add_channel_layout(&out_layouts, channel)) < 0 ||
  105. (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts)) < 0)
  106. return ret;
  107. }
  108. return 0;
  109. }
  110. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  111. {
  112. AVFilterContext *ctx = inlink->dst;
  113. ChannelSplitContext *s = ctx->priv;
  114. int i, ret = 0;
  115. for (i = 0; i < ctx->nb_outputs; i++) {
  116. AVFrame *buf_out = av_frame_clone(buf);
  117. if (!buf_out) {
  118. ret = AVERROR(ENOMEM);
  119. break;
  120. }
  121. buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
  122. buf_out->channel_layout =
  123. av_channel_layout_extract_channel(buf->channel_layout, s->map[i]);
  124. buf_out->channels = 1;
  125. ret = ff_filter_frame(ctx->outputs[i], buf_out);
  126. if (ret < 0)
  127. break;
  128. }
  129. av_frame_free(&buf);
  130. return ret;
  131. }
  132. static const AVFilterPad avfilter_af_channelsplit_inputs[] = {
  133. {
  134. .name = "default",
  135. .type = AVMEDIA_TYPE_AUDIO,
  136. .filter_frame = filter_frame,
  137. },
  138. { NULL }
  139. };
  140. AVFilter ff_af_channelsplit = {
  141. .name = "channelsplit",
  142. .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
  143. .priv_size = sizeof(ChannelSplitContext),
  144. .priv_class = &channelsplit_class,
  145. .init = init,
  146. .query_formats = query_formats,
  147. .inputs = avfilter_af_channelsplit_inputs,
  148. .outputs = NULL,
  149. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  150. };