~martijnbraam/pyatem

ref: 7eea51355e865463c9de77e8f9a79064bfed3f23 pyatem/pyatem/field.py -rw-r--r-- 71.0 KiB
7eea5135Martijn Braam Add UI for the macro editor 3 months ago
                                                                                
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
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
import colorsys
import struct

from hexdump import hexdump


class FieldBase:
    def _get_string(self, raw):
        return raw.split(b'\x00')[0].decode()

    def make_packet(self):
        header = struct.pack('!H2x 4s', len(self.raw) + 8, self.__class__.CODE.encode())
        return header + self.raw


class FirmwareVersionField(FieldBase):
    """
    Data from the `_ver` field. This stores the major/minor firmware version numbers

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      2    u16    Major version
    2      2    u16    Minor version
    ====== ==== ====== ===========

    After parsing:

    :ivar major: Major firmware version
    :ivar minor: Minor firmware version
    """

    CODE = "_ver"

    def __init__(self, raw):
        """
        :param raw:
        """
        self.raw = raw
        self.major, self.minor = struct.unpack('>HH', raw)
        self.version = "{}.{}".format(self.major, self.minor)

    def __repr__(self):
        return '<firmware-version {}>'.format(self.version)


class ProductNameField(FieldBase):
    """
    Data from the `_pin` field. This stores the product name of the mixer

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      44   char[] Product name
    ====== ==== ====== ===========

    After parsing:

    :ivar name: User friendly product name
    """

    CODE = "_pin"

    def __init__(self, raw):
        self.raw = raw
        self.name = self._get_string(raw)

    def __repr__(self):
        return '<product-name {}>'.format(self.name)


class MixerEffectConfigField(FieldBase):
    """
    Data from the `_MeC` field. This stores basic info about the M/E units.

    The mixer will send multiple fields, one for each M/E unit.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    u8     Number of keyers on this M/E
    2      2    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: 0-based M/E index
    :ivar keyers: Number of upstream keyers on this M/E
    """

    CODE = "_MeC"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.keyers = struct.unpack('>2B2x', raw)

    def __repr__(self):
        return '<mixer-effect-config m/e {}: keyers={}>'.format(self.index, self.keyers)


class MediaplayerSlotsField(FieldBase):
    """
    Data from the `_mpl` field. This stores basic info about the mediaplayer slots.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     Number of still slots
    1      1    u8     Number of clip slots
    2      2    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar name: User friendly product name
    """

    CODE = "_mpl"

    def __init__(self, raw):
        self.raw = raw
        self.stills, self.clips = struct.unpack('>2B2x', raw)

    def __repr__(self):
        return '<mediaplayer-slots: stills={} clips={}>'.format(self.stills, self.clips)


class VideoModeField(FieldBase):
    """
    Data from the `VidM` field. This sets the video standard the mixer operates on internally.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     Video mode
    1      3    ?      unknown
    ====== ==== ====== ===========

    The `Video mode` is an enum of these values:

    === ==========
    Key Video mode
    === ==========
    0   NTSC (525i59.94)
    1   PAL (625i50)
    2   NTSC widescreen (525i59.94)
    3   PAL widescreen (625i50)
    4   720p50
    5   720p59.94
    6   1080i50
    7   1080i59.94
    8   1080p23.98
    9   1080p24
    10  1080p25
    11  1080p29.97
    12  1080p50
    13  1080p59.94
    14  4k23.98
    15  4k24
    16  4k25
    17  4k29.97
    26  1080p30
    27  1080p60
    === ==========

    After parsing:

    :ivar mode: mode number
    :ivar resolution: vertical resolution of the mode
    :ivar interlaced: the current mode is interlaced
    :ivar rate: refresh rate of the mode
    """

    CODE = "VidM"

    def __init__(self, raw):
        self.raw = raw
        self.mode, = struct.unpack('>1B3x', raw)

        modes = {
            0: (525, True, 59.94),
            1: (625, True, 50),
            2: (525, True, 59.94),
            3: (625, True, 50),
            4: (720, False, 50),
            5: (720, False, 59.94),
            6: (1080, True, 50),
            7: (1080, True, 59.94),
            8: (1080, False, 23.98),
            9: (1080, False, 24),
            10: (1080, False, 25),
            11: (1080, False, 29.97),
            12: (1080, False, 50),
            13: (1080, False, 59.94),
            14: (2160, False, 23.98),
            15: (2160, False, 24),
            16: (2160, False, 25),
            17: (2160, False, 29.97),
            26: (1080, False, 30),
            27: (1080, False, 60),
        }

        if self.mode in modes:
            self.resolution = modes[self.mode][0]
            self.interlaced = modes[self.mode][1]
            self.rate = modes[self.mode][2]

    def get_label(self):
        if self.resolution is None:
            return 'unknown [{}]'.format(self.mode)

        pi = 'p'
        if self.interlaced:
            pi = 'i'
        return '{}{}{}'.format(self.resolution, pi, self.rate)

    def get_pixels(self):
        w, h = self.get_resolution()
        return w * h

    def get_resolution(self):
        lut = {
            525: (525, 525),
            720: (1280, 720),
            1080: (1920, 1080),
            2160: (3840, 2160),
        }
        return lut[self.resolution]

    def __repr__(self):
        return '<video-mode: mode={}: {}>'.format(self.mode, self.get_label())


class InputPropertiesField(FieldBase):
    """
    Data from the `InPr` field. This stores information about all the internal and external inputs.

    The mixer will send multiple fields, one for each input

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      2    u16    Source index
    2      20   char[] Long name
    22     4    char[] Short name for button
    26     1    u8     Source category 0=input 1=output
    27     1    u8     ? bitfield
    28     1    u8     same as byte 26
    29     1    u8     port
    30     1    u8     same as byte 26
    31     1    u8     same as byte 29
    32     1    u8     port type
    33     1    u8     bitfield
    34     1    u8     bitfield
    35     1    u8     direction
    ====== ==== ====== ===========

    ===== =========
    value port type
    ===== =========
    0     external
    1     black
    2     color bars
    3     color generator
    4     media player
    5     media player key
    6     supersource
    7     passthrough
    128   M/E output
    129   AUX output
    131   Multiview output
    ===== =========

    ===== ===============
    value available ports
    ===== ===============
    0     SDI
    1     HDMI
    2     Component
    3     Composite
    4     S/Video
    ===== ===============

    ===== =============
    value selected port
    ===== =============
    0     internal
    1     SDI
    2     HDMI
    3     Composite
    4     Component
    5     S/Video
    ===== =============

    After parsing:

    :ivar index: Source index
    :ivar name: Long name
    :ivar short_name: Short name for button
    :ivar port_type: Integer describing the port type
    :ivar available_aux: Source can be routed to AUX
    :ivar available_multiview: Source can be routed to multiview
    :ivar available_supersource_art: Source can be routed to supersource
    :ivar available_supersource_box: Source can be routed to supersource
    :ivar available_key_source: Source can be used as keyer key source
    :ivar available_me1: Source can be routed to M/E 1
    :ivar available_me2: Source can be routed to M/E 2
    """

    PORT_EXTERNAL = 0
    PORT_BLACK = 1
    PORT_BARS = 2
    PORT_COLOR = 3
    PORT_MEDIAPLAYER = 4
    PORT_MEDIAPLAYER_KEY = 5
    PORT_SUPERSOURCE = 6
    PORT_PASSTHROUGH = 7
    PORT_ME_OUTPUT = 128
    PORT_AUX_OUTPUT = 129
    PORT_KEY_MASK = 130
    PORT_MULTIVIEW_OUTPUT = 131
    CODE = "InPr"

    def __init__(self, raw):
        self.raw = raw
        fields = struct.unpack('>H 20s 4s 10B', raw)
        self.index = fields[0]
        self.name = self._get_string(fields[1])
        self.short_name = self._get_string(fields[2])
        self.source_category = fields[3]
        self.port_type = fields[9]
        self.source_ports = fields[6]

        self.available_aux = fields[11] & (1 << 0) != 0
        self.available_multiview = fields[11] & (1 << 1) != 0
        self.available_supersource_art = fields[11] & (1 << 2) != 0
        self.available_supersource_box = fields[11] & (1 << 3) != 0
        self.available_key_source = fields[11] & (1 << 4) != 0

        self.available_me1 = fields[12] & (1 << 0) != 0
        self.available_me2 = fields[12] & (1 << 1) != 0

    def __repr__(self):
        return '<input-properties: index={} name={} button={}>'.format(self.index, self.name, self.short_name)


