fwcontrol patch for MPEG TS
Petr Holub
hopet at ics.muni.cz
Mon Jan 31 02:41:51 PST 2005
Hi guys,
below is very rudimentary patch for fwcontrol to be able to receive
high bandwidth MPEG TS stream using
-M filname
flag. I've tested it with SONY HDR-FX1E camera (a HDV camera) and
my notebook. Patch is against 5.3-RELEASE.
Cheers,
Petr
================================================================
Petr Holub
CESNET z.s.p.o. Supercomputing Center Brno
Zikova 4 Institute of Compt. Science
162 00 Praha 6, CZ Masaryk University
Czech Republic Botanicka 68a, 60200 Brno, CZ
e-mail: Petr.Holub at cesnet.cz phone: +420-549493944
fax: +420-541212747
e-mail: hopet at ics.muni.cz
Index: fwcontrol/Makefile
===================================================================
RCS file: /home/hopet/fwcontrol/fwcontrol/Makefile,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 Makefile
--- fwcontrol/Makefile 30 Jan 2005 22:35:05 -0000 1.1.1.1
+++ fwcontrol/Makefile 30 Jan 2005 23:20:38 -0000
@@ -1,7 +1,7 @@
# $FreeBSD: src/usr.sbin/fwcontrol/Makefile,v 1.4 2003/04/30 03:39:25 simokawa
Exp $
PROG= fwcontrol
-SRCS= fwcontrol.c fwcrom.c fwdv.c
+SRCS= fwcontrol.c fwcrom.c fwdv.c fwmpegts.c
MAN= fwcontrol.8
.PATH: ${.CURDIR}/../../sys/dev/firewire
Index: fwcontrol/fwcontrol.8
===================================================================
RCS file: /home/hopet/fwcontrol/fwcontrol/fwcontrol.8,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- fwcontrol/fwcontrol.8 30 Jan 2005 22:35:05 -0000 1.1.1.1
+++ fwcontrol/fwcontrol.8 30 Jan 2005 23:14:07 -0000 1.2
@@ -102,6 +102,8 @@
Ports Collection.
.It Fl S Ar filename
Send a DV file as isochronous stream.
+.It Fl M Ar filename
+Receive an MPEG TS stream and dump it to a file from the isochronous stream.
.El
.Sh EXAMPLES
Each DV frame has a fixed size and it is easy to edit the frame order.
@@ -144,3 +146,5 @@
.An Hidetoshi Shimokawa Aq simokawa at FreeBSD.org
.Sh BUGS
This utility is still under development and provided for debugging purposes.
+MPEG TS receiving support is very experimental and supports only high bandwidth
+streams (fn=3).
Index: fwcontrol/fwcontrol.c
===================================================================
RCS file: /home/hopet/fwcontrol/fwcontrol/fwcontrol.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 fwcontrol.c
--- fwcontrol/fwcontrol.c 30 Jan 2005 22:35:05 -0000 1.1.1.1
+++ fwcontrol/fwcontrol.c 31 Jan 2005 10:14:13 -0000
@@ -54,6 +54,7 @@
extern int dvrecv(int, char *, char, int);
extern int dvsend(int, char *, char, int);
+extern int mpegtsrecv(int, char *, char, int);
static void
usage(void)
@@ -73,7 +74,8 @@
"\t-d: hex dump of configuration ROM\n"
"\t-l: load and parse hex dump file of configuration ROM\n"
"\t-R: Receive DV stream\n"
- "\t-S: Send DV stream\n");
+ "\t-S: Send DV stream\n"
+ "\t-M: Receive MPEG TS stream\n");
exit(0);
}
@@ -609,7 +611,7 @@
list_dev(fd);
}
- while ((ch = getopt(argc, argv, "g:o:s:b:prtc:d:l:u:R:S:")) != -1)
+ while ((ch = getopt(argc, argv, "g:o:s:b:prtc:d:l:u:R:S:M:")) != -1)
switch(ch) {
case 'b':
tmp = strtol(optarg, NULL, 0);
@@ -681,6 +683,10 @@
case 'S':
open_dev(&fd, devbase);
dvsend(fd, optarg, TAG | CHANNEL, -1);
+ break;
+ case 'M':
+ open_dev(&fd, devbase);
+ mpegtsrecv(fd, optarg, TAG | CHANNEL, -1);
break;
default:
usage();
Index: fwcontrol/fwmpegts.c
===================================================================
RCS file: fwcontrol/fwmpegts.c
diff -N fwcontrol/fwmpegts.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ fwcontrol/fwmpegts.c 31 Jan 2005 10:14:27 -0000 1.4
@@ -0,0 +1,190 @@
+/*
+ * Copyright (C) 2003
+ * Petr Holub. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ *
+ * This product includes software developed by Hidetoshi Shimokawa.
+ *
+ * 4. Neither the name of the author nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/usr.sbin/fwcontrol/fwdv.c,v 1.5 2003/04/17 03:38:03 simokawa
Exp $
+ */
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+
+#if __FreeBSD_version >= 500000
+#include <arpa/inet.h>
+#endif
+
+#include <err.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <dev/firewire/firewire.h>
+#include <dev/firewire/iec68113.h>
+
+#define DEBUG 0
+
+typedef uint8_t mpeg_ts_pld[188];
+
+struct mpeg_pldt {
+#if BYTE_ORDER == BIG_ENDIAN
+ uint32_t :7,
+ c_count:13,
+ c_offset:12;
+#else
+ uint32_t c_offset:12,
+ c_count:13,
+ :7;
+#endif
+ mpeg_ts_pld payload;
+};
+
+
+#define NCHUNK 8
+#define PSIZE 596
+#define NPACKET_R 4096
+#define RBUFSIZE (PSIZE * NPACKET_R)
+
+int
+mpegtsrecv(int d, char *filename, char ich, int count)
+{
+ struct fw_isochreq isoreq;
+ struct fw_isobufreq bufreq;
+ struct ciphdr *ciph;
+ struct fw_pkt *pkt;
+ struct mpeg_pldt *pld;
+ int pkt_size;
+ char *buf;
+ u_int32_t *ptr;
+ int len, tlen, fd, k, m, startwr = 0;
+
+ fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
+ buf = (char *)malloc(RBUFSIZE);
+
+ bufreq.rx.nchunk = NCHUNK;
+ bufreq.rx.npacket = NPACKET_R;
+ bufreq.rx.psize = PSIZE;
+ bufreq.tx.nchunk = 0;
+ bufreq.tx.npacket = 0;
+ bufreq.tx.psize = 0;
+ if (ioctl(d, FW_SSTBUF, &bufreq) < 0) {
+ err(1, "ioctl");
+ }
+
+ isoreq.ch = ich & 0x3f;
+ isoreq.tag = (ich >> 6) & 3;
+
+ if( ioctl(d, FW_SRSTREAM, &isoreq) < 0)
+ err(1, "ioctl");
+
+ k = m = 0;
+ while (count <= 0 || k <= count) {
+ len = tlen = read(d, buf, RBUFSIZE);
+#if DEBUG
+ fprintf(stderr, "Read %d bytes.\n", len);
+#endif
+ if (len < 0) {
+ if (errno == EAGAIN) {
+ fprintf(stderr, "(EAGAIN)\n");
+ fflush(stderr);
+ if (len <= 0)
+ continue;
+ } else
+ err(1, "read failed");
+ }
+ ptr = (u_int32_t *) buf;
+
+ do {
+ pkt = (struct fw_pkt *) ptr;
+#if DEBUG
+ fprintf(stderr, "%08x %08x %08x %08x\n",
+ htonl(ptr[0]), htonl(ptr[1]),
+ htonl(ptr[2]), htonl(ptr[3]));
+#endif
+ /* there is no CRC in the 1394 header */
+ ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */
+ if (ciph->fmt != CIP_FMT_MPEG)
+ errx(1, "unknown format 0x%x", ciph->fmt);
+ if (ciph->fn != 3)
+ errx(1, "unsupported MPEG TS stream, fn=%d (only fn=3 is supported)",
ciph->fn);
+ ptr = (u_int32_t *) (ciph + 1); /* skip cip header */
+
+ if (pkt->mode.stream.len <= sizeof(struct ciphdr)) {
+ /* no payload */
+ /* tlen needs to be decremented before end of the loop */
+ goto next;
+ }
+#if DEBUG
+ else {
+ fprintf(stderr, "Packet net payload length (IEEE1394 header): %d\n",
pkt->mode.stream.len - sizeof(struct ciphdr));
+ fprintf(stderr, "Data block size (CIP header): %d [q], %d [B]\n",
ciph->len, ciph->len * 4);
+ fprintf(stderr, "Data fraction number (CIP header): %d => DBC increments
with %d\n", ciph->fn, (1<<ciph->fn) );
+ fprintf(stderr, "QCP (CIP header): %d\n", ciph->qpc );
+ fprintf(stderr, "DBC counter (CIP header): %d\n", ciph->dbc );
+ fprintf(stderr, "MPEG payload type size: %d\n", sizeof(struct mpeg_pldt));
+ }
+#endif
+
+ if ( ciph->dbc % (1<<ciph->fn) == 0) {
+ startwr = 1;
+ }
+ for (pld = (struct mpeg_pldt *)ptr;
+ (int)pld < (int)((char *)ptr + pkt->mode.stream.len - sizeof(struct
ciphdr) );
+ pld++) {
+ if(startwr == 1)
+ write(fd, pld->payload, sizeof(pld->payload));
+ }
+
+next:
+ /* CRCs are removed from both header and trailer
+ so that only 4 bytes of 1394 header remains */
+ pkt_size = pkt->mode.stream.len + 4;
+ ptr = (u_int32_t *) ((int)pkt + pkt_size);
+#if DEBUG
+ fprintf(stderr, "Packet size is %d.\n", pkt_size);
+ fprintf(stderr, "tlen = %d.\n\n", tlen);
+#endif
+ tlen -= pkt_size;
+ } while (tlen > 0);
+#if DEBUG
+ fprintf(stderr, "\nReading a data from firewire.\n");
+#endif
+
+ }
+ close(fd);
+ fprintf(stderr, "\n");
+ return 0;
+}
+
More information about the freebsd-firewire
mailing list