當前位置:成語大全網 - 新華字典 - 求助:關於C語言壓縮MP3算法!

求助:關於C語言壓縮MP3算法!

具體怎樣將記錄了模擬音頻信號的文件轉換為mp3格式文件,我也不知道具體,原理還是可以告訴妳!

但是,無論什麽文件,都可以使用huffman編碼來壓縮,

huffman編碼是壹種無損的編碼,就是可以還原樣的編碼

給妳壹個我大壹看的壹huffman編碼程序,可以編碼也可以解碼

比樓上的強壹點,找qq:504449327要,就說要huffman編碼的程序

這個是我同學的哈夫曼編碼程序

另外還有解碼的程序,要的話再商量

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define TRUE 1

#define ERROR 0

#define OK 1

#define FALSE 0

#define INFEASIBLE -1

#define OVERFLOW -2

#define Status int

#define MAXLENGTH 128

typedef struct HTnode

{

long weight;

int parent;

int lchild;

int rchild;

}HTNode, *HuffmanTree;

typedef struct CTnode

{

long weight;

char *coded_string;

}CharacterTable;

typedef char * *HuffmanCode;

FILE *fp=NULL;

void Analyse (CharacterTable * *character_table, long * *w, char * *chara, int &n)//分析所有不同的字符的權值

{

long *tmpw;

char ch, *tmpchara;

int i;

(*character_table)=(CharacterTable *)malloc(128*sizeof(CharacterTable));//定義存放字母的數組

for(i=0; i<128; i++)

{

(*character_table)[i].weight=0; //初始化

(*character_table)[i].coded_string=NULL;

}

ch=fgetc(fp);

while(!feof(fp))//諾到文件末尾,函數值為真

{

//m=ch;

if(ch<128 && ch>=0)

(*character_table)[ch].weight++;//獲得各個字母在文件中出現的次數

ch=fgetc(fp);

}

for(i=0, n=0; i<128; i++)

if((*character_table)[i].weight)

n++; //統計有多少不同的字符數

(*w)=(long *)malloc(n*sizeof(long));//deliver the character and the weight to main

(*chara)=(char *)malloc(n*sizeof(char));

tmpw=(*w);

tmpchara=(*chara);

for(i=0; i<128; i++)

if((*character_table)[i].weight)

{//將權值放入*w數組中

*(*w)=(*character_table)[i].weight;

*(*chara)=i;//這裏i是字符

(*w)++;

(*chara)++;

}

(*w)=tmpw;

(*chara)=tmpchara;//指針返回數組頭

}

void Select (HuffmanTree *HT, int i, int *Min1, int *Min2)

{

int j, n, tmp1=-1, tmp2=-2;

for(n=0; n<i; n++)

{

if(!(*HT)[n].parent)

{

if(tmp1 == -1)

{

tmp1=n;

continue;

}

if(tmp2 == -2)

{

tmp2=n;

if((*HT)[tmp1].weight > (*HT)[tmp2].weight)

{

j=tmp1;

tmp1=tmp2;

tmp2=j;

}

continue;

}

if((*HT)[n].weight < (*HT)[tmp2].weight) //scan and change

if((*HT)[n].weight < (*HT)[tmp1].weight)

tmp1=n;

else

tmp2=n;

}

}

*Min1=tmp1;

*Min2=tmp2; //tmp[Min2].weight >= tmp[Min1].weight

}

Status Huffman(HuffmanTree *HT, HuffmanCode *HC,long *w, int n)