class ProgramBusInputField(FieldBase):
    """
    Data from the `PrgI` field. This represents the active channel on the program bus of the specific M/E unit.

    The mixer will send a field for every M/E unit in the mixer.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    ?      unknown
    2      2    u16    Source index
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar source: Input source index, refers to an InputPropertiesField index
    """

    CODE = "PrgI"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.source = struct.unpack('>BxH', raw)

    def __repr__(self):
        return '<program-bus-input: me={} source={}>'.format(self.index, self.source)


class PreviewBusInputField(FieldBase):
    """
    Data from the `PrvI` field. This represents the active channel on the preview bus of the specific M/E unit.

    The mixer will send a field for every M/E unit in the mixer.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    ?      unknown
    2      2    u16    Source index
    4      1    u8     1 if preview is mixed in program during a transition
    5      3    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar source: Input source index, refers to an InputPropertiesField index
    :ivar in_program: Preview source is mixed into progam
    """

    CODE = "PrvI"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.source, in_program = struct.unpack('>B x H B 3x', raw)
        self.in_program = in_program == 1

    def __repr__(self):
        in_program = ''
        if self.in_program:
            in_program = ' in-program'
        return '<preview-bus-input: me={} source={}{}>'.format(self.index, self.source, in_program)


class TransitionSettingsField(FieldBase):
    """
    Data from the `TrSS` field. This stores the config of the "Next transition" and "Transition style" blocks on the
    control panels.

    The mixer will send a field for every M/E unit in the mixer.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    u8     Transition style
    2      1    u8     Next transition layers
    3      1    u8     Next transition style
    4      1    u8     Next transition next transition layers
    ====== ==== ====== ===========

    There are two sets of style/layer settings. The first set is the active transition settings. The second one
    will store the transitions settings if you change any of them while a transition is active. These settings will be
    applied as soon as the transition ends. This is signified by blinking transition settings buttons in the official
    control panels.

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar style: Active transition style
    :ivar style_next: Transition style for next transition
    :ivar next_transition_bkgd: Next transition will affect the background layer
    :ivar next_transition_key1: Next transition will affect the upstream key 1 layer
    :ivar next_transition_key2: Next transition will affect the upstream key 2 layer
    :ivar next_transition_key3: Next transition will affect the upstream key 3 layer
    :ivar next_transition_key4: Next transition will affect the upstream key 4 layer
    :ivar next_transition_bkgd_next: Next transition (after current) will affect the background layer
    :ivar next_transition_key1_next: Next transition (after current) will affect the upstream key 1 layer
    :ivar next_transition_key2_next: Next transition (after current) will affect the upstream key 2 layer
    :ivar next_transition_key3_next: Next transition (after current) will affect the upstream key 3 layer
    :ivar next_transition_key4_next: Next transition (after current) will affect the upstream key 4 layer

    """

    STYLE_MIX = 0
    STYLE_DIP = 1
    STYLE_WIPE = 2
    STYLE_DVE = 3
    STYLE_STING = 4
    CODE = "TrSS"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.style, nt, self.style_next, ntn = struct.unpack('>B 2B 2B 3x', raw)

        self.next_transition_bkgd = nt & (1 << 0) != 0
        self.next_transition_key1 = nt & (1 << 1) != 0
        self.next_transition_key2 = nt & (1 << 2) != 0
        self.next_transition_key3 = nt & (1 << 3) != 0
        self.next_transition_key4 = nt & (1 << 4) != 0

        self.next_transition_bkgd_next = ntn & (1 << 0) != 0
        self.next_transition_key1_next = ntn & (1 << 1) != 0
        self.next_transition_key2_next = ntn & (1 << 2) != 0
        self.next_transition_key3_next = ntn & (1 << 3) != 0
        self.next_transition_key4_next = ntn & (1 << 4) != 0

    def __repr__(self):
        return '<transition-settings: me={} style={}>'.format(self.index, self.style)


class TransitionPreviewField(FieldBase):
    """
    Data from the `TsPr` field. This represents the state of the "PREV TRANS" button on the mixer.

    The mixer will send a field for every M/E unit in the mixer.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    bool   Enabled
    2      2    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar enabled: True if the transition preview is enabled
    """

    CODE = "TsPr"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.enabled = struct.unpack('>B ? 2x', raw)

    def __repr__(self):
        return '<transition-preview: me={} enabled={}>'.format(self.index, self.enabled)


class TransitionPositionField(FieldBase):
    """
    Data from the `TrPs` field. This represents the state of the transition T-handle position

    The mixer will send a field for every M/E unit in the mixer.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    bool   In transition
    2      1    u8     Frames remaining
    3      1    ?      unknown
    4      2    u16    Position
    6      1    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar in_transition: True if the transition is active
    :ivar frames_remaining: Number of frames left to complete the transition on auto
    :ivar position: Position of the transition, 0-9999
    """

    CODE = "TrPs"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.in_transition, self.frames_remaining, position = struct.unpack('>B ? B x H 2x', raw)
        self.position = position

    def __repr__(self):
        return '<transition-position: me={} frames-remaining={} position={:02f}>'.format(self.index,
                                                                                         self.frames_remaining,
                                                                                         self.position)


class TallyIndexField(FieldBase):
    """
    Data from the `TlIn`. This is the status of the tally light for every input in order of index number.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      2    u16    Total of tally lights
    n      1    u8     Bitfield, bit0=PROGRAM, bit1=PREVIEW, repeated for every tally light
    ====== ==== ====== ===========

    After parsing:

    :ivar num: number of tally lights
    :ivar tally: List of tally values, every tally light is represented as a tuple with 2 booleans for PROGRAM and PREVIEW
    """

    CODE = "TlIn"

    def __init__(self, raw):
        self.raw = raw
        offset = 0
        self.num, = struct.unpack_from('>H', raw, offset)
        self.tally = []
        offset += 2
        for i in range(0, self.num):
            tally, = struct.unpack_from('>B', raw, offset)
            self.tally.append((tally & 1 != 0, tally & 2 != 0))
            offset += 1

    def __repr__(self):
        return '<tally-index: num={}, val={}>'.format(self.num, self.tally)


