// File: lukasz-wiecaszek[P]h264iframedecoder/picture_cabac.cpp
// Function: ymn::h264::picture_cabac::decode_mb(ymn::h264::slice_header const&)
void picture_cabac::decode_mb(const h264::slice_header& sh)
{
    uint32_t mb_type;
    int cbp_luma;
    int cbp_chroma;
    mb* curr_mb = m_context_variables.curr_mb;
    int decode_chroma = m_context_variables.chroma_array_type == 1 ||
                        m_context_variables.chroma_array_type == 2;

    if (sh.slice_type == h264::slice_type_e::B) {
        /* not implemented */
        mb_type = 0;
        cbp_luma = 0;
        cbp_chroma = 0;
    }
    else
    if (sh.slice_type == h264::slice_type_e::P ||
        sh.slice_type == h264::slice_type_e::SP) {
        /* not implemented */
        mb_type = 0;
        cbp_luma = 0;
        cbp_chroma = 0;
    }
    else {
        int i_mb_type = 0;

        if (sh.slice_type == h264::slice_type_e::I)
            i_mb_type = decode_mb_type_i_slice();

        if (sh.slice_type == h264::slice_type_e::SI) {
            i_mb_type = decode_mb_type_si_slice();
            if (i_mb_type)
                i_mb_type--;
        }

        mb_type = mb_info_i[i_mb_type].type;
        cbp_luma = mb_info_i[i_mb_type].cbp_luma;
        cbp_chroma = mb_info_i[i_mb_type].cbp_chroma;
        curr_mb->intra_luma_pred_mode.m16x16 = mb_info_i[i_mb_type].pred_mode;
    }

    if (m_context_variables.mb_field_decoding_flag)
        mb_type |= MB_TYPE_INTERLACED;

    if (sh.slice_type == h264::slice_type_e::SI ||
        sh.slice_type == h264::slice_type_e::SP)
        mb_type |= MB_TYPE_SWITCHING;

    curr_mb->intra_chroma_pred_mode = 0;

    if (MB_IS_INTRA_PCM(mb_type))
        return;  /* not implemented */

    if (MB_IS_INTRA(mb_type)) {
        if (MB_IS_INTRA_NxN(mb_type)) {
            int pred;
            int mode;

            intraNxN_pred_mode_cache_init(m_decoder.m_active_pps->constrained_intra_pred_flag);

            if (m_decoder.m_active_pps->transform_8x8_mode_flag && decode_transform_size_8x8_flag()) {
                mb_type &= ~MB_TYPE_INTRA_4x4;
                mb_type |=  MB_TYPE_8x8DCT;
                for (int i = 0; i < 4; ++i) {
                    pred = get_predicted_intra_mode(i * 4);
                    mode = get_intra8x8_pred_mode(pred);
                    curr_mb->intra_luma_pred_mode.m8x8[i] = mode;
                    mb_cache_fill_rectangle_2x2(m_context_variables.intraNxN_pred_mode_cache, mb_cache_idx[i * 4], mode);
                }
            }
            else {
                mb_type &= ~MB_TYPE_INTRA_8x8;
                for (int i = 0; i < 16; ++i) {
                    pred = get_predicted_intra_mode(i);
                    mode = get_intra4x4_pred_mode(pred);
                    curr_mb->intra_luma_pred_mode.m4x4[inverse_scanning_4x4[i]] = mode;
                    m_context_variables.intraNxN_pred_mode_cache[mb_cache_idx[i]] = mode;
                }
            }
        }
        if (decode_chroma)
            curr_mb->intra_chroma_pred_mode = decode_intra_chroma_pred_mode();
    }

    if (!MB_IS_INTRA_16x16(mb_type)) {
        cbp_luma = decode_cbp_luma();
        if (decode_chroma)
            cbp_chroma = decode_cbp_chroma();

        if (cbp_luma && m_decoder.m_active_pps->transform_8x8_mode_flag && !MB_IS_INTRA_NxN(mb_type))
            mb_type |= MB_TYPE_8x8DCT * decode_transform_size_8x8_flag();
    }

    curr_mb->type = mb_type;
    curr_mb->cbp_luma = cbp_luma;
    curr_mb->cbp_chroma = cbp_chroma;

    if (cbp_luma || cbp_chroma || MB_IS_INTRA_16x16(mb_type)) {
        const int max_qp = 51 + 6 * m_decoder.m_active_sps->bit_depth_luma_minus8;
        int qp_delta;

        non_zero_count_cache_init(mb_type);

        /* decode mb_qp_delta */
        /* The decoded value of mb_qp_delta shall be in the range of
        -( 26 + QpBdOffsetY / 2) to +( 25 + QpBdOffsetY / 2 ), inclusive. */
        qp_delta = decode_mb_qp_delta();
        if ((qp_delta < -(26 + 3 * static_cast<int>(m_decoder.m_active_sps->bit_depth_luma_minus8))) ||
            (qp_delta >  (25 + 3 * static_cast<int>(m_decoder.m_active_sps->bit_depth_luma_minus8))))
            return;

        m_context_variables.lastQPdelta = qp_delta;
        m_context_variables.QPy += qp_delta;

        if (m_context_variables.QPy < 0)
          m_context_variables.QPy += max_qp + 1;

        if (m_context_variables.QPy > max_qp)
          m_context_variables.QPy -= max_qp + 1;

        m_context_variables.QPc[0] = m_decoder.m_chroma_qp_table[0][m_context_variables.QPy];
        m_context_variables.QPc[1] = m_decoder.m_chroma_qp_table[1][m_context_variables.QPy];

        decode_residual();
    }
    else {
        mb_cache_fill_rectangle_4x4(m_context_variables.non_zero_count_cache[CC_Y],  mb_cache_idx[0], 0);
        mb_cache_fill_rectangle_4x4(m_context_variables.non_zero_count_cache[CC_Cb], mb_cache_idx[0], 0);
        mb_cache_fill_rectangle_4x4(m_context_variables.non_zero_count_cache[CC_Cr], mb_cache_idx[0], 0);
        m_context_variables.lastQPdelta = 0;
    }

    curr_mb->luma_qp = m_context_variables.QPy;
    non_zero_count_save();
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq 0x60(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x28(%rcx), %rax
movq %rax, 0x40(%rsp)
movb $0x1, %al
cmpl $0x1, 0x40(%rcx)
movb %al, 0x1f(%rsp)
je 0x1f23d
movq 0x10(%rsp), %rax
cmpl $0x2, 0x40(%rax)
sete %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x3c(%rsp)
movq 0x58(%rsp), %rax
movl 0x18(%rax), %edi
movl $0x1, %esi
callq 0x22330
testb $0x1, %al
jne 0x1f262
jmp 0x1f27f
movl $0x0, 0x54(%rsp)
movl $0x0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
jmp 0x1f39e
movq 0x58(%rsp), %rax
movl 0x18(%rax), %edi
xorl %esi, %esi
callq 0x22330
testb $0x1, %al
jne 0x1f2aa
movq 0x58(%rsp), %rax
movl 0x18(%rax), %edi
movl $0x3, %esi
callq 0x22330
testb $0x1, %al
jne 0x1f2aa
jmp 0x1f2c7
movl $0x0, 0x54(%rsp)
movl $0x0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
jmp 0x1f39c
movl $0x0, 0x38(%rsp)
movq 0x58(%rsp), %rax
movl 0x18(%rax), %edi
movl $0x2, %esi
callq 0x22330
testb $0x1, %al
jne 0x1f2e7
jmp 0x1f2f5
movq 0x10(%rsp), %rdi
callq 0x20a10
movl %eax, 0x38(%rsp)
movq 0x58(%rsp), %rax
movl 0x18(%rax), %edi
movl $0x4, %esi
callq 0x22330
testb $0x1, %al
jne 0x1f30d
jmp 0x1f32f
movq 0x10(%rsp), %rdi
callq 0x20920
movl %eax, 0x38(%rsp)
cmpl $0x0, 0x38(%rsp)
je 0x1f32d
movl 0x38(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x1f32f
movslq 0x38(%rsp), %rcx
leaq 0x99d5(%rip), %rax      # 0x28d10
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, 0x54(%rsp)
movslq 0x38(%rsp), %rcx
leaq 0x99bc(%rip), %rax      # 0x28d10
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, 0x50(%rsp)
movslq 0x38(%rsp), %rcx
leaq 0x99a2(%rip), %rax      # 0x28d10
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, 0x4c(%rsp)
movslq 0x38(%rsp), %rcx
leaq 0x9988(%rip), %rax      # 0x28d10
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movb %al, %cl
movq 0x40(%rsp), %rax
movb %cl, 0x58(%rax)
jmp 0x1f39e
movq 0x10(%rsp), %rax
testb $0x1, 0x19(%rax)
je 0x1f3b6
movl 0x54(%rsp), %eax
orl $0x100, %eax            # imm = 0x100
movl %eax, 0x54(%rsp)
movq 0x58(%rsp), %rax
movl 0x18(%rax), %edi
movl $0x4, %esi
callq 0x22330
testb $0x1, %al
jne 0x1f3e4
movq 0x58(%rsp), %rax
movl 0x18(%rax), %edi
movl $0x3, %esi
callq 0x22330
testb $0x1, %al
jne 0x1f3e4
jmp 0x1f3f1
movl 0x54(%rsp), %eax
orl $0x100000, %eax         # imm = 0x100000
movl %eax, 0x54(%rsp)
movq 0x40(%rsp), %rax
movb $0x0, 0x68(%rax)
movl 0x54(%rsp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x1f40b
jmp 0x1f805
movl 0x54(%rsp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x1f5dc
movl 0x54(%rsp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1f5bf
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rax
movq 0x88360(%rax), %rax
movl 0x448(%rax), %esi
callq 0x1f810
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x88360(%rax), %rax
cmpl $0x0, 0x450(%rax)
je 0x1f518
movq 0x10(%rsp), %rdi
callq 0x20bc0
cmpl $0x0, %eax
je 0x1f518
movl 0x54(%rsp), %eax
andl $-0x2, %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
orl $0x80000, %eax          # imm = 0x80000
movl %eax, 0x54(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $0x4, 0x2c(%rsp)
jge 0x1f513
movq 0x10(%rsp), %rdi
movl 0x2c(%rsp), %esi
shll $0x2, %esi
callq 0x1fc80
movq 0x10(%rsp), %rdi
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %esi
callq 0x1fe70
movq 0x10(%rsp), %rdi
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
movb %al, %dl
movq 0x40(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movb %dl, 0x58(%rax,%rcx)
addq $0x18, %rdi
addq $0x40, %rdi
movl 0x2c(%rsp), %eax
shll $0x2, %eax
movslq %eax, %rcx
leaq 0x99ca(%rip), %rax      # 0x28ec0
movzbl (%rax,%rcx), %esi
movl 0x30(%rsp), %eax
movzbl %al, %edx
callq 0x22420
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1f496
jmp 0x1f5bd
movl 0x54(%rsp), %eax
andl $-0x3, %eax
movl %eax, 0x54(%rsp)
movl $0x0, 0x28(%rsp)
cmpl $0x10, 0x28(%rsp)
jge 0x1f5bb
movq 0x10(%rsp), %rdi
movl 0x28(%rsp), %esi
callq 0x1fc80
movq 0x10(%rsp), %rdi
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %esi
callq 0x1fd30
movq 0x10(%rsp), %rdi
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
movb %al, %dl
movq 0x40(%rsp), %rax
movslq 0x28(%rsp), %rsi
leaq 0x993a(%rip), %rcx      # 0x28eb0
movzbl (%rcx,%rsi), %ecx
movb %dl, 0x58(%rax,%rcx)
movl 0x30(%rsp), %eax
movb %al, 0xf(%rsp)
addq $0x18, %rdi
addq $0x40, %rdi
movslq 0x28(%rsp), %rcx
leaq 0x9926(%rip), %rax      # 0x28ec0
movzbl (%rax,%rcx), %eax
movl %eax, %esi
callq 0x22380
movb 0xf(%rsp), %cl
movb %cl, (%rax)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x1f52b
jmp 0x1f5bd
jmp 0x1f5bf
cmpl $0x0, 0x3c(%rsp)
je 0x1f5da
movq 0x10(%rsp), %rdi
callq 0x210b0
movb %al, %cl
movq 0x40(%rsp), %rax
movb %cl, 0x68(%rax)
jmp 0x1f5dc
movl 0x54(%rsp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x1f64e
movq 0x10(%rsp), %rdi
callq 0x20c90
movl %eax, 0x50(%rsp)
cmpl $0x0, 0x3c(%rsp)
je 0x1f60b
movq 0x10(%rsp), %rdi
callq 0x20eb0
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x50(%rsp)
je 0x1f64c
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x88360(%rax), %rax
cmpl $0x0, 0x450(%rax)
je 0x1f64c
movl 0x54(%rsp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x1f64c
movq 0x10(%rsp), %rdi
callq 0x20bc0
shll $0x13, %eax
orl 0x54(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x1f64e
movl 0x54(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x10(%rax)
movl 0x50(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x6c(%rax)
movl 0x4c(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x70(%rax)
cmpl $0x0, 0x50(%rsp)
jne 0x1f690
cmpl $0x0, 0x4c(%rsp)
jne 0x1f690
movl 0x54(%rsp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x1f78b
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rax
movq 0x88358(%rax), %rax
imull $0x6, 0x18(%rax), %eax
addl $0x33, %eax
movl %eax, 0x24(%rsp)
movl 0x54(%rsp), %esi
callq 0x1ffb0
movq 0x10(%rsp), %rdi
callq 0x20fe0
movq 0x10(%rsp), %rcx
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
movq 0x8(%rcx), %rcx
movq 0x88358(%rcx), %rcx
imull $0x3, 0x18(%rcx), %edx
addl $0x1a, %edx
xorl %ecx, %ecx
subl %edx, %ecx
cmpl %ecx, %eax
jl 0x1f704
movq 0x10(%rsp), %rcx
movl 0x20(%rsp), %eax
movq 0x8(%rcx), %rcx
movq 0x88358(%rcx), %rcx
imull $0x3, 0x18(%rcx), %ecx
addl $0x19, %ecx
cmpl %ecx, %eax
jle 0x1f709
jmp 0x1f805
movq 0x10(%rsp), %rax
movl 0x20(%rsp), %ecx
movl %ecx, 0x30(%rax)
movl 0x20(%rsp), %ecx
addl 0x34(%rax), %ecx
movl %ecx, 0x34(%rax)
cmpl $0x0, 0x34(%rax)
jge 0x1f737
movq 0x10(%rsp), %rax
movl 0x24(%rsp), %ecx
addl $0x1, %ecx
addl 0x34(%rax), %ecx
movl %ecx, 0x34(%rax)
movq 0x10(%rsp), %rax
movl 0x34(%rax), %eax
cmpl 0x24(%rsp), %eax
jle 0x1f759
movq 0x10(%rsp), %rax
movl 0x24(%rsp), %edx
addl $0x1, %edx
movl 0x34(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x34(%rax)
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rax
movslq 0x34(%rdi), %rcx
movzbl 0xa63d0(%rax,%rcx), %eax
movl %eax, 0x38(%rdi)
movq 0x8(%rdi), %rax
movslq 0x34(%rdi), %rcx
movzbl 0xa6410(%rax,%rcx), %eax
movl %eax, 0x3c(%rdi)
callq 0x21f50
jmp 0x1f7f0
movq 0x10(%rsp), %rdi
addq $0x18, %rdi
addq $0x68, %rdi
movzbl 0x9721(%rip), %esi      # 0x28ec0
xorl %edx, %edx
callq 0x22480
movq 0x10(%rsp), %rdi
addq $0x18, %rdi
addq $0x68, %rdi
addq $0x28, %rdi
movzbl 0x9702(%rip), %esi      # 0x28ec0
xorl %edx, %edx
callq 0x22480
movq 0x10(%rsp), %rdi
addq $0x18, %rdi
addq $0x68, %rdi
addq $0x50, %rdi
movzbl 0x96e3(%rip), %esi      # 0x28ec0
xorl %edx, %edx
callq 0x22480
movq 0x10(%rsp), %rax
movl $0x0, 0x30(%rax)
movq 0x10(%rsp), %rdi
movl 0x34(%rdi), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x74(%rax)
callq 0x20640
addq $0x68, %rsp
retq
nopw (%rax,%rax)