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