3 //#include <stdlib.h>
\r
4 //#include <string.h>
\r
7 //#include "fs_codes.h"
\r
10 short bitrates[] = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
\r
11 192, 224, 256, 320, 384, 448, 512, 576, 640 };
\r
13 short fsize44[] = { 70, 88, 105, 122, 140, 175, 209, 244, 279, 349,
\r
14 418, 488, 558, 697, 836, 976, 1115, 1254, 1394 };
\r
16 unsigned long DecFS(char sratecode, unsigned long fsizecode)
\r
21 return (bitrates[fsizecode / 2] * 2);
\r
23 return ((fsizecode % 2) ? fsize44[fsizecode / 2] : fsize44[fsizecode / 2] - 1);
\r
25 return (bitrates[fsizecode / 2] * 3);
\r
37 unsigned long ChunkID;
\r
38 unsigned long ChunkSize;
\r
39 unsigned long Format;
\r
40 unsigned long SubChunk1ID;
\r
41 unsigned long SubChunk1Size;
\r
42 unsigned short AudioFormat;
\r
43 unsigned short NumChannels;
\r
44 unsigned long SampleRate;
\r
45 unsigned long ByteRate;
\r
46 unsigned short BlockAlign;
\r
47 unsigned short BitsPerSample;
\r
48 unsigned long SubChunk2ID;
\r
49 unsigned long SubChunk2Size;
\r
53 struct AC3PREAMBLE {
\r
54 unsigned short sync1;
\r
55 unsigned short sync2;
\r
56 unsigned char burst_infoLSB;
\r
57 unsigned char burst_infoMSB;
\r
58 unsigned short lengthcode;
\r
63 FILE *infile, *outfile;
\r
65 unsigned long bytesread;
\r
66 unsigned long payloadbytes;
\r
67 char sampleratecode;
\r
68 unsigned long framesizecode;
\r
70 struct AC3PREAMBLE ac3p = { 0xF872, 0x4E1F, 1, 0, 0x3800 };
\r
71 struct WAVEHEADER wavhdr = { 0x46464952,
\r
88 int main( int argc, char *argv[ ], char *envp[ ] )
\r
92 printf("Wrong syntax. AC3PACK <In.ac3> <Out.wav>.\n");
\r
96 infile = fopen(argv[1], "rb");
\r
97 outfile = fopen(argv[2], "wb");
\r
98 //fseek(outfile, SEEK_SET, 44);
\r
99 fwrite (&wavhdr, sizeof(struct WAVEHEADER), 1, outfile);
\r
105 memset (burst, 0, 6144);
\r
106 bytesread = fread(&burst[8], 1, 6, infile);
\r
109 printf ("EOF reached (Frame Header reading)!\nCurrent position in INFILE: %i\n", ftell(infile));
\r
112 if ((burst[8] != 0x0B) || (burst[9] != 0x77))
\r
114 printf("ERROR: INVALID SYNCWORD !\nCurrent position in INFILE: %i\n", ftell(infile));
\r
117 framesizecode = (burst[12] & 63);
\r
118 sampleratecode = ((burst[12] & 192) / 64);
\r
120 if (wavhdr.SampleRate == 0)
\r
122 printf ("First Sampleratecode: %i\n", sampleratecode);
\r
124 switch (sampleratecode)
\r
127 wavhdr.SampleRate = 48000;
\r
128 wavhdr.ByteRate = 192000;
\r
131 wavhdr.SampleRate = 44100;
\r
132 wavhdr.ByteRate = 176000;
\r
135 wavhdr.SampleRate = 32000;
\r
136 wavhdr.ByteRate = 128000;
\r
139 wavhdr.SampleRate = 48000;
\r
140 wavhdr.ByteRate = 192000;
\r
144 payloadbytes = DecFS (sampleratecode, framesizecode);
\r
147 bytesread = fread (&burst[14], 1, payloadbytes - 6, infile);
\r
148 if ((bytesread + 6) < payloadbytes)
\r
150 printf ("EOF reached (Burst Reading)!\nCurrent position in INFILE: %i\n", ftell(infile));
\r
151 printf ("Frame size: %i .. Bytes read: %i\n", payloadbytes, bytesread);
\r
154 ac3p.burst_infoMSB = (burst[13] & 7);
\r
155 ac3p.lengthcode = (short)(payloadbytes * 8);
\r
157 for (i = 8; i < (payloadbytes + 8); i += 2)
\r
160 burst[i] = burst[i + 1];
\r
161 burst[i + 1] = temp;
\r
164 memcpy (burst, &ac3p, sizeof(struct AC3PREAMBLE));
\r
166 fwrite (burst, 1, 6144, outfile);
\r
170 printf ("Last Sampleratecode: %i\n", sampleratecode);
\r
172 wavhdr.SubChunk2Size = (ftell(outfile) - 44);
\r
173 wavhdr.ChunkSize = wavhdr.SubChunk2Size + 36;
\r
175 fseek (outfile, SEEK_SET, 0);
\r
176 fwrite (&wavhdr, sizeof(struct WAVEHEADER), 1, outfile);
\r