// File: /workspace/github_cpp_makefile_star2_O3/mmoanis[P]mpegts_demux/MpegTsDemux.cpp
// Function: MpegTsDemuxer::ReadPMT(unsigned char const*&, unsigned char const*)
bool MpegTsDemuxer::ReadPMT(Packet::const_iterator &p, Packet::const_iterator e)
{
    /// FIXME: Similar to ReadPAT, can it be grouped together.

    const uint8_t pointer = *p++;
    p += pointer; // Jump to table at pointer offset

    bool done = false;

    // Tables can be repeated.
    while (p < e) {
        if (p[0] == MPEGTS_STUFFING_BYTE) {
            if (!done) {
                cerr << "[ERROR]: PKT#" << m_pnum << " No data in PMT table." << endl;
                return false;
            }
            return true;
        }
        const uint8_t id = *p++;
        if (id == MPEGTS_TABLE_NIL) {
            if (!done) {
                cerr << "[ERROR]: PKT#" << m_pnum <<" PMT packet has no associated PSI." << endl;
                return false;
            }
            return true;
        } else if (id != MPEGTS_TABLE_PMS) {
            // can happen! Just ignore.
            cout << "[WARNING]: PKT#" << m_pnum << "Expected PMT SECTION=" << MPEGTS_TABLE_PMS << " found " << id << endl;
            return true;
        }

        const bool ssi = static_cast<bool> (*p&0x80); // PAT/PMT/CAT == 1
        if (!ssi) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PMT section syntax indicator not set." << endl;
            return false;
        }

        const bool pb = static_cast<bool> (*p&0x40); // PAT/PMT/CAT == 0
        if (pb) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PMT private bit is set." << endl;
            return false;
        }

        if (((*p&0x30)>>4) != 0x03 ) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PMT PSI reserved bits not set." << endl;
            return false;
        }

        if (p + 1 >= e) {
            cerr << "[ERROR]: PKT#" << m_pnum << " Not enough data in PMT table." << endl;
            return false;
        }
        const uint16_t slength = uint16_t((p[0]&0x03)<<10) | uint16_t(p[1]);
        p += 2;
        if (p + slength >= e) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PMT bad section length." << endl;
            return false;
        }

        PacketSection section;
        if (!ReadSection(p, e, section))
            return false;

        // Get the program this PMT referring to
        Programs::const_iterator prog = m_programs.find(section.id);
        if (prog == m_programs.end()) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PMT references non existing PROGRAM=" << section.id << endl;
            return false;
        }

        if (((*p&0xE0)>>5) != 0x07) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PMT reserved bits not set." << endl;
            return false;
        }

        if (p + 1 >= e) {
            cerr << "[ERROR]: PKT#" << m_pnum << " Not enough data in PMT table." << endl;
            return false;
        }
        //PID pcrid = uint16_t((p[0]&0x1F)<<8) | uint16_t(p[1]); // clock frequency
        p += 2;

        // expect PES packet with clock frequency
        //m_filters.insert(make_pair(pcrid, DEMUXER_EVENT_PCR));

        if (((*p&0xF0)>>4) != 0x0F) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PMT reserved bits not set." << endl;
            return false;
        }

        // program descriptors
        if (p + 1 >= e) {
            cerr << "[ERROR]: PKT#" << m_pnum << " Not enough data in PMT table." << endl;
            return false;
        }
        const uint16_t pinfol = uint16_t((p[0]&0x03)<<8) | uint16_t(p[1]);
        p += 2;

        if (p + pinfol >= e) {
            cerr << "[ERROR]: PKT#" << m_pnum <<" PMT bad program info length." << endl;
            return false;
        }
        p += pinfol;

        // read data stream info
        if (!ReadESD(p, e, (*prog).second))
            return false;

        // End of the section
        //uint32_t crc = uint32_t(p[0]<<24) | uint32_t(p[1]<<16) | uint32_t(p[2]<<8) | uint32_t(p[3]);
        p += 4;
        //cout << "CRC: " << hex << crc << dec << endl;
        done = true;
    }

    return done;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq (%rsi), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rsi)
