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.

824 lines
30KB

  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(name, nbits) \
  186. static int interp_##nbits##_##name(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 step = lut3d->step; \
  195. const uint8_t r = lut3d->rgba_map[R]; \
  196. const uint8_t g = lut3d->rgba_map[G]; \
  197. const uint8_t b = lut3d->rgba_map[B]; \
  198. const uint8_t a = lut3d->rgba_map[A]; \
  199. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  200. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  201. uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
  202. const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
  203. const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
  204. \
  205. for (y = slice_start; y < slice_end; y++) { \
  206. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  207. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  208. for (x = 0; x < in->width * step; x += step) { \
  209. const struct rgbvec scaled_rgb = {src[x + r] * scale, \
  210. src[x + g] * scale, \
  211. src[x + b] * scale}; \
  212. struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
  213. dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
  214. dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
  215. dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
  216. if (!direct && step == 4) \
  217. dst[x + a] = src[x + a]; \
  218. } \
  219. dstrow += out->linesize[0]; \
  220. srcrow += in ->linesize[0]; \
  221. } \
  222. return 0; \
  223. }
  224. DEFINE_INTERP_FUNC(nearest, 8)
  225. DEFINE_INTERP_FUNC(trilinear, 8)
  226. DEFINE_INTERP_FUNC(tetrahedral, 8)
  227. DEFINE_INTERP_FUNC(nearest, 16)
  228. DEFINE_INTERP_FUNC(trilinear, 16)
  229. DEFINE_INTERP_FUNC(tetrahedral, 16)
  230. #define MAX_LINE_SIZE 512
  231. static int skip_line(const char *p)
  232. {
  233. while (*p && av_isspace(*p))
  234. p++;
  235. return !*p || *p == '#';
  236. }
  237. #define NEXT_LINE(loop_cond) do { \
  238. if (!fgets(line, sizeof(line), f)) { \
  239. av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
  240. return AVERROR_INVALIDDATA; \
  241. } \
  242. } while (loop_cond)
  243. /* Basically r g and b float values on each line, with a facultative 3DLUTSIZE
  244. * directive; seems to be generated by Davinci */
  245. static int parse_dat(AVFilterContext *ctx, FILE *f)
  246. {
  247. LUT3DContext *lut3d = ctx->priv;
  248. char line[MAX_LINE_SIZE];
  249. int i, j, k, size;
  250. lut3d->lutsize = size = 33;
  251. NEXT_LINE(skip_line(line));
  252. if (!strncmp(line, "3DLUTSIZE ", 10)) {
  253. size = strtol(line + 10, NULL, 0);
  254. if (size < 2 || size > MAX_LEVEL) {
  255. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  256. return AVERROR(EINVAL);
  257. }
  258. lut3d->lutsize = size;
  259. NEXT_LINE(skip_line(line));
  260. }
  261. for (k = 0; k < size; k++) {
  262. for (j = 0; j < size; j++) {
  263. for (i = 0; i < size; i++) {
  264. struct rgbvec *vec = &lut3d->lut[k][j][i];
  265. if (k != 0 || j != 0 || i != 0)
  266. NEXT_LINE(skip_line(line));
  267. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  268. return AVERROR_INVALIDDATA;
  269. }
  270. }
  271. }
  272. return 0;
  273. }
  274. /* Iridas format */
  275. static int parse_cube(AVFilterContext *ctx, FILE *f)
  276. {
  277. LUT3DContext *lut3d = ctx->priv;
  278. char line[MAX_LINE_SIZE];
  279. float min[3] = {0.0, 0.0, 0.0};
  280. float max[3] = {1.0, 1.0, 1.0};
  281. while (fgets(line, sizeof(line), f)) {
  282. if (!strncmp(line, "LUT_3D_SIZE ", 12)) {
  283. int i, j, k;
  284. const int size = strtol(line + 12, NULL, 0);
  285. if (size < 2 || size > MAX_LEVEL) {
  286. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  287. return AVERROR(EINVAL);
  288. }
  289. lut3d->lutsize = size;
  290. for (k = 0; k < size; k++) {
  291. for (j = 0; j < size; j++) {
  292. for (i = 0; i < size; i++) {
  293. struct rgbvec *vec = &lut3d->lut[i][j][k];
  294. do {
  295. try_again:
  296. NEXT_LINE(0);
  297. if (!strncmp(line, "DOMAIN_", 7)) {
  298. float *vals = NULL;
  299. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  300. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  301. if (!vals)
  302. return AVERROR_INVALIDDATA;
  303. sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  304. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  305. min[0], min[1], min[2], max[0], max[1], max[2]);
  306. goto try_again;
  307. }
  308. } while (skip_line(line));
  309. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  310. return AVERROR_INVALIDDATA;
  311. vec->r *= max[0] - min[0];
  312. vec->g *= max[1] - min[1];
  313. vec->b *= max[2] - min[2];
  314. }
  315. }
  316. }
  317. break;
  318. }
  319. }
  320. return 0;
  321. }
  322. /* Assume 17x17x17 LUT with a 16-bit depth
  323. * FIXME: it seems there are various 3dl formats */
  324. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  325. {
  326. char line[MAX_LINE_SIZE];
  327. LUT3DContext *lut3d = ctx->priv;
  328. int i, j, k;
  329. const int size = 17;
  330. const float scale = 16*16*16;
  331. lut3d->lutsize = size;
  332. NEXT_LINE(skip_line(line));
  333. for (k = 0; k < size; k++) {
  334. for (j = 0; j < size; j++) {
  335. for (i = 0; i < size; i++) {
  336. int r, g, b;
  337. struct rgbvec *vec = &lut3d->lut[k][j][i];
  338. NEXT_LINE(skip_line(line));
  339. if (sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  340. return AVERROR_INVALIDDATA;
  341. vec->r = r / scale;
  342. vec->g = g / scale;
  343. vec->b = b / scale;
  344. }
  345. }
  346. }
  347. return 0;
  348. }
  349. /* Pandora format */
  350. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  351. {
  352. LUT3DContext *lut3d = ctx->priv;
  353. float scale;
  354. int i, j, k, size, in = -1, out = -1;
  355. char line[MAX_LINE_SIZE];
  356. uint8_t rgb_map[3] = {0, 1, 2};
  357. while (fgets(line, sizeof(line), f)) {
  358. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  359. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  360. else if (!strncmp(line, "values", 6)) {
  361. const char *p = line + 6;
  362. #define SET_COLOR(id) do { \
  363. while (av_isspace(*p)) \
  364. p++; \
  365. switch (*p) { \
  366. case 'r': rgb_map[id] = 0; break; \
  367. case 'g': rgb_map[id] = 1; break; \
  368. case 'b': rgb_map[id] = 2; break; \
  369. } \
  370. while (*p && !av_isspace(*p)) \
  371. p++; \
  372. } while (0)
  373. SET_COLOR(0);
  374. SET_COLOR(1);
  375. SET_COLOR(2);
  376. break;
  377. }
  378. }
  379. if (in == -1 || out == -1) {
  380. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  381. return AVERROR_INVALIDDATA;
  382. }
  383. if (in < 2 || out < 2 ||
  384. in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
  385. out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
  386. av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
  387. return AVERROR_INVALIDDATA;
  388. }
  389. for (size = 1; size*size*size < in; size++);
  390. lut3d->lutsize = size;
  391. scale = 1. / (out - 1);
  392. for (k = 0; k < size; k++) {
  393. for (j = 0; j < size; j++) {
  394. for (i = 0; i < size; i++) {
  395. struct rgbvec *vec = &lut3d->lut[k][j][i];
  396. float val[3];
  397. NEXT_LINE(0);
  398. if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  399. return AVERROR_INVALIDDATA;
  400. vec->r = val[rgb_map[0]] * scale;
  401. vec->g = val[rgb_map[1]] * scale;
  402. vec->b = val[rgb_map[2]] * scale;
  403. }
  404. }
  405. }
  406. return 0;
  407. }
  408. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  409. {
  410. int i, j, k;
  411. const float c = 1. / (size - 1);
  412. lut3d->lutsize = size;
  413. for (k = 0; k < size; k++) {
  414. for (j = 0; j < size; j++) {
  415. for (i = 0; i < size; i++) {
  416. struct rgbvec *vec = &lut3d->lut[k][j][i];
  417. vec->r = k * c;
  418. vec->g = j * c;
  419. vec->b = i * c;
  420. }
  421. }
  422. }
  423. }
  424. static int query_formats(AVFilterContext *ctx)
  425. {
  426. static const enum AVPixelFormat pix_fmts[] = {
  427. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  428. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  429. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  430. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  431. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  432. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  433. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  434. AV_PIX_FMT_NONE
  435. };
  436. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  437. if (!fmts_list)
  438. return AVERROR(ENOMEM);
  439. return ff_set_common_formats(ctx, fmts_list);
  440. }
  441. static int config_input(AVFilterLink *inlink)
  442. {
  443. int is16bit = 0;
  444. LUT3DContext *lut3d = inlink->dst->priv;
  445. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  446. switch (inlink->format) {
  447. case AV_PIX_FMT_RGB48:
  448. case AV_PIX_FMT_BGR48:
  449. case AV_PIX_FMT_RGBA64:
  450. case AV_PIX_FMT_BGRA64:
  451. is16bit = 1;
  452. }
  453. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  454. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  455. #define SET_FUNC(name) do { \
  456. if (is16bit) lut3d->interp = interp_16_##name; \
  457. else lut3d->interp = interp_8_##name; \
  458. } while (0)
  459. switch (lut3d->interpolation) {
  460. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  461. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  462. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  463. default:
  464. av_assert0(0);
  465. }
  466. return 0;
  467. }
  468. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  469. {
  470. AVFilterContext *ctx = inlink->dst;
  471. LUT3DContext *lut3d = ctx->priv;
  472. AVFilterLink *outlink = inlink->dst->outputs[0];
  473. AVFrame *out;
  474. ThreadData td;
  475. if (av_frame_is_writable(in)) {
  476. out = in;
  477. } else {
  478. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  479. if (!out) {
  480. av_frame_free(&in);
  481. return NULL;
  482. }
  483. av_frame_copy_props(out, in);
  484. }
  485. td.in = in;
  486. td.out = out;
  487. ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  488. if (out != in)
  489. av_frame_free(&in);
  490. return out;
  491. }
  492. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  493. {
  494. AVFilterLink *outlink = inlink->dst->outputs[0];
  495. AVFrame *out = apply_lut(inlink, in);
  496. if (!out)
  497. return AVERROR(ENOMEM);
  498. return ff_filter_frame(outlink, out);
  499. }
  500. #if CONFIG_LUT3D_FILTER
  501. static const AVOption lut3d_options[] = {
  502. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  503. COMMON_OPTIONS
  504. };
  505. AVFILTER_DEFINE_CLASS(lut3d);
  506. static av_cold int lut3d_init(AVFilterContext *ctx)
  507. {
  508. int ret;
  509. FILE *f;
  510. const char *ext;
  511. LUT3DContext *lut3d = ctx->priv;
  512. if (!lut3d->file) {
  513. set_identity_matrix(lut3d, 32);
  514. return 0;
  515. }
  516. f = fopen(lut3d->file, "r");
  517. if (!f) {
  518. ret = AVERROR(errno);
  519. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  520. return ret;
  521. }
  522. ext = strrchr(lut3d->file, '.');
  523. if (!ext) {
  524. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  525. ret = AVERROR_INVALIDDATA;
  526. goto end;
  527. }
  528. ext++;
  529. if (!av_strcasecmp(ext, "dat")) {
  530. ret = parse_dat(ctx, f);
  531. } else if (!av_strcasecmp(ext, "3dl")) {
  532. ret = parse_3dl(ctx, f);
  533. } else if (!av_strcasecmp(ext, "cube")) {
  534. ret = parse_cube(ctx, f);
  535. } else if (!av_strcasecmp(ext, "m3d")) {
  536. ret = parse_m3d(ctx, f);
  537. } else {
  538. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  539. ret = AVERROR(EINVAL);
  540. }
  541. if (!ret && !lut3d->lutsize) {
  542. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  543. ret = AVERROR_INVALIDDATA;
  544. }
  545. end:
  546. fclose(f);
  547. return ret;
  548. }
  549. static const AVFilterPad lut3d_inputs[] = {
  550. {
  551. .name = "default",
  552. .type = AVMEDIA_TYPE_VIDEO,
  553. .filter_frame = filter_frame,
  554. .config_props = config_input,
  555. },
  556. { NULL }
  557. };
  558. static const AVFilterPad lut3d_outputs[] = {
  559. {
  560. .name = "default",
  561. .type = AVMEDIA_TYPE_VIDEO,
  562. },
  563. { NULL }
  564. };
  565. AVFilter ff_vf_lut3d = {
  566. .name = "lut3d",
  567. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  568. .priv_size = sizeof(LUT3DContext),
  569. .init = lut3d_init,
  570. .query_formats = query_formats,
  571. .inputs = lut3d_inputs,
  572. .outputs = lut3d_outputs,
  573. .priv_class = &lut3d_class,
  574. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  575. };
  576. #endif
  577. #if CONFIG_HALDCLUT_FILTER
  578. static void update_clut(LUT3DContext *lut3d, const AVFrame *frame)
  579. {
  580. const uint8_t *data = frame->data[0];
  581. const int linesize = frame->linesize[0];
  582. const int w = lut3d->clut_width;
  583. const int step = lut3d->clut_step;
  584. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  585. const int level = lut3d->lutsize;
  586. #define LOAD_CLUT(nbits) do { \
  587. int i, j, k, x = 0, y = 0; \
  588. \
  589. for (k = 0; k < level; k++) { \
  590. for (j = 0; j < level; j++) { \
  591. for (i = 0; i < level; i++) { \
  592. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  593. (data + y*linesize + x*step); \
  594. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  595. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  596. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  597. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  598. if (++x == w) { \
  599. x = 0; \
  600. y++; \
  601. } \
  602. } \
  603. } \
  604. } \
  605. } while (0)
  606. if (!lut3d->clut_is16bit) LOAD_CLUT(8);
  607. else LOAD_CLUT(16);
  608. }
  609. static int config_output(AVFilterLink *outlink)
  610. {
  611. AVFilterContext *ctx = outlink->src;
  612. LUT3DContext *lut3d = ctx->priv;
  613. int ret;
  614. ret = ff_framesync_init_dualinput(&lut3d->fs, ctx);
  615. if (ret < 0)
  616. return ret;
  617. outlink->w = ctx->inputs[0]->w;
  618. outlink->h = ctx->inputs[0]->h;
  619. outlink->time_base = ctx->inputs[0]->time_base;
  620. if ((ret = ff_framesync_configure(&lut3d->fs)) < 0)
  621. return ret;
  622. return 0;
  623. }
  624. static int activate(AVFilterContext *ctx)
  625. {
  626. LUT3DContext *s = ctx->priv;
  627. return ff_framesync_activate(&s->fs);
  628. }
  629. static int config_clut(AVFilterLink *inlink)
  630. {
  631. int size, level, w, h;
  632. AVFilterContext *ctx = inlink->dst;
  633. LUT3DContext *lut3d = ctx->priv;
  634. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  635. av_assert0(desc);
  636. lut3d->clut_is16bit = 0;
  637. switch (inlink->format) {
  638. case AV_PIX_FMT_RGB48:
  639. case AV_PIX_FMT_BGR48:
  640. case AV_PIX_FMT_RGBA64:
  641. case AV_PIX_FMT_BGRA64:
  642. lut3d->clut_is16bit = 1;
  643. }
  644. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  645. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  646. if (inlink->w > inlink->h)
  647. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  648. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  649. else if (inlink->w < inlink->h)
  650. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  651. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  652. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  653. for (level = 1; level*level*level < w; level++);
  654. size = level*level*level;
  655. if (size != w) {
  656. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  657. return AVERROR_INVALIDDATA;
  658. }
  659. av_assert0(w == h && w == size);
  660. level *= level;
  661. if (level > MAX_LEVEL) {
  662. const int max_clut_level = sqrt(MAX_LEVEL);
  663. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  664. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  665. "(maximum level is %d, or %dx%d CLUT)\n",
  666. max_clut_level, max_clut_size, max_clut_size);
  667. return AVERROR(EINVAL);
  668. }
  669. lut3d->lutsize = level;
  670. return 0;
  671. }
  672. static int update_apply_clut(FFFrameSync *fs)
  673. {
  674. AVFilterContext *ctx = fs->parent;
  675. AVFilterLink *inlink = ctx->inputs[0];
  676. AVFrame *master, *second, *out;
  677. int ret;
  678. ret = ff_framesync_dualinput_get(fs, &master, &second);
  679. if (ret < 0)
  680. return ret;
  681. if (!second)
  682. return ff_filter_frame(ctx->outputs[0], master);
  683. update_clut(ctx->priv, second);
  684. out = apply_lut(inlink, master);
  685. return ff_filter_frame(ctx->outputs[0], out);
  686. }
  687. static av_cold int haldclut_init(AVFilterContext *ctx)
  688. {
  689. LUT3DContext *lut3d = ctx->priv;
  690. lut3d->fs.on_event = update_apply_clut;
  691. return 0;
  692. }
  693. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  694. {
  695. LUT3DContext *lut3d = ctx->priv;
  696. ff_framesync_uninit(&lut3d->fs);
  697. }
  698. static const AVOption haldclut_options[] = {
  699. COMMON_OPTIONS
  700. };
  701. FRAMESYNC_DEFINE_CLASS(haldclut, LUT3DContext, fs);
  702. static const AVFilterPad haldclut_inputs[] = {
  703. {
  704. .name = "main",
  705. .type = AVMEDIA_TYPE_VIDEO,
  706. .config_props = config_input,
  707. },{
  708. .name = "clut",
  709. .type = AVMEDIA_TYPE_VIDEO,
  710. .config_props = config_clut,
  711. },
  712. { NULL }
  713. };
  714. static const AVFilterPad haldclut_outputs[] = {
  715. {
  716. .name = "default",
  717. .type = AVMEDIA_TYPE_VIDEO,
  718. .config_props = config_output,
  719. },
  720. { NULL }
  721. };
  722. AVFilter ff_vf_haldclut = {
  723. .name = "haldclut",
  724. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  725. .priv_size = sizeof(LUT3DContext),
  726. .preinit = haldclut_framesync_preinit,
  727. .init = haldclut_init,
  728. .uninit = haldclut_uninit,
  729. .query_formats = query_formats,
  730. .activate = activate,
  731. .inputs = haldclut_inputs,
  732. .outputs = haldclut_outputs,
  733. .priv_class = &haldclut_class,
  734. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  735. };
  736. #endif