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
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* V4L2 Subdevice
*/
#include "libcamera/internal/v4l2_subdevice.h"
#include <fcntl.h>
#include <regex>
#include <sstream>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/media-bus-format.h>
#include <linux/v4l2-subdev.h>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include <libcamera/geometry.h>
#include "libcamera/internal/formats.h"
#include "libcamera/internal/media_device.h"
#include "libcamera/internal/media_object.h"
/**
* \file v4l2_subdevice.h
* \brief V4L2 Subdevice API
*/
namespace libcamera {
LOG_DECLARE_CATEGORY(V4L2)
/**
* \class MediaBusFormatInfo
* \brief Information about media bus formats
*
* The MediaBusFormatInfo class groups together information describing a media
* bus format. It facilitates handling of media bus formats by providing data
* commonly used in pipeline handlers.
*
* \var MediaBusFormatInfo::name
* \brief The format name as a human-readable string, used as the text
* representation of the format
*
* \var MediaBusFormatInfo::code
* \brief The media bus format code described by this instance (MEDIA_BUS_FMT_*)
*
* \var MediaBusFormatInfo::type
* \brief The media bus format type
*
* \var MediaBusFormatInfo::bitsPerPixel
* \brief The average number of bits per pixel
*
* The number of bits per pixel averages the total number of bits for all
* colour components over the whole image, excluding any padding bits or
* padding pixels.
*
* For formats that transmit multiple or fractional pixels per sample, the
* value will differ from the bus width.
*
* Formats that don't have a fixed number of bits per pixel, such as compressed
* formats, or device-specific embedded data formats, report 0 in this field.
*
* \var MediaBusFormatInfo::colourEncoding
* \brief The colour encoding type
*
* This field is valid for Type::Image formats only.
*/
/**
* \enum MediaBusFormatInfo::Type
* \brief The format type
*
* \var MediaBusFormatInfo::Type::Image
* \brief The format describes image data
*
* \var MediaBusFormatInfo::Type::Metadata
* \brief The format describes generic metadata
*
* \var MediaBusFormatInfo::Type::EmbeddedData
* \brief The format describes sensor embedded data
*/
namespace {
const std::map<uint32_t, MediaBusFormatInfo> mediaBusFormatInfo{
/* This table is sorted to match the order in linux/media-bus-format.h */
{ MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE, {
.name = "RGB444_2X8_PADHI_BE",
.code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE, {
.name = "RGB444_2X8_PADHI_LE",
.code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE, {
.name = "RGB555_2X8_PADHI_BE",
.code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE, {
.name = "RGB555_2X8_PADHI_LE",
.code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB565_1X16, {
.name = "RGB565_1X16",
.code = MEDIA_BUS_FMT_RGB565_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_BGR565_2X8_BE, {
.name = "BGR565_2X8_BE",
.code = MEDIA_BUS_FMT_BGR565_2X8_BE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_BGR565_2X8_LE, {
.name = "BGR565_2X8_LE",
.code = MEDIA_BUS_FMT_BGR565_2X8_LE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB565_2X8_BE, {
.name = "RGB565_2X8_BE",
.code = MEDIA_BUS_FMT_RGB565_2X8_BE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB565_2X8_LE, {
.name = "RGB565_2X8_LE",
.code = MEDIA_BUS_FMT_RGB565_2X8_LE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB666_1X18, {
.name = "RGB666_1X18",
.code = MEDIA_BUS_FMT_RGB666_1X18,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 18,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_BGR888_1X24, {
.name = "BGR888_1X24",
.code = MEDIA_BUS_FMT_BGR888_1X24,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB888_1X24, {
.name = "RGB888_1X24",
.code = MEDIA_BUS_FMT_RGB888_1X24,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB888_2X12_BE, {
.name = "RGB888_2X12_BE",
.code = MEDIA_BUS_FMT_RGB888_2X12_BE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_RGB888_2X12_LE, {
.name = "RGB888_2X12_LE",
.code = MEDIA_BUS_FMT_RGB888_2X12_LE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_ARGB8888_1X32, {
.name = "ARGB8888_1X32",
.code = MEDIA_BUS_FMT_ARGB8888_1X32,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 32,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_Y8_1X8, {
.name = "Y8_1X8",
.code = MEDIA_BUS_FMT_Y8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_UV8_1X8, {
.name = "UV8_1X8",
.code = MEDIA_BUS_FMT_UV8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_UYVY8_1_5X8, {
.name = "UYVY8_1_5X8",
.code = MEDIA_BUS_FMT_UYVY8_1_5X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_VYUY8_1_5X8, {
.name = "VYUY8_1_5X8",
.code = MEDIA_BUS_FMT_VYUY8_1_5X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUYV8_1_5X8, {
.name = "YUYV8_1_5X8",
.code = MEDIA_BUS_FMT_YUYV8_1_5X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YVYU8_1_5X8, {
.name = "YVYU8_1_5X8",
.code = MEDIA_BUS_FMT_YVYU8_1_5X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_UYVY8_2X8, {
.name = "UYVY8_2X8",
.code = MEDIA_BUS_FMT_UYVY8_2X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_VYUY8_2X8, {
.name = "VYUY8_2X8",
.code = MEDIA_BUS_FMT_VYUY8_2X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUYV8_2X8, {
.name = "YUYV8_2X8",
.code = MEDIA_BUS_FMT_YUYV8_2X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YVYU8_2X8, {
.name = "YVYU8_2X8",
.code = MEDIA_BUS_FMT_YVYU8_2X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_Y10_1X10, {
.name = "Y10_1X10",
.code = MEDIA_BUS_FMT_Y10_1X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 10,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_UYVY10_2X10, {
.name = "UYVY10_2X10",
.code = MEDIA_BUS_FMT_UYVY10_2X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 20,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_VYUY10_2X10, {
.name = "VYUY10_2X10",
.code = MEDIA_BUS_FMT_VYUY10_2X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 20,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUYV10_2X10, {
.name = "YUYV10_2X10",
.code = MEDIA_BUS_FMT_YUYV10_2X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 20,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YVYU10_2X10, {
.name = "YVYU10_2X10",
.code = MEDIA_BUS_FMT_YVYU10_2X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 20,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_Y12_1X12, {
.name = "Y12_1X12",
.code = MEDIA_BUS_FMT_Y12_1X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_Y16_1X16, {
.name = "Y16_1X16",
.code = MEDIA_BUS_FMT_Y16_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_UYVY8_1X16, {
.name = "UYVY8_1X16",
.code = MEDIA_BUS_FMT_UYVY8_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_VYUY8_1X16, {
.name = "VYUY8_1X16",
.code = MEDIA_BUS_FMT_VYUY8_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUYV8_1X16, {
.name = "YUYV8_1X16",
.code = MEDIA_BUS_FMT_YUYV8_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YVYU8_1X16, {
.name = "YVYU8_1X16",
.code = MEDIA_BUS_FMT_YVYU8_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YDYUYDYV8_1X16, {
.name = "YDYUYDYV8_1X16",
.code = MEDIA_BUS_FMT_YDYUYDYV8_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_UYVY10_1X20, {
.name = "UYVY10_1X20",
.code = MEDIA_BUS_FMT_UYVY10_1X20,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 20,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_VYUY10_1X20, {
.name = "VYUY10_1X20",
.code = MEDIA_BUS_FMT_VYUY10_1X20,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 20,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUYV10_1X20, {
.name = "YUYV10_1X20",
.code = MEDIA_BUS_FMT_YUYV10_1X20,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 20,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YVYU10_1X20, {
.name = "YVYU10_1X20",
.code = MEDIA_BUS_FMT_YVYU10_1X20,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 20,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUV8_1X24, {
.name = "YUV8_1X24",
.code = MEDIA_BUS_FMT_YUV8_1X24,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUV10_1X30, {
.name = "YUV10_1X30",
.code = MEDIA_BUS_FMT_YUV10_1X30,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 30,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_AYUV8_1X32, {
.name = "AYUV8_1X32",
.code = MEDIA_BUS_FMT_AYUV8_1X32,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 32,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_UYVY12_2X12, {
.name = "UYVY12_2X12",
.code = MEDIA_BUS_FMT_UYVY12_2X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_VYUY12_2X12, {
.name = "VYUY12_2X12",
.code = MEDIA_BUS_FMT_VYUY12_2X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUYV12_2X12, {
.name = "YUYV12_2X12",
.code = MEDIA_BUS_FMT_YUYV12_2X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YVYU12_2X12, {
.name = "YVYU12_2X12",
.code = MEDIA_BUS_FMT_YVYU12_2X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_UYVY12_1X24, {
.name = "UYVY12_1X24",
.code = MEDIA_BUS_FMT_UYVY12_1X24,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_VYUY12_1X24, {
.name = "VYUY12_1X24",
.code = MEDIA_BUS_FMT_VYUY12_1X24,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YUYV12_1X24, {
.name = "YUYV12_1X24",
.code = MEDIA_BUS_FMT_YUYV12_1X24,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_YVYU12_1X24, {
.name = "YVYU12_1X24",
.code = MEDIA_BUS_FMT_YVYU12_1X24,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 24,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_SBGGR8_1X8, {
.name = "SBGGR8_1X8",
.code = MEDIA_BUS_FMT_SBGGR8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGBRG8_1X8, {
.name = "SGBRG8_1X8",
.code = MEDIA_BUS_FMT_SGBRG8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGRBG8_1X8, {
.name = "SGRBG8_1X8",
.code = MEDIA_BUS_FMT_SGRBG8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SRGGB8_1X8, {
.name = "SRGGB8_1X8",
.code = MEDIA_BUS_FMT_SRGGB8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8, {
.name = "SBGGR10_ALAW8_1X8",
.code = MEDIA_BUS_FMT_SBGGR10_ALAW8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8, {
.name = "SGBRG10_ALAW8_1X8",
.code = MEDIA_BUS_FMT_SGBRG10_ALAW8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8, {
.name = "SGRBG10_ALAW8_1X8",
.code = MEDIA_BUS_FMT_SGRBG10_ALAW8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8, {
.name = "SRGGB10_ALAW8_1X8",
.code = MEDIA_BUS_FMT_SRGGB10_ALAW8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, {
.name = "SBGGR10_DPCM8_1X8",
.code = MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, {
.name = "SGBRG10_DPCM8_1X8",
.code = MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, {
.name = "SGRBG10_DPCM8_1X8",
.code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, {
.name = "SRGGB10_DPCM8_1X8",
.code = MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE, {
.name = "SBGGR10_2X8_PADHI_BE",
.code = MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, {
.name = "SBGGR10_2X8_PADHI_LE",
.code = MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE, {
.name = "SBGGR10_2X8_PADLO_BE",
.code = MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE, {
.name = "SBGGR10_2X8_PADLO_LE",
.code = MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR10_1X10, {
.name = "SBGGR10_1X10",
.code = MEDIA_BUS_FMT_SBGGR10_1X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 10,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGBRG10_1X10, {
.name = "SGBRG10_1X10",
.code = MEDIA_BUS_FMT_SGBRG10_1X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 10,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGRBG10_1X10, {
.name = "SGRBG10_1X10",
.code = MEDIA_BUS_FMT_SGRBG10_1X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 10,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SRGGB10_1X10, {
.name = "SRGGB10_1X10",
.code = MEDIA_BUS_FMT_SRGGB10_1X10,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 10,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR12_1X12, {
.name = "SBGGR12_1X12",
.code = MEDIA_BUS_FMT_SBGGR12_1X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGBRG12_1X12, {
.name = "SGBRG12_1X12",
.code = MEDIA_BUS_FMT_SGBRG12_1X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGRBG12_1X12, {
.name = "SGRBG12_1X12",
.code = MEDIA_BUS_FMT_SGRBG12_1X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SRGGB12_1X12, {
.name = "SRGGB12_1X12",
.code = MEDIA_BUS_FMT_SRGGB12_1X12,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 12,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR14_1X14, {
.name = "SBGGR14_1X14",
.code = MEDIA_BUS_FMT_SBGGR14_1X14,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 14,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGBRG14_1X14, {
.name = "SGBRG14_1X14",
.code = MEDIA_BUS_FMT_SGBRG14_1X14,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 14,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SGRBG14_1X14, {
.name = "SGRBG14_1X14",
.code = MEDIA_BUS_FMT_SGRBG14_1X14,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 14,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SRGGB14_1X14, {
.name = "SRGGB14_1X14",
.code = MEDIA_BUS_FMT_SRGGB14_1X14,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 14,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
{ MEDIA_BUS_FMT_SBGGR16_1X16, {
.name = "SBGGR16_1X16",
.code = MEDIA_BUS_FMT_SBGGR16_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW
} },
{ MEDIA_BUS_FMT_SGBRG16_1X16, {
.name = "SGBRG16_1X16",
.code = MEDIA_BUS_FMT_SGBRG16_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW
} },
{ MEDIA_BUS_FMT_SGRBG16_1X16, {
.name = "SGRBG16_1X16",
.code = MEDIA_BUS_FMT_SGRBG16_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW
} },
{ MEDIA_BUS_FMT_SRGGB16_1X16, {
.name = "SRGGB16_1X16",
.code = MEDIA_BUS_FMT_SRGGB16_1X16,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 16,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW
} },
/* \todo Clarify colour encoding for HSV formats */
{ MEDIA_BUS_FMT_AHSV8888_1X32, {
.name = "AHSV8888_1X32",
.code = MEDIA_BUS_FMT_AHSV8888_1X32,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 32,
.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
} },
{ MEDIA_BUS_FMT_JPEG_1X8, {
.name = "JPEG_1X8",
.code = MEDIA_BUS_FMT_JPEG_1X8,
.type = MediaBusFormatInfo::Type::Image,
.bitsPerPixel = 8,
.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
} },
{ MEDIA_BUS_FMT_METADATA_FIXED, {
.name = "METADATA_FIXED",
.code = MEDIA_BUS_FMT_METADATA_FIXED,
.type = MediaBusFormatInfo::Type::Metadata,
.bitsPerPixel = 0,
.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
} },
};
} /* namespace */
/**
* \fn bool MediaBusFormatInfo::isValid() const
* \brief Check if the media bus format info is valid
* \return True if the media bus format info is valid, false otherwise
*/
/**
* \brief Retrieve information about a media bus format
* \param[in] code The media bus format code
* \return The MediaBusFormatInfo describing the \a code if known, or an invalid
* MediaBusFormatInfo otherwise
*/
const MediaBusFormatInfo &MediaBusFormatInfo::info(uint32_t code)
{
static const MediaBusFormatInfo invalid{};
const auto it = mediaBusFormatInfo.find(code);
if (it == mediaBusFormatInfo.end()) {
LOG(V4L2, Warning)
<< "Unsupported media bus format "
<< utils::hex(code, 4);
return invalid;
}
return it->second;
}
/**
* \struct V4L2SubdeviceCapability
* \brief struct v4l2_subdev_capability object wrapper and helpers
*
* The V4L2SubdeviceCapability structure manages the information returned by the
* VIDIOC_SUBDEV_QUERYCAP ioctl.
*/
/**
* \fn V4L2SubdeviceCapability::isReadOnly()
* \brief Retrieve if a subdevice is registered as read-only
*
* A V4L2 subdevice is registered as read-only if V4L2_SUBDEV_CAP_RO_SUBDEV
* is listed as part of its capabilities.
*
* \return True if the subdevice is registered as read-only, false otherwise
*/
/**
* \fn V4L2SubdeviceCapability::hasStreams()
* \brief Retrieve if a subdevice supports the V4L2 streams API
* \return True if the subdevice supports the streams API, false otherwise
*/
/**
* \struct V4L2SubdeviceFormat
* \brief The V4L2 sub-device image format and sizes
*
* This structure describes the format of images when transported between
* separate components connected through a physical bus, such as image sensor
* and image receiver or between components part of the same System-on-Chip that
* realize an image transformation pipeline.
*
* The format of images when transported on physical interconnections is known
* as the "media bus format", and it is identified by a resolution and a pixel
* format identification code, known as the "media bus code", not to be confused
* with the fourcc code that identify the format of images when stored in memory
* (see V4L2VideoDevice::V4L2DeviceFormat).
*
* Media Bus formats supported by the V4L2 APIs are described in Section
* 4.15.3.4.1 of the "Part I - Video for Linux API" chapter of the "Linux Media
* Infrastructure userspace API", part of the Linux kernel documentation.
*
* Image media bus formats are properties of the subdev pads. When images are
* transported between two media pads identified by a 0-indexed number, the
* image bus format configured on the two pads should match (according to the
* underlying driver format matching criteria) in order to prepare for a
* successful streaming operation. For a more detailed description of the image
* format negotiation process when performed between V4L2 subdevices, refer to
* Section 4.15.3.1 of the above mentioned Linux kernel documentation section.
*/
/**
* \var V4L2SubdeviceFormat::code
* \brief The image format bus code
*/
/**
* \var V4L2SubdeviceFormat::size
* \brief The image size in pixels
*/
/**
* \var V4L2SubdeviceFormat::colorSpace
* \brief The color space of the pixels
*
* The color space of the image. When setting the format this may be
* unset, in which case the driver gets to use its default color space.
* After being set, this value should contain the color space that
* was actually used. If this value is unset, then the color space chosen
* by the driver could not be represented by the ColorSpace class (and
* should probably be added).
*
* It is up to the pipeline handler or application to check if the
* resulting color space is acceptable.
*/
/**
* \brief Assemble and return a string describing the format
* \return A string describing the V4L2SubdeviceFormat
*/
const std::string V4L2SubdeviceFormat::toString() const
{
std::stringstream ss;
ss << *this;
return ss.str();
}
/**
* \brief Insert a text representation of a V4L2SubdeviceFormat into an output
* stream
* \param[in] out The output stream
* \param[in] f The V4L2SubdeviceFormat
* \return The output stream \a out
*/
std::ostream &operator<<(std::ostream &out, const V4L2SubdeviceFormat &f)
{
out << f.size << "-";
const auto it = mediaBusFormatInfo.find(f.code);
if (it == mediaBusFormatInfo.end())
out << utils::hex(f.code, 4);
else
out << it->second.name;
return out;
}
/**
* \class V4L2Subdevice
* \brief A V4L2 subdevice as exposed by the Linux kernel
*
* The V4L2Subdevice class provides an API to the "Sub-device interface" as
* described in section 4.15 of the "Linux Media Infrastructure userspace API"
* chapter of the Linux Kernel documentation.
*
* A V4L2Subdevice is constructed from a MediaEntity instance, using the system
* path of the entity's device node. No API call other than open(), isOpen()
* and close() shall be called on an unopened device instance. Upon destruction
* any device left open will be closed, and any resources released.
*/
/**
* \typedef V4L2Subdevice::Formats
* \brief A map of supported media bus formats to frame sizes
*/
/**
* \enum V4L2Subdevice::Whence
* \brief Specify the type of format for getFormat() and setFormat() operations
* \var V4L2Subdevice::ActiveFormat
* \brief The format operation applies to ACTIVE formats
* \var V4L2Subdevice::TryFormat
* \brief The format operation applies to TRY formats
*/
/**
* \class V4L2Subdevice::Stream
* \brief V4L2 subdevice stream
*
* This class identifies a subdev stream, by bundling the pad number with the
* stream number. It is used in all stream-aware functions of the V4L2Subdevice
* class to identify the stream the functions operate on.
*
* \var V4L2Subdevice::Stream::pad
* \brief The 0-indexed pad number
*
* \var V4L2Subdevice::Stream::stream
* \brief The stream number
*/
/**
* \fn V4L2Subdevice::Stream::Stream()
* \brief Construct a Stream with pad and stream set to 0
*/
/**
* \fn V4L2Subdevice::Stream::Stream(unsigned int pad, unsigned int stream)
* \brief Construct a Stream with a given \a pad and \a stream number
* \param[in] pad The indexed pad number
* \param[in] stream The stream number
*/
/**
* \brief Compare streams for equality
* \return True if the two streams are equal, false otherwise
*/
bool operator==(const V4L2Subdevice::Stream &lhs, const V4L2Subdevice::Stream &rhs)
{
return lhs.pad == rhs.pad && lhs.stream == rhs.stream;
}
/**
* \fn bool operator!=(const V4L2Subdevice::Stream &lhs, const V4L2Subdevice::Stream &rhs)
* \brief Compare streams for inequality
* \return True if the two streams are not equal, false otherwise
*/
/**
* \brief Insert a text representation of a V4L2Subdevice::Stream into an
* output stream
* \param[in] out The output stream
* \param[in] stream The V4L2Subdevice::Stream
* \return The output stream \a out
*/
std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Stream &stream)
{
out << stream.pad << "/" << stream.stream;
return out;
}
/**
* \class V4L2Subdevice::Route
* \brief V4L2 subdevice routing table entry
*
* This class models a route in the subdevice routing table. It is similar to
* the v4l2_subdev_route structure, but uses the V4L2Subdevice::Stream class
* for easier usage with the V4L2Subdevice stream-aware functions.
*
* \var V4L2Subdevice::Route::sink
* \brief The sink stream of the route
*
* \var V4L2Subdevice::Route::source
* \brief The source stream of the route
*
* \var V4L2Subdevice::Route::flags
* \brief The route flags (V4L2_SUBDEV_ROUTE_FL_*)
*/
/**
* \fn V4L2Subdevice::Route::Route()
* \brief Construct a Route with default streams
*/
/**
* \fn V4L2Subdevice::Route::Route(const Stream &sink, const Stream &source,
* uint32_t flags)
* \brief Construct a Route from \a sink to \a source
* \param[in] sink The sink stream
* \param[in] source The source stream
* \param[in] flags The route flags
*/
/**
* \brief Insert a text representation of a V4L2Subdevice::Route into an
* output stream
* \param[in] out The output stream
* \param[in] route The V4L2Subdevice::Route
* \return The output stream \a out
*/
std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Route &route)
{
out << route.sink << " -> " << route.source
<< " (" << utils::hex(route.flags) << ")";
return out;
}
/**
* \typedef V4L2Subdevice::Routing
* \brief V4L2 subdevice routing table
*
* This class stores a subdevice routing table as a vector of routes.
*/
/**
* \brief Insert a text representation of a V4L2Subdevice::Routing into an
* output stream
* \param[in] out The output stream
* \param[in] routing The V4L2Subdevice::Routing
* \return The output stream \a out
*/
std::ostream &operator<<(std::ostream &out, const V4L2Subdevice::Routing &routing)
{
for (const auto &[i, route] : utils::enumerate(routing)) {
out << "[" << i << "] " << route;
if (i != routing.size() - 1)
out << ", ";
}
return out;
}
/**
* \brief Create a V4L2 subdevice from a MediaEntity using its device node
* path
*/
V4L2Subdevice::V4L2Subdevice(const MediaEntity *entity)
: V4L2Device(entity->deviceNode()), entity_(entity)
{
}
V4L2Subdevice::~V4L2Subdevice()
{
close();
}
/**
* \brief Open a V4L2 subdevice
* \return 0 on success or a negative error code otherwise
*/
int V4L2Subdevice::open()
{
int ret = V4L2Device::open(O_RDWR);
if (ret)
return ret;
/*
* Try to query the subdev capabilities. The VIDIOC_SUBDEV_QUERYCAP API
* was introduced in kernel v5.8, ENOTTY errors must be ignored to
* support older kernels.
*/
caps_ = {};
ret = ioctl(VIDIOC_SUBDEV_QUERYCAP, &caps_);
if (ret < 0 && errno != ENOTTY) {
ret = -errno;
LOG(V4L2, Error)
<< "Unable to query capabilities: " << strerror(-ret);
return ret;
}
/* If the subdev supports streams, enable the streams API. */
if (caps_.hasStreams()) {
struct v4l2_subdev_client_capability clientCaps{};
clientCaps.capabilities = V4L2_SUBDEV_CLIENT_CAP_STREAMS;
ret = ioctl(VIDIOC_SUBDEV_S_CLIENT_CAP, &clientCaps);
if (ret < 0) {
ret = -errno;
LOG(V4L2, Error)
<< "Unable to set client capabilities: "
<< strerror(-ret);
return ret;
}
}
return 0;
}
/**
* \fn V4L2Subdevice::entity()
* \brief Retrieve the media entity associated with the subdevice
* \return The subdevice's associated media entity.
*/
/**
* \brief Get selection rectangle \a rect for \a target
* \param[in] stream The stream the rectangle is retrieved from
* \param[in] target The selection target defined by the V4L2_SEL_TGT_* flags
* \param[out] rect The retrieved selection rectangle
*
* \todo Define a V4L2SelectionTarget enum for the selection target
*
* \return 0 on success or a negative error code otherwise
*/
int V4L2Subdevice::getSelection(const Stream &stream, unsigned int target,
Rectangle *rect)
{
struct v4l2_subdev_selection sel = {};
sel.which = V4L2_SUBDEV_FORMAT_ACTIVE;
sel.pad = stream.pad;
sel.stream = stream.stream;
sel.target = target;
sel.flags = 0;
int ret = ioctl(VIDIOC_SUBDEV_G_SELECTION, &sel);
if (ret < 0) {
LOG(V4L2, Error)
<< "Unable to get rectangle " << target << " on pad "
<< stream << ": " << strerror(-ret);
return ret;
}
rect->x = sel.r.left;
rect->y = sel.r.top;
rect->width = sel.r.width;
rect->height = sel.r.height;
return 0;
}
/**
* \fn V4L2Subdevice::getSelection(unsigned int pad, unsigned int target,
* Rectangle *rect)
* \brief Get selection rectangle \a rect for \a target
* \param[in] pad The 0-indexed pad number the rectangle is retrieved from
* \param[in] target The selection target defined by the V4L2_SEL_TGT_* flags
* \param[out] rect The retrieved selection rectangle
*
* \return 0 on success or a negative error code otherwise
*/
/**
* \brief Set selection rectangle \a rect for \a target
* \param[in] stream The stream the rectangle is to be applied to
* \param[in] target The selection target defined by the V4L2_SEL_TGT_* flags
* \param[inout] rect The selection rectangle to be applied
*
* \todo Define a V4L2SelectionTarget enum for the selection target
*
* \return 0 on success or a negative error code otherwise
*/
int V4L2Subdevice::setSelection(const Stream &stream, unsigned int target,
Rectangle *rect)
{
struct v4l2_subdev_selection sel = {};
sel.which = V4L2_SUBDEV_FORMAT_ACTIVE;
sel.pad = stream.pad;
sel.stream = stream.stream;
sel.target = target;
sel.flags = 0;
sel.r.left = rect->x;
sel.r.top = rect->y;
sel.r.width = rect->width;
sel.r.height = rect->height;
int ret = ioctl(VIDIOC_SUBDEV_S_SELECTION, &sel);
if (ret < 0) {
LOG(V4L2, Error)
<< "Unable to set rectangle " << target << " on pad "
<< stream << ": " << strerror(-ret);
return ret;
}
rect->x = sel.r.left;
rect->y = sel.r.top;
rect->width = sel.r.width;
rect->height = sel.r.height;
return 0;
}
/**
* \fn V4L2Subdevice::setSelection(unsigned int pad, unsigned int target,
* Rectangle *rect)
* \brief Set selection rectangle \a rect for \a target
* \param[in] pad The 0-indexed pad number the rectangle is to be applied to
* \param[in] target The selection target defined by the V4L2_SEL_TGT_* flags
* \param[inout] rect The selection rectangle to be applied
*
* \todo Define a V4L2SelectionTarget enum for the selection target
*
* \return 0 on success or a negative error code otherwise
*/
/**
* \brief Enumerate all media bus codes and frame sizes on a \a stream
* \param[in] stream The stream to enumerate formats for
*
* Enumerate all media bus codes and frame sizes supported by the subdevice on
* a \a stream.
*
* \return A list of the supported device formats
*/
V4L2Subdevice::Formats V4L2Subdevice::formats(const Stream &stream)
{
Formats formats;
if (stream.pad >= entity_->pads().size()) {
LOG(V4L2, Error) << "Invalid pad: " << stream.pad;
return {};
}
for (unsigned int code : enumPadCodes(stream)) {
std::vector<SizeRange> sizes = enumPadSizes(stream, code);
if (sizes.empty())
return {};
const auto inserted = formats.insert({ code, sizes });
if (!inserted.second) {
LOG(V4L2, Error)
<< "Could not add sizes for media bus code "
<< code << " on pad " << stream.pad;
return {};
}
}
return formats;
}
/**
* \fn V4L2Subdevice::formats(unsigned int pad)
* \brief Enumerate all media bus codes and frame sizes on a \a pad
* \param[in] pad The 0-indexed pad number to enumerate formats on
*
* Enumerate all media bus codes and frame sizes supported by the subdevice on
* a \a pad
*
* \return A list of the supported device formats
*/
std::optional<ColorSpace> V4L2Subdevice::toColorSpace(const v4l2_mbus_framefmt &format) const
{
/*
* Only image formats have a color space, for other formats (such as
* metadata formats) the color space concept isn't applicable. V4L2
* subdev drivers return a colorspace set to V4L2_COLORSPACE_DEFAULT in
* that case (as well as for image formats when the driver hasn't
* bothered implementing color space support). Check the colorspace
* field here and return std::nullopt directly to avoid logging a
* warning.
*/
if (format.colorspace == V4L2_COLORSPACE_DEFAULT)
return std::nullopt;
PixelFormatInfo::ColourEncoding colourEncoding;
const MediaBusFormatInfo &info = MediaBusFormatInfo::info(format.code);
if (info.isValid()) {
colourEncoding = info.colourEncoding;
} else {
LOG(V4L2, Warning)
<< "Unknown subdev format "
<< utils::hex(format.code, 4)
<< ", defaulting to RGB encoding";
colourEncoding = PixelFormatInfo::ColourEncodingRGB;
}
return V4L2Device::toColorSpace(format, colourEncoding);
}
/**
* \brief Retrieve the image format set on one of the V4L2 subdevice streams
* \param[in] stream The stream the format is to be retrieved from
* \param[out] format The image bus format
* \param[in] whence The format to get, \ref V4L2Subdevice::ActiveFormat
* "ActiveFormat" or \ref V4L2Subdevice::TryFormat "TryFormat"
* \return 0 on success or a negative error code otherwise
*/
int V4L2Subdevice::getFormat(const Stream &stream, V4L2SubdeviceFormat *format,
Whence whence)
{
struct v4l2_subdev_format subdevFmt = {};
subdevFmt.which = whence;
subdevFmt.pad = stream.pad;
subdevFmt.stream = stream.stream;
int ret = ioctl(VIDIOC_SUBDEV_G_FMT, &subdevFmt);
if (ret) {
LOG(V4L2, Error)
<< "Unable to get format on pad " << stream << ": "
<< strerror(-ret);
return ret;
}
format->size.width = subdevFmt.format.width;
format->size.height = subdevFmt.format.height;
format->code = subdevFmt.format.code;
format->colorSpace = toColorSpace(subdevFmt.format);
return 0;
}
/**
* \fn V4L2Subdevice::getFormat(unsigned int pad, V4L2SubdeviceFormat *format,
* Whence whence)
* \brief Retrieve the image format set on one of the V4L2 subdevice pads
* \param[in] pad The 0-indexed pad number the format is to be retrieved from
* \param[out] format The image bus format
* \param[in] whence The format to get, \ref V4L2Subdevice::ActiveFormat
* "ActiveFormat" or \ref V4L2Subdevice::TryFormat "TryFormat"
* \return 0 on success or a negative error code otherwise
*/
/**
* \brief Set an image format on one of the V4L2 subdevice pads
* \param[in] stream The stream the format is to be applied to
* \param[inout] format The image bus format to apply to the stream
* \param[in] whence The format to set, \ref V4L2Subdevice::ActiveFormat
* "ActiveFormat" or \ref V4L2Subdevice::TryFormat "TryFormat"
*
* Apply the requested image format to the desired stream and return the
* actually applied format parameters, as getFormat() would do.
*
* \return 0 on success or a negative error code otherwise
*/
int V4L2Subdevice::setFormat(const Stream &stream, V4L2SubdeviceFormat *format,
Whence whence)
{
struct v4l2_subdev_format subdevFmt = {};
subdevFmt.which = whence;
subdevFmt.pad = stream.pad;
subdevFmt.stream = stream.stream;
subdevFmt.format.width = format->size.width;
subdevFmt.format.height = format->size.height;
subdevFmt.format.code = format->code;
subdevFmt.format.field = V4L2_FIELD_NONE;
if (format->colorSpace) {
fromColorSpace(format->colorSpace, subdevFmt.format);
/* The CSC flag is only applicable to source pads. */
if (entity_->pads()[stream.pad]->flags() & MEDIA_PAD_FL_SOURCE)
subdevFmt.format.flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
}
int ret = ioctl(VIDIOC_SUBDEV_S_FMT, &subdevFmt);
if (ret) {
LOG(V4L2, Error)
<< "Unable to set format on pad " << stream << ": "
<< strerror(-ret);
return ret;
}
format->size.width = subdevFmt.format.width;
format->size.height = subdevFmt.format.height;
format->code = subdevFmt.format.code;
format->colorSpace = toColorSpace(subdevFmt.format);
return 0;
}
/**
* \fn V4L2Subdevice::setFormat(unsigned int pad, V4L2SubdeviceFormat *format,
* Whence whence)
* \brief Set an image format on one of the V4L2 subdevice pads
* \param[in] pad The 0-indexed pad number the format is to be applied to
* \param[inout] format The image bus format to apply to the subdevice's pad
* \param[in] whence The format to set, \ref V4L2Subdevice::ActiveFormat
* "ActiveFormat" or \ref V4L2Subdevice::TryFormat "TryFormat"
*
* Apply the requested image format to the desired media pad and return the
* actually applied format parameters, as getFormat() would do.
*
* \return 0 on success or a negative error code otherwise
*/
namespace {
void routeFromKernel(V4L2Subdevice::Route &route,
const struct v4l2_subdev_route &kroute)
{
route.sink.pad = kroute.sink_pad;
route.sink.stream = kroute.sink_stream;
route.source.pad = kroute.source_pad;
route.source.stream = kroute.source_stream;
route.flags = kroute.flags;
}
void routeToKernel(const V4L2Subdevice::Route &route,
struct v4l2_subdev_route &kroute)
{
kroute.sink_pad = route.sink.pad;
kroute.sink_stream = route.sink.stream;
kroute.source_pad = route.source.pad;
kroute.source_stream = route.source.stream;
kroute.flags = route.flags;
}
/*
* Legacy routing support for pre-v6.10-rc1 kernels. Drop when v6.12-rc1 gets
* released.
*/
struct v4l2_subdev_routing_legacy {
__u32 which;
__u32 num_routes;
__u64 routes;
__u32 reserved[6];
};
#define VIDIOC_SUBDEV_G_ROUTING_LEGACY _IOWR('V', 38, struct v4l2_subdev_routing_legacy)
#define VIDIOC_SUBDEV_S_ROUTING_LEGACY _IOWR('V', 39, struct v4l2_subdev_routing_legacy)
} /* namespace */
int V4L2Subdevice::getRoutingLegacy(Routing *routing, Whence whence)
{
struct v4l2_subdev_routing_legacy rt = {};
rt.which = whence;
int ret = ioctl(VIDIOC_SUBDEV_G_ROUTING_LEGACY, &rt);
if (ret == 0 || ret == -ENOTTY)
return ret;
if (ret != -ENOSPC) {
LOG(V4L2, Error)
<< "Failed to retrieve number of routes: "
<< strerror(-ret);
return ret;
}
std::vector<struct v4l2_subdev_route> routes{ rt.num_routes };
rt.routes = reinterpret_cast<uintptr_t>(routes.data());
ret = ioctl(VIDIOC_SUBDEV_G_ROUTING_LEGACY, &rt);
if (ret) {
LOG(V4L2, Error)
<< "Failed to retrieve routes: " << strerror(-ret);
return ret;
}
if (rt.num_routes != routes.size()) {
LOG(V4L2, Error) << "Invalid number of routes";
return -EINVAL;
}
routing->resize(rt.num_routes);
for (const auto &[i, route] : utils::enumerate(routes))
routeFromKernel((*routing)[i], route);
return 0;
}
/**
* \brief Retrieve the subdevice's internal routing table
* \param[out] routing The routing table
* \param[in] whence The routing table to get, \ref V4L2Subdevice::ActiveFormat
* "ActiveFormat" or \ref V4L2Subdevice::TryFormat "TryFormat"
*
* \return 0 on success or a negative error code otherwise
*/
int V4L2Subdevice::getRouting(Routing *routing, Whence whence)
{
routing->clear();
if (!caps_.hasStreams())
return 0;
struct v4l2_subdev_routing rt = {};
rt.which = whence;
int ret = ioctl(VIDIOC_SUBDEV_G_ROUTING, &rt);
if (ret == -ENOTTY)
return V4L2Subdevice::getRoutingLegacy(routing, whence);
if (ret) {
LOG(V4L2, Error)
<< "Failed to retrieve number of routes: "
<< strerror(-ret);
return ret;
}
if (!rt.num_routes)
return 0;
std::vector<struct v4l2_subdev_route> routes{ rt.num_routes };
rt.routes = reinterpret_cast<uintptr_t>(routes.data());
rt.len_routes = rt.num_routes;
rt.num_routes = 0;
ret = ioctl(VIDIOC_SUBDEV_G_ROUTING, &rt);
if (ret) {
LOG(V4L2, Error)
<< "Failed to retrieve routes: " << strerror(-ret);
return ret;
}
if (rt.num_routes != routes.size()) {
LOG(V4L2, Error) << "Invalid number of routes";
return -EINVAL;
}
routing->resize(rt.num_routes);
for (const auto &[i, route] : utils::enumerate(routes))
routeFromKernel((*routing)[i], route);
return 0;
}
int V4L2Subdevice::setRoutingLegacy(Routing *routing, Whence whence)
{
std::vector<struct v4l2_subdev_route> routes{ routing->size() };
for (const auto &[i, route] : utils::enumerate(*routing))
routeToKernel(route, routes[i]);
struct v4l2_subdev_routing_legacy rt = {};
rt.which = whence;
rt.num_routes = routes.size();
rt.routes = reinterpret_cast<uintptr_t>(routes.data());
int ret = ioctl(VIDIOC_SUBDEV_S_ROUTING_LEGACY, &rt);
if (ret) {
LOG(V4L2, Error) << "Failed to set routes: " << strerror(-ret);
return ret;
}
routes.resize(rt.num_routes);
routing->resize(rt.num_routes);
for (const auto &[i, route] : utils::enumerate(routes))
routeFromKernel((*routing)[i], route);
return 0;
}
/**
* \brief Set a routing table on the V4L2 subdevice
* \param[inout] routing The routing table
* \param[in] whence The routing table to set, \ref V4L2Subdevice::ActiveFormat
* "ActiveFormat" or \ref V4L2Subdevice::TryFormat "TryFormat"
*
* Apply to the V4L2 subdevice the routing table \a routing and update its
* content to reflect the actually applied routing table as getRouting() would
* do.
*
* \return 0 on success or a negative error code otherwise
*/
int V4L2Subdevice::setRouting(Routing *routing, Whence whence)
{
if (!caps_.hasStreams()) {
routing->clear();
return 0;
}
std::vector<struct v4l2_subdev_route> routes{ routing->size() };
for (const auto &[i, route] : utils::enumerate(*routing))
routeToKernel(route, routes[i]);
struct v4l2_subdev_routing rt = {};
rt.which = whence;
rt.len_routes = routes.size();
rt.num_routes = routes.size();
rt.routes = reinterpret_cast<uintptr_t>(routes.data());
int ret = ioctl(VIDIOC_SUBDEV_S_ROUTING, &rt);
if (ret == -ENOTTY)
return setRoutingLegacy(routing, whence);
if (ret) {
LOG(V4L2, Error) << "Failed to set routes: " << strerror(-ret);
return ret;
}
/*
* The kernel may want to return more routes than we have space for. In
* that event, we must issue a VIDIOC_SUBDEV_G_ROUTING call to retrieve
* the additional routes.
*/
if (rt.num_routes > routes.size()) {
routes.resize(rt.num_routes);
rt.len_routes = rt.num_routes;
rt.num_routes = 0;
ret = ioctl(VIDIOC_SUBDEV_G_ROUTING, &rt);
if (ret) {
LOG(V4L2, Error)
<< "Failed to retrieve routes: " << strerror(-ret);
return ret;
}
}
if (rt.num_routes != routes.size()) {
LOG(V4L2, Error) << "Invalid number of routes";
return -EINVAL;
}
routing->resize(rt.num_routes);
for (const auto &[i, route] : utils::enumerate(routes))
routeFromKernel((*routing)[i], route);
return 0;
}
/**
* \brief Retrieve the model name of the device
*
* The model name allows identification of the specific device model. This can
* be used to infer device characteristics, for instance to determine the
* analogue gain model of a camera sensor based on the sensor model name.
*
* Neither the V4L2 API nor the Media Controller API expose an explicit model
* name. This function implements a heuristics to extract the model name from
* the subdevice's entity name. This should produce accurate results for
* I2C-based devices. If the heuristics can't match a known naming pattern,
* the function returns the full entity name.
*
* \return The model name of the device
*/
const std::string &V4L2Subdevice::model()
{
if (!model_.empty())
return model_;
/*
* Extract model name from the media entity name.
*
* There is no standardized naming scheme for sensor or other entities
* in the Linux kernel at the moment.
*
* - The most common rule, used by I2C sensors, associates the model
* name with the I2C bus number and address (e.g. 'imx219 0-0010').
*
* - When the sensor exposes multiple subdevs, the model name is
* usually followed by a function name, as in the smiapp driver (e.g.
* 'jt8ew9 pixel_array 0-0010').
*
* - The vimc driver names its sensors 'Sensor A' and 'Sensor B'.
*
* Other schemes probably exist. As a best effort heuristic, use the
* part of the entity name before the first space if the name contains
* an I2C address, and use the full entity name otherwise.
*/
std::string entityName = entity_->name();
std::regex i2cRegex{ " [0-9]+-[0-9a-f]{4}" };
std::smatch match;
std::string model;
if (std::regex_search(entityName, match, i2cRegex))
model_ = entityName.substr(0, entityName.find(' '));
else
model_ = entityName;
return model_;
}
/**
* \fn V4L2Subdevice::caps()
* \brief Retrieve the subdevice V4L2 capabilities
* \return The subdevice V4L2 capabilities
*/
/**
* \brief Create a new video subdevice instance from \a entity in media device
* \a media
* \param[in] media The media device where the entity is registered
* \param[in] entity The media entity name
*
* \return A newly created V4L2Subdevice on success, nullptr otherwise
*/
std::unique_ptr<V4L2Subdevice>
V4L2Subdevice::fromEntityName(const MediaDevice *media,
const std::string &entity)
{
MediaEntity *mediaEntity = media->getEntityByName(entity);
if (!mediaEntity)
return nullptr;
return std::make_unique<V4L2Subdevice>(mediaEntity);
}
std::string V4L2Subdevice::logPrefix() const
{
return "'" + entity_->name() + "'";
}
std::vector<unsigned int> V4L2Subdevice::enumPadCodes(const Stream &stream)
{
std::vector<unsigned int> codes;
int ret;
for (unsigned int index = 0; ; index++) {
struct v4l2_subdev_mbus_code_enum mbusEnum = {};
mbusEnum.pad = stream.pad;
mbusEnum.stream = stream.stream;
mbusEnum.index = index;
mbusEnum.which = V4L2_SUBDEV_FORMAT_ACTIVE;
ret = ioctl(VIDIOC_SUBDEV_ENUM_MBUS_CODE, &mbusEnum);
if (ret)
break;
codes.push_back(mbusEnum.code);
}
if (ret < 0 && ret != -EINVAL) {
LOG(V4L2, Error)
|