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> */
8 #include "../../config.h"
14 #include "../rfxswf.h"
16 #include "h263tables.h"
18 static char * filename = 0;
19 static char * indent = " ";
23 struct options_t options[] =
35 int args_callback_option(char*name,char*val)
37 if(!strcmp(name, "V")) {
38 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
41 else if(name[0]=='d') {
45 else if(name[0]=='v') {
50 printf("Unknown option: -%s\n", name);
56 int args_callback_longoption(char*name,char*val)
58 return args_long2shortoption(options, name, val);
60 void args_callback_usage(char*name)
62 printf("Usage: %s [-at] file.swf\n", name);
63 printf("\t-h , --help\t\t Print help and exit\n");
64 printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
65 printf("\t-v , --verbose\t\t Print debugging information\n");
66 printf("\t-V , --version\t\t Print program version and exit\n");
68 int args_callback_command(char*name,char*val)
71 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
78 #define DEBUG if(debug)
80 void handleVideoStream(TAG*tag, char*prefix)
82 U16 id = swf_GetU16(tag);
83 U16 frames = swf_GetU16(tag);
84 U16 width = swf_GetU16(tag);
85 U16 height = swf_GetU16(tag);
86 U8 flags = swf_GetU8(tag); //5-2(videopacket 01=off 10=on)-1(smoothing 1=on)
87 U8 codec = swf_GetU8(tag);
88 printf(" (%d frames, %dx%d", frames, width, height);
92 printf(" sorenson h.263)");
94 printf(" codec 0x%02x)", codec);
97 int checkhufftable(struct huffcode*code, char*name)
100 while(code[t].code) {
102 if(strlen(code[t].code)!=code[t].len) {
103 printf("len mismatch in %s, index %d\n", name, t);
106 if(t != code[t].index) {
107 printf("index mismatch in %s, index %d\n", name, t);
110 while(code[s].code) {
111 char*a = code[s].code;
112 char*b = code[t].code;
115 if(s==t) {s++;continue;}
116 if(code[t].len < code[s].len) {
121 if(!strncmp(a,b,strlen(a))) {
122 printf("index %d (%s) is prefix of %d (%s)\n", ai, a, bi, b);
136 struct hufftree*left;//0
137 struct hufftree*right;//1
141 struct hufftree * rle_tree;
142 struct hufftree * mcbpc_intra_tree;
143 struct hufftree * mcbpc_inter_tree;
144 struct hufftree * cbpy_tree;
145 struct hufftree * mvd_tree;
147 static void insert(struct hufftree*tree, char*code, int index)
150 assert(!tree->left); //shannon conditional
151 assert(!tree->right);
159 tree->left = (struct hufftree*)malloc(sizeof(struct hufftree));
160 memset(tree->left, 0, sizeof(struct hufftree));
161 tree->left->index = -1;
163 insert(tree->left, code+1, index);
166 assert(code[0] == '1');
168 tree->right = (struct hufftree*)malloc(sizeof(struct hufftree));
169 memset(tree->right, 0, sizeof(struct hufftree));
170 tree->right->index = -1;
172 insert(tree->right, code+1, index);
177 struct hufftree* huffcode2tree(struct huffcode*code)
179 struct hufftree* t = malloc(sizeof(struct hufftree));
180 memset(t, 0, sizeof(struct hufftree));
183 insert(t, code->code, code->index);
189 int gethuffvalue(TAG*tag, struct huffcode*code)
195 bits[len] = swf_GetBits(tag, 1)+0x30;
198 while(code[t].code) {
199 if(!strcmp(bits, code[t].code))
202 if(code[t].len >= len)
207 printf("error: %s\n", bits);
208 while(tag->pos < tag->len && nr<80) {
209 int b = swf_GetBits(tag, 1);
221 /*type = 0; // mb-type =3, cbpc = 00
223 printf("can't handle i-frame mcbpc bits %d yet\n", bit);
225 bit = swf_GetBits(tag, 1);
226 type = 0; // mb-type =0, cbpc = 00
228 bit = swf_GetBits(tag, 2);
229 type = 8; // mb-type =2, cbpc = 00
231 printf("can't handle p-frame mcbpc bits 0-%d yet\n", bit);
237 int gethuffvalue2(TAG*tag, struct huffcode*code, struct hufftree*tree)
243 if(!swf_GetBits(tag, 1)) {
253 void get_DC_TCOEF(TAG*tag, int t, int has_dc, int has_tcoef)
256 int ac;// = swf_GetBits();
259 //printf("DC:%d\n", dc);
261 dc = swf_GetBits(tag, 8);
262 if(dc == 0 || dc == 128) {
263 printf("error: dc=%d\n", dc);
266 DEBUG printf(" %d ", dc);
276 index = gethuffvalue2(tag, rle, rle_tree);
277 last = rle_params[index].last;
278 run = rle_params[index].run;
279 level = rle_params[index].level;
280 //DEBUG printf("index:%d\n", index);
281 if(index == RLE_ESCAPE) {
282 last = swf_GetBits(tag, 1);
283 run = swf_GetBits(tag, 6);
284 level = swf_GetBits(tag, 8);
286 DEBUG printf("(%d) E%d", run, level);
289 if(level == 0 || level == 128) {
290 printf("error: level=%d\n", level);
293 level = (int)((signed char)level);
295 int sign = swf_GetBits(tag, 1);
299 DEBUG printf("(%d) %s%d", run, level>0?"+":"",level);
301 DEBUG printf("%s%d", level>0?"+":"",level);
304 //DEBUG printf("run:%d level:%d\n", run, level);
306 DEBUG printf("] pos: %d", pos);
308 printf("\nerror:bad pos (%d)\n", pos);
319 int index = gethuffvalue2(tag, mvd, mvd_tree);
320 DEBUG printf("mvd index:%d\n", index);
324 char has_quant[] = {0,1,0,0,1};
325 char has_mvd[] = {1,1,3,0,0};
332 void decode_block(TAG*tag, int pictype)
335 int mb_type = -1, cbpc = -1;
337 int cbpy_index, cbpy_value;
340 if(pictype == TYPE_INTER) /* non-intra pictures have a cod flag */
342 int cod = swf_GetBits(tag, 1);
343 DEBUG printf("cod=%d\n",cod);
353 if(pictype == TYPE_INTRA) { /* I-frame */
354 type = gethuffvalue2(tag, mcbpc_intra, mcbpc_intra_tree);
355 DEBUG printf("mcbpc=%d\n",type);
356 mb_type = mcbpc_intra_params[type].mb_type;
357 cbpc = mcbpc_intra_params[type].cbpc;
358 if(type == MCBPC_INTRA_STUFFING) {
359 printf("stuffing not supported yet!\n");
360 exit(1); //TODO: goto COD
363 else if(pictype == 1) { /* P-frame */
364 type = gethuffvalue2(tag, mcbpc_inter, mcbpc_inter_tree);
365 DEBUG printf("mcbpc=%d\n",type);
366 mb_type = mcbpc_inter_params[type].mb_type;
367 cbpc = mcbpc_inter_params[type].cbpc;
368 if(type == MCBPC_INTER_STUFFING) {
369 printf("stuffing not supported yet!(2)\n");
370 exit(1); //TODO: goto COD
374 if(mb_type == 3 || mb_type == 4)
379 printf("%c", "vqVii"[mb_type]);
381 DEBUG printf("mcbpc type: %d mb_type:%d cbpc:%d\n", type, mb_type, cbpc);
385 cbpy_index = gethuffvalue2(tag, cbpy, cbpy_tree);
386 cbpy_value = cbpy_index;
389 DEBUG printf("cbpy value:%d (%d)\n", cbpy_value, cbpy_index);
392 /* I 0: 00 mcbpc/cbpy
393 P 0: 00 cod/mcbpc/cbpy/mvd
394 P 6: 10 cod/mcbpc/cbpy/dquant/mvd
395 P 8: 00 cod/mcbpc/cbpy/mvd/mvd24
399 if(has_quant[mb_type]) {
400 dquant = swf_GetBits(tag, 2);
401 if(dquant == 0) dquant = -1;
402 else if(dquant == 1) dquant = -2;
403 else if(dquant == 2) dquant = +1;
404 else if(dquant == 3) dquant = +2;
405 DEBUG printf("dquant: %d\n", dquant);
408 if(has_mvd[mb_type]&1) {
410 x = readMVD(tag); //horizontal
411 y = readMVD(tag); //vertical
415 if(has_mvd[mb_type]&2) {
416 /* only in advanced prediction mode */
417 readMVD(tag); //horizontal
418 readMVD(tag); //vertical
419 readMVD(tag); //horizontal
420 readMVD(tag); //vertical
421 readMVD(tag); //horizontal
422 readMVD(tag); //vertical
426 int has_intradc = intrablock;
427 int has_tcoef = cbpy_value & (8>>t);
428 DEBUG printf("luminance%d ", t);
429 get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*luminance - affected by cbpy*/
433 int has_intradc = intrablock;
434 int has_tcoef = cbpc & (2>>t);
435 DEBUG printf("chrominance%d ", t);
436 get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*chrominance - affected by mcbc*/
441 void handleVideoFrame(TAG*tag, char*prefix)
443 U32 code, version, reference, sizeflags;
445 U8 blocktype, pictype;
446 U16 id = swf_GetU16(tag);
447 U16 frame = swf_GetU16(tag);
448 U8 deblock,flags, tmp, bit;
449 U32 quantizer, extrainfo;
456 char*types[] = {"intra- (I-)frame", "inter- (P-)frame", "disposable interframe", "<reserved>"};
457 printf("============================= frame %d ===================================", frame);
459 /* video packet follows */
461 code = swf_GetBits(tag, 17);
463 printf("code: %x (?)\n", code);
466 version = swf_GetBits(tag, 5); /*actually, part of the picture start code */
467 //printf("version: %x\n", version); /*usually 0*/
469 /* version 1 has some different transform coefficient coding which we
471 printf("spark version %d not supported yet\n", version);
474 reference = swf_GetBits(tag, 8);
475 DEBUG printf("reference: %d\n", reference); /*usually same as frame number (unless frames were skipped while encoding)*/
477 sizeflags = swf_GetBits(tag, 3);
480 case 0: width = swf_GetBits(tag,8); height = swf_GetBits(tag,8); break;
481 case 1: width = swf_GetBits(tag, 16); height = swf_GetBits(tag, 16); break;
482 case 2: width = 352; height = 288; break;
483 case 3: width = 176; height = 144; break;
484 case 4: width = 128; height = 96; break;
485 case 5: width = 320; height = 240; break;
486 case 6: width = 160; height = 120; break;
487 case 7: width = -1; height = -1;/*reserved*/ break;
490 pictype = swf_GetBits(tag, 2);
492 printf("error: unknown pictype: %d\n", pictype);
496 pictype = TYPE_INTER;
499 if(pictype==TYPE_INTER)
500 printf("INTER P%s", disposable?" (disposeable)":"");
504 deblock = swf_GetBits(tag, 1); /*usually 0*/
505 DEBUG printf("deblock: %d\n", deblock);
506 quantizer = swf_GetBits(tag, 5); /*usually 9*/
507 DEBUG printf("quantizer: %d\n", quantizer);
509 extrainfo = swf_GetBits(tag, 1); /*usually none */
511 extrainfo = swf_GetBits(tag, 8);
512 printf("extrainfo: %02x\n", extrainfo);
513 extrainfo = swf_GetBits(tag, 1);
518 bby = (height+15)/16;
520 printf("%dx%d [blocks: %dx%d=%d]\n", width, height, bbx,bby, blocknum);
522 /*if(pictype == TYPE_INTER)
524 /*if(pictype == TYPE_INTRA)
532 for(by=0;by<bby;by++)
534 for(bx=0;bx<bbx;bx++)
536 decode_block(tag, pictype);
542 void hexdumpTag(TAG*tag, char* prefix)
545 printf(" %s-=> ",prefix);
546 for(t=0;t<tag->len;t++) {
547 printf("%02x ", tag->data[t]);
548 if((t && ((t&15)==15)) || (t==tag->len-1))
553 printf("\n %s-=> ",prefix);
558 int main (int argc,char ** argv)
567 checkhufftable(rle, "rle");
568 checkhufftable(mcbpc_intra, "intra");
569 checkhufftable(mcbpc_inter, "inter");
570 checkhufftable(cbpy, "cbpy");
571 checkhufftable(mvd, "mvd");
573 rle_tree = huffcode2tree(rle);
574 mcbpc_intra_tree = huffcode2tree(mcbpc_intra);
575 mcbpc_inter_tree = huffcode2tree(mcbpc_inter);
576 cbpy_tree = huffcode2tree(cbpy);
577 mvd_tree = huffcode2tree(mvd);
579 processargs(argc, argv);
581 fprintf(stderr, "You must supply a filename.\n");
585 f = open(filename,O_RDONLY);
588 perror("Couldn't open file: ");
591 if FAILED(swf_ReadSWF(f,&swf)) {
592 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
599 printf("[HEADER] File version: %d\n", swf.fileVersion);
601 printf("[HEADER] File is zlib compressed.");
602 if(filesize && swf.fileSize)
603 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
607 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
608 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
609 printf("[HEADER] Frame count: %d\n",swf.frameCount);
610 printf("[HEADER] Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
611 if(swf.movieSize.xmin)
612 printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
615 printf("[HEADER] Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
616 if(swf.movieSize.ymin)
617 printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
624 char*name = swf_TagGetName(tag);
626 //printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
628 if(swf_isDefiningTag(tag)) {
629 U16 id = swf_GetDefineID(tag);
630 //printf(" defines id %04d", id);
632 else if(swf_isPseudoDefiningTag(tag)) {
633 U16 id = swf_GetDefineID(tag);
634 //printf(" adds information to id %04d", id);
637 if(tag->id == ST_VIDEOFRAME) {
638 handleVideoFrame(tag, myprefix);
641 else if(tag->id == ST_DEFINEVIDEOSTREAM) {
642 handleVideoStream(tag, myprefix);
649 sprintf(myprefix, " %s", prefix);
651 if(tag->len && hex) {
652 hexdumpTag(tag, prefix);