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.

1486 lines
60KB

  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 (av_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", 11)) {
  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. av_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. } else if (!strncmp(line, "TITLE", 5)) {
  380. goto try_again;
  381. }
  382. } while (skip_line(line));
  383. if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  384. return AVERROR_INVALIDDATA;
  385. vec->r *= max[0] - min[0];
  386. vec->g *= max[1] - min[1];
  387. vec->b *= max[2] - min[2];
  388. }
  389. }
  390. }
  391. break;
  392. }
  393. }
  394. return 0;
  395. }
  396. /* Assume 17x17x17 LUT with a 16-bit depth
  397. * FIXME: it seems there are various 3dl formats */
  398. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  399. {
  400. char line[MAX_LINE_SIZE];
  401. LUT3DContext *lut3d = ctx->priv;
  402. int i, j, k;
  403. const int size = 17;
  404. const float scale = 16*16*16;
  405. lut3d->lutsize = size;
  406. NEXT_LINE(skip_line(line));
  407. for (k = 0; k < size; k++) {
  408. for (j = 0; j < size; j++) {
  409. for (i = 0; i < size; i++) {
  410. int r, g, b;
  411. struct rgbvec *vec = &lut3d->lut[k][j][i];
  412. NEXT_LINE(skip_line(line));
  413. if (av_sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  414. return AVERROR_INVALIDDATA;
  415. vec->r = r / scale;
  416. vec->g = g / scale;
  417. vec->b = b / scale;
  418. }
  419. }
  420. }
  421. return 0;
  422. }
  423. /* Pandora format */
  424. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  425. {
  426. LUT3DContext *lut3d = ctx->priv;
  427. float scale;
  428. int i, j, k, size, in = -1, out = -1;
  429. char line[MAX_LINE_SIZE];
  430. uint8_t rgb_map[3] = {0, 1, 2};
  431. while (fgets(line, sizeof(line), f)) {
  432. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  433. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  434. else if (!strncmp(line, "values", 6)) {
  435. const char *p = line + 6;
  436. #define SET_COLOR(id) do { \
  437. while (av_isspace(*p)) \
  438. p++; \
  439. switch (*p) { \
  440. case 'r': rgb_map[id] = 0; break; \
  441. case 'g': rgb_map[id] = 1; break; \
  442. case 'b': rgb_map[id] = 2; break; \
  443. } \
  444. while (*p && !av_isspace(*p)) \
  445. p++; \
  446. } while (0)
  447. SET_COLOR(0);
  448. SET_COLOR(1);
  449. SET_COLOR(2);
  450. break;
  451. }
  452. }
  453. if (in == -1 || out == -1) {
  454. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  455. return AVERROR_INVALIDDATA;
  456. }
  457. if (in < 2 || out < 2 ||
  458. in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
  459. out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
  460. av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
  461. return AVERROR_INVALIDDATA;
  462. }
  463. for (size = 1; size*size*size < in; size++);
  464. lut3d->lutsize = size;
  465. scale = 1. / (out - 1);
  466. for (k = 0; k < size; k++) {
  467. for (j = 0; j < size; j++) {
  468. for (i = 0; i < size; i++) {
  469. struct rgbvec *vec = &lut3d->lut[k][j][i];
  470. float val[3];
  471. NEXT_LINE(0);
  472. if (av_sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  473. return AVERROR_INVALIDDATA;
  474. vec->r = val[rgb_map[0]] * scale;
  475. vec->g = val[rgb_map[1]] * scale;
  476. vec->b = val[rgb_map[2]] * scale;
  477. }
  478. }
  479. }
  480. return 0;
  481. }
  482. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  483. {
  484. int i, j, k;
  485. const float c = 1. / (size - 1);
  486. lut3d->lutsize = size;
  487. for (k = 0; k < size; k++) {
  488. for (j = 0; j < size; j++) {
  489. for (i = 0; i < size; i++) {
  490. struct rgbvec *vec = &lut3d->lut[k][j][i];
  491. vec->r = k * c;
  492. vec->g = j * c;
  493. vec->b = i * c;
  494. }
  495. }
  496. }
  497. }
  498. static int query_formats(AVFilterContext *ctx)
  499. {
  500. static const enum AVPixelFormat pix_fmts[] = {
  501. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  502. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  503. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  504. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  505. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  506. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  507. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  508. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  509. AV_PIX_FMT_GBRP9,
  510. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
  511. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  512. AV_PIX_FMT_GBRP14,
  513. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  514. AV_PIX_FMT_NONE
  515. };
  516. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  517. if (!fmts_list)
  518. return AVERROR(ENOMEM);
  519. return ff_set_common_formats(ctx, fmts_list);
  520. }
  521. static int config_input(AVFilterLink *inlink)
  522. {
  523. int depth, is16bit = 0, planar = 0;
  524. LUT3DContext *lut3d = inlink->dst->priv;
  525. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  526. depth = desc->comp[0].depth;
  527. switch (inlink->format) {
  528. case AV_PIX_FMT_RGB48:
  529. case AV_PIX_FMT_BGR48:
  530. case AV_PIX_FMT_RGBA64:
  531. case AV_PIX_FMT_BGRA64:
  532. is16bit = 1;
  533. break;
  534. case AV_PIX_FMT_GBRP9:
  535. case AV_PIX_FMT_GBRP10:
  536. case AV_PIX_FMT_GBRP12:
  537. case AV_PIX_FMT_GBRP14:
  538. case AV_PIX_FMT_GBRP16:
  539. case AV_PIX_FMT_GBRAP10:
  540. case AV_PIX_FMT_GBRAP12:
  541. case AV_PIX_FMT_GBRAP16:
  542. is16bit = 1;
  543. case AV_PIX_FMT_GBRP:
  544. case AV_PIX_FMT_GBRAP:
  545. planar = 1;
  546. break;
  547. }
  548. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  549. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  550. #define SET_FUNC(name) do { \
  551. if (planar) { \
  552. switch (depth) { \
  553. case 8: lut3d->interp = interp_8_##name##_p8; break; \
  554. case 9: lut3d->interp = interp_16_##name##_p9; break; \
  555. case 10: lut3d->interp = interp_16_##name##_p10; break; \
  556. case 12: lut3d->interp = interp_16_##name##_p12; break; \
  557. case 14: lut3d->interp = interp_16_##name##_p14; break; \
  558. case 16: lut3d->interp = interp_16_##name##_p16; break; \
  559. } \
  560. } else if (is16bit) { lut3d->interp = interp_16_##name; \
  561. } else { lut3d->interp = interp_8_##name; } \
  562. } while (0)
  563. switch (lut3d->interpolation) {
  564. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  565. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  566. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  567. default:
  568. av_assert0(0);
  569. }
  570. return 0;
  571. }
  572. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  573. {
  574. AVFilterContext *ctx = inlink->dst;
  575. LUT3DContext *lut3d = ctx->priv;
  576. AVFilterLink *outlink = inlink->dst->outputs[0];
  577. AVFrame *out;
  578. ThreadData td;
  579. if (av_frame_is_writable(in)) {
  580. out = in;
  581. } else {
  582. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  583. if (!out) {
  584. av_frame_free(&in);
  585. return NULL;
  586. }
  587. av_frame_copy_props(out, in);
  588. }
  589. td.in = in;
  590. td.out = out;
  591. ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  592. if (out != in)
  593. av_frame_free(&in);
  594. return out;
  595. }
  596. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  597. {
  598. AVFilterLink *outlink = inlink->dst->outputs[0];
  599. AVFrame *out = apply_lut(inlink, in);
  600. if (!out)
  601. return AVERROR(ENOMEM);
  602. return ff_filter_frame(outlink, out);
  603. }
  604. #if CONFIG_LUT3D_FILTER
  605. static const AVOption lut3d_options[] = {
  606. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  607. COMMON_OPTIONS
  608. };
  609. AVFILTER_DEFINE_CLASS(lut3d);
  610. static av_cold int lut3d_init(AVFilterContext *ctx)
  611. {
  612. int ret;
  613. FILE *f;
  614. const char *ext;
  615. LUT3DContext *lut3d = ctx->priv;
  616. if (!lut3d->file) {
  617. set_identity_matrix(lut3d, 32);
  618. return 0;
  619. }
  620. f = fopen(lut3d->file, "r");
  621. if (!f) {
  622. ret = AVERROR(errno);
  623. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  624. return ret;
  625. }
  626. ext = strrchr(lut3d->file, '.');
  627. if (!ext) {
  628. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  629. ret = AVERROR_INVALIDDATA;
  630. goto end;
  631. }
  632. ext++;
  633. if (!av_strcasecmp(ext, "dat")) {
  634. ret = parse_dat(ctx, f);
  635. } else if (!av_strcasecmp(ext, "3dl")) {
  636. ret = parse_3dl(ctx, f);
  637. } else if (!av_strcasecmp(ext, "cube")) {
  638. ret = parse_cube(ctx, f);
  639. } else if (!av_strcasecmp(ext, "m3d")) {
  640. ret = parse_m3d(ctx, f);
  641. } else {
  642. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  643. ret = AVERROR(EINVAL);
  644. }
  645. if (!ret && !lut3d->lutsize) {
  646. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  647. ret = AVERROR_INVALIDDATA;
  648. }
  649. end:
  650. fclose(f);
  651. return ret;
  652. }
  653. static const AVFilterPad lut3d_inputs[] = {
  654. {
  655. .name = "default",
  656. .type = AVMEDIA_TYPE_VIDEO,
  657. .filter_frame = filter_frame,
  658. .config_props = config_input,
  659. },
  660. { NULL }
  661. };
  662. static const AVFilterPad lut3d_outputs[] = {
  663. {
  664. .name = "default",
  665. .type = AVMEDIA_TYPE_VIDEO,
  666. },
  667. { NULL }
  668. };
  669. AVFilter ff_vf_lut3d = {
  670. .name = "lut3d",
  671. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  672. .priv_size = sizeof(LUT3DContext),
  673. .init = lut3d_init,
  674. .query_formats = query_formats,
  675. .inputs = lut3d_inputs,
  676. .outputs = lut3d_outputs,
  677. .priv_class = &lut3d_class,
  678. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  679. };
  680. #endif
  681. #if CONFIG_HALDCLUT_FILTER
  682. static void update_clut_packed(LUT3DContext *lut3d, const AVFrame *frame)
  683. {
  684. const uint8_t *data = frame->data[0];
  685. const int linesize = frame->linesize[0];
  686. const int w = lut3d->clut_width;
  687. const int step = lut3d->clut_step;
  688. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  689. const int level = lut3d->lutsize;
  690. #define LOAD_CLUT(nbits) do { \
  691. int i, j, k, x = 0, y = 0; \
  692. \
  693. for (k = 0; k < level; k++) { \
  694. for (j = 0; j < level; j++) { \
  695. for (i = 0; i < level; i++) { \
  696. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  697. (data + y*linesize + x*step); \
  698. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  699. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  700. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  701. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  702. if (++x == w) { \
  703. x = 0; \
  704. y++; \
  705. } \
  706. } \
  707. } \
  708. } \
  709. } while (0)
  710. switch (lut3d->clut_bits) {
  711. case 8: LOAD_CLUT(8); break;
  712. case 16: LOAD_CLUT(16); break;
  713. }
  714. }
  715. static void update_clut_planar(LUT3DContext *lut3d, const AVFrame *frame)
  716. {
  717. const uint8_t *datag = frame->data[0];
  718. const uint8_t *datab = frame->data[1];
  719. const uint8_t *datar = frame->data[2];
  720. const int glinesize = frame->linesize[0];
  721. const int blinesize = frame->linesize[1];
  722. const int rlinesize = frame->linesize[2];
  723. const int w = lut3d->clut_width;
  724. const int level = lut3d->lutsize;
  725. #define LOAD_CLUT_PLANAR(nbits, depth) do { \
  726. int i, j, k, x = 0, y = 0; \
  727. \
  728. for (k = 0; k < level; k++) { \
  729. for (j = 0; j < level; j++) { \
  730. for (i = 0; i < level; i++) { \
  731. const uint##nbits##_t *gsrc = (const uint##nbits##_t *) \
  732. (datag + y*glinesize); \
  733. const uint##nbits##_t *bsrc = (const uint##nbits##_t *) \
  734. (datab + y*blinesize); \
  735. const uint##nbits##_t *rsrc = (const uint##nbits##_t *) \
  736. (datar + y*rlinesize); \
  737. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  738. vec->r = gsrc[x] / (float)((1<<(depth)) - 1); \
  739. vec->g = bsrc[x] / (float)((1<<(depth)) - 1); \
  740. vec->b = rsrc[x] / (float)((1<<(depth)) - 1); \
  741. if (++x == w) { \
  742. x = 0; \
  743. y++; \
  744. } \
  745. } \
  746. } \
  747. } \
  748. } while (0)
  749. switch (lut3d->clut_bits) {
  750. case 8: LOAD_CLUT_PLANAR(8, 8); break;
  751. case 9: LOAD_CLUT_PLANAR(16, 9); break;
  752. case 10: LOAD_CLUT_PLANAR(16, 10); break;
  753. case 12: LOAD_CLUT_PLANAR(16, 12); break;
  754. case 14: LOAD_CLUT_PLANAR(16, 14); break;
  755. case 16: LOAD_CLUT_PLANAR(16, 16); break;
  756. }
  757. }
  758. static int config_output(AVFilterLink *outlink)
  759. {
  760. AVFilterContext *ctx = outlink->src;
  761. LUT3DContext *lut3d = ctx->priv;
  762. int ret;
  763. ret = ff_framesync_init_dualinput(&lut3d->fs, ctx);
  764. if (ret < 0)
  765. return ret;
  766. outlink->w = ctx->inputs[0]->w;
  767. outlink->h = ctx->inputs[0]->h;
  768. outlink->time_base = ctx->inputs[0]->time_base;
  769. if ((ret = ff_framesync_configure(&lut3d->fs)) < 0)
  770. return ret;
  771. return 0;
  772. }
  773. static int activate(AVFilterContext *ctx)
  774. {
  775. LUT3DContext *s = ctx->priv;
  776. return ff_framesync_activate(&s->fs);
  777. }
  778. static int config_clut(AVFilterLink *inlink)
  779. {
  780. int size, level, w, h;
  781. AVFilterContext *ctx = inlink->dst;
  782. LUT3DContext *lut3d = ctx->priv;
  783. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  784. av_assert0(desc);
  785. lut3d->clut_bits = desc->comp[0].depth;
  786. lut3d->clut_planar = av_pix_fmt_count_planes(inlink->format) > 1;
  787. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  788. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  789. if (inlink->w > inlink->h)
  790. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  791. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  792. else if (inlink->w < inlink->h)
  793. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  794. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  795. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  796. for (level = 1; level*level*level < w; level++);
  797. size = level*level*level;
  798. if (size != w) {
  799. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  800. return AVERROR_INVALIDDATA;
  801. }
  802. av_assert0(w == h && w == size);
  803. level *= level;
  804. if (level > MAX_LEVEL) {
  805. const int max_clut_level = sqrt(MAX_LEVEL);
  806. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  807. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  808. "(maximum level is %d, or %dx%d CLUT)\n",
  809. max_clut_level, max_clut_size, max_clut_size);
  810. return AVERROR(EINVAL);
  811. }
  812. lut3d->lutsize = level;
  813. return 0;
  814. }
  815. static int update_apply_clut(FFFrameSync *fs)
  816. {
  817. AVFilterContext *ctx = fs->parent;
  818. LUT3DContext *lut3d = ctx->priv;
  819. AVFilterLink *inlink = ctx->inputs[0];
  820. AVFrame *master, *second, *out;
  821. int ret;
  822. ret = ff_framesync_dualinput_get(fs, &master, &second);
  823. if (ret < 0)
  824. return ret;
  825. if (!second)
  826. return ff_filter_frame(ctx->outputs[0], master);
  827. if (lut3d->clut_planar)
  828. update_clut_planar(ctx->priv, second);
  829. else
  830. update_clut_packed(ctx->priv, second);
  831. out = apply_lut(inlink, master);
  832. return ff_filter_frame(ctx->outputs[0], out);
  833. }
  834. static av_cold int haldclut_init(AVFilterContext *ctx)
  835. {
  836. LUT3DContext *lut3d = ctx->priv;
  837. lut3d->fs.on_event = update_apply_clut;
  838. return 0;
  839. }
  840. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  841. {
  842. LUT3DContext *lut3d = ctx->priv;
  843. ff_framesync_uninit(&lut3d->fs);
  844. }
  845. static const AVOption haldclut_options[] = {
  846. COMMON_OPTIONS
  847. };
  848. FRAMESYNC_DEFINE_CLASS(haldclut, LUT3DContext, fs);
  849. static const AVFilterPad haldclut_inputs[] = {
  850. {
  851. .name = "main",
  852. .type = AVMEDIA_TYPE_VIDEO,
  853. .config_props = config_input,
  854. },{
  855. .name = "clut",
  856. .type = AVMEDIA_TYPE_VIDEO,
  857. .config_props = config_clut,
  858. },
  859. { NULL }
  860. };
  861. static const AVFilterPad haldclut_outputs[] = {
  862. {
  863. .name = "default",
  864. .type = AVMEDIA_TYPE_VIDEO,
  865. .config_props = config_output,
  866. },
  867. { NULL }
  868. };
  869. AVFilter ff_vf_haldclut = {
  870. .name = "haldclut",
  871. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  872. .priv_size = sizeof(LUT3DContext),
  873. .preinit = haldclut_framesync_preinit,
  874. .init = haldclut_init,
  875. .uninit = haldclut_uninit,
  876. .query_formats = query_formats,
  877. .activate = activate,
  878. .inputs = haldclut_inputs,
  879. .outputs = haldclut_outputs,
  880. .priv_class = &haldclut_class,
  881. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  882. };
  883. #endif
  884. #if CONFIG_LUT1D_FILTER
  885. enum interp_1d_mode {
  886. INTERPOLATE_1D_NEAREST,
  887. INTERPOLATE_1D_LINEAR,
  888. INTERPOLATE_1D_CUBIC,
  889. INTERPOLATE_1D_COSINE,
  890. INTERPOLATE_1D_SPLINE,
  891. NB_INTERP_1D_MODE
  892. };
  893. #define MAX_1D_LEVEL 65536
  894. typedef struct LUT1DContext {
  895. const AVClass *class;
  896. char *file;
  897. int interpolation; ///<interp_1d_mode
  898. uint8_t rgba_map[4];
  899. int step;
  900. float lut[3][MAX_1D_LEVEL];
  901. int lutsize;
  902. avfilter_action_func *interp;
  903. } LUT1DContext;
  904. #undef OFFSET
  905. #define OFFSET(x) offsetof(LUT1DContext, x)
  906. static void set_identity_matrix_1d(LUT1DContext *lut1d, int size)
  907. {
  908. const float c = 1. / (size - 1);
  909. int i;
  910. lut1d->lutsize = size;
  911. for (i = 0; i < size; i++) {
  912. lut1d->lut[0][i] = i * c;
  913. lut1d->lut[1][i] = i * c;
  914. lut1d->lut[2][i] = i * c;
  915. }
  916. }
  917. static int parse_cube_1d(AVFilterContext *ctx, FILE *f)
  918. {
  919. LUT1DContext *lut1d = ctx->priv;
  920. char line[MAX_LINE_SIZE];
  921. float min[3] = {0.0, 0.0, 0.0};
  922. float max[3] = {1.0, 1.0, 1.0};
  923. while (fgets(line, sizeof(line), f)) {
  924. if (!strncmp(line, "LUT_1D_SIZE", 11)) {
  925. const int size = strtol(line + 12, NULL, 0);
  926. int i;
  927. if (size < 2 || size > MAX_1D_LEVEL) {
  928. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 1D LUT size\n");
  929. return AVERROR(EINVAL);
  930. }
  931. lut1d->lutsize = size;
  932. for (i = 0; i < size; i++) {
  933. do {
  934. try_again:
  935. NEXT_LINE(0);
  936. if (!strncmp(line, "DOMAIN_", 7)) {
  937. float *vals = NULL;
  938. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  939. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  940. if (!vals)
  941. return AVERROR_INVALIDDATA;
  942. av_sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  943. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  944. min[0], min[1], min[2], max[0], max[1], max[2]);
  945. goto try_again;
  946. } else if (!strncmp(line, "LUT_1D_INPUT_RANGE ", 19)) {
  947. av_sscanf(line + 19, "%f %f", min, max);
  948. min[1] = min[2] = min[0];
  949. max[1] = max[2] = max[0];
  950. goto try_again;
  951. } else if (!strncmp(line, "TITLE", 5)) {
  952. goto try_again;
  953. }
  954. } while (skip_line(line));
  955. if (av_sscanf(line, "%f %f %f", &lut1d->lut[0][i], &lut1d->lut[1][i], &lut1d->lut[2][i]) != 3)
  956. return AVERROR_INVALIDDATA;
  957. lut1d->lut[0][i] *= max[0] - min[0];
  958. lut1d->lut[1][i] *= max[1] - min[1];
  959. lut1d->lut[2][i] *= max[2] - min[2];
  960. }
  961. break;
  962. }
  963. }
  964. return 0;
  965. }
  966. static const AVOption lut1d_options[] = {
  967. { "file", "set 1D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  968. { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_1D_LINEAR}, 0, NB_INTERP_1D_MODE-1, FLAGS, "interp_mode" },
  969. { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  970. { "linear", "use values from the linear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_LINEAR}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  971. { "cosine", "use values from the cosine interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_COSINE}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  972. { "cubic", "use values from the cubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_CUBIC}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  973. { "spline", "use values from the spline interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_SPLINE}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  974. { NULL }
  975. };
  976. AVFILTER_DEFINE_CLASS(lut1d);
  977. static inline float interp_1d_nearest(const LUT1DContext *lut1d,
  978. int idx, const float s)
  979. {
  980. return lut1d->lut[idx][NEAR(s)];
  981. }
  982. #define NEXT1D(x) (FFMIN((int)(x) + 1, lut1d->lutsize - 1))
  983. static inline float interp_1d_linear(const LUT1DContext *lut1d,
  984. int idx, const float s)
  985. {
  986. const int prev = PREV(s);
  987. const int next = NEXT1D(s);
  988. const float d = s - prev;
  989. const float p = lut1d->lut[idx][prev];
  990. const float n = lut1d->lut[idx][next];
  991. return lerpf(p, n, d);
  992. }
  993. static inline float interp_1d_cosine(const LUT1DContext *lut1d,
  994. int idx, const float s)
  995. {
  996. const int prev = PREV(s);
  997. const int next = NEXT1D(s);
  998. const float d = s - prev;
  999. const float p = lut1d->lut[idx][prev];
  1000. const float n = lut1d->lut[idx][next];
  1001. const float m = (1.f - cosf(d * M_PI)) * .5f;
  1002. return lerpf(p, n, m);
  1003. }
  1004. static inline float interp_1d_cubic(const LUT1DContext *lut1d,
  1005. int idx, const float s)
  1006. {
  1007. const int prev = PREV(s);
  1008. const int next = NEXT1D(s);
  1009. const float mu = s - prev;
  1010. float a0, a1, a2, a3, mu2;
  1011. float y0 = lut1d->lut[idx][FFMAX(prev - 1, 0)];
  1012. float y1 = lut1d->lut[idx][prev];
  1013. float y2 = lut1d->lut[idx][next];
  1014. float y3 = lut1d->lut[idx][FFMIN(next + 1, lut1d->lutsize - 1)];
  1015. mu2 = mu * mu;
  1016. a0 = y3 - y2 - y0 + y1;
  1017. a1 = y0 - y1 - a0;
  1018. a2 = y2 - y0;
  1019. a3 = y1;
  1020. return a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3;
  1021. }
  1022. static inline float interp_1d_spline(const LUT1DContext *lut1d,
  1023. int idx, const float s)
  1024. {
  1025. const int prev = PREV(s);
  1026. const int next = NEXT1D(s);
  1027. const float x = s - prev;
  1028. float c0, c1, c2, c3;
  1029. float y0 = lut1d->lut[idx][FFMAX(prev - 1, 0)];
  1030. float y1 = lut1d->lut[idx][prev];
  1031. float y2 = lut1d->lut[idx][next];
  1032. float y3 = lut1d->lut[idx][FFMIN(next + 1, lut1d->lutsize - 1)];
  1033. c0 = y1;
  1034. c1 = .5f * (y2 - y0);
  1035. c2 = y0 - 2.5f * y1 + 2.f * y2 - .5f * y3;
  1036. c3 = .5f * (y3 - y0) + 1.5f * (y1 - y2);
  1037. return ((c3 * x + c2) * x + c1) * x + c0;
  1038. }
  1039. #define DEFINE_INTERP_FUNC_PLANAR_1D(name, nbits, depth) \
  1040. static int interp_1d_##nbits##_##name##_p##depth(AVFilterContext *ctx, \
  1041. void *arg, int jobnr, \
  1042. int nb_jobs) \
  1043. { \
  1044. int x, y; \
  1045. const LUT1DContext *lut1d = ctx->priv; \
  1046. const ThreadData *td = arg; \
  1047. const AVFrame *in = td->in; \
  1048. const AVFrame *out = td->out; \
  1049. const int direct = out == in; \
  1050. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  1051. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  1052. uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
  1053. uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
  1054. uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
  1055. uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
  1056. const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
  1057. const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
  1058. const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
  1059. const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
  1060. const float factor = (1 << depth) - 1; \
  1061. const float scale = (1. / factor) * (lut1d->lutsize - 1); \
  1062. \
  1063. for (y = slice_start; y < slice_end; y++) { \
  1064. uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
  1065. uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
  1066. uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
  1067. uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
  1068. const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
  1069. const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
  1070. const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
  1071. const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
  1072. for (x = 0; x < in->width; x++) { \
  1073. float r = srcr[x] * scale; \
  1074. float g = srcg[x] * scale; \
  1075. float b = srcb[x] * scale; \
  1076. r = interp_1d_##name(lut1d, 0, r); \
  1077. g = interp_1d_##name(lut1d, 1, g); \
  1078. b = interp_1d_##name(lut1d, 2, b); \
  1079. dstr[x] = av_clip_uintp2(r * factor, depth); \
  1080. dstg[x] = av_clip_uintp2(g * factor, depth); \
  1081. dstb[x] = av_clip_uintp2(b * factor, depth); \
  1082. if (!direct && in->linesize[3]) \
  1083. dsta[x] = srca[x]; \
  1084. } \
  1085. grow += out->linesize[0]; \
  1086. brow += out->linesize[1]; \
  1087. rrow += out->linesize[2]; \
  1088. arow += out->linesize[3]; \
  1089. srcgrow += in->linesize[0]; \
  1090. srcbrow += in->linesize[1]; \
  1091. srcrrow += in->linesize[2]; \
  1092. srcarow += in->linesize[3]; \
  1093. } \
  1094. return 0; \
  1095. }
  1096. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 8, 8)
  1097. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 8, 8)
  1098. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 8, 8)
  1099. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 8, 8)
  1100. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 8, 8)
  1101. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 9)
  1102. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 9)
  1103. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 9)
  1104. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 9)
  1105. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 9)
  1106. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 10)
  1107. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 10)
  1108. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 10)
  1109. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 10)
  1110. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 10)
  1111. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 12)
  1112. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 12)
  1113. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 12)
  1114. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 12)
  1115. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 12)
  1116. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 14)
  1117. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 14)
  1118. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 14)
  1119. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 14)
  1120. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 14)
  1121. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 16)
  1122. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 16)
  1123. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 16)
  1124. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 16)
  1125. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 16)
  1126. #define DEFINE_INTERP_FUNC_1D(name, nbits) \
  1127. static int interp_1d_##nbits##_##name(AVFilterContext *ctx, void *arg, \
  1128. int jobnr, int nb_jobs) \
  1129. { \
  1130. int x, y; \
  1131. const LUT1DContext *lut1d = ctx->priv; \
  1132. const ThreadData *td = arg; \
  1133. const AVFrame *in = td->in; \
  1134. const AVFrame *out = td->out; \
  1135. const int direct = out == in; \
  1136. const int step = lut1d->step; \
  1137. const uint8_t r = lut1d->rgba_map[R]; \
  1138. const uint8_t g = lut1d->rgba_map[G]; \
  1139. const uint8_t b = lut1d->rgba_map[B]; \
  1140. const uint8_t a = lut1d->rgba_map[A]; \
  1141. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  1142. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  1143. uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
  1144. const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
  1145. const float factor = (1 << nbits) - 1; \
  1146. const float scale = (1. / factor) * (lut1d->lutsize - 1); \
  1147. \
  1148. for (y = slice_start; y < slice_end; y++) { \
  1149. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  1150. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  1151. for (x = 0; x < in->width * step; x += step) { \
  1152. float rr = src[x + r] * scale; \
  1153. float gg = src[x + g] * scale; \
  1154. float bb = src[x + b] * scale; \
  1155. rr = interp_1d_##name(lut1d, 0, rr); \
  1156. gg = interp_1d_##name(lut1d, 1, gg); \
  1157. bb = interp_1d_##name(lut1d, 2, bb); \
  1158. dst[x + r] = av_clip_uint##nbits(rr * factor); \
  1159. dst[x + g] = av_clip_uint##nbits(gg * factor); \
  1160. dst[x + b] = av_clip_uint##nbits(bb * factor); \
  1161. if (!direct && step == 4) \
  1162. dst[x + a] = src[x + a]; \
  1163. } \
  1164. dstrow += out->linesize[0]; \
  1165. srcrow += in ->linesize[0]; \
  1166. } \
  1167. return 0; \
  1168. }
  1169. DEFINE_INTERP_FUNC_1D(nearest, 8)
  1170. DEFINE_INTERP_FUNC_1D(linear, 8)
  1171. DEFINE_INTERP_FUNC_1D(cosine, 8)
  1172. DEFINE_INTERP_FUNC_1D(cubic, 8)
  1173. DEFINE_INTERP_FUNC_1D(spline, 8)
  1174. DEFINE_INTERP_FUNC_1D(nearest, 16)
  1175. DEFINE_INTERP_FUNC_1D(linear, 16)
  1176. DEFINE_INTERP_FUNC_1D(cosine, 16)
  1177. DEFINE_INTERP_FUNC_1D(cubic, 16)
  1178. DEFINE_INTERP_FUNC_1D(spline, 16)
  1179. static int config_input_1d(AVFilterLink *inlink)
  1180. {
  1181. int depth, is16bit = 0, planar = 0;
  1182. LUT1DContext *lut1d = inlink->dst->priv;
  1183. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  1184. depth = desc->comp[0].depth;
  1185. switch (inlink->format) {
  1186. case AV_PIX_FMT_RGB48:
  1187. case AV_PIX_FMT_BGR48:
  1188. case AV_PIX_FMT_RGBA64:
  1189. case AV_PIX_FMT_BGRA64:
  1190. is16bit = 1;
  1191. break;
  1192. case AV_PIX_FMT_GBRP9:
  1193. case AV_PIX_FMT_GBRP10:
  1194. case AV_PIX_FMT_GBRP12:
  1195. case AV_PIX_FMT_GBRP14:
  1196. case AV_PIX_FMT_GBRP16:
  1197. case AV_PIX_FMT_GBRAP10:
  1198. case AV_PIX_FMT_GBRAP12:
  1199. case AV_PIX_FMT_GBRAP16:
  1200. is16bit = 1;
  1201. case AV_PIX_FMT_GBRP:
  1202. case AV_PIX_FMT_GBRAP:
  1203. planar = 1;
  1204. break;
  1205. }
  1206. ff_fill_rgba_map(lut1d->rgba_map, inlink->format);
  1207. lut1d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  1208. #define SET_FUNC_1D(name) do { \
  1209. if (planar) { \
  1210. switch (depth) { \
  1211. case 8: lut1d->interp = interp_1d_8_##name##_p8; break; \
  1212. case 9: lut1d->interp = interp_1d_16_##name##_p9; break; \
  1213. case 10: lut1d->interp = interp_1d_16_##name##_p10; break; \
  1214. case 12: lut1d->interp = interp_1d_16_##name##_p12; break; \
  1215. case 14: lut1d->interp = interp_1d_16_##name##_p14; break; \
  1216. case 16: lut1d->interp = interp_1d_16_##name##_p16; break; \
  1217. } \
  1218. } else if (is16bit) { lut1d->interp = interp_1d_16_##name; \
  1219. } else { lut1d->interp = interp_1d_8_##name; } \
  1220. } while (0)
  1221. switch (lut1d->interpolation) {
  1222. case INTERPOLATE_1D_NEAREST: SET_FUNC_1D(nearest); break;
  1223. case INTERPOLATE_1D_LINEAR: SET_FUNC_1D(linear); break;
  1224. case INTERPOLATE_1D_COSINE: SET_FUNC_1D(cosine); break;
  1225. case INTERPOLATE_1D_CUBIC: SET_FUNC_1D(cubic); break;
  1226. case INTERPOLATE_1D_SPLINE: SET_FUNC_1D(spline); break;
  1227. default:
  1228. av_assert0(0);
  1229. }
  1230. return 0;
  1231. }
  1232. static av_cold int lut1d_init(AVFilterContext *ctx)
  1233. {
  1234. int ret;
  1235. FILE *f;
  1236. const char *ext;
  1237. LUT1DContext *lut1d = ctx->priv;
  1238. if (!lut1d->file) {
  1239. set_identity_matrix_1d(lut1d, 32);
  1240. return 0;
  1241. }
  1242. f = fopen(lut1d->file, "r");
  1243. if (!f) {
  1244. ret = AVERROR(errno);
  1245. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut1d->file, av_err2str(ret));
  1246. return ret;
  1247. }
  1248. ext = strrchr(lut1d->file, '.');
  1249. if (!ext) {
  1250. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  1251. ret = AVERROR_INVALIDDATA;
  1252. goto end;
  1253. }
  1254. ext++;
  1255. if (!av_strcasecmp(ext, "cube") || !av_strcasecmp(ext, "1dlut")) {
  1256. ret = parse_cube_1d(ctx, f);
  1257. } else {
  1258. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  1259. ret = AVERROR(EINVAL);
  1260. }
  1261. if (!ret && !lut1d->lutsize) {
  1262. av_log(ctx, AV_LOG_ERROR, "1D LUT is empty\n");
  1263. ret = AVERROR_INVALIDDATA;
  1264. }
  1265. end:
  1266. fclose(f);
  1267. return ret;
  1268. }
  1269. static AVFrame *apply_1d_lut(AVFilterLink *inlink, AVFrame *in)
  1270. {
  1271. AVFilterContext *ctx = inlink->dst;
  1272. LUT1DContext *lut1d = ctx->priv;
  1273. AVFilterLink *outlink = inlink->dst->outputs[0];
  1274. AVFrame *out;
  1275. ThreadData td;
  1276. if (av_frame_is_writable(in)) {
  1277. out = in;
  1278. } else {
  1279. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  1280. if (!out) {
  1281. av_frame_free(&in);
  1282. return NULL;
  1283. }
  1284. av_frame_copy_props(out, in);
  1285. }
  1286. td.in = in;
  1287. td.out = out;
  1288. ctx->internal->execute(ctx, lut1d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  1289. if (out != in)
  1290. av_frame_free(&in);
  1291. return out;
  1292. }
  1293. static int filter_frame_1d(AVFilterLink *inlink, AVFrame *in)
  1294. {
  1295. AVFilterLink *outlink = inlink->dst->outputs[0];
  1296. AVFrame *out = apply_1d_lut(inlink, in);
  1297. if (!out)
  1298. return AVERROR(ENOMEM);
  1299. return ff_filter_frame(outlink, out);
  1300. }
  1301. static const AVFilterPad lut1d_inputs[] = {
  1302. {
  1303. .name = "default",
  1304. .type = AVMEDIA_TYPE_VIDEO,
  1305. .filter_frame = filter_frame_1d,
  1306. .config_props = config_input_1d,
  1307. },
  1308. { NULL }
  1309. };
  1310. static const AVFilterPad lut1d_outputs[] = {
  1311. {
  1312. .name = "default",
  1313. .type = AVMEDIA_TYPE_VIDEO,
  1314. },
  1315. { NULL }
  1316. };
  1317. AVFilter ff_vf_lut1d = {
  1318. .name = "lut1d",
  1319. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 1D LUT."),
  1320. .priv_size = sizeof(LUT1DContext),
  1321. .init = lut1d_init,
  1322. .query_formats = query_formats,
  1323. .inputs = lut1d_inputs,
  1324. .outputs = lut1d_outputs,
  1325. .priv_class = &lut1d_class,
  1326. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  1327. };
  1328. #endif