Id3v2.c 34.9 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 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
#include "c_def.h"  
#include "debug.h"
#include "oem.h"

#include "audio_dec.h"
#include "app_sdram.h"
#include "app_main.h"
#include "id3v2.h"
#include "string.h"
#include "wstring.h"
//#include "mp3_decode.h"
#include "codec.h"
#include "tools.h"
//#include "wma_asf_def.h"


#if 1//def MP3CD

typedef struct{
	char Header[3];     /*±ØÐëΪ "ID3" ·ñÔòÈÏΪ±êÇ©²»´æÔÚ*/
	char Ver;           /*ID3V2°æ±¾ºÅ£¬ID3V2.3 ¾Í¼Ç¼3*/
	char Revision;      /*¸±°æ±¾ºÅ£¬Ò»°ãΪ0*/
	char Flag;          /*´æ·Å±êÖ¾×Ö½Ú£¬Ò»°ãÈ«²¿Îª0*/
	char Size[4];       /*ËùÓбêÇ©´óС£¬²»°üÀ¨±êǩͷµÄ10¸ö×Ö½Ú*/
} ID3V2_TAG;

typedef struct{
	char FrameID[3];         /*ÓÃÈý¸ö×Ö·û±êʾһ¸öÖ¡ID£¬±ÈÈç TT2(¸èÇúÃû)£¬TP1(ÒÕÊõ¼Ò)*/
	char Size[3];            /*Ö¡ÌåÄÚÈÝ´óС£¬²»°üº¬Ö¡ID£¬²»µ½Ð¡ÓÚ1*/
	//char FrameCont_encode; /*Ö¡Ìå×Ö·ûËùÓñàÂë*/
}ID3V22_TAG;


/*
ID3V2.2 ³£ÓÃÖ¡±êʾ
	TT2:   ±êÌâ               	TP1:     ÒÕÊõ¼Ò
	TAL:   ר¼­Ãû            	TRK:     Òô¹ìºÅ/ºÏ¼ÆÒô¹ì
	TYE:   ·¢ÐÐÈÕÆÚ     	COM:     ×¢ÊÍ
	TEN:   ±àÂ뷽ʽ          TCO:     Á÷ÅÉ


FrameCont_encode ÓÐ4¸öÖµ;
	0000 ´ú±íÖ¡ÌåÓà ISO-8859-1 ±àÂ뷽ʽ´æ´¢£»
	0001 ´ú±íÖ¡ÌåÓà UTF-16LE   ±àÂ뷽ʽ´æ´¢£»
	0002 ´ú±íÖ¡ÌåÓà UTF-16BE   ±àÂ뷽ʽ´æ´¢£»
	0003 ´ú±íÖ¡ÌåÓà UTF-8      ±àÂ뷽ʽ´æ´¢£»[Ö»ÓÃID3V2.4²ÅÖ§³ÖUTF-8±àÂ뷽ʽ]
*/


typedef enum{
	ENCODE_ISO_8859,
	ENCODE_UTF_16LE,
	ENCODE_UTF_16BE,
	ENCODE_UTF_8
}ENCODE_TYPE;

#define MAX_FRAME_CONTENT    256 /*only 256 bytes be processed*/


typedef	struct _ID3v2_header
{
	U8	version;	
	U8	flag;
	long	size;
	long	exthead_size;
}	ID3v2_HEADER;

#define	FRAME_FORMAT_GROUPID	0x40
#define	FRAME_FORMAT_COMPRESS	0x08
#define	FRAME_FORMAT_ENCRYPT	0x04
#define	FRAME_FORMAT_UNSYNCH	0x02
#define	FRAME_FORMAT_DATALEN	0x01			

#define	ID3v2_FLAG_UNSYNCHRONIZATION	(1 << 7)
#define	ID3v2_FLAG_EXTHEADER			(1 << 6)
#define	ID3v2_FLAG_EXPERIMENTAL			(1 << 5)
#define	ID3v2_FLAG_FOOTER				(1 << 4)

#define ID3V2_HEADER_SIZE_UNKNOWN	0xFFFFFFFF

#define	MAXIM_FRAME_ID				7	

const U8 ID3v2_Frame_ID[MAXIM_FRAME_ID][5] =
{
	{"TIT2"},
	{"TPE1"},
	{"TALB"},
	{"SEEK"},
	{"TCON"},
	{"TYER"},
	{"TDRC"}
};

typedef	enum
{
	ID3_FRAME_TIT2		=	0,		// title
	ID3_FRAME_TPE1,					// artist
	ID3_FRAME_TALB,					// album
	ID3_FRAME_SEEK,

	ID3_FRAME_TCON,					// genre
	ID3_FRAME_TYER,					// year, in V2.3
	ID3_FRAME_TDRC,					// year in V2.4

	ID3_FRAME_PADDING	=	0xFE,
	ID3_FRAME_OTHER		=	0xFF
}	ID3_FRAME_RETURN;


ID3_FRAME_RETURN ID3_frameID_check(U8 * frame_buf )
{
	int i,j;
	U8 * ps;
	const U8* pt;
	
	if( frame_buf[0] == 0 ) {
		return ID3_FRAME_PADDING;
	}

	for ( i =0; i < MAXIM_FRAME_ID; i ++ ) {
		ps = frame_buf;
		pt = ID3v2_Frame_ID[i];

		for ( j =0; j < 4; j ++ ) {
			if( *ps ++ != *pt ++ ) {
				break;
			}
		}
		if ( j == 4 ) {
			return (ID3_FRAME_RETURN)i;
		}
	}

	return ID3_FRAME_OTHER;
}

long Synsafe_4byte( U8 *buf )
{
	return  ( ( ((long) buf[3]) )  + (((long) buf[2]) << 7 ) 
		+ ( ((long) buf[1]) << 14 ) 
		+ ( ((long) buf[0]) << 21 ) );	
}

void ID3v2_frame_unsynchronization(U8 *buf, long len)
{
	int i,k;
	
	i = 0;
	k = 0;

	while (i < len) {

		if ( (buf[i] == 0xFF) && (buf[i+1] == 0x0) ) {
			if ( (buf[i+2] & 0xE0) != 0 ) {
				buf[k++] = buf[i++];
				i++;						/*pass unsync byte*/
			}
			else if (buf [i+2] == 0 ) {
				buf[k++] = buf[i++];
				i++;						/*pass unsync byte*/
			}
			else {
				buf[k++] = buf[i++];			
			}
		}
		else {
			buf[k++] = buf[i++];			
		}
	}
}

