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
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
39 #include <sys/types.h>
41 #include <arpa/inet.h>
51 #include <dev/firewire/firewire.h>
52 #include <dev/firewire/iec68113.h>
54 #include "fwmethods.h"
63 struct frac frame_cycle[2] = {
64 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */
65 {320, 1}, /* PAL 8000 cycle / 25 Hz */
71 struct frac pad_rate[2] = {
72 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */
73 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */
75 char *system_name[] = {"NTSC", "PAL"};
76 int frame_rate[] = {30, 25};
84 #define TNBUF 100 /* XXX too large value causes block noise */
85 #define NEMPTY 10 /* depends on TNBUF */
86 #define RBUFSIZE (PSIZE * NPACKET_R)
87 #define MAXBLOCKS (300)
88 #define CYCLE_FRAC 0xc00
91 dvrecv(int d, const char *filename, char ich, int count)
93 struct fw_isochreq isoreq;
94 struct fw_isobufreq bufreq;
100 int len, tlen, npad, fd, k, m, vec, system = -1, nb;
101 int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
102 struct iovec wbuf[NPACKET_R];
104 if(strcmp(filename, "-") == 0) {
107 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
109 err(EX_NOINPUT, "%s", filename);
111 buf = malloc(RBUFSIZE);
112 pad = malloc(DSIZE*MAXBLOCKS);
113 memset(pad, 0xff, DSIZE*MAXBLOCKS);
114 bzero(wbuf, sizeof(wbuf));
116 bufreq.rx.nchunk = NCHUNK;
117 bufreq.rx.npacket = NPACKET_R;
118 bufreq.rx.psize = PSIZE;
119 bufreq.tx.nchunk = 0;
120 bufreq.tx.npacket = 0;
122 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
123 err(1, "ioctl FW_SSTBUF");
125 isoreq.ch = ich & 0x3f;
126 isoreq.tag = (ich >> 6) & 3;
128 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
132 while (count <= 0 || k <= count) {
135 while ((len = read(d, buf + tlen, PSIZE
136 /* RBUFSIZE - tlen */)) > 0) {
138 if (errno == EAGAIN) {
139 fprintf(stderr, "(EAGAIN)\n");
144 err(1, "read failed");
147 if ((RBUFSIZE - tlen) < PSIZE)
151 tlen = len = read(d, buf, RBUFSIZE);
153 if (errno == EAGAIN) {
154 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
159 err(1, "read failed");
163 ptr = (u_int32_t *) buf;
165 pkt = (struct fw_pkt *) ptr;
167 fprintf(stderr, "%08x %08x %08x %08x\n",
168 htonl(ptr[0]), htonl(ptr[1]),
169 htonl(ptr[2]), htonl(ptr[3]));
171 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */
172 if (ciph->fmt != CIP_FMT_DVCR)
173 errx(1, "unknown format 0x%x", ciph->fmt);
174 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */
176 if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
177 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
180 if (pkt->mode.stream.len <= sizeof(struct ciphdr))
183 for (dv = (struct dvdbc *)ptr;
184 (char *)dv < (char *)(ptr + ciph->len);
188 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
190 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
192 system = ciph->fdf.dv.fs;
193 fprintf(stderr, "%s\n", system_name[system]);
198 (dv->payload[0] & DV_DSF_12) == 0)
199 dv->payload[0] |= DV_DSF_12;
200 nb = nblocks[system];
201 fprintf(stderr, "%d:%02d:%02d %d\r",
202 k / (3600 * frame_rate[system]),
203 (k / (60 * frame_rate[system])) % 60,
204 (k / frame_rate[system]) % 60,
205 k % frame_rate[system]);
208 if (m > 0 && m != nb) {
209 /* padding bad frame */
210 npad = ((nb - m) % nb);
213 fprintf(stderr, "\n%d blocks padded\n",
216 wbuf[vec].iov_base = pad;
217 wbuf[vec++].iov_len = npad;
218 if (vec >= NPACKET_R) {
219 writev(fd, wbuf, vec);
228 if (k == 0 || (count > 0 && k > count))
231 wbuf[vec].iov_base = (char *) dv;
232 wbuf[vec++].iov_len = DSIZE;
233 if (vec >= NPACKET_R) {
234 writev(fd, wbuf, vec);
238 ptr = (u_int32_t *)dv;
240 if ((char *)ptr < buf + tlen)
243 writev(fd, wbuf, vec);
245 if (fd != STDOUT_FILENO)
247 fprintf(stderr, "\n");
252 dvsend(int d, const char *filename, char ich, int count)
254 struct fw_isochreq isoreq;
255 struct fw_isobufreq bufreq;
258 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
259 int system=-1, pad_acc, cycle_acc, cycle, f_frac;
260 struct iovec wbuf[TNBUF*2 + NEMPTY];
262 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
264 struct timeval start, end;
267 fd = open(filename, O_RDONLY);
269 err(EX_NOINPUT, "%s", filename);
271 pbuf = malloc(DSIZE * TNBUF);
272 bzero(wbuf, sizeof(wbuf));
274 bufreq.rx.nchunk = 0;
275 bufreq.rx.npacket = 0;
277 bufreq.tx.nchunk = NCHUNK;
278 bufreq.tx.npacket = NPACKET_T;
279 bufreq.tx.psize = PSIZE;
280 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
281 err(1, "ioctl FW_SSTBUF");
283 isoreq.ch = ich & 0x3f;
284 isoreq.tag = (ich >> 6) & 3;
286 if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
287 err(1, "ioctl FW_STSTREAM");
290 pkt = (struct fw_pkt *) &iso_data;
291 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
292 pkt->mode.stream.sy = 0;
293 pkt->mode.stream.tcode = FWTCODE_STREAM;
294 pkt->mode.stream.chtag = ich;
295 iso_empty = iso_data;
296 pkt = (struct fw_pkt *) &iso_empty;
297 pkt->mode.stream.len = sizeof(struct ciphdr);
299 bzero(hdr[0], sizeof(hdr[0]));
300 hdr[0][0] = iso_data;
301 ciph = (struct ciphdr *)&hdr[0][1];
302 ciph->src = 0; /* XXX */
306 ciph->fdf.dv.cyc = 0xffff;
308 for (i = 1; i < TNBUF; i++)
309 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
311 gettimeofday(&start, NULL);
313 fprintf(stderr, "%08x %08x %08x\n",
314 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
321 while (tlen < DSIZE * TNBUF) {
322 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
329 fprintf(stderr, "\nend of file\n");
338 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
340 header = (dv->sct == 0 && dv->dseq == 0);
342 header = (packets == 0 || packets % npackets[system] == 0);
345 ciph = (struct ciphdr *)&hdr[nhdr][1];
348 system = ((dv->payload[0] & DV_DSF_12) != 0);
349 printf("%s\n", system_name[system]);
351 cycle_acc = frame_cycle[system].d * cycle;
353 fprintf(stderr, "%d", frames % 10);
355 if (count > 0 && frames > count)
357 if (frames % frame_rate[system] == 0)
358 fprintf(stderr, "\n");
360 f_frac = (cycle_acc % frame_cycle[system].d
361 * CYCLE_FRAC) / frame_cycle[system].d;
364 f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf;
365 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
367 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
369 cycle_acc += frame_cycle[system].n;
370 cycle_acc %= frame_cycle[system].d * 0x10;
373 ciph->fdf.dv.cyc = 0xffff;
375 ciph->dbc = packets++ % 256;
376 pad_acc += pad_rate[system].n;
377 if (pad_acc >= pad_rate[system].d) {
378 pad_acc -= pad_rate[system].d;
379 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
380 hdr[nhdr][0] = iso_empty;
381 wbuf[vec].iov_base = (char *)hdr[nhdr];
382 wbuf[vec++].iov_len = sizeof(hdr[0]);
386 hdr[nhdr][0] = iso_data;
387 wbuf[vec].iov_base = (char *)hdr[nhdr];
388 wbuf[vec++].iov_len = sizeof(hdr[0]);
389 wbuf[vec].iov_base = (char *)dv;
390 wbuf[vec++].iov_len = DSIZE;
394 if (offset * DSIZE < tlen)
398 len = writev(d, wbuf, vec);
400 if (errno == EAGAIN) {
401 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
404 err(1, "write failed");
407 fprintf(stderr, "\n");
409 gettimeofday(&end, NULL);
410 rtime = end.tv_sec - start.tv_sec
411 + (end.tv_usec - start.tv_usec) * 1e-6;
412 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
413 frames, rtime, frames/rtime);