File: ltcdecode.c

package info (click to toggle)
libltc 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 492 kB
  • sloc: ansic: 1,509; makefile: 66
file content (113 lines) | stat: -rw-r--r-- 2,658 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
   @brief self-test and example code for libltc LTCDecoder
   @file ltcdecode.c
   @author Robin Gareus <robin@gareus.org>

   Copyright (C) 2003  Maarten de Boer <mdeboer@iua.upf.es>
   Copyright (C) 2006-2016 Robin Gareus <robin@gareus.org>
   Copyright (C) 2008-2009 Jan <jan@geheimwerk.de>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser Public License as published by
   the Free Software Foundation; either version 3, 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 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, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>
#include <math.h>
#include <ltc.h>

#ifdef _WIN32
#include <fcntl.h> // for _fmode
#endif

#define BUFFER_SIZE (1024)

/**
 * simple example and test decoder
 */
int main(int argc, char **argv) {
	int apv = 1920;
	ltcsnd_sample_t sound[BUFFER_SIZE];
	size_t n;
	long int total;
	FILE* f;
	char* filename;

	LTCDecoder *decoder;
	LTCFrameExt frame;

	if (argc > 1) {
		filename = argv[1];
		if (argc > 2) {
			sscanf(argv[2], "%i", &apv);
		}
	} else {
		printf("Usage: %s <filename> [audio-frames-per-video-frame]\n", argv[0]);
		return -1;
	}

#ifdef _WIN32
	// see https://msdn.microsoft.com/en-us/library/ktss1a9b.aspx and
	// https://github.com/x42/libltc/issues/18
	_set_fmode(_O_BINARY);
#endif

	f = fopen(filename, "r");

	if (!f) {
		fprintf(stderr, "error opening '%s'\n", filename);
		return -1;
	}
	fprintf(stderr, "* reading from: %s\n", filename);

	total = 0;

	decoder = ltc_decoder_create(apv, 32);

	do {
		n = fread(sound, sizeof(ltcsnd_sample_t), BUFFER_SIZE, f);
		ltc_decoder_write(decoder, sound, n, total);

		while (ltc_decoder_read(decoder, &frame)) {
			SMPTETimecode stime;

			ltc_frame_to_time(&stime, &frame.ltc, 1);

			printf("%04d-%02d-%02d %s ",
				((stime.years < 67) ? 2000+stime.years : 1900+stime.years),
				stime.months,
				stime.days,
				stime.timezone
				);

			printf("%02d:%02d:%02d%c%02d | %8lld %8lld%s\n",
					stime.hours,
					stime.mins,
					stime.secs,
					(frame.ltc.dfbit) ? '.' : ':',
					stime.frame,
					frame.off_start,
					frame.off_end,
					frame.reverse ? "  R" : ""
					);
		}

		total += n;

	} while (n);

	fclose(f);
	ltc_decoder_free(decoder);

	return 0;
}