~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/libltc/ltc/ltc.h

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
ImportĀ upstreamĀ versionĀ 3.4~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
   @file ltc.h
 
3
   @brief libltc - en+decode linear timecode
 
4
 
 
5
   Linear (or Longitudinal) Timecode (LTC) is an encoding of
 
6
   timecode data as a Manchester-Biphase encoded audio signal.
 
7
   The audio signal is commonly recorded on a VTR track or other
 
8
   storage media.
 
9
 
 
10
   libltc facilitates decoding and encoding of LTC from/to
 
11
   timecode, including SMPTE date support.
 
12
 
 
13
   @author Robin Gareus <robin@gareus.org>
 
14
   @copyright
 
15
 
 
16
   Copyright (C) 2006-2012 Robin Gareus <robin@gareus.org>
 
17
 
 
18
   Copyright (C) 2008-2009 Jan WeiƟ <jan@geheimwerk.de>
 
19
 
 
20
   Inspired by SMPTE Decoder - Maarten de Boer <mdeboer@iua.upf.es>
 
21
 
 
22
   This program is free software; you can redistribute it and/or modify
 
23
   it under the terms of the GNU Lesser General Public License as
 
24
   published by the Free Software Foundation, either version 3 of the
 
25
   License, or (at your option) any later version.
 
26
 
 
27
   This program is distributed in the hope that it will be useful,
 
28
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
29
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
30
   GNU Lesser General Public License for more details.
 
31
 
 
32
   You should have received a copy of the GNU Lesser General Public
 
33
   License along with this library.
 
34
   If not, see <http://www.gnu.org/licenses/>.
 
35
*/
 
36
#ifndef LTC_H
 
37
#define LTC_H 1
 
38
 
 
39
#ifdef __cplusplus
 
