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.

1683 lines
67KB

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