summaryrefslogtreecommitdiff
path: root/libs/libltc/decoder.c
blob: 7ba2a6c4f9da67b0bf96f7cf253cd04bf645de69 (plain)
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
/*
   libltc - en+decode linear timecode

   Copyright (C) 2005 Maarten de Boer <mdeboer@iua.upf.es>
   Copyright (C) 2006-2016 Robin Gareus <robin@gareus.org>
   Copyright (C) 2008-2009 Jan <jan@geheimwerk.de>

   Binary constant generator macro for endianess conversion
   by Tom Torfs - donated to the public domain

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library.
   If not, see <http://www.gnu.org/licenses/>.
*/

/** turn a numeric literal into a hex constant
 *  (avoids problems with leading zeroes)
 *  8-bit constants max value 0x11111111, always fits in unsigned long
 */
#define HEX__(n) 0x##n##LU

/**
 * 8-bit conversion function
 */
#define B8__(x) ((x&0x0000000FLU)?1:0)	\
	+((x&0x000000F0LU)?2:0)	 \
	+((x&0x00000F00LU)?4:0)	 \
	+((x&0x0000F000LU)?8:0)	 \
	+((x&0x000F0000LU)?16:0) \
	+((x&0x00F00000LU)?32:0) \
	+((x&0x0F000000LU)?64:0) \
	+((x&0xF0000000LU)?128:0)

/** for upto 8-bit binary constants */
#define B8(d) ((unsigned char)B8__(HEX__(d)))

/** for upto 16-bit binary constants, MSB first */
#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))

/** turn a numeric literal into a hex constant
 *(avoids problems with leading zeroes)
 * 8-bit constants max value 0x11111111, always fits in unsigned long
 */
#define HEX__(n) 0x##n##LU

/** 8-bit conversion function */
#define B8__(x) ((x&0x0000000FLU)?1:0)	\
	+((x&0x000000F0LU)?2:0)  \
	+((x&0x00000F00LU)?4:0)  \
	+((x&0x0000F000LU)?8:0)  \
	+((x&0x000F0000LU)?16:0) \
	+((x&0x00F00000LU)?32:0) \
	+((x&0x0F000000LU)?64:0) \
	+((x&0xF0000000LU)?128:0)


/** for upto 8-bit binary constants */
#define B8(d) ((unsigned char)B8__(HEX__(d)))

/** for upto 16-bit binary constants, MSB first */
#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))

