ImageSaveCache.cpp 8.43 KB
#include "ImageSaveCache.h"
#include <algorithm>
#include <cuda_runtime.h>
#include <fstream>
#include <thread>
#include <future>
//std::ofstream osImage("D:\\vptImagelog.txt", std::ofstream::out | std::ofstream::trunc);
void ImageSaveCache::show()
{
	size_t count = 1;
	for (auto  item  = mp_key.begin(); item != mp_key.end(); )
	{
		//osImage << std::unitbuf;
		//osImage << item->first.videoID << " " << item->first.objID << " : " << item->second.videoID << " " << item->second.objID << std::endl;	
		auto unaryPred = [item](std::pair<const OBJ_KEY, FRAME_KEY> & item_key)
		{
			if (item_key.second == item->second)
			{
				return true;
			}
			else
			{
				return false;
			}
		};
		++item;
		if (item == mp_key.end())
			break;
		if (find_if(item, mp_key.end(), unaryPred) == mp_key.end())
		{
			++count;
			continue;
		}
	}

	//osImage << " tital : " << mp_key.size() << " diff : " << count << std::endl;

}
//#include <fstream>
//std::ofstream os1("./mp_frameSize.txt", std::ofstream::out | std::ofstream::trunc);
void ImageSaveCache::insert(const OBJ_KEY &snaphot_id, const FRAME_KEY & frame_id, const DxGPUFrame & frame)
{
	//std::lock_guard<std::mutex> l(tx);
	//os1 << std::unitbuf;
	
	auto iterK = mp_key.find(snaphot_id);
	if (iterK == mp_key.end())																													
	{
		auto unaryF = [&frame_id](std::pair<FRAME_KEY, DxGPUFrame> & item_key)
		{
			if (item_key.first == frame_id)
			{
				return true;
			}
			else
			{
				return false;
			}
		};
		auto iterF = std::find_if(mp_frame.begin(), mp_frame.end(), unaryF);
		if (iterF != mp_frame.end())																			  //����ô�ͼ�ѱ��棬��ֱ�����Ӹ�����������map
		{
			mp_key.insert({ snaphot_id, frame_id });
		}
		else                                                                                                      //�����ͼû���棬���´������ݣ������Ӵ�ͼ������
		{
			auto iterFrame = mp_frame.begin();
			for (; iterFrame != mp_frame.end(); ++iterFrame)
			{
				auto unaryPred = [iterFrame](std::pair<const OBJ_KEY, FRAME_KEY> & item_key)
				{
					if (item_key.second == iterFrame->first)
					{
						return true;
					}
					else
					{
						return false;
					}
				};
				auto iterKey = std::find_if(mp_key.begin(), mp_key.end(), unaryPred);

				if (iterKey == mp_key.end() && iterFrame->second.width == frame.width && iterFrame->second.height == frame.height && iterFrame->second.size == frame.size)																	   //����ô�ͼû���棬���ȱ�����ͼvector,���ijһ����ͼ���ݣ���������map����������ô�ô�ͼ���ݽ�����������
				{

					cudaError_t cudaStatus = cudaMemcpy(iterFrame->second.frame, frame.frame, IMG_CHANNELS * frame.width * frame.height * sizeof(unsigned char), cudaMemcpyDeviceToHost);
					if (cudaStatus != cudaSuccess) {
						fprintf(stderr, "here cudaMemcpy  failed! error: %s\n", cudaGetErrorString(cudaStatus));
					}

					iterFrame->second.height = frame.height;
					iterFrame->second.width = frame.width;
					iterFrame->second.size = frame.size;
					iterFrame->first = frame_id;
					iterK->second = frame_id;
					mp_key.insert({ snaphot_id, frame_id });
					return;
				}
			}
			DxGPUFrame dx_frame;
			dx_frame.frame = malloc(frame.width * frame.height * IMG_CHANNELS * sizeof(unsigned char));
			cudaError_t cudaStatus = cudaMemcpy(dx_frame.frame, frame.frame, IMG_CHANNELS * frame.width * frame.height * sizeof(unsigned char), cudaMemcpyDeviceToHost);
			if (cudaStatus != cudaSuccess) {
				fprintf(stderr, "here cudaMemcpy  failed! error: %s\n", cudaGetErrorString(cudaStatus));
			}

			dx_frame.height = frame.height;
			dx_frame.width = frame.width;
			dx_frame.size = frame.size;

			mp_frame.push_back({ frame_id, dx_frame });
			mp_key.insert({ snaphot_id, frame_id });
		}
		
		return;
	}
	else
	{
		auto unaryF = [&frame_id](std::pair<FRAME_KEY, DxGPUFrame> & item_key)
		{
			if (item_key.first == frame_id)
			{
				return true;
			}
			else
			{
				return false;
			}
		};
		auto iterF = std::find_if(mp_frame.begin(), mp_frame.end(), unaryF);
		if (iterF != mp_frame.end())																			  //����ô�ͼ�ѱ��棬���޸�����
		{
			iterK->second = frame_id;
		}
		else
		{
			auto iterFrame = mp_frame.begin();
			for (; iterFrame != mp_frame.end(); ++iterFrame)	                                              
			{
				auto iterKey = mp_key.begin();
				for ( ;iterKey != mp_key.end(); iterKey++){
					if (iterKey->second == iterFrame->first) {
						break;
					}
				}

				if (iterKey == mp_key.end()&& iterFrame->second.width == frame.width && iterFrame->second.height == frame.height && iterFrame->second.size == frame.size)																	   //����ô�ͼû���棬���ȱ�����ͼvector,���ijһ����ͼ���ݣ���������map����������ô�ô�ͼ���ݽ�����������
				{
					cudaError_t cudaStatus = cudaMemcpy(iterFrame->second.frame, frame.frame, IMG_CHANNELS * frame.width * frame.height * sizeof(unsigned char), cudaMemcpyDeviceToHost);
					if (cudaStatus != cudaSuccess) {
						fprintf(stderr, "here cudaMemcpy  failed! error: %s\n", cudaGetErrorString(cudaStatus));
					}				
					iterFrame->second.height = frame.height;
					iterFrame->second.width = frame.width;
					iterFrame->second.size = frame.size;
					iterFrame->first = frame_id;
					iterK->second = frame_id;
					return;
				}
			}
			DxGPUFrame dx_frame;																										
			dx_frame.frame = malloc(IMG_CHANNELS * frame.width * frame.height * sizeof(unsigned char));
			cudaError_t cudaStatus = cudaMemcpy(dx_frame.frame, frame.frame, IMG_CHANNELS * frame.width * frame.height * sizeof(unsigned char), cudaMemcpyDeviceToHost);
			if (cudaStatus != cudaSuccess) {
				fprintf(stderr, "here cudaMemcpy  failed! error: %s\n", cudaGetErrorString(cudaStatus));
			}

			dx_frame.height = frame.height;
			dx_frame.width = frame.width;
			dx_frame.size = frame.size;

			mp_frame.push_back({ frame_id, dx_frame });
			iterK->second = frame_id;

		}
		return;
	}
}