class TallySourceField(FieldBase):
    """
    Data from the `TlSr`. This is the status of the tally light for every input, but indexed on source index

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      2    u16    Total of tally lights
    n      2    u16    Source index for this tally light
    n+2    1    u8     Bitfield, bit0=PROGRAM, bit1=PREVIEW
    ====== ==== ====== ===========

    After parsing:

    :ivar num: number of tally lights
    :ivar tally: Dict of tally lights, every tally light is represented as a tuple with 2 booleans for PROGRAM and PREVIEW
    """

    CODE = "TlSr"

    def __init__(self, raw):
        self.raw = raw
        offset = 0
        self.num, = struct.unpack_from('>H', raw, offset)
        self.tally = {}
        offset += 2
        for i in range(0, self.num):
            source, tally, = struct.unpack_from('>HB', raw, offset)
            self.tally[source] = (tally & 1 != 0, tally & 2 != 0)
            offset += 3

    def __repr__(self):
        return '<tally-index: num={}, val={}>'.format(self.num, self.tally)


class KeyOnAirField(FieldBase):
    """
    Data from the `KeOn`. This is the on-air state of the upstream keyers

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    u8     Keyer index
    2      1    bool   On-air
    3      1    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar keyer: Upstream keyer number
    :ivar enabled: Wether the keyer is on-air
    """

    CODE = "KeOn"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.keyer, self.enabled = struct.unpack('>BB?x', raw)

    def __repr__(self):
        return '<key-on-air: me={}, keyer={}, enabled={}>'.format(self.index, self.keyer, self.enabled)


class ColorGeneratorField(FieldBase):
    """
    Data from the `ColV`. This is color set in the color generators of the mixer

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     Color generator index
    1      1    ?      unknown
    2      2    u16    Hue [0-3599]
    4      2    u16    Saturation [0-1000]
    6      2    u16    Luma [0-1000]
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar keyer: Upstream keyer number
    :ivar enabled: Wether the keyer is on-air
    """

    CODE = "ColV"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.hue, self.saturation, self.luma = struct.unpack('>Bx 3H', raw)
        self.hue = self.hue / 10.0
        self.saturation = self.saturation / 1000.0
        self.luma = self.luma / 1000.0

    def get_rgb(self):
        return colorsys.hls_to_rgb(self.hue / 360.0, self.luma, self.saturation)

    def __repr__(self):
        return '<color-generator: index={}, hue={} saturation={} luma={}>'.format(self.index, self.hue, self.saturation,
                                                                                  self.luma)


class AuxOutputSourceField(FieldBase):
    """
    Data from the `AuxS`. The routing for the AUX outputs of the hardware.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     AUX output index
    1      1    ?      unknown
    2      2    u16    Source index
    ====== ==== ====== ===========

    After parsing:

    :ivar index: AUX index
    :ivar rate: Source index
    """

    CODE = "AuxS"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.source = struct.unpack('>BxH', raw)

    def __repr__(self):
        return '<aux-output-source: aux={}, source={}>'.format(self.index, self.source)


class FadeToBlackStateField(FieldBase):
    """
    Data from the `FtbS`. This contains the information about the fade-to-black transition.

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    bool   Fade to black done
    2      1    bool   Fade to black is in transition
    3      1    u8     Frames remaining in transition
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar done: Fade to black is completely done (blinking button state in the control panel)
    :ivar transitioning: Fade to black is fading, (Solid red in control panel)
    :ivar frames_remaining: Frames remaining in the transition
    """

    CODE = "FtbS"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.done, self.transitioning, self.frames_remaining = struct.unpack('>B??B', raw)

    def __repr__(self):
        return '<fade-to-black-state: me={}, done={}, transitioning={}, frames-remaining={}>'.format(self.index,
                                                                                                     self.done,
                                                                                                     self.transitioning,
                                                                                                     self.frames_remaining)


class MediaplayerFileInfoField(FieldBase):
    """
    Data from the `MPfe`. This is the metadata about a single frame slot in the mediaplayer

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      1    u8     type
    1      1    ?      unknown
    2      2    u16    index
    4      1    bool   is used
    5      16   char[] hash
    21     2    ?      unknown
    23     ?    string name of the slot, first byte is number of characters
    ====== ==== ====== ===========

    After parsing:

    :ivar index: Slot index
    :ivar type: Slot type, 0=still
    :ivar is_used: Slot contains data
    :ivar hash: 16-byte md5 hash of the slot data
    :ivar name: Name of the content in the slot
    """

    CODE = "MPfe"

    def __init__(self, raw):
        self.raw = raw
        namelen = max(0, len(raw) - 23)
        self.type, self.index, self.is_used, self.hash, self.name = struct.unpack('>Bx H ? 16s 2x {}p'.format(namelen),
                                                                                  raw)

    def __repr__(self):
        return '<mediaplayer-file-info: type={} index={} used={} name={}>'.format(self.type, self.index, self.is_used,
                                                                                  self.name)


class TopologyField(FieldBase):
    """
    Data from the `_top` field. This describes the internal video routing topology.

    =================== ========= ======= ======
    spec                Atem Mini 1M/E 4k TVS HD
    =================== ========= ======= ======
    M/E units           1         1       1
    upstream keyers     1         1       1
    downstream keyers   1         2       2
    dve                 1         1       1
    stinger             0         1       0
    supersources        0         0       0
    multiview           0         1       1
    rs485               0         1       1
    =================== ========= ======= ======

    ====== ==== ====== ========= ========= ======= ======= ====== ===========
    Offset Size Type   Atem Mini Mini Pro  1M/E 4k Prod 4k TVS HD Description
    ====== ==== ====== ========= ========= ======= ======= ====== ===========
    0      1    u8     1         1         1       1       1      Number of M/E units
    1      1    u8     14        15        31      24      24     Sources
    2      1    u8     1         1         2       2       2      Downstream keyers
    3      1    u8     1         1         3       1       1      AUX busses
    4      1    u8     0         0         0       0       4      MixMinus Outputs
    5      1    u8     1         1         2       2       2      Media players
    6      1    u8     0         1         1       1       1      Multiviewers
    7      1    u8     0         0         1       0       1      rs485
    8      1    u8     4         4         4       4       4      Hyperdecks
    9      1    u8     1         1         1       0       1      DVE
    10     1    u8     0         0         1       0       0      Stingers
    11     1    u8     0         0         0       0       0      supersources
    12     1    u8     0         0         1       1       1      ? Multiview routable?
    13     1    u8     0         0         0       0       1      Talkback channels
    14     1    u8     0         0         0       0       4      ?
    15     1    u8     1         1         0       0       0      ?
    16     1    u8     0         0         0       0       0      ?
    17     1    u8     0         0         1       1       0      ?
    18     1    u8     1         1         1       1       1      Camera Control
    19     1    u8     0         0         1       0       1      ?
    20     1    u8     0         0         1       0       1      ?
    21     1    u8     0         0         1       1       1      ? Multiview routable?
    22     1    u8     1         1         0       0       0      Advanced chroma keyers
    23     1    u8     1         1         0       0       0      Only configurable outputs
    24     1    u8     1         1         0       0       0      ?
    25     1    u8     0x20      0x2f      0x20    0       0x10   ?
    26     1    u8     3         108       0       0       0      ?
    27     1    u8     0xe8      0x69      0x00    0       0x0    ?
    ====== ==== ====== ========= ========= ======= ======= ====== ===========


    After parsing:

    :ivar me_units: Number of M/E units in the mixer
    :ivar sources: Number of internal and external sources
    :ivar downstream_keyers: Number of downstream keyers
    :ivar aux_outputs: Number of routable AUX outputs
    :ivar mixminus_outputs: Number of ouputs with MixMinus
    :ivar mediaplayers: Number of mediaplayers
    :ivar multiviewers: Number of multiview ouputs
    :ivar rs485: Number of RS-485 outputs
    :ivar hyperdecks: Number of hyperdeck slots
    :ivar dve: Number of DVE blocks
    :ivar stingers: Number of stinger blocks
    :ivar supersources: Number of supersources
    """

    CODE = "_top"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>28B', raw)

        self.me_units = field[0]
        self.sources = field[1]
        self.downstream_keyers = field[2]
        self.aux_outputs = field[3]
        self.mixminus_outputs = field[4]
        self.mediaplayers = field[5]
        self.multiviewers = field[6]
        self.rs485 = field[7]
        self.hyperdecks = field[8]
        self.dve = field[9]
        self.stingers = field[10]
        self.supersources = field[11]
        self.multiviewer_routable = field[12] == 1

    def __repr__(self):
        return '<topology, me={} sources={} aux={}>'.format(self.me_units, self.sources, self.aux_outputs)


