예제 #1
0
//--------------------------------------------------------------------
void ofVideoGrabber::update(){
	if(	grabber != NULL ){
		grabber->update();
		if( bUseTexture && !grabber->getTexture() && grabber->isFrameNew() ){
			tex.loadData(grabber->getPixels(), (int)tex.getWidth(), (int)tex.getHeight(), ofGetGLTypeFromPixelFormat(internalPixelFormat));
		}
	}
}
예제 #2
0
//------------------------------------
void ofVideoPlayer::setUseTexture(bool bUse){
	bUseTexture = bUse;
	if(bUse && width!=0 && height!=0 && !tex.isAllocated()){
		tex.allocate(width, height, ofGetGLTypeFromPixelFormat(internalPixelFormat));
		if(ofGetGLProgrammableRenderer() && internalPixelFormat == OF_PIXELS_MONO){
			tex.setRGToRGBASwizzles(true);
		}
	}
}
//--------------------------------------------------------------
void ofxThreadedVideo::updateTexture(int videoID){
    if(videoID != VIDEO_NONE){
        if(bUseTexture){
            // make sure we don't have NULL pixels
            if(pixels[videoID]->getPixels() != NULL && textures[videoID].isAllocated()){
                float w = videos[videoID].getWidth();
                float h = videos[videoID].getHeight();
                textures[videoID].loadData(pixels[videoID]->getPixels(), w, h, ofGetGLTypeFromPixelFormat(internalPixelFormat));
            }
        }
        bFrameNew[videoID] = false;
    }
}
예제 #4
0
//--------------------------------------------------------------------
void ofVideoPlayer::update(){
	if(	player != NULL ){

		player->update();
		
		if( bUseTexture && player->isFrameNew() ) {
			
			playerTex = player->getTexture();
			
			if(playerTex == NULL){
				unsigned char *pxls = player->getPixels();
				
				bool bDiffPixFormat = ( tex.bAllocated() && tex.texData.glTypeInternal != ofGetGLInternalFormatFromPixelFormat(internalPixelFormat) );
				
				//TODO: we might be able to do something smarter here for not re-allocating movies of the same size and type. 
				if(width==0 || height==0 || bDiffPixFormat ){ //added a check if the pixel format and the texture don't match
					if(player->getWidth() != 0 && player->getHeight() != 0) {
						
						width = player->getWidth();
						height = player->getHeight();
					
						if(tex.bAllocated())
							tex.clear();

						tex.allocate(width, height, ofGetGLInternalFormatFromPixelFormat(internalPixelFormat));
		        		if(ofGetGLProgrammableRenderer() && internalPixelFormat == OF_PIXELS_MONO){
		        			tex.setRGToRGBASwizzles(true);
		        		}
						tex.loadData(pxls, tex.getWidth(), tex.getHeight(), ofGetGLTypeFromPixelFormat(internalPixelFormat));
					}
				}else{					
					tex.loadData(pxls, width, height, ofGetGLTypeFromPixelFormat(internalPixelFormat));
				}
			}
		}
	}
}
예제 #5
0
//--------------------------------------------------------------
void ofxThreadedVideo::update(){
    
    lock();
    
    if(!bCriticalSection && bLoaded){
        bCriticalSection = true;
        int videoID = currentVideoID;
        bool bUpdate = bLoaded;
        unlock();
        
        if(bUpdate){
            
//            lock();
            video[videoID].update();
            
            bIsFrameNew = video[videoID].isFrameNew();
            position = video[videoID].getPosition();
            frameCurrent = video[videoID].getCurrentFrame();
            bIsMovieDone = video[videoID].getIsMovieDone();
//            unlock();
            
            if(bIsFrameNew || bForceFrameNew){
                
                if(bForceFrameNew) lock();
                
                if(!bIsTextureReady) bIsTextureReady = true;
                
                if(drawTexture.getWidth() != width || drawTexture.getHeight() != height){
                    drawTexture.allocate(width, height, ofGetGLTypeFromPixelFormat(video[videoID].getPixelFormat()));
                }
                
                unsigned char * pixels = video[videoID].getPixels();
                if(pixels != NULL && bUseTexture) drawTexture.loadData(pixels, width, height, ofGetGLTypeFromPixelFormat(video[videoID].getPixelFormat()));
                
                if(bForceFrameNew){
                    bForceFrameNew = false;
                    unlock();
                }
                
                // calculate frameRate -> taken from ofAppRunner
                prevMillis = ofGetElapsedTimeMillis();
                timeNow = ofGetElapsedTimef();
                double diff = timeNow-timeThen;
                if( diff  > 0.00001 ){
                    fps			= 1.0 / diff;
                    frameRate	*= 0.9f;
                    frameRate	+= 0.1f*fps;
                }
                lastFrameTime	= diff;
                timeThen		= timeNow;
                
            }
        }
        
        lock();
        bCriticalSection = false;
        unlock();
    }else{
        unlock();
    }
    
    lock();
    ofxThreadedVideoGlobalMutex.lock();
    if(!ofxThreadedVideoGlobalCritical && !bCriticalSection){
        int videoID = currentVideoID;
        ofxThreadedVideoGlobalCritical = true;
        bCriticalSection = true;
        ofxThreadedVideoCommand c = getCommand();
        bool bCanStop = (bLoaded && !bIsLoading) || (!bLoaded && !bIsLoading);
        bool bPopCommand = false;
        unlock();
        ofxThreadedVideoGlobalMutex.unlock();

        if(c.getInstance() == instanceID){
            
            if(c.getCommand() == "stop" && bCanStop){
                if(bVerbose) ofLogVerbose() << instanceID << " = " << c.getCommandAsString();
                if(bIsPlaying) video[videoID].stop();
                lock();
                //fade = 1.0;
                fades.clear();
                bIsPlaying = false;
                bIsPaused = false; // ????
                bIsLoading = false;
                bIsFrameNew = false;
                bIsMovieDone = false;
                bLoaded = false;
                unlock();
                bPopCommand = true;
            }
            
            if(c.getCommand() == "loadMovie" && bCanStop){
                if(bVerbose) ofLogVerbose() << instanceID << " = " << c.getCommandAsString() << " execute in update";
                if(bIsPlaying) video[videoID].stop();
                lock();
                currentVideoID = getNextLoadID();
                bIsPaused = false;
                bLoaded = false;
                bIsLoading = true;
                bIsPlaying = false;
                bIsMovieDone = false;
                unlock();
            }
            
            if(c.getCommand() == "setSpeed"){
                if(bVerbose) ofLogVerbose() << instanceID << " = " << c.getCommandAsString();
                lock();
                speed = c.getArgument<float>(0);
                unlock();
                video[videoID].setSpeed(speed);
                bPopCommand = true;
            }
            
            if(c.getCommand() == "setFrame"){
                if(bVerbose) ofLogVerbose() << instanceID << " = " << c.getCommandAsString();
                lock();
                int frameTarget = c.getArgument<int>(0);
                bForceFrameNew = true;
                frameTarget = CLAMP(frameTarget, 0, frameTotal);
                //cout << "setframe A: " << frameTarget << " " << videoID << " " << bCriticalSection << endl;
                video[videoID].setFrame(frameTarget);
                //cout << "setframe B: " << frameTarget << " " << videoID << " " << bCriticalSection << endl;
                unlock();
                bPopCommand = true;
            }
            
        }
        
        lock();
        ofxThreadedVideoGlobalMutex.lock();
        
        if(bPopCommand) popCommand();
        
        ofxThreadedVideoGlobalCritical = false;
        bCriticalSection = false;
        ofxThreadedVideoGlobalMutex.unlock();
        unlock();
    }else{
        ofxThreadedVideoGlobalMutex.unlock();
        unlock();
    }
}
//--------------------------------------------------------------
void ofxThreadedVideo::update(){

    if(lock()){
        
        // check if we're loading a video
        if(loadVideoID != VIDEO_NONE){
            
            videos[loadVideoID].update();
            
            float w = videos[loadVideoID].getWidth();
            float h = videos[loadVideoID].getHeight();

            // allocate a texture if the one we have is a different size
            if(bUseTexture && (textures[loadVideoID].getWidth() != w || textures[loadVideoID].getHeight() != h)){
                ofLogVerbose() << "Allocating texture" << loadVideoID << w << h;
                textures[loadVideoID].allocate(w, h, ofGetGLTypeFromPixelFormat(internalPixelFormat));
            }

            // check for a new frame before loading video
            if(bFrameNew[loadVideoID]){

                // switch the current movie ID to the one we just loaded
                int lastVideoID = currentVideoID;
                currentVideoID = loadVideoID;
                loadVideoID = VIDEO_NONE;

                // close the last movie - we do this here because
                // ofQuicktimeVideo chokes if you try to close in a thread
                if(lastVideoID != VIDEO_NONE){
                    ofLogVerbose() << "Closing last video" << lastVideoID;
                    paths[lastVideoID] = names[lastVideoID] = "";
                    videos[lastVideoID].stop();

                    // reset properties to defaults
                    newPosition[lastVideoID] = -1.0f;
                    newFrame[lastVideoID] = -1;
                    newSpeed[lastVideoID] = 1.0f;
                    newLoopType[lastVideoID] = -1;
                    frame[lastVideoID] = 0;

                    bFrameNew[lastVideoID] = false;
                    bPaused[lastVideoID] = false;
                    volume[lastVideoID] = 255;
                }

                // send event notification
                ofxThreadedVideoEvent videoEvent = ofxThreadedVideoEvent(paths[currentVideoID], VIDEO_EVENT_LOAD_OK, this);
                ofNotifyEvent(threadedVideoEvent, videoEvent, this);
            }
        }

        // check for a new frame for current video
        updateTexture(currentVideoID);

        // if there's a movie in the queue
        if(pathsToLoad.size() > 0 && loadPath == "" && loadVideoID == VIDEO_NONE){
            // ...let's start trying to load it!
            loadPath = pathsToLoad.front();
            pathsToLoad.pop();
        };

        // calculate frameRate -> taken from ofAppRunner
        prevMillis = ofGetElapsedTimeMillis();
        timeNow = ofGetElapsedTimef();
        double diff = timeNow-timeThen;
        if( diff  > 0.00001 ){
            fps			= 1.0 / diff;
            frameRate	*= 0.9f;
            frameRate	+= 0.1f*fps;
        }
        lastFrameTime	= diff;
        timeThen		= timeNow;

        unlock();
    }
}
예제 #7
0
//--------------------------------------------------------------
void ofxOpenNIDepthThreshold::setMaskPixelFormat(ofPixelFormat format){
    maskPixelFormat = format;
    if(maskPixels.getImageType() != ofGetImageTypeFromGLType(maskPixelFormat)){
        maskPixels.allocate(maskPixels.getWidth(), maskPixels.getHeight(), maskPixelFormat);
        if(bUseMaskTexture) maskTexture.allocate(maskPixels.getWidth(), maskPixels.getHeight(), ofGetGLTypeFromPixelFormat(maskPixelFormat));
    }
}