// File: /workspace/github_c_makefile_star2_O3/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
pushq %rax
movl %r9d, %ebp
movl %r8d, 0x4(%rsp)
movl %ecx, %r12d
movl %edx, %r13d
movl %esi, %r15d
movl $0x310, %edi            # imm = 0x310
callq 0x21a0
movq %rax, %rbx
movb $0x0, 0x3(%rsp)
xorl %r14d, %r14d
movl $0x310, %edx            # imm = 0x310
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movl %r13d, %eax
orl %r15d, %eax
testb $0xf, %al
jne 0x6c8d
movl $0x60, %edi
callq 0x21a0
movl %r12d, %ecx
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x2c(%rax)
movups %xmm0, 0x4c(%rax)
movups %xmm0, 0x3c(%rax)
movups %xmm0, 0x1c(%rax)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x14(%r12)
movl $0x10, 0x2c(%r12)
movb $0x1, 0x58(%r12)
movq $0x1, (%r12)
movl $0x0, 0x8(%r12)
movl $0x1e, 0x34(%r12)
movl $0x1, 0x5c(%r12)
movl %r15d, 0xc(%r12)
movl %r13d, 0x10(%r12)
movl 0x4(%rsp), %eax
movl %eax, 0x38(%r12)
movl %ecx, 0x3c(%r12)
testl %ebp, %ebp
cmovgl %ebp, %r14d
movl %r14d, 0x24(%r12)
leaq 0x3(%rsp), %rdx
movq %rbx, %rdi
movq %r12, %rsi
movl $0x2, %ecx
callq 0x22e0
testl %eax, %eax
jle 0x6c70
movb $0x0, 0x2e8(%rbx)
movl $0xffffffff, 0x2f8(%rbx) # imm = 0xFFFFFFFF
movq %rbx, %r14
jmp 0x6c8d
movl $0x60, %esi
movq %r12, %rdi
callq 0x21c0
movl $0x310, %esi            # imm = 0x310
movq %rbx, %rdi
callq 0x21c0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq