mp3_decode.c 17.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
#include "os_config.h"
#include "c_def.h"
#include "debug.h"
#include "oem.h"


#ifdef MP3_DECODE_ENABLE

#include "hw_da_pp.h"

#include "app_sdram.h"
#include "app_main.h"
#include "app_media.h"
#include "app_dac.h"
#include "app_cmd.h"
#include "mp3_decode.h"
#include "audio_com.h"
#include "audio_dec.h"
#include "codec.h"
#include "tools.h"
#include "id3v2.h"

#include "xa_error_standards.h"
//#include "xa_error_handler.h"
#include "optek_link.h"
#include "fifo.h"
#ifdef TIME_SCALE_CODE
#include "tscale.h"
int tscaleSpeed;
#endif


//#undef TIME_SCALE_CODE


void mp3_scr_init (void)
{
}

void mp3_decode_buf_init (void)
{
}

int xa_mp3_dec_init (void);

void app_dec_mp3_init (void)
{
#ifdef MP3_DECODE_ENABLE
	I32 iResult;

	DBG_Printf ("%s\n\r", __func__);

	wRequestDecodeFrameExit = FALSE;

	audio_out_mode = AUDIO_OUT_L_R;
	da_pp_channel_setting(MI2S_OUTPUT_CHANNEL_DEFAULT);
	
#if 1
	entry_id3_flag = ID3_NO_TAG;

#if 1//def MP3_ID3_ENABLE
	{
		DBG_Printf ("Search ID3 V2.x\n\r");
		
		if (ID3v2_process(&id3_tag) == ID3_TAG_OK)
		{
			_unicode_to_oem(id3_tag.artist, id3_tag.artist);
			_unicode_to_oem(id3_tag.title, id3_tag.title);
			_unicode_to_oem(id3_tag.album, id3_tag.album);
			
			entry_id3_flag = ID3_TAG_PRESENT;
		}
		else
		{
			DBG_Printf ("Search ID3 V1.x\n\r");

			if (ID3v1_process(&id3_tag) == ID3_TAG_OK )
			{
				DBG_Printf ("ID3 TAG V1.x Present\n\r");
				entry_id3_flag = ID3_TAG_PRESENT;
			}
		}

		if (wRequestDecodeFrameExit == TRUE)
		{
			//wCodecBusy = FALSE;
			return;
		}
	
		if (entry_id3_flag == ID3_TAG_PRESENT)
		{
			DBG_Printf ("Artist: %s\n\r", id3_tag.artist);
			DBG_Printf ("Title: %s\n\r", id3_tag.title);

			DBG_Printf ("Album: %s\n\r", id3_tag.album);
#ifdef ID3ALL
			DBG_Printf ("Genre: %s\n\r", id3_tag.genre);
			DBG_Printf ("Year: %s\n\r", id3_tag.year);
#endif

			//adding id3 tag to disp info
			app_add_song_id3tag (app_main_data.playing_track);
		}
		else
		{
			DBG_Printf("No ID3 Tag.\n\r");
		}
	}
#endif //MP3_ID3_ENABLE

	if (wRequestDecodeFrameExit == TRUE)
	{
		//wCodecBusy = FALSE;
		return;
	}
	
	//check mp3 header and vbr info

	DBG_Printf ("Find MP3 Header\n\r");

	if (mpeg_find_header () == FALSE)
	{
		if (wRequestDecodeFrameExit != TRUE)
		{
			DBG_Printf("Can not find mp3 header\n\r");
			uMsgSend (UI_CD_INTERFACE, UI_MP3_INIT_FAIL, 0);		
		}

		//wCodecBusy = FALSE;
		return;
	}


	mp3_decode_init();

	if  (xa_mp3_dec_init() != XA_NO_ERROR)
	{
		DBG_Printf("mp3 dec init error\n\r");
		uMsgSend (UI_CD_INTERFACE, UI_MP3_INIT_FAIL, 0);
	}
	else
	{
		if (wRequestDecodeFrameExit == FALSE)
		{
			//checking the sample rate freq, now only support 32k,44.1k and 48k)
			if (app_main_data.playing_stream_sample_rate == 44100 ||
				app_main_data.playing_stream_sample_rate == 48000 ||
				app_main_data.playing_stream_sample_rate == 32000)
			{
				#ifdef FOR_ESD_PROTECT
				resume_trk_info.need_resume_play = FALSE;
				#endif
				uMsgSend (UI_CD_INTERFACE,UI_MP3_INIT_SUCCESS,0);
			}
			else
			{
				uMsgSend (UI_CD_INTERFACE, UI_MP3_INIT_FAIL, 0);
			}

			DBG_Printf("mp3 dec init ok\n\r");
		}
	}

	//wCodecBusy = FALSE;

	delayms(10);
	DBG_Puts ("mp3 dec init exit\n\r");
#endif

#endif //MP3_DECODE_ENABLE
}

