2 Shows the structure of a swf file containing video
4 Part of the swftools package.
6 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org> */
13 #include "../lib/rfxswf.h"
14 #include "../lib/args.h"
16 static char * filename = 0;
17 static char * indent = " ";
20 struct options_t options[] =
32 int args_callback_option(char*name,char*val)
34 if(!strcmp(name, "V")) {
35 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
38 else if(name[0]=='d') {
43 printf("Unknown option: -%s\n", name);
49 int args_callback_longoption(char*name,char*val)
51 return args_long2shortoption(options, name, val);
53 void args_callback_usage(char*name)
55 printf("Usage: %s [-at] file.swf\n", name);
56 printf("\t-h , --help\t\t Print help and exit\n");
57 printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
58 printf("\t-V , --version\t\t Print program version and exit\n");
60 int args_callback_command(char*name,char*val)
63 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
72 void handleVideoStream(TAG*tag, char*prefix)
74 U16 id = swf_GetU16(tag);
75 U16 frames = swf_GetU16(tag);
76 U16 width = swf_GetU16(tag);
77 U16 height = swf_GetU16(tag);
78 U8 flags = swf_GetU8(tag); //5-2(videopacket 01=off 10=on)-1(smoothing 1=on)
79 U8 codec = swf_GetU8(tag);
80 printf(" (%d frames, %dx%d", frames, width, height);
84 printf(" sorenson h.263)");
86 printf(" codec 0x%02x)", codec);
95 #define MCBPC_INTRA_STUFFING 8
97 struct huffcode mcbpc_intra[] = {
98 {"1",1, 0}, /*cbpc-00 mb type 3*/
99 {"001",3, 1}, /*cbpc-01 mb type 3*/
100 {"010",3, 2}, /*cbpc-10 mb type 3*/
101 {"011",3, 3}, /*cbpc-11 mb type 3*/
102 {"0001",4, 4}, /*cbpc-00 mb type 4*/
103 {"000001",6, 5}, /*cbpc-01 mb type 4*/
104 {"000010",6, 6}, /*cbpc-10 mb type 4*/
105 {"000011",6, 7}, /*cbpc-11 mb type 4*/
106 {"000000001",9, 8}, /* stuffing */
110 struct mcbpc_intra_params
115 } mcbpc_intra_params[] =
127 #define MCBPC_INTER_STUFFING 20
129 struct huffcode mcbpc_inter[] = {
147 {"000000100", 9, 17},
148 {"000000011", 9, 18},
149 {"000000010", 9, 19},
150 {"000000001", 9, 20}, /*stuffing*/
153 {"00000000010", 11, 21},
154 {"0000000001100", 13, 22},
155 {"0000000001110", 13, 23},
156 {"0000000001111", 13, 24},
161 struct mcbpc_inter_params
166 } mcbpc_inter_params[] =
167 {{0, 0, 0}, {1, 0, 1}, {2, 0, 2}, {3, 0, 3}, {4, 1, 0},
168 {5, 1, 1}, {6, 1, 2}, {7, 1, 3}, {8, 2, 0}, {9, 2, 1},
169 {10, 2, 2}, {11, 2, 3}, {12, 3, 0}, {13, 3, 1}, {14, 3, 2},
170 {15, 3, 3}, {16, 4, 0}, {17, 4, 1}, {18, 4, 2}, {19, 4, 3},
171 {20, -1, -1}, //stuffing
172 //{21, 5, 0}, {22, 5, 1}, {23, 5, 2}, {24, 5, 3}, //h.263+ only
175 struct huffcode cbpy[] = {
195 struct huffcode mvd[] =
197 {"0000000000101", 13, 0},
198 {"0000000000111", 13, 1},
199 {"000000000101", 12, 2},
200 {"000000000111", 12, 3},
201 {"000000001001", 12, 4},
202 {"000000001011", 12, 5},
203 {"000000001101", 12, 6},
204 {"000000001111", 12, 7},
205 {"00000001001", 11, 8},
206 {"00000001011", 11, 9},
207 {"00000001101", 11, 10},
208 {"00000001111", 11, 11},
209 {"00000010001", 11, 12},
210 {"00000010011", 11, 13},
211 {"00000010101", 11, 14},
212 {"00000010111", 11, 15},
213 {"00000011001", 11, 16},
214 {"00000011011", 11, 17},
215 {"00000011101", 11, 18},
216 {"00000011111", 11, 19},
217 {"00000100001", 11, 20},
218 {"00000100011", 11, 21},
219 {"0000010011", 10, 22},
220 {"0000010101", 10, 23},
221 {"0000010111", 10, 24},
237 {"0000010110", 10, 40},
238 {"0000010100", 10, 41},
239 {"0000010010", 10, 42},
240 {"00000100010", 11, 43},
241 {"00000100000", 11, 44},
242 {"00000011110", 11, 45},
243 {"00000011100", 11, 46},
244 {"00000011010", 11, 47},
245 {"00000011000", 11, 48},
246 {"00000010110", 11, 49},
247 {"00000010100", 11, 50},
248 {"00000010010", 11, 51},
249 {"00000010000", 11, 52},
250 {"00000001110", 11, 53},
251 {"00000001100", 11, 54},
252 {"00000001010", 11, 55},
253 {"00000001000", 11, 56},
254 {"000000001110", 12, 57},
255 {"000000001100", 12, 58},
256 {"000000001010", 12, 59},
257 {"000000001000", 12, 60},
258 {"000000000110", 12, 61},
259 {"000000000100", 12, 62},
260 {"0000000000110", 13, 63},
263 struct huffcode rle[] =
272 {"0000100001", 10, 7},
273 {"0000100000", 10, 8},
274 {"00000000111", 11, 9},
275 {"00000000110", 11, 10},
276 {"00000100000", 11, 11},
280 {"0000001111", 10, 15},
281 {"00000100001", 11, 16},
282 {"000001010000", 12, 17},
285 {"0000001110", 10, 20},
286 {"000001010001", 12, 21},
288 {"000100011", 9, 23},
289 {"0000001101", 10, 24},
291 {"000100010", 9, 26},
292 {"000001010010", 12, 27},
294 {"0000001100", 10, 29},
295 {"000001010011", 12, 30},
297 {"0000001011", 10, 32},
298 {"000001010100", 12, 33},
300 {"0000001010", 10, 35},
302 {"0000001001", 10, 37},
304 {"0000001000", 10, 39},
306 {"000001010101", 12, 41},
311 {"000100001", 9, 46},
312 {"000100000", 9, 47},
313 {"000011111", 9, 48},
314 {"000011110", 9, 49},
315 {"000011101", 9, 50},
316 {"000011100", 9, 51},
317 {"000011011", 9, 52},
318 {"000011010", 9, 53},
319 {"00000100010", 11, 54},
320 {"00000100011", 11, 55},
321 {"000001010110", 12, 56},
322 {"000001010111", 12, 57},
324 {"000011001", 9, 59},
325 {"00000000101", 11, 60},
327 {"00000000100", 11, 62},
343 {"000011000", 9, 78},
344 {"000010111", 9, 79},
345 {"000010110", 9, 80},
346 {"000010101", 9, 81},
347 {"000010100", 9, 82},
348 {"000010011", 9, 83},
349 {"000010010", 9, 84},
350 {"000010001", 9, 85},
351 {"0000000111", 10, 86},
352 {"0000000110", 10, 87},
353 {"0000000101", 10, 88},
354 {"0000000100", 10, 89},
355 {"00000100100", 11, 90},
356 {"00000100101", 11, 91},
357 {"00000100110", 11, 92},
358 {"00000100111", 11, 93},
359 {"000001011000", 12, 94},
360 {"000001011001", 12, 95},
361 {"000001011010", 12, 96},
362 {"000001011011", 12, 97},
363 {"000001011100", 12, 98},
364 {"000001011101", 12, 99},
365 {"000001011110", 12, 100},
366 {"000001011111", 12, 101},
367 {"0000011", 7, 102}, /*ESCAPE*/
371 #define RLE_ESCAPE 102
380 {{0,0,0,1}, {1,0,0,2}, {2,0,0,3}, {3,0,0,4}, {4,0,0,5}, {5,0,0,6}, {6,0,0,7},
381 {7,0,0,8}, {8,0,0,9}, {9,0,0,10}, {10,0,0,11}, {11,0,0,12}, {12,0,1,1}, {13,0,1,2},
382 {14,0,1,3}, {15,0,1,4}, {16,0,1,5}, {17,0,1,6}, {18,0,2,1}, {19,0,2,2}, {20,0,2,3}, {21,0,2,4},
383 {22,0,3,1}, {23,0,3,2}, {24,0,3,3}, {25,0,4,1}, {26,0,4,2}, {27,0,4,3}, {28,0,5,1},
384 {29,0,5,2}, {30,0,5,3}, {31,0,6,1}, {32,0,6,2}, {33,0,6,3},
385 {34,0,7,1}, {35,0,7,2}, {36,0,8,1}, {37,0,8,2}, {38,0,9,1}, {39,0,9,2}, {40,0,10,1},
386 {41,0,10,2}, {42,0,11,1}, {43,0,12,1}, {44,0,13,1}, {45,0,14,1}, {46,0,15,1},
387 {47,0,16,1}, {48,0,17,1}, {49,0,18,1}, {50,0,19,1}, {51,0,20,1}, {52,0,21,1},
388 {53,0,22,1}, {54,0,23,1}, {55,0,24,1}, {56,0,25,1}, {57,0,26,1}, {58,1,0,1},
389 {59,1,0,2}, {60,1,0,3}, {61,1,1,1}, {62,1,1,2}, {63,1,2,1}, {64,1,3,1}, {65, 1,4,1},
390 {66,1,5,1}, {67,1,6,1}, {68,1,7,1}, {69,1,8,1}, {70,1,9,1}, {71,1,10,1},
391 {72,1,11,1}, {73,1,12,1}, {74,1,13,1}, {75,1,14,1}, {76,1,15,1}, {77,1,16,1},
392 {78,1,17,1}, {79,1,18,1}, {80,1,19,1}, {81,1,20,1}, {82,1,21,1}, {83,1,22,1},
393 {84,1,23,1}, {85,1,24,1}, {86,1,25,1}, {87,1,26,1}, {88,1,27,1}, {89,1,28,1},
394 {90,1,29,1}, {91,1,30,1}, {92,1,31,1}, {93,1,32,1}, {94,1,33,1}, {95,1,34,1},
395 {96,1,35,1}, {97,1,36,1}, {98,1,37,1}, {99,1,38,1}, {100,1,39,1}, {101,1,40,1},
399 int checkhufftable(struct huffcode*code, char*name)
402 while(code[t].code) {
404 if(strlen(code[t].code)!=code[t].len) {
405 printf("len mismatch in %s, index %d\n", name, t);
408 if(t != code[t].index) {
409 printf("index mismatch in %s, index %d\n", name, t);
412 while(code[s].code) {
413 char*a = code[s].code;
414 char*b = code[t].code;
417 if(s==t) {s++;continue;}
418 if(code[t].len < code[s].len) {
423 if(!strncmp(a,b,strlen(a))) {
424 printf("index %d (%s) is prefix of %d (%s)\n", ai, a, bi, b);
435 int gethuffvalue(TAG*tag, struct huffcode*code)
441 bits[len] = swf_GetBits(tag, 1)+0x30;
444 while(code[t].code) {
445 if(!strcmp(bits, code[t].code))
448 if(code[t].len >= len)
453 printf("error: %s\n", bits);
454 while(tag->pos < tag->len && nr<80) {
455 int b = swf_GetBits(tag, 1);
467 /*type = 0; // mb-type =3, cbpc = 00
469 printf("can't handle i-frame mcbpc bits %d yet\n", bit);
471 bit = swf_GetBits(tag, 1);
472 type = 0; // mb-type =0, cbpc = 00
474 bit = swf_GetBits(tag, 2);
475 type = 8; // mb-type =2, cbpc = 00
477 printf("can't handle p-frame mcbpc bits 0-%d yet\n", bit);
483 void getCoefficient(TAG*tag, int t, int has_dc, int has_tcoef)
486 int ac;// = swf_GetBits();
489 //printf("DC:%d\n", dc);
491 dc = swf_GetBits(tag, 8);
492 if(dc == 0 || dc == 128) {
493 printf("error: dc=%d\n", dc);
496 DEBUG printf(" %d ", dc);
506 index = gethuffvalue(tag, rle);
507 last = rle_params[index].last;
508 run = rle_params[index].run;
509 level = rle_params[index].level;
510 //DEBUG printf("index:%d\n", index);
511 if(index == RLE_ESCAPE) {
512 last = swf_GetBits(tag, 1);
513 run = swf_GetBits(tag, 6);
514 level = swf_GetBits(tag, 8);
516 DEBUG printf("(%d) E%d", run, level);
519 if(level == 0 || level == 128) {
520 printf("error: level=%d\n", level);
523 level = (int)((signed char)level);
525 int sign = swf_GetBits(tag, 1);
529 DEBUG printf("(%d) %s%d", run, level>0?"+":"",level);
531 DEBUG printf("%s%d", level>0?"+":"",level);
534 //DEBUG printf("run:%d level:%d\n", run, level);
536 DEBUG printf("] pos: %d", pos);
547 void readMVD(TAG*tag)
549 int index = gethuffvalue(tag, mvd);
550 DEBUG printf("mvd index:%d\n", index);
553 char has_quant[] = {0,1,0,0,1};
554 char has_mvd[] = {1,1,3,0,0};
561 void decode_block(TAG*tag, int pictype)
564 int mb_type = -1, cbpc = -1;
566 int cbpy_index, cbpy_value;
569 if(pictype == TYPE_INTER) /* non-intra pictures have a cod flag */
570 /* TODO: according to the flash spec, this field is always present */
572 int cod = swf_GetBits(tag, 1);
582 if(pictype == TYPE_INTRA) { /* I-frame */
583 type = gethuffvalue(tag, mcbpc_intra);
584 mb_type = mcbpc_intra_params[type].mb_type;
585 cbpc = mcbpc_intra_params[type].cbpc;
586 if(type == MCBPC_INTRA_STUFFING)
587 exit(1); //TODO: goto COD
589 else if(pictype == 1) { /* P-frame */
590 type = gethuffvalue(tag, mcbpc_inter);
591 mb_type = mcbpc_inter_params[type].mb_type;
592 cbpc = mcbpc_inter_params[type].cbpc;
593 if(type == MCBPC_INTER_STUFFING)
594 exit(1); //TODO: goto COD
597 if(mb_type == 3 || mb_type == 4)
602 printf("%d", intrablock);
604 DEBUG printf("mcbpc type: %d mb_type:%d cbpc:%d\n", type, mb_type, cbpc);
608 cbpy_index = gethuffvalue(tag, cbpy);
609 cbpy_value = cbpy_index;
612 DEBUG printf("cbpy value:%d (%d)\n", cbpy_value, cbpy_index);
615 /* I 0: 00 mcbpc/cbpy
616 P 0: 00 cod/mcbpc/cbpy/mvd
617 P 6: 10 cod/mcbpc/cbpy/dquant/mvd
618 P 8: 00 cod/mcbpc/cbpy/mvd/mvd24
622 if(has_quant[mb_type]) {
623 dbquant = swf_GetBits(tag, 2);
624 DEBUG printf("quantizer: %d\n", dbquant);
627 if(has_mvd[mb_type]&1) {
628 readMVD(tag); //horizontal
629 readMVD(tag); //vertical
631 if(has_mvd[mb_type]&2) {
632 /* only in advanced prediction mode */
633 readMVD(tag); //horizontal
634 readMVD(tag); //vertical
635 readMVD(tag); //horizontal
636 readMVD(tag); //vertical
637 readMVD(tag); //horizontal
638 readMVD(tag); //vertical
642 int has_intradc = intrablock;
643 int has_tcoef = cbpy_value & (8>>t);
644 DEBUG printf("luminance%d ", t);
645 getCoefficient(tag, t, has_intradc, has_tcoef); /*luminance - affected by cbpy*/
649 int has_intradc = intrablock;
650 int has_tcoef = cbpc & (2>>t);
651 DEBUG printf("chrominance%d ", t);
652 getCoefficient(tag, t, has_intradc, has_tcoef); /*chrominance - affected by mcbc*/
657 void handleVideoFrame(TAG*tag, char*prefix)
659 U32 code, version, reference, sizeflags;
661 U8 blocktype, pictype;
662 U16 id = swf_GetU16(tag);
663 U16 frame = swf_GetU16(tag);
664 U8 deblock,flags, tmp, bit;
665 U32 quantizer, extrainfo;
672 char*types[] = {"intra- (I-)frame", "inter- (P-)frame", "disposable interframe", "<reserved>"};
673 printf("============================= frame %d ===================================", frame);
675 /* video packet follows */
677 code = swf_GetBits(tag, 17);
679 printf("code: %x (?)\n", code);
682 version = swf_GetBits(tag, 5); /*actually, part of the picture start code */
683 //printf("version: %x\n", version); /*usually 0*/
685 /* version 1 has some different transform coefficient coding which we
689 reference = swf_GetBits(tag, 8);
690 DEBUG printf("reference: %d\n", reference); /*usually same as frame number (unless frames were skipped while encoding)*/
692 sizeflags = swf_GetBits(tag, 3);
695 case 0: width = swf_GetU8(tag); height = swf_GetU8(tag); break;
696 case 1: width = swf_GetBits(tag, 16); height = swf_GetBits(tag, 16); break;
697 case 2: width = 352; height = 288; break;
698 case 3: width = 176; height = 144; break;
699 case 4: width = 128; height = 96; break;
700 case 5: width = 320; height = 240; break;
701 case 6: width = 160; height = 120; break;
702 case 7: width = -1; height = -1;/*reserved*/ break;
705 pictype = swf_GetBits(tag, 2);
707 printf("pictype: %d\n", pictype);
711 pictype = TYPE_INTER;
714 if(pictype==TYPE_INTER)
715 printf("INTER P%s", disposable?" (disposeable)":"");
719 deblock = swf_GetBits(tag, 1); /*usually 0*/
720 DEBUG printf("deblock: %d\n", deblock);
721 quantizer = swf_GetBits(tag, 5); /*usually 9*/
722 DEBUG printf("quantizer: %d\n", quantizer);
724 extrainfo = swf_GetBits(tag, 1); /*usually none */
726 extrainfo = swf_GetBits(tag, 8);
727 printf("extrainfo: %02x\n", extrainfo);
728 extrainfo = swf_GetBits(tag, 1);
733 bby = (height+15)/16;
735 printf("%dx%d [blocks: %dx%d=%d]\n", width, height, bbx,bby, blocknum);
737 /*if(pictype == TYPE_INTER)
739 /*if(pictype == TYPE_INTRA)
747 for(by=0;by<bby;by++)
749 for(bx=0;bx<bbx;bx++)
751 decode_block(tag, pictype);
757 void hexdumpTag(TAG*tag, char* prefix)
760 printf(" %s-=> ",prefix);
761 for(t=0;t<tag->len;t++) {
762 printf("%02x ", tag->data[t]);
763 if((t && ((t&15)==15)) || (t==tag->len-1))
768 printf("\n %s-=> ",prefix);
773 int main (int argc,char ** argv)
782 checkhufftable(rle, "rle");
783 checkhufftable(mcbpc_intra, "intra");
784 checkhufftable(mcbpc_inter, "inter");
785 checkhufftable(cbpy, "cbpy");
786 checkhufftable(mvd, "mvd");
788 processargs(argc, argv);
790 fprintf(stderr, "You must supply a filename.\n");
794 f = open(filename,O_RDONLY);
797 perror("Couldn't open file: ");
800 if FAILED(swf_ReadSWF(f,&swf)) {
801 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
808 printf("[HEADER] File version: %d\n", swf.fileVersion);
810 printf("[HEADER] File is zlib compressed.");
811 if(filesize && swf.fileSize)
812 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
816 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
817 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
818 printf("[HEADER] Frame count: %d\n",swf.frameCount);
819 printf("[HEADER] Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
820 if(swf.movieSize.xmin)
821 printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
824 printf("[HEADER] Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
825 if(swf.movieSize.ymin)
826 printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
833 char*name = swf_TagGetName(tag);
835 //printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
837 if(swf_isDefiningTag(tag)) {
838 U16 id = swf_GetDefineID(tag);
839 //printf(" defines id %04d", id);
841 else if(swf_isPseudoDefiningTag(tag)) {
842 U16 id = swf_GetDefineID(tag);
843 //printf(" adds information to id %04d", id);
846 if(tag->id == ST_VIDEOFRAME) {
847 handleVideoFrame(tag, myprefix);
850 else if(tag->id == ST_DEFINEVIDEOSTREAM) {
851 handleVideoStream(tag, myprefix);
858 sprintf(myprefix, " %s", prefix);
860 if(tag->len && hex) {
861 hexdumpTag(tag, prefix);