2001-08-19 19:13:00 -03:00
|
|
|
# Parsers/generators for QuickTime media descriptions
|
|
|
|
import struct
|
|
|
|
|
|
|
|
Error = 'MediaDescr.Error'
|
|
|
|
|
|
|
|
class _MediaDescriptionCodec:
|
2004-07-18 03:16:08 -03:00
|
|
|
def __init__(self, trunc, size, names, fmt):
|
|
|
|
self.trunc = trunc
|
|
|
|
self.size = size
|
|
|
|
self.names = names
|
|
|
|
self.fmt = fmt
|
2003-04-06 06:01:11 -03:00
|
|
|
|
2004-07-18 03:16:08 -03:00
|
|
|
def decode(self, data):
|
|
|
|
if self.trunc:
|
|
|
|
data = data[:self.size]
|
|
|
|
values = struct.unpack(self.fmt, data)
|
|
|
|
if len(values) != len(self.names):
|
Merged revisions 78035,78040,78043,78049-78050,78052-78054 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78035 | georg.brandl | 2010-02-06 23:44:17 +0100 (Sa, 06 Feb 2010) | 1 line
Fix duplicate import.
........
r78040 | georg.brandl | 2010-02-07 00:08:00 +0100 (So, 07 Feb 2010) | 1 line
Fix a few UnboundLocalErrors in test_long.
........
r78043 | georg.brandl | 2010-02-07 00:12:19 +0100 (So, 07 Feb 2010) | 1 line
Remove duplicate test method.
........
r78049 | georg.brandl | 2010-02-07 00:33:33 +0100 (So, 07 Feb 2010) | 1 line
Fix import/access for some identifiers. _TestSharedCTypes does not seem to be executed?
........
r78050 | georg.brandl | 2010-02-07 00:34:10 +0100 (So, 07 Feb 2010) | 1 line
Fix more unbound locals in code paths that do not seem to be used.
........
r78052 | georg.brandl | 2010-02-07 00:54:04 +0100 (So, 07 Feb 2010) | 1 line
Add missing import when running these tests standalone.
........
r78053 | georg.brandl | 2010-02-07 00:54:43 +0100 (So, 07 Feb 2010) | 1 line
Fix some name errors in Mac modules.
........
r78054 | georg.brandl | 2010-02-07 00:58:25 +0100 (So, 07 Feb 2010) | 1 line
Add missing import.
........
2010-02-07 08:01:19 -04:00
|
|
|
raise Error, ('Format length does not match number of names')
|
2004-07-18 03:16:08 -03:00
|
|
|
rv = {}
|
|
|
|
for i in range(len(values)):
|
|
|
|
name = self.names[i]
|
|
|
|
value = values[i]
|
|
|
|
if type(name) == type(()):
|
|
|
|
name, cod, dec = name
|
|
|
|
value = dec(value)
|
|
|
|
rv[name] = value
|
|
|
|
return rv
|
2003-04-06 06:01:11 -03:00
|
|
|
|
Merged revisions 78035,78040,78043,78049-78050,78052-78054 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78035 | georg.brandl | 2010-02-06 23:44:17 +0100 (Sa, 06 Feb 2010) | 1 line
Fix duplicate import.
........
r78040 | georg.brandl | 2010-02-07 00:08:00 +0100 (So, 07 Feb 2010) | 1 line
Fix a few UnboundLocalErrors in test_long.
........
r78043 | georg.brandl | 2010-02-07 00:12:19 +0100 (So, 07 Feb 2010) | 1 line
Remove duplicate test method.
........
r78049 | georg.brandl | 2010-02-07 00:33:33 +0100 (So, 07 Feb 2010) | 1 line
Fix import/access for some identifiers. _TestSharedCTypes does not seem to be executed?
........
r78050 | georg.brandl | 2010-02-07 00:34:10 +0100 (So, 07 Feb 2010) | 1 line
Fix more unbound locals in code paths that do not seem to be used.
........
r78052 | georg.brandl | 2010-02-07 00:54:04 +0100 (So, 07 Feb 2010) | 1 line
Add missing import when running these tests standalone.
........
r78053 | georg.brandl | 2010-02-07 00:54:43 +0100 (So, 07 Feb 2010) | 1 line
Fix some name errors in Mac modules.
........
r78054 | georg.brandl | 2010-02-07 00:58:25 +0100 (So, 07 Feb 2010) | 1 line
Add missing import.
........
2010-02-07 08:01:19 -04:00
|
|
|
def encode(self, dict):
|
2004-07-18 03:16:08 -03:00
|
|
|
list = [self.fmt]
|
|
|
|
for name in self.names:
|
|
|
|
if type(name) == type(()):
|
|
|
|
name, cod, dec = name
|
|
|
|
else:
|
|
|
|
cod = dec = None
|
|
|
|
value = dict[name]
|
|
|
|
if cod:
|
|
|
|
value = cod(value)
|
|
|
|
list.append(value)
|
|
|
|
rv = struct.pack(*list)
|
|
|
|
return rv
|
2003-04-06 06:01:11 -03:00
|
|
|
|
2001-08-19 19:13:00 -03:00
|
|
|
# Helper functions
|
|
|
|
def _tofixed(float):
|
2004-07-18 03:16:08 -03:00
|
|
|
hi = int(float)
|
|
|
|
lo = int(float*0x10000) & 0xffff
|
|
|
|
return (hi<<16)|lo
|
2003-04-06 06:01:11 -03:00
|
|
|
|
2001-08-19 19:13:00 -03:00
|
|
|
def _fromfixed(fixed):
|
2004-07-18 03:16:08 -03:00
|
|
|
hi = (fixed >> 16) & 0xffff
|
|
|
|
lo = (fixed & 0xffff)
|
|
|
|
return hi + (lo / float(0x10000))
|
2003-04-06 06:01:11 -03:00
|
|
|
|
2001-08-19 19:13:00 -03:00
|
|
|
def _tostr31(str):
|
2004-07-18 03:16:08 -03:00
|
|
|
return chr(len(str)) + str + '\0'*(31-len(str))
|
2003-04-06 06:01:11 -03:00
|
|
|
|
2001-08-19 19:13:00 -03:00
|
|
|
def _fromstr31(str31):
|
2004-07-18 03:16:08 -03:00
|
|
|
return str31[1:1+ord(str31[0])]
|
2001-08-19 19:13:00 -03:00
|
|
|
|
|
|
|
SampleDescription = _MediaDescriptionCodec(
|
2003-04-06 06:01:11 -03:00
|
|
|
1, # May be longer, truncate
|
|
|
|
16, # size
|
|
|
|
('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex'), # Attributes
|
|
|
|
"l4slhh" # Format
|
2001-08-19 19:13:00 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
SoundDescription = _MediaDescriptionCodec(
|
2003-04-06 06:01:11 -03:00
|
|
|
1,
|
|
|
|
36,
|
|
|
|
('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex',
|
|
|
|
'version', 'revlevel', 'vendor', 'numChannels', 'sampleSize',
|
|
|
|
'compressionID', 'packetSize', ('sampleRate', _tofixed, _fromfixed)),
|
|
|
|
"l4slhhhh4shhhhl" # Format
|
2001-08-19 19:13:00 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
SoundDescriptionV1 = _MediaDescriptionCodec(
|
2003-04-06 06:01:11 -03:00
|
|
|
1,
|
|
|
|
52,
|
|
|
|
('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex',
|
|
|
|
'version', 'revlevel', 'vendor', 'numChannels', 'sampleSize',
|
|
|
|
'compressionID', 'packetSize', ('sampleRate', _tofixed, _fromfixed), 'samplesPerPacket',
|
|
|
|
'bytesPerPacket', 'bytesPerFrame', 'bytesPerSample'),
|
|
|
|
"l4slhhhh4shhhhlllll" # Format
|
2001-08-19 19:13:00 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
ImageDescription = _MediaDescriptionCodec(
|
2003-04-06 06:01:11 -03:00
|
|
|
1, # May be longer, truncate
|
|
|
|
86, # size
|
|
|
|
('idSize', 'cType', 'resvd1', 'resvd2', 'dataRefIndex', 'version',
|
|
|
|
'revisionLevel', 'vendor', 'temporalQuality', 'spatialQuality',
|
|
|
|
'width', 'height', ('hRes', _tofixed, _fromfixed), ('vRes', _tofixed, _fromfixed),
|
|
|
|
'dataSize', 'frameCount', ('name', _tostr31, _fromstr31),
|
|
|
|
'depth', 'clutID'),
|
|
|
|
'l4slhhhh4sllhhlllh32shh',
|
2001-08-19 19:13:00 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
# XXXX Others, like TextDescription and such, remain to be done.
|