-static int return_frame(AVFilterContext *ctx, int is_second)
-{
- BWDIFContext *bwdif = ctx->priv;
- AVFilterLink *link = ctx->outputs[0];
- int tff, ret;
-
- if (bwdif->parity == -1) {
- tff = bwdif->cur->interlaced_frame ?
- bwdif->cur->top_field_first : 1;
- } else {
- tff = bwdif->parity ^ 1;
- }
-
- if (is_second) {
- bwdif->out = ff_get_video_buffer(link, link->w, link->h);
- if (!bwdif->out)
- return AVERROR(ENOMEM);
-
- av_frame_copy_props(bwdif->out, bwdif->cur);
- bwdif->out->interlaced_frame = 0;
- if (bwdif->inter_field < 0)
- bwdif->inter_field = 0;
- }
-
- filter(ctx, bwdif->out, tff ^ !is_second, tff);
-
- if (is_second) {
- int64_t cur_pts = bwdif->cur->pts;
- int64_t next_pts = bwdif->next->pts;
-
- if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
- bwdif->out->pts = cur_pts + next_pts;
- } else {
- bwdif->out->pts = AV_NOPTS_VALUE;
- }
- }
- ret = ff_filter_frame(ctx->outputs[0], bwdif->out);
-
- bwdif->frame_pending = (bwdif->mode&1) && !is_second;
- return ret;
-}
-
-static int checkstride(BWDIFContext *bwdif, const AVFrame *a, const AVFrame *b)
-{
- int i;
- for (i = 0; i < bwdif->csp->nb_components; i++)
- if (a->linesize[i] != b->linesize[i])
- return 1;
- return 0;
-}
-
-static void fixstride(AVFilterLink *link, AVFrame *f)
-{
- AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
- if(!dst)
- return;
- av_frame_copy_props(dst, f);
- av_image_copy(dst->data, dst->linesize,
- (const uint8_t **)f->data, f->linesize,
- dst->format, dst->width, dst->height);
- av_frame_unref(f);
- av_frame_move_ref(f, dst);
- av_frame_free(&dst);
-}
-
-static int filter_frame(AVFilterLink *link, AVFrame *frame)
-{
- AVFilterContext *ctx = link->dst;
- BWDIFContext *bwdif = ctx->priv;
-
- av_assert0(frame);
-
- if (bwdif->frame_pending)
- return_frame(ctx, 1);
-
- if (bwdif->prev)
- av_frame_free(&bwdif->prev);
- bwdif->prev = bwdif->cur;
- bwdif->cur = bwdif->next;
- bwdif->next = frame;
-
- if (!bwdif->cur) {
- bwdif->cur = av_frame_clone(bwdif->next);
- if (!bwdif->cur)
- return AVERROR(ENOMEM);
- bwdif->inter_field = 0;
- }
-
- if (checkstride(bwdif, bwdif->next, bwdif->cur)) {
- av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
- fixstride(link, bwdif->next);
- }
- if (checkstride(bwdif, bwdif->next, bwdif->cur))
- fixstride(link, bwdif->cur);
- if (bwdif->prev && checkstride(bwdif, bwdif->next, bwdif->prev))
- fixstride(link, bwdif->prev);
- if (checkstride(bwdif, bwdif->next, bwdif->cur) || (bwdif->prev && checkstride(bwdif, bwdif->next, bwdif->prev))) {
- av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
- return -1;
- }
-
- if (!bwdif->prev)
- return 0;
-
- if ((bwdif->deint && !bwdif->cur->interlaced_frame) ||
- ctx->is_disabled ||
- (bwdif->deint && !bwdif->prev->interlaced_frame && bwdif->prev->repeat_pict) ||
- (bwdif->deint && !bwdif->next->interlaced_frame && bwdif->next->repeat_pict)
- ) {
- bwdif->out = av_frame_clone(bwdif->cur);
- if (!bwdif->out)
- return AVERROR(ENOMEM);
-
- av_frame_free(&bwdif->prev);
- if (bwdif->out->pts != AV_NOPTS_VALUE)
- bwdif->out->pts *= 2;
- return ff_filter_frame(ctx->outputs[0], bwdif->out);
- }
-
- bwdif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
- if (!bwdif->out)
- return AVERROR(ENOMEM);
-
- av_frame_copy_props(bwdif->out, bwdif->cur);
- bwdif->out->interlaced_frame = 0;
-
- if (bwdif->out->pts != AV_NOPTS_VALUE)
- bwdif->out->pts *= 2;
-
- return return_frame(ctx, 0);
-}
-
-static int request_frame(AVFilterLink *link)
-{
- AVFilterContext *ctx = link->src;
- BWDIFContext *bwdif = ctx->priv;
- int ret;
-
- if (bwdif->frame_pending) {
- return_frame(ctx, 1);
- return 0;
- }
-
- if (bwdif->eof)
- return AVERROR_EOF;
-
- ret = ff_request_frame(link->src->inputs[0]);
-
- if (ret == AVERROR_EOF && bwdif->cur) {
- AVFrame *next = av_frame_clone(bwdif->next);
-
- if (!next)
- return AVERROR(ENOMEM);
-
- bwdif->inter_field = -1;
- next->pts = bwdif->next->pts * 2 - bwdif->cur->pts;
-
- filter_frame(link->src->inputs[0], next);
- bwdif->eof = 1;
- } else if (ret < 0) {
- return ret;
- }
-
- return 0;
-}
-