// File: /workspace/github_cpp_makefile_star2_O2/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 %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rsi)
movzbl (%rax), %ecx
addq %rax, %rcx
incq %rcx
movq %rcx, (%rsi)
leaq 0x8(%rdi), %r13
xorl %eax, %eax
leaq 0x2(%rsp), %r12
cmpq %r14, %rcx
jae 0x31aa
cmpb $-0x1, (%rcx)
je 0x2fdc
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movzbl (%rcx), %ebp
cmpl $0x2, %ebp
jne 0x2f6a
movzbl (%rdx), %eax
testb %al, %al
jns 0x307a
testb $0x40, %al
jne 0x30a8
movl %eax, %edx
notl %edx
testb $0x30, %dl
jne 0x30d6
leaq 0x2(%rcx), %rdx
cmpq %r14, %rdx
jae 0x2f3c
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 0x3104
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x342a
testb %al, %al
je 0x31a8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3e6c
cmpq %r13, %rax
je 0x312f
movq (%r15), %rcx
cmpb $-0x21, (%rcx)
jbe 0x2fae
leaq 0x1(%rcx), %rdx
cmpq %r14, %rdx
jae 0x2f3c
leaq 0x2(%rcx), %rdx
movq %rdx, (%r15)
movzbl 0x2(%rcx), %edx
cmpq $0xef, %rdx
jbe 0x2fae
leaq 0x3(%rcx), %rsi
cmpq %r14, %rsi
jae 0x2f3c
andl $0x3, %edx
shll $0x8, %edx
movzbl 0x3(%rcx), %esi
orq %rdx, %rsi
leaq 0x4(%rcx), %rdx
movq %rdx, (%r15)
addq %rsi, %rcx
addq $0x4, %rcx
cmpq %r14, %rcx
jae 0x316f
movq %rcx, (%r15)
addq $0x22, %rax
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rax, %rcx
callq 0x376a
testb %al, %al
je 0x31a8
movq (%r15), %rcx
addq $0x4, %rcx
movq %rcx, (%r15)
movb $0x1, %al
jmp 0x2e0d
movq 0x50ad(%rip), %rdi      # 0x7ff0
leaq 0x2328(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x24ce(%rip), %rsi      # 0x5433
jmp 0x3198
cmpl $0xff, %ebp
jne 0x3014
testb $0x1, %al
movb $0x1, %al
jne 0x31aa
movq 0x5069(%rip), %rdi      # 0x7ff0
leaq 0x22e4(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x23e1(%rip), %rsi      # 0x538a
jmp 0x3198
movq 0x503b(%rip), %rdi      # 0x7ff0
leaq 0x22b6(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x24ba(%rip), %rsi      # 0x5491
jmp 0x3198
testb $0x1, %al
movb $0x1, %al
jne 0x31aa
movq 0x5003(%rip), %rdi      # 0x7ff0
leaq 0x227e(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2364(%rip), %rsi      # 0x5373
jmp 0x3198
movq 0x4fb5(%rip), %rdi      # 0x7fd0
leaq 0x238b(%rip), %rsi      # 0x53ad
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2380(%rip), %rsi      # 0x53bd
movq %rax, %rdi
callq 0x2110
pushq $0x2
popq %rsi
movq %rax, %rdi
callq 0x21e0
leaq 0x2169(%rip), %rsi      # 0x51c0
movq %rax, %rdi
callq 0x2110
movsbl %bpl, %esi
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2080
movb $0x1, %al
jmp 0x31aa
movq 0x4f6f(%rip), %rdi      # 0x7ff0
leaq 0x21ea(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2330(%rip), %rsi      # 0x53d3
jmp 0x3198
movq 0x4f41(%rip), %rdi      # 0x7ff0
leaq 0x21bc(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2329(%rip), %rsi      # 0x53fa
jmp 0x3198
movq 0x4f13(%rip), %rdi      # 0x7ff0
leaq 0x218e(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2314(%rip), %rsi      # 0x5413
jmp 0x3198
movq 0x4ee5(%rip), %rdi      # 0x7ff0
leaq 0x2160(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2325(%rip), %rsi      # 0x5452
jmp 0x3198
movq 0x4eba(%rip), %rdi      # 0x7ff0
leaq 0x2135(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2313(%rip), %rsi      # 0x546b
movq %rax, %rdi
callq 0x2110
movzwl 0x2(%rsp), %esi
movq %rax, %rdi
callq 0x21d0
jmp 0x31a0
movq 0x4e7a(%rip), %rdi      # 0x7ff0
leaq 0x20f5(%rip), %rsi      # 0x5272
callq 0x2110
movq 0x90(%rbx), %rsi
movq %rax, %rdi
callq 0x20a0
leaq 0x2315(%rip), %rsi      # 0x54ad
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2080
xorl %eax, %eax
andb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop