2 Routines for handling h.263 video tags
4 Part of the swftools package.
6 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
26 #include "../rfxswf.h"
27 #include "h263tables.c"
32 void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width, U16 height)
34 swf_SetU16(tag, frames);
35 swf_SetU16(tag, width);
36 swf_SetU16(tag, height);
37 //swf_SetU8(tag, 1); /* smoothing on */
38 swf_SetU8(tag, 0); /* smoothing off */
39 swf_SetU8(tag, 2); /* codec = h.263 sorenson spark */
43 memset(stream, 0, sizeof(VIDEOSTREAM));
44 stream->olinex = width;
46 height+=15;height&=~15;
47 stream->linex = width;
48 stream->width = width;
49 stream->height = height;
50 stream->bbx = width/16;
51 stream->bby = height/16;
52 stream->current = (YUV*)malloc(width*height*sizeof(YUV));
53 stream->oldpic = (YUV*)malloc(width*height*sizeof(YUV));
54 stream->mvdx = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
55 stream->mvdy = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
56 stream->do_motion = 0;
58 memset(stream->oldpic, 0, width*height*sizeof(YUV));
59 memset(stream->current, 0, width*height*sizeof(YUV));
61 void swf_VideoStreamClear(VIDEOSTREAM*stream)
63 free(stream->oldpic);stream->oldpic = 0;
64 free(stream->current);stream->current = 0;
65 free(stream->mvdx);stream->mvdx=0;
66 free(stream->mvdy);stream->mvdy=0;
69 typedef struct _block_t
79 static int zigzagtable[64] = {
80 0, 1, 5, 6, 14, 15, 27, 28,
81 2, 4, 7, 13, 16, 26, 29, 42,
82 3, 8, 12, 17, 25, 30, 41, 43,
83 9, 11, 18, 24, 31, 40, 44, 53,
84 10, 19, 23, 32, 39, 45, 52, 54,
85 20, 22, 33, 38, 46, 51, 55, 60,
86 21, 34, 37, 47, 50, 56, 59, 61,
87 35, 36, 48, 49, 57, 58, 62, 63};
89 static void zigzag(int*src)
94 tmp[zigzagtable[t]] = src[t];
96 memcpy(src, tmp, sizeof(int)*64);
99 #define PI 3.14159265358979
100 #define SQRT2 1.414214
101 #define RSQRT2 (1.0/1.414214)
103 static double table[8][8] =
105 {0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548},
106 {0.980785280403230,0.831469612302545,0.555570233019602,0.195090322016128,-0.195090322016128,-0.555570233019602,-0.831469612302545,-0.980785280403230},
107 {0.923879532511287,0.382683432365090,-0.382683432365090,-0.923879532511287,-0.923879532511287,-0.382683432365090,0.382683432365090,0.923879532511287},
108 {0.831469612302545,-0.195090322016128,-0.980785280403230,-0.555570233019602,0.555570233019602,0.980785280403230,0.195090322016129,-0.831469612302545},
109 {0.707106781186548,-0.707106781186547,-0.707106781186548,0.707106781186547,0.707106781186548,-0.707106781186547,-0.707106781186547,0.707106781186547},
110 {0.555570233019602,-0.980785280403230,0.195090322016128,0.831469612302545,-0.831469612302545,-0.195090322016128,0.980785280403231,-0.555570233019602},
111 {0.382683432365090,-0.923879532511287,0.923879532511287,-0.382683432365090,-0.382683432365091,0.923879532511287,-0.923879532511286,0.382683432365090},
112 {0.195090322016128,-0.555570233019602,0.831469612302545,-0.980785280403231,0.980785280403230,-0.831469612302545,0.555570233019602,-0.195090322016129}
115 static void dct(int*src)
126 c+=table[u][x]*src[v*8+x];
136 c+=table[v][y]*tmp[y*8+u];
138 src[v*8+u] = (int)(c*0.25+0.5);
142 static void idct(int*src)
152 c+=table[u][x]*src[y*8+u];
162 c+=table[v][y]*tmp[v*8+x];
164 src[y*8+x] = (int)(c*0.25+0.5);
168 static double c[8] = {1.0,
169 0.980785280403230, // cos(Pi*1/16), sin(Pi*7/16)
170 0.923879532511287, // cos(Pi*2/16), sin(Pi*6/16)
171 0.831469612302545, // cos(Pi*3/16), sin(Pi*5/16)
172 0.707106781186548, // cos(Pi*4/16), sin(Pi*4/16), 1/sqrt(2)
173 0.555570233019602, // cos(Pi*5/16), sin(Pi*3/16)
174 0.382683432365090, // cos(Pi*6/16), sin(Pi*2/16)
175 0.195090322016128 // cos(Pi*7/16), sin(Pi*1/16)
181 static void preparequant(int quant)
185 cc[0] = c[0]/(quant*2*4);
186 cc[1] = c[1]/(quant*2*4);
187 cc[2] = c[2]/(quant*2*4);
188 cc[3] = c[3]/(quant*2*4);
189 cc[4] = c[4]/(quant*2*4);
190 cc[5] = c[5]/(quant*2*4);
191 cc[6] = c[6]/(quant*2*4);
192 cc[7] = c[7]/(quant*2*4);
196 inline static void innerdct(double*a,double*b, double*c)
203 //{ 1, 3, 5, 7, -7, -5, -3, -1},
204 //{ 3, -7, -1, -5, 5, 1, 7, -3},
205 //{ 5, -1, 7, 3, -3, -7, 1, -5},
206 //{ 7, -5, 3, -1, 1, -3, 5, -7}
207 double b0,b1,b2,b3,b4,b5;
217 b[2*8] = (b2-b5)*c[2] + (b3-b4)*c[6];
218 b[6*8] = (b2-b5)*c[6] + (b4-b3)*c[2];
225 b[1*8] = b0*c[1] + b1*c[3] + b2*c[5] + b3*c[7];
226 b[3*8] = b0*c[3] - b1*c[7] - b2*c[1] - b3*c[5];
227 b[5*8] = b0*c[5] - b1*c[1] + b2*c[7] + b3*c[3];
228 b[7*8] = b0*c[7] - b1*c[5] + b2*c[3] - b3*c[1];
231 static void dct2(int*src, int*dest)
233 double tmp[64], tmp2[64];
242 double* a=&tmp2[v*8];
253 dest[zigzagtable[t]] = (int)(tmp2[t]);
258 static inline int truncate256(int a)
260 if(a>255) return 255;
265 static void getregion(block_t* bb, YUV*pic, int posx, int posy, int linex)
269 int y1=0, y2=0, y3=0, y4=0;
274 p1 = &pic[posy*linex+posx];
278 bb->u[u++] = (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
279 bb->v[v++] = (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
280 bb->y1[y1++] = p1[x].y;
281 bb->y2[y2++] = p1[x+8].y;
282 bb->y3[y3++] = p1[linex*8+x].y;
283 bb->y4[y4++] = p1[linex*8+x+8].y;
290 static void getmvdregion(block_t* bb, YUV*pic, int posx, int posy, int mvdx, int mvdy, int linex)
298 posx = posx*16 + ((mvdx&~1)/2);
299 posy = posy*16 + ((mvdy&~1)/2);
300 p1 = &pic[posy*linex+posx];
301 p2 = &pic[(posy&~1)*linex+(posx&~1)];
302 uvhp = ((mvdx&1)|((mvdx>>1)&1))|((mvdy&2)|((mvdy&1)<<1));
303 yhp = ((mvdy&1)<<1|(mvdx&1));
306 if(yhp==0 || yhp==2) {
309 bb->y1[yy] = p1[x].y;
310 bb->y2[yy] = p1[x+8].y;
311 bb->y3[yy] = p1[linex*8+x].y;
312 bb->y4[yy] = p1[linex*8+x+8].y;
320 bb->y1[yy] += p1[x].y; bb->y1[yy] /= 2;
321 bb->y2[yy] += p1[x+8].y; bb->y2[yy] /= 2;
322 bb->y3[yy] += p1[linex*8+x].y; bb->y3[yy] /= 2;
323 bb->y4[yy] += p1[linex*8+x+8].y; bb->y4[yy] /= 2;
328 } else if(yhp==1 || yhp==3) {
331 bb->y1[yy] = (p1[x].y + p1[x+1].y);
332 bb->y2[yy] = (p1[x+8].y + p1[x+8+1].y);
333 bb->y3[yy] = (p1[linex*8+x].y + p1[linex*8+x+1].y);
334 bb->y4[yy] = (p1[linex*8+x+8].y + p1[linex*8+x+8+1].y);
341 bb->y1[yy] += (p1[x].y + p1[x+1].y); bb->y1[yy]/=4;
342 bb->y2[yy] += (p1[x+8].y + p1[x+8+1].y); bb->y2[yy]/=4;
343 bb->y3[yy] += (p1[linex*8+x].y + p1[linex*8+x+1].y); bb->y3[yy]/=4;
344 bb->y4[yy] += (p1[linex*8+x+8].y + p1[linex*8+x+8+1].y); bb->y4[yy]/=4;
349 bb->y1[yy]/=2; bb->y2[yy]/=2; bb->y3[yy]/=2; bb->y4[yy]/=2;
357 if(uvhp==0 || uvhp==2) {
360 bb->u[uv] = (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
361 bb->v[uv] = (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
368 bb->u[uv] += (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
369 bb->v[uv] += (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
376 } else /* uvhp==1 || uvhp==3 */ {
379 bb->u[uv] = ((p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4+
380 (p2[x*2+2].u + p2[x*2+1+2].u + p2[linex+x*2+2].u + p2[linex+x*2+1+2].u)/4);
381 bb->v[uv] = ((p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4+
382 (p2[x*2+2].v + p2[x*2+1+2].v + p2[linex+x*2+2].v + p2[linex+x*2+1+2].v)/4);
389 bb->u[uv] += ((p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4+
390 (p2[x*2+2].u + p2[x*2+1+2].u + p2[linex+x*2+2].u + p2[linex+x*2+1+2].u)/4);
391 bb->v[uv] += ((p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4+
392 (p2[x*2+2].v + p2[x*2+1+2].v + p2[linex+x*2+2].v + p2[linex+x*2+1+2].v)/4);
408 static void rgb2yuv(YUV*dest, RGBA*src, int dlinex, int slinex, int width, int height)
411 for(y=0;y<height;y++) {
412 for(x=0;x<width;x++) {
414 r = src[y*slinex+x].r;
415 g = src[y*slinex+x].g;
416 b = src[y*slinex+x].b;
417 /*dest[y*dlinex+x].y = (r*0.299 + g*0.587 + b*0.114);
418 dest[y*dlinex+x].u = (r*-0.169 + g*-0.332 + b*0.500 + 128.0);
419 dest[y*dlinex+x].v = (r*0.500 + g*-0.419 + b*-0.0813 + 128.0);*/
421 //dest[y*dlinex+x].y = 128;//(r*((int)( 0.299*256)) + g*((int)( 0.587*256)) + b*((int)( 0.114 *256)))>>8;
422 dest[y*dlinex+x].y = (r*((int)( 0.299*256)) + g*((int)( 0.587*256)) + b*((int)( 0.114 *256)))>>8;
423 dest[y*dlinex+x].u = (r*((int)(-0.169*256)) + g*((int)(-0.332*256)) + b*((int)( 0.500 *256))+ 128*256)>>8;
424 dest[y*dlinex+x].v = (r*((int)( 0.500*256)) + g*((int)(-0.419*256)) + b*((int)(-0.0813*256))+ 128*256)>>8;
428 static void copyregion(VIDEOSTREAM*s, YUV*dest, YUV*src, int bx, int by)
430 YUV*p1 = &src[by*s->linex*16+bx*16];
431 YUV*p2 = &dest[by*s->linex*16+bx*16];
434 memcpy(p1, p2, 16*sizeof(YUV));
435 p1+=s->linex;p2+=s->linex;
439 static void yuv2rgb(RGBA*dest, YUV*src, int linex, int width, int height)
442 for(y=0;y<height;y++) {
443 for(x=0;x<width;x++) {
445 u = src[y*linex+x].u;
446 v = src[y*linex+x].v;
447 yy = src[y*linex+x].y;
448 dest[y*linex+x].r = truncate256(yy + ((360*(v-128))>>8));
449 dest[y*linex+x].g = truncate256(yy - ((88*(u-128)+183*(v-128))>>8));
450 dest[y*linex+x].b = truncate256(yy + ((455 * (u-128))>>8));
454 static void copyblock(VIDEOSTREAM*s, YUV*dest, block_t*b, int bx, int by)
456 YUV*p1 = &dest[(by*16)*s->linex+bx*16];
457 YUV*p2 = &dest[(by*16+8)*s->linex+bx*16];
462 p1[x+0].u = b->u[(y/2)*8+(x/2)];
463 p1[x+0].v = b->v[(y/2)*8+(x/2)];
464 p1[x+0].y = b->y1[y*8+x];
465 p1[x+8].u = b->u[(y/2)*8+(x/2)+4];
466 p1[x+8].v = b->v[(y/2)*8+(x/2)+4];
467 p1[x+8].y = b->y2[y*8+x];
468 p2[x+0].u = b->u[(y/2+4)*8+(x/2)];
469 p2[x+0].v = b->v[(y/2+4)*8+(x/2)];
470 p2[x+0].y = b->y3[y*8+x];
471 p2[x+8].u = b->u[(y/2+4)*8+(x/2)+4];
472 p2[x+8].v = b->v[(y/2+4)*8+(x/2)+4];
473 p2[x+8].y = b->y4[y*8+x];
480 static int compareregions(VIDEOSTREAM*s, int bx, int by)
482 int linex = s->width;
483 YUV*p1 = &s->current[by*linex*16+bx*16];
484 YUV*p2 = &s->oldpic[by*linex*16+bx*16];
494 diff += y*y+(u*u+v*v)/4;
502 static inline int valtodc(int val)
510 /* TODO: what to do for zero values? skip the block? */
519 static int dctoval(int dc)
532 static int codehuffman(TAG*tag, struct huffcode*table, int index)
534 /* TODO: !optimize! */
536 while(table[index].code[i]) {
537 if(table[index].code[i]=='0')
538 swf_SetBits(tag, 0, 1);
540 swf_SetBits(tag, 1, 1);
546 static void quantize8x8(int*src, int*dest, int has_dc, int quant)
549 double q = 1.0/(quant*2);
551 dest[0] = valtodc((int)src[0]); /*DC*/
556 //dest[t] = (int)src[t];
557 /* exact: if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;} */
558 //if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;}
559 //dest[t] = dest[t]/(quant*2);
560 dest[t] = (int)(src[t]*q);
561 /* TODO: warn if this happens- the video will be buggy */
562 if(dest[t]>127) dest[t]=127;
563 if(dest[t]<-127) dest[t]=-127;
567 static void dequantize8x8(int*b, int has_dc, int quant)
571 b[0] = dctoval(b[0]); //DC
574 for(t=pos;t<64;t++) {
583 b[t] = quant*(2*b[t]+1); //-7,8,24,40
585 b[t] = quant*(2*b[t]+1)-1; //-8,7,23,39
592 /* paragraph 6.2.2, "clipping of reconstruction levels": */
593 if(b[t]>2047) b[t]=2047;
594 if(b[t]<-2048) b[t]=-2048;
598 static int hascoef(int*b, int has_dc)
604 for(t=pos;t<64;t++) {
611 static int coefbits8x8(int*bb, int has_dc)
622 for(last=63;last>=pos;last--) {
629 int run=0, level=0, islast=0,t;
630 while(!bb[pos] && pos<last) {
637 if(level<0) level=-level;
639 for(t=0;t<RLE_ESCAPE;t++) {
640 if(rle_params[t].run == run &&
641 rle_params[t].level == level &&
642 rle_params[t].last == islast) {
643 bits += rle[t].len + 1;
648 bits += rle[RLE_ESCAPE].len + 1 + 6 + 8;
657 static void encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
664 swf_SetBits(tag, bb[0], 8);
670 /* determine last non-null coefficient */
671 for(last=63;last>=pos;last--) {
672 /* TODO: we could leave out small coefficients
673 after a certain point (32?) */
677 /* blocks without coefficients should not be included
678 in the cbpy/cbpc patterns: */
687 while(!bb[pos] && pos<last) {
699 for(t=0;t<RLE_ESCAPE;t++) {
700 /* TODO: lookup table */
701 if(rle_params[t].run == run &&
702 rle_params[t].level == level &&
703 rle_params[t].last == islast) {
704 codehuffman(tag, rle, t);
705 swf_SetBits(tag, sign, 1);
710 codehuffman(tag, rle, RLE_ESCAPE);
717 swf_SetBits(tag, islast, 1);
718 swf_SetBits(tag, run, 6);
719 swf_SetBits(tag, level, 8); //FIXME: fixme??
729 static void quantize(block_t*fb, block_t*b, int has_dc, int quant)
731 quantize8x8(fb->y1, b->y1, has_dc, quant);
732 quantize8x8(fb->y2, b->y2, has_dc, quant);
733 quantize8x8(fb->y3, b->y3, has_dc, quant);
734 quantize8x8(fb->y4, b->y4, has_dc, quant);
735 quantize8x8(fb->u, b->u, has_dc, quant);
736 quantize8x8(fb->v, b->v, has_dc, quant);
739 static void dodct(block_t*fb)
741 dct(fb->y1); dct(fb->y2); dct(fb->y3); dct(fb->y4);
742 dct(fb->u); dct(fb->v);
750 static void dodctandquant(block_t*fb, block_t*b, int has_dc, int quant)
755 quantize(fb,b,has_dc,quant);
759 dct2(fb->y1,b->y1); dct2(fb->y2,b->y2); dct2(fb->y3,b->y3); dct2(fb->y4,b->y4);
760 dct2(fb->u,b->u); dct2(fb->v,b->v);
763 static void doidct(block_t*b)
768 fb.y1[t] = b->y1[zigzagtable[t]];
769 fb.y2[t] = b->y2[zigzagtable[t]];
770 fb.y3[t] = b->y3[zigzagtable[t]];
771 fb.y4[t] = b->y4[zigzagtable[t]];
772 fb.u[t] = b->u[zigzagtable[t]];
773 fb.v[t] = b->v[zigzagtable[t]];
775 idct(fb.y1); idct(fb.y2); idct(fb.y3); idct(fb.y4);
776 idct(fb.u); idct(fb.v);
786 static void truncateblock(block_t*b)
790 b->y1[t] = truncate256(b->y1[t]);
791 b->y2[t] = truncate256(b->y2[t]);
792 b->y3[t] = truncate256(b->y3[t]);
793 b->y4[t] = truncate256(b->y4[t]);
794 b->u[t] = truncate256(b->u[t]);
795 b->v[t] = truncate256(b->v[t]);
799 static void dequantize(block_t*b, int has_dc, int quant)
801 dequantize8x8(b->y1, has_dc, quant);
802 dequantize8x8(b->y2, has_dc, quant);
803 dequantize8x8(b->y3, has_dc, quant);
804 dequantize8x8(b->y4, has_dc, quant);
805 dequantize8x8(b->u, has_dc, quant);
806 dequantize8x8(b->v, has_dc, quant);
809 static void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
814 *cbpybits|=hascoef(b->y1, has_dc)*8;
815 *cbpybits|=hascoef(b->y2, has_dc)*4;
816 *cbpybits|=hascoef(b->y3, has_dc)*2;
817 *cbpybits|=hascoef(b->y4, has_dc)*1;
819 *cbpcbits|=hascoef(b->u, has_dc)*2;
820 *cbpcbits|=hascoef(b->v, has_dc)*1;
823 static void setQuant(TAG*tag, int dquant)
830 swf_SetBits(tag, 0x0, 2);
831 } else if(dquant == -2) {
832 swf_SetBits(tag, 0x1, 2);
833 } else if(dquant == +1) {
834 swf_SetBits(tag, 0x2, 2);
835 } else if(dquant == +2) {
836 swf_SetBits(tag, 0x3, 2);
838 assert(0*strlen("invalid dquant"));
842 static void change_quant(int quant, int*dquant)
848 static void encode_blockI(TAG*tag, VIDEOSTREAM*s, int bx, int by, int*quant)
853 int cbpcbits = 0, cbpybits=0;
855 getregion(&fb, s->current, bx, by, s->width);
857 change_quant(*quant, &dquant);
860 dodctandquant(&fb, &b, 1, *quant);
861 //quantize(&fb, &b, 1, *quant);
863 //decode_blockI(s, &b, bx, by);
865 getblockpatterns(&b, &cbpybits, &cbpcbits, 1);
868 codehuffman(tag, mcbpc_intra, 4+cbpcbits);
870 codehuffman(tag, mcbpc_intra, 0+cbpcbits);
873 codehuffman(tag, cbpy, cbpybits);
876 setQuant(tag, dquant);
880 encode8x8(tag, b.y1, 1, cbpybits&8);
881 encode8x8(tag, b.y2, 1, cbpybits&4);
882 encode8x8(tag, b.y3, 1, cbpybits&2);
883 encode8x8(tag, b.y4, 1, cbpybits&1);
886 encode8x8(tag, b.u, 1, cbpcbits&2);
887 encode8x8(tag, b.v, 1, cbpcbits&1);
890 dequantize(&b, 1, *quant);
893 copyblock(s, s->current, &b, bx, by);
896 static void yuvdiff(block_t*a, block_t*b)
900 a->y1[t] = (a->y1[t] - b->y1[t]);
901 a->y2[t] = (a->y2[t] - b->y2[t]);
902 a->y3[t] = (a->y3[t] - b->y3[t]);
903 a->y4[t] = (a->y4[t] - b->y4[t]);
904 a->u[t] = (a->u[t] - b->u[t]);
905 a->v[t] = (a->v[t] - b->v[t]);
909 static void predictmvd(VIDEOSTREAM*s, int bx, int by, int*px, int*py)
912 int x1,y1,x2,y2,x3,y3;
914 if(bx) {x1=s->mvdx[by*s->bbx+bx-1];
915 y1=s->mvdy[by*s->bbx+bx-1];
918 if(by) {x2=s->mvdx[(by-1)*s->bbx+bx];
919 y2=s->mvdy[(by-1)*s->bbx+bx];
921 x3=s->mvdx[(by-1)*s->bbx+bx+1];
922 y3=s->mvdy[(by-1)*s->bbx+bx+1];
927 else {x2=x3=x1;y2=y3=y1;}
929 if((x1 <= x2 && x2 <= x3) ||
930 (x3 <= x2 && x2 <= x1)) {
932 } else if((x2 <= x1 && x1 <= x3) ||
933 (x3 <= x1 && x1 <= x2)) {
935 } else if((x1 <= x3 && x3 <= x2) ||
936 (x2 <= x3 && x3 <= x1)) {
940 if((y1 <= y2 && y2 <= y3) ||
941 (y3 <= y2 && y2 <= y1)) {
943 } else if((y2 <= y1 && y1 <= y3) ||
944 (y3 <= y1 && y1 <= y2)) {
946 } else if((y1 <= y3 && y3 <= y2) ||
947 (y2 <= y3 && y3 <= y1)) {
953 assert((x4>=-32 && x4<=31) && (y4>=-32 && y4<=31));
956 static inline int mvd2index(int px, int py, int x, int y, int xy)
958 assert((x>=-32 && x<=31) && (y>=-32 && y<=31));
959 //assert((x&1)==0 && (y&1)==0);//for now
960 //assert((x&2)==0 && (y&2)==0);//for now(2)
975 assert(x>=0 && x<64);
979 static int encode_blockP(TAG*tag, VIDEOSTREAM*s, int bx, int by, int*quant)
988 int cbpcbits = 0, cbpybits=0;
998 int bits_v00 = 65535;
1002 diff = compareregions(s, bx, by);
1005 /* TODO: measure the error an I-block encoding would do, and base the decision
1007 swf_SetBits(tag, 1,1); /* cod=1, block skipped */
1008 /* copy the region from the last frame so that we have a complete reconstruction */
1009 copyregion(s, s->current, s->oldpic, bx, by);
1013 predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
1014 getregion(&fb, s->current, bx, by, s->width);
1016 { /* consider I-block */
1019 memcpy(&fb_i, &fb, sizeof(block_t));
1020 dodctandquant(&fb_i, &b_i, 1, *quant);
1021 //quantize(&fb_i, &b_i, 1, *quant);
1022 getblockpatterns(&b_i, &y, &c, 1);
1024 bits_i += mcbpc_inter[3*4+c].len;
1025 bits_i += cbpy[y].len;
1026 bits_i += coefbits8x8(b_i.y1, 1);
1027 bits_i += coefbits8x8(b_i.y2, 1);
1028 bits_i += coefbits8x8(b_i.y3, 1);
1029 bits_i += coefbits8x8(b_i.y4, 1);
1030 bits_i += coefbits8x8(b_i.u, 1);
1031 bits_i += coefbits8x8(b_i.v, 1);
1034 { /* consider mvd(x,y)-block */
1043 int bestx=0,besty=0,bestbits=65536;
1044 int startx=-2,endx=2;
1045 int starty=-2,endy=2;
1049 if(bx==s->bbx-1) endx=0;
1050 if(by==s->bby-1) endy=0;
1052 for(hx=startx;hx<=endx;hx+=1)
1053 for(hy=starty;hy<=endy;hy+=1)
1058 memcpy(&fbdiff, &fb, sizeof(block_t));
1059 getmvdregion(&fbold, s->oldpic, bx, by, hx, hy, s->linex);
1060 yuvdiff(&fbdiff, &fbold);
1061 dodctandquant(&fbdiff, &b, 0, *quant);
1062 //quantize(&fbdiff, &b, 0, *quant);
1063 bits += coefbits8x8(b.y1, 0);
1064 bits += coefbits8x8(b.y2, 0);
1065 bits += coefbits8x8(b.y3, 0);
1066 bits += coefbits8x8(b.y4, 0);
1067 bits += coefbits8x8(b.u, 0);
1068 bits += coefbits8x8(b.v, 0);
1079 memcpy(&fbdiff, &fb, sizeof(block_t));
1080 getmvdregion(&fbold_v00, s->oldpic, bx, by, x_v00, y_v00, s->linex);
1081 yuvdiff(&fbdiff, &fbold_v00);
1082 dodctandquant(&fbdiff, &b_v00, 0, *quant);
1083 //quantize(&fbdiff, &b_v00, 0, *quant);
1084 getblockpatterns(&b_v00, &y, &c, 0);
1087 bits_v00 += mcbpc_inter[0*4+c].len;
1088 bits_v00 += cbpy[y^15].len;
1089 bits_v00 += mvd[mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 0)].len; // (0,0)
1090 bits_v00 += mvd[mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 1)].len;
1091 bits_v00 += coefbits8x8(b_v00.y1, 0);
1092 bits_v00 += coefbits8x8(b_v00.y2, 0);
1093 bits_v00 += coefbits8x8(b_v00.y3, 0);
1094 bits_v00 += coefbits8x8(b_v00.y4, 0);
1095 bits_v00 += coefbits8x8(b_v00.u, 0);
1096 bits_v00 += coefbits8x8(b_v00.v, 0);
1099 if(bits_i > bits_v00)
1101 /* mvd (0,0) block (mode=0) */
1103 mode = 0; // mvd w/o mvd24
1105 memcpy(&b, &b_v00, sizeof(block_t));
1107 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
1108 swf_SetBits(tag,0,1); // COD
1109 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1110 codehuffman(tag, cbpy, cbpybits^15);
1113 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 0));
1114 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, x_v00, y_v00, 1));
1115 s->mvdx[by*s->bbx+bx] = x_v00;
1116 s->mvdy[by*s->bbx+bx] = y_v00;
1119 encode8x8(tag, b.y1, has_dc, cbpybits&8);
1120 encode8x8(tag, b.y2, has_dc, cbpybits&4);
1121 encode8x8(tag, b.y3, has_dc, cbpybits&2);
1122 encode8x8(tag, b.y4, has_dc, cbpybits&1);
1125 encode8x8(tag, b.u, has_dc, cbpcbits&2);
1126 encode8x8(tag, b.v, has_dc, cbpcbits&1);
1128 /* -- reconstruction -- */
1129 dequantize(&b, 0, *quant);
1132 b.y1[t] = truncate256(b.y1[t] + (int)fbold_v00.y1[t]);
1133 b.y2[t] = truncate256(b.y2[t] + (int)fbold_v00.y2[t]);
1134 b.y3[t] = truncate256(b.y3[t] + (int)fbold_v00.y3[t]);
1135 b.y4[t] = truncate256(b.y4[t] + (int)fbold_v00.y4[t]);
1136 b.u[t] = truncate256(b.u[t] + (int)fbold_v00.u[t]);
1137 b.v[t] = truncate256(b.v[t] + (int)fbold_v00.v[t]);
1139 copyblock(s, s->current, &b, bx, by);
1142 /* i block (mode=3) */
1145 memcpy(&b, &b_i, sizeof(block_t));
1146 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
1147 swf_SetBits(tag,0,1); // COD
1148 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1149 codehuffman(tag, cbpy, cbpybits);
1152 encode8x8(tag, b.y1, has_dc, cbpybits&8);
1153 encode8x8(tag, b.y2, has_dc, cbpybits&4);
1154 encode8x8(tag, b.y3, has_dc, cbpybits&2);
1155 encode8x8(tag, b.y4, has_dc, cbpybits&1);
1158 encode8x8(tag, b.u, has_dc, cbpcbits&2);
1159 encode8x8(tag, b.v, has_dc, cbpcbits&1);
1161 /* -- reconstruction -- */
1162 dequantize(&b, 1, *quant);
1165 copyblock(s, s->current, &b, bx, by);
1172 quantize(&fb, &b, has_dc, *quant);
1173 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
1175 if(!dquant && has_mvd && !has_mvd24 && !has_dc) mode = 0;
1176 else if(dquant && has_mvd && !has_mvd24 && !has_dc) mode = 1;
1177 else if(!dquant && has_mvd && has_mvd24 && !has_dc) mode = 2;
1178 else if(!dquant && !has_mvd && !has_mvd24 && has_dc) mode = 3;
1179 else if(dquant && !has_mvd && !has_mvd24 && has_dc) mode = 4;
1182 swf_SetBits(tag,0,1); /* cod - 1 if we're not going to code this block*/
1184 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1185 codehuffman(tag, cbpy, (mode==3 || mode==4)?cbpybits:cbpybits^15);
1188 setQuant(tag, dquant);
1193 codehuffman(tag, mvd, 32);
1194 codehuffman(tag, mvd, 32);
1200 encode8x8(tag, b.y1, has_dc, cbpybits&8);
1201 encode8x8(tag, b.y2, has_dc, cbpybits&4);
1202 encode8x8(tag, b.y3, has_dc, cbpybits&2);
1203 encode8x8(tag, b.y4, has_dc, cbpybits&1);
1206 encode8x8(tag, b.u, has_dc, cbpcbits&2);
1207 encode8x8(tag, b.v, has_dc, cbpcbits&1);
1211 static int bmid = 0;
1212 void setdbgpic(TAG*tag, RGBA*pic, int width, int height)
1217 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
1218 swf_SetU16(tag, 133);
1220 tag = swf_InsertTag(tag, ST_DEFINEBITSLOSSLESS);
1221 swf_SetU16(tag, 1000+bmid);
1222 swf_SetLosslessBits(tag, width, height, (void*)pic, BMF_32BIT);
1224 tag = swf_InsertTag(tag, ST_DEFINESHAPE);
1225 swf_SetU16(tag, 2000+bmid);
1226 swf_ShapeSetBitmapRect(tag, 1000+bmid, width, height);
1228 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1229 swf_GetMatrix(0,&m);
1231 swf_ObjectPlace(tag, 2000+bmid, 133, &m, 0, 0);
1236 #define TYPE_IFRAME 0
1237 #define TYPE_PFRAME 1
1239 static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
1242 swf_SetU16(tag, frame);
1243 swf_SetBits(tag, 1, 17); /* picture start code*/
1244 swf_SetBits(tag, 0, 5); /* version=0, version 1 would optimize rle behaviour*/
1245 swf_SetBits(tag, frame, 8); /* time reference */
1247 /* write dimensions, taking advantage of some predefined sizes
1248 if the opportunity presents itself */
1249 i32 = width<<16|height;
1252 case 352<<16|288: swf_SetBits(tag, 2, 3);break;
1253 case 176<<16|144: swf_SetBits(tag, 3, 3);break;
1254 case 128<<16|96: swf_SetBits(tag, 4, 3);break;
1255 case 320<<16|240: swf_SetBits(tag, 5, 3);break;
1256 case 160<<16|120: swf_SetBits(tag, 6, 3);break;
1258 if(width>255 || height>255) {
1259 swf_SetBits(tag, 1, 3);
1260 swf_SetBits(tag, width, 16);
1261 swf_SetBits(tag, height, 16);
1263 swf_SetBits(tag, 0, 3);
1264 swf_SetBits(tag, width, 8);
1265 swf_SetBits(tag, height, 8);
1269 swf_SetBits(tag, type, 2); /* I-Frame or P-Frame */
1270 swf_SetBits(tag, 0, 1); /* No deblock filter */
1272 swf_SetBits(tag, quant, 5); /* quantizer (1-31), may be updated later on*/
1273 swf_SetBits(tag, 0, 1); /* No extra info */
1276 void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1280 if(quant<1) quant=1;
1281 if(quant>31) quant=31;
1283 writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_IFRAME);
1285 rgb2yuv(s->current, pic, s->linex, s->olinex, s->width, s->height);
1289 for(by=0;by<s->bby;by++)
1291 for(bx=0;bx<s->bbx;bx++)
1293 encode_blockI(tag, s, bx, by, &quant);
1297 memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1300 void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1304 if(quant<1) quant=1;
1305 if(quant>31) quant=31;
1307 writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1309 rgb2yuv(s->current, pic, s->linex, s->olinex, s->width, s->height);
1310 memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1311 memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1313 for(by=0;by<s->bby;by++)
1315 for(bx=0;bx<s->bbx;bx++)
1317 encode_blockP(tag, s, bx, by, &quant);
1321 memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1326 yuv2rgb(pic, s->current, s->linex, s->width, s->height);
1327 setdbgpic(tag, pic, s->width, s->height);
1329 if(s->frame == (int)totalframes-1)
1332 FILE*fi = fopen("test.ppm", "wb");
1333 fprintf(fi, "P6\n%d %d\n255\n", s->width, s->height);
1334 for(t=0;t<s->width*s->height;t++)
1336 fwrite(&pic[t].r, 1, 1, fi);
1337 fwrite(&pic[t].g, 1, 1, fi);
1338 fwrite(&pic[t].b, 1, 1, fi);
1345 int uline[64],vline[64],yline[64];
1346 void swf_SetVideoStreamMover(TAG*tag, VIDEOSTREAM*s, int quant)
1350 if(quant<1) quant=1;
1351 if(quant>31) quant=31;
1353 writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1355 memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1356 memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1358 for(by=0;by<s->bby;by++)
1360 for(bx=0;bx<s->bbx;bx++)
1362 //if((lrand48()&255) || !(bx>8 && bx<24 && by>8 && by<24)) {
1364 /* mvd (0,0) block (mode=0) */
1366 int mode = 0; // mvd w/o mvd24
1368 int cbpybits=0,cbpcbits=0;
1369 int predictmvdx, predictmvdy;
1370 //int mvx=-1+(2*(s->frame&1));
1371 //int mvy=-1+((s->frame&2));
1372 int mvx=(lrand48()%4)-2;
1375 swf_SetBits(tag,0,1); // COD
1376 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1377 codehuffman(tag, cbpy, cbpybits^15);
1380 predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
1381 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, mvx, mvy, 0));
1382 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, mvx, mvy, 1));
1383 s->mvdx[by*s->bbx+bx] = mvx;
1384 s->mvdy[by*s->bbx+bx] = mvy;
1386 /* i block (mode=3) */
1389 int cbpybits,cbpcbits;
1392 memset(&b, 0, sizeof(block_t));
1393 b.y1[0] = b.y2[0] = b.y3[0] = b.y4[0] = yline[bx];
1397 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
1398 swf_SetBits(tag,0,1); // COD
1399 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1400 codehuffman(tag, cbpy, cbpybits);
1403 encode8x8(tag, b.y1, has_dc, cbpybits&8);
1404 encode8x8(tag, b.y2, has_dc, cbpybits&4);
1405 encode8x8(tag, b.y3, has_dc, cbpybits&2);
1406 encode8x8(tag, b.y4, has_dc, cbpybits&1);
1409 encode8x8(tag, b.u, has_dc, cbpcbits&2);
1410 encode8x8(tag, b.v, has_dc, cbpcbits&1);
1418 int main(int argn, char*argv[])
1424 RGBA* pic, *pic2, rgb;
1431 char* fname = "/home/kramm/pics/peppers.png";
1435 memset(&stream, 0, sizeof(stream));
1437 getPNG(fname, &width, &height, &data);
1438 pic = (RGBA*)malloc(width*height*sizeof(RGBA));
1439 pic2 = (RGBA*)malloc(width*height*sizeof(RGBA));
1440 memcpy(pic, data, width*height*sizeof(RGBA));
1443 printf("Compressing %s, size %dx%d\n", fname, width, height);
1445 memset(&swf,0,sizeof(SWF));
1446 memset(&obj,0,sizeof(obj));
1448 swf.fileVersion = 6;
1449 swf.frameRate = framerate*256;
1450 swf.movieSize.xmax = 20*width*2;
1451 swf.movieSize.ymax = 20*height-20*64;
1453 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1455 rgb.r = 0x00;rgb.g = 0x00;rgb.b = 0x00;
1456 swf_SetRGB(tag,&rgb);
1458 tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
1459 swf_SetU16(tag, 33);
1460 swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
1461 stream.do_motion = 1;
1463 for(t=0;t<frames;t++)
1467 for(y=0,yy=0;y<height;y++,yy+=d) {
1468 RGBA*line = &pic[((int)yy)*width];
1469 for(x=0,xx=0;x<width;x++,xx+=d) {
1470 pic2[y*width+x] = line[((int)xx)];
1473 printf("frame:%d\n", t);fflush(stdout);
1475 tag = swf_InsertTag(tag, ST_VIDEOFRAME);
1476 swf_SetU16(tag, 33);
1478 swf_SetVideoStreamIFrame(tag, &stream, pic2, 9);
1480 swf_SetVideoStreamPFrame(tag, &stream, pic2, 9);
1483 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
1484 swf_GetPlaceObject(0, &obj);
1493 swf_SetPlaceObject(tag,&obj);
1495 tag = swf_InsertTag(tag, ST_SHOWFRAME);
1498 swf_VideoStreamClear(&stream);
1500 tag = swf_InsertTag(tag, ST_END);
1502 fi = open("video3.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
1503 if(swf_WriteSWC(fi,&swf)<0) {
1504 fprintf(stderr,"WriteSWF() failed.\n");