2015-01-24 22:38:15 +02:00
/*
2015-02-23 15:00:13 +02:00
* Copyright ( c ) 2015 Stupeflix
2022-11-05 03:48:19 +02:00
* Copyright ( c ) 2022 Clément Bœsch < u pkh me >
2015-02-23 15:00:13 +02:00
*
2015-01-24 22:38:15 +02:00
* This file is part of FFmpeg .
*
* FFmpeg is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* FFmpeg is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
/**
* @ file
* Use a palette to downsample an input video stream .
*/
# include "libavutil/bprint.h"
2022-08-27 23:33:06 +02:00
# include "libavutil/file_open.h"
2015-08-18 02:45:35 +02:00
# include "libavutil/internal.h"
2024-03-25 02:30:37 +02:00
# include "libavutil/mem.h"
2015-01-24 22:38:15 +02:00
# include "libavutil/opt.h"
2015-02-26 12:31:09 +02:00
# include "libavutil/qsort.h"
2015-01-24 22:38:15 +02:00
# include "avfilter.h"
2022-10-30 19:38:23 +02:00
# include "filters.h"
2023-08-03 23:44:07 +02:00
# include "formats.h"
2017-08-31 19:47:37 +02:00
# include "framesync.h"
2022-10-31 13:22:47 +02:00
# include "palette.h"
2023-08-03 13:03:17 +02:00
# include "video.h"
2015-01-24 22:38:15 +02:00
enum dithering_mode {
DITHERING_NONE ,
DITHERING_BAYER ,
DITHERING_HECKBERT ,
DITHERING_FLOYD_STEINBERG ,
DITHERING_SIERRA2 ,
DITHERING_SIERRA2_4A ,
2022-12-30 12:37:20 +02:00
DITHERING_SIERRA3 ,
2022-12-30 12:38:01 +02:00
DITHERING_BURKES ,
2022-12-30 12:38:13 +02:00
DITHERING_ATKINSON ,
2015-01-24 22:38:15 +02:00
NB_DITHERING
} ;
2015-02-16 18:40:55 +02:00
enum diff_mode {
DIFF_MODE_NONE ,
DIFF_MODE_RECTANGLE ,
NB_DIFF_MODE
} ;
2022-10-31 13:22:47 +02:00
struct color_info {
uint32_t srgb ;
int32_t lab [ 3 ] ;
} ;
2015-01-24 22:38:15 +02:00
struct color_node {
2022-10-31 13:22:47 +02:00
struct color_info c ;
2015-01-24 22:38:15 +02:00
uint8_t palette_id ;
int split ;
int left_id , right_id ;
} ;
2022-12-27 18:47:41 +02:00
# define CACHE_SIZE (1<<15)
2015-01-24 22:38:15 +02:00
struct cached_color {
uint32_t color ;
uint8_t pal_entry ;
} ;
struct cache_node {
struct cached_color * entries ;
int nb_entries ;
} ;
struct PaletteUseContext ;
2015-02-16 18:40:55 +02:00
typedef int ( * set_frame_func ) ( struct PaletteUseContext * s , AVFrame * out , AVFrame * in ,
int x_start , int y_start , int width , int height ) ;
2015-01-24 22:38:15 +02:00
typedef struct PaletteUseContext {
const AVClass * class ;
2017-07-31 12:27:58 +02:00
FFFrameSync fs ;
2015-01-24 22:38:15 +02:00
struct cache_node cache [ CACHE_SIZE ] ; /* lookup cache */
struct color_node map [ AVPALETTE_COUNT ] ; /* 3D-Tree (KD-Tree with K=3) for reverse colormap */
uint32_t palette [ AVPALETTE_COUNT ] ;
2017-10-24 01:12:57 +02:00
int transparency_index ; /* index in the palette of transparency. -1 if there is no transparency in the palette. */
int trans_thresh ;
2015-01-24 22:38:15 +02:00
int palette_loaded ;
int dither ;
2016-09-02 22:40:34 +02:00
int new ;
2015-01-24 22:38:15 +02:00
set_frame_func set_frame ;
int bayer_scale ;
int ordered_dither [ 8 * 8 ] ;
2015-02-16 18:40:55 +02:00
int diff_mode ;
AVFrame * last_in ;
AVFrame * last_out ;
2015-01-24 22:38:15 +02:00
/* debug options */
char * dot_filename ;
int calc_mean_err ;
uint64_t total_mean_err ;
} PaletteUseContext ;
# define OFFSET(x) offsetof(PaletteUseContext, x)
2022-10-30 19:38:23 +02:00
# define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
2015-01-24 22:38:15 +02:00
static const AVOption paletteuse_options [ ] = {
2024-02-11 16:41:05 +02:00
{ " dither " , " select dithering mode " , OFFSET ( dither ) , AV_OPT_TYPE_INT , { . i64 = DITHERING_SIERRA2_4A } , 0 , NB_DITHERING - 1 , FLAGS , . unit = " dithering_mode " } ,
{ " bayer " , " ordered 8x8 bayer dithering (deterministic) " , 0 , AV_OPT_TYPE_CONST , { . i64 = DITHERING_BAYER } , INT_MIN , INT_MAX , FLAGS , . unit = " dithering_mode " } ,
{ " heckbert " , " dithering as defined by Paul Heckbert in 1982 (simple error diffusion) " , 0 , AV_OPT_TYPE_CONST , { . i64 = DITHERING_HECKBERT } , INT_MIN , INT_MAX , FLAGS , . unit = " dithering_mode " } ,
{ " floyd_steinberg " , " Floyd and Steingberg dithering (error diffusion) " , 0 , AV_OPT_TYPE_CONST , { . i64 = DITHERING_FLOYD_STEINBERG } , INT_MIN , INT_MAX , FLAGS , . unit = " dithering_mode " } ,
{ " sierra2 " , " Frankie Sierra dithering v2 (error diffusion) " , 0 , AV_OPT_TYPE_CONST , { . i64 = DITHERING_SIERRA2 } , INT_MIN , INT_MAX , FLAGS , . unit = " dithering_mode " } ,
{ " sierra2_4a " , " Frankie Sierra dithering v2 \" Lite \" (error diffusion) " , 0 , AV_OPT_TYPE_CONST , { . i64 = DITHERING_SIERRA2_4A } , INT_MIN , INT_MAX , FLAGS , . unit = " dithering_mode " } ,
{ " sierra3 " , " Frankie Sierra dithering v3 (error diffusion) " , 0 , AV_OPT_TYPE_CONST , { . i64 = DITHERING_SIERRA3 } , INT_MIN , INT_MAX , FLAGS , . unit = " dithering_mode " } ,
{ " burkes " , " Burkes dithering (error diffusion) " , 0 , AV_OPT_TYPE_CONST , { . i64 = DITHERING_BURKES } , INT_MIN , INT_MAX , FLAGS , . unit = " dithering_mode " } ,
{ " atkinson " , " Atkinson dithering by Bill Atkinson at Apple Computer (error diffusion) " , 0 , AV_OPT_TYPE_CONST , { . i64 = DITHERING_ATKINSON } , INT_MIN , INT_MAX , FLAGS , . unit = " dithering_mode " } ,
2015-01-24 22:38:15 +02:00
{ " bayer_scale " , " set scale for bayer dithering " , OFFSET ( bayer_scale ) , AV_OPT_TYPE_INT , { . i64 = 2 } , 0 , 5 , FLAGS } ,
2024-02-11 16:41:05 +02:00
{ " diff_mode " , " set frame difference mode " , OFFSET ( diff_mode ) , AV_OPT_TYPE_INT , { . i64 = DIFF_MODE_NONE } , 0 , NB_DIFF_MODE - 1 , FLAGS , . unit = " diff_mode " } ,
{ " rectangle " , " process smallest different rectangle " , 0 , AV_OPT_TYPE_CONST , { . i64 = DIFF_MODE_RECTANGLE } , INT_MIN , INT_MAX , FLAGS , . unit = " diff_mode " } ,
2017-10-28 17:15:06 +02:00
{ " new " , " take new palette for each output frame " , OFFSET ( new ) , AV_OPT_TYPE_BOOL , { . i64 = 0 } , 0 , 1 , FLAGS } ,
2018-12-11 18:55:05 +02:00
{ " alpha_threshold " , " set the alpha threshold for transparency " , OFFSET ( trans_thresh ) , AV_OPT_TYPE_INT , { . i64 = 128 } , 0 , 255 , FLAGS } ,
2015-01-24 22:38:15 +02:00
/* following are the debug options, not part of the official API */
2020-03-17 23:46:36 +02:00
{ " debug_kdtree " , " save Graphviz graph of the kdtree in specified file " , OFFSET ( dot_filename ) , AV_OPT_TYPE_STRING , { . str = NULL } , 0 , 0 , FLAGS } ,
2015-01-24 22:38:15 +02:00
{ NULL }
} ;
AVFILTER_DEFINE_CLASS ( paletteuse ) ;
2017-07-31 12:27:58 +02:00
static int load_apply_palette ( FFFrameSync * fs ) ;
2015-01-24 22:38:15 +02:00
static int query_formats ( AVFilterContext * ctx )
{
static const enum AVPixelFormat in_fmts [ ] = { AV_PIX_FMT_RGB32 , AV_PIX_FMT_NONE } ;
static const enum AVPixelFormat inpal_fmts [ ] = { AV_PIX_FMT_RGB32 , AV_PIX_FMT_NONE } ;
static const enum AVPixelFormat out_fmts [ ] = { AV_PIX_FMT_PAL8 , AV_PIX_FMT_NONE } ;
2015-10-05 05:39:25 +02:00
int ret ;
2020-08-07 06:09:59 +02:00
if ( ( ret = ff_formats_ref ( ff_make_format_list ( in_fmts ) ,
2020-08-24 12:16:34 +02:00
& ctx - > inputs [ 0 ] - > outcfg . formats ) ) < 0 | |
2020-08-07 06:09:59 +02:00
( ret = ff_formats_ref ( ff_make_format_list ( inpal_fmts ) ,
2020-08-24 12:16:34 +02:00
& ctx - > inputs [ 1 ] - > outcfg . formats ) ) < 0 | |
2020-08-07 06:09:59 +02:00
( ret = ff_formats_ref ( ff_make_format_list ( out_fmts ) ,
2020-08-24 12:16:34 +02:00
& ctx - > outputs [ 0 ] - > incfg . formats ) ) < 0 )
2015-10-05 05:39:25 +02:00
return ret ;
2015-01-24 22:38:15 +02:00
return 0 ;
}
2020-01-22 02:47:21 +02:00
static av_always_inline uint32_t dither_color ( uint32_t px , int er , int eg ,
int eb , int scale , int shift )
2015-01-24 22:38:15 +02:00
{
2022-10-31 13:22:41 +02:00
return ( px & 0xff000000 )
2017-10-24 01:12:57 +02:00
| av_clip_uint8 ( ( px > > 16 & 0xff ) + ( ( er * scale ) / ( 1 < < shift ) ) ) < < 16
2015-02-18 12:17:10 +02:00
| av_clip_uint8 ( ( px > > 8 & 0xff ) + ( ( eg * scale ) / ( 1 < < shift ) ) ) < < 8
| av_clip_uint8 ( ( px & 0xff ) + ( ( eb * scale ) / ( 1 < < shift ) ) ) ;
2015-01-24 22:38:15 +02:00
}
2022-10-31 13:22:47 +02:00
static av_always_inline int diff ( const struct color_info * a , const struct color_info * b , const int trans_thresh )
2015-01-24 22:38:15 +02:00
{
2022-10-31 13:22:47 +02:00
const uint8_t alpha_a = a - > srgb > > 24 ;
const uint8_t alpha_b = b - > srgb > > 24 ;
if ( alpha_a < trans_thresh & & alpha_b < trans_thresh ) {
2017-10-24 01:12:57 +02:00
return 0 ;
2022-10-31 13:22:47 +02:00
} else if ( alpha_a > = trans_thresh & & alpha_b > = trans_thresh ) {
const int64_t dL = a - > lab [ 0 ] - b - > lab [ 0 ] ;
const int64_t da = a - > lab [ 1 ] - b - > lab [ 1 ] ;
const int64_t db = a - > lab [ 2 ] - b - > lab [ 2 ] ;
const int64_t ret = dL * dL + da * da + db * db ;
return FFMIN ( ret , INT32_MAX - 1 ) ;
2017-10-24 01:12:57 +02:00
} else {
2022-10-31 13:22:47 +02:00
return INT32_MAX - 1 ;
2017-10-24 01:12:57 +02:00
}
2015-01-24 22:38:15 +02:00
}
2022-10-31 13:22:47 +02:00
static struct color_info get_color_from_srgb ( uint32_t srgb )
{
const struct Lab lab = ff_srgb_u8_to_oklab_int ( srgb ) ;
struct color_info ret = { . srgb = srgb , . lab = { lab . L , lab . a , lab . b } } ;
return ret ;
}
2015-01-24 22:38:15 +02:00
struct nearest_color {
int node_pos ;
2022-10-31 13:22:47 +02:00
int64_t dist_sqd ;
2015-01-24 22:38:15 +02:00
} ;
2022-10-30 19:38:23 +02:00
static void colormap_nearest_node ( const struct color_node * map ,
2015-01-24 22:38:15 +02:00
const int node_pos ,
2022-10-31 13:22:47 +02:00
const struct color_info * target ,
2022-10-30 19:38:23 +02:00
const int trans_thresh ,
2015-01-24 22:38:15 +02:00
struct nearest_color * nearest )
{
const struct color_node * kd = map + node_pos ;
2022-10-31 13:22:47 +02:00
int nearer_kd_id , further_kd_id ;
const struct color_info * current = & kd - > c ;
const int64_t current_to_target = diff ( target , current , trans_thresh ) ;
2015-01-24 22:38:15 +02:00
if ( current_to_target < nearest - > dist_sqd ) {
nearest - > node_pos = node_pos ;
nearest - > dist_sqd = current_to_target ;
}
if ( kd - > left_id ! = - 1 | | kd - > right_id ! = - 1 ) {
2022-10-31 13:22:47 +02:00
const int64_t dx = target - > lab [ kd - > split ] - current - > lab [ kd - > split ] ;
2015-01-24 22:38:15 +02:00
if ( dx < = 0 ) nearer_kd_id = kd - > left_id , further_kd_id = kd - > right_id ;
else nearer_kd_id = kd - > right_id , further_kd_id = kd - > left_id ;
if ( nearer_kd_id ! = - 1 )
2022-10-30 19:38:23 +02:00
colormap_nearest_node ( map , nearer_kd_id , target , trans_thresh , nearest ) ;
2015-01-24 22:38:15 +02:00
if ( further_kd_id ! = - 1 & & dx * dx < nearest - > dist_sqd )
2022-10-30 19:38:23 +02:00
colormap_nearest_node ( map , further_kd_id , target , trans_thresh , nearest ) ;
2015-01-24 22:38:15 +02:00
}
}
2022-12-27 19:03:46 +02:00
static av_always_inline uint8_t colormap_nearest ( const struct color_node * node , const struct color_info * target , const int trans_thresh )
2015-01-24 22:38:15 +02:00
{
struct nearest_color res = { . dist_sqd = INT_MAX , . node_pos = - 1 } ;
2022-11-05 01:55:55 +02:00
colormap_nearest_node ( node , 0 , target , trans_thresh , & res ) ;
2015-01-24 22:38:15 +02:00
return node [ res . node_pos ] . palette_id ;
}
struct stack_node {
int color_id ;
int dx2 ;
} ;
/**
* Check if the requested color is in the cache already . If not , find it in the
* color tree and cache it .
*/
2022-12-27 19:03:46 +02:00
static av_always_inline int color_get ( PaletteUseContext * s , uint32_t color )
2015-01-24 22:38:15 +02:00
{
2022-10-31 13:22:47 +02:00
struct color_info clrinfo ;
2022-12-27 18:47:41 +02:00
const uint32_t hash = ff_lowbias32 ( color ) & ( CACHE_SIZE - 1 ) ;
2017-10-28 17:39:48 +02:00
struct cache_node * node = & s - > cache [ hash ] ;
2015-01-24 22:38:15 +02:00
struct cached_color * e ;
2017-10-24 01:12:57 +02:00
// first, check for transparency
2022-10-31 13:22:46 +02:00
if ( color > > 24 < s - > trans_thresh & & s - > transparency_index > = 0 ) {
2017-10-28 17:39:48 +02:00
return s - > transparency_index ;
2017-10-24 01:12:57 +02:00
}
2022-12-27 19:13:20 +02:00
for ( int i = 0 ; i < node - > nb_entries ; i + + ) {
2015-01-24 22:38:15 +02:00
e = & node - > entries [ i ] ;
if ( e - > color = = color )
return e - > pal_entry ;
}
e = av_dynarray2_add ( ( void * * ) & node - > entries , & node - > nb_entries ,
sizeof ( * node - > entries ) , NULL ) ;
if ( ! e )
return AVERROR ( ENOMEM ) ;
e - > color = color ;
2022-10-31 13:22:47 +02:00
clrinfo = get_color_from_srgb ( color ) ;
2022-12-27 19:03:46 +02:00
e - > pal_entry = colormap_nearest ( s - > map , & clrinfo , s - > trans_thresh ) ;
2017-10-24 01:12:57 +02:00
2015-01-24 22:38:15 +02:00
return e - > pal_entry ;
}
2017-10-28 17:39:48 +02:00
static av_always_inline int get_dst_color_err ( PaletteUseContext * s ,
2022-12-27 19:03:46 +02:00
uint32_t c , int * er , int * eg , int * eb )
2015-01-24 22:38:15 +02:00
{
2017-11-08 20:17:45 +02:00
uint32_t dstc ;
2022-12-27 19:03:46 +02:00
const int dstx = color_get ( s , c ) ;
2017-11-08 20:17:45 +02:00
if ( dstx < 0 )
return dstx ;
dstc = s - > palette [ dstx ] ;
2021-08-19 09:40:14 +02:00
if ( dstx = = s - > transparency_index ) {
2022-10-30 19:38:23 +02:00
* er = * eg = * eb = 0 ;
2021-08-19 09:40:14 +02:00
} else {
2022-12-27 19:08:21 +02:00
const uint8_t r = c > > 16 & 0xff ;
const uint8_t g = c > > 8 & 0xff ;
const uint8_t b = c & 0xff ;
2021-08-19 09:40:14 +02:00
* er = ( int ) r - ( int ) ( dstc > > 16 & 0xff ) ;
* eg = ( int ) g - ( int ) ( dstc > > 8 & 0xff ) ;
* eb = ( int ) b - ( int ) ( dstc & 0xff ) ;
}
2015-01-24 22:38:15 +02:00
return dstx ;
}
static av_always_inline int set_frame ( PaletteUseContext * s , AVFrame * out , AVFrame * in ,
2015-02-16 18:40:55 +02:00
int x_start , int y_start , int w , int h ,
2022-12-27 19:03:46 +02:00
enum dithering_mode dither )
2015-01-24 22:38:15 +02:00
{
const int src_linesize = in - > linesize [ 0 ] > > 2 ;
const int dst_linesize = out - > linesize [ 0 ] ;
2015-02-16 18:40:55 +02:00
uint32_t * src = ( ( uint32_t * ) in - > data [ 0 ] ) + y_start * src_linesize ;
uint8_t * dst = out - > data [ 0 ] + y_start * dst_linesize ;
2015-01-24 22:38:15 +02:00
2015-02-16 18:40:55 +02:00
w + = x_start ;
h + = y_start ;
2022-12-27 19:13:20 +02:00
for ( int y = y_start ; y < h ; y + + ) {
for ( int x = x_start ; x < w ; x + + ) {
2022-10-30 19:38:23 +02:00
int er , eg , eb ;
2015-01-24 22:38:15 +02:00
if ( dither = = DITHERING_BAYER ) {
const int d = s - > ordered_dither [ ( y & 7 ) < < 3 | ( x & 7 ) ] ;
2022-10-31 13:22:41 +02:00
const uint8_t a8 = src [ x ] > > 24 ;
2015-01-24 22:38:15 +02:00
const uint8_t r8 = src [ x ] > > 16 & 0xff ;
const uint8_t g8 = src [ x ] > > 8 & 0xff ;
const uint8_t b8 = src [ x ] & 0xff ;
const uint8_t r = av_clip_uint8 ( r8 + d ) ;
const uint8_t g = av_clip_uint8 ( g8 + d ) ;
const uint8_t b = av_clip_uint8 ( b8 + d ) ;
vf_paletteuse: fix color cache lookup for Bayer dithering mode.
To trigger this bug, use `paletteuse=dither=bayer:bayer_scale=0`; you will see
that adjacent pixel lines will use the same dither pattern, instead of being
shifted from each other by 32 units (0x20).
One way to demostrate the bug is:
$ convert -size 64x256 gradient:black-white -rotate 270 grad.png
$ echo 'P2 2 1 255 0 255' > bw.pnm
$ ffmpeg -i grad.png -filter_complex 'movie=bw.pnm,scale=256x1[bw]; [0:v][bw]paletteuse=dither=bayer:bayer_scale=0' gradbw.png
Previously: https://www.rm.cloudns.org/img/uploaded/0bd152c11b9cd99e5945115534b1bdde.png
Now: https://www.rm.cloudns.org/img/uploaded/89caaa5e36c38bc2c01755b30811f969.png
This was caused by passing inconsistent color vs (a,r,g,b) parameters to
color_get(), and NBITS being 5 meaning actually hitting the same cache node
does happen in this case, but ONLY if bayer_scale is zero.
The fix is passing the correct color value to color_get().
Also added a previous-failing FATE test; image comparison of the first frame:
Previously: https://www.rm.cloudns.org/img/uploaded/d0ff9db8d8a7d8a3b8b88bbe92bf5fed.png
Now: https://www.rm.cloudns.org/img/uploaded/a72389707e719b5cd1c58916a9e79ca8.png
(on this less synthetic test image, the bug basically causes noise from cache
hits vs misses)
Tested: FATE passes, which exercises this filter but at the default bayer_scale.
Reviewed-by: Paul B Mahol <onemda@gmail.com>
2022-01-10 20:25:31 +02:00
const uint32_t color_new = ( unsigned ) ( a8 ) < < 24 | r < < 16 | g < < 8 | b ;
2022-12-27 19:03:46 +02:00
const int color = color_get ( s , color_new ) ;
2015-01-24 22:38:15 +02:00
if ( color < 0 )
return color ;
dst [ x ] = color ;
} else if ( dither = = DITHERING_HECKBERT ) {
2015-02-16 18:40:55 +02:00
const int right = x < w - 1 , down = y < h - 1 ;
2022-12-27 19:03:46 +02:00
const int color = get_dst_color_err ( s , src [ x ] , & er , & eg , & eb ) ;
2015-01-24 22:38:15 +02:00
if ( color < 0 )
return color ;
dst [ x ] = color ;
if ( right ) src [ x + 1 ] = dither_color ( src [ x + 1 ] , er , eg , eb , 3 , 3 ) ;
if ( down ) src [ src_linesize + x ] = dither_color ( src [ src_linesize + x ] , er , eg , eb , 3 , 3 ) ;
if ( right & & down ) src [ src_linesize + x + 1 ] = dither_color ( src [ src_linesize + x + 1 ] , er , eg , eb , 2 , 3 ) ;
} else if ( dither = = DITHERING_FLOYD_STEINBERG ) {
2015-02-16 18:40:55 +02:00
const int right = x < w - 1 , down = y < h - 1 , left = x > x_start ;
2022-12-27 19:03:46 +02:00
const int color = get_dst_color_err ( s , src [ x ] , & er , & eg , & eb ) ;
2015-01-24 22:38:15 +02:00
if ( color < 0 )
return color ;
dst [ x ] = color ;
if ( right ) src [ x + 1 ] = dither_color ( src [ x + 1 ] , er , eg , eb , 7 , 4 ) ;
if ( left & & down ) src [ src_linesize + x - 1 ] = dither_color ( src [ src_linesize + x - 1 ] , er , eg , eb , 3 , 4 ) ;
if ( down ) src [ src_linesize + x ] = dither_color ( src [ src_linesize + x ] , er , eg , eb , 5 , 4 ) ;
if ( right & & down ) src [ src_linesize + x + 1 ] = dither_color ( src [ src_linesize + x + 1 ] , er , eg , eb , 1 , 4 ) ;
} else if ( dither = = DITHERING_SIERRA2 ) {
2015-02-16 18:40:55 +02:00
const int right = x < w - 1 , down = y < h - 1 , left = x > x_start ;
const int right2 = x < w - 2 , left2 = x > x_start + 1 ;
2022-12-27 19:03:46 +02:00
const int color = get_dst_color_err ( s , src [ x ] , & er , & eg , & eb ) ;
2015-01-24 22:38:15 +02:00
if ( color < 0 )
return color ;
dst [ x ] = color ;
if ( right ) src [ x + 1 ] = dither_color ( src [ x + 1 ] , er , eg , eb , 4 , 4 ) ;
if ( right2 ) src [ x + 2 ] = dither_color ( src [ x + 2 ] , er , eg , eb , 3 , 4 ) ;
if ( down ) {
if ( left2 ) src [ src_linesize + x - 2 ] = dither_color ( src [ src_linesize + x - 2 ] , er , eg , eb , 1 , 4 ) ;
if ( left ) src [ src_linesize + x - 1 ] = dither_color ( src [ src_linesize + x - 1 ] , er , eg , eb , 2 , 4 ) ;
2017-04-10 11:25:44 +02:00
if ( 1 ) src [ src_linesize + x ] = dither_color ( src [ src_linesize + x ] , er , eg , eb , 3 , 4 ) ;
2015-01-24 22:38:15 +02:00
if ( right ) src [ src_linesize + x + 1 ] = dither_color ( src [ src_linesize + x + 1 ] , er , eg , eb , 2 , 4 ) ;
if ( right2 ) src [ src_linesize + x + 2 ] = dither_color ( src [ src_linesize + x + 2 ] , er , eg , eb , 1 , 4 ) ;
}
} else if ( dither = = DITHERING_SIERRA2_4A ) {
2015-02-16 18:40:55 +02:00
const int right = x < w - 1 , down = y < h - 1 , left = x > x_start ;
2022-12-27 19:03:46 +02:00
const int color = get_dst_color_err ( s , src [ x ] , & er , & eg , & eb ) ;
2015-01-24 22:38:15 +02:00
if ( color < 0 )
return color ;
dst [ x ] = color ;
if ( right ) src [ x + 1 ] = dither_color ( src [ x + 1 ] , er , eg , eb , 2 , 2 ) ;
if ( left & & down ) src [ src_linesize + x - 1 ] = dither_color ( src [ src_linesize + x - 1 ] , er , eg , eb , 1 , 2 ) ;
if ( down ) src [ src_linesize + x ] = dither_color ( src [ src_linesize + x ] , er , eg , eb , 1 , 2 ) ;
2022-12-30 12:37:20 +02:00
} else if ( dither = = DITHERING_SIERRA3 ) {
const int right = x < w - 1 , down = y < h - 1 , left = x > x_start ;
const int right2 = x < w - 2 , down2 = y < h - 2 , left2 = x > x_start + 1 ;
const int color = get_dst_color_err ( s , src [ x ] , & er , & eg , & eb ) ;
if ( color < 0 )
return color ;
dst [ x ] = color ;
if ( right ) src [ x + 1 ] = dither_color ( src [ x + 1 ] , er , eg , eb , 5 , 5 ) ;
if ( right2 ) src [ x + 2 ] = dither_color ( src [ x + 2 ] , er , eg , eb , 3 , 5 ) ;
if ( down ) {
if ( left2 ) src [ src_linesize + x - 2 ] = dither_color ( src [ src_linesize + x - 2 ] , er , eg , eb , 2 , 5 ) ;
if ( left ) src [ src_linesize + x - 1 ] = dither_color ( src [ src_linesize + x - 1 ] , er , eg , eb , 4 , 5 ) ;
if ( 1 ) src [ src_linesize + x ] = dither_color ( src [ src_linesize + x ] , er , eg , eb , 5 , 5 ) ;
if ( right ) src [ src_linesize + x + 1 ] = dither_color ( src [ src_linesize + x + 1 ] , er , eg , eb , 4 , 5 ) ;
if ( right2 ) src [ src_linesize + x + 2 ] = dither_color ( src [ src_linesize + x + 2 ] , er , eg , eb , 2 , 5 ) ;
if ( down2 ) {
if ( left ) src [ src_linesize * 2 + x - 1 ] = dither_color ( src [ src_linesize * 2 + x - 1 ] , er , eg , eb , 2 , 5 ) ;
if ( 1 ) src [ src_linesize * 2 + x ] = dither_color ( src [ src_linesize * 2 + x ] , er , eg , eb , 3 , 5 ) ;
if ( right ) src [ src_linesize * 2 + x + 1 ] = dither_color ( src [ src_linesize * 2 + x + 1 ] , er , eg , eb , 2 , 5 ) ;
}
}
2022-12-30 12:38:01 +02:00
} else if ( dither = = DITHERING_BURKES ) {
const int right = x < w - 1 , down = y < h - 1 , left = x > x_start ;
const int right2 = x < w - 2 , left2 = x > x_start + 1 ;
const int color = get_dst_color_err ( s , src [ x ] , & er , & eg , & eb ) ;
if ( color < 0 )
return color ;
dst [ x ] = color ;
if ( right ) src [ x + 1 ] = dither_color ( src [ x + 1 ] , er , eg , eb , 8 , 5 ) ;
if ( right2 ) src [ x + 2 ] = dither_color ( src [ x + 2 ] , er , eg , eb , 4 , 5 ) ;
if ( down ) {
if ( left2 ) src [ src_linesize + x - 2 ] = dither_color ( src [ src_linesize + x - 2 ] , er , eg , eb , 2 , 5 ) ;
if ( left ) src [ src_linesize + x - 1 ] = dither_color ( src [ src_linesize + x - 1 ] , er , eg , eb , 4 , 5 ) ;
if ( 1 ) src [ src_linesize + x ] = dither_color ( src [ src_linesize + x ] , er , eg , eb , 8 , 5 ) ;
if ( right ) src [ src_linesize + x + 1 ] = dither_color ( src [ src_linesize + x + 1 ] , er , eg , eb , 4 , 5 ) ;
if ( right2 ) src [ src_linesize + x + 2 ] = dither_color ( src [ src_linesize + x + 2 ] , er , eg , eb , 2 , 5 ) ;
}
2022-12-30 12:38:13 +02:00
} else if ( dither = = DITHERING_ATKINSON ) {
const int right = x < w - 1 , down = y < h - 1 , left = x > x_start ;
const int right2 = x < w - 2 , down2 = y < h - 2 ;
const int color = get_dst_color_err ( s , src [ x ] , & er , & eg , & eb ) ;
if ( color < 0 )
return color ;
dst [ x ] = color ;
if ( right ) src [ x + 1 ] = dither_color ( src [ x + 1 ] , er , eg , eb , 1 , 3 ) ;
if ( right2 ) src [ x + 2 ] = dither_color ( src [ x + 2 ] , er , eg , eb , 1 , 3 ) ;
if ( down ) {
if ( left ) src [ src_linesize + x - 1 ] = dither_color ( src [ src_linesize + x - 1 ] , er , eg , eb , 1 , 3 ) ;
if ( 1 ) src [ src_linesize + x ] = dither_color ( src [ src_linesize + x ] , er , eg , eb , 1 , 3 ) ;
if ( right ) src [ src_linesize + x + 1 ] = dither_color ( src [ src_linesize + x + 1 ] , er , eg , eb , 1 , 3 ) ;
if ( down2 ) src [ src_linesize * 2 + x ] = dither_color ( src [ src_linesize * 2 + x ] , er , eg , eb , 1 , 3 ) ;
}
2015-01-24 22:38:15 +02:00
} else {
2022-12-27 19:03:46 +02:00
const int color = color_get ( s , src [ x ] ) ;
2015-01-24 22:38:15 +02:00
if ( color < 0 )
return color ;
dst [ x ] = color ;
}
}
src + = src_linesize ;
dst + = dst_linesize ;
}
return 0 ;
}
# define INDENT 4
static void disp_node ( AVBPrint * buf ,
const struct color_node * map ,
int parent_id , int node_id ,
int depth )
{
const struct color_node * node = & map [ node_id ] ;
2022-10-31 13:22:47 +02:00
const uint32_t fontcolor = node - > c . lab [ 0 ] > 0x7fff ? 0 : 0xffffff ;
const int lab_comp = node - > split ;
2015-01-24 22:38:15 +02:00
av_bprintf ( buf , " %*cnode%d [ "
2022-10-31 13:22:47 +02:00
" label= \" %c%d%c%d%c%d%c \" "
2022-10-31 13:22:46 +02:00
" fillcolor= \" #%06 " PRIX32 " \" "
2017-03-27 21:31:46 +02:00
" fontcolor= \" #%06 " PRIX32 " \" ] \n " ,
2015-01-24 22:38:15 +02:00
depth * INDENT , ' ' , node - > palette_id ,
2022-10-31 13:22:47 +02:00
" [ " [ lab_comp ] , node - > c . lab [ 0 ] ,
" ][ " [ lab_comp ] , node - > c . lab [ 1 ] ,
" ][ " [ lab_comp ] , node - > c . lab [ 2 ] ,
" ] " [ lab_comp ] ,
node - > c . srgb & 0xffffff ,
2015-01-24 22:38:15 +02:00
fontcolor ) ;
if ( parent_id ! = - 1 )
av_bprintf ( buf , " %*cnode%d -> node%d \n " , depth * INDENT , ' ' ,
map [ parent_id ] . palette_id , node - > palette_id ) ;
if ( node - > left_id ! = - 1 ) disp_node ( buf , map , node_id , node - > left_id , depth + 1 ) ;
if ( node - > right_id ! = - 1 ) disp_node ( buf , map , node_id , node - > right_id , depth + 1 ) ;
}
// debug_kdtree=kdtree.dot -> dot -Tpng kdtree.dot > kdtree.png
static int disp_tree ( const struct color_node * node , const char * fname )
{
AVBPrint buf ;
2022-05-20 13:55:46 +02:00
FILE * f = avpriv_fopen_utf8 ( fname , " w " ) ;
2015-01-24 22:38:15 +02:00
if ( ! f ) {
int ret = AVERROR ( errno ) ;
av_log ( NULL , AV_LOG_ERROR , " Cannot open file '%s' for writing: %s \n " ,
fname , av_err2str ( ret ) ) ;
return ret ;
}
av_bprint_init ( & buf , 0 , AV_BPRINT_SIZE_UNLIMITED ) ;
av_bprintf ( & buf , " digraph { \n " ) ;
av_bprintf ( & buf , " node [style=filled fontsize=10 shape=box] \n " ) ;
disp_node ( & buf , node , - 1 , 0 , 0 ) ;
av_bprintf ( & buf , " } \n " ) ;
fwrite ( buf . str , 1 , buf . len , f ) ;
fclose ( f ) ;
av_bprint_finalize ( & buf , NULL ) ;
return 0 ;
}
struct color {
2022-10-31 13:22:47 +02:00
struct Lab value ;
2015-01-24 22:38:15 +02:00
uint8_t pal_id ;
} ;
struct color_rect {
2022-10-31 13:22:47 +02:00
int32_t min [ 3 ] ;
int32_t max [ 3 ] ;
2015-01-24 22:38:15 +02:00
} ;
typedef int ( * cmp_func ) ( const void * , const void * ) ;
2022-10-31 13:22:47 +02:00
# define DECLARE_CMP_FUNC(name) \
2015-01-24 22:38:15 +02:00
static int cmp_ # # name ( const void * pa , const void * pb ) \
{ \
const struct color * a = pa ; \
const struct color * b = pb ; \
2022-10-31 13:22:47 +02:00
return FFDIFFSIGN ( a - > value . name , b - > value . name ) ; \
2015-01-24 22:38:15 +02:00
}
2022-10-31 13:22:47 +02:00
DECLARE_CMP_FUNC ( L )
DECLARE_CMP_FUNC ( a )
DECLARE_CMP_FUNC ( b )
2015-01-24 22:38:15 +02:00
2022-10-31 13:22:47 +02:00
static const cmp_func cmp_funcs [ ] = { cmp_L , cmp_a , cmp_b } ;
2015-01-24 22:38:15 +02:00
2022-10-30 19:38:23 +02:00
static int get_next_color ( const uint8_t * color_used , const uint32_t * palette ,
2015-01-24 22:38:15 +02:00
int * component , const struct color_rect * box )
{
2022-10-31 13:22:47 +02:00
int wL , wa , wb ;
2022-12-27 19:13:20 +02:00
int longest = 0 ;
2015-01-24 22:38:15 +02:00
unsigned nb_color = 0 ;
struct color_rect ranges ;
struct color tmp_pal [ 256 ] ;
2015-02-26 12:31:09 +02:00
cmp_func cmpf ;
2015-01-24 22:38:15 +02:00
2022-10-31 13:22:47 +02:00
ranges . min [ 0 ] = ranges . min [ 1 ] = ranges . min [ 2 ] = 0xffff ;
ranges . max [ 0 ] = ranges . max [ 1 ] = ranges . max [ 2 ] = - 0xffff ;
2015-01-24 22:38:15 +02:00
2022-12-27 19:13:20 +02:00
for ( int i = 0 ; i < AVPALETTE_COUNT ; i + + ) {
2022-10-30 19:38:23 +02:00
const uint32_t c = palette [ i ] ;
2022-10-31 13:22:41 +02:00
const uint8_t a = c > > 24 ;
2022-10-31 13:22:47 +02:00
const struct Lab lab = ff_srgb_u8_to_oklab_int ( c ) ;
2015-01-24 22:38:15 +02:00
2022-10-30 19:38:23 +02:00
if ( color_used [ i ] | | ( a ! = 0xff ) | |
2022-10-31 13:22:47 +02:00
lab . L < box - > min [ 0 ] | | lab . a < box - > min [ 1 ] | | lab . b < box - > min [ 2 ] | |
lab . L > box - > max [ 0 ] | | lab . a > box - > max [ 1 ] | | lab . b > box - > max [ 2 ] )
2015-01-24 22:38:15 +02:00
continue ;
2022-10-31 13:22:47 +02:00
if ( lab . L < ranges . min [ 0 ] ) ranges . min [ 0 ] = lab . L ;
if ( lab . a < ranges . min [ 1 ] ) ranges . min [ 1 ] = lab . a ;
if ( lab . b < ranges . min [ 2 ] ) ranges . min [ 2 ] = lab . b ;
2015-01-24 22:38:15 +02:00
2022-10-31 13:22:47 +02:00
if ( lab . L > ranges . max [ 0 ] ) ranges . max [ 0 ] = lab . L ;
if ( lab . a > ranges . max [ 1 ] ) ranges . max [ 1 ] = lab . a ;
if ( lab . b > ranges . max [ 2 ] ) ranges . max [ 2 ] = lab . b ;
2015-01-24 22:38:15 +02:00
2022-10-31 13:22:47 +02:00
tmp_pal [ nb_color ] . value = lab ;
2015-01-24 22:38:15 +02:00
tmp_pal [ nb_color ] . pal_id = i ;
nb_color + + ;
}
if ( ! nb_color )
return - 1 ;
/* define longest axis that will be the split component */
2022-10-31 13:22:47 +02:00
wL = ranges . max [ 0 ] - ranges . min [ 0 ] ;
wa = ranges . max [ 1 ] - ranges . min [ 1 ] ;
2022-10-30 19:38:23 +02:00
wb = ranges . max [ 2 ] - ranges . min [ 2 ] ;
2022-10-31 13:22:47 +02:00
if ( wb > = wL & & wb > = wa ) longest = 2 ;
if ( wa > = wL & & wa > = wb ) longest = 1 ;
if ( wL > = wa & & wL > = wb ) longest = 0 ;
2015-02-26 12:31:09 +02:00
cmpf = cmp_funcs [ longest ] ;
2015-01-24 22:38:15 +02:00
* component = longest ;
/* sort along this axis to get median */
2015-02-26 12:31:09 +02:00
AV_QSORT ( tmp_pal , nb_color , struct color , cmpf ) ;
2015-01-24 22:38:15 +02:00
return tmp_pal [ nb_color > > 1 ] . pal_id ;
}
static int colormap_insert ( struct color_node * map ,
uint8_t * color_used ,
int * nb_used ,
2022-10-30 19:38:23 +02:00
const uint32_t * palette ,
const int trans_thresh ,
2015-01-24 22:38:15 +02:00
const struct color_rect * box )
{
int component , cur_id ;
2022-10-31 13:22:47 +02:00
int comp_value ;
2015-01-24 22:38:15 +02:00
int node_left_id = - 1 , node_right_id = - 1 ;
struct color_node * node ;
struct color_rect box1 , box2 ;
2022-11-05 02:13:54 +02:00
const int pal_id = get_next_color ( color_used , palette , & component , box ) ;
2015-01-24 22:38:15 +02:00
if ( pal_id < 0 )
return - 1 ;
/* create new node with that color */
cur_id = ( * nb_used ) + + ;
node = & map [ cur_id ] ;
node - > split = component ;
node - > palette_id = pal_id ;
2022-10-31 13:22:47 +02:00
node - > c = get_color_from_srgb ( palette [ pal_id ] ) ;
2015-01-24 22:38:15 +02:00
color_used [ pal_id ] = 1 ;
/* get the two boxes this node creates */
box1 = box2 = * box ;
2022-10-31 13:22:47 +02:00
comp_value = node - > c . lab [ component ] ;
2022-11-05 02:00:09 +02:00
box1 . max [ component ] = comp_value ;
2022-10-31 13:22:47 +02:00
box2 . min [ component ] = FFMIN ( comp_value + 1 , 0xffff ) ;
2015-01-24 22:38:15 +02:00
2022-10-30 19:38:23 +02:00
node_left_id = colormap_insert ( map , color_used , nb_used , palette , trans_thresh , & box1 ) ;
2015-01-24 22:38:15 +02:00
2022-11-05 02:00:09 +02:00
if ( box2 . min [ component ] < = box2 . max [ component ] )
2022-10-30 19:38:23 +02:00
node_right_id = colormap_insert ( map , color_used , nb_used , palette , trans_thresh , & box2 ) ;
2015-01-24 22:38:15 +02:00
node - > left_id = node_left_id ;
node - > right_id = node_right_id ;
return cur_id ;
}
static int cmp_pal_entry ( const void * a , const void * b )
{
const int c1 = * ( const uint32_t * ) a & 0xffffff ;
const int c2 = * ( const uint32_t * ) b & 0xffffff ;
return c1 - c2 ;
}
static void load_colormap ( PaletteUseContext * s )
{
2022-12-27 19:13:20 +02:00
int nb_used = 0 ;
2015-01-24 22:38:15 +02:00
uint8_t color_used [ AVPALETTE_COUNT ] = { 0 } ;
uint32_t last_color = 0 ;
struct color_rect box ;
2022-10-30 19:38:23 +02:00
if ( s - > transparency_index > = 0 ) {
2021-08-19 00:13:22 +02:00
FFSWAP ( uint32_t , s - > palette [ s - > transparency_index ] , s - > palette [ 255 ] ) ;
2017-10-24 01:12:57 +02:00
}
2021-08-19 00:13:22 +02:00
/* disable transparent colors and dups */
2022-10-30 19:38:23 +02:00
qsort ( s - > palette , AVPALETTE_COUNT - ( s - > transparency_index > = 0 ) , sizeof ( * s - > palette ) , cmp_pal_entry ) ;
2021-08-19 00:13:22 +02:00
2022-12-27 19:13:20 +02:00
for ( int i = 0 ; i < AVPALETTE_COUNT ; i + + ) {
2015-01-24 22:38:15 +02:00
const uint32_t c = s - > palette [ i ] ;
if ( i ! = 0 & & c = = last_color ) {
color_used [ i ] = 1 ;
continue ;
}
last_color = c ;
2022-10-30 19:38:23 +02:00
if ( c > > 24 < s - > trans_thresh ) {
2015-01-24 22:38:15 +02:00
color_used [ i ] = 1 ; // ignore transparent color(s)
continue ;
}
}
2022-10-31 13:22:47 +02:00
box . min [ 0 ] = box . min [ 1 ] = box . min [ 2 ] = - 0xffff ;
box . max [ 0 ] = box . max [ 1 ] = box . max [ 2 ] = 0xffff ;
2015-01-24 22:38:15 +02:00
2022-10-30 19:38:23 +02:00
colormap_insert ( s - > map , color_used , & nb_used , s - > palette , s - > trans_thresh , & box ) ;
2015-01-24 22:38:15 +02:00
if ( s - > dot_filename )
disp_tree ( s - > map , s - > dot_filename ) ;
}
2015-02-16 18:40:55 +02:00
static void set_processing_window ( enum diff_mode diff_mode ,
const AVFrame * prv_src , const AVFrame * cur_src ,
const AVFrame * prv_dst , AVFrame * cur_dst ,
int * xp , int * yp , int * wp , int * hp )
{
int x_start = 0 , y_start = 0 ;
int width = cur_src - > width ;
int height = cur_src - > height ;
2019-01-15 06:14:34 +02:00
if ( prv_src - > data [ 0 ] & & diff_mode = = DIFF_MODE_RECTANGLE ) {
2015-02-16 18:40:55 +02:00
int y ;
int x_end = cur_src - > width - 1 ,
y_end = cur_src - > height - 1 ;
const uint32_t * prv_srcp = ( const uint32_t * ) prv_src - > data [ 0 ] ;
const uint32_t * cur_srcp = ( const uint32_t * ) cur_src - > data [ 0 ] ;
const uint8_t * prv_dstp = prv_dst - > data [ 0 ] ;
uint8_t * cur_dstp = cur_dst - > data [ 0 ] ;
const int prv_src_linesize = prv_src - > linesize [ 0 ] > > 2 ;
const int cur_src_linesize = cur_src - > linesize [ 0 ] > > 2 ;
const int prv_dst_linesize = prv_dst - > linesize [ 0 ] ;
const int cur_dst_linesize = cur_dst - > linesize [ 0 ] ;
/* skip common lines */
while ( y_start < y_end & & ! memcmp ( prv_srcp + y_start * prv_src_linesize ,
cur_srcp + y_start * cur_src_linesize ,
cur_src - > width * 4 ) ) {
memcpy ( cur_dstp + y_start * cur_dst_linesize ,
prv_dstp + y_start * prv_dst_linesize ,
cur_dst - > width ) ;
y_start + + ;
}
while ( y_end > y_start & & ! memcmp ( prv_srcp + y_end * prv_src_linesize ,
cur_srcp + y_end * cur_src_linesize ,
cur_src - > width * 4 ) ) {
memcpy ( cur_dstp + y_end * cur_dst_linesize ,
prv_dstp + y_end * prv_dst_linesize ,
cur_dst - > width ) ;
y_end - - ;
}
height = y_end + 1 - y_start ;
/* skip common columns */
while ( x_start < x_end ) {
int same_column = 1 ;
for ( y = y_start ; y < = y_end ; y + + ) {
if ( prv_srcp [ y * prv_src_linesize + x_start ] ! = cur_srcp [ y * cur_src_linesize + x_start ] ) {
same_column = 0 ;
break ;
}
}
if ( ! same_column )
break ;
x_start + + ;
}
while ( x_end > x_start ) {
int same_column = 1 ;
for ( y = y_start ; y < = y_end ; y + + ) {
if ( prv_srcp [ y * prv_src_linesize + x_end ] ! = cur_srcp [ y * cur_src_linesize + x_end ] ) {
same_column = 0 ;
break ;
}
}
if ( ! same_column )
break ;
x_end - - ;
}
width = x_end + 1 - x_start ;
if ( x_start ) {
for ( y = y_start ; y < = y_end ; y + + )
memcpy ( cur_dstp + y * cur_dst_linesize ,
prv_dstp + y * prv_dst_linesize , x_start ) ;
}
if ( x_end ! = cur_src - > width - 1 ) {
const int copy_len = cur_src - > width - 1 - x_end ;
for ( y = y_start ; y < = y_end ; y + + )
memcpy ( cur_dstp + y * cur_dst_linesize + x_end + 1 ,
prv_dstp + y * prv_dst_linesize + x_end + 1 ,
copy_len ) ;
}
}
* xp = x_start ;
* yp = y_start ;
* wp = width ;
* hp = height ;
}
2018-01-01 18:28:36 +02:00
static int apply_palette ( AVFilterLink * inlink , AVFrame * in , AVFrame * * outf )
2015-01-24 22:38:15 +02:00
{
2018-01-01 18:28:36 +02:00
int x , y , w , h , ret ;
2015-01-24 22:38:15 +02:00
AVFilterContext * ctx = inlink - > dst ;
PaletteUseContext * s = ctx - > priv ;
AVFilterLink * outlink = inlink - > dst - > outputs [ 0 ] ;
AVFrame * out = ff_get_video_buffer ( outlink , outlink - > w , outlink - > h ) ;
if ( ! out ) {
2018-01-01 18:28:36 +02:00
* outf = NULL ;
return AVERROR ( ENOMEM ) ;
2015-01-24 22:38:15 +02:00
}
av_frame_copy_props ( out , in ) ;
2015-02-16 18:40:55 +02:00
set_processing_window ( s - > diff_mode , s - > last_in , in ,
s - > last_out , out , & x , & y , & w , & h ) ;
2019-01-15 06:14:34 +02:00
av_frame_unref ( s - > last_out ) ;
2023-09-07 12:02:26 +02:00
if ( ( ret = av_frame_replace ( s - > last_in , in ) ) < 0 | |
2020-01-27 09:40:08 +02:00
( ret = av_frame_ref ( s - > last_out , out ) ) < 0 | |
2023-02-06 15:57:50 +02:00
( ret = ff_inlink_make_frame_writable ( inlink , & s - > last_in ) ) < 0 ) {
2015-01-24 22:38:15 +02:00
av_frame_free ( & out ) ;
2018-01-01 18:28:36 +02:00
* outf = NULL ;
2020-01-27 09:40:08 +02:00
return ret ;
2015-01-24 22:38:15 +02:00
}
2015-02-16 18:40:55 +02:00
2015-08-18 02:45:35 +02:00
ff_dlog ( ctx , " %dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d] \n " ,
2015-02-16 18:40:55 +02:00
w , h , x , y , x + w , y + h , in - > width , in - > height ) ;
2018-01-01 18:28:36 +02:00
ret = s - > set_frame ( s , out , in , x , y , w , h ) ;
if ( ret < 0 ) {
2015-02-16 18:40:55 +02:00
av_frame_free ( & out ) ;
2018-01-01 18:28:36 +02:00
* outf = NULL ;
return ret ;
2015-02-16 18:40:55 +02:00
}
2015-01-24 22:38:15 +02:00
memcpy ( out - > data [ 1 ] , s - > palette , AVPALETTE_SIZE ) ;
2018-01-01 18:28:36 +02:00
* outf = out ;
return 0 ;
2015-01-24 22:38:15 +02:00
}
static int config_output ( AVFilterLink * outlink )
{
int ret ;
AVFilterContext * ctx = outlink - > src ;
PaletteUseContext * s = ctx - > priv ;
2017-08-31 19:47:37 +02:00
ret = ff_framesync_init_dualinput ( & s - > fs , ctx ) ;
2017-07-31 12:27:58 +02:00
if ( ret < 0 )
return ret ;
s - > fs . opt_repeatlast = 1 ; // only 1 frame in the palette
s - > fs . in [ 1 ] . before = s - > fs . in [ 1 ] . after = EXT_INFINITY ;
s - > fs . on_event = load_apply_palette ;
2015-01-24 22:38:15 +02:00
outlink - > w = ctx - > inputs [ 0 ] - > w ;
outlink - > h = ctx - > inputs [ 0 ] - > h ;
outlink - > time_base = ctx - > inputs [ 0 ] - > time_base ;
2017-08-31 19:47:37 +02:00
if ( ( ret = ff_framesync_configure ( & s - > fs ) ) < 0 )
2015-01-24 22:38:15 +02:00
return ret ;
return 0 ;
}
static int config_input_palette ( AVFilterLink * inlink )
{
AVFilterContext * ctx = inlink - > dst ;
if ( inlink - > w * inlink - > h ! = AVPALETTE_COUNT ) {
av_log ( ctx , AV_LOG_ERROR ,
" Palette input must contain exactly %d pixels. "
" Specified input has %dx%d=%d pixels \n " ,
AVPALETTE_COUNT , inlink - > w , inlink - > h ,
inlink - > w * inlink - > h ) ;
return AVERROR ( EINVAL ) ;
}
return 0 ;
}
static void load_palette ( PaletteUseContext * s , const AVFrame * palette_frame )
{
int i , x , y ;
const uint32_t * p = ( const uint32_t * ) palette_frame - > data [ 0 ] ;
2023-09-22 09:15:13 +02:00
const ptrdiff_t p_linesize = palette_frame - > linesize [ 0 ] > > 2 ;
2015-01-24 22:38:15 +02:00
2017-10-24 01:12:57 +02:00
s - > transparency_index = - 1 ;
2016-09-02 22:40:34 +02:00
if ( s - > new ) {
memset ( s - > palette , 0 , sizeof ( s - > palette ) ) ;
memset ( s - > map , 0 , sizeof ( s - > map ) ) ;
for ( i = 0 ; i < CACHE_SIZE ; i + + )
av_freep ( & s - > cache [ i ] . entries ) ;
memset ( s - > cache , 0 , sizeof ( s - > cache ) ) ;
}
2015-01-24 22:38:15 +02:00
i = 0 ;
for ( y = 0 ; y < palette_frame - > height ; y + + ) {
2017-10-24 01:12:57 +02:00
for ( x = 0 ; x < palette_frame - > width ; x + + ) {
s - > palette [ i ] = p [ x ] ;
2022-10-30 19:38:23 +02:00
if ( p [ x ] > > 24 < s - > trans_thresh ) {
2017-10-24 01:12:57 +02:00
s - > transparency_index = i ; // we are assuming at most one transparent color in palette
}
i + + ;
}
2015-01-24 22:38:15 +02:00
p + = p_linesize ;
}
load_colormap ( s ) ;
2016-09-02 22:40:34 +02:00
if ( ! s - > new )
s - > palette_loaded = 1 ;
2015-01-24 22:38:15 +02:00
}
2017-07-31 12:27:58 +02:00
static int load_apply_palette ( FFFrameSync * fs )
2015-01-24 22:38:15 +02:00
{
2017-07-31 12:27:58 +02:00
AVFilterContext * ctx = fs - > parent ;
2015-01-24 22:38:15 +02:00
AVFilterLink * inlink = ctx - > inputs [ 0 ] ;
PaletteUseContext * s = ctx - > priv ;
2018-01-01 18:28:36 +02:00
AVFrame * master , * second , * out = NULL ;
2017-07-31 12:27:58 +02:00
int ret ;
// writable for error diffusal dithering
2017-10-07 15:59:22 +02:00
ret = ff_framesync_dualinput_get_writable ( fs , & master , & second ) ;
2017-07-31 12:27:58 +02:00
if ( ret < 0 )
return ret ;
2017-10-07 15:59:22 +02:00
if ( ! master | | ! second ) {
2020-01-27 10:28:20 +02:00
av_frame_free ( & master ) ;
return AVERROR_BUG ;
2017-07-31 12:27:58 +02:00
}
2015-01-24 22:38:15 +02:00
if ( ! s - > palette_loaded ) {
load_palette ( s , second ) ;
}
2018-01-01 18:28:36 +02:00
ret = apply_palette ( inlink , master , & out ) ;
2020-01-27 10:28:20 +02:00
av_frame_free ( & master ) ;
2018-01-01 18:28:36 +02:00
if ( ret < 0 )
2020-01-27 10:28:20 +02:00
return ret ;
2017-07-31 12:27:58 +02:00
return ff_filter_frame ( ctx - > outputs [ 0 ] , out ) ;
2015-01-24 22:38:15 +02:00
}
2022-12-27 19:03:46 +02:00
# define DEFINE_SET_FRAME(name, value) \
2015-02-16 18:40:55 +02:00
static int set_frame_ # # name ( PaletteUseContext * s , AVFrame * out , AVFrame * in , \
int x_start , int y_start , int w , int h ) \
2015-01-24 22:38:15 +02:00
{ \
2022-12-27 19:03:46 +02:00
return set_frame ( s , out , in , x_start , y_start , w , h , value ) ; \
2015-01-24 22:38:15 +02:00
}
2022-12-27 19:03:46 +02:00
DEFINE_SET_FRAME ( none , DITHERING_NONE )
DEFINE_SET_FRAME ( bayer , DITHERING_BAYER )
DEFINE_SET_FRAME ( heckbert , DITHERING_HECKBERT )
DEFINE_SET_FRAME ( floyd_steinberg , DITHERING_FLOYD_STEINBERG )
DEFINE_SET_FRAME ( sierra2 , DITHERING_SIERRA2 )
DEFINE_SET_FRAME ( sierra2_4a , DITHERING_SIERRA2_4A )
2022-12-30 12:37:20 +02:00
DEFINE_SET_FRAME ( sierra3 , DITHERING_SIERRA3 )
2022-12-30 12:38:01 +02:00
DEFINE_SET_FRAME ( burkes , DITHERING_BURKES )
2022-12-30 12:38:13 +02:00
DEFINE_SET_FRAME ( atkinson , DITHERING_ATKINSON )
2022-12-27 19:03:46 +02:00
static const set_frame_func set_frame_lut [ NB_DITHERING ] = {
2022-12-30 12:44:21 +02:00
[ DITHERING_NONE ] = set_frame_none ,
[ DITHERING_BAYER ] = set_frame_bayer ,
[ DITHERING_HECKBERT ] = set_frame_heckbert ,
[ DITHERING_FLOYD_STEINBERG ] = set_frame_floyd_steinberg ,
[ DITHERING_SIERRA2 ] = set_frame_sierra2 ,
[ DITHERING_SIERRA2_4A ] = set_frame_sierra2_4a ,
[ DITHERING_SIERRA3 ] = set_frame_sierra3 ,
[ DITHERING_BURKES ] = set_frame_burkes ,
[ DITHERING_ATKINSON ] = set_frame_atkinson ,
2015-01-24 22:38:15 +02:00
} ;
static int dither_value ( int p )
{
const int q = p ^ ( p > > 3 ) ;
return ( p & 4 ) > > 2 | ( q & 4 ) > > 1 \
| ( p & 2 ) < < 1 | ( q & 2 ) < < 2 \
| ( p & 1 ) < < 4 | ( q & 1 ) < < 5 ;
}
static av_cold int init ( AVFilterContext * ctx )
{
PaletteUseContext * s = ctx - > priv ;
2019-01-15 06:14:34 +02:00
s - > last_in = av_frame_alloc ( ) ;
s - > last_out = av_frame_alloc ( ) ;
2021-08-01 22:11:06 +02:00
if ( ! s - > last_in | | ! s - > last_out )
2019-01-15 06:14:34 +02:00
return AVERROR ( ENOMEM ) ;
2022-12-27 19:03:46 +02:00
s - > set_frame = set_frame_lut [ s - > dither ] ;
2015-01-24 22:38:15 +02:00
if ( s - > dither = = DITHERING_BAYER ) {
const int delta = 1 < < ( 5 - s - > bayer_scale ) ; // to avoid too much luma
2022-12-27 19:13:20 +02:00
for ( int i = 0 ; i < FF_ARRAY_ELEMS ( s - > ordered_dither ) ; i + + )
2015-01-24 22:38:15 +02:00
s - > ordered_dither [ i ] = ( dither_value ( i ) > > s - > bayer_scale ) - delta ;
}
return 0 ;
}
2017-07-31 12:27:58 +02:00
static int activate ( AVFilterContext * ctx )
2015-01-24 22:38:15 +02:00
{
2017-07-31 12:27:58 +02:00
PaletteUseContext * s = ctx - > priv ;
2017-08-31 19:47:37 +02:00
return ff_framesync_activate ( & s - > fs ) ;
2015-01-24 22:38:15 +02:00
}
static av_cold void uninit ( AVFilterContext * ctx )
{
PaletteUseContext * s = ctx - > priv ;
2017-08-31 19:47:37 +02:00
ff_framesync_uninit ( & s - > fs ) ;
2022-12-27 19:13:20 +02:00
for ( int i = 0 ; i < CACHE_SIZE ; i + + )
2015-01-24 22:38:15 +02:00
av_freep ( & s - > cache [ i ] . entries ) ;
2015-02-16 18:40:55 +02:00
av_frame_free ( & s - > last_in ) ;
av_frame_free ( & s - > last_out ) ;
2015-01-24 22:38:15 +02:00
}
static const AVFilterPad paletteuse_inputs [ ] = {
{
. name = " default " ,
. type = AVMEDIA_TYPE_VIDEO ,
} , {
. name = " palette " ,
. type = AVMEDIA_TYPE_VIDEO ,
. config_props = config_input_palette ,
} ,
} ;
static const AVFilterPad paletteuse_outputs [ ] = {
{
. name = " default " ,
. type = AVMEDIA_TYPE_VIDEO ,
. config_props = config_output ,
} ,
} ;
2021-04-19 18:33:56 +02:00
const AVFilter ff_vf_paletteuse = {
2015-01-24 22:38:15 +02:00
. name = " paletteuse " ,
. description = NULL_IF_CONFIG_SMALL ( " Use a palette to downsample an input video stream. " ) ,
. priv_size = sizeof ( PaletteUseContext ) ,
. init = init ,
. uninit = uninit ,
2017-07-31 12:27:58 +02:00
. activate = activate ,
2021-08-12 13:05:31 +02:00
FILTER_INPUTS ( paletteuse_inputs ) ,
FILTER_OUTPUTS ( paletteuse_outputs ) ,
2021-09-27 12:07:35 +02:00
FILTER_QUERY_FUNC ( query_formats ) ,
2015-01-24 22:38:15 +02:00
. priv_class = & paletteuse_class ,
} ;