LCOV - code coverage report
Current view: top level - libavfilter - vf_elbg.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 93 0.0 %
Date: 2017-12-10 21:22:29 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Stefano Sabatini
       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             : /**
      22             :  * @file
      23             :  * video quantizer filter based on ELBG
      24             :  */
      25             : 
      26             : #include "libavcodec/elbg.h"
      27             : #include "libavutil/opt.h"
      28             : #include "libavutil/pixdesc.h"
      29             : #include "libavutil/random_seed.h"
      30             : 
      31             : #include "avfilter.h"
      32             : #include "drawutils.h"
      33             : #include "internal.h"
      34             : #include "video.h"
      35             : 
      36             : typedef struct ELBGContext {
      37             :     const AVClass *class;
      38             :     AVLFG lfg;
      39             :     unsigned int lfg_seed;
      40             :     int max_steps_nb;
      41             :     int *codeword;
      42             :     int codeword_length;
      43             :     int *codeword_closest_codebook_idxs;
      44             :     int *codebook;
      45             :     int codebook_length;
      46             :     const AVPixFmtDescriptor *pix_desc;
      47             :     uint8_t rgba_map[4];
      48             :     int pal8;
      49             : } ELBGContext;
      50             : 
      51             : #define OFFSET(x) offsetof(ELBGContext, x)
      52             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      53             : 
      54             : static const AVOption elbg_options[] = {
      55             :     { "codebook_length", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
      56             :     { "l",               "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
      57             :     { "nb_steps", "set max number of steps used to compute the mapping", OFFSET(max_steps_nb), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
      58             :     { "n",        "set max number of steps used to compute the mapping", OFFSET(max_steps_nb), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
      59             :     { "seed", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
      60             :     { "s",    "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT32_MAX, FLAGS },
      61             :     { "pal8", "set the pal8 output", OFFSET(pal8), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
      62             :     { NULL }
      63             : };
      64             : 
      65             : AVFILTER_DEFINE_CLASS(elbg);
      66             : 
      67           0 : static av_cold int init(AVFilterContext *ctx)
      68             : {
      69           0 :     ELBGContext *elbg = ctx->priv;
      70             : 
      71           0 :     if (elbg->pal8 && elbg->codebook_length > 256) {
      72           0 :         av_log(ctx, AV_LOG_ERROR, "pal8 output allows max 256 codebook length.\n");
      73           0 :         return AVERROR(EINVAL);
      74             :     }
      75             : 
      76           0 :     if (elbg->lfg_seed == -1)
      77           0 :         elbg->lfg_seed = av_get_random_seed();
      78             : 
      79           0 :     av_lfg_init(&elbg->lfg, elbg->lfg_seed);
      80           0 :     return 0;
      81             : }
      82             : 
      83           0 : static int query_formats(AVFilterContext *ctx)
      84             : {
      85           0 :     ELBGContext *elbg = ctx->priv;
      86             :     int ret;
      87             : 
      88             :     static const enum AVPixelFormat pix_fmts[] = {
      89             :         AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
      90             :         AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
      91             :         AV_PIX_FMT_NONE
      92             :     };
      93           0 :     if (!elbg->pal8) {
      94           0 :         AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
      95           0 :         if (!fmts_list)
      96           0 :             return AVERROR(ENOMEM);
      97           0 :         return ff_set_common_formats(ctx, fmts_list);
      98             :     } else {
      99             :         static const enum AVPixelFormat pal8_fmt[] = {
     100             :             AV_PIX_FMT_PAL8,
     101             :             AV_PIX_FMT_NONE
     102             :         };
     103           0 :         if ((ret = ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->inputs[0]->out_formats)) < 0 ||
     104           0 :             (ret = ff_formats_ref(ff_make_format_list(pal8_fmt), &ctx->outputs[0]->in_formats)) < 0)
     105           0 :             return ret;
     106             :     }
     107           0 :     return 0;
     108             : }
     109             : 
     110             : #define NB_COMPONENTS 3
     111             : 
     112           0 : static int config_input(AVFilterLink *inlink)
     113             : {
     114           0 :     AVFilterContext *ctx = inlink->dst;
     115           0 :     ELBGContext *elbg = ctx->priv;
     116             : 
     117           0 :     elbg->pix_desc = av_pix_fmt_desc_get(inlink->format);
     118           0 :     elbg->codeword_length = inlink->w * inlink->h;
     119           0 :     elbg->codeword = av_realloc_f(elbg->codeword, elbg->codeword_length,
     120             :                                   NB_COMPONENTS * sizeof(*elbg->codeword));
     121           0 :     if (!elbg->codeword)
     122           0 :         return AVERROR(ENOMEM);
     123             : 
     124           0 :     elbg->codeword_closest_codebook_idxs =
     125           0 :         av_realloc_f(elbg->codeword_closest_codebook_idxs, elbg->codeword_length,
     126             :                      sizeof(*elbg->codeword_closest_codebook_idxs));
     127           0 :     if (!elbg->codeword_closest_codebook_idxs)
     128           0 :         return AVERROR(ENOMEM);
     129             : 
     130           0 :     elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
     131             :                                   NB_COMPONENTS * sizeof(*elbg->codebook));
     132           0 :     if (!elbg->codebook)
     133           0 :         return AVERROR(ENOMEM);
     134             : 
     135           0 :     ff_fill_rgba_map(elbg->rgba_map, inlink->format);
     136             : 
     137           0 :     return 0;
     138             : }
     139             : 
     140             : #define R 0
     141             : #define G 1
     142             : #define B 2
     143             : 
     144           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     145             : {
     146           0 :     ELBGContext *elbg = inlink->dst->priv;
     147             :     int i, j, k;
     148             :     uint8_t *p, *p0;
     149             : 
     150           0 :     const uint8_t r_idx  = elbg->rgba_map[R];
     151           0 :     const uint8_t g_idx  = elbg->rgba_map[G];
     152           0 :     const uint8_t b_idx  = elbg->rgba_map[B];
     153             : 
     154             :     /* build the codeword */
     155           0 :     p0 = frame->data[0];
     156           0 :     k = 0;
     157           0 :     for (i = 0; i < inlink->h; i++) {
     158           0 :         p = p0;
     159           0 :         for (j = 0; j < inlink->w; j++) {
     160           0 :             elbg->codeword[k++] = p[r_idx];
     161           0 :             elbg->codeword[k++] = p[g_idx];
     162           0 :             elbg->codeword[k++] = p[b_idx];
     163           0 :             p += elbg->pix_desc->nb_components;
     164             :         }
     165           0 :         p0 += frame->linesize[0];
     166             :     }
     167             : 
     168             :     /* compute the codebook */
     169           0 :     avpriv_init_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
     170             :                      elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
     171             :                      elbg->codeword_closest_codebook_idxs, &elbg->lfg);
     172           0 :     avpriv_do_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
     173             :                    elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
     174             :                    elbg->codeword_closest_codebook_idxs, &elbg->lfg);
     175             : 
     176           0 :     if (elbg->pal8) {
     177           0 :         AVFilterLink *outlink = inlink->dst->outputs[0];
     178           0 :         AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     179             :         uint32_t *pal;
     180             : 
     181           0 :         if (!out)
     182           0 :             return AVERROR(ENOMEM);
     183           0 :         out->pts = frame->pts;
     184           0 :         av_frame_free(&frame);
     185           0 :         pal = (uint32_t *)out->data[1];
     186           0 :         p0 = (uint8_t *)out->data[0];
     187             : 
     188           0 :         for (i = 0; i < elbg->codebook_length; i++) {
     189           0 :             pal[i] =  0xFFU                 << 24  |
     190           0 :                      (elbg->codebook[i*3  ] << 16) |
     191           0 :                      (elbg->codebook[i*3+1] <<  8) |
     192           0 :                       elbg->codebook[i*3+2];
     193             :         }
     194             : 
     195           0 :         k = 0;
     196           0 :         for (i = 0; i < inlink->h; i++) {
     197           0 :             p = p0;
     198           0 :             for (j = 0; j < inlink->w; j++, p++) {
     199           0 :                 p[0] = elbg->codeword_closest_codebook_idxs[k++];
     200             :             }
     201           0 :             p0 += out->linesize[0];
     202             :         }
     203             : 
     204           0 :         return ff_filter_frame(outlink, out);
     205             :     }
     206             : 
     207             :     /* fill the output with the codebook values */
     208           0 :     p0 = frame->data[0];
     209             : 
     210           0 :     k = 0;
     211           0 :     for (i = 0; i < inlink->h; i++) {
     212           0 :         p = p0;
     213           0 :         for (j = 0; j < inlink->w; j++) {
     214           0 :             int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
     215           0 :             p[r_idx] = elbg->codebook[cb_idx];
     216           0 :             p[g_idx] = elbg->codebook[cb_idx+1];
     217           0 :             p[b_idx] = elbg->codebook[cb_idx+2];
     218           0 :             p += elbg->pix_desc->nb_components;
     219             :         }
     220           0 :         p0 += frame->linesize[0];
     221             :     }
     222             : 
     223           0 :     return ff_filter_frame(inlink->dst->outputs[0], frame);
     224             : }
     225             : 
     226           0 : static av_cold void uninit(AVFilterContext *ctx)
     227             : {
     228           0 :     ELBGContext *elbg = ctx->priv;
     229             : 
     230           0 :     av_freep(&elbg->codebook);
     231           0 :     av_freep(&elbg->codeword);
     232           0 :     av_freep(&elbg->codeword_closest_codebook_idxs);
     233           0 : }
     234             : 
     235             : static const AVFilterPad elbg_inputs[] = {
     236             :     {
     237             :         .name           = "default",
     238             :         .type           = AVMEDIA_TYPE_VIDEO,
     239             :         .config_props   = config_input,
     240             :         .filter_frame   = filter_frame,
     241             :         .needs_writable = 1,
     242             :     },
     243             :     { NULL }
     244             : };
     245             : 
     246             : static const AVFilterPad elbg_outputs[] = {
     247             :     {
     248             :         .name = "default",
     249             :         .type = AVMEDIA_TYPE_VIDEO,
     250             :     },
     251             :     { NULL }
     252             : };
     253             : 
     254             : AVFilter ff_vf_elbg = {
     255             :     .name          = "elbg",
     256             :     .description   = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
     257             :     .priv_size     = sizeof(ELBGContext),
     258             :     .priv_class    = &elbg_class,
     259             :     .query_formats = query_formats,
     260             :     .init          = init,
     261             :     .uninit        = uninit,
     262             :     .inputs        = elbg_inputs,
     263             :     .outputs       = elbg_outputs,
     264             : };

Generated by: LCOV version 1.13