LCOV - code coverage report
Current view: top level - libavfilter - vf_cropdetect.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 75 107 70.1 %
Date: 2017-12-11 20:48:03 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2002 A'rpi
       3             :  * This file is part of FFmpeg.
       4             :  *
       5             :  * FFmpeg is free software; you can redistribute it and/or modify
       6             :  * it under the terms of the GNU General Public License as published by
       7             :  * the Free Software Foundation; either version 2 of the License, or
       8             :  * (at your option) any later version.
       9             :  *
      10             :  * FFmpeg is distributed in the hope that it will be useful,
      11             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :  * GNU General Public License for more details.
      14             :  *
      15             :  * You should have received a copy of the GNU General Public License along
      16             :  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
      17             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             :  */
      19             : 
      20             : /**
      21             :  * @file
      22             :  * border detection filter
      23             :  * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
      24             :  */
      25             : 
      26             : #include "libavutil/imgutils.h"
      27             : #include "libavutil/internal.h"
      28             : #include "libavutil/opt.h"
      29             : 
      30             : #include "avfilter.h"
      31             : #include "formats.h"
      32             : #include "internal.h"
      33             : #include "video.h"
      34             : 
      35             : typedef struct CropDetectContext {
      36             :     const AVClass *class;
      37             :     int x1, y1, x2, y2;
      38             :     float limit;
      39             :     int round;
      40             :     int reset_count;
      41             :     int frame_nb;
      42             :     int max_pixsteps[4];
      43             :     int max_outliers;
      44             : } CropDetectContext;
      45             : 
      46           1 : static int query_formats(AVFilterContext *ctx)
      47             : {
      48             :     static const enum AVPixelFormat pix_fmts[] = {
      49             :         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
      50             :         AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
      51             :         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
      52             :         AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
      53             :         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
      54             :         AV_PIX_FMT_YUV420P9 , AV_PIX_FMT_YUV422P9 , AV_PIX_FMT_YUV444P9,
      55             :         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
      56             :         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
      57             :         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
      58             :         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
      59             :         AV_PIX_FMT_NV12,    AV_PIX_FMT_NV21,
      60             :         AV_PIX_FMT_RGB24,   AV_PIX_FMT_BGR24,
      61             :         AV_PIX_FMT_RGBA,    AV_PIX_FMT_BGRA,
      62             :         AV_PIX_FMT_NONE
      63             :     };
      64             : 
      65           1 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
      66           1 :     if (!fmts_list)
      67           0 :         return AVERROR(ENOMEM);
      68           1 :     return ff_set_common_formats(ctx, fmts_list);
      69             : }
      70             : 
      71        3676 : static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
      72             : {
      73        3676 :     int total = 0;
      74        3676 :     int div = len;
      75        3676 :     const uint16_t *src16 = (const uint16_t *)src;
      76             : 
      77        3676 :     switch (bpp) {
      78        3676 :     case 1:
      79      337952 :         while (len >= 8) {
      80      661200 :             total += src[       0] + src[  stride] + src[2*stride] + src[3*stride]
      81      330600 :                   +  src[4*stride] + src[5*stride] + src[6*stride] + src[7*stride];
      82      330600 :             src += 8*stride;
      83      330600 :             len -= 8;
      84             :         }
      85        7400 :         while (--len >= 0) {
      86          48 :             total += src[0];
      87          48 :             src += stride;
      88             :         }
      89        3676 :         break;
      90           0 :     case 2:
      91           0 :         stride >>= 1;
      92           0 :         while (len >= 8) {
      93           0 :             total += src16[       0] + src16[  stride] + src16[2*stride] + src16[3*stride]
      94           0 :                   +  src16[4*stride] + src16[5*stride] + src16[6*stride] + src16[7*stride];
      95           0 :             src16 += 8*stride;
      96           0 :             len -= 8;
      97             :         }
      98           0 :         while (--len >= 0) {
      99           0 :             total += src16[0];
     100           0 :             src16 += stride;
     101             :         }
     102           0 :         break;
     103           0 :     case 3:
     104             :     case 4:
     105           0 :         while (len >= 4) {
     106           0 :             total += src[0]        + src[1         ] + src[2         ]
     107           0 :                   +  src[  stride] + src[1+  stride] + src[2+  stride]
     108           0 :                   +  src[2*stride] + src[1+2*stride] + src[2+2*stride]
     109           0 :                   +  src[3*stride] + src[1+3*stride] + src[2+3*stride];
     110           0 :             src += 4*stride;
     111           0 :             len -= 4;
     112             :         }
     113           0 :         while (--len >= 0) {
     114           0 :             total += src[0] + src[1] + src[2];
     115           0 :             src += stride;
     116             :         }
     117           0 :         div *= 3;
     118           0 :         break;
     119             :     }
     120        3676 :     total /= div;
     121             : 
     122        3676 :     av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
     123        3676 :     return total;
     124             : }
     125             : 
     126           1 : static av_cold int init(AVFilterContext *ctx)
     127             : {
     128           1 :     CropDetectContext *s = ctx->priv;
     129             : 
     130           1 :     s->frame_nb = -2;
     131             : 
     132           2 :     av_log(ctx, AV_LOG_VERBOSE, "limit:%f round:%d reset_count:%d\n",
     133           1 :            s->limit, s->round, s->reset_count);
     134             : 
     135           1 :     return 0;
     136             : }
     137             : 
     138           1 : static int config_input(AVFilterLink *inlink)
     139             : {
     140           1 :     AVFilterContext *ctx = inlink->dst;
     141           1 :     CropDetectContext *s = ctx->priv;
     142           1 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     143             : 
     144           1 :     av_image_fill_max_pixsteps(s->max_pixsteps, NULL, desc);
     145             : 
     146           1 :     if (s->limit < 1.0)
     147           1 :         s->limit *= (1 << desc->comp[0].depth) - 1;
     148             : 
     149           1 :     s->x1 = inlink->w - 1;
     150           1 :     s->y1 = inlink->h - 1;
     151           1 :     s->x2 = 0;
     152           1 :     s->y2 = 0;
     153             : 
     154           1 :     return 0;
     155             : }
     156             : 
     157             : #define SET_META(key, value) \
     158             :     av_dict_set_int(metadata, key, value, 0)
     159             : 
     160          32 : static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     161             : {
     162          32 :     AVFilterContext *ctx = inlink->dst;
     163          32 :     CropDetectContext *s = ctx->priv;
     164          32 :     int bpp = s->max_pixsteps[0];
     165             :     int w, h, x, y, shrink_by;
     166             :     AVDictionary **metadata;
     167             :     int outliers, last_y;
     168          32 :     int limit = lrint(s->limit);
     169             : 
     170             :     // ignore first 2 frames - they may be empty
     171          32 :     if (++s->frame_nb > 0) {
     172          30 :         metadata = &frame->metadata;
     173             : 
     174             :         // Reset the crop area every reset_count frames, if reset_count is > 0
     175          30 :         if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
     176           0 :             s->x1 = frame->width  - 1;
     177           0 :             s->y1 = frame->height - 1;
     178           0 :             s->x2 = 0;
     179           0 :             s->y2 = 0;
     180           0 :             s->frame_nb = 1;
     181             :         }
     182             : 
     183             : #define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
     184             :         outliers = 0;\
     185             :         for (last_y = y = FROM; NOEND; y = y INC) {\
     186             :             if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > limit) {\
     187             :                 if (++outliers > s->max_outliers) { \
     188             :                     DST = last_y;\
     189             :                     break;\
     190             :                 }\
     191             :             } else\
     192             :                 last_y = y INC;\
     193             :         }
     194             : 
     195          30 :         FIND(s->y1,                 0,               y < s->y1, +1, frame->linesize[0], bpp, frame->width);
     196          30 :         FIND(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), -1, frame->linesize[0], bpp, frame->width);
     197          30 :         FIND(s->x1,                 0,               y < s->x1, +1, bpp, frame->linesize[0], frame->height);
     198          30 :         FIND(s->x2,  frame->width - 1, y > FFMAX(s->x2, s->x1), -1, bpp, frame->linesize[0], frame->height);
     199             : 
     200             : 
     201             :         // round x and y (up), important for yuv colorspaces
     202             :         // make sure they stay rounded!
     203          30 :         x = (s->x1+1) & ~1;
     204          30 :         y = (s->y1+1) & ~1;
     205             : 
     206          30 :         w = s->x2 - x + 1;
     207          30 :         h = s->y2 - y + 1;
     208             : 
     209             :         // w and h must be divisible by 2 as well because of yuv
     210             :         // colorspace problems.
     211          30 :         if (s->round <= 1)
     212           0 :             s->round = 16;
     213          30 :         if (s->round % 2)
     214           0 :             s->round *= 2;
     215             : 
     216          30 :         shrink_by = w % s->round;
     217          30 :         w -= shrink_by;
     218          30 :         x += (shrink_by/2 + 1) & ~1;
     219             : 
     220          30 :         shrink_by = h % s->round;
     221          30 :         h -= shrink_by;
     222          30 :         y += (shrink_by/2 + 1) & ~1;
     223             : 
     224          30 :         SET_META("lavfi.cropdetect.x1", s->x1);
     225          30 :         SET_META("lavfi.cropdetect.x2", s->x2);
     226          30 :         SET_META("lavfi.cropdetect.y1", s->y1);
     227          30 :         SET_META("lavfi.cropdetect.y2", s->y2);
     228          30 :         SET_META("lavfi.cropdetect.w",  w);
     229          30 :         SET_META("lavfi.cropdetect.h",  h);
     230          30 :         SET_META("lavfi.cropdetect.x",  x);
     231          30 :         SET_META("lavfi.cropdetect.y",  y);
     232             : 
     233          60 :         av_log(ctx, AV_LOG_INFO,
     234             :                "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
     235             :                s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
     236          60 :                frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
     237             :                w, h, x, y);
     238             :     }
     239             : 
     240          32 :     return ff_filter_frame(inlink->dst->outputs[0], frame);
     241             : }
     242             : 
     243             : #define OFFSET(x) offsetof(CropDetectContext, x)
     244             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
     245             : 
     246             : static const AVOption cropdetect_options[] = {
     247             :     { "limit", "Threshold below which the pixel is considered black", OFFSET(limit),       AV_OPT_TYPE_FLOAT, { .dbl = 24.0/255 }, 0, 65535, FLAGS },
     248             :     { "round", "Value by which the width/height should be divisible", OFFSET(round),       AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
     249             :     { "reset", "Recalculate the crop area after this many frames",    OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 },  0, INT_MAX, FLAGS },
     250             :     { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 },  0, INT_MAX, FLAGS },
     251             :     { "max_outliers", "Threshold count of outliers",                  OFFSET(max_outliers),AV_OPT_TYPE_INT, { .i64 = 0 },  0, INT_MAX, FLAGS },
     252             :     { NULL }
     253             : };
     254             : 
     255             : AVFILTER_DEFINE_CLASS(cropdetect);
     256             : 
     257             : static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
     258             :     {
     259             :         .name         = "default",
     260             :         .type         = AVMEDIA_TYPE_VIDEO,
     261             :         .config_props = config_input,
     262             :         .filter_frame = filter_frame,
     263             :     },
     264             :     { NULL }
     265             : };
     266             : 
     267             : static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
     268             :     {
     269             :         .name = "default",
     270             :         .type = AVMEDIA_TYPE_VIDEO
     271             :     },
     272             :     { NULL }
     273             : };
     274             : 
     275             : AVFilter ff_vf_cropdetect = {
     276             :     .name          = "cropdetect",
     277             :     .description   = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
     278             :     .priv_size     = sizeof(CropDetectContext),
     279             :     .priv_class    = &cropdetect_class,
     280             :     .init          = init,
     281             :     .query_formats = query_formats,
     282             :     .inputs        = avfilter_vf_cropdetect_inputs,
     283             :     .outputs       = avfilter_vf_cropdetect_outputs,
     284             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     285             : };

Generated by: LCOV version 1.13