LCOV - code coverage report
Current view: top level - libavfilter - vf_super2xsai.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 171 176 97.2 %
Date: 2017-12-14 01:15:32 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2010 Niel van der Westhuizen <nielkie@gmail.com>
       3             :  * Copyright (c) 2002 A'rpi
       4             :  * Copyright (c) 1997-2001 ZSNES Team ( zsknight@zsnes.com / _demo_@zsnes.com )
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or modify
       9             :  * it under the terms of the GNU General Public License as published by
      10             :  * the Free Software Foundation; either version 2 of the License, or
      11             :  * (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  * GNU General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU General Public License along
      19             :  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
      20             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * Super 2xSaI video filter
      26             :  * Ported from MPlayer libmpcodecs/vf_2xsai.c.
      27             :  */
      28             : 
      29             : #include "libavutil/pixdesc.h"
      30             : #include "libavutil/intreadwrite.h"
      31             : #include "avfilter.h"
      32             : #include "formats.h"
      33             : #include "internal.h"
      34             : #include "video.h"
      35             : 
      36             : typedef struct Super2xSaIContext {
      37             :     /* masks used for two pixels interpolation */
      38             :     uint32_t hi_pixel_mask;
      39             :     uint32_t lo_pixel_mask;
      40             : 
      41             :     /* masks used for four pixels interpolation */
      42             :     uint32_t q_hi_pixel_mask;
      43             :     uint32_t q_lo_pixel_mask;
      44             : 
      45             :     int bpp; ///< bytes per pixel, pixel stride for each (packed) pixel
      46             :     int is_be;
      47             : } Super2xSaIContext;
      48             : 
      49             : #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
      50             : 
      51             : #define INTERPOLATE(A, B) (((A & hi_pixel_mask) >> 1) + ((B & hi_pixel_mask) >> 1) + (A & B & lo_pixel_mask))
      52             : 
      53             : #define Q_INTERPOLATE(A, B, C, D) ((A & q_hi_pixel_mask) >> 2) + ((B & q_hi_pixel_mask) >> 2) + ((C & q_hi_pixel_mask) >> 2) + ((D & q_hi_pixel_mask) >> 2) \
      54             :     + ((((A & q_lo_pixel_mask) + (B & q_lo_pixel_mask) + (C & q_lo_pixel_mask) + (D & q_lo_pixel_mask)) >> 2) & q_lo_pixel_mask)
      55             : 
      56          14 : static void super2xsai(AVFilterContext *ctx,
      57             :                        uint8_t *src, int src_linesize,
      58             :                        uint8_t *dst, int dst_linesize,
      59             :                        int width, int height)
      60             : {
      61          14 :     Super2xSaIContext *s = ctx->priv;
      62             :     unsigned int x, y;
      63             :     uint32_t color[4][4];
      64             :     unsigned char *src_line[4];
      65          14 :     const int bpp = s->bpp;
      66          14 :     const uint32_t hi_pixel_mask = s->hi_pixel_mask;
      67          14 :     const uint32_t lo_pixel_mask = s->lo_pixel_mask;
      68          14 :     const uint32_t q_hi_pixel_mask = s->q_hi_pixel_mask;
      69          14 :     const uint32_t q_lo_pixel_mask = s->q_lo_pixel_mask;
      70             : 
      71             :     /* Point to the first 4 lines, first line is duplicated */
      72          14 :     src_line[0] = src;
      73          14 :     src_line[1] = src;
      74          14 :     src_line[2] = src + src_linesize*FFMIN(1, height-1);
      75          14 :     src_line[3] = src + src_linesize*FFMIN(2, height-1);
      76             : 
      77             : #define READ_COLOR4(dst, src_line, off) dst = *((const uint32_t *)src_line + off)
      78             : #define READ_COLOR3(dst, src_line, off) dst = AV_RL24 (src_line + 3*off)
      79             : #define READ_COLOR2(dst, src_line, off) dst = s->is_be ? AV_RB16(src_line + 2 * off) : AV_RL16(src_line + 2 * off)
      80             : 
      81        4046 :     for (y = 0; y < height; y++) {
      82             :         uint8_t *dst_line[2];
      83             : 
      84        4032 :         dst_line[0] = dst + dst_linesize*2*y;
      85        4032 :         dst_line[1] = dst + dst_linesize*(2*y+1);
      86             : 
      87        4032 :         switch (bpp) {
      88        1152 :         case 4:
      89        1152 :             READ_COLOR4(color[0][0], src_line[0], 0); color[0][1] = color[0][0]; READ_COLOR4(color[0][2], src_line[0], 1); READ_COLOR4(color[0][3], src_line[0], 2);
      90        1152 :             READ_COLOR4(color[1][0], src_line[1], 0); color[1][1] = color[1][0]; READ_COLOR4(color[1][2], src_line[1], 1); READ_COLOR4(color[1][3], src_line[1], 2);
      91        1152 :             READ_COLOR4(color[2][0], src_line[2], 0); color[2][1] = color[2][0]; READ_COLOR4(color[2][2], src_line[2], 1); READ_COLOR4(color[2][3], src_line[2], 2);
      92        1152 :             READ_COLOR4(color[3][0], src_line[3], 0); color[3][1] = color[3][0]; READ_COLOR4(color[3][2], src_line[3], 1); READ_COLOR4(color[3][3], src_line[3], 2);
      93        1152 :             break;
      94         576 :         case 3:
      95         576 :             READ_COLOR3(color[0][0], src_line[0], 0); color[0][1] = color[0][0]; READ_COLOR3(color[0][2], src_line[0], 1); READ_COLOR3(color[0][3], src_line[0], 2);
      96         576 :             READ_COLOR3(color[1][0], src_line[1], 0); color[1][1] = color[1][0]; READ_COLOR3(color[1][2], src_line[1], 1); READ_COLOR3(color[1][3], src_line[1], 2);
      97         576 :             READ_COLOR3(color[2][0], src_line[2], 0); color[2][1] = color[2][0]; READ_COLOR3(color[2][2], src_line[2], 1); READ_COLOR3(color[2][3], src_line[2], 2);
      98         576 :             READ_COLOR3(color[3][0], src_line[3], 0); color[3][1] = color[3][0]; READ_COLOR3(color[3][2], src_line[3], 1); READ_COLOR3(color[3][3], src_line[3], 2);
      99         576 :             break;
     100        2304 :         default:
     101        2304 :             READ_COLOR2(color[0][0], src_line[0], 0); color[0][1] = color[0][0]; READ_COLOR2(color[0][2], src_line[0], 1); READ_COLOR2(color[0][3], src_line[0], 2);
     102        2304 :             READ_COLOR2(color[1][0], src_line[1], 0); color[1][1] = color[1][0]; READ_COLOR2(color[1][2], src_line[1], 1); READ_COLOR2(color[1][3], src_line[1], 2);
     103        2304 :             READ_COLOR2(color[2][0], src_line[2], 0); color[2][1] = color[2][0]; READ_COLOR2(color[2][2], src_line[2], 1); READ_COLOR2(color[2][3], src_line[2], 2);
     104        2304 :             READ_COLOR2(color[3][0], src_line[3], 0); color[3][1] = color[3][0]; READ_COLOR2(color[3][2], src_line[3], 1); READ_COLOR2(color[3][3], src_line[3], 2);
     105             :         }
     106             : 
     107     1423296 :         for (x = 0; x < width; x++) {
     108             :             uint32_t product1a, product1b, product2a, product2b;
     109             : 
     110             : //---------------------------------------  B0 B1 B2 B3    0  1  2  3
     111             : //                                         4  5* 6  S2 -> 4  5* 6  7
     112             : //                                         1  2  3  S1    8  9 10 11
     113             : //                                         A0 A1 A2 A3   12 13 14 15
     114             : //--------------------------------------
     115     1419264 :             if (color[2][1] == color[1][2] && color[1][1] != color[2][2]) {
     116       33756 :                 product2b = color[2][1];
     117       33756 :                 product1b = product2b;
     118     1385508 :             } else if (color[1][1] == color[2][2] && color[2][1] != color[1][2]) {
     119        1754 :                 product2b = color[1][1];
     120        1754 :                 product1b = product2b;
     121     1383900 :             } else if (color[1][1] == color[2][2] && color[2][1] == color[1][2]) {
     122         146 :                 int r = 0;
     123             : 
     124         146 :                 r += GET_RESULT(color[1][2], color[1][1], color[1][0], color[3][1]);
     125         146 :                 r += GET_RESULT(color[1][2], color[1][1], color[2][0], color[0][1]);
     126         146 :                 r += GET_RESULT(color[1][2], color[1][1], color[3][2], color[2][3]);
     127         146 :                 r += GET_RESULT(color[1][2], color[1][1], color[0][2], color[1][3]);
     128             : 
     129         146 :                 if (r > 0)
     130           0 :                     product1b = color[1][2];
     131         146 :                 else if (r < 0)
     132           0 :                     product1b = color[1][1];
     133             :                 else
     134         146 :                     product1b = INTERPOLATE(color[1][1], color[1][2]);
     135             : 
     136         146 :                 product2b = product1b;
     137             :             } else {
     138     1383608 :                 if (color[1][2] == color[2][2] && color[2][2] == color[3][1] && color[2][1] != color[3][2] && color[2][2] != color[3][0])
     139         104 :                     product2b = Q_INTERPOLATE(color[2][2], color[2][2], color[2][2], color[2][1]);
     140     1383504 :                 else if (color[1][1] == color[2][1] && color[2][1] == color[3][2] && color[3][1] != color[2][2] && color[2][1] != color[3][3])
     141          24 :                     product2b = Q_INTERPOLATE(color[2][1], color[2][1], color[2][1], color[2][2]);
     142             :                 else
     143     1383480 :                     product2b = INTERPOLATE(color[2][1], color[2][2]);
     144             : 
     145     1383608 :                 if (color[1][2] == color[2][2] && color[1][2] == color[0][1] && color[1][1] != color[0][2] && color[1][2] != color[0][0])
     146          28 :                     product1b = Q_INTERPOLATE(color[1][2], color[1][2], color[1][2], color[1][1]);
     147     1383580 :                 else if (color[1][1] == color[2][1] && color[1][1] == color[0][2] && color[0][1] != color[1][2] && color[1][1] != color[0][3])
     148         618 :                     product1b = Q_INTERPOLATE(color[1][2], color[1][1], color[1][1], color[1][1]);
     149             :                 else
     150     1382962 :                     product1b = INTERPOLATE(color[1][1], color[1][2]);
     151             :             }
     152             : 
     153     1419264 :             if (color[1][1] == color[2][2] && color[2][1] != color[1][2] && color[1][0] == color[1][1] && color[1][1] != color[3][2])
     154          12 :                 product2a = INTERPOLATE(color[2][1], color[1][1]);
     155     1419252 :             else if (color[1][1] == color[2][0] && color[1][2] == color[1][1] && color[1][0] != color[2][1] && color[1][1] != color[3][0])
     156         216 :                 product2a = INTERPOLATE(color[2][1], color[1][1]);
     157             :             else
     158     1419036 :                 product2a = color[2][1];
     159             : 
     160     1419264 :             if (color[2][1] == color[1][2] && color[1][1] != color[2][2] && color[2][0] == color[2][1] && color[2][1] != color[0][2])
     161         128 :                 product1a = INTERPOLATE(color[2][1], color[1][1]);
     162     1419136 :             else if (color[1][0] == color[2][1] && color[2][2] == color[2][1] && color[2][0] != color[1][1] && color[2][1] != color[0][0])
     163          40 :                 product1a = INTERPOLATE(color[2][1], color[1][1]);
     164             :             else
     165     1419096 :                 product1a = color[1][1];
     166             : 
     167             :             /* Set the calculated pixels */
     168     1419264 :             switch (bpp) {
     169      405504 :             case 4:
     170      405504 :                 AV_WN32A(dst_line[0] + x * 8,     product1a);
     171      405504 :                 AV_WN32A(dst_line[0] + x * 8 + 4, product1b);
     172      405504 :                 AV_WN32A(dst_line[1] + x * 8,     product2a);
     173      405504 :                 AV_WN32A(dst_line[1] + x * 8 + 4, product2b);
     174      405504 :                 break;
     175      202752 :             case 3:
     176      202752 :                 AV_WL24(dst_line[0] + x * 6,     product1a);
     177      202752 :                 AV_WL24(dst_line[0] + x * 6 + 3, product1b);
     178      202752 :                 AV_WL24(dst_line[1] + x * 6,     product2a);
     179      202752 :                 AV_WL24(dst_line[1] + x * 6 + 3, product2b);
     180      202752 :                 break;
     181      811008 :             default: // bpp = 2
     182      811008 :                 if (s->is_be) {
     183      405504 :                     AV_WB32(dst_line[0] + x * 4, product1a | (product1b << 16));
     184      405504 :                     AV_WB32(dst_line[1] + x * 4, product2a | (product2b << 16));
     185             :                 } else {
     186      405504 :                     AV_WL32(dst_line[0] + x * 4, product1a | (product1b << 16));
     187      405504 :                     AV_WL32(dst_line[1] + x * 4, product2a | (product2b << 16));
     188             :                 }
     189             :             }
     190             : 
     191             :             /* Move color matrix forward */
     192     1419264 :             color[0][0] = color[0][1]; color[0][1] = color[0][2]; color[0][2] = color[0][3];
     193     1419264 :             color[1][0] = color[1][1]; color[1][1] = color[1][2]; color[1][2] = color[1][3];
     194     1419264 :             color[2][0] = color[2][1]; color[2][1] = color[2][2]; color[2][2] = color[2][3];
     195     1419264 :             color[3][0] = color[3][1]; color[3][1] = color[3][2]; color[3][2] = color[3][3];
     196             : 
     197     1419264 :             if (x < width - 3) {
     198     1407168 :                 x += 3;
     199     1407168 :                 switch (bpp) {
     200      402048 :                 case 4:
     201      402048 :                     READ_COLOR4(color[0][3], src_line[0], x);
     202      402048 :                     READ_COLOR4(color[1][3], src_line[1], x);
     203      402048 :                     READ_COLOR4(color[2][3], src_line[2], x);
     204      402048 :                     READ_COLOR4(color[3][3], src_line[3], x);
     205      402048 :                     break;
     206      201024 :                 case 3:
     207      201024 :                     READ_COLOR3(color[0][3], src_line[0], x);
     208      201024 :                     READ_COLOR3(color[1][3], src_line[1], x);
     209      201024 :                     READ_COLOR3(color[2][3], src_line[2], x);
     210      201024 :                     READ_COLOR3(color[3][3], src_line[3], x);
     211      201024 :                     break;
     212      804096 :                 default:        /* case 2 */
     213      804096 :                     READ_COLOR2(color[0][3], src_line[0], x);
     214      804096 :                     READ_COLOR2(color[1][3], src_line[1], x);
     215      804096 :                     READ_COLOR2(color[2][3], src_line[2], x);
     216      804096 :                     READ_COLOR2(color[3][3], src_line[3], x);
     217             :                 }
     218     1407168 :                 x -= 3;
     219             :             }
     220             :         }
     221             : 
     222             :         /* We're done with one line, so we shift the source lines up */
     223        4032 :         src_line[0] = src_line[1];
     224        4032 :         src_line[1] = src_line[2];
     225        4032 :         src_line[2] = src_line[3];
     226             : 
     227             :         /* Read next line */
     228        4032 :         src_line[3] = src_line[2];
     229        4032 :         if (y < height - 3)
     230        3990 :             src_line[3] += src_linesize;
     231             :     } // y loop
     232          14 : }
     233             : 
     234          15 : static int query_formats(AVFilterContext *ctx)
     235             : {
     236             :     static const enum AVPixelFormat pix_fmts[] = {
     237             :         AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
     238             :         AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
     239             :         AV_PIX_FMT_RGB565BE, AV_PIX_FMT_BGR565BE, AV_PIX_FMT_RGB555BE, AV_PIX_FMT_BGR555BE,
     240             :         AV_PIX_FMT_RGB565LE, AV_PIX_FMT_BGR565LE, AV_PIX_FMT_RGB555LE, AV_PIX_FMT_BGR555LE,
     241             :         AV_PIX_FMT_NONE
     242             :     };
     243             : 
     244          15 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     245          15 :     if (!fmts_list)
     246           0 :         return AVERROR(ENOMEM);
     247          15 :     return ff_set_common_formats(ctx, fmts_list);
     248             : }
     249             : 
     250          14 : static int config_input(AVFilterLink *inlink)
     251             : {
     252          14 :     Super2xSaIContext *s = inlink->dst->priv;
     253             : 
     254          14 :     s->hi_pixel_mask   = 0xFEFEFEFE;
     255          14 :     s->lo_pixel_mask   = 0x01010101;
     256          14 :     s->q_hi_pixel_mask = 0xFCFCFCFC;
     257          14 :     s->q_lo_pixel_mask = 0x03030303;
     258          14 :     s->bpp  = 4;
     259             : 
     260          14 :     switch (inlink->format) {
     261           2 :     case AV_PIX_FMT_RGB24:
     262             :     case AV_PIX_FMT_BGR24:
     263           2 :         s->bpp = 3;
     264           2 :         break;
     265             : 
     266           2 :     case AV_PIX_FMT_RGB565BE:
     267             :     case AV_PIX_FMT_BGR565BE:
     268           2 :         s->is_be = 1;
     269           4 :     case AV_PIX_FMT_RGB565LE:
     270             :     case AV_PIX_FMT_BGR565LE:
     271           4 :         s->hi_pixel_mask   = 0xF7DEF7DE;
     272           4 :         s->lo_pixel_mask   = 0x08210821;
     273           4 :         s->q_hi_pixel_mask = 0xE79CE79C;
     274           4 :         s->q_lo_pixel_mask = 0x18631863;
     275           4 :         s->bpp = 2;
     276           4 :         break;
     277             : 
     278           2 :     case AV_PIX_FMT_BGR555BE:
     279             :     case AV_PIX_FMT_RGB555BE:
     280           2 :         s->is_be = 1;
     281           4 :     case AV_PIX_FMT_BGR555LE:
     282             :     case AV_PIX_FMT_RGB555LE:
     283           4 :         s->hi_pixel_mask   = 0x7BDE7BDE;
     284           4 :         s->lo_pixel_mask   = 0x04210421;
     285           4 :         s->q_hi_pixel_mask = 0x739C739C;
     286           4 :         s->q_lo_pixel_mask = 0x0C630C63;
     287           4 :         s->bpp = 2;
     288           4 :         break;
     289             :     }
     290             : 
     291          14 :     return 0;
     292             : }
     293             : 
     294          14 : static int config_output(AVFilterLink *outlink)
     295             : {
     296          14 :     AVFilterLink *inlink = outlink->src->inputs[0];
     297             : 
     298          14 :     outlink->w = inlink->w*2;
     299          14 :     outlink->h = inlink->h*2;
     300             : 
     301          28 :     av_log(inlink->dst, AV_LOG_VERBOSE, "fmt:%s size:%dx%d -> size:%dx%d\n",
     302          14 :            av_get_pix_fmt_name(inlink->format),
     303             :            inlink->w, inlink->h, outlink->w, outlink->h);
     304             : 
     305          14 :     return 0;
     306             : }
     307             : 
     308          14 : static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
     309             : {
     310          14 :     AVFilterLink *outlink = inlink->dst->outputs[0];
     311          14 :     AVFrame *outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     312          14 :     if (!outpicref) {
     313           0 :         av_frame_free(&inpicref);
     314           0 :         return AVERROR(ENOMEM);
     315             :     }
     316          14 :     av_frame_copy_props(outpicref, inpicref);
     317          14 :     outpicref->width  = outlink->w;
     318          14 :     outpicref->height = outlink->h;
     319             : 
     320          14 :     super2xsai(inlink->dst, inpicref->data[0], inpicref->linesize[0],
     321             :                outpicref->data[0], outpicref->linesize[0],
     322             :                inlink->w, inlink->h);
     323             : 
     324          14 :     av_frame_free(&inpicref);
     325          14 :     return ff_filter_frame(outlink, outpicref);
     326             : }
     327             : 
     328             : static const AVFilterPad super2xsai_inputs[] = {
     329             :     {
     330             :         .name         = "default",
     331             :         .type         = AVMEDIA_TYPE_VIDEO,
     332             :         .config_props = config_input,
     333             :         .filter_frame = filter_frame,
     334             :     },
     335             :     { NULL }
     336             : };
     337             : 
     338             : static const AVFilterPad super2xsai_outputs[] = {
     339             :     {
     340             :         .name         = "default",
     341             :         .type         = AVMEDIA_TYPE_VIDEO,
     342             :         .config_props = config_output,
     343             :     },
     344             :     { NULL }
     345             : };
     346             : 
     347             : AVFilter ff_vf_super2xsai = {
     348             :     .name          = "super2xsai",
     349             :     .description   = NULL_IF_CONFIG_SMALL("Scale the input by 2x using the Super2xSaI pixel art algorithm."),
     350             :     .priv_size     = sizeof(Super2xSaIContext),
     351             :     .query_formats = query_formats,
     352             :     .inputs        = super2xsai_inputs,
     353             :     .outputs       = super2xsai_outputs,
     354             : };

Generated by: LCOV version 1.13