1 #include <File.h> 2 #include <MediaDecoder.h> 3 #include <MediaTrack.h> 4 #include <MediaFile.h> 5 #include <StorageDefs.h> 6 #include <stdio.h> 7 #include <string.h> 8 9 class FileDecoder : public BMediaDecoder { 10 private: 11 BMediaTrack * track; 12 char buffer[8192]; 13 public: 14 FileDecoder(BMediaTrack * _track, const media_format *inFormat, 15 const void *info = NULL, size_t infoSize = 0) 16 : BMediaDecoder(inFormat,info,infoSize) { 17 track = _track; 18 } 19 protected: 20 virtual status_t GetNextChunk(const void **chunkData, size_t *chunkLen, 21 media_header *mh) { 22 memset(mh,0,sizeof(media_header)); 23 status_t result = track->ReadChunk((char**)chunkData,(int32*)chunkLen,mh); 24 const void * data = *chunkData; 25 (void)data; 26 return result; 27 } 28 }; 29 30 int main (int argc, const char ** argv) { 31 if (argc == 0) { 32 return -1; 33 } 34 if (argc < 3) { 35 fprintf(stderr,"%s: invalid usage\n",argv[0]); 36 fprintf(stderr,"supply an input file and an output file:\n"); 37 fprintf(stderr," media_decoder input.mp3 output.raw\n"); 38 return -1; 39 } 40 // open the file using BMediaFile 41 BFile * file = new BFile(argv[1],B_READ_ONLY); 42 BMediaFile * mf = new BMediaFile(file); 43 if (mf->CountTracks() == 0) { 44 fprintf(stderr,"no tracks found in %s\n",argv[1]); 45 return -1; 46 } 47 media_format format; 48 memset(&format,0,sizeof(format)); 49 // find an audio track 50 BMediaTrack * track = 0; 51 for (int i = 0; i < mf->CountTracks() ; i++) { 52 track = mf->TrackAt(i); 53 track->EncodedFormat(&format); 54 if (format.IsAudio()) { 55 break; 56 } 57 track = 0; 58 } 59 if (track == 0) { 60 fprintf(stderr,"no audio stream found in %s\n",argv[1]); 61 return -1; 62 } 63 // create a BMediaDecoder and initialize it 64 FileDecoder * fd = new FileDecoder(track,&format); 65 // fd->SetInputFormat(&format); 66 memset(&format,0,sizeof(format)); 67 track->DecodedFormat(&format); 68 fd->SetOutputFormat(&format); 69 70 // open the output file 71 BFile * file2 = new BFile(argv[2],B_WRITE_ONLY|B_CREATE_FILE|B_ERASE_FILE); 72 73 // decode until we hit an error 74 uint8 * buffer = new uint8[format.u.raw_audio.buffer_size]; 75 int64 size = 0; 76 media_header mh; 77 memset(&mh,0,sizeof(mh)); 78 while (fd->Decode((void*)buffer,&size,&mh,0) == B_OK) { 79 file2->Write(buffer,format.u.raw_audio.buffer_size); 80 } 81 return 0; 82 } 83