GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Jun-2004, 08:53
pablowablo pablowablo is offline
New Member
 
Join Date: Apr 2004
Posts: 24
pablowablo is on a distinguished road

link error?


I get these errors when I try to execute my program.

Code:
Linking... Mp3_Decoder.obj : error LNK2005: "int __cdecl Open_File(char *)" (?Open_File@@YAHPAD@Z) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "void __cdecl Close_File(void)" (?Close_File@@YAXXZ) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "unsigned int __cdecl Get_Byte(void)" (?Get_Byte@@YAIXZ) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "int __cdecl Get_Num_Bytes(unsigned int,unsigned int * const)" (?Get_Num_Bytes@@YAHIQAI@Z) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "void __cdecl Move_File_Pointer(unsigned int)" (?Move_File_Pointer@@YAXI@Z) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "unsigned int __cdecl Get_File_Pos(void)" (?Get_File_Pos@@YAIXZ) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "unsigned int __cdecl Get_File_Size(void)" (?Get_File_Size@@YAIXZ) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "struct stat results" (?results@@3Ustat@@A) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "long size" (?size@@3JA) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "class fstream myFile" (?myFile@@3Vfstream@@A) already defined in main.obj Mp3_Decoder.obj : error LNK2005: "long count" (?count@@3JA) already defined in main.obj Mp3_Decoder.obj : error LNK2001: unresolved external symbol "private: static unsigned int MP3_DECODER::sideInfoIndex" (?sideInfoIndex@MP3_DECODER@@0IA) Mp3_Decoder.obj : error LNK2001: unresolved external symbol "private: static unsigned int * MP3_DECODER::pSideInfo" (?pSideInfo@MP3_DECODER@@0PAIA) Mp3_Decoder.obj : error LNK2001: unresolved external symbol "private: static unsigned int * MP3_DECODER::sideInfoArray" (?sideInfoArray@MP3_DECODER@@0PAIA) Debug/main.exe : fatal error LNK1120: 3 unresolved externals Error executing link.exe. main.exe - 15 error(s), 0 warning(s)

I really don't know what these mean... Can anyone help me out?

Here's the scenario...

I have the following headers "Overall_Declarations.h", "Mp3_Decoder.h", "Bitstream.h"

and two code files "main.cpp" and "Mp3_Decoder.cpp"...

Mp3_Decoder.h includes Overall_Declarations.h and Bitstream.h
Mp3_Decoder.cpp includes Mp3_Decoder.h
main.cpp includes Mp3_Decoder.h
  #2  
Old 18-Jun-2004, 09:01
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
that's exactly the problem:
Mp3_Decoder.h includes Overall_Declarations.h and Bitstream.h
Mp3_Decoder.cpp includes Mp3_Decoder.h
main.cpp includes Mp3_Decoder.h

multiple includes cause the linker to throw a fit, because it's like declaring the same variables again and again.

in all your header files you must have the #ifndef macro for this reason.

open your Mp3_Decoder.h and do the following:
CPP / C++ / C Code:
//beginning of the file
#ifndef __Mp3_Decoder_h__
#define __Mp3_Decoder_h__

//rest of your file


#endif
//end of your file
do the similar thing for each one of your header files.
__________________
spasms!!!
  #3  
Old 18-Jun-2004, 09:08
pablowablo pablowablo is offline
New Member
 
Join Date: Apr 2004
Posts: 24
pablowablo is on a distinguished road
I already have those in all of my header files so I don't think that's it
  #4  
Old 18-Jun-2004, 09:10
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by pablowablo
I already have those in all of my header files so I don't think that's it

What compiler/linker are you using? If it is command line, what is your compilation statement?
  #5  
Old 18-Jun-2004, 09:10
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
i'm feeling a deja vu. It seems like we've covered this same thing before. Anyway, attach all your files, i'll take a look.
__________________
spasms!!!
  #6  
Old 18-Jun-2004, 17:19
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 167
dabigmooish will become famous soon enough
Quote:
Mp3_Decoder.obj : error LNK2001: unresolved external symbol "private: static unsigned int MP3_DECODER::sideInfoIndex" (?sideInfoIndex@MP3_DECODER@@0IA)
Mp3_Decoder.obj : error LNK2001: unresolved external symbol "private: static unsigned int * MP3_DECODER:SideInfo" (?pSideInfo@MP3_DECODER@@0PAIA)
Mp3_Decoder.obj : error LNK2001: unresolved external symbol "private: static unsigned int * MP3_DECODER::sideInfoArray" (?sideInfoArray@MP3_DECODER@@0PAIA)
From what I can see it seems like you have three function calls that call functions that are not defined

Overall_Declarations.h -> is this a predefined header file or one you made? If you made it then you probally need a Overall_Declarations.cpp file also. I suggest you post your code like machinated said
  #7  
Old 18-Jun-2004, 17:19
pablowablo pablowablo is offline
New Member
 
Join Date: Apr 2004
Posts: 24
pablowablo is on a distinguished road
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
CPP / C++ / C Code:


#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
CPP / C++ / C Code:

#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

CPP / C++ / C Code:

#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
CPP / C++ / C Code:

#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

CPP / C++ / C Code:
#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);
}
  #8  
Old 18-Jun-2004, 17:28
pablowablo pablowablo is offline
New Member
 
Join Date: Apr 2004
Posts: 24
pablowablo is on a distinguished road
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
CPP / C++ / C Code:


#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
CPP / C++ / C Code:

#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

CPP / C++ / C Code:

#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
CPP / C++ / C Code:

#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

CPP / C++ / C Code:
#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
  #9  
Old 18-Jun-2004, 17:31
pablowablo pablowablo is offline
New Member
 
Join Date: Apr 2004
Posts: 24
pablowablo is on a distinguished road
Quote:
Originally Posted by dabigmooish
From what I can see it seems like you have three function calls that call functions that are not defined

Overall_Declarations.h -> is this a predefined header file or one you made? If you made it then you probally need a Overall_Declarations.cpp file also. I suggest you post your code like machinated said


phew, almost didn't see your post there... Hmmm I'll check the methods out
ohhh do I really need to have an Overall_Declarations.cpp? It only contains my typedefs and I dont have anything to put in a cpp file :-? Anyway I'll check it out.
  #10  
Old 18-Jun-2004, 17:44
pablowablo pablowablo is offline
New Member
 
Join Date: Apr 2004
Posts: 24
pablowablo is on a distinguished road
lol, posting my own code made me realize my mistake )

I fixed it now though I don't really know why it was wrong... (I just made the transition from java to C++ so I'm having some problems with these stuff)

anyway, I fixed it by making another file Bitstream.cpp that contains the functions originally in Bitstream.h



Thanks for all those who helped!
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Operator Overloading: << aaroncohn C++ Forum 36 07-Dec-2004 20:22
Hmm what seems to be the problem here? error C2061: syntax error pablowablo C++ Forum 5 12-Jun-2004 23:11
OpenGL always reports error mvt OpenGL Programming 2 04-Jun-2004 07:42
Visual C++ 6 Compiler error vip3r C++ Forum 2 13-Apr-2004 15:34
[script] E-mail webmaster error page BobbyDouglas PHP Code Library 0 19-Aug-2003 21:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.