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
34 * $FreeBSD: src/usr.sbin/fwcontrol/fwdv.c,v 1.2.2.4 2003/04/28 03:29:18 simokawa Exp $
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
40 #include <sys/types.h>
43 #if __FreeBSD_version >= 500000
44 #include <arpa/inet.h>
55 #include <bus/firewire/firewire.h>
56 #include <bus/firewire/iec68113.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 const 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
92 int dvrecv(int, char *, char, int);
93 int dvsend(int, char *, char, int);
96 dvrecv(int d, char *filename, char ich, int count)
98 struct fw_isochreq isoreq;
99 struct fw_isobufreq bufreq;
105 int len, tlen, npad, fd, k, m, vec, systype = -1, nb;
106 int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
107 struct iovec wbuf[NPACKET_R];
109 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
110 buf = (char *)malloc(RBUFSIZE);
111 pad = (char *)malloc(DSIZE*MAXBLOCKS);
112 memset(pad, 0xff, DSIZE*MAXBLOCKS);
113 bzero(wbuf, sizeof(wbuf));
115 bufreq.rx.nchunk = NCHUNK;
116 bufreq.rx.npacket = NPACKET_R;
117 bufreq.rx.psize = PSIZE;
118 bufreq.tx.nchunk = 0;
119 bufreq.tx.npacket = 0;
121 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) {
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)\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 systype = ciph->fdf.dv.fs;
193 printf("%s\n", system_name[systype]);
198 (dv->payload[0] & DV_DSF_12) == 0)
199 dv->payload[0] |= DV_DSF_12;
200 nb = nblocks[systype];
201 fprintf(stderr, "%d", k%10);
203 if (m > 0 && m != nb) {
204 /* padding bad frame */
205 npad = ((nb - m) % nb);
208 fprintf(stderr, "(%d blocks padded)",
211 wbuf[vec].iov_base = pad;
212 wbuf[vec++].iov_len = npad;
213 if (vec >= NPACKET_R) {
214 writev(fd, wbuf, vec);
220 if (k % frame_rate[systype] == 0) {
222 fprintf(stderr, "\n");
227 if (k == 0 || (count > 0 && k > count))
230 wbuf[vec].iov_base = (char *) dv;
231 wbuf[vec++].iov_len = DSIZE;
232 if (vec >= NPACKET_R) {
233 writev(fd, wbuf, vec);
237 ptr = (u_int32_t *)dv;
239 if ((char *)ptr < buf + tlen)
242 writev(fd, wbuf, vec);
245 fprintf(stderr, "\n");
251 dvsend(int d, char *filename, char ich, int count)
253 struct fw_isochreq isoreq;
254 struct fw_isobufreq bufreq;
257 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
258 int systype=-1, pad_acc, cycle_acc=0, cycle=0, f_cycle, f_frac;
259 struct iovec wbuf[TNBUF*2 + NEMPTY];
261 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
263 struct timeval start, end;
266 fd = open(filename, O_RDONLY);
267 pbuf = (char *)malloc(DSIZE * TNBUF);
268 bzero(wbuf, sizeof(wbuf));
270 bufreq.rx.nchunk = 0;
271 bufreq.rx.npacket = 0;
273 bufreq.tx.nchunk = NCHUNK;
274 bufreq.tx.npacket = NPACKET_T;
275 bufreq.tx.psize = PSIZE;
276 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) {
280 isoreq.ch = ich & 0x3f;
281 isoreq.tag = (ich >> 6) & 3;
283 if( ioctl(d, FW_STSTREAM, &isoreq) < 0)
287 pkt = (struct fw_pkt *) &iso_data;
288 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
289 pkt->mode.stream.sy = 0;
290 pkt->mode.stream.tcode = FWTCODE_STREAM;
291 pkt->mode.stream.chtag = ich;
292 iso_empty = iso_data;
293 pkt = (struct fw_pkt *) &iso_empty;
294 pkt->mode.stream.len = sizeof(struct ciphdr);
296 bzero(hdr[0], sizeof(hdr[0]));
297 hdr[0][0] = iso_data;
298 ciph = (struct ciphdr *)&hdr[0][1];
299 ciph->src = 0; /* XXX */
303 ciph->fdf.dv.cyc = 0xffff;
305 for (i = 1; i < TNBUF; i++) {
306 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
309 gettimeofday(&start, NULL);
311 fprintf(stderr, "%08x %08x %08x\n",
312 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
319 while (tlen < DSIZE * TNBUF) {
320 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
327 printf("\nend of file\n");
336 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
338 header = (dv->sct == 0 && dv->dseq == 0);
340 header = (packets == 0 || packets % npackets[systype] == 0);
343 ciph = (struct ciphdr *)&hdr[nhdr][1];
346 systype = ((dv->payload[0] & DV_DSF_12) != 0);
347 printf("%s\n", system_name[systype]);
349 cycle_acc = frame_cycle[systype].d * cycle;
351 fprintf(stderr, "%d", frames % 10);
353 if (count > 0 && frames > count)
355 if (frames % frame_rate[systype] == 0)
356 fprintf(stderr, "\n");
358 f_cycle = (cycle_acc / frame_cycle[systype].d) & 0xf;
359 f_frac = (cycle_acc % frame_cycle[systype].d
360 * CYCLE_FRAC) / frame_cycle[systype].d;
362 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
364 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
366 cycle_acc += frame_cycle[systype].n;
367 cycle_acc %= frame_cycle[systype].d * 0x10;
370 ciph->fdf.dv.cyc = 0xffff;
372 ciph->dbc = packets++ % 256;
373 pad_acc += pad_rate[systype].n;
374 if (pad_acc >= pad_rate[systype].d) {
375 pad_acc -= pad_rate[systype].d;
376 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
377 hdr[nhdr][0] = iso_empty;
378 wbuf[vec].iov_base = (char *)hdr[nhdr];
379 wbuf[vec++].iov_len = sizeof(hdr[0]);
383 hdr[nhdr][0] = iso_data;
384 wbuf[vec].iov_base = (char *)hdr[nhdr];
385 wbuf[vec++].iov_len = sizeof(hdr[0]);
386 wbuf[vec].iov_base = (char *)dv;
387 wbuf[vec++].iov_len = DSIZE;
391 if (offset * DSIZE < tlen)
395 len = writev(d, wbuf, vec);
397 if (errno == EAGAIN) {
398 fprintf(stderr, "(EAGAIN)\n");
402 err(1, "write failed");
406 fprintf(stderr, "\n");
408 gettimeofday(&end, NULL);
409 rtime = end.tv_sec - start.tv_sec
410 + (end.tv_usec - start.tv_usec) * 1e-6;
411 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
412 frames, rtime, frames/rtime);