// File: lukasz-wiecaszek[P]h264iframedecoder/h264_decoder.cpp
// Function: ymn::h264_decoder::init_dequant8x8_coeff_table()
void h264_decoder::init_dequant8x8_coeff_table()
{
    int i, j;
    const int depth = m_active_sps->bit_depth_luma_minus8;
    const int max_qp = 51 + 6 * depth;

    static const uint8_t v[6][6] =
    {
        {20, 18, 32, 19, 25, 24},
        {22, 19, 35, 21, 28, 26},
        {26, 23, 42, 24, 33, 31},
        {28, 25, 45, 26, 35, 33},
        {32, 28, 51, 30, 40, 38},
        {36, 32, 58, 34, 46, 43},
    };

    static const uint8_t v_scan[16] =
    {
        0, 3, 4, 3, 3, 1, 5, 1, 4, 5, 2, 5, 3, 1, 5, 1
    };

    for (i = 0; i < SL_8x8_NUM; ++i) {
        m_dequant8x8_coeff[i] = &m_dequant8x8_buffer[i];
        for (j = 0; j < i; ++j) {
            if (0 == std::memcmp(m_active_pps->sm.scaling_matrices_8x8[j].scaling_list,
                                 m_active_pps->sm.scaling_matrices_8x8[i].scaling_list, 64)) {
                m_dequant8x8_coeff[i] = &m_dequant8x8_buffer[j];
                break;
            }
        }
        if (j < i)
            continue;

        for (int q = 0; q < max_qp + 1; ++q) {
            int shift = q / 6;
            int idx   = q % 6;
            for (int x = 0; x < 64; ++x) {
                uint8_t idx2 = v_scan[((x >> 1) & 12) | (x & 3)];
                uint8_t level_scale_8x8 =
                    m_active_pps->sm.scaling_matrices_8x8[i].scaling_list[x] * v[idx][idx2];
                (*m_dequant8x8_coeff[i])[q][x] = level_scale_8x8 << shift;
            }
        }
    }
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x88358(%rax), %rax
movl 0x18(%rax), %eax
movl %eax, 0x24(%rsp)
imull $0x6, 0x24(%rsp), %eax
addl $0x33, %eax
movl %eax, 0x20(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $0x6, 0x2c(%rsp)
jge 0x1aab9
movq (%rsp), %rax
movq %rax, %rdx
addq $0x8e370, %rdx          # imm = 0x8E370
movslq 0x2c(%rsp), %rcx
shlq $0xe, %rcx
addq %rcx, %rdx
movslq 0x2c(%rsp), %rcx
movq %rdx, 0xa63a0(%rax,%rcx,8)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x1a987
movq (%rsp), %rax
movq 0x88360(%rax), %rdi
addq $0x458, %rdi            # imm = 0x458
addq $0xd0, %rdi
movslq 0x28(%rsp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rdi
addq $0x10, %rdi
movq 0x88360(%rax), %rsi
addq $0x458, %rsi            # imm = 0x458
addq $0xd0, %rsi
movslq 0x2c(%rsp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rsi
addq $0x10, %rsi
movl $0x40, %edx
callq 0x20c0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x1a975
movq (%rsp), %rax
movq %rax, %rdx
addq $0x8e370, %rdx          # imm = 0x8E370
movslq 0x28(%rsp), %rcx
shlq $0xe, %rcx
addq %rcx, %rdx
movslq 0x2c(%rsp), %rcx
movq %rdx, 0xa63a0(%rax,%rcx,8)
jmp 0x1a987
jmp 0x1a977
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x1a8de
movl 0x28(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x1a996
jmp 0x1aaa9
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl 0x20(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1aaa7
movl 0x1c(%rsp), %eax
movl $0x6, %ecx
cltd
idivl %ecx
movl %eax, 0x18(%rsp)
movl 0x1c(%rsp), %eax
movl $0x6, %ecx
cltd
idivl %ecx
movl %edx, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
cmpl $0x40, 0x10(%rsp)
jge 0x1aa95
movq (%rsp), %rax
movl 0x10(%rsp), %ecx
sarl %ecx
andl $0xc, %ecx
movl 0x10(%rsp), %edx
andl $0x3, %edx
orl %edx, %ecx
movslq %ecx, %rdx
leaq 0xd71c(%rip), %rcx      # 0x28120
movb (%rcx,%rdx), %cl
movb %cl, 0xf(%rsp)
movq 0x88360(%rax), %rcx
addq $0x458, %rcx            # imm = 0x458
addq $0xd0, %rcx
movslq 0x2c(%rsp), %rdx
imulq $0x50, %rdx, %rdx
addq %rdx, %rcx
movslq 0x10(%rsp), %rdx
movzbl 0x10(%rcx,%rdx), %ecx
movslq 0x14(%rsp), %rsi
leaq 0xd6ae(%rip), %rdx      # 0x280f0
imulq $0x6, %rsi, %rsi
addq %rsi, %rdx
movzbl 0xf(%rsp), %esi
movzbl (%rdx,%rsi), %edx
imull %edx, %ecx
movb %cl, 0xe(%rsp)
movzbl 0xe(%rsp), %edx
movl 0x18(%rsp), %ecx
shll %cl, %edx
movslq 0x2c(%rsp), %rcx
movq 0xa63a0(%rax,%rcx,8), %rax
movslq 0x1c(%rsp), %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movslq 0x10(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x1a9d9
jmp 0x1aa97
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x1a99e
jmp 0x1aaa9
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1a8a4
addq $0x38, %rsp
retq
nop