File: vdd.cc

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (807 lines) | stat: -rw-r--r-- 17,941 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/*
 * Copyright (c) 1994 Regents of the University of California.
 * 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 the University of
 *      California, Berkeley and the Network Research Group at
 *      Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory 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.
 */

#ifndef lint
static char rcsid[] =
    "@(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/vic/vdd.cc,v 1.1.1.1 1998/02/18 18:03:52 ucacsva Exp $ (LBL)";
#endif

#include <stdio.h>
#include <sys/param.h>
#include <netinet/in.h>
#ifdef USE_SHM
extern "C" {
#include <sys/ipc.h>
#include <sys/shm.h>
#if defined(sun) && !defined(__svr4__)
int shmget(key_t, int, int);
char *shmat(int, char*, int);
int shmdt(char*);
int shmctl(int, int, struct shmid_ds*);
#endif
}
#endif

#include <osfcn.h>

#include "inet.h"
#include "rtp.h"
#include "p64.h"
#include "p64dump.h"
#include "color.h"
#include "renderer.h"
#include "vw.h"
#include "Tcl.h"

/* RTP v1 packet header */
struct rtp1hdr {
	u_char rh_chan;         /* version/channel */
	u_char rh_flags;        /* O/S/content */
	u_short rh_seqno;       /* sequence number */
	u_int32_t  rh_ts;
};
#define	RTPCONT_H261            31      /* CCITT H.261 */
#define	RTP_CONTENT_MASK	0x3f
#define	RTP_OPTION_BIT		0x80

int cnt = 0;
int aflag = 0;
int cflag = 0;
int Dflag = 0;
int iflag = 0;
int nflag = 0;
int qflag = 0;
int sflag = 0;
int vflag = 0;

int dumpno = -1;

u_int packets;
u_int frames;
u_int pictures;
u_int32_t tzero;
timeval start;

u_int firstseq;
u_int maxseq;
u_int seqwraps;
u_int32_t maxts;
int lastSeqno = 0;

#define H261_SLOTS 32
#define H261_SLOTMASK (H261_SLOTS - 1)
/*
 * packet 'slots' for resequencing & reassembling gobs split
 * across packet boundaries.
 */
struct h261_pslot {
	int len;
	u_short flags;
	u_short seqno;
	u_char* bp;
	int filler;
};
h261_pslot slot_[H261_SLOTS];

int fmt_ = -1;
u_int seqno_;
int packetsize_;
u_char* wrkbuf;
int wrkbuflen;
int width;
int height;

Renderer* renderer;
P64Decoder* decoder;
FILE* in;

void
usage()
{
	fprintf(stderr, "usage\n");
	exit(1);
}

char* fmtts(u_int tsdelta)
{
	static char tswrk[64];

	register u_int sec = tsdelta >> 16;
	register u_int h = sec / 3600;
	register u_int m = (sec % 3600) / 60;
	register u_int s = sec % 60;
	register u_int frac = tsdelta & 0xffff;
	register float f = float(s) + float(frac) * (1./65536.);
	sprintf(tswrk, "%02d:%02d:%06.3f", h, m, f);
	return (tswrk);
}

