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.

165 lines
4.1KB

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