blob: 68d50d2ad32fb3b92d1dade596d6e1099330fb71 [file] [log] [blame]
Guido van Rossumb7e3cc11993-05-06 16:06:44 +00001#! /ufs/guido/bin/sgi/python
2
Guido van Rossum9f42f4f1993-05-10 15:07:20 +00003# Video bag of tricks: record video(+audio) in various formats and modes
Guido van Rossumb7e3cc11993-05-06 16:06:44 +00004
Guido van Rossumc5a14331993-05-07 11:20:07 +00005# XXX To do:
6# - audio
Guido van Rossumc5a14331993-05-07 11:20:07 +00007# - improve user interface
8# - help button?
9# - command line options to set initial settings
10# - save settings in a file
11# - ...?
Guido van Rossumbc6d3c31993-05-07 09:37:42 +000012
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000013import sys
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000014import time
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000015import getopt
16import string
17import os
Guido van Rossumc5a14331993-05-07 11:20:07 +000018sts = os.system('makemap') # Must be before "import fl" to work
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000019import sgi
20import gl
21import GL
22import DEVICE
23import fl
24import FL
25import flp
26import watchcursor
27import sv
28import SV
29import VFile
30import VGrabber
31import imageop
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000032sys.path.append('/ufs/jack/src/av/vcr')
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000033
34ARROW = 0
35WATCH = 1
36watchcursor.defwatch(WATCH)
37
38def main():
Guido van Rossumc5a14331993-05-07 11:20:07 +000039## fl.set_graphics_mode(0, 1)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000040 vb = VideoBagOfTricks().init()
41 while 1:
42 dummy = fl.do_forms()
43 [dummy]
44
45StopCapture = 'StopCapture'
46
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000047VideoFormatLabels = ['Video off', 'rgb8', 'grey8', 'grey4', 'grey2', \
Jack Jansen672754a1993-06-08 12:52:41 +000048 'grey2 dith', 'mono dith', 'mono thresh', 'rgb24', 'rgb24-jpeg']
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000049VideoFormats = ['', 'rgb8', 'grey', 'grey4', 'grey2', \
Jack Jansen672754a1993-06-08 12:52:41 +000050 'grey2', 'mono', 'mono', 'rgb', 'jpeg']
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000051
52VideoModeLabels = ['Continuous', 'Burst', 'Single frame', 'VCR sync']
53[VM_CONT, VM_BURST, VM_SINGLE, VM_VCR] = range(1, 5)
54
55AudioFormatLabels = ['Audio off', \
56 '16 bit mono', '16 bit stereo', '8 bit mono', '8 bit stereo']
57[A_OFF, A_16_MONO, A_16_STEREO, A_8_MONO, A_8_STEREO] = range(1, 6)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000058
Jack Jansen672754a1993-06-08 12:52:41 +000059VcrSpeedLabels = ['normal', '1/3', '1/5', '1/10', '1/30', 'single-step']
60VcrSpeeds = [None, 5, 4, 3, 2, 1, 0]
61
62RgbSizeLabels = ['full', 'quarter', 'sixteenth']
63
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000064class VideoBagOfTricks:
65
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000066 # Init/close stuff
67
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000068 def init(self):
Jack Jansen672754a1993-06-08 12:52:41 +000069 self.window = None
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000070 formdef = flp.parse_form('VbForm', 'form')
71 flp.create_full_form(self, formdef)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000072 self.g_cont.hide_object()
Guido van Rossumc5a14331993-05-07 11:20:07 +000073 self.g_burst.hide_object()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000074 self.g_single.hide_object()
75 self.g_vcr.hide_object()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000076 self.setdefaults()
77 self.openvideo()
78 self.makewindow()
79 self.bindvideo()
Guido van Rossumc5a14331993-05-07 11:20:07 +000080 self.showform()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +000081 fl.set_event_call_back(self.do_event)
82 return self
83
Guido van Rossum9f42f4f1993-05-10 15:07:20 +000084 def close(self):
85 self.close_video()
86 self.close_audio()
87 raise SystemExit, 0
88
Guido van Rossumc5a14331993-05-07 11:20:07 +000089 def showform(self):
90 # Get position of video window
91 gl.winset(self.window)
92 x, y = gl.getorigin()
93 width, height = gl.getsize()
94 # Calculate position of form window
95 x1 = x + width + 10
96 x2 = x1 + int(self.form.w) - 1
97 y2 = y + height - 1
98 y1 = y2 - int(self.form.h) + 1
99 # Position and show form window
100 gl.prefposition(x1, x2, y1, y2)
101 self.form.show_form(FL.PLACE_FREE, FL.TRUE, \
102 'Video Bag Of Tricks')
103
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000104 def setdefaults(self):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000105 self.vcr = None
106 self.vout = None
107 self.capturing = 0
108 # Video defaults
109 self.vfile = 'film.video'
110 self.vmode = VM_CONT
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000111 self.mono_thresh = 128
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000112 self.vformat = 'rgb8'
113 self.c_vformat.clear_choice()
114 for label in VideoFormatLabels:
115 self.c_vformat.addto_choice(label)
116 self.c_vformat.set_choice(1 + VideoFormats.index(self.vformat))
117 self.c_vmode.clear_choice()
118 for label in VideoModeLabels:
119 self.c_vmode.addto_choice(label)
120 self.c_vmode.set_choice(self.vmode)
121 self.get_vformat()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000122 self.b_drop.set_button(1)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000123 self.in_rate.set_input('2')
124 self.in_maxmem.set_input('1.0')
125 self.in_nframes.set_input('0')
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000126 self.in_nframes_vcr.set_input('1')
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000127 self.in_rate_vcr.set_input('1')
Jack Jansen672754a1993-06-08 12:52:41 +0000128 self.c_vcrspeed.clear_choice()
129 for label in VcrSpeedLabels:
130 self.c_vcrspeed.addto_choice(label)
131 self.c_vcrspeed.set_choice(4)
132 self.c_rgb24_size.clear_choice()
133 for label in RgbSizeLabels:
134 self.c_rgb24_size.addto_choice(label)
135 self.c_rgb24_size.set_choice(1)
136 self.rgb24_size = 1
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000137 # Audio defaults
138 self.aout = None
139 self.aport = None
140 self.afile = 'film.aiff'
141 self.aformat = A_OFF
142 self.c_aformat.clear_choice()
143 for label in AudioFormatLabels:
144 self.c_aformat.addto_choice(label)
145 self.c_aformat.set_choice(self.aformat)
146 self.get_aformat()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000147
148 def openvideo(self):
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000149 try:
150 self.video = sv.OpenVideo()
151 except sv.error, msg:
152 print 'Error opening video:', msg
153 self.video = None
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000154 param = [SV.BROADCAST, SV.PAL]
155 if self.video: self.video.GetParam(param)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000156 if param[1] == SV.PAL:
157 x = SV.PAL_XMAX
158 y = SV.PAL_YMAX
159 elif param[1] == SV.NTSC:
160 x = SV.NTSC_XMAX
161 y = SV.NTSC_YMAX
162 else:
163 print 'Unknown video standard:', param[1]
164 sys.exit(1)
165 self.maxx, self.maxy = x, y
166
167 def makewindow(self):
168 x, y = self.maxx, self.maxy
169 gl.foreground()
170 gl.maxsize(x, y)
171 gl.keepaspect(x, y)
172 gl.stepunit(8, 6)
173 width = 256 # XXX
174 if width:
175 height = width*3/4
176 x1 = 150
177 x2 = x1 + width-1
178 y2 = 768-150
179 y1 = y2-height+1
180 gl.prefposition(x1, x2, y1, y2)
181 self.window = gl.winopen('Vb: initializing')
182 self.settitle()
183 if width:
184 gl.maxsize(x, y)
185 gl.keepaspect(x, y)
186 gl.stepunit(8, 6)
187 gl.winconstraints()
188 gl.qdevice(DEVICE.LEFTMOUSE)
189 gl.qdevice(DEVICE.WINQUIT)
190 gl.qdevice(DEVICE.WINSHUT)
191
Jack Jansen672754a1993-06-08 12:52:41 +0000192 def optfullsizewindow(self):
193 if not self.window:
194 return
195 gl.winset(self.window)
196 if not self.use_24:
197 gl.winconstraints()
198 return
199 gl.prefsize(self.maxx, self.maxy)
200 gl.winconstraints()
201 self.bindvideo()
202
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000203 def bindvideo(self):
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000204 if not self.video: return
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000205 x, y = gl.getsize()
206 self.video.SetSize(x, y)
207 drop = self.b_drop.get_button()
208 if drop:
209 param = [SV.FIELDDROP, 1, SV.GENLOCK, SV.GENLOCK_OFF]
210 else:
211 param = [SV.FIELDDROP, 0, SV.GENLOCK, SV.GENLOCK_ON]
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000212 if self.rgb:
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000213 param = param+[SV.COLOR, SV.DEFAULT_COLOR, \
214 SV.DITHER, 1, \
215 SV.INPUT_BYPASS, 0]
216 else:
217 param = param+[SV.COLOR, SV.MONO, SV.DITHER, 0, \
218 SV.INPUT_BYPASS, 1]
219 self.video.BindGLWindow(self.window, SV.IN_REPLACE)
220 self.video.SetParam(param)
221
222 def rebindvideo(self):
223 gl.winset(self.window)
224 self.bindvideo()
225
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000226 def reset(self):
227 self.close_video()
228 self.close_audio()
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000229 if self.vcr:
230 try:
231 ok = self.vcr.still()
232 except VCR.error:
233 pass
234 self.vcr = None
235 self.b_capture.set_button(0)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000236
237 # Event handler (catches resize of video window)
238
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000239 def do_event(self, dev, val):
240 #print 'Event:', dev, val
241 if dev in (DEVICE.WINSHUT, DEVICE.WINQUIT):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000242 self.close()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000243 if dev == DEVICE.REDRAW and val == self.window:
244 self.rebindvideo()
245 self.settitle()
246
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000247 # Video controls: format, mode, file
248
249 def cb_vformat(self, *args):
250 self.reset()
251 self.get_vformat()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000252 if self.mono_use_thresh:
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000253 s = `self.mono_thresh`
254 s = fl.show_input('Please enter mono threshold', s)
255 if s:
256 try:
257 self.mono_thresh = string.atoi(s)
258 except string.atoi_error:
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000259 fl.show_message('Bad input, using', \
260 `self.mono_thresh`, '')
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000261 self.rebindvideo()
262
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000263 def cb_vmode(self, *args):
Guido van Rossumc17c84f1993-05-10 15:45:49 +0000264 if self.vcr:
265 self.vcr = None
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000266 self.vmode = self.c_vmode.get_choice()
267 self.form.freeze_form()
268 self.g_cont.hide_object()
269 self.g_burst.hide_object()
270 self.g_single.hide_object()
271 self.g_vcr.hide_object()
272 if self.vmode == VM_CONT:
273 self.g_cont.show_object()
274 elif self.vmode == VM_BURST:
275 self.g_burst.show_object()
276 elif self.vmode == VM_SINGLE:
277 self.g_single.show_object()
278 elif self.vmode == VM_VCR:
279 self.g_vcr.show_object()
280 self.form.unfreeze_form()
281
282 def cb_vfile(self, *args):
283 filename = self.vfile
284 hd, tl = os.path.split(filename)
285 filename = fl.file_selector('Video save file:', hd, '', tl)
286 if filename:
287 self.reset()
288 hd, tl = os.path.split(filename)
289 if hd == os.getcwd():
290 filename = tl
291 self.vfile = filename
292
293 # Video mode specific video controls
294
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000295 def cb_rate(self, *args):
296 pass
297
298 def cb_drop(self, *args):
299 self.rebindvideo()
300
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000301 def cb_maxmem(self, *args):
302 pass
303
304 def cb_nframes(self, *args):
305 pass
306
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000307 def cb_fps(self, *args):
308 pass
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000309
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000310 def cb_nframes_vcr(self, *args):
311 pass
312
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000313 def cb_rate_vcr(self, *args):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000314 pass
315
Jack Jansen672754a1993-06-08 12:52:41 +0000316 def cb_vcrspeed(self, *args):
317 pass
318
319 def cb_rgb24_size(self, *args):
320 i = self.c_rgb24_size.get_choice()
321 if i:
322 self.rgb24_size = i
323
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000324 # Audio controls: format, file
325
326 def cb_aformat(self, *args):
327 self.get_aformat()
328
329 def cb_afile(self, *args):
330 filename = self.afile
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000331 hd, tl = os.path.split(filename)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000332 filename = fl.file_selector('Audio save file:', hd, '', tl)
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000333 if filename:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000334 self.reset()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000335 hd, tl = os.path.split(filename)
336 if hd == os.getcwd():
337 filename = tl
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000338 self.afile = filename
339
340 # General controls: capture, reset, play, quit
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000341
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000342 def cb_capture(self, *args):
Guido van Rossumc5a14331993-05-07 11:20:07 +0000343 if self.capturing:
344 raise StopCapture
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000345 if not self.b_capture.get_button():
346 return
347 if not self.video or not self.vformat:
348 gl.ringbell()
349 return
350 if self.vmode == VM_CONT:
351 self.cont_capture()
352 elif self.vmode == VM_BURST:
353 self.burst_capture()
354 elif self.vmode == VM_SINGLE:
Jack Jansen672754a1993-06-08 12:52:41 +0000355 self.single_capture(None, None)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000356 elif self.vmode == VM_VCR:
357 self.vcr_capture()
358
359 def cb_reset(self, *args):
360 self.reset()
Guido van Rossumc5a14331993-05-07 11:20:07 +0000361
362 def cb_play(self, *args):
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000363 self.reset()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000364 sts = os.system('Vplay -q ' + self.vfile + ' &')
Guido van Rossumc5a14331993-05-07 11:20:07 +0000365
366 def cb_quit(self, *args):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000367 self.close()
368
369 # Capture routines
Guido van Rossumc5a14331993-05-07 11:20:07 +0000370
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000371 def burst_capture(self):
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000372 self.setwatch()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000373 gl.winset(self.window)
374 x, y = gl.getsize()
Jack Jansen672754a1993-06-08 12:52:41 +0000375 if self.use_24:
376 fl.show_message('Sorry, no 24 bit continuous capture yet', '', '')
377 return
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000378 vformat = SV.RGB8_FRAMES
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000379 nframes = self.getint(self.in_nframes, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000380 if nframes == 0:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000381 maxmem = self.getint(self.in_maxmem, 1.0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000382 memsize = int(maxmem * 1024 * 1024)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000383 nframes = self.calcnframes()
384 info = (vformat, x, y, nframes, 1)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000385 try:
386 info2, data, bitvec = self.video.CaptureBurst(info)
387 except sv.error, msg:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000388 self.b_capture.set_button(0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000389 self.setarrow()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000390 fl.show_message('Capture error:', str(msg), '')
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000391 return
Guido van Rossumc5a14331993-05-07 11:20:07 +0000392 if info <> info2: print info, '<>', info2
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000393 self.save_burst(info2, data, bitvec)
394 self.setarrow()
395
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000396 def calcnframes(self):
397 gl.winset(self.window)
398 x, y = gl.getsize()
399 pixels = x*y
400 pixels = pixels/2 # XXX always assume fields
401 if self.mono or self.grey:
402 n = memsize/pixels
403 else:
404 n = memsize/(4*pixels)
405 return max(1, n)
406
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000407 def save_burst(self, info, data, bitvec):
408 (vformat, x, y, nframes, rate) = info
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000409 self.open_if_closed()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000410 fieldsize = x*y/2
411 nskipped = 0
412 realframeno = 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000413 tpf = 1000 / 50.0 # XXX
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000414 for frameno in range(0, nframes*2):
415 if frameno <> 0 and \
416 bitvec[frameno] == bitvec[frameno-1]:
417 nskipped = nskipped + 1
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000418 continue
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000419 #
420 # Save field.
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000421 # XXX Works only for fields and top-to-bottom
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000422 #
423 start = frameno*fieldsize
424 field = data[start:start+fieldsize]
425 realframeno = realframeno + 1
426 fn = int(realframeno*tpf)
427 if not self.write_frame(fn, field):
428 break
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000429
430 def cont_capture(self):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000431 saved_label = self.b_capture.label
432 self.b_capture.label = 'Stop\n' + saved_label
433 self.open_if_closed()
434 self.init_cont()
435 fps = 59.64 # Fields per second
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000436 # XXX (fps of Indigo monitor, not of PAL or NTSC!)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000437 tpf = 1000.0 / fps # Time per field in msec
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000438 self.capturing = 1
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000439 self.start_audio()
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000440 while 1:
441 try:
442 void = fl.check_forms()
443 except StopCapture:
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000444 break
445 try:
446 cd, id = self.video.GetCaptureData()
447 except sv.error:
448 sgi.nap(1)
449 continue
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000450 id = id + 2*self.rate
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000451 data = cd.InterleaveFields(1)
452 cd.UnlockCaptureData()
453 t = id*tpf
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000454 if not self.write_frame(t, data):
Guido van Rossume17c6c31993-05-06 16:27:25 +0000455 break
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000456 self.stop_audio()
Guido van Rossume17c6c31993-05-06 16:27:25 +0000457 self.capturing = 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000458 self.end_cont()
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000459 if self.aout:
460 # If recording audio, can't capture multiple sequences
461 self.reset()
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000462 self.b_capture.label = saved_label
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000463
Jack Jansen672754a1993-06-08 12:52:41 +0000464 def single_capture(self, stepfunc, timecode):
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000465 self.open_if_closed()
466 self.init_cont()
467 while 1:
468 try:
469 cd, id = self.video.GetCaptureData()
470 break
471 except sv.error:
472 pass
473 sgi.nap(1)
Jack Jansen672754a1993-06-08 12:52:41 +0000474 if stepfunc: # This might step the video
475 d=stepfunc() # to the next frame
476 if not self.use_24:
477 data = cd.InterleaveFields(1)
478 else:
479 x, y = self.vout.getsize()
480 if self.rgb24_size == 1:
481 data = cd.YUVtoRGB(1)
482 elif self.rgb24_size == 2:
483 data = cd.YUVtoRGB_quarter(1)
484 x = x/2
485 y = y/2
486 elif self.rgb24_size == 3:
487 data = cd.YUVtoRGB_sixteenth(1)
488 x = x/4
489 y = y/4
490 else:
491 raise 'Kaboo! Kaboo!'
492 if self.use_jpeg:
493 import jpeg
494 data = jpeg.compress(data, x, y, 4)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000495 cd.UnlockCaptureData()
496 self.end_cont()
Jack Jansen672754a1993-06-08 12:52:41 +0000497 if timecode == None:
498 timecode = (self.nframes+1) * (1000/25)
499 return self.write_frame(timecode, data)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000500
501 def vcr_capture(self):
502 if not self.vcr:
503 import VCR
504 try:
505 self.vcr = VCR.VCR().init()
Guido van Rossumc17c84f1993-05-10 15:45:49 +0000506 self.vcr.wait()
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000507 if not (self.vcr.fmmode('dnr') and \
508 self.vcr.dmcontrol('digital slow')):
509 self.vcr_error('digital slow failed')
510 return
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000511 except VCR.error, msg:
Guido van Rossumc17c84f1993-05-10 15:45:49 +0000512 self.vcr = None
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000513 self.vcr_error(msg)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000514 return
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000515 if not self.vcr.still():
516 self.vcr_error('still failed')
517 return
518 # XXX for some reason calling where() too often hangs the VCR,
519 # XXX so we insert sleep(0.1) before every sense() call.
520 self.open_if_closed()
521 rate = self.getint(self.in_rate_vcr, 1)
522 rate = max(rate, 1)
Jack Jansen672754a1993-06-08 12:52:41 +0000523 vcrspeed = self.c_vcrspeed.get_choice()
524 vcrspeed = VcrSpeeds[vcrspeed]
525 if vcrspeed == 0:
526 stepfunc = self.vcr.step
527 else:
528 stepfunc = None
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000529 self.speed_factor = rate
Jack Jansen672754a1993-06-08 12:52:41 +0000530 addr = start_addr = self.vcr.sense()
531 if not self.single_capture(None, 0):
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000532 return
533 print 'captured %02d:%02d:%02d:%02d' % self.vcr.addr2tc(addr)
534 count = self.getint(self.in_nframes_vcr, 1) - 1
535 if count <= 0:
536 while rate > 0:
537 if not self.vcr.step():
538 self.vcr_error('step failed')
539 here = self.vcr.sense()
540 if here > addr:
541 rate = rate - (here - addr)
542 addr = here
543 return
Jack Jansen672754a1993-06-08 12:52:41 +0000544 if not self.vcr.fwdshuttle(vcrspeed): # one tenth speed
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000545 self.vcr_error('fwd shuttle failed')
546 return
547 cycle = 0
548 while count > 0:
549 time.sleep(0.1)
550 try:
551 here = self.vcr.sense()
552 except VCR.error, msg:
553 self.vcr_error(msg)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000554 break
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000555 if here <> addr:
556 if here <> addr+1:
557 print 'Missed', here-addr-1,
558 print 'frame' + 's'*(here-addr-1 <> 1)
559 cycle = (cycle+1) % rate
560 if cycle == 0:
Jack Jansen672754a1993-06-08 12:52:41 +0000561 tc = (here-start_addr)*40
562 if not self.single_capture(stepfunc, \
563 tc):
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000564 break
565 print 'captured %02d:%02d:%02d:%02d' \
566 % self.vcr.addr2tc(here)
567 count = count -1
568 addr = here
569 if self.vcr and not self.vcr.still():
570 self.vcr_error('still failed')
571
572 def vcr_error(self, msg):
573 self.reset()
574 fl.show_message('VCR error:', str(msg), '')
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000575
576 # Init/end continuous capture mode
577
578 def init_cont(self):
579 qsize = 1
580 if self.vmode == VM_CONT:
581 self.rate = self.getint(self.in_rate, 2)
582 else:
583 self.rate = 2
584 x, y = self.vout.getsize()
Jack Jansen672754a1993-06-08 12:52:41 +0000585 if self.use_24:
586 info = (SV.YUV411_FRAMES, x, y, qsize, self.rate)
587 else:
588 info = (SV.RGB8_FRAMES, x, y, qsize, self.rate)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000589 info2 = self.video.InitContinuousCapture(info)
590 if info2 <> info:
591 # XXX This is really only debug info
592 print 'Info mismatch: requested', info, 'got', info2
593
594 def end_cont(self):
595 self.video.EndContinuousCapture()
596
597 # Misc stuff
598
599 def settitle(self):
600 gl.winset(self.window)
601 x, y = gl.getsize()
602 title = 'Vb:' + self.vfile + ' (%dx%d)' % (x, y)
603 gl.wintitle(title)
604
605 def get_vformat(self):
606 i = self.c_vformat.get_choice()
607 label = VideoFormatLabels[i-1]
608 format = VideoFormats[i-1]
609 self.vformat = format
610 if self.vformat == '':
611 self.form.freeze_form()
612 self.g_video.hide_object()
613 self.g_cont.hide_object()
614 self.g_burst.hide_object()
615 self.g_single.hide_object()
616 self.form.unfreeze_form()
617 return
618 else:
619 self.g_video.show_object()
620 if self.vmode == VM_CONT:
621 self.g_cont.show_object()
622 elif self.vmode == VM_BURST:
623 self.g_burst.show_object()
624 elif self.vmode == VM_SINGLE:
625 self.g_single.show_object()
Guido van Rossumc5a14331993-05-07 11:20:07 +0000626 #
627 self.rgb = (format[:3] == 'rgb')
628 self.mono = (format == 'mono')
629 self.grey = (format[:4] == 'grey')
Jack Jansen672754a1993-06-08 12:52:41 +0000630 self.use_24 = (format in ('rgb', 'jpeg'))
631 # Does not work.... if self.use_24:
632 if 0:
633 self.g_rgb24.show_object()
634 else:
635 self.g_rgb24.hide_object()
636 self.use_jpeg = (format == 'jpeg')
Guido van Rossumc5a14331993-05-07 11:20:07 +0000637 self.mono_use_thresh = (label == 'mono thresh')
638 s = format[4:]
639 if s:
640 self.greybits = string.atoi(s)
641 else:
642 self.greybits = 8
643 if label == 'grey2 dith':
644 self.greybits = -2
645 #
646 convertor = None
647 if self.grey:
648 if self.greybits == 2:
649 convertor = imageop.grey2grey2
650 elif self.greybits == 4:
651 convertor = imageop.grey2grey4
652 elif self.greybits == -2:
653 convertor = imageop.dither2grey2
654 self.convertor = convertor
Jack Jansen672754a1993-06-08 12:52:41 +0000655 self.optfullsizewindow()
Guido van Rossumc5a14331993-05-07 11:20:07 +0000656
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000657 def get_aformat(self):
658 self.reset()
659 self.aformat = self.c_aformat.get_choice()
660 if self.aformat == A_OFF:
661 self.g_audio.hide_object()
662 else:
663 self.g_audio.show_object()
664
665 def open_if_closed(self):
666 if not self.vout:
667 self.open_video()
668 if not self.aout:
669 self.open_audio()
670
671 # File I/O handling
672
673 def open_video(self):
674 self.close_video()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000675 gl.winset(self.window)
676 x, y = gl.getsize()
Jack Jansen672754a1993-06-08 12:52:41 +0000677 if self.use_24:
678 if self.rgb24_size == 2:
679 x, y = x/2, y/2
680 elif self.rgb24_size == 4:
681 x, y = x/4, y/4
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000682 vout = VFile.VoutFile().init(self.vfile)
683 vout.setformat(self.vformat)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000684 vout.setsize(x, y)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000685 if self.vmode == VM_BURST:
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000686 vout.setpf((1, -2))
687 vout.writeheader()
688 self.vout = vout
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000689 self.nframes = 0
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000690 self.speed_factor = 1
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000691 self.t_nframes.label = `self.nframes`
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000692
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000693 def write_frame(self, t, data):
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000694 t = t * self.speed_factor
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000695 if not self.vout:
696 gl.ringbell()
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000697 return 0
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000698 if self.convertor:
699 data = self.convertor(data, len(data), 1)
700 elif self.mono:
701 if self.mono_use_thresh:
702 data = imageop.grey2mono(data, \
703 len(data), 1,\
704 self.mono_thresh)
705 else:
706 data = imageop.dither2mono(data, \
707 len(data), 1)
708 try:
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000709 self.vout.writeframe(int(t), data, None)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000710 except IOError, msg:
Guido van Rossumad4fcd41993-05-11 18:36:54 +0000711 self.reset()
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000712 if msg == (0, 'Error 0'):
713 msg = 'disk full??'
714 fl.show_message('IOError', str(msg), '')
715 return 0
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000716 self.nframes = self.nframes + 1
717 self.t_nframes.label = `self.nframes`
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000718 return 1
719
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000720 def close_video(self):
721 if not self.vout:
722 return
723 self.nframes = 0
724 self.t_nframes.label = ''
Guido van Rossumc5a14331993-05-07 11:20:07 +0000725 try:
726 self.vout.close()
727 except IOError, msg:
728 if msg == (0, 'Error 0'):
729 msg = 'disk full??'
730 fl.show_message('IOError', str(msg), '')
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000731 self.vout = None
732
733 # Watch cursor handling
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000734
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000735 def setwatch(self):
736 gl.winset(self.form.window)
737 gl.setcursor(WATCH, 0, 0)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000738 gl.winset(self.window)
739 gl.setcursor(WATCH, 0, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000740
741 def setarrow(self):
742 gl.winset(self.form.window)
743 gl.setcursor(ARROW, 0, 0)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000744 gl.winset(self.window)
745 gl.setcursor(ARROW, 0, 0)
Guido van Rossumbc6d3c31993-05-07 09:37:42 +0000746
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000747 # Numeric field handling
748
749 def getint(self, field, default):
750 try:
751 value = string.atoi(field.get_input())
752 except string.atoi_error:
753 value = default
754 field.set_input(`value`)
755 return value
756
757 def getfloat(self, field, default):
758 try:
759 value = float(eval(field.get_input()))
760 except:
761 value = float(default)
Guido van Rossumc17c84f1993-05-10 15:45:49 +0000762 field.set_input(`value`)
Guido van Rossum9f42f4f1993-05-10 15:07:20 +0000763 return value
764
765 # Audio stuff
766
767 def open_audio(self):
768 if self.aformat == A_OFF:
769 return
770 import aifc
771 import al
772 import AL
773 import thread
774 self.close_audio()
775 params = [AL.INPUT_RATE, 0]
776 al.getparams(AL.DEFAULT_DEVICE, params)
777 rate = params[1]
778 self.aout = aifc.open(self.afile, 'w')
779 if self.aformat in (A_16_STEREO, A_8_STEREO):
780 nch = AL.STEREO
781 else:
782 nch = AL.MONO
783 if self.aformat in (A_16_STEREO, A_16_MONO):
784 width = AL.SAMPLE_16
785 else:
786 width = AL.SAMPLE_8
787 self.aout.setnchannels(nch)
788 self.aout.setsampwidth(width)
789 self.aout.setframerate(rate)
790 self.aout.writeframes('')
791 c = al.newconfig()
792 c.setqueuesize(8000)
793 c.setchannels(nch)
794 c.setwidth(width)
795 self.aport = al.openport('Vb audio record', 'r', c)
796 self.audio_stop = 0
797 self.audio_ok = 0
798 self.audio_busy = 1
799 thread.start_new_thread(self.record_audio, ())
800
801 def start_audio(self):
802 if self.aformat == A_OFF:
803 return
804 self.audio_ok = 1
805
806 def record_audio(self, *args):
807 # This function runs in a separate thread
808 # Currently no semaphores are used
809 while not self.audio_stop:
810 data = self.aport.readsamps(4000)
811 if self.audio_ok:
812 self.aout.writeframesraw(data)
813 data = None
814 self.audio_busy = 0
815
816 def stop_audio(self):
817 self.audio_ok = 0
818
819 def close_audio(self):
820 if self.aout:
821 self.audio_ok = 0
822 self.audio_stop = 1
823 while self.audio_busy:
824 time.sleep(0.1)
825 self.aout.close()
826 self.aout = None
827 if self.aport:
828 self.aport.closeport()
829 self.aport = None
830
Guido van Rossumb7e3cc11993-05-06 16:06:44 +0000831
832try:
833 main()
834except KeyboardInterrupt:
835 print '[Interrupt]'
836 sys.exit(1)