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