// File: 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:
subq $0x7b8, %rsp            # imm = 0x7B8
movl $0x0, 0x7b4(%rsp)
movl %edi, 0x7b0(%rsp)
movq %rsi, 0x7a8(%rsp)
leaq 0x758(%rsp), %rdi
callq 0x6610
movl $0x160, 0x758(%rsp) # imm = 0x160
movl $0x120, 0x75c(%rsp) # imm = 0x120
leaq 0x760(%rsp), %rdi
leaq 0xe7f5(%rip), %rsi      # 0x14358
callq 0x5430
jmp 0x5b6a
leaq 0x780(%rsp), %rdi
leaq 0xe7df(%rip), %rsi      # 0x14358
callq 0x5430
jmp 0x5b80
movb $0x0, 0x7a0(%rsp)
movl 0x7b0(%rsp), %edi
movq 0x7a8(%rsp), %rsi
leaq 0x758(%rsp), %rdx
callq 0x58c0
jmp 0x5ba6
movl 0x758(%rsp), %eax
movl 0x75c(%rsp), %ecx
imull %ecx, %eax
movl %eax, 0x748(%rsp)
movl 0x748(%rsp), %eax
movl %eax, 0x744(%rsp)
movb $-0x1, 0x743(%rsp)
leaq 0x760(%rsp), %rdi
callq 0x5150
movq %rax, 0xe0(%rsp)
movl $0x8, %edi
movl $0x4, %esi
callq 0x6640
movl %eax, 0xec(%rsp)
jmp 0x5c01
movl 0xec(%rsp), %edx
movq 0xe0(%rsp), %rsi
leaq 0x538(%rsp), %rdi
callq 0x55b0
jmp 0x5c1f
leaq 0x780(%rsp), %rdi
callq 0x5150
movq %rax, 0xd0(%rsp)
movl $0x8, %edi
movl $0x4, %esi
callq 0x6640
movl %eax, 0xdc(%rsp)
jmp 0x5c4c
movl 0xdc(%rsp), %edx
movq 0xd0(%rsp), %rsi
leaq 0x330(%rsp), %rdi
callq 0x55b0
jmp 0x5c6a
movq 0x538(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x538(%rsp,%rax), %rdi
callq 0x51a0
movb %al, 0xcf(%rsp)
jmp 0x5c8c
movb 0xcf(%rsp), %al
testb $0x1, %al
jne 0x5c9c
jmp 0x5d72
movq 0x1532d(%rip), %rdi     # 0x1afd0
leaq 0xe693(%rip), %rsi      # 0x1433d
callq 0x5300
movq %rax, 0xc0(%rsp)
jmp 0x5cb9
movq 0xc0(%rsp), %rdi
leaq 0x760(%rsp), %rsi
callq 0x52b0
movq %rax, 0xb8(%rsp)
jmp 0x5cd8
movq 0xb8(%rsp), %rdi
leaq 0xe670(%rip), %rsi      # 0x14357
callq 0x5300
movq %rax, 0xb0(%rsp)
jmp 0x5cf6
movq 0xb0(%rsp), %rdi
movq 0x152ab(%rip), %rsi     # 0x1afb0
callq 0x5320
jmp 0x5d0c
movl $0x1, 0x7b4(%rsp)
movl $0x1, 0x32c(%rsp)
jmp 0x64fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x750(%rsp)
movl %eax, 0x74c(%rsp)
jmp 0x654c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x750(%rsp)
movl %eax, 0x74c(%rsp)
jmp 0x653f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x750(%rsp)
movl %eax, 0x74c(%rsp)
jmp 0x6532
movq 0x330(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x330(%rsp,%rax), %rdi
callq 0x51a0
movb %al, 0xaf(%rsp)
jmp 0x5d94
movb 0xaf(%rsp), %al
testb $0x1, %al
jne 0x5da4
jmp 0x5e2f
movq 0x15225(%rip), %rdi     # 0x1afd0
leaq 0xe58b(%rip), %rsi      # 0x1433d
callq 0x5300
movq %rax, 0xa0(%rsp)
jmp 0x5dc1
movq 0xa0(%rsp), %rdi
leaq 0x780(%rsp), %rsi
callq 0x52b0
movq %rax, 0x98(%rsp)
jmp 0x5de0
movq 0x98(%rsp), %rdi
leaq 0xe568(%rip), %rsi      # 0x14357
callq 0x5300
movq %rax, 0x90(%rsp)
jmp 0x5dfe
movq 0x90(%rsp), %rdi
movq 0x151a3(%rip), %rsi     # 0x1afb0
callq 0x5320
jmp 0x5e14
movl $0x1, 0x7b4(%rsp)
movl $0x1, 0x32c(%rsp)
jmp 0x64fc
movl $0x0, 0x328(%rsp)
movl $0x0, 0x324(%rsp)
movl 0x744(%rsp), %eax
movl %eax, %edi
callq 0x5030
movq %rax, 0x88(%rsp)
jmp 0x5e5d
movq 0x88(%rsp), %rax
movq %rax, 0x318(%rsp)
movl 0x744(%rsp), %eax
movl %eax, %edi
callq 0x5030
movq %rax, 0x80(%rsp)
jmp 0x5e85
movq 0x80(%rsp), %rax
movq %rax, 0x310(%rsp)
movq $0x0, 0x308(%rsp)
movq $0x0, 0x300(%rsp)
movq $0x0, 0x2f8(%rsp)
movq $0x0, 0x2f0(%rsp)
movq $0x0, 0x2e8(%rsp)
movq $0x0, 0x2e0(%rsp)
movq 0x150ec(%rip), %rdi     # 0x1afd0
leaq 0xe46e(%rip), %rsi      # 0x14359
callq 0x5300
movq %rax, 0x78(%rsp)
jmp 0x5ef7
movq 0x78(%rsp), %rdi
movq 0x150ad(%rip), %rsi     # 0x1afb0
callq 0x5320
jmp 0x5f0a
jmp 0x5f0c
movq 0x538(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x538(%rsp,%rax), %rdi
callq 0x55c0
movb %al, 0x77(%rsp)
jmp 0x5f2b
movb 0x77(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x5f3a
jmp 0x62e4
movq 0x318(%rsp), %rsi
movl 0x744(%rsp), %eax
movl %eax, %edx
leaq 0x538(%rsp), %rdi
callq 0x53b0
jmp 0x5f5a
movq 0x310(%rsp), %rsi
movl 0x744(%rsp), %eax
movl %eax, %edx
leaq 0x330(%rsp), %rdi
callq 0x53b0
jmp 0x5f7a
movl 0x744(%rsp), %eax
addl 0x328(%rsp), %eax
movl %eax, 0x328(%rsp)
movl 0x328(%rsp), %eax
cmpl 0x748(%rsp), %eax
ja 0x5fd9
movq 0x318(%rsp), %rdi
movq 0x310(%rsp), %rsi
movl 0x744(%rsp), %edx
leaq 0x2f0(%rsp), %rcx
callq 0x57d0
movl 0x748(%rsp), %eax
shrl $0x2, %eax
movl %eax, 0x744(%rsp)
jmp 0x6065
movl 0x748(%rsp), %eax
cmpl 0x328(%rsp), %eax
jae 0x6027
movl 0x328(%rsp), %eax
movl 0x748(%rsp), %ecx
shrl $0x2, %ecx
imull $0x5, %ecx, %ecx
cmpl %ecx, %eax
ja 0x6027
movq 0x318(%rsp), %rdi
movq 0x310(%rsp), %rsi
movl 0x744(%rsp), %edx
leaq 0x2e8(%rsp), %rcx
callq 0x57d0
jmp 0x6063
movl 0x748(%rsp), %eax
shrl $0x2, %eax
imull $0x5, %eax, %eax
cmpl 0x328(%rsp), %eax
jae 0x6061
movq 0x318(%rsp), %rdi
movq 0x310(%rsp), %rsi
movl 0x744(%rsp), %edx
leaq 0x2e0(%rsp), %rcx
callq 0x57d0
jmp 0x6063
jmp 0x6065
movl 0x328(%rsp), %eax
movl 0x748(%rsp), %ecx
shrl %ecx
imull $0x3, %ecx, %ecx
cmpl %ecx, %eax
jne 0x62df
movsd 0x2f0(%rsp), %xmm1
movsd 0x308(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x308(%rsp)
movsd 0x2e8(%rsp), %xmm1
movsd 0x300(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x300(%rsp)
movsd 0x2e0(%rsp), %xmm1
movsd 0x2f8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2f8(%rsp)
movl 0x748(%rsp), %eax
cvtsi2sd %rax, %xmm1
movsd 0x2f0(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x2f0(%rsp)
movl 0x748(%rsp), %eax
shrl $0x2, %eax
cvtsi2sd %eax, %xmm1
movsd 0x2e8(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x2e8(%rsp)
movl 0x748(%rsp), %eax
shrl $0x2, %eax
cvtsi2sd %eax, %xmm1
movsd 0x2e0(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x2e0(%rsp)
testb $0x1, 0x7a0(%rsp)
je 0x6291
movsd 0x2f0(%rsp), %xmm0
movsd 0xdeba(%rip), %xmm1     # 0x14020
movsd %xmm1, 0x68(%rsp)
callq 0x5850
movsd 0x68(%rsp), %xmm1
movsd %xmm0, 0x2d8(%rsp)
movsd 0x2e8(%rsp), %xmm0
callq 0x5850
movsd 0x68(%rsp), %xmm1
movsd %xmm0, 0x2d0(%rsp)
movsd 0x2e0(%rsp), %xmm0
callq 0x5850
movsd %xmm0, 0x2c8(%rsp)
leaq 0xe1cd(%rip), %rsi      # 0x14388
leaq 0x1e8(%rsp), %rdi
callq 0x6830
jmp 0x61ca
leaq 0x1e8(%rsp), %rdi
leaq 0x324(%rsp), %rsi
callq 0x69d0
movq %rax, 0x60(%rsp)
jmp 0x61e6
movq 0x60(%rsp), %rdi
leaq 0x2d8(%rsp), %rsi
callq 0x6a00
movq %rax, 0x58(%rsp)
jmp 0x61ff
movq 0x58(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
callq 0x6a00
movq %rax, 0x50(%rsp)
jmp 0x6218
movq 0x50(%rsp), %rdi
leaq 0x2c8(%rsp), %rsi
callq 0x6a00
movq %rax, 0x48(%rsp)
jmp 0x6231
movq 0x48(%rsp), %rsi
movq 0x14d93(%rip), %rdi     # 0x1afd0
callq 0x6660
movq %rax, 0x40(%rsp)
jmp 0x6249
movq 0x40(%rsp), %rdi
movq 0x14d5b(%rip), %rsi     # 0x1afb0
callq 0x5320
jmp 0x625c
leaq 0x1e8(%rsp), %rdi
callq 0x6a30
jmp 0x6291
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x750(%rsp)
movl %eax, 0x74c(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x6a30
jmp 0x6532
xorps %xmm0, %xmm0
movsd %xmm0, 0x2f0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x2e8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x2e0(%rsp)
movl 0x324(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x324(%rsp)
movl $0x0, 0x328(%rsp)
movl 0x748(%rsp), %eax
movl %eax, 0x744(%rsp)
jmp 0x5f0c
movl 0x748(%rsp), %eax
movl 0x324(%rsp), %ecx
imull %ecx, %eax
cvtsi2sd %rax, %xmm1
movsd 0x308(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x308(%rsp)
movl 0x748(%rsp), %eax
shrl $0x2, %eax
movl 0x324(%rsp), %ecx
imull %ecx, %eax
cvtsi2sd %rax, %xmm1
movsd 0x300(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x300(%rsp)
movl 0x748(%rsp), %eax
shrl $0x2, %eax
movl 0x324(%rsp), %ecx
imull %ecx, %eax
cvtsi2sd %rax, %xmm1
movsd 0x2f8(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x2f8(%rsp)
movsd 0x308(%rsp), %xmm0
movsd 0xdca1(%rip), %xmm1     # 0x14020
movsd %xmm1, 0x38(%rsp)
callq 0x5850
movsd 0x38(%rsp), %xmm1
movsd %xmm0, 0x1e0(%rsp)
movsd 0x300(%rsp), %xmm0
callq 0x5850
movsd 0x38(%rsp), %xmm1
movsd %xmm0, 0x1d8(%rsp)
movsd 0x2f8(%rsp), %xmm0
callq 0x5850
movsd %xmm0, 0x1d0(%rsp)
leaq 0xdfcf(%rip), %rsi      # 0x143a3
leaq 0xf0(%rsp), %rdi
callq 0x6830
jmp 0x63e3
leaq 0xf0(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x6a00
movq %rax, 0x30(%rsp)
jmp 0x63ff
movq 0x30(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x6a00
movq %rax, 0x28(%rsp)
jmp 0x6418
movq 0x28(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x6a00
movq %rax, 0x20(%rsp)
jmp 0x6431
movq 0x20(%rsp), %rsi
movq 0x14b93(%rip), %rdi     # 0x1afd0
callq 0x6660
movq %rax, 0x18(%rsp)
jmp 0x6449
movq 0x18(%rsp), %rdi
movq 0x14b5b(%rip), %rsi     # 0x1afb0
callq 0x5320
jmp 0x645c
leaq 0xf0(%rsp), %rdi
callq 0x6a30
movq 0x318(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x6486
movq 0x10(%rsp), %rdi
callq 0x53f0
movq 0x310(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x64a3
movq 0x8(%rsp), %rdi
callq 0x53f0
leaq 0x538(%rsp), %rdi
callq 0x5060
jmp 0x64b2
leaq 0x330(%rsp), %rdi
callq 0x5060
jmp 0x64c1
movl $0x0, 0x7b4(%rsp)
movl $0x1, 0x32c(%rsp)
jmp 0x64fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x750(%rsp)
movl %eax, 0x74c(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x6a30
jmp 0x6532
leaq 0x330(%rsp), %rdi
callq 0x5080
leaq 0x538(%rsp), %rdi
callq 0x5080
leaq 0x758(%rsp), %rdi
callq 0x6a90
movl 0x7b4(%rsp), %eax
addq $0x7b8, %rsp            # imm = 0x7B8
retq
leaq 0x330(%rsp), %rdi
callq 0x5080
leaq 0x538(%rsp), %rdi
callq 0x5080
leaq 0x758(%rsp), %rdi
callq 0x6a90
movq 0x750(%rsp), %rdi
callq 0x5580
nopw %cs:(%rax,%rax)