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.

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