2 libltc - en+decode linear timecode
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>
8 Binary constant generator macro for endianess conversion
9 by Tom Torfs - donated to the public domain
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.
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.
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/>.
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
30 #define HEX__(n) 0x##n##LU
33 * 8-bit conversion function
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)
44 /** for upto 8-bit binary constants */
45 #define B8(d) ((unsigned char)B8__(HEX__(d)))
47 /** for upto 16-bit binary constants, MSB first */
48 #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))
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
54 #define HEX__(n) 0x##n##LU
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)
67 /** for upto 8-bit binary constants */
68 #define B8(d) ((unsigned char)B8__(HEX__(d)))
70 /** for upto 16-bit binary constants, MSB first */
71 #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))
75 * B16(10101010,01010101) = 43605
83 #include "ltc/decoder.h"
85 #define DEBUG_DUMP(msg, f) \
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'); \
104 static double calc_volume_db(LTCDecoder *d) {
105 if (d->snd_to_biphase_max <= d->snd_to_biphase_min)
107 return (20.0 * log10((d->snd_to_biphase_max - d->snd_to_biphase_min) / 255.0));
110 static void parse_ltc(LTCDecoder *d, unsigned char bit, ltc_off_t offset, ltc_off_t posinfo) {
111 int bit_num, bit_set, byte_num;
113 if (d->bit_cnt == 0) {
114 memset(&d->ltc_frame, 0, sizeof(LTCFrame));
116 if (d->frame_start_prev < 0) {
117 d->frame_start_off = posinfo - d->snd_to_biphase_period;
119 d->frame_start_off = d->frame_start_prev;
122 d->frame_start_prev = offset + posinfo;
124 if (d->bit_cnt >= LTC_FRAME_BIT_COUNT) {
125 /* shift bits backwards */
127 const int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;
129 for (k=0; k< byte_num_max; k++) {
130 const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
131 unsigned char bo = 0;
132 bo |= (bi & B8(10000000) ) ? B8(01000000) : 0;
133 bo |= (bi & B8(01000000) ) ? B8(00100000) : 0;
134 bo |= (bi & B8(00100000) ) ? B8(00010000) : 0;
135 bo |= (bi & B8(00010000) ) ? B8(00001000) : 0;
136 bo |= (bi & B8(00001000) ) ? B8(00000100) : 0;
137 bo |= (bi & B8(00000100) ) ? B8(00000010) : 0;
138 bo |= (bi & B8(00000010) ) ? B8(00000001) : 0;
139 if (k+1 < byte_num_max) {
140 bo |= ( (((unsigned char*)&d->ltc_frame)[k+1]) & B8(00000001) ) ? B8(10000000): B8(00000000);
142 ((unsigned char*)&d->ltc_frame)[k] = bo;
145 d->frame_start_off += ceil(d->snd_to_biphase_period);
149 d->decoder_sync_word <<= 1;
152 d->decoder_sync_word |= B16(00000000,00000001);
154 if (d->bit_cnt < LTC_FRAME_BIT_COUNT) {
155 // Isolating the lowest three bits: the location of this bit in the current byte
156 bit_num = (d->bit_cnt & B8(00000111));
157 // Using the bit number to define which of the eight bits to set
158 bit_set = (B8(00000001) << bit_num);
159 // Isolating the higher bits: the number of the byte/char the target bit is contained in
160 byte_num = d->bit_cnt >> 3;
162 (((unsigned char*)&d->ltc_frame)[byte_num]) |= bit_set;
168 if (d->decoder_sync_word == B16(00111111,11111101) /*LTC Sync Word 0x3ffd*/) {
169 if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
172 memcpy( &d->queue[d->queue_write_off].ltc,
176 for(bc = 0; bc < LTC_FRAME_BIT_COUNT; ++bc) {
177 const int btc = (d->biphase_tic + bc ) % LTC_FRAME_BIT_COUNT;
178 d->queue[d->queue_write_off].biphase_tics[bc] = d->biphase_tics[btc];
181 d->queue[d->queue_write_off].off_start = d->frame_start_off;
182 d->queue[d->queue_write_off].off_end = posinfo + (ltc_off_t) offset - 1LL;
183 d->queue[d->queue_write_off].reverse = 0;
184 d->queue[d->queue_write_off].volume = calc_volume_db(d);
185 d->queue[d->queue_write_off].sample_min = d->snd_to_biphase_min;
186 d->queue[d->queue_write_off].sample_max = d->snd_to_biphase_max;
188 d->queue_write_off++;
190 if (d->queue_write_off == d->queue_len)
191 d->queue_write_off = 0;
196 if (d->decoder_sync_word == B16(10111111,11111100) /* reverse sync-word*/) {
197 if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
201 int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;
204 for (k=0; k< byte_num_max; k++) {
205 const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
206 unsigned char bo = 0;
207 bo |= (bi & B8(10000000) ) ? B8(00000001) : 0;
208 bo |= (bi & B8(01000000) ) ? B8(00000010) : 0;
209 bo |= (bi & B8(00100000) ) ? B8(00000100) : 0;
210 bo |= (bi & B8(00010000) ) ? B8(00001000) : 0;
211 bo |= (bi & B8(00001000) ) ? B8(00010000) : 0;
212 bo |= (bi & B8(00000100) ) ? B8(00100000) : 0;
213 bo |= (bi & B8(00000010) ) ? B8(01000000) : 0;
214 bo |= (bi & B8(00000001) ) ? B8(10000000) : 0;
215 ((unsigned char*)&d->ltc_frame)[k] = bo;
219 byte_num_max-=2; // skip sync-word
220 for (k=0; k< (byte_num_max)/2; k++) {
221 const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
222 ((unsigned char*)&d->ltc_frame)[k] = ((unsigned char*)&d->ltc_frame)[byte_num_max-1-k];
223 ((unsigned char*)&d->ltc_frame)[byte_num_max-1-k] = bi;
226 memcpy( &d->queue[d->queue_write_off].ltc,
230 for(bc = 0; bc < LTC_FRAME_BIT_COUNT; ++bc) {
231 const int btc = (d->biphase_tic + bc ) % LTC_FRAME_BIT_COUNT;
232 d->queue[d->queue_write_off].biphase_tics[bc] = d->biphase_tics[btc];
235 d->queue[d->queue_write_off].off_start = d->frame_start_off - 16 * d->snd_to_biphase_period;
236 d->queue[d->queue_write_off].off_end = posinfo + (ltc_off_t) offset - 1LL - 16 * d->snd_to_biphase_period;
237 d->queue[d->queue_write_off].reverse = (LTC_FRAME_BIT_COUNT >> 3) * 8 * d->snd_to_biphase_period;
238 d->queue[d->queue_write_off].volume = calc_volume_db(d);
239 d->queue[d->queue_write_off].sample_min = d->snd_to_biphase_min;
240 d->queue[d->queue_write_off].sample_max = d->snd_to_biphase_max;
242 d->queue_write_off++;
244 if (d->queue_write_off == d->queue_len)
245 d->queue_write_off = 0;
251 static inline void biphase_decode2(LTCDecoder *d, ltc_off_t offset, ltc_off_t pos) {
253 d->biphase_tics[d->biphase_tic] = d->snd_to_biphase_period;
254 d->biphase_tic = (d->biphase_tic + 1) % LTC_FRAME_BIT_COUNT;
255 if (d->snd_to_biphase_cnt <= 2 * d->snd_to_biphase_period) {
256 pos -= (d->snd_to_biphase_period - d->snd_to_biphase_cnt);
259 if (d->snd_to_biphase_state == d->biphase_prev) {
260 d->biphase_state = 1;
261 parse_ltc(d, 0, offset, pos);
263 d->biphase_state = 1 - d->biphase_state;
264 if (d->biphase_state == 1) {
265 parse_ltc(d, 1, offset, pos);
268 d->biphase_prev = d->snd_to_biphase_state;
271 void decode_ltc(LTCDecoder *d, ltcsnd_sample_t *sound, size_t size, ltc_off_t posinfo) {
274 for (i = 0 ; i < size ; i++) {
275 ltcsnd_sample_t max_threshold, min_threshold;
277 /* track minimum and maximum values */
278 d->snd_to_biphase_min = SAMPLE_CENTER - (((SAMPLE_CENTER - d->snd_to_biphase_min) * 15) / 16);
279 d->snd_to_biphase_max = SAMPLE_CENTER + (((d->snd_to_biphase_max - SAMPLE_CENTER) * 15) / 16);
281 if (sound[i] < d->snd_to_biphase_min)
282 d->snd_to_biphase_min = sound[i];
283 if (sound[i] > d->snd_to_biphase_max)
284 d->snd_to_biphase_max = sound[i];
286 /* set the thresholds for hi/lo state tracking */
287 min_threshold = SAMPLE_CENTER - (((SAMPLE_CENTER - d->snd_to_biphase_min) * 8) / 16);
288 max_threshold = SAMPLE_CENTER + (((d->snd_to_biphase_max - SAMPLE_CENTER) * 8) / 16);
290 if ( /* Check for a biphase state change */
291 ( d->snd_to_biphase_state && (sound[i] > max_threshold) )
292 || ( !d->snd_to_biphase_state && (sound[i] < min_threshold) )
295 /* If the sample count has risen above the biphase length limit */
296 if (d->snd_to_biphase_cnt > d->snd_to_biphase_lmt) {
297 /* single state change within a biphase priod. decode to a 0 */
298 biphase_decode2(d, i, posinfo);
299 biphase_decode2(d, i, posinfo);
302 /* "short" state change covering half a period
303 * together with the next or previous state change decode to a 1
305 d->snd_to_biphase_cnt *= 2;
306 biphase_decode2(d, i, posinfo);
310 if (d->snd_to_biphase_cnt > (d->snd_to_biphase_period * 4)) {
311 /* "long" silence in between
312 * -> reset parser, don't use it for phase-tracking
316 /* track speed variations
317 * As this is only executed at a state change,
318 * d->snd_to_biphase_cnt is an accurate representation of the current period length.
320 d->snd_to_biphase_period = (d->snd_to_biphase_period * 3.0 + d->snd_to_biphase_cnt) / 4.0;
322 /* This limit specifies when a state-change is
323 * considered biphase-clock or 2*biphase-clock.
324 * The relation with period has been determined
325 * empirically through trial-and-error */
326 d->snd_to_biphase_lmt = (d->snd_to_biphase_period * 3) / 4;
329 d->snd_to_biphase_cnt = 0;
330 d->snd_to_biphase_state = !d->snd_to_biphase_state;
332 d->snd_to_biphase_cnt++;