vf_lut3d.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. \
  204. for (y = slice_start; y < slice_end; y++) { \
  205. uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
  206. const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
  207. for (x = 0; x < in->width * step; x += step) { \
  208. const float scale = (1. / ((1<<nbits) - 1)) * (lut3d->lutsize - 1); \
  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; seems to be generated by
  244. * Davinci */
  245. static int parse_dat(AVFilterContext *ctx, FILE *f)
  246. {
  247. LUT3DContext *lut3d = ctx->priv;
  248. const int size = lut3d->lutsize;
  249. int i, j, k;
  250. for (k = 0; k < size; k++) {
  251. for (j = 0; j < size; j++) {
  252. for (i = 0; i < size; i++) {
  253. char line[MAX_LINE_SIZE];
  254. struct rgbvec *vec = &lut3d->lut[k][j][i];
  255. NEXT_LINE(skip_line(line));
  256. sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b);
  257. }
  258. }
  259. }
  260. return 0;
  261. }
  262. /* Iridas format */
  263. static int parse_cube(AVFilterContext *ctx, FILE *f)
  264. {
  265. LUT3DContext *lut3d = ctx->priv;
  266. char line[MAX_LINE_SIZE];
  267. float min[3] = {0.0, 0.0, 0.0};
  268. float max[3] = {1.0, 1.0, 1.0};
  269. while (fgets(line, sizeof(line), f)) {
  270. if (!strncmp(line, "LUT_3D_SIZE ", 12)) {
  271. int i, j, k;
  272. const int size = strtol(line + 12, NULL, 0);
  273. if (size < 2 || size > MAX_LEVEL) {
  274. av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
  275. return AVERROR(EINVAL);
  276. }
  277. lut3d->lutsize = size;
  278. for (k = 0; k < size; k++) {
  279. for (j = 0; j < size; j++) {
  280. for (i = 0; i < size; i++) {
  281. struct rgbvec *vec = &lut3d->lut[i][j][k];
  282. do {
  283. NEXT_LINE(0);
  284. if (!strncmp(line, "DOMAIN_", 7)) {
  285. float *vals = NULL;
  286. if (!strncmp(line + 7, "MIN ", 4)) vals = min;
  287. else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
  288. if (!vals)
  289. return AVERROR_INVALIDDATA;
  290. sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2);
  291. av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
  292. min[0], min[1], min[2], max[0], max[1], max[2]);
  293. continue;
  294. }
  295. } while (skip_line(line));
  296. if (sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
  297. return AVERROR_INVALIDDATA;
  298. vec->r *= max[0] - min[0];
  299. vec->g *= max[1] - min[1];
  300. vec->b *= max[2] - min[2];
  301. }
  302. }
  303. }
  304. break;
  305. }
  306. }
  307. return 0;
  308. }
  309. /* Assume 17x17x17 LUT with a 16-bit depth
  310. * FIXME: it seems there are various 3dl formats */
  311. static int parse_3dl(AVFilterContext *ctx, FILE *f)
  312. {
  313. char line[MAX_LINE_SIZE];
  314. LUT3DContext *lut3d = ctx->priv;
  315. int i, j, k;
  316. const int size = 17;
  317. const float scale = 16*16*16;
  318. lut3d->lutsize = size;
  319. NEXT_LINE(skip_line(line));
  320. for (k = 0; k < size; k++) {
  321. for (j = 0; j < size; j++) {
  322. for (i = 0; i < size; i++) {
  323. int r, g, b;
  324. struct rgbvec *vec = &lut3d->lut[k][j][i];
  325. NEXT_LINE(skip_line(line));
  326. if (sscanf(line, "%d %d %d", &r, &g, &b) != 3)
  327. return AVERROR_INVALIDDATA;
  328. vec->r = r / scale;
  329. vec->g = g / scale;
  330. vec->b = b / scale;
  331. }
  332. }
  333. }
  334. return 0;
  335. }
  336. /* Pandora format */
  337. static int parse_m3d(AVFilterContext *ctx, FILE *f)
  338. {
  339. LUT3DContext *lut3d = ctx->priv;
  340. float scale;
  341. int i, j, k, size, in = -1, out = -1;
  342. char line[MAX_LINE_SIZE];
  343. uint8_t rgb_map[3] = {0, 1, 2};
  344. while (fgets(line, sizeof(line), f)) {
  345. if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
  346. else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
  347. else if (!strncmp(line, "values", 6)) {
  348. const char *p = line + 6;
  349. #define SET_COLOR(id) do { \
  350. while (av_isspace(*p)) \
  351. p++; \
  352. switch (*p) { \
  353. case 'r': rgb_map[id] = 0; break; \
  354. case 'g': rgb_map[id] = 1; break; \
  355. case 'b': rgb_map[id] = 2; break; \
  356. } \
  357. while (*p && !av_isspace(*p)) \
  358. p++; \
  359. } while (0)
  360. SET_COLOR(0);
  361. SET_COLOR(1);
  362. SET_COLOR(2);
  363. break;
  364. }
  365. }
  366. if (in == -1 || out == -1) {
  367. av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
  368. return AVERROR_INVALIDDATA;
  369. }
  370. if (in < 2 || out < 2 ||
  371. in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
  372. out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
  373. av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
  374. return AVERROR_INVALIDDATA;
  375. }
  376. for (size = 1; size*size*size < in; size++);
  377. lut3d->lutsize = size;
  378. scale = 1. / (out - 1);
  379. for (k = 0; k < size; k++) {
  380. for (j = 0; j < size; j++) {
  381. for (i = 0; i < size; i++) {
  382. struct rgbvec *vec = &lut3d->lut[k][j][i];
  383. float val[3];
  384. NEXT_LINE(0);
  385. if (sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
  386. return AVERROR_INVALIDDATA;
  387. vec->r = val[rgb_map[0]] * scale;
  388. vec->g = val[rgb_map[1]] * scale;
  389. vec->b = val[rgb_map[2]] * scale;
  390. }
  391. }
  392. }
  393. return 0;
  394. }
  395. static void set_identity_matrix(LUT3DContext *lut3d, int size)
  396. {
  397. int i, j, k;
  398. const float c = 1. / (size - 1);
  399. lut3d->lutsize = size;
  400. for (k = 0; k < size; k++) {
  401. for (j = 0; j < size; j++) {
  402. for (i = 0; i < size; i++) {
  403. struct rgbvec *vec = &lut3d->lut[k][j][i];
  404. vec->r = k * c;
  405. vec->g = j * c;
  406. vec->b = i * c;
  407. }
  408. }
  409. }
  410. }
  411. static int query_formats(AVFilterContext *ctx)
  412. {
  413. static const enum AVPixelFormat pix_fmts[] = {
  414. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  415. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  416. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  417. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  418. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  419. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  420. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  421. AV_PIX_FMT_NONE
  422. };
  423. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  424. return 0;
  425. }
  426. static int config_input(AVFilterLink *inlink)
  427. {
  428. int is16bit = 0;
  429. LUT3DContext *lut3d = inlink->dst->priv;
  430. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  431. switch (inlink->format) {
  432. case AV_PIX_FMT_RGB48:
  433. case AV_PIX_FMT_BGR48:
  434. case AV_PIX_FMT_RGBA64:
  435. case AV_PIX_FMT_BGRA64:
  436. is16bit = 1;
  437. }
  438. ff_fill_rgba_map(lut3d->rgba_map, inlink->format);
  439. lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  440. #define SET_FUNC(name) do { \
  441. if (is16bit) lut3d->interp = interp_16_##name; \
  442. else lut3d->interp = interp_8_##name; \
  443. } while (0)
  444. switch (lut3d->interpolation) {
  445. case INTERPOLATE_NEAREST: SET_FUNC(nearest); break;
  446. case INTERPOLATE_TRILINEAR: SET_FUNC(trilinear); break;
  447. case INTERPOLATE_TETRAHEDRAL: SET_FUNC(tetrahedral); break;
  448. default:
  449. av_assert0(0);
  450. }
  451. return 0;
  452. }
  453. static AVFrame *apply_lut(AVFilterLink *inlink, AVFrame *in)
  454. {
  455. AVFilterContext *ctx = inlink->dst;
  456. LUT3DContext *lut3d = ctx->priv;
  457. AVFilterLink *outlink = inlink->dst->outputs[0];
  458. AVFrame *out;
  459. ThreadData td;
  460. if (av_frame_is_writable(in)) {
  461. out = in;
  462. } else {
  463. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  464. if (!out) {
  465. av_frame_free(&in);
  466. return NULL;
  467. }
  468. av_frame_copy_props(out, in);
  469. }
  470. td.in = in;
  471. td.out = out;
  472. ctx->internal->execute(ctx, lut3d->interp, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
  473. if (out != in)
  474. av_frame_free(&in);
  475. return out;
  476. }
  477. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  478. {
  479. AVFilterLink *outlink = inlink->dst->outputs[0];
  480. AVFrame *out = apply_lut(inlink, in);
  481. if (!out)
  482. return AVERROR(ENOMEM);
  483. return ff_filter_frame(outlink, out);
  484. }
  485. #if CONFIG_LUT3D_FILTER
  486. static const AVOption lut3d_options[] = {
  487. { "file", "set 3D LUT file name", OFFSET(file), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  488. COMMON_OPTIONS
  489. };
  490. AVFILTER_DEFINE_CLASS(lut3d);
  491. static av_cold int lut3d_init(AVFilterContext *ctx)
  492. {
  493. int ret;
  494. FILE *f;
  495. const char *ext;
  496. LUT3DContext *lut3d = ctx->priv;
  497. if (!lut3d->file) {
  498. set_identity_matrix(lut3d, 32);
  499. return 0;
  500. }
  501. f = fopen(lut3d->file, "r");
  502. if (!f) {
  503. ret = AVERROR(errno);
  504. av_log(ctx, AV_LOG_ERROR, "%s: %s\n", lut3d->file, av_err2str(ret));
  505. return ret;
  506. }
  507. ext = strrchr(lut3d->file, '.');
  508. if (!ext) {
  509. av_log(ctx, AV_LOG_ERROR, "Unable to guess the format from the extension\n");
  510. ret = AVERROR_INVALIDDATA;
  511. goto end;
  512. }
  513. ext++;
  514. if (!av_strcasecmp(ext, "dat")) {
  515. lut3d->lutsize = 33;
  516. ret = parse_dat(ctx, f);
  517. } else if (!av_strcasecmp(ext, "3dl")) {
  518. ret = parse_3dl(ctx, f);
  519. } else if (!av_strcasecmp(ext, "cube")) {
  520. ret = parse_cube(ctx, f);
  521. } else if (!av_strcasecmp(ext, "m3d")) {
  522. ret = parse_m3d(ctx, f);
  523. } else {
  524. av_log(ctx, AV_LOG_ERROR, "Unrecognized '.%s' file type\n", ext);
  525. ret = AVERROR(EINVAL);
  526. }
  527. if (!ret && !lut3d->lutsize) {
  528. av_log(ctx, AV_LOG_ERROR, "3D LUT is empty\n");
  529. ret = AVERROR_INVALIDDATA;
  530. }
  531. end:
  532. fclose(f);
  533. return ret;
  534. }
  535. static const AVFilterPad lut3d_inputs[] = {
  536. {
  537. .name = "default",
  538. .type = AVMEDIA_TYPE_VIDEO,
  539. .filter_frame = filter_frame,
  540. .config_props = config_input,
  541. },
  542. { NULL }
  543. };
  544. static const AVFilterPad lut3d_outputs[] = {
  545. {
  546. .name = "default",
  547. .type = AVMEDIA_TYPE_VIDEO,
  548. },
  549. { NULL }
  550. };
  551. AVFilter ff_vf_lut3d = {
  552. .name = "lut3d",
  553. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a 3D LUT."),
  554. .priv_size = sizeof(LUT3DContext),
  555. .init = lut3d_init,
  556. .query_formats = query_formats,
  557. .inputs = lut3d_inputs,
  558. .outputs = lut3d_outputs,
  559. .priv_class = &lut3d_class,
  560. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  561. };
  562. #endif
  563. #if CONFIG_HALDCLUT_FILTER
  564. static void update_clut(LUT3DContext *lut3d, const AVFrame *frame)
  565. {
  566. const uint8_t *data = frame->data[0];
  567. const int linesize = frame->linesize[0];
  568. const int w = lut3d->clut_width;
  569. const int step = lut3d->clut_step;
  570. const uint8_t *rgba_map = lut3d->clut_rgba_map;
  571. const int level = lut3d->lutsize;
  572. #define LOAD_CLUT(nbits) do { \
  573. int i, j, k, x = 0, y = 0; \
  574. \
  575. for (k = 0; k < level; k++) { \
  576. for (j = 0; j < level; j++) { \
  577. for (i = 0; i < level; i++) { \
  578. const uint##nbits##_t *src = (const uint##nbits##_t *) \
  579. (data + y*linesize + x*step); \
  580. struct rgbvec *vec = &lut3d->lut[k][j][i]; \
  581. vec->r = src[rgba_map[0]] / (float)((1<<(nbits)) - 1); \
  582. vec->g = src[rgba_map[1]] / (float)((1<<(nbits)) - 1); \
  583. vec->b = src[rgba_map[2]] / (float)((1<<(nbits)) - 1); \
  584. if (++x == w) { \
  585. x = 0; \
  586. y++; \
  587. } \
  588. } \
  589. } \
  590. } \
  591. } while (0)
  592. if (!lut3d->clut_is16bit) LOAD_CLUT(8);
  593. else LOAD_CLUT(16);
  594. }
  595. static int config_output(AVFilterLink *outlink)
  596. {
  597. AVFilterContext *ctx = outlink->src;
  598. LUT3DContext *lut3d = ctx->priv;
  599. int ret;
  600. outlink->w = ctx->inputs[0]->w;
  601. outlink->h = ctx->inputs[0]->h;
  602. outlink->time_base = ctx->inputs[0]->time_base;
  603. if ((ret = ff_dualinput_init(ctx, &lut3d->dinput)) < 0)
  604. return ret;
  605. return 0;
  606. }
  607. static int filter_frame_hald(AVFilterLink *inlink, AVFrame *inpicref)
  608. {
  609. LUT3DContext *s = inlink->dst->priv;
  610. return ff_dualinput_filter_frame(&s->dinput, inlink, inpicref);
  611. }
  612. static int request_frame(AVFilterLink *outlink)
  613. {
  614. LUT3DContext *s = outlink->src->priv;
  615. return ff_dualinput_request_frame(&s->dinput, outlink);
  616. }
  617. static int config_clut(AVFilterLink *inlink)
  618. {
  619. int size, level, w, h;
  620. AVFilterContext *ctx = inlink->dst;
  621. LUT3DContext *lut3d = ctx->priv;
  622. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  623. lut3d->clut_is16bit = 0;
  624. switch (inlink->format) {
  625. case AV_PIX_FMT_RGB48:
  626. case AV_PIX_FMT_BGR48:
  627. case AV_PIX_FMT_RGBA64:
  628. case AV_PIX_FMT_BGRA64:
  629. lut3d->clut_is16bit = 1;
  630. }
  631. lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3;
  632. ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format);
  633. if (inlink->w > inlink->h)
  634. av_log(ctx, AV_LOG_INFO, "Padding on the right (%dpx) of the "
  635. "Hald CLUT will be ignored\n", inlink->w - inlink->h);
  636. else if (inlink->w < inlink->h)
  637. av_log(ctx, AV_LOG_INFO, "Padding at the bottom (%dpx) of the "
  638. "Hald CLUT will be ignored\n", inlink->h - inlink->w);
  639. lut3d->clut_width = w = h = FFMIN(inlink->w, inlink->h);
  640. for (level = 1; level*level*level < w; level++);
  641. size = level*level*level;
  642. if (size != w) {
  643. av_log(ctx, AV_LOG_WARNING, "The Hald CLUT width does not match the level\n");
  644. return AVERROR_INVALIDDATA;
  645. }
  646. av_assert0(w == h && w == size);
  647. level *= level;
  648. if (level > MAX_LEVEL) {
  649. const int max_clut_level = sqrt(MAX_LEVEL);
  650. const int max_clut_size = max_clut_level*max_clut_level*max_clut_level;
  651. av_log(ctx, AV_LOG_ERROR, "Too large Hald CLUT "
  652. "(maximum level is %d, or %dx%d CLUT)\n",
  653. max_clut_level, max_clut_size, max_clut_size);
  654. return AVERROR(EINVAL);
  655. }
  656. lut3d->lutsize = level;
  657. return 0;
  658. }
  659. static AVFrame *update_apply_clut(AVFilterContext *ctx, AVFrame *main,
  660. const AVFrame *second)
  661. {
  662. AVFilterLink *inlink = ctx->inputs[0];
  663. update_clut(ctx->priv, second);
  664. return apply_lut(inlink, main);
  665. }
  666. static av_cold int haldclut_init(AVFilterContext *ctx)
  667. {
  668. LUT3DContext *lut3d = ctx->priv;
  669. lut3d->dinput.process = update_apply_clut;
  670. return 0;
  671. }
  672. static av_cold void haldclut_uninit(AVFilterContext *ctx)
  673. {
  674. LUT3DContext *lut3d = ctx->priv;
  675. ff_dualinput_uninit(&lut3d->dinput);
  676. }
  677. static const AVOption haldclut_options[] = {
  678. { "shortest", "force termination when the shortest input terminates", OFFSET(dinput.shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
  679. { "repeatlast", "continue applying the last clut after eos", OFFSET(dinput.repeatlast), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS },
  680. COMMON_OPTIONS
  681. };
  682. AVFILTER_DEFINE_CLASS(haldclut);
  683. static const AVFilterPad haldclut_inputs[] = {
  684. {
  685. .name = "main",
  686. .type = AVMEDIA_TYPE_VIDEO,
  687. .filter_frame = filter_frame_hald,
  688. .config_props = config_input,
  689. },{
  690. .name = "clut",
  691. .type = AVMEDIA_TYPE_VIDEO,
  692. .filter_frame = filter_frame_hald,
  693. .config_props = config_clut,
  694. },
  695. { NULL }
  696. };
  697. static const AVFilterPad haldclut_outputs[] = {
  698. {
  699. .name = "default",
  700. .type = AVMEDIA_TYPE_VIDEO,
  701. .request_frame = request_frame,
  702. .config_props = config_output,
  703. },
  704. { NULL }
  705. };
  706. AVFilter ff_vf_haldclut = {
  707. .name = "haldclut",
  708. .description = NULL_IF_CONFIG_SMALL("Adjust colors using a Hald CLUT."),
  709. .priv_size = sizeof(LUT3DContext),
  710. .init = haldclut_init,
  711. .uninit = haldclut_uninit,
  712. .query_formats = query_formats,
  713. .inputs = haldclut_inputs,
  714. .outputs = haldclut_outputs,
  715. .priv_class = &haldclut_class,
  716. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  717. };
  718. #endif