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"
30 - get rid of _vxy, _i endings
31 - use prepare* / write* in encode_blockI
38 void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width, U16 height)
40 swf_SetU16(tag, frames);
41 swf_SetU16(tag, width);
42 swf_SetU16(tag, height);
43 //swf_SetU8(tag, 1); /* smoothing on */
44 swf_SetU8(tag, 0); /* smoothing off */
45 swf_SetU8(tag, 2); /* codec = h.263 sorenson spark */
50 memset(stream, 0, sizeof(VIDEOSTREAM));
51 stream->olinex = width;
52 stream->owidth = width;
53 stream->oheight = height;
55 height+=15;height&=~15;
56 stream->linex = width;
57 stream->width = width;
58 stream->height = height;
59 stream->bbx = width/16;
60 stream->bby = height/16;
61 stream->current = (YUV*)malloc(width*height*sizeof(YUV));
62 stream->oldpic = (YUV*)malloc(width*height*sizeof(YUV));
63 stream->mvdx = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
64 stream->mvdy = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
65 stream->do_motion = 0;
67 memset(stream->oldpic, 0, width*height*sizeof(YUV));
68 memset(stream->current, 0, width*height*sizeof(YUV));
70 void swf_VideoStreamClear(VIDEOSTREAM*stream)
72 free(stream->oldpic);stream->oldpic = 0;
73 free(stream->current);stream->current = 0;
74 free(stream->mvdx);stream->mvdx=0;
75 free(stream->mvdy);stream->mvdy=0;
78 typedef struct _block_t
88 static int zigzagtable[64] = {
89 0, 1, 5, 6, 14, 15, 27, 28,
90 2, 4, 7, 13, 16, 26, 29, 42,
91 3, 8, 12, 17, 25, 30, 41, 43,
92 9, 11, 18, 24, 31, 40, 44, 53,
93 10, 19, 23, 32, 39, 45, 52, 54,
94 20, 22, 33, 38, 46, 51, 55, 60,
95 21, 34, 37, 47, 50, 56, 59, 61,
96 35, 36, 48, 49, 57, 58, 62, 63};
98 static void zigzag(int*src)
103 tmp[zigzagtable[t]] = src[t];
105 memcpy(src, tmp, sizeof(int)*64);
108 #define PI 3.14159265358979
109 #define SQRT2 1.414214
110 #define RSQRT2 (1.0/1.414214)
112 static double table[8][8] =
114 {0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548,0.707106781186548},
115 {0.980785280403230,0.831469612302545,0.555570233019602,0.195090322016128,-0.195090322016128,-0.555570233019602,-0.831469612302545,-0.980785280403230},
116 {0.923879532511287,0.382683432365090,-0.382683432365090,-0.923879532511287,-0.923879532511287,-0.382683432365090,0.382683432365090,0.923879532511287},
117 {0.831469612302545,-0.195090322016128,-0.980785280403230,-0.555570233019602,0.555570233019602,0.980785280403230,0.195090322016129,-0.831469612302545},
118 {0.707106781186548,-0.707106781186547,-0.707106781186548,0.707106781186547,0.707106781186548,-0.707106781186547,-0.707106781186547,0.707106781186547},
119 {0.555570233019602,-0.980785280403230,0.195090322016128,0.831469612302545,-0.831469612302545,-0.195090322016128,0.980785280403231,-0.555570233019602},
120 {0.382683432365090,-0.923879532511287,0.923879532511287,-0.382683432365090,-0.382683432365091,0.923879532511287,-0.923879532511286,0.382683432365090},
121 {0.195090322016128,-0.555570233019602,0.831469612302545,-0.980785280403231,0.980785280403230,-0.831469612302545,0.555570233019602,-0.195090322016129}
124 static void dct(int*src)
135 c+=table[u][x]*src[v*8+x];
145 c+=table[v][y]*tmp[y*8+u];
147 src[v*8+u] = (int)(c*0.25+0.5);
151 static void idct(int*src)
161 c+=table[u][x]*src[y*8+u];
171 c+=table[v][y]*tmp[v*8+x];
173 src[y*8+x] = (int)(c*0.25+0.5);
177 static double c[8] = {1.0,
178 0.980785280403230, // cos(Pi*1/16), sin(Pi*7/16)
179 0.923879532511287, // cos(Pi*2/16), sin(Pi*6/16)
180 0.831469612302545, // cos(Pi*3/16), sin(Pi*5/16)
181 0.707106781186548, // cos(Pi*4/16), sin(Pi*4/16), 1/sqrt(2)
182 0.555570233019602, // cos(Pi*5/16), sin(Pi*3/16)
183 0.382683432365090, // cos(Pi*6/16), sin(Pi*2/16)
184 0.195090322016128 // cos(Pi*7/16), sin(Pi*1/16)
188 static int ccquant = -1;
190 static void preparequant(int quant)
194 cc[0] = c[0]/(quant*2*4);
195 cc[1] = c[1]/(quant*2*4);
196 cc[2] = c[2]/(quant*2*4);
197 cc[3] = c[3]/(quant*2*4);
198 cc[4] = c[4]/(quant*2*4);
199 cc[5] = c[5]/(quant*2*4);
200 cc[6] = c[6]/(quant*2*4);
201 cc[7] = c[7]/(quant*2*4);
205 inline static void innerdct(double*a,double*b, double*c)
212 //{ 1, 3, 5, 7, -7, -5, -3, -1},
213 //{ 3, -7, -1, -5, 5, 1, 7, -3},
214 //{ 5, -1, 7, 3, -3, -7, 1, -5},
215 //{ 7, -5, 3, -1, 1, -3, 5, -7}
216 double b0,b1,b2,b3,b4,b5;
226 b[2*8] = (b2-b5)*c[2] + (b3-b4)*c[6];
227 b[6*8] = (b2-b5)*c[6] + (b4-b3)*c[2];
234 b[1*8] = b0*c[1] + b1*c[3] + b2*c[5] + b3*c[7];
235 b[3*8] = b0*c[3] - b1*c[7] - b2*c[1] - b3*c[5];
236 b[5*8] = b0*c[5] - b1*c[1] + b2*c[7] + b3*c[3];
237 b[7*8] = b0*c[7] - b1*c[5] + b2*c[3] - b3*c[1];
240 static void dct2(int*src, int*dest)
242 double tmp[64], tmp2[64];
251 double* a=&tmp2[v*8];
262 int v = (int)(tmp2[t]);
265 dest[zigzagtable[t]] = v;
270 static inline int truncate256(int a)
272 if(a>255) return 255;
277 static void getregion(block_t* bb, YUV*pic, int posx, int posy, int linex)
281 int y1=0, y2=0, y3=0, y4=0;
286 p1 = &pic[posy*linex+posx];
290 bb->u[u++] = (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
291 bb->v[v++] = (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
292 bb->y1[y1++] = p1[x].y;
293 bb->y2[y2++] = p1[x+8].y;
294 bb->y3[y3++] = p1[linex*8+x].y;
295 bb->y4[y4++] = p1[linex*8+x+8].y;
302 static void getmvdregion(block_t* bb, YUV*pic, int posx, int posy, int mvdx, int mvdy, int linex)
310 posx = posx*16 + ((mvdx&~1)/2);
311 posy = posy*16 + ((mvdy&~1)/2);
312 p1 = &pic[posy*linex+posx];
313 p2 = &pic[(posy&~1)*linex+(posx&~1)];
314 uvhp = ((mvdx&1)|((mvdx>>1)&1))|((mvdy&2)|((mvdy&1)<<1));
315 yhp = ((mvdy&1)<<1|(mvdx&1));
318 if(yhp==0 || yhp==2) {
321 bb->y1[yy] = p1[x].y;
322 bb->y2[yy] = p1[x+8].y;
323 bb->y3[yy] = p1[linex*8+x].y;
324 bb->y4[yy] = p1[linex*8+x+8].y;
332 bb->y1[yy] += p1[x].y; bb->y1[yy] /= 2;
333 bb->y2[yy] += p1[x+8].y; bb->y2[yy] /= 2;
334 bb->y3[yy] += p1[linex*8+x].y; bb->y3[yy] /= 2;
335 bb->y4[yy] += p1[linex*8+x+8].y; bb->y4[yy] /= 2;
340 } else if(yhp==1 || yhp==3) {
343 bb->y1[yy] = (p1[x].y + p1[x+1].y);
344 bb->y2[yy] = (p1[x+8].y + p1[x+8+1].y);
345 bb->y3[yy] = (p1[linex*8+x].y + p1[linex*8+x+1].y);
346 bb->y4[yy] = (p1[linex*8+x+8].y + p1[linex*8+x+8+1].y);
353 bb->y1[yy] += (p1[x].y + p1[x+1].y); bb->y1[yy]/=4;
354 bb->y2[yy] += (p1[x+8].y + p1[x+8+1].y); bb->y2[yy]/=4;
355 bb->y3[yy] += (p1[linex*8+x].y + p1[linex*8+x+1].y); bb->y3[yy]/=4;
356 bb->y4[yy] += (p1[linex*8+x+8].y + p1[linex*8+x+8+1].y); bb->y4[yy]/=4;
361 bb->y1[yy]/=2; bb->y2[yy]/=2; bb->y3[yy]/=2; bb->y4[yy]/=2;
369 if(uvhp==0 || uvhp==2) {
372 bb->u[uv] = (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
373 bb->v[uv] = (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
380 bb->u[uv] += (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].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;
388 } else /* uvhp==1 || uvhp==3 */ {
391 bb->u[uv] = ((p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4+
392 (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);
393 bb->v[uv] = ((p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4+
394 (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);
401 bb->u[uv] += ((p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4+
402 (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);
403 bb->v[uv] += ((p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4+
404 (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);
420 static void rgb2yuv(YUV*dest, RGBA*src, int dlinex, int slinex, int width, int height)
423 for(y=0;y<height;y++) {
424 for(x=0;x<width;x++) {
426 r = src[y*slinex+x].r;
427 g = src[y*slinex+x].g;
428 b = src[y*slinex+x].b;
429 /*dest[y*dlinex+x].y = (r*0.299 + g*0.587 + b*0.114);
430 dest[y*dlinex+x].u = (r*-0.169 + g*-0.332 + b*0.500 + 128.0);
431 dest[y*dlinex+x].v = (r*0.500 + g*-0.419 + b*-0.0813 + 128.0);*/
433 //dest[y*dlinex+x].y = 128;//(r*((int)( 0.299*256)) + g*((int)( 0.587*256)) + b*((int)( 0.114 *256)))>>8;
434 dest[y*dlinex+x].y = (r*((int)( 0.299*256)) + g*((int)( 0.587*256)) + b*((int)( 0.114 *256)))>>8;
435 dest[y*dlinex+x].u = (r*((int)(-0.169*256)) + g*((int)(-0.332*256)) + b*((int)( 0.500 *256))+ 128*256)>>8;
436 dest[y*dlinex+x].v = (r*((int)( 0.500*256)) + g*((int)(-0.419*256)) + b*((int)(-0.0813*256))+ 128*256)>>8;
441 static void copyregion(VIDEOSTREAM*s, YUV*dest, YUV*src, int bx, int by)
443 YUV*p1 = &src[by*s->linex*16+bx*16];
444 YUV*p2 = &dest[by*s->linex*16+bx*16];
447 memcpy(p1, p2, 16*sizeof(YUV));
448 p1+=s->linex;p2+=s->linex;
452 static void yuv2rgb(RGBA*dest, YUV*src, int linex, int width, int height)
455 for(y=0;y<height;y++) {
456 for(x=0;x<width;x++) {
458 u = src[y*linex+x].u;
459 v = src[y*linex+x].v;
460 yy = src[y*linex+x].y;
461 dest[y*linex+x].r = truncate256(yy + ((360*(v-128))>>8));
462 dest[y*linex+x].g = truncate256(yy - ((88*(u-128)+183*(v-128))>>8));
463 dest[y*linex+x].b = truncate256(yy + ((455 * (u-128))>>8));
467 static void copyblock(VIDEOSTREAM*s, YUV*dest, block_t*b, int bx, int by)
469 YUV*p1 = &dest[(by*16)*s->linex+bx*16];
470 YUV*p2 = &dest[(by*16+8)*s->linex+bx*16];
475 p1[x+0].u = b->u[(y/2)*8+(x/2)];
476 p1[x+0].v = b->v[(y/2)*8+(x/2)];
477 p1[x+0].y = b->y1[y*8+x];
478 p1[x+8].u = b->u[(y/2)*8+(x/2)+4];
479 p1[x+8].v = b->v[(y/2)*8+(x/2)+4];
480 p1[x+8].y = b->y2[y*8+x];
481 p2[x+0].u = b->u[(y/2+4)*8+(x/2)];
482 p2[x+0].v = b->v[(y/2+4)*8+(x/2)];
483 p2[x+0].y = b->y3[y*8+x];
484 p2[x+8].u = b->u[(y/2+4)*8+(x/2)+4];
485 p2[x+8].v = b->v[(y/2+4)*8+(x/2)+4];
486 p2[x+8].y = b->y4[y*8+x];
493 static int compare_pic_oldpic(VIDEOSTREAM*s, int bx, int by)
495 int linex = s->width;
496 YUV*p1 = &s->current[by*linex*16+bx*16];
497 YUV*p2 = &s->oldpic[by*linex*16+bx*16];
498 int diffy=0, diffuv = 0;
508 diffuv += abs(u)+abs(v);
513 return diffy + diffuv/4;
516 static int compare_pic_block(VIDEOSTREAM*s, block_t* b, int bx, int by)
518 int linex = s->width;
519 YUV*y1 = &s->current[(by*2)*linex*8+bx*16];
520 YUV*y2 = &s->current[(by*2)*linex*8+bx*16+8];
521 YUV*y3 = &s->current[(by*2+1)*linex*8+bx*16];
522 YUV*y4 = &s->current[(by*2+1)*linex*8+bx*16+8];
524 int diffy=0, diffuv = 0;
530 yy = y1[x].y - b->y1[y8x];
532 yy = y2[x].y - b->y2[y8x];
534 yy = y3[x].y - b->y3[y8x];
536 yy = y4[x].y - b->y4[y8x];
538 u = uv[x*2].u - b->u[y8x];
539 v = uv[x*2].v - b->v[y8x];
540 diffuv += (abs(u)+abs(v))*4;
548 return diffy + diffuv/4;
551 static inline int valtodc(int val)
559 /* TODO: what to do for zero values? skip the block? */
568 static int dctoval(int dc)
581 static int codehuffman(TAG*tag, struct huffcode*table, int index)
583 /* TODO: !optimize! */
585 while(table[index].code[i]) {
586 if(table[index].code[i]=='0')
587 swf_SetBits(tag, 0, 1);
589 swf_SetBits(tag, 1, 1);
595 static void quantize8x8(int*src, int*dest, int has_dc, int quant)
598 double q = 1.0/(quant*2);
600 dest[0] = valtodc((int)src[0]); /*DC*/
605 //dest[t] = (int)src[t];
606 /* exact: if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;} */
607 //if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;}
608 //dest[t] = dest[t]/(quant*2);
609 dest[t] = (int)(src[t]*q);
610 /* TODO: warn if this happens- the video will be buggy */
611 if(dest[t]>127) dest[t]=127;
612 if(dest[t]<-127) dest[t]=-127;
616 static void dequantize8x8(int*b, int has_dc, int quant)
620 b[0] = dctoval(b[0]); //DC
623 for(t=pos;t<64;t++) {
632 b[t] = quant*(2*b[t]+1); //-7,8,24,40
634 b[t] = quant*(2*b[t]+1)-1; //-8,7,23,39
641 /* paragraph 6.2.2, "clipping of reconstruction levels": */
642 if(b[t]>2047) b[t]=2047;
643 if(b[t]<-2048) b[t]=-2048;
647 static int hascoef(int*b, int has_dc)
653 for(t=pos;t<64;t++) {
660 static int coefbits8x8(int*bb, int has_dc)
671 for(last=63;last>=pos;last--) {
678 int run=0, level=0, islast=0,t;
679 while(!bb[pos] && pos<last) {
686 if(level<0) level=-level;
688 for(t=0;t<RLE_ESCAPE;t++) {
689 if(rle_params[t].run == run &&
690 rle_params[t].level == level &&
691 rle_params[t].last == islast) {
692 bits += rle[t].len + 1;
697 bits += rle[RLE_ESCAPE].len + 1 + 6 + 8;
706 static int encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
713 swf_SetBits(tag, bb[0], 8);
720 /* determine last non-null coefficient */
721 for(last=63;last>=pos;last--) {
722 /* TODO: we could leave out small coefficients
723 after a certain point (32?) */
727 /* blocks without coefficients should not be included
728 in the cbpy/cbpc patterns: */
737 while(!bb[pos] && pos<last) {
749 for(t=0;t<RLE_ESCAPE;t++) {
750 /* TODO: lookup table */
751 if(rle_params[t].run == run &&
752 rle_params[t].level == level &&
753 rle_params[t].last == islast) {
754 bits += codehuffman(tag, rle, t);
755 swf_SetBits(tag, sign, 1);
761 bits += codehuffman(tag, rle, RLE_ESCAPE);
768 swf_SetBits(tag, islast, 1);
769 swf_SetBits(tag, run, 6);
770 swf_SetBits(tag, level, 8); //FIXME: fixme??
782 static void quantize(block_t*fb, block_t*b, int has_dc, int quant)
784 quantize8x8(fb->y1, b->y1, has_dc, quant);
785 quantize8x8(fb->y2, b->y2, has_dc, quant);
786 quantize8x8(fb->y3, b->y3, has_dc, quant);
787 quantize8x8(fb->y4, b->y4, has_dc, quant);
788 quantize8x8(fb->u, b->u, has_dc, quant);
789 quantize8x8(fb->v, b->v, has_dc, quant);
792 static void dodct(block_t*fb)
794 dct(fb->y1); dct(fb->y2); dct(fb->y3); dct(fb->y4);
795 dct(fb->u); dct(fb->v);
803 static void dodctandquant(block_t*fb, block_t*b, int has_dc, int quant)
808 quantize(fb,b,has_dc,quant);
812 dct2(fb->y1,b->y1); dct2(fb->y2,b->y2); dct2(fb->y3,b->y3); dct2(fb->y4,b->y4);
813 dct2(fb->u,b->u); dct2(fb->v,b->v);
816 static void doidct(block_t*b)
821 fb.y1[t] = b->y1[zigzagtable[t]];
822 fb.y2[t] = b->y2[zigzagtable[t]];
823 fb.y3[t] = b->y3[zigzagtable[t]];
824 fb.y4[t] = b->y4[zigzagtable[t]];
825 fb.u[t] = b->u[zigzagtable[t]];
826 fb.v[t] = b->v[zigzagtable[t]];
828 idct(fb.y1); idct(fb.y2); idct(fb.y3); idct(fb.y4);
829 idct(fb.u); idct(fb.v);
839 static void truncateblock(block_t*b)
843 b->y1[t] = truncate256(b->y1[t]);
844 b->y2[t] = truncate256(b->y2[t]);
845 b->y3[t] = truncate256(b->y3[t]);
846 b->y4[t] = truncate256(b->y4[t]);
847 b->u[t] = truncate256(b->u[t]);
848 b->v[t] = truncate256(b->v[t]);
852 static void dequantize(block_t*b, int has_dc, int quant)
854 dequantize8x8(b->y1, has_dc, quant);
855 dequantize8x8(b->y2, has_dc, quant);
856 dequantize8x8(b->y3, has_dc, quant);
857 dequantize8x8(b->y4, has_dc, quant);
858 dequantize8x8(b->u, has_dc, quant);
859 dequantize8x8(b->v, has_dc, quant);
862 static void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
867 *cbpybits|=hascoef(b->y1, has_dc)*8;
868 *cbpybits|=hascoef(b->y2, has_dc)*4;
869 *cbpybits|=hascoef(b->y3, has_dc)*2;
870 *cbpybits|=hascoef(b->y4, has_dc)*1;
872 *cbpcbits|=hascoef(b->u, has_dc)*2;
873 *cbpcbits|=hascoef(b->v, has_dc)*1;
876 static void setQuant(TAG*tag, int dquant)
883 swf_SetBits(tag, 0x0, 2);
884 } else if(dquant == -2) {
885 swf_SetBits(tag, 0x1, 2);
886 } else if(dquant == +1) {
887 swf_SetBits(tag, 0x2, 2);
888 } else if(dquant == +2) {
889 swf_SetBits(tag, 0x3, 2);
891 assert(0*strlen("invalid dquant"));
895 static void change_quant(int quant, int*dquant)
901 static void yuvdiff(block_t*a, block_t*b)
905 a->y1[t] = (a->y1[t] - b->y1[t]);
906 a->y2[t] = (a->y2[t] - b->y2[t]);
907 a->y3[t] = (a->y3[t] - b->y3[t]);
908 a->y4[t] = (a->y4[t] - b->y4[t]);
909 a->u[t] = (a->u[t] - b->u[t]);
910 a->v[t] = (a->v[t] - b->v[t]);
914 static void predictmvd(VIDEOSTREAM*s, int bx, int by, int*px, int*py)
917 int x1,y1,x2,y2,x3,y3;
919 if(bx) {x1=s->mvdx[by*s->bbx+bx-1];
920 y1=s->mvdy[by*s->bbx+bx-1];
923 if(by) {x2=s->mvdx[(by-1)*s->bbx+bx];
924 y2=s->mvdy[(by-1)*s->bbx+bx];
926 x3=s->mvdx[(by-1)*s->bbx+bx+1];
927 y3=s->mvdy[(by-1)*s->bbx+bx+1];
932 else {x2=x3=x1;y2=y3=y1;}
934 if((x1 <= x2 && x2 <= x3) ||
935 (x3 <= x2 && x2 <= x1)) {
937 } else if((x2 <= x1 && x1 <= x3) ||
938 (x3 <= x1 && x1 <= x2)) {
940 } else if((x1 <= x3 && x3 <= x2) ||
941 (x2 <= x3 && x3 <= x1)) {
948 if((y1 <= y2 && y2 <= y3) ||
949 (y3 <= y2 && y2 <= y1)) {
951 } else if((y2 <= y1 && y1 <= y3) ||
952 (y3 <= y1 && y1 <= y2)) {
954 } else if((y1 <= y3 && y3 <= y2) ||
955 (y2 <= y3 && y3 <= y1)) {
964 assert((x4>=-32 && x4<=31) && (y4>=-32 && y4<=31));
967 static inline int mvd2index(int px, int py, int x, int y, int xy)
969 assert((x>=-32 && x<=31) && (y>=-32 && y<=31));
970 //assert((x&1)==0 && (y&1)==0);//for now
971 //assert((x&2)==0 && (y&2)==0);//for now(2)
986 assert(x>=0 && x<64);
990 typedef struct _iblockdata_t
992 block_t b_i; //transformed quantized coefficients
993 block_t reconstruction;
998 typedef struct _mvdblockdata_t
1002 block_t reconstruction;
1011 void prepareIBlock(VIDEOSTREAM*s, iblockdata_t*data, int bx, int by, block_t* fb, int*bits)
1013 /* consider I-block */
1020 memcpy(&fb_i, fb, sizeof(block_t));
1021 dodctandquant(&fb_i, &data->b_i, 1, s->quant);
1022 getblockpatterns(&data->b_i, &y, &c, 1);
1024 *bits += mcbpc_inter[3*4+c].len;
1025 *bits += cbpy[y].len;
1026 *bits += coefbits8x8(data->b_i.y1, 1);
1027 *bits += coefbits8x8(data->b_i.y2, 1);
1028 *bits += coefbits8x8(data->b_i.y3, 1);
1029 *bits += coefbits8x8(data->b_i.y4, 1);
1030 *bits += coefbits8x8(data->b_i.u, 1);
1031 *bits += coefbits8x8(data->b_i.v, 1);
1034 /* -- reconstruction -- */
1035 memcpy(&data->reconstruction,&data->b_i,sizeof(block_t));
1036 dequantize(&data->reconstruction, 1, s->quant);
1037 doidct(&data->reconstruction);
1038 truncateblock(&data->reconstruction);
1041 int writeIBlock(VIDEOSTREAM*s, TAG*tag, iblockdata_t*data)
1043 int cbpcbits = 0, cbpybits=0;
1044 int mode = 3; /* i block (mode=3) */
1049 getblockpatterns(&data->b_i, &cbpybits, &cbpcbits, has_dc);
1050 swf_SetBits(tag,0,1); bits += 1; // COD
1051 bits += codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1052 bits += codehuffman(tag, cbpy, cbpybits);
1055 bits += encode8x8(tag, data->b_i.y1, has_dc, cbpybits&8);
1056 bits += encode8x8(tag, data->b_i.y2, has_dc, cbpybits&4);
1057 bits += encode8x8(tag, data->b_i.y3, has_dc, cbpybits&2);
1058 bits += encode8x8(tag, data->b_i.y4, has_dc, cbpybits&1);
1061 bits += encode8x8(tag, data->b_i.u, has_dc, cbpcbits&2);
1062 bits += encode8x8(tag, data->b_i.v, has_dc, cbpcbits&1);
1064 copyblock(s, s->current, &data->reconstruction, data->bx, data->by);
1065 assert(data->bits == bits);
1069 void prepareMVDBlock(VIDEOSTREAM*s, mvdblockdata_t*data, int bx, int by, block_t* fb, int*bits)
1070 { /* consider mvd(x,y)-block */
1078 predictmvd(s,bx,by,&data->predictmvdx,&data->predictmvdy);
1087 int bestx=0,besty=0,bestbits=65536;
1088 int startx=-32,endx=31;
1089 int starty=-32,endy=31;
1093 if(bx==s->bbx-1) endx=0;
1094 if(by==s->bby-1) endy=0;
1096 for(hx=startx;hx<=endx;hx+=1)
1097 for(hy=starty;hy<=endy;hy+=1)
1102 memcpy(&fbdiff, fb, sizeof(block_t));
1103 getmvdregion(&fbold, s->oldpic, bx, by, hx, hy, s->linex);
1104 yuvdiff(&fbdiff, &fbold);
1105 dodctandquant(&fbdiff, &b, 0, s->quant);
1106 bits += coefbits8x8(b.y1, 0);
1107 bits += coefbits8x8(b.y2, 0);
1108 bits += coefbits8x8(b.y3, 0);
1109 bits += coefbits8x8(b.y4, 0);
1110 bits += coefbits8x8(b.u, 0);
1111 bits += coefbits8x8(b.v, 0);
1118 data->x_vxy = bestx;
1119 data->y_vxy = besty;
1122 memcpy(&fbdiff, fb, sizeof(block_t));
1123 getmvdregion(&data->fbold_vxy, s->oldpic, bx, by, data->x_vxy, data->y_vxy, s->linex);
1124 yuvdiff(&fbdiff, &data->fbold_vxy);
1125 dodctandquant(&fbdiff, &data->b_vxy, 0, s->quant);
1126 getblockpatterns(&data->b_vxy, &y, &c, 0);
1129 *bits += mcbpc_inter[0*4+c].len;
1130 *bits += cbpy[y^15].len;
1131 *bits += mvd[mvd2index(data->predictmvdx, data->predictmvdy, data->x_vxy, data->y_vxy, 0)].len; // (0,0)
1132 *bits += mvd[mvd2index(data->predictmvdx, data->predictmvdy, data->x_vxy, data->y_vxy, 1)].len;
1133 *bits += coefbits8x8(data->b_vxy.y1, 0);
1134 *bits += coefbits8x8(data->b_vxy.y2, 0);
1135 *bits += coefbits8x8(data->b_vxy.y3, 0);
1136 *bits += coefbits8x8(data->b_vxy.y4, 0);
1137 *bits += coefbits8x8(data->b_vxy.u, 0);
1138 *bits += coefbits8x8(data->b_vxy.v, 0);
1141 /* -- reconstruction -- */
1142 memcpy(&data->reconstruction, &data->b_vxy, sizeof(block_t));
1143 dequantize(&data->reconstruction, 0, s->quant);
1144 doidct(&data->reconstruction);
1146 data->reconstruction.y1[t] = truncate256(data->reconstruction.y1[t] + (int)data->fbold_vxy.y1[t]);
1147 data->reconstruction.y2[t] = truncate256(data->reconstruction.y2[t] + (int)data->fbold_vxy.y2[t]);
1148 data->reconstruction.y3[t] = truncate256(data->reconstruction.y3[t] + (int)data->fbold_vxy.y3[t]);
1149 data->reconstruction.y4[t] = truncate256(data->reconstruction.y4[t] + (int)data->fbold_vxy.y4[t]);
1150 data->reconstruction.u[t] = truncate256(data->reconstruction.u[t] + (int)data->fbold_vxy.u[t]);
1151 data->reconstruction.v[t] = truncate256(data->reconstruction.v[t] + (int)data->fbold_vxy.v[t]);
1155 int writeMVDBlock(VIDEOSTREAM*s, TAG*tag, mvdblockdata_t*data)
1158 /* mvd (0,0) block (mode=0) */
1160 int has_dc=0; // mvd w/o mvd24
1166 getblockpatterns(&data->b_vxy, &y, &c, has_dc);
1167 swf_SetBits(tag,0,1); bits += 1; // COD
1168 bits += codehuffman(tag, mcbpc_inter, mode*4+c);
1169 bits += codehuffman(tag, cbpy, y^15);
1172 bits += codehuffman(tag, mvd, mvd2index(data->predictmvdx, data->predictmvdy, data->x_vxy, data->y_vxy, 0));
1173 bits += codehuffman(tag, mvd, mvd2index(data->predictmvdx, data->predictmvdy, data->x_vxy, data->y_vxy, 1));
1174 s->mvdx[by*s->bbx+bx] = data->x_vxy;
1175 s->mvdy[by*s->bbx+bx] = data->y_vxy;
1178 bits += encode8x8(tag, data->b_vxy.y1, has_dc, y&8);
1179 bits += encode8x8(tag, data->b_vxy.y2, has_dc, y&4);
1180 bits += encode8x8(tag, data->b_vxy.y3, has_dc, y&2);
1181 bits += encode8x8(tag, data->b_vxy.y4, has_dc, y&1);
1184 bits += encode8x8(tag, data->b_vxy.u, has_dc, c&2);
1185 bits += encode8x8(tag, data->b_vxy.v, has_dc, c&1);
1187 copyblock(s, s->current, &data->reconstruction, data->bx, data->by);
1188 assert(data->bits == bits);
1193 /* should be called encode_PFrameBlock */
1194 static int encode_blockP(TAG*tag, VIDEOSTREAM*s, int bx, int by)
1201 iblockdata_t iblock;
1202 mvdblockdata_t mvdblock;
1204 getregion(&fb, s->current, bx, by, s->width);
1205 prepareIBlock(s, &iblock, bx, by, &fb, &bits_i);
1207 /* encoded last frame <=> original current block: */
1208 diff1 = compare_pic_oldpic(s, bx, by);
1209 /* encoded current frame <=> original current block: */
1210 diff2 = compare_pic_block(s, &iblock.reconstruction, bx, by);
1212 if(diff1 <= diff2) {
1213 swf_SetBits(tag, 1,1); /* cod=1, block skipped */
1214 /* copy the region from the last frame so that we have a complete reconstruction */
1215 copyregion(s, s->current, s->oldpic, bx, by);
1218 prepareMVDBlock(s, &mvdblock, bx, by, &fb, &bits_vxy);
1220 if(bits_i > bits_vxy) {
1221 return writeMVDBlock(s, tag, &mvdblock);
1223 return writeIBlock(s, tag, &iblock);
1227 /* should be called encode_IFrameBlock */
1228 static void encode_blockI(TAG*tag, VIDEOSTREAM*s, int bx, int by)
1235 getregion(&fb, s->current, bx, by, s->width);
1237 change_quant(s->quant, &dquant);
1240 dodctandquant(&fb, &b, 1, s->quant);
1242 getblockpatterns(&b, &y, &c, 1);
1245 codehuffman(tag, mcbpc_intra, 4+c);
1247 codehuffman(tag, mcbpc_intra, 0+c);
1250 codehuffman(tag, cbpy, y);
1253 setQuant(tag, dquant);
1257 encode8x8(tag, b.y1, 1, y&8);
1258 encode8x8(tag, b.y2, 1, y&4);
1259 encode8x8(tag, b.y3, 1, y&2);
1260 encode8x8(tag, b.y4, 1, y&1);
1263 encode8x8(tag, b.u, 1, c&2);
1264 encode8x8(tag, b.v, 1, c&1);
1267 dequantize(&b, 1, s->quant);
1270 copyblock(s, s->current, &b, bx, by);
1273 /*static void encode_blockI(TAG*tag, VIDEOSTREAM*s, int bx, int by)
1280 int cbpcbits = 0, cbpybits = 0;
1282 getregion(&fb, s->current, bx, by, s->width);
1283 prepareIBlock(s, &data, bx, by, &fb, &bits, &quality);
1285 getblockpatterns(&data.b_i, &cbpybits, &cbpcbits, has_dc);
1288 codehuffman(tag, mcbpc_intra, 4+cbpcbits);
1290 codehuffman(tag, mcbpc_intra, 0+cbpcbits);
1293 codehuffman(tag, cbpy, cbpybits);
1296 setQuant(tag, dquant);
1300 encode8x8(tag, b.y1, 1, cbpybits&8);
1301 encode8x8(tag, b.y2, 1, cbpybits&4);
1302 encode8x8(tag, b.y3, 1, cbpybits&2);
1303 encode8x8(tag, b.y4, 1, cbpybits&1);
1306 encode8x8(tag, b.u, 1, cbpcbits&2);
1307 encode8x8(tag, b.v, 1, cbpcbits&1);
1309 copyblock(s, s->current, &data->reconstruction, data->bx, data->by);
1312 static int bmid = 0;
1313 void setdbgpic(TAG*tag, RGBA*pic, int width, int height)
1318 tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
1319 swf_SetU16(tag, 133);
1321 tag = swf_InsertTag(tag, ST_DEFINEBITSLOSSLESS);
1322 swf_SetU16(tag, 1000+bmid);
1323 swf_SetLosslessBits(tag, width, height, (void*)pic, BMF_32BIT);
1325 tag = swf_InsertTag(tag, ST_DEFINESHAPE);
1326 swf_SetU16(tag, 2000+bmid);
1327 swf_ShapeSetBitmapRect(tag, 1000+bmid, width, height);
1329 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1330 swf_GetMatrix(0,&m);
1332 swf_ObjectPlace(tag, 2000+bmid, 133, &m, 0, 0);
1337 #define TYPE_IFRAME 0
1338 #define TYPE_PFRAME 1
1340 static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
1343 swf_SetU16(tag, frame);
1344 swf_SetBits(tag, 1, 17); /* picture start code*/
1345 swf_SetBits(tag, 0, 5); /* version=0, version 1 would optimize rle behaviour*/
1346 swf_SetBits(tag, frame, 8); /* time reference */
1348 /* write dimensions, taking advantage of some predefined sizes
1349 if the opportunity presents itself */
1350 i32 = width<<16|height;
1353 case 352<<16|288: swf_SetBits(tag, 2, 3);break;
1354 case 176<<16|144: swf_SetBits(tag, 3, 3);break;
1355 case 128<<16|96: swf_SetBits(tag, 4, 3);break;
1356 case 320<<16|240: swf_SetBits(tag, 5, 3);break;
1357 case 160<<16|120: swf_SetBits(tag, 6, 3);break;
1359 if(width>255 || height>255) {
1360 swf_SetBits(tag, 1, 3);
1361 swf_SetBits(tag, width, 16);
1362 swf_SetBits(tag, height, 16);
1364 swf_SetBits(tag, 0, 3);
1365 swf_SetBits(tag, width, 8);
1366 swf_SetBits(tag, height, 8);
1370 swf_SetBits(tag, type, 2); /* I-Frame or P-Frame */
1371 swf_SetBits(tag, 0, 1); /* No deblock filter */
1373 swf_SetBits(tag, quant, 5); /* quantizer (1-31), may be updated later on*/
1374 swf_SetBits(tag, 0, 1); /* No extra info */
1377 void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1381 if(quant<1) quant=1;
1382 if(quant>31) quant=31;
1385 writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_IFRAME);
1387 memset(s->current, 0, s->linex*s->height*sizeof(YUV));
1388 rgb2yuv(s->current, pic, s->linex, s->olinex, s->owidth, s->oheight);
1392 for(by=0;by<s->bby;by++)
1394 for(bx=0;bx<s->bbx;bx++)
1396 encode_blockI(tag, s, bx, by);
1400 memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1403 void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1407 if(quant<1) quant=1;
1408 if(quant>31) quant=31;
1411 writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1413 memset(s->current, 0, s->linex*s->height*sizeof(YUV));
1414 rgb2yuv(s->current, pic, s->linex, s->olinex, s->owidth, s->oheight);
1415 memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1416 memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1418 for(by=0;by<s->bby;by++)
1420 for(bx=0;bx<s->bbx;bx++)
1422 encode_blockP(tag, s, bx, by);
1426 memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1431 yuv2rgb(pic, s->current, s->linex, s->width, s->height);
1432 setdbgpic(tag, pic, s->width, s->height);
1434 if(s->frame == (int)totalframes-1)
1437 FILE*fi = fopen("test.ppm", "wb");
1438 fprintf(fi, "P6\n%d %d\n255\n", s->width, s->height);
1439 for(t=0;t<s->width*s->height;t++)
1441 fwrite(&pic[t].r, 1, 1, fi);
1442 fwrite(&pic[t].g, 1, 1, fi);
1443 fwrite(&pic[t].b, 1, 1, fi);
1450 int uline[64],vline[64],yline[64];
1451 void swf_SetVideoStreamMover(TAG*tag, VIDEOSTREAM*s, int quant)
1455 if(quant<1) quant=1;
1456 if(quant>31) quant=31;
1458 writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1460 memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1461 memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1463 for(by=0;by<s->bby;by++)
1465 for(bx=0;bx<s->bbx;bx++)
1467 //if((lrand48()&255) || !(bx>8 && bx<24 && by>8 && by<24)) {
1469 /* mvd (0,0) block (mode=0) */
1471 int mode = 0; // mvd w/o mvd24
1473 int cbpybits=0,cbpcbits=0;
1474 int predictmvdx, predictmvdy;
1475 //int mvx=-1+(2*(s->frame&1));
1476 //int mvy=-1+((s->frame&2));
1477 int mvx=0;//(lrand48()%4)-2;
1480 swf_SetBits(tag,0,1); // COD
1481 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1482 codehuffman(tag, cbpy, cbpybits^15);
1485 predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
1486 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, mvx, mvy, 0));
1487 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, mvx, mvy, 1));
1488 s->mvdx[by*s->bbx+bx] = mvx;
1489 s->mvdy[by*s->bbx+bx] = mvy;
1491 /* i block (mode=3) */
1494 int cbpybits,cbpcbits;
1497 memset(&b, 0, sizeof(block_t));
1498 b.y1[0] = b.y2[0] = b.y3[0] = b.y4[0] = yline[bx];
1502 getblockpatterns(&b, &cbpybits, &cbpcbits, has_dc);
1503 swf_SetBits(tag,0,1); // COD
1504 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1505 codehuffman(tag, cbpy, cbpybits);
1508 encode8x8(tag, b.y1, has_dc, cbpybits&8);
1509 encode8x8(tag, b.y2, has_dc, cbpybits&4);
1510 encode8x8(tag, b.y3, has_dc, cbpybits&2);
1511 encode8x8(tag, b.y4, has_dc, cbpybits&1);
1514 encode8x8(tag, b.u, has_dc, cbpcbits&2);
1515 encode8x8(tag, b.v, has_dc, cbpcbits&1);
1523 int main(int argn, char*argv[])
1529 RGBA* pic, *pic2, rgb;
1536 char* fname = "/home/kramm/pics/peppers.png";
1540 memset(&stream, 0, sizeof(stream));
1542 getPNG(fname, &width, &height, &data);
1543 pic = (RGBA*)malloc(width*height*sizeof(RGBA));
1544 pic2 = (RGBA*)malloc(width*height*sizeof(RGBA));
1545 memcpy(pic, data, width*height*sizeof(RGBA));
1548 printf("Compressing %s, size %dx%d\n", fname, width, height);
1550 memset(&swf,0,sizeof(SWF));
1551 memset(&obj,0,sizeof(obj));
1553 swf.fileVersion = 6;
1554 swf.frameRate = framerate*256;
1555 swf.movieSize.xmax = 20*width*2;
1556 swf.movieSize.ymax = 20*height-20*64;
1558 swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1560 rgb.r = 0x00;rgb.g = 0x00;rgb.b = 0x00;
1561 swf_SetRGB(tag,&rgb);
1563 tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
1564 swf_SetU16(tag, 33);
1565 swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
1566 stream.do_motion = 0;
1568 for(t=0;t<frames;t++)
1572 for(y=0,yy=0;y<height;y++,yy+=d) {
1573 RGBA*line = &pic[((int)yy)*width];
1574 for(x=0,xx=0;x<width;x++,xx+=d) {
1575 pic2[y*width+x] = line[((int)xx)];
1578 printf("frame:%d\n", t);fflush(stdout);
1580 tag = swf_InsertTag(tag, ST_VIDEOFRAME);
1581 swf_SetU16(tag, 33);
1583 swf_SetVideoStreamIFrame(tag, &stream, pic2, 9);
1585 swf_SetVideoStreamPFrame(tag, &stream, pic2, 9);
1588 tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
1589 swf_GetPlaceObject(0, &obj);
1598 swf_SetPlaceObject(tag,&obj);
1600 tag = swf_InsertTag(tag, ST_SHOWFRAME);
1603 swf_VideoStreamClear(&stream);
1605 tag = swf_InsertTag(tag, ST_END);
1607 fi = open("video3.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
1608 if(swf_WriteSWC(fi,&swf)<0) {
1609 fprintf(stderr,"WriteSWF() failed.\n");