]> git.sesse.net Git - casparcg/blob - dependencies64/bluefish/include/BlueHANC.h
[scene] More documentation with auto completion for example values/expressions.
[casparcg] / dependencies64 / bluefish / include / BlueHANC.h
1 #pragma once 
2 #include "BlueDriver_p.h"
3
4 #ifdef _WINDOWS
5 #pragma pack(push,1)
6 #endif
7
8 #define BLUE_HANC_INVALID_DID   (0x0)
9
10 #define BLUE_HANC_AUDIOGROUP1   (0x2FF)
11 #define BLUE_HANC_AUDIOGROUP2   (0x1FD)
12 #define BLUE_HANC_AUDIOGROUP3   (0x1FB)
13 #define BLUE_HANC_AUDIOGROUP4   (0x2F9)
14 #define BLUE_HANC_RP188                 (0x260)
15 #define BLUE_HANC_AUDIOGROUP1_CONTROL   (0x1EF)
16 #define BLUE_HANC_AUDIOGROUP2_CONTROL   (0x2EE)
17 #define BLUE_HANC_AUDIOGROUP3_CONTROL   (0x2ED)
18 #define BLUE_HANC_AUDIOGROUP4_CONTROL   (0x1EC)
19 #define BLUE_HANC_AUDIOGROUP1_EXTENDED  (0x1FE)
20 #define BLUE_HANC_AUDIOGROUP2_EXTENDED  (0x2FC)
21 #define BLUE_HANC_AUDIOGROUP3_EXTENDED  (0x2FA)
22 #define BLUE_HANC_AUDIOGROUP4_EXTENDED  (0x1F8)
23
24
25 #define HANC_PACKET_HEADER_CONST        (0xBFFFFC00)
26
27 #define BLUE_HANC_START_NEWLINE(line_number) ((0xC0000000)| (line_number << 16))
28
29 #define BLUE_HANC_CONTROL_WORD                           (0xC0000000)
30 #define BLUE_HANC_3DATA_PACKET_WORD                      (0x80000000)
31 #define BLUE_HANC_2DATA_PACKET_WORD                      (0x40000000)
32 #define BLUE_HANC_1DATA_PACKET_WORD                      (0x00000000)
33 #define BLUE_HANC_ENDOF_FRAME()                          ((0xC0000000)| (1 << 15))
34
35
36 #define AESAUDIO_DATA_BLOCKSIZE                                         (192)
37 #define MAX_HANC_BUFFER_SIZE                                            (65536) //256*256
38 #define MAX_HANC_BUFFER_SIZE_WITHOUT_HEADER                     (65536 - 0x20/4)        //32 bytes = 8 * 4 (8 * UINT32)
39 #define MAX_HANC_BUFFER_SIZE_BYTES                                      (256*1024)
40 #define MAX_HANC_BUFFER_SIZE_WITHOUT_HEADER_BYTES       (256*1024 - 0x20)
41 /* 
42 HANC Packet header structure
43 Contains 2 type of structure , 
44 which makes it easier to parse the data
45 */
46
47 struct GenericV210_structure
48 {
49 #if defined(__LITTLE_ENDIAN__) || defined(_WINDOWS) || defined(BLUE_LINUX_CODE)
50         BLUE_UINT32 first_word:10,second_word:10,third_word:10,unused:2;
51 #else
52         BLUE_UINT32 unused:2,third_word:10,second_word:10,first_word:10;
53 #endif
54 #ifndef _WINDOWS
55 }__attribute__((packed));
56 #else
57 };
58 #endif
59
60 union GenericV210_union
61 {
62         struct GenericV210_structure v210_struct;
63         BLUE_UINT32 v210_word;
64 };
65
66 /* HANC packet header*/
67 struct HancPacketHeaderStruct
68 {
69 #if defined(__LITTLE_ENDIAN__) || defined(_WINDOWS) || defined(BLUE_LINUX_CODE)
70         union GenericV210_union ancillary_data_flag; // 0x0,0x3FF,0x3FF, This is a constant defined by smpte
71         union GenericV210_union packet_info;    //  first 10 bit word --> Data ID
72                                                 //      Commonly used  Data ID packet values are 
73                                                 //              1) 0x2FF --> Group1 Embedded Audio packet
74                                                 //              2) 0x1FD --> Group2 Embedded Audio Packet
75                                                 //              3) 0x1FB --> Group3 Embedded Audio Packet
76                                                 //              4) 0x2F9 --> Group4 Embedded Audio packet
77                                                 //  second 10 bit word --> Data Block Number
78                                                 //      This is used for type 1 packets.
79                                                 //  third 10 bit word --> Data Count 
80                                                 //      This 10 bit word specifies the amount of user data 
81                                                 //      that this hanc will contain.
82 #else
83         union GenericV210_union packet_info;
84         union GenericV210_union ancillary_data_flag;
85 #endif
86 #ifndef _WINDOWS
87 }__attribute__((packed));
88 #else
89 };
90 #endif
91
92
93 /* Audio SubFrame Packet */
94 struct BlueAudioSubFrameStruct
95 {       
96 #if defined(__LITTLE_ENDIAN__) || defined(_WINDOWS) || defined(BLUE_LINUX_CODE)
97         BLUE_UINT32     ZBit:1,                                 //bit 0         set to declare start of channel status word
98                         Channel:2,                                      //bit 1-2
99                         AudioData_0_5:6,                        //bit 3-8
100                         NotBit8:1,                                      //bit 9
101                         AudioData_6_14:9,                       //bit 10-18
102                         NotBit18:1,                                     //bit 19        use same value as NotBit8 (bit 9)
103                         AudioData_15_19:5,                      //bit 20-24
104                         AESSampleValidityBit:1,         //bit 25
105                         AESUserBit:1,                           //bit 26
106                         AESAudioChannelStatusBit:1,     //bit 27        one bit of the channel status word
107                         ParityBit:1,                            //bit 28        xor of all bits except (NotBit8 (bit 9) and NotBit18 (bit 19))
108                         NotBit31:1,                                     //bit 29        not of ParityBit (bit 28)
109                         akiraControlBits:2;                     //bit 30-31
110 #else
111         BLUE_UINT32     akiraControlBits:2,
112                         NotBit31:1,
113                         ParityBit:1,
114                         AESAudioChannelStatusBit:1,
115                         AESUserBit:1,
116                         AESSampleValidityBit:1,
117                         AudioData_15_19:5,
118                         NotBit18:1,
119                         AudioData_6_14:9,
120                         NotBit8:1,
121                         AudioData_0_5:6,
122                         Channel:2,
123                         ZBit:1;
124 #endif
125
126 #ifndef _WINDOWS
127 }__attribute__((packed));
128 #else
129 };
130 #endif
131
132 union BlueAudioSubFrameHeader
133 {
134         struct BlueAudioSubFrameStruct audioSubFrame;
135         BLUE_UINT32  BlueAudioSubFrameWord;
136         struct GenericV210_structure audioSubFrame_v210;
137 };
138
139 #define  MAX_AUDIO_SUBFRAMES_IN_A_LINE (64) // 4 samples per audio group  and 4 channesl for each  audio group per sample
140
141
142 /*
143 Time code structure that the function expects is the same format as LTC time code
144 bits 0 - 3 :units of frame 
145 bits 4 - 7: binary group1 
146 bits 8 - 9: tens of frame 
147 bits 10 -11: flags 
148 bits 12 -15: binary group2 
149 bits 16-19 : units of seconds 
150 bits 20-23 : binary group3 
151
152 bits 24 - 26: tens of seconds
153 bit 27 : flag 
154 bits 28 - 31: group binary4  
155 bits 32 -35: units of minutes 
156
157 bits 36 - 39 :binary5 
158 bits 40 - 42: tens of minutes 
159 bit 43 : flag 
160 bits 44 - 47: binary group6 
161
162 bits 48 - 51: units of hours 
163 bits 52 - 55: binary group7 
164 bits 56 - 57: tens of hours 
165 bits 58 - 59: flag
166 bits 60 - 63: binary8
167
168 */
169 struct LTC_TimeCode
170 {
171 #if defined(__LITTLE_ENDIAN__) || defined(_WINDOWS) || defined(BLUE_LINUX_CODE)
172         BLUE_UINT64     unit_frame:4,binary1:4,ten_frame:2,drop_frame_flag:1,color_frame_flag:1,
173                                  binary2:4,unit_second:4,binary3:4,ten_second:3,unsued_1:1,binary4:4,
174                                  unit_minute:4,binary5:4,ten_minute:3,unsued_2:1,binary6:4,unit_hours:4,
175                                  binary7:4,ten_hours:2,unsued_3:2,binary8:4;
176 #else
177         BLUE_UINT64             binary8:4,unsued_3:2,ten_hours:2,binary7:4,
178                                 unit_hours:4,binary6:4,unused_2:1,ten_minute:3,binary5:4,unit_minute:4,
179                                 binary4:4,unused_1:1,ten_second:3,binary3:4,unit_second:4,binary2:4,
180                                 color_frame_flag:1,drop_frame_flag:1,ten_frame:2,binary1:4,unit_frame:4;
181 #endif
182
183 #ifndef _WINDOWS
184 }__attribute__((packed));
185 #else
186 };
187 #endif
188
189 struct LTC_TimeCode_union
190 {
191         union 
192         {
193                 struct LTC_TimeCode struct_ltc;
194                 BLUE_UINT64 lt_64_value;
195         };
196 };
197
198 /*
199         This is used to unpack the timecode word properly and quickly
200         in RP188 each 4 bits of the timecode is put into a 10 bit word.
201         So this structure helps in decoding 
202 */
203 struct nibble_struct
204 {
205         BLUE_UINT8      first_half:4,second_half:4;
206
207 #ifndef _WINDOWS
208 }__attribute__((packed));
209 #else
210 };
211 #endif
212
213 struct TimeCode
214 {
215         union 
216         {
217         struct LTC_TimeCode struct_ltc;
218         BLUE_UINT64 ltc;
219         struct nibble_struct ltc_char[8]; 
220         };
221         
222 #ifndef _WINDOWS
223 }__attribute__((packed));
224 #else
225 };
226 #endif
227
228 struct HANCTimeCodeStruct 
229 {       
230 #if defined(__LITTLE_ENDIAN__) || defined(_WINDOWS) || defined(BLUE_LINUX_CODE)
231         BLUE_UINT32     zero_0:3,
232                                 DBB_0:1,
233                                 ANC_0:4,
234                                 partiy_0:1,
235                                 NotBit8_0:1,
236                                 zero_1:3,
237                                 DBB_1:1,
238                                 ANC_1:4,
239                                 partiy_1:1,
240                                 NotBit8_1:1,
241                                 zero_2:3,
242                                 DBB_2:1,
243                                 ANC_2:4,
244                                 partiy_2:1,
245                                 NotBit8_2:1,
246                                 akiraControlBits:2;
247 #else
248         BLUE_UINT32 akiraControlBits:2,
249                                 Notbit8_2:1,
250                                 partiy_2:1,
251                                 ANC_2:4,
252                                 DBB_2:1,
253                                 zero_2:3,
254                                 NotBit81_1:1,
255                                 partiy_1:1,
256                                 ANC_1:4,
257                                 DBB_1:1,
258                                 zero_1:3,
259                                 NotBit8_0:1,
260                                 partiy_0:1,
261                                 ANC_0:4,
262                                 DBB_0:1,
263                                 zero_0:3;
264 #endif
265
266 #ifndef _WINDOWS
267 }__attribute__((packed));
268 #else
269 };
270 #endif
271
272 union HANCTimeCode
273 {
274         struct HANCTimeCodeStruct hanc_struct;
275         BLUE_UINT32 hanc_word;
276 };
277
278 struct BAG2VancTimeCodeStruct 
279 {       
280 #if defined(__LITTLE_ENDIAN__) || defined(_WINDOWS) || defined(BLUE_LINUX_CODE)
281         BLUE_UINT16     zero_0:3,
282                                 DBB_0:1,
283                                 ANC_0:4,
284                                 partiy_0:1,
285                                 NotBit8_0:1;
286 #else
287         BLUE_UINT16     NotBit8_0:1,
288                                 partiy_0:1,
289                                 ANC_0:4,
290                                 DBB_0:1,
291                                 zero_0:3;
292 #endif
293
294 #ifndef _WINDOWS
295 }__attribute__((packed));
296 #else
297 };
298 #endif
299
300 union BAG2VancTimeCode
301 {
302         struct BAG2VancTimeCodeStruct vanc_struct;
303         BLUE_UINT16 vanc_word;
304 };
305
306
307 inline BLUE_UINT64 convert_countto_timecode(BLUE_UINT32 frame_count,BLUE_UINT32 framePerSec)
308 {
309         unsigned int frames ,second,minutes ,hour   ;
310         struct TimeCode rp188_timcode;
311         hour = frame_count/(60*60*framePerSec);
312         minutes = frame_count%(60*60*framePerSec);
313         second = minutes%(60*framePerSec);
314         frames = second %framePerSec;
315         second = second/(framePerSec);
316         minutes=minutes/(60*framePerSec);
317         rp188_timcode.ltc = 0;
318         rp188_timcode.struct_ltc.unit_frame = (frames%10);
319         rp188_timcode.struct_ltc.ten_frame = (frames/10);
320         rp188_timcode.struct_ltc.unit_second = (second%10);
321         rp188_timcode.struct_ltc.ten_second = (second/10);
322         rp188_timcode.struct_ltc.unit_minute = (minutes%10);
323         rp188_timcode.struct_ltc.ten_minute = (minutes/10);
324         rp188_timcode.struct_ltc.unit_hours = (hour%10);
325         rp188_timcode.struct_ltc.ten_hours = (hour/10);
326         
327         return rp188_timcode.ltc;
328 }
329
330
331 inline BLUE_UINT64 convert_timecode_to_count(BLUE_UINT64 timecode,
332                                                                                          BLUE_UINT32 framePerSec,
333                                                                                          unsigned int & frames ,
334                                                                                          unsigned int & second,
335                                                                                          unsigned int & minutes ,
336                                                                                          unsigned int & hours)
337 {
338         
339         struct TimeCode rp188_timecode;
340         rp188_timecode.ltc = timecode;
341         hours  = (BLUE_UINT32)((unsigned int)rp188_timecode.struct_ltc.ten_hours*10)+(unsigned int)rp188_timecode.struct_ltc.unit_hours;
342         minutes = (BLUE_UINT32)((unsigned int)rp188_timecode.struct_ltc.ten_minute*10)+(unsigned int)rp188_timecode.struct_ltc.unit_minute;
343         second =  (BLUE_UINT32)((unsigned int)rp188_timecode.struct_ltc.ten_second*10)+(unsigned int)rp188_timecode.struct_ltc.unit_second;
344         frames =         (BLUE_UINT32)((unsigned int)rp188_timecode.struct_ltc.ten_frame*10)+(unsigned int)rp188_timecode.struct_ltc.unit_frame;                
345         return rp188_timecode.ltc;
346 }
347
348 // Determine endianess at run-time
349 inline BLUE_UINT32 Int32SwapBigLittle(const BLUE_UINT32 i)
350 {
351     unsigned char c1, c2, c3, c4;
352         const int endian = 1;
353         #define is_bigendian() ( (*(char*) & endian) == 0 )
354
355     if (is_bigendian())
356         {
357         c1 = i & 255;
358         c2 = (i >> 8) & 255;
359         c3 = (i >> 16) & 255;
360         c4 = (i >> 24) & 255;
361                 
362                 return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
363     }
364         else
365         {
366         return i;
367         }
368 }
369
370 #ifdef _WINDOWS
371 #pragma pack(pop)
372 #endif