void mp3_dec_init( void )
{
	app_main_data.audio_src_bit = AUDIO_SRC_16BIT;
	app_dec_mp3_init();
}

void mp3_dec_fini( int reason )
{
	if (reason == DECODE_END)
	{
		// Reached the end of bit stream.
		//playing to stream end
		uMsgSend (UI_CODEC, UI_DECODE_STREAM_EOF, 0);	
	}
	DBG_Printf("mp3 dec fini\n\r");
}

void mp3_dec_decode( void )
{	
	xa_mp3_decode();

	// Reached the end of bit stream.
	if (wRequestDecodeFrameExit != TRUE)
	{
		//playing to stream end
		uMsgSend (UI_CODEC, UI_DECODE_STREAM_EOF, 0);					
	}
}

void mp3_decode_init(void)
{
#ifdef TIME_SCALE_CODE
	tscaleSpeed	=	app_main_data.play_speed;
	
	tscale_init();
//	tscale_setSpeed( en_SPEED_50, 2 );
	tscale_setSpeed( tscaleSpeed, 2 );
	
//	tscale_initFifoChk();
#endif

//	xa_mp3_dec_init();
}
#if 0
void mp3_decode (void)
{
#ifdef FOR_ESD_PROTECT
	resume_trk_info.need_resume_play = FALSE;
#endif

	xa_mp3_decode();

	// Reached the end of bit stream.
	if (wRequestDecodeFrameExit != TRUE)
	{
		//playing to stream end
		uMsgSend (UI_CODEC, UI_DECODE_STREAM_EOF, 0);					
	}
}
#endif

/************************************************************************************
*							MPEG FIND SYNC											*
*************************************************************************************/

#if 1

typedef struct {
    U8 stereo;
    U8 lsf;
    U8 mpeg25;
    U8 lay;
    U8 error_protection;
    U8 bitrate_index;
    U8 sampling_frequency;
    U8 padding;
    U8 extension;
    U8 mode;
    U8 mode_ext;
    U8 copyright;
    U8 original;
    U8 emphasis;
    U16 framesize; /* computed framesize */
	
	U8 *frame_header;
	//int frame_header_bitpos;
	//unsigned int *main_data_header;
	//int main_data_bitpos;

	U8 *NextRead;
	//int NextBitpos;

	//int src_index;

	U8 vbr;
	U8 vbr_toc;
	unsigned long vbr_frames;
	unsigned long vbr_bytes;
	
	//unsigned int pre_mpeg_header_status;
	//unsigned int pre_mpeg_header[2];
	//int match_count;
	//int decoded_data_ready; 
	//int decode_frames;
	U16 bit_rate;

} FRAME;

typedef struct {
	unsigned int syncword				:	11;
	unsigned int IDex					:	1;
	unsigned int ID						:	1;
	unsigned int layer					:	2;
	unsigned int protection_bit			:	1;
	unsigned int bitrate_index			:	4;
	unsigned int sample_frequencsy		:	2;
	unsigned int padding_bit			:	1;
	unsigned int private_bit			:	1;
	unsigned int mode					:	2;
	unsigned int mode_extension			:	2;
	unsigned int copyright				:	1;
	unsigned int original				:	1;
	unsigned int emphasis				:	2;
} __attribute__ ((packed)) MPEG_HEADER;


const U16 Bit_Rate [2][15] = {
	{0,4000,5000,6000,7000,8000,10000,12000,14000,16000, 20000,24000,28000,32000,40000},
	{0,1000,2000,3000,4000,5000,6000,7000,8000,10000,12000,14000,16000,18000,20000}
};

