2 Allows to extract parts of the swf into a new file.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
25 #include "../lib/rfxswf.h"
26 #include "../lib/args.h"
27 #include "../lib/log.h"
28 #include "../lib/jpeg.h"
29 #include "../lib/png.h"
33 #define _ZLIB_INCLUDED_
38 char * destfilename = "output.swf";
42 char* extractframes = 0;
43 char* extractjpegids = 0;
44 char* extractfontids = 0;
45 char* extractpngids = 0;
46 char* extractsoundids = 0;
47 char* extractbinaryids = 0;
48 char* extractanyids = 0;
51 char* extractname = 0;
54 char originalplaceobjects = 0;
58 char *outputformat = NULL;
60 struct options_t options[] =
83 int args_callback_option(char*name,char*val)
85 if(!strcmp(name, "V")) {
86 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
89 else if(!strcmp(name, "o")) {
93 else if(!strcmp(name, "i")) {
97 fprintf(stderr, "You can only supply either name or id\n");
102 else if(!strcmp(name, "n")) {
106 fprintf(stderr, "You can only supply either name or id\n");
111 else if(!strcmp(name, "v")) {
115 else if(!strcmp(name, "m")) {
120 else if(!strcmp(name, "j")) {
122 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
125 /* TODO: count number of IDs in val range */
127 extractjpegids = val;
130 else if(!strcmp(name, "F")) {
132 fprintf(stderr, "Only one --font argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
136 extractfontids = val;
139 else if(!strcmp(name, "s")) {
140 if(extractsoundids) {
141 fprintf(stderr, "Only one --sound argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
145 extractsoundids = val;
148 else if(!strcmp(name, "b")) {
149 if(extractbinaryids) {
150 fprintf(stderr, "Only one --binary argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
154 extractbinaryids = val;
157 #ifdef _ZLIB_INCLUDED_
158 else if(!strcmp(name, "p")) {
160 fprintf(stderr, "Only one --png argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
168 else if(!strcmp(name, "a")) {
173 else if(!strcmp(name, "f")) {
178 else if(!strcmp(name, "P")) {
179 originalplaceobjects = 1;
182 else if(!strcmp(name, "0")) {
186 else if(!strcmp(name, "w")) {
190 else if (!strcmp(name, "O")) {
195 printf("Unknown option: -%s\n", name);
201 int args_callback_longoption(char*name,char*val)
203 return args_long2shortoption(options, name, val);
205 void args_callback_usage(char*name)
207 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
208 printf("\t-v , --verbose\t\t\t Be more verbose\n");
209 printf("\t-o , --output filename\t\t set output filename\n");
210 printf("\t-V , --version\t\t\t Print program version and exit\n\n");
211 printf("SWF Subelement extraction:\n");
212 printf("\t-n , --name name\t\t instance name of the object (SWF Define) to extract\n");
213 printf("\t-i , --id ID\t\t\t ID of the object, shape or movieclip to extract\n");
214 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
215 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n");
216 printf("\t \t\t\t (use with -f)\n");
217 printf("\t-P , --placeobject\t\t\t Insert original placeobject into output file\n");
218 printf("\t \t\t\t (use with -i)\n");
219 printf("SWF Font/Text extraction:\n");
220 printf("\t-F , --font ID\t\t\t Extract font(s)\n");
221 printf("Picture extraction:\n");
222 printf("\t-j , --jpeg ID\t\t\t Extract JPEG picture(s)\n");
223 #ifdef _ZLIB_INCLUDED_
224 printf("\t-p , --pngs ID\t\t\t Extract PNG picture(s)\n");
227 printf("Sound extraction:\n");
228 printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
229 printf("\t-s , --sound ID\t\t\t Extract Sound(s)\n");
231 int args_callback_command(char*name,char*val)
234 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
241 void prepare_name(char *buf, size_t len, const char *prefix,
242 const char *suffix, int idx) {
243 if (outputformat!=NULL) {
244 // override default file name formatting
245 // make sure single-file behavior is not used
247 // Other parts of codebase use vsnprintf, so I assume snprintf
248 // is available on all platforms that swftools currently works on.
249 // We need to check for buffer overflows now that the user is
250 // supplying the format string.
251 snprintf(buf,len,outputformat,idx,suffix);
253 // use default file name formatting, unchanged
254 sprintf(buf,"%s%d.%s",prefix,idx,suffix);
258 U8 mainr,maing,mainb;
259 /* 1 = used, not expanded,
261 5 = wanted, not expanded
268 int extractname_id = -1;
270 void idcallback(void*data)
272 if(!(used[GET16(data)]&1)) {
274 used[GET16(data)] |= 1;
278 void enumerateIDs(TAG*tag, void(*callback)(void*))
284 data = (U8*)malloc(len);
285 PUT16(data, (tag->id<<6)+63);
286 *(U8*)&data[2] = tag->len;
287 *(U8*)&data[3] = tag->len>>8;
288 *(U8*)&data[4] = tag->len>>16;
289 *(U8*)&data[5] = tag->len>>24;
290 memcpy(&data[6], tag->data, tag->len);
293 data = (U8*)malloc(len);
294 PUT16(data, (tag->id<<6)+tag->len);
295 memcpy(&data[2], tag->data, tag->len);
297 map_ids_mem(data, len, callback);
299 int num = swf_GetNumUsedIDs(tag);
300 int *ptr = malloc(sizeof(int)*num);
302 swf_GetUsedIDs(tag, ptr);
304 callback(&tag->data[ptr[t]]);
307 void moveToZero(TAG*tag)
309 if(!swf_isPlaceTag(tag))
312 swf_GetPlaceObject(tag, &obj);
315 swf_ResetTag(tag, tag->id);
316 swf_SetPlaceObject(tag, &obj);
319 void extractTag(SWF*swf, char*filename)
331 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
333 newswf.fileVersion = swf->fileVersion;
334 newswf.frameRate = swf->frameRate;
335 newswf.movieSize = swf->movieSize;
336 if(movetozero && originalplaceobjects) {
337 newswf.movieSize.xmax = swf->movieSize.xmax - swf->movieSize.xmin;
338 newswf.movieSize.ymax = swf->movieSize.ymax - swf->movieSize.ymin;
339 newswf.movieSize.xmin = 0;
340 newswf.movieSize.ymin = 0;
343 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
344 desttag = newswf.firstTag;
348 swf_SetRGB(desttag,&rgb);
350 swf_GetRect(0, &objectbbox);
354 for(t=0;t<65536;t++) {
355 if(used[t] && !(used[t]&2)) {
357 msg("<warning> ID %d is referenced, but never defined.", t);
358 } else if(tags[t]->id==ST_DEFINESPRITE) {
360 while(tag->id != ST_END)
362 enumerateIDs(tag, idcallback);
367 enumerateIDs(tags[t], idcallback);
374 srctag = swf->firstTag;
377 while(srctag && (srctag->id || sprite)) {
382 if(srctag->id == ST_END) {
385 if(srctag->id == ST_DEFINESPRITE)
387 if(srctag->id == ST_JPEGTABLES)
389 if(swf_isDefiningTag(srctag)) {
390 int id = swf_GetDefineID(srctag);
394 b = swf_GetDefineBBox(srctag);
395 swf_ExpandRect2(&objectbbox, &b);
398 if ((((swf_isPlaceTag(srctag) && originalplaceobjects)
399 || srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
400 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
407 if(srctag->id == ST_REMOVEOBJECT) {
408 if(!used[swf_GetPlaceID(srctag)])
413 TAG*ttag = (TAG*)malloc(sizeof(TAG));
414 desttag = swf_InsertTag(desttag, srctag->id);
415 desttag->len = desttag->memsize = srctag->len;
416 desttag->data = malloc(srctag->len);
417 memcpy(desttag->data, srctag->data, srctag->len);
418 if(movetozero && swf_isPlaceTag(desttag)) {
425 srctag = srctag->next;
428 if(!extractframes && !hollow) {
429 if(!originalplaceobjects && (extractids||extractname_id>=0)) {
435 memset(&bbox, 0, sizeof(SRECT));
437 for(t=0;t<65536;t++) {
438 if(is_in_range(t, extractids)) {
445 printf("warning! You should use the -P when extracting multiple objects\n");
449 /* if there is only one object, we will scale it.
450 So let's figure out its bounding box */
451 TAG*tag = swf->firstTag;
453 if(swf_isDefiningTag(tag) && tag->id != ST_DEFINESPRITE) {
454 if(swf_GetDefineID(tag) == id)
455 bbox = swf_GetDefineBBox(tag);
460 newswf.movieSize.xmin = 0;
461 newswf.movieSize.ymin = 0;
462 newswf.movieSize.xmax = 512*20;
463 newswf.movieSize.ymax = 512*20;
465 if((objectbbox.xmin|objectbbox.ymin|objectbbox.xmax|objectbbox.ymax)!=0)
466 newswf.movieSize = objectbbox;
469 if(extractname_id>=0) {
470 desttag = swf_InsertTag(desttag, ST_PLACEOBJECT2);
471 swf_ObjectPlace(desttag, extractname_id, extractname_id, 0,0,extractname);
473 for(t=0;t<65536;t++) {
474 if(is_in_range(t, extractids)) {
476 desttag = swf_InsertTag(desttag, ST_PLACEOBJECT2);
477 swf_GetMatrix(0, &m);
479 int width = bbox.xmax - bbox.xmin;
480 int height = bbox.ymax - bbox.ymin;
481 int max = width>height?width:height;
485 m.sx = (512*20*65536)/max;
486 m.sy = (512*20*65536)/max;
488 //newswf.movieSize = swf_TurnRect(newswf.movieSize, &m);
490 swf_ObjectPlace(desttag, t, t, &m,0,0);
495 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
497 desttag = swf_InsertTag(desttag,ST_END);
499 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT|O_BINARY, 0644);
500 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
503 swf_FreeTags(&newswf); // cleanup
506 int isOfType(int t, TAG*tag)
509 if(t == 0 && (tag->id == ST_DEFINESHAPE ||
510 tag->id == ST_DEFINESHAPE2 ||
511 tag->id == ST_DEFINESHAPE3)) {
514 if(t==1 && tag->id == ST_DEFINESPRITE) {
517 if(t == 2 && (tag->id == ST_DEFINEBITS ||
518 tag->id == ST_DEFINEBITSJPEG2 ||
519 tag->id == ST_DEFINEBITSJPEG3)) {
522 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
523 tag->id == ST_DEFINEBITSLOSSLESS2)) {
526 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
529 if(t == 5 && (tag->id == ST_DEFINEFONT || tag->id == ST_DEFINEFONT2 || tag->id == ST_DEFINEFONT3)) {
532 if (t== 6 && (tag->id == ST_DEFINEBINARY)) {
538 void listObjects(SWF*swf)
544 char*names[] = {"Shape", "MovieClip", "JPEG", "PNG", "Sound", "Font", "Binary"};
545 char*options[] = {"-i", "-i", "-j", "-p", "-s", "-F","-b"};
547 printf("Objects in file %s:\n",filename);
549 for(t=0;t<sizeof(names)/sizeof(names[0]);t++) {
551 int lastid = -2, lastprint=-1;
556 if(tag->id == ST_SOUNDSTREAMHEAD || tag->id == ST_SOUNDSTREAMHEAD2)
565 printf(" [%s] %d %s%s: ID(s) ", options[t], nr, names[t], nr>1?"s":"");
570 char show = isOfType(t,tag);
576 id = swf_GetDefineID(tag);
581 if(first || !follow) {
586 if(lastprint + 1 == lastid)
587 printf(", %d, %d", lastid, id);
589 printf("-%d, %d", lastid, id);
599 if(lastprint + 1 == lastid)
600 printf(", %d", lastid);
602 printf("-%d", lastid);
608 printf(" [-f] %d Frames: ID(s) 0-%d\n", frame, frame);
610 printf(" [-f] 1 Frame: ID(s) 0\n");
613 printf(" [-m] 1 MP3 Soundstream\n");
616 int handlefont(SWF*swf, TAG*tag)
621 char*filename = name;
624 id = swf_GetDefineID(tag);
625 prepare_name(name, sizeof(name), "font", "swf", id);
627 filename = destfilename;
630 swf_FontExtract(swf, id, &f);
632 if (!extractanyids) {
633 printf("Couldn't extract font %d\n", id);
638 swf_WriteFont(f, filename);
643 static char has_jpegtables=0;
644 static U8*jpegtables = 0;
645 static int jpegtablessize = 0;
647 void handlejpegtables(TAG*tag)
649 if(tag->id == ST_JPEGTABLES) {
650 jpegtables = tag->data;
651 jpegtablessize = tag->len;
656 FILE* save_fopen(char* name, char* mode)
658 FILE*fi = fopen(name, mode);
660 fprintf(stderr, "Error: Couldn't open %s\n", name);
666 int findjpegboundary(U8*data, int len)
670 for(t=0;t<len-4;t++) {
681 /* extract jpeg data out of a tag */
682 int handlejpeg(TAG*tag)
685 char*filename = name;
688 if(tag->id != ST_DEFINEBITSJPEG3) {
689 prepare_name(name, sizeof(name), "pic", "jpg", GET16(tag->data));
691 filename = destfilename;
692 if(!strcmp(filename,"output.swf"))
693 filename = "output.jpg";
696 prepare_name(name, sizeof(name), "pic", "png", GET16(tag->data));
698 filename = destfilename;
699 if(!strcmp(filename,"output.swf"))
700 filename = "output.png";
704 /* swf jpeg images have two streams, which both start with ff d8 and
705 end with ff d9. The following code handles sorting the middle
706 <ff d9 ff d8> bytes out, so that one stream remains */
707 if(tag->id == ST_DEFINEBITSJPEG && tag->len>2 && has_jpegtables) {
708 fi = save_fopen(filename, "wb");
709 if(jpegtablessize>=2) {
710 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
711 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
713 fwrite(tag->data+2, tag->len-2, 1, fi);
717 else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
719 int pos = findjpegboundary(&tag->data[2], tag->len-2);
722 fi = save_fopen(filename, "wb");
723 fwrite(&tag->data[2], pos-2, 1, fi);
724 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
727 fi = save_fopen(filename, "wb");
728 fwrite(&tag->data[2], end-2, 1, fi);
732 else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
733 U32 end = GET32(&tag->data[2])+6;
734 int pos = findjpegboundary(&tag->data[6], end);
735 if(end >= tag->len) {
736 msg("<error> zlib data out of bounds in definebitsjpeg3");
740 /* TODO: do we actually need this? */
741 memmove(&tag->data[pos], &tag->data[pos+4], end-(pos+4));
744 int width=0, height=0;
745 jpeg_load_from_mem(&tag->data[6], end-6, &image, &width, &height);
747 uLongf datalen = width*height;
748 Bytef *data = malloc(datalen);
750 int error = uncompress(data, &datalen, &tag->data[end], (uLong)(tag->len - end));
752 fprintf(stderr, "Zlib error %d\n", error);
755 int t, size = width*height;
756 for(t=0;t<size;t++) {
757 image[t*4+0] = data[t];
760 writePNG(filename, image, width, height);
764 int id = GET16(tag->data);
765 if (!extractanyids) {
766 fprintf(stderr, "Object %d is not a JPEG picture!\n", id);
774 #ifdef _ZLIB_INCLUDED_
777 static U32*crc32_table = 0;
778 static void make_crc32_table(void)
783 crc32_table = (U32*)malloc(1024);
785 for (t = 0; t < 256; t++) {
788 for (s = 0; s < 8; s++) {
789 c = (0xedb88320L*(c&1)) ^ (c >> 1);
794 static inline void png_write_byte(FILE*fi, U8 byte)
796 fwrite(&byte,1,1,fi);
797 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
799 static void png_start_chunk(FILE*fi, char*type, int len)
801 U8 mytype[4]={0,0,0,0};
802 U32 mylen = BE_32_TO_NATIVE(len);
803 memcpy(mytype,type,strlen(type));
804 fwrite(&mylen, 4, 1, fi);
806 png_write_byte(fi,mytype[0]);
807 png_write_byte(fi,mytype[1]);
808 png_write_byte(fi,mytype[2]);
809 png_write_byte(fi,mytype[3]);
811 static void png_write_bytes(FILE*fi, U8*bytes, int len)
815 png_write_byte(fi,bytes[t]);
817 static void png_write_dword(FILE*fi, U32 dword)
819 png_write_byte(fi,dword>>24);
820 png_write_byte(fi,dword>>16);
821 png_write_byte(fi,dword>>8);
822 png_write_byte(fi,dword);
824 static void png_end_chunk(FILE*fi)
826 U32 tmp = BE_32_TO_NATIVE((mycrc32^0xffffffff));
831 /* extract a lossless image (png) out of a tag
832 This routine was originally meant to be a one-pager. I just
833 didn't know png is _that_ much fun. :) -mk
835 int handlelossless(TAG*tag)
838 char*filename = name;
853 U8 head[] = {137,80,78,71,13,10,26,10};
855 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
863 if(tag->id != ST_DEFINEBITSLOSSLESS &&
864 tag->id != ST_DEFINEBITSLOSSLESS2) {
865 int id = GET16(tag->data);
866 if (!extractanyids) {
867 fprintf(stderr, "Object %d is not a PNG picture!\n",id);
874 format = swf_GetU8(tag);
875 if(format == 3) bpp = 8;
876 if(format == 4) bpp = 16;
877 if(format == 5) bpp = 32;
878 if(format!=3 && format!=5) {
880 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
882 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
885 width = swf_GetU16(tag);
886 height = swf_GetU16(tag);
887 if(format == 3) cols = swf_GetU8(tag) + 1;
888 // this is what format means according to the flash specification. (which is
890 // if(format == 4) cols = swf_GetU16(tag) + 1;
891 // if(format == 5) cols = swf_GetU32(tag) + 1;
894 msg("<verbose> Width %d", width);
895 msg("<verbose> Height %d", height);
896 msg("<verbose> Format %d", format);
897 msg("<verbose> Cols %d", cols);
898 msg("<verbose> Bpp %d", bpp);
900 datalen = (width*height*bpp/8+cols*8);
905 data = malloc(datalen);
906 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
907 } while(error == Z_BUF_ERROR);
909 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
912 msg("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
914 datalen2 = datalen+16;
915 data2 = malloc(datalen2);
916 palette = (RGBA*)malloc(cols*sizeof(RGBA));
918 for(t=0;t<cols;t++) {
919 palette[t].r = data[pos++];
920 palette[t].g = data[pos++];
921 palette[t].b = data[pos++];
923 palette[t].a = data[pos++];
927 prepare_name(name, sizeof(name), "pic", "png", id);
929 filename = destfilename;
930 if(!strcmp(filename,"output.swf"))
931 filename = "output.png";
933 fi = save_fopen(filename, "wb");
934 fwrite(head,sizeof(head),1,fi);
936 png_start_chunk(fi, "IHDR", 13);
937 png_write_dword(fi,width);
938 png_write_dword(fi,height);
939 png_write_byte(fi,8);
941 png_write_byte(fi,3); //indexed
942 else if(format == 5 && alpha==0)
943 png_write_byte(fi,2); //rgb
944 else if(format == 5 && alpha==1)
945 png_write_byte(fi,6); //rgba
948 png_write_byte(fi,0); //compression mode
949 png_write_byte(fi,0); //filter mode
950 png_write_byte(fi,0); //interlace mode
954 png_start_chunk(fi, "PLTE", 768);
957 png_write_byte(fi,palette[t].r);
958 png_write_byte(fi,palette[t].g);
959 png_write_byte(fi,palette[t].b);
964 /* write alpha palette */
965 png_start_chunk(fi, "tRNS", 256);
967 png_write_byte(fi,palette[t].a);
975 int srcwidth = width * (bpp/8);
976 datalen3 = (width*4+5)*height;
977 data3 = (U8*)malloc(datalen3);
978 for(y=0;y<height;y++)
980 data3[pos2++]=0; //filter type
983 // 32 bit to 24 bit "conversion"
984 for(x=0;x<width;x++) {
985 data3[pos2++]=data[pos+1];
986 data3[pos2++]=data[pos+2];
987 data3[pos2++]=data[pos+3];
988 pos+=4; //ignore padding byte
991 for(x=0;x<width;x++) {
992 data3[pos2++]=data[pos+1];
993 data3[pos2++]=data[pos+2];
994 data3[pos2++]=data[pos+3];
995 data3[pos2++]=data[pos+0]; //alpha
1001 for(x=0;x<srcwidth;x++)
1002 data3[pos2++]=data[pos++];
1005 pos+=((srcwidth+3)&~3)-srcwidth; //align
1010 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
1011 fprintf(stderr, "zlib error in pic %d\n", id);
1014 msg("<verbose> Compressed data is %d bytes", datalen2);
1015 png_start_chunk(fi, "IDAT", datalen2);
1016 png_write_bytes(fi,data2,datalen2);
1018 png_start_chunk(fi, "IEND", 0);
1028 static FILE*mp3file=0;
1029 void handlesoundstream(TAG*tag)
1031 char*filename = "output.mp3";
1032 if(numextracts==1) {
1033 filename = destfilename;
1034 if(!strcmp(filename,"output.swf"))
1035 filename = "output.mp3";
1038 case ST_SOUNDSTREAMHEAD:
1039 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
1040 mp3file = fopen(filename, "wb");
1041 msg("<notice> Writing mp3 data to %s",filename);
1044 msg("<error> Soundstream is not mp3");
1046 case ST_SOUNDSTREAMHEAD2:
1047 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
1048 mp3file = fopen(filename, "wb");
1049 msg("<notice> Writing mp3 data to %s",filename);
1052 msg("<error> Soundstream is not mp3 (2)");
1054 case ST_SOUNDSTREAMBLOCK:
1056 fwrite(&tag->data[4],tag->len-4,1,mp3file);
1061 int handledefinesound(TAG*tag)
1066 char*filename = buf;
1071 int rate,bits,stereo;
1072 char*rates[] = {"5500","11025","22050","44100"};
1073 id = swf_GetU16(tag); //id
1075 flags = swf_GetU8(tag);
1077 rate = (flags>>2)&3;
1078 bits = flags&2?16:8;
1081 samples = swf_GetU32(tag);
1085 if(format == 2) { // mp3
1086 swf_GetU16(tag); //numsamples_seek
1088 } else if(format == 0) { // raw
1089 printf("Sound is RAW, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
1090 // TODO: convert to WAV
1092 } else if(format == 1) { // adpcm
1093 printf("Sound is ADPCM, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
1094 extension = "adpcm";
1098 prepare_name(buf, sizeof(buf), "sound", extension, id);
1099 if(numextracts==1) {
1100 filename = destfilename;
1101 if(!strcmp(filename,"output.swf")) {
1102 sprintf(buf, "output.%s", extension);
1106 fi = save_fopen(filename, "wb");
1107 fwrite(&tag->data[tag->pos], tag->len - tag->pos, 1, fi);
1112 int handlebinary(TAG*tag) {
1115 char *filename = buf;
1116 int len = tag->memsize;
1117 int dx = 6; // offset to binary data
1118 if (tag->id!=ST_DEFINEBINARY) {
1119 if (!extractanyids) {
1120 fprintf(stderr, "Object %d is not a binary entity!\n",
1125 prepare_name(buf, sizeof(buf), "binary", "bin", GET16(tag->data));
1126 if(numextracts==1) {
1127 filename = destfilename;
1128 if(!strcmp(filename,"output.swf")) {
1129 sprintf(buf, "output.bin");
1133 fout = fopen(filename, "wb");
1134 fwrite(tag->data+dx,len-dx,1,fout);
1139 int main (int argc,char ** argv)
1148 char listavailable = 0;
1149 processargs(argc, argv);
1151 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
1152 && !extractmp3 && !extractsoundids && !extractfontids && !extractbinaryids && !extractanyids)
1155 if(!originalplaceobjects && movetozero) {
1156 fprintf(stderr, "Error: -0 (--movetozero) can only be used in conjunction with -P (--placeobject)\n");
1162 fprintf(stderr, "You must supply a filename.\n");
1165 initLog(0,-1,0,0,-1, verbose);
1167 f = open(filename,O_RDONLY|O_BINARY);
1171 perror("Couldn't open file: ");
1174 if (swf_ReadSWF(f,&swf) < 0)
1176 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
1195 tagused = (char*)malloc(tagnum);
1196 memset(tagused, 0, tagnum);
1197 memset(used, 0, 65536);
1198 memset(depths, 0, 65536);
1203 if(swf_isAllowedSpriteTag(tag)) {
1205 if(extractframes && is_in_range(frame, extractframes)) {
1207 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
1208 depths[swf_GetDepth(tag)] = 1;
1210 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
1211 int depth = swf_GetDepth(tag);
1214 depths[swf_GetDepth(tag)] = 0;
1217 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
1218 (depths[swf_GetDepth(tag)]) && hollow) {
1220 depths[swf_GetDepth(tag)] = 0;
1224 enumerateIDs(tag, idcallback);
1226 tagused[tagnum] = 1;
1230 if(tag->id == ST_SOUNDSTREAMHEAD ||
1231 tag->id == ST_SOUNDSTREAMHEAD2 ||
1232 tag->id == ST_SOUNDSTREAMBLOCK) {
1234 handlesoundstream(tag);
1237 if(tag->id == ST_JPEGTABLES) {
1238 handlejpegtables(tag);
1241 if(swf_isDefiningTag(tag)) {
1242 int id = swf_GetDefineID(tag);
1244 if(extractids && is_in_range(id, extractids)) {
1248 if(extractfontids && is_in_range(id, extractfontids)) {
1249 handlefont(&swf, tag);
1251 if(extractjpegids && is_in_range(id, extractjpegids)) {
1254 if(extractsoundids && is_in_range(id, extractsoundids)) {
1255 handledefinesound(tag);
1257 if(extractbinaryids && is_in_range(id, extractbinaryids)) {
1260 #ifdef _ZLIB_INCLUDED_
1261 if(extractpngids && is_in_range(id, extractpngids)) {
1262 handlelossless(tag);
1265 if(extractanyids && is_in_range(id, extractanyids)) {
1266 if (handlefont(&swf,tag)) {
1268 } else if (handlejpeg(tag)) {
1270 } else if (handlebinary(tag)) {
1272 #ifdef _ZLIB_INCLUDED_
1273 } else if (handlelossless(tag)) {
1276 } else if (handledefinesound(tag)) {
1277 // Not sure if sound code checks carefully for type.
1280 printf("#%d not processed\n", id);
1284 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
1285 mainr = tag->data[0];
1286 maing = tag->data[1];
1287 mainb = tag->data[2];
1289 else if(swf_isPlaceTag(tag) && tag->id != ST_PLACEOBJECT ) {
1290 char*name = swf_GetName(tag);
1291 if(name && extractname && !strcmp(name, extractname)) {
1292 int id = swf_GetPlaceID(tag);
1295 if(originalplaceobjects) {
1296 tagused[tagnum] = 1;
1298 depths[swf_GetDepth(tag)] = 1;
1299 extractname_id = id;
1302 else if(tag->id == ST_SHOWFRAME) {
1305 tagused[tagnum] = 1;
1310 if(tag->id == ST_DEFINESPRITE) {
1311 while(tag->id != ST_END) {
1320 extractTag(&swf, destfilename);
1326 msg("<error> Didn't find a soundstream in file");