#include <stdio.h>
#include <stdlib.h>

#define L_FRAME  80
#define STATE_INIT 0
#define STATE_MARK 1
#define STATE_UPDATE 2

void Usage() {
  fprintf(stderr,"Usage: \n");
  fprintf(stderr,"  mse file1 file2 ofile pfile tfile mfile\n");
  fprintf(stderr,"     file1, file2 are the files to be compared\n");
  fprintf(stderr,"     ofile contains the error signal\n");
  fprintf(stderr,"     pfile contains the markers for frame erasures\n");
  fprintf(stderr,"     tfile contains the convergence times\n");
  fprintf(stderr,"     mfile contains the average and peak mses\n");
}

void main(int argc, char *argv[]) {
  int i;
  FILE *fp1, *fp2, *fpout, *pfile, *tfile, *mfile;
  short buf1[L_FRAME], buf2[L_FRAME];
  int marker,framecount;
  double runningmse;
  int state;
  int burstsize, starttime, stoptime, threshold;
  int avgmse, countmse;
    
  double time;

  /* Process command line arguments */


  if(argc < 7) {
    Usage();
    exit(-1);
  }

  if((fp1 = fopen(argv[1],"r")) == NULL) {
    fprintf(stderr,"Error opening file %s\n",argv[1]);
    exit(-1);
  }
  if((fp2 = fopen(argv[2],"r")) == NULL) {
    fprintf(stderr,"Error opening file %s\n",argv[2]);
    exit(-1);
  }
  if((fpout = fopen(argv[3],"w")) == NULL) {
    fprintf(stderr,"Error opening file %s\n",argv[3]);
    exit(-1);
  }
  if((pfile = fopen(argv[4],"r")) == NULL) {
    fprintf(stderr,"Error opening file %s\n",argv[4]);
    exit(-1);
  }
  if((tfile = fopen(argv[5],"w")) == NULL) {
    fprintf(stderr,"Error opening file %s\n",argv[5]);
    exit(-1);
  }
  if((mfile = fopen(argv[6],"w")) == NULL) {
    fprintf(stderr,"Error opening file %s\n",argv[6]);
    exit(-1);
  }


  time = marker = framecount = runningmse = 0;
  avgmse = 0;
  countmse = 0;
  state = STATE_INIT;
  while(1) {
    if(fread(buf1, sizeof(short), L_FRAME, fp1) != L_FRAME) 
      break;
    if(fread(buf2, sizeof(short), L_FRAME, fp2) != L_FRAME) 
      break;
    fscanf(pfile,"%d\n",&marker);

    runningmse = 0;
    for(i = 0; i < L_FRAME; i++) {
      runningmse += ((buf1[i] - buf2[i])*(buf1[i] - buf2[i]));
    }
    runningmse /= L_FRAME;
    fprintf(fpout, "%d %f\n",framecount,runningmse);


    /* Now update state machine to find convergence time */

    if((state == STATE_INIT) && (marker == 1)) {
      threshold = runningmse;
      avgmse = runningmse;
      countmse = 1;
      burstsize = 1;
      state = STATE_MARK;
    }
    else if((state == STATE_MARK) && (marker == 0)) {
      if(runningmse > threshold)
	threshold = runningmse;
      state = STATE_UPDATE;

      starttime = framecount;
      stoptime = framecount;
      if(runningmse > (threshold * .01)) {
	stoptime = framecount+1;
	avgmse += runningmse;
	countmse++;
      }
    }
    else if((state == STATE_MARK) && (marker == 1)) {
      if(runningmse > threshold)
	threshold = runningmse;

      avgmse += runningmse;
      countmse++;

      ++burstsize;
    }
    else if((state == STATE_UPDATE) && (marker == 0)) {
      if(runningmse > threshold)
	threshold = runningmse;

      if(runningmse > (threshold * .01)) {
	stoptime = framecount+1;
	avgmse += runningmse;
	countmse++;
      }

    }
    else if((state == STATE_UPDATE) && (marker == 1)) {

      /* Output convergence time */

      fprintf(tfile,"%d %d\n",burstsize,(stoptime - starttime));
      fprintf(mfile,"%d %d\n",burstsize, (int) ((double) avgmse / countmse));

      threshold = runningmse;
      avgmse = runningmse;
      countmse = 1;
      burstsize = 1;
      state = STATE_MARK;
    }

    ++framecount;
  }
  printf("Framecount: %d\n",framecount);
  exit(0);
}

    
    

  

