diff options
Diffstat (limited to 'apps/codecs/libffmpegFLAC/main.c')
-rw-r--r-- | apps/codecs/libffmpegFLAC/main.c | 292 |
1 files changed, 292 insertions, 0 deletions
diff --git a/apps/codecs/libffmpegFLAC/main.c b/apps/codecs/libffmpegFLAC/main.c new file mode 100644 index 0000000000..eea97f4f18 --- /dev/null +++ b/apps/codecs/libffmpegFLAC/main.c | |||
@@ -0,0 +1,292 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 Dave Chapman | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | /* A test program for the Rockbox version of the ffmpeg FLAC decoder. | ||
21 | |||
22 | Compile using Makefile.test - run it as "./test file.flac" to decode the | ||
23 | FLAC file to the file "test.wav" in the current directory | ||
24 | |||
25 | This test program should support 16-bit and 24-bit mono and stereo files. | ||
26 | |||
27 | The resulting "test.wav" should have the same md5sum as a WAV file created | ||
28 | by the official FLAC decoder (it produces the same 44-byte canonical WAV | ||
29 | header. | ||
30 | */ | ||
31 | |||
32 | #include <stdio.h> | ||
33 | #include <string.h> | ||
34 | #include <inttypes.h> | ||
35 | #include <stdbool.h> | ||
36 | #include <fcntl.h> | ||
37 | #include <unistd.h> | ||
38 | #include <sys/types.h> | ||
39 | #include <sys/stat.h> | ||
40 | |||
41 | #include "decoder.h" | ||
42 | |||
43 | static unsigned char wav_header[44]={ | ||
44 | 'R','I','F','F',// 0 - ChunkID | ||
45 | 0,0,0,0, // 4 - ChunkSize (filesize-8) | ||
46 | 'W','A','V','E',// 8 - Format | ||
47 | 'f','m','t',' ',// 12 - SubChunkID | ||
48 | 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM | ||
49 | 1,0, // 20 - AudioFormat (1=Uncompressed) | ||
50 | 2,0, // 22 - NumChannels | ||
51 | 0,0,0,0, // 24 - SampleRate in Hz | ||
52 | 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8) | ||
53 | 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8) | ||
54 | 16,0, // 34 - BitsPerSample | ||
55 | 'd','a','t','a',// 36 - Subchunk2ID | ||
56 | 0,0,0,0 // 40 - Subchunk2Size | ||
57 | }; | ||
58 | |||
59 | int open_wav(char* filename) { | ||
60 | int fd; | ||
61 | |||
62 | fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR); | ||
63 | if (fd >= 0) { | ||
64 | write(fd,wav_header,sizeof(wav_header)); | ||
65 | } | ||
66 | return(fd); | ||
67 | } | ||
68 | |||
69 | void close_wav(int fd, FLACContext* fc) { | ||
70 | int x; | ||
71 | int filesize; | ||
72 | int bytespersample; | ||
73 | |||
74 | bytespersample=fc->bps/8; | ||
75 | |||
76 | filesize=fc->totalsamples*bytespersample*fc->channels+44; | ||
77 | |||
78 | // ChunkSize | ||
79 | x=filesize-8; | ||
80 | wav_header[4]=(x&0xff); | ||
81 | wav_header[5]=(x&0xff00)>>8; | ||
82 | wav_header[6]=(x&0xff0000)>>16; | ||
83 | wav_header[7]=(x&0xff000000)>>24; | ||
84 | |||
85 | // Number of channels | ||
86 | wav_header[22]=fc->channels; | ||
87 | |||
88 | // Samplerate | ||
89 | wav_header[24]=fc->samplerate&0xff; | ||
90 | wav_header[25]=(fc->samplerate&0xff00)>>8; | ||
91 | wav_header[26]=(fc->samplerate&0xff0000)>>16; | ||
92 | wav_header[27]=(fc->samplerate&0xff000000)>>24; | ||
93 | |||
94 | // ByteRate | ||
95 | x=fc->samplerate*(fc->bps/8)*fc->channels; | ||
96 | wav_header[28]=(x&0xff); | ||
97 | wav_header[29]=(x&0xff00)>>8; | ||
98 | wav_header[30]=(x&0xff0000)>>16; | ||
99 | wav_header[31]=(x&0xff000000)>>24; | ||
100 | |||
101 | // BlockAlign | ||
102 | wav_header[32]=(fc->bps/8)*fc->channels; | ||
103 | |||
104 | // Bits per sample | ||
105 | wav_header[34]=fc->bps; | ||
106 | |||
107 | // Subchunk2Size | ||
108 | x=filesize-44; | ||
109 | wav_header[40]=(x&0xff); | ||
110 | wav_header[41]=(x&0xff00)>>8; | ||
111 | wav_header[42]=(x&0xff0000)>>16; | ||
112 | wav_header[43]=(x&0xff000000)>>24; | ||
113 | |||
114 | lseek(fd,0,SEEK_SET); | ||
115 | write(fd,wav_header,sizeof(wav_header)); | ||
116 | close(fd); | ||
117 | } | ||
118 | |||
119 | static void dump_headers(FLACContext *s) | ||
120 | { | ||
121 | fprintf(stderr," Blocksize: %d .. %d\n", s->min_blocksize, | ||
122 | s->max_blocksize); | ||
123 | fprintf(stderr," Framesize: %d .. %d\n", s->min_framesize, | ||
124 | s->max_framesize); | ||
125 | fprintf(stderr," Samplerate: %d\n", s->samplerate); | ||
126 | fprintf(stderr," Channels: %d\n", s->channels); | ||
127 | fprintf(stderr," Bits per sample: %d\n", s->bps); | ||
128 | fprintf(stderr," Metadata length: %d\n", s->metadatalength); | ||
129 | fprintf(stderr," Total Samples: %lu\n",s->totalsamples); | ||
130 | fprintf(stderr," Duration: %d ms\n",s->length); | ||
131 | fprintf(stderr," Bitrate: %d kbps\n",s->bitrate); | ||
132 | } | ||
133 | |||
134 | static bool flac_init(int fd, FLACContext* fc) | ||
135 | { | ||
136 | unsigned char buf[255]; | ||
137 | struct stat statbuf; | ||
138 | bool found_streaminfo=false; | ||
139 | int endofmetadata=0; | ||
140 | int blocklength; | ||
141 | |||
142 | if (lseek(fd, 0, SEEK_SET) < 0) | ||
143 | { | ||
144 | return false; | ||
145 | } | ||
146 | |||
147 | if (read(fd, buf, 4) < 4) | ||
148 | { | ||
149 | return false; | ||
150 | } | ||
151 | |||
152 | if (memcmp(buf,"fLaC",4) != 0) | ||
153 | { | ||
154 | return false; | ||
155 | } | ||
156 | fc->metadatalength = 4; | ||
157 | |||
158 | while (!endofmetadata) { | ||
159 | if (read(fd, buf, 4) < 4) | ||
160 | { | ||
161 | return false; | ||
162 | } | ||
163 | |||
164 | endofmetadata=(buf[0]&0x80); | ||
165 | blocklength = (buf[1] << 16) | (buf[2] << 8) | buf[3]; | ||
166 | fc->metadatalength+=blocklength+4; | ||
167 | |||
168 | if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */ | ||
169 | { | ||
170 | /* FIXME: Don't trust the value of blocklength */ | ||
171 | if (read(fd, buf, blocklength) < 0) | ||
172 | { | ||
173 | return false; | ||
174 | } | ||
175 | |||
176 | fstat(fd,&statbuf); | ||
177 | fc->filesize = statbuf.st_size; | ||
178 | fc->min_blocksize = (buf[0] << 8) | buf[1]; | ||
179 | fc->max_blocksize = (buf[2] << 8) | buf[3]; | ||
180 | fc->min_framesize = (buf[4] << 16) | (buf[5] << 8) | buf[6]; | ||
181 | fc->max_framesize = (buf[7] << 16) | (buf[8] << 8) | buf[9]; | ||
182 | fc->samplerate = (buf[10] << 12) | (buf[11] << 4) | ||
183 | | ((buf[12] & 0xf0) >> 4); | ||
184 | fc->channels = ((buf[12]&0x0e)>>1) + 1; | ||
185 | fc->bps = (((buf[12]&0x01) << 4) | ((buf[13]&0xf0)>>4) ) + 1; | ||
186 | |||
187 | /* totalsamples is a 36-bit field, but we assume <= 32 bits are | ||
188 | used */ | ||
189 | fc->totalsamples = (buf[14] << 24) | (buf[15] << 16) | ||
190 | | (buf[16] << 8) | buf[17]; | ||
191 | |||
192 | /* Calculate track length (in ms) and estimate the bitrate | ||
193 | (in kbit/s) */ | ||
194 | fc->length = (fc->totalsamples / fc->samplerate) * 1000; | ||
195 | |||
196 | found_streaminfo=true; | ||
197 | } else if ((buf[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */ | ||
198 | fprintf(stderr,"Seektable length = %d bytes\n",blocklength); | ||
199 | if (lseek(fd, blocklength, SEEK_CUR) < 0) { | ||
200 | return false; | ||
201 | } | ||
202 | } else { | ||
203 | /* Skip to next metadata block */ | ||
204 | if (lseek(fd, blocklength, SEEK_CUR) < 0) | ||
205 | { | ||
206 | return false; | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | |||
211 | if (found_streaminfo) { | ||
212 | fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length; | ||
213 | return true; | ||
214 | } else { | ||
215 | return false; | ||
216 | } | ||
217 | } | ||
218 | |||
219 | /* Dummy function needed to pass to flac_decode_frame() */ | ||
220 | void yield() { | ||
221 | } | ||
222 | |||
223 | int main(int argc, char* argv[]) { | ||
224 | FLACContext fc; | ||
225 | int fd,fdout; | ||
226 | int n; | ||
227 | int i; | ||
228 | int bytesleft; | ||
229 | int consumed; | ||
230 | char buf[MAX_FRAMESIZE]; /* The input buffer */ | ||
231 | /* The output buffers containing the decoded samples (channels 0 and 1) */ | ||
232 | int32_t decoded0[MAX_BLOCKSIZE]; | ||
233 | int32_t decoded1[MAX_BLOCKSIZE]; | ||
234 | |||
235 | /* For testing */ | ||
236 | int8_t wavbuf[MAX_CHANNELS*MAX_BLOCKSIZE*3]; | ||
237 | int8_t* p; | ||
238 | int scale; | ||
239 | |||
240 | fd=open(argv[1],O_RDONLY); | ||
241 | |||
242 | if (fd < 0) { | ||
243 | fprintf(stderr,"Can not parse %s\n",argv[1]); | ||
244 | return(1); | ||
245 | } | ||
246 | |||
247 | /* Read the metadata and position the file pointer at the start of the | ||
248 | first audio frame */ | ||
249 | flac_init(fd,&fc); | ||
250 | |||
251 | dump_headers(&fc); | ||
252 | |||
253 | fdout=open_wav("test.wav"); | ||
254 | bytesleft=read(fd,buf,sizeof(buf)); | ||
255 | while (bytesleft) { | ||
256 | if(flac_decode_frame(&fc,decoded0,decoded1,buf,bytesleft,yield) < 0) { | ||
257 | fprintf(stderr,"DECODE ERROR, ABORTING\n"); | ||
258 | break; | ||
259 | } | ||
260 | consumed=fc.gb.index/8; | ||
261 | |||
262 | scale=FLAC_OUTPUT_DEPTH-fc.bps; | ||
263 | p=wavbuf; | ||
264 | for (i=0;i<fc.blocksize;i++) { | ||
265 | /* Left sample */ | ||
266 | decoded0[i]=decoded0[i]>>scale; | ||
267 | *(p++)=decoded0[i]&0xff; | ||
268 | *(p++)=(decoded0[i]&0xff00)>>8; | ||
269 | if (fc.bps==24) *(p++)=(decoded0[i]&0xff0000)>>16; | ||
270 | |||
271 | if (fc.channels==2) { | ||
272 | /* Right sample */ | ||
273 | decoded1[i]=decoded1[i]>>scale; | ||
274 | *(p++)=decoded1[i]&0xff; | ||
275 | *(p++)=(decoded1[i]&0xff00)>>8; | ||
276 | if (fc.bps==24) *(p++)=(decoded1[i]&0xff0000)>>16; | ||
277 | } | ||
278 | } | ||
279 | write(fdout,wavbuf,fc.blocksize*fc.channels*(fc.bps/8)); | ||
280 | |||
281 | memmove(buf,&buf[consumed],bytesleft-consumed); | ||
282 | bytesleft-=consumed; | ||
283 | |||
284 | n=read(fd,&buf[bytesleft],sizeof(buf)-bytesleft); | ||
285 | if (n > 0) { | ||
286 | bytesleft+=n; | ||
287 | } | ||
288 | } | ||
289 | close_wav(fdout,&fc); | ||
290 | close(fd); | ||
291 | return(0); | ||
292 | } | ||