2 * convert D-Cinema Audio (probably SMPTE 302M) to a
3 * wav file that MPlayer can play.
4 * Usage: 302m_convert <infile> <outfile>
10 #define le2me_32(x) (x)
11 #define le2me_16(x) (x)
12 #define be2me_16(x) bswap_16(x)
14 // From MPlayer libao/ao_pcm.c
15 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */
16 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */
17 #define WAV_ID_FMT 0x20746d66 /* "fmt " */
18 #define WAV_ID_DATA 0x61746164 /* "data" */
19 #define WAV_ID_PCM 0x0001
30 uint32_t bytes_per_second
;
37 static struct WaveHeader wavhdr
= {
38 le2me_32(WAV_ID_RIFF
),
40 le2me_32(WAV_ID_WAVE
),
49 le2me_32(WAV_ID_DATA
),
53 // this format is completely braindead, and this bitorder
54 // is the result of pure guesswork (counting how often
55 // the bits flip), so it might be wrong.
56 void fixup(unsigned char *in_
, unsigned char *out
) {
58 unsigned char in
[3] = {in_
[0], in_
[1], in_
[2]};
59 unsigned char sync
= in
[2] & 0x0f; // sync flags
62 for (i
= 0; i
< 4; i
++) {
67 for (i
= 0; i
< 4; i
++) {
73 for (i
= 0; i
< 4; i
++) {
78 for (i
= 0; i
< 4; i
++) {
84 for (i
= 0; i
< 4; i
++) {
90 out
[0] |= sync
; // sync flags go into lowest bits
91 // it seems those might also contain audio data,
92 // don't know if this is the right order then
93 // these might be also useful to detect the number
94 // of channels in case there are files with != 6 channels
97 int main(int argc
, char *argv
[]) {
98 FILE *in
= fopen(argv
[1], "r");
99 FILE *out
= fopen(argv
[2], "w");
101 uint16_t blocklen
, unknown
;
102 unsigned char *block
;
104 printf("Could not open %s for reading\n", argv
[1]);
108 printf("Could not open %s for writing\n", argv
[2]);
111 fwrite(&wavhdr
, 1, sizeof(wavhdr
), out
);
113 fread(&blocklen
, 2, 1, in
);
114 blocklen
= be2me_16(blocklen
);
115 fread(&unknown
, 2, 1, in
);
116 block
= malloc(blocklen
);
117 blocklen
= fread(block
, 1, blocklen
, in
);
118 for (i
= 0; i
< blocklen
; i
+= 3)
119 fixup(&block
[i
], &block
[i
]);
120 fwrite(block
, 1, blocklen
, out
);