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.

978 lines
40KB

  1. /*
  2. * Copyright (c) 2013 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * 3D Lookup table filter
  23. */
  24. #include "libavutil/opt.h"
  25. #include "libavutil/file.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/avstring.h"
  30. #include "avfilter.h"
  31. #include "drawutils.h"
  32. #include "formats.h"
  33. #include "framesync.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. #define R 0
  37. #define G 1
  38. #define B 2
  39. #define A 3
  40. enum interp_mode {
  41. INTERPOLATE_NEAREST,
  42. INTERPOLATE_TRILINEAR,
  43. INTERPOLATE_TETRAHEDRAL,
  44. NB_INTERP_MODE
  45. };
  46. struct rgbvec {
  47. float r, g, b;
  48. };
  49. /* 3D LUT don't often go up to level 32, but it is common to have a Hald CLUT
  50. * of 512x512 (64x64x64) */
  51. #define MAX_LEVEL 64
  52. typedef struct LUT3DContext {
  53. const AVClass *class;
  54. int interpolation; ///<interp_mode
  55. char *file;
  56. uint8_t rgba_map[4];
  57. int step;
  58. avfilter_action_func *interp;
  59. struct rgbvec lut[MAX_LEVEL][MAX_LEVEL][MAX_LEVEL];
  60. int lutsize;
  61. #if CONFIG_HALDCLUT_FILTER
  62. uint8_t clut_rgba_map[4];
  63. int clut_step;
  64. int clut_bits;
  65. int clut_planar;
  66. int clut_width;
  67. FFFrameSync fs;
  68. #endif
  69. } LUT3DContext;
  70. typedef struct ThreadData {
  71. AVFrame *in, *out;
  72. } ThreadData;
  73. #define OFFSET(x) offsetof(LUT3DContext, x)
  74. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  75. #define COMMON_OPTIONS \
  76. { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, FLAGS, "interp_mode" }, \
  77. { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
  78. { "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" }, \
  79. { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
  80. { NULL }
  81. static inline float lerpf(float v0, float v1, float f)
  82. {
  83. return v0 + (v1 - v0) * f;
  84. }
  85. static inline struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
  86. {
  87. struct rgbvec v = {
  88. lerpf(v0->r, v1->r, f), lerpf(v0->g, v1->g, f), lerpf(v0->b, v1->b, f)
  89. };
  90. return v;
  91. }
  92. #define NEAR(x) ((int)((x) + .5))
  93. #define PREV(x) ((int)(x))
  94. #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
  95. /**
  96. * Get the nearest defined point
  97. */
  98. static inline struct rgbvec interp_nearest(const LUT3DContext *lut3d,
  99. const struct rgbvec *s)
  100. {
  101. return lut3d->lut[NEAR(s->r)][NEAR(s->g)][NEAR(s->b)];
  102. }
  103. /**
  104. * Interpolate using the 8 vertices of a cube
  105. * @see https://en.wikipedia.org/wiki/Trilinear_interpolation
  106. */
  107. static inline struct rgbvec interp_trilinear(const LUT3DContext *lut3d,
  108. const struct rgbvec *s)
  109. {
  110. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  111. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  112. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  113. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  114. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  115. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  116. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  117. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  118. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  119. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  120. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  121. const struct rgbvec c00 = lerp(&c000, &c100, d.r);
  122. const struct rgbvec c10 = lerp(&c010, &c110, d.r);
  123. const struct rgbvec c01 = lerp(&c001, &c101, d.r);
  124. const struct rgbvec c11 = lerp(&c011, &c111, d.r);
  125. const struct rgbvec c0 = lerp(&c00, &c10, d.g);
  126. const struct rgbvec c1 = lerp(&c01, &c11, d.g);
  127. const struct rgbvec c = lerp(&c0, &c1, d.b);
  128. return c;
  129. }
  130. /**
  131. * Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
  132. * @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
  133. */
  134. static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
  135. const struct rgbvec *s)
  136. {
  137. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  138. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  139. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  140. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  141. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  142. struct rgbvec c;
  143. if (d.r > d.g) {
  144. if (d.g > d.b) {
  145. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  146. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  147. c.r = (1-d.r) * c000.r + (d.r-d.g) * c100.r + (d.g-d.b) * c110.r + (d.b) * c111.r;
  148. c.g = (1-d.r) * c000.g + (d.r-d.g) * c100.g + (d.g-d.b) * c110.g + (d.b) * c111.g;
  149. c.b = (1-d.r) * c000.b + (d.r-d.g) * c100.b + (d.g-d.b) * c110.b + (d.b) * c111.b;
  150. } else if (d.r > d.b) {
  151. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  152. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  153. c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
  154. c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
  155. c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
  156. } else {
  157. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  158. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  159. c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
  160. c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
  161. c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
  162. }
  163. } else {
  164. if (d.b > d.g) {
  165. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  166. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  167. c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
  168. c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
  169. c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
  170. } else if (d.b > d.r) {
  171. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  172. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  173. c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
  174. c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
  175. c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
  176. } else {
  177. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  178. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  179. c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
  180. c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
  181. c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
  182. }
  183. }
  184. return c;
  185. }
  186. #define DEFINE_INTERP_FUNC_PLANAR(name, nbits, depth) \
  187. static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  188. { \
  189. int x, y; \
  190. const LUT3DContext *lut3d = ctx->priv; \
  191. const ThreadData *td = arg; \
  192. const AVFrame *in = td->in; \
  193. const AVFrame *out = td->out; \
  194. const int direct = out == in; \
  195. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  196. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  197. uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
  198. uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
  199. uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
  200. uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
  201. const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
  202. const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
  203. const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
  204. const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
  205. const float scale = (1. / ((1<<depth) - 1)) * (lut3d->lutsize - 1); \
  206. \
  207. for (y = slice_start; y < slice_end; y++) { \
  208. uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
  209. uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
  210. uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
  211. uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
  212. const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
  213. const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
  214. const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
  215. const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
  216. for (x = 0; x < in->width; x++) { \
  217. const struct rgbvec scaled_rgb = {srcr[x] * scale, \
  218. srcg[x] * scale, \
  219. srcb[x] * scale}; \
  220. struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
  221. dstr[x] = av_clip_uintp2(vec.r * (float)((1<<depth) - 1), depth); \
  222. dstg[x] = av_clip_uintp2(vec.g * (float)((1<<depth) - 1), depth); \
  223. dstb[x] = av_clip_uintp2(vec.b * (float)((1<<depth) - 1), depth); \
  224. if (!direct && in->linesize[3]) \
  225. dsta[x] = srca[x]; \
  226. } \
  227. grow += out->linesize[0]; \
  228. brow += out->linesize[1]; \
  229. rrow += out->linesize[2]; \
  230. arow += out->linesize[3]; \
  231. srcgrow += in->linesize[0]; \
  232. srcbrow += in->linesize[1]; \
  233. srcrrow += in->linesize[2]; \
  234. srcarow += in->linesize[3]; \
  235. } \
  236. return 0; \
  237. }
  238. DEFINE_INTERP_FUNC_PLANAR(nearest, 8, 8)
  239. DEFINE_INTERP_FUNC_PLANAR(trilinear, 8, 8)
  240. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 8, 8)
  241. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 9)
  242. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 9)
  243. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 9)
  244. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 10)
  245. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 10)
  246. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 10)
  247. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 12)
  248. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 12)
  249. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 12)
  250. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 14)
  251. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 14)
  252. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 14)
  253. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 16)
  254. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 16)
  255. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 16)
  256. #define DEFINE_INTERP_FUNC(name, nbits) \
  257. static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  258. { \
  259. int x, y; \
  260. const LUT3DContext *lut3d = ctx->priv; \
  261. const ThreadData *td = arg; \
  262. const AVFrame *in = td->in; \
  263. const AVFrame *out = td->out; \
  264. const int direct = out == in; \
  265. const int step = lut3d->step; \
  266. const uint8_t r = lut3d->rgba_map[R]; \
  267. const uint8_t g = lut3d->rgba_map[G]; \
  268. const uint8_t b = lut3d->rgba_map[B]; \
  269. const uint8_t a = lut3d->rgba_map[A]; \
  270. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  271. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  272. uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
  273. const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
  274. const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
  275. \
  276. for (y = slice_start; y < slice_end; y++) { \
  277. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  278. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  279. for (x = 0; x < in->width * step; x += step) { \
  280. const struct rgbvec scaled_rgb = {src[x + r] * scale, \
  281. src[x + g] * scale, \
  282. src[x + b] * scale}; \
  283. struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
  284. dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
  285. dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
  286. dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
  287. if (!direct && step == 4) \
  288. dst[x + a] = src[x + a]; \
  289. } \
  290. dstrow += out->linesize[0]; \
  291. srcrow += in ->linesize[0]; \
  292. } \
  293. return 0; \
  294. }
  295. DEFINE_INTERP_FUNC(nearest, 8)
  296. DEFINE_INTERP_FUNC(trilinear, 8)
  297. DEFINE_INTERP_FUNC(tetrahedral, 8)
  298. DEFINE_INTERP_FUNC(nearest, 16)
  299. DEFINE_INTERP_FUNC(trilinear, 16)
  300. DEFINE_INTERP_FUNC(tetrahedral, 16)
  301. #define MAX_LINE_SIZE 512
  302. static int skip_line(const char *p)
  303. {
  304. while (*p && av_isspace(*p))
  305. p++;
  306. return !*p || *p == '#';
  307. }
  308. #define NEXT_LINE(loop_cond) do { \
  309. if (!fgets(line, sizeof(line), f)) { \
  310. av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
  311. return AVERROR_INVALIDDATA; \
  312. } \
  313. } while (loop_cond)
  314. /* Basically r g and b float values on each line, with a facultative 3DLUTSIZE
  315. * directive; seems to be generated by Davinci */
  316. static int parse_dat(AVFilterContext *ctx, FILE *f)
  317. {
  318. LUT3DContext *lut3d = ctx->priv;
  319. char line[MAX_LINE_SIZE];
  320. int i, j, k, size;
  321. lut3d->lutsize = size = 33;
  322. NEXT_LINE(skip_line(line));
  323. if (!strncmp(line, "3DLUTSIZE ", 10)) {
  324. size = strtol(line + 10, NULL, 0);
  325. if (size < 2 || size > MAX_LEVEL) {
  326. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  327. return AVERROR(EINVAL);
  328. }
  329. lut3d->lutsize = size;
  330. NEXT_LINE(skip_line(line));
  331. }
  332. for (k = 0; k < size; k++) {
  333. for (j = 0; j < size; j++) {
  334. for (i = 0; i < size; i++) {
  335. struct rgbvec *vec = &lut3d->lut[k][j][i];
  336. if (k != 0 || j != 0 || i != 0)
  337. NEXT_LINE(skip_line(line));
  338. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  339. return AVERROR_INVALIDDATA;
  340. }
  341. }
  342. }
  343. return 0;
  344. }
  345. /* Iridas format */
  346. static int parse_cube(AVFilterContext *ctx, FILE *f)
  347. {
  348. LUT3DContext *lut3d = ctx->priv;
  349. char line[MAX_LINE_SIZE];
  350. float min[3] = {0.0, 0.0, 0.0};
  351. float max[3] = {1.0, 1.0, 1.0};
  352. while (fgets(line, sizeof(line), f)) {
  353. if (!strncmp(line, "LUT_3D_SIZE ", 12)) {
  354. int i, j, k;
  355. const int size = strtol(line + 12, NULL, 0);
  356. if (size < 2 || size > MAX_LEVEL) {
  357. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  358. return AVERROR(EINVAL);
  359. }
  360. lut3d->lutsize = size;
  361. for (k = 0; k < size; k++) {
  362. for (j = 0; j < size; j++) {
  363. for (i = 0; i < size; i++) {
  364. struct rgbvec *vec = &lut3d->lut[i][j][k];
  365. do {
  366. try_again:
  367. NEXT_LINE(0);
  368. if (!strncmp(line, "DOMAIN_", 7)) {
  369. float *vals = NULL;
  370. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  371. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  372. if (!vals)
  373. return AVERROR_INVALIDDATA;
  374. sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  375. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  376. min[0], min[1], min[2], max[0], max[1], max[2]);
  377. goto try_again;
  378. }
  379. } while (skip_line(line));
  380. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  381. return AVERROR_INVALIDDATA;
  382. vec->r *= max[0] - min[0];
  383. vec->g *= max[1] - min[1];
  384. vec->b *= max[2] - min[2];
  385. }
  386. }
  387. }
  388. break;
  389. }
  390. }
  391. return 0;
  392. }
  393. /* Assume 17x17x17 LUT with a 16-bit depth
  394. * FIXME: it seems there are various 3dl formats */
  395. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  396. {
  397. char line[MAX_LINE_SIZE];
  398. LUT3DContext *lut3d = ctx->priv;
  399. int i, j, k;
  400. const int size = 17;
  401. const float scale = 16*16*16;
  402. lut3d->lutsize = size;
  403. NEXT_LINE(skip_line(line));
  404. for (k = 0; k < size; k++) {
  405. for (j = 0; j < size; j++) {
  406. for (i = 0; i < size; i++) {
  407. int r, g, b;
  408. struct rgbvec *vec = &lut3d->lut[k][j][i];
  409. NEXT_LINE(skip_line(line));
  410. if (sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  411. return AVERROR_INVALIDDATA;
  412. vec->r = r / scale;
  413. vec->g = g / scale;
  414. vec->b = b / scale;
  415. }
  416. }
  417. }
  418. return 0;
  419. }
  420. /* Pandora format */
  421. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  422. {
  423. LUT3DContext *lut3d = ctx->priv;
  424. float scale;
  425. int i, j, k, size, in = -1, out = -1;
  426. char line[MAX_LINE_SIZE];
  427. uint8_t rgb_map[3] = {0, 1, 2};
  428. while (fgets(line, sizeof(line), f)) {
  429. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  430. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  431. else if (!strncmp(line, "values", 6)) {
  432. const char *p = line + 6;
  433. #define SET_COLOR(id) do { \
  434. while (av_isspace(*p)) \
  435. p++; \
  436. switch (*p) { \
  437. case 'r': rgb_map[id] = 0; break; \
  438. case 'g': rgb_map[id] = 1; break; \
  439. case 'b': rgb_map[id] = 2; break; \
  440. } \
  441. while (*p && !av_isspace(*p)) \
  442. p++; \
  443. } while (0)
  444. SET_COLOR(0);
  445. SET_COLOR(1);
  446. SET_COLOR(2);
  447. break;
  448. }
  449. }
  450. if (in == -1 || out == -1) {
  451. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  452. return AVERROR_INVALIDDATA;
  453. }
  454. if (in < 2 || out < 2 ||
  455. in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
  456. out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
  457. av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
  458. return AVERROR_INVALIDDATA;
  459. }
  460. for (size = 1; size*size*size < in; size++);
  461. lut3d->lutsize = size;
  462. scale = 1. / (out - 1);
  463. for (k = 0; k < size; k++) {
  464. for (j = 0; j < size; j++) {
  465. for (i = 0; i < size; i++) {
  466. struct rgbvec *vec = &lut3d->lut[k][j][i];
  467. float val[3];
  468. NEXT_LINE(0);
  469. if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  470. return AVERROR_INVALIDDATA;
  471. vec->r = val[rgb_map[0]] * scale;
  472. vec->g = val[rgb_map[1]] * scale;
  473. vec->b = val[rgb_map[2]] * scale;
  474. }
  475. }
  476. }
  477. return 0;
  478. }
  479. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  480. {
  481. int i, j, k;
  482. const float c = 1. / (size - 1);
  483. lut3d->lutsize = size;
  484. for (k = 0; k < size; k++) {
  485. for (j = 0; j < size; j++) {
  486. for (i = 0; i < size; i++) {
  487. struct rgbvec *vec = &lut3d->lut[k][j][i];
  488. vec->r = k * c;
  489. vec->g = j * c;
  490. vec->b = i * c;
  491. }
  492. }
  493. }
  494. }
  495. static int query_formats(AVFilterContext *ctx)
  496. {
  497. static const enum AVPixelFormat pix_fmts[] = {
  498. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  499. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  500. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  501. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  502. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  503. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  504. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  505. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  506. AV_PIX_FMT_GBRP9,
  507. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
  508. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  509. AV_PIX_FMT_GBRP14,
  510. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  511. AV_PIX_FMT_NONE
  512. };
  513. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  514. if (!fmts_list)
  515. return AVERROR(ENOMEM);
  516. return ff_set_common_formats(ctx, fmts_list);
  517. }
  518. static int config_input(AVFilterLink *inlink)
  519. {
  520. int depth, is16bit = 0, planar = 0;
  521. LUT3DContext *lut3d = inlink->dst->priv;
  522. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  523. depth = desc->comp[0].depth;
  524. switch (inlink->format) {
  525. case AV_PIX_FMT_RGB48:
  526. case AV_PIX_FMT_BGR48:
  527. case AV_PIX_FMT_RGBA64:
  528. case AV_PIX_FMT_BGRA64:
  529. is16bit = 1;
  530. break;
  531. case AV_PIX_FMT_GBRP9:
  532. case AV_PIX_FMT_GBRP10:
  533. case AV_PIX_FMT_GBRP12:
  534. case AV_PIX_FMT_GBRP14:
  535. case AV_PIX_FMT_GBRP16:
  536. case AV_PIX_FMT_GBRAP10:
  537. case AV_PIX_FMT_GBRAP12:
  538. case AV_PIX_FMT_GBRAP16:
  539. is16bit = 1;
  540. case AV_PIX_FMT_GBRP:
  541. case AV_PIX_FMT_GBRAP:
  542. planar = 1;
  543. break;
  544. }
  545. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  546. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  547. #define SET_FUNC(name) do { \
  548. if (planar) { \
  549. switch (depth) { \
  550. case 8: lut3d->interp = interp_8_##name##_p8; break; \
  551. case 9: lut3d->interp = interp_16_##name##_p9; break; \
  552. case 10: lut3d->interp = interp_16_##name##_p10; break; \
  553. case 12: lut3d->interp = interp_16_##name##_p12; break; \
  554. case 14: lut3d->interp = interp_16_##name##_p14; break; \
  555. case 16: lut3d->interp = interp_16_##name##_p16; break; \
  556. } \
  557. } else if (is16bit) { lut3d->interp = interp_16_##name; \
  558. } else { lut3d->interp = interp_8_##name; } \
  559. } while (0)
  560. switch (lut3d->interpolation) {
  561. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  562. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  563. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  564. default:
  565. av_assert0(0);
  566. }
  567. return 0;
  568. }
  569. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  570. {
  571. AVFilterContext *ctx = inlink->dst;
  572. LUT3DContext *lut3d = ctx->priv;
  573. AVFilterLink *outlink = inlink->dst->outputs[0];
  574. AVFrame *out;
  575. ThreadData td;
  576. if (av_frame_is_writable(in)) {
  577. out = in;
  578. } else {
  579. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  580. if (!out) {
  581. av_frame_free(&in);
  582. return NULL;
  583. }
  584. av_frame_copy_props(out, in);
  585. }
  586. td.in = in;
  587. td.out = out;
  588. ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  589. if (out != in)
  590. av_frame_free(&in);
  591. return out;
  592. }
  593. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  594. {
  595. AVFilterLink *outlink = inlink->dst->outputs[0];
  596. AVFrame *out = apply_lut(inlink, in);
  597. if (!out)
  598. return AVERROR(ENOMEM);
  599. return ff_filter_frame(outlink, out);
  600. }
  601. #if CONFIG_LUT3D_FILTER
  602. static const AVOption lut3d_options[] = {
  603. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  604. COMMON_OPTIONS
  605. };
  606. AVFILTER_DEFINE_CLASS(lut3d);
  607. static av_cold int lut3d_init(AVFilterContext *ctx)
  608. {
  609. int ret;
  610. FILE *f;
  611. const char *ext;
  612. LUT3DContext *lut3d = ctx->priv;
  613. if (!lut3d->file) {
  614. set_identity_matrix(lut3d, 32);
  615. return 0;
  616. }
  617. f = fopen(lut3d->file, "r");
  618. if (!f) {
  619. ret = AVERROR(errno);
  620. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  621. return ret;
  622. }
  623. ext = strrchr(lut3d->file, '.');
  624. if (!ext) {
  625. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  626. ret = AVERROR_INVALIDDATA;
  627. goto end;
  628. }
  629. ext++;
  630. if (!av_strcasecmp(ext, "dat")) {
  631. ret = parse_dat(ctx, f);
  632. } else if (!av_strcasecmp(ext, "3dl")) {
  633. ret = parse_3dl(ctx, f);
  634. } else if (!av_strcasecmp(ext, "cube")) {
  635. ret = parse_cube(ctx, f);
  636. } else if (!av_strcasecmp(ext, "m3d")) {
  637. ret = parse_m3d(ctx, f);
  638. } else {
  639. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  640. ret = AVERROR(EINVAL);
  641. }
  642. if (!ret && !lut3d->lutsize) {
  643. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  644. ret = AVERROR_INVALIDDATA;
  645. }
  646. end:
  647. fclose(f);
  648. return ret;
  649. }
  650. static const AVFilterPad lut3d_inputs[] = {
  651. {
  652. .name = "default",
  653. .type = AVMEDIA_TYPE_VIDEO,
  654. .filter_frame = filter_frame,
  655. .config_props = config_input,
  656. },
  657. { NULL }
  658. };
  659. static const AVFilterPad lut3d_outputs[] = {
  660. {
  661. .name = "default",
  662. .type = AVMEDIA_TYPE_VIDEO,
  663. },
  664. { NULL }
  665. };
  666. AVFilter ff_vf_lut3d = {
  667. .name = "lut3d",
  668. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  669. .priv_size = sizeof(LUT3DContext),
  670. .init = lut3d_init,
  671. .query_formats = query_formats,
  672. .inputs = lut3d_inputs,
  673. .outputs = lut3d_outputs,
  674. .priv_class = &lut3d_class,
  675. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  676. };
  677. #endif
  678. #if CONFIG_HALDCLUT_FILTER
  679. static void update_clut_packed(LUT3DContext *lut3d, const AVFrame *frame)
  680. {
  681. const uint8_t *data = frame->data[0];
  682. const int linesize = frame->linesize[0];
  683. const int w = lut3d->clut_width;
  684. const int step = lut3d->clut_step;
  685. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  686. const int level = lut3d->lutsize;
  687. #define LOAD_CLUT(nbits) do { \
  688. int i, j, k, x = 0, y = 0; \
  689. \
  690. for (k = 0; k < level; k++) { \
  691. for (j = 0; j < level; j++) { \
  692. for (i = 0; i < level; i++) { \
  693. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  694. (data + y*linesize + x*step); \
  695. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  696. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  697. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  698. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  699. if (++x == w) { \
  700. x = 0; \
  701. y++; \
  702. } \
  703. } \
  704. } \
  705. } \
  706. } while (0)
  707. switch (lut3d->clut_bits) {
  708. case 8: LOAD_CLUT(8); break;
  709. case 16: LOAD_CLUT(16); break;
  710. }
  711. }
  712. static void update_clut_planar(LUT3DContext *lut3d, const AVFrame *frame)
  713. {
  714. const uint8_t *datag = frame->data[0];
  715. const uint8_t *datab = frame->data[1];
  716. const uint8_t *datar = frame->data[2];
  717. const int glinesize = frame->linesize[0];
  718. const int blinesize = frame->linesize[1];
  719. const int rlinesize = frame->linesize[2];
  720. const int w = lut3d->clut_width;
  721. const int level = lut3d->lutsize;
  722. #define LOAD_CLUT_PLANAR(nbits, depth) do { \
  723. int i, j, k, x = 0, y = 0; \
  724. \
  725. for (k = 0; k < level; k++) { \
  726. for (j = 0; j < level; j++) { \
  727. for (i = 0; i < level; i++) { \
  728. const uint##nbits##_t *gsrc = (const uint##nbits##_t *) \
  729. (datag + y*glinesize); \
  730. const uint##nbits##_t *bsrc = (const uint##nbits##_t *) \
  731. (datab + y*blinesize); \
  732. const uint##nbits##_t *rsrc = (const uint##nbits##_t *) \
  733. (datar + y*rlinesize); \
  734. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  735. vec->r = gsrc[x] / (float)((1<<(depth)) - 1); \
  736. vec->g = bsrc[x] / (float)((1<<(depth)) - 1); \
  737. vec->b = rsrc[x] / (float)((1<<(depth)) - 1); \
  738. if (++x == w) { \
  739. x = 0; \
  740. y++; \
  741. } \
  742. } \
  743. } \
  744. } \
  745. } while (0)
  746. switch (lut3d->clut_bits) {
  747. case 8: LOAD_CLUT_PLANAR(8, 8); break;
  748. case 9: LOAD_CLUT_PLANAR(16, 9); break;
  749. case 10: LOAD_CLUT_PLANAR(16, 10); break;
  750. case 12: LOAD_CLUT_PLANAR(16, 12); break;
  751. case 14: LOAD_CLUT_PLANAR(16, 14); break;
  752. case 16: LOAD_CLUT_PLANAR(16, 16); break;
  753. }
  754. }
  755. static int config_output(AVFilterLink *outlink)
  756. {
  757. AVFilterContext *ctx = outlink->src;
  758. LUT3DContext *lut3d = ctx->priv;
  759. int ret;
  760. ret = ff_framesync_init_dualinput(&lut3d->fs, ctx);
  761. if (ret < 0)
  762. return ret;
  763. outlink->w = ctx->inputs[0]->w;
  764. outlink->h = ctx->inputs[0]->h;
  765. outlink->time_base = ctx->inputs[0]->time_base;
  766. if ((ret = ff_framesync_configure(&lut3d->fs)) < 0)
  767. return ret;
  768. return 0;
  769. }
  770. static int activate(AVFilterContext *ctx)
  771. {
  772. LUT3DContext *s = ctx->priv;
  773. return ff_framesync_activate(&s->fs);
  774. }
  775. static int config_clut(AVFilterLink *inlink)
  776. {
  777. int size, level, w, h;
  778. AVFilterContext *ctx = inlink->dst;
  779. LUT3DContext *lut3d = ctx->priv;
  780. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  781. av_assert0(desc);
  782. lut3d->clut_bits = desc->comp[0].depth;
  783. lut3d->clut_planar = av_pix_fmt_count_planes(inlink->format) > 1;
  784. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  785. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  786. if (inlink->w > inlink->h)
  787. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  788. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  789. else if (inlink->w < inlink->h)
  790. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  791. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  792. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  793. for (level = 1; level*level*level < w; level++);
  794. size = level*level*level;
  795. if (size != w) {
  796. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  797. return AVERROR_INVALIDDATA;
  798. }
  799. av_assert0(w == h && w == size);
  800. level *= level;
  801. if (level > MAX_LEVEL) {
  802. const int max_clut_level = sqrt(MAX_LEVEL);
  803. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  804. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  805. "(maximum level is %d, or %dx%d CLUT)\n",
  806. max_clut_level, max_clut_size, max_clut_size);
  807. return AVERROR(EINVAL);
  808. }
  809. lut3d->lutsize = level;
  810. return 0;
  811. }
  812. static int update_apply_clut(FFFrameSync *fs)
  813. {
  814. AVFilterContext *ctx = fs->parent;
  815. LUT3DContext *lut3d = ctx->priv;
  816. AVFilterLink *inlink = ctx->inputs[0];
  817. AVFrame *master, *second, *out;
  818. int ret;
  819. ret = ff_framesync_dualinput_get(fs, &master, &second);
  820. if (ret < 0)
  821. return ret;
  822. if (!second)
  823. return ff_filter_frame(ctx->outputs[0], master);
  824. if (lut3d->clut_planar)
  825. update_clut_planar(ctx->priv, second);
  826. else
  827. update_clut_packed(ctx->priv, second);
  828. out = apply_lut(inlink, master);
  829. return ff_filter_frame(ctx->outputs[0], out);
  830. }
  831. static av_cold int haldclut_init(AVFilterContext *ctx)
  832. {
  833. LUT3DContext *lut3d = ctx->priv;
  834. lut3d->fs.on_event = update_apply_clut;
  835. return 0;
  836. }
  837. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  838. {
  839. LUT3DContext *lut3d = ctx->priv;
  840. ff_framesync_uninit(&lut3d->fs);
  841. }
  842. static const AVOption haldclut_options[] = {
  843. COMMON_OPTIONS
  844. };
  845. FRAMESYNC_DEFINE_CLASS(haldclut, LUT3DContext, fs);
  846. static const AVFilterPad haldclut_inputs[] = {
  847. {
  848. .name = "main",
  849. .type = AVMEDIA_TYPE_VIDEO,
  850. .config_props = config_input,
  851. },{
  852. .name = "clut",
  853. .type = AVMEDIA_TYPE_VIDEO,
  854. .config_props = config_clut,
  855. },
  856. { NULL }
  857. };
  858. static const AVFilterPad haldclut_outputs[] = {
  859. {
  860. .name = "default",
  861. .type = AVMEDIA_TYPE_VIDEO,
  862. .config_props = config_output,
  863. },
  864. { NULL }
  865. };
  866. AVFilter ff_vf_haldclut = {
  867. .name = "haldclut",
  868. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  869. .priv_size = sizeof(LUT3DContext),
  870. .preinit = haldclut_framesync_preinit,
  871. .init = haldclut_init,
  872. .uninit = haldclut_uninit,
  873. .query_formats = query_formats,
  874. .activate = activate,
  875. .inputs = haldclut_inputs,
  876. .outputs = haldclut_outputs,
  877. .priv_class = &haldclut_class,
  878. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  879. };
  880. #endif