blob: 1b81bd83f7ce580eecd657664aab648b56d81676 [file] [log] [blame]
Guido van Rossum5dafd911991-11-04 18:04:21 +00001import getopt
Guido van Rossume4bddea1991-10-30 11:52:48 +00002from gl import *
3from GL import *
4from DEVICE import *
5import time
6import sys
7import al
8import AL
Guido van Rossum44766701992-05-12 12:33:44 +00009
10sys.path.append('/ufs/guido/src/video') # Increase chance to find colorsys
Guido van Rossuma63f1971991-11-22 14:04:01 +000011import colorsys
Guido van Rossume4bddea1991-10-30 11:52:48 +000012
13BUFFERSIZE = 32000
14
15class Struct(): pass
16epoch = Struct()
Guido van Rossuma63f1971991-11-22 14:04:01 +000017epoch.correcttiming = 1
Guido van Rossume4bddea1991-10-30 11:52:48 +000018EndOfFile = 'End of file'
19bye = 'bye'
20
21def openspkr():
22 conf = al.newconfig()
23 conf.setqueuesize(BUFFERSIZE)
24 conf.setwidth(AL.SAMPLE_16)
25 conf.setchannels(AL.MONO)
26 return al.openport('spkr','w',conf)
Guido van Rossumb51afcc1991-11-04 14:29:27 +000027
Guido van Rossume4bddea1991-10-30 11:52:48 +000028def openvideo(name):
Guido van Rossumb51afcc1991-11-04 14:29:27 +000029 try:
30 f = open(name, 'r')
31 except:
32 sys.stderr.write(name + ': cannot open\n')
33 sys.exit(1)
Guido van Rossume4bddea1991-10-30 11:52:48 +000034 line = f.readline()
35 if not line: raise EndOfFile
Guido van Rossum2c8bf9d1992-05-06 17:58:18 +000036 colorinfo = (8, 0, 0, 0)
Guido van Rossumf47d0481992-02-11 14:50:22 +000037 if line[:4] == 'CMIF':
38 if line[:14] == 'CMIF video 2.0':
Guido van Rossuma63f1971991-11-22 14:04:01 +000039 line = f.readline()
40 colorinfo = eval(line[:-1])
Guido van Rossuma63f1971991-11-22 14:04:01 +000041 line = f.readline()
Guido van Rossume4bddea1991-10-30 11:52:48 +000042 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000043 if len(x) == 3: w, h, pf = x
Guido van Rossume4bddea1991-10-30 11:52:48 +000044 else: w, h = x; pf = 2
Guido van Rossum864cde21992-05-07 15:21:25 +000045 if pf and w/pf % 4 <> 0:
46 sys.stderr.write( \
47 'warning: stride not a multiple of 4 -- may not work on Indigo XS\n')
Guido van Rossuma63f1971991-11-22 14:04:01 +000048 return f, w, h, pf, colorinfo
Guido van Rossumb51afcc1991-11-04 14:29:27 +000049
Guido van Rossuma63f1971991-11-22 14:04:01 +000050def loadframe(f,w,h,pf,af,spkr, (ybits,ibits,qbits,chrompack),mf):
Guido van Rossume4bddea1991-10-30 11:52:48 +000051 line = f.readline()
Guido van Rossumf47d0481992-02-11 14:50:22 +000052 if line == '':
Guido van Rossume4bddea1991-10-30 11:52:48 +000053 raise EndOfFile
54 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000055 if type(x) == type(0) or type(x) == type(0.0):
Guido van Rossume4bddea1991-10-30 11:52:48 +000056 tijd = x
Guido van Rossumf47d0481992-02-11 14:50:22 +000057 if pf == 0:
Guido van Rossume4bddea1991-10-30 11:52:48 +000058 size = w*h*4
59 else:
60 size = (w/pf) * (h/pf)
61 else:
62 tijd, size = x
63 data = f.read(size)
64 if len(data) <> size:
65 raise EndOfFile
66 if pf:
Guido van Rossume4bddea1991-10-30 11:52:48 +000067 w = w/pf
68 h = h/pf
Guido van Rossuma63f1971991-11-22 14:04:01 +000069 if chrompack:
70 cw = (w+chrompack-1)/chrompack
71 ch = (h+chrompack-1)/chrompack
72 chromdata = f.read(2*cw*ch)
73 rectzoom(pf*chrompack*mf,pf*chrompack*mf)
Guido van Rossum864cde21992-05-07 15:21:25 +000074 pixmode(PM_SIZE,16)
Guido van Rossuma63f1971991-11-22 14:04:01 +000075 writemask(0x7ff - ((1<<ybits)-1))
76 lrectwrite(0,0,cw-1,ch-1,chromdata)
77 writemask((1<<ybits)-1)
Guido van Rossum864cde21992-05-07 15:21:25 +000078 pixmode(PM_SIZE,8)
Guido van Rossuma63f1971991-11-22 14:04:01 +000079 if pf:
80 rectzoom(pf*mf, pf*mf)
81 elif mf <> 1:
82 rectzoom(mf,mf)
Guido van Rossume4bddea1991-10-30 11:52:48 +000083 lrectwrite(0,0,w-1,h-1,data)
84 # This is ugly here, but the only way to get the two
85 # channels started in sync
86 #if af <> None:
87 # playsound(af,spkr)
88 ct = time.millitimer() - epoch.epoch
Guido van Rossuma63f1971991-11-22 14:04:01 +000089 if epoch.correcttiming and tijd > 0 and ct < tijd:
Guido van Rossume4bddea1991-10-30 11:52:48 +000090 time.millisleep(tijd-ct)
Guido van Rossumb51afcc1991-11-04 14:29:27 +000091 #swapbuffers()
Guido van Rossume4bddea1991-10-30 11:52:48 +000092 return tijd
Guido van Rossumb51afcc1991-11-04 14:29:27 +000093
Guido van Rossuma63f1971991-11-22 14:04:01 +000094def initcmap(ybits,ibits,qbits,chrompack):
95 if ybits+ibits+qbits > 11:
96 raise 'Sorry, 11 bits max'
97 maxy = pow(2,ybits)
98 maxi = pow(2,ibits)
99 maxq = pow(2,qbits)
100 for i in range(2048,4096-256):
101 mapcolor(i, 0, 255, 0)
102 for y in range(maxy):
103 yv = float(y)/float(maxy-1)
104 for i in range(maxi):
Guido van Rossumf47d0481992-02-11 14:50:22 +0000105 if maxi == 1: iv = 0
Guido van Rossum696f9111991-12-03 17:25:52 +0000106 else: iv = (float(i)/float(maxi-1))-0.5
Guido van Rossuma63f1971991-11-22 14:04:01 +0000107 for q in range(maxq):
Guido van Rossumf47d0481992-02-11 14:50:22 +0000108 if maxq == 1: qv = 0
Guido van Rossum696f9111991-12-03 17:25:52 +0000109 else: qv = (float(q)/float(maxq-1))-0.5
Guido van Rossuma63f1971991-11-22 14:04:01 +0000110 index = 2048 + y + (i << ybits) + (q << (ybits+ibits))
Guido van Rossuma63f1971991-11-22 14:04:01 +0000111 rv,gv,bv = colorsys.yiq_to_rgb(yv,iv,qv)
112 r,g,b = int(rv*255.0), int(gv*255.0), int(bv*255.0)
113 if index < 4096 - 256:
114 mapcolor(index, r,g,b)
115
Guido van Rossume4bddea1991-10-30 11:52:48 +0000116def playsound(af, spkr):
117 nsamp = spkr.getfillable()
118 data = af.read(nsamp*2)
119 spkr.writesamps(data)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000120
Guido van Rossume4bddea1991-10-30 11:52:48 +0000121def main():
Guido van Rossum5dafd911991-11-04 18:04:21 +0000122 looping = 0
123 packfactor = 0
Guido van Rossuma63f1971991-11-22 14:04:01 +0000124 magfactor = 1
125 try:
126 opts, args = getopt.getopt(sys.argv[1:], 'm:p:lF')
127 except getopt.error:
128 sys.stderr.write('usage: video ' + \
129 '[-l] [-p pf] [-m mag] [-F] [moviefile [soundfile [skipbytes]]]\n')
130 sys.exit(2)
Guido van Rossum5dafd911991-11-04 18:04:21 +0000131 for opt, arg in opts:
Guido van Rossumf47d0481992-02-11 14:50:22 +0000132 if opt == '-m':
Guido van Rossuma63f1971991-11-22 14:04:01 +0000133 magfactor = int(eval(arg))
Guido van Rossumf47d0481992-02-11 14:50:22 +0000134 elif opt == '-p':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000135 packfactor = int(eval(arg))
Guido van Rossumf47d0481992-02-11 14:50:22 +0000136 elif opt == '-l':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000137 looping = 1
Guido van Rossumf47d0481992-02-11 14:50:22 +0000138 elif opt == '-F':
Guido van Rossuma63f1971991-11-22 14:04:01 +0000139 epoch.correcttiming = 0
Guido van Rossum5dafd911991-11-04 18:04:21 +0000140 if args:
141 filename = args[0]
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000142 else:
143 filename = 'film.video'
Guido van Rossuma63f1971991-11-22 14:04:01 +0000144 f, w, h, pf, cinfo = openvideo(filename)
Guido van Rossum5dafd911991-11-04 18:04:21 +0000145 if 0 < packfactor <> pf:
146 w = w/pf*packfactor
147 h = h/pf*packfactor
148 pf = packfactor
149 if args[1:]:
150 audiofilename = args[1]
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000151 af = open(audiofilename, 'r')
152 spkr = openspkr()
Guido van Rossum5dafd911991-11-04 18:04:21 +0000153 afskip = 0
Guido van Rossumb3c493c1991-11-04 18:14:23 +0000154 if args[2:]:
155 afskip = eval(args[2])
156 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000157 else:
158 af, spkr = None, None
Guido van Rossum5dafd911991-11-04 18:04:21 +0000159 foreground()
Guido van Rossuma63f1971991-11-22 14:04:01 +0000160 prefsize(w*magfactor,h*magfactor)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000161 win = winopen(filename)
Guido van Rossuma63f1971991-11-22 14:04:01 +0000162 if pf:
163 #doublebuffer()
164 cmode()
165 else:
166 RGBmode()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000167 #doublebuffer()
168 gconfig()
Guido van Rossuma63f1971991-11-22 14:04:01 +0000169 if pf:
170 initcmap(cinfo)
171 color(2048)
172 clear()
173 writemask(2047)
Guido van Rossum864cde21992-05-07 15:21:25 +0000174 pixmode(PM_SIZE,8) # 8 bit pixels
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000175 qdevice(ESCKEY)
176 qdevice(WINSHUT)
177 qdevice(WINQUIT)
178 running = 1
179 epoch.epoch = time.millitimer()
180 nframe = 0
181 tijd = 1
Guido van Rossum5dafd911991-11-04 18:04:21 +0000182 if looping:
183 looping = f.tell()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000184 try:
185 while 1:
186 if running:
187 try:
Guido van Rossuma63f1971991-11-22 14:04:01 +0000188 tijd = loadframe(f, w, h, pf, af, spkr, cinfo,magfactor)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000189 nframe = nframe + 1
190 except EndOfFile:
191 running = 0
192 t = time.millitimer()
193 if tijd > 0:
194 print 'Recorded at',
195 print 0.1 * int(nframe * 10000.0 / tijd),
196 print 'frames/sec'
197 print 'Played', nframe, 'frames at',
198 print 0.1 * int(nframe * 10000.0 / (t-epoch.epoch)),
199 print 'frames/sec'
Guido van Rossum5dafd911991-11-04 18:04:21 +0000200 if looping:
201 f.seek(looping)
202 epoch.epoch = time.millitimer()
203 nframe = 0
204 running = 1
205 if af <> None:
206 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000207 if af <> None:
208 playsound(af,spkr)
209 if not running or qtest():
210 dev, val = qread()
211 if dev in (ESCKEY, WINSHUT, WINQUIT):
212 raise bye
Guido van Rossumf47d0481992-02-11 14:50:22 +0000213 elif dev == REDRAW:
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000214 reshapeviewport()
215 except bye:
216 pass
Guido van Rossume4bddea1991-10-30 11:52:48 +0000217
218main()