summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2012-12-14 16:18:47 +0000
committerPaul Davis <paul@linuxaudiosystems.com>2012-12-14 16:18:47 +0000
commitc9c94ca1c247adfc1a78fda0eda185a9a7589696 (patch)
tree52d520020e7ff2455b8abf83fbcc69a4ba02b9ca
parentfd1414f6668712c641da6c7d51f4b3e2bf6fae5a (diff)
merge robin gareus' libltc into ardour source tree for convenience
git-svn-id: svn://localhost/ardour2/branches/3.0@13665 d708f5d6-7413-0410-9779-e7cbd77b26cf
-rw-r--r--gtk2_ardour/ardev_common.sh.in2
-rw-r--r--libs/ardour/ardour/session.h6
-rw-r--r--libs/ardour/ardour/slave.h6
-rw-r--r--libs/ardour/globals.cc2
-rw-r--r--libs/ardour/session.cc4
-rw-r--r--libs/ardour/session_process.cc6
-rw-r--r--libs/ardour/session_state.cc4
-rw-r--r--libs/ardour/session_time.cc2
-rw-r--r--libs/ardour/session_transport.cc5
-rw-r--r--libs/ardour/wscript13
-rw-r--r--libs/libltc/decoder.c334
-rw-r--r--libs/libltc/encoder.c106
-rw-r--r--libs/libltc/ltc.c327
-rw-r--r--libs/libltc/ltc/decoder.h54
-rw-r--r--libs/libltc/ltc/encoder.h47
-rw-r--r--libs/libltc/ltc/ltc.h778
-rw-r--r--libs/libltc/timecode.c451
-rw-r--r--libs/libltc/wscript53
-rw-r--r--wscript2
19 files changed, 2160 insertions, 42 deletions
diff --git a/gtk2_ardour/ardev_common.sh.in b/gtk2_ardour/ardev_common.sh.in
index ae7841f74b..40d9a410fa 100644
--- a/gtk2_ardour/ardev_common.sh.in
+++ b/gtk2_ardour/ardev_common.sh.in
@@ -35,7 +35,7 @@ fi
export VAMP_PATH=$libs/vamp-plugins${VAMP_PATH:+:$VAMP_PATH}
-export LD_LIBRARY_PATH=$libs/qm-dsp:$libs/vamp-sdk:$libs/surfaces:$libs/surfaces/control_protocol:$libs/ardour:$libs/midi++2:$libs/pbd:$libs/rubberband:$libs/soundtouch:$libs/gtkmm2ext:$libs/gnomecanvas:$libs/libsndfile:$libs/appleutility:$libs/taglib:$libs/evoral:$libs/evoral/src/libsmf:$libs/audiographer:$libs/timecode:${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
+export LD_LIBRARY_PATH=$libs/qm-dsp:$libs/vamp-sdk:$libs/surfaces:$libs/surfaces/control_protocol:$libs/ardour:$libs/midi++2:$libs/pbd:$libs/rubberband:$libs/soundtouch:$libs/gtkmm2ext:$libs/gnomecanvas:$libs/libsndfile:$libs/appleutility:$libs/taglib:$libs/evoral:$libs/evoral/src/libsmf:$libs/audiographer:$libs/timecode:$libs/libltc:${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
# DYLD_LIBRARY_PATH is for darwin.
export DYLD_FALLBACK_LIBRARY_PATH=$LD_LIBRARY_PATH
diff --git a/libs/ardour/ardour/session.h b/libs/ardour/ardour/session.h
index b2a077162c..414a3204bd 100644
--- a/libs/ardour/ardour/session.h
+++ b/libs/ardour/ardour/session.h
@@ -48,6 +48,7 @@
#include "midi++/types.h"
#include "timecode/time.h"
+#include "ltc/ltc.h"
#include "ardour/ardour.h"
#include "ardour/chan_count.h"
@@ -64,9 +65,6 @@
#include <jack/session.h>
#endif
-#ifdef HAVE_LTC
-#include <ltc.h>
-#endif
class XMLTree;
class XMLNode;
@@ -1189,7 +1187,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
int send_midi_time_code_for_cycle (framepos_t, framepos_t, pframes_t nframes);
-#ifdef HAVE_LTC
LTCEncoder* ltc_encoder;
ltcsnd_sample_t* ltc_enc_buf;
@@ -1216,7 +1213,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
void ltc_tx_recalculate_position();
void ltc_tx_parse_offset();
void ltc_tx_send_time_code_for_cycle (framepos_t, framepos_t, double, double, pframes_t nframes);
-#endif
void reset_record_status ();
diff --git a/libs/ardour/ardour/slave.h b/libs/ardour/ardour/slave.h
index d5cb79c522..471a632f6e 100644
--- a/libs/ardour/ardour/slave.h
+++ b/libs/ardour/ardour/slave.h
@@ -29,14 +29,12 @@
#include "pbd/signals.h"
#include "timecode/time.h"
+#include "ltc/ltc.h"
#include "ardour/types.h"
#include "midi++/parser.h"
#include "midi++/types.h"
-#ifdef HAVE_LTC
-#include <ltc.h>
-#endif
/* used for approximate_current_delta(): */
#define PLUSMINUS(A) ( ((A)<0) ? "-" : (((A)>0) ? "+" : "\u00B1") )
@@ -335,7 +333,6 @@ class MTC_Slave : public TimecodeSlave {
void parameter_changed(std::string const & p);
};
-#ifdef HAVE_LTC
class LTC_Slave : public TimecodeSlave {
public:
LTC_Slave (Session&);
@@ -403,7 +400,6 @@ public:
double e2; ///< second order loop error
double b, c; ///< DLL filter coefficients
};
-#endif
class MIDIClock_Slave : public Slave {
public:
diff --git a/libs/ardour/globals.cc b/libs/ardour/globals.cc
index 2b0bc05534..613ecbfc7d 100644
--- a/libs/ardour/globals.cc
+++ b/libs/ardour/globals.cc
@@ -484,9 +484,7 @@ ARDOUR::get_available_sync_options ()
ret.push_back (JACK);
ret.push_back (MTC);
ret.push_back (MIDIClock);
-#ifdef HAVE_LTC
ret.push_back (LTC);
-#endif
return ret;
}
diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc
index 3b46ad3be1..78caf7abc4 100644
--- a/libs/ardour/session.cc
+++ b/libs/ardour/session.cc
@@ -152,9 +152,7 @@ Session::Session (AudioEngine &eng,
, _suspend_timecode_transmission (0)
{
_locations = new Locations (*this);
-#ifdef HAVE_LTC
ltc_encoder = NULL;
-#endif
if (how_many_dsp_threads () > 1) {
/* For now, only create the graph if we are using >1 DSP threads, as
@@ -244,9 +242,7 @@ Session::destroy ()
Port::PortDrop (); /* EMIT SIGNAL */
-#ifdef HAVE_LTC
ltc_tx_cleanup();
-#endif
/* clear history so that no references to objects are held any more */
diff --git a/libs/ardour/session_process.cc b/libs/ardour/session_process.cc
index bd5cc66451..05b800fead 100644
--- a/libs/ardour/session_process.cc
+++ b/libs/ardour/session_process.cc
@@ -116,9 +116,7 @@ Session::no_roll (pframes_t nframes)
_click_io->silence (nframes);
}
-#ifdef HAVE_LTC
ltc_tx_send_time_code_for_cycle (_transport_frame, end_frame, _target_transport_speed, _transport_speed, nframes);
-#endif
if (_process_graph) {
DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/no-roll\n");
@@ -793,9 +791,7 @@ Session::process_without_events (pframes_t nframes)
if (!_exporting && _slave) {
if (!follow_slave (nframes)) {
-#ifdef HAVE_LTC
ltc_tx_send_time_code_for_cycle (_transport_frame, _transport_frame, 0, 0 , nframes);
-#endif
return;
}
}
@@ -817,9 +813,7 @@ Session::process_without_events (pframes_t nframes)
send_midi_time_code_for_cycle (_transport_frame, _transport_frame + frames_moved, nframes);
}
-#ifdef HAVE_LTC
ltc_tx_send_time_code_for_cycle (_transport_frame, _transport_frame + frames_moved, _target_transport_speed, _transport_speed, nframes);
-#endif
framepos_t const stop_limit = compute_stop_limit ();
diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc
index a1f457393a..c38d4aa781 100644
--- a/libs/ardour/session_state.cc
+++ b/libs/ardour/session_state.cc
@@ -367,9 +367,7 @@ Session::second_stage_init ()
MIDI::Name::MidiPatchManager::instance().set_session (this);
-#ifdef HAVE_LTC
ltc_tx_initialize();
-#endif
/* initial program change will be delivered later; see ::config_changed() */
_state_of_the_state = Clean;
@@ -3566,10 +3564,8 @@ Session::config_changed (std::string p, bool ours)
reconnect_ltc_input ();
} else if (p == "ltc-sink-port") {
reconnect_ltc_output ();
-#ifdef HAVE_LTC
} else if (p == "timecode-generator-offset") {
ltc_tx_parse_offset();
-#endif
}
set_dirty ();
diff --git a/libs/ardour/session_time.cc b/libs/ardour/session_time.cc
index 72d8fb1e89..7c0127c9df 100644
--- a/libs/ardour/session_time.cc
+++ b/libs/ardour/session_time.cc
@@ -95,9 +95,7 @@ Session::sync_time_vars ()
}
break;
};
-#ifdef HAVE_LTC
ltc_tx_parse_offset();
-#endif
}
void
diff --git a/libs/ardour/session_transport.cc b/libs/ardour/session_transport.cc
index ae5a8157c7..037ca83484 100644
--- a/libs/ardour/session_transport.cc
+++ b/libs/ardour/session_transport.cc
@@ -1386,7 +1386,6 @@ Session::switch_to_sync_source (SyncSource src)
break;
case LTC:
-#ifdef HAVE_LTC
if (_slave && dynamic_cast<LTC_Slave*>(_slave)) {
return;
}
@@ -1398,9 +1397,7 @@ Session::switch_to_sync_source (SyncSource src)
catch (failed_constructor& err) {
return;
}
-#else
- return;
-#endif
+
break;
case MIDIClock:
diff --git a/libs/ardour/wscript b/libs/ardour/wscript
index f47ba0a3b2..b37acb0271 100644
--- a/libs/ardour/wscript
+++ b/libs/ardour/wscript
@@ -116,6 +116,7 @@ libardour_sources = [
'ladspa_plugin.cc',
'location.cc',
'location_importer.cc',
+ 'ltc_slave.cc',
'meter.cc',
'midi_automation_list_binder.cc',
'midi_buffer.cc',
@@ -184,6 +185,7 @@ libardour_sources = [
'session_events.cc',
'session_export.cc',
'session_handle.cc',
+ 'session_ltc.cc',
'session_metadata.cc',
'session_midi.cc',
'session_object.cc',
@@ -216,7 +218,7 @@ libardour_sources = [
'user_bundle.cc',
'utils.cc',
'version.cc',
- 'worker.cc',
+ 'worker.cc'
]
def flac_supported():
@@ -281,8 +283,6 @@ def configure(conf):
atleast_version='1.2.1')
autowaf.check_pkg(conf, 'libcurl', uselib_store='CURL',
atleast_version='7.0.0')
- autowaf.check_pkg(conf, 'ltc', uselib_store='LTC',
- atleast_version='1.1.0', mandatory=False)
# we don't try to detect this, since its part of our source tree
@@ -380,7 +380,7 @@ def build(bld):
'OSX','BOOST','CURL','DL']
obj.use = ['libpbd','libmidipp','libevoral','libvamphost',
'libvampplugin','libtaglib','librubberband',
- 'libaudiographer']
+ 'libaudiographer','libltc']
obj.vnum = LIBARDOUR_LIB_VERSION
obj.install_path = os.path.join(bld.env['LIBDIR'], 'ardour3')
obj.defines = [
@@ -396,11 +396,6 @@ def build(bld):
#obj.uselib += ' SOUNDTOUCH '
#obj.add_objects = 'default/libs/surfaces/control_protocol/smpte_1.o'
- if bld.is_defined('HAVE_LTC') :
- obj.source += ['ltc_slave.cc']
- obj.source += ['session_ltc.cc']
- obj.uselib += ['LTC']
-
if bld.is_defined('HAVE_LILV') :
obj.source += ['lv2_plugin.cc', 'lv2_evbuf.c', 'uri_map.cc']
obj.uselib += ['LILV']
diff --git a/libs/libltc/decoder.c b/libs/libltc/decoder.c
new file mode 100644
index 0000000000..29e4a32ef0
--- /dev/null
+++ b/libs/libltc/decoder.c
@@ -0,0 +1,334 @@
+/*
+ libltc - en+decode linear timecode
+
+ Copyright (C) 2005 Maarten de Boer <mdeboer@iua.upf.es>
+ Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
+ Copyright (C) 2008-2009 Jan <jan@geheimwerk.de>
+
+ Binary constant generator macro for endianess conversion
+ by Tom Torfs - donated to the public domain
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/** turn a numeric literal into a hex constant
+ * (avoids problems with leading zeroes)
+ * 8-bit constants max value 0x11111111, always fits in unsigned long
+ */
+#define HEX__(n) 0x##n##LU
+
+/**
+ * 8-bit conversion function
+ */
+#define B8__(x) ((x&0x0000000FLU)?1:0) \
+ +((x&0x000000F0LU)?2:0) \
+ +((x&0x00000F00LU)?4:0) \
+ +((x&0x0000F000LU)?8:0) \
+ +((x&0x000F0000LU)?16:0) \
+ +((x&0x00F00000LU)?32:0) \
+ +((x&0x0F000000LU)?64:0) \
+ +((x&0xF0000000LU)?128:0)
+
+/** for upto 8-bit binary constants */
+#define B8(d) ((unsigned char)B8__(HEX__(d)))
+
+/** for upto 16-bit binary constants, MSB first */
+#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))
+
+/** turn a numeric literal into a hex constant
+ *(avoids problems with leading zeroes)
+ * 8-bit constants max value 0x11111111, always fits in unsigned long
+ */
+#define HEX__(n) 0x##n##LU
+
+/** 8-bit conversion function */
+#define B8__(x) ((x&0x0000000FLU)?1:0) \
+ +((x&0x000000F0LU)?2:0) \
+ +((x&0x00000F00LU)?4:0) \
+ +((x&0x0000F000LU)?8:0) \
+ +((x&0x000F0000LU)?16:0) \
+ +((x&0x00F00000LU)?32:0) \
+ +((x&0x0F000000LU)?64:0) \
+ +((x&0xF0000000LU)?128:0)
+
+
+/** for upto 8-bit binary constants */
+#define B8(d) ((unsigned char)B8__(HEX__(d)))
+
+/** for upto 16-bit binary constants, MSB first */
+#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))
+
+/* Example usage:
+ * B8(01010101) = 85
+ * B16(10101010,01010101) = 43605
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#include "ltc/decoder.h"
+
+#define DEBUG_DUMP(msg, f) \
+{ \
+ int _ii; \
+ printf("%s", msg); \
+ for (_ii=0; _ii < (LTC_FRAME_BIT_COUNT >> 3); _ii++) { \
+ const unsigned char _bit = ((unsigned char*)(f))[_ii]; \
+ printf("%c", (_bit & B8(10000000) ) ? '1' : '0'); \
+ printf("%c", (_bit & B8(01000000) ) ? '1' : '0'); \
+ printf("%c", (_bit & B8(00100000) ) ? '1' : '0'); \
+ printf("%c", (_bit & B8(00010000) ) ? '1' : '0'); \
+ printf("%c", (_bit & B8(00001000) ) ? '1' : '0'); \
+ printf("%c", (_bit & B8(00000100) ) ? '1' : '0'); \
+ printf("%c", (_bit & B8(00000010) ) ? '1' : '0'); \
+ printf("%c", (_bit & B8(00000001) ) ? '1' : '0'); \
+ printf(" "); \
+ }\
+ printf("\n"); \
+}
+
+static double calc_volume_db(LTCDecoder *d) {
+ if (d->snd_to_biphase_max <= d->snd_to_biphase_min)
+ return -INFINITY;
+ return (20.0 * log10((d->snd_to_biphase_max - d->snd_to_biphase_min) / 255.0));
+}
+
+static void parse_ltc(LTCDecoder *d, unsigned char bit, int offset, ltc_off_t posinfo) {
+ int bit_num, bit_set, byte_num;
+
+ if (d->bit_cnt == 0) {
+ memset(&d->ltc_frame, 0, sizeof(LTCFrame));
+
+ if (d->frame_start_prev < 0) {
+ d->frame_start_off = posinfo - d->snd_to_biphase_period;
+ } else {
+ d->frame_start_off = d->frame_start_prev;
+ }
+ }
+ d->frame_start_prev = offset + posinfo;
+
+ if (d->bit_cnt >= LTC_FRAME_BIT_COUNT) {
+ /* shift bits backwards */
+ int k = 0;
+ const int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;
+
+ for (k=0; k< byte_num_max; k++) {
+ const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
+ unsigned char bo = 0;
+ bo |= (bi & B8(10000000) ) ? B8(01000000) : 0;
+ bo |= (bi & B8(01000000) ) ? B8(00100000) : 0;
+ bo |= (bi & B8(00100000) ) ? B8(00010000) : 0;
+ bo |= (bi & B8(00010000) ) ? B8(00001000) : 0;
+ bo |= (bi & B8(00001000) ) ? B8(00000100) : 0;
+ bo |= (bi & B8(00000100) ) ? B8(00000010) : 0;
+ bo |= (bi & B8(00000010) ) ? B8(00000001) : 0;
+ if (k+1 < byte_num_max) {
+ bo |= ( (((unsigned char*)&d->ltc_frame)[k+1]) & B8(00000001) ) ? B8(10000000): B8(00000000);
+ }
+ ((unsigned char*)&d->ltc_frame)[k] = bo;
+ }
+
+ d->frame_start_off += ceil(d->snd_to_biphase_period);
+ d->bit_cnt--;
+ }
+
+ d->decoder_sync_word <<= 1;
+ if (bit) {
+
+ d->decoder_sync_word |= B16(00000000,00000001);
+
+ if (d->bit_cnt < LTC_FRAME_BIT_COUNT) {
+ // Isolating the lowest three bits: the location of this bit in the current byte
+ bit_num = (d->bit_cnt & B8(00000111));
+ // Using the bit number to define which of the eight bits to set
+ bit_set = (B8(00000001) << bit_num);
+ // Isolating the higher bits: the number of the byte/char the target bit is contained in
+ byte_num = d->bit_cnt >> 3;
+
+ (((unsigned char*)&d->ltc_frame)[byte_num]) |= bit_set;
+ }
+
+ }
+ d->bit_cnt++;
+
+ if (d->decoder_sync_word == B16(00111111,11111101) /*LTC Sync Word 0x3ffd*/) {
+ if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
+ int bc;
+
+ memcpy( &d->queue[d->queue_write_off].ltc,
+ &d->ltc_frame,
+ sizeof(LTCFrame));
+
+ for(bc = 0; bc < LTC_FRAME_BIT_COUNT; ++bc) {
+ const int btc = (d->biphase_tic + bc ) % LTC_FRAME_BIT_COUNT;
+ d->queue[d->queue_write_off].biphase_tics[bc] = d->biphase_tics[btc];
+ }
+
+ d->queue[d->queue_write_off].off_start = d->frame_start_off;
+ d->queue[d->queue_write_off].off_end = posinfo + (ltc_off_t) offset - 1LL;
+ d->queue[d->queue_write_off].reverse = 0;
+ d->queue[d->queue_write_off].volume = calc_volume_db(d);
+ d->queue[d->queue_write_off].sample_min = d->snd_to_biphase_min;
+ d->queue[d->queue_write_off].sample_max = d->snd_to_biphase_max;
+
+ d->queue_write_off++;
+
+ if (d->queue_write_off == d->queue_len)
+ d->queue_write_off = 0;
+ }
+ d->bit_cnt = 0;
+ }
+
+ if (d->decoder_sync_word == B16(10111111,11111100) /* reverse sync-word*/) {
+ if (d->bit_cnt == LTC_FRAME_BIT_COUNT) {
+ /* reverse frame */
+ int bc;
+ int k = 0;
+ int byte_num_max = LTC_FRAME_BIT_COUNT >> 3;
+
+ /* swap bits */
+ for (k=0; k< byte_num_max; k++) {
+ const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
+ unsigned char bo = 0;
+ bo |= (bi & B8(10000000) ) ? B8(00000001) : 0;
+ bo |= (bi & B8(01000000) ) ? B8(00000010) : 0;
+ bo |= (bi & B8(00100000) ) ? B8(00000100) : 0;
+ bo |= (bi & B8(00010000) ) ? B8(00001000) : 0;
+ bo |= (bi & B8(00001000) ) ? B8(00010000) : 0;
+ bo |= (bi & B8(00000100) ) ? B8(00100000) : 0;
+ bo |= (bi & B8(00000010) ) ? B8(01000000) : 0;
+ bo |= (bi & B8(00000001) ) ? B8(10000000) : 0;
+ ((unsigned char*)&d->ltc_frame)[k] = bo;
+ }
+
+ /* swap bytes */
+ byte_num_max-=2; // skip sync-word
+ for (k=0; k< (byte_num_max)/2; k++) {
+ const unsigned char bi = ((unsigned char*)&d->ltc_frame)[k];
+ ((unsigned char*)&d->ltc_frame)[k] = ((unsigned char*)&d->ltc_frame)[byte_num_max-1-k];
+ ((unsigned char*)&d->ltc_frame)[byte_num_max-1-k] = bi;
+ }
+
+ memcpy( &d->queue[d->queue_write_off].ltc,
+ &d->ltc_frame,
+ sizeof(LTCFrame));
+
+ for(bc = 0; bc < LTC_FRAME_BIT_COUNT; ++bc) {
+ const int btc = (d->biphase_tic + bc ) % LTC_FRAME_BIT_COUNT;
+ d->queue[d->queue_write_off].biphase_tics[bc] = d->biphase_tics[btc];
+ }
+
+ d->queue[d->queue_write_off].off_start = d->frame_start_off - 16 * d->snd_to_biphase_period;
+ d->queue[d->queue_write_off].off_end = posinfo + (ltc_off_t) offset - 1LL - 16 * d->snd_to_biphase_period;
+ d->queue[d->queue_write_off].reverse = (LTC_FRAME_BIT_COUNT >> 3) * 8 * d->snd_to_biphase_period;
+ d->queue[d->queue_write_off].volume = calc_volume_db(d);
+ d->queue[d->queue_write_off].sample_min = d->snd_to_biphase_min;
+ d->queue[d->queue_write_off].sample_max = d->snd_to_biphase_max;
+
+ d->queue_write_off++;
+
+ if (d->queue_write_off == d->queue_len)
+ d->queue_write_off = 0;
+ }
+ d->bit_cnt = 0;
+ }
+}
+
+static inline void biphase_decode2(LTCDecoder *d, int offset, ltc_off_t pos) {
+
+ d->biphase_tics[d->biphase_tic] = d->snd_to_biphase_period;
+ d->biphase_tic = (d->biphase_tic + 1) % LTC_FRAME_BIT_COUNT;
+ if (d->snd_to_biphase_cnt <= 2 * d->snd_to_biphase_period) {
+ pos -= (d->snd_to_biphase_period - d->snd_to_biphase_cnt);
+ }
+
+ if (d->snd_to_biphase_state == d->biphase_prev) {
+ d->biphase_state = 1;
+ parse_ltc(d, 0, offset, pos);
+ } else {
+ d->biphase_state = 1 - d->biphase_state;
+ if (d->biphase_state == 1) {
+ parse_ltc(d, 1, offset, pos);
+ }
+ }
+ d->biphase_prev = d->snd_to_biphase_state;
+}
+
+void decode_ltc(LTCDecoder *d, ltcsnd_sample_t *sound, size_t size, ltc_off_t posinfo) {
+ size_t i;
+
+ for (i = 0 ; i < size ; i++) {
+ ltcsnd_sample_t max_threshold, min_threshold;
+
+ /* track minimum and maximum values */
+ d->snd_to_biphase_min = SAMPLE_CENTER - (((SAMPLE_CENTER - d->snd_to_biphase_min) * 15) / 16);
+ d->snd_to_biphase_max = SAMPLE_CENTER + (((d->snd_to_biphase_max - SAMPLE_CENTER) * 15) / 16);
+
+ if (sound[i] < d->snd_to_biphase_min)
+ d->snd_to_biphase_min = sound[i];
+ if (sound[i] > d->snd_to_biphase_max)
+ d->snd_to_biphase_max = sound[i];
+
+ /* set the thresholds for hi/lo state tracking */
+ min_threshold = SAMPLE_CENTER - (((SAMPLE_CENTER - d->snd_to_biphase_min) * 8) / 16);
+ max_threshold = SAMPLE_CENTER + (((d->snd_to_biphase_max - SAMPLE_CENTER) * 8) / 16);
+
+ if ( /* Check for a biphase state change */
+ ( d->snd_to_biphase_state && (sound[i] > max_threshold) )
+ || ( !d->snd_to_biphase_state && (sound[i] < min_threshold) )
+ ) {
+
+ /* If the sample count has risen above the biphase length limit */
+ if (d->snd_to_biphase_cnt > d->snd_to_biphase_lmt) {
+ /* single state change within a biphase priod. decode to a 0 */
+ biphase_decode2(d, i, posinfo);
+ biphase_decode2(d, i, posinfo);
+
+ } else {
+ /* "short" state change covering half a period
+ * together with the next or previous state change decode to a 1
+ */
+ d->snd_to_biphase_cnt *= 2;
+ biphase_decode2(d, i, posinfo);
+
+ }
+
+ if (d->snd_to_biphase_cnt > (d->snd_to_biphase_period * 4)) {
+ /* "long" silence in between
+ * -> reset parser, don't use it for phase-tracking
+ */
+ d->bit_cnt = 0;
+ } else {
+ /* track speed variations
+ * As this is only executed at a state change,
+ * d->snd_to_biphase_cnt is an accurate representation of the current period length.
+ */
+ d->snd_to_biphase_period = (d->snd_to_biphase_period * 3.0 + d->snd_to_biphase_cnt) / 4.0;
+
+ /* This limit specifies when a state-change is
+ * considered biphase-clock or 2*biphase-clock.
+ * The relation with period has been determined
+ * empirically through trial-and-error */
+ d->snd_to_biphase_lmt = (d->snd_to_biphase_period * 3) / 4;
+ }
+
+ d->snd_to_biphase_cnt = 0;
+ d->snd_to_biphase_state = !d->snd_to_biphase_state;
+ }
+ d->snd_to_biphase_cnt++;
+ }
+}
diff --git a/libs/libltc/encoder.c b/libs/libltc/encoder.c
new file mode 100644
index 0000000000..fce2a0ce51
--- /dev/null
+++ b/libs/libltc/encoder.c
@@ -0,0 +1,106 @@
+/*
+ libltc - en+decode linear timecode
+
+ Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#include "ltc/encoder.h"
+
+/**
+ * add values to the output buffer
+ */
+static int addvalues(LTCEncoder *e, int n) {
+ const ltcsnd_sample_t tgtval = e->state ? e->enc_hi : e->enc_lo;
+
+ if (e->offset + n >= e->bufsize) {
+ fprintf(stderr, "libltc: buffer overflow: %d/%lu\n", (int) e->offset, (unsigned long) e->bufsize);
+ return 1;
+ }
+
+ ltcsnd_sample_t * const wave = &(e->buf[e->offset]);
+ const double tcf = e->filter_const;
+ if (tcf > 0) {
+ /* low-pass-filter
+ * LTC signal should have a rise time of 40 us +/- 10 us.
+ *
+ * rise-time means from <10% to >90% of the signal.
+ * in each call to addvalues() we start at 50%, so
+ * here we need half-of it. (0.000020 sec)
+ *
+ * e->cutoff = 1.0 -exp( -1.0 / (sample_rate * .000020 / exp(1.0)) );
+ */
+ int i;
+ ltcsnd_sample_t val = SAMPLE_CENTER;
+ int m = (n+1)>>1;
+ for (i = 0 ; i < m ; i++) {
+ val = val + tcf * (tgtval - val);
+ wave[n-i-1] = wave[i] = val;
+ }
+ } else {
+ /* perfect square wave */
+ memset(wave, tgtval, n);
+ }
+
+ e->offset += n;
+ return 0;
+}
+
+int encode_byte(LTCEncoder *e, int byte, double speed) {
+ if (byte < 0 || byte > 9) return -1;
+ if (speed ==0) return -1;
+
+ int err = 0;
+ const unsigned char c = ((unsigned char*)&e->f)[byte];
+ unsigned char b = (speed < 0)?128:1; // bit
+ const double spc = e->samples_per_clock * fabs(speed);
+ const double sph = e->samples_per_clock_2 * fabs(speed);
+
+ do
+ {
+ int n;
+ if ((c & b) == 0) {
+ n = (int)(spc + e->sample_remainder);
+ e->sample_remainder = spc + e->sample_remainder - n;
+ e->state = !e->state;
+ err |= addvalues(e, n);
+ } else {
+ n = (int)(sph + e->sample_remainder);
+ e->sample_remainder = sph + e->sample_remainder - n;
+ e->state = !e->state;
+ err |= addvalues(e, n);
+
+ n = (int)(sph + e->sample_remainder);
+ e->sample_remainder = sph + e->sample_remainder - n;
+ e->state = !e->state;
+ err |= addvalues(e, n);
+ }
+ /* this is based on the assumption that with every compiler
+ * ((unsigned char) 128)<<1 == ((unsigned char 1)>>1) == 0
+ */
+ if (speed < 0)
+ b >>= 1;
+ else
+ b <<= 1;
+ } while (b);
+
+ return err;
+}
diff --git a/libs/libltc/ltc.c b/libs/libltc/ltc.c
new file mode 100644
index 0000000000..c0dd5a7d2c
--- /dev/null
+++ b/libs/libltc/ltc.c
@@ -0,0 +1,327 @@
+/*
+ libltc - en+decode linear timecode
+
+ Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#include "ltc/ltc.h"
+#include "ltc/decoder.h"
+#include "ltc/encoder.h"
+
+/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * Decoder
+ */
+
+LTCDecoder* ltc_decoder_create(int apv, int queue_len) {
+ LTCDecoder* d = (LTCDecoder*) calloc(1, sizeof(LTCDecoder));
+ if (!d) return NULL;
+
+ d->queue_len = queue_len;
+ d->queue = (LTCFrameExt*) calloc(d->queue_len, sizeof(LTCFrameExt));
+ if (!d->queue) {
+ free(d);
+ return NULL;
+ }
+ d->biphase_state = 1;
+ d->snd_to_biphase_period = apv / 80;
+ d->snd_to_biphase_lmt = (d->snd_to_biphase_period * 3) / 4;
+
+ d->snd_to_biphase_min = SAMPLE_CENTER;
+ d->snd_to_biphase_max = SAMPLE_CENTER;
+ d->frame_start_prev = -1;
+ d->biphase_tic = 0;
+
+ return d;
+}
+
+int ltc_decoder_free(LTCDecoder *d) {
+ if (!d) return 1;
+ if (d->queue) free(d->queue);
+ free(d);
+
+ return 0;
+}
+
+void ltc_decoder_write(LTCDecoder *d, ltcsnd_sample_t *buf, size_t size, ltc_off_t posinfo) {
+ decode_ltc(d, buf, size, posinfo);
+}
+
+#define LTCWRITE_TEMPLATE(FN, FORMAT, CONV) \
+void ltc_decoder_write_ ## FN (LTCDecoder *d, FORMAT *buf, size_t size, ltc_off_t posinfo) { \
+ ltcsnd_sample_t tmp[1024]; \
+ size_t remain = size; \
+ while (remain > 0) { \
+ int c = (remain > 1024) ? 1024 : remain; \
+ int i; \
+ for (i=0; i<c; i++) { \
+ tmp[i] = CONV; \
+ } \
+ decode_ltc(d, tmp, c, posinfo + (ltc_off_t)c); \
+ remain -= c; \
+ } \
+}
+
+LTCWRITE_TEMPLATE(float, float, 128 + (buf[i] * 127.0))
+LTCWRITE_TEMPLATE(s16, short, 128 + (buf[i] >> 8))
+LTCWRITE_TEMPLATE(u16, short, (buf[i] >> 8))
+
+int ltc_decoder_read(LTCDecoder* d, LTCFrameExt* frame) {
+ if (!frame) return -1;
+ if (d->queue_read_off != d->queue_write_off) {
+ memcpy(frame, &d->queue[d->queue_read_off], sizeof(LTCFrameExt));
+ d->queue_read_off++;
+ if (d->queue_read_off == d->queue_len)
+ d->queue_read_off = 0;
+ return 1;
+ }
+ return 0;
+}
+
+void ltc_decoder_queue_flush(LTCDecoder* d) {
+ while (d->queue_read_off != d->queue_write_off) {
+ d->queue_read_off++;
+ if (d->queue_read_off == d->queue_len)
+ d->queue_read_off = 0;
+ }
+}
+
+int ltc_decoder_queue_length(LTCDecoder* d) {
+ return (d->queue_write_off - d->queue_read_off + d->queue_len) % d->queue_len;
+}
+
+/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * Encoder
+ */
+
+LTCEncoder* ltc_encoder_create(double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags) {
+ if (sample_rate < 1)
+ return NULL;
+
+ LTCEncoder* e = (LTCEncoder*) calloc(1, sizeof(LTCEncoder));
+ if (!e)
+ return NULL;
+
+ /*-3.0 dBFS default */
+ e->enc_lo = 38;
+ e->enc_hi = 218;
+
+ e->bufsize = 1 + ceil(sample_rate / fps);
+ e->buf = (ltcsnd_sample_t*) calloc(e->bufsize, sizeof(ltcsnd_sample_t));
+ if (!e->buf) {
+ free(e);
+ return NULL;
+ }
+
+ ltc_frame_reset(&e->f);
+ ltc_encoder_reinit(e, sample_rate, fps, standard, flags);
+ return e;
+}
+
+void ltc_encoder_free(LTCEncoder *e) {
+ if (!e) return;
+ if (e->buf) free(e->buf);
+ free(e);
+}
+
+int ltc_encoder_reinit(LTCEncoder *e, double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags) {
+ if (sample_rate < 1)
+ return -1;
+
+ size_t bufsize = 1 + ceil(sample_rate / fps);
+ if (bufsize > e->bufsize) {
+ return -1;
+ }
+
+ e->state = 0;
+ e->offset = 0;
+ e->sample_rate = sample_rate;
+ ltc_encoder_set_filter(e, 40.0);
+ e->fps = fps;
+ e->flags = flags;
+ e->standard = standard;
+ e->samples_per_clock = sample_rate / (fps * 80.0);
+ e->samples_per_clock_2 = e->samples_per_clock / 2.0;
+ e->sample_remainder = 0.5;
+
+ if (flags & LTC_BGF_DONT_TOUCH) {
+ e->f.col_frame = 0;
+ if (flags&LTC_TC_CLOCK) {
+ e->f.binary_group_flag_bit1 = 1;
+ } else {
+ e->f.binary_group_flag_bit1 = 0;
+ }
+ switch (standard) {
+ case LTC_TV_625_50: /* 25 fps mode */
+ e->f.biphase_mark_phase_correction = 0; // BGF0
+ e->f.binary_group_flag_bit0 = (flags&LTC_USE_DATE)?1:0; // BGF2
+ break;
+ default:
+ e->f.binary_group_flag_bit0 = 0;
+ e->f.binary_group_flag_bit2 = (flags&LTC_USE_DATE)?1:0;
+ break;
+ }
+ }
+ if ((flags&LTC_NO_PARITY) == 0) {
+ ltc_frame_set_parity(&e->f, standard);
+ }
+
+ if (rint(fps*100) == 2997)
+ e->f.dfbit = 1;
+ else
+ e->f.dfbit = 0;
+ return 0;
+}
+
+void ltc_encoder_reset(LTCEncoder *e) {
+ e->state = 0;
+ e->sample_remainder = 0.5;
+ e->offset = 0;
+}
+
+int ltc_encoder_set_volume(LTCEncoder *e, double dBFS) {
+ if (dBFS > 0)
+ return -1;
+ double pp = rint(127.0 * pow(10, dBFS/20.0));
+ if (pp < 1 || pp > 127)
+ return -1;
+ ltcsnd_sample_t diff = ((ltcsnd_sample_t) pp)&0x7f;
+ e->enc_lo = SAMPLE_CENTER - diff;
+ e->enc_hi = SAMPLE_CENTER + diff;
+ return 0;
+}
+
+void ltc_encoder_set_filter(LTCEncoder *e, double rise_time) {
+ /* low-pass-filter
+ * LTC signal should have a rise time of 40 us +/- 10 us.
+ *
+ * rise-time means from <10% to >90% of the signal.
+ * in each call to addvalues() we start at 50% (SAMPLE_CENTER), so
+ * here we need half-of it.
+ */
+
+ if (rise_time <= 0)
+ e->filter_const = 0;
+ else
+ e->filter_const = 1.0 - exp( -1.0 / (e->sample_rate * rise_time / 2000000.0 / exp(1.0)) );
+}
+
+int ltc_encoder_set_bufsize(LTCEncoder *e, double sample_rate, double fps) {
+ free (e->buf);
+ e->offset = 0;
+ e->bufsize = 1 + ceil(sample_rate / fps);
+ e->buf = (ltcsnd_sample_t*) calloc(e->bufsize, sizeof(ltcsnd_sample_t));
+ if (!e->buf) {
+ return -1;
+ }
+ return 0;
+}
+
+int ltc_encoder_encode_byte(LTCEncoder *e, int byte, double speed) {
+ return encode_byte(e, byte, speed);
+}
+
+void ltc_encoder_encode_frame(LTCEncoder *e) {
+ int byte;
+ for (byte = 0 ; byte < 10 ; byte++) {
+ encode_byte(e, byte, 1.0);
+ }
+}
+
+void ltc_encoder_get_timecode(LTCEncoder *e, SMPTETimecode *t) {
+ ltc_frame_to_time(t, &e->f, e->flags);
+}
+
+void ltc_encoder_set_timecode(LTCEncoder *e, SMPTETimecode *t) {
+ ltc_time_to_frame(&e->f, t, e->standard, e->flags);
+}
+
+void ltc_encoder_get_frame(LTCEncoder *e, LTCFrame *f) {
+ memcpy(f, &e->f, sizeof(LTCFrame));
+}
+
+void ltc_encoder_set_frame(LTCEncoder *e, LTCFrame *f) {
+ memcpy(&e->f, f, sizeof(LTCFrame));
+}
+
+int ltc_encoder_inc_timecode(LTCEncoder *e) {
+ return ltc_frame_increment (&e->f, rint(e->fps), e->standard, e->flags);
+}
+
+int ltc_encoder_dec_timecode(LTCEncoder *e) {
+ return ltc_frame_decrement (&e->f, rint(e->fps), e->standard, e->flags);
+}
+
+size_t ltc_encoder_get_buffersize(LTCEncoder *e) {
+ return(e->bufsize);
+}
+
+void ltc_encoder_buffer_flush(LTCEncoder *e) {
+ e->offset = 0;
+}
+
+ltcsnd_sample_t *ltc_encoder_get_bufptr(LTCEncoder *e, int *size, int flush) {
+ if (size) *size = e->offset;
+ if (flush) e->offset = 0;
+ return e->buf;
+}
+
+int ltc_encoder_get_buffer(LTCEncoder *e, ltcsnd_sample_t *buf) {
+ const int len = e->offset;
+ memcpy(buf, e->buf, len * sizeof(ltcsnd_sample_t) );
+ e->offset = 0;
+ return(len);
+}
+
+void ltc_frame_set_parity(LTCFrame *frame, enum LTC_TV_STANDARD standard) {
+ int i;
+ unsigned char p = 0;
+
+ if (standard != LTC_TV_625_50) { /* 30fps, 24fps */
+ frame->biphase_mark_phase_correction = 0;
+ } else { /* 25fps */
+ frame->binary_group_flag_bit2 = 0;
+ }
+
+ for (i=0; i < LTC_FRAME_BIT_COUNT / 8; ++i){
+ p = p ^ (((unsigned char*)frame)[i]);
+ }
+#define PRY(BIT) ((p>>BIT)&1)
+
+ if (standard != LTC_TV_625_50) { /* 30fps, 24fps */
+ frame->biphase_mark_phase_correction =
+ PRY(0)^PRY(1)^PRY(2)^PRY(3)^PRY(4)^PRY(5)^PRY(6)^PRY(7);
+ } else { /* 25fps */
+ frame->binary_group_flag_bit2 =
+ PRY(0)^PRY(1)^PRY(2)^PRY(3)^PRY(4)^PRY(5)^PRY(6)^PRY(7);
+ }
+}
+
+ltc_off_t ltc_frame_alignment(double samples_per_frame, enum LTC_TV_STANDARD standard) {
+ switch (standard) {
+ case LTC_TV_525_60:
+ return rint(samples_per_frame * 4.0 / 525.0);
+ case LTC_TV_625_50:
+ return rint(samples_per_frame * 1.0 / 625.0);
+ default:
+ return 0;
+ }
+}
diff --git a/libs/libltc/ltc/decoder.h b/libs/libltc/ltc/decoder.h
new file mode 100644
index 0000000000..fc2556f8ac
--- /dev/null
+++ b/libs/libltc/ltc/decoder.h
@@ -0,0 +1,54 @@
+/*
+ libltc - en+decode linear timecode
+
+ Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "ltc/ltc.h"
+#ifndef SAMPLE_CENTER // also defined in encoder.h
+#define SAMPLE_CENTER 128 // unsigned 8 bit.
+#endif
+
+struct LTCDecoder {
+ LTCFrameExt* queue;
+ int queue_len;
+ int queue_read_off;
+ int queue_write_off;
+
+ unsigned char biphase_state;
+ unsigned char biphase_prev;
+ unsigned char snd_to_biphase_state;
+ int snd_to_biphase_cnt; ///< counts the samples in the current period
+ int snd_to_biphase_lmt; ///< specifies when a state-change is considered biphase-clock or 2*biphase-clock
+ double snd_to_biphase_period; ///< track length of a period - used to set snd_to_biphase_lmt
+
+ ltcsnd_sample_t snd_to_biphase_min;
+ ltcsnd_sample_t snd_to_biphase_max;
+
+ unsigned short decoder_sync_word;
+ LTCFrame ltc_frame;
+ int bit_cnt;
+
+ ltc_off_t frame_start_off;
+ ltc_off_t frame_start_prev;
+
+ float biphase_tics[LTC_FRAME_BIT_COUNT];
+ int biphase_tic;
+};
+
+
+void decode_ltc(LTCDecoder *d, ltcsnd_sample_t *sound, size_t size, ltc_off_t posinfo);
diff --git a/libs/libltc/ltc/encoder.h b/libs/libltc/ltc/encoder.h
new file mode 100644
index 0000000000..b815993155
--- /dev/null
+++ b/libs/libltc/ltc/encoder.h
@@ -0,0 +1,47 @@
+/*
+ libltc - en+decode linear timecode
+
+ Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "ltc/ltc.h"
+#ifndef SAMPLE_CENTER // also defined in decoder.h
+#define SAMPLE_CENTER 128 // unsigned 8 bit.
+#endif
+
+struct LTCEncoder {
+ double fps;
+ double sample_rate;
+ double filter_const;
+ int flags;
+ enum LTC_TV_STANDARD standard;
+ ltcsnd_sample_t enc_lo, enc_hi;
+
+ size_t offset;
+ size_t bufsize;
+ ltcsnd_sample_t *buf;
+
+ char state;
+
+ double samples_per_clock;
+ double samples_per_clock_2;
+ double sample_remainder;
+
+ LTCFrame f;
+};
+
+int encode_byte(LTCEncoder *e, int byte, double speed);
diff --git a/libs/libltc/ltc/ltc.h b/libs/libltc/ltc/ltc.h
new file mode 100644
index 0000000000..3ffbeeb3f6
--- /dev/null
+++ b/libs/libltc/ltc/ltc.h
@@ -0,0 +1,778 @@
+/**
+ @file ltc.h
+ @brief libltc - en+decode linear timecode
+
+ Linear (or Longitudinal) Timecode (LTC) is an encoding of
+ timecode data as a Manchester-Biphase encoded audio signal.
+ The audio signal is commonly recorded on a VTR track or other
+ storage media.
+
+ libltc facilitates decoding and encoding of LTC from/to
+ timecode, including SMPTE date support.
+
+ @author Robin Gareus <robin@gareus.org>
+ @copyright
+
+ Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
+
+ Copyright (C) 2008-2009 Jan Weiß <jan@geheimwerk.de>
+
+ Inspired by SMPTE Decoder - Maarten de Boer <mdeboer@iua.upf.es>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef LTC_H
+#define LTC_H 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stddef.h> /* size_t */
+
+#ifndef DOXYGEN_IGNORE
+/* libltc version */
+#define LIBLTC_VERSION "1.1.1"
+#define LIBLTC_VERSION_MAJOR 1
+#define LIBLTC_VERSION_MINOR 1
+#define LIBLTC_VERSION_MICRO 1
+
+/* interface revision number
+ * http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
+ */
+#define LIBLTC_CUR 11
+#define LIBLTC_REV 1
+#define LIBLTC_AGE 0
+#endif /* end DOXYGEN_IGNORE */
+
+/**
+ * default audio sample type: 8bit unsigned (mono)
+ */
+typedef unsigned char ltcsnd_sample_t;
+
+/**
+ * sample-count offset - 64bit wide
+ */
+typedef long long int ltc_off_t;
+
+#define LTC_FRAME_BIT_COUNT 80
+
+/**
+ * Raw 80 bit LTC frame
+ *
+ * The datastream for each video frame of Longitudinal Timecode consists of eighty bit-periods.
+ *
+ * At a frame-rate of 30 fps, the bit-rate corresponds to 30 [fps] * 80 [bits/f] = 2400 bits per second.
+ * The frequency for a stream of zeros would be 1.2 kHz and for a stream of ones it would be 2.4 kHz.
+ * \image html smptefmt.png
+ * With all commonly used video-frame-rates and audio-sample-rates, LTC timecode can be recorded
+ * easily into a audio-track.
+ *
+ * In each frame, 26 of the eighty bits carry the SMPTE time in binary coded decimal (BCD).
+ *
+ * These Bits are FRAME-UNITS, FRAME-TENS, SECS-UNITS, SECS-TENS, MINS-UNITS, MINS-TENS, HOURS-UNITS and HOURS-TENS.
+ * The BCD digits are loaded 'least significant bit first' (libltc takes care of the architecture specific alignment).
+ *
+ * 32 bits are assigned as eight groups of four USER-BITS (also sometimes called the "Binary Groups").
+ * This capacity is generally used to carry extra info such as reel number and/or date.
+ * The User Bits may be allocated howsoever one wishes as long as both Binary Group Flag Bits are cleared.
+ *
+ * The function \ref ltc_frame_to_time can interpret the user-bits as SMPTE Date+Timezone according to SMPTE 309M-1999.
+ * similarly \ref ltc_time_to_frame will do the reverse.
+ *
+ * The last 16 Bits make up the SYNC WORD. These bits indicate the frame boundary, the tape direction, and the bit-rate of the sync tone.
+ * The values of these Bits are fixed as 0011 1111 1111 1101
+ *
+ * The Bi-Phase Mark Phase Correction Bit (Bit 27 or 59) may be set or cleared so that that every 80-bit word
+ * contains an even number of zeroes. This means that the phase of the pulse train in every Sync Word will be the same.
+ *
+ * Bit 10 indicates drop-frame timecode.
+ * The Colour Frame Flag col.frm is Bit 11; if the timecode intentionally synchronized to a colour TV field sequence, this bit is set.
+ *
+ * Bit 58 is not required for the BCD count for HOURS-TENS (which has a maximum value of two)
+ * and has not been given any other special purpose so remains unassigned.
+ * This Bit has been RESERVED for future assignment.
+ *
+ * The Binary Group Flag Bits (bits 43 and 59) are two bits indicate the format of the User Bits data.
+ * SMPTE 12M-1999 defines the previously reserved bit 58 to signals that the time is locked to wall-clock
+ * within a tolerance of ± 0.5 seconds.
+ *
+ * SMPTE 12M-1999 also changes the numbering schema of the BGF. (BGF1 was renamed to BGF2 and bit 58 becomes BGFB1)
+ *
+ * To further complicate matters, the BGFB assignment as well as the biphase_mark_phase_correction (aka parity)
+ * bit depends on the timecode-format used.
+ *
+ * <pre>
+ * 25 fps 24, 30 fps
+ * BGF0 27 43
+ * BGF1 58 58
+ * BGF2 43 59
+ * Parity 59 27
+ * </pre>
+ *
+ * The variable naming chosen for the LTCFrame struct is based on the 24,30 fps standard.
+ *
+ * The Binary Group Flag Bits should be used only as shown in the truth table below.
+ * The Unassigned entries in the table should not be used, as they may be allocated specific meanings in the future.
+ *
+ * <pre>
+ * BGF0 BGF1 BGF2
+ * user-bits timecode Bit 43 Bit 58 Bit 59 (30fps, 24 fps)
+ * | | Bit 27 Bit 58 Bit 43 (25fps)
+ * No User Bits format specified | ? | 0 0 0
+ * Eight-bit character set (1) | ? | 1 0 0
+ * Date and Timezone set | ? | 0 0 1
+ * Page/Line multiplex (2) | ? | 1 0 1
+ * Character set not specified | clk | 0 1 0
+ * Reserved | ? | 1 1 0
+ * Date and Timezone set | clk | 0 1 1
+ * Page/Line multiplex (2) | clk | 1 1 1
+ *
+ * </pre>
+ *
+ * (1) ISO/IEC 646 or ISO/IEC 2022 character set.
+ * If the seven-bit ISO codes are being used, they shall be converted to
+ * eight-bit codes by setting the eighth bit to zero. 4 ISO codes can be encoded,
+ * user7 and user8 are to be used for the first code with LSB 7 and MSB in 8.
+ * the remaining ISO codes are to be distributed in the same manner to
+ * user5/6 user3/4 and user1/2 accordingly.
+ *
+ * (2) The Page/Line indicates ANSI/SMPTE-262M is used for the user-bits. It is multiplex system that
+ * can be used to encode large amounts of data in the binary groups through the use of time multiplexing.
+ *
+ * libltc does not use any of the BGF - except for the Parity bit which can be calculated and set with
+ * \ref ltc_frame_set_parity. Setting and interpreting the BGF is left to the application using libltc.
+ * However libltc provides functionality to parse or set date and timezoe according to SMPTE 309M-1999.
+ *
+ * further information: http://www.philrees.co.uk/articles/timecode.htm
+ * and http://www.barney-wol.net/time/timecode.html
+ */
+#if (defined __BIG_ENDIAN__ && !defined DOXYGEN_IGNORE)
+// Big Endian version, bytes are "upside down"
+struct LTCFrame {
+ unsigned int user1:4;
+ unsigned int frame_units:4;
+
+ unsigned int user2:4;
+ unsigned int col_frame:1;
+ unsigned int dfbit:1;
+ unsigned int frame_tens:2;
+
+ unsigned int user3:4;
+ unsigned int secs_units:4;
+
+ unsigned int user4:4;
+ unsigned int biphase_mark_phase_correction:1;
+ unsigned int secs_tens:3;
+
+ unsigned int user5:4;
+ unsigned int mins_units:4;
+
+ unsigned int user6:4;
+ unsigned int binary_group_flag_bit0:1;
+ unsigned int mins_tens:3;
+
+ unsigned int user7:4;
+ unsigned int hours_units:4;
+
+ unsigned int user8:4;
+ unsigned int binary_group_flag_bit2:1;
+ unsigned int binary_group_flag_bit1:1;
+ unsigned int hours_tens:2;
+
+ unsigned int sync_word:16;
+};
+#else
+/* Little Endian version -- and doxygen doc */
+struct LTCFrame {
+ unsigned int frame_units:4; ///< SMPTE framenumber BCD unit 0..9
+ unsigned int user1:4;
+
+ unsigned int frame_tens:2; ///< SMPTE framenumber BCD tens 0..3
+ unsigned int dfbit:1; ///< indicated drop-frame timecode
+ unsigned int col_frame:1; ///< colour-frame: timecode intentionally synchronized to a colour TV field sequence
+ unsigned int user2:4;
+
+ unsigned int secs_units:4; ///< SMPTE seconds BCD unit 0..9
+ unsigned int user3:4;
+
+ unsigned int secs_tens:3; ///< SMPTE seconds BCD tens 0..6
+ unsigned int biphase_mark_phase_correction:1; ///< see note on Bit 27 in description and \ref ltc_frame_set_parity .
+ unsigned int user4:4;
+
+ unsigned int mins_units:4; ///< SMPTE minutes BCD unit 0..9
+ unsigned int user5:4;
+
+ unsigned int mins_tens:3; ///< SMPTE minutes BCD tens 0..6
+ unsigned int binary_group_flag_bit0:1; ///< indicate user-data char encoding, see table above - bit 43
+ unsigned int user6:4;
+
+ unsigned int hours_units:4; ///< SMPTE hours BCD unit 0..9
+ unsigned int user7:4;
+
+ unsigned int hours_tens:2; ///< SMPTE hours BCD tens 0..2
+ unsigned int binary_group_flag_bit1:1; ///< indicate timecode is local time wall-clock, see table above - bit 58
+ unsigned int binary_group_flag_bit2:1; ///< indicate user-data char encoding (or parity with 25fps), see table above - bit 59
+ unsigned int user8:4;
+
+ unsigned int sync_word:16;
+};
+#endif
+
+/** the standard defines the assignment of the binary-group-flag bits
+ * basically only 25fps is different, but other standards defined in
+ * the SMPTE spec have been included for completeness.
+ */
+enum LTC_TV_STANDARD {
+ LTC_TV_525_60, ///< 30fps
+ LTC_TV_625_50, ///< 25fps
+ LTC_TV_1125_60,///< 30fps
+ LTC_TV_FILM_24 ///< 24fps
+};
+
+/** encoder and LTCframe <> timecode operation flags */
+enum LTC_BG_FLAGS {
+ LTC_USE_DATE = 1, ///< LTCFrame <> SMPTETimecode converter and LTCFrame increment/decrement use date, also set BGF2 to '1' when encoder is initialized or re-initialized (unless LTC_BGF_DONT_TOUCH is given)
+ LTC_TC_CLOCK = 2,///< the Timecode is wall-clock aka freerun. This also sets BGF1 (unless LTC_BGF_DONT_TOUCH is given)
+ LTC_BGF_DONT_TOUCH = 4, ///< encoder init or re-init does not touch the BGF bits (initial values after initialization is zero)
+ LTC_NO_PARITY = 8 ///< parity bit is left untouched when setting or in/decrementing the encoder frame-number
+};
+
+/**
+ * see LTCFrame
+ */
+typedef struct LTCFrame LTCFrame;
+
+/**
+ * Extended LTC frame - includes audio-sample position offsets, volume, etc
+ *
+ * Note: For TV systems, the sample in the LTC audio data stream where the LTC Frame starts is not neccesarily at the same time
+ * as the video-frame which is described by the LTC Frame.
+ *
+ * \ref off_start denotes the time of the first transition of bit 0 in the LTC frame.
+ *
+ * For 525/60 Television systems, the first transition shall occur at the beginning of line 5 of the frame with which it is
+ * associated. The tolerance is ± 1.5 lines.
+ *
+ * For 625/50 systems, the first transition shall occur at the beginning of line 2 ± 1.5 lines of the frame with which it is associated.
+ *
+ * Only for 1125/60 systems, the first transition occurs exactly at the vertical sync timing reference of the frame. ± 1 line.
+ *
+ */
+struct LTCFrameExt {
+ LTCFrame ltc; ///< the actual LTC frame. see \ref LTCFrame
+ ltc_off_t off_start; ///< \anchor off_start the approximate sample in the stream corresponding to the start of the LTC frame.
+ ltc_off_t off_end; ///< \anchor off_end the sample in the stream corresponding to the end of the LTC frame.
+ int reverse; ///< if non-zero, a reverse played LTC frame was detected. Since the frame was reversed, it started at off_end and finishes as off_start (off_end > off_start). (Note: in reverse playback the (reversed) sync-word of the next/previous frame is detected, this offset is corrected).
+ float biphase_tics[LTC_FRAME_BIT_COUNT]; ///< detailed timing info: phase of the LTC signal; the time between each bit in the LTC-frame in audio-frames. Summing all 80 values in the array will yield audio-frames/LTC-frame = (\ref off_end - \ref off_start + 1).
+ ltcsnd_sample_t sample_min; ///< the minimum input sample signal for this frame (0..255)
+ ltcsnd_sample_t sample_max; ///< the maximum input sample signal for this frame (0..255)
+ double volume; ///< the volume of the input signal in dbFS
+};
+
+/**
+ * see \ref LTCFrameExt
+ */
+typedef struct LTCFrameExt LTCFrameExt;
+
+/**
+ * Human readable time representation, decimal values.
+ */
+struct SMPTETimecode {
+ char timezone[6]; ///< the timezone 6bytes: "+HHMM" textual representation
+ unsigned char years; ///< LTC-date uses 2-digit year 00.99
+ unsigned char months; ///< valid months are 1..12
+ unsigned char days; ///< day of month 1..31
+
+ unsigned char hours; ///< hour 0..23
+ unsigned char mins; ///< minute 0..60
+ unsigned char secs; ///< second 0..60
+ unsigned char frame; ///< sub-second frame 0..(FPS - 1)
+};
+
+/**
+ * see \ref SMPTETimecode
+ */
+typedef struct SMPTETimecode SMPTETimecode;
+
+
+/**
+ * Opaque structure
+ * see: \ref ltc_decoder_create, \ref ltc_decoder_free
+ */
+typedef struct LTCDecoder LTCDecoder;
+
+/**
+ * Opaque structure
+ * see: \ref ltc_encoder_create, \ref ltc_encoder_free
+ */
+typedef struct LTCEncoder LTCEncoder;
+
+/**
+ * Convert binary LTCFrame into SMPTETimecode struct
+ *
+ * @param stime output
+ * @param frame input
+ * @param flags binary combination of \ref LTC_BG_FLAGS - here only LTC_USE_DATE is relevant.
+ * if LTC_USE_DATE is set, the user-fields in LTCFrame will be parsed into the date variable of SMPTETimecode.
+ * otherwise the date information in the SMPTETimecode is set to zero.
+ */
+void ltc_frame_to_time(SMPTETimecode* stime, LTCFrame* frame, int flags);
+
+/**
+ * Translate SMPTETimecode struct into its binary LTC representation
+ * and set the LTC frame's parity bit accordingly (see \ref ltc_frame_set_parity)
+ *
+ * @param frame output - the frame to be set
+ * @param stime input - timecode input
+ * @param standard the TV standard to use for parity bit assignment
+ * @param flags binary combination of \ref LTC_BG_FLAGS - here only LTC_USE_DATE and LTC_NO_PARITY are relevant.
+ * if LTC_USE_DATE is given, user-fields in LTCFrame will be set from the date in SMPTETimecode,
+ * otherwise the user-bits are not modified. All non-timecode fields remain untouched - except for the parity bit
+ * unless LTC_NO_PARITY is given.
+ */
+void ltc_time_to_frame(LTCFrame* frame, SMPTETimecode* stime, enum LTC_TV_STANDARD standard, int flags);
+
+/**
+ * Reset all values of a LTC FRAME to zero, except for the sync-word (0x3FFD) at the end.
+ * The sync word is set according to architecture (big/little endian).
+ * Also set the Frame's parity bit accordingly (see \ref ltc_frame_set_parity)
+ * @param frame the LTCFrame to reset
+ */
+void ltc_frame_reset(LTCFrame* frame);
+
+/**
+ * Increment the timecode by one Frame (1/framerate seconds)
+ * and set the Frame's parity bit accordingly (see \ref ltc_frame_set_parity)
+ *
+ * @param frame the LTC-timecode to increment
+ * @param fps integer framerate (for drop-frame-timecode set frame->dfbit and round-up the fps).
+ * @param standard the TV standard to use for parity bit assignment
+ * if set to 1 the 25fps standard is enabled and LTC Frame bit 59 instead of 27 is used for the parity. It only has only has effect flag bit 4 (LTC_NO_PARITY) is cleared.
+ * @param flags binary combination of \ref LTC_BG_FLAGS - here only LTC_USE_DATE and LTC_NO_PARITY are relevant.
+ * If the bit 0 (1) is set (1) interpret user-data as date and increment date if timecode wraps after 24h.
+ * (Note: leap-years are taken into account, but since the year is two-digit only, the 100,400yr rules are ignored.
+ * "00" is assumed to be year 2000 which was a leap year.)
+ * @return 1 if timecode was wrapped around after 23:59:59:ff, 0 otherwise
+ */
+int ltc_frame_increment(LTCFrame* frame, int fps, enum LTC_TV_STANDARD standard, int flags);
+
+/**
+ * Decrement the timecode by one Frame (1/framerate seconds)
+ * and set the Frame's parity bit accordingly (see \ref ltc_frame_set_parity)
+ *
+ * @param frame the LTC-timecode to decrement
+ * @param fps integer framerate (for drop-frame-timecode set frame->dfbit and round-up the fps).
+ * @param standard the TV standard to use for parity bit assignment
+ * if set to 1 the 25fps standard is enabled and LTC Frame bit 59 instead of 27 is used for the parity. It only has only has effect flag bit 4 (LTC_NO_PARITY) is cleared.
+ * @param flags binary combination of \ref LTC_BG_FLAGS - here only LTC_USE_DATE and LTC_NO_PARITY are relevant.
+ * if the bit 0 is set (1) interpret user-data as date and decrement date if timecode wraps at 24h.
+ * (Note: leap-years are taken into account, but since the year is two-digit only, the 100,400yr rules are ignored.
+ * "00" is assumed to be year 2000 which was a leap year.)
+ * bit 3 (8) indicates that the parity bit should not be touched
+ * @return 1 if timecode was wrapped around at 23:59:59:ff, 0 otherwise
+ */
+int ltc_frame_decrement(LTCFrame* frame, int fps, enum LTC_TV_STANDARD standard, int flags);
+
+/**
+ * Create a new LTC decoder.
+ *
+ * @param apv audio-frames per video frame. This is just used for initial settings, the speed is tracked dynamically. setting this in the right ballpark is needed to properly decode the first LTC frame in a sequence.
+ * @param queue_size length of the internal queue to store decoded frames
+ * to SMPTEDecoderWrite.
+ * @return decoder handle or NULL if out-of-memory
+ */
+LTCDecoder * ltc_decoder_create(int apv, int queue_size);
+
+
+/**
+ * Release memory of decoder.
+ * @param d decoder handle
+ */
+int ltc_decoder_free(LTCDecoder *d);
+
+/**
+ * Feed the LTC decoder with new audio samples.
+ *
+ * Parse raw audio for LTC timestamps. Once a complete LTC frame has been
+ * decoded it is pushed into a queue (\ref ltc_decoder_read)
+ *
+ * @param d decoder handle
+ * @param buf pointer to ltcsnd_sample_t - unsigned 8 bit mono audio data
+ * @param size \anchor size number of samples to parse
+ * @param posinfo (optional, recommended) sample-offset in the audio-stream. It is added to \ref off_start, \ref off_end in \ref LTCFrameExt and should be monotonic (ie incremented by \ref size for every call to ltc_decoder_write)
+ */
+void ltc_decoder_write(LTCDecoder *d,
+ ltcsnd_sample_t *buf, size_t size,
+ ltc_off_t posinfo);
+
+/**
+ * Wrapper around \ref ltc_decoder_write that accepts floating point
+ * audio samples. Note: internally libltc uses 8 bit only.
+ *
+ * @param d decoder handle
+ * @param buf pointer to audio sample data
+ * @param size number of samples to parse
+ * @param posinfo (optional, recommended) sample-offset in the audio-stream.
+ */
+void ltc_decoder_write_float(LTCDecoder *d, float *buf, size_t size, ltc_off_t posinfo);
+
+/**
+ * Wrapper around \ref ltc_decoder_write that accepts signed 16 bit
+ * audio samples. Note: internally libltc uses 8 bit only.
+ *
+ * @param d decoder handle
+ * @param buf pointer to audio sample data
+ * @param size number of samples to parse
+ * @param posinfo (optional, recommended) sample-offset in the audio-stream.
+ */
+void ltc_decoder_write_s16(LTCDecoder *d, short *buf, size_t size, ltc_off_t posinfo);
+
+/**
+ * Wrapper around \ref ltc_decoder_write that accepts unsigned 16 bit
+ * audio samples. Note: internally libltc uses 8 bit only.
+ *
+ * @param d decoder handle
+ * @param buf pointer to audio sample data
+ * @param size number of samples to parse
+ * @param posinfo (optional, recommended) sample-offset in the audio-stream.
+ */
+void ltc_decoder_write_u16(LTCDecoder *d, short *buf, size_t size, ltc_off_t posinfo);
+
+/**
+ * Decoded LTC frames are placed in a queue. This function retrieves
+ * a frame from the queue, and stores it at LTCFrameExt*
+ *
+ * @param d decoder handle
+ * @param frame the decoded LTC frame is copied there
+ * @return 1 on success or 0 when no frames queued.
+ */
+int ltc_decoder_read(LTCDecoder *d, LTCFrameExt *frame);
+
+/**
+ * Remove all LTC frames from the internal queue.
+ * @param d decoder handle
+ */
+void ltc_decoder_queue_flush(LTCDecoder* d);
+
+/**
+ * Count number of LTC frames currently in the queue.
+ * @param d decoder handle
+ * @return number of queued frames
+ */
+int ltc_decoder_queue_length(LTCDecoder* d);
+
+
+
+/**
+ * Allocate and initialize LTC audio encoder.
+ *
+ * calls \ref ltc_encoder_reinit internally see, see notes there.
+ *
+ * @param sample_rate audio sample rate (eg. 48000)
+ * @param fps video-frames per second (e.g. 25.0)
+ * @param standard the TV standard to use for Binary Group Flag bit position
+ * @param flags binary combination of \ref LTC_BG_FLAGS
+ */
+LTCEncoder* ltc_encoder_create(double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags);
+
+/**
+ * Release memory of the encoder.
+ * @param e encoder handle
+ */
+void ltc_encoder_free(LTCEncoder *e);
+
+/**
+ * Set the encoder LTC-frame to the given SMPTETimecode.
+ * The next call to \ref ltc_encoder_encode_byte or
+ * \ref ltc_encoder_encode_frame will encode this time to LTC audio-samples.
+ *
+ * Internally this call uses \ref ltc_time_to_frame because
+ * the LTCEncoder operates on LTCframes only.
+ * see als \ref ltc_encoder_set_frame
+ *
+ * @param e encoder handle
+ * @param t timecode to set.
+ */
+void ltc_encoder_set_timecode(LTCEncoder *e, SMPTETimecode *t);
+
+/**
+ * Query the current encoder timecode.
+ *
+ * Note: the decoder stores its internal state in an LTC-frame,
+ * this function converts that LTC-Frame into SMPTETimecode on demand.
+ * see also \ref ltc_encoder_get_frame.
+ *
+ * @param e encoder handle
+ * @param t is set to current timecode
+ */
+void ltc_encoder_get_timecode(LTCEncoder *e, SMPTETimecode *t);
+
+/**
+ * Move the encoder to the next timecode frame.
+ * uses \ref ltc_frame_increment() internally.
+ */
+int ltc_encoder_inc_timecode(LTCEncoder *e);
+
+/**
+ * Move the encoder to the previous timecode frame.
+ * This is useful for encoding reverse LTC.
+ * uses \ref ltc_frame_decrement() internally.
+ */
+int ltc_encoder_dec_timecode(LTCEncoder *e);
+
+/**
+ * Low-level access to the internal LTCFrame data.
+ *
+ * Note: be careful to about f->dfbit, the encoder sets this [only] upon
+ * initialization.
+ *
+ * @param e encoder handle
+ * @param f LTC frame data to use
+ */
+void ltc_encoder_set_frame(LTCEncoder *e, LTCFrame *f);
+
+/**
+ * Low-level access to the encoder internal LTCFrame data
+ *
+ * @param e encoder handle
+ * @param f return LTC frame data
+ */
+void ltc_encoder_get_frame(LTCEncoder *e, LTCFrame *f);
+
+/**
+ * Copy the accumulated encoded audio to the given
+ * sample-buffer and flush the internal buffer.
+ *
+ * @param e encoder handle
+ * @param buf place to store the audio-samples, needs to be large enough
+ * to hold \ref ltc_encoder_get_buffersize bytes
+ * @return the number of bytes written to the memory area
+ * pointed to by buf.
+ */
+int ltc_encoder_get_buffer(LTCEncoder *e, ltcsnd_sample_t *buf);
+
+
+/**
+ * Retrieve a pointer to the accumulated encoded audio-data.
+ *
+ * @param e encoder handle
+ * @param size if set, the number of valid bytes in the buffer is stored there
+ * @param flush call \ref ltc_encoder_buffer_flush - reset the buffer write-pointer
+ * @return pointer to encoder-buffer
+ */
+ltcsnd_sample_t *ltc_encoder_get_bufptr(LTCEncoder *e, int *size, int flush);
+
+/**
+ * reset the write-pointer of the encoder-buffer
+ * @param e encoder handle
+ */
+void ltc_encoder_buffer_flush(LTCEncoder *e);
+
+/**
+ * Query the length of the internal buffer. It is allocated
+ * to hold audio-frames for exactly one LTC frame for the given
+ * sample-rate and frame-rate. ie. (1 + sample-rate / fps) bytes
+ *
+ * Note this returns the total size of the buffer, not the used/free
+ * part. See also \ref ltc_encoder_get_bufptr
+ *
+ * @param e encoder handle
+ * @return size of the allocated internal buffer.
+ */
+size_t ltc_encoder_get_buffersize(LTCEncoder *e);
+
+/**
+ * Change the encoder settings without re-allocating any
+ * library internal data structure (realtime safe).
+ * changing the fps and or sample-rate implies a buffer flush,
+ * and biphase state reset.
+ *
+ * This call will fail if the internal buffer is too small
+ * to hold one full LTC frame. Use \ref ltc_encoder_set_bufsize to
+ * prepare an internal buffer large enough to accommodate all
+ * sample_rate, fps combinations that you would like to re-init to.
+ *
+ * The LTC frame payload data is not modified by this call, however,
+ * the flag-bits of the LTC-Frame are updated:
+ * If fps equals to 29.97 or 30000.0/1001.0, the LTCFrame's 'dfbit' bit is set to 1
+ * to indicate drop-frame timecode.
+ *
+ * Unless the LTC_BGF_DONT_TOUCH flag is set the BGF1 is set or cleared depending
+ * on LTC_TC_CLOCK and BGF0,2 according to LTC_USE_DATE and the given standard.
+ * col_frame is cleared and the parity recomputed (unless LTC_NO_PARITY is given).
+ *
+ * @param e encoder handle
+ * @param sample_rate audio sample rate (eg. 48000)
+ * @param fps video-frames per second (e.g. 25.0)
+ * @param standard the TV standard to use for Binary Group Flag bit position
+ * @param flags binary combination of \ref LTC_BG_FLAGS
+ */
+int ltc_encoder_reinit(LTCEncoder *e, double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags);
+
+/**
+ * reset ecoder state.
+ * flushes buffer, reset biphase state
+ *
+ * @param e encoder handle
+ */
+void ltc_encoder_reset(LTCEncoder *e);
+
+/**
+ * Configure a custom size for the internal buffer.
+ *
+ * This is needed if you are planning to call \ref ltc_encoder_reinit()
+ * or if you want to keep more than one LTC frame's worth of data in
+ * the library's internal buffer.
+ *
+ * The buffer-size is (1 + sample_rate / fps) bytes.
+ * resizing the internal buffer will flush all existing data
+ * in it - alike \ref ltc_encoder_buffer_flush.
+ *
+ * @param e encoder handle
+ * @param sample_rate audio sample rate (eg. 48000)
+ * @param fps video-frames per second (e.g. 25.0)
+ * @return 0 on success, -1 if allocation fails (which makes the
+ * encoder unusable, call \ref ltc_encoder_free or realloc the buffer)
+ */
+int ltc_encoder_set_bufsize(LTCEncoder *e, double sample_rate, double fps);
+
+/**
+ * Set the volume of the generated LTC signal
+ *
+ * typically LTC is sent at 0dBu ; in EBU callibrated systems that
+ * corresponds to -18dBFS. - by default libltc creates -3dBFS
+ *
+ * since libltc generated 8bit audio-data, the minium dBFS
+ * is about -42dB which corresponds to 1 bit.
+ *
+ * 0dB corresponds to a signal range of 127
+ * 1..255 with 128 at the center.
+ *
+ * @param e encoder handle
+ * @param dBFS the volume in dB full-scale (<= 0.0)
+ * @return 0 on success, -1 if the value was out of range
+ */
+int ltc_encoder_set_volume(LTCEncoder *e, double dBFS);
+
+/**
+ * Set encoder signal rise-time / signal filtering
+ *
+ * LTC signal should have a rise time of 40us +/- 10 us.
+ * by default the encoder honors this and low-pass filters
+ * the output depending on the sample-rate.
+ *
+ * If you want a perfect square wave, set 'rise_time' to 0.
+ *
+ * Note \ref ltc_encoder_reinit resets the filter-time-constant to use
+ * the default 40us for the given sample-rate, overriding any value
+ * previously set with \ref ltc_encoder_set_filter
+ *
+ * @param e encoder handle
+ * @param rise_time the signal rise-time in us (10^(-6) sec), set to 0 for perfect square wave, default 40.0
+ */
+void ltc_encoder_set_filter(LTCEncoder *e, double rise_time);
+
+/**
+ * Generate LTC audio for given byte of the LTC-frame and
+ * place it into the internal buffer.
+ *
+ * see \ref ltc_encoder_get_buffer and \ref ltc_encoder_get_bufptr
+ *
+ * LTC has 10 bytes per frame: 0 <= bytecnt < 10
+ * use SMPTESetTime(..) to set the current frame before Encoding.
+ * see tests/encoder.c for an example.
+ *
+ * The default output signal is @ -3dBFS (38..218 at 8 bit unsigned).
+ * see also \ref ltc_encoder_set_volume
+ *
+ * if speed is < 0, the bits are encoded in reverse.
+ * slowdown > 10.0 requires custom buffer sizes; see \ref ltc_encoder_set_bufsize
+ *
+ * @param e encoder handle
+ * @param byte byte of the LTC-frame to encode 0..9
+ * @param speed vari-speed, < 1.0 faster, > 1.0 slower ; must be != 0
+ *
+ * @return 0 on success, -1 if byte is invalid or buffer overflow (speed > 10.0)
+ */
+int ltc_encoder_encode_byte(LTCEncoder *e, int byte, double speed);
+
+/**
+ * Encode a full LTC frame at fixed speed.
+ * This is equivalent to calling \ref ltc_encoder_encode_byte 10 times for
+ * bytes 0..9 with speed 1.0.
+ *
+ * Note: The internal buffer must be empty before calling this function.
+ * Otherwise it may overflow. This is usually the case if it is read with
+ * \ref ltc_encoder_get_buffer after calling this function.
+ *
+ * The default internal buffersize is exactly one full LTC frame at speed 1.0.
+ *
+ * @param e encoder handle
+ */
+void ltc_encoder_encode_frame(LTCEncoder *e);
+
+/**
+ * Set the parity of the LTC frame.
+ *
+ * Bi-Phase Mark Phase Correction bit (bit 27 - or 59) may be set or cleared so that
+ * that every 80-bit word contains an even number of zeroes.
+ * This means that the phase in every Sync Word will be the same.
+ *
+ * This is merely cosmetic; the motivation to keep the polarity of the waveform
+ * constant is to make finding the Sync Word visibly (on a scope) easier.
+ *
+ * There is usually no need to call this function directly. The encoder utility
+ * functions \ref ltc_time_to_frame, \ref ltc_frame_increment and
+ * \ref ltc_frame_decrement include a call to it.
+ *
+ * @param frame the LTC to analyze and set or clear the biphase_mark_phase_correction bit.
+ * @param standard If 1 (aka LTC_TV_625_50) , the 25fps mode (bit 59 - aka binary_group_flag_bit2) is used, otherwise the 30fps, 24fps mode (bit 27 -- biphase_mark_phase_correction) is set or cleared.
+ */
+void ltc_frame_set_parity(LTCFrame *frame, enum LTC_TV_STANDARD standard);
+
+/**
+ * Parse Binary Group Flags into standard independent format:
+ * bit 0 (1) - BGF 0,
+ * bit 1 (2) - BGF 1,
+ * bit 2 (4) - BGF 2
+ *
+ * @param f LTC frame data analyze
+ * @param standard the TV standard to use -- see \ref LTCFrame for BGF assignment
+ * @return LTC Binary Group Flags
+ */
+int parse_bcg_flags(LTCFrame *f, enum LTC_TV_STANDARD standard);
+
+/**
+ * LTCFrame sample alignment offset.
+ *
+ * There is a relative offset of the LTC-Frame start and the TV-frame.
+ * The first bit of a LTC frame corresponds to a specific line in the actual video
+ * frame. When decoding this offset needs to be subtracted from the LTC-frame's
+ * audio-sample-time to match the TV-frame's start position.
+ *
+ * For film frames or HDV the offset is zero.
+ *
+ * @param samples_per_frame audio-samples per timecode-frame (eg. 1920 = 48000/25)
+ * @param standard the TV standard
+ * @return offset in samples
+ */
+ltc_off_t ltc_frame_alignment(double samples_per_frame, enum LTC_TV_STANDARD standard);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libs/libltc/timecode.c b/libs/libltc/timecode.c
new file mode 100644
index 0000000000..83e8e1926b
--- /dev/null
+++ b/libs/libltc/timecode.c
@@ -0,0 +1,451 @@
+/*
+ libltc - en+decode linear timecode
+
+ Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library.
+ If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ltc/ltc.h"
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/**
+ * SMPTE Timezones
+ */
+struct SMPTETimeZonesStruct {
+ unsigned char code; //actually 6 bit!
+ char timezone[6];
+};
+
+/**
+ * SMPTE Timezone codes as per http://www.barney-wol.net/time/timecode.html
+ */
+static const struct SMPTETimeZonesStruct smpte_time_zones[] =
+{
+ /* code, timezone (UTC+) //Standard time //Daylight saving */
+ { 0x00, "+0000" /* Greenwich */ /* - */ },
+ { 0x00, "-0000" /* Greenwich */ /* - */ },
+ { 0x01, "-0100" /* Azores */ /* - */ },
+ { 0x02, "-0200" /* Mid-Atlantic */ /* - */ },
+ { 0x03, "-0300" /* Buenos Aires */ /* Halifax */ },
+ { 0x04, "-0400" /* Halifax */ /* New York */ },
+ { 0x05, "-0500" /* New York */ /* Chicago */ },
+ { 0x06, "-0600" /* Chicago Denver */ /* - */ },
+ { 0x07, "-0700" /* Denver */ /* Los Angeles */ },
+ { 0x08, "-0800" /* Los Angeles */ /* - */ },
+ { 0x09, "-0900" /* Alaska */ /* - */ },
+ { 0x10, "-1000" /* Hawaii */ /* - */ },
+ { 0x11, "-1100" /* Midway Island */ /* - */ },
+ { 0x12, "-1200" /* Kwaialein */ /* - */ },
+ { 0x13, "+1300" /* - */ /* New Zealand */ },
+ { 0x14, "+1200" /* New Zealand */ /* - */ },
+ { 0x15, "+1100" /* Solomon Islands */ /* - */ },
+ { 0x16, "+1000" /* Guam */ /* - */ },
+ { 0x17, "+0900" /* Tokyo */ /* - */ },
+ { 0x18, "+0800" /* Beijing */ /* - */ },
+ { 0x19, "+0700" /* Bangkok */ /* - */ },
+ { 0x20, "+0600" /* Dhaka */ /* - */ },
+ { 0x21, "+0500" /* Islamabad */ /* - */ },
+ { 0x22, "+0400" /* Abu Dhabi */ /* - */ },
+ { 0x23, "+0300" /* Moscow */ /* - */ },
+ { 0x24, "+0200" /* Eastern Europe */ /* - */ },
+ { 0x25, "+0100" /* Central Europe */ /* - */ },
+/* { 0x26, "Undefined" Reserved; do not use },*/
+/* { 0x27, "Undefined" Reserved; do not use },*/
+ { 0x28, "TP-03" /* Time precision class 3 */ /* - */ },
+ { 0x29, "TP-02" /* Time precision class 2 */ /* - */ },
+ { 0x30, "TP-01" /* Time precision class 1 */ /* - */ },
+ { 0x31, "TP-00" /* Time precision class 0 */ /* - */ },
+ { 0x0A, "-0030" /* - */ /* - */ },
+ { 0x0B, "-0130" /* - */ /* - */ },
+ { 0x0C, "-0230" /* - */ /* Newfoundland */ },
+ { 0x0D, "-0330" /* Newfoundland */ /* - */ },
+ { 0x0E, "-0430" /* - */ /* - */ },
+ { 0x0F, "-0530" /* - */ /* - */ },
+ { 0x1A, "-0630" /* - */ /* - */ },
+ { 0x1B, "-0730" /* - */ /* - */ },
+ { 0x1C, "-0830" /* - */ /* - */ },
+ { 0x1D, "-0930" /* Marquesa Islands */ /* - */ },
+ { 0x1E, "-1030" /* - */ /* - */ },
+ { 0x1F, "-1130" /* - */ /* - */ },
+ { 0x2A, "+1130" /* Norfolk Island */ /* - */ },
+ { 0x2B, "+1030" /* Lord Howe Is. */ /* - */ },
+ { 0x2C, "+0930" /* Darwin */ /* - */ },
+ { 0x2D, "+0830" /* - */ /* - */ },
+ { 0x2E, "+0730" /* - */ /* - */ },
+ { 0x2F, "+0630" /* Rangoon */ /* - */ },
+ { 0x3A, "+0530" /* Bombay */ /* - */ },
+ { 0x3B, "+0430" /* Kabul */ /* - */ },
+ { 0x3C, "+0330" /* Tehran */ /* - */ },
+ { 0x3D, "+0230" /* - */ /* - */ },
+ { 0x3E, "+0130" /* - */ /* - */ },
+ { 0x3F, "+0030" /* - */ /* - */ },
+ { 0x32, "+1245" /* Chatham Island */ /* - */ },
+/* { 0x33, "Undefined" Reserved; do not use },*/
+/* { 0x34, "Undefined" Reserved; do not use },*/
+/* { 0x35, "Undefined" Reserved; do not use },*/
+/* { 0x36, "Undefined" Reserved; do not use },*/
+/* { 0x37, "Undefined" Reserved; do not use },*/
+ { 0x38, "+XXXX" /* User defined time offset */ /* - */ },
+/* { 0x39, "Undefined" Unknown Unknown },*/
+/* { 0x39, "Undefined" Unknown Unknown },*/
+
+ { 0xFF, "" /* The End */ }
+};
+
+static void smpte_set_timezone_string(LTCFrame *frame, SMPTETimecode *stime) {
+ int i = 0;
+
+ const unsigned char code = frame->user7 + (frame->user8 << 4);
+
+ char timezone[6] = "+0000";
+
+ for (i = 0 ; smpte_time_zones[i].code != 0xFF ; i++) {
+ if ( smpte_time_zones[i].code == code ) {
+ strcpy(timezone, smpte_time_zones[i].timezone);
+ break;
+ }
+ }
+ strcpy(stime->timezone, timezone);
+}
+
+static void smpte_set_timezone_code(SMPTETimecode *stime, LTCFrame *frame) {
+ int i = 0;
+ unsigned char code = 0x00;
+
+ // Find code for timezone string
+ // Primitive search
+ for (i=0; smpte_time_zones[i].code != 0xFF; i++) {
+ if ( (strcmp(smpte_time_zones[i].timezone, stime->timezone)) == 0 ) {
+ code = smpte_time_zones[i].code;
+ break;
+ }
+ }
+
+ frame->user7 = code & 0x0F;
+ frame->user8 = (code & 0xF0) >> 4;
+}
+
+/** Drop-frame support function
+ * We skip the first two frame numbers (0 and 1) at the beginning of each minute,
+ * except for minutes 0, 10, 20, 30, 40, and 50
+ * (i.e. we skip frame numbers at the beginning of minutes for which mins_units is not 0).
+ */
+static void skip_drop_frames(LTCFrame* frame) {
+ if ((frame->mins_units != 0)
+ && (frame->secs_units == 0)
+ && (frame->secs_tens == 0)
+ && (frame->frame_units == 0)
+ && (frame->frame_tens == 0)
+ ) {
+ frame->frame_units += 2;
+ }
+}
+
+void ltc_frame_to_time(SMPTETimecode *stime, LTCFrame *frame, int flags) {
+ if (!stime) return;
+
+ if (flags & LTC_USE_DATE) {
+ smpte_set_timezone_string(frame, stime);
+
+ stime->years = frame->user5 + frame->user6*10;
+ stime->months = frame->user3 + frame->user4*10;
+ stime->days = frame->user1 + frame->user2*10;
+ } else {
+ stime->years = 0;
+ stime->months = 0;
+ stime->days = 0;
+ sprintf(stime->timezone,"+0000");
+ }
+
+ stime->hours = frame->hours_units + frame->hours_tens*10;
+ stime->mins = frame->mins_units + frame->mins_tens*10;
+ stime->secs = frame->secs_units + frame->secs_tens*10;
+ stime->frame = frame->frame_units + frame->frame_tens*10;
+}
+
+void ltc_time_to_frame(LTCFrame* frame, SMPTETimecode* stime, enum LTC_TV_STANDARD standard, int flags) {
+ if (flags & LTC_USE_DATE) {
+ smpte_set_timezone_code(stime, frame);
+ frame->user6 = stime->years/10;
+ frame->user5 = stime->years - frame->user6*10;
+ frame->user4 = stime->months/10;
+ frame->user3 = stime->months - frame->user4*10;
+ frame->user2 = stime->days/10;
+ frame->user1 = stime->days - frame->user2*10;
+ }
+
+ frame->hours_tens = stime->hours/10;
+ frame->hours_units = stime->hours - frame->hours_tens*10;
+ frame->mins_tens = stime->mins/10;
+ frame->mins_units = stime->mins - frame->mins_tens*10;
+ frame->secs_tens = stime->secs/10;
+ frame->secs_units = stime->secs - frame->secs_tens*10;
+ frame->frame_tens = stime->frame/10;
+ frame->frame_units = stime->frame - frame->frame_tens*10;
+
+ // Prevent illegal SMPTE frames
+ if (frame->dfbit) {
+ skip_drop_frames(frame);
+ }
+
+ if ((flags & LTC_NO_PARITY) == 0) {
+ ltc_frame_set_parity(frame, standard);
+ }
+}
+
+void ltc_frame_reset(LTCFrame* frame) {
+ memset(frame, 0, sizeof(LTCFrame));
+ // syncword = 0x3FFD
+#ifdef __BIG_ENDIAN__
+ // mirrored BE bit order: FCBF
+ frame->sync_word = 0xFCBF;
+#else
+ // mirrored LE bit order: BFFC
+ frame->sync_word = 0xBFFC;
+#endif
+}
+
+int ltc_frame_increment(LTCFrame* frame, int fps, enum LTC_TV_STANDARD standard, int flags) {
+ int rv = 0;
+
+ frame->frame_units++;
+
+ if (frame->frame_units == 10)
+ {
+ frame->frame_units = 0;
+ frame->frame_tens++;
+ }
+ if (fps == frame->frame_units+frame->frame_tens*10)
+ {
+ frame->frame_units = 0;
+ frame->frame_tens = 0;
+ frame->secs_units++;
+ if (frame->secs_units == 10)
+ {
+ frame->secs_units = 0;
+ frame->secs_tens++;
+ if (frame->secs_tens == 6)
+ {
+ frame->secs_tens = 0;
+ frame->mins_units++;
+ if (frame->mins_units == 10)
+ {
+ frame->mins_units = 0;
+ frame->mins_tens++;
+ if (frame->mins_tens == 6)
+ {
+ frame->mins_tens = 0;
+ frame->hours_units++;
+ if (frame->hours_units == 10)
+ {
+ frame->hours_units = 0;
+ frame->hours_tens++;
+ }
+ if (frame->hours_units == 4 && frame->hours_tens==2)
+ {
+ /* 24h wrap around */
+ rv=1;
+ frame->hours_tens=0;
+ frame->hours_units = 0;
+
+ if (flags&1)
+ {
+ /* wrap date */
+ SMPTETimecode stime;
+ stime.years = frame->user5 + frame->user6*10;
+ stime.months = frame->user3 + frame->user4*10;
+ stime.days = frame->user1 + frame->user2*10;
+
+ if (stime.months > 0 && stime.months < 13)
+ {
+ unsigned char dpm[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
+ /* proper leap-year calc:
+ * ((stime.years%4)==0 && ( (stime.years%100) != 0 || (stime.years%400) == 0) )
+ * simplified since year is 0..99
+ */
+ if ((stime.years%4)==0 /* && stime.years!=0 */ ) /* year 2000 was a leap-year */
+ dpm[1]=29;
+ stime.days++;
+ if (stime.days > dpm[stime.months-1])
+ {
+ stime.days=1;
+ stime.months++;
+ if (stime.months > 12) {
+ stime.months=1;
+ stime.years=(stime.years+1)%100;
+ }
+ }
+ frame->user6 = stime.years/10;
+ frame->user5 = stime.years%10;
+ frame->user4 = stime.months/10;
+ frame->user3 = stime.months%10;
+ frame->user2 = stime.days/10;
+ frame->user1 = stime.days%10;
+ } else {
+ rv=-1;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (frame->dfbit) {
+ skip_drop_frames(frame);
+ }
+
+ if ((flags & LTC_NO_PARITY) == 0) {
+ ltc_frame_set_parity(frame, standard);
+ }
+
+ return rv;
+}
+
+int ltc_frame_decrement(LTCFrame* frame, int fps, enum LTC_TV_STANDARD standard, int flags) {
+ int rv = 0;
+
+ int frames = frame->frame_units + frame->frame_tens * 10;
+ if (frames > 0) {
+ frames--;
+ } else {
+ frames = fps -1;
+ }
+
+ frame->frame_units = frames % 10;
+ frame->frame_tens = frames / 10;
+
+ if (frames == fps -1) {
+ int secs = frame->secs_units + frame->secs_tens * 10;
+ if (secs > 0) {
+ secs--;
+ } else {
+ secs = 59;
+ }
+ frame->secs_units = secs % 10;
+ frame->secs_tens = secs / 10;
+
+ if (secs == 59) {
+ int mins = frame->mins_units + frame->mins_tens * 10;
+ if (mins > 0) {
+ mins--;
+ } else {
+ mins = 59;
+ }
+ frame->mins_units = mins % 10;
+ frame->mins_tens = mins / 10;
+
+ if (mins == 59) {
+ int hours = frame->hours_units + frame->hours_tens * 10;
+ if (hours > 0) {
+ hours--;
+ } else {
+ hours = 23;
+ }
+ frame->hours_units = hours % 10;
+ frame->hours_tens = hours / 10;
+
+ if (hours == 23) {
+ /* 24h wrap around */
+ rv=1;
+ if (flags&LTC_USE_DATE)
+ {
+ /* wrap date */
+ SMPTETimecode stime;
+ stime.years = frame->user5 + frame->user6*10;
+ stime.months = frame->user3 + frame->user4*10;
+ stime.days = frame->user1 + frame->user2*10;
+
+ if (stime.months > 0 && stime.months < 13)
+ {
+ unsigned char dpm[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
+ /* proper leap-year calc:
+ * ((stime.years%4)==0 && ( (stime.years%100) != 0 || (stime.years%400) == 0) )
+ * simplified since year is 0..99
+ */
+ if ((stime.years%4)==0 /* && stime.years!=0 */ ) /* year 2000 was a leap-year */
+ dpm[1]=29;
+ //
+ if (stime.days > 1) {
+ stime.days--;
+ } else {
+ stime.months = 1 + (stime.months + 10)%12;
+ stime.days = dpm[stime.months-1];
+ if (stime.months == 12) {
+ stime.years=(stime.years+99)%100; // XXX
+ }
+ }
+
+ frame->user6 = stime.years/10;
+ frame->user5 = stime.years%10;
+ frame->user4 = stime.months/10;
+ frame->user3 = stime.months%10;
+ frame->user2 = stime.days/10;
+ frame->user1 = stime.days%10;
+ } else {
+ rv=-1;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (frame->dfbit && /* prevent endless recursion */ fps > 2) {
+ if ((frame->mins_units != 0)
+ && (frame->secs_units == 0)
+ && (frame->secs_tens == 0)
+ && (frame->frame_units == 1)
+ && (frame->frame_tens == 0)
+ ) {
+ ltc_frame_decrement(frame, fps, standard, flags&LTC_USE_DATE);
+ ltc_frame_decrement(frame, fps, standard, flags&LTC_USE_DATE);
+ }
+ }
+
+ if ((flags & LTC_NO_PARITY) == 0) {
+ ltc_frame_set_parity(frame, standard);
+ }
+
+ return rv;
+}
+
+int parse_bcg_flags(LTCFrame *f, enum LTC_TV_STANDARD standard) {
+ switch (standard) {
+ case LTC_TV_625_50: /* 25 fps mode */
+ return (
+ ((f->binary_group_flag_bit0)?4:0)
+ | ((f->binary_group_flag_bit1)?2:0)
+ | ((f->biphase_mark_phase_correction)?1:0)
+ );
+ break;
+ default: /* 24,30 fps mode */
+ return (
+ ((f->binary_group_flag_bit2)?4:0)
+ | ((f->binary_group_flag_bit1)?2:0)
+ | ((f->binary_group_flag_bit0)?1:0)
+ );
+ break;
+ }
+}
diff --git a/libs/libltc/wscript b/libs/libltc/wscript
new file mode 100644
index 0000000000..9be750ee55
--- /dev/null
+++ b/libs/libltc/wscript
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+from waflib.extras import autowaf as autowaf
+from waflib import TaskGen
+import os
+import sys
+
+# Version of this package (even if built as a child)
+MAJOR = '1'
+MINOR = '1'
+MICRO = '1'
+LIBLTC_VERSION = "%s.%s.%s" % (MAJOR, MINOR, MICRO)
+
+# Library version (UNIX style major, minor, micro)
+# major increment <=> incompatible changes
+# minor increment <=> compatible changes (additions)
+# micro increment <=> no interface changes
+LIBLTC_LIB_VERSION = '1.1.1'
+
+# Variables for 'waf dist'
+APPNAME = 'libltc'
+VERSION = LIBLTC_VERSION
+I18N_PACKAGE = 'libltc'
+
+# Mandatory variables
+top = '.'
+out = 'build'
+
+def options(opt):
+ autowaf.set_options(opt)
+
+def configure(conf):
+ conf.load('compiler_c')
+ autowaf.configure(conf)
+
+def build(bld):
+ obj = bld(features = 'c cshlib')
+ obj.source = '''
+ ltc.c
+ timecode.c
+ encoder.c
+ decoder.c
+ '''
+
+ obj.export_includes = ['.']
+ obj.includes = ['.']
+ obj.name = 'libltc'
+ obj.target = 'ltc'
+ obj.vnum = LIBLTC_LIB_VERSION
+ obj.install_path = os.path.join(bld.env['LIBDIR'], 'ardour3')
+ obj.defines = ['PACKAGE="' + I18N_PACKAGE + '"']
+
+def shutdown():
+ autowaf.shutdown()
diff --git a/wscript b/wscript
index 6d48d8ef3d..a358b962a3 100644
--- a/wscript
+++ b/wscript
@@ -23,6 +23,7 @@ children = [
'libs/qm-dsp',
'libs/vamp-plugins',
'libs/taglib',
+ 'libs/libltc',
'libs/rubberband',
'libs/surfaces',
'libs/panners',
@@ -726,6 +727,7 @@ def build(bld):
bld.path.find_dir ('libs/vamp-sdk/vamp-sdk')
bld.path.find_dir ('libs/surfaces/control_protocol/control_protocol')
bld.path.find_dir ('libs/timecode/timecode')
+ bld.path.find_dir ('libs/libltc/ltc')
bld.path.find_dir ('libs/rubberband/rubberband')
bld.path.find_dir ('libs/gtkmm2ext/gtkmm2ext')
bld.path.find_dir ('libs/ardour/ardour')