You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1426 lines
58KB

  1. /*
  2. * Copyright (c) 2013 Clément Bœsch
  3. * Copyright (c) 2018 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * 3D Lookup table filter
  24. */
  25. #include "libavutil/opt.h"
  26. #include "libavutil/file.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/avstring.h"
  31. #include "avfilter.h"
  32. #include "drawutils.h"
  33. #include "formats.h"
  34. #include "framesync.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #define R 0
  38. #define G 1
  39. #define B 2
  40. #define A 3
  41. enum interp_mode {
  42. INTERPOLATE_NEAREST,
  43. INTERPOLATE_TRILINEAR,
  44. INTERPOLATE_TETRAHEDRAL,
  45. NB_INTERP_MODE
  46. };
  47. struct rgbvec {
  48. float r, g, b;
  49. };
  50. /* 3D LUT don't often go up to level 32, but it is common to have a Hald CLUT
  51. * of 512x512 (64x64x64) */
  52. #define MAX_LEVEL 64
  53. typedef struct LUT3DContext {
  54. const AVClass *class;
  55. int interpolation; ///<interp_mode
  56. char *file;
  57. uint8_t rgba_map[4];
  58. int step;
  59. avfilter_action_func *interp;
  60. struct rgbvec lut[MAX_LEVEL][MAX_LEVEL][MAX_LEVEL];
  61. int lutsize;
  62. #if CONFIG_HALDCLUT_FILTER
  63. uint8_t clut_rgba_map[4];
  64. int clut_step;
  65. int clut_bits;
  66. int clut_planar;
  67. int clut_width;
  68. FFFrameSync fs;
  69. #endif
  70. } LUT3DContext;
  71. typedef struct ThreadData {
  72. AVFrame *in, *out;
  73. } ThreadData;
  74. #define OFFSET(x) offsetof(LUT3DContext, x)
  75. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  76. #define COMMON_OPTIONS \
  77. { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, FLAGS, "interp_mode" }, \
  78. { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
  79. { "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" }, \
  80. { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
  81. { NULL }
  82. static inline float lerpf(float v0, float v1, float f)
  83. {
  84. return v0 + (v1 - v0) * f;
  85. }
  86. static inline struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
  87. {
  88. struct rgbvec v = {
  89. lerpf(v0->r, v1->r, f), lerpf(v0->g, v1->g, f), lerpf(v0->b, v1->b, f)
  90. };
  91. return v;
  92. }
  93. #define NEAR(x) ((int)((x) + .5))
  94. #define PREV(x) ((int)(x))
  95. #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
  96. /**
  97. * Get the nearest defined point
  98. */
  99. static inline struct rgbvec interp_nearest(const LUT3DContext *lut3d,
  100. const struct rgbvec *s)
  101. {
  102. return lut3d->lut[NEAR(s->r)][NEAR(s->g)][NEAR(s->b)];
  103. }
  104. /**
  105. * Interpolate using the 8 vertices of a cube
  106. * @see https://en.wikipedia.org/wiki/Trilinear_interpolation
  107. */
  108. static inline struct rgbvec interp_trilinear(const LUT3DContext *lut3d,
  109. const struct rgbvec *s)
  110. {
  111. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  112. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  113. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  114. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  115. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  116. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  117. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  118. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  119. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  120. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  121. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  122. const struct rgbvec c00 = lerp(&c000, &c100, d.r);
  123. const struct rgbvec c10 = lerp(&c010, &c110, d.r);
  124. const struct rgbvec c01 = lerp(&c001, &c101, d.r);
  125. const struct rgbvec c11 = lerp(&c011, &c111, d.r);
  126. const struct rgbvec c0 = lerp(&c00, &c10, d.g);
  127. const struct rgbvec c1 = lerp(&c01, &c11, d.g);
  128. const struct rgbvec c = lerp(&c0, &c1, d.b);
  129. return c;
  130. }
  131. /**
  132. * Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
  133. * @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
  134. */
  135. static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
  136. const struct rgbvec *s)
  137. {
  138. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  139. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  140. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  141. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  142. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  143. struct rgbvec c;
  144. if (d.r > d.g) {
  145. if (d.g > d.b) {
  146. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  147. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  148. c.r = (1-d.r) * c000.r + (d.r-d.g) * c100.r + (d.g-d.b) * c110.r + (d.b) * c111.r;
  149. c.g = (1-d.r) * c000.g + (d.r-d.g) * c100.g + (d.g-d.b) * c110.g + (d.b) * c111.g;
  150. c.b = (1-d.r) * c000.b + (d.r-d.g) * c100.b + (d.g-d.b) * c110.b + (d.b) * c111.b;
  151. } else if (d.r > d.b) {
  152. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  153. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  154. c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
  155. c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
  156. c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
  157. } else {
  158. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  159. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  160. c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
  161. c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
  162. c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
  163. }
  164. } else {
  165. if (d.b > d.g) {
  166. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  167. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  168. c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
  169. c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
  170. c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
  171. } else if (d.b > d.r) {
  172. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  173. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  174. c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
  175. c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
  176. c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
  177. } else {
  178. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  179. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  180. c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
  181. c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
  182. c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
  183. }
  184. }
  185. return c;
  186. }
  187. #define DEFINE_INTERP_FUNC_PLANAR(name, nbits, depth) \
  188. static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  189. { \
  190. int x, y; \
  191. const LUT3DContext *lut3d = ctx->priv; \
  192. const ThreadData *td = arg; \
  193. const AVFrame *in = td->in; \
  194. const AVFrame *out = td->out; \
  195. const int direct = out == in; \
  196. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  197. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  198. uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
  199. uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
  200. uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
  201. uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
  202. const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
  203. const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
  204. const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
  205. const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
  206. const float scale = (1. / ((1<<depth) - 1)) * (lut3d->lutsize - 1); \
  207. \
  208. for (y = slice_start; y < slice_end; y++) { \
  209. uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
  210. uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
  211. uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
  212. uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
  213. const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
  214. const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
  215. const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
  216. const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
  217. for (x = 0; x < in->width; x++) { \
  218. const struct rgbvec scaled_rgb = {srcr[x] * scale, \
  219. srcg[x] * scale, \
  220. srcb[x] * scale}; \
  221. struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
  222. dstr[x] = av_clip_uintp2(vec.r * (float)((1<<depth) - 1), depth); \
  223. dstg[x] = av_clip_uintp2(vec.g * (float)((1<<depth) - 1), depth); \
  224. dstb[x] = av_clip_uintp2(vec.b * (float)((1<<depth) - 1), depth); \
  225. if (!direct && in->linesize[3]) \
  226. dsta[x] = srca[x]; \
  227. } \
  228. grow += out->linesize[0]; \
  229. brow += out->linesize[1]; \
  230. rrow += out->linesize[2]; \
  231. arow += out->linesize[3]; \
  232. srcgrow += in->linesize[0]; \
  233. srcbrow += in->linesize[1]; \
  234. srcrrow += in->linesize[2]; \
  235. srcarow += in->linesize[3]; \
  236. } \
  237. return 0; \
  238. }
  239. DEFINE_INTERP_FUNC_PLANAR(nearest, 8, 8)
  240. DEFINE_INTERP_FUNC_PLANAR(trilinear, 8, 8)
  241. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 8, 8)
  242. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 9)
  243. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 9)
  244. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 9)
  245. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 10)
  246. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 10)
  247. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 10)
  248. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 12)
  249. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 12)
  250. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 12)
  251. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 14)
  252. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 14)
  253. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 14)
  254. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 16)
  255. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 16)
  256. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 16)
  257. #define DEFINE_INTERP_FUNC(name, nbits) \
  258. static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  259. { \
  260. int x, y; \
  261. const LUT3DContext *lut3d = ctx->priv; \
  262. const ThreadData *td = arg; \
  263. const AVFrame *in = td->in; \
  264. const AVFrame *out = td->out; \
  265. const int direct = out == in; \
  266. const int step = lut3d->step; \
  267. const uint8_t r = lut3d->rgba_map[R]; \
  268. const uint8_t g = lut3d->rgba_map[G]; \
  269. const uint8_t b = lut3d->rgba_map[B]; \
  270. const uint8_t a = lut3d->rgba_map[A]; \
  271. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  272. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  273. uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
  274. const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
  275. const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
  276. \
  277. for (y = slice_start; y < slice_end; y++) { \
  278. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  279. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  280. for (x = 0; x < in->width * step; x += step) { \
  281. const struct rgbvec scaled_rgb = {src[x + r] * scale, \
  282. src[x + g] * scale, \
  283. src[x + b] * scale}; \
  284. struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
  285. dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
  286. dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
  287. dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
  288. if (!direct && step == 4) \
  289. dst[x + a] = src[x + a]; \
  290. } \
  291. dstrow += out->linesize[0]; \
  292. srcrow += in ->linesize[0]; \
  293. } \
  294. return 0; \
  295. }
  296. DEFINE_INTERP_FUNC(nearest, 8)
  297. DEFINE_INTERP_FUNC(trilinear, 8)
  298. DEFINE_INTERP_FUNC(tetrahedral, 8)
  299. DEFINE_INTERP_FUNC(nearest, 16)
  300. DEFINE_INTERP_FUNC(trilinear, 16)
  301. DEFINE_INTERP_FUNC(tetrahedral, 16)
  302. #define MAX_LINE_SIZE 512
  303. static int skip_line(const char *p)
  304. {
  305. while (*p && av_isspace(*p))
  306. p++;
  307. return !*p || *p == '#';
  308. }
  309. #define NEXT_LINE(loop_cond) do { \
  310. if (!fgets(line, sizeof(line), f)) { \
  311. av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
  312. return AVERROR_INVALIDDATA; \
  313. } \
  314. } while (loop_cond)
  315. /* Basically r g and b float values on each line, with a facultative 3DLUTSIZE
  316. * directive; seems to be generated by Davinci */
  317. static int parse_dat(AVFilterContext *ctx, FILE *f)
  318. {
  319. LUT3DContext *lut3d = ctx->priv;
  320. char line[MAX_LINE_SIZE];
  321. int i, j, k, size;
  322. lut3d->lutsize = size = 33;
  323. NEXT_LINE(skip_line(line));
  324. if (!strncmp(line, "3DLUTSIZE ", 10)) {
  325. size = strtol(line + 10, NULL, 0);
  326. if (size < 2 || size > MAX_LEVEL) {
  327. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  328. return AVERROR(EINVAL);
  329. }
  330. lut3d->lutsize = size;
  331. NEXT_LINE(skip_line(line));
  332. }
  333. for (k = 0; k < size; k++) {
  334. for (j = 0; j < size; j++) {
  335. for (i = 0; i < size; i++) {
  336. struct rgbvec *vec = &lut3d->lut[k][j][i];
  337. if (k != 0 || j != 0 || i != 0)
  338. NEXT_LINE(skip_line(line));
  339. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  340. return AVERROR_INVALIDDATA;
  341. }
  342. }
  343. }
  344. return 0;
  345. }
  346. /* Iridas format */
  347. static int parse_cube(AVFilterContext *ctx, FILE *f)
  348. {
  349. LUT3DContext *lut3d = ctx->priv;
  350. char line[MAX_LINE_SIZE];
  351. float min[3] = {0.0, 0.0, 0.0};
  352. float max[3] = {1.0, 1.0, 1.0};
  353. while (fgets(line, sizeof(line), f)) {
  354. if (!strncmp(line, "LUT_3D_SIZE ", 12)) {
  355. int i, j, k;
  356. const int size = strtol(line + 12, NULL, 0);
  357. if (size < 2 || size > MAX_LEVEL) {
  358. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  359. return AVERROR(EINVAL);
  360. }
  361. lut3d->lutsize = size;
  362. for (k = 0; k < size; k++) {
  363. for (j = 0; j < size; j++) {
  364. for (i = 0; i < size; i++) {
  365. struct rgbvec *vec = &lut3d->lut[i][j][k];
  366. do {
  367. try_again:
  368. NEXT_LINE(0);
  369. if (!strncmp(line, "DOMAIN_", 7)) {
  370. float *vals = NULL;
  371. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  372. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  373. if (!vals)
  374. return AVERROR_INVALIDDATA;
  375. sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  376. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  377. min[0], min[1], min[2], max[0], max[1], max[2]);
  378. goto try_again;
  379. }
  380. } while (skip_line(line));
  381. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  382. return AVERROR_INVALIDDATA;
  383. vec->r *= max[0] - min[0];
  384. vec->g *= max[1] - min[1];
  385. vec->b *= max[2] - min[2];
  386. }
  387. }
  388. }
  389. break;
  390. }
  391. }
  392. return 0;
  393. }
  394. /* Assume 17x17x17 LUT with a 16-bit depth
  395. * FIXME: it seems there are various 3dl formats */
  396. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  397. {
  398. char line[MAX_LINE_SIZE];
  399. LUT3DContext *lut3d = ctx->priv;
  400. int i, j, k;
  401. const int size = 17;
  402. const float scale = 16*16*16;
  403. lut3d->lutsize = size;
  404. NEXT_LINE(skip_line(line));
  405. for (k = 0; k < size; k++) {
  406. for (j = 0; j < size; j++) {
  407. for (i = 0; i < size; i++) {
  408. int r, g, b;
  409. struct rgbvec *vec = &lut3d->lut[k][j][i];
  410. NEXT_LINE(skip_line(line));
  411. if (sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  412. return AVERROR_INVALIDDATA;
  413. vec->r = r / scale;
  414. vec->g = g / scale;
  415. vec->b = b / scale;
  416. }
  417. }
  418. }
  419. return 0;
  420. }
  421. /* Pandora format */
  422. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  423. {
  424. LUT3DContext *lut3d = ctx->priv;
  425. float scale;
  426. int i, j, k, size, in = -1, out = -1;
  427. char line[MAX_LINE_SIZE];
  428. uint8_t rgb_map[3] = {0, 1, 2};
  429. while (fgets(line, sizeof(line), f)) {
  430. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  431. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  432. else if (!strncmp(line, "values", 6)) {
  433. const char *p = line + 6;
  434. #define SET_COLOR(id) do { \
  435. while (av_isspace(*p)) \
  436. p++; \
  437. switch (*p) { \
  438. case 'r': rgb_map[id] = 0; break; \
  439. case 'g': rgb_map[id] = 1; break; \
  440. case 'b': rgb_map[id] = 2; break; \
  441. } \
  442. while (*p && !av_isspace(*p)) \
  443. p++; \
  444. } while (0)
  445. SET_COLOR(0);
  446. SET_COLOR(1);
  447. SET_COLOR(2);
  448. break;
  449. }
  450. }
  451. if (in == -1 || out == -1) {
  452. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  453. return AVERROR_INVALIDDATA;
  454. }
  455. if (in < 2 || out < 2 ||
  456. in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
  457. out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
  458. av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
  459. return AVERROR_INVALIDDATA;
  460. }
  461. for (size = 1; size*size*size < in; size++);
  462. lut3d->lutsize = size;
  463. scale = 1. / (out - 1);
  464. for (k = 0; k < size; k++) {
  465. for (j = 0; j < size; j++) {
  466. for (i = 0; i < size; i++) {
  467. struct rgbvec *vec = &lut3d->lut[k][j][i];
  468. float val[3];
  469. NEXT_LINE(0);
  470. if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  471. return AVERROR_INVALIDDATA;
  472. vec->r = val[rgb_map[0]] * scale;
  473. vec->g = val[rgb_map[1]] * scale;
  474. vec->b = val[rgb_map[2]] * scale;
  475. }
  476. }
  477. }
  478. return 0;
  479. }
  480. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  481. {
  482. int i, j, k;
  483. const float c = 1. / (size - 1);
  484. lut3d->lutsize = size;
  485. for (k = 0; k < size; k++) {
  486. for (j = 0; j < size; j++) {
  487. for (i = 0; i < size; i++) {
  488. struct rgbvec *vec = &lut3d->lut[k][j][i];
  489. vec->r = k * c;
  490. vec->g = j * c;
  491. vec->b = i * c;
  492. }
  493. }
  494. }
  495. }
  496. static int query_formats(AVFilterContext *ctx)
  497. {
  498. static const enum AVPixelFormat pix_fmts[] = {
  499. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  500. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  501. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  502. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  503. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  504. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  505. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  506. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  507. AV_PIX_FMT_GBRP9,
  508. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
  509. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  510. AV_PIX_FMT_GBRP14,
  511. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  512. AV_PIX_FMT_NONE
  513. };
  514. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  515. if (!fmts_list)
  516. return AVERROR(ENOMEM);
  517. return ff_set_common_formats(ctx, fmts_list);
  518. }
  519. static int config_input(AVFilterLink *inlink)
  520. {
  521. int depth, is16bit = 0, planar = 0;
  522. LUT3DContext *lut3d = inlink->dst->priv;
  523. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  524. depth = desc->comp[0].depth;
  525. switch (inlink->format) {
  526. case AV_PIX_FMT_RGB48:
  527. case AV_PIX_FMT_BGR48:
  528. case AV_PIX_FMT_RGBA64:
  529. case AV_PIX_FMT_BGRA64:
  530. is16bit = 1;
  531. break;
  532. case AV_PIX_FMT_GBRP9:
  533. case AV_PIX_FMT_GBRP10:
  534. case AV_PIX_FMT_GBRP12:
  535. case AV_PIX_FMT_GBRP14:
  536. case AV_PIX_FMT_GBRP16:
  537. case AV_PIX_FMT_GBRAP10:
  538. case AV_PIX_FMT_GBRAP12:
  539. case AV_PIX_FMT_GBRAP16:
  540. is16bit = 1;
  541. case AV_PIX_FMT_GBRP:
  542. case AV_PIX_FMT_GBRAP:
  543. planar = 1;
  544. break;
  545. }
  546. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  547. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  548. #define SET_FUNC(name) do { \
  549. if (planar) { \
  550. switch (depth) { \
  551. case 8: lut3d->interp = interp_8_##name##_p8; break; \
  552. case 9: lut3d->interp = interp_16_##name##_p9; break; \
  553. case 10: lut3d->interp = interp_16_##name##_p10; break; \
  554. case 12: lut3d->interp = interp_16_##name##_p12; break; \
  555. case 14: lut3d->interp = interp_16_##name##_p14; break; \
  556. case 16: lut3d->interp = interp_16_##name##_p16; break; \
  557. } \
  558. } else if (is16bit) { lut3d->interp = interp_16_##name; \
  559. } else { lut3d->interp = interp_8_##name; } \
  560. } while (0)
  561. switch (lut3d->interpolation) {
  562. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  563. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  564. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  565. default:
  566. av_assert0(0);
  567. }
  568. return 0;
  569. }
  570. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  571. {
  572. AVFilterContext *ctx = inlink->dst;
  573. LUT3DContext *lut3d = ctx->priv;
  574. AVFilterLink *outlink = inlink->dst->outputs[0];
  575. AVFrame *out;
  576. ThreadData td;
  577. if (av_frame_is_writable(in)) {
  578. out = in;
  579. } else {
  580. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  581. if (!out) {
  582. av_frame_free(&in);
  583. return NULL;
  584. }
  585. av_frame_copy_props(out, in);
  586. }
  587. td.in = in;
  588. td.out = out;
  589. ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  590. if (out != in)
  591. av_frame_free(&in);
  592. return out;
  593. }
  594. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  595. {
  596. AVFilterLink *outlink = inlink->dst->outputs[0];
  597. AVFrame *out = apply_lut(inlink, in);
  598. if (!out)
  599. return AVERROR(ENOMEM);
  600. return ff_filter_frame(outlink, out);
  601. }
  602. #if CONFIG_LUT3D_FILTER
  603. static const AVOption lut3d_options[] = {
  604. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  605. COMMON_OPTIONS
  606. };
  607. AVFILTER_DEFINE_CLASS(lut3d);
  608. static av_cold int lut3d_init(AVFilterContext *ctx)
  609. {
  610. int ret;
  611. FILE *f;
  612. const char *ext;
  613. LUT3DContext *lut3d = ctx->priv;
  614. if (!lut3d->file) {
  615. set_identity_matrix(lut3d, 32);
  616. return 0;
  617. }
  618. f = fopen(lut3d->file, "r");
  619. if (!f) {
  620. ret = AVERROR(errno);
  621. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  622. return ret;
  623. }
  624. ext = strrchr(lut3d->file, '.');
  625. if (!ext) {
  626. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  627. ret = AVERROR_INVALIDDATA;
  628. goto end;
  629. }
  630. ext++;
  631. if (!av_strcasecmp(ext, "dat")) {
  632. ret = parse_dat(ctx, f);
  633. } else if (!av_strcasecmp(ext, "3dl")) {
  634. ret = parse_3dl(ctx, f);
  635. } else if (!av_strcasecmp(ext, "cube")) {
  636. ret = parse_cube(ctx, f);
  637. } else if (!av_strcasecmp(ext, "m3d")) {
  638. ret = parse_m3d(ctx, f);
  639. } else {
  640. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  641. ret = AVERROR(EINVAL);
  642. }
  643. if (!ret && !lut3d->lutsize) {
  644. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  645. ret = AVERROR_INVALIDDATA;
  646. }
  647. end:
  648. fclose(f);
  649. return ret;
  650. }
  651. static const AVFilterPad lut3d_inputs[] = {
  652. {
  653. .name = "default",
  654. .type = AVMEDIA_TYPE_VIDEO,
  655. .filter_frame = filter_frame,
  656. .config_props = config_input,
  657. },
  658. { NULL }
  659. };
  660. static const AVFilterPad lut3d_outputs[] = {
  661. {
  662. .name = "default",
  663. .type = AVMEDIA_TYPE_VIDEO,
  664. },
  665. { NULL }
  666. };
  667. AVFilter ff_vf_lut3d = {
  668. .name = "lut3d",
  669. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  670. .priv_size = sizeof(LUT3DContext),
  671. .init = lut3d_init,
  672. .query_formats = query_formats,
  673. .inputs = lut3d_inputs,
  674. .outputs = lut3d_outputs,
  675. .priv_class = &lut3d_class,
  676. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  677. };
  678. #endif
  679. #if CONFIG_HALDCLUT_FILTER
  680. static void update_clut_packed(LUT3DContext *lut3d, const AVFrame *frame)
  681. {
  682. const uint8_t *data = frame->data[0];
  683. const int linesize = frame->linesize[0];
  684. const int w = lut3d->clut_width;
  685. const int step = lut3d->clut_step;
  686. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  687. const int level = lut3d->lutsize;
  688. #define LOAD_CLUT(nbits) do { \
  689. int i, j, k, x = 0, y = 0; \
  690. \
  691. for (k = 0; k < level; k++) { \
  692. for (j = 0; j < level; j++) { \
  693. for (i = 0; i < level; i++) { \
  694. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  695. (data + y*linesize + x*step); \
  696. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  697. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  698. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  699. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  700. if (++x == w) { \
  701. x = 0; \
  702. y++; \
  703. } \
  704. } \
  705. } \
  706. } \
  707. } while (0)
  708. switch (lut3d->clut_bits) {
  709. case 8: LOAD_CLUT(8); break;
  710. case 16: LOAD_CLUT(16); break;
  711. }
  712. }
  713. static void update_clut_planar(LUT3DContext *lut3d, const AVFrame *frame)
  714. {
  715. const uint8_t *datag = frame->data[0];
  716. const uint8_t *datab = frame->data[1];
  717. const uint8_t *datar = frame->data[2];
  718. const int glinesize = frame->linesize[0];
  719. const int blinesize = frame->linesize[1];
  720. const int rlinesize = frame->linesize[2];
  721. const int w = lut3d->clut_width;
  722. const int level = lut3d->lutsize;
  723. #define LOAD_CLUT_PLANAR(nbits, depth) do { \
  724. int i, j, k, x = 0, y = 0; \
  725. \
  726. for (k = 0; k < level; k++) { \
  727. for (j = 0; j < level; j++) { \
  728. for (i = 0; i < level; i++) { \
  729. const uint##nbits##_t *gsrc = (const uint##nbits##_t *) \
  730. (datag + y*glinesize); \
  731. const uint##nbits##_t *bsrc = (const uint##nbits##_t *) \
  732. (datab + y*blinesize); \
  733. const uint##nbits##_t *rsrc = (const uint##nbits##_t *) \
  734. (datar + y*rlinesize); \
  735. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  736. vec->r = gsrc[x] / (float)((1<<(depth)) - 1); \
  737. vec->g = bsrc[x] / (float)((1<<(depth)) - 1); \
  738. vec->b = rsrc[x] / (float)((1<<(depth)) - 1); \
  739. if (++x == w) { \
  740. x = 0; \
  741. y++; \
  742. } \
  743. } \
  744. } \
  745. } \
  746. } while (0)
  747. switch (lut3d->clut_bits) {
  748. case 8: LOAD_CLUT_PLANAR(8, 8); break;
  749. case 9: LOAD_CLUT_PLANAR(16, 9); break;
  750. case 10: LOAD_CLUT_PLANAR(16, 10); break;
  751. case 12: LOAD_CLUT_PLANAR(16, 12); break;
  752. case 14: LOAD_CLUT_PLANAR(16, 14); break;
  753. case 16: LOAD_CLUT_PLANAR(16, 16); break;
  754. }
  755. }
  756. static int config_output(AVFilterLink *outlink)
  757. {
  758. AVFilterContext *ctx = outlink->src;
  759. LUT3DContext *lut3d = ctx->priv;
  760. int ret;
  761. ret = ff_framesync_init_dualinput(&lut3d->fs, ctx);
  762. if (ret < 0)
  763. return ret;
  764. outlink->w = ctx->inputs[0]->w;
  765. outlink->h = ctx->inputs[0]->h;
  766. outlink->time_base = ctx->inputs[0]->time_base;
  767. if ((ret = ff_framesync_configure(&lut3d->fs)) < 0)
  768. return ret;
  769. return 0;
  770. }
  771. static int activate(AVFilterContext *ctx)
  772. {
  773. LUT3DContext *s = ctx->priv;
  774. return ff_framesync_activate(&s->fs);
  775. }
  776. static int config_clut(AVFilterLink *inlink)
  777. {
  778. int size, level, w, h;
  779. AVFilterContext *ctx = inlink->dst;
  780. LUT3DContext *lut3d = ctx->priv;
  781. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  782. av_assert0(desc);
  783. lut3d->clut_bits = desc->comp[0].depth;
  784. lut3d->clut_planar = av_pix_fmt_count_planes(inlink->format) > 1;
  785. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  786. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  787. if (inlink->w > inlink->h)
  788. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  789. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  790. else if (inlink->w < inlink->h)
  791. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  792. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  793. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  794. for (level = 1; level*level*level < w; level++);
  795. size = level*level*level;
  796. if (size != w) {
  797. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  798. return AVERROR_INVALIDDATA;
  799. }
  800. av_assert0(w == h && w == size);
  801. level *= level;
  802. if (level > MAX_LEVEL) {
  803. const int max_clut_level = sqrt(MAX_LEVEL);
  804. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  805. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  806. "(maximum level is %d, or %dx%d CLUT)\n",
  807. max_clut_level, max_clut_size, max_clut_size);
  808. return AVERROR(EINVAL);
  809. }
  810. lut3d->lutsize = level;
  811. return 0;
  812. }
  813. static int update_apply_clut(FFFrameSync *fs)
  814. {
  815. AVFilterContext *ctx = fs->parent;
  816. LUT3DContext *lut3d = ctx->priv;
  817. AVFilterLink *inlink = ctx->inputs[0];
  818. AVFrame *master, *second, *out;
  819. int ret;
  820. ret = ff_framesync_dualinput_get(fs, &master, &second);
  821. if (ret < 0)
  822. return ret;
  823. if (!second)
  824. return ff_filter_frame(ctx->outputs[0], master);
  825. if (lut3d->clut_planar)
  826. update_clut_planar(ctx->priv, second);
  827. else
  828. update_clut_packed(ctx->priv, second);
  829. out = apply_lut(inlink, master);
  830. return ff_filter_frame(ctx->outputs[0], out);
  831. }
  832. static av_cold int haldclut_init(AVFilterContext *ctx)
  833. {
  834. LUT3DContext *lut3d = ctx->priv;
  835. lut3d->fs.on_event = update_apply_clut;
  836. return 0;
  837. }
  838. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  839. {
  840. LUT3DContext *lut3d = ctx->priv;
  841. ff_framesync_uninit(&lut3d->fs);
  842. }
  843. static const AVOption haldclut_options[] = {
  844. COMMON_OPTIONS
  845. };
  846. FRAMESYNC_DEFINE_CLASS(haldclut, LUT3DContext, fs);
  847. static const AVFilterPad haldclut_inputs[] = {
  848. {
  849. .name = "main",
  850. .type = AVMEDIA_TYPE_VIDEO,
  851. .config_props = config_input,
  852. },{
  853. .name = "clut",
  854. .type = AVMEDIA_TYPE_VIDEO,
  855. .config_props = config_clut,
  856. },
  857. { NULL }
  858. };
  859. static const AVFilterPad haldclut_outputs[] = {
  860. {
  861. .name = "default",
  862. .type = AVMEDIA_TYPE_VIDEO,
  863. .config_props = config_output,
  864. },
  865. { NULL }
  866. };
  867. AVFilter ff_vf_haldclut = {
  868. .name = "haldclut",
  869. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  870. .priv_size = sizeof(LUT3DContext),
  871. .preinit = haldclut_framesync_preinit,
  872. .init = haldclut_init,
  873. .uninit = haldclut_uninit,
  874. .query_formats = query_formats,
  875. .activate = activate,
  876. .inputs = haldclut_inputs,
  877. .outputs = haldclut_outputs,
  878. .priv_class = &haldclut_class,
  879. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  880. };
  881. #endif
  882. #if CONFIG_LUT1D_FILTER
  883. enum interp_1d_mode {
  884. INTERPOLATE_1D_NEAREST,
  885. INTERPOLATE_1D_LINEAR,
  886. INTERPOLATE_1D_CUBIC,
  887. NB_INTERP_1D_MODE
  888. };
  889. #define MAX_1D_LEVEL 65536
  890. typedef struct LUT1DContext {
  891. const AVClass *class;
  892. char *file;
  893. int interpolation; ///<interp_1d_mode
  894. uint8_t rgba_map[4];
  895. int step;
  896. float lut[3][MAX_1D_LEVEL];
  897. int lutsize;
  898. avfilter_action_func *interp;
  899. } LUT1DContext;
  900. #undef OFFSET
  901. #define OFFSET(x) offsetof(LUT1DContext, x)
  902. static void set_identity_matrix_1d(LUT1DContext *lut1d, int size)
  903. {
  904. const float c = 1. / (size - 1);
  905. int i;
  906. lut1d->lutsize = size;
  907. for (i = 0; i < size; i++) {
  908. lut1d->lut[0][i] = i * c;
  909. lut1d->lut[1][i] = i * c;
  910. lut1d->lut[2][i] = i * c;
  911. }
  912. }
  913. static int parse_cube_1d(AVFilterContext *ctx, FILE *f)
  914. {
  915. LUT1DContext *lut1d = ctx->priv;
  916. char line[MAX_LINE_SIZE];
  917. float min[3] = {0.0, 0.0, 0.0};
  918. float max[3] = {1.0, 1.0, 1.0};
  919. while (fgets(line, sizeof(line), f)) {
  920. if (!strncmp(line, "LUT_1D_SIZE ", 12)) {
  921. const int size = strtol(line + 12, NULL, 0);
  922. int i;
  923. if (size < 2 || size > MAX_1D_LEVEL) {
  924. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 1D LUT size\n");
  925. return AVERROR(EINVAL);
  926. }
  927. lut1d->lutsize = size;
  928. for (i = 0; i < size; i++) {
  929. do {
  930. try_again:
  931. NEXT_LINE(0);
  932. if (!strncmp(line, "DOMAIN_", 7)) {
  933. float *vals = NULL;
  934. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  935. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  936. if (!vals)
  937. return AVERROR_INVALIDDATA;
  938. sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  939. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  940. min[0], min[1], min[2], max[0], max[1], max[2]);
  941. goto try_again;
  942. } else if (!strncmp(line, "LUT_1D_INPUT_RANGE ", 19)) {
  943. sscanf(line + 19, "%f %f", min, max);
  944. min[1] = min[2] = min[0];
  945. max[1] = max[2] = max[0];
  946. goto try_again;
  947. }
  948. } while (skip_line(line));
  949. if (sscanf(line, "%f %f %f", &lut1d->lut[0][i], &lut1d->lut[1][i], &lut1d->lut[2][i]) != 3)
  950. return AVERROR_INVALIDDATA;
  951. lut1d->lut[0][i] *= max[0] - min[0];
  952. lut1d->lut[1][i] *= max[1] - min[1];
  953. lut1d->lut[2][i] *= max[2] - min[2];
  954. }
  955. break;
  956. }
  957. }
  958. return 0;
  959. }
  960. static const AVOption lut1d_options[] = {
  961. { "file", "set 1D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  962. { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_1D_LINEAR}, 0, NB_INTERP_1D_MODE-1, FLAGS, "interp_mode" },
  963. { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  964. { "linear", "use values from the linear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_LINEAR}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  965. { "cubic", "use values from the cubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_CUBIC}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  966. { NULL }
  967. };
  968. AVFILTER_DEFINE_CLASS(lut1d);
  969. static inline float interp_1d_nearest(const LUT1DContext *lut1d,
  970. int idx, const float s)
  971. {
  972. return lut1d->lut[idx][NEAR(s)];
  973. }
  974. #define NEXT1D(x) (FFMIN((int)(x) + 1, lut1d->lutsize - 1))
  975. static inline float interp_1d_linear(const LUT1DContext *lut1d,
  976. int idx, const float s)
  977. {
  978. const int prev = PREV(s);
  979. const int next = NEXT1D(s);
  980. const float d = s - prev;
  981. const float p = lut1d->lut[idx][prev];
  982. const float n = lut1d->lut[idx][next];
  983. return lerpf(p, n, d);
  984. }
  985. static inline float interp_1d_cubic(const LUT1DContext *lut1d,
  986. int idx, const float s)
  987. {
  988. const int prev = PREV(s);
  989. const int next = NEXT1D(s);
  990. const float mu = s - prev;
  991. float a0, a1, a2, a3, mu2;
  992. float y0 = lut1d->lut[idx][FFMAX(prev - 1, 0)];
  993. float y1 = lut1d->lut[idx][prev];
  994. float y2 = lut1d->lut[idx][next];
  995. float y3 = lut1d->lut[idx][FFMIN(next + 1, lut1d->lutsize - 1)];
  996. mu2 = mu * mu;
  997. a0 = y3 - y2 - y0 + y1;
  998. a1 = y0 - y1 - a0;
  999. a2 = y2 - y0;
  1000. a3 = y1;
  1001. return a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3;
  1002. }
  1003. #define DEFINE_INTERP_FUNC_PLANAR_1D(name, nbits, depth) \
  1004. static int interp_1d_##nbits##_##name##_p##depth(AVFilterContext *ctx, \
  1005. void *arg, int jobnr, \
  1006. int nb_jobs) \
  1007. { \
  1008. int x, y; \
  1009. const LUT1DContext *lut1d = ctx->priv; \
  1010. const ThreadData *td = arg; \
  1011. const AVFrame *in = td->in; \
  1012. const AVFrame *out = td->out; \
  1013. const int direct = out == in; \
  1014. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  1015. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  1016. uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
  1017. uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
  1018. uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
  1019. uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
  1020. const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
  1021. const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
  1022. const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
  1023. const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
  1024. const float factor = (1 << depth) - 1; \
  1025. const float scale = (1. / factor) * (lut1d->lutsize - 1); \
  1026. \
  1027. for (y = slice_start; y < slice_end; y++) { \
  1028. uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
  1029. uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
  1030. uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
  1031. uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
  1032. const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
  1033. const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
  1034. const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
  1035. const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
  1036. for (x = 0; x < in->width; x++) { \
  1037. float r = srcr[x] * scale; \
  1038. float g = srcg[x] * scale; \
  1039. float b = srcb[x] * scale; \
  1040. r = interp_1d_##name(lut1d, 0, r); \
  1041. g = interp_1d_##name(lut1d, 1, g); \
  1042. b = interp_1d_##name(lut1d, 2, b); \
  1043. dstr[x] = av_clip_uintp2(r * factor, depth); \
  1044. dstg[x] = av_clip_uintp2(g * factor, depth); \
  1045. dstb[x] = av_clip_uintp2(b * factor, depth); \
  1046. if (!direct && in->linesize[3]) \
  1047. dsta[x] = srca[x]; \
  1048. } \
  1049. grow += out->linesize[0]; \
  1050. brow += out->linesize[1]; \
  1051. rrow += out->linesize[2]; \
  1052. arow += out->linesize[3]; \
  1053. srcgrow += in->linesize[0]; \
  1054. srcbrow += in->linesize[1]; \
  1055. srcrrow += in->linesize[2]; \
  1056. srcarow += in->linesize[3]; \
  1057. } \
  1058. return 0; \
  1059. }
  1060. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 8, 8)
  1061. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 8, 8)
  1062. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 8, 8)
  1063. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 9)
  1064. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 9)
  1065. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 9)
  1066. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 10)
  1067. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 10)
  1068. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 10)
  1069. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 12)
  1070. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 12)
  1071. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 12)
  1072. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 14)
  1073. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 14)
  1074. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 14)
  1075. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 16)
  1076. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 16)
  1077. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 16)
  1078. #define DEFINE_INTERP_FUNC_1D(name, nbits) \
  1079. static int interp_1d_##nbits##_##name(AVFilterContext *ctx, void *arg, \
  1080. int jobnr, int nb_jobs) \
  1081. { \
  1082. int x, y; \
  1083. const LUT1DContext *lut1d = ctx->priv; \
  1084. const ThreadData *td = arg; \
  1085. const AVFrame *in = td->in; \
  1086. const AVFrame *out = td->out; \
  1087. const int direct = out == in; \
  1088. const int step = lut1d->step; \
  1089. const uint8_t r = lut1d->rgba_map[R]; \
  1090. const uint8_t g = lut1d->rgba_map[G]; \
  1091. const uint8_t b = lut1d->rgba_map[B]; \
  1092. const uint8_t a = lut1d->rgba_map[A]; \
  1093. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  1094. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  1095. uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
  1096. const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
  1097. const float factor = (1 << nbits) - 1; \
  1098. const float scale = (1. / factor) * (lut1d->lutsize - 1); \
  1099. \
  1100. for (y = slice_start; y < slice_end; y++) { \
  1101. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  1102. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  1103. for (x = 0; x < in->width * step; x += step) { \
  1104. float rr = src[x + r] * scale; \
  1105. float gg = src[x + g] * scale; \
  1106. float bb = src[x + b] * scale; \
  1107. rr = interp_1d_##name(lut1d, 0, rr); \
  1108. gg = interp_1d_##name(lut1d, 1, gg); \
  1109. bb = interp_1d_##name(lut1d, 2, bb); \
  1110. dst[x + r] = av_clip_uint##nbits(rr * factor); \
  1111. dst[x + g] = av_clip_uint##nbits(gg * factor); \
  1112. dst[x + b] = av_clip_uint##nbits(bb * factor); \
  1113. if (!direct && step == 4) \
  1114. dst[x + a] = src[x + a]; \
  1115. } \
  1116. dstrow += out->linesize[0]; \
  1117. srcrow += in ->linesize[0]; \
  1118. } \
  1119. return 0; \
  1120. }
  1121. DEFINE_INTERP_FUNC_1D(nearest, 8)
  1122. DEFINE_INTERP_FUNC_1D(linear, 8)
  1123. DEFINE_INTERP_FUNC_1D(cubic, 8)
  1124. DEFINE_INTERP_FUNC_1D(nearest, 16)
  1125. DEFINE_INTERP_FUNC_1D(linear, 16)
  1126. DEFINE_INTERP_FUNC_1D(cubic, 16)
  1127. static int config_input_1d(AVFilterLink *inlink)
  1128. {
  1129. int depth, is16bit = 0, planar = 0;
  1130. LUT1DContext *lut1d = inlink->dst->priv;
  1131. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  1132. depth = desc->comp[0].depth;
  1133. switch (inlink->format) {
  1134. case AV_PIX_FMT_RGB48:
  1135. case AV_PIX_FMT_BGR48:
  1136. case AV_PIX_FMT_RGBA64:
  1137. case AV_PIX_FMT_BGRA64:
  1138. is16bit = 1;
  1139. break;
  1140. case AV_PIX_FMT_GBRP9:
  1141. case AV_PIX_FMT_GBRP10:
  1142. case AV_PIX_FMT_GBRP12:
  1143. case AV_PIX_FMT_GBRP14:
  1144. case AV_PIX_FMT_GBRP16:
  1145. case AV_PIX_FMT_GBRAP10:
  1146. case AV_PIX_FMT_GBRAP12:
  1147. case AV_PIX_FMT_GBRAP16:
  1148. is16bit = 1;
  1149. case AV_PIX_FMT_GBRP:
  1150. case AV_PIX_FMT_GBRAP:
  1151. planar = 1;
  1152. break;
  1153. }
  1154. ff_fill_rgba_map(lut1d->rgba_map, inlink->format);
  1155. lut1d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  1156. #define SET_FUNC_1D(name) do { \
  1157. if (planar) { \
  1158. switch (depth) { \
  1159. case 8: lut1d->interp = interp_1d_8_##name##_p8; break; \
  1160. case 9: lut1d->interp = interp_1d_16_##name##_p9; break; \
  1161. case 10: lut1d->interp = interp_1d_16_##name##_p10; break; \
  1162. case 12: lut1d->interp = interp_1d_16_##name##_p12; break; \
  1163. case 14: lut1d->interp = interp_1d_16_##name##_p14; break; \
  1164. case 16: lut1d->interp = interp_1d_16_##name##_p16; break; \
  1165. } \
  1166. } else if (is16bit) { lut1d->interp = interp_1d_16_##name; \
  1167. } else { lut1d->interp = interp_1d_8_##name; } \
  1168. } while (0)
  1169. switch (lut1d->interpolation) {
  1170. case INTERPOLATE_1D_NEAREST: SET_FUNC_1D(nearest); break;
  1171. case INTERPOLATE_1D_LINEAR: SET_FUNC_1D(linear); break;
  1172. case INTERPOLATE_1D_CUBIC: SET_FUNC_1D(cubic); break;
  1173. default:
  1174. av_assert0(0);
  1175. }
  1176. return 0;
  1177. }
  1178. static av_cold int lut1d_init(AVFilterContext *ctx)
  1179. {
  1180. int ret;
  1181. FILE *f;
  1182. const char *ext;
  1183. LUT1DContext *lut1d = ctx->priv;
  1184. if (!lut1d->file) {
  1185. set_identity_matrix_1d(lut1d, 32);
  1186. return 0;
  1187. }
  1188. f = fopen(lut1d->file, "r");
  1189. if (!f) {
  1190. ret = AVERROR(errno);
  1191. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut1d->file, av_err2str(ret));
  1192. return ret;
  1193. }
  1194. ext = strrchr(lut1d->file, '.');
  1195. if (!ext) {
  1196. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  1197. ret = AVERROR_INVALIDDATA;
  1198. goto end;
  1199. }
  1200. ext++;
  1201. if (!av_strcasecmp(ext, "cube") || !av_strcasecmp(ext, "1dlut")) {
  1202. ret = parse_cube_1d(ctx, f);
  1203. } else {
  1204. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  1205. ret = AVERROR(EINVAL);
  1206. }
  1207. if (!ret && !lut1d->lutsize) {
  1208. av_log(ctx, AV_LOG_ERROR, "1D LUT is empty\n");
  1209. ret = AVERROR_INVALIDDATA;
  1210. }
  1211. end:
  1212. fclose(f);
  1213. return ret;
  1214. }
  1215. static AVFrame *apply_1d_lut(AVFilterLink *inlink, AVFrame *in)
  1216. {
  1217. AVFilterContext *ctx = inlink->dst;
  1218. LUT1DContext *lut1d = ctx->priv;
  1219. AVFilterLink *outlink = inlink->dst->outputs[0];
  1220. AVFrame *out;
  1221. ThreadData td;
  1222. if (av_frame_is_writable(in)) {
  1223. out = in;
  1224. } else {
  1225. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  1226. if (!out) {
  1227. av_frame_free(&in);
  1228. return NULL;
  1229. }
  1230. av_frame_copy_props(out, in);
  1231. }
  1232. td.in = in;
  1233. td.out = out;
  1234. ctx->internal->execute(ctx, lut1d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  1235. if (out != in)
  1236. av_frame_free(&in);
  1237. return out;
  1238. }
  1239. static int filter_frame_1d(AVFilterLink *inlink, AVFrame *in)
  1240. {
  1241. AVFilterLink *outlink = inlink->dst->outputs[0];
  1242. AVFrame *out = apply_1d_lut(inlink, in);
  1243. if (!out)
  1244. return AVERROR(ENOMEM);
  1245. return ff_filter_frame(outlink, out);
  1246. }
  1247. static const AVFilterPad lut1d_inputs[] = {
  1248. {
  1249. .name = "default",
  1250. .type = AVMEDIA_TYPE_VIDEO,
  1251. .filter_frame = filter_frame_1d,
  1252. .config_props = config_input_1d,
  1253. },
  1254. { NULL }
  1255. };
  1256. static const AVFilterPad lut1d_outputs[] = {
  1257. {
  1258. .name = "default",
  1259. .type = AVMEDIA_TYPE_VIDEO,
  1260. },
  1261. { NULL }
  1262. };
  1263. AVFilter ff_vf_lut1d = {
  1264. .name = "lut1d",
  1265. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 1D LUT."),
  1266. .priv_size = sizeof(LUT1DContext),
  1267. .init = lut1d_init,
  1268. .query_formats = query_formats,
  1269. .inputs = lut1d_inputs,
  1270. .outputs = lut1d_outputs,
  1271. .priv_class = &lut1d_class,
  1272. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  1273. };
  1274. #endif