// File: /workspace/github_c_makefile_star2_O0/xiane[P]libvphevcodec/AML_HEVCEncoder.cpp
// Function: AML_HEVCSetInput(AMVHEVCEncHandle_s*, FrameIOhevc_s*)
AMVEnc_Status AML_HEVCSetInput(AMVHEVCEncHandle *Handle, AMVHEVCEncFrameIO *input) {
    Uint32 src_stride;
    Uint32 luma_stride, chroma_stride;
    Uint32 size_src_luma, size_src_chroma;
    char *y = NULL;
    uint32_t idx = Handle->src_idx;

    Handle->src_pics[idx].op_flag = input->op_flag;
    AMVEncFrameFmt fmt = Handle->src_pics[idx].fmt = input->fmt;
    Handle->src_pics[idx].frame_rate = input->frame_rate;
    Handle->src_pics[idx].width = input->pitch;
    Handle->src_pics[idx].height = input->height;
    Handle->src_pics[idx].order = input->disp_order;

    float ms_per_frame = 1000.0f / input->frame_rate;
    int64_t pts = Handle->src_pics[idx].pts = ms_per_frame * input->disp_order + 1;

    if (fmt != AMVENC_NV12 && fmt != AMVENC_NV21 && fmt != AMVENC_YUV420) {
        if (INIT_GE2D) {
#if SUPPORT_SCALE
            if (ge2d_colorFormat(fmt) == AMVENC_SUCCESS) {
                VLOG(DEBUG, "The %d of color format that HEVC need ge2d to change!", fmt);
            } else
#endif
            {
                return AMVENC_NOT_SUPPORTED;
            }
        }
    }
    if (fmt == AMVENC_NV12) {
        Handle->mUvSwap = 0;
    }

    if (Handle->bitrate != input->bitrate) {
        Handle->bitrate = input->bitrate;
        VLOG(DEBUG, "HEVC set bitrate to %d", Handle->bitrate);
        Wave4VpuEncSeqInit(Handle, 0);
    }

    src_stride = wave420l_align32(Handle->enc_width);
    luma_stride = src_stride;
    chroma_stride = src_stride;
#if SUPPORT_SCALE
    if ((input->scale_width !=0 && input->scale_height !=0) || input->crop_left != 0 ||
        input->crop_right != 0 || input->crop_top != 0 || input->crop_bottom != 0 ||
        (fmt != AMVENC_NV12 && fmt != AMVENC_NV21 && fmt != AMVENC_YUV420)) {
        if (INIT_GE2D) {
            INIT_GE2D = false;
            amlge2d.ge2dinfo.src_info[0].format = SRC1_PIXFORMAT;
            amlge2d.ge2dinfo.src_info[1].format = SRC2_PIXFORMAT;
            int ret = aml_ge2d_mem_alloc(&amlge2d);
            if (ret < 0) {
                VLOG(ERR, "encode ge2di mem alloc failed, ret=0x%x", ret);
                return AMVENC_FAIL;
            }
            VLOG(DEBUG, "ge2d init successful!");
        }


        VLOG(DEBUG, "HEVC TEST sclale, enc_width:%d enc_height:%d  pitch:%d height:%d, line:%d",
                 Handle->enc_width, Handle->enc_height, input->pitch, input->height, __LINE__);
        if (input->pitch % 32) {
            VLOG(ERR, "HEVC crop and scale must be 32bit aligned");
            return AMVENC_FAIL;
        }
        if (fmt == AMVENC_RGBA8888)
            memcpy((void *)amlge2d.ge2dinfo.src_info[0].vaddr[0], (void *)input->YCbCr[0], 4 * input->pitch * input->height);
        else if (fmt == AMVENC_NV12 || fmt == AMVENC_NV21) {
            memcpy((void *)amlge2d.ge2dinfo.src_info[0].vaddr[0], (void *)input->YCbCr[0], input->pitch * input->height);
            memcpy((void *) ((char *)amlge2d.ge2dinfo.src_info[0].vaddr[0] + input->pitch * input->height), (void *)input->YCbCr[1], input->pitch * input->height / 2);
        } else if (fmt == AMVENC_YUV420) {
            memcpy((void *)amlge2d.ge2dinfo.src_info[0].vaddr[0], (void *)input->YCbCr[0], input->pitch * input->height);
            memcpy((void *) ((char *)amlge2d.ge2dinfo.src_info[0].vaddr[0] + input->pitch * input->height), (void *)input->YCbCr[1], input->pitch * input->height / 4);
            memcpy((void *) ((char *)amlge2d.ge2dinfo.src_info[0].vaddr[0] + (input->pitch * input->height * 5) /4), (void *)input->YCbCr[2], input->pitch * input->height / 4);
        } else if (fmt == AMVENC_RGB888)
            memcpy((void *)amlge2d.ge2dinfo.src_info[0].vaddr[0], (void *)input->YCbCr[0], input->pitch * input->height * 3);
        do_strechblit(&amlge2d.ge2dinfo, input);
        aml_ge2d_invalid_cache(&amlge2d.ge2dinfo);
        size_src_luma = luma_stride * wave420l_align32(Handle->enc_height);
        size_src_chroma = luma_stride * (wave420l_align16(Handle->enc_height) / 2);
        y = (char *) Handle->src_picx[Handle->src_idx].vb.virt_addr;
        if (Handle->enc_width % 32) {
            for (uint32 i = 0; i < Handle->enc_height; i++) {
                memcpy(y + i * luma_stride, (void *) ((char *) amlge2d.ge2dinfo.dst_info.vaddr[0] + i * Handle->enc_width), Handle->enc_width);
            }
        } else {
            memcpy((void *)Handle->src_pics[Handle->src_idx].vb.virt_addr, amlge2d.ge2dinfo.dst_info.vaddr[0],  Handle->enc_width * Handle->enc_height);
        }
        y = (char *) (Handle->src_pics[Handle->src_idx].vb.virt_addr + size_src_luma);
        memcpy(y, (void *) ((char *)amlge2d.ge2dinfo.dst_info.vaddr[0] + Handle->enc_width * Handle->enc_height), chroma_stride * Handle->enc_height / 2);
    } else
#endif
    {
        // set frame buffers

        // calculate required buffer size
        size_src_luma = luma_stride * wave420l_align32(Handle->enc_height);

        size_src_chroma = luma_stride * (wave420l_align16(Handle->enc_height) / 2);

        y = (char *) Handle->src_pics[Handle->src_idx].vb.virt_addr;
        if (Handle->enc_width % 32) {
            for (unsigned int i = 0; i < Handle->enc_height; i++) {
                memcpy(y + i * luma_stride, (void *) ((char *) input->YCbCr[0] + i * Handle->enc_width), Handle->enc_width);
            }
        } else {
            memcpy(y, (void *) input->YCbCr[0], luma_stride * Handle->enc_height);
        }

        y = (char *) (Handle->src_pics[Handle->src_idx].vb.virt_addr + size_src_luma);
        if (Handle->enc_width % 32) {
            for (unsigned int i = 0; i < Handle->enc_height / 2; i++) {
                memcpy(y + i * chroma_stride, (void *) ((char *) input->YCbCr[1] + i * Handle->enc_width), Handle->enc_width);
            }
        } else {
            memcpy(y, (void *) input->YCbCr[1], chroma_stride * Handle->enc_height / 2);
        }
    }
    flush_memory(Handle->instance_id, &Handle->src_pics[Handle->src_idx].vb);
    return AMVENC_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movl 0x34(%rax), %edx
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl %edx, 0xb0(%rax,%rcx)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x3c(%rbp), %edx
leaq (%rdx,%rdx,4), %rdx
shlq $0x4, %rdx
movl %eax, 0xec(%rcx,%rdx)
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movss 0x40(%rax), %xmm0
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movss %xmm0, 0xb4(%rax,%rcx)
movq -0x18(%rbp), %rax
movl 0x20(%rax), %edx
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl %edx, 0xe4(%rax,%rcx)
movq -0x18(%rbp), %rax
movl 0x24(%rax), %edx
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl %edx, 0xe8(%rax,%rcx)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %edx
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl %edx, 0xe0(%rax,%rcx)
movq -0x18(%rbp), %rax
movss 0x40(%rax), %xmm1
movss 0x26394(%rip), %xmm0    # 0x2e8ac
divss %xmm1, %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x44(%rbp), %xmm0
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
cvtsi2ss %rax, %xmm1
mulss %xmm1, %xmm0
movss 0x2636a(%rip), %xmm1    # 0x2e8a8
addss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movq -0x10(%rbp), %rcx
addq $0xa0, %rcx
movl -0x3c(%rbp), %edx
imulq $0x50, %rdx, %rdx
addq %rdx, %rcx
movq %rax, (%rcx)
movq %rax, -0x50(%rbp)
cmpl $0x3, -0x40(%rbp)
je 0x858c
cmpl $0x2, -0x40(%rbp)
je 0x858c
cmpl $0x4, -0x40(%rbp)
je 0x858c
testb $0x1, 0x2ac84(%rip)     # 0x33200
je 0x858a
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x87a1
jmp 0x858c
cmpl $0x3, -0x40(%rbp)
jne 0x85a0
movq -0x10(%rbp), %rax
movl $0x0, 0x308(%rax)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x3c(%rcx), %eax
je 0x85cb
movq -0x18(%rbp), %rax
movl 0x3c(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
jmp 0x85c0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x2090
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
addl $0x1f, %eax
shrl $0x5, %eax
shll $0x5, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
addl $0x1f, %ecx
shrl $0x5, %ecx
shll $0x5, %ecx
imull %ecx, %eax
movl %eax, -0x28(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
addl $0xf, %ecx
shrl $0x4, %ecx
shll $0x4, %ecx
shrl %ecx
imull %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
addq $0xa0, %rax
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %ecx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq 0x38(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x1f, %eax
cmpl $0x0, %eax
je 0x86a2
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x86a0
movq -0x38(%rbp), %rdi
movl -0x54(%rbp), %eax
imull -0x20(%rbp), %eax
movl %eax, %eax
addq %rax, %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movl -0x54(%rbp), %eax
movq -0x10(%rbp), %rcx
imull 0xc(%rcx), %eax
movl %eax, %eax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %edx
callq 0x2150
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x8654
jmp 0x86c1
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
imull 0x10(%rcx), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x2150
movq -0x10(%rbp), %rax
addq $0xa0, %rax
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %ecx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq 0x38(%rax), %rax
movl -0x28(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x1f, %eax
cmpl $0x0, %eax
je 0x8750
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
shrl %ecx
cmpl %ecx, %eax
jae 0x874e
movq -0x38(%rbp), %rdi
movl -0x58(%rbp), %eax
imull -0x24(%rbp), %eax
movl %eax, %eax
addq %rax, %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0x58(%rbp), %eax
movq -0x10(%rbp), %rcx
imull 0xc(%rcx), %eax
movl %eax, %eax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %edx
callq 0x2150
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x86fd
jmp 0x8772
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
imull 0x10(%rcx), %eax
shrl %eax
movl %eax, %eax
movl %eax, %edx
callq 0x2150
movq -0x10(%rbp), %rax
movl (%rax), %edi
movq -0x10(%rbp), %rsi
addq $0xa0, %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
imulq $0x50, %rax, %rax
addq %rax, %rsi
addq $0x20, %rsi
callq 0x20b0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)