// File: lukasz-wiecaszek[P]h264iframedecoder/picture_cabac.cpp
// Function: ymn::h264::picture_cabac::decode_residual()
void picture_cabac::decode_residual()
{
    mb* curr_mb = m_context_variables.curr_mb;
    uint32_t mb_type = curr_mb->type;
    int cbp_chroma = curr_mb->cbp_chroma;

    const uint8_t* const scan4x4 = MB_IS_INTERLACED(mb_type) ?
        field_scan_4x4 : frame_scan_4x4;
    const uint8_t* const scan8x8 = MB_IS_INTERLACED(mb_type) ?
        field_scan_8x8 : frame_scan_8x8;

    for (int i = 0; i < CC_MAX; ++i)
        memset(&m_context_variables.coeffs_ac[i], 0, sizeof(m_context_variables.coeffs_ac[i]));

    decode_residual(scan4x4, scan8x8, colour_component_e::Y);

    if (m_context_variables.chroma_array_type == 0) { /* monochrome */
        /* do nothing */
    }
    else
    if (m_context_variables.chroma_array_type == 1) { /* 4:2:0 */
        if (cbp_chroma & 3) { /* chroma DC residual present */
            memset(&m_context_variables.coeffs_dc[CC_Cb], 0, 4 * sizeof(dctcoeff));
            memset(&m_context_variables.coeffs_dc[CC_Cr], 0, 4 * sizeof(dctcoeff));

            decode_residual_dc(m_context_variables.coeffs_dc[CC_Cb],
                CAT_CHROMA_DC, MB_NZC_DC_BLOCK_IDX(CC_Cb), scan_table_chroma_dc, 4);
            decode_residual_dc(m_context_variables.coeffs_dc[CC_Cr],
                CAT_CHROMA_DC, MB_NZC_DC_BLOCK_IDX(CC_Cr), scan_table_chroma_dc, 4);
        }
        if (cbp_chroma & 2) { /* chroma AC residual present */
            for (int i4x4 = 0; i4x4 < 4; ++i4x4)
                decode_residual_ac(&m_context_variables.coeffs_ac[CC_Cb][16 * i4x4],
                    CAT_CHROMA_AC, MB_NZC_AC_BLOCK_IDX(CC_Cb, i4x4), scan4x4 + 1, 15);
            for (int i4x4 = 0; i4x4 < 4; ++i4x4)
                decode_residual_ac(&m_context_variables.coeffs_ac[CC_Cr][16 * i4x4],
                    CAT_CHROMA_AC, MB_NZC_AC_BLOCK_IDX(CC_Cr, i4x4), scan4x4 + 1, 15);
        }
        else {
            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);
        }
    }
    else
    if (m_context_variables.chroma_array_type == 2) { /* 4:2:2 */
        if (cbp_chroma & 3) { /* chroma DC residual present */
            memset(&m_context_variables.coeffs_dc[CC_Cb], 0, 8 * sizeof(dctcoeff));
            memset(&m_context_variables.coeffs_dc[CC_Cr], 0, 8 * sizeof(dctcoeff));
        }
        if (cbp_chroma & 2) { /* chroma AC residual present */
        }
        else {
            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);
        }
    }
    else { /* 4:4:4 */
        decode_residual(scan4x4, scan8x8, colour_component_e::Cb);
        decode_residual(scan4x4, scan8x8, colour_component_e::Cr);
    }
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x44(%rsp)
movq 0x48(%rsp), %rax
movl 0x70(%rax), %eax
movl %eax, 0x40(%rsp)
movl 0x44(%rsp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x21fa0
leaq 0x6f37(%rip), %rax      # 0x28ed0
movq %rax, 0x10(%rsp)
jmp 0x21fae
leaq 0x3a09(%rip), %rax      # 0x259b0
movq %rax, 0x10(%rsp)
jmp 0x21fae
movq 0x10(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x44(%rsp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x21fd4
leaq 0x6f13(%rip), %rax      # 0x28ee0
movq %rax, 0x8(%rsp)
jmp 0x21fe2
leaq 0x39e5(%rip), %rax      # 0x259c0
movq %rax, 0x8(%rsp)
jmp 0x21fe2
movq 0x8(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $0x3, 0x2c(%rsp)
jge 0x22030
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0x1a0, %rdi            # imm = 0x1A0
movslq 0x2c(%rsp), %rax
shlq $0xa, %rax
addq %rax, %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x20d0
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x21ff4
movq 0x18(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0x21a60
movq 0x18(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x22056
jmp 0x22324
movq 0x18(%rsp), %rax
cmpl $0x1, 0x40(%rax)
jne 0x22242
movl 0x40(%rsp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x22121
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0xe0, %rdi
addq $0x40, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x20d0
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0xe0, %rdi
addq $0x80, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x20d0
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
addq $0x18, %rsi
addq $0xe0, %rsi
addq $0x40, %rsi
movl $0x3, %edx
movl $0x31, %ecx
leaq 0x6e40(%rip), %r8       # 0x28f20
movl $0x4, %r9d
callq 0x218a0
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
addq $0x18, %rsi
addq $0xe0, %rsi
addq $0x80, %rsi
movl $0x3, %edx
movl $0x32, %ecx
leaq 0x6e0a(%rip), %r8       # 0x28f20
movl $0x4, %r9d
callq 0x218a0
movl 0x40(%rsp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x221ff
movl $0x0, 0x28(%rsp)
cmpl $0x4, 0x28(%rsp)
jge 0x22197
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
addq $0x18, %rsi
addq $0x1a0, %rsi            # imm = 0x1A0
addq $0x400, %rsi            # imm = 0x400
movl 0x28(%rsp), %eax
shll $0x4, %eax
cltq
shlq $0x2, %rax
addq %rax, %rsi
movl 0x28(%rsp), %ecx
addl $0x10, %ecx
movq 0x38(%rsp), %r8
addq $0x1, %r8
movl $0x4, %edx
movl $0xf, %r9d
callq 0x21940
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x22139
movl $0x0, 0x24(%rsp)
cmpl $0x4, 0x24(%rsp)
jge 0x221fd
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
addq $0x18, %rsi
addq $0x1a0, %rsi            # imm = 0x1A0
addq $0x800, %rsi            # imm = 0x800
movl 0x24(%rsp), %eax
shll $0x4, %eax
cltq
shlq $0x2, %rax
addq %rax, %rsi
movl 0x24(%rsp), %ecx
addl $0x20, %ecx
movq 0x38(%rsp), %r8
addq $0x1, %r8
movl $0x4, %edx
movl $0xf, %r9d
callq 0x21940
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x2219f
jmp 0x2223d
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0x68, %rdi
addq $0x28, %rdi
movzbl 0x6ca9(%rip), %esi      # 0x28ec0
xorl %edx, %edx
callq 0x22480
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0x68, %rdi
addq $0x50, %rdi
movzbl 0x6c8a(%rip), %esi      # 0x28ec0
xorl %edx, %edx
callq 0x22480
jmp 0x22322
movq 0x18(%rsp), %rax
cmpl $0x2, 0x40(%rax)
jne 0x222ee
movl 0x40(%rsp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x222a0
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0xe0, %rdi
addq $0x40, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x20d0
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0xe0, %rdi
addq $0x80, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x20d0
movl 0x40(%rsp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x222ae
jmp 0x222ec
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0x68, %rdi
addq $0x28, %rdi
movzbl 0x6bfa(%rip), %esi      # 0x28ec0
xorl %edx, %edx
callq 0x22480
movq 0x18(%rsp), %rdi
addq $0x18, %rdi
addq $0x68, %rdi
addq $0x50, %rdi
movzbl 0x6bdb(%rip), %esi      # 0x28ec0
xorl %edx, %edx
callq 0x22480
jmp 0x22320
movq 0x18(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl $0x1, %ecx
callq 0x21a60
movq 0x18(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl $0x2, %ecx
callq 0x21a60
jmp 0x22322
jmp 0x22324
addq $0x58, %rsp
retq
nopl (%rax)