LCOV - code coverage report
Current view: top level - libavfilter - vf_colorbalance.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 72 0.0 %
Date: 2017-12-16 13:57:32 Functions: 0 3 0.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 "libavutil/pixdesc.h"
      23             : #include "avfilter.h"
      24             : #include "drawutils.h"
      25             : #include "formats.h"
      26             : #include "internal.h"
      27             : #include "video.h"
      28             : 
      29             : #define R 0
      30             : #define G 1
      31             : #define B 2
      32             : #define A 3
      33             : 
      34             : typedef struct Range {
      35             :     double shadows;
      36             :     double midtones;
      37             :     double highlights;
      38             : } Range;
      39             : 
      40             : typedef struct ColorBalanceContext {
      41             :     const AVClass *class;
      42             :     Range cyan_red;
      43             :     Range magenta_green;
      44             :     Range yellow_blue;
      45             : 
      46             :     uint8_t lut[3][256];
      47             : 
      48             :     uint8_t rgba_map[4];
      49             :     int step;
      50             : } ColorBalanceContext;
      51             : 
      52             : #define OFFSET(x) offsetof(ColorBalanceContext, x)
      53             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      54             : static const AVOption colorbalance_options[] = {
      55             :     { "rs", "set red shadows",      OFFSET(cyan_red.shadows),         AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      56             :     { "gs", "set green shadows",    OFFSET(magenta_green.shadows),    AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      57             :     { "bs", "set blue shadows",     OFFSET(yellow_blue.shadows),      AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      58             :     { "rm", "set red midtones",     OFFSET(cyan_red.midtones),        AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      59             :     { "gm", "set green midtones",   OFFSET(magenta_green.midtones),   AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      60             :     { "bm", "set blue midtones",    OFFSET(yellow_blue.midtones),     AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      61             :     { "rh", "set red highlights",   OFFSET(cyan_red.highlights),      AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      62             :     { "gh", "set green highlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      63             :     { "bh", "set blue highlights",  OFFSET(yellow_blue.highlights),   AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
      64             :     { NULL }
      65             : };
      66             : 
      67             : AVFILTER_DEFINE_CLASS(colorbalance);
      68             : 
      69           0 : static int query_formats(AVFilterContext *ctx)
      70             : {
      71             :     static const enum AVPixelFormat pix_fmts[] = {
      72             :         AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
      73             :         AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
      74             :         AV_PIX_FMT_ABGR,  AV_PIX_FMT_ARGB,
      75             :         AV_PIX_FMT_0BGR,  AV_PIX_FMT_0RGB,
      76             :         AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
      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 int config_output(AVFilterLink *outlink)
      86             : {
      87           0 :     AVFilterContext *ctx = outlink->src;
      88           0 :     ColorBalanceContext *s = ctx->priv;
      89           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
      90             :     double *shadows, *midtones, *highlights, *buffer;
      91             :     int i, r, g, b;
      92             : 
      93           0 :     buffer = av_malloc(256 * 3 * sizeof(*buffer));
      94           0 :     if (!buffer)
      95           0 :         return AVERROR(ENOMEM);
      96             : 
      97           0 :     shadows    = buffer + 256 * 0;
      98           0 :     midtones   = buffer + 256 * 1;
      99           0 :     highlights = buffer + 256 * 2;
     100             : 
     101           0 :     for (i = 0; i < 256; i++) {
     102           0 :         double low = av_clipd((i - 85.0) / -64.0 + 0.5, 0, 1) * 178.5;
     103           0 :         double mid = av_clipd((i - 85.0) /  64.0 + 0.5, 0, 1) *
     104           0 :                      av_clipd((i + 85.0 - 255.0) / -64.0 + 0.5, 0, 1) * 178.5;
     105             : 
     106           0 :         shadows[i] = low;
     107           0 :         midtones[i] = mid;
     108           0 :         highlights[255 - i] = low;
     109             :     }
     110             : 
     111           0 :     for (i = 0; i < 256; i++) {
     112           0 :         r = g = b = i;
     113             : 
     114           0 :         r = av_clip_uint8(r + s->cyan_red.shadows         * shadows[r]);
     115           0 :         r = av_clip_uint8(r + s->cyan_red.midtones        * midtones[r]);
     116           0 :         r = av_clip_uint8(r + s->cyan_red.highlights      * highlights[r]);
     117             : 
     118           0 :         g = av_clip_uint8(g + s->magenta_green.shadows    * shadows[g]);
     119           0 :         g = av_clip_uint8(g + s->magenta_green.midtones   * midtones[g]);
     120           0 :         g = av_clip_uint8(g + s->magenta_green.highlights * highlights[g]);
     121             : 
     122           0 :         b = av_clip_uint8(b + s->yellow_blue.shadows      * shadows[b]);
     123           0 :         b = av_clip_uint8(b + s->yellow_blue.midtones     * midtones[b]);
     124           0 :         b = av_clip_uint8(b + s->yellow_blue.highlights   * highlights[b]);
     125             : 
     126           0 :         s->lut[R][i] = r;
     127           0 :         s->lut[G][i] = g;
     128           0 :         s->lut[B][i] = b;
     129             :     }
     130             : 
     131           0 :     av_free(buffer);
     132             : 
     133           0 :     ff_fill_rgba_map(s->rgba_map, outlink->format);
     134           0 :     s->step = av_get_padded_bits_per_pixel(desc) >> 3;
     135             : 
     136           0 :     return 0;
     137             : }
     138             : 
     139           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     140             : {
     141           0 :     AVFilterContext *ctx = inlink->dst;
     142           0 :     ColorBalanceContext *s = ctx->priv;
     143           0 :     AVFilterLink *outlink = ctx->outputs[0];
     144           0 :     const uint8_t roffset = s->rgba_map[R];
     145           0 :     const uint8_t goffset = s->rgba_map[G];
     146           0 :     const uint8_t boffset = s->rgba_map[B];
     147           0 :     const uint8_t aoffset = s->rgba_map[A];
     148           0 :     const int step = s->step;
     149           0 :     const uint8_t *srcrow = in->data[0];
     150             :     uint8_t *dstrow;
     151             :     AVFrame *out;
     152             :     int i, j;
     153             : 
     154           0 :     if (av_frame_is_writable(in)) {
     155           0 :         out = in;
     156             :     } else {
     157           0 :         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     158           0 :         if (!out) {
     159           0 :             av_frame_free(&in);
     160           0 :             return AVERROR(ENOMEM);
     161             :         }
     162           0 :         av_frame_copy_props(out, in);
     163             :     }
     164             : 
     165           0 :     dstrow = out->data[0];
     166           0 :     for (i = 0; i < outlink->h; i++) {
     167           0 :         const uint8_t *src = srcrow;
     168           0 :         uint8_t *dst = dstrow;
     169             : 
     170           0 :         for (j = 0; j < outlink->w * step; j += step) {
     171           0 :             dst[j + roffset] = s->lut[R][src[j + roffset]];
     172           0 :             dst[j + goffset] = s->lut[G][src[j + goffset]];
     173           0 :             dst[j + boffset] = s->lut[B][src[j + boffset]];
     174           0 :             if (in != out && step == 4)
     175           0 :                 dst[j + aoffset] = src[j + aoffset];
     176             :         }
     177             : 
     178           0 :         srcrow += in->linesize[0];
     179           0 :         dstrow += out->linesize[0];
     180             :     }
     181             : 
     182           0 :     if (in != out)
     183           0 :         av_frame_free(&in);
     184           0 :     return ff_filter_frame(ctx->outputs[0], out);
     185             : }
     186             : 
     187             : static const AVFilterPad colorbalance_inputs[] = {
     188             :     {
     189             :         .name         = "default",
     190             :         .type         = AVMEDIA_TYPE_VIDEO,
     191             :         .filter_frame = filter_frame,
     192             :     },
     193             :     { NULL }
     194             : };
     195             : 
     196             : static const AVFilterPad colorbalance_outputs[] = {
     197             :     {
     198             :         .name         = "default",
     199             :         .type         = AVMEDIA_TYPE_VIDEO,
     200             :         .config_props = config_output,
     201             :     },
     202             :     { NULL }
     203             : };
     204             : 
     205             : AVFilter ff_vf_colorbalance = {
     206             :     .name          = "colorbalance",
     207             :     .description   = NULL_IF_CONFIG_SMALL("Adjust the color balance."),
     208             :     .priv_size     = sizeof(ColorBalanceContext),
     209             :     .priv_class    = &colorbalance_class,
     210             :     .query_formats = query_formats,
     211             :     .inputs        = colorbalance_inputs,
     212             :     .outputs       = colorbalance_outputs,
     213             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     214             : };

Generated by: LCOV version 1.13