<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">

<title>OpenCV</title>
<style>
<!--
table.MsoTableGrid
	{border:1.0pt solid windowtext;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.0pt;
	font-family:"Times New Roman"}
h3
	{margin-top:6.0pt;
	margin-right:0cm;
	margin-bottom:3.0pt;
	margin-left:36.0pt;
	text-indent:-36.0pt;
	line-height:12.0pt;
	page-break-after:avoid;
	font-size:10.0pt;
	font-family:Arial;
	font-weight:normal;
	font-style:italic;
	}
-->
</style>
</head>

<body>

<pre>              <b><font size="6" color="#FF0000"> <span lang="en-ca">The Learning of OpenCV</span></font></b></pre>
<pre>					
</pre>
<pre>
</pre>
<pre>Here contains just some personal notes for learning OpenCV and inevitably most parts of them are from their sample codes.</pre>
<pre><font color="#FF0000" size="4"><a name="adaptive-threshold"><b>Here is the first test of adaptive-threshold.</b></a></font><a name="adaptive-threshold"> </a></pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)


void myContour(LPTSTR szFileName)
{
	const int Max_Threshold = 2;
	const int MyStep = 255;
	const char* pszWndName[] = {&quot;pSrcImage&quot;, &quot;cvPyrDown&quot;, &quot;cvPyrUp&quot;, &quot;cvCopy&quot;, &quot;pDst&quot;, &quot;contour&quot;, &quot;clone&quot;};
	IplImage* pSrcImage = NULL;
	CvMemStorage* pMem = NULL;
	CvSize sz;
	IplImage* pImage = NULL;
	IplImage* pSrc, *pDst = NULL;
	IplImage* pPyramid = NULL;
	CvSeq* pHeader = NULL;
	IplImage* pContour = NULL;
	IplImage* pEmpty = NULL;
	IplImage* pClone = NULL;

	int i, j;

	pImage = cvLoadImage(szFileName, 1);

	pMem = cvCreateMemStorage(0);

	sz.width = (pImage-&gt;width&amp;-2);
	sz.height = (pImage-&gt;height&amp;-2);

	pSrcImage = cvCreateImage(sz, 8, 3);

	cvSetImageROI(pImage, cvRect(0, 0, sz.width, sz.height));

	cvCopy(pImage, pSrcImage, 0);

	pPyramid = cvCreateImage(cvSize(sz.width/2, sz.height/2), 8, 3);

	pSrc = cvCreateImage(sz, 8, 1);

	pDst = cvCreateImage(sz, 8, 1);

	//cvSetImageROI(pImage, cvRect(0,0,sz.width, sz.height));

	for (i = 0; i &lt; sizeof(pszWndName)/sizeof(const char*); i ++)
	{
		cvNamedWindow(pszWndName[i], 1);
	}

	cvShowImage(pszWndName[0], pSrcImage);

	cvPyrDown(pSrcImage, pPyramid, 7);

	cvShowImage(pszWndName[1], pPyramid);

	cvPyrUp(pPyramid, pSrcImage, 7);

	cvShowImage(pszWndName[2], pSrcImage);

	//cvWaitKey(0);

	cvSetImageROI(pSrcImage, cvRect(0, 0, sz.width, sz.height));
	for (i = 0; i &lt; 3;  i ++)
	{
		cvSetImageCOI(pSrcImage, i + 1);
		cvCopy(pSrcImage, pSrc, 0);

		cvShowImage(pszWndName[3], pSrc);

		for (j = 1; j &lt; Max_Threshold; j ++)
		{
			switch (j)
			{
			case 0:
				cvCanny(pSrc, pDst, 200, 255, 5);				
				// dilate canny output to remove potential
				// holes between edge segments 
				cvDilate(pDst, pDst, 0, 1 );
				break;
			case 1:
				
				//
				cvAdaptiveThreshold(pSrc, pDst, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 7, 2);
				break;
			default:
				cvThreshold(pSrc, pDst, (j+1)*MyStep/Max_Threshold, MyStep, CV_THRESH_BINARY);
				break;

			}


			pClone = cvCloneImage(pDst);
			pContour = cvCreateImage(cvSize(pDst-&gt;width, pDst-&gt;height), 8, 3);
			pEmpty = cvCreateImage(cvSize(pDst-&gt;width, pDst-&gt;height), 8, 3);


			for (int k = 0; k &lt; 4;  k ++)
			{
				cvFindContours(pClone, pMem, &amp;pHeader, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
				
				if (pHeader)
				{
					if (k == 0)
					{
						cvDrawContours(pEmpty, pHeader, cvScalarAll(0), cvScalarAll(0), 100, 1);
					}
					//cvDrawContours(pContour, pHeader, cvScalarAll(0), cvScalarAll(0), 100, 1);
					cvDrawContours(pClone, pHeader, cvScalarAll(0), cvScalarAll(0), 100, 1);
				}
			}
		
			if (pHeader)
			{
				cvDrawContours(pContour, pHeader, cvScalarAll(0), cvScalarAll(0), 100, 1);
				//cvDrawContours(pClone, pHeader, cvScalarAll(0), cvScalarAll(0), 100, 1);
			}	

		

			cvShowImage(pszWndName[5], pContour);
			cvShowImage(pszWndName[4], pDst);
			cvShowImage(pszWndName[6], pEmpty);
		
			cvWaitKey(0);
		}
	}
	cvReleaseImage(&amp;pSrcImage);
	cvReleaseImage(&amp;pImage);
	cvReleaseImage(&amp;pSrc);
	cvReleaseImage(&amp;pDst);
	cvReleaseImage(&amp;pPyramid);
	cvReleaseImage(&amp;pContour);
	cvReleaseImage(&amp;pClone);
	cvReleaseImage(&amp;pEmpty);


	cvDestroyAllWindows();
}

typedef int (*HandleFileCallBack)(char*dir, char* fileName, void* userData);

int myContourCallback(LPTSTR dir, LPTSTR fileName, void* userData)
{
	TCHAR szNameFile[MAX_PATH];
	LPTSTR ptr = NULL;
	static int counter = 0;
	if (rand()% 3 ==0)
	{
		if ((ptr = _tcsrchr(fileName, _T('.')))!= NULL)
		{
			if (_tcsicmp(ptr, _T(&quot;.jpg&quot;))==0)
			{
				_stprintf(szNameFile, &quot;%s\\%s&quot;, dir, fileName);
				myContour(szNameFile);
			}
		}
	}
	return 0;
}

int genericFind(TCHAR* dir, HandleFileCallBack handleFileCallBack, void* userData)
{
	HANDLE handle;
	int counter = 0, temp;
	TCHAR curFileName[MAX_PATH];
	TCHAR wildFileName[MAX_PATH];
	
	WIN32_FIND_DATA ffd;

	_sntprintf(wildFileName, MAX_PATH, _T(&quot;%s\\*.*&quot;), dir);
	handle=FindFirstFile(wildFileName, &amp;ffd);
	if (handle==INVALID_HANDLE_VALUE)
	{
		_tprintf(_T(&quot;findfirst failed of error code =%d\n&quot;), GetLastError());
		exit(19);
	}
	do
	{	
		if (_tcsicmp(ffd.cFileName, _T(&quot;.&quot;))!=0 &amp;&amp; _tcsicmp(ffd.cFileName, _T(&quot;..&quot;))!=0)
		{
			_stprintf(curFileName, _T(&quot;%s\\%s&quot;), dir, ffd.cFileName);
			if  (GetFileAttributes(curFileName)&amp;FILE_ATTRIBUTE_DIRECTORY)
			{
				counter += genericFind(curFileName, handleFileCallBack, userData);
			}
			else
			{		
				// check extention name
				if ((temp = handleFileCallBack(dir, ffd.cFileName, userData)) == -1)
				{
					counter = -1;
					break;
					
				}
				else
				{
					counter += temp;
				}
			}
		}
	}
	while (FindNextFile(handle, &amp;ffd));	
	
	FindClose(handle);
	return counter;
}


int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	//CommandLineToArgvW(GetCommandLine(), &amp;argc);
	srand(time(0));
	genericFind(&quot;f:\\downloaded\\ladytang&quot;, myContourCallback, NULL);
	return 0;

}</pre>
<pre>　</pre>
<pre><a name="material">还是谷歌拼音好用。摘录了一下资料</a>。</pre>
<pre>http://blog.ednchina.com/FIB/20481/category.aspx</pre>
<div class="article">
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/97789/message.aspx">ASF学习笔记 
			</a></div>
		<div class="article_info">
			发表于 2008/3/25 17:30:00
                    </div>
		<div class="article_content">
			VIA:&nbsp;
			<a href="http://hi.baidu.com/koko200147/blog/item/cfea4af4008a44ddf3d38526.html">
			http://hi.baidu.com/koko200147/blog/item/cfea4af4008a44ddf3d38526.html</a><p>
			&nbsp;</p>
			<p>设置(Profile)</p>
			<p>一个设置是一个ASF的配置(configuration)的描述数据集合。一个设置必须至少包含一个流的配置设置。</p>
			<p>流信息<br>
			设置中的流信息包含流的比特率(bit 
			rate)，缓冲窗口和媒体属性的设置。视频和音频的流信息准确描述了文件中的媒体配置，包括压缩数据使用的编码和解码器（如果有的话）。</p>
			<p>一个设置也包含很多创建ASF文件时使用的ASF的特性，这包括互斥、媒体优先级、带宽共享和数据单位扩展。</p>
			<p>每次写文件时必须提供设置。你可以调用IWMWriter::SetProfile指定一个设置。</p>
			<p>设置有三种形式，应用程序中设置对象包含的数据，XML文件，或者ASF文件头。</p>
			<p>设置对象<br>
			可以用设置管理器创建空设置对象，然后从现有数据载入设置</p>
			<p>XML文件<br>
			具有PRX扩展名.注意Windows Media 9 Series 中没有原来的系统设置(system 
			profiles)也不再使用，而作为这种形式存在。保存自定义设置时必须保存成这种文件。</p>
			<p>ASF文件头<br>
			ASF读者创建一个设置对象，然后从ASF文件头载入格式信息。但是修改文件头不会影响文件的内容。可以重新对文件编码来完成格式的修改。</p>
			<p>使用设置编辑器<br>
			除了用Windows Media Format SDK之外，还可以用Windows Media Encoder 9 
			Series中包含的设置编辑器创建设置。在应用程序中使用IWMProfileManager::LoadProfileByData载入预定义的设置。但是，启用“视频大小：和输入相同”这个选项将设置视频的大小为0；Windows 
			Media Encoder 9 Series可以识别并且处理这种情况，但是Windows Media Format 
			SDK的写入对象不会自动处理，所以应用程序必须并且处理这种情况.</p>
			<p>下面是一个XML格式的配置</p>
			<p>&lt;profile version=&quot;589824&quot; storageformat=&quot;1&quot; name=&quot;ICW&quot; 
			description=&quot;ICW Stream&quot;&gt;<br>
			// 73647561-0000-0010-8000-00AA00389B71 'auds' == WMMEDIATYPE_Audio
			<br>
			&lt;streamconfig majortype=&quot;{73647561-0000-0010-8000-00AA00389B71}&quot; 
			streamnumber=&quot;1&quot; streamname=&quot;Audio Stream&quot; inputname=&quot;Audio804&quot; 
			bitrate=&quot;1411200&quot; bufferwindow=&quot;-1&quot; reliabletransport=&quot;0&quot; 
			decodercomplexity=&quot;&quot; rfc1766langid=&quot;zh-cn&quot;&gt;<br>
			&nbsp;&nbsp; // 00000001-0000-0010-8000-00AA00389B71&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
			WMMEDIASUBTYPE_PCM <br>
			&nbsp;&nbsp; &lt;wmmediatype subtype=&quot;{00000001-0000-0010-8000-00AA00389B71}&quot; 
			bfixedsizesamples=&quot;1&quot; btemporalcompression=&quot;0&quot; lsamplesize=&quot;4&quot;&gt;<br>
			&nbsp;&nbsp;&nbsp; &lt;waveformatex wFormatTag=&quot;1&quot; nChannels=&quot;2&quot; nSamplesPerSec=&quot;44100&quot; 
			nAvgBytesPerSec=&quot;176400&quot; nBlockAlign=&quot;4&quot; wBitsPerSample=&quot;16&quot; /&gt; <br>
			&nbsp;&nbsp; &lt;/wmmediatype&gt;<br>
			&lt;/streamconfig&gt;<br>
			// 73647561-0000-0010-8000-00AA00389B71 'auds' == WMMEDIATYPE_Audio
			<br>
			&lt;streamconfig majortype=&quot;{73646976-0000-0010-8000-00AA00389B71}&quot; 
			streamnumber=&quot;2&quot; streamname=&quot;Video Stream&quot; inputname=&quot;Video804&quot; 
			bitrate=&quot;4000&quot; bufferwindow=&quot;1000&quot; reliabletransport=&quot;0&quot; 
			decodercomplexity=&quot;AU&quot; rfc1766langid=&quot;zh-cn&quot;&gt;<br>
			&nbsp;&nbsp; &lt;videomediaprops maxkeyframespacing=&quot;80000000&quot; quality=&quot;35&quot; /&gt;
			<br>
			&nbsp;&nbsp; // 56555949-0000-0010-8000-00AA00389B71 'YV12' == 
			MEDIASUBTYPE_IYUV <br>
			&nbsp;&nbsp; &lt;wmmediatype subtype=&quot;{56555949-0000-0010-8000-00AA00389B71}&quot; 
			bfixedsizesamples=&quot;1&quot; btemporalcompression=&quot;0&quot; lsamplesize=&quot;0&quot;&gt;<br>
			&nbsp;&nbsp;&nbsp; &lt;videoinfoheader dwbitrate=&quot;4000&quot; dwbiterrorrate=&quot;0&quot; 
			avgtimeperframe=&quot;1000000&quot;&gt;<br>
			&nbsp;&nbsp;&nbsp;&nbsp; &lt;rcsource left=&quot;0&quot; top=&quot;0&quot; right=&quot;0&quot; bottom=&quot;0&quot; /&gt; <br>
			&nbsp;&nbsp;&nbsp;&nbsp; &lt;rctarget left=&quot;0&quot; top=&quot;0&quot; right=&quot;0&quot; bottom=&quot;0&quot; /&gt; <br>
			&nbsp;&nbsp;&nbsp;&nbsp; &lt;bitmapinfoheader biwidth=&quot;0&quot; biheight=&quot;0&quot; biplanes=&quot;1&quot; 
			bibitcount=&quot;12&quot; bicompression=&quot;IYUV&quot; bisizeimage=&quot;0&quot; bixpelspermeter=&quot;0&quot; 
			biypelspermeter=&quot;0&quot; biclrused=&quot;0&quot; biclrimportant=&quot;0&quot; /&gt; <br>
			&nbsp;&nbsp;&nbsp; &lt;/videoinfoheader&gt;<br>
			&nbsp;&nbsp; &lt;/wmmediatype&gt;<br>
			&lt;/streamconfig&gt;<br>
			// 73636d64-0000-0010-8000-00AA00389B71 'scmd' == WMMEDIATYPE_Script
			<br>
			&lt;streamconfig majortype=&quot;{73636D64-0000-0010-8000-00AA00389B71}&quot; 
			streamnumber=&quot;3&quot; streamname=&quot;Script Stream&quot; inputname=&quot;Script804&quot; 
			bitrate=&quot;2560&quot; bufferwindow=&quot;-1&quot; reliabletransport=&quot;0&quot; 
			decodercomplexity=&quot;&quot; rfc1766langid=&quot;zh-cn&quot;&gt;<br>
			&nbsp;&nbsp; &lt;wmmediatype subtype=&quot;{00000000-0000-0000-0000-000000000000}&quot; 
			bfixedsizesamples=&quot;0&quot; btemporalcompression=&quot;0&quot; lsamplesize=&quot;0&quot;&gt;<br>
			&nbsp;&nbsp; // 82f38a70-c29f-11d1-97ad-00a0c95ea850&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
			WMSCRIPTTYPE_TwoStrings <br>
			&nbsp;&nbsp; &lt;WMSCRIPTFORMAT scripttype=&quot;{82F38A70-C29F-11D1-97AD-00A0C95EA850}&quot; 
			/&gt; <br>
			&nbsp;&nbsp; &lt;/wmmediatype&gt;<br>
			&lt;/streamconfig&gt;<br>
			&lt;/profile&gt;</p>
			<p>媒体采样(Media Sample)<br>
			媒体采样，或者采样，是一块数字媒体数据。采样是Windows Media Format 
			SDK可以读写的数据的最小单位。采样内容由采样相关的媒体类型指出。对于视频，每个采样表示一个桢，每个单独采样中包含的数据量由创建ASF时指定的设置设置。<br>
			采样可以包含未压缩的数据，或者压缩过的数据，这时被称为流采样。创建ASF时，采样被传递给写入对象，写入对象使用相关的编码器压缩数据，并且写入ASF文件的数据段。播放时，读出对象读出压缩的数据，解压数据，并且提供未压缩格式的数据。<br>
			采样被封装在Windows Media Format 
			SDK的自动分配的缓冲区对象中。需要的时候，你也可以自己分配缓冲区对象，使用它的读写特性。<br>
			这里的采样并非音频采样。通常，音频采样质量用每秒录制的采样数据数量表示，例如CD质量是44,100采样/秒，或者44.1 kHz。</p>
			<p>输入，流和输出<br>
			输入对象是你用于写入文件的任何数字媒体流，必须是可以支持的格式。支持很多标准RGB和YUV作为视频输入格式，PCM作为音频输入格式。如果编码器不支持某种输入格式，那么写入对象会初始化一个辅助对象，转换输入流到可以支持的格式，例如调整色深转换、缩放，调整声音质量、采样率和频道数目。某些情况下，压缩格式的食品和音频可用于输入。输入也可以是其他格式，例如文字，脚本命令，图像，或者任意文件数据。</p>
			<p>输出是读取对象传递给应用程序，提供用户体验的数据。一个输出等同于一个流。如果你使用互斥属性，那么所有互斥数据共享一个输出。</p>
			<p>
			一个流是一个ASF文件中包含的数据。一个流的生命期中只有一种压缩设置。一个简单的ASF具有两种流：视频和音频。更加复杂的ASF文件可以包含两路音频和多路视频。音频可以有同样的压缩设置，但是内容不同，例如不同语言的讲解；视频可以有同样的内容，但是具有不同的压缩比例。格式是在设置对象中指定的。</p>
			<p>某些输入可以是压缩过的，这时读取对象必须以流编号依次访问数据，而不是按输出顺序访问数据。</p>
			<p>编号<br>
			流具有从1开始的编号，这是在设置中指定的。同时，流具有一个索引以在设置中枚举流。这两个数字并不相关，例如输入1并不一定是编号为1的流，编号为1的流并不一定是输入1，等等。</p>
			<p>格式<br>
			每种媒体类型的全部信息。每个格式有一个主类型，例如音频或视频，并且可能有一个子类型。格式包含依赖于主类型的不同信息。视频和音频格式比其他格式需要更多信息。</p>
			<p>输入格式<br>
			描述你传递给写入对象的数字媒体类型。如果ASF文件中的流是用编码器压缩，那么编码器只支持某些输入格式。使用Windows Media 
			音频和视频编码器时，可以使用写入对象枚举支持的输入格式。写到文件时，你有责任选择一个匹配输入媒体的输入格式。<br>
			某些格式不必匹配编码器指明的输入格式，编码器可以自行转换数据到需要的格式。</p>
			<p>流格式<br>
			ASF文件中的数据保存形式。在设置中描述，可以符合或不符合输入、输出格式（例如使用了某种编码/解码器）。可能必须获得编码/解码器信息之后，才可以设置流格式</p>
			<p>输出格式。<br>
			描述你传递给读出对象的数字媒体类型。如果ASF文件中的流是用编码器压缩，那么编码器只支持某些输出格式。使用Windows Media 
			音频和视频编码器时，可以使用读出对象枚举支持的输出格式。读出文件时，你有责任选择一个匹配输出媒体的输出格式。<br>
			某些格式不必匹配编码器指明的输出格式，编码器可以自行转换数据到需要的格式。</p>
			<p>比特率(Bit Rate)<br>
			每秒传递给ASF的数据的数量，以位/秒(bps)或者千位/秒(kbps)为单位。经常与带宽混淆，带宽也以bps或者kbps为单位。<br>
			如果用户的带宽小于ASF的比特率,那么播放可能中断。通常，带宽不足会导致跳过某些采样，或者更多的数据缓冲时间。<br>
			每个ASF文件创建时被指定一个比特率，它基于文件中流的数量。不同的流可以有不同的比特率。比特率可以是常数（压缩的数据可以以基本同样的速度被传输）或者可变（保留压缩的数据质量，即使可能造成突发数据溢出）。<br>
			同一个内容可以被压缩成多个比特率不同的流，然后你可以配置他们为互斥的。这个属性叫多比特率(multiple bit rate), 
			或者MBR.</p>
			<p>元数据<br>
			描述ASF文件或者文件内容的信息，位于文件头。元数据的项称为属性。每一个属性由名字和值组成。全局常数用于标识属性，例如ASF 
			文件的标题被保存在 g_wszWMTitle 属性中。在Windows Media Format SDK 
			中定义了最常用的内建属性，但是你也可以定义自己的属性。由于其他开发者可能和你是用同样的名字，所以可能造成冲突。<br>
			一些全局属性可以被修改，例如g_wszWMSeekable属性（文档是否可以从任意点被读取）<br>
			一些属性纯粹用于信息用途，并且必须被设置，例如g_wszWMAuthor属性（作者）<br>
			属性可以被应用到整个文件或者单独的流。<br>
			你可以用Windows Media Format 
			SDK编辑MP3文件的元数据，但是必须使用ID3-compliant属性保留与其他MP3应用程序的兼容性。</p>
			<p>媒体时间<br>
			自第一个采样开始的时间计量方式，单位和SDK其他时间的单位一样，是100纳秒。它使得文件中不同的流可以被同步。你写入的每一个采样都必须有媒体时间。ASF文件数据段中每一个数据对象都有媒体时间。每一个输出的数据也都有媒体时间。</p>
			<p>缓冲<br>
			读取对象打开流文件时从文件头的信息决定缓冲区大小。实际比特率是变化的，但是平均值应该是设置中指定的值。</p>
			<p>缓冲窗口是以可以缓冲的数据时间长度来衡量的。例如，32Kbps的流，3秒的缓冲窗口，意味着缓冲区大小为 
			12,000字节(32000*3/8)。解码器限制了这个数值，所以缓冲窗口的平均比特率不大于流的比特率。<br>
			通常在设置中指定这个值，写入对象处理剩下的部分。写入压缩数据到流时，必须自己确定写入的速度不会超出这个值</p>
			<p>ASF文件中的段<br>
			一个ASF文件中的段以对象的方式组织起来。一共有三种顶层对象，必须有的头对象(Head),数据对象(Data)，以及可选的索引对象(Index)。</p>
			<p>每个对象都以全球唯一标志(GUID)和大小开始。这些数字使得文件读者可以解析这些信息，并且载入到相应的对象。因为这些GUID，底层的对象可以以任何顺序排列，并且仍然可以被识别。这使得一个不完整的ASF文件仍然可被正确读取，只要有一个完整的文件头和至少一个数据对象。某些对象，例如流属性对象，可能有多个示例。</p>
			<p>头对象包含文件的描述信息，同时是唯一的顶层对象容器。</p>
			<p>数据对象以包的格式存储流数据。数据对象还具有文件ID和包总个数属性，但是对于流格式，包总个数属性没有意义。</p>
			<p>每一个数据包包含发送时间和持续时间。这使得读者可以发现流传输的中断。<br>
			数据包的数据被封装到载荷(payloads)中。一个载荷可以包含一个或者多个媒体对象（media 
			objects），媒体对象的一个例子是视频流的一个桢。大的媒体对象，例如视频流的一个关键桢，可能被扩展到多个载荷，甚至多个包。为了跟踪对象的片断，每个对象的段具有从0到255的编号。<br>
			除了数据之外，载荷也具有以毫秒为单位的时间戳。<br>
			所有的包具有头对象中指定的统一的大小。当一个包包含的数据少于指定大小时，用数据（&quot;padding&quot; data ）填充不足部分。</p>
			<p>索引对象包含时间《-》关键桢的配对，以更有效地在文件中定位。因为它处于文件末尾，实时媒体不能访问这个对象。</p>
			<p>使用回调方法<br>
			一些Windows Media Format SDK的接口的方法是异步执行的，很多这样的方法使用回调方法和应用程序通讯。</p>
			<p>使用OnStatus回调<br>
			在Windows Media Format SDK中，IWMStatusCallback::OnStatus 
			被很多对象调用。OnStatus接收SDK操作状态的变化。每种对象可能有不同的方式连接到IWMStatusCallback。</p>
			<p>使用事件进行同步调用<br>
			1 使用Platform SDK的API CreateEvent创建一个事件对象<br>
			2 实现回调函数，，捕获事件，并且调用SetEvent函数标记事件对象<br>
			3 在应用程序中调用WaitForSingleObject 
			、监视事件对象。如果你是在为Windows程序编写代码，你必须创建一个消息循环对用户操作做出相应。</p>
			<p>使用上下文参数<br>
			Windows Media Format SDK的一些回调函数具有pvContext参数，这个值是你在异步操作启动时传递给对象的。<br>
			通常，多个对象使用同一个回调时传递对象指针作为这个参数。</p>
			<p>使用设置<br>
			设置的主要目的是描述其中的对象，以及对象之间的关系。不管是否使用编码/解码器，某些流需要配置才可以工作。流的配置信息可以用IWMCodecInfo3 
			接口的方法获得，但是不要手动配置一个使用了Windows Media编码/解码器的流。<br>
			创建/编辑设置的步骤<br>
			1 创建空设置，或者打开旧设置<br>
			2 配置每个流，如果需要的话，使用从编码/解码器获得的数据<br>
			3 配置互斥（可选）<br>
			4 配置带宽共享（可选）<br>
			5 配置优先级（可选）</p>
			<p>设计设置</p>
			<p>选择编码方式<br>
			1-pass Constant Bit Rate (CBR) 直播的唯一选择。以预定的码流率编码，并且质量最低。<br>
			2-pass CBR 文件形式的流媒体，长度固定，质量比1-pass Constant Bit Rate (CBR)好<br>
			1-pass Variable Bit Rate (VBR) 需要指定质量时使用，本地播放或者下载后播放<br>
			2-pass VBR – unconstrained 需要指定带宽时使用，但是真实带宽占用可以偏离指定带宽，本地播放或者下载后播放<br>
			2-pass VBR – constrained 需要指定带宽时使用，但是真实带宽占用不能大于指定带宽，本地播放或者下载后播放</p>
			<p>码流率<br>
			除了数据之外，分包也要占用一定的带宽。如果流包含数据单位扩展，那么这将大大增加流的码流率。<br>
			同时，除了应用程序之外的任何连接都和应用程序共享网络带宽，所以不能认为应用程序可以完全使用客户的网络带宽。</p>
			<p>配置流<br>
			如果流是视频/音频，使用Windows 
			Media编码/解码器，那么你必须使用IWMCodecInfo3的方法从编码/解码器获得流配置对象。<br>
			如果流是其他类型，使用IWMProfile::CreateNewStream.创建一个新的流配置对象。<br>
			每个流配置都必须设置名字、连接名和流序号（从1到63）。<br>
			可能会修改使用Windows Media编码/解码器的two-pass VBR 音频流的VBR设置。视频流无需修改配置。<br>
			根据类型配置其他类型流。所有的这种流需要设置比特率和缓冲窗口。<br>
			使用IWMProfile::AddStream. 将流添加到媒体。<br>
			大部分设置可以通过IWMMediaProps访问。这些设置保存在WM_MEDIA_TYPE 
			结构中。对于音频和视频，WM_MEDIA_TYPE结构指针指向媒体特定的更多信息，通常是WAVEFORMATEX 或者WMVIDEOINFOHEADER结构。视频有第三个结构BITMAPINFOHEADER描述了视频的桢。</p>
			<p>从编码/解码器获得流配置信息<br>
			使用Windows 
			Media编码/解码器的视频/音频流需要从编码/解码器获得流配置信息。尽管你可以自行设置这些配置，从编码/解码器获得流配置信息使得数据是准确的。除非文档推荐，否则不要修改获得的流配置信息。<br>
			可以从设置管理器的IWMCodecInfo, IWMCodecInfo2, 和IWMCodecInfo3接口获得信息。</p>
			<p>枚举安装的编码/解码器<br>
			编码/解码器的编号从0开始，音频和视频的编码/解码器有独立的编号。</p>
			<p>枚举编码/解码器支持的格式</p>
			<p>配置音频流<br>
			不要手动修改获得的配置的质量设置，而应该用IWMPropertyVault接口修改。<br>
			音频流的缓冲窗口不应该设置得比视频流的缓冲窗口大，否则会造成播放不同步。通常，音频流的缓冲窗口是1.5-3秒,视频流的缓冲窗口是3-5秒。</p>
			<p>配置视频流<br>
			除非是RGB24数据，否则大小应该是4的倍数，否则会有非法格式/非法配置等错误。</p>
			<p>配置屏幕流<br>
			和视频流一样，但是如果复杂度设置为0，那么IWMVideoMediaProps::SetQuality设置的质量会被忽略。</p>
			<p>图像流<br>
			包含JPEG形式的图像数据。</p>
			<p>视频流的定位性能<br>
			可以使用IWMVideoMediaProps::SetMaxKeyFrameSpacing设置关键桢间隔。增加关键桢数目会降低视频质量。</p>
			<p>未压缩的音视频格式<br>
			不能用于流，必须手动设置带宽，缓冲窗口应该设为0</p>
			<p>配置其他流<br>
			通常，这种流只需要比特率和缓冲窗口和WM_MEDIA_TYPE 中的媒体主类型设置。但是某些类型的流还需要其他设置</p>
			<p>脚本流<br>
			WM_MEDIA_TYPE的成员formattype 要设置为WMFORMAT_Script，指明pbFormat成员指向一个WMSCRIPTFORMAT 
			结构。<br>
			只有一种脚本媒体类型，WMSCRIPTTYPE_TwoStrings。</p>
			<p>文件传输流<br>
			每个采样需要一个数据单位扩展，你需要实现一个数据单位扩展系统。<br>
			调用IWMStreamConfig2::AddDataUnitExtension添加数据单位扩展到流。<br>
			hr = pStreamConfig2-&gt;AddDataUnitExtension(CLSID_WMTPropertyFileName,<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -1, NULL, 0);</p>
			<p>网页流<br>
			WM_MEDIA_TYPE.majortype WMMEDIATYPE_Filetransfer. <br>
			WM_MEDIA_TYPE.subtype WMMEDIASUBTYPE_WebStream. <br>
			WM_MEDIA_TYPE.bFixedSizeSamples False. <br>
			WM_MEDIA_TYPE.bTemporalCompression True. <br>
			WM_MEDIA_TYPE.lSampleSize 0. <br>
			WM_MEDIA_TYPE.formattype WMFORMAT_WebStream. <br>
			WM_MEDIA_TYPE.pUnk NULL. <br>
			WM_MEDIA_TYPE.cbFormat sizeof(WMT_WEBSTREAM_FORMAT). <br>
			WM_MEDIA_TYPE.pbFormat 一个配置好的WMT_WEBSTREAM_FORMAT结构的指针. <br>
			WMT_WEBSTREAM_FORMAT.cbSampleHeaderFixedData 
			sizeof(WMT_WEBSTREAM_SAMPLE_HEADER). <br>
			WMT_WEBSTREAM_FORMAT.wVersion 1. <br>
			WMT_WEBSTREAM_FORMAT.wreserved 0.</p>
			<p>文本流<br>
			媒体类型WMMEDIATYPE_TEXT</p>
			<p>计算比特率和缓冲窗口<br>
			简单的办法是设置为数据长度/时间.但是图像和文件流可能突发数据很多,但是有很多空闲时间.缓冲窗口必须设置得足够大.需要的时候,可以适当增加这些值.</p>
			<p>变码流率流</p>
			<p>数据单位扩展</p>
			<p>保存/重新使用配置<br>
			不要手动更改PRX文件。看起来很小的改变会使得配置无效。</p>
			<p>互斥</p>
			<p>流优先级</p>
			<p>带宽共享</p>
			<p>包大小</p>
			<p>写ASF文件<br>
			使用IWMWriter::SetProfile对写入对象进行设置。但是，设置了之后，对设置对象的修改不会自动反映到写入对象，除非再次调用IWMWriter::SetProfile。<br>
			设置写入对象会复位全部头属性，所以必须在设置之后再修改这些属性。</p>
			<p>输入</p>
			<p>设置对象中的每个连接有一个输入号。除非配置中有互斥流，否则每个流有一个连接。互斥流共享连接。<br>
			写入流时需要用输入号来区别每个流，所以必须用连接名字来判断每个流的输入号。</p>
			<p>枚举输入格式<br>
			SDK可以对输入进行预处理来判断输入的格式是否支持。</p>
			<p>设置输入格式<br>
			找到符合数据的输入格式之后，可以调用IWMWriter::SetInputProps让它可以被写入对象使用。对于视频流，必须设置桢的大小。</p>
			<p>其他类型的流和预压缩流<br>
			其他类型的流无需设置。<br>
			预压缩流需要设置输入格式为NULL。这个设置必须在BeginWriting之前完成。同时需要调用IWMHeaderInfo3::AddCodecInfo设置预压缩流的格式。</p>
			<p>BeginWriting之前,还可以用IWMWriterAdvanced2::SetInputSetting设置和流无关的设置。</p>
			<p>元数据<br>
			使用写入对象的IWMHeaderInfo 或者IWMHeaderInfo2接口访问元数据。必须在IWMWriter::BeginWriting之前完成元数据的写入。<br>
			注意，如果创建了写入对象而没有释放，然后再创建写入对象，一些元数据会被复制到新的对象中。</p>
			<p>写入采样<br>
			写入采样之前要调用IWMWriter::BeginWriting.<br>
			1 用IWMWriter::AllocateSample分配缓冲区，并且获得其INSSBuffer接口<br>
			2 用INSSBuffer::GetBuffer获得缓冲区地址<br>
			3 复制数据到缓冲区中<br>
			4 用INSSBuffer::SetLength设置复制的数据长度<br>
			5 把缓冲区、输入编号和媒体时间传递给<strong style="color: black; background-color: rgb(255, 255, 102);">IWMWriter::WriteSample</strong>方法。音频数据持续时间是一样的，所以可以简单地在现有时间上加上一个常数。对于视频，需要根据桢率计算媒体时间。<br>
			WriteSample是异步调用，在下一次WriteSample调用之前可能没有结束。所以要在每次写入采样之前调用AllocateSample获取缓冲区对象。<br>
			所有采样写完之后，调用IWMWriter::EndWriting完成写入操作。<br>
			流数据应该几乎同时结束，否则某些流数据可能丢失。</p>
			<p>写入压缩采样<br>
			使用IWMWriterAdvanced::WriteStreamSample 替代<strong style="color: black; background-color: rgb(255, 255, 102);">IWMWriter::WriteSample</strong>。</p>
			<p>写入图像采样<br>
			必须用IWMWriterAdvanced2::SetInputSetting设置图像质量g_wszJPEGCompressionQuality，范围从1到100。图像采样压缩比通常很大，所以要使用尝试的方法设置缓冲窗口大小。</p>
			<p>强制关键桢<br>
			使用INSSBuffer3::SetProperty设置缓冲区对象的WM_SampleExtensionGUID_OutputCleanPoint为TRUE。</p>
			<p>读取</p>
			<p>输出</p>
			<p>默认方式下，每个采样有一个输出编号，对应于ASF文件中的一个流。读取者打开ASF文件时，为每个流赋予一个编号。通常对每个流都有一个输出。但是对于互斥的流，每一组互斥流只有一个输出。多码流率文件的情况，或者程序自行选择流的情况下，输出对应的流是由读取者决定的。<br>
			因为流的连接名字并未保留在文件中，读取者为每个流创建一个简单的连接名字，就是输出号的字符形式,例如&quot;1&quot;,&quot;2&quot;,&quot;3&quot;等等。<br>
			每个输出有由编码器决定的一个或者多个支持的输出格式，打开时默认是从媒体的子类型获得默认输出格式。</p>
			<p>使用异步方式读取ASF文件<br>
			1 实现IWMReaderCallback，处理读取者的消息，OnStatus处理状态消息，OnSample处理解压过的数据<br>
			2 让读取者打开一个文件，为每个流设置一个输出号<br>
			3 从读取者获得输出格式信息<br>
			4 让读取者开始播放，采样在指定的媒体时间传递给OnSample，直到读取者被停止或者达到文件末尾<br>
			5 数据到达时，程序负责播放采样<br>
			6 播放结束之后，让读取者关闭。<br>
			如果采样是预压缩的，那么需要实现的是IWMReaderCallbackAdvanced::OnStreamSample 
			。IWMReaderCallbackAdvanced::OnStreamSample几乎和OnSample完全一样，除了它基于流编号而不是输出编号之外。在开始回放之前，获得读取者对象的IWMReaderAdvanced接口，为每个预压缩流流调用 
			IWMReaderAdvanced::SetReceiveStreamSamples.</p>
			<p>定位<br>
			一个ASF文件必须被适当的配置才可以定位到指定时间。默认情况下只有音频的文件可以定位，但是包含视频的文件需要有索引才可以。如果你不确定文件的创建方式，你可以调用用IWMHeaderInfo::GetAttributeByName，传递g_wszWMSeekable来获得是否可定位信息。<br>
			调用IWMReader::Start可以定位到指定时间。</p>
			<p>[开发经验]</p>
			<p>选择编码器<br>
			Windows Media<br>
			尽管在低码流率下的效果令人满意，但是编码时系统资源占用过高，同时在高码流率的情况下效果不甚理想。</p>
			<p>Windows Media Video 9<br>
			Windows Media Video 9 Screen<br>
			对于格式比较挑剔，例如视频的规格必须是按双字对齐的。对于长时间的多媒体编码，有阶段性的质量变化（一段时间内桢率高，过一段时间桢率低）<br>
			Windows Media Audio 9<br>
			Windows Media Audio 9 Professional<br>
			系统资源占用过高致使采样不足的话会造成音调的变化，效果不可忍受。</p>
			<p>自定义编码器<br>
			多种数据混合编码，避免了同步问题，但是不能单独为一种数据指定码流率和优先级等信息</div>
		<div class="article_more">
			<div class="article_more_left">
				<table id="table1">
					<tr>
						<td class="article_more_left_title">系统分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/10053/category.aspx">
						软件开发 </a></td>
					</tr>
					<tr>
						<td class="article_more_left_title">用户分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/FIB/20481/category.aspx">
						技术文收藏</a>
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">标签:</td>
						<td class="article_more_left_value">无标签
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">来源:</td>
						<td class="article_more_left_value">转贴
                                    </td>
					</tr>
				</table>
			</div>
			<div class="article_more_right">
				<a href="http://blog.ednchina.com/FIB/97789/message.aspx#feedback">
				发表评论</a> 阅读全文(513) | 回复(0)
                        </div>
		</div>
	</div>
