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"
13 #include "../rfxswf.h"
15 #include "h263tables.c"
17 static char * filename = 0;
18 static char * indent = " ";
22 struct options_t options[] =
34 int args_callback_option(char*name,char*val)
36 if(!strcmp(name, "V")) {
37 printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
40 else if(name[0]=='d') {
44 else if(name[0]=='v') {
49 printf("Unknown option: -%s\n", name);
55 int args_callback_longoption(char*name,char*val)
57 return args_long2shortoption(options, name, val);
59 void args_callback_usage(char*name)
61 printf("Usage: %s [-at] file.swf\n", name);
62 printf("\t-h , --help\t\t Print help and exit\n");
63 printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
64 printf("\t-v , --verbose\t\t Print debugging information\n");
65 printf("\t-V , --version\t\t Print program version and exit\n");
67 int args_callback_command(char*name,char*val)
70 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
77 #define DEBUG if(debug)
79 void handleVideoStream(TAG*tag, char*prefix)
81 U16 id = swf_GetU16(tag);
82 U16 frames = swf_GetU16(tag);
83 U16 width = swf_GetU16(tag);
84 U16 height = swf_GetU16(tag);
85 U8 flags = swf_GetU8(tag); //5-2(videopacket 01=off 10=on)-1(smoothing 1=on)
86 U8 codec = swf_GetU8(tag);
87 printf(" (%d frames, %dx%d", frames, width, height);
91 printf(" sorenson h.263)");
93 printf(" codec 0x%02x)", codec);
96 int checkhufftable(struct huffcode*code, char*name)
101 if(strlen(code[t].code)!=code[t].len) {
102 printf("len mismatch in %s, index %d\n", name, t);
105 if(t != code[t].index) {
106 printf("index mismatch in %s, index %d\n", name, t);
109 while(code[s].code) {
110 char*a = code[s].code;
111 char*b = code[t].code;
114 if(s==t) {s++;continue;}
115 if(code[t].len < code[s].len) {
120 if(!strncmp(a,b,strlen(a))) {
121 printf("index %d (%s) is prefix of %d (%s)\n", ai, a, bi, b);
132 int gethuffvalue(TAG*tag, struct huffcode*code)
138 bits[len] = swf_GetBits(tag, 1)+0x30;
141 while(code[t].code) {
142 if(!strcmp(bits, code[t].code))
145 if(code[t].len >= len)
150 printf("error: %s\n", bits);
151 while(tag->pos < tag->len && nr<80) {
152 int b = swf_GetBits(tag, 1);
164 /*type = 0; // mb-type =3, cbpc = 00
166 printf("can't handle i-frame mcbpc bits %d yet\n", bit);
168 bit = swf_GetBits(tag, 1);
169 type = 0; // mb-type =0, cbpc = 00
171 bit = swf_GetBits(tag, 2);
172 type = 8; // mb-type =2, cbpc = 00
174 printf("can't handle p-frame mcbpc bits 0-%d yet\n", bit);
180 void get_DC_TCOEF(TAG*tag, int t, int has_dc, int has_tcoef)
183 int ac;// = swf_GetBits();
186 //printf("DC:%d\n", dc);
188 dc = swf_GetBits(tag, 8);
189 if(dc == 0 || dc == 128) {
190 printf("error: dc=%d\n", dc);
193 DEBUG printf(" %d ", dc);
203 index = gethuffvalue(tag, rle);
204 last = rle_params[index].last;
205 run = rle_params[index].run;
206 level = rle_params[index].level;
207 //DEBUG printf("index:%d\n", index);
208 if(index == RLE_ESCAPE) {
209 last = swf_GetBits(tag, 1);
210 run = swf_GetBits(tag, 6);
211 level = swf_GetBits(tag, 8);
213 DEBUG printf("(%d) E%d", run, level);
216 if(level == 0 || level == 128) {
217 printf("error: level=%d\n", level);
220 level = (int)((signed char)level);
222 int sign = swf_GetBits(tag, 1);
226 DEBUG printf("(%d) %s%d", run, level>0?"+":"",level);
228 DEBUG printf("%s%d", level>0?"+":"",level);
231 //DEBUG printf("run:%d level:%d\n", run, level);
233 DEBUG printf("] pos: %d", pos);
235 printf("\nerror:bad pos (%d)\n", pos);
246 int index = gethuffvalue(tag, mvd);
247 DEBUG printf("mvd index:%d\n", index);
251 char has_quant[] = {0,1,0,0,1};
252 char has_mvd[] = {1,1,3,0,0};
259 void decode_block(TAG*tag, int pictype)
262 int mb_type = -1, cbpc = -1;
264 int cbpy_index, cbpy_value;
267 if(pictype == TYPE_INTER) /* non-intra pictures have a cod flag */
269 int cod = swf_GetBits(tag, 1);
270 DEBUG printf("cod=%d\n",cod);
280 if(pictype == TYPE_INTRA) { /* I-frame */
281 type = gethuffvalue(tag, mcbpc_intra);
282 DEBUG printf("mcbpc=%d\n",type);
283 mb_type = mcbpc_intra_params[type].mb_type;
284 cbpc = mcbpc_intra_params[type].cbpc;
285 if(type == MCBPC_INTRA_STUFFING) {
286 printf("stuffing not supported yet!\n");
287 exit(1); //TODO: goto COD
290 else if(pictype == 1) { /* P-frame */
291 type = gethuffvalue(tag, mcbpc_inter);
292 DEBUG printf("mcbpc=%d\n",type);
293 mb_type = mcbpc_inter_params[type].mb_type;
294 cbpc = mcbpc_inter_params[type].cbpc;
295 if(type == MCBPC_INTER_STUFFING) {
296 printf("stuffing not supported yet!(2)\n");
297 exit(1); //TODO: goto COD
301 if(mb_type == 3 || mb_type == 4)
306 printf("%c", "vqVii"[mb_type]);
308 DEBUG printf("mcbpc type: %d mb_type:%d cbpc:%d\n", type, mb_type, cbpc);
312 cbpy_index = gethuffvalue(tag, cbpy);
313 cbpy_value = cbpy_index;
316 DEBUG printf("cbpy value:%d (%d)\n", cbpy_value, cbpy_index);
319 /* I 0: 00 mcbpc/cbpy
320 P 0: 00 cod/mcbpc/cbpy/mvd
321 P 6: 10 cod/mcbpc/cbpy/dquant/mvd
322 P 8: 00 cod/mcbpc/cbpy/mvd/mvd24
326 if(has_quant[mb_type]) {
327 dquant = swf_GetBits(tag, 2);
328 if(dquant == 0) dquant = -1;
329 else if(dquant == 1) dquant = -2;
330 else if(dquant == 2) dquant = +1;
331 else if(dquant == 3) dquant = +2;
332 DEBUG printf("dquant: %d\n", dquant);
335 if(has_mvd[mb_type]&1) {
337 x = readMVD(tag); //horizontal
338 y = readMVD(tag); //vertical
342 if(has_mvd[mb_type]&2) {
343 /* only in advanced prediction mode */
344 readMVD(tag); //horizontal
345 readMVD(tag); //vertical
346 readMVD(tag); //horizontal
347 readMVD(tag); //vertical
348 readMVD(tag); //horizontal
349 readMVD(tag); //vertical
353 int has_intradc = intrablock;
354 int has_tcoef = cbpy_value & (8>>t);
355 DEBUG printf("luminance%d ", t);
356 get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*luminance - affected by cbpy*/
360 int has_intradc = intrablock;
361 int has_tcoef = cbpc & (2>>t);
362 DEBUG printf("chrominance%d ", t);
363 get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*chrominance - affected by mcbc*/
368 void handleVideoFrame(TAG*tag, char*prefix)
370 U32 code, version, reference, sizeflags;
372 U8 blocktype, pictype;
373 U16 id = swf_GetU16(tag);
374 U16 frame = swf_GetU16(tag);
375 U8 deblock,flags, tmp, bit;
376 U32 quantizer, extrainfo;
383 char*types[] = {"intra- (I-)frame", "inter- (P-)frame", "disposable interframe", "<reserved>"};
384 printf("============================= frame %d ===================================", frame);
386 /* video packet follows */
388 code = swf_GetBits(tag, 17);
390 printf("code: %x (?)\n", code);
393 version = swf_GetBits(tag, 5); /*actually, part of the picture start code */
394 //printf("version: %x\n", version); /*usually 0*/
396 /* version 1 has some different transform coefficient coding which we
398 printf("spark version %d not supported yet\n", version);
401 reference = swf_GetBits(tag, 8);
402 DEBUG printf("reference: %d\n", reference); /*usually same as frame number (unless frames were skipped while encoding)*/
404 sizeflags = swf_GetBits(tag, 3);
407 case 0: width = swf_GetBits(tag,8); height = swf_GetBits(tag,8); break;
408 case 1: width = swf_GetBits(tag, 16); height = swf_GetBits(tag, 16); break;
409 case 2: width = 352; height = 288; break;
410 case 3: width = 176; height = 144; break;
411 case 4: width = 128; height = 96; break;
412 case 5: width = 320; height = 240; break;
413 case 6: width = 160; height = 120; break;
414 case 7: width = -1; height = -1;/*reserved*/ break;
417 pictype = swf_GetBits(tag, 2);
419 printf("error: unknown pictype: %d\n", pictype);
423 pictype = TYPE_INTER;
426 if(pictype==TYPE_INTER)
427 printf("INTER P%s", disposable?" (disposeable)":"");
431 deblock = swf_GetBits(tag, 1); /*usually 0*/
432 DEBUG printf("deblock: %d\n", deblock);
433 quantizer = swf_GetBits(tag, 5); /*usually 9*/
434 DEBUG printf("quantizer: %d\n", quantizer);
436 extrainfo = swf_GetBits(tag, 1); /*usually none */
438 extrainfo = swf_GetBits(tag, 8);
439 printf("extrainfo: %02x\n", extrainfo);
440 extrainfo = swf_GetBits(tag, 1);
445 bby = (height+15)/16;
447 printf("%dx%d [blocks: %dx%d=%d]\n", width, height, bbx,bby, blocknum);
449 /*if(pictype == TYPE_INTER)
451 /*if(pictype == TYPE_INTRA)
459 for(by=0;by<bby;by++)
461 for(bx=0;bx<bbx;bx++)
463 decode_block(tag, pictype);
469 void hexdumpTag(TAG*tag, char* prefix)
472 printf(" %s-=> ",prefix);
473 for(t=0;t<tag->len;t++) {
474 printf("%02x ", tag->data[t]);
475 if((t && ((t&15)==15)) || (t==tag->len-1))
480 printf("\n %s-=> ",prefix);
485 int main (int argc,char ** argv)
494 checkhufftable(rle, "rle");
495 checkhufftable(mcbpc_intra, "intra");
496 checkhufftable(mcbpc_inter, "inter");
497 checkhufftable(cbpy, "cbpy");
498 checkhufftable(mvd, "mvd");
500 processargs(argc, argv);
502 fprintf(stderr, "You must supply a filename.\n");
506 f = open(filename,O_RDONLY);
509 perror("Couldn't open file: ");
512 if FAILED(swf_ReadSWF(f,&swf)) {
513 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
520 printf("[HEADER] File version: %d\n", swf.fileVersion);
522 printf("[HEADER] File is zlib compressed.");
523 if(filesize && swf.fileSize)
524 printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
528 printf("[HEADER] File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
529 printf("[HEADER] Frame rate: %f\n",swf.frameRate/256.0);
530 printf("[HEADER] Frame count: %d\n",swf.frameCount);
531 printf("[HEADER] Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
532 if(swf.movieSize.xmin)
533 printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
536 printf("[HEADER] Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
537 if(swf.movieSize.ymin)
538 printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
545 char*name = swf_TagGetName(tag);
547 //printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
549 if(swf_isDefiningTag(tag)) {
550 U16 id = swf_GetDefineID(tag);
551 //printf(" defines id %04d", id);
553 else if(swf_isPseudoDefiningTag(tag)) {
554 U16 id = swf_GetDefineID(tag);
555 //printf(" adds information to id %04d", id);
558 if(tag->id == ST_VIDEOFRAME) {
559 handleVideoFrame(tag, myprefix);
562 else if(tag->id == ST_DEFINEVIDEOSTREAM) {
563 handleVideoStream(tag, myprefix);
570 sprintf(myprefix, " %s", prefix);
572 if(tag->len && hex) {
573 hexdumpTag(tag, prefix);