blob: 2b50e396587079d59a87d2298248e000c9eaec2c [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 Rossuma63f1971991-11-22 14:04:01 +000043 return f, w, h, pf, colorinfo
Guido van Rossumb51afcc1991-11-04 14:29:27 +000044
Guido van Rossuma63f1971991-11-22 14:04:01 +000045def loadframe(f,w,h,pf,af,spkr, (ybits,ibits,qbits,chrompack),mf):
Guido van Rossume4bddea1991-10-30 11:52:48 +000046 line = f.readline()
Guido van Rossumf47d0481992-02-11 14:50:22 +000047 if line == '':
Guido van Rossume4bddea1991-10-30 11:52:48 +000048 raise EndOfFile
49 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000050 if type(x) == type(0) or type(x) == type(0.0):
Guido van Rossume4bddea1991-10-30 11:52:48 +000051 tijd = x
Guido van Rossumf47d0481992-02-11 14:50:22 +000052 if pf == 0:
Guido van Rossume4bddea1991-10-30 11:52:48 +000053 size = w*h*4
54 else:
55 size = (w/pf) * (h/pf)
56 else:
57 tijd, size = x
58 data = f.read(size)
59 if len(data) <> size:
60 raise EndOfFile
61 if pf:
Guido van Rossume4bddea1991-10-30 11:52:48 +000062 w = w/pf
63 h = h/pf
Guido van Rossuma63f1971991-11-22 14:04:01 +000064 if chrompack:
65 cw = (w+chrompack-1)/chrompack
66 ch = (h+chrompack-1)/chrompack
67 chromdata = f.read(2*cw*ch)
68 rectzoom(pf*chrompack*mf,pf*chrompack*mf)
69 pixmode(5,16)
70 writemask(0x7ff - ((1<<ybits)-1))
71 lrectwrite(0,0,cw-1,ch-1,chromdata)
72 writemask((1<<ybits)-1)
73 pixmode(5,8)
74 if pf:
75 rectzoom(pf*mf, pf*mf)
76 elif mf <> 1:
77 rectzoom(mf,mf)
Guido van Rossume4bddea1991-10-30 11:52:48 +000078 lrectwrite(0,0,w-1,h-1,data)
79 # This is ugly here, but the only way to get the two
80 # channels started in sync
81 #if af <> None:
82 # playsound(af,spkr)
83 ct = time.millitimer() - epoch.epoch
Guido van Rossuma63f1971991-11-22 14:04:01 +000084 if epoch.correcttiming and tijd > 0 and ct < tijd:
Guido van Rossume4bddea1991-10-30 11:52:48 +000085 time.millisleep(tijd-ct)
Guido van Rossumb51afcc1991-11-04 14:29:27 +000086 #swapbuffers()
Guido van Rossume4bddea1991-10-30 11:52:48 +000087 return tijd
Guido van Rossumb51afcc1991-11-04 14:29:27 +000088
Guido van Rossuma63f1971991-11-22 14:04:01 +000089def initcmap(ybits,ibits,qbits,chrompack):
90 if ybits+ibits+qbits > 11:
91 raise 'Sorry, 11 bits max'
92 maxy = pow(2,ybits)
93 maxi = pow(2,ibits)
94 maxq = pow(2,qbits)
95 for i in range(2048,4096-256):
96 mapcolor(i, 0, 255, 0)
97 for y in range(maxy):
98 yv = float(y)/float(maxy-1)
99 for i in range(maxi):
Guido van Rossumf47d0481992-02-11 14:50:22 +0000100 if maxi == 1: iv = 0
Guido van Rossum696f9111991-12-03 17:25:52 +0000101 else: iv = (float(i)/float(maxi-1))-0.5
Guido van Rossuma63f1971991-11-22 14:04:01 +0000102 for q in range(maxq):
Guido van Rossumf47d0481992-02-11 14:50:22 +0000103 if maxq == 1: qv = 0
Guido van Rossum696f9111991-12-03 17:25:52 +0000104 else: qv = (float(q)/float(maxq-1))-0.5
Guido van Rossuma63f1971991-11-22 14:04:01 +0000105 index = 2048 + y + (i << ybits) + (q << (ybits+ibits))
Guido van Rossuma63f1971991-11-22 14:04:01 +0000106 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 Rossumf47d0481992-02-11 14:50:22 +0000127 if opt == '-m':
Guido van Rossuma63f1971991-11-22 14:04:01 +0000128 magfactor = int(eval(arg))
Guido van Rossumf47d0481992-02-11 14:50:22 +0000129 elif opt == '-p':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000130 packfactor = int(eval(arg))
Guido van Rossumf47d0481992-02-11 14:50:22 +0000131 elif opt == '-l':
Guido van Rossum5dafd911991-11-04 18:04:21 +0000132 looping = 1
Guido van Rossumf47d0481992-02-11 14:50:22 +0000133 elif opt == '-F':
Guido van Rossuma63f1971991-11-22 14:04:01 +0000134 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
Guido van Rossumf47d0481992-02-11 14:50:22 +0000208 elif dev == REDRAW:
Guido van Rossumb51afcc1991-11-04 14:29:27 +0000209 reshapeviewport()
210 except bye:
211 pass
Guido van Rossume4bddea1991-10-30 11:52:48 +0000212
213main()