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

📄 mmutils.cpp

📁 神龙卡 SDK_84xx_DShow_145_02.zip 这个是 windows 上二个是linux
💻 CPP
📖 第 1 页 / 共 4 页
字号:
			{
				bFound &= (*(pBuffer+dwAd) == M2T_SYNC_BYTE);
				if (!bFound)
					break;
				dwAd += TRANSPORT_PACKET_LENGTH;
			}
			if (ind >= 5)
			{
				MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG2_TRANSPORT"));
				return FT_MPEG2_TRANSPORT;
			}
		}


		else if (MATCHBYTESTREAM(pBuffer, SEQUENCE_HEADER) ||
			MATCHBYTESTREAM(pBuffer, EXTENSION_START_CODE))
		{
			if (dwBufferPos == 0)
				dwCanBeVideo += 6;	// We need to wait at least a packet size...
			else
				dwCanBeVideo += 2;	// We need to wait at least a packet size...
		}

		else if (dwCanBeVideo && MATCHBYTESTREAM(pBuffer, EXTENSION_START_CODE))
		{
			dwCanBeVideo2 += 2;
		}
		else if ( MAKEWORD(*(pBuffer+1),*pBuffer) == AC3_HEADER)
		{	
			DWORD dwAd = 0;
			INT  wSyncWord = 0;
			BOOL  bFound = TRUE;
			INT   iFrameSize = 0;
			INT ind=0;
			for (; (ind < 20) && (dwBufferPos+dwAd+5 < dwSearchBufferSize); ind++)
			{
				wSyncWord = *(pBuffer+dwAd);
				wSyncWord = (wSyncWord << 8) | *(pBuffer+dwAd+1);
				if (wSyncWord == 0x0B77)
				{
					BYTE byte = *(pBuffer+dwAd+4);
					INT bSampleRate = byte & 0x3;
					INT bFrameSizeCode = byte & 0x3F;

					bFound &= (bFrameSizeCode < 38);
					if (!bFound)
						break;
					if      (bSampleRate == 0) iFrameSize = Ac3FrameSize[bFrameSizeCode].SR48;
					else if (bSampleRate == 1) iFrameSize = Ac3FrameSize[bFrameSizeCode].SR441;
					else if (bSampleRate == 2) iFrameSize = Ac3FrameSize[bFrameSizeCode].SR32;
					// Advance to next syncword
					if (iFrameSize > 0)
					{
						dwAd += (iFrameSize * 2);
						bFound = TRUE;
					}
					else
						dwAd++;
					iFrameSize = 0;
				}
				// Found syncword but unsuccessfull to try next syncword.
				else if (bFound)
				{
					bFound = FALSE;
					break;
				}
				else
					dwAd++;
			}
			if (bFound && (ind >= 10))
			{
				MmDebugLogfile((MmDebugLevelTrace, "Filetype: AC3_AUDIO"));
				return FT_AC3_AUDIO;
			}
			if (dwBufferPos == 0)
				dwCanBeAC3Audio += 6;	// We need to wait at least a packet size...
			else
				dwCanBeAC3Audio += 2;	// We need to wait at least a packet size...
		}
		else if ( MATCHBYTESTREAM(pBuffer, DTS_HEADER) )
		{	
			DWORD dwAd = 0;
			BOOL bFound = TRUE;
			INT iFrameSize = 0;
			INT ind = 0;
			for (; (ind < 20) && (dwBufferPos+dwAd+5 < dwSearchBufferSize); ind++)
			{
				if ( MATCHBYTESTREAM((pBuffer+dwAd), DTS_HEADER) )
				{
					iFrameSize = (((ULONG)(*(pBuffer+dwAd+5) & 0x3) << 12) | 
						((ULONG)(*(pBuffer+dwAd+6)) << 4) |
						((ULONG)(*(pBuffer+dwAd+7) & 0xf0) >> 4));
					iFrameSize++;

					// Advance to next syncword
					dwAd += iFrameSize;
					bFound = TRUE;
				}
				else
				{
					bFound = FALSE;
					break;
				}
			}
			if (bFound && (ind >= 10))
			{
				MmDebugLogfile((MmDebugLevelTrace, "Filetype: DTS_AUDIO"));
				return FT_DTS_AUDIO;
			}
			if (dwBufferPos == 0)
				dwCanBeDtsAudio += 6;	// We need to wait at least a packet size...
			else
				dwCanBeDtsAudio += 2;	// We need to wait at least a packet size...
		}
		else if (!dwCanBeVideo && ((MAKEWORD(*(pBuffer+1), *pBuffer) & AUDIO_MASK) == AUDIO_HEADER) &&
			((*(pBuffer+1) & 0xF) == 0))
		{	
			DWORD dwAd = 0;
			INT  wSyncWord = 0;
			BOOL  bFound = TRUE;
			WORD ind=0;
			for (; (ind < 20) && (dwBufferPos+dwAd) < dwSearchBufferSize; ind++)
			{
				wSyncWord = *(pBuffer+dwAd);
				wSyncWord = (wSyncWord << 8) | *(pBuffer+dwAd+1);
				if ((wSyncWord & AUDIO_MASK) == AUDIO_HEADER)
				{
					INT id = (wSyncWord >> 3) & 0x1;
					if (!id)
						break;
					INT layer = (wSyncWord >> 1) & 0x3;
					INT byte = *(pBuffer+dwAd+2);
					INT bitrateIndex = byte >> 4;
					INT sampleFreq   = (byte >> 2) & 0x3;
					INT padding      = (byte >> 1) & 0x1;
					WORD framesize = 0;
					LONG bitrate = BitRate[layer][bitrateIndex] * 1000;
					if (sampleFreq == 0)   // 44.1
					{
						if (layer == 3)
							framesize = WORD(384 * ((double)bitrate / 44100));
						else if ((layer == 2) || (layer == 1))
							framesize = WORD(1152 * ((double)bitrate / 44100));
//						if (padding)
//							dwAd++;
					}
					else if (sampleFreq == 1) // 48
					{
						if (layer == 3)
							framesize = WORD(384 * ((double)bitrate / 48000));
						else if ((layer == 2) || (layer == 1))
							framesize = WORD(1152 * ((double)bitrate / 48000));
					}
					else if (sampleFreq == 2)  // 32
					{
						if (layer == 3)
							framesize = WORD(384 * ((double)bitrate / 32000));
						else if ((layer == 2) || (layer == 1))
							framesize = WORD(1152 * ((double)bitrate / 32000));
					}
					dwAd += framesize;
					if (framesize == 0)
						dwAd++;
					// This should depend on padding for 44.1 but can't guarantee, need to do this way to
					// advance to next correct syncword.
					else if (sampleFreq == 0)
					{
						if (*(pBuffer+dwAd) != 0xFF)
							dwAd++;
					}
				}
				else
				{
					bFound = FALSE;
					break;
				}
			}
			if (bFound && (ind >= 10))
			{
				MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG_AUDIO"));
				return FT_MPEG_AUDIO;
			}
			if (dwCanBeMPEGAudio == 0)
			{
				wAudioStart = *(WORD *) pBuffer;
			}
			if (wAudioStart == *(WORD *) pBuffer)
			{
				if (dwBufferPos == 0)
					dwCanBeMPEGAudio += 6;	// We need to wait at least a packet size...
				else
					dwCanBeMPEGAudio += 1;	// We need to wait at least a packet size...
			}
		}

		if (dwBufferPos > MIN_PACKET_HEADER_SEARCH)
		{	
			// We have waited at least a packet size...
			DWORD dwCanbeMAX = max (dwCanBeVideo, max (dwCanBeAC3Audio, 
				max (dwCanBeMPEGAudio, max (dwCanBeSystem , dwCanBeSystem2))));
			if (dwCanbeMAX == 0)
			{
				// Found Nothing !!!
			}
			else if (dwCanBeSystem == dwCanbeMAX)
			{
				MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG_SYSTEM"));
				return FT_MPEG_SYSTEM;
			}
			else if (dwCanBeSystem2 == dwCanbeMAX)
			{
				MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG2_SYSTEM"));
				return FT_MPEG2_SYSTEM;
			}
			else if (dwCanBeVideo == dwCanbeMAX)
			{
				if (dwCanBeVideo2!=0)
				{
					MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG2_VIDEO"));
					return FT_MPEG2_VIDEO;
				}
				else
				{
					MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG_VIDEO"));
					return FT_MPEG_VIDEO;
				}
			}
/*			else if (dwCanBeAC3Audio == dwCanbeMAX)
			{
				MmDebugLogfile((MmDebugLevelTrace, "Filetype: AC3_AUDIO"));
				return FT_AC3_AUDIO;
			}
			else if (dwCanBeMPEGAudio == dwCanbeMAX)
			{
				MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG_AUDIO"));
				return FT_MPEG_AUDIO;
			}
*/
		}
		pBuffer++;	// BYTE pointer inc => @+1
	}
	MmDebugLogfile((MmDebugLevelTrace, "Filetype: FT_UNKNOWN"));
	return FT_UNKNOWN;
}

