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