implemented P-frame stubs.
[swftools.git] / lib / h.263 / mkvideo.c
1 /* mkvideo.c
2    Create a video file.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2003 Matthias Kramm <kramm@quiss.org> */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <assert.h>
11 #include <math.h>
12 #include "../lib/rfxswf.h"
13 #include "png.h"
14 #include "h263tables.c"
15
16 typedef struct _VIDEOSTREAM
17 {
18     int width;
19     int height;
20     RGBA*oldpic;
21 } VIDEOSTREAM;
22
23 void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width, U16 height)
24 {
25     width=width&~15; height=height&~15;
26     swf_SetU16(tag, frames);
27     swf_SetU16(tag, width);
28     swf_SetU16(tag, height);
29     swf_SetU8(tag, 1); /* smoothing on */
30     swf_SetU8(tag, 2); /* codec = h.263 sorenson spark */
31
32     stream->width = width;
33     stream->height = height;
34     stream->oldpic = 0;
35 }
36
37 typedef struct _block_t
38 {
39     int y1[64];
40     int y2[64];
41     int y3[64];
42     int y4[64];
43     int u[64];
44     int v[64];
45 } block_t;
46
47 typedef struct _fblock_t
48 {
49     double y1[64];
50     double y2[64];
51     double y3[64];
52     double y4[64];
53     double u[64];
54     double v[64];
55 } fblock_t;
56
57 void fzigzag(double*src) 
58 {
59     int table[64] = {
60         0, 1, 5, 6, 14, 15, 27, 28, 
61         2, 4, 7, 13, 16, 26, 29, 42, 
62         3, 8, 12, 17, 25, 30, 41, 43, 
63         9, 11, 18, 24, 31, 40, 44, 53, 
64         10, 19, 23, 32, 39, 45, 52, 54, 
65         20, 22, 33, 38, 46, 51, 55, 60, 
66         21, 34, 37, 47, 50, 56, 59, 61, 
67         35, 36, 48, 49, 57, 58, 62, 63};
68     double tmp[64];
69     int t;
70     for(t=0;t<64;t++) {
71         ((int*)&tmp[table[t]])[0] = ((int*)&src[t])[0];
72         ((int*)&tmp[table[t]])[1] = ((int*)&src[t])[1];
73     }
74     memcpy(src, tmp, sizeof(double)*64);
75 }
76
77 #define PI 3.14159265358979
78 #define SQRT2 1.414214
79 #define RSQRT2 (1.0/1.414214)
80
81 double table[8][8] =
82 {
83 {0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548},
84 {0.980785280403230,0.831469612302545,0.555570233019602,0.195090322016128,-0.195090322016128,-0.555570233019602,-0.831469612302545,-0.980785280403230},
85 {0.923879532511287,0.382683432365090,-0.382683432365090,-0.923879532511287,-0.923879532511287,-0.382683432365090,0.382683432365090,0.923879532511287},
86 {0.831469612302545,-0.195090322016128,-0.980785280403230,-0.555570233019602,0.555570233019602,0.980785280403230,0.195090322016129,-0.831469612302545},
87 {0.707106781186548,-0.707106781186547,-0.707106781186548,0.707106781186547,0.707106781186548,-0.707106781186547,-0.707106781186547,0.707106781186547},
88 {0.555570233019602,-0.980785280403230,0.195090322016128,0.831469612302545,-0.831469612302545,-0.195090322016128,0.980785280403231,-0.555570233019602},
89 {0.382683432365090,-0.923879532511287,0.923879532511287,-0.382683432365090,-0.382683432365091,0.923879532511287,-0.923879532511286,0.382683432365090},
90 {0.195090322016128,-0.555570233019602,0.831469612302545,-0.980785280403231,0.980785280403230,-0.831469612302545,0.555570233019602,-0.195090322016129}
91 };
92
93 void dct(double*src)
94 {
95     double tmp[64];
96     int x,y,u,v,t;
97
98     for(v=0;v<8;v++)
99     for(u=0;u<8;u++)
100     {
101         double c = 0;
102         for(x=0;x<8;x++)
103         {
104             c+=table[u][x]*src[v*8+x];
105         }
106         tmp[v*8+u] = c;
107     }
108     for(u=0;u<8;u++)
109     for(v=0;v<8;v++)
110     {
111         double c = 0;
112         for(y=0;y<8;y++)
113         {
114             c+=table[v][y]*tmp[y*8+u];
115         }
116         src[v*8+u] = c*0.25;
117     }
118 }
119
120 void idct(double*src)
121 {
122     double tmp[64];
123     int x,y,u,v;
124     for(y=0;y<8;y++)
125     for(x=0;x<8;x++)
126     {
127         double c = 0;
128         for(u=0;u<8;u++)
129         {
130             c+=table[u][x]*src[y*8+u];
131         }
132         tmp[y*8+x] = c;
133     }
134     for(y=0;y<8;y++)
135     for(x=0;x<8;x++)
136     {
137         double c = 0;
138         for(v=0;v<8;v++)
139         {
140             c+=table[v][y]*tmp[v*8+x];
141         }
142         src[y*8+x] = c*0.25;
143     }
144 }
145
146 void getregion(fblock_t* bb, RGBA*pic, int bx, int by, int width, int height)
147 {
148     RGBA*p1 = &pic[by*width*16+bx*16];
149     RGBA*p2 = p1;
150     int linex = width;
151     int y1=0, y2=0, y3=0, y4=0;
152     int u=0,v=0;
153     int x,y;
154     for(y=0;y<8;y++) {
155         for(x=0;x<8;x++) {
156             double r,g,b;
157             r = (p2[x*2].r + p2[x*2+1].r + p2[linex+x*2].r + p2[linex+x*2+1].r)/4.0;
158             g = (p2[x*2].g + p2[x*2+1].g + p2[linex+x*2].g + p2[linex+x*2+1].g)/4.0;
159             b = (p2[x*2].b + p2[x*2+1].b + p2[linex+x*2].b + p2[linex+x*2+1].b)/4.0;
160             bb->u[u++] = (r*-0.169 + g*-0.332 + b*0.500 + 128.0);
161             bb->v[v++] = (r*0.500 + g*-0.419 + b*-0.0813 + 128.0);
162
163             r = p1[x].r; g = p1[x].g; b = p1[x].b;
164             bb->y1[y1++] = (r*0.299 + g*0.587 + b*0.114);
165             r = p1[x+8].r; g = p1[x+8].g; b = p1[x+8].b;
166             bb->y2[y2++] = (r*0.299 + g*0.587 + b*0.114);
167             r = p1[linex*8+x].r; g = p1[linex*8+x].g; b = p1[linex*8+x].b;
168             bb->y3[y3++] = (r*0.299 + g*0.587 + b*0.114);
169             r = p1[linex*8+x+8].r; g = p1[linex*8+x+8].g; b = p1[linex*8+x+8].b;
170             bb->y4[y4++] = (r*0.299 + g*0.587 + b*0.114);
171         }
172         p1+=linex;
173         p2+=linex*2;
174     }
175 }
176
177 int valtodc(int val)
178 {
179     assert(val>=0);
180
181     /* table 12/h.263 */
182
183     val+=4; //round
184     val/=8;
185     /* TODO: what to do for zero values? skip the block? */
186     if(val==0)
187         return 1;
188     if(val==128)
189         return 255;
190     if(val>254)
191         return 254;
192     return val;
193 }
194
195 void codehuffman(TAG*tag, struct huffcode*table, int index)
196 {
197     /* TODO: !optimize! */
198     int i=0;
199     while(table[index].code[i]) {
200         if(table[index].code[i]=='0')
201             swf_SetBits(tag, 0, 1);
202         else
203             swf_SetBits(tag, 1, 1);
204         i++;
205     }
206 }
207
208 void quantize8x8(double*src, int*dest, int has_dc, int quant)
209 {
210     int t,pos=0;
211     if(has_dc) {
212         dest[0] = valtodc((int)src[0]); /*DC*/
213         pos++;
214     }
215     for(t=pos;t<64;t++)
216     {
217         dest[t] = (int)src[t];
218         //val = (quant*(2*level+1)-1)+quant&1
219 /*      if(quant&1) {
220             dest[t] = (dest[t]/quant - 1)/2;
221         } else {
222             dest[t] = ((dest[t]+1)/quant - 1)/2;
223         }*/
224         //dest[t] = (dest[t]/quant-1)/2;
225         dest[t] = dest[t]/(quant*2);
226     }
227 }
228
229 int hascoef(int*b, int has_dc)
230 {
231     int t;
232     int pos=0;
233     int range=2; /*TODO: should be a global parameter */
234     if(has_dc)
235         pos++;
236     for(t=pos;t<64;t++) {
237         if(b[t]<=-range || b[t]>=range)
238             return 1;
239     }
240     return 0;
241 }
242
243 void encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
244 {
245     int t;
246     int pos=0;
247
248     if(has_dc) {
249         swf_SetBits(tag, bb[0], 8);
250         pos++;
251     }
252
253     if(has_tcoef) {
254         int last;
255         /* determine last non-null coefficient */
256         for(last=63;last>=pos;last--) {
257             /* TODO: we could leave out small coefficients
258                      after a certain point (32?) */
259             if(bb[last])
260                 break;
261         }
262         assert(bb[last]);
263         /* blocks without coefficients should not be included
264            in the cbpy/cbpc patterns */
265         while(1) {
266             int run=0;
267             int level=0;
268             int islast=0;
269             int sign=0;
270             int t;
271             while(!bb[pos] && pos<last) {
272                 pos++;
273                 run++;
274             }
275             if(pos==last)
276                 islast=1;
277             level=bb[pos];
278             assert(level);
279             if(level<0) {
280                 level = -level;
281                 sign = 1;
282             }
283             for(t=0;t<RLE_ESCAPE;t++) {
284                 if(rle_params[t].run == run &&
285                    rle_params[t].level == level &&
286                    rle_params[t].last == islast) {
287                     codehuffman(tag, rle, t);
288                     swf_SetBits(tag, sign, 1);
289                     break;
290                 }
291             }
292             if(t==RLE_ESCAPE) {
293                 codehuffman(tag, rle, RLE_ESCAPE);
294                 level=bb[pos];
295                 /* table 14/h.263 */
296                 assert(level);
297                 if(level<-127) level = -127;
298                 if(level>127) level = 127;
299
300                 swf_SetBits(tag, islast, 1);
301                 swf_SetBits(tag, run, 6);
302                 swf_SetBits(tag, level, 8); //fixme
303             }
304
305             if(islast)
306                 break;
307             pos++;
308         }
309
310         //codehuffman(tag, rle, 58);
311         //swf_SetBits(tag, 1, 1); //sign
312     }
313 }
314
315 void dodct(fblock_t*fb)
316 {
317     dct(fb->y1); dct(fb->y2); dct(fb->y3); dct(fb->y4); 
318     dct(fb->u);  dct(fb->v);  
319     fzigzag(fb->y1);
320     fzigzag(fb->y2);
321     fzigzag(fb->y3);
322     fzigzag(fb->y4);
323     fzigzag(fb->u);
324     fzigzag(fb->v); 
325 }
326
327 void quantize(fblock_t*fb, block_t*b, int has_dc, int quant)
328 {
329     quantize8x8(fb->y1,b->y1,has_dc,quant); 
330     quantize8x8(fb->y2,b->y2,has_dc,quant); 
331     quantize8x8(fb->y3,b->y3,has_dc,quant); 
332     quantize8x8(fb->y4,b->y4,has_dc,quant); 
333     quantize8x8(fb->u,b->u,has_dc,quant);   
334     quantize8x8(fb->v,b->v,has_dc,quant);   
335 }
336
337 void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
338 {
339     *cbpybits = 0;
340     *cbpcbits = 0;
341
342     *cbpybits|=hascoef(b->y1, has_dc)*8;
343     *cbpybits|=hascoef(b->y2, has_dc)*4;
344     *cbpybits|=hascoef(b->y3, has_dc)*2;
345     *cbpybits|=hascoef(b->y4, has_dc)*1;
346
347     *cbpcbits|=hascoef(b->u, has_dc)*2;
348     *cbpcbits|=hascoef(b->v, has_dc)*1;
349 }
350
351 void setQuant(TAG*tag, int dquant)
352 {
353     int code = 0;
354     /* 00 01 10 11
355        -1 -2 +1 +2
356     */
357     if(dquant == -1) {
358         swf_SetBits(tag, 0x0, 2);
359     } else if(dquant == -2) {
360         swf_SetBits(tag, 0x1, 2);
361     } else if(dquant == +1) {
362         swf_SetBits(tag, 0x2, 2);
363     } else if(dquant == +2) {
364         swf_SetBits(tag, 0x3, 2);
365     } else {
366         assert(0*strlen("invalid dquant"));
367     }
368 }
369
370 void change_quant(int quant, int*dquant)
371 {
372     /* TODO */
373     *dquant = 0;
374 }
375
376 void encode_blockI(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int*quant)
377 {
378     fblock_t fb;
379     block_t b;
380     int dquant=0;
381     int cbpcbits = 0, cbpybits=0;
382
383     getregion(&fb, pic, bx, by, width, height);
384     dodct(&fb);
385     
386     change_quant(*quant, &dquant);
387     *quant+=dquant;
388
389     quantize(&fb, &b, 1, *quant);
390     getblockpatterns(&b, &cbpybits, &cbpcbits, 1);
391
392     if(dquant) {
393         codehuffman(tag, mcbpc_intra, 4+cbpcbits);
394     } else {
395         codehuffman(tag, mcbpc_intra, 0+cbpcbits);
396     }
397
398     codehuffman(tag, cbpy, cbpybits);
399
400     if(dquant) {
401         setQuant(tag, dquant);
402     }
403
404     /* luminance */
405     encode8x8(tag, b.y1, 1, cbpybits&8);
406     encode8x8(tag, b.y2, 1, cbpybits&4);
407     encode8x8(tag, b.y3, 1, cbpybits&2);
408     encode8x8(tag, b.y4, 1, cbpybits&1);
409
410     /* chrominance */
411     encode8x8(tag, b.u, 1, cbpcbits&2);
412     encode8x8(tag, b.v, 1, cbpcbits&1);
413 }
414
415 void encode_blockP(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int*quant)
416 {
417     fblock_t fb;
418     block_t b;
419     int dquant=0;
420     int has_mvd=0;
421     int has_mvd24=0;
422     int has_dc=1;
423     int mode = 0;
424     int cbpcbits = 0, cbpybits=0;
425
426     getregion(&fb, pic, bx, by, width, height);
427     dodct(&fb);
428
429     change_quant(*quant, &dquant);
430     *quant += dquant;
431
432     quantize(&fb, &b, has_dc, *quant);
433
434     getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
435
436     if(!dquant && has_mvd && !has_mvd24 && !has_dc) mode = 0;
437     else if(dquant && has_mvd && !has_mvd24 && !has_dc) mode = 1;
438     else if(!dquant && has_mvd && has_mvd24 && !has_dc) mode = 2;
439     else if(!dquant && !has_mvd && !has_mvd24 && has_dc) mode = 3;
440     else if(dquant && !has_mvd && !has_mvd24 && has_dc) mode = 4;
441     else exit(1);
442
443     swf_SetBits(tag,0,1); /* cod - 1 if we're not going to code this block*/
444         
445     codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
446     codehuffman(tag, cbpy, (mode==3 || mode==4)?cbpybits:cbpybits^15);
447
448     if(!bx&&!by) {
449         printf("cbpcbits: %d\n", cbpcbits);
450         printf("cbpybits: %d\n", cbpybits);
451     }
452
453     if(dquant) {
454         setQuant(tag, dquant);
455     }
456
457     if(has_mvd) {
458     }
459     if(has_mvd24) {
460     }
461
462     /* luminance */
463     encode8x8(tag, b.y1, has_dc, cbpybits&8);
464     encode8x8(tag, b.y2, has_dc, cbpybits&4);
465     encode8x8(tag, b.y3, has_dc, cbpybits&2);
466     encode8x8(tag, b.y4, has_dc, cbpybits&1);
467
468     /* chrominance */
469     encode8x8(tag, b.u, has_dc, cbpcbits&2);
470     encode8x8(tag, b.v, has_dc, cbpcbits&1);
471 }
472
473 #define TYPE_IFRAME 0
474 #define TYPE_PFRAME 1
475
476 static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
477 {
478     U32 i32;
479     swf_SetU16(tag, frame);
480     swf_SetBits(tag, 1, 17); /* picture start code*/
481     swf_SetBits(tag, 0, 5); /* version=0, version 1 would optimize rle behaviour*/
482     swf_SetBits(tag, frame, 8); /* time reference */
483
484     /* write dimensions, taking advantage of some predefined sizes
485        if the opportunity presents itself */
486     i32 = width<<16|height;
487     switch(i32)
488     {
489         case 352<<16|288: swf_SetBits(tag, 2, 3);break;
490         case 176<<16|144: swf_SetBits(tag, 3, 3);break;
491         case 128<<16|96: swf_SetBits(tag, 4, 3);break;
492         case 320<<16|240: swf_SetBits(tag, 5, 3);break;
493         case 160<<16|120: swf_SetBits(tag, 6, 3);break;
494         default:
495             if(width>255 || height>255) {
496                 swf_SetBits(tag, 1, 3);
497                 swf_SetBits(tag, width, 16);
498                 swf_SetBits(tag, height, 16);
499             } else {
500                 swf_SetBits(tag, 0, 3);
501                 swf_SetBits(tag, width, 8);
502                 swf_SetBits(tag, height, 8);
503             }
504     }
505
506     swf_SetBits(tag, type, 2); /* I-Frame or P-Frame */
507     swf_SetBits(tag, 0, 1); /* No deblock filter */
508     swf_SetBits(tag, quant, 5); /* quantizer (1-31), may be updated later on*/
509     swf_SetBits(tag, 0, 1); /* No extra info */
510 }
511
512 void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, U16 width, U16 height, int frame)
513 {
514     int bx, by, bbx, bby;
515     int quant = 7;
516
517     /* TODO: width not divisible by 16 will get us in trouble */
518     width=width&~15; height=height&~15;
519
520     writeHeader(tag, width, height, frame, quant, TYPE_IFRAME);
521
522     bbx = (width+15)/16;
523     bby = (height+15)/16;
524
525     for(by=0;by<bby;by++)
526     {
527         for(bx=0;bx<bbx;bx++)
528         {
529             encode_blockI(tag, pic, bx, by, width, height, &quant);
530         }
531     }
532 }
533
534 void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, U16 width, U16 height, int frame)
535 {
536     int bx, by, bbx, bby;
537     int quant = 7;
538
539     /* TODO: width not divisible by 16 will get us in trouble */
540     width=width&~15; height=height&~15;
541
542     writeHeader(tag, width, height, frame, quant, TYPE_PFRAME);
543
544     bbx = (width+15)/16;
545     bby = (height+15)/16;
546
547     for(by=0;by<bby;by++)
548     {
549         for(bx=0;bx<bbx;bx++)
550         {
551             encode_blockP(tag, pic, bx, by, width, height, &quant);
552         }
553     }
554 }
555
556 int main(int argn, char*argv[])
557 {
558     int fi;
559     int t;
560     SWF swf;
561     TAG * tag;
562     RGBA* pic, rgb;
563     SWFPLACEOBJECT obj;
564     int width = 0;
565     int height = 0;
566     int frames = 2;
567     unsigned char*data;
568     char* fname = "/home/kramm/pics/peppers.png";
569     VIDEOSTREAM stream;
570
571     memset(&stream, 0, sizeof(stream));
572
573     getPNG(fname, &width, &height, &data);
574     pic = (RGBA*)malloc(width*height*sizeof(RGBA));
575     memcpy(pic, data, width*height*sizeof(RGBA));
576     free(data);
577
578     printf("Compressing %s, size %dx%d\n", fname, width, height);
579
580     memset(&swf,0,sizeof(SWF));
581     memset(&obj,0,sizeof(obj));
582
583     swf.fileVersion    = 6;
584     swf.frameRate      = 29*256;
585     swf.movieSize.xmax = 20*width;
586     swf.movieSize.ymax = 20*height;
587
588     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
589     tag = swf.firstTag;
590     rgb.r = 0x00;rgb.g = 0x00;rgb.b = 0x00;
591     swf_SetRGB(tag,&rgb);
592
593     tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
594     swf_SetU16(tag, 33);
595     swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
596     
597     for(t=0;t<frames;t++)
598     {
599         tag = swf_InsertTag(tag, ST_VIDEOFRAME);
600         swf_SetU16(tag, 33);
601         if(t==0)
602             swf_SetVideoStreamIFrame(tag, &stream, pic, width, height, t);
603         else
604             swf_SetVideoStreamPFrame(tag, &stream, pic, width, height, t);
605
606         tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
607         swf_GetPlaceObject(0, &obj);
608         if(t==0) {
609             obj.depth = 1;
610             obj.id = 33;
611         } else {
612             obj.move = 1;
613             obj.depth = 1;
614             obj.ratio = t;
615         }
616         swf_SetPlaceObject(tag,&obj);
617
618         tag = swf_InsertTag(tag, ST_SHOWFRAME);
619     }
620    
621     tag = swf_InsertTag(tag, ST_END);
622
623     fi = open("video3.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
624     if(swf_WriteSWF(fi,&swf)<0) {
625         fprintf(stderr,"WriteSWF() failed.\n");
626     }
627     close(fi);
628     swf_FreeTags(&swf);
629 }