// File: /workspace/github_c_makefile_star2_O0/xiane[P]libvphevcodec/libvp_hevc_codec.cpp
// Function: vl_video_encoder_encode
int vl_video_encoder_encode(vl_codec_handle_t codec_handle, vl_frame_type_t frame_type, unsigned char *in, unsigned int outputBufferLen, unsigned char *out, vl_img_format_t format) {
    AMVEnc_Status ret;
    uint32_t dataLength = 0;
    int type = 0;
    AMVHEVCEncHandle *handle = (AMVHEVCEncHandle *)codec_handle;

    (void)frame_type;

    handle->mOutputBufferLen = outputBufferLen;
    if (!handle->mSpsPpsHeaderReceived) {
        ret = AML_HEVCEncHeader(handle, (unsigned char *)out, (unsigned int *)&dataLength, &type);
        if (ret == AMVENC_SUCCESS) {
            handle->mSPSPPSDataSize = 0;
            handle->mSPSPPSData = (uint8_t *)malloc(dataLength);
            if (handle->mSPSPPSData) {
                handle->mSPSPPSDataSize = dataLength;
                memcpy(handle->mSPSPPSData, (unsigned char *)out, handle->mSPSPPSDataSize);
                VLOG(DEBUG, "get mSPSPPSData size= %d at line %d \n", handle->mSPSPPSDataSize, __LINE__);
            }
            handle->mNumInputFrames = 0;
            handle->mSpsPpsHeaderReceived = true;
        } else {
            VLOG(ERR, "Encode SPS and PPS error, encoderStatus = %d. handle: %p\n", ret, (void *)handle);
            if (ret == AMVENC_OVERFLOW)
                return ERR_OVERFLOW;
            else
                return ERR_HARDWARE;
        }
    }
    if (handle->mNumInputFrames >= 0) {
        AMVHEVCEncFrameIO videoInput;
        memset(&videoInput, 0, sizeof(videoInput));
        videoInput.height = handle->mEncParams->height;
        videoInput.pitch = handle->mEncParams->width;//((handle->mEncParams.width + 15) >> 4) << 4;
        /* TODO*/
        videoInput.bitrate = handle->mEncParams->bitrate;
        videoInput.frame_rate = (float)handle->mEncParams->frame_rate;
        videoInput.coding_timestamp = (float)(handle->mNumInputFrames * 1000) / (float)videoInput.frame_rate;  // in ms
        videoInput.YCbCr[0] = (unsigned long)&in[0];
        videoInput.YCbCr[1] = (unsigned long)(videoInput.YCbCr[0] + videoInput.height * videoInput.pitch);

        switch (format) {
            case IMG_FMT_NV12:
                videoInput.fmt = AMVENC_NV12;
                videoInput.YCbCr[2] = 0;
                break;
            case IMG_FMT_NV21:
                videoInput.fmt = AMVENC_NV21;
                videoInput.YCbCr[2] = 0;
                break;
            case IMG_FMT_RGB888:
                videoInput.fmt = AMVENC_RGB888;
                videoInput.YCbCr[1] = 0;
                videoInput.YCbCr[2] = 0;
                break;
            default:
                videoInput.fmt = AMVENC_YUV420;
                videoInput.YCbCr[2] = (unsigned long)(videoInput.YCbCr[1] + videoInput.height * videoInput.pitch / 4);
                break;
        }

        videoInput.canvas = 0xffffffff;
        videoInput.type = VMALLOC_BUFFER;
        videoInput.disp_order = handle->mNumInputFrames;
        videoInput.op_flag = 0;
        //if(handle->mNumInputFrames == 0)
           //handle->mKeyFrameRequested = true;
        if (handle->mKeyFrameRequested == true) {
            videoInput.op_flag = AMVEncFrameIO_FORCE_IDR_FLAG;
            handle->mKeyFrameRequested = false;
            VLOG(INFO, "Force encode a IDR frame at %d frame", handle->mNumInputFrames);
        }
        //if (handle->idrPeriod == 0) {
            //videoInput.op_flag |= AMVEncFrameIO_FORCE_IDR_FLAG;
        //}
        ret = AML_HEVCSetInput(handle, &videoInput);
        ++(handle->mNumInputFrames);

        VLOG(DEBUG, "AML_HEVCSetInput ret %d\n", ret);
        if (ret == AMVENC_SUCCESS) {
        } else if (ret < AMVENC_SUCCESS) {
            VLOG(ERR, "encoderStatus = %d at line %d, handle: %p", ret, __LINE__, (void *)handle);
            if (ret == AMVENC_NOT_SUPPORTED)
                return ERR_NOTSUPPORT;
            else
                return ERR_HARDWARE;
        }

        ret = AML_HEVCEncNAL(handle, out, (unsigned int *)&dataLength, &type);
        if (ret == AMVENC_PICTURE_READY) {
            if (type == HEVC_IDR || type == HEVC_CRA  || type == HEVC_BLA) {
                if ((handle->mPrependSPSPPSToIDRFrames || handle->mNumInputFrames == 1) && (handle->mSPSPPSData)) {
                    if (handle->mSPSPPSDataSize + dataLength > handle->mOutputBufferLen) {
                        VLOG(ERR, "nal size %d bigger than output buffer %d!\n", handle->mSPSPPSDataSize + dataLength, handle->mOutputBufferLen);
                        return ERR_OVERFLOW;
                    }
                    memmove(out + handle->mSPSPPSDataSize, out, dataLength);
                    memcpy(out, handle->mSPSPPSData, handle->mSPSPPSDataSize);
                    dataLength += handle->mSPSPPSDataSize;
                    //VLOG(DEBUG, "copy mSPSPPSData to buffer size= %d at line %d \n", handle->mSPSPPSDataSize, __LINE__);
                }
            }
        } else if ((ret == AMVENC_SKIPPED_PICTURE) || (ret == AMVENC_TIMEOUT)) {
            dataLength = 0;
            if (ret == AMVENC_TIMEOUT) {
                handle->mKeyFrameRequested = true;
                ret = AMVENC_SKIPPED_PICTURE;
            }
            VLOG(DEBUG, "ret = %d at line %d, handle: %p", ret, __LINE__, (void *)handle);
        } else if (ret != AMVENC_SUCCESS) {
            dataLength = 0;
            VLOG(ERR, "encoderStatus = %d at line %d, handle: %p", ret , __LINE__, (void *)handle);
            if (ret == AMVENC_OVERFLOW)
                return ERR_OVERFLOW;
            else
                return ERR_HARDWARE;
        }
    }
    return dataLength;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x24(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x300(%rax)
movq -0x48(%rbp), %rax
testb $0x1, 0x2e8(%rax)
jne 0x8fec
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x2350
movl %eax, -0x38(%rbp)
cmpl $0x1, -0x38(%rbp)
jne 0x8fa7
movq -0x48(%rbp), %rax
movb $0x0, 0x2e9(%rax)
movl -0x3c(%rbp), %eax
movl %eax, %edi
callq 0x2220
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x2f0(%rax)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x2f0(%rax)
je 0x8f8c
movl -0x3c(%rbp), %eax
movb %al, %cl
movq -0x48(%rbp), %rax
movb %cl, 0x2e9(%rax)
movq -0x48(%rbp), %rax
movq 0x2f0(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %rax
movzbl 0x2e9(%rax), %eax
movl %eax, %edx
callq 0x2150
jmp 0x8f8a
jmp 0x8f8c
movq -0x48(%rbp), %rax
movl $0x0, 0x2f8(%rax)
movq -0x48(%rbp), %rax
movb $0x1, 0x2e8(%rax)
jmp 0x8fea
jmp 0x8fa9
movl -0x38(%rbp), %esi
movq -0x48(%rbp), %rdx
leaq 0x273be(%rip), %rdi     # 0x30375
movb $0x0, %al
callq 0x2050
leaq 0x272a1(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
cmpl $-0x27, -0x38(%rbp)
jne 0x8fde
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x9421
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x9421
jmp 0x8fec
movq -0x48(%rbp), %rax
cmpl $0x0, 0x2f8(%rax)
jl 0x941b
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movq -0x48(%rbp), %rax
movq 0x2e0(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x8c(%rbp)
movq -0x48(%rbp), %rax
movq 0x2e0(%rax), %rax
movl 0xc(%rax), %eax
movl %eax, -0x90(%rbp)
movq -0x48(%rbp), %rax
movq 0x2e0(%rax), %rax
movl 0x38(%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x48(%rbp), %rax
movq 0x2e0(%rax), %rax
movl 0x3c(%rax), %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, -0x70(%rbp)
movq -0x48(%rbp), %rax
movl 0x2f8(%rax), %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cvtsi2ss %eax, %xmm0
movss -0x70(%rbp), %xmm1
divss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movl -0x8c(%rbp), %ecx
movl -0x90(%rbp), %edx
imull %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0xb4(%rbp)
subl $0x1, %eax
je 0x90ef
jmp 0x90d5
movl -0xb4(%rbp), %eax
subl $0x2, %eax
je 0x9106
jmp 0x90e2
movl -0xb4(%rbp), %eax
subl $0x5, %eax
je 0x911d
jmp 0x913f
movl $0x3, -0x94(%rbp)
movq $0x0, -0xa0(%rbp)
jmp 0x9182
movl $0x2, -0x94(%rbp)
movq $0x0, -0xa0(%rbp)
jmp 0x9182
movl $0x6, -0x94(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
jmp 0x9182
movl $0x4, -0x94(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl -0x8c(%rbp), %eax
imull -0x90(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movl $0xffffffff, -0x78(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x98(%rbp)
movq -0x48(%rbp), %rax
movl 0x2f8(%rax), %eax
movl %eax, -0x88(%rbp)
movl $0x0, -0x7c(%rbp)
movq -0x48(%rbp), %rax
movb 0x2fc(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x91f8
movl $0x1, -0x7c(%rbp)
movq -0x48(%rbp), %rax
movb $0x0, 0x2fc(%rax)
movq -0x48(%rbp), %rax
movl 0x2f8(%rax), %esi
leaq 0x271ce(%rip), %rdi     # 0x303af
movb $0x0, %al
callq 0x2050
leaq 0x27077(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
jmp 0x91f8
movq -0x48(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2250
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl 0x2f8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2f8(%rax)
jmp 0x9220
cmpl $0x1, -0x38(%rbp)
jne 0x9228
jmp 0x9278
cmpl $0x1, -0x38(%rbp)
jge 0x9276
jmp 0x9230
movl -0x38(%rbp), %esi
movq -0x48(%rbp), %rcx
leaq 0x27196(%rip), %rdi     # 0x303d4
movl $0xc4, %edx
movb $0x0, %al
callq 0x2050
leaq 0x27015(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
cmpl $-0x2, -0x38(%rbp)
jne 0x926a
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x9421
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x9421
jmp 0x9278
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x2290
movl %eax, -0x38(%rbp)
cmpl $0x2, -0x38(%rbp)
jne 0x9397
cmpl $0x2, -0x40(%rbp)
je 0x92b0
cmpl $0x1, -0x40(%rbp)
je 0x92b0
cmpl $0x3, -0x40(%rbp)
jne 0x9392
movq -0x48(%rbp), %rax
testb $0x1, 0x2fd(%rax)
jne 0x92ce
movq -0x48(%rbp), %rax
cmpl $0x1, 0x2f8(%rax)
jne 0x9390
movq -0x48(%rbp), %rax
cmpq $0x0, 0x2f0(%rax)
je 0x9390
movq -0x48(%rbp), %rax
movzbl 0x2e9(%rax), %eax
addl -0x3c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x300(%rcx), %eax
jbe 0x933c
jmp 0x92fc
movq -0x48(%rbp), %rax
movzbl 0x2e9(%rax), %esi
addl -0x3c(%rbp), %esi
movq -0x48(%rbp), %rax
movl 0x300(%rax), %edx
leaq 0x26f22(%rip), %rdi     # 0x3023d
movb $0x0, %al
callq 0x2050
leaq 0x26f3d(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x9421
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rax
movzbl 0x2e9(%rax), %eax
cltq
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movl -0x3c(%rbp), %eax
movl %eax, %edx
callq 0x22b0
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x2f0(%rax), %rsi
movq -0x48(%rbp), %rax
movzbl 0x2e9(%rax), %eax
movl %eax, %edx
callq 0x2150
movq -0x48(%rbp), %rax
movzbl 0x2e9(%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x9392
jmp 0x9419
cmpl $0x4, -0x38(%rbp)
je 0x93a3
cmpl $-0x24, -0x38(%rbp)
jne 0x93c8
movl $0x0, -0x3c(%rbp)
cmpl $-0x24, -0x38(%rbp)
jne 0x93c2
movq -0x48(%rbp), %rax
movb $0x1, 0x2fc(%rax)
movl $0x4, -0x38(%rbp)
jmp 0x93c4
jmp 0x93c6
jmp 0x9417
cmpl $0x1, -0x38(%rbp)
je 0x9415
movl $0x0, -0x3c(%rbp)
movl -0x38(%rbp), %esi
movq -0x48(%rbp), %rcx
leaq 0x26ff1(%rip), %rdi     # 0x303d4
movl $0xe2, %edx
movb $0x0, %al
callq 0x2050
leaq 0x26e70(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
cmpl $-0x27, -0x38(%rbp)
jne 0x940c
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x9421
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x9421
jmp 0x9417
jmp 0x9419
jmp 0x941b
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)