fwmpegts.c revision 163712
1163712Simp/* 2163712Simp * Copyright (C) 2005 3163712Simp * Petr Holub, Hidetoshi Shimokawa. All rights reserved. 4163712Simp * 5163712Simp * Redistribution and use in source and binary forms, with or without 6163712Simp * modification, are permitted provided that the following conditions 7163712Simp * are met: 8163712Simp * 1. Redistributions of source code must retain the above copyright 9163712Simp * notice, this list of conditions and the following disclaimer. 10163712Simp * 2. Redistributions in binary form must reproduce the above copyright 11163712Simp * notice, this list of conditions and the following disclaimer in the 12163712Simp * documentation and/or other materials provided with the distribution. 13163712Simp * 3. All advertising materials mentioning features or use of this software 14163712Simp * must display the following acknowledgement: 15163712Simp * 16163712Simp * This product includes software developed by Hidetoshi Shimokawa. 17163712Simp * 18163712Simp * 4. Neither the name of the author nor the names of its contributors 19163712Simp * may be used to endorse or promote products derived from this software 20163712Simp * without specific prior written permission. 21163712Simp * 22163712Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23163712Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24163712Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25163712Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26163712Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27163712Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28163712Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29163712Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30163712Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31163712Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32163712Simp * SUCH DAMAGE. 33163712Simp * 34163712Simp * $FreeBSD: head/usr.sbin/fwcontrol/fwmpegts.c 163712 2006-10-26 22:33:38Z imp $ 35163712Simp */ 36163712Simp#include <sys/param.h> 37163712Simp#include <sys/ioctl.h> 38163712Simp#include <sys/time.h> 39163712Simp#include <sys/types.h> 40163712Simp#include <sys/uio.h> 41163712Simp 42163712Simp#if __FreeBSD_version >= 500000 43163712Simp#include <arpa/inet.h> 44163712Simp#endif 45163712Simp 46163712Simp#include <err.h> 47163712Simp#include <errno.h> 48163712Simp#include <unistd.h> 49163712Simp#include <fcntl.h> 50163712Simp#include <stdio.h> 51163712Simp#include <stdlib.h> 52163712Simp#include <string.h> 53163712Simp#include <sysexits.h> 54163712Simp 55163712Simp#include <dev/firewire/firewire.h> 56163712Simp#include <dev/firewire/iec68113.h> 57163712Simp 58163712Simp#include "fwmethods.h" 59163712Simp 60163712Simp#define DEBUG 0 61163712Simp 62163712Simp/***************************************************************************** 63163712Simp 64163712SimpMPEG-2 Transport Stream (MPEG TS) packet format according to IEC 61883: 65163712Simp 66163712Simp31 15 0 67163712Simp+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -------- 68163712Simp| len |tag| channel | tcode | sy | 69163712Simp+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 1394 70163712Simp| header_CRC | 71163712Simp+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -------- 72163712Simp|0|0| sid | dbs |fn | qpc |S|RSV| dbc | 73163712Simp+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ CIP 74163712Simp|1|0| fmt | fdf | fdf/syt | 75163712Simp+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -------- 76163712Simp| reserved | cycle_count | cycle_offset | 77163712Simp+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 78163712Simp| | N x 79163712Simp. . MPEG 80163712Simp. MPEG TS payload 188 bytes . 81163712Simp. . 82163712Simp| | 83163712Simp+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -------- 84163712Simp| data_CRC | 85163712Simp+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 86163712Simp 87163712SimpN.b. that CRCs are removed by firewire layer! 88163712Simp 89163712SimpThe following fiels are fixed for IEEE-1394: 90163712Simptag = 01b 91163712Simptcode = 1010b 92163712SimpThe length is payload length, i.e. includes CIP header and data size. 93163712Simp 94163712SimpThe following fields are constant for MPEG TS: 95163712Simpsph = 1 (denoted as S in CIP header above) 96163712Simpdbs = 6 97163712Simpfmt = (1<<5) 98163712Simpfdf = reserved 99163712SimpIn the supported streams we also require 100163712Simpqpc = 0 101163712Simpfn = 3 102163712Simpand thus the payload is divided in 8 blocks as follows: 103163712Simp 104163712Simp +-----+-----+-----+-----+-----+-----+-----+-----+ 105163712Simp | db0 | db1 | db2 | db3 | db4 | db5 | db6 | db7 | 106163712Simp +-----+-----+-----+-----+-----+-----+-----+-----+ 107163712Simp 108163712SimpWe have several cases of payload distribution based on stream 109163712Simpbandwidth (R): 110163712Simp1) R < 1.5 Mbps: any of db0..db7 may be payload, 111163712Simp2) 1.5 < R < 3 Mbps: db0/db1 or db2/db3 or db4/db5 or db6/db7 is payload, 112163712Simp3) 3 < R < 6 Mbps: db0/db1/db2/db3 or db4/db5/db6/db7 is payload, 113163712Simp4) R > 6 Mbps: all db0..db7 contain the payload. 114163712SimpCurently, only case (4) is supported in fwmpegts.c 115163712Simp 116163712SimpEach packet may contain N MPEG TS data blocks with timestamp header, 117163712Simpwhich are (4+188)B long. Experimentally, the N ranges from 0 through 3. 118163712Simp 119163712Simp*****************************************************************************/ 120163712Simp 121163712Simp 122163712Simptypedef uint8_t mpeg_ts_pld[188]; 123163712Simp 124163712Simpstruct mpeg_pldt { 125163712Simp#if BYTE_ORDER == BIG_ENDIAN 126163712Simp uint32_t :7, 127163712Simp c_count:13, 128163712Simp c_offset:12; 129163712Simp#else /* BYTE_ORDER != BIG_ENDIAN */ 130163712Simp uint32_t c_offset:12, 131163712Simp c_count:13, 132163712Simp :7; 133163712Simp#endif /* BYTE_ORDER == BIG_ENDIAN */ 134163712Simp mpeg_ts_pld payload; 135163712Simp}; 136163712Simp 137163712Simp 138163712Simp#define NCHUNK 8 139163712Simp#define PSIZE 596 140163712Simp#define NPACKET_R 4096 141163712Simp#define RBUFSIZE (PSIZE * NPACKET_R) 142163712Simp 143163712Simpvoid 144163712Simpmpegtsrecv(int d, const char *filename, char ich, int count) 145163712Simp{ 146163712Simp struct ciphdr *ciph; 147163712Simp struct fw_isochreq isoreq; 148163712Simp struct fw_isobufreq bufreq; 149163712Simp struct fw_pkt *pkt; 150163712Simp struct mpeg_pldt *pld; 151163712Simp uint32_t *ptr; 152163712Simp int fd, k, len, m, pkt_size, startwr, tlen; 153163712Simp char *buf; 154163712Simp 155163712Simp startwr = 0; 156163712Simp 157163712Simp if (strcmp(filename, "-") == 0) 158163712Simp fd = STDOUT_FILENO; 159163712Simp else { 160163712Simp fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660); 161163712Simp if (fd == -1) 162163712Simp err(EX_NOINPUT, filename); 163163712Simp } 164163712Simp buf = malloc(RBUFSIZE); 165163712Simp 166163712Simp bufreq.rx.nchunk = NCHUNK; 167163712Simp bufreq.rx.npacket = NPACKET_R; 168163712Simp bufreq.rx.psize = PSIZE; 169163712Simp bufreq.tx.nchunk = 0; 170163712Simp bufreq.tx.npacket = 0; 171163712Simp bufreq.tx.psize = 0; 172163712Simp if (ioctl(d, FW_SSTBUF, &bufreq) < 0) 173163712Simp err(1, "ioctl"); 174163712Simp 175163712Simp isoreq.ch = ich & 0x3f; 176163712Simp isoreq.tag = (ich >> 6) & 3; 177163712Simp 178163712Simp if (ioctl(d, FW_SRSTREAM, &isoreq) < 0) 179163712Simp err(1, "ioctl"); 180163712Simp 181163712Simp k = m = 0; 182163712Simp while (count <= 0 || k <= count) { 183163712Simp len = tlen = read(d, buf, RBUFSIZE); 184163712Simp#if DEBUG 185163712Simp fprintf(stderr, "Read %d bytes.\n", len); 186163712Simp#endif /* DEBUG */ 187163712Simp if (len < 0) { 188163712Simp if (errno == EAGAIN) { 189163712Simp fprintf(stderr, "(EAGAIN) - push 'Play'?\n"); 190163712Simp if (len <= 0) 191163712Simp continue; 192163712Simp } else 193163712Simp err(1, "read failed"); 194163712Simp } 195163712Simp ptr = (uint32_t *) buf; 196163712Simp 197163712Simp do { 198163712Simp pkt = (struct fw_pkt *) ptr; 199163712Simp#if DEBUG 200163712Simp fprintf(stderr, "\nReading new packet.\n"); 201163712Simp fprintf(stderr, "%08x %08x %08x %08x\n", 202163712Simp htonl(ptr[0]), htonl(ptr[1]), 203163712Simp htonl(ptr[2]), htonl(ptr[3])); 204163712Simp#endif /* DEBUG */ 205163712Simp /* there is no CRC in the 1394 header */ 206163712Simp ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */ 207163712Simp if (ciph->fmt != CIP_FMT_MPEG) 208163712Simp errx(1, "unknown format 0x%x", ciph->fmt); 209163712Simp if (ciph->fn != 3) { 210163712Simp errx(1, 211163712Simp "unsupported MPEG TS stream, fn=%d (only fn=3 is supported)", 212163712Simp ciph->fn); 213163712Simp } 214163712Simp ptr = (uint32_t *) (ciph + 1); /* skip cip header */ 215163712Simp 216163712Simp if (pkt->mode.stream.len <= sizeof(struct ciphdr)) { 217163712Simp /* no payload */ 218163712Simp /* tlen needs to be decremented before end of the loop */ 219163712Simp goto next; 220163712Simp } 221163712Simp#if DEBUG 222163712Simp else { 223163712Simp fprintf(stderr, 224163712Simp "Packet net payload length (IEEE1394 header): %d\n", 225163712Simp pkt->mode.stream.len - sizeof(struct ciphdr)); 226163712Simp fprintf(stderr, "Data block size (CIP header): %d [q], %d [B]\n", 227163712Simp ciph->len, ciph->len * 4); 228163712Simp fprintf(stderr, 229163712Simp "Data fraction number (CIP header): %d => DBC increments with %d\n", 230163712Simp ciph->fn, (1<<ciph->fn) ); 231163712Simp fprintf(stderr, "QCP (CIP header): %d\n", ciph->qpc ); 232163712Simp fprintf(stderr, "DBC counter (CIP header): %d\n", ciph->dbc ); 233163712Simp fprintf(stderr, "MPEG payload type size: %d\n", 234163712Simp sizeof(struct mpeg_pldt)); 235163712Simp } 236163712Simp#endif /* DEBUG */ 237163712Simp 238163712Simp /* This is a condition that needs to be satisfied to start 239163712Simp writing the data */ 240163712Simp if (ciph->dbc % (1<<ciph->fn) == 0) 241163712Simp startwr = 1; 242163712Simp /* Read out all the MPEG TS data blocks from current packet */ 243163712Simp for (pld = (struct mpeg_pldt *)ptr; 244163712Simp (intptr_t)pld < (intptr_t)((char *)ptr + 245163712Simp pkt->mode.stream.len - sizeof(struct ciphdr)); 246163712Simp pld++) { 247163712Simp if (startwr == 1) 248163712Simp write(fd, pld->payload, 249163712Simp sizeof(pld->payload)); 250163712Simp } 251163712Simp 252163712Simpnext: 253163712Simp /* CRCs are removed from both header and trailer 254163712Simp so that only 4 bytes of 1394 header remains */ 255163712Simp pkt_size = pkt->mode.stream.len + 4; 256163712Simp ptr = (uint32_t *)((intptr_t)pkt + pkt_size); 257163712Simp tlen -= pkt_size; 258163712Simp } while (tlen > 0); 259163712Simp#if DEBUG 260163712Simp fprintf(stderr, "\nReading a data from firewire.\n"); 261163712Simp#endif /* DEBUG */ 262163712Simp 263163712Simp } 264163712Simp if (fd != STDOUT_FILENO) 265163712Simp close(fd); 266163712Simp fprintf(stderr, "\n"); 267163712Simp} 268