summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/video/Vrec.py
blob: a78490f62607865b8773bc042b8027d43592d350 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#! /ufs/guido/bin/sgi/python
#! /ufs/guido/bin/sgi/python-405

# Capture a CMIF movie using the Indigo video library and board

# The CMIF video file format is documented in cmif-film.ms.
# Audio data is recorded in AIFF format, using the input sampling
# rate, source and volume set by the audio panel, in mono, 8
# bits/sample.


# Usage and help functions (keep this up-to-date if you change the program!)

def usage():
	print 'Usage: Vrec [options] [moviefile [audiofile]]'
	print
	print 'Options:'
	print '-a            : record audio as well'
	print '-q queuesize  : set the capture queue size (default 2)'
	print '-r rate       : capture 1 out of every "rate" frames', \
	                     '(default and min 2)'
	print '-w width      : initial window width', \
                             '(default 256, use 0 for interactive placement)'
	print '-n            : Don\'t write to file, only timing info'
	print '-d            : drop fields if needed'
	print '-g bits       : greyscale (2, 4 or 8 bits)'
	print '-G            : 2-bit greyscale dithered'
	print '-m            : monochrome dithered'
	print '-M value      : monochrome tresholded with value'
	print '-f            : Capture fields (in stead of frames)'
	print '-P frames     : preallocate space for "frames" frames'
	print 'moviefile     : here goes the movie data (default film.video)'
	print 'audiofile     : with -a, here goes the audio data', \
		  	     '(default film.aiff)'

def help():
	print 'Press the left mouse button to start recording, release it to'
	print 'end recording.  You can record as many times as you wish, but'
	print 'each recording overwrites the output file(s) -- only the last'
	print 'recording is kept.'
	print
	print 'Press ESC or use the window manager Quit or Close window option'
	print 'to quit.  If you quit before recording anything, the output'
	print 'file(s) are not touched.'


# Imported modules

import sys
sys.path.append('/ufs/guido/src/video')
import sv, SV
import VFile
import gl, GL, DEVICE
import al, AL
import time
import posix
import getopt
import string
import imageop
import sgi


# Main program

