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_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 #if 0 266 int f_cycle; 267 #endif 268 269 fd = open(filename, O_RDONLY); 270 pbuf = (char *)malloc(DSIZE * TNBUF); 271 bzero(wbuf, sizeof(wbuf)); 272 273 bufreq.rx.nchunk = 0; 274 bufreq.rx.npacket = 0; 275 bufreq.rx.psize = 0; 276 bufreq.tx.nchunk = NCHUNK; 277 bufreq.tx.npacket = NPACKET_T; 278 bufreq.tx.psize = PSIZE; 279 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) { 280 err(1, "ioctl"); 281 } 282 283 isoreq.ch = ich & 0x3f; 284 isoreq.tag = (ich >> 6) & 3; 285 286 if( ioctl(d, FW_STSTREAM, &isoreq) < 0) 287 err(1, "ioctl"); 288 289 iso_data = 0; 290 pkt = (struct fw_pkt *) &iso_data; 291 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr); 292 pkt->mode.stream.sy = 0; 293 pkt->mode.stream.tcode = FWTCODE_STREAM; 294 pkt->mode.stream.chtag = ich; 295 iso_empty = iso_data; 296 pkt = (struct fw_pkt *) &iso_empty; 297 pkt->mode.stream.len = sizeof(struct ciphdr); 298 299 bzero(hdr[0], sizeof(hdr[0])); 300 hdr[0][0] = iso_data; 301 ciph = (struct ciphdr *)&hdr[0][1]; 302 ciph->src = 0; /* XXX */ 303 ciph->len = 120; 304 ciph->dbc = 0; 305 ciph->eoh1 = 1; 306 ciph->fdf.dv.cyc = 0xffff; 307 308 for (i = 1; i < TNBUF; i++) { 309 bcopy(hdr[0], hdr[i], sizeof(hdr[0])); 310 } 311 312 gettimeofday(&start, NULL); 313 #if DEBUG 314 fprintf(stderr, "%08x %08x %08x\n", 315 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); 316 #endif 317 frames = 0; 318 packets = 0; 319 pad_acc = 0; 320 while (1) { 321 tlen = 0; 322 while (tlen < DSIZE * TNBUF) { 323 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen); 324 if (len <= 0) { 325 if (tlen > 0) 326 break; 327 if (len < 0) 328 warn("read"); 329 else 330 printf("\nend of file\n"); 331 goto send_end; 332 } 333 tlen += len; 334 } 335 vec = 0; 336 offset = 0; 337 nhdr = 0; 338 next: 339 dv = (struct dvdbc *)(pbuf + offset * DSIZE); 340 #if 0 341 header = (dv->sct == 0 && dv->dseq == 0); 342 #else 343 header = (packets == 0 || packets % npackets[systype] == 0); 344 #endif 345 346 ciph = (struct ciphdr *)&hdr[nhdr][1]; 347 if (header) { 348 if (systype < 0) { 349 systype = ((dv->payload[0] & DV_DSF_12) != 0); 350 printf("%s\n", system_name[systype]); 351 cycle = 1; 352 cycle_acc = frame_cycle[systype].d * cycle; 353 } 354 fprintf(stderr, "%d", frames % 10); 355 frames ++; 356 if (count > 0 && frames > count) 357 break; 358 if (frames % frame_rate[systype] == 0) 359 fprintf(stderr, "\n"); 360 fflush(stderr); 361 #if 0 362 f_cycle = (cycle_acc / frame_cycle[systype].d) & 0xf; 363 #endif 364 f_frac = (cycle_acc % frame_cycle[systype].d 365 * CYCLE_FRAC) / frame_cycle[systype].d; 366 #if 0 367 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac); 368 #else 369 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac); 370 #endif 371 cycle_acc += frame_cycle[systype].n; 372 cycle_acc %= frame_cycle[systype].d * 0x10; 373 374 } else { 375 ciph->fdf.dv.cyc = 0xffff; 376 } 377 ciph->dbc = packets++ % 256; 378 pad_acc += pad_rate[systype].n; 379 if (pad_acc >= pad_rate[systype].d) { 380 pad_acc -= pad_rate[systype].d; 381 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0])); 382 hdr[nhdr][0] = iso_empty; 383 wbuf[vec].iov_base = (char *)hdr[nhdr]; 384 wbuf[vec++].iov_len = sizeof(hdr[0]); 385 nhdr ++; 386 cycle ++; 387 } 388 hdr[nhdr][0] = iso_data; 389 wbuf[vec].iov_base = (char *)hdr[nhdr]; 390 wbuf[vec++].iov_len = sizeof(hdr[0]); 391 wbuf[vec].iov_base = (char *)dv; 392 wbuf[vec++].iov_len = DSIZE; 393 nhdr ++; 394 cycle ++; 395 offset ++; 396 if (offset * DSIZE < tlen) 397 goto next; 398 399 again: 400 len = writev(d, wbuf, vec); 401 if (len < 0) { 402 if (errno == EAGAIN) { 403 fprintf(stderr, "(EAGAIN)\n"); 404 fflush(stderr); 405 goto again; 406 } 407 err(1, "write failed"); 408 } 409 } 410 close(fd); 411 fprintf(stderr, "\n"); 412 send_end: 413 gettimeofday(&end, NULL); 414 rtime = end.tv_sec - start.tv_sec 415 + (end.tv_usec - start.tv_usec) * 1e-6; 416 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n", 417 frames, rtime, frames/rtime); 418 return 0; 419 } 420