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

📄 main.c

📁 embedded linux 下MP3的解码程序
💻 C
字号:
//****************************************************************************
// 
// Faraday Technology Coporation 
// Copyright (C) Microsoft Corporation. All rights reserved.
//                                
//*--------------------------------------------------------------------------*
//* Name:main.c                                                              *
//* Description: MPEG-1/2 Audio Layer 3 Decoder example main                 *
//* Author: Shawn, Hsiao                                                     *
//* Date: 2006/06/06                                                         *
//* Version:1.3	                                                             *
//****************************************************************************

//============================================================================
//        Include FIle                                 
//============================================================================
#include 	"FMAD.h"				// FMAD header file
#include 	<stdio.h>
#include 	<stdlib.h>
#include 	<fcntl.h>
#include 	<errno.h>
#include 	<getopt.h>
#include	<linux/soundcard.h> 

//============================================================================
//        Global Variables
//============================================================================

struct MP3dec_info FMAD_info;

char input_buf[IN_SIZE];			// MP3 decoder input buffer
char output_buf[OUT_SIZE];		// MP3 decoder output buffer

volatile int output_mode = 0;	// 0: audio driver, 1: file

//============================================================================
//        Call-back functions
//============================================================================

//******************************************************************************
// 
// int OutputPCM(char *, int, int)
//  
//******************************************************************************
//      
// input: 
// parameter 1 -> output buffer pointer 
// parameter 2 -> output data size in bytes 
// parameter 3 -> output file ("0" means no file output)
// output: data size which is really output in bytes
//
// This call-back function is called when decoder decoded one frame data.
// For the case of MP3 decoder, the data in one frame is fixed to 9216 bytes
// (32-bits output sample) or 4608 bytes (16-bits output sample).
// It is suggested that when the function is called, the main program must 
// get all output samples at once. Otherwise, the decoder will keep calling
// until all output samples are got.
// The coorect size of output data, which is really got from output buffer, must be 
// given to make sure the decoder works normally.
//
//******************************************************************************

int OutputPCM(char *output_buf, int output_size, int output_file)
{
	return write(output_file, output_buf, output_size);
}

//******************************************************************************
// 
// int InputData(char *, int, int)
//  
//******************************************************************************
//      
// input: 
// parameter 1 -> input buffer pointer
// parameter 2 -> input size
// parameter 3 -> input file ("0" means no file input)
// output: data size which is really input in bytes
//
// This call-back function is called when decoder needs more data to input buffer.
// The decoder gives an expected data size and a pointer, which the data must be placed at.
// The coorect size of input data, which is really placed to input buffer, must be 
// given to make sure the decoder works normally.
//
//******************************************************************************

int InputData(char *input_buf, int input_size, int input_file)
{
	if (input_file != 0)
		return(read(input_file, input_buf, input_size));
	
	return 0;
}

//******************************************************************************
// 
// int DataSeek(int, int, int)
//  
//******************************************************************************
//      
// input: 
// parameter 1 -> input file ("0" means no file input)
// parameter 2 -> data seek size
// parameter 3 -> seek type 0: seek from start, 1: seek from current, 2: seek from end
// output: error message -> 0: ok, others: fail
//
// This function is called when the decoder want to seek to some specific point in 
// the bitstream or file.
//
//******************************************************************************

int DataSeek(int input_file, int size, int type)
{
	int seek_mode;
        
	switch (type)
	{
		case 0:
			seek_mode = SEEK_SET;
			break;
 		case 1:
			seek_mode = SEEK_CUR;
			break;
		case 2:
			seek_mode = SEEK_END;
			break;
		default:
			seek_mode = SEEK_SET;
			break;
	}
        
	return (lseek(input_file, size, seek_mode));        
}

//******************************************************************************
// 
// void DisplaySet_MP3dec(struct MP3dec_info *)
//  
//******************************************************************************
//      
// input: 
// parameter 1 -> MP3 decoder information pointer
// output: NA
//
// This function is called when the decoder can serve the display and set process.
// It can be defined 1) once per frame, or 2) once per input data.
// If the program does not need to display or set, this function must be sure as
// an empty function. Otherwise, it may does some unpredictable processing.
//
//******************************************************************************

int Set_Audio(struct MP3dec_info *mp_c)
{
	int freq = mp_c->sampling_frequency;
	int stereo;
	int out_file = mp_c->output_file;

	if (mp_c->mode == 0)stereo = 0;
	else stereo = 1;

	if (ioctl(out_file, SNDCTL_DSP_SPEED, &freq) < 0)
	{
		printf("sampling frequency fail\n\n");	
		return (0);
	}
	if (ioctl(out_file, SNDCTL_DSP_STEREO, &stereo) < 0)
	{
		printf("stereo fail\n\n");
		return (0);
	}

	return (1);
}