void ID3v2_init(ID3_TAG *id3_tag)
{
/*
	ID3v2_header.version		= 0;
	ID3v2_header.size			= ID3V2_HEADER_SIZE_UNKNOWN;
	ID3v2_header.flag			= 0;
	ID3v2_header.exthead_size	= 0;
*/

	id3_tag->artist[0] = 0x0;
	id3_tag->artist[1] = 0x0;
	id3_tag->title[0] = 0x0;
	id3_tag->title[1] = 0x0;

	id3_tag->album[0] = 0x0;
	id3_tag->album[1] = 0x0;				//unicode null
#ifdef ID3ALL
	id3_tag->genre[0] = 0x0;
	id3_tag->genre[1] = 0x0;
	id3_tag->year[0] = 0x0;
	id3_tag->year[1] = 0x0;
#endif
}
  
static void Id3v2_parse_content (U8 encoding, U8 *pFrame, U16 len, U16 * pBuf, U16 max_wlen)
{
	U16 tmp,i;
	U16 *pWord;

	if (encoding == ENCODE_UTF_16LE)
	{
		DBG_Printf ("16 LE Tag\n\r");

		tmp = *((U16 *) pFrame);
		pFrame += 2;
		pWord = (U16 *) pFrame;
		len -= 2;

		//len = wstrlen (pWord);

		//DBG_Printf ("content len %d\n\r", len);

		if (len > 2 * max_wlen)
			len = 2 * max_wlen;

		if (tmp == 0xFFFE) {
			//Little Endian

			for (i=0; i< len/2; i++) {
#ifdef LITTLE_ENDIAN
				pBuf[i] = pWord[i];
#else
				pBuf[i] = w_Little_Big_Endian(&pWord[i]);
#endif
			}
			

			pBuf[i] = 0x0;

			//DBG_Printf ("%s\n\r", pBuf);

		}
		else {
			//Big Endian
			for (i=0; i< len/2; i++) {
#ifdef LITTLE_ENDIAN
				pBuf[i] = w_Little_Big_Endian(&pWord[i]);
#else
				pBuf[i] = pWord[i];
#endif
			}

			pBuf[i] = 0x0;

		}

		wstrCutLeadingSpace (pBuf);
		wstrCutTrailingSpace (pBuf);

		//_unicode_to_oem (pBuf, pBuf);
	}
	else if (encoding == ENCODE_UTF_16BE) 
	{
		DBG_Printf ("16 BE Tag\n\r");

		pWord = (U16 *) pFrame;

		//len = wstrlen (pWord);
		if (len > 2 * max_wlen)
			len = 2 * max_wlen;
						
		for (i=0; i< len/2; i++) {
#ifdef LITTLE_ENDIAN
			pBuf[i] = w_Little_Big_Endian(&pWord[i]);
#else
			pBuf[i] = pWord[i];
#endif
		}

		pBuf[i] = 0x0;

		wstrCutLeadingSpace (pBuf);
		wstrCutTrailingSpace (pBuf);

		//_unicode_to_oem (pBuf, pBuf);

	}
	else //if( (encoding == ENCODE_ISO_8859) ||(encoding == ENCODE_UTF_8) )
	{
		//len = strlen ((const char *) pFrame);

		if (len > max_wlen) {
			len = max_wlen;
		}

		pFrame[len] = 0x0;

		strCutLeadingSpace (pFrame);
		strCutSpace (pFrame);
		oem_to_unicode (pFrame,pBuf);

	}
}

#define ID3V2_ARTIST_FOUND		0X01
#define ID3V2_TITLE_FOUND		0X02
#define ID3V2_ALBUM_FOUND		0X04

#ifdef ID3ALL
#define ID3V2_GENRE_FOUND		0X08
#define ID3V2_YEAR_FOUND		0X10
#else
#define ID3V2_GENRE_FOUND		0
#define ID3V2_YEAR_FOUND		0
#endif

#define ID3V2_ALL_FOUND	(ID3V2_ARTIST_FOUND|ID3V2_TITLE_FOUND|ID3V2_ALBUM_FOUND|ID3V2_GENRE_FOUND|ID3V2_YEAR_FOUND)

typedef struct{
	char id[3];
	ID3_FRAME_RETURN type;
}ID3V22_TAB;
static const ID3V22_TAB id3v22_id_tab[] =
{
	{'T','T','2',	ID3_FRAME_TIT2},	//title
	{'T','P','1',	ID3_FRAME_TPE1},	//artist
	{'T','A','L',	ID3_FRAME_TALB},	//album
	{'T','C','O',	ID3_FRAME_TCON},//genre
	{'T','Y','E',	ID3_FRAME_TYER},	//year
};

static ID3_FRAME_RETURN ID3v22_frameID_check(U8 * frame_buf )
{
	U8 i;
	const ID3V22_TAB *id3v22 = id3v22_id_tab;
	
	if( frame_buf[0] == 0 ) {
		return ID3_FRAME_PADDING;
	}

	for ( i =0; i < NELEMS(id3v22_id_tab); i ++,id3v22++ )
	{
		if((frame_buf[0] == id3v22->id[0])&&
			(frame_buf[1] == id3v22->id[1])&&
			(frame_buf[2] == id3v22->id[2]))
		{
			return id3v22->type;
		}
	}

	return ID3_FRAME_OTHER;
}