40
extern "C" {
 
41
#endif
 
42
 
 
43
#include <stddef.h> /* size_t */
 
44
 
 
45
#ifndef DOXYGEN_IGNORE
 
46
/* libltc version */
 
47
#define LIBLTC_VERSION "1.1.1"
 
48
#define LIBLTC_VERSION_MAJOR  1
 
49
#define LIBLTC_VERSION_MINOR  1
 
50
#define LIBLTC_VERSION_MICRO  1
 
51
 
 
52
/* interface revision number
 
53
 * http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
 
54
 */
 
55
#define LIBLTC_CUR 11
 
56
#define LIBLTC_REV  1
 
57
#define LIBLTC_AGE  0
 
58
#endif /* end DOXYGEN_IGNORE */
 
59
 
 
60
/**
 
61
 * default audio sample type: 8bit unsigned (mono)
 
62
 */
 
63
typedef unsigned char ltcsnd_sample_t;
 
64
 
 
65
/**
 
66
 * sample-count offset - 64bit wide
 
67
 */
 
68
typedef long long int ltc_off_t;
 
69
 
 
70
#define LTC_FRAME_BIT_COUNT     80
 
71
 
 
72
/**
 
73
 * Raw 80 bit LTC frame
 
74
 *
 
75
 * The datastream for each video frame of Longitudinal Timecode consists of eighty bit-periods.
 
76
 *
 
77
 * At a frame-rate of 30 fps, the bit-rate corresponds to 30 [fps] * 80 [bits/f] = 2400 bits per second.
 
78
 * The frequency for a stream of zeros would be 1.2 kHz and for a stream of ones it would be 2.4 kHz.
 
79
 * \image html smptefmt.png
 
80
 * With all commonly used video-frame-rates and audio-sample-rates,  LTC timecode can be recorded
 
81
 * easily into a audio-track.
 
82
 *
 
83
 * In each frame, 26 of the eighty bits carry the SMPTE time in binary coded decimal (BCD).
 
84
 *
 
85
 * These Bits are FRAME-UNITS, FRAME-TENS, SECS-UNITS, SECS-TENS, MINS-UNITS, MINS-TENS, HOURS-UNITS and HOURS-TENS.
 
86
 * The BCD digits are loaded 'least significant bit first' (libltc takes care of the architecture specific alignment).
 
87
 *
 
88
 * 32 bits are assigned as eight groups of four USER-BITS (also sometimes called the "Binary Groups").
 
89
 * This capacity is generally used to carry extra info such as reel number and/or date.
 
90
 * The User Bits may be allocated howsoever one wishes as long as both Binary Group Flag Bits are cleared.
 
91
 *
 
92
 * The function \ref ltc_frame_to_time can interpret the user-bits as SMPTE Date+Timezone according to SMPTE 309M-1999.
 
93
 * similarly \ref ltc_time_to_frame will do the reverse.
 
94
 *
 
95
 * 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.
 
96
 * The values of these Bits are fixed as 0011 1111 1111 1101
 
97
 *
 
98
 * The Bi-Phase Mark Phase Correction Bit (Bit 27 or 59) may be set or cleared so that that every 80-bit word
 
99
 * contains an even number of zeroes. This means that the phase of the pulse train in every Sync Word will be the same.
 
100
 *
 
101
 * Bit 10 indicates drop-frame timecode.
 
102
 * The Colour Frame Flag col.frm is Bit 11; if the timecode intentionally synchronized to a colour TV field sequence, this bit is set.
 
103
 *
 
104
 * Bit 58 is not required for the BCD count for HOURS-TENS (which has a maximum value of two)
 
105
 * and has not been given any other special purpose so remains unassigned.
 
106
 * This Bit has been RESERVED for future assignment.
 
107
 *
 
108
 * The Binary Group Flag Bits (bits 43 and 59) are two bits indicate the format of the User Bits data.
 
109
 * SMPTE 12M-1999 defines the previously reserved bit 58 to signals that the time is locked to wall-clock
 
110
 * within a tolerance of Ā± 0.5 seconds.
 
111
 *
 
112
 * SMPTE 12M-1999 also changes the numbering schema of the BGF. (BGF1 was renamed to BGF2 and bit 58 becomes BGFB1)
 
113
 *
 
114
 * To further complicate matters, the BGFB assignment as well as the biphase_mark_phase_correction (aka parity)
 
115
 * bit depends on the timecode-format used.
 
116
 *
 
117
 * <pre>
 
118
 *          25 fps   24, 30 fps
 
119
 *  BGF0      27        43
 
120
 *  BGF1      58        58
 
121
 *  BGF2      43        59
 
122
 *  Parity    59        27
 
123
 * </pre>
 
124
 *
 
125
 * The variable naming chosen for the LTCFrame struct is based on the 24,30 fps standard.
 
126
 *
 
127
 * The Binary Group Flag Bits should be used only as shown in the truth table below.
 
128
 * The Unassigned entries in the table should not be used, as they may be allocated specific meanings in the future.
 
129
 *
 
130
 * <pre>
 
131
 *                                                 BGF0      BGF1    BGF2
 
132
 *       user-bits                     timecode    Bit 43   Bit 58  Bit 59 (30fps, 24 fps)
 
133
 *                                    |        |   Bit 27   Bit 58  Bit 43 (25fps)
 
134
 *  No User Bits format specified     |   ?    |     0       0        0
 
135
 *  Eight-bit character set (1)       |   ?    |     1       0        0
 
136
 *  Date and Timezone set             |   ?    |     0       0        1
 
137
 *  Page/Line multiplex (2)           |   ?    |     1       0        1
 
138
 *  Character set not specified       |  clk   |     0       1        0
 
139
 *  Reserved                          |   ?    |     1       1        0
 
140
 *  Date and Timezone set             |  clk   |     0       1        1
 
141
 *  Page/Line multiplex (2)           |  clk   |     1       1        1
 
142
 *
 
143
 * </pre>
 
144
 *
 
145
 * (1) ISO/IEC 646 or ISO/IEC 2022 character set.
 
146
 * If the seven-bit ISO codes are being used, they shall be converted to
 
147
 * eight-bit codes by setting the eighth bit to zero. 4 ISO codes can be encoded,
 
148
 * user7 and user8 are to be used for the first code with LSB 7 and MSB in 8.
 
149
 * the remaining ISO codes are to be distributed in the same manner to
 
150
 * user5/6 user3/4 and user1/2 accordingly.
 
151
 *
 
152
 * (2) The Page/Line indicates ANSI/SMPTE-262M is used for the user-bits. It is multiplex system that
 
153
 * can be used to encode large amounts of data in the binary groups through the use of time multiplexing.
 
154
 *
 
155
 * libltc does not use any of the BGF - except for the Parity bit which can be calculated and set with
 
156
 * \ref ltc_frame_set_parity. Setting and interpreting the BGF is left to the application using libltc.
 
157
 * However libltc provides functionality to parse or set date and timezoe according to SMPTE 309M-1999.
 
158
 *
 
159
 * further information: http://www.philrees.co.uk/articles/timecode.htm
 
160
 * and http://www.barney-wol.net/time/timecode.html
 
161
 */
 
162
#if (defined __BIG_ENDIAN__ && !defined DOXYGEN_IGNORE)
 
163
// Big Endian version, bytes are "upside down"
 
164
struct LTCFrame {
 
165
        unsigned int user1:4;
 
166
        unsigned int frame_units:4;
 
167
 
 
168
        unsigned int user2:4;
 
169
        unsigned int col_frame:1;
 
170
        unsigned int dfbit:1;
 
171
        unsigned int frame_tens:2;
 
172
 
 
173
        unsigned int user3:4;
 
174
        unsigned int secs_units:4;
 
175
 
 
176
        unsigned int user4:4;
 
177
        unsigned int biphase_mark_phase_correction:1;
 
178
        unsigned int secs_tens:3;
 
179
 
 
180
        unsigned int user5:4;
 
181
        unsigned int mins_units:4;
 
182
 
 
183
        unsigned int user6:4;
 
184
        unsigned int binary_group_flag_bit0:1;
 
185
        unsigned int mins_tens:3;
 
186
 
 
187
        unsigned int user7:4;
 
188
        unsigned int hours_units:4;
 
189
 
 
190
        unsigned int user8:4;
 
191
        unsigned int binary_group_flag_bit2:1;
 
192
        unsigned int binary_group_flag_bit1:1;
 
193
        unsigned int hours_tens:2;
 
194
 
 
195
        unsigned int sync_word:16;
 
196
};
 
197
#else
 
198
/* Little Endian version -- and doxygen doc */
 
199
struct LTCFrame {
 
200
        unsigned int frame_units:4; ///< SMPTE framenumber BCD unit 0..9
 
201
        unsigned int user1:4;
 
202
 
 
203
        unsigned int frame_tens:2; ///< SMPTE framenumber BCD tens 0..3
 
204
        unsigned int dfbit:1; ///< indicated drop-frame timecode
 
205
        unsigned int col_frame:1; ///< colour-frame: timecode intentionally synchronized to a colour TV field sequence
 
206
        unsigned int user2:4;
 
207
 
 
208
        unsigned int secs_units:4; ///< SMPTE seconds BCD unit 0..9
 
209
        unsigned int user3:4;
 
210
 
 
211
        unsigned int secs_tens:3; ///< SMPTE seconds BCD tens 0..6
 
212
        unsigned int biphase_mark_phase_correction:1; ///< see note on Bit 27 in description and \ref ltc_frame_set_parity .
 
213
        unsigned int user4:4;
 
214
 
 
215
        unsigned int mins_units:4; ///< SMPTE minutes BCD unit 0..9
 
216
        unsigned int user5:4;
 
217
 
 
218
        unsigned int mins_tens:3; ///< SMPTE minutes BCD tens 0..6
 
219
        unsigned int binary_group_flag_bit0:1; ///< indicate user-data char encoding, see table above - bit 43
 
220
        unsigned int user6:4;
 
221
 
 
222
        unsigned int hours_units:4; ///< SMPTE hours BCD unit 0..9
 
223
        unsigned int user7:4;
 
224
 
 
225
        unsigned int hours_tens:2; ///< SMPTE hours BCD tens 0..2
 
226
        unsigned int binary_group_flag_bit1:1; ///< indicate timecode is local time wall-clock, see table above - bit 58
 
227
        unsigned int binary_group_flag_bit2:1; ///< indicate user-data char encoding (or parity with 25fps), see table above - bit 59
 
228
        unsigned int user8:4;
 
229
 
 
230
        unsigned int sync_word:16;
 
231
};
 
232
#endif
 
233
 
 
234
/** the standard defines the assignment of the binary-group-flag bits
 
235
 * basically only 25fps is different, but other standards defined in
 
236
 * the SMPTE spec have been included for completeness.
 
237
 */
 
238
enum LTC_TV_STANDARD {
 
239
        LTC_TV_525_60, ///< 30fps
 
240
        LTC_TV_625_50, ///< 25fps
 
241
        LTC_TV_1125_60,///< 30fps
 
242
        LTC_TV_FILM_24 ///< 24fps
 
243
};
 
244
 
 
245
/** encoder and LTCframe <> timecode operation flags */
 
246
enum LTC_BG_FLAGS {
 
247
        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)
 
248
        LTC_TC_CLOCK  = 2,///< the Timecode is wall-clock aka freerun. This also sets BGF1 (unless LTC_BGF_DONT_TOUCH is given)
 
249
        LTC_BGF_DONT_TOUCH = 4, ///< encoder init or re-init does not touch the BGF bits (initial values after initialization is zero)
 
250
        LTC_NO_PARITY = 8 ///< parity bit is left untouched when setting or in/decrementing the encoder frame-number
 
251
};
 
