void WordcodeEmitter::peep(uint32_t opcode, uintptr_t* loc)
	{
		const peep_state_t *s;
		uint32_t limit, next_state;
		
		AvmAssert(opcode != OP_lookupswitch);

		if (state == 0) 
			goto initial_state;
		
		if (opcode == 0) {
			s = &states[state];
			goto accept;
		}
		
		// Search for a transition from the current state to a next
		// state on input 'opcode'.
		
		O[nextI] = opcode;
		I[nextI] = loc;
		nextI++;
		s = &states[state];
		limit = s->numTransitions;
		
		// The transition lists can get quite long for popular instructions like GETLOCAL;
		// binary search if it that might be profitable.
		
		if (limit > 4) {
			int32_t lo = s->transitionPtr;
			int32_t hi = lo + limit - 1;
			while (lo <= hi) {
				uint32_t mid = (unsigned)(lo + hi) / 2;
				uint32_t probe = transitions[mid].opcode;
				if (probe == opcode) {
					next_state = transitions[mid].next_state;
					goto found;
				}
				if (opcode < probe)
					hi = mid-1;
				else
					lo = mid+1;
			}
			next_state = 0;
		}
		else {
			const peep_transition_t* t = &transitions[s->transitionPtr];
			uint32_t i = 0;
			while (i < limit && t->opcode != opcode) 
				i++, t++;
			
			next_state = (i == limit) ? 0 : t->next_state;
		}
	found:
		
		if (next_state != 0) {

			// Advance
			//
			// There is a next state, so push the current state on the backtrack
			// stack if it is final, and move to the next state.  If that state has
			// successor states then return, as the search continues.  Otherwise, the
			// next state must be final and we try to accept.
			//
			// (The shortcut of checking the successors is necessary for correctness,
			// as otherwise the peephole window could contain a branch in the non-final
			// position.)
			
			if (s->guardAndAction != 0)
				backtrack_stack[backtrack_idx++] = state;

		advance:
			state = next_state;
			s = &states[state];
			if (s->numTransitions > 0)
				return;
			
			next_state = 0;
			AvmAssert(s->guardAndAction != 0);
		}

		// Accept
		//
		// The next state is 0.  Commit to 'state' if it is final; otherwise to 
		// successive backtrack states.  Committing means checking the guard
		// (which may fail, forcing further backtracking) and if the guard passes
		// then performing the transformation.  The commit function is generated,
		// see above; the replace logic is in the function replace() above.
		
	accept:
		if (s->guardAndAction && commit(s->guardAndAction)) 
			return;
		
		for ( int bi=backtrack_idx-1 ; bi >= 0 ; bi-- ) {
			const peep_state_t *b = &states[backtrack_stack[bi]];
			AvmAssert(b->guardAndAction != 0);
			if (commit(b->guardAndAction)) 
				return;
		}
		
		// If we could not accept or backtrack because of failing guards then
		// try the failure state, if defined.  We discard anything not relevant
		// to the failure state by shifting the window, so relevant instructions
		// always begin in offset 0 of the window.
		
		if (s->fail != 0) {
			shiftBuffers(s->failShift);
			next_state = s->fail;
			goto advance;
		}

		// If we failed to find an accepting state then fall through to initial_state
		// to reset the machine.  Resetting discards the first instruction only,
		// other cases - where larger shifts are possible - are handled above, because
		// in that case s->fail will be nonzero.
		//
		// After shifting, rerun the optimizer on the input buffer, since there may
		// be optimization opportunities there.

		shiftBuffers(1);
		if (nextI > 0) {
			replace(0, 0);
			peepFlush();
		}
		return;
		
	initial_state:
		AvmAssert(opcode < WOP_LAST+1);

		state = toplevel[opcode];  // may remain 0
		nextI = 0;
		backtrack_idx = 0;
		if (state != 0) {
			O[nextI] = opcode;
			I[nextI] = loc;
			nextI++;
		}
	}
