class __GIF__(ImageMetrics): HEADER = Con.Struct('header', Con.Const(Con.String('gif', 3), 'GIF'), Con.String('version', 3)) SCREEN_DESCRIPTOR = Con.Struct('logical_screen_descriptor', Con.ULInt16('width'), Con.ULInt16('height'), Con.Embed( Con.BitStruct('packed_fields', Con.Flag('global_color_table'), Con.Bits('color_resolution', 3), Con.Flag('sort'), Con.Bits('global_color_table_size', 3))), Con.Byte('background_color_index'), Con.Byte('pixel_aspect_ratio')) def __init__(self, width, height, color_count): ImageMetrics.__init__(self, width, height, 8, color_count, u'image/gif') @classmethod def parse(cls, file): try: header = cls.HEADER.parse_stream(file) descriptor = cls.SCREEN_DESCRIPTOR.parse_stream(file) return __GIF__(descriptor.width, descriptor.height, 2 ** (descriptor.global_color_table_size + 1)) except Con.ConstError: raise InvalidGIF(_(u'Invalid GIF'))
class SpeexAudio(VorbisAudio): """An Ogg Speex audio file.""" SUFFIX = "spx" NAME = SUFFIX DEFAULT_COMPRESSION = "8" COMPRESSION_MODES = tuple([str(i) for i in range(0, 11)]) BINARIES = ("speexenc", "speexdec") REPLAYGAIN_BINARIES = tuple() SPEEX_HEADER = Con.Struct('speex_header', Con.String('speex_string', 8), Con.String('speex_version', 20), Con.ULInt32('speex_version_id'), Con.ULInt32('header_size'), Con.ULInt32('sampling_rate'), Con.ULInt32('mode'), Con.ULInt32('mode_bitstream_version'), Con.ULInt32('channels'), Con.ULInt32('bitrate'), Con.ULInt32('frame_size'), Con.ULInt32('vbr'), Con.ULInt32('frame_per_packet'), Con.ULInt32('extra_headers'), Con.ULInt32('reserved1'), Con.ULInt32('reserved2')) def __init__(self, filename): """filename is a plain string.""" AudioFile.__init__(self, filename) try: self.__read_metadata__() except IOError, msg: raise InvalidSpeex(str(msg))
class Musepack8StreamReader: """An object for parsing Musepack SV8 streams.""" NUT_HEADER = Con.Struct('nut_header', Con.String('key', 2), NutValue('length')) def __init__(self, stream): """Initialized with a file object.""" self.stream = stream def packets(self): """Yields a set of (key, data) tuples.""" import string UPPERCASE = frozenset(string.ascii_uppercase) while (True): try: frame_header = self.NUT_HEADER.parse_stream(self.stream) except Con.core.FieldError: break if (not frozenset(frame_header.key).issubset(UPPERCASE)): break yield (frame_header.key, self.stream.read(frame_header.length - len(self.NUT_HEADER.build(frame_header))))
class VorbisAudio(AudioFile): """An Ogg Vorbis file.""" SUFFIX = "ogg" NAME = SUFFIX DEFAULT_COMPRESSION = "3" COMPRESSION_MODES = tuple([str(i) for i in range(0, 11)]) BINARIES = ("oggenc", "oggdec") REPLAYGAIN_BINARIES = ("vorbisgain", ) OGG_IDENTIFICATION = Con.Struct( "ogg_id", Con.ULInt32("vorbis_version"), Con.Byte("channels"), Con.ULInt32("sample_rate"), Con.ULInt32("bitrate_maximum"), Con.ULInt32("bitrate_nominal"), Con.ULInt32("bitrate_minimum"), Con.Embed( Con.BitStruct("flags", Con.Bits("blocksize_0", 4), Con.Bits("blocksize_1", 4))), Con.Byte("framing")) COMMENT_HEADER = Con.Struct("comment_header", Con.Byte("packet_type"), Con.String("vorbis", 6)) def __init__(self, filename): """filename is a plain string.""" AudioFile.__init__(self, filename) try: self.__read_metadata__() except IOError, msg: raise InvalidVorbis(str(msg))
class AuAudio(AudioFile): """A Sun AU audio file.""" SUFFIX = "au" NAME = SUFFIX AU_HEADER = Con.Struct('header', Con.Const(Con.String('magic_number', 4), '.snd'), Con.UBInt32('data_offset'), Con.UBInt32('data_size'), Con.UBInt32('encoding_format'), Con.UBInt32('sample_rate'), Con.UBInt32('channels')) def __init__(self, filename): AudioFile.__init__(self, filename) try: f = file(filename, 'rb') except IOError, msg: raise InvalidAU(str(msg)) try: header = AuAudio.AU_HEADER.parse_stream(f) if (header.encoding_format not in (2, 3, 4)): raise InvalidFile(_(u"Unsupported Sun AU encoding format")) self.__bits_per_sample__ = { 2: 8, 3: 16, 4: 24 }[header.encoding_format] self.__channels__ = header.channels self.__sample_rate__ = header.sample_rate self.__total_frames__ = header.data_size / \ (self.__bits_per_sample__ / 8) / \ self.__channels__ self.__data_offset__ = header.data_offset self.__data_size__ = header.data_size except Con.ConstError: raise InvalidFile(_(u"Invalid Sun AU header")) except Con.FieldError: raise InvalidAU(_(u"Invalid Sun AU header"))
class __BMP__(ImageMetrics): HEADER = Con.Struct('bmp_header', Con.Const(Con.String('magic_number', 2), 'BM'), Con.ULInt32('file_size'), Con.ULInt16('reserved1'), Con.ULInt16('reserved2'), Con.ULInt32('bitmap_data_offset')) INFORMATION = Con.Struct('bmp_information', Con.ULInt32('header_size'), Con.ULInt32('width'), Con.ULInt32('height'), Con.ULInt16('color_planes'), Con.ULInt16('bits_per_pixel'), Con.ULInt32('compression_method'), Con.ULInt32('image_size'), Con.ULInt32('horizontal_resolution'), Con.ULInt32('vertical_resolution'), Con.ULInt32('colors_used'), Con.ULInt32('important_colors_used')) def __init__(self, width, height, bits_per_pixel, color_count): ImageMetrics.__init__(self, width, height, bits_per_pixel, color_count, u'image/x-ms-bmp') @classmethod def parse(cls, file): try: header = cls.HEADER.parse_stream(file) information = cls.INFORMATION.parse_stream(file) return __BMP__(information.width, information.height, information.bits_per_pixel, information.colors_used) except Con.ConstError: raise InvalidBMP(_(u'Invalid BMP'))
class MusepackAudio(ApeTaggedAudio, AudioFile): """A Musepack audio file.""" SUFFIX = "mpc" NAME = SUFFIX DEFAULT_COMPRESSION = "standard" COMPRESSION_MODES = ("thumb", "radio", "standard", "extreme", "insane") ###Musepack SV7### #BINARIES = ('mppdec','mppenc') ###Musepack SV8### BINARIES = ('mpcdec', 'mpcenc') MUSEPACK8_HEADER = Con.Struct('musepack8_header', Con.UBInt32('crc32'), Con.Byte('bitstream_version'), NutValue('sample_count'), NutValue('beginning_silence'), Con.Embed(Con.BitStruct( 'flags', Con.Bits('sample_frequency', 3), Con.Bits('max_used_bands', 5), Con.Bits('channel_count', 4), Con.Flag('mid_side_used'), Con.Bits('audio_block_frames', 3)))) #not sure about some of the flag locations #Musepack 7's header is very unusual MUSEPACK7_HEADER = Con.Struct('musepack7_header', Con.Const(Con.String('signature', 3), 'MP+'), Con.Byte('version'), Con.ULInt32('frame_count'), Con.ULInt16('max_level'), Con.Embed( Con.BitStruct('flags', Con.Bits('profile', 4), Con.Bits('link', 2), Con.Bits('sample_frequency', 2), Con.Flag('intensity_stereo'), Con.Flag('midside_stereo'), Con.Bits('maxband', 6))), Con.ULInt16('title_gain'), Con.ULInt16('title_peak'), Con.ULInt16('album_gain'), Con.ULInt16('album_peak'), Con.Embed( Con.BitStruct('more_flags', Con.Bits('unused1', 16), Con.Bits('last_frame_length_low', 4), Con.Flag('true_gapless'), Con.Bits('unused2', 3), Con.Flag('fast_seeking'), Con.Bits('last_frame_length_high', 7))), Con.Bytes('unknown', 3), Con.Byte('encoder_version')) def __init__(self, filename): """filename is a plain string.""" AudioFile.__init__(self, filename) f = file(filename, 'rb') try: if (f.read(4) == 'MPCK'): # a Musepack 8 stream for (key, packet) in Musepack8StreamReader(f).packets(): if (key == 'SH'): header = MusepackAudio.MUSEPACK8_HEADER.parse(packet) self.__sample_rate__ = (44100, 48000, 37800, 32000)[ header.sample_frequency] self.__total_frames__ = header.sample_count self.__channels__ = header.channel_count + 1 break elif (key == 'SE'): raise InvalidFile(_(u'No Musepack header found')) else: # a Musepack 7 stream f.seek(0, 0) try: header = MusepackAudio.MUSEPACK7_HEADER.parse_stream(f) except Con.ConstError: raise InvalidFile(_(u'Musepack signature incorrect')) header.last_frame_length = \ (header.last_frame_length_high << 4) | \ header.last_frame_length_low self.__sample_rate__ = (44100, 48000, 37800, 32000)[header.sample_frequency] self.__total_frames__ = (((header.frame_count - 1) * 1152) + header.last_frame_length) self.__channels__ = 2 finally: f.close() @classmethod def from_pcm(cls, filename, pcmreader, compression=None): """Encodes a new file from PCM data. Takes a filename string, PCMReader object and optional compression level string. Encodes a new audio file from pcmreader's data at the given filename with the specified compression level and returns a new MusepackAudio object.""" import tempfile import bisect if (str(compression) not in cls.COMPRESSION_MODES): compression = cls.DEFAULT_COMPRESSION if ((pcmreader.channels > 2) or (pcmreader.sample_rate not in (44100, 48000, 37800, 32000)) or (pcmreader.bits_per_sample != 16)): pcmreader = PCMConverter( pcmreader, sample_rate=[32000, 32000, 37800, 44100, 48000][bisect.bisect( [32000, 37800, 44100, 48000], pcmreader.sample_rate)], channels=min(pcmreader.channels, 2), bits_per_sample=16) f = tempfile.NamedTemporaryFile(suffix=".wav") w = WaveAudio.from_pcm(f.name, pcmreader) try: return cls.__from_wave__(filename, f.name, compression) finally: del(w) f.close() #While Musepack needs to pipe things through WAVE, #not all WAVEs are acceptable. #Use the *_pcm() methods first. def __to_wave__(self, wave_filename): devnull = file(os.devnull, "wb") try: sub = subprocess.Popen([BIN['mpcdec'], self.filename, wave_filename], stdout=devnull, stderr=devnull) #FIXME - small files (~5 seconds) result in an error by mpcdec, #even if they decode correctly. #Not much we can do except try to workaround its bugs. if (sub.wait() not in [0, 250]): raise DecodingError() finally: devnull.close() @classmethod def __from_wave__(cls, filename, wave_filename, compression=None): if (str(compression) not in cls.COMPRESSION_MODES): compression = cls.DEFAULT_COMPRESSION #mppenc requires files to end with .mpc for some reason if (not filename.endswith(".mpc")): import tempfile actual_filename = filename tempfile = tempfile.NamedTemporaryFile(suffix=".mpc") filename = tempfile.name else: actual_filename = tempfile = None ###Musepack SV7### #sub = subprocess.Popen([BIN['mppenc'], # "--silent", # "--overwrite", # "--%s" % (compression), # wave_filename, # filename], # preexec_fn=ignore_sigint) ###Musepack SV8### sub = subprocess.Popen([BIN['mpcenc'], "--silent", "--overwrite", "--%s" % (compression), wave_filename, filename]) if (sub.wait() == 0): if (tempfile is not None): filename = actual_filename f = file(filename, 'wb') tempfile.seek(0, 0) transfer_data(tempfile.read, f.write) f.close() tempfile.close() return MusepackAudio(filename) else: if (tempfile is not None): tempfile.close() raise EncodingError(u"error encoding file with mpcenc") @classmethod def is_type(cls, file): """Returns True if the given file object describes this format. Takes a seekable file pointer rewound to the start of the file.""" header = file.read(4) ###Musepack SV7### #return header == 'MP+\x07' ###Musepack SV8### return (header == 'MP+\x07') or (header == 'MPCK') def sample_rate(self): """Returns the rate of the track's audio as an integer number of Hz.""" return self.__sample_rate__ def total_frames(self): """Returns the total PCM frames of the track as an integer.""" return self.__total_frames__ def channels(self): """Returns an integer number of channels this track contains.""" return self.__channels__ def bits_per_sample(self): """Returns an integer number of bits-per-sample this track contains.""" return 16 def lossless(self): """Returns False.""" return False
class ID3v1Comment(MetaData, list): """A complete ID3v1 tag.""" ID3v1 = Con.Struct("id3v1", Con.Const(Con.String("identifier", 3), 'TAG'), Con.String("song_title", 30), Con.String("artist", 30), Con.String("album", 30), Con.String("year", 4), Con.String("comment", 28), Con.Padding(1), Con.Byte("track_number"), Con.Byte("genre")) ID3v1_NO_TRACKNUMBER = Con.Struct( "id3v1_notracknumber", Con.Const(Con.String("identifier", 3), 'TAG'), Con.String("song_title", 30), Con.String("artist", 30), Con.String("album", 30), Con.String("year", 4), Con.String("comment", 30), Con.Byte("genre")) ATTRIBUTES = [ 'track_name', 'artist_name', 'album_name', 'year', 'comment', 'track_number' ] @classmethod def read_id3v1_comment(cls, mp3filename): """Reads a ID3v1Comment data from an MP3 filename. Returns a (song title, artist, album, year, comment, track number) tuple. If no ID3v1 tag is present, returns a tuple with those fields blank. All text is in unicode. If track number is -1, the id3v1 comment could not be found. """ mp3file = file(mp3filename, "rb") try: mp3file.seek(-128, 2) try: id3v1 = ID3v1Comment.ID3v1.parse(mp3file.read()) except Con.adapters.PaddingError: mp3file.seek(-128, 2) id3v1 = ID3v1Comment.ID3v1_NO_TRACKNUMBER.parse(mp3file.read()) id3v1.track_number = 0 except Con.ConstError: return tuple([u""] * 5 + [-1]) field_list = (id3v1.song_title, id3v1.artist, id3v1.album, id3v1.year, id3v1.comment) return tuple( map(lambda t: t.rstrip('\x00').decode('ascii', 'replace'), field_list) + [id3v1.track_number]) finally: mp3file.close() @classmethod def build_id3v1(cls, song_title, artist, album, year, comment, track_number): """Turns fields into a complete ID3v1 binary tag string. All fields are unicode except for track_number, an int.""" def __s_pad__(s, length): if (len(s) < length): return s + chr(0) * (length - len(s)) else: s = s[0:length].rstrip() return s + chr(0) * (length - len(s)) c = Con.Container() c.identifier = 'TAG' c.song_title = __s_pad__(song_title.encode('ascii', 'replace'), 30) c.artist = __s_pad__(artist.encode('ascii', 'replace'), 30) c.album = __s_pad__(album.encode('ascii', 'replace'), 30) c.year = __s_pad__(year.encode('ascii', 'replace'), 4) c.comment = __s_pad__(comment.encode('ascii', 'replace'), 28) c.track_number = int(track_number) c.genre = 0 return ID3v1Comment.ID3v1.build(c) def __init__(self, metadata): """Initialized with a read_id3v1_comment tuple. Fields are (title,artist,album,year,comment,tracknum)""" list.__init__(self, metadata) def supports_images(self): """Returns False.""" return False #if an attribute is updated (e.g. self.track_name) #make sure to update the corresponding list item def __setattr__(self, key, value): if (key in self.ATTRIBUTES): if (key != 'track_number'): self[self.ATTRIBUTES.index(key)] = value else: self[self.ATTRIBUTES.index(key)] = int(value) elif (key in MetaData.__FIELDS__): pass else: self.__dict__[key] = value def __delattr__(self, key): if (key == 'track_number'): setattr(self, key, 0) elif (key in self.ATTRIBUTES): setattr(self, key, u"") def __getattr__(self, key): if (key in self.ATTRIBUTES): return self[self.ATTRIBUTES.index(key)] elif (key in MetaData.__INTEGER_FIELDS__): return 0 elif (key in MetaData.__FIELDS__): return u"" else: raise AttributeError(key) @classmethod def converted(cls, metadata): """Converts a MetaData object to an ID3v1Comment object.""" if ((metadata is None) or (isinstance(metadata, ID3v1Comment))): return metadata return ID3v1Comment( (metadata.track_name, metadata.artist_name, metadata.album_name, metadata.year, metadata.comment, int(metadata.track_number))) def __comment_name__(self): return u'ID3v1' def __comment_pairs__(self): return zip(('Title', 'Artist', 'Album', 'Year', 'Comment', 'Tracknum'), self) def build_tag(self): """Returns a binary string of this tag's data.""" return self.build_id3v1(self.track_name, self.artist_name, self.album_name, self.year, self.comment, self.track_number) def images(self): """Returns an empty list of Image objects.""" return []
def Atom(name): """A basic QuickTime atom struct.""" return AtomAdapter( Con.Struct(name, Con.UBInt32("size"), Con.String("type", 4), Con.String("data", lambda ctx: ctx["size"] - 8)))
def __init__(self, atom_name, sub_atom): Con.Struct.__init__(self, atom_name) self.atom_name = atom_name self.sub_atom = sub_atom self.header = Con.Struct(atom_name, Con.UBInt32("size"), Con.Const(Con.String("type", 4), atom_name))
def _parse(self, stream, context): header = self.header.parse_stream(stream) return self.sub_atom.parse_stream(stream) def _build(self, obj, stream, context): data = self.sub_atom.build(obj) stream.write( self.header.build( Con.Container(type=self.atom_name, size=len(data) + 8))) stream.write(data) def _sizeof(self, context): return self.sub_atom.sizeof(context) + 8 ATOM_FTYP = Con.Struct("ftyp", Con.String("major_brand", 4), Con.UBInt32("major_brand_version"), Con.GreedyRepeater(Con.String("compatible_brands", 4))) ATOM_MVHD = Con.Struct( "mvhd", Con.Byte("version"), Con.String("flags", 3), VersionLength("created_mac_UTC_date"), VersionLength("modified_mac_UTC_date"), Con.UBInt32("time_scale"), VersionLength("duration"), Con.UBInt32("playback_speed"), Con.UBInt16("user_volume"), Con.Padding(10), Con.Struct("windows", Con.UBInt32("geometry_matrix_a"), Con.UBInt32("geometry_matrix_b"), Con.UBInt32("geometry_matrix_u"), Con.UBInt32("geometry_matrix_c"), Con.UBInt32("geometry_matrix_d"), Con.UBInt32("geometry_matrix_v"),
class __TIFF__(ImageMetrics): HEADER = Con.Struct('header', Con.String('byte_order', 2), Con.Switch('order', lambda ctx: ctx['byte_order'], {"II": Con.Embed( Con.Struct('little_endian', Con.Const(Con.ULInt16('version'), 42), Con.ULInt32('offset'))), "MM": Con.Embed( Con.Struct('big_endian', Con.Const(Con.UBInt16('version'), 42), Con.UBInt32('offset')))})) L_IFD = Con.Struct('ifd', Con.PrefixedArray( length_field=Con.ULInt16('length'), subcon=Con.Struct('tags', Con.ULInt16('id'), Con.ULInt16('type'), Con.ULInt32('count'), Con.ULInt32('offset'))), Con.ULInt32('next')) B_IFD = Con.Struct('ifd', Con.PrefixedArray( length_field=Con.UBInt16('length'), subcon=Con.Struct('tags', Con.UBInt16('id'), Con.UBInt16('type'), Con.UBInt32('count'), Con.UBInt32('offset'))), Con.UBInt32('next')) def __init__(self, width, height, bits_per_pixel, color_count): ImageMetrics.__init__(self, width, height, bits_per_pixel, color_count, u'image/tiff') @classmethod def b_tag_value(cls, file, tag): subtype = {1: Con.Byte("data"), 2: Con.CString("data"), 3: Con.UBInt16("data"), 4: Con.UBInt32("data"), 5: Con.Struct("data", Con.UBInt32("high"), Con.UBInt32("low"))}[tag.type] data = Con.StrictRepeater(tag.count, subtype) if ((tag.type != 2) and (data.sizeof() <= 4)): return tag.offset else: file.seek(tag.offset, 0) return data.parse_stream(file) @classmethod def l_tag_value(cls, file, tag): subtype = {1: Con.Byte("data"), 2: Con.CString("data"), 3: Con.ULInt16("data"), 4: Con.ULInt32("data"), 5: Con.Struct("data", Con.ULInt32("high"), Con.ULInt32("low"))}[tag.type] data = Con.StrictRepeater(tag.count, subtype) if ((tag.type != 2) and (data.sizeof() <= 4)): return tag.offset else: file.seek(tag.offset, 0) return data.parse_stream(file) @classmethod def parse(cls, file): width = 0 height = 0 bits_per_sample = 0 color_count = 0 try: header = cls.HEADER.parse_stream(file) if (header.byte_order == 'II'): IFD = cls.L_IFD tag_value = cls.l_tag_value elif (header.byte_order == 'MM'): IFD = cls.B_IFD tag_value = cls.b_tag_value else: raise InvalidTIFF(_(u'Invalid byte order')) file.seek(header.offset, 0) ifd = IFD.parse_stream(file) while (True): for tag in ifd.tags: if (tag.id == 0x0100): width = tag_value(file, tag) elif (tag.id == 0x0101): height = tag_value(file, tag) elif (tag.id == 0x0102): try: bits_per_sample = sum(tag_value(file, tag)) except TypeError: bits_per_sample = tag_value(file, tag) elif (tag.id == 0x0140): color_count = tag.count / 3 else: pass if (ifd.next == 0x00): break else: file.seek(ifd.next, 0) ifd = IFD.parse_stream(file) return __TIFF__(width, height, bits_per_sample, color_count) except Con.ConstError: raise InvalidTIFF(_(u'Invalid TIFF'))
class __PNG__(ImageMetrics): HEADER = Con.Const(Con.String('header', 8), '89504e470d0a1a0a'.decode('hex')) CHUNK_HEADER = Con.Struct('chunk', Con.UBInt32('length'), Con.String('type', 4)) CHUNK_FOOTER = Con.Struct('crc32', Con.UBInt32('crc')) IHDR = Con.Struct('IHDR', Con.UBInt32('width'), Con.UBInt32('height'), Con.Byte('bit_depth'), Con.Byte('color_type'), Con.Byte('compression_method'), Con.Byte('filter_method'), Con.Byte('interlace_method')) def __init__(self, width, height, bits_per_pixel, color_count): ImageMetrics.__init__(self, width, height, bits_per_pixel, color_count, u'image/png') @classmethod def parse(cls, file): ihdr = None plte = None try: header = cls.HEADER.parse_stream(file) chunk_header = cls.CHUNK_HEADER.parse_stream(file) data = file.read(chunk_header.length) chunk_footer = cls.CHUNK_FOOTER.parse_stream(file) while (chunk_header.type != 'IEND'): if (chunk_header.type == 'IHDR'): ihdr = cls.IHDR.parse(data) elif (chunk_header.type == 'PLTE'): plte = data chunk_header = cls.CHUNK_HEADER.parse_stream(file) data = file.read(chunk_header.length) chunk_footer = cls.CHUNK_FOOTER.parse_stream(file) if (ihdr.color_type == 0): # grayscale bits_per_pixel = ihdr.bit_depth color_count = 0 elif (ihdr.color_type == 2): # RGB bits_per_pixel = ihdr.bit_depth * 3 color_count = 0 elif (ihdr.color_type == 3): # palette bits_per_pixel = 8 if ((len(plte) % 3) != 0): raise InvalidPNG(_(u'Invalid PLTE chunk length')) else: color_count = len(plte) / 3 elif (ihdr.color_type == 4): # grayscale + alpha bits_per_pixel = ihdr.bit_depth * 2 color_count = 0 elif (ihdr.color_type == 6): # RGB + alpha bits_per_pixel = ihdr.bit_depth * 4 color_count = 0 return __PNG__(ihdr.width, ihdr.height, bits_per_pixel, color_count) except Con.ConstError: raise InvalidPNG(_(u'Invalid PNG'))
class __JPEG__(ImageMetrics): SEGMENT_HEADER = Con.Struct('segment_header', Con.Const(Con.Byte('header'), 0xFF), Con.Byte('type'), Con.If( lambda ctx: ctx['type'] not in (0xD8, 0xD9), Con.UBInt16('length'))) APP0 = Con.Struct('JFIF_segment_marker', Con.String('identifier', 5), Con.Byte('major_version'), Con.Byte('minor_version'), Con.Byte('density_units'), Con.UBInt16('x_density'), Con.UBInt16('y_density'), Con.Byte('thumbnail_width'), Con.Byte('thumbnail_height')) SOF = Con.Struct('start_of_frame', Con.Byte('data_precision'), Con.UBInt16('image_height'), Con.UBInt16('image_width'), Con.Byte('components')) def __init__(self, width, height, bits_per_pixel): ImageMetrics.__init__(self, width, height, bits_per_pixel, 0, u'image/jpeg') @classmethod def parse(cls, file): try: header = cls.SEGMENT_HEADER.parse_stream(file) if (header.type != 0xD8): raise InvalidJPEG(_(u'Invalid JPEG header')) segment = cls.SEGMENT_HEADER.parse_stream(file) while (segment.type != 0xD9): if (segment.type == 0xDA): break if (segment.type in (0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0XC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF)): # start of frame segment_data = cStringIO.StringIO( file.read(segment.length - 2)) frame0 = cls.SOF.parse_stream(segment_data) segment_data.close() return __JPEG__(width=frame0.image_width, height=frame0.image_height, bits_per_pixel=(frame0.data_precision * frame0.components)) else: file.seek(segment.length - 2, 1) segment = cls.SEGMENT_HEADER.parse_stream(file) raise InvalidJPEG(_(u'Start of frame not found')) except Con.ConstError: raise InvalidJPEG(_(u"Invalid JPEG segment marker at 0x%X") % \ (file.tell()))
class OggStreamReader: """A class for walking through an Ogg stream.""" OGGS = Con.Struct( "oggs", Con.Const(Con.String("magic_number", 4), "OggS"), Con.Byte("version"), Con.Byte("header_type"), Con.SLInt64("granule_position"), Con.ULInt32("bitstream_serial_number"), Con.ULInt32("page_sequence_number"), Con.ULInt32("checksum"), Con.Byte("segments"), Con.MetaRepeater(lambda ctx: ctx["segments"], Con.Byte("segment_lengths"))) def __init__(self, stream): """stream is a file-like object with read() and close() methods.""" self.stream = stream def close(self): """Closes the sub-stream.""" self.stream.close() def packets(self, from_beginning=True): """Yields one fully reassembled Ogg packet per pass. Packets are returned as binary strings.""" if (from_beginning): self.stream.seek(0, 0) segment = cStringIO.StringIO() while (True): try: page = OggStreamReader.OGGS.parse_stream(self.stream) for length in page.segment_lengths: if (length == 255): segment.write(self.stream.read(length)) else: segment.write(self.stream.read(length)) yield segment.getvalue() segment = cStringIO.StringIO() except Con.core.FieldError: break except Con.ConstError: break def pages(self, from_beginning=True): """Yields a (Container,string) tuple per pass. Container is parsed from OggStreamReader.OGGS. string is a binary string of combined segments (which may not be a complete packet).""" if (from_beginning): self.stream.seek(0, 0) while (True): try: page = OggStreamReader.OGGS.parse_stream(self.stream) yield (page, self.stream.read(sum(page.segment_lengths))) except Con.core.FieldError: break except Con.ConstError: break @classmethod def pages_to_packet(cls, pages_iter): """Returns a complete packet as a list of (Container,string) tuples. pages_iter should be an iterator of (Container,string) tuples as returned from the pages() method. """ packet = [pages_iter.next()] while (packet[-1][0].segment_lengths[-1] == 255): packet.append(pages_iter.next()) return packet CRC_LOOKUP = ( 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072, 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53, 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3, 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4) @classmethod def calculate_ogg_checksum(cls, page_header, page_data): """Calculates an Ogg checksum integer. page_header is a Container object parsed through OGGS. page_data is a string of data contained by the page. """ old_checksum = page_header.checksum try: page_header.checksum = 0 sum = 0 for c in cls.OGGS.build(page_header) + page_data: sum = ((sum << 8) ^ \ cls.CRC_LOOKUP[((sum >> 24) & 0xFF) ^ ord(c)]) \ & 0xFFFFFFFF return sum finally: page_header.checksum = old_checksum
class WavPackAudio(ApeTaggedAudio, AudioFile): """A WavPack audio file.""" SUFFIX = "wv" NAME = SUFFIX DEFAULT_COMPRESSION = "standard" COMPRESSION_MODES = ("veryfast", "fast", "standard", "high", "veryhigh") APE_TAG_CLASS = WavPackAPEv2 HEADER = Con.Struct( "wavpackheader", Con.Const(Con.String("id", 4), 'wvpk'), Con.ULInt32("block_size"), Con.ULInt16("version"), Con.ULInt8("track_number"), Con.ULInt8("index_number"), Con.ULInt32("total_samples"), Con.ULInt32("block_index"), Con.ULInt32("block_samples"), Con.Embed( Con.BitStruct("flags", Con.Flag("floating_point_data"), Con.Flag("hybrid_noise_shaping"), Con.Flag("cross_channel_decorrelation"), Con.Flag("joint_stereo"), Con.Flag("hybrid_mode"), Con.Flag("mono_output"), Con.Bits("bits_per_sample", 2), Con.Bits("left_shift_data_low", 3), Con.Flag("final_block_in_sequence"), Con.Flag("initial_block_in_sequence"), Con.Flag("hybrid_noise_balanced"), Con.Flag("hybrid_mode_control_bitrate"), Con.Flag("extended_size_integers"), Con.Bit("sampling_rate_low"), Con.Bits("maximum_magnitude", 5), Con.Bits("left_shift_data_high", 2), Con.Flag("reserved2"), Con.Flag("false_stereo"), Con.Flag("use_IIR"), Con.Bits("reserved1", 2), Con.Bits("sampling_rate_high", 3))), Con.ULInt32("crc")) SUB_HEADER = Con.Struct( "wavpacksubheader", Con.Embed( Con.BitStruct("flags", Con.Flag("large_block"), Con.Flag("actual_size_1_less"), Con.Flag("nondecoder_data"), Con.Bits("metadata_function", 5))), Con.IfThenElse('size', lambda ctx: ctx['large_block'], ULInt24('s'), Con.Byte('s'))) BITS_PER_SAMPLE = (8, 16, 24, 32) SAMPLING_RATE = (6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 192000, 0) __options__ = { "veryfast": { "block_size": 44100, "joint_stereo": True, "false_stereo": True, "wasted_bits": True, "decorrelation_passes": 1 }, "fast": { "block_size": 44100, "joint_stereo": True, "false_stereo": True, "wasted_bit": True, "decorrelation_passes": 2 }, "standard": { "block_size": 44100, "joint_stereo": True, "false_stereo": True, "wasted_bits": True, "decorrelation_passes": 5 }, "high": { "block_size": 44100, "joint_stereo": True, "false_stereo": True, "wasted_bits": True, "decorrelation_passes": 10 }, "veryhigh": { "block_size": 44100, "joint_stereo": True, "false_stereo": True, "wasted_bits": True, "decorrelation_passes": 16 } } def __init__(self, filename): """filename is a plain string.""" self.filename = filename self.__samplerate__ = 0 self.__channels__ = 0 self.__bitspersample__ = 0 self.__total_frames__ = 0 try: self.__read_info__() except IOError, msg: raise InvalidWavPack(str(msg))