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 (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);
200 void extractTag(SWF*swf, char*filename)
211 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
213 newswf.fileVersion = swf->fileVersion;
214 newswf.frameRate = swf->frameRate;
215 newswf.movieSize = swf->movieSize;
217 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
218 desttag = newswf.firstTag;
222 swf_SetRGB(desttag,&rgb);
226 for(t=0;t<65536;t++) {
227 if(used[t] && !(used[t]&2)) {
228 if(tags[t]->id==ST_DEFINESPRITE) {
230 while(tag->id != ST_END)
232 enumerateIDs(tag, idcallback);
237 enumerateIDs(tags[t], idcallback);
244 srctag = swf->firstTag;
247 while(srctag && (srctag->id || sprite)) {
252 if(srctag->id == ST_END) {
255 if(srctag->id == ST_DEFINESPRITE)
257 if(swf_isDefiningTag(srctag)) {
258 int id = swf_GetDefineID(srctag);
262 if (((srctag->id == ST_PLACEOBJECT ||
263 srctag->id == ST_PLACEOBJECT2 ||
264 srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
265 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
272 if(srctag->id == ST_REMOVEOBJECT) {
273 if(!used[swf_GetPlaceID(srctag)])
278 TAG*ttag = (TAG*)malloc(sizeof(TAG));
279 desttag = swf_InsertTag(desttag, srctag->id);
280 desttag->len = desttag->memsize = srctag->len;
281 desttag->data = malloc(srctag->len);
282 memcpy(desttag->data, srctag->data, srctag->len);
287 srctag = srctag->next;
290 if(!extractframes && !hollow)
291 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
293 desttag = swf_InsertTag(desttag,ST_END);
295 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT, 0644);
296 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
299 swf_FreeTags(&newswf); // cleanup
302 void listObjects(SWF*swf)
308 char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Frames"};
309 printf("Objects in file %s:\n",filename);
317 (tag->id == ST_DEFINESHAPE ||
318 tag->id == ST_DEFINESHAPE2 ||
319 tag->id == ST_DEFINESHAPE3)) {
321 sprintf(text,"%d", swf_GetDefineID(tag));
324 if(tag->id == ST_DEFINESPRITE) {
327 sprintf(text,"%d", swf_GetDefineID(tag));
330 while(tag->id != ST_END)
334 if(t == 2 && (tag->id == ST_DEFINEBITS ||
335 tag->id == ST_DEFINEBITSJPEG2 ||
336 tag->id == ST_DEFINEBITSJPEG3)) {
338 sprintf(text,"%d", swf_GetDefineID(tag));
341 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
342 tag->id == ST_DEFINEBITSLOSSLESS2)) {
344 sprintf(text,"%d", swf_GetDefineID(tag));
348 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
350 sprintf(text,"%d", swf_GetDefineID(tag));
353 if(t == 5 && (tag->id == ST_SHOWFRAME)) {
355 sprintf(text,"%d", frame);
363 printf("%s: ", names[t]);
377 void handlejpegtables(TAG*tag)
379 if(tag->id == ST_JPEGTABLES) {
380 jpegtables = tag->data;
381 jpegtablessize = tag->len;
385 FILE* save_fopen(char* name, char* mode)
387 FILE*fi = fopen(name, mode);
389 fprintf(stderr, "Error: Couldn't open %s\n", name);
395 int findjpegboundary(U8*data, int len)
410 /* extract jpeg data out of a tag */
411 void handlejpeg(TAG*tag)
415 sprintf(name, "pic%d.jpeg", GET16(tag->data));
416 /* swf jpeg images have two streams, which both start with ff d8 and
417 end with ff d9. The following code handles sorting the middle
418 <ff d9 ff d8> bytes out, so that one stream remains */
419 if(tag->id == ST_DEFINEBITS && tag->len>2 && jpegtables) {
420 fi = save_fopen(name, "wb");
421 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
422 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
425 if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
427 int pos = findjpegboundary(&tag->data[2], tag->len-2);
431 fi = save_fopen(name, "wb");
432 fwrite(&tag->data[2], pos-2, 1, fi);
433 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
436 if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
437 U32 end = GET32(&tag->data[2])+6;
438 int pos = findjpegboundary(&tag->data[6], tag->len-6);
442 fi = save_fopen(name, "wb");
443 fwrite(&tag->data[6], pos-6, 1, fi);
444 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
449 #ifdef _ZLIB_INCLUDED_
452 static U32*crc32_table = 0;
453 static void make_crc32_table(void)
458 crc32_table = (U32*)malloc(1024);
460 for (t = 0; t < 256; t++) {
463 for (s = 0; s < 8; s++) {
464 c = (0xedb88320L*(c&1)) ^ (c >> 1);
469 static void png_write_byte(FILE*fi, U8 byte)
471 fwrite(&byte,1,1,fi);
472 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
474 static void png_start_chunk(FILE*fi, char*type, int len)
476 U8 mytype[4]={0,0,0,0};
477 U32 mylen = REVERSESWAP32(len);
478 memcpy(mytype,type,strlen(type));
479 fwrite(&mylen, 4, 1, fi);
481 png_write_byte(fi,mytype[0]);
482 png_write_byte(fi,mytype[1]);
483 png_write_byte(fi,mytype[2]);
484 png_write_byte(fi,mytype[3]);
486 static void png_write_bytes(FILE*fi, U8*bytes, int len)
490 png_write_byte(fi,bytes[t]);
492 static void png_write_dword(FILE*fi, U32 dword)
494 png_write_byte(fi,dword>>24);
495 png_write_byte(fi,dword>>16);
496 png_write_byte(fi,dword>>8);
497 png_write_byte(fi,dword);
499 static void png_end_chunk(FILE*fi)
501 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
506 /* extract a lossless image (png) out of a tag
507 This routine was originally meant to be a one-pager. I just
508 didn't know png is _that_ much fun. :) -mk
510 void handlelossless(TAG*tag)
527 U8 head[] = {137,80,78,71,13,10,26,10};
529 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
537 if(tag->id != ST_DEFINEBITSLOSSLESS &&
538 tag->id != ST_DEFINEBITSLOSSLESS2)
542 format = swf_GetU8(tag);
543 if(format == 3) bpp = 8;
544 if(format == 4) bpp = 16;
545 if(format == 5) bpp = 32;
546 if(format!=3 && format!=5) {
548 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
550 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
553 width = swf_GetU16(tag);
554 height = swf_GetU16(tag);
555 if(format == 3) cols = swf_GetU8(tag) + 1;
556 // this is what format means according to the flash specification. (which is
558 // if(format == 4) cols = swf_GetU16(tag) + 1;
559 // if(format == 5) cols = swf_GetU32(tag) + 1;
562 logf("<verbose> Width %d", width);
563 logf("<verbose> Height %d", height);
564 logf("<verbose> Format %d", format);
565 logf("<verbose> Cols %d", cols);
566 logf("<verbose> Bpp %d", bpp);
568 datalen = (width*height*bpp/8+cols*8);
573 data = malloc(datalen);
574 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
575 } while(error == Z_BUF_ERROR);
577 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
580 logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
583 data2 = malloc(datalen2);
584 palette = (RGBA*)malloc(cols*sizeof(RGBA));
586 for(t=0;t<cols;t++) {
587 palette[t].r = data[pos++];
588 palette[t].g = data[pos++];
589 palette[t].b = data[pos++];
591 palette[t].a = data[pos++];
595 sprintf(name, "pic%d.png", id);
596 fi = save_fopen(name, "wb");
597 fwrite(head,sizeof(head),1,fi);
599 png_start_chunk(fi, "IHDR", 13);
600 png_write_dword(fi,width);
601 png_write_dword(fi,height);
602 png_write_byte(fi,8);
604 png_write_byte(fi,3); //indexed
606 png_write_byte(fi,2); //rgb
609 png_write_byte(fi,0); //compression mode
610 png_write_byte(fi,0); //filter mode
611 png_write_byte(fi,0); //interlace mode
615 png_start_chunk(fi, "PLTE", 768);
617 png_write_byte(fi,palette[t].r);
618 png_write_byte(fi,palette[t].g);
619 png_write_byte(fi,palette[t].b);
626 int srcwidth = width * (bpp/8);
627 datalen3 = width*height*4;
628 data3 = (U8*)malloc(datalen3);
629 for(y=0;y<height;y++)
631 data3[pos2++]=0; //filter type
633 // 32 bit to 24 bit "conversion"
634 for(x=0;x<width;x++) {
635 data3[pos2++]=data[pos+1];
636 data3[pos2++]=data[pos+2];
637 data3[pos2++]=data[pos+3];
638 pos+=4; //ignore padding byte
642 for(x=0;x<srcwidth;x++)
643 data3[pos2++]=data[pos++];
646 pos+=((srcwidth+3)&~3)-srcwidth; //align
651 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
652 fprintf(stderr, "zlib error in pic %d\n", id);
655 logf("<verbose> Compressed data is %d bytes", datalen2);
656 png_start_chunk(fi, "IDAT", datalen2);
657 png_write_bytes(fi,data2,datalen2);
659 png_start_chunk(fi, "IEND", 0);
669 void handlesoundstream(TAG*tag)
671 char*filename = "output.mp3";
673 filename = destfilename;
674 if(!strcmp(filename,"output.swf"))
675 filename = "output.mp3";
678 case ST_SOUNDSTREAMHEAD:
679 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
680 mp3file = fopen(filename, "wb");
681 logf("<notice> Writing mp3 data to %s",filename);
684 logf("<error> Soundstream is not mp3");
686 case ST_SOUNDSTREAMHEAD2:
687 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
688 mp3file = fopen("mainstream.mp3", "wb");
689 logf("<notice> Writing mp3 data to %s",filename);
692 logf("<error> Soundstream is not mp3 (2)");
694 case ST_SOUNDSTREAMBLOCK:
696 fwrite(&tag->data[4],tag->len-4,1,mp3file);
701 int main (int argc,char ** argv)
710 char listavailable = 0;
711 processargs(argc, argv);
713 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
719 fprintf(stderr, "You must supply a filename.\n");
722 initLog(0,-1,0,0,-1, verbose);
724 f = open(filename,O_RDONLY);
728 perror("Couldn't open file: ");
731 if (swf_ReadSWF(f,&swf) < 0)
733 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
752 tagused = (char*)malloc(tagnum);
753 memset(tagused, 0, tagnum);
754 memset(used, 0, 65536);
755 memset(depths, 0, 65536);
760 if(swf_isAllowedSpriteTag(tag)) {
762 if(extractframes && is_in_range(frame, extractframes)) {
764 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
765 depths[swf_GetDepth(tag)] = 1;
767 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
768 int depth = swf_GetDepth(tag);
771 depths[swf_GetDepth(tag)] = 0;
774 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
775 (depths[swf_GetDepth(tag)]) && hollow) {
777 depths[swf_GetDepth(tag)] = 0;
781 enumerateIDs(tag, idcallback);
787 if(tag->id == ST_SOUNDSTREAMHEAD ||
788 tag->id == ST_SOUNDSTREAMHEAD2 ||
789 tag->id == ST_SOUNDSTREAMBLOCK) {
790 handlesoundstream(tag);
793 if(tag->id == ST_JPEGTABLES)
794 handlejpegtables(tag);
796 if(swf_isDefiningTag(tag)) {
797 int id = swf_GetDefineID(tag);
799 if(extractids && is_in_range(id, extractids)) {
803 if(extractjpegids && is_in_range(id, extractjpegids)) {
806 #ifdef _ZLIB_INCLUDED_
807 if(extractpngids && is_in_range(id, extractpngids)) {
812 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
813 mainr = tag->data[0];
814 maing = tag->data[1];
815 mainb = tag->data[2];
817 else if(tag->id == ST_PLACEOBJECT2) {
818 char*name = swf_GetName(tag);
819 if(name && extractname && !strcmp(name, extractname)) {
820 int id = swf_GetPlaceID(tag);
824 depths[swf_GetDepth(tag)] = 1;
827 else if(tag->id == ST_SHOWFRAME) {
835 if(tag->id == ST_DEFINESPRITE) {
836 while(tag->id != ST_END) {
845 extractTag(&swf, destfilename);