summaryrefslogtreecommitdiffstats
path: root/usr.sbin/fwcontrol/fwdv.c
blob: 757d09e602e88b997a00cf6e3a0f09b77abc70d6 (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Copyright (C) 2003
 * 	Hidetoshi Shimokawa. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *
 *	This product includes software developed by Hidetoshi Shimokawa.
 *
 * 4. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * $FreeBSD$
 */
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>

#if __FreeBSD_version >= 500000
#include <arpa/inet.h>
#endif

#include <err.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dev/firewire/firewire.h>
#include <dev/firewire/iec68113.h>


struct frac {
	int n,d;	
};

struct frac frame_cycle[2]  = {
	{8000*100, 2997},	/* NTSC 8000 cycle / 29.97 Hz */
	{320, 1},		/* PAL  8000 cycle / 25 Hz */
};
int npackets[] = {
	250		/* NTSC */,
	300		/* PAL */
};
struct frac pad_rate[2]  = {
	{203, 2997},	/* = (8000 - 29.97 * 250)/(29.97 * 250) */
	{1, 15},	/* = (8000 - 25 * 300)/(25 * 300) */
};

#define PSIZE 512
#define DSIZE 480
#define NCHUNK 8

#define NPACKET_R 256
#define NPACKET_T 255
#define NEMPTY 20
#define BUFSIZE (PSIZE * NPACKET_R)
#define MAXBLOCKS (300)
#define CYCLE_FRAC 0xc00

int
dvrecv(int d, char *filename, char ich, int count)
{
	struct fw_isochreq isoreq;
	struct fw_isobufreq bufreq;
	struct dvdbc *dv;
	struct ciphdr *ciph;
	struct fw_pkt *pkt;
	char *pad, *buf;
	u_int32_t *ptr;
	int len, tlen, npad, fd, k, m, vec, pal, nb;
	int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
	struct iovec wbuf[NPACKET_R];

	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
	buf = (char *)malloc(BUFSIZE);
	pad = (char *)malloc(DSIZE*MAXBLOCKS);
	bzero(pad, DSIZE*MAXBLOCKS);
	bzero(wbuf, sizeof(wbuf));

	bufreq.rx.nchunk = NCHUNK;
	bufreq.rx.npacket = NPACKET_R;
	bufreq.rx.psize = PSIZE;
	bufreq.tx.nchunk = 0;
	bufreq.tx.npacket = 0;
	bufreq.tx.psize = 0;
	if (ioctl(d, FW_SSTBUF, &bufreq) < 0) {
		err(1, "ioctl");
	}

	isoreq.ch = ich & 0x3f;
	isoreq.tag = (ich >> 6) & 3;

	if( ioctl(d, FW_SRSTREAM, &isoreq) < 0)
       		err(1, "ioctl");

	k = m = 0;
	while (count <= 0 || k <= count) {
#if 0
		tlen = 0;
		while ((len = read(d, buf + tlen, PSIZE
						/*BUFSIZE - tlen*/)) > 0) {
			if (len < 0) {
				if (errno == EAGAIN) {
					fprintf(stderr, "(EAGAIN)\n");
					fflush(stderr);
					if (len <= 0)
						continue;
				} else
					err(1, "read failed");
			}
			tlen += len;
			if ((BUFSIZE - tlen) < PSIZE)
				break;
		};
#else
		tlen = len = read(d, buf, BUFSIZE);
		if (len < 0) {
			if (errno == EAGAIN) {
				fprintf(stderr, "(EAGAIN)\n");
				fflush(stderr);
				if (len <= 0)
					continue;
			} else
				err(1, "read failed");
		}
#endif
		vec = 0;
		ptr = (u_int32_t *) buf;
again:
		pkt = (struct fw_pkt *) ptr;	
#if 0
		fprintf(stderr, "%08x %08x %08x %08x\n",
			htonl(ptr[0]), htonl(ptr[1]),
			htonl(ptr[2]), htonl(ptr[3]));
#endif
		ciph = (struct ciphdr *)(ptr + 1);	/* skip iso header */
		if (ciph->fmt != CIP_FMT_DVCR)
			errx(1, "unknown format 0x%x", ciph->fmt);
		ptr = (u_int32_t *) (ciph + 1);		/* skip cip header */
#if 0
		if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
			fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
		}
#endif
		if (ntohs(pkt->mode.stream.len) <= sizeof(struct ciphdr))
			/* no payload */
			goto next;
		for (dv = (struct dvdbc *)ptr;
				(char *)dv < (char *)(ptr + ciph->len);
				dv+=6) {

#if 0
			fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
#endif
			if  (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
#if 0
				fprintf(stderr, "%d(%d) ", k, m);
#else
				fprintf(stderr, "%d", k%10);
#endif
				pal = ((dv->payload[0] & DV_DSF_12) != 0);
				nb = nblocks[pal];
#if 1
				if (m > 0 && m != nb) {
					/* padding bad frame */
					npad = ((nb - m) % nb);
					if (npad < 0)
						npad += nb;
					fprintf(stderr, "(%d blocks padded)",
								npad);
					npad *= DSIZE;
					wbuf[vec].iov_base = pad;
					wbuf[vec++].iov_len = npad;
					if (vec >= NPACKET_R) {
						writev(fd, wbuf, vec);
						vec = 0;
					}
				}
#endif
				k++;
				if (k % 30 == 0) { /* every second */
					fprintf(stderr, "\n");
				}
				fflush(stderr);
				m = 0;
			}
			if (k == 0 || (count > 0 && k > count))
				continue;
			m++;
			wbuf[vec].iov_base = (char *) dv;
			wbuf[vec++].iov_len = DSIZE;
			if (vec >= NPACKET_R) {
				writev(fd, wbuf, vec);
				vec = 0;
			}
		}
		ptr = (u_int32_t *)dv;
next:
		if ((char *)ptr < buf + tlen)
			goto again;
		if (vec > 0)
			writev(fd, wbuf, vec);
	}
	close(fd);
	fprintf(stderr, "\n");
	return 0;
}


