⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 camera.cpp

📁 S3C24A0的完整BSP包,对开发此芯片的开发者很有用.
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	}

	RETAILMSG(MSG_EN_1,(_T("pBufOut 0 offset = 0x%x\r\n"), pImage));
	memcpy(pImage, &yuvinfo, sizeof(YUVINFO));
	pImage += sizeof(YUVINFO);	
	
	RETAILMSG(MSG_EN_1,(_T("pBufOut Y = 0x%x\r\n"), pImage));
	memcpy(pImage, buffer_y, Y_size);
	pImage += (Y_size);
	
	RETAILMSG(MSG_EN_1,(_T("pBufOut cb = 0x%x\r\n"), pImage));
	memcpy(pImage, buffer_cb, C_size);
	pImage += C_size;
	RETAILMSG(MSG_EN_1,(_T("pBufOut cr = 0x%x\r\n"), pImage));
	memcpy(pImage, buffer_cr, C_size);
//	pBufOut += (size_x*size_y)/4;


#if (DOTNET_DRIVER)
	SetKMode(FALSE);
#endif	

	Tick_COPY_FRAME = GetTickCount();

	RETAILMSG(MSG_EN_1,(_T("COPY_Tick = %d\r\n"), (Tick_COPY_FRAME-Tick_GET_FRAME_CUR)));
	RETAILMSG(MSG_EN_1,(_T("Frame_Tick = %d\r\n"), (Tick_GET_FRAME_CUR-Tick_GET_FRAME_PREV)));
}

void Samsung_camcoder(U8 *pBufOut)
{
	U8 *pImage;
	PINGPONG caminfo;

	pImage = pBufOut;

	if( codec_flag )
	{
		caminfo.flag = 1;
		caminfo.y_address = y_address;
		caminfo.cb_address = cb_address;
		caminfo.cr_address = cr_address;
		
		memcpy(pImage, &caminfo, sizeof(PINGPONG));
		codec_flag = 0;
	}
}

void Samsung_camcoder_pr(U8 *pBufOut)
{
	U8 *pImage;
	PINGPONG_PR prinfo;

	pImage = pBufOut;

	if ( rgb_flag )
	{
		prinfo.flag = 1;
		prinfo.rgb_address = rgb_address;
		memcpy(pImage, &prinfo, sizeof(PINGPONG_PR));
		rgb_flag = 0;
	}	
}

void Buffer_codec_info_update()
{
	U32 Y_size, C_size, P_size;
	int temp;
	unsigned int buffer_y, buffer_cb, buffer_cr;
	
	if ( image_size == 1 )
		Y_size = QCIF_XSIZE*QCIF_YSIZE;
	else if ( image_size == 2 )
		Y_size = CIF_XSIZE*CIF_YSIZE;

	C_size = Y_size/4;	
	P_size = Y_size + C_size*2;

	temp = (s24A0CAM->CICOSTATUS>>26)&3;
	temp = (temp + 2) % 4;

	RETAILMSG(MSG_EN_2,(_T("codec index = %d, size %d\r\n"), temp, image_size));

	switch (temp)
	{
	case 0:
		buffer_y = s24A0CAM->CICOYSA1;
		buffer_cb = s24A0CAM->CICOCBSA1;
		buffer_cr = s24A0CAM->CICOCRSA1;
		break;
	case 1:
		buffer_y = s24A0CAM->CICOYSA2;
		buffer_cb = s24A0CAM->CICOCBSA2;
		buffer_cr = s24A0CAM->CICOCRSA2;
		break;
	case 2:
		buffer_y = s24A0CAM->CICOYSA3;
		buffer_cb = s24A0CAM->CICOCBSA3;
		buffer_cr = s24A0CAM->CICOCRSA3;
		break;
	case 3:
		buffer_y = s24A0CAM->CICOYSA4;
		buffer_cb = s24A0CAM->CICOCBSA4;
		buffer_cr = s24A0CAM->CICOCRSA4;
		break;
	default :
		buffer_y = s24A0CAM->CICOYSA1;
		buffer_cb = s24A0CAM->CICOCBSA1;
		buffer_cr = s24A0CAM->CICOCRSA1;
		break;
	}

	buffer_y += VIRTUAL_ADDR_OFFSET;
	buffer_cb += VIRTUAL_ADDR_OFFSET;
	buffer_cr += VIRTUAL_ADDR_OFFSET;
#if 0
	RETAILMSG(MSG_EN_1,(_T("buffer_y = 0x%x\r\n"), buffer_y));
	RETAILMSG(MSG_EN_1,(_T("buffer_cb = 0x%x\r\n"), buffer_cb));
	RETAILMSG(MSG_EN_1,(_T("buffer_cr = 0x%x\r\n"), buffer_cr));
#endif
	if( codec_flag )	RETAILMSG(MSG_EN_1,(_T("Buffer is not read\r\n")));
	codec_flag = 1;
	y_address = buffer_y;
	cb_address = buffer_cb;
	cr_address = buffer_cr;
}


