4 Part of the swftools package.
6 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org> */
12 #include "../rfxswf.h"
13 #include "h263tables.c"
16 void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width, U16 height)
18 width=width&~15; height=height&~15;
19 swf_SetU16(tag, frames);
20 swf_SetU16(tag, width);
21 swf_SetU16(tag, height);
22 swf_SetU8(tag, 1); /* smoothing on */
23 swf_SetU8(tag, 2); /* codec = h.263 sorenson spark */
25 memset(stream, 0, sizeof(VIDEOSTREAM));
26 stream->olinex = width;
28 height+=15;height&=~15;
29 stream->linex = width;
30 stream->width = width;
31 stream->height = height;
32 stream->bbx = width/16;
33 stream->bby = height/16;
34 stream->current = (YUV*)malloc(width*height*sizeof(YUV));
35 stream->oldpic = (YUV*)malloc(width*height*sizeof(YUV));
36 stream->mvdx = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
37 stream->mvdy = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
38 stream->do_motion = 0;
40 memset(stream->oldpic, 0, width*height*sizeof(YUV));
41 memset(stream->current, 0, width*height*sizeof(YUV));
43 void swf_VideoStreamClear(VIDEOSTREAM*stream)
45 free(stream->oldpic);stream->oldpic = 0;
46 free(stream->current);stream->current = 0;
47 free(stream->mvdx);stream->mvdx=0;
48 free(stream->mvdy);stream->mvdy=0;
51 typedef struct _block_t
61 typedef struct _fblock_t
71 static int zigzagtable[64] = {
72 0, 1, 5, 6, 14, 15, 27, 28,
73 2, 4, 7, 13, 16, 26, 29, 42,
74 3, 8, 12, 17, 25, 30, 41, 43,
75 9, 11, 18, 24, 31, 40, 44, 53,
76 10, 19, 23, 32, 39, 45, 52, 54,
77 20, 22, 33, 38, 46, 51, 55, 60,
78 21, 34, 37, 47, 50, 56, 59, 61,
79 35, 36, 48, 49, 57, 58, 62, 63};
81 static void fzigzag(double*src)
86 ((int*)&tmp[zigzagtable[t]])[0] = ((int*)&src[t])[0];
87 ((int*)&tmp[zigzagtable[t]])[1] = ((int*)&src[t])[1];
89 memcpy(src, tmp, sizeof(double)*64);
92 #define PI 3.14159265358979
93 #define SQRT2 1.414214
94 #define RSQRT2 (1.0/1.414214)
96 static double table[8][8] =
98 {0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548},
99 {0.980785280403230,0.831469612302545,0.555570233019602,0.195090322016128,-0.195090322016128,-0.555570233019602,-0.831469612302545,-0.980785280403230},
100 {0.923879532511287,0.382683432365090,-0.382683432365090,-0.923879532511287,-0.923879532511287,-0.382683432365090,0.382683432365090,0.923879532511287},
101 {0.831469612302545,-0.195090322016128,-0.980785280403230,-0.555570233019602,0.555570233019602,0.980785280403230,0.195090322016129,-0.831469612302545},
102 {0.707106781186548,-0.707106781186547,-0.707106781186548,0.707106781186547,0.707106781186548,-0.707106781186547,-0.707106781186547,0.707106781186547},
103 {0.555570233019602,-0.980785280403230,0.195090322016128,0.831469612302545,-0.831469612302545,-0.195090322016128,0.980785280403231,-0.555570233019602},
104 {0.382683432365090,-0.923879532511287,0.923879532511287,-0.382683432365090,-0.382683432365091,0.923879532511287,-0.923879532511286,0.382683432365090},
105 {0.195090322016128,-0.555570233019602,0.831469612302545,-0.980785280403231,0.980785280403230,-0.831469612302545,0.555570233019602,-0.195090322016129}
108 static void dct(double*src)
119 c+=table[u][x]*src[v*8+x];
129 c+=table[v][y]*tmp[y*8+u];
135 static void idct(double*src)
145 c+=table[u][x]*src[y*8+u];
155 c+=table[v][y]*tmp[v*8+x];
161 static inline int truncate256(int a)
163 if(a>255) return 255;
168 static void getregion(fblock_t* bb, YUV*pic, int posx, int posy, int linex)
170 YUV*p1 = &pic[posy*linex+posx];
172 int y1=0, y2=0, y3=0, y4=0;
177 bb->u[u++] = (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
178 bb->v[v++] = (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
179 bb->y1[y1++] = p1[x].y;
180 bb->y2[y2++] = p1[x+8].y;
181 bb->y3[y3++] = p1[linex*8+x].y;
182 bb->y4[y4++] = p1[linex*8+x+8].y;
188 static void rgb2yuv(YUV*dest, RGBA*src, int dlinex, int slinex, int width, int height)
191 for(y=0;y<height;y++) {
192 for(x=0;x<width;x++) {
194 r = src[y*slinex+x].r;
195 g = src[y*slinex+x].g;
196 b = src[y*slinex+x].b;
197 /*dest[y*dlinex+x].y = (r*0.299 + g*0.587 + b*0.114);
198 dest[y*dlinex+x].u = (r*-0.169 + g*-0.332 + b*0.500 + 128.0);
199 dest[y*dlinex+x].v = (r*0.500 + g*-0.419 + b*-0.0813 + 128.0);*/
200 dest[y*dlinex+x].y = (r*((int)( 0.299*256)) + g*((int)( 0.587*256)) + b*((int)( 0.114 *256)))>>8;
201 dest[y*dlinex+x].u = (r*((int)(-0.169*256)) + g*((int)(-0.332*256)) + b*((int)( 0.500 *256))+ 128*256)>>8;
202 dest[y*dlinex+x].v = (r*((int)( 0.500*256)) + g*((int)(-0.419*256)) + b*((int)(-0.0813*256))+ 128*256)>>8;
206 static void copyregion(VIDEOSTREAM*s, YUV*dest, YUV*src, int bx, int by)
208 YUV*p1 = &src[by*s->linex*16+bx*16];
209 YUV*p2 = &dest[by*s->linex*16+bx*16];
212 memcpy(p1, p2, 16*sizeof(YUV));
213 p1+=s->linex;p2+=s->linex;
217 static void yuv2rgb(RGBA*dest, YUV*src, int linex, int width, int height)
220 for(y=0;y<height;y++) {
221 for(x=0;x<width;x++) {
223 u = src[y*linex+x].u;
224 v = src[y*linex+x].v;
225 yy = src[y*linex+x].y;
226 dest[y*linex+x].r = truncate256(yy + ((360*(v-128))>>8));
227 dest[y*linex+x].g = truncate256(yy - ((88*(u-128)+183*(v-128))>>8));
228 dest[y*linex+x].b = truncate256(yy + ((455 * (u-128))>>8));
232 static void copyblock(VIDEOSTREAM*s, YUV*dest, block_t*b, int bx, int by)
234 YUV*p1 = &dest[(by*16)*s->linex+bx*16];
235 YUV*p2 = &dest[(by*16+8)*s->linex+bx*16];
240 p1[x+0].u = b->u[(y/2)*8+(x/2)];
241 p1[x+0].v = b->v[(y/2)*8+(x/2)];
242 p1[x+0].y = b->y1[y*8+x];
243 p1[x+8].u = b->u[(y/2)*8+(x/2)+4];
244 p1[x+8].v = b->v[(y/2)*8+(x/2)+4];
245 p1[x+8].y = b->y2[y*8+x];
246 p2[x+0].u = b->u[(y/2+4)*8+(x/2)];
247 p2[x+0].v = b->v[(y/2+4)*8+(x/2)];
248 p2[x+0].y = b->y3[y*8+x];
249 p2[x+8].u = b->u[(y/2+4)*8+(x/2)+4];
250 p2[x+8].v = b->v[(y/2+4)*8+(x/2)+4];
251 p2[x+8].y = b->y4[y*8+x];
258 static int compareregions(VIDEOSTREAM*s, int bx, int by)
260 int linex = s->width;
261 YUV*p1 = &s->current[by*linex*16+bx*16];
262 YUV*p2 = &s->oldpic[by*linex*16+bx*16];
272 diff += y*y+(u*u+v*v)/4;
280 static inline int valtodc(int val)
288 /* TODO: what to do for zero values? skip the block? */
297 static int dctoval(int dc)
310 static int codehuffman(TAG*tag, struct huffcode*table, int index)
312 /* TODO: !optimize! */
314 while(table[index].code[i]) {
315 if(table[index].code[i]=='0')
316 swf_SetBits(tag, 0, 1);
318 swf_SetBits(tag, 1, 1);
324 static void quantize8x8(double*src, int*dest, int has_dc, int quant)
327 double q = 1.0/(quant*2);
329 dest[0] = valtodc((int)src[0]); /*DC*/
334 //dest[t] = (int)src[t];
335 /* exact: if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;} */
336 //if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;}
337 //dest[t] = dest[t]/(quant*2);
338 dest[t] = (int)(src[t]*q);
339 /* TODO: warn if this happens- the video will be buggy */
340 if(dest[t]>127) dest[t]=127;
341 if(dest[t]<-127) dest[t]=-127;
345 static void dequantize8x8(int*b, int has_dc, int quant)
349 b[0] = dctoval(b[0]); //DC
352 for(t=pos;t<64;t++) {
361 b[t] = quant*(2*b[t]+1); //-7,8,24,40
363 b[t] = quant*(2*b[t]+1)-1; //-8,7,23,39
370 /* paragraph 6.2.2, "clipping of reconstruction levels": */
371 if(b[t]>2047) b[t]=2047;
372 if(b[t]<-2048) b[t]=-2048;
376 static int hascoef(int*b, int has_dc)
382 for(t=pos;t<64;t++) {
389 static int coefbits8x8(int*bb, int has_dc)
400 for(last=63;last>=pos;last--) {
407 int run=0, level=0, islast=0,t;
408 while(!bb[pos] && pos<last) {
415 if(level<0) level=-level;
417 for(t=0;t<RLE_ESCAPE;t++) {
418 if(rle_params[t].run == run &&
419 rle_params[t].level == level &&
420 rle_params[t].last == islast) {
421 bits += rle[t].len + 1;
426 bits += rle[RLE_ESCAPE].len + 1 + 6 + 8;
435 static void encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
442 swf_SetBits(tag, bb[0], 8);
448 /* determine last non-null coefficient */
449 for(last=63;last>=pos;last--) {
450 /* TODO: we could leave out small coefficients
451 after a certain point (32?) */
455 /* blocks without coefficients should not be included
456 in the cbpy/cbpc patterns: */
465 while(!bb[pos] && pos<last) {
477 for(t=0;t<RLE_ESCAPE;t++) {
478 /* TODO: lookup table */
479 if(rle_params[t].run == run &&
480 rle_params[t].level == level &&
481 rle_params[t].last == islast) {
482 codehuffman(tag, rle, t);
483 swf_SetBits(tag, sign, 1);
488 codehuffman(tag, rle, RLE_ESCAPE);
495 swf_SetBits(tag, islast, 1);
496 swf_SetBits(tag, run, 6);
497 swf_SetBits(tag, level, 8); //FIXME: fixme??
507 static void dodct(fblock_t*fb)
510 dct(fb->y1); dct(fb->y2); dct(fb->y3); dct(fb->y4);
511 dct(fb->u); dct(fb->v);
520 static void doidct(block_t*b)
525 fb.y1[t] = b->y1[zigzagtable[t]];
526 fb.y2[t] = b->y2[zigzagtable[t]];
527 fb.y3[t] = b->y3[zigzagtable[t]];
528 fb.y4[t] = b->y4[zigzagtable[t]];
529 fb.u[t] = b->u[zigzagtable[t]];
530 fb.v[t] = b->v[zigzagtable[t]];
532 idct(fb.y1); idct(fb.y2); idct(fb.y3); idct(fb.y4);
533 idct(fb.u); idct(fb.v);
543 static void truncateblock(block_t*b)
547 b->y1[t] = truncate256(b->y1[t]);
548 b->y2[t] = truncate256(b->y2[t]);
549 b->y3[t] = truncate256(b->y3[t]);
550 b->y4[t] = truncate256(b->y4[t]);
551 b->u[t] = truncate256(b->u[t]);
552 b->v[t] = truncate256(b->v[t]);
556 static void quantize(fblock_t*fb, block_t*b, int has_dc, int quant)
558 quantize8x8(fb->y1, b->y1, has_dc, quant);
559 quantize8x8(fb->y2, b->y2, has_dc, quant);
560 quantize8x8(fb->y3, b->y3, has_dc, quant);
561 quantize8x8(fb->y4, b->y4, has_dc, quant);
562 quantize8x8(fb->u, b->u, has_dc, quant);
563 quantize8x8(fb->v, b->v, has_dc, quant);
565 static void dequantize(block_t*b, int has_dc, int quant)
567 dequantize8x8(b->y1, has_dc, quant);
568 dequantize8x8(b->y2, has_dc, quant);
569 dequantize8x8(b->y3, has_dc, quant);
570 dequantize8x8(b->y4, has_dc, quant);
571 dequantize8x8(b->u, has_dc, quant);
572 dequantize8x8(b->v, has_dc, quant);
575 static void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
580 *cbpybits|=hascoef(b->y1, has_dc)*8;
581 *cbpybits|=hascoef(b->y2, has_dc)*4;
582 *cbpybits|=hascoef(b->y3, has_dc)*2;
583 *cbpybits|=hascoef(b->y4, has_dc)*1;
585 *cbpcbits|=hascoef(b->u, has_dc)*2;
586 *cbpcbits|=hascoef(b->v, has_dc)*1;
589 static void setQuant(TAG*tag, int dquant)
596 swf_SetBits(tag, 0x0, 2);
597 } else if(dquant == -2) {
598 swf_SetBits(tag, 0x1, 2);
599 } else if(dquant == +1) {
600 swf_SetBits(tag, 0x2, 2);
601 } else if(dquant == +2) {
602 swf_SetBits(tag, 0x3, 2);
604 assert(0*strlen("invalid dquant"));
608 static void change_quant(int quant, int*dquant)
614 static void encode_blockI(TAG*tag, VIDEOSTREAM*s, int bx, int by, int*quant)
619 int cbpcbits = 0, cbpybits=0;
621 getregion(&fb, s->current, bx*16, by*16, s->width);
624 change_quant(*quant, &dquant);
626 quantize(&fb, &b, 1, *quant);
628 //decode_blockI(s, &b, bx, by);
630 getblockpatterns(&b, &cbpybits, &cbpcbits, 1);
633 codehuffman(tag, mcbpc_intra, 4+cbpcbits);
635 codehuffman(tag, mcbpc_intra, 0+cbpcbits);
638 codehuffman(tag, cbpy, cbpybits);
641 setQuant(tag, dquant);
645 encode8x8(tag, b.y1, 1, cbpybits&8);
646 encode8x8(tag, b.y2, 1, cbpybits&4);
647 encode8x8(tag, b.y3, 1, cbpybits&2);
648 encode8x8(tag, b.y4, 1, cbpybits&1);
651 encode8x8(tag, b.u, 1, cbpcbits&2);
652 encode8x8(tag, b.v, 1, cbpcbits&1);
655 dequantize(&b, 1, *quant);
658 copyblock(s, s->current, &b, bx, by);
661 static void yuvdiff(fblock_t*a, fblock_t*b)
665 a->y1[t] = (a->y1[t] - b->y1[t]);
666 a->y2[t] = (a->y2[t] - b->y2[t]);
667 a->y3[t] = (a->y3[t] - b->y3[t]);
668 a->y4[t] = (a->y4[t] - b->y4[t]);
669 a->u[t] = (a->u[t] - b->u[t]);
670 a->v[t] = (a->v[t] - b->v[t]);
674 static void predictmvd(VIDEOSTREAM*s, int bx, int by, int*px, int*py)
677 int x1,y1,x2,y2,x3,y3;
679 if(bx) {x1=s->mvdx[by*s->bbx+bx-1];
680 y1=s->mvdy[by*s->bbx+bx-1];
683 if(by) {x2=s->mvdx[(by-1)*s->bbx+bx];
684 y2=s->mvdy[(by-1)*s->bbx+bx];
686 x3=s->mvdx[(by-1)*s->bbx+bx+1];
687 y3=s->mvdy[(by-1)*s->bbx+bx+1];
692 else {x2=x3=x1;y2=y3=y1;}
694 if((x1 <= x2 && x2 <= x3) ||
695 (x3 <= x2 && x2 <= x1)) {
697 } else if((x2 <= x1 && x1 <= x3) ||
698 (x3 <= x1 && x1 <= x2)) {
700 } else if((x1 <= x3 && x3 <= x2) ||
701 (x2 <= x3 && x3 <= x1)) {
705 if((y1 <= y2 && y2 <= y3) ||
706 (y3 <= y2 && y2 <= y1)) {
708 } else if((y2 <= y1 && y1 <= y3) ||
709 (y3 <= y1 && y1 <= y2)) {
711 } else if((y1 <= y3 && y3 <= y2) ||
712 (y2 <= y3 && y3 <= y1)) {
718 assert((x4>=-32 && x4<=31) && (y4>=-32 && y4<=31));
721 static inline int mvd2index(int px, int py, int x, int y, int xy)
723 assert((x>=-32 && x<=31) && (y>=-32 && y<=31));
724 assert((x&1)==0 && (y&1)==0);//for now
725 assert((x&2)==0 && (y&2)==0);//for now(2)
740 assert(x>=0 && x<64);
744 static int encode_blockP(TAG*tag, VIDEOSTREAM*s, int bx, int by, int*quant)
753 int cbpcbits = 0, cbpybits=0;
763 int bits_v00 = 65535;
767 diff = compareregions(s, bx, by);
768 if(diff < 20 /*TODO: should be a parameter- good values are between 32 and 48 */) {
769 swf_SetBits(tag, 1,1); /* cod=1, block skipped */
770 /* copy the region from the last frame so that we have a complete reconstruction */
771 copyregion(s, s->current, s->oldpic, bx, by);
775 predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
776 getregion(&fb, s->current, bx*16, by*16, s->width);
778 { /* consider I-block */
781 memcpy(&fb_i, &fb, sizeof(fblock_t));
783 quantize(&fb_i, &b_i, 1, *quant);
784 getblockpatterns(&b_i, &y, &c, 1);
786 bits_i += mcbpc_inter[3*4+c].len;
787 bits_i += cbpy[y].len;
788 bits_i += coefbits8x8(b_i.y1, 1);
789 bits_i += coefbits8x8(b_i.y2, 1);
790 bits_i += coefbits8x8(b_i.y3, 1);
791 bits_i += coefbits8x8(b_i.y4, 1);
792 bits_i += coefbits8x8(b_i.u, 1);
793 bits_i += coefbits8x8(b_i.v, 1);
796 if(bx&&by&&(bx<s->bbx-1)&&(by<s->bby-1))
797 { /* consider mvd(x,y)-block */
806 int bestx=0,besty=0,bestbits=65536;
808 for(hx=-8;hx<8;hx+=4)
809 for(hy=-8;hy<8;hy+=4)
814 memcpy(&fbdiff, &fb, sizeof(fblock_t));
815 getregion(&fbold, s->oldpic, bx*16+hx/2, by*16+hy/2, s->linex);
816 yuvdiff(&fbdiff, &fbold);
818 quantize(&fbdiff, &b, 0, *quant);
819 bits += coefbits8x8(b.y1, 0);
820 bits += coefbits8x8(b.y2, 0);
821 bits += coefbits8x8(b.y3, 0);
822 bits += coefbits8x8(b.y4, 0);
823 bits += coefbits8x8(b.u, 0);
824 bits += coefbits8x8(b.v, 0);
835 memcpy(&fbdiff, &fb, sizeof(fblock_t));
836 getregion(&fbold_v00, s->oldpic, bx*16+x_v00/2, by*16+y_v00/2, s->linex);
837 yuvdiff(&fbdiff, &fbold_v00);
839 quantize(&fbdiff, &b_v00, 0, *quant);
840 getblockpatterns(&b_v00, &y, &c, 0);
843 bits_v00 += mcbpc_inter[0*4+c].len;
844 bits_v00 += cbpy[y^15].len;
845 bits_v00 += mvd[mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 0)].len; // (0,0)
846 bits_v00 += mvd[mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 1)].len;
847 bits_v00 += coefbits8x8(b_v00.y1, 0);
848 bits_v00 += coefbits8x8(b_v00.y2, 0);
849 bits_v00 += coefbits8x8(b_v00.y3, 0);
850 bits_v00 += coefbits8x8(b_v00.y4, 0);
851 bits_v00 += coefbits8x8(b_v00.u, 0);
852 bits_v00 += coefbits8x8(b_v00.v, 0);
855 if(bits_i > bits_v00)
857 /* mvd (0,0) block (mode=0) */
859 mode = 0; // mvd w/o mvd24
861 memcpy(&b, &b_v00, sizeof(block_t));
863 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
864 swf_SetBits(tag,0,1); // COD
865 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
866 codehuffman(tag, cbpy, cbpybits^15);
869 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 0));
870 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 1));
871 s->mvdx[by*s->bbx+bx] = x_v00;
872 s->mvdy[by*s->bbx+bx] = y_v00;
875 encode8x8(tag, b.y1, has_dc, cbpybits&8);
876 encode8x8(tag, b.y2, has_dc, cbpybits&4);
877 encode8x8(tag, b.y3, has_dc, cbpybits&2);
878 encode8x8(tag, b.y4, has_dc, cbpybits&1);
881 encode8x8(tag, b.u, has_dc, cbpcbits&2);
882 encode8x8(tag, b.v, has_dc, cbpcbits&1);
884 /* -- reconstruction -- */
885 dequantize(&b, 0, *quant);
888 b.y1[t] = truncate256(b.y1[t] + (int)fbold_v00.y1[t]);
889 b.y2[t] = truncate256(b.y2[t] + (int)fbold_v00.y2[t]);
890 b.y3[t] = truncate256(b.y3[t] + (int)fbold_v00.y3[t]);
891 b.y4[t] = truncate256(b.y4[t] + (int)fbold_v00.y4[t]);
892 b.u[t] = truncate256(b.u[t] + (int)fbold_v00.u[t]);
893 b.v[t] = truncate256(b.v[t] + (int)fbold_v00.v[t]);
895 copyblock(s, s->current, &b, bx, by);
898 /* i block (mode=3) */
901 memcpy(&b, &b_i, sizeof(block_t));
902 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
903 swf_SetBits(tag,0,1); // COD
904 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
905 codehuffman(tag, cbpy, cbpybits);
908 encode8x8(tag, b.y1, has_dc, cbpybits&8);
909 encode8x8(tag, b.y2, has_dc, cbpybits&4);
910 encode8x8(tag, b.y3, has_dc, cbpybits&2);
911 encode8x8(tag, b.y4, has_dc, cbpybits&1);
914 encode8x8(tag, b.u, has_dc, cbpcbits&2);
915 encode8x8(tag, b.v, has_dc, cbpcbits&1);
917 /* -- reconstruction -- */
918 dequantize(&b, 1, *quant);
921 copyblock(s, s->current, &b, bx, by);
928 quantize(&fb, &b, has_dc, *quant);
929 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
931 if(!dquant && has_mvd && !has_mvd24 && !has_dc) mode = 0;
932 else if(dquant && has_mvd && !has_mvd24 && !has_dc) mode = 1;
933 else if(!dquant && has_mvd && has_mvd24 && !has_dc) mode = 2;
934 else if(!dquant && !has_mvd && !has_mvd24 && has_dc) mode = 3;
935 else if(dquant && !has_mvd && !has_mvd24 && has_dc) mode = 4;
938 swf_SetBits(tag,0,1); /* cod - 1 if we're not going to code this block*/
940 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
941 codehuffman(tag, cbpy, (mode==3 || mode==4)?cbpybits:cbpybits^15);
944 setQuant(tag, dquant);
949 codehuffman(tag, mvd, 32);
950 codehuffman(tag, mvd, 32);
956 encode8x8(tag, b.y1, has_dc, cbpybits&8);
957 encode8x8(tag, b.y2, has_dc, cbpybits&4);
958 encode8x8(tag, b.y3, has_dc, cbpybits&2);
959 encode8x8(tag, b.y4, has_dc, cbpybits&1);
962 encode8x8(tag, b.u, has_dc, cbpcbits&2);
963 encode8x8(tag, b.v, has_dc, cbpcbits&1);
967 #define TYPE_IFRAME 0
968 #define TYPE_PFRAME 1
970 static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
973 swf_SetU16(tag, frame);
974 swf_SetBits(tag, 1, 17); /* picture start code*/
975 swf_SetBits(tag, 0, 5); /* version=0, version 1 would optimize rle behaviour*/
976 swf_SetBits(tag, frame, 8); /* time reference */
978 /* write dimensions, taking advantage of some predefined sizes
979 if the opportunity presents itself */
980 i32 = width<<16|height;
983 case 352<<16|288: swf_SetBits(tag, 2, 3);break;
984 case 176<<16|144: swf_SetBits(tag, 3, 3);break;
985 case 128<<16|96: swf_SetBits(tag, 4, 3);break;
986 case 320<<16|240: swf_SetBits(tag, 5, 3);break;
987 case 160<<16|120: swf_SetBits(tag, 6, 3);break;
989 if(width>255 || height>255) {
990 swf_SetBits(tag, 1, 3);
991 swf_SetBits(tag, width, 16);
992 swf_SetBits(tag, height, 16);
994 swf_SetBits(tag, 0, 3);
995 swf_SetBits(tag, width, 8);
996 swf_SetBits(tag, height, 8);
1000 swf_SetBits(tag, type, 2); /* I-Frame or P-Frame */
1001 swf_SetBits(tag, 0, 1); /* No deblock filter */
1003 swf_SetBits(tag, quant, 5); /* quantizer (1-31), may be updated later on*/
1004 swf_SetBits(tag, 0, 1); /* No extra info */
1007 int stat_qdiff(double*b1, double*b2)
1012 double y1 = b1[x] - b2[x];
1015 return (int)(diff/64);
1018 int stat_absdiff(double*b1, double*b2)
1023 double y1 = b1[x] - b2[x];
1026 return (int)(diff/64);
1029 int stat_absfreq(double*b1, double*b2)
1033 double d1[64],d2[64];
1034 memcpy(&d1, b1, 64*sizeof(double));
1036 memcpy(&d2, b2, 64*sizeof(double));
1039 double y1 = d1[x] - d2[x];
1042 return (int)(diff/64);
1045 int stat_qfreq(double*b1, double*b2)
1049 double d1[64],d2[64];
1050 memcpy(&d1, b1, 64*sizeof(double));
1052 memcpy(&d2, b2, 64*sizeof(double));
1055 double y1 = d1[x] - d2[x];
1058 return (int)(diff/64);
1061 int stat_nonnull(double*b1, double*b2)
1065 double d1[64],d2[64];
1066 memcpy(&d1, b1, 64*sizeof(double));
1068 memcpy(&d2, b2, 64*sizeof(double));
1071 int y1 = (int)((d1[x] - d2[x])/9);
1078 void stat_filter(FILE*fi, double*d1, double*d2)
1087 double add1=0,add2=0;
1088 for(xx=x;xx<x+d;xx++)
1089 for(yy=y;yy<y+d;yy++)
1091 add1 += d1[yy*8+xx];
1092 add2 += d2[yy*8+xx];
1094 diff += fabs(add1-add2);
1096 fprintf(fi, "\t%d",(int)(diff/64));
1100 void qstat_filter(FILE*fi, double*d1, double*d2)
1109 double add1=0,add2=0;
1110 for(xx=x;xx<x+d;xx++)
1111 for(yy=y;yy<y+d;yy++)
1113 add1 += d1[yy*8+xx];
1114 add2 += d2[yy*8+xx];
1116 diff += (add1-add2)*(add1-add2);
1118 fprintf(fi, "\t%d",(int)(diff/64));
1122 void qqstat_filter(FILE*fi, double*d1, double*d2)
1131 double add1=0,add2=0;
1132 for(xx=x;xx<x+d;xx++)
1133 for(yy=y;yy<y+d;yy++)
1135 add1 += d1[yy*8+xx]*d1[yy*8+xx];
1136 add2 += d2[yy*8+xx]*d2[yy*8+xx];
1138 diff += fabs(add1-add2);
1140 fprintf(fi, "\t%d",(int)(diff/64));
1144 void stat(FILE*fi, int*vals, double*yold, double*ynew)
1147 int bits = coefbits8x8(vals, 0);
1148 fprintf(fi, "%d\t%d\t%d\t%d\t%d\t%d", bits,
1149 stat_nonnull(ynew, yold),
1150 stat_qdiff(ynew,yold),
1151 stat_absdiff(ynew,yold),
1152 stat_absfreq(ynew,yold),
1153 stat_qfreq(ynew,yold));
1154 stat_filter(fi, ynew, yold);
1155 qqstat_filter(fi, ynew, yold);
1159 void dostat(VIDEOSTREAM*s)
1164 FILE*fi = fopen("mvd.dat", "wb");
1165 fprintf(fi, "bits\tnonnull\tqdiff\tabsdiff\tabsfreq\tqfreq\tf1\tf2\tf4\tf8\tqf1\tqf2\tqf4\tqf8\n");
1166 for(by=0;by<s->bby;by++)
1167 for(bx=0;bx<s->bbx;bx++)
1169 for(by2=0;by2<s->bby;by2++)
1170 for(bx2=0;bx2<bx;bx2++)
1172 fblock_t fbnew,fbdiff,fbold;
1175 getregion(&fbnew, s->current, bx*16, by*16, s->linex);
1176 memcpy(&fbdiff, &fbnew, sizeof(fblock_t));
1177 getregion(&fbold, s->current, bx2*16, by2*16, s->linex);
1178 yuvdiff(&fbdiff, &fbold);
1180 quantize(&fbdiff, &b, 0, quant);
1182 stat(fi, b.y1, fbnew.y1, fbold.y1);
1183 stat(fi, b.y2, fbnew.y2, fbold.y2);
1184 stat(fi, b.y3, fbnew.y3, fbold.y3);
1185 stat(fi, b.y4, fbnew.y4, fbold.y4);
1186 stat(fi, b.u, fbnew.u, fbold.u);
1187 stat(fi, b.v, fbnew.v, fbold.v);
1195 printf("%d\n", num);fflush(stdout);
1201 void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1205 if(quant<1) quant=1;
1206 if(quant>31) quant=31;
1208 writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_IFRAME);
1210 rgb2yuv(s->current, pic, s->linex, s->olinex, s->width, s->height);
1214 for(by=0;by<s->bby;by++)
1216 for(bx=0;bx<s->bbx;bx++)
1218 encode_blockI(tag, s, bx, by, &quant);
1222 memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1225 void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1229 if(quant<1) quant=1;
1230 if(quant>31) quant=31;
1232 writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1234 rgb2yuv(s->current, pic, s->linex, s->olinex, s->width, s->height);
1235 memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1236 memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1238 for(by=0;by<s->bby;by++)
1240 for(bx=0;bx<s->bbx;bx++)
1242 encode_blockP(tag, s, bx, by, &quant);
1246 memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1250 FILE*fi = fopen("test.ppm", "wb");
1251 yuv2rgb(pic, s->current, s->linex, s->width, s->height);
1252 fprintf(fi, "P6\n%d %d\n255\n", s->width, s->height);
1253 for(t=0;t<s->width*s->height;t++)
1255 fwrite(&pic[t].r, 1, 1, fi);
1256 fwrite(&pic[t].g, 1, 1, fi);
1257 fwrite(&pic[t].b, 1, 1, fi);
1266 int main(int argn, char*argv[])
1272 RGBA* pic, *pic2, rgb;
1279 char* fname = "/home/kramm/pics/peppers.png";
1283 memset(&stream, 0, sizeof(stream));
1285 getPNG(fname, &width, &height, &data);
1286 pic = (RGBA*)malloc(width*height*sizeof(RGBA));
1287 pic2 = (RGBA*)malloc(width*height*sizeof(RGBA));
1288 memcpy(pic, data, width*height*sizeof(RGBA));
1291 printf("Compressing %s, size %dx%d\n", fname, width, height);
1293 memset(&swf,0,sizeof(SWF));
1294 memset(&obj,0,sizeof(obj));
1296 swf.fileVersion = 6;
1297 swf.frameRate = framerate*256;
1298 swf.movieSize.xmax = 20*width;
1299 swf.movieSize.ymax = 20*height;
1301 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1303 rgb.r = 0x00;rgb.g = 0x00;rgb.b = 0x00;
1304 swf_SetRGB(tag,&rgb);
1306 tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
1307 swf_SetU16(tag, 33);
1308 swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
1309 stream.do_motion = 1;
1311 for(t=0;t<frames;t++)
1315 for(y=0,yy=0;y<height;y++,yy+=d) {
1316 RGBA*line = &pic[((int)yy)*width];
1317 for(x=0,xx=0;x<width;x++,xx+=d) {
1318 pic2[y*width+x] = line[((int)xx)];
1321 printf("frame:%d\n", t);fflush(stdout);
1323 tag = swf_InsertTag(tag, ST_VIDEOFRAME);
1324 swf_SetU16(tag, 33);
1326 swf_SetVideoStreamIFrame(tag, &stream, pic2, 9);
1328 swf_SetVideoStreamPFrame(tag, &stream, pic2, 9);
1330 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
1331 swf_GetPlaceObject(0, &obj);
1340 swf_SetPlaceObject(tag,&obj);
1342 tag = swf_InsertTag(tag, ST_SHOWFRAME);
1346 swf_VideoStreamClear(&stream);
1348 tag = swf_InsertTag(tag, ST_END);
1350 fi = open("video3.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
1351 if(swf_WriteSWC(fi,&swf)<0) {
1352 fprintf(stderr,"WriteSWF() failed.\n");