int nextpacket(u_char* pktbuf, int cc)
{
	if (packets == 0)
		gettimeofday(&start, 0);

	int sts = 0;
	u_int seqno;
	u_int32_t ts;
	register u_char* bp;

	u_int vers = *pktbuf >> 6;
	switch (vers) {
	case 1: {
			rtp1hdr* rh = (rtp1hdr*)pktbuf;
			cc -= sizeof(*rh);
			if ((rh->rh_chan & 0xc0) != 0x40 ||
			    (rh->rh_flags & RTP_CONTENT_MASK) != RTPCONT_H261)
				/* wrong version or content type */
				return (sts);

			seqno = ntohs(rh->rh_seqno);
			ts = ntohl(rh->rh_ts);
			bp = (u_char*)(rh + 1);
			if (rh->rh_flags & RTP_OPTION_BIT) {
				/* skip over options */
				register u_int optflags;
				do {
					optflags = bp[0];
					register u_int optlen = bp[1] << 2;
					if (optlen >= cc)
						/* empty packet or bad optlen */
						return (sts);
					bp += optlen;
					cc -= optlen;
				} while ((optflags & 0x80) == 0);
			}
		}
		break;

	case 2: {
			rtphdr* rh = (rtphdr*)pktbuf;
			int flags = ntohs(rh->rh_flags);
			if ((flags & 0x7f) != RTP_PT_H261)
				/* wrong content type */
				return (sts);
			if ((flags & 0x0f00) != 0)
				/* XXX - can't deal with csrcs */
				return (sts);
			seqno = ntohs(rh->rh_seqno);
			ts = ntohl(rh->rh_ts);
			cc -= sizeof(*rh);
			bp = (u_char*)(rh + 1);
		}
		break;

	default:
		/* unknown protocol version */
		return (sts);
	}
	if (cc <= 2)
		/* wrong size */
		return (sts);

	u_int flags = ntohs(*(u_short*)bp);
	bp += 2;
	cc -= 2;
	/*
	 * if S is set, we have a start-of-GOB, but the RTPv1/H.261
	 * spec strips the start code.  put it back.
	 */
	if (flags & 0x8000) {
		bp -= 2;
		cc += 2;
		bp[0] = 0;
		int sbit = (flags >> 12) & 7;
		if (sbit == 0)
			bp[1] = 1;
		else {
			bp[1] = 0;
			bp[2] |= 0x100 >> sbit;
		}
	}
	u_int fmt = flags & 7;
	int hascolor = flags & 0x80;
	flags >>= 8;
	u_int sbit = (flags >> 4) & 7;
	u_int ebit = flags & 7;
	if (packets == 0) {
		tzero = ts;
		maxts = ts;
		firstseq = seqno;
		maxseq = seqno;
		seqno_ = (seqno  - 1) & 0xffff;
	}
	if (Dflag || vflag) {
		printf("packet %d bytes %d fmt %d%s sbit %d%s ebit %d%s",
			seqno, cc, fmt,
			(flags & 0x80)? " S":"", sbit,
			(flags & 0x08)? " E":"", ebit,
			hascolor? " color" : "");
		if (vflag) {
			if (aflag == 0) {
				printf(" ts %s", fmtts(ts - tzero));
			} else 
				printf(" ts 0x%08x", ts);
		}
		if (((seqno_ + 1) & 0xffff) != seqno)
			printf(" *");
		printf("\n");
	}
	register int dt = int(ts - maxts);
	register int ds = (seqno - maxseq) & 0xffff;
	if (dt > 0 || (dt == 0 && ds && ds < 32768)) {
		maxts = ts;
		if (seqno < maxseq)
			/* keep track of seq no. wraps */
			++seqwraps;
		maxseq = seqno;
	}
	seqno_ = seqno;
	++packets;

	/*
	 * if the packet contains both start & end gobs we can
	 * just render it.  Otherwise we wait until we get the
	 * start and end markers and all the packets in between.
	 */
	if ((flags & 0x88) != 0x88) {
		/*
		 * if the packet has a non-zero sbit but no SOG or
		 * a non-zero ebit but no EOG, toss it.
		 */
		if ((sbit && ((flags & 0x80) == 0)) ||
		    (ebit && ((flags & 0x08) == 0)))
			return (sts);
		/*
		 * Packets waiting for reassembly are kept in 'slots'
		 * indexed by the low bits of the sequence number.
		 * To try to avoid copying packets twice, we assume
		 * that the packet source is breaking packets at some
		 * fixed, max size (which we can determine from the size
		 * of a packet with the SOG marker) and we allocate the buffer 
		 * for slots to be this size.  As long as a packet doesn't
		 * wrap, this should make the final packets contiguous.
		 */
		if (packetsize_ != cc) {
			if (packetsize_ == 0) {
				if ((flags & 0x80) == 0)
					return (sts);
				packetsize_ = cc;
				u_int len = cc * H261_SLOTS;
				u_char* wp = (u_char*)malloc(len);
				if (wp == 0) {
					fprintf(stderr,
       "vic: h261decoder: can't get %d bytes for reassembly buffer\n", len);
					packetsize_ = 0;
					return (sts);
				}
				for (int i = 0; i < H261_SLOTS; ++i) {
					slot_[i].seqno = ~0;
					slot_[i].bp = wp;
					wp += cc;
				}
				if (len > wrkbuflen) {
					if (wrkbuflen == 0)
						wrkbuf = (u_char*)malloc(len);
					else
						wrkbuf =
						  (u_char*)realloc(wrkbuf, len);
					if (wrkbuf == 0) {
						fprintf(stderr,
	       "vic: h261decoder: can't get %d bytes for work buffer\n", len);
						free(slot_[0].bp);
						packetsize_ = 0;
						wrkbuflen = 0;
						return (sts);
					}
					wrkbuflen = len;
				}
			} else if ((flags & 0x08) == 0 || cc > packetsize_) {
				static int msgcnt;
				if (++msgcnt < 20)
					fprintf(stderr,
	"vic: h261decoder: expected %d byte frags, got %d\n", packetsize_, cc);
				return (sts);
			}
		}
		register u_int l = seqno & H261_SLOTMASK;
		h261_pslot& slot = slot_[l];
		if (slot.seqno == seqno)
			/* got a duplicate */
			return (sts);
		slot.len = cc;
		slot.seqno = seqno;
		slot.flags = flags | (hascolor << 8);
		memcpy((char*)slot.bp, (char*)bp, cc);

		/*
		 * Don't bother trying to reassemble packet unless it
		 * has an EOG marker or we have the next packet.
		 */
		if ((flags & 0x08) == 0 &&
		    ((seqno + 1) & 0xffff) !=
		     slot_[(l + 1) & H261_SLOTMASK].seqno)
			return (sts);

		/* scan backward for BOG */
		register u_int pseq = seqno;
		register u_int f = pseq & H261_SLOTMASK;
		while ((slot_[f].flags & 0x80) == 0) {
			pseq = (pseq - 1) & 0xffff;
			f = pseq & H261_SLOTMASK;
			if (slot_[f].seqno != pseq)
				/* packet missing */
				return (sts);
		}

		/* scan forward for EOG */
		while ((slot_[l].flags & 0x08) == 0) {
			seqno = (seqno + 1) & 0xffff;
			l = seqno & H261_SLOTMASK;
			if (slot_[l].seqno != seqno)
				/* packet missing */
				return (sts);
		}

		if (l < f) {
			/* packet wraps around buffer, have to copy it */
			u_char* wp = wrkbuf;
			register u_int i = f - 1;
			do {
				i = (i + 1) & H261_SLOTMASK;
				register int slen = slot_[i].len;
				memcpy((char*)wp, (char*)slot_[i].bp, slen);
				wp += slen;
			} while (i != l);
			bp = wrkbuf;
			cc = wp - bp;
		} else {
			bp = slot_[f].bp;
			cc = slot_[l].bp - bp + slot_[l].len;
		}
		hascolor = slot_[f].flags >> 8;
		sbit = (slot_[f].flags >> 4) & 7;
		ebit = slot_[l].flags & 7;
	}
	lastSeqno = seqno;
	(void)decoder->decode(bp, cc, sbit, ebit, 0, 0, 0, 0, 0);
	/*
	 * This check only works if GOBs appear in order and
	 * are not fragmented. XXX i.e., it doesn't work for RTPv2/H.261
	 */
	int eop;
	if (decoder->seenMaxGob()) {
		eop = 1;
		decoder->sync();
	} else
		eop = 0;
	++frames;
	if (Dflag || vflag)
		printf("frame %d picture %d\n", frames, pictures);
	if (! nflag && (eop || iflag)) {
		Renderer* r = renderer;
		if (width != decoder->width() ||
		    height != decoder->height()) {
			width = decoder->width();
			height = decoder->height();
			r->resize(width, height, 1);
		}
#ifdef notdef
		int x, y, w, h;
		decoder->bb(x, y, w, h);
		int off = y * decoder->width() + x;
#endif
		/*XXX*/
		u_char rvts[640 * 480 / 64];
		memset(rvts, 1, sizeof(rvts));
		r->render_frame(decoder->frame(), rvts, 0);
		sts = 1;
	}
	return (sts);
}