void Buffer_preview_info_update()
{
//	U32 Y_size;
	int temp;
	unsigned int buffer_rgb;
	
/*
	if ( image_size == 1 )
		Y_size = QCIF_XSIZE*QCIF_YSIZE;
	else if ( image_size == 2 )
		Y_size = CIF_XSIZE*CIF_YSIZE;
*/

	temp = (s24A0CAM->CIPRSTATUS>>26)&3;
	temp = (temp + 2) % 4;

	RETAILMSG(MSG_EN_2,(_T("preview index = %d, size %d\r\n"), temp, image_size));

	switch (temp)
	{
	case 0:
		buffer_rgb = s24A0CAM->CIPRCLRSA1;
		break;
	case 1:
		buffer_rgb = s24A0CAM->CIPRCLRSA2;
		break;
	case 2:
		buffer_rgb = s24A0CAM->CIPRCLRSA3;
		break;
	case 3:
		buffer_rgb = s24A0CAM->CIPRCLRSA4;
		break;
	default :
		buffer_rgb = s24A0CAM->CIPRCLRSA1;
		break;
	}

	buffer_rgb += VIRTUAL_ADDR_OFFSET;
#if 0
	RETAILMSG(MSG_EN_1,(_T("buffer_rgb = 0x%x\r\n"), buffer_rgb));

	if( rgb_flag )
		RETAILMSG(MSG_EN_1,(_T("Buffer is not read 0x%x\r\n"), buffer_rgb));
#endif	
	rgb_flag = 1;
	rgb_address = buffer_rgb;
}


