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.

148 lines
3.8KB

  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 "internal.h"
  32. #include "video.h"
  33. typedef struct SplitContext {
  34. const AVClass *class;
  35. int nb_outputs;
  36. } SplitContext;
  37. static av_cold int split_init(AVFilterContext *ctx)
  38. {
  39. SplitContext *s = ctx->priv;
  40. int i;
  41. for (i = 0; i < s->nb_outputs; i++) {
  42. char name[32];
  43. AVFilterPad pad = { 0 };
  44. snprintf(name, sizeof(name), "output%d", i);
  45. pad.type = ctx->filter->inputs[0].type;
  46. pad.name = av_strdup(name);
  47. ff_insert_outpad(ctx, i, &pad);
  48. }
  49. return 0;
  50. }
  51. static av_cold void split_uninit(AVFilterContext *ctx)
  52. {
  53. int i;
  54. for (i = 0; i < ctx->nb_outputs; i++)
  55. av_freep(&ctx->output_pads[i].name);
  56. }
  57. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  58. {
  59. AVFilterContext *ctx = inlink->dst;
  60. int i, ret = AVERROR_EOF;
  61. for (i = 0; i < ctx->nb_outputs; i++) {
  62. AVFrame *buf_out;
  63. if (ctx->outputs[i]->closed)
  64. continue;
  65. buf_out = av_frame_clone(frame);
  66. if (!buf_out) {
  67. ret = AVERROR(ENOMEM);
  68. break;
  69. }
  70. ret = ff_filter_frame(ctx->outputs[i], buf_out);
  71. if (ret < 0)
  72. break;
  73. }
  74. av_frame_free(&frame);
  75. return ret;
  76. }
  77. #define OFFSET(x) offsetof(SplitContext, x)
  78. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  79. static const AVOption options[] = {
  80. { "outputs", "set number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, FLAGS },
  81. { NULL }
  82. };
  83. #define split_options options
  84. AVFILTER_DEFINE_CLASS(split);
  85. #define asplit_options options
  86. AVFILTER_DEFINE_CLASS(asplit);
  87. static const AVFilterPad avfilter_vf_split_inputs[] = {
  88. {
  89. .name = "default",
  90. .type = AVMEDIA_TYPE_VIDEO,
  91. .filter_frame = filter_frame,
  92. },
  93. { NULL }
  94. };
  95. AVFilter ff_vf_split = {
  96. .name = "split",
  97. .description = NULL_IF_CONFIG_SMALL("Pass on the input to N video outputs."),
  98. .priv_size = sizeof(SplitContext),
  99. .priv_class = &split_class,
  100. .init = split_init,
  101. .uninit = split_uninit,
  102. .inputs = avfilter_vf_split_inputs,
  103. .outputs = NULL,
  104. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  105. };
  106. static const AVFilterPad avfilter_af_asplit_inputs[] = {
  107. {
  108. .name = "default",
  109. .type = AVMEDIA_TYPE_AUDIO,
  110. .filter_frame = filter_frame,
  111. },
  112. { NULL }
  113. };
  114. AVFilter ff_af_asplit = {
  115. .name = "asplit",
  116. .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
  117. .priv_size = sizeof(SplitContext),
  118. .priv_class = &asplit_class,
  119. .init = split_init,
  120. .uninit = split_uninit,
  121. .inputs = avfilter_af_asplit_inputs,
  122. .outputs = NULL,
  123. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  124. };