// File: /workspace/github_cpp_makefile_star2_O3/zhangk145[P]video_envalue/Main.cpp
// Function: main
int main (int argc, char **argv) {
    Context context;
    char *org_file_name = NULL;
    char *new_file_name = NULL;
    EnvalMode mode = EVL_BOTH; //0表示计算psnr和ssim, 1表示只计算psnr，2表示只计算ssim

    const char *psnr_file_name = "psnr.txt";
    const char *ssim_file_name = "ssim.txt";
    FILE *psnr_file = NULL;
    FILE *ssim_file = NULL;
    int ret = 0;
    AVFrame *frame[2] = {0};
    uint8_t *org_image_data[4];
    int      org_image_linesize[4];
    uint8_t *new_image_data[4];
    int      new_image_linesize[4];
    int decode_max_nm = -1;
    int decode_count = 0;

    Decoder decoder[2];

    if (argc < 3) {
        return -1;
    }
    else if (argc > 3) {
        if (!strcmp(argv[3], "psnr")) {
            mode = EVL_PSNR;
        }
        else if (!strcmp(argv[3], "ssim")) {
            mode = EVL_SSIM;
        }
        else {

        }
    }
    else if (argc > 4) {
        decode_max_nm = atoi(argv[4]);
    }
    decode_count = decode_max_nm;
    Envaluer envaluer(mode);
    org_file_name = argv[1];
    new_file_name = argv[2];

    psnr_file = fopen(psnr_file_name, "w");
    if (psnr_file == NULL) {
        return -1;
    }
    context.psnr_file = psnr_file;
    ssim_file = fopen(ssim_file_name, "w");
    if (ssim_file == NULL) {
        return -1;
    }
    context.ssim_file = ssim_file;

    envaluer.setCallback(psnr_write, ssim_write, &context);

    Decoder::global_init();
    
    frame[0] = av_frame_alloc();
    frame[1] = av_frame_alloc();

    ret = decoder[0].open(org_file_name);
    if (ret < 0) {
        return -1;
    }
    ret = av_image_alloc(org_image_data, org_image_linesize,
                            decoder[0].width(), decoder[0].height(), decoder[0].pix_fmt(), 1);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate raw video buffer\n");
        return ret;
    }
    ret = decoder[1].open(new_file_name);
    if (ret < 0) {
        return -1;
    }
    ret = av_image_alloc(new_image_data, new_image_linesize,
                            decoder[1].width(), decoder[1].height(), decoder[1].pix_fmt(), 1);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate raw video buffer\n");
        return ret;
    }

    envaluer.open(decoder[0].width(), decoder[0].height(), decoder[0].pix_fmt());
    // int cnt = 10;
    while(1) {
        if (decode_max_nm > 0) {
            if (decode_count-- <= 0) {
                break;
            }
        }
        ret = decoder[0].decode(frame[0]);
        // printf("[%s:%d]==ret:%d\n", __FUNCTION__, __LINE__, ret);
        if (ret < 0) {
            break;
        }
        av_image_copy(org_image_data, org_image_linesize,
                    (const uint8_t **)(frame[0]->data), frame[0]->linesize,
                    decoder[0].pix_fmt(), decoder[0].width(), decoder[0].height());
        ret = decoder[1].decode(frame[1]);
        // printf("[%s:%d]==ret:%d\n", __FUNCTION__, __LINE__, ret);
        if (ret < 0) {
            printf("[%s:%d]==ret:%d\n", __FUNCTION__, __LINE__, ret);
            break;
        }
        // printf("[%s:%d]==\n", __FUNCTION__, __LINE__);
        av_image_copy(new_image_data, new_image_linesize,
                    (const uint8_t **)(frame[1]->data), frame[1]->linesize,
                    decoder[1].pix_fmt(), decoder[1].width(), decoder[1].height());
        // printf("[%s:%d]==\n", __FUNCTION__, __LINE__);
        envaluer.compute(org_image_data[0], new_image_data[0]);
    }
    envaluer.close();
    av_frame_free(&frame[0]);
    av_frame_free(&frame[1]);
    av_free(org_image_data[0]);
    av_free(new_image_data[0]);
    fclose(psnr_file);
    fclose(ssim_file);
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rsi, %rbx
movl %edi, %r14d
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x26fc
leaq 0x50(%rsp), %rdi
callq 0x26fc
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, %r14d
jl 0x350e
movl $0x2, %esi
je 0x3276
movq 0x18(%rbx), %r14
leaq 0x1075(%rip), %rsi      # 0x42c1
movq %r14, %rdi
callq 0x21c0
testl %eax, %eax
je 0x3274
leaq 0x1067(%rip), %rsi      # 0x42c6
movq %r14, %rdi
callq 0x21c0
cmpl $0x1, %eax
movl $0x2, %esi
sbbl $0x0, %esi
jmp 0x3276
xorl %esi, %esi
leaq 0x108(%rsp), %rdi
callq 0x2ab0
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %r15
leaq 0x101d(%rip), %rdi      # 0x42af
leaq 0x1032(%rip), %rsi      # 0x42cb
callq 0x22b0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x3501
movq %rax, %rbx
movq %rax, 0xf8(%rsp)
leaq 0xffa(%rip), %rdi       # 0x42b8
leaq 0x1006(%rip), %rsi      # 0x42cb
callq 0x22b0
testq %rax, %rax
je 0x3501
movq %rbx, 0x88(%rsp)
leaq 0xf8(%rsp), %rdx
movq %rax, 0x80(%rsp)
movq %rax, 0x8(%rdx)
leaq 0x2a3(%rip), %rcx       # 0x3599
movq %rcx, 0x160(%rsp)
leaq 0x2f3(%rip), %rcx       # 0x35f8
movq %rcx, 0x168(%rsp)
movq %rdx, 0x170(%rsp)
callq 0x2740
callq 0x20d0
movq %rax, %r12
movq %rax, 0x10(%rsp)
callq 0x20d0
movq %rax, %r13
movq %rax, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x2746
testl %eax, %eax
js 0x3501
movl 0x3c(%rsp), %r8d
movl 0x40(%rsp), %edx
movl 0x44(%rsp), %ecx
leaq 0xd0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movl $0x1, %r9d
callq 0x2110
movl %eax, %ebx
testl %eax, %eax
js 0x353e
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x2746
testl %eax, %eax
js 0x3501
movl 0x6c(%rsp), %r8d
movl 0x70(%rsp), %edx
movl 0x74(%rsp), %ecx
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movl $0x1, %r9d
callq 0x2110
movl %eax, %ebp
testl %eax, %eax
js 0x3547
movl 0x3c(%rsp), %ecx
movl 0x40(%rsp), %esi
movl 0x44(%rsp), %edx
leaq 0x108(%rsp), %rdi
callq 0x2ad8
movq %r12, %r15
addq $0x40, %r15
movq %r13, %r14
addq $0x40, %r14
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x29f0
movl %eax, %ebp
testl %eax, %eax
js 0x34ac
movl 0x3c(%rsp), %r8d
movl 0x40(%rsp), %r9d
movl 0x44(%rsp), %eax
movl %eax, (%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2220
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0x29f0
movl %eax, %ebp
testl %eax, %eax
js 0x3490
movl 0x6c(%rsp), %r8d
movl 0x70(%rsp), %r9d
movl 0x74(%rsp), %eax
movl %eax, (%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2220
movq 0xd0(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x108(%rsp), %rdi
callq 0x3060
jmp 0x33ee
leaq 0xe5b(%rip), %rdi       # 0x42f2
leaq 0xe65(%rip), %rsi       # 0x4303
movl $0x87, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2050
leaq 0x108(%rsp), %rdi
callq 0x2b36
leaq 0x10(%rsp), %rdi
callq 0x20c0
leaq 0x18(%rsp), %rdi
callq 0x20c0
movq 0xd0(%rsp), %rdi
callq 0x2210
movq 0xa0(%rsp), %rdi
callq 0x2210
movq 0x88(%rsp), %rdi
callq 0x20b0
movq 0x80(%rsp), %rdi
callq 0x20b0
leaq 0x108(%rsp), %rdi
callq 0x2ad6
movl $0x30, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x20, %rdi
callq 0x2720
addq $-0x30, %rbx
cmpq $-0x30, %rbx
jne 0x3513
movl %ebp, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x259f
movl %ebx, %ebp
jmp 0x3501
callq 0x257d
jmp 0x3501
movq %rax, %rbx
jmp 0x3574
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2720
jmp 0x3591
jmp 0x3564
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x2ad6
movl $0x30, %r14d
leaq (%rsp,%r14), %rdi
addq $0x20, %rdi
callq 0x2720
addq $-0x30, %r14
cmpq $-0x30, %r14
jne 0x357a
movq %rbx, %rdi
callq 0x2170