63 "red",
"yellow",
"green",
"cyan",
"blue",
"magenta",
"white",
"neutral",
"black"
91 #define OFFSET(x) offsetof(SelectiveColorContext, x)
92 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
93 #define RANGE_OPTION(color_name, range) \
94 { color_name"s", "adjust "color_name" regions", OFFSET(opt_cmyk_adjust[range]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS }
117 if ((r < g && r > b) || (r < b && r > g))
return r;
118 if ((g < r && g > b) || (g < b && g > r))
return g;
119 if ((b < r && b > g) || (b < g && b > r))
return b;
129 if ((r != min_val && g == min_val && b == min_val) ||
130 (r == min_val && g != min_val && b == min_val) ||
131 (r == min_val && g == min_val && b != min_val))
132 return max_val - min_val;
135 return max_val - mid_val;
144 if ((r != max_val && g == max_val && b == max_val) ||
145 (r == max_val && g != max_val && b == max_val) ||
146 (r == max_val && g == max_val && b != max_val))
147 return max_val - min_val;
150 return mid_val - min_val;
153 #define DECLARE_ADJUST_RANGE_FUNCS(nbits) \
154 static int get_neutrals_adjust_range##nbits(int r, int g, int b, int min_val, int max_val) \
157 return (((1<<nbits)-1)*2 - ( abs((max_val<<1) - ((1<<nbits)-1)) \
158 + abs((min_val<<1) - ((1<<nbits)-1))) + 1) >> 1; \
161 static int get_whites_adjust_range##nbits(int r, int g, int b, int min_val, int max_val) \
164 return (min_val<<1) - ((1<<nbits)-1); \
167 static int get_blacks_adjust_range##nbits(int r, int g, int b, int min_val, int max_val) \
170 return ((1<<nbits)-1) - (max_val<<1); \
178 const float *cmyk = s->cmyk_adjust[
range_id];
182 if (cmyk[0] || cmyk[1] || cmyk[2] || cmyk[3]) {
183 struct process_range *pr = &s->process_ranges[s->nb_process_ranges++];
185 if (cmyk[0] < -1.0 || cmyk[0] > 1.0 ||
186 cmyk[1] < -1.0 || cmyk[1] > 1.0 ||
187 cmyk[2] < -1.0 || cmyk[2] > 1.0 ||
188 cmyk[3] < -1.0 || cmyk[3] > 1.0) {
190 "Settings must be set in [-1;1] range\n",
191 color_names[range_id], cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
223 #define READ16(dst) do { \
225 ret = AVERROR_INVALIDDATA; \
228 dst = AV_RB16(buf); \
236 "the settings might not be loaded properly\n", version);
245 "but %d\n",
"CMYK"[i], val);
291 if (opt_cmyk_adjust) {
294 sscanf(s->
opt_cmyk_adjust[i],
"%f %f %f %f", cmyk, cmyk+1, cmyk+2, cmyk+3);
335 const float max = 1. -
value;
336 float res = (-1. -
adjust) * k - adjust;
339 return lrint(av_clipf(res, min, max) * adjust_range);
342 #define DECLARE_SELECTIVE_COLOR_FUNC(nbits) \
343 static inline int selective_color_##nbits(AVFilterContext *ctx, ThreadData *td, \
344 int jobnr, int nb_jobs, int direct, int correction_method) \
347 const AVFrame *in = td->in; \
348 AVFrame *out = td->out; \
349 const SelectiveColorContext *s = ctx->priv; \
350 const int height = in->height; \
351 const int width = in->width; \
352 const int slice_start = (height * jobnr ) / nb_jobs; \
353 const int slice_end = (height * (jobnr+1)) / nb_jobs; \
354 const int dst_linesize = out->linesize[0]; \
355 const int src_linesize = in->linesize[0]; \
356 const uint8_t roffset = s->rgba_map[R]; \
357 const uint8_t goffset = s->rgba_map[G]; \
358 const uint8_t boffset = s->rgba_map[B]; \
359 const uint8_t aoffset = s->rgba_map[A]; \
361 for (y = slice_start; y < slice_end; y++) { \
362 uint##nbits##_t *dst = ( uint##nbits##_t *)(out->data[0] + y * dst_linesize); \
363 const uint##nbits##_t *src = (const uint##nbits##_t *)( in->data[0] + y * src_linesize); \
365 for (x = 0; x < width * s->step; x += s->step) { \
366 const int r = src[x + roffset]; \
367 const int g = src[x + goffset]; \
368 const int b = src[x + boffset]; \
369 const int min_color = FFMIN3(r, g, b); \
370 const int max_color = FFMAX3(r, g, b); \
371 const int is_white = (r > 1<<(nbits-1) && g > 1<<(nbits-1) && b > 1<<(nbits-1)); \
372 const int is_neutral = (r || g || b) && \
373 r != (1<<nbits)-1 && g != (1<<nbits)-1 && b != (1<<nbits)-1; \
374 const int is_black = (r < 1<<(nbits-1) && g < 1<<(nbits-1) && b < 1<<(nbits-1)); \
375 const uint32_t range_flag = (r == max_color) << RANGE_REDS \
376 | (r == min_color) << RANGE_CYANS \
377 | (g == max_color) << RANGE_GREENS \
378 | (g == min_color) << RANGE_MAGENTAS \
379 | (b == max_color) << RANGE_BLUES \
380 | (b == min_color) << RANGE_YELLOWS \
381 | is_white << RANGE_WHITES \
382 | is_neutral << RANGE_NEUTRALS \
383 | is_black << RANGE_BLACKS; \
385 const float rnorm = r * (1.f / ((1<<nbits)-1)); \
386 const float gnorm = g * (1.f / ((1<<nbits)-1)); \
387 const float bnorm = b * (1.f / ((1<<nbits)-1)); \
388 int adjust_r = 0, adjust_g = 0, adjust_b = 0; \
390 for (i = 0; i < s->nb_process_ranges; i++) { \
391 const struct process_range *pr = &s->process_ranges[i]; \
393 if (range_flag & pr->mask) { \
394 const int adjust_range = pr->get_adjust_range(r, g, b, min_color, max_color); \
396 if (adjust_range > 0) { \
397 const float *cmyk_adjust = s->cmyk_adjust[pr->range_id]; \
398 const float adj_c = cmyk_adjust[0]; \
399 const float adj_m = cmyk_adjust[1]; \
400 const float adj_y = cmyk_adjust[2]; \
401 const float k = cmyk_adjust[3]; \
403 adjust_r += comp_adjust(adjust_range, rnorm, adj_c, k, correction_method); \
404 adjust_g += comp_adjust(adjust_range, gnorm, adj_m, k, correction_method); \
405 adjust_b += comp_adjust(adjust_range, bnorm, adj_y, k, correction_method); \
410 if (!direct || adjust_r || adjust_g || adjust_b) { \
411 dst[x + roffset] = av_clip_uint##nbits(r + adjust_r); \
412 dst[x + goffset] = av_clip_uint##nbits(g + adjust_g); \
413 dst[x + boffset] = av_clip_uint##nbits(b + adjust_b); \
414 if (!direct && s->step == 4) \
415 dst[x + aoffset] = src[x + aoffset]; \
422 #define DEF_SELECTIVE_COLOR_FUNC(name, direct, correction_method, nbits) \
423 static int selective_color_##name##_##nbits(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
425 return selective_color_##nbits(ctx, arg, jobnr, nb_jobs, direct, correction_method); \
428 #define DEF_SELECTIVE_COLOR_FUNCS(nbits) \
429 DECLARE_SELECTIVE_COLOR_FUNC(nbits) \
430 DEF_SELECTIVE_COLOR_FUNC(indirect_absolute, 0, CORRECTION_METHOD_ABSOLUTE, nbits) \
431 DEF_SELECTIVE_COLOR_FUNC(indirect_relative, 0, CORRECTION_METHOD_RELATIVE, nbits) \
432 DEF_SELECTIVE_COLOR_FUNC( direct_absolute, 1, CORRECTION_METHOD_ABSOLUTE, nbits) \
433 DEF_SELECTIVE_COLOR_FUNC( direct_relative, 1, CORRECTION_METHOD_RELATIVE, nbits)
450 {selective_color_indirect_absolute_8, selective_color_indirect_relative_8},
451 {selective_color_direct_absolute_8, selective_color_direct_relative_8},
453 {selective_color_indirect_absolute_16, selective_color_indirect_relative_16},
454 {selective_color_direct_absolute_16, selective_color_direct_relative_16},
500 .
name =
"selectivecolor",
504 .
inputs = selectivecolor_inputs,
505 .
outputs = selectivecolor_outputs,
506 .priv_class = &selectivecolor_class,
const char const char void * val
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
#define AV_LOG_WARNING
Something somehow does not look correct.
get_adjust_range_func get_adjust_range
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
static int get_rgb_adjust_range(int r, int g, int b, int min_val, int max_val)
#define AV_PIX_FMT_RGBA64
int h
agreed upon image height
static int get_mid_val(int r, int g, int b)
AVFilter ff_vf_selectivecolor
int(* selective_color_func_type)(AVFilterContext *ctx, void *td, int jobnr, int nb_jobs)
#define AV_PIX_FMT_BGRA64
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
static const AVFilterPad selectivecolor_inputs[]
const char * name
Pad name.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
struct process_range process_ranges[NB_RANGES]
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
static av_cold int end(AVCodecContext *avctx)
static int register_range(SelectiveColorContext *s, int range_id)
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
char * opt_cmyk_adjust[NB_RANGES]
#define AV_LOG_VERBOSE
Detailed information.
A filter pad used for either input or output.
A link between two filters.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
AVFILTER_DEFINE_CLASS(selectivecolor)
static int config_input(AVFilterLink *inlink)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
void * priv
private data for use by the filter
int av_get_padded_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel for the pixel format described by pixdesc, including any padding ...
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
simple assert() macros that are a bit more flexible than ISO C assert().
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
#define RANGE_OPTION(color_name, range)
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
static const AVFilterPad selectivecolor_outputs[]
#define DEF_SELECTIVE_COLOR_FUNCS(nbits)
int w
agreed upon image width
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
int(* get_adjust_range_func)(int r, int g, int b, int min_val, int max_val)
static int get_cmy_adjust_range(int r, int g, int b, int min_val, int max_val)
GLsizei GLboolean const GLfloat * value
#define DECLARE_ADJUST_RANGE_FUNCS(nbits)
packed RGB 8:8:8, 24bpp, BGRBGR...
static const AVFilterPad outputs[]
int format
agreed upon media format
#define FF_ARRAY_ELEMS(a)
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
static const AVFilterPad inputs[]
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
float cmyk_adjust[NB_RANGES][4]
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static const char * color_names[NB_RANGES]
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
static const AVOption selectivecolor_options[]
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
const char * name
Filter name.
AVFilterLink ** outputs
array of pointers to output links
static enum AVPixelFormat pix_fmts[]
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
static int parse_psfile(AVFilterContext *ctx, const char *fname)
static int query_formats(AVFilterContext *ctx)
avfilter_execute_func * execute
AVFilterContext * dst
dest filter
static int comp_adjust(int adjust_range, float value, float adjust, float k, int correction_method)
int depth
Number of bits in the component.
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
AVPixelFormat
Pixel format.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.