fixed bug in jpeg2000 decoding
[swftools.git] / src / wav2swf.c
1 /* wav2swf.c
2    Converts WAV/WAVE files to SWF.
3
4    Part of the swftools package.
5
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "../lib/rfxswf.h"
25 #include "../lib/log.h"
26 #include "../lib/args.h"
27 #include "../lib/wav.h"
28
29 char * filename = 0;
30 char * outputname = "output.swf";
31 int verbose = 2;
32 int stopframe0 = 0;
33 int stopframe1 = 0;
34
35 #define DEFINESOUND_MP3 1 //define sound uses mp3?- undefine for raw sound.
36
37 static struct options_t options[] = {
38 {"h", "help"},
39 {"V", "version"},
40 {"o", "output"},
41 {"r", "framerate"},
42 {"s", "samplerate"},
43 {"d", "definesound"},
44 {"l", "loop"},
45 {"C", "cgi"},
46 {"S", "stop"},
47 {"E", "end"},
48 {"b", "bitrate"},
49 {"v", "verbose"},
50 {0,0}
51 };
52
53 static int loop = 0;
54 static int definesound = 0;
55 static int framerate = 0;
56 static int samplerate = 11025;
57 static int bitrate = 32;
58 static int do_cgi = 0;
59
60 static int mp3_bitrates[] =
61 { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0};
62
63 int args_callback_option(char*name,char*val)
64 {
65     if(!strcmp(name, "V")) {
66         printf("wav2swf - part of %s %s\n", PACKAGE, VERSION);
67         exit(0);
68     }
69     else if(!strcmp(name, "o")) {
70         outputname = val;
71         return 1;
72     }
73     else if(!strcmp(name, "d")) {
74         definesound = 1;
75         return 0;
76     }
77     else if(!strcmp(name, "l")) {
78         loop = atoi(val);
79         definesound = 1;
80         return 1;
81     }
82     else if(!strcmp(name, "v")) {
83         verbose ++;
84         return 0;
85     }
86     else if(!strcmp(name, "S")) {
87         stopframe0 = 1;
88         return 0;
89     }
90     else if(!strcmp(name, "E")) {
91         stopframe1 = 1;
92         return 0;
93     }
94     else if(!strcmp(name, "C")) {
95         do_cgi = 1;
96         return 0;
97     }
98     else if(!strcmp(name, "r")) {
99         float f;
100         sscanf(val, "%f", &f);
101         framerate = f*256;
102         return 1;
103     }
104     else if(!strcmp(name, "s")) {
105         samplerate = atoi(val);
106         if(samplerate > 5000 && samplerate < 6000)
107             samplerate = 5512;
108         else if(samplerate > 11000 && samplerate < 12000)
109             samplerate = 11025;
110         else if(samplerate > 22000 && samplerate < 23000)
111             samplerate = 22050;
112         else if(samplerate > 44000 && samplerate < 45000)
113             samplerate = 44100;
114         else {
115             fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
116             fprintf(stderr, "Allowed values: 11025, 22050, 44100\n");
117             exit(1);
118         }
119         return 1;
120     }
121     else if(!strcmp(name, "b")) {
122         int t;
123         int b = atoi(val);
124         if(b<=0) {
125             fprintf(stderr, "Not a valid bitrate: %s\n", val);
126             exit(1);
127         }
128         if(b>160) {
129             fprintf(stderr, "Bitrate must be <144. (%s)\n", val);
130             exit(1);
131         }
132         for(t=0;mp3_bitrates[t];t++) {
133             if(b== mp3_bitrates[t]) {
134                 bitrate = b;
135                 return 1;
136             }
137         }
138         fprintf(stderr, "Invalid bitrate. Allowed bitrates are:\n");
139         for(t=0;mp3_bitrates[t];t++) {
140             printf("%d ", mp3_bitrates[t]);
141         }
142         printf("\n");
143         exit(1);
144     }
145     else {
146         printf("Unknown option: -%s\n", name);
147         exit(1);
148     }
149     return 0;
150 }
151 int args_callback_longoption(char*name,char*val)
152 {
153     return args_long2shortoption(options, name, val);
154 }
155 void args_callback_usage(char *name)
156 {
157     printf("\n");
158     printf("Usage: %s [-o filename] file.wav\n", name);
159     printf("\n");
160     printf("-h , --help                    Print short help message and exit\n");
161     printf("-V , --version                 Print version info and exit\n");
162     printf("-o , --output <filename>       Explicitly specify output file. (Otherwise, output will go to output.swf)\n");
163     printf("-r , --framerate <fps>         Set file framerate to <fps> frames per second.\n");
164     printf("-s , --samplerate <sps>        Set samplerate to <sps> frames per second (default: 11025).\n");
165     printf("-d , --definesound             Generate a DefineSound tag instead of streaming sound.\n");
166     printf("-l , --loop n                  (Only used with -d)\n");
167     printf("-C , --cgi                     For use as CGI- prepend http header, write to stdout.\n");
168     printf("-S , --stop                    Stop the movie at frame 0\n");
169     printf("-E , --end                     Stop the movie at the end frame\n");
170     printf("-b , --bitrate <bps>           Set mp3 bitrate to <bps> (default: 32)\n");
171     printf("-v , --verbose                 Be more verbose\n");
172     printf("\n");
173 }
174 int args_callback_command(char*name,char*val)
175 {
176     if(filename) {
177         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
178                  filename, name);
179     }
180     filename = name;
181     return 0;
182 }
183
184 extern int swf_mp3_bitrate;
185 extern int swf_mp3_out_samplerate;
186 extern int swf_mp3_in_samplerate;
187
188 int main (int argc,char ** argv)
189
190     SWF swf;
191     RGBA rgb;
192     SRECT r;
193     S32 width=300,height = 300;
194     TAG * tag;
195
196     int f,i,ls1,fs1;
197     int count;
198     int t;
199     struct WAV wav,wav2;
200     int blocksize;
201     float blockspersecond;
202     float framespersecond;
203     float samplesperframe;
204     float framesperblock;
205     float samplesperblock;
206     U16* samples;
207     int numsamples;
208
209     processargs(argc, argv);
210
211     blocksize = (samplerate > 22050) ? 1152 : 576;
212
213     blockspersecond = (float)samplerate/blocksize;
214
215     framespersecond = blockspersecond;
216     if(framerate)
217         framespersecond = framerate/256.0;
218
219     framesperblock = framespersecond / blockspersecond;
220     samplesperframe = (blocksize * blockspersecond) / framespersecond;
221     samplesperblock = samplesperframe * framesperblock;
222     
223     initLog(0,-1,0,0,-1,verbose);
224
225     if(!filename) {
226         msg("<fatal> You must supply a filename");
227         exit(1);
228     }
229
230     if(!wav_read(&wav, filename))
231     {
232         msg("<fatal> Error reading %s", filename);
233         exit(1);
234     }
235     wav_convert2mono(&wav,&wav2, samplerate);
236     //wav_print(&wav);
237     //wav_print(&wav2);
238     samples = (U16*)wav2.data;
239     numsamples = wav2.size/2;
240
241 #ifdef WORDS_BIGENDIAN
242     /* swap bytes */
243     for(t=0;t<numsamples;t++) {
244         samples[t] = (samples[t]>>8)&0xff | (samples[t]<<8)&0xff00;
245     }
246 #endif
247
248     if(numsamples%blocksize != 0)
249     {
250         // apply padding, so that block is a multiple of blocksize
251         int numblocks = (numsamples+blocksize-1)/blocksize;
252         int numsamples2;
253         U16* samples2;
254         numsamples2 = numblocks * blocksize;
255         samples2 = malloc(sizeof(U16)*numsamples2);
256         memcpy(samples2, samples, numsamples*sizeof(U16));
257         memset(&samples2[numsamples], 0, sizeof(U16)*(numsamples2 - numsamples));
258         numsamples = numsamples2;
259         samples = samples2;
260     }
261
262     memset(&swf,0x00,sizeof(SWF));
263
264     swf.fileVersion    = 5;
265     swf.frameRate      = (int)(framespersecond*256);
266
267     swf.movieSize.xmax = 20*width;
268     swf.movieSize.ymax = 20*height;
269
270     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
271     tag = swf.firstTag;
272     rgb.r = 0xff;
273     rgb.g = 0xff;
274     rgb.b = 0xff;
275     swf_SetRGB(tag,&rgb);
276
277     if(stopframe0) {
278         ActionTAG*action = 0;
279         tag = swf_InsertTag(tag, ST_DOACTION);
280         action = action_Stop(action);
281         action = action_End(action);
282         swf_ActionSet(tag, action);
283         swf_ActionFree(action);
284
285         tag = swf_InsertTag(tag, ST_SHOWFRAME);
286     }
287         
288     swf_mp3_bitrate = bitrate;
289     swf_mp3_out_samplerate = samplerate;
290     swf_mp3_in_samplerate = samplerate;
291
292     if(!definesound)
293     {
294         int oldframepos=-1, newframepos=0;
295         float framesamplepos = 0;
296         float framepos = 0;
297         float samplepos = 0;
298         ActionTAG* a = 0;
299         U16 v1=0,v2=0;
300         tag = swf_InsertTag(tag, ST_SOUNDSTREAMHEAD);
301         swf_SetSoundStreamHead(tag, samplesperframe);
302         msg("<notice> %d blocks", numsamples/blocksize);
303         for(t=0;t<numsamples/blocksize;t++) {
304             int s;
305             U16*block1;
306             int seek = blocksize - ((int)samplepos - (int)framesamplepos);
307
308             if(newframepos!=oldframepos) {
309                 tag = swf_InsertTag(tag, ST_SOUNDSTREAMBLOCK);
310                 msg("<notice> Starting block %d %d+%d", t, (int)samplepos, (int)blocksize);
311                 block1 = &samples[t*blocksize];
312                 swf_SetSoundStreamBlock(tag, block1, seek, 1);
313                 v1 = v2 = GET16(tag->data);
314             } else {
315                 msg("<notice> Adding data...", t);
316                 block1 = &samples[t*blocksize];
317                 swf_SetSoundStreamBlock(tag, block1, seek, 0);
318                 v1+=v2;
319                 PUT16(tag->data, v1);
320             }
321             samplepos += blocksize;
322
323             oldframepos = (int)framepos;
324             framepos += framesperblock;
325             newframepos = (int)framepos;
326
327             for(s=oldframepos;s<newframepos;s++) {
328                 tag = swf_InsertTag(tag, ST_SHOWFRAME);
329                 framesamplepos += samplesperframe;
330             }
331         }
332         tag = swf_InsertTag(tag, ST_END);
333     } else {
334         SOUNDINFO info;
335         tag = swf_InsertTag(tag, ST_DEFINESOUND);
336         swf_SetU16(tag, 24); //id
337 #ifdef DEFINESOUND_MP3
338         swf_SetSoundDefine(tag, samples, numsamples);
339 #else
340         swf_SetU8(tag,(/*compression*/0<<4)|(/*rate*/3<<2)|(/*size*/1<<1)|/*mono*/0);
341         swf_SetU32(tag, numsamples); // 44100 -> 11025
342         swf_SetBlock(tag, samples, numsamples*2);
343 #endif
344
345
346         tag = swf_InsertTag(tag, ST_STARTSOUND);
347         swf_SetU16(tag, 24); //id
348         memset(&info, 0, sizeof(info));
349         info.loops = loop;
350         swf_SetSoundInfo(tag, &info);
351         tag = swf_InsertTag(tag, ST_SHOWFRAME);
352         if(stopframe1) {
353             ActionTAG*action = 0;
354             tag = swf_InsertTag(tag, ST_DOACTION);
355             action = action_Stop(action);
356             action = action_End(action);
357             swf_ActionSet(tag, action);
358             swf_ActionFree(action);
359             tag = swf_InsertTag(tag, ST_SHOWFRAME);
360         }
361         tag = swf_InsertTag(tag, ST_END);
362     }
363
364     if(do_cgi) {
365         if FAILED(swf_WriteCGI(&swf)) fprintf(stderr,"WriteCGI() failed.\n");
366     } else {
367         f = open(outputname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
368         if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
369         close(f);
370     }
371
372     swf_FreeTags(&swf);
373     return 0;
374 }
375
376