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 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)
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}
104 c+=table[u][x]*src[v*8+x];
114 c+=table[v][y]*tmp[y*8+u];
120 void idct(double*src)
130 c+=table[u][x]*src[y*8+u];
140 c+=table[v][y]*tmp[v*8+x];
146 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);
185 /* TODO: what to do for zero values? skip the block? */
195 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 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 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 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?) */
263 /* blocks without coefficients should not be included
264 in the cbpy/cbpc patterns */
271 while(!bb[pos] && pos<last) {
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);
293 codehuffman(tag, rle, RLE_ESCAPE);
297 if(level<-127) level = -127;
298 if(level>127) level = 127;
300 swf_SetBits(tag, islast, 1);
301 swf_SetBits(tag, run, 6);
302 swf_SetBits(tag, level, 8); //fixme
310 //codehuffman(tag, rle, 58);
311 //swf_SetBits(tag, 1, 1); //sign
315 void dodct(fblock_t*fb)
317 dct(fb->y1); dct(fb->y2); dct(fb->y3); dct(fb->y4);
318 dct(fb->u); dct(fb->v);
327 void quantize(fblock_t*fb, block_t*b, int has_dc, int quant)
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);
337 void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
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;
347 *cbpcbits|=hascoef(b->u, has_dc)*2;
348 *cbpcbits|=hascoef(b->v, has_dc)*1;
351 void setQuant(TAG*tag, int dquant)
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);
366 assert(0*strlen("invalid dquant"));
370 void change_quant(int quant, int*dquant)
376 void encode_blockI(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int*quant)
381 int cbpcbits = 0, cbpybits=0;
383 getregion(&fb, pic, bx, by, width, height);
386 change_quant(*quant, &dquant);
389 quantize(&fb, &b, 1, *quant);
390 getblockpatterns(&b, &cbpybits, &cbpcbits, 1);
393 codehuffman(tag, mcbpc_intra, 4+cbpcbits);
395 codehuffman(tag, mcbpc_intra, 0+cbpcbits);
398 codehuffman(tag, cbpy, cbpybits);
401 setQuant(tag, dquant);
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);
411 encode8x8(tag, b.u, 1, cbpcbits&2);
412 encode8x8(tag, b.v, 1, cbpcbits&1);
415 void encode_blockP(TAG*tag, RGBA*pic, int bx, int by, int width, int height, int*quant)
424 int cbpcbits = 0, cbpybits=0;
426 getregion(&fb, pic, bx, by, width, height);
429 change_quant(*quant, &dquant);
432 quantize(&fb, &b, has_dc, *quant);
434 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
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;
443 swf_SetBits(tag,0,1); /* cod - 1 if we're not going to code this block*/
445 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
446 codehuffman(tag, cbpy, (mode==3 || mode==4)?cbpybits:cbpybits^15);
449 printf("cbpcbits: %d\n", cbpcbits);
450 printf("cbpybits: %d\n", cbpybits);
454 setQuant(tag, dquant);
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);
469 encode8x8(tag, b.u, has_dc, cbpcbits&2);
470 encode8x8(tag, b.v, has_dc, cbpcbits&1);
473 #define TYPE_IFRAME 0
474 #define TYPE_PFRAME 1
476 static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
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 */
484 /* write dimensions, taking advantage of some predefined sizes
485 if the opportunity presents itself */
486 i32 = width<<16|height;
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;
495 if(width>255 || height>255) {
496 swf_SetBits(tag, 1, 3);
497 swf_SetBits(tag, width, 16);
498 swf_SetBits(tag, height, 16);
500 swf_SetBits(tag, 0, 3);
501 swf_SetBits(tag, width, 8);
502 swf_SetBits(tag, height, 8);
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 */
512 void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, U16 width, U16 height, int frame)
514 int bx, by, bbx, bby;
517 /* TODO: width not divisible by 16 will get us in trouble */
518 width=width&~15; height=height&~15;
520 writeHeader(tag, width, height, frame, quant, TYPE_IFRAME);
523 bby = (height+15)/16;
525 for(by=0;by<bby;by++)
527 for(bx=0;bx<bbx;bx++)
529 encode_blockI(tag, pic, bx, by, width, height, &quant);
534 void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, U16 width, U16 height, int frame)
536 int bx, by, bbx, bby;
539 /* TODO: width not divisible by 16 will get us in trouble */
540 width=width&~15; height=height&~15;
542 writeHeader(tag, width, height, frame, quant, TYPE_PFRAME);
545 bby = (height+15)/16;
547 for(by=0;by<bby;by++)
549 for(bx=0;bx<bbx;bx++)
551 encode_blockP(tag, pic, bx, by, width, height, &quant);
556 int main(int argn, char*argv[])
568 char* fname = "/home/kramm/pics/peppers.png";
571 memset(&stream, 0, sizeof(stream));
573 getPNG(fname, &width, &height, &data);
574 pic = (RGBA*)malloc(width*height*sizeof(RGBA));
575 memcpy(pic, data, width*height*sizeof(RGBA));
578 printf("Compressing %s, size %dx%d\n", fname, width, height);
580 memset(&swf,0,sizeof(SWF));
581 memset(&obj,0,sizeof(obj));
584 swf.frameRate = 29*256;
585 swf.movieSize.xmax = 20*width;
586 swf.movieSize.ymax = 20*height;
588 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
590 rgb.r = 0x00;rgb.g = 0x00;rgb.b = 0x00;
591 swf_SetRGB(tag,&rgb);
593 tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
595 swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
597 for(t=0;t<frames;t++)
599 tag = swf_InsertTag(tag, ST_VIDEOFRAME);
602 swf_SetVideoStreamIFrame(tag, &stream, pic, width, height, t);
604 swf_SetVideoStreamPFrame(tag, &stream, pic, width, height, t);
606 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
607 swf_GetPlaceObject(0, &obj);
616 swf_SetPlaceObject(tag,&obj);
618 tag = swf_InsertTag(tag, ST_SHOWFRAME);
621 tag = swf_InsertTag(tag, ST_END);
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");