f_interleave.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "bufferqueue.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "audio.h"
  32. #include "video.h"
  33. typedef struct {
  34. const AVClass *class;
  35. int nb_inputs;
  36. struct FFBufQueue *queues;
  37. } InterleaveContext;
  38. #define OFFSET(x) offsetof(InterleaveContext, x)
  39. #define DEFINE_OPTIONS(filt_name, flags_) \
  40. static const AVOption filt_name##_options[] = { \
  41. { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
  42. { "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
  43. { NULL } \
  44. }
  45. inline static int push_frame(AVFilterContext *ctx)
  46. {
  47. InterleaveContext *s = ctx->priv;
  48. AVFrame *frame;
  49. int i, queue_idx = -1;
  50. int64_t pts_min = INT64_MAX;
  51. /* look for oldest frame */
  52. for (i = 0; i < ctx->nb_inputs; i++) {
  53. struct FFBufQueue *q = &s->queues[i];
  54. if (!q->available && !ctx->inputs[i]->closed)
  55. return 0;
  56. if (q->available) {
  57. frame = ff_bufqueue_peek(q, 0);
  58. if (frame->pts < pts_min) {
  59. pts_min = frame->pts;
  60. queue_idx = i;
  61. }
  62. }
  63. }
  64. /* all inputs are closed */
  65. if (queue_idx < 0)
  66. return AVERROR_EOF;
  67. frame = ff_bufqueue_get(&s->queues[queue_idx]);
  68. av_log(ctx, AV_LOG_DEBUG, "queue:%d -> frame time:%f\n",
  69. queue_idx, frame->pts * av_q2d(AV_TIME_BASE_Q));
  70. return ff_filter_frame(ctx->outputs[0], frame);
  71. }
  72. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  73. {
  74. AVFilterContext *ctx = inlink->dst;
  75. InterleaveContext *s = ctx->priv;
  76. unsigned in_no = FF_INLINK_IDX(inlink);
  77. if (frame->pts == AV_NOPTS_VALUE) {
  78. av_log(ctx, AV_LOG_WARNING,
  79. "NOPTS value for input frame cannot be accepted, frame discarded\n");
  80. av_frame_free(&frame);
  81. return AVERROR_INVALIDDATA;
  82. }
  83. /* queue frame */
  84. frame->pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
  85. av_log(ctx, AV_LOG_DEBUG, "frame pts:%f -> queue idx:%d available:%d\n",
  86. frame->pts * av_q2d(AV_TIME_BASE_Q), in_no, s->queues[in_no].available);
  87. ff_bufqueue_add(ctx, &s->queues[in_no], frame);
  88. return push_frame(ctx);
  89. }
  90. static av_cold int init(AVFilterContext *ctx)
  91. {
  92. InterleaveContext *s = ctx->priv;
  93. const AVFilterPad *outpad = &ctx->filter->outputs[0];
  94. int i;
  95. s->queues = av_calloc(s->nb_inputs, sizeof(s->queues[0]));
  96. if (!s->queues)
  97. return AVERROR(ENOMEM);
  98. for (i = 0; i < s->nb_inputs; i++) {
  99. AVFilterPad inpad = { 0 };
  100. inpad.name = av_asprintf("input%d", i);
  101. if (!inpad.name)
  102. return AVERROR(ENOMEM);
  103. inpad.type = outpad->type;
  104. inpad.filter_frame = filter_frame;
  105. switch (outpad->type) {
  106. case AVMEDIA_TYPE_VIDEO:
  107. inpad.get_video_buffer = ff_null_get_video_buffer; break;
  108. case AVMEDIA_TYPE_AUDIO:
  109. inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
  110. default:
  111. av_assert0(0);
  112. }
  113. ff_insert_inpad(ctx, i, &inpad);
  114. }
  115. return 0;
  116. }
  117. static av_cold void uninit(AVFilterContext *ctx)
  118. {
  119. InterleaveContext *s = ctx->priv;
  120. int i;
  121. for (i = 0; i < ctx->nb_inputs; i++) {
  122. ff_bufqueue_discard_all(&s->queues[i]);
  123. av_freep(&s->queues[i]);
  124. av_freep(&ctx->input_pads[i].name);
  125. }
  126. }
  127. static int config_output(AVFilterLink *outlink)
  128. {
  129. AVFilterContext *ctx = outlink->src;
  130. AVFilterLink *inlink0 = ctx->inputs[0];
  131. int i;
  132. if (outlink->type == AVMEDIA_TYPE_VIDEO) {
  133. outlink->time_base = AV_TIME_BASE_Q;
  134. outlink->w = inlink0->w;
  135. outlink->h = inlink0->h;
  136. outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
  137. outlink->format = inlink0->format;
  138. outlink->frame_rate = (AVRational) {1, 0};
  139. for (i = 1; i < ctx->nb_inputs; i++) {
  140. AVFilterLink *inlink = ctx->inputs[i];
  141. if (outlink->w != inlink->w ||
  142. outlink->h != inlink->h ||
  143. outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
  144. outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
  145. av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
  146. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  147. "output link parameters (%dx%d, SAR %d:%d)\n",
  148. ctx->input_pads[i].name, inlink->w, inlink->h,
  149. inlink->sample_aspect_ratio.num,
  150. inlink->sample_aspect_ratio.den,
  151. outlink->w, outlink->h,
  152. outlink->sample_aspect_ratio.num,
  153. outlink->sample_aspect_ratio.den);
  154. return AVERROR(EINVAL);
  155. }
  156. }
  157. }
  158. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  159. return 0;
  160. }
  161. static int request_frame(AVFilterLink *outlink)
  162. {
  163. AVFilterContext *ctx = outlink->src;
  164. InterleaveContext *s = ctx->priv;
  165. int i, ret;
  166. for (i = 0; i < ctx->nb_inputs; i++) {
  167. if (!s->queues[i].available && !ctx->inputs[i]->closed) {
  168. ret = ff_request_frame(ctx->inputs[i]);
  169. if (ret != AVERROR_EOF)
  170. return ret;
  171. }
  172. }
  173. return push_frame(ctx);
  174. }
  175. #if CONFIG_INTERLEAVE_FILTER
  176. DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  177. AVFILTER_DEFINE_CLASS(interleave);
  178. static const AVFilterPad interleave_outputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .config_props = config_output,
  183. .request_frame = request_frame,
  184. },
  185. { NULL }
  186. };
  187. AVFilter ff_vf_interleave = {
  188. .name = "interleave",
  189. .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
  190. .priv_size = sizeof(InterleaveContext),
  191. .init = init,
  192. .uninit = uninit,
  193. .outputs = interleave_outputs,
  194. .priv_class = &interleave_class,
  195. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  196. };
  197. #endif
  198. #if CONFIG_AINTERLEAVE_FILTER
  199. DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  200. AVFILTER_DEFINE_CLASS(ainterleave);
  201. static const AVFilterPad ainterleave_outputs[] = {
  202. {
  203. .name = "default",
  204. .type = AVMEDIA_TYPE_AUDIO,
  205. .config_props = config_output,
  206. .request_frame = request_frame,
  207. },
  208. { NULL }
  209. };
  210. AVFilter ff_af_ainterleave = {
  211. .name = "ainterleave",
  212. .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
  213. .priv_size = sizeof(InterleaveContext),
  214. .init = init,
  215. .uninit = uninit,
  216. .outputs = ainterleave_outputs,
  217. .priv_class = &ainterleave_class,
  218. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  219. };
  220. #endif