void CamInit(U32 CoDstWidth, U32 CoDstHeight, U32 PrDstWidth, U32 PrDstHeight, 
			U32 WinHorOffset, U32 WinVerOffset,  U32 CoFrameBuffer, U32 PrFrameBuffer)
{
	U32 WinOfsEn;
	U32 divisor, multiplier;
	U32 MainBurstSizeY, RemainedBurstSizeY, MainBurstSizeC, RemainedBurstSizeC, MainBurstSizeRGB, RemainedBurstSizeRGB;
	U32 H_Shift, V_Shift, PreHorRatio, PreVerRatio, MainHorRatio, MainVerRatio;
	U32 SrcWidth, SrcHeight;
	U32 ScaleUp_H_Co, ScaleUp_V_Co, ScaleUp_H_Pr, ScaleUp_V_Pr;

	RETAILMSG(RETAIL_ON, (TEXT("##CamInit --> %d %d %d %d %d %d %0x %0x\r\n"), 
	  CoDstWidth,   CoDstHeight,   PrDstWidth,   PrDstHeight, 
	  WinHorOffset,   WinVerOffset, CoFrameBuffer, PrFrameBuffer));

	//constant for calculating codec dma address
	if(CAM_CODEC_OUTPUT)
		divisor=2; //CCIR-422
	else
		divisor=4; //CCIR-420
		
	//constant for calculating preview dma address
	if(CAM_PVIEW_OUTPUT)
		multiplier=4;
	else
		multiplier=2;
	
	if(WinHorOffset==0 && WinVerOffset==0)
		WinOfsEn=0;
	else
		WinOfsEn=1;

	SrcWidth=CAM_SRC_HSIZE-WinHorOffset*2;
	SrcHeight=CAM_SRC_VSIZE-WinVerOffset*2;

	if(SrcWidth>=CoDstWidth) 
		ScaleUp_H_Co=0; 		//down
	else 
		ScaleUp_H_Co=1;		//up

	if(SrcHeight>=CoDstHeight) 
		ScaleUp_V_Co=0;
	else 
		ScaleUp_V_Co=1;		

	if(SrcWidth>=PrDstWidth) 
		ScaleUp_H_Pr=0; 		//down
	else 
		ScaleUp_H_Pr=1;		//up

	if(SrcHeight>=PrDstHeight) 
		ScaleUp_V_Pr=0;
	else 
		ScaleUp_V_Pr=1;

	////////////////// common control setting
	s24A0CAM->CIGCTRL |= (1<<26)|(0<<27); // inverse PCLK, test pattern
	s24A0CAM->CIWDOFST = (1<<30)|(0xf<<12); // clear overflow 
	s24A0CAM->CIWDOFST = 0;
	s24A0CAM->CIWDOFST=(WinOfsEn<<31)|(WinHorOffset<<16)|(WinVerOffset);

	s24A0CAM->CISRCFMT=(CAM_ITU601<<31)|(0<<30)|(0<<29)|(CAM_SRC_HSIZE<<16)|(CAM_ORDER_YCBYCR<<14)|(CAM_SRC_VSIZE);
	
	////////////////// codec port setting
	s24A0CAM->CICOYSA1=CoFrameBuffer;
	s24A0CAM->CICOYSA2=s24A0CAM->CICOYSA1+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
	s24A0CAM->CICOYSA3=s24A0CAM->CICOYSA2+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
	s24A0CAM->CICOYSA4=s24A0CAM->CICOYSA3+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
	
	s24A0CAM->CICOCBSA1=s24A0CAM->CICOYSA1+CoDstWidth*CoDstHeight;
	s24A0CAM->CICOCBSA2=s24A0CAM->CICOYSA2+CoDstWidth*CoDstHeight;
	s24A0CAM->CICOCBSA3=s24A0CAM->CICOYSA3+CoDstWidth*CoDstHeight;
	s24A0CAM->CICOCBSA4=s24A0CAM->CICOYSA4+CoDstWidth*CoDstHeight;

	s24A0CAM->CICOCRSA1=s24A0CAM->CICOCBSA1+CoDstWidth*CoDstHeight/divisor;
	s24A0CAM->CICOCRSA2=s24A0CAM->CICOCBSA2+CoDstWidth*CoDstHeight/divisor;
	s24A0CAM->CICOCRSA3=s24A0CAM->CICOCBSA3+CoDstWidth*CoDstHeight/divisor;
	s24A0CAM->CICOCRSA4=s24A0CAM->CICOCBSA4+CoDstWidth*CoDstHeight/divisor;
    
	s24A0CAM->CICOTRGFMT=(CAM_CODEC_IN_420<<31)|(CAM_CODEC_OUTPUT<<30)|(CoDstWidth<<16)|(CAM_FLIP_NORMAL<<14)|(CoDstHeight);

	CalculateBurstSize(CoDstWidth, &MainBurstSizeY, &RemainedBurstSizeY);
	CalculateBurstSize(CoDstWidth/2, &MainBurstSizeC, &RemainedBurstSizeC);
	s24A0CAM->CICOCTRL=(MainBurstSizeY<<19)|(RemainedBurstSizeY<<14)|(MainBurstSizeC<<9)|(RemainedBurstSizeC<<4);

	CalculatePrescalerRatioShift(SrcWidth, CoDstWidth, &PreHorRatio, &H_Shift);
	CalculatePrescalerRatioShift(SrcHeight, CoDstHeight, &PreVerRatio, &V_Shift);
	MainHorRatio=(SrcWidth<<8)/(CoDstWidth<<H_Shift);
	MainVerRatio=(SrcHeight<<8)/(CoDstHeight<<V_Shift);
    			
	s24A0CAM->CICOSCPRERATIO=((10-H_Shift-V_Shift)<<28)|(PreHorRatio<<16)|(PreVerRatio);
	s24A0CAM->CICOSCPREDST=((SrcWidth/PreHorRatio)<<16)|(SrcHeight/PreVerRatio); 
	s24A0CAM->CICOSCCTRL=(CAM_SCALER_BYPASS_OFF<<31)|(ScaleUp_H_Co<<30)|(ScaleUp_V_Co<<29)|(MainHorRatio<<16)|(MainVerRatio);

	s24A0CAM->CICOTAREA=CoDstWidth*CoDstHeight;

	///////////////// preview port setting
	s24A0CAM->CIPRCLRSA1=PrFrameBuffer;
	s24A0CAM->CIPRCLRSA2=s24A0CAM->CIPRCLRSA1+PrDstWidth*PrDstHeight*multiplier;
	s24A0CAM->CIPRCLRSA3=s24A0CAM->CIPRCLRSA2+PrDstWidth*PrDstHeight*multiplier;
	s24A0CAM->CIPRCLRSA4=s24A0CAM->CIPRCLRSA3+PrDstWidth*PrDstHeight*multiplier;

	s24A0CAM->CIPRTRGFMT=(PrDstWidth<<16)|(CAM_FLIP_NORMAL<<14)|(PrDstHeight);

	if (CAM_PVIEW_OUTPUT==CAM_RGB24B)
		CalculateBurstSize(PrDstWidth*4, &MainBurstSizeRGB, &RemainedBurstSizeRGB);
	else // RGB16B
		CalculateBurstSize(PrDstWidth*2, &MainBurstSizeRGB, &RemainedBurstSizeRGB);
   	s24A0CAM->CIPRCTRL=(MainBurstSizeRGB<<19)|(RemainedBurstSizeRGB<<14);

	CalculatePrescalerRatioShift(SrcWidth, PrDstWidth, &PreHorRatio, &H_Shift);
	CalculatePrescalerRatioShift(SrcHeight, PrDstHeight, &PreVerRatio, &V_Shift);
	MainHorRatio=(SrcWidth<<8)/(PrDstWidth<<H_Shift);
	MainVerRatio=(SrcHeight<<8)/(PrDstHeight<<V_Shift);
	s24A0CAM->CIPRSCPRERATIO=((10-H_Shift-V_Shift)<<28)|(PreHorRatio<<16)|(PreVerRatio);
	s24A0CAM->CIPRSCPREDST=((SrcWidth/PreHorRatio)<<16)|(SrcHeight/PreVerRatio);
	s24A0CAM->CIPRSCCTRL=(1<<31)|(CAM_RGB16B<<30)|(ScaleUp_H_Pr<<29)|(ScaleUp_V_Pr<<28)|(MainHorRatio<<16)|(MainVerRatio);
    
	s24A0CAM->CIPRTAREA= PrDstWidth*PrDstHeight;

	
}


