summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/video/Vplay.py
blob: b8d06a1ff399a0212306826894c10f17dfc26741 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#! /ufs/guido/bin/sgi/python-405
#! /ufs/guido/bin/sgi/python
#! /usr/local/python

# Play CMIF movie files


# Help function

def help():
	print 'Usage: Vplay [options] [file] ...'
	print
	print 'Options:'
	print '-M magnify : magnify the image by the given factor'
	print '-d         : write some debug stuff on stderr'
	print '-l         : loop, playing the movie over and over again'
	print '-m delta   : drop frames closer than delta msec (default 0)'
	print '-q         : quiet, no informative messages'
	print '-r delta   : regenerate input time base delta msec apart'
	print '-s speed   : speed change factor (default 1.0)'
	print '-t         : use a 2nd thread for read-ahead'
	print '-x left    : window offset from left of screen'
	print '-y top     : window offset from top of screen'
	print 'file ...   : file(s) to play; default film.video'
	print
	print 'User interface:'
	print 'Press the left mouse button to stop or restart the movie.'
	print 'Press ESC or use the window manager Close or Quit command'
	print 'to close the window and play the next file (if any).'


# Imported modules

import sys
sys.path.append('/ufs/guido/src/video') # Increase chance of finding VFile
import VFile
import time
import gl, GL
from DEVICE import REDRAW, ESCKEY, LEFTMOUSE, WINSHUT, WINQUIT
import getopt
import string


# Global options

debug = 0
looping = 0
magnify = 1
mindelta = 0
nowait = 0
quiet = 0
regen = None
speed = 1.0
threading = 0
xoff = yoff = None


# Main program -- mostly command line parsing

def main():
	global debug, looping, magnify, mindelta, nowait, quiet, regen, speed
	global threading, xoff, yoff

	# Parse command line
	try:
		opts, args = getopt.getopt(sys.argv[1:], 'M:dlm:nqr:s:tx:y:')
	except getopt.error, msg:
		sys.stdout = sys.stderr
		print 'Error:', msg, '\n'
		help()
		sys.exit(2)

	# Interpret options
	try:
		for opt, arg in opts:
			if opt == '-M': magnify = string.atoi(arg)
			if opt == '-d': debug = debug + 1
			if opt == '-l': looping = 1
			if opt == '-m': mindelta = string.atoi(arg)
			if opt == '-n': nowait = 1
			if opt == '-q': quiet = 1
			if opt == '-r': regen = string.atoi(arg)
			if opt == '-s':
				try:
					speed = float(eval(arg))
				except:
					sys.stdout = sys.stderr
					print 'Option -s needs float argument'
					sys.exit(2)
			if opt == '-t':
				try:
					import thread
					threading = 1
				except ImportError:
					print 'Sorry, this version of Python',
					print 'does not support threads:',
					print '-t ignored'
			if opt == '-x': xoff = string.atoi(arg)
			if opt == '-y': yoff = string.atoi(arg)
	except string.atoi_error:
		sys.stdout = sys.stderr
		print 'Option', opt, 'require integer argument'
		sys.exit(2)

	# Check validity of certain options combinations
	if nowait and looping:
		print 'Warning: -n and -l are mutually exclusive; -n ignored'
		nowait = 0
	if xoff <> None and yoff == None:
		print 'Warning: -x without -y ignored'
	if xoff == None and yoff <> None:
		print 'Warning: -y without -x ignored'

	# Process all files
	if not args: args = ['film.video']
	sts = 0
	for filename in args:
		sts = (process(filename) or sts)

	# Exit with proper exit status
	sys.exit(sts)


# Process one movie file

