Line | Branch | Exec | Source |
---|---|---|---|
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 "filters.h" | ||
25 | #include "video.h" | ||
26 | #include "yadif.h" | ||
27 | |||
28 | 268 | static int return_frame(AVFilterContext *ctx, int is_second) | |
29 | { | ||
30 | 268 | YADIFContext *yadif = ctx->priv; | |
31 | 268 | AVFilterLink *link = ctx->outputs[0]; | |
32 | int tff, ret; | ||
33 | |||
34 |
1/2✓ Branch 0 taken 268 times.
✗ Branch 1 not taken.
|
268 | if (yadif->parity == -1) { |
35 | 268 | tff = (yadif->cur->flags & AV_FRAME_FLAG_INTERLACED) ? | |
36 |
2/4✓ Branch 0 taken 268 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 268 times.
|
268 | !!(yadif->cur->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) : 1; |
37 | } else { | ||
38 | ✗ | tff = yadif->parity ^ 1; | |
39 | } | ||
40 | |||
41 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 210 times.
|
268 | if (is_second) { |
42 | 58 | yadif->out = ff_get_video_buffer(link, link->w, link->h); | |
43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (!yadif->out) |
44 | ✗ | return AVERROR(ENOMEM); | |
45 | |||
46 | 58 | av_frame_copy_props(yadif->out, yadif->cur); | |
47 | #if FF_API_INTERLACED_FRAME | ||
48 | FF_DISABLE_DEPRECATION_WARNINGS | ||
49 | 58 | yadif->out->interlaced_frame = 0; | |
50 | FF_ENABLE_DEPRECATION_WARNINGS | ||
51 | #endif | ||
52 | 58 | yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (yadif->current_field == YADIF_FIELD_BACK_END) |
54 | ✗ | yadif->current_field = YADIF_FIELD_END; | |
55 | } | ||
56 | |||
57 | 268 | yadif->filter(ctx, yadif->out, tff ^ !is_second, tff); | |
58 | |||
59 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 210 times.
|
268 | if (is_second) { |
60 | 58 | int64_t cur_pts = yadif->cur->pts; | |
61 | 58 | int64_t next_pts = yadif->next->pts; | |
62 | |||
63 |
2/4✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
|
58 | if (next_pts != AV_NOPTS_VALUE && cur_pts != AV_NOPTS_VALUE) { |
64 | 58 | yadif->out->pts = cur_pts + next_pts; | |
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (yadif->pts_multiplier == 1) { |
66 | ✗ | yadif->out->pts >>= 1; | |
67 | ✗ | yadif->out->duration >>= 1; | |
68 | } | ||
69 | } else { | ||
70 | ✗ | yadif->out->pts = AV_NOPTS_VALUE; | |
71 | } | ||
72 | } | ||
73 | |||
74 | 268 | ff_ccfifo_inject(&yadif->cc_fifo, yadif->out); | |
75 | 268 | ret = ff_filter_frame(ctx->outputs[0], yadif->out); | |
76 | |||
77 |
4/4✓ Branch 0 taken 118 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 58 times.
|
268 | yadif->frame_pending = (yadif->mode&1) && !is_second; |
78 | 268 | return ret; | |
79 | } | ||
80 | |||
81 | 1071 | static int checkstride(YADIFContext *yadif, const AVFrame *a, const AVFrame *b) | |
82 | { | ||
83 | int i; | ||
84 |
2/2✓ Branch 0 taken 3213 times.
✓ Branch 1 taken 1071 times.
|
4284 | for (i = 0; i < yadif->csp->nb_components; i++) |
85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3213 times.
|
3213 | if (a->linesize[i] != b->linesize[i]) |
86 | ✗ | return 1; | |
87 | 1071 | return 0; | |
88 | } | ||
89 | |||
90 | ✗ | static void fixstride(AVFilterLink *link, AVFrame *f) | |
91 | { | ||
92 | ✗ | AVFrame *dst = ff_default_get_video_buffer(link, f->width, f->height); | |
93 | ✗ | if(!dst) | |
94 | ✗ | return; | |
95 | ✗ | av_frame_copy_props(dst, f); | |
96 | ✗ | av_image_copy2(dst->data, dst->linesize, | |
97 | ✗ | f->data, f->linesize, | |
98 | ✗ | dst->format, dst->width, dst->height); | |
99 | ✗ | av_frame_unref(f); | |
100 | ✗ | av_frame_move_ref(f, dst); | |
101 | ✗ | av_frame_free(&dst); | |
102 | } | ||
103 | |||
104 | 217 | int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame) | |
105 | { | ||
106 | 217 | AVFilterContext *ctx = link->dst; | |
107 | 217 | YADIFContext *yadif = ctx->priv; | |
108 | |||
109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
|
217 | av_assert0(frame); |
110 | |||
111 | 217 | ff_ccfifo_extract(&yadif->cc_fifo, frame); | |
112 | |||
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 217 times.
|
217 | if (yadif->frame_pending) |
114 | ✗ | return_frame(ctx, 1); | |
115 | |||
116 |
2/2✓ Branch 0 taken 203 times.
✓ Branch 1 taken 14 times.
|
217 | if (yadif->prev) |
117 | 203 | av_frame_free(&yadif->prev); | |
118 | 217 | yadif->prev = yadif->cur; | |
119 | 217 | yadif->cur = yadif->next; | |
120 | 217 | yadif->next = frame; | |
121 | |||
122 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 210 times.
|
217 | if (!yadif->cur) { |
123 | 7 | yadif->cur = av_frame_clone(yadif->next); | |
124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!yadif->cur) |
125 | ✗ | return AVERROR(ENOMEM); | |
126 | 7 | yadif->current_field = YADIF_FIELD_END; | |
127 | } | ||
128 | |||
129 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 217 times.
|
217 | if (checkstride(yadif, yadif->next, yadif->cur)) { |
130 | ✗ | av_log(ctx, AV_LOG_VERBOSE, "Reallocating frame due to differing stride\n"); | |
131 | ✗ | fixstride(link, yadif->next); | |
132 | } | ||
133 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 217 times.
|
217 | if (checkstride(yadif, yadif->next, yadif->cur)) |
134 | ✗ | fixstride(link, yadif->cur); | |
135 |
3/4✓ Branch 0 taken 210 times.
✓ Branch 1 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 210 times.
|
217 | if (yadif->prev && checkstride(yadif, yadif->next, yadif->prev)) |
136 | ✗ | fixstride(link, yadif->prev); | |
137 |
4/6✓ Branch 1 taken 217 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 210 times.
✓ Branch 4 taken 7 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 210 times.
|
217 | if (checkstride(yadif, yadif->next, yadif->cur) || (yadif->prev && checkstride(yadif, yadif->next, yadif->prev))) { |
138 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate frame\n"); | |
139 | ✗ | return -1; | |
140 | } | ||
141 | |||
142 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 210 times.
|
217 | if (!yadif->prev) |
143 | 7 | return 0; | |
144 | |||
145 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
210 | if ((yadif->deint && !(yadif->cur->flags & AV_FRAME_FLAG_INTERLACED)) || |
146 |
1/2✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
|
210 | ctx->is_disabled || |
147 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
210 | (yadif->deint && !(yadif->prev->flags & AV_FRAME_FLAG_INTERLACED) && yadif->prev->repeat_pict) || |
148 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
210 | (yadif->deint && !(yadif->next->flags & AV_FRAME_FLAG_INTERLACED) && yadif->next->repeat_pict) |
149 | ) { | ||
150 | ✗ | yadif->out = av_frame_clone(yadif->cur); | |
151 | ✗ | if (!yadif->out) | |
152 | ✗ | return AVERROR(ENOMEM); | |
153 | |||
154 | ✗ | ff_ccfifo_inject(&yadif->cc_fifo, yadif->out); | |
155 | ✗ | av_frame_free(&yadif->prev); | |
156 | ✗ | if (yadif->out->pts != AV_NOPTS_VALUE) | |
157 | ✗ | yadif->out->pts *= yadif->pts_multiplier; | |
158 | ✗ | yadif->out->duration *= yadif->pts_multiplier; | |
159 | ✗ | return ff_filter_frame(ctx->outputs[0], yadif->out); | |
160 | } | ||
161 | |||
162 | 210 | yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h); | |
163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | if (!yadif->out) |
164 | ✗ | return AVERROR(ENOMEM); | |
165 | |||
166 | 210 | av_frame_copy_props(yadif->out, yadif->cur); | |
167 | #if FF_API_INTERLACED_FRAME | ||
168 | FF_DISABLE_DEPRECATION_WARNINGS | ||
169 | 210 | yadif->out->interlaced_frame = 0; | |
170 | FF_ENABLE_DEPRECATION_WARNINGS | ||
171 | #endif | ||
172 | 210 | yadif->out->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
173 | |||
174 |
1/2✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
|
210 | if (yadif->out->pts != AV_NOPTS_VALUE) |
175 | 210 | yadif->out->pts *= yadif->pts_multiplier; | |
176 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 60 times.
|
210 | if (!(yadif->mode & 1)) |
177 | 150 | yadif->out->duration *= yadif->pts_multiplier; | |
178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | else if (yadif->pts_multiplier == 1) |
179 | ✗ | yadif->out->duration >>= 1; | |
180 | |||
181 | 210 | return return_frame(ctx, 0); | |
182 | } | ||
183 | |||
184 | 268 | int ff_yadif_request_frame(AVFilterLink *link) | |
185 | { | ||
186 | 268 | AVFilterContext *ctx = link->src; | |
187 | 268 | YADIFContext *yadif = ctx->priv; | |
188 | int ret; | ||
189 | |||
190 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 210 times.
|
268 | if (yadif->frame_pending) { |
191 | 58 | return_frame(ctx, 1); | |
192 | 58 | return 0; | |
193 | } | ||
194 | |||
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | if (yadif->eof) |
196 | ✗ | return AVERROR_EOF; | |
197 | |||
198 | 210 | ret = ff_request_frame(ctx->inputs[0]); | |
199 | |||
200 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
210 | if (ret == AVERROR_EOF && yadif->cur) { |
201 | ✗ | AVFrame *next = av_frame_clone(yadif->next); | |
202 | |||
203 | ✗ | if (!next) | |
204 | ✗ | return AVERROR(ENOMEM); | |
205 | |||
206 | ✗ | yadif->current_field = YADIF_FIELD_BACK_END; | |
207 | ✗ | next->pts = yadif->next->pts * 2 - yadif->cur->pts; | |
208 | |||
209 | ✗ | ff_yadif_filter_frame(ctx->inputs[0], next); | |
210 | ✗ | yadif->eof = 1; | |
211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | } else if (ret < 0) { |
212 | ✗ | return ret; | |
213 | } | ||
214 | |||
215 | 210 | return 0; | |
216 | } | ||
217 | |||
218 | 7 | int ff_yadif_config_output_common(AVFilterLink *outlink) | |
219 | { | ||
220 | 7 | AVFilterContext *ctx = outlink->src; | |
221 | 7 | FilterLink *il = ff_filter_link(ctx->inputs[0]); | |
222 | 7 | FilterLink *ol = ff_filter_link(outlink); | |
223 | 7 | YADIFContext *yadif = ctx->priv; | |
224 | 7 | AVRational tb = ctx->inputs[0]->time_base; | |
225 | int ret; | ||
226 | |||
227 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | if (av_reduce(&outlink->time_base.num, &outlink->time_base.den, tb.num, tb.den * 2LL, INT_MAX)) { |
228 | 7 | yadif->pts_multiplier = 2; | |
229 | } else { | ||
230 | ✗ | av_log(ctx, AV_LOG_WARNING, "Cannot use exact output timebase\n"); | |
231 | ✗ | outlink->time_base = tb; | |
232 | ✗ | yadif->pts_multiplier = 1; | |
233 | } | ||
234 | |||
235 | 7 | outlink->w = ctx->inputs[0]->w; | |
236 | 7 | outlink->h = ctx->inputs[0]->h; | |
237 | |||
238 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | if (outlink->w < 3 || outlink->h < 3) { |
239 | ✗ | av_log(ctx, AV_LOG_ERROR, "Video of less than 3 columns or lines is not supported\n"); | |
240 | ✗ | return AVERROR(EINVAL); | |
241 | } | ||
242 | |||
243 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | if(yadif->mode & 1) |
244 | 2 | ol->frame_rate = av_mul_q(il->frame_rate, | |
245 | 2 | (AVRational){2, 1}); | |
246 | else | ||
247 | 5 | ol->frame_rate = il->frame_rate; | |
248 | |||
249 | 7 | ret = ff_ccfifo_init(&yadif->cc_fifo, ol->frame_rate, ctx); | |
250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (ret < 0) { |
251 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n"); | |
252 | ✗ | return ret; | |
253 | } | ||
254 | |||
255 | 7 | return 0; | |
256 | } | ||
257 | |||
258 | 14 | void ff_yadif_uninit(AVFilterContext *ctx) | |
259 | { | ||
260 | 14 | YADIFContext *yadif = ctx->priv; | |
261 | |||
262 | 14 | av_frame_free(&yadif->prev); | |
263 | 14 | av_frame_free(&yadif->cur ); | |
264 | 14 | av_frame_free(&yadif->next); | |
265 | 14 | ff_ccfifo_uninit(&yadif->cc_fifo); | |
266 | 14 | } | |
267 | |||
268 | #define OFFSET(x) offsetof(YADIFContext, x) | ||
269 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
270 | |||
271 | #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, .unit = u } | ||
272 | |||
273 | const AVOption ff_yadif_options[] = { | ||
274 | { "mode", "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, .unit = "mode"}, | ||
275 | CONST("send_frame", "send one frame for each frame", YADIF_MODE_SEND_FRAME, "mode"), | ||
276 | CONST("send_field", "send one frame for each field", YADIF_MODE_SEND_FIELD, "mode"), | ||
277 | CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"), | ||
278 | CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"), | ||
279 | |||
280 | { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, .unit = "parity" }, | ||
281 | CONST("tff", "assume top field first", YADIF_PARITY_TFF, "parity"), | ||
282 | CONST("bff", "assume bottom field first", YADIF_PARITY_BFF, "parity"), | ||
283 | CONST("auto", "auto detect parity", YADIF_PARITY_AUTO, "parity"), | ||
284 | |||
285 | { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, .unit = "deint" }, | ||
286 | CONST("all", "deinterlace all frames", YADIF_DEINT_ALL, "deint"), | ||
287 | CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED, "deint"), | ||
288 | |||
289 | { NULL } | ||
290 | }; | ||
291 |