You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.8 KiB

4 years ago
  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. #
  15. from . import Image, ImageFile
  16. from ._binary import i8
  17. __version__ = "0.1"
  18. #
  19. # Bitstream parser
  20. class BitStream(object):
  21. def __init__(self, fp):
  22. self.fp = fp
  23. self.bits = 0
  24. self.bitbuffer = 0
  25. def next(self):
  26. return i8(self.fp.read(1))
  27. def peek(self, bits):
  28. while self.bits < bits:
  29. c = self.next()
  30. if c < 0:
  31. self.bits = 0
  32. continue
  33. self.bitbuffer = (self.bitbuffer << 8) + c
  34. self.bits += 8
  35. return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1
  36. def skip(self, bits):
  37. while self.bits < bits:
  38. self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1))
  39. self.bits += 8
  40. self.bits = self.bits - bits
  41. def read(self, bits):
  42. v = self.peek(bits)
  43. self.bits = self.bits - bits
  44. return v
  45. ##
  46. # Image plugin for MPEG streams. This plugin can identify a stream,
  47. # but it cannot read it.
  48. class MpegImageFile(ImageFile.ImageFile):
  49. format = "MPEG"
  50. format_description = "MPEG"
  51. def _open(self):
  52. s = BitStream(self.fp)
  53. if s.read(32) != 0x1B3:
  54. raise SyntaxError("not an MPEG file")
  55. self.mode = "RGB"
  56. self._size = s.read(12), s.read(12)
  57. # --------------------------------------------------------------------
  58. # Registry stuff
  59. Image.register_open(MpegImageFile.format, MpegImageFile)
  60. Image.register_extensions(MpegImageFile.format, [".mpg", ".mpeg"])
  61. Image.register_mime(MpegImageFile.format, "video/mpeg")