// File: /workspace/github_cpp_makefile_star2_O1/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)
xorl %r15d, %r15d
leaq (%rsp,%r15), %rdi
addq $0x30, %rdi
callq 0x26fc
addq $0x30, %r15
cmpq $0x60, %r15
jne 0x314e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, %r14d
jl 0x3453
movl $0x2, %esi
je 0x31b0
movq 0x18(%rbx), %r14
leaq 0x113b(%rip), %rsi      # 0x42c1
movq %r14, %rdi
callq 0x21c0
testl %eax, %eax
je 0x31ae
leaq 0x112d(%rip), %rsi      # 0x42c6
movq %r14, %rdi
callq 0x21c0
cmpl $0x1, %eax
movl $0x2, %esi
sbbl $0x0, %esi
jmp 0x31b0
xorl %esi, %esi
leaq 0x108(%rsp), %rdi
callq 0x2ab0
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %r12
leaq 0x10e3(%rip), %rdi      # 0x42af
leaq 0x10f8(%rip), %rsi      # 0x42cb
callq 0x22b0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x3446
movq %rax, %r15
movq %rax, 0xf8(%rsp)
leaq 0x10c0(%rip), %rdi      # 0x42b8
leaq 0x10cc(%rip), %rsi      # 0x42cb
callq 0x22b0
testq %rax, %rax
je 0x3446
movq %rax, %r13
leaq 0xf8(%rsp), %rax
movq %r13, 0x8(%rax)
leaq 0x2c9(%rip), %rcx       # 0x34ec
movq %rcx, 0x160(%rsp)
leaq 0x319(%rip), %rcx       # 0x354b
movq %rcx, 0x168(%rsp)
movq %rax, 0x170(%rsp)
callq 0x2740
callq 0x20d0
movq %rax, 0x10(%rsp)
callq 0x20d0
movq %rax, 0x18(%rsp)
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x2746
testl %eax, %eax
js 0x3446
movl 0x4c(%rsp), %r8d
movl 0x50(%rsp), %edx
movl 0x54(%rsp), %ecx
leaq 0xd0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movl $0x1, %r9d
callq 0x2110
movl %eax, %ebx
testl %eax, %eax
js 0x3483
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x2746
testl %eax, %eax
js 0x3446
movl 0x7c(%rsp), %r8d
movl 0x80(%rsp), %edx
movl 0x84(%rsp), %ecx
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movl $0x1, %r9d
callq 0x2110
movl %eax, %ebp
testl %eax, %eax
js 0x348c
movl 0x4c(%rsp), %ecx
movl 0x50(%rsp), %esi
movl 0x54(%rsp), %edx
leaq 0x108(%rsp), %rdi
callq 0x2ad8
movq %r13, 0x20(%rsp)
movq %r15, 0x28(%rsp)
leaq 0x30(%rsp), %r13
leaq 0xd0(%rsp), %r12
leaq 0xc0(%rsp), %r14
leaq 0x90(%rsp), %rbx
leaq 0x108(%rsp), %r15
movq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x29e8
movl %eax, %ebp
testl %eax, %eax
js 0x33f1
movq 0x10(%rsp), %rdx
leaq 0x40(%rdx), %rcx
movl 0x4c(%rsp), %r8d
movl 0x50(%rsp), %r9d
movl 0x54(%rsp), %eax
movl %eax, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x2220
movq 0x18(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x29e8
movl %eax, %ebp
testl %eax, %eax
js 0x33d5
movq 0x18(%rsp), %rdx
leaq 0x40(%rdx), %rcx
movl 0x7c(%rsp), %r8d
movl 0x80(%rsp), %r9d
movl 0x84(%rsp), %eax
movl %eax, (%rsp)
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
callq 0x2220
movq 0xd0(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq %r15, %rdi
callq 0x2f9a
jmp 0x3337
leaq 0xf16(%rip), %rdi       # 0x42f2
leaq 0xf20(%rip), %rsi       # 0x4303
movl $0x87, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2050
leaq 0x108(%rsp), %rdi
callq 0x2b36
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r14
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 %rbx, %rdi
callq 0x20b0
movq %r14, %rdi
callq 0x20b0
leaq 0x108(%rsp), %rdi
callq 0x2ad6
movl $0x30, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x30, %rdi
callq 0x2720
addq $-0x30, %rbx
cmpq $-0x30, %rbx
jne 0x3458
movl %ebp, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2595
movl %ebx, %ebp
jmp 0x3446
callq 0x2573
jmp 0x3446
movq %rax, %rbx
jmp 0x34aa
jmp 0x349a
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x2ad6
movl $0x30, %r14d
leaq (%rsp,%r14), %rdi
addq $0x30, %rdi
callq 0x2720
addq $-0x30, %r14
cmpq $-0x30, %r14
jne 0x34b0
jmp 0x34e4
movq %rax, %rbx
testq %r15, %r15
je 0x34e4
leaq (%rsp), %r14
leaq (%r14,%r15), %rdi
callq 0x2720
addq $-0x30, %r15
jne 0x34d5
movq %rbx, %rdi
callq 0x2170