void DisplaySet_MP3dec(struct MP3dec_info *mp_c)
{
	int mod = mp_c->sampling_frequency * (mp_c->mode + 1);
	int count_all;
	int second_count;
	int minute_count;
	int main_count;
	char mode[6];
	int i;
	
	count_all = mp_c->count;			// get time information	
	main_count = count_all & 0xFFFFF;			// get basic count	
	second_count = (count_all >> 20) & 0x3F;		// get second
	minute_count = (count_all >> 26) & 0x3F;	// get minute
	
	if (!output_mode)
		Set_Audio(mp_c);
			
	if (main_count >= mod)
	{			
		/************** Display MPEG information *********************/	
		// display time in the form mm:ss (m:minute, s:second)		
		
		fprintf(stderr, "%02d:%02d, ", minute_count, second_count);

		// display MP3 decoder content
		// display layer, bit rate, sampling frequency
		fprintf(stderr, "Layer %d, %d kbps, %d Hz, ", mp_c->layer, mp_c->bit_rate, mp_c->sampling_frequency);
        	
		// display channel mode
    if (mp_c->mode == 0)
    {
			mode[0] = 'm'; mode[1] = 'o'; mode[2] = 'n'; mode[3] = 'o'; mode[4] = ' '; mode[5] = ' ';
		}
		else
		{
			mode[0] = 's'; mode[1] = 't'; mode[2] = 'e'; mode[3] = 'r'; mode[4] = 'e'; mode[5] = 'o';
		}
        	
		for (i = 0; i < 6 ; i ++)
			fprintf(stderr, "%c", mode[i]);
        		
		// display EQ mode			
		switch (mp_c->EQ)
		{
			case 1: fprintf(stderr, ", Treble ");
				break;
			case 2: fprintf(stderr, ", Bass   ");
				break;
			case 3: fprintf(stderr, ", Jazz");
				break;
			case 4: fprintf(stderr, ", Classic");		
				break;
			case 5: fprintf(stderr, ", Rock   ");
				break;
			default: fprintf(stderr, ", Normal ");
				break;
		}
		
		fprintf(stderr, "\r");
		/********** End of Display MP3 decoder information ******************/		
	}
}

//******************************************************************************
// 
// main
//                                
//******************************************************************************
//
// This is an example main function for Faraday MP3 Decoder on FA410.
//
//******************************************************************************
int main(int argc, char *argv[])
{
	char *in_file_name, *out_file_name;
	char *eq_mode_sel;
	int eq_sel = 0;
	int in_file, out_file;
	int err;      
	
	/**************** Display title ******************************/
	printf("\n====================================================");
	printf("\n\n      Faraday MPEG Audio Decoder (FMAD) V1.3\n\n");
	printf("====================================================\n");
	/**************** End of Display title ***********************/	

	// Ensure commands are valid
	if(4 != argc)
	{
		fprintf(stderr,"! Invalid command line.\r\n\r\n\tUsage : fmad <input file> <output file> <EQ>\r\n\r\n");
		exit(-1);
	}

	/************ Access input information ***********************/
	// Get input argument
	in_file_name = argv[1];
	out_file_name = argv[2];
	eq_mode_sel = argv[3];
	
	// Open input & output file 
	in_file = open(in_file_name,O_RDONLY);
	out_file = open(out_file_name, (O_WRONLY | O_CREAT | O_TRUNC), 0660);
	
	if (!strcmp(out_file_name, "/dev/dsp"))		output_mode = 0;                	else		output_mode = 1; 			

	// Get equalizer mode
	switch (*eq_mode_sel)
	{
		case '1': eq_sel = 1;
			  break;
		case '2': eq_sel = 2;
			  break;
		case '3': eq_sel = 3;
			  break;
		case '4': eq_sel = 4;
			  break;
		case '5': eq_sel = 5;
			  break;
		default: eq_sel = 0;
			 break;
	}
	
	/*** Initial MP3 decoder infomation & Data & Callback functions *******/
	// The initail processing is used to initialize IO and callback
	// functions including:
	// 1) initail MP3 decoder information
	// 2) set I/O file (if has one) and I/O buffer.
	// 3) set callback functions.
	err = init_MP3dec_info(&FMAD_info);
	
	if (err != 0)
	{
		fprintf(stderr, "\nInitialize MP3 decoder information error!!!\n");
		return (1);
	}
        
	err = init_Data(&FMAD_info, (int)in_file, (int)out_file, &input_buf[0], &output_buf[0]);
	
	if (err != 0)
	{
		fprintf(stderr, "\nInitialize IO error!!!\n");
		return (1);
	}
	
	err = init_CallbackFunction(&FMAD_info, &InputData, &OutputPCM, &DataSeek, &DisplaySet_MP3dec);
	
	if (err != 0)
	{
		fprintf(stderr, "\nInitialize Call-back functions error!!!\n");
		return (1);
	}
	
	/******************* Initial MP3 decoder **********************/
	// The initial processing handle three processing:
	// 1. Check ID3 tag. If there is one, skip it.
	// 2. Find the first valid header (frame) and ignore the data before it.
	// 3. Initial MP3 decoder.
	// If initializing is OK, return "0".
	err = init_Faraday_MP3dec(&FMAD_info);
	
	if (err != 0)
	{
		fprintf(stderr, "\nInitialize Faraday MP3 Decoder error!!!\n");
		return (1);
	}	
	
	//set EQ
	set_Faraday_EQ(eq_sel);

	// show welcome message
	fprintf(stderr, "Start MP3 decoder\n");
        
	/**************** MP3 decoder processing **********************/
	// The MP3 decoder proccessing includes three steps:
	// 1. Read data form file to input buffer.
	// 2. Decode data in input buffer.
	// 3. After one frame is decoded, write them to output file.
	err = do_Faraday_MP3dec(&FMAD_info);
	
	if (err != 0)
	{
		fprintf(stderr, "\nInput Data error!!!\n");
		return (1);
	} 
	
	close(in_file);        // close input file
	close(out_file);       // close output file
	
	return (0);
}


⌨️ 快捷键说明

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