pablowablo]Sorry for replying just now... It was late where I was and I fell asleep
anyway, I'm using Visual C++ 6.0
here are the files:
Overall_Declarations.h
#ifndef _OVERALL_GUARD_
#define _OVERALL_GUARD_
#define OK 0
#define ERROR -1
#define TRUE 1
#define FALSE 0
#define TEMP_EOF 205
typedef unsigned int UINT32; //unsigned integer (32 bits)
typedef int INT32; //signed integer (32 bits)
typedef short int INT16; //signed integer (16 bits)
typedef unsigned short int UINT16; //unsigned integer (16 bits)
typedef float FLOAT32;//float (32 bits)
typedef double FLOAT64;//float (64 bits)
typedef int BOOLEAN;//boolean (true = 1, false = 0)
typedef unsigned char UCHAR8; //unsigned character (8 bits)
typedef int STATUS;
typedef char* STRING;
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#endif
Mp3 Decoder.h
#ifndef _DECODER_GUARD_
#define _DECODER_GUARD_
#include "Overall_Declarations.h"
#include "Mp3_Decoder.h"
//Global definitions
#define MP3_SYNC 0xfff00000
#define Hz 1
#define kHz 1000*Hz
#define bit_s 1
#define kbit_s 1000*bit_s
#define PI 3.14159265358979323846
#define INV_SQRT_2 0.70710678118654752440
//Layer Description
typedef enum
{
mpeg1_layer_reserved,
mpeg1_layer3,
mpeg1_layer2,
mpeg1_layer1
}mpeg1_layer;
//Channel Mode
typedef enum
{
mpeg1_mode_stereo,
mpeg1_mode_joint_stereo,
mpeg1_dual_channel,
mpeg1_single_channel
}mpeg1_channel_mode;
//Mp3 Frame Header
typedef struct
{
UINT32 id; //MPEG audio version ID 1 bit
mpeg1_layer layer; //layer description 2 bits
UINT32 protection_bit; //Protection 1 bit, if 1 then checksum follows header
UINT32 bitrate_index; //4 bits use lookup table
UINT32 sampling_frequency; //2 bits use lookup table
UINT32 padding_bit; //1 bit
UINT32 private_bit; //1 bit
mpeg1_channel_mode mode; //2 bits
UINT32 mode_extension; //2 bits used only in joint stereo
UINT32 copyright; //1 bit
UINT32 original; //1 bit
UINT32 emphasis; //2 bits
}mp3_header;
//Mp3 Side Information
typedef struct
{
UINT32 main_data_begin; //9 bits
UINT32 private_bits; //3 bits in mono, 5 in stereo
UINT32 scfsi[2][4]; //1 bit
UINT32 part2_3_length[2][2]; //12 bits
UINT32 big_values[2][2]; //9 bits
UINT32 global_gain[2][2]; //8 bits
UINT32 scalefac_compress[2][2]; //4 bits
UINT32 win_switch_flag[2][2]; //1 bit
/* if (win_switch_flag[][]) */
UINT32 block_type[2][2]; //2 bits
UINT32 mixed_block_flag[2][2]; //1 bit
UINT32 table_select[2][2][3]; //5 bits
UINT32 subblock_gain[2][2][3]; //3 bits
/* else */
/* table_select[][][] */
UINT32 region0_count[2][2]; //4 bits
UINT32 region1_count[2][2]; //3 bits
/* end */
UINT32 preflag[2][2]; //1 bit
UINT32 scalefac_scale[2][2]; //1 bit
UINT32 count1table_select[2][2]; //1 bit
UINT32 count1[2][2]; //Not in file, calc. by huff.dec.!
} mp3_side_info;
//mp3 main data
typedef struct
{
UINT32 scalefac_l[2][2][21]; //0-4 bits
UINT32 scalefac_s[2][2][12][3]; //0-4 bits
FLOAT32 is[2][2][576]; //Huffman coded freq. lines
} mp3_main_data;
//frame info
typedef struct
{
UINT32 frameSize;
UINT32 sideInfoSize;
UINT32 mainDataSize;
UINT32 numChannels;
}mp3_frame_info;
class MP3_DECODER
{
public:
//public methods
MP3_DECODER(); //default constructor
~MP3_DECODER(); //destructor
//private:
//private methods
STATUS Mp3_Read_Frame(void);
STATUS Mp3_Read_Header(void);
STATUS Mp3_Read_CRC(void);
STATUS Mp3_Read_Main_Data(mp3_frame_info f_frame_info);
STATUS Mp3_Get_Decode_Info(mp3_frame_info &f_frame_info);
STATUS Mp3_Get_Side_Info(UINT32 sideInfoSize);
STATUS Mp3_Get_Main_Data(UINT32 main_data_begin, UINT32 main_data_size);
UINT32 Mp3_Get_Side_Bits(UINT32 numOfBits);
public:
//public variables
private:
//private variables
mp3_header g_frame_header;
mp3_side_info g_side_info;
mp3_main_data g_main_data;
UINT32 g_sampling_frequency[3];
UINT32 g_mpeg_bitrates[3][15];
//Bit reservoir for side info
static UINT32 sideInfoArray[32];
static UINT32 *pSideInfo; //pointer to the side info reservoir
static UINT32 sideInfoIndex; //index to the current byte
//Bit reservoir for main data
UINT32 mainDataArray[1024*2];
UINT32 *pMainData; //pointer to the main data
UINT32 mainDataIndex; //pointer to the current byte
UINT32 mainDataTop; //number of bytes in the reservoir
};
#endif
main.c
#include "Mp3_Decoder.h"
#include "Bitstream.h"
#define filea "192.mp3"
void main()
{
MP3_DECODER test;
if(Open_File(filea) == OK)
{
// test.Mp3_Read_Frame();
// Mp3_Print_Header(g_frame_header);
}
Close_File();
}
Bitstream.h
#ifndef _Bitstream_Declared_
#define _Bitstream_Declared_
//#include "Mp3_Decoder.h"
#include <iostream.h>
#include <fstream.h>
#include <sys/stat.h>
fstream myFile;
struct stat results;
long size, count;
STATUS Open_File(STRING filename)
{
if (stat(filename, &results) == 0)
size = results.st_size;
myFile.open(filename, ios::in | ios::binary);
if(!myFile)
return ERROR;
return OK;
}
void Close_File()
{
myFile.close();
}
UINT32 Get_Byte()
{
UCHAR8 * temp = 0;
temp = new UCHAR8;
myFile.read(temp, 1);
count++;
return((UINT32)*temp);
}
STATUS Get_Num_Bytes(UINT32 num, UINT32 byte_array[])
{
UINT32 temp, i;
for(i = 0; i < num; i++)
{
temp = Get_Byte();
if(temp == TEMP_EOF)
return(ERROR);
else
byte_array[i] = temp;
}
return(OK);
}
void Move_File_Pointer(UINT32 offset)
{
myFile.seekg(offset);
}
UINT32 Get_File_Pos()
{
return(myFile.tellg());
}
UINT32 Get_File_Size()
{
return(size);
}
#endif
Mp3_Decoder.cpp
#include "Mp3_Decoder.h"
#include "Bitstream.h"
MP3_DECODER::MP3_DECODER()
{
const UINT32 bitrates[3][15] = {
{//Layer 1
0, 32000, 64000, 96000, 128000, 160000, 192000, 224000,
256000, 288000, 320000, 352000, 384000, 416000, 448000
},
{//Layer 2
0, 32000, 48000, 56000, 64000, 80000, 96000, 112000,
128000, 160000, 192000, 224000, 256000, 320000, 384000
},
{//Layer 3
0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000,
128000, 160000, 192000, 224000, 256000, 320000
}
};
//Sampling frequencies in hertz
const UINT32 frequency[3] = {
44100 * Hz,
48000 * Hz,
32000 * Hz
};
for(int i =0;i<3;i++)
{
for(int j=0;j<15;j++)
this->g_mpeg_bitrates[i][j] = bitrates[i][j];
}
for(i=0;i<3;i++)
this->g_sampling_frequency[i] = frequency[i];
}
MP3_DECODER::~MP3_DECODER()
{
}
STATUS MP3_DECODER::Mp3_Read_Frame()
{
UINT32 first = 0;
mp3_frame_info f_frame_info; //f means same for current frame only
//is it the first time to decode?
if(Get_File_Pos() == 0)
{
first = 1;
mainDataTop = 0;
}
//Find the next frame in the bitstream
if (Mp3_Read_Header () != OK)
return (ERROR);
//Check CRC
if(g_frame_header.protection_bit == 0) //with CRC
{
if(Mp3_Read_CRC() != OK)
return(ERROR);
}
//get side info
if(g_frame_header.layer == 3) //is mp3?
{
Mp3_Get_Decode_Info(f_frame_info);
if (Mp3_Read_Main_Data (f_frame_info) != OK)
{
return (ERROR);
}
}
else
{
cout << "FILE IS NOT AN MP3 FILE!\n";
return(ERROR);
}
return(OK);
}
STATUS MP3_DECODER::Mp3_Read_Header()
{
UINT32 byte1, byte2, byte3, byte4, header;
byte1 = Get_Byte();
byte2 = Get_Byte();
byte3 = Get_Byte();
byte4 = Get_Byte();
//is EOF encountered?
if(byte1 == TEMP_EOF || byte2 == TEMP_EOF || byte3 == TEMP_EOF || byte4 == TEMP_EOF)
return(ERROR);
//arrange the bitstream into one 32 bit container
header = (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | (byte4 << 0);
//does header contain the sync byte?
if(header & MP3_SYNC != MP3_SYNC)
//if not, search for the first sync or EOF
while(1)
{
//shift the bits one byte to the left
byte1 = byte2;
byte2 = byte3;
byte3 = byte4;
//get the next byte
byte4 = Get_Byte();
//if byte is EOF then end
if(byte4 == TEMP_EOF)
return(ERROR);
//make the new header
header = (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | (byte4 << 0);
//if header has the sync then exit while
if(header & MP3_SYNC == MP3_SYNC)
break;
}//while(1)
/** If here, then the sync was found **/
//the following lines gets the bits from the header and interprets it
g_frame_header.id = (header & 0x00080000) >> 19;
g_frame_header.layer = (mpeg1_layer)((header & 0x00060000) >> 17);
g_frame_header.protection_bit = (header & 0x00010000) >> 16;
g_frame_header.bitrate_index = (header & 0x0000f000) >> 12;
g_frame_header.sampling_frequency = (header & 0x00000c00) >> 10;
g_frame_header.padding_bit = (header & 0x00000200) >> 9;
g_frame_header.private_bit = (header & 0x00000100) >> 8;
g_frame_header.mode = (mpeg1_channel_mode)((header & 0x000000c0) >> 6);
g_frame_header.mode_extension = (header & 0x00000030) >> 4;
g_frame_header.copyright = (header & 0x00000008) >> 3;
g_frame_header.original = (header & 0x00000004) >> 2;
g_frame_header.emphasis = (header & 0x00000003) >> 0;
if(g_frame_header.id != 1)
{
cout << g_frame_header.id;
cout << "ID value should be 1\n";
return(ERROR);
}
if(g_frame_header.bitrate_index == 0)
{
cout << "Free Bit rate index! (0 bps)\n";
return(ERROR);
exit (1); //terminate calling process
}
if(g_frame_header.bitrate_index == 15)
{
cout << "Bad bit rate!\n";
return(ERROR);
exit(1); //terminate calling process
}
if(g_frame_header.sampling_frequency == 3)
{
cout << "Bad sampling frequency!\n";
return(ERROR);
}
if(g_frame_header.layer == 0)
{
cout << "Layer 0 is invalid\n";
return(ERROR);
}
//change layer to corresponding value... 3 for layer 3....
g_frame_header.layer = (mpeg1_layer)(4 - g_frame_header.layer);
return(OK);
}
STATUS MP3_DECODER::Mp3_Read_CRC()
{
UINT32 byte1, byte2;
//get the next two bytes from the bitstream
byte1 = Get_Byte();
byte2 = Get_Byte();
//check if end of file
if(byte1 == TEMP_EOF || byte2 == TEMP_EOF)
return(ERROR);
cout << "\nCRC read\n";
return(OK);
}
STATUS MP3_DECODER::Mp3_Get_Decode_Info(mp3_frame_info &f_frame_info)
{
//UINT32 frameSize, sideInfoSize, mainDataSize, numChannels;
UINT32 channel, granule, scfsi_band, region, window;
//number of channels is 1 for mono 2 for stereo
f_frame_info.numChannels = (g_frame_header.mode == mpeg1_single_channel ? 1 : 2);
// Get header audio data size
f_frame_info.frameSize = (144 * g_mpeg_bitrates[g_frame_header.layer - 1][g_frame_header.bitrate_index]) /
g_sampling_frequency[g_frame_header.sampling_frequency] +
g_frame_header.padding_bit;
//Error if frame is more than 2kb
if(f_frame_info.frameSize > 2000)
{
cout << "Error! Frame size = " << f_frame_info.frameSize;
return(ERROR);
}
//Side info size is 17 bytes for 1 channel and 32 for two channels
f_frame_info.sideInfoSize = (f_frame_info.numChannels == 1 ? 17 : 32);
//main data size is the rest of the frame - the sync and header
f_frame_info.mainDataSize = f_frame_info.frameSize - f_frame_info.sideInfoSize - 4;
//minus another 2 bytes if there is CRC
f_frame_info.mainDataSize = (!g_frame_header.protection_bit ? f_frame_info.mainDataSize - 2 : f_frame_info.mainDataSize);
cout << "Frame size: " << f_frame_info.frameSize << "\n";
cout << "Side info size: " << f_frame_info.sideInfoSize << "\n";
cout << "Main data size: " << f_frame_info.mainDataSize << "\n";
cout << "Number of frames: " << Get_File_Size() / f_frame_info.frameSize << "\n\n";
if(Get_File_Pos() == Get_File_Size())
return(ERROR);
//parse the side info data
g_side_info.main_data_begin = Mp3_Get_Side_Bits(9);
//get private bits
if(g_frame_header.mode == mpeg1_single_channel)
g_side_info.private_bits = Mp3_Get_Side_Bits(5);
else
g_side_info.private_bits = Mp3_Get_Side_Bits(3);
//get scalefactor selection information
for(channel = 0; channel < f_frame_info.numChannels; channel++)
{
for(scfsi_band =0; scfsi_band < 4; scfsi_band++)
g_side_info.scfsi[channel][scfsi_band] = Mp3_Get_Side_Bits(1);
}
//get the rest of side info
for (granule = 0; granule < 2; granule++)
{
for (channel = 0; channel < f_frame_info.numChannels; channel++)
{
g_side_info.part2_3_length[granule][channel] = Mp3_Get_Side_Bits (12);
g_side_info.big_values[granule][channel] = Mp3_Get_Side_Bits (9);
g_side_info.global_gain[granule][channel] = Mp3_Get_Side_Bits (8);
g_side_info.scalefac_compress[granule][channel] = Mp3_Get_Side_Bits (4);
g_side_info.win_switch_flag[granule][channel] = Mp3_Get_Side_Bits (1);
if (g_side_info.win_switch_flag[granule][channel] == 1)
{
g_side_info.block_type[granule][channel] = Mp3_Get_Side_Bits (2);
g_side_info.mixed_block_flag[granule][channel] = Mp3_Get_Side_Bits (1);
for (region = 0; region < 2; region++)
{
g_side_info.table_select[granule][channel][region] = Mp3_Get_Side_Bits (5);
}
for (window = 0; window < 3; window++)
{
g_side_info.subblock_gain[granule][channel][window] = Mp3_Get_Side_Bits (3);
}
if ((g_side_info.block_type[granule][channel] == 2) &&
(g_side_info.mixed_block_flag[granule][channel] == 0))
{
g_side_info.region0_count[granule][channel] = 8;
}
else
{
g_side_info.region0_count[granule][channel] = 7;
}
g_side_info.region1_count[granule][channel] = 20 - g_side_info.region0_count[granule][channel];
}
else
{
for (region = 0; region < 3; region++)
{
g_side_info.table_select[granule][channel][region] = Mp3_Get_Side_Bits (5);
}
g_side_info.region0_count[granule][channel] = Mp3_Get_Side_Bits (4);
g_side_info.region1_count[granule][channel] = Mp3_Get_Side_Bits (3);
g_side_info.block_type[granule][channel] = 0;
}
g_side_info.preflag[granule][channel] = Mp3_Get_Side_Bits (1);
g_side_info.scalefac_scale[granule][channel] = Mp3_Get_Side_Bits (1);
g_side_info.count1table_select[granule][channel] = Mp3_Get_Side_Bits (1);
} //inner loop
} //outer loop
/* Done */
return(OK);
}
STATUS MP3_DECODER::Mp3_Get_Side_Info(UINT32 sideInfoSize)
{
return(OK);
}
UINT32 MP3_DECODER::Mp3_Get_Side_Bits(UINT32 numOfBits)
{
UINT32 temp;
//join the next four bytes to a word (32 bits)
temp = (pSideInfo[0] << 24) | (pSideInfo[1] << 16) | (pSideInfo[2] << 8) | (pSideInfo[3] << 0);
//remove used bits
temp = temp << sideInfoIndex;
//remove undesired bits
temp = temp >> (32 - numOfBits);
//update pointer and index
pSideInfo += (sideInfoIndex + numOfBits) >> 3;
sideInfoIndex = (sideInfoIndex + numOfBits) & 0x07;
return(temp);
}
STATUS MP3_DECODER::Mp3_Read_Main_Data(mp3_frame_info f_frame_info)
{
cout << "Frame size: " << f_frame_info.frameSize << "\n";
cout << "Side info size: " << f_frame_info.sideInfoSize << "\n";
cout << "Main data size: " << f_frame_info.mainDataSize << "\n";
cout << "Number of frames: " << Get_File_Size() / f_frame_info.frameSize << "\n\n";
return(OK);
}
STATUS MP3_DECODER::Mp3_Get_Main_Data(UINT32 main_data_begin, UINT32 main_data_size)
{
return(OK);
}
by the way, I get the following errors now:
ARGH! disregard that, I still get the same errors
|