const U16 freqs[9] = {
	44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000
};

const U16 tabsel_123[2][3][16] = {
   { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
     {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
     {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },

   { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
     {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
     {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
};

#define         MPG_MD_STEREO           0
#define         MPG_MD_JOINT_STEREO     1
#define         MPG_MD_DUAL_CHANNEL     2
#define         MPG_MD_MONO             3

#define			MAXFRAMESIZE 1792

#define FRAMES_FLAG     0x0001
#define BYTES_FLAG      0x0002
#define TOC_FLAG        0x0004
#define VBR_SCALE_FLAG  0x0008

#define NUMTOCENTRIES	100

enum {
	MPEG1_STREAM = 0,
	MPEG2_STREAM = 1
};

#define MASK_IDex					0x10				//:	1;
#define MASK_ID						0x08				//:	1;
#define MASK_layer					0x06				//:	2;
#define MASK_protection_bit			0x01				//:	1;
	
#define MASK_bitrate_index			0xF0				//:	4;
#define MASK_sample_frequencsy		0x0C				//:	2;
#define MASK_padding_bit				0x02				////:	1;
#define MASK_private_bit				0x01				//:	1;

#define MASK_mode					0xC0				//:	2;
#define MASK_mode_extension			0x30				//:	2;
#define MASK_copyright				0x08				//:	1;
#define MASK_original				0x04				//:	1;
#define MASK_emphasis				0x03				//:	2;



int decode_header(U8 *header, FRAME *mpeg_frame)
{
	long framesize;
	U8 tmp;

//	mpeg_frame->single = -1;

//	if (header->IDex) {
	if ( (header[1] & MASK_IDex) != 0x0)
	{
		mpeg_frame->mpeg25 = FALSE;

	//	if (header->ID) {
		if ( (header[1] & MASK_ID) != 0x0) {
			mpeg_frame->lsf = MPEG1_STREAM;				/*MPEG 1*/
		}
		else {
			mpeg_frame->lsf = MPEG2_STREAM;				/*MPEG 2*/
		}
	}
	else
	{
		/*MPEG 2.5*/
		//if (header->ID)
		if ( (header[1] & MASK_ID) != 0x0)
			return FALSE;					/*MPEG2.5 MUST BE MPEG 2.0*/

			
		mpeg_frame->lsf = MPEG2_STREAM;
		mpeg_frame->mpeg25 = TRUE;
	}
	
	tmp = (header[1] & MASK_layer) >> 1;
	
	if (tmp == 0) {

		return FALSE;
	}

	mpeg_frame->lay = 4 - tmp;

	tmp = header[1] & MASK_protection_bit;
	mpeg_frame->error_protection = tmp ^ 1;

	tmp = (header[2] & MASK_bitrate_index) >> 4;
	mpeg_frame->bitrate_index = tmp;

	tmp = (header[2] & MASK_sample_frequencsy) >> 2;

	if (tmp == 3) {
		return FALSE;
	}

	//if ((U8) header->sample_frequencsy == 3) {
	//	return FALSE;
	//}

	if (mpeg_frame->mpeg25) {
		mpeg_frame->sampling_frequency = 6 + tmp;
	}
	else {
		mpeg_frame->sampling_frequency = tmp + 3 * mpeg_frame->lsf;
	}

//	mpeg_frame->error_protection = (U8) header->protection_bit ^ 1;
//	mpeg_frame->bitrate_index = (U8) header->bitrate_index;

	tmp = (header[2] & MASK_padding_bit) >> 1;
	mpeg_frame->padding = tmp;
	tmp = (header[2] & MASK_private_bit);
	mpeg_frame->extension = tmp;

	tmp = (header[3] & MASK_mode) >> 6;
	mpeg_frame->mode = tmp;
	tmp = (header[3] & MASK_mode_extension) >> 4;
	mpeg_frame->mode_ext = tmp;
	tmp = (header[3] & MASK_copyright) >> 3;
	mpeg_frame->copyright = tmp;
	tmp = (header[3] & MASK_original) >> 2;
	mpeg_frame->original = tmp;
	tmp = (header[3] & MASK_emphasis);
	mpeg_frame->emphasis = tmp;

	mpeg_frame->stereo = ((U8) mpeg_frame->mode == MPG_MD_MONO) ? 1 : 2;

	if ( mpeg_frame->bitrate_index == 0 ||  mpeg_frame->bitrate_index == 15) {
		/*we don't support free running format*/
		//DBG_Printf ("Can not play free running stream\n\r");
		return FALSE;
	}

	/*caculate frame size*/
	if ((U8) mpeg_frame->lsf == 0)
	{
		framesize  = (long) ((long) tabsel_123[mpeg_frame->lsf][mpeg_frame->lay-1][mpeg_frame->bitrate_index]) * 144000l;
		framesize /= (long) (freqs[mpeg_frame->sampling_frequency]);
		framesize = framesize + (long) mpeg_frame->padding - 4l;
		mpeg_frame->framesize = (U16) framesize;			
	}
	else
	{
		framesize  = (long) ((long) tabsel_123[mpeg_frame->lsf][mpeg_frame->lay-1][mpeg_frame->bitrate_index]) * 72000l;
		framesize /= (long) (freqs[mpeg_frame->sampling_frequency]);
		framesize = framesize + (long) mpeg_frame->padding - 4l;
		mpeg_frame->framesize = (U16) framesize;			
	}


	return TRUE;
}

int mpeg_find_header (void)
{
	U8 tmp1, tmp2, num_readbuf = 0;
	long len;
	U8 mpeg_header [4], xing_buf[4];
	STREAM_ID s;

	FRAME *mpeg_frame, *second_mpeg_frame;
	U32 pos,tmp_pos;
	U32 times,stream_len;
	MYFILE *pFile;
	U32 rlen;
	int ret;

	DBG_Printf ("Find MP3 header\n\r");

	pFile = app_main_data.pMediaRead;					//just pass one parm, no meaning

	codec_malloc_init ();

	mpeg_frame = (FRAME *)codec_malloc( sizeof (FRAME) );
	DBG_Assert (mpeg_frame != NULL);

	second_mpeg_frame = (FRAME *)codec_malloc( sizeof (FRAME) );
	DBG_Assert (mpeg_frame != NULL);
	
	app_main_data.playing_stream_total_time = 0;
	app_main_data.playing_stream_vbr = 0;

	times = 0;

	//debug
//	xa_fseek (pFile, 0, SEEK_SET);

	pos = xa_ftell (pFile);
	rlen = xa_fread (&tmp1,1,1,pFile);
	if (rlen == 0) {
		return FALSE;
	}

	while (1)
	{
		while (1)
		{
			if (wRequestDecodeFrameExit == TRUE)
			{
				return FALSE;
			}

			times++;
			if (times > 0x2000)
			{
				times = 0;
				app_nav_sleep (100);
			}

			if (tmp1 == 0xFF)
			{
				rlen = xa_fread (&tmp2, 1, 1, pFile);
				if (rlen == 0)
				{
					return FALSE;
				}
			
				if ( (tmp2 & 0xE0) == 0xE0 )
				{
					//can not be layer 4

					/*Header is found*/
					mpeg_header[0] = tmp1;
					mpeg_header[1] = tmp2;

					rlen = xa_fread (&mpeg_header[2], 1, 2, pFile);
					if (rlen == 0)
					{
						return FALSE;
					}
						
					/* decode header */
					if (decode_header(mpeg_header, mpeg_frame) == TRUE)
					{
													
						break;							
					}
					else
					{
						pos += 1;
//						ret = xa_fseek (pFile, pos, SEEK_SET);
						ret = xa_fseek (pFile, (pos+1), SEEK_SET);
						if (ret == -1)
						{
							return FALSE;
						}

						tmp1 = tmp2;
					}
				}
				else
				{
					pos = xa_ftell (pFile);
					rlen = xa_fread (&tmp1, 1, 1, pFile);
					if (rlen == 0)
					{
						return FALSE;
					}
				}
			}
			else
			{
				pos = xa_ftell (pFile);
				rlen = xa_fread (&tmp1, 1, 1, pFile);
				if (rlen == 0)
				{
					return FALSE;
				}
			}
		}

		app_main_data.playing_stream_media_offset = pos;

		if ( mpeg_frame->lsf == 0 )
		{
			/*MPEG 1*/
			if(mpeg_frame->stereo == 1)
			{
				/*siggle channel*/
				//mpeg_streambuf_rpoint (17+4);
				ret = xa_fseek (pFile, (pos+17+4), SEEK_SET);
				if (ret == -1)
				{
					return FALSE;
				}
			}
			else
			{
				/*two channels*/
				//mpeg_streambuf_rpoint (32+4);
				ret = xa_fseek (pFile, (pos+32+4), SEEK_SET);
				if (ret == -1)
				{
					return FALSE;
				}
			}
		}
		else
		{
			/*MPEG 2*/
			if(mpeg_frame->stereo == 1)
			{
				/*single channel*/
				//mpeg_streambuf_rpoint (9+4);
				ret = xa_fseek (pFile, (pos+9+4), SEEK_SET);
				if (ret == -1)
				{
					return FALSE;
				}
			}
			else
			{
				/*two channels*/
				//mpeg_streambuf_rpoint (17+4);
				ret = xa_fseek (pFile, (pos+17+4), SEEK_SET);
				if (ret == -1)
				{
					return FALSE;
				}
			}

		}

		rlen = xa_fread (xing_buf,1,4,pFile);
		if (rlen == 0)
		{
			return FALSE;
		}

		if (  (xing_buf[0] != 0x58 ) ||
				(xing_buf[1] != 0x69 )  ||		//Xing
				(xing_buf[2] != 0x6E )  ||
				(xing_buf[3] != 0x67) )
		{			
			//not vbr stream
//			xa_fseek (pFile, pos, SEEK_SET);
//			return;
		}
		else
		{
			//vbr stream
			unsigned int head_flags;
			unsigned int i;

			mpeg_frame->vbr = TRUE;

			head_flags = 0;

			rlen = xa_fread (&tmp1, 1, 1, pFile);
			if (rlen == 0)
			{
				return FALSE;
			}
			head_flags |= tmp1;
			head_flags <<= 8;

			rlen = xa_fread (&tmp1, 1, 1, pFile);
			if (rlen == 0)
			{
				return FALSE;
			}

			head_flags |= tmp1;
			head_flags <<= 8;
				
			rlen = xa_fread (&tmp1,1,1,pFile);
			if (rlen == 0)
			{
				return FALSE;
			}
			head_flags |= tmp1;
			head_flags <<= 8;

			rlen = xa_fread (&tmp1,1,1,pFile);
			if (rlen == 0)
			{
				return FALSE;
			}

			head_flags |= tmp1;

			if ( head_flags & FRAMES_FLAG )
			{
				//mpeg_frame->vbr_frames   = ((unsigned long) read_bits (16)) << 16;
				//mpeg_frame->vbr_frames |= (unsigned long) read_bits (16);
				
				//xa_fread (&(mpeg_frame->vbr_frames),1,4,pFile);
				//mpeg_frame->vbr_frames = dw_Little_Big_Endian (&(mpeg_frame->vbr_frames));

				mpeg_frame->vbr_frames = 0;

				rlen = xa_fread (&tmp1,1,1,pFile);
				if (rlen == 0) {
					return FALSE;
				}

				mpeg_frame->vbr_frames |= tmp1;
				mpeg_frame->vbr_frames <<= 8;

				rlen = xa_fread (&tmp1, 1, 1, pFile);
				if (rlen == 0)
				{
					return FALSE;
				}

				mpeg_frame->vbr_frames |= tmp1;
				mpeg_frame->vbr_frames <<= 8;

				rlen = xa_fread (&tmp1, 1, 1, pFile);
				if (rlen == 0)
				{
					return FALSE;
				}

				mpeg_frame->vbr_frames |= tmp1;
				mpeg_frame->vbr_frames <<= 8;

				rlen = xa_fread (&tmp1, 1, 1, pFile);
				if (rlen == 0)
				{
					return FALSE;
				}

				mpeg_frame->vbr_frames |= tmp1;

				if( mpeg_frame->lsf == MPEG1_STREAM ) {
					mpeg_frame->vbr_frames <<= 1;
				}

				app_main_data.playing_stream_vbr = 1;

				app_main_data.playing_stream_total_time =
				(mpeg_frame->vbr_frames * 576l) / freqs[mpeg_frame->sampling_frequency];

				stream_len = AUDIO_FIFO_STREAM_STREAM_LEN() - pos;
				//app_main_data.playing_stream_media_offset = pos;

#if 0
				//average bitrate
				app_main_data.playing_stream_bitrate = (U16) (stream_len / app_main_data.playing_stream_total_time);
#else
				if (app_main_data.playing_stream_total_time != 0)
				{
					//average bitrate
					app_main_data.playing_stream_bitrate = (U16) (stream_len / app_main_data.playing_stream_total_time);
				}
				else
				{
					DBG_Assert(FALSE);
				}
#endif

				DBG_Printf ("VBR total playing time %d\n\r", app_main_data.playing_stream_total_time);
				DBG_Printf ("VBR average bitrate %d\n\r", app_main_data.playing_stream_bitrate);
			}

			ret = xa_fseek (pFile, pos, SEEK_SET);
			if (ret == -1)
			{
				return FALSE;
			}
		}

		//check the second header
		ret = xa_fseek (pFile, (pos + mpeg_frame->framesize + 4) , SEEK_SET);
		if (ret == -1)
		{
			return FALSE;
		}

		tmp_pos = xa_ftell (pFile);
		rlen = xa_fread (&tmp1,1,1,pFile);
		if (rlen == 0)
		{
			return FALSE;
		}

		if (tmp1 == 0xFF)
		{
			rlen = xa_fread (&tmp2,1,1,pFile);
			if (rlen == 0)
			{
				return FALSE;
			}
			if ( (tmp2 & 0xE0) == 0xE0) 
			{
				rlen = xa_fread (&mpeg_header[2], 1, 2, pFile);
				if (rlen == 0) {
					return FALSE;
				}

				/* decode header */
				if (decode_header(mpeg_header, second_mpeg_frame) == TRUE) {

					if ( (mpeg_frame->sampling_frequency == second_mpeg_frame->sampling_frequency) &&
						(mpeg_frame->mpeg25 == second_mpeg_frame->mpeg25) &&
						(mpeg_frame->lsf == second_mpeg_frame->lsf) )
					{
						
						//header is find, jump the the second header of mp3, missing the first header?
						ret = xa_fseek (pFile, (pos + mpeg_frame->framesize + 4) , SEEK_SET);
						if (ret == -1) {
							return FALSE;
						}

						DBG_Printf ("Find mp3 header\n\r");

						return TRUE;
					}
				}
			}
		}
		

		{
			DBG_Printf ("The first head is err\n\r");
					
			//Search from last pos

			tmp_pos += 1;
			//ret = xa_fseek (pFile, pos, SEEK_SET);
			pos = app_main_data.playing_stream_media_offset + 1;
			ret = xa_fseek (pFile, pos, SEEK_SET);
			if (ret == -1)
			{
				return FALSE;
			}

			rlen = xa_fread (&tmp1, 1, 1, pFile);
			if (rlen == 0)
			{
				return FALSE;
			}

			//tmp1 = tmp2;
		}
	}
}

#endif



#ifdef TIME_SCALE_CODE

U16 srcTmpBuffer[MP3_DEC_FRAME_SIZE*2] __attribute__((section(".tscale_bss")));

int mp3GetPcmData(void )
{
	int ret;
	U32 frame_len;
	U32 *decoder_buf;
				
	ret = xa_mp3_dec_frame(&decoder_buf, &frame_len);
	if (!ret)
	{
		return 1;
	}
	
	if (frame_len > 0)
	{
		ret =  Decode_OutputBuffer(decoder_buf, frame_len);
		if (!ret)
		{
			return 1;
		}
	}
	
	if (wRequestDecodeFrameExit == TRUE)
	{
		return TRUE;
	}

#if 0
	wSrcSlice = getSrcSliceSize( out_dwords, in_dwords, &wSrcSliceSize );
	for (i=0; i<wSrcSlice; i++)
	{
		tscale_input( srcTmpBuffer, *pframe_len );
	}
#endif

	return 0;
}

#endif //TIME_SCALE_CODE


#endif //MP3_DECODE_ENABLE