diff options
Diffstat (limited to 'apps/codecs/libffmpegFLAC/main.c')
-rw-r--r-- | apps/codecs/libffmpegFLAC/main.c | 322 |
1 files changed, 0 insertions, 322 deletions
diff --git a/apps/codecs/libffmpegFLAC/main.c b/apps/codecs/libffmpegFLAC/main.c deleted file mode 100644 index e6d4b2f4fd..0000000000 --- a/apps/codecs/libffmpegFLAC/main.c +++ /dev/null | |||
@@ -1,322 +0,0 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 Dave Chapman | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | /* A test program for the Rockbox version of the ffmpeg FLAC decoder. | ||
23 | |||
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 | ||
26 | |||
27 | This test program should support 16-bit and 24-bit mono and stereo files. | ||
28 | |||
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 | ||
31 | header. | ||
32 | */ | ||
33 | |||
34 | #include <stdio.h> | ||
35 | #include <string.h> | ||
36 | #include <stdlib.h> | ||
37 | #include <inttypes.h> | ||
38 | #include <stdbool.h> | ||
39 | #include <fcntl.h> | ||
40 | #include <unistd.h> | ||
41 | #include <sys/types.h> | ||
42 | #include <sys/stat.h> | ||
43 | |||
44 | #include "decoder.h" | ||
45 | |||
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 | ||
60 | }; | ||
61 | |||
62 | int open_wav(char* filename) { | ||
63 | int fd; | ||
64 | |||
65 | fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR); | ||
66 | if (fd >= 0) { | ||
67 | if (write(fd,wav_header,sizeof(wav_header)) < sizeof(wav_header)) { | ||
68 | fprintf(stderr,"[ERR} Failed to write wav header\n"); | ||
69 | exit(1); | ||
70 | } | ||
71 | } | ||
72 | return(fd); | ||
73 | } | ||
74 | |||
75 | void close_wav(int fd, FLACContext* fc) { | ||
76 | int x; | ||
77 | int filesize; | ||
78 | int bytespersample; | ||
79 | |||
80 | bytespersample=fc->bps/8; | ||
81 | |||
82 | filesize=fc->totalsamples*bytespersample*fc->channels+44; | ||
83 | |||
84 | // ChunkSize | ||
85 | x=filesize-8; | ||
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; | ||
90 | |||
91 | // Number of channels | ||
92 | wav_header[22]=fc->channels; | ||
93 | |||
94 | // Samplerate | ||
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; | ||
99 | |||
100 | // ByteRate | ||
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; | ||
106 | |||
107 | // BlockAlign | ||
108 | wav_header[32]=(fc->bps/8)*fc->channels; | ||
109 | |||
110 | // Bits per sample | ||
111 | wav_header[34]=fc->bps; | ||
112 | |||
113 | // Subchunk2Size | ||
114 | x=filesize-44; | ||
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; | ||
119 | |||
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"); | ||
123 | exit(1); | ||
124 | } | ||
125 | close(fd); | ||
126 | } | ||
127 | |||
128 | static void dump_headers(FLACContext *s) | ||
129 | { | ||
130 | fprintf(stderr," Blocksize: %d .. %d\n", s->min_blocksize, | ||
131 | s->max_blocksize); | ||
132 | fprintf(stderr," Framesize: %d .. %d\n", s->min_framesize, | ||
133 | s->max_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); | ||
141 | } | ||
142 | |||
143 | static bool flac_init(int fd, FLACContext* fc) | ||
144 | { | ||
145 | unsigned char buf[255]; | ||
146 | struct stat statbuf; | ||
147 | bool found_streaminfo=false; | ||
148 | int endofmetadata=0; | ||
149 | int blocklength; | ||
150 | uint32_t* p; | ||
151 | uint32_t seekpoint_lo,seekpoint_hi; | ||
152 | uint32_t offset_lo,offset_hi; | ||
153 | int n; | ||
154 | |||
155 | if (lseek(fd, 0, SEEK_SET) < 0) | ||
156 | { | ||
157 | return false; | ||
158 | } | ||
159 | |||
160 | if (read(fd, buf, 4) < 4) | ||
161 | { | ||
162 | return false; | ||
163 | } | ||
164 | |||
165 | if (memcmp(buf,"fLaC",4) != 0) | ||
166 | { | ||
167 | return false; | ||
168 | } | ||
169 | fc->metadatalength = 4; | ||
170 | |||
171 | while (!endofmetadata) { | ||
172 | if (read(fd, buf, 4) < 4) | ||
173 | { | ||
174 | return false; | ||
175 | } | ||
176 | |||
177 | endofmetadata=(buf[0]&0x80); | ||
178 | blocklength = (buf[1] << 16) | (buf[2] << 8) | buf[3]; | ||
179 | fc->metadatalength+=blocklength+4; | ||
180 | |||
181 | if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */ | ||
182 | { | ||
183 | /* FIXME: Don't trust the value of blocklength */ | ||
184 | if (read(fd, buf, blocklength) < 0) | ||
185 | { | ||
186 | return false; | ||
187 | } | ||
188 | |||
189 | fstat(fd,&statbuf); | ||
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; | ||
199 | |||
200 | /* totalsamples is a 36-bit field, but we assume <= 32 bits are | ||
201 | used */ | ||
202 | fc->totalsamples = (buf[14] << 24) | (buf[15] << 16) | ||
203 | | (buf[16] << 8) | buf[17]; | ||
204 | |||
205 | /* Calculate track length (in ms) and estimate the bitrate | ||
206 | (in kbit/s) */ | ||
207 | fc->length = (fc->totalsamples / fc->samplerate) * 1000; | ||
208 | |||
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) { | ||
213 | n=read(fd,buf,18); | ||
214 | if (n < 18) return false; | ||
215 | blocklength-=n; | ||
216 | |||
217 | p=(uint32_t*)buf; | ||
218 | seekpoint_hi=betoh32(*(p++)); | ||
219 | seekpoint_lo=betoh32(*(p++)); | ||
220 | offset_hi=betoh32(*(p++)); | ||
221 | offset_lo=betoh32(*(p++)); | ||
222 | |||
223 | if ((seekpoint_hi != 0xffffffff) && (seekpoint_lo != 0xffffffff)) { | ||
224 | fprintf(stderr,"Seekpoint: %u, Offset=%u\n",seekpoint_lo,offset_lo); | ||
225 | } | ||
226 | } | ||
227 | lseek(fd, blocklength, SEEK_CUR); | ||
228 | } else { | ||
229 | /* Skip to next metadata block */ | ||
230 | if (lseek(fd, blocklength, SEEK_CUR) < 0) | ||
231 | { | ||
232 | return false; | ||
233 | } | ||
234 | } | ||
235 | } | ||
236 | |||
237 | if (found_streaminfo) { | ||
238 | fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length; | ||
239 | return true; | ||
240 | } else { | ||
241 | return false; | ||
242 | } | ||
243 | } | ||
244 | |||
245 | /* Dummy function needed to pass to flac_decode_frame() */ | ||
246 | void yield() { | ||
247 | } | ||
248 | |||
249 | int main(int argc, char* argv[]) { | ||
250 | FLACContext fc; | ||
251 | int fd,fdout; | ||
252 | int n; | ||
253 | int i; | ||
254 | int bytesleft; | ||
255 | int consumed; | ||
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]; | ||
260 | |||
261 | /* For testing */ | ||
262 | int8_t wavbuf[MAX_CHANNELS*MAX_BLOCKSIZE*3]; | ||
263 | int8_t* p; | ||
264 | int scale; | ||
265 | |||
266 | fd=open(argv[1],O_RDONLY); | ||
267 | |||
268 | if (fd < 0) { | ||
269 | fprintf(stderr,"Can not parse %s\n",argv[1]); | ||
270 | return(1); | ||
271 | } | ||
272 | |||
273 | /* Read the metadata and position the file pointer at the start of the | ||
274 | first audio frame */ | ||
275 | flac_init(fd,&fc); | ||
276 | |||
277 | dump_headers(&fc); | ||
278 | |||
279 | fdout=open_wav("test.wav"); | ||
280 | bytesleft=read(fd,buf,sizeof(buf)); | ||
281 | while (bytesleft) { | ||
282 | if(flac_decode_frame(&fc,decoded0,decoded1,buf,bytesleft,yield) < 0) { | ||
283 | fprintf(stderr,"DECODE ERROR, ABORTING\n"); | ||
284 | break; | ||
285 | } | ||
286 | consumed=fc.gb.index/8; | ||
287 | |||
288 | scale=FLAC_OUTPUT_DEPTH-fc.bps; | ||
289 | p=wavbuf; | ||
290 | for (i=0;i<fc.blocksize;i++) { | ||
291 | /* Left sample */ | ||
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; | ||
296 | |||
297 | if (fc.channels==2) { | ||
298 | /* Right sample */ | ||
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; | ||
303 | } | ||
304 | } | ||
305 | n = fc.blocksize*fc.channels*(fc.bps/8); | ||
306 | if (write(fdout,wavbuf,n) < n) { | ||
307 | fprintf(stderr,"[ERR] Write failed\n"); | ||
308 | exit(1); | ||
309 | } | ||
310 | |||
311 | memmove(buf,&buf[consumed],bytesleft-consumed); | ||
312 | bytesleft-=consumed; | ||
313 | |||
314 | n=read(fd,&buf[bytesleft],sizeof(buf)-bytesleft); | ||
315 | if (n > 0) { | ||
316 | bytesleft+=n; | ||
317 | } | ||
318 | } | ||
319 | close_wav(fdout,&fc); | ||
320 | close(fd); | ||
321 | return(0); | ||
322 | } | ||