Merge branch 'master' into cairocanvas
[ardour.git] / libs / libltc / decoder.c
1 /*
2    libltc - en+decode linear timecode
3
4    Copyright (C) 2005 Maarten de Boer <mdeboer@iua.upf.es>
5    Copyright (C) 2006-2012 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 "ltc/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 static double calc_volume_db(LTCDecoder *d) {
105         if (d->snd_to_biphase_max <= d->snd_to_biphase_min)
106                 return -INFINITY;
107         return (20.0 * log10((d->snd_to_biphase_max - d->snd_to_biphase_min) / 255.0));
108 }
109
110 static void parse_ltc(LTCDecoder *d, unsigned char bit, int offset, ltc_off_t posinfo) {
111         int bit_num, bit_set, byte_num;
112
113         if (d->bit_cnt == 0) {
114                 memset(&d->ltc_frame, 0, sizeof(LTCFrame));
115
116                 if (d->frame_start_prev < 0) {
117                         d->frame_start_off = posinfo - d->snd_to_biphase_period;
118                 } else {
119                         d->frame_start_off = d->frame_start_prev;
120                 }
121         }
122         d->frame_start_prev = offset + posinfo;
123
124         if (d->bit_cnt >= LTC_FRAME_BIT_COUNT) {
125                 /* shift bits backwards */
126                 int k = 0;
127                 const int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;
128
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);
141                         }
142                         ((unsigned char*)&d->ltc_frame)[k] = bo;
143                 }
144
145                 d->frame_start_off += ceil(d->snd_to_biphase_period);
146                 d->bit_cnt--;
147         }
148
149         d->decoder_sync_word <<= 1;
150         if (bit) {
151
152                 d->decoder_sync_word |= B16(00000000,00000001);
153
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;
161
162                         (((unsigned char*)&d->ltc_frame)[byte_num]) |= bit_set;
163                 }
164
165         }
166         d->bit_cnt++;
167
168         if (d->decoder_sync_word == B16(00111111,11111101) /*LTC Sync Word 0x3ffd*/) {
169                 if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
170                         int bc;
171
172                         memcpy( &d->queue[d->queue_write_off].ltc,
173                                 &d->ltc_frame,
174                                 sizeof(LTCFrame));
175
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];
179                         }
180
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;
187
188                         d->queue_write_off++;
189
190                         if (d->queue_write_off == d->queue_len)
191                                 d->queue_write_off = 0;
192                 }
193                 d->bit_cnt = 0;
194         }
195
196         if (d->decoder_sync_word == B16(10111111,11111100) /* reverse sync-word*/) {
197                 if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
198                         /* reverse frame */
199                         int bc;
200                         int k = 0;
201                         int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;
202
203                         /* swap bits */
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;
216                         }
217
218                         /* swap bytes */
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;
224                         }
225
226                         memcpy( &d->queue[d->queue_write_off].ltc,
227                                 &d->ltc_frame,
228                                 sizeof(LTCFrame));
229
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];
233                         }
234
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;
241
242                         d->queue_write_off++;
243
244                         if (d->queue_write_off == d->queue_len)
245                                 d->queue_write_off = 0;
246                 }
247                 d->bit_cnt = 0;
248         }
249 }
250
251 static inline void biphase_decode2(LTCDecoder *d, int offset, ltc_off_t pos) {
252
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);
257         }
258
259         if (d->snd_to_biphase_state == d->biphase_prev) {
260                 d->biphase_state = 1;
261                 parse_ltc(d, 0, offset, pos);
262         } else {
263                 d->biphase_state = 1 - d->biphase_state;
264                 if (d->biphase_state == 1) {
265                         parse_ltc(d, 1, offset, pos);
266                 }
267         }
268         d->biphase_prev = d->snd_to_biphase_state;
269 }
270
271 void decode_ltc(LTCDecoder *d, ltcsnd_sample_t *sound, size_t size, ltc_off_t posinfo) {
272         size_t i;
273
274         for (i = 0 ; i < size ; i++) {
275                 ltcsnd_sample_t max_threshold, min_threshold;
276
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);
280
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];
285
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);
289
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) )
293                    ) {
294
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);
300
301                         } else {
302                                 /* "short" state change covering half a period
303                                  * together with the next or previous state change decode to a 1
304                                  */
305                                 d->snd_to_biphase_cnt *= 2;
306                                 biphase_decode2(d, i, posinfo);
307
308                         }
309
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
313                                  */
314                                 d->bit_cnt = 0;
315                         } else  {
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.
319                                  */
320                                 d->snd_to_biphase_period = (d->snd_to_biphase_period * 3.0 + d->snd_to_biphase_cnt) / 4.0;
321
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;
327                         }
328
329                         d->snd_to_biphase_cnt = 0;
330                         d->snd_to_biphase_state = !d->snd_to_biphase_state;
331                 }
332                 d->snd_to_biphase_cnt++;
333         }
334 }