split.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2007 Bobby Bingham
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * audio and video splitter
  23. */
  24. #include <stdio.h>
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/internal.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/opt.h"
  29. #include "avfilter.h"
  30. #include "audio.h"
  31. #include "filters.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. typedef struct SplitContext {
  36. const AVClass *class;
  37. int nb_outputs;
  38. } SplitContext;
  39. static av_cold int split_init(AVFilterContext *ctx)
  40. {
  41. SplitContext *s = ctx->priv;
  42. int i, ret;
  43. for (i = 0; i < s->nb_outputs; i++) {
  44. char name[32];
  45. AVFilterPad pad = { 0 };
  46. snprintf(name, sizeof(name), "output%d", i);
  47. pad.type = ctx->filter->inputs[0].type;
  48. pad.name = av_strdup(name);
  49. if (!pad.name)
  50. return AVERROR(ENOMEM);
  51. if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
  52. av_freep(&pad.name);
  53. return ret;
  54. }
  55. }
  56. return 0;
  57. }
  58. static av_cold void split_uninit(AVFilterContext *ctx)
  59. {
  60. int i;
  61. for (i = 0; i < ctx->nb_outputs; i++)
  62. av_freep(&ctx->output_pads[i].name);
  63. }
  64. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  65. {
  66. AVFilterContext *ctx = inlink->dst;
  67. int i, ret = AVERROR_EOF;
  68. for (i = 0; i < ctx->nb_outputs; i++) {
  69. AVFrame *buf_out;
  70. if (ff_outlink_get_status(ctx->outputs[i]))
  71. continue;
  72. buf_out = av_frame_clone(frame);
  73. if (!buf_out) {
  74. ret = AVERROR(ENOMEM);
  75. break;
  76. }
  77. ret = ff_filter_frame(ctx->outputs[i], buf_out);
  78. if (ret < 0)
  79. break;
  80. }
  81. av_frame_free(&frame);
  82. return ret;
  83. }
  84. #define OFFSET(x) offsetof(SplitContext, x)
  85. #define FLAGS (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
  86. static const AVOption options[] = {
  87. { "outputs", "set number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, FLAGS },
  88. { NULL }
  89. };
  90. #define split_options options
  91. AVFILTER_DEFINE_CLASS(split);
  92. #define asplit_options options
  93. AVFILTER_DEFINE_CLASS(asplit);
  94. static const AVFilterPad avfilter_vf_split_inputs[] = {
  95. {
  96. .name = "default",
  97. .type = AVMEDIA_TYPE_VIDEO,
  98. .filter_frame = filter_frame,
  99. },
  100. { NULL }
  101. };
  102. AVFilter ff_vf_split = {
  103. .name = "split",
  104. .description = NULL_IF_CONFIG_SMALL("Pass on the input to N video outputs."),
  105. .priv_size = sizeof(SplitContext),
  106. .priv_class = &split_class,
  107. .init = split_init,
  108. .uninit = split_uninit,
  109. .inputs = avfilter_vf_split_inputs,
  110. .outputs = NULL,
  111. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  112. };
  113. static const AVFilterPad avfilter_af_asplit_inputs[] = {
  114. {
  115. .name = "default",
  116. .type = AVMEDIA_TYPE_AUDIO,
  117. .filter_frame = filter_frame,
  118. },
  119. { NULL }
  120. };
  121. AVFilter ff_af_asplit = {
  122. .name = "asplit",
  123. .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
  124. .priv_size = sizeof(SplitContext),
  125. .priv_class = &asplit_class,
  126. .init = split_init,
  127. .uninit = split_uninit,
  128. .inputs = avfilter_af_asplit_inputs,
  129. .outputs = NULL,
  130. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  131. };