1 /* 2 * Copyright (C) 2003 3 * Hidetoshi Shimokawa. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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: 15 * 16 * This product includes software developed by Hidetoshi Shimokawa. 17 * 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD$ 35 */ 36 #include <sys/param.h> 37 #include <sys/ioctl.h> 38 #include <sys/time.h> 39 #include <sys/types.h> 40 #include <sys/uio.h> 41 42 #if __FreeBSD_version >= 500000 43 #include <arpa/inet.h> 44 #endif 45 46 #include <err.h> 47 #include <errno.h> 48 #include <unistd.h> 49 #include <fcntl.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 #include <dev/firewire/firewire.h> 55 #include <dev/firewire/iec68113.h> 56 57 58 struct frac { 59 int n,d; 60 }; 61 62 struct frac frame_cycle[2] = { 63 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */ 64 {320, 1}, /* PAL 8000 cycle / 25 Hz */ 65 }; 66 int npackets[] = { 67 250 /* NTSC */, 68 300 /* PAL */ 69 }; 70 struct frac pad_rate[2] = { 71 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */ 72 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */ 73 }; 74 75 #define PSIZE 512 76 #define DSIZE 480 77 #define NCHUNK 8 78 79 #define NPACKET_R 256 80 #define NPACKET_T 255 81 #define NEMPTY 20 82 #define BUFSIZE (PSIZE * NPACKET_R) 83 #define MAXBLOCKS (300) 84 #define CYCLE_FRAC 0xc00 85 86 int 87 dvrecv(int d, char *filename, char ich, int count) 88 { 89 struct fw_isochreq isoreq; 90 struct fw_isobufreq bufreq; 91 struct dvdbc *dv; 92 struct ciphdr *ciph; 93 struct fw_pkt *pkt; 94 char *pad, *buf; 95 u_int32_t *ptr; 96 int len, tlen, npad, fd, k, m, vec, pal, nb; 97 int nblocks[] = {250 /* NTSC */, 300 /* PAL */}; 98 struct iovec wbuf[NPACKET_R]; 99 100 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660); 101 buf = (char *)malloc(BUFSIZE); 102 pad = (char *)malloc(DSIZE*MAXBLOCKS); 103 bzero(pad, DSIZE*MAXBLOCKS); 104 bzero(wbuf, sizeof(wbuf)); 105 106 bufreq.rx.nchunk = NCHUNK; 107 bufreq.rx.npacket = NPACKET_R; 108 bufreq.rx.psize = PSIZE; 109 bufreq.tx.nchunk = 0; 110 bufreq.tx.npacket = 0; 111 bufreq.tx.psize = 0; 112 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) { 113 err(1, "ioctl"); 114 } 115 116 isoreq.ch = ich & 0x3f; 117 isoreq.tag = (ich >> 6) & 3; 118 119 if( ioctl(d, FW_SRSTREAM, &isoreq) < 0) 120 err(1, "ioctl"); 121 122 k = m = 0; 123 while (count <= 0 || k <= count) { 124 #if 0 125 tlen = 0; 126 while ((len = read(d, buf + tlen, PSIZE 127 /*BUFSIZE - tlen*/)) > 0) { 128 if (len < 0) { 129 if (errno == EAGAIN) { 130 fprintf(stderr, "(EAGAIN)\n"); 131 fflush(stderr); 132 if (len <= 0) 133 continue; 134 } else 135 err(1, "read failed"); 136 } 137 tlen += len; 138 if ((BUFSIZE - tlen) < PSIZE) 139 break; 140 }; 141 #else 142 tlen = len = read(d, buf, BUFSIZE); 143 if (len < 0) { 144 if (errno == EAGAIN) { 145 fprintf(stderr, "(EAGAIN)\n"); 146 fflush(stderr); 147 if (len <= 0) 148 continue; 149 } else 150 err(1, "read failed"); 151 } 152 #endif 153 vec = 0; 154 ptr = (u_int32_t *) buf; 155 again: 156 pkt = (struct fw_pkt *) ptr; 157 #if 0 158 fprintf(stderr, "%08x %08x %08x %08x\n", 159 htonl(ptr[0]), htonl(ptr[1]), 160 htonl(ptr[2]), htonl(ptr[3])); 161 #endif 162 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */ 163 if (ciph->fmt != CIP_FMT_DVCR) 164 errx(1, "unknown format 0x%x", ciph->fmt); 165 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */ 166 #if 0 167 if (ciph->fdf.dv.cyc != 0xffff && k == 0) { 168 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc)); 169 } 170 #endif 171 if (ntohs(pkt->mode.stream.len) <= sizeof(struct ciphdr)) 172 /* no payload */ 173 goto next; 174 for (dv = (struct dvdbc *)ptr; 175 (char *)dv < (char *)(ptr + ciph->len); 176 dv+=6) { 177 178 #if 0 179 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq); 180 #endif 181 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) { 182 #if 0 183 fprintf(stderr, "%d(%d) ", k, m); 184 #else 185 fprintf(stderr, "%d", k%10); 186 #endif 187 pal = ((dv->payload[0] & DV_DSF_12) != 0); 188 nb = nblocks[pal]; 189 #if 1 190 if (m > 0 && m != nb) { 191 /* padding bad frame */ 192 npad = ((nb - m) % nb); 193 if (npad < 0) 194 npad += nb; 195 fprintf(stderr, "(%d blocks padded)", 196 npad); 197 npad *= DSIZE; 198 wbuf[vec].iov_base = pad; 199 wbuf[vec++].iov_len = npad; 200 if (vec >= NPACKET_R) { 201 writev(fd, wbuf, vec); 202 vec = 0; 203 } 204 } 205 #endif 206 k++; 207 if (k % 30 == 0) { /* every second */ 208 fprintf(stderr, "\n"); 209 } 210 fflush(stderr); 211 m = 0; 212 } 213 if (k == 0 || (count > 0 && k > count)) 214 continue; 215 m++; 216 wbuf[vec].iov_base = (char *) dv; 217 wbuf[vec++].iov_len = DSIZE; 218 if (vec >= NPACKET_R) { 219 writev(fd, wbuf, vec); 220 vec = 0; 221 } 222 } 223 ptr = (u_int32_t *)dv; 224 next: 225 if ((char *)ptr < buf + tlen) 226 goto again; 227 if (vec > 0) 228 writev(fd, wbuf, vec); 229 } 230 close(fd); 231 fprintf(stderr, "\n"); 232 return 0; 233 } 234 235 236 int 237 dvsend(int d, char *filename, char ich, int count) 238 { 239 struct fw_isochreq isoreq; 240 struct fw_isobufreq bufreq; 241 struct dvdbc *dv; 242 struct fw_pkt *pkt; 243 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i; 244 int system=0, pad_acc, cycle_acc, cycle, f_cycle, f_frac; 245 struct iovec wbuf[NPACKET_T*2 + NEMPTY]; 246 char *pbuf; 247 u_int32_t iso_data, iso_empty, hdr[NPACKET_T + NEMPTY][3]; 248 struct ciphdr *ciph; 249 struct timeval start, end; 250 double rtime; 251 252 fd = open(filename, O_RDONLY); 253 pbuf = (char *)malloc(DSIZE * NPACKET_T); 254 bzero(wbuf, sizeof(wbuf)); 255 256 bufreq.rx.nchunk = 0; 257 bufreq.rx.npacket = 0; 258 bufreq.rx.psize = 0; 259 bufreq.tx.nchunk = NCHUNK; 260 bufreq.tx.npacket = NPACKET_T; 261 bufreq.tx.psize = PSIZE; 262 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) { 263 err(1, "ioctl"); 264 } 265 266 isoreq.ch = ich & 0x3f; 267 isoreq.tag = (ich >> 6) & 3; 268 269 if( ioctl(d, FW_STSTREAM, &isoreq) < 0) 270 err(1, "ioctl"); 271 272 pkt = (struct fw_pkt *) &iso_data; 273 pkt->mode.stream.len = htons(DSIZE + sizeof(struct ciphdr)); 274 pkt->mode.stream.sy = 0; 275 pkt->mode.stream.tcode = FWTCODE_STREAM; 276 pkt->mode.stream.chtag = ich; 277 iso_empty = iso_data; 278 pkt = (struct fw_pkt *) &iso_empty; 279 pkt->mode.stream.len = htons(sizeof(struct ciphdr)); 280 281 hdr[0][0] = iso_data; 282 ciph = (struct ciphdr *)&hdr[0][1]; 283 ciph->src = 0; 284 ciph->len = 120; 285 ciph->dbc = 0; 286 ciph->eoh1 = 1; 287 ciph->fdf.dv.cyc = 0xffff; 288 289 for (i = 1; i < NPACKET_T; i++) { 290 bcopy(hdr[0], hdr[i], sizeof(hdr[0])); 291 } 292 293 gettimeofday(&start, NULL); 294 #if 0 295 fprintf(stderr, "%08x %08x %08x\n", 296 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); 297 #endif 298 frames = 0; 299 packets = 0; 300 pad_acc = 0; 301 cycle = 1; 302 cycle_acc = frame_cycle[system].d * cycle; 303 while (1) { 304 tlen = 0; 305 while (tlen < DSIZE * NPACKET_T) { 306 len = read(fd, pbuf + tlen, DSIZE * NPACKET_T - tlen); 307 if (len <= 0) { 308 if (tlen > 0) 309 break; 310 if (len < 0) 311 warn("read"); 312 else 313 printf("\nend of file\n"); 314 goto send_end; 315 } 316 tlen += len; 317 } 318 vec = 0; 319 count = 0; 320 offset = 0; 321 nhdr = 0; 322 next: 323 dv = (struct dvdbc *)(pbuf + offset * DSIZE); 324 #if 0 325 header = (dv->sct == 0 && dv->dseq == 0); 326 #else 327 header = (packets % npackets[system] == 0); 328 #endif 329 330 ciph = (struct ciphdr *)&hdr[nhdr][1]; 331 if (header) { 332 fprintf(stderr, "%d", frames % 10); 333 frames ++; 334 if (count > 0 && frames > count) 335 break; 336 if (frames % 30 == 0) 337 fprintf(stderr, "\n"); 338 fflush(stderr); 339 system = ((dv->payload[0] & DV_DSF_12) != 0); 340 f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf; 341 f_frac = (cycle_acc % frame_cycle[system].d 342 * CYCLE_FRAC) / frame_cycle[system].d; 343 #if 0 344 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac); 345 #else 346 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac); 347 #endif 348 cycle_acc += frame_cycle[system].n; 349 cycle_acc %= frame_cycle[system].d * 0x10; 350 351 } else { 352 ciph->fdf.dv.cyc = 0xffff; 353 } 354 ciph->dbc = packets++ % 256; 355 pad_acc += pad_rate[system].n; 356 if (pad_acc >= pad_rate[system].d) { 357 pad_acc -= pad_rate[system].d; 358 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0])); 359 hdr[nhdr][0] = iso_empty; 360 wbuf[vec].iov_base = (char *)hdr[nhdr]; 361 wbuf[vec++].iov_len = sizeof(hdr[0]); 362 nhdr ++; 363 cycle ++; 364 } 365 hdr[nhdr][0] = iso_data; 366 wbuf[vec].iov_base = (char *)hdr[nhdr]; 367 wbuf[vec++].iov_len = sizeof(hdr[0]); 368 wbuf[vec].iov_base = (char *)dv; 369 wbuf[vec++].iov_len = DSIZE; 370 nhdr ++; 371 cycle ++; 372 offset ++; 373 if (offset * DSIZE < tlen) 374 goto next; 375 376 again: 377 len = writev(d, wbuf, vec); 378 if (len < 0) { 379 if (errno == EAGAIN) { 380 fprintf(stderr, "(EAGAIN)\n"); 381 fflush(stderr); 382 goto again; 383 } 384 err(1, "write failed"); 385 } 386 } 387 close(fd); 388 fprintf(stderr, "\n"); 389 send_end: 390 gettimeofday(&end, NULL); 391 rtime = end.tv_sec - start.tv_sec 392 + (end.tv_usec - start.tv_usec) * 1e-6; 393 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n", 394 frames, rtime, frames/rtime); 395 return 0; 396 } 397 398 399 #if 0 400 int 401 main(int argc, char *argv[]) 402 { 403 extern char *optarg; 404 extern int optind; 405 int ch; 406 int fd; 407 char devname[] = "/dev/fw0"; 408 409 while ((ch = getopt(argc, argv, "I:")) != -1){ 410 switch(ch) { 411 case 'I': 412 strcpy(devname, optarg); 413 break; 414 default: 415 usage(); 416 break; 417 } 418 } 419 argc -= optind; 420 argv += optind; 421 422 if (argc < 2) 423 usage(); 424 425 fd = open(devname, O_RDWR); 426 if (fd < 0) 427 err(1, "open"); 428 dvrec(fd, argv[0], (TAG<<6) | CHANNEL, atoi(argv[1])); 429 close(fd); 430 exit(0); 431 } 432 433 #endif 434