57 "red",
"yellow",
"green",
"cyan",
"blue",
"magenta",
"white",
"neutral",
"black"
82 #define OFFSET(x) offsetof(SelectiveColorContext, x)
83 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
84 #define RANGE_OPTION(color_name, range) \
85 { color_name"s", "adjust "color_name" regions", OFFSET(opt_cmyk_adjust[range]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS }
108 if ((r < g && r > b) || (r < b && r > g))
return r;
109 if ((g < r && g > b) || (g < b && g > r))
return g;
110 if ((b < r && b > g) || (b < g && b > r))
return b;
120 if ((r != min_val && g == min_val && b == min_val) ||
121 (r == min_val && g != min_val && b == min_val) ||
122 (r == min_val && g == min_val && b != min_val))
123 return max_val - min_val;
126 return max_val - mid_val;
135 if ((r != max_val && g == max_val && b == max_val) ||
136 (r == max_val && g != max_val && b == max_val) ||
137 (r == max_val && g == max_val && b != max_val))
138 return max_val - min_val;
141 return mid_val - min_val;
147 return (255*2 - (abs((max_val<<1) - 255) + abs((min_val<<1) - 255)) + 1) >> 1;
153 return (min_val<<1) - 255;
159 return 255 - (max_val<<1);
168 if (cmyk[0] || cmyk[1] || cmyk[2] || cmyk[3]) {
171 if (cmyk[0] < -1.0 || cmyk[0] > 1.0 ||
172 cmyk[1] < -1.0 || cmyk[1] > 1.0 ||
173 cmyk[2] < -1.0 || cmyk[2] > 1.0 ||
174 cmyk[3] < -1.0 || cmyk[3] > 1.0) {
176 "Settings must be set in [-1;1] range\n",
177 color_names[range_id], cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
206 #define READ16(dst) do { \
208 ret = AVERROR_INVALIDDATA; \
211 dst = AV_RB16(buf); \
219 "the settings might not be loaded properly\n", version);
228 "but %d\n",
"CMYK"[i], val);
265 if (opt_cmyk_adjust) {
268 sscanf(s->
opt_cmyk_adjust[i],
"%f %f %f %f", cmyk, cmyk+1, cmyk+2, cmyk+3);
300 const float max = 1. -
value;
301 float res = (-1. -
adjust) * k - adjust;
304 return lrint(av_clipf(res, min, max) * adjust_range);
316 const int slice_start = (height * jobnr ) / nb_jobs;
317 const int slice_end = (height * (jobnr+1)) / nb_jobs;
318 const int dst_linesize = out->
linesize[0];
319 const int src_linesize = in->
linesize[0];
320 uint8_t *dst = out->
data[0] + slice_start * dst_linesize;
323 for (y = slice_start; y <
slice_end; y++) {
324 const uint32_t *src32 = (
const uint32_t *)src;
325 uint32_t *dst32 = (uint32_t *)dst;
327 for (x = 0; x <
width; x++) {
328 const uint32_t
color = *src32++;
329 const int r = color >> 16 & 0xff;
330 const int g = color >> 8 & 0xff;
331 const int b = color & 0xff;
332 const int min_color =
FFMIN3(r, g, b);
333 const int max_color =
FFMAX3(r, g, b);
334 const uint32_t range_flag = (r == max_color) <<
RANGE_REDS
344 const float rnorm = r / 255.;
345 const float gnorm = g / 255.;
346 const float bnorm = b / 255.;
347 int adjust_r = 0, adjust_g = 0, adjust_b = 0;
352 if (range_flag & pr->
mask) {
353 const int adjust_range = pr->
get_adjust_range(r, g, b, min_color, max_color);
355 if (adjust_range > 0) {
357 const float adj_c = cmyk_adjust[0];
358 const float adj_m = cmyk_adjust[1];
359 const float adj_y = cmyk_adjust[2];
360 const float k = cmyk_adjust[3];
362 adjust_r +=
comp_adjust(adjust_range, rnorm, adj_c, k, correction_method);
363 adjust_g +=
comp_adjust(adjust_range, gnorm, adj_m, k, correction_method);
364 adjust_b +=
comp_adjust(adjust_range, bnorm, adj_y, k, correction_method);
369 if (!direct || adjust_r || adjust_g || adjust_b)
370 *dst32 = (color & 0xff000000)
371 | av_clip_uint8(r + adjust_r) << 16
372 | av_clip_uint8(g + adjust_g) << 8
373 | av_clip_uint8(b + adjust_b);
382 #define DEF_SELECTIVE_COLOR_FUNC(name, direct, correction_method) \
383 static int selective_color_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
385 return selective_color(ctx, arg, jobnr, nb_jobs, direct, correction_method); \
404 {selective_color_indirect_absolute, selective_color_indirect_relative},
405 {selective_color_direct_absolute, selective_color_direct_relative},
449 .
name =
"selectivecolor",
454 .
inputs = selectivecolor_inputs,
455 .
outputs = selectivecolor_outputs,
456 .priv_class = &selectivecolor_class,
const char const char void * val
static int get_neutrals_adjust_range(int r, int g, int b, int min_val, int max_val)
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.
static int get_whites_adjust_range(int r, int g, int b, int min_val, int max_val)
static int get_rgb_adjust_range(int r, int g, int b, int min_val, int max_val)
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)
static int selective_color(AVFilterContext *ctx, ThreadData *td, int jobnr, int nb_jobs, int direct, int correction_method)
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)
struct AVFilterGraph * graph
filtergraph this filter belongs to
#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.
static av_cold int end(AVCodecContext *avctx)
static int register_range(SelectiveColorContext *s, int range_id)
char * opt_cmyk_adjust[NB_RANGES]
int nb_threads
Maximum number of threads used by filters in this graph.
#define AV_LOG_VERBOSE
Detailed information.
A filter pad used for either input or output.
A link between two filters.
int width
width and height of the video frame
#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)
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. ...
void * priv
private data for use by the filter
#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().
static int get_blacks_adjust_range(int r, int g, int b, int min_val, int max_val)
#define RANGE_OPTION(color_name, range)
static const AVFilterPad selectivecolor_outputs[]
int w
agreed upon image width
#define DEF_SELECTIVE_COLOR_FUNC(name, direct, correction_method)
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
static const AVFilterPad outputs[]
#define FF_ARRAY_ELEMS(a)
static const AVFilterPad inputs[]
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
float cmyk_adjust[NB_RANGES][4]
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
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
static av_cold int init(AVFilterContext *ctx)
Describe the class of an AVClass context structure.
static const AVOption selectivecolor_options[]
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.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int parse_psfile(AVFilterContext *ctx, const char *fname)
static int query_formats(AVFilterContext *ctx)
avfilter_execute_func * execute
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
static int comp_adjust(int adjust_range, float value, float adjust, float k, int correction_method)
AVPixelFormat
Pixel format.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define AV_PIX_FMT_0RGB32