2 libltc - en+decode linear timecode
4 Copyright (C) 2006-2012 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 LTCWRITE_TEMPLATE(FN, FORMAT, CONV) \
69 void ltc_decoder_write_ ## FN (LTCDecoder *d, FORMAT *buf, size_t size, ltc_off_t posinfo) { \
70 ltcsnd_sample_t tmp[1024]; \
71 size_t remain = size; \
72 while (remain > 0) { \
73 int c = (remain > 1024) ? 1024 : remain; \
75 for (i=0; i<c; i++) { \
78 decode_ltc(d, tmp, c, posinfo + (ltc_off_t)c); \
83 LTCWRITE_TEMPLATE(float, float, 128 + (buf[i] * 127.0))
84 LTCWRITE_TEMPLATE(s16, short, 128 + (buf[i] >> 8))
85 LTCWRITE_TEMPLATE(u16, short, (buf[i] >> 8))
87 int ltc_decoder_read(LTCDecoder* d, LTCFrameExt* frame) {
88 if (!frame) return -1;
89 if (d->queue_read_off != d->queue_write_off) {
90 memcpy(frame, &d->queue[d->queue_read_off], sizeof(LTCFrameExt));
92 if (d->queue_read_off == d->queue_len)
93 d->queue_read_off = 0;
99 void ltc_decoder_queue_flush(LTCDecoder* d) {
100 while (d->queue_read_off != d->queue_write_off) {
102 if (d->queue_read_off == d->queue_len)
103 d->queue_read_off = 0;
107 int ltc_decoder_queue_length(LTCDecoder* d) {
108 return (d->queue_write_off - d->queue_read_off + d->queue_len) % d->queue_len;
111 /* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 LTCEncoder* ltc_encoder_create(double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags) {
119 LTCEncoder* e = (LTCEncoder*) calloc(1, sizeof(LTCEncoder));
123 /*-3.0 dBFS default */
127 e->bufsize = 1 + ceil(sample_rate / fps);
128 e->buf = (ltcsnd_sample_t*) calloc(e->bufsize, sizeof(ltcsnd_sample_t));
134 ltc_frame_reset(&e->f);
135 ltc_encoder_reinit(e, sample_rate, fps, standard, flags);
139 void ltc_encoder_free(LTCEncoder *e) {
141 if (e->buf) free(e->buf);
145 int ltc_encoder_reinit(LTCEncoder *e, double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags) {
149 size_t bufsize = 1 + ceil(sample_rate / fps);
150 if (bufsize > e->bufsize) {
156 e->sample_rate = sample_rate;
157 ltc_encoder_set_filter(e, 40.0);
160 e->standard = standard;
161 e->samples_per_clock = sample_rate / (fps * 80.0);
162 e->samples_per_clock_2 = e->samples_per_clock / 2.0;
163 e->sample_remainder = 0.5;
165 if (flags & LTC_BGF_DONT_TOUCH) {
167 if (flags<C_TC_CLOCK) {
168 e->f.binary_group_flag_bit1 = 1;
170 e->f.binary_group_flag_bit1 = 0;
173 case LTC_TV_625_50: /* 25 fps mode */
174 e->f.biphase_mark_phase_correction = 0; // BGF0
175 e->f.binary_group_flag_bit0 = (flags<C_USE_DATE)?1:0; // BGF2
178 e->f.binary_group_flag_bit0 = 0;
179 e->f.binary_group_flag_bit2 = (flags<C_USE_DATE)?1:0;
183 if ((flags<C_NO_PARITY) == 0) {
184 ltc_frame_set_parity(&e->f, standard);
187 if (rint(fps*100) == 2997)
194 void ltc_encoder_reset(LTCEncoder *e) {
196 e->sample_remainder = 0.5;
200 int ltc_encoder_set_volume(LTCEncoder *e, double dBFS) {
203 double pp = rint(127.0 * pow(10, dBFS/20.0));
204 if (pp < 1 || pp > 127)
206 ltcsnd_sample_t diff = ((ltcsnd_sample_t) pp)&0x7f;
207 e->enc_lo = SAMPLE_CENTER - diff;
208 e->enc_hi = SAMPLE_CENTER + diff;
212 void ltc_encoder_set_filter(LTCEncoder *e, double rise_time) {
214 * LTC signal should have a rise time of 40 us +/- 10 us.
216 * rise-time means from <10% to >90% of the signal.
217 * in each call to addvalues() we start at 50% (SAMPLE_CENTER), so
218 * here we need half-of it.
224 e->filter_const = 1.0 - exp( -1.0 / (e->sample_rate * rise_time / 2000000.0 / exp(1.0)) );
227 int ltc_encoder_set_bufsize(LTCEncoder *e, double sample_rate, double fps) {
230 e->bufsize = 1 + ceil(sample_rate / fps);
231 e->buf = (ltcsnd_sample_t*) calloc(e->bufsize, sizeof(ltcsnd_sample_t));
238 int ltc_encoder_encode_byte(LTCEncoder *e, int byte, double speed) {
239 return encode_byte(e, byte, speed);
242 void ltc_encoder_encode_frame(LTCEncoder *e) {
244 for (byte = 0 ; byte < 10 ; byte++) {
245 encode_byte(e, byte, 1.0);
249 void ltc_encoder_get_timecode(LTCEncoder *e, SMPTETimecode *t) {
250 ltc_frame_to_time(t, &e->f, e->flags);
253 void ltc_encoder_set_timecode(LTCEncoder *e, SMPTETimecode *t) {
254 ltc_time_to_frame(&e->f, t, e->standard, e->flags);
257 void ltc_encoder_get_frame(LTCEncoder *e, LTCFrame *f) {
258 memcpy(f, &e->f, sizeof(LTCFrame));
261 void ltc_encoder_set_frame(LTCEncoder *e, LTCFrame *f) {
262 memcpy(&e->f, f, sizeof(LTCFrame));
265 int ltc_encoder_inc_timecode(LTCEncoder *e) {
266 return ltc_frame_increment (&e->f, rint(e->fps), e->standard, e->flags);
269 int ltc_encoder_dec_timecode(LTCEncoder *e) {
270 return ltc_frame_decrement (&e->f, rint(e->fps), e->standard, e->flags);
273 size_t ltc_encoder_get_buffersize(LTCEncoder *e) {
277 void ltc_encoder_buffer_flush(LTCEncoder *e) {
281 ltcsnd_sample_t *ltc_encoder_get_bufptr(LTCEncoder *e, int *size, int flush) {
282 if (size) *size = e->offset;
283 if (flush) e->offset = 0;
287 int ltc_encoder_get_buffer(LTCEncoder *e, ltcsnd_sample_t *buf) {
288 const int len = e->offset;
289 memcpy(buf, e->buf, len * sizeof(ltcsnd_sample_t) );
294 void ltc_frame_set_parity(LTCFrame *frame, enum LTC_TV_STANDARD standard) {
298 if (standard != LTC_TV_625_50) { /* 30fps, 24fps */
299 frame->biphase_mark_phase_correction = 0;
301 frame->binary_group_flag_bit2 = 0;
304 for (i=0; i < LTC_FRAME_BIT_COUNT / 8; ++i){
305 p = p ^ (((unsigned char*)frame)[i]);
307 #define PRY(BIT) ((p>>BIT)&1)
309 if (standard != LTC_TV_625_50) { /* 30fps, 24fps */
310 frame->biphase_mark_phase_correction =
311 PRY(0)^PRY(1)^PRY(2)^PRY(3)^PRY(4)^PRY(5)^PRY(6)^PRY(7);
313 frame->binary_group_flag_bit2 =
314 PRY(0)^PRY(1)^PRY(2)^PRY(3)^PRY(4)^PRY(5)^PRY(6)^PRY(7);
318 ltc_off_t ltc_frame_alignment(double samples_per_frame, enum LTC_TV_STANDARD standard) {
321 return rint(samples_per_frame * 4.0 / 525.0);
323 return rint(samples_per_frame * 1.0 / 625.0);