FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_showpalette.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 31 34 91.2%
Functions: 4 4 100.0%
Branches: 10 12 83.3%

Line Branch Exec Source
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/opt.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29
30 typedef struct ShowPaletteContext {
31 const AVClass *class;
32 int size;
33 } ShowPaletteContext;
34
35 #define OFFSET(x) offsetof(ShowPaletteContext, x)
36 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
37 static const AVOption showpalette_options[] = {
38 { "s", "set pixel box size", OFFSET(size), AV_OPT_TYPE_INT, {.i64=30}, 1, 100, FLAGS },
39 { NULL }
40 };
41
42 AVFILTER_DEFINE_CLASS(showpalette);
43
44 1 static int query_formats(AVFilterContext *ctx)
45 {
46 static const enum AVPixelFormat in_fmts[] = {AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE};
47 static const enum AVPixelFormat out_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
48 1 int ret = ff_formats_ref(ff_make_format_list(in_fmts),
49 1 &ctx->inputs[0]->outcfg.formats);
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
51 return ret;
52
53 1 return ff_formats_ref(ff_make_format_list(out_fmts),
54 1 &ctx->outputs[0]->incfg.formats);
55 }
56
57 1 static int config_output(AVFilterLink *outlink)
58 {
59 1 AVFilterContext *ctx = outlink->src;
60 1 const ShowPaletteContext *s = ctx->priv;
61 1 outlink->w = outlink->h = 16 * s->size;
62 1 return 0;
63 }
64
65 315 static void disp_palette(AVFrame *out, const AVFrame *in, int size)
66 {
67 int x, y, i, j;
68 315 uint32_t *dst = (uint32_t *)out->data[0];
69 315 const int dst_linesize = out->linesize[0] >> 2;
70 315 const uint32_t *pal = (uint32_t *)in->data[1];
71
72
2/2
✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 315 times.
5355 for (y = 0; y < 16; y++)
73
2/2
✓ Branch 0 taken 80640 times.
✓ Branch 1 taken 5040 times.
85680 for (x = 0; x < 16; x++)
74
2/2
✓ Branch 0 taken 241920 times.
✓ Branch 1 taken 80640 times.
322560 for (j = 0; j < size; j++)
75
2/2
✓ Branch 0 taken 725760 times.
✓ Branch 1 taken 241920 times.
967680 for (i = 0; i < size; i++)
76 725760 dst[(y*dst_linesize + x) * size + j*dst_linesize + i] = pal[y*16 + x];
77 315 }
78
79 315 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
80 {
81 AVFrame *out;
82 315 AVFilterContext *ctx = inlink->dst;
83 315 const ShowPaletteContext *s = ctx->priv;
84 315 AVFilterLink *outlink = ctx->outputs[0];
85
86 315 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
315 if (!out) {
88 av_frame_free(&in);
89 return AVERROR(ENOMEM);
90 }
91 315 av_frame_copy_props(out, in);
92 315 disp_palette(out, in, s->size);
93 315 av_frame_free(&in);
94 315 return ff_filter_frame(outlink, out);
95 }
96
97 static const AVFilterPad showpalette_inputs[] = {
98 {
99 .name = "default",
100 .type = AVMEDIA_TYPE_VIDEO,
101 .filter_frame = filter_frame,
102 },
103 };
104
105 static const AVFilterPad showpalette_outputs[] = {
106 {
107 .name = "default",
108 .type = AVMEDIA_TYPE_VIDEO,
109 .config_props = config_output,
110 },
111 };
112
113 const AVFilter ff_vf_showpalette = {
114 .name = "showpalette",
115 .description = NULL_IF_CONFIG_SMALL("Display frame palette."),
116 .priv_size = sizeof(ShowPaletteContext),
117 FILTER_INPUTS(showpalette_inputs),
118 FILTER_OUTPUTS(showpalette_outputs),
119 FILTER_QUERY_FUNC(query_formats),
120 .priv_class = &showpalette_class,
121 };
122