class DkeyPropertiesBaseField(FieldBase):
    """
    Data from the `DskB`. Downstream keyer base info.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     Downstream keyer index, 0-indexed
    1      1    ?      unknown
    2      2    u16    Fill source index
    4      2    u16    Key source index
    6      2    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: DSK index
    :ivar fill_source: Source index for the fill input
    :ivar key_source: Source index for the key input
    """

    CODE = "DskB"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.fill_source, self.key_source = struct.unpack('>BxHH2x', raw)

    def __repr__(self):
        return '<downstream-keyer-base: dsk={}, fill={}, key={}>'.format(self.index, self.fill_source, self.key_source)


class DkeyPropertiesField(FieldBase):
    """
    Data from the `DskP`. Downstream keyer info.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     Downstream keyer index, 0-indexed
    1      1    bool   Tie enabled
    2      1    u8     Transition rate in frames
    3      1    bool   Mask is pre-multiplied alpha
    4      2    u16    Clip [0-1000]
    6      2    u16    Gain [0-1000]
    8      1    bool   Invert key
    9      1    bool   Enable mask
    10     2    i16    Top [-9000 - 9000]
    12     2    i16    Bottom [-9000 - 9000]
    14     2    i16    Left [-9000 - 9000]
    16     2    i16    Right [-9000 - 9000]
    18     2    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar done: Fade to black is completely done (blinking button state in the control panel)
    :ivar transitioning: Fade to black is fading, (Solid red in control panel)
    :ivar frames_remaining: Frames remaining in the transition
    """

    CODE = "DskP"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>B?B ?HH? ?4h 2B', raw)
        self.index = field[0]
        self.tie = field[1]
        self.rate = field[2]
        self.premultiplied = field[3]
        self.clip = field[4]
        self.gain = field[5]
        self.invert_key = field[6]
        self.masked = field[7]
        self.top = field[8]
        self.bottom = field[9]
        self.left = field[10]
        self.right = field[11]

    def __repr__(self):
        return '<downstream-keyer-mask: dsk={}, tie={}, rate={}, masked={}>'.format(self.index, self.tie, self.rate,
                                                                                    self.masked)


class DkeyStateField(FieldBase):
    """
    Data from the `DskS`. Downstream keyer state.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     Downstream keyer index, 0-indexed
    1      1    bool   On air
    2      1    bool   Is transitioning
    3      1    bool   Is autotransitioning
    4      1    u8     Frames remaining
    5      3    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: Downstream keyer index
    :ivar on_air: Keyer is on air
    :ivar is_transitioning: Is transitioning
    :ivar is_autotransitioning: Is transitioning due to auto button
    :ivar frames_remaining: Frames remaining in transition
    """

    CODE = "DskS"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>B 3? B 3x', raw)
        self.index = field[0]
        self.on_air = field[1]
        self.is_transitioning = field[2]
        self.is_autotransitioning = field[3]
        self.frames_remaining = field[4]

    def __repr__(self):
        return '<downstream-keyer-state: dsk={}, onair={}, transitioning={} autotrans={} frames={}>'.format(self.index,
                                                                                                            self.on_air,
                                                                                                            self.is_transitioning,
                                                                                                            self.is_autotransitioning,
                                                                                                            self.frames_remaining)


class TransitionMixField(FieldBase):
    """
    Data from the `TMxP`. Settings for the mix transition.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    u8     rate in frames
    2      2    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar rate: Number of frames in the transition
    """

    CODE = "TMxP"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.rate = struct.unpack('>BBxx', raw)

    def __repr__(self):
        return '<transition-mix: me={}, rate={}>'.format(self.index, self.rate)


class FadeToBlackField(FieldBase):
    """
    Data from the `FtbP`. Settings for the fade-to-black transition.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    u8     rate in frames
    2      2    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar rate: Number of frames in transition
    """

    CODE = "FtbP"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.rate = struct.unpack('>BBxx', raw)

    def __repr__(self):
        return '<fade-to-black: me={}, rate={}>'.format(self.index, self.rate)


class TransitionDipField(FieldBase):
    """
    Data from the `TDpP`. Settings for the dip transition.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    u8     rate in frames
    2      2    u16    Source index for the DIP source
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar rate: Number of frames in transition
    :ivar source: Source index for the dip
    """

    CODE = "TDpP"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.rate, self.source = struct.unpack('>BBH', raw)

    def __repr__(self):
        return '<transition-dip: me={}, rate={} source={}>'.format(self.index, self.rate, self.source)


class TransitionWipeField(FieldBase):
    """
    Data from the `TWpP`. Settings for the wipe transition.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    u8     Rate in frames
    2      1    u8     Pattern id
    3      1    ?      unknown
    4      2    u16    Border width
    6      2    u16    Border fill source index
    8      2    u16    Symmetry
    10     2    u16    Softness
    12     2    u16    Origin position X
    14     2    u16    Origin position Y
    16     1    bool   Reverse
    16     1    bool   Flip flop
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar rate: Number of frames in transition
    :ivar source: Source index for the dip
    """

    CODE = "TWpP"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>BBBx 6H 2? 2x', raw)
        self.index = field[0]
        self.rate = field[1]
        self.pattern = field[2]
        self.width = field[3]
        self.source = field[4]
        self.symmetry = field[5]
        self.softness = field[6]
        self.positionx = field[7]
        self.positiony = field[8]
        self.reverse = field[9]
        self.flipflop = field[10]

    def __repr__(self):
        return '<transition-wipe: me={}, rate={} pattern={}>'.format(self.index, self.rate, self.pattern)


