From d7123619a6dc6814ff99e1a44edf6a961714a0be Mon Sep 17 00:00:00 2001 From: "jiyong.min" Date: Wed, 5 Feb 2020 16:36:17 +0900 Subject: [PATCH] [code cleanup] Improved code readability for getting ID3Tag Change-Id: I1be3ab6b95a7e0c75e349b6de365769ef8e3944e --- formats/ffmpeg/mm_file_format_mp3.c | 104 ++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 34 deletions(-) diff --git a/formats/ffmpeg/mm_file_format_mp3.c b/formats/ffmpeg/mm_file_format_mp3.c index 2e37726..4b8fccd 100644 --- a/formats/ffmpeg/mm_file_format_mp3.c +++ b/formats/ffmpeg/mm_file_format_mp3.c @@ -701,21 +701,38 @@ __AvGetBitrate(AvFileContentInfo *pInfo) return true; } -static bool __AvGetID3Header(unsigned char *buf, size_t buf_size, AvTagVer2AdditionalData *id3v2info) +static bool __AvGetID3v1Header(unsigned char *buf, size_t buf_size, int *offset) +{ + unsigned char TagV1ID[4] = { 0x54, 0x41, 0x47}; /* TAG */ + int id3v1_offset = 0; + + id3v1_offset = __AvMemstr(buf, TagV1ID, 3, TAGV1_SEEK_GAP + 5); + if (id3v1_offset < 0) { + debug_msg(RELEASE, "ID3v1 is not existing"); + return false; + } + + *offset = id3v1_offset; + + debug_msg(RELEASE, "ID3v1 is existing"); + return true; +} + +static bool __AvGetID3v2Header(unsigned char *buf, size_t buf_size, AvTagVer2AdditionalData *id3v2info) { if (!buf || buf_size < MP3_TAGv2_HEADER_LEN || !id3v2info) { debug_error(DEBUG, "Invalid parameters!"); return false; } - if (!IS_ID3V2_TAG(buf)) { - debug_error(RELEASE, "Invalid ID3 identifier !"); + if (!IS_ID3V2_TAG(buf)) { /* ID3 */ + debug_error(RELEASE, "Invalid ID3v2 identifier !"); return false; } /* weak id3v2 tag check */ if (buf[3] == 0xFF || buf[4] == 0xFF || buf[6] >= 0x80 || buf[7] >= 0x80 || buf[8] >= 0x80 || buf[9] >= 0x80) { - debug_error(RELEASE, "Invalid ID3 base header!"); + debug_error(RELEASE, "Invalid ID3v2 base header!"); return false; } @@ -732,7 +749,7 @@ static bool __AvGetID3Header(unsigned char *buf, size_t buf_size, AvTagVer2Addit return true; } -static int __AvGetLastID3offset(MMFileIOHandle *fp, unsigned int *offset) +static int __AvGetLastID3v2offset(MMFileIOHandle *fp, unsigned int *offset) { unsigned char tagHeader[MP3_TAGv2_HEADER_LEN] = {0, }; unsigned int total_taglen = 0; @@ -752,7 +769,7 @@ _START_TAG_SEARCH: return 0; } - if (!__AvGetID3Header(tagHeader, MP3_TAGv2_HEADER_LEN, &tagInfo)) { + if (!__AvGetID3v2Header(tagHeader, MP3_TAGv2_HEADER_LEN, &tagInfo)) { debug_msg(RELEASE, "Invalid ID3 header\n"); goto search_end; } @@ -769,6 +786,46 @@ search_end: return 1; } +static bool __AvGetID3v1Tags(unsigned char *buf, int offset, AvFileContentInfo *pInfo) +{ + unsigned char TmpBuff[MP3TAGINFO_SIZE] = {0, }; + + if (!buf || !pInfo || offset < 0) { + debug_error(DEBUG, "Invalid parameters!"); + return true; + } + + if (pInfo->tagV2Info.tagLen != 0 || offset != TAGV1_SEEK_GAP) { + debug_msg(RELEASE, "No need to check id3v1"); + return true; + } + + memcpy(TmpBuff, buf, MP3TAGINFO_SIZE); + + return mm_file_id3tag_parse_v110(pInfo, TmpBuff); +} + +static void __AvGetID3v2Tags(unsigned char *buf, AvFileContentInfo *pInfo) +{ + if (!buf || !pInfo || pInfo->tagV2Info.tagLen == 0) { + debug_error(DEBUG, "Invalid parameters!"); + return; + } + + if (pInfo->tagV2Info.tagVersion == 0x02) { + if (!mm_file_id3tag_parse_v222(pInfo, buf)) + pInfo->tagV2Info.tagLen = 0; + } else if (pInfo->tagV2Info.tagVersion == 0x03) { + if (!mm_file_id3tag_parse_v223(pInfo, buf)) + pInfo->tagV2Info.tagLen = 0; + } else if (pInfo->tagV2Info.tagVersion == 0x04) { + if (!mm_file_id3tag_parse_v224(pInfo, buf)) /* currently 2.4 ver pased by 2.3 routine */ + pInfo->tagV2Info.tagLen = 0; + } else { + debug_msg(RELEASE, "Invalid tag version(%d)\n", pInfo->tagV2Info.tagVersion); + } +} + /* * This fuction retrieves the start position of header. * Param _pFile [in] Specifies the file pointer of mp3 file. @@ -795,19 +852,6 @@ __AvFindStartOfMp3Header(MMFileIOHandle *hFile, unsigned char *buf, AvFileConte bufLen = pInfo->fileLen - pInfo->tagV2Info.tagLen; if (IS_ID3V2_TAG(buf)) { - if (pInfo->tagV2Info.tagVersion == 0x02) { - if (!mm_file_id3tag_parse_v222(pInfo, buf)) - pInfo->tagV2Info.tagLen = 0; - } else if (pInfo->tagV2Info.tagVersion == 0x03) { - if (!mm_file_id3tag_parse_v223(pInfo, buf)) - pInfo->tagV2Info.tagLen = 0; - } else if (pInfo->tagV2Info.tagVersion == 0x04) { - if (!mm_file_id3tag_parse_v224(pInfo, buf)) /* currently 2.4 ver pased by 2.3 routine */ - pInfo->tagV2Info.tagLen = 0; - } else { - debug_msg(RELEASE, "pInfo->tagV2Info.tagVersion(%d)\n", pInfo->tagV2Info.tagVersion); - } - id3v2TagLen = pInfo->tagV2Info.tagLen; debug_msg(RELEASE, "id3v2TagLen(%lu)\n", id3v2TagLen); @@ -966,7 +1010,6 @@ static int mmf_file_mp3_get_infomation(char *filename, AvFileContentInfo *pInfo) unsigned char *v2TagExistCheck = NULL; int readAmount = 0, readedDataLen = 0; unsigned char TagBuff[MP3TAGINFO_SIZE + TAGV1_SEEK_GAP]; - unsigned char TagV1ID[4] = { 0x54, 0x41, 0x47}; /*TAG */ int tagHeaderPos = 0; int ret = 0; unsigned int head_offset = 0; @@ -1004,7 +1047,7 @@ static int mmf_file_mp3_get_infomation(char *filename, AvFileContentInfo *pInfo) } if (mmfile_read(hFile, v2TagExistCheck, MP3_TAGv2_HEADER_LEN) > 0) { - __AvGetID3Header(v2TagExistCheck, MP3_TAGv2_HEADER_LEN, &pInfo->tagV2Info); + __AvGetID3v2Header(v2TagExistCheck, MP3_TAGv2_HEADER_LEN, &pInfo->tagV2Info); mmfile_free(v2TagExistCheck); } else { debug_error(DEBUG, "v2TagExistCheck value read fail!\n"); @@ -1065,12 +1108,13 @@ static int mmf_file_mp3_get_infomation(char *filename, AvFileContentInfo *pInfo) } } - - if (__AvGetLastID3offset(hFile, &head_offset)) { + if (__AvGetLastID3v2offset(hFile, &head_offset)) { debug_msg(RELEASE, "search start offset: %u\n", head_offset); pInfo->tagV2Info.tagLen = head_offset; } + __AvGetID3v2Tags(buf, pInfo); + pInfo->headerPos = (long) __AvFindStartOfMp3Header(hFile, buf, pInfo); debug_msg(RELEASE, "Header Pos: %ld\n", pInfo->headerPos); @@ -1106,18 +1150,10 @@ static int mmf_file_mp3_get_infomation(char *filename, AvFileContentInfo *pInfo) if (mmfile_read(hFile, TagBuff, MP3TAGINFO_SIZE + TAGV1_SEEK_GAP) <= 0) goto EXCEPTION; - if ((tagHeaderPos = __AvMemstr(TagBuff, TagV1ID, 3, TAGV1_SEEK_GAP + 5)) >= 0) { - debug_msg(RELEASE, "Mp3 File Tag is existing\n"); - + if (__AvGetID3v1Header(TagBuff, MP3TAGINFO_SIZE + TAGV1_SEEK_GAP, &tagHeaderPos)) { pInfo->bV1tagFound = true; - /* In this case, V2 Tag not exist.. So, try to read V1 tag */ - if (pInfo->tagV2Info.tagLen == 0 && tagHeaderPos == TAGV1_SEEK_GAP) { - unsigned char TmpBuff[MP3TAGINFO_SIZE] = {0, }; - - memcpy(TmpBuff, (TagBuff + tagHeaderPos), MP3TAGINFO_SIZE); - if (!mm_file_id3tag_parse_v110(pInfo, TmpBuff)) - goto EXCEPTION; - } + if (!__AvGetID3v1Tags((TagBuff + tagHeaderPos), tagHeaderPos, pInfo)) + goto EXCEPTION; } mm_file_id3tag_restore_content_info(pInfo); -- 2.7.4