2376017530a1b9f28439f45a1cf5fbccce8f768d
[swftools.git] / lib / h.263 / video.c
1 /* video.c
2    Shows the structure of a swf file containing video
3
4    Part of the swftools package.
5    
6    Copyright (c) 2003 Matthias Kramm <kramm@quiss.org> */
7
8 #include "../config.h"
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <stdarg.h>
13 #include "../lib/rfxswf.h"
14 #include "../lib/args.h"
15 #include "h263tables.c"
16
17 static char * filename = 0;
18 static char * indent = "                ";
19 int hex = 0;
20 int debug = 0;
21
22 struct options_t options[] =
23 {
24  {"v","verbose"},
25  {"V","version"},
26  {"d","hex"},
27  {"M","video"},
28  {0,0}
29 };
30
31 /* TODO:
32    * check rle tables
33 */
34 int args_callback_option(char*name,char*val)
35 {
36     if(!strcmp(name, "V")) {
37         printf("swfdump - part of %s %s\n", PACKAGE, VERSION);
38         exit(0);
39     }
40     else if(name[0]=='d') {
41         hex = 1;
42         return 0;
43     }
44     else if(name[0]=='v') {
45         debug = 1;
46         return 0;
47     }
48     else {
49         printf("Unknown option: -%s\n", name);
50         exit(1);
51     }
52
53     return 0;
54 }
55 int args_callback_longoption(char*name,char*val)
56 {
57     return args_long2shortoption(options, name, val);
58 }
59 void args_callback_usage(char*name)
60 {
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");
66 }
67 int args_callback_command(char*name,char*val)
68 {
69     if(filename) {
70         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
71                  filename, name);
72     }
73     filename = name;
74     return 0;
75 }
76
77 #define DEBUG if(debug)
78
79 void handleVideoStream(TAG*tag, char*prefix)
80 {
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);
88     if(flags&1)
89         printf(" smoothed");
90     if(codec == 2)
91         printf(" sorenson h.263)");
92     else
93         printf(" codec 0x%02x)", codec);
94 }
95
96 int checkhufftable(struct huffcode*code, char*name)
97 {
98     int t=0;
99     while(code[t].code) {
100         int s=0;
101         if(strlen(code[t].code)!=code[t].len) {
102             printf("len mismatch in %s, index %d\n", name, t);
103             exit(1);
104         }
105         if(t != code[t].index) {
106             printf("index mismatch in %s, index %d\n", name, t);
107             exit(1);
108         }
109         while(code[s].code) {
110             char*a = code[s].code;
111             char*b = code[t].code;
112             int ai = s;
113             int bi = t;
114             if(s==t) {s++;continue;}
115             if(code[t].len < code[s].len) {
116                  a = code[t].code;
117                  b = code[s].code;
118                  ai = t; bi = s;
119             }
120             if(!strncmp(a,b,strlen(a))) {
121                 printf("index %d (%s) is prefix of %d (%s)\n", ai, a, bi, b);
122                 exit(1);
123             }
124
125             s++;
126         }
127
128         t++;
129     }
130 }
131
132 int gethuffvalue(TAG*tag, struct huffcode*code)
133 {
134     int len = 0;
135     char bits[80];
136     while(1) {
137         int t=0,pot=0;
138         bits[len] = swf_GetBits(tag, 1)+0x30;
139         len++;
140         bits[len] = 0;
141         while(code[t].code) {
142             if(!strcmp(bits, code[t].code))
143                 return t;
144             t++;
145             if(code[t].len >= len)
146                 pot++;
147         }
148         if(!pot) {
149             int nr=0;
150             printf("error: %s\n", bits);
151             while(tag->pos < tag->len && nr<80) {
152                 int b = swf_GetBits(tag, 1);
153                 printf("%d", b);
154                 nr++;
155             }
156             if(nr==80)
157                 printf("...");
158             printf("\n");
159             exit(1);
160             return -1;
161         }
162     }
163
164         /*type = 0; // mb-type =3, cbpc = 00
165         if(!bit) {
166             printf("can't handle i-frame mcbpc bits %d yet\n", bit);
167         }
168         bit = swf_GetBits(tag, 1);
169         type = 0; // mb-type =0, cbpc = 00
170         if(!bit) {
171             bit = swf_GetBits(tag, 2);
172             type = 8; // mb-type =2, cbpc = 00
173             if(bit!=3) {
174                 printf("can't handle p-frame mcbpc bits 0-%d yet\n", bit);
175                 exit(1);
176             }
177         }*/
178 }
179                 
180 void get_DC_TCOEF(TAG*tag, int t, int has_dc, int has_tcoef)
181 {
182     int dc;
183     int ac;// = swf_GetBits();
184     int index;
185     int pos = 0;
186     //printf("DC:%d\n", dc);
187     if(has_dc) {
188         dc = swf_GetBits(tag, 8);
189         if(dc == 0 || dc == 128) {
190             printf("error: dc=%d\n", dc);
191             exit(1);
192         }
193         DEBUG printf(" %d ", dc);
194         pos++;
195     }
196
197     if(has_tcoef) {
198         DEBUG printf("[");
199         while(1) {
200             int last;
201             int run;
202             int level;
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);
212                 if(run)
213                     DEBUG printf("(%d) E%d", run, level);
214                 else
215                     DEBUG printf("E");
216                 if(level == 0 || level == 128) {
217                     printf("error: level=%d\n", level);
218                     exit(1);
219                 }
220                 level = (int)((signed char)level);
221             } else {
222                 int sign = swf_GetBits(tag, 1);
223                 if(sign)
224                     level = -level;
225                 if(run)
226                     DEBUG printf("(%d) %s%d", run, level>0?"+":"",level);
227                 else
228                     DEBUG printf("%s%d", level>0?"+":"",level);
229             }
230             pos += run+1;
231             //DEBUG printf("run:%d level:%d\n", run, level);
232             if(last) {
233                 DEBUG printf("] pos: %d", pos);
234                 if(pos>64) {
235                     printf("\nerror:bad pos (%d)\n", pos);
236                     exit(1);
237                 }
238                 return;
239             }
240         }
241     }
242 }
243             
244 void readMVD(TAG*tag)
245 {
246     int index = gethuffvalue(tag, mvd);
247     DEBUG printf("mvd index:%d\n", index);
248 }
249
250 char has_quant[] = {0,1,0,0,1};
251 char has_mvd[] = {1,1,3,0,0};
252
253 #define TYPE_INTRA 0
254 #define TYPE_INTER 1
255
256 int tagnr = 0;
257
258 void decode_block(TAG*tag, int pictype)
259 {
260     int t;
261     int mb_type = -1, cbpc = -1;
262     int dbquant;
263     int cbpy_index, cbpy_value;
264     int intrablock = 0;
265     int type;
266     if(pictype == TYPE_INTER) /* non-intra pictures have a cod flag */
267     /* TODO: according to the flash spec, this field is always present */
268     {
269         int cod = swf_GetBits(tag, 1);
270         DEBUG printf("cod=%d\n",cod);
271         if(cod) {
272             printf(".");
273             return;
274         }
275     }
276     type = -1;
277     
278     /* read mcbpc */
279
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
288         }
289     } 
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
298         }
299     }
300
301     if(mb_type == 3 || mb_type == 4)
302     {
303         intrablock = 1;
304     }
305         
306     printf("%d", intrablock);
307
308     DEBUG printf("mcbpc type: %d mb_type:%d cbpc:%d\n", type, mb_type, cbpc);
309
310     /* read cbpy */
311
312     cbpy_index = gethuffvalue(tag, cbpy);
313     cbpy_value = cbpy_index;
314     if(!intrablock)
315         cbpy_value ^= 15;
316     DEBUG printf("cbpy value:%d (%d)\n", cbpy_value, cbpy_index);
317
318
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
323     */
324
325     /* quantizer */
326     if(has_quant[mb_type]) {
327         dbquant = swf_GetBits(tag, 2);
328         DEBUG printf("quantizer: %d\n", dbquant);
329     }
330
331     if(has_mvd[mb_type]&1) {
332         readMVD(tag); //horizontal
333         readMVD(tag); //vertical
334     }
335     if(has_mvd[mb_type]&2) {
336         /* only in advanced prediction mode */
337         readMVD(tag); //horizontal
338         readMVD(tag); //vertical
339         readMVD(tag); //horizontal
340         readMVD(tag); //vertical
341         readMVD(tag); //horizontal
342         readMVD(tag); //vertical
343     }
344
345     for(t=0;t<4;t++) {
346         int has_intradc = intrablock;
347         int has_tcoef = cbpy_value & (8>>t);
348         DEBUG printf("luminance%d ", t);
349         get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*luminance - affected by cbpy*/
350         DEBUG printf("\n");
351     }
352     for(t=0;t<2;t++) {
353         int has_intradc = intrablock;
354         int has_tcoef = cbpc & (2>>t);
355         DEBUG printf("chrominance%d ", t); 
356         get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*chrominance - affected by mcbc*/
357         DEBUG printf("\n");
358     }
359 }
360
361 void handleVideoFrame(TAG*tag, char*prefix)
362 {
363     U32 code, version, reference, sizeflags;
364     U32 width, height;
365     U8 blocktype, pictype;
366     U16 id = swf_GetU16(tag);
367     U16 frame = swf_GetU16(tag);
368     U8 deblock,flags, tmp, bit;
369     U32 quantizer, extrainfo;
370     int skipped = 0;
371     int pos=0;
372     int num;
373     int disposable = 0;
374     int blocknum;
375     int bbx,bby,bx,by;
376     char*types[] = {"intra- (I-)frame", "inter- (P-)frame", "disposable interframe", "<reserved>"};
377     printf("============================= frame %d ===================================", frame);
378
379     /* video packet follows */
380     printf("\n");
381     code = swf_GetBits(tag, 17);
382     if(code!=1) {
383         printf("code: %x (?)\n", code);
384         return;
385     }
386     version = swf_GetBits(tag, 5); /*actually, part of the picture start code */
387     //printf("version: %x\n", version); /*usually 0*/
388     if(version >= 1) {
389         /* version 1 has some different transform coefficient coding which we
390            can't handle yet */
391         printf("spark version %d not supported yet\n", version);
392         exit(1);
393     }
394     reference = swf_GetBits(tag, 8);
395     DEBUG printf("reference: %d\n", reference); /*usually same as frame number (unless frames were skipped while encoding)*/
396
397     sizeflags = swf_GetBits(tag, 3);
398     switch(sizeflags)
399     {
400         case 0: width = swf_GetU8(tag); height = swf_GetU8(tag); break;
401         case 1: width = swf_GetBits(tag, 16); height = swf_GetBits(tag, 16); break;
402         case 2: width = 352; height = 288; break;
403         case 3: width = 176; height = 144; break;
404         case 4: width = 128; height = 96; break;
405         case 5: width = 320; height = 240; break;
406         case 6: width = 160; height = 120; break;
407         case 7: width = -1; height = -1;/*reserved*/ break;
408     }
409     
410     pictype = swf_GetBits(tag, 2);
411     if(pictype==3) {
412         printf("error: unknown pictype: %d\n", pictype);
413         exit(1);
414     }
415     if(pictype==2)  {
416         pictype = TYPE_INTER;
417         disposable = 1;
418     }
419     if(pictype==TYPE_INTER)
420         printf("INTER P%s", disposable?" (disposeable)":"");
421     else
422         printf("INTRA I");
423
424     deblock = swf_GetBits(tag, 1); /*usually 0*/
425     DEBUG printf("deblock: %d\n", deblock);
426     quantizer = swf_GetBits(tag, 5); /*usually 9*/
427     DEBUG printf("quantizer: %d\n", quantizer);
428
429     extrainfo = swf_GetBits(tag, 1); /*usually none */
430     while(extrainfo) {
431         extrainfo = swf_GetBits(tag, 8);
432         printf("extrainfo: %02x\n", extrainfo);
433         extrainfo = swf_GetBits(tag, 1);
434     }
435
436     /* macro block */
437     bbx = (width+15)/16;
438     bby = (height+15)/16;
439     blocknum = bbx*bby;
440     printf("%dx%d [blocks: %dx%d=%d]\n", width, height, bbx,bby, blocknum);
441
442     /*if(pictype == TYPE_INTER)
443         return;*/
444     if(pictype == TYPE_INTRA)
445         return;
446
447     /*tagnr++;
448     if(tagnr!=2)
449         return;*/
450
451     DEBUG printf("\n");
452     for(by=0;by<bby;by++)
453     {
454         for(bx=0;bx<bbx;bx++)
455         {
456             decode_block(tag, pictype);
457         }
458         printf("\n");
459     }
460 }
461
462 void hexdumpTag(TAG*tag, char* prefix)
463 {
464     int t;
465     printf("                %s-=> ",prefix);
466     for(t=0;t<tag->len;t++) {
467         printf("%02x ", tag->data[t]);
468         if((t && ((t&15)==15)) || (t==tag->len-1))
469         {
470             if(t==tag->len-1)
471                 printf("\n");
472             else
473                 printf("\n                %s-=> ",prefix);
474         }
475     }
476 }
477
478 int main (int argc,char ** argv)
479 {
480     TAG*tag;
481     SWF swf;
482     int f;
483     char prefix[128];
484     int filesize = 0;
485     prefix[0] = 0;
486
487     checkhufftable(rle, "rle");
488     checkhufftable(mcbpc_intra, "intra");
489     checkhufftable(mcbpc_inter, "inter");
490     checkhufftable(cbpy, "cbpy");
491     checkhufftable(mvd, "mvd");
492
493     processargs(argc, argv);
494     if(!filename) {
495         fprintf(stderr, "You must supply a filename.\n");
496         return 1;
497     }
498
499     f = open(filename,O_RDONLY);
500
501     if (f<0) {
502         perror("Couldn't open file: ");
503         exit(1);
504     }
505     if FAILED(swf_ReadSWF(f,&swf)) {
506         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
507         close(f);
508         exit(1);
509     }
510
511     close(f);
512
513     printf("[HEADER]        File version: %d\n", swf.fileVersion);
514     if(swf.compressed) {
515         printf("[HEADER]        File is zlib compressed.");
516         if(filesize && swf.fileSize)
517             printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
518         else
519             printf("\n");
520     }
521     printf("[HEADER]        File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
522     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
523     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
524     printf("[HEADER]        Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
525     if(swf.movieSize.xmin)
526         printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
527     else
528         printf("\n");
529     printf("[HEADER]        Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
530     if(swf.movieSize.ymin)
531         printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
532     else
533         printf("\n");
534
535     tag = swf.firstTag;
536
537     while(tag) {
538         char*name = swf_TagGetName(tag);
539         char myprefix[128];
540         //printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
541
542         if(swf_isDefiningTag(tag)) {
543             U16 id = swf_GetDefineID(tag);
544             //printf(" defines id %04d", id);
545         }
546         else if(swf_isPseudoDefiningTag(tag)) {
547             U16 id = swf_GetDefineID(tag);
548             //printf(" adds information to id %04d", id);
549         }
550
551         if(tag->id == ST_VIDEOFRAME) {
552             handleVideoFrame(tag, myprefix);
553             //printf("\n");
554         }
555         else if(tag->id == ST_DEFINEVIDEOSTREAM) {
556             handleVideoStream(tag, myprefix);
557             printf("\n");
558         }
559         else {
560             //printf("\n");
561         }
562
563         sprintf(myprefix, "                %s", prefix);
564
565         if(tag->len && hex) {
566             hexdumpTag(tag, prefix);
567         }
568         tag = tag->next;
569         fflush(stdout);
570     }
571
572     swf_FreeTags(&swf);
573     return 0;
574 }
575
576