1992-12-23 11:41:38 -04:00
|
|
|
#! /ufs/guido/bin/sgi/python
|
|
|
|
|
1993-02-05 11:34:22 -04:00
|
|
|
# Capture a CMIF movie using the Indigo video library and board in burst mode
|
1992-12-23 11:41:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
# User interface:
|
|
|
|
#
|
|
|
|
# Start the application. Resize the window to the desired movie size.
|
|
|
|
# Press the left mouse button to start recording, release it to end
|
|
|
|
# recording. You can record as many times as you wish, but each time
|
|
|
|
# you overwrite the output file(s), so only the last recording is
|
|
|
|
# kept.
|
|
|
|
#
|
|
|
|
# Press ESC or select the window manager Quit or Close window option
|
|
|
|
# to quit. If you quit before recording anything, the output file(s)
|
|
|
|
# are not touched.
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
1993-02-05 11:34:22 -04:00
|
|
|
|
|
|
|
# Usage and help functions (keep this up-to-date if you change the program!)
|
|
|
|
|
|
|
|
def usage():
|
1993-02-24 12:08:21 -04:00
|
|
|
print 'Usage: Vrecb [options] [moviefile [audiofile]]'
|
1993-02-05 11:34:22 -04:00
|
|
|
print
|
|
|
|
print 'Options:'
|
1993-02-24 12:08:21 -04:00
|
|
|
print '-a : record audio as well'
|
1993-02-05 11:34:22 -04:00
|
|
|
print '-r rate : capture 1 out of every "rate" frames', \
|
|
|
|
'(default and min 1)'
|
|
|
|
print '-w width : initial window width', \
|
1993-02-25 12:10:16 -04:00
|
|
|
'(default 256, use 0 for interactive placement)'
|
1993-02-05 11:34:22 -04:00
|
|
|
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 (instead of frames)'
|
|
|
|
print '-n number : Capture this many frames (default 60)'
|
1993-05-10 12:45:49 -03:00
|
|
|
print '-N memsize : Capture frames fitting in this many kbytes'
|
1993-02-05 11:34:22 -04:00
|
|
|
print 'moviefile : here goes the movie data (default film.video)'
|
|
|
|
|
|
|
|
def help():
|
|
|
|
print 'Press the left mouse button to start recording.'
|
|
|
|
print 'Recording time is determined by the -n option.'
|
|
|
|
print 'You can record as many times as you wish, but each'
|
|
|
|
print 'recording overwrites the output file(s) -- only the'
|
|
|
|
print 'last 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.'
|
|
|
|
|
|
|
|
|
1992-12-23 11:41:38 -04:00
|
|
|
# Main program
|
|
|
|
|
|
|
|
def main():
|
|
|
|
format = SV.RGB8_FRAMES
|
1993-02-24 12:08:21 -04:00
|
|
|
audio = 0
|
1992-12-23 11:41:38 -04:00
|
|
|
rate = 1
|
1993-02-25 12:10:16 -04:00
|
|
|
width = 256
|
1992-12-23 11:41:38 -04:00
|
|
|
drop = 0
|
|
|
|
mono = 0
|
|
|
|
grey = 0
|
|
|
|
greybits = 0
|
|
|
|
monotreshold = -1
|
|
|
|
fields = 0
|
1993-05-10 12:45:49 -03:00
|
|
|
number = 0
|
|
|
|
memsize = 0
|
1992-12-23 11:41:38 -04:00
|
|
|
|
1993-02-05 11:34:22 -04:00
|
|
|
try:
|
1993-05-10 12:45:49 -03:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'ar:w:dg:mM:Gfn:N:')
|
1993-02-05 11:34:22 -04:00
|
|
|
except getopt.error, msg:
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
print 'Error:', msg, '\n'
|
|
|
|
usage()
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
try:
|
|
|
|
for opt, arg in opts:
|
1993-02-24 12:08:21 -04:00
|
|
|
if opt == '-a':
|
|
|
|
audio = 1
|
1993-02-05 11:34:22 -04:00
|
|
|
if opt == '-r':
|
|
|
|
rate = string.atoi(arg)
|
|
|
|
if rate < 1:
|
|
|
|
sys.stderr.write('-r rate must be >= 1\n')
|
|
|
|
sys.exit(2)
|
|
|
|
elif opt == '-w':
|
|
|
|
width = string.atoi(arg)
|
|
|
|
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 == '-n':
|
|
|
|
number = string.atoi(arg)
|
1993-05-10 12:45:49 -03:00
|
|
|
elif opt == '-N':
|
|
|
|
memsize = string.atoi(arg)
|
|
|
|
if 0 < memsize < 1024:
|
|
|
|
memsize = memsize * 1024
|
|
|
|
if 0 < memsize < 1024*1024:
|
|
|
|
memsize = memsize * 1024
|
|
|
|
print 'memsize', memsize
|
1993-02-05 11:34:22 -04:00
|
|
|
except string.atoi_error:
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
print 'Option', opt, 'requires integer argument'
|
|
|
|
sys.exit(2)
|
|
|
|
|
1993-05-10 12:45:49 -03:00
|
|
|
if number <> 0 and memsize <> 0:
|
|
|
|
sys.stderr.write('-n and -N are mutually exclusive\n')
|
|
|
|
sys.exit(2)
|
|
|
|
if number == 0 and memsize == 0:
|
|
|
|
number = 60
|
|
|
|
|
1993-02-05 11:34:22 -04:00
|
|
|
if not fields:
|
1993-02-24 12:08:21 -04:00
|
|
|
print '-f option assumed until somebody fixes it'
|
1993-02-05 11:34:22 -04:00
|
|
|
fields = 1
|
1992-12-23 11:41:38 -04:00
|
|
|
|
|
|
|
if args[2:]:
|
1993-02-24 12:08:21 -04:00
|
|
|
sys.stderr.write('usage: Vrecb [options] [file [audiofile]]\n')
|
1992-12-23 11:41:38 -04:00
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
if args:
|
|
|
|
filename = args[0]
|
|
|
|
else:
|
|
|
|
filename = 'film.video'
|
|
|
|
|
1993-02-24 12:08:21 -04:00
|
|
|
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
|
|
|
|
|
1992-12-23 11:41:38 -04: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)
|
|
|
|
|
|
|
|
gl.foreground()
|
|
|
|
gl.maxsize(x, y)
|
|
|
|
gl.keepaspect(x, y)
|
|
|
|
gl.stepunit(8, 6)
|
|
|
|
if width:
|
1993-02-25 12:10:16 -04:00
|
|
|
height = width*3/4
|
|
|
|
x1 = 150
|
|
|
|
x2 = x1 + width-1
|
|
|
|
y2 = 768-150
|
|
|
|
y1 = y2-height+1
|
|
|
|
gl.prefposition(x1, x2, y1, y2)
|
1992-12-23 11:41:38 -04:00
|
|
|
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
|
1993-05-10 12:45:49 -03:00
|
|
|
if memsize:
|
|
|
|
number = calcnumber(x, y, grey or mono, memsize)
|
|
|
|
print number, 'frames'
|
1992-12-23 11:41:38 -04:00
|
|
|
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:
|
1993-02-25 12:10:16 -04:00
|
|
|
param = param+[SV.COLOR, SV.MONO, SV.DITHER, 0, \
|
|
|
|
SV.INPUT_BYPASS, 1]
|
1992-12-23 11:41:38 -04:00
|
|
|
else:
|
|
|
|
param = param+[SV.COLOR, SV.DEFAULT_COLOR, SV.INPUT_BYPASS, 0]
|
|
|
|
|
|
|
|
v.BindGLWindow(win, SV.IN_REPLACE)
|
1993-02-25 12:10:16 -04:00
|
|
|
v.SetParam(param)
|
1992-12-23 11:41:38 -04:00
|
|
|
|
|
|
|
gl.qdevice(DEVICE.LEFTMOUSE)
|
|
|
|
gl.qdevice(DEVICE.WINQUIT)
|
|
|
|
gl.qdevice(DEVICE.WINSHUT)
|
|
|
|
gl.qdevice(DEVICE.ESCKEY)
|
|
|
|
|
1993-02-05 11:34:22 -04:00
|
|
|
help()
|
1992-12-23 11:41:38 -04:00
|
|
|
|
|
|
|
while 1:
|
|
|
|
dev, val = gl.qread()
|
|
|
|
if dev == DEVICE.LEFTMOUSE:
|
|
|
|
if val == 1:
|
|
|
|
info = format, x, y, number, rate
|
1993-02-24 12:08:21 -04:00
|
|
|
record(v, info, filename, audiofilename, \
|
|
|
|
mono, grey, \
|
1992-12-23 11:41:38 -04:00
|
|
|
greybits, monotreshold, fields)
|
|
|
|
elif dev == DEVICE.REDRAW:
|
|
|
|
# Window resize (or move)
|
|
|
|
x, y = gl.getsize()
|
|
|
|
print x, 'x', y
|
1993-05-10 12:45:49 -03:00
|
|
|
if memsize:
|
|
|
|
number = calcnumber(x, y, grey or mono, memsize)
|
|
|
|
print number, 'frames'
|
1992-12-23 11:41:38 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
1993-05-10 12:45:49 -03:00
|
|
|
def calcnumber(x, y, grey, memsize):
|
|
|
|
pixels = x*y
|
|
|
|
pixels = pixels/2 # XXX always assume fields
|
|
|
|
if grey: n = memsize/pixels
|
|
|
|
else: n = memsize/(4*pixels)
|
|
|
|
return max(1, n)
|
|
|
|
|
|
|
|
|
1992-12-23 11:41:38 -04:00
|
|
|
# Record until the mouse is released (or any other GL event)
|
|
|
|
# XXX audio not yet supported
|
|
|
|
|
1993-02-24 12:08:21 -04:00
|
|
|
def record(v, info, filename, audiofilename, \
|
|
|
|
mono, grey, greybits, monotreshold, fields):
|
1992-12-23 11:41:38 -04:00
|
|
|
import thread
|
|
|
|
format, x, y, number, 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
|
|
|
|
#
|
|
|
|
# Go grab
|
|
|
|
#
|
1993-02-24 12:08:21 -04:00
|
|
|
if audiofilename:
|
|
|
|
gl.wintitle('(start audio) ' + filename)
|
|
|
|
audiodone = thread.allocate_lock()
|
|
|
|
audiodone.acquire_lock()
|
|
|
|
audiostart = thread.allocate_lock()
|
|
|
|
audiostart.acquire_lock()
|
|
|
|
audiostop = []
|
|
|
|
initaudio(audiofilename, audiostop, audiostart, audiodone)
|
|
|
|
audiostart.acquire_lock()
|
1992-12-23 11:41:38 -04:00
|
|
|
gl.wintitle('(rec) ' + filename)
|
|
|
|
try:
|
|
|
|
ninfo, data, bitvec = v.CaptureBurst(info)
|
|
|
|
except sv.error, arg:
|
|
|
|
print 'CaptureBurst failed:', arg
|
|
|
|
print 'info:', info
|
|
|
|
gl.wintitle(filename)
|
|
|
|
return
|
|
|
|
gl.wintitle('(save) '+ filename)
|
|
|
|
#
|
|
|
|
# Check results
|
|
|
|
#
|
|
|
|
if info <> ninfo:
|
|
|
|
print 'Sorry, format changed.'
|
|
|
|
print 'Wanted:',info
|
|
|
|
print 'Got :',ninfo
|
|
|
|
gl.wintitle(filename)
|
|
|
|
return
|
|
|
|
# print bitvec
|
|
|
|
if x*y*number <> len(data):
|
|
|
|
print 'Funny data length: wanted',x,'*',y,'*', number,'=',\
|
|
|
|
x*y*number,'got',len(data)
|
|
|
|
gl.wintitle(filename)
|
|
|
|
return
|
|
|
|
#
|
|
|
|
# Save
|
|
|
|
#
|
1993-02-24 12:08:21 -04:00
|
|
|
if filename and audiofilename:
|
|
|
|
audiostop.append(None)
|
|
|
|
audiodone.acquire_lock()
|
1992-12-23 11:41:38 -04:00
|
|
|
if filename:
|
|
|
|
#
|
|
|
|
# Construct header and write it
|
|
|
|
#
|
1993-02-15 13:33:36 -04:00
|
|
|
try:
|
1993-12-17 11:11:41 -04:00
|
|
|
vout = VFile.VoutFile(filename)
|
1993-02-15 13:33:36 -04:00
|
|
|
except IOError, msg:
|
|
|
|
print filename, ':', msg
|
|
|
|
sys.exit(1)
|
1992-12-23 11:41:38 -04:00
|
|
|
if mono:
|
|
|
|
vout.format = 'mono'
|
|
|
|
elif grey and greybits == 8:
|
|
|
|
vout.format = 'grey'
|
|
|
|
elif grey:
|
|
|
|
vout.format = 'grey'+`abs(greybits)`
|
|
|
|
else:
|
|
|
|
vout.format = 'rgb8'
|
|
|
|
vout.width = x
|
|
|
|
vout.height = y
|
|
|
|
if fields:
|
|
|
|
vout.packfactor = (1,-2)
|
|
|
|
else:
|
|
|
|
print 'Sorry, can only save fields at the moment'
|
1993-02-05 11:34:22 -04:00
|
|
|
print '(i.e. you *must* use the -f option)'
|
1992-12-23 11:41:38 -04:00
|
|
|
gl.wintitle(filename)
|
|
|
|
return
|
|
|
|
vout.writeheader()
|
|
|
|
#
|
|
|
|
# Compute convertor, if needed
|
|
|
|
#
|
|
|
|
convertor = None
|
|
|
|
if grey:
|
|
|
|
if greybits == 2:
|
|
|
|
convertor = imageop.grey2grey2
|
|
|
|
elif greybits == 4:
|
|
|
|
convertor = imageop.grey2grey4
|
|
|
|
elif greybits == -2:
|
|
|
|
convertor = imageop.dither2grey2
|
|
|
|
fieldsize = x*y/2
|
|
|
|
nskipped = 0
|
|
|
|
realframeno = 0
|
|
|
|
tpf = 1000 / 50.0 #XXXX
|
1993-05-10 12:45:49 -03:00
|
|
|
# Trying to find the pattern in frame skipping
|
|
|
|
okstretch = 0
|
|
|
|
skipstretch = 0
|
1992-12-23 11:41:38 -04:00
|
|
|
for frameno in range(0, number*2):
|
|
|
|
if frameno <> 0 and \
|
|
|
|
bitvec[frameno] == bitvec[frameno-1]:
|
|
|
|
nskipped = nskipped + 1
|
1993-05-10 12:45:49 -03:00
|
|
|
if okstretch:
|
|
|
|
print okstretch, 'ok',
|
|
|
|
okstretch = 0
|
|
|
|
skipstretch = skipstretch + 1
|
1992-12-23 11:41:38 -04:00
|
|
|
continue
|
1993-05-10 12:45:49 -03:00
|
|
|
if skipstretch:
|
|
|
|
print skipstretch, 'skipped'
|
|
|
|
skipstretch = 0
|
|
|
|
okstretch = okstretch + 1
|
1992-12-23 11:41:38 -04:00
|
|
|
#
|
|
|
|
# Save field.
|
|
|
|
# XXXX Works only for fields and top-to-bottom
|
|
|
|
#
|
|
|
|
start = frameno*fieldsize
|
|
|
|
field = data[start:start+fieldsize]
|
|
|
|
if convertor:
|
1993-02-15 13:33:36 -04:00
|
|
|
field = convertor(field, len(field), 1)
|
1992-12-23 11:41:38 -04:00
|
|
|
elif mono and monotreshold >= 0:
|
1993-02-15 13:33:36 -04:00
|
|
|
field = imageop.grey2mono( \
|
|
|
|
field, len(field), 1, monotreshold)
|
1992-12-23 11:41:38 -04:00
|
|
|
elif mono:
|
1993-02-15 13:33:36 -04:00
|
|
|
field = imageop.dither2mono( \
|
|
|
|
field, len(field), 1)
|
|
|
|
realframeno = realframeno + 1
|
1993-05-10 12:45:49 -03:00
|
|
|
vout.writeframe(int(realframeno*tpf), field, None)
|
|
|
|
print okstretch, 'ok',
|
|
|
|
print skipstretch, 'skipped'
|
|
|
|
print 'Skipped', nskipped, 'duplicate frames'
|
1992-12-23 11:41:38 -04:00
|
|
|
vout.close()
|
|
|
|
|
|
|
|
gl.wintitle('(done) ' + filename)
|
1993-02-24 12:08:21 -04:00
|
|
|
# Initialize audio recording
|
|
|
|
|
|
|
|
AQSIZE = 8*8000 # XXX should be a user option
|
|
|
|
|
|
|
|
def initaudio(filename, stop, start, done):
|
1993-12-17 11:11:41 -04:00
|
|
|
import thread, aifc
|
|
|
|
afile = aifc.open(filename, 'w')
|
1993-02-24 12:08:21 -04:00
|
|
|
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, start, 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, start, done):
|
|
|
|
start.release_lock()
|
|
|
|
leeway = 4
|
|
|
|
while leeway > 0:
|
|
|
|
if stop:
|
|
|
|
leeway = leeway - 1
|
|
|
|
data = aport.readsamps(AQSIZE/8)
|
|
|
|
## afile.writesampsraw(data)
|
|
|
|
afile.writesamps(data)
|
|
|
|
del data
|
|
|
|
afile.destroy()
|
|
|
|
print 'Done writing audio'
|
|
|
|
done.release_lock()
|
|
|
|
|
1992-12-23 11:41:38 -04:00
|
|
|
|
|
|
|
# Don't forget to call the main program
|
|
|
|
|
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print '[Interrupt]'
|