1 /*
2    libltc - en+decode linear timecode
3 
4    Copyright (C) 2005 Maarten de Boer <mdeboer@iua.upf.es>
5    Copyright (C) 2006-2016 Robin Gareus <robin@gareus.org>
6    Copyright (C) 2008-2009 Jan <jan@geheimwerk.de>
7 
8    Binary constant generator macro for endianess conversion
9    by Tom Torfs - donated to the public domain
10 
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU Lesser General Public License as
13    published by the Free Software Foundation, either version 3 of the
14    License, or (at your option) any later version.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU Lesser General Public License for more details.
20 
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library.
23    If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 /** turn a numeric literal into a hex constant
27  *  (avoids problems with leading zeroes)
28  *  8-bit constants max value 0x11111111, always fits in unsigned long
29  */
30 #define HEX__(n) 0x##n##LU
31 
32 /**
33  * 8-bit conversion function
34  */
35 #define B8__(x) ((x&0x0000000FLU)?1:0)	\
36 	+((x&0x000000F0LU)?2:0)	 \
37 	+((x&0x00000F00LU)?4:0)	 \
38 	+((x&0x0000F000LU)?8:0)	 \
39 	+((x&0x000F0000LU)?16:0) \
40 	+((x&0x00F00000LU)?32:0) \
41 	+((x&0x0F000000LU)?64:0) \
42 	+((x&0xF0000000LU)?128:0)
43 
44 /** for upto 8-bit binary constants */
45 #define B8(d) ((unsigned char)B8__(HEX__(d)))
46 
47 /** for upto 16-bit binary constants, MSB first */
48 #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))
49 
50 /** turn a numeric literal into a hex constant
51  *(avoids problems with leading zeroes)
52  * 8-bit constants max value 0x11111111, always fits in unsigned long
53  */
54 #define HEX__(n) 0x##n##LU
55 
56 /** 8-bit conversion function */
57 #define B8__(x) ((x&0x0000000FLU)?1:0)	\
58 	+((x&0x000000F0LU)?2:0)  \
59 	+((x&0x00000F00LU)?4:0)  \
60 	+((x&0x0000F000LU)?8:0)  \
61 	+((x&0x000F0000LU)?16:0) \
62 	+((x&0x00F00000LU)?32:0) \
63 	+((x&0x0F000000LU)?64:0) \
64 	+((x&0xF0000000LU)?128:0)
65 
66 
67 /** for upto 8-bit binary constants */
68 #define B8(d) ((unsigned char)B8__(HEX__(d)))
69 
70 /** for upto 16-bit binary constants, MSB first */
71 #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))
72 
73 /* Example usage:
74  * B8(01010101) = 85
75  * B16(10101010,01010101) = 43605
76  */
77 
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <math.h>
82 
83 #include "decoder.h"
84 
85 #define DEBUG_DUMP(msg, f) \
86 { \
87 	int _ii; \
88 	printf("%s", msg); \
89 	for (_ii=0; _ii < (LTC_FRAME_BIT_COUNT >> 3); _ii++) { \
90 		const unsigned char _bit = ((unsigned char*)(f))[_ii]; \
91 		printf("%c", (_bit & B8(10000000) ) ? '1' : '0'); \
92 		printf("%c", (_bit & B8(01000000) ) ? '1' : '0'); \
93 		printf("%c", (_bit & B8(00100000) ) ? '1' : '0'); \
94 		printf("%c", (_bit & B8(00010000) ) ? '1' : '0'); \
95 		printf("%c", (_bit & B8(00001000) ) ? '1' : '0'); \
96 		printf("%c", (_bit & B8(00000100) ) ? '1' : '0'); \
97 		printf("%c", (_bit & B8(00000010) ) ? '1' : '0'); \
98 		printf("%c", (_bit & B8(00000001) ) ? '1' : '0'); \
99 		printf(" "); \
100 	}\
101 	printf("\n"); \
102 }
103 
104 #if (defined _MSC_VER && _MSC_VER <= 1800)
105 #define inline __inline
106 #endif
107 
108 #if (!defined INFINITY && defined _MSC_VER)
109 #define INFINITY std::numeric_limits<double>::infinity()
110 #endif
111 #if (!defined INFINITY && defined HUGE_VAL)
112 #define INFINITY HUGE_VAL
113 #endif
114 
calc_volume_db(LTCDecoder * d)115 static double calc_volume_db(LTCDecoder *d) {
116 	if (d->snd_to_biphase_max <= d->snd_to_biphase_min)
117 		return -INFINITY;
118 	return (20.0 * log10((d->snd_to_biphase_max - d->snd_to_biphase_min) / 255.0));
119 }
120 
parse_ltc(LTCDecoder * d,unsigned char bit,ltc_off_t offset,ltc_off_t posinfo)121 static void parse_ltc(LTCDecoder *d, unsigned char bit, ltc_off_t offset, ltc_off_t posinfo) {
122 	int bit_num, bit_set, byte_num;
123 
124 	if (d->bit_cnt == 0) {
125 		memset(&d->ltc_frame, 0, sizeof(LTCFrame));
126 
127 		if (d->frame_start_prev < 0) {
128 			d->frame_start_off = posinfo - d->snd_to_biphase_period;
129 		} else {
130 			d->frame_start_off = d->frame_start_prev;
131 		}
132 	}
133 	d->frame_start_prev = offset + posinfo;
134 
135 	if (d->bit_cnt >= LTC_FRAME_BIT_COUNT) {
136 		/* shift bits backwards */
137 		int k = 0;
138 		const int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;
139 
140 		for (k=0; k< byte_num_max; k++) {
141 			const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
142 			unsigned char bo = 0;
143 			bo |= (bi & B8(10000000) ) ? B8(01000000) : 0;
144 			bo |= (bi & B8(01000000) ) ? B8(00100000) : 0;
145 			bo |= (bi & B8(00100000) ) ? B8(00010000) : 0;
146 			bo |= (bi & B8(00010000) ) ? B8(00001000) : 0;
147 			bo |= (bi & B8(00001000) ) ? B8(00000100) : 0;
148 			bo |= (bi & B8(00000100) ) ? B8(00000010) : 0;
149 			bo |= (bi & B8(00000010) ) ? B8(00000001) : 0;
150 			if (k+1 < byte_num_max) {
151 				bo |= ( (((unsigned char*)&d->ltc_frame)[k+1]) & B8(00000001) ) ? B8(10000000): B8(00000000);
152 			}
153 			((unsigned char*)&d->ltc_frame)[k] = bo;
154 		}
155 
156 		d->frame_start_off += ceil(d->snd_to_biphase_period);
157 		d->bit_cnt--;
158 	}
159 
160 	d->decoder_sync_word <<= 1;
161 	if (bit) {
162 
163 		d->decoder_sync_word |= B16(00000000,00000001);
164 
165 		if (d->bit_cnt < LTC_FRAME_BIT_COUNT) {
166 			// Isolating the lowest three bits: the location of this bit in the current byte
167 			bit_num = (d->bit_cnt & B8(00000111));
168 			// Using the bit number to define which of the eight bits to set
169 			bit_set = (B8(00000001) << bit_num);
170 			// Isolating the higher bits: the number of the byte/char the target bit is contained in
171 			byte_num = d->bit_cnt >> 3;
172 
173 			(((unsigned char*)&d->ltc_frame)[byte_num]) |= bit_set;
174 		}
175 
176 	}
177 	d->bit_cnt++;
178 
179 	if (d->decoder_sync_word == B16(00111111,11111101) /*LTC Sync Word 0x3ffd*/) {
180 		if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
181 			int bc;
182 
183 			memcpy( &d->queue[d->queue_write_off].ltc,
184 				&d->ltc_frame,
185 				sizeof(LTCFrame));
186 
187 			for(bc = 0; bc < LTC_FRAME_BIT_COUNT; ++bc) {
188 				const int btc = (d->biphase_tic + bc ) % LTC_FRAME_BIT_COUNT;
189 				d->queue[d->queue_write_off].biphase_tics[bc] = d->biphase_tics[btc];
190 			}
191 
192 			d->queue[d->queue_write_off].off_start = d->frame_start_off;
193 			d->queue[d->queue_write_off].off_end = posinfo + (ltc_off_t) offset - 1LL;
194 			d->queue[d->queue_write_off].reverse = 0;
195 			d->queue[d->queue_write_off].volume = calc_volume_db(d);
196 			d->queue[d->queue_write_off].sample_min = d->snd_to_biphase_min;
197 			d->queue[d->queue_write_off].sample_max = d->snd_to_biphase_max;
198 
199 			d->queue_write_off++;
200 
201 			if (d->queue_write_off == d->queue_len)
202 				d->queue_write_off = 0;
203 		}
204 		d->bit_cnt = 0;
205 	}
206 
207 	if (d->decoder_sync_word == B16(10111111,11111100) /* reverse sync-word*/) {
208 		if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
209 			/* reverse frame */
210 			int bc;
211 			int k = 0;
212 			int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;
213 
214 			/* swap bits */
215 			for (k=0; k< byte_num_max; k++) {
216 				const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
217 				unsigned char bo = 0;
218 				bo |= (bi & B8(10000000) ) ? B8(00000001) : 0;
219 				bo |= (bi & B8(01000000) ) ? B8(00000010) : 0;
220 				bo |= (bi & B8(00100000) ) ? B8(00000100) : 0;
221 				bo |= (bi & B8(00010000) ) ? B8(00001000) : 0;
222 				bo |= (bi & B8(00001000) ) ? B8(00010000) : 0;
223 				bo |= (bi & B8(00000100) ) ? B8(00100000) : 0;
224 				bo |= (bi & B8(00000010) ) ? B8(01000000) : 0;
225 				bo |= (bi & B8(00000001) ) ? B8(10000000) : 0;
226 				((unsigned char*)&d->ltc_frame)[k] = bo;
227 			}
228 
229 			/* swap bytes */
230 			byte_num_max-=2; // skip sync-word
231 			for (k=0; k< (byte_num_max)/2; k++) {
232 				const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
233 				((unsigned char*)&d->ltc_frame)[k] = ((unsigned char*)&d->ltc_frame)[byte_num_max-1-k];
234 				((unsigned char*)&d->ltc_frame)[byte_num_max-1-k] = bi;
235 			}
236 
237 			memcpy( &d->queue[d->queue_write_off].ltc,
238 				&d->ltc_frame,
239 				sizeof(LTCFrame));
240 
241 			for(bc = 0; bc < LTC_FRAME_BIT_COUNT; ++bc) {
242 				const int btc = (d->biphase_tic + bc ) % LTC_FRAME_BIT_COUNT;
243 				d->queue[d->queue_write_off].biphase_tics[bc] = d->biphase_tics[btc];
244 			}
245 
246 			d->queue[d->queue_write_off].off_start = d->frame_start_off - 16 * d->snd_to_biphase_period;
247 			d->queue[d->queue_write_off].off_end = posinfo + (ltc_off_t) offset - 1LL - 16 * d->snd_to_biphase_period;
248 			d->queue[d->queue_write_off].reverse = (LTC_FRAME_BIT_COUNT >> 3) * 8 * d->snd_to_biphase_period;
249 			d->queue[d->queue_write_off].volume = calc_volume_db(d);
250 			d->queue[d->queue_write_off].sample_min = d->snd_to_biphase_min;
251 			d->queue[d->queue_write_off].sample_max = d->snd_to_biphase_max;
252 
253 			d->queue_write_off++;
254 
255 			if (d->queue_write_off == d->queue_len)
256 				d->queue_write_off = 0;
257 		}
258 		d->bit_cnt = 0;
259 	}
260 }
261 
biphase_decode2(LTCDecoder * d,ltc_off_t offset,ltc_off_t pos)262 static inline void biphase_decode2(LTCDecoder *d, ltc_off_t offset, ltc_off_t pos) {
263 
264 	d->biphase_tics[d->biphase_tic] = d->snd_to_biphase_period;
265 	d->biphase_tic = (d->biphase_tic + 1) % LTC_FRAME_BIT_COUNT;
266 	if (d->snd_to_biphase_cnt <= 2 * d->snd_to_biphase_period) {
267 		pos -= (d->snd_to_biphase_period - d->snd_to_biphase_cnt);
268 	}
269 
270 	if (d->snd_to_biphase_state == d->biphase_prev) {
271 		d->biphase_state = 1;
272 		parse_ltc(d, 0, offset, pos);
273 	} else {
274 		d->biphase_state = 1 - d->biphase_state;
275 		if (d->biphase_state == 1) {
276 			parse_ltc(d, 1, offset, pos);
277 		}
278 	}
279 	d->biphase_prev = d->snd_to_biphase_state;
280 }
281 
decode_ltc(LTCDecoder * d,ltcsnd_sample_t * sound,size_t size,ltc_off_t posinfo)282 void decode_ltc(LTCDecoder *d, ltcsnd_sample_t *sound, size_t size, ltc_off_t posinfo) {
283 	size_t i;
284 
285 	for (i = 0 ; i < size ; i++) {
286 		ltcsnd_sample_t max_threshold, min_threshold;
287 
288 		/* track minimum and maximum values */
289 		d->snd_to_biphase_min = SAMPLE_CENTER - (((SAMPLE_CENTER - d->snd_to_biphase_min) * 15) / 16);
290 		d->snd_to_biphase_max = SAMPLE_CENTER + (((d->snd_to_biphase_max - SAMPLE_CENTER) * 15) / 16);
291 
292 		if (sound[i] < d->snd_to_biphase_min)
293 			d->snd_to_biphase_min = sound[i];
294 		if (sound[i] > d->snd_to_biphase_max)
295 			d->snd_to_biphase_max = sound[i];
296 
297 		/* set the thresholds for hi/lo state tracking */
298 		min_threshold = SAMPLE_CENTER - (((SAMPLE_CENTER - d->snd_to_biphase_min) * 8) / 16);
299 		max_threshold = SAMPLE_CENTER + (((d->snd_to_biphase_max - SAMPLE_CENTER) * 8) / 16);
300 
301 		if ( /* Check for a biphase state change */
302 			   (  d->snd_to_biphase_state && (sound[i] > max_threshold) )
303 			|| ( !d->snd_to_biphase_state && (sound[i] < min_threshold) )
304 		   ) {
305 
306 			/* If the sample count has risen above the biphase length limit */
307 			if (d->snd_to_biphase_cnt > d->snd_to_biphase_lmt) {
308 				/* single state change within a biphase priod. decode to a 0 */
309 				biphase_decode2(d, i, posinfo);
310 				biphase_decode2(d, i, posinfo);
311 
312 			} else {
313 				/* "short" state change covering half a period
314 				 * together with the next or previous state change decode to a 1
315 				 */
316 				d->snd_to_biphase_cnt *= 2;
317 				biphase_decode2(d, i, posinfo);
318 
319 			}
320 
321 			if (d->snd_to_biphase_cnt > (d->snd_to_biphase_period * 4)) {
322 				/* "long" silence in between
323 				 * -> reset parser, don't use it for phase-tracking
324 				 */
325 				d->bit_cnt = 0;
326 			} else  {
327 				/* track speed variations
328 				 * As this is only executed at a state change,
329 				 * d->snd_to_biphase_cnt is an accurate representation of the current period length.
330 				 */
331 				d->snd_to_biphase_period = (d->snd_to_biphase_period * 3.0 + d->snd_to_biphase_cnt) / 4.0;
332 
333 				/* This limit specifies when a state-change is
334 				 * considered biphase-clock or 2*biphase-clock.
335 				 * The relation with period has been determined
336 				 * empirically through trial-and-error */
337 				d->snd_to_biphase_lmt = (d->snd_to_biphase_period * 3) / 4;
338 			}
339 
340 			d->snd_to_biphase_cnt = 0;
341 			d->snd_to_biphase_state = !d->snd_to_biphase_state;
342 		}
343 		d->snd_to_biphase_cnt++;
344 	}
345 }
346