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