def main():
	format = SV.RGB8_FRAMES
	qsize = 2
	audio = 0
	rate = 2
	width = 0
	norecord = 0
	drop = 0
	mono = 0
	grey = 0
	greybits = 0
	monotreshold = -1
	fields = 0
	preallocspace = 0

	# Parse command line
	try:
		opts, args = getopt.getopt(sys.argv[1:], 'aq:r:w:ndg:mM:GfP:')
	except getopt.error, msg:
		sys.stdout = sys.stderr
		print 'Error:', msg, '\n'
		usage()
		sys.exit(2)

	# Interpret options
	try:
		for opt, arg in opts:
			if opt == '-a':
				audio = 1
			elif opt == '-q':
				qsize = string.atoi(arg)
			elif opt == '-r':
				rate = string.atoi(arg)
				if rate < 2:
					sys.stderr.write( \
						  '-r rate must be >= 2\n')
					sys.exit(2)
			elif opt == '-w':
				width = string.atoi(arg)
			elif opt == '-n':
				norecord = 1
			elif opt == '-d':
				drop = 1
			elif opt == '-g':
				grey = 1
				greybits = string.atoi(arg)
				if not greybits in (2, 4, 8):
					sys.stderr.write( \
				'Only 2, 4 or 8 bit greyscale supported\n')
					sys.exit(2)
			elif opt == '-G':
				grey = 1
				greybits = -2
			elif opt == '-m':
				mono = 1
			elif opt == '-M':
				mono = 1
				monotreshold = string.atoi(arg)
			elif opt == '-f':
				fields = 1
			elif opt == '-P':
				preallocspace = string.atoi(arg)
	except string.atoi_error:
		sys.stdout = sys.stderr
		print 'Option', opt, 'requires integer argument'
		sys.exit(2)

	# Check excess arguments
	# If norecord is on, refuse filename arguments
	if norecord:
		if args:
			sys.stdout = sys.stderr
			print 'With -n, no filename arguments are used\n'
			usage()
			sys.exit(2)
	elif args[2:]:
		sys.stdout = sys.stderr
		print 'Too many filename arguments\n'
		usage()
		sys.exit(2)

	# Process file arguments
	if args:
		filename = args[0]
	else:
		filename = 'film.video'

	if args[1:] and not audio:
		sys.stderr.write('-a turned on by appearance of 2nd file\n')
		audio = 1

	if audio:
		if args[1:]:
			audiofilename = args[1]
		else:
			audiofilename = 'film.aiff'
	else:
		audiofilename = None

	if norecord:
		filename = audiofilename = ''

	# Open video
	v = sv.OpenVideo()
	# Determine maximum window size based on signal standard
	param = [SV.BROADCAST, 0]
	v.GetParam(param)
	if param[1] == SV.PAL:
		x = SV.PAL_XMAX
		y = SV.PAL_YMAX
	elif param[1] == SV.NTSC:
		x = SV.NTSC_XMAX
		y = SV.NTSC_YMAX
	else:
		print 'Unknown video standard', param[1]
		sys.exit(1)

	gl.foreground()
	gl.maxsize(x, y)
	gl.keepaspect(x, y)
	gl.stepunit(8, 6)
	if width:
		height = width*3/4
		x1 = 150
		x2 = x1 + width-1
		y2 = 768-150
		y1 = y2-height+1
		gl.prefposition(x1, x2, y1, y2)
	win = gl.winopen(filename)
	if width:
		gl.maxsize(x, y)
		gl.keepaspect(x, y)
		gl.stepunit(8, 6)
		gl.winconstraints()
	x, y = gl.getsize()
	print x, 'x', y

	v.SetSize(x, y)

	if drop:
		param = [SV.FIELDDROP, 1, SV.GENLOCK, SV.GENLOCK_OFF]
	else:
		param = [SV.FIELDDROP, 0, SV.GENLOCK, SV.GENLOCK_ON]
	if mono or grey:
		param = param+[SV.COLOR, SV.MONO, SV.DITHER, 0, \
			       SV.INPUT_BYPASS, 1]
	else:
		param = param+[SV.COLOR, SV.DEFAULT_COLOR, SV.INPUT_BYPASS, 0]

	v.BindGLWindow(win, SV.IN_REPLACE)
	v.SetParam(param)

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

	help()

	while 1:
		dev, val = gl.qread()
		if dev == DEVICE.LEFTMOUSE:
			if val == 1:
				info = format, x, y, qsize, rate
				record(v, info, filename, audiofilename,\
					  mono, grey, greybits, monotreshold, \
					  fields, preallocspace)
		elif dev == DEVICE.REDRAW:
			# Window resize (or move)
			x, y = gl.getsize()
			print x, 'x', y
			v.SetSize(x, y)
			v.BindGLWindow(win, SV.IN_REPLACE)
		elif dev in (DEVICE.ESCKEY, DEVICE.WINQUIT, DEVICE.WINSHUT):
			# Quit
			v.CloseVideo()
			gl.winclose(win)
			break


# Record until the mouse is released (or any other GL event)
# XXX audio not yet supported

def record(v, info, filename, audiofilename, mono, grey, greybits, \
	  monotreshold, fields, preallocspace):
	import thread
	format, x, y, qsize, rate = info
	fps = 59.64 # Fields per second
	# XXX (Strange: need fps of Indigo monitor, not of PAL or NTSC!)
	tpf = 1000.0 / fps # Time per field in msec
	if filename:
		vout = VFile.VoutFile().init(filename)
		if mono:
			format = 'mono'
		elif grey and greybits == 8:
			format = 'grey'
		elif grey:
			format = 'grey'+`abs(greybits)`
		else:
			format = 'rgb8'
		vout.setformat(format)
		vout.setsize(x, y)
		if fields:
			vout.setpf(1, -2))
		vout.writeheader()
		if preallocspace:
			print 'Preallocating space...'
			vout.prealloc(preallocspace)
			print 'done.'
		MAXSIZE = 20 # XXX should be a user option
		import Queue
		queue = Queue.Queue().init(MAXSIZE)
		done = thread.allocate_lock()
		done.acquire_lock()
		convertor = None
		if grey:
			if greybits == 2:
				convertor = imageop.grey2grey2
			elif greybits == 4:
				convertor = imageop.grey2grey4
			elif greybits == -2:
				convertor = imageop.dither2grey2
		thread.start_new_thread(saveframes, \
			  (vout, queue, done, mono, monotreshold, convertor))
		if audiofilename:
			audiodone = thread.allocate_lock()
			audiodone.acquire_lock()
			audiostop = []
			initaudio(audiofilename, audiostop, audiodone)
	gl.wintitle('(rec) ' + filename)
	lastid = 0
	t0 = time.millitimer()
	count = 0
	ids = []
	v.InitContinuousCapture(info)
	while not gl.qtest():
		try:
			cd, id = v.GetCaptureData()
		except sv.error:
			#time.millisleep(10) # XXX is this necessary?
			sgi.nap(1)	# XXX Try by Jack
			continue
		ids.append(id)
		
		id = id + 2*rate
