implemented --framerate.
[swftools.git] / src / swfextract.c
index 126eff4..28a45a3 100644 (file)
 #include <fcntl.h>
 #include "../lib/rfxswf.h"
 #include "../lib/args.h"
-#include "reloc.h"
+#include "../lib/log.h"
+#ifdef HAVE_ZLIB_H
+#ifdef HAVE_LIBZ
 #include "zlib.h"
+#define _ZLIB_INCLUDED_
+#endif
+#endif
 
 char * filename = 0;
 char * destfilename = "output.swf";
-int verbose = 2;
+int verbose = 3;
 
 char* extractids = 0;
 char* extractframes = 0;
 char* extractjpegids = 0;
 char* extractpngids = 0;
+char* extractsoundids = 0;
+char extractmp3 = 0;
 
 char* extractname = 0;
 
 char hollow = 0;
 
+int numextracts = 0;
+
 struct options_t options[] =
 {
  {"o","output"},
@@ -36,12 +45,15 @@ struct options_t options[] =
  {"i","id"},
  {"j","jpegs"},
  {"p","pngs"},
+ {"m","mp3"},
+ {"s","sound"},
  {"n","name"},
  {"f","frame"},
  {"V","version"},
  {0,0}
 };
 
+
 int args_callback_option(char*name,char*val)
 {
     if(!strcmp(name, "V")) {
@@ -54,6 +66,7 @@ int args_callback_option(char*name,char*val)
     } 
     else if(!strcmp(name, "i")) {
        extractids = val;
+       numextracts++;
        if(extractname) {
            fprintf(stderr, "You can only supply either name or id\n");
            exit(1);
@@ -62,6 +75,7 @@ int args_callback_option(char*name,char*val)
     } 
     else if(!strcmp(name, "n")) {
        extractname = val;
+       numextracts++;
        if(extractids) {
            fprintf(stderr, "You can only supply either name or id\n");
            exit(1);
@@ -72,23 +86,42 @@ int args_callback_option(char*name,char*val)
        verbose ++;
        return 0;
     } 
+    else if(!strcmp(name, "m")) {
+       extractmp3 = 1;
+       numextracts++;
+       return 0;
+    }
     else if(!strcmp(name, "j")) {
        if(extractjpegids) {
            fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
            exit(1);
        }
+       numextracts++;
        extractjpegids = val;
        return 1;
     } 
+    else if(!strcmp(name, "s")) {
+       if(extractsoundids) {
+           fprintf(stderr, "Only one --sound argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
+           exit(1);
+       }
+       numextracts++;
+       extractsoundids = val;
+       return 1;
+    } 
+#ifdef _ZLIB_INCLUDED_
     else if(!strcmp(name, "p")) {
        if(extractpngids) {
            fprintf(stderr, "Only one --pngs argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
            exit(1);
        }
+       numextracts++;
        extractpngids = val;
        return 1;
     } 
+#endif
     else if(!strcmp(name, "f")) {
+       numextracts++;
        extractframes = val;
        return 1;
     }
@@ -112,13 +145,22 @@ void args_callback_usage(char*name)
     printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
     printf("\t-v , --verbose\t\t\t Be more verbose\n");
     printf("\t-o , --output filename\t\t set output filename\n");
-    printf("\t-n , --name name\t\t instance name of the object to extract\n");
-    printf("\t-i , --id IDs\t\t\t ID of the object to extract\n");
-    printf("\t-j , --jpeg IDs\t\t\t IDs of the JPEG pictures to extract\n");
-    printf("\t-p , --pngs IDs\t\t\t IDs of the PNG pictures to extract\n");
+    printf("\t-V , --version\t\t\t Print program version and exit\n\n");
+    printf("SWF Subelement extraction:\n");
+    printf("\t-n , --name name\t\t instance name of the object (SWF Define) to extract\n");
+    printf("\t-i , --id ID\t\t\t ID of the object (SWF Define) to extract\n");
     printf("\t-f , --frame frames\t\t frame numbers to extract\n");
-    printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames (use with -f)\n");
-    printf("\t-V , --version\t\t\t Print program version and exit\n");
+    printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n"); 
+    printf("\t             \t\t\t (use with -f)\n");
+    printf("Picture extraction:\n");
+    printf("\t-j , --jpeg ID\t\t\t Extract JPEG picture(s)\n");
+#ifdef _ZLIB_INCLUDED_
+    printf("\t-p , --pngs ID\t\t\t Extract PNG picture(s)\n");
+#endif
+    printf("\n");
+    printf("Sound extraction:\n");
+    printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
+    printf("\t-s , --sound ID\t\t\t Extract Sound(s)\n");
 }
 int args_callback_command(char*name,char*val)
 {
@@ -143,29 +185,39 @@ char * tagused;
 
 void idcallback(void*data)
 {
-    if(!(used[*(U16*)data]&1)) {
+    if(!(used[GET16(data)]&1)) {
        changed = 1;
-       used[*(U16*)data] |= 1;
+       used[GET16(data)] |= 1;
     }
 }
 
 void enumerateIDs(TAG*tag, void(*callback)(void*))
 {
-    U8*data;
+/*    U8*data;
     int len = tag->len;
     if(tag->len>=64) {
        len += 6;
        data = (U8*)malloc(len);
-       *(U16*)data = (tag->id<<6)+63;
-       *(U32*)&data[2] = tag->len;
+       PUT16(data, (tag->id<<6)+63);
+       *(U8*)&data[2] = tag->len;
+       *(U8*)&data[3] = tag->len>>8;
+       *(U8*)&data[4] = tag->len>>16;
+       *(U8*)&data[5] = tag->len>>24;
        memcpy(&data[6], tag->data, tag->len);
     } else {
        len += 2;
        data = (U8*)malloc(len);
-       *(U16*)data = (tag->id<<6)+tag->len;
+       PUT16(data, (tag->id<<6)+tag->len);
        memcpy(&data[2], tag->data, tag->len);
     }
     map_ids_mem(data, len, callback);
+ */
+    int num = swf_GetNumUsedIDs(tag);
+    int *ptr = malloc(sizeof(int)*num);
+    int t;
+    swf_GetUsedIDs(tag, ptr);
+    for(t=0;t<num;t++)
+       callback(&tag->data[ptr[t]]);
 }
 
 void extractTag(SWF*swf, char*filename)
@@ -383,7 +435,7 @@ void handlejpeg(TAG*tag)
 {
     char name[80];
     FILE*fi;
-    sprintf(name, "pic%d.jpeg", *(U16*)tag->data);
+    sprintf(name, "pic%d.jpeg", GET16(tag->data));
     /* swf jpeg images have two streams, which both start with ff d8 and
        end with ff d9. The following code handles sorting the middle
        <ff d9 ff d8> bytes out, so that one stream remains */
@@ -393,7 +445,7 @@ void handlejpeg(TAG*tag)
        fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
        fclose(fi);
     }
-    if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
+    else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
        int end = tag->len;
        int pos = findjpegboundary(&tag->data[2], tag->len-2);
        if(pos<0)
@@ -404,8 +456,8 @@ void handlejpeg(TAG*tag)
        fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
        fclose(fi);
     }
-    if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
-       U32 end = *(U32*)&tag->data[2]+6;
+    else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
+       U32 end = GET32(&tag->data[2])+6;
        int pos = findjpegboundary(&tag->data[6], tag->len-6);
        if(pos<0)
            return;
@@ -415,8 +467,14 @@ void handlejpeg(TAG*tag)
        fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
        fclose(fi);
     }
+    else {
+       int id = GET16(tag->data);
+       fprintf(stderr, "Object %d is not a JPEG picture!\n",id);
+       exit(1);
+    }
 }
 
+#ifdef _ZLIB_INCLUDED_
 static U32 mycrc32;
 
 static U32*crc32_table = 0;
@@ -505,8 +563,11 @@ void handlelossless(TAG*tag)
     make_crc32_table();
 
     if(tag->id != ST_DEFINEBITSLOSSLESS &&
-       tag->id != ST_DEFINEBITSLOSSLESS2)
-       return;
+       tag->id != ST_DEFINEBITSLOSSLESS2) {
+       int id = GET16(tag->data);
+       fprintf(stderr, "Object %d is not a PNG picture!\n",id);
+       exit(1);
+    }
 
     id =swf_GetU16(tag);
     format = swf_GetU8(tag);
@@ -633,6 +694,71 @@ void handlelossless(TAG*tag)
     free(data2);
     free(data3);
 }
+#endif
+
+FILE*mp3file;
+void handlesoundstream(TAG*tag)
+{
+    char*filename = "output.mp3";
+    if(numextracts==1) {
+       filename = destfilename;
+       if(!strcmp(filename,"output.swf"))
+           filename = "output.mp3";
+    }
+    switch(tag->id) {
+       case ST_SOUNDSTREAMHEAD:
+           if((tag->data[1]&0x30) == 0x20) { //mp3 compression
+               mp3file = fopen(filename, "wb");
+               logf("<notice> Writing mp3 data to %s",filename);
+           }
+           else
+               logf("<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);
+           }
+           else
+               logf("<error> Soundstream is not mp3 (2)");
+       break;
+       case ST_SOUNDSTREAMBLOCK:
+           if(mp3file)
+               fwrite(&tag->data[4],tag->len-4,1,mp3file);
+       break;
+    }
+}
+
+void handledefinesound(TAG*tag)
+{
+    U8 flags;
+    U32 samples;
+    char buf[128];
+    char*filename = buf;
+    FILE*fi;
+    U16 id;
+    id = swf_GetU16(tag); //id
+    sprintf(buf, "sound%d.mp3", id);
+
+    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;
+    }
+    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);
+}
 
 int main (int argc,char ** argv)
 { 
@@ -646,7 +772,8 @@ int main (int argc,char ** argv)
     char listavailable = 0;
     processargs(argc, argv);
 
-    if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids)
+    if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
+       && !extractmp3 && !extractsoundids)
        listavailable = 1;
 
     if(!filename)
