Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pillow-10.4.0-py3.8-linux-x86_64.egg/PIL/MpegImagePlugin.py: 42%

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

45 statements  

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 

16 

17from . import Image, ImageFile 

18from ._binary import i8 

19from ._typing import SupportsRead 

20 

21# 

22# Bitstream parser 

23 

24 

25class BitStream: 

26 def __init__(self, fp: SupportsRead[bytes]) -> None: 

27 self.fp = fp 

28 self.bits = 0 

29 self.bitbuffer = 0 

30 

31 def next(self) -> int: 

32 return i8(self.fp.read(1)) 

33 

34 def peek(self, bits: int) -> int: 

35 while self.bits < bits: 

36 c = self.next() 

37 if c < 0: 

38 self.bits = 0 

39 continue 

40 self.bitbuffer = (self.bitbuffer << 8) + c 

41 self.bits += 8 

42 return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1 

43 

44 def skip(self, bits: int) -> None: 

45 while self.bits < bits: 

46 self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1)) 

47 self.bits += 8 

48 self.bits = self.bits - bits 

49 

50 def read(self, bits: int) -> int: 

51 v = self.peek(bits) 

52 self.bits = self.bits - bits 

53 return v 

54 

55 

56def _accept(prefix: bytes) -> bool: 

57 return prefix[:4] == b"\x00\x00\x01\xb3" 

58 

59 

60## 

61# Image plugin for MPEG streams. This plugin can identify a stream, 

62# but it cannot read it. 

63 

64 

65class MpegImageFile(ImageFile.ImageFile): 

66 format = "MPEG" 

67 format_description = "MPEG" 

68 

69 def _open(self) -> None: 

70 assert self.fp is not None 

71 

72 s = BitStream(self.fp) 

73 if s.read(32) != 0x1B3: 

74 msg = "not an MPEG file" 

75 raise SyntaxError(msg) 

76 

77 self._mode = "RGB" 

78 self._size = s.read(12), s.read(12) 

79 

80 

81# -------------------------------------------------------------------- 

82# Registry stuff 

83 

84Image.register_open(MpegImageFile.format, MpegImageFile, _accept) 

85 

86Image.register_extensions(MpegImageFile.format, [".mpg", ".mpeg"]) 

87 

88Image.register_mime(MpegImageFile.format, "video/mpeg")