diff -urN old/mpeg_encode/Makefile mpeg_encode/Makefile
--- old/mpeg_encode/Makefile	Thu Aug 17 00:16:56 1995
+++ mpeg_encode/Makefile	Wed Apr 29 17:50:24 1998
@@ -36,17 +36,21 @@
 ##############
 # JPEG STUFF #
 ##############
-JPEG_LIB = jpeg/libjpeg.a
+JPEG_LIB = -ljpeg
 JPEG_DIR = -Ijpeg
 JPEG_LIB_DIR = -Ljpeg
 MP_JPEG_OBJS = jpeg.o
 MP_JPEG_SRCS = jpeg.c
+#MP_JPEG_OBJS = nojpeg.o
+#MP_JPEG_SRCS = nojpeg.c
 
 ####################################################################
 # DEBUGGING # use -g if and only if you want to debug, else use -O #
 ####################################################################
 #DEBUGFLAG = -g 
-DEBUGFLAG = -O
+#DEBUGFLAG = -O3 -mpentium -fomit-frame-pointer -ffast-math 
+DEBUGFLAG = -O2 -mpentium -DLONG_32 -DSYSV -DLINUX
+
 
 ###############################################################################
 # MISSING PROTOTYES # use -DMISSING_PROTOS if your standard headers are wrong #
@@ -68,7 +72,7 @@
 #####################################################################
 # COMPILER # specify compiler; should be ANSI-compliant if possible #
 #####################################################################
-CC = gcc
+CC = gccold
 
 ################################################
 # MAKE # command to use for the "make" command #
@@ -263,3 +267,559 @@
 
 ##############################################################################
 # DO NOT DELETE THIS LINE -- make depend depends on it.
diff -urN old/mpeg_encode/bframe.c mpeg_encode/bframe.c
--- old/mpeg_encode/bframe.c	Tue Aug 15 00:28:26 1995
+++ mpeg_encode/bframe.c	Tue Apr 28 19:02:31 1998
@@ -385,7 +385,7 @@
 	      goto skipsearch;
 	    }}
 	gosearch:		/* do bsearch */