##		if id <> lastid + 2*rate:
##			print lastid, id
		lastid = id
		count = count+1
		if fields:
			data1, data2 = cd.GetFields()
			cd.UnlockCaptureData()
			if filename:
				queue.put((data1, int(id*tpf)))
				queue.put((data2, int((id+1)*tpf)))
		else:
			data = cd.InterleaveFields(1)
			cd.UnlockCaptureData()
			if filename:
				queue.put((data, int(id*tpf)))
	t1 = time.millitimer()
	gl.wintitle('(busy) ' + filename)
	print lastid, 'fields in', t1-t0, 'msec',
	print '--', 0.1 * int(lastid * 10000.0 / (t1-t0)), 'fields/sec'
	print 'Captured',count*2, 'fields,',
	print 0.1*int(count*20000.0/(t1-t0)), 'f/s',
	if lastid:
		print count*200.0/lastid, '%,',
		print count*rate*200.0/lastid, '% of wanted rate',
	print
	if ids:
		print 'Ids:',
		t0 = ids[0]
		del ids[0]
		for t1 in ids:
			print t1-t0,
			t0 = t1
		print
	if filename and audiofilename:
		audiostop.append(None)
		audiodone.acquire_lock()
	v.EndContinuousCapture()
	if filename:
		queue.put(None) # Sentinel
		done.acquire_lock()
	gl.wintitle('(done) ' + filename)


# Thread to save the frames to the file

def saveframes(vout, queue, done, mono, monotreshold, convertor):
	while 1:
		x = queue.get()
		if not x:
			break
		data, t = x
		if convertor:
			data = convertor(data, len(data), 1)
		elif mono and monotreshold >= 0:
			data = imageop.grey2mono(data, len(data), 1,\
				  monotreshold)
		elif mono:
			data = imageop.dither2mono(data, len(data), 1)
		vout.writeframe(t, data, None)
	sys.stderr.write('Done writing video\n')
	vout.close()
	done.release_lock()


# Initialize audio recording

AQSIZE = 8000 # XXX should be a user option

def initaudio(filename, stop, done):
	import thread, aiff
	afile = aiff.Aiff().init(filename, 'w')
	afile.nchannels = AL.MONO
	afile.sampwidth = AL.SAMPLE_8
	params = [AL.INPUT_RATE, 0]
	al.getparams(AL.DEFAULT_DEVICE, params)
	print 'audio sampling rate =', params[1]
	afile.samprate = params[1]
	c = al.newconfig()
	c.setchannels(AL.MONO)
	c.setqueuesize(AQSIZE)
	c.setwidth(AL.SAMPLE_8)
	aport = al.openport(filename, 'r', c)
	thread.start_new_thread(audiorecord, (afile, aport, stop, done))


# Thread to record audio samples

# XXX should use writesampsraw for efficiency, but then destroy doesn't
# XXX seem to set the #samples in the header correctly

def audiorecord(afile, aport, stop, done):
	while not stop:
		data = aport.readsamps(AQSIZE/2)
##		afile.writesampsraw(data)
		afile.writesamps(data)
		del data
	afile.destroy()
	print 'Done writing audio'
	done.release_lock()


# Don't forget to call the main program

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