// File: lukasz-wiecaszek[P]h264iframedecoder/h264_parser.cpp
// Function: ymn::h264_parser::parse_vui_parameters(ymn::istream<0>&, ymn::h264::vui_parameters&)
bool h264_parser::parse_vui_parameters(istream_be& s, h264::vui_parameters& vui)
{
    bool retval = false;

    do {
        s.read_bits(1, vui.aspect_ratio_info_present_flag);
        if (vui.aspect_ratio_info_present_flag) {
            s.read_bits(8, vui.aspect_ratio_idc);
            if (vui.aspect_ratio_idc == static_cast<uint32_t>(h264::aspect_ratio_idc_e::SAR_EXTENDED)) {
                s.read_bits(16, vui.sar_width);
                s.read_bits(16, vui.sar_height);
            }
        }

        s.read_bits(1, vui.overscan_info_present_flag);
        if (vui.overscan_info_present_flag)
            s.read_bits(1, vui.overscan_appropriate_flag);

        s.read_bits(1, vui.video_signal_type_present_flag);
        if (vui.video_signal_type_present_flag) {
            s.read_bits(3, vui.video_format);
            s.read_bits(1, vui.video_full_range_flag);
            s.read_bits(1, vui.colour_description_present_flag);
            if (vui.colour_description_present_flag) {
                s.read_bits(8, vui.colour_primaries);
                s.read_bits(8, vui.transfer_characteristics);
                s.read_bits(8, vui.matrix_coefficients);
            }
        }

        s.read_bits(1, vui.chroma_loc_info_present_flag);
        if (vui.chroma_loc_info_present_flag) {
            s.read_exp_golomb_u(vui.chroma_sample_loc_type_top_field);
            s.read_exp_golomb_u(vui.chroma_sample_loc_type_bottom_field);
        }

        s.read_bits(1, vui.timing_info_present_flag);
        if (vui.timing_info_present_flag) {
            s.read_bits(32, vui.num_units_in_tick);
            s.read_bits(32, vui.time_scale);
            s.read_bits(1, vui.fixed_frame_rate_flag);
        }

        s.read_bits(1, vui.nal_hrd_parameters_present_flag);
        if (vui.nal_hrd_parameters_present_flag)
            parse_hrd_parameters(s, vui.nal_hrd);

        s.read_bits(1, vui.vcl_hrd_parameters_present_flag);
        if (vui.vcl_hrd_parameters_present_flag)
            parse_hrd_parameters(s, vui.vcl_hrd);

        if (vui.nal_hrd_parameters_present_flag || vui.vcl_hrd_parameters_present_flag) {
            s.read_bits(1, vui.low_delay_hrd_flag);
        }

        s.read_bits(1, vui.pic_struct_present_flag);
        s.read_bits(1, vui.bitstream_restriction_flag);
        if (vui.bitstream_restriction_flag) {
            s.read_bits(1, vui.motion_vectors_over_pic_boundaries_flag);
            s.read_exp_golomb_u(vui.max_bytes_per_pic_denom);
            s.read_exp_golomb_u(vui.max_bits_per_mb_denom);
            s.read_exp_golomb_u(vui.log2_max_mv_length_horizontal);
            s.read_exp_golomb_u(vui.log2_max_mv_length_vertical);
            s.read_exp_golomb_u(vui.max_num_reorder_frames);
            s.read_exp_golomb_u(vui.max_dec_frame_buffering);
        }

        if (s.status() != ISTREAM_STATUS_OK)
            break;

        vui.set_valid(true);
        retval = true;
    } while (0);

    return retval;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
movb $0x0, 0xf(%rsp)
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0xc, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0x1306c
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x10, %rdx
movl $0x8, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0xff, 0x10(%rax)
jne 0x1306a
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x14, %rdx
movl $0x10, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x18, %rdx
movl $0x10, %esi
callq 0x16f30
jmp 0x1306c
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x1c, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x130a7
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x20, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x24, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0x24(%rax)
je 0x1316b
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x28, %rdx
movl $0x3, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x2c, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x30, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0x30(%rax)
je 0x13169
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x34, %rdx
movl $0x8, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x38, %rdx
movl $0x8, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x3c, %rdx
movl $0x8, %esi
callq 0x16f30
jmp 0x1316b
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x40, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0x40(%rax)
je 0x131b4
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x44, %rsi
callq 0x17200
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x48, %rsi
callq 0x17200
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x4c, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0x4c(%rax)
je 0x1321f
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x50, %rdx
movl $0x20, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x54, %rdx
movl $0x20, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x58, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x5c, %rdx
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0x5c(%rax)
je 0x13259
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq $0x60, %rdx
callq 0x12dd0
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x208, %rdx            # imm = 0x208
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0x208(%rax)
je 0x1329c
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq $0x210, %rdx            # imm = 0x210
callq 0x12dd0
movq 0x10(%rsp), %rax
cmpl $0x0, 0x5c(%rax)
jne 0x132b5
movq 0x10(%rsp), %rax
cmpl $0x0, 0x208(%rax)
je 0x132d0
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x3b8, %rdx            # imm = 0x3B8
movl $0x1, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x3bc, %rdx            # imm = 0x3BC
movl $0x1, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x3c0, %rdx            # imm = 0x3C0
movl $0x1, %esi
callq 0x16f30
movq 0x10(%rsp), %rax
cmpl $0x0, 0x3c0(%rax)
je 0x133b7
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
addq $0x3c4, %rdx            # imm = 0x3C4
movl $0x1, %esi
callq 0x16f30
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x3c8, %rsi            # imm = 0x3C8
callq 0x17200
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x3cc, %rsi            # imm = 0x3CC
callq 0x17200
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x3d0, %rsi            # imm = 0x3D0
callq 0x17200
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x3d4, %rsi            # imm = 0x3D4
callq 0x17200
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x3d8, %rsi            # imm = 0x3D8
callq 0x17200
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x3dc, %rsi            # imm = 0x3DC
callq 0x17200
movq 0x18(%rsp), %rdi
callq 0x17150
cmpl $0x0, %eax
je 0x133c8
jmp 0x133dc
movq 0x10(%rsp), %rdi
movl $0x1, %esi
callq 0x17160
movb $0x1, 0xf(%rsp)
movb 0xf(%rsp), %al
andb $0x1, %al
addq $0x28, %rsp
retq
nopw (%rax,%rax)