//Added by APR ++
//This thread is used to keep track of the idle time of the device. 
//Once the inactivity time period elapses, the device is gradually
//powered down one level at a time till D4 is reached.
DWORD WINAPI DeviceActivityThreadProc(LPVOID pvParam)
{	
	BOOL	fDone		= FALSE;
	LPTSTR	pszFname	= _T("Camera_DeviceThreadProc");
	DWORD	dwTimeout	= 0;
	TCHAR	szDeviceName[128];

	PCAM_INFO pCamInf = (PCAM_INFO)pvParam;
	CEDEVICE_POWER_STATE NewDx;
	
	//extract info from the head for local use
	LOCK(pCamInf);
	dwTimeout	= pCamInf->dwInactivityTimeout;
	fDone		= pCamInf->bKillActivityThread;
	//_stprintf(szDeviceName, "%s\\%s", _T("{A32942B7-920C-486b-B0E6-92A702A99B35}"), _T(pCamInf->szName));	//APRREMINDER hardcoding
	_tcscpy(szDeviceName, _T("{A32942B7-920C-486b-B0E6-92A702A99B35}\\"));

	_tcscat(szDeviceName, pCamInf->szName);

	UNLOCK(pCamInf);

	//create the event that indicates something is happening with our device.
	HANDLE hevActivity				= CreateEvent(NULL, FALSE, FALSE, CAM_ACTIVITY_EVENT);
	HANDLE hevKillCamActivityThread = CreateEvent(NULL, FALSE, FALSE, CAM_ACTIVITY_KILL_EVENT);
	//check if the events could be created
	if((NULL == hevActivity) || (NULL == hevKillCamActivityThread))
	{
		RETAILMSG(RETAIL_ON, (_T("CreateEvent('%s') failed %d\r\n"), CAM_ACTIVITY_EVENT, GetLastError()));
		return 1;								//denotes abnormal behaviour
	}
	
	HANDLE hEvents[2];
	hEvents[0] = hevActivity;
	hEvents[1] = hevKillCamActivityThread;
	//go in a continuous loop
	while(FALSE == fDone)
	{
		//wait until something happens or timeout occurs
		//DWORD dwStatus = WaitForSingleObject(hevActivity, dwTimeout);
		DWORD dwStatus = WaitForMultipleObjects(2, hEvents, FALSE, dwTimeout);
		switch(dwStatus) 
		{
		case WAIT_TIMEOUT:
			LOCK(pCamInf);
			
			// if we're "off" don't do anything
			if(sCurrentDx == D4)
				break;
			//RETAILMSG(RETAIL_ON, (_T("%s: inactivity timeout, Dx is %d, fBoost %d, fReduction %d\r\n"), 
			//  pszFname, sCurrentDx, spCamInfo->fBoostRequested, spCamInfo->fReductionRequested));
			
			
			//we don't want to automatically go all the way off (D4), or to "sleep" (D3), 
			//but in other cases step down our power consumption one level
			if((pCamInf->fReductionRequested == FALSE) && (sCurrentDx < D2))
			{
				//go to off state straightaway
				NewDx = (CEDEVICE_POWER_STATE)D4;//((DWORD) sCurrentDx + 1);
				RETAILMSG(RETAIL_ON, (TEXT("#####New Cam Dx is: %d\n"), NewDx));
				
				dwStatus = DevicePowerNotify(szDeviceName, NewDx, POWER_NAME);
				if(dwStatus != ERROR_SUCCESS)
					RETAILMSG(RETAIL_ON, (TEXT("DevicePowerNotify in DeviceActivityThreadProc (%u) failed %d \r\n"), NewDx, dwStatus));
					
				pCamInf->fReductionRequested = TRUE;
			}
			
			// don't need to maintain an inactivity timeout if we've gotten to our lowest power state
			if(sCurrentDx >= D2)
				dwTimeout = INFINITE;
			
			UNLOCK(pCamInf);
			break;
		case (WAIT_OBJECT_0 + 0):
			LOCK(pCamInf);
			//RETAILMSG(RETAIL_ON, (_T("%s: device activity, Dx is %d, fBoost %d, fReduction %d\r\n"), 
			//  pszFname, pds->CurrentDx, pds->fBoostRequested, pds->fReductionRequested));
			
			//are we at our top activity level?
			if(pCamInf->fBoostRequested == FALSE && sCurrentDx != D0) 
			{
				//we are below full power, try to go to D0
				NewDx = (CEDEVICE_POWER_STATE)D0;//((DWORD) sCurrentDx - 1);
				pCamInf->fBoostRequested = TRUE;
				dwStatus = DevicePowerNotify(szDeviceName, NewDx, POWER_NAME);
				if(dwStatus != ERROR_SUCCESS)
					RETAILMSG(RETAIL_ON, (_T("%s: DevicePowerNotify(D0) failed %d\r\n"), 
					pszFname, dwStatus));
			}
			
			//set the timeout back to normal
			dwTimeout = pCamInf->dwInactivityTimeout;
			
			UNLOCK(pCamInf);
			break;
		case (WAIT_OBJECT_0 + 1):
			fDone = TRUE;
			break;
			
		default:
			RETAILMSG(RETAIL_ON, (_T("%s: WaitForSingleObjects() returned %u, error %u\r\n"), 
				pszFname, dwStatus, GetLastError()));
			pCamInf->bKillActivityThread = TRUE;
			break;
		}

		//get the kill flag status for next iteration
		LOCK(pCamInf);
		fDone = pCamInf->bKillActivityThread;
		UNLOCK(pCamInf);
		sCurrentDx = NewDx;
	}

	//set the thread id to NULL
	pCamInf->hActivityThread = NULL;

	return 0;
}

