FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
vf_palettegen.c File Reference

Generate one palette for a whole video stream. More...

#include "libavutil/avassert.h"
#include "libavutil/internal.h"
#include "libavutil/opt.h"
#include "libavutil/qsort.h"
#include "avfilter.h"
#include "internal.h"

Go to the source code of this file.

Data Structures

struct  color_ref
 
struct  range_box
 
struct  hist_node
 
struct  PaletteGenContext
 

Macros

#define NBITS   5
 
#define HIST_SIZE   (1<<(3*NBITS))
 
#define OFFSET(x)   offsetof(PaletteGenContext, x)
 
#define FLAGS   AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
#define DECLARE_CMP_FUNC(name, pos)
 

Typedefs

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

Enumerations

enum  { STATS_MODE_ALL_FRAMES, STATS_MODE_DIFF_FRAMES, NB_STATS_MODE }
 

Functions

 AVFILTER_DEFINE_CLASS (palettegen)
 
static int query_formats (AVFilterContext *ctx)
 
static int cmp_color (const void *a, const void *b)
 Simple color comparison for sorting the final palette. More...
 
static av_always_inline int diff (const uint32_t a, const uint32_t b)
 
static int get_next_box_id_to_split (PaletteGenContext *s)
 Find the next box to split: pick the one with the highest variance. More...
 
static uint32_t get_avg_color (struct color_ref *const *refs, const struct range_box *box)
 Get the 32-bit average color for the range of RGB colors enclosed in the specified box. 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 unsigned color_hash (uint32_t color)
 Hashing function for the color. 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 av_cold void uninit (AVFilterContext *ctx)
 

Variables

static const AVOption palettegen_options []
 
static const cmp_func cmp_funcs [] = {cmp_r, cmp_g, cmp_b}
 
static const AVFilterPad palettegen_inputs []
 
static const AVFilterPad palettegen_outputs []
 
AVFilter ff_vf_palettegen
 

Detailed Description

Generate one palette for a whole video stream.

Definition in file vf_palettegen.c.

Macro Definition Documentation

#define NBITS   5

Definition at line 59 of file vf_palettegen.c.

Referenced by color_hash().

#define HIST_SIZE   (1<<(3*NBITS))

Definition at line 60 of file vf_palettegen.c.

Referenced by load_color_refs(), and uninit().

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

Definition at line 78 of file vf_palettegen.c.

Definition at line 79 of file vf_palettegen.c.

#define DECLARE_CMP_FUNC (   name,
  pos 
)
Value:
static int cmp_##name(const void *pa, const void *pb) \
{ \
const struct color_ref * const *a = pa; \
const struct color_ref * const *b = pb; \
return ((*a)->color >> (8 * (2 - (pos))) & 0xff) \
- ((*b)->color >> (8 * (2 - (pos))) & 0xff); \
}
const char * b
Definition: vf_curves.c:109
return
const char * name
Definition: opengl_enc.c:103

Definition at line 111 of file vf_palettegen.c.

Typedef Documentation

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

Definition at line 109 of file vf_palettegen.c.

Enumeration Type Documentation

anonymous enum
Enumerator
STATS_MODE_ALL_FRAMES 
STATS_MODE_DIFF_FRAMES 
NB_STATS_MODE 

Definition at line 53 of file vf_palettegen.c.

Function Documentation

AVFILTER_DEFINE_CLASS ( palettegen  )
static int query_formats ( AVFilterContext ctx)
static

Definition at line 91 of file vf_palettegen.c.

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

Simple color comparison for sorting the final palette.

Definition at line 129 of file vf_palettegen.c.

Referenced by get_palette_frame().

static av_always_inline int diff ( const uint32_t  a,
const uint32_t  b 
)
static

Definition at line 136 of file vf_palettegen.c.

Referenced by adpcm_ct_expand_nibble(), adpcm_decode_frame(), adpcm_ima_expand_nibble(), adpcm_ima_oki_expand_nibble(), adpcm_ima_qt_compress_sample(), adpcm_ima_qt_expand_nibble(), adpcm_ima_wav_expand_nibble(), adpcm_sbpro_expand_nibble(), arith2_get_consumed_bytes(), asf_read_frame_header(), av_malloc(), av_realloc(), block_angle(), blur(), build_diff_map(), celt_decode_tf_changes(), codebook_sanity_check_for_rate_quarter(), comp_harmonic_coeff(), compute_target_delay(), copy_region_enc(), deband_16_c(), deband_8_c(), decode_channel_sf_idx(), decode_channel_wordlen(), decode_dc(), decode_frame(), decode_line(), decode_tones_amplitude(), dering_TMPL(), diff_c(), dnxhd_calc_bits_thread(), do_chromakey_pixel(), do_colorkey_pixel(), dpcm_decode_frame(), dpcm_predict(), dts_probe(), encode_block(), encode_high(), encode_line(), encode_low(), encode_residual_ch(), encode_scale_factors(), enqueue_packet(), est_alpha_diff(), estimate_pitch(), eval_sse(), fbdev_write_packet(), ff_libwebp_get_frame(), ff_mpeg1_encode_init(), ff_mpeg4_encode_mb(), ff_mxf_get_samples_per_frame(), ff_rate_estimate_qscale(), fft(), find_block_motion(), find_missing_packets(), find_timestamp_in_playlist(), fixed_gain_smooth(), fold(), get_closest_codebook(), get_next_box_id_to_split(), get_scene_score(), get_scene_score16(), get_video_frame(), glyph_cmp(), h263_decode_block(), index_mb(), init_quantized_coeffs_elem0(), inv_predict_11(), ljpeg_encode_bgr(), lsf_decode(), mdec_decode_block_intra(), motion_search(), mpeg1_decode_block_intra(), mpeg1_encode_block(), mpeg2_decode_block_intra(), mpeg2_fast_decode_block_intra(), perform_dc_correction(), pixel_color7_slow(), process_subpacket_9(), put_alpha_diff(), pxr24_uncompress(), rtp_parse_one_packet(), rv30_weak_loop_filter(), rv40_weak_loop_filter(), sad_hv_bilinear_filter_16width_msa(), sad_hv_bilinear_filter_8width_msa(), select_from_pts_buffer(), silk_stabilize_lsf(), svq1_decode_motion_vector(), swap_samplerates_on_filter(), sws_subVec(), synchronize_audio(), tm2_null_res_block(), update_qscale(), validate_options(), vp8_handle_packet(), wavpack_encode_block(), and wmv2_pred_motion().

