#include <stdio.h>
#include <stdlib.h>

#ifdef __linux

#include <unistd.h>
#include <sys/time.h>

static inline unsigned int GetTimer()
{
	struct timeval tv;
	struct timezone tz;
	gettimeofday(&tv,&tz);
	return (tv.tv_sec*1000000+tv.tv_usec);
}  

#else

extern unsigned int __stdcall GetTickCount(void);

static unsigned int GetTimer() { return GetTickCount() * 1000; }

#endif

#include "mp3.h"

static FILE* mp3file=NULL;

int mplayer_audio_read(char *buf,int size)
{
	return fread(buf,1,size,mp3file);
}

#define BUFFLEN 4608
static unsigned char buffer[BUFFLEN];

int do_decompress(char *fname,char *outfile)
{
  int len;
  int total=0;
  unsigned int time1;
  float length;
  FILE *f=fopen(outfile,"wb");

  mp3file=fopen(fname,"rb");

  if(!mp3file)
  {
  	printf("file not found\n");
  	exit(1);
  }

#ifdef USE_FAKE_MONO
  MP3_Init(0);
#else
  MP3_Init();
#endif

  MP3_samplerate = MP3_channels = 0;
  
  time1 = GetTimer();
  
  while((len=MP3_DecodeFrame(buffer,-1))>0 && total<100000000)
  {
      total+=len;
      fwrite(buffer,len,1,f);
  }
  time1 = GetTimer()-time1;
  length=(float)total/(float)(MP3_samplerate*MP3_channels*2);
  printf("\nDecoding time: %8.6f\n",(float)time1*0.000001f);
  printf("Uncompressed size: %d bytes  (%8.3f secs)\n",total,length);
  printf("CPU usage at normal playback: %5.2f %\n",time1*0.0001f/length);

  fclose(mp3file);
  fclose(f);
  
  return 0;
}

int main(void)
{
#ifndef __linux
	do_decompress("M:\\Media\\Music\\Accept\\36-96-RunThroughTheNight.mp3","d:\\test1.raw");
	do_decompress("M:\\Media\\Music\\Accept\\37-96-TakeOutTheCrime.mp3","d:\\test2.raw");
#else
	do_decompress("/mnt/media/Media/Music/Accept/36-96-RunThroughTheNight.mp3","test1.raw");
	do_decompress("/mnt/media/Media/Music/Accept/37-96-TakeOutTheCrime.mp3","test2.raw");
#endif
	return 0;
}
