blob: 4469329444a07167ccaeb83af8529e7bfb12b173 [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 Rossum2c8bf9d1992-05-06 17:58:18 +000034 colorinfo = (8, 0, 0, 0)
Guido van Rossumf47d0481992-02-11 14:50:22 +000035 if line[:4] == 'CMIF':
36 if line[:14] == 'CMIF video 2.0':
Guido van Rossuma63f1971991-11-22 14:04:01 +000037 line = f.readline()
38 colorinfo = eval(line[:-1])
Guido van Rossuma63f1971991-11-22 14:04:01 +000039 line = f.readline()
Guido van Rossume4bddea1991-10-30 11:52:48 +000040 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000041 if len(x) == 3: w, h, pf = x
Guido van Rossume4bddea1991-10-30 11:52:48 +000042 else: w, h = x; pf = 2
Guido van Rossum864cde21992-05-07 15:21:25 +000043 if pf and w/pf % 4 <> 0:
44 sys.stderr.write( \
45 'warning: stride not a multiple of 4 -- may not work on Indigo XS\n')
Guido van Rossuma63f1971991-11-22 14:04:01 +000046 return f, w, h, pf, colorinfo
Guido van Rossumb51afcc1991-11-04 14:29:27 +000047
Guido van Rossuma63f1971991-11-22 14:04:01 +000048def loadframe(f,w,h,pf,af,spkr, (ybits,ibits,qbits,chrompack),mf):
Guido van Rossume4bddea1991-10-30 11:52:48 +000049 line = f.readline()
Guido van Rossumf47d0481992-02-11 14:50:22 +000050 if line == '':
Guido van Rossume4bddea1991-10-30 11:52:48 +000051 raise EndOfFile
52 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000053 if type(x) == type(0) or type(x) == type(0.0):
Guido van Rossume4bddea1991-10-30 11:52:48 +000054 tijd = x
Guido van Rossumf47d0481992-02-11 14:50:22 +000055 if pf == 0:
Guido van Rossume4bddea1991-10-30 11:52:48 +000056 size = w*h*4
57 else:
58 size = (w/pf) * (h/pf)
59 else:
60 tijd, size = x
61 data = f.read(size)
62 if len(data) <> size:
63 raise EndOfFile
64 if pf:
Guido van Rossume4bddea1991-10-30 11:52:48 +000065 w = w/pf
66 h = h/pf
Guido van Rossuma63f1971991-11-22 14:04:01 +000067 if chrompack:
68 cw = (w+chrompack-1)/chrompack
69 ch = (h+chrompack-1)/chrompack
70 chromdata = f.read(2*cw*ch)
71 rectzoom(pf*chrompack*mf,pf*chrompack*mf)
Guido van Rossum864cde21992-05-07 15:21:25 +000072 pixmode(PM_SIZE,16)
Guido van Rossuma63f1971991-11-22 14:04:01 +000073 writemask(0x7ff - ((1<<ybits)-1))
74 lrectwrite(0,0,cw-1,ch-1,chromdata)
75 writemask((1<<ybits)-1)
Guido van Rossum864cde21992-05-07 15:21:25 +000076 pixmode(PM_SIZE,8)
Guido van Rossuma63f1971991-11-22 14:04:01 +000077 if pf:
78 rectzoom(pf*mf, pf*mf)
79 elif mf <> 1:
80 rectzoom(mf,mf)
Guido van Rossume4bddea1991-10-30 11:52:48 +000081 lrectwrite(0,0,w-1,h-1,data)
82 # This is ugly here, but the only way to get the two
83 # channels started in sync
84 #if af <> None:
85 # playsound(af,spkr)
86 ct = time.millitimer() - epoch.epoch
Guido van Rossuma63f1971991-11-22 14:04:01 +000087 if epoch.correcttiming and tijd > 0 and ct < tijd:
Guido van Rossume4bddea1991-10-30 11:52:48 +000088 time.millisleep(tijd-ct)
Guido van Rossumb51afcc1991-11-04 14:29:27 +000089 #swapbuffers()
Guido van Rossume4bddea1991-10-30 11:52:48 +000090 return tijd
Guido van Rossumb51afcc1991-11-04 14:29:27 +000091
Guido van Rossuma63f1971991-11-22 14:04:01 +000092def initcmap(ybits,ibits,qbits,chrompack):
93 if ybits+ibits+qbits > 11:
94 raise 'Sorry, 11 bits max'
95 maxy = pow(2,ybits)
96 maxi = pow(2,ibits)
97 maxq = pow(2,qbits)
98 for i in range(2048,4096-256):
99 mapcolor(i, 0, 255, 0)
100 for y in range(maxy):
101 yv = float(y)/float(maxy-1)
102 for i in range(maxi):
Guido van Rossumf47d0481992-02-11 14:50:22 +0000103 if maxi == 1: iv = 0
Guido van Rossum696f9111991-12-03 17:25:52 +0000104 else: iv = (float(i)/float(maxi-1))-0.5
Guido van Rossuma63f1971991-11-22 14:04:01 +0000105 for q in range(maxq):
Guido van Rossumf47d0481992-02-11 14:50:22 +0000106 if maxq == 1: qv = 0
Guido van Rossum696f9111991-12-03 17:25:52 +0000107 else: qv = (float(q)/float(maxq-1))-0.5
Guido van Rossuma63f1971991-11-22 14:04:01 +0000108 index = 2048 + y + (i << ybits) + (q << (ybits+ibits))
Guido van Rossuma63f1971991-11-22 14:04:01 +0000109 rv,gv,bv = colorsys.yiq_to_rgb(yv,iv,qv)
110 r,g,b = int(rv*255.0), int(gv*255.0), int(bv*255.0)
111 if index < 4096 - 256:
112 mapcolor(index, r,g,b)
113
Guido van Rossume4bddea1991-10-30 11:52:48 +0000114def playsound(af, spkr):
115 nsamp = spkr.getfillable()
116 data = af.read(nsamp*2)
117 spkr.writesamps(data)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000118
Guido van Rossume4bddea1991-10-30 11:52:48 +0000119def main():
Guido van Rossum5dafd911991-11-04 18:04:21 +0000120 looping = 0
121 packfactor = 0
Guido van Rossuma63f1971991-11-22 14:04:01 +0000122 magfactor = 1
123 try:
124 opts, args = getopt.getopt(sys.argv[1:], 'm:p:lF')
125 except getopt.error:
126 sys.stderr.write('usage: video ' + \
127 '[-l] [-p pf] [-m mag] [-F] [moviefile [soundfile [skipbytes]]]\n')
128 sys.exit(2)
Guido van Rossum5dafd911991-11-04 18:04:21 +0000129 for opt, arg in opts:
Guido van Rossumf47d0481992-02-11 14:50:22 +0000130 if opt == '-m':
Guido van Rossuma63f1971991-11-22 14:04:01 +0000131 magfactor = int(eval(arg))
Guido van Rossumf47d0481992-02-11 14:50:22 +0000132 elif opt == '-p':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000133 packfactor = int(eval(arg))
Guido van Rossumf47d0481992-02-11 14:50:22 +0000134 elif opt == '-l':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000135 looping = 1
Guido van Rossumf47d0481992-02-11 14:50:22 +0000136 elif opt == '-F':
Guido van Rossuma63f1971991-11-22 14:04:01 +0000137 epoch.correcttiming = 0
Guido van Rossum5dafd911991-11-04 18:04:21 +0000138 if args:
139 filename = args[0]
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000140 else:
141 filename = 'film.video'
Guido van Rossuma63f1971991-11-22 14:04:01 +0000142 f, w, h, pf, cinfo = openvideo(filename)
Guido van Rossum5dafd911991-11-04 18:04:21 +0000143 if 0 < packfactor <> pf:
144 w = w/pf*packfactor
145 h = h/pf*packfactor
146 pf = packfactor
147 if args[1:]:
148 audiofilename = args[1]
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000149 af = open(audiofilename, 'r')
150 spkr = openspkr()
Guido van Rossum5dafd911991-11-04 18:04:21 +0000151 afskip = 0
Guido van Rossumb3c493c1991-11-04 18:14:23 +0000152 if args[2:]:
153 afskip = eval(args[2])
154 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000155 else:
156 af, spkr = None, None
Guido van Rossum5dafd911991-11-04 18:04:21 +0000157 foreground()
Guido van Rossuma63f1971991-11-22 14:04:01 +0000158 prefsize(w*magfactor,h*magfactor)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000159 win = winopen(filename)
Guido van Rossuma63f1971991-11-22 14:04:01 +0000160 if pf:
161 #doublebuffer()
162 cmode()
163 else:
164 RGBmode()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000165 #doublebuffer()
166 gconfig()
Guido van Rossuma63f1971991-11-22 14:04:01 +0000167 if pf:
168 initcmap(cinfo)
169 color(2048)
170 clear()
171 writemask(2047)
Guido van Rossum864cde21992-05-07 15:21:25 +0000172 pixmode(PM_SIZE,8) # 8 bit pixels
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000173 qdevice(ESCKEY)
174 qdevice(WINSHUT)
175 qdevice(WINQUIT)
176 running = 1
177 epoch.epoch = time.millitimer()
178 nframe = 0
179 tijd = 1
Guido van Rossum5dafd911991-11-04 18:04:21 +0000180 if looping:
181 looping = f.tell()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000182 try:
183 while 1:
184 if running:
185 try:
Guido van Rossuma63f1971991-11-22 14:04:01 +0000186 tijd = loadframe(f, w, h, pf, af, spkr, cinfo,magfactor)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000187 nframe = nframe + 1
188 except EndOfFile:
189 running = 0
190 t = time.millitimer()
191 if tijd > 0:
192 print 'Recorded at',
193 print 0.1 * int(nframe * 10000.0 / tijd),
194 print 'frames/sec'
195 print 'Played', nframe, 'frames at',
196 print 0.1 * int(nframe * 10000.0 / (t-epoch.epoch)),
197 print 'frames/sec'
Guido van Rossum5dafd911991-11-04 18:04:21 +0000198 if looping:
199 f.seek(looping)
200 epoch.epoch = time.millitimer()
201 nframe = 0
202 running = 1
203 if af <> None:
204 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000205 if af <> None:
206 playsound(af,spkr)
207 if not running or qtest():
208 dev, val = qread()
209 if dev in (ESCKEY, WINSHUT, WINQUIT):
210 raise bye
Guido van Rossumf47d0481992-02-11 14:50:22 +0000211 elif dev == REDRAW:
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000212 reshapeviewport()
213 except bye:
214 pass
Guido van Rossume4bddea1991-10-30 11:52:48 +0000215
216main()