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
41 #include <sys/types.h>
46 static unsigned char wav_header
[44]={
47 'R','I','F','F',// 0 - ChunkID
48 0,0,0,0, // 4 - ChunkSize (filesize-8)
49 'W','A','V','E',// 8 - Format
50 'f','m','t',' ',// 12 - SubChunkID
51 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
52 1,0, // 20 - AudioFormat (1=Uncompressed)
53 2,0, // 22 - NumChannels
54 0,0,0,0, // 24 - SampleRate in Hz
55 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
56 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
57 16,0, // 34 - BitsPerSample
58 'd','a','t','a',// 36 - Subchunk2ID
59 0,0,0,0 // 40 - Subchunk2Size
62 int open_wav(char* filename
) {
65 fd
=open(filename
,O_CREAT
|O_WRONLY
|O_TRUNC
,S_IRUSR
|S_IWUSR
);
67 if (write(fd
,wav_header
,sizeof(wav_header
)) < sizeof(wav_header
)) {
68 fprintf(stderr
,"[ERR} Failed to write wav header\n");
75 void close_wav(int fd
, FLACContext
* fc
) {
80 bytespersample
=fc
->bps
/8;
82 filesize
=fc
->totalsamples
*bytespersample
*fc
->channels
+44;
86 wav_header
[4]=(x
&0xff);
87 wav_header
[5]=(x
&0xff00)>>8;
88 wav_header
[6]=(x
&0xff0000)>>16;
89 wav_header
[7]=(x
&0xff000000)>>24;
92 wav_header
[22]=fc
->channels
;
95 wav_header
[24]=fc
->samplerate
&0xff;
96 wav_header
[25]=(fc
->samplerate
&0xff00)>>8;
97 wav_header
[26]=(fc
->samplerate
&0xff0000)>>16;
98 wav_header
[27]=(fc
->samplerate
&0xff000000)>>24;
101 x
=fc
->samplerate
*(fc
->bps
/8)*fc
->channels
;
102 wav_header
[28]=(x
&0xff);
103 wav_header
[29]=(x
&0xff00)>>8;
104 wav_header
[30]=(x
&0xff0000)>>16;
105 wav_header
[31]=(x
&0xff000000)>>24;
108 wav_header
[32]=(fc
->bps
/8)*fc
->channels
;
111 wav_header
[34]=fc
->bps
;
115 wav_header
[40]=(x
&0xff);
116 wav_header
[41]=(x
&0xff00)>>8;
117 wav_header
[42]=(x
&0xff0000)>>16;
118 wav_header
[43]=(x
&0xff000000)>>24;
120 lseek(fd
,0,SEEK_SET
);
121 if (write(fd
,wav_header
,sizeof(wav_header
)) < sizeof(wav_header
)) {
122 fprintf(stderr
,"[ERR} Failed to write wav header\n");
128 static void dump_headers(FLACContext
*s
)
130 fprintf(stderr
," Blocksize: %d .. %d\n", s
->min_blocksize
,
132 fprintf(stderr
," Framesize: %d .. %d\n", s
->min_framesize
,
134 fprintf(stderr
," Samplerate: %d\n", s
->samplerate
);
135 fprintf(stderr
," Channels: %d\n", s
->channels
);
136 fprintf(stderr
," Bits per sample: %d\n", s
->bps
);
137 fprintf(stderr
," Metadata length: %d\n", s
->metadatalength
);
138 fprintf(stderr
," Total Samples: %lu\n",s
->totalsamples
);
139 fprintf(stderr
," Duration: %d ms\n",s
->length
);
140 fprintf(stderr
," Bitrate: %d kbps\n",s
->bitrate
);
143 static bool flac_init(int fd
, FLACContext
* fc
)
145 unsigned char buf
[255];
147 bool found_streaminfo
=false;
151 uint32_t seekpoint_lo
,seekpoint_hi
;
152 uint32_t offset_lo
,offset_hi
;
155 if (lseek(fd
, 0, SEEK_SET
) < 0)
160 if (read(fd
, buf
, 4) < 4)
165 if (memcmp(buf
,"fLaC",4) != 0)
169 fc
->metadatalength
= 4;
171 while (!endofmetadata
) {
172 if (read(fd
, buf
, 4) < 4)
177 endofmetadata
=(buf
[0]&0x80);
178 blocklength
= (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
179 fc
->metadatalength
+=blocklength
+4;
181 if ((buf
[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
183 /* FIXME: Don't trust the value of blocklength */
184 if (read(fd
, buf
, blocklength
) < 0)
190 fc
->filesize
= statbuf
.st_size
;
191 fc
->min_blocksize
= (buf
[0] << 8) | buf
[1];
192 fc
->max_blocksize
= (buf
[2] << 8) | buf
[3];
193 fc
->min_framesize
= (buf
[4] << 16) | (buf
[5] << 8) | buf
[6];
194 fc
->max_framesize
= (buf
[7] << 16) | (buf
[8] << 8) | buf
[9];
195 fc
->samplerate
= (buf
[10] << 12) | (buf
[11] << 4)
196 | ((buf
[12] & 0xf0) >> 4);
197 fc
->channels
= ((buf
[12]&0x0e)>>1) + 1;
198 fc
->bps
= (((buf
[12]&0x01) << 4) | ((buf
[13]&0xf0)>>4) ) + 1;
200 /* totalsamples is a 36-bit field, but we assume <= 32 bits are
202 fc
->totalsamples
= (buf
[14] << 24) | (buf
[15] << 16)
203 | (buf
[16] << 8) | buf
[17];
205 /* Calculate track length (in ms) and estimate the bitrate
207 fc
->length
= (fc
->totalsamples
/ fc
->samplerate
) * 1000;
209 found_streaminfo
=true;
210 } else if ((buf
[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */
211 fprintf(stderr
,"Seektable length = %d bytes\n",blocklength
);
212 while (blocklength
>= 18) {
214 if (n
< 18) return false;
218 seekpoint_hi
=betoh32(*(p
++));
219 seekpoint_lo
=betoh32(*(p
++));
220 offset_hi
=betoh32(*(p
++));
221 offset_lo
=betoh32(*(p
++));
223 if ((seekpoint_hi
!= 0xffffffff) && (seekpoint_lo
!= 0xffffffff)) {
224 fprintf(stderr
,"Seekpoint: %u, Offset=%u\n",seekpoint_lo
,offset_lo
);
227 lseek(fd
, blocklength
, SEEK_CUR
);
229 /* Skip to next metadata block */
230 if (lseek(fd
, blocklength
, SEEK_CUR
) < 0)
237 if (found_streaminfo
) {
238 fc
->bitrate
= ((fc
->filesize
-fc
->metadatalength
) * 8) / fc
->length
;
245 /* Dummy function needed to pass to flac_decode_frame() */
249 int main(int argc
, char* argv
[]) {
256 unsigned char buf
[MAX_FRAMESIZE
]; /* The input buffer */
257 /* The output buffers containing the decoded samples (channels 0 and 1) */
258 int32_t decoded0
[MAX_BLOCKSIZE
];
259 int32_t decoded1
[MAX_BLOCKSIZE
];
262 int8_t wavbuf
[MAX_CHANNELS
*MAX_BLOCKSIZE
*3];
266 fd
=open(argv
[1],O_RDONLY
);
269 fprintf(stderr
,"Can not parse %s\n",argv
[1]);
273 /* Read the metadata and position the file pointer at the start of the
279 fdout
=open_wav("test.wav");
280 bytesleft
=read(fd
,buf
,sizeof(buf
));
282 if(flac_decode_frame(&fc
,decoded0
,decoded1
,buf
,bytesleft
,yield
) < 0) {
283 fprintf(stderr
,"DECODE ERROR, ABORTING\n");
286 consumed
=fc
.gb
.index
/8;
288 scale
=FLAC_OUTPUT_DEPTH
-fc
.bps
;
290 for (i
=0;i
<fc
.blocksize
;i
++) {
292 decoded0
[i
]=decoded0
[i
]>>scale
;
293 *(p
++)=decoded0
[i
]&0xff;
294 *(p
++)=(decoded0
[i
]&0xff00)>>8;
295 if (fc
.bps
==24) *(p
++)=(decoded0
[i
]&0xff0000)>>16;
297 if (fc
.channels
==2) {
299 decoded1
[i
]=decoded1
[i
]>>scale
;
300 *(p
++)=decoded1
[i
]&0xff;
301 *(p
++)=(decoded1
[i
]&0xff00)>>8;
302 if (fc
.bps
==24) *(p
++)=(decoded1
[i
]&0xff0000)>>16;
305 n
= fc
.blocksize
*fc
.channels
*(fc
.bps
/8);
306 if (write(fdout
,wavbuf
,n
) < n
) {
307 fprintf(stderr
,"[ERR] Write failed\n");
311 memmove(buf
,&buf
[consumed
],bytesleft
-consumed
);
314 n
=read(fd
,&buf
[bytesleft
],sizeof(buf
)-bytesleft
);
319 close_wav(fdout
,&fc
);