static int get_next_box_id_to_split ( PaletteGenContext s)
static

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

Definition at line 149 of file vf_palettegen.c.

Referenced by get_palette_frame().

static uint32_t get_avg_color ( struct color_ref *const *  refs,
const struct range_box box 
)
static

Get the 32-bit average color for the range of RGB colors enclosed in the specified box.

Takes into account the weight of each color.

Definition at line 186 of file vf_palettegen.c.

Referenced by get_palette_frame(), and 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 212 of file vf_palettegen.c.

Referenced by get_palette_frame().

static void write_palette ( AVFilterContext ctx,
AVFrame out 
)
static

Write the palette into the output frame.

Definition at line 232 of file vf_palettegen.c.

Referenced by get_palette_frame().

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 265 of file vf_palettegen.c.

Referenced by get_palette_frame().

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

Definition at line 283 of file vf_palettegen.c.

Referenced by 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 296 of file vf_palettegen.c.

Referenced by request_frame().

static unsigned color_hash ( uint32_t  color)
inlinestatic

Hashing function for the color.

It keeps the NBITS least significant bit of each component to make it "random" even if the scene doesn't have much different colors.

Definition at line 397 of file vf_palettegen.c.

Referenced by 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 408 of file vf_palettegen.c.

Referenced by update_histogram_diff(), and update_histogram_frame().

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 435 of file vf_palettegen.c.

Referenced by filter_frame().

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

Simple histogram of the frame.

Definition at line 459 of file vf_palettegen.c.

Referenced by 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 479 of file vf_palettegen.c.

static int request_frame ( AVFilterLink outlink)
static

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

Definition at line 502 of file vf_palettegen.c.

static int config_output ( AVFilterLink outlink)
static

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

Definition at line 521 of file vf_palettegen.c.

static av_cold void uninit ( AVFilterContext ctx)
static

Definition at line 528 of file vf_palettegen.c.

Variable Documentation

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}, 4, 256, FLAGS },
{ "reserve_transparent", "reserve a palette entry for transparency", OFFSET(reserve_transparent), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
{ "stats_mode", "set statistics mode", OFFSET(stats_mode), AV_OPT_TYPE_INT, {.i64=STATS_MODE_ALL_FRAMES}, 0, NB_STATS_MODE, 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" },
{ NULL }
}
#define NULL
Definition: coverity.c:32
#define FLAGS
Definition: vf_palettegen.c:79
#define OFFSET(x)
Definition: vf_palettegen.c:78

Definition at line 80 of file vf_palettegen.c.

const cmp_func cmp_funcs[] = {cmp_r, cmp_g, cmp_b}
static

Definition at line 124 of file vf_palettegen.c.

Referenced by get_palette_frame().

const AVFilterPad palettegen_inputs[]
static
Initial value:
= {
{
.name = "default",
.filter_frame = filter_frame,
},
{ NULL }
}
#define NULL
Definition: coverity.c:32
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Update the histogram for each passing frame.

Definition at line 539 of file vf_palettegen.c.

const AVFilterPad palettegen_outputs[]
static
Initial value:
= {
{
.name = "default",
.config_props = config_output,
.request_frame = request_frame,
},
{ NULL }
}
#define NULL
Definition: coverity.c:32
static int config_output(AVFilterLink *outlink)
The output is one simple 16x16 squared-pixels palette.
static int request_frame(AVFilterLink *outlink)
Returns only one frame at the end containing the full palette.

Definition at line 548 of file vf_palettegen.c.

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),
.priv_class = &palettegen_class,
}
static int query_formats(AVFilterContext *ctx)
Definition: vf_palettegen.c:91
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
static const AVFilterPad palettegen_outputs[]
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:385
static const AVFilterPad palettegen_inputs[]
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:375
static av_cold void uninit(AVFilterContext *ctx)

Definition at line 558 of file vf_palettegen.c.