void finish(int sts)
{
	if (!qflag) {
		timeval end;
		gettimeofday(&end, 0);

		int expected = (seqwraps << 16) + maxseq - firstseq + 1;
		int nlost = expected - packets;
		int plost = (nlost * 100) / expected;
		fprintf(stderr,
	     "got %d packets of %d (%d%% lost), %d frames, %d pictures in %s\n",
			packets, expected, plost, frames, pictures,
			fmtts(maxts - tzero));
		double s = end.tv_sec - start.tv_sec;
		s += 1e-6 * (end.tv_usec - start.tv_usec);
		fprintf(stderr, "running time %g sec (%g fps)\n", s,
			pictures / s);
	}
	exit(sts);
}

int nextrec()
{
	if (cnt > 0 && --cnt == 0)
		finish(0);;

	if (feof(in))
		finish(0);;

	struct ivs_record_hdr {
		u_short length;
		u_short delay;
	} irh;
	fread(&irh, sizeof(irh), 1, in);
	int length = ntohs(irh.length);
	if (length > 8000 || length <= 0) {
		printf("ridiculous packet length in file\n");
		finish(1);;
	}
	u_char pktbuf[8192];
	register int len = fread(pktbuf, 1, length, in);
	if (len != length) {
		if (len > 0) {
			fprintf(stderr,
				"-truncated read: expected %d, got %d\n",
				length, len);
		}
		finish(2);;
	}
	if (ntohs(irh.delay) & 0x8000)
		/* got a session packet (ignore for now) */
		return (-1);

	return (nextpacket(pktbuf, length));
}