252
 
 
253
/**
 
254
 * see LTCFrame
 
255
 */
 
256
typedef struct LTCFrame LTCFrame;
 
257
 
 
258
/**
 
259
 * Extended LTC frame - includes audio-sample position offsets, volume, etc
 
260
 *
 
261
 * Note: For TV systems, the sample in the LTC audio data stream where the LTC Frame starts is not neccesarily at the same time
 
262
 * as the video-frame which is described by the LTC Frame.
 
263
 *
 
264
 * \ref off_start denotes the time of the first transition of bit 0 in the LTC frame.
 
265
 *
 
266
 * For 525/60 Television systems, the first transition shall occur at the beginning of line 5 of the frame with which it is
 
267
 * associated. The tolerance is Ā± 1.5 lines.
 
268
 *
 
269
 * 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.
 
270
 *
 
271
 * Only for 1125/60 systems, the first transition occurs exactly at the vertical sync timing reference of the frame. Ā± 1 line.
 
272
 *
 
273
 */
 
274
struct LTCFrameExt {
 
275
        LTCFrame ltc; ///< the actual LTC frame. see \ref LTCFrame
 
276
        ltc_off_t off_start; ///< \anchor off_start the approximate sample in the stream corresponding to the start of the LTC frame.
 
277
        ltc_off_t off_end; ///< \anchor off_end the sample in the stream corresponding to the end of the LTC frame.
 
278
        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).
 
