// File: /workspace/github_cpp_makefile_star2_O2/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), %r13
movl 0x40(%rdi), %ebp
movl 0x18(%rsi), %ecx
xorl %r14d, %r14d
pushq $0x5
popq %r12
movl %ecx, %eax
xorl %edx, %edx
divl %r12d
cmpl $0x2, %edx
jb 0x11daf
cmpl $0x3, %edx
je 0x11daf
movq %rsi, %r15
cmpl $0x2, %edx
jne 0x11d5e
movq %rbx, %rdi
callq 0x1260e
movl %eax, %r14d
movl 0x18(%r15), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r12d
cmpl $0x4, %edx
jne 0x11d82
movq %rbx, %rdi
callq 0x125b8
movl %eax, %r14d
subl $0x1, %r14d
movl $0x0, %eax
cmovbl %eax, %r14d
movslq %r14d, %rax
shlq $0x4, %rax
leaq 0x75e0(%rip), %rdx      # 0x19370
movl (%rax,%rdx), %ecx
movl 0x8(%rax,%rdx), %esi
movl 0xc(%rax,%rdx), %r14d
movb 0x4(%rax,%rdx), %al
movb %al, 0x58(%r13)
movl 0x18(%r15), %eax
xorl %edx, %edx
divl %r12d
jmp 0x11db3
xorl %esi, %esi
xorl %ecx, %ecx
movl %ecx, %eax
orl $0x100, %eax            # imm = 0x100
cmpb $0x0, 0x19(%rbx)
cmovel %ecx, %eax
addl $-0x3, %edx
movl %eax, %r15d
orl $0x100000, %r15d        # imm = 0x100000
cmpl $0x2, %edx
cmovael %eax, %r15d
movb $0x0, 0x68(%r13)
testb $0x8, %r15b
jne 0x12031
testb $0x7, %r15b
je 0x11f10
testb $0x3, %r15b
je 0x11ef6
movl %esi, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
movl %r14d, 0xc(%rsp)
movq 0x8(%rbx), %rax
movq 0x88360(%rax), %rax
movl 0x448(%rax), %esi
movq %rbx, %rdi
callq 0x120a2
movq 0x8(%rbx), %rax
movq 0x88360(%rax), %rax
cmpl $0x0, 0x450(%rax)
je 0x11e97
movq %rbx, %rdi
callq 0x126d2
testl %eax, %eax
je 0x11e97
andl $0xfff7fff6, %r15d      # imm = 0xFFF7FFF6
leaq 0x58(%r13), %r14
xorl %r12d, %r12d
leaq 0x6ffc(%rip), %rbp      # 0x18e50
cmpq $0x10, %r12
je 0x11ee1
movq %rbx, %rdi
movl %r12d, %esi
callq 0x121b0
movq %rbx, %rdi
movl %eax, %esi
callq 0x12252
movb %al, (%r14)
movzbl (%r12,%rbp), %ecx
movzbl %al, %eax
imull $0x101, %eax, %eax      # imm = 0x101
movw %ax, 0x58(%rbx,%rcx)
movw %ax, 0x60(%rbx,%rcx)
addq $0x4, %r12
incq %r14
jmp 0x11e54
xorl %r12d, %r12d
leaq 0x76ef(%rip), %r14      # 0x19590
leaq 0x6fa8(%rip), %rbp      # 0x18e50
cmpq $0x10, %r12
je 0x11edb
movq %rbx, %rdi
movl %r12d, %esi
callq 0x121b0
movq %rbx, %rdi
movl %eax, %esi
callq 0x121d2
movzbl (%r12,%r14), %ecx
movb %al, 0x58(%r13,%rcx)
movzbl (%r12,%rbp), %ecx
movb %al, 0x58(%rbx,%rcx)
incq %r12
jmp 0x11ea8
andl $-0xb, %r15d
jmp 0x11ee8
orl $0x80000, %r15d         # imm = 0x80000
movl 0xc(%rsp), %r14d
movq 0x10(%rsp), %rbp
movl 0x8(%rsp), %esi
leal -0x1(%rbp), %eax
cmpl $0x1, %eax
ja 0x11f10
movq %rbx, %rdi
movl %esi, %r12d
callq 0x128e8
movl %r12d, %esi
movb %al, 0x68(%r13)
testb $0x4, %r15b
jne 0x11f6a
movq %rbx, %rdi
callq 0x12724
movl %eax, %esi
decl %ebp
cmpl $0x1, %ebp
ja 0x11f36
movq %rbx, %rdi
movl %esi, %ebp
callq 0x12810
movl %ebp, %esi
movl %eax, %r14d
testl %esi, %esi
je 0x11f68
testb $0x3, %r15b
jne 0x11f6a
movq 0x8(%rbx), %rax
movq 0x88360(%rax), %rax
cmpl $0x0, 0x450(%rax)
je 0x11f6a
movq %rbx, %rdi
movl %esi, %ebp
callq 0x126d2
movl %ebp, %esi
shll $0x13, %eax
orl %eax, %r15d
jmp 0x11f6a
xorl %esi, %esi
movl %r15d, 0x10(%r13)
movl %esi, 0x6c(%r13)
movl %r14d, 0x70(%r13)
testl %esi, %esi
jne 0x11fe4
testl %r14d, %r14d
jne 0x11fe4
movl %r15d, %eax
andl $0x4, %eax
jne 0x11fe4
andl $0x0, 0x8c(%rbx)
andl $0x0, 0x94(%rbx)
andl $0x0, 0x9c(%rbx)
andl $0x0, 0xa4(%rbx)
andl $0x0, 0xb4(%rbx)
andl $0x0, 0xbc(%rbx)
andl $0x0, 0xc4(%rbx)
andl $0x0, 0xcc(%rbx)
andl $0x0, 0xdc(%rbx)
andl $0x0, 0xe4(%rbx)
andl $0x0, 0xec(%rbx)
andl $0x0, 0xf4(%rbx)
andl $0x0, 0x30(%rbx)
jmp 0x12085
movq 0x8(%rbx), %rax
movq 0x88358(%rax), %rax
movl 0x18(%rax), %ebp
movq %rbx, %rdi
movl %r15d, %esi
callq 0x122d2
movq %rbx, %rdi
callq 0x12894
movq 0x8(%rbx), %rcx
movq 0x88358(%rcx), %rdx
movl 0x18(%rdx), %edx
leal (%rdx,%rdx,2), %esi
pushq $-0x1a
popq %rdi
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 0x12040
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
imull $0x6, %ebp, %edx
leal 0x33(%rdx), %esi
movl %eax, 0x30(%rbx)
addl 0x34(%rbx), %eax
addl $0x34, %edx
movl %eax, %edi
sarl $0x1f, %edi
andl %edx, %edi
addl %eax, %edi
xorl %eax, %eax
cmpl %esi, %edi
cmovgl %edx, %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 0x12fe6
movl 0x34(%rbx), %eax
movl %eax, 0x74(%r13)
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x124f8