Subversion Repositories shark

Rev

Go to most recent revision | Blame | 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 "ANIMbase.H"

#ifndef NOTHREADS
THREADmp_manager *ANIMbase::mp_man=NULL;
#endif

//
//Constructor
//
//
ANIMbase::ANIMbase()
  : threadNumber(-1),
    moreFrames(FALSE),
#ifndef NOTHREADS
    MultiThreaded(FALSE),
#endif
    fileName(NULL),
    buffering(FALSE),
    allBuffered(FALSE),
    frameBuffer(NULL),
    filePtr(NULL),
    frames(0)
{
}

//
//Copy constructor
//Copies settings from MultiThreading and buffering, and starts
//the same movie from the beginning
//
ANIMbase::ANIMbase(const ANIMbase& rhs)
  : threadNumber(-1),
    moreFrames(FALSE),
#ifndef NOTHREADS
    MultiThreaded(rhs.MultiThreaded),
#endif
    fileName(NULL),
    buffering(rhs.buffering),
    allBuffered(FALSE),
    frameBuffer(NULL),
    filePtr(NULL),
    frames(0)
{
}


// 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++)
---------------------------------------------------------------------------- */

ANIMbase::~ANIMbase()
{
//   newMovie(NULL);  //Like we are playing a new movie, but no new
//                  //allocation
#ifndef NOTHREADS
   MultiThread(FALSE);
#endif

}

//
// Access to private member MultiThreaded for the masses
// (Deals with starting or stopping MultiThreading)
//
#ifndef NOTHREADS
void
ANIMbase::MultiThread(Boolean whether) {
        if (whether==MultiThreaded) return;

        if (whether)  {
            //We should only do multi-threading if we have a routine to run
            //or we haven't buffered the movie yet
            if ((thread_routine!=NULL) && !allBuffered)  {
               if (mp_man==NULL) mp_man=new THREADmp_manager(1);
               threadNumber=mp_man->deqThread();
               // If we haven't called open() then don't start process
               if (moreFrames)
                 mp_man->processor(threadNumber)->
                  start_execute(thread_routine,this,NULL);
            } else return;
        } else {
           whether=FALSE;
           if (mp_man->processor(threadNumber)->running) {
              mp_man->processor(threadNumber)->wait_for_done();
           }
           mp_man->enqThread(threadNumber);
           threadNumber=-1;
        }
        MultiThreaded=whether;
}
#endif

//
// Access to private member buffering for the masses
// (allocates or deallocates buffer)
//
void
ANIMbase::Buffer(Boolean whether, Boolean compress) {
        if (whether==(buffering || allBuffered))  return;
        if (whether && !allBuffered && moreFrames) {
           int bytesSave=4;
           buffering=TRUE;
           allBuffered=FALSE;
           // create buffer to hold the frames
           if (compress) bytesSave=3;
           frameBuffer=new BUFFER(Size,4,bytesSave);
        }
        if (!whether) {
           buffering=allBuffered=FALSE;
           // delete buffer
           delete frameBuffer;
           frameBuffer=NULL;
        }
}