21 #include "libavutil/opt.h"
22 #include "libavutil/bprint.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/file.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/pixdesc.h"
72 #define OFFSET(x) offsetof(CurvesContext, x)
73 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
102 static const struct {
109 "0/1 0.129/1 0.466/0.498 0.725/0 1/0",
110 "0/1 0.109/1 0.301/0.498 0.517/0 1/0",
111 "0/1 0.098/1 0.235/0.498 0.423/0 1/0",
114 "0.25/0.156 0.501/0.501 0.686/0.745",
115 "0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
116 "0.231/0.094 0.709/0.874",
126 "0/0.11 0.42/0.51 1/0.95",
128 "0/0.22 0.49/0.44 1/0.8",
154 point->
x =
av_strtod(p, &p);
if (p && *p) p++;
155 point->
y =
av_strtod(p, &p);
if (p && *p) p++;
156 if (point->
x < 0 || point->
x > 1 || point->
y < 0 || point->
y > 1) {
158 "x and y must be in the [0;1] range.\n", point->
x, point->
y);
164 if ((
int)(last->
x * 255) >= (
int)(point->
x * 255)) {
166 "and (%f;%f) are too close from each other or not "
167 "strictly increasing on the x-axis\n",
168 last->
x, last->
y, point->
x, point->
y);
181 last->
x = last->
y = 0;
183 }
else if ((*points)->x != 0.) {
226 double (*matrix)[3] =
av_calloc(n,
sizeof(*matrix));
227 double *h =
av_malloc((n - 1) *
sizeof(*h));
230 if (!matrix || !h || !r) {
237 for (point = points; point; point = point->
next) {
239 h[i] = point->
x - xprev;
246 for (i = 1; i < n - 1; i++) {
247 double yp = point->
y,
250 r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
259 matrix[0][
MD] = matrix[n - 1][
MD] = 1;
260 for (i = 1; i < n - 1; i++) {
261 matrix[i][
BD] = h[i-1];
262 matrix[i][
MD] = 2 * (h[i-1] + h[i]);
263 matrix[i][
AD] = h[i];
267 for (i = 1; i <
n; i++) {
268 double den = matrix[i][
MD] - matrix[i][
BD] * matrix[i-1][
AD];
269 double k = den ? 1./den : 1.;
271 r[i] = (r[i] - matrix[i][
BD] * r[i - 1]) * k;
273 for (i = n - 2; i >= 0; i--)
274 r[i] = r[i] - matrix[i][
AD] * r[i + 1];
280 while (point->
next) {
281 double yc = point->
y;
282 double yn = point->
next->
y;
285 double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
286 double c = r[i] / 2.;
287 double d = (r[i+1] - r[i]) / (6.*h[i]);
290 int x_start = point->
x * 255;
291 int x_end = point->
next->
x * 255;
294 x_end >= 0 && x_end <= 255);
296 for (x = x_start; x <= x_end; x++) {
297 double xx = (x - x_start) * 1/255.;
298 double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
299 y[
x] = av_clipf(yy, 0, 1) * 255;
321 static const int comp_ids[] = {3, 0, 1, 2};
329 #define READ16(dst) do { \
331 return AVERROR_INVALIDDATA; \
332 dst = AV_RB16(buf); \
343 for (n = 0; n < nb_points; n++) {
347 av_bprintf(&ptstr,
"%f/%f ", x / 255., y / 255.);
354 i, comp_ids[i], nb_points, *pts);
380 for (i = 0; i <
NB_COMP; i++) {
395 #define SET_COMP_IF_NOT_SET(n, name) do { \
396 if (!pts[n] && curves_presets[curves->preset].name) { \
397 pts[n] = av_strdup(curves_presets[curves->preset].name); \
399 return AVERROR(ENOMEM); \
408 for (i = 0; i <
NB_COMP + 1; i++) {
419 for (j = 0; j < 256; j++)
424 for (i = 0; i <
NB_COMP; i++) {
425 struct keypoint *point = comp_points[i];
433 for (j = 0; j < 256; j++)
439 for (i = 0; i < NB_COMP + 1; i++) {
440 struct keypoint *point = comp_points[i];
478 int x,
y, direct = 0;
485 const int step = curves->
step;
506 for (y = 0; y < inlink->
h; y++) {
507 for (x = 0; x < inlink->
w * step; x += step) {
508 dst[x +
r] = curves->
graph[
R][src[x +
r]];
509 dst[x +
g] = curves->
graph[
G][src[x +
g]];
510 dst[x +
b] = curves->
graph[
B][src[x +
b]];
511 if (!direct && step == 4)
512 dst[x +
a] = src[x +
a];
550 .priv_class = &curves_class,
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
This structure describes decoded (raw) audio or video data.
const char * name
Filter name.
void * priv
private data for use by the filter
static const AVFilterPad outputs[]
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
int h
agreed upon image height
packed RGB 8:8:8, 24bpp, RGBRGB...
static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *points)
Natural cubic spline interpolation Finding curves using Cubic Splines notes by Steven Rauch and John ...
static struct keypoint * make_point(double x, double y, struct keypoint *next)
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
packed RGB 8:8:8, 32bpp, RGB0RGB0...
packed BGR 8:8:8, 32bpp, 0BGR0BGR...
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
packed RGB 8:8:8, 24bpp, BGRBGR...
const char * name
Pad name.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static const AVOption curves_options[]
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
static av_cold int end(AVCodecContext *avctx)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
#define AV_LOG_VERBOSE
Detailed information.
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
A filter pad used for either input or output.
A link between two filters.
#define SET_COMP_IF_NOT_SET(n, name)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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_dlog(ac->avr,"%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
AVFilter avfilter_vf_curves
packed BGR 8:8:8, 32bpp, BGR0BGR0...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
int av_log_get_level(void)
Get the current log level.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVPixelFormat
Pixel format.
#define av_unused
Disable warnings about deprecated features This is useful for sections of code kept for backward comp...
static int config_input(AVFilterLink *inlink)
int w
agreed upon image width
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Init a print buffer.
#define FF_ARRAY_ELEMS(a)
Buffer to print data progressively.
char * comp_points_str[NB_COMP+1]
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define AV_BPRINT_SIZE_AUTOMATIC
int format
agreed upon media format
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Main libavfilter public API header.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
AVFilterLink ** outputs
array of pointers to output links
static int parse_psfile(AVFilterContext *ctx, const char *fname)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s)
static av_cold int init(AVFilterContext *ctx)
static int get_nb_points(const struct keypoint *d)
Describe the class of an AVClass context structure.
static const AVFilterPad inputs[]
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static const AVFilterPad curves_inputs[]
AVFilterContext * dst
dest filter
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
static struct @124 curves_presets[]
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
uint8_t graph[NB_COMP+1][256]
void av_bprintf(AVBPrint *buf, const char *fmt,...) av_printf_format(2
Append a formatted string to a print buffer.
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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_dlog(ac->avr,"%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-> out
#define AVFILTER_DEFINE_CLASS(fname)
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
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 ...
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...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static int query_formats(AVFilterContext *ctx)
packed RGB 8:8:8, 32bpp, 0RGB0RGB...
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static const AVFilterPad curves_outputs[]