1 # Parsers/generators for QuickTime media descriptions
4 Error
= 'MediaDescr.Error'
6 class _MediaDescriptionCodec
:
7 def __init__(self
, trunc
, size
, names
, fmt
):
13 def decode(self
, data
):
15 data
= data
[:self
.size
]
16 values
= struct
.unpack(self
.fmt
, data
)
17 if len(values
) != len(self
.names
):
18 raise Error
, ('Format length does not match number of names', descr
)
20 for i
in range(len(values
)):
23 if type(name
) == type(()):
31 for name
in self
.names
:
32 if type(name
) == type(()):
40 rv
= struct
.pack(*list)
46 lo
= int(float*0x10000) & 0xffff
49 def _fromfixed(fixed
):
50 hi
= (fixed
>> 16) & 0xffff
52 return hi
+ (lo
/ float(0x10000))
55 return chr(len(str)) + str + '\0'*(31-len(str))
57 def _fromstr31(str31
):
58 return str31
[1:1+ord(str31
[0])]
60 SampleDescription
= _MediaDescriptionCodec(
61 1, # May be longer, truncate
63 ('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex'), # Attributes
67 SoundDescription
= _MediaDescriptionCodec(
70 ('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex',
71 'version', 'revlevel', 'vendor', 'numChannels', 'sampleSize',
72 'compressionID', 'packetSize', ('sampleRate', _tofixed
, _fromfixed
)),
73 "l4slhhhh4shhhhl" # Format
76 SoundDescriptionV1
= _MediaDescriptionCodec(
79 ('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex',
80 'version', 'revlevel', 'vendor', 'numChannels', 'sampleSize',
81 'compressionID', 'packetSize', ('sampleRate', _tofixed
, _fromfixed
), 'samplesPerPacket',
82 'bytesPerPacket', 'bytesPerFrame', 'bytesPerSample'),
83 "l4slhhhh4shhhhlllll" # Format
86 ImageDescription
= _MediaDescriptionCodec(
87 1, # May be longer, truncate
89 ('idSize', 'cType', 'resvd1', 'resvd2', 'dataRefIndex', 'version',
90 'revisionLevel', 'vendor', 'temporalQuality', 'spatialQuality',
91 'width', 'height', ('hRes', _tofixed
, _fromfixed
), ('vRes', _tofixed
, _fromfixed
),
92 'dataSize', 'frameCount', ('name', _tostr31
, _fromstr31
),
94 'l4slhhhh4sllhhlllh32shh',
97 # XXXX Others, like TextDescription and such, remain to be done.