2 * SPDX-License-Identifier: BSD-4-Clause
5 * Hidetoshi Shimokawa. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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:
18 * This product includes software developed by Hidetoshi Shimokawa.
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.
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
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
41 #include <sys/types.h>
43 #include <arpa/inet.h>
53 #include <dev/firewire/firewire.h>
54 #include <dev/firewire/iec68113.h>
56 #include "fwmethods.h"
65 struct frac frame_cycle[2] = {
66 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */
67 {320, 1}, /* PAL 8000 cycle / 25 Hz */
73 struct frac pad_rate[2] = {
74 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */
75 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */
77 char *system_name[] = {"NTSC", "PAL"};
78 int frame_rate[] = {30, 25};
86 #define TNBUF 100 /* XXX too large value causes block noise */
87 #define NEMPTY 10 /* depends on TNBUF */
88 #define RBUFSIZE (PSIZE * NPACKET_R)
89 #define MAXBLOCKS (300)
90 #define CYCLE_FRAC 0xc00
93 dvrecv(int d, const char *filename, char ich, int count)
95 struct fw_isochreq isoreq;
96 struct fw_isobufreq bufreq;
102 int len, tlen, npad, fd, k, m, vec, system = -1, nb;
103 int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
104 struct iovec wbuf[NPACKET_R];
106 if(strcmp(filename, "-") == 0) {
109 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
111 err(EX_NOINPUT, "%s", filename);
113 buf = malloc(RBUFSIZE);
114 pad = malloc(DSIZE*MAXBLOCKS);
115 memset(pad, 0xff, DSIZE*MAXBLOCKS);
116 bzero(wbuf, sizeof(wbuf));
118 bufreq.rx.nchunk = NCHUNK;
119 bufreq.rx.npacket = NPACKET_R;
120 bufreq.rx.psize = PSIZE;
121 bufreq.tx.nchunk = 0;
122 bufreq.tx.npacket = 0;
124 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
125 err(1, "ioctl FW_SSTBUF");
127 isoreq.ch = ich & 0x3f;
128 isoreq.tag = (ich >> 6) & 3;
130 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
134 while (count <= 0 || k <= count) {
137 while ((len = read(d, buf + tlen, PSIZE
138 /* RBUFSIZE - tlen */)) > 0) {
140 if (errno == EAGAIN) {
141 fprintf(stderr, "(EAGAIN)\n");
146 err(1, "read failed");
149 if ((RBUFSIZE - tlen) < PSIZE)
153 tlen = len = read(d, buf, RBUFSIZE);
155 if (errno == EAGAIN) {
156 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
161 err(1, "read failed");
165 ptr = (u_int32_t *) buf;
167 pkt = (struct fw_pkt *) ptr;
169 fprintf(stderr, "%08x %08x %08x %08x\n",
170 htonl(ptr[0]), htonl(ptr[1]),
171 htonl(ptr[2]), htonl(ptr[3]));
173 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */
174 if (ciph->fmt != CIP_FMT_DVCR)
175 errx(1, "unknown format 0x%x", ciph->fmt);
176 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */
178 if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
179 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
182 if (pkt->mode.stream.len <= sizeof(struct ciphdr))
185 for (dv = (struct dvdbc *)ptr;
186 (char *)dv < (char *)(ptr + ciph->len);
190 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
192 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
194 system = ciph->fdf.dv.fs;
195 fprintf(stderr, "%s\n", system_name[system]);
200 (dv->payload[0] & DV_DSF_12) == 0)
201 dv->payload[0] |= DV_DSF_12;
202 nb = nblocks[system];
203 fprintf(stderr, "%d:%02d:%02d %d\r",
204 k / (3600 * frame_rate[system]),
205 (k / (60 * frame_rate[system])) % 60,
206 (k / frame_rate[system]) % 60,
207 k % frame_rate[system]);
210 if (m > 0 && m != nb) {
211 /* padding bad frame */
212 npad = ((nb - m) % nb);
215 fprintf(stderr, "\n%d blocks padded\n",
218 wbuf[vec].iov_base = pad;
219 wbuf[vec++].iov_len = npad;
220 if (vec >= NPACKET_R) {
221 writev(fd, wbuf, vec);
230 if (k == 0 || (count > 0 && k > count))
233 wbuf[vec].iov_base = (char *) dv;
234 wbuf[vec++].iov_len = DSIZE;
235 if (vec >= NPACKET_R) {
236 writev(fd, wbuf, vec);
240 ptr = (u_int32_t *)dv;
242 if ((char *)ptr < buf + tlen)
245 writev(fd, wbuf, vec);
247 if (fd != STDOUT_FILENO)
249 fprintf(stderr, "\n");
254 dvsend(int d, const char *filename, char ich, int count)
256 struct fw_isochreq isoreq;
257 struct fw_isobufreq bufreq;
260 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
261 int system=-1, pad_acc, cycle_acc, cycle, f_frac;
262 struct iovec wbuf[TNBUF*2 + NEMPTY];
264 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
266 struct timeval start, end;
269 fd = open(filename, O_RDONLY);
271 err(EX_NOINPUT, "%s", filename);
273 pbuf = malloc(DSIZE * TNBUF);
274 bzero(wbuf, sizeof(wbuf));
276 bufreq.rx.nchunk = 0;
277 bufreq.rx.npacket = 0;
279 bufreq.tx.nchunk = NCHUNK;
280 bufreq.tx.npacket = NPACKET_T;
281 bufreq.tx.psize = PSIZE;
282 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
283 err(1, "ioctl FW_SSTBUF");
285 isoreq.ch = ich & 0x3f;
286 isoreq.tag = (ich >> 6) & 3;
288 if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
289 err(1, "ioctl FW_STSTREAM");
292 pkt = (struct fw_pkt *) &iso_data;
293 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
294 pkt->mode.stream.sy = 0;
295 pkt->mode.stream.tcode = FWTCODE_STREAM;
296 pkt->mode.stream.chtag = ich;
297 iso_empty = iso_data;
298 pkt = (struct fw_pkt *) &iso_empty;
299 pkt->mode.stream.len = sizeof(struct ciphdr);
301 bzero(hdr[0], sizeof(hdr[0]));
302 hdr[0][0] = iso_data;
303 ciph = (struct ciphdr *)&hdr[0][1];
304 ciph->src = 0; /* XXX */
308 ciph->fdf.dv.cyc = 0xffff;
310 for (i = 1; i < TNBUF; i++)
311 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
313 gettimeofday(&start, NULL);
315 fprintf(stderr, "%08x %08x %08x\n",
316 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
323 while (tlen < DSIZE * TNBUF) {
324 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
331 fprintf(stderr, "\nend of file\n");
340 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
342 header = (dv->sct == 0 && dv->dseq == 0);
344 header = (packets == 0 || packets % npackets[system] == 0);
347 ciph = (struct ciphdr *)&hdr[nhdr][1];
350 system = ((dv->payload[0] & DV_DSF_12) != 0);
351 printf("%s\n", system_name[system]);
353 cycle_acc = frame_cycle[system].d * cycle;
355 fprintf(stderr, "%d", frames % 10);
357 if (count > 0 && frames > count)
359 if (frames % frame_rate[system] == 0)
360 fprintf(stderr, "\n");
362 f_frac = (cycle_acc % frame_cycle[system].d
363 * CYCLE_FRAC) / frame_cycle[system].d;
366 f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf;
367 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
369 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
371 cycle_acc += frame_cycle[system].n;
372 cycle_acc %= frame_cycle[system].d * 0x10;
375 ciph->fdf.dv.cyc = 0xffff;
377 ciph->dbc = packets++ % 256;
378 pad_acc += pad_rate[system].n;
379 if (pad_acc >= pad_rate[system].d) {
380 pad_acc -= pad_rate[system].d;
381 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
382 hdr[nhdr][0] = iso_empty;
383 wbuf[vec].iov_base = (char *)hdr[nhdr];
384 wbuf[vec++].iov_len = sizeof(hdr[0]);
388 hdr[nhdr][0] = iso_data;
389 wbuf[vec].iov_base = (char *)hdr[nhdr];
390 wbuf[vec++].iov_len = sizeof(hdr[0]);
391 wbuf[vec].iov_base = (char *)dv;
392 wbuf[vec++].iov_len = DSIZE;
396 if (offset * DSIZE < tlen)
400 len = writev(d, wbuf, vec);
402 if (errno == EAGAIN) {
403 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
406 err(1, "write failed");
409 fprintf(stderr, "\n");
411 gettimeofday(&end, NULL);
412 rtime = end.tv_sec - start.tv_sec
413 + (end.tv_usec - start.tv_usec) * 1e-6;
414 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
415 frames, rtime, frames/rtime);