FFmpeg
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
vf_palettegen.c File Reference
#include "libavutil/avassert.h"
#include "libavutil/internal.h"
#include "libavutil/opt.h"
#include "libavutil/intreadwrite.h"
#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "palette.h"
#include "video.h"

Go to the source code of this file.

Data Structures

struct  color_ref
 
struct  range_box
 
struct  hist_node
 
struct  PaletteGenContext
 

Macros

#define HIST_SIZE   (1<<15)
 
#define OFFSET(x)   offsetof(PaletteGenContext, x)
 
#define FLAGS   AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
#define DECLARE_CMP_FUNC(k0, k1, k2)
 

Typedefs

typedef int(* cmp_func) (const void *, const void *)
 

Enumerations

enum  { STATS_MODE_ALL_FRAMES, STATS_MODE_DIFF_FRAMES, STATS_MODE_SINGLE_FRAMES, NB_STATS_MODE }
 
enum  {
  ID_XYZ, ID_XZY, ID_ZXY, ID_YXZ,
  ID_ZYX, ID_YZX
}
 

Functions

 AVFILTER_DEFINE_CLASS (palettegen)
 
static int query_formats (AVFilterContext *ctx)
 
static int sort3id (int64_t x, int64_t y, int64_t z)
 
static int cmp_color (const void *a, const void *b)
 Simple color comparison for sorting the final palette. More...
 
static void compute_box_stats (PaletteGenContext *s, struct range_box *box)
 
static int get_next_box_id_to_split (PaletteGenContext *s)
 Find the next box to split: pick the one with the highest cut score. More...
 
static void split_box (PaletteGenContext *s, struct range_box *box, int n)
 Split given box in two at position n. More...
 
static void write_palette (AVFilterContext *ctx, AVFrame *out)
 Write the palette into the output frame. More...
 
static struct color_ref ** load_color_refs (const struct hist_node *hist, int nb_refs)
 Crawl the histogram to get all the defined colors, and create a linear list of them (each color reference entry is a pointer to the value in the histogram/hash table). More...
 
static double set_colorquant_ratio_meta (AVFrame *out, int nb_out, int nb_in)
 
static AVFrameget_palette_frame (AVFilterContext *ctx)
 Main function implementing the Median Cut Algorithm defined by Paul Heckbert in Color Image Quantization for Frame Buffer Display (1982) More...
 
static int color_inc (struct hist_node *hist, uint32_t color)
 Locate the color in the hash table and increment its counter. More...
 
static int update_histogram_diff (struct hist_node *hist, const AVFrame *f1, const AVFrame *f2)
 Update histogram when pixels differ from previous frame. More...
 
static int update_histogram_frame (struct hist_node *hist, const AVFrame *f)
 Simple histogram of the frame. More...
 
static int filter_frame (AVFilterLink *inlink, AVFrame *in)
 Update the histogram for each passing frame. More...
 
static int request_frame (AVFilterLink *outlink)
 Returns only one frame at the end containing the full palette. More...
 
static int config_output (AVFilterLink *outlink)
 The output is one simple 16x16 squared-pixels palette. More...
 
static int init (AVFilterContext *ctx)
 
static av_cold void uninit (AVFilterContext *ctx)
 

Variables

static const AVOption palettegen_options []
 
static const char *const sortstr [] = { "Lab", "Lba", "bLa", "aLb", "baL", "abL" }
 
static const cmp_func cmp_funcs []
 
static const AVFilterPad palettegen_inputs []
 
static const AVFilterPad palettegen_outputs []
 
const AVFilter ff_vf_palettegen
 

Detailed Description

Generate one palette for a whole video stream.

Definition in file vf_palettegen.c.

Macro Definition Documentation

◆ HIST_SIZE

#define HIST_SIZE   (1<<15)

Definition at line 68 of file vf_palettegen.c.

◆ OFFSET

#define OFFSET (   x)    offsetof(PaletteGenContext, x)

Definition at line 87 of file vf_palettegen.c.

◆ FLAGS

Definition at line 88 of file vf_palettegen.c.

◆ DECLARE_CMP_FUNC

#define DECLARE_CMP_FUNC (   k0,
  k1,
  k2 
)
Value:
static int cmp_##k0##k1##k2(const void *pa, const void *pb) \
{ \
const struct color_ref * const *a = pa; \
const struct color_ref * const *b = pb; \
const int c0 = FFDIFFSIGN((*a)->lab.k0, (*b)->lab.k0); \
const int c1 = FFDIFFSIGN((*a)->lab.k1, (*b)->lab.k1); \
const int c2 = FFDIFFSIGN((*a)->lab.k2, (*b)->lab.k2); \
return c0 ? c0 : c1 ? c1 : c2; \
}

Definition at line 117 of file vf_palettegen.c.

Typedef Documentation

◆ cmp_func

typedef int(* cmp_func) (const void *, const void *)

Definition at line 115 of file vf_palettegen.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
STATS_MODE_ALL_FRAMES 
STATS_MODE_DIFF_FRAMES 
STATS_MODE_SINGLE_FRAMES 
NB_STATS_MODE 