</div>
<div class="article">
	<div class="article_digg">
		<div id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl01_Digg1" class="digg">
			<h4 id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl01_Digg1_display" style="opacity: 1;">
			1 </h4>
			<span class="unclicked" onmouseout="Digg_Mouseout(this)" onmouseover="Digg_Mouseover(this)" onclick="if(this.className != 'clicked'){WebForm_DoCallback('ctl00$ctl00$SkinBody$Content$ContentControl$ctl01$Digg1',null,DiggClientCallBack,null,null,true)}">
			</span></div>
		<img src="http://blog.ednchina.com/images/q.gif" id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl01_aboutvote" alt="关于投票">
                    
                </div>
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/97611/message.aspx">Wma Tag 
			读写类 </a></div>
		<div class="article_info">
			发表于 2008/3/25 14:40:00
                    </div>
		<div class="article_content">
			VIA :&nbsp;
			<p><font style="background-color: rgb(255, 255, 255);">
			<a href="http://linle.ycool.com/post.1118133.html">
			http://linle.ycool.com/post.1118133.html</a></font></p>
			<p>&nbsp;</p>
			<p>文件结构示意图<br>
			<br>
			<a target="_blank" href="http://foto.yculblog.com/linle/wma.jpg">
			<img alt="点击看大图" src="http://foto.yculblog.com/linle/wma.jpg" width="520" height="157"></a><br>
			<br>
			<font size="5">格式的简单说明：<br>
　</p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="3">
			<span style><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size="2"> 
			如图1，每一个WMA文件，它的头16个字节是固定的，为十六进制的“30 26 B2 75 8E 66 CF 11 A6 D9 00 AA 
			00 62 CE 
			6C”，用来标识这个是否为WMA文件。接下来的8个字节为一个整数，表示整个WMA文件头部的大小，这个头部里面包含了Tag信息等所有非音频信息，头部后面的是音频信息，我们在这里就不深入了解了。那个整数接下来的6个字节还没搞清楚是什么用的，不过不影响我们对Tag信息的读写。<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
			也就是说从文件开始偏移量为31开始，里面存放了很多帧，有我们需要的标准Tag信息，扩展Tag信息，WMA文件控制信息等等。每个帧不是等长的，但是帧头是固定的24个字节，其中前16字节是用来标识这个帧的名字，后8个字节是用来表示这个帧（包括帧头）的大小。这一点和MP3文件的ID3V2信息比较像。<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
			由于我们只需要读写Tag信息，而Tag信息又分别保存在两个帧里，分别为标准Tag帧和扩展Tag帧，所有我们只需要处理这两个帧，其他帧完全可以根据获得的帧长度来跳过。<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如图2，标准Tag帧只包含歌曲标题，艺术家，版权，备注四个内容。它的帧名是十六进制的“33 26 B2 75 8E 66 
			CF 11 A6 D9 00 AA 00 62 CE 
			6C”，在24个字节的帧头后紧跟着5个分别为2个字节的整数，前四个分别表示歌曲标题，艺术家，版权，备注的大小，第五个还不清楚是什么用的，大部分情况下是不使用的，即它的大小为0的。<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
			在这10个字节后，这四个信息的内容就按顺序存放了。记住，在WMA文件里，所有的文字都是按Unicode宽字符的编码方式储存的，而且每个字符串后面都又一个0结束字符的。<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如图3，再看扩展Tag帧，这里就比较麻烦了，里面包含的信息的个数是不确定的，每个信息也是按照像帧一样的方式组织起来的。扩展Tag帧的帧名是十六进制的“40 
			A4 D0 D2 07 E3 D2 11 97 F0 00 A0 C9 5E A8 
			50”，在24字节的帧头后先有一个两个字节的整数表示这个帧里一共有的扩展信息个数（ExNo）。<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
			如图4，每一个扩展信息包含扩展信息名字和对应的值。先有一个两个字节的整数来表示扩展名字信息的大小，接着是扩展信息，然后有一个两个字节的整数标志（Flag），这个后面再讲。然后又是一个两个字节的整数，表示值的大小。接着就是这个值。<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当扩展信息名字为WMFSDKVersion时，这个值表示的是这个WMA文件的版本；当扩展信息名字为WM/AlbumTitle时，这个值代表的就是专辑名；当扩展信息名字为WM/Genre时，这个值代表的就是流派；同理，很容易从扩展信息的名字看出这个值的用途的。这些扩展信息的名字和值几乎都是用Unicode的字符串来存储的，到现在为止只发现对下面两个情况例外。（关于所有扩展信息的名字可以从很多地方查到，比如SDK帮助,MSDN）<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 下面再来看看那个标志Flag，这个基本上是为没什么用的（通常值为0），只对WM/TrackNumber和WM/Track这两个扩展信息名字有用，当Flag为3的时候后面的值（也就是曲目信息）是以4个字节的整数的形式表示，当Flag为0的时候，曲目信息是以普通的字符串形式表示的。</font></font></span></font><br>
　</p>
			</font>　</p></div>
		<div class="article_more">
			<div class="article_more_left">
				<table id="table2">
					<tr>
						<td class="article_more_left_title">系统分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/10053/category.aspx">
						软件开发 </a></td>
					</tr>
					<tr>
						<td class="article_more_left_title">用户分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/FIB/20481/category.aspx">
						技术文收藏</a>
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">标签:</td>
						<td class="article_more_left_value">
						<a title="Wma" href="http://blog.ednchina.com/lable/Wma.aspx">
						Wma</a>
						<a title="Tag" href="http://blog.ednchina.com/lable/Tag.aspx">
						Tag</a>
						<a title="读写类" href="http://blog.ednchina.com/lable/读写类.aspx">
						读写类</a> 
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">来源:</td>
						<td class="article_more_left_value">转贴
                                    </td>
					</tr>
				</table>
			</div>
			<div class="article_more_right">
				<a href="http://blog.ednchina.com/FIB/97611/message.aspx#feedback">
				发表评论</a> 阅读全文(1104) | 回复(0)
                        </div>
		</div>
	</div>