279
        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).
 
280
        ltcsnd_sample_t sample_min; ///< the minimum input sample signal for this frame (0..255)
 
281
        ltcsnd_sample_t sample_max; ///< the maximum input sample signal for this frame (0..255)
 
282
        double volume; ///< the volume of the input signal in dbFS
 
283
};
 
284
 
 
285
/**
 
286
 * see \ref LTCFrameExt
 
287
 */
 
288
typedef struct LTCFrameExt LTCFrameExt;
 
289
 
 
290
/**
 
291
 * Human readable time representation, decimal values.
 
292
 */
 
293
struct SMPTETimecode {
 
294
        char timezone[6];   ///< the timezone 6bytes: "+HHMM" textual representation
 
295
        unsigned char years; ///< LTC-date uses 2-digit year 00.99
 
296
        unsigned char months; ///< valid months are 1..12
 
297
        unsigned char days; ///< day of month 1..31
 
298
 
 
299
        unsigned char hours; ///< hour 0..23
 
300
        unsigned char mins; ///< minute 0..60
 
301
        unsigned char secs; ///< second 0..60
 
302
        unsigned char frame; ///< sub-second frame 0..(FPS - 1)
 
303
};
 
304
 
 
305
/**
 
306
 * see \ref SMPTETimecode
 
307
 */
 
308
typedef struct SMPTETimecode SMPTETimecode;
 
309
 
 
310
 
 
311
/**
 
312
 * Opaque structure
 
313
 * see: \ref ltc_decoder_create, \ref ltc_decoder_free
 
314
 */
 
315
typedef struct LTCDecoder LTCDecoder;
 
316
 
 
317
/**
 
318
 * Opaque structure
 
319
 * see: \ref ltc_encoder_create, \ref ltc_encoder_free
 
320
 */
 
321
typedef struct LTCEncoder LTCEncoder;
 
322
 
 
323
/**
 
324
 * Convert binary LTCFrame into SMPTETimecode struct
 
325
 *
 
326
 * @param stime output
 
327
 * @param frame input
 
328
 * @param flags binary combination of \ref LTC_BG_FLAGS - here only LTC_USE_DATE is relevant.
 
329
 * if LTC_USE_DATE is set, the user-fields in LTCFrame will be parsed into the date variable of SMPTETimecode.
 
330
 * otherwise the date information in the SMPTETimecode is set to zero.
 
331
 */
 
332
void ltc_frame_to_time(SMPTETimecode* stime, LTCFrame* frame, int flags);
 
333
 
 
334
/**
 
335
 * Translate SMPTETimecode struct into its binary LTC representation
 
336
 * and set the LTC frame's parity bit accordingly (see \ref ltc_frame_set_parity)
 
337
 *
 
338
 * @param frame output - the frame to be set
 
339
 * @param stime input - timecode input
 
340
 * @param standard the TV standard to use for parity bit assignment
 
341
 * @param flags binary combination of \ref LTC_BG_FLAGS - here only LTC_USE_DATE and LTC_NO_PARITY are relevant.
 
342
 * if LTC_USE_DATE is given, user-fields in LTCFrame will be set from the date in SMPTETimecode,
 
343
 * otherwise the user-bits are not modified. All non-timecode fields remain untouched - except for the parity bit
 
344
 * unless LTC_NO_PARITY is given.
 
345
 */
 
346
void ltc_time_to_frame(LTCFrame* frame, SMPTETimecode* stime, enum LTC_TV_STANDARD standard, int flags);
 
347
 
 
348
/**
 
349
 * Reset all values of a LTC FRAME to zero, except for the sync-word (0x3FFD) at the end.
 
350
 * The sync word is set according to architecture (big/little endian).
 
351
 * Also set the Frame's parity bit accordingly (see \ref ltc_frame_set_parity)
 
352
 * @param frame the LTCFrame to reset
 
353
 */
 
354
void ltc_frame_reset(LTCFrame* frame);
 
355
 
 
356
/**
 
357
 * Increment the timecode by one Frame (1/framerate seconds)
 
358
 * and set the Frame's parity bit accordingly (see \ref ltc_frame_set_parity)
 
359
 *
 
360
 * @param frame the LTC-timecode to increment
 
361
 * @param fps integer framerate (for drop-frame-timecode set frame->dfbit and round-up the fps).
 
362
 * @param standard the TV standard to use for parity bit assignment
 
363
 * 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.
 
364
 * @param flags binary combination of \ref LTC_BG_FLAGS - here only LTC_USE_DATE and LTC_NO_PARITY are relevant.
 
365
 * If the bit 0 (1) is set (1) interpret user-data as date and increment date if timecode wraps after 24h.
 
366
 * (Note: leap-years are taken into account, but since the year is two-digit only, the 100,400yr rules are ignored.
 
367
 * "00" is assumed to be year 2000 which was a leap year.)
 
368
 * @return 1 if timecode was wrapped around after 23:59:59:ff, 0 otherwise
 
369
 */
 