static ID3_RETURN ID3v2_parse( ID3_TAG *id3_tag, MYFILE *pFile )
{
	STREAM_ID s;
	long len, rlen, size, frame_size;
	long frame_start_pos;
	U8 *pFrame;
	U16 frame_flag;
	ID3_FRAME_RETURN ftype;
	U8 read, find = 0;
	U8 encoding;
	
	//U32 mem_used = codec_get_mem_used();
	U8 *stream_buf;
	U8 *m_buf;

	codec_malloc_init ();

	stream_buf = (U8 *)codec_malloc(MAX_FRAME_CONTENT);
	m_buf = (U8 *)codec_malloc(10);
	
	ID3v2_HEADER *ID3v2_header = (ID3v2_HEADER *)codec_malloc(sizeof(ID3v2_HEADER));

	len = xa_fread (m_buf, 1, 10, pFile);
	DBG_Assert (len == 10);

	if ( (m_buf[0] != 'I') || (m_buf[1] != 'D') || (m_buf[2] != '3') )
	{
		/*no id3 v2 tag present*/
		//codec_set_mem_used(mem_used);

		xa_fseek (pFile, 0, SEEK_SET);
		app_main_data.playing_stream_media_offset = 0;
		return NO_ID3_TAG;
	}

//---------------------------------------------------	/*id3 v2 tag present*/
	ID3v2_header->version	=	m_buf[3];
	ID3v2_header->flag		=	m_buf[5];
	ID3v2_header->size		=	Synsafe_4byte( &m_buf[6] );
	DBG_Printf ("ID3 TAG V2.%d Present\n\r", ID3v2_header->version);

	app_main_data.playing_stream_media_offset = (ID3v2_header->size + 10);


//---------------------------------------------------	/*id3 v2.2 present*/
	if( ID3v2_header->version < 2 || ID3v2_header->version > 4 )
	{
		//codec_set_mem_used(mem_used);
		xa_fseek (pFile, app_main_data.playing_stream_media_offset, SEEK_SET);
		return NO_ID3_TAG;
	}
//---------------------------------------------------	/*	jump pass the extended header */
	if ( ID3v2_header->flag & ID3v2_FLAG_FOOTER )
	{
		//write id3v2.4 footer here laser
	}

	if ( ID3v2_header->flag & ID3v2_FLAG_EXTHEADER )
	{
		len = xa_fread (m_buf, 1, 4, pFile);
		DBG_Assert (len == 4);

		ID3v2_header->exthead_size =  Synsafe_4byte(m_buf);
		frame_start_pos = 10 + ID3v2_header->exthead_size;

		xa_fseek(pFile, frame_start_pos, SEEK_SET);
	}
	else
	{
		frame_start_pos = 10;
		ID3v2_header->exthead_size =	0;
	}
//---------------------------------------------------
//	process frames , note all frames are restricted to <= 64 bytes
	size = ID3v2_header->size - ID3v2_header->exthead_size;
	read = (ID3v2_header->version==2) ? sizeof(ID3V22_TAG) : 10;

	while( (size > 0) && (find != ID3V2_ALL_FOUND))
	{
		len = xa_fread (m_buf, 1, read, pFile);
		DBG_Assert (len == read);

		if (ID3v2_header->version == 2)
			ftype = (U8) ID3v22_frameID_check(m_buf);
		else
			ftype = (U8) ID3_frameID_check(m_buf);

		if (ftype == ID3_FRAME_PADDING)
		{
			break;
		}
		else 
		{
			if (ID3v2_header->version == 2)
			{
				frame_size = m_buf[3]<<16 | m_buf[4]<<8|m_buf[5];
				frame_flag = 0;
			}
			else
			{
				// frame size is exclude header
				frame_size = m_buf[4]<<24 | m_buf[5]<<16 | m_buf[6]<<8 | m_buf[7];
				frame_flag = m_buf[8]<<8 | m_buf[9];
			}

			if ( frame_size > (size - read) ) {
				/*in error condition*/
				frame_size = size - read;
			}

			size -= (frame_size + read );			
			frame_start_pos += (frame_size + read );			/*next frame start pos*/
			
			if ( ftype == ID3_FRAME_OTHER )
			{
			}
			else if ( ftype == ID3_FRAME_SEEK )
			{
				//seek frames, later process
				break;
			}
			else if (frame_size > 0)
			{
				rlen = (frame_size > MAX_FRAME_CONTENT) ? MAX_FRAME_CONTENT: frame_size;	
				pFrame = (U8 *) stream_buf;

				xa_fread (&encoding, 1,1, pFile);
				rlen--;

				len = xa_fread (pFrame, 1,rlen, pFile);
				DBG_Assert (len == rlen);
				
				pFrame[len] = 0x0;		/*add null*/

				if( ( ID3v2_header->flag & ID3v2_FLAG_UNSYNCHRONIZATION )|| ( frame_flag & FRAME_FORMAT_UNSYNCH ))
				{
					ID3v2_frame_unsynchronization ((U8 *) pFrame, rlen);
				}

				//DBG_Printf ("encoding %d len %d\n\r", encoding, rlen);


				switch ( ftype )
				{
				case ID3_FRAME_TIT2:
					Id3v2_parse_content(encoding, pFrame,len, id3_tag->title, MAX_ID3_TEXT);
					//DBG_Printf ("T%s\n\r", id3_tag->title);

					find |= ID3V2_TITLE_FOUND;
					break;

				case ID3_FRAME_TPE1:
					Id3v2_parse_content(encoding, pFrame, len, id3_tag->artist, MAX_ID3_TEXT);
					//DBG_Printf ("A%s\n\r", id3_tag->artist);
					find |= ID3V2_ARTIST_FOUND;
					break;

				case ID3_FRAME_TALB:
//#ifdef ID3ALL
					Id3v2_parse_content(encoding, pFrame,len, id3_tag->album, MAX_ID3_TEXT);
					//DBG_Printf ("Album%s\n\r", id3_tag->album);
					find |= ID3V2_ALBUM_FOUND;
//#endif
					break;
				
				// genre
				// V2.3 is different from V2.4
				case ID3_FRAME_TCON:
#ifdef ID3ALL
					Id3v2_parse_content(encoding, pFrame, id3_tag->genre, MAX_ID3_GENRE_TEXT);
					find |= ID3V2_GENRE_FOUND;
//change (13) to Pop
					{
						int len = wstrlen(id3_tag->genre);

						if ( (id3_tag->genre[0] == '(') && (id3_tag->genre[len-1] == ')') )
						{
							int i,genre;
							U16 *p = id3_tag->genre +1;
							
							for(genre=0,i=0;i<len-2;i++,p++)
							{
								genre = genre*10 + (*p-'0');
							}

							if (genre >= KNOWN_MP3_GENRES) 
								id3_tag->genre[0] = 0x0;
							else 
								oem_to_unicode (mp3_genre[genre], id3_tag->genre);
						}
					}
#endif
					break;

				// year for V2.3
				case ID3_FRAME_TYER:
				// year for V2.4
				case ID3_FRAME_TDRC:
#ifdef ID3ALL
					Id3v2_parse_content(encoding, pFrame, id3_tag->year, MAX_ID3_YEAR);
					find |= ID3V2_YEAR_FOUND;
#endif
					break;

				default:
					DBG_Assert(0);
				}
			}
			
			xa_fseek (pFile, frame_start_pos, SEEK_SET);
			//app_main_data.playing_stream_media_offset = frame_start_pos;
		}
	}
	
	//codec_set_mem_used(mem_used);
	xa_fseek (pFile, app_main_data.playing_stream_media_offset, SEEK_SET);
	return ID3_TAG_OK;
}

