1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (C) 2003 5 * Hidetoshi Shimokawa. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * 18 * This product includes software developed by Hidetoshi Shimokawa. 19 * 20 * 4. Neither the name of the author nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $FreeBSD$ 37 */ 38 #include <sys/param.h> 39 #include <sys/ioctl.h> 40 #include <sys/time.h> 41 #include <sys/types.h> 42 #include <sys/uio.h> 43 #include <arpa/inet.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <unistd.h> 47 #include <fcntl.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <sysexits.h> 52 53 #include <dev/firewire/firewire.h> 54 #include <dev/firewire/iec68113.h> 55 56 #include "fwmethods.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 char *system_name[] = {"NTSC", "PAL"}; 78 int frame_rate[] = {30, 25}; 79 80 #define PSIZE 512 81 #define DSIZE 480 82 #define NCHUNK 64 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 void 93 dvrecv(int d, const char *filename, char ich, int count) 94 { 95 struct fw_isochreq isoreq; 96 struct fw_isobufreq bufreq; 97 struct dvdbc *dv; 98 struct ciphdr *ciph; 99 struct fw_pkt *pkt; 100 char *pad, *buf; 101 u_int32_t *ptr; 102 int len, tlen, npad, fd, k, m, vec, system = -1, nb; 103 int nblocks[] = {250 /* NTSC */, 300 /* PAL */}; 104 struct iovec wbuf[NPACKET_R]; 105 106 if(strcmp(filename, "-") == 0) { 107 fd = STDOUT_FILENO; 108 } else { 109 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660); 110 if (fd == -1) 111 err(EX_NOINPUT, "%s", filename); 112 } 113 buf = malloc(RBUFSIZE); 114 pad = malloc(DSIZE*MAXBLOCKS); 115 memset(pad, 0xff, DSIZE*MAXBLOCKS); 116 bzero(wbuf, sizeof(wbuf)); 117 118 bufreq.rx.nchunk = NCHUNK; 119 bufreq.rx.npacket = NPACKET_R; 120 bufreq.rx.psize = PSIZE; 121 bufreq.tx.nchunk = 0; 122 bufreq.tx.npacket = 0; 123 bufreq.tx.psize = 0; 124 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) 125 err(1, "ioctl FW_SSTBUF"); 126 127 isoreq.ch = ich & 0x3f; 128 isoreq.tag = (ich >> 6) & 3; 129 130 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0) 131 err(1, "ioctl"); 132 133 k = m = 0; 134 while (count <= 0 || k <= count) { 135 #if 0 136 tlen = 0; 137 while ((len = read(d, buf + tlen, PSIZE 138 /* RBUFSIZE - tlen */)) > 0) { 139 if (len < 0) { 140 if (errno == EAGAIN) { 141 fprintf(stderr, "(EAGAIN)\n"); 142 fflush(stderr); 143 if (len <= 0) 144 continue; 145 } else 146 err(1, "read failed"); 147 } 148 tlen += len; 149 if ((RBUFSIZE - tlen) < PSIZE) 150 break; 151 }; 152 #else 153 tlen = len = read(d, buf, RBUFSIZE); 154 if (len < 0) { 155 if (errno == EAGAIN) { 156 fprintf(stderr, "(EAGAIN) - push 'Play'?\n"); 157 fflush(stderr); 158 if (len <= 0) 159 continue; 160 } else 161 err(1, "read failed"); 162 } 163 #endif 164 vec = 0; 165 ptr = (u_int32_t *) buf; 166 again: 167 pkt = (struct fw_pkt *) ptr; 168 #if DEBUG 169 fprintf(stderr, "%08x %08x %08x %08x\n", 170 htonl(ptr[0]), htonl(ptr[1]), 171 htonl(ptr[2]), htonl(ptr[3])); 172 #endif 173 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */ 174 if (ciph->fmt != CIP_FMT_DVCR) 175 errx(1, "unknown format 0x%x", ciph->fmt); 176 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */ 177 #if DEBUG 178 if (ciph->fdf.dv.cyc != 0xffff && k == 0) { 179 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc)); 180 } 181 #endif 182 if (pkt->mode.stream.len <= sizeof(struct ciphdr)) 183 /* no payload */ 184 goto next; 185 for (dv = (struct dvdbc *)ptr; 186 (char *)dv < (char *)(ptr + ciph->len); 187 dv+=6) { 188 189 #if DEBUG 190 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq); 191 #endif 192 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) { 193 if (system < 0) { 194 system = ciph->fdf.dv.fs; 195 fprintf(stderr, "%s\n", system_name[system]); 196 } 197 198 /* Fix DSF bit */ 199 if (system == 1 && 200 (dv->payload[0] & DV_DSF_12) == 0) 201 dv->payload[0] |= DV_DSF_12; 202 nb = nblocks[system]; 203 fprintf(stderr, "%d:%02d:%02d %d\r", 204 k / (3600 * frame_rate[system]), 205 (k / (60 * frame_rate[system])) % 60, 206 (k / frame_rate[system]) % 60, 207 k % frame_rate[system]); 208 209 #if FIX_FRAME 210 if (m > 0 && m != nb) { 211 /* padding bad frame */ 212 npad = ((nb - m) % nb); 213 if (npad < 0) 214 npad += nb; 215 fprintf(stderr, "\n%d blocks padded\n", 216 npad); 217 npad *= DSIZE; 218 wbuf[vec].iov_base = pad; 219 wbuf[vec++].iov_len = npad; 220 if (vec >= NPACKET_R) { 221 writev(fd, wbuf, vec); 222 vec = 0; 223 } 224 } 225 #endif 226 k++; 227 fflush(stderr); 228 m = 0; 229 } 230 if (k == 0 || (count > 0 && k > count)) 231 continue; 232 m++; 233 wbuf[vec].iov_base = (char *) dv; 234 wbuf[vec++].iov_len = DSIZE; 235 if (vec >= NPACKET_R) { 236 writev(fd, wbuf, vec); 237 vec = 0; 238 } 239 } 240 ptr = (u_int32_t *)dv; 241 next: 242 if ((char *)ptr < buf + tlen) 243 goto again; 244 if (vec > 0) 245 writev(fd, wbuf, vec); 246 } 247 if (fd != STDOUT_FILENO) 248 close(fd); 249 fprintf(stderr, "\n"); 250 } 251 252 253 void 254 dvsend(int d, const char *filename, char ich, int count) 255 { 256 struct fw_isochreq isoreq; 257 struct fw_isobufreq bufreq; 258 struct dvdbc *dv; 259 struct fw_pkt *pkt; 260 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i; 261 int system=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac; 262 struct iovec wbuf[TNBUF*2 + NEMPTY]; 263 char *pbuf; 264 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3]; 265 struct ciphdr *ciph; 266 struct timeval start, end; 267 double rtime; 268 269 fd = open(filename, O_RDONLY); 270 if (fd == -1) 271 err(EX_NOINPUT, "%s", filename); 272 273 pbuf = malloc(DSIZE * TNBUF); 274 bzero(wbuf, sizeof(wbuf)); 275 276 bufreq.rx.nchunk = 0; 277 bufreq.rx.npacket = 0; 278 bufreq.rx.psize = 0; 279 bufreq.tx.nchunk = NCHUNK; 280 bufreq.tx.npacket = NPACKET_T; 281 bufreq.tx.psize = PSIZE; 282 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) 283 err(1, "ioctl FW_SSTBUF"); 284 285 isoreq.ch = ich & 0x3f; 286 isoreq.tag = (ich >> 6) & 3; 287 288 if (ioctl(d, FW_STSTREAM, &isoreq) < 0) 289 err(1, "ioctl FW_STSTREAM"); 290 291 iso_data = 0; 292 pkt = (struct fw_pkt *) &iso_data; 293 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr); 294 pkt->mode.stream.sy = 0; 295 pkt->mode.stream.tcode = FWTCODE_STREAM; 296 pkt->mode.stream.chtag = ich; 297 iso_empty = iso_data; 298 pkt = (struct fw_pkt *) &iso_empty; 299 pkt->mode.stream.len = sizeof(struct ciphdr); 300 301 bzero(hdr[0], sizeof(hdr[0])); 302 hdr[0][0] = iso_data; 303 ciph = (struct ciphdr *)&hdr[0][1]; 304 ciph->src = 0; /* XXX */ 305 ciph->len = 120; 306 ciph->dbc = 0; 307 ciph->eoh1 = 1; 308 ciph->fdf.dv.cyc = 0xffff; 309 310 for (i = 1; i < TNBUF; i++) 311 bcopy(hdr[0], hdr[i], sizeof(hdr[0])); 312 313 gettimeofday(&start, NULL); 314 #if DEBUG 315 fprintf(stderr, "%08x %08x %08x\n", 316 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); 317 #endif 318 frames = 0; 319 packets = 0; 320 pad_acc = 0; 321 while (1) { 322 tlen = 0; 323 while (tlen < DSIZE * TNBUF) { 324 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen); 325 if (len <= 0) { 326 if (tlen > 0) 327 break; 328 if (len < 0) 329 warn("read"); 330 else 331 fprintf(stderr, "\nend of file\n"); 332 goto send_end; 333 } 334 tlen += len; 335 } 336 vec = 0; 337 offset = 0; 338 nhdr = 0; 339 next: 340 dv = (struct dvdbc *)(pbuf + offset * DSIZE); 341 #if 0 342 header = (dv->sct == 0 && dv->dseq == 0); 343 #else 344 header = (packets == 0 || packets % npackets[system] == 0); 345 #endif 346 347 ciph = (struct ciphdr *)&hdr[nhdr][1]; 348 if (header) { 349 if (system < 0) { 350 system = ((dv->payload[0] & DV_DSF_12) != 0); 351 printf("%s\n", system_name[system]); 352 cycle = 1; 353 cycle_acc = frame_cycle[system].d * cycle; 354 } 355 fprintf(stderr, "%d", frames % 10); 356 frames ++; 357 if (count > 0 && frames > count) 358 break; 359 if (frames % frame_rate[system] == 0) 360 fprintf(stderr, "\n"); 361 fflush(stderr); 362 f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf; 363 f_frac = (cycle_acc % frame_cycle[system].d 364 * CYCLE_FRAC) / frame_cycle[system].d; 365 #if 0 366 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac); 367 #else 368 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac); 369 #endif 370 cycle_acc += frame_cycle[system].n; 371 cycle_acc %= frame_cycle[system].d * 0x10; 372 373 } else { 374 ciph->fdf.dv.cyc = 0xffff; 375 } 376 ciph->dbc = packets++ % 256; 377 pad_acc += pad_rate[system].n; 378 if (pad_acc >= pad_rate[system].d) { 379 pad_acc -= pad_rate[system].d; 380 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0])); 381 hdr[nhdr][0] = iso_empty; 382 wbuf[vec].iov_base = (char *)hdr[nhdr]; 383 wbuf[vec++].iov_len = sizeof(hdr[0]); 384 nhdr ++; 385 cycle ++; 386 } 387 hdr[nhdr][0] = iso_data; 388 wbuf[vec].iov_base = (char *)hdr[nhdr]; 389 wbuf[vec++].iov_len = sizeof(hdr[0]); 390 wbuf[vec].iov_base = (char *)dv; 391 wbuf[vec++].iov_len = DSIZE; 392 nhdr ++; 393 cycle ++; 394 offset ++; 395 if (offset * DSIZE < tlen) 396 goto next; 397 398 again: 399 len = writev(d, wbuf, vec); 400 if (len < 0) { 401 if (errno == EAGAIN) { 402 fprintf(stderr, "(EAGAIN) - push 'Play'?\n"); 403 goto again; 404 } 405 err(1, "write failed"); 406 } 407 } 408 fprintf(stderr, "\n"); 409 send_end: 410 gettimeofday(&end, NULL); 411 rtime = end.tv_sec - start.tv_sec 412 + (end.tv_usec - start.tv_usec) * 1e-6; 413 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n", 414 frames, rtime, frames/rtime); 415 close(fd); 416 } 417