3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
16 * This product includes software developed by Hidetoshi Shimokawa.
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * 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>
42 #if __FreeBSD_version >= 500000
43 #include <arpa/inet.h>
55 #include <dev/firewire/firewire.h>
56 #include <dev/firewire/iec68113.h>
58 #include "fwmethods.h"
67 struct frac frame_cycle[2] = {
68 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */
69 {320, 1}, /* PAL 8000 cycle / 25 Hz */
75 struct frac pad_rate[2] = {
76 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */
77 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */
79 char *system_name[] = {"NTSC", "PAL"};
80 int frame_rate[] = {30, 25};
88 #define TNBUF 100 /* XXX too large value causes block noise */
89 #define NEMPTY 10 /* depends on TNBUF */
90 #define RBUFSIZE (PSIZE * NPACKET_R)
91 #define MAXBLOCKS (300)
92 #define CYCLE_FRAC 0xc00
95 dvrecv(int d, const char *filename, char ich, int count)
97 struct fw_isochreq isoreq;
98 struct fw_isobufreq bufreq;
104 int len, tlen, npad, fd, k, m, vec, system = -1, nb;
105 int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
106 struct iovec wbuf[NPACKET_R];
108 if(strcmp(filename, "-") == 0) {
111 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
113 err(EX_NOINPUT, "%s", filename);
115 buf = malloc(RBUFSIZE);
116 pad = malloc(DSIZE*MAXBLOCKS);
117 memset(pad, 0xff, DSIZE*MAXBLOCKS);
118 bzero(wbuf, sizeof(wbuf));
120 bufreq.rx.nchunk = NCHUNK;
121 bufreq.rx.npacket = NPACKET_R;
122 bufreq.rx.psize = PSIZE;
123 bufreq.tx.nchunk = 0;
124 bufreq.tx.npacket = 0;
126 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
127 err(1, "ioctl FW_SSTBUF");
129 isoreq.ch = ich & 0x3f;
130 isoreq.tag = (ich >> 6) & 3;
132 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
136 while (count <= 0 || k <= count) {
139 while ((len = read(d, buf + tlen, PSIZE
140 /* RBUFSIZE - tlen */)) > 0) {
142 if (errno == EAGAIN) {
143 fprintf(stderr, "(EAGAIN)\n");
148 err(1, "read failed");
151 if ((RBUFSIZE - tlen) < PSIZE)
155 tlen = len = read(d, buf, RBUFSIZE);
157 if (errno == EAGAIN) {
158 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
163 err(1, "read failed");
167 ptr = (u_int32_t *) buf;
169 pkt = (struct fw_pkt *) ptr;
171 fprintf(stderr, "%08x %08x %08x %08x\n",
172 htonl(ptr[0]), htonl(ptr[1]),
173 htonl(ptr[2]), htonl(ptr[3]));
175 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */
176 if (ciph->fmt != CIP_FMT_DVCR)
177 errx(1, "unknown format 0x%x", ciph->fmt);
178 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */
180 if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
181 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
184 if (pkt->mode.stream.len <= sizeof(struct ciphdr))
187 for (dv = (struct dvdbc *)ptr;
188 (char *)dv < (char *)(ptr + ciph->len);
192 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
194 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
196 system = ciph->fdf.dv.fs;
197 fprintf(stderr, "%s\n", system_name[system]);
202 (dv->payload[0] & DV_DSF_12) == 0)
203 dv->payload[0] |= DV_DSF_12;
204 nb = nblocks[system];
205 fprintf(stderr, "%d:%02d:%02d %d\r",
206 k / (3600 * frame_rate[system]),
207 (k / (60 * frame_rate[system])) % 60,
208 (k / frame_rate[system]) % 60,
209 k % frame_rate[system]);
212 if (m > 0 && m != nb) {
213 /* padding bad frame */
214 npad = ((nb - m) % nb);
217 fprintf(stderr, "\n%d blocks padded\n",
220 wbuf[vec].iov_base = pad;
221 wbuf[vec++].iov_len = npad;
222 if (vec >= NPACKET_R) {
223 writev(fd, wbuf, vec);
232 if (k == 0 || (count > 0 && k > count))
235 wbuf[vec].iov_base = (char *) dv;
236 wbuf[vec++].iov_len = DSIZE;
237 if (vec >= NPACKET_R) {
238 writev(fd, wbuf, vec);
242 ptr = (u_int32_t *)dv;
244 if ((char *)ptr < buf + tlen)
247 writev(fd, wbuf, vec);
249 if (fd != STDOUT_FILENO)
251 fprintf(stderr, "\n");
256 dvsend(int d, const char *filename, char ich, int count)
258 struct fw_isochreq isoreq;
259 struct fw_isobufreq bufreq;
262 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
263 int system=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac;
264 struct iovec wbuf[TNBUF*2 + NEMPTY];
266 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
268 struct timeval start, end;
271 fd = open(filename, O_RDONLY);
273 err(EX_NOINPUT, "%s", filename);
275 pbuf = malloc(DSIZE * TNBUF);
276 bzero(wbuf, sizeof(wbuf));
278 bufreq.rx.nchunk = 0;
279 bufreq.rx.npacket = 0;
281 bufreq.tx.nchunk = NCHUNK;
282 bufreq.tx.npacket = NPACKET_T;
283 bufreq.tx.psize = PSIZE;
284 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
285 err(1, "ioctl FW_SSTBUF");
287 isoreq.ch = ich & 0x3f;
288 isoreq.tag = (ich >> 6) & 3;
290 if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
291 err(1, "ioctl FW_STSTREAM");
294 pkt = (struct fw_pkt *) &iso_data;
295 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
296 pkt->mode.stream.sy = 0;
297 pkt->mode.stream.tcode = FWTCODE_STREAM;
298 pkt->mode.stream.chtag = ich;
299 iso_empty = iso_data;
300 pkt = (struct fw_pkt *) &iso_empty;
301 pkt->mode.stream.len = sizeof(struct ciphdr);
303 bzero(hdr[0], sizeof(hdr[0]));
304 hdr[0][0] = iso_data;
305 ciph = (struct ciphdr *)&hdr[0][1];
306 ciph->src = 0; /* XXX */
310 ciph->fdf.dv.cyc = 0xffff;
312 for (i = 1; i < TNBUF; i++)
313 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
315 gettimeofday(&start, NULL);
317 fprintf(stderr, "%08x %08x %08x\n",
318 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
325 while (tlen < DSIZE * TNBUF) {
326 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
333 fprintf(stderr, "\nend of file\n");
342 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
344 header = (dv->sct == 0 && dv->dseq == 0);
346 header = (packets == 0 || packets % npackets[system] == 0);
349 ciph = (struct ciphdr *)&hdr[nhdr][1];
352 system = ((dv->payload[0] & DV_DSF_12) != 0);
353 printf("%s\n", system_name[system]);
355 cycle_acc = frame_cycle[system].d * cycle;
357 fprintf(stderr, "%d", frames % 10);
359 if (count > 0 && frames > count)
361 if (frames % frame_rate[system] == 0)
362 fprintf(stderr, "\n");
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;
368 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
370 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
372 cycle_acc += frame_cycle[system].n;
373 cycle_acc %= frame_cycle[system].d * 0x10;
376 ciph->fdf.dv.cyc = 0xffff;
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]);
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;
397 if (offset * DSIZE < tlen)
401 len = writev(d, wbuf, vec);
403 if (errno == EAGAIN) {
404 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
407 err(1, "write failed");
411 fprintf(stderr, "\n");
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);