2 * Copyright (c) 2003 Rich Felker
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
29 #include "vf_pullup.h"
31 #define F_HAVE_BREAKS 1
32 #define F_HAVE_AFFINITY 2
37 #define OFFSET(x) offsetof(PullupContext, x)
38 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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" },
53 AVFILTER_DEFINE_CLASS(pullup);
55 static int query_formats(AVFilterContext *ctx)
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,
67 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
69 return AVERROR(ENOMEM);
70 return ff_set_common_formats(ctx, fmts_list);
73 #define ABS(a) (((a) ^ ((a) >> 31)) - ((a) >> 31))
75 static int diff_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s)
79 for (i = 0; i < 4; i++) {
80 for (j = 0; j < 8; j++)
81 diff += ABS(a[j] - b[j]);
89 static int comb_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s)
93 for (i = 0; i < 4; i++) {
94 for (j = 0; j < 8; j++)
95 comb += ABS((a[j] << 1) - b[j - s] - b[j ]) +
96 ABS((b[j] << 1) - a[j ] - a[j + s]);
104 static int var_c(const uint8_t *a, const uint8_t *b, ptrdiff_t s)
108 for (i = 0; i < 3; i++) {
109 for (j = 0; j < 8; j++)
110 var += ABS(a[j] - a[j + s]);
114 return 4 * var; /* match comb scaling */
117 static int alloc_metrics(PullupContext *s, PullupField *f)
119 f->diffs = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->diffs));
120 f->combs = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->combs));
121 f->vars = av_calloc(FFALIGN(s->metric_length, 16), sizeof(*f->vars));
123 if (!f->diffs || !f->combs || !f->vars) {
127 return AVERROR(ENOMEM);
132 static void free_field_queue(PullupField *head)
134 PullupField *f = head;
143 memset(f, 0, sizeof(*f)); // clear all pointers to avoid stale ones
149 static PullupField *make_field_queue(PullupContext *s, int len)
151 PullupField *head, *f;
153 f = head = av_mallocz(sizeof(*head));
157 if (alloc_metrics(s, f) < 0) {
162 for (; len > 0; len--) {
163 f->next = av_mallocz(sizeof(*f->next));
165 free_field_queue(head);
171 if (alloc_metrics(s, f) < 0) {
172 free_field_queue(head);
183 static int config_input(AVFilterLink *inlink)
185 AVFilterContext *ctx = inlink->dst;
186 PullupContext *s = ctx->priv;
187 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
188 int mp = s->metric_plane;
190 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
192 if (mp + 1 > s->nb_planes) {
193 av_log(ctx, AV_LOG_ERROR, "input format does not have such plane\n");
194 return AVERROR(EINVAL);
197 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
198 s->planeheight[0] = s->planeheight[3] = inlink->h;
199 s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
200 s->planewidth[0] = s->planewidth[3] = inlink->w;
202 s->metric_w = (s->planewidth[mp] - ((s->junk_left + s->junk_right) << 3)) >> 3;
203 s->metric_h = (s->planeheight[mp] - ((s->junk_top + s->junk_bottom) << 1)) >> 3;
204 s->metric_offset = (s->junk_left << 3) + (s->junk_top << 1) * s->planewidth[mp];
205 s->metric_length = s->metric_w * s->metric_h;
207 av_log(ctx, AV_LOG_DEBUG, "w: %d h: %d\n", s->metric_w, s->metric_h);
208 av_log(ctx, AV_LOG_DEBUG, "offset: %d length: %d\n", s->metric_offset, s->metric_length);
210 s->head = make_field_queue(s, 8);
212 return AVERROR(ENOMEM);
219 ff_pullup_init_x86(s);
223 static int config_output(AVFilterLink *outlink)
225 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
229 static PullupBuffer *pullup_lock_buffer(PullupBuffer *b, int parity)
234 if ((parity + 1) & 1)
236 if ((parity + 1) & 2)
242 static void pullup_release_buffer(PullupBuffer *b, int parity)
247 if ((parity + 1) & 1)
249 if ((parity + 1) & 2)
253 static int alloc_buffer(PullupContext *s, PullupBuffer *b)
259 for (i = 0; i < s->nb_planes; i++) {
260 b->planes[i] = av_malloc(s->planeheight[i] * s->planewidth[i]);
262 if (s->nb_planes == 1)
263 b->planes[1] = av_malloc(4*256);
268 static PullupBuffer *pullup_get_buffer(PullupContext *s, int parity)
272 /* Try first to get the sister buffer for the previous field */
273 if (parity < 2 && s->last && parity != s->last->parity
274 && !s->last->buffer->lock[parity]) {
275 alloc_buffer(s, s->last->buffer);
276 return pullup_lock_buffer(s->last->buffer, parity);
279 /* Prefer a buffer with both fields open */
280 for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
281 if (s->buffers[i].lock[0])
283 if (s->buffers[i].lock[1])
285 alloc_buffer(s, &s->buffers[i]);
286 return pullup_lock_buffer(&s->buffers[i], parity);
292 /* Search for any half-free buffer */
293 for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
294 if (((parity + 1) & 1) && s->buffers[i].lock[0])
296 if (((parity + 1) & 2) && s->buffers[i].lock[1])
298 alloc_buffer(s, &s->buffers[i]);
299 return pullup_lock_buffer(&s->buffers[i], parity);
305 static int queue_length(PullupField *begin, PullupField *end)
313 for (f = begin; f != end; f = f->next)
319 static int find_first_break(PullupField *f, int max)
323 for (i = 0; i < max; i++) {
324 if (f->breaks & BREAK_RIGHT || f->next->breaks & BREAK_LEFT)
332 static void compute_breaks(PullupContext *s, PullupField *f0)
334 PullupField *f1 = f0->next;
335 PullupField *f2 = f1->next;
336 PullupField *f3 = f2->next;
337 int i, l, max_l = 0, max_r = 0;
339 if (f0->flags & F_HAVE_BREAKS)
342 f0->flags |= F_HAVE_BREAKS;
344 /* Special case when fields are 100% identical */
345 if (f0->buffer == f2->buffer && f1->buffer != f3->buffer) {
346 f2->breaks |= BREAK_RIGHT;
350 if (f0->buffer != f2->buffer && f1->buffer == f3->buffer) {
351 f1->breaks |= BREAK_LEFT;
355 for (i = 0; i < s->metric_length; i++) {
356 l = f2->diffs[i] - f3->diffs[i];
364 /* Don't get tripped up when differences are mostly quant error */
365 if (max_l + max_r < 128)
367 if (max_l > 4 * max_r)
368 f1->breaks |= BREAK_LEFT;
369 if (max_r > 4 * max_l)
370 f2->breaks |= BREAK_RIGHT;
373 static void compute_affinity(PullupContext *s, PullupField *f)
375 int i, max_l = 0, max_r = 0, l;
377 if (f->flags & F_HAVE_AFFINITY)
380 f->flags |= F_HAVE_AFFINITY;
382 if (f->buffer == f->next->next->buffer) {
384 f->next->affinity = 0;
385 f->next->next->affinity = -1;
386 f->next->flags |= F_HAVE_AFFINITY;
387 f->next->next->flags |= F_HAVE_AFFINITY;
391 for (i = 0; i < s->metric_length; i++) {
393 int lv = f->prev->vars[i];
394 int rv = f->next->vars[i];
395 int lc = f-> combs[i] - 2*(v < lv ? v : lv);
396 int rc = f->next->combs[i] - 2*(v < rv ? v : rv);
408 if (max_l + max_r < 64)
411 if (max_r > 6 * max_l)
413 else if (max_l > 6 * max_r)
417 static int decide_frame_length(PullupContext *s)
419 PullupField *f0 = s->first;
420 PullupField *f1 = f0->next;
421 PullupField *f2 = f1->next;
425 if (queue_length(s->first, s->last) < 4)
429 n = queue_length(f, s->last);
430 for (i = 0; i < n - 1; i++) {
432 compute_breaks(s, f);
434 compute_affinity(s, f);
439 if (f0->affinity == -1)
442 l = find_first_break(f0, 3);
444 if (l == 1 && s->strict_breaks < 0)
449 return 1 + (s->strict_breaks < 1 && f0->affinity == 1 && f1->affinity == -1);
451 /* FIXME: strictly speaking, f0->prev is no longer valid... :) */
453 && (f0->prev->breaks & BREAK_RIGHT) && (f2->breaks & BREAK_LEFT)
454 && (f0->affinity != 1 || f1->affinity != -1) )
456 return 1 + (f1->affinity != 1);
458 return 2 + (f2->affinity != 1);
460 /* 9 possibilities covered before switch */
461 if (f1->affinity == 1)
462 return 1; /* covers 6 */
463 else if (f1->affinity == -1)
464 return 2; /* covers 6 */
465 else if (f2->affinity == -1) { /* covers 2 */
466 return (f0->affinity == 1) ? 3 : 1;
468 return 2; /* the remaining 6 */
473 static PullupFrame *pullup_get_frame(PullupContext *s)
475 PullupFrame *fr = &s->frame;
476 int i, n = decide_frame_length(s);
477 int aff = s->first->next->affinity;
479 av_assert1(n < FF_ARRAY_ELEMS(fr->ifields));
485 fr->parity = s->first->parity;
488 for (i = 0; i < n; i++) {
489 /* We cheat and steal the buffer without release+relock */
490 fr->ifields[i] = s->first->buffer;
491 s->first->buffer = 0;
492 s->first = s->first->next;
496 fr->ofields[fr->parity ] = fr->ifields[0];
497 fr->ofields[fr->parity ^ 1] = 0;
499 fr->ofields[fr->parity ] = fr->ifields[0];
500 fr->ofields[fr->parity ^ 1] = fr->ifields[1];
503 aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1;
504 fr->ofields[fr->parity ] = fr->ifields[1 + aff];
505 fr->ofields[fr->parity ^ 1] = fr->ifields[1 ];
508 pullup_lock_buffer(fr->ofields[0], 0);
509 pullup_lock_buffer(fr->ofields[1], 1);
511 if (fr->ofields[0] == fr->ofields[1]) {
512 fr->buffer = fr->ofields[0];
513 pullup_lock_buffer(fr->buffer, 2);
520 static void pullup_release_frame(PullupFrame *f)
524 for (i = 0; i < f->length; i++)
525 pullup_release_buffer(f->ifields[i], f->parity ^ (i & 1));
527 pullup_release_buffer(f->ofields[0], 0);
528 pullup_release_buffer(f->ofields[1], 1);
531 pullup_release_buffer(f->buffer, 2);
535 static void compute_metric(PullupContext *s, int *dest,
536 PullupField *fa, int pa, PullupField *fb, int pb,
537 int (*func)(const uint8_t *, const uint8_t *, ptrdiff_t))
539 int mp = s->metric_plane;
541 int ystep = s->planewidth[mp] << 3;
542 int stride = s->planewidth[mp] << 1; /* field stride */
543 int w = s->metric_w * xstep;
547 if (!fa->buffer || !fb->buffer)
550 /* Shortcut for duplicate fields (e.g. from RFF flag) */
551 if (fa->buffer == fb->buffer && pa == pb) {
552 memset(dest, 0, s->metric_length * sizeof(*dest));
556 a = fa->buffer->planes[mp] + pa * s->planewidth[mp] + s->metric_offset;
557 b = fb->buffer->planes[mp] + pb * s->planewidth[mp] + s->metric_offset;
559 for (y = 0; y < s->metric_h; y++) {
560 for (x = 0; x < w; x += xstep)
561 *dest++ = func(a + x, b + x, stride);
562 a += ystep; b += ystep;
566 static int check_field_queue(PullupContext *s)
570 if (s->head->next == s->first) {
571 PullupField *f = av_mallocz(sizeof(*f));
574 return AVERROR(ENOMEM);
576 if ((ret = alloc_metrics(s, f)) < 0) {
590 static void pullup_submit_field(PullupContext *s, PullupBuffer *b, int parity)
594 /* Grow the circular list if needed */
595 if (check_field_queue(s) < 0)
598 /* Cannot have two fields of same parity in a row; drop the new one */
599 if (s->last && s->last->parity == parity)
604 f->buffer = pullup_lock_buffer(b, parity);
609 compute_metric(s, f->diffs, f, parity, f->prev->prev, parity, s->diff);
610 compute_metric(s, f->combs, parity ? f->prev : f, 0, parity ? f : f->prev, 1, s->comb);
611 compute_metric(s, f->vars, f, parity, f, -1, s->var);
614 /* Advance the circular list */
619 s->head = s->head->next;
622 static void copy_field(PullupContext *s,
623 PullupBuffer *dst, PullupBuffer *src, int parity)
628 for (i = 0; i < s->nb_planes; i++) {
629 ss = src->planes[i] + parity * s->planewidth[i];
630 dd = dst->planes[i] + parity * s->planewidth[i];
632 av_image_copy_plane(dd, s->planewidth[i] << 1,
633 ss, s->planewidth[i] << 1,
634 s->planewidth[i], s->planeheight[i] >> 1);
638 static void pullup_pack_frame(PullupContext *s, PullupFrame *fr)
646 return; /* FIXME: deal with this */
648 for (i = 0; i < 2; i++) {
649 if (fr->ofields[i]->lock[i^1])
652 fr->buffer = fr->ofields[i];
653 pullup_lock_buffer(fr->buffer, 2);
654 copy_field(s, fr->buffer, fr->ofields[i^1], i^1);
658 fr->buffer = pullup_get_buffer(s, 2);
660 copy_field(s, fr->buffer, fr->ofields[0], 0);
661 copy_field(s, fr->buffer, fr->ofields[1], 1);
664 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
666 AVFilterContext *ctx = inlink->dst;
667 AVFilterLink *outlink = ctx->outputs[0];
668 PullupContext *s = ctx->priv;
674 b = pullup_get_buffer(s, 2);
676 av_log(ctx, AV_LOG_WARNING, "Could not get buffer!\n");
677 f = pullup_get_frame(s);
678 pullup_release_frame(f);
682 av_image_copy(b->planes, s->planewidth,
683 (const uint8_t**)in->data, in->linesize,
684 inlink->format, inlink->w, inlink->h);
686 p = in->interlaced_frame ? !in->top_field_first : 0;
687 pullup_submit_field(s, b, p );
688 pullup_submit_field(s, b, p^1);
691 pullup_submit_field(s, b, p);
693 pullup_release_buffer(b, 2);
695 f = pullup_get_frame(s);
700 pullup_release_frame(f);
701 f = pullup_get_frame(s);
705 pullup_release_frame(f);
706 if (!in->repeat_pict)
708 f = pullup_get_frame(s);
712 pullup_release_frame(f);
718 /* If the frame isn't already exportable... */
720 pullup_pack_frame(s, f);
722 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
724 ret = AVERROR(ENOMEM);
727 av_frame_copy_props(out, in);
729 av_image_copy(out->data, out->linesize,
730 (const uint8_t**)f->buffer->planes, s->planewidth,
731 inlink->format, inlink->w, inlink->h);
733 ret = ff_filter_frame(outlink, out);
734 pullup_release_frame(f);
740 static av_cold void uninit(AVFilterContext *ctx)
742 PullupContext *s = ctx->priv;
745 free_field_queue(s->head);
748 for (i = 0; i < FF_ARRAY_ELEMS(s->buffers); i++) {
749 av_freep(&s->buffers[i].planes[0]);
750 av_freep(&s->buffers[i].planes[1]);
751 av_freep(&s->buffers[i].planes[2]);
755 static const AVFilterPad pullup_inputs[] = {
758 .type = AVMEDIA_TYPE_VIDEO,
759 .filter_frame = filter_frame,
760 .config_props = config_input,
765 static const AVFilterPad pullup_outputs[] = {
768 .type = AVMEDIA_TYPE_VIDEO,
769 .config_props = config_output,
774 AVFilter ff_vf_pullup = {
776 .description = NULL_IF_CONFIG_SMALL("Pullup from field sequence to frames."),
777 .priv_size = sizeof(PullupContext),
778 .priv_class = &pullup_class,
780 .query_formats = query_formats,
781 .inputs = pullup_inputs,
782 .outputs = pullup_outputs,