Home | History | Annotate | Line # | Download | only in fwctl
      1  1.5    andvar /* $NetBSD: fwmpegts.c,v 1.5 2022/05/28 10:36:24 andvar Exp $ */
      2  1.1  kiyohara /*
      3  1.1  kiyohara  * Copyright (C) 2005
      4  1.1  kiyohara  * 	Petr Holub, Hidetoshi Shimokawa. All rights reserved.
      5  1.1  kiyohara  *
      6  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7  1.1  kiyohara  * modification, are permitted provided that the following conditions
      8  1.1  kiyohara  * are met:
      9  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14  1.1  kiyohara  * 3. All advertising materials mentioning features or use of this software
     15  1.1  kiyohara  *    must display the following acknowledgement:
     16  1.1  kiyohara  *
     17  1.1  kiyohara  *	This product includes software developed by Hidetoshi Shimokawa.
     18  1.1  kiyohara  *
     19  1.1  kiyohara  * 4. Neither the name of the author nor the names of its contributors
     20  1.1  kiyohara  *    may be used to endorse or promote products derived from this software
     21  1.1  kiyohara  *    without specific prior written permission.
     22  1.1  kiyohara  *
     23  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  kiyohara  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  kiyohara  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  kiyohara  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  kiyohara  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  kiyohara  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  kiyohara  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  kiyohara  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  kiyohara  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  kiyohara  * SUCH DAMAGE.
     34  1.1  kiyohara  *
     35  1.2    cegger  * $FreeBSD: src/usr.sbin/fwcontrol/fwmpegts.c,v 1.3 2009/02/02 21:05:12 sbruno Exp $
     36  1.1  kiyohara  */
     37  1.1  kiyohara #include <sys/param.h>
     38  1.1  kiyohara #include <sys/ioctl.h>
     39  1.1  kiyohara #include <sys/time.h>
     40  1.1  kiyohara #include <sys/types.h>
     41  1.1  kiyohara #include <sys/uio.h>
     42  1.1  kiyohara 
     43  1.1  kiyohara #include <err.h>
     44  1.1  kiyohara #include <errno.h>
     45  1.1  kiyohara #include <unistd.h>
     46  1.1  kiyohara #include <fcntl.h>
     47  1.1  kiyohara #include <stdio.h>
     48  1.1  kiyohara #include <stdlib.h>
     49  1.1  kiyohara #include <string.h>
     50  1.1  kiyohara #include <sysexits.h>
     51  1.1  kiyohara 
     52  1.1  kiyohara #include <dev/ieee1394/firewire.h>
     53  1.1  kiyohara #include <dev/ieee1394/iec68113.h>
     54  1.1  kiyohara 
     55  1.1  kiyohara #include "fwmethods.h"
     56  1.1  kiyohara 
     57  1.1  kiyohara #define	DEBUG 0
     58  1.1  kiyohara 
     59  1.1  kiyohara /*****************************************************************************
     60  1.1  kiyohara 
     61  1.1  kiyohara MPEG-2 Transport Stream (MPEG TS) packet format according to IEC 61883:
     62  1.1  kiyohara 
     63  1.1  kiyohara 31                              15                             0
     64  1.1  kiyohara +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  --------
     65  1.1  kiyohara |           len                 |tag|  channel  | tcode |  sy   |
     66  1.1  kiyohara +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    1394
     67  1.1  kiyohara |                           header_CRC                          |
     68  1.1  kiyohara +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  --------
     69  1.1  kiyohara |0|0|    sid    |      dbs      |fn | qpc |S|RSV|       dbc     |
     70  1.1  kiyohara +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    CIP
     71  1.1  kiyohara |1|0|    fmt    |      fdf      |          fdf/syt              |
     72  1.1  kiyohara +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  --------
     73  1.1  kiyohara |   reserved  |        cycle_count      |      cycle_offset     |
     74  1.1  kiyohara +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     75  1.1  kiyohara |                                                               |    N x
     76  1.1  kiyohara .                                                               .    MPEG
     77  1.1  kiyohara .                   MPEG TS payload 188 bytes                   .
     78  1.1  kiyohara .                                                               .
     79  1.1  kiyohara |                                                               |
     80  1.1  kiyohara +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  --------
     81  1.1  kiyohara |                            data_CRC                           |
     82  1.1  kiyohara +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     83  1.1  kiyohara 
     84  1.1  kiyohara N.b. that CRCs are removed by firewire layer!
     85  1.1  kiyohara 
     86  1.5    andvar The following fields are fixed for IEEE-1394:
     87  1.1  kiyohara tag = 01b
     88  1.1  kiyohara tcode = 1010b
     89  1.1  kiyohara The length is payload length, i.e. includes CIP header and data size.
     90  1.1  kiyohara 
     91  1.1  kiyohara The following fields are constant for MPEG TS:
     92  1.1  kiyohara sph = 1 (denoted as S in CIP header above)
     93  1.1  kiyohara dbs = 6
     94  1.1  kiyohara fmt = (1<<5)
     95  1.1  kiyohara fdf = reserved
     96  1.1  kiyohara In the supported streams we also require
     97  1.1  kiyohara qpc = 0
     98  1.1  kiyohara fn = 3
     99  1.1  kiyohara and thus the payload is divided in 8 blocks as follows:
    100  1.1  kiyohara 
    101  1.1  kiyohara   +-----+-----+-----+-----+-----+-----+-----+-----+
    102  1.1  kiyohara   | db0 | db1 | db2 | db3 | db4 | db5 | db6 | db7 |
    103  1.1  kiyohara   +-----+-----+-----+-----+-----+-----+-----+-----+
    104  1.1  kiyohara 
    105  1.1  kiyohara We have several cases of payload distribution based on stream
    106  1.1  kiyohara bandwidth (R):
    107  1.1  kiyohara 1) R < 1.5 Mbps: any of db0..db7 may be payload,
    108  1.1  kiyohara 2) 1.5 < R < 3 Mbps: db0/db1 or db2/db3 or db4/db5 or db6/db7 is payload,
    109  1.1  kiyohara 3) 3 < R < 6 Mbps: db0/db1/db2/db3 or db4/db5/db6/db7 is payload,
    110  1.1  kiyohara 4) R > 6 Mbps: all db0..db7 contain the payload.
    111  1.5    andvar Currently, only case (4) is supported in fwmpegts.c
    112  1.1  kiyohara 
    113  1.1  kiyohara Each packet may contain N  MPEG TS data blocks with timestamp header,
    114  1.1  kiyohara which are (4+188)B long. Experimentally, the N ranges from 0 through 3.
    115  1.1  kiyohara 
    116  1.1  kiyohara *****************************************************************************/
    117  1.1  kiyohara 
    118  1.1  kiyohara 
    119  1.1  kiyohara typedef uint8_t mpeg_ts_pld[188];
    120  1.1  kiyohara 
    121  1.1  kiyohara struct mpeg_pldt {
    122  1.1  kiyohara #if BYTE_ORDER == BIG_ENDIAN
    123  1.1  kiyohara 	uint32_t	:7,
    124  1.1  kiyohara 				c_count:13,
    125  1.1  kiyohara 				c_offset:12;
    126  1.1  kiyohara #else /* BYTE_ORDER != BIG_ENDIAN */
    127  1.1  kiyohara 	uint32_t	c_offset:12,
    128  1.1  kiyohara 				c_count:13,
    129  1.1  kiyohara 				:7;
    130  1.1  kiyohara #endif /* BYTE_ORDER == BIG_ENDIAN */
    131  1.1  kiyohara 	mpeg_ts_pld payload;
    132  1.1  kiyohara };
    133  1.1  kiyohara 
    134  1.1  kiyohara 
    135  1.1  kiyohara #define	NCHUNK 8
    136  1.1  kiyohara #define	PSIZE 596
    137  1.1  kiyohara #define	NPACKET_R 4096
    138  1.1  kiyohara #define	RBUFSIZE (PSIZE * NPACKET_R)
    139  1.1  kiyohara 
    140  1.1  kiyohara void
    141  1.1  kiyohara mpegtsrecv(int d, const char *filename, char ich, int count)
    142  1.1  kiyohara {
    143  1.1  kiyohara 	struct ciphdr *ciph;
    144  1.1  kiyohara 	struct fw_isochreq isoreq;
    145  1.1  kiyohara 	struct fw_isobufreq bufreq;
    146  1.1  kiyohara 	struct fw_pkt *pkt;
    147  1.1  kiyohara 	struct mpeg_pldt *pld;
    148  1.1  kiyohara 	uint32_t *ptr;
    149  1.1  kiyohara 	int fd, k, len, m, pkt_size, startwr, tlen;
    150  1.1  kiyohara 	char *buf;
    151  1.1  kiyohara 
    152  1.1  kiyohara 	startwr = 0;
    153  1.1  kiyohara 
    154  1.1  kiyohara 	if (strcmp(filename, "-") == 0)
    155  1.1  kiyohara 		fd = STDOUT_FILENO;
    156  1.1  kiyohara 	else {
    157  1.1  kiyohara 		fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
    158  1.1  kiyohara 		if (fd == -1)
    159  1.4  christos 			err(EX_NOINPUT, "%s: %s", __func__, filename);
    160  1.1  kiyohara 	}
    161  1.1  kiyohara 	buf = malloc(RBUFSIZE);
    162  1.1  kiyohara 
    163  1.1  kiyohara 	bufreq.rx.nchunk = NCHUNK;
    164  1.1  kiyohara 	bufreq.rx.npacket = NPACKET_R;
    165  1.1  kiyohara 	bufreq.rx.psize = PSIZE;
    166  1.1  kiyohara 	bufreq.tx.nchunk = 0;
    167  1.1  kiyohara 	bufreq.tx.npacket = 0;
    168  1.1  kiyohara 	bufreq.tx.psize = 0;
    169  1.1  kiyohara 	if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
    170  1.4  christos 		err(EXIT_FAILURE, "%s: ioctl", __func__);
    171  1.1  kiyohara 
    172  1.1  kiyohara 	isoreq.ch = ich & 0x3f;
    173  1.1  kiyohara 	isoreq.tag = (ich >> 6) & 3;
    174  1.1  kiyohara 
    175  1.1  kiyohara 	if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
    176  1.4  christos 		err(EXIT_FAILURE, "%s: ioctl", __func__);
    177  1.1  kiyohara 
    178  1.1  kiyohara 	k = m = 0;
    179  1.1  kiyohara 	while (count <= 0 || k <= count) {
    180  1.1  kiyohara 		len = tlen = read(d, buf, RBUFSIZE);
    181  1.1  kiyohara #if DEBUG
    182  1.1  kiyohara 		fprintf(stderr, "Read %d bytes.\n", len);
    183  1.1  kiyohara #endif /* DEBUG */
    184  1.1  kiyohara 		if (len < 0) {
    185  1.1  kiyohara 			if (errno == EAGAIN) {
    186  1.1  kiyohara 				fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
    187  1.2    cegger 				continue;
    188  1.2    cegger 			}
    189  1.2    cegger 			err(EXIT_FAILURE, "read failed");
    190  1.1  kiyohara 		}
    191  1.1  kiyohara 		ptr = (uint32_t *) buf;
    192  1.1  kiyohara 
    193  1.1  kiyohara 		do {
    194  1.1  kiyohara 			pkt = (struct fw_pkt *) ptr;
    195  1.1  kiyohara #if DEBUG
    196  1.1  kiyohara 			fprintf(stderr, "\nReading new packet.\n");
    197  1.1  kiyohara 			fprintf(stderr, "%08x %08x %08x %08x\n",
    198  1.1  kiyohara 				htonl(ptr[0]), htonl(ptr[1]),
    199  1.1  kiyohara 				htonl(ptr[2]), htonl(ptr[3]));
    200  1.1  kiyohara #endif /* DEBUG */
    201  1.1  kiyohara 			/* there is no CRC in the 1394 header */
    202  1.1  kiyohara 			ciph = (struct ciphdr *)(ptr + 1);	/* skip iso header */
    203  1.1  kiyohara 			if (ciph->fmt != CIP_FMT_MPEG)
    204  1.4  christos 				errx(EXIT_FAILURE,
    205  1.4  christos 				    "%s: unknown format 0x%x", __func__,
    206  1.4  christos 				    ciph->fmt);
    207  1.1  kiyohara 			if (ciph->fn != 3) {
    208  1.4  christos 				errx(EXIT_FAILURE,
    209  1.4  christos 				    "%s: unsupported MPEG TS stream, "
    210  1.4  christos 				    "fn=%d (only fn=3 is supported)",
    211  1.4  christos 				    __func__, ciph->fn);
    212  1.1  kiyohara 			}
    213  1.1  kiyohara 			ptr = (uint32_t *) (ciph + 1);		/* skip cip header */
    214  1.1  kiyohara 
    215  1.1  kiyohara 			if (pkt->mode.stream.len <= sizeof(struct ciphdr)) {
    216  1.1  kiyohara 				/* no payload */
    217  1.1  kiyohara 				/* tlen needs to be decremented before end of the loop */
    218  1.1  kiyohara 				goto next;
    219  1.1  kiyohara 			}
    220  1.1  kiyohara #if DEBUG
    221  1.1  kiyohara 			else {
    222  1.1  kiyohara 				fprintf(stderr,
    223  1.1  kiyohara 						"Packet net payload length (IEEE1394 header): %d\n",
    224  1.1  kiyohara 						pkt->mode.stream.len - sizeof(struct ciphdr));
    225  1.1  kiyohara 				fprintf(stderr, "Data block size (CIP header): %d [q], %d [B]\n",
    226  1.1  kiyohara 						ciph->len, ciph->len * 4);
    227  1.1  kiyohara 				fprintf(stderr,
    228  1.1  kiyohara 						"Data fraction number (CIP header): %d => DBC increments with %d\n",
    229  1.1  kiyohara 						ciph->fn, (1<<ciph->fn) );
    230  1.1  kiyohara 				fprintf(stderr, "QCP (CIP header): %d\n", ciph->qpc );
    231  1.1  kiyohara 				fprintf(stderr, "DBC counter (CIP header): %d\n", ciph->dbc );
    232  1.1  kiyohara 				fprintf(stderr, "MPEG payload type size: %d\n",
    233  1.1  kiyohara 						sizeof(struct mpeg_pldt));
    234  1.1  kiyohara 			}
    235  1.1  kiyohara #endif /* DEBUG */
    236  1.1  kiyohara 
    237  1.1  kiyohara 			/* This is a condition that needs to be satisfied to start
    238  1.1  kiyohara 			   writing the data */
    239  1.1  kiyohara 			if (ciph->dbc % (1<<ciph->fn) == 0)
    240  1.1  kiyohara 				startwr = 1;
    241  1.1  kiyohara 			/* Read out all the MPEG TS data blocks from current packet */
    242  1.1  kiyohara 			for (pld = (struct mpeg_pldt *)ptr;
    243  1.1  kiyohara 			    (intptr_t)pld < (intptr_t)((char *)ptr +
    244  1.1  kiyohara 			    pkt->mode.stream.len - sizeof(struct ciphdr));
    245  1.1  kiyohara 			    pld++) {
    246  1.1  kiyohara 				if (startwr == 1)
    247  1.1  kiyohara 					write(fd, pld->payload,
    248  1.1  kiyohara 					    sizeof(pld->payload));
    249  1.1  kiyohara 			}
    250  1.1  kiyohara 
    251  1.1  kiyohara next:
    252  1.1  kiyohara 			/* CRCs are removed from both header and trailer
    253  1.1  kiyohara 			so that only 4 bytes of 1394 header remains */
    254  1.1  kiyohara 			pkt_size = pkt->mode.stream.len + 4;
    255  1.1  kiyohara 			ptr = (uint32_t *)((intptr_t)pkt + pkt_size);
    256  1.1  kiyohara 			tlen -= pkt_size;
    257  1.1  kiyohara 		} while (tlen > 0);
    258  1.1  kiyohara #if DEBUG
    259  1.1  kiyohara 		fprintf(stderr, "\nReading a data from firewire.\n");
    260  1.1  kiyohara #endif /* DEBUG */
    261  1.1  kiyohara 
    262  1.1  kiyohara 	}
    263  1.1  kiyohara 	if (fd != STDOUT_FILENO)
    264  1.1  kiyohara 		close(fd);
    265  1.1  kiyohara 	fprintf(stderr, "\n");
    266  1.1  kiyohara }
    267