// File: /workspace/github_cpp_makefile_star2_O1/Jiro-iwai[P]yuv2psnr/yuv2psnr.cpp
// Function: main
int main(int argc, char* argv[])
{
  Option opt;
  // CIF
  opt.width = 352;
  opt.height = 288;
  // HD
  //int width  = 1920;
  //int height = 1080;
  opt.file0 = "";
  opt.file1 = "";
  opt.showFrame = false;
  parse_option(argc, argv, opt);

  const unsigned int area = opt.width*opt.height;
  //const unsigned int min_read_unit = 16;
  //const unsigned int min_read_unit = 8;
  unsigned int min_read_unit = area;
  const unsigned char MAX = 255;

  ifstream fin0( opt.file0.c_str(), ios::in | ios::binary );
  ifstream fin1( opt.file1.c_str(), ios::in | ios::binary );
    
  if( !fin0 ){
    cout << "Error: cannot open file `" << opt.file0 << "'" << endl;
    return 1;
  }

  if( !fin1 ){
    cout << "Error: cannot open file `" << opt.file1 << "'" << endl;
    return 1;
  }

  int count = 0;    
  int nframe = 0;
  unsigned char* c0 = new unsigned char[min_read_unit];
  unsigned char* c1 = new unsigned char[min_read_unit];

  double totalYMSE = 0.0;
  double totalUMSE = 0.0;
  double totalVMSE = 0.0;

  double frameYMSE = 0.0;
  double frameUMSE = 0.0;
  double frameVMSE = 0.0;

  cout << "Frame       Y-PSNR[dB]  U-PSNR[dB]  V-PSNR[dB]" << endl;

  while( !fin0.eof() ){

    fin0.read( (char*)c0, min_read_unit ); 
    fin1.read( (char*)c1, min_read_unit ); 
    count += min_read_unit;

    // extract Y
    if( count <= area ) { 
      MSE(c0, c1, min_read_unit, frameYMSE);
      min_read_unit = area/4;

    // extract U
    } else if( area < count && count <= area/4*5 ) { // YUV420
      MSE(c0, c1, min_read_unit, frameUMSE);

    // extract V
    } else if( area/4*5 < count ) { // YUV420
      MSE(c0, c1, min_read_unit, frameVMSE);
    }

    // end of 1 frame
    if( count == area/2*3 ) {

      totalYMSE += frameYMSE;
      totalUMSE += frameUMSE;
      totalVMSE += frameVMSE;

      frameYMSE /= area;
      frameUMSE /= area/4;
      frameVMSE /= area/4;

      if( opt.showFrame ) {

        double y = MSE2PSNR(frameYMSE, MAX);
        double u = MSE2PSNR(frameUMSE, MAX);
        double v = MSE2PSNR(frameVMSE, MAX);
        cout << boost::format("%-12d%-12.4f%-12.4f%-12.4f") % nframe % y % u % v << endl;
      }

      frameYMSE = 0.0;
      frameUMSE = 0.0;
      frameVMSE = 0.0;

      nframe++;
      count = 0;
      min_read_unit = area;
    }
  }

  totalYMSE /= area*nframe;
  totalUMSE /= area/4*nframe;
  totalVMSE /= area/4*nframe;

  double y = MSE2PSNR(totalYMSE, MAX);
  double u = MSE2PSNR(totalUMSE, MAX);
  double v = MSE2PSNR(totalVMSE, MAX);
  cout << boost::format("Total       %-12.4f%-12.4f%-12.4f") % y % u % v << endl;

  delete [] c0;
  delete [] c1;

  fin0.close();
  fin1.close();
    
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5c8, %rsp            # imm = 0x5C8
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %r15
movq %r15, -0x10(%r15)
xorl %eax, %eax
movq %rax, -0x8(%r15)
movb %al, (%r15)
leaq 0xc0(%rsp), %r13
movq %r13, -0x10(%r13)
movq %rax, -0x8(%r13)
movb %al, (%r13)
movabsq $0x12000000160, %rax    # imm = 0x12000000160
movq %rax, -0x38(%r13)
leaq 0x6867(%rip), %rcx      # 0xb170
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x4340
leaq 0xb0(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x6848(%rip), %rcx      # 0xb170
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x4340
leaq 0x88(%rsp), %rdx
movb $0x0, 0x48(%rdx)
movl %ebp, %edi
movq %rbx, %rsi
callq 0x45f4
movl 0x8c(%rsp), %ebp
imull 0x88(%rsp), %ebp
movq 0x90(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
movl $0xc, %edx
callq 0x43f0
movq 0xb0(%rsp), %rsi
leaq 0x3c0(%rsp), %rdi
movl $0xc, %edx
callq 0x43f0
movq 0x1b8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x1d8(%rsp,%rax)
je 0x4a02
movq 0xb628(%rip), %rdi      # 0xffd0
leaq 0x67a6(%rip), %rsi      # 0xb155
movl $0x19, %edx
callq 0x4280
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
movq 0xb600(%rip), %rdi      # 0xffd0
callq 0x4280
movq %rax, %rbx
leaq 0x6790(%rip), %rsi      # 0xb16f
movl $0x1, %edx
movq %rax, %rdi
callq 0x4280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4240
jmp 0x4a7b
movq 0x3c0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x3e0(%rsp,%rax)
je 0x4ae5
movq 0xb5ad(%rip), %rdi      # 0xffd0
leaq 0x672b(%rip), %rsi      # 0xb155
movl $0x19, %edx
callq 0x4280
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movq 0xb585(%rip), %rdi      # 0xffd0
callq 0x4280
movq %rax, %rbx
leaq 0x6715(%rip), %rsi      # 0xb16f
movl $0x1, %edx
movq %rax, %rdi
callq 0x4280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4240
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movl $0x1, %ebx
movq %rax, %rdi
callq 0x41d0
leaq 0x3c0(%rsp), %rdi
callq 0x4080
leaq 0x1b8(%rsp), %rdi
callq 0x4080
movq 0xb0(%rsp), %rdi
cmpq %r13, %rdi
je 0x4abf
callq 0x4230
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x4ad1
callq 0x4230
movl %ebx, %eax
addq $0x5c8, %rsp            # imm = 0x5C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x14(%rsp)
movl %ebp, %r15d
movq %r15, %rdi
callq 0x4030
movq %rax, %rbx
movq %r15, %rdi
callq 0x4030
movq %rax, %r14
movq 0xb4c3(%rip), %r12      # 0xffd0
leaq 0x665d(%rip), %rsi      # 0xb171
movl $0x2e, %edx
movq %r12, %rdi
callq 0x4280
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x4240
movsbl %al, %esi
movq 0xb490(%rip), %rdi      # 0xffd0
callq 0x4040
movq %rax, %rdi
callq 0x41d0
movq 0x1b8(%rsp), %rax
movq -0x18(%rax), %rax
xorpd %xmm3, %xmm3
testb $0x2, 0x1d8(%rsp,%rax)
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
jne 0x4fb9
movl %ebp, %ecx
shrl $0x2, %ecx
leal (%rcx,%rcx,4), %eax
movl %eax, 0x68(%rsp)
movl %ebp, %eax
shrl %eax
leal (%rax,%rax,2), %eax
movl %eax, 0x6c(%rsp)
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x80(%rsp)
movq %rcx, 0x78(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x70(%rsp)
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
xorpd %xmm6, %xmm6
xorl %r15d, %r15d
movl %ebp, %r13d
movsd %xmm6, 0x40(%rsp)
movsd %xmm5, 0x20(%rsp)
movsd %xmm4, (%rsp)
movsd %xmm3, 0x48(%rsp)
movsd %xmm2, 0x18(%rsp)
movsd %xmm1, 0x8(%rsp)
movl %r13d, %r12d
leaq 0x1b8(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x42b0
leaq 0x3c0(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x42b0
addl %r13d, %r15d
cmpl %ebp, %r15d
jbe 0x4c6c
cmpl 0x68(%rsp), %r15d
movsd 0x8(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movsd (%rsp), %xmm4
movsd 0x20(%rsp), %xmm5
movsd 0x40(%rsp), %xmm6
jbe 0x4cc1
testl %r13d, %r13d
je 0x4ce9
xorl %eax, %eax
movzbl (%rbx,%rax), %ecx
movzbl (%r14,%rax), %edx
subl %edx, %ecx
imull %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
addsd %xmm0, %xmm4
incq %rax
cmpq %rax, %r12
jne 0x4c49
jmp 0x4ce9
testl %r13d, %r13d
movsd 0x8(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movsd (%rsp), %xmm4
movsd 0x20(%rsp), %xmm5
movsd 0x40(%rsp), %xmm6
je 0x4cb7
xorl %eax, %eax
movzbl (%rbx,%rax), %ecx
movzbl (%r14,%rax), %edx
subl %edx, %ecx
imull %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
addsd %xmm0, %xmm6
incq %rax
cmpq %rax, %r12
jne 0x4c96
movq 0x78(%rsp), %rax
movl %eax, %r13d
jmp 0x4ce9
testl %r13d, %r13d
je 0x4ce9
xorl %eax, %eax
movzbl (%rbx,%rax), %ecx
movzbl (%r14,%rax), %edx
subl %edx, %ecx
imull %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
addsd %xmm0, %xmm5
incq %rax
cmpq %rax, %r12
jne 0x4cc8
cmpl 0x6c(%rsp), %r15d
jne 0x4f9f
cmpb $0x1, 0xd0(%rsp)
jne 0x4f7d
movsd %xmm6, 0x40(%rsp)
movapd %xmm6, %xmm1
divsd 0x80(%rsp), %xmm1
ucomisd 0x62f3(%rip), %xmm1     # 0xb010
movsd 0x62e3(%rip), %xmm0     # 0xb008
movsd %xmm4, (%rsp)
movsd %xmm5, 0x20(%rsp)
jbe 0x4d56
movsd 0x62e6(%rip), %xmm0     # 0xb020
divsd %xmm1, %xmm0
callq 0x4170
movsd 0x20(%rsp), %xmm5
movsd (%rsp), %xmm4
mulsd 0x62c2(%rip), %xmm0     # 0xb018
movapd %xmm5, %xmm1
divsd 0x70(%rsp), %xmm1
movsd %xmm0, 0x60(%rsp)
ucomisd 0x62a2(%rip), %xmm1     # 0xb010
movsd 0x6292(%rip), %xmm0     # 0xb008
leaq 0xd8(%rsp), %r15
leaq 0x28(%rsp), %r12
leaq 0x4ef1(%rip), %r13      # 0x9c7b
jbe 0x4daa
movsd 0x628c(%rip), %xmm0     # 0xb020
divsd %xmm1, %xmm0
callq 0x4170
movsd (%rsp), %xmm4
mulsd 0x626e(%rip), %xmm0     # 0xb018
movapd %xmm4, %xmm1
divsd 0x70(%rsp), %xmm1
movsd %xmm0, 0x58(%rsp)
ucomisd 0x624e(%rip), %xmm1     # 0xb010
movsd 0x623e(%rip), %xmm0     # 0xb008
jbe 0x4de5
movsd 0x624c(%rip), %xmm0     # 0xb020
divsd %xmm1, %xmm0
callq 0x4170
mulsd 0x6233(%rip), %xmm0     # 0xb018
movsd %xmm0, 0x50(%rsp)
movq %r15, %rdi
leaq 0x63ab(%rip), %rsi      # 0xb1a0
callq 0x53f4
leaq 0x14(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x4e68(%rip), %rax      # 0x9c73
movq %rax, 0x30(%rsp)
leaq 0x4e5d(%rip), %rax      # 0x9c74
movq %rax, 0x38(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x8bde
leaq 0x60(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq %r13, 0x30(%rsp)
leaq 0x4e3f(%rip), %rcx      # 0x9c7c
movq %rcx, 0x38(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x8bde
leaq 0x58(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq %r13, 0x30(%rsp)
leaq 0x4e19(%rip), %rcx      # 0x9c7c
movq %rcx, 0x38(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x8bde
leaq 0x50(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq %r13, 0x30(%rsp)
leaq 0x4df3(%rip), %rcx      # 0x9c7c
movq %rcx, 0x38(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x8bde
movq 0xb130(%rip), %rdi      # 0xffd0
movq %rax, %rsi
callq 0x5295
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4240
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41d0
cmpb $0x1, 0x1a8(%rsp)
jne 0x4ef1
leaq 0x1b0(%rsp), %rdi
callq 0x4310
movb $0x0, 0x1a8(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x55de
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x4f18
callq 0x4230
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x4f52
callq 0x4230
xorl %eax, %eax
movq %rax, 0xf0(%rsp)
movl %eax, 0xf8(%rsp)
movq %rax, 0x100(%rsp)
movl %eax, 0x108(%rsp)
movq %rax, 0x110(%rsp)
movq %r15, %rdi
callq 0x5632
movsd 0x8(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movsd (%rsp), %xmm4
movsd 0x20(%rsp), %xmm5
movsd 0x40(%rsp), %xmm6
addsd %xmm6, %xmm3
addsd %xmm5, %xmm2
addsd %xmm4, %xmm1
incl 0x14(%rsp)
xorl %r15d, %r15d
xorpd %xmm6, %xmm6
xorpd %xmm5, %xmm5
xorpd %xmm4, %xmm4
movl %ebp, %r13d
movq 0x1b8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x1d8(%rsp,%rax)
je 0x4bbc
movl 0x14(%rsp), %eax
movl %eax, %ecx
imull %ebp, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
divsd %xmm0, %xmm3
shrl $0x2, %ebp
imull %eax, %ebp
xorps %xmm5, %xmm5
cvtsi2sd %rbp, %xmm5
divsd %xmm5, %xmm2
movsd 0x6020(%rip), %xmm4     # 0xb008
ucomisd 0x6020(%rip), %xmm3     # 0xb010
movapd %xmm4, %xmm0
jbe 0x5039
movsd 0x6022(%rip), %xmm0     # 0xb020
divsd %xmm3, %xmm0
movsd %xmm1, 0x8(%rsp)
movsd %xmm2, 0x18(%rsp)
movsd %xmm5, (%rsp)
callq 0x4170
movsd (%rsp), %xmm5
movsd 0x5fe3(%rip), %xmm4     # 0xb008
movsd 0x18(%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
mulsd 0x5fdf(%rip), %xmm0     # 0xb018
divsd %xmm5, %xmm1
movsd %xmm0, 0x60(%rsp)
ucomisd 0x5fc5(%rip), %xmm2     # 0xb010
movapd %xmm4, %xmm0
jbe 0x507e
movsd 0x5fc7(%rip), %xmm0     # 0xb020
divsd %xmm2, %xmm0
movsd %xmm1, 0x8(%rsp)
callq 0x4170
movsd 0x5f98(%rip), %xmm4     # 0xb008
movsd 0x8(%rsp), %xmm1
mulsd 0x5f9a(%rip), %xmm0     # 0xb018
movsd %xmm0, 0x58(%rsp)
ucomisd 0x5f84(%rip), %xmm1     # 0xb010
jbe 0x50ab
movsd 0x5f8a(%rip), %xmm0     # 0xb020
divsd %xmm1, %xmm0
callq 0x4170
movapd %xmm0, %xmm4
mulsd 0x5f6d(%rip), %xmm4     # 0xb018
movsd %xmm4, 0x50(%rsp)
leaq 0x6103(%rip), %rsi      # 0xb1bb
leaq 0xd8(%rsp), %rdi
callq 0x53f4
leaq 0xc0(%rsp), %r13
leaq 0x60(%rsp), %rax
leaq 0x28(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x4b9a(%rip), %r15      # 0x9c7b
movq %r15, 0x8(%rsi)
leaq 0x4b90(%rip), %r12      # 0x9c7c
movq %r12, 0x10(%rsi)
leaq 0xd8(%rsp), %rdi
callq 0x8bde
leaq 0x58(%rsp), %rcx
leaq 0x28(%rsp), %rsi
movq %rcx, (%rsi)
movq %r15, 0x8(%rsi)
movq %r12, 0x10(%rsi)
movq %rax, %rdi
callq 0x8bde
leaq 0x50(%rsp), %rcx
leaq 0x28(%rsp), %rsi
movq %rcx, (%rsi)
movq %r15, 0x8(%rsi)
movq %r12, 0x10(%rsi)
movq %rax, %rdi
callq 0x8bde
movq 0xae92(%rip), %rdi      # 0xffd0
movq %rax, %rsi
callq 0x5295
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4240
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41d0
cmpb $0x1, 0x1a8(%rsp)
jne 0x518f
leaq 0x1b0(%rsp), %r15
movq %r15, %rdi
callq 0x4310
movb $0x0, -0x8(%r15)
leaq 0x150(%rsp), %r15
movq %r15, %rdi
callq 0x55de
movq -0x28(%r15), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x51b5
callq 0x4230
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
leaq 0xa0(%rsp), %r15
je 0x51f7
callq 0x4230
xorl %eax, %eax
movq %rax, 0xf0(%rsp)
movl %eax, 0xf8(%rsp)
movq %rax, 0x100(%rsp)
movl %eax, 0x108(%rsp)
movq %rax, 0x110(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x5632
movq %rbx, %rdi
callq 0x42c0
movq %r14, %rdi
callq 0x42c0
leaq 0x1b8(%rsp), %rdi
callq 0x4060
leaq 0x3c0(%rsp), %rdi
callq 0x4060
xorl %ebx, %ebx
jmp 0x4a93
jmp 0x5263
jmp 0x5263
jmp 0x5263
jmp 0x5263
jmp 0x5263
jmp 0x5263
jmp 0x5251
movq %rax, %rbx
jmp 0x5273
jmp 0x524a
movq %rax, %rbx
jmp 0x5280
jmp 0x5263
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x5552
jmp 0x5266
movq %rax, %rbx
leaq 0x3c0(%rsp), %rdi
callq 0x4080
leaq 0x1b8(%rsp), %rdi
callq 0x4080
leaq 0x88(%rsp), %rdi
callq 0x55a8
movq %rbx, %rdi
callq 0x43d0