Reinforce comment and '__AvGetLastID3v2Offset' function 62/224162/4
authorjiyong.min <jiyong.min@samsung.com>
Fri, 7 Feb 2020 00:54:37 +0000 (09:54 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Fri, 7 Feb 2020 01:48:33 +0000 (10:48 +0900)
Change-Id: I8fc9af5c7003715aa8968a56203047a251d68c10

formats/ffmpeg/mm_file_format_mp3.c

index 15a7563..144dce1 100644 (file)
@@ -775,10 +775,10 @@ static bool __AvGetID3v2Header(unsigned char *buf, size_t buf_size, AvTagVer2Add
        return true;
 }
 
-static int __AvGetLastID3v2offset(MMFileIOHandle *fp, unsigned int *offset)
+static int __AvGetLastID3v2Offset(MMFileIOHandle *fp, int *offset)
 {
        unsigned char tagHeader[MP3_TAGv2_HEADER_LEN] = {0, };
-       unsigned int total_taglen = 0;
+       int total_taglen = 0;
        int read = 0;
        AvTagVer2AdditionalData tagInfo = { 0, };
 
@@ -838,15 +838,14 @@ static void __AvGetID3v2Tags(unsigned char *buf, AvFileContentInfo *pInfo)
                return;
        }
 
-       if (pInfo->tagV2Info.tagVersion == 0x02) {
+       if (pInfo->tagV2Info.tagVersion == 0x02)
                mm_file_id3tag_parse_v222(pInfo, buf);
-       } else if (pInfo->tagV2Info.tagVersion == 0x03) {
+       else if (pInfo->tagV2Info.tagVersion == 0x03)
                mm_file_id3tag_parse_v223(pInfo, buf);
-       } else if (pInfo->tagV2Info.tagVersion == 0x04) {
+       else if (pInfo->tagV2Info.tagVersion == 0x04)
                mm_file_id3tag_parse_v224(pInfo, buf); /* currently 2.4 ver pased by 2.3 routine */
-       } else {
+       else
                debug_msg(RELEASE, "Invalid tag version(%d)\n", pInfo->tagV2Info.tagVersion);
-       }
 }
 
 /*
@@ -992,6 +991,23 @@ __AvFindStartOfMp3Header(unsigned char *buf, unsigned long bufLen, AvFileContent
 }
 
 /*
+ * MP3 file structure
+ *
+ * -----------------------------------------------------
+ * |  Start Offset: 0                                  |
+ * |  ID3(ID3v2)                                       |
+ * |  Length: variable                                 |
+ * -----------------------------------------------------
+ * |  Start Offset: at the end of Last ID3             |
+ * |  MP3(Mpeg audio)                                  |
+ * |  Length: variable                                 |
+ * -----------------------------------------------------
+ * |  Start Offset: -(128byte + a) at the end of file  |
+ * |  TAG(ID3v1)                                       |
+ * |  Length: Fixed 128byte                            |
+ * -----------------------------------------------------
+ */
+/*
  *     This function retrieves the ID3 tag information.
  *     Param   filename [in] Specifies a mp3 file path.
  *     Param   pInfo [out]     Specifies a struct pointer for ID3 tag information.
@@ -1005,7 +1021,6 @@ static int mmf_file_mp3_get_tag_info(char *filename, AvFileContentInfo *pInfo)
        unsigned char   TagBuff[MP3TAGINFO_SIZE + TAGV1_SEEK_GAP];
        int             tagHeaderPos = 0;
        int ret = 0;
-       unsigned int head_offset = 0;
 
        debug_fenter(RELEASE);
 
@@ -1030,19 +1045,20 @@ static int mmf_file_mp3_get_tag_info(char *filename, AvFileContentInfo *pInfo)
                goto EXCEPTION;
        }
 
+       /* read file to check ID3v2 header */
        v2TagExistCheck = mmfile_malloc(MP3_TAGv2_HEADER_LEN);
        if (!v2TagExistCheck) {
                debug_error(DEBUG, "malloc failed.\n");
                goto EXCEPTION;
        }
 
-       /* check ID3v2 header */
        if (mmfile_read(hFile, v2TagExistCheck, MP3_TAGv2_HEADER_LEN) != MP3_TAGv2_HEADER_LEN) {
                debug_error(DEBUG, "v2TagExistCheck value read fail!\n");
                mmfile_free(v2TagExistCheck);
                goto EXCEPTION;
        }
 
+       /* check ID3v2 header */
        __AvGetID3v2Header(v2TagExistCheck, MP3_TAGv2_HEADER_LEN, &pInfo->tagV2Info);
        mmfile_free(v2TagExistCheck);
 
@@ -1051,7 +1067,7 @@ static int mmf_file_mp3_get_tag_info(char *filename, AvFileContentInfo *pInfo)
 
        debug_msg(RELEASE, "pInfo->fileLen(%lld)\n", pInfo->fileLen);
 
-       /* read file to get ID3v2 */
+       /* read file to get ID3v2 tags from offset 0 */
        if (pInfo->fileLen <= (long long)pInfo->tagV2Info.tagLen)
                pInfo->tagV2Info.tagLen = pInfo->fileLen;
 
@@ -1068,22 +1084,14 @@ static int mmf_file_mp3_get_tag_info(char *filename, AvFileContentInfo *pInfo)
                goto EXCEPTION;
        }
 
