'Parse a PES header.
Since it starts with 0x00 0x00 0x01 like \'normal\' mpegs, this
function will return (0, None) when it is no PES header or
(packet length, timestamp position (maybe None))
http://dvd.sourceforge.net/dvdinfo/pes-hdr.html'

def ReadPESHeader(self, offset, buffer, id=0):

    if (not (buffer[0:3] == '\x00\x00\x01')):
        return (0, None)
    packet_length = (((ord(buffer[4]) << 8) + ord(buffer[5])) + 6)
    align = (ord(buffer[6]) & 4)
    header_length = ord(buffer[8])
    if ((ord(buffer[3]) & 224) == 192):
        id = (id or (ord(buffer[3]) & 31))
        for a in self.audio:
            if (a.id == id):
                break
        else:
            self.audio.append(core.AudioStream())
            self.audio[(-1)]._set('id', id)
    elif ((ord(buffer[3]) & 240) == 224):
        id = (id or (ord(buffer[3]) & 15))
        for v in self.video:
            if (v.id == id):
                break
        else:
            self.video.append(core.VideoStream())
            self.video[(-1)]._set('id', id)
        if ((buffer[(header_length + 9):(header_length + 13)] == '\x00\x00\x01\xb3') and (not self.sequence_header_offset)):
            self.sequence_header_offset = ((offset + header_length) + 9)
    elif ((ord(buffer[3]) == 189) or (ord(buffer[3]) == 191)):
        id = (id or (ord(buffer[3]) & 15))
        if (align and (buffer[(header_length + 9):(header_length + 11)] == '\x0bw')):
            for a in self.audio:
                if (a.id == id):
                    break
            else:
                self.audio.append(core.AudioStream())
                self.audio[(-1)]._set('id', id)
                self.audio[(-1)].codec = 8192
    else:
        pass
    ptsdts = (ord(buffer[7]) >> 6)
    if (ptsdts and (ptsdts == (ord(buffer[9]) >> 4))):
        if ((ord(buffer[9]) >> 4) != ptsdts):
            log.warning(u'WARNING:    bad    PTS/DTS,    please    contact    us')
            return (packet_length, None)
        high = ((ord(buffer[9]) & 15) >> 1)
        med = ((ord(buffer[10]) << 7) + (ord(buffer[11]) >> 1))
        low = ((ord(buffer[12]) << 7) + (ord(buffer[13]) >> 1))
        return (packet_length, 9)
    return (packet_length, None)
