// File: /workspace/github_c_makefile_star2_O0/xiane[P]libvphevcodec/libvp_hevc_codec.cpp
// Function: vl_video_encoder_init
vl_codec_handle_t vl_video_encoder_init(vl_codec_id_t codec_id, int width, int height, int frame_rate, int bit_rate, int gop) {
    AMVEnc_Status ret;
    AMVHEVCEncHandle *mHandle = new AMVHEVCEncHandle;
    bool has_mix = false;
    (void)codec_id;

    if (mHandle == NULL)
        return (vl_codec_handle_t) NULL;
    memset(mHandle, 0, sizeof(AMVHEVCEncHandle));

    VLOG(DEBUG, "bit_rate:%d", bit_rate);
    if ((width % 16 != 0 || height % 2 != 0)) {
        VLOG(DEBUG, "Video frame size %dx%d must be a multiple of 16", width, height);
        return (vl_codec_handle_t) NULL;
    } else if (height % 16 != 0) {
        VLOG(DEBUG, "Video frame height is not standard:%d", height);
        return (vl_codec_handle_t) NULL;
    } else {
        VLOG(DEBUG, "Video frame size is %d x %d", width, height);
    }

    AMVHEVCEncParams *const params = createEncParams();
    if (params == NULL) {
        delete mHandle;
        return (vl_codec_handle_t) NULL;
    }

    if(initEncParams(params)) {
        destroyEncParams(params);
        delete mHandle;
        return (vl_codec_handle_t) NULL;
    }

    params->width = width;
    params->height = height;
#if SUPPORT_SCALE
    params->src_width = width;
    params->src_height = height;
#endif
    params->bitrate = bit_rate;
    params->frame_rate = frame_rate;
    // Set IDR frame refresh interval
    if (gop <= 0) {
        params->idr_period = 0;   //an infinite period, only one I frame
    } else {
        params->idr_period = gop; //period of I frame, 1 means all frames are I type.
    }
    VLOG(DEBUG, "mEncParams.idrPeriod: %d, gop %d\n", params->idr_period, gop);

    ret = AML_HEVCInitialize(mHandle, params, &has_mix, 2);
    if (ret < AMVENC_SUCCESS) {
        destroyEncParams(params);
        delete mHandle;
        return (vl_codec_handle_t) NULL;
    }
    mHandle->mSpsPpsHeaderReceived = false;
    mHandle->mNumInputFrames = -1;  // 1st two buffers contain SPS and PPS
    return (vl_codec_handle_t) mHandle;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movl $0x310, %edi            # imm = 0x310
callq 0x21b0
movq %rax, -0x30(%rbp)
movb $0x0, -0x31(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x8d02
movq $0x0, -0x8(%rbp)
jmp 0x8e9f
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x310, %edx            # imm = 0x310
callq 0x20f0
jmp 0x8d14
movl -0x10(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x8d34
movl -0x14(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x8d45
jmp 0x8d36
jmp 0x8d38
movq $0x0, -0x8(%rbp)
jmp 0x8e9f
movl -0x14(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x8d66
jmp 0x8d57
jmp 0x8d59
movq $0x0, -0x8(%rbp)
jmp 0x8e9f
jmp 0x8d68
jmp 0x8d6a
jmp 0x8d6c
jmp 0x8d6e
callq 0x2380
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x8da7
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x8d9a
movq -0x48(%rbp), %rdi
movl $0x310, %esi            # imm = 0x310
callq 0x21d0
movq $0x0, -0x8(%rbp)
jmp 0x8e9f
movq -0x40(%rbp), %rdi
callq 0x22e0
cmpl $0x0, %eax
je 0x8de7
movq -0x40(%rbp), %rdi
callq 0x20c0
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x8dda
movq -0x50(%rbp), %rdi
movl $0x310, %esi            # imm = 0x310
callq 0x21d0
movq $0x0, -0x8(%rbp)
jmp 0x8e9f
movl -0x10(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x14(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x1c(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x38(%rax)
movl -0x18(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x3c(%rax)
cmpl $0x0, -0x20(%rbp)
jg 0x8e22
movq -0x40(%rbp), %rax
movl $0x0, 0x24(%rax)
jmp 0x8e2c
movl -0x20(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x24(%rax)
jmp 0x8e2e
jmp 0x8e30
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq -0x31(%rbp), %rdx
movl $0x2, %ecx
callq 0x22d0
movl %eax, -0x24(%rbp)
cmpl $0x1, -0x24(%rbp)
jge 0x8e7e
movq -0x40(%rbp), %rdi
callq 0x20c0
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0x8e74
movq -0x58(%rbp), %rdi
movl $0x310, %esi            # imm = 0x310
callq 0x21d0
movq $0x0, -0x8(%rbp)
jmp 0x8e9f
movq -0x30(%rbp), %rax
movb $0x0, 0x2e8(%rax)
movq -0x30(%rbp), %rax
movl $0xffffffff, 0x2f8(%rax) # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)