// File: lukasz-wiecaszek[P]h264iframedecoder/h264_decoder.cpp
// Function: ymn::h264_decoder::init_dequant4x4_coeff_table()
void h264_decoder::init_dequant4x4_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][3] =
    {
        {10, 13, 16},
        {11, 14, 18},
        {13, 16, 20},
        {14, 18, 23},
        {16, 20, 25},
        {18, 23, 29},
    };

    for (i = 0; i < SL_4x4_NUM; ++i) {
        m_dequant4x4_coeff[i] = &m_dequant4x4_buffer[i];
        for (j = 0; j < i; ++j) {
            if (0 == std::memcmp(m_active_pps->sm.scaling_matrices_4x4[j].scaling_list,
                                 m_active_pps->sm.scaling_matrices_4x4[i].scaling_list, 16)) {
                m_dequant4x4_coeff[i] = &m_dequant4x4_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 < 16; ++x) {
                uint8_t idx2 = (x & 1) + ((x >> 2) & 1);
                uint8_t level_scale_4x4 =
                    m_active_pps->sm.scaling_matrices_4x4[i].scaling_list[x] * v[idx][idx2];
                (*m_dequant4x4_coeff[i])[q][x] = level_scale_4x4 << 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 0x1a864
movq (%rsp), %rax
movq %rax, %rdx
addq $0x88370, %rdx          # imm = 0x88370
movslq 0x2c(%rsp), %rcx
shlq $0xc, %rcx
addq %rcx, %rdx
movslq 0x2c(%rsp), %rcx
movq %rdx, 0xa6370(%rax,%rcx,8)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x1a741
movq (%rsp), %rax
movq 0x88360(%rax), %rdi
addq $0x458, %rdi            # imm = 0x458
addq $0x10, %rdi
movslq 0x28(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rdi
addq $0x10, %rdi
movq 0x88360(%rax), %rsi
addq $0x458, %rsi            # imm = 0x458
addq $0x10, %rsi
movslq 0x2c(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
addq $0x10, %rsi
movl $0x10, %edx
callq 0x20c0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x1a72f
movq (%rsp), %rax
movq %rax, %rdx
addq $0x88370, %rdx          # imm = 0x88370
movslq 0x28(%rsp), %rcx
shlq $0xc, %rcx
addq %rcx, %rdx
movslq 0x2c(%rsp), %rcx
movq %rdx, 0xa6370(%rax,%rcx,8)
jmp 0x1a741
jmp 0x1a731
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x1a69e
movl 0x28(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x1a750
jmp 0x1a854
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl 0x20(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1a852
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 $0x10, 0x10(%rsp)
jge 0x1a840
movq (%rsp), %rax
movl 0x10(%rsp), %ecx
andl $0x1, %ecx
movl 0x10(%rsp), %edx
sarl $0x2, %edx
andl $0x1, %edx
addl %edx, %ecx
movb %cl, 0xf(%rsp)
movq 0x88360(%rax), %rcx
addq $0x458, %rcx            # imm = 0x458
addq $0x10, %rcx
movslq 0x2c(%rsp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq 0x10(%rsp), %rdx
movzbl 0x10(%rcx,%rdx), %ecx
movslq 0x14(%rsp), %rsi
leaq 0xd8e3(%rip), %rdx      # 0x280d0
imulq $0x3, %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 0xa6370(%rax,%rcx,8), %rax
movslq 0x1c(%rsp), %rcx
shlq $0x6, %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 0x1a793
jmp 0x1a842
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x1a758
jmp 0x1a854
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1a664
addq $0x38, %rsp
retq
nopl (%rax)