Commits

Anonymous committed 5f26009

MPEG2-TS:
* Write validate() method
* Able to synchronize to first valid chunk
* Split adaptation_field_control 2-bit field in two bit fields

Comments (0)

Files changed (1)

hachoir-parser/hachoir_parser/video/mpeg_ts.py

             self._size = 188*8
 
     def createFields(self):
-        yield Bits(self, "sync", 8)
+        yield UInt8(self, "sync", 8, text_handler=hexadecimal)
         if self["sync"].value != 0x47:
             raise ParserError("MPEG-2 TS: Invalid synchronization byte")
         yield Bit(self, "has_error")
-        yield Bit(self, "payload_unistart")
+        yield Bit(self, "payload_unit_start")
         yield Bit(self, "priority")
-        yield Bits(self, "pid", 13)
+        yield Bits(self, "pid", 13, text_handler=hexadecimal)
         yield Bits(self, "scrambling_control", 2)
-        yield Bits(self, "adaptation_field_control", 2)
+
+        yield Bit(self, "has_adaptation")
+        yield Bit(self, "has_payload")
+
         yield Bits(self, "counter", 4)
         yield RawBytes(self, "payload", 184)
         if self["has_error"].value:
             yield RawBytes(self, "error_correction", 16)
 
+    def createDescription(self):
+        text = "Packet: PID %s" % self["pid"].display
+        if self["payload_unit_start"].value:
+            text += ", start of payload"
+        return text
+
+    def isValid(self):
+        if not self["has_payload"].value and not self["has_adaptation"].value:
+            return u"No payload and no adaptation"
+        return ""
+
 class MPEG_TS(Parser):
     tags = {
         "file_ext": ("ts",),
     endian = BIG_ENDIAN
 
     def validate(self):
+        sync = self.stream.searchBytes("\x47", 0, 204*8)
+        if sync is None:
+            return "Unable to find synchronization byte"
+        err = self["packet[0]"].isValid()
+        if err:
+            return "Packet #0 is invalid: %s" % err
         return True
 
     def createFields(self):
+        sync = self.stream.searchBytes("\x47", 0, 204*8)
+        if sync is None:
+            raise ParserError("Unable to find synchronization byte")
+        elif sync:
+            yield RawBytes(self, "incomplete_packet", sync//8)
         while not self.eof:
             yield Packet(self, "packet[]")
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.