ID3_RETURN ID3v2_process( ID3_TAG *id3_tag )
{
	return ID3v2_parse(id3_tag, app_main_data.pMediaRead);
}

/*********************************************************
	ID3 V1 Genre function
*********************************************************/

#define	KNOWN_MP3_GENRES	148

const I8 * const mp3_genre[KNOWN_MP3_GENRES] = {
	"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",
	"Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies",
	"Other", "Pop", "R&B", "Rap", "Reggae", "Rock",
	"Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks",
	"Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk",
	"Fusion", "Trance", "Classical", "Instrumental", "Acid", "House",
	"Game", "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass",
	"Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock",
	"Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk",
	"Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta",
	"Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret",
	"New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi",
	"Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical",
	"Rock & Roll", "Hard Rock", "Folk", "Folk/Rock", "National Folk", "Swing",
	"Fast-Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde",
	"Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band",
	"Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson",
	"Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus",
	"Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
	"Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet",
	"Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall",
	"Goa", "Drum & Bass", "Club House", "Hardcore", "Terror",
	"Indie", "BritPop", "NegerPunk", "Polsk Punk", "Beat",
	"Christian Gangsta", "Heavy Metal", "Black Metal", "Crossover", "Contemporary C",
	"Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "JPop",
	"SynthPop",
};

U8 * get_genre_info( int genre)
{
	if( genre < 0 || genre >= KNOWN_MP3_GENRES ) {
		return 0;
	}
	else {
		return (U8 *) mp3_genre[genre] ;
	}
}

typedef struct {
	BYTE	tag [3];
	BYTE	title [30];
	BYTE	artist [30];
	BYTE	album [30];
	BYTE	year[4];
	BYTE	comment[30];
	BYTE	genre;
} ID3V1_TAG;
#define ID3V1_READ_SIZE		128

static ID3_RETURN id3v1_parser (U8 *buf, ID3_TAG *id3_tag)
{
	ID3V1_TAG *pId3v1_tag;
	int genre;
	int i;

	id3_tag->artist[0] = 0x0;
	id3_tag->artist[1] = 0x0;
	id3_tag->title[0] = 0x0;
	id3_tag->title[1] = 0x0;

	id3_tag->album[0] = 0x0;
	id3_tag->album[1] = 0x0;

#ifdef ID3ALL
	id3_tag->genre[0] = 0x0;
	id3_tag->genre[1] = 0x0;
	id3_tag->year[0] = 0x0;
	id3_tag->year[1] = 0x0;
#endif

	for(i=0; i<= ID3V1_READ_SIZE-sizeof(ID3V1_TAG); i++)
	{
		if ( (buf[i] == 0x54) && (buf[i+1] == 0x41) && (buf[i+2] == 0x47) )
		{
			/* TAG */
			pId3v1_tag = (ID3V1_TAG *) &buf[i];
			strncpy ( (I8 *) id3_tag->title, (const I8 *) pId3v1_tag->title, 30);
			id3_tag->title [15] = 0x0;									//word
			strCutSpace ((U8 *) id3_tag->title);
			strAddUnicodeNull ((U8 *) id3_tag->title);
			
			strncpy ( (I8 *) id3_tag->artist, (const I8 *) pId3v1_tag->artist, 30);
			id3_tag->artist [15] = 0x0;									//word
			strCutSpace ((U8 *) id3_tag->artist);
			strAddUnicodeNull ((U8 *) id3_tag->artist);

			strncpy ((I8 *) id3_tag->album, (const I8 *)pId3v1_tag->album, 30);			
			id3_tag->album [15] = 0x0;									//word
			strCutSpace ((U8 *) id3_tag->album);
			strAddUnicodeNull ((U8 *) id3_tag->album);

#ifdef ID3ALL
			strncpy ((I8 *) id3_tag->year, (const I8 *)pId3v1_tag->year, 4);			
			id3_tag->year [2] = 0x0;										//word
			strCutSpace ((U8 *) id3_tag->year);
			strAddUnicodeNull ((U8 *) id3_tag->year);

			genre = pId3v1_tag->genre;

			if (genre >= KNOWN_MP3_GENRES) {
				id3_tag->genre[0] = 0x0;
				id3_tag->genre[1] = 0x0;
			}
			else {
				strncpy ((I8 *)id3_tag->genre, (const I8 *) mp3_genre[genre], 2*MAX_ID3_GENRE_TEXT);
				id3_tag->genre [MAX_ID3_GENRE_TEXT] = 0x0;				//word
			}

			strAddUnicodeNull ((U8 *) id3_tag->genre);
#endif

			return ID3_TAG_OK;
		}
	}

	return NO_ID3_TAG;
}