</div>
<div class="article">
	<div class="article_digg">
		<div id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl02_Digg1" class="digg">
			<h4 id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl02_Digg1_display" style="opacity: 1;">
			1 </h4>
			<span class="unclicked" onmouseout="Digg_Mouseout(this)" onmouseover="Digg_Mouseover(this)" onclick="if(this.className != 'clicked'){WebForm_DoCallback('ctl00$ctl00$SkinBody$Content$ContentControl$ctl02$Digg1',null,DiggClientCallBack,null,null,true)}">
			</span></div>
		<img src="http://blog.ednchina.com/images/q.gif" id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl02_aboutvote" alt="关于投票">
                    
                </div>
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/95018/message.aspx">利用VC++实现AVI文件的合成和分解 
			</a></div>
		<div class="article_info">
			发表于 2008/3/19 15:32:03
                    </div>
		<div class="article_content">
			出处： 天极开发<p><strong>摘要</strong>：本文详细的解析了AVI文件的存储结构，介绍了<a class="bluekey" target="_blank" href="http://com.chinabyte.com/microsoft/"><font color="#003399">微软</font></a>提供的用来操作AVI文件的一组API使用方法，并通过例子代码，演示了如何将一组静态Bmp图片合成一个avi视频文件以及如何将一个avi视频文件解析保存为一系列的bmp图像文件。 
			<br>
			<br>
			　　<b>关键词</b>：avi文件 bmp图像 vc<br>
			<br>
			　　AVI是音频视频交错(Audio Video 
			Interleaved)的英文缩写，它是Microsoft公司开发的一种符合RIFF文件规范的数字音频与视频文件格式，原先用于Microsoft 
			Video for Windows (简称VFW)环境，现在已被Windows 95/98、OS/2等多数<a class="bluekey" target="_blank" href="http://os.yesky.com/"><font color="#003399">操作系统</font></a>直接支持。AVI格式允许视频和音频交错在一起同步播放，支持256色和RLE压缩，但AVI文件并未限定压缩标准，因此，AVI文件格式只是作为控制界面上的标准，不具有兼容性，用不同压缩算法生成的AVI文件，必须使用相应的解压缩算法才能播放出来。常用的AVI播放驱动<a class="bluekey" target="_blank" href="http://dev.yesky.com/"><font color="#003399">程序</font></a>，主要是Microsoft 
			Video for Windows或Windows 95/98中的Video 1，以及Intel公司的Indeo Video。<br>
			<br>
			　　在介绍AVI文件前，我们要先来看看RIFF文件结构。AVI文件采用的是RIFF文件结构方式，RIFF（Resource 
			Interchange File 
			Format，资源互换文件格式）是微软公司定义的一种用于管理windows环境中多媒体数据的文件格式，波形音频wave，MIDI和数字视频AVI 
			都采用这种格式存储。构造RIFF文件的基本单元叫做数据块（Chunk），每个数据块包含3个部分，<br>
			<br>
			　　1、4字节的数据块标记（或者叫做数据块的ID）<br>
			<br>
			　　2、数据块的大小<br>
			<br>
			　　3、数据<br>
			<br>
			　　整个RIFF文件可以看成一个数据块，其数据块ID为RIFF，称为RIFF块。一个RIFF文件中只允许存在一个RIFF块。RIFF块中包含一系列的子块，其中有一种字块的ID为&quot;LIST&quot;，称为LIST，LIST块中可以再包含一系列的子块，但除了LIST块外的其他所有的子块都不能再包含子块。<br>
			<br>
			　　RIFF和LIST块分别比普通的数据块多一个被称为形式类型（Form Type）和列表类型（List 
			Type）的数据域，其组成如下： <br>
			<br>
			　　1、4字节的数据块标记（Chunk ID）<br>
			<br>
			　　2、数据块的大小<br>
			<br>
			　　3、4字节的形式类型或者列表类型<br>
			<br>
			　　4、数据<br>
			<br>
			　　下面我们看看AVI文件的结构。AVI文件是目前使用的最复杂的RIFF文件，它能同时存储同步表现的音频视频数据。AVI的RIFF块的形式类型是AVI，它包含3个子块，如下所述：<br>
			<br>
			　　1、信息块，一个ID为&quot;hdrl&quot;的LIST块，定义AVI文件的数据格式。<br>
			<br>
			　　2、数据块，一个ID为 &quot;movi&quot;的LIST块，包含AVI的音视频序列数据。<br>
			<br>
			　　3、索引块，ID为 &quot;idxl&quot;的子块，定义 &quot;movi&quot;LIST块的索引数据，是可选块。<br>
			<br>
			　　AVI文件的结构如下图所示，下面将具体介绍AVI文件的各子块构造。<br>
			<br>
			　　1、信息块，信息块包含两个子块，即一个ID为 avih 的子块和一个ID 为 strl 的LIST块。<br>
			<br>
　</p>
			<p>　</p>
			<table width="90%" align="center" border="0" id="table3">
				<tr>
					<td>
					<div align="center">
						<img src="http://dev.yesky.com/imagelist/06/02/48368q6r4tua.jpg" border="0"></div>
					</td>
				</tr>
			</table>
			<p><br>
			　　&quot;avih&quot;子块的内容可由如下的结构定义：<br>
			<br>
　</p>
			<p>　</p>
			<table width="90%" align="center" bgcolor="#e3e3e3" border="1" bordercolor="#cccccc" id="table4">
				<tr>
					<td>typedef struct <br>
					{<br>
					　DWORD dwMicroSecPerFrame ; //显示每桢所需的时间ns，定义avi的显示速率<br>
					　DWORD dwMaxBytesPerSec; // 最大的数据传输率<br>
					　DWORD dwPaddingGranularity; //记录块的长度需为此值的倍数，通常是2048<br>
					　DWORD dwFlages; //AVI文件的特殊属性，如是否包含索引块，音视频数据是否交叉存储<br>
					　DWORD dwTotalFrame; //文件中的总桢数<br>
					　DWORD dwInitialFrames; //说明在开始播放前需要多少桢<br>
					　DWORD dwStreams; //文件中包含的数据流种类<br>
					　DWORD dwSuggestedBufferSize; //建议使用的缓冲区的大小，<br>
					　//通常为存储一桢图像以及同步声音所需要的数据之和<br>
					　DWORD dwWidth; //图像宽<br>
					　DWORD dwHeight; //图像高<br>
					　DWORD dwReserved[4]; //保留值<br>
					}MainAVIHeader;</td>
				</tr>
			</table>
			<p><br>
			　　&quot;strl&quot; 
			LIST块用于记录AVI数据流，每一种数据流都在该LIST块中占有3个子块，他们的ID分别是&quot;strh&quot;,&quot;strf&quot;, &quot;strd&quot;；<br>
			&quot;strh&quot;子块由如下结构定义。<br>
			<br>
　</p>
			<p>　</p>
			<table width="90%" align="center" bgcolor="#e3e3e3" border="1" bordercolor="#cccccc" id="table5">
				<tr>
					<td>typedef struct <br>
					{<br>
					　FOURCC fccType; //4字节，表示数据流的种类 vids 表示视频数据流<br>
					　//auds 音频数据流<br>
					　FOURCC fccHandler;//4字节 ，表示数据流解压缩的驱动程序代号<br>
					　DWORD dwFlags; //数据流属性<br>
					　WORD wPriority; //此数据流的播放优先级<br>
					　WORD wLanguage; //音频的语言代号<br>
					　DWORD dwInitalFrames;//说明在开始播放前需要多少桢<br>
					　DWORD dwScale; //数据量，视频每桢的大小或者音频的采样大小<br>
					　DWORD dwRate; //dwScale /dwRate = 每秒的采样数<br>
					　DWORD dwStart; //数据流开始播放的位置，以dwScale为单位<br>
					　DWORD dwLength; //数据流的数据量，以dwScale为单位<br>
					　DWORD dwSuggestedBufferSize; //建议缓冲区的大小<br>
					　DWORD dwQuality; //解压缩质量参数，值越大，质量越好<br>
					　DWORD dwSampleSize; //音频的采样大小<br>
					　RECT rcFrame; //视频图像所占的矩形<br>
					}AVIStreamHeader;</td>
				</tr>
			</table>
			<p><br>
			　　&quot;strf&quot;子块紧跟在&quot;strh&quot;子块之后，其结构视&quot;strh&quot;子块的类型而定，如下所述；如果 strh子块是视频数据流，则 
			strf子块的内容是一个与windows设备无关位图的BIMAPINFO结构，如下：<br>
			<br>
　</p>
			<p>　</p>
			<table width="90%" align="center" bgcolor="#e3e3e3" border="1" bordercolor="#cccccc" id="table6">
				<tr>
					<td>typedef struct tagBITMAPINFO<br>
					{<br>
					　BITMAPINFOHEADER bmiHeader;<br>
					　RGBQUAD bmiColors[1]; //颜色表<br>
					}BITMAPINFO;<br>
					<br>
					typedef struct tagBITMAPINFOHEADER<br>
					{<br>
					　DWORD biSize;<br>
					　LONG biWidth;<br>
					　LONG biHeight;<br>
					　WORD biPlanes;<br>
					　WORD biBitCount;<br>
					　DWORD biCompression;<br>
					　DWORD biSizeImage;<br>
					　LONG biXPelsPerMeter;<br>
					　LONG biYPelsPerMeter;<br>
					　DWORD biClrUsed;<br>
					　DWORD biClrImportant;<br>
					}BITMAPINFOHEADER;</td>
				</tr>
			</table>
			<p><br>
			　　如果 strh子块是音频数据流，则strf子块的内容是一个WAVEFORMAT结构，如下：<br>
			<br>
　</p>
			<p>　</p>
			<table width="90%" align="center" bgcolor="#e3e3e3" border="1" bordercolor="#cccccc" id="table7">
				<tr>
					<td>typedef struct <br>
					{<br>
					　WORD wFormatTag; <br>
					　WORD nChannels; //声道数<br>
					　DWORD nSamplesPerSec; //采样率<br>
					　DWORD nAvgBytesPerSec; //WAVE声音中每秒的数据量<br>
					　WORD nBlockAlign; //数据块的对齐标志<br>
					　WORD biSize; //此结构的大小<br>
					}WAVEFORMAT</td>
				</tr>
			</table>
			<p><br>
			　　&quot;strd&quot;子块紧跟在strf子块后，存储供压缩驱动程序使用的参数，不一定存在，也没有固定的结构。<br>
			<br>
			　　&quot;strl&quot; LIST块定义的AVI数据流依次将 &quot;hdrl &quot; LIST 块中的数据流头结构与&quot;movi&quot; 
			LIST块中的数据联系在一起，第一个数据流头结构用于数据流0，第二个用于数据流1，依次类推。<br>
			<br>
			　　数据块中存储视频和音频数据流，数据可直接存于 &quot;movi&quot; LIST块中。数据块中音视频数据按不同的字块存放，其结构如下所述，<br>
			<br>
			　　音频字块<br>
			　　　　&quot;##wb&quot;<br>
			　　　　Wave 数据流<br>
			　　视频子块中存储DIB数据，又分为压缩或者未压缩DIB，<br>
			　　　　&quot;##db&quot;<br>
			　　　　RGB数据流<br>
			　　　 &quot;##dc&quot;<br>
			　　压缩的图像数据流<br>
			<br>
			　　看到了吧，avi文件的图像数据可以是压缩的，和非压缩格式的。对于压缩格式来说，也可采用不同的编码，也许你曾经遇到有些avi没法识别，就是因为编码方式不一样，如果没有相应的解码，你就没法识别视频数据。AVI的编码方式有很多种，比较常见的有 
			mpeg2，mpeg4，divx等。<br>
　</p>
			<p>
			索引块，索引快包含数据块在文件中的位置索引，能提高avi文件的读写速度，其中存放着一组AVIINDEXENTRY结构数据。如下，这个块并不是必需的，也许不存在。<br>
			<br>
　</p>
			<table width="90%" align="center" bgcolor="#e3e3e3" border="1" bordercolor="#cccccc" id="table8">
				<tr>
					<td>typedef struct <br>
					{<br>
					　DWORD ckid; //记录数据块中子块的标记<br>
					　DWORD dwFlags; //表示chid所指子块的属性<br>
					　DWORD dwChunkOffset; //子块的相对位置<br>
					　DWORD dwChunkLength; //子块长度<br>
					};</td>
				</tr>
			</table>
			<br>
			　　现在我相信你肯定会对AVI的文件结构已经很清楚了，在介绍完了AVI文件结构后，我们就来看看如何对avi文件进行读写了，为了对avi进行读写，微软提供了一套API，总共50个函数，他们的用途主要有两类，一个是avi文件的操作，一类是数据流streams的操作。<br>
			<br>
			　　1、打开和关闭文件<br>
			<br>
			　　AVIFileOpen ，AVIFileAddRef， AVIFileRelease<br>
			<br>
			　　2、从文件中读取文件信息<br>
			<br>
			　　通过AVIFileInfo可以获取avi文件的一些信息，这个函数返回一个AVIFILEINFO结构，通过AVIFileReadData可以用来获取AVIFileInfo函数得不到的信息。这些信息也许不包含在文件的头部，比如拥有file的公司和个人的名称。<br>
			<br>
			　　3、写入文件信息<br>
			<br>
			　　可以通过AVIFileWriteData函数来写入文件的一些额外信息。<br>
			<br>
			　　4、打开和关闭一个流<br>
			<br>
			　　打开一个数据流就跟打开文件一样，你可以通过 AVIFileGetStream函数来打开一个数据流，这个函数创建了一个流的接口，然后在该接口中保存了一个句柄。<br>
			<br>
			　　如果你想操作文件的某一个单独的流，你可以采用AVIStreamOpenFromFile函数，这个函数综合了AVIFileOpen和AVIFileGetStream函数。<br>
			<br>
			　　如果你想操作文件中的多个数据流，你就要首先AVIFileOpen，然后AVIFileGetStream。<br>
			<br>
			　　可以通过AVIStreamAddRef来增加stream接口的引用。<br>
			<br>
			　　通过AVIStreamRelease函数来关闭数据流。这个函数用来减少streams的引用计数，当计数减少为0时，删除。<br>
			<br>
			　　5、从流中读取数据和信息<br>
			<br>
			　　AVIStreamInfo函数可以获取数据的一些信息，该函数返回一个AVISTREAMINFO结构，该结构包含了数据的类型压缩方法，建议的buffersize，回放的rate，以及一些description。<br>
			<br>
			　　如果数据流还有一些其它的额外的信息，你可以通过AVIStreamReadData函数来获取。应用程序分配一个<a class="bluekey" target="_blank" href="http://diy.yesky.com/memoery/"><font color="#003399">内存</font></a>，传递给这个函数，然后这个函数会通过这个内存返回数据流的信息，额外的信息可能包括数据流的压缩和解压缩的方法，你可以通过AVIStreamDataSize宏来回去需要申请内存块的大小。<br>
			<br>
			　　可以通过AVIStreamReadFormat函数获取数据流的格式信息。这个函数通过指定的内存返回数据流的格式信息，比如对于视频流，这个 
			buffer包含了一个BIMAPINFO结构，对于音频流，内存块包含了WAVEFORMATEX或者PCMAVEFORMAT结构。你可以通过给 
			AVIStreamReadFormat传递一个空buffer就可以获取buffer的大小。也可以通过AVIStreamFormatSize宏。<br>
			<br>
			　　可以通过AVIStreamRead函数来返回多媒体的数据。这个函数将数据复制到应用程序提供的内存中，对于视频流，这个函数返回图像祯，对于音频流，这个函数返回音频的sample数据。可以通过给AVIStreamRead传递一个NULL的buffer来获取需要的buffer的大小。也可以通过AVIStreamSampleSize宏来获取buffer的大小。<br>
			<br>
			　　有些AVI数据流句柄可能需要在启动数据流的前要做一下准备工作，此时，我们可以调用AVIStreamBeginStreaming函数来告知AVI数据流handle来申请分配它需要的一些资源。在完毕后，调用AVIStreamEndStreamming函数来释放资源。<br>
			<br>
			　　6、操作压缩的视频数据<br>
			<br>
			　　如果你要演示一祯或者几祯压缩视频图像时，你可以调用AVIStreamRead函数，将获取的数据传递给DrawDib函数来显示图像。这些函数可以显示压缩和未压缩的图像。<br>
			<br>
			　　AVIFile也提供了一个函数AVIStreamGetFrameOpen，来获取未压缩的视频祯，这个函数创建了内存来获取未压缩的数据。也可以通过AVIStreamGetFrame函数来解压缩一个单独的视频祯。这个函数可以解压缩某一祯图像，然后将数据以一个BIMAPINFOHEADER结构返回。当你调用完AVIStreamGetFrame函数后，要调用AVIStreamGetFrameClose函数释放上一个函数申请的资源。<br>
			<br>
			　　7、根据已存在的数据流创建文件<br>
			<br>
			　　创建一个包含多个数据流的文件的方法就是整合多个数据流，将其写入一个新文件。这些数据流可以是内存中的数据，也可以是存在于另一个文件中。<br>
			<br>
			　　我们可以用AVISave这个函数来build一个文件。这个函数可以创建一个文件，并且将指定的多个数据流按照指定的顺序写入文件，你也可以通过 
			AVISaveV函数来创建一个新的文件，这个函数的功能和AVISave的功能一样，主要区别是AVISaveV采用的数据流数组，而AVISave是单个的数据流，多次保存。<br>
			<br>
			　　我们可以调用AVISaveOptions函数来显示一个对话框，可以让用户来选择压缩方式。<br>
			<br>
			　　我们可以在调用AVISave和AVISaveV函数时指定一个回调函数，用来显示avi文件的生成进度，可以让用户随时地取消生成avi文件。<br>
			<br>
			　　我们可以调用GetSaveFileNamePreview函数来显示保存的对话框让用户选择保存的文件名。<br>
			<br>
			　　通过AVIMakeFileFromStreams函数我们可以创建一个虚拟的文件句柄，其他的avi函数可以通过这个虚拟的文件句柄来操作文件中的数据流，操作完毕要记得调用AVIFileRelease释放。
			<p>&nbsp;</p>
			<p>8、向文件写入一个数据流<br>
			<br>
			　　我们可以通过AVIFileCreateStream函数来在一个新文件或者已经存在的文件中创建一个数据流。这个函数根据AVISTREAMINFO结构定义了新的数据流，并为新的数据流创建一个接口，返回接口的指针。<br>
			<br>
			　　在写入新的数据前，一定要指定流的格式信息，通过AVIStreamSetFormat函数，当设置一个视频流的时候，一定要使用BIMAPINFO结构来设置，音频就用WAVEFORMAT。<br>
			<br>
			　　然后我们就可以通过AVIStreamWrite函数将我们的多媒体数据写入数据流了。这个函数将应用程序提供的内存数据复制到指定的流。缺省的avi 
			handler将数据写入流的最后。<br>
			<br>
			　　如果你有其他额外的信息需要写入流，你可以调用AVIFileWriteData或者AVIStreamWriteData，最后记得在完成数据写入后，要调用AVIStreamRelease。<br>
			<br>
			　　9、数据流中的祯的位置<br>
			<br>
			　　寻找起始祯：<br>
			<br>
			　　可以通过AVIStreamStart函数来获取第一祯包含的sample number。也可以通过AVIStreamInfo函数来获取这个信息，这个函数的AVISTREAMINFO结构中包含了dwStart，可以通过 
			AVIStreamStartTime宏来获取第一个sample。<br>
			<br>
			　　可以通过AVIStreamLength函数来获取流的长度。这个函数返回流中的sample的数目。也可以通过AVIStreamInfo函数来获取这些信息，可以通过AVIStreamLengthTime宏来获取流的长度，毫秒。<br>
			<br>
			　　在视频流中，一个sample对应着一祯图像，所以，有时这些sample中没有视频数据，如果你调用AVIStreamRead函数来数据，可能返回 
			NULL，也可以通过AVIStreamFindSample通过指定FIND_ANY标志来查找指定的sample。<br>
			<br>
			　　查找关键祯<br>
			<br>
			　　通过AVIStreamFindSample函数查找符合要寻找的sample，然后可以通过下面的宏判断是否关键祯。<br>
			<br>
			　　在time和sample间互相切换。<br>
			<br>
			　　AVIStreamSampleToTime这个函数可以将smaple转换成毫秒。对于视频，这个值代表的是这个祯开始播放的时间。<br>
			<br>
			　　在了解了上面的知识后，我们对avi的文件结构以及如何操作avi文件心里就明白了，下面我们可以开始我们的编程了。我们要做两件事情：<br>
			<br>
			　　1、如何将一组静态的bmp位图合成一个avi的视频文件；<br>
			<br>
			　　2、如何将一个未压缩的avi文件解析成一幅幅位图。<br>
			<br>
			　　示例程序界面如下：<br>
			<br>
　</p>
			<table width="90%" align="center" border="0" id="table9">
				<tr>
					<td>
					<div align="center">
						<img src="http://dev.yesky.com/imagelist/06/02/5rqv5z1pgq8p.jpg" border="0"></div>
					</td>
				</tr>
			</table>
			<br>
			　　下面的函数演示了如何将一个文件夹下面的所有bmp文件都保存为一个avi文件，函数的第一个参数是要生成的AVI的文件名，第二个参数是存放bmp文件的文件夹名，这个函数会枚举该文件夹下的所有bmp文件，合成一个AVI文件。<br>
			<br>
