2013-04-08 16:16:06 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Stefano Sabatini
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* audio and video interleaver
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/opt.h"
|
2016-01-03 16:44:42 +02:00
|
|
|
|
2013-04-08 16:16:06 +03:00
|
|
|
#include "avfilter.h"
|
|
|
|
#include "formats.h"
|
2019-05-03 20:17:28 +02:00
|
|
|
#include "filters.h"
|
2013-04-08 16:16:06 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "video.h"
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct InterleaveContext {
|
2013-04-08 16:16:06 +03:00
|
|
|
const AVClass *class;
|
|
|
|
int nb_inputs;
|
2019-05-03 20:17:28 +02:00
|
|
|
int64_t pts;
|
2013-04-08 16:16:06 +03:00
|
|
|
} InterleaveContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(InterleaveContext, x)
|
|
|
|
|
|
|
|
#define DEFINE_OPTIONS(filt_name, flags_) \
|
|
|
|
static const AVOption filt_name##_options[] = { \
|
|
|
|
{ "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
|
|
|
|
{ "n", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL } \
|
2013-04-08 16:16:06 +03:00
|
|
|
}
|
|
|
|
|
2019-05-03 20:17:28 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
2013-04-08 16:16:06 +03:00
|
|
|
{
|
2019-05-03 20:17:28 +02:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2013-10-05 22:16:55 +03:00
|
|
|
InterleaveContext *s = ctx->priv;
|
2019-05-03 20:17:28 +02:00
|
|
|
int64_t q_pts, pts = INT64_MAX;
|
|
|
|
int i, nb_eofs = 0, input_idx = -1;
|
|
|
|
|
|
|
|
FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
|
2013-04-08 16:16:06 +03:00
|
|
|
|
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
2019-05-03 20:17:28 +02:00
|
|
|
if (!ff_outlink_get_status(ctx->inputs[i]) &&
|
|
|
|
!ff_inlink_queued_frames(ctx->inputs[i]))
|
|
|
|
break;
|
|
|
|
}
|
2013-04-08 16:16:06 +03:00
|
|
|
|
2019-05-03 20:17:28 +02:00
|
|
|
if (i == ctx->nb_inputs) {
|
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
|
|
|
AVFrame *frame;
|
|
|
|
|
|
|
|
if (ff_outlink_get_status(ctx->inputs[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
frame = ff_inlink_peek_frame(ctx->inputs[i], 0);
|
|
|
|
if (frame->pts == AV_NOPTS_VALUE) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
|
|
|
"NOPTS value for input frame cannot be accepted, frame discarded\n");
|
|
|
|
ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
q_pts = av_rescale_q(frame->pts, ctx->inputs[i]->time_base, AV_TIME_BASE_Q);
|
|
|
|
if (q_pts < pts) {
|
|
|
|
pts = q_pts;
|
|
|
|
input_idx = i;
|
2013-04-08 16:16:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 20:17:28 +02:00
|
|
|
if (input_idx >= 0) {
|
|
|
|
AVFrame *frame;
|
|
|
|
int ret;
|
2013-04-08 16:16:06 +03:00
|
|
|
|
2019-05-03 20:17:28 +02:00
|
|
|
ret = ff_inlink_consume_frame(ctx->inputs[input_idx], &frame);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-04-08 16:16:06 +03:00
|
|
|
|
2019-05-03 20:17:28 +02:00
|
|
|
frame->pts = s->pts = pts;
|
|
|
|
return ff_filter_frame(outlink, frame);
|
|
|
|
}
|
|
|
|
}
|
2013-04-08 16:16:06 +03:00
|
|
|
|
2019-05-03 20:17:28 +02:00
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
|
|
|
if (ff_inlink_queued_frames(ctx->inputs[i]))
|
|
|
|
continue;
|
|
|
|
if (ff_outlink_frame_wanted(outlink) &&
|
|
|
|
!ff_outlink_get_status(ctx->inputs[i])) {
|
|
|
|
ff_inlink_request_frame(ctx->inputs[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
nb_eofs++;
|
2013-04-08 16:16:06 +03:00
|
|
|
}
|
|
|
|
|
2019-05-03 20:17:28 +02:00
|
|
|
if (nb_eofs == ctx->nb_inputs) {
|
|
|
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-08 16:16:06 +03:00
|
|
|
|
2019-05-03 20:17:28 +02:00
|
|
|
return FFERROR_NOT_READY;
|
2013-04-08 16:16:06 +03:00
|
|
|
}
|
|
|
|
|
2013-09-27 21:51:49 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2013-04-08 16:16:06 +03:00
|
|
|
{
|
2013-10-05 22:16:55 +03:00
|
|
|
InterleaveContext *s = ctx->priv;
|
2013-04-08 16:16:06 +03:00
|
|
|
const AVFilterPad *outpad = &ctx->filter->outputs[0];
|
2017-08-25 09:53:46 +02:00
|
|
|
int i, ret;
|
2013-04-08 16:16:06 +03:00
|
|
|
|
2013-10-05 22:16:55 +03:00
|
|
|
for (i = 0; i < s->nb_inputs; i++) {
|
2013-04-08 16:16:06 +03:00
|
|
|
AVFilterPad inpad = { 0 };
|
|
|
|
|
|
|
|
inpad.name = av_asprintf("input%d", i);
|
|
|
|
if (!inpad.name)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
inpad.type = outpad->type;
|
|
|
|
|
|
|
|
switch (outpad->type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
inpad.get_video_buffer = ff_null_get_video_buffer; break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
|
|
|
|
default:
|
|
|
|
av_assert0(0);
|
|
|
|
}
|
2017-08-25 09:53:46 +02:00
|
|
|
if ((ret = ff_insert_inpad(ctx, i, &inpad)) < 0) {
|
|
|
|
av_freep(&inpad.name);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-04-08 16:16:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-06 17:55:06 +03:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
2013-04-08 16:16:06 +03:00
|
|
|
{
|
2019-05-03 20:17:28 +02:00
|
|
|
for (int i = 0; i < ctx->nb_inputs; i++)
|
2013-04-08 16:16:06 +03:00
|
|
|
av_freep(&ctx->input_pads[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AVFilterLink *inlink0 = ctx->inputs[0];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (outlink->type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
outlink->time_base = AV_TIME_BASE_Q;
|
|
|
|
outlink->w = inlink0->w;
|
|
|
|
outlink->h = inlink0->h;
|
|
|
|
outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
|
|
|
|
outlink->format = inlink0->format;
|
|
|
|
outlink->frame_rate = (AVRational) {1, 0};
|
|
|
|
for (i = 1; i < ctx->nb_inputs; i++) {
|
|
|
|
AVFilterLink *inlink = ctx->inputs[i];
|
|
|
|
|
|
|
|
if (outlink->w != inlink->w ||
|
|
|
|
outlink->h != inlink->h ||
|
|
|
|
outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
|
|
|
|
outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
|
|
|
|
"(size %dx%d, SAR %d:%d) do not match the corresponding "
|
|
|
|
"output link parameters (%dx%d, SAR %d:%d)\n",
|
|
|
|
ctx->input_pads[i].name, inlink->w, inlink->h,
|
|
|
|
inlink->sample_aspect_ratio.num,
|
|
|
|
inlink->sample_aspect_ratio.den,
|
|
|
|
outlink->w, outlink->h,
|
|
|
|
outlink->sample_aspect_ratio.num,
|
|
|
|
outlink->sample_aspect_ratio.den);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_INTERLEAVE_FILTER
|
|
|
|
|
|
|
|
DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
|
|
|
|
AVFILTER_DEFINE_CLASS(interleave);
|
|
|
|
|
|
|
|
static const AVFilterPad interleave_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_vf_interleave = {
|
2013-04-08 16:16:06 +03:00
|
|
|
.name = "interleave",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
|
|
|
|
.priv_size = sizeof(InterleaveContext),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2019-05-03 20:17:28 +02:00
|
|
|
.activate = activate,
|
2013-04-08 16:16:06 +03:00
|
|
|
.outputs = interleave_outputs,
|
|
|
|
.priv_class = &interleave_class,
|
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_AINTERLEAVE_FILTER
|
|
|
|
|
|
|
|
DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
|
|
|
|
AVFILTER_DEFINE_CLASS(ainterleave);
|
|
|
|
|
|
|
|
static const AVFilterPad ainterleave_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_af_ainterleave = {
|
2013-04-08 16:16:06 +03:00
|
|
|
.name = "ainterleave",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
|
|
|
|
.priv_size = sizeof(InterleaveContext),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2019-05-03 20:17:28 +02:00
|
|
|
.activate = activate,
|
2013-04-08 16:16:06 +03:00
|
|
|
.outputs = ainterleave_outputs,
|
|
|
|
.priv_class = &ainterleave_class,
|
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|