{

int m, i, Min1, Min2, p1, p2, start, *M1, *M2;

char *cd;

HuffmanTree *HTp;

if(n<1) return ERROR;

m=2*n-1;

(*HT)=(HTNode *)malloc(m*sizeof(HTNode)); //intialise Hc in main

HTp=HT;

for(i=0; i<n; i++, w++)

{

(*HTp)[i].weight=*w;

(*HTp)[i].parent=0;

(*HTp)[i].lchild=0;

(*HTp)[i].rchild=0;

}

for(; i<m; i++)

{

(*HTp)[i].weight=0;

(*HTp)[i].parent=0;

(*HTp)[i].lchild=0;

(*HTp)[i].rchild=0;

}

M1=&Min1;

M2=&Min2;

for(i=n; i<m; i++)

{

Select(HT, i, M1, M2);

(*HTp)[Min1].parent=i;

(*HTp)[Min2].parent=i;

(*HTp)[i].lchild=Min1; //左孩子要小壹些

(*HTp)[i].rchild=Min2;

(*HTp)[i].weight=(*HTp)[Min1].weight + (*HTp)[Min2].weight;

}

//coded the weight below

(*HC)=(HuffmanCode)malloc(n*sizeof(char *));

cd=(char *)malloc(n*sizeof(char));

cd[n-1]='\0';

for(i=0; i<n; i++)

{

start=n-1;

for(p1=i, p2=(*HTp)[p1].parent; p2!=0; p1=p2, p2=(*HTp)[p1].parent)

{

if( (*HTp)[p2].lchild ==p1) //編碼, 左孩子為0, 右孩子為1

cd[--start]='0';

else

cd[--start]='1';

}

(*HC)[i]=(char *)malloc((n-start)*sizeof(char));

strcpy((*HC)[i],&cd[start]);

} //over

return OK;

}

void Weinumber_to_stringnumber(char * *stringnumber, long *w, int leaves)

{//將權值以字符數組形式存放在上米的數組中

char tmp[30];

long i, j, k;

int start;

for(i=0; i<leaves; i++)

{

start=29;

tmp[start--]='\0';

for(k=w[i], j=k%10; k!=0; k=k/10, j=k%10)

tmp[start--]=j+'0';

stringnumber[i]=(char *)malloc((29-start)*sizeof(char));

strcpy(stringnumber[i], &tmp[start+1]);

}

}

void Save_huffman_weight_dictionary(long *w, char *character, int leaves, HuffmanCode *HC)

{

char * *stringnumber;

int i;

FILE *fp1;

fp1=fopen("weight.txt", "w");

stringnumber=(char * *)malloc(leaves * sizeof(char *));

Weinumber_to_stringnumber(stringnumber, w, leaves);

for(i=0; i<leaves; i++)

{

fputc(' ', fp1); // for unhuffman add '

fputc(character[i], fp1);

fputc('\t', fp1);

fputs(stringnumber[i], fp1);

fputc('\t', fp1);

fputc('\'', fp1);

fputs((*HC)[i], fp1);

fputc('\'', fp1);

fputc('\n', fp1);

}

fclose(fp1);

}

void Huffman_file_convert(HuffmanCode *HC, CharacterTable *character_table) //fp had opened

{

int i;

char ch;

FILE *fp2=fopen("coded.txt","w");

for( i=0; i<128; i++)

if(character_table[i].weight)

{

character_table[i].coded_string=*(*HC);

(*HC)++;

}

ch=fgetc(fp);

while(!feof(fp))

{

if( (ch>=0 && ch<128) && (character_table[ch].weight) )//it is very importan to add (ch>=0 && ch<128)

fputs(character_table[ch].coded_string,fp2);

ch=fgetc(fp);

}

fclose(fp2);

}

void fileopen1() //通過指針fp傳遞信息

{

char filename[100];

do{

printf("\n\n\t請輸入要編碼的文件:");

scanf("%s", filename);

if ((fp=fopen(filename,"r"))==NULL)

printf("\n\t不能打開此文件! 請重新輸入!\n");

}while(!fp);

}

void main()

{

HuffmanTree Ht, *ht;//three level pointer

HuffmanCode Hc, *hc;

CharacterTable *CT, * *character_table;

long *weight, * *w;

char * character, * *chara;

int leave; //the all leaves number

ht=&Ht;

hc=&Hc;

w=&weight;

chara=&character;

character_table=&CT;

fileopen1();

Analyse(character_table, w, chara, leave);

fseek(fp, 0, 0);//將文件指針還原

Huffman(ht, hc, weight, leave);//構建哈弗曼樹!

Save_huffman_weight_dictionary(weight, character, leave, hc);

Huffman_file_convert(hc, CT);

fclose(fp);

}