blob: cf994a3eb4e9af7eaa00448f5ec432bc412938cc [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 Rossuma63f1971991-11-22 14:04:01 +00009import colorsys
Guido van Rossume4bddea1991-10-30 11:52:48 +000010
11BUFFERSIZE = 32000
12
13class Struct(): pass
14epoch = Struct()
Guido van Rossuma63f1971991-11-22 14:04:01 +000015epoch.correcttiming = 1
Guido van Rossume4bddea1991-10-30 11:52:48 +000016EndOfFile = 'End of file'
17bye = 'bye'
18
19def openspkr():
20 conf = al.newconfig()
21 conf.setqueuesize(BUFFERSIZE)
22 conf.setwidth(AL.SAMPLE_16)
23 conf.setchannels(AL.MONO)
24 return al.openport('spkr','w',conf)
Guido van Rossumb51afcc1991-11-04 14:29:27 +000025
Guido van Rossume4bddea1991-10-30 11:52:48 +000026def openvideo(name):
Guido van Rossumb51afcc1991-11-04 14:29:27 +000027 try:
28 f = open(name, 'r')
29 except:
30 sys.stderr.write(name + ': cannot open\n')
31 sys.exit(1)
Guido van Rossume4bddea1991-10-30 11:52:48 +000032 line = f.readline()
33 if not line: raise EndOfFile
Guido van Rossumf47d0481992-02-11 14:50:22 +000034 if line[:4] == 'CMIF':
35 if line[:14] == 'CMIF video 2.0':
Guido van Rossuma63f1971991-11-22 14:04:01 +000036 line = f.readline()
37 colorinfo = eval(line[:-1])
38 else:
39 colorinfo = (8,0,0,0)
40 line = f.readline()
Guido van Rossume4bddea1991-10-30 11:52:48 +000041 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000042 if len(x) == 3: w, h, pf = x
Guido van Rossume4bddea1991-10-30 11:52:48 +000043 else: w, h = x; pf = 2
Guido van Rossuma63f1971991-11-22 14:04:01 +000044 return f, w, h, pf, colorinfo
Guido van Rossumb51afcc1991-11-04 14:29:27 +000045
Guido van Rossuma63f1971991-11-22 14:04:01 +000046def loadframe(f,w,h,pf,af,spkr, (ybits,ibits,qbits,chrompack),mf):
Guido van Rossume4bddea1991-10-30 11:52:48 +000047 line = f.readline()
Guido van Rossumf47d0481992-02-11 14:50:22 +000048 if line == '':
Guido van Rossume4bddea1991-10-30 11:52:48 +000049 raise EndOfFile
50 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000051 if type(x) == type(0) or type(x) == type(0.0):
Guido van Rossume4bddea1991-10-30 11:52:48 +000052 tijd = x
Guido van Rossumf47d0481992-02-11 14:50:22 +000053 if pf == 0:
Guido van Rossume4bddea1991-10-30 11:52:48 +000054 size = w*h*4
55 else:
56 size = (w/pf) * (h/pf)
57 else:
58 tijd, size = x
59 data = f.read(size)
60 if len(data) <> size:
61 raise EndOfFile
62 if pf:
Guido van Rossume4bddea1991-10-30 11:52:48 +000063 w = w/pf
64 h = h/pf
Guido van Rossuma63f1971991-11-22 14:04:01 +000065 if chrompack:
66 cw = (w+chrompack-1)/chrompack
67 ch = (h+chrompack-1)/chrompack
68 chromdata = f.read(2*cw*ch)
69 rectzoom(pf*chrompack*mf,pf*chrompack*mf)
70 pixmode(5,16)
71 writemask(0x7ff - ((1<<ybits)-1))
72 lrectwrite(0,0,cw-1,ch-1,chromdata)
73 writemask((1<<ybits)-1)
74 pixmode(5,8)
75 if pf:
76 rectzoom(pf*mf, pf*mf)
77 elif mf <> 1:
78 rectzoom(mf,mf)
Guido van Rossume4bddea1991-10-30 11:52:48 +000079 lrectwrite(0,0,w-1,h-1,data)
80 # This is ugly here, but the only way to get the two
81 # channels started in sync
82 #if af <> None:
83 # playsound(af,spkr)
84 ct = time.millitimer() - epoch.epoch
Guido van Rossuma63f1971991-11-22 14:04:01 +000085 if epoch.correcttiming and tijd > 0 and ct < tijd:
Guido van Rossume4bddea1991-10-30 11:52:48 +000086 time.millisleep(tijd-ct)
Guido van Rossumb51afcc1991-11-04 14:29:27 +000087 #swapbuffers()
Guido van Rossume4bddea1991-10-30 11:52:48 +000088 return tijd
Guido van Rossumb51afcc1991-11-04 14:29:27 +000089
Guido van Rossuma63f1971991-11-22 14:04:01 +000090def initcmap(ybits,ibits,qbits,chrompack):
91 if ybits+ibits+qbits > 11:
92 raise 'Sorry, 11 bits max'
93 maxy = pow(2,ybits)
94 maxi = pow(2,ibits)
95 maxq = pow(2,qbits)
96 for i in range(2048,4096-256):
97 mapcolor(i, 0, 255, 0)
98 for y in range(maxy):
99 yv = float(y)/float(maxy-1)
100 for i in range(maxi):
Guido van Rossumf47d0481992-02-11 14:50:22 +0000101 if maxi == 1: iv = 0
Guido van Rossum696f9111991-12-03 17:25:52 +0000102 else: iv = (float(i)/float(maxi-1))-0.5
Guido van Rossuma63f1971991-11-22 14:04:01 +0000103 for q in range(maxq):
Guido van Rossumf47d0481992-02-11 14:50:22 +0000104 if maxq == 1: qv = 0
Guido van Rossum696f9111991-12-03 17:25:52 +0000105 else: qv = (float(q)/float(maxq-1))-0.5
Guido van Rossuma63f1971991-11-22 14:04:01 +0000106 index = 2048 + y + (i << ybits) + (q << (ybits+ibits))
Guido van Rossuma63f1971991-11-22 14:04:01 +0000107 rv,gv,bv = colorsys.yiq_to_rgb(yv,iv,qv)
108 r,g,b = int(rv*255.0), int(gv*255.0), int(bv*255.0)
109 if index < 4096 - 256:
110 mapcolor(index, r,g,b)
111
Guido van Rossume4bddea1991-10-30 11:52:48 +0000112def playsound(af, spkr):
113 nsamp = spkr.getfillable()
114 data = af.read(nsamp*2)
115 spkr.writesamps(data)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000116
Guido van Rossume4bddea1991-10-30 11:52:48 +0000117def main():
Guido van Rossum5dafd911991-11-04 18:04:21 +0000118 looping = 0
119 packfactor = 0
Guido van Rossuma63f1971991-11-22 14:04:01 +0000120 magfactor = 1
121 try:
122 opts, args = getopt.getopt(sys.argv[1:], 'm:p:lF')
123 except getopt.error:
124 sys.stderr.write('usage: video ' + \
125 '[-l] [-p pf] [-m mag] [-F] [moviefile [soundfile [skipbytes]]]\n')
126 sys.exit(2)
Guido van Rossum5dafd911991-11-04 18:04:21 +0000127 for opt, arg in opts:
Guido van Rossumf47d0481992-02-11 14:50:22 +0000128 if opt == '-m':
Guido van Rossuma63f1971991-11-22 14:04:01 +0000129 magfactor = int(eval(arg))
Guido van Rossumf47d0481992-02-11 14:50:22 +0000130 elif opt == '-p':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000131 packfactor = int(eval(arg))
Guido van Rossumf47d0481992-02-11 14:50:22 +0000132 elif opt == '-l':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000133 looping = 1
Guido van Rossumf47d0481992-02-11 14:50:22 +0000134 elif opt == '-F':
Guido van Rossuma63f1971991-11-22 14:04:01 +0000135 epoch.correcttiming = 0
Guido van Rossum5dafd911991-11-04 18:04:21 +0000136 if args:
137 filename = args[0]
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000138 else:
139 filename = 'film.video'
Guido van Rossuma63f1971991-11-22 14:04:01 +0000140 f, w, h, pf, cinfo = openvideo(filename)
Guido van Rossum5dafd911991-11-04 18:04:21 +0000141 if 0 < packfactor <> pf:
142 w = w/pf*packfactor
143 h = h/pf*packfactor
144 pf = packfactor
145 if args[1:]:
146 audiofilename = args[1]
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000147 af = open(audiofilename, 'r')
148 spkr = openspkr()
Guido van Rossum5dafd911991-11-04 18:04:21 +0000149 afskip = 0
Guido van Rossumb3c493c1991-11-04 18:14:23 +0000150 if args[2:]:
151 afskip = eval(args[2])
152 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000153 else:
154 af, spkr = None, None
Guido van Rossum5dafd911991-11-04 18:04:21 +0000155 foreground()
Guido van Rossuma63f1971991-11-22 14:04:01 +0000156 prefsize(w*magfactor,h*magfactor)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000157 win = winopen(filename)
Guido van Rossuma63f1971991-11-22 14:04:01 +0000158 if pf:
159 #doublebuffer()
160 cmode()
161 else:
162 RGBmode()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000163 #doublebuffer()
164 gconfig()
Guido van Rossuma63f1971991-11-22 14:04:01 +0000165 if pf:
166 initcmap(cinfo)
167 color(2048)
168 clear()
169 writemask(2047)
170 pixmode(5,8) # 8 bit pixels
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000171 qdevice(ESCKEY)
172 qdevice(WINSHUT)
173 qdevice(WINQUIT)
174 running = 1
175 epoch.epoch = time.millitimer()
176 nframe = 0
177 tijd = 1
Guido van Rossum5dafd911991-11-04 18:04:21 +0000178 if looping:
179 looping = f.tell()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000180 try:
181 while 1:
182 if running:
183 try:
Guido van Rossuma63f1971991-11-22 14:04:01 +0000184 tijd = loadframe(f, w, h, pf, af, spkr, cinfo,magfactor)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000185 nframe = nframe + 1
186 except EndOfFile:
187 running = 0
188 t = time.millitimer()
189 if tijd > 0:
190 print 'Recorded at',
191 print 0.1 * int(nframe * 10000.0 / tijd),
192 print 'frames/sec'
193 print 'Played', nframe, 'frames at',
194 print 0.1 * int(nframe * 10000.0 / (t-epoch.epoch)),
195 print 'frames/sec'
Guido van Rossum5dafd911991-11-04 18:04:21 +0000196 if looping:
197 f.seek(looping)
198 epoch.epoch = time.millitimer()
199 nframe = 0
200 running = 1
201 if af <> None:
202 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000203 if af <> None:
204 playsound(af,spkr)
205 if not running or qtest():
206 dev, val = qread()
207 if dev in (ESCKEY, WINSHUT, WINQUIT):
208 raise bye
Guido van Rossumf47d0481992-02-11 14:50:22 +0000209 elif dev == REDRAW:
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000210 reshapeviewport()
211 except bye:
212 pass
Guido van Rossume4bddea1991-10-30 11:52:48 +0000213
214main()