1 /* $NetBSD: fwdv.c,v 1.8 2013/10/19 17:06:57 christos Exp $ */
2 /*
3 * Copyright (C) 2003
4 * 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/fwdv.c,v 1.8 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 #define FIX_FRAME 1
59
60 struct frac {
61 int n,d;
62 };
63
64 struct frac frame_cycle[2] = {
65 {8000*100, 2997}, /* NTSC 8000 cycle / 29.97 Hz */
66 {320, 1}, /* PAL 8000 cycle / 25 Hz */
67 };
68 int npackets[] = {
69 250 /* NTSC */,
70 300 /* PAL */
71 };
72 struct frac pad_rate[2] = {
73 {203, 2997}, /* = (8000 - 29.97 * 250)/(29.97 * 250) */
74 {1, 15}, /* = (8000 - 25 * 300)/(25 * 300) */
75 };
76 const char *system_name[] = {"NTSC", "PAL"};
77 int frame_rate[] = {30, 25};
78
79 #define PSIZE 512
80 #define DSIZE 480
81 #define NCHUNK 64
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
91 void
dvrecv(int d,const char * filename,char ich,int count)92 dvrecv(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 uint32_t *ptr;
101 int len, tlen, npad, fd, k, m, vec, lsystem = -1, nb;
102 int nblocks[] = {250 /* NTSC */, 300 /* PAL */};
103 struct iovec wbuf[NPACKET_R];
104
105 if (strcmp(filename, "-") == 0) {
106 fd = STDOUT_FILENO;
107 } else {
108 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0660);
109 if (fd == -1)
110 err(EX_NOINPUT, "%s: %s", __func__, filename);
111 }
112 buf = malloc(RBUFSIZE);
113 if (buf == NULL)
114 err(EX_SOFTWARE, "%s: buffer alloc", __func__);
115 memset(wbuf, 0, sizeof(wbuf));
116
117 pad = malloc(DSIZE*MAXBLOCKS);
118 if (pad == NULL)
119 err(EX_SOFTWARE, "%s: pad alloc", __func__);
120
121 memset(pad, 0xff, DSIZE*MAXBLOCKS);
122
123 bufreq.rx.nchunk = NCHUNK;
124 bufreq.rx.npacket = NPACKET_R;
125 bufreq.rx.psize = PSIZE;
126 bufreq.tx.nchunk = 0;
127 bufreq.tx.npacket = 0;
128 bufreq.tx.psize = 0;
129 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
130 err(EXIT_FAILURE, "%s: ioctl FW_SSTBUF", __func__);
131
132 isoreq.ch = ich & 0x3f;
133 isoreq.tag = (ich >> 6) & 3;
134
135 if (ioctl(d, FW_SRSTREAM, &isoreq) < 0)
136 err(EXIT_FAILURE, "%s: ioctl", __func__);
137
138 k = m = 0;
139 while (count <= 0 || k <= count) {
140 #if 0
141 tlen = 0;
142 while ((len = read(d, buf + tlen, PSIZE
143 /* RBUFSIZE - tlen */)) > 0) {
144 if (len < 0) {
145 if (errno == EAGAIN) {
146 fprintf(stderr, "(EAGAIN)\n");
147 fflush(stderr);
148 if (len <= 0)
149 continue;
150 } else
151 err(EXIT_FAILURE, "%s: read failed",
152 __func__);
153 }
154 tlen += len;
155 if ((RBUFSIZE - tlen) < PSIZE)
156 break;
157 };
158 #else
159 tlen = len = read(d, buf, RBUFSIZE);
160 if (len < 0) {
161 if (errno == EAGAIN) {
162 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
163 fflush(stderr);
164 if (len <= 0)
165 continue;
166 } else
167 err(EXIT_FAILURE, "%s: read failed", __func__);
168 }
169 #endif
170 vec = 0;
171 ptr = (uint32_t *) buf;
172 again:
173 pkt = (struct fw_pkt *) ptr;
174 #if DEBUG
175 fprintf(stderr, "%08x %08x %08x %08x\n",
176 htonl(ptr[0]), htonl(ptr[1]),
177 htonl(ptr[2]), htonl(ptr[3]));
178 #endif
179 ciph = (struct ciphdr *)(ptr + 1); /* skip iso header */
180 if (ciph->fmt != CIP_FMT_DVCR)
181 errx(EXIT_FAILURE, "%s: unknown format 0x%x",
182 __func__, ciph->fmt);
183 ptr = (uint32_t *) (ciph + 1); /* skip cip header */
184 #if DEBUG
185 if (ciph->fdf.dv.cyc != 0xffff && k == 0)
186 fprintf(stderr, "0x%04x\n", ntohs(ciph->fdf.dv.cyc));
187 #endif
188 if (pkt->mode.stream.len <= sizeof(struct ciphdr))
189 /* no payload */
190 goto next;
191 for (dv = (struct dvdbc *)ptr;
192 (char *)dv < (char *)(ptr + ciph->len);
193 dv+=6) {
194
195 #if DEBUG
196 fprintf(stderr, "(%d,%d) ", dv->sct, dv->dseq);
197 #endif
198 if (dv->sct == DV_SCT_HEADER && dv->dseq == 0) {
199 if (lsystem < 0) {
200 lsystem = ciph->fdf.dv.fs;
201 fprintf(stderr,
202 "%s\n", system_name[lsystem]);
203 }
204
205 /* Fix DSF bit */
206 if (lsystem == 1 &&
207 (dv->payload[0] & DV_DSF_12) == 0)
208 dv->payload[0] |= DV_DSF_12;
209 nb = nblocks[lsystem];
210 fprintf(stderr, "%d:%02d:%02d %d\r",
211 k / (3600 * frame_rate[lsystem]),
212 (k / (60 * frame_rate[lsystem])) % 60,
213 (k / frame_rate[lsystem]) % 60,
214 k % frame_rate[lsystem]);
215
216 #if FIX_FRAME
217 if (m > 0 && m != nb) {
218 /* padding bad frame */
219 npad = ((nb - m) % nb);
220 if (npad < 0)
221 npad += nb;
222 fprintf(stderr, "\n%d blocks padded\n",
223 npad);
224 npad *= DSIZE;
225 wbuf[vec].iov_base = pad;
226 wbuf[vec++].iov_len = npad;
227 if (vec >= NPACKET_R) {
228 writev(fd, wbuf, vec);
229 vec = 0;
230 }
231 }
232 #endif
233 k++;
234 fflush(stderr);
235 m = 0;
236 }
237 if (k == 0 || (count > 0 && k > count))
238 continue;
239 m++;
240 wbuf[vec].iov_base = (char *) dv;
241 wbuf[vec++].iov_len = DSIZE;
242 if (vec >= NPACKET_R) {
243 writev(fd, wbuf, vec);
244 vec = 0;
245 }
246 }
247 ptr = (uint32_t *)dv;
248 next:
249 if ((char *)ptr < buf + tlen)
250 goto again;
251 if (vec > 0)
252 writev(fd, wbuf, vec);
253 }
254 if (fd != STDOUT_FILENO)
255 close(fd);
256 fprintf(stderr, "\n");
257 }
258
259
260 void
dvsend(int d,const char * filename,char ich,int count)261 dvsend(int d, const char *filename, char ich, int count)
262 {
263 struct fw_isochreq isoreq;
264 struct fw_isobufreq bufreq;
265 struct dvdbc *dv;
266 struct fw_pkt *pkt;
267 int len, tlen, header, fd, frames, packets, vec, offset, nhdr, i;
268 int lsystem=-1, pad_acc, cycle_acc, cycle, f_frac;
269 struct iovec wbuf[TNBUF*2 + NEMPTY];
270 char *pbuf;
271 uint32_t iso_data, iso_empty, hdr[TNBUF + NEMPTY][3];
272 struct ciphdr *ciph;
273 struct timeval start, end;
274 double rtime;
275
276 cycle_acc = cycle = 0;
277
278 fd = open(filename, O_RDONLY);
279 if (fd == -1)
280 err(EX_NOINPUT, "%s: %s", __func__, filename);
281
282 pbuf = malloc(DSIZE * TNBUF);
283 bzero(wbuf, sizeof(wbuf));
284
285 bufreq.rx.nchunk = 0;
286 bufreq.rx.npacket = 0;
287 bufreq.rx.psize = 0;
288 bufreq.tx.nchunk = NCHUNK;
289 bufreq.tx.npacket = NPACKET_T;
290 bufreq.tx.psize = PSIZE;
291 if (ioctl(d, FW_SSTBUF, &bufreq) < 0)
292 err(EXIT_FAILURE, "%s: ioctl FW_SSTBUF", __func__);
293
294 isoreq.ch = ich & 0x3f;
295 isoreq.tag = (ich >> 6) & 3;
296
297 if (ioctl(d, FW_STSTREAM, &isoreq) < 0)
298 err(EXIT_FAILURE, "%s: ioctl FW_STSTREAM", __func__);
299
300 iso_data = 0;
301 pkt = (struct fw_pkt *) &iso_data;
302 pkt->mode.stream.len = DSIZE + sizeof(struct ciphdr);
303 pkt->mode.stream.sy = 0;
304 pkt->mode.stream.tcode = FWTCODE_STREAM;
305 pkt->mode.stream.chtag = ich;
306 iso_empty = iso_data;
307 pkt = (struct fw_pkt *) &iso_empty;
308 pkt->mode.stream.len = sizeof(struct ciphdr);
309
310 bzero(hdr[0], sizeof(hdr[0]));
311 hdr[0][0] = iso_data;
312 ciph = (struct ciphdr *)&hdr[0][1];
313 ciph->src = 0; /* XXX */
314 ciph->len = 120;
315 ciph->dbc = 0;
316 ciph->eoh1 = 1;
317 ciph->fdf.dv.cyc = 0xffff;
318
319 for (i = 1; i < TNBUF; i++)
320 bcopy(hdr[0], hdr[i], sizeof(hdr[0]));
321
322 gettimeofday(&start, NULL);
323 #if DEBUG
324 fprintf(stderr, "%08x %08x %08x\n",
325 htonl(hdr[0]), htonl(hdr[1]), htonl(hdr[2]));
326 #endif
327 frames = 0;
328 packets = 0;
329 pad_acc = 0;
330 for (;;) {
331 tlen = 0;
332 while (tlen < DSIZE * TNBUF) {
333 len = read(fd, pbuf + tlen, DSIZE * TNBUF - tlen);
334 if (len <= 0) {
335 if (tlen > 0)
336 break;
337 if (len < 0)
338 warn("read");
339 else
340 fprintf(stderr, "\nend of file\n");
341 goto send_end;
342 }
343 tlen += len;
344 }
345 vec = 0;
346 offset = 0;
347 nhdr = 0;
348 next:
349 dv = (struct dvdbc *)(pbuf + offset * DSIZE);
350 #if 0
351 header = (dv->sct == 0 && dv->dseq == 0);
352 #else
353 header = (packets == 0 || packets % npackets[lsystem] == 0);
354 #endif
355
356 ciph = (struct ciphdr *)&hdr[nhdr][1];
357 if (header) {
358 if (lsystem < 0) {
359 lsystem = ((dv->payload[0] & DV_DSF_12) != 0);
360 printf("%s\n", system_name[lsystem]);
361 cycle = 1;
362 cycle_acc = frame_cycle[lsystem].d * cycle;
363 }
364 fprintf(stderr, "%d", frames % 10);
365 frames ++;
366 if (count > 0 && frames > count)
367 break;
368 if (frames % frame_rate[lsystem] == 0)
369 fprintf(stderr, "\n");
370 fflush(stderr);
371 f_frac = (cycle_acc % frame_cycle[lsystem].d
372 * CYCLE_FRAC) / frame_cycle[lsystem].d;
373 #if 0
374 int f_cycle = (cycle_acc / frame_cycle[lsystem].d)
375 & 0xf;
376 ciph->fdf.dv.cyc = htons(f_cycle << 12 | f_frac);
377 #else
378 ciph->fdf.dv.cyc = htons(cycle << 12 | f_frac);
379 #endif
380 cycle_acc += frame_cycle[lsystem].n;
381 cycle_acc %= frame_cycle[lsystem].d * 0x10;
382
383 } else {
384 ciph->fdf.dv.cyc = 0xffff;
385 }
386 ciph->dbc = packets++ % 256;
387 pad_acc += pad_rate[lsystem].n;
388 if (pad_acc >= pad_rate[lsystem].d) {
389 pad_acc -= pad_rate[lsystem].d;
390 bcopy(hdr[nhdr], hdr[nhdr+1], sizeof(hdr[0]));
391 hdr[nhdr][0] = iso_empty;
392 wbuf[vec].iov_base = (char *)hdr[nhdr];
393 wbuf[vec++].iov_len = sizeof(hdr[0]);
394 nhdr ++;
395 cycle ++;
396 }
397 hdr[nhdr][0] = iso_data;
398 wbuf[vec].iov_base = (char *)hdr[nhdr];
399 wbuf[vec++].iov_len = sizeof(hdr[0]);
400 wbuf[vec].iov_base = (char *)dv;
401 wbuf[vec++].iov_len = DSIZE;
402 nhdr ++;
403 cycle ++;
404 offset ++;
405 if (offset * DSIZE < tlen)
406 goto next;
407
408 again:
409 len = writev(d, wbuf, vec);
410 if (len < 0) {
411 if (errno == EAGAIN) {
412 fprintf(stderr, "(EAGAIN) - push 'Play'?\n");
413 goto again;
414 }
415 err(EXIT_FAILURE, "%s: write failed", __func__);
416 }
417 }
418 close(fd);
419 fprintf(stderr, "\n");
420 send_end:
421 gettimeofday(&end, NULL);
422 rtime = end.tv_sec - start.tv_sec
423 + (end.tv_usec - start.tv_usec) * 1e-6;
424 fprintf(stderr, "%d frames, %.2f secs, %.2f frames/sec\n",
425 frames, rtime, frames/rtime);
426 }
427