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: src/usr.sbin/fwcontrol/fwdv.c,v 1.2.2.4 2003/04/28 03:29:18 simokawa Exp $ 35 */ 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 #include <bus/firewire/firewire.h> 56 #include <bus/firewire/iec68113.h> 57 58 #define DEBUG 0 59 #define FIX_FRAME 1 60 61 struct frac { 62 int n,d; 63 }; 64 65 struct frac frame_cycle[2] = { 66 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */ 67 {320, 1}, /* PAL 8000 cycle / 25 Hz */ 68 }; 69 int npackets[] = { 70 250 /* NTSC */, 71 300 /* PAL */ 72 }; 73 struct frac pad_rate[2] = { 74 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */ 75 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */ 76 }; 77 const char *system_name[] = {"NTSC", "PAL"}; 78 int frame_rate[] = {30, 25}; 79 80 #define PSIZE 512 81 #define DSIZE 480 82 #define NCHUNK 8 83 84 #define NPACKET_R 256 85 #define NPACKET_T 255 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 91 92 int dvrecv(int, char *, char, int); 93 int dvsend(int, char *, char, int); 94 95 int 96 dvrecv(int d, char *filename, char ich, int count) 97 { 98 struct fw_isochreq isoreq; 99 struct fw_isobufreq bufreq; 100 struct dvdbc *dv; 101 struct ciphdr *ciph; 102 struct fw_pkt *pkt; 103 char *pad, *buf; 104 u_int32_t *ptr; 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]; 108 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)); 114 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; 120 bufreq.tx.psize = 0; 121 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) { 122 err(1, "ioctl"); 123 } 124 125 isoreq.ch = ich & 0x3f; 126 isoreq.tag = (ich >> 6) & 3; 127 128 if( ioctl(d, FW_SRSTREAM, &isoreq) < 0) 129 err(1, "ioctl"); 130 131 k = m = 0; 132 while (count <= 0 || k <= count) { 133 #if 0 134 tlen = 0; 135 while ((len = read(d, buf + tlen, PSIZE 136 /* RBUFSIZE - tlen */)) > 0) { 137 if (len < 0) { 138 if (errno == EAGAIN) { 139 fprintf(stderr, "(EAGAIN)\n"); 140 fflush(stderr); 141 if (len <= 0) 142 continue; 143 } else 144 err(1, "read failed"); 145 } 146 tlen += len; 147 if ((RBUFSIZE - tlen) < PSIZE) 148 break; 149 }; 150 #else 151 tlen = len = read(d, buf, RBUFSIZE); 152 if (len < 0) { 153 if (errno == EAGAIN) { 154 fprintf(stderr, "(EAGAIN)\n"); 155 fflush(stderr); 156 if (len <= 0) 157 continue; 158 } else 159 err(1, "read failed"); 160 } 161 #endif 162 vec = 0; 163 ptr = (u_int32_t *) buf; 164 again: 165 pkt = (struct fw_pkt *) ptr; 166 #if DEBUG 167 fprintf(stderr, "%08x %08x %08x %08x\n", 168 htonl(ptr[0]), htonl(ptr[1]), 169 htonl(ptr[2]), htonl(ptr[3])); 170 #endif 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 */ 175 #if DEBUG 176 if (ciph->fdf.dv.cyc != 0xffff && k == 0) { 177 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc)); 178 } 179 #endif 180 if (pkt->mode.stream.len <= sizeof(struct ciphdr)) 181 /* no payload */ 182 goto next; 183 for (dv = (struct dvdbc *)ptr; 184 (char *)dv < (char *)(ptr + ciph->len); 185 dv+=6) { 186 187 #if DEBUG 188 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq); 189 #endif 190 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) { 191 if (systype < 0) { 192 systype = ciph->fdf.dv.fs; 193 printf("%s\n", system_name[systype]); 194 } 195 196 /* Fix DSF bit */ 197 if (systype == 1 && 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); 202 #if FIX_FRAME 203 if (m > 0 && m != nb) { 204 /* padding bad frame */ 205 npad = ((nb - m) % nb); 206 if (npad < 0) 207 npad += nb; 208 fprintf(stderr, "(%d blocks padded)", 209 npad); 210 npad *= DSIZE; 211 wbuf[vec].iov_base = pad; 212 wbuf[vec++].iov_len = npad; 213 if (vec >= NPACKET_R) { 214 writev(fd, wbuf, vec); 215 vec = 0; 216 } 217 } 218 #endif 219 k++; 220 if (k % frame_rate[systype] == 0) { 221 /* every second */ 222 fprintf(stderr, "\n"); 223 } 224 fflush(stderr); 225 m = 0; 226 } 227 if (k == 0 || (count > 0 && k > count)) 228 continue; 229 m++; 230 wbuf[vec].iov_base = (char *) dv; 231 wbuf[vec++].iov_len = DSIZE; 232 if (vec >= NPACKET_R) { 233 writev(fd, wbuf, vec); 234 vec = 0; 235 } 236 } 237 ptr = (u_int32_t *)dv; 238 next: 239 if ((char *)ptr < buf + tlen) 240 goto again; 241 if (vec > 0) 242 writev(fd, wbuf, vec); 243 } 244 close(fd); 245 fprintf(stderr, "\n"); 246 return 0; 247 } 248 249 250 int 251 dvsend(int d, char *filename, char ich, int count) 252 { 253 struct fw_isochreq isoreq; 254 struct fw_isobufreq bufreq; 255 struct dvdbc *dv; 256 struct fw_pkt *pkt; 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]; 260 char *pbuf; 261 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3]; 262 struct ciphdr *ciph; 263 struct timeval start, end; 264 double rtime; 265 266 fd = open(filename, O_RDONLY); 267 pbuf = (char *)malloc(DSIZE * TNBUF); 268 bzero(wbuf, sizeof(wbuf)); 269 270 bufreq.rx.nchunk = 0; 271 bufreq.rx.npacket = 0; 272 bufreq.rx.psize = 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) { 277 err(1, "ioctl"); 278 } 279 280 isoreq.ch = ich & 0x3f; 281 isoreq.tag = (ich >> 6) & 3; 282 283 if( ioctl(d, FW_STSTREAM, &isoreq) < 0) 284 err(1, "ioctl"); 285 286 iso_data = 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); 295 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 */ 300 ciph->len = 120; 301 ciph->dbc = 0; 302 ciph->eoh1 = 1; 303 ciph->fdf.dv.cyc = 0xffff; 304 305 for (i = 1; i < TNBUF; i++) { 306 bcopy(hdr[0], hdr[i], sizeof(hdr[0])); 307 } 308 309 gettimeofday(&start, NULL); 310 #if DEBUG 311 fprintf(stderr, "%08x %08x %08x\n", 312 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); 313 #endif 314 frames = 0; 315 packets = 0; 316 pad_acc = 0; 317 while (1) { 318 tlen = 0; 319 while (tlen < DSIZE * TNBUF) { 320 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen); 321 if (len <= 0) { 322 if (tlen > 0) 323 break; 324 if (len < 0) 325 warn("read"); 326 else 327 printf("\nend of file\n"); 328 goto send_end; 329 } 330 tlen += len; 331 } 332 vec = 0; 333 offset = 0; 334 nhdr = 0; 335 next: 336 dv = (struct dvdbc *)(pbuf + offset * DSIZE); 337 #if 0 338 header = (dv->sct == 0 && dv->dseq == 0); 339 #else 340 header = (packets == 0 || packets % npackets[systype] == 0); 341 #endif 342 343 ciph = (struct ciphdr *)&hdr[nhdr][1]; 344 if (header) { 345 if (systype < 0) { 346 systype = ((dv->payload[0] & DV_DSF_12) != 0); 347 printf("%s\n", system_name[systype]); 348 cycle = 1; 349 cycle_acc = frame_cycle[systype].d * cycle; 350 } 351 fprintf(stderr, "%d", frames % 10); 352 frames ++; 353 if (count > 0 && frames > count) 354 break; 355 if (frames % frame_rate[systype] == 0) 356 fprintf(stderr, "\n"); 357 fflush(stderr); 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; 361 #if 0 362 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac); 363 #else 364 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac); 365 #endif 366 cycle_acc += frame_cycle[systype].n; 367 cycle_acc %= frame_cycle[systype].d * 0x10; 368 369 } else { 370 ciph->fdf.dv.cyc = 0xffff; 371 } 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]); 380 nhdr ++; 381 cycle ++; 382 } 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; 388 nhdr ++; 389 cycle ++; 390 offset ++; 391 if (offset * DSIZE < tlen) 392 goto next; 393 394 again: 395 len = writev(d, wbuf, vec); 396 if (len < 0) { 397 if (errno == EAGAIN) { 398 fprintf(stderr, "(EAGAIN)\n"); 399 fflush(stderr); 400 goto again; 401 } 402 err(1, "write failed"); 403 } 404 } 405 close(fd); 406 fprintf(stderr, "\n"); 407 send_end: 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); 413 return 0; 414 } 415