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.

589 lines
22KB

  1. /*
  2. * Copyright (c) 2013 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * 3D Lookup table filter
  23. */
  24. #include "libavutil/opt.h"
  25. #include "libavutil/file.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/avstring.h"
  30. #include "avfilter.h"
  31. #include "drawutils.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. #define R 0
  36. #define G 1
  37. #define B 2
  38. #define A 3
  39. enum interp_mode {
  40. INTERPOLATE_NEAREST,
  41. INTERPOLATE_TRILINEAR,
  42. INTERPOLATE_TETRAHEDRAL,
  43. NB_INTERP_MODE
  44. };
  45. struct rgbvec {
  46. float r, g, b;
  47. };
  48. #define MAX_LEVEL 36
  49. typedef struct LUT3DContext {
  50. const AVClass *class;
  51. enum interp_mode interpolation;
  52. char *file;
  53. uint8_t rgba_map[4];
  54. int step;
  55. int is16bit;
  56. struct rgbvec (*interp_8) (const struct LUT3DContext*, uint8_t, uint8_t, uint8_t);
  57. struct rgbvec (*interp_16)(const struct LUT3DContext*, uint16_t, uint16_t, uint16_t);
  58. struct rgbvec lut[MAX_LEVEL][MAX_LEVEL][MAX_LEVEL];
  59. int lutsize;
  60. } LUT3DContext;
  61. #define OFFSET(x) offsetof(LUT3DContext, x)
  62. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  63. static const AVOption lut3d_options[] = {
  64. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  65. { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, FLAGS, "interp_mode" },
  66. { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  67. { "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" },
  68. { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  69. { NULL }
  70. };
  71. AVFILTER_DEFINE_CLASS(lut3d);
  72. static inline float lerpf(float v0, float v1, float f)
  73. {
  74. return v0 + (v1 - v0) * f;
  75. }
  76. static inline struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
  77. {
  78. struct rgbvec v = {
  79. lerpf(v0->r, v1->r, f), lerpf(v0->g, v1->g, f), lerpf(v0->b, v1->b, f)
  80. };
  81. return v;
  82. }
  83. #define NEAR(x) ((int)((x) + .5))
  84. #define PREV(x) ((int)(x))
  85. #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
  86. /**
  87. * Get the nearest defined point
  88. */
  89. static inline struct rgbvec interp_nearest(const LUT3DContext *lut3d,
  90. const struct rgbvec *s)
  91. {
  92. return lut3d->lut[NEAR(s->r)][NEAR(s->g)][NEAR(s->b)];
  93. }
  94. /**
  95. * Interpolate using the 8 vertices of a cube
  96. * @see https://en.wikipedia.org/wiki/Trilinear_interpolation
  97. */
  98. static inline struct rgbvec interp_trilinear(const LUT3DContext *lut3d,
  99. const struct rgbvec *s)
  100. {
  101. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  102. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  103. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  104. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  105. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  106. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  107. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  108. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  109. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  110. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  111. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  112. const struct rgbvec c00 = lerp(&c000, &c100, d.r);
  113. const struct rgbvec c10 = lerp(&c010, &c110, d.r);
  114. const struct rgbvec c01 = lerp(&c001, &c101, d.r);
  115. const struct rgbvec c11 = lerp(&c011, &c111, d.r);
  116. const struct rgbvec c0 = lerp(&c00, &c10, d.g);
  117. const struct rgbvec c1 = lerp(&c01, &c11, d.g);
  118. const struct rgbvec c = lerp(&c0, &c1, d.b);
  119. return c;
  120. }
  121. /**
  122. * Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
  123. * @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
  124. */
  125. static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
  126. const struct rgbvec *s)
  127. {
  128. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  129. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  130. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  131. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  132. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  133. struct rgbvec c;
  134. if (d.r > d.g) {
  135. if (d.g > d.b) {
  136. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  137. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  138. c.r = (1-d.r) * c000.r + (d.r-d.g) * c100.r + (d.g-d.b) * c110.r + (d.b) * c111.r;
  139. c.g = (1-d.r) * c000.g + (d.r-d.g) * c100.g + (d.g-d.b) * c110.g + (d.b) * c111.g;
  140. c.b = (1-d.r) * c000.b + (d.r-d.g) * c100.b + (d.g-d.b) * c110.b + (d.b) * c111.b;
  141. } else if (d.r > d.b) {
  142. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  143. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  144. c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
  145. c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
  146. c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
  147. } else {
  148. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  149. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  150. c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
  151. c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
  152. c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
  153. }
  154. } else {
  155. if (d.b > d.g) {
  156. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  157. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  158. c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
  159. c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
  160. c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
  161. } else if (d.b > d.r) {
  162. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  163. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  164. c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
  165. c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
  166. c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
  167. } else {
  168. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  169. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  170. c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
  171. c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
  172. c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
  173. }
  174. }
  175. return c;
  176. }
  177. #define DEFINE_INTERP_FUNC(name, nbits) \
  178. static struct rgbvec interp_##nbits##_##name(const LUT3DContext *lut3d, \
  179. uint##nbits##_t r, \
  180. uint##nbits##_t g, \
  181. uint##nbits##_t b) \
  182. { \
  183. const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
  184. const struct rgbvec scaled_rgb = {r * scale, g * scale, b * scale}; \
  185. return interp_##name(lut3d, &scaled_rgb); \
  186. }
  187. DEFINE_INTERP_FUNC(nearest, 8)
  188. DEFINE_INTERP_FUNC(trilinear, 8)
  189. DEFINE_INTERP_FUNC(tetrahedral, 8)
  190. DEFINE_INTERP_FUNC(nearest, 16)
  191. DEFINE_INTERP_FUNC(trilinear, 16)
  192. DEFINE_INTERP_FUNC(tetrahedral, 16)
  193. #define MAX_LINE_SIZE 512
  194. static int skip_line(const char *p)
  195. {
  196. while (*p && av_isspace(*p))
  197. p++;
  198. return !*p || *p == '#';
  199. }
  200. #define NEXT_LINE(loop_cond) do { \
  201. if (!fgets(line, sizeof(line), f)) { \
  202. av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
  203. return AVERROR_INVALIDDATA; \
  204. } \
  205. } while (loop_cond)
  206. /* Basically r g and b float values on each line; seems to be generated by
  207. * Davinci */
  208. static int parse_dat(AVFilterContext *ctx, FILE *f)
  209. {
  210. LUT3DContext *lut3d = ctx->priv;
  211. const int size = lut3d->lutsize;
  212. int i, j, k;
  213. for (k = 0; k < size; k++) {
  214. for (j = 0; j < size; j++) {
  215. for (i = 0; i < size; i++) {
  216. char line[MAX_LINE_SIZE];
  217. struct rgbvec *vec = &lut3d->lut[k][j][i];
  218. NEXT_LINE(skip_line(line));
  219. sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b);
  220. }
  221. }
  222. }
  223. return 0;
  224. }
  225. /* Iridas format */
  226. static int parse_cube(AVFilterContext *ctx, FILE *f)
  227. {
  228. LUT3DContext *lut3d = ctx->priv;
  229. char line[MAX_LINE_SIZE];
  230. float min[3] = {0.0, 0.0, 0.0};
  231. float max[3] = {1.0, 1.0, 1.0};
  232. while (fgets(line, sizeof(line), f)) {
  233. if (!strncmp(line, "LUT_3D_SIZE ", 12)) {
  234. int i, j, k;
  235. const int size = strtol(line + 12, NULL, 0);
  236. if (size > MAX_LEVEL) {
  237. av_log(ctx, AV_LOG_ERROR, "Too large 3D LUT\n");
  238. return AVERROR(EINVAL);
  239. }
  240. lut3d->lutsize = size;
  241. for (k = 0; k < size; k++) {
  242. for (j = 0; j < size; j++) {
  243. for (i = 0; i < size; i++) {
  244. struct rgbvec *vec = &lut3d->lut[k][j][i];
  245. do {
  246. NEXT_LINE(0);
  247. if (!strncmp(line, "DOMAIN_", 7)) {
  248. float *vals = NULL;
  249. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  250. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  251. if (!vals)
  252. return AVERROR_INVALIDDATA;
  253. sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  254. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  255. min[0], min[1], min[2], max[0], max[1], max[2]);
  256. continue;
  257. }
  258. } while (skip_line(line));
  259. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  260. return AVERROR_INVALIDDATA;
  261. vec->r *= max[0] - min[0];
  262. vec->g *= max[1] - min[1];
  263. vec->b *= max[2] - min[2];
  264. }
  265. }
  266. }
  267. break;
  268. }
  269. }
  270. return 0;
  271. }
  272. /* Assume 17x17x17 LUT with a 16-bit depth
  273. * FIXME: it seems there are various 3dl formats */
  274. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  275. {
  276. char line[MAX_LINE_SIZE];
  277. LUT3DContext *lut3d = ctx->priv;
  278. int i, j, k;
  279. const int size = 17;
  280. const float scale = 16*16*16;
  281. lut3d->lutsize = size;
  282. if (!fgets(line, sizeof(line), f))
  283. return AVERROR_INVALIDDATA;
  284. for (k = 0; k < size; k++) {
  285. for (j = 0; j < size; j++) {
  286. for (i = 0; i < size; i++) {
  287. int r, g, b;
  288. struct rgbvec *vec = &lut3d->lut[k][j][i];
  289. NEXT_LINE(skip_line(line));
  290. if (sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  291. return AVERROR_INVALIDDATA;
  292. vec->r = r / scale;
  293. vec->g = g / scale;
  294. vec->b = b / scale;
  295. }
  296. }
  297. }
  298. return 0;
  299. }
  300. /* Pandora format */
  301. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  302. {
  303. LUT3DContext *lut3d = ctx->priv;
  304. float scale;
  305. int i, j, k, size, in = -1, out = -1;
  306. char line[MAX_LINE_SIZE];
  307. uint8_t rgb_map[3] = {0, 1, 2};
  308. while (fgets(line, sizeof(line), f)) {
  309. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  310. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  311. else if (!strncmp(line, "values", 6)) {
  312. const char *p = line + 6;
  313. #define SET_COLOR(id) do { \
  314. while (av_isspace(*p)) \
  315. p++; \
  316. switch (*p) { \
  317. case 'r': rgb_map[id] = 0; break; \
  318. case 'g': rgb_map[id] = 1; break; \
  319. case 'b': rgb_map[id] = 2; break; \
  320. } \
  321. while (*p && !av_isspace(*p)) \
  322. p++; \
  323. } while (0)
  324. SET_COLOR(0);
  325. SET_COLOR(1);
  326. SET_COLOR(2);
  327. break;
  328. }
  329. }
  330. if (in == -1 || out == -1) {
  331. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  332. return AVERROR_INVALIDDATA;
  333. }
  334. for (size = 1; size*size*size < in; size++);
  335. lut3d->lutsize = size;
  336. scale = 1. / (out - 1);
  337. for (k = 0; k < size; k++) {
  338. for (j = 0; j < size; j++) {
  339. for (i = 0; i < size; i++) {
  340. struct rgbvec *vec = &lut3d->lut[k][j][i];
  341. float val[3];
  342. NEXT_LINE(0);
  343. if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  344. return AVERROR_INVALIDDATA;
  345. vec->r = val[rgb_map[0]] * scale;
  346. vec->g = val[rgb_map[1]] * scale;
  347. vec->b = val[rgb_map[2]] * scale;
  348. }
  349. }
  350. }
  351. return 0;
  352. }
  353. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  354. {
  355. int i, j, k;
  356. const float c = 1. / (size - 1);
  357. lut3d->lutsize = size;
  358. for (k = 0; k < size; k++) {
  359. for (j = 0; j < size; j++) {
  360. for (i = 0; i < size; i++) {
  361. struct rgbvec *vec = &lut3d->lut[k][j][i];
  362. vec->r = k * c;
  363. vec->g = j * c;
  364. vec->b = i * c;
  365. }
  366. }
  367. }
  368. }
  369. static av_cold int init(AVFilterContext *ctx)
  370. {
  371. int ret;
  372. FILE *f;
  373. const char *ext;
  374. LUT3DContext *lut3d = ctx->priv;
  375. if (!lut3d->file) {
  376. set_identity_matrix(lut3d, 32);
  377. return 0;
  378. }
  379. f = fopen(lut3d->file, "r");
  380. if (!f) {
  381. ret = AVERROR(errno);
  382. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  383. return ret;
  384. }
  385. ext = strrchr(lut3d->file, '.');
  386. if (!ext) {
  387. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  388. ret = AVERROR_INVALIDDATA;
  389. goto end;
  390. }
  391. ext++;
  392. if (!av_strcasecmp(ext, "dat")) {
  393. lut3d->lutsize = 33;
  394. ret = parse_dat(ctx, f);
  395. } else if (!av_strcasecmp(ext, "3dl")) {
  396. ret = parse_3dl(ctx, f);
  397. } else if (!av_strcasecmp(ext, "cube")) {
  398. ret = parse_cube(ctx, f);
  399. } else if (!av_strcasecmp(ext, "m3d")) {
  400. ret = parse_m3d(ctx, f);
  401. } else {
  402. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  403. ret = AVERROR(EINVAL);
  404. }
  405. if (!ret && !lut3d->lutsize) {
  406. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  407. ret = AVERROR_INVALIDDATA;
  408. }
  409. end:
  410. fclose(f);
  411. return ret;
  412. }
  413. static int query_formats(AVFilterContext *ctx)
  414. {
  415. static const enum AVPixelFormat pix_fmts[] = {
  416. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  417. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  418. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  419. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  420. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  421. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  422. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  423. AV_PIX_FMT_NONE
  424. };
  425. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  426. return 0;
  427. }
  428. static int config_input(AVFilterLink *inlink)
  429. {
  430. LUT3DContext *lut3d = inlink->dst->priv;
  431. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  432. switch (inlink->format) {
  433. case AV_PIX_FMT_RGB48:
  434. case AV_PIX_FMT_BGR48:
  435. case AV_PIX_FMT_RGBA64:
  436. case AV_PIX_FMT_BGRA64:
  437. lut3d->is16bit = 1;
  438. }
  439. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  440. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + lut3d->is16bit);
  441. #define SET_FUNC(name) do { \
  442. if (lut3d->is16bit) lut3d->interp_16 = interp_16_##name; \
  443. else lut3d->interp_8 = interp_8_##name; \
  444. } while (0)
  445. switch (lut3d->interpolation) {
  446. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  447. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  448. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  449. default:
  450. av_assert0(0);
  451. }
  452. return 0;
  453. }
  454. #define FILTER(nbits) do { \
  455. uint8_t *dstrow = out->data[0]; \
  456. const uint8_t *srcrow = in ->data[0]; \
  457. \
  458. for (y = 0; y < inlink->h; y++) { \
  459. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  460. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  461. for (x = 0; x < inlink->w * step; x += step) { \
  462. struct rgbvec vec = lut3d->interp_##nbits(lut3d, src[x + r], src[x + g], src[x + b]); \
  463. dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
  464. dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
  465. dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
  466. if (!direct && step == 4) \
  467. dst[x + a] = src[x + a]; \
  468. } \
  469. dstrow += out->linesize[0]; \
  470. srcrow += in ->linesize[0]; \
  471. } \
  472. } while (0)
  473. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  474. {
  475. int x, y, direct = 0;
  476. AVFilterContext *ctx = inlink->dst;
  477. LUT3DContext *lut3d = ctx->priv;
  478. AVFilterLink *outlink = inlink->dst->outputs[0];
  479. AVFrame *out;
  480. const int step = lut3d->step;
  481. const uint8_t r = lut3d->rgba_map[R];
  482. const uint8_t g = lut3d->rgba_map[G];
  483. const uint8_t b = lut3d->rgba_map[B];
  484. const uint8_t a = lut3d->rgba_map[A];
  485. if (av_frame_is_writable(in)) {
  486. direct = 1;
  487. out = in;
  488. } else {
  489. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  490. if (!out) {
  491. av_frame_free(&in);
  492. return AVERROR(ENOMEM);
  493. }
  494. av_frame_copy_props(out, in);
  495. }
  496. if (lut3d->is16bit) FILTER(16);
  497. else FILTER(8);
  498. if (!direct)
  499. av_frame_free(&in);
  500. return ff_filter_frame(outlink, out);
  501. }
  502. static const AVFilterPad lut3d_inputs[] = {
  503. {
  504. .name = "default",
  505. .type = AVMEDIA_TYPE_VIDEO,
  506. .filter_frame = filter_frame,
  507. .config_props = config_input,
  508. },
  509. { NULL }
  510. };
  511. static const AVFilterPad lut3d_outputs[] = {
  512. {
  513. .name = "default",
  514. .type = AVMEDIA_TYPE_VIDEO,
  515. },
  516. { NULL }
  517. };
  518. AVFilter avfilter_vf_lut3d = {
  519. .name = "lut3d",
  520. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  521. .priv_size = sizeof(LUT3DContext),
  522. .init = init,
  523. .query_formats = query_formats,
  524. .inputs = lut3d_inputs,
  525. .outputs = lut3d_outputs,
  526. .priv_class = &lut3d_class,
  527. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  528. };