70 #if CONFIG_HALDCLUT_FILTER
79 #define OFFSET(x) offsetof(LUT3DContext, x)
80 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
81 #define COMMON_OPTIONS \
82 { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, FLAGS, "interp_mode" }, \
83 { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
84 { "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" }, \
85 { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
88 static inline float lerpf(
float v0,
float v1,
float f)
90 return v0 + (v1 -
v0) * f;
101 #define NEAR(x) ((int)((x) + .5))
102 #define PREV(x) ((int)(x))
103 #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
123 const struct rgbvec d = {
s->r - prev[0],
s->g - prev[1],
s->b - prev[2]};
124 const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
125 const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
126 const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
127 const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
128 const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
129 const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
130 const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
131 const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
151 const struct rgbvec d = {
s->r - prev[0],
s->g - prev[1],
s->b - prev[2]};
152 const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
153 const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
157 const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
158 const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
159 c.
r = (1-d.
r) * c000.
r + (d.
r-d.
g) * c100.
r + (d.
g-d.
b) * c110.
r + (d.
b) * c111.
r;
160 c.
g = (1-d.
r) * c000.
g + (d.
r-d.
g) * c100.
g + (d.
g-d.
b) * c110.
g + (d.
b) * c111.
g;
161 c.
b = (1-d.
r) * c000.
b + (d.
r-d.
g) * c100.
b + (d.
g-d.
b) * c110.
b + (d.
b) * c111.
b;
162 }
else if (d.
r > d.
b) {
163 const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
164 const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
165 c.
r = (1-d.
r) * c000.
r + (d.
r-d.
b) * c100.
r + (d.
b-d.
g) * c101.
r + (d.
g) * c111.
r;
166 c.
g = (1-d.
r) * c000.
g + (d.
r-d.
b) * c100.
g + (d.
b-d.
g) * c101.
g + (d.
g) * c111.
g;
167 c.
b = (1-d.
r) * c000.
b + (d.
r-d.
b) * c100.
b + (d.
b-d.
g) * c101.
b + (d.
g) * c111.
b;
169 const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
170 const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
171 c.
r = (1-d.
b) * c000.
r + (d.
b-d.
r) * c001.
r + (d.
r-d.
g) * c101.
r + (d.
g) * c111.
r;
172 c.
g = (1-d.
b) * c000.
g + (d.
b-d.
r) * c001.
g + (d.
r-d.
g) * c101.
g + (d.
g) * c111.
g;
173 c.
b = (1-d.
b) * c000.
b + (d.
b-d.
r) * c001.
b + (d.
r-d.
g) * c101.
b + (d.
g) * c111.
b;
177 const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
178 const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
179 c.
r = (1-d.
b) * c000.
r + (d.
b-d.
g) * c001.
r + (d.
g-d.
r) * c011.
r + (d.
r) * c111.
r;
180 c.
g = (1-d.
b) * c000.
g + (d.
b-d.
g) * c001.
g + (d.
g-d.
r) * c011.
g + (d.
r) * c111.
g;
181 c.
b = (1-d.
b) * c000.
b + (d.
b-d.
g) * c001.
b + (d.
g-d.
r) * c011.
b + (d.
r) * c111.
b;
182 }
else if (d.
b > d.
r) {
183 const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
184 const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
185 c.
r = (1-d.
g) * c000.
r + (d.
g-d.
b) * c010.
r + (d.
b-d.
r) * c011.
r + (d.
r) * c111.
r;
186 c.
g = (1-d.
g) * c000.
g + (d.
g-d.
b) * c010.
g + (d.
b-d.
r) * c011.
g + (d.
r) * c111.
g;
187 c.
b = (1-d.
g) * c000.
b + (d.
g-d.
b) * c010.
b + (d.
b-d.
r) * c011.
b + (d.
r) * c111.
b;
189 const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
190 const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
191 c.
r = (1-d.
g) * c000.
r + (d.
g-d.
r) * c010.
r + (d.
r-d.
b) * c110.
r + (d.
b) * c111.
r;
192 c.
g = (1-d.
g) * c000.
g + (d.
g-d.
r) * c010.
g + (d.
r-d.
b) * c110.
g + (d.
b) * c111.
g;
193 c.
b = (1-d.
g) * c000.
b + (d.
g-d.
r) * c010.
b + (d.
r-d.
b) * c110.
b + (d.
b) * c111.
b;
199 #define DEFINE_INTERP_FUNC(name, nbits) \
200 static struct rgbvec interp_##nbits##_##name(const LUT3DContext *lut3d, \
205 const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
206 const struct rgbvec scaled_rgb = {r * scale, g * scale, b * scale}; \
207 return interp_##name(lut3d, &scaled_rgb); \
218 #define MAX_LINE_SIZE 512
224 return !*p || *p ==
'#';
227 #define NEXT_LINE(loop_cond) do { \
228 if (!fgets(line, sizeof(line), f)) { \
229 av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
230 return AVERROR_INVALIDDATA; \
242 for (k = 0; k <
size; k++) {
243 for (j = 0; j <
size; j++) {
244 for (i = 0; i <
size; i++) {
246 struct rgbvec *vec = &lut3d->
lut[k][j][i];
248 sscanf(line,
"%f %f %f", &vec->
r, &vec->
g, &vec->
b);
260 float min[3] = {0.0, 0.0, 0.0};
261 float max[3] = {1.0, 1.0, 1.0};
263 while (fgets(line,
sizeof(line), f)) {
264 if (!strncmp(line,
"LUT_3D_SIZE ", 12)) {
266 const int size = strtol(line + 12, NULL, 0);
273 for (k = 0; k <
size; k++) {
274 for (j = 0; j <
size; j++) {
275 for (i = 0; i <
size; i++) {
276 struct rgbvec *vec = &lut3d->
lut[k][j][i];
280 if (!strncmp(line,
"DOMAIN_", 7)) {
282 if (!strncmp(line + 7,
"MIN ", 4)) vals =
min;
283 else if (!strncmp(line + 7,
"MAX ", 4)) vals = max;
286 sscanf(line + 11,
"%f %f %f", vals, vals + 1, vals + 2);
288 min[0], min[1], min[2], max[0], max[1], max[2]);
292 if (sscanf(line,
"%f %f %f", &vec->
r, &vec->
g, &vec->
b) != 3)
294 vec->
r *= max[0] - min[0];
295 vec->
g *= max[1] - min[1];
296 vec->
b *= max[2] - min[2];
314 const float scale = 16*16*16;
317 if (!fgets(line,
sizeof(line), f))
319 for (k = 0; k <
size; k++) {
320 for (j = 0; j <
size; j++) {
321 for (i = 0; i <
size; i++) {
323 struct rgbvec *vec = &lut3d->
lut[k][j][i];
326 if (sscanf(line,
"%d %d %d", &r, &g, &b) != 3)
344 uint8_t rgb_map[3] = {0, 1, 2};
346 while (fgets(line,
sizeof(line), f)) {
347 if (!strncmp(line,
"in", 2)) in = strtol(line + 2, NULL, 0);
348 else if (!strncmp(line,
"out", 3))
out = strtol(line + 3, NULL, 0);
349 else if (!strncmp(line,
"values", 6)) {
350 const char *p = line + 6;
351 #define SET_COLOR(id) do { \
352 while (av_isspace(*p)) \
355 case 'r': rgb_map[id] = 0; break; \
356 case 'g': rgb_map[id] = 1; break; \
357 case 'b': rgb_map[id] = 2; break; \
359 while (*p && !av_isspace(*p)) \
369 if (in == -1 ||
out == -1) {
373 if (in < 2 ||
out < 2 ||
379 for (size = 1; size*size*size <
in; size++);
381 scale = 1. / (
out - 1);
383 for (k = 0; k <
size; k++) {
384 for (j = 0; j <
size; j++) {
385 for (i = 0; i <
size; i++) {
386 struct rgbvec *vec = &lut3d->
lut[k][j][i];
390 if (sscanf(line,
"%f %f %f", val, val + 1, val + 2) != 3)
392 vec->
r = val[rgb_map[0]] *
scale;
393 vec->
g = val[rgb_map[1]] *
scale;
394 vec->
b = val[rgb_map[2]] *
scale;
404 const float c = 1. / (size - 1);
407 for (k = 0; k <
size; k++) {
408 for (j = 0; j <
size; j++) {
409 for (i = 0; i <
size; i++) {
410 struct rgbvec *vec = &lut3d->
lut[k][j][i];
451 #define SET_FUNC(name) do { \
452 if (lut3d->is16bit) lut3d->interp_16 = interp_16_##name; \
453 else lut3d->interp_8 = interp_8_##name; \
467 #define FILTER(nbits) do { \
468 uint8_t *dstrow = out->data[0]; \
469 const uint8_t *srcrow = in ->data[0]; \
471 for (y = 0; y < inlink->h; y++) { \
472 uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
473 const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
474 for (x = 0; x < inlink->w * step; x += step) { \
475 struct rgbvec vec = lut3d->interp_##nbits(lut3d, src[x + r], src[x + g], src[x + b]); \
476 dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
477 dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
478 dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
479 if (!direct && step == 4) \
480 dst[x + a] = src[x + a]; \
482 dstrow += out->linesize[0]; \
483 srcrow += in ->linesize[0]; \
489 int x,
y, direct = 0;
494 const int step = lut3d->
step;
530 #if CONFIG_LUT3D_FILTER
531 static const AVOption lut3d_options[] = {
550 f = fopen(lut3d->
file,
"r");
557 ext = strrchr(lut3d->
file,
'.');
615 .priv_class = &lut3d_class,
620 #if CONFIG_HALDCLUT_FILTER
625 const int linesize = frame->
linesize[0];
626 const int w = lut3d->clut_width;
627 const int step = lut3d->clut_step;
628 const uint8_t *rgba_map = lut3d->clut_rgba_map;
631 #define LOAD_CLUT(nbits) do { \
632 int i, j, k, x = 0, y = 0; \
634 for (k = 0; k < level; k++) { \
635 for (j = 0; j < level; j++) { \
636 for (i = 0; i < level; i++) { \
637 const uint##nbits##_t *src = (const uint##nbits##_t *) \
638 (data + y*linesize + x*step); \
639 struct rgbvec *vec = &lut3d->lut[k][j][i]; \
640 vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
641 vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
642 vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
652 if (!lut3d->clut_is16bit) LOAD_CLUT(8);
692 lut3d->clut_is16bit = 0;
698 lut3d->clut_is16bit = 1;
704 if (inlink->
w > inlink->
h)
706 "Hald CLUT will be ignored\n", inlink->
w - inlink->
h);
707 else if (inlink->
w < inlink->
h)
709 "Hald CLUT will be ignored\n", inlink->
h - inlink->
w);
710 lut3d->clut_width = w = h =
FFMIN(inlink->
w, inlink->
h);
712 for (level = 1; level*level*level < w; level++);
713 size = level*level*
level;
721 const int max_clut_level = sqrt(
MAX_LEVEL);
722 const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
724 "(maximum level is %d, or %dx%d CLUT)\n",
725 max_clut_level, max_clut_size, max_clut_size);
737 update_clut(ctx->
priv, second);
744 lut3d->dinput.process = update_apply_clut;
754 static const AVOption haldclut_options[] = {
755 {
"shortest",
"force termination when the shortest input terminates",
OFFSET(dinput.shortest),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
FLAGS },
756 {
"repeatlast",
"continue applying the last clut after eos",
OFFSET(dinput.repeatlast),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
FLAGS },
771 .filter_frame = filter_frame_clut,
772 .config_props = config_clut,
791 .
init = haldclut_init,
792 .
uninit = haldclut_uninit,
794 .
inputs = haldclut_inputs,
796 .priv_class = &haldclut_class,