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