avfilter/af_amix: switch to activate
Really fixes hangs and infinite loops. Signed-off-by: Paul B Mahol <onemda@gmail.com>
This commit is contained in:
parent
473e18fdba
commit
15e9c4afdc
@ -41,6 +41,7 @@
|
|||||||
|
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "avfilter.h"
|
#include "avfilter.h"
|
||||||
|
#include "filters.h"
|
||||||
#include "formats.h"
|
#include "formats.h"
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
|
||||||
@ -151,6 +152,7 @@ static int frame_list_add_frame(FrameList *frame_list, int nb_samples, int64_t p
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* FIXME: use directly links fifo */
|
||||||
|
|
||||||
typedef struct MixContext {
|
typedef struct MixContext {
|
||||||
const AVClass *class; /**< class for AVOptions */
|
const AVClass *class; /**< class for AVOptions */
|
||||||
@ -263,21 +265,15 @@ static int config_output(AVFilterLink *outlink)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int calc_active_inputs(MixContext *s);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read samples from the input FIFOs, mix, and write to the output link.
|
* Read samples from the input FIFOs, mix, and write to the output link.
|
||||||
*/
|
*/
|
||||||
static int output_frame(AVFilterLink *outlink, int need_request)
|
static int output_frame(AVFilterLink *outlink)
|
||||||
{
|
{
|
||||||
AVFilterContext *ctx = outlink->src;
|
AVFilterContext *ctx = outlink->src;
|
||||||
MixContext *s = ctx->priv;
|
MixContext *s = ctx->priv;
|
||||||
AVFrame *out_buf, *in_buf;
|
AVFrame *out_buf, *in_buf;
|
||||||
int nb_samples, ns, ret, i;
|
int nb_samples, ns, i;
|
||||||
|
|
||||||
ret = calc_active_inputs(s);
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
if (s->input_state[0] & INPUT_ON) {
|
if (s->input_state[0] & INPUT_ON) {
|
||||||
/* first input live: use the corresponding frame size */
|
/* first input live: use the corresponding frame size */
|
||||||
@ -288,7 +284,7 @@ static int output_frame(AVFilterLink *outlink, int need_request)
|
|||||||
if (ns < nb_samples) {
|
if (ns < nb_samples) {
|
||||||
if (!(s->input_state[i] & INPUT_EOF))
|
if (!(s->input_state[i] & INPUT_EOF))
|
||||||
/* unclosed input with not enough samples */
|
/* unclosed input with not enough samples */
|
||||||
return need_request ? ff_request_frame(ctx->inputs[i]) : 0;
|
return 0;
|
||||||
/* closed input to drain */
|
/* closed input to drain */
|
||||||
nb_samples = ns;
|
nb_samples = ns;
|
||||||
}
|
}
|
||||||
@ -303,8 +299,10 @@ static int output_frame(AVFilterLink *outlink, int need_request)
|
|||||||
nb_samples = FFMIN(nb_samples, ns);
|
nb_samples = FFMIN(nb_samples, ns);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nb_samples == INT_MAX)
|
if (nb_samples == INT_MAX) {
|
||||||
return AVERROR_EOF;
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s->next_pts = frame_list_next_pts(s->frame_list);
|
s->next_pts = frame_list_next_pts(s->frame_list);
|
||||||
@ -367,27 +365,18 @@ static int output_frame(AVFilterLink *outlink, int need_request)
|
|||||||
static int request_samples(AVFilterContext *ctx, int min_samples)
|
static int request_samples(AVFilterContext *ctx, int min_samples)
|
||||||
{
|
{
|
||||||
MixContext *s = ctx->priv;
|
MixContext *s = ctx->priv;
|
||||||
int i, ret;
|
int i;
|
||||||
|
|
||||||
av_assert0(s->nb_inputs > 1);
|
av_assert0(s->nb_inputs > 1);
|
||||||
|
|
||||||
for (i = 1; i < s->nb_inputs; i++) {
|
for (i = 1; i < s->nb_inputs; i++) {
|
||||||
ret = 0;
|
|
||||||
if (!(s->input_state[i] & INPUT_ON))
|
if (!(s->input_state[i] & INPUT_ON))
|
||||||
continue;
|
continue;
|
||||||
if (av_audio_fifo_size(s->fifos[i]) >= min_samples)
|
if (av_audio_fifo_size(s->fifos[i]) >= min_samples)
|
||||||
continue;
|
continue;
|
||||||
ret = ff_request_frame(ctx->inputs[i]);
|
ff_inlink_request_frame(ctx->inputs[i]);
|
||||||
if (ret == AVERROR_EOF) {
|
|
||||||
s->input_state[i] |= INPUT_EOF;
|
|
||||||
if (av_audio_fifo_size(s->fifos[i]) == 0) {
|
|
||||||
s->input_state[i] = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
return output_frame(ctx->outputs[0], 1);
|
return output_frame(ctx->outputs[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -411,73 +400,87 @@ static int calc_active_inputs(MixContext *s)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int request_frame(AVFilterLink *outlink)
|
static int activate(AVFilterContext *ctx)
|
||||||
{
|
{
|
||||||
AVFilterContext *ctx = outlink->src;
|
|
||||||
MixContext *s = ctx->priv;
|
|
||||||
int ret;
|
|
||||||
int wanted_samples;
|
|
||||||
|
|
||||||
ret = calc_active_inputs(s);
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
if (!(s->input_state[0] & INPUT_ON))
|
|
||||||
return request_samples(ctx, 1);
|
|
||||||
|
|
||||||
if (s->frame_list->nb_frames == 0) {
|
|
||||||
ret = ff_request_frame(ctx->inputs[0]);
|
|
||||||
if (ret == AVERROR_EOF) {
|
|
||||||
s->input_state[0] = 0;
|
|
||||||
if (s->nb_inputs == 1)
|
|
||||||
return AVERROR_EOF;
|
|
||||||
return output_frame(ctx->outputs[0], 1);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
av_assert0(s->frame_list->nb_frames > 0);
|
|
||||||
|
|
||||||
wanted_samples = frame_list_next_frame_size(s->frame_list);
|
|
||||||
|
|
||||||
return request_samples(ctx, wanted_samples);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
|
|
||||||
{
|
|
||||||
AVFilterContext *ctx = inlink->dst;
|
|
||||||
MixContext *s = ctx->priv;
|
|
||||||
AVFilterLink *outlink = ctx->outputs[0];
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
int i, ret = 0;
|
MixContext *s = ctx->priv;
|
||||||
|
AVFrame *buf = NULL;
|
||||||
|
int i, ret;
|
||||||
|
|
||||||
for (i = 0; i < ctx->nb_inputs; i++)
|
for (i = 0; i < s->nb_inputs; i++) {
|
||||||
if (ctx->inputs[i] == inlink)
|
AVFilterLink *inlink = ctx->inputs[i];
|
||||||
break;
|
|
||||||
if (i >= ctx->nb_inputs) {
|
if ((ret = ff_inlink_consume_frame(ctx->inputs[i], &buf)) > 0) {
|
||||||
av_log(ctx, AV_LOG_ERROR, "unknown input link\n");
|
if (i == 0) {
|
||||||
ret = AVERROR(EINVAL);
|
int64_t pts = av_rescale_q(buf->pts, inlink->time_base,
|
||||||
goto fail;
|
outlink->time_base);
|
||||||
|
ret = frame_list_add_frame(s->frame_list, buf->nb_samples, pts);
|
||||||
|
if (ret < 0) {
|
||||||
|
av_frame_free(&buf);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = av_audio_fifo_write(s->fifos[i], (void **)buf->extended_data,
|
||||||
|
buf->nb_samples);
|
||||||
|
if (ret < 0) {
|
||||||
|
av_frame_free(&buf);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
av_frame_free(&buf);
|
||||||
|
|
||||||
|
ret = output_frame(outlink);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i == 0) {
|
for (i = 0; i < s->nb_inputs; i++) {
|
||||||
int64_t pts = av_rescale_q(buf->pts, inlink->time_base,
|
int64_t pts;
|
||||||
outlink->time_base);
|
int status;
|
||||||
ret = frame_list_add_frame(s->frame_list, buf->nb_samples, pts);
|
|
||||||
if (ret < 0)
|
if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
|
||||||
goto fail;
|
if (status == AVERROR_EOF) {
|
||||||
|
if (i == 0) {
|
||||||
|
s->input_state[i] = 0;
|
||||||
|
if (s->nb_inputs == 1) {
|
||||||
|
ff_outlink_set_status(outlink, status, pts);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
s->input_state[i] |= INPUT_EOF;
|
||||||
|
if (av_audio_fifo_size(s->fifos[i]) == 0) {
|
||||||
|
s->input_state[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = av_audio_fifo_write(s->fifos[i], (void **)buf->extended_data,
|
if (calc_active_inputs(s)) {
|
||||||
buf->nb_samples);
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
|
||||||
if (ret < 0)
|
return 0;
|
||||||
goto fail;
|
}
|
||||||
|
|
||||||
av_frame_free(&buf);
|
if (ff_outlink_frame_wanted(outlink)) {
|
||||||
return output_frame(outlink, 0);
|
int wanted_samples;
|
||||||
|
|
||||||
fail:
|
if (!(s->input_state[0] & INPUT_ON))
|
||||||
av_frame_free(&buf);
|
return request_samples(ctx, 1);
|
||||||
|
|
||||||
return ret;
|
if (s->frame_list->nb_frames == 0) {
|
||||||
|
ff_inlink_request_frame(ctx->inputs[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
av_assert0(s->frame_list->nb_frames > 0);
|
||||||
|
|
||||||
|
wanted_samples = frame_list_next_frame_size(s->frame_list);
|
||||||
|
|
||||||
|
return request_samples(ctx, wanted_samples);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static av_cold int init(AVFilterContext *ctx)
|
static av_cold int init(AVFilterContext *ctx)
|
||||||
@ -494,7 +497,6 @@ static av_cold int init(AVFilterContext *ctx)
|
|||||||
pad.name = av_strdup(name);
|
pad.name = av_strdup(name);
|
||||||
if (!pad.name)
|
if (!pad.name)
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
pad.filter_frame = filter_frame;
|
|
||||||
|
|
||||||
if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
|
if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
|
||||||
av_freep(&pad.name);
|
av_freep(&pad.name);
|
||||||
@ -562,7 +564,6 @@ static const AVFilterPad avfilter_af_amix_outputs[] = {
|
|||||||
.name = "default",
|
.name = "default",
|
||||||
.type = AVMEDIA_TYPE_AUDIO,
|
.type = AVMEDIA_TYPE_AUDIO,
|
||||||
.config_props = config_output,
|
.config_props = config_output,
|
||||||
.request_frame = request_frame
|
|
||||||
},
|
},
|
||||||
{ NULL }
|
{ NULL }
|
||||||
};
|
};
|
||||||
@ -574,6 +575,7 @@ AVFilter ff_af_amix = {
|
|||||||
.priv_class = &amix_class,
|
.priv_class = &amix_class,
|
||||||
.init = init,
|
.init = init,
|
||||||
.uninit = uninit,
|
.uninit = uninit,
|
||||||
|
.activate = activate,
|
||||||
.query_formats = query_formats,
|
.query_formats = query_formats,
|
||||||
.inputs = NULL,
|
.inputs = NULL,
|
||||||
.outputs = avfilter_af_amix_outputs,
|
.outputs = avfilter_af_amix_outputs,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user