-       /* Is this needed? */
-       if (__AvGetLastID3v2offset(hFile, &head_offset)) {
-               debug_msg(RELEASE, "search start offset: %u\n", head_offset);
-               pInfo->tagV2Info.tagLen = head_offset;
-       }
-
        /* get ID3v2 information */
        __AvGetID3v2Tags(buf, pInfo);
 
-       /* relocate file to read TAG(ID3v1) information */
+       /* locate fp to read TAG(ID3v1) information */
        if (mmfile_seek(hFile, -(MP3TAGINFO_SIZE + TAGV1_SEEK_GAP), SEEK_END) < 0)
                goto EXCEPTION;
 
-
        pInfo->bV1tagFound = false;
-
        /* read with TAG(ID3v1) length */
        if (mmfile_read(hFile, TagBuff, MP3TAGINFO_SIZE + TAGV1_SEEK_GAP) != MP3TAGINFO_SIZE + TAGV1_SEEK_GAP)
                goto EXCEPTION;
@@ -1143,11 +1151,9 @@ static int mmf_file_mp3_get_stream_info(char *filename, AvFileContentInfo *pInfo
        unsigned char   header[256];
        unsigned long   frameSamples = 0;
        unsigned char   *buf = NULL;
-       unsigned char   *v2TagExistCheck = NULL;
+       unsigned long   bufLen = 0;
        unsigned char   TagBuff[MP3TAGINFO_SIZE + TAGV1_SEEK_GAP];
        int ret = 0;
-       unsigned int head_offset = 0;
-       unsigned long bufLen = 0;
 
        debug_fenter(RELEASE);
 
@@ -1172,39 +1178,30 @@ static int mmf_file_mp3_get_stream_info(char *filename, AvFileContentInfo *pInfo
                goto EXCEPTION;
        }
 
-       v2TagExistCheck = mmfile_malloc(MP3_TAGv2_HEADER_LEN);
-       if (!v2TagExistCheck) {
-               debug_error(DEBUG, "malloc failed.\n");
-               goto EXCEPTION;
-       }
-
-       if (mmfile_read(hFile, v2TagExistCheck, MP3_TAGv2_HEADER_LEN) != MP3_TAGv2_HEADER_LEN) {
-               debug_error(DEBUG, "v2TagExistCheck value read fail!\n");
-               mmfile_free(v2TagExistCheck);
-               goto EXCEPTION;
-       }
-
-       __AvGetID3v2Header(v2TagExistCheck, MP3_TAGv2_HEADER_LEN, &pInfo->tagV2Info);
-       mmfile_free(v2TagExistCheck);
-
-       if (mmfile_seek(hFile, 0L, SEEK_SET) < 0)
-               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\n", pInfo->tagV2Info.tagLen);
 
        debug_msg(RELEASE, "pInfo->fileLen(%lld)\n", pInfo->fileLen);
 
-       /* read file to get MP3 stream info */
+       /* read file to find MP3 header */
        if (pInfo->fileLen - (long long)pInfo->tagV2Info.tagLen > (long long)_AV_MP3_HEADER_POSITION_MAX) {
+               /* set offset at the end of ID3v2 */
                if (mmfile_seek(hFile, pInfo->tagV2Info.tagLen, SEEK_SET) < 0)
                        goto EXCEPTION;
 
                bufLen = _AV_MP3_HEADER_POSITION_MAX;
        } else if (pInfo->fileLen - (long long)pInfo->tagV2Info.tagLen > 0) {
+               /* set offset at the end of ID3v2 */
                if (mmfile_seek(hFile, pInfo->tagV2Info.tagLen, SEEK_SET) < 0)
                        goto EXCEPTION;
 
                bufLen = pInfo->fileLen - (long long)pInfo->tagV2Info.tagLen;
        } else {
                /* if taglen is invlaid, check whole file to get MP3 info */
+               if (mmfile_seek(hFile, 0L, SEEK_SET) < 0)
+                       goto EXCEPTION;
                pInfo->tagV2Info.tagLen = 0;
                bufLen = pInfo->fileLen;
        }
@@ -1221,12 +1218,7 @@ static int mmf_file_mp3_get_stream_info(char *filename, AvFileContentInfo *pInfo
                goto EXCEPTION;
        }
 
-       if (__AvGetLastID3v2offset(hFile, &head_offset)) {
-               debug_msg(RELEASE, "search start offset: %u\n", head_offset);
-               pInfo->tagV2Info.tagLen = head_offset;
-       }
-
-       /* get position of MP3 header */
+       /* get offset of MP3 header */
        pInfo->headerPos = (long) __AvFindStartOfMp3Header(buf, bufLen, pInfo);
 
        debug_msg(RELEASE, "Header Pos: %ld\n", pInfo->headerPos);
@@ -1278,6 +1270,7 @@ static int mmf_file_mp3_get_stream_info(char *filename, AvFileContentInfo *pInfo
        if (__AvGetID3v1Header(TagBuff, MP3TAGINFO_SIZE + TAGV1_SEEK_GAP, NULL))
                pInfo->bV1tagFound = true;
 
+       /* get MP3 duration */
        if (pInfo->bVbr) {
                pInfo->duration = ((double)(frameSamples * 1000) / pInfo->sampleRate) * pInfo->frameNum;
                debug_msg(DEBUG, "duration for VBR : %lld", pInfo->duration);