// File: lukasz-wiecaszek[P]h264iframedecoder/picture.cpp
// Function: ymn::h264::picture::init_coxtext_variables(ymn::h264::slice_header const&)
void picture::init_coxtext_variables(const h264::slice_header& sh)
{
    const sps* sps = m_decoder.m_active_sps;
    const pps* pps = m_decoder.m_active_pps;

    if (sps->frame_mbs_only_flag) {
        m_picture_structure = picture_structure_e::frame;
        m_context_variables.mb_aff_frame = false;
    }
    else {
        if (sh.field_pic_flag) {
            if (sh.bottom_field_flag)
                m_picture_structure = picture_structure_e::field_bottom;
            else
                m_picture_structure = picture_structure_e::field_top;
            m_context_variables.mb_aff_frame = false;
        }
        else {
            m_picture_structure = picture_structure_e::frame;
            m_context_variables.mb_aff_frame = sps->mb_adaptive_frame_field_flag;
        }
    }

    m_context_variables.mb_field_decoding_flag = m_picture_structure != picture_structure_e::frame;

    m_context_variables.mb_x = sh.first_mb_in_slice % m_decoder.m_dimensions.mb_width;
    m_context_variables.mb_y = sh.first_mb_in_slice / m_decoder.m_dimensions.mb_width;

    if (sh.field_pic_flag || m_context_variables.mb_aff_frame)
        m_context_variables.mb_y *= 2;

    if (m_picture_structure == picture_structure_e::field_bottom)
        m_context_variables.mb_y +=1;

    update_mb_pos();

    m_context_variables.lastQPdelta = 0;
    m_context_variables.QPy = pps->pic_init_qp_minus26 + 26 + sh.slice_qp_delta;
    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];

    m_context_variables.chroma_array_type = sps->chroma_format_idc;
    if (sps->chroma_format_idc == 3 && sps->separate_colour_plane_flag)
        m_context_variables.chroma_array_type = 0;

    m_context_variables.left_blocks = nullptr; // it will be evaluated in calculate_neighbours_part2()
    m_context_variables.left_blocks_nzc = nullptr; // it will be evaluated in calculate_neighbours_part2()

    //m_context_variables.intraNxN_pred_mode_cache - it will be initialized during processing of macroblock
    //m_context_variables.non_zero_count           - it will be initialized during processing of macroblock
    //m_context_variables.coeffs_ac                - it will be initialized during processing of macroblock
    //m_context_variables.coeffs_dc                - it will be initialized during processing of macroblock
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rax), %rcx
movq 0x88358(%rcx), %rcx
movq %rcx, 0x10(%rsp)
movq 0x8(%rax), %rax
movq 0x88360(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x6fc(%rax)
je 0x1cc26
movq (%rsp), %rax
movl $0x0, 0x10(%rax)
movb $0x0, 0x18(%rax)
jmp 0x1cc80
movq 0x18(%rsp), %rax
cmpl $0x0, 0x28(%rax)
je 0x1cc5e
movq 0x18(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x1cc49
movq (%rsp), %rax
movl $0x2, 0x10(%rax)
jmp 0x1cc54
movq (%rsp), %rax
movl $0x1, 0x10(%rax)
movq (%rsp), %rax
movb $0x0, 0x18(%rax)
jmp 0x1cc7e
movq (%rsp), %rax
movl $0x0, 0x10(%rax)
movq 0x10(%rsp), %rcx
cmpl $0x0, 0x700(%rcx)
setne %cl
andb $0x1, %cl
movb %cl, 0x18(%rax)
jmp 0x1cc80
movq (%rsp), %rcx
cmpl $0x0, 0x10(%rcx)
setne %al
andb $0x1, %al
movb %al, 0x19(%rcx)
movq 0x18(%rsp), %rax
movl 0x14(%rax), %eax
movq 0x8(%rcx), %rsi
xorl %edx, %edx
divl 0x88340(%rsi)
movl %edx, 0x1c(%rcx)
movq 0x18(%rsp), %rax
movl 0x14(%rax), %eax
movq 0x8(%rcx), %rcx
xorl %edx, %edx
divl 0x88340(%rcx)
movl %eax, %ecx
movq (%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x18(%rsp), %rax
cmpl $0x0, 0x28(%rax)
jne 0x1ccd9
movq (%rsp), %rax
testb $0x1, 0x18(%rax)
je 0x1cce5
movq (%rsp), %rax
movl 0x20(%rax), %ecx
shll %ecx
movl %ecx, 0x20(%rax)
movq (%rsp), %rax
cmpl $0x2, 0x10(%rax)
jne 0x1ccfc
movq (%rsp), %rax
movl 0x20(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x20(%rax)
movq (%rsp), %rdi
callq 0x1cdf0
movq (%rsp), %rax
movl $0x0, 0x30(%rax)
movq 0x8(%rsp), %rcx
movl 0x438(%rcx), %ecx
addl $0x1a, %ecx
movq 0x18(%rsp), %rdx
addl 0x95c(%rdx), %ecx
movl %ecx, 0x34(%rax)
movq 0x8(%rax), %rcx
movslq 0x34(%rax), %rdx
movzbl 0xa63d0(%rcx,%rdx), %ecx
movl %ecx, 0x38(%rax)
movq 0x8(%rax), %rcx
movslq 0x34(%rax), %rdx
movzbl 0xa6410(%rcx,%rdx), %ecx
movl %ecx, 0x3c(%rax)
movq 0x10(%rsp), %rcx
movl 0x10(%rcx), %ecx
movl %ecx, 0x40(%rax)
movq 0x10(%rsp), %rax
cmpl $0x3, 0x10(%rax)
jne 0x1cd7e
movq 0x10(%rsp), %rax
cmpl $0x0, 0x14(%rax)
je 0x1cd7e
movq (%rsp), %rax
movl $0x0, 0x40(%rax)
movq (%rsp), %rax
movq $0x0, 0x48(%rax)
movq $0x0, 0x50(%rax)
addq $0x28, %rsp
retq
nopw (%rax,%rax)