Pillow/src/PIL/MpegImagePlugin.py

85 lines
1.8 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04: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.
#
from . import Image, ImageFile
from ._binary import i8
2010-07-31 06:52:47 +04:00
#
# Bitstream parser
class BitStream:
2010-07-31 06:52:47 +04:00
def __init__(self, fp):
self.fp = fp
self.bits = 0
self.bitbuffer = 0
def next(self):
return i8(self.fp.read(1))
2010-07-31 06:52:47 +04: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
self.bits += 8
return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1
2010-07-31 06:52:47 +04:00
def skip(self, bits):
while self.bits < bits:
self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1))
self.bits += 8
2010-07-31 06:52:47 +04:00
self.bits = self.bits - bits
def read(self, bits):
v = self.peek(bits)
self.bits = self.bits - bits
return v
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
##
# Image plugin for MPEG streams. This plugin can identify a stream,
# but it cannot read it.
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
class MpegImageFile(ImageFile.ImageFile):
format = "MPEG"
format_description = "MPEG"
def _open(self):
s = BitStream(self.fp)
if s.read(32) != 0x1B3:
msg = "not an MPEG file"
raise SyntaxError(msg)
2010-07-31 06:52:47 +04:00
self.mode = "RGB"
self._size = s.read(12), s.read(12)
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------
# Registry stuff
Image.register_open(MpegImageFile.format, MpegImageFile)
2010-07-31 06:52:47 +04:00
2016-04-25 07:59:02 +03:00
Image.register_extensions(MpegImageFile.format, [".mpg", ".mpeg"])
2010-07-31 06:52:47 +04:00
Image.register_mime(MpegImageFile.format, "video/mpeg")