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* extractpngids = 0;
31 char* extractsoundids = 0;
34 char* extractname = 0;
40 struct options_t options[] =
57 int args_callback_option(char*name,char*val)
59 if(!strcmp(name, "V")) {
60 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
63 else if(!strcmp(name, "o")) {
67 else if(!strcmp(name, "i")) {
71 fprintf(stderr, "You can only supply either name or id\n");
76 else if(!strcmp(name, "n")) {
80 fprintf(stderr, "You can only supply either name or id\n");
85 else if(!strcmp(name, "v")) {
89 else if(!strcmp(name, "m")) {
94 else if(!strcmp(name, "j")) {
96 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
100 extractjpegids = val;
103 else if(!strcmp(name, "s")) {
104 if(extractsoundids) {
105 fprintf(stderr, "Only one --sound argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
109 extractsoundids = val;
112 #ifdef _ZLIB_INCLUDED_
113 else if(!strcmp(name, "p")) {
115 fprintf(stderr, "Only one --pngs argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
123 else if(!strcmp(name, "f")) {
128 else if(!strcmp(name, "w")) {
133 printf("Unknown option: -%s\n", name);
139 int args_callback_longoption(char*name,char*val)
141 return args_long2shortoption(options, name, val);
143 void args_callback_usage(char*name)
145 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
146 printf("\t-v , --verbose\t\t\t Be more verbose\n");
147 printf("\t-o , --output filename\t\t set output filename\n");
148 printf("\t-V , --version\t\t\t Print program version and exit\n\n");
149 printf("SWF Subelement extraction:\n");
150 printf("\t-n , --name name\t\t instance name of the object (SWF Define) to extract\n");
151 printf("\t-i , --id ID\t\t\t ID of the object (SWF Define) to extract\n");
152 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
153 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n");
154 printf("\t \t\t\t (use with -f)\n");
155 printf("Picture extraction:\n");
156 printf("\t-j , --jpeg ID\t\t\t Extract JPEG picture(s)\n");
157 #ifdef _ZLIB_INCLUDED_
158 printf("\t-p , --pngs ID\t\t\t Extract PNG picture(s)\n");
161 printf("Sound extraction:\n");
162 printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
163 printf("\t-s , --sound ID\t\t\t Extract Sound(s)\n");
165 int args_callback_command(char*name,char*val)
168 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
175 U8 mainr,maing,mainb;
176 /* 1 = used, not expanded,
178 5 = wanted, not expanded
186 void idcallback(void*data)
188 if(!(used[GET16(data)]&1)) {
190 used[GET16(data)] |= 1;
194 void enumerateIDs(TAG*tag, void(*callback)(void*))
200 data = (U8*)malloc(len);
201 PUT16(data, (tag->id<<6)+63);
202 *(U8*)&data[2] = tag->len;
203 *(U8*)&data[3] = tag->len>>8;
204 *(U8*)&data[4] = tag->len>>16;
205 *(U8*)&data[5] = tag->len>>24;
206 memcpy(&data[6], tag->data, tag->len);
209 data = (U8*)malloc(len);
210 PUT16(data, (tag->id<<6)+tag->len);
211 memcpy(&data[2], tag->data, tag->len);
213 map_ids_mem(data, len, callback);
215 int num = swf_GetNumUsedIDs(tag);
216 int *ptr = malloc(sizeof(int)*num);
218 swf_GetUsedIDs(tag, ptr);
220 callback(&tag->data[ptr[t]]);
223 void extractTag(SWF*swf, char*filename)
234 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
236 newswf.fileVersion = swf->fileVersion;
237 newswf.frameRate = swf->frameRate;
238 newswf.movieSize = swf->movieSize;
240 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
241 desttag = newswf.firstTag;
245 swf_SetRGB(desttag,&rgb);
249 for(t=0;t<65536;t++) {
250 if(used[t] && !(used[t]&2)) {
251 if(tags[t]->id==ST_DEFINESPRITE) {
253 while(tag->id != ST_END)
255 enumerateIDs(tag, idcallback);
260 enumerateIDs(tags[t], idcallback);
267 srctag = swf->firstTag;
270 while(srctag && (srctag->id || sprite)) {
275 if(srctag->id == ST_END) {
278 if(srctag->id == ST_DEFINESPRITE)
280 if(swf_isDefiningTag(srctag)) {
281 int id = swf_GetDefineID(srctag);
285 if (((srctag->id == ST_PLACEOBJECT ||
286 srctag->id == ST_PLACEOBJECT2 ||
287 srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
288 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
295 if(srctag->id == ST_REMOVEOBJECT) {
296 if(!used[swf_GetPlaceID(srctag)])
301 TAG*ttag = (TAG*)malloc(sizeof(TAG));
302 desttag = swf_InsertTag(desttag, srctag->id);
303 desttag->len = desttag->memsize = srctag->len;
304 desttag->data = malloc(srctag->len);
305 memcpy(desttag->data, srctag->data, srctag->len);
310 srctag = srctag->next;
313 if(!extractframes && !hollow)
314 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
316 desttag = swf_InsertTag(desttag,ST_END);
318 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT, 0644);
319 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
322 swf_FreeTags(&newswf); // cleanup
325 void listObjects(SWF*swf)
331 char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Frames"};
332 printf("Objects in file %s:\n",filename);
340 (tag->id == ST_DEFINESHAPE ||
341 tag->id == ST_DEFINESHAPE2 ||
342 tag->id == ST_DEFINESHAPE3)) {
344 sprintf(text,"%d", swf_GetDefineID(tag));
347 if(tag->id == ST_DEFINESPRITE) {
350 sprintf(text,"%d", swf_GetDefineID(tag));
353 while(tag->id != ST_END)
357 if(t == 2 && (tag->id == ST_DEFINEBITS ||
358 tag->id == ST_DEFINEBITSJPEG2 ||
359 tag->id == ST_DEFINEBITSJPEG3)) {
361 sprintf(text,"%d", swf_GetDefineID(tag));
364 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
365 tag->id == ST_DEFINEBITSLOSSLESS2)) {
367 sprintf(text,"%d", swf_GetDefineID(tag));
371 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
373 sprintf(text,"%d", swf_GetDefineID(tag));
376 if(t == 5 && (tag->id == ST_SHOWFRAME)) {
378 sprintf(text,"%d", frame);
386 printf("%s: ", names[t]);
400 void handlejpegtables(TAG*tag)
402 if(tag->id == ST_JPEGTABLES) {
403 jpegtables = tag->data;
404 jpegtablessize = tag->len;
408 FILE* save_fopen(char* name, char* mode)
410 FILE*fi = fopen(name, mode);
412 fprintf(stderr, "Error: Couldn't open %s\n", name);
418 int findjpegboundary(U8*data, int len)
433 /* extract jpeg data out of a tag */
434 void handlejpeg(TAG*tag)
438 sprintf(name, "pic%d.jpeg", GET16(tag->data));
439 /* swf jpeg images have two streams, which both start with ff d8 and
440 end with ff d9. The following code handles sorting the middle
441 <ff d9 ff d8> bytes out, so that one stream remains */
442 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
443 fi = save_fopen(name, "wb");
444 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
445 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
448 else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
450 int pos = findjpegboundary(&tag->data[2], tag->len-2);
454 fi = save_fopen(name, "wb");
455 fwrite(&tag->data[2], pos-2, 1, fi);
456 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
459 else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
460 U32 end = GET32(&tag->data[2])+6;
461 int pos = findjpegboundary(&tag->data[6], tag->len-6);
465 fi = save_fopen(name, "wb");
466 fwrite(&tag->data[6], pos-6, 1, fi);
467 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
471 int id = GET16(tag->data);
472 fprintf(stderr, "Object %d is not a JPEG picture!\n",id);
477 #ifdef _ZLIB_INCLUDED_
480 static U32*crc32_table = 0;
481 static void make_crc32_table(void)
486 crc32_table = (U32*)malloc(1024);
488 for (t = 0; t < 256; t++) {
491 for (s = 0; s < 8; s++) {
492 c = (0xedb88320L*(c&1)) ^ (c >> 1);
497 static void png_write_byte(FILE*fi, U8 byte)
499 fwrite(&byte,1,1,fi);
500 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
502 static void png_start_chunk(FILE*fi, char*type, int len)
504 U8 mytype[4]={0,0,0,0};
505 U32 mylen = REVERSESWAP32(len);
506 memcpy(mytype,type,strlen(type));
507 fwrite(&mylen, 4, 1, fi);
509 png_write_byte(fi,mytype[0]);
510 png_write_byte(fi,mytype[1]);
511 png_write_byte(fi,mytype[2]);
512 png_write_byte(fi,mytype[3]);
514 static void png_write_bytes(FILE*fi, U8*bytes, int len)
518 png_write_byte(fi,bytes[t]);
520 static void png_write_dword(FILE*fi, U32 dword)
522 png_write_byte(fi,dword>>24);
523 png_write_byte(fi,dword>>16);
524 png_write_byte(fi,dword>>8);
525 png_write_byte(fi,dword);
527 static void png_end_chunk(FILE*fi)
529 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
534 /* extract a lossless image (png) out of a tag
535 This routine was originally meant to be a one-pager. I just
536 didn't know png is _that_ much fun. :) -mk
538 void handlelossless(TAG*tag)
555 U8 head[] = {137,80,78,71,13,10,26,10};
557 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
565 if(tag->id != ST_DEFINEBITSLOSSLESS &&
566 tag->id != ST_DEFINEBITSLOSSLESS2) {
567 int id = GET16(tag->data);
568 fprintf(stderr, "Object %d is not a PNG picture!\n",id);
573 format = swf_GetU8(tag);
574 if(format == 3) bpp = 8;
575 if(format == 4) bpp = 16;
576 if(format == 5) bpp = 32;
577 if(format!=3 && format!=5) {
579 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
581 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
584 width = swf_GetU16(tag);
585 height = swf_GetU16(tag);
586 if(format == 3) cols = swf_GetU8(tag) + 1;
587 // this is what format means according to the flash specification. (which is
589 // if(format == 4) cols = swf_GetU16(tag) + 1;
590 // if(format == 5) cols = swf_GetU32(tag) + 1;
593 logf("<verbose> Width %d", width);
594 logf("<verbose> Height %d", height);
595 logf("<verbose> Format %d", format);
596 logf("<verbose> Cols %d", cols);
597 logf("<verbose> Bpp %d", bpp);
599 datalen = (width*height*bpp/8+cols*8);
604 data = malloc(datalen);
605 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
606 } while(error == Z_BUF_ERROR);
608 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
611 logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
614 data2 = malloc(datalen2);
615 palette = (RGBA*)malloc(cols*sizeof(RGBA));
617 for(t=0;t<cols;t++) {
618 palette[t].r = data[pos++];
619 palette[t].g = data[pos++];
620 palette[t].b = data[pos++];
622 palette[t].a = data[pos++];
626 sprintf(name, "pic%d.png", id);
627 fi = save_fopen(name, "wb");
628 fwrite(head,sizeof(head),1,fi);
630 png_start_chunk(fi, "IHDR", 13);
631 png_write_dword(fi,width);
632 png_write_dword(fi,height);
633 png_write_byte(fi,8);
635 png_write_byte(fi,3); //indexed
637 png_write_byte(fi,2); //rgb
640 png_write_byte(fi,0); //compression mode
641 png_write_byte(fi,0); //filter mode
642 png_write_byte(fi,0); //interlace mode
646 png_start_chunk(fi, "PLTE", 768);
648 png_write_byte(fi,palette[t].r);
649 png_write_byte(fi,palette[t].g);
650 png_write_byte(fi,palette[t].b);
657 int srcwidth = width * (bpp/8);
658 datalen3 = width*height*4;
659 data3 = (U8*)malloc(datalen3);
660 for(y=0;y<height;y++)
662 data3[pos2++]=0; //filter type
664 // 32 bit to 24 bit "conversion"
665 for(x=0;x<width;x++) {
666 data3[pos2++]=data[pos+1];
667 data3[pos2++]=data[pos+2];
668 data3[pos2++]=data[pos+3];
669 pos+=4; //ignore padding byte
673 for(x=0;x<srcwidth;x++)
674 data3[pos2++]=data[pos++];
677 pos+=((srcwidth+3)&~3)-srcwidth; //align
682 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
683 fprintf(stderr, "zlib error in pic %d\n", id);
686 logf("<verbose> Compressed data is %d bytes", datalen2);
687 png_start_chunk(fi, "IDAT", datalen2);
688 png_write_bytes(fi,data2,datalen2);
690 png_start_chunk(fi, "IEND", 0);
700 void handlesoundstream(TAG*tag)
702 char*filename = "output.mp3";
704 filename = destfilename;
705 if(!strcmp(filename,"output.swf"))
706 filename = "output.mp3";
709 case ST_SOUNDSTREAMHEAD:
710 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
711 mp3file = fopen(filename, "wb");
712 logf("<notice> Writing mp3 data to %s",filename);
715 logf("<error> Soundstream is not mp3");
717 case ST_SOUNDSTREAMHEAD2:
718 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
719 mp3file = fopen(filename, "wb");
720 logf("<notice> Writing mp3 data to %s",filename);
723 logf("<error> Soundstream is not mp3 (2)");
725 case ST_SOUNDSTREAMBLOCK:
727 fwrite(&tag->data[4],tag->len-4,1,mp3file);
732 void handledefinesound(TAG*tag)
740 id = swf_GetU16(tag); //id
741 sprintf(buf, "sound%d.mp3", id);
744 filename = destfilename;
745 if(!strcmp(filename,"output.swf"))
746 filename = "output.mp3";
748 flags = swf_GetU8(tag);
750 printf("Sorry, can only extract MP3 sounds. Sound %d is ADPCM or RAW.\n", id);
754 samples = swf_GetU32(tag);
756 swf_GetU16(tag); //(only for mp3) numsamples_seek
758 fi = save_fopen(filename, "wb");
759 fwrite(&tag->data[tag->pos], tag->len - tag->pos, 1, fi);
763 int main (int argc,char ** argv)
772 char listavailable = 0;
773 processargs(argc, argv);
775 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
776 && !extractmp3 && !extractsoundids)
781 fprintf(stderr, "You must supply a filename.\n");
784 initLog(0,-1,0,0,-1, verbose);
786 f = open(filename,O_RDONLY);
790 perror("Couldn't open file: ");
793 if (swf_ReadSWF(f,&swf) < 0)
795 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
814 tagused = (char*)malloc(tagnum);
815 memset(tagused, 0, tagnum);
816 memset(used, 0, 65536);
817 memset(depths, 0, 65536);
822 if(swf_isAllowedSpriteTag(tag)) {
824 if(extractframes && is_in_range(frame, extractframes)) {
826 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
827 depths[swf_GetDepth(tag)] = 1;
829 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
830 int depth = swf_GetDepth(tag);
833 depths[swf_GetDepth(tag)] = 0;
836 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
837 (depths[swf_GetDepth(tag)]) && hollow) {
839 depths[swf_GetDepth(tag)] = 0;
843 enumerateIDs(tag, idcallback);
849 if(tag->id == ST_SOUNDSTREAMHEAD ||
850 tag->id == ST_SOUNDSTREAMHEAD2 ||
851 tag->id == ST_SOUNDSTREAMBLOCK) {
853 handlesoundstream(tag);
856 if(tag->id == ST_JPEGTABLES)
857 handlejpegtables(tag);
859 if(swf_isDefiningTag(tag)) {
860 int id = swf_GetDefineID(tag);
862 if(extractids && is_in_range(id, extractids)) {
866 if(extractjpegids && is_in_range(id, extractjpegids)) {
869 if(extractsoundids && is_in_range(id, extractsoundids)) {
870 handledefinesound(tag);
872 #ifdef _ZLIB_INCLUDED_
873 if(extractpngids && is_in_range(id, extractpngids)) {
878 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
879 mainr = tag->data[0];
880 maing = tag->data[1];
881 mainb = tag->data[2];
883 else if(tag->id == ST_PLACEOBJECT2) {
884 char*name = swf_GetName(tag);
885 if(name && extractname && !strcmp(name, extractname)) {
886 int id = swf_GetPlaceID(tag);
890 depths[swf_GetDepth(tag)] = 1;
893 else if(tag->id == ST_SHOWFRAME) {
901 if(tag->id == ST_DEFINESPRITE) {
902 while(tag->id != ST_END) {
911 extractTag(&swf, destfilename);