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