split.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "avfilter.h"
  28. #include "audio.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. static int split_init(AVFilterContext *ctx, const char *args)
  32. {
  33. int i, nb_outputs = 2;
  34. if (args) {
  35. nb_outputs = strtol(args, NULL, 0);
  36. if (nb_outputs <= 0) {
  37. av_log(ctx, AV_LOG_ERROR, "Invalid number of outputs specified: %d.\n",
  38. nb_outputs);
  39. return AVERROR(EINVAL);
  40. }
  41. }
  42. for (i = 0; i < nb_outputs; i++) {
  43. char name[32];
  44. AVFilterPad pad = { 0 };
  45. snprintf(name, sizeof(name), "output%d", i);
  46. pad.type = ctx->filter->inputs[0].type;
  47. pad.name = av_strdup(name);
  48. pad.rej_perms = AV_PERM_WRITE;
  49. ff_insert_outpad(ctx, i, &pad);
  50. }
  51. return 0;
  52. }
  53. static void split_uninit(AVFilterContext *ctx)
  54. {
  55. int i;
  56. for (i = 0; i < ctx->nb_outputs; i++)
  57. av_freep(&ctx->output_pads[i].name);
  58. }
  59. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  60. {
  61. AVFilterContext *ctx = inlink->dst;
  62. int i, ret = AVERROR_EOF;
  63. for (i = 0; i < ctx->nb_outputs; i++) {
  64. AVFilterBufferRef *buf_out;
  65. if (ctx->outputs[i]->closed)
  66. continue;
  67. buf_out = avfilter_ref_buffer(frame, ~AV_PERM_WRITE);
  68. if (!buf_out) {
  69. ret = AVERROR(ENOMEM);
  70. break;
  71. }
  72. ret = ff_filter_frame(ctx->outputs[i], buf_out);
  73. if (ret < 0)
  74. break;
  75. }
  76. avfilter_unref_bufferp(&frame);
  77. return ret;
  78. }
  79. static const AVFilterPad avfilter_vf_split_inputs[] = {
  80. {
  81. .name = "default",
  82. .type = AVMEDIA_TYPE_VIDEO,
  83. .get_video_buffer = ff_null_get_video_buffer,
  84. .filter_frame = filter_frame,
  85. },
  86. { NULL }
  87. };
  88. AVFilter avfilter_vf_split = {
  89. .name = "split",
  90. .description = NULL_IF_CONFIG_SMALL("Pass on the input video to N outputs."),
  91. .init = split_init,
  92. .uninit = split_uninit,
  93. .inputs = avfilter_vf_split_inputs,
  94. .outputs = NULL,
  95. };
  96. static const AVFilterPad avfilter_af_asplit_inputs[] = {
  97. {
  98. .name = "default",
  99. .type = AVMEDIA_TYPE_AUDIO,
  100. .get_audio_buffer = ff_null_get_audio_buffer,
  101. .filter_frame = filter_frame,
  102. },
  103. { NULL }
  104. };
  105. AVFilter avfilter_af_asplit = {
  106. .name = "asplit",
  107. .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
  108. .init = split_init,
  109. .uninit = split_uninit,
  110. .inputs = avfilter_af_asplit_inputs,
  111. .outputs = NULL,
  112. };