baseport/syborg/webcamera/webcamera_uvc.cpp
author Shimizu Satoshi <s_shimizu@isb.co.jp>
Mon, 18 Oct 2010 19:39:25 +0900
changeset 124 606eafc6d6a8
permissions -rw-r--r--
Obtain an image of Webcamera from QEMU and add the Bitmap change display function.

/*
* Copyright (c) 2010 ISB.
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the License "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* ISB - initial contribution.
*
* Contributors:
*
* Description: USB driver for test
*
*/

#ifndef WEBCAMERA_UVC_H_
#include "webcamera_uvc.h"
#endif

#define DP(format...) Kern::Printf(format)

//Describe UVC base.

/**
Constructor.
*/
DWebCameraUVCDescriptor::DWebCameraUVCDescriptor()
	{
	DP("DWebcameraUVCDescriptor::DWebcameraUVCDescriptor construct");
	}

/**
Destructor.
*/
DWebCameraUVCDescriptor::~DWebCameraUVCDescriptor()
	{
	DP("DWebcameraUVCDescriptor::DWebcameraUVCDescriptor destruct");
	}

/**
Describe UVC Descriptor.
@param		aDataInfo		[in]				DataInfo from lower layer.
@return		bDescriptor.
*/
TWebcameraUVCDescriptor DWebCameraUVCDescriptor::DescribeDescriptor(const TWebcameraDataInfo aDataInfo)
	{
	TWebcameraUVCDescriptor bDescriptor;
	return bDescriptor;
	}

//Describe UVC descriptor YUY2 class.

/**
Constructor.
*/
DWebCameraUVCDescribeDescriptorYUY2::DWebCameraUVCDescribeDescriptorYUY2()
	{
	DP("DWebCameraUVCDescribeDescriptorYUY2::DWebCameraUVCDescribeDescriptorYUY2 construct");
	}

/**
Destructor.
*/
DWebCameraUVCDescribeDescriptorYUY2::~DWebCameraUVCDescribeDescriptorYUY2()
	{
	DP("DWebCameraUVCDescribeDescriptorYUY2::DWebCameraUVCDescribeDescriptorYUY2 destruct");
	}

