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.

282 lines
9.0KB

  1. /*
  2. * Copyright (c) 2013 Stefano Sabatini
  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 interleaver
  23. */
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "filters.h"
  30. #include "internal.h"
  31. #include "audio.h"
  32. #include "video.h"
  33. typedef struct InterleaveContext {
  34. const AVClass *class;
  35. int nb_inputs;
  36. int duration_mode;
  37. int64_t pts;
  38. } InterleaveContext;
  39. #define DURATION_LONGEST 0
  40. #define DURATION_SHORTEST 1
  41. #define DURATION_FIRST 2
  42. #define OFFSET(x) offsetof(InterleaveContext, x)
  43. #define DEFINE_OPTIONS(filt_name, flags_) \
  44. static const AVOption filt_name##_options[] = { \
  45. { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
  46. { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
  47. { "duration", "how to determine the end-of-stream", \
  48. OFFSET(duration_mode), AV_OPT_TYPE_INT, { .i64 = DURATION_LONGEST }, 0, 2, flags_, "duration" }, \
  49. { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_LONGEST }, 0, 0, flags_, "duration" }, \
  50. { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_SHORTEST }, 0, 0, flags_, "duration" }, \
  51. { "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, { .i64 = DURATION_FIRST }, 0, 0, flags_, "duration" }, \
  52. { NULL } \
  53. }
  54. static int activate(AVFilterContext *ctx)
  55. {
  56. AVFilterLink *outlink = ctx->outputs[0];
  57. InterleaveContext *s = ctx->priv;
  58. int64_t q_pts, pts = INT64_MAX;
  59. int i, nb_eofs = 0, input_idx = -1;
  60. int first_eof = 0;
  61. int64_t rpts;
  62. int status;
  63. int nb_inputs_with_frames = 0;
  64. FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
  65. for (i = 0; i < ctx->nb_inputs; i++) {
  66. int is_eof = !!ff_inlink_acknowledge_status(ctx->inputs[i], &status, &rpts);
  67. nb_eofs += is_eof;
  68. if (i == 0)
  69. first_eof = is_eof;
  70. }
  71. if ((nb_eofs > 0 && s->duration_mode == DURATION_SHORTEST) ||
  72. (nb_eofs == ctx->nb_inputs && s->duration_mode == DURATION_LONGEST) ||
  73. (first_eof && s->duration_mode == DURATION_FIRST)) {
  74. ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
  75. return 0;
  76. }
  77. for (i = 0; i < ctx->nb_inputs; i++) {
  78. if (!ff_inlink_queued_frames(ctx->inputs[i]))
  79. continue;
  80. nb_inputs_with_frames++;
  81. }
  82. if (nb_inputs_with_frames >= ctx->nb_inputs - nb_eofs) {
  83. for (i = 0; i < ctx->nb_inputs; i++) {
  84. AVFrame *frame;
  85. if (ff_inlink_queued_frames(ctx->inputs[i]) == 0)
  86. continue;
  87. frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
  88. if (frame->pts == AV_NOPTS_VALUE) {
  89. int ret;
  90. av_log(ctx, AV_LOG_WARNING,
  91. "NOPTS value for input frame cannot be accepted, frame discarded\n");
  92. ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
  93. if (ret < 0)
  94. return ret;
  95. av_frame_free(&frame);
  96. return AVERROR_INVALIDDATA;
  97. }
  98. q_pts = av_rescale_q(frame->pts, ctx->inputs[i]->time_base, AV_TIME_BASE_Q);
  99. if (q_pts < pts) {
  100. pts = q_pts;
  101. input_idx = i;
  102. }
  103. }
  104. if (input_idx >= 0) {
  105. AVFrame *frame;
  106. int ret;
  107. ret = ff_inlink_consume_frame(ctx->inputs[input_idx], &frame);
  108. if (ret < 0)
  109. return ret;
  110. frame->pts = s->pts = pts;
  111. return ff_filter_frame(outlink, frame);
  112. }
  113. }
  114. for (i = 0; i < ctx->nb_inputs; i++) {
  115. if (ff_inlink_queued_frames(ctx->inputs[i]))
  116. continue;
  117. if (ff_outlink_frame_wanted(outlink) &&
  118. !ff_outlink_get_status(ctx->inputs[i])) {
  119. ff_inlink_request_frame(ctx->inputs[i]);
  120. return 0;
  121. }
  122. }
  123. if (i == ctx->nb_inputs - nb_eofs && ff_outlink_frame_wanted(outlink)) {
  124. ff_filter_set_ready(ctx, 100);
  125. return 0;
  126. }
  127. return FFERROR_NOT_READY;
  128. }
  129. static av_cold int init(AVFilterContext *ctx)
  130. {
  131. InterleaveContext *s = ctx->priv;
  132. const AVFilterPad *outpad = &ctx->filter->outputs[0];
  133. int i, ret;
  134. for (i = 0; i < s->nb_inputs; i++) {
  135. AVFilterPad inpad = { 0 };
  136. inpad.name = av_asprintf("input%d", i);
  137. if (!inpad.name)
  138. return AVERROR(ENOMEM);
  139. inpad.type = outpad->type;
  140. switch (outpad->type) {
  141. case AVMEDIA_TYPE_VIDEO:
  142. inpad.get_video_buffer = ff_null_get_video_buffer; break;
  143. case AVMEDIA_TYPE_AUDIO:
  144. inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
  145. default:
  146. av_assert0(0);
  147. }
  148. if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
  149. av_freep(&inpad.name);
  150. return ret;
  151. }
  152. }
  153. return 0;
  154. }
  155. static av_cold void uninit(AVFilterContext *ctx)
  156. {
  157. for (int i = 0; i < ctx->nb_inputs; i++)
  158. av_freep(&ctx->input_pads[i].name);
  159. }
  160. static int config_output(AVFilterLink *outlink)
  161. {
  162. AVFilterContext *ctx = outlink->src;
  163. AVFilterLink *inlink0 = ctx->inputs[0];
  164. int i;
  165. if (outlink->type == AVMEDIA_TYPE_VIDEO) {
  166. outlink->time_base = AV_TIME_BASE_Q;
  167. outlink->w = inlink0->w;
  168. outlink->h = inlink0->h;
  169. outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
  170. outlink->format = inlink0->format;
  171. outlink->frame_rate = (AVRational) {1, 0};
  172. for (i = 1; i < ctx->nb_inputs; i++) {
  173. AVFilterLink *inlink = ctx->inputs[i];
  174. if (outlink->w != inlink->w ||
  175. outlink->h != inlink->h ||
  176. outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
  177. outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
  178. av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
  179. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  180. "output link parameters (%dx%d, SAR %d:%d)\n",
  181. ctx->input_pads[i].name, inlink->w, inlink->h,
  182. inlink->sample_aspect_ratio.num,
  183. inlink->sample_aspect_ratio.den,
  184. outlink->w, outlink->h,
  185. outlink->sample_aspect_ratio.num,
  186. outlink->sample_aspect_ratio.den);
  187. return AVERROR(EINVAL);
  188. }
  189. }
  190. }
  191. return 0;
  192. }
  193. #if CONFIG_INTERLEAVE_FILTER
  194. DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  195. AVFILTER_DEFINE_CLASS(interleave);
  196. static const AVFilterPad interleave_outputs[] = {
  197. {
  198. .name = "default",
  199. .type = AVMEDIA_TYPE_VIDEO,
  200. .config_props = config_output,
  201. },
  202. { NULL }
  203. };
  204. AVFilter ff_vf_interleave = {
  205. .name = "interleave",
  206. .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
  207. .priv_size = sizeof(InterleaveContext),
  208. .init = init,
  209. .uninit = uninit,
  210. .activate = activate,
  211. .outputs = interleave_outputs,
  212. .priv_class = &interleave_class,
  213. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  214. };
  215. #endif
  216. #if CONFIG_AINTERLEAVE_FILTER
  217. DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  218. AVFILTER_DEFINE_CLASS(ainterleave);
  219. static const AVFilterPad ainterleave_outputs[] = {
  220. {
  221. .name = "default",
  222. .type = AVMEDIA_TYPE_AUDIO,
  223. .config_props = config_output,
  224. },
  225. { NULL }
  226. };
  227. AVFilter ff_af_ainterleave = {
  228. .name = "ainterleave",
  229. .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
  230. .priv_size = sizeof(InterleaveContext),
  231. .init = init,
  232. .uninit = uninit,
  233. .activate = activate,
  234. .outputs = ainterleave_outputs,
  235. .priv_class = &ainterleave_class,
  236. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  237. };
  238. #endif