moved to lib/h.263, bugfix in width/height parsing.
[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 "../rfxswf.h"
14 #include "../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 dquant;
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     {
268         int cod = swf_GetBits(tag, 1);
269         DEBUG printf("cod=%d\n",cod);
270         if(cod) {
271             printf(".");
272             return;
273         }
274     }
275     type = -1;
276     
277     /* read mcbpc */
278
279     if(pictype == TYPE_INTRA) { /* I-frame */
280         type = gethuffvalue(tag, mcbpc_intra);
281         DEBUG printf("mcbpc=%d\n",type);
282         mb_type = mcbpc_intra_params[type].mb_type;
283         cbpc = mcbpc_intra_params[type].cbpc;
284         if(type == MCBPC_INTRA_STUFFING) {
285             printf("stuffing not supported yet!\n");
286             exit(1); //TODO: goto COD
287         }
288     } 
289     else if(pictype == 1) { /* P-frame */
290         type = gethuffvalue(tag, mcbpc_inter);
291         DEBUG printf("mcbpc=%d\n",type);
292         mb_type = mcbpc_inter_params[type].mb_type;
293         cbpc = mcbpc_inter_params[type].cbpc;
294         if(type == MCBPC_INTER_STUFFING) {
295             printf("stuffing not supported yet!(2)\n");
296             exit(1); //TODO: goto COD
297         }
298     }
299
300     if(mb_type == 3 || mb_type == 4)
301     {
302         intrablock = 1;
303     }
304
305     printf("%c", "vqVii"[mb_type]);
306
307     DEBUG printf("mcbpc type: %d mb_type:%d cbpc:%d\n", type, mb_type, cbpc);
308
309     /* read cbpy */
310
311     cbpy_index = gethuffvalue(tag, cbpy);
312     cbpy_value = cbpy_index;
313     if(!intrablock)
314         cbpy_value ^= 15;
315     DEBUG printf("cbpy value:%d (%d)\n", cbpy_value, cbpy_index);
316
317
318     /* I 0: 00 mcbpc/cbpy 
319        P 0: 00 cod/mcbpc/cbpy/mvd
320        P 6: 10 cod/mcbpc/cbpy/dquant/mvd
321        P 8: 00 cod/mcbpc/cbpy/mvd/mvd24
322     */
323
324     /* quantizer */
325     if(has_quant[mb_type]) {
326         dquant = swf_GetBits(tag, 2);
327         if(dquant == 0) dquant = -1;
328         else if(dquant == 1) dquant = -2;
329         else if(dquant == 2) dquant = +1;
330         else if(dquant == 3) dquant = +2;
331         DEBUG printf("dquant: %d\n", dquant);
332     }
333
334     if(has_mvd[mb_type]&1) {
335         readMVD(tag); //horizontal
336         readMVD(tag); //vertical
337     }
338     if(has_mvd[mb_type]&2) {
339         /* only in advanced prediction mode */
340         readMVD(tag); //horizontal
341         readMVD(tag); //vertical
342         readMVD(tag); //horizontal
343         readMVD(tag); //vertical
344         readMVD(tag); //horizontal
345         readMVD(tag); //vertical
346     }
347
348     for(t=0;t<4;t++) {
349         int has_intradc = intrablock;
350         int has_tcoef = cbpy_value & (8>>t);
351         DEBUG printf("luminance%d ", t);
352         get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*luminance - affected by cbpy*/
353         DEBUG printf("\n");
354     }
355     for(t=0;t<2;t++) {
356         int has_intradc = intrablock;
357         int has_tcoef = cbpc & (2>>t);
358         DEBUG printf("chrominance%d ", t); 
359         get_DC_TCOEF(tag, t, has_intradc, has_tcoef); /*chrominance - affected by mcbc*/
360         DEBUG printf("\n");
361     }
362 }
363
364 void handleVideoFrame(TAG*tag, char*prefix)
365 {
366     U32 code, version, reference, sizeflags;
367     U32 width, height;
368     U8 blocktype, pictype;
369     U16 id = swf_GetU16(tag);
370     U16 frame = swf_GetU16(tag);
371     U8 deblock,flags, tmp, bit;
372     U32 quantizer, extrainfo;
373     int skipped = 0;
374     int pos=0;
375     int num;
376     int disposable = 0;
377     int blocknum;
378     int bbx,bby,bx,by;
379     char*types[] = {"intra- (I-)frame", "inter- (P-)frame", "disposable interframe", "<reserved>"};
380     printf("============================= frame %d ===================================", frame);
381
382     /* video packet follows */
383     printf("\n");
384     code = swf_GetBits(tag, 17);
385     if(code!=1) {
386         printf("code: %x (?)\n", code);
387         return;
388     }
389     version = swf_GetBits(tag, 5); /*actually, part of the picture start code */
390     //printf("version: %x\n", version); /*usually 0*/
391     if(version >= 1) {
392         /* version 1 has some different transform coefficient coding which we
393            can't handle yet */
394         printf("spark version %d not supported yet\n", version);
395         exit(1);
396     }
397     reference = swf_GetBits(tag, 8);
398     DEBUG printf("reference: %d\n", reference); /*usually same as frame number (unless frames were skipped while encoding)*/
399
400     sizeflags = swf_GetBits(tag, 3);
401     switch(sizeflags)
402     {
403         case 0: width = swf_GetBits(tag,8); height = swf_GetBits(tag,8); break;
404         case 1: width = swf_GetBits(tag, 16); height = swf_GetBits(tag, 16); break;
405         case 2: width = 352; height = 288; break;
406         case 3: width = 176; height = 144; break;
407         case 4: width = 128; height = 96; break;
408         case 5: width = 320; height = 240; break;
409         case 6: width = 160; height = 120; break;
410         case 7: width = -1; height = -1;/*reserved*/ break;
411     }
412     
413     pictype = swf_GetBits(tag, 2);
414     if(pictype==3) {
415         printf("error: unknown pictype: %d\n", pictype);
416         exit(1);
417     }
418     if(pictype==2)  {
419         pictype = TYPE_INTER;
420         disposable = 1;
421     }
422     if(pictype==TYPE_INTER)
423         printf("INTER P%s", disposable?" (disposeable)":"");
424     else
425         printf("INTRA I");
426
427     deblock = swf_GetBits(tag, 1); /*usually 0*/
428     DEBUG printf("deblock: %d\n", deblock);
429     quantizer = swf_GetBits(tag, 5); /*usually 9*/
430     DEBUG printf("quantizer: %d\n", quantizer);
431
432     extrainfo = swf_GetBits(tag, 1); /*usually none */
433     while(extrainfo) {
434         extrainfo = swf_GetBits(tag, 8);
435         printf("extrainfo: %02x\n", extrainfo);
436         extrainfo = swf_GetBits(tag, 1);
437     }
438
439     /* macro block */
440     bbx = (width+15)/16;
441     bby = (height+15)/16;
442     blocknum = bbx*bby;
443     printf("%dx%d [blocks: %dx%d=%d]\n", width, height, bbx,bby, blocknum);
444
445     /*if(pictype == TYPE_INTER)
446         return;*/
447     /*if(pictype == TYPE_INTRA)
448         return;*/
449
450     /*tagnr++;
451     if(tagnr!=2)
452         return;*/
453
454     DEBUG printf("\n");
455     for(by=0;by<bby;by++)
456     {
457         for(bx=0;bx<bbx;bx++)
458         {
459             decode_block(tag, pictype);
460         }
461         printf("\n");
462     }
463 }
464
465 void hexdumpTag(TAG*tag, char* prefix)
466 {
467     int t;
468     printf("                %s-=> ",prefix);
469     for(t=0;t<tag->len;t++) {
470         printf("%02x ", tag->data[t]);
471         if((t && ((t&15)==15)) || (t==tag->len-1))
472         {
473             if(t==tag->len-1)
474                 printf("\n");
475             else
476                 printf("\n                %s-=> ",prefix);
477         }
478     }
479 }
480
481 int main (int argc,char ** argv)
482 {
483     TAG*tag;
484     SWF swf;
485     int f;
486     char prefix[128];
487     int filesize = 0;
488     prefix[0] = 0;
489
490     checkhufftable(rle, "rle");
491     checkhufftable(mcbpc_intra, "intra");
492     checkhufftable(mcbpc_inter, "inter");
493     checkhufftable(cbpy, "cbpy");
494     checkhufftable(mvd, "mvd");
495
496     processargs(argc, argv);
497     if(!filename) {
498         fprintf(stderr, "You must supply a filename.\n");
499         return 1;
500     }
501
502     f = open(filename,O_RDONLY);
503
504     if (f<0) {
505         perror("Couldn't open file: ");
506         exit(1);
507     }
508     if FAILED(swf_ReadSWF(f,&swf)) {
509         fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
510         close(f);
511         exit(1);
512     }
513
514     close(f);
515
516     printf("[HEADER]        File version: %d\n", swf.fileVersion);
517     if(swf.compressed) {
518         printf("[HEADER]        File is zlib compressed.");
519         if(filesize && swf.fileSize)
520             printf(" Ratio: %02d%%\n", filesize*100/(swf.fileSize));
521         else
522             printf("\n");
523     }
524     printf("[HEADER]        File size: %ld%s\n", swf.fileSize, swf.compressed?" (Depacked)":"");
525     printf("[HEADER]        Frame rate: %f\n",swf.frameRate/256.0);
526     printf("[HEADER]        Frame count: %d\n",swf.frameCount);
527     printf("[HEADER]        Movie width: %.2f",(swf.movieSize.xmax-swf.movieSize.xmin)/20.0);
528     if(swf.movieSize.xmin)
529         printf(" (left offset: %.2f)\n", swf.movieSize.xmin/20.0);
530     else
531         printf("\n");
532     printf("[HEADER]        Movie height: %.2f",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
533     if(swf.movieSize.ymin)
534         printf(" (top offset: %.2f)\n", swf.movieSize.ymin/20.0);
535     else
536         printf("\n");
537
538     tag = swf.firstTag;
539
540     while(tag) {
541         char*name = swf_TagGetName(tag);
542         char myprefix[128];
543         //printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
544
545         if(swf_isDefiningTag(tag)) {
546             U16 id = swf_GetDefineID(tag);
547             //printf(" defines id %04d", id);
548         }
549         else if(swf_isPseudoDefiningTag(tag)) {
550             U16 id = swf_GetDefineID(tag);
551             //printf(" adds information to id %04d", id);
552         }
553
554         if(tag->id == ST_VIDEOFRAME) {
555             handleVideoFrame(tag, myprefix);
556             //printf("\n");
557         }
558         else if(tag->id == ST_DEFINEVIDEOSTREAM) {
559             handleVideoStream(tag, myprefix);
560             printf("\n");
561         }
562         else {
563             //printf("\n");
564         }
565
566         sprintf(myprefix, "                %s", prefix);
567
568         if(tag->len && hex) {
569             hexdumpTag(tag, prefix);
570         }
571         tag = tag->next;
572         fflush(stdout);
573     }
574
575     swf_FreeTags(&swf);
576     return 0;
577 }
578
579