DxGPUFrame* ImageSaveCache::get_frame(const OBJ_KEY &snaphot_id)
{
	//std::lock_guard<std::mutex> l(tx);
	auto iter = mp_key.find(snaphot_id);
	if (iter == mp_key.end())
	{
		return nullptr;
	}
	else
	{
		auto unaryF = [iter](std::pair<FRAME_KEY, DxGPUFrame> & item_key)
		{
			if (item_key.first == iter->second)
			{
				return true;
			}
			else
			{
				return false;
			}
		};
		auto iterf = std::find_if(mp_frame.begin(), mp_frame.end(), unaryF);
		return &iterf->second;
	}
		
}

void ImageSaveCache::release(const OBJ_KEY & snaphot_id)
{
	//std::lock_guard<std::mutex> l(tx);
	auto iter = mp_key.find(snaphot_id);
	if (iter == mp_key.end())
	{
		return;
	}
	else
	{
		auto item = *iter;
		mp_key.erase(iter);                                                                       //ɾ������
		auto unaryK = [&item](std::pair<const OBJ_KEY, FRAME_KEY> & item_key)
		{
			if (item_key.second == item.second)
			{
				return true;
			}
			else
			{
				return false;
			}
		};
		auto iterK = std::find_if(mp_key.begin(), mp_key.end(), unaryK);                          //���û���������յ����Ŵ�ͼ�Ǹ�ͼƬ����ɾ���ô�ͼ
		if (iterK == mp_key.end())
		{
			auto unaryF = [&item](std::pair<FRAME_KEY, DxGPUFrame> & item_key)
			{
				if (item_key.first == item.second)
				{
					return true;
				}
				else
				{
					return false;
				}
			};
			auto iterF = std::find_if(mp_frame.begin(), mp_frame.end(), unaryF);
			free(iterF->second.frame);
			mp_frame.erase(iterF);
		}

		auto iterFrame = mp_frame.begin();
		for (; iterFrame != mp_frame.end();)
		{
			auto unaryPred = [iterFrame](std::pair<const OBJ_KEY, FRAME_KEY> & item_key)
			{
				if (item_key.second == iterFrame->first)
				{
					return true;
				}
				else
				{
					return false;
				}
			};
			auto iterKey = std::find_if(mp_key.begin(), mp_key.end(), unaryPred);

			if (iterKey == mp_key.end())	
			{
				free(iterFrame->second.frame);
				iterFrame = mp_frame.erase(iterFrame);
				continue;
				
			}
			++iterFrame;
		}
		return;
	}
}