370
int ltc_frame_increment(LTCFrame* frame, int fps, enum LTC_TV_STANDARD standard, int flags);
 
371
 
 
372
/**
 
373
 * Decrement the timecode by one Frame (1/framerate seconds)
 
374
 * and set the Frame's parity bit accordingly (see \ref ltc_frame_set_parity)
 
375
 *
 
376
 * @param frame the LTC-timecode to decrement
 
377
 * @param fps integer framerate (for drop-frame-timecode set frame->dfbit and round-up the fps).
 
378
 * @param standard the TV standard to use for parity bit assignment
 
379
 * 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.
 
380
 * @param flags binary combination of \ref LTC_BG_FLAGS - here only LTC_USE_DATE and LTC_NO_PARITY are relevant.
 
381
 * if the bit 0 is set (1) interpret user-data as date and decrement date if timecode wraps at 24h.
 
382
 * (Note: leap-years are taken into account, but since the year is two-digit only, the 100,400yr rules are ignored.
 
383
 * "00" is assumed to be year 2000 which was a leap year.)
 
384
 * bit 3 (8) indicates that the parity bit should not be touched
 
385
 * @return 1 if timecode was wrapped around at 23:59:59:ff, 0 otherwise
 
386
 */
 
387
int ltc_frame_decrement(LTCFrame* frame, int fps, enum LTC_TV_STANDARD standard, int flags);
 
388
 
 
389
/**
 
390
 * Create a new LTC decoder.
 
391
 *
 
392
 * @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.
 
393
 * @param queue_size length of the internal queue to store decoded frames
 
394
 * to SMPTEDecoderWrite.
 
395
 * @return decoder handle or NULL if out-of-memory
 
396
 */
 
397
LTCDecoder * ltc_decoder_create(int apv, int queue_size);
 
398
 
 
399
 
 
400
/**
 
401
 * Release memory of decoder.
 
402
 * @param d decoder handle
 
403
 */
 
404
int ltc_decoder_free(LTCDecoder *d);
 
405
 
 
406
/**
 
407
 * Feed the LTC decoder with new audio samples.
 
408
 *
 
409
 * Parse raw audio for LTC timestamps. Once a complete LTC frame has been
 
410
 * decoded it is pushed into a queue (\ref ltc_decoder_read)
 
411
 *
 
412
 * @param d decoder handle
 
413
 * @param buf pointer to ltcsnd_sample_t - unsigned 8 bit mono audio data
 
414
 * @param size \anchor size number of samples to parse
 
415
 * @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)
 
416
 */
 
417
void ltc_decoder_write(LTCDecoder *d,
 
418
                ltcsnd_sample_t *buf, size_t size,
 
419
                ltc_off_t posinfo);
 
420
 
 
421
/**
 
422
 * Wrapper around \ref ltc_decoder_write that accepts floating point
 
423
 * audio samples. Note: internally libltc uses 8 bit only.
 
424
 *
 
425
 * @param d decoder handle
 
426
 * @param buf pointer to audio sample data
 
427
 * @param size number of samples to parse
 
428
 * @param posinfo (optional, recommended) sample-offset in the audio-stream.
 
429
 */
 
430
void ltc_decoder_write_float(LTCDecoder *d, float *buf, size_t size, ltc_off_t posinfo);
 
431
 
 
432
/**
 
433
 * Wrapper around \ref ltc_decoder_write that accepts signed 16 bit
 
434
 * audio samples. Note: internally libltc uses 8 bit only.
 
435
 *
 
436
 * @param d decoder handle
 
437
 * @param buf pointer to audio sample data
 
438
 * @param size number of samples to parse
 
439
 * @param posinfo (optional, recommended) sample-offset in the audio-stream.
 
440
 */
 
441
void ltc_decoder_write_s16(LTCDecoder *d, short *buf, size_t size, ltc_off_t posinfo);
 
442
 
 
443
/**
 
444
 * Wrapper around \ref ltc_decoder_write that accepts unsigned 16 bit
 
445
 * audio samples. Note: internally libltc uses 8 bit only.
 
446
 *
 
447
 * @param d decoder handle
 
448
 * @param buf pointer to audio sample data
 
449
 * @param size number of samples to parse
 
450
 * @param posinfo (optional, recommended) sample-offset in the audio-stream.
 
451
 */
 
452
void ltc_decoder_write_u16(LTCDecoder *d, short *buf, size_t size, ltc_off_t posinfo);
 
453
 
 
454
/**
 
455
 * Decoded LTC frames are placed in a queue. This function retrieves
 
456
 * a frame from the queue, and stores it at LTCFrameExt*
 
457
 *
 
458
 * @param d decoder handle
 
459
 * @param frame the decoded LTC frame is copied there
 
460
 * @return 1 on success or 0 when no frames queued.
 
461
 */
 
462
int ltc_decoder_read(LTCDecoder *d, LTCFrameExt *frame);
 
463
 
 
464
/**
 
465
 * Remove all LTC frames from the internal queue.
 
466
 * @param d decoder handle
 
467
 */
 