////////////////////////////////////////////////////////////////////

UINT CheckMPG4type(char *pFileName)
{
	BYTE bBuffer[READ_BUFFERSIZE];
	DWORD dwSize;
	DWORD dwCurPos = 0;
	BYTE* pbtype;
	LONG lSearchBufferSize = 0;
	INT inexttype = 0;
	INT iMp4Type = FT_UNKNOWN;
	char* typelist[NUMBERTYPES] = {"moov", "trak", "mdia", "minf", "stbl", "stsd"};
	char* mpeg4types[MPEG4TYPES] = {"mp4v", "mp4a", "mp4s"};

	FILE *file = 0;

	file = fopen(pFileName, "rb");
	if (!file)	return FT_UNKNOWN;

	lSearchBufferSize = fread((char*)&bBuffer, sizeof(BYTE), MAXHEADERSIZE, file);
	if(lSearchBufferSize < SIZEPLUSTYPE) goto end_1;

	dwSize = (((DWORD)bBuffer[0])<<24) | (((DWORD)bBuffer[1])<<16) |
		(((DWORD)bBuffer[2])<<8) | ((DWORD)bBuffer[3]);
	if(dwSize == 0) goto end_1;
	else if(dwSize == 1)
	{
		dwSize =(((DWORD)bBuffer[SIZEPLUSTYPE])<<56) | (((DWORD)bBuffer[SIZEPLUSTYPE+1])<<48) |
			(((DWORD)bBuffer[SIZEPLUSTYPE+2])<<40) | (((DWORD)bBuffer[SIZEPLUSTYPE+3])<<32) |
			(((DWORD)bBuffer[SIZEPLUSTYPE+4])<<24) | (((DWORD)bBuffer[SIZEPLUSTYPE+5])<<16) |
			(((DWORD)bBuffer[SIZEPLUSTYPE+6])<<8)  | ((DWORD)bBuffer[SIZEPLUSTYPE+7]);
	}
	pbtype = &(bBuffer[4]);
	if(strncmp((const char*)pbtype, (const char*)typelist[inexttype], TYPENAMELENGTH) == 0)
	{
		inexttype++;
		dwCurPos += SIZEPLUSTYPE;
	}
	else 
		dwCurPos += dwSize;
	fseek(file, (LONG)dwCurPos, SEEK_SET);
	while(lSearchBufferSize == MAXHEADERSIZE)
	{
		lSearchBufferSize = fread((char*)&bBuffer, sizeof(BYTE), MAXHEADERSIZE, file);
		if(lSearchBufferSize < SIZEPLUSTYPE)
		{
			if(inexttype > NUMBERTYPES - 1) goto end_quicktime;
			goto end_1;
		}
		dwSize = (((DWORD)bBuffer[0])<<24) | (((DWORD)bBuffer[1])<<16) |
			(((DWORD)bBuffer[2])<<8) | ((DWORD)bBuffer[3]);
		if(dwSize == 0)
		{
			if(inexttype > NUMBERTYPES - 1) goto end_quicktime;
			goto end_1;
		}
		else if(dwSize == 1)
		{
			dwSize =(((DWORD)bBuffer[SIZEPLUSTYPE])<<56) | (((DWORD)bBuffer[SIZEPLUSTYPE+1])<<48) |
				(((DWORD)bBuffer[SIZEPLUSTYPE+2])<<40) | (((DWORD)bBuffer[SIZEPLUSTYPE+3])<<32) |
				(((DWORD)bBuffer[SIZEPLUSTYPE+4])<<24) | (((DWORD)bBuffer[SIZEPLUSTYPE+5])<<16) |
				(((DWORD)bBuffer[SIZEPLUSTYPE+6])<<8)  | ((DWORD)bBuffer[SIZEPLUSTYPE+7]);
		}
		pbtype = &bBuffer[4];
		if(inexttype < NUMBERTYPES)
		{
			if(strncmp((const char*)pbtype, (const char*)typelist[inexttype], TYPENAMELENGTH) == 0)
			{
				inexttype++;
				dwCurPos += SIZEPLUSTYPE;
				if(inexttype == NUMBERTYPES) dwCurPos +=STSDOFFSET;
			}
			else 
				dwCurPos += dwSize;
		}
		else
		{
			if(	memcmp(pbtype, mpeg4types[0], TYPENAMELENGTH) == 0 ) goto end_systemvideo;
			if(	memcmp(pbtype, mpeg4types[1], TYPENAMELENGTH) == 0 ) goto end_systemaudio;
			if(	memcmp(pbtype, mpeg4types[2], TYPENAMELENGTH) == 0 ) goto end_system;
			dwCurPos += dwSize;
		}
		if (fseek(file, (LONG)dwCurPos, SEEK_SET) != 0)
		{
			if(inexttype > NUMBERTYPES - 1) goto end_quicktime;
			else goto end_1;
		}
	}

end_system:
	iMp4Type = FT_MPEG4_SYSTEM;
	MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG4_SYSTEM"));
	goto end_1;
end_systemaudio:
	iMp4Type = FT_MPEG4_SYSTEMAUDIO;
	MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG4_SYSTEMAUDIO"));
	goto end_1;
end_systemvideo:
	iMp4Type = FT_MPEG4_SYSTEMVIDEO;
	MmDebugLogfile((MmDebugLevelTrace, "Filetype: MPEG4_SYSTEMVIDEO"));
	goto end_1;
end_quicktime:
	iMp4Type = FT_QUICKTIME;
end_1:
	fclose(file);
	return iMp4Type;
}

