Made much faster, but for SHORT movies only, by saving lrectread

data unprocessed in memory.  As long as memory doesn't run out this
gets us 19 frames/sec!
Also many cosmetic changes.
This commit is contained in:
Guido van Rossum 1991-11-04 14:31:54 +00:00
parent 96059b318c
commit 5395c067de
1 changed files with 115 additions and 45 deletions

View File

@ -1,4 +1,3 @@
#!/ufs/guido/bin/sgi/python3.3
from gl import * from gl import *
from GL import * from GL import *
from DEVICE import * from DEVICE import *
@ -9,15 +8,14 @@ import socket
import posix import posix
import vtime import vtime
# Preallocation parameter
PREALLOC = 8 # Megabyte
# Sync audio parameters
SYNCPORT = 10000 SYNCPORT = 10000
CTLPORT = 10001 CTLPORT = 10001
VP_GBXORG = 0x1000001 from vpregs import *
VP_GBYORG = 0x1000002
VP_FBXORG = 0x1000003
VP_FBYORG = 0x1000004
VP_WIDTH = 0x1000005
VP_HEIGHT = 0x1000006
class Struct(): pass class Struct(): pass
epoch = Struct() epoch = Struct()
@ -25,17 +23,52 @@ epoch = Struct()
def getvideosize(): def getvideosize():
w = getvideo(VP_WIDTH) w = getvideo(VP_WIDTH)
h = getvideo(VP_HEIGHT) h = getvideo(VP_HEIGHT)
print getvideo(VP_GBXORG), getvideo(VP_GBYORG) print 'WIDTH,HEIGHT:', w, h
print getvideo(VP_FBXORG), getvideo(VP_FBYORG) print 'GB{X,Y}ORG:', getvideo(VP_GBXORG), getvideo(VP_GBYORG)
print 'FB{X,Y}ORG:', getvideo(VP_FBXORG), getvideo(VP_FBYORG)
x = 0 x = 0
y = 0 y = 0
return x,y,w,h return x,y,w,h
framelist = []
def prealloc(w, h):
nbytes = w*h*4
limit = PREALLOC*1024*1024
total = 0
list = []
print 'Prealloc to', PREALLOC, 'Megabytes...'
while total+nbytes <= limit:
list.append('x'*nbytes)
total = total + nbytes
print 'Done.'
def grabframe(f,x,y,w,h,pf):
#### saveframes(f, w, h, pf)
readsource(SRC_FRONT)
if pf:
w = w/pf*pf
h = h/pf*pf
data = lrectread(x,y,x+w-1,y+h-1)
t = time.millitimer()-epoch.epoch
framelist.append(data, t)
readsource(SRC_FRAMEGRABBER)
def saveframes(f, w, h, pf):
for data, t in framelist:
if pf:
w = w/pf*pf
h = h/pf*pf
data = packrect(w,h,pf,data)
f.write(`t` + ',' + `len(data)` + '\n')
f.write(data)
framelist[:] = []
def saveframe(f,x,y,w,h,pf, notime): def saveframe(f,x,y,w,h,pf, notime):
readsource(SRC_FRONT) readsource(SRC_FRONT)
if pf: if pf:
w = w/pf*pf w = w/pf*pf
h = h/pf*pf h = h/pf*pf
data = None
data = lrectread(x,y,x+w-1,y+h-1) data = lrectread(x,y,x+w-1,y+h-1)
if pf: data = packrect(w,h,pf,data) if pf: data = packrect(w,h,pf,data)
if notime: t = 0 if notime: t = 0
@ -43,6 +76,7 @@ def saveframe(f,x,y,w,h,pf, notime):
f.write(`t` + ',' + `len(data)` + '\n') f.write(`t` + ',' + `len(data)` + '\n')
f.write(data) f.write(data)
readsource(SRC_FRAMEGRABBER) readsource(SRC_FRAMEGRABBER)
def drawframe(x,y,w,h,col): def drawframe(x,y,w,h,col):
drawmode(OVERDRAW) drawmode(OVERDRAW)
color(col) color(col)
@ -50,12 +84,25 @@ def drawframe(x,y,w,h,col):
v2i(x-1,y-1) ; v2i(x+w,y-1); v2i(x+w,y+h); v2i(x-1,y+h); v2i(x-1,y-1) v2i(x-1,y-1) ; v2i(x+w,y-1); v2i(x+w,y+h); v2i(x-1,y+h); v2i(x-1,y-1)
endline() endline()
drawmode(NORMALDRAW) drawmode(NORMALDRAW)
def usage():
sys.stderr.write('Usage: camcorder ' + \
'[-c] [-p packfactor] [-a audiomachine [-s]] [outputfile]\n')
sys.exit(2)
def wrheader(f, w, h, pf):
f.write('CMIF video 1.0\n')
f.write(`w,h,pf` + '\n')
print 'width,height,pf:', w, h, pf,
if pf = 0: pf = 4
print '(i.e.,', w*h*pf, 'bytes/frame)'
def main(): def main():
foreground() foreground()
pf = 2 pf = 2
ausync = 0 ausync = 0
austart = 0 austart = 0
optlist, args = getopt.getopt(sys.argv[1:],'ca:s') optlist, args = getopt.getopt(sys.argv[1:],'ca:sp:')
for opt, arg in optlist: for opt, arg in optlist:
if opt = '-c': if opt = '-c':
pf = 0 pf = 0
@ -64,13 +111,21 @@ def main():
aumachine = arg aumachine = arg
elif opt = '-s': elif opt = '-s':
austart = 1 austart = 1
elif opt = '-p':
pf = int(eval(arg))
else: else:
print 'Usage: camcorder [-c] [-a audiomachine [-s]]' usage()
sys.exit(1) if args:
if len(args) > 1:
print 'Too many arguments'
usage()
filename = args[0]
else:
filename = 'film.video'
if austart: if austart:
if not ausync: if not ausync:
print 'Cannot use -s without -a' print 'Cannot use -s without -a'
sys.exit(1) usage()
print 'Starting audio recorder...' print 'Starting audio recorder...'
posix.system('rsh '+aumachine+' syncrecord '+socket.gethostname()+' &') posix.system('rsh '+aumachine+' syncrecord '+socket.gethostname()+' &')
if ausync: if ausync:
@ -81,15 +136,14 @@ def main():
aua = (socket.gethostbyname(aumachine), CTLPORT) aua = (socket.gethostbyname(aumachine), CTLPORT)
print 'Done.' print 'Done.'
vidx, vidy, w, h = getvideosize() vidx, vidy, w, h = getvideosize()
prefsize(w,h) #prefsize(w,h)
win = winopen('Camcorder') winx, winy = 1280-w-10, 1024-h-30
if len(args) > 1: prefposition(winx,winx+w-1,winy,winy+h-1)
f = open(args, 'w') win = winopen(filename)
else: f = open(filename, 'w')
f = open('film.video', 'w')
w, h = getsize() w, h = getsize()
realw, realh = w, h realw, realh = w, h
doublebuffer() #doublebuffer()
RGBmode() RGBmode()
gconfig() gconfig()
qdevice(LEFTMOUSE) qdevice(LEFTMOUSE)
@ -98,6 +152,8 @@ def main():
qdevice(CKEY) qdevice(CKEY)
qdevice(PKEY) qdevice(PKEY)
qdevice(ESCKEY) qdevice(ESCKEY)
qdevice(WINQUIT)
qdevice(WINSHUT)
inrunning = 1 inrunning = 1
outrunning = 0 outrunning = 0
stop = 'stop' stop = 'stop'
@ -108,9 +164,10 @@ def main():
sizewritten = 0 sizewritten = 0
x, y = realw/4, realh/4 x, y = realw/4, realh/4
w, h = w/2, h/2 w, h = w/2, h/2
drawframe(x,y,w,h,1) prealloc(w, h)
nframe = 0
try: try:
drawframe(x,y,w,h,1)
nframe = 0
num = 0 num = 0
while 1: while 1:
insingle = 0 insingle = 0
@ -125,7 +182,8 @@ def main():
w = getvaluator(MOUSEX)-x-ox w = getvaluator(MOUSEX)-x-ox
h = getvaluator(MOUSEY)-y-oy h = getvaluator(MOUSEY)-y-oy
drawframe(x,y,w,h,1) drawframe(x,y,w,h,1)
if qtest(): if qtest() or \
not (mousing or inrunning or insingle or outrunning or outsingle):
ev, val = qread() ev, val = qread()
if ev = LEFTMOUSE and val = 1: if ev = LEFTMOUSE and val = 1:
drawframe(x,y,w,h,0) drawframe(x,y,w,h,0)
@ -134,12 +192,16 @@ def main():
x = getvaluator(MOUSEX)-ox x = getvaluator(MOUSEX)-ox
y = getvaluator(MOUSEY)-oy y = getvaluator(MOUSEY)-oy
elif ev = LEFTMOUSE and val = 0: elif ev = LEFTMOUSE and val = 0:
if h < 0:
y, h = y+h, -h
if w < 0:
x, w = x+w, -w
mousing = 0 mousing = 0
if not sizewritten: if not sizewritten:
f.write('CMIF video 1.0\n') wrheader(f, w, h, pf)
f.write(`w,h,pf` + '\n')
sizewritten = 1 sizewritten = 1
if ev = RKEY and val = 1: prealloc(w, h)
elif ev = RKEY and val = 1:
if not inrunning: if not inrunning:
ringbell() ringbell()
else: else:
@ -155,14 +217,12 @@ def main():
stoptime = time.millitimer() stoptime = time.millitimer()
if ausync: if ausync:
ctl.sendto(`(0,stoptime)`, aua) ctl.sendto(`(0,stoptime)`, aua)
nf = nframe * 1000.0 / (time.millitimer()-starttime) fps = nframe * 1000.0 / (time.millitimer()-starttime)
drawmode(OVERDRAW) print 'Recorded', nframe,
color(0) print 'frames at', 0.1*int(fps*10),'frames/sec'
clear() print 'Saving...'
color(1) saveframes(f, w, h, pf)
cmov2i(5,5) print 'Done.'
charstr('Recorded ' + `nf` + ' frames/sec')
drawmode(NORMALDRAW)
elif ev = PKEY and val = 1 and not outrunning: elif ev = PKEY and val = 1 and not outrunning:
outsingle = 1 outsingle = 1
elif ev = CKEY and val = 1: elif ev = CKEY and val = 1:
@ -174,24 +234,34 @@ def main():
inrunning = 0 inrunning = 0
else: else:
insingle = 1 insingle = 1
elif ev = ESCKEY: elif ev in (ESCKEY, WINQUIT, WINSHUT):
if ausync: if ausync:
ctl.sendto(`(2,time.millitimer())`, aua) ctl.sendto(`(2,time.millitimer())`, aua)
raise stop raise stop
elif ev = REDRAW:
drawframe(x,y,w,h,0)
reshapeviewport()
drawframe(x,y,w,h,1)
if inrunning or insingle: if inrunning or insingle:
rectcopy(vidx,vidy,realw,realh,0,0) if outrunning:
swapbuffers() rectcopy(vidx+x,vidy+y,vidx+x+w-1,vidy+y+h-1,x,y)
else:
rectcopy(vidx,vidy,vidx+realw-1,vidx+realh-1,0,0)
#swapbuffers()
if outrunning or outsingle: if outrunning or outsingle:
nframe = nframe + 1 nframe = nframe + 1
if not sizewritten: if not sizewritten:
f.write('CMIF video 1.0\n') wrheader(f, w, h, pf)
f.write(`w,h,pf` + '\n')
sizewritten = 1 sizewritten = 1
saveframe(f, x, y, w, h, pf, outsingle) if outrunning:
grabframe(f, x, y, w, h, pf)
else:
saveframe(f, x, y, w, h, pf, outsingle)
except stop: except stop:
pass pass
drawmode(OVERDRAW) finally:
color(0) drawmode(OVERDRAW)
clear() color(0)
# clear()
main() main()