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.

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