// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, %r15
movq %rdi, %rbx
movq 0x28(%rdi), %r12
movl 0x40(%rdi), %r14d
movl 0x18(%rsi), %eax
movl $0xcccccccd, %r13d      # imm = 0xCCCCCCCD
movq %rax, %rcx
imulq %r13, %rcx
shrq $0x22, %rcx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
xorl %ebp, %ebp
cmpl $0x2, %eax
jb 0x16cb4
cmpl $0x3, %eax
je 0x16cb4
cmpl $0x2, %eax
jne 0x16c6a
movq %rbx, %rdi
callq 0x1757e
movl %eax, %ebp
imull $0xcccccccd, 0x18(%r15), %eax # imm = 0xCCCCCCCD
addl $0xcccccccc, %eax       # imm = 0xCCCCCCCC
cmpl $0x33333332, %eax       # imm = 0x33333332
ja 0x16c90
movq %rbx, %rdi
callq 0x17528
movl %eax, %ebp
xorl %eax, %eax
subl $0x1, %ebp
cmovbl %eax, %ebp
movslq %ebp, %rcx
shlq $0x4, %rcx
leaq 0x7662(%rip), %rdx      # 0x1e300
movl (%rcx,%rdx), %eax
movl 0x8(%rcx,%rdx), %esi
movl 0xc(%rcx,%rdx), %ebp
movb 0x4(%rcx,%rdx), %cl
movb %cl, 0x58(%r12)
jmp 0x16cb8
xorl %esi, %esi
xorl %eax, %eax
movl %eax, %ecx
orl $0x100, %ecx            # imm = 0x100
cmpb $0x0, 0x19(%rbx)
cmovel %eax, %ecx
movl 0x18(%r15), %eax
imulq %rax, %r13
shrq $0x22, %r13
leal (%r13,%r13,4), %edx
subl %edx, %eax
addl $-0x3, %eax
movl %ecx, %r13d
orl $0x100000, %r13d        # imm = 0x100000
cmpl $0x2, %eax
cmovael %ecx, %r13d
movb $0x0, 0x68(%r12)
testb $0x8, %r13b
jne 0x16f70
testb $0x7, %r13b
je 0x16e52
testb $0x3, %r13b
je 0x16e36
movl %esi, (%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 0x16fea
movq 0x8(%rbx), %rax
movq 0x88360(%rax), %rax
cmpl $0x0, 0x450(%rax)
je 0x16dd5
movq %rbx, %rdi
callq 0x17658
testl %eax, %eax
je 0x16dd5
andl $0xfff7fff6, %r13d      # imm = 0xFFF7FFF6
movl %r13d, %r15d
movq %r12, 0x8(%rsp)
leaq 0x58(%r12), %r13
xorl %r14d, %r14d
movl $0xff, %r12d
leaq 0x7061(%rip), %rax      # 0x1dde0
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 0x171be
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 0x16d78
movl %r15d, %r13d
orl $0x80000, %r13d         # imm = 0x80000
movq 0x8(%rsp), %r12
jmp 0x16e2a
movl %r13d, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0x6ffc(%rip), %r15      # 0x1dde0
leaq 0x7735(%rip), %r13      # 0x1e520
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 0x17128
movzbl (%r14,%r13), %ecx
movb %al, 0x58(%r12,%rcx)
movb %al, 0x58(%rbx,%rbp)
incq %r14
cmpq $0x10, %r14
jne 0x16deb
movl 0x8(%rsp), %r13d
andl $-0xb, %r13d
movl 0x4(%rsp), %ebp
movq 0x10(%rsp), %r14
movl (%rsp), %esi
leal -0x1(%r14), %eax
cmpl $0x1, %eax
ja 0x16e52
movq %rbx, %rdi
movl %esi, %r15d
callq 0x17874
movl %r15d, %esi
movb %al, 0x68(%r12)
testb $0x4, %r13b
jne 0x16eaf
movq %rbx, %rdi
callq 0x176aa
movl %eax, %esi
decl %r14d
cmpl $0x1, %r14d
ja 0x16e79
movq %rbx, %rdi
movl %esi, %ebp
callq 0x17796
movl %ebp, %esi
movl %eax, %ebp
testl %esi, %esi
je 0x16ead
testb $0x3, %r13b
jne 0x16eaf
movq 0x8(%rbx), %rax
movq 0x88360(%rax), %rax
cmpl $0x0, 0x450(%rax)
je 0x16eaf
movq %rbx, %rdi
movl %esi, %r14d
callq 0x17658
movl %r14d, %esi
shll $0x13, %eax
orl %eax, %r13d
jmp 0x16eaf
xorl %esi, %esi
movl %r13d, 0x10(%r12)
movl %esi, 0x6c(%r12)
movl %ebp, 0x70(%r12)
testl %esi, %esi
jne 0x16f20
testl %ebp, %ebp
jne 0x16f20
movl %r13d, %eax
andl $0x4, %eax
jne 0x16f20
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 0x16fcc
movq 0x8(%rbx), %rax
movq 0x88358(%rax), %rax
movl 0x18(%rax), %r14d
movq %rbx, %rdi
movl %r13d, %esi
callq 0x17254
movq %rbx, %rdi
callq 0x1781a
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 0x16f7f
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 0x18058
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 0x17468