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>
44 #if __FreeBSD_version >= 500000
45 #include <arpa/inet.h>
57 #include <dev/firewire/firewire.h>
58 #include <dev/firewire/iec68113.h>
60 #include "fwmethods.h"
69 struct frac frame_cycle[2] = {
70 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */
71 {320, 1}, /* PAL 8000 cycle / 25 Hz */
77 struct frac pad_rate[2] = {
78 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */
79 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */
81 char *system_name[] = {"NTSC", "PAL"};
82 int frame_rate[] = {30, 25};
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
97 dvrecv(int d, const char *filename, char ich, int count)
99 struct fw_isochreq isoreq;
100 struct fw_isobufreq bufreq;
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];
110 if(strcmp(filename, "-") == 0) {
113 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
115 err(EX_NOINPUT, "%s", filename);
117 buf = malloc(RBUFSIZE);
118 pad = malloc(DSIZE*MAXBLOCKS);
119 memset(pad, 0xff, DSIZE*MAXBLOCKS);
120 bzero(wbuf, sizeof(wbuf));
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;
128 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
129 err(1, "ioctl FW_SSTBUF");
131 isoreq.ch = ich & 0x3f;
132 isoreq.tag = (ich >> 6) & 3;
134 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
138 while (count <= 0 || k <= count) {
141 while ((len = read(d, buf + tlen, PSIZE
142 /* RBUFSIZE - tlen */)) > 0) {
144 if (errno == EAGAIN) {
145 fprintf(stderr, "(EAGAIN)\n");
150 err(1, "read failed");
153 if ((RBUFSIZE - tlen) < PSIZE)
157 tlen = len = read(d, buf, RBUFSIZE);
159 if (errno == EAGAIN) {
160 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
165 err(1, "read failed");
169 ptr = (u_int32_t *) buf;
171 pkt = (struct fw_pkt *) ptr;
173 fprintf(stderr, "%08x %08x %08x %08x\n",
174 htonl(ptr[0]), htonl(ptr[1]),
175 htonl(ptr[2]), htonl(ptr[3]));
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 */
182 if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
183 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
186 if (pkt->mode.stream.len <= sizeof(struct ciphdr))
189 for (dv = (struct dvdbc *)ptr;
190 (char *)dv < (char *)(ptr + ciph->len);
194 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
196 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
198 system = ciph->fdf.dv.fs;
199 fprintf(stderr, "%s\n", system_name[system]);
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]);
214 if (m > 0 && m != nb) {
215 /* padding bad frame */
216 npad = ((nb - m) % nb);
219 fprintf(stderr, "\n%d blocks padded\n",
222 wbuf[vec].iov_base = pad;
223 wbuf[vec++].iov_len = npad;
224 if (vec >= NPACKET_R) {
225 writev(fd, wbuf, vec);
234 if (k == 0 || (count > 0 && k > count))
237 wbuf[vec].iov_base = (char *) dv;
238 wbuf[vec++].iov_len = DSIZE;
239 if (vec >= NPACKET_R) {
240 writev(fd, wbuf, vec);
244 ptr = (u_int32_t *)dv;
246 if ((char *)ptr < buf + tlen)
249 writev(fd, wbuf, vec);
251 if (fd != STDOUT_FILENO)
253 fprintf(stderr, "\n");
258 dvsend(int d, const char *filename, char ich, int count)
260 struct fw_isochreq isoreq;
261 struct fw_isobufreq bufreq;
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];
268 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
270 struct timeval start, end;
273 fd = open(filename, O_RDONLY);
275 err(EX_NOINPUT, "%s", filename);
277 pbuf = malloc(DSIZE * TNBUF);
278 bzero(wbuf, sizeof(wbuf));
280 bufreq.rx.nchunk = 0;
281 bufreq.rx.npacket = 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");
289 isoreq.ch = ich & 0x3f;
290 isoreq.tag = (ich >> 6) & 3;
292 if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
293 err(1, "ioctl FW_STSTREAM");
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);
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 */
312 ciph->fdf.dv.cyc = 0xffff;
314 for (i = 1; i < TNBUF; i++)
315 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
317 gettimeofday(&start, NULL);
319 fprintf(stderr, "%08x %08x %08x\n",
320 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
327 while (tlen < DSIZE * TNBUF) {
328 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
335 fprintf(stderr, "\nend of file\n");
344 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
346 header = (dv->sct == 0 && dv->dseq == 0);
348 header = (packets == 0 || packets % npackets[system] == 0);
351 ciph = (struct ciphdr *)&hdr[nhdr][1];
354 system = ((dv->payload[0] & DV_DSF_12) != 0);
355 printf("%s\n", system_name[system]);
357 cycle_acc = frame_cycle[system].d * cycle;
359 fprintf(stderr, "%d", frames % 10);
361 if (count > 0 && frames > count)
363 if (frames % frame_rate[system] == 0)
364 fprintf(stderr, "\n");
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;
370 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
372 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
374 cycle_acc += frame_cycle[system].n;
375 cycle_acc %= frame_cycle[system].d * 0x10;
378 ciph->fdf.dv.cyc = 0xffff;
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]);
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;
399 if (offset * DSIZE < tlen)
403 len = writev(d, wbuf, vec);
405 if (errno == EAGAIN) {
406 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
409 err(1, "write failed");
412 fprintf(stderr, "\n");
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);