// File: /workspace/github_c_makefile_star2_O3/akumrao[P]videoAnalyzer/encoder/encoder.c
// Function: x264_encoder_open(x264_param_t*)
x264_t *x264_encoder_open(x264_param_t *param) {LOG_CALL;
    x264_t *h = (x264_t *)x264_aligned_alloc(sizeof ( x264_t));
    int i;
    x264_me_t me[2];

    if (param->i_width <= 0 || param->i_height <= 0)
    {
        fprintf(stderr, "invalid width x height (%dx%d)\n",
                param->i_width, param->i_height);
        free(h);
        return NULL;
    }

    if (param->i_width % 16 != 0 || param->i_height % 16 != 0)
    {
        fprintf(stderr, "width %% 16 != 0 pr height %% 16 != 0 (%dx%d)\n",
                param->i_width, param->i_height);
        free(h);
        return NULL;
    }

    memcpy(&h->param, param, sizeof ( x264_param_t));
    if (h->param.i_frame_reference <= 0)
    {
        h->param.i_frame_reference = 1;
    } else if (h->param.i_frame_reference > 15)
    {
        h->param.i_frame_reference = 15;
    }
    if (h->param.i_idrframe <= 0)
    {
        h->param.i_idrframe = 1;
    }
    if (h->param.i_iframe <= 0)
    {
        h->param.i_iframe = 1;
    }
    if (h->param.i_bframe < 0)
    {
        h->param.i_bframe = 0;
    } else if (h->param.i_bframe > X264_BFRAME_MAX)
    {
        h->param.i_bframe = X264_BFRAME_MAX;
    }
    /* XXX for now we use fixed patern IB...BPB...BPB...BP */
    if (h->param.i_bframe > 0)
    {
        h->param.i_iframe = ((h->param.i_iframe - 1) / (h->param.i_bframe + 1))*(h->param.i_bframe + 1) + 1;

        if (h->param.b_cabac)
        {
            fprintf(stderr, "cabac not supported with B frame (cabac disabled)\n");
            h->param.b_cabac = 0;
        }
    }
    if (h->param.i_cabac_init_idc < -1)
    {
        h->param.i_cabac_init_idc = -1;
    } else if (h->param.i_cabac_init_idc > 2)
    {
        h->param.i_cabac_init_idc = 2;
    }

    h->cpu = param->cpu;

    h->i_nal = 0;
    h->i_bitstream = 1000000; /* FIXME calculate max size from width/height */
    h->p_bitstream = (uint8_t*)x264_aligned_alloc(h->i_bitstream);

    h->i_frame = 0;
    h->i_frame_num = 0;
    h->i_poc = 0;
    h->i_idr_pic_id = 0;

    h->sps = &h->sps_array[0];
    x264_sps_init(h->sps, 0, &h->param);

    h->pps = &h->pps_array[0];
    x264_pps_init(h->pps, 0, &h->param, h->sps);

    /* init picture and bframe context */
    h->picture = NULL;
    for (i = 0; i < X264_BFRAME_MAX; i++)
    {
        h->bframe_current[i] = NULL;
        h->frame_next[i] = NULL;
        h->frame_unused[i] = NULL;
    }
    h->frame_next[X264_BFRAME_MAX] = NULL;
    h->frame_unused[X264_BFRAME_MAX] = NULL;
    for (i = 0; i < h->param.i_bframe + 1; i++)
    {
        h->frame_unused[i] = x264_frame_new(h);
    }

    h->mb = x264_macroblocks_new(h->sps->i_mb_width, h->sps->i_mb_height);

    /* reference frame (2 for B frame) and current one */
    for (i = 0; i < h->param.i_frame_reference + 2; i++)
    {
        h->freference[i] = x264_frame_new(h);
    }
    h->fdec = h->freference[0];
    h->i_ref0 = 0;
    h->i_ref1 = 0;

    /* init cabac adaptive model */
    x264_cabac_model_init(&h->cabac);

    /* init predict_XxX */
    x264_predict_16x16_init(h->cpu, h->predict_16x16);
    x264_predict_8x8_init(h->cpu, h->predict_8x8);
    x264_predict_4x4_init(h->cpu, h->predict_4x4);

    x264_pixel_init(h->cpu, &h->pixf);
    x264_dct_init(h->cpu, &h->dctf);

    x264_mc_init(h->cpu, h->mc);

    x264_me_init(h->cpu, me);
    h->me = me[h->param.i_me];

    /* rate control */
    h->rc = x264_ratecontrol_new(&h->param);

    return h;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r15
leaq 0x12c85(%rip), %rdi     # 0x271b4
leaq 0x169f2(%rip), %rsi     # 0x2af28
leaq 0x169fd(%rip), %rdx     # 0x2af3a
xorl %eax, %eax
callq 0x4a56
movl $0x10f4c0, %edi         # imm = 0x10F4C0
callq 0x461e
movq %rax, %rbx
movl 0x4(%r15), %edx
movl 0x8(%r15), %ecx
testl %edx, %edx
jle 0x14827
testl %ecx, %ecx
jle 0x14827
movl %ecx, %eax
orl %edx, %eax
testb $0xf, %al
jne 0x1483a
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups 0x30(%r15), %xmm3
movups %xmm1, 0x90(%rbx)
movups %xmm3, 0xb0(%rbx)
movups %xmm2, 0xa0(%rbx)
movups %xmm0, 0x80(%rbx)
movl 0x98(%rbx), %ecx
testl %ecx, %ecx
jle 0x145ba
movl $0xf, %eax
cmpl $0x10, %ecx
jae 0x145bf
jmp 0x145c5
movl $0x1, %eax
movl %eax, 0x98(%rbx)
cmpl $0x0, 0x9c(%rbx)
jg 0x145d8
movl $0x1, 0x9c(%rbx)
movl 0xa0(%rbx), %ecx
testl %ecx, %ecx
jg 0x145f1
movl $0x1, 0xa0(%rbx)
movl $0x1, %ecx
leaq 0xa4(%rbx), %r14
movl 0xa4(%rbx), %esi
testl %esi, %esi
js 0x1463e
cmpl $0x11, %esi
jb 0x14615
movl $0x10, (%r14)
movl $0x10, %esi
jmp 0x14619
testl %esi, %esi
je 0x14645
leal -0x1(%rcx), %eax
incl %esi
cltd
idivl %esi
subl %edx, %ecx
movl %ecx, 0xa0(%rbx)
cmpl $0x0, 0xac(%rbx)
je 0x14645
leaq 0xac(%rbx), %r14
callq 0x2c8f
movl $0x0, (%r14)
leaq 0x80(%rbx), %r14
movl 0xb0(%rbx), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $-0x1, %ecx
jl 0x14666
movl $0x2, %eax
cmpl $0x3, %ecx
jl 0x1466c
movl %eax, 0xb0(%rbx)
movl (%r15), %eax
movl %eax, (%rbx)
xorl %r15d, %r15d
movl %r15d, 0x4(%rbx)
movl $0xf4240, 0x50(%rbx)    # imm = 0xF4240
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x461e
movq %rax, 0x58(%rbx)
movq $0x0, 0xc0(%rbx)
movl %r15d, 0xcc(%rbx)
movl %r15d, 0x10dae8(%rbx)
leaq 0xd8(%rbx), %rdi
movq %rdi, 0x8ed8(%rbx)
xorl %esi, %esi
movq %r14, %rdx
callq 0x1f758
leaq 0x8ee0(%rbx), %rdi
movq %rdi, 0x10dae0(%rbx)
movq 0x8ed8(%rbx), %rcx
xorl %esi, %esi
movq %r14, %rdx
callq 0x211af
leaq 0x10ee48(%rbx), %rdi
movl $0x198, %edx            # imm = 0x198
xorl %esi, %esi
callq 0x2070
cmpl %r15d, 0xa4(%rbx)
js 0x1471f
movq $-0x1, %r15
movq %rbx, %rdi
callq 0x4e9c
movq %rax, 0x10ef60(%rbx,%r15,8)
movslq 0xa4(%rbx), %rax
incq %r15
cmpq %rax, %r15
jl 0x14700
movq 0x8ed8(%rbx), %rax
movl 0x43c(%rax), %edi
movl 0x440(%rax), %esi
callq 0x64a8
movq %rax, 0x10efe8(%rbx)
cmpl $-0x1, 0x98(%rbx)
jl 0x1476d
movq $-0x1, %r15
movq %rbx, %rdi
callq 0x4e9c
movq %rax, 0x10eff8(%rbx,%r15,8)
movslq 0x98(%rbx), %rax
incq %r15
cmpq %rax, %r15
jle 0x1474e
movq 0x10eff0(%rbx), %rax
movq %rax, 0x10efe0(%rbx)
xorl %eax, %eax
movl %eax, 0x10f078(%rbx)
movl %eax, 0x10f100(%rbx)
leaq 0x10db60(%rbx), %rdi
callq 0x3d57
movl (%rbx), %edi
leaq 0x10f1d8(%rbx), %rsi
callq 0xa8dc
movl (%rbx), %edi
leaq 0x10f210(%rbx), %rsi
callq 0xaba9
movl (%rbx), %edi
leaq 0x10f248(%rbx), %rsi
callq 0xaf08
movl (%rbx), %edi
leaq 0x10f2a8(%rbx), %rsi
callq 0x9d40
movl (%rbx), %edi
leaq 0x10f398(%rbx), %rsi
callq 0x46a8
movl (%rbx), %edi
movq %rbx, %rsi
addq $0x10f380, %rsi         # imm = 0x10F380
callq 0x85cc
movl (%rbx), %edi
leaq -0x30(%rbp), %rsi
callq 0x1c3ec
movslq 0xbc(%rbx), %rax
movq -0x30(%rbp,%rax,8), %rax
movq %rax, 0x10f390(%rbx)
movq %r14, %rdi
callq 0x1f6bc
movq %rax, 0x10f188(%rbx)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x1e7a2(%rip), %rax     # 0x32fd0
movq (%rax), %rdi
leaq 0x16714(%rip), %rsi     # 0x2af4c
jmp 0x1484b
movq 0x1e78f(%rip), %rax     # 0x32fd0
movq (%rax), %rdi
leaq 0x16721(%rip), %rsi     # 0x2af6c
xorl %eax, %eax
callq 0x2170
movq %rbx, %rdi
callq 0x2110
xorl %ebx, %ebx
jmp 0x14819