68 #if CONFIG_HALDCLUT_FILTER
81 #define OFFSET(x) offsetof(LUT3DContext, x)
82 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
83 #define COMMON_OPTIONS \
84 { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, FLAGS, "interp_mode" }, \
85 { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
86 { "trilinear", "interpolate values using the 8 points defining a cube", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TRILINEAR}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
87 { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
90 static inline float lerpf(
float v0,
float v1,
float f)
92 return v0 + (v1 -
v0) * f;
103 #define NEAR(x) ((int)((x) + .5))
104 #define PREV(x) ((int)(x))
105 #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
125 const struct rgbvec d = {
s->r - prev[0],
s->g - prev[1],
s->b - prev[2]};
126 const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
127 const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
128 const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
129 const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
130 const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
131 const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
132 const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
133 const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
153 const struct rgbvec d = {
s->r - prev[0],
s->g - prev[1],
s->b - prev[2]};
154 const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
155 const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
159 const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
160 const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
161 c.
r = (1-d.
r) * c000.
r + (d.
r-d.
g) * c100.
r + (d.
g-d.
b) * c110.
r + (d.
b) * c111.
r;
162 c.
g = (1-d.
r) * c000.
g + (d.
r-d.
g) * c100.
g + (d.
g-d.
b) * c110.
g + (d.
b) * c111.
g;
163 c.
b = (1-d.
r) * c000.
b + (d.
r-d.
g) * c100.
b + (d.
g-d.
b) * c110.
b + (d.
b) * c111.
b;
164 }
else if (d.
r > d.
b) {
165 const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
166 const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
167 c.
r = (1-d.
r) * c000.
r + (d.
r-d.
b) * c100.
r + (d.
b-d.
g) * c101.
r + (d.
g) * c111.
r;
168 c.
g = (1-d.
r) * c000.
g + (d.
r-d.
b) * c100.
g + (d.
b-d.
g) * c101.
g + (d.
g) * c111.
g;
169 c.
b = (1-d.
r) * c000.
b + (d.
r-d.
b) * c100.
b + (d.
b-d.
g) * c101.
b + (d.
g) * c111.
b;
171 const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
172 const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
173 c.
r = (1-d.
b) * c000.
r + (d.
b-d.
r) * c001.
r + (d.
r-d.
g) * c101.
r + (d.
g) * c111.
r;
174 c.
g = (1-d.
b) * c000.
g + (d.
b-d.
r) * c001.
g + (d.
r-d.
g) * c101.
g + (d.
g) * c111.
g;
175 c.
b = (1-d.
b) * c000.
b + (d.
b-d.
r) * c001.
b + (d.
r-d.
g) * c101.
b + (d.
g) * c111.
b;
179 const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
180 const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
181 c.
r = (1-d.
b) * c000.
r + (d.
b-d.
g) * c001.
r + (d.
g-d.
r) * c011.
r + (d.
r) * c111.
r;
182 c.
g = (1-d.
b) * c000.
g + (d.
b-d.
g) * c001.
g + (d.
g-d.
r) * c011.
g + (d.
r) * c111.
g;
183 c.
b = (1-d.
b) * c000.
b + (d.
b-d.
g) * c001.
b + (d.
g-d.
r) * c011.
b + (d.
r) * c111.
b;
184 }
else if (d.
b > d.
r) {
185 const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
186 const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
187 c.
r = (1-d.
g) * c000.
r + (d.
g-d.
b) * c010.
r + (d.
b-d.
r) * c011.
r + (d.
r) * c111.
r;
188 c.
g = (1-d.
g) * c000.
g + (d.
g-d.
b) * c010.
g + (d.
b-d.
r) * c011.
g + (d.
r) * c111.
g;
189 c.
b = (1-d.
g) * c000.
b + (d.
g-d.
b) * c010.
b + (d.
b-d.
r) * c011.
b + (d.
r) * c111.
b;
191 const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
192 const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
193 c.
r = (1-d.
g) * c000.
r + (d.
g-d.
r) * c010.
r + (d.
r-d.
b) * c110.
r + (d.
b) * c111.
r;
194 c.
g = (1-d.
g) * c000.
g + (d.
g-d.
r) * c010.
g + (d.
r-d.
b) * c110.
g + (d.
b) * c111.
g;
195 c.
b = (1-d.
g) * c000.
b + (d.
g-d.
r) * c010.
b + (d.
r-d.
b) * c110.
b + (d.
b) * c111.
b;
201 #define DEFINE_INTERP_FUNC(name, nbits) \
202 static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
205 const LUT3DContext *lut3d = ctx->priv; \
206 const ThreadData *td = arg; \
207 const AVFrame *in = td->in; \
208 const AVFrame *out = td->out; \
209 const int direct = out == in; \
210 const int step = lut3d->step; \
211 const uint8_t r = lut3d->rgba_map[R]; \
212 const uint8_t g = lut3d->rgba_map[G]; \
213 const uint8_t b = lut3d->rgba_map[B]; \
214 const uint8_t a = lut3d->rgba_map[A]; \
215 const int slice_start = (in->height * jobnr ) / nb_jobs; \
216 const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
217 uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
218 const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
220 for (y = slice_start; y < slice_end; y++) { \
221 uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
222 const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
223 for (x = 0; x < in->width * step; x += step) { \
224 const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
225 const struct rgbvec scaled_rgb = {src[x + r] * scale, \
226 src[x + g] * scale, \
227 src[x + b] * scale}; \
228 struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
229 dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
230 dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
231 dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
232 if (!direct && step == 4) \
233 dst[x + a] = src[x + a]; \
235 dstrow += out->linesize[0]; \
236 srcrow += in ->linesize[0]; \
249 #define MAX_LINE_SIZE 512
255 return !*p || *p ==
'#';
258 #define NEXT_LINE(loop_cond) do { \
259 if (!fgets(line, sizeof(line), f)) { \
260 av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
261 return AVERROR_INVALIDDATA; \
273 for (k = 0; k <
size; k++) {
274 for (j = 0; j <
size; j++) {
275 for (i = 0; i <
size; i++) {
277 struct rgbvec *vec = &lut3d->
lut[k][j][i];
279 sscanf(line,
"%f %f %f", &vec->
r, &vec->
g, &vec->
b);
291 float min[3] = {0.0, 0.0, 0.0};
292 float max[3] = {1.0, 1.0, 1.0};
294 while (fgets(line,
sizeof(line), f)) {
295 if (!strncmp(line,
"LUT_3D_SIZE ", 12)) {
297 const int size = strtol(line + 12, NULL, 0);
304 for (k = 0; k <
size; k++) {
305 for (j = 0; j <
size; j++) {
306 for (i = 0; i <
size; i++) {
307 struct rgbvec *vec = &lut3d->
lut[i][j][k];
311 if (!strncmp(line,
"DOMAIN_", 7)) {
313 if (!strncmp(line + 7,
"MIN ", 4)) vals =
min;
314 else if (!strncmp(line + 7,
"MAX ", 4)) vals = max;
317 sscanf(line + 11,
"%f %f %f", vals, vals + 1, vals + 2);
319 min[0], min[1], min[2], max[0], max[1], max[2]);
323 if (sscanf(line,
"%f %f %f", &vec->
r, &vec->
g, &vec->
b) != 3)
325 vec->
r *= max[0] - min[0];
326 vec->
g *= max[1] - min[1];
327 vec->
b *= max[2] - min[2];
345 const float scale = 16*16*16;
349 for (k = 0; k <
size; k++) {
350 for (j = 0; j <
size; j++) {
351 for (i = 0; i <
size; i++) {
353 struct rgbvec *vec = &lut3d->
lut[k][j][i];
356 if (sscanf(line,
"%d %d %d", &r, &g, &b) != 3)
374 uint8_t rgb_map[3] = {0, 1, 2};
376 while (fgets(line,
sizeof(line), f)) {
377 if (!strncmp(line,
"in", 2)) in = strtol(line + 2, NULL, 0);
378 else if (!strncmp(line,
"out", 3))
out = strtol(line + 3, NULL, 0);
379 else if (!strncmp(line,
"values", 6)) {
380 const char *p = line + 6;
381 #define SET_COLOR(id) do { \
382 while (av_isspace(*p)) \
385 case 'r': rgb_map[id] = 0; break; \
386 case 'g': rgb_map[id] = 1; break; \
387 case 'b': rgb_map[id] = 2; break; \
389 while (*p && !av_isspace(*p)) \
399 if (in == -1 ||
out == -1) {
403 if (in < 2 ||
out < 2 ||
409 for (size = 1; size*size*size <
in; size++);
411 scale = 1. / (
out - 1);
413 for (k = 0; k <
size; k++) {
414 for (j = 0; j <
size; j++) {
415 for (i = 0; i <
size; i++) {
416 struct rgbvec *vec = &lut3d->
lut[k][j][i];
420 if (sscanf(line,
"%f %f %f", val, val + 1, val + 2) != 3)
422 vec->
r = val[rgb_map[0]] *
scale;
423 vec->
g = val[rgb_map[1]] *
scale;
424 vec->
b = val[rgb_map[2]] *
scale;
434 const float c = 1. / (size - 1);
437 for (k = 0; k <
size; k++) {
438 for (j = 0; j <
size; j++) {
439 for (i = 0; i <
size; i++) {
440 struct rgbvec *vec = &lut3d->
lut[k][j][i];
482 #define SET_FUNC(name) do { \
483 if (is16bit) lut3d->interp = interp_16_##name; \
484 else lut3d->interp = interp_8_##name; \
536 #if CONFIG_LUT3D_FILTER
537 static const AVOption lut3d_options[] = {
556 f = fopen(lut3d->
file,
"r");
563 ext = strrchr(lut3d->
file,
'.');
621 .priv_class = &lut3d_class,
626 #if CONFIG_HALDCLUT_FILTER
631 const int linesize = frame->
linesize[0];
632 const int w = lut3d->clut_width;
633 const int step = lut3d->clut_step;
634 const uint8_t *rgba_map = lut3d->clut_rgba_map;
637 #define LOAD_CLUT(nbits) do { \
638 int i, j, k, x = 0, y = 0; \
640 for (k = 0; k < level; k++) { \
641 for (j = 0; j < level; j++) { \
642 for (i = 0; i < level; i++) { \
643 const uint##nbits##_t *src = (const uint##nbits##_t *) \
644 (data + y*linesize + x*step); \
645 struct rgbvec *vec = &lut3d->lut[k][j][i]; \
646 vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
647 vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
648 vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
658 if (!lut3d->clut_is16bit) LOAD_CLUT(8);
696 lut3d->clut_is16bit = 0;
702 lut3d->clut_is16bit = 1;
708 if (inlink->
w > inlink->
h)
710 "Hald CLUT will be ignored\n", inlink->
w - inlink->
h);
711 else if (inlink->
w < inlink->
h)
713 "Hald CLUT will be ignored\n", inlink->
h - inlink->
w);
714 lut3d->clut_width = w = h =
FFMIN(inlink->
w, inlink->
h);
716 for (level = 1; level*level*level < w; level++);
717 size = level*level*
level;
725 const int max_clut_level = sqrt(
MAX_LEVEL);
726 const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
728 "(maximum level is %d, or %dx%d CLUT)\n",
729 max_clut_level, max_clut_size, max_clut_size);
741 update_clut(ctx->
priv, second);
748 lut3d->dinput.process = update_apply_clut;
758 static const AVOption haldclut_options[] = {
759 {
"shortest",
"force termination when the shortest input terminates",
OFFSET(dinput.shortest),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
FLAGS },
760 {
"repeatlast",
"continue applying the last clut after eos",
OFFSET(dinput.repeatlast),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
FLAGS },
770 .filter_frame = filter_frame_hald,
775 .filter_frame = filter_frame_hald,
776 .config_props = config_clut,
795 .
init = haldclut_init,
796 .
uninit = haldclut_uninit,
798 .
inputs = haldclut_inputs,
800 .priv_class = &haldclut_class,