Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78991fd

Browse files
committed
VFile - Added support for creating compression lib movies
Vb, VbForm - Compression lib movie support Save settings in ~/.Vb_init
1 parent f1cda91 commit 78991fd

3 files changed

Lines changed: 242 additions & 54 deletions

File tree

Demo/sgi/video/VFile.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ def getinfo(self):
304304
self.c0bits, self.c1bits, self.c2bits, self.offset, \
305305
self.chrompack)
306306

307+
def getcompressheader(self):
308+
return self.compressheader
309+
310+
def setcompressheader(self, ch):
311+
self.compressheader = ch
312+
307313
# Write the relevant bits to stdout
308314

309315
def printinfo(self):
@@ -765,6 +771,24 @@ def writefileheader(fp, values):
765771
#
766772
data = (width, height, packfactor)
767773
fp.write(`data`+'\n')
774+
775+
def writecompressfileheader(fp, cheader, values):
776+
(format, width, height, packfactor, \
777+
c0bits, c1bits, c2bits, offset, chrompack) = values
778+
#
779+
# Write identifying header
780+
#
781+
fp.write('CMIF video 3.1\n')
782+
#
783+
# Write color encoding info
784+
#
785+
data = (format, cheader)
786+
fp.write(`data`+'\n')
787+
#
788+
# Write frame geometry info
789+
#
790+
data = (width, height, packfactor)
791+
fp.write(`data`+'\n')
768792

769793

770794
# Basic class for reading CMIF video files
@@ -1058,7 +1082,11 @@ def prealloc(self, nframes):
10581082

10591083
def writeheader(self):
10601084
if self.frozen: raise CallError
1061-
writefileheader(self.fp, self.getinfo())
1085+
if self.format == 'compress':
1086+
writecompressfileheader(self.fp, self.compressheader, \
1087+
self.getinfo())
1088+
else:
1089+
writefileheader(self.fp, self.getinfo())
10621090
self.freeze()
10631091
self.atheader = 1
10641092
self.framecount = 0

Demo/sgi/video/Vb.py

Lines changed: 146 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
import imageop
3232
sys.path.append('/ufs/jack/src/av/vcr')
3333
import VCR
34+
try:
35+
import cl
36+
import CL
37+
except ImportError:
38+
cl = None
3439

3540
ARROW = 0
3641
WATCH = 1
@@ -46,9 +51,10 @@ def main():
4651
StopCapture = 'StopCapture'
4752

4853
VideoFormatLabels = ['Video off', 'rgb8', 'grey8', 'grey4', 'grey2', \
49-
'grey2 dith', 'mono dith', 'mono thresh', 'rgb24', 'rgb24-jpeg']
54+
'grey2 dith', 'mono dith', 'mono thresh', 'rgb24', 'rgb24-jpeg', \
55+
'compress']
5056
VideoFormats = ['', 'rgb8', 'grey', 'grey4', 'grey2', \
51-
'grey2', 'mono', 'mono', 'rgb', 'jpeg']
57+
'grey2', 'mono', 'mono', 'rgb', 'jpeg', 'compress']
5258

5359
VideoModeLabels = ['Continuous', 'Burst', 'Single frame', 'VCR sync']
5460
[VM_CONT, VM_BURST, VM_SINGLE, VM_VCR] = range(1, 5)
@@ -62,6 +68,16 @@ def main():
6268

6369
RgbSizeLabels = ['full', 'quarter', 'sixteenth']
6470

71+
# init file stuff:
72+
if os.environ.has_key('HOME'):
73+
HOME=os.environ['HOME']
74+
else:
75+
HOME='.'
76+
VB_INIT_FILE=HOME + '/.Vb_init'
77+
78+
VB_INIT_KEYS=['vfile', 'vmode', 'mono_thresh', 'vformat', 'comp_scheme', \
79+
'rgb24_size', 'afile', 'aformat']
80+
6581
class VideoBagOfTricks:
6682

6783
# Init/close stuff
@@ -70,21 +86,31 @@ def init(self):
7086
self.window = None
7187
formdef = flp.parse_form('VbForm', 'form')
7288
flp.create_full_form(self, formdef)
73-
self.g_cont.hide_object()
74-
self.g_burst.hide_object()
75-
self.g_single.hide_object()
76-
self.g_vcr.hide_object()
7789
self.setdefaults()
90+
if self.vmode <> VM_CONT:
91+
self.g_cont.hide_object()
92+
if self.vmode <> VM_BURST:
93+
self.g_burst.hide_object()
94+
if self.vmode <> VM_SINGLE:
95+
self.g_single.hide_object()
96+
if self.vmode <> VM_VCR:
97+
self.g_vcr.hide_object()
98+
if self.vformat <> 'compress':
99+
self.g_compress.hide_object()
100+
78101
self.openvideo()
79102
self.makewindow()
80103
self.bindvideo()
104+
if self.use_24:
105+
self.optfullsizewindow()
81106
self.showform()
82107
fl.set_event_call_back(self.do_event)
83108
return self
84109