#ifndef USE_SHM
int use_shm = 0;
#else
int use_shm = 1;
static int
noXShm(Display* dpy, XErrorEvent*)
{
	use_shm = 0;
	fprintf(stderr, "vdd: warning: not using shared memory\n");
	return (0);
}

/*
 * XXX this is a hack to see if we can used shared memory.
 * if the X server says we can, and we\'re on the same
 * host as the X server, then we are golden.
 */
static void
checkXShm(Display* dpy)
{
	XShmSegmentInfo si;
	if ((si.shmid = shmget(IPC_PRIVATE, 512, IPC_CREAT|0777)) < 0) {
		perror("shmget");
		exit(1);
	}
	si.readOnly = 1;
	XSync(dpy, 0);
	XSetErrorHandler(noXShm);
	XShmAttach(dpy, &si);
	XSync(dpy, 0);
	XSetErrorHandler(NULL);
	(void)shmctl(si.shmid, IPC_RMID, 0);
}
#endif


void
dump_ppm(char* file, const u_char* yp, const u_char* up, const u_char* vp,
	 int w, int h)
{
	FILE *f;
	if (file == 0)
		f = stdout;
	else {
		(void)unlink(file);
		f = fopen(file, "w");
		if (f == 0)
			/*XXX*/
			return;
	}
	fprintf(f, "P3\n%d %d\n255\n", w, h);
	for (int y = 0; y < h; ++y) {
		for (int x = 0; x < w; ++x) {
			double Y = (double)yp[w * y + x];
			double U = up[(w / 2) * (y / 2) + x / 2] - 128.;
			double V = vp[(w / 2) * (y / 2) + x / 2] - 128.;
			
			int r = int(Y + 1.402 * V);
			int g = int(Y - 0.34414 * U - 0.71414 * V);
			int b = int(Y + 1.772 * U);
#define LIM(x) ((x) >= 255 ? 255 : ((x) < 0 ? 0 : (x)))
			r = LIM(r);
			g = LIM(g);
			b = LIM(b);
			fprintf(f, "%d %d %d\n", r, g, b);
		}
	}
	if (f == stdout)
		fflush(stdout);
	else
		fclose(f);
}

extern "C" char *optarg;
extern "C" int optind;
extern "C" int opterr;

const char*
disparg(int argc, const char** argv, const char* optstr)
{
	const char* display = 0;
	int op;
	while ((op = getopt(argc, (char**)argv, (char*)optstr)) != -1) {
		if (op == 'd') {
			display = optarg;
			break;
		}
		else if (op == '?')
			usage();
	}
	optind = 1;
	return (display);
}

/*
 * decoder decode $frmno
 * decoder dump [file]
 * decoder next
 * decoder frame
 */
