summaryrefslogtreecommitdiff
path: root/apps/playback.c
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2005-06-14 22:27:57 +0000
committerDave Chapman <dave@dchapman.com>2005-06-14 22:27:57 +0000
commit3ad485b15a51e92a23540429b64e89656555bba6 (patch)
treea84ff392ab5e5cc6a6e170e7607996d7fd4d13d3 /apps/playback.c
parent88a89e0cdbfc4d745c48ff607b0774192d66218c (diff)
downloadrockbox-3ad485b15a51e92a23540429b64e89656555bba6.tar.gz
rockbox-3ad485b15a51e92a23540429b64e89656555bba6.zip
Move metadata parsing code from playback.c into metadata.c
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6714 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/playback.c')
-rw-r--r--apps/playback.c357
1 files changed, 18 insertions, 339 deletions
diff --git a/apps/playback.c b/apps/playback.c
index 0828d58ce0..d0b2882204 100644
--- a/apps/playback.c
+++ b/apps/playback.c
@@ -58,6 +58,7 @@
58#include "bookmark.h" 58#include "bookmark.h"
59#include "misc.h" 59#include "misc.h"
60#include "sound.h" 60#include "sound.h"
61#include "metadata.h"
61 62
62static volatile bool playing; 63static volatile bool playing;
63static volatile bool paused; 64static volatile bool paused;
@@ -124,23 +125,6 @@ static volatile int codecbufused;
124static volatile int buf_ridx; 125static volatile int buf_ridx;
125static volatile int buf_widx; 126static volatile int buf_widx;
126 127
127#define MAX_TRACK 10
128struct track_info {
129 struct mp3entry id3; /* TAG metadata */
130 struct mp3info mp3data; /* MP3 metadata */
131 char *codecbuf; /* Pointer to codec buffer */
132 size_t codecsize; /* Codec length in bytes */
133 int codectype; /* Codec type (example AFMT_MPA_L3) */
134
135 off_t filerem; /* Remaining bytes of file NOT in buffer */
136 off_t filesize; /* File total length */
137 off_t filepos; /* Read position of file for next buffer fill */
138 off_t start_pos; /* Position to first bytes of file in buffer */
139 volatile int available; /* Available bytes to read from buffer */
140 bool taginfo_ready; /* Is metadata read */
141 int playlist_offset; /* File location in playlist */
142};
143
144/* Track information (count in file buffer, read/write indexes for 128/* Track information (count in file buffer, read/write indexes for
145 track ring structure. */ 129 track ring structure. */
146static int track_count; 130static int track_count;
@@ -394,49 +378,6 @@ void codec_configure_callback(int setting, void *value)
394 } 378 }
395} 379}
396 380
397/* Simple file type probing by looking filename extension. */
398int probe_file_format(const char *filename)
399{
400 char *suffix;
401
402 suffix = strrchr(filename, '.');
403 if (suffix == NULL)
404 return AFMT_UNKNOWN;
405 suffix += 1;
406
407 if (!strcasecmp("mp1", suffix))
408 return AFMT_MPA_L1;
409 else if (!strcasecmp("mp2", suffix))
410 return AFMT_MPA_L2;
411 else if (!strcasecmp("mpa", suffix))
412 return AFMT_MPA_L2;
413 else if (!strcasecmp("mp3", suffix))
414 return AFMT_MPA_L3;
415 else if (!strcasecmp("ogg", suffix))
416 return AFMT_OGG_VORBIS;
417 else if (!strcasecmp("wav", suffix))
418 return AFMT_PCM_WAV;
419 else if (!strcasecmp("flac", suffix))
420 return AFMT_FLAC;
421 else if (!strcasecmp("mpc", suffix))
422 return AFMT_MPC;
423 else if (!strcasecmp("aac", suffix))
424 return AFMT_AAC;
425 else if (!strcasecmp("ape", suffix))
426 return AFMT_APE;
427 else if (!strcasecmp("wma", suffix))
428 return AFMT_WMA;
429 else if ((!strcasecmp("a52", suffix)) || (!strcasecmp("ac3", suffix)))
430 return AFMT_A52;
431 else if (!strcasecmp("rm", suffix))
432 return AFMT_REAL;
433 else if (!strcasecmp("wv", suffix))
434 return AFMT_WAVPACK;
435
436 return AFMT_UNKNOWN;
437
438}
439
440void yield_codecs(void) 381void yield_codecs(void)
441{ 382{
442 yield(); 383 yield();
@@ -620,10 +561,6 @@ bool audio_load_track(int offset, bool start_play, int peek_offset)
620 off_t size; 561 off_t size;
621 int rc, i; 562 int rc, i;
622 int copy_n; 563 int copy_n;
623 /* Used by the metadata parsers */
624 unsigned long totalsamples,bytespersample,channels,bitspersample,numbytes;
625 unsigned char* buf;
626 int j,eof;
627 564
628 if (track_count >= MAX_TRACK) 565 if (track_count >= MAX_TRACK)
629 return false; 566 return false;
@@ -673,283 +610,25 @@ bool audio_load_track(int offset, bool start_play, int peek_offset)
673 close(fd); 610 close(fd);
674 return false; 611 return false;
675 } 612 }
676
677 /* Load codec specific track tag information. */
678 switch (tracks[track_widx].codectype) {
679 case AFMT_MPA_L2:
680 case AFMT_MPA_L3:
681 /* Should check the return value. */
682 mp3info(&tracks[track_widx].id3, trackname, v1first);
683 lseek(fd, 0, SEEK_SET);
684 /* This is too slow to execute on some files. */
685 get_mp3file_info(fd, &tracks[track_widx].mp3data);
686 if (offset) {
687 lseek(fd, offset, SEEK_SET);
688 tracks[track_widx].id3.offset = offset;
689 mp3_set_elapsed(&tracks[track_widx].id3);
690 tracks[track_widx].filepos = offset;
691 tracks[track_widx].filerem = tracks[track_widx].filesize - offset;
692 ci.curpos = offset;
693 tracks[track_widx].start_pos = offset;
694 } else {
695 lseek(fd, 0, SEEK_SET);
696 }
697 /*
698 logf("T:%s", tracks[track_widx].id3.title);
699 logf("L:%d", tracks[track_widx].id3.length);
700 logf("O:%d", tracks[track_widx].id3.first_frame_offset);
701 logf("F:%d", tracks[track_widx].id3.frequency);
702 */
703 tracks[track_widx].taginfo_ready = true;
704 break ;
705
706 case AFMT_PCM_WAV:
707 /* Use the trackname part of the id3 structure as a temporary buffer */
708 buf=tracks[track_widx].id3.path;
709
710 lseek(fd, 0, SEEK_SET);
711
712 rc = read(fd, buf, 44);
713 if (rc < 44) {
714 close(fd);
715 return false;
716 }
717
718 if ((memcmp(buf,"RIFF",4)!=0) ||
719 (memcmp(&buf[8],"WAVEfmt",7)!=0)) {
720 logf("%s is not a WAV file\n",trackname);
721 close(fd);
722 return(false);
723 }
724
725 /* FIX: Correctly parse WAV header - we assume canonical
726 44-byte header */
727
728 bitspersample=buf[34];
729 channels=buf[22];
730
731 if ((bitspersample!=16) || (channels != 2)) {
732 logf("Unsupported WAV file - %d bitspersample, %d channels\n",
733 bitspersample,channels);
734 close(fd);
735 return(false);
736 }
737
738 bytespersample=((bitspersample/8)*channels);
739 numbytes=(buf[40]|(buf[41]<<8)|(buf[42]<<16)|(buf[43]<<24));
740 totalsamples=numbytes/bytespersample;
741 613
742 tracks[track_widx].id3.vbr=false; /* All WAV files are CBR */ 614 if (!get_metadata(&tracks[track_widx],fd,trackname,v1first)) {
743 tracks[track_widx].id3.filesize=filesize(fd); 615 close(fd);
744 tracks[track_widx].id3.frequency=buf[24]|(buf[25]<<8)|(buf[26]<<16)|(buf[27]<<24); 616 return false;
745
746 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
747 tracks[track_widx].id3.length=(totalsamples/tracks[track_widx].id3.frequency)*1000;
748 tracks[track_widx].id3.bitrate=(tracks[track_widx].id3.frequency*bytespersample)/(1000/8);
749
750 lseek(fd, 0, SEEK_SET);
751 strncpy(tracks[track_widx].id3.path,trackname,sizeof(tracks[track_widx].id3.path));
752 tracks[track_widx].taginfo_ready = true;
753
754 break;
755
756 case AFMT_FLAC:
757 /* A simple parser to read vital metadata from a FLAC file - length, frequency, bitrate etc. */
758 /* This code should either be moved to a seperate file, or discarded in favour of the libFLAC code */
759 /* The FLAC stream specification can be found at http://flac.sourceforge.net/format.html#stream */
760
761 /* Use the trackname part of the id3 structure as a temporary buffer */
762 buf=tracks[track_widx].id3.path;
763
764 lseek(fd, 0, SEEK_SET);
765
766 rc = read(fd, buf, 4);
767 if (rc < 4) {
768 close(fd);
769 return false;
770 }
771
772 if (memcmp(buf,"fLaC",4)!=0) {
773 logf("%s is not a FLAC file\n",trackname);
774 close(fd);
775 return(false);
776 }
777
778 while (1) {
779 rc = read(fd, buf, 4);
780 i = (buf[1]<<16)|(buf[2]<<8)|buf[3]; /* The length of the block */
781
782 if ((buf[0]&0x7f)==0) { /* 0 is the STREAMINFO block */
783 rc = read(fd, buf, i); /* FIXME: Don't trust the value of i */
784 if (rc < 0) {
785 close(fd);
786 return false;
787 }
788 tracks[track_widx].id3.vbr=true; /* All FLAC files are VBR */
789 tracks[track_widx].id3.filesize=filesize(fd);
790
791 tracks[track_widx].id3.frequency=(buf[10]<<12)|(buf[11]<<4)|((buf[12]&0xf0)>>4);
792
793 /* NOT NEEDED: bitspersample=(((buf[12]&0x01)<<4)|((buf[13]&0xf0)>>4))+1; */
794
795 /* totalsamples is a 36-bit field, but we assume <= 32 bits are used */
796 totalsamples=(buf[14]<<24)|(buf[15]<<16)|(buf[16]<<8)|buf[17];
797
798 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
799 tracks[track_widx].id3.length=(totalsamples/tracks[track_widx].id3.frequency)*1000;
800 tracks[track_widx].id3.bitrate=(filesize(fd)*8)/tracks[track_widx].id3.length;
801 } else if ((buf[0]&0x7f)==4) { /* 4 is the VORBIS_COMMENT block */
802
803 /* The next i bytes of the file contain the VORBIS COMMENTS - just skip them for now. */
804 lseek(fd, i, SEEK_CUR);
805
806 } else {
807 if (buf[0]&0x80) { /* If we have reached the last metadata block, abort. */
808 break;
809 } else {
810 lseek(fd, i, SEEK_CUR); /* Skip to next metadata block */
811 }
812 }
813 }
814
815 lseek(fd, 0, SEEK_SET);
816 strncpy(tracks[track_widx].id3.path,trackname,sizeof(tracks[track_widx].id3.path));
817 tracks[track_widx].taginfo_ready = true;
818 break;
819
820 case AFMT_OGG_VORBIS:
821 /* A simple parser to read vital metadata from an Ogg Vorbis file */
822
823 /* An Ogg File is split into pages, each starting with the string
824 "OggS". Each page has a timestamp (in PCM samples) referred to as
825 the "granule position".
826
827 An Ogg Vorbis has the following structure:
828 1) Identification header (containing samplerate, numchannels, etc)
829 2) Comment header - containing the Vorbis Comments
830 3) Setup header - containing codec setup information
831 4) Many audio packets...
832
833 */
834
835 /* Use the trackname part of the id3 structure as a temporary buffer */
836 buf=tracks[track_widx].id3.path;
837
838 lseek(fd, 0, SEEK_SET);
839
840 rc = read(fd, buf, 58);
841 if (rc < 4) {
842 close(fd);
843 return false;
844 }
845
846 if ((memcmp(buf,"OggS",4)!=0) || (memcmp(&buf[29],"vorbis",6)!=0)) {
847 logf("%s is not an Ogg Vorbis file\n",trackname);
848 close(fd);
849 return(false);
850 }
851
852 /* Ogg stores integers in little-endian format. */
853 tracks[track_widx].id3.filesize=filesize(fd);
854 tracks[track_widx].id3.frequency=buf[40]|(buf[41]<<8)|(buf[42]<<16)|(buf[43]<<24);
855 channels=buf[39];
856
857 /* We now need to search for the last page in the file - identified by
858 by ('O','g','g','S',0) and retrieve totalsamples */
859
860 lseek(fd, -32*1024, SEEK_END);
861 eof=0;
862 j=0; /* The number of bytes currently in buffer */
863 i=0;
864 totalsamples=0;
865 while (!eof) {
866 rc = read(fd, &buf[j], MAX_PATH-j);
867 if (rc <= 0) {
868 eof=1;
869 } else {
870 j+=rc;
871 }
872 /* Inefficient (but simple) search */
873 i=0;
874 while (i < (j-5)) {
875 if (memcmp(&buf[i],"OggS",5)==0) {
876 if (i < (j-10)) {
877 totalsamples=(buf[i+6])|(buf[i+7]<<8)|(buf[i+8]<<16)|(buf[i+9]<<24);
878 j=0; /* We can discard the rest of the buffer */
879 } else {
880 break;
881 }
882 } else {
883 i++;
884 }
885 }
886 if (i < (j-5)) {
887 /* Move OggS to start of buffer */
888 while(i>0) buf[i--]=buf[j--];
889 } else {
890 j=0;
891 }
892 }
893
894 tracks[track_widx].id3.length=(totalsamples/tracks[track_widx].id3.frequency)*1000;
895
896 /* The following calculation should use datasize, not filesize (i.e. exclude comments etc) */
897 tracks[track_widx].id3.bitrate=(filesize(fd)*8)/tracks[track_widx].id3.length;
898 tracks[track_widx].id3.vbr=true;
899
900 lseek(fd, 0, SEEK_SET);
901 strncpy(tracks[track_widx].id3.path,trackname,sizeof(tracks[track_widx].id3.path));
902 tracks[track_widx].taginfo_ready = true;
903 break;
904
905 case AFMT_WAVPACK:
906 /* A simple parser to read basic information from a WavPack file.
907 * This will fail on WavPack files that don't have the WavPack header
908 * as the first thing (i.e. self-extracting WavPack files) or WavPack
909 * files that have so much extra RIFF data stored in the first block
910 * that they don't have samples (very rare, I would think).
911 */
912
913 /* Use the trackname part of the id3 structure as a temporary buffer */
914 buf=tracks[track_widx].id3.path;
915
916 lseek(fd, 0, SEEK_SET);
917
918 rc = read(fd, buf, 32);
919 if (rc < 32) {
920 close(fd);
921 return false;
922 }
923
924 if (memcmp (buf, "wvpk", 4) != 0 || buf [9] != 4 || buf [8] < 2) {
925 logf ("%s is not a WavPack file\n", trackname);
926 close (fd);
927 return (false);
928 }
929
930 tracks[track_widx].id3.vbr = true; /* All WavPack files are VBR */
931 tracks[track_widx].id3.filesize = filesize (fd);
932 tracks[track_widx].id3.frequency = 44100;
933
934 if ((buf [20] | buf [21] | buf [22] | buf [23]) &&
935 (buf [12] & buf [13] & buf [14] & buf [15]) != 0xff) {
936 totalsamples = (buf[15] << 24) | (buf[14] << 16) | (buf[13] << 8) | buf[12];
937 tracks[track_widx].id3.length = (totalsamples + 220) / 441 * 10;
938 tracks[track_widx].id3.bitrate = filesize (fd) /
939 (tracks[track_widx].id3.length / 8);
940 }
941
942 lseek (fd, 0, SEEK_SET);
943 strncpy (tracks[track_widx].id3.path, trackname, sizeof (tracks[track_widx].id3.path));
944 tracks[track_widx].taginfo_ready = true;
945 break;
946
947 /* If we don't know how to read the metadata, just store the filename */
948 default:
949 strncpy(tracks[track_widx].id3.path,trackname,sizeof(tracks[track_widx].id3.path));
950 tracks[track_widx].taginfo_ready = true;
951 break;
952 } 617 }
618
619 /* Starting playback from an offset is only support in MPA at the moment */
620 if (offset > 0) {
621 if ((tracks[track_widx].codectype==AFMT_MPA_L2) ||
622 (tracks[track_widx].codectype==AFMT_MPA_L3)) {
623 lseek(fd, offset, SEEK_SET);
624 tracks[track_widx].id3.offset = offset;
625 mp3_set_elapsed(&tracks[track_widx].id3);
626 tracks[track_widx].filepos = offset;
627 tracks[track_widx].filerem = tracks[track_widx].filesize - offset;
628 ci.curpos = offset;
629 tracks[track_widx].start_pos = offset;
630 }
631 }
953 632
954 if (start_play) { 633 if (start_play) {
955 track_count++; 634 track_count++;