　<table width="90%" align="center" bgcolor="#e3e3e3" border="1" bordercolor="#cccccc" id="table10">
				<tr>
					<td>void Cbmp2aviDlg::AVItoBmp(CString strAVIFileName, 
					CString strBmpDir)<br>
					{<br>
					　// TODO: 在此添加控件通知处理程序代码<br>
					　AVIFileInit();<br>
					　PAVIFILE avi;<br>
					　int res=&quot;AVIFileOpen&quot;(&amp;avi, strAVIFileName, OF_READ, NULL);<br>
					　int n = GetLastError();<br>
					　if (res!=AVIERR_OK)<br>
					　{<br>
					　　//an error occures<br>
					　　if (avi!=NULL)<br>
					　　　AVIFileRelease(avi);<br>
					　　return ;<br>
					　}<br>
					　AVIFILEINFO avi_info;<br>
					　AVIFileInfo(avi, &amp;avi_info, sizeof(AVIFILEINFO));<br>
					　PAVISTREAM pStream;<br>
					　res=AVIFileGetStream(avi, &amp;pStream, streamtypeVIDEO /*video 
					stream*/, <br>
					　　　0 /*first stream*/);<br>
					　if (res!=AVIERR_OK)<br>
					　{<br>
					　　if (pStream!=NULL)<br>
					　　　AVIStreamRelease(pStream);<br>
					　　　AVIFileExit();<br>
					　　return ;<br>
					　}<br>
					<br>
					　//do some task with the stream<br>
					　int iNumFrames;<br>
					　int iFirstFrame;<br>
					　iFirstFrame=AVIStreamStart(pStream);<br>
					　if (iFirstFrame==-1)<br>
					　{<br>
					　　//Error getteing the frame inside the stream<br>
					　　if (pStream!=NULL)<br>
					　　　AVIStreamRelease(pStream);<br>
					　　AVIFileExit();<br>
					　　return ;<br>
					　}<br>
					　iNumFrames=AVIStreamLength(pStream);<br>
					　if (iNumFrames==-1)<br>
					　{<br>
					　　//Error getteing the number of frames inside the stream<br>
					　　if (pStream!=NULL)<br>
					　　　AVIStreamRelease(pStream);<br>
					　　AVIFileExit();<br>
					　　return ;<br>
					　}<br>
					<br>
					　//getting bitmap from frame<br>
					　BITMAPINFOHEADER bih;<br>
					　ZeroMemory(&amp;bih, sizeof(BITMAPINFOHEADER));<br>
					<br>
					　bih.biBitCount=24; //24 bit per pixel<br>
					　bih.biClrImportant=0;<br>
					　bih.biClrUsed = 0;<br>
					　bih.biCompression = BI_RGB;<br>
					　bih.biPlanes = 1;<br>
					　bih.biSize = 40;<br>
					　bih.biXPelsPerMeter = 0;<br>
					　bih.biYPelsPerMeter = 0;<br>
					　//calculate total size of RGBQUAD scanlines (DWORD aligned)<br>
					　bih.biSizeImage = (((bih.biWidth * 3) + 3) &amp; 0xFFFC) * 
					bih.biHeight ;<br>
					<br>
					　PGETFRAME pFrame;<br>
					　pFrame=AVIStreamGetFrameOpen(pStream, NULL );<br>
					<br>
					　AVISTREAMINFO streaminfo;<br>
					　AVIStreamInfo(pStream,&amp;streaminfo,sizeof(AVISTREAMINFO));<br>
					<br>
					　//Get the first frame<br>
					　BITMAPINFOHEADER bih2;<br>
					　long lsize = sizeof(bih2);<br>
					　int index=&quot;0&quot;;<br>
					　for (int i=&quot;iFirstFrame&quot;; i&lt;iNumFrames; i++)<br>
					　{<br>
					　　index= i-iFirstFrame;<br>
					　　BYTE* pDIB = (BYTE*) AVIStreamGetFrame(pFrame, index); //<br>
					　　AVIStreamReadFormat(pStream,index,&amp;bih2,&amp;lsize);<br>
					　　BITMAPFILEHEADER stFileHdr;<br>
					<br>
					　　BYTE* Bits=&quot;new&quot; BYTE[bih2.biSizeImage];<br>
					　　AVIStreamRead(pStream,index,1,Bits,bih2.biSizeImage,NULL,NULL);<br>
					　　//RtlMoveMemory(Bits, pDIB + sizeof(BITMAPINFOHEADER), 
					bih2.biSizeImage);<br>
					<br>
					　　bih2.biClrUsed =0;<br>
					　　stFileHdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);<br>
					　　stFileHdr.bfSize=sizeof(BITMAPFILEHEADER);<br>
					　　stFileHdr.bfType=0x4d42; <br>
					<br>
					　　CString FileName;<br>
					　　FileName.Format(&quot;Frame-%05d.bmp&quot;, index);<br>
					　　CString strtemp = strBmpDir;<br>
					　　strtemp += &quot;\\&quot;;<br>
					　　strtemp += FileName;<br>
					　　FILE* fp=_tfopen(strtemp ,_T(&quot;wb&quot;));<br>
					　　fwrite(&amp;stFileHdr,1,sizeof(BITMAPFILEHEADER),fp);<br>
					　　fwrite(&amp;bih2,1,sizeof(BITMAPINFOHEADER),fp);<br>
					　　int ff = fwrite(Bits,1,bih2.biSizeImage,fp);<br>
					　　int e = GetLastError();<br>
					　　fclose(fp);<br>
					　　/////<br>
					　　delete Bits;<br>
					　　//CreateFromPackedDIBPointer(pDIB, index);<br>
					　}<br>
					<br>
					　AVIStreamGetFrameClose(pFrame);<br>
					<br>
					　//close the stream after finishing the task<br>
					　if (pStream!=NULL)<br>
					　　AVIStreamRelease(pStream);<br>
					　AVIFileExit();<br>
					}</td>
				</tr>
			</table>
			<br>
			　　下面的这个函数演示了如何将AVI文件中的每一桢图像单独取出来，保存为bmp文件。函数的头一个参数是avi文件名，第二个参数是存放bmp文件的文件夹。<br>
			<br>
　<table width="90%" align="center" bgcolor="#e3e3e3" border="1" bordercolor="#cccccc" id="table11">
				<tr>
					<td>//生成avi<br>
					void Cbmp2aviDlg::BMPtoAVI(CString szAVIName, CString 
					strBmpDir)<br>
					{<br>
					　CFileFind finder;<br>
					　strBmpDir += _T(&quot;\\*.*&quot;); <br>
					　AVIFileInit(); <br>
					　AVISTREAMINFO strhdr;<br>
					　PAVIFILE pfile;<br>
					　PAVISTREAM ps; <br>
					　int nFrames =0; <br>
					　HRESULT hr; <br>
					<br>
					　BOOL bFind = finder.FindFile(strBmpDir);<br>
					　while(bFind)<br>
					　{<br>
					　　bFind = finder.FindNextFile();<br>
					　　if(!finder.IsDots() &amp;&amp; !finder.IsDirectory())<br>
					　　{<br>
					　　　CString str = finder.GetFilePath();<br>
					　　　FILE *fp = fopen(str,&quot;rb&quot;);<br>
					　　　BITMAPFILEHEADER bmpFileHdr;<br>
					　　　BITMAPINFOHEADER bmpInfoHdr;<br>
					　　　fseek( fp,0,SEEK_SET);<br>
					　　　fread(&amp;bmpFileHdr,sizeof(BITMAPFILEHEADER),1, fp);<br>
					　　　fread(&amp;bmpInfoHdr,sizeof(BITMAPINFOHEADER),1, fp);<br>
					<br>
					　　　BYTE *tmp_buf = NULL;<br>
					　　　if(nFrames ==0 )<br>
					　　　{<br>
					　　　　AVIFileOpen(&amp;pfile,szAviName,OF_WRITE | OF_CREATE,NULL);<br>
					　　　　_fmemset(&amp;strhdr, 0, sizeof(strhdr));<br>
					　　　　strhdr.fccType = streamtypeVIDEO;// stream type<br>
					　　　　strhdr.fccHandler = 0;<br>
					　　　　strhdr.dwScale = 1;<br>
					　　　　strhdr.dwRate = 15; // 15 fps<br>
					　　　　strhdr.dwSuggestedBufferSize = bmpInfoHdr.biSizeImage ;<br>
					　　　　SetRect(&amp;strhdr.rcFrame, 0, 0, bmpInfoHdr.biWidth, 
					bmpInfoHdr.biHeight);<br>
					<br>
					　　　　// And create the stream;<br>
					　　　　hr = AVIFileCreateStream(pfile,&amp;ps,&amp;strhdr); <br>
					　　　　// hr = AVIStreamSetFormat(ps,nFrames,&amp;bmpInfoHdr,sizeof(bmpInfoHdr));<br>
					　　　}<br>
					　　　tmp_buf = new BYTE[bmpInfoHdr.biWidth * 
					bmpInfoHdr.biHeight * 3];<br>
					　　　fread(tmp_buf, 1, bmpInfoHdr.biWidth * 
					bmpInfoHdr.biHeight * 3, fp);<br>
					　　　hr = AVIStreamSetFormat(ps,nFrames,&amp;bmpInfoHdr,sizeof(bmpInfoHdr));<br>
					　　　hr = AVIStreamWrite(ps, // stream pointer<br>
					　　　　　　nFrames , // time of this frame<br>
					　　　　　　1, // number to write<br>
					　　　　　　(LPBYTE) tmp_buf,<br>
					　　　　　　bmpInfoHdr.biSizeImage , // size of this frame<br>
					　　　　　　AVIIF_KEYFRAME, // flags....<br>
					　　　　　　NULL,<br>
					　　　　　　NULL);<br>
					<br>
					　　　nFrames ++; <br>
					　　　fclose(fp);<br>
					　　}<br>
					　}<br>
					<br>
					　AVIStreamClose(ps);<br>
					<br>
					　if(pfile != NULL)<br>
					　　AVIFileRelease(pfile);<br>
					　AVIFileExit();<br>
					}</td>
				</tr>
			</table>
			<br>
			　　<b>结束语：</b><br>
			<br>
			　　以上代码在 vc 6.0 和windows xp平台调试通过。这两个函数你可以直接在你的程序中使用，更详细的代码可以参见随着本文附上的示例源码。这里我要指出的是，这个AVI文件和bmp互相转换过程中，avi中的视频数据都是存放的是没有压缩的数据，如果你要分解AVI文件是经过压缩编码，比如，DVSD，MPEG4编码，首先你要采用相应的解码器对视频数据解码，然后将解码过的数据保存为bmp文件。好了，关于avi文件的介绍就到这里结束了.
			<p>&nbsp;</p>
			<p>&nbsp;</div>
		<div class="article_more">
			<div class="article_more_left">
				<table id="table12">
					<tr>
						<td class="article_more_left_title">系统分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/10053/category.aspx">
						软件开发 </a></td>
					</tr>
					<tr>
						<td class="article_more_left_title">用户分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/FIB/20481/category.aspx">
						技术文收藏</a>
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">标签:</td>
						<td class="article_more_left_value">无标签
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">来源:</td>
						<td class="article_more_left_value">转贴
                                    </td>
					</tr>
				</table>
			</div>
			<div class="article_more_right">
				<a href="http://blog.ednchina.com/FIB/95018/message.aspx#feedback">
				发表评论</a> 阅读全文(531) | 回复(0)
                        </div>
		</div>
	</div>
</div>
<div class="article">
	<div class="article_digg">
		<div id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl03_Digg1" class="digg">
			<h4 id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl03_Digg1_display" style="opacity: 1;">
			1 </h4>
			<span class="unclicked" onmouseout="Digg_Mouseout(this)" onmouseover="Digg_Mouseover(this)" onclick="if(this.className != 'clicked'){WebForm_DoCallback('ctl00$ctl00$SkinBody$Content$ContentControl$ctl03$Digg1',null,DiggClientCallBack,null,null,true)}">
			</span></div>
		<img src="http://blog.ednchina.com/images/q.gif" id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl03_aboutvote" alt="关于投票">
                    
                </div>
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/94909/message.aspx">ACM采样频率转换 
			</a></div>
		<div class="article_info">
			发表于 2008/3/19 12:19:51
                    </div>
		<div class="article_content">
			<font color="#ff0000">原作者姓名</font> 陆其明<br>
			<font color="red">文章原始出处</font> http://hqtech.nease.net 
			<p align="left"><br>
			在音频的处理中，采样频率的转换是经常碰到的问题，比如输入44.1k，要求输出48k，或者相反从48k转换到44.1k。表面上看来，只是增加或减少采样点而已。其实不然。如果只是简单地从时间域上进行采样点的增减，必然导致原有波形的改变，从而声音失真，严重的时候更是不堪入耳。<br>
			正确的方法，应该是对输入的数据进行FFT变换到频域，然后再进行转化。这是一个比较繁琐的过程。那么，有没有更简单一点的方法呢？答案是肯定的。微软提供了一套ACM的API函数可以帮我们的忙。熟悉DirectShow 
			Filter的朋友更加知道，在SDK中提供的Filter中就有一个叫ACM Wrapper的，其实它就是微软对ACM 
			API函数的包装。可以说，ACM Wrapper Filter是ACM API在DirectShow环境中应用形式。<br>
			美中不足的是，经过 ACM Wrapper 
			Filter进行采样频率转化后，由于浮点运算的误差，有可能会导致数据的丢失。每次转化的一点点丢失，如果再经过时间上的累加，音频数据会丢得越来越多。由于微软的DirectShow是基于Playback模式的一套架构，时间戳上显示的数据丢失对于人耳根本微不足道。所以仅从播放的角度上来说，这个“问题”是很难被察觉的。如果你要使用经过ACM 
			Wrapper Filter转化后的数据跟视频流合成，那么，你生成的文件很有可能在半个小时或更长的一段时间后出现音视频的不同步现象。<br>
			解决的办法有两种，一种是自己开发一个In-place-transform的Filter。这个Filter紧跟着接到ACM Wrapper 
			Filter的后面，对进来的每一个Sample检查时间戳，如果累加的音频丢失“时间”超过一个采样点的时间，则马上补上一个采样点的数据。另外一种解决方法，就是干脆使用ACM 
			API函数写一个自己的ACM Wrapper Filter。这样，就可以直接在ACM Wrapper内部监视数据的丢失。<br>
			下面我们就来看一下ACM API的使用。请先确认包含了以下头文件：mmreg.h, mmsytem.h, msacm.h；以及连接了以下库文件：msacm32.lib, 
			winmm.lib。在进行采样频率转换之前，首先要使用acmStreamOpen函数打开一个转化流，以及对输入输出数据类型的设置。示例代码如下：<br>
			bool CConversionStream::OpenStream(void)<br>
			{<br>
			&nbsp;&nbsp;&nbsp;&nbsp;DWORD maxSize = 0;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;MMRESULT mmr = acmMetrics(NULL, ACM_METRIC_MAX_SIZE_FORMAT, &amp;maxSize);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;bool pass = (mmr == MMSYSERR_NOERROR);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;if (pass)<br>
			&nbsp;&nbsp;&nbsp;&nbsp;{<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LPWAVEFORMATEX sourceFormat = (LPWAVEFORMATEX) new char [maxSize];<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LPWAVEFORMATEX destFormat&nbsp;&nbsp; = (LPWAVEFORMATEX) new char [maxSize];<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(sourceFormat, 0, maxSize);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(destFormat, 0, maxSize);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceFormat-&gt;wFormatTag = WAVE_FORMAT_PCM;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceFormat-&gt;nChannels&nbsp;&nbsp;= 2;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceFormat-&gt;nSamplesPerSec = 44100;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceFormat-&gt;wBitsPerSample = 16;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceFormat-&gt;cbSize = 0;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceFormat-&gt;nBlockAlign&nbsp;&nbsp;&nbsp;&nbsp; = 4;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceFormat-&gt;nAvgBytesPerSec = 44100 * 4;<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destFormat-&gt;wFormatTag = WAVE_FORMAT_PCM;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destFormat-&gt;nChannels&nbsp;&nbsp;= 2;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destFormat-&gt;nSamplesPerSec = 48000;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destFormat-&gt;wBitsPerSample = 16;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destFormat-&gt;cbSize = 0;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destFormat-&gt;nBlockAlign&nbsp;&nbsp;&nbsp;&nbsp; = 4;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;destFormat-&gt;nAvgBytesPerSec = 48000 * 4;<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mmr = acmStreamOpen(&amp;mStreamHandler, NULL, sourceFormat, 
			destFormat, NULL, 0, 0, 0);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass = (mmr == MMSYSERR_NOERROR);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete[] sourceFormat;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete[] destFormat;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;}<br>
			&nbsp;&nbsp;&nbsp;&nbsp;return pass;<br>
			}<br>
			实际的数据转化也很简单。首先要建立一个ACM 
			header，并对其进行设置，如果输入数据的缓冲及数据长度，输出数据的缓冲及缓冲大小。之后务必调用acmStreamPrepareHeader函数对这个ACM 
			header进行初始化。然后就调用acmStreamConvert进行数据转换。最后不要忘记调用 
			acmStreamUnprepareHeader。<br>
			bool CConversionStream::DoConverting(unsigned char * inSourceBuffer, 
			long inSourceLength,<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned char * outDestBuffer, 
			long * ioDestLength)<br>
			{<br>
			&nbsp;&nbsp;&nbsp;&nbsp;memset(mAcmheader, 0, sizeof(ACMSTREAMHEADER));<br>
			&nbsp;&nbsp;&nbsp;&nbsp;DWORD suggestedDestSize = 0;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;acmStreamSize(mStreamHandler, inSourceLength, &amp;suggestedDestSize, 
			ACM_STREAMSIZEF_SOURCE);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;ASSERT(suggestedDestSize &lt;= *ioDestLength);<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;// Build ACM header on buffer <br>
			&nbsp;&nbsp;&nbsp;&nbsp;mAcmheader-&gt;cbStruct&nbsp;&nbsp;&nbsp;&nbsp;= sizeof(ACMSTREAMHEADER);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;mAcmheader-&gt;cbSrcLength = inSourceLength;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;mAcmheader-&gt;pbSrc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = inSourceBuffer;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;mAcmheader-&gt;cbDstLength = *ioDestLength;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;mAcmheader-&gt;pbDst&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = outDestBuffer;<br>
			<br>
			&nbsp;&nbsp;&nbsp;&nbsp;// Prepare the buffer for ACM<br>
			&nbsp;&nbsp;&nbsp;&nbsp;MMRESULT mmr = acmStreamPrepareHeader(mStreamHandler, mAcmheader, 
			0);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;bool pass = (mmr == MMSYSERR_NOERROR);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;if (pass)<br>
			&nbsp;&nbsp;&nbsp;&nbsp;{<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mmr&nbsp;&nbsp;= acmStreamConvert(mStreamHandler, mAcmheader, 
			ACM_STREAMCONVERTF_BLOCKALIGN);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass = (mmr == MMSYSERR_NOERROR);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;}<br>
			&nbsp;&nbsp;&nbsp;&nbsp;*ioDestLength = mAcmheader-&gt;cbDstLengthUsed;<br>
			&nbsp;&nbsp;&nbsp;&nbsp;ASSERT(mAcmheader-&gt;cbSrcLengthUsed == mAcmheader-&gt;cbSrcLength);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;// Unprepare ACM header<br>
			&nbsp;&nbsp;&nbsp;&nbsp;acmStreamUnprepareHeader(mStreamHandler, mAcmheader,0);<br>
			&nbsp;&nbsp;&nbsp;&nbsp;return pass;<br>
			}<br>
			就这么简单！轻轻松松，实现了音频的采样频率转换。最后，当所有数据都已经转换完毕，不要忘了调用acmStreamClose函数关闭转化流。</div>
		<div class="article_more">
			<div class="article_more_left">
				<table id="table13">
					<tr>
						<td class="article_more_left_title">系统分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/10053/category.aspx">
						软件开发 </a></td>
					</tr>
					<tr>
						<td class="article_more_left_title">用户分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/FIB/20481/category.aspx">
						技术文收藏</a>
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">标签:</td>
						<td class="article_more_left_value">
						<a title="音频" href="http://blog.ednchina.com/lable/音频.aspx">
						音频</a>
						<a title="转码" href="http://blog.ednchina.com/lable/转码.aspx">
						转码</a> 
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">来源:</td>
						<td class="article_more_left_value">转贴
                                    </td>
					</tr>
				</table>
			</div>
			<div class="article_more_right">
				<a href="http://blog.ednchina.com/FIB/94909/message.aspx#feedback">
				发表评论</a> 阅读全文(1222) | 回复(0)
                        </div>
		</div>
	</div>
