[FFmpeg-devel] [PATCH 2/2] avfilter/f_interleave: reimplement on top of framesync

Paul B Mahol onemda at gmail.com
Wed Oct 9 20:23:19 CEST 2013


On 10/9/13, Nicolas George <george at nsup.org> wrote:
> Le quartidi 14 vendemiaire, an CCXXII, Paul B Mahol a ecrit :
>> Signed-off-by: Paul B Mahol <onemda at gmail.com>
>> ---
>>  libavfilter/Makefile       |   4 +-
>>  libavfilter/f_interleave.c | 109
>> +++++++++++++++++----------------------------
>>  2 files changed, 44 insertions(+), 69 deletions(-)
>>
>> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
>> index b2d3587..5a84c81 100644
>> --- a/libavfilter/Makefile
>> +++ b/libavfilter/Makefile
>> @@ -56,7 +56,7 @@ OBJS-$(CONFIG_ADELAY_FILTER)                 +=
>> af_adelay.o
>>  OBJS-$(CONFIG_AECHO_FILTER)                  += af_aecho.o
>>  OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
>>  OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
>> -OBJS-$(CONFIG_AINTERLEAVE_FILTER)            += f_interleave.o
>> +OBJS-$(CONFIG_AINTERLEAVE_FILTER)            += f_interleave.o
>> framesync.o
>>  OBJS-$(CONFIG_ALLPASS_FILTER)                += af_biquads.o
>>  OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
>>  OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
>> @@ -150,7 +150,7 @@ OBJS-$(CONFIG_HUE_FILTER)                    +=
>> vf_hue.o
>>  OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
>>  OBJS-$(CONFIG_IL_FILTER)                     += vf_il.o
>>  OBJS-$(CONFIG_INTERLACE_FILTER)              += vf_interlace.o
>> -OBJS-$(CONFIG_INTERLEAVE_FILTER)             += f_interleave.o
>> +OBJS-$(CONFIG_INTERLEAVE_FILTER)             += f_interleave.o
>> framesync.o
>>  OBJS-$(CONFIG_KERNDEINT_FILTER)              += vf_kerndeint.o
>>  OBJS-$(CONFIG_LUT3D_FILTER)                  += vf_lut3d.o
>>  OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
>> diff --git a/libavfilter/f_interleave.c b/libavfilter/f_interleave.c
>> index 72050db..a107802 100644
>> --- a/libavfilter/f_interleave.c
>> +++ b/libavfilter/f_interleave.c
>> @@ -27,8 +27,8 @@
>>  #include "libavutil/avstring.h"
>>  #include "libavutil/opt.h"
>>  #include "avfilter.h"
>> -#include "bufferqueue.h"
>>  #include "formats.h"
>> +#include "framesync.h"
>>  #include "internal.h"
>>  #include "audio.h"
>>  #include "video.h"
>> @@ -36,7 +36,8 @@
>>  typedef struct {
>>      const AVClass *class;
>>      int nb_inputs;
>> -    struct FFBufQueue *queues;
>> +
>> +    FFFrameSync fs;
>>  } InterleaveContext;
>>
>>  #define OFFSET(x) offsetof(InterleaveContext, x)
>> @@ -48,58 +49,30 @@ static const AVOption filt_name##_options[] = {
>>              \
>>     { NULL }                                                         \
>>  }
>>
>> -inline static int push_frame(AVFilterContext *ctx)
>
>> +inline static int push_frame(FFFrameSync *fs)
>
> I suspect the "inline" should go away.
>
>>  {
>> +    AVFilterContext *ctx = fs->parent;
>> +    AVFilterLink *outlink = ctx->outputs[0];
>>      InterleaveContext *s = ctx->priv;
>>      AVFrame *frame;
>> -    int i, queue_idx = -1;
>> -    int64_t pts_min = INT64_MAX;
>> +    int i, ret;
>>
>> -    /* look for oldest frame */
>> -    for (i = 0; i < ctx->nb_inputs; i++) {
>> -        struct FFBufQueue *q = &s->queues[i];
>> -
>> -        if (!q->available && !ctx->inputs[i]->closed)
>> -            return 0;
>> -        if (q->available) {
>> -            frame = ff_bufqueue_peek(q, 0);
>> -            if (frame->pts < pts_min) {
>> -                pts_min = frame->pts;
>> -                queue_idx = i;
>> -            }
>> -        }
>> -    }
>
>> +    for (i = 0; i < s->nb_inputs; i++) {
>> +        if ((ret = ff_framesync_get_frame(fs, i, &frame, 1)) < 0)
>> +            return ret;
>>
>> -    /* all inputs are closed */
>> -    if (queue_idx < 0)
>> -        return AVERROR_EOF;
>> +        frame->pts = av_rescale_q(fs->pts, fs->time_base,
>> outlink->time_base);
>> +        if ((ret = ff_filter_frame(outlink, frame)) < 0)
>> +            return ret;
>> +    }
>
> I wonder about the logic here: it seems to me you my send each frame
> multiple times for each sync event. Did you test this with inputs at
> different frame rates?

I can not use current state of filter to do anything useful. So it
is really hard to find if something is broken.

>
>>
>> -    frame = ff_bufqueue_get(&s->queues[queue_idx]);
>> -    av_log(ctx, AV_LOG_DEBUG, "queue:%d -> frame time:%f\n",
>> -           queue_idx, frame->pts * av_q2d(AV_TIME_BASE_Q));
>> -    return ff_filter_frame(ctx->outputs[0], frame);
>> +    return 0;
>>  }
>>
>>  static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
>>  {
>> -    AVFilterContext *ctx = inlink->dst;
>> -    InterleaveContext *s = ctx->priv;
>> -    unsigned in_no = FF_INLINK_IDX(inlink);
>> -
>> -    if (frame->pts == AV_NOPTS_VALUE) {
>> -        av_log(ctx, AV_LOG_WARNING,
>> -               "NOPTS value for input frame cannot be accepted, frame
>> discarded\n");
>> -        av_frame_free(&frame);
>> -        return AVERROR_INVALIDDATA;
>> -    }
>> -
>> -    /* queue frame */
>> -    frame->pts = av_rescale_q(frame->pts, inlink->time_base,
>> AV_TIME_BASE_Q);
>> -    av_log(ctx, AV_LOG_DEBUG, "frame pts:%f -> queue idx:%d
>> available:%d\n",
>> -           frame->pts * av_q2d(AV_TIME_BASE_Q), in_no,
>> s->queues[in_no].available);
>> -    ff_bufqueue_add(ctx, &s->queues[in_no], frame);
>> -
>> -    return push_frame(ctx);
>> +    InterleaveContext *s = inlink->dst->priv;
>> +    return ff_framesync_filter_frame(&s->fs, inlink, frame);
>>  }
>>
>>  static av_cold int init(AVFilterContext *ctx)
>> @@ -108,10 +81,6 @@ static av_cold int init(AVFilterContext *ctx)
>>      const AVFilterPad *outpad = &ctx->filter->outputs[0];
>>      int i;
>>
>> -    s->queues = av_calloc(s->nb_inputs, sizeof(s->queues[0]));
>> -    if (!s->queues)
>> -        return AVERROR(ENOMEM);
>> -
>>      for (i = 0; i < s->nb_inputs; i++) {
>>          AVFilterPad inpad = { 0 };
>>
>> @@ -140,18 +109,26 @@ static av_cold void uninit(AVFilterContext *ctx)
>>      InterleaveContext *s = ctx->priv;
>>      int i;
>>
>> -    for (i = 0; i < ctx->nb_inputs; i++) {
>> -        ff_bufqueue_discard_all(&s->queues[i]);
>> -        av_freep(&s->queues[i]);
>> +    ff_framesync_uninit(&s->fs);
>> +
>> +    for (i = 0; i < ctx->nb_inputs; i++)
>>          av_freep(&ctx->input_pads[i].name);
>> -    }
>>  }
>>
>>  static int config_output(AVFilterLink *outlink)
>>  {
>>      AVFilterContext *ctx = outlink->src;
>> +    InterleaveContext *s = ctx->priv;
>>      AVFilterLink *inlink0 = ctx->inputs[0];
>> -    int i;
>> +    FFFrameSyncIn *in;
>> +    int ret, i;
>> +
>> +    if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
>> +        return ret;
>> +
>> +    in = s->fs.in;
>> +    s->fs.opaque = s;
>> +    s->fs.on_event = push_frame;
>>
>>      if (outlink->type == AVMEDIA_TYPE_VIDEO) {
>>          outlink->time_base           = AV_TIME_BASE_Q;
>> @@ -181,25 +158,23 @@ static int config_output(AVFilterLink *outlink)
>>          }
>>      }
>>
>> +    for (i = 0; i < ctx->nb_inputs; i++) {
>> +        AVFilterLink *inlink = ctx->inputs[i];
>> +
>> +        in[i].time_base = inlink->time_base;
>> +        in[i].sync   = 1;
>> +        in[i].before = EXT_STOP;
>> +        in[i].after  = EXT_STOP;
>> +    }
>> +
>>      outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
>> -    return 0;
>> +    return ff_framesync_configure(&s->fs);
>>  }
>>
>>  static int request_frame(AVFilterLink *outlink)
>>  {
>> -    AVFilterContext *ctx = outlink->src;
>> -    InterleaveContext *s = ctx->priv;
>> -    int i, ret;
>> -
>> -    for (i = 0; i < ctx->nb_inputs; i++) {
>> -        if (!s->queues[i].available && !ctx->inputs[i]->closed) {
>> -            ret = ff_request_frame(ctx->inputs[i]);
>> -            if (ret != AVERROR_EOF)
>> -                return ret;
>> -        }
>> -    }
>> -
>> -    return push_frame(ctx);
>> +    InterleaveContext *s = outlink->src->priv;
>> +    return ff_framesync_request_frame(&s->fs, outlink);
>>  }
>>
>>  #if CONFIG_INTERLEAVE_FILTER
>
> Regards,
>
> --
>   Nicolas George
>


More information about the ffmpeg-devel mailing list