1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /* A test program for the Rockbox version of the ffmpeg FLAC decoder.
24 Compile using Makefile.test - run it as "./test file.flac" to decode the
25 FLAC file to the file "test.wav" in the current directory
27 This test program should support 16-bit and 24-bit mono and stereo files.
29 The resulting "test.wav" should have the same md5sum as a WAV file created
30 by the official FLAC decoder (it produces the same 44-byte canonical WAV
40 #include <sys/types.h>
45 static unsigned char wav_header
[44]={
46 'R','I','F','F',// 0 - ChunkID
47 0,0,0,0, // 4 - ChunkSize (filesize-8)
48 'W','A','V','E',// 8 - Format
49 'f','m','t',' ',// 12 - SubChunkID
50 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
51 1,0, // 20 - AudioFormat (1=Uncompressed)
52 2,0, // 22 - NumChannels
53 0,0,0,0, // 24 - SampleRate in Hz
54 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
55 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
56 16,0, // 34 - BitsPerSample
57 'd','a','t','a',// 36 - Subchunk2ID
58 0,0,0,0 // 40 - Subchunk2Size
61 int open_wav(char* filename
) {
64 fd
=open(filename
,O_CREAT
|O_WRONLY
|O_TRUNC
,S_IRUSR
|S_IWUSR
);
66 write(fd
,wav_header
,sizeof(wav_header
));
71 void close_wav(int fd
, FLACContext
* fc
) {
76 bytespersample
=fc
->bps
/8;
78 filesize
=fc
->totalsamples
*bytespersample
*fc
->channels
+44;
82 wav_header
[4]=(x
&0xff);
83 wav_header
[5]=(x
&0xff00)>>8;
84 wav_header
[6]=(x
&0xff0000)>>16;
85 wav_header
[7]=(x
&0xff000000)>>24;
88 wav_header
[22]=fc
->channels
;
91 wav_header
[24]=fc
->samplerate
&0xff;
92 wav_header
[25]=(fc
->samplerate
&0xff00)>>8;
93 wav_header
[26]=(fc
->samplerate
&0xff0000)>>16;
94 wav_header
[27]=(fc
->samplerate
&0xff000000)>>24;
97 x
=fc
->samplerate
*(fc
->bps
/8)*fc
->channels
;
98 wav_header
[28]=(x
&0xff);
99 wav_header
[29]=(x
&0xff00)>>8;
100 wav_header
[30]=(x
&0xff0000)>>16;
101 wav_header
[31]=(x
&0xff000000)>>24;
104 wav_header
[32]=(fc
->bps
/8)*fc
->channels
;
107 wav_header
[34]=fc
->bps
;
111 wav_header
[40]=(x
&0xff);
112 wav_header
[41]=(x
&0xff00)>>8;
113 wav_header
[42]=(x
&0xff0000)>>16;
114 wav_header
[43]=(x
&0xff000000)>>24;
116 lseek(fd
,0,SEEK_SET
);
117 write(fd
,wav_header
,sizeof(wav_header
));
121 static void dump_headers(FLACContext
*s
)
123 fprintf(stderr
," Blocksize: %d .. %d\n", s
->min_blocksize
,
125 fprintf(stderr
," Framesize: %d .. %d\n", s
->min_framesize
,
127 fprintf(stderr
," Samplerate: %d\n", s
->samplerate
);
128 fprintf(stderr
," Channels: %d\n", s
->channels
);
129 fprintf(stderr
," Bits per sample: %d\n", s
->bps
);
130 fprintf(stderr
," Metadata length: %d\n", s
->metadatalength
);
131 fprintf(stderr
," Total Samples: %lu\n",s
->totalsamples
);
132 fprintf(stderr
," Duration: %d ms\n",s
->length
);
133 fprintf(stderr
," Bitrate: %d kbps\n",s
->bitrate
);
136 static bool flac_init(int fd
, FLACContext
* fc
)
138 unsigned char buf
[255];
140 bool found_streaminfo
=false;
144 uint32_t seekpoint_lo
,seekpoint_hi
;
145 uint32_t offset_lo
,offset_hi
;
148 if (lseek(fd
, 0, SEEK_SET
) < 0)
153 if (read(fd
, buf
, 4) < 4)
158 if (memcmp(buf
,"fLaC",4) != 0)
162 fc
->metadatalength
= 4;
164 while (!endofmetadata
) {
165 if (read(fd
, buf
, 4) < 4)
170 endofmetadata
=(buf
[0]&0x80);
171 blocklength
= (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
172 fc
->metadatalength
+=blocklength
+4;
174 if ((buf
[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
176 /* FIXME: Don't trust the value of blocklength */
177 if (read(fd
, buf
, blocklength
) < 0)
183 fc
->filesize
= statbuf
.st_size
;
184 fc
->min_blocksize
= (buf
[0] << 8) | buf
[1];
185 fc
->max_blocksize
= (buf
[2] << 8) | buf
[3];
186 fc
->min_framesize
= (buf
[4] << 16) | (buf
[5] << 8) | buf
[6];
187 fc
->max_framesize
= (buf
[7] << 16) | (buf
[8] << 8) | buf
[9];
188 fc
->samplerate
= (buf
[10] << 12) | (buf
[11] << 4)
189 | ((buf
[12] & 0xf0) >> 4);
190 fc
->channels
= ((buf
[12]&0x0e)>>1) + 1;
191 fc
->bps
= (((buf
[12]&0x01) << 4) | ((buf
[13]&0xf0)>>4) ) + 1;
193 /* totalsamples is a 36-bit field, but we assume <= 32 bits are
195 fc
->totalsamples
= (buf
[14] << 24) | (buf
[15] << 16)
196 | (buf
[16] << 8) | buf
[17];
198 /* Calculate track length (in ms) and estimate the bitrate
200 fc
->length
= (fc
->totalsamples
/ fc
->samplerate
) * 1000;
202 found_streaminfo
=true;
203 } else if ((buf
[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */
204 fprintf(stderr
,"Seektable length = %d bytes\n",blocklength
);
205 while (blocklength
>= 18) {
207 if (n
< 18) return false;
211 seekpoint_hi
=betoh32(*(p
++));
212 seekpoint_lo
=betoh32(*(p
++));
213 offset_hi
=betoh32(*(p
++));
214 offset_lo
=betoh32(*(p
++));
216 if ((seekpoint_hi
!= 0xffffffff) && (seekpoint_lo
!= 0xffffffff)) {
217 fprintf(stderr
,"Seekpoint: %u, Offset=%u\n",seekpoint_lo
,offset_lo
);
220 lseek(fd
, blocklength
, SEEK_CUR
);
222 /* Skip to next metadata block */
223 if (lseek(fd
, blocklength
, SEEK_CUR
) < 0)
230 if (found_streaminfo
) {
231 fc
->bitrate
= ((fc
->filesize
-fc
->metadatalength
) * 8) / fc
->length
;
238 /* Dummy function needed to pass to flac_decode_frame() */
242 int main(int argc
, char* argv
[]) {
249 unsigned char buf
[MAX_FRAMESIZE
]; /* The input buffer */
250 /* The output buffers containing the decoded samples (channels 0 and 1) */
251 int32_t decoded0
[MAX_BLOCKSIZE
];
252 int32_t decoded1
[MAX_BLOCKSIZE
];
255 int8_t wavbuf
[MAX_CHANNELS
*MAX_BLOCKSIZE
*3];
259 fd
=open(argv
[1],O_RDONLY
);
262 fprintf(stderr
,"Can not parse %s\n",argv
[1]);
266 /* Read the metadata and position the file pointer at the start of the
272 fdout
=open_wav("test.wav");
273 bytesleft
=read(fd
,buf
,sizeof(buf
));
275 if(flac_decode_frame(&fc
,decoded0
,decoded1
,buf
,bytesleft
,yield
) < 0) {
276 fprintf(stderr
,"DECODE ERROR, ABORTING\n");
279 consumed
=fc
.gb
.index
/8;
281 scale
=FLAC_OUTPUT_DEPTH
-fc
.bps
;
283 for (i
=0;i
<fc
.blocksize
;i
++) {
285 decoded0
[i
]=decoded0
[i
]>>scale
;
286 *(p
++)=decoded0
[i
]&0xff;
287 *(p
++)=(decoded0
[i
]&0xff00)>>8;
288 if (fc
.bps
==24) *(p
++)=(decoded0
[i
]&0xff0000)>>16;
290 if (fc
.channels
==2) {
292 decoded1
[i
]=decoded1
[i
]>>scale
;
293 *(p
++)=decoded1
[i
]&0xff;
294 *(p
++)=(decoded1
[i
]&0xff00)>>8;
295 if (fc
.bps
==24) *(p
++)=(decoded1
[i
]&0xff0000)>>16;
298 write(fdout
,wavbuf
,fc
.blocksize
*fc
.channels
*(fc
.bps
/8));
300 memmove(buf
,&buf
[consumed
],bytesleft
-consumed
);
303 n
=read(fd
,&buf
[bytesleft
],sizeof(buf
)-bytesleft
);
308 close_wav(fdout
,&fc
);