/* Example usage:
 * B8(01010101) = 85
 * B16(10101010,01010101) = 43605
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "ltc/decoder.h"

#define DEBUG_DUMP(msg, f) \
{ \
	int _ii; \
	printf("%s", msg); \
	for (_ii=0; _ii < (LTC_FRAME_BIT_COUNT >> 3); _ii++) { \
		const unsigned char _bit = ((unsigned char*)(f))[_ii]; \
		printf("%c", (_bit & B8(10000000) ) ? '1' : '0'); \
		printf("%c", (_bit & B8(01000000) ) ? '1' : '0'); \
		printf("%c", (_bit & B8(00100000) ) ? '1' : '0'); \
		printf("%c", (_bit & B8(00010000) ) ? '1' : '0'); \
		printf("%c", (_bit & B8(00001000) ) ? '1' : '0'); \
		printf("%c", (_bit & B8(00000100) ) ? '1' : '0'); \
		printf("%c", (_bit & B8(00000010) ) ? '1' : '0'); \
		printf("%c", (_bit & B8(00000001) ) ? '1' : '0'); \
		printf(" "); \
	}\
	printf("\n"); \
}

static double calc_volume_db(LTCDecoder *d) {
	if (d->snd_to_biphase_max <= d->snd_to_biphase_min)
		return -INFINITY;
	return (20.0 * log10((d->snd_to_biphase_max - d->snd_to_biphase_min) / 255.0));
}

static void parse_ltc(LTCDecoder *d, unsigned char bit, ltc_off_t offset, ltc_off_t posinfo) {
	int bit_num, bit_set, byte_num;

	if (d->bit_cnt == 0) {
		memset(&d->ltc_frame, 0, sizeof(LTCFrame));

		if (d->frame_start_prev < 0) {
			d->frame_start_off = posinfo - d->snd_to_biphase_period;
		} else {
			d->frame_start_off = d->frame_start_prev;
		}
	}
	d->frame_start_prev = offset + posinfo;

	if (d->bit_cnt >= LTC_FRAME_BIT_COUNT) {
		/* shift bits backwards */
		int k = 0;
		const int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;

		for (k=0; k< byte_num_max; k++) {
			const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
			unsigned char bo = 0;
			bo |= (bi & B8(10000000) ) ? B8(01000000) : 0;
			bo |= (bi & B8(01000000) ) ? B8(00100000) : 0;
			bo |= (bi & B8(00100000) ) ? B8(00010000) : 0;
			bo |= (bi & B8(00010000) ) ? B8(00001000) : 0;
			bo |= (bi & B8(00001000) ) ? B8(00000100) : 0;
			bo |= (bi & B8(00000100) ) ? B8(00000010) : 0;
			bo |= (bi & B8(00000010) ) ? B8(00000001) : 0;
			if (k+1 < byte_num_max) {
				bo |= ( (((unsigned char*)&d->ltc_frame)[k+1]) & B8(00000001) ) ? B8(10000000): B8(00000000);
			}
			((unsigned char*)&d->ltc_frame)[k] = bo;
		}

		d->frame_start_off += ceil(d->snd_to_biphase_period);
		d->bit_cnt--;
	}

	d->decoder_sync_word <<= 1;
	if (bit) {

		d->decoder_sync_word |= B16(00000000,00000001);

		if (d->bit_cnt < LTC_FRAME_BIT_COUNT) {
			// Isolating the lowest three bits: the location of this bit in the current byte
			bit_num = (d->bit_cnt & B8(00000111));
			// Using the bit number to define which of the eight bits to set
			bit_set = (B8(00000001) << bit_num);
			// Isolating the higher bits: the number of the byte/char the target bit is contained in
			byte_num = d->bit_cnt >> 3;

			(((unsigned char*)&d->ltc_frame)[byte_num]) |= bit_set;
		}

	}
	d->bit_cnt++;

	if (d->decoder_sync_word == B16(00111111,11111101) /*LTC Sync Word 0x3ffd*/) {
		if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
			int bc;

			memcpy( &d->queue[d->queue_write_off].ltc,
				&d->ltc_frame,
				sizeof(LTCFrame));

			for(bc = 0; bc < LTC_FRAME_BIT_COUNT; ++bc) {
				const int btc = (d->biphase_tic + bc ) % LTC_FRAME_BIT_COUNT;
				d->queue[d->queue_write_off].biphase_tics[bc] = d->biphase_tics[btc];
			}

			d->queue[d->queue_write_off].off_start = d->frame_start_off;
			d->queue[d->queue_write_off].off_end = posinfo + (ltc_off_t) offset - 1LL;
			d->queue[d->queue_write_off].reverse = 0;
			d->queue[d->queue_write_off].volume = calc_volume_db(d);
			d->queue[d->queue_write_off].sample_min = d->snd_to_biphase_min;
			d->queue[d->queue_write_off].sample_max = d->snd_to_biphase_max;

			d->queue_write_off++;

			if (d->queue_write_off == d->queue_len)
				d->queue_write_off = 0;
		}
		d->bit_cnt = 0;
	}

	if (d->decoder_sync_word == B16(10111111,11111100) /* reverse sync-word*/) {
		if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
			/* reverse frame */
			int bc;
			int k = 0;
			int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;

			/* swap bits */
			for (k=0; k< byte_num_max; k++) {
				const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
				unsigned char bo = 0;
				bo |= (bi & B8(10000000) ) ? B8(00000001) : 0;
				bo |= (bi & B8(01000000) ) ? B8(00000010) : 0;
				bo |= (bi & B8(00100000) ) ? B8(00000100) : 0;
				bo |= (bi & B8(00010000) ) ? B8(00001000) : 0;
				bo |= (bi & B8(00001000) ) ? B8(00010000) : 0;
				bo |= (bi & B8(00000100) ) ? B8(00100000) : 0;
				bo |= (bi & B8(00000010) ) ? B8(01000000) : 0;
				bo |= (bi & B8(00000001) ) ? B8(10000000) : 0;
				((unsigned char*)&d->ltc_frame)[k] = bo;
			}

			/* swap bytes */
			byte_num_max-=2; // skip sync-word
			for (k=0; k< (byte_num_max)/2; k++) {
				const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
				((unsigned char*)&d->ltc_frame)[k] = ((unsigned char*)&d->ltc_frame)[byte_num_max-1-k];
				((unsigned char*)&d->ltc_frame)[byte_num_max-1-k] = bi;
			}

			memcpy( &d->queue[d->queue_write_off].ltc,
				&d->ltc_frame,
				sizeof(LTCFrame));

			for(bc = 0; bc < LTC_FRAME_BIT_COUNT; ++bc) {
				const int btc = (d->biphase_tic + bc ) % LTC_FRAME_BIT_COUNT;
				d->queue[d->queue_write_off].biphase_tics[bc] = d->biphase_tics[btc];
			}

			d->queue[d->queue_write_off].off_start = d->frame_start_off - 16 * d->snd_to_biphase_period;
			d->queue[d->queue_write_off].off_end = posinfo + (ltc_off_t) offset - 1LL - 16 * d->snd_to_biphase_period;
			d->queue[d->queue_write_off].reverse = (LTC_FRAME_BIT_COUNT >> 3) * 8 * d->snd_to_biphase_period;
			d->queue[d->queue_write_off].volume = calc_volume_db(d);
			d->queue[d->queue_write_off].sample_min = d->snd_to_biphase_min;
			d->queue[d->queue_write_off].sample_max = d->snd_to_biphase_max;

			d->queue_write_off++;

			if (d->queue_write_off == d->queue_len)
				d->queue_write_off = 0;
		}
		d->bit_cnt = 0;
	}
}

