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.

265 lines
8.1KB

  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, ret;
  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. if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
  116. av_freep(&inpad.name);
  117. return ret;
  118. }
  119. }
  120. return 0;
  121. }
  122. static av_cold void uninit(AVFilterContext *ctx)
  123. {
  124. InterleaveContext *s = ctx->priv;
  125. int i;
  126. for (i = 0; i < ctx->nb_inputs; i++) {
  127. ff_bufqueue_discard_all(&s->queues[i]);
  128. av_freep(&s->queues[i]);
  129. av_freep(&ctx->input_pads[i].name);
  130. }
  131. }
  132. static int config_output(AVFilterLink *outlink)
  133. {
  134. AVFilterContext *ctx = outlink->src;
  135. AVFilterLink *inlink0 = ctx->inputs[0];
  136. int i;
  137. if (outlink->type == AVMEDIA_TYPE_VIDEO) {
  138. outlink->time_base = AV_TIME_BASE_Q;
  139. outlink->w = inlink0->w;
  140. outlink->h = inlink0->h;
  141. outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
  142. outlink->format = inlink0->format;
  143. outlink->frame_rate = (AVRational) {1, 0};
  144. for (i = 1; i < ctx->nb_inputs; i++) {
  145. AVFilterLink *inlink = ctx->inputs[i];
  146. if (outlink->w != inlink->w ||
  147. outlink->h != inlink->h ||
  148. outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
  149. outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
  150. av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
  151. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  152. "output link parameters (%dx%d, SAR %d:%d)\n",
  153. ctx->input_pads[i].name, inlink->w, inlink->h,
  154. inlink->sample_aspect_ratio.num,
  155. inlink->sample_aspect_ratio.den,
  156. outlink->w, outlink->h,
  157. outlink->sample_aspect_ratio.num,
  158. outlink->sample_aspect_ratio.den);
  159. return AVERROR(EINVAL);
  160. }
  161. }
  162. }
  163. return 0;
  164. }
  165. static int request_frame(AVFilterLink *outlink)
  166. {
  167. AVFilterContext *ctx = outlink->src;
  168. InterleaveContext *s = ctx->priv;
  169. int i, ret;
  170. for (i = 0; i < ctx->nb_inputs; i++) {
  171. if (!s->queues[i].available && !ctx->inputs[i]->status_out) {
  172. ret = ff_request_frame(ctx->inputs[i]);
  173. if (ret != AVERROR_EOF)
  174. return ret;
  175. }
  176. }
  177. return push_frame(ctx);
  178. }
  179. #if CONFIG_INTERLEAVE_FILTER
  180. DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  181. AVFILTER_DEFINE_CLASS(interleave);
  182. static const AVFilterPad interleave_outputs[] = {
  183. {
  184. .name = "default",
  185. .type = AVMEDIA_TYPE_VIDEO,
  186. .config_props = config_output,
  187. .request_frame = request_frame,
  188. },
  189. { NULL }
  190. };
  191. AVFilter ff_vf_interleave = {
  192. .name = "interleave",
  193. .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
  194. .priv_size = sizeof(InterleaveContext),
  195. .init = init,
  196. .uninit = uninit,
  197. .outputs = interleave_outputs,
  198. .priv_class = &interleave_class,
  199. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  200. };
  201. #endif
  202. #if CONFIG_AINTERLEAVE_FILTER
  203. DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
  204. AVFILTER_DEFINE_CLASS(ainterleave);
  205. static const AVFilterPad ainterleave_outputs[] = {
  206. {
  207. .name = "default",
  208. .type = AVMEDIA_TYPE_AUDIO,
  209. .config_props = config_output,
  210. .request_frame = request_frame,
  211. },
  212. { NULL }
  213. };
  214. AVFilter ff_af_ainterleave = {
  215. .name = "ainterleave",
  216. .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
  217. .priv_size = sizeof(InterleaveContext),
  218. .init = init,
  219. .uninit = uninit,
  220. .outputs = ainterleave_outputs,
  221. .priv_class = &ainterleave_class,
  222. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  223. };
  224. #endif