openmedialibrary_platform/Darwin/lib/python3.5/site-packages/PIL/MpegImagePlugin.py

87 lines
1.8 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
#
# The Python Imaging Library.
# $Id$
#
# MPEG file handling
#
# History:
# 95-09-09 fl Created
#
# Copyright (c) Secret Labs AB 1997.
# Copyright (c) Fredrik Lundh 1995.
#
# See the README file for information on usage and redistribution.
#
2014-09-30 16:15:32 +00:00
from PIL import Image, ImageFile
from PIL._binary import i8
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
__version__ = "0.1"
2013-10-11 17:28:32 +00:00
#
# Bitstream parser
2015-11-25 01:25:01 +00:00
class BitStream(object):
2013-10-11 17:28:32 +00:00
def __init__(self, fp):
self.fp = fp
self.bits = 0
self.bitbuffer = 0
def next(self):
2014-09-30 16:15:32 +00:00
return i8(self.fp.read(1))
2013-10-11 17:28:32 +00:00
def peek(self, bits):
while self.bits < bits:
c = self.next()
if c < 0:
self.bits = 0
continue
self.bitbuffer = (self.bitbuffer << 8) + c
2014-09-30 16:15:32 +00:00
self.bits += 8
return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1
2013-10-11 17:28:32 +00:00
def skip(self, bits):
while self.bits < bits:
2014-09-30 16:15:32 +00:00
self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1))
self.bits += 8
2013-10-11 17:28:32 +00:00
self.bits = self.bits - bits
def read(self, bits):
v = self.peek(bits)
self.bits = self.bits - bits
return v
2015-11-25 01:25:01 +00:00
2013-10-11 17:28:32 +00:00
##
# Image plugin for MPEG streams. This plugin can identify a stream,
# but it cannot read it.
class MpegImageFile(ImageFile.ImageFile):
format = "MPEG"
format_description = "MPEG"
def _open(self):
s = BitStream(self.fp)
if s.read(32) != 0x1B3:
2014-09-30 16:15:32 +00:00
raise SyntaxError("not an MPEG file")
2013-10-11 17:28:32 +00:00
self.mode = "RGB"
self.size = s.read(12), s.read(12)
# --------------------------------------------------------------------
# Registry stuff
2015-11-25 01:25:01 +00:00
Image.register_open(MpegImageFile.format, MpegImageFile)
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
Image.register_extension(MpegImageFile.format, ".mpg")
Image.register_extension(MpegImageFile.format, ".mpeg")
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
Image.register_mime(MpegImageFile.format, "video/mpeg")