// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x28(%rdi), %r12
movl 0x40(%rdi), %r14d
movl 0x18(%rsi), %ecx
movl $0xcccccccd, %eax       # imm = 0xCCCCCCCD
imulq %rcx, %rax
shrq $0x22, %rax
leal (%rax,%rax,4), %edx
movl %ecx, %eax
subl %edx, %eax
xorl %ebp, %ebp
cmpl $0x2, %eax
jb 0x1686b
cmpl $0x3, %eax
je 0x1686b
movq %rsi, %r15
cmpl $0x2, %eax
jne 0x1680c
movq %rbx, %rdi
callq 0x17146
movl %eax, %ebp
movl 0x18(%r15), %ecx
imull $0xcccccccd, %ecx, %eax # imm = 0xCCCCCCCD
addl $0xcccccccc, %eax       # imm = 0xCCCCCCCC
cmpl $0x33333332, %eax       # imm = 0x33333332
ja 0x16830
movq %rbx, %rdi
callq 0x170f0
movl %eax, %ebp
xorl %eax, %eax
subl $0x1, %ebp
cmovbl %eax, %ebp
movslq %ebp, %rax
shlq $0x4, %rax
leaq 0x6ab2(%rip), %rdx      # 0x1d2f0
movl (%rax,%rdx), %ecx
movl 0x8(%rax,%rdx), %r13d
movl 0xc(%rax,%rdx), %ebp
movb 0x4(%rax,%rdx), %al
movb %al, 0x58(%r12)
movl 0x18(%r15), %eax
movl $0xcccccccd, %edx       # imm = 0xCCCCCCCD
imulq %rax, %rdx
shrq $0x22, %rdx
leal (%rdx,%rdx,4), %edx
subl %edx, %eax
jmp 0x16870
xorl %r13d, %r13d
xorl %ecx, %ecx
movl %ecx, %edx
orl $0x100, %edx            # imm = 0x100
cmpb $0x0, 0x19(%rbx)
cmovel %ecx, %edx
addl $-0x3, %eax
movl %edx, %r15d
orl $0x100000, %r15d        # imm = 0x100000
cmpl $0x2, %eax
cmovael %edx, %r15d
movb $0x0, 0x68(%r12)
testb $0x8, %r15b
jne 0x16b05
testb $0x7, %r15b
je 0x169ed
testb $0x3, %r15b
je 0x169d7
movl %r13d, (%rsp)
movq %r14, 0x10(%rsp)
movl %ebp, 0x4(%rsp)
movq 0x8(%rbx), %rax
movq 0x88360(%rax), %rax
movl 0x448(%rax), %esi
movq %rbx, %rdi
callq 0x16b80
movq 0x8(%rbx), %rax
movq 0x88360(%rax), %rax
cmpl $0x0, 0x450(%rax)
je 0x16975
movq %rbx, %rdi
callq 0x17220
testl %eax, %eax
je 0x16975
andl $0xfff7fff6, %r15d      # imm = 0xFFF7FFF6
movq %r12, 0x8(%rsp)
leaq 0x58(%r12), %r13
xorl %r14d, %r14d
movl $0xff, %r12d
leaq 0x64ae(%rip), %rax      # 0x1cdd0
movzbl (%r14,%rax), %ebp
movzbl 0x57(%rbx,%rbp), %esi
movzbl 0x50(%rbx,%rbp), %eax
cmpb %sil, %al
cmovbl %eax, %esi
andl %r12d, %esi
movq %rbx, %rdi
callq 0x16d48
movb %al, (%r13)
movl %eax, %ecx
andl %r12d, %ecx
shll $0x8, %eax
orl %ecx, %eax
movw %ax, 0x58(%rbx,%rbp)
movw %ax, 0x60(%rbx,%rbp)
addq $0x4, %r14
incq %r13
cmpq $0x10, %r14
jne 0x1691b
orl $0x80000, %r15d         # imm = 0x80000
movq 0x8(%rsp), %r12
jmp 0x169ca
movl %r15d, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0x644c(%rip), %r15      # 0x1cdd0
leaq 0x6b85(%rip), %r13      # 0x1d510
movzbl (%r14,%r15), %ebp
movzbl 0x57(%rbx,%rbp), %eax
movzbl 0x50(%rbx,%rbp), %ecx
cmpb %al, %cl
cmovbl %ecx, %eax
movzbl %al, %esi
movq %rbx, %rdi
callq 0x16cb2
movzbl (%r14,%r13), %ecx
movb %al, 0x58(%r12,%rcx)
movb %al, 0x58(%rbx,%rbp)
incq %r14
cmpq $0x10, %r14
jne 0x1698b
movl 0x8(%rsp), %r15d
andl $-0xb, %r15d
movl 0x4(%rsp), %ebp
movq 0x10(%rsp), %r14
movl (%rsp), %r13d
leal -0x1(%r14), %eax
cmpl $0x1, %eax
ja 0x169ed
movq %rbx, %rdi
callq 0x1744e
movb %al, 0x68(%r12)
testb $0x4, %r15b
jne 0x16a43
movq %rbx, %rdi
callq 0x17272
movl %eax, %r13d
decl %r14d
cmpl $0x1, %r14d
ja 0x16a11
movq %rbx, %rdi
callq 0x17368
movl %eax, %ebp
testl %r13d, %r13d
je 0x16a40
testb $0x3, %r15b
jne 0x16a43
movq 0x8(%rbx), %rax
movq 0x88360(%rax), %rax
cmpl $0x0, 0x450(%rax)
je 0x16a43
movq %rbx, %rdi
callq 0x17220
shll $0x13, %eax
orl %eax, %r15d
jmp 0x16a43
xorl %r13d, %r13d
movl %r15d, 0x10(%r12)
movl %r13d, 0x6c(%r12)
movl %ebp, 0x70(%r12)
testl %r13d, %r13d
jne 0x16ab5
testl %ebp, %ebp
jne 0x16ab5
movl %r15d, %eax
andl $0x4, %eax
jne 0x16ab5
xorl %eax, %eax
movl %eax, 0x8c(%rbx)
movl %eax, 0x94(%rbx)
movl %eax, 0x9c(%rbx)
movl %eax, 0xa4(%rbx)
movl %eax, 0xb4(%rbx)
movl %eax, 0xbc(%rbx)
movl %eax, 0xc4(%rbx)
movl %eax, 0xcc(%rbx)
movl %eax, 0xdc(%rbx)
movl %eax, 0xe4(%rbx)
movl %eax, 0xec(%rbx)
movl %eax, 0xf4(%rbx)
movl %eax, 0x30(%rbx)
jmp 0x16b61
movq 0x8(%rbx), %rax
movq 0x88358(%rax), %rax
movl 0x18(%rax), %r14d
movq %rbx, %rdi
movl %r15d, %esi
callq 0x16dde
movq %rbx, %rdi
callq 0x173f4
movq 0x8(%rbx), %rcx
movq 0x88358(%rcx), %rdx
movl 0x18(%rdx), %edx
leal (%rdx,%rdx,2), %esi
movl $0xffffffe6, %edi       # imm = 0xFFFFFFE6
subl %esi, %edi
cmpl %edi, %eax
setl %sil
leal (%rdx,%rdx,2), %edx
addl $0x19, %edx
cmpl %edx, %eax
setg %dl
orb %sil, %dl
je 0x16b14
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl %r14d, %r14d
leal (%r14,%r14,2), %edx
addl $0x33, %edx
movl %eax, 0x30(%rbx)
addl 0x34(%rbx), %eax
leal (%r14,%r14,2), %esi
addl $0x34, %esi
movl %eax, %edi
sarl $0x1f, %edi
andl %esi, %edi
addl %eax, %edi
xorl %eax, %eax
cmpl %edx, %edi
cmovgl %esi, %eax
subl %eax, %edi
movl %edi, 0x34(%rbx)
movslq %edi, %rax
movzbl 0xa63d0(%rcx,%rax), %edx
movl %edx, 0x38(%rbx)
movzbl 0xa6410(%rcx,%rax), %eax
movl %eax, 0x3c(%rbx)
movq %rbx, %rdi
callq 0x17c50
movl 0x34(%rbx), %eax
movl %eax, 0x74(%r12)
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x17030
nop