3 //#include <stdlib.h>
\r
4 //#include <string.h>
\r
8 unsigned long ChunkID;
\r
9 unsigned long ChunkSize;
\r
10 unsigned long Format;
\r
11 unsigned long SubChunk1ID;
\r
12 unsigned long SubChunk1Size;
\r
13 unsigned short AudioFormat;
\r
14 unsigned short NumChannels;
\r
15 unsigned long SampleRate;
\r
16 unsigned long ByteRate;
\r
17 unsigned short BlockAlign;
\r
18 unsigned short BitsPerSample;
\r
19 unsigned long SubChunk2ID;
\r
20 unsigned long SubChunk2Size;
\r
24 unsigned char burst[6144];
\r
26 FILE *infile, *outfile;
\r
28 unsigned long bytesread;
\r
29 unsigned long pcmbytes;
\r
30 unsigned long pcmbytesmultiplier;
\r
31 char sampleratecode;
\r
32 unsigned long framesize;
\r
34 struct WAVEHEADER wavhdr = { 0x46464952,
\r
51 int main( int argc, char *argv[ ], char *envp[ ] )
\r
55 printf("Wrong syntax. dts2spdif <In.dts> <Out.wav>.\n");
\r
59 infile = fopen(argv[1], "rb");
\r
60 outfile = fopen(argv[2], "wb");
\r
61 //fseek(outfile, SEEK_SET, 44);
\r
62 fwrite (&wavhdr, sizeof(struct WAVEHEADER), 1, outfile);
\r
67 memset (burst, 0, 6144);
\r
68 bytesread = fread(burst, 1, 9, infile);
\r
71 printf ("EOF reached (Frame Header reading)!\nCurrent position in INFILE: %i\n", ftell(infile));
\r
74 if (((unsigned long*)burst)[0] != 0x0180fe7f)
\r
76 printf("ERROR: INVALID SYNCWORD !\nCurrent position in INFILE: %i\n", ftell(infile));
\r
79 framesize = (burst[5] & 3);
\r
80 framesize = framesize * 4096;
\r
81 framesize = framesize + (burst[6] * 16);
\r
82 framesize = framesize + ((burst[7] & 240) / 16);
\r
83 framesize = framesize + 1;
\r
85 sampleratecode = ((burst[8] & 60) / 4);
\r
87 if (wavhdr.SampleRate == 0)
\r
89 printf ("First Sampleratecode: %i\n", sampleratecode);
\r
91 switch (sampleratecode)
\r
94 pcmbytesmultiplier = 4;
\r
96 pcmbytesmultiplier = 2;
\r
98 pcmbytesmultiplier = 1;
\r
99 wavhdr.SampleRate = 32000;
\r
100 wavhdr.ByteRate = 128000;
\r
103 pcmbytesmultiplier = 4;
\r
105 pcmbytesmultiplier = 2;
\r
107 pcmbytesmultiplier = 1;
\r
108 wavhdr.SampleRate = 44100;
\r
109 wavhdr.ByteRate = 176000;
\r
112 pcmbytesmultiplier = 4;
\r
114 pcmbytesmultiplier = 2;
\r
116 pcmbytesmultiplier = 1;
\r
117 wavhdr.SampleRate = 48000;
\r
118 wavhdr.ByteRate = 192000;
\r
121 wavhdr.SampleRate = 0;
\r
122 wavhdr.ByteRate = 0;
\r
123 printf ("Invalid Sampleratecode ! Aborting process...\n");
\r
128 pcmbytes = (burst[4] & 1);
\r
129 pcmbytes = pcmbytes * 64;
\r
130 pcmbytes = pcmbytes + ((burst[5] & 252) / 4);
\r
131 pcmbytes = pcmbytes + 1;
\r
132 pcmbytes = pcmbytes * 128;
\r
133 pcmbytes = pcmbytes * pcmbytesmultiplier;
\r
136 bytesread = fread (&burst[9], 1, framesize - 9, infile);
\r
137 if ((bytesread + 9) < framesize)
\r
139 printf ("EOF reached (Burst Reading)!\nCurrent position in INFILE: %i\n", ftell(infile));
\r
140 printf ("Frame size: %i .. Bytes read: %i\n", framesize, bytesread);
\r
144 for (i = 0; i < framesize; i += 2)
\r
147 burst[i] = burst[i + 1];
\r
148 burst[i + 1] = temp;
\r
152 fwrite (burst, 1, framesize, outfile);
\r
153 if (pcmbytes > framesize)
\r
155 fwrite ("", 1, pcmbytes - framesize, outfile);
\r
159 printf("Warning: Frame Size > LPCM Frame Size! Buffer underrun may occur/File damaged?\n");
\r
160 printf("Frame start at INFILE Offset: %i\n", ftell(infile) - framesize);
\r
164 printf ("Last Sampleratecode: %i\n", sampleratecode);
\r
166 wavhdr.SubChunk2Size = (ftell(outfile) - 44);
\r
167 wavhdr.ChunkSize = wavhdr.SubChunk2Size + 36;
\r
169 fseek (outfile, SEEK_SET, 0);
\r
170 fwrite (&wavhdr, sizeof(struct WAVEHEADER), 1, outfile);
\r