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