4 Part of the swftools package.
6 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org> */
12 #include "../lib/rfxswf.h"
14 #include "h263tables.c"
16 typedef struct _VIDEOSTREAM
23 void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width, U16 height)
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 */
32 stream->width = width;
33 stream->height = height;
37 typedef struct _block_t
47 typedef struct _fblock_t
57 static void fzigzag(double*src)
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};
71 ((int*)&tmp[table[t]])[0] = ((int*)&src[t])[0];
72 ((int*)&tmp[table[t]])[1] = ((int*)&src[t])[1];
74 memcpy(src, tmp, sizeof(double)*64);
77 #define PI 3.14159265358979
78 #define SQRT2 1.414214
79 #define RSQRT2 (1.0/1.414214)
81 static double table[8][8] =
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}
93 static void dct(double*src)
104 c+=table[u][x]*src[v*8+x];
114 c+=table[v][y]*tmp[y*8+u];
120 static void idct(double*src)
130 c+=table[u][x]*src[y*8+u];
140 c+=table[v][y]*tmp[v*8+x];
146 static void getregion(fblock_t* bb, RGBA*pic, int bx, int by, int width, int height)
148 RGBA*p1 = &pic[by*width*16+bx*16];
151 int y1=0, y2=0, y3=0, y4=0;
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);
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);
177 static int valtodc(int val)
185 /* TODO: what to do for zero values? skip the block? */
195 static void codehuffman(TAG*tag, struct huffcode*table, int index)
197 /* TODO: !optimize! */
199 while(table[index].code[i]) {
200 if(table[index].code[i]=='0')
201 swf_SetBits(tag, 0, 1);
203 swf_SetBits(tag, 1, 1);
208 static void quantize8x8(double*src, int*dest, int has_dc, int quant)
212 dest[0] = valtodc((int)src[0]); /*DC*/
217 dest[t] = (int)src[t];
218 //val = (quant*(2*level+1)-1)+quant&1
220 dest[t] = (dest[t]/quant - 1)/2;
222 dest[t] = ((dest[t]+1)/quant - 1)/2;
224 //dest[t] = (dest[t]/quant-1)/2;
225 dest[t] = dest[t]/(quant*2);
229 static int hascoef(int*b, int has_dc)
233 int range=2; /*TODO: should be a global parameter */
236 for(t=pos;t<64;t++) {
237 if(b[t]<=-range || b[t]>=range)
243 static void encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
249 swf_SetBits(tag, bb[0], 8);
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?) */
262 /* blocks without coefficients should not be included
263 in the cbpy/cbpc patterns: */
272 while(!bb[pos] && pos<last) {
284 for(t=0;t<RLE_ESCAPE;t++) {
285 if(rle_params[t].run == run &&
286 rle_params[t].level == level &&
287 rle_params[t].last == islast) {
288 codehuffman(tag, rle, t);
289 swf_SetBits(tag, sign, 1);
294 codehuffman(tag, rle, RLE_ESCAPE);
298 if(level<-127) level = -127;
299 if(level>127) level = 127;
301 swf_SetBits(tag, islast, 1);
302 swf_SetBits(tag, run, 6);
303 swf_SetBits(tag, level, 8); //fixme
311 //codehuffman(tag, rle, 58);
312 //swf_SetBits(tag, 1, 1); //sign
316 static void dodct(fblock_t*fb)
318 dct(fb->y1); dct(fb->y2); dct(fb->y3); dct(fb->y4);
319 dct(fb->u); dct(fb->v);
328 static void quantize(fblock_t*fb, block_t*b, int has_dc, int quant)
330 quantize8x8(fb->y1,b->y1,has_dc,quant);
331 quantize8x8(fb->y2,b->y2,has_dc,quant);
332 quantize8x8(fb->y3,b->y3,has_dc,quant);
333 quantize8x8(fb->y4,b->y4,has_dc,quant);
334 quantize8x8(fb->u,b->u,has_dc,quant);
335 quantize8x8(fb->v,b->v,has_dc,quant);
338 static void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
343 *cbpybits|=hascoef(b->y1, has_dc)*8;
344 *cbpybits|=hascoef(b->y2, has_dc)*4;
345 *cbpybits|=hascoef(b->y3, has_dc)*2;
346 *cbpybits|=hascoef(b->y4, has_dc)*1;
348 *cbpcbits|=hascoef(b->u, has_dc)*2;
349 *cbpcbits|=hascoef(b->v, has_dc)*1;
352 static void setQuant(TAG*tag, int dquant)
359 swf_SetBits(tag, 0x0, 2);
360 } else if(dquant == -2) {
361 swf_SetBits(tag, 0x1, 2);
362 } else if(dquant == +1) {
363 swf_SetBits(tag, 0x2, 2);
364 } else if(dquant == +2) {
365 swf_SetBits(tag, 0x3, 2);
367 assert(0*strlen("invalid dquant"));
371 static void change_quant(int quant, int*dquant)
377 void encode_blockI(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int*quant)
382 int cbpcbits = 0, cbpybits=0;
384 getregion(&fb, pic, bx, by, width, height);
387 change_quant(*quant, &dquant);
390 quantize(&fb, &b, 1, *quant);
391 getblockpatterns(&b, &cbpybits, &cbpcbits, 1);
394 codehuffman(tag, mcbpc_intra, 4+cbpcbits);
396 codehuffman(tag, mcbpc_intra, 0+cbpcbits);
399 codehuffman(tag, cbpy, cbpybits);
402 setQuant(tag, dquant);
406 encode8x8(tag, b.y1, 1, cbpybits&8);
407 encode8x8(tag, b.y2, 1, cbpybits&4);
408 encode8x8(tag, b.y3, 1, cbpybits&2);
409 encode8x8(tag, b.y4, 1, cbpybits&1);
412 encode8x8(tag, b.u, 1, cbpcbits&2);
413 encode8x8(tag, b.v, 1, cbpcbits&1);
416 void encode_blockP(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int*quant)
425 int cbpcbits = 0, cbpybits=0;
427 getregion(&fb, pic, bx, by, width, height);
430 change_quant(*quant, &dquant);
433 quantize(&fb, &b, has_dc, *quant);
435 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
437 if(!dquant && has_mvd && !has_mvd24 && !has_dc) mode = 0;
438 else if(dquant && has_mvd && !has_mvd24 && !has_dc) mode = 1;
439 else if(!dquant && has_mvd && has_mvd24 && !has_dc) mode = 2;
440 else if(!dquant && !has_mvd && !has_mvd24 && has_dc) mode = 3;
441 else if(dquant && !has_mvd && !has_mvd24 && has_dc) mode = 4;
444 swf_SetBits(tag,0,1); /* cod - 1 if we're not going to code this block*/
446 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
447 codehuffman(tag, cbpy, (mode==3 || mode==4)?cbpybits:cbpybits^15);
450 printf("cbpcbits: %d\n", cbpcbits);
451 printf("cbpybits: %d\n", cbpybits);
455 setQuant(tag, dquant);
464 encode8x8(tag, b.y1, has_dc, cbpybits&8);
465 encode8x8(tag, b.y2, has_dc, cbpybits&4);
466 encode8x8(tag, b.y3, has_dc, cbpybits&2);
467 encode8x8(tag, b.y4, has_dc, cbpybits&1);
470 encode8x8(tag, b.u, has_dc, cbpcbits&2);
471 encode8x8(tag, b.v, has_dc, cbpcbits&1);
474 #define TYPE_IFRAME 0
475 #define TYPE_PFRAME 1
477 static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
480 swf_SetU16(tag, frame);
481 swf_SetBits(tag, 1, 17); /* picture start code*/
482 swf_SetBits(tag, 0, 5); /* version=0, version 1 would optimize rle behaviour*/
483 swf_SetBits(tag, frame, 8); /* time reference */
485 /* write dimensions, taking advantage of some predefined sizes
486 if the opportunity presents itself */
487 i32 = width<<16|height;
490 case 352<<16|288: swf_SetBits(tag, 2, 3);break;
491 case 176<<16|144: swf_SetBits(tag, 3, 3);break;
492 case 128<<16|96: swf_SetBits(tag, 4, 3);break;
493 case 320<<16|240: swf_SetBits(tag, 5, 3);break;
494 case 160<<16|120: swf_SetBits(tag, 6, 3);break;
496 if(width>255 || height>255) {
497 swf_SetBits(tag, 1, 3);
498 swf_SetBits(tag, width, 16);
499 swf_SetBits(tag, height, 16);
501 swf_SetBits(tag, 0, 3);
502 swf_SetBits(tag, width, 8);
503 swf_SetBits(tag, height, 8);
507 swf_SetBits(tag, type, 2); /* I-Frame or P-Frame */
508 swf_SetBits(tag, 0, 1); /* No deblock filter */
509 swf_SetBits(tag, quant, 5); /* quantizer (1-31), may be updated later on*/
510 swf_SetBits(tag, 0, 1); /* No extra info */
513 void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, U16 width, U16 height, int frame)
515 int bx, by, bbx, bby;
518 /* TODO: width not divisible by 16 will get us in trouble */
519 width=width&~15; height=height&~15;
521 writeHeader(tag, width, height, frame, quant, TYPE_IFRAME);
524 bby = (height+15)/16;
526 for(by=0;by<bby;by++)
528 for(bx=0;bx<bbx;bx++)
530 encode_blockI(tag, pic, bx, by, width, height, &quant);
535 void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, U16 width, U16 height, int frame)
537 int bx, by, bbx, bby;
540 /* TODO: width not divisible by 16 will get us in trouble */
541 width=width&~15; height=height&~15;
543 writeHeader(tag, width, height, frame, quant, TYPE_PFRAME);
546 bby = (height+15)/16;
548 for(by=0;by<bby;by++)
550 for(bx=0;bx<bbx;bx++)
552 encode_blockP(tag, pic, bx, by, width, height, &quant);
557 int main(int argn, char*argv[])
569 char* fname = "/home/kramm/pics/peppers.png";
572 memset(&stream, 0, sizeof(stream));
574 getPNG(fname, &width, &height, &data);
575 pic = (RGBA*)malloc(width*height*sizeof(RGBA));
576 memcpy(pic, data, width*height*sizeof(RGBA));
579 printf("Compressing %s, size %dx%d\n", fname, width, height);
581 memset(&swf,0,sizeof(SWF));
582 memset(&obj,0,sizeof(obj));
585 swf.frameRate = 29*256;
586 swf.movieSize.xmax = 20*width;
587 swf.movieSize.ymax = 20*height;
589 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
591 rgb.r = 0x00;rgb.g = 0x00;rgb.b = 0x00;
592 swf_SetRGB(tag,&rgb);
594 tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
596 swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
598 for(t=0;t<frames;t++)
600 tag = swf_InsertTag(tag, ST_VIDEOFRAME);
603 swf_SetVideoStreamIFrame(tag, &stream, pic, width, height, t);
605 swf_SetVideoStreamPFrame(tag, &stream, pic, width, height, t);
607 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
608 swf_GetPlaceObject(0, &obj);
617 swf_SetPlaceObject(tag,&obj);
619 tag = swf_InsertTag(tag, ST_SHOWFRAME);
622 tag = swf_InsertTag(tag, ST_END);
624 fi = open("video3.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
625 if(swf_WriteSWF(fi,&swf)<0) {
626 fprintf(stderr,"WriteSWF() failed.\n");