2 function: Ogg Writ 1.2 reference encoder
3 last mod: $Id: writ_encoder.py,v 1.2 2004/02/24 06:31:49 arc Exp $
5 This is an example for how py-ogg2 can be used to rapidly design a new
6 Ogg codec or test an existing codec's specifications for accuracy.
16 self
.pb
= ogg2
.OggPackBuff()
18 def uniwrite(self
,ustring
) :
21 for pos
in range(len(ustring
)) :
22 value
= ord(ustring
[pos
])
27 elif value
< 16777216 :
31 total
= total
+ (size
/8)
32 output
.append((value
,size
))
34 raise('ValueError: Unicode must be no greater than 255 bytes long')
35 self
.pb
.write(total
,8)
37 self
.pb
.write(char
[0],char
[1])
39 def write(self
,value
,size
) :
42 lo
=value
-(hi
*4294967296)
44 self
.pb
.write(hi
,size
-32)
46 self
.pb
.write(value
,size
)
49 return self
.pb
.packetout()
52 self
.pb
= ogg2
.OggPackBuff()
53 # return self.pb.reset()
57 self
.os
= ogg2
.OggStreamState(random
.randrange(2**24))
58 self
.os
.setmode(ogg2
.Ogg_Discont
)
59 self
.oy
= ogg2
.OggSyncState()
60 self
.fd
= open('test.writ.ogg','w')
63 def packetin(self
, packet
, end_granule
=0) :
64 packet
.packetno
= self
.pn
65 packet
.end_granule
= end_granule
69 packetno
= self
.packet
.packetno
70 self
.os
.packetin(self
.packet
)
77 page
= self
.os
.flush()
81 while len(output
)!= 0 :
82 output
= self
.oy
.read(4096)
87 self
.os
.packetin(self
.packet
)
102 langs
=(('en','English'),('es','Spanish'))
104 # winds: location_x, location_y, width, height, alignment_x, alignment_y
109 totl
= (bitx
*2)+(bity
*2)+4
110 bitp
= ((((totl
-1)/8)+1)*8)-totl
111 winds
=((1,2,3,1,3,3),(5,6,7,1,3,3))
113 # texts: start, duration, lang(s), wind
114 texts
=((05,10, ( u
'Hello World!' ,
115 u
'Hola, Mundo!' ), 0),
116 (12,15, ( u
'It\'s a beautiful day to be born.' ,
117 u
'Es un d\N{LATIN SMALL LETTER I WITH ACUTE}'+\
118 u
'a hermoso para que se llevar'+\
119 u
'\N{LATIN SMALL LETTER A WITH ACUTE}.' ), 1) )
124 # Start with Header 0
125 bp
.write(0,8) # header packet 0
126 bp
.write(1953067639,32) # "writ"
127 bp
.write(1,8) # version = 1
128 bp
.write(2,8) # subversion = 2
129 bp
.write(gnum
,32) # granulerate_numerator
130 bp
.write(gden
,32) # granulerate_denominator
131 os
.packetin(bp
.export())
134 # Start with Header 1
135 bp
.write(1,8) # header packet 1
136 bp
.write(1953067639,32) # "writ"
137 bp
.write(len(langs
)-1,8) # num_languages
139 bp
.uniwrite(lang
[0]) # lang_name length, string
140 bp
.uniwrite(lang
[1]) # lang_desc length, string
141 os
.packetin(bp
.export())
144 # Start with Header 1
145 bp
.write(2,8) # header packet 2
146 bp
.write(1953067639,32) # "writ"
147 bp
.write(sclx
,16) # location_scale_x
148 bp
.write(scly
,16) # location_scale_y
149 bp
.write(len(winds
),8) # num_windows
151 bp
.write(win
[0],bitx
) # location_x
152 bp
.write(win
[1],bity
) # location_x
153 bp
.write(win
[2],bitx
) # location_width
154 bp
.write(win
[3],bity
) # location_height
155 bp
.write(win
[4],2) # alignment_x
156 bp
.write(win
[5],2) # alignment_y
157 os
.packetin(bp
.export())
160 for text
in texts
: # - Each Phrase -
161 bp
.write(255,8) # data packet
162 bp
.write(text
[0],64) # granule_start
163 bp
.write(text
[1],32) # granule_duration
164 for lang
in range(len(langs
)):
165 bp
.uniwrite(text
[2][lang
]) # text length, string
166 if len(winds
) > 1 : # only if we need to specify
167 bp
.write(text
[3],8) # window_id
168 os
.packetin(bp
.export(), text
[0])