////////////////////////////////////////////////////////////////////
/****f* MMDemux/IsPESFile
 * USAGE
 *  BOOL IsPESFile (const BYTE *pSearchBuffer, DWORD dwSearchBufferSize)
 * DESCRIPTION
 *  Verify whether the source file is a PES file by looking for the pack and
 *  header start code. Also need to verify the sync byte of Transport Stream.
 * PARAMETERS
 *  const BYTE *pSearchBuffer - a pointer to the data buffer to search.
 *  DWORD dwSearchBufferSize  - size of the search buffer.
 * RETURN VALUE
 *  TRUE if it is a PES file.
 *  FALSE otherwise.
/**********************************************************************/
BOOL IsPESFile(const BYTE *pSearchBuffer, DWORD dwSearchBufferSize)
{
	DWORD dwSyncCode = 0;
	DWORD dwPos = 0;
	BOOL bFound = TRUE;
	DWORD i;

	for (i = 0; i < 4; i++)
	{
		if (*(pSearchBuffer + i) == M2T_SYNC_BYTE)
		{
			BOOL bFound = TRUE;
			DWORD dwPos;
			for (dwPos=0; i+dwPos < min (dwSearchBufferSize, 20 * TRANSPORT_PACKET_LENGTH);
					dwPos+= TRANSPORT_PACKET_LENGTH)
			{
				bFound &= (*(pSearchBuffer+dwPos) == M2T_SYNC_BYTE);
				if (!bFound)
					break;
			}
			if ((bFound) && (dwPos > 10))
				return FALSE;
		}
		dwSyncCode = (dwSyncCode << 8) + (*(pSearchBuffer + i));
	}

	for (; i < dwSearchBufferSize; i++)
	{
		if (dwSyncCode == PACK_START_CODE || dwSyncCode == SYSTEM_START_CODE)
			return FALSE;
		else if (((dwSyncCode & 0xFFFFFFF0) == AUDIO_STREAM) || 
						((dwSyncCode & 0xFFFFFFF0) == VIDEO_STREAM) || (dwSyncCode == AC3_PCM_DTS_STREAM))
		{
			dwPos = i;
			DWORD dwPacketLength = *(pSearchBuffer + dwPos++);
			dwPacketLength = (dwPacketLength << 8) + *(pSearchBuffer + dwPos++);
			for (; dwPos < min (dwSearchBufferSize, 10 * dwPacketLength);)
			{
				dwPos += dwPacketLength;
				dwSyncCode = 0;
				for (int j = 0; j < 4; j++)
					dwSyncCode = (dwSyncCode << 8) + (*(pSearchBuffer + dwPos++));
				if (((dwSyncCode & 0xFFFFFFF0) == AUDIO_STREAM) || 
						((dwSyncCode & 0xFFFFFFF0) == VIDEO_STREAM) || (dwSyncCode == AC3_PCM_DTS_STREAM))
				{
					bFound = TRUE;
				}
				else
					bFound = FALSE;
				dwPacketLength = *(pSearchBuffer + dwPos++);
				dwPacketLength = (dwPacketLength << 8) + *(pSearchBuffer + dwPos++);
			}
			if (bFound)
			{
				printf("FT_PES\n");
				return TRUE;
			}				
		}
		else if (*(pSearchBuffer + i) == M2T_SYNC_BYTE)
		{
			bFound = TRUE;
			for (dwPos=i; i+dwPos < min (dwSearchBufferSize, 20 * TRANSPORT_PACKET_LENGTH);
					dwPos+= TRANSPORT_PACKET_LENGTH)
			{
				bFound &= (*(pSearchBuffer+dwPos) == M2T_SYNC_BYTE);
				if (!bFound)
					break;
			}
			if ((bFound) && (dwPos > 10))
				return FALSE;
		}
		dwSyncCode = (dwSyncCode << 8) + (*(pSearchBuffer + i));
	}
	return FALSE;
}

