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"
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 (use with -f)\n");
146 printf("\t-V , --version\t\t\t Print program version and exit\n");
148 int args_callback_command(char*name,char*val)
151 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
158 U8 mainr,maing,mainb;
159 /* 1 = used, not expanded,
161 5 = wanted, not expanded
169 void idcallback(void*data)
171 if(!(used[GET16(data)]&1)) {
173 used[GET16(data)] |= 1;
177 void enumerateIDs(TAG*tag, void(*callback)(void*))
183 data = (U8*)malloc(len);
184 PUT16(data, (tag->id<<6)+63);
185 *(U8*)&data[2] = tag->len;
186 *(U8*)&data[3] = tag->len>>8;
187 *(U8*)&data[4] = tag->len>>16;
188 *(U8*)&data[5] = tag->len>>24;
189 memcpy(&data[6], tag->data, tag->len);
192 data = (U8*)malloc(len);
193 PUT16(data, (tag->id<<6)+tag->len);
194 memcpy(&data[2], tag->data, tag->len);
196 map_ids_mem(data, len, callback);
199 void extractTag(SWF*swf, char*filename)
210 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
212 newswf.fileVersion = swf->fileVersion;
213 newswf.frameRate = swf->frameRate;
214 newswf.movieSize = swf->movieSize;
216 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
217 desttag = newswf.firstTag;
221 swf_SetRGB(desttag,&rgb);
225 for(t=0;t<65536;t++) {
226 if(used[t] && !(used[t]&2)) {
227 if(tags[t]->id==ST_DEFINESPRITE) {
229 while(tag->id != ST_END)
231 enumerateIDs(tag, idcallback);
236 enumerateIDs(tags[t], idcallback);
243 srctag = swf->firstTag;
246 while(srctag && (srctag->id || sprite)) {
251 if(srctag->id == ST_END) {
254 if(srctag->id == ST_DEFINESPRITE)
256 if(swf_isDefiningTag(srctag)) {
257 int id = swf_GetDefineID(srctag);
261 if (((srctag->id == ST_PLACEOBJECT ||
262 srctag->id == ST_PLACEOBJECT2 ||
263 srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
264 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
271 if(srctag->id == ST_REMOVEOBJECT) {
272 if(!used[swf_GetPlaceID(srctag)])
277 TAG*ttag = (TAG*)malloc(sizeof(TAG));
278 desttag = swf_InsertTag(desttag, srctag->id);
279 desttag->len = desttag->memsize = srctag->len;
280 desttag->data = malloc(srctag->len);
281 memcpy(desttag->data, srctag->data, srctag->len);
286 srctag = srctag->next;
289 if(!extractframes && !hollow)
290 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
292 desttag = swf_InsertTag(desttag,ST_END);
294 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT, 0644);
295 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
298 swf_FreeTags(&newswf); // cleanup
301 void listObjects(SWF*swf)
307 char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Frames"};
308 printf("Objects in file %s:\n",filename);
316 (tag->id == ST_DEFINESHAPE ||
317 tag->id == ST_DEFINESHAPE2 ||
318 tag->id == ST_DEFINESHAPE3)) {
320 sprintf(text,"%d", swf_GetDefineID(tag));
323 if(tag->id == ST_DEFINESPRITE) {
326 sprintf(text,"%d", swf_GetDefineID(tag));
329 while(tag->id != ST_END)
333 if(t == 2 && (tag->id == ST_DEFINEBITS ||
334 tag->id == ST_DEFINEBITSJPEG2 ||
335 tag->id == ST_DEFINEBITSJPEG3)) {
337 sprintf(text,"%d", swf_GetDefineID(tag));
340 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
341 tag->id == ST_DEFINEBITSLOSSLESS2)) {
343 sprintf(text,"%d", swf_GetDefineID(tag));
347 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
349 sprintf(text,"%d", swf_GetDefineID(tag));
352 if(t == 5 && (tag->id == ST_SHOWFRAME)) {
354 sprintf(text,"%d", frame);
362 printf("%s: ", names[t]);
376 void handlejpegtables(TAG*tag)
378 if(tag->id == ST_JPEGTABLES) {
379 jpegtables = tag->data;
380 jpegtablessize = tag->len;
384 FILE* save_fopen(char* name, char* mode)
386 FILE*fi = fopen(name, mode);
388 fprintf(stderr, "Error: Couldn't open %s\n", name);
394 int findjpegboundary(U8*data, int len)
409 /* extract jpeg data out of a tag */
410 void handlejpeg(TAG*tag)
414 sprintf(name, "pic%d.jpeg", GET16(tag->data));
415 /* swf jpeg images have two streams, which both start with ff d8 and
416 end with ff d9. The following code handles sorting the middle
417 <ff d9 ff d8> bytes out, so that one stream remains */
418 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
419 fi = save_fopen(name, "wb");
420 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
421 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
424 if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
426 int pos = findjpegboundary(&tag->data[2], tag->len-2);
430 fi = save_fopen(name, "wb");
431 fwrite(&tag->data[2], pos-2, 1, fi);
432 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
435 if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
436 U32 end = GET32(&tag->data[2])+6;
437 int pos = findjpegboundary(&tag->data[6], tag->len-6);
441 fi = save_fopen(name, "wb");
442 fwrite(&tag->data[6], pos-6, 1, fi);
443 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
448 #ifdef _ZLIB_INCLUDED_
451 static U32*crc32_table = 0;
452 static void make_crc32_table(void)
457 crc32_table = (U32*)malloc(1024);
459 for (t = 0; t < 256; t++) {
462 for (s = 0; s < 8; s++) {
463 c = (0xedb88320L*(c&1)) ^ (c >> 1);
468 static void png_write_byte(FILE*fi, U8 byte)
470 fwrite(&byte,1,1,fi);
471 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
473 static void png_start_chunk(FILE*fi, char*type, int len)
475 U8 mytype[4]={0,0,0,0};
476 U32 mylen = REVERSESWAP32(len);
477 memcpy(mytype,type,strlen(type));
478 fwrite(&mylen, 4, 1, fi);
480 png_write_byte(fi,mytype[0]);
481 png_write_byte(fi,mytype[1]);
482 png_write_byte(fi,mytype[2]);
483 png_write_byte(fi,mytype[3]);
485 static void png_write_bytes(FILE*fi, U8*bytes, int len)
489 png_write_byte(fi,bytes[t]);
491 static void png_write_dword(FILE*fi, U32 dword)
493 png_write_byte(fi,dword>>24);
494 png_write_byte(fi,dword>>16);
495 png_write_byte(fi,dword>>8);
496 png_write_byte(fi,dword);
498 static void png_end_chunk(FILE*fi)
500 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
505 /* extract a lossless image (png) out of a tag
506 This routine was originally meant to be a one-pager. I just
507 didn't know png is _that_ much fun. :) -mk
509 void handlelossless(TAG*tag)
526 U8 head[] = {137,80,78,71,13,10,26,10};
528 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
536 if(tag->id != ST_DEFINEBITSLOSSLESS &&
537 tag->id != ST_DEFINEBITSLOSSLESS2)
541 format = swf_GetU8(tag);
542 if(format == 3) bpp = 8;
543 if(format == 4) bpp = 16;
544 if(format == 5) bpp = 32;
545 if(format!=3 && format!=5) {
547 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
549 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
552 width = swf_GetU16(tag);
553 height = swf_GetU16(tag);
554 if(format == 3) cols = swf_GetU8(tag) + 1;
555 // this is what format means according to the flash specification. (which is
557 // if(format == 4) cols = swf_GetU16(tag) + 1;
558 // if(format == 5) cols = swf_GetU32(tag) + 1;
561 logf("<verbose> Width %d", width);
562 logf("<verbose> Height %d", height);
563 logf("<verbose> Format %d", format);
564 logf("<verbose> Cols %d", cols);
565 logf("<verbose> Bpp %d", bpp);
567 datalen = (width*height*bpp/8+cols*8);
572 data = malloc(datalen);
573 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
574 } while(error == Z_BUF_ERROR);
576 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
579 logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
582 data2 = malloc(datalen2);
583 palette = (RGBA*)malloc(cols*sizeof(RGBA));
585 for(t=0;t<cols;t++) {
586 palette[t].r = data[pos++];
587 palette[t].g = data[pos++];
588 palette[t].b = data[pos++];
590 palette[t].a = data[pos++];
594 sprintf(name, "pic%d.png", id);
595 fi = save_fopen(name, "wb");
596 fwrite(head,sizeof(head),1,fi);
598 png_start_chunk(fi, "IHDR", 13);
599 png_write_dword(fi,width);
600 png_write_dword(fi,height);
601 png_write_byte(fi,8);
603 png_write_byte(fi,3); //indexed
605 png_write_byte(fi,2); //rgb
608 png_write_byte(fi,0); //compression mode
609 png_write_byte(fi,0); //filter mode
610 png_write_byte(fi,0); //interlace mode
614 png_start_chunk(fi, "PLTE", 768);
616 png_write_byte(fi,palette[t].r);
617 png_write_byte(fi,palette[t].g);
618 png_write_byte(fi,palette[t].b);
625 int srcwidth = width * (bpp/8);
626 datalen3 = width*height*4;
627 data3 = (U8*)malloc(datalen3);
628 for(y=0;y<height;y++)
630 data3[pos2++]=0; //filter type
632 // 32 bit to 24 bit "conversion"
633 for(x=0;x<width;x++) {
634 data3[pos2++]=data[pos+1];
635 data3[pos2++]=data[pos+2];
636 data3[pos2++]=data[pos+3];
637 pos+=4; //ignore padding byte
641 for(x=0;x<srcwidth;x++)
642 data3[pos2++]=data[pos++];
645 pos+=((srcwidth+3)&~3)-srcwidth; //align
650 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
651 fprintf(stderr, "zlib error in pic %d\n", id);
654 logf("<verbose> Compressed data is %d bytes", datalen2);
655 png_start_chunk(fi, "IDAT", datalen2);
656 png_write_bytes(fi,data2,datalen2);
658 png_start_chunk(fi, "IEND", 0);
668 void handlesoundstream(TAG*tag)
670 char*filename = "output.mp3";
672 filename = destfilename;
673 if(!strcmp(filename,"output.swf"))
674 filename = "output.mp3";
677 case ST_SOUNDSTREAMHEAD:
678 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
679 mp3file = fopen(filename, "wb");
680 logf("<notice> Writing mp3 data to %s",filename);
683 logf("<error> Soundstream is not mp3");
685 case ST_SOUNDSTREAMHEAD2:
686 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
687 mp3file = fopen("mainstream.mp3", "wb");
688 logf("<notice> Writing mp3 data to %s",filename);
691 logf("<error> Soundstream is not mp3 (2)");
693 case ST_SOUNDSTREAMBLOCK:
695 fwrite(&tag->data[4],tag->len-4,1,mp3file);
700 int main (int argc,char ** argv)
709 char listavailable = 0;
710 processargs(argc, argv);
712 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
718 fprintf(stderr, "You must supply a filename.\n");
721 initLog(0,-1,0,0,-1, verbose);
723 f = open(filename,O_RDONLY);
727 perror("Couldn't open file: ");
730 if (swf_ReadSWF(f,&swf) < 0)
732 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
751 tagused = (char*)malloc(tagnum);
752 memset(tagused, 0, tagnum);
753 memset(used, 0, 65536);
754 memset(depths, 0, 65536);
759 if(swf_isAllowedSpriteTag(tag)) {
761 if(extractframes && is_in_range(frame, extractframes)) {
763 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
764 depths[swf_GetDepth(tag)] = 1;
766 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
767 int depth = swf_GetDepth(tag);
770 depths[swf_GetDepth(tag)] = 0;
773 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
774 (depths[swf_GetDepth(tag)]) && hollow) {
776 depths[swf_GetDepth(tag)] = 0;
780 enumerateIDs(tag, idcallback);
786 if(tag->id == ST_SOUNDSTREAMHEAD ||
787 tag->id == ST_SOUNDSTREAMHEAD2 ||
788 tag->id == ST_SOUNDSTREAMBLOCK) {
789 handlesoundstream(tag);
792 if(tag->id == ST_JPEGTABLES)
793 handlejpegtables(tag);
795 if(swf_isDefiningTag(tag)) {
796 int id = swf_GetDefineID(tag);
798 if(extractids && is_in_range(id, extractids)) {
802 if(extractjpegids && is_in_range(id, extractjpegids)) {
805 #ifdef _ZLIB_INCLUDED_
806 if(extractpngids && is_in_range(id, extractpngids)) {
811 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
812 mainr = tag->data[0];
813 maing = tag->data[1];
814 mainb = tag->data[2];
816 else if(tag->id == ST_PLACEOBJECT2) {
817 char*name = swf_GetName(tag);
818 if(name && extractname && !strcmp(name, extractname)) {
819 int id = swf_GetPlaceID(tag);
823 depths[swf_GetDepth(tag)] = 1;
826 else if(tag->id == ST_SHOWFRAME) {
834 if(tag->id == ST_DEFINESPRITE) {
835 while(tag->id != ST_END) {
844 extractTag(&swf, destfilename);