Ticket #10253: yadif_common.c

File yadif_common.c, 7.3 KB (added by Christian Zillner, 4 years ago)

suggested fix for that issue

Line 
1/*
2 * Copyright (C) 2006-2011 Michael Niedermayer <michaelni@gmx.at>
3 * 2010 James Darnley <james.darnley@gmail.com>
4
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavutil/avassert.h"
23#include "libavutil/imgutils.h"
24#include "internal.h"
25#include "yadif.h"
26
27static int return_frame(AVFilterContext *ctx, int is_second)
28{
29 YADIFContext *yadif = ctx->priv;
30 AVFilterLink *link = ctx->outputs[0];
31 int tff, ret;
32
33 if (yadif->parity == -1) {
34 tff = yadif->cur->interlaced_frame ?
35 yadif->cur->top_field_first : 1;
36 } else {
37 tff = yadif->parity ^ 1;
38 }
39
40 if (is_second) {
41 yadif->out = ff_get_video_buffer(link, link->w, link->h);
42 if (!yadif->out)
43 return AVERROR(ENOMEM);
44
45 av_frame_copy_props(yadif->out, yadif->cur);
46 yadif->out->interlaced_frame = 0;
47 if (yadif->current_field == YADIF_FIELD_BACK_END)
48 yadif->current_field = YADIF_FIELD_END;
49 }
50
51 yadif->filter(ctx, yadif->out, tff ^ !is_second, tff);
52
53 if (is_second) {
54 int64_t cur_pts = yadif->cur->pts;
55 int64_t next_pts = yadif->next->pts;
56
57 if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) {
58 yadif->out->pts = cur_pts + next_pts;
59 if (next_pts < cur_pts)
60 yadif->out->pts &= ((1ll << 33) - 1);
61 } else {
62 yadif->out->pts = AV_NOPTS_VALUE;
63 }
64 }
65 ret = ff_filter_frame(ctx->outputs[0], yadif->out);
66
67 yadif->frame_pending = (yadif->mode&1) && !is_second;
68 return ret;
69}
70
71static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b)
72{
73 int i;
74 for (i = 0; i < yadif->csp->nb_components; i++)
75 if (a->linesize[i] != b->linesize[i])
76 return 1;
77 return 0;
78}
79
80static void fixstride(AVFilterLink *link, AVFrame *f)
81{
82 AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height);
83 if(!dst)
84 return;
85 av_frame_copy_props(dst, f);
86 av_image_copy(dst->data, dst->linesize,
87 (const uint8_t **)f->data, f->linesize,
88 dst->format, dst->width, dst->height);
89 av_frame_unref(f);
90 av_frame_move_ref(f, dst);
91 av_frame_free(&dst);
92}
93
94int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
95{
96 AVFilterContext *ctx = link->dst;
97 YADIFContext *yadif = ctx->priv;
98
99 av_assert0(frame);
100
101 if (yadif->frame_pending)
102 return_frame(ctx, 1);
103
104 if (yadif->prev)
105 av_frame_free(&yadif->prev);
106 yadif->prev = yadif->cur;
107 yadif->cur = yadif->next;
108 yadif->next = frame;
109
110 if (!yadif->cur) {
111 yadif->cur = av_frame_clone(yadif->next);
112 if (!yadif->cur)
113 return AVERROR(ENOMEM);
114 yadif->current_field = YADIF_FIELD_END;
115 }
116
117 if (checkstride(yadif, yadif->next, yadif->cur)) {
118 av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n");
119 fixstride(link, yadif->next);
120 }
121 if (checkstride(yadif, yadif->next, yadif->cur))
122 fixstride(link, yadif->cur);
123 if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))
124 fixstride(link, yadif->prev);
125 if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) {
126 av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n");
127 return -1;
128 }
129
130 if (!yadif->prev)
131 return 0;
132
133 if ((yadif->deint && !yadif->cur->interlaced_frame) ||
134 ctx->is_disabled ||
135 (yadif->deint && !yadif->prev->interlaced_frame && yadif->prev->repeat_pict) ||
136 (yadif->deint && !yadif->next->interlaced_frame && yadif->next->repeat_pict)
137 ) {
138 yadif->out = av_frame_clone(yadif->cur);
139 if (!yadif->out)
140 return AVERROR(ENOMEM);
141
142 av_frame_free(&yadif->prev);
143 if (yadif->out->pts != AV_NOPTS_VALUE)
144 yadif->out->pts *= 2;
145 return ff_filter_frame(ctx->outputs[0], yadif->out);
146 }
147
148 yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
149 if (!yadif->out)
150 return AVERROR(ENOMEM);
151
152 av_frame_copy_props(yadif->out, yadif->cur);
153 yadif->out->interlaced_frame = 0;
154
155 if (yadif->out->pts != AV_NOPTS_VALUE)
156 yadif->out->pts *= 2;
157
158 return return_frame(ctx, 0);
159}
160
161int ff_yadif_request_frame(AVFilterLink *link)
162{
163 AVFilterContext *ctx = link->src;
164 YADIFContext *yadif = ctx->priv;
165 int ret;
166
167 if (yadif->frame_pending) {
168 return_frame(ctx, 1);
169 return 0;
170 }
171
172 if (yadif->eof)
173 return AVERROR_EOF;
174
175 ret = ff_request_frame(ctx->inputs[0]);
176
177 if (ret == AVERROR_EOF && yadif->cur) {
178 AVFrame *next = av_frame_clone(yadif->next);
179
180 if (!next)
181 return AVERROR(ENOMEM);
182
183 yadif->current_field = YADIF_FIELD_BACK_END;
184 next->pts = yadif->next->pts * 2 - yadif->cur->pts;
185
186 ff_yadif_filter_frame(ctx->inputs[0], next);
187 yadif->eof = 1;
188 } else if (ret < 0) {
189 return ret;
190 }
191
192 return 0;
193}
194
195#define OFFSET(x) offsetof(YADIFContext, x)
196#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
197
198#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
199
200const AVOption ff_yadif_options[] = {
201 { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
202 CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"),
203 CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"),
204 CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
205 CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
206
207 { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
208 CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"),
209 CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"),
210 CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"),
211
212 { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
213 CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"),
214 CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"),
215
216 { NULL }
217};