// File: /workspace/github_cpp_makefile_star2_O2/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, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x241c
leaq 0x50(%rsp), %rdi
callq 0x241c
cmpl $0x3, %ebp
jge 0x3053
pushq $-0x1
popq %rbp
jmp 0x331f
jne 0x305a
pushq $0x2
popq %rsi
jmp 0x308d
movq 0x18(%rbx), %r14
leaq 0x125c(%rip), %rsi      # 0x42c1
movq %r14, %rdi
callq 0x21c0
testl %eax, %eax
je 0x308b
leaq 0x124e(%rip), %rsi      # 0x42c6
movq %r14, %rdi
callq 0x21c0
cmpl $0x1, %eax
pushq $0x2
popq %rsi
sbbl $0x0, %esi
jmp 0x308d
xorl %esi, %esi
leaq 0x108(%rsp), %rdi
callq 0x28b4
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %r15
leaq 0x1206(%rip), %rdi      # 0x42af
leaq 0x121b(%rip), %rsi      # 0x42cb
callq 0x22b0
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0x3312
movq %rax, %rbx
movq %rax, 0xf8(%rsp)
leaq 0x11e5(%rip), %rdi      # 0x42b8
leaq 0x11f1(%rip), %rsi      # 0x42cb
callq 0x22b0
testq %rax, %rax
je 0x3312
movq %rbx, 0x88(%rsp)
leaq 0xf8(%rsp), %rdx
movq %rax, 0x80(%rsp)
movq %rax, 0x8(%rdx)
leaq 0x2c9(%rip), %rcx       # 0x33d4
movq %rcx, 0x160(%rsp)
leaq 0x313(%rip), %rcx       # 0x342d
movq %rcx, 0x168(%rsp)
movq %rdx, 0x170(%rsp)
callq 0x245a
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 0x2460
testl %eax, %eax
js 0x3312
movl 0x3c(%rsp), %r8d
movl 0x40(%rsp), %edx
movl 0x44(%rsp), %ecx
leaq 0xd0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
pushq $0x1
popq %r9
callq 0x2110
movl %eax, %ebx
testl %eax, %eax
js 0x334d
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x2460
testl %eax, %eax
js 0x3312
movl 0x6c(%rsp), %r8d
movl 0x70(%rsp), %edx
movl 0x74(%rsp), %ecx
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
pushq $0x1
popq %r9
callq 0x2110
movl %eax, %ebp
testl %eax, %eax
js 0x336d
movl 0x3c(%rsp), %ecx
movl 0x40(%rsp), %esi
movl 0x44(%rsp), %edx
leaq 0x108(%rsp), %rdi
callq 0x28d8
movq %r12, %r15
addq $0x40, %r15
movq %r13, %rbx
addq $0x40, %rbx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x27ee
movl %eax, %ebp
testl %eax, %eax
js 0x32bd
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 0x27ee
movl %eax, %ebp
testl %eax, %eax
js 0x32a1
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 %rbx, %rcx
callq 0x2220
movq 0xd0(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x108(%rsp), %rdi
callq 0x2e82
jmp 0x31ff
leaq 0x104a(%rip), %rdi      # 0x42f2
leaq 0x1054(%rip), %rsi      # 0x4303
movl $0x87, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2050
leaq 0x108(%rsp), %rdi
callq 0x2936
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 0x28d6
pushq $0x30
popq %rbx
leaq (%rsp,%rbx), %rdi
addq $0x20, %rdi
callq 0x243a
addq $-0x30, %rbx
cmpq $-0x30, %rbx
jne 0x3322
movl %ebp, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c8c(%rip), %rax      # 0x5fe0
movq (%rax), %rcx
leaq 0xf6f(%rip), %rdi       # 0x42cd
pushq $0x24
popq %rsi
pushq $0x1
popq %rdx
callq 0x2290
movl %ebx, %ebp
jmp 0x3312
movq 0x2c6c(%rip), %rax      # 0x5fe0
movq (%rax), %rcx
leaq 0xf4f(%rip), %rdi       # 0x42cd
pushq $0x24
popq %rsi
pushq $0x1
popq %rdx
callq 0x2290
jmp 0x3312
movq %rax, %rbx
jmp 0x33b1
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x243a
jmp 0x33cc
jmp 0x33a1
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x28d6
pushq $0x30
popq %r14
leaq (%rsp,%r14), %rdi
addq $0x20, %rdi
callq 0x243a
addq $-0x30, %r14
cmpq $-0x30, %r14
jne 0x33b5
movq %rbx, %rdi
callq 0x2170