/**
Describe UVC Descriptor of YUY2 Format.
@param		aDataInfo		[in]					DataInfo from lower layer.
@return 	bDescriptor.
*/
TWebcameraUVCDescriptor DWebCameraUVCDescribeDescriptorYUY2::DescribeDescriptor(const TWebcameraDataInfo aDataInfo)
	{
	DP("DWebcameraUVCDescribeDescriptorYUY2::DescribeDescriptor Start");
	//Describe FormatDescriptor Part.
	TWebcameraUVCDescriptor bDescriptor;

	bDescriptor.iFormatDescriptor.iLength = 27;
	bDescriptor.iFormatDescriptor.iDescriptorType = KWebcameraSCVideoStreaming;
	bDescriptor.iFormatDescriptor.iDescriptorSubType = KWebcameraVSFormatUnconmpressed;

	//bFormatIndex : First and only format descriptor.
	//This field is set by the host.
	bDescriptor.iFormatDescriptor.iFormatIndex = 0x01;

	bDescriptor.iFormatDescriptor.iNumFrameDescriptor = KWebcameraVSFrameUncompressed;
	bDescriptor.iFormatDescriptor.iGuid = aDataInfo.iDataFortmat.iSubFormat;
	bDescriptor.iFormatDescriptor.iBitsPerPixel = aDataInfo.iVideoInfoHeader.iBmiHeader.iBitCount;

	//bDefaultFrameIndex : Default frame index is 1.
	bDescriptor.iFormatDescriptor.iDefaultFrameIndex = 0x01;

	bDescriptor.iFormatDescriptor.iAspectRatioX = 4;

	bDescriptor.iFormatDescriptor.iAspectRatioY = 3;

	//InterlaceFlags.		//VideoInfoHeader2 dwInterlaceFlags.
	//No interlaced stream.
	bDescriptor.iFormatDescriptor.iInterlaceFlags = 0x00;

	//0: No restrictions.
	bDescriptor.iFormatDescriptor.iCopyProtect = 0x00;

	//Describe FrameDescriptor Part.
	bDescriptor.iFrameDescriptor.iDescriptorType = KWebcameraSCVideoStreaming;
	bDescriptor.iFrameDescriptor.iDescriptorSubType = KWebcameraVSFrameUncompressed;

	//First and only frame descriptor.
	bDescriptor.iFrameDescriptor.iFrameIndex = 0x01;

	//Still images using capture method 0 are supported at this frame setting.D1: Fixed frame-rate.
	//D2~D7 reserved.
	bDescriptor.iFrameDescriptor.iCapabilities = 0x02;

	bDescriptor.iFrameDescriptor.iWidth = (TUint16)aDataInfo.iVideoInfoHeader.iBmiHeader.iWidth;
	bDescriptor.iFrameDescriptor.iHeight = (TUint16)aDataInfo.iVideoInfoHeader.iBmiHeader.iHeight;

	//iDwBitRate = 147456000 = 640 * 480 * 16 * 30(FPS).
	//bDescriptor.iFrameDescriptor.iMaxBitRate.
	bDescriptor.iFrameDescriptor.iMaxBitRate = aDataInfo.iVideoInfoHeader.iDwBitRate;
	//bDescriptor.iFrameDescriptor.iMinBitRate.
	bDescriptor.iFrameDescriptor.iMinBitRate = aDataInfo.iVideoInfoHeader.iDwBitRate;

	//bDescriptor.iFrameDescriptor.iMaxVideoFrameBufferSize.
	//max frame size is 640 * 480 * 2 = 614400.
	bDescriptor.iFrameDescriptor.iMaxVideoFrameBufferSize = aDataInfo.iVideoInfoHeader.iBmiHeader.iSizeImage;

	//bDescriptor.iFrameDescriptor.iDefaultFrameInterval.
	//147456000 / 640 / 480 / 16 = 30(FPS) -> 33.3ms -> 33,333.3ƒÊs -> 333333.3 * 100ns = 333333 = iAvgTimePerFrame.
	bDescriptor.iFrameDescriptor.iDefaultFrameInterval = (TUint32)aDataInfo.iVideoInfoHeader.iAvgTimePerFrame;

	//0: Continuous frame interval.
	bDescriptor.iFrameDescriptor.iFrameIntervalType = 0x00;

	//bDescriptor.iFrameDescriptor.iLength.
	if (bDescriptor.iFrameDescriptor.iFrameIntervalType == 0)
		{
		bDescriptor.iFrameDescriptor.iFrameInterval.iFrameInterval1 = (TUint32)aDataInfo.iVideoInfoHeader.iAvgTimePerFrame;
		//highest frame rate.

		bDescriptor.iFrameDescriptor.iFrameInterval.iFrameInterval2 = (TUint32)aDataInfo.iVideoInfoHeader.iAvgTimePerFrame;
		//lowest frame rate.
		
		bDescriptor.iFrameDescriptor.iFrameInterval.iFrameInterval3 = (TUint32)aDataInfo.iVideoInfoHeader.iAvgTimePerFrame;
		//Indicates granularity of frame interval range.
		
		bDescriptor.iFrameDescriptor.iLength = 38;
		//iFrameIntervalType = 0 : 38, > 0 : 26+(4*n).
		}
	return bDescriptor;
	}

//UVC heder class.

/**
Constructor.
*/
DWebCameraUVCHeader::DWebCameraUVCHeader()
	{
	DP("DWebCameraUVCHeader::DWebCameraUVCHeader construct");
	}

/**
Destructor.
*/
DWebCameraUVCHeader::~DWebCameraUVCHeader()
	{
	DP("DWebCameraUVCHeader::DWebCameraUVCHeader destruct");
	}

/**
Describe UVC Header.
@param	aDataInfo	[in]				Information of data from lower layer.
@return	bHeader							UVC Header descibed.
*/
TWebcameraUVCHeaderFormat DWebCameraUVCHeader::DescribeHeader(const TWebcameraDataInfo aDataInfo)
	{
	TWebcameraDataInfo bDataInfo = aDataInfo;
	TWebcameraUVCHeaderFormat bHeader;

	//Describe HeaderInfo to be included UVC Header.
	bHeader.iHeaderInfo = DescribeHeaderInfo(bDataInfo);

	//Describe HeaderLength to be included UVC Header if PTS was set.
	bHeader.iHeaderLength = DescribeHeaderLength(bDataInfo, bHeader.iHeaderInfo);

	//Describe PresentationTime to be included UVC Header.
	if ((bHeader.iHeaderInfo & KWebcameraPts) > 0)
		{
		bHeader.iPresentationTime = DescribePresentationTime(bDataInfo);
		}

	//Describe SourceClockReference to be included UVC Header if SCR was set.
	if ((bHeader.iHeaderInfo & KWebcameraScr) > 0)
		{
		bHeader.iSourceClockReference = DescribeSourceClockReference(bDataInfo);
		}
	return bHeader;
	}

