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.

579 lines
21KB

  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 struct rgbvec d = {s->r - PREV(s->r), s->g - PREV(s->g), s->b - PREV(s->b)};
  102. const struct rgbvec c000 = lut3d->lut[PREV(s->r)][PREV(s->g)][PREV(s->b)];
  103. const struct rgbvec c001 = lut3d->lut[PREV(s->r)][PREV(s->g)][NEXT(s->b)];
  104. const struct rgbvec c010 = lut3d->lut[PREV(s->r)][NEXT(s->g)][PREV(s->b)];
  105. const struct rgbvec c011 = lut3d->lut[PREV(s->r)][NEXT(s->g)][NEXT(s->b)];
  106. const struct rgbvec c100 = lut3d->lut[NEXT(s->r)][PREV(s->g)][PREV(s->b)];
  107. const struct rgbvec c101 = lut3d->lut[NEXT(s->r)][PREV(s->g)][NEXT(s->b)];
  108. const struct rgbvec c110 = lut3d->lut[NEXT(s->r)][NEXT(s->g)][PREV(s->b)];
  109. const struct rgbvec c111 = lut3d->lut[NEXT(s->r)][NEXT(s->g)][NEXT(s->b)];
  110. const struct rgbvec c00 = lerp(&c000, &c100, d.r);
  111. const struct rgbvec c10 = lerp(&c010, &c110, d.r);
  112. const struct rgbvec c01 = lerp(&c001, &c101, d.r);
  113. const struct rgbvec c11 = lerp(&c011, &c111, d.r);
  114. const struct rgbvec c0 = lerp(&c00, &c10, d.g);
  115. const struct rgbvec c1 = lerp(&c01, &c11, d.g);
  116. const struct rgbvec c = lerp(&c0, &c1, d.b);
  117. return c;
  118. }
  119. /**
  120. * Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
  121. * @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
  122. */
  123. static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
  124. const struct rgbvec *s)
  125. {
  126. const struct rgbvec d = {s->r - PREV(s->r), s->g - PREV(s->g), s->b - PREV(s->b)};
  127. const struct rgbvec c000 = lut3d->lut[PREV(s->r)][PREV(s->g)][PREV(s->b)];
  128. const struct rgbvec c001 = lut3d->lut[PREV(s->r)][PREV(s->g)][NEXT(s->b)];
  129. const struct rgbvec c010 = lut3d->lut[PREV(s->r)][NEXT(s->g)][PREV(s->b)];
  130. const struct rgbvec c011 = lut3d->lut[PREV(s->r)][NEXT(s->g)][NEXT(s->b)];
  131. const struct rgbvec c100 = lut3d->lut[NEXT(s->r)][PREV(s->g)][PREV(s->b)];
  132. const struct rgbvec c101 = lut3d->lut[NEXT(s->r)][PREV(s->g)][NEXT(s->b)];
  133. const struct rgbvec c110 = lut3d->lut[NEXT(s->r)][NEXT(s->g)][PREV(s->b)];
  134. const struct rgbvec c111 = lut3d->lut[NEXT(s->r)][NEXT(s->g)][NEXT(s->b)];
  135. struct rgbvec c;
  136. if (d.r > d.g) {
  137. if (d.g > d.b) {
  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. c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
  143. c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
  144. c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
  145. } else {
  146. c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
  147. c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
  148. c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
  149. }
  150. } else {
  151. if (d.b > d.g) {
  152. c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
  153. c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
  154. c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
  155. } else if (d.b > d.r) {
  156. c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
  157. c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
  158. c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
  159. } else {
  160. c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
  161. c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
  162. c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
  163. }
  164. }
  165. return c;
  166. }
  167. #define DEFINE_INTERP_FUNC(name, nbits) \
  168. static struct rgbvec interp_##nbits##_##name(const LUT3DContext *lut3d, \
  169. uint##nbits##_t r, \
  170. uint##nbits##_t g, \
  171. uint##nbits##_t b) \
  172. { \
  173. const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
  174. const struct rgbvec scaled_rgb = {r * scale, g * scale, b * scale}; \
  175. return interp_##name(lut3d, &scaled_rgb); \
  176. }
  177. DEFINE_INTERP_FUNC(nearest, 8)
  178. DEFINE_INTERP_FUNC(trilinear, 8)
  179. DEFINE_INTERP_FUNC(tetrahedral, 8)
  180. DEFINE_INTERP_FUNC(nearest, 16)
  181. DEFINE_INTERP_FUNC(trilinear, 16)
  182. DEFINE_INTERP_FUNC(tetrahedral, 16)
  183. #define MAX_LINE_SIZE 512
  184. static int skip_line(const char *p)
  185. {
  186. while (*p && av_isspace(*p))
  187. p++;
  188. return !*p || *p == '#';
  189. }
  190. #define NEXT_LINE(loop_cond) do { \
  191. if (!fgets(line, sizeof(line), f)) { \
  192. av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
  193. return AVERROR_INVALIDDATA; \
  194. } \
  195. } while (loop_cond)
  196. /* Basically r g and b float values on each line; seems to be generated by
  197. * Davinci */
  198. static int parse_dat(AVFilterContext *ctx, FILE *f)
  199. {
  200. LUT3DContext *lut3d = ctx->priv;
  201. const int size = lut3d->lutsize;
  202. int i, j, k;
  203. for (k = 0; k < size; k++) {
  204. for (j = 0; j < size; j++) {
  205. for (i = 0; i < size; i++) {
  206. char line[MAX_LINE_SIZE];
  207. struct rgbvec *vec = &lut3d->lut[k][j][i];
  208. NEXT_LINE(skip_line(line));
  209. sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b);
  210. }
  211. }
  212. }
  213. return 0;
  214. }
  215. /* Iridas format */
  216. static int parse_cube(AVFilterContext *ctx, FILE *f)
  217. {
  218. LUT3DContext *lut3d = ctx->priv;
  219. char line[MAX_LINE_SIZE];
  220. float min[3] = {0.0, 0.0, 0.0};
  221. float max[3] = {1.0, 1.0, 1.0};
  222. while (fgets(line, sizeof(line), f)) {
  223. if (!strncmp(line, "LUT_3D_SIZE ", 12)) {
  224. int i, j, k;
  225. const int size = strtol(line + 12, NULL, 0);
  226. if (size > MAX_LEVEL) {
  227. av_log(ctx, AV_LOG_ERROR, "Too large 3D LUT\n");
  228. return AVERROR(EINVAL);
  229. }
  230. lut3d->lutsize = size;
  231. for (k = 0; k < size; k++) {
  232. for (j = 0; j < size; j++) {
  233. for (i = 0; i < size; i++) {
  234. struct rgbvec *vec = &lut3d->lut[k][j][i];
  235. do {
  236. NEXT_LINE(0);
  237. if (!strncmp(line, "DOMAIN_", 7)) {
  238. float *vals = NULL;
  239. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  240. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  241. if (!vals)
  242. return AVERROR_INVALIDDATA;
  243. sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  244. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  245. min[0], min[1], min[2], max[0], max[1], max[2]);
  246. continue;
  247. }
  248. } while (skip_line(line));
  249. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  250. return AVERROR_INVALIDDATA;
  251. vec->r *= max[0] - min[0];
  252. vec->g *= max[1] - min[1];
  253. vec->b *= max[2] - min[2];
  254. }
  255. }
  256. }
  257. break;
  258. }
  259. }
  260. return 0;
  261. }
  262. /* Assume 17x17x17 LUT with a 16-bit depth
  263. * FIXME: it seems there are various 3dl formats */
  264. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  265. {
  266. char line[MAX_LINE_SIZE];
  267. LUT3DContext *lut3d = ctx->priv;
  268. int i, j, k;
  269. const int size = 17;
  270. const float scale = 16*16*16;
  271. lut3d->lutsize = size;
  272. if (!fgets(line, sizeof(line), f))
  273. return AVERROR_INVALIDDATA;
  274. for (k = 0; k < size; k++) {
  275. for (j = 0; j < size; j++) {
  276. for (i = 0; i < size; i++) {
  277. int r, g, b;
  278. struct rgbvec *vec = &lut3d->lut[k][j][i];
  279. NEXT_LINE(skip_line(line));
  280. if (sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  281. return AVERROR_INVALIDDATA;
  282. vec->r = r / scale;
  283. vec->g = g / scale;
  284. vec->b = b / scale;
  285. }
  286. }
  287. }
  288. return 0;
  289. }
  290. /* Pandora format */
  291. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  292. {
  293. LUT3DContext *lut3d = ctx->priv;
  294. float scale;
  295. int i, j, k, size, in = -1, out = -1;
  296. char line[MAX_LINE_SIZE];
  297. uint8_t rgb_map[3] = {0, 1, 2};
  298. while (fgets(line, sizeof(line), f)) {
  299. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  300. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  301. else if (!strncmp(line, "values", 6)) {
  302. const char *p = line + 6;
  303. #define SET_COLOR(id) do { \
  304. while (av_isspace(*p)) \
  305. p++; \
  306. switch (*p) { \
  307. case 'r': rgb_map[id] = 0; break; \
  308. case 'g': rgb_map[id] = 1; break; \
  309. case 'b': rgb_map[id] = 2; break; \
  310. } \
  311. while (*p && !av_isspace(*p)) \
  312. p++; \
  313. } while (0)
  314. SET_COLOR(0);
  315. SET_COLOR(1);
  316. SET_COLOR(2);
  317. break;
  318. }
  319. }
  320. if (in == -1 || out == -1) {
  321. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  322. return AVERROR_INVALIDDATA;
  323. }
  324. for (size = 1; size*size*size < in; size++);
  325. lut3d->lutsize = size;
  326. scale = 1. / (out - 1);
  327. for (k = 0; k < size; k++) {
  328. for (j = 0; j < size; j++) {
  329. for (i = 0; i < size; i++) {
  330. struct rgbvec *vec = &lut3d->lut[k][j][i];
  331. float val[3];
  332. NEXT_LINE(0);
  333. if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  334. return AVERROR_INVALIDDATA;
  335. vec->r = val[rgb_map[0]] * scale;
  336. vec->g = val[rgb_map[1]] * scale;
  337. vec->b = val[rgb_map[2]] * scale;
  338. }
  339. }
  340. }
  341. return 0;
  342. }
  343. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  344. {
  345. int i, j, k;
  346. const float c = 1. / (size - 1);
  347. lut3d->lutsize = size;
  348. for (k = 0; k < size; k++) {
  349. for (j = 0; j < size; j++) {
  350. for (i = 0; i < size; i++) {
  351. struct rgbvec *vec = &lut3d->lut[k][j][i];
  352. vec->r = k * c;
  353. vec->g = j * c;
  354. vec->b = i * c;
  355. }
  356. }
  357. }
  358. }
  359. static av_cold int init(AVFilterContext *ctx)
  360. {
  361. int ret;
  362. FILE *f;
  363. const char *ext;
  364. LUT3DContext *lut3d = ctx->priv;
  365. if (!lut3d->file) {
  366. set_identity_matrix(lut3d, 32);
  367. return 0;
  368. }
  369. f = fopen(lut3d->file, "r");
  370. if (!f) {
  371. ret = AVERROR(errno);
  372. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  373. return ret;
  374. }
  375. ext = strrchr(lut3d->file, '.');
  376. if (!ext) {
  377. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  378. ret = AVERROR_INVALIDDATA;
  379. goto end;
  380. }
  381. ext++;
  382. if (!av_strcasecmp(ext, "dat")) {
  383. lut3d->lutsize = 33;
  384. ret = parse_dat(ctx, f);
  385. } else if (!av_strcasecmp(ext, "3dl")) {
  386. ret = parse_3dl(ctx, f);
  387. } else if (!av_strcasecmp(ext, "cube")) {
  388. ret = parse_cube(ctx, f);
  389. } else if (!av_strcasecmp(ext, "m3d")) {
  390. ret = parse_m3d(ctx, f);
  391. } else {
  392. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  393. ret = AVERROR(EINVAL);
  394. }
  395. if (!ret && !lut3d->lutsize) {
  396. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  397. ret = AVERROR_INVALIDDATA;
  398. }
  399. end:
  400. fclose(f);
  401. return ret;
  402. }
  403. static int query_formats(AVFilterContext *ctx)
  404. {
  405. static const enum AVPixelFormat pix_fmts[] = {
  406. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  407. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  408. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  409. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  410. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  411. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  412. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  413. AV_PIX_FMT_NONE
  414. };
  415. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  416. return 0;
  417. }
  418. static int config_input(AVFilterLink *inlink)
  419. {
  420. LUT3DContext *lut3d = inlink->dst->priv;
  421. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  422. switch (inlink->format) {
  423. case AV_PIX_FMT_RGB48:
  424. case AV_PIX_FMT_BGR48:
  425. case AV_PIX_FMT_RGBA64:
  426. case AV_PIX_FMT_BGRA64:
  427. lut3d->is16bit = 1;
  428. }
  429. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  430. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + lut3d->is16bit);
  431. #define SET_FUNC(name) do { \
  432. if (lut3d->is16bit) lut3d->interp_16 = interp_16_##name; \
  433. else lut3d->interp_8 = interp_8_##name; \
  434. } while (0)
  435. switch (lut3d->interpolation) {
  436. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  437. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  438. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  439. default:
  440. av_assert0(0);
  441. }
  442. return 0;
  443. }
  444. #define FILTER(nbits) do { \
  445. uint8_t *dstrow = out->data[0]; \
  446. const uint8_t *srcrow = in ->data[0]; \
  447. \
  448. for (y = 0; y < inlink->h; y++) { \
  449. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  450. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  451. for (x = 0; x < inlink->w * step; x += step) { \
  452. struct rgbvec vec = lut3d->interp_##nbits(lut3d, src[x + r], src[x + g], src[x + b]); \
  453. dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
  454. dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
  455. dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
  456. if (!direct && step == 4) \
  457. dst[x + a] = src[x + a]; \
  458. } \
  459. dstrow += out->linesize[0]; \
  460. srcrow += in ->linesize[0]; \
  461. } \
  462. } while (0)
  463. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  464. {
  465. int x, y, direct = 0;
  466. AVFilterContext *ctx = inlink->dst;
  467. LUT3DContext *lut3d = ctx->priv;
  468. AVFilterLink *outlink = inlink->dst->outputs[0];
  469. AVFrame *out;
  470. const int step = lut3d->step;
  471. const uint8_t r = lut3d->rgba_map[R];
  472. const uint8_t g = lut3d->rgba_map[G];
  473. const uint8_t b = lut3d->rgba_map[B];
  474. const uint8_t a = lut3d->rgba_map[A];
  475. if (av_frame_is_writable(in)) {
  476. direct = 1;
  477. out = in;
  478. } else {
  479. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  480. if (!out) {
  481. av_frame_free(&in);
  482. return AVERROR(ENOMEM);
  483. }
  484. av_frame_copy_props(out, in);
  485. }
  486. if (lut3d->is16bit) FILTER(16);
  487. else FILTER(8);
  488. if (!direct)
  489. av_frame_free(&in);
  490. return ff_filter_frame(outlink, out);
  491. }
  492. static const AVFilterPad lut3d_inputs[] = {
  493. {
  494. .name = "default",
  495. .type = AVMEDIA_TYPE_VIDEO,
  496. .filter_frame = filter_frame,
  497. .config_props = config_input,
  498. },
  499. { NULL }
  500. };
  501. static const AVFilterPad lut3d_outputs[] = {
  502. {
  503. .name = "default",
  504. .type = AVMEDIA_TYPE_VIDEO,
  505. },
  506. { NULL }
  507. };
  508. AVFilter avfilter_vf_lut3d = {
  509. .name = "lut3d",
  510. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  511. .priv_size = sizeof(LUT3DContext),
  512. .init = init,
  513. .query_formats = query_formats,
  514. .inputs = lut3d_inputs,
  515. .outputs = lut3d_outputs,
  516. .priv_class = &lut3d_class,
  517. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  518. };