Exemplo n.º 1
0
class __GIF__(ImageMetrics):
    HEADER = construct.Struct('header',
                        construct.Const(construct.String('gif',3),'GIF'),
                        construct.String('version',3))

    SCREEN_DESCRIPTOR = construct.Struct('logical_screen_descriptor',
                                   construct.ULInt16('width'),
                                   construct.ULInt16('height'),
                                   construct.Embed(
        construct.BitStruct('packed_fields',
                      construct.Flag('global_color_table'),
                      construct.Bits('color_resolution',3),
                      construct.Flag('sort'),
                      construct.Bits('global_color_table_size',3))),
                                   construct.Byte('background_color_index'),
                                   construct.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 construct.ConstError:
            raise InvalidGIF(_(u'Invalid GIF'))
Exemplo n.º 2
0
 def __init__(self, atom_name, sub_atom):
     construct.Struct.__init__(self, atom_name)
     self.atom_name = atom_name
     self.sub_atom = sub_atom
     self.header = construct.Struct(
         atom_name, construct.UBInt32("size"),
         construct.Const(construct.String("type", 4), atom_name))
Exemplo n.º 3
0
class __BMP__(ImageMetrics):
    HEADER = construct.Struct('bmp_header',
                        construct.Const(construct.String('magic_number',2),'BM'),
                        construct.ULInt32('file_size'),
                        construct.ULInt16('reserved1'),
                        construct.ULInt16('reserved2'),
                        construct.ULInt32('bitmap_data_offset'))

    INFORMATION = construct.Struct('bmp_information',
                             construct.ULInt32('header_size'),
                             construct.ULInt32('width'),
                             construct.ULInt32('height'),
                             construct.ULInt16('color_planes'),
                             construct.ULInt16('bits_per_pixel'),
                             construct.ULInt32('compression_method'),
                             construct.ULInt32('image_size'),
                             construct.ULInt32('horizontal_resolution'),
                             construct.ULInt32('vertical_resolution'),
                             construct.ULInt32('colors_used'),
                             construct.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 construct.ConstError:
            raise InvalidBMP(_(u'Invalid BMP'))
Exemplo n.º 4
0
class AuAudio(AudioFile):
    SUFFIX = "au"
    NAME = SUFFIX

    AU_HEADER = construct.Struct(
        'header', construct.Const(construct.String('magic_number', 4), '.snd'),
        construct.UBInt32('data_offset'), construct.UBInt32('data_size'),
        construct.UBInt32('encoding_format'), construct.UBInt32('sample_rate'),
        construct.UBInt32('channels'))

    def __init__(self, filename):
        AudioFile.__init__(self, filename)

        f = file(filename, 'rb')
        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 construct.ConstError:
            raise InvalidFile(str(msg))

    @classmethod
    def is_type(cls, file):
        return file.read(4) == ".snd"

    def lossless(self):
        return True

    def bits_per_sample(self):
        return self.__bits_per_sample__

    def channels(self):
        return self.__channels__

    def channel_mask(self):
        if (self.channels() <= 2):
            return ChannelMask.from_channels(self.channels())
        else:
            return ChannelMask(0)

    def sample_rate(self):
        return self.__sample_rate__

    def total_frames(self):
        return self.__total_frames__

    def to_pcm(self):
        f = file(self.filename, 'rb')
        f.seek(self.__data_offset__, 0)

        return PCMReader(f,
                         sample_rate=self.sample_rate(),
                         channels=self.channels(),
                         channel_mask=int(self.channel_mask()),
                         bits_per_sample=self.bits_per_sample(),
                         signed=True,
                         big_endian=True)

    @classmethod
    def from_pcm(cls, filename, pcmreader, compression=None):
        if (pcmreader.bits_per_sample not in (8, 16, 24)):
            raise InvalidFormat(
                _(u"Unsupported bits per sample %s") %
                (pcmreader.bits_per_sample))

        bytes_per_sample = pcmreader.bits_per_sample / 8

        header = construct.Container(magic_number='.snd',
                                     data_offset=0,
                                     data_size=0,
                                     encoding_format={
                                         8: 2,
                                         16: 3,
                                         24: 4
                                     }[pcmreader.bits_per_sample],
                                     sample_rate=pcmreader.sample_rate,
                                     channels=pcmreader.channels)

        try:
            f = file(filename, 'wb')
        except IOError:
            raise EncodingError(None)
        try:
            #send out a dummy header
            f.write(AuAudio.AU_HEADER.build(header))
            header.data_offset = f.tell()

            #send our big-endian PCM data
            #d will be a list of ints, so we can't use transfer_data
            framelist = pcmreader.read(BUFFER_SIZE)
            while (len(framelist) > 0):
                bytes = framelist.to_bytes(True, True)
                f.write(bytes)
                header.data_size += len(bytes)
                framelist = pcmreader.read(BUFFER_SIZE)

            #send out a complete header
            f.seek(0, 0)
            f.write(AuAudio.AU_HEADER.build(header))
        finally:
            f.close()

        try:
            pcmreader.close()
        except DecodingError:
            raise EncodingError()

        return AuAudio(filename)

    @classmethod
    def track_name(cls, file_path, track_metadata=None, format=None):
        if (format is None):
            format = "track%(track_number)2.2d.au"
        return AudioFile.track_name(file_path,
                                    track_metadata,
                                    format,
                                    suffix=cls.SUFFIX)
Exemplo n.º 5
0
def Atom(name):
    return AtomAdapter(
        construct.Struct(name, construct.UBInt32("size"),
                         construct.String("type", 4),
                         construct.String("data",
                                          lambda ctx: ctx["size"] - 8)))
Exemplo n.º 6
0
        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(
                construct.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 = construct.Struct(
    "ftyp", construct.String("major_brand", 4),
    construct.UBInt32("major_brand_version"),
    construct.GreedyRepeater(construct.String("compatible_brands", 4)))

ATOM_MVHD = construct.Struct(
    "mvhd", construct.Byte("version"), construct.String("flags", 3),
    VersionLength("created_mac_UTC_date"),
    VersionLength("modified_mac_UTC_date"), construct.UBInt32("time_scale"),
    VersionLength("duration"), construct.UBInt32("playback_speed"),
    construct.UBInt16("user_volume"), construct.Padding(10),
    construct.Struct("windows", construct.UBInt32("geometry_matrix_a"),
                     construct.UBInt32("geometry_matrix_b"),
                     construct.UBInt32("geometry_matrix_u"),
                     construct.UBInt32("geometry_matrix_c"),
                     construct.UBInt32("geometry_matrix_d"),
                     construct.UBInt32("geometry_matrix_v"),
Exemplo n.º 7
0
class ApeAudio(ApeTaggedAudio, AudioFile):
    SUFFIX = "ape"
    NAME = SUFFIX
    DEFAULT_COMPRESSION = "5000"
    COMPRESSION_MODES = tuple([str(x * 1000) for x in range(1, 6)])
    del (x)
    BINARIES = ("mac", )

    FILE_HEAD = construct.Struct("ape_head", construct.String('id', 4),
                                 construct.ULInt16('version'))

    #version >= 3.98
    APE_DESCRIPTOR = construct.Struct(
        "ape_descriptor", construct.ULInt16('padding'),
        construct.ULInt32('descriptor_bytes'),
        construct.ULInt32('header_bytes'),
        construct.ULInt32('seektable_bytes'),
        construct.ULInt32('header_data_bytes'),
        construct.ULInt32('frame_data_bytes'),
        construct.ULInt32('frame_data_bytes_high'),
        construct.ULInt32('terminating_data_bytes'),
        construct.String('md5', 16))

    APE_HEADER = construct.Struct("ape_header",
                                  construct.ULInt16('compression_level'),
                                  construct.ULInt16('format_flags'),
                                  construct.ULInt32('blocks_per_frame'),
                                  construct.ULInt32('final_frame_blocks'),
                                  construct.ULInt32('total_frames'),
                                  construct.ULInt16('bits_per_sample'),
                                  construct.ULInt16('number_of_channels'),
                                  construct.ULInt32('sample_rate'))

    #version <= 3.97
    APE_HEADER_OLD = construct.Struct("ape_header_old",
                                      construct.ULInt16('compression_level'),
                                      construct.ULInt16('format_flags'),
                                      construct.ULInt16('number_of_channels'),
                                      construct.ULInt32('sample_rate'),
                                      construct.ULInt32('header_bytes'),
                                      construct.ULInt32('terminating_bytes'),
                                      construct.ULInt32('total_frames'),
                                      construct.ULInt32('final_frame_blocks'))

    def __init__(self, filename):
        AudioFile.__init__(self, filename)

        (self.__samplespersec__, self.__channels__, self.__bitspersample__,
         self.__totalsamples__) = ApeAudio.__ape_info__(filename)

    @classmethod
    def is_type(cls, file):
        return file.read(4) == "MAC "

    def lossless(self):
        return True

    @classmethod
    def supports_foreign_riff_chunks(cls):
        return True

    def has_foreign_riff_chunks(self):
        #FIXME - this isn't strictly true
        #I'll need a way to detect foreign chunks in APE's stream
        #without decoding it first,
        #but since I'm not supporting APE anyway, I'll take the lazy way out
        return True

    def bits_per_sample(self):
        return self.__bitspersample__

    def channels(self):
        return self.__channels__

    def total_frames(self):
        return self.__totalsamples__

    def sample_rate(self):
        return self.__samplespersec__

    @classmethod
    def __ape_info__(cls, filename):
        f = file(filename, 'rb')
        try:
            file_head = cls.FILE_HEAD.parse_stream(f)

            if (file_head.id != 'MAC '):
                raise InvalidFile(_(u"Invalid Monkey's Audio header"))

            if (file_head.version >= 3980):  #the latest APE file type
                descriptor = cls.APE_DESCRIPTOR.parse_stream(f)
                header = cls.APE_HEADER.parse_stream(f)

                return (header.sample_rate,
                        header.number_of_channels,
                        header.bits_per_sample,
                        ((header.total_frames - 1) * \
                         header.blocks_per_frame) + \
                         header.final_frame_blocks)
            else:  #old-style APE file (obsolete)
                header = cls.APE_HEADER_OLD.parse_stream(f)

                if (file_head.version >= 3950):
                    blocks_per_frame = 0x48000
                elif ((file_head.version >= 3900)
                      or ((file_head.version >= 3800) and
                          (header.compression_level == 4000))):
                    blocks_per_frame = 0x12000
                else:
                    blocks_per_frame = 0x2400

                if (header.format_flags & 0x01):
                    bits_per_sample = 8
                elif (header.format_flags & 0x08):
                    bits_per_sample = 24
                else:
                    bits_per_sample = 16

                return (header.sample_rate,
                        header.number_of_channels,
                        bits_per_sample,
                        ((header.total_frames - 1) * \
                         blocks_per_frame) + \
                         header.final_frame_blocks)

        finally:
            f.close()

    def to_wave(self, wave_filename):
        if (self.filename.endswith(".ape")):
            devnull = file(os.devnull, "wb")
            sub = subprocess.Popen(
                [BIN['mac'], self.filename, wave_filename, '-d'],
                stdout=devnull,
                stderr=devnull)
            sub.wait()
            devnull.close()
        else:
            devnull = file(os.devnull, 'ab')
            import tempfile
            ape = tempfile.NamedTemporaryFile(suffix='.ape')
            f = file(self.filename, 'rb')
            transfer_data(f.read, ape.write)
            f.close()
            ape.flush()
            sub = subprocess.Popen([BIN['mac'], ape.name, wave_filename, '-d'],
                                   stdout=devnull,
                                   stderr=devnull)
            sub.wait()
            ape.close()
            devnull.close()

    @classmethod
    def from_wave(cls, filename, wave_filename, compression=None):
        if (str(compression) not in cls.COMPRESSION_MODES):
            compression = cls.DEFAULT_COMPRESSION

        devnull = file(os.devnull, "wb")
        sub = subprocess.Popen(
            [BIN['mac'], wave_filename, filename,
             "-c%s" % (compression)],
            stdout=devnull,
            stderr=devnull)
        sub.wait()
        devnull.close()
        return ApeAudio(filename)
Exemplo n.º 8
0
class ApeTag(MetaData):
    ITEM = ApeTagItem

    APEv2_FLAGS = construct.BitStruct("APEv2_FLAGS",
                                      construct.Bits("undefined1", 5),
                                      construct.Flag("read_only"),
                                      construct.Bits("encoding", 2),
                                      construct.Bits("undefined2", 16),
                                      construct.Flag("contains_header"),
                                      construct.Flag("contains_no_footer"),
                                      construct.Flag("is_header"),
                                      construct.Bits("undefined3", 5))

    APEv2_FOOTER = construct.Struct("APEv2", construct.String("preamble", 8),
                                    construct.ULInt32("version_number"),
                                    construct.ULInt32("tag_size"),
                                    construct.ULInt32("item_count"),
                                    construct.Embed(APEv2_FLAGS),
                                    construct.ULInt64("reserved"))

    APEv2_HEADER = APEv2_FOOTER

    APEv2_TAG = ApeTagItem.APEv2_TAG

    ATTRIBUTE_MAP = {
        'track_name': 'Title',
        'track_number': 'Track',
        'track_total': 'Track',
        'album_number': 'Media',
        'album_total': 'Media',
        'album_name': 'Album',
        'artist_name': 'Artist',
        #"Performer" is not a defined APEv2 key
        #it would be nice to have, yet would not be standard
        'performer_name': 'Performer',
        'composer_name': 'Composer',
        'conductor_name': 'Conductor',
        'ISRC': 'ISRC',
        'catalog': 'Catalog',
        'copyright': 'Copyright',
        'publisher': 'Publisher',
        'year': 'Year',
        'date': 'Record Date',
        'comment': 'Comment'
    }

    INTEGER_ITEMS = ('Track', 'Media')

    #tags is a list of ApeTagItem objects
    #tag_length is an optional total length integer
    def __init__(self, tags, tag_length=None):
        for tag in tags:
            if (not isinstance(tag, ApeTagItem)):
                raise ValueError("%s is not ApeTag" % (repr(tag)))
        self.__dict__["tags"] = tags
        self.__dict__["tag_length"] = tag_length

    def __eq__(self, metadata):
        if (isinstance(metadata, ApeTag)):
            if (set(self.keys()) != set(metadata.keys())):
                return False

            for tag in self.tags:
                try:
                    if (tag.data != metadata[tag.key].data):
                        return False
                except KeyError:
                    return False
            else:
                return True
        elif (isinstance(metadata, MetaData)):
            return MetaData.__eq__(self, metadata)
        else:
            return False

    def keys(self):
        return [tag.key for tag in self.tags]

    def __getitem__(self, key):
        for tag in self.tags:
            if (tag.key == key):
                return tag
        else:
            raise KeyError(key)

    def get(self, key, default):
        try:
            return self[key]
        except KeyError:
            return default

    def __setitem__(self, key, value):
        for i in xrange(len(self.tags)):
            if (self.tags[i].key == key):
                self.tags[i] = value
                return
        else:
            self.tags.append(value)

    def index(self, key):
        for (i, tag) in enumerate(self.tags):
            if (tag.key == key):
                return i
        else:
            raise ValueError(key)

    def __delitem__(self, key):
        for i in xrange(len(self.tags)):
            if (self.tags[i].key == key):
                del (self.tags[i])
                return

    #if an attribute is updated (e.g. self.track_name)
    #make sure to update the corresponding dict pair
    def __setattr__(self, key, value):
        if (key in self.ATTRIBUTE_MAP):
            if (key == 'track_number'):
                self['Track'] = self.ITEM.string(
                    'Track', __number_pair__(value, self.track_total))
            elif (key == 'track_total'):
                self['Track'] = self.ITEM.string(
                    'Track', __number_pair__(self.track_number, value))
            elif (key == 'album_number'):
                self['Media'] = self.ITEM.string(
                    'Media', __number_pair__(value, self.album_total))
            elif (key == 'album_total'):
                self['Media'] = self.ITEM.string(
                    'Media', __number_pair__(self.album_number, value))
            else:
                self[self.ATTRIBUTE_MAP[key]] = self.ITEM.string(
                    self.ATTRIBUTE_MAP[key], value)
        else:
            self.__dict__[key] = value

    def __getattr__(self, key):
        if (key == 'track_number'):
            try:
                return int(
                    re.findall('\d+', unicode(self.get("Track", u"0")))[0])
            except IndexError:
                return 0
        elif (key == 'track_total'):
            try:
                return int(
                    re.findall('\d+/(\d+)', unicode(self.get("Track",
                                                             u"0")))[0])
            except IndexError:
                return 0
        elif (key == 'album_number'):
            try:
                return int(
                    re.findall('\d+', unicode(self.get("Media", u"0")))[0])
            except IndexError:
                return 0
        elif (key == 'album_total'):
            try:
                return int(
                    re.findall('\d+/(\d+)', unicode(self.get("Media",
                                                             u"0")))[0])
            except IndexError:
                return 0
        elif (key in self.ATTRIBUTE_MAP):
            return unicode(self.get(self.ATTRIBUTE_MAP[key], u''))
        elif (key in MetaData.__FIELDS__):
            return u''
        else:
            try:
                return self.__dict__[key]
            except KeyError:
                raise AttributeError(key)

    def __delattr__(self, key):
        if (key == 'track_number'):
            setattr(self, 'track_number', 0)
            if ((self.track_number == 0) and (self.track_total == 0)):
                del (self['Track'])
        elif (key == 'track_total'):
            setattr(self, 'track_total', 0)
            if ((self.track_number == 0) and (self.track_total == 0)):
                del (self['Track'])
        elif (key == 'album_number'):
            setattr(self, 'album_number', 0)
            if ((self.album_number == 0) and (self.album_total == 0)):
                del (self['Media'])
        elif (key == 'album_total'):
            setattr(self, 'album_total', 0)
            if ((self.album_number == 0) and (self.album_total == 0)):
                del (self['Media'])
        elif (key in self.ATTRIBUTE_MAP):
            try:
                del (self[self.ATTRIBUTE_MAP[key]])
            except ValueError:
                pass
        elif (key in MetaData.__FIELDS__):
            pass
        else:
            try:
                del (self.__dict__[key])
            except KeyError:
                raise AttributeError(key)

    @classmethod
    def converted(cls, metadata):
        if ((metadata is None) or (isinstance(metadata, ApeTag))):
            return metadata
        else:
            tags = cls([])
            for (field, key) in cls.ATTRIBUTE_MAP.items():
                if (field not in cls.__INTEGER_FIELDS__):
                    field = unicode(getattr(metadata, field))
                    if (len(field) > 0):
                        tags[key] = cls.ITEM.string(key, field)

            if ((metadata.track_number != 0) or (metadata.track_total != 0)):
                tags["Track"] = cls.ITEM.string(
                    "Track",
                    __number_pair__(metadata.track_number,
                                    metadata.track_total))

            if ((metadata.album_number != 0) or (metadata.album_total != 0)):
                tags["Media"] = cls.ITEM.string(
                    "Media",
                    __number_pair__(metadata.album_number,
                                    metadata.album_total))

            for image in metadata.images():
                tags.add_image(image)

            return tags

    def merge(self, metadata):
        metadata = self.__class__.converted(metadata)
        if (metadata is None):
            return

        for tag in metadata.tags:
            if ((tag.key not in ('Track', 'Media')) and (len(str(tag)) > 0)
                    and (len(str(self.get(tag.key, ""))) == 0)):
                self[tag.key] = tag
        for attr in ("track_number", "track_total", "album_number",
                     "album_total"):
            if ((getattr(self, attr) == 0) and (getattr(metadata, attr) != 0)):
                setattr(self, attr, getattr(metadata, attr))

    def __comment_name__(self):
        return u'APEv2'

    #takes two (key,value) apetag pairs
    #returns cmp on the weighted set of them
    #(title first, then artist, album, tracknumber)
    @classmethod
    def __by_pair__(cls, pair1, pair2):
        KEY_MAP = {
            "Title": 1,
            "Album": 2,
            "Track": 3,
            "Media": 4,
            "Artist": 5,
            "Performer": 6,
            "Composer": 7,
            "Conductor": 8,
            "Catalog": 9,
            "Publisher": 10,
            "ISRC": 11,
            #"Media":12,
            "Year": 13,
            "Record Date": 14,
            "Copyright": 15
        }

        return cmp((KEY_MAP.get(pair1[0], 16), pair1[0], pair1[1]),
                   (KEY_MAP.get(pair2[0], 16), pair2[0], pair2[1]))

    def __comment_pairs__(self):
        items = []

        for tag in self.tags:
            if (tag.key in ('Cover Art (front)', 'Cover Art (back)')):
                pass
            elif (tag.type == 0):
                items.append((tag.key, unicode(tag)))
            else:
                if (len(str(tag)) <= 20):
                    items.append((tag.key, str(tag).encode('hex')))
                else:
                    items.append(
                        (tag.key,
                         str(tag).encode('hex')[0:39].upper() + u"\u2026"))

        return sorted(items, ApeTag.__by_pair__)

    @classmethod
    def supports_images(cls):
        return True

    def __parse_image__(self, key, type):
        data = cStringIO.StringIO(str(self[key]))
        description = construct.CString(None).parse_stream(data).decode(
            'utf-8', 'replace')
        data = data.read()
        return Image.new(data, description, type)

    def add_image(self, image):
        if (image.type == 0):
            self['Cover Art (front)'] = self.ITEM.external(
                'Cover Art (front)',
                construct.CString(None).build(
                    image.description.encode('utf-8', 'replace')) + image.data)
        elif (image.type == 1):
            self['Cover Art (back)'] = self.ITEM.binary(
                'Cover Art (back)',
                construct.CString(None).build(
                    image.description.encode('utf-8', 'replace')) + image.data)

    def delete_image(self, image):
        if ((image.type == 0) and 'Cover Art (front)' in self.keys()):
            del (self['Cover Art (front)'])
        elif ((image.type == 1) and 'Cover Art (back)' in self.keys()):
            del (self['Cover Art (back)'])

    def images(self):
        #APEv2 supports only one value per key
        #so a single front and back cover are all that is possible
        img = []
        if ('Cover Art (front)' in self.keys()):
            img.append(self.__parse_image__('Cover Art (front)', 0))
        if ('Cover Art (back)' in self.keys()):
            img.append(self.__parse_image__('Cover Art (back)', 1))
        return img

    #takes a file object of a APEv2 tagged file
    #returns an ApeTag object or None
    @classmethod
    def read(cls, apefile):
        apefile.seek(-32, 2)
        footer = cls.APEv2_FOOTER.parse(apefile.read(32))

        if (footer.preamble != 'APETAGEX'):
            return None

        apefile.seek(-(footer.tag_size), 2)

        return cls([
            ApeTagItem(item_type=tag.encoding,
                       read_only=tag.read_only,
                       key=tag.key,
                       data=tag.value)
            for tag in construct.StrictRepeater(
                footer.item_count, cls.APEv2_TAG).parse(apefile.read())
        ],
                   tag_length=footer.tag_size + ApeTag.APEv2_FOOTER.sizeof()
                   if footer.contains_header else footer.tag_size)

    def build(self):
        header = construct.Container(preamble='APETAGEX',
                                     version_number=2000,
                                     tag_size=0,
                                     item_count=len(self.tags),
                                     undefined1=0,
                                     undefined2=0,
                                     undefined3=0,
                                     read_only=False,
                                     encoding=0,
                                     contains_header=True,
                                     contains_no_footer=False,
                                     is_header=True,
                                     reserved=0l)

        footer = construct.Container(preamble=header.preamble,
                                     version_number=header.version_number,
                                     tag_size=0,
                                     item_count=len(self.tags),
                                     undefined1=0,
                                     undefined2=0,
                                     undefined3=0,
                                     read_only=False,
                                     encoding=0,
                                     contains_header=True,
                                     contains_no_footer=False,
                                     is_header=False,
                                     reserved=0l)

        tags = "".join([tag.build() for tag in self.tags])

        footer.tag_size = header.tag_size = \
          len(tags) + len(ApeTag.APEv2_FOOTER.build(footer))

        return ApeTag.APEv2_FOOTER.build(header) + \
               tags + \
               ApeTag.APEv2_FOOTER.build(footer)
Exemplo n.º 9
0
class OggStreamReader:
    OGGS = construct.Struct(
        "oggs", construct.Const(construct.String("magic_number", 4), "OggS"),
        construct.Byte("version"), construct.Byte("header_type"),
        construct.SLInt64("granule_position"),
        construct.ULInt32("bitstream_serial_number"),
        construct.ULInt32("page_sequence_number"),
        construct.ULInt32("checksum"), construct.Byte("segments"),
        construct.MetaRepeater(lambda ctx: ctx["segments"],
                               construct.Byte("segment_lengths")))

    #stream is a file-like object with read() and close() methods
    def __init__(self, stream):
        self.stream = stream

    def close(self):
        self.stream.close()

    #an iterator which yields one fully-reassembled Ogg packet per pass
    def packets(self, from_beginning=True):
        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 construct.core.FieldError:
                break
            except construct.ConstError:
                break

    #an iterator which yields (Container,data string) tuples per pass
    #Container is parsed from OGGS
    #data string is a collection of segments as a string
    #(it may not be a complete packet)
    def pages(self, from_beginning=True):
        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 construct.core.FieldError:
                break
            except construct.ConstError:
                break

    #takes a page iterator (such as pages(), above)
    #returns a list of (Container,data string) tuples
    #which form a complete packet
    @classmethod
    def pages_to_packet(cls, pages_iter):
        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)

    #page_header is a Container object parsed through OGGS, above
    #page_data is a string of data contained by the page
    #returns an integer of the page's checksum
    @classmethod
    def calculate_ogg_checksum(cls, page_header, page_data):
        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
Exemplo n.º 10
0
class VorbisAudio(AudioFile):
    SUFFIX = "ogg"
    NAME = SUFFIX
    DEFAULT_COMPRESSION = "3"
    COMPRESSION_MODES = tuple([str(i) for i in range(0, 11)])
    BINARIES = ("oggenc", "oggdec")

    OGG_IDENTIFICATION = construct.Struct(
        "ogg_id", construct.ULInt32("vorbis_version"),
        construct.Byte("channels"), construct.ULInt32("sample_rate"),
        construct.ULInt32("bitrate_maximum"),
        construct.ULInt32("bitrate_nominal"),
        construct.ULInt32("bitrate_minimum"),
        construct.Embed(
            construct.BitStruct("flags", construct.Bits("blocksize_0", 4),
                                construct.Bits("blocksize_1", 4))),
        construct.Byte("framing"))

    COMMENT_HEADER = construct.Struct("comment_header",
                                      construct.Byte("packet_type"),
                                      construct.String("vorbis", 6))

    def __init__(self, filename):
        AudioFile.__init__(self, filename)
        self.__read_metadata__()

    @classmethod
    def is_type(cls, file):
        header = file.read(0x23)

        return (header.startswith('OggS')
                and header[0x1C:0x23] == '\x01vorbis')

    def __read_metadata__(self):
        f = OggStreamReader(file(self.filename, "rb"))
        packets = f.packets()

        try:
            #we'll assume this Vorbis file isn't interleaved
            #with any other Ogg stream

            #the Identification packet comes first
            id_packet = packets.next()
            header = VorbisAudio.COMMENT_HEADER.parse(
                id_packet[0:VorbisAudio.COMMENT_HEADER.sizeof()])
            if ((header.packet_type == 0x01) and (header.vorbis == 'vorbis')):
                identification = VorbisAudio.OGG_IDENTIFICATION.parse(
                    id_packet[VorbisAudio.COMMENT_HEADER.sizeof():])
                self.__sample_rate__ = identification.sample_rate
                self.__channels__ = identification.channels
            else:
                raise InvalidFile(_(u'First packet is not Vorbis'))

            #the Comment packet comes next
            comment_packet = packets.next()
            header = VorbisAudio.COMMENT_HEADER.parse(
                comment_packet[0:VorbisAudio.COMMENT_HEADER.sizeof()])
            if ((header.packet_type == 0x03) and (header.vorbis == 'vorbis')):
                self.comment = VorbisComment.VORBIS_COMMENT.parse(
                    comment_packet[VorbisAudio.COMMENT_HEADER.sizeof():])

        finally:
            del (packets)
            f.close()
            del (f)

    def lossless(self):
        return False

    def bits_per_sample(self):
        return 16

    def channels(self):
        return self.__channels__

    def channel_mask(self):
        if (self.channels() == 1):
            return ChannelMask.from_fields(front_center=True)
        elif (self.channels() == 2):
            return ChannelMask.from_fields(front_left=True, front_right=True)
        elif (self.channels() == 3):
            return ChannelMask.from_fields(front_left=True,
                                           front_right=True,
                                           front_center=True)
        elif (self.channels() == 4):
            return ChannelMask.from_fields(front_left=True,
                                           front_right=True,
                                           back_left=True,
                                           back_right=True)
        elif (self.channels() == 5):
            return ChannelMask.from_fields(front_left=True,
                                           front_right=True,
                                           front_center=True,
                                           back_left=True,
                                           back_right=True)
        elif (self.channels() == 6):
            return ChannelMask.from_fields(front_left=True,
                                           front_right=True,
                                           front_center=True,
                                           back_left=True,
                                           back_right=True,
                                           low_frequency=True)
        elif (self.channels() == 7):
            return ChannelMask.from_fields(front_left=True,
                                           front_right=True,
                                           front_center=True,
                                           side_left=True,
                                           side_right=True,
                                           back_center=True,
                                           low_frequency=True)
        elif (self.channels() == 8):
            return ChannelMask.from_fields(front_left=True,
                                           front_right=True,
                                           side_left=True,
                                           side_right=True,
                                           back_left=True,
                                           back_right=True,
                                           front_center=True,
                                           low_frequency=True)
        else:
            return ChannelMask(0)

    def total_frames(self):
        pcm_samples = 0
        f = file(self.filename, "rb")
        try:
            while (True):
                try:
                    page = OggStreamReader.OGGS.parse_stream(f)
                    pcm_samples = page.granule_position
                    f.seek(sum(page.segment_lengths), 1)
                except construct.core.FieldError:
                    break
                except construct.ConstError:
                    break

            return pcm_samples
        finally:
            f.close()

    def sample_rate(self):
        return self.__sample_rate__

    def to_pcm(self):
        sub = subprocess.Popen([
            BIN['oggdec'], '-Q', '-b',
            str(16), '-e',
            str(0), '-s',
            str(1), '-R', '-o', '-', self.filename
        ],
                               stdout=subprocess.PIPE,
                               stderr=file(os.devnull, "a"))

        pcmreader = PCMReader(sub.stdout,
                              sample_rate=self.sample_rate(),
                              channels=self.channels(),
                              channel_mask=int(self.channel_mask()),
                              bits_per_sample=self.bits_per_sample(),
                              process=sub)

        if (self.channels() <= 2):
            return pcmreader
        elif (self.channels() <= 8):
            #these mappings transform Vorbis order into ChannelMask order
            standard_channel_mask = self.channel_mask()
            vorbis_channel_mask = VorbisChannelMask(self.channel_mask())
            return ReorderedPCMReader(pcmreader, [
                vorbis_channel_mask.channels().index(channel)
                for channel in standard_channel_mask.channels()
            ])
        else:
            return pcmreader

    @classmethod
    def from_pcm(cls, filename, pcmreader, compression=None):
        if (compression not in cls.COMPRESSION_MODES):
            compression = cls.DEFAULT_COMPRESSION

        devnull = file(os.devnull, 'ab')

        sub = subprocess.Popen([
            BIN['oggenc'], '-Q', '-r', '-B',
            str(pcmreader.bits_per_sample), '-C',
            str(pcmreader.channels), '-R',
            str(pcmreader.sample_rate), '--raw-endianness',
            str(0), '-q', compression, '-o', filename, '-'
        ],
                               stdin=subprocess.PIPE,
                               stdout=devnull,
                               stderr=devnull,
                               preexec_fn=ignore_sigint)

        if ((pcmreader.channels <= 2) or (int(pcmreader.channel_mask) == 0)):
            transfer_framelist_data(pcmreader, sub.stdin.write)
        elif (pcmreader.channels <= 8):
            if (int(pcmreader.channel_mask) in (
                    0x7,  #FR, FC, FL
                    0x33,  #FR, FL, BR, BL
                    0x37,  #FR, FC, FL, BL, BR
                    0x3f,  #FR, FC, FL, BL, BR, LFE
                    0x70f,  #FL, FC, FR, SL, SR, BC, LFE
                    0x63f)  #FL, FC, FR, SL, SR, BL, BR, LFE
                ):
                standard_channel_mask = ChannelMask(pcmreader.channel_mask)
                vorbis_channel_mask = VorbisChannelMask(standard_channel_mask)
            else:
                raise UnsupportedChannelMask()

            transfer_framelist_data(
                ReorderedPCMReader(pcmreader, [
                    standard_channel_mask.channels().index(channel)
                    for channel in vorbis_channel_mask.channels()
                ]), sub.stdin.write)
        else:
            raise UnsupportedChannelMask()

        try:
            pcmreader.close()
        except DecodingError:
            raise EncodingError()
        sub.stdin.close()

        devnull.close()

        if (sub.wait() == 0):
            return VorbisAudio(filename)
        else:
            raise EncodingError(BIN['oggenc'])

    def set_metadata(self, metadata):
        metadata = VorbisComment.converted(metadata)

        if (metadata is None): return

        reader = OggStreamReader(file(self.filename, 'rb'))
        new_file = cStringIO.StringIO()
        writer = OggStreamWriter(new_file)
        current_sequence_number = 0

        pages = reader.pages()

        #transfer our old header
        #this must always be the first packet and the first page
        (header_page, header_data) = pages.next()
        writer.write_page(header_page, header_data)
        current_sequence_number += 1

        #grab the current "comment" and "setup headers" packets
        #these may take one or more pages,
        #but will always end on a page boundary
        del (pages)
        packets = reader.packets(from_beginning=False)

        comment_packet = packets.next()
        headers_packet = packets.next()

        #write the pages for our new "comment" packet
        for (page, data) in OggStreamWriter.build_pages(
                0, header_page.bitstream_serial_number,
                current_sequence_number,
                VorbisAudio.COMMENT_HEADER.build(
                    construct.Container(packet_type=3, vorbis='vorbis')) +
                metadata.build()):
            writer.write_page(page, data)
            current_sequence_number += 1

        #write the pages for the old "setup headers" packet
        for (page, data) in OggStreamWriter.build_pages(
                0, header_page.bitstream_serial_number,
                current_sequence_number, headers_packet):
            writer.write_page(page, data)
            current_sequence_number += 1

        #write the rest of the pages, re-sequenced and re-checksummed
        del (packets)
        pages = reader.pages(from_beginning=False)

        for (i, (page, data)) in enumerate(pages):
            page.page_sequence_number = i + current_sequence_number
            page.checksum = OggStreamReader.calculate_ogg_checksum(page, data)
            writer.write_page(page, data)

        reader.close()

        #re-write the file with our new data in "new_file"
        f = file(self.filename, "wb")
        f.write(new_file.getvalue())
        f.close()
        writer.close()

        self.__read_metadata__()

    def get_metadata(self):
        self.__read_metadata__()
        data = {}
        for pair in self.comment.value:
            try:
                (key, value) = pair.split('=', 1)
                data.setdefault(key, []).append(value.decode('utf-8'))
            except ValueError:
                continue

        return VorbisComment(data)

    def delete_metadata(self):
        self.set_metadata(MetaData())

    @classmethod
    def add_replay_gain(cls, filenames):
        track_names = [
            track.filename for track in open_files(filenames)
            if isinstance(track, cls)
        ]

        if ((len(track_names) > 0) and BIN.can_execute(BIN['vorbisgain'])):
            devnull = file(os.devnull, 'ab')

            sub = subprocess.Popen([BIN['vorbisgain'], '-q', '-a'] +
                                   track_names,
                                   stdout=devnull,
                                   stderr=devnull)
            sub.wait()
            devnull.close()

    @classmethod
    def can_add_replay_gain(cls):
        return BIN.can_execute(BIN['vorbisgain'])

    @classmethod
    def lossless_replay_gain(cls):
        return True

    def replay_gain(self):
        vorbis_metadata = self.get_metadata()

        if (set([
                'REPLAYGAIN_TRACK_PEAK', 'REPLAYGAIN_TRACK_GAIN',
                'REPLAYGAIN_ALBUM_PEAK', 'REPLAYGAIN_ALBUM_GAIN'
        ]).issubset(vorbis_metadata.keys())):  #we have ReplayGain data
            try:
                return ReplayGain(
                    vorbis_metadata['REPLAYGAIN_TRACK_GAIN'][0][0:-len(" dB")],
                    vorbis_metadata['REPLAYGAIN_TRACK_PEAK'][0],
                    vorbis_metadata['REPLAYGAIN_ALBUM_GAIN'][0][0:-len(" dB")],
                    vorbis_metadata['REPLAYGAIN_ALBUM_PEAK'][0])
            except ValueError:
                return None
        else:
            return None
Exemplo n.º 11
0
class ID3v1Comment(MetaData, list):
    ID3v1 = construct.Struct(
        "id3v1", construct.Const(construct.String("identifier", 3), 'TAG'),
        construct.String("song_title", 30), construct.String("artist", 30),
        construct.String("album", 30), construct.String("year", 4),
        construct.String("comment", 28), construct.Padding(1),
        construct.Byte("track_number"), construct.Byte("genre"))

    ID3v1_NO_TRACKNUMBER = construct.Struct(
        "id3v1_notracknumber",
        construct.Const(construct.String("identifier", 3), 'TAG'),
        construct.String("song_title", 30), construct.String("artist", 30),
        construct.String("album", 30), construct.String("year", 4),
        construct.String("comment", 30), construct.Byte("genre"))

    ATTRIBUTES = [
        'track_name', 'artist_name', 'album_name', 'year', 'comment',
        'track_number'
    ]

    #takes 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
    @classmethod
    def read_id3v1_comment(cls, mp3filename):
        mp3file = file(mp3filename, "rb")
        try:
            mp3file.seek(-128, 2)
            try:
                id3v1 = ID3v1Comment.ID3v1.parse(mp3file.read())
            except construct.adapters.PaddingError:
                mp3file.seek(-128, 2)
                id3v1 = ID3v1Comment.ID3v1_NO_TRACKNUMBER.parse(mp3file.read())
                id3v1.track_number = 0
            except construct.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()

    #takes several unicode strings (except for track_number, an int)
    #pads them with nulls and returns a complete ID3v1 tag
    @classmethod
    def build_id3v1(cls, song_title, artist, album, year, comment,
                    track_number):
        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 = construct.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)

    #metadata is the title,artist,album,year,comment,tracknum tuple returned by
    #read_id3v1_comment
    def __init__(self, metadata):
        list.__init__(self, metadata)

    def supports_images(self):
        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):
        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):
        return self.build_id3v1(self.track_name, self.artist_name,
                                self.album_name, self.year, self.comment,
                                self.track_number)

    def images(self):
        return []
Exemplo n.º 12
0
class SpeexAudio(VorbisAudio):
    SUFFIX = "spx"
    NAME = SUFFIX
    DEFAULT_COMPRESSION = "8"
    COMPRESSION_MODES = tuple([str(i) for i in range(0,11)])
    BINARIES = ("speexenc","speexdec")

    SPEEX_HEADER = construct.Struct('speex_header',
                              construct.String('speex_string',8),
                              construct.String('speex_version',20),
                              construct.ULInt32('speex_version_id'),
                              construct.ULInt32('header_size'),
                              construct.ULInt32('sampling_rate'),
                              construct.ULInt32('mode'),
                              construct.ULInt32('mode_bitstream_version'),
                              construct.ULInt32('channels'),
                              construct.ULInt32('bitrate'),
                              construct.ULInt32('frame_size'),
                              construct.ULInt32('vbr'),
                              construct.ULInt32('frame_per_packet'),
                              construct.ULInt32('extra_headers'),
                              construct.ULInt32('reserved1'),
                              construct.ULInt32('reserved2'))

    def __init__(self, filename):
        AudioFile.__init__(self, filename)
        self.__read_metadata__()

    @classmethod
    def is_type(cls, file):
        header = file.read(0x23)

        return (header.startswith('OggS') and
                header[0x1C:0x23] == 'Speex  ')

    def __read_metadata__(self):
        f = OggStreamReader(file(self.filename,"rb"))
        packets = f.packets()
        try:
            #first read the Header packet
            header = SpeexAudio.SPEEX_HEADER.parse(packets.next())

            self.__sample_rate__ = header.sampling_rate
            self.__channels__ = header.channels

            #the read the Comment packet
            comment_packet = packets.next()

            self.comment = UnframedVorbisComment.VORBIS_COMMENT.parse(
                comment_packet)
        finally:
            del(packets); f.close(); del(f)

    def to_pcm(self):
        devnull = file(os.devnull,'ab')
        sub = subprocess.Popen([BIN['speexdec'],self.filename,'-'],
                               stdout=subprocess.PIPE,
                               stderr=devnull)
        return PCMReader(
            sub.stdout,
            sample_rate=self.sample_rate(),
            channels=self.channels(),
            channel_mask=int(ChannelMask.from_channels(self.channels())),
            bits_per_sample=self.bits_per_sample(),
            process=sub)

    @classmethod
    def from_pcm(cls, filename, pcmreader, compression=None):
        import bisect

        if (compression not in cls.COMPRESSION_MODES):
            compression = cls.DEFAULT_COMPRESSION

        if ((pcmreader.bits_per_sample not in (8,16)) or
            (pcmreader.channels > 2) or
            (pcmreader.sample_rate not in (8000,16000,32000,44100))):
            pcmreader = PCMConverter(
                pcmreader,
                sample_rate=[8000,8000,16000,32000,44100][bisect.bisect(
                    [8000,16000,32000,44100],pcmreader.sample_rate)],
                channels=min(pcmreader.channels,2),
                channel_mask=ChannelMask.from_channels(min(pcmreader.channels,2)),
                bits_per_sample=min(pcmreader.bits_per_sample,16))


        BITS_PER_SAMPLE = {8:['--8bit'],
                           16:['--16bit']}[pcmreader.bits_per_sample]

        CHANNELS = {1:[],2:['--stereo']}[pcmreader.channels]

        devnull = file(os.devnull,"ab")

        sub = subprocess.Popen([BIN['speexenc'],
                                '--quality',str(compression),
                                '--rate',str(pcmreader.sample_rate),
                                '--le'] + \
                               BITS_PER_SAMPLE + \
                               CHANNELS + \
                               ['-',filename],
                               stdin=subprocess.PIPE,
                               stderr=devnull,
                               preexec_fn=ignore_sigint)

        transfer_framelist_data(pcmreader,sub.stdin.write)
        try:
            pcmreader.close()
        except DecodingError:
            raise EncodingError()
        sub.stdin.close()
        result = sub.wait()
        devnull.close()

        if (result == 0):
            return SpeexAudio(filename)
        else:
            raise EncodingError(BIN['speexenc'])

    def set_metadata(self, metadata):
        comment = VorbisComment.converted(metadata)

        if (comment is None): return

        reader = OggStreamReader(file(self.filename,'rb'))
        new_file = cStringIO.StringIO()
        writer = OggStreamWriter(new_file)

        pages = reader.pages()

        #transfer our old header
        (header_page,header_data) = pages.next()
        writer.write_page(header_page,header_data)

        #skip the existing comment packet
        (page,data) = pages.next()
        while (page.segment_lengths[-1] == 255):
            (page,data) = pages.next()

        #write the pages for our new comment packet
        comment_pages = OggStreamWriter.build_pages(
            0,
            header_page.bitstream_serial_number,
            header_page.page_sequence_number + 1,
            comment.build())

        for (page,data) in comment_pages:
            writer.write_page(page,data)

        #write the rest of the pages, re-sequenced and re-checksummed
        sequence_number = comment_pages[-1][0].page_sequence_number + 1
        for (i,(page,data)) in enumerate(pages):
            page.page_sequence_number = i + sequence_number
            page.checksum = OggStreamReader.calculate_ogg_checksum(page,data)
            writer.write_page(page,data)

        reader.close()

        #re-write the file with our new data in "new_file"
        f = file(self.filename,"wb")
        f.write(new_file.getvalue())
        f.close()
        writer.close()

        self.__read_metadata__()

    @classmethod
    def can_add_replay_gain(cls):
        return False
Exemplo n.º 13
0
class WavPackAudio(ApeTaggedAudio, AudioFile):
    SUFFIX = "wv"
    NAME = SUFFIX
    DEFAULT_COMPRESSION = "veryhigh"
    COMPRESSION_MODES = ("fast", "standard", "high", "veryhigh")
    BINARIES = ("wavpack", "wvunpack")

    APE_TAG_CLASS = WavePackAPEv2

    HEADER = construct.Struct(
        "wavpackheader", construct.Const(construct.String("id", 4), 'wvpk'),
        construct.ULInt32("block_size"), construct.ULInt16("version"),
        construct.ULInt8("track_number"), construct.ULInt8("index_number"),
        construct.ULInt32("total_samples"), construct.ULInt32("block_index"),
        construct.ULInt32("block_samples"),
        construct.Embed(
            construct.BitStruct("flags", construct.Flag("floating_point_data"),
                                construct.Flag("hybrid_noise_shaping"),
                                construct.Flag("cross_channel_decorrelation"),
                                construct.Flag("joint_stereo"),
                                construct.Flag("hybrid_mode"),
                                construct.Flag("mono_output"),
                                construct.Bits("bits_per_sample", 2),
                                construct.Bits("left_shift_data_low", 3),
                                construct.Flag("final_block_in_sequence"),
                                construct.Flag("initial_block_in_sequence"),
                                construct.Flag("hybrid_noise_balanced"),
                                construct.Flag("hybrid_mode_control_bitrate"),
                                construct.Flag("extended_size_integers"),
                                construct.Bit("sampling_rate_low"),
                                construct.Bits("maximum_magnitude", 5),
                                construct.Bits("left_shift_data_high", 2),
                                construct.Flag("reserved2"),
                                construct.Flag("false_stereo"),
                                construct.Flag("use_IIR"),
                                construct.Bits("reserved1", 2),
                                construct.Bits("sampling_rate_high", 3))),
        construct.ULInt32("crc"))

    SUB_HEADER = construct.Struct(
        "wavpacksubheader",
        construct.Embed(
            construct.BitStruct("flags", construct.Flag("large_block"),
                                construct.Flag("actual_size_1_less"),
                                construct.Flag("nondecoder_data"),
                                construct.Bits("metadata_function", 5))),
        construct.IfThenElse('size', lambda ctx: ctx['large_block'],
                             ULInt24('s'), construct.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)

    def __init__(self, filename):
        self.filename = filename
        self.__samplerate__ = 0
        self.__channels__ = 0
        self.__bitspersample__ = 0
        self.__total_frames__ = 0

        self.__read_info__()

    @classmethod
    def is_type(cls, file):
        return file.read(4) == 'wvpk'

    def lossless(self):
        return True

    @classmethod
    def supports_foreign_riff_chunks(cls):
        return True

    def channel_mask(self):
        fmt_chunk = WaveAudio.FMT_CHUNK.parse(self.__fmt_chunk__())
        if (fmt_chunk.compression != 0xFFFE):
            if (self.__channels__ == 1):
                return ChannelMask.from_fields(front_center=True)
            elif (self.__channels__ == 2):
                return ChannelMask.from_fields(front_left=True,
                                               front_right=True)
            #if we have a multi-channel WavPack file
            #that's not WAVEFORMATEXTENSIBLE,
            #assume the channels follow SMPTE/ITU-R recommendations
            #and hope for the best
            elif (self.__channels__ == 3):
                return ChannelMask.from_fields(front_left=True,
                                               front_right=True,
                                               front_center=True)
            elif (self.__channels__ == 4):
                return ChannelMask.from_fields(front_left=True,
                                               front_right=True,
                                               back_left=True,
                                               back_right=True)
            elif (self.__channels__ == 5):
                return ChannelMask.from_fields(front_left=True,
                                               front_right=True,
                                               back_left=True,
                                               back_right=True,
                                               front_center=True)
            elif (self.__channels__ == 6):
                return ChannelMask.from_fields(front_left=True,
                                               front_right=True,
                                               back_left=True,
                                               back_right=True,
                                               front_center=True,
                                               low_frequency=True)
            else:
                return ChannelMask(0)
        else:
            return WaveAudio.fmt_chunk_to_channel_mask(fmt_chunk.channel_mask)

    def get_metadata(self):
        metadata = ApeTaggedAudio.get_metadata(self)
        if (metadata is not None):
            metadata.frame_count = self.total_frames()
        return metadata

    def has_foreign_riff_chunks(self):
        for (sub_header, nondecoder, data) in self.sub_frames():
            if ((sub_header == 1) and nondecoder):
                return set(__riff_chunk_ids__(data)) != set(['fmt ', 'data'])
        else:
            return False

    def __fmt_chunk__(self):
        for (sub_header, nondecoder, data) in self.sub_frames():
            if ((sub_header == 1) and nondecoder):
                for (chunk_id, chunk_data) in __riff_chunks__(data):
                    if (chunk_id == 'fmt '):
                        return chunk_data
        else:
            return None

    def frames(self):
        f = file(self.filename)
        remaining_samples = None
        try:
            while ((remaining_samples is None) or (remaining_samples > 0)):
                try:
                    header = WavPackAudio.HEADER.parse(
                        f.read(WavPackAudio.HEADER.sizeof()))
                except construct.ConstError:
                    raise InvalidFile(_(u'WavPack header ID invalid'))

                if (remaining_samples is None):
                    remaining_samples = (header.total_samples - \
                                         header.block_samples)
                else:
                    remaining_samples -= header.block_samples

                data = f.read(header.block_size - 24)

                yield (header, data)
        finally:
            f.close()

    def sub_frames(self):
        import cStringIO

        for (header, data) in self.frames():
            total_size = len(data)
            data = cStringIO.StringIO(data)
            while (data.tell() < total_size):
                sub_header = WavPackAudio.SUB_HEADER.parse_stream(data)
                if (sub_header.actual_size_1_less):
                    yield (sub_header.metadata_function,
                           sub_header.nondecoder_data,
                           data.read((sub_header.size * 2) - 1))
                    data.read(1)
                else:
                    yield (sub_header.metadata_function,
                           sub_header.nondecoder_data,
                           data.read(sub_header.size * 2))

    def __read_info__(self):
        f = file(self.filename)
        try:
            try:
                header = WavPackAudio.HEADER.parse(
                    f.read(WavPackAudio.HEADER.sizeof()))
            except construct.ConstError:
                raise InvalidFile(_(u'WavPack header ID invalid'))

            self.__samplerate__ = WavPackAudio.SAMPLING_RATE[(
                header.sampling_rate_high << 1) | header.sampling_rate_low]
            self.__bitspersample__ = WavPackAudio.BITS_PER_SAMPLE[
                header.bits_per_sample]
            self.__total_frames__ = header.total_samples

            self.__channels__ = 0

            #go through as many headers as necessary
            #to count the number of channels
            if (header.mono_output):
                self.__channels__ += 1
            else:
                self.__channels__ += 2

            while (not header.final_block_in_sequence):
                f.seek(header.block_size - 24, 1)
                header = WavPackAudio.HEADER.parse(
                    f.read(WavPackAudio.HEADER.sizeof()))
                if (header.mono_output):
                    self.__channels__ += 1
                else:
                    self.__channels__ += 2
        finally:
            f.close()

    def bits_per_sample(self):
        return self.__bitspersample__

    def channels(self):
        return self.__channels__

    def total_frames(self):
        return self.__total_frames__

    def sample_rate(self):
        return self.__samplerate__

    @classmethod
    def from_pcm(cls, filename, pcmreader, compression=None):
        compression_param = {
            "fast": ["-f"],
            "standard": [],
            "high": ["-h"],
            "veryhigh": ["-hh"]
        }

        if (str(compression) not in cls.COMPRESSION_MODES):
            compression = cls.DEFAULT_COMPRESSION

        if ('--raw-pcm' in cls.__wavpack_help__()):
            if (filename.endswith(".wv")):
                devnull = file(os.devnull, 'ab')

                if (pcmreader.channels > 18):
                    raise UnsupportedChannelMask()
                elif (pcmreader.channels > 2):
                    order_map = {
                        "front_left": "FL",
                        "front_right": "FR",
                        "front_center": "FC",
                        "low_frequency": "LFE",
                        "back_left": "BL",
                        "back_right": "BR",
                        "front_left_of_center": "FLC",
                        "front_right_of_center": "FRC",
                        "back_center": "BC",
                        "side_left": "SL",
                        "side_right": "SR",
                        "top_center": "TC",
                        "top_front_left": "TFL",
                        "top_front_center": "TFC",
                        "top_front_right": "TFR",
                        "top_back_left": "TBL",
                        "top_back_center": "TBC",
                        "top_back_right": "TBR"
                    }

                    channel_order = [
                        "--channel-order=%s" % (",".join([
                            order_map[channel] for channel in ChannelMask(
                                pcmreader.channel_mask).channels()
                        ]))
                    ]
                else:
                    channel_order = []

                sub = subprocess.Popen([BIN['wavpack']] + \
                                           compression_param[compression] + \
                                           ['-q','-y',
                                            "--raw-pcm=%(sr)s,%(bps)s,%(ch)s"%\
                                              {"sr":pcmreader.sample_rate,
                                               "bps":pcmreader.bits_per_sample,
                                               "ch":pcmreader.channels}] + \
                                           channel_order + \
                                           ['-','-o',filename],
                                       stdout=devnull,
                                       stderr=devnull,
                                       stdin=subprocess.PIPE,
                                       preexec_fn=ignore_sigint)

                transfer_framelist_data(pcmreader, sub.stdin.write)
                devnull.close()
                sub.stdin.close()

                if (sub.wait() == 0):
                    return WavPackAudio(filename)
                else:
                    raise EncodingError(BIN['wavpack'])
            else:
                import tempfile

                tempdir = tempfile.mkdtemp()
                symlink = os.path.join(tempdir,
                                       os.path.basename(filename) + ".wv")
                try:
                    os.symlink(os.path.abspath(filename), symlink)
                    cls.from_pcm(symlink, pcmreader, compression)
                    return WavPackAudio(filename)
                finally:
                    os.unlink(symlink)
                    os.rmdir(tempdir)
        else:
            import tempfile

            f = tempfile.NamedTemporaryFile(suffix=".wav")
            w = WaveAudio.from_pcm(f.name, pcmreader)

            try:
                return cls.from_wave(filename, w.filename, compression)
            finally:
                del (w)
                f.close()

    def to_wave(self, wave_filename):
        devnull = file(os.devnull, 'ab')

        #WavPack stupidly refuses to run if the filename doesn't end with .wv
        if (self.filename.endswith(".wv")):
            sub = subprocess.Popen([
                BIN['wvunpack'], '-q', '-y', self.filename, '-o', wave_filename
            ],
                                   stdout=devnull,
                                   stderr=devnull)
            if (sub.wait() != 0):
                raise EncodingError()
        else:
            #create a temporary symlink to the current file
            #rather than rewrite the whole thing
            import tempfile

            tempdir = tempfile.mkdtemp()
            symlink = os.path.join(tempdir,
                                   os.path.basename(self.filename) + ".wv")
            try:
                os.symlink(os.path.abspath(self.filename), symlink)
                WavPackAudio(symlink).to_wave(wave_filename)
            finally:
                os.unlink(symlink)
                os.rmdir(tempdir)

    def to_pcm(self):
        if (self.filename.endswith(".wv")):
            if ('-r' in WavPackAudio.__wvunpack_help__()):
                sub = subprocess.Popen([
                    BIN['wvunpack'], '-q', '-y', self.filename, '-r', '-o', '-'
                ],
                                       stdout=subprocess.PIPE,
                                       stderr=file(os.devnull, 'ab'))

                return PCMReader(sub.stdout,
                                 sample_rate=self.sample_rate(),
                                 channels=self.channels(),
                                 channel_mask=int(self.channel_mask()),
                                 bits_per_sample=self.bits_per_sample(),
                                 process=sub)
            else:
                sub = subprocess.Popen(
                    [BIN['wvunpack'], '-q', '-y', self.filename, '-o', '-'],
                    stdout=subprocess.PIPE,
                    stderr=file(os.devnull, 'ab'))

                return WaveReader(sub.stdout,
                                  sample_rate=self.sample_rate(),
                                  channels=self.channels(),
                                  channel_mask=int(self.channel_mask()),
                                  bits_per_sample=self.bits_per_sample(),
                                  process=sub)
        else:
            #create a temporary symlink to the current file
            #rather than rewrite the whole thing
            (tempdir, symlink) = SymlinkPCMReader.new(self.filename, ".wv")
            return SymlinkPCMReader(
                WavPackAudio(symlink).to_pcm(), tempdir, symlink)

    @classmethod
    def from_wave(cls, filename, wave_filename, compression=None):
        if (str(compression) not in cls.COMPRESSION_MODES):
            compression = cls.DEFAULT_COMPRESSION

        compression_param = {
            "fast": ["-f"],
            "standard": [],
            "high": ["-h"],
            "veryhigh": ["-hh"]
        }

        #wavpack will add a .wv suffix if there isn't one
        #this isn't desired behavior
        if (filename.endswith(".wv")):
            devnull = file(os.devnull, 'ab')

            sub = subprocess.Popen([BIN['wavpack'],
                                    wave_filename] + \
                                   compression_param[compression] + \
                                   ['-q','-y','-o',
                                    filename],
                                   stdout=devnull,
                                   stderr=devnull,
                                   preexec_fn=ignore_sigint)

            devnull.close()

            if (sub.wait() == 0):
                return WavPackAudio(filename)
            else:
                raise EncodingError(BIN['wavpack'])
        else:
            import tempfile

            tempdir = tempfile.mkdtemp()
            symlink = os.path.join(tempdir, os.path.basename(filename) + ".wv")
            try:
                os.symlink(os.path.abspath(filename), symlink)
                cls.from_wave(symlink, wave_filename, compression)
                return WavPackAudio(filename)
            finally:
                os.unlink(symlink)
                os.rmdir(tempdir)

    @classmethod
    def __wavpack_help__(cls):
        devnull = open(os.devnull, "wb")
        sub = subprocess.Popen([BIN["wavpack"], "--help"],
                               stdout=subprocess.PIPE,
                               stderr=devnull)
        help_data = sub.stdout.read()
        sub.stdout.close()
        devnull.close()
        sub.wait()
        return help_data

    @classmethod
    def __wvunpack_help__(cls):
        devnull = open(os.devnull, "wb")
        sub = subprocess.Popen([BIN["wvunpack"], "--help"],
                               stdout=subprocess.PIPE,
                               stderr=devnull)
        help_data = sub.stdout.read()
        sub.stdout.close()
        devnull.close()
        sub.wait()
        return help_data

    @classmethod
    def add_replay_gain(cls, filenames):
        track_names = [
            track.filename for track in open_files(filenames)
            if isinstance(track, cls)
        ]

        if ((len(track_names) > 0) and BIN.can_execute(BIN['wvgain'])):
            devnull = file(os.devnull, 'ab')

            sub = subprocess.Popen([BIN['wvgain'], '-q', '-a'] + track_names,
                                   stdout=devnull,
                                   stderr=devnull)
            sub.wait()
            devnull.close()

    @classmethod
    def can_add_replay_gain(cls):
        return BIN.can_execute(BIN['wvgain'])

    @classmethod
    def lossless_replay_gain(cls):
        return True

    def replay_gain(self):
        metadata = self.get_metadata()
        if (metadata is None):
            return None

        if (set([
                'replaygain_track_gain', 'replaygain_track_peak',
                'replaygain_album_gain', 'replaygain_album_peak'
        ]).issubset(metadata.keys())):  #we have ReplayGain data
            try:
                return ReplayGain(
                    unicode(metadata['replaygain_track_gain'])[0:-len(" dB")],
                    unicode(metadata['replaygain_track_peak']),
                    unicode(metadata['replaygain_album_gain'])[0:-len(" dB")],
                    unicode(metadata['replaygain_album_peak']))
            except ValueError:
                return None
        else:
            return None

    def get_cuesheet(self):
        import cue

        metadata = self.get_metadata()

        if ((metadata is not None) and ('Cuesheet' in metadata.keys())):
            try:
                return cue.parse(
                    cue.tokens(
                        unicode(metadata['Cuesheet']).encode(
                            'utf-8', 'replace')))
            except cue.CueException:
                #unlike FLAC, just because a cuesheet is embedded
                #does not mean it is compliant
                return None
        else:
            return None

    def set_cuesheet(self, cuesheet):
        import os.path
        import cue

        if (cuesheet is None):
            return

        metadata = self.get_metadata()
        if (metadata is None):
            metadata = WavePackAPEv2.converted(MetaData())

        metadata['Cuesheet'] = WavePackAPEv2.ITEM.string(
            'Cuesheet',
            cue.Cuesheet.file(cuesheet,
                              os.path.basename(self.filename)).decode(
                                  'ascii', 'replace'))
        self.set_metadata(metadata)
Exemplo n.º 14
0
class __JPEG__(ImageMetrics):
    SEGMENT_HEADER = construct.Struct('segment_header',
                                construct.Const(construct.Byte('header'),0xFF),
                                construct.Byte('type'),
                                construct.If(
        lambda ctx: ctx['type'] not in (0xD8,0xD9),
        construct.UBInt16('length')))

    APP0 = construct.Struct('JFIF_segment_marker',
                      construct.String('identifier',5),
                      construct.Byte('major_version'),
                      construct.Byte('minor_version'),
                      construct.Byte('density_units'),
                      construct.UBInt16('x_density'),
                      construct.UBInt16('y_density'),
                      construct.Byte('thumbnail_width'),
                      construct.Byte('thumbnail_height'))

    SOF = construct.Struct('start_of_frame',
                     construct.Byte('data_precision'),
                     construct.UBInt16('image_height'),
                     construct.UBInt16('image_width'),
                     construct.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 construct.ConstError:
            raise InvalidJPEG(_(u"Invalid JPEG segment marker at 0x%X") % \
                                  (file.tell()))
Exemplo n.º 15
0
class __TIFF__(ImageMetrics):
    HEADER = construct.Struct('header',
                        construct.String('byte_order',2),
                        construct.Switch('order',
                                   lambda ctx: ctx['byte_order'],
                                   {"II":construct.Embed(
        construct.Struct('little_endian',
                   construct.Const(construct.ULInt16('version'),42),
                   construct.ULInt32('offset'))),
                                    "MM":construct.Embed(
        construct.Struct('big_endian',
                   construct.Const(construct.UBInt16('version'),42),
                   construct.UBInt32('offset')))}))

    L_IFD = construct.Struct('ifd',
                       construct.PrefixedArray(
        length_field=construct.ULInt16('length'),
        subcon=construct.Struct('tags',
                          construct.ULInt16('id'),
                          construct.ULInt16('type'),
                          construct.ULInt32('count'),
                          construct.ULInt32('offset'))),
                       construct.ULInt32('next'))

    B_IFD = construct.Struct('ifd',
                       construct.PrefixedArray(
        length_field=construct.UBInt16('length'),
        subcon=construct.Struct('tags',
                          construct.UBInt16('id'),
                          construct.UBInt16('type'),
                          construct.UBInt32('count'),
                          construct.UBInt32('offset'))),
                       construct.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:construct.Byte("data"),
                   2:construct.CString("data"),
                   3:construct.UBInt16("data"),
                   4:construct.UBInt32("data"),
                   5:construct.Struct("data",
                                construct.UBInt32("high"),
                                construct.UBInt32("low"))}[tag.type]


        data = construct.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:construct.Byte("data"),
                   2:construct.CString("data"),
                   3:construct.ULInt16("data"),
                   4:construct.ULInt32("data"),
                   5:construct.Struct("data",
                                construct.ULInt32("high"),
                                construct.ULInt32("low"))}[tag.type]


        data = construct.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 construct.ConstError:
            raise InvalidTIFF(_(u'Invalid TIFF'))
Exemplo n.º 16
0
class __PNG__(ImageMetrics):
    HEADER = construct.Const(construct.String('header',8),
                       '89504e470d0a1a0a'.decode('hex'))
    CHUNK_HEADER = construct.Struct('chunk',
                              construct.UBInt32('length'),
                              construct.String('type',4))
    CHUNK_FOOTER = construct.Struct('crc32',
                              construct.UBInt32('crc'))

    IHDR = construct.Struct('IHDR',
                      construct.UBInt32('width'),
                      construct.UBInt32('height'),
                      construct.Byte('bit_depth'),
                      construct.Byte('color_type'),
                      construct.Byte('compression_method'),
                      construct.Byte('filter_method'),
                      construct.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 construct.ConstError:
            raise InvalidPNG(_(u'Invalid PNG'))