Exemplo n.º 2
0
void PTracker::run()
{
	IdGenerator idGenerator(0);
	frameCount = 0;
	while(true)
	{
		SubFrame subResult = receive(subtractorBuffer);
		ClasifierFrame detectionResult = receive(classifierBuffer);

		bool buffersFull = shiftBuffers(subResult, detectionResult);
		if(!buffersFull)
			continue;
		
		frameCount++;
#if PROCESS

#pragma region setting data for time t
		auto detections = detectionBuffer[1];
		auto currentFrame = frameBuffer[1];
		auto currentGrayFrame = grayFrameBuffer[1];
		auto prevFrame = frameBuffer[0];
		auto currentForeground = foregroundBuffer[1];			
#pragma endregion 

#pragma region init tracks from first detections
		if(tracks.size() == 0 && detections.size() > 0)
		{
			tracks.clear();
			tracks.reserve(detections.size());
			for_each(begin(detections), end(detections), [&](detection& d){
				createTrack(d, idGenerator, currentGrayFrame);
			});
			continue;
		}
#pragma endregion

#pragma region predict track positions for time t
		
		auto lkoutput = currentFrame.clone();		
			deleteExitedTracks();
		beginTracking();
		for(auto it = begin(tracks); it != end(tracks); it++)		
			registerForTracking(*it);			
				
		performTracking();

		for(auto it = begin(tracks); it != end(tracks); it++)
		{			
			Rect kanadePrediction, kalmanPrediction;
			bool lucasSuccess = getMedianFlowPrediction(*it, kanadePrediction);
			bool kalmanSuccess = getKalmanPrediction(*it, kalmanPrediction);
			
			float minDist = 999999;
			auto finalPrediction = mergePredictions(lucasSuccess, kalmanSuccess, *it, kanadePrediction, kalmanPrediction, grayFrameBuffer, minDist);		
			it->assign(finalPrediction);		
			it->predictionDist = minDist;
		}			

#pragma endregion

		std::map<int, trackMatch> trMatches; 
		std::map<int, detectionMatch> detMatches;	

		
		matcher->begin();
		secMatcher->begin();
		for_each(begin(tracks), end(tracks), [&](track& tr){

			auto dit = detections.begin();
			auto dend = detections.end();
			for(;dit != dend; ++dit)
			{
				float score = matcher->match(tr, *dit, currentGrayFrame);
				if(score < 0)
					continue;

				float dist = secMatcher->match(tr, *dit, currentGrayFrame);
				if(dist > secMatcher->goodMaxDist)
				{					
					continue;
				}

				trackMatch trMatch = {dit->id, score};
				detectionMatch dMatch = {tr.id, score};

				if(detMatches.find(dit->id) == detMatches.end())
				{
					trMatches[tr.id] = trMatch;
					detMatches[dit->id] = dMatch;

				}else if(detMatches[dit->id].score < score)
				{
					auto exmatch = detMatches[dit->id];					
					trMatches.erase(exmatch.trackId);

					trMatches[tr.id] = trMatch;
					detMatches[dit->id] = dMatch;
				}								
			}							
		});			

#pragma region update track models and kalman
		for_each(begin(tracks), end(tracks), [&](track& tr){
			if(trMatches.find(tr.id) != trMatches.end())//matched detection with track
			{
				auto m = trMatches[tr.id];
				matcher->inferModel(tr, detections[m.detectionId], currentGrayFrame);
				secMatcher->inferModel(tr, detections[m.detectionId], currentGrayFrame);
				validators[tr.id].tick(true);

				correctKalman(tr);					
			}else
			{//detection not found for track
				float max = secMatcher->maxSimilarityDist;
				float dist = tr.predictionDist;
				if(dist < max) 
					validators[tr.id].tick(true);
				else
					validators[tr.id].tick(false);

				forwardKalman(tr);
			}
		});
#pragma endregion

#pragma region init new tracks from unmatched detections
		for_each(begin(detections), end(detections), [&](detection& mockdet){
			auto it = detMatches.find(mockdet.id);
			if(it == detMatches.end()){				
				auto inited = createTrack(mockdet, idGenerator, currentGrayFrame);
				trackMatch m = {mockdet.id, 1};
				trMatches[inited.id] = m;					
			}
		});
#pragma endregion 

#pragma region drawing_results

		if(debugPrint)
		{
			printf("frame %d========\n", frameCount);
			for(auto it = begin(tracks); it != end(tracks); it++)
			{
				if(trMatches.find(it->id) != trMatches.end())
					printf("track %d : detection %d\n", it->id, trMatches[it->id].detectionId);
			}
			cv::waitKey();
		}		

		auto fclone = currentFrame.clone();								
		DrawExtensions::drawDetections(detections, fclone);			

		DrawExtensions::drawTracks(tracks, fclone, Scalar(255,0,0));

		for_each(begin(tracks), end(tracks), [&](track& tr){
			Draw::rect(tr.model.kalmanRect, fclone, Scalar(0,0,255));
		});

		//imshow("xxx", currentForeground);
		//cv::waitKey(50);

		std::stringstream str;
		str << carCount;			
		Draw::text(str.str(), Point(10,20), fclone, Scalar(255,255,0));

		imshow("kalman", fclone);
		
		fclone.release();
		lkoutput.release();
#pragma endregion

#endif

		char key;
		key = cv::waitKey(1.);
		if(key == 's')
			debugPrint = true;
		else if(key == 'f')
			debugPrint = false;
	
		send(syncBuffer,1);			
	}
}