⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3dec.cpp.bak

📁 基于mips架构的ATI-XILLEON 226的mp3解码程序
💻 BAK
📖 第 1 页 / 共 2 页
字号:
	

done:

	return (result) ;

}



/*===================================================================*/

int Mp3DecodeFrame(Mp3DecodeCtrl_t *pMp3DecCtrl)

{

	int 		result ;

	int		i ;

	int		Status = MP3DEC_ERR_GENERAL ;

	int 		DoFilter = 0 ;

	mad_fixed_t	Filter[32] ;

	unsigned char	*LeftChannelData = NULL ;

	unsigned char	*RightChannelData = NULL ;

	

	unsigned char	*InputBuffer ;

	Mp3DecodeCtrl_t *p = pMp3DecCtrl ;

	

	if (!p

		|| p->outbuf == NULL

		|| p->outbuf_len <= 0

		)

		goto done ;

		

	InputBuffer = &(p->InputBuffer[0]) ;



	if (p->pMp3Option)

	{

		if (!p->pMp3Option->notIgnoreCrc)

			p->Stream.options |= MAD_OPTION_IGNORECRC ;

		else

			p->Stream.options &= (!MAD_OPTION_IGNORECRC) ;



			

		if (p->pMp3Option->halfSamRateDecode)

			p->Stream.options |= MAD_OPTION_HALFSAMPLERATE ;

		else

			p->Stream.options &= (!MAD_OPTION_HALFSAMPLERATE) ;



# if 0  /* not yet implemented */

		if (p->pMp3Option->decodeChannel == 1)

		{

			p->Stream.options |= MAD_OPTION_LEFTCHANNEL ;

			p->Stream.options &= (!MAD_OPTION_RIGHTCHANNEL) ;

		}

		else

		if (p->pMp3Option->decodeChannel == 2)

		{

			p->Stream.options |= MAD_OPTION_RIGHTCHANNEL ;

			p->Stream.options &= (!MAD_OPTION_LEFTCHANNEL) ;

		}

		else

		if (p->pMp3Option->decodeChannel == 3)

		{

			p->Stream.options |= MAD_OPTION_LEFTCHANNEL ;

			p->Stream.options |= MAD_OPTION_RIGHTCHANNEL ;

		}

		else

		{

			p->Stream.options |= MAD_OPTION_LEFTCHANNEL ;

			p->Stream.options |= MAD_OPTION_RIGHTCHANNEL ;

		}

#endif

	}

	

try_again:

	if(p->Stream.buffer==NULL || p->Stream.error==MAD_ERROR_BUFLEN)

	{

		size_t			ReadSize ;

		size_t			Remaining ;

		unsigned char		*ReadStart ;

		size_t			r = 0 ;



		if (p->Stream.next_frame!=NULL)

		{

			Remaining = p->Stream.bufend - p->Stream.next_frame ;

			if (Remaining > 0)

				memmove(InputBuffer, p->Stream.next_frame, Remaining) ;

			ReadStart = InputBuffer + Remaining ;

			ReadSize = INPUT_BUFFER_SIZE - Remaining ;

		}

		else

		{

			ReadSize = INPUT_BUFFER_SIZE,

				ReadStart = InputBuffer,

				Remaining = 0 ;

		}



		/* Fill-in the buffer. If an error occurs print a message

		 * and leave the decoding loop. If the end of stream is

		 * reached we also leave the loop but the return status is

		 * left untouched.

		 */

		 if (p->r_h != 0)

		 {

		 	/*r = RingBuf_Read(p->r_h, ReadStart, 1, ReadSize) ;*/

		 	r = ReadFeedBuffer(p->r_h, (char*)ReadStart, 1,ReadSize);

		 	if (r <= 0)

		 	{

		 		/* undo it */

		 		if (Remaining > 0)

		 			memmove((void *)p->Stream.next_frame, InputBuffer, Remaining) ;

		 		Status = MP3DEC_ERR_DATAREQ ;

		 		if (_mp3decode_debug)

		 			printf("can't get data \n") ;

		 		goto done ;

		 	}

		 }

		 else

		 {

		 	Status = MP3DEC_ERR_GENERAL ;

		 	goto done ;	

		 }

		

		 mad_stream_buffer(&p->Stream, InputBuffer, r+Remaining) ;

		 p->Stream.error= (mad_error)0 ;

	}

	

	if (mad_frame_decode(&p->Frame, &p->Stream))

	{

		if(MAD_RECOVERABLE(p->Stream.error))

		{

			/* Do not print a message if the error is a loss of

			 * synchronization and this loss is due to the end of

			 * stream guard bytes. (See the comments marked {3}

			 * supra for more informations about guard bytes.)

			 */

			if (p->Stream.error!=MAD_ERROR_LOSTSYNC)

			{

				if (_mp3decode_debug)

				{

					fprintf(stderr,"recoverable frame level error (%s)\n",

							MadErrorString(&p->Stream));

					fflush(stderr);

				}

			}

			Status = MP3DEC_ERR_CANRECOV ;

			goto done ;

		}

		else

		{

			if(p->Stream.error==MAD_ERROR_BUFLEN)

			{

				/* try to get data from ring-buffer */

				goto try_again ;

			}

			else

			{

				if (_mp3decode_debug)

					fprintf(stderr,"unrecoverable frame level error (%s).\n",

							MadErrorString(&p->Stream));

				Status = MP3DEC_ERR_FATAL ;

				goto done ;

			}

		}

	}

	

	if(p->FrameCount == 0)

	{

		if (p->pMp3Info == NULL)

			p->pMp3Info = (Mp3Info_t *)malloc(sizeof(Mp3Info_t)) ;

			

		if (p->pMp3Info)

			FillMp3Info(p->pMp3Info, &(p->Frame.header)) ;

	}

	

	p->FrameCount++ ;

	mad_timer_add(&p->Timer, p->Frame.header.duration) ;



	/* Between the frame decoding and samples synthesis we can

	 * perform some operations on the audio data. We do this only

	 * if some processing was required. Detailed explanations are

	 * given in the ApplyFilter() function.

	 */

	if(DoFilter)

		ApplyFilter(&p->Frame, Filter) ;

		

	/* Once decoded the frame is synthesized to PCM samples. No errors

	 * are reported by mad_synth_frame();

	 */

	mad_synth_frame(&p->Synth, &p->Frame) ;

	

	/* calculate buffer */

	if (p->Synth.pcm.length > 0)

	{

		int output_len = (2*p->Synth.pcm.length) ;

		

		/* calulate output buffer is big enough */

		if (MAD_NCHANNELS(&p->Frame.header) == 2)

			output_len += (2*p->Synth.pcm.length) ;

		if (output_len > p->outbuf_len)

		{

			Status = MP3DEC_ERR_OUTPUTBUF ;

			goto done ;

		}

		

		p->output_len = output_len ;

		

		LeftChannelData = (unsigned char *) p->outbuf ;

		RightChannelData = LeftChannelData + 2 ;

	}

	else

	{

		Status = MP3DEC_ERR_CANRECOV ;

		p->output_len = 0 ;

		goto done ;

	}

	

	/* Synthesized samples must be converted from libmad's fixed

	 * point number to the consumer format. Here we use unsigned

	 * 16 bit big endian integers on two channels. Integer samples

	 * are temporarily stored in a buffer that is flushed when

	 * full.

	 */

	for(i=0;i<p->Synth.pcm.length;i++)

	{

		signed short	Sample;



		/* Left channel */

		Sample=MadFixedToSshort(p->Synth.pcm.samples[0][i]);

		if (LeftChannelData)

		{

			*LeftChannelData=(unsigned char)(Sample&0xff);

			LeftChannelData++ ;

			*LeftChannelData=(unsigned char)(Sample>>8);

			LeftChannelData++ ;

		}



		/* Right channel. If the decoded stream is monophonic then

		 * the right output channel is the same as the left one.

		 */

		if(MAD_NCHANNELS(&p->Frame.header)==2)

		{

			Sample=MadFixedToSshort(p->Synth.pcm.samples[1][i]);

			if (RightChannelData)

			{

				*RightChannelData=(unsigned char)(Sample&0xff);

				RightChannelData++ ;

				*RightChannelData=(unsigned char)(Sample>>8);

				RightChannelData++ ;

				if (LeftChannelData)

				{

					LeftChannelData = RightChannelData ;

					RightChannelData += 2 ;

				}

			}

		}

	}

	Status = MP3DEC_OK ;

		

done:

	result = Status ;

	return (result) ;

}



/*===================================================================*/

int Mp3DecodeEnd(Mp3DecodeCtrl_t *pMp3DecCtrl)

{

	int 		result = -1 ;

	char		Buffer[80] ;

	Mp3DecodeCtrl_t *p = pMp3DecCtrl ;

	

	if (!p)

		goto done ;

		

	mad_synth_finish(&p->Synth);

	mad_frame_finish(&p->Frame);

	mad_stream_finish(&p->Stream);



	if (_mp3decode_debug)

	{

		mad_timer_string(p->Timer,Buffer,"%lu:%02lu.%03u",

						 MAD_UNITS_MINUTES,MAD_UNITS_MILLISECONDS,0) ;

		fprintf(stderr,"\n %lu frames decoded (%s).\n",

				p->FrameCount,Buffer) ;

	}

	if (p->pMp3Info)

	{

		free(p->pMp3Info) ;

		p->pMp3Info = NULL ;

	}

	result = 0 ;

	

done:

	return (result) ;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -