// File: /workspace/github_cpp_makefile_star2_O2/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 $0x5d8, %rsp            # imm = 0x5D8
movq %rsi, %r15
leaq 0xb0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movl %edi, %ebp
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0xd0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq 0xa0(%rsp), %rbx
movb %cl, (%rax)
movabsq $0x12000000160, %rcx    # imm = 0x12000000160
movq %rcx, -0x38(%rax)
leaq 0x49a3(%rip), %rsi      # 0x9170
movq %rbx, %rdi
callq 0x4470
leaq 0xc0(%rsp), %r14
leaq 0x498c(%rip), %rsi      # 0x9170
movq %r14, %rdi
callq 0x4470
leaq 0x98(%rsp), %rdx
movb $0x0, 0x48(%rdx)
movl %ebp, %edi
movq %r15, %rsi
callq 0x463f
movl 0x9c(%rsp), %r15d
imull 0x98(%rsp), %r15d
movq 0xa0(%rsp), %rsi
leaq 0x1c8(%rsp), %rdi
pushq $0xc
popq %rdx
callq 0x4440
movq 0xc0(%rsp), %rsi
leaq 0x3d0(%rsp), %rdi
pushq $0xc
popq %rdx
callq 0x4440
movq 0x1c8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x1e8(%rsp,%rax)
je 0x486e
movq 0x9770(%rip), %rdi      # 0xdfd0
leaq 0x48ee(%rip), %rsi      # 0x9155
callq 0x4270
jmp 0x489a
movq 0x3d0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x3f0(%rsp,%rax)
je 0x48fa
movq 0x9745(%rip), %rdi      # 0xdfd0
leaq 0x48c3(%rip), %rsi      # 0x9155
callq 0x4270
movq %r14, %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4240
leaq 0x48c3(%rip), %rsi      # 0x916f
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4170
pushq $0x1
popq %rbx
leaq 0x3d0(%rsp), %rdi
callq 0x4070
leaq 0x1c8(%rsp), %rdi
callq 0x4070
leaq 0x98(%rsp), %rdi
callq 0x4f6e
movl %ebx, %eax
addq $0x5d8, %rsp            # imm = 0x5D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x0, 0x4(%rsp)
movl %r15d, %r13d
movq %r13, %rdi
callq 0x4030
movq %rax, %rbx
movq %r13, %rdi
callq 0x4030
movq %rax, %r14
andq $0x0, 0x48(%rsp)
andq $0x0, 0x40(%rsp)
andq $0x0, 0x38(%rsp)
movq 0x969f(%rip), %rdi      # 0xdfd0
leaq 0x4839(%rip), %rsi      # 0x9171
callq 0x4270
movq %rax, %rdi
callq 0x4170
movl %r15d, %ecx
shrl $0x2, %ecx
leal (%rcx,%rcx,4), %eax
movl %eax, 0x54(%rsp)
movl %r15d, %eax
shrl %eax
leal (%rax,%rax,2), %eax
movl %eax, 0x50(%rsp)
cvtsi2sd %r13, %xmm0
movsd %xmm0, 0x90(%rsp)
movq %rcx, 0x58(%rsp)
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x88(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
xorl %r12d, %r12d
movl %r15d, %r13d
movq 0x1c8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x1e8(%rsp,%rax)
jne 0x4b94
movl %r13d, %ebp
leaq 0x1c8(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x42e0
leaq 0x3d0(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x42e0
addl %r13d, %r12d
cmpl 0x54(%rsp), %r12d
leaq 0x40(%rsp), %rcx
leaq 0x38(%rsp), %rax
cmovaq %rax, %rcx
cmpl %r15d, %r12d
leaq 0x48(%rsp), %rax
cmovbeq %rax, %rcx
movq 0x58(%rsp), %rax
movl %eax, %ebp
cmoval %r13d, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0x45dc
movl %ebp, %r13d
cmpl 0x50(%rsp), %r12d
jne 0x499a
movsd 0x48(%rsp), %xmm3
movsd 0x40(%rsp), %xmm2
movsd 0x38(%rsp), %xmm1
cmpb $0x1, 0xe0(%rsp)
jne 0x4b49
movsd %xmm1, 0x70(%rsp)
movsd 0x88(%rsp), %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
movsd %xmm2, 0x68(%rsp)
divsd %xmm0, %xmm2
movsd %xmm2, 0x78(%rsp)
movsd %xmm3, 0x60(%rsp)
movapd %xmm3, %xmm0
divsd 0x90(%rsp), %xmm0
movsd 0x4593(%rip), %xmm1     # 0x9020
callq 0x460d
movsd %xmm0, 0x30(%rsp)
movsd 0x78(%rsp), %xmm0
movsd 0x457a(%rip), %xmm1     # 0x9020
callq 0x460d
movsd %xmm0, 0x28(%rsp)
movsd 0x80(%rsp), %xmm0
movsd 0x455e(%rip), %xmm1     # 0x9020
callq 0x460d
movsd %xmm0, 0x20(%rsp)
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
leaq 0x46c1(%rip), %rsi      # 0x91a0
callq 0x4e58
movq %r12, %rdi
leaq 0x4(%rsp), %rsi
callq 0x7ba5
movq %rax, %rdi
leaq 0x30(%rsp), %rsi
callq 0x896b
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x896b
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x896b
movq 0x94b1(%rip), %rdi      # 0xdfd0
movq %rax, %rsi
callq 0x4d3c
movq %rax, %rdi
callq 0x4170
movq %r12, %rdi
callq 0x4f3a
movsd 0x70(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movsd 0x60(%rsp), %xmm3
andq $0x0, 0x48(%rsp)
movsd 0x18(%rsp), %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, 0x18(%rsp)
andq $0x0, 0x40(%rsp)
movsd 0x8(%rsp), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rsp)
andq $0x0, 0x38(%rsp)
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
incl 0x4(%rsp)
jmp 0x4994
movl 0x4(%rsp), %eax
imull %eax, %r15d
cvtsi2sd %r15, %xmm1
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
movq 0x58(%rsp), %rcx
imull %eax, %ecx
cvtsi2sd %rcx, %xmm1
movsd 0x8(%rsp), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x10(%rsp), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movsd 0x4440(%rip), %xmm1     # 0x9020
callq 0x460d
movsd %xmm0, 0x30(%rsp)
movsd 0x8(%rsp), %xmm0
movsd 0x4427(%rip), %xmm1     # 0x9020
callq 0x460d
movsd %xmm0, 0x28(%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x440e(%rip), %xmm1     # 0x9020
callq 0x460d
movsd %xmm0, 0x20(%rsp)
leaq 0x4597(%rip), %rsi      # 0x91bb
leaq 0xe8(%rsp), %rdi
callq 0x4e58
leaq 0xe8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x896b
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x896b
leaq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0x896b
movq 0x936c(%rip), %rdi      # 0xdfd0
movq %rax, %rsi
callq 0x4d3c
movq %rax, %rdi
callq 0x4170
leaq 0xe8(%rsp), %rdi
callq 0x4f3a
movq %rbx, %rdi
callq 0x42f0
movq %r14, %rdi
callq 0x42f0
leaq 0x1c8(%rsp), %rdi
callq 0x4050
leaq 0x3d0(%rsp), %rdi
callq 0x4050
xorl %ebx, %ebx
jmp 0x48bf
jmp 0x4ce0
jmp 0x4ce0
jmp 0x4ce0
jmp 0x4ce0
jmp 0x4ce0
jmp 0x4ce0
jmp 0x4cc0
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x4f3a
jmp 0x4ce3
movq %rax, %rbx
jmp 0x4cf0
jmp 0x4cd9
movq %rax, %rbx
jmp 0x4cfd
jmp 0x4ce0
movq %rax, %rbx
leaq 0x3d0(%rsp), %rdi
callq 0x4070
leaq 0x1c8(%rsp), %rdi
callq 0x4070
leaq 0x98(%rsp), %rdi
callq 0x4f6e
movq %rbx, %rdi
callq 0x4430