}
}
-static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
+static int try_start_frame(AVFilterContext *ctx, AVFilterBufferRef *mainpic)
{
- AVFilterContext *ctx = inlink->dst;
+ OverlayContext *over = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
- AVFilterBufferRef *outpicref = inlink->cur_buf;
+ AVFilterBufferRef *next_overpic, *outpicref;
+ int ret;
+
+ /* Discard obsolete overlay frames: if there is a next frame with pts is
+ * before the main frame, we can drop the current overlay. */
+ while (1) {
+ next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
- if (!next_overpic || next_overpic->pts > mainpic->pts)
++ if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
++ mainpic->pts , ctx->inputs[MAIN]->time_base) > 0)
+ break;
+ ff_bufqueue_get(&over->queue_over);
+ avfilter_unref_buffer(over->overpicref);
+ over->overpicref = next_overpic;
+ }
+ /* If there is no next frame and no EOF and the overlay frame is before
+ * the main frame, we can not know yet if it will be superseded. */
+ if (!over->queue_over.available && !over->overlay_eof &&
- (!over->overpicref || over->overpicref->pts < mainpic->pts))
++ (!over->overpicref || av_compare_ts(over->overpicref->pts, ctx->inputs[OVERLAY]->time_base,
++ mainpic->pts , ctx->inputs[MAIN]->time_base) < 0))
+ return AVERROR(EAGAIN);
+ /* At this point, we know that the current overlay frame extends to the
+ * time of the main frame. */
+ outlink->out_buf = outpicref = avfilter_ref_buffer(mainpic, ~0);
+
+ av_dlog(ctx, "main_pts:%s main_pts_time:%s",
+ av_ts2str(outpicref->pts), av_ts2timestr(outpicref->pts, &outlink->time_base));
+ if (over->overpicref)
+ av_dlog(ctx, " over_pts:%s over_pts_time:%s",
+ av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
+ av_dlog(ctx, "\n");
+
+ ret = ff_start_frame(ctx->outputs[0], avfilter_ref_buffer(outpicref, ~0));
+ over->frame_requested = 0;
+ return ret;
+}
+
+static int try_start_next_frame(AVFilterContext *ctx)
+{
OverlayContext *over = ctx->priv;
- inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[MAIN]->time_base,
- ctx->outputs[0]->time_base);
+ AVFilterBufferRef *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
+ int ret;
+
+ if (!next_mainpic)
+ return AVERROR(EAGAIN);
+ if ((ret = try_start_frame(ctx, next_mainpic)) == AVERROR(EAGAIN))
+ return ret;
+ avfilter_unref_buffer(ff_bufqueue_get(&over->queue_main));
+ return ret;
+}
+
+static int try_push_frame(AVFilterContext *ctx)
+{
+ OverlayContext *over = ctx->priv;
+ AVFilterLink *outlink = ctx->outputs[0];
+ AVFilterBufferRef *outpicref;
+ int ret;
+
+ if ((ret = try_start_next_frame(ctx)) < 0)
+ return ret;
+ outpicref = outlink->out_buf;
+ if (over->overpicref)
+ blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
+ over->overpicref->video->w, over->overpicref->video->h,
+ 0, outpicref->video->w, outpicref->video->h);
+ if ((ret = ff_draw_slice(outlink, 0, outpicref->video->h, +1)) < 0 ||
+ (ret = ff_end_frame(outlink)) < 0)
+ return ret;
+ return 0;
+}
+
+static int flush_frames(AVFilterContext *ctx)
+{
+ int ret;
+
+ while (!(ret = try_push_frame(ctx)));
+ return ret == AVERROR(EAGAIN) ? 0 : ret;
+}
+
+static int start_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+{
+ AVFilterContext *ctx = inlink->dst;
+ OverlayContext *over = ctx->priv;
+ int ret;
+
+ if ((ret = flush_frames(ctx)) < 0)
+ return ret;
+ if ((ret = try_start_frame(ctx, inpicref)) < 0) {
+ if (ret != AVERROR(EAGAIN))
+ return ret;
+ ff_bufqueue_add(ctx, &over->queue_main, inpicref);
+ av_assert1(inpicref == inlink->cur_buf);
+ inlink->cur_buf = NULL;
+ }
+ return 0;
+}
+static int draw_slice_main(AVFilterLink *inlink, int y, int h, int slice_dir)
+{
+ AVFilterContext *ctx = inlink->dst;
+ OverlayContext *over = ctx->priv;
+ AVFilterLink *outlink = ctx->outputs[0];
+ AVFilterBufferRef *outpicref = outlink->out_buf;
+
+ if (!outpicref)
+ return 0;
if (over->overpicref &&
- !(over->x >= outpicref->video->w || over->y >= outpicref->video->h ||
- y+h < over->y || y >= over->y + over->overpicref->video->h)) {
+ y + h > over->y && y < over->y + over->overpicref->video->h) {
blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
over->overpicref->video->w, over->overpicref->video->h,
y, outpicref->video->w, h);
return 0;
}
-static int null_end_frame(AVFilterLink *inlink)
+static int end_frame_over(AVFilterLink *inlink)
{
- return 0;
+ AVFilterContext *ctx = inlink->dst;
+ OverlayContext *over = ctx->priv;
+ AVFilterBufferRef *inpicref = inlink->cur_buf;
+ int ret;
+
+ inlink->cur_buf = NULL;
+
+ if ((ret = flush_frames(ctx)) < 0)
+ return ret;
- inpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
- ctx->outputs[0]->time_base);
+ ff_bufqueue_add(ctx, &over->queue_over, inpicref);
+ ret = try_push_frame(ctx);
+ return ret == AVERROR(EAGAIN) ? 0 : ret;
}
-static int poll_frame(AVFilterLink *link)
+static int request_frame(AVFilterLink *outlink)
{
- AVFilterContext *s = link->src;
- OverlayContext *over = s->priv;
- int ret = ff_poll_frame(s->inputs[OVERLAY]);
-
- if (ret == AVERROR_EOF)
- ret = !!over->overpicref;
+ AVFilterContext *ctx = outlink->src;
+ OverlayContext *over = ctx->priv;
+ int input, ret;
+
+ if (!try_push_frame(ctx))
+ return 0;
+ over->frame_requested = 1;
+ while (over->frame_requested) {
+ /* TODO if we had a frame duration, we could guess more accurately */
+ input = !over->overlay_eof && (over->queue_main.available ||
+ over->queue_over.available < 2) ?
+ OVERLAY : MAIN;
+ ret = ff_request_frame(ctx->inputs[input]);
+ /* EOF on main is reported immediately */
+ if (ret == AVERROR_EOF && input == OVERLAY) {
+ over->overlay_eof = 1;
+ if ((ret = try_start_next_frame(ctx)) != AVERROR(EAGAIN))
+ return ret;
+ ret = 0; /* continue requesting frames on main */
+ }
+ if (ret < 0)
+ return ret;
+ }
+ return 0;
+}
- return ret && ff_poll_frame(s->inputs[MAIN]);
+static int null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
+{
+ return 0;
}
AVFilter avfilter_vf_overlay = {