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.

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