LCOV - code coverage report
Current view: top level - libavfilter - vf_showpalette.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 44 72.7 %
Date: 2017-12-18 20:14:19 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : /**
      20             :  * @file
      21             :  * Display frame palette (AV_PIX_FMT_PAL8)
      22             :  */
      23             : 
      24             : #include "libavutil/avassert.h"
      25             : #include "libavutil/opt.h"
      26             : #include "avfilter.h"
      27             : #include "formats.h"
      28             : #include "internal.h"
      29             : #include "video.h"
      30             : 
      31             : typedef struct ShowPaletteContext {
      32             :     const AVClass *class;
      33             :     int size;
      34             : } ShowPaletteContext;
      35             : 
      36             : #define OFFSET(x) offsetof(ShowPaletteContext, x)
      37             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      38             : static const AVOption showpalette_options[] = {
      39             :     { "s", "set pixel box size", OFFSET(size), AV_OPT_TYPE_INT, {.i64=30}, 1, 100, FLAGS },
      40             :     { NULL }
      41             : };
      42             : 
      43             : AVFILTER_DEFINE_CLASS(showpalette);
      44             : 
      45           1 : static int query_formats(AVFilterContext *ctx)
      46             : {
      47             :     static const enum AVPixelFormat in_fmts[]  = {AV_PIX_FMT_PAL8,  AV_PIX_FMT_NONE};
      48             :     static const enum AVPixelFormat out_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
      49             :     int ret;
      50           1 :     AVFilterFormats *in  = ff_make_format_list(in_fmts);
      51           1 :     AVFilterFormats *out = ff_make_format_list(out_fmts);
      52           1 :     if (!in || !out) {
      53           0 :         ret = AVERROR(ENOMEM);
      54           0 :         goto fail;
      55             :     }
      56             : 
      57           2 :     if ((ret = ff_formats_ref(in , &ctx->inputs[0]->out_formats)) < 0 ||
      58           1 :         (ret = ff_formats_ref(out, &ctx->outputs[0]->in_formats)) < 0)
      59             :         goto fail;
      60           1 :     return 0;
      61           0 : fail:
      62           0 :     if (in)
      63           0 :         av_freep(&in->formats);
      64           0 :     av_freep(&in);
      65           0 :     if (out)
      66           0 :         av_freep(&out->formats);
      67           0 :     av_freep(&out);
      68           0 :     return ret;
      69             : }
      70             : 
      71           1 : static int config_output(AVFilterLink *outlink)
      72             : {
      73           1 :     AVFilterContext *ctx = outlink->src;
      74           1 :     const ShowPaletteContext *s = ctx->priv;
      75           1 :     outlink->w = outlink->h = 16 * s->size;
      76           1 :     return 0;
      77             : }
      78             : 
      79         315 : static int disp_palette(AVFrame *out, const AVFrame *in, int size)
      80             : {
      81             :     int x, y, i, j;
      82         315 :     uint32_t *dst = (uint32_t *)out->data[0];
      83         315 :     const int dst_linesize = out->linesize[0] >> 2;
      84         315 :     const uint32_t *pal = (uint32_t *)in->data[1];
      85             : 
      86        5355 :     for (y = 0; y < 16; y++)
      87       85680 :         for (x = 0; x < 16; x++)
      88      322560 :             for (j = 0; j < size; j++)
      89      967680 :                 for (i = 0; i < size; i++)
      90      725760 :                     dst[(y*dst_linesize + x) * size + j*dst_linesize + i] = pal[y*16 + x];
      91         315 :     return 0;
      92             : }
      93             : 
      94         315 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
      95             : {
      96             :     int ret;
      97             :     AVFrame *out;
      98         315 :     AVFilterContext *ctx = inlink->dst;
      99         315 :     const ShowPaletteContext *s= ctx->priv;
     100         315 :     AVFilterLink *outlink = ctx->outputs[0];
     101             : 
     102         315 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     103         315 :     if (!out) {
     104           0 :         av_frame_free(&in);
     105           0 :         return AVERROR(ENOMEM);
     106             :     }
     107         315 :     av_frame_copy_props(out, in);
     108         315 :     ret = disp_palette(out, in, s->size);
     109         315 :     av_frame_free(&in);
     110         315 :     return ret < 0 ? ret : ff_filter_frame(outlink, out);
     111             : }
     112             : 
     113             : static const AVFilterPad showpalette_inputs[] = {
     114             :     {
     115             :         .name         = "default",
     116             :         .type         = AVMEDIA_TYPE_VIDEO,
     117             :         .filter_frame = filter_frame,
     118             :     },
     119             :     { NULL }
     120             : };
     121             : 
     122             : static const AVFilterPad showpalette_outputs[] = {
     123             :     {
     124             :         .name         = "default",
     125             :         .type         = AVMEDIA_TYPE_VIDEO,
     126             :         .config_props = config_output,
     127             :     },
     128             :     { NULL }
     129             : };
     130             : 
     131             : AVFilter ff_vf_showpalette = {
     132             :     .name          = "showpalette",
     133             :     .description   = NULL_IF_CONFIG_SMALL("Display frame palette."),
     134             :     .priv_size     = sizeof(ShowPaletteContext),
     135             :     .query_formats = query_formats,
     136             :     .inputs        = showpalette_inputs,
     137             :     .outputs       = showpalette_outputs,
     138             :     .priv_class    = &showpalette_class,
     139             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     140             : };

Generated by: LCOV version 1.13