class TransitionDveField(FieldBase):
    """
    Data from the `TDvP`. Settings for the DVE transition.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     M/E index
    1      1    u8     Rate in frames
    2      1    ?      unknown
    3      1    u8     DVE style
    4      2    u16    Fill source index
    6      2    u16    Key source index
    8      1    bool   Enable key
    9      1    bool   Key is premultiplied
    10     2    u16    Key clip [0-1000]
    12     2    u16    Key gain [0-1000]
    14     1    bool   Key invert
    15     1    bool   Reverse
    16     1    bool   Flip flop
    17     3    ?      unknown
    ====== ==== ====== ===========

    After parsing:

    :ivar index: M/E index in the mixer
    :ivar rate: Number of frames in transition
    :ivar style: Style or effect index for the DVE
    :ivar fill_source: Fill source index
    :ivar key_source: Key source index
    :ivar key_premultiplied: Key is premultiplied alpha
    :ivar key_clip: Key clipping point
    :ivar key_gain: Key Gain
    :ivar key_invert: Invert key source
    :ivar reverse: Reverse transition
    :ivar flipflop: Flip flop transition
    """

    CODE = "TDvP"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>BBx B 2H 2? 2H 3? 3x', raw)
        self.index = field[0]
        self.rate = field[1]
        self.style = field[2]
        self.fill_source = field[3]
        self.key_source = field[4]
        self.key_enable = field[5]
        self.key_premultiplied = field[6]
        self.key_clip = field[7]
        self.key_gain = field[8]
        self.key_invert = field[9]
        self.reverse = field[10]
        self.flipflop = field[11]

    def __repr__(self):
        return '<transition-dve: me={}, rate={} style={}>'.format(self.index, self.rate, self.style)


class AudioMixerMasterPropertiesField(FieldBase):
    """
    Data from the `AMMO`. Settings for the master bus on legacy audio units.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Program Gain
    2      2    ?      unknown
    4      1    bool   Follow fade to black
    5      1    ?      unknown
    ====== ==== ====== ===========

    After parsing:
    :ivar volume: Master volume for the mixer, unsigned int which maps [? - ?] to +10dB - -100dB (inf)
    :ivar afv: Wether the master volume follows the fade-to-bloack
    """

    CODE = "AMMO"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>H 2x ?x 2x', raw)
        self.volume = field[0]
        self.afv = field[1]

    def __repr__(self):
        return '<audio-master-properties: volume={} afv={}>'.format(self.volume, self.avf)


class AudioMixerMonitorPropertiesField(FieldBase):
    """
    Data from the `AMmO`. Settings for the monitor bus on legacy audio units.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    bool   Monitoring enabled
    1      1    ?      unknown
    2      2    u16    Volume
    4      1    bool   Mute
    5      1    bool   Solo
    6      2    u16    Solo source index
    8      1    bool   Dim
    10     2    u16    Dim volume
    ====== ==== ====== ===========

    After parsing:
    :ivar volume: Master volume for the mixer, unsigned int which maps [? - ?] to +10dB - -100dB (inf)
    """

    CODE = "AMmO"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>?xH? ?H ?x H', raw)
        self.enabled = field[0]
        self.volume = field[1]
        self.mute = field[2]
        self.solo = field[3]
        self.solo_source = field[4]
        self.dim = field[5]
        self.dim_volume = field[6]

    def __repr__(self):
        return '<audio-monitor-properties: volume={}>'.format(self.volume)


class AudioMixerInputPropertiesField(FieldBase):
    """
    Data from the `AMIP`. Settings for a channel strip on legacy audio units.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Audio source index
    2      1    u8     Type [0: External Video, 1: Media Player, 2: External Audio]
    3      3    ?      unknown
    6      1    bool   From Media Player
    7      1    u8     Plug Type [0: Internal, 1: SDI, 2: HDMI, 3: Component, 4: Composite, 5: SVideo, 32: XLR, 64, AES/EBU, 128 RCA]
    8      1    u8     Mix Option [0: Off, 1: On, 2: AFV]
    9      1    u8     unknown
    10     2    u16    Volume [0 - 65381]
    12     2    i16    Pan [-10000 - 10000]
    14     1    u8     unknown
    ====== ==== ====== ===========

    After parsing:
    :ivar volume: Master volume for the mixer, signed int which maps [-10000 - 1000] to +10dB - -100dB (inf)
    :ivar afv: Enable/disabled state for master audio-follow-video (for fade-to-black)
    """

    CODE = "AMIP"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>H B 2x ? B B x H h x x x', raw)
        self.index = field[0]
        self.type = field[1]
        self.is_media_player = field[2]
        self.number = field[3]
        self.mix_option = field[4]
        self.volume = field[5]
        self.balance = field[6]

        self.strip_id = str(self.index) + '.0'

    def __repr__(self):
        return '<audio-mixer-input-properties: index={} volume={} balance={} >'.format(self.strip_id, self.volume,
                                                                                       self.balance)


class AudioMixerTallyField(FieldBase):
    """
    Data from the `AMTl`. Encodes the state of tally lights on the audio mixer

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Number of tally lights
    2      2    u16    Audio Source
    4      1    bool   IsMixedIn (On/Off)
    ====== ==== ====== ===========
    """

    CODE = "AMTl"

    def __init__(self, raw):
        self.raw = raw
        offset = 0
        self.num, = struct.unpack_from('>H', raw, offset)
        self.tally = {}
        offset += 2
        for i in range(0, self.num):
            source, tally, = struct.unpack_from('>H?', raw, offset)
            strip_id = '{}.{}'.format(source, 0)
            self.tally[strip_id] = tally
            offset += 3

    def __repr__(self):
        return '<audio-mixer-tally {}>'.format(self.tally)


class FairlightMasterPropertiesField(FieldBase):
    """
    Data from the `FAMP`. Settings for the master bus on fairlight audio units.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    ?      unknown
    1      1    bool   Enable master EQ
    2      4    ?      unknown
    6      2    i16    EQ gain [-2000 - 2000]
    8      2    ?      unknown
    10     2    u16    Dynamics make-up gain [0 - 2000]
    12     4    i32    Master volume [-10000 - 1000]
    16     1    bool   Audio follow video
    17     3    ?      unknown
    ====== ==== ====== ===========

    After parsing:
    :ivar volume: Master volume for the mixer, signed int which maps [-10000 - 1000] to +10dB - -100dB (inf)
    :ivar eq_enable: Enabled/disabled state for the master EQ
    :ivar eq_gain: Gain applied after EQ, [-2000 - 2000] maps to -20dB - +20dB
    :ivar dynamics_gain: Make-up gain for the dynamics section, [0 - 2000] maps to 0dB - +20dB
    :ivar afv: Enable/disabled state for master audio-follow-video (for fade-to-black)
    """

    CODE = "FAMP"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>x ? 4x h 2x H i ? 3x', raw)
        self.eq_enable = field[0]
        self.eq_gain = field[1]
        self.dynamics_gain = field[2]
        self.volume = field[3]
        self.afv = field[4]

    def __repr__(self):
        return '<fairlight-master-properties: volume={} make-up={} eq={}>'.format(self.volume, self.dynamics_gain,
                                                                                  self.eq_gain)


