?? main.cpp
字號:
// sample app on how to use the avi demux object as defined in avi.h//// the only purpose of this app is to only show you how to use the avi demux// object and nothing else//#include <stdio.h>#include <stdlib.h>#include "mpegdec/mpegdec.h"#include "mpegdec/em85xx.h"#include "mp3/mp3.h"#include "avi/avi.h"#include "realmagichwl.h"#include "jasperlib.h"#include "util.h"#include "input/input.h"#include "vol.h"#include "scanner.h"#include "mpegdec/myfont.c" #include "avi_subtitle.h"#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),1:0))#else#define ASSERT(exp)#define DEBUGMSG(cond,printf_exp)#endif// 2MB requiredstatic RMuint32 databuffers[1024*1024*2/4];// memory map// [avi buffer] 1024 * 1024 bytes (8 buffers of 128k)// [pcm buffer] 1024 * 1024 bytes (200 buffers of 1125*4)#define AVI_BUFFERSIZE (128 * 1024)#define AVI_NBUFFERS (8)#define PCM_BUFFERSIZE (1152 * 4)#define PCM_NBUFFERS (200)// avi .srt decoderstatic AVISubtitle *pAVISubtitle = 0;// mpeg decoderMpegDecoder *pMpegDecoder = 0;// avi buffersstatic BufferPool BPavi;static RMuint32 *avi_buffer = (databuffers);// decoded mp3 buffersstatic RMuint32 *pcm_buffer = (avi_buffer + (AVI_BUFFERSIZE*AVI_NBUFFERS)/4);static BufferPool BPpcm;static RMuint32 tbytes = 0;static RMuint32 NumberOfBitsPerSample = 0;static RMuint32 NumberOfChannels = 0;static RMuint32 SamplesPerSecond = 0;static RMuint32 BytesPerSecond = 0;static RMuint32 CompressedBytesPerSecond = 0;// decode/demux objectsstatic AVIDemux *pAVIDemux = 0;static MP3Decoder *pMP3BufferDecoder = 0;static MP3Decoder *pMP3Decoder = 0;// for AVI playback#define AVI_DELAY_MS 250static RMint64 aviTimeScale = 0;static RMint64 aviStartupDelay = 0;static RMint64 aviTime0 = 0;static RMint64 audioTime = 0;static RMint64 videoTime = 0;static RMint64 aviFrameCounter = 0;static RMint64 aviTicksPerFrame = 0;static RMint32 ac3FrameSize = 0;static RMint32 dtsFrameSize = 0;static RMint32 video_stream_count = 0;static RMint32 audio_stream_count = 0;static RMint32 avi_stream_count = 0;static RMint8 selected_audio_stream[3];static RMint16 selected_audio_stream_format = 0;static RMint32 KeyFramesOnly = 0;static RMuint32 KeyFramesPosition = 0;static RMint32 avi_initial_start = 0;static RMuint32 saved_input = 0;static RMint32 frmsizecod_table[4][38] = { // 48 {64,64,80,80,96,96,112,112,128,128,160,160,192,192,224,224,256,256, 320,320,384,384, 448,448,512,512,640,640,768,768,896,896,1024,1024, 1152,1152,1280,1280}, // 44.1 {69,70,87,88,104,105,121,122,139,140,174,175,208,209,243,244,278, 279,348,349,417,418,487,488,557,558,696,697,835,836,975,976,1114, 1115,1253,1254,1393,1394}, // 32 {96,96,120,120,144,144,168,168,192,192,240,240,288,288,336,336,384, 384,480,480,576,576,672,672,768,768,960,960,1152,1152,1344,1344, 1536,1536,1728,1728,1920,1920}};#define DIVX_FOURCC AVI_FOURCC('D','I','V','X')#define divx_FOURCC AVI_FOURCC('d','i','v','x')#define DX50_FOURCC AVI_FOURCC('D','X','5','0')#define XVID_FOURCC AVI_FOURCC('X','V','I','D')#define xvid_FOURCC AVI_FOURCC('x','v','i','d')#define XCRD_FOURCC AVI_FOURCC('X','C','R','D')#define RMP4_FOURCC AVI_FOURCC('R','M','P','4')#define div3_FOURCC AVI_FOURCC('d','i','v','3')#define DIV3_FOURCC AVI_FOURCC('D','I','V','3')#define mp43_FOURCC AVI_FOURCC('m','p','4','3')#define MP43_FOURCC AVI_FOURCC('M','P','4','3')#define mp42_FOURCC AVI_FOURCC('m','p','4','2')#define MP42_FOURCC AVI_FOURCC('M','P','4','2')#define MPG4_FOURCC AVI_FOURCC('M','P','G','4')#define MP41_FOURCC AVI_FOURCC('M','P','4','1')static RMuint32 currentFOURCC = 0;static RMint32 currentWidth = 0;static RMint32 currentHeight = 0;static RMint32 currentRate = 0;static RMint32 currentScale = 0;static RMuint32 msmpeg4v3 = 0;static RMuint8 pic_startcode[4];static RMuint32 VOLHdr[256];static RMuint32 VOLHdrLength = 0;static SCANNER_PARAM MP43Scanner;static RMint32 mp43scan_status = 0;static RMuint32 mp43scan_buffer[128*1024/4];#define N_MP43_SCAN_BUFFER 8static RMuint32 mp43pending_buffer[128*1024*N_MP43_SCAN_BUFFER/4];static BufferPool BPscan;static RMint32 IsFOURCCSupported (RMuint32 fourcc){ // iso mpeg-4 if ((fourcc == DIVX_FOURCC) || (fourcc == divx_FOURCC) || (fourcc == DX50_FOURCC) || (fourcc == XVID_FOURCC) || (fourcc == xvid_FOURCC) || (fourcc == XCRD_FOURCC) || (fourcc == RMP4_FOURCC)) { msmpeg4v3 = 0; return 1; } // microsoft mpeg-4 version 3 if ((fourcc == div3_FOURCC) || (fourcc == DIV3_FOURCC) || (fourcc == mp43_FOURCC) || (fourcc == MP43_FOURCC)) { msmpeg4v3 = 1; return 1; } return 0;}#define MY_MIN(X,Y) (((X)<=(Y))?(X):(Y)) // to find the min between two number.// define USE_FATFS_LIB when you want to use the sigma provided// fat file system instead of the linux one// note that the linux ide driver and fat file system must be// disabled in the kernel for the sigma fat file system to operate// correctly//#define USE_FATFS_LIB 1#ifdef USE_FATFS_LIB#include "fatfs.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) { printf ("***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 printf ("***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) { printf ("***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 printf ("***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); printf ("%08lx = fatfs_malloc (%d)\n", (RMuint32)p, (RMint32)s); return p;}static void fatfs_free (void *p){ printf ("fatfs_free (%08lx)\n", (RMuint32)p); return free (p);}static int mountfatfs (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, (RMuint8)-1); 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 0;}#endif// *******************************************// callback funstions requied by the avi demux// *******************************************static RMuint32 my_fopen (RMint8 *filename, void *context){#ifdef USE_FATFS_LIB FATFS_ERROR fatfs_err; RMuint32 fd = (RMuint32)INVALID_HANDLE; fatfs_err = fatfs_fopen ((RMuint8 *)filename, _O_RDONLY, &fd); ASSERT (fatfs_err == FATFS_ERROR_NO_ERROR); DEBUGMSG ((fd == 0), ("fopen (%s) returns %d\n", filename, (int)fd)); if (fatfs_err != FATFS_ERROR_NO_ERROR) return 0; return fd;#else RMuint32 handle; handle = (RMuint32)fopen (filename, "rb"); DEBUGMSG ((handle == 0), ("fopen (%s) returns %d\n", filename, (int)handle)); return handle;#endif}static RMuint32 my_fread (RMuint32 handle, void *buf, RMuint32 length, void *context){#ifdef USE_FATFS_LIB FATFS_ERROR fatfs_err; RMuint32 n; fatfs_err = fatfs_fread (handle, (RMuint8 *)buf, length, &n); if (fatfs_err != FATFS_ERROR_NO_ERROR) return 0; return n;#else return (RMuint32)fread (buf, 1, length, (FILE *)handle);#endif}static RMuint32 my_fseek (RMuint32 handle, RMuint32 pos, void *context){#ifdef USE_FATFS_LIB FATFS_ERROR fatfs_err; fatfs_err = fatfs_fseek (handle, pos, SEEK_SET); if (fatfs_err != FATFS_ERROR_NO_ERROR) return 0; return pos;#else return (RMuint32)fseek ((FILE *)handle, pos, SEEK_SET);#endif}static RMuint32 my_fseek2 (RMuint32 handle, RMuint32 pos, RMuint32 whence, void *context){#ifdef USE_FATFS_LIB FATFS_ERROR fatfs_err; fatfs_err = fatfs_fseek (handle, pos, whence); if (fatfs_err != FATFS_ERROR_NO_ERROR) return 0; return pos;#else return (RMuint32)fseek ((FILE *)handle, pos, whence);#endif}static RMuint32 my_ftell (RMuint32 handle, void *context){#ifdef USE_FATFS_LIB FATFS_ERROR fatfs_err; RMuint32 pos; fatfs_err = fatfs_ftell (handle, &pos); return pos;#else return (RMuint32)ftell ((FILE *)handle);#endif}static RMuint32 my_feof (RMuint32 handle, void *context){#ifdef USE_FATFS_LIB FATFS_ERROR fatfs_err; RMuint32 eof; fatfs_err = fatfs_feof (handle, &eof); return eof;#else return (RMuint32)feof ((FILE *)handle);#endif}static RMuint32 my_fclose (RMuint32 handle, void *context){#ifdef USE_FATFS_LIB fatfs_fclose (handle);#else return (RMuint32)fclose ((FILE *)handle);#endif}static RMuint32 my_addref (RMuint8 *pBuffer, void *context){ BufferPool *BP; if (((int)pBuffer >= (int)avi_buffer) && ((int)pBuffer < ((int)avi_buffer + (AVI_BUFFERSIZE * AVI_NBUFFERS)))) { ASSERT (pAVIDemux); BP = &BPavi; } else { BP = &BPpcm; } 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; if (((int)pBuffer >= (int)avi_buffer) && ((int)pBuffer < ((int)avi_buffer + (AVI_BUFFERSIZE * AVI_NBUFFERS)))) { ASSERT (pAVIDemux); BP = &BPavi; } else { BP = &BPpcm; } int idx = ((int)pBuffer - (int)BP->start_address) / BP->buffers_size; ASSERT (idx >= 0); release_buffer (&(BP->B[idx])); return 0;}static RMuint32 mp3wma_putPCM (RMuint8 *pBuffer, RMuint32 length, void *context){ BufferPool *BP; ASSERT (((int)pBuffer >= (int)pcm_buffer) && ((int)pBuffer < ((int)pcm_buffer + PCM_BUFFERSIZE * PCM_NBUFFERS))); BP = &BPpcm; int idx = ((int)pBuffer - (int)BP->start_address) / BP->buffers_size; addref_buffer (&(BP->B[idx])); DEBUGMSG (0, ("mp3wma_putPCM (%d)\n", idx)); DEBUGMSG (0, ("mp3wma_putPCM: address = 0x%08lx, length = %d, idx = %d\n", (RMuint32)pBuffer, (RMint32)length, idx)); RMuint32 flag = 0; if (aviTimeScale) flag = CTS_AVAILABLE_FLAG; DEBUGMSG (0, ("WritePCM (%d, %d, %d)\n", (RMint32)audioTime, (RMint32)length, aviFrameCounter)); if (pMpegDecoder->WritePCM (pBuffer, length, &(BP->B[idx]), flag, aviTime0, audioTime) == MPEG_DECODER_ERROR_NO_ERROR) { DEBUGMSG (0, ("WritePCM (%d)\n", (RMint32)audioTime)); tbytes += length; ASSERT (BytesPerSecond); audioTime = aviStartupDelay + ((RMint64)tbytes * (RMint64)aviTimeScale / (RMint64)BytesPerSecond); return 0; } release_buffer (&(BP->B[idx])); return 1;}static RMuint32 mp3wma_getPCM (RMuint8 **pBuffer, RMuint32 *plength, void *context){ DEBUGMSG (0, ("mp3wma_getPCM\n")); int idx = find_free_buffer (&BPpcm, 0); if (idx < 0) { return 1; } DEBUGMSG (0, ("mp3wma_getPCM (%d)\n", idx));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -