summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/video/Vplay.py
blob: 1ad6713f49b91f7bb7f30559c0cdc2eb16f82103 (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
#! /usr/local/python

# Play CMIF movie files


# Usage:
#
# Vplay [-L] [-M maginfy] [-m msec] [-q] [-r msec] [-s factor] [file] ...


# Options:
#
# -L         : loop, playing the movie over and over again
# -M magnify : magnify the image by the given factor
# -m n       : drop frames closer than n msec (default 0)
# -q         : quiet, no informative messages
# -r n       : regenerate input time base n msec apart
# -s speed   : speed change factor after other processing (default 1.0)
# file ...   : file(s) to play; default film.video


# User interface:
#
# Place the windo where you want it.  The size is determined by the
# movie file and the -m option.
#
# Press ESC or select the window manager Quit or Close window option
# to close a window; if more files are given the window for the next
# file now pops up.  Unless looping, the window closes automatically
# at the end of the movie.


import sys
sys.path.append('/ufs/guido/src/video')
import VFile
import time
import gl, GL
from DEVICE import *
import getopt
import string


# Global options

magnify = 1
looping = 0
speed = 1.0
mindelta = 0
quiet = 0
regen = None


# Main program -- mostly command line parsing

def main():
	global magnify, looping, speed, mindelta, quiet, regen
	opts, args = getopt.getopt(sys.argv[1:], 'LM:m:qr:s:')
	for opt, arg in opts:
		if opt == '-L':
			looping = 1
		elif opt == '-M':
			magnify = string.atoi(arg)
		elif opt == '-m':
			mindelta = string.atoi(arg)
		elif opt == '-q':
			quiet = 1
		elif opt == '-r':
			regen = string.atoi(arg)
		elif opt == '-s':
			speed = float(eval(arg))
	if not args:
		args = ['film.video']
	for filename in args:
		process(filename)


# 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
	except VFile.Error, msg:
		sys.stderr.write(msg + '\n')
		return
	except EOFError:
		sys.stderr.write(filename + ': EOF in video header\n')
		return

	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()
	gl.prefsize(vin.width * magnify, vin.height * magnify)
	win = gl.winopen(filename)
	if quiet:
		vin.quiet = 1
	vin.initcolormap()

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

	while 1:
		stop = playonce(vin)
		if stop or not looping:
			break

	gl.wintitle('(done) ' + filename)

	while not stop:
		dev, val = gl.qread()
		if dev in (ESCKEY, WINSHUT, WINQUIT):
			stop = 1

	gl.winclose(win)


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

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

	tin = 0
	told = 0
	nin = 0
	nout = 0
	nmissed = 0
	nskipped = 0

	t0 = time.millitimer()

	while 1:
		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:
			try:
				vin.skipnextframedata(size, csize)
			except EOFError:
				print '[short data at EOF]'
				break
			nskipped = nskipped + 1
		else:
			told = tout
			dt = tout + t0 - time.millitimer()
			if dt < 0:
				try:
					vin.skipnextframedata(size, csize)
				except EOFError:
					print '[short data at EOF]'
					break
				nmissed = nmissed + 1
			else:
				try:
					data, cdata = vin.getnextframedata \
						(size, csize)
				except EOFError:
					print '[short data at EOF]'
					break
				dt = tout + t0 - time.millitimer()
				if dt > 0:
					time.millisleep(dt)
				vin.showframe(data, cdata)
				nout = nout + 1
		if gl.qtest():
			dev, val = gl.qread()
			if dev in (ESCKEY, WINSHUT, WINQUIT):
				return 1
			if dev == REDRAW:
				gl.reshapeviewport()
				if data:
					vin.showframe(data, cdata)

	t1 = time.millitimer()

	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 nmissed:
		print 'Missed', nmissed, 'frames'

	return 0


# Don't forget to call the main program

main()