468
void ltc_decoder_queue_flush(LTCDecoder* d);
 
469
 
 
470
/**
 
471
 * Count number of LTC frames currently in the queue.
 
472
 * @param d decoder handle
 
473
 * @return number of queued frames
 
474
 */
 
475
int ltc_decoder_queue_length(LTCDecoder* d);
 
476
 
 
477
 
 
478
 
 
479
/**
 
480
 * Allocate and initialize LTC audio encoder.
 
481
 *
 
482
 * calls \ref ltc_encoder_reinit internally see, see notes there.
 
483
 *
 
484
 * @param sample_rate audio sample rate (eg. 48000)
 
485
 * @param fps video-frames per second (e.g. 25.0)
 
486
 * @param standard the TV standard to use for Binary Group Flag bit position
 
487
 * @param flags binary combination of \ref LTC_BG_FLAGS
 
488
 */
 
489
LTCEncoder* ltc_encoder_create(double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags);
 
490
 
 
491
/**
 
492
 * Release memory of the encoder.
 
493
 * @param e encoder handle
 
494
 */
 
495
void ltc_encoder_free(LTCEncoder *e);
 
496
 
 
497
/**
 
498
 * Set the encoder LTC-frame to the given SMPTETimecode.
 
499
 * The next call to \ref ltc_encoder_encode_byte or
 
500
 * \ref ltc_encoder_encode_frame will encode this time to LTC audio-samples.
 
501
 *
 
502
 * Internally this call uses \ref ltc_time_to_frame because
 
503
 * the LTCEncoder operates on LTCframes only.
 
504
 * see als \ref ltc_encoder_set_frame
 
505
 *
 
506
 * @param e encoder handle
 
507
 * @param t timecode to set.
 
508
 */
 
509
void ltc_encoder_set_timecode(LTCEncoder *e, SMPTETimecode *t);
 
510
 
 
511
/**
 
512
 * Query the current encoder timecode.
 
513
 *
 
514
 * Note: the decoder stores its internal state in an LTC-frame,
 
515
 * this function converts that LTC-Frame into SMPTETimecode on demand.
 
516
 * see also \ref ltc_encoder_get_frame.
 
517
 *
 
518
 * @param e encoder handle
 
519
 * @param t is set to current timecode
 
520
 */
 
521
void ltc_encoder_get_timecode(LTCEncoder *e, SMPTETimecode *t);
 
522
 
 
523
/**
 
524
 * Move the encoder to the next timecode frame.
 
525
 * uses \ref ltc_frame_increment() internally.
 
526
 */
 
527
int ltc_encoder_inc_timecode(LTCEncoder *e);
 
528
 
 
529
/**
 
530
 * Move the encoder to the previous timecode frame.
 
531
 * This is useful for encoding reverse LTC.
 
532
 * uses \ref ltc_frame_decrement() internally.
 
533
 */
 
534
int ltc_encoder_dec_timecode(LTCEncoder *e);
 
535
 
 
536
/**
 
537
 * Low-level access to the internal LTCFrame data.
 
538
 *
 
539
 * Note: be careful to about f->dfbit, the encoder sets this [only] upon
 
540
 * initialization.
 
541
 *
 
542
 * @param e encoder handle
 
543
 * @param f LTC frame data to use
 
544
 */
 
545
void ltc_encoder_set_frame(LTCEncoder *e, LTCFrame *f);
 
546
 
 
547
/**
 
548
 * Low-level access to the encoder internal LTCFrame data
 
549
 *
 
550
 * @param e encoder handle
 
551
 * @param f return LTC frame data
 
552
 */
 
553
void ltc_encoder_get_frame(LTCEncoder *e, LTCFrame *f);
 
554
 
 
555
/**
 
556
 * Copy the accumulated encoded audio to the given
 
557
 * sample-buffer and flush the internal buffer.
 
558
 *
 
559
 * @param e encoder handle
 
560
 * @param buf place to store the audio-samples, needs to be large enough
 
561
 * to hold \ref ltc_encoder_get_buffersize bytes
 
562
 * @return the number of bytes written to the memory area
 
563
 * pointed to by buf.
 
564
 */
 
565
int ltc_encoder_get_buffer(LTCEncoder *e, ltcsnd_sample_t *buf);
 
566
 
 
567
 
 
568
/**
 
569
 * Retrieve a pointer to the accumulated encoded audio-data.
 
570
 *
 
571
 * @param e encoder handle
 
572
 * @param size if set, the number of valid bytes in the buffer is stored there
 
573
 * @param flush call \ref ltc_encoder_buffer_flush - reset the buffer write-pointer
 
574
 * @return pointer to encoder-buffer
 
575
 */
 
576
ltcsnd_sample_t *ltc_encoder_get_bufptr(LTCEncoder *e, int *size, int flush);
 
577
 
 
578
/**
 
579
 * reset the write-pointer of the encoder-buffer
 
580
 * @param e encoder handle
 
581
 */
 
582
void ltc_encoder_buffer_flush(LTCEncoder *e);
 
