// File: mmoanis[P]mpegts_demux/MpegTsDemux.cpp
// Function: MpegTsDemuxer::DecodePacket(Packet const&)
bool MpegTsDemuxer::DecodePacket(const Packet& packet)
{
    ++m_pnum;

    Packet::const_iterator p = packet.begin(), e = packet.end();
    PacketHeader header;
    if (!ReadHeader(p, e, header))
        return false;

    PID &id = header.id;
    //cout << "DecodePacket: PID=" << id << endl;

    // Lookup this ID in our filters
    Filters::iterator f = m_filters.find(id);
    if (f != m_filters.end()) {
        DemuxerEvents E = (*f).second;
        switch (E) {
        case DEMUXER_EVENT_NIL:
            // NULL PACKET
            //cout << "[INFO]: PKT#" << m_pnum << " Null packet." << endl;
            return true;
        case DEMUXER_EVENT_PAT:
            if (!ReadPAT(p, e))
                return false;
            break;
        case DEMUXER_EVENT_PMT:
            if (!ReadPMT(p, e))
                return false;
            break;
        case DEMUXER_EVENT_PES:
            if (!ReadPES(p, e, header, id))
                return false;
            break;
        default:
            cout << "[WARNING]: PID=" << id << " filtered but not handled." << endl;
            break;
        }
    } else {
        // Check if orphaned PES with no program.
        if (CheckPES(p, e, header)) {
            if (!ReadPES(p, e, header, id))
                return false;
        }
    }
    return true;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x90(%rax)
movq 0x50(%rsp), %rdi
callq 0x5740
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
callq 0x5750
movq 0x8(%rsp), %rdi
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rsi
leaq 0x36(%rsp), %rcx
callq 0x3110
testb $0x1, %al
jne 0x2f4f
movb $0x0, 0x67(%rsp)
jmp 0x30f9
movq 0x8(%rsp), %rdi
leaq 0x36(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x28(%rsp)
addq $0x60, %rdi
movq 0x28(%rsp), %rsi
callq 0x5770
movq 0x8(%rsp), %rdi
movq %rax, 0x20(%rsp)
addq $0x60, %rdi
callq 0x57c0
movq %rax, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x57a0
testb $0x1, %al
jne 0x2fa0
jmp 0x30a4
leaq 0x20(%rsp), %rdi
callq 0x57f0
movl 0x4(%rax), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, (%rsp)
subl $0x3, %eax
ja 0x3061
movq (%rsp), %rax
leaq 0x61fb(%rip), %rcx      # 0x91cc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, 0x67(%rsp)
jmp 0x30f9
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rsi
callq 0x3340
testb $0x1, %al
jne 0x3006
movb $0x0, 0x67(%rsp)
jmp 0x30f9
jmp 0x30a2
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rsi
callq 0x37d0
testb $0x1, %al
jne 0x302d
movb $0x0, 0x67(%rsp)
jmp 0x30f9
jmp 0x30a2
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq 0x28(%rsp), %rax
leaq 0x48(%rsp), %rsi
leaq 0x36(%rsp), %rcx
movzwl (%rax), %r8d
callq 0x3fa0
testb $0x1, %al
jne 0x305f
movb $0x0, 0x67(%rsp)
jmp 0x30f9
jmp 0x30a2
movq 0x9f68(%rip), %rdi      # 0xcfd0
leaq 0x61dc(%rip), %rsi      # 0x924b
callq 0x2140
movq %rax, %rdi
movq 0x28(%rsp), %rax
movzwl (%rax), %esi
callq 0x2200
movq %rax, %rdi
leaq 0x61cd(%rip), %rsi      # 0x925b
callq 0x2140
movq %rax, %rdi
movq 0x9f23(%rip), %rsi      # 0xcfc0
callq 0x2180
jmp 0x30f4
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rsi
leaq 0x36(%rsp), %rcx
callq 0x4420
testb $0x1, %al
jne 0x30c3
jmp 0x30f2
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq 0x28(%rsp), %rax
leaq 0x48(%rsp), %rsi
leaq 0x36(%rsp), %rcx
movzwl (%rax), %r8d
callq 0x3fa0
testb $0x1, %al
jne 0x30f0
movb $0x0, 0x67(%rsp)
jmp 0x30f9
jmp 0x30f2
jmp 0x30f4
movb $0x1, 0x67(%rsp)
movb 0x67(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)