1163611Sphk/*
2163611Sphk * Copyright (C) 2003
3163611Sphk * 	Hidetoshi Shimokawa. All rights reserved.
4163611Sphk *
5163611Sphk * Redistribution and use in source and binary forms, with or without
6163611Sphk * modification, are permitted provided that the following conditions
7163611Sphk * are met:
8163611Sphk * 1. Redistributions of source code must retain the above copyright
9163611Sphk *    notice, this list of conditions and the following disclaimer.
10163611Sphk * 2. Redistributions in binary form must reproduce the above copyright
11163611Sphk *    notice, this list of conditions and the following disclaimer in the
12163611Sphk *    documentation and/or other materials provided with the distribution.
13163611Sphk * 3. All advertising materials mentioning features or use of this software
14163611Sphk *    must display the following acknowledgement:
15163611Sphk *
16163611Sphk *	This product includes software developed by Hidetoshi Shimokawa.
17163611Sphk *
18163611Sphk * 4. Neither the name of the author nor the names of its contributors
19163611Sphk *    may be used to endorse or promote products derived from this software
20163611Sphk *    without specific prior written permission.
21163611Sphk *
22163611Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23163611Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24163611Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25163611Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26163611Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27163611Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28163611Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29163611Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30163611Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31163611Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32163611Sphk * SUCH DAMAGE.
33163611Sphk *
34163611Sphk * $FreeBSD$
35163611Sphk */
36163611Sphk#include <sys/param.h>
37163611Sphk#include <sys/ioctl.h>
38163611Sphk#include <sys/time.h>
39163611Sphk#include <sys/types.h>
40163611Sphk#include <sys/uio.h>
41163611Sphk
42163611Sphk#if __FreeBSD_version >= 500000
43163611Sphk#include <arpa/inet.h>
44163611Sphk#endif
45163611Sphk
46163611Sphk#include <err.h>
47163611Sphk#include <errno.h>
48163611Sphk#include <unistd.h>
49163611Sphk#include <fcntl.h>
50163611Sphk#include <stdio.h>
51163646Sphk#include <stdlib.h>
52163646Sphk#include <string.h>
53163611Sphk#include <sysexits.h>
54163611Sphk
55163611Sphk#include <dev/firewire/firewire.h>
56163611Sphk#include <dev/firewire/iec68113.h>
57163611Sphk
58163611Sphk#include "fwmethods.h"
59163611Sphk
60163611Sphk#define DEBUG		0
61163611Sphk#define FIX_FRAME	1
62163611Sphk
63163611Sphkstruct frac {
64163611Sphk	int n,d;
65163611Sphk};
66163611Sphk
67163611Sphkstruct frac frame_cycle[2]  = {
68163611Sphk	{8000*100, 2997},	/* NTSC 8000 cycle / 29.97 Hz */
69163611Sphk	{320, 1},		/* PAL  8000 cycle / 25 Hz */
70163611Sphk};
71163611Sphkint npackets[] = {
72163611Sphk	250		/* NTSC */,
73163611Sphk	300		/* PAL */
74163611Sphk};
75163611Sphkstruct frac pad_rate[2]  = {
76163611Sphk	{203, 2997},	/* = (8000 - 29.97 * 250)/(29.97 * 250) */
77163611Sphk	{1, 15},	/* = (8000 - 25 * 300)/(25 * 300) */
78163611Sphk};
79163611Sphkchar *system_name[] = {"NTSC", "PAL"};
80163611Sphkint frame_rate[] = {30, 25};
81163611Sphk
82163611Sphk#define PSIZE 512
83163611Sphk#define DSIZE 480
84163611Sphk#define NCHUNK 64
85163611Sphk
86163611Sphk#define NPACKET_R 256
87163611Sphk#define NPACKET_T 255
88163611Sphk#define TNBUF 100	/* XXX too large value causes block noise */
89163611Sphk#define NEMPTY 10	/* depends on TNBUF */
90163611Sphk#define RBUFSIZE (PSIZE * NPACKET_R)
91163611Sphk#define MAXBLOCKS (300)
92163611Sphk#define CYCLE_FRAC 0xc00
93163611Sphk
94163611Sphkvoid
95163611Sphkdvrecv(int d, const char *filename, char ich, int count)
96163611Sphk{
97163611Sphk	struct fw_isochreq isoreq;
98163611Sphk	struct fw_isobufreq bufreq;
99163611Sphk	struct dvdbc *dv;
100163611Sphk	struct ciphdr *ciph;
101163611Sphk	struct fw_pkt *pkt;
102163611Sphk	char *pad, *buf;
103163611Sphk	u_int32_t *ptr;
104163611Sphk	int len, tlen, npad, fd, k, m, vec, system = -1, nb;
105163611Sphk	int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
106163611Sphk	struct iovec wbuf[NPACKET_R];
107163611Sphk
108163611Sphk	if(strcmp(filename, "-") == 0) {
109163611Sphk		fd = STDOUT_FILENO;
110163611Sphk	} else {
111163611Sphk		fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
112163611Sphk		if (fd == -1)
113163611Sphk			err(EX_NOINPUT, "%s", filename);
114163611Sphk	}
115163611Sphk	buf = malloc(RBUFSIZE);
116163611Sphk	pad = malloc(DSIZE*MAXBLOCKS);
117163611Sphk	memset(pad, 0xff, DSIZE*MAXBLOCKS);
118163611Sphk	bzero(wbuf, sizeof(wbuf));
119163611Sphk
120163611Sphk	bufreq.rx.nchunk = NCHUNK;
121163611Sphk	bufreq.rx.npacket = NPACKET_R;
122163611Sphk	bufreq.rx.psize = PSIZE;
123163611Sphk	bufreq.tx.nchunk = 0;
124163611Sphk	bufreq.tx.npacket = 0;
125163611Sphk	bufreq.tx.psize = 0;
126163611Sphk	if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
127163611Sphk		err(1, "ioctl FW_SSTBUF");
128163611Sphk
129163611Sphk	isoreq.ch = ich & 0x3f;
130163611Sphk	isoreq.tag = (ich >> 6) & 3;
131163611Sphk
132163611Sphk	if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
133163611Sphk       		err(1, "ioctl");
134163611Sphk
135163611Sphk	k = m = 0;
136163611Sphk	while (count <= 0 || k <= count) {
137163611Sphk#if 0
138163646Sphk		tlen = 0;
139163611Sphk		while ((len = read(d, buf + tlen, PSIZE
140163611Sphk						/* RBUFSIZE - tlen */)) > 0) {
141163611Sphk			if (len < 0) {
142163611Sphk				if (errno == EAGAIN) {
143163611Sphk					fprintf(stderr, "(EAGAIN)\n");
144163646Sphk					fflush(stderr);
145163646Sphk					if (len <= 0)
146163611Sphk						continue;
147163611Sphk				} else
148163611Sphk					err(1, "read failed");
149163611Sphk			}
150163611Sphk			tlen += len;
151163611Sphk			if ((RBUFSIZE - tlen) < PSIZE)
152163611Sphk				break;
153163611Sphk		};
154163611Sphk#else
155163611Sphk		tlen = len = read(d, buf, RBUFSIZE);
156163611Sphk		if (len < 0) {
157163611Sphk			if (errno == EAGAIN) {
158163611Sphk				fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
159163611Sphk				fflush(stderr);
160163611Sphk				if (len <= 0)
161163611Sphk					continue;
162163611Sphk			} else
163163611Sphk				err(1, "read failed");
164163611Sphk		}
165163611Sphk#endif
166163611Sphk		vec = 0;
167163611Sphk		ptr = (u_int32_t *) buf;
168163611Sphkagain:
169163611Sphk		pkt = (struct fw_pkt *) ptr;
170163611Sphk#if DEBUG
171163611Sphk		fprintf(stderr, "%08x %08x %08x %08x\n",
172163611Sphk			htonl(ptr[0]), htonl(ptr[1]),
173163611Sphk			htonl(ptr[2]), htonl(ptr[3]));
174163611Sphk#endif
175163611Sphk		ciph = (struct ciphdr *)(ptr + 1);	/* skip iso header */
176163611Sphk		if (ciph->fmt != CIP_FMT_DVCR)
177163611Sphk			errx(1, "unknown format 0x%x", ciph->fmt);
178163611Sphk		ptr = (u_int32_t *) (ciph + 1);		/* skip cip header */
179163611Sphk#if DEBUG
180163611Sphk		if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
181163611Sphk			fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
182163611Sphk		}
183163611Sphk#endif
184163611Sphk		if (pkt->mode.stream.len <= sizeof(struct ciphdr))
185163611Sphk			/* no payload */
186163611Sphk			goto next;
187163611Sphk		for (dv = (struct dvdbc *)ptr;
188163611Sphk				(char *)dv < (char *)(ptr + ciph->len);
189163611Sphk				dv+=6) {
190163611Sphk
191163611Sphk#if DEBUG
192163611Sphk			fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
193163611Sphk#endif
194163611Sphk			if  (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
195163611Sphk				if (system < 0) {
196163611Sphk					system = ciph->fdf.dv.fs;
197163611Sphk					fprintf(stderr, "%s\n", system_name[system]);
198163611Sphk				}
199163611Sphk
200163611Sphk				/* Fix DSF bit */
201163611Sphk				if (system == 1 &&
202163611Sphk					(dv->payload[0] & DV_DSF_12) == 0)
203163611Sphk					dv->payload[0] |= DV_DSF_12;
204163611Sphk				nb = nblocks[system];
205163611Sphk 				fprintf(stderr, "%d:%02d:%02d %d\r",
206163611Sphk					k / (3600 * frame_rate[system]),
207163611Sphk					(k / (60 * frame_rate[system])) % 60,
208163611Sphk					(k / frame_rate[system]) % 60,
209163611Sphk					k % frame_rate[system]);
210163611Sphk
211163611Sphk#if FIX_FRAME
212163611Sphk				if (m > 0 && m != nb) {
213163611Sphk					/* padding bad frame */
214163611Sphk					npad = ((nb - m) % nb);
215163611Sphk					if (npad < 0)
216163611Sphk						npad += nb;
217163611Sphk					fprintf(stderr, "\n%d blocks padded\n",
218163646Sphk					    npad);
219163611Sphk					npad *= DSIZE;
220163611Sphk					wbuf[vec].iov_base = pad;
221163611Sphk					wbuf[vec++].iov_len = npad;
222163611Sphk					if (vec >= NPACKET_R) {
223163611Sphk						writev(fd, wbuf, vec);
224163611Sphk						vec = 0;
225163611Sphk					}
226163611Sphk				}
227163611Sphk#endif
228163611Sphk				k++;
229163611Sphk				fflush(stderr);
230163611Sphk				m = 0;
231163611Sphk			}
232163611Sphk			if (k == 0 || (count > 0 && k > count))
233163611Sphk				continue;
234163611Sphk			m++;
235163611Sphk			wbuf[vec].iov_base = (char *) dv;
236163611Sphk			wbuf[vec++].iov_len = DSIZE;
237163611Sphk			if (vec >= NPACKET_R) {
238163611Sphk				writev(fd, wbuf, vec);
239163611Sphk				vec = 0;
240163611Sphk			}
241163611Sphk		}
242163611Sphk		ptr = (u_int32_t *)dv;
243163611Sphknext:
244163611Sphk		if ((char *)ptr < buf + tlen)
245163611Sphk			goto again;
246163611Sphk		if (vec > 0)
247163611Sphk			writev(fd, wbuf, vec);
248163611Sphk	}
249163646Sphk	if (fd != STDOUT_FILENO)
250163646Sphk		close(fd);
251163611Sphk	fprintf(stderr, "\n");
252163611Sphk}
253163611Sphk
254163611Sphk
255163611Sphkvoid
256163611Sphkdvsend(int d, const char *filename, char ich, int count)
257163611Sphk{
258163611Sphk	struct fw_isochreq isoreq;
259163611Sphk	struct fw_isobufreq bufreq;
260163611Sphk	struct dvdbc *dv;
261163611Sphk	struct fw_pkt *pkt;
262163611Sphk	int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
263163611Sphk	int system=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac;
264163611Sphk	struct iovec wbuf[TNBUF*2 + NEMPTY];
265163611Sphk	char *pbuf;
266163611Sphk	u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
267163611Sphk	struct ciphdr *ciph;
268163611Sphk	struct timeval start, end;
269163611Sphk	double rtime;
270163611Sphk
271163611Sphk	fd = open(filename, O_RDONLY);
272163611Sphk	if (fd == -1)
273163611Sphk		err(EX_NOINPUT, "%s", filename);
274163611Sphk
275163611Sphk	pbuf = malloc(DSIZE * TNBUF);
276163611Sphk	bzero(wbuf, sizeof(wbuf));
277163611Sphk
278163611Sphk	bufreq.rx.nchunk = 0;
279163611Sphk	bufreq.rx.npacket = 0;
280163611Sphk	bufreq.rx.psize = 0;
281163611Sphk	bufreq.tx.nchunk = NCHUNK;
282163611Sphk	bufreq.tx.npacket = NPACKET_T;
283163611Sphk	bufreq.tx.psize = PSIZE;
284163611Sphk	if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
285163611Sphk		err(1, "ioctl FW_SSTBUF");
286163611Sphk
287163611Sphk	isoreq.ch = ich & 0x3f;
288163611Sphk	isoreq.tag = (ich >> 6) & 3;
289163611Sphk
290163611Sphk	if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
291163611Sphk       		err(1, "ioctl FW_STSTREAM");
292163611Sphk
293163611Sphk	iso_data = 0;
294163611Sphk	pkt = (struct fw_pkt *) &iso_data;
295163611Sphk	pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
296163611Sphk	pkt->mode.stream.sy = 0;
297163611Sphk	pkt->mode.stream.tcode = FWTCODE_STREAM;
298163611Sphk	pkt->mode.stream.chtag = ich;
299163611Sphk	iso_empty = iso_data;
300163611Sphk	pkt = (struct fw_pkt *) &iso_empty;
301163611Sphk	pkt->mode.stream.len = sizeof(struct ciphdr);
302163611Sphk
303163611Sphk	bzero(hdr[0], sizeof(hdr[0]));
304163611Sphk	hdr[0][0] = iso_data;
305163611Sphk	ciph = (struct ciphdr *)&hdr[0][1];
306163611Sphk	ciph->src = 0;	 /* XXX */
307163611Sphk	ciph->len = 120;
308	ciph->dbc = 0;
309	ciph->eoh1 = 1;
310	ciph->fdf.dv.cyc = 0xffff;
311
312	for (i = 1; i < TNBUF; i++)
313		bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
314
315	gettimeofday(&start, NULL);
316#if DEBUG
317	fprintf(stderr, "%08x %08x %08x\n",
318			htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
319#endif
320	frames = 0;
321	packets = 0;
322	pad_acc = 0;
323	while (1) {
324		tlen = 0;
325		while (tlen < DSIZE * TNBUF) {
326			len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
327			if (len <= 0) {
328				if (tlen > 0)
329					break;
330				if (len < 0)
331					warn("read");
332				else
333					fprintf(stderr, "\nend of file\n");
334				goto send_end;
335			}
336			tlen += len;
337		}
338		vec = 0;
339		offset = 0;
340		nhdr = 0;
341next:
342		dv = (struct dvdbc *)(pbuf + offset * DSIZE);
343#if 0
344		header = (dv->sct == 0 && dv->dseq == 0);
345#else
346		header = (packets == 0 || packets % npackets[system] == 0);
347#endif
348
349		ciph = (struct ciphdr *)&hdr[nhdr][1];
350		if (header) {
351			if (system < 0) {
352				system = ((dv->payload[0] & DV_DSF_12) != 0);
353				printf("%s\n", system_name[system]);
354				cycle = 1;
355				cycle_acc = frame_cycle[system].d * cycle;
356			}
357			fprintf(stderr, "%d", frames % 10);
358			frames ++;
359			if (count > 0 && frames > count)
360				break;
361			if (frames % frame_rate[system] == 0)
362				fprintf(stderr, "\n");
363			fflush(stderr);
364			f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf;
365			f_frac = (cycle_acc % frame_cycle[system].d
366					* CYCLE_FRAC) / frame_cycle[system].d;
367#if 0
368			ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
369#else
370			ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
371#endif
372			cycle_acc += frame_cycle[system].n;
373			cycle_acc %= frame_cycle[system].d * 0x10;
374
375		} else {
376			ciph->fdf.dv.cyc = 0xffff;
377		}
378		ciph->dbc = packets++ % 256;
379		pad_acc += pad_rate[system].n;
380		if (pad_acc >= pad_rate[system].d) {
381			pad_acc -= pad_rate[system].d;
382			bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
383			hdr[nhdr][0] = iso_empty;
384			wbuf[vec].iov_base = (char *)hdr[nhdr];
385			wbuf[vec++].iov_len = sizeof(hdr[0]);
386			nhdr ++;
387			cycle ++;
388		}
389		hdr[nhdr][0] = iso_data;
390		wbuf[vec].iov_base = (char *)hdr[nhdr];
391		wbuf[vec++].iov_len = sizeof(hdr[0]);
392		wbuf[vec].iov_base = (char *)dv;
393		wbuf[vec++].iov_len = DSIZE;
394		nhdr ++;
395		cycle ++;
396		offset ++;
397		if (offset * DSIZE < tlen)
398			goto next;
399
400again:
401		len = writev(d, wbuf, vec);
402		if (len < 0) {
403			if (errno == EAGAIN) {
404				fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
405				goto again;
406			}
407			err(1, "write failed");
408		}
409	}
410	close(fd);
411	fprintf(stderr, "\n");
412send_end:
413	gettimeofday(&end, NULL);
414	rtime = end.tv_sec - start.tv_sec
415			+ (end.tv_usec - start.tv_usec) * 1e-6;
416	fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
417			frames, rtime, frames/rtime);
418}
419