vf_lut3d.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * Copyright (c) 2013 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * 3D Lookup table filter
  23. */
  24. #include "libavutil/opt.h"
  25. #include "libavutil/file.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/avstring.h"
  30. #include "avfilter.h"
  31. #include "drawutils.h"
  32. #include "dualinput.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. #define R 0
  37. #define G 1
  38. #define B 2
  39. #define A 3
  40. enum interp_mode {
  41. INTERPOLATE_NEAREST,
  42. INTERPOLATE_TRILINEAR,
  43. INTERPOLATE_TETRAHEDRAL,
  44. NB_INTERP_MODE
  45. };
  46. struct rgbvec {
  47. float r, g, b;
  48. };
  49. /* 3D LUT don't often go up to level 32, but it is common to have a Hald CLUT
  50. * of 512x512 (64x64x64) */
  51. #define MAX_LEVEL 64
  52. typedef struct LUT3DContext {
  53. const AVClass *class;
  54. enum interp_mode interpolation;
  55. char *file;
  56. uint8_t rgba_map[4];
  57. int step;
  58. avfilter_action_func *interp;
  59. struct rgbvec lut[MAX_LEVEL][MAX_LEVEL][MAX_LEVEL];
  60. int lutsize;
  61. #if CONFIG_HALDCLUT_FILTER
  62. uint8_t clut_rgba_map[4];
  63. int clut_step;
  64. int clut_is16bit;
  65. int clut_width;
  66. FFDualInputContext dinput;
  67. #endif
  68. } LUT3DContext;
  69. typedef struct ThreadData {
  70. AVFrame *in, *out;
  71. } ThreadData;
  72. #define OFFSET(x) offsetof(LUT3DContext, x)
  73. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  74. #define COMMON_OPTIONS \
  75. { "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, FLAGS, "interp_mode" }, \
  76. { "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
  77. { "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" }, \
  78. { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \
  79. { NULL }
  80. static inline float lerpf(float v0, float v1, float f)
  81. {
  82. return v0 + (v1 - v0) * f;
  83. }
  84. static inline struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
  85. {
  86. struct rgbvec v = {
  87. lerpf(v0->r, v1->r, f), lerpf(v0->g, v1->g, f), lerpf(v0->b, v1->b, f)
  88. };
  89. return v;
  90. }
  91. #define NEAR(x) ((int)((x) + .5))
  92. #define PREV(x) ((int)(x))
  93. #define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
  94. /**
  95. * Get the nearest defined point
  96. */
  97. static inline struct rgbvec interp_nearest(const LUT3DContext *lut3d,
  98. const struct rgbvec *s)
  99. {
  100. return lut3d->lut[NEAR(s->r)][NEAR(s->g)][NEAR(s->b)];
  101. }
  102. /**
  103. * Interpolate using the 8 vertices of a cube
  104. * @see https://en.wikipedia.org/wiki/Trilinear_interpolation
  105. */
  106. static inline struct rgbvec interp_trilinear(const LUT3DContext *lut3d,
  107. const struct rgbvec *s)
  108. {
  109. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  110. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  111. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  112. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  113. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  114. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  115. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  116. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  117. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  118. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  119. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  120. const struct rgbvec c00 = lerp(&c000, &c100, d.r);
  121. const struct rgbvec c10 = lerp(&c010, &c110, d.r);
  122. const struct rgbvec c01 = lerp(&c001, &c101, d.r);
  123. const struct rgbvec c11 = lerp(&c011, &c111, d.r);
  124. const struct rgbvec c0 = lerp(&c00, &c10, d.g);
  125. const struct rgbvec c1 = lerp(&c01, &c11, d.g);
  126. const struct rgbvec c = lerp(&c0, &c1, d.b);
  127. return c;
  128. }
  129. /**
  130. * Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
  131. * @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
  132. */
  133. static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
  134. const struct rgbvec *s)
  135. {
  136. const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
  137. const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
  138. const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
  139. const struct rgbvec c000 = lut3d->lut[prev[0]][prev[1]][prev[2]];
  140. const struct rgbvec c111 = lut3d->lut[next[0]][next[1]][next[2]];
  141. struct rgbvec c;
  142. if (d.r > d.g) {
  143. if (d.g > d.b) {
  144. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  145. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  146. c.r = (1-d.r) * c000.r + (d.r-d.g) * c100.r + (d.g-d.b) * c110.r + (d.b) * c111.r;
  147. c.g = (1-d.r) * c000.g + (d.r-d.g) * c100.g + (d.g-d.b) * c110.g + (d.b) * c111.g;
  148. c.b = (1-d.r) * c000.b + (d.r-d.g) * c100.b + (d.g-d.b) * c110.b + (d.b) * c111.b;
  149. } else if (d.r > d.b) {
  150. const struct rgbvec c100 = lut3d->lut[next[0]][prev[1]][prev[2]];
  151. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  152. c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
  153. c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
  154. c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
  155. } else {
  156. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  157. const struct rgbvec c101 = lut3d->lut[next[0]][prev[1]][next[2]];
  158. c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
  159. c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
  160. c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
  161. }
  162. } else {
  163. if (d.b > d.g) {
  164. const struct rgbvec c001 = lut3d->lut[prev[0]][prev[1]][next[2]];
  165. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  166. c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
  167. c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
  168. c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
  169. } else if (d.b > d.r) {
  170. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  171. const struct rgbvec c011 = lut3d->lut[prev[0]][next[1]][next[2]];
  172. c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
  173. c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
  174. c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
  175. } else {
  176. const struct rgbvec c010 = lut3d->lut[prev[0]][next[1]][prev[2]];
  177. const struct rgbvec c110 = lut3d->lut[next[0]][next[1]][prev[2]];
  178. c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
  179. c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
  180. c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
  181. }
  182. }
  183. return c;
  184. }
  185. #define DEFINE_INTERP_FUNC(name, nbits) \
  186. static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  187. { \
  188. int x, y; \
  189. const LUT3DContext *lut3d = ctx->priv; \
  190. const ThreadData *td = arg; \
  191. const AVFrame *in = td->in; \
  192. const AVFrame *out = td->out; \
  193. const int direct = out == in; \
  194. const int step = lut3d->step; \
  195. const uint8_t r = lut3d->rgba_map[R]; \
  196. const uint8_t g = lut3d->rgba_map[G]; \
  197. const uint8_t b = lut3d->rgba_map[B]; \
  198. const uint8_t a = lut3d->rgba_map[A]; \
  199. const int slice_start = (in->height * jobnr ) / nb_jobs; \
  200. const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
  201. uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
  202. const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
  203. const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
  204. \
  205. for (y = slice_start; y < slice_end; y++) { \
  206. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  207. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  208. for (x = 0; x < in->width * step; x += step) { \
  209. const struct rgbvec scaled_rgb = {src[x + r] * scale, \
  210. src[x + g] * scale, \
  211. src[x + b] * scale}; \
  212. struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
  213. dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
  214. dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
  215. dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
  216. if (!direct && step == 4) \
  217. dst[x + a] = src[x + a]; \
  218. } \
  219. dstrow += out->linesize[0]; \
  220. srcrow += in ->linesize[0]; \
  221. } \
  222. return 0; \
  223. }
  224. DEFINE_INTERP_FUNC(nearest, 8)
  225. DEFINE_INTERP_FUNC(trilinear, 8)
  226. DEFINE_INTERP_FUNC(tetrahedral, 8)
  227. DEFINE_INTERP_FUNC(nearest, 16)
  228. DEFINE_INTERP_FUNC(trilinear, 16)
  229. DEFINE_INTERP_FUNC(tetrahedral, 16)
  230. #define MAX_LINE_SIZE 512
  231. static int skip_line(const char *p)
  232. {
  233. while (*p && av_isspace(*p))
  234. p++;
  235. return !*p || *p == '#';
  236. }
  237. #define NEXT_LINE(loop_cond) do { \
  238. if (!fgets(line, sizeof(line), f)) { \
  239. av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
  240. return AVERROR_INVALIDDATA; \
  241. } \
  242. } while (loop_cond)
  243. /* Basically r g and b float values on each line, with a facultative 3DLUTSIZE
  244. * directive; seems to be generated by Davinci */
  245. static int parse_dat(AVFilterContext *ctx, FILE *f)
  246. {
  247. LUT3DContext *lut3d = ctx->priv;
  248. char line[MAX_LINE_SIZE];
  249. int i, j, k, size;
  250. lut3d->lutsize = size = 33;
  251. NEXT_LINE(skip_line(line));
  252. if (!strncmp(line, "3DLUTSIZE ", 10)) {
  253. size = strtol(line + 10, NULL, 0);
  254. if (size < 2 || size > MAX_LEVEL) {
  255. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  256. return AVERROR(EINVAL);
  257. }
  258. lut3d->lutsize = size;
  259. NEXT_LINE(skip_line(line));
  260. }
  261. for (k = 0; k < size; k++) {
  262. for (j = 0; j < size; j++) {
  263. for (i = 0; i < size; i++) {
  264. struct rgbvec *vec = &lut3d->lut[k][j][i];
  265. if (k != 0 || j != 0 || i != 0)
  266. NEXT_LINE(skip_line(line));
  267. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  268. return AVERROR_INVALIDDATA;
  269. }
  270. }
  271. }
  272. return 0;
  273. }
  274. /* Iridas format */
  275. static int parse_cube(AVFilterContext *ctx, FILE *f)
  276. {
  277. LUT3DContext *lut3d = ctx->priv;
  278. char line[MAX_LINE_SIZE];
  279. float min[3] = {0.0, 0.0, 0.0};
  280. float max[3] = {1.0, 1.0, 1.0};
  281. while (fgets(line, sizeof(line), f)) {
  282. if (!strncmp(line, "LUT_3D_SIZE ", 12)) {
  283. int i, j, k;
  284. const int size = strtol(line + 12, NULL, 0);
  285. if (size < 2 || size > MAX_LEVEL) {
  286. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  287. return AVERROR(EINVAL);
  288. }
  289. lut3d->lutsize = size;
  290. for (k = 0; k < size; k++) {
  291. for (j = 0; j < size; j++) {
  292. for (i = 0; i < size; i++) {
  293. struct rgbvec *vec = &lut3d->lut[i][j][k];
  294. do {
  295. NEXT_LINE(0);
  296. if (!strncmp(line, "DOMAIN_", 7)) {
  297. float *vals = NULL;
  298. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  299. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  300. if (!vals)
  301. return AVERROR_INVALIDDATA;
  302. sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  303. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  304. min[0], min[1], min[2], max[0], max[1], max[2]);
  305. continue;
  306. }
  307. } while (skip_line(line));
  308. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  309. return AVERROR_INVALIDDATA;
  310. vec->r *= max[0] - min[0];
  311. vec->g *= max[1] - min[1];
  312. vec->b *= max[2] - min[2];
  313. }
  314. }
  315. }
  316. break;
  317. }
  318. }
  319. return 0;
  320. }
  321. /* Assume 17x17x17 LUT with a 16-bit depth
  322. * FIXME: it seems there are various 3dl formats */
  323. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  324. {
  325. char line[MAX_LINE_SIZE];
  326. LUT3DContext *lut3d = ctx->priv;
  327. int i, j, k;
  328. const int size = 17;
  329. const float scale = 16*16*16;
  330. lut3d->lutsize = size;
  331. NEXT_LINE(skip_line(line));
  332. for (k = 0; k < size; k++) {
  333. for (j = 0; j < size; j++) {
  334. for (i = 0; i < size; i++) {
  335. int r, g, b;
  336. struct rgbvec *vec = &lut3d->lut[k][j][i];
  337. NEXT_LINE(skip_line(line));
  338. if (sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  339. return AVERROR_INVALIDDATA;
  340. vec->r = r / scale;
  341. vec->g = g / scale;
  342. vec->b = b / scale;
  343. }
  344. }
  345. }
  346. return 0;
  347. }
  348. /* Pandora format */
  349. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  350. {
  351. LUT3DContext *lut3d = ctx->priv;
  352. float scale;
  353. int i, j, k, size, in = -1, out = -1;
  354. char line[MAX_LINE_SIZE];
  355. uint8_t rgb_map[3] = {0, 1, 2};
  356. while (fgets(line, sizeof(line), f)) {
  357. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  358. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  359. else if (!strncmp(line, "values", 6)) {
  360. const char *p = line + 6;
  361. #define SET_COLOR(id) do { \
  362. while (av_isspace(*p)) \
  363. p++; \
  364. switch (*p) { \
  365. case 'r': rgb_map[id] = 0; break; \
  366. case 'g': rgb_map[id] = 1; break; \
  367. case 'b': rgb_map[id] = 2; break; \
  368. } \
  369. while (*p && !av_isspace(*p)) \
  370. p++; \
  371. } while (0)
  372. SET_COLOR(0);
  373. SET_COLOR(1);
  374. SET_COLOR(2);
  375. break;
  376. }
  377. }
  378. if (in == -1 || out == -1) {
  379. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  380. return AVERROR_INVALIDDATA;
  381. }
  382. if (in < 2 || out < 2 ||
  383. in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
  384. out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
  385. av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
  386. return AVERROR_INVALIDDATA;
  387. }
  388. for (size = 1; size*size*size < in; size++);
  389. lut3d->lutsize = size;
  390. scale = 1. / (out - 1);
  391. for (k = 0; k < size; k++) {
  392. for (j = 0; j < size; j++) {
  393. for (i = 0; i < size; i++) {
  394. struct rgbvec *vec = &lut3d->lut[k][j][i];
  395. float val[3];
  396. NEXT_LINE(0);
  397. if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  398. return AVERROR_INVALIDDATA;
  399. vec->r = val[rgb_map[0]] * scale;
  400. vec->g = val[rgb_map[1]] * scale;
  401. vec->b = val[rgb_map[2]] * scale;
  402. }
  403. }
  404. }
  405. return 0;
  406. }
  407. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  408. {
  409. int i, j, k;
  410. const float c = 1. / (size - 1);
  411. lut3d->lutsize = size;
  412. for (k = 0; k < size; k++) {
  413. for (j = 0; j < size; j++) {
  414. for (i = 0; i < size; i++) {
  415. struct rgbvec *vec = &lut3d->lut[k][j][i];
  416. vec->r = k * c;
  417. vec->g = j * c;
  418. vec->b = i * c;
  419. }
  420. }
  421. }
  422. }
  423. static int query_formats(AVFilterContext *ctx)
  424. {
  425. static const enum AVPixelFormat pix_fmts[] = {
  426. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  427. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  428. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  429. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  430. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  431. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  432. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  433. AV_PIX_FMT_NONE
  434. };
  435. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  436. return 0;
  437. }
  438. static int config_input(AVFilterLink *inlink)
  439. {
  440. int is16bit = 0;
  441. LUT3DContext *lut3d = inlink->dst->priv;
  442. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  443. switch (inlink->format) {
  444. case AV_PIX_FMT_RGB48:
  445. case AV_PIX_FMT_BGR48:
  446. case AV_PIX_FMT_RGBA64:
  447. case AV_PIX_FMT_BGRA64:
  448. is16bit = 1;
  449. }
  450. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  451. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  452. #define SET_FUNC(name) do { \
  453. if (is16bit) lut3d->interp = interp_16_##name; \
  454. else lut3d->interp = interp_8_##name; \
  455. } while (0)
  456. switch (lut3d->interpolation) {
  457. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  458. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  459. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  460. default:
  461. av_assert0(0);
  462. }
  463. return 0;
  464. }
  465. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  466. {
  467. AVFilterContext *ctx = inlink->dst;
  468. LUT3DContext *lut3d = ctx->priv;
  469. AVFilterLink *outlink = inlink->dst->outputs[0];
  470. AVFrame *out;
  471. ThreadData td;
  472. if (av_frame_is_writable(in)) {
  473. out = in;
  474. } else {
  475. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  476. if (!out) {
  477. av_frame_free(&in);
  478. return NULL;
  479. }
  480. av_frame_copy_props(out, in);
  481. }
  482. td.in = in;
  483. td.out = out;
  484. ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
  485. if (out != in)
  486. av_frame_free(&in);
  487. return out;
  488. }
  489. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  490. {
  491. AVFilterLink *outlink = inlink->dst->outputs[0];
  492. AVFrame *out = apply_lut(inlink, in);
  493. if (!out)
  494. return AVERROR(ENOMEM);
  495. return ff_filter_frame(outlink, out);
  496. }
  497. #if CONFIG_LUT3D_FILTER
  498. static const AVOption lut3d_options[] = {
  499. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  500. COMMON_OPTIONS
  501. };
  502. AVFILTER_DEFINE_CLASS(lut3d);
  503. static av_cold int lut3d_init(AVFilterContext *ctx)
  504. {
  505. int ret;
  506. FILE *f;
  507. const char *ext;
  508. LUT3DContext *lut3d = ctx->priv;
  509. if (!lut3d->file) {
  510. set_identity_matrix(lut3d, 32);
  511. return 0;
  512. }
  513. f = fopen(lut3d->file, "r");
  514. if (!f) {
  515. ret = AVERROR(errno);
  516. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  517. return ret;
  518. }
  519. ext = strrchr(lut3d->file, '.');
  520. if (!ext) {
  521. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  522. ret = AVERROR_INVALIDDATA;
  523. goto end;
  524. }
  525. ext++;
  526. if (!av_strcasecmp(ext, "dat")) {
  527. ret = parse_dat(ctx, f);
  528. } else if (!av_strcasecmp(ext, "3dl")) {
  529. ret = parse_3dl(ctx, f);
  530. } else if (!av_strcasecmp(ext, "cube")) {
  531. ret = parse_cube(ctx, f);
  532. } else if (!av_strcasecmp(ext, "m3d")) {
  533. ret = parse_m3d(ctx, f);
  534. } else {
  535. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  536. ret = AVERROR(EINVAL);
  537. }
  538. if (!ret && !lut3d->lutsize) {
  539. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  540. ret = AVERROR_INVALIDDATA;
  541. }
  542. end:
  543. fclose(f);
  544. return ret;
  545. }
  546. static const AVFilterPad lut3d_inputs[] = {
  547. {
  548. .name = "default",
  549. .type = AVMEDIA_TYPE_VIDEO,
  550. .filter_frame = filter_frame,
  551. .config_props = config_input,
  552. },
  553. { NULL }
  554. };
  555. static const AVFilterPad lut3d_outputs[] = {
  556. {
  557. .name = "default",
  558. .type = AVMEDIA_TYPE_VIDEO,
  559. },
  560. { NULL }
  561. };
  562. AVFilter ff_vf_lut3d = {
  563. .name = "lut3d",
  564. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  565. .priv_size = sizeof(LUT3DContext),
  566. .init = lut3d_init,
  567. .query_formats = query_formats,
  568. .inputs = lut3d_inputs,
  569. .outputs = lut3d_outputs,
  570. .priv_class = &lut3d_class,
  571. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  572. };
  573. #endif
  574. #if CONFIG_HALDCLUT_FILTER
  575. static void update_clut(LUT3DContext *lut3d, const AVFrame *frame)
  576. {
  577. const uint8_t *data = frame->data[0];
  578. const int linesize = frame->linesize[0];
  579. const int w = lut3d->clut_width;
  580. const int step = lut3d->clut_step;
  581. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  582. const int level = lut3d->lutsize;
  583. #define LOAD_CLUT(nbits) do { \
  584. int i, j, k, x = 0, y = 0; \
  585. \
  586. for (k = 0; k < level; k++) { \
  587. for (j = 0; j < level; j++) { \
  588. for (i = 0; i < level; i++) { \
  589. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  590. (data + y*linesize + x*step); \
  591. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  592. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  593. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  594. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  595. if (++x == w) { \
  596. x = 0; \
  597. y++; \
  598. } \
  599. } \
  600. } \
  601. } \
  602. } while (0)
  603. if (!lut3d->clut_is16bit) LOAD_CLUT(8);
  604. else LOAD_CLUT(16);
  605. }
  606. static int config_output(AVFilterLink *outlink)
  607. {
  608. AVFilterContext *ctx = outlink->src;
  609. LUT3DContext *lut3d = ctx->priv;
  610. int ret;
  611. outlink->w = ctx->inputs[0]->w;
  612. outlink->h = ctx->inputs[0]->h;
  613. outlink->time_base = ctx->inputs[0]->time_base;
  614. if ((ret = ff_dualinput_init(ctx, &lut3d->dinput)) < 0)
  615. return ret;
  616. return 0;
  617. }
  618. static int filter_frame_hald(AVFilterLink *inlink, AVFrame *inpicref)
  619. {
  620. LUT3DContext *s = inlink->dst->priv;
  621. return ff_dualinput_filter_frame(&s->dinput, inlink, inpicref);
  622. }
  623. static int request_frame(AVFilterLink *outlink)
  624. {
  625. LUT3DContext *s = outlink->src->priv;
  626. return ff_dualinput_request_frame(&s->dinput, outlink);
  627. }
  628. static int config_clut(AVFilterLink *inlink)
  629. {
  630. int size, level, w, h;
  631. AVFilterContext *ctx = inlink->dst;
  632. LUT3DContext *lut3d = ctx->priv;
  633. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  634. lut3d->clut_is16bit = 0;
  635. switch (inlink->format) {
  636. case AV_PIX_FMT_RGB48:
  637. case AV_PIX_FMT_BGR48:
  638. case AV_PIX_FMT_RGBA64:
  639. case AV_PIX_FMT_BGRA64:
  640. lut3d->clut_is16bit = 1;
  641. }
  642. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  643. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  644. if (inlink->w > inlink->h)
  645. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  646. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  647. else if (inlink->w < inlink->h)
  648. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  649. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  650. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  651. for (level = 1; level*level*level < w; level++);
  652. size = level*level*level;
  653. if (size != w) {
  654. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  655. return AVERROR_INVALIDDATA;
  656. }
  657. av_assert0(w == h && w == size);
  658. level *= level;
  659. if (level > MAX_LEVEL) {
  660. const int max_clut_level = sqrt(MAX_LEVEL);
  661. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  662. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  663. "(maximum level is %d, or %dx%d CLUT)\n",
  664. max_clut_level, max_clut_size, max_clut_size);
  665. return AVERROR(EINVAL);
  666. }
  667. lut3d->lutsize = level;
  668. return 0;
  669. }
  670. static AVFrame *update_apply_clut(AVFilterContext *ctx, AVFrame *main,
  671. const AVFrame *second)
  672. {
  673. AVFilterLink *inlink = ctx->inputs[0];
  674. update_clut(ctx->priv, second);
  675. return apply_lut(inlink, main);
  676. }
  677. static av_cold int haldclut_init(AVFilterContext *ctx)
  678. {
  679. LUT3DContext *lut3d = ctx->priv;
  680. lut3d->dinput.process = update_apply_clut;
  681. return 0;
  682. }
  683. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  684. {
  685. LUT3DContext *lut3d = ctx->priv;
  686. ff_dualinput_uninit(&lut3d->dinput);
  687. }
  688. static const AVOption haldclut_options[] = {
  689. { "shortest", "force termination when the shortest input terminates", OFFSET(dinput.shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  690. { "repeatlast", "continue applying the last clut after eos", OFFSET(dinput.repeatlast), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS },
  691. COMMON_OPTIONS
  692. };
  693. AVFILTER_DEFINE_CLASS(haldclut);
  694. static const AVFilterPad haldclut_inputs[] = {
  695. {
  696. .name = "main",
  697. .type = AVMEDIA_TYPE_VIDEO,
  698. .filter_frame = filter_frame_hald,
  699. .config_props = config_input,
  700. },{
  701. .name = "clut",
  702. .type = AVMEDIA_TYPE_VIDEO,
  703. .filter_frame = filter_frame_hald,
  704. .config_props = config_clut,
  705. },
  706. { NULL }
  707. };
  708. static const AVFilterPad haldclut_outputs[] = {
  709. {
  710. .name = "default",
  711. .type = AVMEDIA_TYPE_VIDEO,
  712. .request_frame = request_frame,
  713. .config_props = config_output,
  714. },
  715. { NULL }
  716. };
  717. AVFilter ff_vf_haldclut = {
  718. .name = "haldclut",
  719. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  720. .priv_size = sizeof(LUT3DContext),
  721. .init = haldclut_init,
  722. .uninit = haldclut_uninit,
  723. .query_formats = query_formats,
  724. .inputs = haldclut_inputs,
  725. .outputs = haldclut_outputs,
  726. .priv_class = &haldclut_class,
  727. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  728. };
  729. #endif