static inline void biphase_decode2(LTCDecoder *d, ltc_off_t offset, ltc_off_t pos) {

	d->biphase_tics[d->biphase_tic] = d->snd_to_biphase_period;
	d->biphase_tic = (d->biphase_tic + 1) % LTC_FRAME_BIT_COUNT;
	if (d->snd_to_biphase_cnt <= 2 * d->snd_to_biphase_period) {
		pos -= (d->snd_to_biphase_period - d->snd_to_biphase_cnt);
	}

	if (d->snd_to_biphase_state == d->biphase_prev) {
		d->biphase_state = 1;
		parse_ltc(d, 0, offset, pos);
	} else {
		d->biphase_state = 1 - d->biphase_state;
		if (d->biphase_state == 1) {
			parse_ltc(d, 1, offset, pos);
		}
	}
	d->biphase_prev = d->snd_to_biphase_state;
}

void decode_ltc(LTCDecoder *d, ltcsnd_sample_t *sound, size_t size, ltc_off_t posinfo) {
	size_t i;

	for (i = 0 ; i < size ; i++) {
		ltcsnd_sample_t max_threshold, min_threshold;

		/* track minimum and maximum values */
		d->snd_to_biphase_min = SAMPLE_CENTER - (((SAMPLE_CENTER - d->snd_to_biphase_min) * 15) / 16);
		d->snd_to_biphase_max = SAMPLE_CENTER + (((d->snd_to_biphase_max - SAMPLE_CENTER) * 15) / 16);

		if (sound[i] < d->snd_to_biphase_min)
			d->snd_to_biphase_min = sound[i];
		if (sound[i] > d->snd_to_biphase_max)
			d->snd_to_biphase_max = sound[i];

		/* set the thresholds for hi/lo state tracking */
		min_threshold = SAMPLE_CENTER - (((SAMPLE_CENTER - d->snd_to_biphase_min) * 8) / 16);
		max_threshold = SAMPLE_CENTER + (((d->snd_to_biphase_max - SAMPLE_CENTER) * 8) / 16);

		if ( /* Check for a biphase state change */
			   (  d->snd_to_biphase_state && (sound[i] > max_threshold) )
			|| ( !d->snd_to_biphase_state && (sound[i] < min_threshold) )
		   ) {

			/* If the sample count has risen above the biphase length limit */
			if (d->snd_to_biphase_cnt > d->snd_to_biphase_lmt) {
				/* single state change within a biphase priod. decode to a 0 */
				biphase_decode2(d, i, posinfo);
				biphase_decode2(d, i, posinfo);

			} else {
				/* "short" state change covering half a period
				 * together with the next or previous state change decode to a 1
				 */
				d->snd_to_biphase_cnt *= 2;
				biphase_decode2(d, i, posinfo);

			}

			if (d->snd_to_biphase_cnt > (d->snd_to_biphase_period * 4)) {
				/* "long" silence in between
				 * -> reset parser, don't use it for phase-tracking
				 */
				d->bit_cnt = 0;
			} else  {
				/* track speed variations
				 * As this is only executed at a state change,
				 * d->snd_to_biphase_cnt is an accurate representation of the current period length.
				 */
				d->snd_to_biphase_period = (d->snd_to_biphase_period * 3.0 + d->snd_to_biphase_cnt) / 4.0;

				/* This limit specifies when a state-change is
				 * considered biphase-clock or 2*biphase-clock.
				 * The relation with period has been determined
				 * empirically through trial-and-error */
				d->snd_to_biphase_lmt = (d->snd_to_biphase_period * 3) / 4;
			}

			d->snd_to_biphase_cnt = 0;
			d->snd_to_biphase_state = !d->snd_to_biphase_state;
		}
		d->snd_to_biphase_cnt++;
	}
}