LCOV - code coverage report
Current view: top level - libavfilter - vf_lut3d.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 350 0.0 %
Date: 2017-12-17 04:34:43 Functions: 0 32 0.0 %

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

Generated by: LCOV version 1.13