Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2003 Rich Felker | ||
3 | * | ||
4 | * This file is part of FFmpeg. | ||
5 | * | ||
6 | * FFmpeg is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * FFmpeg is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with FFmpeg; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | #include "libavutil/avassert.h" | ||
22 | #include "libavutil/imgutils.h" | ||
23 | #include "libavutil/mem.h" | ||
24 | #include "libavutil/opt.h" | ||
25 | #include "libavutil/pixdesc.h" | ||
26 | #include "avfilter.h" | ||
27 | #include "filters.h" | ||
28 | #include "video.h" | ||
29 | #include "vf_pullup.h" | ||
30 | |||
31 | #define F_HAVE_BREAKS 1 | ||
32 | #define F_HAVE_AFFINITY 2 | ||
33 | |||
34 | #define BREAK_LEFT 1 | ||
35 | #define BREAK_RIGHT 2 | ||
36 | |||
37 | #define OFFSET(x) offsetof(PullupContext, x) | ||
38 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
39 | |||
40 | static const AVOption pullup_options[] = { | ||
41 | { "jl", "set left junk size", OFFSET(junk_left), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS }, | ||
42 | { "jr", "set right junk size", OFFSET(junk_right), AV_OPT_TYPE_INT, {.i64=1}, 0, INT_MAX, FLAGS }, | ||
43 | { "jt", "set top junk size", OFFSET(junk_top), AV_OPT_TYPE_INT, {.i64=4}, 1, INT_MAX, FLAGS }, | ||
44 | { "jb", "set bottom junk size", OFFSET(junk_bottom), AV_OPT_TYPE_INT, {.i64=4}, 1, INT_MAX, FLAGS }, | ||
45 | { "sb", "set strict breaks", OFFSET(strict_breaks), AV_OPT_TYPE_BOOL,{.i64=0},-1, 1, FLAGS }, | ||
46 | { "mp", "set metric plane", OFFSET(metric_plane), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, .unit = "mp" }, | ||
47 | { "y", "luma", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "mp" }, | ||
48 | { "u", "chroma blue", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "mp" }, | ||
49 | { "v", "chroma red", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, .unit = "mp" }, | ||
50 | { NULL } | ||
51 | }; | ||
52 | |||
53 | AVFILTER_DEFINE_CLASS(pullup); | ||
54 | |||
55 | static const enum AVPixelFormat pix_fmts[] = { | ||
56 | AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, | ||
57 | AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, | ||
58 | AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, | ||
59 | AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, | ||
60 | AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, | ||
61 | AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_GRAY8, | ||
62 | AV_PIX_FMT_NONE | ||
63 | }; | ||
64 | |||
65 | #define ABS(a) (((a) ^ ((a) >> 31)) - ((a) >> 31)) | ||
66 | |||
67 | 1045296 | static int diff_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s) | |
68 | { | ||
69 | 1045296 | int i, j, diff = 0; | |
70 | |||
71 |
2/2✓ Branch 0 taken 4181184 times.
✓ Branch 1 taken 1045296 times.
|
5226480 | for (i = 0; i < 4; i++) { |
72 |
2/2✓ Branch 0 taken 33449472 times.
✓ Branch 1 taken 4181184 times.
|
37630656 | for (j = 0; j < 8; j++) |
73 | 33449472 | diff += ABS(a[j] - b[j]); | |
74 | 4181184 | a += s; | |
75 | 4181184 | b += s; | |
76 | } | ||
77 | |||
78 | 1045296 | return diff; | |
79 | } | ||
80 | |||
81 | 1182384 | static int comb_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s) | |
82 | { | ||
83 | 1182384 | int i, j, comb = 0; | |
84 | |||
85 |
2/2✓ Branch 0 taken 4729536 times.
✓ Branch 1 taken 1182384 times.
|
5911920 | for (i = 0; i < 4; i++) { |
86 |
2/2✓ Branch 0 taken 37836288 times.
✓ Branch 1 taken 4729536 times.
|
42565824 | for (j = 0; j < 8; j++) |
87 | 37836288 | comb += ABS((a[j] << 1) - b[j - s] - b[j ]) + | |
88 | 37836288 | ABS((b[j] << 1) - a[j ] - a[j + s]); | |
89 | 4729536 | a += s; | |
90 | 4729536 | b += s; | |
91 | } | ||
92 | |||
93 | 1182384 | return comb; | |
94 | } | ||
95 | |||
96 | 1199520 | static int var_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s) | |
97 | { | ||
98 | 1199520 | int i, j, var = 0; | |
99 | |||
100 |
2/2✓ Branch 0 taken 3598560 times.
✓ Branch 1 taken 1199520 times.
|
4798080 | for (i = 0; i < 3; i++) { |
101 |
2/2✓ Branch 0 taken 28788480 times.
✓ Branch 1 taken 3598560 times.
|
32387040 | for (j = 0; j < 8; j++) |
102 | 28788480 | var += ABS(a[j] - a[j + s]); | |
103 | 3598560 | a += s; | |
104 | } | ||
105 | |||
106 | 1199520 | return 4 * var; /* match comb scaling */ | |
107 | } | ||
108 | |||
109 | 108 | static int alloc_metrics(PullupContext *s, PullupField *f) | |
110 | { | ||
111 | 108 | f->diffs = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->diffs)); | |
112 | 108 | f->combs = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->combs)); | |
113 | 108 | f->vars = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->vars)); | |
114 | |||
115 |
3/6✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 108 times.
|
108 | if (!f->diffs || !f->combs || !f->vars) { |
116 | ✗ | av_freep(&f->diffs); | |
117 | ✗ | av_freep(&f->combs); | |
118 | ✗ | av_freep(&f->vars); | |
119 | ✗ | return AVERROR(ENOMEM); | |
120 | } | ||
121 | 108 | return 0; | |
122 | } | ||
123 | |||
124 | 25 | static void free_field_queue(PullupField *head) | |
125 | { | ||
126 | 25 | PullupField *f = head; | |
127 | do { | ||
128 | PullupField *next; | ||
129 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 108 times.
|
121 | if (!f) |
130 | 13 | break; | |
131 | 108 | av_free(f->diffs); | |
132 | 108 | av_free(f->combs); | |
133 | 108 | av_free(f->vars); | |
134 | 108 | next = f->next; | |
135 | 108 | memset(f, 0, sizeof(*f)); // clear all pointers to avoid stale ones | |
136 | 108 | av_free(f); | |
137 | 108 | f = next; | |
138 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 12 times.
|
108 | } while (f != head); |
139 | 25 | } | |
140 | |||
141 | 12 | static PullupField *make_field_queue(PullupContext *s, int len) | |
142 | { | ||
143 | PullupField *head, *f; | ||
144 | |||
145 | 12 | f = head = av_mallocz(sizeof(*head)); | |
146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!f) |
147 | ✗ | return NULL; | |
148 | |||
149 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (alloc_metrics(s, f) < 0) { |
150 | ✗ | av_free(f); | |
151 | ✗ | return NULL; | |
152 | } | ||
153 | |||
154 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 12 times.
|
108 | for (; len > 0; len--) { |
155 | 96 | f->next = av_mallocz(sizeof(*f->next)); | |
156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 | if (!f->next) { |
157 | ✗ | free_field_queue(head); | |
158 | ✗ | return NULL; | |
159 | } | ||
160 | |||
161 | 96 | f->next->prev = f; | |
162 | 96 | f = f->next; | |
163 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
|
96 | if (alloc_metrics(s, f) < 0) { |
164 | ✗ | free_field_queue(head); | |
165 | ✗ | return NULL; | |
166 | } | ||
167 | } | ||
168 | |||
169 | 12 | f->next = head; | |
170 | 12 | head->prev = f; | |
171 | |||
172 | 12 | return head; | |
173 | } | ||
174 | |||
175 | 12 | static int config_input(AVFilterLink *inlink) | |
176 | { | ||
177 | 12 | AVFilterContext *ctx = inlink->dst; | |
178 | 12 | PullupContext *s = ctx->priv; | |
179 | 12 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
180 | 12 | int mp = s->metric_plane; | |
181 | |||
182 | 12 | s->nb_planes = av_pix_fmt_count_planes(inlink->format); | |
183 | |||
184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (mp + 1 > s->nb_planes) { |
185 | ✗ | av_log(ctx, AV_LOG_ERROR, "input format does not have such plane\n"); | |
186 | ✗ | return AVERROR(EINVAL); | |
187 | } | ||
188 | |||
189 | 12 | s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); | |
190 | 12 | s->planeheight[0] = s->planeheight[3] = inlink->h; | |
191 | 12 | s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); | |
192 | 12 | s->planewidth[0] = s->planewidth[3] = inlink->w; | |
193 | |||
194 | 12 | s->metric_w = (s->planewidth[mp] - ((s->junk_left + s->junk_right) << 3)) >> 3; | |
195 | 12 | s->metric_h = (s->planeheight[mp] - ((s->junk_top + s->junk_bottom) << 1)) >> 3; | |
196 | 12 | s->metric_offset = (s->junk_left << 3) + (s->junk_top << 1) * s->planewidth[mp]; | |
197 | 12 | s->metric_length = s->metric_w * s->metric_h; | |
198 | |||
199 | 12 | av_log(ctx, AV_LOG_DEBUG, "w: %d h: %d\n", s->metric_w, s->metric_h); | |
200 | 12 | av_log(ctx, AV_LOG_DEBUG, "offset: %d length: %d\n", s->metric_offset, s->metric_length); | |
201 | |||
202 | 12 | s->head = make_field_queue(s, 8); | |
203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!s->head) |
204 | ✗ | return AVERROR(ENOMEM); | |
205 | |||
206 | 12 | s->diff = diff_c; | |
207 | 12 | s->comb = comb_c; | |
208 | 12 | s->var = var_c; | |
209 | |||
210 | #if ARCH_X86 | ||
211 | 12 | ff_pullup_init_x86(s); | |
212 | #endif | ||
213 | 12 | return 0; | |
214 | } | ||
215 | |||
216 | 2244 | static PullupBuffer *pullup_lock_buffer(PullupBuffer *b, int parity) | |
217 | { | ||
218 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2220 times.
|
2244 | if (!b) |
219 | 24 | return NULL; | |
220 | |||
221 |
2/2✓ Branch 0 taken 1476 times.
✓ Branch 1 taken 744 times.
|
2220 | if ((parity + 1) & 1) |
222 | 1476 | b->lock[0]++; | |
223 |
2/2✓ Branch 0 taken 1476 times.
✓ Branch 1 taken 744 times.
|
2220 | if ((parity + 1) & 2) |
224 | 1476 | b->lock[1]++; | |
225 | |||
226 | 2220 | return b; | |
227 | } | ||
228 | |||
229 | 2208 | static void pullup_release_buffer(PullupBuffer *b, int parity) | |
230 | { | ||
231 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2184 times.
|
2208 | if (!b) |
232 | 24 | return; | |
233 | |||
234 |
2/2✓ Branch 0 taken 1464 times.
✓ Branch 1 taken 720 times.
|
2184 | if ((parity + 1) & 1) |
235 | 1464 | b->lock[0]--; | |
236 |
2/2✓ Branch 0 taken 1452 times.
✓ Branch 1 taken 732 times.
|
2184 | if ((parity + 1) & 2) |
237 | 1452 | b->lock[1]--; | |
238 | } | ||
239 | |||
240 | 420 | static int alloc_buffer(PullupContext *s, PullupBuffer *b) | |
241 | { | ||
242 | int i; | ||
243 | |||
244 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 36 times.
|
420 | if (b->planes[0]) |
245 | 384 | return 0; | |
246 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 36 times.
|
138 | for (i = 0; i < s->nb_planes; i++) { |
247 | 102 | b->planes[i] = av_malloc(s->planeheight[i] * s->planewidth[i]); | |
248 | } | ||
249 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 33 times.
|
36 | if (s->nb_planes == 1) |
250 | 3 | b->planes[1] = av_malloc(4*256); | |
251 | |||
252 | 36 | return 0; | |
253 | } | ||
254 | |||
255 | 420 | static PullupBuffer *pullup_get_buffer(PullupContext *s, int parity) | |
256 | { | ||
257 | int i; | ||
258 | |||
259 | /* Try first to get the sister buffer for the previous field */ | ||
260 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 420 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
420 | if (parity < 2 && s->last && parity != s->last->parity |
261 | ✗ | && !s->last->buffer->lock[parity]) { | |
262 | ✗ | alloc_buffer(s, s->last->buffer); | |
263 | ✗ | return pullup_lock_buffer(s->last->buffer, parity); | |
264 | } | ||
265 | |||
266 | /* Prefer a buffer with both fields open */ | ||
267 |
1/2✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
|
768 | for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) { |
268 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 576 times.
|
768 | if (s->buffers[i].lock[0]) |
269 | 192 | continue; | |
270 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 420 times.
|
576 | if (s->buffers[i].lock[1]) |
271 | 156 | continue; | |
272 | 420 | alloc_buffer(s, &s->buffers[i]); | |
273 | 420 | return pullup_lock_buffer(&s->buffers[i], parity); | |
274 | } | ||
275 | |||
276 | ✗ | if (parity == 2) | |
277 | ✗ | return 0; | |
278 | |||
279 | /* Search for any half-free buffer */ | ||
280 | ✗ | for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) { | |
281 | ✗ | if (((parity + 1) & 1) && s->buffers[i].lock[0]) | |
282 | ✗ | continue; | |
283 | ✗ | if (((parity + 1) & 2) && s->buffers[i].lock[1]) | |
284 | ✗ | continue; | |
285 | ✗ | alloc_buffer(s, &s->buffers[i]); | |
286 | ✗ | return pullup_lock_buffer(&s->buffers[i], parity); | |
287 | } | ||
288 | |||
289 | ✗ | return NULL; | |
290 | } | ||
291 | |||
292 | 780 | static int queue_length(PullupField *begin, PullupField *end) | |
293 | { | ||
294 | PullupField *f; | ||
295 | 780 | int count = 1; | |
296 | |||
297 |
2/4✓ Branch 0 taken 780 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 780 times.
|
780 | if (!begin || !end) |
298 | ✗ | return 0; | |
299 | |||
300 |
2/2✓ Branch 0 taken 2556 times.
✓ Branch 1 taken 780 times.
|
3336 | for (f = begin; f != end; f = f->next) |
301 | 2556 | count++; | |
302 | |||
303 | 780 | return count; | |
304 | } | ||
305 | |||
306 | 312 | static int find_first_break(PullupField *f, int max) | |
307 | { | ||
308 | int i; | ||
309 | |||
310 |
2/2✓ Branch 0 taken 852 times.
✓ Branch 1 taken 72 times.
|
924 | for (i = 0; i < max; i++) { |
311 |
4/4✓ Branch 0 taken 696 times.
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 612 times.
|
852 | if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT) |
312 | 240 | return i + 1; | |
313 | 612 | f = f->next; | |
314 | } | ||
315 | |||
316 | 72 | return 0; | |
317 | } | ||
318 | |||
319 | 504 | static void compute_breaks(PullupContext *s, PullupField *f0) | |
320 | { | ||
321 | 504 | PullupField *f1 = f0->next; | |
322 | 504 | PullupField *f2 = f1->next; | |
323 | 504 | PullupField *f3 = f2->next; | |
324 | 504 | int i, l, max_l = 0, max_r = 0; | |
325 | |||
326 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 492 times.
|
504 | if (f0->flags & F_HAVE_BREAKS) |
327 | 12 | return; | |
328 | |||
329 | 492 | f0->flags |= F_HAVE_BREAKS; | |
330 | |||
331 | /* Special case when fields are 100% identical */ | ||
332 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 492 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
492 | if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) { |
333 | ✗ | f2->breaks |= BREAK_RIGHT; | |
334 | ✗ | return; | |
335 | } | ||
336 | |||
337 |
2/4✓ Branch 0 taken 492 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 492 times.
|
492 | if (f0->buffer != f2->buffer && f1->buffer == f3->buffer) { |
338 | ✗ | f1->breaks |= BREAK_LEFT; | |
339 | ✗ | return; | |
340 | } | ||
341 | |||
342 |
2/2✓ Branch 0 taken 702576 times.
✓ Branch 1 taken 492 times.
|
703068 | for (i = 0; i < s->metric_length; i++) { |
343 | 702576 | l = f2->diffs[i] - f3->diffs[i]; | |
344 | |||
345 |
2/2✓ Branch 0 taken 3150 times.
✓ Branch 1 taken 699426 times.
|
702576 | if ( l > max_l) |
346 | 3150 | max_l = l; | |
347 |
2/2✓ Branch 0 taken 4086 times.
✓ Branch 1 taken 698490 times.
|
702576 | if (-l > max_r) |
348 | 4086 | max_r = -l; | |
349 | } | ||
350 | |||
351 | /* Don't get tripped up when differences are mostly quant error */ | ||
352 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 492 times.
|
492 | if (max_l + max_r < 128) |
353 | ✗ | return; | |
354 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 396 times.
|
492 | if (max_l > 4 * max_r) |
355 | 96 | f1->breaks |= BREAK_LEFT; | |
356 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 336 times.
|
492 | if (max_r > 4 * max_l) |
357 | 156 | f2->breaks |= BREAK_RIGHT; | |
358 | } | ||
359 | |||
360 | 1176 | static void compute_affinity(PullupContext *s, PullupField *f) | |
361 | { | ||
362 | 1176 | int i, max_l = 0, max_r = 0, l; | |
363 | |||
364 |
2/2✓ Branch 0 taken 348 times.
✓ Branch 1 taken 828 times.
|
1176 | if (f->flags & F_HAVE_AFFINITY) |
365 | 348 | return; | |
366 | |||
367 | 828 | f->flags |= F_HAVE_AFFINITY; | |
368 | |||
369 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 828 times.
|
828 | if (f->buffer == f->next->next->buffer) { |
370 | ✗ | f->affinity = 1; | |
371 | ✗ | f->next->affinity = 0; | |
372 | ✗ | f->next->next->affinity = -1; | |
373 | ✗ | f->next->flags |= F_HAVE_AFFINITY; | |
374 | ✗ | f->next->next->flags |= F_HAVE_AFFINITY; | |
375 | ✗ | return; | |
376 | } | ||
377 | |||
378 |
2/2✓ Branch 0 taken 1182384 times.
✓ Branch 1 taken 828 times.
|
1183212 | for (i = 0; i < s->metric_length; i++) { |
379 | 1182384 | int v = f->vars[i]; | |
380 | 1182384 | int lv = f->prev->vars[i]; | |
381 | 1182384 | int rv = f->next->vars[i]; | |
382 | 1182384 | int lc = f-> combs[i] - 2*(v < lv ? v : lv); | |
383 | 1182384 | int rc = f->next->combs[i] - 2*(v < rv ? v : rv); | |
384 | |||
385 | 1182384 | lc = FFMAX(lc, 0); | |
386 | 1182384 | rc = FFMAX(rc, 0); | |
387 | 1182384 | l = lc - rc; | |
388 | |||
389 |
2/2✓ Branch 0 taken 4254 times.
✓ Branch 1 taken 1178130 times.
|
1182384 | if ( l > max_l) |
390 | 4254 | max_l = l; | |
391 |
2/2✓ Branch 0 taken 4278 times.
✓ Branch 1 taken 1178106 times.
|
1182384 | if (-l > max_r) |
392 | 4278 | max_r = -l; | |
393 | } | ||
394 | |||
395 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 660 times.
|
828 | if (max_l + max_r < 64) |
396 | 168 | return; | |
397 | |||
398 |
2/2✓ Branch 0 taken 264 times.
✓ Branch 1 taken 396 times.
|
660 | if (max_r > 6 * max_l) |
399 | 264 | f->affinity = -1; | |
400 |
2/2✓ Branch 0 taken 228 times.
✓ Branch 1 taken 168 times.
|
396 | else if (max_l > 6 * max_r) |
401 | 228 | f->affinity = 1; | |
402 | } | ||
403 | |||
404 | 444 | static int decide_frame_length(PullupContext *s) | |
405 | { | ||
406 | 444 | PullupField *f0 = s->first; | |
407 | 444 | PullupField *f1 = f0->next; | |
408 | 444 | PullupField *f2 = f1->next; | |
409 | PullupField *f; | ||
410 | int i, l, n; | ||
411 | |||
412 |
2/2✓ Branch 1 taken 108 times.
✓ Branch 2 taken 336 times.
|
444 | if (queue_length(s->first, s->last) < 4) |
413 | 108 | return 0; | |
414 | |||
415 | 336 | f = s->first; | |
416 | 336 | n = queue_length(f, s->last); | |
417 |
2/2✓ Branch 0 taken 1176 times.
✓ Branch 1 taken 336 times.
|
1512 | for (i = 0; i < n - 1; i++) { |
418 |
2/2✓ Branch 0 taken 504 times.
✓ Branch 1 taken 672 times.
|
1176 | if (i < n - 3) |
419 | 504 | compute_breaks(s, f); | |
420 | |||
421 | 1176 | compute_affinity(s, f); | |
422 | |||
423 | 1176 | f = f->next; | |
424 | } | ||
425 | |||
426 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 312 times.
|
336 | if (f0->affinity == -1) |
427 | 24 | return 1; | |
428 | |||
429 | 312 | l = find_first_break(f0, 3); | |
430 | |||
431 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
312 | if (l == 1 && s->strict_breaks < 0) |
432 | ✗ | l = 0; | |
433 | |||
434 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 156 times.
✓ Branch 3 taken 72 times.
|
312 | switch (l) { |
435 | ✗ | case 1: | |
436 | ✗ | return 1 + (s->strict_breaks < 1 && f0->affinity == 1 && f1->affinity == -1); | |
437 | 84 | case 2: | |
438 | /* FIXME: strictly speaking, f0->prev is no longer valid... :) */ | ||
439 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (s->strict_pairs |
440 | ✗ | && (f0->prev->breaks & BREAK_RIGHT) && (f2->breaks & BREAK_LEFT) | |
441 | ✗ | && (f0->affinity != 1 || f1->affinity != -1) ) | |
442 | ✗ | return 1; | |
443 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | return 1 + (f1->affinity != 1); |
444 | 156 | case 3: | |
445 |
1/2✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
|
156 | return 2 + (f2->affinity != 1); |
446 | 72 | default: | |
447 | /* 9 possibilities covered before switch */ | ||
448 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (f1->affinity == 1) |
449 | ✗ | return 1; /* covers 6 */ | |
450 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 12 times.
|
72 | else if (f1->affinity == -1) |
451 | 60 | return 2; /* covers 6 */ | |
452 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | else if (f2->affinity == -1) { /* covers 2 */ |
453 | ✗ | return (f0->affinity == 1) ? 3 : 1; | |
454 | } else { | ||
455 | 12 | return 2; /* the remaining 6 */ | |
456 | } | ||
457 | } | ||
458 | } | ||
459 | |||
460 | 444 | static PullupFrame *pullup_get_frame(PullupContext *s) | |
461 | { | ||
462 | 444 | PullupFrame *fr = &s->frame; | |
463 | 444 | int i, n = decide_frame_length(s); | |
464 | 444 | int aff = s->first->next->affinity; | |
465 | |||
466 | av_assert1(n < FF_ARRAY_ELEMS(fr->ifields)); | ||
467 |
3/4✓ Branch 0 taken 336 times.
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 336 times.
|
444 | if (!n || fr->lock) |
468 | 108 | return NULL; | |
469 | |||
470 | 336 | fr->lock++; | |
471 | 336 | fr->length = n; | |
472 | 336 | fr->parity = s->first->parity; | |
473 | 336 | fr->buffer = 0; | |
474 | |||
475 |
2/2✓ Branch 0 taken 804 times.
✓ Branch 1 taken 336 times.
|
1140 | for (i = 0; i < n; i++) { |
476 | /* We cheat and steal the buffer without release+relock */ | ||
477 | 804 | fr->ifields[i] = s->first->buffer; | |
478 | 804 | s->first->buffer = 0; | |
479 | 804 | s->first = s->first->next; | |
480 | } | ||
481 | |||
482 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 312 times.
|
336 | if (n == 1) { |
483 | 24 | fr->ofields[fr->parity ] = fr->ifields[0]; | |
484 | 24 | fr->ofields[fr->parity ^ 1] = 0; | |
485 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 156 times.
|
312 | } else if (n == 2) { |
486 | 156 | fr->ofields[fr->parity ] = fr->ifields[0]; | |
487 | 156 | fr->ofields[fr->parity ^ 1] = fr->ifields[1]; | |
488 |
1/2✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
|
156 | } else if (n == 3) { |
489 |
1/2✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
|
156 | if (!aff) |
490 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 72 times.
|
156 | aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1; |
491 | 156 | fr->ofields[fr->parity ] = fr->ifields[1 + aff]; | |
492 | 156 | fr->ofields[fr->parity ^ 1] = fr->ifields[1 ]; | |
493 | } | ||
494 | |||
495 | 336 | pullup_lock_buffer(fr->ofields[0], 0); | |
496 | 336 | pullup_lock_buffer(fr->ofields[1], 1); | |
497 | |||
498 |
2/2✓ Branch 0 taken 228 times.
✓ Branch 1 taken 108 times.
|
336 | if (fr->ofields[0] == fr->ofields[1]) { |
499 | 228 | fr->buffer = fr->ofields[0]; | |
500 | 228 | pullup_lock_buffer(fr->buffer, 2); | |
501 | 228 | return fr; | |
502 | } | ||
503 | |||
504 | 108 | return fr; | |
505 | } | ||
506 | |||
507 | 336 | static void pullup_release_frame(PullupFrame *f) | |
508 | { | ||
509 | int i; | ||
510 | |||
511 |
2/2✓ Branch 0 taken 804 times.
✓ Branch 1 taken 336 times.
|
1140 | for (i = 0; i < f->length; i++) |
512 | 804 | pullup_release_buffer(f->ifields[i], f->parity ^ (i & 1)); | |
513 | |||
514 | 336 | pullup_release_buffer(f->ofields[0], 0); | |
515 | 336 | pullup_release_buffer(f->ofields[1], 1); | |
516 | |||
517 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 24 times.
|
336 | if (f->buffer) |
518 | 312 | pullup_release_buffer(f->buffer, 2); | |
519 | 336 | f->lock--; | |
520 | 336 | } | |
521 | |||
522 | 2520 | static void compute_metric(PullupContext *s, int *dest, | |
523 | PullupField *fa, int pa, PullupField *fb, int pb, | ||
524 | int (*func)(const uint8_t *, const uint8_t *, ptrdiff_t)) | ||
525 | { | ||
526 | 2520 | int mp = s->metric_plane; | |
527 | 2520 | int xstep = 8; | |
528 | 2520 | int ystep = s->planewidth[mp] << 3; | |
529 | 2520 | int stride = s->planewidth[mp] << 1; /* field stride */ | |
530 | 2520 | int w = s->metric_w * xstep; | |
531 | uint8_t *a, *b; | ||
532 | int x, y; | ||
533 | |||
534 |
3/4✓ Branch 0 taken 2520 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 2400 times.
|
2520 | if (!fa->buffer || !fb->buffer) |
535 | 120 | return; | |
536 | |||
537 | /* Shortcut for duplicate fields (e.g. from RFF flag) */ | ||
538 |
3/4✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 1140 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1260 times.
|
2400 | if (fa->buffer == fb->buffer && pa == pb) { |
539 | ✗ | memset(dest, 0, s->metric_length * sizeof(*dest)); | |
540 | ✗ | return; | |
541 | } | ||
542 | |||
543 | 2400 | a = fa->buffer->planes[mp] + pa * s->planewidth[mp] + s->metric_offset; | |
544 | 2400 | b = fb->buffer->planes[mp] + pb * s->planewidth[mp] + s->metric_offset; | |
545 | |||
546 |
2/2✓ Branch 0 taken 81600 times.
✓ Branch 1 taken 2400 times.
|
84000 | for (y = 0; y < s->metric_h; y++) { |
547 |
2/2✓ Branch 0 taken 3427200 times.
✓ Branch 1 taken 81600 times.
|
3508800 | for (x = 0; x < w; x += xstep) |
548 | 3427200 | *dest++ = func(a + x, b + x, stride); | |
549 | 81600 | a += ystep; b += ystep; | |
550 | } | ||
551 | } | ||
552 | |||
553 | 840 | static int check_field_queue(PullupContext *s) | |
554 | { | ||
555 | int ret; | ||
556 | |||
557 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 840 times.
|
840 | if (s->head->next == s->first) { |
558 | ✗ | PullupField *f = av_mallocz(sizeof(*f)); | |
559 | |||
560 | ✗ | if (!f) | |
561 | ✗ | return AVERROR(ENOMEM); | |
562 | |||
563 | ✗ | if ((ret = alloc_metrics(s, f)) < 0) { | |
564 | ✗ | av_free(f); | |
565 | ✗ | return ret; | |
566 | } | ||
567 | |||
568 | ✗ | f->prev = s->head; | |
569 | ✗ | f->next = s->first; | |
570 | ✗ | s->head->next = f; | |
571 | ✗ | s->first->prev = f; | |
572 | } | ||
573 | |||
574 | 840 | return 0; | |
575 | } | ||
576 | |||
577 | 840 | static void pullup_submit_field(PullupContext *s, PullupBuffer *b, int parity) | |
578 | { | ||
579 | PullupField *f; | ||
580 | |||
581 | /* Grow the circular list if needed */ | ||
582 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 840 times.
|
840 | if (check_field_queue(s) < 0) |
583 | ✗ | return; | |
584 | |||
585 | /* Cannot have two fields of same parity in a row; drop the new one */ | ||
586 |
3/4✓ Branch 0 taken 828 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 828 times.
|
840 | if (s->last && s->last->parity == parity) |
587 | ✗ | return; | |
588 | |||
589 | 840 | f = s->head; | |
590 | 840 | f->parity = parity; | |
591 | 840 | f->buffer = pullup_lock_buffer(b, parity); | |
592 | 840 | f->flags = 0; | |
593 | 840 | f->breaks = 0; | |
594 | 840 | f->affinity = 0; | |
595 | |||
596 | 840 | compute_metric(s, f->diffs, f, parity, f->prev->prev, parity, s->diff); | |
597 |
4/4✓ Branch 0 taken 420 times.
✓ Branch 1 taken 420 times.
✓ Branch 2 taken 420 times.
✓ Branch 3 taken 420 times.
|
840 | compute_metric(s, f->combs, parity ? f->prev : f, 0, parity ? f : f->prev, 1, s->comb); |
598 | 840 | compute_metric(s, f->vars, f, parity, f, -1, s->var); | |
599 | |||
600 | /* Advance the circular list */ | ||
601 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 828 times.
|
840 | if (!s->first) |
602 | 12 | s->first = s->head; | |
603 | |||
604 | 840 | s->last = s->head; | |
605 | 840 | s->head = s->head->next; | |
606 | } | ||
607 | |||
608 | 84 | static void copy_field(PullupContext *s, | |
609 | PullupBuffer *dst, PullupBuffer *src, int parity) | ||
610 | { | ||
611 | uint8_t *dd, *ss; | ||
612 | int i; | ||
613 | |||
614 |
2/2✓ Branch 0 taken 238 times.
✓ Branch 1 taken 84 times.
|
322 | for (i = 0; i < s->nb_planes; i++) { |
615 | 238 | ss = src->planes[i] + parity * s->planewidth[i]; | |
616 | 238 | dd = dst->planes[i] + parity * s->planewidth[i]; | |
617 | |||
618 | 238 | av_image_copy_plane(dd, s->planewidth[i] << 1, | |
619 | 238 | ss, s->planewidth[i] << 1, | |
620 | 238 | s->planewidth[i], s->planeheight[i] >> 1); | |
621 | } | ||
622 | 84 | } | |
623 | |||
624 | 84 | static void pullup_pack_frame(PullupContext *s, PullupFrame *fr) | |
625 | { | ||
626 | int i; | ||
627 | |||
628 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (fr->buffer) |
629 | ✗ | return; | |
630 | |||
631 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (fr->length < 2) |
632 | ✗ | return; /* FIXME: deal with this */ | |
633 | |||
634 |
1/2✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
|
168 | for (i = 0; i < 2; i++) { |
635 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 84 times.
|
168 | if (fr->ofields[i]->lock[i^1]) |
636 | 84 | continue; | |
637 | |||
638 | 84 | fr->buffer = fr->ofields[i]; | |
639 | 84 | pullup_lock_buffer(fr->buffer, 2); | |
640 | 84 | copy_field(s, fr->buffer, fr->ofields[i^1], i^1); | |
641 | 84 | return; | |
642 | } | ||
643 | |||
644 | ✗ | fr->buffer = pullup_get_buffer(s, 2); | |
645 | |||
646 | ✗ | copy_field(s, fr->buffer, fr->ofields[0], 0); | |
647 | ✗ | copy_field(s, fr->buffer, fr->ofields[1], 1); | |
648 | } | ||
649 | |||
650 | 420 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
651 | { | ||
652 | 420 | AVFilterContext *ctx = inlink->dst; | |
653 | 420 | AVFilterLink *outlink = ctx->outputs[0]; | |
654 | 420 | PullupContext *s = ctx->priv; | |
655 | PullupBuffer *b; | ||
656 | PullupFrame *f; | ||
657 | AVFrame *out; | ||
658 | 420 | int p, ret = 0; | |
659 | |||
660 | 420 | b = pullup_get_buffer(s, 2); | |
661 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 420 times.
|
420 | if (!b) { |
662 | ✗ | av_log(ctx, AV_LOG_WARNING, "Could not get buffer!\n"); | |
663 | ✗ | f = pullup_get_frame(s); | |
664 | ✗ | pullup_release_frame(f); | |
665 | ✗ | goto end; | |
666 | } | ||
667 | |||
668 | 420 | av_image_copy2(b->planes, s->planewidth, | |
669 | 420 | in->data, in->linesize, | |
670 | 420 | inlink->format, inlink->w, inlink->h); | |
671 | |||
672 | 840 | p = (in->flags & AV_FRAME_FLAG_INTERLACED) ? | |
673 |
3/4✓ Branch 0 taken 312 times.
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 312 times.
|
420 | !(in->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) : 0; |
674 | 420 | pullup_submit_field(s, b, p ); | |
675 | 420 | pullup_submit_field(s, b, p^1); | |
676 | |||
677 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 420 times.
|
420 | if (in->repeat_pict) |
678 | ✗ | pullup_submit_field(s, b, p); | |
679 | |||
680 | 420 | pullup_release_buffer(b, 2); | |
681 | |||
682 | 420 | f = pullup_get_frame(s); | |
683 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 324 times.
|
420 | if (!f) |
684 | 96 | goto end; | |
685 | |||
686 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 300 times.
|
324 | if (f->length < 2) { |
687 | 24 | pullup_release_frame(f); | |
688 | 24 | f = pullup_get_frame(s); | |
689 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (!f) |
690 | 12 | goto end; | |
691 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (f->length < 2) { |
692 | ✗ | pullup_release_frame(f); | |
693 | ✗ | if (!in->repeat_pict) | |
694 | ✗ | goto end; | |
695 | ✗ | f = pullup_get_frame(s); | |
696 | ✗ | if (!f) | |
697 | ✗ | goto end; | |
698 | ✗ | if (f->length < 2) { | |
699 | ✗ | pullup_release_frame(f); | |
700 | ✗ | goto end; | |
701 | } | ||
702 | } | ||
703 | } | ||
704 | |||
705 | /* If the frame isn't already exportable... */ | ||
706 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 228 times.
|
312 | if (!f->buffer) |
707 | 84 | pullup_pack_frame(s, f); | |
708 | |||
709 | 312 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
710 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
|
312 | if (!out) { |
711 | ✗ | ret = AVERROR(ENOMEM); | |
712 | ✗ | goto end; | |
713 | } | ||
714 | 312 | av_frame_copy_props(out, in); | |
715 | |||
716 | 312 | av_image_copy2(out->data, out->linesize, | |
717 | 312 | f->buffer->planes, s->planewidth, | |
718 | 312 | inlink->format, inlink->w, inlink->h); | |
719 | |||
720 | 312 | ret = ff_filter_frame(outlink, out); | |
721 | 312 | pullup_release_frame(f); | |
722 | 420 | end: | |
723 | 420 | av_frame_free(&in); | |
724 | 420 | return ret; | |
725 | } | ||
726 | |||
727 | 25 | static av_cold void uninit(AVFilterContext *ctx) | |
728 | { | ||
729 | 25 | PullupContext *s = ctx->priv; | |
730 | int i; | ||
731 | |||
732 | 25 | free_field_queue(s->head); | |
733 | 25 | s->last = NULL; | |
734 | |||
735 |
2/2✓ Branch 0 taken 250 times.
✓ Branch 1 taken 25 times.
|
275 | for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) { |
736 | 250 | av_freep(&s->buffers[i].planes[0]); | |
737 | 250 | av_freep(&s->buffers[i].planes[1]); | |
738 | 250 | av_freep(&s->buffers[i].planes[2]); | |
739 | } | ||
740 | 25 | } | |
741 | |||
742 | static const AVFilterPad pullup_inputs[] = { | ||
743 | { | ||
744 | .name = "default", | ||
745 | .type = AVMEDIA_TYPE_VIDEO, | ||
746 | .filter_frame = filter_frame, | ||
747 | .config_props = config_input, | ||
748 | }, | ||
749 | }; | ||
750 | |||
751 | const FFFilter ff_vf_pullup = { | ||
752 | .p.name = "pullup", | ||
753 | .p.description = NULL_IF_CONFIG_SMALL("Pullup from field sequence to frames."), | ||
754 | .p.priv_class = &pullup_class, | ||
755 | .priv_size = sizeof(PullupContext), | ||
756 | .uninit = uninit, | ||
757 | FILTER_INPUTS(pullup_inputs), | ||
758 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
759 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
760 | }; | ||
761 |