class FairlightStripPropertiesField(FieldBase):
    """
    Data from the `FASP`. Settings for a channel strip on fairlight audio units.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Audio source index
    14     1    u8     Split indicator? [01 for normal, FF for split]
    15     1    u8     Subchannel index
    18     1    u8     Delay in frames
    22     2    i16    Gain [-10000 - 600]
    29     1    bool   Enable EQ
    34     2    i16    EQ Gain
    38     2    u16    Dynamics gain
    40     2    i16    Pan [-10000 - 10000]
    46     2    i16    Volume [-10000 - 1000]
    49     1    u8     AFV bitfield? 1=off 2=on 4=afv
    ====== ==== ====== ===========

    The way byte 14 and 15 work is unclear at the moment, this need verification on a mixer with an video input that has
    more than 2 embedded channels, of of these bytes might be a channel count.

    After parsing:
    :ivar volume: Master volume for the mixer, signed int which maps [-10000 - 1000] to +10dB - -100dB (inf)
    :ivar eq_enable: Enabled/disabled state for the master EQ
    :ivar eq_gain: Gain applied after EQ, [-2000 - 2000] maps to -20dB - +20dB
    :ivar dynamics_gain: Make-up gain for the dynamics section, [0 - 2000] maps to 0dB - +20dB
    :ivar afv: Enable/disabled state for master audio-follow-video (for fade-to-black)
    """

    CODE = "FASP"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>H 12xBBxB 4x h 5x ? 4x h 2x Hh 4x h x B 2x', raw)
        self.index = field[0]
        self.is_split = field[1]
        self.subchannel = field[2]
        self.delay = field[3]
        self.gain = field[4]
        self.eq_enable = field[5]
        self.eq_gain = field[6]
        self.dynamics_gain = field[7]
        self.pan = field[8]
        self.volume = field[9]
        self.state = field[10]

        self.strip_id = str(self.index)
        if self.is_split == 0xff:
            self.strip_id += '.' + str(self.subchannel)
        else:
            self.strip_id += '.0'

    def __repr__(self):
        extra = ''
        if self.eq_enable:
            extra += ' EQ {}'.format(self.eq_gain)

        return '<fairlight-strip-properties: index={} gain={} volume={} pan={} dgn={} {}>'.format(self.strip_id,
                                                                                                  self.gain,
                                                                                                  self.volume,
                                                                                                  self.pan,
                                                                                                  self.dynamics_gain,
                                                                                                  extra)


class FairlightStripDeleteField(FieldBase):
    """
    Data from the `FASD`. Fairlight strip delete, received only when changing the source routing in fairlight to remove
    channels that have changed.

    """

    CODE = "FASD"

    def __init__(self, raw):
        self.raw = raw

    def __repr__(self):
        return '<fairlight-strip-delete {}>'.format(self.raw)


class FairlightAudioInputField(FieldBase):
    """
    Data from the `FAIP`. Describes the inputs to the fairlight mixer

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Audio source index
    2      1    u8     Input type
    3      2    ?      unknown
    5      1    u8     Index in group
    10     1    u8     Changes when stereo is split into dual mono
    12     1    u8     Analog audio input level [1=mic, 2=line]
    ====== ==== ====== ===========

    === ==========
    Val Input type
    === ==========
    0   External video input
    1   Media player audio
    2   External audio input
    === ==========

    After parsing:
    :ivar volume: Master volume for the mixer, signed int which maps [-10000 - 1000] to +10dB - -100dB (inf)
    """

    CODE = "FAIP"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.type, self.number, self.split, self.level = struct.unpack('>HB 2x B xxxx B x B 3x', raw)

    def __repr__(self):
        return '<fairlight-input index={} type={}>'.format(self.index, self.type)


class FairlightTallyField(FieldBase):
    """
    Data from the `FMTl`. Encodes the state of tally lights on the audio mixer

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Number of tally lights
    2      1    u8     Input type
    3      2    ?      unknown
    5      1    u8     Index in group
    10     1    u8     Changes when stereo is split into dual mono
    12     1    u8     Analog audio input level [1=mic, 2=line]
    ====== ==== ====== ===========

    === ==========
    Val Input type
    === ==========
    0   External video input
    1   Media player audio
    2   External audio input
    === ==========

    After parsing:
    :ivar volume: Master volume for the mixer, signed int which maps [-10000 - 1000] to +10dB - -100dB (inf)
    """

    CODE = "FMTl"

    def __init__(self, raw):
        self.raw = raw
        offset = 0
        self.num, = struct.unpack_from('>H', raw, offset)
        self.tally = {}
        offset += 15
        for i in range(0, self.num):
            subchan, source, tally, = struct.unpack_from('>BH?', raw, offset)
            strip_id = '{}.{}'.format(source, subchan)
            self.tally[strip_id] = tally
            offset += 11

    def __repr__(self):
        return '<fairlight-tally {}>'.format(self.tally)


class AtemEqBandPropertiesField(FieldBase):
    """
    Data from the `AEBP` field. This encodes the EQ settings in the fairlight mixer. For every channel there will
    be 6 off these fields sent, one for every band of the EQ. The "possible band filters" differentiates the first
    and last band which have a different option list for the filter dropdown in the UI.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Audio source index
    2      2    ?      unknown
    4      4    ?      unknown
    14     1    u8     Split indicator? [01 for normal, FF for split]
    15     1    u8     subchannel index
    16     1    u8     bond index
    17     1    bool   band enabled
    18     1    u8     possible band filters
    19     1    u8     band filter
    20     1    ?      ?
    21     1    ?      band frequency range
    26     2    u16    band frequency
    28     4    i32    band gain
    32     2    u16    band Q
    ====== ==== ====== ===========

    === ==========
    Val Band filter
    === ==========
    01  Low shelf
    02  Low pass
    04  Bell
    08  Notch
    10  High pass
    20  High shelf
    === ==========

    After parsing:
    :ivar volume: Master volume for the mixer, signed int which maps [-10000 - 1000] to +10dB - -100dB (inf)
    """

    CODE = "AEBP"

    def __init__(self, raw):
        self.raw = raw
        values = struct.unpack('>H 2x 4x 6x BB B ? B B x B 4x H i H 2x', raw)
        self.index = values[0]
        self.is_split = values[1]
        self.subchannel = values[2]
        self.band_index = values[3]
        self.band_enabled = values[4]
        self.band_possible_filters = values[5]
        self.band_filter = values[6]
        self.band_freq_range = values[7]
        self.band_frequency = values[8]
        self.band_gain = values[9]
        self.band_q = values[10]

        self.strip_id = str(self.index)
        if self.is_split == 0xff:
            self.strip_id += '.' + str(self.subchannel)
        else:
            self.strip_id += '.0'

    def __repr__(self):
        desc = ''
        filters = {
            0x01: 'low-shelf',
            0x02: 'low-pass',
            0x04: 'bell',
            0x08: 'notch',
            0x10: 'high-pass',
            0x20: 'high-shelf'
        }
        if self.band_filter in filters:
            desc += filters[self.band_filter]
        else:
            desc += 'filter ' + str(self.band_filter)

        if self.band_enabled:
            desc += '[on]'
        else:
            desc += '[off]'
        desc += ' freq ' + str(self.band_frequency)
        desc += ' gain ' + str(self.band_gain)
        desc += ' Q ' + str(self.band_q)
        return '<atem-eq-band-properties {} band {} {}>'.format(self.strip_id, self.band_index, desc)


class AudioInputField(FieldBase):
    """
    Data from the `AMIP`. Describes the inputs to the atem audio mixer

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Audio source index
    2      1    u8     Input type
    3      2    ?      unknown
    5      1    u8     Index in group
    6      1    ?      ?
    7      1    u8     Input plug
    8      1    u8     State [0=off, 1=on, 2=afv]
    10     2    u16    Channel volume
    12     2    i16    Channel balance [-10000 - 10000]
    ====== ==== ====== ===========

    === ==========
    Val Input type
    === ==========
    0   External video input
    1   Media player audio
    2   External audio input
    === ==========

    === =========
    Val Plug type
    === =========
    0   Internal
    1   SDI
    2   HDMI
    3   Component
    4   Composite
    5   SVideo
    32  XLR
    64  AES/EBU
    128 RCA
    === =========

    After parsing:
    :ivar volume: Master volume for the mixer, signed int which maps [-10000 - 1000] to +10dB - -100dB (inf)
    """

    CODE = "AMIP"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.type, self.number, self.plug, self.state, self.volume, self.balance = struct.unpack(
            '>HB 2x B x BB x Hh 2x', raw)
        self.strip_id = str(self.index) + '.0'

    def plug_name(self):
        """Return the display name for the connector"""
        lut = {
            0: "Internal",
            1: "SDI",
            2: "HDMI",
            3: "Component",
            4: "Composite",
            5: "SVideo",
            32: "XLR",
            64: "AES",
            128: "RCA",
        }
        if self.plug in lut:
            return lut[self.plug]
        else:
            return 'Analog'

    def __repr__(self):
        return '<audio-input index={} type={} plug={}>'.format(self.index, self.type, self.plug)


