diff options
author | Jack Jansen <jack.jansen@cwi.nl> | 2002-12-30 22:04:23 (GMT) |
---|---|---|
committer | Jack Jansen <jack.jansen@cwi.nl> | 2002-12-30 22:04:23 (GMT) |
commit | 60087fb45092d9c199cea162e58d9193c7c1558c (patch) | |
tree | 05f3343e7707c4a4179e409506b39601279f04c1 /Lib/plat-mac/Carbon/MediaDescr.py | |
parent | c262a1f51ce89dbea4aeb072cf631686c47ed97f (diff) | |
download | cpython-60087fb45092d9c199cea162e58d9193c7c1558c.zip cpython-60087fb45092d9c199cea162e58d9193c7c1558c.tar.gz cpython-60087fb45092d9c199cea162e58d9193c7c1558c.tar.bz2 |
Moved most of Mac/Lib hierarchy to Lib/plat-mac: it can be used both
in MacPython-OS9 and MacPython-OSX (or the equivalent unix Python on
Mac OS X). The only items remaining in Mac/Lib are modules that are
meaningful only for MacPython-OS9 (CFM stuff, MacPython preferences
in resources, etc).
Diffstat (limited to 'Lib/plat-mac/Carbon/MediaDescr.py')
-rw-r--r-- | Lib/plat-mac/Carbon/MediaDescr.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/Lib/plat-mac/Carbon/MediaDescr.py b/Lib/plat-mac/Carbon/MediaDescr.py new file mode 100644 index 0000000..3c73820 --- /dev/null +++ b/Lib/plat-mac/Carbon/MediaDescr.py @@ -0,0 +1,97 @@ +# Parsers/generators for QuickTime media descriptions +import struct + +Error = 'MediaDescr.Error' + +class _MediaDescriptionCodec: + def __init__(self, trunc, size, names, fmt): + self.trunc = trunc + self.size = size + self.names = names + self.fmt = fmt + + def decode(self, data): + if self.trunc: + data = data[:self.size] + values = struct.unpack(self.fmt, data) + if len(values) != len(self.names): + raise Error, ('Format length does not match number of names', descr) + 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 + + def encode(dict): + 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 = apply(struct.pack, tuple(list)) + return rv + +# Helper functions +def _tofixed(float): + hi = int(float) + lo = int(float*0x10000) & 0xffff + return (hi<<16)|lo + +def _fromfixed(fixed): + hi = (fixed >> 16) & 0xffff + lo = (fixed & 0xffff) + return hi + (lo / float(0x10000)) + +def _tostr31(str): + return chr(len(str)) + str + '\0'*(31-len(str)) + +def _fromstr31(str31): + return str31[1:1+ord(str31[0])] + +SampleDescription = _MediaDescriptionCodec( + 1, # May be longer, truncate + 16, # size + ('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex'), # Attributes + "l4slhh" # Format +) + +SoundDescription = _MediaDescriptionCodec( + 1, + 36, + ('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex', + 'version', 'revlevel', 'vendor', 'numChannels', 'sampleSize', + 'compressionID', 'packetSize', ('sampleRate', _tofixed, _fromfixed)), + "l4slhhhh4shhhhl" # Format +) + +SoundDescriptionV1 = _MediaDescriptionCodec( + 1, + 52, + ('descSize', 'dataFormat', 'resvd1', 'resvd2', 'dataRefIndex', + 'version', 'revlevel', 'vendor', 'numChannels', 'sampleSize', + 'compressionID', 'packetSize', ('sampleRate', _tofixed, _fromfixed), 'samplesPerPacket', + 'bytesPerPacket', 'bytesPerFrame', 'bytesPerSample'), + "l4slhhhh4shhhhlllll" # Format +) + +ImageDescription = _MediaDescriptionCodec( + 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', +) + +# XXXX Others, like TextDescription and such, remain to be done. |