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 file is distributed under the GPL, see file COPYING for details */
13 #include "../lib/rfxswf.h"
14 #include "../lib/args.h"
15 #include "../lib/log.h"
19 #define _ZLIB_INCLUDED_
24 char * destfilename = "output.swf";
28 char* extractframes = 0;
29 char* extractjpegids = 0;
30 char* extractfontids = 0;
31 char* extractpngids = 0;
32 char* extractsoundids = 0;
35 char* extractname = 0;
38 char originalplaceobjects = 0;
42 struct options_t options[] =
61 int args_callback_option(char*name,char*val)
63 if(!strcmp(name, "V")) {
64 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
67 else if(!strcmp(name, "o")) {
71 else if(!strcmp(name, "i")) {
75 fprintf(stderr, "You can only supply either name or id\n");
80 else if(!strcmp(name, "n")) {
84 fprintf(stderr, "You can only supply either name or id\n");
89 else if(!strcmp(name, "v")) {
93 else if(!strcmp(name, "m")) {
98 else if(!strcmp(name, "j")) {
100 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
104 extractjpegids = val;
107 else if(!strcmp(name, "F")) {
109 fprintf(stderr, "Only one --font argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
113 extractfontids = val;
116 else if(!strcmp(name, "s")) {
117 if(extractsoundids) {
118 fprintf(stderr, "Only one --sound argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
122 extractsoundids = val;
125 #ifdef _ZLIB_INCLUDED_
126 else if(!strcmp(name, "p")) {
128 fprintf(stderr, "Only one --png argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
136 else if(!strcmp(name, "f")) {
141 else if(!strcmp(name, "P")) {
142 originalplaceobjects = 1;
145 else if(!strcmp(name, "w")) {
150 printf("Unknown option: -%s\n", name);
156 int args_callback_longoption(char*name,char*val)
158 return args_long2shortoption(options, name, val);
160 void args_callback_usage(char*name)
162 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
163 printf("\t-v , --verbose\t\t\t Be more verbose\n");
164 printf("\t-o , --output filename\t\t set output filename\n");
165 printf("\t-V , --version\t\t\t Print program version and exit\n\n");
166 printf("SWF Subelement extraction:\n");
167 printf("\t-n , --name name\t\t instance name of the object (SWF Define) to extract\n");
168 printf("\t-i , --id ID\t\t\t ID of the object, shape or movieclip to extract\n");
169 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
170 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n");
171 printf("\t \t\t\t (use with -f)\n");
172 printf("\t-P , --placeobject\t\t\t Insert original placeobject into output file\n");
173 printf("\t \t\t\t (use with -i)\n");
174 printf("SWF Font/Text extraction:\n");
175 printf("\t-F , --font ID\t\t\t Extract font(s)\n");
176 printf("Picture extraction:\n");
177 printf("\t-j , --jpeg ID\t\t\t Extract JPEG picture(s)\n");
178 #ifdef _ZLIB_INCLUDED_
179 printf("\t-p , --pngs ID\t\t\t Extract PNG picture(s)\n");
182 printf("Sound extraction:\n");
183 printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
184 printf("\t-s , --sound ID\t\t\t Extract Sound(s)\n");
186 int args_callback_command(char*name,char*val)
189 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
196 U8 mainr,maing,mainb;
197 /* 1 = used, not expanded,
199 5 = wanted, not expanded
206 int extractname_id = -1;
208 void idcallback(void*data)
210 if(!(used[GET16(data)]&1)) {
212 used[GET16(data)] |= 1;
216 void enumerateIDs(TAG*tag, void(*callback)(void*))
222 data = (U8*)malloc(len);
223 PUT16(data, (tag->id<<6)+63);
224 *(U8*)&data[2] = tag->len;
225 *(U8*)&data[3] = tag->len>>8;
226 *(U8*)&data[4] = tag->len>>16;
227 *(U8*)&data[5] = tag->len>>24;
228 memcpy(&data[6], tag->data, tag->len);
231 data = (U8*)malloc(len);
232 PUT16(data, (tag->id<<6)+tag->len);
233 memcpy(&data[2], tag->data, tag->len);
235 map_ids_mem(data, len, callback);
237 int num = swf_GetNumUsedIDs(tag);
238 int *ptr = malloc(sizeof(int)*num);
240 swf_GetUsedIDs(tag, ptr);
242 callback(&tag->data[ptr[t]]);
245 void extractTag(SWF*swf, char*filename)
257 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
259 newswf.fileVersion = swf->fileVersion;
260 newswf.frameRate = swf->frameRate;
261 newswf.movieSize = swf->movieSize;
263 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
264 desttag = newswf.firstTag;
268 swf_SetRGB(desttag,&rgb);
270 swf_GetRect(0, &objectbbox);
274 for(t=0;t<65536;t++) {
275 if(used[t] && !(used[t]&2)) {
276 if(tags[t]->id==ST_DEFINESPRITE) {
278 while(tag->id != ST_END)
280 enumerateIDs(tag, idcallback);
285 enumerateIDs(tags[t], idcallback);
292 srctag = swf->firstTag;
295 while(srctag && (srctag->id || sprite)) {
300 if(srctag->id == ST_END) {
303 if(srctag->id == ST_DEFINESPRITE)
305 if(swf_isDefiningTag(srctag)) {
306 int id = swf_GetDefineID(srctag);
310 b = swf_GetDefineBBox(srctag);
311 swf_ExpandRect2(&objectbbox, &b);
314 if (((((srctag->id == ST_PLACEOBJECT || srctag->id == ST_PLACEOBJECT2) && originalplaceobjects)
315 || srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
316 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
323 if(srctag->id == ST_REMOVEOBJECT) {
324 if(!used[swf_GetPlaceID(srctag)])
329 TAG*ttag = (TAG*)malloc(sizeof(TAG));
330 desttag = swf_InsertTag(desttag, srctag->id);
331 desttag->len = desttag->memsize = srctag->len;
332 desttag->data = malloc(srctag->len);
333 memcpy(desttag->data, srctag->data, srctag->len);
338 srctag = srctag->next;
341 if(!extractframes && !hollow) {
342 if(!originalplaceobjects && (extractids||extractname_id>=0)) {
345 if((objectbbox.xmin|objectbbox.ymin|objectbbox.xmax|objectbbox.ymax)!=0)
346 newswf.movieSize = objectbbox;
347 if(extractname_id>=0) {
348 desttag = swf_InsertTag(desttag, ST_PLACEOBJECT2);
349 swf_ObjectPlace(desttag, extractname_id, extractname_id, 0,0,extractname);
351 for(t=0;t<65536;t++) {
352 if(is_in_range(t, extractids)) {
353 desttag = swf_InsertTag(desttag, ST_PLACEOBJECT2);
354 swf_ObjectPlace(desttag, t, t, 0,0,0);
357 printf("warning! You should use the -P when extracting multiple objects\n");
362 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
364 desttag = swf_InsertTag(desttag,ST_END);
366 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT|O_BINARY, 0644);
367 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
370 swf_FreeTags(&newswf); // cleanup
373 void listObjects(SWF*swf)
379 char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Fonts"};
380 printf("Objects in file %s:\n",filename);
381 for(t=0;t<sizeof(names)/sizeof(names[0]);t++) {
388 (tag->id == ST_DEFINESHAPE ||
389 tag->id == ST_DEFINESHAPE2 ||
390 tag->id == ST_DEFINESHAPE3)) {
392 sprintf(text,"%d", swf_GetDefineID(tag));
395 if(tag->id == ST_DEFINESPRITE) {
398 sprintf(text,"%d", swf_GetDefineID(tag));
401 while(tag->id != ST_END)
405 if(t == 2 && (tag->id == ST_DEFINEBITS ||
406 tag->id == ST_DEFINEBITSJPEG2 ||
407 tag->id == ST_DEFINEBITSJPEG3)) {
409 sprintf(text,"%d", swf_GetDefineID(tag));
412 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
413 tag->id == ST_DEFINEBITSLOSSLESS2)) {
415 sprintf(text,"%d", swf_GetDefineID(tag));
419 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
421 sprintf(text,"%d", swf_GetDefineID(tag));
424 if(t == 5 && (tag->id == ST_DEFINEFONT || tag->id == ST_DEFINEFONT2)) {
426 sprintf(text,"%d", swf_GetDefineID(tag));
433 printf("%s: ", names[t]);
444 printf("Frames: 0-%d\n", frame);
446 printf("Frames: 0\n");
449 void handlefont(SWF*swf, TAG*tag)
454 char*filename = name;
457 id = swf_GetDefineID(tag);
458 sprintf(name, "font%d.swf", id);
460 filename = destfilename;
463 swf_FontExtract(swf, id, &f);
465 printf("Couldn't extract font %d\n", id);
469 swf_FontCreateLayout(f);
471 swf_WriteFont(f, filename);
478 void handlejpegtables(TAG*tag)
480 if(tag->id == ST_JPEGTABLES) {
481 jpegtables = tag->data;
482 jpegtablessize = tag->len;
486 FILE* save_fopen(char* name, char* mode)
488 FILE*fi = fopen(name, mode);
490 fprintf(stderr, "Error: Couldn't open %s\n", name);
496 int findjpegboundary(U8*data, int len)
511 /* extract jpeg data out of a tag */
512 void handlejpeg(TAG*tag)
515 char*filename = name;
518 sprintf(name, "pic%d.jpg", GET16(tag->data));
520 filename = destfilename;
521 if(!strcmp(filename,"output.swf"))
522 filename = "output.jpg";
524 /* swf jpeg images have two streams, which both start with ff d8 and
525 end with ff d9. The following code handles sorting the middle
526 <ff d9 ff d8> bytes out, so that one stream remains */
527 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
528 fi = save_fopen(filename, "wb");
529 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
530 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
533 else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
535 int pos = findjpegboundary(&tag->data[2], tag->len-2);
539 fi = save_fopen(filename, "wb");
540 fwrite(&tag->data[2], pos-2, 1, fi);
541 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
544 else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
545 U32 end = GET32(&tag->data[2])+6;
546 int pos = findjpegboundary(&tag->data[6], tag->len-6);
550 fi = save_fopen(filename, "wb");
551 fwrite(&tag->data[6], pos-6, 1, fi);
552 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
556 int id = GET16(tag->data);
557 fprintf(stderr, "Object %d is not a JPEG picture!\n",id);
562 #ifdef _ZLIB_INCLUDED_
565 static U32*crc32_table = 0;
566 static void make_crc32_table(void)
571 crc32_table = (U32*)malloc(1024);
573 for (t = 0; t < 256; t++) {
576 for (s = 0; s < 8; s++) {
577 c = (0xedb88320L*(c&1)) ^ (c >> 1);
582 static void png_write_byte(FILE*fi, U8 byte)
584 fwrite(&byte,1,1,fi);
585 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
587 static void png_start_chunk(FILE*fi, char*type, int len)
589 U8 mytype[4]={0,0,0,0};
590 U32 mylen = REVERSESWAP32(len);
591 memcpy(mytype,type,strlen(type));
592 fwrite(&mylen, 4, 1, fi);
594 png_write_byte(fi,mytype[0]);
595 png_write_byte(fi,mytype[1]);
596 png_write_byte(fi,mytype[2]);
597 png_write_byte(fi,mytype[3]);
599 static void png_write_bytes(FILE*fi, U8*bytes, int len)
603 png_write_byte(fi,bytes[t]);
605 static void png_write_dword(FILE*fi, U32 dword)
607 png_write_byte(fi,dword>>24);
608 png_write_byte(fi,dword>>16);
609 png_write_byte(fi,dword>>8);
610 png_write_byte(fi,dword);
612 static void png_end_chunk(FILE*fi)
614 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
619 /* extract a lossless image (png) out of a tag
620 This routine was originally meant to be a one-pager. I just
621 didn't know png is _that_ much fun. :) -mk
623 void handlelossless(TAG*tag)
626 char*filename = name;
641 U8 head[] = {137,80,78,71,13,10,26,10};
643 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
651 if(tag->id != ST_DEFINEBITSLOSSLESS &&
652 tag->id != ST_DEFINEBITSLOSSLESS2) {
653 int id = GET16(tag->data);
654 fprintf(stderr, "Object %d is not a PNG picture!\n",id);
659 format = swf_GetU8(tag);
660 if(format == 3) bpp = 8;
661 if(format == 4) bpp = 16;
662 if(format == 5) bpp = 32;
663 if(format!=3 && format!=5) {
665 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
667 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
670 width = swf_GetU16(tag);
671 height = swf_GetU16(tag);
672 if(format == 3) cols = swf_GetU8(tag) + 1;
673 // this is what format means according to the flash specification. (which is
675 // if(format == 4) cols = swf_GetU16(tag) + 1;
676 // if(format == 5) cols = swf_GetU32(tag) + 1;
679 logf("<verbose> Width %d", width);
680 logf("<verbose> Height %d", height);
681 logf("<verbose> Format %d", format);
682 logf("<verbose> Cols %d", cols);
683 logf("<verbose> Bpp %d", bpp);
685 datalen = (width*height*bpp/8+cols*8);
690 data = malloc(datalen);
691 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
692 } while(error == Z_BUF_ERROR);
694 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
697 logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
700 data2 = malloc(datalen2);
701 palette = (RGBA*)malloc(cols*sizeof(RGBA));
703 for(t=0;t<cols;t++) {
704 palette[t].r = data[pos++];
705 palette[t].g = data[pos++];
706 palette[t].b = data[pos++];
708 palette[t].a = data[pos++];
712 sprintf(name, "pic%d.png", id);
714 filename = destfilename;
715 if(!strcmp(filename,"output.swf"))
716 filename = "output.png";
718 fi = save_fopen(filename, "wb");
719 fwrite(head,sizeof(head),1,fi);
721 png_start_chunk(fi, "IHDR", 13);
722 png_write_dword(fi,width);
723 png_write_dword(fi,height);
724 png_write_byte(fi,8);
726 png_write_byte(fi,3); //indexed
728 png_write_byte(fi,2); //rgb
731 png_write_byte(fi,0); //compression mode
732 png_write_byte(fi,0); //filter mode
733 png_write_byte(fi,0); //interlace mode
737 png_start_chunk(fi, "PLTE", 768);
739 png_write_byte(fi,palette[t].r);
740 png_write_byte(fi,palette[t].g);
741 png_write_byte(fi,palette[t].b);
748 int srcwidth = width * (bpp/8);
749 datalen3 = width*height*4;
750 data3 = (U8*)malloc(datalen3);
751 for(y=0;y<height;y++)
753 data3[pos2++]=0; //filter type
755 // 32 bit to 24 bit "conversion"
756 for(x=0;x<width;x++) {
757 data3[pos2++]=data[pos+1];
758 data3[pos2++]=data[pos+2];
759 data3[pos2++]=data[pos+3];
760 pos+=4; //ignore padding byte
764 for(x=0;x<srcwidth;x++)
765 data3[pos2++]=data[pos++];
768 pos+=((srcwidth+3)&~3)-srcwidth; //align
773 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
774 fprintf(stderr, "zlib error in pic %d\n", id);
777 logf("<verbose> Compressed data is %d bytes", datalen2);
778 png_start_chunk(fi, "IDAT", datalen2);
779 png_write_bytes(fi,data2,datalen2);
781 png_start_chunk(fi, "IEND", 0);
791 void handlesoundstream(TAG*tag)
793 char*filename = "output.mp3";
795 filename = destfilename;
796 if(!strcmp(filename,"output.swf"))
797 filename = "output.mp3";
800 case ST_SOUNDSTREAMHEAD:
801 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
802 mp3file = fopen(filename, "wb");
803 logf("<notice> Writing mp3 data to %s",filename);
806 logf("<error> Soundstream is not mp3");
808 case ST_SOUNDSTREAMHEAD2:
809 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
810 mp3file = fopen(filename, "wb");
811 logf("<notice> Writing mp3 data to %s",filename);
814 logf("<error> Soundstream is not mp3 (2)");
816 case ST_SOUNDSTREAMBLOCK:
818 fwrite(&tag->data[4],tag->len-4,1,mp3file);
823 void handledefinesound(TAG*tag)
831 id = swf_GetU16(tag); //id
832 sprintf(buf, "sound%d.mp3", id);
835 filename = destfilename;
836 if(!strcmp(filename,"output.swf"))
837 filename = "output.mp3";
839 flags = swf_GetU8(tag);
841 printf("Sorry, can only extract MP3 sounds. Sound %d is ADPCM or RAW.\n", id);
845 samples = swf_GetU32(tag);
847 swf_GetU16(tag); //(only for mp3) numsamples_seek
849 fi = save_fopen(filename, "wb");
850 fwrite(&tag->data[tag->pos], tag->len - tag->pos, 1, fi);
854 int main (int argc,char ** argv)
863 char listavailable = 0;
864 processargs(argc, argv);
866 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
867 && !extractmp3 && !extractsoundids && !extractfontids)
872 fprintf(stderr, "You must supply a filename.\n");
875 initLog(0,-1,0,0,-1, verbose);
877 f = open(filename,O_RDONLY);
881 perror("Couldn't open file: ");
884 if (swf_ReadSWF(f,&swf) < 0)
886 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
905 tagused = (char*)malloc(tagnum);
906 memset(tagused, 0, tagnum);
907 memset(used, 0, 65536);
908 memset(depths, 0, 65536);
913 if(swf_isAllowedSpriteTag(tag)) {
915 if(extractframes && is_in_range(frame, extractframes)) {
917 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
918 depths[swf_GetDepth(tag)] = 1;
920 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
921 int depth = swf_GetDepth(tag);
924 depths[swf_GetDepth(tag)] = 0;
927 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
928 (depths[swf_GetDepth(tag)]) && hollow) {
930 depths[swf_GetDepth(tag)] = 0;
934 enumerateIDs(tag, idcallback);
940 if(tag->id == ST_SOUNDSTREAMHEAD ||
941 tag->id == ST_SOUNDSTREAMHEAD2 ||
942 tag->id == ST_SOUNDSTREAMBLOCK) {
944 handlesoundstream(tag);
947 if(tag->id == ST_JPEGTABLES)
948 handlejpegtables(tag);
950 if(swf_isDefiningTag(tag)) {
951 int id = swf_GetDefineID(tag);
953 if(extractids && is_in_range(id, extractids)) {
957 if(extractfontids && is_in_range(id, extractfontids)) {
958 handlefont(&swf, tag);
960 if(extractjpegids && is_in_range(id, extractjpegids)) {
963 if(extractsoundids && is_in_range(id, extractsoundids)) {
964 handledefinesound(tag);
966 #ifdef _ZLIB_INCLUDED_
967 if(extractpngids && is_in_range(id, extractpngids)) {
972 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
973 mainr = tag->data[0];
974 maing = tag->data[1];
975 mainb = tag->data[2];
977 else if(tag->id == ST_PLACEOBJECT2) {
978 char*name = swf_GetName(tag);
979 if(name && extractname && !strcmp(name, extractname)) {
980 int id = swf_GetPlaceID(tag);
984 depths[swf_GetDepth(tag)] = 1;
988 else if(tag->id == ST_SHOWFRAME) {
996 if(tag->id == ST_DEFINESPRITE) {
997 while(tag->id != ST_END) {
1006 extractTag(&swf, destfilename);