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;
33 char* extractname = 0;
39 struct options_t options[] =
55 int args_callback_option(char*name,char*val)
57 if(!strcmp(name, "V")) {
58 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
61 else if(!strcmp(name, "o")) {
65 else if(!strcmp(name, "i")) {
69 fprintf(stderr, "You can only supply either name or id\n");
74 else if(!strcmp(name, "n")) {
78 fprintf(stderr, "You can only supply either name or id\n");
83 else if(!strcmp(name, "v")) {
87 else if(!strcmp(name, "m")) {
92 else if(!strcmp(name, "j")) {
94 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
101 #ifdef _ZLIB_INCLUDED_
102 else if(!strcmp(name, "p")) {
104 fprintf(stderr, "Only one --pngs argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
112 else if(!strcmp(name, "f")) {
117 else if(!strcmp(name, "w")) {
122 printf("Unknown option: -%s\n", name);
128 int args_callback_longoption(char*name,char*val)
130 return args_long2shortoption(options, name, val);
132 void args_callback_usage(char*name)
134 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
135 printf("\t-v , --verbose\t\t\t Be more verbose\n");
136 printf("\t-o , --output filename\t\t set output filename\n");
137 printf("\t-n , --name name\t\t instance name of the object to extract\n");
138 printf("\t-i , --id IDs\t\t\t ID of the object to extract\n");
139 printf("\t-j , --jpeg IDs\t\t\t IDs of the JPEG pictures to extract\n");
140 #ifdef _ZLIB_INCLUDED_
141 printf("\t-p , --pngs IDs\t\t\t IDs of the PNG pictures to extract\n");
143 printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
144 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
145 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n");
146 printf("\t \t\t\t (use with -f)\n");
147 printf("\t-V , --version\t\t\t Print program version and exit\n");
149 int args_callback_command(char*name,char*val)
152 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
159 U8 mainr,maing,mainb;
160 /* 1 = used, not expanded,
162 5 = wanted, not expanded
170 void idcallback(void*data)
172 if(!(used[GET16(data)]&1)) {
174 used[GET16(data)] |= 1;
178 void enumerateIDs(TAG*tag, void(*callback)(void*))
184 data = (U8*)malloc(len);
185 PUT16(data, (tag->id<<6)+63);
186 *(U8*)&data[2] = tag->len;
187 *(U8*)&data[3] = tag->len>>8;
188 *(U8*)&data[4] = tag->len>>16;
189 *(U8*)&data[5] = tag->len>>24;
190 memcpy(&data[6], tag->data, tag->len);
193 data = (U8*)malloc(len);
194 PUT16(data, (tag->id<<6)+tag->len);
195 memcpy(&data[2], tag->data, tag->len);
197 map_ids_mem(data, len, callback);
199 int num = swf_GetNumUsedIDs(tag);
200 int *ptr = malloc(sizeof(int)*num);
202 swf_GetUsedIDs(tag, ptr);
204 callback(&tag->data[ptr[t]]);
207 void extractTag(SWF*swf, char*filename)
218 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
220 newswf.fileVersion = swf->fileVersion;
221 newswf.frameRate = swf->frameRate;
222 newswf.movieSize = swf->movieSize;
224 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
225 desttag = newswf.firstTag;
229 swf_SetRGB(desttag,&rgb);
233 for(t=0;t<65536;t++) {
234 if(used[t] && !(used[t]&2)) {
235 if(tags[t]->id==ST_DEFINESPRITE) {
237 while(tag->id != ST_END)
239 enumerateIDs(tag, idcallback);
244 enumerateIDs(tags[t], idcallback);
251 srctag = swf->firstTag;
254 while(srctag && (srctag->id || sprite)) {
259 if(srctag->id == ST_END) {
262 if(srctag->id == ST_DEFINESPRITE)
264 if(swf_isDefiningTag(srctag)) {
265 int id = swf_GetDefineID(srctag);
269 if (((srctag->id == ST_PLACEOBJECT ||
270 srctag->id == ST_PLACEOBJECT2 ||
271 srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
272 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
279 if(srctag->id == ST_REMOVEOBJECT) {
280 if(!used[swf_GetPlaceID(srctag)])
285 TAG*ttag = (TAG*)malloc(sizeof(TAG));
286 desttag = swf_InsertTag(desttag, srctag->id);
287 desttag->len = desttag->memsize = srctag->len;
288 desttag->data = malloc(srctag->len);
289 memcpy(desttag->data, srctag->data, srctag->len);
294 srctag = srctag->next;
297 if(!extractframes && !hollow)
298 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
300 desttag = swf_InsertTag(desttag,ST_END);
302 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT, 0644);
303 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
306 swf_FreeTags(&newswf); // cleanup
309 void listObjects(SWF*swf)
315 char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Frames"};
316 printf("Objects in file %s:\n",filename);
324 (tag->id == ST_DEFINESHAPE ||
325 tag->id == ST_DEFINESHAPE2 ||
326 tag->id == ST_DEFINESHAPE3)) {
328 sprintf(text,"%d", swf_GetDefineID(tag));
331 if(tag->id == ST_DEFINESPRITE) {
334 sprintf(text,"%d", swf_GetDefineID(tag));
337 while(tag->id != ST_END)
341 if(t == 2 && (tag->id == ST_DEFINEBITS ||
342 tag->id == ST_DEFINEBITSJPEG2 ||
343 tag->id == ST_DEFINEBITSJPEG3)) {
345 sprintf(text,"%d", swf_GetDefineID(tag));
348 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
349 tag->id == ST_DEFINEBITSLOSSLESS2)) {
351 sprintf(text,"%d", swf_GetDefineID(tag));
355 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
357 sprintf(text,"%d", swf_GetDefineID(tag));
360 if(t == 5 && (tag->id == ST_SHOWFRAME)) {
362 sprintf(text,"%d", frame);
370 printf("%s: ", names[t]);
384 void handlejpegtables(TAG*tag)
386 if(tag->id == ST_JPEGTABLES) {
387 jpegtables = tag->data;
388 jpegtablessize = tag->len;
392 FILE* save_fopen(char* name, char* mode)
394 FILE*fi = fopen(name, mode);
396 fprintf(stderr, "Error: Couldn't open %s\n", name);
402 int findjpegboundary(U8*data, int len)
417 /* extract jpeg data out of a tag */
418 void handlejpeg(TAG*tag)
422 sprintf(name, "pic%d.jpeg", GET16(tag->data));
423 /* swf jpeg images have two streams, which both start with ff d8 and
424 end with ff d9. The following code handles sorting the middle
425 <ff d9 ff d8> bytes out, so that one stream remains */
426 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
427 fi = save_fopen(name, "wb");
428 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
429 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
432 if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
434 int pos = findjpegboundary(&tag->data[2], tag->len-2);
438 fi = save_fopen(name, "wb");
439 fwrite(&tag->data[2], pos-2, 1, fi);
440 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
443 if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
444 U32 end = GET32(&tag->data[2])+6;
445 int pos = findjpegboundary(&tag->data[6], tag->len-6);
449 fi = save_fopen(name, "wb");
450 fwrite(&tag->data[6], pos-6, 1, fi);
451 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
456 #ifdef _ZLIB_INCLUDED_
459 static U32*crc32_table = 0;
460 static void make_crc32_table(void)
465 crc32_table = (U32*)malloc(1024);
467 for (t = 0; t < 256; t++) {
470 for (s = 0; s < 8; s++) {
471 c = (0xedb88320L*(c&1)) ^ (c >> 1);
476 static void png_write_byte(FILE*fi, U8 byte)
478 fwrite(&byte,1,1,fi);
479 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
481 static void png_start_chunk(FILE*fi, char*type, int len)
483 U8 mytype[4]={0,0,0,0};
484 U32 mylen = REVERSESWAP32(len);
485 memcpy(mytype,type,strlen(type));
486 fwrite(&mylen, 4, 1, fi);
488 png_write_byte(fi,mytype[0]);
489 png_write_byte(fi,mytype[1]);
490 png_write_byte(fi,mytype[2]);
491 png_write_byte(fi,mytype[3]);
493 static void png_write_bytes(FILE*fi, U8*bytes, int len)
497 png_write_byte(fi,bytes[t]);
499 static void png_write_dword(FILE*fi, U32 dword)
501 png_write_byte(fi,dword>>24);
502 png_write_byte(fi,dword>>16);
503 png_write_byte(fi,dword>>8);
504 png_write_byte(fi,dword);
506 static void png_end_chunk(FILE*fi)
508 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
513 /* extract a lossless image (png) out of a tag
514 This routine was originally meant to be a one-pager. I just
515 didn't know png is _that_ much fun. :) -mk
517 void handlelossless(TAG*tag)
534 U8 head[] = {137,80,78,71,13,10,26,10};
536 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
544 if(tag->id != ST_DEFINEBITSLOSSLESS &&
545 tag->id != ST_DEFINEBITSLOSSLESS2)
549 format = swf_GetU8(tag);
550 if(format == 3) bpp = 8;
551 if(format == 4) bpp = 16;
552 if(format == 5) bpp = 32;
553 if(format!=3 && format!=5) {
555 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
557 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
560 width = swf_GetU16(tag);
561 height = swf_GetU16(tag);
562 if(format == 3) cols = swf_GetU8(tag) + 1;
563 // this is what format means according to the flash specification. (which is
565 // if(format == 4) cols = swf_GetU16(tag) + 1;
566 // if(format == 5) cols = swf_GetU32(tag) + 1;
569 logf("<verbose> Width %d", width);
570 logf("<verbose> Height %d", height);
571 logf("<verbose> Format %d", format);
572 logf("<verbose> Cols %d", cols);
573 logf("<verbose> Bpp %d", bpp);
575 datalen = (width*height*bpp/8+cols*8);
580 data = malloc(datalen);
581 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
582 } while(error == Z_BUF_ERROR);
584 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
587 logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
590 data2 = malloc(datalen2);
591 palette = (RGBA*)malloc(cols*sizeof(RGBA));
593 for(t=0;t<cols;t++) {
594 palette[t].r = data[pos++];
595 palette[t].g = data[pos++];
596 palette[t].b = data[pos++];
598 palette[t].a = data[pos++];
602 sprintf(name, "pic%d.png", id);
603 fi = save_fopen(name, "wb");
604 fwrite(head,sizeof(head),1,fi);
606 png_start_chunk(fi, "IHDR", 13);
607 png_write_dword(fi,width);
608 png_write_dword(fi,height);
609 png_write_byte(fi,8);
611 png_write_byte(fi,3); //indexed
613 png_write_byte(fi,2); //rgb
616 png_write_byte(fi,0); //compression mode
617 png_write_byte(fi,0); //filter mode
618 png_write_byte(fi,0); //interlace mode
622 png_start_chunk(fi, "PLTE", 768);
624 png_write_byte(fi,palette[t].r);
625 png_write_byte(fi,palette[t].g);
626 png_write_byte(fi,palette[t].b);
633 int srcwidth = width * (bpp/8);
634 datalen3 = width*height*4;
635 data3 = (U8*)malloc(datalen3);
636 for(y=0;y<height;y++)
638 data3[pos2++]=0; //filter type
640 // 32 bit to 24 bit "conversion"
641 for(x=0;x<width;x++) {
642 data3[pos2++]=data[pos+1];
643 data3[pos2++]=data[pos+2];
644 data3[pos2++]=data[pos+3];
645 pos+=4; //ignore padding byte
649 for(x=0;x<srcwidth;x++)
650 data3[pos2++]=data[pos++];
653 pos+=((srcwidth+3)&~3)-srcwidth; //align
658 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
659 fprintf(stderr, "zlib error in pic %d\n", id);
662 logf("<verbose> Compressed data is %d bytes", datalen2);
663 png_start_chunk(fi, "IDAT", datalen2);
664 png_write_bytes(fi,data2,datalen2);
666 png_start_chunk(fi, "IEND", 0);
676 void handlesoundstream(TAG*tag)
678 char*filename = "output.mp3";
680 filename = destfilename;
681 if(!strcmp(filename,"output.swf"))
682 filename = "output.mp3";
685 case ST_SOUNDSTREAMHEAD:
686 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
687 mp3file = fopen(filename, "wb");
688 logf("<notice> Writing mp3 data to %s",filename);
691 logf("<error> Soundstream is not mp3");
693 case ST_SOUNDSTREAMHEAD2:
694 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
695 mp3file = fopen("mainstream.mp3", "wb");
696 logf("<notice> Writing mp3 data to %s",filename);
699 logf("<error> Soundstream is not mp3 (2)");
701 case ST_SOUNDSTREAMBLOCK:
703 fwrite(&tag->data[4],tag->len-4,1,mp3file);
708 int main (int argc,char ** argv)
717 char listavailable = 0;
718 processargs(argc, argv);
720 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
726 fprintf(stderr, "You must supply a filename.\n");
729 initLog(0,-1,0,0,-1, verbose);
731 f = open(filename,O_RDONLY);
735 perror("Couldn't open file: ");
738 if (swf_ReadSWF(f,&swf) < 0)
740 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
759 tagused = (char*)malloc(tagnum);
760 memset(tagused, 0, tagnum);
761 memset(used, 0, 65536);
762 memset(depths, 0, 65536);
767 if(swf_isAllowedSpriteTag(tag)) {
769 if(extractframes && is_in_range(frame, extractframes)) {
771 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
772 depths[swf_GetDepth(tag)] = 1;
774 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
775 int depth = swf_GetDepth(tag);
778 depths[swf_GetDepth(tag)] = 0;
781 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
782 (depths[swf_GetDepth(tag)]) && hollow) {
784 depths[swf_GetDepth(tag)] = 0;
788 enumerateIDs(tag, idcallback);
794 if(tag->id == ST_SOUNDSTREAMHEAD ||
795 tag->id == ST_SOUNDSTREAMHEAD2 ||
796 tag->id == ST_SOUNDSTREAMBLOCK) {
797 handlesoundstream(tag);
800 if(tag->id == ST_JPEGTABLES)
801 handlejpegtables(tag);
803 if(swf_isDefiningTag(tag)) {
804 int id = swf_GetDefineID(tag);
806 if(extractids && is_in_range(id, extractids)) {
810 if(extractjpegids && is_in_range(id, extractjpegids)) {
813 #ifdef _ZLIB_INCLUDED_
814 if(extractpngids && is_in_range(id, extractpngids)) {
819 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
820 mainr = tag->data[0];
821 maing = tag->data[1];
822 mainb = tag->data[2];
824 else if(tag->id == ST_PLACEOBJECT2) {
825 char*name = swf_GetName(tag);
826 if(name && extractname && !strcmp(name, extractname)) {
827 int id = swf_GetPlaceID(tag);
831 depths[swf_GetDepth(tag)] = 1;
834 else if(tag->id == ST_SHOWFRAME) {
842 if(tag->id == ST_DEFINESPRITE) {
843 while(tag->id != ST_END) {
852 extractTag(&swf, destfilename);