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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*
decode_tm6000.c - decode multiplexed format from TM5600/TM6000 USB
Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2.
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 General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "../lib/v4l2_driver.h"
#include <stdio.h>
#include <string.h>
#include <argp.h>
#include <unistd.h>
const char *argp_program_version="decode_tm6000 version 0.0.1";
const char *argp_program_bug_address="Mauro Carvalho Chehab <mchehab@infradead.org>";
const char doc[]="Decodes tm6000 proprietary format streams";
const struct argp_option options[] = {
{"verbose", 'v', 0, 0, "enables debug messages", 0},
{"device", 'd', "DEV", 0, "uses device for reading", 0},
{"output", 'o', "FILE", 0, "outputs raw stream to a file", 0},
{"input", 'i', "FILE", 0, "parses a file, instead of a device", 0},
{"audio", 'a', 0, 0, "outputs audio on stdout", 0},
{"read", 'r', 0, 0, "use read() instead of mmap method", 0},
{ 0, 0, 0, 0, 0, 0 }
};
static int debug=0, audio=0, use_mmap=1;
static char *devname="/dev/video0";
static char *filename=NULL;
static enum {
NORMAL,
INPUT,
OUTPUT
} mode = NORMAL;
static FILE *fout;
//const char args_doc[]="ARG1 ARG2";
static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key) {
case 'a':
audio++;
break;
case 'r':
use_mmap=0;
break;
case 'v':
debug++;
break;
case 'd':
devname=arg;
break;
case 'i':
case 'o':
if (mode!=NORMAL) {
argp_error(state,"You can't use input/output options simultaneously.\n");
break;
}
if (key=='i')
mode=INPUT;
else
mode=OUTPUT;
filename=arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
.options=options,
.parser=parse_opt,
.args_doc=NULL,
.doc=doc,
};
#define TM6000_URB_MSG_LEN 180
enum {
TM6000_URB_MSG_VIDEO=1,
TM6000_URB_MSG_AUDIO,
TM6000_URB_MSG_VBI,
TM6000_URB_MSG_PTS,
TM6000_URB_MSG_ERR,
};
const char *tm6000_msg_type[]= {
"unknown(0)", //0
"video", //1
"audio", //2
"vbi", //3
"pts", //4
"err", //5
"unknown(6)", //6
"unknown(7)", //7
};
#define dprintf(fmt,arg...) \
if (debug) fprintf(stderr, fmt, ##arg)
int recebe_buffer (struct v4l2_buffer *v4l2_buf, struct v4l2_t_buf *buf)
{
fwrite (buf->start,buf->length,1,fout);
return 0;
}
int read_mmap(struct v4l2_driver *drv)
{
double freq;
freq=193.25 * 1000 * 1000;
v4l2_getset_freq (drv,V4L2_SET, &freq);
printf("Preparing for frames...\n");
fflush (stdout);
sleep(1);
v4l2_mmap_bufs(drv, 2);
v4l2_start_streaming(drv);
printf("Waiting for frames...\n");
while (1) {
fd_set fds;
struct timeval tv;
int r;
FD_ZERO (&fds);
FD_SET (drv->fd, &fds);
/* Timeout. */
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select (drv->fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r) {
if (EINTR == errno)
perror ("select");
return errno;
}
if (0 == r) {
fprintf (stderr, "select timeout\n");
return errno;
}
if (v4l2_rcvbuf(drv, recebe_buffer))
break;
}
return 0;
}
int main (int argc, char*argv[])
{
FILE *fp;
unsigned char c, buf[TM6000_URB_MSG_LEN], img[720*2*480];
unsigned int cmd, cpysize, pktsize, size, field, block, line, pos=0;
unsigned long header=0;
int linesize=720*2,skip=0;
struct v4l2_format fmt;
struct v4l2_driver drv;
argp_parse (&argp, argc, argv, 0, 0, 0);
if (mode!=INPUT) {
if (v4l2_open (devname, 1,&drv)<0) {
perror ("Error opening dev");
return -1;
}
fp=fdopen(drv.fd,"r");
if (!fp) {
perror("error associating dev");
return -1;
}
memset (&fmt,0,sizeof(fmt));
uint32_t pixelformat=V4L2_PIX_FMT_TM6000;
if (v4l2_gettryset_fmt_cap (&drv,V4L2_SET,&fmt, 720, 480,
pixelformat,V4L2_FIELD_ANY)) {
perror("set_input to tm6000 raw format");
return -1;
}
}
if (mode==INPUT) {
fp=fopen(filename,"r");
if (!fp) {
perror ("error opening a file for parsing");
return -1;
}
dprintf("file %s opened for parsing\n",filename);
}
if (mode==OUTPUT) {
char outbuf[2<<18];
fout=fopen(filename,"w");
if (!fout) {
perror ("error opening a file to write");
return -1;
}
dprintf("file %s opened for output\n",filename);
if (use_mmap) {
return (read_mmap(&drv));
}
do {
size=fread(outbuf,1, sizeof(outbuf), fp);
if (!size) {
fclose (fp);
return 0;
}
dprintf("writing %d bytes\n",size);
fwrite(outbuf,1, size,fout);
// fflush(fout);
} while (1);
}
while (1) {
skip=0;
header=0;
do {
c=fgetc(fp);
header=(header>>8)&0xffffff;
header=header|(c<<24);
skip++;
} while ( (((header>>24)&0xff) != 0x47) );
/* split the header fields */
size = (((header & 0x7e)<<1) -1) * 4;
block = (header>>7) & 0xf;
field = (header>>11) & 0x1;
line = (header>>12) & 0x1ff;
cmd = (header>>21) & 0x7;
/* Read the remaining buffer */
fread((char *)buf,sizeof(buf), 1, fp);
/* FIXME: Mounts the image as field0+field1
* It should, instead, check if the user selected
* entrelaced or non-entrelaced mode
*/
pos=((line<<1)+field)*linesize+
block*TM6000_URB_MSG_LEN;
/* Prints debug info */
dprintf("0x%08x (skip %d), %s size=%d, num=%d,line=%d, field=%d\n",
(unsigned int)header, skip,
tm6000_msg_type[cmd],
size, block, line, field);
/* Don't allow to write out of the buffer */
if (pos+sizeof(buf) > sizeof(img))
cmd = TM6000_URB_MSG_ERR;
/* handles each different URB message */
switch(cmd) {
case TM6000_URB_MSG_VIDEO:
/* Fills video buffer */
memcpy(buf,&img[pos],sizeof(buf));
case TM6000_URB_MSG_AUDIO:
if (audio)
fwrite(buf,sizeof(buf),1,stdout);
// case TM6000_URB_MSG_VBI:
// case TM6000_URB_MSG_PTS:
break;
}
}
fclose(fp);
return 0;
}
|