LCOV - code coverage report
Current view: top level - libavfilter - vf_colorchannelmixer.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 73 181 40.3 %
Date: 2017-12-16 01:21:47 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 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/opt.h"
      22             : #include "avfilter.h"
      23             : #include "drawutils.h"
      24             : #include "formats.h"
      25             : #include "internal.h"
      26             : #include "video.h"
      27             : 
      28             : #define R 0
      29             : #define G 1
      30             : #define B 2
      31             : #define A 3
      32             : 
      33             : typedef struct ColorChannelMixerContext {
      34             :     const AVClass *class;
      35             :     double rr, rg, rb, ra;
      36             :     double gr, gg, gb, ga;
      37             :     double br, bg, bb, ba;
      38             :     double ar, ag, ab, aa;
      39             : 
      40             :     int *lut[4][4];
      41             : 
      42             :     int *buffer;
      43             : 
      44             :     uint8_t rgba_map[4];
      45             : } ColorChannelMixerContext;
      46             : 
      47             : #define OFFSET(x) offsetof(ColorChannelMixerContext, x)
      48             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      49             : static const AVOption colorchannelmixer_options[] = {
      50             :     { "rr", "set the red gain for the red channel",     OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
      51             :     { "rg", "set the green gain for the red channel",   OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      52             :     { "rb", "set the blue gain for the red channel",    OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      53             :     { "ra", "set the alpha gain for the red channel",   OFFSET(ra), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      54             :     { "gr", "set the red gain for the green channel",   OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      55             :     { "gg", "set the green gain for the green channel", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
      56             :     { "gb", "set the blue gain for the green channel",  OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      57             :     { "ga", "set the alpha gain for the green channel", OFFSET(ga), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      58             :     { "br", "set the red gain for the blue channel",    OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      59             :     { "bg", "set the green gain for the blue channel",  OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      60             :     { "bb", "set the blue gain for the blue channel",   OFFSET(bb), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
      61             :     { "ba", "set the alpha gain for the blue channel",  OFFSET(ba), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      62             :     { "ar", "set the red gain for the alpha channel",   OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      63             :     { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      64             :     { "ab", "set the blue gain for the alpha channel",  OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
      65             :     { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
      66             :     { NULL }
      67             : };
      68             : 
      69             : AVFILTER_DEFINE_CLASS(colorchannelmixer);
      70             : 
      71           1 : static int query_formats(AVFilterContext *ctx)
      72             : {
      73             :     static const enum AVPixelFormat pix_fmts[] = {
      74             :         AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
      75             :         AV_PIX_FMT_RGBA,   AV_PIX_FMT_BGRA,
      76             :         AV_PIX_FMT_ARGB,   AV_PIX_FMT_ABGR,
      77             :         AV_PIX_FMT_0RGB,   AV_PIX_FMT_0BGR,
      78             :         AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
      79             :         AV_PIX_FMT_RGB48,  AV_PIX_FMT_BGR48,
      80             :         AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
      81             :         AV_PIX_FMT_NONE
      82             :     };
      83             : 
      84           1 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
      85           1 :     if (!fmts_list)
      86           0 :         return AVERROR(ENOMEM);
      87           1 :     return ff_set_common_formats(ctx, fmts_list);
      88             : }
      89             : 
      90           1 : static int config_output(AVFilterLink *outlink)
      91             : {
      92           1 :     AVFilterContext *ctx = outlink->src;
      93           1 :     ColorChannelMixerContext *s = ctx->priv;
      94             :     int i, j, size, *buffer;
      95             : 
      96           1 :     ff_fill_rgba_map(s->rgba_map, outlink->format);
      97             : 
      98           1 :     switch (outlink->format) {
      99           0 :     case AV_PIX_FMT_RGB48:
     100             :     case AV_PIX_FMT_BGR48:
     101             :     case AV_PIX_FMT_RGBA64:
     102             :     case AV_PIX_FMT_BGRA64:
     103           0 :         size = 65536;
     104           0 :         break;
     105           1 :     default:
     106           1 :         size = 256;
     107             :     }
     108             : 
     109           1 :     s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
     110           1 :     if (!s->buffer)
     111           0 :         return AVERROR(ENOMEM);
     112             : 
     113           5 :     for (i = 0; i < 4; i++)
     114          20 :         for (j = 0; j < 4; j++, buffer += size)
     115          16 :             s->lut[i][j] = buffer;
     116             : 
     117         257 :     for (i = 0; i < size; i++) {
     118         256 :         s->lut[R][R][i] = lrint(i * s->rr);
     119         256 :         s->lut[R][G][i] = lrint(i * s->rg);
     120         256 :         s->lut[R][B][i] = lrint(i * s->rb);
     121         256 :         s->lut[R][A][i] = lrint(i * s->ra);
     122             : 
     123         256 :         s->lut[G][R][i] = lrint(i * s->gr);
     124         256 :         s->lut[G][G][i] = lrint(i * s->gg);
     125         256 :         s->lut[G][B][i] = lrint(i * s->gb);
     126         256 :         s->lut[G][A][i] = lrint(i * s->ga);
     127             : 
     128         256 :         s->lut[B][R][i] = lrint(i * s->br);
     129         256 :         s->lut[B][G][i] = lrint(i * s->bg);
     130         256 :         s->lut[B][B][i] = lrint(i * s->bb);
     131         256 :         s->lut[B][A][i] = lrint(i * s->ba);
     132             : 
     133         256 :         s->lut[A][R][i] = lrint(i * s->ar);
     134         256 :         s->lut[A][G][i] = lrint(i * s->ag);
     135         256 :         s->lut[A][B][i] = lrint(i * s->ab);
     136         256 :         s->lut[A][A][i] = lrint(i * s->aa);
     137             :     }
     138             : 
     139           1 :     return 0;
     140             : }
     141             : 
     142          50 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     143             : {
     144          50 :     AVFilterContext *ctx = inlink->dst;
     145          50 :     ColorChannelMixerContext *s = ctx->priv;
     146          50 :     AVFilterLink *outlink = ctx->outputs[0];
     147          50 :     const uint8_t roffset = s->rgba_map[R];
     148          50 :     const uint8_t goffset = s->rgba_map[G];
     149          50 :     const uint8_t boffset = s->rgba_map[B];
     150          50 :     const uint8_t aoffset = s->rgba_map[A];
     151          50 :     const uint8_t *srcrow = in->data[0];
     152             :     uint8_t *dstrow;
     153             :     AVFrame *out;
     154             :     int i, j;
     155             : 
     156          50 :     if (av_frame_is_writable(in)) {
     157          50 :         out = in;
     158             :     } else {
     159           0 :         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     160           0 :         if (!out) {
     161           0 :             av_frame_free(&in);
     162           0 :             return AVERROR(ENOMEM);
     163             :         }
     164           0 :         av_frame_copy_props(out, in);
     165             :     }
     166             : 
     167          50 :     dstrow = out->data[0];
     168          50 :     switch (outlink->format) {
     169          50 :     case AV_PIX_FMT_BGR24:
     170             :     case AV_PIX_FMT_RGB24:
     171       14450 :         for (i = 0; i < outlink->h; i++) {
     172       14400 :             const uint8_t *src = srcrow;
     173       14400 :             uint8_t *dst = dstrow;
     174             : 
     175     5083200 :             for (j = 0; j < outlink->w * 3; j += 3) {
     176     5068800 :                 const uint8_t rin = src[j + roffset];
     177     5068800 :                 const uint8_t gin = src[j + goffset];
     178     5068800 :                 const uint8_t bin = src[j + boffset];
     179             : 
     180    15206400 :                 dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
     181     5068800 :                                                  s->lut[R][G][gin] +
     182     5068800 :                                                  s->lut[R][B][bin]);
     183    15206400 :                 dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
     184     5068800 :                                                  s->lut[G][G][gin] +
     185     5068800 :                                                  s->lut[G][B][bin]);
     186    15206400 :                 dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
     187     5068800 :                                                  s->lut[B][G][gin] +
     188     5068800 :                                                  s->lut[B][B][bin]);
     189             :             }
     190             : 
     191       14400 :             srcrow += in->linesize[0];
     192       14400 :             dstrow += out->linesize[0];
     193             :         }
     194          50 :         break;
     195           0 :     case AV_PIX_FMT_0BGR:
     196             :     case AV_PIX_FMT_0RGB:
     197             :     case AV_PIX_FMT_BGR0:
     198             :     case AV_PIX_FMT_RGB0:
     199           0 :         for (i = 0; i < outlink->h; i++) {
     200           0 :             const uint8_t *src = srcrow;
     201           0 :             uint8_t *dst = dstrow;
     202             : 
     203           0 :             for (j = 0; j < outlink->w * 4; j += 4) {
     204           0 :                 const uint8_t rin = src[j + roffset];
     205           0 :                 const uint8_t gin = src[j + goffset];
     206           0 :                 const uint8_t bin = src[j + boffset];
     207             : 
     208           0 :                 dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
     209           0 :                                                  s->lut[R][G][gin] +
     210           0 :                                                  s->lut[R][B][bin]);
     211           0 :                 dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
     212           0 :                                                  s->lut[G][G][gin] +
     213           0 :                                                  s->lut[G][B][bin]);
     214           0 :                 dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
     215           0 :                                                  s->lut[B][G][gin] +
     216           0 :                                                  s->lut[B][B][bin]);
     217           0 :                 if (in != out)
     218           0 :                     dst[j + aoffset] = 0;
     219             :             }
     220             : 
     221           0 :             srcrow += in->linesize[0];
     222           0 :             dstrow += out->linesize[0];
     223             :         }
     224           0 :         break;
     225           0 :     case AV_PIX_FMT_ABGR:
     226             :     case AV_PIX_FMT_ARGB:
     227             :     case AV_PIX_FMT_BGRA:
     228             :     case AV_PIX_FMT_RGBA:
     229           0 :         for (i = 0; i < outlink->h; i++) {
     230           0 :             const uint8_t *src = srcrow;
     231           0 :             uint8_t *dst = dstrow;
     232             : 
     233           0 :             for (j = 0; j < outlink->w * 4; j += 4) {
     234           0 :                 const uint8_t rin = src[j + roffset];
     235           0 :                 const uint8_t gin = src[j + goffset];
     236           0 :                 const uint8_t bin = src[j + boffset];
     237           0 :                 const uint8_t ain = src[j + aoffset];
     238             : 
     239           0 :                 dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
     240           0 :                                                  s->lut[R][G][gin] +
     241           0 :                                                  s->lut[R][B][bin] +
     242           0 :                                                  s->lut[R][A][ain]);
     243           0 :                 dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
     244           0 :                                                  s->lut[G][G][gin] +
     245           0 :                                                  s->lut[G][B][bin] +
     246           0 :                                                  s->lut[G][A][ain]);
     247           0 :                 dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
     248           0 :                                                  s->lut[B][G][gin] +
     249           0 :                                                  s->lut[B][B][bin] +
     250           0 :                                                  s->lut[B][A][ain]);
     251           0 :                 dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
     252           0 :                                                  s->lut[A][G][gin] +
     253           0 :                                                  s->lut[A][B][bin] +
     254           0 :                                                  s->lut[A][A][ain]);
     255             :             }
     256             : 
     257           0 :             srcrow += in->linesize[0];
     258           0 :             dstrow += out->linesize[0];
     259             :         }
     260           0 :         break;
     261           0 :     case AV_PIX_FMT_BGR48:
     262             :     case AV_PIX_FMT_RGB48:
     263           0 :         for (i = 0; i < outlink->h; i++) {
     264           0 :             const uint16_t *src = (const uint16_t *)srcrow;
     265           0 :             uint16_t *dst = (uint16_t *)dstrow;
     266             : 
     267           0 :             for (j = 0; j < outlink->w * 3; j += 3) {
     268           0 :                 const uint16_t rin = src[j + roffset];
     269           0 :                 const uint16_t gin = src[j + goffset];
     270           0 :                 const uint16_t bin = src[j + boffset];
     271             : 
     272           0 :                 dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
     273           0 :                                                   s->lut[R][G][gin] +
     274           0 :                                                   s->lut[R][B][bin]);
     275           0 :                 dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
     276           0 :                                                   s->lut[G][G][gin] +
     277           0 :                                                   s->lut[G][B][bin]);
     278           0 :                 dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
     279           0 :                                                   s->lut[B][G][gin] +
     280           0 :                                                   s->lut[B][B][bin]);
     281             :             }
     282             : 
     283           0 :             srcrow += in->linesize[0];
     284           0 :             dstrow += out->linesize[0];
     285             :         }
     286           0 :         break;
     287           0 :     case AV_PIX_FMT_BGRA64:
     288             :     case AV_PIX_FMT_RGBA64:
     289           0 :         for (i = 0; i < outlink->h; i++) {
     290           0 :             const uint16_t *src = (const uint16_t *)srcrow;
     291           0 :             uint16_t *dst = (uint16_t *)dstrow;
     292             : 
     293           0 :             for (j = 0; j < outlink->w * 4; j += 4) {
     294           0 :                 const uint16_t rin = src[j + roffset];
     295           0 :                 const uint16_t gin = src[j + goffset];
     296           0 :                 const uint16_t bin = src[j + boffset];
     297           0 :                 const uint16_t ain = src[j + aoffset];
     298             : 
     299           0 :                 dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
     300           0 :                                                   s->lut[R][G][gin] +
     301           0 :                                                   s->lut[R][B][bin] +
     302           0 :                                                   s->lut[R][A][ain]);
     303           0 :                 dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
     304           0 :                                                   s->lut[G][G][gin] +
     305           0 :                                                   s->lut[G][B][bin] +
     306           0 :                                                   s->lut[G][A][ain]);
     307           0 :                 dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
     308           0 :                                                   s->lut[B][G][gin] +
     309           0 :                                                   s->lut[B][B][bin] +
     310           0 :                                                   s->lut[B][A][ain]);
     311           0 :                 dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
     312           0 :                                                   s->lut[A][G][gin] +
     313           0 :                                                   s->lut[A][B][bin] +
     314           0 :                                                   s->lut[A][A][ain]);
     315             :             }
     316             : 
     317           0 :             srcrow += in->linesize[0];
     318           0 :             dstrow += out->linesize[0];
     319             :         }
     320             :     }
     321             : 
     322          50 :     if (in != out)
     323           0 :         av_frame_free(&in);
     324          50 :     return ff_filter_frame(ctx->outputs[0], out);
     325             : }
     326             : 
     327           1 : static av_cold void uninit(AVFilterContext *ctx)
     328             : {
     329           1 :     ColorChannelMixerContext *s = ctx->priv;
     330             : 
     331           1 :     av_freep(&s->buffer);
     332           1 : }
     333             : 
     334             : static const AVFilterPad colorchannelmixer_inputs[] = {
     335             :     {
     336             :         .name         = "default",
     337             :         .type         = AVMEDIA_TYPE_VIDEO,
     338             :         .filter_frame = filter_frame,
     339             :     },
     340             :     { NULL }
     341             : };
     342             : 
     343             : static const AVFilterPad colorchannelmixer_outputs[] = {
     344             :     {
     345             :         .name         = "default",
     346             :         .type         = AVMEDIA_TYPE_VIDEO,
     347             :         .config_props = config_output,
     348             :     },
     349             :     { NULL }
     350             : };
     351             : 
     352             : AVFilter ff_vf_colorchannelmixer = {
     353             :     .name          = "colorchannelmixer",
     354             :     .description   = NULL_IF_CONFIG_SMALL("Adjust colors by mixing color channels."),
     355             :     .priv_size     = sizeof(ColorChannelMixerContext),
     356             :     .priv_class    = &colorchannelmixer_class,
     357             :     .uninit        = uninit,
     358             :     .query_formats = query_formats,
     359             :     .inputs        = colorchannelmixer_inputs,
     360             :     .outputs       = colorchannelmixer_outputs,
     361             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     362             : };

Generated by: LCOV version 1.13