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

📄 main.cpp

📁 EM8511s中使用的mp4播放器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include <stdio.h>#include <sys/time.h>#include "mp4/mp4.h"#include "mpegdec/mpegdec.h"#include "mpegdec/em85xx.h"#include "realmagichwl.h"#include "jasperlib.h"#include "aac/aac.h"#include "dac.h"#include "mpegdec/myfont.c"#include "bitstrm.h"///////////////////////////////////////////////////////////////////////////////////////// Enable this ONLY if the kernel module has hardware AAC support# #define KERNEL_MODULE_SUPPORTS_AAC	1// If the aac stream is error prone, it is required that the application converts the// AAC stream into the ADTS formatted stream.  The ADTS formatted stream allows the// aac decoding microcode to resync when errors occur. The drawback is that since// reformatting is required, more memory is required, and there is an extra memory copy.// see 14496-3 Annex 1.A for the description of the ADTS header#define REFORMAT_AAC_TO_ADTS	1#if 1static void debug_break (void){}#define ASSERT(exp)					((void)((exp)?1:(printf ("***ASSERT failed: line %d, file %s\n", __LINE__,__FILE__), debug_break(), 0)))#define DEBUGMSG(cond,printf_exp)	((void)((cond)?({printf printf_exp;fflush(stdout);}),1:0))#else#define ASSERT(exp)#define DEBUGMSG(cond,printf_exp)#endif#define MP4_MIN(X,Y) (((X)<=(Y))?(X):(Y))#define MP4_MAX(X,Y) (((X)>=(Y))?(X):(Y))// the fatfs system ASSUMES ata.o is already loaded//#define USE_FATFS	1#ifdef USE_FATFS////////////////////////////////////////////////////////////////////////////////// optional - use when using the sigma provided fat filesystem#include "fatfs.h"#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <sys/wait.h>#include <malloc.h>#include <sys/ioctl.h>#include <string.h>#include <sys/time.h>#include "rmbasic.h"#include "ata_ioctl.h"//////////////////////////////////////////////////////////////////////// required fatfs callbacksstatic int ata_fd = -1;static RMuint32 fatfs_Ata_SendCmdRWS (	RMuint8  bDrive, RMuint8  bCmd, RMuint32 nSectorLBA, 	RMuint16 nNumSectors, RMuint8  *pbBuf, RMuint32 SizeInBytes){	RMint32		result;	ATA_COMMAND	AtaCommand;	CMD_PACKET	*pCmdPacket;	RMuint8		CmdPacket[16];	if (ata_fd == -1)	{		DEBUGMSG (1, ("***Error: Ata_ReadSectorsLBA: Invalid gAtaFd. Kernel Driver not open.\n"));		return (RMuint32)-1;	}	pCmdPacket = (CMD_PACKET *)&(CmdPacket[0]);	pCmdPacket->bCmd				= bCmd;	pCmdPacket->dwLBA				= nSectorLBA;	pCmdPacket->wXxLength			= nNumSectors;		// N sectors	AtaCommand.pbCmdPkt				= CmdPacket;	AtaCommand.wPacketSize			= 12;	AtaCommand.bDrv					= bDrive;	// Primary Drive	AtaCommand.pbBuffer				= pbBuf;	AtaCommand.dwRequestedXferCount	= SizeInBytes;		// SizeOf AtaCommand.pbBuffer	AtaCommand.dwActualXferCount	= 0;	AtaCommand.wAtaStatus			= 0;	AtaCommand.wAtaError			= 0;	AtaCommand.wExStatusBytes		= 0;	result = ioctl (ata_fd, ATA_IOCTL_ISSUE_ATA_COMMAND, &AtaCommand);	if (AtaCommand.wAtaError != 0) 	{		// Some Error		DEBUGMSG (1, ("***Error: Ata_SendCmd(): ioctl(): AtaCommand.wAtaError = 0x%04X.\n", AtaCommand.wAtaError));	}	return ((RMuint32)AtaCommand.wAtaError);}static RMuint32 fatfs_Ata_SendCmd (	RMuint8 bDrive, RMuint8	bCmd, RMuint8 bFeature, 	RMuint8	bSectCounter, RMuint8 bSectNumber, RMuint8 bCylinderLo,	RMuint8	bCylinderHi, RMuint8 bDevHead, RMuint8 *pbIoBuf,	RMuint32 dwBufSizeInBytes){	RMint32 result;	ATA_COMMAND AtaCommand;	RMuint8 CmdPacket[16];	if (ata_fd == -1)	{		DEBUGMSG (1, ("***Error: Ata_ReadSectorsLBA: Invalid gAtaFd. Kernel Driver not open.\n"));		return (RMuint32)-1;	}	// Universal control - Send values for all registers + bDrv	CmdPacket[0]	= bCmd;	CmdPacket[1]	= bFeature;	CmdPacket[2]	= bSectCounter;	CmdPacket[3]	= bSectNumber;	CmdPacket[4]	= bCylinderLo;	CmdPacket[5]	= bCylinderHi;	CmdPacket[6]	= bDevHead;	AtaCommand.pbCmdPkt				= CmdPacket;	AtaCommand.wPacketSize			= 12;	AtaCommand.bDrv					= bDrive;			// Primary Drive	AtaCommand.pbBuffer				= pbIoBuf;	AtaCommand.dwRequestedXferCount	= dwBufSizeInBytes;	// SizeOf AtaCommand.pbBuffer	AtaCommand.dwActualXferCount	= 0;	AtaCommand.wAtaStatus			= 0;	AtaCommand.wAtaError			= 0;	AtaCommand.wExStatusBytes		= 0;	result = ioctl (ata_fd, ATA_IOCTL_ISSUE_ATA_COMMAND, &AtaCommand);	if (AtaCommand.wAtaError != 0) 	{		// Some Error		DEBUGMSG (1, ("***Error: Ata_SendCmd(): ioctl(): AtaCommand.wAtaError = 0x%04X.\n", AtaCommand.wAtaError));	}	return( (RMuint32)AtaCommand.wAtaError );}static void *fatfs_malloc (RMuint32 s){	void *p;		p = malloc (s);	DEBUGMSG (1, ("%08lx = fatfs_malloc (%d)\n", (RMuint32)p, (RMint32)s));	return p;}static void fatfs_free (void *p){	DEBUGMSG (1, ("fatfs_free (%08lx)\n", (RMuint32)p));	return free (p);}// mount/unmount filesystemRMuint32 fs_fat_mount (void){	FATFS_ERROR err;	FATFS_CALLBACK_TABLE callbacktable;	if (ata_fd == -1)		ata_fd = open ("/dev/ata", O_RDWR | O_NONBLOCK); 	ASSERT (ata_fd != -1);	if (ata_fd == -1)	{		DEBUGMSG (1, ("error opening /dev/ata\n"));		return (RMuint32)-1;	}	callbacktable.Ata_SendCmd = fatfs_Ata_SendCmd;	callbacktable.Ata_SendCmdRWS = fatfs_Ata_SendCmdRWS;	callbacktable.malloc = fatfs_malloc;	callbacktable.mfree = fatfs_free;	DEBUGMSG (1, ("mounting the file system ...\n"));	err = fatfs_mount (0, &callbacktable, 0);	ASSERT (err == FATFS_ERROR_NO_ERROR);	if (err != FATFS_ERROR_NO_ERROR)	{		DEBUGMSG (1, ("error mounting filesystem.\n"));		close (ata_fd);		ata_fd = -1;		return (RMuint32)-1;	}	return 0;}RMuint32 fs_fat_unmount (void){	fatfs_umount (0);}// file functionsRMuint32 fs_fat_fopen (RMint8 *filename, void *context){	FATFS_ERROR err;	RMuint32 fd = 0;	err = fatfs_fopen ((RMuint8 *)filename, _O_RDONLY, &fd);	ASSERT (err == FATFS_ERROR_NO_ERROR);	return fd;}RMuint32 fs_fat_fread (RMuint32 handle, void *buf, RMuint32 length, void *context){			RMuint8 *p = (RMuint8 *)buf;	FATFS_ERROR err;	RMuint32 n = 0;	err = fatfs_fread (handle, (RMuint8 *)buf, length, &n);	DEBUGMSG (0, ("fread (%d) : %d read\n", (RMint32)length, (RMint32)n));	for (int i=0; i<10; i++)	{		DEBUGMSG (0, ("(%02x %02x %02x %02x %02x %02x %02x %02x)\n", 			p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]));		p += 8; 	}	if (err != FATFS_ERROR_NO_ERROR)		ASSERT (n == 0);	return n;}RMuint32 fs_fat_fseek (RMuint32 handle, RMuint32 pos, RMuint32 whence, void *context){	FATFS_ERROR err;	if (whence == SEEK_SET)	{		DEBUGMSG (0, ("seek (%d, SEEK_SET)\n", (RMint32)pos));		err = fatfs_fseek (handle, pos, SEEK_SET);		ASSERT (err == FATFS_ERROR_NO_ERROR);	}	else if (whence == SEEK_CUR)	{		DEBUGMSG (0, ("seek (%d, SEEK_CUR)\n", (RMint32)pos));		RMuint32 curpos = 0;		err = fatfs_ftell (handle, &curpos);		ASSERT (err == FATFS_ERROR_NO_ERROR);		pos = (RMuint32)((RMint32)curpos + (RMint32)pos);		DEBUGMSG (0, ("seek (%d, SEEK_SET)\n", (RMint32)pos));		err = fatfs_fseek (handle, pos, SEEK_SET); 		ASSERT (err == FATFS_ERROR_NO_ERROR);	}	else		ASSERT (0);	return pos;}RMuint32 fs_fat_ftell (RMuint32 handle, void *context){	FATFS_ERROR err;	RMuint32 pos = 0;	err = fatfs_ftell (handle, &pos);	ASSERT (err == FATFS_ERROR_NO_ERROR);	return pos;}RMuint32 fs_fat_feof (RMuint32 handle, void *context){	FATFS_ERROR err;	RMuint32 eof = 0;	err = fatfs_feof (handle, &eof);	ASSERT (err == FATFS_ERROR_NO_ERROR);	return eof;}RMuint32 fs_fat_fclose (RMuint32 handle, void *context){	FATFS_ERROR err;	err = fatfs_fclose (handle);	ASSERT (err == FATFS_ERROR_NO_ERROR);	return 0;}////////////////////////////////////////////////////////////////////////#endif // USE_FATFS// If REFORMAT_AAC_TO_ADTS is defined, the pcm buffers are used as the memory// to reformat the aac data into an ADTS stream.// So, (ADTSBUFFERSIZE * ADTSNBUFFERS) == (PCMBUFFERSIZE * PCMNBUFFERS)#define BUFFERSIZE		(1024*32)#define NBUFFERS		(32)#define ADTSBUFFERSIZE	(1024*1)#define ADTSNBUFFERS	(512)#define PCMBUFFERSIZE	(1024*4)#define PCMNBUFFERS		(128)#define MY_MIN(X,Y) (((X)<=(Y))?(X):(Y))	// to find the min between two number.static RMuint32 databuffer[BUFFERSIZE * NBUFFERS / 4];static BufferPool BPmp4;static MpegDecoder *pMpegDecoder = 0;static FILE *fout = 0;static RMint32 PreBuffering = 0;static AACDecoder *pAACDecoder = 0;static RMuint32 pcmbuffer[PCMBUFFERSIZE * PCMNBUFFERS / 4];static BufferPool BPpcm;static FILE *fpcm = 0;static RMuint8 aacframe[1024*64];	// max length for an aac frame?static RMuint32 aacframe_valid = 0;static RMint32 aacframe_count = 0;static RMint32 aacdecode_time = 0;static RMuint32 *adtsbuffer = pcmbuffer;static BufferPool BPadts;static RMuint32 adtshdrlength;static RMint64 mp4_scr0 = 0;static RMuint32 vopTimeIncrRes = 30000;static RMuint32 fixedvopRate = 0;static RMuint32 videoTimeScale = 30000;static RMuint32 audioTimeScale = 30000;static RMint32 pixel_aspect_ratio_x;static RMint32 pixel_aspect_ratio_y;static RMint64 mp4_scrDelay = 0;static RMint32 resync_aacstream = 1;static RMint32 cts_counter = 0;static RMint32 gettime_ms (void){	struct timeval tv1;	struct timezone tz;	gettimeofday (&tv1, &tz);	return tv1.tv_sec * 1000 + tv1.tv_usec/1000;}///////////////////////////////////////////////////////////////////////////////// file system callbacksstatic RMuint32 my_fopen (char *filename, void *context){#ifdef USE_FATFS	return fs_fat_fopen (filename, context);#else	return (RMuint32)fopen (filename, "rb");#endif}static RMuint32 my_fread (RMuint32 handle, void *buf, RMuint32 length, void *context){#ifdef USE_FATFS	return fs_fat_fread (handle, buf, length, context);#else	return fread (buf, 1, length, (FILE *)handle);#endif}static RMuint32 my_fseek (RMuint32 handle, RMuint32 pos, RMuint32 whence, void *context){#ifdef USE_FATFS	return fs_fat_fseek (handle, pos, whence, context);#else	return fseek ((FILE *)handle, pos, whence);#endif}static RMuint32 my_ftell (RMuint32 handle, void *context){#ifdef USE_FATFS	return fs_fat_ftell (handle, context);#else	return ftell ((FILE *)handle);#endif}static RMuint32 my_fclose (RMuint32 handle, void *context){#ifdef USE_FATFS	return fs_fat_fclose (handle, context);#else		return fclose ((FILE *)handle);#endif}////////////////////////////////////////////////////////////////////////////////static RMuint32 my_addref (RMuint8 *pBuffer, void *context){	BufferPool *BP;	BP = &BPmp4;	int idx = ((int)pBuffer - (int)BP->start_address) / BP->buffers_size;	ASSERT (idx >= 0);	addref_buffer (&(BP->B[idx]));	return 0;}static RMuint32 my_release (RMuint8 *pBuffer, void *context){	BufferPool *BP;	BP = &BPmp4;	int idx = ((int)pBuffer - (int)BP->start_address) / BP->buffers_size;

⌨️ 快捷键说明

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