forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvf_lut3d.c
2243 lines (1977 loc) · 97.2 KB
/
vf_lut3d.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Clément Bœsch
* Copyright (c) 2018 Paul B Mahol
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* 3D Lookup table filter
*/
#include "config_components.h"
#include "float.h"
#include "libavutil/opt.h"
#include "libavutil/file_open.h"
#include "libavutil/intfloat.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "drawutils.h"
#include "internal.h"
#include "video.h"
#include "lut3d.h"
#define R 0
#define G 1
#define B 2
#define A 3
#define OFFSET(x) offsetof(LUT3DContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
#define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
#define COMMON_OPTIONS \
{ "interp", "select interpolation mode", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, NB_INTERP_MODE-1, TFLAGS, .unit = "interp_mode" }, \
{ "nearest", "use values from the nearest defined points", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_NEAREST}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
{ "trilinear", "interpolate values using the 8 points defining a cube", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TRILINEAR}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
{ "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
{ "pyramid", "interpolate values using a pyramid", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_PYRAMID}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
{ "prism", "interpolate values using a prism", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_PRISM}, 0, 0, TFLAGS, .unit = "interp_mode" }, \
{ NULL }
#define EXPONENT_MASK 0x7F800000
#define MANTISSA_MASK 0x007FFFFF
#define SIGN_MASK 0x80000000
static inline float sanitizef(float f)
{
union av_intfloat32 t;
t.f = f;
if ((t.i & EXPONENT_MASK) == EXPONENT_MASK) {
if ((t.i & MANTISSA_MASK) != 0) {
// NAN
return 0.0f;
} else if (t.i & SIGN_MASK) {
// -INF
return -FLT_MAX;
} else {
// +INF
return FLT_MAX;
}
}
return f;
}
static inline float lerpf(float v0, float v1, float f)
{
return v0 + (v1 - v0) * f;
}
static inline struct rgbvec lerp(const struct rgbvec *v0, const struct rgbvec *v1, float f)
{
struct rgbvec v = {
lerpf(v0->r, v1->r, f), lerpf(v0->g, v1->g, f), lerpf(v0->b, v1->b, f)
};
return v;
}
#define NEAR(x) ((int)((x) + .5))
#define PREV(x) ((int)(x))
#define NEXT(x) (FFMIN((int)(x) + 1, lut3d->lutsize - 1))
/**
* Get the nearest defined point
*/
static inline struct rgbvec interp_nearest(const LUT3DContext *lut3d,
const struct rgbvec *s)
{
return lut3d->lut[NEAR(s->r) * lut3d->lutsize2 + NEAR(s->g) * lut3d->lutsize + NEAR(s->b)];
}
/**
* Interpolate using the 8 vertices of a cube
* @see https://en.wikipedia.org/wiki/Trilinear_interpolation
*/
static inline struct rgbvec interp_trilinear(const LUT3DContext *lut3d,
const struct rgbvec *s)
{
const int lutsize2 = lut3d->lutsize2;
const int lutsize = lut3d->lutsize;
const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
const struct rgbvec c000 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + prev[2]];
const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c111 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + next[2]];
const struct rgbvec c00 = lerp(&c000, &c100, d.r);
const struct rgbvec c10 = lerp(&c010, &c110, d.r);
const struct rgbvec c01 = lerp(&c001, &c101, d.r);
const struct rgbvec c11 = lerp(&c011, &c111, d.r);
const struct rgbvec c0 = lerp(&c00, &c10, d.g);
const struct rgbvec c1 = lerp(&c01, &c11, d.g);
const struct rgbvec c = lerp(&c0, &c1, d.b);
return c;
}
static inline struct rgbvec interp_pyramid(const LUT3DContext *lut3d,
const struct rgbvec *s)
{
const int lutsize2 = lut3d->lutsize2;
const int lutsize = lut3d->lutsize;
const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
const struct rgbvec c000 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + prev[2]];
const struct rgbvec c111 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + next[2]];
struct rgbvec c;
if (d.g > d.r && d.b > d.r) {
const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
c.r = c000.r + (c111.r - c011.r) * d.r + (c010.r - c000.r) * d.g + (c001.r - c000.r) * d.b +
(c011.r - c001.r - c010.r + c000.r) * d.g * d.b;
c.g = c000.g + (c111.g - c011.g) * d.r + (c010.g - c000.g) * d.g + (c001.g - c000.g) * d.b +
(c011.g - c001.g - c010.g + c000.g) * d.g * d.b;
c.b = c000.b + (c111.b - c011.b) * d.r + (c010.b - c000.b) * d.g + (c001.b - c000.b) * d.b +
(c011.b - c001.b - c010.b + c000.b) * d.g * d.b;
} else if (d.r > d.g && d.b > d.g) {
const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
c.r = c000.r + (c100.r - c000.r) * d.r + (c111.r - c101.r) * d.g + (c001.r - c000.r) * d.b +
(c101.r - c001.r - c100.r + c000.r) * d.r * d.b;
c.g = c000.g + (c100.g - c000.g) * d.r + (c111.g - c101.g) * d.g + (c001.g - c000.g) * d.b +
(c101.g - c001.g - c100.g + c000.g) * d.r * d.b;
c.b = c000.b + (c100.b - c000.b) * d.r + (c111.b - c101.b) * d.g + (c001.b - c000.b) * d.b +
(c101.b - c001.b - c100.b + c000.b) * d.r * d.b;
} else {
const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
c.r = c000.r + (c100.r - c000.r) * d.r + (c010.r - c000.r) * d.g + (c111.r - c110.r) * d.b +
(c110.r - c100.r - c010.r + c000.r) * d.r * d.g;
c.g = c000.g + (c100.g - c000.g) * d.r + (c010.g - c000.g) * d.g + (c111.g - c110.g) * d.b +
(c110.g - c100.g - c010.g + c000.g) * d.r * d.g;
c.b = c000.b + (c100.b - c000.b) * d.r + (c010.b - c000.b) * d.g + (c111.b - c110.b) * d.b +
(c110.b - c100.b - c010.b + c000.b) * d.r * d.g;
}
return c;
}
static inline struct rgbvec interp_prism(const LUT3DContext *lut3d,
const struct rgbvec *s)
{
const int lutsize2 = lut3d->lutsize2;
const int lutsize = lut3d->lutsize;
const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
const struct rgbvec c000 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + prev[2]];
const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
const struct rgbvec c111 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + next[2]];
struct rgbvec c;
if (d.b > d.r) {
const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
c.r = c000.r + (c001.r - c000.r) * d.b + (c101.r - c001.r) * d.r + (c010.r - c000.r) * d.g +
(c000.r - c010.r - c001.r + c011.r) * d.b * d.g +
(c001.r - c011.r - c101.r + c111.r) * d.r * d.g;
c.g = c000.g + (c001.g - c000.g) * d.b + (c101.g - c001.g) * d.r + (c010.g - c000.g) * d.g +
(c000.g - c010.g - c001.g + c011.g) * d.b * d.g +
(c001.g - c011.g - c101.g + c111.g) * d.r * d.g;
c.b = c000.b + (c001.b - c000.b) * d.b + (c101.b - c001.b) * d.r + (c010.b - c000.b) * d.g +
(c000.b - c010.b - c001.b + c011.b) * d.b * d.g +
(c001.b - c011.b - c101.b + c111.b) * d.r * d.g;
} else {
const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
c.r = c000.r + (c101.r - c100.r) * d.b + (c100.r - c000.r) * d.r + (c010.r - c000.r) * d.g +
(c100.r - c110.r - c101.r + c111.r) * d.b * d.g +
(c000.r - c010.r - c100.r + c110.r) * d.r * d.g;
c.g = c000.g + (c101.g - c100.g) * d.b + (c100.g - c000.g) * d.r + (c010.g - c000.g) * d.g +
(c100.g - c110.g - c101.g + c111.g) * d.b * d.g +
(c000.g - c010.g - c100.g + c110.g) * d.r * d.g;
c.b = c000.b + (c101.b - c100.b) * d.b + (c100.b - c000.b) * d.r + (c010.b - c000.b) * d.g +
(c100.b - c110.b - c101.b + c111.b) * d.b * d.g +
(c000.b - c010.b - c100.b + c110.b) * d.r * d.g;
}
return c;
}
/**
* Tetrahedral interpolation. Based on code found in Truelight Software Library paper.
* @see http://www.filmlight.ltd.uk/pdf/whitepapers/FL-TL-TN-0057-SoftwareLib.pdf
*/
static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d,
const struct rgbvec *s)
{
const int lutsize2 = lut3d->lutsize2;
const int lutsize = lut3d->lutsize;
const int prev[] = {PREV(s->r), PREV(s->g), PREV(s->b)};
const int next[] = {NEXT(s->r), NEXT(s->g), NEXT(s->b)};
const struct rgbvec d = {s->r - prev[0], s->g - prev[1], s->b - prev[2]};
const struct rgbvec c000 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + prev[2]];
const struct rgbvec c111 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + next[2]];
struct rgbvec c;
if (d.r > d.g) {
if (d.g > d.b) {
const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
c.r = (1-d.r) * c000.r + (d.r-d.g) * c100.r + (d.g-d.b) * c110.r + (d.b) * c111.r;
c.g = (1-d.r) * c000.g + (d.r-d.g) * c100.g + (d.g-d.b) * c110.g + (d.b) * c111.g;
c.b = (1-d.r) * c000.b + (d.r-d.g) * c100.b + (d.g-d.b) * c110.b + (d.b) * c111.b;
} else if (d.r > d.b) {
const struct rgbvec c100 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + prev[2]];
const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
c.r = (1-d.r) * c000.r + (d.r-d.b) * c100.r + (d.b-d.g) * c101.r + (d.g) * c111.r;
c.g = (1-d.r) * c000.g + (d.r-d.b) * c100.g + (d.b-d.g) * c101.g + (d.g) * c111.g;
c.b = (1-d.r) * c000.b + (d.r-d.b) * c100.b + (d.b-d.g) * c101.b + (d.g) * c111.b;
} else {
const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
const struct rgbvec c101 = lut3d->lut[next[0] * lutsize2 + prev[1] * lutsize + next[2]];
c.r = (1-d.b) * c000.r + (d.b-d.r) * c001.r + (d.r-d.g) * c101.r + (d.g) * c111.r;
c.g = (1-d.b) * c000.g + (d.b-d.r) * c001.g + (d.r-d.g) * c101.g + (d.g) * c111.g;
c.b = (1-d.b) * c000.b + (d.b-d.r) * c001.b + (d.r-d.g) * c101.b + (d.g) * c111.b;
}
} else {
if (d.b > d.g) {
const struct rgbvec c001 = lut3d->lut[prev[0] * lutsize2 + prev[1] * lutsize + next[2]];
const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
c.r = (1-d.b) * c000.r + (d.b-d.g) * c001.r + (d.g-d.r) * c011.r + (d.r) * c111.r;
c.g = (1-d.b) * c000.g + (d.b-d.g) * c001.g + (d.g-d.r) * c011.g + (d.r) * c111.g;
c.b = (1-d.b) * c000.b + (d.b-d.g) * c001.b + (d.g-d.r) * c011.b + (d.r) * c111.b;
} else if (d.b > d.r) {
const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c011 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + next[2]];
c.r = (1-d.g) * c000.r + (d.g-d.b) * c010.r + (d.b-d.r) * c011.r + (d.r) * c111.r;
c.g = (1-d.g) * c000.g + (d.g-d.b) * c010.g + (d.b-d.r) * c011.g + (d.r) * c111.g;
c.b = (1-d.g) * c000.b + (d.g-d.b) * c010.b + (d.b-d.r) * c011.b + (d.r) * c111.b;
} else {
const struct rgbvec c010 = lut3d->lut[prev[0] * lutsize2 + next[1] * lutsize + prev[2]];
const struct rgbvec c110 = lut3d->lut[next[0] * lutsize2 + next[1] * lutsize + prev[2]];
c.r = (1-d.g) * c000.r + (d.g-d.r) * c010.r + (d.r-d.b) * c110.r + (d.b) * c111.r;
c.g = (1-d.g) * c000.g + (d.g-d.r) * c010.g + (d.r-d.b) * c110.g + (d.b) * c111.g;
c.b = (1-d.g) * c000.b + (d.g-d.r) * c010.b + (d.r-d.b) * c110.b + (d.b) * c111.b;
}
}
return c;
}
static inline float prelut_interp_1d_linear(const Lut3DPreLut *prelut,
int idx, const float s)
{
const int lut_max = prelut->size - 1;
const float scaled = (s - prelut->min[idx]) * prelut->scale[idx];
const float x = av_clipf(scaled, 0.0f, lut_max);
const int prev = PREV(x);
const int next = FFMIN((int)(x) + 1, lut_max);
const float p = prelut->lut[idx][prev];
const float n = prelut->lut[idx][next];
const float d = x - (float)prev;
return lerpf(p, n, d);
}
static inline struct rgbvec apply_prelut(const Lut3DPreLut *prelut,
const struct rgbvec *s)
{
struct rgbvec c;
if (prelut->size <= 0)
return *s;
c.r = prelut_interp_1d_linear(prelut, 0, s->r);
c.g = prelut_interp_1d_linear(prelut, 1, s->g);
c.b = prelut_interp_1d_linear(prelut, 2, s->b);
return c;
}
#define DEFINE_INTERP_FUNC_PLANAR(name, nbits, depth) \
static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
{ \
int x, y; \
const LUT3DContext *lut3d = ctx->priv; \
const Lut3DPreLut *prelut = &lut3d->prelut; \
const ThreadData *td = arg; \
const AVFrame *in = td->in; \
const AVFrame *out = td->out; \
const int direct = out == in; \
const int slice_start = (in->height * jobnr ) / nb_jobs; \
const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
const float lut_max = lut3d->lutsize - 1; \
const float scale_f = 1.0f / ((1<<depth) - 1); \
const float scale_r = lut3d->scale.r * lut_max; \
const float scale_g = lut3d->scale.g * lut_max; \
const float scale_b = lut3d->scale.b * lut_max; \
\
for (y = slice_start; y < slice_end; y++) { \
uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \
uint##nbits##_t *dstb = (uint##nbits##_t *)brow; \
uint##nbits##_t *dstr = (uint##nbits##_t *)rrow; \
uint##nbits##_t *dsta = (uint##nbits##_t *)arow; \
const uint##nbits##_t *srcg = (const uint##nbits##_t *)srcgrow; \
const uint##nbits##_t *srcb = (const uint##nbits##_t *)srcbrow; \
const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \
const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \
for (x = 0; x < in->width; x++) { \
const struct rgbvec rgb = {srcr[x] * scale_f, \
srcg[x] * scale_f, \
srcb[x] * scale_f}; \
const struct rgbvec prelut_rgb = apply_prelut(prelut, &rgb); \
const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \
av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \
av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \
struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
dstr[x] = av_clip_uintp2(vec.r * (float)((1<<depth) - 1), depth); \
dstg[x] = av_clip_uintp2(vec.g * (float)((1<<depth) - 1), depth); \
dstb[x] = av_clip_uintp2(vec.b * (float)((1<<depth) - 1), depth); \
if (!direct && in->linesize[3]) \
dsta[x] = srca[x]; \
} \
grow += out->linesize[0]; \
brow += out->linesize[1]; \
rrow += out->linesize[2]; \
arow += out->linesize[3]; \
srcgrow += in->linesize[0]; \
srcbrow += in->linesize[1]; \
srcrrow += in->linesize[2]; \
srcarow += in->linesize[3]; \
} \
return 0; \
}
DEFINE_INTERP_FUNC_PLANAR(nearest, 8, 8)
DEFINE_INTERP_FUNC_PLANAR(trilinear, 8, 8)
DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 8, 8)
DEFINE_INTERP_FUNC_PLANAR(pyramid, 8, 8)
DEFINE_INTERP_FUNC_PLANAR(prism, 8, 8)
DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 9)
DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 9)
DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 9)
DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 9)
DEFINE_INTERP_FUNC_PLANAR(prism, 16, 9)
DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 10)
DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 10)
DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 10)
DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 10)
DEFINE_INTERP_FUNC_PLANAR(prism, 16, 10)
DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 12)
DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 12)
DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 12)
DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 12)
DEFINE_INTERP_FUNC_PLANAR(prism, 16, 12)
DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 14)
DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 14)
DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 14)
DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 14)
DEFINE_INTERP_FUNC_PLANAR(prism, 16, 14)
DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 16)
DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 16)
DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 16)
DEFINE_INTERP_FUNC_PLANAR(pyramid, 16, 16)
DEFINE_INTERP_FUNC_PLANAR(prism, 16, 16)
#define DEFINE_INTERP_FUNC_PLANAR_FLOAT(name, depth) \
static int interp_##name##_pf##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
{ \
int x, y; \
const LUT3DContext *lut3d = ctx->priv; \
const Lut3DPreLut *prelut = &lut3d->prelut; \
const ThreadData *td = arg; \
const AVFrame *in = td->in; \
const AVFrame *out = td->out; \
const int direct = out == in; \
const int slice_start = (in->height * jobnr ) / nb_jobs; \
const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \
uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \
uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \
uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \
const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \
const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \
const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \
const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \
const float lut_max = lut3d->lutsize - 1; \
const float scale_r = lut3d->scale.r * lut_max; \
const float scale_g = lut3d->scale.g * lut_max; \
const float scale_b = lut3d->scale.b * lut_max; \
\
for (y = slice_start; y < slice_end; y++) { \
float *dstg = (float *)grow; \
float *dstb = (float *)brow; \
float *dstr = (float *)rrow; \
float *dsta = (float *)arow; \
const float *srcg = (const float *)srcgrow; \
const float *srcb = (const float *)srcbrow; \
const float *srcr = (const float *)srcrrow; \
const float *srca = (const float *)srcarow; \
for (x = 0; x < in->width; x++) { \
const struct rgbvec rgb = {sanitizef(srcr[x]), \
sanitizef(srcg[x]), \
sanitizef(srcb[x])}; \
const struct rgbvec prelut_rgb = apply_prelut(prelut, &rgb); \
const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \
av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \
av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \
struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
dstr[x] = vec.r; \
dstg[x] = vec.g; \
dstb[x] = vec.b; \
if (!direct && in->linesize[3]) \
dsta[x] = srca[x]; \
} \
grow += out->linesize[0]; \
brow += out->linesize[1]; \
rrow += out->linesize[2]; \
arow += out->linesize[3]; \
srcgrow += in->linesize[0]; \
srcbrow += in->linesize[1]; \
srcrrow += in->linesize[2]; \
srcarow += in->linesize[3]; \
} \
return 0; \
}
DEFINE_INTERP_FUNC_PLANAR_FLOAT(nearest, 32)
DEFINE_INTERP_FUNC_PLANAR_FLOAT(trilinear, 32)
DEFINE_INTERP_FUNC_PLANAR_FLOAT(tetrahedral, 32)
DEFINE_INTERP_FUNC_PLANAR_FLOAT(pyramid, 32)
DEFINE_INTERP_FUNC_PLANAR_FLOAT(prism, 32)
#define DEFINE_INTERP_FUNC(name, nbits) \
static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
{ \
int x, y; \
const LUT3DContext *lut3d = ctx->priv; \
const Lut3DPreLut *prelut = &lut3d->prelut; \
const ThreadData *td = arg; \
const AVFrame *in = td->in; \
const AVFrame *out = td->out; \
const int direct = out == in; \
const int step = lut3d->step; \
const uint8_t r = lut3d->rgba_map[R]; \
const uint8_t g = lut3d->rgba_map[G]; \
const uint8_t b = lut3d->rgba_map[B]; \
const uint8_t a = lut3d->rgba_map[A]; \
const int slice_start = (in->height * jobnr ) / nb_jobs; \
const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \
uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \
const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \
const float lut_max = lut3d->lutsize - 1; \
const float scale_f = 1.0f / ((1<<nbits) - 1); \
const float scale_r = lut3d->scale.r * lut_max; \
const float scale_g = lut3d->scale.g * lut_max; \
const float scale_b = lut3d->scale.b * lut_max; \
\
for (y = slice_start; y < slice_end; y++) { \
uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \
const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \
for (x = 0; x < in->width * step; x += step) { \
const struct rgbvec rgb = {src[x + r] * scale_f, \
src[x + g] * scale_f, \
src[x + b] * scale_f}; \
const struct rgbvec prelut_rgb = apply_prelut(prelut, &rgb); \
const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \
av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \
av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \
struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \
dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1<<nbits) - 1)); \
dst[x + g] = av_clip_uint##nbits(vec.g * (float)((1<<nbits) - 1)); \
dst[x + b] = av_clip_uint##nbits(vec.b * (float)((1<<nbits) - 1)); \
if (!direct && step == 4) \
dst[x + a] = src[x + a]; \
} \
dstrow += out->linesize[0]; \
srcrow += in ->linesize[0]; \
} \
return 0; \
}
DEFINE_INTERP_FUNC(nearest, 8)
DEFINE_INTERP_FUNC(trilinear, 8)
DEFINE_INTERP_FUNC(tetrahedral, 8)
DEFINE_INTERP_FUNC(pyramid, 8)
DEFINE_INTERP_FUNC(prism, 8)
DEFINE_INTERP_FUNC(nearest, 16)
DEFINE_INTERP_FUNC(trilinear, 16)
DEFINE_INTERP_FUNC(tetrahedral, 16)
DEFINE_INTERP_FUNC(pyramid, 16)
DEFINE_INTERP_FUNC(prism, 16)
#define MAX_LINE_SIZE 512
static int skip_line(const char *p)
{
while (*p && av_isspace(*p))
p++;
return !*p || *p == '#';
}
static char* fget_next_word(char* dst, int max, FILE* f)
{
int c;
char *p = dst;
/* for null */
max--;
/* skip until next non whitespace char */
while ((c = fgetc(f)) != EOF) {
if (av_isspace(c))
continue;
*p++ = c;
max--;
break;
}
/* get max bytes or up until next whitespace char */
for (; max > 0; max--) {
if ((c = fgetc(f)) == EOF)
break;
if (av_isspace(c))
break;
*p++ = c;
}
*p = 0;
if (p == dst)
return NULL;
return p;
}
#define NEXT_LINE(loop_cond) do { \
if (!fgets(line, sizeof(line), f)) { \
av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
return AVERROR_INVALIDDATA; \
} \
} while (loop_cond)
#define NEXT_LINE_OR_GOTO(loop_cond, label) do { \
if (!fgets(line, sizeof(line), f)) { \
av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \
ret = AVERROR_INVALIDDATA; \
goto label; \
} \
} while (loop_cond)
static int allocate_3dlut(AVFilterContext *ctx, int lutsize, int prelut)
{
LUT3DContext *lut3d = ctx->priv;
int i;
if (lutsize < 2 || lutsize > MAX_LEVEL) {
av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n");
return AVERROR(EINVAL);
}
av_freep(&lut3d->lut);
lut3d->lut = av_malloc_array(lutsize * lutsize * lutsize, sizeof(*lut3d->lut));
if (!lut3d->lut)
return AVERROR(ENOMEM);
if (prelut) {
lut3d->prelut.size = PRELUT_SIZE;
for (i = 0; i < 3; i++) {
av_freep(&lut3d->prelut.lut[i]);
lut3d->prelut.lut[i] = av_malloc_array(PRELUT_SIZE, sizeof(*lut3d->prelut.lut[0]));
if (!lut3d->prelut.lut[i])
return AVERROR(ENOMEM);
}
} else {
lut3d->prelut.size = 0;
for (i = 0; i < 3; i++) {
av_freep(&lut3d->prelut.lut[i]);
}
}
lut3d->lutsize = lutsize;
lut3d->lutsize2 = lutsize * lutsize;
return 0;
}
/* Basically r g and b float values on each line, with a facultative 3DLUTSIZE
* directive; seems to be generated by Davinci */
static int parse_dat(AVFilterContext *ctx, FILE *f)
{
LUT3DContext *lut3d = ctx->priv;
char line[MAX_LINE_SIZE];
int ret, i, j, k, size, size2;
lut3d->lutsize = size = 33;
size2 = size * size;
NEXT_LINE(skip_line(line));
if (!strncmp(line, "3DLUTSIZE ", 10)) {
size = strtol(line + 10, NULL, 0);
NEXT_LINE(skip_line(line));
}
ret = allocate_3dlut(ctx, size, 0);
if (ret < 0)
return ret;
for (k = 0; k < size; k++) {
for (j = 0; j < size; j++) {
for (i = 0; i < size; i++) {
struct rgbvec *vec = &lut3d->lut[k * size2 + j * size + i];
if (k != 0 || j != 0 || i != 0)
NEXT_LINE(skip_line(line));
if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
return AVERROR_INVALIDDATA;
}
}
}
return 0;
}
/* Iridas format */
static int parse_cube(AVFilterContext *ctx, FILE *f)
{
LUT3DContext *lut3d = ctx->priv;
char line[MAX_LINE_SIZE];
float min[3] = {0.0, 0.0, 0.0};
float max[3] = {1.0, 1.0, 1.0};
while (fgets(line, sizeof(line), f)) {
if (!strncmp(line, "LUT_3D_SIZE", 11)) {
int ret, i, j, k;
const int size = strtol(line + 12, NULL, 0);
const int size2 = size * size;
ret = allocate_3dlut(ctx, size, 0);
if (ret < 0)
return ret;
for (k = 0; k < size; k++) {
for (j = 0; j < size; j++) {
for (i = 0; i < size; i++) {
struct rgbvec *vec = &lut3d->lut[i * size2 + j * size + k];
do {
try_again:
NEXT_LINE(0);
if (!strncmp(line, "DOMAIN_", 7)) {
float *vals = NULL;
if (!strncmp(line + 7, "MIN ", 4)) vals = min;
else if (!strncmp(line + 7, "MAX ", 4)) vals = max;
if (!vals)
return AVERROR_INVALIDDATA;
if (av_sscanf(line + 11, "%f %f %f", vals, vals + 1, vals + 2) != 3)
return AVERROR_INVALIDDATA;
av_log(ctx, AV_LOG_DEBUG, "min: %f %f %f | max: %f %f %f\n",
min[0], min[1], min[2], max[0], max[1], max[2]);
goto try_again;
} else if (!strncmp(line, "TITLE", 5)) {
goto try_again;
}
} while (skip_line(line));
if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3)
return AVERROR_INVALIDDATA;
}
}
}
break;
}
}
lut3d->scale.r = av_clipf(1. / (max[0] - min[0]), 0.f, 1.f);
lut3d->scale.g = av_clipf(1. / (max[1] - min[1]), 0.f, 1.f);
lut3d->scale.b = av_clipf(1. / (max[2] - min[2]), 0.f, 1.f);
return 0;
}
/* Assume 17x17x17 LUT with a 16-bit depth
* FIXME: it seems there are various 3dl formats */
static int parse_3dl(AVFilterContext *ctx, FILE *f)
{
char line[MAX_LINE_SIZE];
LUT3DContext *lut3d = ctx->priv;
int ret, i, j, k;
const int size = 17;
const int size2 = 17 * 17;
const float scale = 16*16*16;
lut3d->lutsize = size;
ret = allocate_3dlut(ctx, size, 0);
if (ret < 0)
return ret;
NEXT_LINE(skip_line(line));
for (k = 0; k < size; k++) {
for (j = 0; j < size; j++) {
for (i = 0; i < size; i++) {
int r, g, b;
struct rgbvec *vec = &lut3d->lut[k * size2 + j * size + i];
NEXT_LINE(skip_line(line));
if (av_sscanf(line, "%d %d %d", &r, &g, &b) != 3)
return AVERROR_INVALIDDATA;
vec->r = r / scale;
vec->g = g / scale;
vec->b = b / scale;
}
}
}
return 0;
}
/* Pandora format */
static int parse_m3d(AVFilterContext *ctx, FILE *f)
{
LUT3DContext *lut3d = ctx->priv;
float scale;
int ret, i, j, k, size, size2, in = -1, out = -1;
char line[MAX_LINE_SIZE];
uint8_t rgb_map[3] = {0, 1, 2};
while (fgets(line, sizeof(line), f)) {
if (!strncmp(line, "in", 2)) in = strtol(line + 2, NULL, 0);
else if (!strncmp(line, "out", 3)) out = strtol(line + 3, NULL, 0);
else if (!strncmp(line, "values", 6)) {
const char *p = line + 6;
#define SET_COLOR(id) do { \
while (av_isspace(*p)) \
p++; \
switch (*p) { \
case 'r': rgb_map[id] = 0; break; \
case 'g': rgb_map[id] = 1; break; \
case 'b': rgb_map[id] = 2; break; \
} \
while (*p && !av_isspace(*p)) \
p++; \
} while (0)
SET_COLOR(0);
SET_COLOR(1);
SET_COLOR(2);
break;
}
}
if (in == -1 || out == -1) {
av_log(ctx, AV_LOG_ERROR, "in and out must be defined\n");
return AVERROR_INVALIDDATA;
}
if (in < 2 || out < 2 ||
in > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL ||
out > MAX_LEVEL*MAX_LEVEL*MAX_LEVEL) {
av_log(ctx, AV_LOG_ERROR, "invalid in (%d) or out (%d)\n", in, out);
return AVERROR_INVALIDDATA;
}
for (size = 1; size*size*size < in; size++);
lut3d->lutsize = size;
size2 = size * size;
ret = allocate_3dlut(ctx, size, 0);
if (ret < 0)
return ret;
scale = 1. / (out - 1);
for (k = 0; k < size; k++) {
for (j = 0; j < size; j++) {
for (i = 0; i < size; i++) {
struct rgbvec *vec = &lut3d->lut[k * size2 + j * size + i];
float val[3];
NEXT_LINE(0);
if (av_sscanf(line, "%f %f %f", val, val + 1, val + 2) != 3)
return AVERROR_INVALIDDATA;
vec->r = val[rgb_map[0]] * scale;
vec->g = val[rgb_map[1]] * scale;
vec->b = val[rgb_map[2]] * scale;
}
}
}
return 0;
}
static int nearest_sample_index(float *data, float x, int low, int hi)
{
int mid;
if (x < data[low])
return low;
if (x > data[hi])
return hi;
for (;;) {
av_assert0(x >= data[low]);
av_assert0(x <= data[hi]);
av_assert0((hi-low) > 0);
if (hi - low == 1)
return low;
mid = (low + hi) / 2;
if (x < data[mid])
hi = mid;
else
low = mid;
}
return 0;
}
#define NEXT_FLOAT_OR_GOTO(value, label) \
if (!fget_next_word(line, sizeof(line) ,f)) { \
ret = AVERROR_INVALIDDATA; \
goto label; \
} \
if (av_sscanf(line, "%f", &value) != 1) { \
ret = AVERROR_INVALIDDATA; \
goto label; \
}
static int parse_cinespace(AVFilterContext *ctx, FILE *f)
{
LUT3DContext *lut3d = ctx->priv;
char line[MAX_LINE_SIZE];
float in_min[3] = {0.0, 0.0, 0.0};
float in_max[3] = {1.0, 1.0, 1.0};
float out_min[3] = {0.0, 0.0, 0.0};
float out_max[3] = {1.0, 1.0, 1.0};
int inside_metadata = 0, size, size2;
int prelut = 0;
int ret = 0;
int prelut_sizes[3] = {0, 0, 0};
float *in_prelut[3] = {NULL, NULL, NULL};
float *out_prelut[3] = {NULL, NULL, NULL};
NEXT_LINE_OR_GOTO(skip_line(line), end);
if (strncmp(line, "CSPLUTV100", 10)) {
av_log(ctx, AV_LOG_ERROR, "Not cineSpace LUT format\n");
ret = AVERROR(EINVAL);
goto end;
}
NEXT_LINE_OR_GOTO(skip_line(line), end);
if (strncmp(line, "3D", 2)) {
av_log(ctx, AV_LOG_ERROR, "Not 3D LUT format\n");
ret = AVERROR(EINVAL);
goto end;
}
while (1) {
NEXT_LINE_OR_GOTO(skip_line(line), end);
if (!strncmp(line, "BEGIN METADATA", 14)) {
inside_metadata = 1;
continue;
}
if (!strncmp(line, "END METADATA", 12)) {
inside_metadata = 0;
continue;
}
if (inside_metadata == 0) {
int size_r, size_g, size_b;
for (int i = 0; i < 3; i++) {
int npoints = strtol(line, NULL, 0);
if (npoints > 2) {
float v,last;
if (npoints > PRELUT_SIZE) {
av_log(ctx, AV_LOG_ERROR, "Prelut size too large.\n");
ret = AVERROR_INVALIDDATA;
goto end;
}
if (in_prelut[i] || out_prelut[i]) {
av_log(ctx, AV_LOG_ERROR, "Invalid file has multiple preluts.\n");
ret = AVERROR_INVALIDDATA;
goto end;
}
in_prelut[i] = (float*)av_malloc(npoints * sizeof(float));
out_prelut[i] = (float*)av_malloc(npoints * sizeof(float));
if (!in_prelut[i] || !out_prelut[i]) {
ret = AVERROR(ENOMEM);
goto end;
}
prelut_sizes[i] = npoints;
in_min[i] = FLT_MAX;
in_max[i] = -FLT_MAX;
out_min[i] = FLT_MAX;
out_max[i] = -FLT_MAX;
for (int j = 0; j < npoints; j++) {
NEXT_FLOAT_OR_GOTO(v, end)
in_min[i] = FFMIN(in_min[i], v);
in_max[i] = FFMAX(in_max[i], v);
in_prelut[i][j] = v;
if (j > 0 && v < last) {
av_log(ctx, AV_LOG_ERROR, "Invalid file, non increasing prelut.\n");
ret = AVERROR(ENOMEM);
goto end;
}
last = v;
}
for (int j = 0; j < npoints; j++) {
NEXT_FLOAT_OR_GOTO(v, end)
out_min[i] = FFMIN(out_min[i], v);
out_max[i] = FFMAX(out_max[i], v);
out_prelut[i][j] = v;
}
} else if (npoints == 2) {
NEXT_LINE_OR_GOTO(skip_line(line), end);
if (av_sscanf(line, "%f %f", &in_min[i], &in_max[i]) != 2) {
ret = AVERROR_INVALIDDATA;
goto end;
}
NEXT_LINE_OR_GOTO(skip_line(line), end);
if (av_sscanf(line, "%f %f", &out_min[i], &out_max[i]) != 2) {
ret = AVERROR_INVALIDDATA;
goto end;
}
} else {
av_log(ctx, AV_LOG_ERROR, "Unsupported number of pre-lut points.\n");
ret = AVERROR_PATCHWELCOME;
goto end;
}
NEXT_LINE_OR_GOTO(skip_line(line), end);
}
if (av_sscanf(line, "%d %d %d", &size_r, &size_g, &size_b) != 3) {
ret = AVERROR(EINVAL);
goto end;
}
if (size_r != size_g || size_r != size_b) {
av_log(ctx, AV_LOG_ERROR, "Unsupported size combination: %dx%dx%d.\n", size_r, size_g, size_b);
ret = AVERROR_PATCHWELCOME;
goto end;
}
size = size_r;