ID3_RETURN ID3v1_process (ID3_TAG *id3_tag)
{
#if 1
	U8 *buf;
	int ret, i, j;
	MYFILE *pFile = app_main_data.pMediaRead;			//no need file handle, just pass one parameter

	codec_malloc_init ();

#if 1
	buf = (U8 *)codec_malloc(0x4096);
	//DBG_Assert (buf != NULL);
	if (buf == NULL)
	{
		DBG_Puts("malloc err\n\r");
	}

	ret = xa_fseek (pFile, 4096, 2); //2 SEEK_END
	if (ret == -1)
	{
		return NO_ID3_TAG;
	}
	
	ret = xa_fread (buf, 1, 4096, pFile);
	if(ret == STREAM_UNDERFLOW)
	{
		DBG_Puts("xa_fread err\n\r");
	}

	ret = NO_ID3_TAG;

	for (i=0; i <(4096 - 3); i++)
	{
		if ( (buf[i] == 0x54) && (buf[i+1] == 0x41) && (buf[i+2] == 0x47) )
		{
			ret = id3v1_parser (&buf[i], id3_tag);	

			if (ret == ID3_TAG_OK)
			{
				break;
			}
		}
	}

#else

	buf = (U8 *)codec_malloc(2048+8);
//	DBG_Assert (buf != NULL);
	if (buf == NULL)
	{
		DBG_Puts("malloc err\n\r");
	}

	ret = xa_fseek (pFile, 4096, 2);			//2 SEEK_END
	if (ret == -1)
	{
		return NO_ID3_TAG;
	}
	
	ret = NO_ID3_TAG;

	xa_fread (buf, 1, 2048+2, pFile);
	for (i=0; i <2048; i++)
	{
		if ( (buf[i] == 0x54) && (buf[i+1] == 0x41) && (buf[i+2] == 0x47) )
		{
			ret = id3v1_parser (&buf[i], id3_tag);	
		}
	}
	xa_fread (buf+2, 1, 2048-2, pFile);
	for (i=0; i <(2048 - 2); i++)
	{
		if ( (buf[i] == 0x54) && (buf[i+1] == 0x41) && (buf[i+2] == 0x47) )
		{
			ret = id3v1_parser (&buf[i], id3_tag);	
		}
	}	
#endif

	xa_fseek (pFile, 0, 0);				//jump to the file start pos

	return ret;

#else

	U8 *buf;
	ID3_RETURN r;
	U32 len;

	codec_malloc_init ();
	buf = (U8 *)codec_malloc(ID3V1_READ_SIZE);

	app_nav_lock_stream_mutex ();//force the hdd to idle state
				
	Fs_file_seek(app_main_data.pMediaRead , app_main_data.pMediaRead->lenth - ID3V1_READ_SIZE);
	len = Fs_read_file(buf, ID3V1_READ_SIZE, app_main_data.pMediaRead);

	app_nav_unlock_stream_mutex ();

	r = id3v1_process1 (buf, id3_tag);

//back to the stream start pos
#ifdef FOR_ESD_PROTECT
	if (resume_trk_info.need_resume_play)
	{
		app_cmd_jmp(resume_trk_info.resume_pos);
	}
	else
	{
#endif
		app_cmd_jmp(0);
	}

	return r;
#endif
}



#if 0

typedef struct {
	U64	ID[2];
	U64	Size;
} sObjGUID;

/*
typedef struct {
	sObjGUID GUID;
	U32  NumberofHeaderObjects;
	U16   Reserved;
	
} __attribute__((packed)) sHeaderObject;
*/

typedef struct {
	U8	ObjectID[16];
	U32	ObjectSize[2];
	U32  NumHeadObjects;
	U8   Rev[2];
} __attribute__((packed)) sHeaderObject;


typedef struct {
	U8	ObjectID[16];
	U32	ObjectSize[2];
} __attribute__((packed)) sObject;

typedef struct {
	U8	ObjectID[16];
	U32	ObjectSize[2];
	U16	Title_Len;
	U16 Author_Len;
	U16 Copyright_Len;
	U16 Discription_Len;
	U16 Rating_Len;
} __attribute__((packed)) sContentObject;

typedef struct {
	U8	ObjectID[16];
	U32	ObjectSize[2];
	U16	ContentCount;
} __attribute__((packed)) sExtendContentObject;


#define WM_GENRE_LEN	18
#define WM_ALBUM_LEN	28
	
//static const U16 WM_Genre[WM_GENRE_LEN] = {'W'<<8,'M'<<8,0x2F<<8,'G'<<8,'e'<<8,'n'<<8,'r'<<8,'e'<<8,0x0};
//static const U16 WM_Album[WM_ALBUM_LEN] = {'W'<<8,'M'<<8,0x2F<<8,'A'<<8,'l'<<8,'b'<<8,'u'<<8,'m'<<8,'T'<<8,'i'<<8,'t'<<8,'l'<<8,'e'<<8,0x0};

static const U8 ASF_Header_Object[]={0x30,0x26,0xB2,0x75,0x8E,0x66,0xCF,0x11,0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C};
static const U8 ASF_Content_Description_Object[]={0x33,0x26,0xB2,0x75,0x8E,0x66,0xCF,0x11,0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C};
static const U8 ASF_Extended_Content_Description_Object[]={0x40,0xA4,0xD0,0xD2,0x07,0xE3,0xD2,0x11,0x97,0xF0,0x00,0xA0,0xC9,0x5E,0xA8,0x50};
static const U8 ASF_File_Properties_Object[]={0xa1,0xdc,0xab,0x8c,0x47,0xa9,0xCF,0x11,0x8e,0xe4,0x00,0xC0,0x0C,0x20,0x53,0x65};


static U16 GUID_compare (const U8 *pGuid, U8 *pBuf)
{
	U16 i,ret = TRUE;

	for (i=0; i<16; i++) {
		if (pGuid[i] != pBuf[i]) {
			ret = FALSE;
			break;
		}	
	}

	return ret;
}


