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.

1654 lines
66KB

  1. /*
  2. * Copyright (c) 2013 Clément Bœsch
  3. * Copyright (c) 2018 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * 3D Lookup table filter
  24. */
  25. #include "libavutil/opt.h"
  26. #include "libavutil/file.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/avstring.h"
  31. #include "avfilter.h"
  32. #include "drawutils.h"
  33. #include "formats.h"
  34. #include "framesync.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #define R 0
  38. #define G 1
  39. #define B 2
  40. #define A 3
  41. enum interp_mode {
  42. INTERPOLATE_NEAREST,
  43. INTERPOLATE_TRILINEAR,
  44. INTERPOLATE_TETRAHEDRAL,
  45. NB_INTERP_MODE
  46. };
  47. struct rgbvec {
  48. float r, g, b;
  49. };
  50. /* 3D LUT don't often go up to level 32, but it is common to have a Hald CLUT
  51. * of 512x512 (64x64x64) */
  52. #define MAX_LEVEL 64
  53. typedef struct LUT3DContext {
  54. const AVClass *class;
  55. int interpolation; ///<interp_mode
  56. char *file;
  57. uint8_t rgba_map[4];
  58. int step;
  59. avfilter_action_func *interp;
  60. struct rgbvec lut[MAX_LEVEL][MAX_LEVEL][MAX_LEVEL];
  61. int lutsize;
  62. #if CONFIG_HALDCLUT_FILTER
  63. uint8_t clut_rgba_map[4];
  64. int clut_step;
  65. int clut_bits;
  66. int clut_planar;
  67. int clut_width;
  68. FFFrameSync fs;
  69. #endif
  70. } LUT3DContext;
  71. typedef struct ThreadData {
  72. AVFrame *in, *out;
  73. } ThreadData;
  74. #define OFFSET(x) offsetof(LUT3DContext, x)
  75. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  76. #define COMMON_OPTIONS \
  77. { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, FLAGS, "interp_mode" }, \
  78. { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
  79. { "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" }, \
  80. { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
  81. { NULL }
  82. static inline float lerpf(float v0, float v1, float f)
  83. {
  84. return v0 + (v1 - v0) * f;
  85. }
  86. static inline struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
  87. {
  88. struct rgbvec v = {
  89. lerpf(v0->r, v1->r, f), lerpf(v0->g, v1->g, f), lerpf(v0->b, v1->b, f)
  90. };
  91. return v;
  92. }
  93. #define NEAR(x) ((int)((x) + .5))
  94. #define PREV(x) ((int)(x))
  95. #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
  96. /**
  97. * Get the nearest defined point
  98. */
  99. static inline struct rgbvec interp_nearest(const LUT3DContext *lut3d,
  100. const struct rgbvec *s)
  101. {
  102. return lut3d->lut[NEAR(s->r)][NEAR(s->g)][NEAR(s->b)];
  103. }
  104. /**
  105. * Interpolate using the 8 vertices of a cube
  106. * @see https://en.wikipedia.org/wiki/Trilinear_interpolation
  107. */
  108. static inline struct rgbvec interp_trilinear(const LUT3DContext *lut3d,
  109. const struct rgbvec *s)
  110. {
  111. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  112. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  113. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  114. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  115. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  116. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  117. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  118. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  119. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  120. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  121. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  122. const struct rgbvec c00 = lerp(&c000, &c100, d.r);
  123. const struct rgbvec c10 = lerp(&c010, &c110, d.r);
  124. const struct rgbvec c01 = lerp(&c001, &c101, d.r);
  125. const struct rgbvec c11 = lerp(&c011, &c111, d.r);
  126. const struct rgbvec c0 = lerp(&c00, &c10, d.g);
  127. const struct rgbvec c1 = lerp(&c01, &c11, d.g);
  128. const struct rgbvec c = lerp(&c0, &c1, d.b);
  129. return c;
  130. }
  131. /**
  132. * Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
  133. * @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
  134. */
  135. static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
  136. const struct rgbvec *s)
  137. {
  138. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  139. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  140. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  141. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  142. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  143. struct rgbvec c;
  144. if (d.r > d.g) {
  145. if (d.g > d.b) {
  146. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  147. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  148. c.r = (1-d.r) * c000.r + (d.r-d.g) * c100.r + (d.g-d.b) * c110.r + (d.b) * c111.r;
  149. c.g = (1-d.r) * c000.g + (d.r-d.g) * c100.g + (d.g-d.b) * c110.g + (d.b) * c111.g;
  150. c.b = (1-d.r) * c000.b + (d.r-d.g) * c100.b + (d.g-d.b) * c110.b + (d.b) * c111.b;
  151. } else if (d.r > d.b) {
  152. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  153. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  154. c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
  155. c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
  156. c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
  157. } else {
  158. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  159. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  160. c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
  161. c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
  162. c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
  163. }
  164. } else {
  165. if (d.b > d.g) {
  166. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  167. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  168. c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
  169. c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
  170. c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
  171. } else if (d.b > d.r) {
  172. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  173. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  174. c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
  175. c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
  176. c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
  177. } else {
  178. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  179. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  180. c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
  181. c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
  182. c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
  183. }
  184. }
  185. return c;
  186. }
  187. #define DEFINE_INTERP_FUNC_PLANAR(name, nbits, depth) \
  188. static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  189. { \
  190. int x, y; \
  191. const LUT3DContext *lut3d = ctx->priv; \
  192. const ThreadData *td = arg; \
  193. const AVFrame *in = td->in; \
  194. const AVFrame *out = td->out; \
  195. const int direct = out == in; \
  196. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  197. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  198. uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
  199. uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
  200. uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
  201. uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
  202. const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
  203. const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
  204. const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
  205. const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
  206. const float scale = (1. / ((1<<depth) - 1)) * (lut3d->lutsize - 1); \
  207. \
  208. for (y = slice_start; y < slice_end; y++) { \
  209. uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
  210. uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
  211. uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
  212. uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
  213. const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
  214. const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
  215. const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
  216. const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
  217. for (x = 0; x < in->width; x++) { \
  218. const struct rgbvec scaled_rgb = {srcr[x] * scale, \
  219. srcg[x] * scale, \
  220. srcb[x] * scale}; \
  221. struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
  222. dstr[x] = av_clip_uintp2(vec.r * (float)((1<<depth) - 1), depth); \
  223. dstg[x] = av_clip_uintp2(vec.g * (float)((1<<depth) - 1), depth); \
  224. dstb[x] = av_clip_uintp2(vec.b * (float)((1<<depth) - 1), depth); \
  225. if (!direct && in->linesize[3]) \
  226. dsta[x] = srca[x]; \
  227. } \
  228. grow += out->linesize[0]; \
  229. brow += out->linesize[1]; \
  230. rrow += out->linesize[2]; \
  231. arow += out->linesize[3]; \
  232. srcgrow += in->linesize[0]; \
  233. srcbrow += in->linesize[1]; \
  234. srcrrow += in->linesize[2]; \
  235. srcarow += in->linesize[3]; \
  236. } \
  237. return 0; \
  238. }
  239. DEFINE_INTERP_FUNC_PLANAR(nearest, 8, 8)
  240. DEFINE_INTERP_FUNC_PLANAR(trilinear, 8, 8)
  241. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 8, 8)
  242. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 9)
  243. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 9)
  244. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 9)
  245. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 10)
  246. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 10)
  247. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 10)
  248. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 12)
  249. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 12)
  250. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 12)
  251. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 14)
  252. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 14)
  253. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 14)
  254. DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 16)
  255. DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 16)
  256. DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 16)
  257. #define DEFINE_INTERP_FUNC(name, nbits) \
  258. static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  259. { \
  260. int x, y; \
  261. const LUT3DContext *lut3d = ctx->priv; \
  262. const ThreadData *td = arg; \
  263. const AVFrame *in = td->in; \
  264. const AVFrame *out = td->out; \
  265. const int direct = out == in; \
  266. const int step = lut3d->step; \
  267. const uint8_t r = lut3d->rgba_map[R]; \
  268. const uint8_t g = lut3d->rgba_map[G]; \
  269. const uint8_t b = lut3d->rgba_map[B]; \
  270. const uint8_t a = lut3d->rgba_map[A]; \
  271. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  272. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  273. uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
  274. const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
  275. const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
  276. \
  277. for (y = slice_start; y < slice_end; y++) { \
  278. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  279. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  280. for (x = 0; x < in->width * step; x += step) { \
  281. const struct rgbvec scaled_rgb = {src[x + r] * scale, \
  282. src[x + g] * scale, \
  283. src[x + b] * scale}; \
  284. struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
  285. dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
  286. dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
  287. dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
  288. if (!direct && step == 4) \
  289. dst[x + a] = src[x + a]; \
  290. } \
  291. dstrow += out->linesize[0]; \
  292. srcrow += in ->linesize[0]; \
  293. } \
  294. return 0; \
  295. }
  296. DEFINE_INTERP_FUNC(nearest, 8)
  297. DEFINE_INTERP_FUNC(trilinear, 8)
  298. DEFINE_INTERP_FUNC(tetrahedral, 8)
  299. DEFINE_INTERP_FUNC(nearest, 16)
  300. DEFINE_INTERP_FUNC(trilinear, 16)
  301. DEFINE_INTERP_FUNC(tetrahedral, 16)
  302. #define MAX_LINE_SIZE 512
  303. static int skip_line(const char *p)
  304. {
  305. while (*p && av_isspace(*p))
  306. p++;
  307. return !*p || *p == '#';
  308. }
  309. #define NEXT_LINE(loop_cond) do { \
  310. if (!fgets(line, sizeof(line), f)) { \
  311. av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
  312. return AVERROR_INVALIDDATA; \
  313. } \
  314. } while (loop_cond)
  315. /* Basically r g and b float values on each line, with a facultative 3DLUTSIZE
  316. * directive; seems to be generated by Davinci */
  317. static int parse_dat(AVFilterContext *ctx, FILE *f)
  318. {
  319. LUT3DContext *lut3d = ctx->priv;
  320. char line[MAX_LINE_SIZE];
  321. int i, j, k, size;
  322. lut3d->lutsize = size = 33;
  323. NEXT_LINE(skip_line(line));
  324. if (!strncmp(line, "3DLUTSIZE ", 10)) {
  325. size = strtol(line + 10, NULL, 0);
  326. if (size < 2 || size > MAX_LEVEL) {
  327. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  328. return AVERROR(EINVAL);
  329. }
  330. lut3d->lutsize = size;
  331. NEXT_LINE(skip_line(line));
  332. }
  333. for (k = 0; k < size; k++) {
  334. for (j = 0; j < size; j++) {
  335. for (i = 0; i < size; i++) {
  336. struct rgbvec *vec = &lut3d->lut[k][j][i];
  337. if (k != 0 || j != 0 || i != 0)
  338. NEXT_LINE(skip_line(line));
  339. if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  340. return AVERROR_INVALIDDATA;
  341. }
  342. }
  343. }
  344. return 0;
  345. }
  346. /* Iridas format */
  347. static int parse_cube(AVFilterContext *ctx, FILE *f)
  348. {
  349. LUT3DContext *lut3d = ctx->priv;
  350. char line[MAX_LINE_SIZE];
  351. float min[3] = {0.0, 0.0, 0.0};
  352. float max[3] = {1.0, 1.0, 1.0};
  353. while (fgets(line, sizeof(line), f)) {
  354. if (!strncmp(line, "LUT_3D_SIZE", 11)) {
  355. int i, j, k;
  356. const int size = strtol(line + 12, NULL, 0);
  357. if (size < 2 || size > MAX_LEVEL) {
  358. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  359. return AVERROR(EINVAL);
  360. }
  361. lut3d->lutsize = size;
  362. for (k = 0; k < size; k++) {
  363. for (j = 0; j < size; j++) {
  364. for (i = 0; i < size; i++) {
  365. struct rgbvec *vec = &lut3d->lut[i][j][k];
  366. do {
  367. try_again:
  368. NEXT_LINE(0);
  369. if (!strncmp(line, "DOMAIN_", 7)) {
  370. float *vals = NULL;
  371. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  372. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  373. if (!vals)
  374. return AVERROR_INVALIDDATA;
  375. av_sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  376. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  377. min[0], min[1], min[2], max[0], max[1], max[2]);
  378. goto try_again;
  379. } else if (!strncmp(line, "TITLE", 5)) {
  380. goto try_again;
  381. }
  382. } while (skip_line(line));
  383. if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  384. return AVERROR_INVALIDDATA;
  385. vec->r *= max[0] - min[0];
  386. vec->g *= max[1] - min[1];
  387. vec->b *= max[2] - min[2];
  388. }
  389. }
  390. }
  391. break;
  392. }
  393. }
  394. return 0;
  395. }
  396. /* Assume 17x17x17 LUT with a 16-bit depth
  397. * FIXME: it seems there are various 3dl formats */
  398. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  399. {
  400. char line[MAX_LINE_SIZE];
  401. LUT3DContext *lut3d = ctx->priv;
  402. int i, j, k;
  403. const int size = 17;
  404. const float scale = 16*16*16;
  405. lut3d->lutsize = size;
  406. NEXT_LINE(skip_line(line));
  407. for (k = 0; k < size; k++) {
  408. for (j = 0; j < size; j++) {
  409. for (i = 0; i < size; i++) {
  410. int r, g, b;
  411. struct rgbvec *vec = &lut3d->lut[k][j][i];
  412. NEXT_LINE(skip_line(line));
  413. if (av_sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  414. return AVERROR_INVALIDDATA;
  415. vec->r = r / scale;
  416. vec->g = g / scale;
  417. vec->b = b / scale;
  418. }
  419. }
  420. }
  421. return 0;
  422. }
  423. /* Pandora format */
  424. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  425. {
  426. LUT3DContext *lut3d = ctx->priv;
  427. float scale;
  428. int i, j, k, size, in = -1, out = -1;
  429. char line[MAX_LINE_SIZE];
  430. uint8_t rgb_map[3] = {0, 1, 2};
  431. while (fgets(line, sizeof(line), f)) {
  432. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  433. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  434. else if (!strncmp(line, "values", 6)) {
  435. const char *p = line + 6;
  436. #define SET_COLOR(id) do { \
  437. while (av_isspace(*p)) \
  438. p++; \
  439. switch (*p) { \
  440. case 'r': rgb_map[id] = 0; break; \
  441. case 'g': rgb_map[id] = 1; break; \
  442. case 'b': rgb_map[id] = 2; break; \
  443. } \
  444. while (*p && !av_isspace(*p)) \
  445. p++; \
  446. } while (0)
  447. SET_COLOR(0);
  448. SET_COLOR(1);
  449. SET_COLOR(2);
  450. break;
  451. }
  452. }
  453. if (in == -1 || out == -1) {
  454. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  455. return AVERROR_INVALIDDATA;
  456. }
  457. if (in < 2 || out < 2 ||
  458. in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
  459. out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
  460. av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
  461. return AVERROR_INVALIDDATA;
  462. }
  463. for (size = 1; size*size*size < in; size++);
  464. lut3d->lutsize = size;
  465. scale = 1. / (out - 1);
  466. for (k = 0; k < size; k++) {
  467. for (j = 0; j < size; j++) {
  468. for (i = 0; i < size; i++) {
  469. struct rgbvec *vec = &lut3d->lut[k][j][i];
  470. float val[3];
  471. NEXT_LINE(0);
  472. if (av_sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  473. return AVERROR_INVALIDDATA;
  474. vec->r = val[rgb_map[0]] * scale;
  475. vec->g = val[rgb_map[1]] * scale;
  476. vec->b = val[rgb_map[2]] * scale;
  477. }
  478. }
  479. }
  480. return 0;
  481. }
  482. static int parse_cinespace(AVFilterContext *ctx, FILE *f)
  483. {
  484. LUT3DContext *lut3d = ctx->priv;
  485. char line[MAX_LINE_SIZE];
  486. float in_min[3] = {0.0, 0.0, 0.0};
  487. float in_max[3] = {1.0, 1.0, 1.0};
  488. float out_min[3] = {0.0, 0.0, 0.0};
  489. float out_max[3] = {1.0, 1.0, 1.0};
  490. int inside_metadata = 0, size;
  491. NEXT_LINE(skip_line(line));
  492. if (strncmp(line, "CSPLUTV100", 10)) {
  493. av_log(ctx, AV_LOG_ERROR, "Not cineSpace LUT format\n");
  494. return AVERROR(EINVAL);
  495. }
  496. NEXT_LINE(skip_line(line));
  497. if (strncmp(line, "3D", 2)) {
  498. av_log(ctx, AV_LOG_ERROR, "Not 3D LUT format\n");
  499. return AVERROR(EINVAL);
  500. }
  501. while (1) {
  502. NEXT_LINE(skip_line(line));
  503. if (!strncmp(line, "BEGIN METADATA", 14)) {
  504. inside_metadata = 1;
  505. continue;
  506. }
  507. if (!strncmp(line, "END METADATA", 12)) {
  508. inside_metadata = 0;
  509. continue;
  510. }
  511. if (inside_metadata == 0) {
  512. int size_r, size_g, size_b;
  513. for (int i = 0; i < 3; i++) {
  514. int npoints = strtol(line, NULL, 0);
  515. if (npoints != 2) {
  516. av_log(ctx, AV_LOG_ERROR, "Unsupported number of pre-lut points.\n");
  517. return AVERROR_PATCHWELCOME;
  518. }
  519. NEXT_LINE(skip_line(line));
  520. if (av_sscanf(line, "%f %f", &in_min[i], &in_max[i]) != 2)
  521. return AVERROR_INVALIDDATA;
  522. NEXT_LINE(skip_line(line));
  523. if (av_sscanf(line, "%f %f", &out_min[i], &out_max[i]) != 2)
  524. return AVERROR_INVALIDDATA;
  525. NEXT_LINE(skip_line(line));
  526. }
  527. if (av_sscanf(line, "%d %d %d", &size_r, &size_g, &size_b) != 3)
  528. return AVERROR(EINVAL);
  529. if (size_r != size_g || size_r != size_b) {
  530. av_log(ctx, AV_LOG_ERROR, "Unsupported size combination: %dx%dx%d.\n", size_r, size_g, size_b);
  531. return AVERROR_PATCHWELCOME;
  532. }
  533. size = size_r;
  534. if (size < 2 || size > MAX_LEVEL) {
  535. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  536. return AVERROR(EINVAL);
  537. }
  538. lut3d->lutsize = size;
  539. for (int k = 0; k < size; k++) {
  540. for (int j = 0; j < size; j++) {
  541. for (int i = 0; i < size; i++) {
  542. struct rgbvec *vec = &lut3d->lut[i][j][k];
  543. if (k != 0 || j != 0 || i != 0)
  544. NEXT_LINE(skip_line(line));
  545. if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  546. return AVERROR_INVALIDDATA;
  547. vec->r *= out_max[0] - out_min[0];
  548. vec->g *= out_max[1] - out_min[1];
  549. vec->b *= out_max[2] - out_min[2];
  550. }
  551. }
  552. }
  553. break;
  554. }
  555. }
  556. return 0;
  557. }
  558. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  559. {
  560. int i, j, k;
  561. const float c = 1. / (size - 1);
  562. lut3d->lutsize = size;
  563. for (k = 0; k < size; k++) {
  564. for (j = 0; j < size; j++) {
  565. for (i = 0; i < size; i++) {
  566. struct rgbvec *vec = &lut3d->lut[k][j][i];
  567. vec->r = k * c;
  568. vec->g = j * c;
  569. vec->b = i * c;
  570. }
  571. }
  572. }
  573. }
  574. static int query_formats(AVFilterContext *ctx)
  575. {
  576. static const enum AVPixelFormat pix_fmts[] = {
  577. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  578. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  579. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  580. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  581. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  582. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  583. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  584. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  585. AV_PIX_FMT_GBRP9,
  586. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
  587. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  588. AV_PIX_FMT_GBRP14,
  589. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  590. AV_PIX_FMT_NONE
  591. };
  592. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  593. if (!fmts_list)
  594. return AVERROR(ENOMEM);
  595. return ff_set_common_formats(ctx, fmts_list);
  596. }
  597. static int config_input(AVFilterLink *inlink)
  598. {
  599. int depth, is16bit = 0, planar = 0;
  600. LUT3DContext *lut3d = inlink->dst->priv;
  601. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  602. depth = desc->comp[0].depth;
  603. switch (inlink->format) {
  604. case AV_PIX_FMT_RGB48:
  605. case AV_PIX_FMT_BGR48:
  606. case AV_PIX_FMT_RGBA64:
  607. case AV_PIX_FMT_BGRA64:
  608. is16bit = 1;
  609. break;
  610. case AV_PIX_FMT_GBRP9:
  611. case AV_PIX_FMT_GBRP10:
  612. case AV_PIX_FMT_GBRP12:
  613. case AV_PIX_FMT_GBRP14:
  614. case AV_PIX_FMT_GBRP16:
  615. case AV_PIX_FMT_GBRAP10:
  616. case AV_PIX_FMT_GBRAP12:
  617. case AV_PIX_FMT_GBRAP16:
  618. is16bit = 1;
  619. case AV_PIX_FMT_GBRP:
  620. case AV_PIX_FMT_GBRAP:
  621. planar = 1;
  622. break;
  623. }
  624. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  625. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  626. #define SET_FUNC(name) do { \
  627. if (planar) { \
  628. switch (depth) { \
  629. case 8: lut3d->interp = interp_8_##name##_p8; break; \
  630. case 9: lut3d->interp = interp_16_##name##_p9; break; \
  631. case 10: lut3d->interp = interp_16_##name##_p10; break; \
  632. case 12: lut3d->interp = interp_16_##name##_p12; break; \
  633. case 14: lut3d->interp = interp_16_##name##_p14; break; \
  634. case 16: lut3d->interp = interp_16_##name##_p16; break; \
  635. } \
  636. } else if (is16bit) { lut3d->interp = interp_16_##name; \
  637. } else { lut3d->interp = interp_8_##name; } \
  638. } while (0)
  639. switch (lut3d->interpolation) {
  640. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  641. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  642. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  643. default:
  644. av_assert0(0);
  645. }
  646. return 0;
  647. }
  648. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  649. {
  650. AVFilterContext *ctx = inlink->dst;
  651. LUT3DContext *lut3d = ctx->priv;
  652. AVFilterLink *outlink = inlink->dst->outputs[0];
  653. AVFrame *out;
  654. ThreadData td;
  655. if (av_frame_is_writable(in)) {
  656. out = in;
  657. } else {
  658. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  659. if (!out) {
  660. av_frame_free(&in);
  661. return NULL;
  662. }
  663. av_frame_copy_props(out, in);
  664. }
  665. td.in = in;
  666. td.out = out;
  667. ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  668. if (out != in)
  669. av_frame_free(&in);
  670. return out;
  671. }
  672. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  673. {
  674. AVFilterLink *outlink = inlink->dst->outputs[0];
  675. AVFrame *out = apply_lut(inlink, in);
  676. if (!out)
  677. return AVERROR(ENOMEM);
  678. return ff_filter_frame(outlink, out);
  679. }
  680. #if CONFIG_LUT3D_FILTER
  681. static const AVOption lut3d_options[] = {
  682. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  683. COMMON_OPTIONS
  684. };
  685. AVFILTER_DEFINE_CLASS(lut3d);
  686. static av_cold int lut3d_init(AVFilterContext *ctx)
  687. {
  688. int ret;
  689. FILE *f;
  690. const char *ext;
  691. LUT3DContext *lut3d = ctx->priv;
  692. if (!lut3d->file) {
  693. set_identity_matrix(lut3d, 32);
  694. return 0;
  695. }
  696. f = fopen(lut3d->file, "r");
  697. if (!f) {
  698. ret = AVERROR(errno);
  699. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  700. return ret;
  701. }
  702. ext = strrchr(lut3d->file, '.');
  703. if (!ext) {
  704. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  705. ret = AVERROR_INVALIDDATA;
  706. goto end;
  707. }
  708. ext++;
  709. if (!av_strcasecmp(ext, "dat")) {
  710. ret = parse_dat(ctx, f);
  711. } else if (!av_strcasecmp(ext, "3dl")) {
  712. ret = parse_3dl(ctx, f);
  713. } else if (!av_strcasecmp(ext, "cube")) {
  714. ret = parse_cube(ctx, f);
  715. } else if (!av_strcasecmp(ext, "m3d")) {
  716. ret = parse_m3d(ctx, f);
  717. } else if (!av_strcasecmp(ext, "csp")) {
  718. ret = parse_cinespace(ctx, f);
  719. } else {
  720. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  721. ret = AVERROR(EINVAL);
  722. }
  723. if (!ret && !lut3d->lutsize) {
  724. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  725. ret = AVERROR_INVALIDDATA;
  726. }
  727. end:
  728. fclose(f);
  729. return ret;
  730. }
  731. static const AVFilterPad lut3d_inputs[] = {
  732. {
  733. .name = "default",
  734. .type = AVMEDIA_TYPE_VIDEO,
  735. .filter_frame = filter_frame,
  736. .config_props = config_input,
  737. },
  738. { NULL }
  739. };
  740. static const AVFilterPad lut3d_outputs[] = {
  741. {
  742. .name = "default",
  743. .type = AVMEDIA_TYPE_VIDEO,
  744. },
  745. { NULL }
  746. };
  747. AVFilter ff_vf_lut3d = {
  748. .name = "lut3d",
  749. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  750. .priv_size = sizeof(LUT3DContext),
  751. .init = lut3d_init,
  752. .query_formats = query_formats,
  753. .inputs = lut3d_inputs,
  754. .outputs = lut3d_outputs,
  755. .priv_class = &lut3d_class,
  756. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  757. };
  758. #endif
  759. #if CONFIG_HALDCLUT_FILTER
  760. static void update_clut_packed(LUT3DContext *lut3d, const AVFrame *frame)
  761. {
  762. const uint8_t *data = frame->data[0];
  763. const int linesize = frame->linesize[0];
  764. const int w = lut3d->clut_width;
  765. const int step = lut3d->clut_step;
  766. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  767. const int level = lut3d->lutsize;
  768. #define LOAD_CLUT(nbits) do { \
  769. int i, j, k, x = 0, y = 0; \
  770. \
  771. for (k = 0; k < level; k++) { \
  772. for (j = 0; j < level; j++) { \
  773. for (i = 0; i < level; i++) { \
  774. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  775. (data + y*linesize + x*step); \
  776. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  777. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  778. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  779. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  780. if (++x == w) { \
  781. x = 0; \
  782. y++; \
  783. } \
  784. } \
  785. } \
  786. } \
  787. } while (0)
  788. switch (lut3d->clut_bits) {
  789. case 8: LOAD_CLUT(8); break;
  790. case 16: LOAD_CLUT(16); break;
  791. }
  792. }
  793. static void update_clut_planar(LUT3DContext *lut3d, const AVFrame *frame)
  794. {
  795. const uint8_t *datag = frame->data[0];
  796. const uint8_t *datab = frame->data[1];
  797. const uint8_t *datar = frame->data[2];
  798. const int glinesize = frame->linesize[0];
  799. const int blinesize = frame->linesize[1];
  800. const int rlinesize = frame->linesize[2];
  801. const int w = lut3d->clut_width;
  802. const int level = lut3d->lutsize;
  803. #define LOAD_CLUT_PLANAR(nbits, depth) do { \
  804. int i, j, k, x = 0, y = 0; \
  805. \
  806. for (k = 0; k < level; k++) { \
  807. for (j = 0; j < level; j++) { \
  808. for (i = 0; i < level; i++) { \
  809. const uint##nbits##_t *gsrc = (const uint##nbits##_t *) \
  810. (datag + y*glinesize); \
  811. const uint##nbits##_t *bsrc = (const uint##nbits##_t *) \
  812. (datab + y*blinesize); \
  813. const uint##nbits##_t *rsrc = (const uint##nbits##_t *) \
  814. (datar + y*rlinesize); \
  815. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  816. vec->r = gsrc[x] / (float)((1<<(depth)) - 1); \
  817. vec->g = bsrc[x] / (float)((1<<(depth)) - 1); \
  818. vec->b = rsrc[x] / (float)((1<<(depth)) - 1); \
  819. if (++x == w) { \
  820. x = 0; \
  821. y++; \
  822. } \
  823. } \
  824. } \
  825. } \
  826. } while (0)
  827. switch (lut3d->clut_bits) {
  828. case 8: LOAD_CLUT_PLANAR(8, 8); break;
  829. case 9: LOAD_CLUT_PLANAR(16, 9); break;
  830. case 10: LOAD_CLUT_PLANAR(16, 10); break;
  831. case 12: LOAD_CLUT_PLANAR(16, 12); break;
  832. case 14: LOAD_CLUT_PLANAR(16, 14); break;
  833. case 16: LOAD_CLUT_PLANAR(16, 16); break;
  834. }
  835. }
  836. static int config_output(AVFilterLink *outlink)
  837. {
  838. AVFilterContext *ctx = outlink->src;
  839. LUT3DContext *lut3d = ctx->priv;
  840. int ret;
  841. ret = ff_framesync_init_dualinput(&lut3d->fs, ctx);
  842. if (ret < 0)
  843. return ret;
  844. outlink->w = ctx->inputs[0]->w;
  845. outlink->h = ctx->inputs[0]->h;
  846. outlink->time_base = ctx->inputs[0]->time_base;
  847. if ((ret = ff_framesync_configure(&lut3d->fs)) < 0)
  848. return ret;
  849. return 0;
  850. }
  851. static int activate(AVFilterContext *ctx)
  852. {
  853. LUT3DContext *s = ctx->priv;
  854. return ff_framesync_activate(&s->fs);
  855. }
  856. static int config_clut(AVFilterLink *inlink)
  857. {
  858. int size, level, w, h;
  859. AVFilterContext *ctx = inlink->dst;
  860. LUT3DContext *lut3d = ctx->priv;
  861. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  862. av_assert0(desc);
  863. lut3d->clut_bits = desc->comp[0].depth;
  864. lut3d->clut_planar = av_pix_fmt_count_planes(inlink->format) > 1;
  865. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  866. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  867. if (inlink->w > inlink->h)
  868. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  869. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  870. else if (inlink->w < inlink->h)
  871. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  872. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  873. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  874. for (level = 1; level*level*level < w; level++);
  875. size = level*level*level;
  876. if (size != w) {
  877. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  878. return AVERROR_INVALIDDATA;
  879. }
  880. av_assert0(w == h && w == size);
  881. level *= level;
  882. if (level > MAX_LEVEL) {
  883. const int max_clut_level = sqrt(MAX_LEVEL);
  884. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  885. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  886. "(maximum level is %d, or %dx%d CLUT)\n",
  887. max_clut_level, max_clut_size, max_clut_size);
  888. return AVERROR(EINVAL);
  889. }
  890. lut3d->lutsize = level;
  891. return 0;
  892. }
  893. static int update_apply_clut(FFFrameSync *fs)
  894. {
  895. AVFilterContext *ctx = fs->parent;
  896. LUT3DContext *lut3d = ctx->priv;
  897. AVFilterLink *inlink = ctx->inputs[0];
  898. AVFrame *master, *second, *out;
  899. int ret;
  900. ret = ff_framesync_dualinput_get(fs, &master, &second);
  901. if (ret < 0)
  902. return ret;
  903. if (!second)
  904. return ff_filter_frame(ctx->outputs[0], master);
  905. if (lut3d->clut_planar)
  906. update_clut_planar(ctx->priv, second);
  907. else
  908. update_clut_packed(ctx->priv, second);
  909. out = apply_lut(inlink, master);
  910. return ff_filter_frame(ctx->outputs[0], out);
  911. }
  912. static av_cold int haldclut_init(AVFilterContext *ctx)
  913. {
  914. LUT3DContext *lut3d = ctx->priv;
  915. lut3d->fs.on_event = update_apply_clut;
  916. return 0;
  917. }
  918. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  919. {
  920. LUT3DContext *lut3d = ctx->priv;
  921. ff_framesync_uninit(&lut3d->fs);
  922. }
  923. static const AVOption haldclut_options[] = {
  924. COMMON_OPTIONS
  925. };
  926. FRAMESYNC_DEFINE_CLASS(haldclut, LUT3DContext, fs);
  927. static const AVFilterPad haldclut_inputs[] = {
  928. {
  929. .name = "main",
  930. .type = AVMEDIA_TYPE_VIDEO,
  931. .config_props = config_input,
  932. },{
  933. .name = "clut",
  934. .type = AVMEDIA_TYPE_VIDEO,
  935. .config_props = config_clut,
  936. },
  937. { NULL }
  938. };
  939. static const AVFilterPad haldclut_outputs[] = {
  940. {
  941. .name = "default",
  942. .type = AVMEDIA_TYPE_VIDEO,
  943. .config_props = config_output,
  944. },
  945. { NULL }
  946. };
  947. AVFilter ff_vf_haldclut = {
  948. .name = "haldclut",
  949. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  950. .priv_size = sizeof(LUT3DContext),
  951. .preinit = haldclut_framesync_preinit,
  952. .init = haldclut_init,
  953. .uninit = haldclut_uninit,
  954. .query_formats = query_formats,
  955. .activate = activate,
  956. .inputs = haldclut_inputs,
  957. .outputs = haldclut_outputs,
  958. .priv_class = &haldclut_class,
  959. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  960. };
  961. #endif
  962. #if CONFIG_LUT1D_FILTER
  963. enum interp_1d_mode {
  964. INTERPOLATE_1D_NEAREST,
  965. INTERPOLATE_1D_LINEAR,
  966. INTERPOLATE_1D_CUBIC,
  967. INTERPOLATE_1D_COSINE,
  968. INTERPOLATE_1D_SPLINE,
  969. NB_INTERP_1D_MODE
  970. };
  971. #define MAX_1D_LEVEL 65536
  972. typedef struct LUT1DContext {
  973. const AVClass *class;
  974. char *file;
  975. int interpolation; ///<interp_1d_mode
  976. uint8_t rgba_map[4];
  977. int step;
  978. float lut[3][MAX_1D_LEVEL];
  979. int lutsize;
  980. avfilter_action_func *interp;
  981. } LUT1DContext;
  982. #undef OFFSET
  983. #define OFFSET(x) offsetof(LUT1DContext, x)
  984. static void set_identity_matrix_1d(LUT1DContext *lut1d, int size)
  985. {
  986. const float c = 1. / (size - 1);
  987. int i;
  988. lut1d->lutsize = size;
  989. for (i = 0; i < size; i++) {
  990. lut1d->lut[0][i] = i * c;
  991. lut1d->lut[1][i] = i * c;
  992. lut1d->lut[2][i] = i * c;
  993. }
  994. }
  995. static int parse_cinespace_1d(AVFilterContext *ctx, FILE *f)
  996. {
  997. LUT1DContext *lut1d = ctx->priv;
  998. char line[MAX_LINE_SIZE];
  999. float in_min[3] = {0.0, 0.0, 0.0};
  1000. float in_max[3] = {1.0, 1.0, 1.0};
  1001. float out_min[3] = {0.0, 0.0, 0.0};
  1002. float out_max[3] = {1.0, 1.0, 1.0};
  1003. int inside_metadata = 0, size;
  1004. NEXT_LINE(skip_line(line));
  1005. if (strncmp(line, "CSPLUTV100", 10)) {
  1006. av_log(ctx, AV_LOG_ERROR, "Not cineSpace LUT format\n");
  1007. return AVERROR(EINVAL);
  1008. }
  1009. NEXT_LINE(skip_line(line));
  1010. if (strncmp(line, "1D", 2)) {
  1011. av_log(ctx, AV_LOG_ERROR, "Not 1D LUT format\n");
  1012. return AVERROR(EINVAL);
  1013. }
  1014. while (1) {
  1015. NEXT_LINE(skip_line(line));
  1016. if (!strncmp(line, "BEGIN METADATA", 14)) {
  1017. inside_metadata = 1;
  1018. continue;
  1019. }
  1020. if (!strncmp(line, "END METADATA", 12)) {
  1021. inside_metadata = 0;
  1022. continue;
  1023. }
  1024. if (inside_metadata == 0) {
  1025. for (int i = 0; i < 3; i++) {
  1026. int npoints = strtol(line, NULL, 0);
  1027. if (npoints != 2) {
  1028. av_log(ctx, AV_LOG_ERROR, "Unsupported number of pre-lut points.\n");
  1029. return AVERROR_PATCHWELCOME;
  1030. }
  1031. NEXT_LINE(skip_line(line));
  1032. if (av_sscanf(line, "%f %f", &in_min[i], &in_max[i]) != 2)
  1033. return AVERROR_INVALIDDATA;
  1034. NEXT_LINE(skip_line(line));
  1035. if (av_sscanf(line, "%f %f", &out_min[i], &out_max[i]) != 2)
  1036. return AVERROR_INVALIDDATA;
  1037. NEXT_LINE(skip_line(line));
  1038. }
  1039. size = strtol(line, NULL, 0);
  1040. if (size < 2 || size > MAX_1D_LEVEL) {
  1041. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 1D LUT size\n");
  1042. return AVERROR(EINVAL);
  1043. }
  1044. lut1d->lutsize = size;
  1045. for (int i = 0; i < size; i++) {
  1046. NEXT_LINE(skip_line(line));
  1047. if (av_sscanf(line, "%f %f %f", &lut1d->lut[0][i], &lut1d->lut[1][i], &lut1d->lut[2][i]) != 3)
  1048. return AVERROR_INVALIDDATA;
  1049. lut1d->lut[0][i] *= out_max[0] - out_min[0];
  1050. lut1d->lut[1][i] *= out_max[1] - out_min[1];
  1051. lut1d->lut[2][i] *= out_max[2] - out_min[2];
  1052. }
  1053. break;
  1054. }
  1055. }
  1056. return 0;
  1057. }
  1058. static int parse_cube_1d(AVFilterContext *ctx, FILE *f)
  1059. {
  1060. LUT1DContext *lut1d = ctx->priv;
  1061. char line[MAX_LINE_SIZE];
  1062. float min[3] = {0.0, 0.0, 0.0};
  1063. float max[3] = {1.0, 1.0, 1.0};
  1064. while (fgets(line, sizeof(line), f)) {
  1065. if (!strncmp(line, "LUT_1D_SIZE", 11)) {
  1066. const int size = strtol(line + 12, NULL, 0);
  1067. int i;
  1068. if (size < 2 || size > MAX_1D_LEVEL) {
  1069. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 1D LUT size\n");
  1070. return AVERROR(EINVAL);
  1071. }
  1072. lut1d->lutsize = size;
  1073. for (i = 0; i < size; i++) {
  1074. do {
  1075. try_again:
  1076. NEXT_LINE(0);
  1077. if (!strncmp(line, "DOMAIN_", 7)) {
  1078. float *vals = NULL;
  1079. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  1080. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  1081. if (!vals)
  1082. return AVERROR_INVALIDDATA;
  1083. av_sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  1084. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  1085. min[0], min[1], min[2], max[0], max[1], max[2]);
  1086. goto try_again;
  1087. } else if (!strncmp(line, "LUT_1D_INPUT_RANGE ", 19)) {
  1088. av_sscanf(line + 19, "%f %f", min, max);
  1089. min[1] = min[2] = min[0];
  1090. max[1] = max[2] = max[0];
  1091. goto try_again;
  1092. } else if (!strncmp(line, "TITLE", 5)) {
  1093. goto try_again;
  1094. }
  1095. } while (skip_line(line));
  1096. if (av_sscanf(line, "%f %f %f", &lut1d->lut[0][i], &lut1d->lut[1][i], &lut1d->lut[2][i]) != 3)
  1097. return AVERROR_INVALIDDATA;
  1098. lut1d->lut[0][i] *= max[0] - min[0];
  1099. lut1d->lut[1][i] *= max[1] - min[1];
  1100. lut1d->lut[2][i] *= max[2] - min[2];
  1101. }
  1102. break;
  1103. }
  1104. }
  1105. return 0;
  1106. }
  1107. static const AVOption lut1d_options[] = {
  1108. { "file", "set 1D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  1109. { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_1D_LINEAR}, 0, NB_INTERP_1D_MODE-1, FLAGS, "interp_mode" },
  1110. { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  1111. { "linear", "use values from the linear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_LINEAR}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  1112. { "cosine", "use values from the cosine interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_COSINE}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  1113. { "cubic", "use values from the cubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_CUBIC}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  1114. { "spline", "use values from the spline interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_1D_SPLINE}, INT_MIN, INT_MAX, FLAGS, "interp_mode" },
  1115. { NULL }
  1116. };
  1117. AVFILTER_DEFINE_CLASS(lut1d);
  1118. static inline float interp_1d_nearest(const LUT1DContext *lut1d,
  1119. int idx, const float s)
  1120. {
  1121. return lut1d->lut[idx][NEAR(s)];
  1122. }
  1123. #define NEXT1D(x) (FFMIN((int)(x) + 1, lut1d->lutsize - 1))
  1124. static inline float interp_1d_linear(const LUT1DContext *lut1d,
  1125. int idx, const float s)
  1126. {
  1127. const int prev = PREV(s);
  1128. const int next = NEXT1D(s);
  1129. const float d = s - prev;
  1130. const float p = lut1d->lut[idx][prev];
  1131. const float n = lut1d->lut[idx][next];
  1132. return lerpf(p, n, d);
  1133. }
  1134. static inline float interp_1d_cosine(const LUT1DContext *lut1d,
  1135. int idx, const float s)
  1136. {
  1137. const int prev = PREV(s);
  1138. const int next = NEXT1D(s);
  1139. const float d = s - prev;
  1140. const float p = lut1d->lut[idx][prev];
  1141. const float n = lut1d->lut[idx][next];
  1142. const float m = (1.f - cosf(d * M_PI)) * .5f;
  1143. return lerpf(p, n, m);
  1144. }
  1145. static inline float interp_1d_cubic(const LUT1DContext *lut1d,
  1146. int idx, const float s)
  1147. {
  1148. const int prev = PREV(s);
  1149. const int next = NEXT1D(s);
  1150. const float mu = s - prev;
  1151. float a0, a1, a2, a3, mu2;
  1152. float y0 = lut1d->lut[idx][FFMAX(prev - 1, 0)];
  1153. float y1 = lut1d->lut[idx][prev];
  1154. float y2 = lut1d->lut[idx][next];
  1155. float y3 = lut1d->lut[idx][FFMIN(next + 1, lut1d->lutsize - 1)];
  1156. mu2 = mu * mu;
  1157. a0 = y3 - y2 - y0 + y1;
  1158. a1 = y0 - y1 - a0;
  1159. a2 = y2 - y0;
  1160. a3 = y1;
  1161. return a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3;
  1162. }
  1163. static inline float interp_1d_spline(const LUT1DContext *lut1d,
  1164. int idx, const float s)
  1165. {
  1166. const int prev = PREV(s);
  1167. const int next = NEXT1D(s);
  1168. const float x = s - prev;
  1169. float c0, c1, c2, c3;
  1170. float y0 = lut1d->lut[idx][FFMAX(prev - 1, 0)];
  1171. float y1 = lut1d->lut[idx][prev];
  1172. float y2 = lut1d->lut[idx][next];
  1173. float y3 = lut1d->lut[idx][FFMIN(next + 1, lut1d->lutsize - 1)];
  1174. c0 = y1;
  1175. c1 = .5f * (y2 - y0);
  1176. c2 = y0 - 2.5f * y1 + 2.f * y2 - .5f * y3;
  1177. c3 = .5f * (y3 - y0) + 1.5f * (y1 - y2);
  1178. return ((c3 * x + c2) * x + c1) * x + c0;
  1179. }
  1180. #define DEFINE_INTERP_FUNC_PLANAR_1D(name, nbits, depth) \
  1181. static int interp_1d_##nbits##_##name##_p##depth(AVFilterContext *ctx, \
  1182. void *arg, int jobnr, \
  1183. int nb_jobs) \
  1184. { \
  1185. int x, y; \
  1186. const LUT1DContext *lut1d = ctx->priv; \
  1187. const ThreadData *td = arg; \
  1188. const AVFrame *in = td->in; \
  1189. const AVFrame *out = td->out; \
  1190. const int direct = out == in; \
  1191. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  1192. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  1193. uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
  1194. uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
  1195. uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
  1196. uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
  1197. const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
  1198. const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
  1199. const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
  1200. const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
  1201. const float factor = (1 << depth) - 1; \
  1202. const float scale = (1. / factor) * (lut1d->lutsize - 1); \
  1203. \
  1204. for (y = slice_start; y < slice_end; y++) { \
  1205. uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
  1206. uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
  1207. uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
  1208. uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
  1209. const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
  1210. const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
  1211. const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
  1212. const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
  1213. for (x = 0; x < in->width; x++) { \
  1214. float r = srcr[x] * scale; \
  1215. float g = srcg[x] * scale; \
  1216. float b = srcb[x] * scale; \
  1217. r = interp_1d_##name(lut1d, 0, r); \
  1218. g = interp_1d_##name(lut1d, 1, g); \
  1219. b = interp_1d_##name(lut1d, 2, b); \
  1220. dstr[x] = av_clip_uintp2(r * factor, depth); \
  1221. dstg[x] = av_clip_uintp2(g * factor, depth); \
  1222. dstb[x] = av_clip_uintp2(b * factor, depth); \
  1223. if (!direct && in->linesize[3]) \
  1224. dsta[x] = srca[x]; \
  1225. } \
  1226. grow += out->linesize[0]; \
  1227. brow += out->linesize[1]; \
  1228. rrow += out->linesize[2]; \
  1229. arow += out->linesize[3]; \
  1230. srcgrow += in->linesize[0]; \
  1231. srcbrow += in->linesize[1]; \
  1232. srcrrow += in->linesize[2]; \
  1233. srcarow += in->linesize[3]; \
  1234. } \
  1235. return 0; \
  1236. }
  1237. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 8, 8)
  1238. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 8, 8)
  1239. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 8, 8)
  1240. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 8, 8)
  1241. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 8, 8)
  1242. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 9)
  1243. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 9)
  1244. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 9)
  1245. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 9)
  1246. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 9)
  1247. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 10)
  1248. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 10)
  1249. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 10)
  1250. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 10)
  1251. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 10)
  1252. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 12)
  1253. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 12)
  1254. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 12)
  1255. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 12)
  1256. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 12)
  1257. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 14)
  1258. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 14)
  1259. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 14)
  1260. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 14)
  1261. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 14)
  1262. DEFINE_INTERP_FUNC_PLANAR_1D(nearest, 16, 16)
  1263. DEFINE_INTERP_FUNC_PLANAR_1D(linear, 16, 16)
  1264. DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 16)
  1265. DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 16)
  1266. DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 16)
  1267. #define DEFINE_INTERP_FUNC_1D(name, nbits) \
  1268. static int interp_1d_##nbits##_##name(AVFilterContext *ctx, void *arg, \
  1269. int jobnr, int nb_jobs) \
  1270. { \
  1271. int x, y; \
  1272. const LUT1DContext *lut1d = ctx->priv; \
  1273. const ThreadData *td = arg; \
  1274. const AVFrame *in = td->in; \
  1275. const AVFrame *out = td->out; \
  1276. const int direct = out == in; \
  1277. const int step = lut1d->step; \
  1278. const uint8_t r = lut1d->rgba_map[R]; \
  1279. const uint8_t g = lut1d->rgba_map[G]; \
  1280. const uint8_t b = lut1d->rgba_map[B]; \
  1281. const uint8_t a = lut1d->rgba_map[A]; \
  1282. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  1283. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  1284. uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
  1285. const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
  1286. const float factor = (1 << nbits) - 1; \
  1287. const float scale = (1. / factor) * (lut1d->lutsize - 1); \
  1288. \
  1289. for (y = slice_start; y < slice_end; y++) { \
  1290. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  1291. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  1292. for (x = 0; x < in->width * step; x += step) { \
  1293. float rr = src[x + r] * scale; \
  1294. float gg = src[x + g] * scale; \
  1295. float bb = src[x + b] * scale; \
  1296. rr = interp_1d_##name(lut1d, 0, rr); \
  1297. gg = interp_1d_##name(lut1d, 1, gg); \
  1298. bb = interp_1d_##name(lut1d, 2, bb); \
  1299. dst[x + r] = av_clip_uint##nbits(rr * factor); \
  1300. dst[x + g] = av_clip_uint##nbits(gg * factor); \
  1301. dst[x + b] = av_clip_uint##nbits(bb * factor); \
  1302. if (!direct && step == 4) \
  1303. dst[x + a] = src[x + a]; \
  1304. } \
  1305. dstrow += out->linesize[0]; \
  1306. srcrow += in ->linesize[0]; \
  1307. } \
  1308. return 0; \
  1309. }
  1310. DEFINE_INTERP_FUNC_1D(nearest, 8)
  1311. DEFINE_INTERP_FUNC_1D(linear, 8)
  1312. DEFINE_INTERP_FUNC_1D(cosine, 8)
  1313. DEFINE_INTERP_FUNC_1D(cubic, 8)
  1314. DEFINE_INTERP_FUNC_1D(spline, 8)
  1315. DEFINE_INTERP_FUNC_1D(nearest, 16)
  1316. DEFINE_INTERP_FUNC_1D(linear, 16)
  1317. DEFINE_INTERP_FUNC_1D(cosine, 16)
  1318. DEFINE_INTERP_FUNC_1D(cubic, 16)
  1319. DEFINE_INTERP_FUNC_1D(spline, 16)
  1320. static int config_input_1d(AVFilterLink *inlink)
  1321. {
  1322. int depth, is16bit = 0, planar = 0;
  1323. LUT1DContext *lut1d = inlink->dst->priv;
  1324. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  1325. depth = desc->comp[0].depth;
  1326. switch (inlink->format) {
  1327. case AV_PIX_FMT_RGB48:
  1328. case AV_PIX_FMT_BGR48:
  1329. case AV_PIX_FMT_RGBA64:
  1330. case AV_PIX_FMT_BGRA64:
  1331. is16bit = 1;
  1332. break;
  1333. case AV_PIX_FMT_GBRP9:
  1334. case AV_PIX_FMT_GBRP10:
  1335. case AV_PIX_FMT_GBRP12:
  1336. case AV_PIX_FMT_GBRP14:
  1337. case AV_PIX_FMT_GBRP16:
  1338. case AV_PIX_FMT_GBRAP10:
  1339. case AV_PIX_FMT_GBRAP12:
  1340. case AV_PIX_FMT_GBRAP16:
  1341. is16bit = 1;
  1342. case AV_PIX_FMT_GBRP:
  1343. case AV_PIX_FMT_GBRAP:
  1344. planar = 1;
  1345. break;
  1346. }
  1347. ff_fill_rgba_map(lut1d->rgba_map, inlink->format);
  1348. lut1d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  1349. #define SET_FUNC_1D(name) do { \
  1350. if (planar) { \
  1351. switch (depth) { \
  1352. case 8: lut1d->interp = interp_1d_8_##name##_p8; break; \
  1353. case 9: lut1d->interp = interp_1d_16_##name##_p9; break; \
  1354. case 10: lut1d->interp = interp_1d_16_##name##_p10; break; \
  1355. case 12: lut1d->interp = interp_1d_16_##name##_p12; break; \
  1356. case 14: lut1d->interp = interp_1d_16_##name##_p14; break; \
  1357. case 16: lut1d->interp = interp_1d_16_##name##_p16; break; \
  1358. } \
  1359. } else if (is16bit) { lut1d->interp = interp_1d_16_##name; \
  1360. } else { lut1d->interp = interp_1d_8_##name; } \
  1361. } while (0)
  1362. switch (lut1d->interpolation) {
  1363. case INTERPOLATE_1D_NEAREST: SET_FUNC_1D(nearest); break;
  1364. case INTERPOLATE_1D_LINEAR: SET_FUNC_1D(linear); break;
  1365. case INTERPOLATE_1D_COSINE: SET_FUNC_1D(cosine); break;
  1366. case INTERPOLATE_1D_CUBIC: SET_FUNC_1D(cubic); break;
  1367. case INTERPOLATE_1D_SPLINE: SET_FUNC_1D(spline); break;
  1368. default:
  1369. av_assert0(0);
  1370. }
  1371. return 0;
  1372. }
  1373. static av_cold int lut1d_init(AVFilterContext *ctx)
  1374. {
  1375. int ret;
  1376. FILE *f;
  1377. const char *ext;
  1378. LUT1DContext *lut1d = ctx->priv;
  1379. if (!lut1d->file) {
  1380. set_identity_matrix_1d(lut1d, 32);
  1381. return 0;
  1382. }
  1383. f = fopen(lut1d->file, "r");
  1384. if (!f) {
  1385. ret = AVERROR(errno);
  1386. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut1d->file, av_err2str(ret));
  1387. return ret;
  1388. }
  1389. ext = strrchr(lut1d->file, '.');
  1390. if (!ext) {
  1391. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  1392. ret = AVERROR_INVALIDDATA;
  1393. goto end;
  1394. }
  1395. ext++;
  1396. if (!av_strcasecmp(ext, "cube") || !av_strcasecmp(ext, "1dlut")) {
  1397. ret = parse_cube_1d(ctx, f);
  1398. } else if (!av_strcasecmp(ext, "csp")) {
  1399. ret = parse_cinespace_1d(ctx, f);
  1400. } else {
  1401. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  1402. ret = AVERROR(EINVAL);
  1403. }
  1404. if (!ret && !lut1d->lutsize) {
  1405. av_log(ctx, AV_LOG_ERROR, "1D LUT is empty\n");
  1406. ret = AVERROR_INVALIDDATA;
  1407. }
  1408. end:
  1409. fclose(f);
  1410. return ret;
  1411. }
  1412. static AVFrame *apply_1d_lut(AVFilterLink *inlink, AVFrame *in)
  1413. {
  1414. AVFilterContext *ctx = inlink->dst;
  1415. LUT1DContext *lut1d = ctx->priv;
  1416. AVFilterLink *outlink = inlink->dst->outputs[0];
  1417. AVFrame *out;
  1418. ThreadData td;
  1419. if (av_frame_is_writable(in)) {
  1420. out = in;
  1421. } else {
  1422. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  1423. if (!out) {
  1424. av_frame_free(&in);
  1425. return NULL;
  1426. }
  1427. av_frame_copy_props(out, in);
  1428. }
  1429. td.in = in;
  1430. td.out = out;
  1431. ctx->internal->execute(ctx, lut1d->interp, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  1432. if (out != in)
  1433. av_frame_free(&in);
  1434. return out;
  1435. }
  1436. static int filter_frame_1d(AVFilterLink *inlink, AVFrame *in)
  1437. {
  1438. AVFilterLink *outlink = inlink->dst->outputs[0];
  1439. AVFrame *out = apply_1d_lut(inlink, in);
  1440. if (!out)
  1441. return AVERROR(ENOMEM);
  1442. return ff_filter_frame(outlink, out);
  1443. }
  1444. static const AVFilterPad lut1d_inputs[] = {
  1445. {
  1446. .name = "default",
  1447. .type = AVMEDIA_TYPE_VIDEO,
  1448. .filter_frame = filter_frame_1d,
  1449. .config_props = config_input_1d,
  1450. },
  1451. { NULL }
  1452. };
  1453. static const AVFilterPad lut1d_outputs[] = {
  1454. {
  1455. .name = "default",
  1456. .type = AVMEDIA_TYPE_VIDEO,
  1457. },
  1458. { NULL }
  1459. };
  1460. AVFilter ff_vf_lut1d = {
  1461. .name = "lut1d",
  1462. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 1D LUT."),
  1463. .priv_size = sizeof(LUT1DContext),
  1464. .init = lut1d_init,
  1465. .query_formats = query_formats,
  1466. .inputs = lut1d_inputs,
  1467. .outputs = lut1d_outputs,
  1468. .priv_class = &lut1d_class,
  1469. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  1470. };
  1471. #endif