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 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 #include "../../config.h"
28 #include "../rfxswf.h"
30 #include "h263tables.h"
32 static char * filename = 0;
33 static char * indent = " ";
37 struct options_t options[] =
49 int args_callback_option(char*name,char*val)
51 if(!strcmp(name, "V")) {
52 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
55 else if(name[0]=='d') {
59 else if(name[0]=='v') {
64 printf("Unknown option: -%s\n", name);
70 int args_callback_longoption(char*name,char*val)
72 return args_long2shortoption(options, name, val);
74 void args_callback_usage(char*name)
76 printf("Usage: %s [-at] file.swf\n", name);
77 printf("\t-h , --help\t\t Print help and exit\n");
78 printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
79 printf("\t-v , --verbose\t\t Print debugging information\n");
80 printf("\t-V , --version\t\t Print program version and exit\n");
82 int args_callback_command(char*name,char*val)
85 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
92 #define DEBUG if(debug)
94 void handleVideoStream(TAG*tag, char*prefix)
96 U16 id = swf_GetU16(tag);
97 U16 frames = swf_GetU16(tag);
98 U16 width = swf_GetU16(tag);
99 U16 height = swf_GetU16(tag);
100 U8 flags = swf_GetU8(tag); //5-2(videopacket 01=off 10=on)-1(smoothing 1=on)
101 U8 codec = swf_GetU8(tag);
102 printf(" (%d frames, %dx%d", frames, width, height);
106 printf(" sorenson h.263)");
108 printf(" codec 0x%02x)", codec);
111 int checkhufftable(struct huffcode*code, char*name)
114 while(code[t].code) {
116 if(strlen(code[t].code)!=code[t].len) {
117 printf("len mismatch in %s, index %d\n", name, t);
120 if(t != code[t].index) {
121 printf("index mismatch in %s, index %d\n", name, t);
124 while(code[s].code) {
125 char*a = code[s].code;
126 char*b = code[t].code;
129 if(s==t) {s++;continue;}
130 if(code[t].len < code[s].len) {
135 if(!strncmp(a,b,strlen(a))) {
136 printf("index %d (%s) is prefix of %d (%s)\n", ai, a, bi, b);
150 struct hufftree*left;//0
151 struct hufftree*right;//1
155 struct hufftree * rle_tree;
156 struct hufftree * mcbpc_intra_tree;
157 struct hufftree * mcbpc_inter_tree;
158 struct hufftree * cbpy_tree;
159 struct hufftree * mvd_tree;
161 static void insert(struct hufftree*tree, char*code, int index)
164 assert(!tree->left); //shannon conditional
165 assert(!tree->right);
173 tree->left = (struct hufftree*)malloc(sizeof(struct hufftree));
174 memset(tree->left, 0, sizeof(struct hufftree));
175 tree->left->index = -1;
177 insert(tree->left, code+1, index);
180 assert(code[0] == '1');
182 tree->right = (struct hufftree*)malloc(sizeof(struct hufftree));
183 memset(tree->right, 0, sizeof(struct hufftree));
184 tree->right->index = -1;
186 insert(tree->right, code+1, index);
191 struct hufftree* huffcode2tree(struct huffcode*code)
193 struct hufftree* t = malloc(sizeof(struct hufftree));
194 memset(t, 0, sizeof(struct hufftree));
197 insert(t, code->code, code->index);
203 int gethuffvalue(TAG*tag, struct huffcode*code)
209 bits[len] = swf_GetBits(tag, 1)+0x30;
212 while(code[t].code) {
213 if(!strcmp(bits, code[t].code))
216 if(code[t].len >= len)
221 printf("error: %s\n", bits);
222 while(tag->pos < tag->len && nr<80) {
223 int b = swf_GetBits(tag, 1);
235 /*type = 0; // mb-type =3, cbpc = 00
237 printf("can't handle i-frame mcbpc bits %d yet\n", bit);
239 bit = swf_GetBits(tag, 1);
240 type = 0; // mb-type =0, cbpc = 00
242 bit = swf_GetBits(tag, 2);
243 type = 8; // mb-type =2, cbpc = 00
245 printf("can't handle p-frame mcbpc bits 0-%d yet\n", bit);
251 int gethuffvalue2(TAG*tag, struct huffcode*code, struct hufftree*tree)
257 if(!swf_GetBits(tag, 1)) {
267 void get_DC_TCOEF(TAG*tag, int t, int has_dc, int has_tcoef)
270 int ac;// = swf_GetBits();
273 //printf("DC:%d\n", dc);
275 dc = swf_GetBits(tag, 8);
276 if(dc == 0 || dc == 128) {
277 printf("error: dc=%d\n", dc);
280 DEBUG printf(" %d ", dc);
290 index = gethuffvalue2(tag, rle, rle_tree);
291 last = rle_params[index].last;
292 run = rle_params[index].run;
293 level = rle_params[index].level;
294 //DEBUG printf("index:%d\n", index);
295 if(index == RLE_ESCAPE) {
296 last = swf_GetBits(tag, 1);
297 run = swf_GetBits(tag, 6);
298 level = swf_GetBits(tag, 8);
300 DEBUG printf(" (%d) E%d", run, level);
303 if(level == 0 || level == 128) {
304 printf("error: level=%d\n", level);
307 level = (int)((signed char)level);
309 int sign = swf_GetBits(tag, 1);
314 DEBUG printf(" (%d) %s%d", run, level>0?"+":"",level);
316 DEBUG printf(" %s%d", level>0?"+":"",level);
320 //DEBUG printf("run:%d level:%d\n", run, level);
322 DEBUG printf("] pos: %d", pos);
324 printf("\nerror:bad pos (%d)\n", pos);
335 int index = gethuffvalue2(tag, mvd, mvd_tree);
336 DEBUG printf("mvd index:%d\n", index);
340 char has_quant[] = {0,1,0,0,1};
341 char has_mvd[] = {1,1,3,0,0};
348 void decode_block(TAG*tag, int pictype)
351 int mb_type = -1, cbpc = -1;
353 int cbpy_index, cbpy_value;
356 if(pictype == TYPE_INTER) /* non-intra pictures have a cod flag */
358 int cod = swf_GetBits(tag, 1);
359 DEBUG printf("cod=%d\n",cod);
369 if(pictype == TYPE_INTRA) { /* I-frame */
370 type = gethuffvalue2(tag, mcbpc_intra, mcbpc_intra_tree);
371 DEBUG printf("mcbpc=%d\n",type);
372 mb_type = mcbpc_intra_params[type].mb_type;
373 cbpc = mcbpc_intra_params[type].cbpc;
374 if(type == MCBPC_INTRA_STUFFING) {
375 printf("stuffing not supported yet!\n");
376 exit(1); //TODO: goto COD
379 else if(pictype == 1) { /* P-frame */
380 type = gethuffvalue2(tag, mcbpc_inter, mcbpc_inter_tree);
381 DEBUG printf("mcbpc=%d\n",type);
382 mb_type = mcbpc_inter_params[type].mb_type;
383 cbpc = mcbpc_inter_params[type].cbpc;
384 if(type == MCBPC_INTER_STUFFING) {
385 printf("stuffing not supported yet!(2)\n");
386 exit(1); //TODO: goto COD
390 if(mb_type == 3 || mb_type == 4)
395 printf("%c", "vqVii"[mb_type]);
397 DEBUG printf("mcbpc type: %d mb_type:%d cbpc:%d\n", type, mb_type, cbpc);
401 cbpy_index = gethuffvalue2(tag, cbpy, cbpy_tree);
402 cbpy_value = cbpy_index;
405 DEBUG printf("cbpy value:%d (%d)\n", cbpy_value, cbpy_index);
408 /* I 0: 00 mcbpc/cbpy
409 P 0: 00 cod/mcbpc/cbpy/mvd
410 P 6: 10 cod/mcbpc/cbpy/dquant/mvd
411 P 8: 00 cod/mcbpc/cbpy/mvd/mvd24
415 if(has_quant[mb_type]) {
416 dquant = swf_GetBits(tag, 2);
417 if(dquant == 0) dquant = -1;
418 else if(dquant == 1) dquant = -2;
419 else if(dquant == 2) dquant = +1;
420 else if(dquant == 3) dquant = +2;
421 DEBUG printf("dquant: %d\n", dquant);
424 if(has_mvd[mb_type]&1) {
426 x = readMVD(tag); //horizontal
427 y = readMVD(tag); //vertical
429 printf("
\b0"); // prediction was 100% match
432 if(has_mvd[mb_type]&2) {
433 /* only in advanced prediction mode */
434 readMVD(tag); //horizontal
435 readMVD(tag); //vertical
436 readMVD(tag); //horizontal
437 readMVD(tag); //vertical
438 readMVD(tag); //horizontal
439 readMVD(tag); //vertical
443 int has_intradc = intrablock;
444 int has_tcoef = cbpy_value & (8>>t);
445 DEBUG printf("luminance%d ", t);
446 get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*luminance - affected by cbpy*/
450 int has_intradc = intrablock;
451 int has_tcoef = cbpc & (2>>t);
452 DEBUG printf("chrominance%d ", t);
453 get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*chrominance - affected by mcbc*/
458 void handleVideoFrame(TAG*tag, char*prefix)
460 U32 code, version, reference, sizeflags;
462 U8 blocktype, pictype;
463 U16 id = swf_GetU16(tag);
464 U16 frame = swf_GetU16(tag);
465 U8 deblock,flags, tmp, bit;
466 U32 quantizer, extrainfo;
473 char*types[] = {"intra- (I-)frame", "inter- (P-)frame", "disposable interframe", "<reserved>"};
474 printf("============================= frame %d ===================================", frame);
476 /* video packet follows */
478 code = swf_GetBits(tag, 17);
480 printf("code: %x (?)\n", code);
483 version = swf_GetBits(tag, 5); /*actually, part of the picture start code */
484 //printf("version: %x\n", version); /*usually 0*/
486 /* version 1 has some different transform coefficient coding which we
488 printf("spark version %d not supported yet\n", version);
491 reference = swf_GetBits(tag, 8);
492 DEBUG printf("reference: %d\n", reference); /*usually same as frame number (unless frames were skipped while encoding)*/
494 sizeflags = swf_GetBits(tag, 3);
497 case 0: width = swf_GetBits(tag,8); height = swf_GetBits(tag,8); break;
498 case 1: width = swf_GetBits(tag, 16); height = swf_GetBits(tag, 16); break;
499 case 2: width = 352; height = 288; break;
500 case 3: width = 176; height = 144; break;
501 case 4: width = 128; height = 96; break;
502 case 5: width = 320; height = 240; break;
503 case 6: width = 160; height = 120; break;
504 case 7: width = -1; height = -1;/*reserved*/ break;
507 pictype = swf_GetBits(tag, 2);
509 printf("error: unknown pictype: %d\n", pictype);
513 pictype = TYPE_INTER;
516 if(pictype==TYPE_INTER)
517 printf("INTER P%s", disposable?" (disposeable)":"");
521 deblock = swf_GetBits(tag, 1); /*usually 0*/
522 DEBUG printf("deblock: %d\n", deblock);
523 quantizer = swf_GetBits(tag, 5); /*usually 9*/
524 DEBUG printf("quantizer: %d\n", quantizer);
526 extrainfo = swf_GetBits(tag, 1); /*usually none */
528 extrainfo = swf_GetBits(tag, 8);
529 printf("extrainfo: %02x\n", extrainfo);
530 extrainfo = swf_GetBits(tag, 1);
535 bby = (height+15)/16;
537 printf("%dx%d [blocks: %dx%d=%d]\n", width, height, bbx,bby, blocknum);
539 /*if(pictype == TYPE_INTER)
541 /*if(pictype == TYPE_INTRA)
549 for(by=0;by<bby;by++)
551 for(bx=0;bx<bbx;bx++)
553 decode_block(tag, pictype);
559 void hexdumpTag(TAG*tag, char* prefix)
562 printf(" %s-=> ",prefix);
563 for(t=0;t<tag->len;t++) {
564 printf("%02x ", tag->data[t]);
565 if((t && ((t&15)==15)) || (t==tag->len-1))
570 printf("\n %s-=> ",prefix);
575 int main (int argc,char ** argv)
584 checkhufftable(rle, "rle");
585 checkhufftable(mcbpc_intra, "intra");
586 checkhufftable(mcbpc_inter, "inter");
587 checkhufftable(cbpy, "cbpy");
588 checkhufftable(mvd, "mvd");
590 rle_tree = huffcode2tree(rle);
591 mcbpc_intra_tree = huffcode2tree(mcbpc_intra);
592 mcbpc_inter_tree = huffcode2tree(mcbpc_inter);
593 cbpy_tree = huffcode2tree(cbpy);
594 mvd_tree = huffcode2tree(mvd);
596 processargs(argc, argv);
598 fprintf(stderr, "You must supply a filename.\n");
602 f = open(filename,O_RDONLY);
605 perror("Couldn't open file: ");
608 if FAILED(swf_ReadSWF(f,&swf)) {
609 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
616 printf("[HEADER] File version: %d\n", swf.fileVersion);
618 printf("[HEADER] File is zlib compressed.");
619 if(filesize && swf.fileSize)
620 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
624 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
625 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
626 printf("[HEADER] Frame count: %d\n",swf.frameCount);
627 printf("[HEADER] Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
628 if(swf.movieSize.xmin)
629 printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
632 printf("[HEADER] Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
633 if(swf.movieSize.ymin)
634 printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
641 char*name = swf_TagGetName(tag);
643 //printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
645 if(swf_isDefiningTag(tag)) {
646 U16 id = swf_GetDefineID(tag);
647 //printf(" defines id %04d", id);
649 else if(swf_isPseudoDefiningTag(tag)) {
650 U16 id = swf_GetDefineID(tag);
651 //printf(" adds information to id %04d", id);
654 if(tag->id == ST_VIDEOFRAME) {
655 handleVideoFrame(tag, myprefix);
658 else if(tag->id == ST_DEFINEVIDEOSTREAM) {
659 handleVideoStream(tag, myprefix);
666 sprintf(myprefix, " %s", prefix);
668 if(tag->len && hex) {
669 hexdumpTag(tag, prefix);