New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / codecs / libffmpegFLAC / main.c
blob4a989a4dd07effde71eba6d8aeb99bdbe14909f6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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
31 header.
34 #include <stdio.h>
35 #include <string.h>
36 #include <inttypes.h>
37 #include <stdbool.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
43 #include "decoder.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) {
62 int fd;
64 fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR);
65 if (fd >= 0) {
66 write(fd,wav_header,sizeof(wav_header));
68 return(fd);
71 void close_wav(int fd, FLACContext* fc) {
72 int x;
73 int filesize;
74 int bytespersample;
76 bytespersample=fc->bps/8;
78 filesize=fc->totalsamples*bytespersample*fc->channels+44;
80 // ChunkSize
81 x=filesize-8;
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;
87 // Number of channels
88 wav_header[22]=fc->channels;
90 // Samplerate
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;
96 // ByteRate
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;
103 // BlockAlign
104 wav_header[32]=(fc->bps/8)*fc->channels;
106 // Bits per sample
107 wav_header[34]=fc->bps;
109 // Subchunk2Size
110 x=filesize-44;
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));
118 close(fd);
121 static void dump_headers(FLACContext *s)
123 fprintf(stderr," Blocksize: %d .. %d\n", s->min_blocksize,
124 s->max_blocksize);
125 fprintf(stderr," Framesize: %d .. %d\n", s->min_framesize,
126 s->max_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];
139 struct stat statbuf;
140 bool found_streaminfo=false;
141 int endofmetadata=0;
142 int blocklength;
143 uint32_t* p;
144 uint32_t seekpoint_lo,seekpoint_hi;
145 uint32_t offset_lo,offset_hi;
146 int n;
148 if (lseek(fd, 0, SEEK_SET) < 0)
150 return false;
153 if (read(fd, buf, 4) < 4)
155 return false;
158 if (memcmp(buf,"fLaC",4) != 0)
160 return false;
162 fc->metadatalength = 4;
164 while (!endofmetadata) {
165 if (read(fd, buf, 4) < 4)
167 return false;
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)
179 return false;
182 fstat(fd,&statbuf);
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
194 used */
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
199 (in kbit/s) */
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) {
206 n=read(fd,buf,18);
207 if (n < 18) return false;
208 blocklength-=n;
210 p=(uint32_t*)buf;
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);
221 } else {
222 /* Skip to next metadata block */
223 if (lseek(fd, blocklength, SEEK_CUR) < 0)
225 return false;
230 if (found_streaminfo) {
231 fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length;
232 return true;
233 } else {
234 return false;
238 /* Dummy function needed to pass to flac_decode_frame() */
239 void yield() {
242 int main(int argc, char* argv[]) {
243 FLACContext fc;
244 int fd,fdout;
245 int n;
246 int i;
247 int bytesleft;
248 int consumed;
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];
254 /* For testing */
255 int8_t wavbuf[MAX_CHANNELS*MAX_BLOCKSIZE*3];
256 int8_t* p;
257 int scale;
259 fd=open(argv[1],O_RDONLY);
261 if (fd < 0) {
262 fprintf(stderr,"Can not parse %s\n",argv[1]);
263 return(1);
266 /* Read the metadata and position the file pointer at the start of the
267 first audio frame */
268 flac_init(fd,&fc);
270 dump_headers(&fc);
272 fdout=open_wav("test.wav");
273 bytesleft=read(fd,buf,sizeof(buf));
274 while (bytesleft) {
275 if(flac_decode_frame(&fc,decoded0,decoded1,buf,bytesleft,yield) < 0) {
276 fprintf(stderr,"DECODE ERROR, ABORTING\n");
277 break;
279 consumed=fc.gb.index/8;
281 scale=FLAC_OUTPUT_DEPTH-fc.bps;
282 p=wavbuf;
283 for (i=0;i<fc.blocksize;i++) {
284 /* Left sample */
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) {
291 /* Right sample */
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);
301 bytesleft-=consumed;
303 n=read(fd,&buf[bytesleft],sizeof(buf)-bytesleft);
304 if (n > 0) {
305 bytesleft+=n;
308 close_wav(fdout,&fc);
309 close(fd);
310 return(0);