xref: /freebsd/usr.sbin/fwcontrol/fwmpegts.c (revision 3df5ecac8c5e0a9105ba0e783bd9e7077df944e7)
16d815a7dSWarner Losh /*
26d815a7dSWarner Losh  * Copyright (C) 2005
36d815a7dSWarner Losh  * 	Petr Holub, Hidetoshi Shimokawa. All rights reserved.
46d815a7dSWarner Losh  *
56d815a7dSWarner Losh  * Redistribution and use in source and binary forms, with or without
66d815a7dSWarner Losh  * modification, are permitted provided that the following conditions
76d815a7dSWarner Losh  * are met:
86d815a7dSWarner Losh  * 1. Redistributions of source code must retain the above copyright
96d815a7dSWarner Losh  *    notice, this list of conditions and the following disclaimer.
106d815a7dSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
116d815a7dSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
126d815a7dSWarner Losh  *    documentation and/or other materials provided with the distribution.
136d815a7dSWarner Losh  * 3. All advertising materials mentioning features or use of this software
146d815a7dSWarner Losh  *    must display the following acknowledgement:
156d815a7dSWarner Losh  *
166d815a7dSWarner Losh  *	This product includes software developed by Hidetoshi Shimokawa.
176d815a7dSWarner Losh  *
186d815a7dSWarner Losh  * 4. Neither the name of the author nor the names of its contributors
196d815a7dSWarner Losh  *    may be used to endorse or promote products derived from this software
206d815a7dSWarner Losh  *    without specific prior written permission.
216d815a7dSWarner Losh  *
226d815a7dSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
236d815a7dSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
246d815a7dSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
256d815a7dSWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
266d815a7dSWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
276d815a7dSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
286d815a7dSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
296d815a7dSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
306d815a7dSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
316d815a7dSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
326d815a7dSWarner Losh  * SUCH DAMAGE.
336d815a7dSWarner Losh  *
346d815a7dSWarner Losh  * $FreeBSD$
356d815a7dSWarner Losh  */
366d815a7dSWarner Losh #include <sys/param.h>
376d815a7dSWarner Losh #include <sys/ioctl.h>
386d815a7dSWarner Losh #include <sys/time.h>
396d815a7dSWarner Losh #include <sys/types.h>
406d815a7dSWarner Losh #include <sys/uio.h>
416d815a7dSWarner Losh 
426d815a7dSWarner Losh #if __FreeBSD_version >= 500000
436d815a7dSWarner Losh #include <arpa/inet.h>
446d815a7dSWarner Losh #endif
456d815a7dSWarner Losh 
466d815a7dSWarner Losh #include <err.h>
476d815a7dSWarner Losh #include <errno.h>
486d815a7dSWarner Losh #include <unistd.h>
496d815a7dSWarner Losh #include <fcntl.h>
506d815a7dSWarner Losh #include <stdio.h>
516d815a7dSWarner Losh #include <stdlib.h>
526d815a7dSWarner Losh #include <string.h>
536d815a7dSWarner Losh #include <sysexits.h>
546d815a7dSWarner Losh 
55c1de48a3SSean Bruno #if defined(__FreeBSD__)
566d815a7dSWarner Losh #include <dev/firewire/firewire.h>
576d815a7dSWarner Losh #include <dev/firewire/iec68113.h>
58c1de48a3SSean Bruno #elif defined(__NetBSD__)
59c1de48a3SSean Bruno #include <dev/ieee1394/firewire.h>
60c1de48a3SSean Bruno #include <dev/ieee1394/iec68113.h>
61c1de48a3SSean Bruno #else
62c1de48a3SSean Bruno #warning "You need to add support for your OS"
63c1de48a3SSean Bruno #endif
64c1de48a3SSean Bruno 
656d815a7dSWarner Losh 
666d815a7dSWarner Losh #include "fwmethods.h"
676d815a7dSWarner Losh 
686d815a7dSWarner Losh #define	DEBUG 0
696d815a7dSWarner Losh 
706d815a7dSWarner Losh /*****************************************************************************
716d815a7dSWarner Losh 
726d815a7dSWarner Losh MPEG-2 Transport Stream (MPEG TS) packet format according to IEC 61883:
736d815a7dSWarner Losh 
746d815a7dSWarner Losh 31                              15                             0
756d815a7dSWarner Losh +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  --------
766d815a7dSWarner Losh |           len                 |tag|  channel  | tcode |  sy   |
776d815a7dSWarner Losh +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    1394
786d815a7dSWarner Losh |                           header_CRC                          |
796d815a7dSWarner Losh +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  --------
806d815a7dSWarner Losh |0|0|    sid    |      dbs      |fn | qpc |S|RSV|       dbc     |
816d815a7dSWarner Losh +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    CIP
826d815a7dSWarner Losh |1|0|    fmt    |      fdf      |          fdf/syt              |
836d815a7dSWarner Losh +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  --------
846d815a7dSWarner Losh |   reserved  |        cycle_count      |      cycle_offset     |
856d815a7dSWarner Losh +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
866d815a7dSWarner Losh |                                                               |    N x
876d815a7dSWarner Losh .                                                               .    MPEG
886d815a7dSWarner Losh .                   MPEG TS payload 188 bytes                   .
896d815a7dSWarner Losh .                                                               .
906d815a7dSWarner Losh |                                                               |
916d815a7dSWarner Losh +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  --------
926d815a7dSWarner Losh |                            data_CRC                           |
936d815a7dSWarner Losh +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
946d815a7dSWarner Losh 
956d815a7dSWarner Losh N.b. that CRCs are removed by firewire layer!
966d815a7dSWarner Losh 
97*3df5ecacSUlrich Spörlein The following fields are fixed for IEEE-1394:
986d815a7dSWarner Losh tag = 01b
996d815a7dSWarner Losh tcode = 1010b
1006d815a7dSWarner Losh The length is payload length, i.e. includes CIP header and data size.
1016d815a7dSWarner Losh 
1026d815a7dSWarner Losh The following fields are constant for MPEG TS:
1036d815a7dSWarner Losh sph = 1 (denoted as S in CIP header above)
1046d815a7dSWarner Losh dbs = 6
1056d815a7dSWarner Losh fmt = (1<<5)
1066d815a7dSWarner Losh fdf = reserved
1076d815a7dSWarner Losh In the supported streams we also require
1086d815a7dSWarner Losh qpc = 0
1096d815a7dSWarner Losh fn = 3
1106d815a7dSWarner Losh and thus the payload is divided in 8 blocks as follows:
1116d815a7dSWarner Losh 
1126d815a7dSWarner Losh   +-----+-----+-----+-----+-----+-----+-----+-----+
1136d815a7dSWarner Losh   | db0 | db1 | db2 | db3 | db4 | db5 | db6 | db7 |
1146d815a7dSWarner Losh   +-----+-----+-----+-----+-----+-----+-----+-----+
1156d815a7dSWarner Losh 
1166d815a7dSWarner Losh We have several cases of payload distribution based on stream
1176d815a7dSWarner Losh bandwidth (R):
1186d815a7dSWarner Losh 1) R < 1.5 Mbps: any of db0..db7 may be payload,
1196d815a7dSWarner Losh 2) 1.5 < R < 3 Mbps: db0/db1 or db2/db3 or db4/db5 or db6/db7 is payload,
1206d815a7dSWarner Losh 3) 3 < R < 6 Mbps: db0/db1/db2/db3 or db4/db5/db6/db7 is payload,
1216d815a7dSWarner Losh 4) R > 6 Mbps: all db0..db7 contain the payload.
1226d815a7dSWarner Losh Curently, only case (4) is supported in fwmpegts.c
1236d815a7dSWarner Losh 
1246d815a7dSWarner Losh Each packet may contain N  MPEG TS data blocks with timestamp header,
1256d815a7dSWarner Losh which are (4+188)B long. Experimentally, the N ranges from 0 through 3.
1266d815a7dSWarner Losh 
1276d815a7dSWarner Losh *****************************************************************************/
1286d815a7dSWarner Losh 
1296d815a7dSWarner Losh 
1306d815a7dSWarner Losh typedef uint8_t mpeg_ts_pld[188];
1316d815a7dSWarner Losh 
1326d815a7dSWarner Losh struct mpeg_pldt {
1336d815a7dSWarner Losh #if BYTE_ORDER == BIG_ENDIAN
1346d815a7dSWarner Losh 	uint32_t	:7,
1356d815a7dSWarner Losh 				c_count:13,
1366d815a7dSWarner Losh 				c_offset:12;
1376d815a7dSWarner Losh #else /* BYTE_ORDER != BIG_ENDIAN */
1386d815a7dSWarner Losh 	uint32_t	c_offset:12,
1396d815a7dSWarner Losh 				c_count:13,
1406d815a7dSWarner Losh 				:7;
1416d815a7dSWarner Losh #endif /* BYTE_ORDER == BIG_ENDIAN */
1426d815a7dSWarner Losh 	mpeg_ts_pld payload;
1436d815a7dSWarner Losh };
1446d815a7dSWarner Losh 
1456d815a7dSWarner Losh 
1466d815a7dSWarner Losh #define	NCHUNK 8
1476d815a7dSWarner Losh #define	PSIZE 596
1486d815a7dSWarner Losh #define	NPACKET_R 4096
1496d815a7dSWarner Losh #define	RBUFSIZE (PSIZE * NPACKET_R)
1506d815a7dSWarner Losh 
1516d815a7dSWarner Losh void
1526d815a7dSWarner Losh mpegtsrecv(int d, const char *filename, char ich, int count)
1536d815a7dSWarner Losh {
1546d815a7dSWarner Losh 	struct ciphdr *ciph;
1556d815a7dSWarner Losh 	struct fw_isochreq isoreq;
1566d815a7dSWarner Losh 	struct fw_isobufreq bufreq;
1576d815a7dSWarner Losh 	struct fw_pkt *pkt;
1586d815a7dSWarner Losh 	struct mpeg_pldt *pld;
1596d815a7dSWarner Losh 	uint32_t *ptr;
1606d815a7dSWarner Losh 	int fd, k, len, m, pkt_size, startwr, tlen;
1616d815a7dSWarner Losh 	char *buf;
1626d815a7dSWarner Losh 
1636d815a7dSWarner Losh 	startwr = 0;
1646d815a7dSWarner Losh 
1656d815a7dSWarner Losh 	if (strcmp(filename, "-") == 0)
1666d815a7dSWarner Losh 		fd = STDOUT_FILENO;
1676d815a7dSWarner Losh 	else {
1686d815a7dSWarner Losh 		fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
1696d815a7dSWarner Losh 		if (fd == -1)
1700737626aSEd Maste 			err(EX_NOINPUT, "%s", filename);
1716d815a7dSWarner Losh 	}
1726d815a7dSWarner Losh 	buf = malloc(RBUFSIZE);
1736d815a7dSWarner Losh 
1746d815a7dSWarner Losh 	bufreq.rx.nchunk = NCHUNK;
1756d815a7dSWarner Losh 	bufreq.rx.npacket = NPACKET_R;
1766d815a7dSWarner Losh 	bufreq.rx.psize = PSIZE;
1776d815a7dSWarner Losh 	bufreq.tx.nchunk = 0;
1786d815a7dSWarner Losh 	bufreq.tx.npacket = 0;
1796d815a7dSWarner Losh 	bufreq.tx.psize = 0;
1806d815a7dSWarner Losh 	if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
1816d815a7dSWarner Losh 		err(1, "ioctl");
1826d815a7dSWarner Losh 
1836d815a7dSWarner Losh 	isoreq.ch = ich & 0x3f;
1846d815a7dSWarner Losh 	isoreq.tag = (ich >> 6) & 3;
1856d815a7dSWarner Losh 
1866d815a7dSWarner Losh 	if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
1876d815a7dSWarner Losh 		err(1, "ioctl");
1886d815a7dSWarner Losh 
1896d815a7dSWarner Losh 	k = m = 0;
1906d815a7dSWarner Losh 	while (count <= 0 || k <= count) {
1916d815a7dSWarner Losh 		len = tlen = read(d, buf, RBUFSIZE);
1926d815a7dSWarner Losh #if DEBUG
1936d815a7dSWarner Losh 		fprintf(stderr, "Read %d bytes.\n", len);
1946d815a7dSWarner Losh #endif /* DEBUG */
1956d815a7dSWarner Losh 		if (len < 0) {
1966d815a7dSWarner Losh 			if (errno == EAGAIN) {
1976d815a7dSWarner Losh 				fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
1986d815a7dSWarner Losh 				continue;
199e627b033SSean Bruno 			}
2006d815a7dSWarner Losh 			err(1, "read failed");
2016d815a7dSWarner Losh 		}
2026d815a7dSWarner Losh 		ptr = (uint32_t *) buf;
2036d815a7dSWarner Losh 
2046d815a7dSWarner Losh 		do {
2056d815a7dSWarner Losh 			pkt = (struct fw_pkt *) ptr;
2066d815a7dSWarner Losh #if DEBUG
2076d815a7dSWarner Losh 			fprintf(stderr, "\nReading new packet.\n");
2086d815a7dSWarner Losh 			fprintf(stderr, "%08x %08x %08x %08x\n",
2096d815a7dSWarner Losh 				htonl(ptr[0]), htonl(ptr[1]),
2106d815a7dSWarner Losh 				htonl(ptr[2]), htonl(ptr[3]));
2116d815a7dSWarner Losh #endif /* DEBUG */
2126d815a7dSWarner Losh 			/* there is no CRC in the 1394 header */
2136d815a7dSWarner Losh 			ciph = (struct ciphdr *)(ptr + 1);	/* skip iso header */
2146d815a7dSWarner Losh 			if (ciph->fmt != CIP_FMT_MPEG)
2156d815a7dSWarner Losh 				errx(1, "unknown format 0x%x", ciph->fmt);
2166d815a7dSWarner Losh 			if (ciph->fn != 3) {
2176d815a7dSWarner Losh 				errx(1,
2186d815a7dSWarner Losh 						"unsupported MPEG TS stream, fn=%d (only fn=3 is supported)",
2196d815a7dSWarner Losh 						ciph->fn);
2206d815a7dSWarner Losh 			}
2216d815a7dSWarner Losh 			ptr = (uint32_t *) (ciph + 1);		/* skip cip header */
2226d815a7dSWarner Losh 
2236d815a7dSWarner Losh 			if (pkt->mode.stream.len <= sizeof(struct ciphdr)) {
2246d815a7dSWarner Losh 				/* no payload */
2256d815a7dSWarner Losh 				/* tlen needs to be decremented before end of the loop */
2266d815a7dSWarner Losh 				goto next;
2276d815a7dSWarner Losh 			}
2286d815a7dSWarner Losh #if DEBUG
2296d815a7dSWarner Losh 			else {
2306d815a7dSWarner Losh 				fprintf(stderr,
2316d815a7dSWarner Losh 						"Packet net payload length (IEEE1394 header): %d\n",
2326d815a7dSWarner Losh 						pkt->mode.stream.len - sizeof(struct ciphdr));
2336d815a7dSWarner Losh 				fprintf(stderr, "Data block size (CIP header): %d [q], %d [B]\n",
2346d815a7dSWarner Losh 						ciph->len, ciph->len * 4);
2356d815a7dSWarner Losh 				fprintf(stderr,
2366d815a7dSWarner Losh 						"Data fraction number (CIP header): %d => DBC increments with %d\n",
2376d815a7dSWarner Losh 						ciph->fn, (1<<ciph->fn) );
2386d815a7dSWarner Losh 				fprintf(stderr, "QCP (CIP header): %d\n", ciph->qpc );
2396d815a7dSWarner Losh 				fprintf(stderr, "DBC counter (CIP header): %d\n", ciph->dbc );
2406d815a7dSWarner Losh 				fprintf(stderr, "MPEG payload type size: %d\n",
2416d815a7dSWarner Losh 						sizeof(struct mpeg_pldt));
2426d815a7dSWarner Losh 			}
2436d815a7dSWarner Losh #endif /* DEBUG */
2446d815a7dSWarner Losh 
2456d815a7dSWarner Losh 			/* This is a condition that needs to be satisfied to start
2466d815a7dSWarner Losh 			   writing the data */
2476d815a7dSWarner Losh 			if (ciph->dbc % (1<<ciph->fn) == 0)
2486d815a7dSWarner Losh 				startwr = 1;
2496d815a7dSWarner Losh 			/* Read out all the MPEG TS data blocks from current packet */
2506d815a7dSWarner Losh 			for (pld = (struct mpeg_pldt *)ptr;
2516d815a7dSWarner Losh 			    (intptr_t)pld < (intptr_t)((char *)ptr +
2526d815a7dSWarner Losh 			    pkt->mode.stream.len - sizeof(struct ciphdr));
2536d815a7dSWarner Losh 			    pld++) {
2546d815a7dSWarner Losh 				if (startwr == 1)
2556d815a7dSWarner Losh 					write(fd, pld->payload,
2566d815a7dSWarner Losh 					    sizeof(pld->payload));
2576d815a7dSWarner Losh 			}
2586d815a7dSWarner Losh 
2596d815a7dSWarner Losh next:
2606d815a7dSWarner Losh 			/* CRCs are removed from both header and trailer
2616d815a7dSWarner Losh 			so that only 4 bytes of 1394 header remains */
2626d815a7dSWarner Losh 			pkt_size = pkt->mode.stream.len + 4;
2636d815a7dSWarner Losh 			ptr = (uint32_t *)((intptr_t)pkt + pkt_size);
2646d815a7dSWarner Losh 			tlen -= pkt_size;
2656d815a7dSWarner Losh 		} while (tlen > 0);
2666d815a7dSWarner Losh #if DEBUG
2676d815a7dSWarner Losh 		fprintf(stderr, "\nReading a data from firewire.\n");
2686d815a7dSWarner Losh #endif /* DEBUG */
2696d815a7dSWarner Losh 
2706d815a7dSWarner Losh 	}
2716d815a7dSWarner Losh 	if (fd != STDOUT_FILENO)
2726d815a7dSWarner Losh 		close(fd);
2736d815a7dSWarner Losh 	fprintf(stderr, "\n");
2746d815a7dSWarner Losh }
275