class KeyPropertiesBaseField(FieldBase):
    """
    Data from the `KeBP`. The upstream keyer base properties.
    """

    CODE = "KeBP"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>BBB Bx B HH ?x 4h', raw)
        self.index = field[0]
        self.keyer = field[1]
        self.type = field[2]
        self.enabled = field[3]
        self.fly_enabled = field[4]
        self.fill_source = field[5]
        self.key_source = field[6]
        self.mask_enabled = field[7]

        self.mask_top = field[8]
        self.mask_bottom = field[9]
        self.mask_left = field[10]
        self.mask_right = field[11]

    def __repr__(self):
        return '<key-properties-base me={}, key={}, type={}>'.format(self.index, self.keyer, self.type)


class KeyPropertiesDveField(FieldBase):
    """
    Data from the `KeDV`. The upstream keyer DVE-specific properties.
    """

    CODE = "KeDV"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>BBxx 5i ??Bx HH BBBBBx 4HB? 4hB 3x', raw)
        self.index = field[0]
        self.keyer = field[1]

        self.size_x = field[2]
        self.size_y = field[3]
        self.pos_x = field[4]
        self.pos_y = field[5]
        self.rotation = field[6]

        self.border_enabled = field[7]
        self.shadow_enabled = field[8]
        self.border_bevel = field[9]

        self.border_outer_width = field[10]
        self.border_inner_width = field[11]

        self.border_outer_softness = field[12]
        self.border_inner_softness = field[13]
        self.border_bevel_softness = field[14]
        self.border_bevel_position = field[15]
        self.border_opacity = field[16]

        self.border_hue = field[17] / 10.0
        self.border_saturation = field[18] / 1000.0
        self.border_luma = field[19] / 1000.0
        self.light_angle = field[20]
        self.light_altitude = field[21]
        self.mask_enabled = field[22]

        self.mask_top = field[23]
        self.mask_bottom = field[24]
        self.mask_left = field[25]
        self.mask_right = field[26]
        self.rate = field[27]

    def get_border_color_rgb(self):
        return colorsys.hls_to_rgb(self.border_hue / 360.0, self.border_luma, self.border_saturation)

    def __repr__(self):
        return '<key-properties-dve me={}, key={}>'.format(self.index, self.keyer)


class KeyPropertiesLumaField(FieldBase):
    """
    Data from the `KeLm`. The upstream keyer luma-specific properties.
    """

    CODE = "KeLm"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>BB?x HH ?3x', raw)
        self.index = field[0]
        self.keyer = field[1]
        self.premultiplied = field[2]

        self.clip = field[3]
        self.gain = field[4]
        self.key_inverted = field[5]

    def __repr__(self):
        return '<key-properties-luma me={}, key={}>'.format(self.index, self.keyer)


class RecordingDiskField(FieldBase):
    """
    Data from the `RTMD`. Info about an attached recording disk.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      4    u32    Disk index
    4      4    u32    Recording time available in seconds
    8      2    u16    Status bitfield
    10     64   char[] Volume name
    ====== ==== ====== ===========

    === ==========
    Bit Status value
    === ==========
    0   Idle
    1   Unformatted
    2   Ready
    3   Recording
    4   ?
    5   Deleted
    === ==========

    """

    CODE = "RTMD"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>IIH 64s 2x', raw)
        self.index = field[0]
        self.time_available = field[1]
        self.status = field[2]
        self.volumename = self._get_string(field[3])

        self.is_attached = field[2] & 1 << 0 > 0
        self.is_attached = field[2] & 1 << 1 > 0
        self.is_ready = field[2] & 1 << 2 > 0
        self.is_recording = field[2] & 1 << 3 > 0
        self.is_deleted = field[2] & 1 << 5 > 0

    def __repr__(self):
        return '<recording-disk disk={} label={} status={} available={}>'.format(self.index, self.volumename,
                                                                                 self.status, self.time_available)


class RecordingSettingsField(FieldBase):
    """
    Data from the `RMSu`. The settings for the stream recorder.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      128  char[] Audio source index
    128    4    i32    Disk slot 1 index, or -1 for no disk
    132    4    i32    Disk slot 2 index, or -1 for no disk
    136    1    bool   Trigger recording in cameras
    137    3    ?      ?
    ====== ==== ====== ===========

    The recorder settings has 2 slots to select attached USB disks. If no disk is selected the i32 will be -1 otherwise
    it will be the disk number referring a RTMD field
    """

    CODE = "RMSu"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>128s ii ?3x', raw)
        self.filename = self._get_string(field[0])
        self.disk1 = field[1] if field[1] != -1 else None
        self.disk2 = field[2] if field[2] != -1 else None
        self.record_in_cameras = field[3]

    def __repr__(self):
        return '<recording-settings filename={} disk1={} disk2={} in-camera={}>'.format(self.filename, self.disk1,
                                                                                        self.disk2,
                                                                                        self.record_in_cameras)


class RecordingStatusField(FieldBase):
    """
    Data from the `RTMS`. The status for the stream recorder.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Recording status
    4      4    i32    Total recording time available
    ====== ==== ====== ===========

    === ==========
    Bit Status value
    === ==========
    0   Recording
    1   ?
    2   Disk full
    3   Disk error
    4   Disk unformatted
    5   Frames dropped
    6   ?
    7   Stopping
    === ==========

    """

    CODE = "RMTS"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>H2xi', raw)
        self.status = field[0]
        self.time_available = field[1] if field[1] != -1 else None

        self.is_recording = field[0] & 1 << 0 > 0
        self.is_stopping = field[0] & 1 << 7 > 0
        self.disk_full = field[0] & 1 << 2 > 0
        self.disk_error = field[0] & 1 << 3 > 0
        self.disk_unformatted = field[0] & 1 << 4 > 0
        self.has_dropped = field[0] & 1 << 5 > 0

    def __repr__(self):
        return '<recording-status status={} time-available={}>'.format(self.status, self.time_available)


class RecordingDurationField(FieldBase):
    """
    Data from the `RTMR`. The status for the stream recorder.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     Hours
    1      1    u8     Minutes
    2      1    u8     Seconds
    3      1    u8     Frames
    4      1    bool   Has dropped frames
    5      3    ?      unknown
    ====== ==== ====== ===========

    """

    CODE = "RTMR"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>4B ?3x', raw)
        self.hours = field[0]
        self.minutes = field[1]
        self.seconds = field[2]
        self.frames = field[3]
        self.has_dropped_frames = field[4]

    def __repr__(self):
        drop = ''
        if self.has_dropped_frames:
            drop = ' dropped-frames'
        return '<recording-duration {}:{}:{}:{}{}>'.format(self.hours, self.minutes, self.seconds, self.frames, drop)


