align stem-export (raw track outputs (with and w/p processing)
[ardour.git] / libs / libltc / ltc.c
1 /*
2    libltc - en+decode linear timecode
3
4    Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation, either version 3 of the
9    License, or (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library.
18    If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25
26 #include "ltc/ltc.h"
27 #include "ltc/decoder.h"
28 #include "ltc/encoder.h"
29
30 /* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31  * Decoder
32  */
33
34 LTCDecoder* ltc_decoder_create(int apv, int queue_len) {
35         LTCDecoder* d = (LTCDecoder*) calloc(1, sizeof(LTCDecoder));
36         if (!d) return NULL;
37
38         d->queue_len = queue_len;
39         d->queue = (LTCFrameExt*) calloc(d->queue_len, sizeof(LTCFrameExt));
40         if (!d->queue) {
41                 free(d);
42                 return NULL;
43         }
44         d->biphase_state = 1;
45         d->snd_to_biphase_period = apv / 80;
46         d->snd_to_biphase_lmt = (d->snd_to_biphase_period * 3) / 4;
47
48         d->snd_to_biphase_min = SAMPLE_CENTER;
49         d->snd_to_biphase_max = SAMPLE_CENTER;
50         d->frame_start_prev = -1;
51         d->biphase_tic = 0;
52
53         return d;
54 }
55
56 int ltc_decoder_free(LTCDecoder *d) {
57         if (!d) return 1;
58         if (d->queue) free(d->queue);
59         free(d);
60
61         return 0;
62 }
63
64 void ltc_decoder_write(LTCDecoder *d, ltcsnd_sample_t *buf, size_t size, ltc_off_t posinfo) {
65         decode_ltc(d, buf, size, posinfo);
66 }
67
68 #define LTC_CONVERSION_BUF_SIZE 1024
69
70 #define LTCWRITE_TEMPLATE(FN, FORMAT, CONV) \
71 void ltc_decoder_write_ ## FN (LTCDecoder *d, FORMAT *buf, size_t size, ltc_off_t posinfo) { \
72         ltcsnd_sample_t tmp[LTC_CONVERSION_BUF_SIZE]; \
73         size_t copyStart = 0; \
74         while (copyStart < size) { \
75                 int i; \
76                 int c = size - copyStart; \
77                 c = (c > LTC_CONVERSION_BUF_SIZE) ? LTC_CONVERSION_BUF_SIZE : c; \
78                 for (i=0; i < c; i++) { \
79                         tmp[i] = CONV; \
80                 } \
81                 decode_ltc(d, tmp, c, posinfo + (ltc_off_t)copyStart); \
82                 copyStart += c; \
83         } \
84 }
85
86 LTCWRITE_TEMPLATE(float, float, 128 + (buf[copyStart+i] * 127.0))
87 /* this relies on the compiler to use an arithemtic right-shift for signed values */
88 LTCWRITE_TEMPLATE(s16, short, 128 + (buf[copyStart+i] >> 8))
89 /* this relies on the compiler to use a logical right-shift for unsigned values */
90 LTCWRITE_TEMPLATE(u16, unsigned short, (buf[copyStart+i] >> 8))
91
92 #undef LTC_CONVERSION_BUF_SIZE
93
94 int ltc_decoder_read(LTCDecoder* d, LTCFrameExt* frame) {
95         if (!frame) return -1;
96         if (d->queue_read_off != d->queue_write_off) {
97                 memcpy(frame, &d->queue[d->queue_read_off], sizeof(LTCFrameExt));
98                 d->queue_read_off++;
99                 if (d->queue_read_off == d->queue_len)
100                         d->queue_read_off = 0;
101                 return 1;
102         }
103         return 0;
104 }
105
106 void ltc_decoder_queue_flush(LTCDecoder* d) {
107         while (d->queue_read_off != d->queue_write_off) {
108                 d->queue_read_off++;
109                 if (d->queue_read_off == d->queue_len)
110                         d->queue_read_off = 0;
111         }
112 }
113
114 int ltc_decoder_queue_length(LTCDecoder* d) {
115         return (d->queue_write_off - d->queue_read_off + d->queue_len) % d->queue_len;
116 }
117
118 /* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119  * Encoder
120  */
121
122 LTCEncoder* ltc_encoder_create(double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags) {
123         if (sample_rate < 1)
124                 return NULL;
125
126         LTCEncoder* e = (LTCEncoder*) calloc(1, sizeof(LTCEncoder));
127         if (!e)
128                 return NULL;
129
130         /*-3.0 dBFS default */
131         e->enc_lo = 38;
132         e->enc_hi = 218;
133
134         e->bufsize = 1 + ceil(sample_rate / fps);
135         e->buf = (ltcsnd_sample_t*) calloc(e->bufsize, sizeof(ltcsnd_sample_t));
136         if (!e->buf) {
137                 free(e);
138                 return NULL;
139         }
140
141         ltc_frame_reset(&e->f);
142         ltc_encoder_reinit(e, sample_rate, fps, standard, flags);
143         return e;
144 }
145
146 void ltc_encoder_free(LTCEncoder *e) {
147         if (!e) return;
148         if (e->buf) free(e->buf);
149         free(e);
150 }
151
152 int ltc_encoder_reinit(LTCEncoder *e, double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags) {
153         if (sample_rate < 1)
154                 return -1;
155
156         size_t bufsize = 1 + ceil(sample_rate / fps);
157         if (bufsize > e->bufsize) {
158                 return -1;
159         }
160
161         e->state = 0;
162         e->offset = 0;
163         e->sample_rate = sample_rate;
164         ltc_encoder_set_filter(e, 40.0);
165         e->fps = fps;
166         e->flags = flags;
167         e->standard = standard;
168         e->samples_per_clock = sample_rate / (fps * 80.0);
169         e->samples_per_clock_2 = e->samples_per_clock / 2.0;
170         e->sample_remainder = 0.5;
171
172         if (flags & LTC_BGF_DONT_TOUCH) {
173                 e->f.col_frame = 0;
174                 if (flags&LTC_TC_CLOCK) {
175                         e->f.binary_group_flag_bit1 = 1;
176                 } else {
177                         e->f.binary_group_flag_bit1 = 0;
178                 }
179                 switch (standard) {
180                         case LTC_TV_625_50: /* 25 fps mode */
181                                 e->f.biphase_mark_phase_correction = 0; // BGF0
182                                 e->f.binary_group_flag_bit0 = (flags&LTC_USE_DATE)?1:0; // BGF2
183                                 break;
184                         default:
185                                 e->f.binary_group_flag_bit0 = 0;
186                                 e->f.binary_group_flag_bit2 = (flags&LTC_USE_DATE)?1:0;
187                                 break;
188                 }
189         }
190         if ((flags&LTC_NO_PARITY) == 0) {
191                 ltc_frame_set_parity(&e->f, standard);
192         }
193
194         if (rint(fps*100) == 2997)
195                 e->f.dfbit = 1;
196         else
197                 e->f.dfbit = 0;
198         return 0;
199 }
200
201 void ltc_encoder_reset(LTCEncoder *e) {
202         e->state = 0;
203         e->sample_remainder = 0.5;
204         e->offset = 0;
205 }
206
207 int ltc_encoder_set_volume(LTCEncoder *e, double dBFS) {
208         if (dBFS > 0)
209                 return -1;
210         double pp = rint(127.0 * pow(10, dBFS/20.0));
211         if (pp < 1 || pp > 127)
212                 return -1;
213         ltcsnd_sample_t diff = ((ltcsnd_sample_t) pp)&0x7f;
214         e->enc_lo = SAMPLE_CENTER - diff;
215         e->enc_hi = SAMPLE_CENTER + diff;
216         return 0;
217 }
218
219 void ltc_encoder_set_filter(LTCEncoder *e, double rise_time) {
220         /* low-pass-filter
221          * LTC signal should have a rise time of 40 us +/- 10 us.
222          *
223          * rise-time means from <10% to >90% of the signal.
224          * in each call to addvalues() we start at 50% (SAMPLE_CENTER), so
225          * here we need half-of it.
226          */
227
228         if (rise_time <= 0)
229                 e->filter_const = 0;
230         else
231                 e->filter_const = 1.0 - exp( -1.0 / (e->sample_rate * rise_time / 2000000.0 / exp(1.0)) );
232 }
233
234 int ltc_encoder_set_bufsize(LTCEncoder *e, double sample_rate, double fps) {
235         free (e->buf);
236         e->offset = 0;
237         e->bufsize = 1 + ceil(sample_rate / fps);
238         e->buf = (ltcsnd_sample_t*) calloc(e->bufsize, sizeof(ltcsnd_sample_t));
239         if (!e->buf) {
240                 return -1;
241         }
242         return 0;
243 }
244
245 int ltc_encoder_encode_byte(LTCEncoder *e, int byte, double speed) {
246         return encode_byte(e, byte, speed);
247 }
248
249 void ltc_encoder_encode_frame(LTCEncoder *e) {
250         int byte;
251         for (byte = 0 ; byte < 10 ; byte++) {
252                 encode_byte(e, byte, 1.0);
253         }
254 }
255
256 void ltc_encoder_get_timecode(LTCEncoder *e, SMPTETimecode *t) {
257         ltc_frame_to_time(t, &e->f, e->flags);
258 }
259
260 void ltc_encoder_set_timecode(LTCEncoder *e, SMPTETimecode *t) {
261         ltc_time_to_frame(&e->f, t, e->standard, e->flags);
262 }
263
264 void ltc_encoder_get_frame(LTCEncoder *e, LTCFrame *f) {
265         memcpy(f, &e->f, sizeof(LTCFrame));
266 }
267
268 void ltc_encoder_set_frame(LTCEncoder *e, LTCFrame *f) {
269         memcpy(&e->f, f, sizeof(LTCFrame));
270 }
271
272 int ltc_encoder_inc_timecode(LTCEncoder *e) {
273         return ltc_frame_increment (&e->f, rint(e->fps), e->standard, e->flags);
274 }
275
276 int ltc_encoder_dec_timecode(LTCEncoder *e) {
277         return ltc_frame_decrement (&e->f, rint(e->fps), e->standard, e->flags);
278 }
279
280 size_t ltc_encoder_get_buffersize(LTCEncoder *e) {
281         return(e->bufsize);
282 }
283
284 void ltc_encoder_buffer_flush(LTCEncoder *e) {
285         e->offset = 0;
286 }
287
288 ltcsnd_sample_t *ltc_encoder_get_bufptr(LTCEncoder *e, int *size, int flush) {
289         if (size) *size = e->offset;
290         if (flush) e->offset = 0;
291         return e->buf;
292 }
293
294 int ltc_encoder_get_buffer(LTCEncoder *e, ltcsnd_sample_t *buf) {
295         const int len = e->offset;
296         memcpy(buf, e->buf, len * sizeof(ltcsnd_sample_t) );
297         e->offset = 0;
298         return(len);
299 }
300
301 void ltc_frame_set_parity(LTCFrame *frame, enum LTC_TV_STANDARD standard) {
302         int i;
303         unsigned char p = 0;
304
305         if (standard != LTC_TV_625_50) { /* 30fps, 24fps */
306                 frame->biphase_mark_phase_correction = 0;
307         } else { /* 25fps */
308                 frame->binary_group_flag_bit2 = 0;
309         }
310
311         for (i=0; i < LTC_FRAME_BIT_COUNT / 8; ++i){
312                 p = p ^ (((unsigned char*)frame)[i]);
313         }
314 #define PRY(BIT) ((p>>BIT)&1)
315
316         if (standard != LTC_TV_625_50) { /* 30fps, 24fps */
317                 frame->biphase_mark_phase_correction =
318                         PRY(0)^PRY(1)^PRY(2)^PRY(3)^PRY(4)^PRY(5)^PRY(6)^PRY(7);
319         } else { /* 25fps */
320                 frame->binary_group_flag_bit2 =
321                         PRY(0)^PRY(1)^PRY(2)^PRY(3)^PRY(4)^PRY(5)^PRY(6)^PRY(7);
322         }
323 }
324
325 ltc_off_t ltc_frame_alignment(double samples_per_frame, enum LTC_TV_STANDARD standard) {
326         switch (standard) {
327                 case LTC_TV_525_60:
328                         return rint(samples_per_frame * 4.0 / 525.0);
329                 case LTC_TV_625_50:
330                         return rint(samples_per_frame * 1.0 / 625.0);
331                 default:
332                         return 0;
333         }
334 }