/**
Describe HeaderInfo of UVC Header.
@param	aDataInfo					[in]	DataInfo get from lower layer.
@return	bHeaderInfo.
*/
TUint8 DWebCameraUVCHeader::DescribeHeaderInfo(const TWebcameraDataInfo aDataInfo)
	{
	TUint8 bHeaderInfo = 0x00;

//	bHeaderInfo |= KWebcameraEoh;

//	bHeaderInfo |= KWebcameraErr;

//	bHeaderInfo |= KWebcameraSti;

//	bHeaderInfo |= KWebcameraScr;

	//Case: Set PTS infomation.
	if (aDataInfo.iVideoInfoHeader.iAvgTimePerFrame != 0)
		{
		bHeaderInfo |= KWebcameraPts;
		}

//	bHeaderInfo |= KWebcameraEof;

	//Reversing FID.
	iFID = ~iFID;
	iFID &= KWebcameraFid;
	bHeaderInfo |= iFID;

	return bHeaderInfo;
	}

/**
Describe HeaderLength to be included UVC Header.
@param	aDataInfo		[in]								Information of data from lower layer.
		aHeaderInfo		[in]								HeaderInfo to be included UVC Header.
@return	bHeaderLength.
*/
TUint8 DWebCameraUVCHeader::DescribeHeaderLength(const TWebcameraDataInfo aDataInfo, TUint8 aHeaderInfo)
	{
	TUint8 bHeaderLength;
	bHeaderLength = 2;

	//Get information of PTS .
	if ((aHeaderInfo & KWebcameraPts) > 0)
		{
		bHeaderLength += 4;
		}

	//Get information of SCR.
	if ((aHeaderInfo & KWebcameraScr) > 0)
		{
		bHeaderLength += 6;
		}

	return bHeaderLength;
	}

/**
Describe PresentationTime to be included UVC Header.
@param	@param	aDataInfo						[in]	Information of Data from lower layer.
@return	bPresentationTime.
*/
TUint32 DWebCameraUVCHeader::DescribePresentationTime(const TWebcameraDataInfo aDataInfo)
	{
	TUint32 bPresentationTime;
	//Substitution information of AvgTimePerFrame for PresentationTime.
	bPresentationTime = (TUint32)aDataInfo.iVideoInfoHeader.iAvgTimePerFrame;

	return bPresentationTime;
	}

/**
Describe SourceClockReference to be included UVC Header.
@param	aDataInfo					[in]	Information of data from lower layer.
@return	bSourceClockReference.
*/
TWebcameraUVCSourceClockReference DWebCameraUVCHeader::DescribeSourceClockReference(const TWebcameraDataInfo aDataInfo)
	{
	TWebcameraUVCSourceClockReference bSourceClockReference;
	//Substitute information of TokenCounter for iTokenCounter.
	bSourceClockReference.iTokenCounter = 0x0000;
	bSourceClockReference.iTokenCounter &= KWebcameraTokenCounter;

	//Substitute information of SourceTimeClock for iSourceTimeClock.
//	bSourceClockReference.iSourceTimeClock = (TUint32) aSourceTimeClock;

	return bSourceClockReference;
	}

//Describe class.

/**
Constructor.
*/
DWebCameraDescribe::DWebCameraDescribe()
	{
	DP("DWebCameraDescribe::DWebCameraDescribe construct");
	}

/**
Destructor.
*/
DWebCameraDescribe::~DWebCameraDescribe()
	{
	delete iDescribe;
	DP("DWebCameraDescribe::DWebCameraDescribe destruct");
	}
	
/**
Parse iDataInfo from lower layer and describe UVC Header and Descriptor.
@param	aDataInfo					[in]	Information of data from lower layer.
@return	bUVC.
*/
TWebcameraUVC DWebCameraDescribe::ParseDataInfo(const TWebcameraDataInfo aDataInfo)
	{
	TWebcameraDataInfo bDataInfo = aDataInfo;
	TWebcameraUVC bUvc;

	//Describe UVC Header.
	DWebCameraUVCHeader* bHeader;
	bHeader = new DWebCameraUVCHeader;
	bUvc.iUVCHeader = bHeader->DescribeHeader(bDataInfo);
	delete bHeader;

	//If MajorFormat equal KWebcameraVideoStream, this format is VideoStream.
	if (bDataInfo.iDataFortmat.iMajorFormat.iData1 == KWebcameraVideoStream)
		{
		//Discriminate by SubFormat.
		switch (bDataInfo.iDataFortmat.iSubFormat.iData1)
			{
			//Case of YUY2 Format.
			case KWebcameraYuy2:
				iDescribe = new DWebCameraUVCDescribeDescriptorYUY2;
				break;
			
			//Case of default.
			default:
				//No format that discriminated.
				break;
			}
		//Describe UVC Descriptor.
		bUvc.iUVCDescriptor = iDescribe->DescribeDescriptor(bDataInfo);
		}

	return bUvc;
	}