xref: /freebsd/usr.sbin/fwcontrol/fwdv.c (revision 4db78cacdee1b6f3b7880eb8c5560e8edaf91698)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 2003
5  * 	Hidetoshi Shimokawa. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *
18  *	This product includes software developed by Hidetoshi Shimokawa.
19  *
20  * 4. Neither the name of the author nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD$
37  */
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/uio.h>
43 
44 #if __FreeBSD_version >= 500000
45 #include <arpa/inet.h>
46 #endif
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sysexits.h>
56 
57 #include <dev/firewire/firewire.h>
58 #include <dev/firewire/iec68113.h>
59 
60 #include "fwmethods.h"
61 
62 #define DEBUG		0
63 #define FIX_FRAME	1
64 
65 struct frac {
66 	int n,d;
67 };
68 
69 struct frac frame_cycle[2]  = {
70 	{8000*100, 2997},	/* NTSC 8000 cycle / 29.97 Hz */
71 	{320, 1},		/* PAL  8000 cycle / 25 Hz */
72 };
73 int npackets[] = {
74 	250		/* NTSC */,
75 	300		/* PAL */
76 };
77 struct frac pad_rate[2]  = {
78 	{203, 2997},	/* = (8000 - 29.97 * 250)/(29.97 * 250) */
79 	{1, 15},	/* = (8000 - 25 * 300)/(25 * 300) */
80 };
81 char *system_name[] = {"NTSC", "PAL"};
82 int frame_rate[] = {30, 25};
83 
84 #define PSIZE 512
85 #define DSIZE 480
86 #define NCHUNK 64
87 
88 #define NPACKET_R 256
89 #define NPACKET_T 255
90 #define TNBUF 100	/* XXX too large value causes block noise */
91 #define NEMPTY 10	/* depends on TNBUF */
92 #define RBUFSIZE (PSIZE * NPACKET_R)
93 #define MAXBLOCKS (300)
94 #define CYCLE_FRAC 0xc00
95 
96 void
97 dvrecv(int d, const char *filename, char ich, int count)
98 {
99 	struct fw_isochreq isoreq;
100 	struct fw_isobufreq bufreq;
101 	struct dvdbc *dv;
102 	struct ciphdr *ciph;
103 	struct fw_pkt *pkt;
104 	char *pad, *buf;
105 	u_int32_t *ptr;
106 	int len, tlen, npad, fd, k, m, vec, system = -1, nb;
107 	int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
108 	struct iovec wbuf[NPACKET_R];
109 
110 	if(strcmp(filename, "-") == 0) {
111 		fd = STDOUT_FILENO;
112 	} else {
113 		fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
114 		if (fd == -1)
115 			err(EX_NOINPUT, "%s", filename);
116 	}
117 	buf = malloc(RBUFSIZE);
118 	pad = malloc(DSIZE*MAXBLOCKS);
119 	memset(pad, 0xff, DSIZE*MAXBLOCKS);
120 	bzero(wbuf, sizeof(wbuf));
121 
122 	bufreq.rx.nchunk = NCHUNK;
123 	bufreq.rx.npacket = NPACKET_R;
124 	bufreq.rx.psize = PSIZE;
125 	bufreq.tx.nchunk = 0;
126 	bufreq.tx.npacket = 0;
127 	bufreq.tx.psize = 0;
128 	if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
129 		err(1, "ioctl FW_SSTBUF");
130 
131 	isoreq.ch = ich & 0x3f;
132 	isoreq.tag = (ich >> 6) & 3;
133 
134 	if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
135        		err(1, "ioctl");
136 
137 	k = m = 0;
138 	while (count <= 0 || k <= count) {
139 #if 0
140 		tlen = 0;
141 		while ((len = read(d, buf + tlen, PSIZE
142 						/* RBUFSIZE - tlen */)) > 0) {
143 			if (len < 0) {
144 				if (errno == EAGAIN) {
145 					fprintf(stderr, "(EAGAIN)\n");
146 					fflush(stderr);
147 					if (len <= 0)
148 						continue;
149 				} else
150 					err(1, "read failed");
151 			}
152 			tlen += len;
153 			if ((RBUFSIZE - tlen) < PSIZE)
154 				break;
155 		};
156 #else
157 		tlen = len = read(d, buf, RBUFSIZE);
158 		if (len < 0) {
159 			if (errno == EAGAIN) {
160 				fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
161 				fflush(stderr);
162 				if (len <= 0)
163 					continue;
164 			} else
165 				err(1, "read failed");
166 		}
167 #endif
168 		vec = 0;
169 		ptr = (u_int32_t *) buf;
170 again:
171 		pkt = (struct fw_pkt *) ptr;
172 #if DEBUG
173 		fprintf(stderr, "%08x %08x %08x %08x\n",
174 			htonl(ptr[0]), htonl(ptr[1]),
175 			htonl(ptr[2]), htonl(ptr[3]));
176 #endif
177 		ciph = (struct ciphdr *)(ptr + 1);	/* skip iso header */
178 		if (ciph->fmt != CIP_FMT_DVCR)
179 			errx(1, "unknown format 0x%x", ciph->fmt);
180 		ptr = (u_int32_t *) (ciph + 1);		/* skip cip header */
181 #if DEBUG
182 		if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
183 			fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
184 		}
185 #endif
186 		if (pkt->mode.stream.len <= sizeof(struct ciphdr))
187 			/* no payload */
188 			goto next;
189 		for (dv = (struct dvdbc *)ptr;
190 				(char *)dv < (char *)(ptr + ciph->len);
191 				dv+=6) {
192 
193 #if DEBUG
194 			fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
195 #endif
196 			if  (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
197 				if (system < 0) {
198 					system = ciph->fdf.dv.fs;
199 					fprintf(stderr, "%s\n", system_name[system]);
200 				}
201 
202 				/* Fix DSF bit */
203 				if (system == 1 &&
204 					(dv->payload[0] & DV_DSF_12) == 0)
205 					dv->payload[0] |= DV_DSF_12;
206 				nb = nblocks[system];
207  				fprintf(stderr, "%d:%02d:%02d %d\r",
208 					k / (3600 * frame_rate[system]),
209 					(k / (60 * frame_rate[system])) % 60,
210 					(k / frame_rate[system]) % 60,
211 					k % frame_rate[system]);
212 
213 #if FIX_FRAME
214 				if (m > 0 && m != nb) {
215 					/* padding bad frame */
216 					npad = ((nb - m) % nb);
217 					if (npad < 0)
218 						npad += nb;
219 					fprintf(stderr, "\n%d blocks padded\n",
220 					    npad);
221 					npad *= DSIZE;
222 					wbuf[vec].iov_base = pad;
223 					wbuf[vec++].iov_len = npad;
224 					if (vec >= NPACKET_R) {
225 						writev(fd, wbuf, vec);
226 						vec = 0;
227 					}
228 				}
229 #endif
230 				k++;
231 				fflush(stderr);
232 				m = 0;
233 			}
234 			if (k == 0 || (count > 0 && k > count))
235 				continue;
236 			m++;
237 			wbuf[vec].iov_base = (char *) dv;
238 			wbuf[vec++].iov_len = DSIZE;
239 			if (vec >= NPACKET_R) {
240 				writev(fd, wbuf, vec);
241 				vec = 0;
242 			}
243 		}
244 		ptr = (u_int32_t *)dv;
245 next:
246 		if ((char *)ptr < buf + tlen)
247 			goto again;
248 		if (vec > 0)
249 			writev(fd, wbuf, vec);
250 	}
251 	if (fd != STDOUT_FILENO)
252 		close(fd);
253 	fprintf(stderr, "\n");
254 }
255 
256 
257 void
258 dvsend(int d, const char *filename, char ich, int count)
259 {
260 	struct fw_isochreq isoreq;
261 	struct fw_isobufreq bufreq;
262 	struct dvdbc *dv;
263 	struct fw_pkt *pkt;
264 	int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
265 	int system=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac;
266 	struct iovec wbuf[TNBUF*2 + NEMPTY];
267 	char *pbuf;
268 	u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
269 	struct ciphdr *ciph;
270 	struct timeval start, end;
271 	double rtime;
272 
273 	fd = open(filename, O_RDONLY);
274 	if (fd == -1)
275 		err(EX_NOINPUT, "%s", filename);
276 
277 	pbuf = malloc(DSIZE * TNBUF);
278 	bzero(wbuf, sizeof(wbuf));
279 
280 	bufreq.rx.nchunk = 0;
281 	bufreq.rx.npacket = 0;
282 	bufreq.rx.psize = 0;
283 	bufreq.tx.nchunk = NCHUNK;
284 	bufreq.tx.npacket = NPACKET_T;
285 	bufreq.tx.psize = PSIZE;
286 	if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
287 		err(1, "ioctl FW_SSTBUF");
288 
289 	isoreq.ch = ich & 0x3f;
290 	isoreq.tag = (ich >> 6) & 3;
291 
292 	if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
293        		err(1, "ioctl FW_STSTREAM");
294 
295 	iso_data = 0;
296 	pkt = (struct fw_pkt *) &iso_data;
297 	pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
298 	pkt->mode.stream.sy = 0;
299 	pkt->mode.stream.tcode = FWTCODE_STREAM;
300 	pkt->mode.stream.chtag = ich;
301 	iso_empty = iso_data;
302 	pkt = (struct fw_pkt *) &iso_empty;
303 	pkt->mode.stream.len = sizeof(struct ciphdr);
304 
305 	bzero(hdr[0], sizeof(hdr[0]));
306 	hdr[0][0] = iso_data;
307 	ciph = (struct ciphdr *)&hdr[0][1];
308 	ciph->src = 0;	 /* XXX */
309 	ciph->len = 120;
310 	ciph->dbc = 0;
311 	ciph->eoh1 = 1;
312 	ciph->fdf.dv.cyc = 0xffff;
313 
314 	for (i = 1; i < TNBUF; i++)
315 		bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
316 
317 	gettimeofday(&start, NULL);
318 #if DEBUG
319 	fprintf(stderr, "%08x %08x %08x\n",
320 			htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
321 #endif
322 	frames = 0;
323 	packets = 0;
324 	pad_acc = 0;
325 	while (1) {
326 		tlen = 0;
327 		while (tlen < DSIZE * TNBUF) {
328 			len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
329 			if (len <= 0) {
330 				if (tlen > 0)
331 					break;
332 				if (len < 0)
333 					warn("read");
334 				else
335 					fprintf(stderr, "\nend of file\n");
336 				goto send_end;
337 			}
338 			tlen += len;
339 		}
340 		vec = 0;
341 		offset = 0;
342 		nhdr = 0;
343 next:
344 		dv = (struct dvdbc *)(pbuf + offset * DSIZE);
345 #if 0
346 		header = (dv->sct == 0 && dv->dseq == 0);
347 #else
348 		header = (packets == 0 || packets % npackets[system] == 0);
349 #endif
350 
351 		ciph = (struct ciphdr *)&hdr[nhdr][1];
352 		if (header) {
353 			if (system < 0) {
354 				system = ((dv->payload[0] & DV_DSF_12) != 0);
355 				printf("%s\n", system_name[system]);
356 				cycle = 1;
357 				cycle_acc = frame_cycle[system].d * cycle;
358 			}
359 			fprintf(stderr, "%d", frames % 10);
360 			frames ++;
361 			if (count > 0 && frames > count)
362 				break;
363 			if (frames % frame_rate[system] == 0)
364 				fprintf(stderr, "\n");
365 			fflush(stderr);
366 			f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf;
367 			f_frac = (cycle_acc % frame_cycle[system].d
368 					* CYCLE_FRAC) / frame_cycle[system].d;
369 #if 0
370 			ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
371 #else
372 			ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
373 #endif
374 			cycle_acc += frame_cycle[system].n;
375 			cycle_acc %= frame_cycle[system].d * 0x10;
376 
377 		} else {
378 			ciph->fdf.dv.cyc = 0xffff;
379 		}
380 		ciph->dbc = packets++ % 256;
381 		pad_acc += pad_rate[system].n;
382 		if (pad_acc >= pad_rate[system].d) {
383 			pad_acc -= pad_rate[system].d;
384 			bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
385 			hdr[nhdr][0] = iso_empty;
386 			wbuf[vec].iov_base = (char *)hdr[nhdr];
387 			wbuf[vec++].iov_len = sizeof(hdr[0]);
388 			nhdr ++;
389 			cycle ++;
390 		}
391 		hdr[nhdr][0] = iso_data;
392 		wbuf[vec].iov_base = (char *)hdr[nhdr];
393 		wbuf[vec++].iov_len = sizeof(hdr[0]);
394 		wbuf[vec].iov_base = (char *)dv;
395 		wbuf[vec++].iov_len = DSIZE;
396 		nhdr ++;
397 		cycle ++;
398 		offset ++;
399 		if (offset * DSIZE < tlen)
400 			goto next;
401 
402 again:
403 		len = writev(d, wbuf, vec);
404 		if (len < 0) {
405 			if (errno == EAGAIN) {
406 				fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
407 				goto again;
408 			}
409 			err(1, "write failed");
410 		}
411 	}
412 	fprintf(stderr, "\n");
413 send_end:
414 	gettimeofday(&end, NULL);
415 	rtime = end.tv_sec - start.tv_sec
416 			+ (end.tv_usec - start.tv_usec) * 1e-6;
417 	fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
418 			frames, rtime, frames/rtime);
419 	close(fd);
420 }
421