-	  mode = BMotionSearch(currentBlock, prev, next, y, x, &fMotionY,
+	  mode = BMotionSearch(curr, currentBlock, prev, next, y, x, &fMotionY,
 			       &fMotionX, &bMotionY, &bMotionX, mode);
 	skipsearch:	      
 	      
diff -urN old/mpeg_encode/bsearch.c mpeg_encode/bsearch.c
--- old/mpeg_encode/bsearch.c	Mon Aug  7 23:49:10 1995
+++ mpeg_encode/bsearch.c	Wed Apr 29 09:54:18 1998
@@ -87,6 +87,7 @@
  *==================*/
 
 static int	bsearchAlg;
+extern int nguesses,nfails;
 
 
 /*===============================*
@@ -222,8 +223,10 @@
  * NOTES:	the search procedure MAY return (0,0) motion vectors
  *
  *===========================================================================*/
+    MpegFrame *currentMPEG;
 int
-BMotionSearch(currentBlock, prev, next, by, bx, fmy, fmx, bmy, bmx, oldMode)
+BMotionSearch(current, currentBlock, prev, next, by, bx, fmy, fmx, bmy, bmx, oldMode)
+    MpegFrame *current;
     LumBlock currentBlock;
     MpegFrame *prev;
     MpegFrame *next;
@@ -235,25 +238,27 @@
     int *bmx;
     int oldMode;
 {
+  int diff, fx, fy;
   /* If we are an initial B frame, no possibility of forward motion */
   if (prev == (MpegFrame *) NULL) {
-    PMotionSearch(currentBlock, next, by, bx, bmy, bmx);
+    PMotionSearch(current, currentBlock, next, by, bx, bmy, bmx);
     return MOTION_BACKWARD;
   }
+  currentMPEG=current;
   
   /* otherwise simply call the appropriate algorithm, based on user preference */
   
     switch(bsearchAlg) {
 	case BSEARCH_SIMPLE:
-	    return BMotionSearchSimple(currentBlock, prev, next, by, bx, fmy,
+	    diff=BMotionSearchSimple(currentBlock, prev, next, by, bx, fmy,
 				       fmx, bmy, bmx, oldMode);
 	    break;
 	case BSEARCH_CROSS2:
-	    return BMotionSearchCross2(currentBlock, prev, next, by, bx, fmy,
+	    diff=BMotionSearchCross2(currentBlock, prev, next, by, bx, fmy,
 				       fmx, bmy, bmx, oldMode);
 	    break;
 	case BSEARCH_EXHAUSTIVE:
-	    return BMotionSearchExhaust(currentBlock, prev, next, by, bx, fmy,
+	    diff=BMotionSearchExhaust(currentBlock, prev, next, by, bx, fmy,
 				       fmx, bmy, bmx, oldMode);
 	    break;
 	default:
@@ -261,6 +266,63 @@
 		    bsearchAlg);
 	    exit(1);
     }
+  MOTION_TO_FRAME_COORD (bx, by, 0, 0, fx, fy);
+#if 0
+  printf("%i-%i (%i-%i):%i_%i  %i_%i \n",bx,by,fx,fy,*fmx,*fmy,*bmx,*bmy);
+  if (current->pmotions != NULL&&SearchRangeP)
+    {
+      int stepSize;
+      int leftMY, leftMX;
+      int rightMY, rightMX;
+      int mx, my;
+      int mmx, mmy;
+      int omx, omy;
+      int b;
+      best++;
+      b=best;
+
+      stepSize = (pixelFullSearch ? 2 : 1);
+
+      COMPUTE_MOTION_BOUNDARY (by, bx, stepSize, leftMY, leftMX, rightMY, rightMX);
+
+      /* try old motion vector first */
+      omx = current->pmotions[fx / 8 + (fy / 8) * ((Fsize_x + 7) / 8)][0];
+      omy = current->pmotions[fx / 8 + (fy / 8) * ((Fsize_x + 7) / 8)][1];
+
+      for (mmx = 0; mmx <= 3; mmx++)
+	{
+	  for (mmy = 0; mmy <= 3; mmy++)
+	    {
+	      int d2;
+	      mx=omx+mmx/2*((mmx&1)?-1:1);
+	      my=omy+mmy/2*((mmy&1)?-1:1);
+#if 0
+	      if (mx>=rightMX) mx = rightMX-1;
+	      if (my>=rightMY) my = rightMY-1;
+	      if (mx<leftMX) mx = leftMX;
+	      if (my<leftMY) my = leftMY;
+#endif
+	      if (VALID_MOTION (my, mx))
+		{
+		  d2 = LumMotionError (currentBlock, prev, by, bx, my, mx, best);
+		  if (d2 < best)
+		    {
+		      best=d2;
+		      /*printf("\a"); */
+		      *motionX = mx;
+		      *motionY = my;
+		    }
+		}
+	      else
+		printf ("Invalid");
+	    }
+	}
+      if(best!=b) printf("Guess!\a\n"); else printf("Fail\n");
+
+    }
+  printf("%i-%i (%i-%i):%i-%i\n",bx,by,fx,fy,*motionX,*motionY);
+#endif
+  return diff;
 }
 
 
@@ -1045,6 +1107,7 @@
     int32 *backErr;
     boolean backNeeded;
 {
+  int fx,fy;
     /* CALL SEARCH PROCEDURE */
     switch(psearchAlg) {
 	case PSEARCH_SUBSAMPLE:
@@ -1086,6 +1149,123 @@
 	    exit(1);	
 	    break;
     }
+  MOTION_TO_FRAME_COORD (bx, by, 0, 0, fx, fy);
+  /*printf("%i-%i (%i-%i):%i-%i  ",bx,by,fx,fy,*motionX,*motionY);*/
+  if (currentMPEG->pmotions != NULL&&searchRangeB)
+    {
+      int stepSize;
+      int leftMY, leftMX;
+      int rightMY, rightMX;
+      int mx, my;
+      int mmx, mmy;
+      int omx, omy;
+      int b;
+      b=*forwardErr;
+      *forwardErr++;
+
+      stepSize = (pixelFullSearch ? 2 : 1);
+
+      COMPUTE_MOTION_BOUNDARY (by, bx, stepSize, leftMY, leftMX, rightMY, rightMX);
+
+      /* try old motion vector first */
+      omx = currentMPEG->pmotions[fx / 8 + (fy / 8) * ((Fsize_x + 7) / 8)][0];
+      omy = currentMPEG->pmotions[fx / 8 + (fy / 8) * ((Fsize_x + 7) / 8)][1];
+
+      for (mmx = 0; mmx <= 3; mmx++)
+	{
+	  for (mmy = 0; mmy <= 3; mmy++)
+	    {
+	      int d2;
+	      mx=omx+mmx/2*((mmx&1)?-1:1);
+	      my=omy+mmy/2*((mmy&1)?-1:1);
+	      if (mx>=rightMX) mx = rightMX-1;
+	      if (my>=rightMY) my = rightMY-1;
+	      if (mx<leftMX) mx = leftMX;
+	      if (my<leftMY) my = leftMY;
+#if 0
+	      if(-mx>searchRangeP/2-1) mx=-searchRangeP/2+1;
+	      if(mx>searchRangeP/2-1) mx=searchRangeP/2-1;
+	      if(-my>searchRangeP/2-1) my=-searchRangeP/2+1;
+	      if(my>searchRangeP/2-1) my=searchRangeP/2-1;
+#endif
+
+	      if (VALID_MOTION (my, mx))
+		{
+		  d2 = LumMotionError (currentBlock, prev, by, bx, my, mx, *forwardErr);
+		  if (d2 < *forwardErr)
+		    {
+		      *forwardErr=d2;
+		      /*printf("\a"); */
+		      *fmx = mx;
+		      *fmy = my;
+		    }
+		}
+	      else
+		printf ("Invalid");
+	    }
+	}
+      /*if(*forwardErr!=b) printf("Guess!\a\n"); else printf("Fail\n");*/
+      if(*forwardErr<=b) nguesses++; else nfails++,*forwardErr=b;
+
+    }
+  if (currentMPEG->bmotions != NULL&&searchRangeB)
+    {
+      int stepSize;
+      int leftMY, leftMX;
+      int rightMY, rightMX;
+      int mx, my;
+      int mmx, mmy;
+      int omx, omy;
+      int b;
+      b=*backErr;
+      *backErr++;
+
+      stepSize = (pixelFullSearch ? 2 : 1);
+
+      COMPUTE_MOTION_BOUNDARY (by, bx, stepSize, leftMY, leftMX, rightMY, rightMX);
+
+      /* try old motion vector first */
+      omx = currentMPEG->bmotions[fx / 8 + (fy / 8) * ((Fsize_x + 7) / 8)][0];
+      omy = currentMPEG->bmotions[fx / 8 + (fy / 8) * ((Fsize_x + 7) / 8)][1];
+
+      for (mmx = 0; mmx <= 3; mmx++)
+	{
+	  for (mmy = 0; mmy <= 3; mmy++)
+	    {
+	      int d2;
+	      mx=omx+mmx/2*((mmx&1)?-1:1);
+	      my=omy+mmy/2*((mmy&1)?-1:1);
+	      if (mx>=rightMX) mx = rightMX-1;
+	      if (my>=rightMY) my = rightMY-1;
+	      if (mx<leftMX) mx = leftMX;
+	      if (my<leftMY) my = leftMY;
+#if 0
+	      if(-mx>searchRangeP/2-1) mx=-searchRangeP/2+1;
+	      if(mx>searchRangeP/2-1) mx=searchRangeP/2-1;
+	      if(-my>searchRangeP/2-1) my=-searchRangeP/2+1;
+	      if(my>searchRangeP/2-1) my=searchRangeP/2-1;
+#endif
+	      if (VALID_MOTION (my, mx))
+		{
+		  d2 = LumMotionError (currentBlock, prev, by, bx, my, mx, *backErr);
+		  if (d2 < *backErr)
+		    {
+		      *backErr=d2;
+		      /*printf("\a"); */
+		      *bmx = mx;
+		      *bmy = my;
+		    }
+		}
+	      else
+		printf ("Invalid");
+	    }
+	}
+      /*if(*backErr!=b) printf("Guess!\a\n"); else printf("Fail\n");*/
+      if(*backErr<=b) nguesses++; else nfails++,*backErr*=b;
+
+    }
+  /*printf("%i-%i (%i-%i):%i-%i\n",bx,by,fx,fy,*motionX,*motionY);*/
+
 }
 
 
@@ -1149,3 +1329,7 @@
 
 
 #endif /* UNUSED_PROCEDURES */
+
+
+
+
diff -urN old/mpeg_encode/frame.c mpeg_encode/frame.c
--- old/mpeg_encode/frame.c	Tue Aug 15 00:27:57 1995
+++ mpeg_encode/frame.c	Tue Apr 28 22:33:00 1998
@@ -864,6 +864,8 @@
     frame->id = id;
     frame->halfComputed = FALSE;
     frame->next = NULL;
+    frame->pmotions=NULL;
+    frame->bmotions=NULL;
 }
 
 
@@ -965,6 +967,10 @@
 	}
 	free(frame->halfBoth);
     }
