Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/MpegImagePlugin.py: 82%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#
2# The Python Imaging Library.
3# $Id$
4#
5# MPEG file handling
6#
7# History:
8# 95-09-09 fl Created
9#
10# Copyright (c) Secret Labs AB 1997.
11# Copyright (c) Fredrik Lundh 1995.
12#
13# See the README file for information on usage and redistribution.
14#
15from __future__ import annotations
17__lazy_modules__ = {"PIL._binary"}
19from . import Image, ImageFile
20from ._binary import i8
22TYPE_CHECKING = False
23if TYPE_CHECKING:
24 from ._typing import SupportsRead
26#
27# Bitstream parser
30class BitStream:
31 def __init__(self, fp: SupportsRead[bytes]) -> None:
32 self.fp = fp
33 self.bits = 0
34 self.bitbuffer = 0
36 def next(self) -> int:
37 return i8(self.fp.read(1))
39 def peek(self, bits: int) -> int:
40 while self.bits < bits:
41 self.bitbuffer = (self.bitbuffer << 8) + self.next()
42 self.bits += 8
43 return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1
45 def skip(self, bits: int) -> None:
46 while self.bits < bits:
47 self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1))
48 self.bits += 8
49 self.bits = self.bits - bits
51 def read(self, bits: int) -> int:
52 v = self.peek(bits)
53 self.bits = self.bits - bits
54 return v
57def _accept(prefix: bytes) -> bool:
58 return prefix.startswith(b"\x00\x00\x01\xb3")
61##
62# Image plugin for MPEG streams. This plugin can identify a stream,
63# but it cannot read it.
66class MpegImageFile(ImageFile.ImageFile):
67 format = "MPEG"
68 format_description = "MPEG"
70 def _open(self) -> None:
71 assert self.fp is not None
73 s = BitStream(self.fp)
74 if s.read(32) != 0x1B3:
75 msg = "not an MPEG file"
76 raise SyntaxError(msg)
78 self._mode = "RGB"
79 self._size = s.read(12), s.read(12)
82# --------------------------------------------------------------------
83# Registry stuff
85Image.register_open(MpegImageFile.format, MpegImageFile, _accept)
87Image.register_extensions(MpegImageFile.format, [".mpg", ".mpeg"])
89Image.register_mime(MpegImageFile.format, "video/mpeg")