MediaDescr.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » Lib » lib-toolbox » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » Lib » lib toolbox » MediaDescr.py
# 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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.