summaryrefslogtreecommitdiff
path: root/dts2spdif.c
blob: 6aa15bb89fc0ad210581942d356fc4d6be0fc488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
#include <memory.h>

struct WAVEHEADER {
    unsigned long  ChunkID;
    unsigned long  ChunkSize;
    unsigned long  Format;
    unsigned long  SubChunk1ID;
    unsigned long  SubChunk1Size;
    unsigned short AudioFormat;
    unsigned short NumChannels;
    unsigned long  SampleRate;
    unsigned long  ByteRate;
    unsigned short BlockAlign;
    unsigned short BitsPerSample;
    unsigned long  SubChunk2ID;
    unsigned long  SubChunk2Size;
};


unsigned char burst[6144];

FILE *infile, *outfile;

unsigned long bytesread;
unsigned long pcmbytes;
unsigned long pcmbytesmultiplier;
char sampleratecode;
unsigned long framesize;

struct WAVEHEADER wavhdr = { 0x46464952,
							0,
							0x45564157,
							0x20746D66,
							16,
							1,
							2,
							0,
							0,
							4,
							16,
							0x61746164,
							0 };

unsigned long i;
char temp;

int main( int argc, char *argv[ ], char *envp[ ] )
{
	if (argc < 3)
	{
		printf("Wrong syntax. dts2spdif <In.dts> <Out.wav>.\n");
		return 0;
	}

	infile = fopen(argv[1], "rb");
	outfile = fopen(argv[2], "wb");
	//fseek(outfile, SEEK_SET, 44);
	fwrite (&wavhdr, sizeof(struct WAVEHEADER), 1, outfile);


	for(;;)
	{
		memset (burst, 0, 6144);
		bytesread = fread(burst, 1, 9, infile);
		if (bytesread < 9)
		{
			printf ("EOF reached (Frame Header reading)!\nCurrent position in INFILE: %i\n", ftell(infile));
			break;
		}
		if (((unsigned long*)burst)[0] != 0x0180fe7f)
		{
			printf("ERROR: INVALID SYNCWORD !\nCurrent position in INFILE: %i\n", ftell(infile));
			break;
		}
		framesize = (burst[5] & 3);
		framesize = framesize * 4096;
		framesize = framesize + (burst[6] * 16);
		framesize = framesize + ((burst[7] & 240) / 16);
		framesize = framesize + 1;

		sampleratecode = ((burst[8] & 60) / 4);

		if (wavhdr.SampleRate == 0)
		{
				printf ("First Sampleratecode: %i\n", sampleratecode);

				switch (sampleratecode)
				{
				case 1:
					pcmbytesmultiplier = 4;
				case 2:
					pcmbytesmultiplier = 2;
				case 3:
					pcmbytesmultiplier = 1;
					wavhdr.SampleRate = 32000;
					wavhdr.ByteRate = 128000;
					break;
				case 6:
					pcmbytesmultiplier = 4;
				case 7:
					pcmbytesmultiplier = 2;
				case 8:
					pcmbytesmultiplier = 1;
					wavhdr.SampleRate = 44100;
					wavhdr.ByteRate = 176000;
					break;
				case 11:
					pcmbytesmultiplier = 4;
				case 12:
					pcmbytesmultiplier = 2;
				case 13:
					pcmbytesmultiplier = 1;
					wavhdr.SampleRate = 48000;
					wavhdr.ByteRate = 192000;
					break;
				default:
					wavhdr.SampleRate = 0;
					wavhdr.ByteRate = 0;
					printf ("Invalid Sampleratecode ! Aborting process...\n");
					break;
				}
		}

		pcmbytes = (burst[4] & 1);
		pcmbytes = pcmbytes * 64;
		pcmbytes = pcmbytes + ((burst[5] & 252) / 4);
		pcmbytes = pcmbytes + 1;
		pcmbytes = pcmbytes * 128;
		pcmbytes = pcmbytes * pcmbytesmultiplier;


		bytesread = fread (&burst[9], 1, framesize - 9, infile);
		if ((bytesread + 9) < framesize)
		{
			printf ("EOF reached (Burst Reading)!\nCurrent position in INFILE: %i\n", ftell(infile));
			printf ("Frame size: %i  .. Bytes read: %i\n", framesize, bytesread);
			break;
		}

		for (i = 0; i < framesize; i += 2)
		{
			temp = burst[i];
			burst[i] = burst[i + 1];
			burst[i + 1] = temp;
		}


		fwrite (burst, 1, framesize, outfile);
		if (pcmbytes > framesize)
		{
			fwrite ("", 1, pcmbytes - framesize, outfile);
		}
		else
		{
			printf("Warning: Frame Size > LPCM Frame Size! Buffer underrun may occur/File damaged?\n");
			printf("Frame start at INFILE Offset: %i\n", ftell(infile) - framesize);
		}
	}

	printf ("Last Sampleratecode: %i\n", sampleratecode);

	wavhdr.SubChunk2Size = (ftell(outfile) - 44);
	wavhdr.ChunkSize = wavhdr.SubChunk2Size + 36;

	fseek (outfile, SEEK_SET, 0);
	fwrite (&wavhdr, sizeof(struct WAVEHEADER), 1, outfile);


	fclose (infile);
	fclose (outfile);

	return 0;
}