1 /* $NetBSD: fwdv.c,v 1.4 2010/08/24 08:41:24 cegger 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.8 2009/02/02 21:05:12 sbruno 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 #include <err.h> 44 #include <errno.h> 45 #include <unistd.h> 46 #include <fcntl.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <sysexits.h> 51 52 #include <dev/ieee1394/firewire.h> 53 #include <dev/ieee1394/iec68113.h> 54 55 #include "fwmethods.h" 56 57 #define DEBUG 0 58 #define FIX_FRAME 1 59 60 struct frac { 61 int n,d; 62 }; 63 64 struct frac frame_cycle[2] = { 65 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */ 66 {320, 1}, /* PAL 8000 cycle / 25 Hz */ 67 }; 68 int npackets[] = { 69 250 /* NTSC */, 70 300 /* PAL */ 71 }; 72 struct frac pad_rate[2] = { 73 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */ 74 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */ 75 }; 76 const char *system_name[] = {"NTSC", "PAL"}; 77 int frame_rate[] = {30, 25}; 78 79 #define PSIZE 512 80 #define DSIZE 480 81 #define NCHUNK 64 82 83 #define NPACKET_R 256 84 #define NPACKET_T 255 85 #define TNBUF 100 /* XXX too large value causes block noise */ 86 #define NEMPTY 10 /* depends on TNBUF */ 87 #define RBUFSIZE (PSIZE * NPACKET_R) 88 #define MAXBLOCKS (300) 89 #define CYCLE_FRAC 0xc00 90 91 void 92 dvrecv(int d, const char *filename, char ich, int count) 93 { 94 struct fw_isochreq isoreq; 95 struct fw_isobufreq bufreq; 96 struct dvdbc *dv; 97 struct ciphdr *ciph; 98 struct fw_pkt *pkt; 99 char *pad, *buf; 100 uint32_t *ptr; 101 int len, tlen, npad, fd, k, m, vec, lsystem = -1, nb; 102 int nblocks[] = {250 /* NTSC */, 300 /* PAL */}; 103 struct iovec wbuf[NPACKET_R]; 104 105 if (strcmp(filename, "-") == 0) { 106 fd = STDOUT_FILENO; 107 } else { 108 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660); 109 if (fd == -1) 110 err(EX_NOINPUT, filename); 111 } 112 buf = malloc(RBUFSIZE); 113 pad = malloc(DSIZE*MAXBLOCKS); 114 memset(pad, 0xff, DSIZE*MAXBLOCKS); 115 bzero(wbuf, sizeof(wbuf)); 116 117 bufreq.rx.nchunk = NCHUNK; 118 bufreq.rx.npacket = NPACKET_R; 119 bufreq.rx.psize = PSIZE; 120 bufreq.tx.nchunk = 0; 121 bufreq.tx.npacket = 0; 122 bufreq.tx.psize = 0; 123 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) 124 err(EXIT_FAILURE, "ioctl FW_SSTBUF"); 125 126 isoreq.ch = ich & 0x3f; 127 isoreq.tag = (ich >> 6) & 3; 128 129 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0) 130 err(EXIT_FAILURE, "ioctl"); 131 132 k = m = 0; 133 while (count <= 0 || k <= count) { 134 #if 0 135 tlen = 0; 136 while ((len = read(d, buf + tlen, PSIZE 137 /* RBUFSIZE - tlen */)) > 0) { 138 if (len < 0) { 139 if (errno == EAGAIN) { 140 fprintf(stderr, "(EAGAIN)\n"); 141 fflush(stderr); 142 if (len <= 0) 143 continue; 144 } else 145 err(EXIT_FAILURE, "read failed"); 146 } 147 tlen += len; 148 if ((RBUFSIZE - tlen) < PSIZE) 149 break; 150 }; 151 #else 152 tlen = len = read(d, buf, RBUFSIZE); 153 if (len < 0) { 154 if (errno == EAGAIN) { 155 fprintf(stderr, "(EAGAIN) - push 'Play'?\n"); 156 fflush(stderr); 157 if (len <= 0) 158 continue; 159 } else 160 err(EXIT_FAILURE, "read failed"); 161 } 162 #endif 163 vec = 0; 164 ptr = (uint32_t *) buf; 165 again: 166 pkt = (struct fw_pkt *) ptr; 167 #if DEBUG 168 fprintf(stderr, "%08x %08x %08x %08x\n", 169 htonl(ptr[0]), htonl(ptr[1]), 170 htonl(ptr[2]), htonl(ptr[3])); 171 #endif 172 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */ 173 if (ciph->fmt != CIP_FMT_DVCR) 174 errx(1, "unknown format 0x%x", ciph->fmt); 175 ptr = (uint32_t *) (ciph + 1); /* skip cip header */ 176 #if DEBUG 177 if (ciph->fdf.dv.cyc != 0xffff && k == 0) 178 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc)); 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 (lsystem < 0) { 192 lsystem = ciph->fdf.dv.fs; 193 fprintf(stderr, 194 "%s\n", system_name[lsystem]); 195 } 196 197 /* Fix DSF bit */ 198 if (lsystem == 1 && 199 (dv->payload[0] & DV_DSF_12) == 0) 200 dv->payload[0] |= DV_DSF_12; 201 nb = nblocks[lsystem]; 202 fprintf(stderr, "%d:%02d:%02d %d\r", 203 k / (3600 * frame_rate[lsystem]), 204 (k / (60 * frame_rate[lsystem])) % 60, 205 (k / frame_rate[lsystem]) % 60, 206 k % frame_rate[lsystem]); 207 208 #if FIX_FRAME 209 if (m > 0 && m != nb) { 210 /* padding bad frame */ 211 npad = ((nb - m) % nb); 212 if (npad < 0) 213 npad += nb; 214 fprintf(stderr, "\n%d blocks padded\n", 215 npad); 216 npad *= DSIZE; 217 wbuf[vec].iov_base = pad; 218 wbuf[vec++].iov_len = npad; 219 if (vec >= NPACKET_R) { 220 writev(fd, wbuf, vec); 221 vec = 0; 222 } 223 } 224 #endif 225 k++; 226 fflush(stderr); 227 m = 0; 228 } 229 if (k == 0 || (count > 0 && k > count)) 230 continue; 231 m++; 232 wbuf[vec].iov_base = (char *) dv; 233 wbuf[vec++].iov_len = DSIZE; 234 if (vec >= NPACKET_R) { 235 writev(fd, wbuf, vec); 236 vec = 0; 237 } 238 } 239 ptr = (uint32_t *)dv; 240 next: 241 if ((char *)ptr < buf + tlen) 242 goto again; 243 if (vec > 0) 244 writev(fd, wbuf, vec); 245 } 246 if (fd != STDOUT_FILENO) 247 close(fd); 248 fprintf(stderr, "\n"); 249 } 250 251 252 void 253 dvsend(int d, const char *filename, char ich, int count) 254 { 255 struct fw_isochreq isoreq; 256 struct fw_isobufreq bufreq; 257 struct dvdbc *dv; 258 struct fw_pkt *pkt; 259 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i; 260 int lsystem=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac; 261 struct iovec wbuf[TNBUF*2 + NEMPTY]; 262 char *pbuf; 263 uint32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3]; 264 struct ciphdr *ciph; 265 struct timeval start, end; 266 double rtime; 267 268 cycle_acc = cycle = 0; 269 270 fd = open(filename, O_RDONLY); 271 if (fd == -1) 272 err(EX_NOINPUT, filename); 273 274 pbuf = 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(EXIT_FAILURE, "ioctl FW_SSTBUF"); 285 286 isoreq.ch = ich & 0x3f; 287 isoreq.tag = (ich >> 6) & 3; 288 289 if (ioctl(d, FW_STSTREAM, &isoreq) < 0) 290 err(EXIT_FAILURE, "ioctl FW_STSTREAM"); 291 292 iso_data = 0; 293 pkt = (struct fw_pkt *) &iso_data; 294 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr); 295 pkt->mode.stream.sy = 0; 296 pkt->mode.stream.tcode = FWTCODE_STREAM; 297 pkt->mode.stream.chtag = ich; 298 iso_empty = iso_data; 299 pkt = (struct fw_pkt *) &iso_empty; 300 pkt->mode.stream.len = sizeof(struct ciphdr); 301 302 bzero(hdr[0], sizeof(hdr[0])); 303 hdr[0][0] = iso_data; 304 ciph = (struct ciphdr *)&hdr[0][1]; 305 ciph->src = 0; /* XXX */ 306 ciph->len = 120; 307 ciph->dbc = 0; 308 ciph->eoh1 = 1; 309 ciph->fdf.dv.cyc = 0xffff; 310 311 for (i = 1; i < TNBUF; i++) 312 bcopy(hdr[0], hdr[i], sizeof(hdr[0])); 313 314 gettimeofday(&start, NULL); 315 #if DEBUG 316 fprintf(stderr, "%08x %08x %08x\n", 317 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); 318 #endif 319 frames = 0; 320 packets = 0; 321 pad_acc = 0; 322 while (1) { 323 tlen = 0; 324 while (tlen < DSIZE * TNBUF) { 325 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen); 326 if (len <= 0) { 327 if (tlen > 0) 328 break; 329 if (len < 0) 330 warn("read"); 331 else 332 fprintf(stderr, "\nend of file\n"); 333 goto send_end; 334 } 335 tlen += len; 336 } 337 vec = 0; 338 offset = 0; 339 nhdr = 0; 340 next: 341 dv = (struct dvdbc *)(pbuf + offset * DSIZE); 342 #if 0 343 header = (dv->sct == 0 && dv->dseq == 0); 344 #else 345 header = (packets == 0 || packets % npackets[lsystem] == 0); 346 #endif 347 348 ciph = (struct ciphdr *)&hdr[nhdr][1]; 349 if (header) { 350 if (lsystem < 0) { 351 lsystem = ((dv->payload[0] & DV_DSF_12) != 0); 352 printf("%s\n", system_name[lsystem]); 353 cycle = 1; 354 cycle_acc = frame_cycle[lsystem].d * cycle; 355 } 356 fprintf(stderr, "%d", frames % 10); 357 frames ++; 358 if (count > 0 && frames > count) 359 break; 360 if (frames % frame_rate[lsystem] == 0) 361 fprintf(stderr, "\n"); 362 fflush(stderr); 363 f_cycle = (cycle_acc / frame_cycle[lsystem].d) & 0xf; 364 f_frac = (cycle_acc % frame_cycle[lsystem].d 365 * CYCLE_FRAC) / frame_cycle[lsystem].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[lsystem].n; 372 cycle_acc %= frame_cycle[lsystem].d * 0x10; 373 374 } else { 375 ciph->fdf.dv.cyc = 0xffff; 376 } 377 ciph->dbc = packets++ % 256; 378 pad_acc += pad_rate[lsystem].n; 379 if (pad_acc >= pad_rate[lsystem].d) { 380 pad_acc -= pad_rate[lsystem].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) - push 'Play'?\n"); 404 goto again; 405 } 406 err(EXIT_FAILURE, "write failed"); 407 } 408 } 409 close(fd); 410 fprintf(stderr, "\n"); 411 send_end: 412 gettimeofday(&end, NULL); 413 rtime = end.tv_sec - start.tv_sec 414 + (end.tv_usec - start.tv_usec) * 1e-6; 415 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n", 416 frames, rtime, frames/rtime); 417 } 418