1109737Ssimokawa/* 2109737Ssimokawa * Copyright (C) 2003 3109737Ssimokawa * Hidetoshi Shimokawa. All rights reserved. 4163712Simp * 5109737Ssimokawa * Redistribution and use in source and binary forms, with or without 6109737Ssimokawa * modification, are permitted provided that the following conditions 7109737Ssimokawa * are met: 8109737Ssimokawa * 1. Redistributions of source code must retain the above copyright 9109737Ssimokawa * notice, this list of conditions and the following disclaimer. 10109737Ssimokawa * 2. Redistributions in binary form must reproduce the above copyright 11109737Ssimokawa * notice, this list of conditions and the following disclaimer in the 12109737Ssimokawa * documentation and/or other materials provided with the distribution. 13109737Ssimokawa * 3. All advertising materials mentioning features or use of this software 14109737Ssimokawa * must display the following acknowledgement: 15109737Ssimokawa * 16109737Ssimokawa * This product includes software developed by Hidetoshi Shimokawa. 17109737Ssimokawa * 18109737Ssimokawa * 4. Neither the name of the author nor the names of its contributors 19109737Ssimokawa * may be used to endorse or promote products derived from this software 20109737Ssimokawa * without specific prior written permission. 21163712Simp * 22109737Ssimokawa * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23109737Ssimokawa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24109737Ssimokawa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25109737Ssimokawa * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26109737Ssimokawa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27109737Ssimokawa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28109737Ssimokawa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29109737Ssimokawa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30109737Ssimokawa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31109737Ssimokawa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32109737Ssimokawa * SUCH DAMAGE. 33163712Simp * 34109737Ssimokawa * $FreeBSD$ 35109737Ssimokawa */ 36109737Ssimokawa#include <sys/param.h> 37109737Ssimokawa#include <sys/ioctl.h> 38109737Ssimokawa#include <sys/time.h> 39109737Ssimokawa#include <sys/types.h> 40109737Ssimokawa#include <sys/uio.h> 41109737Ssimokawa 42109737Ssimokawa#if __FreeBSD_version >= 500000 43109737Ssimokawa#include <arpa/inet.h> 44109737Ssimokawa#endif 45109737Ssimokawa 46109737Ssimokawa#include <err.h> 47109737Ssimokawa#include <errno.h> 48109737Ssimokawa#include <unistd.h> 49109737Ssimokawa#include <fcntl.h> 50109737Ssimokawa#include <stdio.h> 51109737Ssimokawa#include <stdlib.h> 52109737Ssimokawa#include <string.h> 53163712Simp#include <sysexits.h> 54109737Ssimokawa 55109737Ssimokawa#include <dev/firewire/firewire.h> 56109737Ssimokawa#include <dev/firewire/iec68113.h> 57109737Ssimokawa 58163712Simp#include "fwmethods.h" 59163712Simp 60111112Ssimokawa#define DEBUG 0 61111112Ssimokawa#define FIX_FRAME 1 62109737Ssimokawa 63109737Ssimokawastruct frac { 64163712Simp int n,d; 65109737Ssimokawa}; 66109737Ssimokawa 67109737Ssimokawastruct frac frame_cycle[2] = { 68109737Ssimokawa {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */ 69109737Ssimokawa {320, 1}, /* PAL 8000 cycle / 25 Hz */ 70109737Ssimokawa}; 71109737Ssimokawaint npackets[] = { 72109737Ssimokawa 250 /* NTSC */, 73109737Ssimokawa 300 /* PAL */ 74109737Ssimokawa}; 75109737Ssimokawastruct frac pad_rate[2] = { 76109737Ssimokawa {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */ 77109737Ssimokawa {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */ 78109737Ssimokawa}; 79111112Ssimokawachar *system_name[] = {"NTSC", "PAL"}; 80111112Ssimokawaint frame_rate[] = {30, 25}; 81109737Ssimokawa 82109737Ssimokawa#define PSIZE 512 83109737Ssimokawa#define DSIZE 480 84170878Ssimokawa#define NCHUNK 64 85109991Ssimokawa 86109991Ssimokawa#define NPACKET_R 256 87109991Ssimokawa#define NPACKET_T 255 88111112Ssimokawa#define TNBUF 100 /* XXX too large value causes block noise */ 89111112Ssimokawa#define NEMPTY 10 /* depends on TNBUF */ 90111112Ssimokawa#define RBUFSIZE (PSIZE * NPACKET_R) 91109991Ssimokawa#define MAXBLOCKS (300) 92109737Ssimokawa#define CYCLE_FRAC 0xc00 93109737Ssimokawa 94163712Simpvoid 95163712Simpdvrecv(int d, const char *filename, char ich, int count) 96109737Ssimokawa{ 97109737Ssimokawa struct fw_isochreq isoreq; 98109737Ssimokawa struct fw_isobufreq bufreq; 99109737Ssimokawa struct dvdbc *dv; 100109737Ssimokawa struct ciphdr *ciph; 101109737Ssimokawa struct fw_pkt *pkt; 102109737Ssimokawa char *pad, *buf; 103109737Ssimokawa u_int32_t *ptr; 104111112Ssimokawa int len, tlen, npad, fd, k, m, vec, system = -1, nb; 105109991Ssimokawa int nblocks[] = {250 /* NTSC */, 300 /* PAL */}; 106109991Ssimokawa struct iovec wbuf[NPACKET_R]; 107109737Ssimokawa 108163712Simp if(strcmp(filename, "-") == 0) { 109163712Simp fd = STDOUT_FILENO; 110163712Simp } else { 111163712Simp fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660); 112163712Simp if (fd == -1) 113216948Semaste err(EX_NOINPUT, "%s", filename); 114163712Simp } 115163712Simp buf = malloc(RBUFSIZE); 116163712Simp pad = malloc(DSIZE*MAXBLOCKS); 117111112Ssimokawa memset(pad, 0xff, DSIZE*MAXBLOCKS); 118109737Ssimokawa bzero(wbuf, sizeof(wbuf)); 119109737Ssimokawa 120109991Ssimokawa bufreq.rx.nchunk = NCHUNK; 121109991Ssimokawa bufreq.rx.npacket = NPACKET_R; 122109737Ssimokawa bufreq.rx.psize = PSIZE; 123109737Ssimokawa bufreq.tx.nchunk = 0; 124109737Ssimokawa bufreq.tx.npacket = 0; 125109737Ssimokawa bufreq.tx.psize = 0; 126163712Simp if (ioctl(d, FW_SSTBUF, &bufreq) < 0) 127163712Simp err(1, "ioctl FW_SSTBUF"); 128109737Ssimokawa 129109737Ssimokawa isoreq.ch = ich & 0x3f; 130109737Ssimokawa isoreq.tag = (ich >> 6) & 3; 131109737Ssimokawa 132163712Simp if (ioctl(d, FW_SRSTREAM, &isoreq) < 0) 133109737Ssimokawa err(1, "ioctl"); 134109737Ssimokawa 135109737Ssimokawa k = m = 0; 136109737Ssimokawa while (count <= 0 || k <= count) { 137109991Ssimokawa#if 0 138109991Ssimokawa tlen = 0; 139109991Ssimokawa while ((len = read(d, buf + tlen, PSIZE 140111112Ssimokawa /* RBUFSIZE - tlen */)) > 0) { 141109991Ssimokawa if (len < 0) { 142109991Ssimokawa if (errno == EAGAIN) { 143109991Ssimokawa fprintf(stderr, "(EAGAIN)\n"); 144109991Ssimokawa fflush(stderr); 145109991Ssimokawa if (len <= 0) 146109991Ssimokawa continue; 147109991Ssimokawa } else 148109991Ssimokawa err(1, "read failed"); 149109991Ssimokawa } 150109991Ssimokawa tlen += len; 151111112Ssimokawa if ((RBUFSIZE - tlen) < PSIZE) 152109991Ssimokawa break; 153109991Ssimokawa }; 154109991Ssimokawa#else 155111112Ssimokawa tlen = len = read(d, buf, RBUFSIZE); 156109737Ssimokawa if (len < 0) { 157109787Ssimokawa if (errno == EAGAIN) { 158163712Simp fprintf(stderr, "(EAGAIN) - push 'Play'?\n"); 159109787Ssimokawa fflush(stderr); 160109991Ssimokawa if (len <= 0) 161109991Ssimokawa continue; 162109991Ssimokawa } else 163109991Ssimokawa err(1, "read failed"); 164109737Ssimokawa } 165109991Ssimokawa#endif 166109991Ssimokawa vec = 0; 167109737Ssimokawa ptr = (u_int32_t *) buf; 168109737Ssimokawaagain: 169163712Simp pkt = (struct fw_pkt *) ptr; 170111112Ssimokawa#if DEBUG 171109787Ssimokawa fprintf(stderr, "%08x %08x %08x %08x\n", 172109737Ssimokawa htonl(ptr[0]), htonl(ptr[1]), 173109737Ssimokawa htonl(ptr[2]), htonl(ptr[3])); 174109737Ssimokawa#endif 175109737Ssimokawa ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */ 176109737Ssimokawa if (ciph->fmt != CIP_FMT_DVCR) 177109991Ssimokawa errx(1, "unknown format 0x%x", ciph->fmt); 178109737Ssimokawa ptr = (u_int32_t *) (ciph + 1); /* skip cip header */ 179111112Ssimokawa#if DEBUG 180109737Ssimokawa if (ciph->fdf.dv.cyc != 0xffff && k == 0) { 181109737Ssimokawa fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc)); 182109737Ssimokawa } 183109737Ssimokawa#endif 184113584Ssimokawa if (pkt->mode.stream.len <= sizeof(struct ciphdr)) 185109991Ssimokawa /* no payload */ 186109991Ssimokawa goto next; 187109737Ssimokawa for (dv = (struct dvdbc *)ptr; 188109737Ssimokawa (char *)dv < (char *)(ptr + ciph->len); 189109991Ssimokawa dv+=6) { 190109737Ssimokawa 191111112Ssimokawa#if DEBUG 192109787Ssimokawa fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq); 193109737Ssimokawa#endif 194109737Ssimokawa if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) { 195111112Ssimokawa if (system < 0) { 196111112Ssimokawa system = ciph->fdf.dv.fs; 197163712Simp fprintf(stderr, "%s\n", system_name[system]); 198111112Ssimokawa } 199111112Ssimokawa 200111112Ssimokawa /* Fix DSF bit */ 201111112Ssimokawa if (system == 1 && 202111112Ssimokawa (dv->payload[0] & DV_DSF_12) == 0) 203111112Ssimokawa dv->payload[0] |= DV_DSF_12; 204111112Ssimokawa nb = nblocks[system]; 205188029Ssbruno fprintf(stderr, "%d:%02d:%02d %d\r", 206188029Ssbruno k / (3600 * frame_rate[system]), 207188029Ssbruno (k / (60 * frame_rate[system])) % 60, 208188029Ssbruno (k / frame_rate[system]) % 60, 209188029Ssbruno k % frame_rate[system]); 210188029Ssbruno 211111112Ssimokawa#if FIX_FRAME 212109737Ssimokawa if (m > 0 && m != nb) { 213109737Ssimokawa /* padding bad frame */ 214109737Ssimokawa npad = ((nb - m) % nb); 215109737Ssimokawa if (npad < 0) 216109737Ssimokawa npad += nb; 217188029Ssbruno fprintf(stderr, "\n%d blocks padded\n", 218188029Ssbruno npad); 219109991Ssimokawa npad *= DSIZE; 220109991Ssimokawa wbuf[vec].iov_base = pad; 221109991Ssimokawa wbuf[vec++].iov_len = npad; 222109991Ssimokawa if (vec >= NPACKET_R) { 223109991Ssimokawa writev(fd, wbuf, vec); 224109991Ssimokawa vec = 0; 225109991Ssimokawa } 226109737Ssimokawa } 227109737Ssimokawa#endif 228109737Ssimokawa k++; 229109787Ssimokawa fflush(stderr); 230109737Ssimokawa m = 0; 231109737Ssimokawa } 232109737Ssimokawa if (k == 0 || (count > 0 && k > count)) 233109737Ssimokawa continue; 234109737Ssimokawa m++; 235109991Ssimokawa wbuf[vec].iov_base = (char *) dv; 236109991Ssimokawa wbuf[vec++].iov_len = DSIZE; 237109991Ssimokawa if (vec >= NPACKET_R) { 238109991Ssimokawa writev(fd, wbuf, vec); 239109991Ssimokawa vec = 0; 240109737Ssimokawa } 241109737Ssimokawa } 242109737Ssimokawa ptr = (u_int32_t *)dv; 243109737Ssimokawanext: 244109991Ssimokawa if ((char *)ptr < buf + tlen) 245109737Ssimokawa goto again; 246109991Ssimokawa if (vec > 0) 247109991Ssimokawa writev(fd, wbuf, vec); 248109737Ssimokawa } 249188029Ssbruno if (fd != STDOUT_FILENO) 250163712Simp close(fd); 251109787Ssimokawa fprintf(stderr, "\n"); 252109737Ssimokawa} 253109737Ssimokawa 254109991Ssimokawa 255163712Simpvoid 256163712Simpdvsend(int d, const char *filename, char ich, int count) 257109737Ssimokawa{ 258109737Ssimokawa struct fw_isochreq isoreq; 259109737Ssimokawa struct fw_isobufreq bufreq; 260109737Ssimokawa struct dvdbc *dv; 261109737Ssimokawa struct fw_pkt *pkt; 262109991Ssimokawa int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i; 263163712Simp int system=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac; 264111112Ssimokawa struct iovec wbuf[TNBUF*2 + NEMPTY]; 265109737Ssimokawa char *pbuf; 266111112Ssimokawa u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3]; 267109991Ssimokawa struct ciphdr *ciph; 268109737Ssimokawa struct timeval start, end; 269109737Ssimokawa double rtime; 270109737Ssimokawa 271109737Ssimokawa fd = open(filename, O_RDONLY); 272163712Simp if (fd == -1) 273216948Semaste err(EX_NOINPUT, "%s", filename); 274163712Simp 275163712Simp pbuf = malloc(DSIZE * TNBUF); 276109737Ssimokawa bzero(wbuf, sizeof(wbuf)); 277109737Ssimokawa 278109737Ssimokawa bufreq.rx.nchunk = 0; 279109737Ssimokawa bufreq.rx.npacket = 0; 280109737Ssimokawa bufreq.rx.psize = 0; 281109991Ssimokawa bufreq.tx.nchunk = NCHUNK; 282109991Ssimokawa bufreq.tx.npacket = NPACKET_T; 283109737Ssimokawa bufreq.tx.psize = PSIZE; 284163712Simp if (ioctl(d, FW_SSTBUF, &bufreq) < 0) 285163712Simp err(1, "ioctl FW_SSTBUF"); 286109737Ssimokawa 287109737Ssimokawa isoreq.ch = ich & 0x3f; 288109737Ssimokawa isoreq.tag = (ich >> 6) & 3; 289109737Ssimokawa 290163712Simp if (ioctl(d, FW_STSTREAM, &isoreq) < 0) 291163712Simp err(1, "ioctl FW_STSTREAM"); 292109737Ssimokawa 293111112Ssimokawa iso_data = 0; 294109991Ssimokawa pkt = (struct fw_pkt *) &iso_data; 295113584Ssimokawa pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr); 296109737Ssimokawa pkt->mode.stream.sy = 0; 297109737Ssimokawa pkt->mode.stream.tcode = FWTCODE_STREAM; 298109737Ssimokawa pkt->mode.stream.chtag = ich; 299109991Ssimokawa iso_empty = iso_data; 300109991Ssimokawa pkt = (struct fw_pkt *) &iso_empty; 301113584Ssimokawa pkt->mode.stream.len = sizeof(struct ciphdr); 302109737Ssimokawa 303111112Ssimokawa bzero(hdr[0], sizeof(hdr[0])); 304109991Ssimokawa hdr[0][0] = iso_data; 305109991Ssimokawa ciph = (struct ciphdr *)&hdr[0][1]; 306111112Ssimokawa ciph->src = 0; /* XXX */ 307109737Ssimokawa ciph->len = 120; 308109737Ssimokawa ciph->dbc = 0; 309109737Ssimokawa ciph->eoh1 = 1; 310109737Ssimokawa ciph->fdf.dv.cyc = 0xffff; 311109737Ssimokawa 312163712Simp for (i = 1; i < TNBUF; i++) 313109991Ssimokawa bcopy(hdr[0], hdr[i], sizeof(hdr[0])); 314109991Ssimokawa 315109737Ssimokawa gettimeofday(&start, NULL); 316111112Ssimokawa#if DEBUG 317109787Ssimokawa fprintf(stderr, "%08x %08x %08x\n", 318109787Ssimokawa htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2])); 319109737Ssimokawa#endif 320109737Ssimokawa frames = 0; 321109737Ssimokawa packets = 0; 322109737Ssimokawa pad_acc = 0; 323109737Ssimokawa while (1) { 324109991Ssimokawa tlen = 0; 325111112Ssimokawa while (tlen < DSIZE * TNBUF) { 326111112Ssimokawa len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen); 327109737Ssimokawa if (len <= 0) { 328109991Ssimokawa if (tlen > 0) 329109991Ssimokawa break; 330109991Ssimokawa if (len < 0) 331109991Ssimokawa warn("read"); 332109991Ssimokawa else 333163712Simp fprintf(stderr, "\nend of file\n"); 334109737Ssimokawa goto send_end; 335109737Ssimokawa } 336109991Ssimokawa tlen += len; 337109737Ssimokawa } 338109991Ssimokawa vec = 0; 339109991Ssimokawa offset = 0; 340109991Ssimokawa nhdr = 0; 341109991Ssimokawanext: 342109991Ssimokawa dv = (struct dvdbc *)(pbuf + offset * DSIZE); 343109737Ssimokawa#if 0 344109737Ssimokawa header = (dv->sct == 0 && dv->dseq == 0); 345109737Ssimokawa#else 346111112Ssimokawa header = (packets == 0 || packets % npackets[system] == 0); 347109737Ssimokawa#endif 348109737Ssimokawa 349109991Ssimokawa ciph = (struct ciphdr *)&hdr[nhdr][1]; 350109737Ssimokawa if (header) { 351111112Ssimokawa if (system < 0) { 352111112Ssimokawa system = ((dv->payload[0] & DV_DSF_12) != 0); 353111112Ssimokawa printf("%s\n", system_name[system]); 354111112Ssimokawa cycle = 1; 355111112Ssimokawa cycle_acc = frame_cycle[system].d * cycle; 356111112Ssimokawa } 357109787Ssimokawa fprintf(stderr, "%d", frames % 10); 358109737Ssimokawa frames ++; 359109737Ssimokawa if (count > 0 && frames > count) 360109737Ssimokawa break; 361111112Ssimokawa if (frames % frame_rate[system] == 0) 362109787Ssimokawa fprintf(stderr, "\n"); 363109787Ssimokawa fflush(stderr); 364109737Ssimokawa f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf; 365109737Ssimokawa f_frac = (cycle_acc % frame_cycle[system].d 366109737Ssimokawa * CYCLE_FRAC) / frame_cycle[system].d; 367109737Ssimokawa#if 0 368109737Ssimokawa ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac); 369109737Ssimokawa#else 370109737Ssimokawa ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac); 371109737Ssimokawa#endif 372109737Ssimokawa cycle_acc += frame_cycle[system].n; 373109737Ssimokawa cycle_acc %= frame_cycle[system].d * 0x10; 374109737Ssimokawa 375109737Ssimokawa } else { 376109991Ssimokawa ciph->fdf.dv.cyc = 0xffff; 377109737Ssimokawa } 378109737Ssimokawa ciph->dbc = packets++ % 256; 379109737Ssimokawa pad_acc += pad_rate[system].n; 380109737Ssimokawa if (pad_acc >= pad_rate[system].d) { 381109737Ssimokawa pad_acc -= pad_rate[system].d; 382109991Ssimokawa bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0])); 383109991Ssimokawa hdr[nhdr][0] = iso_empty; 384109991Ssimokawa wbuf[vec].iov_base = (char *)hdr[nhdr]; 385109991Ssimokawa wbuf[vec++].iov_len = sizeof(hdr[0]); 386109991Ssimokawa nhdr ++; 387109737Ssimokawa cycle ++; 388109737Ssimokawa } 389109991Ssimokawa hdr[nhdr][0] = iso_data; 390109991Ssimokawa wbuf[vec].iov_base = (char *)hdr[nhdr]; 391109991Ssimokawa wbuf[vec++].iov_len = sizeof(hdr[0]); 392109991Ssimokawa wbuf[vec].iov_base = (char *)dv; 393109991Ssimokawa wbuf[vec++].iov_len = DSIZE; 394109991Ssimokawa nhdr ++; 395109991Ssimokawa cycle ++; 396109991Ssimokawa offset ++; 397109991Ssimokawa if (offset * DSIZE < tlen) 398109991Ssimokawa goto next; 399109737Ssimokawa 400109991Ssimokawaagain: 401109991Ssimokawa len = writev(d, wbuf, vec); 402109737Ssimokawa if (len < 0) { 403109737Ssimokawa if (errno == EAGAIN) { 404163712Simp fprintf(stderr, "(EAGAIN) - push 'Play'?\n"); 405109991Ssimokawa goto again; 406109737Ssimokawa } 407109737Ssimokawa err(1, "write failed"); 408109737Ssimokawa } 409109737Ssimokawa } 410109737Ssimokawa close(fd); 411109787Ssimokawa fprintf(stderr, "\n"); 412109737Ssimokawasend_end: 413109737Ssimokawa gettimeofday(&end, NULL); 414163712Simp rtime = end.tv_sec - start.tv_sec 415109737Ssimokawa + (end.tv_usec - start.tv_usec) * 1e-6; 416109787Ssimokawa fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n", 417109737Ssimokawa frames, rtime, frames/rtime); 418109737Ssimokawa} 419