+#if 0
+    if(frame->pmotions!=NULL) free(frame->pmotions);
+    if(frame->bmotions!=NULL) free(frame->bmotions);
+#endif
 
         
     free(frame);
diff -urN old/mpeg_encode/headers/frame.h mpeg_encode/headers/frame.h
--- old/mpeg_encode/headers/frame.h	Wed Feb  1 02:45:50 1995
+++ mpeg_encode/headers/frame.h	Tue Apr 28 17:46:19 1998
@@ -95,6 +95,8 @@
     struct mpegFrame *next;  /* points to the next B-frame to be encoded, if
 		       * stdin is used as the input. 
 		       */
+    int (*pmotions)[2];
+    int (*bmotions)[2];
 } MpegFrame;
 
 
diff -urN old/mpeg_encode/headers/frames.h mpeg_encode/headers/frames.h
--- old/mpeg_encode/headers/frames.h	Wed Aug 16 01:43:05 1995
+++ mpeg_encode/headers/frames.h	Tue Apr 28 18:55:12 1998
@@ -296,7 +296,7 @@
 void	ComputeBMotionLumBlock _ANSI_ARGS_((MpegFrame *prev, MpegFrame *next,
 			       int by, int bx, int mode, int fmy, int fmx,
 			       int bmy, int bmx, LumBlock motionBlock));
