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