</div>
<div class="article">
	<div class="article_digg">
		<div id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl04_Digg1" class="digg">
			<h4 id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl04_Digg1_display" style="opacity: 1;">
			1 </h4>
			<span class="unclicked" onmouseout="Digg_Mouseout(this)" onmouseover="Digg_Mouseover(this)" onclick="if(this.className != 'clicked'){WebForm_DoCallback('ctl00$ctl00$SkinBody$Content$ContentControl$ctl04$Digg1',null,DiggClientCallBack,null,null,true)}">
			</span></div>
		<img src="http://blog.ednchina.com/images/q.gif" id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl04_aboutvote" alt="关于投票">
                    
                </div>
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/94849/message.aspx">关于Video 
			Renderer和Overlay Mixer </a></div>
		<div class="article_info">
			发表于 2008/3/19 9:42:48
                    </div>
		<div class="article_content">
			<li>文章来源: http://hqtech.nease.net </li>
			<li>原文作者: 陆其明
			<p>大家知道，Video Renderer (VR)是接收RGB/YUV裸数据，然后在显示器上显示的Filter。为提高计算机画图性能，根据你计算机显卡的能力，VR会优先使用 
			DirectDraw以及Overlay表面；如果这些特性得不到显卡的支持，VR会使用GDI函数进行画图。在上级Filter连接到VR时，VR总是先要求当前显示器设置的色彩位数的RGB格式，如你的机器设置的是24位彩色，则VR首先要求连接的Media 
			type为RGB24。如果你的显卡支持YUV Overlay表面，那么在Filter 
			Graph运行起来的时候，VR会动态改变已经连接的Media type，要求上级Filter输出一种合适的YUV格式。VR 
			Filter上实现了IVideoWindow接口，Filter Graph Manager主要通过这个接口来控制视频窗口。 <br>
			<br>
			那么，Overlay Mixer又是怎么回事呢？简单地说，Overlay 
			Mixer就是能够将几路视频流合成输出的Filter。这个Filter是特地为DVD回放（DVD有Sub-picture或line-21数据需要叠加显示）或广播视频流（含有line-21数据）而设计的。同时，它还支持硬件解码器使用Video 
			Port Extensions，就是绕过PCI总线，将硬件解码出来的数据直接送给显卡显示。这个Filter同样优先使用显卡的DirectDraw能力，而且必须要有Overlay表面。Overlay 
			Mixer有一个输出Pin，输出的Media type是：MEDIATYPE_VIDEO，MEDIASUBTYPE_ 
			Overlay；后面一般连上一个Video Renderer。当Filter Graph运行时，实际的图像显示工作由Overlay 
			Mixer完成，而Video Renderer只是做一个视频窗口的管理工作。还有另外一个更常见的Filter：Overlay Mixer 
			2。这个Filter跟Overlay Mixer功能上是一样的，只是两个Filter支持的Format 
			type不同和Merit值不同而已。 <br>
			<br>
			Overlay Mixer使用Color keying来实现几路视频的合成：它将Color 
			key和sub-picture（或line-21）数据送到主表面，将主视频数据送到Overlay表面；显卡然后将两个表面的数据合成，送到帧缓存（Frame 
			buffer）中进行显示。典型的情况，Overlay Mixer使用三个Input pin：Pin 0输入主视频数据，Pin 1和Pin 
			2输入sub-picture数据和line-21数据。Overlay Mixer在内部根据Pin 
			0输入的数据来创建Overlay表面。Overlay Mixer向上一般连接的是Video Decoder。如果这是个Software 
			decoder，则Pin 0上的数据传输使用标准的IMemInputPin接口；如果使用了硬件加速，则Pin 0上必须使用IAMVideoAccelerator接口。（注意这两种接口是不能同时使用的！）如果上一级Filter是硬件解码器的包装 
			Filter，使用VP pin输出，则解码器与Overlay Mixer使用IVPConfig和IVPNotify接口对通讯，以协调工作。Overlay 
			Mixer不支持1394或USB接口的采集设备。Overlay Mixer向下一般连的是Video Renderer。这时Video 
			Renderer只是一个视频窗口管理器。两个Filter通过IOverlay和IOverlayNotify接口对进行通讯，以协调工作。（Video 
			Renderer的Input pin有两种连接方式：VR直接做图像显示时，则使用IMemInputPin接口接收视频流数据；Overlay 
			Mixer做图像显示时，则VR使用IOverlay接口与上一级Filter进行通讯，Overlay Mixer与VR之间没有视频数据的传输。注意这两种接口是不会同时使用的！） 
			<br>
			<br>
			大家看到了，其实Video Renderer与Overlay Mixer有一部分功能是重复的。Video Renderer是最早设计的，设计之初，很多应用情况没有考虑进去；于是，就用Overlay 
			Mixer来“打补丁”。现在，我们为什么不把两部分功能整合一下呢？微软也正是这么做了！在Windows 
			XP（家庭版和专业版）中，新出现了一个Filter（注册的名字也叫“Video Renderer”，但两个Filter的CLSID是不同的，Merit值也不一样），替代了原来默认的Video 
			Renderer。这个新的Filter，称之为Video Mixing Renderer Filter 7 
			（VMR-7），因为它内部使用了DirectDraw 7的技术。可以这么说，VMR是Windows平台上新一代的Video 
			Renderer。值得注意的是，这个Filter仅在Windows 
			XP里集成，在其他任何DirectX发布包里都得不到这个Filter。VMR-7的大致功能如下：支持最多16路输入流的alpha混合；支持在合成图像显示之前得到对其访问权；支持插入第三方开发的Video 
			Effects和Transitions组件功能等等。还有，VMR连接时不要求RGB的Media type，因为它任何情况下都不会使用GDI函数来画图。 
			<br>
			<br>
			随着DirectX 9的发布，又会出现一个新的Video Renderer，称之为VMR-9。这个Filter使用了Direct3D 
			9的技术。VMR-9与VMR-7是两个不同的Filter。VMR-9的性能更加强劲。值得注意的是，为了保持向下兼容，VMR-9的Merit值并不高，它不作为系统默认的Video 
			Renderer；如果你的应用程序只需要很少的视频显示控制，建议还是使用各自平台默认的Video Renderer。 <br>
			<br>
			下面是关于一些Video Renderer使用的常见问题，可供参考： <br>
			1. 写基于DirectShow的应用程序，肯定会用到Filter Graph Manager的IVideoWindow接口。Filter 
			Graph Manager上的这个接口，实际实现于Video Renderer上。需要特别注意的是，必须在Video Renderer连接成功后才能调用这个接口的方法，否则方法调用总会失败。 
			<br>
			2. 通过IVideoWindow::put_FullScreenMode实现全屏模式。对于一些新的显卡，VR能够对图像直接拉伸后再显示（性能不会损失很大）；但如果显卡本身性能不佳，Filter 
			Graph Manager会自动将VR替换为Full Screen Renderer 
			Filter。事实上，当用户调用该接口函数要求切换到全屏模式时，Filter Graph 
			Manager的控制逻辑为：优先使用在Filter Graph中直接支持全屏模式的Video Renderer(通过IVideoWindow::get_FullScreen 
			Mode判断)；否则，使用一个对图像缩放到全屏，性能损失不是很大的Video Renderer；再则，使用Full Screen 
			Renderer Filter替换；以上尝试都失败，则选择Filter Graph中任意一个支持IVideoWindow接口的Video 
			Renderer。除了一些比较老的显卡，一般第二步尝试就能成功。 <br>
			3. 通过IBasicVideo::GetCurrentImage得到当前的图像数据。对于一般的Video Renderer来说，使用这个接口函数是不可靠的。因为如果Video 
			Renderer使用了DirectDraw加速，这个函数调用会失败；而且调用这个函数，Video Renderer必须处于Pause状态。而对于VMR，则完全没有如上这些限制。所以，在使用Video 
			Renderer的情况下，想得到整个视频流中的某一帧的图像，建议写一个In-place-trans filter，插入到Video 
			Renderer的前面，很简单就能实现。 <br>
			4. 有时候，从一个Decoder的Output pin Render出去，会自动接上Overlay Mixer 
			2这个Filter？或者自己写的Decoder，怎么样让它连接到Overlay Mixer 2？这主要是Decoder的Output 
			pin支持的Media type使用的Format type的原因。需要注意的是：Overlay Mixer 
			2仅支持Format_VIDEOINFO2，Overlay 
			Mixer虽然同时支持Format_VIDEOINFO和Format_VIDEOINFO2，但它的Merit值为 
			MERIT_DO_NOT_USE，不会被自动加入Filter Graph中</li>
		</div>
		<div class="article_more">
			<div class="article_more_left">
				<table id="table14">
					<tr>
						<td class="article_more_left_title">系统分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/10053/category.aspx">
						软件开发 </a></td>
					</tr>
					<tr>
						<td class="article_more_left_title">用户分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/FIB/20481/category.aspx">
						技术文收藏</a>
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">标签:</td>
						<td class="article_more_left_value">无标签
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">来源:</td>
						<td class="article_more_left_value">转贴
                                    </td>
					</tr>
				</table>
			</div>
			<div class="article_more_right">
				<a href="http://blog.ednchina.com/FIB/94849/message.aspx#feedback">
				发表评论</a> 阅读全文(553) | 回复(0)
                        </div>
		</div>
	</div>
</div>
<div class="article">
	<div class="article_digg">
		<div id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl05_Digg1" class="digg">
			<h4 id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl05_Digg1_display" style="opacity: 1;">
			0 </h4>
			<span class="unclicked" onmouseout="Digg_Mouseout(this)" onmouseover="Digg_Mouseover(this)" onclick="if(this.className != 'clicked'){WebForm_DoCallback('ctl00$ctl00$SkinBody$Content$ContentControl$ctl05$Digg1',null,DiggClientCallBack,null,null,true)}">
			</span></div>
		<img src="http://blog.ednchina.com/images/q.gif" id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl05_aboutvote" alt="关于投票">
                    
                </div>
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/94848/message.aspx">
			浅析DirectShow音视频同步解决完整方案 </a></div>
		<div class="article_info">
			发表于 2008/3/19 9:33:05
                    </div>
		<div class="article_content">
			<font color="#05006c">浅析DirectShow音视频同步解决完整方案</font><p>文/陆其明</p>
			<p>　　多媒体处理，不可避免地要解决音视频的同步问题。DirectShow是怎么来实现的呢？我们一起来学习一下。 </p>
			<p>　　大家知道，DirectShow结构最核心的部分是Filter Graph 
			Manager：向下控制Graph中的所有Filter，向上对应用程序提供编程接口。其中，Filter Graph 
			Manager实现的很重要一个功能，就是同步音视频的处理。简单地说，就是选一个公共的参考时钟，并并且要求给个Sample都打上时间戳，Video 
			Renderer或Audio Renderer根据Sample的时间戳来控制播放。如果到达Renderer的Sample晚了，则加快Sample的播放；如果早了，则 
			Renderer等待，一直到Sample时间戳的开始时间再开始播放。这个控制过程还引入一个叫Quality Control的反馈机制。</p>
			<p>　　下面，我们来看一下参考时钟(Reference 
			Clock)。所有Filter都参照于同一个时钟，才能统一步调。DirectShow引入了两种时钟时间：Reference 
			time和Stream time。前者是从参考时钟返回的绝对时间(IReferenceClock::GetTime)，数值本身的意义取决于参考时钟的内部实现，利用价值不大；后者是两次从参考时钟读取的数值的差值，实际应用于Filter 
			Graph内部的同步。Stream time在Filter Graph不同状态的取值为：</p>
			<p>　　1. Filter Graph运行时，取值为当前参考时钟时间减去Filter 
			Graph启动时的时间(启动时间是通过调用Filter上的IMediaFilter::Run来设置的)；</p>
			<p>　　2. Filter Graph暂停时，保持为暂停那一刻的Stream time;</p>
			<p>　　3. 执行完一次Seek操作后，复位至零；</p>
			<p>　　4. Filter Graph停止时，取值不确定。</p>
			<p>　　那么，参考时钟究竟是什么东西呢？其实，它只是一个实现了IReferenceClock接口的对象。也就是说，任何一个实现了 
			IReferenceClock接口的对象都可以成为参考时钟。在Filter Graph中，这个对象一般就是一个Filter。(在GraphEdit中，实现了参考时钟的Filter上会显示一个时钟的图标；如果同一个 
			Graph中有多个Fiter实现了参考时钟，当前被Filter Graph 
			Manager使用的那个会高亮度显示。)而且大多数情况下，参考时钟是由Audio Renderer这个Filter提供的，因为声卡上本身带有了硬件定时器资源。接下来的问题是，如果Filter 
			Graph中有多个对象实现了IReferenceClock接口，Filter Graph 
			Manager是如何做出选择的呢？默认的算法如下：</p>
			<p>　　1. 如果应用程序设置了一个参考时钟，则直接使用这个参考时钟。(应用程序通过IMediaFilter:: 
			SetSyncSource设置参考时钟，参数即为参考时钟；如果参数值为NULL，表示Filter 
			Graph不使用参考时钟，以最快的速度处理Sample；可以调用IFilterGraph:: SetDefaultSyncSource来恢复Filter 
			Graph Manager默认的参考时钟。值得注意的是，这时候的IMediaFilter接口应该从Filter Graph 
			Manager上获得，而不是枚举Graph中所有的Filter并分别调用Filter上的这个接口方法。)</p>
			<p>　　2. 如果Graph中有支持IReferenceClock接口的Live Source，则选择这个Live Source。</p>
			<p>　　3. 如果Graph中没有Live Source，则从Renderer依次往上选择一个实现IReferenceClock接口的Filter。如果连接着的Filter都不能提供参考时钟，则再从没有连接的Filter中选择。这一步算法中还有一个优先情况，就是如果Filter 
			Graph中含有一个Audio Render的链路，则直接选择Audio Renderer这个Filter(原因上面已经提及)。</p>
			<p>　　4. 如果以上方法都找不到一个适合的Filter，则选取系统参考时钟。(System Reference Clock，通过CoCreateInstance创建，CLSID为CLSID_SystemClock。)</p>
			<p>　　我们再来看一下Sample的时间戳(Time Stamp)。需要注意的是，每个Sample上可以设置两种时间戳：IMediaSample::SetTime和 
			IMediaSample::SetMediaTime。我们通常讲到时间戳，一般是指前者，它又叫Presentation 
			time，Renderer正是根据这个时间戳来控制播放；而后者对于Filter来说不是必须的，Media 
			time有没有用取决于你的实现，比如你给每个发出去的Sample依次打上递增的序号，在后面的Filter接收时就可以判断传输的过程中是否有 
			Sample丢失。我们再看一下IMediaSample::SetTime的参数，两个参数类型都是REFERENCE_TIME，千万不要误解这里的时间是Reference 
			time，其实它们用的是Stream 
			time。还有一点，就是并不是所有的Sample都要求打上时间戳。对于一些压缩数据，时间戳是很难打的，而且意义也不是很大(不过压缩数据经过 
			Decoder出来之后到达Renderer之前，一般都会打好时间戳了)。时间戳包括两个时间，开始时间和结束时间。当Renderer接收到一个 
			Sample时，一般会将Sample的开始时间和当前的Stream 
			time作比较，如果Sample来晚了或者没有时间戳，则马上播放这个Sample；如果Sample来得早了，则通过调用参考时钟的 
			IReferenceClock::AdviseTime等待Sample的开始时间到达后再将这个Sample播放。Sample上的时间戳一般由 
			Source Filter或Parser Filter来设置，设置的方法有如下几种情况：</p>
			<p>　　1. 文件回放(File 
			playback)：第一个Sample的时间戳从0开始打起，后面Sample的时间戳根据Sample有效数据的长度和回放速率来定。</p>
			<p>　　2. 音视频捕捉(Video and audio 
			capture)：原则上，采集到的每一个Sample的开始时间都打上采集时刻的Stream time。对于视频帧，Preview 
			pin出来的Sample是个例外，因为如果按上述方法打时间戳的话，每个Sample通过Filter链路传输，最后到达Video 
			Renderer的时候都将是迟到的；Video Renderer通过Quality Control反馈给Source 
			Filter，会导致Source Filter丢帧。所以，Preview 
			pin出来的Sample都不打时间戳。对于音频采集，需要注意的是，Audio Capture 
			Filter与声卡驱动程序两者各自使用了不同的缓存，采集的数据是定时从驱动程序缓存拷贝到Filter的缓存的，这里面有一定时间的消耗。</p>
			<p>　　3. 合成(Mux Filters)：取决于Mux后输出的数据类型，可以打时间戳，也可以不打时间戳。</p>
			<p>　　大家可以看到，Sample的时间戳对于保证音视频同步是很重要的。Video Renderer和Audio Renderer作为音视频同步的最终执行者，需要做很多工作。我们或许要开发其它各种类型的Filter，但一般这两个Filter是不用再开发的。一是因为Renderer 
			Filter本身的复杂性，二是因为微软会对这两个Filter不断升级，集成DirectX中其它模块的最新技术(如DirectSound、 
			DirectDraw、Direct3D等)。</p>
			<p>　　最后，我们再来仔细看一下Live Source的情况。Live Source又叫Push source，包括Video 
			/Audio Capture Filter、网络广播接收器等。Filter Graph 
			Manager是如何知道一个Filter是Live Source的呢？通过如下任何一个条件判断：</p>
			<p>　　1. 
			调用Filter上的IAMFilterMiscFlags::GetMiscFlags返回有AM_FILTER_MISC_FLAGS_IS_SOURCE标记，并且至少有一个Output 
			pin实现了IAMPushSource接口。</p>
			<p>　　2. Filter实现了IKsPropertySet接口，并且有一个Capture output 
			pin(Pin的类型为PIN_CATEGORY_CAPTURE)。</p>
			<p>　　Live Source对于音视频同步的影响主要是以下两个方面：Latency和Rate 
			Matching。Latency是指Filter处理一个Sample花费的时间，对于Live 
			Source来说，主要取决于使用缓存的大小，比如采集30fps的视频一般采集完一帧后才将数据以一个Sample发送出去，则这个Filter的 
			Latency为33ms，而Audio一般缓存500ms后才发送一个Sample，则它的Latency就为500ms。这样的话，Audio与 
			Video到达Renderer就会偏差470ms，造成音视频的不同步。默认情况下，Filter Graph 
			Manager是不会对这种情况进行调整的。当然，应用程序可以通过IAMPushSource接口来进行Latency的补偿，方法是调用 
			IAMGraphStreams::SyncUsingStreamOffset函数。Filter Graph 
			Manager的实现如下：对所有实现IAMPushSource接口的Filter调用IAMLatency::GetLatency得到各个 
			Source的Latency值，记下所有Latency值中的最大值，然后调用IAMPushSource::SetStreamOffset对各个 
			Source设置偏移值。</p>
			<p>　　这样，在Source Filter产生Sample时，打的时间戳就会加上这个偏移量。Rate 
			Matching问题的引入，主要是由于Renderer Filter和Source 
			Filter使用的是不同的参考时钟。这种情况下，Renderer对数据的播放要么太快，要么太慢。另外，一般Live 
			Source不能控制输出数据的速率，所以必须在Renderer上进行播放速率的匹配。因为人的听觉敏感度要大于视觉敏感度，所以微软目前只在 
			Audio Renderer上实现了Rate Matching。实现Rate Matching的算法是比较复杂的，这里就不再赘述。</p>
			<p>　　看到这里，大家应该对DirectShow是如何解决音视频同步问题的方案有一点眉目了吧。深层次的研究，还需要更多的测试、Base 
			class源码阅读，以及DirectShow相关控制机制的理解，比如Quality Control Management等。</div>
		<div class="article_more">
			<div class="article_more_left">
				<table id="table15">
					<tr>
						<td class="article_more_left_title">系统分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/16524/category.aspx">
						汽车电子 </a></td>
					</tr>
					<tr>
						<td class="article_more_left_title">用户分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/FIB/20481/category.aspx">
						技术文收藏</a>
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">标签:</td>
						<td class="article_more_left_value">无标签
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">来源:</td>
						<td class="article_more_left_value">转贴
                                    </td>
					</tr>
				</table>
			</div>
			<div class="article_more_right">
				<a href="http://blog.ednchina.com/FIB/94848/message.aspx#feedback">
				发表评论</a> 阅读全文(732) | 回复(0)
                        </div>
		</div>
	</div>
