vf_lut3d.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. int interpolation; ///<interp_mode
  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. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  436. if (!fmts_list)
  437. return AVERROR(ENOMEM);
  438. return ff_set_common_formats(ctx, fmts_list);
  439. }
  440. static int config_input(AVFilterLink *inlink)
  441. {
  442. int is16bit = 0;
  443. LUT3DContext *lut3d = inlink->dst->priv;
  444. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  445. switch (inlink->format) {
  446. case AV_PIX_FMT_RGB48:
  447. case AV_PIX_FMT_BGR48:
  448. case AV_PIX_FMT_RGBA64:
  449. case AV_PIX_FMT_BGRA64:
  450. is16bit = 1;
  451. }
  452. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  453. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  454. #define SET_FUNC(name) do { \
  455. if (is16bit) lut3d->interp = interp_16_##name; \
  456. else lut3d->interp = interp_8_##name; \
  457. } while (0)
  458. switch (lut3d->interpolation) {
  459. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  460. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  461. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  462. default:
  463. av_assert0(0);
  464. }
  465. return 0;
  466. }
  467. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  468. {
  469. AVFilterContext *ctx = inlink->dst;
  470. LUT3DContext *lut3d = ctx->priv;
  471. AVFilterLink *outlink = inlink->dst->outputs[0];
  472. AVFrame *out;
  473. ThreadData td;
  474. if (av_frame_is_writable(in)) {
  475. out = in;
  476. } else {
  477. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  478. if (!out) {
  479. av_frame_free(&in);
  480. return NULL;
  481. }
  482. av_frame_copy_props(out, in);
  483. }
  484. td.in = in;
  485. td.out = out;
  486. ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
  487. if (out != in)
  488. av_frame_free(&in);
  489. return out;
  490. }
  491. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  492. {
  493. AVFilterLink *outlink = inlink->dst->outputs[0];
  494. AVFrame *out = apply_lut(inlink, in);
  495. if (!out)
  496. return AVERROR(ENOMEM);
  497. return ff_filter_frame(outlink, out);
  498. }
  499. #if CONFIG_LUT3D_FILTER
  500. static const AVOption lut3d_options[] = {
  501. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  502. COMMON_OPTIONS
  503. };
  504. AVFILTER_DEFINE_CLASS(lut3d);
  505. static av_cold int lut3d_init(AVFilterContext *ctx)
  506. {
  507. int ret;
  508. FILE *f;
  509. const char *ext;
  510. LUT3DContext *lut3d = ctx->priv;
  511. if (!lut3d->file) {
  512. set_identity_matrix(lut3d, 32);
  513. return 0;
  514. }
  515. f = fopen(lut3d->file, "r");
  516. if (!f) {
  517. ret = AVERROR(errno);
  518. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  519. return ret;
  520. }
  521. ext = strrchr(lut3d->file, '.');
  522. if (!ext) {
  523. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  524. ret = AVERROR_INVALIDDATA;
  525. goto end;
  526. }
  527. ext++;
  528. if (!av_strcasecmp(ext, "dat")) {
  529. ret = parse_dat(ctx, f);
  530. } else if (!av_strcasecmp(ext, "3dl")) {
  531. ret = parse_3dl(ctx, f);
  532. } else if (!av_strcasecmp(ext, "cube")) {
  533. ret = parse_cube(ctx, f);
  534. } else if (!av_strcasecmp(ext, "m3d")) {
  535. ret = parse_m3d(ctx, f);
  536. } else {
  537. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  538. ret = AVERROR(EINVAL);
  539. }
  540. if (!ret && !lut3d->lutsize) {
  541. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  542. ret = AVERROR_INVALIDDATA;
  543. }
  544. end:
  545. fclose(f);
  546. return ret;
  547. }
  548. static const AVFilterPad lut3d_inputs[] = {
  549. {
  550. .name = "default",
  551. .type = AVMEDIA_TYPE_VIDEO,
  552. .filter_frame = filter_frame,
  553. .config_props = config_input,
  554. },
  555. { NULL }
  556. };
  557. static const AVFilterPad lut3d_outputs[] = {
  558. {
  559. .name = "default",
  560. .type = AVMEDIA_TYPE_VIDEO,
  561. },
  562. { NULL }
  563. };
  564. AVFilter ff_vf_lut3d = {
  565. .name = "lut3d",
  566. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  567. .priv_size = sizeof(LUT3DContext),
  568. .init = lut3d_init,
  569. .query_formats = query_formats,
  570. .inputs = lut3d_inputs,
  571. .outputs = lut3d_outputs,
  572. .priv_class = &lut3d_class,
  573. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  574. };
  575. #endif
  576. #if CONFIG_HALDCLUT_FILTER
  577. static void update_clut(LUT3DContext *lut3d, const AVFrame *frame)
  578. {
  579. const uint8_t *data = frame->data[0];
  580. const int linesize = frame->linesize[0];
  581. const int w = lut3d->clut_width;
  582. const int step = lut3d->clut_step;
  583. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  584. const int level = lut3d->lutsize;
  585. #define LOAD_CLUT(nbits) do { \
  586. int i, j, k, x = 0, y = 0; \
  587. \
  588. for (k = 0; k < level; k++) { \
  589. for (j = 0; j < level; j++) { \
  590. for (i = 0; i < level; i++) { \
  591. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  592. (data + y*linesize + x*step); \
  593. struct rgbvec *vec = &lut3d->lut[i][j][k]; \
  594. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  595. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  596. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  597. if (++x == w) { \
  598. x = 0; \
  599. y++; \
  600. } \
  601. } \
  602. } \
  603. } \
  604. } while (0)
  605. if (!lut3d->clut_is16bit) LOAD_CLUT(8);
  606. else LOAD_CLUT(16);
  607. }
  608. static int config_output(AVFilterLink *outlink)
  609. {
  610. AVFilterContext *ctx = outlink->src;
  611. LUT3DContext *lut3d = ctx->priv;
  612. int ret;
  613. outlink->w = ctx->inputs[0]->w;
  614. outlink->h = ctx->inputs[0]->h;
  615. outlink->time_base = ctx->inputs[0]->time_base;
  616. if ((ret = ff_dualinput_init(ctx, &lut3d->dinput)) < 0)
  617. return ret;
  618. return 0;
  619. }
  620. static int filter_frame_hald(AVFilterLink *inlink, AVFrame *inpicref)
  621. {
  622. LUT3DContext *s = inlink->dst->priv;
  623. return ff_dualinput_filter_frame(&s->dinput, inlink, inpicref);
  624. }
  625. static int request_frame(AVFilterLink *outlink)
  626. {
  627. LUT3DContext *s = outlink->src->priv;
  628. return ff_dualinput_request_frame(&s->dinput, outlink);
  629. }
  630. static int config_clut(AVFilterLink *inlink)
  631. {
  632. int size, level, w, h;
  633. AVFilterContext *ctx = inlink->dst;
  634. LUT3DContext *lut3d = ctx->priv;
  635. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  636. av_assert0(desc);
  637. lut3d->clut_is16bit = 0;
  638. switch (inlink->format) {
  639. case AV_PIX_FMT_RGB48:
  640. case AV_PIX_FMT_BGR48:
  641. case AV_PIX_FMT_RGBA64:
  642. case AV_PIX_FMT_BGRA64:
  643. lut3d->clut_is16bit = 1;
  644. }
  645. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  646. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  647. if (inlink->w > inlink->h)
  648. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  649. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  650. else if (inlink->w < inlink->h)
  651. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  652. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  653. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  654. for (level = 1; level*level*level < w; level++);
  655. size = level*level*level;
  656. if (size != w) {
  657. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  658. return AVERROR_INVALIDDATA;
  659. }
  660. av_assert0(w == h && w == size);
  661. level *= level;
  662. if (level > MAX_LEVEL) {
  663. const int max_clut_level = sqrt(MAX_LEVEL);
  664. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  665. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  666. "(maximum level is %d, or %dx%d CLUT)\n",
  667. max_clut_level, max_clut_size, max_clut_size);
  668. return AVERROR(EINVAL);
  669. }
  670. lut3d->lutsize = level;
  671. return 0;
  672. }
  673. static AVFrame *update_apply_clut(AVFilterContext *ctx, AVFrame *main,
  674. const AVFrame *second)
  675. {
  676. AVFilterLink *inlink = ctx->inputs[0];
  677. update_clut(ctx->priv, second);
  678. return apply_lut(inlink, main);
  679. }
  680. static av_cold int haldclut_init(AVFilterContext *ctx)
  681. {
  682. LUT3DContext *lut3d = ctx->priv;
  683. lut3d->dinput.process = update_apply_clut;
  684. return 0;
  685. }
  686. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  687. {
  688. LUT3DContext *lut3d = ctx->priv;
  689. ff_dualinput_uninit(&lut3d->dinput);
  690. }
  691. static const AVOption haldclut_options[] = {
  692. { "shortest", "force termination when the shortest input terminates", OFFSET(dinput.shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  693. { "repeatlast", "continue applying the last clut after eos", OFFSET(dinput.repeatlast), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  694. COMMON_OPTIONS
  695. };
  696. AVFILTER_DEFINE_CLASS(haldclut);
  697. static const AVFilterPad haldclut_inputs[] = {
  698. {
  699. .name = "main",
  700. .type = AVMEDIA_TYPE_VIDEO,
  701. .filter_frame = filter_frame_hald,
  702. .config_props = config_input,
  703. },{
  704. .name = "clut",
  705. .type = AVMEDIA_TYPE_VIDEO,
  706. .filter_frame = filter_frame_hald,
  707. .config_props = config_clut,
  708. },
  709. { NULL }
  710. };
  711. static const AVFilterPad haldclut_outputs[] = {
  712. {
  713. .name = "default",
  714. .type = AVMEDIA_TYPE_VIDEO,
  715. .request_frame = request_frame,
  716. .config_props = config_output,
  717. },
  718. { NULL }
  719. };
  720. AVFilter ff_vf_haldclut = {
  721. .name = "haldclut",
  722. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  723. .priv_size = sizeof(LUT3DContext),
  724. .init = haldclut_init,
  725. .uninit = haldclut_uninit,
  726. .query_formats = query_formats,
  727. .inputs = haldclut_inputs,
  728. .outputs = haldclut_outputs,
  729. .priv_class = &haldclut_class,
  730. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  731. };
  732. #endif