583
 
 
584
/**
 
585
 * Query the length of the internal buffer. It is allocated
 
586
 * to hold audio-frames for exactly one LTC frame for the given
 
587
 * sample-rate and frame-rate.  ie. (1 + sample-rate / fps) bytes
 
588
 *
 
589
 * Note this returns the total size of the buffer, not the used/free
 
590
 * part. See also \ref ltc_encoder_get_bufptr
 
591
 *
 
592
 * @param e encoder handle
 
593
 * @return size of the allocated internal buffer.
 
594
 */
 
595
size_t ltc_encoder_get_buffersize(LTCEncoder *e);
 
596
 
 
597
/**
 
598
 * Change the encoder settings without re-allocating any
 
599
 * library internal data structure (realtime safe).
 
600
 * changing the fps and or sample-rate implies a buffer flush,
 
601
 * and biphase state reset.
 
602
 *
 
603
 * This call will fail if the internal buffer is too small
 
604
 * to hold one full LTC frame. Use \ref ltc_encoder_set_bufsize to
 
605
 * prepare an internal buffer large enough to accommodate all
 
606
 * sample_rate, fps combinations that you would like to re-init to.
 
607
 *
 
608
 * The LTC frame payload data is not modified by this call, however,
 
609
 * the flag-bits of the LTC-Frame are updated:
 
610
 * If fps equals to 29.97 or 30000.0/1001.0, the LTCFrame's 'dfbit' bit is set to 1
 
611
 * to indicate drop-frame timecode.
 
612
 *
 
613
 * Unless the LTC_BGF_DONT_TOUCH flag is set the BGF1 is set or cleared depending
 
614
 * on LTC_TC_CLOCK and BGF0,2 according to LTC_USE_DATE and the given standard.
 
615
 * col_frame is cleared  and the parity recomputed (unless LTC_NO_PARITY is given).
 
616
 *
 
617
 * @param e encoder handle
 
618
 * @param sample_rate audio sample rate (eg. 48000)
 
619
 * @param fps video-frames per second (e.g. 25.0)
 
620
 * @param standard the TV standard to use for Binary Group Flag bit position
 
621
 * @param flags binary combination of \ref LTC_BG_FLAGS
 
622
 */
 
623
int ltc_encoder_reinit(LTCEncoder *e, double sample_rate, double fps, enum LTC_TV_STANDARD standard, int flags);
 
624
 
 
625
/**
 
626
 * reset ecoder state.
 
627
 * flushes buffer, reset biphase state
 
628
 *
 
629
 * @param e encoder handle
 
630
 */
 
631
void ltc_encoder_reset(LTCEncoder *e);
 
632
 
 
633
/**
 
634
 * Configure a custom size for the internal buffer.
 
635
 *
 
636
 * This is needed if you are planning to call \ref ltc_encoder_reinit()
 
637
 * or if you want to keep more than one LTC frame's worth of data in
 
638
 * the library's internal buffer.
 
639
 *
 
640
 * The buffer-size is (1 + sample_rate / fps) bytes.
 
641
 * resizing the internal buffer will flush all existing data
 
642
 * in it - alike \ref ltc_encoder_buffer_flush.
 
643
 *
 
644
 * @param e encoder handle
 
645
 * @param sample_rate audio sample rate (eg. 48000)
 
646
 * @param fps video-frames per second (e.g. 25.0)
 
647
 * @return 0 on success, -1 if allocation fails (which makes the
 
648
 *   encoder unusable, call \ref ltc_encoder_free or realloc the buffer)
 
649
 */
 
650
int ltc_encoder_set_bufsize(LTCEncoder *e, double sample_rate, double fps);
 
651
 
 
652
/**
 
653
 * Set the volume of the generated LTC signal
 
654
 *
 
655
 * typically LTC is sent at 0dBu ; in EBU callibrated systems that
 
656
 * corresponds to -18dBFS. - by default libltc creates -3dBFS
 
657
 *
 
658
 * since libltc generated 8bit audio-data, the minium dBFS
 
659
 * is about -42dB which corresponds to 1 bit.
 
660
 *
 
661
 * 0dB corresponds to a signal range of 127
 
662
 * 1..255 with 128 at the center.
 
663
 *
 
664
 * @param e encoder handle
 
665
 * @param dBFS the volume in dB full-scale (<= 0.0)
 
666
 * @return 0 on success, -1 if the value was out of range
 
667
 */
 
668
int ltc_encoder_set_volume(LTCEncoder *e, double dBFS);
 
669
 
 
670
/**
 
671
 * Set encoder signal rise-time / signal filtering
 
672
 *
 
673
 * LTC signal should have a rise time of 40us +/- 10 us.
 
674
 * by default the encoder honors this and low-pass filters
 
675
 * the output depending on the sample-rate.
 
676
 *
 
677
 * If you want a perfect square wave, set 'rise_time' to 0.
 
678
 *
 
679
 * Note \ref ltc_encoder_reinit resets the filter-time-constant to use
 
680
 * the default 40us for the given sample-rate, overriding any value
 
681
 * previously set with \ref ltc_encoder_set_filter
 
682
 *
 
683
 * @param e encoder handle
 
684
 * @param rise_time the signal rise-time in us (10^(-6) sec), set to 0 for perfect square wave, default 40.0
 
685
 */
 