Definition at line 61 of file vf_palettegen.c.

◆ anonymous enum

anonymous enum
Enumerator
ID_XYZ 
ID_XZY 
ID_ZXY 
ID_YXZ 
ID_ZYX 
ID_YZX 

Definition at line 135 of file vf_palettegen.c.

Function Documentation

◆ AVFILTER_DEFINE_CLASS()

AVFILTER_DEFINE_CLASS ( palettegen  )

◆ query_formats()

static int query_formats ( AVFilterContext ctx)
static

Definition at line 102 of file vf_palettegen.c.

◆ sort3id()

static int sort3id ( int64_t  x,
int64_t  y,
int64_t  z 
)
static

Definition at line 151 of file vf_palettegen.c.

Referenced by compute_box_stats().

◆ cmp_color()

static int cmp_color ( const void *  a,
const void *  b 
)
static

Simple color comparison for sorting the final palette.

Definition at line 166 of file vf_palettegen.c.

Referenced by get_palette_frame().

◆ compute_box_stats()

static void compute_box_stats ( PaletteGenContext s,
struct range_box box 
)
static

Definition at line 173 of file vf_palettegen.c.

Referenced by get_palette_frame(), and split_box().

◆ get_next_box_id_to_split()

static int get_next_box_id_to_split ( PaletteGenContext s)
static

Find the next box to split: pick the one with the highest cut score.

Definition at line 212 of file vf_palettegen.c.

Referenced by get_palette_frame().

◆ split_box()

static void split_box ( PaletteGenContext s,
struct range_box box,
int  n 
)
static

Split given box in two at position n.

The original box becomes the left part of the split, and the new index box is the right part.

Definition at line 234 of file vf_palettegen.c.

Referenced by get_palette_frame().

◆ write_palette()

static void write_palette ( AVFilterContext ctx,
AVFrame out 
)
static

Write the palette into the output frame.

Definition at line 252 of file vf_palettegen.c.

Referenced by get_palette_frame().

◆ load_color_refs()

static struct color_ref** load_color_refs ( const struct hist_node hist,
int  nb_refs 
)
static

Crawl the histogram to get all the defined colors, and create a linear list of them (each color reference entry is a pointer to the value in the histogram/hash table).

Definition at line 285 of file vf_palettegen.c.

Referenced by get_palette_frame().

◆ set_colorquant_ratio_meta()

static double set_colorquant_ratio_meta ( AVFrame out,
int  nb_out,
int  nb_in 
)
static

Definition at line 303 of file vf_palettegen.c.

Referenced by get_palette_frame().

◆ get_palette_frame()

static AVFrame* get_palette_frame ( AVFilterContext ctx)
static

Main function implementing the Median Cut Algorithm defined by Paul Heckbert in Color Image Quantization for Frame Buffer Display (1982)

Definition at line 316 of file vf_palettegen.c.

Referenced by filter_frame(), and request_frame().

◆ color_inc()

static int color_inc ( struct hist_node hist,
uint32_t  color 
)
static

Locate the color in the hash table and increment its counter.

Definition at line 394 of file vf_palettegen.c.

Referenced by update_histogram_diff(), and update_histogram_frame().

◆ update_histogram_diff()

static int update_histogram_diff ( struct hist_node hist,
const AVFrame f1,
const AVFrame f2 
)
static

Update histogram when pixels differ from previous frame.

Definition at line 421 of file vf_palettegen.c.

Referenced by filter_frame().

◆ update_histogram_frame()

static int update_histogram_frame ( struct hist_node hist,
const AVFrame f 
)
static

Simple histogram of the frame.

Definition at line 445 of file vf_palettegen.c.

Referenced by filter_frame().

◆ filter_frame()

static int filter_frame ( AVFilterLink inlink,
AVFrame in 
)
static

Update the histogram for each passing frame.

No frame will be pushed here.

Definition at line 465 of file vf_palettegen.c.

◆ request_frame()

static int request_frame ( AVFilterLink outlink)
static

Returns only one frame at the end containing the full palette.

Definition at line 507 of file vf_palettegen.c.

◆ config_output()

static int config_output ( AVFilterLink outlink)
static

The output is one simple 16x16 squared-pixels palette.

Definition at line 526 of file vf_palettegen.c.

◆ init()

static int init ( AVFilterContext ctx)
static

Definition at line 533 of file vf_palettegen.c.

◆ uninit()

static av_cold void uninit ( AVFilterContext ctx)
static

Definition at line 545 of file vf_palettegen.c.

Variable Documentation

◆ palettegen_options

