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.

800 lines
29KB

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