1907f9f5bSshatty#include <File.h>
2907f9f5bSshatty#include <MediaDecoder.h>
3907f9f5bSshatty#include <MediaTrack.h>
4907f9f5bSshatty#include <MediaFile.h>
5907f9f5bSshatty#include <StorageDefs.h>
6907f9f5bSshatty#include <stdio.h>
7907f9f5bSshatty#include <string.h>
8907f9f5bSshatty
9907f9f5bSshattyclass FileDecoder : public BMediaDecoder {
10907f9f5bSshattyprivate:
11907f9f5bSshatty	BMediaTrack * track;
12907f9f5bSshatty	char buffer[8192];
13907f9f5bSshattypublic:
14907f9f5bSshatty	FileDecoder(BMediaTrack * _track, const media_format *inFormat,
15907f9f5bSshatty                const void *info = NULL, size_t infoSize = 0)
16907f9f5bSshatty    : BMediaDecoder(inFormat,info,infoSize) {
17907f9f5bSshatty	 	track = _track;
18907f9f5bSshatty	}
19907f9f5bSshattyprotected:
20907f9f5bSshatty	virtual status_t GetNextChunk(const void **chunkData, size_t *chunkLen,
21907f9f5bSshatty		                              media_header *mh) {
2224817be9Sshatty		memset(mh,0,sizeof(media_header));
2324817be9Sshatty		status_t result = track->ReadChunk((char**)chunkData,(int32*)chunkLen,mh);
2424817be9Sshatty		const void * data = *chunkData;
259833385dSshatty		(void)data;
2624817be9Sshatty		return result;
27907f9f5bSshatty	}
28907f9f5bSshatty};
29907f9f5bSshatty
30907f9f5bSshattyint main (int argc, const char ** argv) {
31907f9f5bSshatty	if (argc == 0) {
32907f9f5bSshatty		return -1;
33907f9f5bSshatty	}
34907f9f5bSshatty	if (argc < 3) {
35907f9f5bSshatty		fprintf(stderr,"%s: invalid usage\n",argv[0]);
36907f9f5bSshatty		fprintf(stderr,"supply an input file and an output file:\n");
37907f9f5bSshatty		fprintf(stderr,"  media_decoder input.mp3 output.raw\n");
3824817be9Sshatty		return -1;
39907f9f5bSshatty	}
4024817be9Sshatty	// open the file using BMediaFile
41907f9f5bSshatty	BFile * file = new BFile(argv[1],B_READ_ONLY);
42907f9f5bSshatty	BMediaFile * mf = new BMediaFile(file);
4324817be9Sshatty	if (mf->CountTracks() == 0) {
4424817be9Sshatty		fprintf(stderr,"no tracks found in %s\n",argv[1]);
4524817be9Sshatty		return -1;
4624817be9Sshatty	}
47907f9f5bSshatty	media_format format;
48907f9f5bSshatty	memset(&format,0,sizeof(format));
4924817be9Sshatty	// find an audio track
5024817be9Sshatty	BMediaTrack * track = 0;
5124817be9Sshatty	for (int i = 0; i < mf->CountTracks() ; i++) {
5224817be9Sshatty		track = mf->TrackAt(i);
5324817be9Sshatty		track->EncodedFormat(&format);
5424817be9Sshatty		if (format.IsAudio()) {
5524817be9Sshatty			break;
5624817be9Sshatty		}
5724817be9Sshatty		track = 0;
5824817be9Sshatty	}
5924817be9Sshatty	if (track == 0) {
6024817be9Sshatty		fprintf(stderr,"no audio stream found in %s\n",argv[1]);
6124817be9Sshatty		return -1;
6224817be9Sshatty	}
6324817be9Sshatty	// create a BMediaDecoder and initialize it
64907f9f5bSshatty	FileDecoder * fd = new FileDecoder(track,&format);
65907f9f5bSshatty//	fd->SetInputFormat(&format);
66907f9f5bSshatty	memset(&format,0,sizeof(format));
67907f9f5bSshatty	track->DecodedFormat(&format);
68907f9f5bSshatty	fd->SetOutputFormat(&format);
69907f9f5bSshatty
70907f9f5bSshatty	// open the output file
71907f9f5bSshatty	BFile * file2 = new BFile(argv[2],B_WRITE_ONLY|B_CREATE_FILE|B_ERASE_FILE);
72907f9f5bSshatty
73907f9f5bSshatty	// decode until we hit an error
74907f9f5bSshatty	uint8 * buffer = new uint8[format.u.raw_audio.buffer_size];
75907f9f5bSshatty	int64 size = 0;
76907f9f5bSshatty	media_header mh;
77907f9f5bSshatty	memset(&mh,0,sizeof(mh));
78907f9f5bSshatty	while (fd->Decode((void*)buffer,&size,&mh,0) == B_OK) {
79907f9f5bSshatty		file2->Write(buffer,format.u.raw_audio.buffer_size);
80907f9f5bSshatty	}
81907f9f5bSshatty	return 0;
82907f9f5bSshatty}
83