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"
20 #define _ZLIB_INCLUDED_
25 char * destfilename = "output.swf";
29 char* extractframes = 0;
30 char* extractjpegids = 0;
31 char* extractpngids = 0;
34 char* extractname = 0;
40 struct options_t options[] =
56 int args_callback_option(char*name,char*val)
58 if(!strcmp(name, "V")) {
59 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
62 else if(!strcmp(name, "o")) {
66 else if(!strcmp(name, "i")) {
70 fprintf(stderr, "You can only supply either name or id\n");
75 else if(!strcmp(name, "n")) {
79 fprintf(stderr, "You can only supply either name or id\n");
84 else if(!strcmp(name, "v")) {
88 else if(!strcmp(name, "m")) {
93 else if(!strcmp(name, "j")) {
95 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
102 #ifdef _ZLIB_INCLUDED_
103 else if(!strcmp(name, "p")) {
105 fprintf(stderr, "Only one --pngs argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
113 else if(!strcmp(name, "f")) {
118 else if(!strcmp(name, "w")) {
123 printf("Unknown option: -%s\n", name);
129 int args_callback_longoption(char*name,char*val)
131 return args_long2shortoption(options, name, val);
133 void args_callback_usage(char*name)
135 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
136 printf("\t-v , --verbose\t\t\t Be more verbose\n");
137 printf("\t-o , --output filename\t\t set output filename\n");
138 printf("\t-n , --name name\t\t instance name of the object to extract\n");
139 printf("\t-i , --id IDs\t\t\t ID of the object to extract\n");
140 printf("\t-j , --jpeg IDs\t\t\t IDs of the JPEG pictures to extract\n");
141 #ifdef _ZLIB_INCLUDED_
142 printf("\t-p , --pngs IDs\t\t\t IDs of the PNG pictures to extract\n");
144 printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
145 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
146 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n");
147 printf("\t \t\t\t (use with -f)\n");
148 printf("\t-V , --version\t\t\t Print program version and exit\n");
150 int args_callback_command(char*name,char*val)
153 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
160 U8 mainr,maing,mainb;
161 /* 1 = used, not expanded,
163 5 = wanted, not expanded
171 void idcallback(void*data)
173 if(!(used[GET16(data)]&1)) {
175 used[GET16(data)] |= 1;
179 void enumerateIDs(TAG*tag, void(*callback)(void*))
185 data = (U8*)malloc(len);
186 PUT16(data, (tag->id<<6)+63);
187 *(U8*)&data[2] = tag->len;
188 *(U8*)&data[3] = tag->len>>8;
189 *(U8*)&data[4] = tag->len>>16;
190 *(U8*)&data[5] = tag->len>>24;
191 memcpy(&data[6], tag->data, tag->len);
194 data = (U8*)malloc(len);
195 PUT16(data, (tag->id<<6)+tag->len);
196 memcpy(&data[2], tag->data, tag->len);
198 map_ids_mem(data, len, callback);
201 void extractTag(SWF*swf, char*filename)
212 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
214 newswf.fileVersion = swf->fileVersion;
215 newswf.frameRate = swf->frameRate;
216 newswf.movieSize = swf->movieSize;
218 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
219 desttag = newswf.firstTag;
223 swf_SetRGB(desttag,&rgb);
227 for(t=0;t<65536;t++) {
228 if(used[t] && !(used[t]&2)) {
229 if(tags[t]->id==ST_DEFINESPRITE) {
231 while(tag->id != ST_END)
233 enumerateIDs(tag, idcallback);
238 enumerateIDs(tags[t], idcallback);
245 srctag = swf->firstTag;
248 while(srctag && (srctag->id || sprite)) {
253 if(srctag->id == ST_END) {
256 if(srctag->id == ST_DEFINESPRITE)
258 if(swf_isDefiningTag(srctag)) {
259 int id = swf_GetDefineID(srctag);
263 if (((srctag->id == ST_PLACEOBJECT ||
264 srctag->id == ST_PLACEOBJECT2 ||
265 srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
266 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
273 if(srctag->id == ST_REMOVEOBJECT) {
274 if(!used[swf_GetPlaceID(srctag)])
279 TAG*ttag = (TAG*)malloc(sizeof(TAG));
280 desttag = swf_InsertTag(desttag, srctag->id);
281 desttag->len = desttag->memsize = srctag->len;
282 desttag->data = malloc(srctag->len);
283 memcpy(desttag->data, srctag->data, srctag->len);
288 srctag = srctag->next;
291 if(!extractframes && !hollow)
292 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
294 desttag = swf_InsertTag(desttag,ST_END);
296 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT, 0644);
297 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
300 swf_FreeTags(&newswf); // cleanup
303 void listObjects(SWF*swf)
309 char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Frames"};
310 printf("Objects in file %s:\n",filename);
318 (tag->id == ST_DEFINESHAPE ||
319 tag->id == ST_DEFINESHAPE2 ||
320 tag->id == ST_DEFINESHAPE3)) {
322 sprintf(text,"%d", swf_GetDefineID(tag));
325 if(tag->id == ST_DEFINESPRITE) {
328 sprintf(text,"%d", swf_GetDefineID(tag));
331 while(tag->id != ST_END)
335 if(t == 2 && (tag->id == ST_DEFINEBITS ||
336 tag->id == ST_DEFINEBITSJPEG2 ||
337 tag->id == ST_DEFINEBITSJPEG3)) {
339 sprintf(text,"%d", swf_GetDefineID(tag));
342 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
343 tag->id == ST_DEFINEBITSLOSSLESS2)) {
345 sprintf(text,"%d", swf_GetDefineID(tag));
349 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
351 sprintf(text,"%d", swf_GetDefineID(tag));
354 if(t == 5 && (tag->id == ST_SHOWFRAME)) {
356 sprintf(text,"%d", frame);
364 printf("%s: ", names[t]);
378 void handlejpegtables(TAG*tag)
380 if(tag->id == ST_JPEGTABLES) {
381 jpegtables = tag->data;
382 jpegtablessize = tag->len;
386 FILE* save_fopen(char* name, char* mode)
388 FILE*fi = fopen(name, mode);
390 fprintf(stderr, "Error: Couldn't open %s\n", name);
396 int findjpegboundary(U8*data, int len)
411 /* extract jpeg data out of a tag */
412 void handlejpeg(TAG*tag)
416 sprintf(name, "pic%d.jpeg", GET16(tag->data));
417 /* swf jpeg images have two streams, which both start with ff d8 and
418 end with ff d9. The following code handles sorting the middle
419 <ff d9 ff d8> bytes out, so that one stream remains */
420 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
421 fi = save_fopen(name, "wb");
422 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
423 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
426 if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
428 int pos = findjpegboundary(&tag->data[2], tag->len-2);
432 fi = save_fopen(name, "wb");
433 fwrite(&tag->data[2], pos-2, 1, fi);
434 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
437 if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
438 U32 end = GET32(&tag->data[2])+6;
439 int pos = findjpegboundary(&tag->data[6], tag->len-6);
443 fi = save_fopen(name, "wb");
444 fwrite(&tag->data[6], pos-6, 1, fi);
445 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
450 #ifdef _ZLIB_INCLUDED_
453 static U32*crc32_table = 0;
454 static void make_crc32_table(void)
459 crc32_table = (U32*)malloc(1024);
461 for (t = 0; t < 256; t++) {
464 for (s = 0; s < 8; s++) {
465 c = (0xedb88320L*(c&1)) ^ (c >> 1);
470 static void png_write_byte(FILE*fi, U8 byte)
472 fwrite(&byte,1,1,fi);
473 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
475 static void png_start_chunk(FILE*fi, char*type, int len)
477 U8 mytype[4]={0,0,0,0};
478 U32 mylen = REVERSESWAP32(len);
479 memcpy(mytype,type,strlen(type));
480 fwrite(&mylen, 4, 1, fi);
482 png_write_byte(fi,mytype[0]);
483 png_write_byte(fi,mytype[1]);
484 png_write_byte(fi,mytype[2]);
485 png_write_byte(fi,mytype[3]);
487 static void png_write_bytes(FILE*fi, U8*bytes, int len)
491 png_write_byte(fi,bytes[t]);
493 static void png_write_dword(FILE*fi, U32 dword)
495 png_write_byte(fi,dword>>24);
496 png_write_byte(fi,dword>>16);
497 png_write_byte(fi,dword>>8);
498 png_write_byte(fi,dword);
500 static void png_end_chunk(FILE*fi)
502 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
507 /* extract a lossless image (png) out of a tag
508 This routine was originally meant to be a one-pager. I just
509 didn't know png is _that_ much fun. :) -mk
511 void handlelossless(TAG*tag)
528 U8 head[] = {137,80,78,71,13,10,26,10};
530 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
538 if(tag->id != ST_DEFINEBITSLOSSLESS &&
539 tag->id != ST_DEFINEBITSLOSSLESS2)
543 format = swf_GetU8(tag);
544 if(format == 3) bpp = 8;
545 if(format == 4) bpp = 16;
546 if(format == 5) bpp = 32;
547 if(format!=3 && format!=5) {
549 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
551 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
554 width = swf_GetU16(tag);
555 height = swf_GetU16(tag);
556 if(format == 3) cols = swf_GetU8(tag) + 1;
557 // this is what format means according to the flash specification. (which is
559 // if(format == 4) cols = swf_GetU16(tag) + 1;
560 // if(format == 5) cols = swf_GetU32(tag) + 1;
563 logf("<verbose> Width %d", width);
564 logf("<verbose> Height %d", height);
565 logf("<verbose> Format %d", format);
566 logf("<verbose> Cols %d", cols);
567 logf("<verbose> Bpp %d", bpp);
569 datalen = (width*height*bpp/8+cols*8);
574 data = malloc(datalen);
575 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
576 } while(error == Z_BUF_ERROR);
578 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
581 logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
584 data2 = malloc(datalen2);
585 palette = (RGBA*)malloc(cols*sizeof(RGBA));
587 for(t=0;t<cols;t++) {
588 palette[t].r = data[pos++];
589 palette[t].g = data[pos++];
590 palette[t].b = data[pos++];
592 palette[t].a = data[pos++];
596 sprintf(name, "pic%d.png", id);
597 fi = save_fopen(name, "wb");
598 fwrite(head,sizeof(head),1,fi);
600 png_start_chunk(fi, "IHDR", 13);
601 png_write_dword(fi,width);
602 png_write_dword(fi,height);
603 png_write_byte(fi,8);
605 png_write_byte(fi,3); //indexed
607 png_write_byte(fi,2); //rgb
610 png_write_byte(fi,0); //compression mode
611 png_write_byte(fi,0); //filter mode
612 png_write_byte(fi,0); //interlace mode
616 png_start_chunk(fi, "PLTE", 768);
618 png_write_byte(fi,palette[t].r);
619 png_write_byte(fi,palette[t].g);
620 png_write_byte(fi,palette[t].b);
627 int srcwidth = width * (bpp/8);
628 datalen3 = width*height*4;
629 data3 = (U8*)malloc(datalen3);
630 for(y=0;y<height;y++)
632 data3[pos2++]=0; //filter type
634 // 32 bit to 24 bit "conversion"
635 for(x=0;x<width;x++) {
636 data3[pos2++]=data[pos+1];
637 data3[pos2++]=data[pos+2];
638 data3[pos2++]=data[pos+3];
639 pos+=4; //ignore padding byte
643 for(x=0;x<srcwidth;x++)
644 data3[pos2++]=data[pos++];
647 pos+=((srcwidth+3)&~3)-srcwidth; //align
652 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
653 fprintf(stderr, "zlib error in pic %d\n", id);
656 logf("<verbose> Compressed data is %d bytes", datalen2);
657 png_start_chunk(fi, "IDAT", datalen2);
658 png_write_bytes(fi,data2,datalen2);
660 png_start_chunk(fi, "IEND", 0);
670 void handlesoundstream(TAG*tag)
672 char*filename = "output.mp3";
674 filename = destfilename;
675 if(!strcmp(filename,"output.swf"))
676 filename = "output.mp3";
679 case ST_SOUNDSTREAMHEAD:
680 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
681 mp3file = fopen(filename, "wb");
682 logf("<notice> Writing mp3 data to %s",filename);
685 logf("<error> Soundstream is not mp3");
687 case ST_SOUNDSTREAMHEAD2:
688 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
689 mp3file = fopen("mainstream.mp3", "wb");
690 logf("<notice> Writing mp3 data to %s",filename);
693 logf("<error> Soundstream is not mp3 (2)");
695 case ST_SOUNDSTREAMBLOCK:
697 fwrite(&tag->data[4],tag->len-4,1,mp3file);
702 int main (int argc,char ** argv)
711 char listavailable = 0;
712 processargs(argc, argv);
714 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
720 fprintf(stderr, "You must supply a filename.\n");
723 initLog(0,-1,0,0,-1, verbose);
725 f = open(filename,O_RDONLY);
729 perror("Couldn't open file: ");
732 if (swf_ReadSWF(f,&swf) < 0)
734 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
753 tagused = (char*)malloc(tagnum);
754 memset(tagused, 0, tagnum);
755 memset(used, 0, 65536);
756 memset(depths, 0, 65536);
761 if(swf_isAllowedSpriteTag(tag)) {
763 if(extractframes && is_in_range(frame, extractframes)) {
765 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
766 depths[swf_GetDepth(tag)] = 1;
768 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
769 int depth = swf_GetDepth(tag);
772 depths[swf_GetDepth(tag)] = 0;
775 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
776 (depths[swf_GetDepth(tag)]) && hollow) {
778 depths[swf_GetDepth(tag)] = 0;
782 enumerateIDs(tag, idcallback);
788 if(tag->id == ST_SOUNDSTREAMHEAD ||
789 tag->id == ST_SOUNDSTREAMHEAD2 ||
790 tag->id == ST_SOUNDSTREAMBLOCK) {
791 handlesoundstream(tag);
794 if(tag->id == ST_JPEGTABLES)
795 handlejpegtables(tag);
797 if(swf_isDefiningTag(tag)) {
798 int id = swf_GetDefineID(tag);
800 if(extractids && is_in_range(id, extractids)) {
804 if(extractjpegids && is_in_range(id, extractjpegids)) {
807 #ifdef _ZLIB_INCLUDED_
808 if(extractpngids && is_in_range(id, extractpngids)) {
813 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
814 mainr = tag->data[0];
815 maing = tag->data[1];
816 mainb = tag->data[2];
818 else if(tag->id == ST_PLACEOBJECT2) {
819 char*name = swf_GetName(tag);
820 if(name && extractname && !strcmp(name, extractname)) {
821 int id = swf_GetPlaceID(tag);
825 depths[swf_GetDepth(tag)] = 1;
828 else if(tag->id == ST_SHOWFRAME) {
836 if(tag->id == ST_DEFINESPRITE) {
837 while(tag->id != ST_END) {
846 extractTag(&swf, destfilename);