// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %r15
leaq 0xcb6b(%rip), %rdi      # 0x1b1b4
leaq 0x10a18(%rip), %rsi     # 0x1f068
leaq 0x10a23(%rip), %rdx     # 0x1f07a
xorl %eax, %eax
callq 0x39ba
movl $0x10f4c0, %edi         # imm = 0x10F4C0
callq 0x34d2
movq %rax, %rbx
movl 0x4(%r15), %edx
movl 0x8(%r15), %ecx
testl %edx, %edx
jle 0xe962
testl %ecx, %ecx
jle 0xe962
movl %ecx, %eax
orl %edx, %eax
testb $0xf, %al
jne 0xe975
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), %eax
testl %eax, %eax
jle 0xe6d1
cmpl $0x10, %eax
jb 0xe6da
pushq $0xf
jmp 0xe6d3
pushq $0x1
popq %rax
movl %eax, 0x98(%rbx)
cmpl $0x0, 0x9c(%rbx)
jg 0xe6ed
movl $0x1, 0x9c(%rbx)
movl 0xa0(%rbx), %ecx
testl %ecx, %ecx
jg 0xe704
movl $0x1, 0xa0(%rbx)
pushq $0x1
popq %rcx
leaq 0xa4(%rbx), %r12
movl 0xa4(%rbx), %esi
movq %r12, %r14
testl %esi, %esi
js 0xe76a
cmpl $0x11, %esi
jb 0xe72a
movl $0x10, (%r12)
pushq $0x10
popq %rsi
jmp 0xe72e
testl %esi, %esi
je 0xe76e
leal -0x1(%rcx), %eax
incl %esi
cltd
idivl %esi
subl %edx, %ecx
movl %ecx, 0xa0(%rbx)
cmpl $0x0, 0xac(%rbx)
je 0xe76e
leaq 0xac(%rbx), %r14
movq 0x1987b(%rip), %rax     # 0x27fd0
movq (%rax), %rcx
leaq 0x1097c(%rip), %rdi     # 0x1f0db
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x21e0
andl $0x0, (%r14)
leaq 0x80(%rbx), %r14
movl 0xb0(%rbx), %eax
cmpl $-0x1, %eax
jge 0xe784
pushq $-0x1
jmp 0xe78b
cmpl $0x3, %eax
jl 0xe792
pushq $0x2
popq %rax
movl %eax, 0xb0(%rbx)
movl (%r15), %eax
movl %eax, (%rbx)
andl $0x0, 0x4(%rbx)
movl $0xf4240, 0x50(%rbx)    # imm = 0xF4240
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x34d2
movq %rax, 0x58(%rbx)
andq $0x0, 0xc0(%rbx)
andl $0x0, 0xcc(%rbx)
andl $0x0, 0x10dae8(%rbx)
leaq 0xd8(%rbx), %rdi
movq %rdi, 0x8ed8(%rbx)
xorl %esi, %esi
movq %r14, %rdx
callq 0x158a4
leaq 0x8ee0(%rbx), %rdi
movq %rdi, 0x10dae0(%rbx)
movq 0x8ed8(%rbx), %rcx
xorl %esi, %esi
movq %r14, %rdx
callq 0x160e7
andq $0x0, 0x10ee48(%rbx)
movl $0x21dca, %eax          # imm = 0x21DCA
cmpq $0x21dda, %rax          # imm = 0x21DDA
je 0xe82e
andq $0x0, (%rbx,%rax,8)
andq $0x0, 0x80(%rbx,%rax,8)
andq $0x0, 0x108(%rbx,%rax,8)
incq %rax
jmp 0xe80a
andq $0x0, 0x10ef50(%rbx)
andq $0x0, 0x10efd8(%rbx)
xorl %r15d, %r15d
movslq (%r12), %rax
cmpq %rax, %r15
jg 0xe85f
movq %rbx, %rdi
callq 0x3df8
movq %rax, 0x10ef58(%rbx,%r15,8)
incq %r15
jmp 0xe841
movq 0x8ed8(%rbx), %rax
movl 0x43c(%rax), %edi
movl 0x440(%rax), %esi
callq 0x53c4
movq %rax, 0x10efe8(%rbx)
xorl %r15d, %r15d
movslq 0x98(%rbx), %rax
addq $0x2, %rax
cmpq %rax, %r15
jge 0xe8a6
movq %rbx, %rdi
callq 0x3df8
movq %rax, 0x10eff0(%rbx,%r15,8)
incq %r15
jmp 0xe881
movq 0x10eff0(%rbx), %rax
movq %rax, 0x10efe0(%rbx)
andl $0x0, 0x10f078(%rbx)
andl $0x0, 0x10f100(%rbx)
leaq 0x10db60(%rbx), %rdi
callq 0x2f21
movl (%rbx), %edi
leaq 0x10f1d8(%rbx), %rsi
callq 0x86f0
movl (%rbx), %edi
leaq 0x10f210(%rbx), %rsi
callq 0x89bf
movl (%rbx), %edi
leaq 0x10f248(%rbx), %rsi
callq 0x8d4b
movl (%rbx), %edi
leaq 0x10f2a8(%rbx), %rsi
callq 0x7dd0
movl (%rbx), %edi
leaq 0x10f398(%rbx), %rsi
callq 0x361c
movl (%rbx), %edi
movq %rbx, %rsi
addq $0x10f380, %rsi         # imm = 0x10F380
callq 0x72b0
movl (%rbx), %edi
leaq -0x30(%rbp), %rsi
callq 0x14104
movslq 0xbc(%rbx), %rax
movq -0x30(%rbp,%rax,8), %rax
movq %rax, 0x10f390(%rbx)
movq %r14, %rdi
callq 0x15810
movq %rax, 0x10f188(%rbx)
movq %rbx, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x19667(%rip), %rax     # 0x27fd0
movq (%rax), %rdi
leaq 0x10719(%rip), %rsi     # 0x1f08c
jmp 0xe986
movq 0x19654(%rip), %rax     # 0x27fd0
movq (%rax), %rdi
leaq 0x10726(%rip), %rsi     # 0x1f0ac
xorl %eax, %eax
callq 0x2170
movq %rbx, %rdi
callq 0x2110
xorl %ebx, %ebx
jmp 0xe952