@@ -719,6 +846,13 @@ int main (int argc,char ** argv)
            }
        }
 
+       if(tag->id == ST_SOUNDSTREAMHEAD ||
+          tag->id == ST_SOUNDSTREAMHEAD2 ||
+          tag->id == ST_SOUNDSTREAMBLOCK) {
+           if(extractmp3)
+               handlesoundstream(tag);
+       }
+
        if(tag->id == ST_JPEGTABLES)
            handlejpegtables(tag);
 
@@ -732,9 +866,14 @@ int main (int argc,char ** argv)
            if(extractjpegids && is_in_range(id, extractjpegids)) {
                handlejpeg(tag);
            }
+           if(extractsoundids && is_in_range(id, extractsoundids)) {
+               handledefinesound(tag);
+           }
+#ifdef _ZLIB_INCLUDED_
            if(extractpngids && is_in_range(id, extractpngids)) {
                handlelossless(tag);
            }
+#endif
        }
        else if (tag->id == ST_SETBACKGROUNDCOLOR) {
            mainr = tag->data[0];
@@ -771,6 +910,9 @@ int main (int argc,char ** argv)
     if (found)
        extractTag(&swf, destfilename);
 
+    if(mp3file)
+       fclose(mp3file);
+
     swf_FreeTags(&swf);
     return 0;
 }