LCOV - code coverage report
Current view: top level - libavfilter - vf_pullup.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 343 426 80.5 %
Date: 2017-12-16 13:57:32 Functions: 26 26 100.0 %

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

Generated by: LCOV version 1.13