// File: /workspace/github_c_makefile_star2_O2/akumrao[P]videoAnalyzer/decoder/set.c
// Function: x264_sps_read(bs_s*, x264_sps_t*)
int x264_sps_read(bs_t *s, x264_sps_t sps_array[32]) {LOG_CALL;
    x264_sps_t *sps;

    int i_profile_idc;
    int i_level_idc;

    int b_constraint_set0;
    int b_constraint_set1;
    int b_constraint_set2;

    int id;

    i_profile_idc = bs_read(s, 8, "i_profile_idc");
    b_constraint_set0 = bs_read1(s, "b_constraint_set0");
    b_constraint_set1 = bs_read1(s, "b_constraint_set1");
    b_constraint_set2 = bs_read1(s, "b_constraint_set2");

    bs_skip(s, 5); /* reserved */
    i_level_idc = bs_read(s, 8,"i_level_idc");


    id = bs_read_ue(s,"id");
    if (bs_eof(s) || id >= 32)
    {
        /* the sps is invalid, no need to corrupt sps_array[0] */
        return -1;
    }

    sps = &sps_array[id];
    sps->i_id = id;

    /* put pack parsed value */
    sps->i_profile_idc = i_profile_idc;
    sps->i_level_idc = i_level_idc;
    sps->b_constraint_set0 = b_constraint_set0;
    sps->b_constraint_set1 = b_constraint_set1;
    sps->b_constraint_set2 = b_constraint_set2;

    sps->i_log2_max_frame_num = bs_read_ue(s,"i_log2_max_frame_num") + 4;

    sps->i_poc_type = bs_read_ue(s,"i_poc_type");
    if (sps->i_poc_type == 0)
    {
        sps->i_log2_max_poc_lsb = bs_read_ue(s,"i_log2_max_poc_lsb") + 4;
    } else if (sps->i_poc_type == 1)
    {
        int i;
        sps->b_delta_pic_order_always_zero = bs_read1(s, "b_delta_pic_order_always_zero");
        sps->i_offset_for_non_ref_pic = bs_read_se(s,"i_offset_for_non_ref_pic");
        sps->i_offset_for_top_to_bottom_field = bs_read_se(s,"i_offset_for_top_to_bottom_field");
        sps->i_num_ref_frames_in_poc_cycle = bs_read_ue(s,"i_num_ref_frames_in_poc_cycle");
        if (sps->i_num_ref_frames_in_poc_cycle > 256)
        {
            /* FIXME what to do */
            sps->i_num_ref_frames_in_poc_cycle = 256;
        }
        for (i = 0; i < sps->i_num_ref_frames_in_poc_cycle; i++)
        {
            sps->i_offset_for_ref_frame[i] = bs_read_se(s,"i_offset_for_ref_frame");
        }
    } else if (sps->i_poc_type > 2)
    {
        goto error;
    }

    sps->i_num_ref_frames = bs_read_ue(s,"i_num_ref_frames");
    sps->b_gaps_in_frame_num_value_allowed = bs_read1(s, "b_gaps_in_frame_num_value_allowed");
    sps->i_mb_width = bs_read_ue(s,"i_mb_width") + 1;
    sps->i_mb_height = bs_read_ue(s,"i_mb_height") + 1;
    sps->b_frame_mbs_only = bs_read1(s, "b_frame_mbs_only");
    if (!sps->b_frame_mbs_only)
    {
        sps->b_mb_adaptive_frame_field = bs_read1(s, "b_mb_adaptive_frame_field");
    } else
    {
        sps->b_mb_adaptive_frame_field = 0;
    }
    sps->b_direct8x8_inference = bs_read1(s, "b_direct8x8_inference");

    sps->b_crop = bs_read1(s, "b_crop");
    if (sps->b_crop)
    {
        sps->crop.i_left = bs_read_ue(s,"crop.i_left");
        sps->crop.i_right = bs_read_ue(s,"crop.rigth");
        sps->crop.i_top = bs_read_ue(s,"crop.top");
        sps->crop.i_bottom = bs_read_ue(s,"crop.i_bottom");
    } else
    {
        sps->crop.i_left = 0;
        sps->crop.i_right = 0;
        sps->crop.i_top = 0;
        sps->crop.i_bottom = 0;
    }

    sps->b_vui = bs_read1(s, "b_vui");
    if (sps->b_vui)
    {
        /* FIXME */
    } else
    {

    }

    if (bs_eof(s))
    {
        /* no rbsp trailing */
        fprintf(stderr, "incomplete SPS\n");
        goto error;
    }

    fprintf(stderr, "x264_sps_read: sps:0x%x profile:%d/%d poc:%d ref:%d %xx%d crop:%d-%d-%d-%d\n",
            sps->i_id,
            sps->i_profile_idc, sps->i_level_idc,
            sps->i_poc_type,
            sps->i_num_ref_frames,
            sps->i_mb_width, sps->i_mb_height,
            sps->crop.i_left, sps->crop.i_right,
            sps->crop.i_top, sps->crop.i_bottom);

    return id;

error:
    /* invalidate this sps */
    sps->i_id = -1;
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %rbx
leaq 0xec7a(%rip), %rdi      # 0x1b1b4
leaq 0x12043(%rip), %rsi     # 0x1e584
leaq 0x1204a(%rip), %rdx     # 0x1e592
xorl %eax, %eax
callq 0x39ba
leaq 0x1204a(%rip), %rdx     # 0x1e5a0
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0xc915
movl %eax, %r13d
leaq 0x12043(%rip), %rsi     # 0x1e5ae
movq %rbx, %rdi
callq 0xc9bb
movl %eax, -0x38(%rbp)
leaq 0x12043(%rip), %rsi     # 0x1e5c0
movq %rbx, %rdi
callq 0xc9bb
movl %eax, -0x34(%rbp)
leaq 0x12043(%rip), %rsi     # 0x1e5d2
movq %rbx, %rdi
callq 0xc9bb
movl %eax, -0x30(%rbp)
movl 0x18(%rbx), %eax
addl $-0x5, %eax
testl %eax, %eax
jg 0xc5ad
incq 0x8(%rbx)
addl $0x8, %eax
jmp 0xc5a0
movl %eax, 0x18(%rbx)
leaq 0x11dd0(%rip), %rdi     # 0x1e387
pushq $0x5
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x39ba
leaq 0x1201a(%rip), %rdx     # 0x1e5e4
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0xc915
movl %eax, %r15d
leaq 0x1136b(%rip), %rsi     # 0x1d94a
movq %rbx, %rdi
callq 0xca0e
movl %eax, %r14d
movq %rbx, %rdi
callq 0xca81
pushq $-0x1
popq %rcx
cmpl $0x1f, %r14d
jg 0xc689
testl %eax, %eax
jne 0xc689
movslq %r14d, %rax
movl %r14d, %ecx
imulq $0x470, %rax, %r14      # imm = 0x470
movl %r13d, %eax
leaq (%r12,%r14), %r13
movl %ecx, -0x2c(%rbp)
movl %ecx, (%r12,%r14)
movl %eax, 0x4(%r12,%r14)
movl %r15d, 0x8(%r12,%r14)
movl -0x38(%rbp), %eax
movl %eax, 0xc(%r12,%r14)
movl -0x34(%rbp), %eax
movl %eax, 0x10(%r12,%r14)
movl -0x30(%rbp), %eax
movl %eax, 0x14(%r12,%r14)
leaq 0x112c5(%rip), %rsi     # 0x1d910
movq %rbx, %rdi
callq 0xca0e
addl $0x4, %eax
movl %eax, 0x18(%r12,%r14)
leaq 0x11f8e(%rip), %rsi     # 0x1e5f0
movq %rbx, %rdi
callq 0xca0e
movl %eax, 0x1c(%r12,%r14)
cmpl $0x1, %eax
je 0xc890
testl %eax, %eax
je 0xc69a
cmpl $0x2, %eax
jle 0xc6b0
orl $-0x1, (%r13)
pushq $-0x1
popq %rcx
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x112ac(%rip), %rsi     # 0x1d94d
movq %rbx, %rdi
callq 0xca0e
addl $0x4, %eax
movl %eax, 0x20(%r13)
leaq 0x11fd1(%rip), %rsi     # 0x1e688
movq %rbx, %rdi
callq 0xca0e
movl %eax, 0x434(%r13)
leaq 0x11fcc(%rip), %rsi     # 0x1e699
movq %rbx, %rdi
callq 0xc9bb
movl %eax, 0x438(%r13)
leaq 0x11fd8(%rip), %rsi     # 0x1e6bb
movq %rbx, %rdi
callq 0xca0e
incl %eax
movl %eax, 0x43c(%r13)
leaq 0x11fcb(%rip), %rsi     # 0x1e6c6
movq %rbx, %rdi
callq 0xca0e
incl %eax
movl %eax, 0x440(%r13)
leaq 0x11fbf(%rip), %rsi     # 0x1e6d2
movq %rbx, %rdi
callq 0xc9bb
movl %eax, %ecx
movl %eax, 0x444(%r13)
xorl %eax, %eax
testl %ecx, %ecx
jne 0xc739
leaq 0x11fb2(%rip), %rsi     # 0x1e6e3
movq %rbx, %rdi
callq 0xc9bb
movl %eax, 0x448(%r13)
leaq 0x11fb6(%rip), %rsi     # 0x1e6fd
movq %rbx, %rdi
callq 0xc9bb
movl %eax, 0x44c(%r13)
leaq 0x11fb6(%rip), %rsi     # 0x1e713
movq %rbx, %rdi
callq 0xc9bb
movl %eax, 0x450(%r13)
testl %eax, %eax
jne 0xc784
andq $0x0, 0x454(%r13)
andl $0x0, 0x45c(%r13)
xorl %eax, %eax
jmp 0xc7d5
leaq 0x11f8f(%rip), %rsi     # 0x1e71a
movq %rbx, %rdi
callq 0xca0e
movl %eax, 0x454(%r13)
leaq 0x11f85(%rip), %rsi     # 0x1e726
movq %rbx, %rdi
callq 0xca0e
movl %eax, 0x458(%r13)
leaq 0x11f7a(%rip), %rsi     # 0x1e731
movq %rbx, %rdi
callq 0xca0e
movl %eax, 0x45c(%r13)
leaq 0x11f6d(%rip), %rsi     # 0x1e73a
movq %rbx, %rdi
callq 0xca0e
movl %eax, 0x460(%r13)
leaq 0x11f65(%rip), %rsi     # 0x1e748
movq %rbx, %rdi
callq 0xc9bb
movl %eax, 0x464(%r13)
movq %rbx, %rdi
callq 0xca81
movq 0x1b7cf(%rip), %rcx     # 0x27fd0
movq (%rcx), %rcx
testl %eax, %eax
jne 0xc879
movl (%r13), %edx
movl 0x4(%r13), %eax
movl 0x8(%r13), %r8d
movl 0x1c(%r13), %r9d
movl 0x434(%r13), %r10d
movl 0x43c(%r13), %r11d
movl 0x440(%r13), %ebx
movl 0x454(%r13), %r14d
movl 0x458(%r13), %r15d
movl 0x45c(%r13), %r12d
movl 0x460(%r13), %r13d
subq $0x8, %rsp
leaq 0x11f0a(%rip), %rsi     # 0x1e75e
movq %rcx, %rdi
movl %eax, %ecx
xorl %eax, %eax
pushq %r13
pushq %r12
pushq %r15
pushq %r14
pushq %rbx
pushq %r11
pushq %r10
callq 0x2170
addq $0x40, %rsp
movl -0x2c(%rbp), %ecx
jmp 0xc689
leaq 0x11ece(%rip), %rdi     # 0x1e74e
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x21e0
jmp 0xc681
leaq 0x11d64(%rip), %rsi     # 0x1e5fb
movq %rbx, %rdi
callq 0xc9bb
movl %eax, 0x24(%r13)
leaq 0x11d6f(%rip), %rsi     # 0x1e619
movq %rbx, %rdi
callq 0xcaba
movl %eax, 0x28(%r13)
leaq 0x11d75(%rip), %rsi     # 0x1e632
movq %rbx, %rdi
callq 0xcaba
movl %eax, 0x2c(%r13)
leaq 0x11d83(%rip), %rsi     # 0x1e653
movq %rbx, %rdi
callq 0xca0e
movl $0x100, %ecx            # imm = 0x100
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, 0x30(%r13)
xorl %r15d, %r15d
leaq 0x11d81(%rip), %r14     # 0x1e671
movslq %ecx, %rax
cmpq %rax, %r15
jge 0xc6b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcaba
movl %eax, 0x34(%r13,%r15,4)
incq %r15
movl 0x30(%r13), %ecx
jmp 0xc8f0