blob: d9872d5ef052ba0957b765f323cd89f7e764e21d [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 Rossuma63f1971991-11-22 14:04:01 +000034 if line[:4] = 'CMIF':
35 if line[:14] = 'CMIF video 2.0':
36 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])
42 if len(x) = 3: w, h, pf = x
43 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()
48 if line = '':
49 raise EndOfFile
50 x = eval(line[:-1])
51 if type(x) = type(0) or type(x) = type(0.0):
52 tijd = x
53 if pf = 0:
54 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):
101 iv = (float(i)/float(maxi-1))-0.5
102 for q in range(maxq):
103 qv = (float(q)/float(maxq-1))-0.5
104 index = 2048 + y + (i << ybits) + (q << (ybits+ibits))
105
106 rv,gv,bv = colorsys.yiq_to_rgb(yv,iv,qv)
107 r,g,b = int(rv*255.0), int(gv*255.0), int(bv*255.0)
108 if index < 4096 - 256:
109 mapcolor(index, r,g,b)
110
Guido van Rossume4bddea1991-10-30 11:52:48 +0000111def playsound(af, spkr):
112 nsamp = spkr.getfillable()
113 data = af.read(nsamp*2)
114 spkr.writesamps(data)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000115
Guido van Rossume4bddea1991-10-30 11:52:48 +0000116def main():
Guido van Rossum5dafd911991-11-04 18:04:21 +0000117 looping = 0
118 packfactor = 0
Guido van Rossuma63f1971991-11-22 14:04:01 +0000119 magfactor = 1
120 try:
121 opts, args = getopt.getopt(sys.argv[1:], 'm:p:lF')
122 except getopt.error:
123 sys.stderr.write('usage: video ' + \
124 '[-l] [-p pf] [-m mag] [-F] [moviefile [soundfile [skipbytes]]]\n')
125 sys.exit(2)
Guido van Rossum5dafd911991-11-04 18:04:21 +0000126 for opt, arg in opts:
Guido van Rossuma63f1971991-11-22 14:04:01 +0000127 if opt = '-m':
128 magfactor = int(eval(arg))
129 elif opt = '-p':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000130 packfactor = int(eval(arg))
131 elif opt = '-l':
132 looping = 1
Guido van Rossuma63f1971991-11-22 14:04:01 +0000133 elif opt = '-F':
134 epoch.correcttiming = 0
Guido van Rossum5dafd911991-11-04 18:04:21 +0000135 if args:
136 filename = args[0]
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000137 else:
138 filename = 'film.video'
Guido van Rossuma63f1971991-11-22 14:04:01 +0000139 f, w, h, pf, cinfo = openvideo(filename)
Guido van Rossum5dafd911991-11-04 18:04:21 +0000140 if 0 < packfactor <> pf:
141 w = w/pf*packfactor
142 h = h/pf*packfactor
143 pf = packfactor
144 if args[1:]:
145 audiofilename = args[1]
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000146 af = open(audiofilename, 'r')
147 spkr = openspkr()
Guido van Rossum5dafd911991-11-04 18:04:21 +0000148 afskip = 0
Guido van Rossumb3c493c1991-11-04 18:14:23 +0000149 if args[2:]:
150 afskip = eval(args[2])
151 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000152 else:
153 af, spkr = None, None
Guido van Rossum5dafd911991-11-04 18:04:21 +0000154 foreground()
Guido van Rossuma63f1971991-11-22 14:04:01 +0000155 prefsize(w*magfactor,h*magfactor)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000156 win = winopen(filename)
Guido van Rossuma63f1971991-11-22 14:04:01 +0000157 if pf:
158 #doublebuffer()
159 cmode()
160 else:
161 RGBmode()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000162 #doublebuffer()
163 gconfig()
Guido van Rossuma63f1971991-11-22 14:04:01 +0000164 if pf:
165 initcmap(cinfo)
166 color(2048)
167 clear()
168 writemask(2047)
169 pixmode(5,8) # 8 bit pixels
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000170 qdevice(ESCKEY)
171 qdevice(WINSHUT)
172 qdevice(WINQUIT)
173 running = 1
174 epoch.epoch = time.millitimer()
175 nframe = 0
176 tijd = 1
Guido van Rossum5dafd911991-11-04 18:04:21 +0000177 if looping:
178 looping = f.tell()
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000179 try:
180 while 1:
181 if running:
182 try:
Guido van Rossuma63f1971991-11-22 14:04:01 +0000183 tijd = loadframe(f, w, h, pf, af, spkr, cinfo,magfactor)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000184 nframe = nframe + 1
185 except EndOfFile:
186 running = 0
187 t = time.millitimer()
188 if tijd > 0:
189 print 'Recorded at',
190 print 0.1 * int(nframe * 10000.0 / tijd),
191 print 'frames/sec'
192 print 'Played', nframe, 'frames at',
193 print 0.1 * int(nframe * 10000.0 / (t-epoch.epoch)),
194 print 'frames/sec'
Guido van Rossum5dafd911991-11-04 18:04:21 +0000195 if looping:
196 f.seek(looping)
197 epoch.epoch = time.millitimer()
198 nframe = 0
199 running = 1
200 if af <> None:
201 af.seek(afskip)
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000202 if af <> None:
203 playsound(af,spkr)
204 if not running or qtest():
205 dev, val = qread()
206 if dev in (ESCKEY, WINSHUT, WINQUIT):
207 raise bye
208 elif dev = REDRAW:
209 reshapeviewport()
210 except bye:
211 pass
Guido van Rossume4bddea1991-10-30 11:52:48 +0000212
213main()