1 /* $NetBSD: fwdv.c,v 1.3 2008/05/02 19:59:19 xtraeme Exp $ */ 2 /* 3 * Copyright (C) 2003 4 * Hidetoshi Shimokawa. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * 17 * This product includes software developed by Hidetoshi Shimokawa. 18 * 19 * 4. Neither the name of the author nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD: src/usr.sbin/fwcontrol/fwdv.c,v 1.7 2007/06/17 10:20:55 simokawa Exp $ 36 */ 37 #include <sys/param.h> 38 #include <sys/ioctl.h> 39 #include <sys/time.h> 40 #include <sys/types.h> 41 #include <sys/uio.h> 42 43 #if __FreeBSD_version >= 500000 44 #include <arpa/inet.h> 45 #endif 46 47 #include <err.h> 48 #include <errno.h> 49 #include <unistd.h> 50 #include <fcntl.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <sysexits.h> 55 56 #if defined(__FreeBSD__) 57 #include <dev/firewire/firewire.h> 58 #include <dev/firewire/iec68113.h> 59 #elif defined(__NetBSD__) 60 #include <dev/ieee1394/firewire.h> 61 #include <dev/ieee1394/iec68113.h> 62 #endif 63 64 #include "fwmethods.h" 65 66 #define DEBUG 0 67 #define FIX_FRAME 1 68 69 struct frac { 70 int n,d; 71 }; 72 73 struct frac frame_cycle[2] = { 74 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */ 75 {320, 1}, /* PAL 8000 cycle / 25 Hz */ 76 }; 77 int npackets[] = { 78 250 /* NTSC */, 79 300 /* PAL */ 80 }; 81 struct frac pad_rate[2] = { 82 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */ 83 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */ 84 }; 85 const char *system_name[] = {"NTSC", "PAL"}; 86 int frame_rate[] = {30, 25}; 87 88 #define PSIZE 512 89 #define DSIZE 480 90 #define NCHUNK 64 91 92 #define NPACKET_R 256 93 #define NPACKET_T 255 94 #define TNBUF 100 /* XXX too large value causes block noise */ 95 #define NEMPTY 10 /* depends on TNBUF */ 96 #define RBUFSIZE (PSIZE * NPACKET_R) 97 #define MAXBLOCKS (300) 98 #define CYCLE_FRAC 0xc00 99 100 void 101 dvrecv(int d, const char *filename, char ich, int count) 102 { 103 struct fw_isochreq isoreq; 104 struct fw_isobufreq bufreq; 105 struct dvdbc *dv; 106 struct ciphdr *ciph; 107 struct fw_pkt *pkt; 108 char *pad, *buf; 109 u_int32_t *ptr; 110 int len, tlen, npad, fd, k, m, vec, lsystem = -1, nb; 111 int nblocks[] = {250 /* NTSC */, 300 /* PAL */}; 112 struct iovec wbuf[NPACKET_R]; 113 114 if(strcmp(filename, "-") == 0) { 115 fd = STDOUT_FILENO; 116 } else { 117 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660); 118 if (fd == -1) 119 err(EX_NOINPUT, filename); 120 } 121 buf = malloc(RBUFSIZE); 122 pad = malloc(DSIZE*MAXBLOCKS); 123 memset(pad, 0xff, DSIZE*MAXBLOCKS); 124 bzero(wbuf, sizeof(wbuf)); 125 126 bufreq.rx.nchunk = NCHUNK; 127 bufreq.rx.npacket = NPACKET_R; 128 bufreq.rx.psize = PSIZE; 129 bufreq.tx.nchunk = 0; 130 bufreq.tx.npacket = 0; 131 bufreq.tx.psize = 0; 132 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) 133 err(1, "ioctl FW_SSTBUF"); 134 135 isoreq.ch = ich & 0x3f; 136 isoreq.tag = (ich >> 6) & 3; 137 138 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0) 139 err(1, "ioctl"); 140 141 k = m = 0; 142 while (count <= 0 || k <= count) { 143 #if 0 144 tlen = 0; 145 while ((len = read(d, buf + tlen, PSIZE 146 /* RBUFSIZE - tlen */)) > 0) { 147 if (len < 0) { 148 if (errno == EAGAIN) { 149 fprintf(stderr, 150 "(EAGAIN)- push 'Play'?\n"); 151 fflush(stderr); 152 if (len <= 0) 153 continue; 154 } else 155 err(1, "read failed"); 156 } 157 tlen += len; 158 if ((RBUFSIZE - tlen) < PSIZE) 159 break; 160 }; 161 #else 162 tlen = len = read(d, buf, RBUFSIZE); 163 if (len < 0) { 164 if (errno == EAGAIN) { 165 fprintf(stderr, "(EAGAIN)\n"); 166 fflush(stderr); 167 if (len <= 0) 168 continue; 169 } else 170 err(1, "read failed"); 171 } 172 #endif 173 vec = 0; 174 ptr = (u_int32_t *) buf; 175 again: 176 pkt = (struct fw_pkt *) ptr; 177 #if DEBUG 178 fprintf(stderr, "%08x %08x %08x %08x\n", 179 htonl(ptr[0]), htonl(ptr[1]), 180 htonl(ptr[2]), htonl(ptr[3])); 181 #endif 182 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */ 183 if (ciph->fmt != CIP_FMT_DVCR) 184 errx(1, "unknown format 0x%x", ciph->fmt); 185 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */ 186 #if DEBUG 187 if (ciph->fdf.dv.cyc != 0xffff && k == 0) { 188 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc)); 189 } 190 #endif 191 if (pkt->mode.stream.len <= sizeof(struct ciphdr)) 192 /* no payload */ 193 goto next; 194 for (dv = (struct dvdbc *)ptr; 195 (char *)dv < (char *)(ptr + ciph->len); 196 dv+=6) { 197 198 #if DEBUG 199 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq); 200 #endif 201 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) { 202 if (lsystem < 0) { 203 lsystem = ciph->fdf.dv.fs; 204 fprintf(stderr, 205 "%s\n", system_name[lsystem]); 206 } 207 208 /* Fix DSF bit */ 209 if (lsystem == 1 && 210 (dv->payload[0] & DV_DSF_12) == 0) 211 dv->payload[0] |= DV_DSF_12; 212 nb = nblocks[lsystem]; 213 fprintf(stderr, "%d", k%10); 214 #if FIX_FRAME 215 if (m > 0 && m != nb) { 216 /* padding bad frame */ 217 npad = ((nb - m) % nb); 218 if (npad < 0) 219 npad += nb; 220 fprintf(stderr, "(%d blocks padded)", 221 npad); 222 npad *= DSIZE; 223 wbuf[vec].iov_base = pad; 224 wbuf[vec++].iov_len = npad; 225 if (vec >= NPACKET_R) { 226 writev(fd, wbuf, vec); 227 vec = 0; 228 } 229 } 230 #endif 231 k++; 232 if (k % frame_rate[lsystem] == 0) { 233 /* every second */ 234 fprintf(stderr, "\n"); 235 } 236 fflush(stderr); 237 m = 0; 238 } 239 if (k == 0 || (count > 0 && k > count)) 240 continue; 241 m++; 242 wbuf[vec].iov_base = (char *) dv; 243 wbuf[vec++].iov_len = DSIZE; 244 if (vec >= NPACKET_R) { 245 writev(fd, wbuf, vec); 246 vec = 0; 247 } 248 } 249 ptr = (u_int32_t *)dv; 250 next: 251 if ((char *)ptr < buf + tlen) 252 goto again; 253 if (vec > 0) 254 writev(fd, wbuf, vec); 255 } 256 if(fd != STDOUT_FILENO) { 257 close(fd); 258 } 259 fprintf(stderr, "\n"); 260 } 261 262 263 void 264 dvsend(int d, const char *filename, char ich, int count) 265 { 266 struct fw_isochreq isoreq; 267 struct fw_isobufreq bufreq; 268 struct dvdbc *dv; 269 struct fw_pkt *pkt; 270 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i; 271 int lsystem=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac; 272 struct iovec wbuf[TNBUF*2 + NEMPTY]; 273 char *pbuf; 274 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3]; 275 struct ciphdr *ciph; 276 struct timeval start, end; 277 double rtime; 278 279 cycle_acc = cycle = 0; 280 281 fd = open(filename, O_RDONLY); 282 if (fd == -1) 283 err(EX_NOINPUT, filename); 284 285 pbuf = malloc(DSIZE * TNBUF); 286 bzero(wbuf, sizeof(wbuf)); 287 288 bufreq.rx.nchunk = 0; 289 bufreq.rx.npacket = 0; 290 bufreq.rx.psize = 0; 291 bufreq.tx.nchunk = NCHUNK; 292 bufreq.tx.npacket = NPACKET_T; 293 bufreq.tx.psize = PSIZE; 294 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) 295 err(1, "ioctl FW_SSTBUF"); 296 297 isoreq.ch = ich & 0x3f; 298 isoreq.tag = (ich >> 6) & 3; 299 300 if (ioctl(d, FW_STSTREAM, &isoreq) < 0) 301 err(1, "ioctl FW_STSTREAM"); 302 303 iso_data = 0; 304 pkt = (struct fw_pkt *) &iso_data; 305 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr); 306 pkt->mode.stream.sy = 0; 307 pkt->mode.stream.tcode = FWTCODE_STREAM; 308 pkt->mode.stream.chtag = ich; 309 iso_empty = iso_data; 310 pkt = (struct fw_pkt *) &iso_empty; 311 pkt->mode.stream.len = sizeof(struct ciphdr); 312 313 bzero(hdr[0], sizeof(hdr[0])); 314 hdr[0][0] = iso_data; 315 ciph = (struct ciphdr *)&hdr[0][1]; 316 ciph->src = 0; /* XXX */ 317 ciph->len = 120; 318 ciph->dbc = 0; 319 ciph->eoh1 = 1; 320 ciph->fdf.dv.cyc = 0xffff; 321 322 for (i = 1; i < TNBUF; i++) 323 bcopy(hdr[0], hdr[i], sizeof(hdr[0])); 324 325 gettimeofday(&start, NULL); 326 #if DEBUG 327 fprintf(stderr, "%08x %08x %08x\n", 328 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); 329 #endif 330 frames = 0; 331 packets = 0; 332 pad_acc = 0; 333 while (1) { 334 tlen = 0; 335 while (tlen < DSIZE * TNBUF) { 336 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen); 337 if (len <= 0) { 338 if (tlen > 0) 339 break; 340 if (len < 0) 341 warn("read"); 342 else 343 fprintf(stderr, "\nend of file\n"); 344 goto send_end; 345 } 346 tlen += len; 347 } 348 vec = 0; 349 offset = 0; 350 nhdr = 0; 351 next: 352 dv = (struct dvdbc *)(pbuf + offset * DSIZE); 353 #if 0 354 header = (dv->sct == 0 && dv->dseq == 0); 355 #else 356 header = (packets == 0 || packets % npackets[lsystem] == 0); 357 #endif 358 359 ciph = (struct ciphdr *)&hdr[nhdr][1]; 360 if (header) { 361 if (lsystem < 0) { 362 lsystem = ((dv->payload[0] & DV_DSF_12) != 0); 363 printf("%s\n", system_name[lsystem]); 364 cycle = 1; 365 cycle_acc = frame_cycle[lsystem].d * cycle; 366 } 367 fprintf(stderr, "%d", frames % 10); 368 frames ++; 369 if (count > 0 && frames > count) 370 break; 371 if (frames % frame_rate[lsystem] == 0) 372 fprintf(stderr, "\n"); 373 fflush(stderr); 374 f_cycle = (cycle_acc / frame_cycle[lsystem].d) & 0xf; 375 f_frac = (cycle_acc % frame_cycle[lsystem].d 376 * CYCLE_FRAC) / frame_cycle[lsystem].d; 377 #if 0 378 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac); 379 #else 380 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac); 381 #endif 382 cycle_acc += frame_cycle[lsystem].n; 383 cycle_acc %= frame_cycle[lsystem].d * 0x10; 384 385 } else { 386 ciph->fdf.dv.cyc = 0xffff; 387 } 388 ciph->dbc = packets++ % 256; 389 pad_acc += pad_rate[lsystem].n; 390 if (pad_acc >= pad_rate[lsystem].d) { 391 pad_acc -= pad_rate[lsystem].d; 392 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0])); 393 hdr[nhdr][0] = iso_empty; 394 wbuf[vec].iov_base = (char *)hdr[nhdr]; 395 wbuf[vec++].iov_len = sizeof(hdr[0]); 396 nhdr ++; 397 cycle ++; 398 } 399 hdr[nhdr][0] = iso_data; 400 wbuf[vec].iov_base = (char *)hdr[nhdr]; 401 wbuf[vec++].iov_len = sizeof(hdr[0]); 402 wbuf[vec].iov_base = (char *)dv; 403 wbuf[vec++].iov_len = DSIZE; 404 nhdr ++; 405 cycle ++; 406 offset ++; 407 if (offset * DSIZE < tlen) 408 goto next; 409 410 again: 411 len = writev(d, wbuf, vec); 412 if (len < 0) { 413 if (errno == EAGAIN) { 414 fprintf(stderr, "(EAGAIN) - push 'Play'?\n"); 415 goto again; 416 } 417 err(1, "write failed"); 418 } 419 } 420 close(fd); 421 fprintf(stderr, "\n"); 422 send_end: 423 gettimeofday(&end, NULL); 424 rtime = end.tv_sec - start.tv_sec 425 + (end.tv_usec - start.tv_usec) * 1e-6; 426 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n", 427 frames, rtime, frames/rtime); 428 } 429