// File: lukasz-wiecaszek[P]h264iframedecoder/picture_cabac.cpp
// Function: ymn::h264::picture_cabac::decode_residual(unsigned char const*, unsigned char const*, ymn::colour_component_e)
void picture_cabac::decode_residual(const uint8_t* scan4x4,
                                    const uint8_t* scan8x8,
                                    colour_component_e cc)
{
    mb* curr_mb = m_context_variables.curr_mb;
    uint32_t mb_type = curr_mb->type;
    int cbp_luma = curr_mb->cbp_luma;

    static const enum ctx_block_cat_e ctx_cat[CC_MAX][4] =
    {
        {CAT_16x16_DC_Y,  CAT_16x16_AC_Y,  CAT_4x4_Y,  CAT_8x8_Y},  // Y
        {CAT_16x16_DC_Cb, CAT_16x16_AC_Cb, CAT_4x4_Cb, CAT_8x8_Cb}, // Cb
        {CAT_16x16_DC_Cr, CAT_16x16_AC_Cr, CAT_4x4_Cr, CAT_8x8_Cr}  // Cr
    };

    if (MB_IS_INTRA_16x16(mb_type)) {
        memset(&m_context_variables.coeffs_dc[to_int(cc)], 0, 16 * sizeof(dctcoeff));
        decode_residual_dc(m_context_variables.coeffs_dc[to_int(cc)],
            ctx_cat[to_int(cc)][0], MB_NZC_DC_BLOCK_IDX(to_int(cc)), scan4x4, 16);

        if (cbp_luma & 0x0F)
            for (int i4x4 = 0; i4x4 < 16; ++i4x4)
                decode_residual_ac(&m_context_variables.coeffs_ac[to_int(cc)][16 * i4x4],
                    ctx_cat[to_int(cc)][1], MB_NZC_AC_BLOCK_IDX(to_int(cc), i4x4), scan4x4 + 1, 15);
        else
            mb_cache_fill_rectangle_4x4(m_context_variables.non_zero_count_cache[to_int(cc)], mb_cache_idx[0], 0);
    }
    else {
        for (int i8x8 = 0; i8x8 < 4; ++i8x8) {
            if (cbp_luma & (1U << i8x8)) {
                if (!MB_IS_8x8DCT(mb_type)) {
                    for (int i4x4 = 0; i4x4 < 4; ++i4x4) {
                        const int index = i8x8 * 4 + i4x4;
                        decode_residual_ac(&m_context_variables.coeffs_ac[to_int(cc)][16 * index],
                            ctx_cat[to_int(cc)][2], MB_NZC_AC_BLOCK_IDX(to_int(cc), index), scan4x4, 16);
                    }
                }
                else {
                    const int index = i8x8 * 4;
                    decode_residual_ac(&m_context_variables.coeffs_ac[to_int(cc)][16 * index],
                        ctx_cat[to_int(cc)][3], MB_NZC_AC_BLOCK_IDX(to_int(cc), index), scan8x8, 64);
                }
            }
            else
                mb_cache_fill_rectangle_2x2(m_context_variables.non_zero_count_cache[to_int(cc)], mb_cache_idx[4 * i8x8], 0);
        }
    }
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movq %rdx, 0xb0(%rsp)
movl %ecx, 0xac(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x28(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x9c(%rsp)
movq 0xa0(%rsp), %rax
movl 0x6c(%rax), %eax
movl %eax, 0x98(%rsp)
movl 0x9c(%rsp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x21cc9
movq 0x78(%rsp), %rax
addq $0x18, %rax
addq $0xe0, %rax
movq %rax, 0x58(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movq 0x58(%rsp), %rdi
cltq
shlq $0x6, %rax
addq %rax, %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x20d0
movq 0x78(%rsp), %rax
addq $0x18, %rax
addq $0xe0, %rax
movq %rax, 0x60(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movl %eax, %ecx
movq 0x60(%rsp), %rax
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movslq %eax, %rcx
leaq 0x7184(%rip), %rax      # 0x28ce0
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, 0x74(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movq 0x78(%rsp), %rdi
movq 0x68(%rsp), %rsi
movl 0x74(%rsp), %edx
movl %eax, %ecx
addl $0x30, %ecx
movq 0xb8(%rsp), %r8
movl $0x10, %r9d
callq 0x218a0
movl 0x98(%rsp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x21c8a
movl $0x0, 0x94(%rsp)
cmpl $0x10, 0x94(%rsp)
jge 0x21c88
movq 0x78(%rsp), %rax
addq $0x18, %rax
addq $0x1a0, %rax            # imm = 0x1A0
movq %rax, 0x40(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movl %eax, %ecx
movq 0x40(%rsp), %rax
movslq %ecx, %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movl 0x94(%rsp), %ecx
shll $0x4, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movslq %eax, %rcx
leaq 0x70b9(%rip), %rax      # 0x28ce0
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, 0x54(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movq 0x78(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl 0x54(%rsp), %edx
movl %eax, %ecx
shll $0x4, %ecx
addl 0x94(%rsp), %ecx
movq 0xb8(%rsp), %r8
addq $0x1, %r8
movl $0xf, %r9d
callq 0x21940
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x21bb9
jmp 0x21cc4
movq 0x78(%rsp), %rax
addq $0x18, %rax
addq $0x68, %rax
movq %rax, 0x38(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movq 0x38(%rsp), %rdi
cltq
imulq $0x28, %rax, %rax
addq %rax, %rdi
movzbl 0x7203(%rip), %esi      # 0x28ec0
xorl %edx, %edx
callq 0x22480
jmp 0x21f29
movl $0x0, 0x90(%rsp)
cmpl $0x4, 0x90(%rsp)
jge 0x21f27
movl 0x98(%rsp), %eax
movl 0x90(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x21ec6
movl 0x9c(%rsp), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
jne 0x21e0c
movl $0x0, 0x8c(%rsp)
cmpl $0x4, 0x8c(%rsp)
jge 0x21e07
movq 0x78(%rsp), %rax
movl 0x90(%rsp), %ecx
shll $0x2, %ecx
addl 0x8c(%rsp), %ecx
movl %ecx, 0x88(%rsp)
addq $0x18, %rax
addq $0x1a0, %rax            # imm = 0x1A0
movq %rax, 0x20(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movl %eax, %ecx
movq 0x20(%rsp), %rax
movslq %ecx, %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movl 0x88(%rsp), %ecx
shll $0x4, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movslq %eax, %rcx
leaq 0x6f36(%rip), %rax      # 0x28ce0
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, 0x34(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movq 0x78(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0x34(%rsp), %edx
movl %eax, %ecx
shll $0x4, %ecx
addl 0x88(%rsp), %ecx
movq 0xb8(%rsp), %r8
movl $0x10, %r9d
callq 0x21940
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x21d24
jmp 0x21ec4
movq 0x78(%rsp), %rax
movl 0x90(%rsp), %ecx
shll $0x2, %ecx
movl %ecx, 0x84(%rsp)
addq $0x18, %rax
addq $0x1a0, %rax            # imm = 0x1A0
movq %rax, 0x8(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movl %eax, %ecx
movq 0x8(%rsp), %rax
movslq %ecx, %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movl 0x84(%rsp), %ecx
shll $0x4, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movslq %eax, %rcx
leaq 0x6e63(%rip), %rax      # 0x28ce0
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, 0x1c(%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movq 0x78(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl 0x1c(%rsp), %edx
movl %eax, %ecx
shll $0x4, %ecx
addl 0x84(%rsp), %ecx
movq 0xb0(%rsp), %r8
movl $0x40, %r9d
callq 0x21940
jmp 0x21f0f
movq 0x78(%rsp), %rax
addq $0x18, %rax
addq $0x68, %rax
movq %rax, (%rsp)
movl 0xac(%rsp), %edi
callq 0x21f40
movq (%rsp), %rdi
cltq
imulq $0x28, %rax, %rax
addq %rax, %rdi
movl 0x90(%rsp), %eax
shll $0x2, %eax
movslq %eax, %rcx
leaq 0x6fbc(%rip), %rax      # 0x28ec0
movzbl (%rax,%rcx), %esi
xorl %edx, %edx
callq 0x22420
jmp 0x21f11
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x21cd4
jmp 0x21f29
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)