686
void ltc_encoder_set_filter(LTCEncoder *e, double rise_time);
 
687
 
 
688
/**
 
689
 * Generate LTC audio for given byte of the LTC-frame and
 
690
 * place it into the internal buffer.
 
691
 *
 
692
 * see \ref ltc_encoder_get_buffer and  \ref ltc_encoder_get_bufptr
 
693
 *
 
694
 * LTC has 10 bytes per frame: 0 <= bytecnt < 10
 
695
 * use SMPTESetTime(..) to set the current frame before Encoding.
 
696
 * see tests/encoder.c for an example.
 
697
 *
 
698
 * The default output signal is @ -3dBFS (38..218 at 8 bit unsigned).
 
699
 * see also \ref ltc_encoder_set_volume
 
700
 *
 
701
 * if speed is < 0, the bits are encoded in reverse.
 
702
 * slowdown > 10.0 requires custom buffer sizes; see \ref ltc_encoder_set_bufsize
 
703
 *
 
704
 * @param e encoder handle
 
705
 * @param byte byte of the LTC-frame to encode 0..9
 
706
 * @param speed vari-speed, < 1.0 faster,  > 1.0 slower ; must be != 0
 
707
 *
 
708
 * @return 0 on success, -1 if byte is invalid or buffer overflow (speed > 10.0)
 
709
 */
 
710
int ltc_encoder_encode_byte(LTCEncoder *e, int byte, double speed);
 
711
 
 
712
/**
 
713
 * Encode a full LTC frame at fixed speed.
 
714
 * This is equivalent to calling \ref ltc_encoder_encode_byte 10 times for
 
715
 * bytes 0..9 with speed 1.0.
 
716
 *
 
717
 * Note: The internal buffer must be empty before calling this function.
 
718
 * Otherwise it may overflow. This is usually the case if it is read with
 
719
 * \ref ltc_encoder_get_buffer after calling this function.
 
720
 *
 
721
 * The default internal buffersize is exactly one full LTC frame at speed 1.0.
 
722
 *
 
723
 * @param e encoder handle
 
724
 */
 
725
void ltc_encoder_encode_frame(LTCEncoder *e);
 
726
 
 
727
/**
 
728
 * Set the parity of the LTC frame.
 
729
 *
 
730
 * Bi-Phase Mark Phase Correction bit (bit 27 - or 59) may be set or cleared so that
 
731
 * that every 80-bit word contains an even number of zeroes.
 
732
 * This means that the phase in every Sync Word will be the same.
 
733
 *
 
734
 * This is merely cosmetic; the motivation to keep the polarity of the waveform
 
735
 * constant is to make finding the Sync Word visibly (on a scope) easier.
 
736
 *
 
737
 * There is usually no need to call this function directly. The encoder utility
 
738
 * functions \ref ltc_time_to_frame, \ref ltc_frame_increment and
 
739
 * \ref ltc_frame_decrement include a call to it.
 
740
 *
 
741
 * @param frame the LTC to analyze and set or clear the biphase_mark_phase_correction bit.
 
742
 * @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.
 
743
 */
 
744
void ltc_frame_set_parity(LTCFrame *frame, enum LTC_TV_STANDARD standard);
 
745
 
 
746
/**
 
747
 * Parse Binary Group Flags into standard independent format:
 
748
 * bit 0 (1) - BGF 0,
 
749
 * bit 1 (2) - BGF 1,
 
750
 * bit 2 (4) - BGF 2
 
751
 *
 
752
 * @param f LTC frame data analyze
 
753
 * @param standard the TV standard to use -- see \ref LTCFrame for BGF assignment
 
754
 * @return LTC Binary Group Flags
 
755
 */
 
756
int parse_bcg_flags(LTCFrame *f, enum LTC_TV_STANDARD standard);
 
757
 
 
758
/**
 
759
 * LTCFrame sample alignment offset.
 
760
 *
 
761
 * There is a relative offset of the LTC-Frame start and the TV-frame.
 
762
 * The first bit of a LTC frame corresponds to a specific line in the actual video
 
763
 * frame. When decoding this offset needs to be subtracted from the LTC-frame's
 
764
 * audio-sample-time to match the TV-frame's start position.
 
765
 *
 
766
 * For film frames or HDV the offset is zero.
 
767
 *
 
768
 * @param samples_per_frame audio-samples per timecode-frame (eg. 1920 = 48000/25)
 
769
 * @param standard the TV standard
 
770
 * @return offset in samples
 
771
 */
 
772
ltc_off_t ltc_frame_alignment(double samples_per_frame, enum LTC_TV_STANDARD standard);
 
773
 
 
774
#ifdef __cplusplus
 
775
}
 
776
#endif
 
777
 
 
778
#endif