2 * This file is part of FFmpeg.
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.
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.
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
21 * Display frame palette (AV_PIX_FMT_PAL8)
24 #include "libavutil/avassert.h"
25 #include "libavutil/opt.h"
31 typedef struct ShowPaletteContext {
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 },
43 AVFILTER_DEFINE_CLASS(showpalette);
45 static int query_formats(AVFilterContext *ctx)
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 = ff_formats_ref(ff_make_format_list(in_fmts),
50 &ctx->inputs[0]->outcfg.formats);
54 return ff_formats_ref(ff_make_format_list(out_fmts),
55 &ctx->outputs[0]->incfg.formats);
58 static int config_output(AVFilterLink *outlink)
60 AVFilterContext *ctx = outlink->src;
61 const ShowPaletteContext *s = ctx->priv;
62 outlink->w = outlink->h = 16 * s->size;
66 static void disp_palette(AVFrame *out, const AVFrame *in, int size)
69 uint32_t *dst = (uint32_t *)out->data[0];
70 const int dst_linesize = out->linesize[0] >> 2;
71 const uint32_t *pal = (uint32_t *)in->data[1];
73 for (y = 0; y < 16; y++)
74 for (x = 0; x < 16; x++)
75 for (j = 0; j < size; j++)
76 for (i = 0; i < size; i++)
77 dst[(y*dst_linesize + x) * size + j*dst_linesize + i] = pal[y*16 + x];
80 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
83 AVFilterContext *ctx = inlink->dst;
84 const ShowPaletteContext *s = ctx->priv;
85 AVFilterLink *outlink = ctx->outputs[0];
87 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
90 return AVERROR(ENOMEM);
92 av_frame_copy_props(out, in);
93 disp_palette(out, in, s->size);
95 return ff_filter_frame(outlink, out);
98 static const AVFilterPad showpalette_inputs[] = {
101 .type = AVMEDIA_TYPE_VIDEO,
102 .filter_frame = filter_frame,
107 static const AVFilterPad showpalette_outputs[] = {
110 .type = AVMEDIA_TYPE_VIDEO,
111 .config_props = config_output,
116 const AVFilter ff_vf_showpalette = {
117 .name = "showpalette",
118 .description = NULL_IF_CONFIG_SMALL("Display frame palette."),
119 .priv_size = sizeof(ShowPaletteContext),
120 .query_formats = query_formats,
121 .inputs = showpalette_inputs,
122 .outputs = showpalette_outputs,
123 .priv_class = &showpalette_class,