Deleted Added
full compact
fwdv.c (113584) fwdv.c (163712)
1/*
2 * Copyright (C) 2003
3 * Hidetoshi Shimokawa. All rights reserved.
1/*
2 * Copyright (C) 2003
3 * Hidetoshi Shimokawa. All rights reserved.
4 *
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 *
16 * This product includes software developed by Hidetoshi Shimokawa.
17 *
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 *
16 * This product includes software developed by Hidetoshi Shimokawa.
17 *
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/usr.sbin/fwcontrol/fwdv.c 113584 2003-04-17 03:38:03Z simokawa $
33 *
34 * $FreeBSD: head/usr.sbin/fwcontrol/fwdv.c 163712 2006-10-26 22:33:38Z imp $
35 */
36#include <sys/param.h>
37#include <sys/ioctl.h>
38#include <sys/time.h>
39#include <sys/types.h>
40#include <sys/uio.h>
41
42#if __FreeBSD_version >= 500000
43#include <arpa/inet.h>
44#endif
45
46#include <err.h>
47#include <errno.h>
48#include <unistd.h>
49#include <fcntl.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
35 */
36#include <sys/param.h>
37#include <sys/ioctl.h>
38#include <sys/time.h>
39#include <sys/types.h>
40#include <sys/uio.h>
41
42#if __FreeBSD_version >= 500000
43#include <arpa/inet.h>
44#endif
45
46#include <err.h>
47#include <errno.h>
48#include <unistd.h>
49#include <fcntl.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <sysexits.h>
53
54#include <dev/firewire/firewire.h>
55#include <dev/firewire/iec68113.h>
56
54
55#include <dev/firewire/firewire.h>
56#include <dev/firewire/iec68113.h>
57
58#include "fwmethods.h"
59
57#define DEBUG 0
58#define FIX_FRAME 1
59
60struct frac {
60#define DEBUG 0
61#define FIX_FRAME 1
62
63struct frac {
61 int n,d;
64 int n,d;
62};
63
64struct frac frame_cycle[2] = {
65 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */
66 {320, 1}, /* PAL 8000 cycle / 25 Hz */
67};
68int npackets[] = {
69 250 /* NTSC */,
70 300 /* PAL */
71};
72struct frac pad_rate[2] = {
73 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */
74 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */
75};
76char *system_name[] = {"NTSC", "PAL"};
77int frame_rate[] = {30, 25};
78
79#define PSIZE 512
80#define DSIZE 480
81#define NCHUNK 8
82
83#define NPACKET_R 256
84#define NPACKET_T 255
85#define TNBUF 100 /* XXX too large value causes block noise */
86#define NEMPTY 10 /* depends on TNBUF */
87#define RBUFSIZE (PSIZE * NPACKET_R)
88#define MAXBLOCKS (300)
89#define CYCLE_FRAC 0xc00
90
65};
66
67struct frac frame_cycle[2] = {
68 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */
69 {320, 1}, /* PAL 8000 cycle / 25 Hz */
70};
71int npackets[] = {
72 250 /* NTSC */,
73 300 /* PAL */
74};
75struct frac pad_rate[2] = {
76 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */
77 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */
78};
79char *system_name[] = {"NTSC", "PAL"};
80int frame_rate[] = {30, 25};
81
82#define PSIZE 512
83#define DSIZE 480
84#define NCHUNK 8
85
86#define NPACKET_R 256
87#define NPACKET_T 255
88#define TNBUF 100 /* XXX too large value causes block noise */
89#define NEMPTY 10 /* depends on TNBUF */
90#define RBUFSIZE (PSIZE * NPACKET_R)
91#define MAXBLOCKS (300)
92#define CYCLE_FRAC 0xc00
93
91int
92dvrecv(int d, char *filename, char ich, int count)
94void
95dvrecv(int d, const char *filename, char ich, int count)
93{
94 struct fw_isochreq isoreq;
95 struct fw_isobufreq bufreq;
96 struct dvdbc *dv;
97 struct ciphdr *ciph;
98 struct fw_pkt *pkt;
99 char *pad, *buf;
100 u_int32_t *ptr;
101 int len, tlen, npad, fd, k, m, vec, system = -1, nb;
102 int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
103 struct iovec wbuf[NPACKET_R];
104
96{
97 struct fw_isochreq isoreq;
98 struct fw_isobufreq bufreq;
99 struct dvdbc *dv;
100 struct ciphdr *ciph;
101 struct fw_pkt *pkt;
102 char *pad, *buf;
103 u_int32_t *ptr;
104 int len, tlen, npad, fd, k, m, vec, system = -1, nb;
105 int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
106 struct iovec wbuf[NPACKET_R];
107
105 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
106 buf = (char *)malloc(RBUFSIZE);
107 pad = (char *)malloc(DSIZE*MAXBLOCKS);
108 if(strcmp(filename, "-") == 0) {
109 fd = STDOUT_FILENO;
110 } else {
111 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
112 if (fd == -1)
113 err(EX_NOINPUT, filename);
114 }
115 buf = malloc(RBUFSIZE);
116 pad = malloc(DSIZE*MAXBLOCKS);
108 memset(pad, 0xff, DSIZE*MAXBLOCKS);
109 bzero(wbuf, sizeof(wbuf));
110
111 bufreq.rx.nchunk = NCHUNK;
112 bufreq.rx.npacket = NPACKET_R;
113 bufreq.rx.psize = PSIZE;
114 bufreq.tx.nchunk = 0;
115 bufreq.tx.npacket = 0;
116 bufreq.tx.psize = 0;
117 memset(pad, 0xff, DSIZE*MAXBLOCKS);
118 bzero(wbuf, sizeof(wbuf));
119
120 bufreq.rx.nchunk = NCHUNK;
121 bufreq.rx.npacket = NPACKET_R;
122 bufreq.rx.psize = PSIZE;
123 bufreq.tx.nchunk = 0;
124 bufreq.tx.npacket = 0;
125 bufreq.tx.psize = 0;
117 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) {
118 err(1, "ioctl");
119 }
126 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
127 err(1, "ioctl FW_SSTBUF");
120
121 isoreq.ch = ich & 0x3f;
122 isoreq.tag = (ich >> 6) & 3;
123
128
129 isoreq.ch = ich & 0x3f;
130 isoreq.tag = (ich >> 6) & 3;
131
124 if( ioctl(d, FW_SRSTREAM, &isoreq) < 0)
132 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
125 err(1, "ioctl");
126
127 k = m = 0;
128 while (count <= 0 || k <= count) {
129#if 0
130 tlen = 0;
131 while ((len = read(d, buf + tlen, PSIZE
132 /* RBUFSIZE - tlen */)) > 0) {
133 if (len < 0) {
134 if (errno == EAGAIN) {
135 fprintf(stderr, "(EAGAIN)\n");
136 fflush(stderr);
137 if (len <= 0)
138 continue;
139 } else
140 err(1, "read failed");
141 }
142 tlen += len;
143 if ((RBUFSIZE - tlen) < PSIZE)
144 break;
145 };
146#else
147 tlen = len = read(d, buf, RBUFSIZE);
148 if (len < 0) {
149 if (errno == EAGAIN) {
133 err(1, "ioctl");
134
135 k = m = 0;
136 while (count <= 0 || k <= count) {
137#if 0
138 tlen = 0;
139 while ((len = read(d, buf + tlen, PSIZE
140 /* RBUFSIZE - tlen */)) > 0) {
141 if (len < 0) {
142 if (errno == EAGAIN) {
143 fprintf(stderr, "(EAGAIN)\n");
144 fflush(stderr);
145 if (len <= 0)
146 continue;
147 } else
148 err(1, "read failed");
149 }
150 tlen += len;
151 if ((RBUFSIZE - tlen) < PSIZE)
152 break;
153 };
154#else
155 tlen = len = read(d, buf, RBUFSIZE);
156 if (len < 0) {
157 if (errno == EAGAIN) {
150 fprintf(stderr, "(EAGAIN)\n");
158 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
151 fflush(stderr);
152 if (len <= 0)
153 continue;
154 } else
155 err(1, "read failed");
156 }
157#endif
158 vec = 0;
159 ptr = (u_int32_t *) buf;
160again:
159 fflush(stderr);
160 if (len <= 0)
161 continue;
162 } else
163 err(1, "read failed");
164 }
165#endif
166 vec = 0;
167 ptr = (u_int32_t *) buf;
168again:
161 pkt = (struct fw_pkt *) ptr;
169 pkt = (struct fw_pkt *) ptr;
162#if DEBUG
163 fprintf(stderr, "%08x %08x %08x %08x\n",
164 htonl(ptr[0]), htonl(ptr[1]),
165 htonl(ptr[2]), htonl(ptr[3]));
166#endif
167 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */
168 if (ciph->fmt != CIP_FMT_DVCR)
169 errx(1, "unknown format 0x%x", ciph->fmt);
170 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */
171#if DEBUG
172 if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
173 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
174 }
175#endif
176 if (pkt->mode.stream.len <= sizeof(struct ciphdr))
177 /* no payload */
178 goto next;
179 for (dv = (struct dvdbc *)ptr;
180 (char *)dv < (char *)(ptr + ciph->len);
181 dv+=6) {
182
183#if DEBUG
184 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
185#endif
186 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
187 if (system < 0) {
188 system = ciph->fdf.dv.fs;
170#if DEBUG
171 fprintf(stderr, "%08x %08x %08x %08x\n",
172 htonl(ptr[0]), htonl(ptr[1]),
173 htonl(ptr[2]), htonl(ptr[3]));
174#endif
175 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */
176 if (ciph->fmt != CIP_FMT_DVCR)
177 errx(1, "unknown format 0x%x", ciph->fmt);
178 ptr = (u_int32_t *) (ciph + 1); /* skip cip header */
179#if DEBUG
180 if (ciph->fdf.dv.cyc != 0xffff && k == 0) {
181 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
182 }
183#endif
184 if (pkt->mode.stream.len <= sizeof(struct ciphdr))
185 /* no payload */
186 goto next;
187 for (dv = (struct dvdbc *)ptr;
188 (char *)dv < (char *)(ptr + ciph->len);
189 dv+=6) {
190
191#if DEBUG
192 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
193#endif
194 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
195 if (system < 0) {
196 system = ciph->fdf.dv.fs;
189 printf("%s\n", system_name[system]);
197 fprintf(stderr, "%s\n", system_name[system]);
190 }
191
192 /* Fix DSF bit */
193 if (system == 1 &&
194 (dv->payload[0] & DV_DSF_12) == 0)
195 dv->payload[0] |= DV_DSF_12;
196 nb = nblocks[system];
197 fprintf(stderr, "%d", k%10);
198#if FIX_FRAME
199 if (m > 0 && m != nb) {
200 /* padding bad frame */
201 npad = ((nb - m) % nb);
202 if (npad < 0)
203 npad += nb;
204 fprintf(stderr, "(%d blocks padded)",
205 npad);
206 npad *= DSIZE;
207 wbuf[vec].iov_base = pad;
208 wbuf[vec++].iov_len = npad;
209 if (vec >= NPACKET_R) {
210 writev(fd, wbuf, vec);
211 vec = 0;
212 }
213 }
214#endif
215 k++;
216 if (k % frame_rate[system] == 0) {
217 /* every second */
218 fprintf(stderr, "\n");
219 }
220 fflush(stderr);
221 m = 0;
222 }
223 if (k == 0 || (count > 0 && k > count))
224 continue;
225 m++;
226 wbuf[vec].iov_base = (char *) dv;
227 wbuf[vec++].iov_len = DSIZE;
228 if (vec >= NPACKET_R) {
229 writev(fd, wbuf, vec);
230 vec = 0;
231 }
232 }
233 ptr = (u_int32_t *)dv;
234next:
235 if ((char *)ptr < buf + tlen)
236 goto again;
237 if (vec > 0)
238 writev(fd, wbuf, vec);
239 }
198 }
199
200 /* Fix DSF bit */
201 if (system == 1 &&
202 (dv->payload[0] & DV_DSF_12) == 0)
203 dv->payload[0] |= DV_DSF_12;
204 nb = nblocks[system];
205 fprintf(stderr, "%d", k%10);
206#if FIX_FRAME
207 if (m > 0 && m != nb) {
208 /* padding bad frame */
209 npad = ((nb - m) % nb);
210 if (npad < 0)
211 npad += nb;
212 fprintf(stderr, "(%d blocks padded)",
213 npad);
214 npad *= DSIZE;
215 wbuf[vec].iov_base = pad;
216 wbuf[vec++].iov_len = npad;
217 if (vec >= NPACKET_R) {
218 writev(fd, wbuf, vec);
219 vec = 0;
220 }
221 }
222#endif
223 k++;
224 if (k % frame_rate[system] == 0) {
225 /* every second */
226 fprintf(stderr, "\n");
227 }
228 fflush(stderr);
229 m = 0;
230 }
231 if (k == 0 || (count > 0 && k > count))
232 continue;
233 m++;
234 wbuf[vec].iov_base = (char *) dv;
235 wbuf[vec++].iov_len = DSIZE;
236 if (vec >= NPACKET_R) {
237 writev(fd, wbuf, vec);
238 vec = 0;
239 }
240 }
241 ptr = (u_int32_t *)dv;
242next:
243 if ((char *)ptr < buf + tlen)
244 goto again;
245 if (vec > 0)
246 writev(fd, wbuf, vec);
247 }
240 close(fd);
248 if(fd != STDOUT_FILENO) {
249 close(fd);
250 }
241 fprintf(stderr, "\n");
251 fprintf(stderr, "\n");
242 return 0;
243}
244
245
252}
253
254
246int
247dvsend(int d, char *filename, char ich, int count)
255void
256dvsend(int d, const char *filename, char ich, int count)
248{
249 struct fw_isochreq isoreq;
250 struct fw_isobufreq bufreq;
251 struct dvdbc *dv;
252 struct fw_pkt *pkt;
253 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
257{
258 struct fw_isochreq isoreq;
259 struct fw_isobufreq bufreq;
260 struct dvdbc *dv;
261 struct fw_pkt *pkt;
262 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
254 int system=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac;
263 int system=-1, pad_acc, cycle_acc, cycle, f_cycle, f_frac;
255 struct iovec wbuf[TNBUF*2 + NEMPTY];
256 char *pbuf;
257 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
258 struct ciphdr *ciph;
259 struct timeval start, end;
260 double rtime;
261
262 fd = open(filename, O_RDONLY);
264 struct iovec wbuf[TNBUF*2 + NEMPTY];
265 char *pbuf;
266 u_int32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
267 struct ciphdr *ciph;
268 struct timeval start, end;
269 double rtime;
270
271 fd = open(filename, O_RDONLY);
263 pbuf = (char *)malloc(DSIZE * TNBUF);
272 if (fd == -1)
273 err(EX_NOINPUT, filename);
274
275 pbuf = malloc(DSIZE * TNBUF);
264 bzero(wbuf, sizeof(wbuf));
265
266 bufreq.rx.nchunk = 0;
267 bufreq.rx.npacket = 0;
268 bufreq.rx.psize = 0;
269 bufreq.tx.nchunk = NCHUNK;
270 bufreq.tx.npacket = NPACKET_T;
271 bufreq.tx.psize = PSIZE;
276 bzero(wbuf, sizeof(wbuf));
277
278 bufreq.rx.nchunk = 0;
279 bufreq.rx.npacket = 0;
280 bufreq.rx.psize = 0;
281 bufreq.tx.nchunk = NCHUNK;
282 bufreq.tx.npacket = NPACKET_T;
283 bufreq.tx.psize = PSIZE;
272 if (ioctl(d, FW_SSTBUF, &bufreq) < 0) {
273 err(1, "ioctl");
274 }
284 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
285 err(1, "ioctl FW_SSTBUF");
275
276 isoreq.ch = ich & 0x3f;
277 isoreq.tag = (ich >> 6) & 3;
278
286
287 isoreq.ch = ich & 0x3f;
288 isoreq.tag = (ich >> 6) & 3;
289
279 if( ioctl(d, FW_STSTREAM, &isoreq) < 0)
280 err(1, "ioctl");
290 if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
291 err(1, "ioctl FW_STSTREAM");
281
282 iso_data = 0;
283 pkt = (struct fw_pkt *) &iso_data;
284 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
285 pkt->mode.stream.sy = 0;
286 pkt->mode.stream.tcode = FWTCODE_STREAM;
287 pkt->mode.stream.chtag = ich;
288 iso_empty = iso_data;
289 pkt = (struct fw_pkt *) &iso_empty;
290 pkt->mode.stream.len = sizeof(struct ciphdr);
291
292 bzero(hdr[0], sizeof(hdr[0]));
293 hdr[0][0] = iso_data;
294 ciph = (struct ciphdr *)&hdr[0][1];
295 ciph->src = 0; /* XXX */
296 ciph->len = 120;
297 ciph->dbc = 0;
298 ciph->eoh1 = 1;
299 ciph->fdf.dv.cyc = 0xffff;
300
292
293 iso_data = 0;
294 pkt = (struct fw_pkt *) &iso_data;
295 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
296 pkt->mode.stream.sy = 0;
297 pkt->mode.stream.tcode = FWTCODE_STREAM;
298 pkt->mode.stream.chtag = ich;
299 iso_empty = iso_data;
300 pkt = (struct fw_pkt *) &iso_empty;
301 pkt->mode.stream.len = sizeof(struct ciphdr);
302
303 bzero(hdr[0], sizeof(hdr[0]));
304 hdr[0][0] = iso_data;
305 ciph = (struct ciphdr *)&hdr[0][1];
306 ciph->src = 0; /* XXX */
307 ciph->len = 120;
308 ciph->dbc = 0;
309 ciph->eoh1 = 1;
310 ciph->fdf.dv.cyc = 0xffff;
311
301 for (i = 1; i < TNBUF; i++) {
312 for (i = 1; i < TNBUF; i++)
302 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
313 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
303 }
304
305 gettimeofday(&start, NULL);
306#if DEBUG
307 fprintf(stderr, "%08x %08x %08x\n",
308 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
309#endif
310 frames = 0;
311 packets = 0;
312 pad_acc = 0;
313 while (1) {
314 tlen = 0;
315 while (tlen < DSIZE * TNBUF) {
316 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
317 if (len <= 0) {
318 if (tlen > 0)
319 break;
320 if (len < 0)
321 warn("read");
322 else
314
315 gettimeofday(&start, NULL);
316#if DEBUG
317 fprintf(stderr, "%08x %08x %08x\n",
318 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
319#endif
320 frames = 0;
321 packets = 0;
322 pad_acc = 0;
323 while (1) {
324 tlen = 0;
325 while (tlen < DSIZE * TNBUF) {
326 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
327 if (len <= 0) {
328 if (tlen > 0)
329 break;
330 if (len < 0)
331 warn("read");
332 else
323 printf("\nend of file\n");
333 fprintf(stderr, "\nend of file\n");
324 goto send_end;
325 }
326 tlen += len;
327 }
328 vec = 0;
329 offset = 0;
330 nhdr = 0;
331next:
332 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
333#if 0
334 header = (dv->sct == 0 && dv->dseq == 0);
335#else
336 header = (packets == 0 || packets % npackets[system] == 0);
337#endif
338
339 ciph = (struct ciphdr *)&hdr[nhdr][1];
340 if (header) {
341 if (system < 0) {
342 system = ((dv->payload[0] & DV_DSF_12) != 0);
343 printf("%s\n", system_name[system]);
344 cycle = 1;
345 cycle_acc = frame_cycle[system].d * cycle;
346 }
347 fprintf(stderr, "%d", frames % 10);
348 frames ++;
349 if (count > 0 && frames > count)
350 break;
351 if (frames % frame_rate[system] == 0)
352 fprintf(stderr, "\n");
353 fflush(stderr);
354 f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf;
355 f_frac = (cycle_acc % frame_cycle[system].d
356 * CYCLE_FRAC) / frame_cycle[system].d;
357#if 0
358 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
359#else
360 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
361#endif
362 cycle_acc += frame_cycle[system].n;
363 cycle_acc %= frame_cycle[system].d * 0x10;
364
365 } else {
366 ciph->fdf.dv.cyc = 0xffff;
367 }
368 ciph->dbc = packets++ % 256;
369 pad_acc += pad_rate[system].n;
370 if (pad_acc >= pad_rate[system].d) {
371 pad_acc -= pad_rate[system].d;
372 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
373 hdr[nhdr][0] = iso_empty;
374 wbuf[vec].iov_base = (char *)hdr[nhdr];
375 wbuf[vec++].iov_len = sizeof(hdr[0]);
376 nhdr ++;
377 cycle ++;
378 }
379 hdr[nhdr][0] = iso_data;
380 wbuf[vec].iov_base = (char *)hdr[nhdr];
381 wbuf[vec++].iov_len = sizeof(hdr[0]);
382 wbuf[vec].iov_base = (char *)dv;
383 wbuf[vec++].iov_len = DSIZE;
384 nhdr ++;
385 cycle ++;
386 offset ++;
387 if (offset * DSIZE < tlen)
388 goto next;
389
390again:
391 len = writev(d, wbuf, vec);
392 if (len < 0) {
393 if (errno == EAGAIN) {
334 goto send_end;
335 }
336 tlen += len;
337 }
338 vec = 0;
339 offset = 0;
340 nhdr = 0;
341next:
342 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
343#if 0
344 header = (dv->sct == 0 && dv->dseq == 0);
345#else
346 header = (packets == 0 || packets % npackets[system] == 0);
347#endif
348
349 ciph = (struct ciphdr *)&hdr[nhdr][1];
350 if (header) {
351 if (system < 0) {
352 system = ((dv->payload[0] & DV_DSF_12) != 0);
353 printf("%s\n", system_name[system]);
354 cycle = 1;
355 cycle_acc = frame_cycle[system].d * cycle;
356 }
357 fprintf(stderr, "%d", frames % 10);
358 frames ++;
359 if (count > 0 && frames > count)
360 break;
361 if (frames % frame_rate[system] == 0)
362 fprintf(stderr, "\n");
363 fflush(stderr);
364 f_cycle = (cycle_acc / frame_cycle[system].d) & 0xf;
365 f_frac = (cycle_acc % frame_cycle[system].d
366 * CYCLE_FRAC) / frame_cycle[system].d;
367#if 0
368 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
369#else
370 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
371#endif
372 cycle_acc += frame_cycle[system].n;
373 cycle_acc %= frame_cycle[system].d * 0x10;
374
375 } else {
376 ciph->fdf.dv.cyc = 0xffff;
377 }
378 ciph->dbc = packets++ % 256;
379 pad_acc += pad_rate[system].n;
380 if (pad_acc >= pad_rate[system].d) {
381 pad_acc -= pad_rate[system].d;
382 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
383 hdr[nhdr][0] = iso_empty;
384 wbuf[vec].iov_base = (char *)hdr[nhdr];
385 wbuf[vec++].iov_len = sizeof(hdr[0]);
386 nhdr ++;
387 cycle ++;
388 }
389 hdr[nhdr][0] = iso_data;
390 wbuf[vec].iov_base = (char *)hdr[nhdr];
391 wbuf[vec++].iov_len = sizeof(hdr[0]);
392 wbuf[vec].iov_base = (char *)dv;
393 wbuf[vec++].iov_len = DSIZE;
394 nhdr ++;
395 cycle ++;
396 offset ++;
397 if (offset * DSIZE < tlen)
398 goto next;
399
400again:
401 len = writev(d, wbuf, vec);
402 if (len < 0) {
403 if (errno == EAGAIN) {
394 fprintf(stderr, "(EAGAIN)\n");
395 fflush(stderr);
404 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
396 goto again;
397 }
398 err(1, "write failed");
399 }
400 }
401 close(fd);
402 fprintf(stderr, "\n");
403send_end:
404 gettimeofday(&end, NULL);
405 goto again;
406 }
407 err(1, "write failed");
408 }
409 }
410 close(fd);
411 fprintf(stderr, "\n");
412send_end:
413 gettimeofday(&end, NULL);
405 rtime = end.tv_sec - start.tv_sec
414 rtime = end.tv_sec - start.tv_sec
406 + (end.tv_usec - start.tv_usec) * 1e-6;
407 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
408 frames, rtime, frames/rtime);
415 + (end.tv_usec - start.tv_usec) * 1e-6;
416 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
417 frames, rtime, frames/rtime);
409 return 0;
410}
418}