1 /* $NetBSD: fwdv.c,v 1.1 2005/07/11 15:35:25 kiyohara 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.5 2003/04/17 03:38:03 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 55 #if defined(__FreeBSD__) 56 #include <dev/firewire/firewire.h> 57 #include <dev/firewire/iec68113.h> 58 #elif defined(__NetBSD__) 59 #include <dev/ieee1394/firewire.h> 60 #include <dev/ieee1394/iec68113.h> 61 #endif 62 63 int dvrecv(int, char *, char, int); 64 int dvsend(int, char *, char, int); 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 char *system_name[] = {"NTSC", "PAL"}; 86 int frame_rate[] = {30, 25}; 87 88 #define PSIZE 512 89 #define DSIZE 480 90 #define NCHUNK 8 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 int 101 dvrecv(int d, 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, system = -1, nb; 111 int nblocks[] = {250 /* NTSC */, 300 /* PAL */}; 112 struct iovec wbuf[NPACKET_R]; 113 114 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660); 115 buf = (char *)malloc(RBUFSIZE); 116 pad = (char *)malloc(DSIZE*MAXBLOCKS); 117 memset(pad, 0xff, DSIZE*MAXBLOCKS); 118 bzero(wbuf, sizeof(wbuf)); 119 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; 125 bufreq.tx.psize = 0; 126 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) { 127 err(1, "ioctl"); 128 } 129 130 isoreq.ch = ich & 0x3f; 131 isoreq.tag = (ich >> 6) & 3; 132 133 if( ioctl(d, FW_SRSTREAM, &isoreq) < 0) 134 err(1, "ioctl"); 135 136 k = m = 0; 137 while (count <= 0 || k <= count) { 138 #if 0 139 tlen = 0; 140 while ((len = read(d, buf + tlen, PSIZE 141 /* RBUFSIZE - tlen */)) > 0) { 142 if (len < 0) { 143 if (errno == EAGAIN) { 144 fprintf(stderr, "(EAGAIN)\n"); 145 fflush(stderr); 146 if (len <= 0) 147 continue; 148 } else 149 err(1, "read failed"); 150 } 151 tlen += len; 152 if ((RBUFSIZE - tlen) < PSIZE) 153 break; 154 }; 155 #else 156 tlen = len = read(d, buf, RBUFSIZE); 157 if (len < 0) { 158 if (errno == EAGAIN) { 159 fprintf(stderr, "(EAGAIN)\n"); 160 fflush(stderr); 161 if (len <= 0) 162 continue; 163 } else 164 err(1, "read failed"); 165 } 166 #endif 167 vec = 0; 168 ptr = (u_int32_t *) buf; 169 again: 170 pkt = (struct fw_pkt *) ptr; 171 #if DEBUG 172 fprintf(stderr, "%08x %08x %08x %08x\n", 173 htonl(ptr[0]), htonl(ptr[1]), 174 htonl(ptr[2]), htonl(ptr[3])); 175 #endif 176 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */ 177 if (ciph->fmt != CIP_FMT_DVCR) 178 errx(1, "unknown format 0x%x", ciph->fmt); 179 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */ 180 #if DEBUG 181 if (ciph->fdf.dv.cyc != 0xffff && k == 0) { 182 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc)); 183 } 184 #endif 185 if (pkt->mode.stream.len <= sizeof(struct ciphdr)) 186 /* no payload */ 187 goto next; 188 for (dv = (struct dvdbc *)ptr; 189 (char *)dv < (char *)(ptr + ciph->len); 190 dv+=6) { 191 192 #if DEBUG 193 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq); 194 #endif 195 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) { 196 if (system < 0) { 197 system = ciph->fdf.dv.fs; 198 printf("%s\n", system_name[system]); 199 } 200 201 /* Fix DSF bit */ 202 if (system == 1 && 203 (dv->payload[0] & DV_DSF_12) == 0) 204 dv->payload[0] |= DV_DSF_12; 205 nb = nblocks[system]; 206 fprintf(stderr, "%d", k%10); 207 #if FIX_FRAME 208 if (m > 0 && m != nb) { 209 /* padding bad frame */ 210 npad = ((nb - m) % nb); 211 if (npad < 0) 212 npad += nb; 213 fprintf(stderr, "(%d blocks padded)", 214 npad); 215 npad *= DSIZE; 216 wbuf[vec].iov_base = pad; 217 wbuf[vec++].iov_len = npad; 218 if (vec >= NPACKET_R) { 219 writev(fd, wbuf, vec); 220 vec = 0; 221 } 222 } 223 #endif 224 k++; 225 if (k % frame_rate[system] == 0) { 226 /* every second */ 227 fprintf(stderr, "\n"); 228 } 229 fflush(stderr); 230 m = 0; 231 } 232 if (k == 0 || (count > 0 && k > count)) 233 continue; 234 m++; 235 wbuf[vec].iov_base = (char *) dv; 236 wbuf[vec++].iov_len = DSIZE; 237 if (vec >= NPACKET_R) { 238 writev(fd, wbuf, vec); 239 vec = 0; 240 } 241 } 242 ptr = (u_int32_t *)dv; 243 next: 244 if ((char *)ptr < buf + tlen) 245 goto again; 246 if (vec > 0) 247 writev(fd, wbuf, vec); 248 } 249 close(fd); 250 fprintf(stderr, "\n"); 251 return 0; 252 } 253 254 255 int 256 dvsend(int d, char *filename, char ich, int count) 257 { 258 struct fw_isochreq isoreq; 259 struct fw_isobufreq bufreq; 260 struct dvdbc *dv; 261 struct fw_pkt *pkt; 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]; 265 char *pbuf; 266 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3]; 267 struct ciphdr *ciph; 268 struct timeval start, end; 269 double rtime; 270 271 cycle_acc = cycle = 0; 272 273 fd = open(filename, O_RDONLY); 274 pbuf = (char *)malloc(DSIZE * TNBUF); 275 bzero(wbuf, sizeof(wbuf)); 276 277 bufreq.rx.nchunk = 0; 278 bufreq.rx.npacket = 0; 279 bufreq.rx.psize = 0; 280 bufreq.tx.nchunk = NCHUNK; 281 bufreq.tx.npacket = NPACKET_T; 282 bufreq.tx.psize = PSIZE; 283 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) { 284 err(1, "ioctl"); 285 } 286 287 isoreq.ch = ich & 0x3f; 288 isoreq.tag = (ich >> 6) & 3; 289 290 if( ioctl(d, FW_STSTREAM, &isoreq) < 0) 291 err(1, "ioctl"); 292 293 iso_data = 0; 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); 302 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 */ 307 ciph->len = 120; 308 ciph->dbc = 0; 309 ciph->eoh1 = 1; 310 ciph->fdf.dv.cyc = 0xffff; 311 312 for (i = 1; i < TNBUF; i++) { 313 bcopy(hdr[0], hdr[i], sizeof(hdr[0])); 314 } 315 316 gettimeofday(&start, NULL); 317 #if DEBUG 318 fprintf(stderr, "%08x %08x %08x\n", 319 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); 320 #endif 321 frames = 0; 322 packets = 0; 323 pad_acc = 0; 324 while (1) { 325 tlen = 0; 326 while (tlen < DSIZE * TNBUF) { 327 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen); 328 if (len <= 0) { 329 if (tlen > 0) 330 break; 331 if (len < 0) 332 warn("read"); 333 else 334 printf("\nend of file\n"); 335 goto send_end; 336 } 337 tlen += len; 338 } 339 vec = 0; 340 offset = 0; 341 nhdr = 0; 342 next: 343 dv = (struct dvdbc *)(pbuf + offset * DSIZE); 344 #if 0 345 header = (dv->sct == 0 && dv->dseq == 0); 346 #else 347 header = (packets == 0 || packets % npackets[system] == 0); 348 #endif 349 350 ciph = (struct ciphdr *)&hdr[nhdr][1]; 351 if (header) { 352 if (system < 0) { 353 system = ((dv->payload[0] & DV_DSF_12) != 0); 354 printf("%s\n", system_name[system]); 355 cycle = 1; 356 cycle_acc = frame_cycle[system].d * cycle; 357 } 358 fprintf(stderr, "%d", frames % 10); 359 frames ++; 360 if (count > 0 && frames > count) 361 break; 362 if (frames % frame_rate[system] == 0) 363 fprintf(stderr, "\n"); 364 fflush(stderr); 365 f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf; 366 f_frac = (cycle_acc % frame_cycle[system].d 367 * CYCLE_FRAC) / frame_cycle[system].d; 368 #if 0 369 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac); 370 #else 371 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac); 372 #endif 373 cycle_acc += frame_cycle[system].n; 374 cycle_acc %= frame_cycle[system].d * 0x10; 375 376 } else { 377 ciph->fdf.dv.cyc = 0xffff; 378 } 379 ciph->dbc = packets++ % 256; 380 pad_acc += pad_rate[system].n; 381 if (pad_acc >= pad_rate[system].d) { 382 pad_acc -= pad_rate[system].d; 383 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0])); 384 hdr[nhdr][0] = iso_empty; 385 wbuf[vec].iov_base = (char *)hdr[nhdr]; 386 wbuf[vec++].iov_len = sizeof(hdr[0]); 387 nhdr ++; 388 cycle ++; 389 } 390 hdr[nhdr][0] = iso_data; 391 wbuf[vec].iov_base = (char *)hdr[nhdr]; 392 wbuf[vec++].iov_len = sizeof(hdr[0]); 393 wbuf[vec].iov_base = (char *)dv; 394 wbuf[vec++].iov_len = DSIZE; 395 nhdr ++; 396 cycle ++; 397 offset ++; 398 if (offset * DSIZE < tlen) 399 goto next; 400 401 again: 402 len = writev(d, wbuf, vec); 403 if (len < 0) { 404 if (errno == EAGAIN) { 405 fprintf(stderr, "(EAGAIN)\n"); 406 fflush(stderr); 407 goto again; 408 } 409 err(1, "write failed"); 410 } 411 } 412 close(fd); 413 fprintf(stderr, "\n"); 414 send_end: 415 gettimeofday(&end, NULL); 416 rtime = end.tv_sec - start.tv_sec 417 + (end.tv_usec - start.tv_usec) * 1e-6; 418 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n", 419 frames, rtime, frames/rtime); 420 return 0; 421 } 422