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