1992-08-20 11:46:28 +00:00
|
|
|
#! /ufs/guido/bin/sgi/python
|
1992-12-24 11:39:00 +00:00
|
|
|
#! /ufs/guido/bin/sgi/python-405
|
1992-08-20 11:46:28 +00:00
|
|
|
|
|
|
|
# Capture a CMIF movie using the Indigo video library and board
|
|
|
|
|
1992-12-24 11:39:00 +00:00
|
|
|
# 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 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
|
1992-08-20 11:46:28 +00:00
|
|
|
|
|
|
|
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
|
1992-12-14 12:25:21 +00:00
|
|
|
import imageop
|
1992-12-23 15:37:20 +00:00
|
|
|
import sgi
|
1992-08-20 11:46:28 +00:00
|
|
|
|
1992-12-24 11:39:00 +00:00
|
|
|
|
1992-08-20 11:46:28 +00:00
|
|
|
# Main program
|
|
|
|
|
|
|
|
def main():
|
1992-09-03 16:56:04 +00:00
|
|
|
format = SV.RGB8_FRAMES
|
|
|
|
qsize = 2
|
1992-08-20 11:46:28 +00:00
|
|
|
audio = 0
|
1992-09-03 16:56:04 +00:00
|
|
|
rate = 2
|
1992-09-04 13:26:59 +00:00
|
|
|
width = 0
|
1992-12-14 12:25:21 +00:00
|
|
|
norecord = 0
|
|
|
|
drop = 0
|
|
|
|
mono = 0
|
|
|
|
grey = 0
|
1992-12-23 15:37:20 +00:00
|
|
|
greybits = 0
|
1992-12-14 12:25:21 +00:00
|
|
|
monotreshold = -1
|
1992-12-23 15:37:20 +00:00
|
|
|
fields = 0
|
|
|
|
preallocspace = 0
|
1992-08-20 11:46:28 +00:00
|
|
|
|
1992-12-24 11:39:00 +00:00
|
|
|
# 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()
|
1992-08-20 14:46:46 +00:00
|
|
|
sys.exit(2)
|
|
|
|
|
1992-12-24 11:39:00 +00:00
|
|
|
# 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
|
1992-08-20 11:46:28 +00:00
|
|
|
if args:
|
|
|
|
filename = args[0]
|
|
|
|
else:
|
|
|
|
filename = 'film.video'
|
|
|
|
|
1992-08-20 14:46:46 +00:00
|
|
|
if args[1:] and not audio:
|
|
|
|
sys.stderr.write('-a turned on by appearance of 2nd file\n')
|
|
|
|
audio = 1
|
|
|
|
|
1992-08-20 11:46:28 +00:00
|
|
|
if audio:
|
|
|
|
if args[1:]:
|
|
|
|
audiofilename = args[1]
|
|
|
|
else:
|
|
|
|
audiofilename = 'film.aiff'
|
|
|
|
else:
|
|
|
|
audiofilename = None
|
|
|
|
|
1992-12-14 12:25:21 +00:00
|
|
|
if norecord:
|
|
|
|
filename = audiofilename = ''
|
1992-12-24 11:39:00 +00:00
|
|
|
|
|
|
|
# Open video
|
1992-09-04 13:26:59 +00:00
|
|
|
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)
|
1992-08-20 11:46:28 +00:00
|
|
|
|
1992-09-04 13:26:59 +00:00
|
|
|
gl.foreground()
|
|
|
|
gl.maxsize(x, y)
|
|
|
|
gl.keepaspect(x, y)
|
1992-08-20 11:46:28 +00:00
|
|
|
gl.stepunit(8, 6)
|
1992-09-04 13:26:59 +00:00
|
|
|
if width:
|
|
|
|
gl.prefsize(width, width*3/4)
|
1992-08-20 11:46:28 +00:00
|
|
|
win = gl.winopen(filename)
|
1992-09-04 13:26:59 +00:00
|
|
|
if width:
|
|
|
|
gl.maxsize(x, y)
|
|
|
|
gl.keepaspect(x, y)
|
|
|
|
gl.stepunit(8, 6)
|
|
|
|
gl.winconstraints()
|
1992-08-20 11:46:28 +00:00
|
|
|
x, y = gl.getsize()
|
|
|
|
print x, 'x', y
|
|
|
|
|
|
|
|
v.SetSize(x, y)
|
1992-12-14 12:25:21 +00:00
|
|
|
|
|
|
|
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.INPUT_BYPASS, 1]
|
|
|
|
else:
|
|
|
|
param = param+[SV.COLOR, SV.DEFAULT_COLOR, SV.INPUT_BYPASS, 0]
|
|
|
|
v.SetParam(param)
|
1992-12-09 22:16:35 +00:00
|
|
|
|
1992-08-20 11:46:28 +00:00
|
|
|
v.BindGLWindow(win, SV.IN_REPLACE)
|
|
|
|
|
|
|
|
gl.qdevice(DEVICE.LEFTMOUSE)
|
|
|
|
gl.qdevice(DEVICE.WINQUIT)
|
|
|
|
gl.qdevice(DEVICE.WINSHUT)
|
|
|
|
gl.qdevice(DEVICE.ESCKEY)
|
|
|
|
|
1992-12-24 11:39:00 +00:00
|
|
|
help()
|
1992-08-20 11:46:28 +00:00
|
|
|
|
|
|
|
while 1:
|
|
|
|
dev, val = gl.qread()
|
|
|
|
if dev == DEVICE.LEFTMOUSE:
|
|
|
|
if val == 1:
|
1992-09-03 16:56:04 +00:00
|
|
|
info = format, x, y, qsize, rate
|
1992-12-14 12:25:21 +00:00
|
|
|
record(v, info, filename, audiofilename,\
|
1992-12-23 15:37:20 +00:00
|
|
|
mono, grey, greybits, monotreshold, \
|
|
|
|
fields, preallocspace)
|
1992-08-20 11:46:28 +00:00
|
|
|
elif dev == DEVICE.REDRAW:
|
|
|
|
# Window resize (or move)
|
1992-09-03 16:56:04 +00:00
|
|
|
x, y = gl.getsize()
|
|
|
|
print x, 'x', y
|
|
|
|
v.SetSize(x, y)
|
|
|
|
v.BindGLWindow(win, SV.IN_REPLACE)
|
1992-08-20 11:46:28 +00:00
|
|
|
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
|
|
|
|
|
1992-12-23 15:37:20 +00:00
|
|
|
def record(v, info, filename, audiofilename, mono, grey, greybits, \
|
|
|
|
monotreshold, fields, preallocspace):
|
1992-08-20 11:46:28 +00:00
|
|
|
import thread
|
1992-09-03 16:56:04 +00:00
|
|
|
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
|
1992-12-14 12:25:21 +00:00
|
|
|
if filename:
|
|
|
|
vout = VFile.VoutFile().init(filename)
|
|
|
|
if mono:
|
|
|
|
vout.format = 'mono'
|
1992-12-23 15:37:20 +00:00
|
|
|
elif grey and greybits == 8:
|
1992-12-14 12:25:21 +00:00
|
|
|
vout.format = 'grey'
|
1992-12-23 15:37:20 +00:00
|
|
|
elif grey:
|
|
|
|
vout.format = 'grey'+`abs(greybits)`
|
1992-12-14 12:25:21 +00:00
|
|
|
else:
|
|
|
|
vout.format = 'rgb8'
|
|
|
|
vout.width = x
|
|
|
|
vout.height = y
|
1992-12-23 15:37:20 +00:00
|
|
|
if fields:
|
|
|
|
vout.packfactor = (1,-2)
|
1992-12-14 12:25:21 +00:00
|
|
|
vout.writeheader()
|
1992-12-23 15:37:20 +00:00
|
|
|
if preallocspace:
|
|
|
|
print 'Preallocating space...'
|
|
|
|
vout.prealloc(preallocspace)
|
|
|
|
print 'done.'
|
1992-12-14 12:25:21 +00:00
|
|
|
MAXSIZE = 20 # XXX should be a user option
|
|
|
|
import Queue
|
|
|
|
queue = Queue.Queue().init(MAXSIZE)
|
|
|
|
done = thread.allocate_lock()
|
|
|
|
done.acquire_lock()
|
1992-12-23 15:37:20 +00:00
|
|
|
convertor = None
|
|
|
|
if grey:
|
|
|
|
if greybits == 2:
|
|
|
|
convertor = imageop.grey2grey2
|
|
|
|
elif greybits == 4:
|
|
|
|
convertor = imageop.grey2grey4
|
|
|
|
elif greybits == -2:
|
|
|
|
convertor = imageop.dither2grey2
|
1992-12-14 12:25:21 +00:00
|
|
|
thread.start_new_thread(saveframes, \
|
1992-12-23 15:37:20 +00:00
|
|
|
(vout, queue, done, mono, monotreshold, convertor))
|
1992-12-14 12:25:21 +00:00
|
|
|
if audiofilename:
|
|
|
|
audiodone = thread.allocate_lock()
|
|
|
|
audiodone.acquire_lock()
|
|
|
|
audiostop = []
|
|
|
|
initaudio(audiofilename, audiostop, audiodone)
|
1992-08-20 11:46:28 +00:00
|
|
|
gl.wintitle('(rec) ' + filename)
|
1992-09-03 16:56:04 +00:00
|
|
|
lastid = 0
|
1992-08-20 11:46:28 +00:00
|
|
|
t0 = time.millitimer()
|
1992-12-14 12:25:21 +00:00
|
|
|
count = 0
|
|
|
|
ids = []
|
1992-09-03 16:56:04 +00:00
|
|
|
v.InitContinuousCapture(info)
|
1992-08-20 11:46:28 +00:00
|
|
|
while not gl.qtest():
|
1992-09-03 16:56:04 +00:00
|
|
|
try:
|
|
|
|
cd, id = v.GetCaptureData()
|
1992-09-22 15:01:43 +00:00
|
|
|
except sv.error:
|
1992-12-23 15:37:20 +00:00
|
|
|
#time.millisleep(10) # XXX is this necessary?
|
|
|
|
sgi.nap(1) # XXX Try by Jack
|
1992-09-03 16:56:04 +00:00
|
|
|
continue
|
1992-12-14 12:25:21 +00:00
|
|
|
ids.append(id)
|
|
|
|
|
1992-09-03 16:56:04 +00:00
|
|
|
id = id + 2*rate
|
|
|
|
## if id <> lastid + 2*rate:
|
|
|
|
## print lastid, id
|
|
|
|
lastid = id
|
1992-12-14 12:25:21 +00:00
|
|
|
count = count+1
|
1992-12-23 15:37:20 +00:00
|
|
|
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)))
|
1992-09-03 16:56:04 +00:00
|
|
|
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'
|
1992-12-14 12:29:43 +00:00
|
|
|
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
|
1993-02-15 17:33:36 +00:00
|
|
|
if ids:
|
|
|
|
print 'Ids:',
|
|
|
|
t0 = ids[0]
|
|
|
|
del ids[0]
|
|
|
|
for t1 in ids:
|
|
|
|
print t1-t0,
|
|
|
|
t0 = t1
|
|
|
|
print
|
1992-12-14 12:25:21 +00:00
|
|
|
if filename and audiofilename:
|
1992-09-03 16:56:04 +00:00
|
|
|
audiostop.append(None)
|
|
|
|
audiodone.acquire_lock()
|
|
|
|
v.EndContinuousCapture()
|
1992-12-14 12:25:21 +00:00
|
|
|
if filename:
|
|
|
|
queue.put(None) # Sentinel
|
|
|
|
done.acquire_lock()
|
1992-08-20 11:46:28 +00:00
|
|
|
gl.wintitle('(done) ' + filename)
|
|
|
|
|
|
|
|
|
|
|
|
# Thread to save the frames to the file
|
|
|
|
|
1992-12-23 15:37:20 +00:00
|
|
|
def saveframes(vout, queue, done, mono, monotreshold, convertor):
|
1992-08-20 11:46:28 +00:00
|
|
|
while 1:
|
1992-09-03 16:56:04 +00:00
|
|
|
x = queue.get()
|
|
|
|
if not x:
|
|
|
|
break
|
|
|
|
data, t = x
|
1992-12-23 15:37:20 +00:00
|
|
|
if convertor:
|
|
|
|
data = convertor(data, len(data), 1)
|
|
|
|
elif mono and monotreshold >= 0:
|
1992-12-14 12:25:21 +00:00
|
|
|
data = imageop.grey2mono(data, len(data), 1,\
|
|
|
|
monotreshold)
|
|
|
|
elif mono:
|
|
|
|
data = imageop.dither2mono(data, len(data), 1)
|
1992-09-03 16:56:04 +00:00
|
|
|
vout.writeframe(t, data, None)
|
|
|
|
sys.stderr.write('Done writing video\n')
|
1992-08-20 11:46:28 +00:00
|
|
|
vout.close()
|
1992-09-03 16:56:04 +00:00
|
|
|
done.release_lock()
|
1992-08-20 11:46:28 +00:00
|
|
|
|
|
|
|
|
1992-08-20 14:46:46 +00:00
|
|
|
# Initialize audio recording
|
|
|
|
|
1992-09-03 16:56:04 +00:00
|
|
|
AQSIZE = 8000 # XXX should be a user option
|
1992-08-20 14:46:46 +00:00
|
|
|
|
1992-09-03 16:56:04 +00:00
|
|
|
def initaudio(filename, stop, done):
|
1992-08-20 14:46:46 +00:00
|
|
|
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)
|
1992-09-03 16:56:04 +00:00
|
|
|
thread.start_new_thread(audiorecord, (afile, aport, stop, done))
|
1992-08-20 14:46:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
1992-09-03 16:56:04 +00:00
|
|
|
def audiorecord(afile, aport, stop, done):
|
|
|
|
while not stop:
|
1992-08-20 14:46:46 +00:00
|
|
|
data = aport.readsamps(AQSIZE/2)
|
|
|
|
## afile.writesampsraw(data)
|
|
|
|
afile.writesamps(data)
|
|
|
|
del data
|
|
|
|
afile.destroy()
|
|
|
|
print 'Done writing audio'
|
1992-09-03 16:56:04 +00:00
|
|
|
done.release_lock()
|
1992-08-20 14:46:46 +00:00
|
|
|
|
|
|
|
|
1992-08-20 11:46:28 +00:00
|
|
|
# Don't forget to call the main program
|
|
|
|
|
1992-09-01 14:45:57 +00:00
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print '[Interrupt]'
|