85110
def close(self):
86111
self.close_video()
87112
self.close_audio()
113+
self.savedefaults()
88114
raise SystemExit, 0
89115

90116
def showform(self):
@@ -101,15 +127,56 @@ def showform(self):
101127
gl.prefposition(x1, x2, y1, y2)
102128
self.form.show_form(FL.PLACE_FREE, FL.TRUE, 'Vb Control')
103129

104-
def setdefaults(self):
105-
self.vcr = None
106-
self.vout = None
107-
self.capturing = 0
130+
def getdefaultdefaults(self):
108131
# Video defaults
109132
self.vfile = 'film.video'
110133
self.vmode = VM_CONT
111134
self.mono_thresh = 128
112135
self.vformat = 'rgb8'
136+
self.comp_scheme = 'Uncompressed'
137+
self.rgb24_size = 1
138+
# Missing: drop, rate, maxmem, nframes, rate, vcrspeed
139+
# Audio defaults:
140+
self.afile = 'film.aiff'
141+
self.aformat = A_OFF
142+
143+
def getdefaults(self):
144+
self.getdefaultdefaults()
145+
# XXXX Read defaults file and override.
146+
try:
147+
fp = open(VB_INIT_FILE, 'r')
148+
except IOError:
149+
print 'Vb: no init file'
150+
self.initcont = {}
151+
return
152+
data = fp.read(1000000)
153+
try:
154+
self.initcont = eval(data)
155+
except:
156+
print 'Vb: Ill-formatted init file'
157+
self.initcont = {}
158+
for k in self.initcont.keys():
159+
if hasattr(self, k):
160+
setattr(self, k, self.initcont[k])
161+
162+
def savedefaults(self):
163+
newdb = {}
164+
for k in VB_INIT_KEYS:
165+
newdb[k] = getattr(self, k)
166+
if newdb <> self.initcont:
167+
try:
168+
fp = open(VB_INIT_FILE, 'w')
169+
except IOError:
170+
print 'Vb: Cannot create', VB_INIT_FILE
171+
return
172+
fp.write(`newdb`)
173+
fp.close()
174+
175+
def setdefaults(self):
176+
self.getdefaults()
177+
self.vcr = None
178+
self.vout = None
179+
self.capturing = 0
113180
self.c_vformat.clear_choice()
114181
for label in VideoFormatLabels:
115182
self.c_vformat.addto_choice(label)
@@ -132,13 +199,21 @@ def setdefaults(self):
132199
self.c_rgb24_size.clear_choice()
133200
for label in RgbSizeLabels:
134201
self.c_rgb24_size.addto_choice(label)
135-
self.c_rgb24_size.set_choice(1)
136-
self.rgb24_size = 1
202+
self.c_rgb24_size.set_choice(self.rgb24_size)
203+
if cl:
204+
algs = cl.QueryAlgorithms(CL.VIDEO)
205+
self.all_comp_schemes = []
206+
for i in range(0, len(algs), 2):
207+
if algs[i+1] in (CL.COMPRESSOR, CL.CODEC):
208+
self.all_comp_schemes.append(algs[i])
209+
self.c_cformat.clear_choice()
210+
for label in self.all_comp_schemes:
211+
self.c_cformat.addto_choice(label)
212+
i = self.all_comp_schemes.index(self.comp_scheme)
213+
self.c_cformat.set_choice(i+1)
137214
# Audio defaults
138215
self.aout = None
139216
self.aport = None
140-
self.afile = 'film.aiff'
141-
self.aformat = A_OFF
142217
self.c_aformat.clear_choice()
143218
for label in AudioFormatLabels:
144219
self.c_aformat.addto_choice(label)
@@ -276,6 +351,11 @@ def cb_vformat(self, *args):
276351
`self.mono_thresh`, '')
277352
self.rebindvideo()
278353

