From 88cf8f642824d493a33a410698f5eae04c8513fe Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Fri, 8 May 2020 15:26:23 +0900 Subject: [PATCH] Format MIDI cleanup Change-Id: I14f8bc26ebe25f94841e9b6fe9b444e89ba0b386 Signed-off-by: Minje Ahn --- formats/ffmpeg/mm_file_format_imelody.c | 4 - formats/ffmpeg/mm_file_format_midi.c | 1388 +++++++++++-------------------- formats/ffmpeg/mm_file_format_mmf.c | 12 - 3 files changed, 500 insertions(+), 904 deletions(-) diff --git a/formats/ffmpeg/mm_file_format_imelody.c b/formats/ffmpeg/mm_file_format_imelody.c index 833b8be..d9e4298 100755 --- a/formats/ffmpeg/mm_file_format_imelody.c +++ b/formats/ffmpeg/mm_file_format_imelody.c @@ -168,10 +168,6 @@ int mmfile_format_read_stream_imy(MMFileFormatContext *formatContext) /*get infomation*/ info = mmfile_format_get_midi_infomation(src2); - if (!info) { - debug_error(DEBUG, "failed to get infomation"); - goto exception; - } formatContext->duration = info->duration; formatContext->videoStreamId = -1; diff --git a/formats/ffmpeg/mm_file_format_midi.c b/formats/ffmpeg/mm_file_format_midi.c index b84774c..f111b45 100755 --- a/formats/ffmpeg/mm_file_format_midi.c +++ b/formats/ffmpeg/mm_file_format_midi.c @@ -42,56 +42,9 @@ enum { #define MMFILE_MXMF_200 "XMF_2.00" #define MMFILE_RMF "IREZ" -#define AvSMW_CNVID_MMF (1) /* SMAF/MA-1/MA-2/MA-3/MA-5 */ -#define AvSMW_CNVID_PHR (2) /* SMAF/Phrase L1/L2 */ -#define AvSMW_CNVID_RMD (3) /* Realtime MIDI */ -#define AvSMW_CNVID_AUD (4) /* SMAF/Audio */ -#define AvSMW_CNVID_MID (5) /* SMF */ -#define AvSMW_CNVID_HVS (9) /* HV Script */ -#define AvSMW_CNVID_WAV (11) /* WAVE */ - -#define AvSMW_SUCCESS (0) /* success */ -#define AvSMW_ERROR (-1) /* error */ -#define AvSMW_ERROR_ARGUMENT (-2) /* error of arguments */ -#define AvSMW_ERROR_RESOURCE_OVER (-3) /* over specified resources */ -#define AvSMW_ERROR_ID (-4) /* error id number */ -#define AvSMW_ERROR_TIMEOUT (-5) /* timeout */ -#define AvSMW_ERROR_SOFTRESET (-6) /* error of soft reset for MA-5 */ - -#define AvSMW_ERROR_FILE (-16) /* file error */ -#define AvSMW_ERROR_CONTENTS_CLASS (-17) /* SMAF Contents Class shows can't play */ -#define AvSMW_ERROR_CONTENTS_TYPE (-18) /* SMAF Contents Type shows can't play */ -#define AvSMW_ERROR_CHUNK_SIZE (-19) /* illegal SAvF Chunk Size value */ -#define AvSMW_ERROR_CHUNK (-20) /* illegal SAvF Track Chunk value */ -#define AvSMW_ERROR_UNMATCHED_TAG (-21) /* unmathced specified TAG */ -#define AvSMW_ERROR_SHORT_LENGTH (-22) /* short sequence */ -#define AvSMW_ERROR_LONG_LENGTH (-23) /* long sequence */ -#define AvSMW_ERROR_UNSUPPORTED (-24) /* unsupported format */ -#define AvSMW_ERROR_NO_INFORMATION (-25) /* no specified information */ -#define AvSMW_ERROR_HV_CONFLICT (-26) /* conflict about HV resource */ - -#define AvSMW_ERROR_SMF_FORMAT (-50) /* invalid format type != 0/1 */ -#define AvSMW_ERROR_SMF_TRACKNUM (-51) /* invalid number of tracks */ -#define AvSMW_ERROR_SMF_TIMEUNIT (-52) /* invalid time unit */ -#define AvSMW_ERROR_SMF_CMD (-53) /* invalid command byte */ - - -#define SINT signed int -#define SINT8 signed char -#define SINT16 signed short -#define SINT32 signed long -#define UINT unsigned int -#define UINT8 unsigned char -#define UINT16 unsigned short -#define UINT32 unsigned long -#define ULONG unsigned long - /*--------------------------------------------------------------------------*/ /* Defines */ /*--------------------------------------------------------------------------*/ -#define SMF_TIMEBASE_SHIFT 2 /* */ -#define SMF_TIMEBASE (1L<uriFileName) { - debug_error(DEBUG, "error: mmfile_format_open_mid\n"); - return MMFILE_FORMAT_FAIL; - } - - res = MMFileFormatIsValidMID(NULL, formatContext->uriFileName, 0); - if (res == 0) { - debug_error(DEBUG, "It is not MIDI file\n"); - return MMFILE_FORMAT_FAIL; - } - - formatContext->ReadStream = mmfile_format_read_stream_mid; - formatContext->ReadFrame = mmfile_format_read_frame_mid; - formatContext->ReadTag = mmfile_format_read_tag_mid; - formatContext->Close = mmfile_format_close_mid; - - formatContext->videoTotalTrackNum = 0; - formatContext->audioTotalTrackNum = 1; - - formatContext->privateFormatData = NULL; - - return MMFILE_FORMAT_SUCCESS; -} - - -int mmfile_format_read_stream_mid(MMFileFormatContext *formatContext) -{ - MMFileFormatStream *audioStream = NULL; - int ret = MMFILE_FORMAT_FAIL; - MIDI_INFO_SIMPLE *info = NULL; - - if (NULL == formatContext) { - debug_error(DEBUG, "error: invalid params\n"); - ret = MMFILE_FORMAT_FAIL; - goto exception; - } - - /*get infomation*/ - info = mmfile_format_get_midi_infomation(formatContext->uriFileName); - if (!info) { - debug_error(DEBUG, "failed to get infomation"); - goto exception; - } - - formatContext->duration = info->duration; - formatContext->videoStreamId = -1; - formatContext->videoTotalTrackNum = 0; - formatContext->audioTotalTrackNum = info->track_num; - formatContext->nbStreams = 1; - - audioStream = g_new0(MMFileFormatStream, 1); - - audioStream->streamType = MMFILE_AUDIO_STREAM; - audioStream->codecId = (info->is_xmf == 1) ? MM_AUDIO_CODEC_MXMF : MM_AUDIO_CODEC_MIDI; - audioStream->bitRate = 0; - audioStream->framePerSec = 0; - audioStream->width = 0; - audioStream->height = 0; - audioStream->nbChannel = 1; - audioStream->samplePerSec = 0; - formatContext->streams[MMFILE_AUDIO_STREAM] = audioStream; - -#ifdef __MMFILE_TEST_MODE__ - mmfile_format_print_contents(formatContext); -#endif - - mmfile_format_free_midi_infomation(info); - return MMFILE_FORMAT_SUCCESS; - -exception: - mmfile_format_free_midi_infomation(info); - mmfile_free(audioStream); - - return ret; -} - - -int mmfile_format_read_tag_mid(MMFileFormatContext *formatContext) -{ - int ret = MMFILE_FORMAT_FAIL; - MIDI_INFO_SIMPLE *info = NULL; - const char *locale = MMFileUtilGetLocale(); - unsigned int tag_len; - unsigned int cnv_len; - - if (NULL == formatContext) { - debug_error(DEBUG, "error: invalid params\n"); - ret = MMFILE_FORMAT_FAIL; - goto exception; - } - - /*get infomation*/ - info = mmfile_format_get_midi_infomation(formatContext->uriFileName); - if (!info) { - debug_error(DEBUG, "failed to get infomation"); - ret = MMFILE_FORMAT_FAIL; - goto exception; - } - - /** - * UTF8 converting. - */ - if (info->title) { - tag_len = strlen(info->title); - cnv_len = 0; - formatContext->title = mmfile_string_convert((const char *)info->title, tag_len, "UTF-8", locale, NULL, (unsigned int *)&cnv_len); - if (formatContext->title == NULL) { - debug_warning(DEBUG, "failed to UTF8 convert.\n"); - formatContext->title = g_strdup(info->title); - } - } - - if (info->copyright) { - tag_len = strlen(info->copyright); - cnv_len = 0; - formatContext->copyright = mmfile_string_convert((const char *)info->copyright, tag_len, "UTF-8", locale, NULL, (unsigned int *)&cnv_len); - if (formatContext->copyright == NULL) { - debug_warning(DEBUG, "failed to UTF8 convert.\n"); - formatContext->copyright = g_strdup(info->copyright); - } - } - - if (info->comment) { - tag_len = strlen(info->comment); - cnv_len = 0; - formatContext->comment = mmfile_string_convert((const char *)info->comment, tag_len, "UTF-8", locale, NULL, (unsigned int *)&cnv_len); - if (formatContext->comment == NULL) { - debug_warning(DEBUG, "failed to UTF8 convert.\n"); - formatContext->comment = g_strdup(info->comment); - } - } - -#ifdef __MMFILE_TEST_MODE__ - mmfile_format_print_contents(formatContext); -#endif - - mmfile_format_free_midi_infomation(info); - return MMFILE_FORMAT_SUCCESS; - -exception: - mmfile_format_free_midi_infomation(info); - return ret; -} - - -int mmfile_format_read_frame_mid(MMFileFormatContext *formatContext, unsigned int timestamp, MMFileFormatFrame *frame) -{ - debug_error(DEBUG, "error: mmfile_format_read_frame_midi, no handling\n"); - - return MMFILE_FORMAT_FAIL; -} - - -int mmfile_format_close_mid(MMFileFormatContext *formatContext) -{ - if (NULL == formatContext) { - debug_error(DEBUG, "error: invalid params\n"); - return MMFILE_FORMAT_FAIL; - } - - mmfile_free(formatContext->streams[MMFILE_AUDIO_STREAM]); - - formatContext->ReadStream = NULL; - formatContext->ReadFrame = NULL; - formatContext->ReadTag = NULL; - formatContext->Close = NULL; - - return MMFILE_FORMAT_SUCCESS; -} - -MIDI_INFO_SIMPLE * -mmfile_format_get_midi_infomation(char *szFileName) -{ - MIDI_INFO_SIMPLE *info = g_new0(MIDI_INFO_SIMPLE, 1); - - __AvGetMidiDuration(szFileName, info); - - return info; -} - -void -mmfile_format_free_midi_infomation(MIDI_INFO_SIMPLE *info) -{ - if (info) { - mmfile_free(info->title); - mmfile_free(info->copyright); - mmfile_free(info->comment); - mmfile_free(info); - } -} - - +static PMIDINFO gPi; /** * internal midi parsing functions */ - -/**************************************************************************** - * __AvGetMidiDuration(char* szFileName) - * - * Desc. - * Load SMF data - * Param - * pbFile pointer to the data - * dFileSize size fo the data - * dMode error check (0:No, 1:Yes, 2:Check, 3:OnlyInfo) - * pfnFunc pointer of rhe callback function - * pvExtArgs Reserved - * Return - * >= 0 : FileID, < 0 : Error code - ****************************************************************************/ -static int -__AvGetMidiDuration(char *szFileName, MIDI_INFO_SIMPLE *info) +static bool __AvParseSkipXmf2Mid(unsigned char *pbFile, unsigned long dFSize, unsigned long *skipSize) { + unsigned long skipVal; - int xmfheaderSkip = 0; - MMFileIOHandle *hFile = NULL; - UINT8 *pbFile = NULL; - UINT8 *pIMYbuf = NULL; - SINT32 dFileSize; - int sdCurrentTime = 0; - /* void* pvExtArgs = "mid"; */ - int readed = 0; - int ret; - int codecType = AV_DEC_AUDIO_MIDI; - int is_xmf = 0; - - if (szFileName == NULL || info == NULL) - return -1; - - /* debug_msg(DEBUG, "URI: %s\n", szFileName); */ - /*open*/ - ret = mmfile_open(&hFile, szFileName, MMFILE_RDONLY); - if (ret == MMFILE_UTIL_FAIL) { - debug_error(DEBUG, "open failed.\n"); - return -1; - } - - /*get file size*/ - dFileSize = mmfile_get_size(hFile); - if (dFileSize <= 0) { - debug_error(DEBUG, "failed to get file size.\n"); - goto _RELEASE_RESOURCE; - } - - /*alloc read buffer*/ - pbFile = g_malloc0(dFileSize + 1); - - /*read data*/ - if ((readed = mmfile_read(hFile, pbFile, dFileSize)) != dFileSize) { - debug_error(DEBUG, "read error. size = %d\n", readed); - goto _RELEASE_RESOURCE; - } - - /*init global workspace*/ - if (__AvMidFile_Initialize()) - goto _RELEASE_RESOURCE; - - /*check format*/ - if (!(memcmp(pbFile, MMFILE_XMF_100, 8)) || - !(memcmp(pbFile, MMFILE_XMF_101, 8)) || - !(memcmp(pbFile, MMFILE_MXMF_200, 8))) { - - is_xmf = 1; - codecType = AV_DEC_AUDIO_XMF; - } else if (!(memcmp(pbFile, MMFILE_RMF, 4))) { - is_xmf = 0; - codecType = AV_DEC_AUDIO_RMF; - } else { - is_xmf = 0; - codecType = AV_DEC_AUDIO_MIDI; - } - - /*set output param*/ - if (codecType == AV_DEC_AUDIO_RMF) { - - info->duration = sdCurrentTime = 0; /*not yet implemented.*/ - info->track_num = 1; /*not yet implemented.*/ - info->is_xmf = is_xmf; - - } else { - - /*get duration. XMF/MIDI*/ - if (codecType == AV_DEC_AUDIO_XMF) { - xmfheaderSkip = __AvParseSkipXmf2Mid(pbFile, dFileSize); - if (xmfheaderSkip == -1) - goto _RELEASE_RESOURCE; - - sdCurrentTime = __AvCheckSizeOfMidFile(pbFile + xmfheaderSkip, dFileSize); - } else { - sdCurrentTime = __AvCheckSizeOfMidFile(pbFile, dFileSize); - } - - if (sdCurrentTime < 0) { - debug_error(DEBUG, "__AvGetMidiDuration: sdResult's error Code!(%d)\n", sdCurrentTime); - goto _RELEASE_RESOURCE; + for (skipVal = 0; skipVal < dFSize - 3; skipVal++) { + if (pbFile[skipVal] == 'M' && + pbFile[skipVal + 1] == 'T' && + pbFile[skipVal + 2] == 'h' && + pbFile[skipVal + 3] == 'd') { + debug_msg(RELEASE, "__AvParseSkipForXMF : MThd Header found!\n"); + *skipSize = skipVal; + return true; } - - if (sdCurrentTime > 0) - sdCurrentTime /= 1000; - - info->duration = sdCurrentTime; - info->track_num = gPi->dNumOfTracks; - info->is_xmf = is_xmf; - - info->title = g_strndup((const char *)gPi->pbTitle, gPi->dSizeTitle); - info->copyright = g_strndup((const char *)gPi->pbCopyright, gPi->dSizeCopyright); - info->comment = g_strndup((const char *)gPi->pbText, gPi->dSizeText); } -_RELEASE_RESOURCE: + debug_error(DEBUG, "MThd header not found\n"); - /*resource release*/ - __AvMidFile_Deinitialize(); - mmfile_close(hFile); - mmfile_free(pbFile); - mmfile_free(pIMYbuf); - - return sdCurrentTime; -} - -static SINT32 -__AvMidFile_Initialize(void) -{ - gpMidInfo = g_new0(MIDGLOBAL, 1); - - gpMidInfo->sdSeqID = -1; /* Sequence ID */ - gpMidInfo->sdFileID = -1; /* File ID */ - gpMidInfo->dEnable = 0; /* 0:disabel */ - gpMidInfo->DataInfo[0].dValid = 0; - gpMidInfo->DataInfo[1].dValid = 0; - - return (AvSMW_SUCCESS); + return false; } -static void -__AvMidFile_Deinitialize(void) +static void __AvMidInitializeOrderList(void) { - mmfile_free(gpMidInfo); + int idx; + + for (idx = 1; idx <= MAX_SMF_TRACKS + 1; idx++) { + gPi->OrderList[idx].pPrev = &gPi->OrderList[idx - 1]; + gPi->OrderList[idx].pNext = &gPi->OrderList[idx + 1]; + gPi->OrderList[idx].dTrack = 0xFF; + gPi->OrderList[idx].dTicks = 0xFFFFFFFFL; + } + gPi->OrderList[0].pPrev = NULL; + gPi->OrderList[0].pNext = &gPi->OrderList[1]; + gPi->OrderList[MAX_SMF_TRACKS + 2].pPrev = &gPi->OrderList[MAX_SMF_TRACKS + 1]; + gPi->OrderList[MAX_SMF_TRACKS + 2].pNext = NULL; + gPi->pTopOrderList = &gPi->OrderList[0]; + gPi->pDoneOrderList = &gPi->OrderList[1]; + gPi->pBottomOrderList = &gPi->OrderList[MAX_SMF_TRACKS + 2]; } - -/*---------------------------------------------------------------------------*/ -/* Functions (internal use only) */ -/*---------------------------------------------------------------------------*/ - -/**************************************************************************** - * __AvMidInitializeOrderList(PMIDINFO pI) - * - * Description: - * Initialize OrderList. - * Param: - * pI ... pointer to the data info - * Return: - * none - ****************************************************************************/ -static void -__AvMidInitializeOrderList(PMIDINFO pI) -{ - int ix2; - - for (ix2 = 1; ix2 <= MAX_SMF_TRACKS + 1; ix2++) { - pI->OrderList[ix2].pPrev = &pI->OrderList[ix2 - 1]; - pI->OrderList[ix2].pNext = &pI->OrderList[ix2 + 1]; - pI->OrderList[ix2].dTrack = 0xFF; - pI->OrderList[ix2].dTicks = 0xFFFFFFFFL; - } - pI->OrderList[0].pPrev = NULL; - pI->OrderList[0].pNext = &pI->OrderList[1]; - pI->OrderList[MAX_SMF_TRACKS + 2].pPrev = &pI->OrderList[MAX_SMF_TRACKS + 1]; - pI->OrderList[MAX_SMF_TRACKS + 2].pNext = NULL; - pI->pTopOrderList = &pI->OrderList[0]; - pI->pDoneOrderList = &pI->OrderList[1]; - pI->pBottomOrderList = &pI->OrderList[MAX_SMF_TRACKS + 2]; -} - - -/**************************************************************************** - * __AvMidSortOrderList(PMIDINFO pI) - * - * Description: - * Sort OrderList. (Ascending order) - * Param: - * pI ... pointer to the data info - * Return: - * none - ****************************************************************************/ -static void -__AvMidSortOrderList(PMIDINFO pI) +static void __AvMidSortOrderList(void) { PORDERLIST pSlot; PORDERLIST pTerget; - pSlot = (pI->pTopOrderList)->pNext; + pSlot = (gPi->pTopOrderList)->pNext; (pSlot->pPrev)->pNext = pSlot->pNext; (pSlot->pNext)->pPrev = pSlot->pPrev; - pSlot->dTicks = ((UINT32)pI->TrackInfo[pSlot->dTrack].sdTicks << 5) + pSlot->dTrack; + pSlot->dTicks = ((unsigned long)gPi->TrackInfo[pSlot->dTrack].sdTicks << 5) + pSlot->dTrack; pTerget = pSlot->pNext; - while (pTerget != pI->pDoneOrderList) { - if (pSlot->dTicks <= pTerget->dTicks) break; + while (pTerget != gPi->pDoneOrderList) { + if (pSlot->dTicks <= pTerget->dTicks) + break; pTerget = pTerget->pNext; } @@ -663,339 +180,257 @@ __AvMidSortOrderList(PMIDINFO pI) pSlot->pNext = pTerget; } - -/**************************************************************************** - * __AvMidInsertOrderList(PMIDINFO pI, UINT32 dTrack, SINT32 sdTicks) - * - * Description: - * Add item to the top of the list. - * Param: - * pI ... pointer to the data info - * Return: - * none - ****************************************************************************/ -static void -__AvMidInsertOrderList(PMIDINFO pI, UINT32 dTrack, SINT32 sdTicks) +static void __AvMidInsertOrderList(unsigned long dTrack, long sdTicks) { PORDERLIST pTerget; - if (pI->dNumOfTracks == 1) return; - if (((pI->dEndFlag >> dTrack) & 0x01) == 0) return; + if (gPi->dNumOfTracks == 1) + return; - pTerget = pI->pDoneOrderList->pNext; - if (pTerget == pI->pBottomOrderList) return; + if (((gPi->dEndFlag >> dTrack) & 0x01) == 0) + return; - pI->pDoneOrderList->pNext = pTerget->pNext; - (pTerget->pNext)->pPrev = pI->pDoneOrderList; + pTerget = gPi->pDoneOrderList->pNext; + if (pTerget == gPi->pBottomOrderList) + return; + + gPi->pDoneOrderList->pNext = pTerget->pNext; + (pTerget->pNext)->pPrev = gPi->pDoneOrderList; pTerget->dTrack = dTrack; - pTerget->dTicks = ((UINT32)sdTicks << 5) + dTrack; - pTerget->pPrev = pI->pTopOrderList; - pTerget->pNext = (pI->pTopOrderList)->pNext; - ((pI->pTopOrderList)->pNext)->pPrev = pTerget; - (pI->pTopOrderList)->pNext = pTerget; + pTerget->dTicks = ((unsigned long)sdTicks << 5) + dTrack; + pTerget->pPrev = gPi->pTopOrderList; + pTerget->pNext = (gPi->pTopOrderList)->pNext; + ((gPi->pTopOrderList)->pNext)->pPrev = pTerget; + (gPi->pTopOrderList)->pNext = pTerget; - __AvMidSortOrderList(pI); + __AvMidSortOrderList(); } - -/**************************************************************************** - * __AvMidRemoveFromOrderList(PMIDINFO pI) - * - * Description: - * delete Item from the top of the list. - * Param: - * pI ... pointer to the data info - * Return: - * none - ****************************************************************************/ -static void -__AvMidRemoveFromOrderList(PMIDINFO pI) +static void __AvMidRemoveFromOrderList(void) { PORDERLIST pSlot; PORDERLIST pTerget; - pSlot = (pI->pTopOrderList)->pNext; + pSlot = (gPi->pTopOrderList)->pNext; (pSlot->pPrev)->pNext = pSlot->pNext; (pSlot->pNext)->pPrev = pSlot->pPrev; - pTerget = pI->pBottomOrderList; + pTerget = gPi->pBottomOrderList; (pTerget->pPrev)->pNext = pSlot; pSlot->pPrev = pTerget->pPrev; pTerget->pPrev = pSlot; pSlot->pNext = pTerget; } - -/**************************************************************************** - * __AvMidGetTrackTime(PMIDINFO pI, UINT32 dTrack) - * - * Description: - * Get the 1st DT from the list. - * Param: - * pI ... pointer to the data info - * bTrack ... #Track - * Return: - * 0 : NoError, < 0 : Error code - ****************************************************************************/ -static SINT32 -__AvMidGetTrackTime(PMIDINFO pI, UINT32 dTrack) +static void __AvMidGetTrackTime(unsigned long dTrack) { - UINT32 dTemp; - SINT32 dTime; - PTRACKINFO pMt; + unsigned long dTemp; + long dTime; + PTRACKINFO pMt; - if (((pI->dEndFlag >> dTrack) & 0x01) == 0) return (-1); + if (((gPi->dEndFlag >> dTrack) & 0x01) == 0) + return; - pMt = &(pI->TrackInfo[dTrack]); + pMt = &(gPi->TrackInfo[dTrack]); dTime = 0; do { if (pMt->dOffset >= pMt->dSize) { - pI->dEndFlag &= ~(1L << dTrack); - return (-1); + gPi->dEndFlag &= ~(1L << dTrack); + return; } - dTemp = (UINT32)pMt->pbBase[pMt->dOffset++]; + dTemp = (unsigned long)pMt->pbBase[pMt->dOffset++]; dTime = (dTime << 7) + (dTemp & 0x7f); } while (dTemp >= 0x80); - /*debug_msg(DEBUG, "dTime is %d\n", dTime); */ - pMt->sdTicks += dTime; - return (0); + pMt->sdTicks += dTime; } - -/**************************************************************************** - * __AvMidUpdateTrackTime(PMIDINFO pI, UINT32 dTrack) - * - * Description: - * Update the 1st DT on the Track and OrderList - * Param: - * pI ... pointer to the data info - * bTrack ... #Track - * Return: - * 0 : NoError, < 0 : Error code - ****************************************************************************/ -static SINT32 -__AvMidUpdateTrackTime(PMIDINFO pI, UINT32 dTrack) +static void __AvMidUpdateTrackTime(unsigned long dTrack) { - UINT32 dTemp; - SINT32 dTime; - PTRACKINFO pMt; + unsigned long dTemp; + long dTime; + PTRACKINFO pMt; - if (pI->dNumOfTracks == 1) { + if (gPi->dNumOfTracks == 1) { /* Single track */ - if (((pI->dEndFlag >> dTrack) & 0x01) == 0) { - return (-1); - } + if (((gPi->dEndFlag >> dTrack) & 0x01) == 0) + return; - pMt = &(pI->TrackInfo[dTrack]); + pMt = &(gPi->TrackInfo[dTrack]); dTime = 0; do { if (pMt->dOffset >= pMt->dSize) { - pI->dEndFlag &= ~(1L << dTrack); - return (-1); + gPi->dEndFlag &= ~(1L << dTrack); + return; } - dTemp = (UINT32)pMt->pbBase[pMt->dOffset++]; + dTemp = (unsigned long)pMt->pbBase[pMt->dOffset++]; dTime = (dTime << 7) + (dTemp & 0x7f); } while (dTemp >= 0x80); pMt->sdTicks += dTime; } else { /* Multi track */ - if (((pI->dEndFlag >> dTrack) & 0x01) == 0) { - __AvMidRemoveFromOrderList(pI); - return (-1); + if (((gPi->dEndFlag >> dTrack) & 0x01) == 0) { + __AvMidRemoveFromOrderList(); + return; } - pMt = &(pI->TrackInfo[dTrack]); + pMt = &(gPi->TrackInfo[dTrack]); dTime = 0; do { if (pMt->dOffset >= pMt->dSize) { - pI->dEndFlag &= ~(1L << dTrack); - __AvMidRemoveFromOrderList(pI); - return (-1); + gPi->dEndFlag &= ~(1L << dTrack); + __AvMidRemoveFromOrderList(); + return; } - dTemp = (UINT32)pMt->pbBase[pMt->dOffset++]; + dTemp = (unsigned long)pMt->pbBase[pMt->dOffset++]; dTime = (dTime << 7) + (dTemp & 0x7f); } while (dTemp >= 0x80); pMt->sdTicks += dTime; - __AvMidSortOrderList(pI); + __AvMidSortOrderList(); } - - return (0); } - -/**************************************************************************** - * __AvMidResetTimeInfo(PMIDINFO pI) - * - * Description: - * Reset time info - * Param: - * pI ... pointer to the data info - * Return: - * none - ****************************************************************************/ -static void -__AvMidResetTimeInfo(PMIDINFO pI) +static void __AvMidResetTimeInfo(void) { - UINT32 sdTrack; - PTRACKINFO pMt; + unsigned long sdTrack; + PTRACKINFO pMt; - pI->dEndFlag = 0; + gPi->dEndFlag = 0; - for (sdTrack = 0; sdTrack < (UINT32)pI->dNumOfTracks; sdTrack++) { - pMt = &(pI->TrackInfo[sdTrack]); + for (sdTrack = 0; sdTrack < gPi->dNumOfTracks; sdTrack++) { + pMt = &(gPi->TrackInfo[sdTrack]); pMt->dSmfCmd = 0; pMt->dOffset = 0; pMt->sdTicks = 0; - if (pMt->dSize > 0) pI->dEndFlag |= (1L << sdTrack); + if (pMt->dSize > 0) + gPi->dEndFlag |= (1L << sdTrack); } - __AvMidInitializeOrderList(pI); + __AvMidInitializeOrderList(); - if ((UINT32)pI->dNumOfTracks > MAX_SMF_TRACKS) { + if (gPi->dNumOfTracks > MAX_SMF_TRACKS) { debug_error(DEBUG, "__AvMidResetTimeInfo: Num of tracks is over MAX track number. !!\n"); return; } - for (sdTrack = 0; sdTrack < (UINT32)pI->dNumOfTracks; sdTrack++) { - __AvMidGetTrackTime(pI, (UINT32)sdTrack); - pMt = &(pI->TrackInfo[sdTrack]); - __AvMidInsertOrderList(pI, (UINT32)sdTrack, pMt->sdTicks); + for (sdTrack = 0; sdTrack < gPi->dNumOfTracks; sdTrack++) { + __AvMidGetTrackTime(sdTrack); + pMt = &(gPi->TrackInfo[sdTrack]); + __AvMidInsertOrderList(sdTrack, pMt->sdTicks); } } - -/**************************************************************************** - * __AvMidGetLeastTimeTrack(PMIDINFO pI) - * - * Description: - * Get the track@LeasetTime - * Param: - * pI ... pointer to the setup storage - * Return: - * 0 : NoError, < 0 : Error - ****************************************************************************/ -static SINT32 -__AvMidGetLeastTimeTrack(PMIDINFO pI) +static bool __AvMidGetLeastTimeTrack(unsigned long *leastTimeTrack) { PORDERLIST pTerget; - pTerget = (pI->pTopOrderList)->pNext; - if (pTerget == pI->pBottomOrderList) return (-1); + pTerget = (gPi->pTopOrderList)->pNext; + if (pTerget == gPi->pBottomOrderList) + return false; - return ((UINT32)pTerget->dTrack); + *leastTimeTrack = pTerget->dTrack; + return true; } - -/**************************************************************************** - * __AvGetSizeOfFileInfo(PMIDINFO pI) - * - * Description: - * Get SMF info from the file - * Param: - * pI ... pointer to the setup storage - * Return: - * 0 : NoError, < 0 : Error - ****************************************************************************/ -static SINT32 -__AvGetSizeOfFileInfo(PMIDINFO pI) +static long __AvGetSizeOfFileInfo(void) { - UINT32 dCmd; - UINT32 dCmd2; - UINT32 dSize; - - UINT32 dTemp; - UINT32 dTime; - UINT32 sdTotalTicks; - SINT32 sdCurrentTime; - SINT32 sdDelta; + unsigned long dCmd; + unsigned long dCmd2; + unsigned long dSize; + + unsigned long dTemp; + unsigned long dTime; + unsigned long sdTotalTicks; + long sdCurrentTime; + long sdDelta; PMIDCHINFO pCh; - UINT32 dCh; + unsigned long dCh; - UINT32 dSetup; /* bit0:beat@0, bit1:tempo@0, bit2:GmOn@0, bit3:tempo@1 */ + unsigned long dSetup; /* bit0:beat@0, bit1:tempo@0, bit2:GmOn@0, bit3:tempo@1 */ PTRACKINFO pMt; - SINT32 sdTr; + unsigned long sdTr; - static UINT32 dBank[16]; - static UINT32 dCurrBank[16]; + static unsigned long dBank[16]; + static unsigned long dCurrBank[16]; - SINT32 sdNonConductorTime; - SINT32 sdNonConductorTicks; - UINT32 dConductorNote; + long sdNonConductorTime; + long sdNonConductorTicks; + unsigned long dConductorNote; dSetup = 0; sdTotalTicks = 0; sdCurrentTime = 0; sdNonConductorTime = 0x7FFFFFFF; sdNonConductorTicks = 0; dConductorNote = 0; - sdDelta = (UINT32)(500 << 10) / pI->dTimeResolution; /* default=0.5sec */ - - pI->pbText = NULL; - pI->dSizeText = 0; - pI->pbTitle = NULL; - pI->dSizeTitle = 0; - pI->pbCopyright = NULL; - pI->dSizeCopyright = 0; - pI->dStart = 0; - pI->dSetupBar = 0; - pI->dVibNoteVoice = 0; + sdDelta = (unsigned long)(500 << 10) / gPi->dTimeResolution; /* default=0.5sec */ + + gPi->pbText = NULL; + gPi->dSizeText = 0; + gPi->pbTitle = NULL; + gPi->dSizeTitle = 0; + gPi->pbCopyright = NULL; + gPi->dSizeCopyright = 0; + gPi->dStart = 0; + gPi->dSetupBar = 0; + gPi->dVibNoteVoice = 0; for (dCh = 0; dCh < NUM_OF_MAPS; dCh++) { for (dTemp = 0; dTemp < 128; dTemp++) { - pI->bVoiceMap[dCh][dTemp] = 0; + gPi->bVoiceMap[dCh][dTemp] = 0; } } - pI->bVoiceMap[MELODY_MAP][0] = 1; /* GM Default Piano */ + gPi->bVoiceMap[MELODY_MAP][0] = 1; /* GM Default Piano */ for (dCh = 0; dCh < 16; dCh++) { dBank[dCh] = 0; dCurrBank[dCh] = 0; - pCh = &pI->ChInfo[dCh]; + pCh = &gPi->ChInfo[dCh]; pCh->dKeyCon = 0; pCh->dVibSync = 0; pCh->dLedSync = 0; } - __AvMidResetTimeInfo(pI); + __AvMidResetTimeInfo(); - if (pI->dSmfFormat != 0) dSetup |= 0x20; + if (gPi->dSmfFormat != 0) + dSetup |= 0x20; - while (pI->dEndFlag != 0) { - if ((pI->dEndFlag == 1) && (sdNonConductorTime == 0x7FFFFFFF)) { + while (gPi->dEndFlag != 0) { + if ((gPi->dEndFlag == 1) && (sdNonConductorTime == 0x7FFFFFFF)) { sdNonConductorTime = sdCurrentTime; sdNonConductorTicks = sdTotalTicks; dConductorNote |= 2; } - if (pI->dNumOfTracks == 1) { + if (gPi->dNumOfTracks == 1) { sdTr = 0; } else { - sdTr = __AvMidGetLeastTimeTrack(pI); - if (sdTr < 0) break; + if (!__AvMidGetLeastTimeTrack(&sdTr)) + break; } - pMt = &(pI->TrackInfo[sdTr]); + pMt = &(gPi->TrackInfo[sdTr]); dTime = pMt->sdTicks - sdTotalTicks; sdCurrentTime += dTime * sdDelta; sdTotalTicks = pMt->sdTicks; - if ((sdCurrentTime < 0) || (sdTotalTicks > 0x07FFFFFFL)) { - return (AvSMW_ERROR_LONG_LENGTH); - } + if ((sdCurrentTime < 0) || (sdTotalTicks > 0x07FFFFFFL)) + return -1; - dCmd = (UINT32)pMt->pbBase[pMt->dOffset++]; + dCmd = (unsigned long)pMt->pbBase[pMt->dOffset++]; if (dCmd < 0xf0) { /*--- MidiMsg ---*/ if (dCmd < 0x80) { dCmd = pMt->dSmfCmd; - if (dCmd < 0x80) return (AvSMW_ERROR_SMF_CMD); + if (dCmd < 0x80) + return -1; pMt->dOffset--; } else { pMt->dSmfCmd = dCmd; @@ -1006,7 +441,8 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) switch (dCmd & 0xf0) { case 0x90: /* NoteOn */ /* Conductor Track Note Check */ - if (sdTr == 0) dConductorNote |= 1; + if (sdTr == 0) + dConductorNote |= 1; switch (dCurrBank[dCh] >> 8) { case 0x79: /* Melody */ @@ -1014,13 +450,13 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) case 0x78: /* Drum */ - pI->bVoiceMap[DRUM_MAP][pMt->pbBase[pMt->dOffset] & 0x7F] = 1; + gPi->bVoiceMap[DRUM_MAP][pMt->pbBase[pMt->dOffset] & 0x7F] = 1; break; default: if (dCh == 9) { /* Unknown: default GM Drum */ - pI->bVoiceMap[DRUM_MAP][pMt->pbBase[pMt->dOffset] & 0x7F] = 1; + gPi->bVoiceMap[DRUM_MAP][pMt->pbBase[pMt->dOffset] & 0x7F] = 1; } } pMt->dOffset += 2; @@ -1031,10 +467,10 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) case 0x79: if (dBank[dCh] != 0x7906) { /* Melody */ - pI->bVoiceMap[MELODY_MAP][pMt->pbBase[pMt->dOffset] & 0x7F] = 1; + gPi->bVoiceMap[MELODY_MAP][pMt->pbBase[pMt->dOffset] & 0x7F] = 1; } else { /* Vibration Note */ - pI->dVibNoteVoice = 1; + gPi->dVibNoteVoice = 1; } break; @@ -1045,7 +481,7 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) default: /* default GM Melody */ if (dCh != 9) { - pI->bVoiceMap[MELODY_MAP][pMt->pbBase[pMt->dOffset] & 0x7F] = 1; + gPi->bVoiceMap[MELODY_MAP][pMt->pbBase[pMt->dOffset] & 0x7F] = 1; } } @@ -1083,7 +519,7 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) pMt->dSmfCmd = 0; dSize = 0; do { - dTemp = (UINT32)pMt->pbBase[pMt->dOffset++]; + dTemp = (unsigned long)pMt->pbBase[pMt->dOffset++]; dSize = (dSize << 7) + (dTemp & 0x7f); } while (dTemp >= 0x80); @@ -1097,7 +533,7 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) dSetup |= 0x04; } } else { - if (pI->dSetupBar == 0) { + if (gPi->dSetupBar == 0) { if ((dSize == 22) && (pMt->pbBase[pMt->dOffset] == 0x43) && (pMt->pbBase[pMt->dOffset + 1] == 0x79) && @@ -1106,7 +542,7 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) (pMt->pbBase[pMt->dOffset + 4] == 0x02)) { /* Channel status */ for (dCh = 0; dCh < 16; dCh++) { - pCh = &pI->ChInfo[dCh]; + pCh = &gPi->ChInfo[dCh]; dTemp = pMt->pbBase[pMt->dOffset + 5 + dCh]; pCh->dKeyCon = (dTemp >> 2) & 0x03; pCh->dVibSync = (dTemp >> 1) & 0x01; @@ -1129,53 +565,50 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) break; case 0xFF: /* Meta */ - dCmd2 = (UINT32)pMt->pbBase[pMt->dOffset++]; /* Meta Command */ + dCmd2 = (unsigned long)pMt->pbBase[pMt->dOffset++]; /* Meta Command */ dSize = 0; /* Size */ do { - dTemp = (UINT32)pMt->pbBase[pMt->dOffset++]; + dTemp = (unsigned long)pMt->pbBase[pMt->dOffset++]; dSize = (dSize << 7) + (dTemp & 0x7f); } while (dTemp >= 0x80); switch (dCmd2) { case 0x01: /* Text */ - if (pI->pbText == NULL) { - pI->pbText = &pMt->pbBase[pMt->dOffset]; - pI->dSizeText = dSize; + if (gPi->pbText == NULL) { + gPi->pbText = &pMt->pbBase[pMt->dOffset]; + gPi->dSizeText = dSize; } break; case 0x02: /* Copyright */ - if (pI->pbCopyright == NULL) { - pI->pbCopyright = &pMt->pbBase[pMt->dOffset]; - pI->dSizeCopyright = dSize; + if (gPi->pbCopyright == NULL) { + gPi->pbCopyright = &pMt->pbBase[pMt->dOffset]; + gPi->dSizeCopyright = dSize; } break; case 0x06: /* Title */ - if (pI->pbTitle == NULL) { - pI->pbTitle = &pMt->pbBase[pMt->dOffset]; - pI->dSizeTitle = dSize; + if (gPi->pbTitle == NULL) { + gPi->pbTitle = &pMt->pbBase[pMt->dOffset]; + gPi->dSizeTitle = dSize; } break; case 0x2f: /* End */ - pI->dEndFlag &= ~(1L << sdTr); + gPi->dEndFlag &= ~(1L << sdTr); break; case 0x51: /* Set Tempo */ switch (dSize) { case 3: case 4: - dTime = ((UINT32)pMt->pbBase[pMt->dOffset] << 16) + - ((UINT32)pMt->pbBase[pMt->dOffset + 1] << 8) + - (UINT32)pMt->pbBase[pMt->dOffset + 2]; + dTime = ((unsigned long)pMt->pbBase[pMt->dOffset] << 16) + + ((unsigned long)pMt->pbBase[pMt->dOffset + 1] << 8) + + (unsigned long)pMt->pbBase[pMt->dOffset + 2]; if ((sdTotalTicks == 0) && (dTime == 250000)) dSetup |= 0x02; - if (sdTotalTicks == (UINT32)pI->dTimeResolution) dSetup |= 0x08; - - /*<== I Think that Below Code is Trash!! and Erase it! (Actually I Don Know ^^) - dTime = (dTime << 7) / 125; */ + if (sdTotalTicks == (unsigned long)gPi->dTimeResolution) dSetup |= 0x08; - sdDelta = (UINT32)(dTime / (pI->dTimeResolution > 0 ? pI->dTimeResolution : 1)); + sdDelta = (unsigned long)(dTime / (gPi->dTimeResolution > 0 ? gPi->dTimeResolution : 1)); break; default: @@ -1198,129 +631,89 @@ __AvGetSizeOfFileInfo(PMIDINFO pI) } } - if ((UINT32)sdTr >= MAX_SMF_TRACKS) { + if (sdTr >= MAX_SMF_TRACKS) { debug_error(DEBUG, "__AvGetSizeOfFileInfo: Num of tracks is over MAX track number. !!\n"); - return AvSMW_ERROR_SMF_CMD; + return -1; } - __AvMidUpdateTrackTime(pI, (UINT32)sdTr); + __AvMidUpdateTrackTime(sdTr); if (dSetup == 0x0F) { dSetup |= 0x10; sdCurrentTime = 0; - pI->dSetupBar = 1; - pI->dStart = pI->TrackInfo[0].dOffset; + gPi->dSetupBar = 1; + gPi->dStart = gPi->TrackInfo[0].dOffset; } } - if ((dConductorNote != 2) || (pI->dSmfFormat == 0)) { - pI->sdTotalTicks = sdTotalTicks; - pI->sdDataEndTime = sdCurrentTime; + if ((dConductorNote != 2) || (gPi->dSmfFormat == 0)) { + gPi->sdTotalTicks = sdTotalTicks; + gPi->sdDataEndTime = sdCurrentTime; } else { - pI->sdTotalTicks = sdNonConductorTicks; - pI->sdDataEndTime = sdNonConductorTime; + gPi->sdTotalTicks = sdNonConductorTicks; + gPi->sdDataEndTime = sdNonConductorTime; } - if (pI->dSetupBar == 0) { + if (gPi->dSetupBar == 0) { for (dCh = 0; dCh < 16; dCh++) { - pCh = &pI->ChInfo[dCh]; + pCh = &gPi->ChInfo[dCh]; pCh->dKeyCon = 0; pCh->dVibSync = 0; pCh->dLedSync = 0; } } - if ((pI->sdDataEndTime >> 10) <= MINIMUM_LENGTH) return (AvSMW_ERROR_SHORT_LENGTH); - - /* debug_msg(DEBUG, "__AvGetSizeOfFileInfo/Done\n"); */ + if ((gPi->sdDataEndTime >> 10) <= MINIMUM_LENGTH) + return -1; - return pI->sdDataEndTime; + return gPi->sdDataEndTime; } - -/**************************************************************************** - * __AvCheckSizeOfMidFile(UINT8* fp, UINT32 dFsize) - * - * Description: - * Check SMF structure - * Param: - * fp ... pointer to the data - * dFsize ... size fo the data - * dMode ... error check (0:No, 1:Yes, 2:ErrorCheck, 3:CNTI) - * Return: - * 0 : NoError, < 0 : Error - ****************************************************************************/ -static SINT32 -__AvCheckSizeOfMidFile(UINT8 *src_fp, UINT32 dFsize) +static long __AvCheckSizeOfMidFile(unsigned char *src_fp, unsigned long dFsize) { - UINT32 dTemp; - UINT32 dSize; - PMIDINFO pI = NULL; - UINT32 dFormat; - UINT32 dNumOfTracks; - UINT32 i; - UINT8 *fp = src_fp; - /* debug_msg(DEBUG, "input param: %p, %d\n", fp, dFsize); */ + unsigned long dTemp; + unsigned long dSize; + unsigned long dFormat; + unsigned long dNumOfTracks; + unsigned long i; + unsigned char *fp = src_fp; + while (dFsize >= 22) { - dTemp = ((UINT32)fp[0] << 24) + ((UINT32)fp[1] << 16) + ((UINT32)fp[2] << 8) + (UINT32)fp[3]; - if (dTemp == 0x4D546864) break; /* 'MThd' */ + dTemp = ((unsigned long)fp[0] << 24) + ((unsigned long)fp[1] << 16) + ((unsigned long)fp[2] << 8) + (unsigned long)fp[3]; + if (dTemp == 0x4D546864) + break; /* 'MThd' */ fp++; dFsize--; } - /* debug_msg(DEBUG, "__AvCheckSizeOfMidFile(): MThd Position is dFsize(%d)\n", dFsize); */ - - if (dFsize < 22) { - debug_error(DEBUG, "__AvCheckSizeOfMidFile Error / Too small size\n"); - return (AvSMW_ERROR_FILE); - } + mm_file_retvm_if_fails(DEBUG, dFsize >= 22, -1); fp += 4; dFsize -= 4; - /*--- Check size ----------------------------------------------------*/ - dTemp = ((UINT32)fp[0] << 24) + ((UINT32)fp[1] << 16) + ((UINT32)fp[2] << 8) + (UINT32)fp[3]; - - if (dTemp != 6) { - debug_error(DEBUG, "__AvCheckSizeOfMidFile Error / Size != 6\n"); - return (AvSMW_ERROR_CHUNK_SIZE); - } + dTemp = ((unsigned long)fp[0] << 24) + ((unsigned long)fp[1] << 16) + ((unsigned long)fp[2] << 8) + (unsigned long)fp[3]; + mm_file_retvm_if_fails(DEBUG, dTemp == 6, -1); fp += 4; dFsize -= 4; - if (gpMidInfo->DataInfo[1].dValid == 1) return (AvSMW_ERROR); - pI = &gpMidInfo->DataInfo[1]; - - /** - * set global val - */ + gPi = g_new0(MIDINFO, 1); /*--- Check format -------------------------------------------------*/ - dFormat = ((UINT32)fp[0] << 8) + (UINT32)fp[1]; - if (dFormat > 1) { - debug_error(DEBUG, "__AvCheckSizeOfMidFile Error/ Not Format 0 or 1\n"); - return (AvSMW_ERROR_SMF_FORMAT); - } + dFormat = ((unsigned long)fp[0] << 8) + (unsigned long)fp[1]; + mm_file_retvm_if_fails(DEBUG, dFormat <= 1, -1); /*--- Check number of tracks ---------------------------------------*/ - dNumOfTracks = (SINT32)((UINT32)fp[2] << 8) + (UINT32)fp[3]; - if (dNumOfTracks == 0) { - debug_error(DEBUG, "__AvCheckSizeOfMidFile Error/ Number of Tracks = 0\n"); - return (AvSMW_ERROR_SMF_TRACKNUM); - } - if ((dFormat == 0) && (dNumOfTracks != 1)) { - debug_error(DEBUG, "__AvCheckSizeOfMidFile Error/ Number of Tracks > 1\n"); - return (AvSMW_ERROR_SMF_TRACKNUM); - } + dNumOfTracks = ((unsigned long)fp[2] << 8) + (unsigned long)fp[3]; + mm_file_retvm_if_fails(DEBUG, dNumOfTracks > 0, -1); + mm_file_retvm_if_fails(DEBUG, dFormat != 0 || dNumOfTracks == 1, -1); - if (dNumOfTracks > MAX_SMF_TRACKS) dNumOfTracks = MAX_SMF_TRACKS; - pI->dNumOfTracks = (UINT8)dNumOfTracks; + gPi->dNumOfTracks = (dNumOfTracks > MAX_SMF_TRACKS) ? MAX_SMF_TRACKS : dNumOfTracks; /*--- Check Time unit --------------------------------------------*/ - dTemp = ((UINT32)fp[4] << 8) + (UINT32)fp[5]; - pI->dTimeResolution = dTemp & 0x7fff; - if (((dTemp & 0x8000) != 0) || (pI->dTimeResolution == 0)) { + dTemp = ((unsigned long)fp[4] << 8) + (unsigned long)fp[5]; + gPi->dTimeResolution = dTemp & 0x7fff; + if (((dTemp & 0x8000) != 0) || (gPi->dTimeResolution == 0)) { debug_error(DEBUG, "__AvCheckSizeOfMidFile Error/ Unknown TimeUnit\n"); - return (AvSMW_ERROR_SMF_TIMEUNIT); + return -1; } fp += 6; dFsize -= 6; @@ -1328,59 +721,278 @@ __AvCheckSizeOfMidFile(UINT8 *src_fp, UINT32 dFsize) for (i = 0; i < dNumOfTracks; i++) { /*--- Check chunk name --------------------------------------------*/ while (dFsize >= 8) { - dTemp = ((UINT32)fp[0] << 24) + ((UINT32)fp[1] << 16) + ((UINT32)fp[2] << 8) + (UINT32)fp[3]; - if (dTemp == 0x4D54726B) break; /* 'MTrk' */ + dTemp = ((unsigned long)fp[0] << 24) + ((unsigned long)fp[1] << 16) + ((unsigned long)fp[2] << 8) + (unsigned long)fp[3]; + if (dTemp == 0x4D54726B) + break; /* 'MTrk' */ fp++; dFsize--; } - if (dFsize < 8) { - debug_error(DEBUG, "__AvCheckSizeOfMidFile Error/ Bad size\n"); - return (AvSMW_ERROR_CHUNK_SIZE); - } + mm_file_retvm_if_fails(DEBUG, dFsize >= 8, -1); /*--- Check size ----------------------------------------------------*/ - dSize = ((UINT32)fp[4] << 24) + ((UINT32)fp[5] << 16) + ((UINT32)fp[6] << 8) + (UINT32)fp[7]; + dSize = ((unsigned long)fp[4] << 24) + ((unsigned long)fp[5] << 16) + ((unsigned long)fp[6] << 8) + (unsigned long)fp[7]; if (dFsize < (dSize + 8)) { debug_error(DEBUG, "__AvCheckSizeOfMidFile Error/ Bad size [%ld] vs [%ld]\n", dFsize, dSize + 22); - return (AvSMW_ERROR_CHUNK_SIZE); + return -1; } - pI->TrackInfo[i].pbBase = &fp[8]; - pI->TrackInfo[i].dSize = dSize; + gPi->TrackInfo[i].pbBase = &fp[8]; + gPi->TrackInfo[i].dSize = dSize; fp += (dSize + 8); dFsize -= (dSize + 8); } - pI->dSmfFormat = dFormat; - - /** - * set global - */ - gPi = pI; + gPi->dSmfFormat = dFormat; - return (__AvGetSizeOfFileInfo(pI)); + return __AvGetSizeOfFileInfo(); } -static int -__AvParseSkipXmf2Mid(UINT8 *pbFile, UINT32 dFSize) +static void __AvGetMidiDuration(char *szFileName, MIDI_INFO_SIMPLE *info) { - UINT32 skipVal = 0, sizeOfpbFile = dFSize; - while (1) { - if (pbFile[skipVal] == 'M' && pbFile[skipVal + 1] == 'T' && pbFile[skipVal + 2] == 'h' && pbFile[skipVal + 3] == 'd') { - debug_msg(RELEASE, "__AvParseSkipForXMF : MThd Header found!\n"); - break; + unsigned long xmfheaderSkip = 0; + MMFileIOHandle *hFile = NULL; + unsigned char *pbFile = NULL; + unsigned char *pIMYbuf = NULL; + long dFileSize; + int sdCurrentTime = 0; + int readed = 0; + int codecType = AV_DEC_AUDIO_MIDI; + int is_xmf = 0; + + if (!szFileName || !info) + return; + + /*open*/ + if (mmfile_open(&hFile, szFileName, MMFILE_RDONLY) != MMFILE_UTIL_SUCCESS) { + debug_error(DEBUG, "failed to open\n"); + return; + } + + /*get file size*/ + dFileSize = mmfile_get_size(hFile); + if (dFileSize <= 0) { + debug_error(DEBUG, "failed to get file size.\n"); + goto _RELEASE_RESOURCE; + } + + /*alloc read buffer*/ + pbFile = g_malloc0(dFileSize + 1); + + /*read data*/ + if ((readed = mmfile_read(hFile, pbFile, dFileSize)) != dFileSize) { + debug_error(DEBUG, "read error. size = %d\n", readed); + goto _RELEASE_RESOURCE; + } + + /*check format*/ + if (!(memcmp(pbFile, MMFILE_XMF_100, 8)) || + !(memcmp(pbFile, MMFILE_XMF_101, 8)) || + !(memcmp(pbFile, MMFILE_MXMF_200, 8))) { + + is_xmf = 1; + codecType = AV_DEC_AUDIO_XMF; + } else if (!(memcmp(pbFile, MMFILE_RMF, 4))) { + is_xmf = 0; + codecType = AV_DEC_AUDIO_RMF; + } else { + is_xmf = 0; + codecType = AV_DEC_AUDIO_MIDI; + } + + /*set output param*/ + if (codecType == AV_DEC_AUDIO_RMF) { + info->duration = sdCurrentTime = 0; /*not yet implemented.*/ + info->track_num = 1; /*not yet implemented.*/ + info->is_xmf = is_xmf; + } else { + /*get duration. XMF/MIDI*/ + if (codecType == AV_DEC_AUDIO_XMF) { + if (!__AvParseSkipXmf2Mid(pbFile, dFileSize, &xmfheaderSkip)) + goto _RELEASE_RESOURCE; + + sdCurrentTime = __AvCheckSizeOfMidFile(pbFile + xmfheaderSkip, dFileSize); } else { - skipVal++; - if (skipVal >= sizeOfpbFile) { - debug_error(DEBUG, "__AvParseSkipForXMF : MThd Header is not found!\n"); - debug_error(DEBUG, "__AvParseSkipForXMF :skipVal(%lu) sizeOfpbFile(%lu) \n", skipVal, sizeOfpbFile); - return -1; - } + sdCurrentTime = __AvCheckSizeOfMidFile(pbFile, dFileSize); + } + + if (sdCurrentTime < 0) { + debug_error(DEBUG, "__AvGetMidiDuration: sdResult's error Code!(%d)\n", sdCurrentTime); + goto _RELEASE_RESOURCE; + } + + if (sdCurrentTime > 0) + sdCurrentTime /= 1000; + + info->duration = sdCurrentTime; + info->track_num = gPi->dNumOfTracks; + info->is_xmf = is_xmf; + + info->title = g_strndup((const char *)gPi->pbTitle, gPi->dSizeTitle); + info->copyright = g_strndup((const char *)gPi->pbCopyright, gPi->dSizeCopyright); + info->comment = g_strndup((const char *)gPi->pbText, gPi->dSizeText); + } + +_RELEASE_RESOURCE: + /*resource release*/ + mmfile_free(gPi); + mmfile_close(hFile); + mmfile_free(pbFile); + mmfile_free(pIMYbuf); +} + +/* mm plugin interface */ +int mmfile_format_read_stream_mid(MMFileFormatContext *formatContext); +int mmfile_format_read_frame_mid(MMFileFormatContext *formatContext, unsigned int timestamp, MMFileFormatFrame *frame); +int mmfile_format_read_tag_mid(MMFileFormatContext *formatContext); +int mmfile_format_close_mid(MMFileFormatContext *formatContext); + + +int mmfile_format_open_mid(MMFileFormatContext *formatContext) +{ + mm_file_retvm_if_fails(DEBUG, formatContext, MMFILE_FORMAT_FAIL); + mm_file_retvm_if_fails(DEBUG, formatContext->uriFileName, MMFILE_FORMAT_FAIL); + mm_file_retvm_if_fails(DEBUG, MMFileFormatIsValidMID(NULL, formatContext->uriFileName, 0), MMFILE_FORMAT_FAIL); + + formatContext->ReadStream = mmfile_format_read_stream_mid; + formatContext->ReadFrame = mmfile_format_read_frame_mid; + formatContext->ReadTag = mmfile_format_read_tag_mid; + formatContext->Close = mmfile_format_close_mid; + + formatContext->videoTotalTrackNum = 0; + formatContext->audioTotalTrackNum = 1; + + formatContext->privateFormatData = NULL; + + return MMFILE_FORMAT_SUCCESS; +} + +int mmfile_format_read_stream_mid(MMFileFormatContext *formatContext) +{ + MMFileFormatStream *audioStream = NULL; + MIDI_INFO_SIMPLE *info = NULL; + + mm_file_retvm_if_fails(DEBUG, formatContext, MMFILE_FORMAT_FAIL); + + /*get infomation*/ + info = mmfile_format_get_midi_infomation(formatContext->uriFileName); + + formatContext->duration = info->duration; + formatContext->videoStreamId = -1; + formatContext->videoTotalTrackNum = 0; + formatContext->audioTotalTrackNum = info->track_num; + formatContext->nbStreams = 1; + + audioStream = g_new0(MMFileFormatStream, 1); + + audioStream->streamType = MMFILE_AUDIO_STREAM; + audioStream->codecId = (info->is_xmf == 1) ? MM_AUDIO_CODEC_MXMF : MM_AUDIO_CODEC_MIDI; + audioStream->bitRate = 0; + audioStream->framePerSec = 0; + audioStream->width = 0; + audioStream->height = 0; + audioStream->nbChannel = 1; + audioStream->samplePerSec = 0; + formatContext->streams[MMFILE_AUDIO_STREAM] = audioStream; + +#ifdef __MMFILE_TEST_MODE__ + mmfile_format_print_contents(formatContext); +#endif + + mmfile_format_free_midi_infomation(info); + return MMFILE_FORMAT_SUCCESS; +} + + +int mmfile_format_read_tag_mid(MMFileFormatContext *formatContext) +{ + MIDI_INFO_SIMPLE *info = NULL; + const char *locale = MMFileUtilGetLocale(); + unsigned int tag_len; + unsigned int cnv_len; + + mm_file_retvm_if_fails(DEBUG, formatContext, MMFILE_FORMAT_FAIL); + + /*get infomation*/ + info = mmfile_format_get_midi_infomation(formatContext->uriFileName); + + /** + * UTF8 converting. + */ + if (info->title) { + tag_len = strlen(info->title); + cnv_len = 0; + formatContext->title = mmfile_string_convert((const char *)info->title, tag_len, "UTF-8", locale, NULL, (unsigned int *)&cnv_len); + if (formatContext->title == NULL) { + debug_warning(DEBUG, "failed to UTF8 convert.\n"); + formatContext->title = g_strdup(info->title); + } + } + + if (info->copyright) { + tag_len = strlen(info->copyright); + cnv_len = 0; + formatContext->copyright = mmfile_string_convert((const char *)info->copyright, tag_len, "UTF-8", locale, NULL, (unsigned int *)&cnv_len); + if (formatContext->copyright == NULL) { + debug_warning(DEBUG, "failed to UTF8 convert.\n"); + formatContext->copyright = g_strdup(info->copyright); + } + } + + if (info->comment) { + tag_len = strlen(info->comment); + cnv_len = 0; + formatContext->comment = mmfile_string_convert((const char *)info->comment, tag_len, "UTF-8", locale, NULL, (unsigned int *)&cnv_len); + if (formatContext->comment == NULL) { + debug_warning(DEBUG, "failed to UTF8 convert.\n"); + formatContext->comment = g_strdup(info->comment); } } - /* debug_msg(DEBUG, "__AvParseSkipForXMF : skip value(%d)\n", skipVal); */ +#ifdef __MMFILE_TEST_MODE__ + mmfile_format_print_contents(formatContext); +#endif + + mmfile_format_free_midi_infomation(info); + return MMFILE_FORMAT_SUCCESS; +} + + +int mmfile_format_read_frame_mid(MMFileFormatContext *formatContext, unsigned int timestamp, MMFileFormatFrame *frame) +{ + debug_error(DEBUG, "error: mmfile_format_read_frame_midi, no handling\n"); + + return MMFILE_FORMAT_FAIL; +} + + +int mmfile_format_close_mid(MMFileFormatContext *formatContext) +{ + mm_file_retvm_if_fails(DEBUG, formatContext, MMFILE_FORMAT_FAIL); + mmfile_free(formatContext->streams[MMFILE_AUDIO_STREAM]); + + formatContext->ReadStream = NULL; + formatContext->ReadFrame = NULL; + formatContext->ReadTag = NULL; + formatContext->Close = NULL; + + return MMFILE_FORMAT_SUCCESS; +} + +MIDI_INFO_SIMPLE * mmfile_format_get_midi_infomation(char *szFileName) +{ + MIDI_INFO_SIMPLE *info = g_new0(MIDI_INFO_SIMPLE, 1); + + __AvGetMidiDuration(szFileName, info); - return skipVal; + return info; } +void mmfile_format_free_midi_infomation(MIDI_INFO_SIMPLE *info) +{ + if (info) { + mmfile_free(info->title); + mmfile_free(info->copyright); + mmfile_free(info->comment); + mmfile_free(info); + } +} diff --git a/formats/ffmpeg/mm_file_format_mmf.c b/formats/ffmpeg/mm_file_format_mmf.c index bdea05d..fdfeea8 100755 --- a/formats/ffmpeg/mm_file_format_mmf.c +++ b/formats/ffmpeg/mm_file_format_mmf.c @@ -550,18 +550,6 @@ static SMAFINFO g_sSmaf_Info; static const unsigned char g_abBitMaskTable1[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; static const unsigned short g_awBitMaskTable2[8] = {0x00FF, 0x01FE, 0x03FC, 0x07F8, 0x0FF0, 0x1FE0, 0x3FC0, 0x7F80}; -/** - * Define. - */ -#define MMF_FILE_MMF_HEADER_LEN 18 - -#define MMF_FILE_MMF_MAGIC_STR_LEN 4 -#define MMF_FILE_MMF_MAGIC_STR "MMMD" - -#define MMF_FILE_MMF_TYPE_POSITION ((char)0x11) - - - /* internals */ static int mmf_file_mmf_get_duration(char *src, int is_xmf); -- 2.7.4