-
-
Notifications
You must be signed in to change notification settings - Fork 332
/
video.cpp
3906 lines (3328 loc) · 93 KB
/
video.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <linux/fb.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include "hardware.h"
#include "user_io.h"
#include "spi.h"
#include "cfg.h"
#include "file_io.h"
#include "mat4x4.h"
#include "menu.h"
#include "video.h"
#include "input.h"
#include "shmem.h"
#include "smbus.h"
#include "str_util.h"
#include "profiling.h"
#include "offload.h"
#include "support.h"
#include "lib/imlib2/Imlib2.h"
#include "lib/md5/md5.h"
#define FB_SIZE (1920*1080)
#define FB_ADDR (0x20000000 + (32*1024*1024)) // 512mb + 32mb(Core's fb)
/*
-- [2:0] : 011=8bpp(palette) 100=16bpp 101=24bpp 110=32bpp
-- [3] : 0=16bits 565 1=16bits 1555
-- [4] : 0=RGB 1=BGR (for 16/24/32 modes)
-- [5] : TBD
*/
#define FB_FMT_565 0b00100
#define FB_FMT_1555 0b01100
#define FB_FMT_888 0b00101
#define FB_FMT_8888 0b00110
#define FB_FMT_PAL8 0b00011
#define FB_FMT_RxB 0b10000
#define FB_EN 0x8000
#define FB_DV_LBRD 3
#define FB_DV_RBRD 6
#define FB_DV_UBRD 2
#define FB_DV_BBRD 2
#define VRR_NONE 0x00
#define VRR_FREESYNC 0x01
#define VRR_VESA 0x02
static int use_vrr = 0;
static uint8_t vrr_min_fr = 0;
static uint8_t vrr_max_fr = 0;
static volatile uint32_t *fb_base = 0;
static int fb_enabled = 0;
static int fb_width = 0;
static int fb_height = 0;
static int fb_num = 0;
static int brd_x = 0;
static int brd_y = 0;
static int menu_bg = 0;
static int menu_bgn = 0;
static VideoInfo current_video_info;
static int support_FHD = 0;
yc_mode yc_modes[10];
struct vrr_cap_t
{
uint8_t active;
uint8_t available;
uint8_t min_fr;
uint8_t max_fr;
char description[128];
};
static vrr_cap_t vrr_modes[3] = {
{0, 0, 0, 0, "None"},
{0, 0, 0, 0, "AMD Freesync"},
{0, 0, 0, 0, "Vesa Forum VRR"},
};
static uint8_t last_vrr_mode = 0xFF;
static float last_vrr_rate = 0.0f;
static uint32_t last_vrr_vfp = 0;
static uint8_t edid[256] = {};
struct vmode_t
{
uint32_t vpar[8];
double Fpix;
uint8_t vic_mode;
uint8_t pr;
};
vmode_t vmodes[] =
{
{ { 1280, 110, 40, 220, 720, 5, 5, 20 }, 74.25, 4, 0 }, //0 1280x720@60
{ { 1024, 24, 136, 160, 768, 3, 6, 29 }, 65, 0, 0 }, //1 1024x768@60
{ { 720, 16, 62, 60, 480, 9, 6, 30 }, 27, 3, 0 }, //2 720x480@60
{ { 720, 12, 64, 68, 576, 5, 5, 39 }, 27, 18, 0 }, //3 720x576@50
{ { 1280, 48, 112, 248, 1024, 1, 3, 38 }, 108, 0, 0 }, //4 1280x1024@60
{ { 800, 40, 128, 88, 600, 1, 4, 23 }, 40, 0, 0 }, //5 800x600@60
{ { 640, 16, 96, 48, 480, 10, 2, 33 }, 25.175, 1, 0 }, //6 640x480@60
{ { 1280, 440, 40, 220, 720, 5, 5, 20 }, 74.25, 19, 0 }, //7 1280x720@50
{ { 1920, 88, 44, 148, 1080, 4, 5, 36 }, 148.5, 16, 0 }, //8 1920x1080@60
{ { 1920, 528, 44, 148, 1080, 4, 5, 36 }, 148.5, 31, 0 }, //9 1920x1080@50
{ { 1366, 70, 143, 213, 768, 3, 3, 24 }, 85.5, 0, 0 }, //10 1366x768@60
{ { 1024, 40, 104, 144, 600, 1, 3, 18 }, 48.96, 0, 0 }, //11 1024x600@60
{ { 1920, 48, 32, 80, 1440, 2, 4, 38 }, 185.203, 0, 0 }, //12 1920x1440@60
{ { 2048, 48, 32, 80, 1536, 2, 4, 38 }, 209.318, 0, 0 }, //13 2048x1536@60
{ { 1280, 24, 16, 40, 1440, 3, 5, 33 }, 120.75, 0, 1 }, //14 2560x1440@60 (pr)
};
#define VMODES_NUM (sizeof(vmodes) / sizeof(vmodes[0]))
vmode_t tvmodes[] =
{
{{ 640, 30, 60, 70, 240, 4, 4, 14 }, 12.587, 0, 0 }, //NTSC 15K
{{ 640, 16, 96, 48, 480, 8, 4, 33 }, 25.175, 0, 0 }, //NTSC 31K
{{ 640, 30, 60, 70, 288, 6, 4, 14 }, 12.587, 0, 0 }, //PAL 15K
{{ 640, 16, 96, 48, 576, 2, 4, 42 }, 25.175, 0, 0 }, //PAL 31K
};
// named aliases for vmode_custom_t items
struct vmode_custom_param_t
{
uint32_t mode;
// [1]
uint32_t hact;
uint32_t hfp;
uint32_t hs;
uint32_t hbp;
// [5]
uint32_t vact;
uint32_t vfp;
uint32_t vs;
uint32_t vbp;
// [9]
uint32_t pll[12];
// [21]
uint32_t hpol;
uint32_t vpol;
uint32_t vic;
uint32_t rb;
uint32_t pr;
// [26]
uint32_t unused[6];
};
struct vmode_custom_t
{
union // anonymous
{
vmode_custom_param_t param;
uint32_t item[32];
};
double Fpix;
};
static_assert(sizeof(vmode_custom_param_t) == sizeof(vmode_custom_t::item));
// Static fwd decl
static void video_fb_config();
static void video_calculate_cvt(int horiz_pixels, int vert_pixels, float refresh_rate, int reduced_blanking, vmode_custom_t *vmode);
static vmode_custom_t v_cur = {}, v_def = {}, v_pal = {}, v_ntsc = {};
static int vmode_def = 0, vmode_pal = 0, vmode_ntsc = 0;
static bool supports_pr()
{
static uint16_t video_version = 0xffff;
if (video_version == 0xffff) video_version = spi_uio_cmd(UIO_SET_VIDEO) & 1;
return video_version != 0;
}
static bool supports_vrr()
{
static uint16_t video_version = 0xffff;
if (video_version == 0xffff) video_version = spi_uio_cmd(UIO_SET_VIDEO) & 2;
return video_version != 0;
}
static uint32_t getPLLdiv(uint32_t div)
{
if (div & 1) return 0x20000 | (((div / 2) + 1) << 8) | (div / 2);
return ((div / 2) << 8) | (div / 2);
}
static int findPLLpar(double Fout, uint32_t *pc, uint32_t *pm, double *pko)
{
uint32_t c = 1;
while ((Fout*c) < 400) c++;
while (1)
{
double fvco = Fout*c;
uint32_t m = (uint32_t)(fvco / 50);
double ko = ((fvco / 50) - m);
fvco = ko + m;
fvco *= 50.f;
if (ko && (ko <= 0.05f || ko >= 0.95f))
{
printf("Fvco=%f, C=%d, M=%d, K=%f ", fvco, c, m, ko);
if (fvco > 1500.f)
{
printf("-> No exact parameters found\n");
return 0;
}
printf("-> K is outside allowed range\n");
c++;
}
else
{
*pc = c;
*pm = m;
*pko = ko;
return 1;
}
}
//will never reach here
return 0;
}
static void setPLL(double Fout, vmode_custom_t *v)
{
PROFILE_FUNCTION();
double Fpix;
double fvco, ko;
uint32_t m, c;
printf("Calculate PLL for %.4f MHz:\n", Fout);
if (!findPLLpar(Fout, &c, &m, &ko))
{
c = 1;
while ((Fout*c) < 400) c++;
fvco = Fout*c;
m = (uint32_t)(fvco / 50);
ko = ((fvco / 50) - m);
//Make sure K is in allowed range.
if (ko <= 0.05f)
{
ko = 0;
}
else if (ko >= 0.95f)
{
m++;
ko = 0;
}
}
uint32_t k = ko ? (uint32_t)(ko * 4294967296) : 1;
fvco = ko + m;
fvco *= 50.f;
Fpix = fvco / c;
printf("Fvco=%f, C=%d, M=%d, K=%f(%u) -> Fpix=%f\n", fvco, c, m, ko, k, Fpix);
v->item[9] = 4;
v->item[10] = getPLLdiv(m);
v->item[11] = 3;
v->item[12] = 0x10000;
v->item[13] = 5;
v->item[14] = getPLLdiv(c);
v->item[15] = 9;
v->item[16] = 2;
v->item[17] = 8;
v->item[18] = 7;
v->item[19] = 7;
v->item[20] = k;
v->Fpix = Fpix;
}
struct ScalerFilter
{
char mode;
char filename[1023];
};
static ScalerFilter scaler_flt[3];
struct FilterPhase
{
short t[4];
};
static constexpr int N_PHASES = 256;
struct VideoFilterDigest
{
VideoFilterDigest() { memset(md5, 0, sizeof(md5)); }
bool operator!=(const VideoFilterDigest& other) { return memcmp(md5, other.md5, sizeof(md5)) != 0; }
bool operator==(const VideoFilterDigest& other) { return memcmp(md5, other.md5, sizeof(md5)) == 0; }
unsigned char md5[16];
};
struct VideoFilter
{
bool is_adaptive;
FilterPhase phases[N_PHASES];
FilterPhase adaptive_phases[N_PHASES];
VideoFilterDigest digest;
};
static VideoFilter scaler_flt_data[3];
static bool scale_phases(FilterPhase out_phases[N_PHASES], FilterPhase *in_phases, int in_count)
{
if (!in_count)
{
return false;
}
int dup = N_PHASES / in_count;
if ((in_count * dup) != N_PHASES)
{
return false;
}
for (int i = 0; i < in_count; i++)
{
for (int j = 0; j < dup; j++)
{
out_phases[(i * dup) + j] = in_phases[i];
}
}
return true;
}
static bool read_video_filter(int type, VideoFilter *out)
{
PROFILE_FUNCTION();
fileTextReader reader = {};
FilterPhase phases[512];
int count = 0;
bool is_adaptive = false;
int scale = 2;
memset(out, 0, sizeof(VideoFilter));
static char filename[1024];
snprintf(filename, sizeof(filename), COEFF_DIR"/%s", scaler_flt[type].filename);
if (FileOpenTextReader(&reader, filename))
{
const char *line;
while ((line = FileReadLine(&reader)))
{
if (count == 0 && !strcasecmp(line, "adaptive"))
{
is_adaptive = true;
continue;
}
if (count == 0 && !strcasecmp(line, "10bit"))
{
scale = 1;
continue;
}
int phase[4];
int n = sscanf(line, "%d,%d,%d,%d", &phase[0], &phase[1], &phase[2], &phase[3]);
if (n == 4)
{
if (count >= (is_adaptive ? N_PHASES * 2 : N_PHASES)) return false; //too many
phases[count].t[0] = phase[0] * scale;
phases[count].t[1] = phase[1] * scale;
phases[count].t[2] = phase[2] * scale;
phases[count].t[3] = phase[3] * scale;
count++;
}
}
}
printf( "Filter \'%s\', phases: %d adaptive: %s\n",
scaler_flt[type].filename,
is_adaptive ? count / 2 : count,
is_adaptive ? "true" : "false" );
bool valid = false;
if (is_adaptive)
{
out->is_adaptive = true;
valid = scale_phases(out->phases, phases, count / 2);
valid = valid && scale_phases(out->adaptive_phases, phases + (count / 2), count / 2);
}
else if (count == 32 && !is_adaptive) // legacy
{
out->is_adaptive = false;
valid = scale_phases(out->phases, phases, 16);
}
else if (!is_adaptive)
{
out->is_adaptive = false;
valid = scale_phases(out->phases, phases, count);
}
if (!valid)
{
// Make a default NN filter in case of error
out->is_adaptive = false;
FilterPhase nn_phases[2] =
{
{ .t = { 0, 256, 0, 0 } },
{ .t = { 0, 0, 256, 0 } }
};
scale_phases(out->phases, nn_phases, 2);
}
MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx, (unsigned char *)&out->is_adaptive, sizeof(VideoFilter::is_adaptive));
MD5Update(&ctx, (unsigned char *)out->phases, sizeof(VideoFilter::phases));
MD5Update(&ctx, (unsigned char *)out->adaptive_phases, sizeof(VideoFilter::adaptive_phases));
MD5Final(out->digest.md5, &ctx);
return valid;
}
static void send_phases_legacy(int addr, const FilterPhase phases[N_PHASES])
{
PROFILE_FUNCTION();
for (int idx = 0; idx < N_PHASES; idx += 16)
{
const FilterPhase *p = &phases[idx];
spi_w(((p->t[0] >> 1) & 0x1FF) | ((addr + 0) << 9));
spi_w(((p->t[1] >> 1) & 0x1FF) | ((addr + 1) << 9));
spi_w(((p->t[2] >> 1) & 0x1FF) | ((addr + 2) << 9));
spi_w(((p->t[3] >> 1) & 0x1FF) | ((addr + 3) << 9));
addr += 4;
}
}
static void send_phases(int addr, const FilterPhase phases[N_PHASES], bool full_precision)
{
PROFILE_FUNCTION();
const int skip = full_precision ? 1 : 4;
const int shift = full_precision ? 0 : 1;
addr *= full_precision ? (N_PHASES * 4) : (64 * 4);
for (int idx = 0; idx < N_PHASES; idx += skip)
{
const FilterPhase *p = &phases[idx];
spi_w(addr + 0); spi_w((p->t[0] >> shift) & 0x3FF);
spi_w(addr + 1); spi_w((p->t[1] >> shift) & 0x3FF);
spi_w(addr + 2); spi_w((p->t[2] >> shift) & 0x3FF);
spi_w(addr + 3); spi_w((p->t[3] >> shift) & 0x3FF);
addr += 4;
}
}
static VideoFilterDigest horiz_filter_digest, vert_filter_digest;
static void send_video_filters(const VideoFilter *horiz, const VideoFilter *vert, int ver)
{
PROFILE_FUNCTION();
spi_uio_cmd_cont(UIO_SET_FLTCOEF);
const bool full_precision = (ver & 0x4) != 0;
const bool send_horiz = horiz_filter_digest != horiz->digest;
const bool send_vert = vert_filter_digest != vert->digest;
switch( ver & 0x3 )
{
case 1:
if (send_horiz) send_phases_legacy(0, horiz->phases);
if (send_vert) send_phases_legacy(64, vert->phases);
break;
case 2:
if (send_horiz) send_phases(0, horiz->phases, full_precision);
if (send_vert) send_phases(1, vert->phases, full_precision);
break;
case 3:
if (send_horiz) send_phases(0, horiz->phases, full_precision);
if (send_vert) send_phases(1, vert->phases, full_precision);
if (horiz->is_adaptive && send_horiz)
{
send_phases(2, horiz->adaptive_phases, full_precision);
}
else if (vert->is_adaptive && send_vert)
{
send_phases(3, vert->adaptive_phases, full_precision);
}
break;
default:
break;
}
horiz_filter_digest = horiz->digest;
vert_filter_digest = vert->digest;
DisableIO();
}
static void set_vfilter(int force)
{
PROFILE_FUNCTION();
static int last_flags = 0;
int flt_flags = spi_uio_cmd_cont(UIO_SET_FLTNUM);
if (!flt_flags || (!force && last_flags == flt_flags))
{
DisableIO();
return;
}
last_flags = flt_flags;
printf("video_set_filter: flt_flags=%d\n", flt_flags);
spi8(scaler_flt[0].mode);
DisableIO();
int vert_flt;
if (current_video_info.interlaced) vert_flt = VFILTER_HORZ;
else if ((flt_flags & 0x30) && scaler_flt[VFILTER_SCAN].mode) vert_flt = VFILTER_SCAN;
else if (scaler_flt[VFILTER_VERT].mode) vert_flt = VFILTER_VERT;
else vert_flt = VFILTER_HORZ;
send_video_filters(&scaler_flt_data[VFILTER_HORZ], &scaler_flt_data[vert_flt], flt_flags & 0xF);
}
static void setScaler()
{
PROFILE_FUNCTION();
uint32_t arc[4] = {};
for (int i = 0; i < 2; i++)
{
if (cfg.custom_aspect_ratio[i][0])
{
if (sscanf(cfg.custom_aspect_ratio[i], "%u:%u", &arc[i * 2], &arc[(i * 2) + 1]) != 2 || arc[i * 2] < 1 || arc[i * 2] > 4095 || arc[(i * 2) + 1] < 1 || arc[(i * 2) + 1] > 4095)
{
arc[(i * 2) + 0] = 0;
arc[(i * 2) + 1] = 0;
}
}
}
spi_uio_cmd_cont(UIO_SET_AR_CUST);
for (int i = 0; i < 4; i++) spi_w(arc[i]);
DisableIO();
set_vfilter(1);
}
int video_get_scaler_flt(int type)
{
return scaler_flt[type].mode;
}
char* video_get_scaler_coeff(int type, int only_name)
{
char *path = scaler_flt[type].filename;
if (only_name)
{
char *p = strrchr(path, '/');
if (p) return p + 1;
}
return path;
}
static char scaler_cfg_path[128] = { 0 };
static void video_save_scaler_cfg()
{
FileSaveConfig(scaler_cfg_path, &scaler_flt, sizeof(scaler_flt));
}
static void video_apply_scaler_flt(int type, int n)
{
scaler_flt[type].mode = (char)n;
spi_uio_cmd8(UIO_SET_FLTNUM, scaler_flt[0].mode);
set_vfilter(1);
}
void video_set_scaler_flt(int type, int n)
{
video_apply_scaler_flt(type, n);
video_save_scaler_cfg();
}
void video_apply_scaler_coeff(int type, const char *name)
{
strcpy(scaler_flt[type].filename, name);
read_video_filter(type, &scaler_flt_data[type]);
setScaler();
user_io_send_buttons(1);
}
void video_set_scaler_coeff(int type, const char *name)
{
video_apply_scaler_coeff(type, name);
video_save_scaler_cfg();
}
static void loadScalerCfg()
{
PROFILE_FUNCTION();
if (FileLoadConfig(scaler_cfg_path, &scaler_flt, sizeof(scaler_flt)))
{
if (scaler_flt[0].mode > 1)
{
memset(scaler_flt, 0, sizeof(scaler_flt));
}
}
else
{
if (cfg.vfilter_default[0])
{
strcpy(scaler_flt[VFILTER_HORZ].filename, cfg.vfilter_default);
scaler_flt[VFILTER_HORZ].mode = 1;
}
if (cfg.vfilter_vertical_default[0])
{
strcpy(scaler_flt[VFILTER_VERT].filename, cfg.vfilter_vertical_default);
scaler_flt[VFILTER_VERT].mode = 1;
}
if (cfg.vfilter_scanlines_default[0])
{
strcpy(scaler_flt[VFILTER_SCAN].filename, cfg.vfilter_scanlines_default);
scaler_flt[VFILTER_SCAN].mode = 1;
}
}
if (!read_video_filter(VFILTER_HORZ, &scaler_flt_data[VFILTER_HORZ])) memset(&scaler_flt[VFILTER_HORZ], 0, sizeof(scaler_flt[VFILTER_HORZ]));
if (!read_video_filter(VFILTER_VERT, &scaler_flt_data[VFILTER_VERT])) memset(&scaler_flt[VFILTER_VERT], 0, sizeof(scaler_flt[VFILTER_VERT]));
if (!read_video_filter(VFILTER_SCAN, &scaler_flt_data[VFILTER_SCAN])) memset(&scaler_flt[VFILTER_SCAN], 0, sizeof(scaler_flt[VFILTER_SCAN]));
}
static char active_gamma_cfg[1024] = { 0 };
static char gamma_cfg[1024] = { 0 };
static char has_gamma = 0; // set in video_init
static void setGamma()
{
PROFILE_FUNCTION();
if (!memcmp(active_gamma_cfg, gamma_cfg, sizeof(gamma_cfg))) return;
fileTextReader reader = {};
static char filename[1024];
if (!has_gamma) return;
snprintf(filename, sizeof(filename), GAMMA_DIR"/%s", gamma_cfg + 1);
if (FileOpenTextReader(&reader, filename))
{
spi_uio_cmd_cont(UIO_SET_GAMCURV);
const char *line;
int index = 0;
while ((line = FileReadLine(&reader)))
{
int c0, c1, c2;
int n = sscanf(line, "%d,%d,%d", &c0, &c1, &c2);
if (n == 1)
{
c1 = c0;
c2 = c0;
n = 3;
}
if (n == 3)
{
spi_w((index << 8) | (c0 & 0xFF));
spi_w((index << 8) | (c1 & 0xFF));
spi_w((index << 8) | (c2 & 0xFF));
index++;
if (index >= 256) break;
}
}
DisableIO();
spi_uio_cmd8(UIO_SET_GAMMA, gamma_cfg[0]);
}
memcpy(active_gamma_cfg, gamma_cfg, sizeof(gamma_cfg));
}
int video_get_gamma_en()
{
return has_gamma ? gamma_cfg[0] : -1;
}
char* video_get_gamma_curve(int only_name)
{
char *path = gamma_cfg + 1;
if (only_name)
{
char *p = strrchr(path, '/');
if (p) return p + 1;
}
return path;
}
static char gamma_cfg_path[1024] = { 0 };
static void video_save_gamma_cfg()
{
FileSaveConfig(gamma_cfg_path, &gamma_cfg, sizeof(gamma_cfg));
}
static void video_apply_gamma_en(int n)
{
gamma_cfg[0] = (char)n;
setGamma();
}
void video_set_gamma_en(int n)
{
video_apply_gamma_en(n);
video_save_gamma_cfg();
}
static void video_apply_gamma_curve(const char *name)
{
strcpy(gamma_cfg + 1, name);
setGamma();
user_io_send_buttons(1);
}
void video_set_gamma_curve(const char *name)
{
video_apply_gamma_curve(name);
video_save_gamma_cfg();
}
static void loadGammaCfg()
{
PROFILE_FUNCTION();
if (FileLoadConfig(gamma_cfg_path, &gamma_cfg, sizeof(gamma_cfg) - 1))
{
if (gamma_cfg[0] > 1)
{
memset(gamma_cfg, 0, sizeof(gamma_cfg));
}
}
}
static char shadow_mask_cfg[1024] = { 0 };
static bool has_shadow_mask = false;
#define SM_FLAG_2X ( 1 << 1 )
#define SM_FLAG_ROTATED ( 1 << 2 )
#define SM_FLAG_ENABLED ( 1 << 3 )
#define SM_FLAG(v) ( ( 0x0 << 13 ) | (v) )
#define SM_VMAX(v) ( ( 0x1 << 13 ) | (v) )
#define SM_HMAX(v) ( ( 0x2 << 13 ) | (v) )
#define SM_LUT(v) ( ( 0x3 << 13 ) | (v) )
enum
{
SM_MODE_NONE = 0,
SM_MODE_1X,
SM_MODE_2X,
SM_MODE_1X_ROTATED,
SM_MODE_2X_ROTATED,
SM_MODE_COUNT
};
static void setShadowMask()
{
PROFILE_FUNCTION();
static char filename[1024];
has_shadow_mask = 0;
if (!spi_uio_cmd_cont(UIO_SHADOWMASK))
{
DisableIO();
return;
}
has_shadow_mask = 1;
switch (video_get_shadow_mask_mode())
{
default: spi_w(SM_FLAG(0)); break;
case SM_MODE_1X: spi_w(SM_FLAG(SM_FLAG_ENABLED)); break;
case SM_MODE_2X: spi_w(SM_FLAG(SM_FLAG_ENABLED | SM_FLAG_2X)); break;
case SM_MODE_1X_ROTATED: spi_w(SM_FLAG(SM_FLAG_ENABLED | SM_FLAG_ROTATED)); break;
case SM_MODE_2X_ROTATED: spi_w(SM_FLAG(SM_FLAG_ENABLED | SM_FLAG_ROTATED | SM_FLAG_2X)); break;
}
int loaded = 0;
snprintf(filename, sizeof(filename), SMASK_DIR"/%s", shadow_mask_cfg + 1);
fileTextReader reader;
if (FileOpenTextReader(&reader, filename))
{
char *start_pos = reader.pos;
const char *line;
uint32_t res = 0;
while ((line = FileReadLine(&reader)))
{
if (!strncasecmp(line, "resolution=", 11))
{
if (sscanf(line + 11, "%u", &res))
{
if (v_cur.item[5] >= res)
{
start_pos = reader.pos;
}
}
}
}
int w = -1, h = -1;
int y = 0;
int v2 = 0;
reader.pos = start_pos;
while ((line = FileReadLine(&reader)))
{
if (w == -1)
{
if (!strcasecmp(line, "v2"))
{
v2 = 1;
continue;
}
if (!strncasecmp(line, "resolution=", 11))
{
continue;
}
int n = sscanf(line, "%d,%d", &w, &h);
if ((n != 2) || (w <= 0) || (h <= 0) || (w > 16) || (h > 16))
{
break;
}
}
else
{
unsigned int p[16];
int n = sscanf(line, "%X,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", p + 0, p + 1, p + 2, p + 3, p + 4, p + 5, p + 6, p + 7, p + 8, p + 9, p + 10, p + 11, p + 12, p + 13, p + 14, p + 15);
if (n != w)
{
break;
}
for (int x = 0; x < 16; x++) spi_w(SM_LUT(v2 ? (p[x] & 0x7FF) : (((p[x] & 7) << 8) | 0x2A)));
y += 1;
if (y == h)
{
loaded = 1;
break;
}
}
}
if (y == h)
{
spi_w(SM_HMAX(w - 1));
spi_w(SM_VMAX(h - 1));
}
}
if (!loaded) spi_w(SM_FLAG(0));
DisableIO();
}
int video_get_shadow_mask_mode()
{
return has_shadow_mask ? shadow_mask_cfg[0] : -1;
}
char* video_get_shadow_mask(int only_name)
{
char *path = shadow_mask_cfg + 1;
if (only_name)
{
char *p = strrchr(path, '/');
if (p) return p + 1;
}
return path;
}
static char shadow_mask_cfg_path[1024] = { 0 };
static void video_save_shadow_mask_cfg()
{
FileSaveConfig(shadow_mask_cfg_path, &shadow_mask_cfg, sizeof(shadow_mask_cfg));
}
static void video_apply_shadow_mask_mode(int n)
{
if( n >= SM_MODE_COUNT )
{
n = 0;
}
else if (n < 0)
{
n = SM_MODE_COUNT - 1;
}
shadow_mask_cfg[0] = (char)n;
setShadowMask();
}
void video_set_shadow_mask_mode(int n)
{
video_apply_shadow_mask_mode(n);
video_save_shadow_mask_cfg();
}
static void video_apply_shadow_mask(const char *name)
{
strcpy(shadow_mask_cfg + 1, name);
setShadowMask();
user_io_send_buttons(1);
}
void video_set_shadow_mask(const char *name)
{
video_apply_shadow_mask(name);
video_save_shadow_mask_cfg();
}
static void loadShadowMaskCfg()
{
PROFILE_FUNCTION();
if (!FileLoadConfig(shadow_mask_cfg_path, &shadow_mask_cfg, sizeof(shadow_mask_cfg) - 1))
{
if (cfg.shmask_default[0])
{
strcpy(shadow_mask_cfg + 1, cfg.shmask_default);
shadow_mask_cfg[0] = cfg.shmask_mode_default;
}
}
if( shadow_mask_cfg[0] >= SM_MODE_COUNT )
{
shadow_mask_cfg[0] = 0;
}
}
#define IS_NEWLINE(c) (((c) == '\r') || ((c) == '\n'))
#define IS_WHITESPACE(c) (IS_NEWLINE(c) || ((c) == ' ') || ((c) == '\t'))
static char* get_preset_arg(const char *str)
{
static char par[1024];
snprintf(par, sizeof(par), "%s", str);
char *pos = par;
while (*pos && !IS_NEWLINE(*pos)) pos++;
*pos-- = 0;
while (pos >= par)
{
if (!IS_WHITESPACE(*pos)) break;
*pos-- = 0;
}