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.

794 lines
28KB

  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 > MAX_LEVEL) {
  244. av_log(ctx, AV_LOG_ERROR, "Too large 3D LUT\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. for (size = 1; size*size*size < in; size++);
  342. lut3d->lutsize = size;
  343. scale = 1. / (out - 1);
  344. for (k = 0; k < size; k++) {
  345. for (j = 0; j < size; j++) {
  346. for (i = 0; i < size; i++) {
  347. struct rgbvec *vec = &lut3d->lut[k][j][i];
  348. float val[3];
  349. NEXT_LINE(0);
  350. if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  351. return AVERROR_INVALIDDATA;
  352. vec->r = val[rgb_map[0]] * scale;
  353. vec->g = val[rgb_map[1]] * scale;
  354. vec->b = val[rgb_map[2]] * scale;
  355. }
  356. }
  357. }
  358. return 0;
  359. }
  360. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  361. {
  362. int i, j, k;
  363. const float c = 1. / (size - 1);
  364. lut3d->lutsize = size;
  365. for (k = 0; k < size; k++) {
  366. for (j = 0; j < size; j++) {
  367. for (i = 0; i < size; i++) {
  368. struct rgbvec *vec = &lut3d->lut[k][j][i];
  369. vec->r = k * c;
  370. vec->g = j * c;
  371. vec->b = i * c;
  372. }
  373. }
  374. }
  375. }
  376. static int query_formats(AVFilterContext *ctx)
  377. {
  378. static const enum AVPixelFormat pix_fmts[] = {
  379. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  380. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  381. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  382. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  383. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  384. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  385. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  386. AV_PIX_FMT_NONE
  387. };
  388. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  389. return 0;
  390. }
  391. static int config_input(AVFilterLink *inlink)
  392. {
  393. LUT3DContext *lut3d = inlink->dst->priv;
  394. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  395. switch (inlink->format) {
  396. case AV_PIX_FMT_RGB48:
  397. case AV_PIX_FMT_BGR48:
  398. case AV_PIX_FMT_RGBA64:
  399. case AV_PIX_FMT_BGRA64:
  400. lut3d->is16bit = 1;
  401. }
  402. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  403. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + lut3d->is16bit);
  404. #define SET_FUNC(name) do { \
  405. if (lut3d->is16bit) lut3d->interp_16 = interp_16_##name; \
  406. else lut3d->interp_8 = interp_8_##name; \
  407. } while (0)
  408. switch (lut3d->interpolation) {
  409. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  410. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  411. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  412. default:
  413. av_assert0(0);
  414. }
  415. return 0;
  416. }
  417. #define FILTER(nbits) do { \
  418. uint8_t *dstrow = out->data[0]; \
  419. const uint8_t *srcrow = in ->data[0]; \
  420. \
  421. for (y = 0; y < inlink->h; y++) { \
  422. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  423. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  424. for (x = 0; x < inlink->w * step; x += step) { \
  425. struct rgbvec vec = lut3d->interp_##nbits(lut3d, src[x + r], src[x + g], src[x + b]); \
  426. dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
  427. dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
  428. dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
  429. if (!direct && step == 4) \
  430. dst[x + a] = src[x + a]; \
  431. } \
  432. dstrow += out->linesize[0]; \
  433. srcrow += in ->linesize[0]; \
  434. } \
  435. } while (0)
  436. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  437. {
  438. int x, y, direct = 0;
  439. AVFilterContext *ctx = inlink->dst;
  440. LUT3DContext *lut3d = ctx->priv;
  441. AVFilterLink *outlink = inlink->dst->outputs[0];
  442. AVFrame *out;
  443. const int step = lut3d->step;
  444. const uint8_t r = lut3d->rgba_map[R];
  445. const uint8_t g = lut3d->rgba_map[G];
  446. const uint8_t b = lut3d->rgba_map[B];
  447. const uint8_t a = lut3d->rgba_map[A];
  448. if (av_frame_is_writable(in)) {
  449. direct = 1;
  450. out = in;
  451. } else {
  452. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  453. if (!out) {
  454. av_frame_free(&in);
  455. return NULL;
  456. }
  457. av_frame_copy_props(out, in);
  458. }
  459. if (lut3d->is16bit) FILTER(16);
  460. else FILTER(8);
  461. if (!direct)
  462. av_frame_free(&in);
  463. return out;
  464. }
  465. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  466. {
  467. AVFilterLink *outlink = inlink->dst->outputs[0];
  468. AVFrame *out = apply_lut(inlink, in);
  469. if (!out)
  470. return AVERROR(ENOMEM);
  471. return ff_filter_frame(outlink, out);
  472. }
  473. #if CONFIG_LUT3D_FILTER
  474. static const AVOption lut3d_options[] = {
  475. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  476. COMMON_OPTIONS
  477. };
  478. AVFILTER_DEFINE_CLASS(lut3d);
  479. static av_cold int lut3d_init(AVFilterContext *ctx)
  480. {
  481. int ret;
  482. FILE *f;
  483. const char *ext;
  484. LUT3DContext *lut3d = ctx->priv;
  485. if (!lut3d->file) {
  486. set_identity_matrix(lut3d, 32);
  487. return 0;
  488. }
  489. f = fopen(lut3d->file, "r");
  490. if (!f) {
  491. ret = AVERROR(errno);
  492. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  493. return ret;
  494. }
  495. ext = strrchr(lut3d->file, '.');
  496. if (!ext) {
  497. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  498. ret = AVERROR_INVALIDDATA;
  499. goto end;
  500. }
  501. ext++;
  502. if (!av_strcasecmp(ext, "dat")) {
  503. lut3d->lutsize = 33;
  504. ret = parse_dat(ctx, f);
  505. } else if (!av_strcasecmp(ext, "3dl")) {
  506. ret = parse_3dl(ctx, f);
  507. } else if (!av_strcasecmp(ext, "cube")) {
  508. ret = parse_cube(ctx, f);
  509. } else if (!av_strcasecmp(ext, "m3d")) {
  510. ret = parse_m3d(ctx, f);
  511. } else {
  512. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  513. ret = AVERROR(EINVAL);
  514. }
  515. if (!ret && !lut3d->lutsize) {
  516. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  517. ret = AVERROR_INVALIDDATA;
  518. }
  519. end:
  520. fclose(f);
  521. return ret;
  522. }
  523. static const AVFilterPad lut3d_inputs[] = {
  524. {
  525. .name = "default",
  526. .type = AVMEDIA_TYPE_VIDEO,
  527. .filter_frame = filter_frame,
  528. .config_props = config_input,
  529. },
  530. { NULL }
  531. };
  532. static const AVFilterPad lut3d_outputs[] = {
  533. {
  534. .name = "default",
  535. .type = AVMEDIA_TYPE_VIDEO,
  536. },
  537. { NULL }
  538. };
  539. AVFilter avfilter_vf_lut3d = {
  540. .name = "lut3d",
  541. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  542. .priv_size = sizeof(LUT3DContext),
  543. .init = lut3d_init,
  544. .query_formats = query_formats,
  545. .inputs = lut3d_inputs,
  546. .outputs = lut3d_outputs,
  547. .priv_class = &lut3d_class,
  548. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  549. };
  550. #endif
  551. #if CONFIG_HALDCLUT_FILTER
  552. static void update_clut(LUT3DContext *lut3d, const AVFrame *frame)
  553. {
  554. const uint8_t *data = frame->data[0];
  555. const int linesize = frame->linesize[0];
  556. const int w = lut3d->clut_width;
  557. const int step = lut3d->clut_step;
  558. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  559. const int level = lut3d->lutsize;
  560. #define LOAD_CLUT(nbits) do { \
  561. int i, j, k, x = 0, y = 0; \
  562. \
  563. for (k = 0; k < level; k++) { \
  564. for (j = 0; j < level; j++) { \
  565. for (i = 0; i < level; i++) { \
  566. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  567. (data + y*linesize + x*step); \
  568. struct rgbvec *vec = &lut3d->lut[k][j][i]; \
  569. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  570. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  571. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  572. if (++x == w) { \
  573. x = 0; \
  574. y++; \
  575. } \
  576. } \
  577. } \
  578. } \
  579. } while (0)
  580. if (!lut3d->clut_is16bit) LOAD_CLUT(8);
  581. else LOAD_CLUT(16);
  582. }
  583. static int config_output(AVFilterLink *outlink)
  584. {
  585. AVFilterContext *ctx = outlink->src;
  586. outlink->w = ctx->inputs[0]->w;
  587. outlink->h = ctx->inputs[0]->h;
  588. outlink->time_base = ctx->inputs[0]->time_base;
  589. return 0;
  590. }
  591. static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
  592. {
  593. LUT3DContext *s = inlink->dst->priv;
  594. return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
  595. }
  596. static int filter_frame_clut(AVFilterLink *inlink, AVFrame *inpicref)
  597. {
  598. LUT3DContext *s = inlink->dst->priv;
  599. return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
  600. }
  601. static int request_frame(AVFilterLink *outlink)
  602. {
  603. LUT3DContext *s = outlink->src->priv;
  604. return ff_dualinput_request_frame(&s->dinput, outlink);
  605. }
  606. static int config_clut(AVFilterLink *inlink)
  607. {
  608. int size, level, w, h;
  609. AVFilterContext *ctx = inlink->dst;
  610. LUT3DContext *lut3d = ctx->priv;
  611. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  612. lut3d->clut_is16bit = 0;
  613. switch (inlink->format) {
  614. case AV_PIX_FMT_RGB48:
  615. case AV_PIX_FMT_BGR48:
  616. case AV_PIX_FMT_RGBA64:
  617. case AV_PIX_FMT_BGRA64:
  618. lut3d->clut_is16bit = 1;
  619. }
  620. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  621. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  622. if (inlink->w > inlink->h)
  623. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  624. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  625. else if (inlink->w < inlink->h)
  626. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  627. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  628. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  629. for (level = 1; level*level*level < w; level++);
  630. size = level*level*level;
  631. if (size != w) {
  632. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  633. return AVERROR_INVALIDDATA;
  634. }
  635. av_assert0(w == h && w == size);
  636. level *= level;
  637. if (level > MAX_LEVEL) {
  638. const int max_clut_level = sqrt(MAX_LEVEL);
  639. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  640. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  641. "(maximum level is %d, or %dx%d CLUT)\n",
  642. max_clut_level, max_clut_size, max_clut_size);
  643. return AVERROR(EINVAL);
  644. }
  645. lut3d->lutsize = level;
  646. return 0;
  647. }
  648. static AVFrame *update_apply_clut(AVFilterContext *ctx, AVFrame *main,
  649. const AVFrame *second)
  650. {
  651. AVFilterLink *inlink = ctx->inputs[0];
  652. update_clut(ctx->priv, second);
  653. return apply_lut(inlink, main);
  654. }
  655. static av_cold int haldclut_init(AVFilterContext *ctx)
  656. {
  657. LUT3DContext *lut3d = ctx->priv;
  658. lut3d->dinput.process = update_apply_clut;
  659. return 0;
  660. }
  661. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  662. {
  663. LUT3DContext *lut3d = ctx->priv;
  664. ff_dualinput_uninit(&lut3d->dinput);
  665. }
  666. static const AVOption haldclut_options[] = {
  667. { "shortest", "force termination when the shortest input terminates", OFFSET(dinput.shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  668. { "repeatlast", "continue applying the last clut after eos", OFFSET(dinput.repeatlast), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS },
  669. COMMON_OPTIONS
  670. };
  671. AVFILTER_DEFINE_CLASS(haldclut);
  672. static const AVFilterPad haldclut_inputs[] = {
  673. {
  674. .name = "main",
  675. .type = AVMEDIA_TYPE_VIDEO,
  676. .filter_frame = filter_frame_main,
  677. .config_props = config_input,
  678. },{
  679. .name = "clut",
  680. .type = AVMEDIA_TYPE_VIDEO,
  681. .filter_frame = filter_frame_clut,
  682. .config_props = config_clut,
  683. },
  684. { NULL }
  685. };
  686. static const AVFilterPad haldclut_outputs[] = {
  687. {
  688. .name = "default",
  689. .type = AVMEDIA_TYPE_VIDEO,
  690. .request_frame = request_frame,
  691. .config_props = config_output,
  692. },
  693. { NULL }
  694. };
  695. AVFilter avfilter_vf_haldclut = {
  696. .name = "haldclut",
  697. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  698. .priv_size = sizeof(LUT3DContext),
  699. .init = haldclut_init,
  700. .uninit = haldclut_uninit,
  701. .query_formats = query_formats,
  702. .inputs = haldclut_inputs,
  703. .outputs = haldclut_outputs,
  704. .priv_class = &haldclut_class,
  705. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  706. };
  707. #endif