4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include <string.h> /*memcmp*/
24 #include <sys/types.h>
25 #include <sys/stat.h> /*stat*/
28 #include <stdlib.h> /*malloc*/
30 #include "mm_file_debug.h"
31 #include "mm_file_utils.h"
32 #include "mm_file_format_private.h"
33 #include "mm_file_format_audio.h"
34 #include "mm_file_format_mp3.h"
37 #define __MMFILE_NEW_FRAME_FUNC
39 #ifdef __MMFILE_NEW_TAGLIBS__
40 #include "mm_file_format_tags.h"
44 #define AV_MP3_FIND_SYNC_LEN 1024*30
46 #define MIN(a, b) ((a) < (b) ? (a) : (b))
49 static const unsigned char mp3FrameMasking[4] = {0xFF, 0xFE, 0x0C, 0x00};
50 static unsigned char mp3FrameDataValid[4];
52 static const int mp3BitRateTable[2][3][16] = {
53 { {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
54 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
55 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
58 { {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
59 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
60 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
64 static const int mp3SamRateTable[3][3] = {
65 {44100, 48000, 32000},
66 {22050, 24000, 16000},
70 #define IS_VALID_FRAME_MP3(x) \
71 ((((x)[0] & mp3FrameMasking[0]) == mp3FrameDataValid[0]) && \
72 (((x)[1] & mp3FrameMasking[1]) == mp3FrameDataValid[1]) && \
73 (((x)[2] & mp3FrameMasking[2]) == mp3FrameDataValid[2]) && \
74 (((x)[3] & mp3FrameMasking[3]) == mp3FrameDataValid[3]))
78 /* interface functions */
79 int mmfile_format_read_stream_mp3(MMFileFormatContext *formatContext);
80 int mmfile_format_read_frame_mp3(MMFileFormatContext *formatContext, unsigned int timestamp, MMFileFormatFrame *frame);
81 int mmfile_format_read_tag_mp3(MMFileFormatContext *formatContext);
82 int mmfile_format_close_mp3(MMFileFormatContext *formatContext);
85 static int mmf_file_mp3_get_infomation(char *src, AvFileContentInfo *pInfo);
88 int mmfile_format_open_mp3(MMFileFormatContext *formatContext)
90 AvFileContentInfo *privateData = NULL;;
93 debug_fenter(RELEASE);
95 if (NULL == formatContext) {
96 debug_error(DEBUG, "formatContext is NULL\n");
97 return MMFILE_FORMAT_FAIL;
100 if (formatContext->pre_checked == 0) {
101 ret = MMFileFormatIsValidMP3(NULL, formatContext->uriFileName, 5);
103 debug_error(DEBUG, "It is not mp3 file\n");
104 return MMFILE_FORMAT_FAIL;
109 formatContext->ReadStream = mmfile_format_read_stream_mp3;
110 formatContext->ReadFrame = mmfile_format_read_frame_mp3;
111 formatContext->ReadTag = mmfile_format_read_tag_mp3;
112 formatContext->Close = mmfile_format_close_mp3;
114 formatContext->videoTotalTrackNum = 0;
115 formatContext->audioTotalTrackNum = 1;
117 privateData = mmfile_malloc(sizeof(AvFileContentInfo));
119 debug_error(DEBUG, "error: mmfile_malloc MP3 privateData\n");
120 return MMFILE_FORMAT_FAIL;
123 formatContext->privateFormatData = privateData;
125 ret = mmf_file_mp3_get_infomation(formatContext->uriFileName, privateData);
127 debug_error(DEBUG, "error: mmfile_format_read_stream_mp3\n");
131 return MMFILE_FORMAT_SUCCESS;
134 mmfile_format_close_mp3(formatContext);
135 return MMFILE_FORMAT_FAIL;
140 int mmfile_format_read_stream_mp3(MMFileFormatContext *formatContext)
142 AvFileContentInfo *privateData = NULL;
144 debug_fenter(RELEASE);
146 if (!formatContext || !formatContext->privateFormatData) {
147 debug_error(DEBUG, "formatContext is NULL\n");
148 return MMFILE_FORMAT_FAIL;
151 privateData = formatContext->privateFormatData;
153 formatContext->duration = privateData->duration;
154 formatContext->videoTotalTrackNum = 0;
155 formatContext->audioTotalTrackNum = 1;
156 formatContext->nbStreams = 1;
157 formatContext->streams[MMFILE_AUDIO_STREAM] = mmfile_malloc(sizeof(MMFileFormatStream));
158 if (NULL == formatContext->streams[MMFILE_AUDIO_STREAM]) {
159 debug_error(DEBUG, "formatContext->streams[MMFILE_AUDIO_STREAM] is NULL\n");
160 return MMFILE_FORMAT_FAIL;
163 formatContext->streams[MMFILE_AUDIO_STREAM]->streamType = MMFILE_AUDIO_STREAM;
164 formatContext->streams[MMFILE_AUDIO_STREAM]->codecId = MM_AUDIO_CODEC_MP3;
165 formatContext->streams[MMFILE_AUDIO_STREAM]->bitRate = (privateData->bitRate * 1000);
166 formatContext->streams[MMFILE_AUDIO_STREAM]->framePerSec = (privateData->duration == 0 ? 0 : privateData->frameNum / privateData->duration);
167 formatContext->streams[MMFILE_AUDIO_STREAM]->width = 0;
168 formatContext->streams[MMFILE_AUDIO_STREAM]->height = 0;
169 formatContext->streams[MMFILE_AUDIO_STREAM]->nbChannel = privateData->channels;
170 formatContext->streams[MMFILE_AUDIO_STREAM]->samplePerSec = privateData->sampleRate;
172 return MMFILE_FORMAT_SUCCESS;
177 int mmfile_format_read_frame_mp3(MMFileFormatContext *formatContext, unsigned int timestamp, MMFileFormatFrame *frame)
179 return MMFILE_FORMAT_SUCCESS;
183 int mmfile_format_read_tag_mp3(MMFileFormatContext *formatContext)
185 AvFileContentInfo *privateData = NULL;
187 debug_fenter(RELEASE);
189 if (!formatContext || !formatContext->privateFormatData) {
190 debug_error(DEBUG, "formatContext is NULL\n");
191 return MMFILE_FORMAT_FAIL;
194 privateData = formatContext->privateFormatData;
196 formatContext->title = mmfile_strdup(privateData->pTitle);
197 formatContext->artist = mmfile_strdup(privateData->pArtist);
198 formatContext->author = mmfile_strdup(privateData->pAuthor);
199 formatContext->copyright = mmfile_strdup(privateData->pCopyright);
200 formatContext->comment = mmfile_strdup(privateData->pComment);
201 formatContext->album = mmfile_strdup(privateData->pAlbum);
202 formatContext->album_artist = mmfile_strdup(privateData->pAlbum_Artist);
203 formatContext->year = mmfile_strdup(privateData->pYear);
204 formatContext->genre = mmfile_strdup(privateData->pGenre);
205 formatContext->tagTrackNum = mmfile_strdup(privateData->pTrackNum);
206 formatContext->composer = mmfile_strdup(privateData->pComposer);
207 formatContext->classification = mmfile_strdup(privateData->pContentGroup);
208 formatContext->conductor = mmfile_strdup(privateData->pConductor);
209 formatContext->unsyncLyrics = mmfile_strdup(privateData->pUnsyncLyrics);
210 formatContext->syncLyrics = privateData->pSyncLyrics;
211 formatContext->syncLyricsNum = privateData->syncLyricsNum;
212 formatContext->recDate = mmfile_strdup(privateData->pRecDate);
213 formatContext->part_of_set = mmfile_strdup(privateData->pPartOfASet);
215 if (privateData->imageInfo.imageLen > 0) {
216 formatContext->artwork = mmfile_malloc(privateData->imageInfo.imageLen);
217 if (formatContext->artwork) {
218 formatContext->artworkSize = privateData->imageInfo.imageLen;
219 memcpy(formatContext->artwork, privateData->imageInfo.pImageBuf, privateData->imageInfo.imageLen);
220 if (strlen(privateData->imageInfo.imageMIMEType) > 0)
221 formatContext->artworkMime = mmfile_strdup(privateData->imageInfo.imageMIMEType);
222 else if (strlen(privateData->imageInfo.imageExt) > 0) {
223 debug_msg(RELEASE, "ID3 tag V2 File");
224 formatContext->artworkMime = mmfile_strdup(privateData->imageInfo.imageExt);
226 debug_error(DEBUG, "Album art image exist but there is no type information of album art\n");
231 return MMFILE_FORMAT_SUCCESS;
235 int mmfile_format_close_mp3(MMFileFormatContext *formatContext)
237 AvFileContentInfo *privateData = NULL;
240 privateData = formatContext->privateFormatData;
242 mm_file_free_AvFileContentInfo(privateData);
243 mmfile_free(privateData);
244 formatContext->privateFormatData = NULL;
248 return MMFILE_FORMAT_SUCCESS;
252 __AvExtractI4(unsigned char *buf)
268 __AvExtractI2(unsigned char *buf)
281 __AvGetXingHeader(AvXHeadData *headData, unsigned char *buf)
283 int index, headFlags;
284 int hId, hMode, hSrIndex;
285 int mp3SampleRateTable[4] = { 44100, 48000, 32000, 99999 };
287 /* get Xing header data */
290 /* get selected MP3 header data */
291 hId = (buf[1] >> 3) & 1;
292 hSrIndex = (buf[2] >> 2) & 3;
293 hMode = (buf[3] >> 6) & 3;
296 /* determine offset of header */
297 if (hId) { /* mpeg1 */
309 /* There could be 2 attrs in this header : Xing or Info */
311 if (buf[1] != 'i') return 0;
312 if (buf[2] != 'n') return 0;
313 if (buf[3] != 'g') return 0;
314 } else if (buf[0] == 'I') {
315 if (buf[1] != 'n') return 0;
316 if (buf[2] != 'f') return 0;
317 if (buf[3] != 'o') return 0;
325 headData->sampRate = mp3SampleRateTable[hSrIndex];
327 headData->sampRate >>= 1;
329 headFlags = headData->flags = __AvExtractI4(buf); /* get flags */
332 if (headFlags & FRAMES_FLAG) {
333 headData->frames = __AvExtractI4(buf);
336 if (headFlags & BYTES_FLAG) {
337 headData->bytes = __AvExtractI4(buf);
341 if (headFlags & TOC_FLAG) {
342 if (headData->toc != NULL) {
343 for (index = 0; index < 100; index++)
344 headData->toc[index] = buf[index];
349 headData->vbrScale = -1;
350 if (headFlags & VBR_SCALE_FLAG) {
351 headData->vbrScale = __AvExtractI4(buf);
354 debug_msg(RELEASE, "Xing header: sampling-rate:%d, stream-size:%d, frame-number:%d\n",
355 headData->sampRate, headData->bytes, headData->frames);
357 return 1; /* success */
361 __AvGetVBRIHeader(AvVBRIHeadData *headData, unsigned char *buf)
364 int mp3SampleRateTable[4] = { 44100, 48000, 32000, 99999 };
367 /* get selected MP3 header data */
368 hId = (buf[1] >> 3) & 1;
369 hSrIndex = (buf[2] >> 2) & 3;
373 if (buf[0] != 'V') return 0; /* fail */
374 if (buf[1] != 'B') return 0; /* header not found */
375 if (buf[2] != 'R') return 0;
376 if (buf[3] != 'I') return 0;
380 headData->sampRate = mp3SampleRateTable[hSrIndex];
382 headData->sampRate >>= 1;
384 headData->vID = __AvExtractI2(buf); /* get ver ID */
386 headData->delay = __AvExtractI2(buf);
388 headData->qualityIndicator = buf[0];
390 headData->bytes = __AvExtractI4(buf);
392 headData->frames = __AvExtractI4(buf);
394 headData->numOfTOC = __AvExtractI2(buf);
396 headData->vbriScale = __AvExtractI2(buf);
398 headData->sizePerTable = __AvExtractI2(buf);
400 headData->framesPerTable = __AvExtractI2(buf);
402 debug_msg(RELEASE, "Vbri header: sampling-rate:%d, stream-size:%d, frame-number:%d\n", headData->sampRate, headData->bytes, headData->frames);
404 return true; /* success */
407 __AvIsValidHeader(AvFileContentInfo *pInfo, unsigned char *buf)
411 if (VALID_SYNC(buf)) {
412 mp3FrameDataValid[0] = (0xFF) & (mp3FrameMasking[0]);
413 mp3FrameDataValid[1] = (0xE0 | (buf[AV_MP3HDR_VERSION_OFS] & AV_MP3HDR_VERSION_M) | (buf[AV_MP3HDR_LAYER_OFS] & AV_MP3HDR_LAYER_M)) & (mp3FrameMasking[1]);
414 mp3FrameDataValid[2] = (buf[AV_MP3HDR_SAMPLERATE_OFS] & AV_MP3HDR_SAMPLERATE_M) & (mp3FrameMasking[2]);
415 mp3FrameDataValid[3] = (buf[AV_MP3HDR_CHANNEL_OFS] & AV_MP3HDR_CHANNEL_M) & (mp3FrameMasking[3]);
417 debug_msg(RELEASE, "*** [%02x][%02x][%02x][%02x] : [%02x][%02x][%02x][%02x]", buf[0], buf[1], buf[2], buf[3], mp3FrameDataValid[0], mp3FrameDataValid[1], mp3FrameDataValid[2], mp3FrameDataValid[3]);
420 * MPEG Audio Layer I/II/III frame header
421 * from : http://www.mp3-tech.org/programmer/frame_header.html *
423 * AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
425 * A 11 (31-21) Frame sync (all bits must be set)
426 * B 2 (20,19) MPEG Audio version ID
427 * C 2 (18,17) Layer description
428 * D 1 (16) Protection bit
429 * E 4 (15,12) Bitrate index
430 * F 2 (11,10) Sampling rate frequency index
431 * G 1 (9) Padding bit
432 * H 1 (8) Private bit. This one is only informative.
433 * I 2 (7,6) Channel Mode
434 * J 2 (5,4) Mode extension (Only used in Joint stereo)
441 /* Simple check for version, layer, bitrate, samplerate */
442 if ((buf[1] & 0x18) != 0x08 && /* 000XX000 : MPEG Audio version ID, XX=01 - reserved => 00001000(0x08) */
443 (buf[1] & 0x06) != 0x00 && /* 00000XX0 : Layer description, XX=00 - reserved => 00000000(0x00) */
444 (buf[2] & 0xF0) != 0xF0 && /* XXXX0000 : Bitrate index, XX=1111 - bad => 11110000(0xF0) */
445 (buf[2] & 0x0C) != 0x0C) { /* 0000XX00 : Sampling rate frequency index, XX=11 -reserved => 00001100(0x0C) */
448 debug_msg(RELEASE, "=> %s\n", bSync ? "Good!" : "Bad...");
458 __AvParseMp3Header(AvFileContentInfo *pInfo, unsigned char *header)
460 unsigned char result;
462 debug_msg(RELEASE, "### [%02x][%02x][%02x][%02x] ###\n", header[0], header[1], header[2], header[3]);
464 /* 1. Check the version of mp3 */
465 result = header[1] & MASK_MPEG;
468 pInfo->mpegVersion = AV_MPEG_VER_1;
471 pInfo->mpegVersion = AV_MPEG_VER_2;
474 pInfo->mpegVersion = AV_MPEG_VER_25;
481 result = header[1] & MASK_LAYER;
484 pInfo->layer = AV_MP3_LAYER_1;
487 pInfo->layer = AV_MP3_LAYER_2;
490 pInfo->layer = AV_MP3_LAYER_3;
497 result = header[2] >> 4;
498 if (pInfo->mpegVersion == AV_MPEG_VER_1)
499 pInfo->bitRate = mp3BitRateTable[0][pInfo->layer - 1][(int)result] ;
501 pInfo->bitRate = mp3BitRateTable[1][pInfo->layer - 1][(int)result] ;
504 result = (header[2] & MASK_SAMPLERATE) >> 2;
508 pInfo->sampleRate = mp3SamRateTable[pInfo->mpegVersion - 1][(int)result];
511 result = header[3] & MASK_CHANNEL;
513 case MASK_CHANNEL_ST:
514 pInfo->channelIndex = 0;
517 case MASK_CHANNEL_JS:
518 pInfo->channelIndex = 1;
521 case MASK_CHANNEL_DC:
522 pInfo->channelIndex = 2;
525 case MASK_CHANNEL_MN:
526 pInfo->channelIndex = 3;;
534 result = header[2] & MASK_PADDING;
535 if (result == MASK_PADDING)
536 pInfo->bPadding = true;
538 pInfo->bPadding = false;
540 debug_msg(RELEASE, "=> samplerate=%d, bitrate=%d, layer=%d, version=%d, channel=%d, padding=%d", pInfo->sampleRate, pInfo->bitRate, pInfo->layer, pInfo->mpegVersion, pInfo->channels, pInfo->bPadding);
546 __AvParseXingHeader(AvFileContentInfo *pInfo, unsigned char *buf)
549 memset(&data, 0x00, sizeof(AvXHeadData));
552 /* There could be 2 attrs in this header : Xing or Info */
553 if ((pInfo->mpegVersion == AV_MPEG_VER_1) && (pInfo->channels == 2)) {
554 if (buf[36] == 'X') {
555 if (buf[37] != 'i') return false;
556 if (buf[38] != 'n') return false;
557 if (buf[39] != 'g') return false;
558 } else if (buf[36] == 'I') {
559 if (buf[37] != 'n') return false;
560 if (buf[38] != 'f') return false;
561 if (buf[39] != 'o') return false;
565 } else if ((pInfo->mpegVersion == AV_MPEG_VER_2 || pInfo->mpegVersion == AV_MPEG_VER_25) && (pInfo->channels == 1)) {
566 if (buf[13] == 'X') {
567 if (buf[14] != 'i') return false;
568 if (buf[15] != 'n') return false;
569 if (buf[16] != 'g') return false;
570 } else if (buf[13] == 'I') {
571 if (buf[14] != 'n') return false;
572 if (buf[15] != 'f') return false;
573 if (buf[16] != 'o') return false;
578 if (buf[21] == 'X') {
579 if (buf[22] != 'i') return false;
580 if (buf[23] != 'n') return false;
581 if (buf[24] != 'g') return false;
582 } else if (buf[21] == 'I') {
583 if (buf[22] != 'n') return false;
584 if (buf[23] != 'f') return false;
585 if (buf[24] != 'o') return false;
593 data.toc = (unsigned char *)(pInfo->pToc);
595 if (__AvGetXingHeader(&data, (unsigned char *)buf) == 1) { /* VBR. */
596 if (data.sampRate == 0 || data.bytes == 0 || data.frames == 0) {
597 debug_error(DEBUG, "invalid Xing header\n");
601 pInfo->datafileLen = data.bytes;
602 pInfo->frameNum = data.frames;
603 pInfo->frameSize = (int)((float) data.bytes / (float) data.frames) ;
611 __AvParseVBRIHeader(AvFileContentInfo *pInfo, unsigned char *buf)
614 memset(&data, 0x00, sizeof(AvVBRIHeadData));
617 if (buf[36] != 'V') return false;
618 if (buf[37] != 'B') return false;
619 if (buf[38] != 'R') return false;
620 if (buf[39] != 'I') return false;
624 data.toc = (unsigned char *)(pInfo->pToc);
626 if (__AvGetVBRIHeader(&data, (unsigned char *)buf) == 1) { /* VBR. */
627 if (data.sampRate == 0 || data.bytes == 0 || data.frames == 0) {
628 debug_error(DEBUG, "invalid Vbri header\n");
632 pInfo->sampleRate = data.sampRate;
633 pInfo->datafileLen = data.bytes;
634 pInfo->frameNum = data.frames;
635 pInfo->frameSize = (int)((float) data.bytes / (float) data.frames) ;
642 #ifdef __MMFILE_NEW_FRAME_FUNC /* from gst */
644 __AvGetMp3FrameSize(AvFileContentInfo *pInfo)
646 unsigned int frameSize = 0;
650 frameSize = pInfo->bPadding;
651 if (pInfo->bitRate == 0) {
652 if (pInfo->layer == 1) {
654 frameSize += 0/* FIXME: possible_free_framelen*/;
655 pInfo->bitRate = frameSize * pInfo->sampleRate / 48000;
657 frameSize += 0/* FIXME: possible_free_framelen*/;
658 pInfo->bitRate = frameSize * pInfo->sampleRate / ((pInfo->layer == AV_MP3_LAYER_3 && pInfo->mpegVersion != AV_MPEG_VER_1) ? 72000 : 144000);
662 if (pInfo->layer == 1) {
663 frameSize = ((12000 * pInfo->bitRate / pInfo->sampleRate) + frameSize) * 4;
665 frameSize += ((pInfo->layer == AV_MP3_LAYER_3 && pInfo->mpegVersion != AV_MPEG_VER_1) ? 72000 : 144000) * pInfo->bitRate / pInfo->sampleRate;
669 pInfo->frameSize = (int)frameSize;
676 __AvGetBitrate(AvFileContentInfo *pInfo)
681 if (pInfo == NULL || pInfo->bVbr == false)
689 if (pInfo->mpegVersion == AV_MPEG_VER_1) { /* MPEG version 1 */
690 if (pInfo->layer == AV_MP3_LAYER_1) /* Layer 1 */
692 else /* Layer 2, 3 */
694 } else { /* MPEG version 2 */
695 if (pInfo->layer == AV_MP3_LAYER_1) /* Layer 1 */
697 else /* Layer 2, 3 */
701 br = (pInfo->frameSize - padding) * pInfo->sampleRate / factor;
703 pInfo->bitRate = (int) br;
708 static int __AvGetLastID3offset(MMFileIOHandle *fp, unsigned int *offset)
710 #define _MMFILE_MP3_TAGV2_HEADER_LEN 10
711 #define _MMFILE_GET_INT_NUMBER(buff) (int)((((int)(buff)[0]) << 24) | (((int)(buff)[1]) << 16) | (((int)(buff)[2]) << 8) | (((int)(buff)[3])))
713 unsigned char tagHeader[_MMFILE_MP3_TAGV2_HEADER_LEN] = {0, };
714 unsigned int tagInfoSize = 0;
715 unsigned int acc_tagsize = 0;
723 mmfile_seek(fp, 0, MMFILE_SEEK_SET);
727 readed = mmfile_read(fp, tagHeader, _MMFILE_MP3_TAGV2_HEADER_LEN);
728 if (readed != _MMFILE_MP3_TAGV2_HEADER_LEN) {
729 debug_error(DEBUG, "read error occured.\n");
733 if (memcmp(tagHeader, "ID3", 3) == 0) {
734 debug_msg(RELEASE, "'ID3' found.\n");
736 debug_msg(RELEASE, "'ID3' not found.\n");
740 /**@note weak id3v2 tag checking*/
741 if (tagHeader[3] != 0xFF && tagHeader[4] != 0xFF &&
742 (tagHeader[6] & 0x80) == 0 && (tagHeader[7] & 0x80) == 0 &&
743 (tagHeader[8] & 0x80) == 0 && (tagHeader[9] & 0x80) == 0) {
744 debug_msg(RELEASE, "good ID3V2 tag.\n");
746 debug_warning(DEBUG, "It's bad ID3V2 tag.\n");
750 tagVersion = tagHeader[3];
752 if (tagVersion > 4) {
753 debug_msg(RELEASE, "Tag version not supported\n");
757 encSize = _MMFILE_GET_INT_NUMBER(&tagHeader[6]);
758 tagInfoSize = _MMFILE_MP3_TAGV2_HEADER_LEN;
759 tagInfoSize += (((encSize & 0x0000007F) >> 0) | ((encSize & 0x00007F00) >> 1) | ((encSize & 0x007F0000) >> 2) | ((encSize & 0x7F000000) >> 3));
761 /**@note unfortunately, some contents has many id3 tag.*/
762 acc_tagsize += tagInfoSize;
763 debug_msg(RELEASE, "tag size: %u, offset: %u\n", tagInfoSize, acc_tagsize);
765 mmfile_seek(fp, acc_tagsize, MMFILE_SEEK_SET);
766 *offset = acc_tagsize;
767 goto _START_TAG_SEARCH;
776 * This fuction retrieves the start position of header.
777 * Param _pFile [in] Specifies the file pointer of mp3 file.
778 * This function returns the start position of header.
781 __AvFindStartOfMp3Header(MMFileIOHandle *hFile, unsigned char *buf, AvFileContentInfo *pInfo)
783 unsigned int index = 0;
785 unsigned long id3v2TagLen = 0;
786 unsigned char *pHeader = NULL;
787 unsigned long preHeaderGap = 0;
788 unsigned long frameLen = 0;
789 unsigned long nextFrameOff = 0; /* Offset to the start of the next frame */
790 unsigned long nextFrameOffEnd = 0;
791 unsigned long bufLen = 0;
792 bool bFoundSync = false;
793 unsigned long minLen;
795 if (pInfo->fileLen > ((long long)_AV_MP3_HEADER_POSITION_MAX + (long long)pInfo->tagV2Info.tagLen))
796 bufLen = _AV_MP3_HEADER_POSITION_MAX;
798 bufLen = pInfo->fileLen - pInfo->tagV2Info.tagLen;
800 if (IS_ID3V2_TAG(buf)) {
801 if (pInfo->tagV2Info.tagVersion == 0x02) {
802 if (!mm_file_id3tag_parse_v222(pInfo, buf))
803 pInfo->tagV2Info.tagLen = 0;
804 } else if (pInfo->tagV2Info.tagVersion == 0x03) {
805 if (!mm_file_id3tag_parse_v223(pInfo, buf))
806 pInfo->tagV2Info.tagLen = 0;
807 } else if (pInfo->tagV2Info.tagVersion == 0x04) {
808 if (!mm_file_id3tag_parse_v224(pInfo, buf)) /* currently 2.4 ver pased by 2.3 routine */
809 pInfo->tagV2Info.tagLen = 0;
811 debug_msg(RELEASE, "pInfo->tagV2Info.tagVersion(%d)\n", pInfo->tagV2Info.tagVersion);
814 id3v2TagLen = pInfo->tagV2Info.tagLen;
816 debug_msg(RELEASE, "id3v2TagLen(%lu)\n", id3v2TagLen);
819 if (mmfile_seek(hFile, id3v2TagLen, SEEK_SET) < 0) {
820 debug_error(DEBUG, "seek failed.\n");
823 if ((readLen = mmfile_read(hFile, buf, bufLen)) <= 0) {
824 debug_error(DEBUG, "seek failed.\n");
829 if (preHeaderGap == bufLen - 2)
831 if (__AvIsValidHeader(pInfo, buf + preHeaderGap))
838 if (preHeaderGap == bufLen - 2)
840 if (__AvIsValidHeader(pInfo, buf + preHeaderGap))
849 index += preHeaderGap;
850 while (index <= (bufLen - minLen)) {
851 if (buf[0] == 0xff) {
852 if (VALID_SYNC(buf)) {
853 if (bufLen - index > 256) {
854 pHeader = mmfile_malloc(256);
855 if (pHeader == NULL) {
856 debug_error(DEBUG, "malloc failed.\n");
859 strncpy((char *)pHeader, (char *)buf, 256);
861 debug_error(DEBUG, "Header field is not exist\n");
864 if (__AvParseMp3Header(pInfo, pHeader) == false) {
866 mmfile_free(pHeader);
867 debug_warning(DEBUG, "Mp3 parse header failed & index(%d)\n", index);
872 debug_msg(RELEASE, "This header is valid. index(%d)\n", index);
875 if (__AvParseXingHeader(pInfo, pHeader) || __AvParseVBRIHeader(pInfo, pHeader))
876 __AvGetBitrate(pInfo);
879 mmfile_free(pHeader);
883 if (__AvIsValidHeader(pInfo, pHeader)) {
884 mmfile_free(pHeader);
886 __AvGetMp3FrameSize(pInfo);
887 pInfo->datafileLen = pInfo->fileLen - pInfo->headerPos;
888 frameLen = pInfo->frameSize;
890 debug_msg(RELEASE, "<<< frameLen=[%lu] >>> \n", frameLen);
892 #ifndef __MMFILE_NEW_FRAME_FUNC /* FIXME : what purpose to do this? */
893 /* Account for loss of precision in the frame length calculation*/
897 /* Check if the remaining buffer size is large enough to
898 * look for another sync */
899 if ((index + frameLen) < (bufLen - (minLen - 1))) {
900 nextFrameOff = frameLen;
901 nextFrameOffEnd = nextFrameOff + MIN(6, bufLen - (index + frameLen) - (minLen - 1));
903 /* Search the next few bytes for the next sync */
904 while (nextFrameOff < nextFrameOffEnd) {
905 if (VALID_SYNC(buf + nextFrameOff)) {
906 if (IS_VALID_FRAME_MP3(buf + nextFrameOff)) {
913 if (bFoundSync == true)
916 /* Assume that the first sync is valid, since there is not
917 * enough data in the buffer to look for the next sync */
924 debug_warning(DEBUG, "Is not vaild header pHeader\n");
927 mmfile_free(pHeader);
929 debug_warning(RELEASE, "Mp3 file frist byte is 0xff, but not header sync\n");
936 mmfile_free(pHeader);
938 if (mmfile_seek(hFile, 0, SEEK_SET) < 0) {
939 debug_error(DEBUG, "seek error!\n");
942 if (index > (bufLen - minLen)) {
943 debug_warning(DEBUG, "Mp3 file sync is not found : index(%u) bufLen(%lu), minLen(%lu)\n", index, bufLen, minLen);
947 if (bFoundSync == true) {
948 debug_msg(RELEASE, "Mp3 file found a sync Success!\n");
950 debug_msg(RELEASE, "Mp3 file found a sync Failed!\n");
954 return index + id3v2TagLen;
958 * This function retrieves the mp3 information.
959 * Param szFileName [in] Specifies a mp3 file path.
960 * Param _frame [out] Specifies a struct pointer for mp3 information.
961 * This function returns true on success, or false on failure.
963 static int mmf_file_mp3_get_infomation(char *filename, AvFileContentInfo *pInfo)
965 MMFileIOHandle *hFile;
966 unsigned char header[256];
967 unsigned long frameSamples = 0;
968 unsigned char *buf = NULL;
969 unsigned char *v2TagExistCheck = NULL;
970 int readAmount = 0, readedDataLen = 0;
971 unsigned char TagBuff[MP3TAGINFO_SIZE + TAGV1_SEEK_GAP];
972 unsigned char TagV1ID[4] = { 0x54, 0x41, 0x47}; /*TAG */
973 int tagHeaderPos = 0;
975 unsigned int head_offset = 0;
977 debug_fenter(RELEASE);
979 if (pInfo == NULL || filename == NULL)
982 memset(pInfo, 0x00, sizeof(AvFileContentInfo));
984 pInfo->tagV2Info.tagLen = 0;
985 pInfo->headerPos = 0;
989 ret = mmfile_open(&hFile, filename, MMFILE_RDONLY);
990 if (ret == MMFILE_UTIL_FAIL) {
991 debug_error(DEBUG, "open failed.\n");
995 mmfile_seek(hFile, 0L, SEEK_END);
996 pInfo->fileLen = mmfile_tell(hFile);
997 if (pInfo->fileLen <= 0) {
998 debug_error(DEBUG, "file is too small.\n");
1001 mmfile_seek(hFile, 0L, SEEK_SET);
1003 v2TagExistCheck = mmfile_malloc(MP3_TAGv2_HEADER_LEN);
1004 if (v2TagExistCheck == NULL) {
1005 debug_error(DEBUG, "malloc failed.\n");
1009 if (mmfile_read(hFile, v2TagExistCheck, MP3_TAGv2_HEADER_LEN) > 0) {
1010 if (IS_ID3V2_TAG(v2TagExistCheck)) {
1011 if (!(v2TagExistCheck[3] == 0xFF || v2TagExistCheck[4] == 0xFF || v2TagExistCheck[6] >= 0x80 || v2TagExistCheck[7] >= 0x80 || v2TagExistCheck[8] >= 0x80 || v2TagExistCheck[9] >= 0x80)) {
1012 if (!(v2TagExistCheck[3] > 0x04)) {
1013 pInfo->tagV2Info.tagVersion = v2TagExistCheck[3];
1014 pInfo->tagV2Info.tagLen = MP3_TAGv2_HEADER_LEN;
1015 pInfo->tagV2Info.tagLen += (unsigned long)v2TagExistCheck[6] << 21 | (unsigned long)v2TagExistCheck[7] << 14 | (unsigned long)v2TagExistCheck[8] << 7 | (unsigned long)v2TagExistCheck[9];
1016 debug_msg(RELEASE, "pInfo->tagV2Info.tagLen(%d), pInfo->tagV2Info.tagVersion(%d)\n", pInfo->tagV2Info.tagLen, pInfo->tagV2Info.tagVersion);
1018 debug_msg(RELEASE, "tag is a not supported version(%d)\n", v2TagExistCheck[3]);
1021 debug_msg(RELEASE, "tag is a tag data is valid.\n");
1024 debug_msg(RELEASE, "this mp3 file is not included ID3v2 tag info!\n");
1027 debug_error(DEBUG, "v2TagExistCheck value read fail!\n");
1028 mmfile_free(v2TagExistCheck);
1032 mmfile_free(v2TagExistCheck);
1034 if (!(pInfo->fileLen > pInfo->tagV2Info.tagLen))
1035 pInfo->tagV2Info.tagLen = 0;
1037 if (mmfile_seek(hFile, 0L, SEEK_SET) < 0)
1040 debug_msg(RELEASE, "pInfo->fileLen(%lld)\n", pInfo->fileLen);
1042 if (pInfo->fileLen > ((long long)_AV_MP3_HEADER_POSITION_MAX + (long long)pInfo->tagV2Info.tagLen)) {
1043 readAmount = _AV_MP3_HEADER_POSITION_MAX + pInfo->tagV2Info.tagLen;
1044 buf = mmfile_malloc(readAmount);
1046 debug_error(DEBUG, "malloc failed.\n");
1050 while (readAmount > 0) {
1051 if (readAmount >= AV_MP3_HEADER_READ_MAX) {
1052 if ((readedDataLen <= _AV_MP3_HEADER_POSITION_MAX + pInfo->tagV2Info.tagLen) && (mmfile_read(hFile, buf + readedDataLen, AV_MP3_HEADER_READ_MAX) <= 0)) {
1057 debug_msg(RELEASE, "Reading buf readedDataLen(%d) readAmount (%d)\n", readedDataLen, readAmount);
1060 if ((readedDataLen <= _AV_MP3_HEADER_POSITION_MAX + pInfo->tagV2Info.tagLen) && (mmfile_read(hFile, buf + readedDataLen, readAmount) <= 0)) {
1065 debug_msg(RELEASE, "The remained buf readed! readedDataLen(%d) readAmount (%d)\n", readedDataLen, readAmount);
1069 readAmount -= AV_MP3_HEADER_READ_MAX;
1070 readedDataLen += AV_MP3_HEADER_READ_MAX;
1072 if (readAmount <= 0)
1076 buf = mmfile_malloc(pInfo->fileLen);
1081 if (mmfile_read(hFile, buf, pInfo->fileLen) <= 0) {
1088 if (__AvGetLastID3offset(hFile, &head_offset)) {
1089 debug_msg(RELEASE, "search start offset: %u\n", head_offset);
1090 pInfo->tagV2Info.tagLen = head_offset;
1093 pInfo->headerPos = (long) __AvFindStartOfMp3Header(hFile, buf, pInfo);
1095 debug_msg(RELEASE, "Header Pos: %ld\n", pInfo->headerPos);
1099 if (pInfo->headerPos == -1)
1102 if (mmfile_seek(hFile, pInfo->headerPos, SEEK_SET) < 0)
1105 if (mmfile_read(hFile, header, 256) <= 0)
1108 if (__AvParseMp3Header(pInfo, header) == false)
1111 if (__AvParseXingHeader(pInfo, header) || __AvParseVBRIHeader(pInfo, header)) {
1112 __AvGetBitrate(pInfo);
1114 __AvGetMp3FrameSize(pInfo);
1115 pInfo->datafileLen = pInfo->fileLen - pInfo->headerPos;
1116 debug_msg(RELEASE, "Mp3 File FrameSize (%d) pInfo->headerPos(%ld)\n", pInfo->frameSize, pInfo->headerPos);
1119 if (mmfile_seek(hFile, -(MP3TAGINFO_SIZE + TAGV1_SEEK_GAP), SEEK_END) < 0)
1123 pInfo->bV1tagFound = false;
1125 if (mmfile_read(hFile, TagBuff, MP3TAGINFO_SIZE + TAGV1_SEEK_GAP) <= 0)
1128 if ((tagHeaderPos = __AvMemstr(TagBuff, TagV1ID, 3, TAGV1_SEEK_GAP + 5)) >= 0) {
1129 debug_msg(RELEASE, "Mp3 File Tag is existing\n");
1131 pInfo->bV1tagFound = true;
1132 /* In this case, V2 Tag not exist.. So, try to read V1 tag */
1133 if (pInfo->tagV2Info.tagLen == 0 && tagHeaderPos == TAGV1_SEEK_GAP) {
1134 unsigned char TmpBuff[MP3TAGINFO_SIZE] = {0, };
1136 memcpy(TmpBuff, (TagBuff + tagHeaderPos), MP3TAGINFO_SIZE);
1137 if (!mm_file_id3tag_parse_v110(pInfo, TmpBuff))
1142 mm_file_id3tag_restore_content_info(pInfo);
1144 if (pInfo->mpegVersion == 1) {
1145 if (pInfo->layer == 1)
1146 frameSamples = MPEG_1_SIZE_LAYER_1;
1148 frameSamples = MPEG_1_SIZE_LAYER_2_3;
1150 if (pInfo->layer == 1)
1151 frameSamples = MPEG_2_SIZE_LAYER_1;
1153 frameSamples = MPEG_2_SIZE_LAYER_2_3;
1157 unsigned long numOfFrames = 0;
1158 unsigned long long tempduration = 0;
1159 unsigned int tempNumFrames = 0;
1162 numOfFrames = pInfo->frameNum * 10;
1164 numOfFrames = ((pInfo->fileLen - (pInfo->headerPos + (pInfo->bV1tagFound ? MP3TAGINFO_SIZE : 0))) * 10) / pInfo->frameSize;
1166 tempNumFrames = (unsigned int)(numOfFrames / 10);
1168 if ((numOfFrames - tempNumFrames * 10) > 5)
1169 numOfFrames = (numOfFrames / 10) + 1;
1171 numOfFrames = numOfFrames / 10;
1175 tempduration = (unsigned long long)(numOfFrames * 1000);
1177 if (pInfo->mpegVersion == 1) {
1178 if (pInfo->layer == 1)
1179 frameSamples = MPEG_1_SIZE_LAYER_1;
1181 frameSamples = MPEG_1_SIZE_LAYER_2_3;
1183 if (pInfo->layer == 1)
1184 frameSamples = MPEG_2_SIZE_LAYER_1;
1186 frameSamples = MPEG_2_SIZE_LAYER_2_3;
1189 debug_msg(RELEASE, "frameSamples : %d, tempduration : %ld", frameSamples, tempduration);
1191 if (tempduration < (unsigned long long)pInfo->sampleRate) {
1192 tempduration = (tempduration * frameSamples * 10) / pInfo->sampleRate;
1193 tempduration = (tempduration / 10);
1195 tempduration = (tempduration * frameSamples) / pInfo->sampleRate;
1197 pInfo->duration = tempduration;
1200 pInfo->duration = ((double)(frameSamples * 1000) / pInfo->sampleRate) * pInfo->frameNum;
1201 debug_msg(DEBUG, "duration for VBR : %lld", pInfo->duration);
1203 unsigned long long frame_duration = (((unsigned long long)frameSamples * 1000000000) / pInfo->sampleRate / 1000);
1204 int file_size_except_header = pInfo->fileLen - (pInfo->headerPos + (pInfo->bV1tagFound ? MP3TAGINFO_SIZE : 0));
1205 pInfo->duration = ((double)file_size_except_header / (double)pInfo->frameSize) * frame_duration / 1000;
1206 /*pInfo->duration = ((double)file_size_except_header / (double)pInfo->frameSize) * (frameSamples * 1000 / pInfo->sampleRate); */
1207 debug_msg(DEBUG, "duration from new algorithm : %lld", pInfo->duration);
1211 mmfile_close(hFile);
1214 debug_msg(RELEASE, "Mp3 File pInfo->duration (%lld) \n", pInfo->duration);
1215 debug_msg(RELEASE, "** MP3 **\n");
1216 debug_msg(RELEASE, "Version : %u\n", pInfo->mpegVersion);
1217 debug_msg(RELEASE, "Layer : %u\n", pInfo->layer);
1218 debug_msg(RELEASE, "Channel idx: %u\n", pInfo->channelIndex);
1219 debug_msg(RELEASE, "Is VBR : %d\n", (pInfo->bVbr == true ? 1 : 0));
1220 debug_msg(RELEASE, "Bitrate : %u\n", pInfo->bitRate);
1221 debug_msg(RELEASE, "SampleRate : %u\n", pInfo->sampleRate);
1222 debug_msg(RELEASE, "Channels : %u\n", pInfo->channels);
1223 debug_msg(RELEASE, "**** Info #1 ****\n");
1224 debug_msg(RELEASE, "Title : %s\n", pInfo->pTitle);
1225 debug_msg(RELEASE, "Artist : %s\n", pInfo->pArtist);
1226 debug_msg(RELEASE, "Album : %s\n", pInfo->pAlbum);
1227 debug_msg(RELEASE, "Album_Artist: %s\n", pInfo->pAlbum_Artist);
1228 debug_msg(RELEASE, "Year : %s\n", pInfo->pYear);
1229 debug_msg(RELEASE, "Comment : %s\n", pInfo->pComment);
1230 debug_msg(RELEASE, "TrackNum : %s\n", pInfo->pTrackNum);
1231 debug_msg(RELEASE, "Genre : %s\n", pInfo->pGenre);
1232 debug_msg(RELEASE, "**** Info #2 ****\n");
1233 debug_msg(RELEASE, "Author : %s\n", pInfo->pAuthor);
1234 debug_msg(RELEASE, "Copyright : %s\n", pInfo->pCopyright);
1235 debug_msg(RELEASE, "Comment : %s\n", pInfo->pComment);
1236 debug_msg(RELEASE, "Rating : %s\n", pInfo->pRating);
1237 debug_msg(RELEASE, "RecDate : %s\n", pInfo->pRecDate);
1238 debug_msg(RELEASE, "PartOfASet : %s\n", pInfo->pPartOfASet);
1239 debug_msg(RELEASE, "Encoded by : %s\n", pInfo->pEncBy);
1240 debug_msg(RELEASE, "URL : %s\n", pInfo->pURL);
1241 debug_msg(RELEASE, "Ori. Artist : %s\n", pInfo->pOriginArtist);
1242 debug_msg(RELEASE, "Composer : %s\n", pInfo->pComposer);
1243 debug_msg(RELEASE, "Conductor : %s\n", pInfo->pConductor);
1244 debug_msg(RELEASE, "Artwork : mime(%s) addr(%p) size(%d)\n", pInfo->imageInfo.imageMIMEType, pInfo->imageInfo.pImageBuf, pInfo->imageInfo.imageLen);
1245 debug_msg(RELEASE, "UnsyncLyrics : %s\n", pInfo->pUnsyncLyrics);
1246 debug_msg(RELEASE, "SyncLyrics size : %d\n", pInfo->syncLyricsNum);
1251 debug_error(DEBUG, "Error occured!\n");
1252 mmfile_close(hFile);