2 #include <MediaDecoder.h>
3 #include <MediaTrack.h>
5 #include <StorageDefs.h>
9 class FileDecoder
: public BMediaDecoder
{
14 FileDecoder(BMediaTrack
* _track
, const media_format
*inFormat
,
15 const void *info
= NULL
, size_t infoSize
= 0)
16 : BMediaDecoder(inFormat
,info
,infoSize
) {
20 virtual status_t
GetNextChunk(const void **chunkData
, size_t *chunkLen
,
22 memset(mh
,0,sizeof(media_header
));
23 status_t result
= track
->ReadChunk((char**)chunkData
,(int32
*)chunkLen
,mh
);
24 const void * data
= *chunkData
;
30 int main (int argc
, const char ** argv
) {
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");
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]);
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()) {
60 fprintf(stderr
,"no audio stream found in %s\n",argv
[1]);
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
);
70 // open the output file
71 BFile
* file2
= new BFile(argv
[2],B_WRITE_ONLY
|B_CREATE_FILE
|B_ERASE_FILE
);
73 // decode until we hit an error
74 uint8
* buffer
= new uint8
[format
.u
.raw_audio
.buffer_size
];
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
);