swfextract now uses the reloc functions from rfxswflib.
[swftools.git] / src / swfextract.c
1 /* swfextract.c
2    Allows to extract parts of the swf into a new file.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
7
8    This file is distributed under the GPL, see file COPYING for details */
9
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include "../lib/rfxswf.h"
14 #include "../lib/args.h"
15 #include "../lib/log.h"
16 #ifdef HAVE_ZLIB_H
17 #ifdef HAVE_LIBZ
18 #include "zlib.h"
19 #define _ZLIB_INCLUDED_
20 #endif
21 #endif
22
23 char * filename = 0;
24 char * destfilename = "output.swf";
25 int verbose = 3;
26
27 char* extractids = 0;
28 char* extractframes = 0;
29 char* extractjpegids = 0;
30 char* extractpngids = 0;
31 char extractmp3 = 0;
32
33 char* extractname = 0;
34
35 char hollow = 0;
36
37 int numextracts = 0;
38
39 struct options_t options[] =
40 {
41  {"o","output"},
42  {"w","hollow"},
43  {"v","verbose"},
44  {"i","id"},
45  {"j","jpegs"},
46  {"p","pngs"},
47  {"m","mp3"},
48  {"n","name"},
49  {"f","frame"},
50  {"V","version"},
51  {0,0}
52 };
53
54
55 int args_callback_option(char*name,char*val)
56 {
57     if(!strcmp(name, "V")) {
58         printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
59         exit(0);
60     } 
61     else if(!strcmp(name, "o")) {
62         destfilename = val;
63         return 1;
64     } 
65     else if(!strcmp(name, "i")) {
66         extractids = val;
67         numextracts++;
68         if(extractname) {
69             fprintf(stderr, "You can only supply either name or id\n");
70             exit(1);
71         }
72         return 1;
73     } 
74     else if(!strcmp(name, "n")) {
75         extractname = val;
76         numextracts++;
77         if(extractids) {
78             fprintf(stderr, "You can only supply either name or id\n");
79             exit(1);
80         }
81         return 1;
82     } 
83     else if(!strcmp(name, "v")) {
84         verbose ++;
85         return 0;
86     } 
87     else if(!strcmp(name, "m")) {
88         extractmp3 = 1;
89         numextracts++;
90         return 0;
91     }
92     else if(!strcmp(name, "j")) {
93         if(extractjpegids) {
94             fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
95             exit(1);
96         }
97         numextracts++;
98         extractjpegids = val;
99         return 1;
100     } 
101 #ifdef _ZLIB_INCLUDED_
102     else if(!strcmp(name, "p")) {
103         if(extractpngids) {
104             fprintf(stderr, "Only one --pngs argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
105             exit(1);
106         }
107         numextracts++;
108         extractpngids = val;
109         return 1;
110     } 
111 #endif
112     else if(!strcmp(name, "f")) {
113         numextracts++;
114         extractframes = val;
115         return 1;
116     }
117     else if(!strcmp(name, "w")) {
118         hollow = 1;
119         return 0;
120     }
121     else {
122         printf("Unknown option: -%s\n", name);
123         return 0;
124     }
125
126     return 0;
127 }
128 int args_callback_longoption(char*name,char*val)
129 {
130     return args_long2shortoption(options, name, val);
131 }
132 void args_callback_usage(char*name)
133 {    
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");
142 #endif
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");
148 }
149 int args_callback_command(char*name,char*val)
150 {
151     if(filename) {
152         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
153                  filename, name);
154     }
155     filename = name;
156     return 0;
157 }
158
159 U8 mainr,maing,mainb;
160 /* 1 = used, not expanded,
161    3 = used, expanded
162    5 = wanted, not expanded
163    7 = wanted, expanded
164  */
165 char used[65536];
166 TAG*tags[65536];
167 int changed;
168 char * tagused;
169
170 void idcallback(void*data)
171 {
172     if(!(used[GET16(data)]&1)) {
173         changed = 1;
174         used[GET16(data)] |= 1;
175     }
176 }
177
178 void enumerateIDs(TAG*tag, void(*callback)(void*))
179 {
180 /*    U8*data;
181     int len = tag->len;
182     if(tag->len>=64) {
183         len += 6;
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);
191     } else {
192         len += 2;
193         data = (U8*)malloc(len);
194         PUT16(data, (tag->id<<6)+tag->len);
195         memcpy(&data[2], tag->data, tag->len);
196     }
197     map_ids_mem(data, len, callback);
198  */
199     int num = swf_GetNumUsedIDs(tag);
200     int *ptr = malloc(sizeof(int)*num);
201     int t;
202     swf_GetUsedIDs(tag, ptr);
203     for(t=0;t<num;t++)
204         callback(&tag->data[ptr[t]]);
205 }
206
207 void extractTag(SWF*swf, char*filename)
208 {
209     SWF newswf;
210     TAG*desttag;
211     TAG*srctag;
212     RGBA rgb;
213     char sprite;
214     int f;
215     int t;
216     int tagnum;
217     int copy = 0;
218     memset(&newswf,0x00,sizeof(SWF));        // set global movie parameters
219
220     newswf.fileVersion    = swf->fileVersion;
221     newswf.frameRate      = swf->frameRate;
222     newswf.movieSize      = swf->movieSize;
223                 
224     newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
225     desttag = newswf.firstTag;
226     rgb.r = mainr;
227     rgb.g = maing;
228     rgb.b = mainb;
229     swf_SetRGB(desttag,&rgb);
230
231     do {
232        changed = 0;
233        for(t=0;t<65536;t++) {
234            if(used[t] && !(used[t]&2)) {
235              if(tags[t]->id==ST_DEFINESPRITE) {
236                  TAG*tag = tags[t];
237                  while(tag->id != ST_END)
238                  {
239                      enumerateIDs(tag, idcallback);
240                      tag = tag->next;
241                  }
242              }
243              else 
244                enumerateIDs(tags[t], idcallback);
245              used[t] |= 2;
246            }
247        }
248     }
249     while(changed);
250
251     srctag = swf->firstTag;
252     tagnum = 0;
253     sprite = 0;
254     while(srctag && (srctag->id || sprite)) {
255         int reset = 0;
256         if(!sprite) {
257             copy = 0;
258         }
259         if(srctag->id == ST_END) {
260             sprite = 0;
261         }
262         if(srctag->id == ST_DEFINESPRITE)
263             sprite = 1;
264         if(swf_isDefiningTag(srctag)) {
265             int id = swf_GetDefineID(srctag);
266             if(used[id]) 
267                 copy = 1;
268         } else 
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)]) ||
273               (tagused[tagnum])) 
274         {
275                 if(copy == 0)
276                     reset = 1;
277                 copy = 1;
278         } 
279         if(srctag->id == ST_REMOVEOBJECT) {
280             if(!used[swf_GetPlaceID(srctag)])
281                 copy = 0;
282         }
283
284         if(copy) {
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);
290             if(reset)
291                 copy = 0;
292         }
293         
294         srctag = srctag->next;
295         tagnum ++;
296     }
297     if(!extractframes && !hollow)
298         desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
299
300     desttag = swf_InsertTag(desttag,ST_END);
301     
302     f = open(filename, O_TRUNC|O_WRONLY|O_CREAT, 0644);
303     if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
304     close(f);
305
306     swf_FreeTags(&newswf);                       // cleanup
307 }
308
309 void listObjects(SWF*swf)
310 {
311     TAG*tag;
312     char first;
313     int t;
314     int frame = 0;
315     char*names[] = {"Shapes","MovieClips","JPEGs","PNGs","Sounds","Frames"};
316     printf("Objects in file %s:\n",filename);
317     for(t=0;t<6;t++) {
318         tag = swf->firstTag;
319         first = 1;
320         while(tag) {
321             char show = 0;
322             char text[80];
323             if(t == 0 &&
324                (tag->id == ST_DEFINESHAPE ||
325                 tag->id == ST_DEFINESHAPE2 ||
326                 tag->id == ST_DEFINESHAPE3)) {
327                 show = 1;
328                 sprintf(text,"%d", swf_GetDefineID(tag));
329             }
330
331             if(tag->id == ST_DEFINESPRITE) {
332                 if (t == 1)  {
333                     show = 1;
334                     sprintf(text,"%d", swf_GetDefineID(tag));
335                 }
336
337                 while(tag->id != ST_END)
338                     tag = tag->next;
339             }
340
341             if(t == 2 && (tag->id == ST_DEFINEBITS ||
342                 tag->id == ST_DEFINEBITSJPEG2 ||
343                 tag->id == ST_DEFINEBITSJPEG3)) {
344                 show = 1;
345                 sprintf(text,"%d", swf_GetDefineID(tag));
346             }
347
348             if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
349                 tag->id == ST_DEFINEBITSLOSSLESS2)) {
350                 show = 1;
351                 sprintf(text,"%d", swf_GetDefineID(tag));
352             }
353
354
355             if(t == 4 && (tag->id == ST_DEFINESOUND)) {
356                 show = 1;
357                 sprintf(text,"%d", swf_GetDefineID(tag));
358             }
359             
360             if(t == 5 && (tag->id == ST_SHOWFRAME)) {
361                 show = 1;
362                 sprintf(text,"%d", frame);
363                 frame ++;
364             }
365
366             if(show) {
367                 if(!first)
368                     printf(", ");
369                 else
370                     printf("%s: ", names[t]);
371                 printf("%s", text);
372                 first = 0;
373             }
374             tag=tag->next;
375         }
376         if(!first)
377             printf("\n");
378     }
379 }
380
381 U8*jpegtables = 0;
382 int jpegtablessize;
383
384 void handlejpegtables(TAG*tag)
385 {
386     if(tag->id == ST_JPEGTABLES) {
387         jpegtables = tag->data;
388         jpegtablessize = tag->len;
389     }
390 }
391
392 FILE* save_fopen(char* name, char* mode)
393 {
394     FILE*fi = fopen(name, mode);
395     if(!fi) {
396         fprintf(stderr, "Error: Couldn't open %s\n", name);
397         exit(1);
398     }
399     return fi;
400 }
401
402 int findjpegboundary(U8*data, int len)
403 {
404     int t;
405     int pos=-1;
406     for(t=0;t<len;t++) {
407         if(data[t  ]==0xff &&
408            data[t+1]==0xd9 &&
409            data[t+2]==0xff &&
410            data[t+3]==0xd8) {
411             pos = t;
412         }
413     }
414     return pos;
415 }
416
417 /* extract jpeg data out of a tag */
418 void handlejpeg(TAG*tag)
419 {
420     char name[80];
421     FILE*fi;
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)
430         fclose(fi);
431     }
432     if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
433         int end = tag->len;
434         int pos = findjpegboundary(&tag->data[2], tag->len-2);
435         if(pos<0)
436             return;
437         pos+=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);
441         fclose(fi);
442     }
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);
446         if(pos<0)
447             return;
448         pos+=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);
452         fclose(fi);
453     }
454 }
455
456 #ifdef _ZLIB_INCLUDED_
457 static U32 mycrc32;
458
459 static U32*crc32_table = 0;
460 static void make_crc32_table(void)
461 {
462   int t;
463   if(crc32_table) 
464       return;
465   crc32_table = (U32*)malloc(1024);
466
467   for (t = 0; t < 256; t++) {
468     U32 c = t;
469     int s;
470     for (s = 0; s < 8; s++) {
471       c = (0xedb88320L*(c&1)) ^ (c >> 1);
472     }
473     crc32_table[t] = c;
474   }
475 }
476 static void png_write_byte(FILE*fi, U8 byte)
477 {
478     fwrite(&byte,1,1,fi);
479     mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
480 }
481 static void png_start_chunk(FILE*fi, char*type, int len)
482 {
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);
487     mycrc32=0xffffffff;
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]);
492 }
493 static void png_write_bytes(FILE*fi, U8*bytes, int len)
494 {
495     int t;
496     for(t=0;t<len;t++)
497         png_write_byte(fi,bytes[t]);
498 }
499 static void png_write_dword(FILE*fi, U32 dword)
500 {
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);
505 }
506 static void png_end_chunk(FILE*fi)
507 {
508     U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
509     fwrite(&tmp,4,1,fi);
510 }
511
512
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
516  */
517 void handlelossless(TAG*tag)
518 {
519     char name[80];
520     FILE*fi;
521     int width, height;
522     int crc;
523     int id;
524     int t;
525     U8 bpp = 1;
526     U8 format;
527     U8 tmp;
528     U8* data=0;
529     U8* data2=0;
530     U8* data3=0;
531     U32 datalen;
532     U32 datalen2;
533     U32 datalen3;
534     U8 head[] = {137,80,78,71,13,10,26,10};
535     int cols;
536     char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
537     RGBA* palette;
538     int pos;
539     int error;
540     U32 tmp32;
541
542     make_crc32_table();
543
544     if(tag->id != ST_DEFINEBITSLOSSLESS &&
545        tag->id != ST_DEFINEBITSLOSSLESS2)
546         return;
547
548     id =swf_GetU16(tag);
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) {
554         if(format==4)
555         fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
556         else 
557         fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
558         return;
559     }
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
564 // clearly wrong)
565 //    if(format == 4) cols = swf_GetU16(tag) + 1;
566 //    if(format == 5) cols = swf_GetU32(tag) + 1;
567     else cols = 0;
568
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);
574
575     datalen = (width*height*bpp/8+cols*8);
576     do {
577         if(data)
578             free(data);
579         datalen+=4096;
580         data = malloc(datalen);
581         error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
582     } while(error == Z_BUF_ERROR);
583     if(error != Z_OK) {
584         fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
585         return;
586     }
587     logf("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
588     pos = 0;
589     datalen2 = datalen;
590     data2 = malloc(datalen2);
591     palette = (RGBA*)malloc(cols*sizeof(RGBA));
592
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++];
597         if(alpha) {
598             palette[t].a = data[pos++];
599         }
600     }
601
602     sprintf(name, "pic%d.png", id);
603     fi = save_fopen(name, "wb");
604     fwrite(head,sizeof(head),1,fi);     
605
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);
610      if(format == 3)
611      png_write_byte(fi,3); //indexed
612      else if(format == 5)
613      png_write_byte(fi,2); //rgb
614      else return;
615
616      png_write_byte(fi,0); //compression mode
617      png_write_byte(fi,0); //filter mode
618      png_write_byte(fi,0); //interlace mode
619     png_end_chunk(fi);
620    
621     if(format == 3) {
622         png_start_chunk(fi, "PLTE", 768);
623          for(t=0;t<256;t++) {
624              png_write_byte(fi,palette[t].r);
625              png_write_byte(fi,palette[t].g);
626              png_write_byte(fi,palette[t].b);
627          }
628         png_end_chunk(fi);
629     }
630     {
631         int pos2 = 0;
632         int x,y;
633         int srcwidth = width * (bpp/8);
634         datalen3 = width*height*4;
635         data3 = (U8*)malloc(datalen3);
636         for(y=0;y<height;y++)
637         {
638            data3[pos2++]=0; //filter type
639            if(bpp==32) {
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
646             }
647            }
648            else {
649                 for(x=0;x<srcwidth;x++)
650                     data3[pos2++]=data[pos++];
651            }
652            
653            pos+=((srcwidth+3)&~3)-srcwidth; //align
654         }
655         datalen3=pos2;
656     }
657
658     if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
659         fprintf(stderr, "zlib error in pic %d\n", id);
660         return;
661     }
662     logf("<verbose> Compressed data is %d bytes", datalen2);
663     png_start_chunk(fi, "IDAT", datalen2);
664     png_write_bytes(fi,data2,datalen2);
665     png_end_chunk(fi);
666     png_start_chunk(fi, "IEND", 0);
667     png_end_chunk(fi);
668
669     free(data);
670     free(data2);
671     free(data3);
672 }
673 #endif
674
675 FILE*mp3file;
676 void handlesoundstream(TAG*tag)
677 {
678     char*filename = "output.mp3";
679     if(numextracts==1) {
680         filename = destfilename;
681         if(!strcmp(filename,"output.swf"))
682             filename = "output.mp3";
683     }
684     switch(tag->id) {
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);
689             }
690             else
691                 logf("<error> Soundstream is not mp3");
692         break;
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);
697             }
698             else
699                 logf("<error> Soundstream is not mp3 (2)");
700         break;
701         case ST_SOUNDSTREAMBLOCK:
702             if(mp3file)
703                 fwrite(&tag->data[4],tag->len-4,1,mp3file);
704         break;
705     }
706 }
707
708 int main (int argc,char ** argv)
709
710     TAG*tag;
711     SWF swf;
712     int f;
713     int found = 0;
714     int frame = 0;
715     int tagnum = 0;
716     char depths[65536];
717     char listavailable = 0;
718     processargs(argc, argv);
719
720     if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
721         && !extractmp3)
722         listavailable = 1;
723
724     if(!filename)
725     {
726         fprintf(stderr, "You must supply a filename.\n");
727         return 1;
728     }
729     initLog(0,-1,0,0,-1, verbose);
730
731     f = open(filename,O_RDONLY);
732
733     if (f<0)
734     { 
735         perror("Couldn't open file: ");
736         exit(1);
737     }
738     if (swf_ReadSWF(f,&swf) < 0)
739     { 
740         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
741         close(f);
742         exit(1);
743     }
744     close(f);
745
746     if(listavailable) {
747         listObjects(&swf);
748         swf_FreeTags(&swf);
749         return 0;
750     }
751
752     tag = swf.firstTag;
753     tagnum = 0;
754     while(tag) {
755         tagnum ++;
756         tag = tag->next;
757     }
758
759     tagused = (char*)malloc(tagnum);
760     memset(tagused, 0, tagnum);
761     memset(used, 0, 65536);
762     memset(depths, 0, 65536);
763
764     tag = swf.firstTag;
765     tagnum = 0;
766     while(tag) {
767         if(swf_isAllowedSpriteTag(tag)) {
768             int write = 0;
769             if(extractframes && is_in_range(frame, extractframes)) {
770                 write = 1;
771                 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
772                     depths[swf_GetDepth(tag)] = 1;
773                 }
774                 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
775                     int depth = swf_GetDepth(tag);
776                     if(!depths[depth]) 
777                         write = 0;
778                     depths[swf_GetDepth(tag)] = 0;
779                 }
780             } else {
781                 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) && 
782                     (depths[swf_GetDepth(tag)]) && hollow) {
783                     write = 1;
784                     depths[swf_GetDepth(tag)] = 0;
785                 }
786             }
787             if(write) {
788                 enumerateIDs(tag, idcallback);
789                 found = 1;
790                 tagused[tagnum] = 1;
791             }
792         }
793
794         if(tag->id == ST_SOUNDSTREAMHEAD ||
795            tag->id == ST_SOUNDSTREAMHEAD2 ||
796            tag->id == ST_SOUNDSTREAMBLOCK) {
797             handlesoundstream(tag);
798         }
799
800         if(tag->id == ST_JPEGTABLES)
801             handlejpegtables(tag);
802
803         if(swf_isDefiningTag(tag)) {
804             int id = swf_GetDefineID(tag);
805             tags[id] = tag;
806             if(extractids && is_in_range(id, extractids)) {
807                 used[id] = 5;
808                 found = 1;
809             }
810             if(extractjpegids && is_in_range(id, extractjpegids)) {
811                 handlejpeg(tag);
812             }
813 #ifdef _ZLIB_INCLUDED_
814             if(extractpngids && is_in_range(id, extractpngids)) {
815                 handlelossless(tag);
816             }
817 #endif
818         }
819         else if (tag->id == ST_SETBACKGROUNDCOLOR) {
820             mainr = tag->data[0];
821             maing = tag->data[1];
822             mainb = tag->data[2];
823         }
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); 
828                 used[id] = 5;
829                 found = 1;
830                 tagused[tagnum] = 1;
831                 depths[swf_GetDepth(tag)] = 1;
832             }
833         }
834         else if(tag->id == ST_SHOWFRAME) {
835             frame ++;
836             if(hollow) {
837                 tagused[tagnum] = 1;
838                 found = 1;
839             }
840         }
841         
842         if(tag->id == ST_DEFINESPRITE) {
843             while(tag->id != ST_END) { 
844                 tag = tag->next;
845                 tagnum ++;
846             }
847         }
848         tag = tag->next;
849         tagnum ++;
850     }
851     if (found)
852         extractTag(&swf, destfilename);
853
854     if(mp3file)
855         fclose(mp3file);
856
857     swf_FreeTags(&swf);
858     return 0;
859 }
860