int
cmd_decoder(ClientData cd, Tcl_Interp*, int argc, char** argv)
{
	Tcl& tcl = Tcl::instance();
	if (argc == 2) {
		if (strcmp(argv[1], "next") == 0) {
			while (nextrec() == 0) {
			}
			return (TCL_OK);
		}
		if (strcmp(argv[1], "frame") == 0) {
			char* bp = tcl.buffer();
			sprintf(bp, "%d %d", frames, pictures);
			tcl.result(bp);
			return (TCL_OK);
		}
		if (strcmp(argv[1], "nextpacket") == 0) {
			nextrec();
			return (TCL_OK);
		}
		if (strcmp(argv[1], "format") == 0) {
			tcl.result(decoder->width() == 352 ? "cif" : "qcif");
			return (TCL_OK);
		}
		if (strcmp(argv[1], "dump") == 0) {
#ifdef notyet
			dump_ppm(0,
				 decoder->yplane(),
				 decoder->uplane(),
				 decoder->vplane(), 
				 decoder->width(), decoder->height());
			return (TCL_OK);
#endif
		}
	} else if (argc == 3) {
		if (strcmp(argv[1], "dump") == 0) {
#ifdef notyet
			dump_ppm(argv[2],
				 decoder->yplane(),
				 decoder->uplane(),
				 decoder->vplane(), 
				 decoder->width(), decoder->height());
			return (TCL_OK);
#endif
		}
		if (strcmp(argv[1], "target") == 0) {
			renderer = (Renderer*)TclObject::lookup(argv[2]);
			renderer->resize(decoder->width(),
					 decoder->height(), 1);
			return (TCL_OK);
		}
	}
	return (TCL_ERROR);
}

char*
parse_assignment(char* cp)
{
	cp = strchr(cp, '=');
	if (cp != 0) {
		*cp = 0;
		return (cp + 1);
	} else
		return ("true");
}

int
main(int argc, const char **argv)
{
	Tcl::init("vdd");

	char clipvers[8];
	int op;
	int osize = 2;

	const char* optstr = "abC:c:Dd:F:inSstu:vqX:";
	const char* display = disparg(argc, argv, optstr);
	Tcl::init("vdd");
	Tcl& tcl = Tcl::instance();
	Tk_Window tk = Tk_CreateMainWindow(tcl.interp(), (char*)display,
					   "vdd", "Vdd");
	if (tk == 0) {
		fprintf(stderr, "vdd: %s\n", tcl.result());
		exit(1);
	}
	tcl.tkmain(tk);

	EmbeddedTcl::init();
	tcl.evalc("init_resources");

	while ((op = getopt(argc, (char**)argv, optstr)) != -1) {
		switch (op) {

		default:
			usage();

		case 'a':
			aflag = 1;
			break;

		case 'b':
			osize = 3;
			break;

		case 'd':
			break;

		case 'c':
			tcl.add_option("dither", optarg);
			break;

		case 'C': 
			cnt = atoi(optarg);
			break; 

		case 'F':
			dumpno = atoi(optarg);
			break;

		case 'D': 
			++Dflag;
			break; 

		case 'i':
			++iflag;
			break;

		case 'n':
			++nflag;
			break;

		case 'q':
			qflag = 1;
			break;

		case 's':
			osize = 1;
			break;

		case 'S':
			use_shm = 0;
			break;

		case 't':
			osize = 0;
			break;

		case 'u':
			tcl.add_option("startupScript", optarg);
			break;

		case 'v':
			vflag = 1;
			break;
			
		case 'X':
			{
				const char* value = parse_assignment(optarg);
				tcl.add_option(optarg, value);
			}
			break;
		}
	}
#ifdef USE_SHM
	if (use_shm)
		checkXShm(Tk_Display(tk));
#endif

	if (optind >= argc || argc <= 1)
		usage();
	const char* infile = argv[optind];

	if (Dflag)
		decoder = new P64Dumper;
	else
		decoder = new FullP64Decoder;

	/*XXX*/
	in = fopen(infile, "r");
	if (in == 0) {
		perror(infile);
		exit(1);
	}
	in = fopen(infile, "r");
	if (in == 0) {
		perror(infile);
		exit(1);
	}

	/*
	 * Check to see which of the 3 incompatible ivs_record
	 * formats this file uses.
	 * XXX seems we handle only the latest now
	 */
	op = fread(clipvers, 1, sizeof(clipvers), in);
	if (op != sizeof(clipvers)) {
		fprintf(stderr, "-%s: truncated read of %s (%d bytes)\n",
			argv[0], infile, op);
		exit(2);
	}
	if (strncmp(clipvers, "IVS CLIP", 8) == 0 ||
	    strncmp(clipvers, "RTPCLIP ", 8) == 0) {
		while ((op = getc(in)) != 0 && op != EOF) {
		}
		if (op == EOF) {
			fprintf(stderr, "-%s: unexpected eof in %s\n", argv[0],
				infile);
			exit(3);
		}
	} else {
		fseek(in, 0L, 0);
	}

	tcl.CreateCommand("decoder", cmd_decoder);
	tcl.evalf("vdd_main %d %d", 88 << osize, 72 << osize);

	Tk_MainLoop();
	exit(0);
}