354+
def cb_cformat(self, *args):
355+
i = self.c_cformat.get_choice()
356+
self.comp_scheme = self.all_comp_schemes[i-1]
357+
358+
279359
def cb_vmode(self, *args):
280360
if self.vcr:
281361
self.vcr = None
@@ -493,21 +573,33 @@ def single_capture(self, stepfunc, timecode):
493573
data = cd.InterleaveFields(1)
494574
else:
495575
x, y = self.vout.getsize()
496-
if self.rgb24_size == 1:
497-
data = cd.YUVtoRGB(1)
498-
elif self.rgb24_size == 2:
499-
data = cd.YUVtoRGB_quarter(1)
500-
x = x/2
501-
y = y/2
502-
elif self.rgb24_size == 3:
503-
data = cd.YUVtoRGB_sixteenth(1)
504-
x = x/4
505-
y = y/4
576+
if self.use_compress:
577+
if self.rgb24_size == 1:
578+
data = cd.YUVtoYUV422DC(0)
579+
elif self.rgb24_size == 2:
580+
data = cd.YUVtoYUV422DC_quarter(1)
581+
x = x/2
582+
y = y/2
583+
elif self.rgb24_size == 3:
584+
data = cd.YUVtoYUV422DC_sixteenth(1)
585+
x = x/4
586+
y = y/4
506587
else:
507-
raise 'Kaboo! Kaboo!'
588+
data = cd.YUVtoRGB(1)
589+
if self.maxx*self.maxy*4 <> len(data):
590+
print 'maxx,maxy,exp,got=', self.maxx,
591+
print self.maxy,self.maxx*self.maxy*4,
592+
print len(data)
593+
fl.showmessage('Wrong sized data')
594+
return 0
595+
if self.rgb24_size <> 1:
596+
data = imageop.scale(data, 4, \
597+
self.maxx, self.maxy, x, y)
508598
if self.use_jpeg:
509599
import jpeg
510600
data = jpeg.compress(data, x, y, 4)
601+
if self.use_compress:
602+
data = self.compressor.Compress(1, data)
511603
cd.UnlockCaptureData()
512604
self.end_cont()
513605
if timecode == None:
@@ -520,7 +612,7 @@ def vcr_capture(self):
520612
print 'Connecting to VCR ...'
521613
self.vcr = VCR.VCR().init()
522614
print 'Waiting for VCR to come online ...'
523-
self.vcr.wait()
615+
self.vcr.initvcr()
524616
print 'Preparing VCR ...'
525617
if not (self.vcr.fmmode('dnr') and \
526618
self.vcr.dmcontrol('digital slow')):
@@ -622,6 +714,10 @@ def get_vformat(self):
622714
i = self.c_vformat.get_choice()
623715
label = VideoFormatLabels[i-1]
624716
format = VideoFormats[i-1]
717+
if format == 'compress' and cl == None:
718+
fl.show_message('Sorry, no compression library support')
719+
format = ''
720+
label = 'Video off'
625721
self.vformat = format
626722
if self.vformat == '':
627723
self.form.freeze_form()
@@ -639,18 +735,23 @@ def get_vformat(self):
639735
elif self.vmode == VM_SINGLE:
640736
self.g_single.show_object()
641737
#
642-
self.rgb = (format[:3] == 'rgb')
738+
self.rgb = (format[:3] == 'rgb' or format == 'compress')
643739
self.mono = (format == 'mono')
644740
self.grey = (format[:4] == 'grey')
645-
self.use_24 = (format in ('rgb', 'jpeg'))
646-
if self.use_24 and 0: ### quarter/sixteenth decoding not impl.
741+
self.use_24 = (format in ('rgb', 'jpeg', 'compress'))
742+
if self.use_24:
647743
self.g_rgb24.show_object()
648744
else:
649745
self.g_rgb24.hide_object()
650746
self.use_jpeg = (format == 'jpeg')
651747
self.mono_use_thresh = (label == 'mono thresh')
748+
self.use_compress = (format == 'compress')
749+
if self.use_compress:
750+
self.g_compress.show_object()
751+
else:
752+
self.g_compress.hide_object()
652753
s = format[4:]
653-
if s:
754+
if self.grey and s:
654755
self.greybits = string.atoi(s)
655756
else:
656757
self.greybits = 8
@@ -676,6 +777,16 @@ def get_aformat(self):
676777
else:
677778
self.g_audio.show_object()
678779

780+
def init_compressor(self, w, h):
781+
self.compressor = None
782+
scheme = cl.QuerySchemeFromName(CL.VIDEO, self.comp_scheme)
783+
self.compressor = cl.OpenCompressor(scheme)
784+
parambuf = [CL.IMAGE_WIDTH, w, \
785+
CL.IMAGE_HEIGHT, h, \
786+
CL.ORIGINAL_FORMAT, CL.YUV422DC]
787+
self.compressor.SetParams(parambuf)
788+
return self.compressor.Compress(0, '')
789+
679790
def open_if_closed(self):
680791
if not self.vout:
681792
self.open_video()
@@ -691,10 +802,13 @@ def open_video(self):
691802
if self.use_24:
692803
if self.rgb24_size == 2:
693804
x, y = x/2, y/2
694-
elif self.rgb24_size == 4:
805+
elif self.rgb24_size == 3:
695806
x, y = x/4, y/4
696807
vout = VFile.VoutFile().init(self.vfile)
697808
vout.setformat(self.vformat)
809+
if self.vformat == 'compress':
810+
cheader = self.init_compressor(x, y)
811+
vout.setcompressheader(cheader)
698812
vout.setsize(x, y)
699813
if self.vmode == VM_BURST:
700814
vout.setpf((1, -2))
@@ -743,6 +857,7 @@ def close_video(self):
743857
msg = 'disk full??'
744858
fl.show_message('IOError', str(msg), '')
745859
self.vout = None
860+
self.compressor = None
746861

747862
# Watch cursor handling
748863

0 commit comments

Comments
 (0)