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.

166 lines
4.7KB

  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 start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  60. {
  61. AVFilterContext *ctx = inlink->dst;
  62. int i, ret = 0;
  63. for (i = 0; i < ctx->nb_outputs; i++) {
  64. AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~AV_PERM_WRITE);
  65. if (!buf_out)
  66. return AVERROR(ENOMEM);
  67. ret = ff_start_frame(ctx->outputs[i], buf_out);
  68. if (ret < 0)
  69. break;
  70. }
  71. return ret;
  72. }
  73. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  74. {
  75. AVFilterContext *ctx = inlink->dst;
  76. int i, ret = 0;
  77. for (i = 0; i < ctx->nb_outputs; i++) {
  78. ret = ff_draw_slice(ctx->outputs[i], y, h, slice_dir);
  79. if (ret < 0)
  80. break;
  81. }
  82. return ret;
  83. }
  84. static int end_frame(AVFilterLink *inlink)
  85. {
  86. AVFilterContext *ctx = inlink->dst;
  87. int i, ret = 0;
  88. for (i = 0; i < ctx->nb_outputs; i++) {
  89. ret = ff_end_frame(ctx->outputs[i]);
  90. if (ret < 0)
  91. break;
  92. }
  93. return ret;
  94. }
  95. AVFilter avfilter_vf_split = {
  96. .name = "split",
  97. .description = NULL_IF_CONFIG_SMALL("Pass on the input video to N outputs."),
  98. .init = split_init,
  99. .uninit = split_uninit,
  100. .inputs = (const AVFilterPad[]) {{ .name = "default",
  101. .type = AVMEDIA_TYPE_VIDEO,
  102. .get_video_buffer= ff_null_get_video_buffer,
  103. .start_frame = start_frame,
  104. .draw_slice = draw_slice,
  105. .end_frame = end_frame, },
  106. { .name = NULL}},
  107. .outputs = (const AVFilterPad[]) {{ .name = NULL}},
  108. };
  109. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
  110. {
  111. AVFilterContext *ctx = inlink->dst;
  112. int i, ret = 0;
  113. for (i = 0; i < ctx->nb_outputs; i++) {
  114. AVFilterBufferRef *buf_out = avfilter_ref_buffer(samplesref,
  115. ~AV_PERM_WRITE);
  116. if (!buf_out) {
  117. ret = AVERROR(ENOMEM);
  118. break;
  119. }
  120. ret = ff_filter_samples(inlink->dst->outputs[i], buf_out);
  121. if (ret < 0)
  122. break;
  123. }
  124. avfilter_unref_buffer(samplesref);
  125. return ret;
  126. }
  127. AVFilter avfilter_af_asplit = {
  128. .name = "asplit",
  129. .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
  130. .init = split_init,
  131. .uninit = split_uninit,
  132. .inputs = (const AVFilterPad[]) {{ .name = "default",
  133. .type = AVMEDIA_TYPE_AUDIO,
  134. .get_audio_buffer = ff_null_get_audio_buffer,
  135. .filter_samples = filter_samples },
  136. { .name = NULL }},
  137. .outputs = (const AVFilterPad[]) {{ .name = NULL }},
  138. };