📄 mp3emain.c
字号:
/******************************************************************************
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright (C) 2003 Intel Corporation. All Rights Reserved.
//
// Description:
// Intel(R) Integrated Performance Primitives Sample Code MP3 Encoder
//
// Function List:
// encoder_parse_cmdline_mp3()
// main()
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sampmp3.h"
/******************************************************************************
// Name: encoder_parse_cmdline_mp3
// Description: Function for process the command line
// Input Arguments : argc - number of input arguments.
// argv - pointer to pointer to input arguments list.
// fpinput_cmp - pointer to pointer to input file.
// fpoutput_mp3 - pointer to pointer to ouput file.
// Output Arguments: enc_state - pointer to encoder state structure.
// Returns: TRUE - successful.
// FALSE - error accur when process input arguments.
******************************************************************************/
SAMPLE_BOOL encoder_parse_cmdline_mp3(int argc, char **argv,
FILE **fpinput_cmp,FILE **fpoutput_mp3,
mp3_enc_state *enc_state)
{
int bit_rate;
int sample_rate;
int valid_flag;
int i,j;
if(2 == argc) {
if(strstr(argv[1], "-h")) {
printf("\
***************************Help information***************************\n\
Parameter list: \n\
-i inputFile -o outputFile -c channelNumber -b bitRate -s sampleRate \n\
InputFile *.PCM file. \n\
OutputFile *.mp3 file. \n\
ChannelNumber 1 or 2. \n\
BitRate 32000, 40000, 48000, 56000, 64000, 80000, 96000, \n\
112000, 128000, 160000, 192000, 224000, 256000, 320000 \n\
SampleRate 44100, 48000, 32000\n \n\
**********************************************************************\n\
");
}else {
printf("Invalid command parameters! use '-h' to see help.\n");
}
return FALSE;
}
if(2 != argc && 11 != argc ) {
printf("Invalid command parameters! use '-h' to see help.\n");
return FALSE;
}
/* Parse first arguument: inputFile and open it */
if(strcmp(argv[1], "-i")) {
printf("Input File not specified\n");
return FALSE;
} else {
*fpinput_cmp = NULL;
*fpinput_cmp = fopen(argv[2], "rb");
if(!(*fpinput_cmp)) {
printf("Input file not valid\n");
return FALSE;
}
}
/* Parse second argument: outputFile and open it */
if(strcmp(argv[3], "-o")) {
printf("Output File not specified\n");
return FALSE;
} else {
*fpoutput_mp3 = NULL;
*fpoutput_mp3 = fopen(argv[4], "wb");
if(!(*fpoutput_mp3)) {
printf("Output file not valid\n");
return FALSE;
}
}
i = 5;
while(i < 11) {
/* It is channel number */
if(!strcmp(argv[i], "-c")) {
if(!strcmp(argv[i + 1], "2")) {
enc_state->channel_num = 2;
} else if(!strcmp(argv[i + 1], "1")) {
enc_state->channel_num = 1;
} else {
printf("Invalid channel number, program terminated\n");
return FALSE;
}
} else if (!strcmp(argv[i], "-b")) {
/* It is bit rate */
bit_rate = atoi(argv[i + 1]);
valid_flag = 0;
for(j = 0; j < 15; j ++) {
if(bit_rate == mpeg1_bitrate_table[j]) {
enc_state->bitrate_index = j;
valid_flag = 1;
break;
}
}
if(!valid_flag) {
printf("Invalid bit rate value, program terminate\n");
return FALSE;
}
} else if (!strcmp(argv[i], "-s")) {
/* It is sample rate */
sample_rate = atoi(argv[i + 1]);
valid_flag = 0;
for(j = 0; j < 3; j ++) {
if(sample_rate == mpeg1_samplerate_table[j]) {
enc_state->sample_rate = j;
}
valid_flag = 1;
break;
}
if(!valid_flag) {
printf("Invalid sampling rate value, program terminated\n");
return FALSE;
}
} else {
printf("Invalid command parameters! use '-h' to see help.\n");
return FALSE;
}
i += 2;
}
return TRUE;
}
/******************************************************************************
// Name: main
//
// Description: Entry function for MP3 Encoder
// Usage: mp3enc -i inputFile -o outputFile -c channelNumber
// -b bitRate -s sampleRate
//
// where:
// inputFile is the name of a valid .PCM file
// outputFile is the name of cmpressed .MP3 file
// channelNumber is channel number (1 or 2)
// bitRate is the bit rate of output stream
// sampleRate is samping frenquency of raw data
// Input Arguments: argc - Standard C argument count
// argv - Standard C argument list
//
// Output Arguments: None
//
// Returns: 0 - No error when decoding
// -1 - Error when decoding
******************************************************************************/
int main(int argc, char **argv)
{
unsigned int channel_num;
int end_flag;
int frame_num;
int ret_code;
FILE *fpinput_cmp, *fpoutput_mp3;
mp3_enc_state *enc_state;
sample_sound sound;
sample_bitstream stream_buf;
enc_state = (mp3_enc_state *)malloc(sizeof(mp3_enc_state));
if(NULL == enc_state) {
printf("Error when allocate enc_state buffer!\n");
return -1;
}
/* Process the command line argument */
ret_code = encoder_parse_cmdline_mp3(argc, argv, &fpinput_cmp, \
&fpoutput_mp3, enc_state);
if(FALSE == ret_code) {
return -1;
}
/* Initialize the encoder */
ret_code = encoder_init_alloc_mp3(&sound, &stream_buf, enc_state);
if(SAMPLE_STATUS_NOERR != ret_code) {
printf("Initialization failed, program terminated!\n");
return -1;
}
/* Enter the encoding loop */
end_flag = FALSE;
channel_num = enc_state->channel_num;
frame_num = 0;
while(!end_flag) {
if(fread(sound.snd_frame, 2, channel_num * 1152, fpinput_cmp) <\
channel_num * 1152 ) {
end_flag = TRUE;
}
if(!end_flag) {
/******************************************************************
// return value:
// SAMPLE_STATUS_NOERR - one frame encode successful
// SAMPLE_STATUS_ERR - one frame encode error
// SAMPLE_STATUS_BADARG_ERR - bad input arguments
******************************************************************/
/* Encode one frame */
ret_code = encode_mp3(&sound, &stream_buf, enc_state);
frame_num ++;
if(ret_code != SAMPLE_STATUS_NOERR) {
printf("Error when encode frame %d!\n", frame_num);
}else {
printf("frame = %d\r", frame_num);
if((stream_buf.bs_bytelen - (stream_buf.bs_cur_byte - \
stream_buf.bs_buffer)) < 1024) {
fwrite(stream_buf.bs_buffer, 1, (stream_buf.bs_cur_byte - \
stream_buf.bs_buffer), fpoutput_mp3);
stream_buf.bs_cur_byte = stream_buf.bs_buffer;
}
}
} else {
fwrite(stream_buf.bs_buffer, 1, (stream_buf.bs_cur_byte - \
stream_buf.bs_buffer), fpoutput_mp3);
stream_buf.bs_cur_byte = stream_buf.bs_buffer;
}
}
/* Free encoder state structure */
free(enc_state);
/* Free input PCM and output bit stream buffer */
encoder_free_mp3(&sound, &stream_buf);
fclose(fpinput_cmp);
fclose(fpoutput_mp3);
printf("\nEncoding completed.\n");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -