LCOV - code coverage report
Current view: top level - libavfilter - vf_fillborders.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 163 0.0 %
Date: 2017-12-14 01:15:32 Functions: 0 9 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Paul B Mahol
       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 Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 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 GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along 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/colorspace.h"
      22             : #include "libavutil/common.h"
      23             : #include "libavutil/opt.h"
      24             : #include "libavutil/pixdesc.h"
      25             : #include "avfilter.h"
      26             : #include "drawutils.h"
      27             : #include "formats.h"
      28             : #include "internal.h"
      29             : #include "video.h"
      30             : 
      31             : enum { Y, U, V, A };
      32             : enum { R, G, B };
      33             : 
      34             : enum FillMode { FM_SMEAR, FM_MIRROR, FM_FIXED, FM_NB_MODES };
      35             : 
      36             : typedef struct Borders {
      37             :     int left, right, top, bottom;
      38             : } Borders;
      39             : 
      40             : typedef struct FillBordersContext {
      41             :     const AVClass *class;
      42             :     int left, right, top, bottom;
      43             :     int mode;
      44             : 
      45             :     int nb_planes;
      46             :     int depth;
      47             :     Borders borders[4];
      48             :     int planewidth[4];
      49             :     int planeheight[4];
      50             :     uint8_t fill[4];
      51             :     uint8_t yuv_color[4];
      52             :     uint8_t rgba_color[4];
      53             : 
      54             :     void (*fillborders)(struct FillBordersContext *s, AVFrame *frame);
      55             : } FillBordersContext;
      56             : 
      57           0 : static int query_formats(AVFilterContext *ctx)
      58             : {
      59             :     static const enum AVPixelFormat pix_fmts[] = {
      60             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
      61             :         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
      62             :         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
      63             :         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
      64             :         AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
      65             :         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
      66             :         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
      67             :         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
      68             :         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
      69             :         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
      70             :         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
      71             :         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
      72             :         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
      73             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
      74             :         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
      75             :         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
      76             :         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
      77             :         AV_PIX_FMT_NONE
      78             :     };
      79           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
      80           0 :     if (!fmts_list)
      81           0 :         return AVERROR(ENOMEM);
      82           0 :     return ff_set_common_formats(ctx, fmts_list);
      83             : }
      84             : 
      85           0 : static void smear_borders8(FillBordersContext *s, AVFrame *frame)
      86             : {
      87             :     int p, y;
      88             : 
      89           0 :     for (p = 0; p < s->nb_planes; p++) {
      90           0 :         uint8_t *ptr = frame->data[p];
      91           0 :         int linesize = frame->linesize[p];
      92             : 
      93           0 :         for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
      94           0 :             memset(ptr + y * linesize,
      95           0 :                    *(ptr + y * linesize + s->borders[p].left),
      96           0 :                    s->borders[p].left);
      97           0 :             memset(ptr + y * linesize + s->planewidth[p] - s->borders[p].right,
      98           0 :                    *(ptr + y * linesize + s->planewidth[p] - s->borders[p].right - 1),
      99           0 :                    s->borders[p].right);
     100             :         }
     101             : 
     102           0 :         for (y = 0; y < s->borders[p].top; y++) {
     103           0 :             memcpy(ptr + y * linesize,
     104           0 :                    ptr + s->borders[p].top * linesize, s->planewidth[p]);
     105             :         }
     106             : 
     107           0 :         for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) {
     108           0 :             memcpy(ptr + y * linesize,
     109           0 :                    ptr + (s->planeheight[p] - s->borders[p].bottom - 1) * linesize,
     110           0 :                    s->planewidth[p]);
     111             :         }
     112             :     }
     113           0 : }
     114             : 
     115           0 : static void smear_borders16(FillBordersContext *s, AVFrame *frame)
     116             : {
     117             :     int p, y, x;
     118             : 
     119           0 :     for (p = 0; p < s->nb_planes; p++) {
     120           0 :         uint16_t *ptr = (uint16_t *)frame->data[p];
     121           0 :         int linesize = frame->linesize[p] / 2;
     122             : 
     123           0 :         for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
     124           0 :             for (x = 0; x < s->borders[p].left; x++) {
     125           0 :                 ptr[y * linesize + x] =  *(ptr + y * linesize + s->borders[p].left);
     126             :             }
     127             : 
     128           0 :             for (x = 0; x < s->borders[p].right; x++) {
     129           0 :                 ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
     130           0 :                    *(ptr + y * linesize + s->planewidth[p] - s->borders[p].right - 1);
     131             :             }
     132             :         }
     133             : 
     134           0 :         for (y = 0; y < s->borders[p].top; y++) {
     135           0 :             memcpy(ptr + y * linesize,
     136           0 :                    ptr + s->borders[p].top * linesize, s->planewidth[p] * 2);
     137             :         }
     138             : 
     139           0 :         for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) {
     140           0 :             memcpy(ptr + y * linesize,
     141           0 :                    ptr + (s->planeheight[p] - s->borders[p].bottom - 1) * linesize,
     142           0 :                    s->planewidth[p] * 2);
     143             :         }
     144             :     }
     145           0 : }
     146             : 
     147           0 : static void mirror_borders8(FillBordersContext *s, AVFrame *frame)
     148             : {
     149             :     int p, y, x;
     150             : 
     151           0 :     for (p = 0; p < s->nb_planes; p++) {
     152           0 :         uint8_t *ptr = frame->data[p];
     153           0 :         int linesize = frame->linesize[p];
     154             : 
     155           0 :         for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
     156           0 :             for (x = 0; x < s->borders[p].left; x++) {
     157           0 :                 ptr[y * linesize + x] = ptr[y * linesize + s->borders[p].left * 2 - 1 - x];
     158             :             }
     159             : 
     160           0 :             for (x = 0; x < s->borders[p].right; x++) {
     161           0 :                 ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
     162           0 :                     ptr[y * linesize + s->planewidth[p] - s->borders[p].right - 1 - x];
     163             :             }
     164             :         }
     165             : 
     166           0 :         for (y = 0; y < s->borders[p].top; y++) {
     167           0 :             memcpy(ptr + y * linesize,
     168           0 :                    ptr + (s->borders[p].top * 2 - 1 - y) * linesize,
     169           0 :                    s->planewidth[p]);
     170             :         }
     171             : 
     172           0 :         for (y = 0; y < s->borders[p].bottom; y++) {
     173           0 :             memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize,
     174           0 :                    ptr + (s->planeheight[p] - s->borders[p].bottom - 1 - y) * linesize,
     175           0 :                    s->planewidth[p]);
     176             :         }
     177             :     }
     178           0 : }
     179             : 
     180           0 : static void mirror_borders16(FillBordersContext *s, AVFrame *frame)
     181             : {
     182             :     int p, y, x;
     183             : 
     184           0 :     for (p = 0; p < s->nb_planes; p++) {
     185           0 :         uint16_t *ptr = (uint16_t *)frame->data[p];
     186           0 :         int linesize = frame->linesize[p] / 2;
     187             : 
     188           0 :         for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
     189           0 :             for (x = 0; x < s->borders[p].left; x++) {
     190           0 :                 ptr[y * linesize + x] = ptr[y * linesize + s->borders[p].left * 2 - 1 - x];
     191             :             }
     192             : 
     193           0 :             for (x = 0; x < s->borders[p].right; x++) {
     194           0 :                 ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
     195           0 :                     ptr[y * linesize + s->planewidth[p] - s->borders[p].right - 1 - x];
     196             :             }
     197             :         }
     198             : 
     199           0 :         for (y = 0; y < s->borders[p].top; y++) {
     200           0 :             memcpy(ptr + y * linesize,
     201           0 :                    ptr + (s->borders[p].top * 2 - 1 - y) * linesize,
     202           0 :                    s->planewidth[p] * 2);
     203             :         }
     204             : 
     205           0 :         for (y = 0; y < s->borders[p].bottom; y++) {
     206           0 :             memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize,
     207           0 :                    ptr + (s->planeheight[p] - s->borders[p].bottom - 1 - y) * linesize,
     208           0 :                    s->planewidth[p] * 2);
     209             :         }
     210             :     }
     211           0 : }
     212             : 
     213           0 : static void fixed_borders8(FillBordersContext *s, AVFrame *frame)
     214             : {
     215             :     int p, y;
     216             : 
     217           0 :     for (p = 0; p < s->nb_planes; p++) {
     218           0 :         uint8_t *ptr = frame->data[p];
     219           0 :         uint8_t fill = s->fill[p];
     220           0 :         int linesize = frame->linesize[p];
     221             : 
     222           0 :         for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
     223           0 :             memset(ptr + y * linesize, fill, s->borders[p].left);
     224           0 :             memset(ptr + y * linesize + s->planewidth[p] - s->borders[p].right, fill,
     225           0 :                    s->borders[p].right);
     226             :         }
     227             : 
     228           0 :         for (y = 0; y < s->borders[p].top; y++) {
     229           0 :             memset(ptr + y * linesize, fill, s->planewidth[p]);
     230             :         }
     231             : 
     232           0 :         for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) {
     233           0 :             memset(ptr + y * linesize, fill, s->planewidth[p]);
     234             :         }
     235             :     }
     236           0 : }
     237             : 
     238           0 : static void fixed_borders16(FillBordersContext *s, AVFrame *frame)
     239             : {
     240             :     int p, y, x;
     241             : 
     242           0 :     for (p = 0; p < s->nb_planes; p++) {
     243           0 :         uint16_t *ptr = (uint16_t *)frame->data[p];
     244           0 :         uint16_t fill = s->fill[p] << (s->depth - 8);
     245           0 :         int linesize = frame->linesize[p] / 2;
     246             : 
     247           0 :         for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
     248           0 :             for (x = 0; x < s->borders[p].left; x++) {
     249           0 :                 ptr[y * linesize + x] = fill;
     250             :             }
     251             : 
     252           0 :             for (x = 0; x < s->borders[p].right; x++) {
     253           0 :                 ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] = fill;
     254             :             }
     255             :         }
     256             : 
     257           0 :         for (y = 0; y < s->borders[p].top; y++) {
     258           0 :             for (x = 0; x < s->planewidth[p]; x++) {
     259           0 :                 ptr[y * linesize + x] = fill;
     260             :             }
     261             :         }
     262             : 
     263           0 :         for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) {
     264           0 :             for (x = 0; x < s->planewidth[p]; x++) {
     265           0 :                 ptr[y * linesize + x] = fill;
     266             :             }
     267             :         }
     268             :     }
     269           0 : }
     270             : 
     271           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     272             : {
     273           0 :     FillBordersContext *s = inlink->dst->priv;
     274             : 
     275           0 :     s->fillborders(s, frame);
     276             : 
     277           0 :     return ff_filter_frame(inlink->dst->outputs[0], frame);
     278             : }
     279             : 
     280           0 : static int config_input(AVFilterLink *inlink)
     281             : {
     282           0 :     AVFilterContext *ctx = inlink->dst;
     283           0 :     FillBordersContext *s = ctx->priv;
     284           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     285             : 
     286           0 :     s->nb_planes = desc->nb_components;
     287           0 :     s->depth = desc->comp[0].depth;
     288             : 
     289           0 :     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
     290           0 :     s->planeheight[0] = s->planeheight[3] = inlink->h;
     291           0 :     s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
     292           0 :     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
     293             : 
     294           0 :     s->borders[0].left   = s->borders[3].left = s->left;
     295           0 :     s->borders[0].right  = s->borders[3].right = s->right;
     296           0 :     s->borders[0].top    = s->borders[3].top = s->top;
     297           0 :     s->borders[0].bottom = s->borders[3].bottom = s->bottom;
     298             : 
     299           0 :     s->borders[1].left   = s->left >> desc->log2_chroma_w;
     300           0 :     s->borders[1].right  = s->right >> desc->log2_chroma_w;
     301           0 :     s->borders[1].top    = s->top >> desc->log2_chroma_h;
     302           0 :     s->borders[1].bottom = s->bottom >> desc->log2_chroma_h;
     303             : 
     304           0 :     s->borders[2].left   = s->left >> desc->log2_chroma_w;
     305           0 :     s->borders[2].right  = s->right >> desc->log2_chroma_w;
     306           0 :     s->borders[2].top    = s->top >> desc->log2_chroma_h;
     307           0 :     s->borders[2].bottom = s->bottom >> desc->log2_chroma_h;
     308             : 
     309           0 :     if (inlink->w < s->left + s->right ||
     310           0 :         inlink->w <= s->left ||
     311           0 :         inlink->w <= s->right ||
     312           0 :         inlink->h < s->top + s->bottom ||
     313           0 :         inlink->h <= s->top ||
     314           0 :         inlink->h <= s->bottom ||
     315           0 :         inlink->w < s->left * 2 ||
     316           0 :         inlink->w < s->right * 2 ||
     317           0 :         inlink->h < s->top * 2 ||
     318           0 :         inlink->h < s->bottom * 2) {
     319           0 :         av_log(ctx, AV_LOG_ERROR, "Borders are bigger than input frame size.\n");
     320           0 :         return AVERROR(EINVAL);
     321             :     }
     322             : 
     323           0 :     switch (s->mode) {
     324           0 :     case FM_SMEAR:  s->fillborders = s->depth <= 8 ? smear_borders8  : smear_borders16;  break;
     325           0 :     case FM_MIRROR: s->fillborders = s->depth <= 8 ? mirror_borders8 : mirror_borders16; break;
     326           0 :     case FM_FIXED:  s->fillborders = s->depth <= 8 ? fixed_borders8  : fixed_borders16;  break;
     327             :     }
     328             : 
     329           0 :     s->yuv_color[Y] = RGB_TO_Y_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B]);
     330           0 :     s->yuv_color[U] = RGB_TO_U_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B], 0);
     331           0 :     s->yuv_color[V] = RGB_TO_V_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B], 0);
     332           0 :     s->yuv_color[A] = s->rgba_color[A];
     333             : 
     334           0 :     if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
     335             :         uint8_t rgba_map[4];
     336             :         int i;
     337             : 
     338           0 :         ff_fill_rgba_map(rgba_map, inlink->format);
     339           0 :         for (i = 0; i < 4; i++)
     340           0 :             s->fill[rgba_map[i]] = s->rgba_color[i];
     341             :     } else {
     342           0 :         memcpy(s->fill, s->yuv_color, sizeof(s->yuv_color));
     343             :     }
     344             : 
     345           0 :     return 0;
     346             : }
     347             : 
     348             : #define OFFSET(x) offsetof(FillBordersContext, x)
     349             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
     350             : 
     351             : static const AVOption fillborders_options[] = {
     352             :     { "left",   "set the left fill border",   OFFSET(left),   AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX,    FLAGS },
     353             :     { "right",  "set the right fill border",  OFFSET(right),  AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX,    FLAGS },
     354             :     { "top",    "set the top fill border",    OFFSET(top),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX,    FLAGS },
     355             :     { "bottom", "set the bottom fill border", OFFSET(bottom), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX,    FLAGS },
     356             :     { "mode",   "set the fill borders mode",  OFFSET(mode),   AV_OPT_TYPE_INT, {.i64=FM_SMEAR}, 0, FM_NB_MODES-1, FLAGS, "mode" },
     357             :         { "smear",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_SMEAR},  0, 0, FLAGS, "mode" },
     358             :         { "mirror", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_MIRROR}, 0, 0, FLAGS, "mode" },
     359             :         { "fixed",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_FIXED},  0, 0, FLAGS, "mode" },
     360             :     { "color",  "set the color for the fixed mode", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
     361             :     { NULL }
     362             : };
     363             : 
     364             : AVFILTER_DEFINE_CLASS(fillborders);
     365             : 
     366             : static const AVFilterPad fillborders_inputs[] = {
     367             :     {
     368             :         .name           = "default",
     369             :         .type           = AVMEDIA_TYPE_VIDEO,
     370             :         .config_props   = config_input,
     371             :         .filter_frame   = filter_frame,
     372             :         .needs_writable = 1,
     373             :     },
     374             :     { NULL }
     375             : };
     376             : 
     377             : static const AVFilterPad fillborders_outputs[] = {
     378             :     {
     379             :         .name = "default",
     380             :         .type = AVMEDIA_TYPE_VIDEO,
     381             :     },
     382             :     { NULL }
     383             : };
     384             : 
     385             : AVFilter ff_vf_fillborders = {
     386             :     .name          = "fillborders",
     387             :     .description   = NULL_IF_CONFIG_SMALL("Fill borders of the input video."),
     388             :     .priv_size     = sizeof(FillBordersContext),
     389             :     .priv_class    = &fillborders_class,
     390             :     .query_formats = query_formats,
     391             :     .inputs        = fillborders_inputs,
     392             :     .outputs       = fillborders_outputs,
     393             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     394             : };

Generated by: LCOV version 1.13