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)
437 char*filename = name;
440 sprintf(name, "pic%d.jpg", GET16(tag->data));
442 filename = destfilename;
443 if(!strcmp(filename,"output.swf"))
444 filename = "output.jpg";
446 /* swf jpeg images have two streams, which both start with ff d8 and
447 end with ff d9. The following code handles sorting the middle
448 <ff d9 ff d8> bytes out, so that one stream remains */
449 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
450 fi = save_fopen(filename, "wb");
451 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
452 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
455 else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
457 int pos = findjpegboundary(&tag->data[2], tag->len-2);
461 fi = save_fopen(filename, "wb");
462 fwrite(&tag->data[2], pos-2, 1, fi);
463 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
466 else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
467 U32 end = GET32(&tag->data[2])+6;
468 int pos = findjpegboundary(&tag->data[6], tag->len-6);
472 fi = save_fopen(filename, "wb");
473 fwrite(&tag->data[6], pos-6, 1, fi);
474 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
478 int id = GET16(tag->data);
479 fprintf(stderr, "Object %d is not a JPEG picture!\n",id);
484 #ifdef _ZLIB_INCLUDED_
487 static U32*crc32_table = 0;
488 static void make_crc32_table(void)
493 crc32_table = (U32*)malloc(1024);
495 for (t = 0; t < 256; t++) {
498 for (s = 0; s < 8; s++) {
499 c = (0xedb88320L*(c&1)) ^ (c >> 1);
504 static void png_write_byte(FILE*fi, U8 byte)
506 fwrite(&byte,1,1,fi);
507 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
509 static void png_start_chunk(FILE*fi, char*type, int len)
511 U8 mytype[4]={0,0,0,0};
512 U32 mylen = REVERSESWAP32(len);
513 memcpy(mytype,type,strlen(type));
514 fwrite(&mylen, 4, 1, fi);
516 png_write_byte(fi,mytype[0]);
517 png_write_byte(fi,mytype[1]);
518 png_write_byte(fi,mytype[2]);
519 png_write_byte(fi,mytype[3]);
521 static void png_write_bytes(FILE*fi, U8*bytes, int len)
525 png_write_byte(fi,bytes[t]);
527 static void png_write_dword(FILE*fi, U32 dword)
529 png_write_byte(fi,dword>>24);
530 png_write_byte(fi,dword>>16);
531 png_write_byte(fi,dword>>8);
532 png_write_byte(fi,dword);
534 static void png_end_chunk(FILE*fi)
536 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
541 /* extract a lossless image (png) out of a tag
542 This routine was originally meant to be a one-pager. I just
543 didn't know png is _that_ much fun. :) -mk
545 void handlelossless(TAG*tag)
548 char*filename = name;
563 U8 head[] = {137,80,78,71,13,10,26,10};
565 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
573 if(tag->id != ST_DEFINEBITSLOSSLESS &&
574 tag->id != ST_DEFINEBITSLOSSLESS2) {
575 int id = GET16(tag->data);
576 fprintf(stderr, "Object %d is not a PNG picture!\n",id);
581 format = swf_GetU8(tag);
582 if(format == 3) bpp = 8;
583 if(format == 4) bpp = 16;
584 if(format == 5) bpp = 32;
585 if(format!=3 && format!=5) {
587 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
589 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
592 width = swf_GetU16(tag);
593 height = swf_GetU16(tag);
594 if(format == 3) cols = swf_GetU8(tag) + 1;
595 // this is what format means according to the flash specification. (which is
597 // if(format == 4) cols = swf_GetU16(tag) + 1;
598 // if(format == 5) cols = swf_GetU32(tag) + 1;
601 logf("<verbose> Width %d", width);
602 logf("<verbose> Height %d", height);
603 logf("<verbose> Format %d", format);
604 logf("<verbose> Cols %d", cols);
605 logf("<verbose> Bpp %d", bpp);
607 datalen = (width*height*bpp/8+cols*8);
612 data = malloc(datalen);
613 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
614 } while(error == Z_BUF_ERROR);
616 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
619 logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
622 data2 = malloc(datalen2);
623 palette = (RGBA*)malloc(cols*sizeof(RGBA));
625 for(t=0;t<cols;t++) {
626 palette[t].r = data[pos++];
627 palette[t].g = data[pos++];
628 palette[t].b = data[pos++];
630 palette[t].a = data[pos++];
634 sprintf(name, "pic%d.png", id);
636 filename = destfilename;
637 if(!strcmp(filename,"output.swf"))
638 filename = "output.png";
640 fi = save_fopen(filename, "wb");
641 fwrite(head,sizeof(head),1,fi);
643 png_start_chunk(fi, "IHDR", 13);
644 png_write_dword(fi,width);
645 png_write_dword(fi,height);
646 png_write_byte(fi,8);
648 png_write_byte(fi,3); //indexed
650 png_write_byte(fi,2); //rgb
653 png_write_byte(fi,0); //compression mode
654 png_write_byte(fi,0); //filter mode
655 png_write_byte(fi,0); //interlace mode
659 png_start_chunk(fi, "PLTE", 768);
661 png_write_byte(fi,palette[t].r);
662 png_write_byte(fi,palette[t].g);
663 png_write_byte(fi,palette[t].b);
670 int srcwidth = width * (bpp/8);
671 datalen3 = width*height*4;
672 data3 = (U8*)malloc(datalen3);
673 for(y=0;y<height;y++)
675 data3[pos2++]=0; //filter type
677 // 32 bit to 24 bit "conversion"
678 for(x=0;x<width;x++) {
679 data3[pos2++]=data[pos+1];
680 data3[pos2++]=data[pos+2];
681 data3[pos2++]=data[pos+3];
682 pos+=4; //ignore padding byte
686 for(x=0;x<srcwidth;x++)
687 data3[pos2++]=data[pos++];
690 pos+=((srcwidth+3)&~3)-srcwidth; //align
695 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
696 fprintf(stderr, "zlib error in pic %d\n", id);
699 logf("<verbose> Compressed data is %d bytes", datalen2);
700 png_start_chunk(fi, "IDAT", datalen2);
701 png_write_bytes(fi,data2,datalen2);
703 png_start_chunk(fi, "IEND", 0);
713 void handlesoundstream(TAG*tag)
715 char*filename = "output.mp3";
717 filename = destfilename;
718 if(!strcmp(filename,"output.swf"))
719 filename = "output.mp3";
722 case ST_SOUNDSTREAMHEAD:
723 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
724 mp3file = fopen(filename, "wb");
725 logf("<notice> Writing mp3 data to %s",filename);
728 logf("<error> Soundstream is not mp3");
730 case ST_SOUNDSTREAMHEAD2:
731 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
732 mp3file = fopen(filename, "wb");
733 logf("<notice> Writing mp3 data to %s",filename);
736 logf("<error> Soundstream is not mp3 (2)");
738 case ST_SOUNDSTREAMBLOCK:
740 fwrite(&tag->data[4],tag->len-4,1,mp3file);
745 void handledefinesound(TAG*tag)
753 id = swf_GetU16(tag); //id
754 sprintf(buf, "sound%d.mp3", id);
757 filename = destfilename;
758 if(!strcmp(filename,"output.swf"))
759 filename = "output.mp3";
761 flags = swf_GetU8(tag);
763 printf("Sorry, can only extract MP3 sounds. Sound %d is ADPCM or RAW.\n", id);
767 samples = swf_GetU32(tag);
769 swf_GetU16(tag); //(only for mp3) numsamples_seek
771 fi = save_fopen(filename, "wb");
772 fwrite(&tag->data[tag->pos], tag->len - tag->pos, 1, fi);
776 int main (int argc,char ** argv)
785 char listavailable = 0;
786 processargs(argc, argv);
788 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
789 && !extractmp3 && !extractsoundids)
794 fprintf(stderr, "You must supply a filename.\n");
797 initLog(0,-1,0,0,-1, verbose);
799 f = open(filename,O_RDONLY);
803 perror("Couldn't open file: ");
806 if (swf_ReadSWF(f,&swf) < 0)
808 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
827 tagused = (char*)malloc(tagnum);
828 memset(tagused, 0, tagnum);
829 memset(used, 0, 65536);
830 memset(depths, 0, 65536);
835 if(swf_isAllowedSpriteTag(tag)) {
837 if(extractframes && is_in_range(frame, extractframes)) {
839 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
840 depths[swf_GetDepth(tag)] = 1;
842 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
843 int depth = swf_GetDepth(tag);
846 depths[swf_GetDepth(tag)] = 0;
849 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
850 (depths[swf_GetDepth(tag)]) && hollow) {
852 depths[swf_GetDepth(tag)] = 0;
856 enumerateIDs(tag, idcallback);
862 if(tag->id == ST_SOUNDSTREAMHEAD ||
863 tag->id == ST_SOUNDSTREAMHEAD2 ||
864 tag->id == ST_SOUNDSTREAMBLOCK) {
866 handlesoundstream(tag);
869 if(tag->id == ST_JPEGTABLES)
870 handlejpegtables(tag);
872 if(swf_isDefiningTag(tag)) {
873 int id = swf_GetDefineID(tag);
875 if(extractids && is_in_range(id, extractids)) {
879 if(extractjpegids && is_in_range(id, extractjpegids)) {
882 if(extractsoundids && is_in_range(id, extractsoundids)) {
883 handledefinesound(tag);
885 #ifdef _ZLIB_INCLUDED_
886 if(extractpngids && is_in_range(id, extractpngids)) {
891 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
892 mainr = tag->data[0];
893 maing = tag->data[1];
894 mainb = tag->data[2];
896 else if(tag->id == ST_PLACEOBJECT2) {
897 char*name = swf_GetName(tag);
898 if(name && extractname && !strcmp(name, extractname)) {
899 int id = swf_GetPlaceID(tag);
903 depths[swf_GetDepth(tag)] = 1;
906 else if(tag->id == ST_SHOWFRAME) {
914 if(tag->id == ST_DEFINESPRITE) {
915 while(tag->id != ST_END) {
924 extractTag(&swf, destfilename);