FFmpeg
vf_showpalette.c
Go to the documentation of this file.
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;
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 
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  int ret = ff_formats_ref(ff_make_format_list(in_fmts),
49  &ctx->inputs[0]->outcfg.formats);
50  if (ret < 0)
51  return ret;
52 
53  return ff_formats_ref(ff_make_format_list(out_fmts),
54  &ctx->outputs[0]->incfg.formats);
55 }
56 
57 static int config_output(AVFilterLink *outlink)
58 {
59  AVFilterContext *ctx = outlink->src;
60  const ShowPaletteContext *s = ctx->priv;
61  outlink->w = outlink->h = 16 * s->size;
62  return 0;
63 }
64 
65 static void disp_palette(AVFrame *out, const AVFrame *in, int size)
66 {
67  int x, y, i, j;
68  uint32_t *dst = (uint32_t *)out->data[0];
69  const int dst_linesize = out->linesize[0] >> 2;
70  const uint32_t *pal = (uint32_t *)in->data[1];
71 
72  for (y = 0; y < 16; y++)
73  for (x = 0; x < 16; x++)
74  for (j = 0; j < size; j++)
75  for (i = 0; i < size; i++)
76  dst[(y*dst_linesize + x) * size + j*dst_linesize + i] = pal[y*16 + x];
77 }
78 
80 {
81  AVFrame *out;
82  AVFilterContext *ctx = inlink->dst;
83  const ShowPaletteContext *s = ctx->priv;
84  AVFilterLink *outlink = ctx->outputs[0];
85 
86  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
87  if (!out) {
88  av_frame_free(&in);
89  return AVERROR(ENOMEM);
90  }
92  disp_palette(out, in, s->size);
93  av_frame_free(&in);
94  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 
106  {
107  .name = "default",
108  .type = AVMEDIA_TYPE_VIDEO,
109  .config_props = config_output,
110  },
111 };
112 
114  .name = "showpalette",
115  .description = NULL_IF_CONFIG_SMALL("Display frame palette."),
116  .priv_size = sizeof(ShowPaletteContext),
120  .priv_class = &showpalette_class,
121 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:108
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:401
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:169
ShowPaletteContext::size
int size
Definition: vf_showpalette.c:32
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
ShowPaletteContext
Definition: vf_showpalette.c:30
FLAGS
#define FLAGS
Definition: vf_showpalette.c:36
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_showpalette.c:44
formats.h
disp_palette
static void disp_palette(AVFrame *out, const AVFrame *in, int size)
Definition: vf_showpalette.c:65
showpalette_outputs
static const AVFilterPad showpalette_outputs[]
Definition: vf_showpalette.c:105
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(showpalette)
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:617
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
ff_vf_showpalette
const AVFilter ff_vf_showpalette
Definition: vf_showpalette.c:113
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:736
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_showpalette.c:79
size
int size
Definition: twinvq_data.h:10344
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:441
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
showpalette_options
static const AVOption showpalette_options[]
Definition: vf_showpalette.c:37
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AVFilter
Filter definition.
Definition: avfilter.h:166
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_showpalette.c:57
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
showpalette_inputs
static const AVFilterPad showpalette_inputs[]
Definition: vf_showpalette.c:97
OFFSET
#define OFFSET(x)
Definition: vf_showpalette.c:35