From 35c1ccb884c7c22aefb25d71222e4e245ad86ef8 Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Wed, 28 Oct 2020 08:42:40 +0900 Subject: [PATCH] Improve function to find ID3v2 last offset Change-Id: I7303450a3783de29c813f3644ce9ccd15cadd1fe Signed-off-by: Minje Ahn --- formats/ffmpeg/mm_file_format_mp3.c | 60 +++++++++++-------------------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/formats/ffmpeg/mm_file_format_mp3.c b/formats/ffmpeg/mm_file_format_mp3.c index 9d78df8..5f6f049 100644 --- a/formats/ffmpeg/mm_file_format_mp3.c +++ b/formats/ffmpeg/mm_file_format_mp3.c @@ -626,43 +626,6 @@ static bool __AvGetID3v2Header(unsigned char *buf, size_t buf_size, AvTagVer2Add return true; } -static bool __AvGetLastID3v2Offset(MMFileIOHandle *fp, int *offset) -{ - unsigned char tagHeader[MP3_TAGv2_HEADER_LEN] = {0, }; - int total_taglen = 0; - int read = 0; - AvTagVer2AdditionalData tagInfo = { 0, }; - - /*init offset*/ - *offset = 0; - - mmfile_seek(fp, 0, MMFILE_SEEK_SET); - -_START_TAG_SEARCH: - - read = mmfile_read(fp, tagHeader, MP3_TAGv2_HEADER_LEN); - if (read != MP3_TAGv2_HEADER_LEN) { - debug_error(DEBUG, "mmfile_read failed"); - return false; - } - - if (!__AvGetID3v2Header(tagHeader, MP3_TAGv2_HEADER_LEN, &tagInfo)) { - debug_msg(RELEASE, "Invalid ID3 header"); - goto search_end; - } - - /**@note unfortunately, some contents has many id3 tag.*/ - total_taglen += tagInfo.tagLen; - debug_msg(RELEASE, "tag size: %u, offset: %u", tagInfo.tagLen, total_taglen); - - mmfile_seek(fp, total_taglen, MMFILE_SEEK_SET); - *offset = total_taglen; - goto _START_TAG_SEARCH; - -search_end: - return true; -} - /* * This fuction retrieves the start position of header. * Param _pFile [in] Specifies the file pointer of mp3 file. @@ -941,8 +904,9 @@ EXCEPTION: static bool __get_stream_info(char *filename, AvFileContentInfo *pInfo) { MMFileIOHandle *hFile; - unsigned char header[256] = {0, }; unsigned long frameSamples = 0; + AvTagVer2AdditionalData tagInfo = { 0, }; + unsigned char header[256] = {0, }; unsigned char *buf = NULL; unsigned long bufLen = 0; @@ -965,12 +929,22 @@ static bool __get_stream_info(char *filename, AvFileContentInfo *pInfo) goto EXCEPTION; } - /* get total length from the beginning of first ID3v2 to the end of last ID3v2 - to get the offset of MP3 data */ - __AvGetLastID3v2Offset(hFile, &pInfo->tagV2Info.tagLen); - debug_msg(RELEASE, "ID3 tag end offset: %u", pInfo->tagV2Info.tagLen); + /* Find the end of ID3v2 */ + while (pInfo->tagV2Info.tagLen < pInfo->fileLen - MP3_TAGv2_HEADER_LEN) { + mmfile_seek(hFile, pInfo->tagV2Info.tagLen, SEEK_SET); - debug_msg(RELEASE, "pInfo->fileLen(%lld)", pInfo->fileLen); + if (mmfile_read(hFile, header, MP3_TAGv2_HEADER_LEN) != MP3_TAGv2_HEADER_LEN) + goto EXCEPTION; + + if (!__AvGetID3v2Header(header, MP3_TAGv2_HEADER_LEN, &tagInfo)) + break; + + /* Some contents may have multiple id3 tags */ + pInfo->tagV2Info.tagLen += tagInfo.tagLen; + debug_msg(RELEASE, "ID3 size[%u] Total size[%u]", tagInfo.tagLen, pInfo->tagV2Info.tagLen); + } + + debug_msg(RELEASE, "ID3 Len[%u] File Len[%lld]", pInfo->tagV2Info.tagLen, pInfo->fileLen); /* read file to find MP3 header */ if (pInfo->fileLen - (long long)pInfo->tagV2Info.tagLen > (long long)_AV_MP3_HEADER_POSITION_MAX) { -- 2.7.4