FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_pseudocolor.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 0 290 0.0%
Branches: 0 143 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2017 Paul B Mahol
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 #include "libavutil/attributes.h"
22 #include "libavutil/common.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 static const char *const var_names[] = {
33 "w", ///< width of the input video
34 "h", ///< height of the input video
35 "val", ///< input value for the pixel
36 "ymin",
37 "umin",
38 "vmin",
39 "amin",
40 "ymax",
41 "umax",
42 "vmax",
43 "amax",
44 NULL
45 };
46
47 enum var_name {
48 VAR_W,
49 VAR_H,
50 VAR_VAL,
51 VAR_YMIN,
52 VAR_UMIN,
53 VAR_VMIN,
54 VAR_AMIN,
55 VAR_YMAX,
56 VAR_UMAX,
57 VAR_VMAX,
58 VAR_AMAX,
59 VAR_VARS_NB
60 };
61
62 enum Curves {
63 MAGMA,
64 INFERNO,
65 PLASMA,
66 VIRIDIS,
67 TURBO,
68 CIVIDIS,
69 SOLAR,
70 SPECTRAL,
71 NB_CURVES,
72 };
73
74 enum Presets {
75 PRESET_MAGMA,
76 PRESET_INFERNO,
77 PRESET_PLASMA,
78 PRESET_VIRIDIS,
79 PRESET_TURBO,
80 PRESET_CIVIDIS,
81 PRESET_RANGE1,
82 PRESET_RANGE2,
83 PRESET_SHADOWS,
84 PRESET_HIGHLIGHTS,
85 PRESET_SOLAR,
86 PRESET_NOMINAL,
87 PRESET_PREFERRED,
88 PRESET_TOTAL,
89 PRESET_SPECTRAL,
90 NB_PRESETS,
91 };
92
93 typedef double (*curve_fun)(double x);
94
95 typedef struct Curve {
96 double coef[3][8];
97 double offset[3];
98 curve_fun fun[3];
99 } Curve;
100
101 typedef struct Fill {
102 float fill[4];
103 } Fill;
104
105 typedef struct Range {
106 AVRational start, end;
107 } Range;
108
109 typedef struct Preset {
110 int nb_segments;
111 const Range *ranges;
112 const Curve *curves;
113 const Fill *fills;
114 } Preset;
115
116 static const Range full_range = {{0, 1}, {1, 1}};
117 static const Range nominal_range[] = {{{0, 1}, {4096, 65536}}, {{4096, 65536}, {60161, 65536}}, {{60161, 65536}, {1, 1}}};
118 static const Range preferred_range[] = {{{0, 1}, {1280, 65536}}, {{1280, 65536}, {62977, 65536}}, {{62977, 65536}, {1, 1}}};
119 static const Range total_range[] = {{{0, 1}, {256, 65536}}, {{256, 65536}, {65280, 65536}}, {{65280, 65536}, {1, 1}}};
120 static const Range spec1_range[] = {{{0, 1}, {16, 256}}, {{16, 256}, {236, 256}}, {{236, 256}, {256, 256}}};
121 static const Range spec2_range[] = {{{0, 1}, {16, 256}}, {{16, 256}, {22, 256}}, {{22, 256}, {226, 256}}, {{226, 256}, {236, 256}}, {{236, 256}, {256, 256}}};
122 static const Range shadows_range[] = {{{0, 1}, {32, 256}}, {{32, 256}, {256, 256}}};
123 static const Range highlights_range[] = {{{0,1}, {214,256}}, {{214, 256}, {224, 256}}, {{224, 256}, {256, 256}}};
124
125 static const Fill spec1_fills[] = {{{0.5f, 0.f, .5f, 1.f}}, {{-1.f, -1.f, -1.f, 1.f}}, {{1.f, 0.f, 0.f, 1.f}}};
126 static const Fill spec2_fills[] = {{{0.5f, 0.f, .5f, 1.f}}, {{0.f, 1.f, 1.f, 1.f}}, {{-1.f, -1.f, -1.f, 1.f}}, {{1.f, 1.f, 0.f, 1.f}}, {{1.f, 0.f, 0.f, 1.f}}};
127 static const Fill shadows_fills[] = {{{0.8f, 0.4f, .8f, 1.f}}, {{-1.f, -1.f, -1.f, 1.f}}};
128 static const Fill highlights_fills[] = {{{-1.f, -1.f, -1.f, 1.f}}, {{1.f, 0.3f, 0.6f, 1.f}}, {{1.f, 0.2f, .5f, 1.f}}};
129
130 static double limit(double x)
131 {
132 return av_clipd(x, 0., 1.);
133 }
134
135 static double solarfun(double x)
136 {
137 return 0.5 * sin(x) + 0.5;
138 }
139
140 static const Curve curves[] =
141 {
142 [MAGMA] = {{
143 {-7.5631093e-16, 7.4289183e-13, -2.8525484e-10, 5.4446085e-08, -5.5596238e-06, 3.0569325e-04, -2.3137421e-03, 1.2152095e-02 },
144 { 1.3217636e-15, -1.2214648e-12, 4.4319712e-10, -8.0197993e-08, 7.6598370e-06, -3.6523704e-04, 8.4836670e-03, -2.5536888e-02 },
145 {-1.1446568e-15, 1.0013446e-12, -3.5651575e-10, 6.6775016e-08, -6.7120346e-06, 2.7346619e-04, 4.7969657e-03, 1.1971441e-02 },
146 }, .fun = { limit, limit, limit }, },
147 [INFERNO] = {{
148 {-3.9848859e-18, 9.4821649e-14, -6.7371977e-11, 1.8469937e-08, -2.5359307e-06, 1.7959053e-04, 3.9782564e-04, 2.8845935e-04 },
149 { 6.8408539e-16, -6.5499979e-13, 2.4562526e-10, -4.5989298e-08, 4.5723324e-06, -2.2111913e-04, 5.2023164e-03, -1.1226064e-02 },
150 {-2.9921470e-15, 2.5864165e-12, -8.7403799e-10, 1.4713388e-07, -1.2701505e-05, 4.5159935e-04, 3.1087989e-03, 1.9122831e-02 },
151 }, .fun = { limit, limit, limit }, },
152 [PLASMA] = {{
153 { 3.6196089e-16, -3.3623041e-13, 1.2324010e-10, -2.2769060e-08, 2.2297792e-06, -1.2567829e-04, 9.9791629e-03, 5.7247918e-02 },
154 { 5.0262888e-16, -5.3193896e-13, 2.2451715e-10, -4.7529623e-08, 5.1374873e-06, -2.3260136e-04, 3.1502825e-03, 1.5362491e-02 },
155 {-1.7782261e-16, 2.2487839e-13, -1.0610236e-10, 2.4112644e-08, -2.6331623e-06, 8.9499751e-05, 2.1386328e-03, 5.3824268e-01 },
156 }, .fun = { limit, limit, limit }, },
157 [VIRIDIS] = {{
158 { 9.4850045e-16, -8.6629383e-13, 3.0310944e-10, -5.1340396e-08, 4.6024275e-06, -2.2744239e-04, 4.5559993e-03, 2.5662350e-01 },
159 { 9.6461041e-17, -6.9209477e-14, 1.7625397e-11, -2.0229773e-09, 1.4900110e-07, -1.9315187e-05, 5.8967339e-03, 3.9544827e-03 },
160 { 5.1785449e-16, -3.6663004e-13, 1.0249990e-10, -1.5431998e-08, 1.5007941e-06, -1.2001502e-04, 7.6951526e-03, 3.2292815e-01 },
161 }, .fun = { limit, limit, limit }, },
162 [TURBO] = {{
163 {-4.3683890e-15, 3.7020347e-12, -1.1712592e-09, 1.6401790e-07, -8.6842919e-06, -1.8542465e-06, 8.4485325e-03, 1.6267077e-01 },
164 {-4.0011069e-16, 2.7861423e-13, -6.3388921e-11, 5.8872238e-09, -5.4466522e-07, 1.8037114e-05, 1.0599869e-02, 7.6914696e-02 },
165 {-2.8242609e-15, 2.9234108e-12, -1.1726546e-09, 2.2552115e-07, -2.0059387e-05, 5.0595552e-04, 1.7714932e-02, 2.7271836e-01 },
166 }, .fun = { limit, limit, limit }, },
167 [CIVIDIS] = {{
168 {-9.5484131e-16, 9.6988184e-13, -4.0058766e-10, 8.5743924e-08, -9.9644797e-06, 5.9197908e-04, -1.0361579e-02, 3.3164429e-02 },
169 { 1.2731941e-17, -9.4238449e-15, 2.2808841e-12, -1.1548296e-10, -2.3888913e-08, 3.8986680e-06, 2.5879330e-03, 1.2769733e-01 },
170 { 4.6004608e-16, -5.0686849e-13, 2.2753449e-10, -5.3074099e-08, 6.7196096e-06, -4.4120020e-04, 1.3435551e-02, 2.8293355e-01 },
171 }, .fun = { limit, limit, limit }, },
172 [SOLAR] = {{
173 { 0, 0, 0, 0, 0.000001983938313, -0.0007618323, 0.2, -M_PI_2 },
174 { 0, 0, 0, 0, 0.000001983938313, -0.0007618323, 0.2, -M_PI_2 },
175 { 0, 0, 0, 0, 0.000001983938313, -0.0007618323, 0.2, -M_PI_2 },
176 },
177 .offset = { 0., -9., 9. },
178 .fun = { solarfun, solarfun, solarfun }, },
179 [SPECTRAL] = {{
180 { -1.6820e-15, 1.4982e-12, -5.0442e-10, 8.0490e-08, -6.1903e-06, 1.5821e-04, 6.4359e-03, 6.2887e-01 },
181 { 1.2526e-15, -1.2203e-12, 4.7013e-10, -8.9360e-08, 8.3839e-06, -3.6642e-04, 1.4784e-02, -9.8075e-03 },
182 { 1.4755e-15, -1.6765e-12, 7.3188e-10, -1.5522e-07, 1.6406e-05, -7.7883e-04, 1.4502e-02, 2.1597e-01 },
183 }, .fun = { limit, limit, limit }, },
184 };
185
186 static const Preset presets[] =
187 {
188 [PRESET_MAGMA] = { 1, &full_range, &curves[MAGMA], NULL },
189 [PRESET_INFERNO] = { 1, &full_range, &curves[INFERNO], NULL },
190 [PRESET_PLASMA] = { 1, &full_range, &curves[PLASMA], NULL },
191 [PRESET_VIRIDIS] = { 1, &full_range, &curves[VIRIDIS], NULL },
192 [PRESET_TURBO] = { 1, &full_range, &curves[TURBO], NULL },
193 [PRESET_CIVIDIS] = { 1, &full_range, &curves[CIVIDIS], NULL },
194 [PRESET_RANGE1] = { 3, spec1_range, NULL, spec1_fills },
195 [PRESET_NOMINAL] = { 3, nominal_range, NULL, spec1_fills },
196 [PRESET_PREFERRED]={ 3, preferred_range, NULL, spec1_fills },
197 [PRESET_TOTAL] = { 3, total_range, NULL, spec1_fills },
198 [PRESET_RANGE2] = { 5, spec2_range, NULL, spec2_fills },
199 [PRESET_SHADOWS] = { 2, shadows_range, NULL, shadows_fills },
200 [PRESET_HIGHLIGHTS] = { 3, highlights_range, NULL, highlights_fills },
201 [PRESET_SOLAR] = { 1, &full_range, &curves[SOLAR], NULL },
202 [PRESET_SPECTRAL]= { 1, &full_range, &curves[SPECTRAL],NULL },
203 };
204
205 typedef struct PseudoColorContext {
206 const AVClass *class;
207 int preset;
208 float opacity;
209 int max;
210 int index;
211 int nb_planes;
212 int color;
213 int linesize[4];
214 int width[4], height[4];
215 double var_values[VAR_VARS_NB];
216 char *comp_expr_str[4];
217 AVExpr *comp_expr[4];
218 float lut[4][256*256];
219
220 void (*filter[4])(int max, int width, int height,
221 const uint8_t *index, const uint8_t *src,
222 uint8_t *dst,
223 ptrdiff_t ilinesize,
224 ptrdiff_t slinesize,
225 ptrdiff_t dlinesize,
226 float *lut,
227 float opacity);
228 } PseudoColorContext;
229
230 #define OFFSET(x) offsetof(PseudoColorContext, x)
231 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
232
233 static const AVOption pseudocolor_options[] = {
234 { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, {.str="val"}, .flags = FLAGS },
235 { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, {.str="val"}, .flags = FLAGS },
236 { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, {.str="val"}, .flags = FLAGS },
237 { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, {.str="val"}, .flags = FLAGS },
238 { "index", "set component as base", OFFSET(index), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = FLAGS },
239 { "i", "set component as base", OFFSET(index), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = FLAGS },
240 { "preset", "set preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=-1},-1, NB_PRESETS-1, .flags = FLAGS, "preset" },
241 { "p", "set preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=-1},-1, NB_PRESETS-1, .flags = FLAGS, "preset" },
242 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=-1}, .flags = FLAGS, "preset" },
243 { "magma", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MAGMA}, .flags = FLAGS, "preset" },
244 { "inferno", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INFERNO}, .flags = FLAGS, "preset" },
245 { "plasma", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_PLASMA}, .flags = FLAGS, "preset" },
246 { "viridis", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VIRIDIS}, .flags = FLAGS, "preset" },
247 { "turbo", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_TURBO}, .flags = FLAGS, "preset" },
248 { "cividis", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CIVIDIS}, .flags = FLAGS, "preset" },
249 { "range1", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_RANGE1}, .flags = FLAGS, "preset" },
250 { "range2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_RANGE2}, .flags = FLAGS, "preset" },
251 { "shadows", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_SHADOWS}, .flags = FLAGS, "preset" },
252 { "highlights", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_HIGHLIGHTS},.flags=FLAGS, "preset" },
253 { "solar", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_SOLAR}, .flags=FLAGS, "preset" },
254 { "nominal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NOMINAL}, .flags=FLAGS, "preset" },
255 { "preferred", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_PREFERRED},.flags=FLAGS,"preset" },
256 { "total", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_TOTAL}, .flags=FLAGS, "preset" },
257 { "spectral", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_SPECTRAL},.flags = FLAGS, "preset" },
258 { "opacity", "set pseudocolor opacity",OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, .flags = FLAGS },
259 { NULL }
260 };
261
262 static const enum AVPixelFormat pix_fmts[] = {
263 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
264 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
265 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
266 AV_PIX_FMT_YUV444P, AV_PIX_FMT_GBRP,
267 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_GBRAP,
268 AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUVA422P9,
269 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUVA420P9,
270 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUVA444P9,
271 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUVA420P10,
272 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUVA422P10,
273 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10,
274 AV_PIX_FMT_YUV420P12,
275 AV_PIX_FMT_YUV422P12,
276 AV_PIX_FMT_YUV444P12,
277 AV_PIX_FMT_YUV420P14,
278 AV_PIX_FMT_YUV422P14,
279 AV_PIX_FMT_YUV444P14,
280 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUVA420P16,
281 AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUVA422P16,
282 AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUVA444P16,
283 AV_PIX_FMT_GBRP9,
284 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
285 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
286 AV_PIX_FMT_GBRP14,
287 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
288 AV_PIX_FMT_NONE
289 };
290
291 static inline float lerpf(float v0, float v1, float f)
292 {
293 return v0 + (v1 - v0) * f;
294 }
295
296 #define PCLIP(v, max, dst, src, x) \
297 if (v >= 0 && v <= max) { \
298 dst[x] = lerpf(src[x], v, opacity);\
299 } else { \
300 dst[x] = src[x]; \
301 }
302
303 static void pseudocolor_filter(int max, int width, int height,
304 const uint8_t *index,
305 const uint8_t *src,
306 uint8_t *dst,
307 ptrdiff_t ilinesize,
308 ptrdiff_t slinesize,
309 ptrdiff_t dlinesize,
310 float *lut,
311 float opacity)
312 {
313 int x, y;
314
315 for (y = 0; y < height; y++) {
316 for (x = 0; x < width; x++) {
317 int v = lut[index[x]];
318
319 PCLIP(v, max, dst, src, x);
320 }
321 index += ilinesize;
322 src += slinesize;
323 dst += dlinesize;
324 }
325 }
326
327 static void pseudocolor_filter_11(int max, int width, int height,
328 const uint8_t *index,
329 const uint8_t *src,
330 uint8_t *dst,
331 ptrdiff_t ilinesize,
332 ptrdiff_t slinesize,
333 ptrdiff_t dlinesize,
334 float *lut,
335 float opacity)
336 {
337 int x, y;
338
339 for (y = 0; y < height; y++) {
340 for (x = 0; x < width; x++) {
341 int v = lut[index[(y << 1) * ilinesize + (x << 1)]];
342
343 PCLIP(v, max, dst, src, x);
344 }
345 src += slinesize;
346 dst += dlinesize;
347 }
348 }
349
350 static void pseudocolor_filter_11d(int max, int width, int height,
351 const uint8_t *index,
352 const uint8_t *src,
353 uint8_t *dst,
354 ptrdiff_t ilinesize,
355 ptrdiff_t slinesize,
356 ptrdiff_t dlinesize,
357 float *lut,
358 float opacity)
359 {
360 int x, y;
361
362 for (y = 0; y < height; y++) {
363 for (x = 0; x < width; x++) {
364 int v = lut[index[(y >> 1) * ilinesize + (x >> 1)]];
365
366 PCLIP(v, max, dst, src, x);
367 }
368 src += slinesize;
369 dst += dlinesize;
370 }
371 }
372
373 static void pseudocolor_filter_10(int max, int width, int height,
374 const uint8_t *index,
375 const uint8_t *src,
376 uint8_t *dst,
377 ptrdiff_t ilinesize,
378 ptrdiff_t slinesize,
379 ptrdiff_t dlinesize,
380 float *lut,
381 float opacity)
382 {
383 int x, y;
384
385 for (y = 0; y < height; y++) {
386 for (x = 0; x < width; x++) {
387 int v = lut[index[x << 1]];
388
389 PCLIP(v, max, dst, src, x);
390 }
391 index += ilinesize;
392 src += slinesize;
393 dst += dlinesize;
394 }
395 }
396
397 static void pseudocolor_filter_10d(int max, int width, int height,
398 const uint8_t *index,
399 const uint8_t *src,
400 uint8_t *dst,
401 ptrdiff_t ilinesize,
402 ptrdiff_t slinesize,
403 ptrdiff_t dlinesize,
404 float *lut,
405 float opacity)
406 {
407 int x, y;
408
409 for (y = 0; y < height; y++) {
410 for (x = 0; x < width; x++) {
411 int v = lut[index[x >> 1]];
412
413 PCLIP(v, max, dst, src, x);
414 }
415 index += ilinesize;
416 src += slinesize;
417 dst += dlinesize;
418 }
419 }
420
421 static void pseudocolor_filter_16(int max, int width, int height,
422 const uint8_t *iindex,
423 const uint8_t *ssrc,
424 uint8_t *ddst,
425 ptrdiff_t ilinesize,
426 ptrdiff_t slinesize,
427 ptrdiff_t dlinesize,
428 float *lut,
429 float opacity)
430 {
431 const uint16_t *index = (const uint16_t *)iindex;
432 const uint16_t *src = (const uint16_t *)ssrc;
433 uint16_t *dst = (uint16_t *)ddst;
434 int x, y;
435
436 for (y = 0; y < height; y++) {
437 for (x = 0; x < width; x++) {
438 int v = lut[index[x]];
439
440 PCLIP(v, max, dst, src, x);
441 }
442 index += ilinesize / 2;
443 src += slinesize / 2;
444 dst += dlinesize / 2;
445 }
446 }
447
448 static void pseudocolor_filter_16_10(int max, int width, int height,
449 const uint8_t *iindex,
450 const uint8_t *ssrc,
451 uint8_t *ddst,
452 ptrdiff_t ilinesize,
453 ptrdiff_t slinesize,
454 ptrdiff_t dlinesize,
455 float *lut,
456 float opacity)
457 {
458 const uint16_t *index = (const uint16_t *)iindex;
459 const uint16_t *src = (const uint16_t *)ssrc;
460 uint16_t *dst = (uint16_t *)ddst;
461 int x, y;
462
463 for (y = 0; y < height; y++) {
464 for (x = 0; x < width; x++) {
465 int v = lut[index[x << 1]];
466
467 PCLIP(v, max, dst, src, x);
468 }
469 index += ilinesize / 2;
470 src += slinesize / 2;
471 dst += dlinesize / 2;
472 }
473 }
474
475 static void pseudocolor_filter_16_10d(int max, int width, int height,
476 const uint8_t *iindex,
477 const uint8_t *ssrc,
478 uint8_t *ddst,
479 ptrdiff_t ilinesize,
480 ptrdiff_t slinesize,
481 ptrdiff_t dlinesize,
482 float *lut,
483 float opacity)
484 {
485 const uint16_t *index = (const uint16_t *)iindex;
486 const uint16_t *src = (const uint16_t *)ssrc;
487 uint16_t *dst = (uint16_t *)ddst;
488 int x, y;
489
490 for (y = 0; y < height; y++) {
491 for (x = 0; x < width; x++) {
492 int v = lut[index[x >> 1]];
493
494 PCLIP(v, max, dst, src, x);
495 }
496 index += ilinesize / 2;
497 src += slinesize / 2;
498 dst += dlinesize / 2;
499 }
500 }
501
502 static void pseudocolor_filter_16_11(int max, int width, int height,
503 const uint8_t *iindex,
504 const uint8_t *ssrc,
505 uint8_t *ddst,
506 ptrdiff_t ilinesize,
507 ptrdiff_t slinesize,
508 ptrdiff_t dlinesize,
509 float *lut,
510 float opacity)
511 {
512 const uint16_t *index = (const uint16_t *)iindex;
513 const uint16_t *src = (const uint16_t *)ssrc;
514 uint16_t *dst = (uint16_t *)ddst;
515 int x, y;
516
517 ilinesize /= 2;
518 dlinesize /= 2;
519 slinesize /= 2;
520
521 for (y = 0; y < height; y++) {
522 for (x = 0; x < width; x++) {
523 int v = lut[index[(y << 1) * ilinesize + (x << 1)]];
524
525 PCLIP(v, max, dst, src, x);
526 }
527 src += slinesize;
528 dst += dlinesize;
529 }
530 }
531
532 static void pseudocolor_filter_16_11d(int max, int width, int height,
533 const uint8_t *iindex,
534 const uint8_t *ssrc,
535 uint8_t *ddst,
536 ptrdiff_t ilinesize,
537 ptrdiff_t slinesize,
538 ptrdiff_t dlinesize,
539 float *lut,
540 float opacity)
541 {
542 const uint16_t *index = (const uint16_t *)iindex;
543 const uint16_t *src = (const uint16_t *)ssrc;
544 uint16_t *dst = (uint16_t *)ddst;
545 int x, y;
546
547 ilinesize /= 2;
548 dlinesize /= 2;
549 slinesize /= 2;
550
551 for (y = 0; y < height; y++) {
552 for (x = 0; x < width; x++) {
553 int v = lut[index[(y >> 1) * ilinesize + (x >> 1)]];
554
555 PCLIP(v, max, dst, src, x);
556 }
557 src += slinesize;
558 dst += dlinesize;
559 }
560 }
561
562 #define RGB_TO_Y_BT709(r, g, b) \
563 ((0.21260*219.0/255.0) * (r) + (0.71520*219.0/255.0) * (g) + \
564 (0.07220*219.0/255.0) * (b))
565
566 #define RGB_TO_U_BT709(r1, g1, b1, max) \
567 (-(0.11457*224.0/255.0) * r1 - (0.38543*224.0/255.0) * g1 + \
568 (0.50000*224.0/255.0) * b1 + max * 0.5)
569
570 #define RGB_TO_V_BT709(r1, g1, b1, max) \
571 ((0.50000*224.0/255.0) * r1 - (0.45415*224.0/255.0) * g1 - \
572 (0.04585*224.0/255.0) * b1 + max * 0.5)
573
574 static double poly_eval(const double *const poly, double x, curve_fun fun)
575 {
576 double res = 0.;
577
578 for (int i = 0; i < 8; i++) {
579 res += pow(x, i) * poly[7-i];
580 }
581
582 return fun(res);
583 }
584
585 static int config_input(AVFilterLink *inlink)
586 {
587 AVFilterContext *ctx = inlink->dst;
588 PseudoColorContext *s = ctx->priv;
589 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
590 int depth, ret, hsub, vsub, color, rgb;
591
592 rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
593 depth = desc->comp[0].depth;
594 s->max = (1 << depth) - 1;
595 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
596
597 if (s->index >= s->nb_planes) {
598 av_log(ctx, AV_LOG_ERROR, "index out of allowed range\n");
599 return AVERROR(EINVAL);
600 }
601
602 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
603 return ret;
604
605 hsub = desc->log2_chroma_w;
606 vsub = desc->log2_chroma_h;
607 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
608 s->height[0] = s->height[3] = inlink->h;
609 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
610 s->width[0] = s->width[3] = inlink->w;
611
612 s->var_values[VAR_W] = inlink->w;
613 s->var_values[VAR_H] = inlink->h;
614
615 s->var_values[VAR_YMIN] = 16 * (1 << (depth - 8));
616 s->var_values[VAR_UMIN] = 16 * (1 << (depth - 8));
617 s->var_values[VAR_VMIN] = 16 * (1 << (depth - 8));
618 s->var_values[VAR_AMIN] = 0;
619 s->var_values[VAR_YMAX] = 235 * (1 << (depth - 8));
620 s->var_values[VAR_UMAX] = 240 * (1 << (depth - 8));
621 s->var_values[VAR_VMAX] = 240 * (1 << (depth - 8));
622 s->var_values[VAR_AMAX] = s->max;
623
624 for (color = 0; color < s->nb_planes && s->preset < 0; color++) {
625 double res;
626 int val;
627
628 /* create the parsed expression */
629 av_expr_free(s->comp_expr[color]);
630 s->comp_expr[color] = NULL;
631 ret = av_expr_parse(&s->comp_expr[color], s->comp_expr_str[color],
632 var_names, NULL, NULL, NULL, NULL, 0, ctx);
633 if (ret < 0) {
634 av_log(ctx, AV_LOG_ERROR,
635 "Error when parsing the expression '%s' for the component %d and color %d.\n",
636 s->comp_expr_str[color], color, color);
637 return AVERROR(EINVAL);
638 }
639
640 /* compute the lut */
641 for (val = 0; val < FF_ARRAY_ELEMS(s->lut[color]); val++) {
642 s->var_values[VAR_VAL] = val;
643
644 res = av_expr_eval(s->comp_expr[color], s->var_values, s);
645 if (isnan(res)) {
646 av_log(ctx, AV_LOG_ERROR,
647 "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
648 s->comp_expr_str[color], val, color);
649 return AVERROR(EINVAL);
650 }
651 s->lut[color][val] = res;
652 }
653 }
654
655 if (s->preset >= 0) {
656 int nb_segments = presets[s->preset].nb_segments;
657
658 for (int seg = 0; seg < nb_segments; seg++) {
659 AVRational rstart = presets[s->preset].ranges[seg].start;
660 AVRational rend = presets[s->preset].ranges[seg].end;
661 int start = av_rescale_rnd(s->max + 1, rstart.num, rstart.den, AV_ROUND_UP);
662 int end = av_rescale_rnd(s->max + 1, rend.num, rend.den, AV_ROUND_UP);
663
664 for (int i = start; i < end; i++) {
665 if (!presets[s->preset].curves) {
666 const Fill fill = presets[s->preset].fills[seg];
667 double r, g, b, a;
668
669 g = fill.fill[1];
670 b = fill.fill[2];
671 r = fill.fill[0];
672 a = fill.fill[3];
673
674 if (g >= 0.f && b >= 0.f && r >= 0.f) {
675 g *= s->max;
676 b *= s->max;
677 r *= s->max;
678
679 if (!rgb) {
680 double y = RGB_TO_Y_BT709(r, g, b);
681 double u = RGB_TO_U_BT709(r, g, b, s->max);
682 double v = RGB_TO_V_BT709(r, g, b, s->max);
683
684 r = v;
685 g = y;
686 b = u;
687 }
688 }
689
690 s->lut[0][i] = g;
691 s->lut[1][i] = b;
692 s->lut[2][i] = r;
693 s->lut[3][i] = a * s->max;
694 } else {
695 const Curve curve = presets[s->preset].curves[seg];
696 const double lf = i / (double)s->max * 256.;
697 double r, g, b;
698
699 g = poly_eval(curve.coef[1], lf + curve.offset[1], curve.fun[1]) * s->max;
700 b = poly_eval(curve.coef[2], lf + curve.offset[2], curve.fun[2]) * s->max;
701 r = poly_eval(curve.coef[0], lf + curve.offset[0], curve.fun[0]) * s->max;
702
703 if (!rgb) {
704 double y = RGB_TO_Y_BT709(r, g, b);
705 double u = RGB_TO_U_BT709(r, g, b, s->max);
706 double v = RGB_TO_V_BT709(r, g, b, s->max);
707
708 r = v;
709 g = y;
710 b = u;
711 }
712
713 s->lut[0][i] = g;
714 s->lut[1][i] = b;
715 s->lut[2][i] = r;
716 s->lut[3][i] = 1.f * s->max;
717 }
718 }
719 }
720 }
721
722 switch (inlink->format) {
723 case AV_PIX_FMT_YUV444P:
724 case AV_PIX_FMT_YUVA444P:
725 case AV_PIX_FMT_GBRP:
726 case AV_PIX_FMT_GBRAP:
727 case AV_PIX_FMT_GRAY8:
728 s->filter[0] = s->filter[1] = s->filter[2] = s->filter[3] = pseudocolor_filter;
729 break;
730 case AV_PIX_FMT_YUV420P:
731 case AV_PIX_FMT_YUVA420P:
732 switch (s->index) {
733 case 0:
734 case 3:
735 s->filter[0] = s->filter[3] = pseudocolor_filter;
736 s->filter[1] = s->filter[2] = pseudocolor_filter_11;
737 break;
738 case 1:
739 case 2:
740 s->filter[0] = s->filter[3] = pseudocolor_filter_11d;
741 s->filter[1] = s->filter[2] = pseudocolor_filter;
742 break;
743 }
744 break;
745 case AV_PIX_FMT_YUV422P:
746 case AV_PIX_FMT_YUVA422P:
747 switch (s->index) {
748 case 0:
749 case 3:
750 s->filter[0] = s->filter[3] = pseudocolor_filter;
751 s->filter[1] = s->filter[2] = pseudocolor_filter_10;
752 break;
753 case 1:
754 case 2:
755 s->filter[0] = s->filter[3] = pseudocolor_filter_10d;
756 s->filter[1] = s->filter[2] = pseudocolor_filter;
757 break;
758 }
759 break;
760 case AV_PIX_FMT_YUV444P9:
761 case AV_PIX_FMT_YUVA444P9:
762 case AV_PIX_FMT_YUV444P10:
763 case AV_PIX_FMT_YUVA444P10:
764 case AV_PIX_FMT_YUV444P12:
765 case AV_PIX_FMT_YUV444P14:
766 case AV_PIX_FMT_YUV444P16:
767 case AV_PIX_FMT_YUVA444P16:
768 case AV_PIX_FMT_GBRP9:
769 case AV_PIX_FMT_GBRP10:
770 case AV_PIX_FMT_GBRP12:
771 case AV_PIX_FMT_GBRP14:
772 case AV_PIX_FMT_GBRP16:
773 case AV_PIX_FMT_GBRAP10:
774 case AV_PIX_FMT_GBRAP12:
775 case AV_PIX_FMT_GBRAP16:
776 case AV_PIX_FMT_GRAY9:
777 case AV_PIX_FMT_GRAY10:
778 case AV_PIX_FMT_GRAY12:
779 case AV_PIX_FMT_GRAY14:
780 case AV_PIX_FMT_GRAY16:
781 s->filter[0] = s->filter[1] = s->filter[2] = s->filter[3] = pseudocolor_filter_16;
782 break;
783 case AV_PIX_FMT_YUV422P9:
784 case AV_PIX_FMT_YUVA422P9:
785 case AV_PIX_FMT_YUV422P10:
786 case AV_PIX_FMT_YUVA422P10:
787 case AV_PIX_FMT_YUV422P12:
788 case AV_PIX_FMT_YUV422P14:
789 case AV_PIX_FMT_YUV422P16:
790 case AV_PIX_FMT_YUVA422P16:
791 switch (s->index) {
792 case 0:
793 case 3:
794 s->filter[0] = s->filter[3] = pseudocolor_filter_16;
795 s->filter[1] = s->filter[2] = pseudocolor_filter_16_10;
796 break;
797 case 1:
798 case 2:
799 s->filter[0] = s->filter[3] = pseudocolor_filter_16_10d;
800 s->filter[1] = s->filter[2] = pseudocolor_filter_16;
801 break;
802 }
803 break;
804 case AV_PIX_FMT_YUV420P9:
805 case AV_PIX_FMT_YUVA420P9:
806 case AV_PIX_FMT_YUV420P10:
807 case AV_PIX_FMT_YUVA420P10:
808 case AV_PIX_FMT_YUV420P12:
809 case AV_PIX_FMT_YUV420P14:
810 case AV_PIX_FMT_YUV420P16:
811 case AV_PIX_FMT_YUVA420P16:
812 switch (s->index) {
813 case 0:
814 case 3:
815 s->filter[0] = s->filter[3] = pseudocolor_filter_16;
816 s->filter[1] = s->filter[2] = pseudocolor_filter_16_11;
817 break;
818 case 1:
819 case 2:
820 s->filter[0] = s->filter[3] = pseudocolor_filter_16_11d;
821 s->filter[1] = s->filter[2] = pseudocolor_filter_16;
822 break;
823 }
824 break;
825 }
826
827 return 0;
828 }
829
830 typedef struct ThreadData {
831 AVFrame *in, *out;
832 } ThreadData;
833
834 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
835 {
836 PseudoColorContext *s = ctx->priv;
837 ThreadData *td = arg;
838 AVFrame *in = td->in;
839 AVFrame *out = td->out;
840
841 for (int plane = 0; plane < s->nb_planes; plane++) {
842 const int slice_start = (s->height[plane] * jobnr) / nb_jobs;
843 const int slice_end = (s->height[plane] * (jobnr+1)) / nb_jobs;
844 const int islice_start = (s->height[s->index] * jobnr) / nb_jobs;
845 ptrdiff_t ilinesize = in->linesize[s->index];
846 ptrdiff_t slinesize = in->linesize[plane];
847 ptrdiff_t dlinesize = out->linesize[plane];
848 const uint8_t *index = in->data[s->index] + islice_start * ilinesize;
849 const uint8_t *src = in->data[plane] + slice_start * slinesize;
850 uint8_t *dst = out->data[plane] + slice_start * dlinesize;
851
852 s->filter[plane](s->max, s->width[plane], slice_end - slice_start,
853 index, src, dst, ilinesize, slinesize,
854 dlinesize, s->lut[plane], s->opacity);
855 }
856
857 return 0;
858 }
859
860 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
861 {
862 AVFilterContext *ctx = inlink->dst;
863 PseudoColorContext *s = ctx->priv;
864 AVFilterLink *outlink = ctx->outputs[0];
865 ThreadData td;
866 AVFrame *out;
867
868 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
869 if (!out) {
870 av_frame_free(&in);
871 return AVERROR(ENOMEM);
872 }
873 av_frame_copy_props(out, in);
874
875 td.out = out, td.in = in;
876 ff_filter_execute(ctx, filter_slice, &td, NULL,
877 FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
878
879 av_frame_free(&in);
880 return ff_filter_frame(outlink, out);
881 }
882
883 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
884 char *res, int res_len, int flags)
885 {
886 int ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
887
888 if (ret < 0)
889 return ret;
890
891 return config_input(ctx->inputs[0]);
892 }
893
894 static const AVFilterPad inputs[] = {
895 {
896 .name = "default",
897 .type = AVMEDIA_TYPE_VIDEO,
898 .filter_frame = filter_frame,
899 .config_props = config_input,
900 },
901 };
902
903 static const AVFilterPad outputs[] = {
904 {
905 .name = "default",
906 .type = AVMEDIA_TYPE_VIDEO,
907 },
908 };
909
910 static av_cold void uninit(AVFilterContext *ctx)
911 {
912 PseudoColorContext *s = ctx->priv;
913 int i;
914
915 for (i = 0; i < 4; i++) {
916 av_expr_free(s->comp_expr[i]);
917 s->comp_expr[i] = NULL;
918 }
919 }
920
921 AVFILTER_DEFINE_CLASS(pseudocolor);
922
923 const AVFilter ff_vf_pseudocolor = {
924 .name = "pseudocolor",
925 .description = NULL_IF_CONFIG_SMALL("Make pseudocolored video frames."),
926 .priv_size = sizeof(PseudoColorContext),
927 .priv_class = &pseudocolor_class,
928 .uninit = uninit,
929 FILTER_INPUTS(inputs),
930 FILTER_OUTPUTS(outputs),
931 FILTER_PIXFMTS_ARRAY(pix_fmts),
932 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
933 .process_command = process_command,
934 };
935