def process(filename):
	try:
		vin = VFile.VinFile().init(filename)
	except IOError, msg:
		sys.stderr.write(filename + ': I/O error: ' + `msg` + '\n')
		return 1
	except VFile.Error, msg:
		sys.stderr.write(msg + '\n')
		return 1
	except EOFError:
		sys.stderr.write(filename + ': EOF in video header\n')
		return 1

	if not quiet:
		print 'File:    ', filename
		print 'Version: ', vin.version
		print 'Size:    ', vin.width, 'x', vin.height
		print 'Pack:    ', vin.packfactor, '; chrom:', vin.chrompack
		print 'Bits:    ', vin.c0bits, vin.c1bits, vin.c2bits
		print 'Format:  ', vin.format
		print 'Offset:  ', vin.offset
	
	gl.foreground()

	width, height = vin.width * magnify, vin.height * magnify
	if xoff <> None and yoff <> None:
		scrheight = gl.getgdesc(GL.GD_YPMAX)
		gl.prefposition(xoff, xoff+width-1, \
			scrheight-yoff-height, scrheight-yoff-1)
	else:
		gl.prefsize(width, height)

	win = gl.winopen(filename)
	gl.clear()

	if quiet: vin.quiet = 1
	vin.initcolormap()

	gl.qdevice(ESCKEY)
	gl.qdevice(WINSHUT)
	gl.qdevice(WINQUIT)
	gl.qdevice(LEFTMOUSE)

	stop = 0

	while not stop:
		gl.wintitle(filename)
		stop = (playonce(vin) or nowait)
		gl.wintitle('(done) ' + filename)
		if not looping:
			while not stop:
				dev, val = gl.qread()
				if dev == LEFTMOUSE and val == 1:
					break # Continue outer loop
				if dev == ESCKEY and val == 1 or \
						dev in (WINSHUT, WINQUIT):
					stop = 1

	# Set xoff, yoff for the next window from the current window
	global xoff, yoff
	xoff, yoff = gl.getorigin()
	width, height = gl.getsize()
	scrheight = gl.getgdesc(GL.GD_YPMAX)
	yoff = scrheight - yoff - height
	gl.winclose(win)

	return 0


# Play a movie once; return 1 if user wants to stop, 0 if not

def playonce(vin):
	vin.rewind()
	vin.colormapinited = 1
	vin.magnify = magnify

	if threading:
		MAXSIZE = 20 # Don't read ahead too much
		import thread
		import Queue
		queue = Queue.Queue().init(MAXSIZE)
		stop = []
		thread.start_new_thread(read_ahead, (vin, queue, stop))
		# Get the read-ahead thread going
		while queue.qsize() < MAXSIZE/2 and not stop:
			time.millisleep(100)

	tin = 0
	told = 0
	nin = 0
	nout = 0
	nlate = 0
	nskipped = 0
	data = None

	tlast = t0 = time.millitimer()

	while 1:
		if gl.qtest():
			dev, val = gl.qread()
			if dev == ESCKEY and val == 1 or \
					dev in (WINSHUT, WINQUIT) or \
					dev == LEFTMOUSE and val == 1:
				if debug: sys.stderr.write('\n')
				if threading:
					stop.append(None)
					while 1:
						item = queue.get()
						if item == None: break
				return (dev != LEFTMOUSE)
			if dev == REDRAW:
				gl.reshapeviewport()
				if data: vin.showframe(data, cdata)
		if threading:
			if debug and queue.empty(): sys.stderr.write('.')
			item = queue.get()
			if item == None: break
			tin, data, cdata = item
		else:
			try:
				tin, size, csize = vin.getnextframeheader()
			except EOFError:
				break
		nin = nin+1
		if regen: tout = nin * regen
		else: tout = tin
		tout = int(tout / speed)
		if tout - told < mindelta:
			nskipped = nskipped + 1
		else:
			if not threading:
				try:
					data, cdata = \
					  vin.getnextframedata(size, csize)
				except EOFError:
					if not quiet:
						print '[incomplete last frame]'
					break
			now = time.millitimer()
			dt = (tout-told) - (now-tlast)
			told = tout
			if debug: sys.stderr.write(`dt/10` + ' ')
			if dt < 0: nlate = nlate + 1
			if dt > 0:
				time.millisleep(dt)
				now = now + dt
			tlast = now
			vin.showframe(data, cdata)
			nout = nout + 1

	t1 = time.millitimer()

	if debug: sys.stderr.write('\n')

	if quiet: return 0

	print 'Recorded:', nin, 'frames in', tin*0.001, 'sec.',
	if tin: print '-- average', int(nin*10000.0/tin)*0.1, 'frames/sec',
	print

	if nskipped: print 'Skipped', nskipped, 'frames'

	tout = t1-t0
	print 'Played:', nout,
	print 'frames in', tout*0.001, 'sec.',
	if tout: print '-- average', int(nout*10000.0/tout)*0.1, 'frames/sec',
	print

	if nlate: print 'There were', nlate, 'late frames'

	return 0


# Read-ahead thread

def read_ahead(vin, queue, stop):
	try:
		while not stop: queue.put(vin.getnextframe())
	except EOFError:
		pass
	queue.put(None)
	stop.append(None)


# Don't forget to call the main program

try:
	main()
except KeyboardInterrupt:
	print '[Interrupt]'