2 libltc - en+decode linear timecode
4 Copyright (C) 2006-2015 Robin Gareus <robin@gareus.org>
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.
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.
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/>.
27 #include "ltc/decoder.h"
28 #include "ltc/encoder.h"
30 /* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 LTCDecoder* ltc_decoder_create(int apv, int queue_len) {
35 LTCDecoder* d = (LTCDecoder*) calloc(1, sizeof(LTCDecoder));
38 d->queue_len = queue_len;
39 d->queue = (LTCFrameExt*) calloc(d->queue_len, sizeof(LTCFrameExt));
45 d->snd_to_biphase_period = apv / 80;
46 d->snd_to_biphase_lmt = (d->snd_to_biphase_period * 3) / 4;
48 d->snd_to_biphase_min = SAMPLE_CENTER;
49 d->snd_to_biphase_max = SAMPLE_CENTER;
50 d->frame_start_prev = -1;
56 int ltc_decoder_free(LTCDecoder *d) {
58 if (d->queue) free(d->queue);
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);
68 #define LTC_CONVERSION_BUF_SIZE 1024
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) { \
76 int c = size - copyStart; \
77 c = (c > LTC_CONVERSION_BUF_SIZE) ? LTC_CONVERSION_BUF_SIZE : c; \
78 for (i=0; i < c; i++) { \
81 decode_ltc(d, tmp, c, posinfo + (ltc_off_t)copyStart); \
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))
92 #undef LTC_CONVERSION_BUF_SIZE
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));
99 if (d->queue_read_off == d->queue_len)
100 d->queue_read_off = 0;
106 void ltc_decoder_queue_flush(LTCDecoder* d) {
107 while (d->queue_read_off != d->queue_write_off) {
109 if (d->queue_read_off == d->queue_len)
110 d->queue_read_off = 0;
114 int ltc_decoder_queue_length(LTCDecoder* d) {
115 return (d->queue_write_off - d->queue_read_off + d->queue_len) % d->queue_len;
118 /* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 LTCEncoder* ltc_encoder_create(double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags) {
126 LTCEncoder* e = (LTCEncoder*) calloc(1, sizeof(LTCEncoder));
130 /*-3.0 dBFS default */
134 e->bufsize = 1 + ceil(sample_rate / fps);
135 e->buf = (ltcsnd_sample_t*) calloc(e->bufsize, sizeof(ltcsnd_sample_t));
141 ltc_frame_reset(&e->f);
142 ltc_encoder_reinit(e, sample_rate, fps, standard, flags);
146 void ltc_encoder_free(LTCEncoder *e) {
148 if (e->buf) free(e->buf);
152 int ltc_encoder_reinit(LTCEncoder *e, double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags) {
156 size_t bufsize = 1 + ceil(sample_rate / fps);
157 if (bufsize > e->bufsize) {
163 e->sample_rate = sample_rate;
164 ltc_encoder_set_filter(e, 40.0);
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;
172 if (flags & LTC_BGF_DONT_TOUCH) {
174 if (flags<C_TC_CLOCK) {
175 e->f.binary_group_flag_bit1 = 1;
177 e->f.binary_group_flag_bit1 = 0;
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<C_USE_DATE)?1:0; // BGF2
185 e->f.binary_group_flag_bit0 = 0;
186 e->f.binary_group_flag_bit2 = (flags<C_USE_DATE)?1:0;
190 if ((flags<C_NO_PARITY) == 0) {
191 ltc_frame_set_parity(&e->f, standard);
194 if ((int)rint(fps * 100.0) == 2997)
201 void ltc_encoder_reset(LTCEncoder *e) {
203 e->sample_remainder = 0.5;
207 int ltc_encoder_set_volume(LTCEncoder *e, double dBFS) {
210 double pp = rint(127.0 * pow(10, dBFS/20.0));
211 if (pp < 1 || pp > 127)
213 ltcsnd_sample_t diff = ((ltcsnd_sample_t) pp)&0x7f;
214 e->enc_lo = SAMPLE_CENTER - diff;
215 e->enc_hi = SAMPLE_CENTER + diff;
219 void ltc_encoder_set_filter(LTCEncoder *e, double rise_time) {
221 * LTC signal should have a rise time of 40 us +/- 10 us.
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.
231 e->filter_const = 1.0 - exp( -1.0 / (e->sample_rate * rise_time / 2000000.0 / exp(1.0)) );
234 int ltc_encoder_set_bufsize(LTCEncoder *e, double sample_rate, double fps) {
237 e->bufsize = 1 + ceil(sample_rate / fps);
238 e->buf = (ltcsnd_sample_t*) calloc(e->bufsize, sizeof(ltcsnd_sample_t));
245 int ltc_encoder_encode_byte(LTCEncoder *e, int byte, double speed) {
246 return encode_byte(e, byte, speed);
249 void ltc_encoder_encode_frame(LTCEncoder *e) {
251 for (byte = 0 ; byte < 10 ; byte++) {
252 encode_byte(e, byte, 1.0);
256 void ltc_encoder_get_timecode(LTCEncoder *e, SMPTETimecode *t) {
257 ltc_frame_to_time(t, &e->f, e->flags);
260 void ltc_encoder_set_timecode(LTCEncoder *e, SMPTETimecode *t) {
261 ltc_time_to_frame(&e->f, t, e->standard, e->flags);
264 void ltc_encoder_get_frame(LTCEncoder *e, LTCFrame *f) {
265 memcpy(f, &e->f, sizeof(LTCFrame));
268 void ltc_encoder_set_frame(LTCEncoder *e, LTCFrame *f) {
269 memcpy(&e->f, f, sizeof(LTCFrame));
272 int ltc_encoder_inc_timecode(LTCEncoder *e) {
273 return ltc_frame_increment (&e->f, rint(e->fps), e->standard, e->flags);
276 int ltc_encoder_dec_timecode(LTCEncoder *e) {
277 return ltc_frame_decrement (&e->f, rint(e->fps), e->standard, e->flags);
280 size_t ltc_encoder_get_buffersize(LTCEncoder *e) {
284 void ltc_encoder_buffer_flush(LTCEncoder *e) {
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;
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) );
301 void ltc_frame_set_parity(LTCFrame *frame, enum LTC_TV_STANDARD standard) {
305 if (standard != LTC_TV_625_50) { /* 30fps, 24fps */
306 frame->biphase_mark_phase_correction = 0;
308 frame->binary_group_flag_bit2 = 0;
311 for (i=0; i < LTC_FRAME_BIT_COUNT / 8; ++i){
312 p = p ^ (((unsigned char*)frame)[i]);
314 #define PRY(BIT) ((p>>BIT)&1)
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);
320 frame->binary_group_flag_bit2 =
321 PRY(0)^PRY(1)^PRY(2)^PRY(3)^PRY(4)^PRY(5)^PRY(6)^PRY(7);
325 ltc_off_t ltc_frame_alignment(double samples_per_frame, enum LTC_TV_STANDARD standard) {
328 return rint(samples_per_frame * 4.0 / 525.0);
330 return rint(samples_per_frame * 1.0 / 625.0);