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.6KB

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