movzbl (%rax), %ecx
addq %rax, %rcx
incq %rcx
movq %rcx, (%rsi)
cmpq %rdx, %rcx
jae 0x36cd
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8(%rdi), %rbp
xorl %eax, %eax
leaq 0x2(%rsp), %r12
cmpb $-0x1, (%rcx)
je 0x343a
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movzbl (%rcx), %r13d
cmpl $0x2, %r13d
jne 0x3369
movzbl (%rdx), %eax
testb %al, %al
jns 0x352d
testb $0x40, %al
jne 0x356b
movl %eax, %edx
notl %edx
testb $0x30, %dl
jne 0x35a4
leaq 0x2(%rcx), %rdx
cmpq %r14, %rdx
jae 0x35df
andl $0x3, %eax
shll $0xa, %eax
movzbl 0x2(%rcx), %edx
orq %rax, %rdx
leaq 0x3(%rcx), %rax
movq %rax, (%r15)
leaq (%rcx,%rdx), %rax
addq $0x3, %rax
cmpq %r14, %rax
jae 0x361a
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x3a08
testb %al, %al
je 0x36cd
movq 0x10(%rbx), %rax
movzwl 0x2(%rsp), %r13d
testq %rax, %rax
je 0x3312
movq %rbp, %rcx
xorl %edx, %edx
cmpw %r13w, 0x20(%rax)
setb %dl
cmovaeq %rax, %rcx
movq 0x10(%rax,%rdx,8), %rax
testq %rax, %rax
jne 0x324d
cmpq %rbp, %rcx
je 0x3312
cmpw 0x20(%rcx), %r13w
jb 0x3312
movq (%r15), %rax
cmpb $-0x21, (%rax)
jbe 0x33be
leaq 0x1(%rax), %rdx
cmpq %r14, %rdx
jae 0x33fc
leaq 0x2(%rax), %rdx
movq %rdx, (%r15)
movzbl 0x2(%rax), %edx
cmpq $0xef, %rdx
jbe 0x33be
leaq 0x3(%rax), %rsi
cmpq %r14, %rsi
jae 0x33fc
andl $0x3, %edx
shll $0x8, %edx
movzbl 0x3(%rax), %esi
orq %rdx, %rsi
leaq 0x4(%rax), %rdx
movq %rdx, (%r15)
addq %rsi, %rax
addq $0x4, %rax
cmpq %r14, %rax
jae 0x3684
movq %rax, (%r15)
addq $0x22, %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3e9e
testb %al, %al
je 0x36cd
movq (%r15), %rcx
addq $0x4, %rcx
movq %rcx, (%r15)
movb $0x1, %al
cmpq %r14, %rcx
jb 0x31ae
jmp 0x36cf
movq 0x4cd7(%rip), %r14      # 0x7ff0
leaq 0x1f52(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x2125(%rip), %rsi      # 0x546b
movl $0x25, %edx
movq %rax, %rdi
callq 0x2160
movq %rbx, %rdi
movq %r13, %rsi
callq 0x20b0
movq %rax, %rbx
movq (%rax), %rax
jmp 0x365e
cmpl $0xff, %r13d
jne 0x3482
testb $0x1, %al
movb $0x1, %al
jne 0x36cf
movq 0x4c69(%rip), %r14      # 0x7ff0
leaq 0x1ee4(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1fd6(%rip), %rsi      # 0x538a
movl $0x22, %edx
jmp 0x3653
movq 0x4c2b(%rip), %r14      # 0x7ff0
leaq 0x1ea6(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x209f(%rip), %rsi      # 0x5491
movl $0x1b, %edx
jmp 0x36bd
movq 0x4bed(%rip), %r14      # 0x7ff0
leaq 0x1e68(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x2003(%rip), %rsi      # 0x5433
movl $0x1e, %edx
jmp 0x36bd
testb $0x1, %al
movb $0x1, %al
jne 0x36cf
movq 0x4ba5(%rip), %r14      # 0x7ff0
leaq 0x1e20(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1efb(%rip), %rsi      # 0x5373
movl $0x16, %edx
jmp 0x3653
movq 0x4b47(%rip), %r14      # 0x7fd0
leaq 0x1f1d(%rip), %rsi      # 0x53ad
movl $0xf, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1f07(%rip), %rsi      # 0x53bd
movl $0x15, %edx
movq %rax, %rdi
callq 0x2160
movq %rbx, %rdi
movl $0x2, %esi
callq 0x21e0
movq %rax, %rbx
leaq 0x1ce6(%rip), %rsi      # 0x51c0
movl $0x7, %edx
movq %rax, %rdi
callq 0x2160
leaq 0x1(%rsp), %rsi
movb %r13b, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20c0
movb $0x1, %al
jmp 0x36cf
movq 0x4abc(%rip), %r14      # 0x7ff0
leaq 0x1d37(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1e72(%rip), %rsi      # 0x53d3
movl $0x26, %edx
jmp 0x3653
movq 0x4a7e(%rip), %r14      # 0x7ff0
leaq 0x1cf9(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1e5b(%rip), %rsi      # 0x53fa
jmp 0x364e
movq 0x4a45(%rip), %r14      # 0x7ff0
leaq 0x1cc0(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1e3b(%rip), %rsi      # 0x5413
movl $0x1f, %edx
jmp 0x3653
movq 0x4a0a(%rip), %r14      # 0x7ff0
leaq 0x1c85(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1e20(%rip), %rsi      # 0x5433
movl $0x1e, %edx
jmp 0x3653
movq 0x49cf(%rip), %r14      # 0x7ff0
leaq 0x1c4a(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1e04(%rip), %rsi      # 0x5452
movl $0x18, %edx
movq %rax, %rdi
callq 0x2160
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20c0
jmp 0x36cd
movq 0x4965(%rip), %r14      # 0x7ff0
leaq 0x1be0(%rip), %rsi      # 0x5272
movl $0xd, %edx
movq %r14, %rdi
callq 0x2160
movq 0x90(%rbx), %rsi
movq %r14, %rdi
callq 0x20b0
movq %rax, %rbx
leaq 0x1df5(%rip), %rsi      # 0x54ad
movl $0x1d, %edx
movq %rax, %rdi
callq 0x2160
movq %rbx, %rdi
callq 0x2090
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq