// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, %ebp
movl %r8d, 0x14(%rsp)
movl %ecx, %r12d
movl %edx, %r13d
movl %esi, %r15d
movl $0x310, %edi            # imm = 0x310
callq 0x21b0
movq %rax, %rbx
movb $0x0, 0xf(%rsp)
xorl %r14d, %r14d
movl $0x310, %edx            # imm = 0x310
movq %rax, %rdi
xorl %esi, %esi
callq 0x20f0
movl %r13d, %eax
orl %r15d, %eax
testb $0xf, %al
jne 0x68d3
callq 0x23a0
movl %r12d, 0x10(%rsp)
movq %rax, %r12
movq %rax, %rdi
callq 0x2300
movl %r15d, 0xc(%r12)
movl %r13d, 0x10(%r12)
movl 0x14(%rsp), %eax
movl %eax, 0x38(%r12)
movl 0x10(%rsp), %eax
movl %eax, 0x3c(%r12)
testl %ebp, %ebp
cmovgl %ebp, %r14d
movl %r14d, 0x24(%r12)
leaq 0xf(%rsp), %rdx
pushq $0x2
popq %rcx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x22f0
testl %eax, %eax
jle 0x68bb
movb $0x0, 0x2e8(%rbx)
orl $-0x1, 0x2f8(%rbx)
movq %rbx, %r14
jmp 0x68d3
movq %r12, %rdi
callq 0x20c0
movl $0x310, %esi            # imm = 0x310
movq %rbx, %rdi
callq 0x21d0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq