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.

157 lines
4.0KB

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