</div>
<div class="article">
	<div class="article_digg">
		<div id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl06_Digg1" class="digg">
			<h4 id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl06_Digg1_display" style="opacity: 1;">
			1 </h4>
			<span class="unclicked" onmouseout="Digg_Mouseout(this)" onmouseover="Digg_Mouseover(this)" onclick="if(this.className != 'clicked'){WebForm_DoCallback('ctl00$ctl00$SkinBody$Content$ContentControl$ctl06$Digg1',null,DiggClientCallBack,null,null,true)}">
			</span></div>
		<img src="http://blog.ednchina.com/images/q.gif" id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl06_aboutvote" alt="关于投票">
                    
                </div>
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/73867/message.aspx">Windows CE 
			OAL层的结构与开发 </a></div>
		<div class="article_info">
			发表于 2007/12/28 14:09:59
                    </div>
		<div class="article_content">
			摘要:<font color="#006699">Windows CE的OAL层是一个复杂的函数集。它的复杂性不但体现在包含函数数目繁多，而且体现在很多函数的硬件相关性非常大。</font> 
			<p>　</p>
			<p><strong>&nbsp;&nbsp;&nbsp; 引 言&nbsp;&nbsp;&nbsp;&nbsp; </strong></p>
			<p>&nbsp;&nbsp;&nbsp; Windows CE是<a title="microsoft" ,微软,microsoft target="_blank" href="http://it.mie168.com/qiye/microsoft">微软</a>针对嵌入式领域推出的一款全新的操作系统。之所以说它是一款全新的操作系统，是因为尽管Windows 
			CE的UI非常接近其它的桌面版Windows操作系统，但是它的内核完全是重新写的，并不是任何一款桌面版Windows的精简版本。 
			Windows CE是一种支持多种CPU架构的操作系统，这其中包括ARM、x86、MIPS和SHx，极大地减轻了0EM开发过程中移植操作系统的工作量。&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; 
			操作系统移植包含两个层面上的工作：一个层面是CPU级的，另一个层面是板级的。CPU级的移植通常由微软或芯片制造商来完成；板级移植则是由OEM来完成的。0AL正是0EM完成这一系统移植的工作核心！ 
			</p>
			<p><strong>&nbsp;&nbsp;&nbsp; 1 .<a title="oa" ,oa,办公自动化 target="_blank" href="http://it.mie168.com/OA/index.htm">OA</a>L&nbsp;&nbsp;&nbsp;
			</strong></p>
			<p>&nbsp;&nbsp;&nbsp; OAL的全称是OEM Adaption Layer，即原始<a title="shebei-kucun" ,设备,库存 target="_blank" href="http://mie168.com/manage/shebei-kucun.htm">设备</a>制造商适配层。从逻辑结构上看，它位于操作系统的内核与硬件之间，是连接系统与硬件的枢纽；从功能上看，OAL颇似桌面机上的<a title="bis" ,bi,智能系统,商业智能 target="_blank" href="http://it.mie168.com/BI/index.htm">BI</a>OS，具有初始化设备、引导操作系统以及抽象硬件功能等作用。与B10S不同的是，0AL隶属于操作系统，是操作系统的一部分。从存在方式上，讲OAL是一组函数的集合体，这些函数体现出0AL的功能，如图1所示。 
			</p>
			<p align="center">　</p>
			<p align="center">图1 Win CE 系统结构</p>
			<p><strong>&nbsp;&nbsp;&nbsp; 2 .最小化的OAL </strong></p>
			<p>&nbsp;&nbsp;&nbsp; OAL层的首要任务是加载内核。OAL层中为内核的启动作种种铺垫的函数的集合构成最小OAL层。我们可以由此深入0AL层，如图2所示。</p>
			<p align="center">　</p>
			<p align="center">图2 OS启动顺序</p>
			<p>&nbsp;&nbsp;&nbsp; 首先来看一下OS的启动顺序。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ①CPU执行引导向量，跳转到硬件初始化代码，即Startup函数；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ②在start up函数完成最小硬件环境初始化后跳转到KernelStart函数(当CPU为x86架构时为Kernel 
			Initial-ize函数)，来对内核进行初始化；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ③Kernelstart函数调用OEMInitDebugSerial完成对调试串口的初始化，调用0EMInit函数来完成硬件初始化工作以及设置时钟、中断，调用OEMGetExtensionDRAM函数来判断是否还有另外一块DRAM。&nbsp;&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; 至此，内核加载完毕。由此可见，OS启动的重中之重是Startup函数的正确加载。&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; 2.1 Startup&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; 
			Startup阶段的特点是Kernel还没有加载起来，调试工作比较困难。StartuP函数的两大核心任务分别是把CPU初始化到一已知状态和调用内核初始化函数来初始化内核。以下是Startup函数中通常包含的内容：①把处理器置为监控模式；②禁止CPU的IRQ和FIQ输入：③禁止内存管理单元 
			MMU和指令、数据Cache； ④刷新指令和数据Cache、TLB、清空写buffr； ⑤确定启动的原因一hard reset，wake 
			from sleep， GPIO reset，Watchdog reset，eboot handoff； ⑥根据<a title="zuzhi-mubiao" ,组织,目标 target="_blank" href="http://mie168.com/manage/zuzhi-mubiao.htm">目标</a>板需要配置GPIO，比如连接LED的GPIO； 
			⑦配置内存管理器，设置刷新频率，使能时钟； ⑧配置中断控制器；⑨初始化实时时钟(RTC)为0，使能实时时钟：⑩设置电源管理寄存器；⑾打开所有板级时钟和片内外部时钟；⑿取得 
			OEMAddressTable的物理基地址并把它存在r0中；⒀跳转到KernelStart。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; Bootloader和OAL中均包含Startup函数。它的功能大致相同，都是要初始化最小硬件环境。Bootloader是在为自己的执行准备硬件环境，OAL则是为kernel的执行准备硬件环境。由于这两种硬件环境要求基本相同，所以它们的代码也有很大部分可以相互借鉴。但应该明白，Bootloader与OAL在物理上是独立的，它们并不是同一段代码。而且，如果可以确定这一硬件部分Bootloader已经初始化过，则在 
			OAL中不必重复。当然，前提是每次加载都要经过Bootloader这一环节。最典型的例子就是x86 OAL中的Startup，见例程：<br>
			Naked_Startup()<br>
			{_asm<br>
			&nbsp;{<br>
			&nbsp; cli<br>
			&nbsp; jmp KernelInitialize<br>
			&nbsp;}<br>
			}<br>
			StartuP执行完毕后，跳转至Kerne1Start/Kemellnitialize(X86下）。</p>
			<p>&nbsp;&nbsp;&nbsp; 2.2 Kernel Start</p>
			<p>&nbsp;&nbsp;&nbsp; Kernel Start主要完成内核的最小初始化并且通过调用OEMInit函数来完成板级硬件初始化。以下是ARM内核初始化过程：① 
			初始化一级页表；②使能MMU和cache；③为每种工作模式使能栈(stack)；&nbsp;&nbsp;&nbsp;&nbsp; ④重新定位内核；⑤执行串口调试函数；⑥调用OEMInit；⑦初始化内存；⑧执行其它初始化。&nbsp;&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; KernelStart中用到的三个函数OEMInit()、OEMInitDebugSerial()和OEMGetExtensionDRAM() 
			中，OEMInit()硬件相关性较大，也最重要。(1)OEMlnit() 0EMInit的最小任务是初始化其它硬件和注册系统时钟。通常OEMInit应该完成以下工作。①通过设置以下值来设置中断映射表一 
			SYSINTR→IRQ和IRQ→SYSINTR。②在中断映射表中设置静态中断映射。③设置KITL，但在最小化的OAL层中通常不包括KITL。④用 
			Init 
			Clock配置系统定时器、实时时钟、时钟。⑤确定系统时钟的中断源。⑥初始化内核时间粒度为1ms。⑦配置中断控制器或可编程中断控制器(PICS)。 
			⑧提供调试用LED指示灯。⑨置pWriteDedugLED=OEMWriteDedugLED。⑩调用HookInterrupt函数来注册中断服务例程ISRs，以下示例说明如何处理TIMERISR硬件中断5的中断服务例程：<br>
			void OEMInit(void)<br>
			{<br>
			...<br>
			HookInterrupt(5,TIMERISR);//注意定时器中断<br>
			...<br>
			}</p>
			<p>&nbsp;&nbsp;&nbsp; (11)&nbsp; 清楚中断掩码，防止初始化内核有中断申请。</p>
			<p><br>
			&nbsp;&nbsp;</p>
			<p>&nbsp; (2)串口调试函数</p>
			<p>&nbsp;&nbsp;&nbsp; 
			有限的调试手段是0S移植人员经常遇到的难题。串口调试函数虽然不像以太网口调试函数那样功能强大，但仍然要比LED指示灯或数码管要直观得多，是调试 
			OAL层代码不可或缺的一组工具。这个函数组由四个函数组成，分别是0EMI 
			nitDebugSerial()、OEMReadDebugByte()、OEMWriteDebugByte()和 
			OEMWriteDebugString()。&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ◇OEMInitDebugSerial()用于配置串口；&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ◇OEMReadDebugByte0和OEMWriteDebugByte()用于向串口读写一个字节；&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ◇OEMWriteDebugString()用于向串口写一个调试用字符串。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; KernelStart中调用的是OEMInitDebugSerial()，完成串口初始化，为串口调试工作作好准备。&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; (3)OEMGetExtensionDRAM()</p>
			<p>&nbsp;&nbsp;&nbsp; 在最简最小化OAL层函数中，OEMGetExtensionDRAM()并不是一个必需的函数。OEMGetExtensionDRAM()的主要功能是查询是否存在另外一片DRAM．如果目标板上只有一片DRAM，则该函数返回FALSE。但在KernelStart通常都包含此函数。至此，最小的 
			OAL层已经完毕，kernel的最基本的功能可以正常使用。骨架搭起，第一阶段的任务告一段落，但是很多非常重要的功能还不完整，还不能做到物尽其用。于是需要进一步加强OAL层的功能。这种做法也是OAL层开发通常使用的方法。先完成基本功能，在基本功能确保正确无误后，逐渐加入其它功能。循序渐进，即使出错也很容易找到出错的地方，便于排查。&nbsp;&nbsp;&nbsp;
			</p>
			<p><strong>&nbsp;&nbsp;&nbsp; 3 .加强OAL</strong></p>
			<p>&nbsp;&nbsp;&nbsp; 第二阶段主要目的是充分利用板上硬件资源和加强调试手段。主要包括中断、KITL、以太网口调试函数和OEMIOControl四方面内容。我们把包含这四方面内容的OAL层称为加强OAL。&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; 3.1 中 断</p>
			<p>&nbsp;&nbsp;&nbsp; 
			外设硬件与CPU的数据交换基本上是异步进行的、最常用的中断形式。CE的中断处理顺序如图3所示。由图3可知，CE的中断实际上是由两部分ISR和 
			IST组成的。其中IST包含在驱动程序中，而ISR包含在OAL层中。所以，要想支持一个硬件，首先必须从0AL层为其作好准备。这个准备用两步完成。</p>
			<p align="center">　</p>
			<p align="center">图3 中断响应顺序</p>
			<p>　</p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ①创建中断标识符。下面代码节选自SAM<a title="SUN" ,sun target="_blank" href="http://it.mie168.com/qiye/SUN">SUN</a>G2410的oalintr.h。中断映射表通常位于\Platform\<br>
			\INC。#define SYSINTR USB (SYSlNTR FIRMWARE+11) #define SYSINTR USBD 
			(SYSlNTR_FIRMWARE+12)&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ②创建并注册ISR。ISR的主要任务是返回中断标识符。ISR代码通常位于\Platform\\KERNEL\HAL下。 
			下面代码节选自SAMSUNG2410的armint.c。<br>
			&nbsp;&nbsp;&nbsp; if(IntPendval==INTSRC_ADC){<br>
			&nbsp;&nbsp;&nbsp; s2410INT.&gt;r1INTSUBMSKI=BIT_SUB_TC;<br>
			&nbsp;&nbsp;&nbsp; s2410INT_&gt;rINTMSK |=BIT_ADC;<br>
			&nbsp;&nbsp;&nbsp; s2410INT_&gt;rSRCPND |=BIT_ADC;<br>
			&nbsp;&nbsp;&nbsp; s2410INT_&gt;rINTPND =BIT_ADC;<br>
			&nbsp;&nbsp;&nbsp; return(SYSINTR_TOUCH);<br>
			&nbsp;&nbsp;&nbsp; }</p>
			<p>在中断处理中，还有三个函数也起着至关重要的作用。它是OEMInterruptEnable()、OEMInterruptDisable()和OEMInterruptDone()。&nbsp;&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMInterruptEnable()用于执行允许设备产生中断的硬件操作；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMInterruptDisable()禁止设备发出中断申请；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMInterruptDone()中断处理结束。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; 3.2 以太网口调试函数</p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; 以太网口调试函数与串口调试函数相比，具有更快的速度。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMEthInit 初始化以太网调试口；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMEthEnableInts开以太网适配器中断；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMEthDisableInts关以太网适配器中断；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMEthISR 以太网适配器中断服务例程；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMEthGetFrame从以太网调试口收数据；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMEthSendFrame从以太网调试口发数据；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMEthQueryClientlnfo获取平台相关信息；&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ◇OEMEthGetSecs 返回从某一特定时间开始的计时值。本函数用于处理超时。&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; 3.3 KITL&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; KITL全称为Kernel Independent TransportLayer。它的主要用途是提供更方便的调试手段，如图4所示。KITL出现在Windows 
			CE．net之后，把软件传输协议与硬件传输层隔离开。KITL使得开发者不必了解硬件传输层如何与软件协议层接口。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; 以下是应该在OEMInit函数中加入的KITL初始化代码。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p><br>
			&nbsp;&nbsp;&nbsp;&nbsp; ①初始化所有PCI桥和设备，枚举它们并且给它们分配资源，然后使能，使他们能正常工作。注：此条适于有KITL网络接口卡(NIC)和NIC桥的情况。&nbsp;&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ② 对相关总线进行初始化，使得CPU能够正确识别NIC。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ③通过调用KitlInit函数来初始化KITL。这部分代码可参照其它平台，代码文件为Halkitl.c。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp; ④执行0EMKitlInit函数，进行相关的硬件初始化工作。搜索是否存在KITL 网口、串口或并口连接。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ⑤执行完OEMKitlInit后，把Kitl.1ib和Kitleth.1ib包含入平台资源文件\\Kernel\Buildexe\Kernkitl，以便把KITL打包进内核。有关KITL的其它函数请参考微软MSDN。 
			</p>
			<p align="center">　</p>
			<p align="center">图4&nbsp; KITL功能结构</p>
			<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.4 OEMIOControl</p>
			<p>&nbsp;&nbsp;&nbsp; OEMIOContr01在OAL层是一个非常重要的函数，应用程序是通过调用KernelIoContrOI来调用OEMl0Control的。内核对许多硬件平台信息的获得都要通过对它的调用来实现。此外，0EMl0Contr0I还是用户模式应用代码到内核模式OAL代码之间的转换入口。这就是说，用在用户模式下通过调用0EMl0Control可以获得内核模式的权力。0EMIOControl函数原型如下：<br>
			&nbsp;&nbsp;&nbsp; BOOL OEMIoControl(...)<br>
			&nbsp;&nbsp; {switch(dwloCtrolCode)<br>
			&nbsp;&nbsp; {caseIOCTL_HAL_SET_DEVICE_INFO;<br>
			&nbsp;&nbsp; case10CTL_HAL_REBOOT;<br>
			&nbsp;&nbsp; ……<br>
			&nbsp;&nbsp; default;<br>
			&nbsp; &nbsp;return FALSE;<br>
			&nbsp;&nbsp; }<br>
			&nbsp;&nbsp; return TURE;<br>
			&nbsp;&nbsp; }</p>
			<p>&nbsp;&nbsp;&nbsp; 硬件资源利用和调试手段的加强大大丰富了OAL的功能，但是嵌入式系统通常会面临的功耗问题和由于网络功能的日益普及而带来的安全性问题并没有涉及到。&nbsp;&nbsp;&nbsp;
			<strong>&nbsp;&nbsp;&nbsp;</strong></p>
			<p>　</p>
			<p>&nbsp;</p>
			<p><strong>&nbsp;4. 完整OAL</strong></p>
			<p>&nbsp;&nbsp;&nbsp; 完整OAL是指在加强OAL的基础上扩充了功耗和安全性验证的OAL。所以这一阶段的主要工作集中在电源管理与模块认证两部分。&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; 4.1 电源管理</p>
			<p>&nbsp;&nbsp;&nbsp; OAL层的电源管理与驱动程序的电源管理颇为不同。一种设备驱动程序仅负责某种特定的设备，如果可能，则把这种设备置为省电模式，当形势需要时再把设备置为满载荷模式。OAL层的电源管理则是负责整个系统功耗管理。例如，调度器在下一个25ms没有线程要运行时，系统将被置为省电模式。&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; 电源管理函数响应关闭系统和使系统空闲的系统调用。这些系统调用可能是软触发也可能是硬触发。以下两个函数是须在OAL层中实现的电源管理函数：&nbsp;&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; ◇0EMIdle一一把设备置为空闲状态，此时系统处于低功耗状态；&nbsp;&nbsp;&nbsp;&nbsp; <br>
			&nbsp; <br>
			&nbsp;&nbsp;&nbsp; ◇0EMPoweroff一一把设备置为断电状态；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ◇OEMPowerOff和OEMIdle的程序代码可在如下目录中参照例程％_WINCER00T％\Platform\\Kerlael\Hal。&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; 4.2 模块认证</p>
			<p>&nbsp;&nbsp;&nbsp; 自从Windows CE 3.0以来，在RAM中加载和运行模块前，内核可以对其进行<a title="shouquan" ,授权 target="_blank" href="http://mie168.com/zhuanti/shouquan.htm">授权</a>核查。对于在ROM中运行的模块则不需要此过程。模块认证实际上是在被加载的模块后添加一数字签名，只有当系统用公开密钥验证数字签名通过后，该模块才可以被加载到RAM中运行。这样系统可以阻止或限制一些模块的运行，达到系统安全的目的。要达到以上目的须完成以下两个函数：&nbsp;&nbsp;&nbsp;&nbsp;
			</p>
			<p>&nbsp;&nbsp;&nbsp; ◇O<a title="emc" ,emc target="_blank" href="http://it.mie168.com/qiye/emc">EMC</a>ertifyModuleInit，用于初始化验证过程，每验 
			证一个模块调用一次；&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; ◇OEMCertifyM0dule，用于验证数字签名。&nbsp;&nbsp;&nbsp;&nbsp; </p>
			<p>&nbsp;&nbsp;&nbsp; 为了支持这两个函数，在OEMInit函数中须分配两个全局变量pOEMLoadInit和p0EMLoadModule，用来存放这两个函数的地址。&nbsp;&nbsp;&nbsp;
			</p>
			<p><strong>&nbsp;&nbsp;&nbsp; 结语</strong></p>
			<p>&nbsp;&nbsp;&nbsp; Windows CE的OAL层是一个复杂的函数集。它的复杂性不但体现在包含函数数目繁多，而且体现在很多函数的硬件相关性非常大。本文并没有详细讲解每个OAL层函数，而是就一些通常会遇到的OAL层函数进行层层划分；在说明OAL层的功能和结构的同时，提出开发OAL的一种方法和思路。 
			</p>
			<p>&nbsp;</p>
			<p><font color="#999999">来源：21IC中国电子网</font></div>
		<div class="article_more">
			<div class="article_more_left">
				<table id="table16">
					<tr>
						<td class="article_more_left_title">系统分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/10046/category.aspx">
						嵌入式 </a></td>
					</tr>
					<tr>
						<td class="article_more_left_title">用户分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/FIB/20481/category.aspx">
						技术文收藏</a>
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">标签:</td>
						<td class="article_more_left_value">
						<a title="wince" href="http://blog.ednchina.com/lable/wince.aspx">
						wince</a>
						<a title="OAL" href="http://blog.ednchina.com/lable/OAL.aspx">
						OAL</a> 
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">来源:</td>
						<td class="article_more_left_value">转贴
                                    </td>
					</tr>
				</table>
			</div>
			<div class="article_more_right">
				<a href="http://blog.ednchina.com/FIB/73867/message.aspx#feedback">
				发表评论</a> 阅读全文(543) | 回复(0)
                        </div>
		</div>
	</div>
</div>
<div class="article">
	<div class="article_digg">
		<div id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl07_Digg1" class="digg">
			<h4 id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl07_Digg1_display" style="opacity: 1;">
			1 </h4>
			<span class="unclicked" onmouseout="Digg_Mouseout(this)" onmouseover="Digg_Mouseover(this)" onclick="if(this.className != 'clicked'){WebForm_DoCallback('ctl00$ctl00$SkinBody$Content$ContentControl$ctl07$Digg1',null,DiggClientCallBack,null,null,true)}">
			</span></div>
		<img src="http://blog.ednchina.com/images/q.gif" id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl07_aboutvote" alt="关于投票">
                    
                </div>
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/73836/message.aspx">Windows CE 
			内存管理 </a></div>
		<div class="article_info">
			发表于 2007/12/28 11:59:58
                    </div>
		<div class="article_content">
			<font size="2">本文译自 Douglas Boling 的 《Programming Microsoft Windows 
			CE.NET 3rd Ed》原文版权归原作者所有，译文版权归个人所有。离我第一篇blog 
			译文发布已有半年多，其实这些文章我在2003年就已经翻译完成，一直没空贴上来，但在CSDN论坛上看到许多人询问我关于内存的问题，觉得有必要将最新的经典文章带给大家，虽然该书没有中文版，但是仍然建议大家有空去买本原著，虽然亚马逊有点贵30多美金，但还是值得的。本文仅是初稿，不足之处请大家指正。译者：罗俊(nbcool) 
			原著：Douglas Boling。 </font>
			<h1 style="margin: 17pt 0cm 16.5pt;"><span style="font-family: 宋体;">
			<font size="2">内存管理</font></span></h1>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">如果你在写</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE </font></span><span style="font-family: 宋体;">
			程序中遇到的最重要的问题，那一定是内存问题。一个</span><span lang="EN-US"><font face="Times New Roman">WinCE
			</font></span><span style="font-family: 宋体;">系统可能只有</span><span lang="EN-US"><font face="Times New Roman">4MB
			</font></span><span style="font-family: 宋体;">的</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">，这相对于个人电脑来说是十分少的，因为个人电脑的标准配置已经到了</span><span lang="EN-US"><font face="Times New Roman">128MB
			</font></span><span style="font-family: 宋体;">甚至更多。事实上，运行</span><span lang="EN-US"><font face="Times New Roman">WinCE
			</font></span><span style="font-family: 宋体;">
			的机器的内存十分缺乏，以至于有时候有必要在写程序的时候为节约内存而牺牲程序的整体性能。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">幸运的是，尽管</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">系统的内存很小，但可用来管理内存的函数却十分完善。</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">实现了</span><span lang="EN-US"><font face="Times New Roman">Microsoft 
			Windows XP</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">Microsoft 
			Windows Me</font></span><span style="font-family: 宋体;">中可用到的几乎全部的</span><span lang="EN-US"><font face="Times New Roman">Win32</font></span><span style="font-family: 宋体;">内存管理</span><span lang="EN-US"><font face="Times New Roman">API</font></span><span style="font-family: 宋体;">。</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">支持虚拟内存（</span><span lang="EN-US"><font face="Times New Roman">virtual 
			memory</font></span><span style="font-family: 宋体;">）分配，本地（</span><span lang="EN-US"><font face="Times New Roman">local</font></span><span style="font-family: 宋体;">）和分离（</span><span lang="EN-US"><font face="Times New Roman">separate</font></span><span style="font-family: 宋体;">）的堆（</span><span lang="EN-US"><font face="Times New Roman">heaps</font></span><span style="font-family: 宋体;">），甚至还有（</span><span lang="EN-US"><font face="Times New Roman">memory-mapped 
			files</font></span><span style="font-family: 宋体;">）内存映射文件。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">像</span><span lang="EN-US"><font face="Times New Roman">Windows 
			XP</font></span><span style="font-family: 宋体;">一样，</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">支持一个带有应用程序间内存保护功能的</span><span lang="EN-US"><font face="Times New Roman">32</font></span><span style="font-family: 宋体;">位平面地址空间，但是</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">是被设计来应用于不同场合，所以它底层的内存结构不同于</span><span lang="EN-US"><font face="Times New Roman">Windows 
			XP</font></span><span style="font-family: 宋体;">。这些不同能够影响到你如何设计一个</span><span lang="EN-US"><font face="Times New Roman">WinCE
			</font></span><span style="font-family: 宋体;">应用程序。在这一章中，我将讲述最基础的</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">内存结构。我也将讲述包括</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">中可用的内存分配方式中的不同点以及如何使用这些不同的内存类型来最小化你的程序的内存占有率。</span></font></p>
			<h2 style="margin: 13pt 0cm;"><span style="font-family: 黑体;">
			<font size="2">内存基础</font></span></h2>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">对所有的电脑来说，系统地运行一个</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">，需要</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">（只读存储器）和</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">（随机存储器）。但不论如何，在</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">系统中，</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">的使用还是稍微有些不同于个人电脑环境。</span></font></p>
			<h3 style="margin: 13pt 0cm;"><font size="2">
			<span style="font-family: 宋体;">关于</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span></font></h3>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span lang="EN-US"><font face="Times New Roman"><span style>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			</span>RAM</font></span><span style="font-family: 宋体;">在</span><span lang="EN-US"><font face="Times New Roman">WinCE
			</font></span><span style="font-family: 宋体;">系统中被分为两个区域：第一个是程序的存储区（</span><span lang="EN-US"><font face="Times New Roman">program 
			memory</font></span><span style="font-family: 宋体;">），也叫做系统堆（</span><span lang="EN-US"><font face="Times New Roman">system 
			heap</font></span><span style="font-family: 宋体;">）。第二个是对象存储区（</span><span lang="EN-US"><font face="Times New Roman">object 
			store</font></span><span style="font-family: 宋体;">）。这个对象存储区有点像一个永久的虚拟</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">磁盘。不同于</span><span lang="EN-US"><font face="Times New Roman">PC</font></span><span style="font-family: 宋体;">上的旧式的虚拟</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">磁盘，对象存储区保留存储的文件甚至当系统被关闭以后。（脚注）这种安排的原因是</span><span lang="EN-US"><font face="Times New Roman">WinCE
			</font></span><span style="font-family: 宋体;">系统，例如</span><span lang="EN-US"><font face="Times New Roman">Pocket 
			PC</font></span><span style="font-family: 宋体;">代表性地具有一个主电池和一个备用电池。当用户更换主电池的时候，备用电池的工作是提供电源给</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">以便维持文件在对象存储区的存储。当用户按了重启键之后，</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">核心就开始寻找在关闭系统前建立的对象存储区，如果找到的话就将继续使用它。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span lang="EN-US"><font face="Times New Roman"><span style>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			</span>RAM</font></span><span style="font-family: 宋体;">中的另一个区域则用作程序存储区。程序存储区有点像个人电脑中的</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">，它为正在运行的应用程序保存堆和栈的内容。在对象存储区和程序存储区之间的分界线是可以通过移动它来改变的，用户可以在控制面板中找到改变这条分界线的设置。在可用内存降低的（</span><span lang="EN-US"><font face="Times New Roman">low-memory</font></span><span style="font-family: 宋体;">）条件下，系统将会弹出对话框询问用户是否要将对象存储区</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">划分一些给程序存储区</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">以满足要运行的应用程序的需求。</span></font></p>
			<h3 style="margin: 13pt 0cm;"><font size="2">
			<span style="font-family: 宋体;">关于</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span></font></h3>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">在个人电脑中，</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">是用来存储</span><span lang="EN-US"><font face="Times New Roman">BIOS</font></span><span style="font-family: 宋体;">（基本输入输出系统）并且只有</span><span lang="EN-US"><font face="Times New Roman">64</font></span><span style="font-family: 宋体;">－</span><span lang="EN-US"><font face="Times New Roman">128KB</font></span><span style="font-family: 宋体;">。在</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">系统中，</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">大小可以从</span><span lang="EN-US"><font face="Times New Roman">4MB</font></span><span style="font-family: 宋体;">到</span><span lang="EN-US"><font face="Times New Roman">32MB</font></span><span style="font-family: 宋体;">并且存放整个操作系统以及和系统捆绑在一起的应用程序。在这种情况下，</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">在</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">系统中就好像一个只读的硬盘。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">在一个</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">系统中，存储在</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">之上的程序能够以现场执行（</span><span lang="EN-US"><font face="Times New Roman">Execute 
			in Place</font></span><span style="font-family: 宋体;">，</span><span lang="EN-US"><font face="Times New Roman">XIP</font></span><span style="font-family: 宋体;">）的方式运行。换句话说，程序可以直接从</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">中执行而不必先加载到</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">中再执行。这种能力对小型系统来说，使之在两个方面具有巨大的优势。代码直接从</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">中执行意味着程序代码不会占据更有价值的</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">。同样，程序在执行前也不必先复制到</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">中，这样就只需要很少的时间来启动一个应用程序。不在</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">中，但是被包含在对象存储区（译者注：上文将对象存储区比作永久的</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">磁盘，故此处要说明，只有</span><span lang="EN-US"><font face="Times New Roman">Intel</font></span><span style="font-family: 宋体;">力推的</span><span lang="EN-US"><font face="Times New Roman">nor 
			flash memroy</font></span><span style="font-family: 宋体;">类型才能以</span><span lang="EN-US"><font face="Times New Roman">XIP</font></span><span style="font-family: 宋体;">方式执行，</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">其实也是一种</span><span lang="EN-US"><font face="Times New Roman">nor 
			flash memory</font></span><span style="font-family: 宋体;">类型）或闪存卡（</span><span lang="EN-US"><font face="Times New Roman">Flash 
			memory storage card</font></span><span style="font-family: 宋体;">）中的程序将不能以现场方式执行，它们将被复制到</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">中再执行。</span></font></p>
			<h3 style="margin: 13pt 0cm;"><span style="font-family: 宋体;">
			<font size="2">关于虚拟内存</font></span></h3>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span lang="EN-US"><font face="Times New Roman"><span style>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			</span>WinCE </font></span><span style="font-family: 宋体;">
			实现了系统的虚拟内存管理，在一个虚拟内存系统中，应用程序主要处理这个分离（译者注：物理上可能分离，但系统将它们联系起来），虚拟的地址空间，因此并不涉及到由硬件管理的物理内存。操作系统使用微处理器的内存管理单元来处理虚拟地址和物理地址间的实时转换。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">这种虚拟内存方法的优势能从</span><span lang="EN-US"><font face="Times New Roman">MS-DOS</font></span><span style="font-family: 宋体;">系统复杂的地址空间看出来。一旦请求的</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">超过最初</span><span lang="EN-US"><font face="Times New Roman">PC</font></span><span style="font-family: 宋体;">设计的</span><span lang="EN-US"><font face="Times New Roman">640-KB</font></span><span style="font-family: 宋体;">限制，程序设计者将不得不作出像扩展内存一样的计划以便增加可用内存的数量。</span><span lang="EN-US"><font face="Times New Roman">OS/2 
			1.x</font></span><span style="font-family: 宋体;">（译者注：</span><span lang="EN-US"><font face="Times New Roman">IBM</font></span><span style="font-family: 宋体;">研制的操作系统）和</span><span lang="EN-US"><font face="Times New Roman">Windows 
			3.0</font></span><span style="font-family: 宋体;">采用了一种基于段（</span><span lang="EN-US"><font face="Times New Roman">segment-based</font></span><span style="font-family: 宋体;">）的虚拟内存系统来解决问题。应用程序使用虚拟内存不需要知道实际物理内存的位置，只要有内存可用就行。在这些系统中，虚拟内存以一种段的方式被实现了，即可移动的内存块（译者注：段其实就是内存分块）大小从</span><span lang="EN-US"><font face="Times New Roman">16</font></span><span style="font-family: 宋体;">字节到</span><span lang="EN-US"><font face="Times New Roman">64KB</font></span><span style="font-family: 宋体;">。</span><span lang="EN-US"><font face="Times New Roman">64-KB</font></span><span style="font-family: 宋体;">的限制并不是由于段本身原因，而是由于</span><span lang="EN-US"><font face="Times New Roman">Intel 
			80286</font></span><span style="font-family: 宋体;">的特性所致，这就是</span><span lang="EN-US"><font face="Times New Roman">Windows3.x</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">OS/21.x</font></span><span style="font-family: 宋体;">的分段式虚拟内存系统结构。</span></font></p>
			<h4 style="margin: 14pt 0cm 14.5pt;"><span style="font-family: 宋体;">
			<font size="2">分页存储</font></span></h4>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<font size="2"><span lang="EN-US"><font face="Times New Roman">Intel 
			80386</font></span><span style="font-family: 宋体;">支持的段大小已经超过</span><span lang="EN-US"><font face="Times New Roman">64KB</font></span><span style="font-family: 宋体;">，但是</span><span lang="EN-US"><font face="Times New Roman">Microsoft</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">IBM</font></span><span style="font-family: 宋体;">开始设计</span><span lang="EN-US"><font face="Times New Roman">OS/2 
			2.0</font></span><span style="font-family: 宋体;">，他们选择了一种不同的虚拟内存系统，随后也被</span><span lang="EN-US"><font face="Times New Roman">386</font></span><span style="font-family: 宋体;">所支持，这就是分页式虚拟内存系统。在一个分页存储的系统中，最小的可被微处理器管理的单元是页（</span><span lang="EN-US"><font face="Times New Roman">page</font></span><span style="font-family: 宋体;">）。对于</span><span lang="EN-US"><font face="Times New Roman">Windows 
			NT</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">OS/2 
			2.0</font></span><span style="font-family: 宋体;">系统来说，页大小都被设置为</span><span lang="EN-US"><font face="Times New Roman">386</font></span><span style="font-family: 宋体;">处理器默认的</span><span lang="EN-US"><font face="Times New Roman">4096</font></span><span style="font-family: 宋体;">字节。当一个应用程序存取一个页的时候，微处理器将转换该页的虚拟内存地址到实际的</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">或</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">中的物理页（译者注：这就是实现了地址映射和转换，将虚拟的和实际的存储单元一一对应），这一页同时被标记以便其他程序对该页的访问将被排斥。操作系统决定虚拟内存页是否有效，如果有效，将做一个物理内存页到虚拟页的映射。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<font size="2"><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">实现了一个和其他</span><span lang="EN-US"><font face="Times New Roman">Win32</font></span><span style="font-family: 宋体;">操作系统类似的分页式虚拟内存系统。在</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">中，一页的大小可以从</span><span lang="EN-US"><font face="Times New Roman">1024</font></span><span style="font-family: 宋体;">字节到</span><span lang="EN-US"><font face="Times New Roman">4096</font></span><span style="font-family: 宋体;">字节，基于微处理器的不同而不同。这和</span><span lang="EN-US"><font face="Times New Roman">Windows 
			XP</font></span><span style="font-family: 宋体;">不同，</span><span lang="EN-US"><font face="Times New Roman">Windows 
			XP</font></span><span style="font-family: 宋体;">页面尺寸是</span><span lang="EN-US"><font face="Times New Roman">Intel</font></span><span style="font-family: 宋体;">微处理器所支持的</span><span lang="EN-US"><font face="Times New Roman">4096</font></span><span style="font-family: 宋体;">字节。对</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">所支持的</span><span lang="EN-US"><font face="Times New Roman">CPU</font></span><span style="font-family: 宋体;">类型来说，有</span><span lang="EN-US"><font face="Times New Roman">486</font></span><span style="font-family: 宋体;">，</span><span lang="EN-US"><font face="Times New Roman">Intel</font></span><span style="font-family: 宋体;">的</span><span lang="EN-US"><font face="Times New Roman">Strong-ARM</font></span><span style="font-family: 宋体;">，和</span><span lang="EN-US"><font face="Times New Roman">Hitachi 
			SH4 </font></span><span style="font-family: 宋体;">都是是用了</span><span lang="EN-US"><font face="Times New Roman">4096-byte
			</font></span><span style="font-family: 宋体;">的页面。</span><span lang="EN-US"><font face="Times New Roman">NEC 
			4100</font></span><span style="font-family: 宋体;">在</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE 3.0</font></span><span style="font-family: 宋体;">中使用了</span><span lang="EN-US"><font face="Times New Roman">4-KB</font></span><span style="font-family: 宋体;">的页面尺寸但是在较早期的开放式系统版本中使用了</span><span lang="EN-US"><font face="Times New Roman">1-KB</font></span><span style="font-family: 宋体;">的页面大小。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<font size="2"><span style="font-family: 宋体;">虚拟内存页可以处在三种状态：自由（</span><span lang="EN-US"><font face="Times New Roman">free</font></span><span style="font-family: 宋体;">），保留（</span><span lang="EN-US"><font face="Times New Roman">reserved</font></span><span style="font-family: 宋体;">），或被提交（</span><span lang="EN-US"><font face="Times New Roman">committed</font></span><span style="font-family: 宋体;">），）。自由页就像它的名称一样，自由并且可被分配。保留页是虚拟地址已经被保留，并且不能被操作系统或进程中的其他线程重新分配。保留页不能用在别处，但是它同样不能被当前程序使用，因为它没有被映射到物理内存。要想执行映射，它必须被提交，一个提交页能被应用程序保留，并且直接映射到物理地址。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 5.25pt; text-indent: 15.75pt;">
			<font size="2"><span style="font-family: 宋体;">所有我刚才讲述的内容对有经验的</span><span lang="EN-US"><font face="Times New Roman">Win32
			</font></span><span style="font-family: 宋体;">程序员们来说是些陈旧的知识。对</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE </font></span><span style="font-family: 宋体;">程序员来说最重要的东西是学习</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE </font></span><span style="font-family: 宋体;">是如何改变这些因素的。当</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE </font></span><span style="font-family: 宋体;">实现了大部分和它的老大哥</span><span lang="EN-US"><font face="Times New Roman">Win32</font></span><span style="font-family: 宋体;">一样的内存</span><span lang="EN-US"><font face="Times New Roman">API</font></span><span style="font-family: 宋体;">集的时候，</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">下面的基础结构将影响到上面的程序。在分开来看</span><span lang="EN-US"><font face="Times New Roman">Window 
			CE </font></span><span style="font-family: 宋体;">
			应用程序的内存结构之前，让我们先来看看一些提供系统内存全局状态的函数。</span></font></p>
			<h4 style="margin: 14pt 0cm 14.5pt;"><span style="font-family: 宋体;">
			<font size="2">查询系统的内存</font></span></h4>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">
			如果一个应用程序知道系统当前的内存状态，它将可以较好地管理可用到的资源。</span><span lang="EN-US"><font face="Times New Roman">WinCE</font></span><span style="font-family: 宋体;">实现了</span><span lang="EN-US"><font face="Times New Roman">Win32</font></span><span style="font-family: 宋体;">的</span><span class="programelement"><span lang="EN-US"><font face="Times New Roman">GetSystemInfo</font></span></span><span style="font-family: 宋体;">和</span><span class="programelement"><span lang="EN-US"><font face="Times New Roman">GlobalMemoryStatus</font></span></span><span style="font-family: 宋体;">函数，</span><span class="programelement"><span lang="EN-US"><font face="Times New Roman">GetSystemInfo</font></span></span><span style="font-family: 宋体;">函数原型如下：</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<span lang="EN-US"><font size="2" face="Times New Roman">VOID&nbsp;GetSystemInfo&nbsp;(LPSYSTEM_INFO&nbsp;lpSystemInfo);</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<font size="2"><span style="font-family: 宋体;">它传递了一个指针给</span><span lang="EN-US"><font face="Times New Roman">SYSTEM_INFO</font></span><span style="font-family: 宋体;">结构，定义如下</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">　</p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: 21pt;">　</p>
			<p><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">wProcessorArchitecture</font></span><font size="2"><span style="font-size: 12pt; font-family: 宋体;">参数表示系统微处理器的架构。它的值是定义在<span lang="EN-US">Winnt.h</span>中，例如</span><span class="programelement"><span lang="EN-US"><font face="Times New Roman">PROCESSOR_ARCHITECTURE_INTEL</font></span></span><span style="font-family: 宋体;">。</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">扩展了这些常数，包括</span><span class="programelement"><span lang="EN-US"><font face="Times New Roman">PROCESSOR_ARCHITECTURE_ARM</font></span><span style="font-family: 宋体;">，</span><span lang="EN-US"><font face="Times New Roman">PROCESSOR_ARCHITECTURE_SHx</font></span><span style="font-family: 宋体;">，等等。增加的常数包括像</span><span lang="EN-US"><font face="Times New Roman">Win32</font></span><span style="font-family: 宋体;">操作系统支持的网络</span><span lang="EN-US"><font face="Times New Roman">CPU</font></span><span style="font-family: 宋体;">（</span><span lang="EN-US"><font face="Times New Roman">net 
			CPU</font></span><span style="font-family: 宋体;">）。跳过一些参数，我们看</span><span lang="EN-US"><font face="Times New Roman">dwProcessorType</font></span><span style="font-family: 宋体;">参数，它来自于特定的微处理器类型。常数有</span></span><span lang="EN-US"><font face="Times New Roman">Hitachi 
			SHx</font></span><span style="font-family: 宋体;">架构中的</span><span class="programelement"><span lang="EN-US"><font face="Times New Roman">PROCESSOR_HITACHI_SH3</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">PROCESSOR_HITACHI_SH4</font></span><span style="font-family: 宋体;">。最后两个参数，</span><span lang="EN-US"><font face="Times New Roman">wProcessorLevel</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">wProcessorRevision</font></span><span style="font-family: 宋体;">，指明了</span><span lang="EN-US"><font face="Times New Roman">CPU</font></span><span style="font-family: 宋体;">类型的特征。</span><span lang="EN-US"><font face="Times New Roman">wProcessorLevel</font></span><span style="font-family: 宋体;">参数类似于</span><span lang="EN-US"><font face="Times New Roman">dwProcessorType</font></span><span style="font-family: 宋体;">参数，它一个指定的微处理器系列中被定义了，</span><span lang="EN-US"><font face="Times New Roman">dwProcessorRevision</font></span><span style="font-family: 宋体;">告诉你模式（</span><span lang="EN-US"><font face="Times New Roman">model</font></span><span style="font-family: 宋体;">）和芯片的步进级别（</span><span lang="EN-US"><font face="Times New Roman">stepping 
			level</font></span><span style="font-family: 宋体;">）。</span></span>
			</font></p>
			<p>　</p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">typedef&nbsp;struct&nbsp;{</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;WORD&nbsp;wProcessorArchitecture;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;WORD&nbsp;wReserved;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;dwPageSize;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;LPVOID&nbsp;lpMinimumApplicationAddress;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;LPVOID&nbsp;lpMaximumApplicationAddress;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;dwActiveProcessorMask;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;dwNumberOfProcessors;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;dwProcessorType;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;dwAllocationGranularity;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;WORD&nbsp;&nbsp;wProcessorLevel;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;WORD&nbsp;&nbsp;wProcessorRevision;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: 21pt;">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">}&nbsp;SYSTEM_INFO;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: 21pt;">
			&nbsp;</p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span class="programelement"><span lang="EN-US">
			<font face="Times New Roman"><span style>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>dwPageSize</font></span><span style="font-family: 宋体;">参数说明了微处理器页面的大小，以字节为单位。知道这个值，将会在你直接处理虚拟内存</span><span lang="EN-US"><font face="Times New Roman">API</font></span><span style="font-family: 宋体;">的时候带来方便，在此我只作简短说明。</span><span lang="EN-US"><font face="Times New Roman">lpMinimumApplication&shy;Address</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">lpMaximumApplicationAddress</font></span><span style="font-family: 宋体;">参数说明了应用程序可用到的最小和最大的虚拟内存地址。</span><span lang="EN-US"><font face="Times New Roman">dwActiveProcessorMask</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">dwNumberOfProcessors</font></span><span style="font-family: 宋体;">参数显示被</span><span lang="EN-US"><font face="Times New Roman">Window 
			XP</font></span><span style="font-family: 宋体;">系统支持的多个处理器数量。因为</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE </font></span><span style="font-family: 宋体;">
			只支持一个处理器，所以你可以忽略这个参数。</span><span lang="EN-US"><font face="Times New Roman">dwAllocationGranularity</font></span><span style="font-family: 宋体;">参数说明了一个完整的虚拟内存区域分配的界限。像</span><span lang="EN-US"><font face="Times New Roman">Windows 
			XP</font></span><span style="font-family: 宋体;">，</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE </font></span><span style="font-family: 宋体;">规定虚拟区为</span><span lang="EN-US"><font face="Times New Roman">64-KB</font></span><span style="font-family: 宋体;">的界限（译者注：作者此处</span><span lang="EN-US"><font face="Times New Roman">64-KB</font></span><span style="font-family: 宋体;">的意思是即使你只分配一个字节的内存，系统也将会保留</span><span lang="EN-US"><font face="Times New Roman">64-KB</font></span><span style="font-family: 宋体;">的虚拟地址空间给它，这个值一般是由硬件代码实现的，但是不同硬件可能不同值）。</span></span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span class="programelement"><span style lang="EN-US">
			<font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></span>
			<span style="font-family: 宋体;">第二个方便的检测系统状态的函数如下：</span></span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<span lang="EN-US"><font size="2" face="Times New Roman">void&nbsp;GlobalMemoryStatus(LPMEMORYSTATUS&nbsp;lpmst);</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<font size="2"><span style="font-family: 宋体;">它返回一个</span><span class="programelement"><span lang="EN-US"><font face="Times New Roman">MEMORYSTATUS</font></span><span style="font-family: 宋体;">结构，定义为</span></span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">typedef&nbsp;struct&nbsp;{&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwLength;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwMemoryLoad;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwTotalPhys;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwAvailPhys;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwTotalPageFile;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwAvailPageFile;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwTotalVirtual;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwAvailVirtual;&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-indent: 21pt;">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">}&nbsp;MEMORYSTATUS;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style="font-size: 12pt; font-family: 宋体" lang="EN-US">&nbsp;&nbsp;&nbsp;
			</span><span class="programelement"><span lang="EN-US">
			<font face="Times New Roman">dwLength</font></span><span style="font-family: 宋体;">参数在调用这个函数之前必须初始化。</span><span lang="EN-US"><font face="Times New Roman">dwMemoryLoad</font></span><span style="font-family: 宋体;">参数是一个不确定的值；这是一个可用的一般性的参数指示了当前系统的内存使用情况（译者注：该参数是一个近似的百分比值，指明了物理内存的使用情况）。</span><span lang="EN-US"><font face="Times New Roman">dwTotalPhys</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">dwAvailPhys</font></span><span style="font-family: 宋体;">参数指明了</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">有多少页被分配给了程序存储区</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">，和还有多少可用（译者注：实际上是以字节为单位）。这些值不包括分配对象存储区的</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">。</span></span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span class="programelement"><span lang="EN-US">
			<font face="Times New Roman"><span style>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
			dwTotalPageFile</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">dwAvailPageFile</font></span><span style="font-family: 宋体;">参数在</span><span lang="EN-US"><font face="Times New Roman">Windows 
			XP</font></span><span style="font-family: 宋体;">下和</span><span lang="EN-US"><font face="Times New Roman">Windows 
			Me</font></span><span style="font-family: 宋体;">下指明了当前页面文件（</span><span lang="EN-US"><font face="Times New Roman">paging 
			file</font></span><span style="font-family: 宋体;">）的状态。因为</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">不支持页面文件，所以这些参数总是</span><span lang="EN-US"><font face="Times New Roman">0</font></span><span style="font-family: 宋体;">。</span><span lang="EN-US"><font face="Times New Roman">dwTotalVirtual</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">dwAvailVirtual</font></span><span style="font-family: 宋体;">参数指明了总共的和可用的可被应用程序存取的虚拟内存页的数量（译者注：参数都是以字节为单位，而不是指页数，</span><span lang="EN-US"><font face="Times New Roman">dwAvailVirtual</font></span><span style="font-family: 宋体;">是指未保留和未提交的内存）。</span></span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span class="programelement"><span style lang="EN-US">
			<font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></span>
			<span style="font-family: 宋体;">通过</span><span lang="EN-US"><font face="Times New Roman">GlobalMemoryStatus</font></span><span style="font-family: 宋体;">返回的信息可以验证</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">内存结构，通过在有</span><span lang="EN-US"><font face="Times New Roman">32MBRAM</font></span><span style="font-family: 宋体;">的</span></span><span lang="EN-US"><font face="Times New Roman">HP 
			iPaq Pocket PC</font></span><span style="font-family: 宋体;">上调用函数，返回值如下：</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">dwMemoryLoad&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(24)</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">dwTotalPhys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x<st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="11" unitname="ac">011ac</st1:chmetcnv>000&nbsp;&nbsp;&nbsp;&nbsp;(18,530,304)</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">dwAvailPhys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x00B66000&nbsp;&nbsp;&nbsp;&nbsp;(11,952,128)</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">dwTotalPageFile&nbsp;&nbsp;&nbsp;&nbsp;0</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">dwAvailPageFile&nbsp;&nbsp;&nbsp;&nbsp;0</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt; text-align: left;" align="left">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">dwTotalVirtual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x02000000&nbsp;&nbsp;&nbsp;&nbsp;(33,554,432)</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt;">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">dwAvailVirtual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0x01e10000&nbsp;&nbsp;&nbsp;&nbsp;(31,522,816)</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 21pt;">
			<span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">
			<font size="2">&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style="font-size: 12pt; font-family: 宋体" lang="EN-US">&nbsp;&nbsp;&nbsp;
			</span><span class="programelement"><span lang="EN-US">
			<font face="Times New Roman">dwTotalPhys</font></span><span style="font-family: 宋体;">参数表明了系统的</span><span lang="EN-US"><font face="Times New Roman">32MB 
			RAM</font></span><span style="font-family: 宋体;">，分配了</span><span lang="EN-US"><font face="Times New Roman">18.5MB</font></span><span style="font-family: 宋体;">给程序存储区</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">，其中</span><span lang="EN-US"><font face="Times New Roman">12MB</font></span><span style="font-family: 宋体;">仍然可用。注意这对应用程序来说，并不是通过这次调用，就知道了另外</span><span lang="EN-US"><font face="Times New Roman">14MB</font></span><span style="font-family: 宋体;">的</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">是分配给对象存储区的。要检测分配给对象存储区的</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">的大小，要使用</span><span lang="EN-US"><font face="Times New Roman">GetStoreInformation</font></span><span style="font-family: 宋体;">。</span></span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span class="programelement"><span lang="EN-US">
			<font face="Times New Roman"><span style>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
			dwTotalPageFile</font></span><span style="font-family: 宋体;">和</span><span lang="EN-US"><font face="Times New Roman">dwAvailPageFile</font></span><span style="font-family: 宋体;">参数是</span><span lang="EN-US"><font face="Times New Roman">0</font></span><span style="font-family: 宋体;">，表明页面文件不被</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">所支持。</span><span lang="EN-US"><font face="Times New Roman">dwTotalVirtual</font></span><span style="font-family: 宋体;">参数十分有趣，因为它显示了</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE </font></span><span style="font-family: 宋体;">强制给程序的</span><span lang="EN-US"><font face="Times New Roman">32-MB</font></span><span style="font-family: 宋体;">的虚拟内存限制。其间，</span><span lang="EN-US"><font face="Times New Roman">dwAvailVirtual</font></span><span style="font-family: 宋体;">参数显示了只使用了</span><span lang="EN-US"><font face="Times New Roman">32MB</font></span><span style="font-family: 宋体;">虚拟内存的一小部分（译者注：即</span></span><span style="font-size: 12pt; font-family: 宋体;" lang="EN-US">33,554,432-31,522,816=2,031,616</span><span class="programelement"><span style="font-family: 宋体;">）。</span></span></font></p>
			<h3 style="margin: 13pt 0cm;"><span class="programelement">
			<font size="2"><span style="font-family: 宋体;">应用程序的地址空间</span></font></span></h3>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<font size="2"><span style="font-family: 宋体;">尽管和</span><span lang="EN-US"><font face="Times New Roman">Windows 
			XP</font></span><span style="font-family: 宋体;">的应用程序设计类似，但</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">应用程序地址空间有一个巨大的不同影响到应用程序。在</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">之下，一个应用程序被限制在虚拟内存空间中它自己的</span><span lang="EN-US"><font face="Times New Roman">32MB 
			slot</font></span><span style="font-family: 宋体;">（槽）和</span><span lang="EN-US"><font face="Times New Roman"> 
			32MB </font></span><span style="font-family: 宋体;">的</span><span lang="EN-US"><font face="Times New Roman">slot 
			1</font></span><span style="font-family: 宋体;">中，</span><span lang="EN-US"><font face="Times New Roman">slot 
			1</font></span><span style="font-family: 宋体;">用来加载基于</span><span lang="EN-US"><font face="Times New Roman">XIP</font></span><span style="font-family: 宋体;">的</span><span lang="EN-US"><font face="Times New Roman">DLL</font></span><span style="font-family: 宋体;">（译者注：</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">将虚拟地址空间分为</span><span lang="EN-US"><font face="Times New Roman">33</font></span><span style="font-family: 宋体;">个</span><span lang="EN-US"><font face="Times New Roman">slot</font></span><span style="font-family: 宋体;">，每个</span><span lang="EN-US"><font face="Times New Roman">slot 
			32MB</font></span><span style="font-family: 宋体;">，序号从</span><span lang="EN-US"><font face="Times New Roman">0-32
			</font></span><span style="font-family: 宋体;">，附插图</span><span lang="EN-US"><font face="Times New Roman">c-1,c-2</font></span><span style="font-family: 宋体;">）。当系统只有</span><span lang="EN-US"><font face="Times New Roman">4MB 
			RAM</font></span><span style="font-family: 宋体;">的时候，分配给应用程序</span><span lang="EN-US"><font face="Times New Roman">32MB</font></span><span style="font-family: 宋体;">的虚拟地址空间看起来是比较合理的，</span><span lang="EN-US"><font face="Times New Roman">Win32</font></span><span style="font-family: 宋体;">的程序员在使用这个</span><span lang="EN-US"><font face="Times New Roman">2-GB</font></span><span style="font-family: 宋体;">的虚拟地址空间的时候，必须记住对</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE</font></span><span style="font-family: 宋体;">应用程序的虚拟地址空间限制。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<font size="2"><span style="font-family: 宋体;">图</span><span lang="EN-US"><font face="Times New Roman">7-1</font></span><span style="font-family: 宋体;">展示了一个应用程序的</span><span lang="EN-US"><font face="Times New Roman">64-MB</font></span><span style="font-family: 宋体;">虚拟地址空间，其中包括</span><span lang="EN-US"><font face="Times New Roman">32MB</font></span><span style="font-family: 宋体;">用于</span><span lang="EN-US"><font face="Times New Roman">XIP</font></span><span style="font-family: 宋体;">的</span><span lang="EN-US"><font face="Times New Roman">DLL</font></span><span style="font-family: 宋体;">空间。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">
			<span style="font-family: 宋体;"><font size="2">
			<img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/nbcool/p1.bmp"></font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 21pt;">　</p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 84pt; text-indent: 21pt;">
			<font size="2"><span style="font-family: 宋体;">图</span><span lang="EN-US"><font face="Times New Roman">7-1<span style>&nbsp;&nbsp;&nbsp;&nbsp;
			</span>Windows CE</font></span><span style="font-family: 宋体;">的内存映射图</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt 84pt; text-indent: 21pt;">　</p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
			<span style="font-family: 宋体;">要注意的是应用程序是以一个</span><span lang="EN-US"><font face="Times New Roman">64-KB</font></span><span style="font-family: 宋体;">的内存区域开始从</span><span lang="EN-US"><font face="Times New Roman">0x10000</font></span><span style="font-family: 宋体;">映射，记得最低的</span><span lang="EN-US"><font face="Times New Roman">64KB</font></span><span style="font-family: 宋体;">地址空间是由</span><span lang="EN-US"><font face="Times New Roman">Windows</font></span><span style="font-family: 宋体;">为所有应用程序保留的。文件映象包括代码，静态数据段和资源段。在实际过程中，当应用程序启动时代码页（</span><span lang="EN-US"><font face="Times New Roman">code 
			pages</font></span><span style="font-family: 宋体;">）不会载入进来，只有在需要该页面被载入的时候，代码才被载入进来。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">只读静态数据段（</span><span lang="EN-US"><font face="Times New Roman">read-only 
			static data segment</font></span><span style="font-family: 宋体;">）和可读写静态数据区（</span><span lang="EN-US"><font face="Times New Roman">read/write 
			static data areas</font></span><span style="font-family: 宋体;">）只占很少的页面。这些段都是排列在一起的。如同代码一样，只有当这些数据段被应用程序读或者写的时候才会提交给</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">。应用程序的资源将被载入到一些分离的页面中，这些资源是只读的，并且只有当它们被应用程序获取的时候才会分页进入</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">应用程序的栈（</span><span lang="EN-US"><font face="Times New Roman">stack</font></span><span style="font-family: 宋体;">）被映射到资源段之上。栈的段位置很容易被找到，因为它提交的页就在被保留的区域的尾部。栈的表现是从高地址到低地址增长（译者注：即从高到低填满地址）。如果该应用程序有超过一个线程，那么应用程序的地址空间就会保留超过一个的栈的段。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">紧接着栈的就是本地堆（</span><span lang="EN-US"><font face="Times New Roman">local 
			heap</font></span><span style="font-family: 宋体;">）。引导程序保留了大量的页，大约有几百</span><span lang="EN-US"><font face="Times New Roman">K</font></span><span style="font-family: 宋体;">交给</span><span lang="EN-US"><font face="Times New Roman">heap</font></span><span style="font-family: 宋体;">来使用，但是只提交满足</span><span lang="EN-US"><font face="Times New Roman">malloc</font></span><span style="font-family: 宋体;">，</span><span lang="EN-US"><font face="Times New Roman">new</font></span><span style="font-family: 宋体;">，</span><span lang="EN-US"><font face="Times New Roman">LocalAlloc</font></span><span style="font-family: 宋体;">函数调用分配的内存（译者注：这里是说，被分配多少内存才可以提交多少内存，没被分配的不能用作提交）。从本地堆的保留页尾部到</span><span lang="EN-US"><font face="Times New Roman">non-XIP 
			DLL</font></span><span style="font-family: 宋体;">开始的部分剩余保留页面将被映射为自由的保留空间，如果</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">允许，应用程序可以提交这些保留页。</span><span lang="EN-US"><font face="Times New Roman">Non-XIP 
			DLLs </font></span><span style="font-family: 宋体;">就是不能被在</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">中现场执行的</span><span lang="EN-US"><font face="Times New Roman">DLL</font></span><span style="font-family: 宋体;">将被从上至下载入到</span><span lang="EN-US"><font face="Times New Roman">32MB</font></span><span style="font-family: 宋体;">的地址空间。</span><span lang="EN-US"><font face="Times New Roman">Non-XIP 
			DLLs </font></span><span style="font-family: 宋体;">包括那些被压缩存储在</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">中的</span><span lang="EN-US"><font face="Times New Roman">DLL</font></span><span style="font-family: 宋体;">。被压缩的</span><span lang="EN-US"><font face="Times New Roman">ROM
			</font></span><span style="font-family: 宋体;">中的文件在被载入到</span><span lang="EN-US"><font face="Times New Roman">RAM</font></span><span style="font-family: 宋体;">中执行前必须先解压缩。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style lang="EN-US"><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
			</span><span style="font-family: 宋体;">被保留给</span><span lang="EN-US"><font face="Times New Roman">XIP 
			DLLs</font></span><span style="font-family: 宋体;">的</span><span lang="EN-US"><font face="Times New Roman">32MB
			</font></span><span style="font-family: 宋体;">应用程序地址空间的较高位置。</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE </font></span><span style="font-family: 宋体;">映射这些</span><span lang="EN-US"><font face="Times New Roman">XIP 
			DLLs</font></span><span style="font-family: 宋体;">的代码进入这个空间（译者注：即较高空间），而可读写段被映射进较低位置。从</span><span lang="EN-US"><font face="Times New Roman">Windows 
			CE 4.2</font></span><span style="font-family: 宋体;">开始，在</span><span lang="EN-US"><font face="Times New Roman">ROM</font></span><span style="font-family: 宋体;">中的纯资源的</span><span lang="EN-US"><font face="Times New Roman">DLL</font></span><span style="font-family: 宋体;">将被载入到应用程序</span><span lang="EN-US"><font face="Times New Roman">64MB</font></span><span style="font-family: 宋体;">空间之外的虚拟内存空间。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">
			<span lang="EN-US"><font size="2" face="Times New Roman">&nbsp;</font></span></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><strong style>
			<font size="2"><span style="font-family: 宋体;">脚注</span></font></strong></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style="font-family: 宋体;">在</span><span lang="EN-US"><font face="Times New Roman">PocketPC</font></span><span style="font-family: 宋体;">这样的移动式系统中，当用户按下关闭按钮时系统将不会被真正的关闭，系统进入一种低功耗的挂起状态。</span></font></p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">&nbsp;</p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;">&nbsp;</p>
			<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><font size="2">
			<span style="font-family: 宋体;">
			<a class title target="_blank" href="http://www.j2medev.com/windowsmobile/ShowArticle.asp?ArticleID=4803">
			VIA</a></span></font></div>
		<div class="article_more">
			<div class="article_more_left">
				<table id="table17">
					<tr>
						<td class="article_more_left_title">系统分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/10046/category.aspx">
						嵌入式 </a></td>
					</tr>
					<tr>
						<td class="article_more_left_title">用户分类:</td>
						<td class="article_more_left_value">
						<a href="http://blog.ednchina.com/FIB/20481/category.aspx">
						技术文收藏</a>
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">标签:</td>
						<td class="article_more_left_value">
						<a title="winCE" href="http://blog.ednchina.com/lable/winCE.aspx">
						winCE</a>
						<a title="内存管理" href="http://blog.ednchina.com/lable/内存管理.aspx">
						内存管理</a> 
                                    </td>
					</tr>
					<tr>
						<td class="article_more_left_title">来源:</td>
						<td class="article_more_left_value">转贴
                                    </td>
					</tr>
				</table>
			</div>
			<div class="article_more_right">
				<a href="http://blog.ednchina.com/FIB/73836/message.aspx#feedback">
				发表评论</a> 阅读全文(363) | 回复(0)
                        </div>
		</div>
	</div>
</div>
<div class="article">
	<div class="article_digg">
		<div id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl08_Digg1" class="digg">
			<h4 id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl08_Digg1_display" style="opacity: 1;">
			1 </h4>
			<span class="unclicked" onmouseout="Digg_Mouseout(this)" onmouseover="Digg_Mouseover(this)" onclick="if(this.className != 'clicked'){WebForm_DoCallback('ctl00$ctl00$SkinBody$Content$ContentControl$ctl08$Digg1',null,DiggClientCallBack,null,null,true)}">
			</span></div>
		<img src="http://blog.ednchina.com/images/q.gif" id="ctl00_ctl00_SkinBody_Content_ContentControl_ctl08_aboutvote" alt="关于投票">
                    
                </div>
	<div style="margin-left: 100px;">
		<div class="article_title">
			<a href="http://blog.ednchina.com/FIB/73834/message.aspx">什么是 NAND 
			Flash </a></div>
		<div class="article_info">
			发表于 2007/12/28 11:19:43
                    </div>
		<div class="article_content">
			NOR和NAND是现在市场上两种主要的非易失闪存技术。Intel于1988年首先开发出NOR <br>
			flash技术，彻底改变了原先由EPROM和EEPROM一统 <br>
			天下的局面。紧接着，1989年，东芝公司发表了NAND flash结构，强调降低每比特的成 <br>
			本，更高的性能，并且象磁盘一样可以通过接口 <br>
			轻松升级。但是经过了十多年之后，仍然有相当多的硬件工程师分不清NOR和NAND闪存。 
			<p>　　相&quot;flash存储器&quot;经常可以与相&quot;NOR存储器&quot;互换使用。许多业内人士也搞不清 <br>
			楚NAND闪存技术相对于NOR技术的优越之处，因 <br>
			为大多数情况下闪存只是用来存储少量的代码，这时NOR闪存更适合一些。而NAND则是高 <br>
			数据存储密度的理想解决方案。 <br>
			　　NOR的特点是芯片内执行(XIP, eXecute In Place)，这样应用程序可以直接在flash <br>
			闪存内运行，不必再把代码读到系统RAM中。 <br>
			NOR的传输效率很高，在1～4MB的小容量时具有很高的成本效益，但是很低的写入和擦除 <br>
			速度大大影响了它的性能。 <br>
			　　NAND结构能提供极高的单元密度，可以达到高存储密度，并且写入和擦除的速度也 <br>
			很快。应用NAND的困难在于flash的管理和需要 <br>
			特殊的系统接口。 </p>
			<p>性能比较 <br>
			　　flash闪存是非易失存储器，可以对称为块的存储器单元块进行擦写和再编程。任何 <br>
			flash器件的写入操作只能在空或已擦除的单元 <br>
			内进行，所以大多数情况下，在进行写入操作之前必须先执行擦除。NAND器件执行擦除 <br>
			操作是十分简单的，而NOR则要求在进行擦除前 <br>
			先要将目标块内所有的位都写为0。 <br>
			　　由于擦除NOR器件时是以64～128KB的块进行的，执行一个写入/擦除操作的时间为5s <br>
			，与此相反，擦除NAND器件是以8～32KB的块进 <br>
			行的，执行相同的操作最多只需要4ms。 <br>
			　　执行擦除时块尺寸的不同进一步拉大了NOR和NADN之间的性能差距，统计表明，对于 <br>
			给定的一套写入操作(尤其是更新小文件时)， <br>
			更多的擦除操作必须在基于NOR的单元中进行。这样，当选择存储解决方案时，设计师必 <br>
			须权衡以下的各项因素。 <br>
			　　● NOR的读速度比NAND稍快一些。 <br>
			　　● NAND的写入速度比NOR快很多。 <br>
			　　● NAND的4ms擦除速度远比NOR的5s快。 <br>
			　　● 大多数写入操作需要先进行擦除操作。 <br>
			　　● NAND的擦除单元更小，相应的擦除电路更少。 </p>
			<p>接口差别 <br>
			　　NOR flash带有SRAM接口，有足够的地址引脚来寻址，可以很容易地存取其内部的每 <br>
			一个字节。 <br>
			　　NAND器件使用复杂的I/O口来串行地存取数据，各个产品或厂商的方法可能各不相同 <br>
			。8个引脚用来传送控制、地址和数据信息。 <br>
			　　NAND读和写操作采用512字节的块，这一点有点像硬盘管理此类操作，很自然地，基 <br>
			于NAND的存储器就可以取代硬盘或其他块设备。 </p>
			<p>容量和成本 <br>
			　　NAND flash的单元尺寸几乎是NOR器件的一半，由于生产过程更为简单，NAND结构可 <br>
			以在给定的模具尺寸内提供更高的容量，也就 <br>
			相应地降低了价格。 <br>
			　　NOR flash占据了容量为1～16MB闪存市场的大部分，而NAND flash只是用在8～128M <br>
			B的产品当中，这也说明NOR主要应用在代码存 <br>
			储介质中，NAND适合于数据存储，NAND在CompactFlash、Secure Digital、PC Cards和M <br>
			MC存储卡市场上所占份额最大。 </p>
			<p>可靠性和耐用性 <br>
			　　采用flahs介质时一个需要重点考虑的问题是可靠性。对于需要扩展MTBF的系统来说 <br>
			，Flash是非常合适的存储方案。可以从寿命 <br>
			(耐用性)、位交换和坏块处理三个方面来比较NOR和NAND的可靠性。 <br>
			　　寿命(耐用性) <br>
			　　在NAND闪存中每个块的最大擦写次数是一百万次，而NOR的擦写次数是十万次。NAND <br>
			存储器除了具有10比1的块擦除周期优势，典型 <br>
			的NAND块尺寸要比NOR器件小8倍，每个NAND存储器块在给定的时间内的删除次数要少一 <br>
			些。 <br>
			　　位交换 <br>
			　　所有flash器件都受位交换现象的困扰。在某些情况下(很少见，NAND发生的次数要 <br>
			比NOR多)，一个比特位会发生反转或被报告反转 <br>
			了。 <br>
			　　一位的变化可能不很明显，但是如果发生在一个关键文件上，这个小小的故障可能 <br>
			导致系统停机。如果只是报告有问题，多读几次 <br>
			就可能解决了。 <br>
			　　当然，如果这个位真的改变了，就必须采用错误探测/错误更正(EDC/ECC)算法。位 <br>
			反转的问题更多见于NAND闪存，NAND的供应商建 <br>
			议使用NAND闪存的时候，同时使用EDC/ECC算法。 <br>
			　　这个问题对于用NAND存储多媒体信息时倒不是致命的。当然，如果用本地存储设备 <br>
			来存储操作系统、配置文件或其他敏感信息时， <br>
			必须使用EDC/ECC系统以确保可靠性。 <br>
			　　坏块处理 <br>
			　　NAND器件中的坏块是随机分布的。以前也曾有过消除坏块的努力，但发现成品率太 <br>
			低，代价太高，根本不划算。 <br>
			　　NAND器件需要对介质进行初始化扫描以发现坏块，并将坏块标记为不可用。在已制 <br>
			成的器件中，如果通过可靠的方法不能进行这项 <br>
			处理，将导致高故障率。 </p>
			<p>易于使用 <br>
			　　可以非常直接地使用基于NOR的闪存，可以像其他存储器那样连接，并可以在上面直 <br>
			接运行代码。 <br>
			　　由于需要I/O接口，NAND要复杂得多。各种NAND器件的存取方法因厂家而异。 <br>
			　　在使用NAND器件时，必须先写入驱动程序，才能继续执行其他操作。向NAND器件写 <br>
			入信息需要相当的技巧，因为设计师绝不能向坏 <br>
			块写入，这就意味着在NAND器件上自始至终都必须进行虚拟映射。 </p>
			<p>软件支持 <br>
			　　当讨论软件支持的时候，应该区别基本的读/写/擦操作和高一级的用于磁盘仿真和 <br>
			闪存管理算法的软件，包括性能优化。 <br>
			　　在NOR器件上运行代码不需要任何的软件支持，在NAND器件上进行同样操作时，通常 <br>
			需要驱动程序，也就是内存技术驱动程序(MTD <br>
			)，NAND和NOR器件在进行写入和擦除操作时都需要MTD。 <br>
			　　使用NOR器件时所需要的MTD要相对少一些，许多厂商都提供用于NOR器件的更高级软 <br>
			件，这其中包括M-System的TrueFFS驱动，该驱 <br>
			动被Wind River System、Microsoft、QNX Software System、Symbian和Intel等厂商所 
			<br>
			采用。 <br>
			　　驱动还用于对DiskOnChip产品进行仿真和NAND闪存的管理，包括纠错、坏块处理和 <br>
			损耗平衡。</div>
	</div>
</div>
<pre>　</pre>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="logic.htm">      







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="Matrix.htm">
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">            


</a>            


</p>

</body>

</html>