added handling for RAW and ADPCM sounds
[swftools.git] / src / swfextract.c
index b0cb511..c7e7ec0 100644 (file)
@@ -4,8 +4,20 @@
    Part of the swftools package.
    
    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
 
-   This file is distributed under the GPL, see file COPYING for details */
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
 #include <unistd.h>
 #include <stdio.h>
@@ -273,7 +285,9 @@ void extractTag(SWF*swf, char*filename)
        changed = 0;
        for(t=0;t<65536;t++) {
           if(used[t] && !(used[t]&2)) {
-            if(tags[t]->id==ST_DEFINESPRITE) {
+            if(tags[t]==0) {
+                msg("<warning> ID %d is referenced, but never defined.", t);
+            } else if(tags[t]->id==ST_DEFINESPRITE) {
                 TAG*tag = tags[t];
                 while(tag->id != ST_END)
                 {
@@ -676,11 +690,11 @@ void handlelossless(TAG*tag)
 //    if(format == 5) cols = swf_GetU32(tag) + 1;
     else cols = 0;
 
-    logf("<verbose> Width %d", width);
-    logf("<verbose> Height %d", height);
-    logf("<verbose> Format %d", format);
-    logf("<verbose> Cols %d", cols);
-    logf("<verbose> Bpp %d", bpp);
+    msg("<verbose> Width %d", width);
+    msg("<verbose> Height %d", height);
+    msg("<verbose> Format %d", format);
+    msg("<verbose> Cols %d", cols);
+    msg("<verbose> Bpp %d", bpp);
 
     datalen = (width*height*bpp/8+cols*8);
     do {
@@ -694,7 +708,7 @@ void handlelossless(TAG*tag)
        fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
        return;
     }
-    logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
+    msg("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
     pos = 0;
     datalen2 = datalen;
     data2 = malloc(datalen2);
@@ -774,7 +788,7 @@ void handlelossless(TAG*tag)
        fprintf(stderr, "zlib error in pic %d\n", id);
        return;
     }
-    logf("<verbose> Compressed data is %d bytes", datalen2);
+    msg("<verbose> Compressed data is %d bytes", datalen2);
     png_start_chunk(fi, "IDAT", datalen2);
     png_write_bytes(fi,data2,datalen2);
     png_end_chunk(fi);
@@ -800,18 +814,18 @@ void handlesoundstream(TAG*tag)
        case ST_SOUNDSTREAMHEAD:
            if((tag->data[1]&0x30) == 0x20) { //mp3 compression
                mp3file = fopen(filename, "wb");
-               logf("<notice> Writing mp3 data to %s",filename);
+               msg("<notice> Writing mp3 data to %s",filename);
            }
            else
-               logf("<error> Soundstream is not mp3");
+               msg("<error> Soundstream is not mp3");
        break;
        case ST_SOUNDSTREAMHEAD2:
            if((tag->data[1]&0x30) == 0x20) {//mp3 compression
                mp3file = fopen(filename, "wb");
-               logf("<notice> Writing mp3 data to %s",filename);
+               msg("<notice> Writing mp3 data to %s",filename);
            }
            else
-               logf("<error> Soundstream is not mp3 (2)");
+               msg("<error> Soundstream is not mp3 (2)");
        break;
        case ST_SOUNDSTREAMBLOCK:
            if(mp3file)
@@ -827,25 +841,40 @@ void handledefinesound(TAG*tag)
     char buf[128];
     char*filename = buf;
     FILE*fi;
+    char*extension = 0;
+    int format;
     U16 id;
+    int rate,bits,stereo;
+    char*rates[] = {"5500","11025","22050","44100"};
     id = swf_GetU16(tag); //id
-    sprintf(buf, "sound%d.mp3", id);
+    
+    flags = swf_GetU8(tag);
+    format = flags>>4;
+    rate = (flags>>2)&3;
+    bits = flags&2?16:8;
+    stereo = flags&1;
+    
+    samples = swf_GetU32(tag);
 
+    if(format == 2) { // mp3
+       swf_GetU16(tag); //numsamples_seek
+       extension = "mp3";
+    } else if(format == 0) { // raw
+       printf("Sound is RAW, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
+       // TODO: convert to WAV
+       extension = "raw";
+    } else if(format == 1) { // adpcm
+       printf("Sound is ADPCM, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
+       extension = "adpcm";
+    }
+    sprintf(buf, "sound%d.%s", id, extension);
     if(numextracts==1) {
        filename = destfilename;
-       if(!strcmp(filename,"output.swf"))
-           filename = "output.mp3";
-    }
-    flags = swf_GetU8(tag);
-    if((flags>>4)!=2) {
-       printf("Sorry, can only extract MP3 sounds. Sound %d is ADPCM or RAW.\n", id);
-       /* not mp3 */
-       return;
+       if(!strcmp(filename,"output.swf")) {
+           sprintf(buf, "output.%s", extension);
+           filename = buf;
+       }
     }
-    samples = swf_GetU32(tag);
-    
-    swf_GetU16(tag); //(only for mp3) numsamples_seek
-
     fi = save_fopen(filename, "wb");
     fwrite(&tag->data[tag->pos], tag->len - tag->pos, 1, fi);
     fclose(fi);