Go to the documentation of this file.
91 #define OFFSET(x) offsetof(CurvesContext, x)
92 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
125 static const struct {
132 "0.129/1 0.466/0.498 0.725/0",
133 "0.109/1 0.301/0.498 0.517/0",
134 "0.098/1 0.235/0.498 0.423/0",
137 "0/0 0.25/0.156 0.501/0.501 0.686/0.745 1/1",
138 "0/0 0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
139 "0/0 0.231/0.094 0.709/0.874 1/1",
149 "0/0.11 0.42/0.51 1/0.95",
151 "0/0.22 0.49/0.44 1/0.8",
172 const int scale = lut_size - 1;
179 point->
x =
av_strtod(p, &p);
if (p && *p) p++;
180 point->
y =
av_strtod(p, &p);
if (p && *p) p++;
181 if (point->
x < 0 || point->
x > 1 || point->
y < 0 || point->
y > 1) {
183 "x and y must be in the [0;1] range.\n", point->
x, point->
y);
189 if ((
int)(last->
x *
scale) >= (
int)(point->
x *
scale)) {
191 "and (%f;%f) are too close from each other or not "
192 "strictly increasing on the x-axis\n",
193 last->
x, last->
y, point->
x, point->
y);
201 if (*points && !(*points)->
next) {
203 "this is unlikely to behave as you expect. You probably want"
204 "at least 2 points.",
205 (*points)->x, (*points)->y);
227 #define CLIP(v) (nbits == 8 ? av_clip_uint8(v) : av_clip_uintp2_c(v, nbits))
230 const struct keypoint *points,
int nbits)
233 const struct keypoint *point = points;
235 const int lut_size = 1<<nbits;
236 const int scale = lut_size - 1;
243 for (
i = 0;
i < lut_size;
i++)
249 for (
i = 0;
i < lut_size;
i++)
265 for (point = points; point; point = point->
next) {
267 h[
i] = point->
x - xprev;
274 for (
i = 1;
i < n - 1;
i++) {
275 const double yp = point->
y;
276 const double yc = point->
next->
y;
278 r[
i] = 6 * ((yn-yc)/
h[
i] - (yc-yp)/
h[
i-1]);
288 for (
i = 1;
i < n - 1;
i++) {
295 for (
i = 1;
i < n;
i++) {
297 const double k = den ? 1./den : 1.;
301 for (
i = n - 2;
i >= 0;
i--)
313 while (point->
next) {
314 const double yc = point->
y;
315 const double yn = point->
next->
y;
318 const double b = (yn-yc)/
h[
i] -
h[
i]*
r[
i]/2. -
h[
i]*(
r[
i+1]-
r[
i])/6.;
319 const double c =
r[
i] / 2.;
320 const double d = (
r[
i+1] -
r[
i]) / (6.*
h[
i]);
323 const int x_start = point->
x *
scale;
326 av_assert0(x_start >= 0 && x_start < lut_size &&
327 x_end >= 0 && x_end < lut_size);
329 for (
x = x_start;
x <= x_end;
x++) {
330 const double xx = (
x - x_start) * 1./
scale;
331 const double yy =
a +
b*xx +
c*xx*xx +
d*xx*xx*xx;
341 for (
i = (
int)(point->
x *
scale);
i < lut_size;
i++)
352 #define SIGN(x) (x > 0.0 ? 1 : x < 0.0 ? -1 : 0)
373 d = ((2 *
h0 + h1) * m0 -
h0 * m1) / (
h0 + h1);
379 else if (mask2)
d = 3.0 * m0;
410 for (
int i = 0;
i < n;
i++) smk[
i] =
SIGN(mk[
i]);
413 for (
int i = 0;
i < m;
i++) {
414 int8_t condition = (smk[
i + 1] != smk[
i]) || (mk[
i + 1] == 0) || (mk[
i] == 0);
418 double w1 = 2 * hk[
i + 1] + hk[
i];
419 double w2 = hk[
i + 1] + 2 * hk[
i];
420 dk[
i + 1] = (w1 + w2) / (w1 / mk[
i] + w2 / mk[
i + 1]);
444 double x2 =
x *
x, x3 = x2 *
x;
445 return f * (3.0 * x2 - 2.0 * x3) +
d * (x3 - x2);
463 const struct keypoint *points,
int nbits)
465 const struct keypoint *point = points;
466 const int lut_size = 1<<nbits;
468 double *
xi, *fi, *di, *hi, *
mi;
469 const int scale = lut_size - 1;
476 for (
int i = 0;
i < lut_size;
i++)
y[
i] =
i;
483 for (
int i = 0;
i < lut_size;
i++)
y[
i] = yval;
499 for (
int i = 0;
i < n;
i++) {
506 for (
int i = 0;
i < n - 1;
i++) {
514 const double m =
mi[0],
b = fi[0] -
xi[0]*m;
515 for (
int i = 0;
i < lut_size;
i++)
y[
i] =
CLIP(
i*m +
b);
528 const double xi0 =
xi[0];
529 const double yi0 = fi[0];
530 const uint16_t yval =
CLIP(yi0);
531 for (;
x < xi0;
x++) {
539 for (
int i = 0, x0 =
x;
i < n-1;
i++, x0 =
x) {
540 const double xi0 =
xi[
i];
541 const double xi1 =
xi[
i + 1];
542 const double h = hi[
i];
543 const double f0 = fi[
i];
544 const double f1 = fi[
i + 1];
545 const double d0 = di[
i];
546 const double d1 = di[
i + 1];
549 for (;
x < xi1;
x++) {
550 const double xx = (
x - xi0) /
h;
559 i, x0,
x-1,
y[x0],
y[
x-1]);
564 if (
x &&
x < lut_size) {
566 const double xi1 =
xi[n - 1];
567 const double yi1 = fi[n - 1];
568 const uint16_t yval =
CLIP(yi1);
570 n-1,
x, lut_size - 1, yval);
571 for (;
x &&
x < lut_size;
x++) {
590 static const int comp_ids[] = {3, 0, 1, 2};
598 #define READ16(dst) do { \
600 ret = AVERROR_INVALIDDATA; \
603 dst = AV_RB16(buf); \
614 for (n = 0; n < nb_points; n++) {
621 char **
pts = &
curves->comp_points_str[comp_ids[
i]];
625 i, comp_ids[
i], nb_points, *
pts);
645 const double scale = 1. / (lut_size - 1);
646 static const char *
const colors[] = {
"red",
"green",
"blue",
"#404040", };
666 av_bprintf(&buf,
"%s'-' using 1:2 with lines lc '%s' title ''",
667 i ?
", " :
"plot ", colors[
i]);
669 av_bprintf(&buf,
", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
678 for (
x = 0;
x < lut_size;
x++)
683 if (comp_points[
i]) {
684 const struct keypoint *point = comp_points[
i];
694 fwrite(buf.str, 1, buf.len,
f);
705 const char *allp =
curves->comp_points_str_all;
723 curves->parsed_psfile = 1;
727 #define SET_COMP_IF_NOT_SET(n, name) do { \
728 if (!pts[n] && curves_presets[curves->preset].name) { \
729 pts[n] = av_strdup(curves_presets[curves->preset].name); \
731 return AVERROR(ENOMEM); \
751 const int direct =
out == in;
753 const uint8_t
r =
curves->rgba_map[
R];
754 const uint8_t
g =
curves->rgba_map[
G];
755 const uint8_t
b =
curves->rgba_map[
B];
756 const uint8_t
a =
curves->rgba_map[
A];
762 uint16_t *dstp = ( uint16_t *)(
out->data[0] +
y *
out->linesize[0]);
763 const uint16_t *srcp = (
const uint16_t *)(in ->
data[0] +
y * in->
linesize[0]);
769 if (!direct &&
step == 4)
770 dstp[
x +
a] = srcp[
x +
a];
782 if (!direct &&
step == 4)
785 dst +=
out->linesize[0];
799 const int direct =
out == in;
801 const uint8_t
r =
curves->rgba_map[
R];
802 const uint8_t
g =
curves->rgba_map[
G];
803 const uint8_t
b =
curves->rgba_map[
B];
804 const uint8_t
a =
curves->rgba_map[
A];
810 uint16_t *dstrp = ( uint16_t *)(
out->data[
r] +
y *
out->linesize[
r]);
811 uint16_t *dstgp = ( uint16_t *)(
out->data[
g] +
y *
out->linesize[
g]);
812 uint16_t *dstbp = ( uint16_t *)(
out->data[
b] +
y *
out->linesize[
b]);
813 uint16_t *dstap = ( uint16_t *)(
out->data[
a] +
y *
out->linesize[
a]);
814 const uint16_t *srcrp = (
const uint16_t *)(in ->
data[
r] +
y * in->
linesize[
r]);
815 const uint16_t *srcgp = (
const uint16_t *)(in ->
data[
g] +
y * in->
linesize[
g]);
816 const uint16_t *srcbp = (
const uint16_t *)(in ->
data[
b] +
y * in->
linesize[
b]);
817 const uint16_t *srcap = (
const uint16_t *)(in ->
data[
a] +
y * in->
linesize[
a]);
823 if (!direct &&
step == 4)
842 if (!direct &&
step == 4)
845 dstr +=
out->linesize[
r];
846 dstg +=
out->linesize[
g];
847 dstb +=
out->linesize[
b];
848 dsta +=
out->linesize[
a];
892 for (j = 0; j <
curves->lut_size; j++)
898 const struct keypoint *point = comp_points[
i];
955 char *res,
int res_len,
int flags)
960 if (!strcmp(cmd,
"plot")) {
962 }
else if (!strcmp(cmd,
"all") || !strcmp(cmd,
"preset") || !strcmp(cmd,
"psfile") || !strcmp(cmd,
"interp")) {
963 if (!strcmp(cmd,
"psfile"))
964 curves->parsed_psfile = 0;
970 }
else if (!strcmp(cmd,
"red") || !strcmp(cmd,
"r")) {
972 }
else if (!strcmp(cmd,
"green") || !strcmp(cmd,
"g")) {
974 }
else if (!strcmp(cmd,
"blue") || !strcmp(cmd,
"b")) {
976 }
else if (!strcmp(cmd,
"master") || !strcmp(cmd,
"m")) {
1029 .priv_class = &curves_class,
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
#define AV_PIX_FMT_GBRAP16
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_BPRINT_SIZE_UNLIMITED
static const float h0[64]
uint16_t * graph[NB_COMP+1]
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static int dump_curves(const char *fname, uint16_t *graph[NB_COMP+1], struct keypoint *comp_points[NB_COMP+1], int lut_size)
static double interp_cubic_hermite_half(const double x, const double f, const double d)
Evalaute half of the cubic hermite interpolation expression, wrt one interval endpoint.
static const Curve curves[]
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
static const AVFilterPad curves_inputs[]
#define AV_LOG_VERBOSE
Detailed information.
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
const char * name
Filter name.
static int get_nb_points(const struct keypoint *d)
A link between two filters.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int(* filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define AV_PIX_FMT_GBRP14
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...
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
#define AV_PIX_FMT_GBRP10
#define AV_BPRINT_SIZE_AUTOMATIC
static double val(void *priv, double ch)
A filter pad used for either input or output.
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
static const uint16_t mask[17]
#define AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP12
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define CLIP(v)
Natural cubic spline interpolation Finding curves using Cubic Splines notes by Steven Rauch and John ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
AVFILTER_DEFINE_CLASS(curves)
#define xi(width, name, var, range_min, range_max, subs,...)
#define FILTER_INPUTS(array)
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
int av_log_get_level(void)
Get the current log level.
#define AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_RGBA64
Describe the class of an AVClass context structure.
static int interpolate(void *log_ctx, uint16_t *y, const struct keypoint *points, int nbits)
static __device__ float fabs(float a)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
int(* init)(AVBSFContext *ctx)
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
static void scale(int *out, const int *in, const int w, const int h, const int shift)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
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 FILTER_PIXFMTS(...)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
const AVFilter ff_vf_curves
static av_cold void curves_uninit(AVFilterContext *ctx)
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
static int filter_slice_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
static const AVOption curves_options[]
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
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
static int filter_slice_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
char * comp_points_str[NB_COMP+1]
@ PRESET_INCREASE_CONTRAST
static void uninit(AVBSFContext *ctx)
#define AV_PIX_FMT_BGRA64
#define i(width, name, range_min, range_max)
int w
agreed upon image width
#define AV_PIX_FMT_GBRP12
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Used for passing data between threads.
static const struct @267 curves_presets[]
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const char * name
Pad name.
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
void * av_calloc(size_t nmemb, size_t size)
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
char * comp_points_str_all
void av_bprintf(AVBPrint *buf, const char *fmt,...)
int h
agreed upon image height
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
static int pchip_find_derivatives(const int n, const double *hk, const double *mk, double *dk)
Evalaute the piecewise polynomial derivatives at endpoints.
static struct keypoint * make_point(double x, double y, struct keypoint *next)
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
static int config_input(AVFilterLink *inlink)
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
char * av_strdup(const char *s)
Duplicate a string.
#define SET_COMP_IF_NOT_SET(n, name)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static av_cold int curves_init(AVFilterContext *ctx)
#define FILTER_OUTPUTS(array)
static int interpolate_pchip(void *log_ctx, uint16_t *y, const struct keypoint *points, int nbits)
Prepare the lookup table by piecewise monotonic cubic interpolation (PCHIP)
static int parse_psfile(AVFilterContext *ctx, const char *fname)
static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s, int lut_size)
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
static double pchip_edge_case(double h0, double h1, double m0, double m1)
Evalaute the derivative of an edge endpoint.
#define flags(name, subs,...)
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)