forked from KapLex/PGE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgeMp3.c
1297 lines (1032 loc) · 31.9 KB
/
pgeMp3.c
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
/*
* This file is part of "Phoenix Game Engine".
*
* Copyright (C) 2008 Phoenix Game Engine
* Copyright (C) 2008 InsertWittyName <[email protected]>
* Copyright (C) 2008 MK2k <[email protected]>
*
* Phoenix Game Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Phoenix Game Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Phoenix Game Engine. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <pspkernel.h>
#include <pspsdk.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pspaudiocodec.h>
#include <pspaudio.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <pspdisplay.h>
#include <psppower.h>
#include "pgeMp3.h"
#include "pgeFile.h"
#include "pge.h"
#define PGE_MP3_MIN_INFRAME_SIZE (96)
#define PGE_MP3_READ_SIZE (2*1024)
#define PGE_MP3_IN_BUFFER_SIZE (1152 * 64)
#define PGE_MP3_BUFFER1_EMPTY (0x00000001)
#define PGE_MP3_BUFFER2_EMPTY (0x00000010)
#define PGE_MP3_BUFFER3_EMPTY (0x00000100)
#define PGE_MP3_RESUMED (0x00001000)
#define PGE_MP3_SUSPENDING (0x00001000)
static int pgeMp3LastError = 0;
static int pgeMp3Initialized = 0;
static int pgeMp3ThreadExit = 0;
static int pgeMp3Looping = 0;
static int pgeMp3JobStarted = 0;
static int pgeMp3VolumeValue = 0x8000;
static int pgeMp3SamplesReady = 0;
static int pgeMp3BufferOffset = 0;
static int pgeMp3PlayBufferSel = 0;
static int pgeMp3OutputIndex = 0;
static int pgeMp3WorkingBuffer = 0;
static const char *pgeMp3Filename = NULL;
static pgeFile *pgeMp3Handle = NULL;
static int pgeMp3DataPos = 0;
static int pgeMp3DataEnd = 0;
static int pgeMp3DataStart = 0;
static int pgeMp3SampleRate = 0;
static int pgeMp3AudioChannel = -1;
static int pgeMp3Playing = 0;
static int pgeMp3Paused = 0;
static int pgeMp3DecodeThreadId = -1;
static int pgeMp3ReadThreadId = -1;
static int pgeMp3BufferSize[2];
static int bufferempty = 0;
static int pgeMp3NumFrames = 0;
static int pgeMp3DecodeCurrentFrame = 0;
static int pgeMp3SamplesPerFrame = 0;
static int pgeMp3ShouldExit = 0;
static int pgeMp3ReadCurrentBuffer = 0;
static int pgeMp3ReadNumFrames = 0;
static int pgeMp3DecodeReady = 0;
static pgeMp3Info pgeMp3Id3TagInfo;
static int pgeMp3EventFlag = -1;
static int samplerates[4][3] =
{
{11025, 12000, 8000,}, //MPEG 2.5
{0, 0, 0,}, //reserved
{22050, 24000, 16000,}, //MPEG 2
{44100, 48000, 32000} //MPEG 1
};
static int bitrates[] = {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320};
static int bitrates_v2[] = {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160};
static unsigned long pgeMp3CodecStruct[65] __attribute__((aligned(64)));
static short pgeMp3SrcBuffer[3][PGE_MP3_IN_BUFFER_SIZE*2] __attribute__((aligned(64)));
static short pgeMp3OutputBuffer[4][1152*2] __attribute__((aligned(64)));
static unsigned char pgeMp3ReadBuffer[PGE_MP3_READ_SIZE]__attribute__((aligned(64)));
typedef void *pgeMp3Id3Tag;
typedef struct
{
int code;
char *text;
} pgeMp3Genre;
static pgeMp3Genre pgeMp3GenreList[] =
{
{0 , "Blues"}, {1 , "Classic Rock"}, {2 , "Country"}, {3 , "Dance"}, {4 , "Disco"}, {5 , "Funk"}, {6 , "Grunge"}, {7 , "Hip-Hop"}, {8 , "Jazz"}, {9 , "Metal"}, {10 , "New Age"},
{11 , "Oldies"}, {12 , "Other"}, {13 , "Pop"}, {14 , "R&B"}, {15 , "Rap"}, {16 , "Reggae"}, {17 , "Rock"}, {18 , "Techno"}, {19 , "Industrial"}, {20 , "Alternative"},
{21 , "Ska"}, {22 , "Death Metal"}, {23 , "Pranks"}, {24 , "Soundtrack"}, {25 , "Euro-Techno"}, {26 , "Ambient"}, {27 , "Trip-Hop"}, {28 , "Vocal"}, {29 , "Jazz+Funk"}, {30 , "Fusion"},
{31 , "Trance"}, {32 , "Classical"}, {33 , "Instrumental"}, {34 , "Acid"}, {35 , "House"}, {36 , "Game"}, {37 , "Sound Clip"}, {38 , "Gospel"}, {39 , "Noise"}, {40 , "Alternative Rock"},
{41 , "Bass"}, {42 , "Soul"}, {43 , "Punk"}, {44 , "Space"}, {45 , "Meditative"}, {46 , "Instrumental Pop"}, {47 , "Instrumental Rock"}, {48 , "Ethnic"}, {49 , "Gothic"}, {50 , "Darkwave"},
{51 , "Techno-Industrial"}, {52 , "Electronic"}, {53 , "Pop-Folk"}, {54 , "Eurodance"}, {55 , "Dream"}, {56 , "Southern Rock"}, {57 , "Comedy"}, {58 , "Cult"}, {59 , "Gangsta"}, {60 , "Top 40"},
{61 , "Christian Rap"}, {62 , "Pop/Funk"}, {63 , "Jungle"}, {64 , "Native US"}, {65 , "Cabaret"}, {66 , "New Wave"}, {67 , "Psychadelic"}, {68 , "Rave"}, {69 , "Showtunes"}, {70 , "Trailer"},
{71 , "Lo-Fi"}, {72 , "Tribal"}, {73 , "Acid Punk"}, {74 , "Acid Jazz"}, {75 , "Polka"}, {76 , "Retro"}, {77 , "Musical"}, {78 , "Rock & Roll"}, {79 , "Hard Rock"}, {80 , "Folk"},
{81 , "Folk-Rock"}, {82 , "National Folk"}, {83 , "Swing"}, {84 , "Fast Fusion"}, {85 , "Bebob"}, {86 , "Latin"}, {87 , "Revival"}, {88 , "Celtic"}, {89 , "Bluegrass"}, {90 , "Avantgarde"},
{91 , "Gothic Rock"}, {92 , "Progressive Rock"}, {93 , "Psychedelic Rock"}, {94 , "Symphonic Rock"}, {95 , "Slow Rock"}, {96 , "Big Band"}, {97 , "Chorus"}, {98 , "Easy Listening"}, {99 , "Acoustic"},
{100 , "Humour"}, {101 , "Speech"}, {102 , "Chanson"}, {103 , "Opera"}, {104 , "Chamber Music"}, {105 , "Sonata"}, {106 , "Symphony"}, {107 , "Booty Bass"}, {108 , "Primus"}, {109 , "Porn Groove"},
{110 , "Satire"}, {111 , "Slow Jam"}, {112 , "Club"}, {113 , "Tango"}, {114 , "Samba"}, {115 , "Folklore"}, {116 , "Ballad"}, {117 , "Power Ballad"}, {118 , "Rhytmic Soul"}, {119 , "freestyle"}, {120 , "Duet"},
{121 , "Punk Rock"}, {122 , "Drum Solo"}, {123 , "Acapella"}, {124 , "Euro-House"}, {125 , "Dance Hall"}, {126 , "Goa"}, {127 , "Drum & Bass"}, {128 , "Club-House"}, {129 , "Hardcore"}, {130 , "Terror"},
{131 , "Indie"}, {132 , "BritPop"}, {133 , "Negerpunk"}, {134 , "Polsk Punk"}, {135 , "Beat"}, {136 , "Christian Gangsta"}, {137 , "Heavy Metal"}, {138 , "Black Metal"}, {139 , "Crossover"}, {140 , "Contemporary C"},
{141 , "Christian Rock"}, {142 , "Merengue"}, {143 , "Salsa"}, {144 , "Thrash Metal"}, {145 , "Anime"}, {146 , "JPop"}, {147 , "SynthPop"}
};
static int pgeMp3GenreCount = sizeof(pgeMp3GenreList) / sizeof(pgeMp3Genre);
static int pgeMp3SeekPastNull(pgeFile *file)
{
char buffer;
int i = 0;
while(pgeRunning())
{
pgeFileRead(file, &buffer, 1);
i++;
if(buffer == '\0')
break;
}
return i;
}
static short swapInt16BigToHost(short arg)
{
short i = 0;
int checkendian = 1;
if(1 == *(char *)&checkendian)
{
// Little endian
i = arg;
i = ((i&0xFF00)>>8)|((i&0x00FF)<<8);
}
else
{
// Big endian
i = arg;
}
return i;
}
static int swapInt32BigToHost(int arg)
{
int i = 0;
int checkendian = 1;
if(1 == *(char *)&checkendian)
{
// Intel (little endian)
i = arg;
i = ((i&0xFF000000)>>24)|((i&0x00FF0000)>>8)|((i&0x0000FF00)<<8)|((i&0x000000FF)<<24);
}
else
{
// PPC (big endian)
i = arg;
}
return i;
}
static int ID3v2TagSize(void)
{
int size;
char sig[3];
pgeFileSeek(pgeMp3Handle, 0, PGE_FILE_SET);
pgeFileRead(pgeMp3Handle, sig, 3);
if(strncmp("ID3", sig, 3) != 0)
return 0;
pgeFileSeek(pgeMp3Handle, 6, PGE_FILE_SET);
pgeFileRead(pgeMp3Handle, &size, 4);
size = (unsigned int) swapInt32BigToHost((int)size);
size = ( ( (size & 0x7f000000) >> 3 ) | ( (size & 0x7f0000) >> 2 ) | ( (size & 0x7f00) >> 1 ) | (size & 0x7f) );
return size;
}
static int ID3v2(void)
{
char sig[3];
unsigned short version;
pgeFileSeek(pgeMp3Handle, 0, PGE_FILE_SET);
pgeFileRead(pgeMp3Handle, sig, 3);
if(!strncmp("ID3", sig, 3))
{
pgeFileRead(pgeMp3Handle, &version, 2);
version = (unsigned short int) swapInt16BigToHost((short int)version);
version /= 256;
}
return (int)version;
}
static void ParseID3v2_2(pgeMp3Info *id3tag)
{
int size;
int tag_length;
char tag[3];
int skip;
if(ID3v2() == 2)
{
size = ID3v2TagSize();
pgeFileSeek(pgeMp3Handle, 10, PGE_FILE_SET);
while(size != 0)
{
pgeFileRead(pgeMp3Handle, tag, 3);
size -= 3;
pgeFileRead(pgeMp3Handle, &tag_length, 4);
pgeFileSeek(pgeMp3Handle, -1, PGE_FILE_CUR);
tag_length = (unsigned int) swapInt32BigToHost((int)tag_length);
tag_length = (tag_length / 256);
size -= 3;
if(*tag == 0 || tag_length > size || tag_length == 0)
break;
if(!strncmp("TP1", tag, 3)) /* Artist */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->artist = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->artist, tag_length - 1);
id3tag->artist[tag_length - 1] = '\0';
}
else if(!strncmp("TP2", tag, 3)) /* Title */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->title = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->title, tag_length - 1);
id3tag->title[tag_length - 1] = '\0';
}
else if(!strncmp("TAL", tag, 3)) /* Album */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->album = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->album, tag_length - 1);
id3tag->album[tag_length - 1] = '\0';
}
else if(!strncmp("TRK", tag, 3)) /* Track No. */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->tracktext = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->tracktext, tag_length - 1);
id3tag->tracktext[tag_length - 1] = '\0';
id3tag->track = atoi(id3tag->tracktext);
}
else if(!strncmp("TYE", tag, 3)) /* Year */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->year = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->year, tag_length - 1);
id3tag->year[tag_length - 1] = '\0';
}
else if(!strncmp("COM", tag, 3))
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
skip = pgeMp3SeekPastNull(pgeMp3Handle);
id3tag->comment = (char *)pgeMalloc(tag_length - skip);
pgeFileRead(pgeMp3Handle, id3tag->comment, tag_length - skip - 1);
id3tag->comment[tag_length - skip - 1] = '\0';
}
else if(!strncmp("TCO", tag, 3)) /* Genre */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->genretext = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->genretext, tag_length - 1);
id3tag->genretext[tag_length - 1] = '\0';
}
else if(!strncmp("PIC", tag, 3)) /* Picture */
{
pgeFileSeek(pgeMp3Handle, 6, PGE_FILE_CUR);
id3tag->pictureoffset = pgeFileTell(pgeMp3Handle);
pgeFileSeek(pgeMp3Handle, tag_length - 6, PGE_FILE_CUR);
}
else
{
pgeFileSeek(pgeMp3Handle, tag_length, PGE_FILE_CUR);
}
size -= tag_length;
}
strcpy(id3tag->id3version, "2.2");
}
}
static void ParseID3v2_3(pgeMp3Info *id3tag)
{
int size;
int tag_length = 0;
char tag[4];
int skip;
if(ID3v2() == 3)
{
size = ID3v2TagSize();
pgeFileSeek(pgeMp3Handle, 10, PGE_FILE_SET);
while(size > 0)
{
pgeFileRead(pgeMp3Handle, tag, 4);
size -= 4;
pgeFileRead(pgeMp3Handle, &tag_length, 4);
tag_length = (unsigned int) swapInt32BigToHost((int)tag_length);
size -= 4;
pgeFileSeek(pgeMp3Handle, 2, PGE_FILE_CUR);
size -= 2;
if(*tag == 0 || tag_length > size || tag_length == 0)
break;
if(!strncmp("TPE1", tag, 4)) /* Artist */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->artist = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->artist, tag_length - 1);
id3tag->artist[tag_length - 1] = '\0';
}
else if(!strncmp("TIT2", tag, 4)) /* Title */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->title = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->title, tag_length - 1);
id3tag->title[tag_length - 1] = '\0';
}
else if(!strncmp("TALB", tag, 4)) /* Album */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->album = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->album, tag_length - 1);
id3tag->album[tag_length - 1] = '\0';
}
else if(!strncmp("TRCK", tag, 4)) /* Track No. */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->tracktext = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->tracktext, tag_length - 1);
id3tag->tracktext[tag_length - 1] = '\0';
id3tag->track = atoi(id3tag->tracktext);
}
else if(!strncmp("TYER", tag, 4)) /* Year */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->year = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->year, tag_length - 1);
id3tag->year[tag_length - 1] = '\0';
}
else if(!strncmp("COMM", tag, 4)) /* Comment */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
skip = pgeMp3SeekPastNull(pgeMp3Handle);
id3tag->comment = (char *)pgeMalloc(tag_length - skip);
pgeFileRead(pgeMp3Handle, id3tag->comment, tag_length - skip - 1);
id3tag->comment[tag_length - skip - 1] = '\0';
}
else if(!strncmp("TCON", tag, 4)) /* Genre */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->genretext = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->genretext, tag_length - 1);
id3tag->genretext[tag_length - 1] = '\0';
}
else if(!strncmp("APIC", tag, 4)) /* Picture */
{
pgeFileSeek(pgeMp3Handle, 13, PGE_FILE_CUR);
id3tag->pictureoffset = pgeFileTell(pgeMp3Handle);
pgeFileSeek(pgeMp3Handle, tag_length - 13, PGE_FILE_CUR);
}
else
{
pgeFileSeek(pgeMp3Handle, tag_length, PGE_FILE_CUR);
}
size -= tag_length;
}
strcpy(id3tag->id3version, "2.3");
}
}
static void ParseID3v2_4(pgeMp3Info *id3tag)
{
int size;
int tag_length;
char tag[4];
int skip;
if(ID3v2() == 4)
{
size = ID3v2TagSize();
pgeFileSeek(pgeMp3Handle, 10, PGE_FILE_SET);
while(size != 0)
{
pgeFileRead(pgeMp3Handle, tag, 4);
size -= 4;
pgeFileRead(pgeMp3Handle, &tag_length, 4);
tag_length = (unsigned int) swapInt32BigToHost((int)tag_length);
size -= 4;
pgeFileSeek(pgeMp3Handle, 2, PGE_FILE_CUR);
size -= 2;
if(*tag == 0 || tag_length > size || tag_length == 0)
break;
if(!strncmp("TPE1", tag, 4)) /* Artist */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->artist = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->artist, tag_length - 1);
id3tag->artist[tag_length - 1] = '\0';
}
else if(!strncmp("TIT2", tag, 4)) /* Title */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->title = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->title, tag_length - 1);
id3tag->title[tag_length - 1] = '\0';
}
else if(!strncmp("TALB", tag, 4)) /* Album */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->album = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->album, tag_length - 1);
id3tag->album[tag_length - 1] = '\0';
}
else if(!strncmp("TRCK", tag, 4)) /* Track No. */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->tracktext = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->tracktext, tag_length - 1);
id3tag->tracktext[tag_length - 1] = '\0';
id3tag->track = atoi(id3tag->tracktext);
}
else if(!strncmp("TYER", tag, 4)) /* Year */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->year = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->year, tag_length - 1);
id3tag->year[tag_length - 1] = '\0';
}
else if(!strncmp("COMM", tag, 4))
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
skip = pgeMp3SeekPastNull(pgeMp3Handle);
id3tag->comment = (char *)pgeMalloc(tag_length - skip);
pgeFileRead(pgeMp3Handle, id3tag->comment, tag_length - skip - 1);
id3tag->comment[tag_length - skip - 1] = '\0';
}
else if(!strncmp("TCON", tag, 4)) /* Genre */
{
pgeFileSeek(pgeMp3Handle, 1, PGE_FILE_CUR);
id3tag->genretext = (char *)pgeMalloc(tag_length);
pgeFileRead(pgeMp3Handle, id3tag->genretext, tag_length - 1);
id3tag->genretext[tag_length - 1] = '\0';
}
else if(!strncmp("APIC", tag, 4)) /* Picture */
{
pgeFileSeek(pgeMp3Handle, 13, PGE_FILE_CUR);
id3tag->pictureoffset = pgeFileTell(pgeMp3Handle);
pgeFileSeek(pgeMp3Handle, tag_length - 13, PGE_FILE_CUR);
}
else
{
pgeFileSeek(pgeMp3Handle, tag_length, PGE_FILE_CUR);
}
size -= tag_length;
}
strcpy(id3tag->id3version, "2.4");
}
}
static int ParseID3v2(pgeMp3Info *id3tag)
{
switch(ID3v2())
{
case 2:
ParseID3v2_2(id3tag);
break;
case 3:
ParseID3v2_3(id3tag);
break;
case 4:
ParseID3v2_4(id3tag);
break;
default:
return -1;
}
return 0;
}
static int ParseID3v1(pgeMp3Info *id3tag)
{
char id3buffer[512];
pgeFileSeek(pgeMp3Handle, -128, PGE_FILE_END);
pgeFileRead(pgeMp3Handle, id3buffer, 128);
if(strstr(id3buffer, "TAG") != NULL)
{
if(id3tag->title == NULL)
{
pgeFileSeek(pgeMp3Handle, -125, PGE_FILE_END);
id3tag->title = (char *)pgeMalloc(31);
pgeFileRead(pgeMp3Handle, id3tag->title, 30);
id3tag->title[30] = '\0';
}
if(id3tag->artist == NULL)
{
pgeFileSeek(pgeMp3Handle, -95, PGE_FILE_END);
id3tag->artist = (char *)pgeMalloc(31);
pgeFileRead(pgeMp3Handle, id3tag->artist, 30);
id3tag->artist[30] = '\0';
}
if(id3tag->album == NULL)
{
pgeFileSeek(pgeMp3Handle, -65, PGE_FILE_END);
id3tag->album = (char *)pgeMalloc(31);
pgeFileRead(pgeMp3Handle, id3tag->album, 30);
id3tag->album[30] = '\0';
}
if(id3tag->year == NULL)
{
pgeFileSeek(pgeMp3Handle, -35, PGE_FILE_END);
id3tag->year = (char *)pgeMalloc(5);
pgeFileRead(pgeMp3Handle, id3tag->year, 4);
id3tag->year[4] = '\0';
}
if(id3tag->comment == NULL)
{
pgeFileSeek(pgeMp3Handle, -31, PGE_FILE_END);
id3tag->comment = (char *)pgeMalloc(31);
pgeFileRead(pgeMp3Handle, id3tag->comment, 30);
id3tag->comment[30] = '\0';
}
if(id3tag->genrecode == NULL)
{
pgeFileSeek(pgeMp3Handle, -1, PGE_FILE_END);
id3tag->genrecode = (char *)pgeMalloc(2);
pgeFileRead(pgeMp3Handle, id3tag->genrecode, 1);
id3tag->genrecode[1] = '\0';
}
/* Track */
if(*(id3tag->comment + 28) == 0 && *(id3tag->comment + 29) > 0)
{
id3tag->track = *(id3tag->comment + 29);
strcpy(id3tag->id3version, "1.1");
}
else
{
id3tag->track = 0;
strcpy(id3tag->id3version, "1.0");
}
if(id3tag->genretext == NULL)
{
id3tag->genretext = (char *)pgeMalloc(31);
if(((int)id3tag->genrecode[0] >= 0) & ((int)id3tag->genrecode[0] < pgeMp3GenreCount))
strcpy(id3tag->genretext, pgeMp3GenreList[(int)id3tag->genrecode[0]].text);
else
strcpy(id3tag->genretext, "");
id3tag->genretext[30] = '\0';
}
return 0;
}
return -1;
}
// Main function:
static void ParseID3(pgeMp3Info *id3tag)
{
memset(id3tag, 0, sizeof(pgeMp3Info));
ParseID3v2(id3tag);
ParseID3v1(id3tag);
}
///////////////////////
// INTERNAL FUNCTIONS
///////////////////////
static int pgeMpCheckId3TagV2(pgeFile *uid)
{
char identifier[3];
pgeFileSeek(uid, 0, PGE_FILE_SET);
pgeFileRead(uid, identifier, 3);
if(strncmp(identifier, "ID3", 3) != 0)
return 0;
unsigned char majorVersion;
pgeFileRead(uid, &majorVersion, 1);
if (majorVersion < 2 || majorVersion > 4)
return -1;
unsigned char flags;
pgeFileSeek(uid, 1, PGE_FILE_CUR);
pgeFileRead(uid, &flags, 1);
switch(majorVersion)
{
case 2:
if(flags)
return -1;
break;
case 3:
if(flags & ~0x40) // Extended header
return -1;
break;
case 4:
if(flags & ~(0x40 | 0x10)) // Extended header or footer
return -1;
break;
}
unsigned int tagSize;
pgeFileRead(uid, &tagSize, 4);
unsigned int nTagSize = swapInt32BigToHost(tagSize);
nTagSize = (nTagSize & 0x0000007f)
| ((nTagSize & 0x00007f00) >> 1)
| ((nTagSize & 0x007f0000) >> 2)
| ((nTagSize & 0x7f000000) >> 3);
nTagSize += 10;
return nTagSize;
}
static int pgeMpCheckId3TagV1(pgeFile *uid)
{
char identifier[3];
pgeFileSeek(uid, -128, PGE_FILE_END);
pgeFileRead(uid, identifier, 3);
if(strncmp(identifier, "TAG", 3) != 0)
return 0;
return 1;
}
static int pgeMp3FindSyncWord(unsigned char *data, int len)
{
int i;
for(i = 0; i < len-1;i++)
{
if(data[i+0] != 0xff)
continue;
if((data[i+1] & 0xE0) == 0xE0)
break;
i++;
}
return i;
}
static int pgeMp3ReadNextFrame(int buffer)
{
int i, frameoffset;
int bitrate, padding, version, framesize = 0;
int required = PGE_MP3_READ_SIZE;
u32 outbits;
pgeFileSeek(pgeMp3Handle, pgeMp3DataPos, PGE_FILE_SET);
for(i = 0; i < 64; i++)
{
if(pgeMp3ThreadExit)
return 0;
sceKernelPollEventFlag(pgeMp3EventFlag, PGE_MP3_RESUMED, PSP_EVENT_WAITAND, &outbits);
if(outbits & PGE_MP3_RESUMED)
{
pgeFileClose(pgeMp3Handle);
char dir[256];
getcwd(dir, 256);
sceIoChdir(dir);
pgeMp3Handle = pgeFileOpen((char*)pgeMp3Filename, PGE_FILE_RDONLY);
pgeFileSeek(pgeMp3Handle, pgeMp3DataPos, PGE_FILE_SET);
sceKernelClearEventFlag(pgeMp3EventFlag, ~PGE_MP3_RESUMED);
}
if(pgeMp3Handle == NULL)
{
sceKernelSetEventFlag(pgeMp3EventFlag, PGE_MP3_RESUMED);
return 0;
}
pgeFileSeek(pgeMp3Handle, -2, PGE_FILE_CUR);
required = PGE_MP3_READ_SIZE;
if(pgeMp3DataPos + required > pgeMp3DataEnd)
{
required = (pgeMp3DataEnd - pgeMp3DataPos);
pgeMp3ShouldExit = 1;
}
pgeFileRead(pgeMp3Handle, pgeMp3ReadBuffer, required);
pgeMp3DataPos += required;
pgeMp3BufferSize[pgeMp3ReadCurrentBuffer] = required;
frameoffset = pgeMp3FindSyncWord(pgeMp3ReadBuffer, required);
version = (pgeMp3ReadBuffer[frameoffset+1] >> 3) & 3;
pgeMp3SampleRate = samplerates[version][(pgeMp3ReadBuffer[frameoffset+2] >> 2) & 3];
bitrate = pgeMp3ReadBuffer[frameoffset+2] >> 4;
padding = (pgeMp3ReadBuffer[frameoffset+2] & 2) >> 1;
if(version == 3)
{
pgeMp3SamplesPerFrame = 1152;
framesize = 144000*bitrates[bitrate]/pgeMp3SampleRate + padding;
}
else
{
pgeMp3SamplesPerFrame = 576;
framesize = 72000*bitrates_v2[bitrate]/pgeMp3SampleRate + padding;
}
pgeMp3CodecStruct[6] = (unsigned long)&pgeMp3ReadBuffer[frameoffset];
pgeMp3CodecStruct[8] = (unsigned long)&pgeMp3SrcBuffer[buffer][i*pgeMp3SamplesPerFrame*2];
pgeMp3CodecStruct[7] = pgeMp3CodecStruct[10] = framesize;
pgeMp3CodecStruct[9] = pgeMp3SamplesPerFrame * 4;
int ret = sceAudiocodecDecode(pgeMp3CodecStruct, 0x1002);
if(ret < 0)
{
printf("sceAudiocodecDecode failed with %08x\n", ret);
}
pgeMp3DataPos -= required - (frameoffset + framesize);
pgeFileSeek(pgeMp3Handle, pgeMp3DataPos, PGE_FILE_SET);
pgeMp3ReadNumFrames++;
if(pgeMp3ShouldExit)
{
if(pgeMp3Looping)
{
pgeMp3DataPos = pgeMp3DataStart;
pgeFileSeek(pgeMp3Handle, pgeMp3DataPos, PGE_FILE_SET);
pgeMp3ShouldExit = 0;
}
else
{
pgeMp3NumFrames = pgeMp3ReadNumFrames;
return 0;
}
}
}
return 1;
}
static void pgeMp3InitCleanup(int level, int ret)
{
switch(level)
{
case 3:
case 2:
case 1:
sceAudiocodecReleaseEDRAM(pgeMp3CodecStruct);
case 0:
pgeMp3LastError = ret;
pgeMp3Initialized = 0;
break;
default:
break;
}
}
static int pgeMp3DecodeThread(SceSize args, void *argp)
{
(void)args;
(void)argp;
pgeMp3AudioChannel = sceAudioSRCChReserve(pgeMp3SamplesPerFrame, pgeMp3SampleRate, 2);
pgeMp3WorkingBuffer = 0;
u32 outbits;
while(!pgeMp3ThreadExit)
{
if(pgeMp3ThreadExit)
break;
int numframes = 0;
while(pgeMp3JobStarted)
{
sceKernelDelayThreadCB(1000);
if(pgeMp3ThreadExit)
break;
if(!pgeMp3Paused)
{
sceKernelPollEventFlag(pgeMp3EventFlag, PGE_MP3_BUFFER1_EMPTY, PSP_EVENT_WAITAND, &outbits);
if(((outbits & PGE_MP3_BUFFER1_EMPTY) == 0 && pgeMp3WorkingBuffer == 0) || ((outbits & PGE_MP3_BUFFER2_EMPTY) == 0 && pgeMp3WorkingBuffer == 1) || ((outbits & PGE_MP3_BUFFER3_EMPTY) == 0 && pgeMp3WorkingBuffer == 2))
sceKernelDelayThreadCB(1);
else
continue;
sceAudioSRCOutputBlocking(pgeMp3VolumeValue, &pgeMp3SrcBuffer[pgeMp3WorkingBuffer][pgeMp3DecodeCurrentFrame*pgeMp3SamplesPerFrame*2]);
pgeMp3DecodeCurrentFrame++;
numframes++;
if(pgeMp3DecodeCurrentFrame >= 64)
{
if(pgeMp3WorkingBuffer == 0)
sceKernelSetEventFlag(pgeMp3EventFlag, PGE_MP3_BUFFER1_EMPTY);
else if(pgeMp3WorkingBuffer == 1)
sceKernelSetEventFlag(pgeMp3EventFlag, PGE_MP3_BUFFER2_EMPTY);
else if(pgeMp3WorkingBuffer == 2)
sceKernelSetEventFlag(pgeMp3EventFlag, PGE_MP3_BUFFER3_EMPTY);
pgeMp3WorkingBuffer = (pgeMp3WorkingBuffer+1)%3;
pgeMp3DecodeCurrentFrame = 0;
bufferempty = 1;
}
//printf("numframes: %d\n", numframes);
//printf("pgeMp3NumFrames: %d\n", pgeMp3NumFrames);
if(numframes == pgeMp3NumFrames)
{
if(!pgeMp3Looping)
{
printf("Here");
pgeMp3Stop();
pgeMp3NumFrames = 0;
break;
}
}
}
else
{
memset(pgeMp3OutputBuffer[pgeMp3OutputIndex], 0, sizeof(pgeMp3OutputBuffer[pgeMp3OutputIndex]));
sceKernelDelayThreadCB(1000);
}
}
if(!pgeMp3JobStarted)
{
if(pgeMp3Handle != NULL)
pgeFileClose(pgeMp3Handle);
pgeMp3Handle = NULL;
pgeMp3Filename = NULL;
}
}
pgeMp3Playing = 0;
sceKernelExitDeleteThread(0);
return 0;
}
static int power_callback(int unknown, int pwrflags, void *common)
{
(void)unknown;
(void)common;
if((pwrflags & (PSP_POWER_CB_POWER_SWITCH|PSP_POWER_CB_STANDBY)) > 0)
{
pgeMp3Paused = 1;
}
if((pwrflags & 0x00040000) > 0)
{
sceKernelDelayThread(2*1000*1000);
sceKernelSetEventFlag(pgeMp3EventFlag, PGE_MP3_RESUMED);
pgeMp3Paused = 0;
}
return 0;
}
static int pgeMp3ReadThread(SceSize args, void *argp)
{
(void)args;
(void)argp;
int cbid = sceKernelCreateCallback("Power Callback", power_callback, NULL);
scePowerRegisterCallback(-1, cbid);
pgeMp3ReadNextFrame(0);
sceKernelClearEventFlag(pgeMp3EventFlag, ~PGE_MP3_BUFFER1_EMPTY);
pgeMp3ReadNextFrame(1);
sceKernelClearEventFlag(pgeMp3EventFlag, ~PGE_MP3_BUFFER2_EMPTY);
pgeMp3ReadNextFrame(2);
sceKernelClearEventFlag(pgeMp3EventFlag, ~PGE_MP3_BUFFER3_EMPTY);
int result;
u32 outbits;
sceKernelStartThread(pgeMp3DecodeThreadId, 0, 0);
while(!pgeMp3ThreadExit)
{
sceKernelDelayThreadCB(1000);
sceKernelPollEventFlag(pgeMp3EventFlag, PGE_MP3_BUFFER1_EMPTY, PSP_EVENT_WAITAND, &outbits);
if(outbits & PGE_MP3_BUFFER1_EMPTY)
{
result = pgeMp3ReadNextFrame(0);