int
dvsend(int d, char *filename, char ich, int count)
{
	struct fw_isochreq isoreq;
	struct fw_isobufreq bufreq;
	struct dvdbc *dv;
	struct fw_pkt *pkt;
	int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
	int system=0, pad_acc, cycle_acc, cycle, f_cycle, f_frac; 
	struct iovec wbuf[NPACKET_T*2 + NEMPTY];
	char *pbuf;
	u_int32_t iso_data, iso_empty, hdr[NPACKET_T + NEMPTY][3];
	struct ciphdr *ciph;
	struct timeval start, end;
	double rtime;

	fd = open(filename, O_RDONLY);
	pbuf = (char *)malloc(DSIZE * NPACKET_T);
	bzero(wbuf, sizeof(wbuf));

	bufreq.rx.nchunk = 0;
	bufreq.rx.npacket = 0;
	bufreq.rx.psize = 0;
	bufreq.tx.nchunk = NCHUNK;
	bufreq.tx.npacket = NPACKET_T;
	bufreq.tx.psize = PSIZE;
	if (ioctl(d, FW_SSTBUF, &bufreq) < 0) {
		err(1, "ioctl");
	}

	isoreq.ch = ich & 0x3f;
	isoreq.tag = (ich >> 6) & 3;

	if( ioctl(d, FW_STSTREAM, &isoreq) < 0)
       		err(1, "ioctl");

	pkt = (struct fw_pkt *) &iso_data;
	pkt->mode.stream.len = htons(DSIZE + sizeof(struct ciphdr));
	pkt->mode.stream.sy = 0;
	pkt->mode.stream.tcode = FWTCODE_STREAM;
	pkt->mode.stream.chtag = ich;
	iso_empty = iso_data;
	pkt = (struct fw_pkt *) &iso_empty;
	pkt->mode.stream.len = htons(sizeof(struct ciphdr));

	hdr[0][0] = iso_data;
	ciph = (struct ciphdr *)&hdr[0][1];
	ciph->src = 0;
	ciph->len = 120;
	ciph->dbc = 0;
	ciph->eoh1 = 1;
	ciph->fdf.dv.cyc = 0xffff;

	for (i = 1; i < NPACKET_T; i++) {
		bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
	}

	gettimeofday(&start, NULL);
#if 0
	fprintf(stderr, "%08x %08x %08x\n",
			htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
#endif
	frames = 0;
	packets = 0;
	pad_acc = 0;
	cycle = 1;
	cycle_acc = frame_cycle[system].d * cycle;
	while (1) {
		tlen = 0;
		while (tlen < DSIZE * NPACKET_T) {
			len = read(fd, pbuf + tlen, DSIZE * NPACKET_T - tlen);
			if (len <= 0) {
				if (tlen > 0)
					break;
				if (len < 0)
					warn("read");
				else
					printf("\nend of file\n");
				goto send_end;
			}
			tlen += len;
		}
		vec = 0;
		count = 0;
		offset = 0;
		nhdr = 0;
next:
		dv = (struct dvdbc *)(pbuf + offset * DSIZE);
#if 0
		header = (dv->sct == 0 && dv->dseq == 0);
#else
		header = (packets % npackets[system] == 0);
#endif

		ciph = (struct ciphdr *)&hdr[nhdr][1];
		if (header) {
			fprintf(stderr, "%d", frames % 10);
			frames ++;
			if (count > 0 && frames > count)
				break;
			if (frames % 30 == 0)
				fprintf(stderr, "\n");
			fflush(stderr);
			system = ((dv->payload[0] & DV_DSF_12) != 0);
			f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf;
			f_frac = (cycle_acc % frame_cycle[system].d
					* CYCLE_FRAC) / frame_cycle[system].d;
#if 0
			ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
#else
			ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
#endif
			cycle_acc += frame_cycle[system].n;
			cycle_acc %= frame_cycle[system].d * 0x10;

		} else {
			ciph->fdf.dv.cyc = 0xffff;
		}
		ciph->dbc = packets++ % 256;
		pad_acc += pad_rate[system].n;
		if (pad_acc >= pad_rate[system].d) {
			pad_acc -= pad_rate[system].d;
			bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
			hdr[nhdr][0] = iso_empty;
			wbuf[vec].iov_base = (char *)hdr[nhdr];
			wbuf[vec++].iov_len = sizeof(hdr[0]);
			nhdr ++;
			cycle ++;
		}
		hdr[nhdr][0] = iso_data;
		wbuf[vec].iov_base = (char *)hdr[nhdr];
		wbuf[vec++].iov_len = sizeof(hdr[0]);
		wbuf[vec].iov_base = (char *)dv;
		wbuf[vec++].iov_len = DSIZE;
		nhdr ++;
		cycle ++;
		offset ++;
		if (offset * DSIZE < tlen)
			goto next;

again:
		len = writev(d, wbuf, vec);
		if (len < 0) {
			if (errno == EAGAIN) {
				fprintf(stderr, "(EAGAIN)\n");
				fflush(stderr);
				goto again;
			}
			err(1, "write failed");
		}
	}
	close(fd);
	fprintf(stderr, "\n");
send_end:
	gettimeofday(&end, NULL);
	rtime = end.tv_sec - start.tv_sec 
			+ (end.tv_usec - start.tv_usec) * 1e-6;
	fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
			frames, rtime, frames/rtime);
	return 0;
}


#if 0
int
main(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind;
	int ch;
	int fd;
	char devname[] = "/dev/fw0";

	while ((ch = getopt(argc, argv, "I:")) != -1){
		switch(ch) {
			case 'I':
				strcpy(devname, optarg);
		     		break;
			default:
				usage();
				break;
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 2)
		usage(); 

	fd = open(devname, O_RDWR);
	if (fd < 0)
		err(1, "open");
	dvrec(fd, argv[0], (TAG<<6) | CHANNEL, atoi(argv[1]));
	close(fd);
	exit(0);
}

#endif
OpenPOWER on IntegriCloud