ID3_RETURN WMA_ID3_parse(U16 *total_time, ID3_TAG *id3_tag, MYFILE *pFile )
{
	U32	i,k,len;
	U8 *m_buf;
	U16 m_buf16[64]; /* 64 bytes */
	int ret,j;
	U16	wHeaderObjectHits;
	U16 Hits;


	U32 dwHeaderLen,dwHeaderObjectPos,dwHeaderObjectLen,dwNumberofHeaderObjects;

	DBG_Assert (pFile!= NULL);

	memset (id3_tag,0,sizeof (ID3_TAG));

	m_buf = (U8 *)m_buf16;
	DBG_Assert(((U32)m_buf & 1) == 0);

	
	/*init id3 tag*/
	id3_tag->title[0] = 0x0;
	id3_tag->artist[0] = 0x0;
	id3_tag->album[0] = 0x0;

#ifdef ID3ALL
	
	id3_tag->genre[0] = 0x0;
	id3_tag->year[0] = 0x0;

#endif	

	*total_time = 0;

	
	/*parse header object*/
	//len = Fs_read_file (m_buf, sizeof(sHeaderObject), pFile);	
	len = xa_fread (m_buf, 1, sizeof(sHeaderObject), pFile);

	if (len != sizeof (sHeaderObject) ) {
		return NO_ID3_TAG;
	}

	if ( (GUID_compare (ASF_Header_Object, m_buf)) == FALSE) {
		/*the first object is not header object*/
		return NO_ID3_TAG;		
	}

	if ( ((sHeaderObject *) m_buf)->ObjectSize[1] != 0 ) {
		/*Header object is too bigger*/
		return NO_ID3_TAG;			
	}

#ifdef LITTLE_ENDIAN

	dwHeaderLen = ((sHeaderObject *) m_buf)->ObjectSize[0];
	dwNumberofHeaderObjects = ((sHeaderObject *) m_buf)->NumHeadObjects;

#else
	
	dwHeaderLen = w_Little_Big_Endian(&(((sHeaderObject *) m_buf)->ObjectSize[0]));
	dwNumberofHeaderObjects = w_Little_Big_Endian(&(((sHeaderObject *) m_buf)->NumHeadObjects));

#endif

	wHeaderObjectHits = 0;

	for (i=0; i<dwNumberofHeaderObjects; i++) {
	
		//len = Fs_read_file (m_buf, sizeof (sObject), pFile);
		len = xa_fread (m_buf, 1, sizeof (sObject), pFile);
		if (len != sizeof (sObject) ) {
			return NO_ID3_TAG;
		}

		if ( ((sHeaderObject *) m_buf)->ObjectSize[1] != 0 ) {
			/*Header object is too bigger*/
			return NO_ID3_TAG;			
		}


#ifdef LITTLE_ENDIAN

		dwHeaderObjectLen = ((sHeaderObject *) m_buf)->ObjectSize[0];

#else
		
		dwHeaderObjectLen = w_Little_Big_Endian(&(((sHeaderObject *) m_buf)->ObjectSize[0]));

#endif

 
		//dwHeaderObjectPos = Fs_file_tell (pFile);
		dwHeaderObjectPos = xa_ftell (pFile);

		if (dwHeaderObjectLen < 24) {
			return NO_ID3_TAG;			
		}

		//if ( (pFile->pos + (dwHeaderObjectLen-24)) > dwHeaderLen ) {
		if ( (dwHeaderObjectPos + (dwHeaderObjectLen-24)) > dwHeaderLen ) {
			break;			
		}

//--------------------------------------------------------------------------------------
		if ( (GUID_compare (ASF_Content_Description_Object, m_buf)) == TRUE) {

			U32	wTitleLen,wAthorLen,wCopyrightLength,wDescriptionLength,wRatingLength;
			U32 dwContentDescriptionPos;

			/*the first object is not header object*/
			wHeaderObjectHits++;
			//len = Fs_read_file (&m_buf[24], (sizeof (sContentObject)-24), pFile);
			len = xa_fread (&m_buf[24], 1, (sizeof (sContentObject)-24), pFile);
			if (len != (sizeof (sContentObject)-24) ) {
				return NO_ID3_TAG;
			}

			//dwContentDescriptionPos = Fs_file_tell (pFile);
			dwContentDescriptionPos = xa_ftell (pFile);


#ifdef LITTLE_ENDIAN
			wTitleLen = ((sContentObject *) m_buf)->Title_Len;
			wAthorLen = ((sContentObject *) m_buf)->Author_Len;
			wCopyrightLength = ((sContentObject *) m_buf)->Copyright_Len;
			wDescriptionLength = ((sContentObject *) m_buf)->Discription_Len;
			wRatingLength = ((sContentObject *) m_buf)->Rating_Len;
#else
			wTitleLen = w_Little_Big_Endian(&(((sContentObject *) m_buf)->Title_Len));
			wAthorLen = w_Little_Big_Endian(&(((sContentObject *) m_buf)->Author_Len));
			wCopyrightLength = w_Little_Big_Endian(&(((sContentObject *) m_buf)->Copyright_Len));
			wDescriptionLength = w_Little_Big_Endian(&(((sContentObject *) m_buf)->Discription_Len));
			wRatingLength = w_Little_Big_Endian(&(((sContentObject *) m_buf)->Rating_Len));
#endif
			
			len = wTitleLen;
			
			if (len > 2*MAX_ID3_TEXT)
					len = 2*MAX_ID3_TEXT;

			//Fs_read_file ( (BYTE *) id3_tag->title, len, pFile);
			len = xa_fread ((BYTE *) id3_tag->title, 1, len, pFile);
			if (len == 0) {
				return NO_ID3_TAG;
			}

			/*Unicode*/
			len = (wTitleLen+1)/2;
			if (len > MAX_ID3_TEXT)
					len = MAX_ID3_TEXT;

			id3_tag->title[len] = 0x0;								/*Null*/

#ifndef LITTLE_ENDIAN
			for (j=0; j<len;j++) {
				id3_tag->title[j] = w_Little_Big_Endian(&(id3_tag->title[j]));
			}
#endif
		
			wstrCutLeadingSpace ( id3_tag->title);
			wstrCutTrailingSpace ( id3_tag->title);

			if ( (dwContentDescriptionPos + wTitleLen) > dwHeaderLen ) {
				break;			
			}

			//Fs_file_seek (pFile, (dwContentDescriptionPos + wTitleLen) );
			ret = xa_fseek(pFile, (dwContentDescriptionPos + wTitleLen),SEEK_SET);
			if (ret == -1) {
				return NO_ID3_TAG;
			}

			len = wAthorLen;
			
			if (len > 2*MAX_ID3_TEXT)
					len = 2*MAX_ID3_TEXT;

			//Fs_read_file ( (BYTE *) id3_tag->artist, len, pFile);
			len = xa_fread ((BYTE *) id3_tag->artist, 1, len, pFile);
			if (len == 0) {
				return NO_ID3_TAG;
			}


			/*Unicode*/
			len = (wAthorLen+1)/2;
			if (len > MAX_ID3_TEXT)
					len = MAX_ID3_TEXT;

			id3_tag->artist[len] = 0x0;								/*Null*/

#ifndef LITTLE_ENDIAN
			for (j=0; j<len;j++) {
				id3_tag->artist[j] = w_Little_Big_Endian(&(id3_tag->artist[j]));
			}
#endif
		
			wstrCutLeadingSpace ( id3_tag->artist);
			wstrCutTrailingSpace ( id3_tag->artist);
			
		}
		else if ( (GUID_compare (ASF_Extended_Content_Description_Object, m_buf)) == TRUE) {
			
			U16 wContentDescriptorsCount,wDescriptorNameLength,wDescriptorValueDataType,wDescriptorValueLength;
			U16	wContentDescriptorsHits;
			U32 dwContentDescriptorsPos;
			U16 wContentType;
			
			/*the first object is not header object*/
			wHeaderObjectHits++;


			wContentDescriptorsHits = 0;

			//len = Fs_read_file (m_buf, 2, pFile);
			len = xa_fread (m_buf, 1, 2, pFile);
			if ( len != 2 ) {
				return NO_ID3_TAG;
			}

#ifdef LITTLE_ENDIAN
			wContentDescriptorsCount = *((U16 *) m_buf);
#else			
			wContentDescriptorsCount = w_Little_Big_Endian(m_buf);
#endif


			Hits = 0;

			for (k=0; k<wContentDescriptorsCount; k++) {

				//len = Fs_read_file (m_buf, 2, pFile);
				len = xa_fread (m_buf, 1, 2, pFile);
				if ( len != 2 ) {
					return NO_ID3_TAG;
				}

#ifdef LITTLE_ENDIAN
				wDescriptorNameLength = *((U16 *) m_buf);
#else				
				wDescriptorNameLength = w_Little_Big_Endian(m_buf);
#endif

				memset (m_buf,0,sizeof(m_buf));
				len = xa_fread (m_buf, 1, wDescriptorNameLength, pFile);
				if ( len != wDescriptorNameLength ) {
					return NO_ID3_TAG;
				}

				wContentType = Other_Content;

				if((Hits & 0x01)==0){
					if(memcmp(m_buf,WM_AlbumTitle,wDescriptorNameLength)==0){
						wContentType = Album_Content;
						Hits |= 0x01;
					}
				}

				if((Hits & 0x02)==0){
					if(memcmp(m_buf,WM_Genre,wDescriptorNameLength)==0){
						wContentType = Genre_Content;
						Hits |= 0x02;
					}
				}
		
				if((Hits & 0x04)==0){
					if(memcmp(m_buf,WM_Year,wDescriptorNameLength)==0){
						wContentType = Year_Content;
						Hits |= 0x04;
					}
				}

#if 0

				//dwContentDescriptorsPos = Fs_file_tell (pFile);
				dwContentDescriptorsPos = xa_ftell (pFile);

				wContentType = Other_Content;

				if ( (wDescriptorNameLength == WM_GENRE_LEN) || 
					(wDescriptorNameLength == WM_ALBUM_LEN) )
				{
				
					if (wDescriptorNameLength < (sizeof(m_buf16) - 2) )
					{
						//len = Fs_read_file ( m_buf, wDescriptorNameLength, pFile);			
						len = xa_fread (m_buf, 1, wDescriptorNameLength, pFile);
						if (len == 0) {
							return NO_ID3_TAG;
						}
				
					}

					/*unicoe null*/
					m_buf [sizeof(m_buf16) - 2] = 0x0;
					m_buf [sizeof(m_buf16) - 1] = 0x0;
					

					if ( (wstrcmp ((U16 *)m_buf, (U16 *)WM_Album)) == 0 ) {
						/*album content*/
						wContentType = Album_Content;
					}
					else if ( (wstrcmp ((U16 *)m_buf, (U16 *)WM_Genre)) == 0 ) {
						/*Genre content*/
						wContentType = Genre_Content;
					}

				}

				
				if ( (dwContentDescriptorsPos+wDescriptorNameLength) > dwHeaderLen ) {
					break;			
				}

				//Fs_file_seek (pFile,(dwContentDescriptorsPos+wDescriptorNameLength));
				ret = xa_fseek(pFile,(dwContentDescriptorsPos+wDescriptorNameLength),SEEK_SET);
				if (ret == -1) {
					return NO_ID3_TAG;
				}

#endif

				//len = Fs_read_file ( m_buf, 2, pFile);
				len = xa_fread (m_buf, 1, 2, pFile);
				if ( len != 2 ) {
					return NO_ID3_TAG;
				}

#ifdef LITTLE_ENDIAN
				wDescriptorValueDataType = *((U16 *) m_buf);
#else				
				wDescriptorValueDataType = w_Little_Big_Endian(m_buf);
#endif


				//len = Fs_read_file (m_buf, 2, pFile);
				len = xa_fread (m_buf, 1, 2, pFile);
				if ( len != 2 ) {
					return NO_ID3_TAG;
				}

				DBG_Assert(((U32)m_buf & 1) == 0);
#ifdef LITTLE_ENDIAN	
				wDescriptorValueLength = *((U16 *) m_buf);
#else
				wDescriptorValueLength = w_Little_Big_Endian(m_buf);
#endif
				
				//dwContentDescriptorsPos = Fs_file_tell (pFile);
				dwContentDescriptorsPos = xa_ftell (pFile);
				

				if (wContentType == Album_Content) {

					wContentDescriptorsHits++;

					if (wDescriptorValueDataType == 0) {
						/*unicode string*/

						len = wDescriptorValueLength;
						if (len > 2*MAX_ID3_TEXT)
							len = 2*MAX_ID3_TEXT;

						//Fs_read_file ( (BYTE *) id3_tag->album, len, pFile);
						len = xa_fread ((BYTE *) id3_tag->album, 1, len, pFile);
						if (len == 0) {
							return NO_ID3_TAG;
						}
			
						/*Unicode*/
						len = (wDescriptorValueLength+1)/2;
						if (len > MAX_ID3_TEXT)
							len = MAX_ID3_TEXT;
						
						id3_tag->album[len] = 0x0;								/*Null*/

#ifndef LITTLE_ENDIAN
						for (j=0; j<len;j++) {
							id3_tag->album[j] = w_Little_Big_Endian(&(id3_tag->album[j]));
						}
#endif
		
						wstrCutLeadingSpace ( id3_tag->album);
						wstrCutTrailingSpace ( id3_tag->album);
					}
					else if (wDescriptorValueDataType == 1) {

						/*Byte string*/
						len = wDescriptorValueLength;
						if (len > sizeof (m_buf16) )
							len = sizeof (m_buf16);

						//Fs_read_file (m_buf, len, pFile);
						len = xa_fread (m_buf, 1, len, pFile);
						if (len == 0) {
							return NO_ID3_TAG;
						}

						if (len > MAX_ID3_TEXT)
							len = MAX_ID3_TEXT;

						for (j=0; j<len;j++) {
							id3_tag->album[j] = m_buf[j];
						}

						id3_tag->album[j] = 0x0;
		
						wstrCutLeadingSpace ( id3_tag->album);
						wstrCutTrailingSpace ( id3_tag->album);					
					}
				
				}
				else if (wContentType == Genre_Content) {

#ifdef ID3ALL

					wContentDescriptorsHits++;


					if (wDescriptorValueDataType == 0) {
						/*unicode string*/
						len = wDescriptorValueLength;
						if (len > 2*MAX_ID3_GENRE_TEXT)
							len = 2*MAX_ID3_GENRE_TEXT;

						//Fs_read_file ( (BYTE *) id3_tag->genre, len, pFile);
						len = xa_fread ((BYTE *) id3_tag->genre, 1, len, pFile);
						if (len == 0) {
							return NO_ID3_TAG;
						}

						/*Unicode*/
						len = (wDescriptorValueLength+1)/2;
						if (len > MAX_ID3_GENRE_TEXT)
							len = MAX_ID3_GENRE_TEXT;


						id3_tag->genre[len] = 0x0;								/*Null*/

#ifndef LITTLE_ENDIAN
						for (j=0; j<len;j++) {
							id3_tag->genre[j] = w_Little_Big_Endian(&(id3_tag->genre[j]));
						}
#endif
		
						wstrCutLeadingSpace ( id3_tag->genre);
						wstrCutTrailingSpace ( id3_tag->genre);


					}
					else if (wDescriptorValueDataType == 1) {

						/*Byte string*/
						len = wDescriptorValueLength;
						if (len > sizeof (m_buf16) )
							len = sizeof (m_buf16);

						//Fs_read_file (m_buf, len, pFile);
						len = xa_fread (m_buf, 1, len, pFile);
						if (len == 0) {
							return NO_ID3_TAG;
						}
       
						if (len > MAX_ID3_GENRE_TEXT)
							len = MAX_ID3_GENRE_TEXT;

						for (j=0; j<len;j++) {
							id3_tag->genre[j] = m_buf[j];
						}

						id3_tag->genre[j] = 0x0;
		
						wstrCutLeadingSpace ( id3_tag->genre);
						wstrCutTrailingSpace ( id3_tag->genre);					
					}
#endif


				}
				else if (wContentType == Year_Content) {

#ifdef ID3ALL

					wContentDescriptorsHits++;


					if (wDescriptorValueDataType == 0) {
						/*unicode string*/
						len = wDescriptorValueLength;
						if (len > 2*MAX_ID3_GENRE_TEXT)
							len = 2*MAX_ID3_GENRE_TEXT;

						//Fs_read_file ( (BYTE *) id3_tag->genre, len, pFile);
						len = xa_fread ((BYTE *) id3_tag->year, 1, len, pFile);
						if (len == 0) {
							return NO_ID3_TAG;
						}

						/*Unicode*/
						len = (wDescriptorValueLength+1)/2;
						if (len > MAX_ID3_YEAR_TEXT)
							len = MAX_ID3_YEAR_TEXT;


						id3_tag->year[len] = 0x0;								/*Null*/

#ifndef LITTLE_ENDIAN
						for (j=0; j<len;j++) {
							id3_tag->year[j] = w_Little_Big_Endian(&(id3_tag->year[j]));
						}
#endif
		
						wstrCutLeadingSpace ( id3_tag->year);
						wstrCutTrailingSpace ( id3_tag->year);


					}
					else if (wDescriptorValueDataType == 1) {

						/*Byte string*/
						len = wDescriptorValueLength;
						if (len > sizeof (m_buf16) )
							len = sizeof (m_buf16);

						//Fs_read_file (m_buf, len, pFile);
						len = xa_fread (m_buf, 1, len, pFile);
						if (len == 0) {
							return NO_ID3_TAG;
						}

						if (len > MAX_ID3_YEAR_TEXT)
							len = MAX_ID3_YEAR_TEXT;

						for (j=0; j<len;j++) {
							id3_tag->year[j] = m_buf[j];
						}

						id3_tag->year[j] = 0x0;
		
						wstrCutLeadingSpace ( id3_tag->year);
						wstrCutTrailingSpace ( id3_tag->year);					
					}
#endif
				}

				
				if (wContentDescriptorsHits >= 2)
					break;

				if ((dwContentDescriptorsPos+wDescriptorNameLength) > dwHeaderLen)
					break;

				//Fs_file_seek (pFile,(dwContentDescriptorsPos+wDescriptorValueLength));
				ret = xa_fseek(pFile,(dwContentDescriptorsPos+wDescriptorValueLength),SEEK_SET);
				if (ret == -1) {
					return NO_ID3_TAG;
				}

			}

		}
//--------------------------------------------------------------------------------------
		else if ( (GUID_compare (ASF_File_Properties_Object, m_buf)) == TRUE) {
			U64 PlayDuration,Preroll;
			U32 total_playtime_ms;
#if 0			
			sFilePropertiesObject *pfile;
#endif			
			wHeaderObjectHits++;

			//Fs_file_seek (pFile, Fs_file_tell (pFile)+4*sizeof(U32)+3*sizeof(U64));//skip no use infomation
			ret = xa_fseek(pFile,(xa_ftell(pFile)+4*sizeof(U32)+3*sizeof(U64)),SEEK_SET);
			if (ret == -1) {
				return NO_ID3_TAG;			
			}

			//Fs_read_file ((BYTE *)&PlayDuration, sizeof(U64), pFile);
			len = xa_fread ((BYTE *)&PlayDuration, 1, sizeof(U64), pFile);
			if (len == 0) {
				return NO_ID3_TAG;
			}
			//Fs_file_seek (pFile, Fs_file_tell (pFile)+sizeof(U64));//skip no use infomation
			ret = xa_fseek(pFile,(xa_ftell(pFile)+sizeof(U64)),SEEK_SET);
			if (ret == -1) {
				return NO_ID3_TAG;			
			}

			//Fs_read_file ((BYTE *)&Preroll, sizeof(U64), pFile);
			len = xa_fread ((BYTE *)&Preroll, 1, sizeof(U64), pFile);
			if (len == 0) {
				return NO_ID3_TAG;
			}

			total_playtime_ms = PlayDuration / 10000 - Preroll;//ms
			*total_time = total_playtime_ms/1000;
		}		

//--------------------------------------------------------------------------------------
		if (wHeaderObjectHits >= 3) {
			break;
		}
		
		//Fs_file_seek (pFile, (dwHeaderObjectPos+(dwHeaderObjectLen-24)));	
		ret = xa_fseek(pFile,(dwHeaderObjectPos+(dwHeaderObjectLen-24)),SEEK_SET);
		if (ret == -1) {
				return NO_ID3_TAG;		
		}

	}	
	
	return ID3_TAG_OK;	
}

#endif //0

#endif