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.

932 lines
37KB

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