// ****************************************************************
//
//	@doc		INTERNAL
//	@func		BOOL | StartActivityThread | Start thread 
//	@parm 		CAM_INFO* | pCam_Info
//	@rdesc		TRUE if success, FALSE if failed.
//
BOOL StartActivityThread(PCAM_INFO pCamInfo)
{
    // Set up the activity thread and it's kill flag. Note that the thread
    // fills in its own handle in pCamInfo.
	LOCK(pCamInfo);
    pCamInfo->hActivityThread		= NULL;
	pCamInfo->bKillActivityThread	= FALSE;
	pCamInfo->fBoostRequested		= FALSE;
	pCamInfo->fReductionRequested	= FALSE;
	ResetEvent(pCamInfo->hevCamActivity);
	ResetEvent(pCamInfo->hevKillCamActivity);

    pCamInfo->hActivityThread = CreateThread(NULL,0, DeviceActivityThreadProc,
		(LPVOID)pCamInfo, NULL, NULL);
    if(NULL == pCamInfo->hActivityThread)	//check if thread was created alright
	{
        RETAILMSG(RETAIL_ON, (TEXT("Error creating camera activity thread (%d)\n\r"), GetLastError()));
		UNLOCK(pCamInfo);
        return FALSE;
    }
	UNLOCK(pCamInfo);
	
	//successfully created thread, returning success
    return TRUE;
}

BOOL ActivateCamera(DWORD dwDeviceContext)
{
	PCAM_INFO	pCamInf				= (PCAM_INFO)dwDeviceContext;
	HANDLE		hCamActivityThread	= NULL;

	if(NULL == pCamInf)
		return FALSE;

	//
	LOCK(pCamInf);
	hCamActivityThread = pCamInf->hActivityThread;
	UNLOCK(pCamInf);

	//if the activity thread is not started, start it
	//not required at the time of initialization
	if(NULL == hCamActivityThread)
	{
		//notify the PM to change the device state to full powered
		DWORD dwStatus = DevicePowerNotify(_T("{A32942B7-920C-486b-B0E6-92A702A99B35}\\CIS1:"), (CEDEVICE_POWER_STATE)D0, POWER_NAME);
		if(dwStatus != ERROR_SUCCESS)
			RETAILMSG(RETAIL_ON, (TEXT("DevicePowerNotify in ActivateCamera (%u) failed %d \r\n"), 0, dwStatus));
		
		//start the activity thread again
		if(FALSE == StartActivityThread(pCamInf))
		{		
			return FALSE;							//failure to activate device
		}
	}

	//successfully activated camera, returning success
	return TRUE;
}
//APR --

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -