Subversion Repositories shark

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (c) 1994 by Gregory P. Ward.
 * All rights reserved.
 *
 * This file is part of the MNI front end of the Berkeley MPEG decoder.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
 * UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER
 * IS ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO PROVIDE
 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.  
 */

/*
 * Portions of this software Copyright (c) 1995 Brown University.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement
 * is hereby granted, provided that the above copyright notice and the
 * following two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR DIRECT,
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */


/*
 *
 * Original C version by Greg Ward <greg@pet.mni.mcgill.ca>,
 * reentrant C++ version by Loring Holden <lsh@cs.brown.edu>
 *
 * All functions that have an "MNI Header" are rewritten from the original
 * MNI C function
 *
 */


#include "ANIMmpeg.H"

// These are MPEG decoder globals but shouldn't be, really;
int quietFlag=1, loopFlag=1,qualityFlag=0, requireKeypressFlag=0;
int framerate=0,gammaCorrectFlag=0, chromaCorrectFlag=0, noDisplayFlag=0;
int partialFlag=0,startFrame=-1, endFrame=-1;
double gammaCorrect = 1.0, chromaCorrect=1.0;

int ANIMmpeg::numMovies=0;

//
//Constructor
//
//
ANIMmpeg::ANIMmpeg(const char * const inFileName)
  : vid_stream(NULL)
{
   newMovie(inFileName);
   init();
}

//
//Copy constructor
//
ANIMmpeg::ANIMmpeg(const ANIMmpeg& rhs)
  : ANIMbase(rhs),
    vid_stream(NULL)
{
   newMovie(rhs.fileName);
   init();
}

//
// Takes in read-only string that is the the name of the new movie,
// if NULL, then just deallocates memory
//
void
ANIMmpeg::newMovie(const char * const infile)
{
   int stringLength = infile==NULL ? 0 : strlen(infile);
#ifndef NOTHREADS
   Boolean tmpThreaded=MultiThreaded;

   //  In case we're decoding in another thread...
   if (MultiThreaded && mp_man->processor(threadNumber)->running) {
       mp_man->processor(threadNumber)->wait_for_done();
   }
#endif

   // Clean up after old movie
   if (fileName) {
      delete [] fileName;
   }
   if (filePtr) fclose(filePtr);
   if (vid_stream) {
      DestroyVidStream(vid_stream, NULL);
      vid_stream=NULL;
   }
   if (frameBuffer) {
      Buffer(FALSE);
   }
#ifndef NOTHREADS
   MultiThread(FALSE);
#endif

   if (stringLength) {
      fileName = new char[stringLength];
      strcpy(fileName, infile);
      filePtr = fopen(fileName,"r");
#ifndef NOTHREADS
      MultiThread(tmpThreaded);
#endif
   }

   /* Initialize movie information */
   Height=Width=BlockHeight=BlockWidth=Depth=PixelSize=Size=BitmapPad=0;
   PictureRate=BitRate=0;
}

//
//Initialization used by all constructors
//
//
void
ANIMmpeg::init() {
   thread_routine=RunDecode;
   numMovies++;
   if (numMovies==1) {
       /* Allocate/initialize tables used for dithering (?) */
       lum_values = (int *) malloc(LUM_RANGE*sizeof(int));
       cr_values = (int *) malloc(CR_RANGE*sizeof(int));
       cb_values = (int *) malloc(CB_RANGE*sizeof(int));
       init_tables();           /* initialize decoding stuff */
       InitCrop();
       InitDither();            /* initializes dithering structures and */
                                /* colormap (i.e. this is where we do */
                                /* all dither-specific stuff) */
   }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : OpenMPEG
@INPUT      : MPEGfile - pointer to a stream opened for reading, positioned
                         at the beginning of an MPEG stream
              ImgInfo  - pointer to an ImageDesc structure which will have
                         information such as frame height, width, depth
                         and size (total bytes per frame) put in it.
@OUTPUT     :
@RETURNS    :
@DESCRIPTION: Creates and initializes the variables needed to start
              reading/decoding an MPEG stream.  

              This function is part of the MNI front end to the Berkeley
              MPEG decoder, adapted from the original Berkeley code.
@METHOD     :
@GLOBALS    : LUM_RANGE, CR_RANGE, CB_RANGE
              lum_values, cr_values, cb_values
@CALLS      : GetMPEGInfo()
              init_tables()
              InitDither()
@CREATED    : 94/6/16, Greg Ward (adapted from main() in the original
              Berkeley source)
@MODIFIED   : 95/10, Loring Holden (made reentrant and ported to C++)
---------------------------------------------------------------------------- */

Boolean ANIMmpeg::open()
{
   Boolean okMovie;
   /*
    * Create the video stream and read the first chunk to get movie
    * stats -- width and height in particular.
    */

#ifndef NOTHREADS
   if (MultiThreaded && (mp_man->processor(threadNumber)->running)) {
#ifdef sol
       fprintf(stderr,"%s:  Decode running when open called\n",
           fileName);
#else
       fprintf(stderr,"%s: Decode running when open called\n",fileName);
#endif
       fflush(stderr);
       exit(2);
   }
#endif

   if (vid_stream==NULL) {
     vid_stream =NewVidStream((unsigned int) BUF_LENGTH);
     /* Initialize start time */
     vid_stream->realTimeStart = ReadSysClock();
   } else {
     // clear_data_stream(vid_stream) already called in mpegVidRsrc
     ResetVidStream(vid_stream);
     allBuffered=FALSE;
     // call buffer destructor
     if (frameBuffer) Buffer(FALSE);
   }

   vid_stream->input = filePtr;
   vid_stream->filename = (char *) fileName;
   vid_stream->ditherType=FULL_COLOR_DITHER; /* default */
   frames=0;
   InitStream();

   okMovie=(mpegVidRsrc(0, vid_stream, 1, NULL)!=NULL);
   if (okMovie) {
     GetInfo();
     moreFrames=TRUE;
   } else moreFrames=FALSE;

#ifndef NOTHREADS
   if (!allBuffered && MultiThreaded && okMovie) {
      // Should check that nothing is running, but assume rewind has been
      // called
      mp_man->processor(threadNumber)->start_execute(RunDecode,this,NULL);
   }
#endif
   return okMovie;

}     /* OpenMPEG () */


// Destructor
/* ----------------------------- MNI Header -----------------------------------
@NAME       : CloseMPEG
@INPUT      : (none)
@OUTPUT     : (none)
@RETURNS    : (void)
@DESCRIPTION: Frees up some of the memory allocated by OpenMPEG() (some
              is not freed because the Berkeley code doesn't take into
              account the fact that somebody might want to, say, free
              up the memory it allocates... someday, I'll probably have
              to hack into it to fix that, but not today thanks.)
@METHOD     :
@GLOBALS    : lum_values
              cr_values
              cb_values
@CALLS      : DestroyVidStream
@CREATED    : 94/6/27, Greg Ward
@MODIFIED   : 95/10, Loring Holden (made reentrant and ported to C++)
---------------------------------------------------------------------------- */

ANIMmpeg::~ANIMmpeg()
{
   newMovie(NULL);  //Like we are playing a new movie, but no new
                    //allocation
   // These stay around for the life of the program
   // free (lum_values);
   // free (cr_values);
   // free (cb_values);
}



/* ----------------------------- MNI Header -----------------------------------
@NAME       : RewindMPEG
@INPUT      : MPEGfile - the input stream where the MPEG's coming from
              Image    - image descriptor (just passed to OpenMPEG ())
@OUTPUT     : (none)
@RETURNS    : (void)
@DESCRIPTION: Resets things so that the caller can start reading the MPEG
              stream from the start again.  Note that the caller does NOT
              need to call OpenMPEG() again -- after a call to RewindMPEG(),
              the next call to GetMPEGFrame() will return the first frame
              of the MPEG.
@METHOD     :
@GLOBALS    :
@CALLS      :
@CREATED    : 94/7/20, Greg Ward
@MODIFIED   : 95/10, Loring Holden (made reentrant and ported to C++)
@COMMENTS   :
---------------------------------------------------------------------------- */

void ANIMmpeg::rewind()
{
#ifndef NOTHREADS
   if (MultiThreaded && (mp_man->processor(threadNumber)->running)) {
#ifdef sol
       fprintf(stderr,"%s:  Decode running when rewind called\n",
           fileName);
#else
       fprintf(stderr,"%s: Decode running when rewind called\n",fileName);
#endif
       fflush(stderr);
       exit(2);
   }
#endif

   if (!allBuffered) {
     ::rewind(filePtr);
     open();
   } else {
     // do buffering stuff here
   }
}



/* ----------------------------- MNI Header -----------------------------------
@NAME       : GetMPEGInfo
@INPUT      : vid_stream - a video stream that is open and has had at
                           least one call to mpegVidRsrc() performed on it
              Info       - a pointer to an ImageDesc struct in the caller's
                           space (i.e., the argument to OpenMPEG()) where
                           the image information will be copied
@OUTPUT     :
@RETURNS    : (void)
@DESCRIPTION: From the video stream, determines the width, height, pixel
              size and depth (in bits) and total image size (in bytes)
              for an MPEG stream.  Sets the global variable Info
              (part of the interface between wrapper.c and globals.c),
              and then copies that struct to the user application's
              space via the Info pointer.
@METHOD     :
@GLOBALS    :
@CALLS      :
@CREATED    : 94/6/17, Greg Ward: based on code from ExecuteDisplay() in the
              original Berkeley source
@MODIFIED   : 95/10, Loring Holden (made reentrant and ported to C++)
---------------------------------------------------------------------------- */

void ANIMmpeg::GetInfo()
{
#ifndef DISABLE_DITHER
   switch (vid_stream->ditherType)
   {
      case Twox2_DITHER:
      {
         Height = vid_stream->v_size;
         Width = vid_stream->h_size;
         BlockHeight=vid_stream->mb_height;
         BlockWidth=vid_stream->mb_width;
         Depth = 8;
         PixelSize = 8;
         BitmapPad = 8;
         break;
      }
      case FULL_COLOR_DITHER:
      {
#endif
         Height = vid_stream->v_size;
         Width = vid_stream->h_size;
         BlockHeight=vid_stream->mb_height;
         BlockWidth=vid_stream->mb_width;
         Depth = 24;
         PixelSize = 32;
         BitmapPad = 32;
#ifndef DISABLE_DITHER
         break;
      }
      case MONO_DITHER:
      case MONO_THRESHOLD:
      {
         Height = vid_stream->v_size;
         Width = vid_stream->h_size;
         Depth = 1;
         PixelSize = 1;
         BitmapPad = 8;
         break;
      }
      default:                  /* including GRAY_DITHER and ORDERED_DITHER */
      {
         Height = vid_stream->v_size;
         Width = vid_stream->h_size;
         Depth = 8;
         PixelSize = 8;
         BitmapPad = 8;
         break;
      }
   }     /* switch on ditherType */
#endif

   Size = (BlockHeight*16*BlockWidth*16*PixelSize) / 8;
   PictureRate = vid_stream->picture_rate;
   BitRate = vid_stream->bit_rate;

}     /* GetMPEGInfo () */

/* ----------------------------- MNI Header -----------------------------------
@NAME       : GetMPEGFrame
@INPUT      :
@OUTPUT     : Frame - the image data, converted to RGB space, is
              copied to the area pointed to by Frame.  There must be
              enough room for the entire image; the ImageDesc
              structure returned by OpenMPEG() will tell you just how
              much memory this is.  The format of the data depends on
              the dithering type; for full colour dithering, there are
              four bytes per pixel: red, green, blue, and unused.
              (Perfect for passing to lrectwrite() or XPutImage().)
@RETURNS    : TRUE if there are still frames left to decode
              FALSE if we have just decoded the last frame
@DESCRIPTION: Part of the MNI front end to the Berkeley MPEG decoder.  
              Essentially reads, decodes, and converts to RGB space the
              next frame in the MPEG stream opened with OpenMPEG().
@METHOD     :
@GLOBALS    :
@CALLS      : mpegVidRsrc ()
@CREATED    : 94/6/16, Greg Ward
@MODIFIED   : 95/10, Loring Holden (made reentrant and ported to C++)
---------------------------------------------------------------------------- */

void *ANIMmpeg::RunDecode(void *ptr, void *)
{
   ANIMmpeg *movie=(ANIMmpeg *) ptr;
   Boolean   MovieDone = FALSE;
   int frames;

#if 0
   dprintf ("GetMPEGFrame: starting or continuing movie\n");
#endif

   frames=movie->vid_stream->totNumFrames;
   while (!MovieDone && (frames==movie->vid_stream->totNumFrames))
   {
      mpegVidRsrc(0,movie->vid_stream,0, NULL);
      MovieDone = movie->vid_stream->film_has_ended;
   }

   return NULL;

}     /* GetMPEGFrame () */

Boolean ANIMmpeg::GetFrame(char **Frame, Boolean *newFrame)
{
   void *CurrentImage;
   int looped;

   if ((vid_stream==NULL) && !open()) {
        *newFrame=FALSE;
        return FALSE;
   }

   if (!allBuffered) {
#ifndef NOTHREADS
      if (MultiThreaded) {
         mp_man->processor(threadNumber)->wait_for_done();
      } else {
         ANIMmpeg::RunDecode(this,NULL);
      }
#else
      ANIMmpeg::RunDecode(this,NULL);
#endif
      moreFrames=!vid_stream->film_has_ended;
      CurrentImage=(void *) vid_stream->current->display;
      if (buffering) {
         // copy CurrentImage to the right place in the array
         if (!frameBuffer->addToBuffer(CurrentImage)) {
            // ran out of memory - can't buffer
            Buffer(FALSE);
            fflush(stdout);
            fprintf(stderr,"%s: no more room to buffer\n",fileName);
            fflush(stderr);
         } else if (!moreFrames) {
            allBuffered=TRUE;
            moreFrames=TRUE;
         }
      }
#if 0
      dprintf ("\nGetMPEGFrame: just received a finished frame: ",
            "copying from %08X to %08X\n", CurrentImage, Frame);
#endif
      memcpy ((void *) *Frame, CurrentImage, Size);

      if (allBuffered) {
         int totFrames=vid_stream->totNumFrames;

         DestroyVidStream(vid_stream,NULL);
         // This is a hack in order to use PrintTimeInfo
         //   - Create a NewVidStream just to use realTimeStart
         //   - all the image information is deallocated, so we do save space
         vid_stream =NewVidStream((unsigned int) BUF_LENGTH);
         vid_stream->totNumFrames=totFrames;
         /* Initialize start time */
         vid_stream->realTimeStart = ReadSysClock();
      }

   }  else {
      // set moreFrames
      moreFrames=TRUE;
      // set CurrentImage to point to the right place in the array
      *Frame=(char *) frameBuffer->nextBufferItem(&looped);
      if (looped) {
        PrintTimeInfo(vid_stream);
        vid_stream->realTimeStart = ReadSysClock();
      }
   }

#ifndef NOTHREADS
   if (!allBuffered && MultiThreaded && moreFrames) {
      mp_man->processor(threadNumber)->start_execute(RunDecode,this,NULL);
   }
#endif

   frames++;
   *newFrame=TRUE;
   return moreFrames;
}

#ifndef DISABLE_DITHER
/* ----------------------------- MNI Header -----------------------------------
@NAME       : DoDitherImage
@INPUT      : l, Cr, Cb - pointers to the luminance, Cr, and Cb planes
              disp - ?
              h, w - height and width of image (?)
@OUTPUT     :
@RETURNS    :
@DESCRIPTION: Called when image needs to be dithered. Selects correct
              dither routine based on info in ditherType.
@METHOD     :
@GLOBALS    : ditherType
@CALLS      : One of the following, depending on the value of ditherType:
                 HybridDitherImage       (hybrid.c)
                 HybridErrorDitherImage  (hybriderr.c)
                 FS2FastDitherImage      (fs2fast.c)
                 FS2DitherImage          (fs2.c)
                 FS4DitherImage          (fs4.c)
                 Twox2DitherImage        (2x2.c)
                 ColorDitherImage        (16bit.c)
                 GrayDitherImage         (gray.c)
                 OrderedDitherImage      (ordered.c)
                 MonoDitherImage         (mono.c)
                 MonoThresholdImage      (mono.c)
                 Ordered2DitherImage     (ordered2.c)
                 MBOrderedDitherImage    (mb_ordered.c)
@CREATED    : (taken from the original Berkeley code)
@MODIFIED   :
---------------------------------------------------------------------------- */

void
DoDitherImage(VidStream *vid_stream)
{
 unsigned char *l=vid_stream->current->luminance,
               *Cr=vid_stream->current->Cr,
               *Cb=vid_stream->current->Cb,
               *disp=vid_stream->current->display;
 int h=(int) vid_stream->mb_height * 16;
 int w=(int) vid_stream->mb_width * 16;
 int ditherType=vid_stream->ditherType;
 int matched_depth=vid_stream->matched_depth;

  switch(ditherType) {
  case HYBRID_DITHER:
    HybridDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case HYBRID2_DITHER:
    HybridErrorDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case FS2FAST_DITHER:
    FS2FastDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case FS2_DITHER:
    FS2DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case FS4_DITHER:
    FS4DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case Twox2_DITHER:
    Twox2DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case FULL_COLOR_DITHER:
    Color32DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case GRAY_DITHER:
    GrayDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case NO_DITHER:
    break;

  case PPM_DITHER:
    Color32DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case ORDERED_DITHER:
    OrderedDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case MONO_DITHER:
    MonoDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case MONO_THRESHOLD:
    MonoThresholdImage(l, Cr, Cb, disp, h, w);
    break;

  case ORDERED2_DITHER:
    Ordered2DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case MBORDERED_DITHER:
    MBOrderedDitherImage(l, Cr, Cb, disp, h, w, vid_stream->ditherFlags);
    break;
  }
}   /* DoDitherImage () */
#else
void
DoDitherImage(VidStream *vid_stream)
{
 unsigned char *l=vid_stream->current->luminance,
               *Cr=vid_stream->current->Cr,
               *Cb=vid_stream->current->Cb,
               *disp=vid_stream->current->display;
 int h=(int) vid_stream->mb_height * 16;
 int w=(int) vid_stream->mb_width * 16;

 Color32DitherImage(l,Cr,Cb,disp,h,w);
}
#endif

/* ----------------------------- MNI Header -----------------------------------
@NAME       : InitColormap
@INPUT      : (none)
@OUTPUT     : *NumColors - number of entries in the newly-created colormap
              *Map - an array of colourmap entries; each one contains a
                     red, green, and blue byte-values (0 .. 255).  
                     *Map[i] gives the colour to display a pixel value i.
@RETURNS    : (none)
@DESCRIPTION: Creates a colour map used for most dithering methods
              (everything except full-colour, gray, and monochrome).
              The colour map itself is pretty self-explanatory -- a
              pixel with value i is to be displayed using the red, green
              and blue values in *Map[i] after InitColormap() is done.
@METHOD     :
@GLOBALS    :
@CALLS      :
@CREATED    : 95/3/4, Greg Ward: based on InitColorDisplay(), from gdith.c
                                 in the original Berkeley player
@MODIFIED   :
---------------------------------------------------------------------------- */

#ifndef DISABLE_DITHER
static void
InitColormap (int *NumColors, ColormapEntry **Map)
{
   int i, lum_num, cr_num, cb_num;

   *NumColors =  LUM_RANGE*CB_RANGE*CR_RANGE;
   *Map = (ColormapEntry *) malloc (*NumColors * sizeof (ColormapEntry));

   for (i = 0; i < *NumColors; i++)
   {
      lum_num = (i / (CR_RANGE*CB_RANGE))%LUM_RANGE;
      cr_num = (i / CB_RANGE)%CR_RANGE;
      cb_num = i % CB_RANGE;
     
/*      ConvertColor(lum_values[lum_num], cr_values[cr_num], cb_values[cb_num],
                   &(*Map)[i].red, &(*Map)[i].green, &(*Map)[i].blue);
*/

      pixel[i] = i;
   }
}
#endif


/* ----------------------------- MNI Header -----------------------------------
@NAME       : InitGrayColormap
@INPUT      : (none)
@OUTPUT     : *NumColors - number of entries in the newly-created colormap
              *Map - an array of colourmap entries
@RETURNS    : (none)
@DESCRIPTION: Creates a colour map used for gray-scale dithering, i.e.
              the red/green/blue values are the same for any given
              pixel value.
@METHOD     :
@GLOBALS    :
@CALLS      :
@CREATED    : 95/3/4, Greg Ward: based on InitGrayDisplay(), from gdith.c
                                 in the original Berkeley player
@MODIFIED   :
---------------------------------------------------------------------------- */

#ifndef DISABLE_DITHER
static void
InitGrayColormap (int *NumColors, ColormapEntry **Map)
{
   int  i;
   
   *NumColors =  NUM_COLORS;
   *Map = (ColormapEntry *) malloc (*NumColors * sizeof (ColormapEntry));

   for (i = 0; i < *NumColors; i++)
   {
      (*Map)[i].red = (*Map)[i].green = (*Map)[i].blue = i;
      pixel[i] = i;
   }
}
#endif


/* ----------------------------- MNI Header -----------------------------------
@NAME       : InitDither
@INPUT      : Image - pointer to the image descriptor for the current MPEG
@OUTPUT     : ColormapSize, Colormap - the colour map for
              this movie, as initialized by either InitColormap or
              InitGrayColormap (unless the current dithering scheme
              is full colour, in which case there is no colour map)
@RETURNS    : (none)
@DESCRIPTION: Does all initialization particular to the type of dithering
              being used.  Basically, sets up the internal data structures
              needed by the dithering code, and then sets up a colour map
              needed to display the pixels output by the ditherers.
@METHOD     :
@GLOBALS    :
@CALLS      : InitColor     (for most dithering methods)
              InitColormap  (for most dithering methods)
              InitGrayColormap (for gray-scale dithering)
              Init(..)Dither  (.. = the current dithering method)
@CREATED    : 95/3/3, Greg Ward: taken mostly from main() in the original
                                 Berkeley player
@MODIFIED   :
---------------------------------------------------------------------------- */

void
ANIMmpeg::InitDither(unsigned long red,
                      unsigned long green,
                      unsigned long blue)
{
   LUM_RANGE = 8;
   CR_RANGE = 4;
   CB_RANGE = 4;
#ifndef DISABLE_DITHER
   switch (vid_stream->ditherType)
   {
      case HYBRID_DITHER:
         InitColor ();
         InitHybridDither ();
         InitColormap (&ColormapSize, &Colormap);
         break;

      case HYBRID2_DITHER:
         InitColor ();
         InitHybridErrorDither ();
         InitColormap (&ColormapSize, &Colormap);
         break;

      case FS4_DITHER:
         InitColor ();
         InitFS4Dither ();
         InitColormap (&ColormapSize, &Colormap);
         break;

      case FS2_DITHER:
         InitColor ();
         InitFS2Dither ();
         InitColormap (&ColormapSize, &Colormap);
         break;

      case FS2FAST_DITHER:
         InitColor ();
         InitFS2FastDither ();
         InitColormap (&ColormapSize, &Colormap);
         break;

      case Twox2_DITHER:
         InitColor ();
         Init2x2Dither ();
         InitColormap (&ColormapSize, &Colormap);
         PostInit2x2Dither ();
         break;

      case GRAY_DITHER:
         InitGrayColormap (&ColormapSize, &Colormap);
         break;
      case FULL_COLOR_DITHER:
#endif
         wpixel[0]=red;
         wpixel[1]=green;
         wpixel[2]=blue;
         InitColorDither(1);
#ifndef DISABLE_DITHER
         break;

      case NO_DITHER:
         break;

      case ORDERED_DITHER:
         InitColor ();
         InitOrderedDither ();
         InitColormap (&ColormapSize, &Colormap);
         break;

      case MONO_DITHER:
      case MONO_THRESHOLD:
         break;

      case ORDERED2_DITHER:
         InitColor ();
         InitColormap (&ColormapSize, &Colormap);
         InitOrdered2Dither ();
         break;

      case MBORDERED_DITHER:
         InitColor ();
         InitColormap (&ColormapSize, &Colormap);
         InitMBOrderedDither ();
         break;

      case PPM_DITHER:
         ColormapSize = -1;
         Colormap = NULL;
         wpixel[0] = 0xff;
         wpixel[1] = 0xff00;
         wpixel[2] = 0xff0000;
         vid_stream->matched_depth=24;
         InitColorDither(TRUE);
         break;
   }
#endif
}  

//
//
//
void
ANIMmpeg::InitStream()
{
   if ((vid_stream->ditherType==FULL_COLOR_DITHER) ||
       (vid_stream->ditherType==PPM_DITHER)) {
         vid_stream->matched_depth=24;
//       InitColorDither(1);
   }
}