class MultiviewerPropertiesField(FieldBase):
    """
    Data from the `MvPr`. The layout preset for the multiviewer output.

    The multiviewer is divided in 4 quadrants and the layout bitfield describes which of those quadrants are
    subdivided again in 4 more viewers. The default layout will have the top 2 quadrants not divided and the bottom
    quadrants used for small viewers.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     Multiviewer index, 0-indexed
    1      1    u8     Layout bitfield
    1      1    bool   Flip program/preview
    1      1    ?      unknown
    ====== ==== ====== ===========

    === ==========
    Bit Layout value
    === ==========
    0   Top left small
    1   Top right small
    2   Bottom left small
    4   Bottom right small
    === ==========
    """

    CODE = "MvPr"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack('>BB?B', raw)
        self.index = field[0]
        self.layout = field[1]
        self.flip = field[2]
        self.u1 = field[3]

        self.top_left_small = field[1] & 0x01 > 0
        self.top_right_small = field[1] & 0x02 > 0
        self.bottom_left_small = field[1] & 0x04 > 0
        self.bottom_right_small = field[1] & 0x08 > 0

    def __repr__(self):
        return '<multiviewer-properties mv={} layout={} flip={} u1={}>'.format(self.index, self.layout, self.flip,
                                                                               self.u1)


class MultiviewerInputField(FieldBase):
    """
    Data from the `MvIn`. The input routing for the multiviewer.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     Multiviewer index, 0-indexed
    1      1    u8     Window index 0-9
    2      2    u16    Source index
    4      1    bool   Supports enabling the VU meter
    5      1    bool   Supports enabling the safe area overlay
    6      2    ?      unknown
    ====== ==== ====== ===========
    """

    CODE = "MvIn"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.window, self.source, self.vu, self.safearea = struct.unpack('>BBH??2x', raw)

    def __repr__(self):
        return '<multiviewer-input mv={} win={} source={}>'.format(self.index, self.window, self.source)


class MultiviewerVuField(FieldBase):
    """
    Data from the `VuMC`. This describes if a multiview window has the VU meter overlay enabled.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     Multiviewer index, 0-indexed
    1      1    u8     Window index 0-9
    2      1    bool   VU enabled
    3      1    ?      unknown
    ====== ==== ====== ===========
    """

    CODE = "VuMC"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.window, self.enabled = struct.unpack('>BB?x', raw)

    def __repr__(self):
        return '<multiviewer-vu mv={} win={} enabled={}>'.format(self.index, self.window, self.enabled)


class MultiviewerSafeAreaField(FieldBase):
    """
    Data from the `SaMw`. This describes if a multiview window has the safe area overlay enabled. This is generally
    only enabled on the preview window.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      1    u8     Multiviewer index, 0-indexed
    1      1    u8     Window index 0-9
    2      1    bool   safe area enabled
    3      1    ?      unknown
    ====== ==== ====== ===========
    """

    CODE = "SaMw"

    def __init__(self, raw):
        self.raw = raw
        self.index, self.window, self.enabled = struct.unpack('>BB?x', raw)

    def __repr__(self):
        return '<multiviewer-safe-area mv={} win={} enabled={}>'.format(self.index, self.window, self.enabled)


class LockObtainedField(FieldBase):
    """
    Data from the `LKOB`. This signals that a datastore lock has been successfully obtained for
    a specific datastore index. Used for data transfers.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Store id
    2      2    ?      Unknown
    ====== ==== ====== ===========

    """

    CODE = "LKOB"

    def __init__(self, raw):
        self.raw = raw
        self.store, = struct.unpack('>H2x', raw)

    def __repr__(self):
        return '<lock-obtained store={}>'.format(self.store)


class LockStateField(FieldBase):
    """
    Data from the `LKST`. This updates the state of the locks.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Store id
    2      1    bool   State
    3      1    ?      unknown
    ====== ==== ====== ===========

    """

    CODE = "LKST"

    def __init__(self, raw):
        self.raw = raw
        self.store, self.state, self.u1 = struct.unpack('>H?B', raw)

    def __repr__(self):
        return '<lock-state store={} state={}>'.format(self.store, self.state)


class FileTransferDataField(FieldBase):
    """
    Data from the `FTDa`. This is an incoming chunk of data for a running file transfer.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Transfer id
    2      2    u16    Data length
    ?      ?    bytes  The rest of the packet contains [Data length] bytes of data
    ====== ==== ====== ===========
    """

    CODE = "FTDa"

    def __init__(self, raw):
        self.raw = raw
        self.transfer, self.size = struct.unpack('>HH', raw[0:4])
        self.data = raw[4:(4 + self.size)]

    def __repr__(self):
        return '<file-transfer-data transfer={} size={}>'.format(self.transfer, self.size)


class FileTransferErrorField(FieldBase):
    """
    Data from the `FTDE`. Something went wrong with a file transfer and it has been aborted.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Transfer id
    2      1    u8     Error code
    3      1    ?      unknown
    ====== ==== ====== ===========

    ========== ===========
    Error code Description
    ========== ===========
    1          try-again, Try the transfer again
    2          not-found, The requested store/slot index doesn't contain data
    5          no-lock, You didn't obtain the lock before doing the transfer
    ========== ===========

    """

    CODE = "FTDE"

    def __init__(self, raw):
        self.raw = raw
        self.transfer, self.status = struct.unpack('>HBx', raw)

    def __repr__(self):
        errors = {
            1: 'try-again',
            2: 'not-found',
            5: 'no-lock'
        }

        if self.status in errors:
            status = errors[self.status]
        else:
            status = f'unknown ({self.status})'
        return '<file-transfer-error transfer={} status={}>'.format(self.transfer, status)


class FileTransferDataCompleteField(FieldBase):
    """
    Data from the `FTDC`. Sent after pushing a file.

    ====== ==== ====== ===========
    Offset Size Type   Descriptions
    ====== ==== ====== ===========
    0      2    u16    Transfer id
    2      1    u8     ? (always 1)
    3      1    u8     ? (always 2 or 6)
    ====== ==== ====== ===========

    """

    CODE = "FTDC"

    def __init__(self, raw):
        self.raw = raw
        self.transfer, self.u1, self.u2 = struct.unpack('>HBB', raw)

    def __repr__(self):
        return '<file-transfer-complete transfer={} u1={} u2={}>'.format(self.transfer, self.u1, self.u2)


class MacroPropertiesField(FieldBase):
    """
    Data from the `MPrp`. This is the metadata about a stored macro

    ====== ==== ====== ===========
    Offset Size Type   Description
    ====== ==== ====== ===========
    0      2    u16    Macro slot index
    2      1    bool   is used
    3      1    bool   has invalid commands
    4      2    u16    Name length
    6      2    u16    Description length
    8      ?    char[] Name
    ?      ?    char[] Description
    ====== ==== ====== ===========

    After parsing:

    :ivar index: Macro slot index
    :ivar is_used: Slot contains data
    :ivar is_invalid: Slot contains invalid data
    :ivar name: Name of the macro
    :ivar description: Description of the macro
    """

    CODE = "MPrp"

    def __init__(self, raw):
        self.raw = raw
        field = struct.unpack_from('>H ?? H H', raw, 0)
        self.index = field[0]
        self.is_used = field[1]
        self.is_invalid = field[2]
        name_length = field[3]
        desc_length = field[4]
        self.name, self.description = struct.unpack_from('>{}s {}s'.format(name_length, desc_length), raw, 8)

    def __repr__(self):
        return '<macro-properties: index={} used={} name={}>'.format(self.index, self.is_used,
                                                                     self.name)