////////////////////////////////////////////////////////////////////
/****f* MMDemux/GetAudioFrequency
 * USAGE
 *  DWORD GetAudioFrequency (char *pFile, int iStreamType = FT_UNKNOWN)
 *  DWORD GetAudioFrequency(unsigned char* pBuffer, unsigned long lLength, int iStreamType = FT_UNKNOWN)
 * DESCRIPTION
 *  Get the audio frequency of a stream (a file).
 * PARAMETERS
 *  char *pFile - name of the file to get the frequency.
 * RETURN VALUE
 *  One of the following frequencies:
 *    AUDIO_FREQ_441
 *    AUDIO_FREQ_48
 *    AUDIO_FREQ_32
 *    AUDIO_FREQ_RESERVED
/**********************************************************************/
DWORD GetAudioFrequency(char *pFile, int iStreamType)
{
	BYTE bBuffer[READ_BUFFERSIZE];
	DWORD dwReadSize = 0;
	FILE *pPlayfile;

	if ((pPlayfile = fopen(pFile, "rb")) == NULL)
	{
		printf("Unable to open file!\n");
		return FALSE;
	}
	dwReadSize = fread(bBuffer, sizeof(char), READ_BUFFERSIZE, pPlayfile);
	fclose(pPlayfile);

	return GetAudioFrequency(bBuffer, dwReadSize, iStreamType);
}

////////////////////////////////////////////////////////////////////

DWORD GetAudioFrequency(unsigned char* pBuffer, unsigned long lLength, int iStreamType)
{
	DWORD dwBufferIndex = 0;
	DWORD dwSyncCode = 0;
	INT wPacketLength = 0;
	BYTE byte = 0;

	if (iStreamType == FT_UNKNOWN)
		iStreamType = IdentifyHeader(pBuffer, lLength);
	if (iStreamType == FT_AC3_AUDIO)
		return GetAc3AudioFrequency(pBuffer, lLength);
	if (iStreamType == FT_DTS_AUDIO)
		return AUDIO_FREQ_48;

	while (dwBufferIndex < lLength)
	{
		dwSyncCode = (dwSyncCode << 8) + *(pBuffer + dwBufferIndex++);

		if (dwSyncCode == PACK_START_CODE)
		{	
			dwBufferIndex += 9;   // SCR and Mux rate
			if (dwBufferIndex >= lLength)
				break;

⌨️ 快捷键说明

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