-int	BMotionSearch _ANSI_ARGS_((LumBlock currentBlock, MpegFrame *prev, MpegFrame *next,
+int	BMotionSearch _ANSI_ARGS_((MpegFrame *curr, LumBlock currentBlock, MpegFrame *prev, MpegFrame *next,
 		      int by, int bx, int *fmy, int *fmx, int *bmy, int *bmx, int oldMode));
 
 
diff -urN old/mpeg_encode/headers/prototypes.h mpeg_encode/headers/prototypes.h
--- old/mpeg_encode/headers/prototypes.h	Mon Jun 26 23:54:35 1995
+++ mpeg_encode/headers/prototypes.h	Tue Apr 28 19:00:36 1998
@@ -97,7 +97,7 @@
 void PPMtoYCC _ANSI_ARGS_((MpegFrame * mf));
 
 void	MotionSearchPreComputation _ANSI_ARGS_((MpegFrame *frame));
-boolean	PMotionSearch _ANSI_ARGS_((LumBlock currentBlock, MpegFrame *prev,
+boolean	PMotionSearch _ANSI_ARGS_((MpegFrame *curr, LumBlock currentBlock, MpegFrame *prev,
 				   int by, int bx, int *motionY, int *motionX));
 void	ComputeHalfPixelData _ANSI_ARGS_((MpegFrame *frame));
 void mp_validate_size _ANSI_ARGS_((int *x, int *y));
diff -urN old/mpeg_encode/headers/search.h mpeg_encode/headers/search.h
--- old/mpeg_encode/headers/search.h	Wed Aug 16 01:43:36 1995
+++ mpeg_encode/headers/search.h	Tue Apr 28 19:36:57 1998
@@ -115,7 +115,7 @@
 				     MpegFrame *prev, int by, int bx,
 				     int *motionY, int *motionX,
 				     int32 bestSoFar, int searchRange));
-boolean PMotionSearch _ANSI_ARGS_((LumBlock currentBlock,
+boolean PMotionSearch _ANSI_ARGS_((MpegFrame *curr, LumBlock currentBlock,
 				   MpegFrame *prev,
 				   int by, int bx,
 				   int *motionY, int *motionX));
diff -urN old/mpeg_encode/jpeg.c mpeg_encode/jpeg.c
--- old/mpeg_encode/jpeg.c	Sat Aug  5 01:35:07 1995
+++ mpeg_encode/jpeg.c	Wed Apr 15 19:31:58 1998
@@ -77,7 +77,7 @@
 
 /* make it happier.... */
 #undef DCTSIZE2
-#include "jpeg/jpeglib.h"
+#include <jpeglib.h>
 
 
 #define HEADER_SIZE 607   /*JFIF header size used on output images*/
diff -urN old/mpeg_encode/libpnmrw.c mpeg_encode/libpnmrw.c
--- old/mpeg_encode/libpnmrw.c	Thu Aug 17 00:24:26 1995
+++ mpeg_encode/libpnmrw.c	Wed Apr 15 19:28:07 1998
@@ -28,6 +28,7 @@
 #endif
 
 #include <stdio.h>
+#include <errno.h>
 #include "libpnmrw.h"
 
 /* if don't have string.h, try strings.h */
@@ -126,8 +127,6 @@
   pm_perror( reason )
 char* reason;
 {
-  extern char* sys_errlist[];
-  extern int errno;
   char* e;
 
   e = sys_errlist[errno];
Binary files old/mpeg_encode/mpeg_encode and mpeg_encode/mpeg_encode differ
Binary files old/mpeg_encode/mpeg_encode2 and mpeg_encode/mpeg_encode2 differ
diff -urN old/mpeg_encode/param.c mpeg_encode/param.c
--- old/mpeg_encode/param.c	Thu Aug 17 00:21:25 1995
+++ mpeg_encode/param.c	Mon Apr 27 21:16:11 1998
@@ -240,6 +240,9 @@
     int	    numPadding;	    /* -1 if there is none */
     int	    numFiles;
     boolean repeat;
+    int range1;
+    int range2;
+    char *psearchalg;
 } InputFileEntry;
 
 
@@ -250,6 +253,8 @@
 static InputFileEntry **inputFileEntries;
 static int numInputFileEntries = 0;
 static int  maxInputFileEntries;
+static int range1, range2;
+static char *psearchalg;
 
 
 /*==================*
@@ -310,13 +315,14 @@
  *		    parallel execution and input file names
  *
  *===========================================================================*/
+#define SIZE 1000000
 boolean
   ReadParamFile(fileName, function)
 char *fileName;
 int function;
 {
   FILE *fpointer;
-  char    input[256];
+  char    input[SIZE];
   char    *charPtr;
   boolean yuvUsed = FALSE;
   static char *optionText[LAST_OPTION+1] = { "GOP_SIZE", "PATTERN", "PIXEL", "PQSCALE",
@@ -372,7 +378,7 @@
     optionSeen[index]=FALSE;
   }
 
-  while ( fgets(input, 256, fpointer) != NULL ) {
+  while ( fgets(input, SIZE, fpointer) != NULL ) {
     /* skip comments */
     if ( input[0] == '#' ) {            
       continue;
@@ -527,7 +533,7 @@
         optionSeen[OPTION_IO_CONVERT] = TRUE;
       } else if ( strncmp(input, "IQTABLE", 7) == 0 ) {
         for ( row = 0; row < 8; row ++ ) {
-          fgets(input, 256, fpointer);
+          fgets(input, SIZE, fpointer);
           charPtr = input;
           if (8!=sscanf(charPtr,"%d %d %d %d %d %d %d %d",
                         &qtable[row*8+0],  &qtable[row*8+1],
@@ -565,7 +571,7 @@
     case 'N':
       if ( strncmp(input, "NIQTABLE", 8) == 0 ) {
         for ( row = 0; row < 8; row ++ ) {
-          fgets(input, 256, fpointer);
+          fgets(input, SIZE, fpointer);
           charPtr = input;
           if (8!=sscanf(charPtr,"%d %d %d %d %d %d %d %d",
                         &niqtable[row*8+0],  &niqtable[row*8+1],
@@ -619,6 +625,7 @@
       } else if ( strncmp(input, "PSEARCH_ALG", 11) == 0 ) {
         charPtr = SkipSpacesTabs(&input[11]);
         SetPSearchAlg(charPtr);
+	psearchalg=strdup(charPtr);
         optionSeen[OPTION_PSEARCH_ALG] = TRUE;
       } else if ( strncmp(input, "PARALLEL_TEST_FRAMES", 20) == 0 ) {
         SetParallelPerfect(FALSE);
@@ -647,9 +654,17 @@
         num_ranges=sscanf(charPtr,"%d %d",&a,&b);
         if (num_ranges==2) {
           SetSearchRange(a,b);
+	  range1=a;
+	  range2=b;
         } else if (sscanf(charPtr,"%d [%d]",&a,&b)==2) {
           SetSearchRange(a,b);
-        } else SetSearchRange(a,a);
+	  range1=a;
+	  range2=b;
+        } else {
+	  SetSearchRange(a,a);
+	  range1=a;
+	  range2=a;
+	}
       } else if ( strncmp(input, "REFERENCE_FRAME", 15) == 0 ) {
         charPtr = SkipSpacesTabs(&input[15]);
         SetReferenceFrameType(charPtr);
@@ -884,6 +899,18 @@
       sprintf(fileName, "%s", inputFileEntries[mapN]->left);
     }
   }
+  {
+    int r1,r2;
+    if(inputFileEntries[mapN]->range1==-1) r1=range1; else r1=inputFileEntries[mapN]->range1;
+    if(inputFileEntries[mapN]->range2==-1) r2=range2; else r2=inputFileEntries[mapN]->range2;
+  printf("Search Range:%i %i\n\n",r1,r2);
+  SetSearchRange(r1,r2);
+  if(inputFileEntries[mapN]->psearchalg!=NULL) {
+    printf("Setalg%i\n",inputFileEntries[mapN]->psearchalg);
+    SetPSearchAlg(inputFileEntries[mapN]->psearchalg);
+  printf("Alg:%s\n\n",inputFileEntries[mapN]->psearchalg);
+  }
+  }
 
   lastN = n;
   lastMapN = mapN;
@@ -910,10 +937,10 @@
   ReadMachineNames(fpointer)
 FILE *fpointer;
 {
-  char    input[256];
+  char    input[SIZE];
   char    *charPtr;
 
-  while ( (fgets(input, 256, fpointer) != NULL) &&
+  while ( (fgets(input, SIZE, fpointer) != NULL) &&
 	 (strncmp(input, "END_PARALLEL", 12) != 0) ) {
     if ( input[0] == '#' || input[0] == '\n') {
       continue;
@@ -966,6 +993,8 @@
   int	    length;
   char      full_path[MAXPATHLEN + 256];
   FILE      *jmovie;
+  char *psearchalg=NULL;
+  int range1=-1;range2=-1;
 
   inputFileEntries = (InputFileEntry **) malloc(INPUT_ENTRY_BLOCK_SIZE*
 						sizeof(InputFileEntry *));
@@ -981,6 +1010,27 @@
      * endInput string is reached so that the program points to the right 
      * place on exit.
      */
+    if (!strncmp(input,"RANGE",5)) {
+      int pos=5;
+      int num_ranges;
+      while(input[pos]==' '||input[pos]=='\t') pos++;
+      range1=-1;
+      range2=-1;
+        num_ranges=sscanf(input+pos,"%d %d",&range1,&range2);
+        if (num_ranges==2) {
+        } else if (sscanf(input+pos,"%d [%d]",&range1,&range2)==2) {
+        } else range2=range1;
+      printf("range:%i %i\n",range1,range2);
+      continue;
+    }
+    if (!strncmp(input,"PSEARCH_ALG",11)) {
+      char alg[256];
+      int pos=11;
+      while(input[pos]==' '||input[pos]=='\t') pos++;
+      sscanf(input+pos,"%s",alg);
+      psearchalg=strdup(alg);
+      continue;
+    }
     if ( stdinUsed ) {
       continue;
     }
@@ -1035,6 +1085,9 @@
 
     inputFileEntries[numInputFileEntries] = (InputFileEntry *)
       malloc(sizeof(InputFileEntry));
+    inputFileEntries[numInputFileEntries]->range1=range1;
+    inputFileEntries[numInputFileEntries]->range2=range2;
+    inputFileEntries[numInputFileEntries]->psearchalg=psearchalg;
 
     if ( input[strlen(input)-1] == ']' ) {
       inputFileEntries[numInputFileEntries]->glob = TRUE;
diff -urN old/mpeg_encode/pframe.c mpeg_encode/pframe.c
--- old/mpeg_encode/pframe.c	Mon Aug  7 23:51:39 1995
+++ mpeg_encode/pframe.c	Wed Apr 29 09:39:16 1998
@@ -184,7 +184,7 @@
 						      int numBlock));
 #endif
      
-    
+    extern int nguesses, nfails;
 /*=====================*
  * EXPORTED PROCEDURES *
  *=====================*/
@@ -340,7 +340,7 @@
 	motionY = 0;
 	useMotion = TRUE;
       } else {
-	useMotion = PMotionSearch(currentBlock, prev, y, x,
+	useMotion = PMotionSearch(current, currentBlock, prev, y, x,
 				  &motionY, &motionX);
 	if ( useMotion ) {
 	  if ( ZeroMotionBetter(currentBlock, prev, y, x, motionY,
@@ -379,9 +379,10 @@
 	ComputeDiffDCTs(current, prev, y, x, motionY, motionX,
 			&pattern);
 
+#if 0
 	assert(motionX+searchRangeP+1 >= 0);
 	assert(motionY+searchRangeP+1 >= 0);
-
+#endif
 #ifdef BLEAH
 	if ( motionX+searchRangeP+1 > 2*searchRangeP+2 )
 	  {
@@ -391,9 +392,11 @@
 #endif
 
 	if ( computeMVHist ) {
+#if 0
 	  assert(motionX+searchRangeP+1 <= 2*searchRangeP+2);
 	  assert(motionY+searchRangeP+1 <= 2*searchRangeP+2);
 	  pmvHistogram[motionX+searchRangeP+1][motionY+searchRangeP+1]++;
+#endif
 	}
 	/* Save specs for next loops */
 	dct_data[y][x].pattern = pattern;
@@ -690,6 +693,7 @@
 	      current->id, snr[0], snr[1], snr[2],
 	      psnr[0], psnr[1], psnr[2]);
     }
+    printf("Guesses:%i,Fails:%i\n",nguesses,nfails);
   }
 
   numFrameBits += (bb->cumulativeBits-totalFrameBits);
diff -urN old/mpeg_encode/psearch.c mpeg_encode/psearch.c
--- old/mpeg_encode/psearch.c	Tue Jul 18 21:01:59 1995
+++ mpeg_encode/psearch.c	Wed Apr 29 09:54:26 1998
@@ -104,6 +104,8 @@
 int searchRangeP,searchRangeB;
 int psearchAlg;
 
+int nguesses, nfails;
+
 
 /*===============================*
  * INTERNAL PROCEDURE prototypes *
@@ -147,37 +149,101 @@
  *
  *===========================================================================*/
 boolean
-PMotionSearch(currentBlock, prev, by, bx, motionY, motionX)
-    LumBlock currentBlock;
-    MpegFrame *prev;
-    int by;
-    int bx;
-    int *motionY;
-    int *motionX;
+PMotionSearch (current, currentBlock, prev, by, bx, motionY, motionX)
+     MpegFrame *current;
+     LumBlock currentBlock;
+     MpegFrame *prev;
+     int by;
+     int bx;
+     int *motionY;
+     int *motionX;
 {
-    /* CALL SEARCH PROCEDURE */
+  /* CALL SEARCH PROCEDURE */
+  int fx, fy;
+  int best;
+
+  switch (psearchAlg)
+    {
+    case PSEARCH_SUBSAMPLE:
+      best = PSubSampleSearch (currentBlock, prev, by, bx, motionY, motionX, searchRangeP);
+      break;
+    case PSEARCH_EXHAUSTIVE:
+      best = PLocalSearch (currentBlock, prev, by, bx, motionY, motionX,
+			   0x7fffffff, searchRangeP);
+      break;
+    case PSEARCH_LOGARITHMIC:
+      best = PLogarithmicSearch (currentBlock, prev, by, bx, motionY, motionX, searchRangeP);
+      break;
+    case PSEARCH_TWOLEVEL:
+      best = PTwoLevelSearch (currentBlock, prev, by, bx, motionY, motionX,
+			      0x7fffffff, searchRangeP);
+      break;
+    default:
+      fprintf (stderr, "ILLEGAL PSEARCH ALG:  %d\n", psearchAlg);
+      exit (1);
+    }
+  MOTION_TO_FRAME_COORD (bx, by, 0, 0, fx, fy);
+  /*printf("%i-%i (%i-%i):%i-%i  ",bx,by,fx,fy,*motionX,*motionY);*/
+  if (current->pmotions != NULL&&searchRangeP)
+    {
+      int stepSize;
+      int leftMY, leftMX;
+      int rightMY, rightMX;
+      int mx, my;
+      int mmx, mmy;
+      int omx, omy;
+      int b;
+      b=best;
+      best++;
+
+      stepSize = (pixelFullSearch ? 2 : 1);
+
+      COMPUTE_MOTION_BOUNDARY (by, bx, stepSize, leftMY, leftMX, rightMY, rightMX);
+
+      /* try old motion vector first */
+      omx = current->pmotions[fx / 8 + (fy / 8) * ((Fsize_x + 7) / 8)][0];
+      omy = current->pmotions[fx / 8 + (fy / 8) * ((Fsize_x + 7) / 8)][1];
+
+      for (mmx = 0; mmx <= 3; mmx++)
+	{
+	  for (mmy = 0; mmy <= 3; mmy++)
+	    {
+	      int d2;
+	      mx=omx+mmx/2*((mmx&1)?-1:1);
+	      my=omy+mmy/2*((mmy&1)?-1:1);
+	      /*if(abs(mx)>searchRangeP/2||abs(my)>searchRangeP/2) continue;*/
+#if 0
+	      if(-mx>searchRangeP/2-1) mx=-searchRangeP/2+1;
+	      if(mx>searchRangeP/2-1) mx=searchRangeP/2-1;
+	      if(-my>searchRangeP/2-1) my=-searchRangeP/2+1;
+	      if(my>searchRangeP/2-1) my=searchRangeP/2-1;
+#endif
+	      if (mx>=rightMX) mx = rightMX-1;
+	      if (my>=rightMY) my = rightMY-1;
+	      if (mx<leftMX) mx = leftMX;
+	      if (my<leftMY) my = leftMY;
+	      if (VALID_MOTION (my, mx))
+		{
+		  d2 = LumMotionError (currentBlock, prev, by, bx, my, mx, best);
+		  if (d2 < best)
+		    {
+		      best=d2;
+		      /*printf("\a"); */
+		      *motionX = mx;
+		      *motionY = my;
+		    }
+		}
+	      else
+		printf ("Invalid");
+	    }
+	}
+      /*if(best!=b) printf("Guess!\a\n"); else printf("Fail\n");*/
+      if(best<=b) nguesses++; else nfails++,best=b;
 
-    switch(psearchAlg) {
-	case PSEARCH_SUBSAMPLE:
-	    PSubSampleSearch(currentBlock, prev, by, bx, motionY, motionX, searchRangeP);
-	    break;
-	case PSEARCH_EXHAUSTIVE:
-	    PLocalSearch(currentBlock, prev, by, bx, motionY, motionX,
-			 0x7fffffff, searchRangeP);
-	    break;
-	case PSEARCH_LOGARITHMIC:
-	    PLogarithmicSearch(currentBlock, prev, by, bx, motionY, motionX, searchRangeP);
-	    break;
-	case PSEARCH_TWOLEVEL:
-	    PTwoLevelSearch(currentBlock, prev, by, bx, motionY, motionX,
-			    0x7fffffff, searchRangeP);
-	    break;
-	default:
-	    fprintf(stderr, "ILLEGAL PSEARCH ALG:  %d\n", psearchAlg);
-	    exit(1);
     }
+  /*printf("%i-%i (%i-%i):%i-%i\n",bx,by,fx,fy,*motionX,*motionY);*/
 
-    return TRUE;
+  return TRUE;
 }
 
 
@@ -201,7 +267,7 @@
     } else if ( strcmp(searchType, "HALF") == 0 ) {
 	pixelFullSearch = FALSE;
     } else {
-	fprintf(stderr, "ERROR:  Invalid pixel search type:  %s\n",
+	fprintf(stderr, "ERROR:  Invalid pixel search type:  '%s'\n",
 		searchType);
 	exit(1);
     }
@@ -232,7 +298,7 @@
     } else if ( strcmp(alg, "TWOLEVEL") == 0 ) {
 	psearchAlg = PSEARCH_TWOLEVEL;
     } else {
-	fprintf(stderr, "ERROR:  Invalid psearch algorithm:  %s\n", alg);
+	fprintf(stderr, "ERROR:  Invalid psearch algorithm:  '%s'\n", alg);
 	exit(1);
     }
 }
@@ -295,8 +361,11 @@
 	((searchRangeP>searchRangeB)?searchRangeP:searchRangeB)
 	  : ((searchRangeB>searchRangeB)?searchRangeB:searchRangeB);
 	
+        if(pmvHistogram!=NULL) free(pmvHistogram);
 	pmvHistogram = (int **) malloc((2*searchRangeP+3)*sizeof(int *));
+        if(bbmvHistogram!=NULL) free(pmvHistogram);
 	bbmvHistogram = (int **) malloc((2*searchRangeB+3)*sizeof(int *));
+        if(bfmvHistogram!=NULL) free(pmvHistogram);
 	bfmvHistogram = (int **) malloc((2*searchRangeB+3)*sizeof(int *));
 	for ( index = 0; index < 2*max_search+3; index++ ) {
 	    pmvHistogram[index] = (int *) calloc(2*searchRangeP+3, sizeof(int));
diff -urN old/mpeg_encode/readframe.c mpeg_encode/readframe.c
--- old/mpeg_encode/readframe.c	Tue Aug 15 00:31:58 1995
+++ mpeg_encode/readframe.c	Wed Apr 29 08:24:58 1998
@@ -236,7 +236,12 @@
 {
     FILE    *ifp;
     char    command[1024];
+    static char *oldFileName=NULL;
+    static char *bmotions=NULL, *pmotions=NULL;
     char    fullFileName[1024];
+    char s[1024];
+    char s2[1024];
+    int again=0;
     MpegFrame    tempFrame;
     MpegFrame    *framePtr;
 #ifdef BLEAH
@@ -245,7 +250,12 @@
 
 time(&diskStartTime);
 #endif
+printf("Start\n");
 
+    if(oldFileName==NULL||strcmp(fileName,oldFileName)) again=1;
+    {
+      if(oldFileName!=NULL) free(oldFileName);
+      oldFileName=strdup(fileName);
     if ( resizeFrame ) {
       tempFrame.inUse = FALSE;
       tempFrame.ppm_data = NULL;
@@ -264,6 +274,8 @@
     } else {
       sprintf(fullFileName, "%s", fileName);
     }
+    sprintf(s,"%s.b",fullFileName);
+    sprintf(s2,"%s.p",fullFileName);
 
     CurrFile = fullFileName;
 
@@ -400,8 +412,46 @@
     if ( kill_dim ) {
       DoKillDim(frame, Fsize_x, Fsize_y);
     }
-
+    }
+    if(again) {
+    {
+      FILE *f;
+      int i;
+      printf("%s\n",s);
+      if((f=fopen(s,"r"))!=NULL) {
+	float f1,f2;
+	printf("Found B frames!\n");
+	if(bmotions==NULL) bmotions=malloc(sizeof(*frame->bmotions)*((Fsize_x+7)/8)*((Fsize_y+7)/8)+20);
+	frame->bmotions=bmotions;
+	for(i=0;i<(((Fsize_x+7)/8)*((Fsize_y+7)/8));i++) {
+	  fscanf(f,"%f %f",&f1,&f2);
+	  frame->bmotions[i][0]=f1*2+0.5;
+	  frame->bmotions[i][1]=f2*2+0.5;
+	}
+      fclose(f);
+      }
+    }
+    {
+      FILE *f;
+      int i;
+      printf("%s\n",s2);
+      if((f=fopen(s2,"r"))!=NULL) {
+	printf("Found P frames!\n");
+	if(pmotions==NULL) pmotions=malloc(sizeof(*frame->pmotions)*((Fsize_x+7)/8)*((Fsize_y+7)/8)+20);
+	frame->pmotions=pmotions;
+	for(i=0;i<(((Fsize_x+7)/8)*((Fsize_y+7)/8));i++) {
+	  float f1,f2;
+	  fscanf(f,"%f %f",&f1,&f2);
+	  frame->pmotions[i][0]=f1*2+0.5;
+	  frame->pmotions[i][1]=f2*2+0.5;
+	}
+      fclose(f);
+      }
+    }
+    }
     MotionSearchPreComputation(frame);
+printf("End\n");
+
 }
 
 