const AVOption palettegen_options[]
static
Initial value:
= {
{ "max_colors", "set the maximum number of colors to use in the palette", OFFSET(max_colors), AV_OPT_TYPE_INT, {.i64=256}, 2, 256, FLAGS },
{ "reserve_transparent", "reserve a palette entry for transparency", OFFSET(reserve_transparent), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
{ "transparency_color", "set a background color for transparency", OFFSET(transparency_color), AV_OPT_TYPE_COLOR, {.str="lime"}, 0, 0, FLAGS },
{ "stats_mode", "set statistics mode", OFFSET(stats_mode), AV_OPT_TYPE_INT, {.i64=STATS_MODE_ALL_FRAMES}, 0, NB_STATS_MODE-1, FLAGS, "mode" },
{ "full", "compute full frame histograms", 0, AV_OPT_TYPE_CONST, {.i64=STATS_MODE_ALL_FRAMES}, INT_MIN, INT_MAX, FLAGS, "mode" },
{ "diff", "compute histograms only for the part that differs from previous frame", 0, AV_OPT_TYPE_CONST, {.i64=STATS_MODE_DIFF_FRAMES}, INT_MIN, INT_MAX, FLAGS, "mode" },
{ "single", "compute new histogram for each frame", 0, AV_OPT_TYPE_CONST, {.i64=STATS_MODE_SINGLE_FRAMES}, INT_MIN, INT_MAX, FLAGS, "mode" },
{ NULL }
}

Definition at line 89 of file vf_palettegen.c.

◆ sortstr

const char* const sortstr[] = { "Lab", "Lba", "bLa", "aLb", "baL", "abL" }
static

Definition at line 136 of file vf_palettegen.c.

Referenced by get_palette_frame().

◆ cmp_funcs

const cmp_func cmp_funcs[]
static
Initial value:
= {
[ID_XYZ] = cmp_Lab,
[ID_XZY] = cmp_Lba,
[ID_ZXY] = cmp_bLa,
[ID_YXZ] = cmp_aLb,
[ID_ZYX] = cmp_baL,
[ID_YZX] = cmp_abL,
}

Definition at line 138 of file vf_palettegen.c.

Referenced by get_palette_frame().

◆ palettegen_inputs

const AVFilterPad palettegen_inputs[]
static
Initial value:
= {
{
.name = "default",
.filter_frame = filter_frame,
},
}

Definition at line 556 of file vf_palettegen.c.

◆ palettegen_outputs

const AVFilterPad palettegen_outputs[]
static
Initial value:
= {
{
.name = "default",
.config_props = config_output,
.request_frame = request_frame,
},
}

Definition at line 564 of file vf_palettegen.c.

◆ ff_vf_palettegen

const AVFilter ff_vf_palettegen
Initial value:
= {
.name = "palettegen",
.description = NULL_IF_CONFIG_SMALL("Find the optimal palette for a given stream."),
.priv_size = sizeof(PaletteGenContext),
.init = init,
.priv_class = &palettegen_class,
}

Definition at line 573 of file vf_palettegen.c.

request_frame
static int request_frame(AVFilterLink *outlink)
Returns only one frame at the end containing the full palette.
Definition: vf_palettegen.c:507
ID_XYZ
@ ID_XYZ
Definition: vf_palettegen.c:135
config_output
static int config_output(AVFilterLink *outlink)
The output is one simple 16x16 squared-pixels palette.
Definition: vf_palettegen.c:526
ID_ZXY
@ ID_ZXY
Definition: vf_palettegen.c:135
b
#define b
Definition: input.c:41
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:169
NB_STATS_MODE
@ NB_STATS_MODE
Definition: vf_palettegen.c:65
c1
static const uint64_t c1
Definition: murmur3.c:52
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Update the histogram for each passing frame.
Definition: vf_palettegen.c:465
STATS_MODE_ALL_FRAMES
@ STATS_MODE_ALL_FRAMES
Definition: vf_palettegen.c:62
ID_YZX
@ ID_YZX
Definition: vf_palettegen.c:135
FFDIFFSIGN
#define FFDIFFSIGN(x, y)
Comparator.
Definition: macros.h:45
init
static int init(AVFilterContext *ctx)
Definition: vf_palettegen.c:533
FLAGS
#define FLAGS
Definition: vf_palettegen.c:88
OFFSET
#define OFFSET(x)
Definition: vf_palettegen.c:87
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_palettegen.c:545
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
NULL
#define NULL
Definition: coverity.c:32
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_palettegen.c:102
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Definition: opt.h:240
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
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
PaletteGenContext
Definition: vf_palettegen.c:70
palettegen_outputs
static const AVFilterPad palettegen_outputs[]
Definition: vf_palettegen.c:564
STATS_MODE_SINGLE_FRAMES
@ STATS_MODE_SINGLE_FRAMES
Definition: vf_palettegen.c:64
color_ref
Definition: vf_palettegen.c:38
c2
static const uint64_t c2
Definition: murmur3.c:53
STATS_MODE_DIFF_FRAMES
@ STATS_MODE_DIFF_FRAMES
Definition: vf_palettegen.c:63
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
palettegen_inputs
static const AVFilterPad palettegen_inputs[]
Definition: vf_palettegen.c:556
ID_XZY
@ ID_XZY
Definition: vf_palettegen.c:135
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ID_YXZ
@ ID_YXZ
Definition: vf_palettegen.c:135
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
ID_ZYX
@ ID_ZYX
Definition: vf_palettegen.c:135
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234