yscv-video 0.1.8

Video decoding (H.264, HEVC), MP4 parsing, and camera I/O
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
//! Detection overlay renderer and telemetry OSD for RGB8 frames.
//!
//! Provides low-level drawing primitives (rectangles, text) and high-level
//! functions for rendering YOLO detection boxes and flight controller telemetry.

// ---------------------------------------------------------------------------
// Built-in 8x16 bitmap font (standard VGA/PC BIOS font, ASCII 32-127)
// ---------------------------------------------------------------------------

/// Standard VGA 8x16 bitmap font covering ASCII 32-127 (96 characters).
/// Each character is 16 bytes (one per row, MSB = leftmost pixel).
const FONT_8X16: [[u8; 16]; 96] = [
    // 32: space
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 33: !
    [
        0x00, 0x00, 0x18, 0x3C, 0x3C, 0x3C, 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 34: "
    [
        0x00, 0x66, 0x66, 0x66, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 35: #
    [
        0x00, 0x00, 0x00, 0x6C, 0x6C, 0xFE, 0x6C, 0x6C, 0x6C, 0xFE, 0x6C, 0x6C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 36: $
    [
        0x18, 0x18, 0x7C, 0xC6, 0xC2, 0xC0, 0x7C, 0x06, 0x06, 0x86, 0xC6, 0x7C, 0x18, 0x18, 0x00,
        0x00,
    ],
    // 37: %
    [
        0x00, 0x00, 0x00, 0x00, 0xC2, 0xC6, 0x0C, 0x18, 0x30, 0x60, 0xC6, 0x86, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 38: &
    [
        0x00, 0x00, 0x38, 0x6C, 0x6C, 0x38, 0x76, 0xDC, 0xCC, 0xCC, 0xCC, 0x76, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 39: '
    [
        0x00, 0x30, 0x30, 0x30, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 40: (
    [
        0x00, 0x00, 0x0C, 0x18, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x18, 0x0C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 41: )
    [
        0x00, 0x00, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x18, 0x30, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 42: *
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 43: +
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 44: ,
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x30, 0x00, 0x00,
        0x00,
    ],
    // 45: -
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 46: .
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 47: /
    [
        0x00, 0x00, 0x00, 0x00, 0x02, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x80, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 48: 0
    [
        0x00, 0x00, 0x38, 0x6C, 0xC6, 0xC6, 0xD6, 0xD6, 0xC6, 0xC6, 0x6C, 0x38, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 49: 1
    [
        0x00, 0x00, 0x18, 0x38, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 50: 2
    [
        0x00, 0x00, 0x7C, 0xC6, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xC6, 0xFE, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 51: 3
    [
        0x00, 0x00, 0x7C, 0xC6, 0x06, 0x06, 0x3C, 0x06, 0x06, 0x06, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 52: 4
    [
        0x00, 0x00, 0x0C, 0x1C, 0x3C, 0x6C, 0xCC, 0xFE, 0x0C, 0x0C, 0x0C, 0x1E, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 53: 5
    [
        0x00, 0x00, 0xFE, 0xC0, 0xC0, 0xC0, 0xFC, 0x06, 0x06, 0x06, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 54: 6
    [
        0x00, 0x00, 0x38, 0x60, 0xC0, 0xC0, 0xFC, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 55: 7
    [
        0x00, 0x00, 0xFE, 0xC6, 0x06, 0x06, 0x0C, 0x18, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 56: 8
    [
        0x00, 0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 57: 9
    [
        0x00, 0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0x7E, 0x06, 0x06, 0x06, 0x0C, 0x78, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 58: :
    [
        0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 59: ;
    [
        0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 60: <
    [
        0x00, 0x00, 0x00, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 61: =
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 62: >
    [
        0x00, 0x00, 0x00, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 63: ?
    [
        0x00, 0x00, 0x7C, 0xC6, 0xC6, 0x0C, 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 64: @
    [
        0x00, 0x00, 0x00, 0x7C, 0xC6, 0xC6, 0xDE, 0xDE, 0xDE, 0xDC, 0xC0, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 65: A
    [
        0x00, 0x00, 0x10, 0x38, 0x6C, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 66: B
    [
        0x00, 0x00, 0xFC, 0x66, 0x66, 0x66, 0x7C, 0x66, 0x66, 0x66, 0x66, 0xFC, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 67: C
    [
        0x00, 0x00, 0x3C, 0x66, 0xC2, 0xC0, 0xC0, 0xC0, 0xC0, 0xC2, 0x66, 0x3C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 68: D
    [
        0x00, 0x00, 0xF8, 0x6C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x6C, 0xF8, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 69: E
    [
        0x00, 0x00, 0xFE, 0x66, 0x62, 0x68, 0x78, 0x68, 0x60, 0x62, 0x66, 0xFE, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 70: F
    [
        0x00, 0x00, 0xFE, 0x66, 0x62, 0x68, 0x78, 0x68, 0x60, 0x60, 0x60, 0xF0, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 71: G
    [
        0x00, 0x00, 0x3C, 0x66, 0xC2, 0xC0, 0xC0, 0xDE, 0xC6, 0xC6, 0x66, 0x3A, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 72: H
    [
        0x00, 0x00, 0xC6, 0xC6, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 73: I
    [
        0x00, 0x00, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 74: J
    [
        0x00, 0x00, 0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0xCC, 0xCC, 0xCC, 0x78, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 75: K
    [
        0x00, 0x00, 0xE6, 0x66, 0x66, 0x6C, 0x78, 0x78, 0x6C, 0x66, 0x66, 0xE6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 76: L
    [
        0x00, 0x00, 0xF0, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x62, 0x66, 0xFE, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 77: M
    [
        0x00, 0x00, 0xC6, 0xEE, 0xFE, 0xFE, 0xD6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 78: N
    [
        0x00, 0x00, 0xC6, 0xE6, 0xF6, 0xFE, 0xDE, 0xCE, 0xC6, 0xC6, 0xC6, 0xC6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 79: O
    [
        0x00, 0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 80: P
    [
        0x00, 0x00, 0xFC, 0x66, 0x66, 0x66, 0x7C, 0x60, 0x60, 0x60, 0x60, 0xF0, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 81: Q
    [
        0x00, 0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xD6, 0xDE, 0x7C, 0x0C, 0x0E, 0x00,
        0x00,
    ],
    // 82: R
    [
        0x00, 0x00, 0xFC, 0x66, 0x66, 0x66, 0x7C, 0x6C, 0x66, 0x66, 0x66, 0xE6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 83: S
    [
        0x00, 0x00, 0x7C, 0xC6, 0xC6, 0x60, 0x38, 0x0C, 0x06, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 84: T
    [
        0x00, 0x00, 0xFF, 0xDB, 0x99, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 85: U
    [
        0x00, 0x00, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 86: V
    [
        0x00, 0x00, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x6C, 0x38, 0x10, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 87: W
    [
        0x00, 0x00, 0xC6, 0xC6, 0xC6, 0xC6, 0xD6, 0xD6, 0xD6, 0xFE, 0xEE, 0x6C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 88: X
    [
        0x00, 0x00, 0xC6, 0xC6, 0x6C, 0x7C, 0x38, 0x38, 0x7C, 0x6C, 0xC6, 0xC6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 89: Y
    [
        0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 90: Z
    [
        0x00, 0x00, 0xFE, 0xC6, 0x86, 0x0C, 0x18, 0x30, 0x60, 0xC2, 0xC6, 0xFE, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 91: [
    [
        0x00, 0x00, 0x3C, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 92: backslash
    [
        0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0x70, 0x38, 0x1C, 0x0E, 0x06, 0x02, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 93: ]
    [
        0x00, 0x00, 0x3C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x3C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 94: ^
    [
        0x10, 0x38, 0x6C, 0xC6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 95: _
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00,
        0x00,
    ],
    // 96: `
    [
        0x00, 0x30, 0x18, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 97: a
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x0C, 0x7C, 0xCC, 0xCC, 0xCC, 0x76, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 98: b
    [
        0x00, 0x00, 0xE0, 0x60, 0x60, 0x78, 0x6C, 0x66, 0x66, 0x66, 0x66, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 99: c
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0xC6, 0xC0, 0xC0, 0xC0, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 100: d
    [
        0x00, 0x00, 0x1C, 0x0C, 0x0C, 0x3C, 0x6C, 0xCC, 0xCC, 0xCC, 0xCC, 0x76, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 101: e
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0xC6, 0xFE, 0xC0, 0xC0, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 102: f
    [
        0x00, 0x00, 0x1C, 0x36, 0x32, 0x30, 0x78, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 103: g
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x7C, 0x0C, 0xCC, 0x78,
        0x00,
    ],
    // 104: h
    [
        0x00, 0x00, 0xE0, 0x60, 0x60, 0x6C, 0x76, 0x66, 0x66, 0x66, 0x66, 0xE6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 105: i
    [
        0x00, 0x00, 0x18, 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 106: j
    [
        0x00, 0x00, 0x06, 0x06, 0x00, 0x0E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x66, 0x66, 0x3C,
        0x00,
    ],
    // 107: k
    [
        0x00, 0x00, 0xE0, 0x60, 0x60, 0x66, 0x6C, 0x78, 0x78, 0x6C, 0x66, 0xE6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 108: l
    [
        0x00, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 109: m
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0xFE, 0xD6, 0xD6, 0xD6, 0xD6, 0xC6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 110: n
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xDC, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 111: o
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 112: p
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xDC, 0x66, 0x66, 0x66, 0x66, 0x66, 0x7C, 0x60, 0x60, 0xF0,
        0x00,
    ],
    // 113: q
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x7C, 0x0C, 0x0C, 0x1E,
        0x00,
    ],
    // 114: r
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xDC, 0x76, 0x66, 0x60, 0x60, 0x60, 0xF0, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 115: s
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0xC6, 0x60, 0x38, 0x0C, 0xC6, 0x7C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 116: t
    [
        0x00, 0x00, 0x10, 0x30, 0x30, 0xFC, 0x30, 0x30, 0x30, 0x30, 0x36, 0x1C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 117: u
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x76, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 118: v
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x6C, 0x38, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 119: w
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xC6, 0xC6, 0xD6, 0xD6, 0xD6, 0xFE, 0x6C, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 120: x
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xC6, 0x6C, 0x38, 0x38, 0x38, 0x6C, 0xC6, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 121: y
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0x7E, 0x06, 0x0C, 0xF8,
        0x00,
    ],
    // 122: z
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xCC, 0x18, 0x30, 0x60, 0xC6, 0xFE, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 123: {
    [
        0x00, 0x00, 0x0E, 0x18, 0x18, 0x18, 0x70, 0x18, 0x18, 0x18, 0x18, 0x0E, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 124: |
    [
        0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 125: }
    [
        0x00, 0x00, 0x70, 0x18, 0x18, 0x18, 0x0E, 0x18, 0x18, 0x18, 0x18, 0x70, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 126: ~
    [
        0x00, 0x00, 0x76, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // 127: DEL (blank)
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
];

// ---------------------------------------------------------------------------
// Drawing primitives (operate on RGB8 byte slices, not Tensor)
// ---------------------------------------------------------------------------

/// Draw a rectangle border on an RGB8 frame buffer.
///
/// `(x, y)` is the top-left corner; `(w, h)` is the size in pixels.
/// `thickness` specifies border width (0 = no drawing).
pub fn draw_rect(
    frame: &mut [u8],
    width: usize,
    height: usize,
    x: u32,
    y: u32,
    w: u32,
    h: u32,
    r: u8,
    g: u8,
    b: u8,
    thickness: u32,
) {
    let color = [r, g, b];
    let fw = width;
    let fh = height;

    let set_pixel = |frame: &mut [u8], px: usize, py: usize| {
        if px < fw && py < fh {
            let idx = (py * fw + px) * 3;
            frame[idx] = color[0];
            frame[idx + 1] = color[1];
            frame[idx + 2] = color[2];
        }
    };

    let x = x as usize;
    let y = y as usize;
    let rect_w = w as usize;
    let rect_h = h as usize;

    for t in 0..thickness as usize {
        // Top edge
        let ty = y.wrapping_add(t); // draw outward-inward from top
        if ty < fh {
            for px in x..x.saturating_add(rect_w).min(fw) {
                set_pixel(frame, px, ty);
            }
        }
        // Bottom edge
        let by = y.saturating_add(rect_h).saturating_sub(1).saturating_sub(t);
        if by < fh && by != ty {
            for px in x..x.saturating_add(rect_w).min(fw) {
                set_pixel(frame, px, by);
            }
        }
        // Left edge
        let lx = x.wrapping_add(t);
        if lx < fw {
            for py in y..y.saturating_add(rect_h).min(fh) {
                set_pixel(frame, lx, py);
            }
        }
        // Right edge
        let rx = x.saturating_add(rect_w).saturating_sub(1).saturating_sub(t);
        if rx < fw && rx != lx {
            for py in y..y.saturating_add(rect_h).min(fh) {
                set_pixel(frame, rx, py);
            }
        }
    }
}

/// Draw text at position `(x, y)` on an RGB8 frame using the built-in 8x16 VGA font.
///
/// Characters outside the frame bounds are silently clipped. Non-ASCII bytes
/// and characters outside the 32-127 range are replaced with `?`.
pub fn draw_text(
    frame: &mut [u8],
    width: usize,
    height: usize,
    x: u32,
    y: u32,
    text: &str,
    r: u8,
    g: u8,
    b: u8,
) {
    let fw = width;
    let fh = height;

    for (ci, ch) in text.bytes().enumerate() {
        let glyph_idx = if (32..=127).contains(&ch) {
            (ch - 32) as usize
        } else {
            // '?' glyph
            31
        };
        let glyph = &FONT_8X16[glyph_idx];
        let cx = x as usize + ci * 8;

        for row in 0..16 {
            let py = y as usize + row;
            if py >= fh {
                break;
            }
            let bits = glyph[row];
            for col in 0..8 {
                let px = cx + col;
                if px >= fw {
                    break;
                }
                if bits & (0x80 >> col) != 0 {
                    let idx = (py * fw + px) * 3;
                    frame[idx] = r;
                    frame[idx + 1] = g;
                    frame[idx + 2] = b;
                }
            }
        }
    }
}

/// Draw detection bounding boxes with labels on an RGB8 frame.
///
/// Each detection is `(x, y, w, h, confidence, label)` where `(x, y)` is
/// the top-left corner and `(w, h)` is the size in pixels.
pub fn overlay_detections(
    frame: &mut [u8],
    width: usize,
    height: usize,
    detections: &[(f32, f32, f32, f32, f32, &str)],
) {
    // Color palette for different classes (cycled by label hash)
    const PALETTE: [(u8, u8, u8); 10] = [
        (255, 0, 0),     // red
        (0, 255, 0),     // green
        (0, 128, 255),   // blue
        (255, 255, 0),   // yellow
        (255, 0, 255),   // magenta
        (0, 255, 255),   // cyan
        (255, 128, 0),   // orange
        (128, 0, 255),   // purple
        (0, 128, 0),     // dark green
        (128, 128, 128), // gray
    ];

    for &(dx, dy, dw, dh, conf, label) in detections {
        // Simple hash of label for color selection
        let color_idx = label
            .bytes()
            .fold(0usize, |acc, b| acc.wrapping_add(b as usize))
            % PALETTE.len();
        let (r, g, b) = PALETTE[color_idx];

        let bx = dx as u32;
        let by = dy as u32;
        let bw = dw as u32;
        let bh = dh as u32;

        // Draw bounding box
        draw_rect(frame, width, height, bx, by, bw, bh, r, g, b, 2);

        // Draw label with confidence
        let label_text = format!("{label} {conf:.2}");
        let text_y = if by >= 18 { by - 18 } else { by + bh + 2 };
        draw_text(frame, width, height, bx, text_y, &label_text, r, g, b);
    }
}

// ---------------------------------------------------------------------------
// Telemetry OSD
// ---------------------------------------------------------------------------

/// Telemetry data from a flight controller for OSD rendering.
#[derive(Clone, Debug)]
pub struct TelemetryData {
    pub battery_voltage: f32,
    pub battery_current: f32,
    pub altitude_m: f32,
    pub speed_ms: f32,
    pub lat: f64,
    pub lon: f64,
    pub heading_deg: f32,
    pub ai_detections: u32,
}

impl Default for TelemetryData {
    fn default() -> Self {
        Self {
            battery_voltage: 0.0,
            battery_current: 0.0,
            altitude_m: 0.0,
            speed_ms: 0.0,
            lat: 0.0,
            lon: 0.0,
            heading_deg: 0.0,
            ai_detections: 0,
        }
    }
}

/// Lock-free atomic-snapshot wrapper around [`TelemetryData`].
///
/// **Why this exists:** `TelemetryData` contains `f64` fields. On 32-bit
/// ARM platforms (RV1106 Cortex-A7), `f64` reads decompose into two
/// 32-bit loads which can tear if a writer interleaves between them —
/// resulting in nonsense GPS coordinates on the OSD. `arc-swap` gives an
/// atomic pointer swap so readers see a fully-consistent snapshot.
///
/// Designed for the common pattern of one MAVLink-parser writer thread
/// and many OSD reader threads (every overlay render).
#[derive(Clone)]
pub struct SharedTelemetry(std::sync::Arc<arc_swap::ArcSwap<TelemetryData>>);

impl SharedTelemetry {
    /// Create with all-zero defaults.
    pub fn new() -> Self {
        Self(std::sync::Arc::new(arc_swap::ArcSwap::from_pointee(
            TelemetryData::default(),
        )))
    }

    /// Construct from an initial value.
    pub fn from(td: TelemetryData) -> Self {
        Self(std::sync::Arc::new(arc_swap::ArcSwap::from_pointee(td)))
    }

    /// Atomically replace the current snapshot.
    pub fn store(&self, td: TelemetryData) {
        self.0.store(std::sync::Arc::new(td));
    }

    /// Atomically read the current snapshot. Returned value is a
    /// consistent view — never torn.
    pub fn load(&self) -> std::sync::Arc<TelemetryData> {
        self.0.load_full()
    }

    /// Atomically read-modify-write: clone current, apply `f`, store back.
    /// Use for batched MAVLink update applies.
    pub fn modify<F: FnOnce(&mut TelemetryData)>(&self, f: F) {
        let mut copy = (*self.load()).clone();
        f(&mut copy);
        self.store(copy);
    }
}

impl Default for SharedTelemetry {
    fn default() -> Self {
        Self::new()
    }
}

/// Draw telemetry OSD overlay on an RGB8 frame.
///
/// Renders battery, altitude, speed, GPS coordinates, heading, and AI detection
/// count in a fixed layout: top-left for battery/altitude, top-right for GPS,
/// bottom-left for speed/heading, bottom-right for AI status.
pub fn overlay_telemetry(frame: &mut [u8], width: usize, height: usize, telemetry: &TelemetryData) {
    let white = (255u8, 255u8, 255u8);
    let green = (0u8, 255u8, 0u8);
    let yellow = (255u8, 255u8, 0u8);

    // Battery indicator color: green > 11.5V, yellow > 10.5V, red otherwise
    let batt_color = if telemetry.battery_voltage > 11.5 {
        green
    } else if telemetry.battery_voltage > 10.5 {
        yellow
    } else {
        (255, 0, 0)
    };

    // Top-left: battery
    let batt_str = format!(
        "BAT {:.1}V {:.1}A",
        telemetry.battery_voltage, telemetry.battery_current
    );
    draw_text(
        frame,
        width,
        height,
        4,
        4,
        &batt_str,
        batt_color.0,
        batt_color.1,
        batt_color.2,
    );

    // Second line: altitude
    let alt_str = format!("ALT {:.1}m", telemetry.altitude_m);
    draw_text(
        frame, width, height, 4, 22, &alt_str, white.0, white.1, white.2,
    );

    // Top-right: GPS
    let gps_str = format!("{:.6},{:.6}", telemetry.lat, telemetry.lon);
    let gps_x = width.saturating_sub(gps_str.len() * 8 + 4) as u32;
    draw_text(
        frame, width, height, gps_x, 4, &gps_str, white.0, white.1, white.2,
    );

    // Bottom-left: speed + heading
    let spd_str = format!(
        "SPD {:.1}m/s  HDG {:.0}",
        telemetry.speed_ms, telemetry.heading_deg
    );
    let spd_y = height.saturating_sub(22) as u32;
    draw_text(
        frame, width, height, 4, spd_y, &spd_str, white.0, white.1, white.2,
    );

    // Bottom-right: AI detection count
    let ai_str = format!("AI:{}", telemetry.ai_detections);
    let ai_x = width.saturating_sub(ai_str.len() * 8 + 4) as u32;
    draw_text(
        frame, width, height, ai_x, spd_y, &ai_str, green.0, green.1, green.2,
    );
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn draw_rect_sets_border_pixels() {
        let w = 32;
        let h = 32;
        let mut frame = vec![0u8; w * h * 3];

        draw_rect(&mut frame, w, h, 4, 4, 10, 10, 255, 0, 0, 1);

        // Top-left corner of rect should be red
        let idx = (4 * w + 4) * 3;
        assert_eq!(frame[idx], 255, "R at top-left corner");
        assert_eq!(frame[idx + 1], 0, "G at top-left corner");
        assert_eq!(frame[idx + 2], 0, "B at top-left corner");

        // Center should be untouched (black)
        let center_idx = (8 * w + 8) * 3;
        assert_eq!(frame[center_idx], 0, "center should be black");
    }

    #[test]
    fn draw_rect_clips_out_of_bounds() {
        let w = 16;
        let h = 16;
        let mut frame = vec![0u8; w * h * 3];

        // Rectangle extends beyond frame bounds — should not panic
        draw_rect(&mut frame, w, h, 10, 10, 20, 20, 255, 255, 255, 2);
    }

    #[test]
    fn draw_text_renders_pixels() {
        let w = 64;
        let h = 32;
        let mut frame = vec![0u8; w * h * 3];

        draw_text(&mut frame, w, h, 0, 0, "A", 255, 255, 255);

        // 'A' glyph has some set pixels — verify at least one is non-zero
        let has_pixel = frame.iter().any(|&v| v != 0);
        assert!(has_pixel, "drawing 'A' should produce visible pixels");
    }

    #[test]
    fn draw_text_clips_gracefully() {
        let w = 4;
        let h = 4;
        let mut frame = vec![0u8; w * h * 3];

        // Text much wider than frame — should not panic
        draw_text(&mut frame, w, h, 0, 0, "Hello World!", 255, 255, 255);
    }

    #[test]
    fn draw_text_replaces_non_printable() {
        let w = 64;
        let h = 32;
        let mut frame = vec![0u8; w * h * 3];

        // DEL (0x7F) maps to index 95 (blank glyph in our table).
        // Control char (0x01) should render as '?' with visible pixels.
        draw_text(&mut frame, w, h, 0, 0, "\x01", 255, 255, 255);
        let has_pixel = frame.iter().any(|&v| v != 0);
        assert!(
            has_pixel,
            "non-printable should render as '?' with visible pixels"
        );
    }

    #[test]
    fn overlay_detections_draws_boxes() {
        let w = 128;
        let h = 128;
        let mut frame = vec![0u8; w * h * 3];

        let dets = [(10.0f32, 10.0, 50.0, 50.0, 0.95, "person")];
        overlay_detections(&mut frame, w, h, &dets);

        // Should have drawn something
        let nonzero_count = frame.iter().filter(|&&v| v != 0).count();
        assert!(
            nonzero_count > 0,
            "detection overlay should produce visible pixels"
        );
    }

    #[test]
    fn overlay_telemetry_renders_text() {
        let w = 320;
        let h = 240;
        let mut frame = vec![0u8; w * h * 3];

        let telemetry = TelemetryData {
            battery_voltage: 12.4,
            battery_current: 2.1,
            altitude_m: 45.5,
            speed_ms: 8.2,
            lat: 55.753215,
            lon: 37.622504,
            heading_deg: 270.0,
            ai_detections: 3,
        };
        overlay_telemetry(&mut frame, w, h, &telemetry);

        let nonzero_count = frame.iter().filter(|&&v| v != 0).count();
        assert!(
            nonzero_count > 100,
            "telemetry overlay should produce many visible pixels"
        );
    }

    #[test]
    fn font_table_dimensions() {
        assert_eq!(FONT_8X16.len(), 96);
        for glyph in &FONT_8X16 {
            assert_eq!(glyph.len(), 16);
        }
    }

    #[test]
    fn shared_telemetry_default_zero() {
        let st = SharedTelemetry::new();
        let snap = st.load();
        assert_eq!(snap.battery_voltage, 0.0);
        assert_eq!(snap.lat, 0.0);
        assert_eq!(snap.ai_detections, 0);
    }

    #[test]
    fn shared_telemetry_atomic_swap_observed() {
        let st = SharedTelemetry::new();
        st.store(TelemetryData {
            lat: 55.7558,
            lon: 37.6173,
            battery_voltage: 12.4,
            ..TelemetryData::default()
        });
        let snap = st.load();
        assert!((snap.lat - 55.7558).abs() < 1e-9);
        assert!((snap.lon - 37.6173).abs() < 1e-9);
        assert!((snap.battery_voltage - 12.4).abs() < 1e-3);
    }

    /// Multi-thread torn-read safety check: writer thread spams updates
    /// with sentinel-paired f64 fields (lat == lon mathematically), reader
    /// thread verifies invariant. Without atomic snapshot, on 32-bit ARM
    /// this would observe lat from one update and lon from another.
    #[test]
    fn shared_telemetry_no_torn_read_under_contention() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicBool, Ordering};
        use std::thread;

        let st = Arc::new(SharedTelemetry::new());
        let stop = Arc::new(AtomicBool::new(false));
        let observed_torn = Arc::new(AtomicBool::new(false));

        // Writer: alternates lat/lon between (1.0,1.0) and (-1.0,-1.0)
        let st_w = Arc::clone(&st);
        let stop_w = Arc::clone(&stop);
        let writer = thread::spawn(move || {
            let mut sign = 1.0f64;
            while !stop_w.load(Ordering::Relaxed) {
                st_w.store(TelemetryData {
                    lat: sign,
                    lon: sign,
                    ..TelemetryData::default()
                });
                sign = -sign;
            }
        });

        // Reader: invariant `lat == lon` must always hold.
        for _ in 0..200_000 {
            let snap = st.load();
            if (snap.lat - snap.lon).abs() > 1e-9 {
                observed_torn.store(true, Ordering::Relaxed);
                break;
            }
        }

        stop.store(true, Ordering::Relaxed);
        writer.join().unwrap();
        assert!(
            !observed_torn.load(Ordering::Relaxed),
            "torn read observed — atomic snapshot broken"
        );
    }

    #[test]
    fn shared_telemetry_modify_rmw() {
        let st = SharedTelemetry::new();
        st.modify(|td| {
            td.battery_voltage = 11.5;
            td.ai_detections = 7;
        });
        let snap = st.load();
        assert!((snap.battery_voltage - 11.5).abs() < 1e-3);
        assert_eq!(snap.ai_detections, 7);
    }
}