05a1df80ecfcf1557a940071a245aa127bffdac9
[swftools.git] / lib / h.263 / swfvideo.c
1 /* swfvideo.c
2    Routines for handling h.263 video tags
3
4    Part of the swftools package.
5
6    Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
7
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.
12
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.
17
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 */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <assert.h>
25 #include <math.h>
26 #include "../rfxswf.h"
27 #include "h263tables.h"
28 #include "dct.h"
29
30 /* TODO:
31    - use prepare* / write* in encode_IFrame_block
32    - check whether mvd steps of 2 lead to (much) smaller results
33 */ 
34
35 #ifdef MAIN
36 U16 totalframes = 0;
37 #endif
38 void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width, U16 height)
39 {
40 #ifdef MAIN
41     totalframes = frames;
42 #endif
43     memset(stream, 0, sizeof(VIDEOSTREAM));
44     stream->olinex = width;
45     stream->owidth = width;
46     stream->oheight = height;
47     width+=15;width&=~15;
48     height+=15;height&=~15;
49     stream->linex = width;
50     stream->width = width;
51     stream->height = height;
52     stream->bbx = width/16;
53     stream->bby = height/16;
54     stream->current = (YUV*)malloc(width*height*sizeof(YUV));
55     stream->oldpic = (YUV*)malloc(width*height*sizeof(YUV));
56     stream->mvdx = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
57     stream->mvdy = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
58     stream->do_motion = 0;
59
60     memset(stream->oldpic, 0, width*height*sizeof(YUV));
61     memset(stream->current, 0, width*height*sizeof(YUV));
62
63     assert((stream->width&15) == 0);
64     assert((stream->height&15) == 0);
65     assert((stream->bbx*16) == stream->width);
66     assert((stream->bby*16) == stream->height);
67     
68     swf_SetU16(tag, frames);
69     swf_SetU16(tag, width);
70     swf_SetU16(tag, height);
71     //swf_SetU8(tag, 1); /* smoothing on */
72     swf_SetU8(tag, 0); /* smoothing off */
73     swf_SetU8(tag, 2); /* codec = h.263 sorenson spark */
74
75 }
76 void swf_VideoStreamClear(VIDEOSTREAM*stream)
77 {
78     free(stream->oldpic);stream->oldpic = 0;
79     free(stream->current);stream->current = 0;
80     free(stream->mvdx);stream->mvdx=0;
81     free(stream->mvdy);stream->mvdy=0;
82 }
83
84 typedef struct _block_t
85 {
86     int y1[64];
87     int y2[64];
88     int y3[64];
89     int y4[64];
90     int u[64];
91     int v[64];
92 } block_t;
93
94 static inline int truncate256(int a)
95 {
96     if(a>255) return 255;
97     if(a<0) return 0;
98     return a;
99 }
100
101 static void getregion(block_t* bb, YUV*pic, int posx, int posy, int linex)
102 {
103     YUV*p1;
104     YUV*p2;
105     int i=0;
106     int x,y;
107     posx*=16;
108     posy*=16;
109     p1 = &pic[posy*linex+posx];
110     p2 = p1;
111     for(y=0;y<8;y++) {
112         for(x=0;x<8;x++) {
113             bb->u[i] = (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
114             bb->v[i] = (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
115             bb->y1[i] = p1[x].y;
116             bb->y2[i] = p1[x+8].y;
117             bb->y3[i] = p1[linex*8+x].y;
118             bb->y4[i] = p1[linex*8+x+8].y;
119             i++;
120         }
121         p1+=linex;
122         p2+=linex*2;
123     }
124 }
125
126 /* This function is pretty complex. Let's hope it works correctly */
127 static void getmvdregion(block_t* bb, YUV*pic, int posx, int posy, int mvdx, int mvdy, int linex)
128 {
129     YUV*p1;
130     YUV*p2;
131     int yy=0,uv=0;
132     int x,y;
133     int yhp = 0, uvhp=0;
134     int uvposx, uvposy;
135     posx = posx*16 + ((mvdx&~1)/2); //works also for negative mvdx (unlike mvdx/2)
136     posy = posy*16 + ((mvdy&~1)/2);
137     p1 = &pic[posy*linex+posx];
138     p2 = &pic[(posy&~1)*linex+(posx&~1)];
139     uvhp = ((mvdx&1)|((mvdx>>1)&1))|((mvdy&2)|((mvdy&1)<<1));
140     yhp = ((mvdy&1)<<1|(mvdx&1));
141
142     /* y */
143     if(yhp==0 || yhp==2) {
144         for(y=0;y<8;y++) {
145             for(x=0;x<8;x++) {
146                 bb->y1[yy] = p1[x].y;
147                 bb->y2[yy] = p1[x+8].y;
148                 bb->y3[yy] = p1[linex*8+x].y;
149                 bb->y4[yy] = p1[linex*8+x+8].y;
150                 yy++;
151             }
152             p1+=linex;
153
154             if(yhp==2) {
155                 yy-=8;
156                 for(x=0;x<8;x++) {
157                     bb->y1[yy] += p1[x].y; bb->y1[yy] /= 2;
158                     bb->y2[yy] += p1[x+8].y; bb->y2[yy] /= 2;
159                     bb->y3[yy] += p1[linex*8+x].y; bb->y3[yy] /= 2;
160                     bb->y4[yy] += p1[linex*8+x+8].y; bb->y4[yy] /= 2;
161                     yy++;
162                 }
163             }
164         }
165     } else if(yhp==1 || yhp==3) {
166         for(y=0;y<8;y++) {
167             for(x=0;x<8;x++) {
168                 bb->y1[yy] = (p1[x].y + p1[x+1].y);
169                 bb->y2[yy] = (p1[x+8].y + p1[x+8+1].y);
170                 bb->y3[yy] = (p1[linex*8+x].y + p1[linex*8+x+1].y);
171                 bb->y4[yy] = (p1[linex*8+x+8].y + p1[linex*8+x+8+1].y);
172                 yy++;
173             }
174             yy-=8;
175             p1+=linex;
176             if(yhp==3) {
177                 for(x=0;x<8;x++) {
178                     bb->y1[yy] += (p1[x].y + p1[x+1].y); bb->y1[yy]/=4;
179                     bb->y2[yy] += (p1[x+8].y + p1[x+8+1].y); bb->y2[yy]/=4;
180                     bb->y3[yy] += (p1[linex*8+x].y + p1[linex*8+x+1].y); bb->y3[yy]/=4;
181                     bb->y4[yy] += (p1[linex*8+x+8].y + p1[linex*8+x+8+1].y); bb->y4[yy]/=4;
182                     yy++;
183                 }
184             } else {
185                 for(x=0;x<8;x++) {
186                     bb->y1[yy]/=2; bb->y2[yy]/=2; bb->y3[yy]/=2; bb->y4[yy]/=2;
187                     yy++;
188                 }
189             }
190         }
191     }
192
193     /* u,v */
194     if(uvhp==0 || uvhp==2) {
195         for(y=0;y<8;y++) {
196             for(x=0;x<8;x++) {
197                 bb->u[uv] = (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
198                 bb->v[uv] = (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
199                 uv++;
200             }
201             p2+=linex*2;
202             if(uvhp==2) {
203                 uv-=8;
204                 for(x=0;x<8;x++) {
205                     bb->u[uv] += (p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4;
206                     bb->v[uv] += (p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4;
207                     bb->u[uv] /= 2;
208                     bb->v[uv] /= 2;
209                     uv++;
210                 }
211             }
212         }
213     } else /* uvhp==1 || uvhp==3 */ {
214         for(y=0;y<8;y++) {
215             for(x=0;x<8;x++) {
216                 bb->u[uv] = ((p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4+
217                              (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);
218                 bb->v[uv] = ((p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4+
219                              (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);
220                 uv++;
221             }
222             uv-=8;
223             p2+=linex*2;
224             if(uvhp==3) {
225                 for(x=0;x<8;x++) {
226                     bb->u[uv] += ((p2[x*2].u + p2[x*2+1].u + p2[linex+x*2].u + p2[linex+x*2+1].u)/4+
227                                   (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);
228                     bb->v[uv] += ((p2[x*2].v + p2[x*2+1].v + p2[linex+x*2].v + p2[linex+x*2+1].v)/4+
229                                   (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);
230                     bb->u[uv] /= 4;
231                     bb->v[uv] /= 4;
232                     uv++;
233                 }
234             } else {
235                 for(x=0;x<8;x++) {
236                     bb->u[uv] /= 2;
237                     bb->v[uv] /= 2;
238                     uv++;
239                 }
240             }
241         }
242     }
243 }
244
245 static void rgb2yuv(YUV*dest, RGBA*src, int dlinex, int slinex, int width, int height)
246 {
247     int x,y;
248     for(y=0;y<height;y++) {
249         for(x=0;x<width;x++) {
250             int r,g,b;
251             r = src[y*slinex+x].r;
252             g = src[y*slinex+x].g;
253             b = src[y*slinex+x].b;
254             /*dest[y*dlinex+x].y = (r*0.299 + g*0.587 + b*0.114);
255             dest[y*dlinex+x].u = (r*-0.169 + g*-0.332 + b*0.500 + 128.0);
256             dest[y*dlinex+x].v = (r*0.500 + g*-0.419 + b*-0.0813 + 128.0);*/
257
258             //dest[y*dlinex+x].y = 128;//(r*((int)( 0.299*256)) + g*((int)( 0.587*256)) + b*((int)( 0.114 *256)))>>8;
259
260             dest[y*dlinex+x].y = (r*((int)( 0.299*256)) + g*((int)( 0.587*256)) + b*((int)( 0.114 *256)))>>8;
261             dest[y*dlinex+x].u = (r*((int)(-0.169*256)) + g*((int)(-0.332*256)) + b*((int)( 0.500 *256))+ 128*256)>>8;
262             dest[y*dlinex+x].v = (r*((int)( 0.500*256)) + g*((int)(-0.419*256)) + b*((int)(-0.0813*256))+ 128*256)>>8;
263         }
264     }
265 }
266
267 static void copyregion(VIDEOSTREAM*s, YUV*dest, YUV*src, int bx, int by)
268 {
269     YUV*p1 = &dest[by*s->linex*16+bx*16];
270     YUV*p2 = &src[by*s->linex*16+bx*16];
271     int y;
272     for(y=0;y<16;y++) {
273         memcpy(p1, p2, 16*sizeof(YUV));
274         p1+=s->linex;p2+=s->linex;
275     }
276 }
277
278 static void yuv2rgb(RGBA*dest, YUV*src, int linex, int width, int height)
279 {
280     int x,y;
281     for(y=0;y<height;y++) {
282         for(x=0;x<width;x++) {
283             int u,v,yy;
284             u = src[y*linex+x].u;
285             v = src[y*linex+x].v;
286             yy = src[y*linex+x].y;
287             dest[y*linex+x].r = truncate256(yy + ((360*(v-128))>>8));
288             dest[y*linex+x].g = truncate256(yy - ((88*(u-128)+183*(v-128))>>8));
289             dest[y*linex+x].b = truncate256(yy + ((455 * (u-128))>>8));
290         }
291     }
292 }
293 static void copy_block_pic(VIDEOSTREAM*s, YUV*dest, block_t*b, int bx, int by)
294 {
295     YUV*p1 = &dest[(by*16)*s->linex+bx*16];
296     YUV*p2 = &dest[(by*16+8)*s->linex+bx*16];
297     int x,y;
298     for(y=0;y<8;y++) {
299         for(x=0;x<8;x++) {
300             int u,v,yy;
301             p1[x+0].u = b->u[(y/2)*8+(x/2)];
302             p1[x+0].v = b->v[(y/2)*8+(x/2)];
303             p1[x+0].y = b->y1[y*8+x];
304             p1[x+8].u = b->u[(y/2)*8+(x/2)+4];
305             p1[x+8].v = b->v[(y/2)*8+(x/2)+4];
306             p1[x+8].y = b->y2[y*8+x];
307             p2[x+0].u = b->u[(y/2+4)*8+(x/2)];
308             p2[x+0].v = b->v[(y/2+4)*8+(x/2)];
309             p2[x+0].y = b->y3[y*8+x];
310             p2[x+8].u = b->u[(y/2+4)*8+(x/2)+4];
311             p2[x+8].v = b->v[(y/2+4)*8+(x/2)+4];
312             p2[x+8].y = b->y4[y*8+x];
313         }
314         p1+=s->linex;
315         p2+=s->linex;
316     }
317 }
318
319 static int compare_pic_pic(VIDEOSTREAM*s, YUV*pp1, YUV*pp2, int bx, int by)
320 {
321     int linex = s->width;
322     YUV*p1 = &pp1[by*linex*16+bx*16];
323     YUV*p2 = &pp2[by*linex*16+bx*16];
324     int diffy=0, diffuv = 0;
325     int x,y;
326     for(y=0;y<16;y++) {
327         for(x=0;x<16;x++) {
328             YUV*m = &p1[x];
329             YUV*n = &p2[x];
330             int y = m->y - n->y;
331             int u = m->u - n->u;
332             int v = m->v - n->v;
333             diffy += abs(y);
334             diffuv += abs(u)+abs(v);
335         }
336         p1+=linex;
337         p2+=linex;
338     }
339     return diffy + diffuv/4;
340 }
341
342 static int compare_pic_block(VIDEOSTREAM*s, block_t* b, YUV*pic, int bx, int by)
343 {
344     int linex = s->width;
345     YUV*y1 = &pic[(by*2)*linex*8+bx*16];
346     YUV*y2 = &pic[(by*2)*linex*8+bx*16+8];
347     YUV*y3 = &pic[(by*2+1)*linex*8+bx*16];
348     YUV*y4 = &pic[(by*2+1)*linex*8+bx*16+8];
349     YUV*uv1 = y1;
350     YUV*uv2 = &y1[linex];
351     int diffy=0, diffuv = 0;
352     int x,y;
353     for(y=0;y<8;y++) {
354         for(x=0;x<8;x++) {
355             int yy,u1,v1,u2,v2,u3,v3,u4,v4;
356             int y8x = y*8+x;
357             yy = y1[x].y - b->y1[y8x];
358             diffy += abs(yy);
359             yy = y2[x].y - b->y2[y8x];
360             diffy += abs(yy);
361             yy = y3[x].y - b->y3[y8x];
362             diffy += abs(yy);
363             yy = y4[x].y - b->y4[y8x];
364             diffy += abs(yy);
365             u1 = uv1[x*2].u - b->u[y8x];
366             v1 = uv1[x*2].v - b->v[y8x];
367             u2 = uv1[x*2+1].u - b->u[y8x];
368             v2 = uv1[x*2+1].v - b->v[y8x];
369             u3 = uv2[x*2].u - b->u[y8x];
370             v3 = uv2[x*2].v - b->v[y8x];
371             u4 = uv2[x*2+1].u - b->u[y8x];
372             v4 = uv2[x*2+1].v - b->v[y8x];
373             diffuv += (abs(u1)+abs(v1));
374             diffuv += (abs(u2)+abs(v2));
375             diffuv += (abs(u3)+abs(v3));
376             diffuv += (abs(u4)+abs(v4));
377         }
378         y1+=linex;
379         y2+=linex;
380         y3+=linex;
381         y4+=linex;
382         uv1+=linex*2;
383         uv2+=linex*2;
384     }
385     return diffy + diffuv/4;
386 }
387
388 static inline int valtodc(int val)
389 {
390     assert(val>=0);
391
392     /* table 12/h.263 */
393
394     //val+=4; //round
395     val/=8;
396     /* TODO: what to do for zero values? skip the block? */
397     if(val==0)
398         return 1;
399     if(val==128)
400         return 255;
401     if(val>254)
402         return 254;
403     return val;
404 }
405 static int dctoval(int dc)
406 {
407     int val;
408     assert(dc>0);
409     assert(dc!=128);
410     assert(dc<256);
411     /* table 12/h.263 */
412     val = dc*8;
413     if(val == 255*8)
414         val = 128*8;
415     return val;
416 }
417
418 /* TODO: we could also just let the caller pass only the string table[index] here */
419 static int codehuffman(TAG*tag, struct huffcode*table, int index)
420 {
421     /* TODO: !optimize! */
422     int i=0;
423     while(table[index].code[i]) {
424         if(table[index].code[i]=='0')
425             swf_SetBits(tag, 0, 1);
426         else
427             swf_SetBits(tag, 1, 1);
428         i++;
429     }
430     return i;
431 }
432
433 static void quantize8x8(int*src, int*dest, int has_dc, int quant)
434 {
435     int t,pos=0;
436     double q = 1.0/(quant*2);
437     if(has_dc) {
438         dest[0] = valtodc((int)src[0]); /*DC*/
439         pos++;
440     }
441     for(t=pos;t<64;t++)
442     {
443         //dest[t] = (int)src[t];
444     /* exact: if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;} */
445         //if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;}
446         //dest[t] = dest[t]/(quant*2);
447         dest[t] = (int)(src[t]*q);
448         /* TODO: warn if this happens- the video will be buggy */
449         if(dest[t]>127) dest[t]=127;
450         if(dest[t]<-127) dest[t]=-127;
451     }
452 }
453
454 static void dequantize8x8(int*b, int has_dc, int quant)
455 {
456     int t,pos=0;
457     if(has_dc) {
458         b[0] = dctoval(b[0]); //DC
459         pos++;
460     }
461     for(t=pos;t<64;t++) {
462         if(b[t]) {
463             int sign = 0;
464             if(b[t]<0) {
465                 b[t] = -b[t];
466                 sign = 1;
467             }
468
469             if(quant&1) {
470                 b[t] = quant*(2*b[t]+1); //-7,8,24,40
471             } else {
472                 b[t] = quant*(2*b[t]+1)-1; //-8,7,23,39
473             }
474
475             if(sign)
476                 b[t] = -b[t];
477         }
478
479         /* paragraph 6.2.2, "clipping of reconstruction levels": */
480         if(b[t]>2047) b[t]=2047;
481         if(b[t]<-2048) b[t]=-2048;
482     }
483 }
484
485 static int hascoef(int*b, int has_dc)
486 {
487     int t;
488     int pos=0;
489     if(has_dc)
490         pos++;
491     for(t=pos;t<64;t++) {
492         if(b[t])
493             return 1;
494     }
495     return 0;
496 }
497
498 static int coefbits8x8(int*bb, int has_dc)
499 {
500     int t;
501     int pos=0;
502     int bits=0;
503     int last;
504
505     if(has_dc) {
506         bits+=8;
507         pos++;
508     }
509     for(last=63;last>=pos;last--) {
510         if(bb[last])
511             break;
512     }
513     if(last < pos)
514         return bits;
515     while(1) {
516         int run=0, level=0, islast=0,t;
517         while(!bb[pos] && pos<last) {
518             pos++;
519             run++;
520         }
521         if(pos==last)
522             islast=1;
523         level=bb[pos];
524         if(level<0) level=-level;
525         assert(level);
526         for(t=0;t<RLE_ESCAPE;t++) {
527             if(rle_params[t].run == run &&
528                rle_params[t].level == level &&
529                rle_params[t].last == islast) {
530                 bits += rle[t].len + 1;
531                 break;
532             }
533         }
534         if(t==RLE_ESCAPE) {
535             bits += rle[RLE_ESCAPE].len + 1 + 6 + 8;
536         }
537         if(islast)
538             break;
539         pos++;
540     }
541     return bits;
542 }
543
544 static int encode8x8(TAG*tag, int*bb, int has_dc, int has_tcoef)
545 {
546     int t;
547     int pos=0;
548     int bits=0;
549
550     if(has_dc) {
551         swf_SetBits(tag, bb[0], 8);
552         bits += 8;
553         pos++;
554     }
555
556     if(has_tcoef) {
557         int last;
558         /* determine last non-null coefficient */
559         for(last=63;last>=pos;last--) {
560             /* TODO: we could leave out small coefficients
561                      after a certain point (32?) */
562             if(bb[last])
563                 break;
564         }
565         /* blocks without coefficients should not be included
566            in the cbpy/cbpc patterns: */
567         assert(bb[last]);
568
569         while(1) {
570             int run=0;
571             int level=0;
572             int islast=0;
573             int sign=0;
574             int t;
575             while(!bb[pos] && pos<last) {
576                 pos++;
577                 run++;
578             }
579             if(pos==last)
580                 islast=1;
581             level=bb[pos];
582             assert(level);
583             if(level<0) {
584                 level = -level;
585                 sign = 1;
586             }
587             for(t=0;t<RLE_ESCAPE;t++) {
588                 /* TODO: lookup table */
589                 if(rle_params[t].run == run &&
590                    rle_params[t].level == level &&
591                    rle_params[t].last == islast) {
592                     bits += codehuffman(tag, rle, t);
593                     swf_SetBits(tag, sign, 1);
594                     bits += 1;
595                     break;
596                 }
597             }
598             if(t==RLE_ESCAPE) {
599                 bits += codehuffman(tag, rle, RLE_ESCAPE);
600                 level=bb[pos];
601                 /* table 14/h.263 */
602                 if(!level || level<-127 || level>127) {
603                     fprintf(stderr, "Warning: Overflow- Level %d at pos %d\n", level, pos);
604                     if(level<-127) level=-127;
605                     if(level>127) level=127;
606                 }
607
608                 assert(level);
609                 assert(level>=-127);
610                 assert(level<=127); //TODO: known to fail for pos=0 (with custom frames?)
611
612                 swf_SetBits(tag, islast, 1);
613                 swf_SetBits(tag, run, 6);
614                 swf_SetBits(tag, level, 8); //FIXME: fixme??
615                 bits += 1 + 6 + 8;
616             }
617
618             if(islast)
619                 break;
620             pos++;
621         }
622     }
623     return bits;
624 }
625
626 static void quantize(block_t*fb, block_t*b, int has_dc, int quant)
627 {
628     quantize8x8(fb->y1, b->y1, has_dc, quant);
629     quantize8x8(fb->y2, b->y2, has_dc, quant);
630     quantize8x8(fb->y3, b->y3, has_dc, quant);
631     quantize8x8(fb->y4, b->y4, has_dc, quant);
632     quantize8x8(fb->u, b->u, has_dc, quant);
633     quantize8x8(fb->v, b->v, has_dc, quant);
634 }
635
636 static void dodct(block_t*fb)
637 {
638     dct(fb->y1); dct(fb->y2); dct(fb->y3); dct(fb->y4);
639     dct(fb->u);  dct(fb->v);
640     zigzag(fb->y1);
641     zigzag(fb->y2);
642     zigzag(fb->y3);
643     zigzag(fb->y4);
644     zigzag(fb->u);
645     zigzag(fb->v);
646 }
647
648 static void dodctandquant(block_t*fb, block_t*b, int has_dc, int quant)
649 {
650     int t;
651     if(has_dc) {
652         dodct(fb);
653         quantize(fb,b,has_dc,quant);
654         return;
655     }
656     preparequant(quant);
657     dct2(fb->y1,b->y1); dct2(fb->y2,b->y2); dct2(fb->y3,b->y3); dct2(fb->y4,b->y4);
658     dct2(fb->u,b->u);  dct2(fb->v,b->v);
659
660     for(t=0;t<64;t++) {
661         /* prepare for encoding (only values in (-127..-1,1..127) are
662            allowed as non-zero, non-dc values */
663         if(b->y1[t]<-127) b->y1[t]=-127;
664         if(b->y2[t]<-127) b->y2[t]=-127;
665         if(b->y3[t]<-127) b->y3[t]=-127;
666         if(b->y4[t]<-127) b->y4[t]=-127;
667         if(b->u[t]<-127) b->u[t]=-127;
668         if(b->v[t]<-127) b->v[t]=-127;
669
670         if(b->y1[t]>127) b->y1[t]=127;
671         if(b->y2[t]>127) b->y2[t]=127;
672         if(b->y3[t]>127) b->y3[t]=127;
673         if(b->y4[t]>127) b->y4[t]=127;
674         if(b->u[t]>127) b->u[t]=127;
675         if(b->v[t]>127) b->v[t]=127;
676     }
677 }
678
679 static void doidct(block_t*b)
680 {
681     block_t fb;
682     int t;
683     for(t=0;t<64;t++) {
684         fb.y1[t] = b->y1[zigzagtable[t]];
685         fb.y2[t] = b->y2[zigzagtable[t]];
686         fb.y3[t] = b->y3[zigzagtable[t]];
687         fb.y4[t] = b->y4[zigzagtable[t]];
688         fb.u[t] = b->u[zigzagtable[t]];
689         fb.v[t] = b->v[zigzagtable[t]];
690     }
691     idct(fb.y1); idct(fb.y2); idct(fb.y3); idct(fb.y4);
692     idct(fb.u);  idct(fb.v);
693
694     memcpy(b, &fb, sizeof(block_t));
695 }
696
697 static void truncateblock(block_t*b)
698 {
699     int t;
700     for(t=0;t<64;t++) {
701         b->y1[t] = truncate256(b->y1[t]);
702         b->y2[t] = truncate256(b->y2[t]);
703         b->y3[t] = truncate256(b->y3[t]);
704         b->y4[t] = truncate256(b->y4[t]);
705         b->u[t] = truncate256(b->u[t]);
706         b->v[t] = truncate256(b->v[t]);
707     }
708 }
709
710 static void dequantize(block_t*b, int has_dc, int quant)
711 {
712     dequantize8x8(b->y1, has_dc, quant);
713     dequantize8x8(b->y2, has_dc, quant);
714     dequantize8x8(b->y3, has_dc, quant);
715     dequantize8x8(b->y4, has_dc, quant);
716     dequantize8x8(b->u, has_dc, quant);
717     dequantize8x8(b->v, has_dc, quant);
718 }
719
720 static void getblockpatterns(block_t*b, int*cbpybits,int*cbpcbits, int has_dc)
721 {
722     *cbpybits = 0;
723     *cbpcbits = 0;
724
725     *cbpybits|=hascoef(b->y1, has_dc)*8;
726     *cbpybits|=hascoef(b->y2, has_dc)*4;
727     *cbpybits|=hascoef(b->y3, has_dc)*2;
728     *cbpybits|=hascoef(b->y4, has_dc)*1;
729
730     *cbpcbits|=hascoef(b->u, has_dc)*2;
731     *cbpcbits|=hascoef(b->v, has_dc)*1;
732 }
733
734 static void setQuant(TAG*tag, int dquant)
735 {
736     int code = 0;
737     /* 00 01 10 11
738        -1 -2 +1 +2
739     */
740     if(dquant == -1) {
741         swf_SetBits(tag, 0x0, 2);
742     } else if(dquant == -2) {
743         swf_SetBits(tag, 0x1, 2);
744     } else if(dquant == +1) {
745         swf_SetBits(tag, 0x2, 2);
746     } else if(dquant == +2) {
747         swf_SetBits(tag, 0x3, 2);
748     } else {
749         assert(0*strlen("invalid dquant"));
750     }
751 }
752
753 static void change_quant(int quant, int*dquant)
754 {
755     /* TODO */
756     *dquant = 0;
757 }
758
759 static void yuvdiff(block_t*a, block_t*b)
760 {
761     int t;
762     for(t=0;t<64;t++) {
763         a->y1[t] = (a->y1[t] - b->y1[t]);
764         a->y2[t] = (a->y2[t] - b->y2[t]);
765         a->y3[t] = (a->y3[t] - b->y3[t]);
766         a->y4[t] = (a->y4[t] - b->y4[t]);
767         a->u[t]  = (a->u[t] - b->u[t]);
768         a->v[t]  = (a->v[t] - b->v[t]);
769     }
770 }
771
772 static void predictmvd(VIDEOSTREAM*s, int bx, int by, int*px, int*py)
773 {
774     int i1,i2;
775     int x1,y1,x2,y2,x3,y3;
776     int x4,y4,p;
777     if(bx) {x1=s->mvdx[by*s->bbx+bx-1];
778             y1=s->mvdy[by*s->bbx+bx-1];
779     } else {x1=y1=0;}
780
781     if(by) {x2=s->mvdx[(by-1)*s->bbx+bx];
782             y2=s->mvdy[(by-1)*s->bbx+bx];
783             if(bx<s->bbx-1) {
784                 x3=s->mvdx[(by-1)*s->bbx+bx+1];
785                 y3=s->mvdy[(by-1)*s->bbx+bx+1];
786             } else {
787                 x3=y3=0;
788             }
789            }
790     else   {x2=x3=x1;y2=y3=y1;}
791
792            if((x1 <= x2 && x2 <= x3) ||
793               (x3 <= x2 && x2 <= x1)) {
794         x4=x2;
795     } else if((x2 <= x1 && x1 <= x3) ||
796               (x3 <= x1 && x1 <= x2)) {
797         x4=x1;
798     } else if((x1 <= x3 && x3 <= x2) ||
799               (x2 <= x3 && x3 <= x1)) {
800         x4=x3;
801     } else {
802         x4=0;
803         assert(x4);
804     }
805
806            if((y1 <= y2 && y2 <= y3) ||
807               (y3 <= y2 && y2 <= y1)) {
808         y4=y2;
809     } else if((y2 <= y1 && y1 <= y3) ||
810               (y3 <= y1 && y1 <= y2)) {
811         y4=y1;
812     } else if((y1 <= y3 && y3 <= y2) ||
813               (y2 <= y3 && y3 <= y1)) {
814         y4=y3;
815     } else {
816         y4=0;
817         assert(y4);
818     }
819
820     *px = x4;
821     *py = y4;
822     assert((x4>=-32 && x4<=31) && (y4>=-32 && y4<=31));
823 }
824
825 static inline int mvd2index(int px, int py, int x, int y, int xy)
826 {
827
828     if((x<-32 && x>31) || (y<-32 && y>31)) 
829         fprintf(stderr, "(%d,%d)\n", x,y);
830     assert((x>=-32 && x<=31) && (y>=-32 && y<=31));
831     //assert((x&1)==0 && (y&1)==0);//for now
832     //assert((x&2)==0 && (y&2)==0);//for now(2)
833
834     x-=px;
835     y-=py;
836
837     if(xy)
838         x=y;
839     x+=32;
840
841     /* (x&63) */
842     if(x>63)
843         x-=64;
844     if(x<0)
845         x+=64;
846
847     assert(x>=0 && x<64);
848     return x;
849 }
850
851 typedef struct _iblockdata_t
852 {
853     block_t b; //transformed quantized coefficients
854     block_t reconstruction;
855     int bits;
856     int bx,by;
857     struct huffcode*ctable; //table to use for chrominance encoding (different for i-frames)
858     int iframe; // 1 if this is part of an iframe
859 } iblockdata_t;
860
861 typedef struct _mvdblockdata_t
862 {
863     block_t b;
864     block_t fbold;
865     block_t reconstruction;
866     int xindex;
867     int yindex;
868     int movex;
869     int movey;
870     int bits;
871     int bx,by;
872 } mvdblockdata_t;
873
874 void prepareIBlock(VIDEOSTREAM*s, iblockdata_t*data, int bx, int by, block_t* fb, int*bits, int iframe)
875 {
876     /* consider I-block */
877     block_t fb_i;
878     block_t b;
879     int y,c;
880     struct huffcode*ctable;
881
882     data->bx = bx;
883     data->by = by;
884
885     data->iframe = iframe;
886     if(!iframe) {
887         data->ctable = &mcbpc_inter[3*4];
888     } else {
889         data->ctable = &mcbpc_intra[0];
890     }
891
892     memcpy(&fb_i, fb, sizeof(block_t));
893     dodctandquant(&fb_i, &data->b, 1, s->quant);
894     getblockpatterns(&data->b, &y, &c, 1);
895     *bits = 0;
896     if(!data->iframe) {
897         *bits += 1; //cod
898     }
899     *bits += data->ctable[c].len;
900     *bits += cbpy[y].len;
901     *bits += coefbits8x8(data->b.y1, 1);
902     *bits += coefbits8x8(data->b.y2, 1);
903     *bits += coefbits8x8(data->b.y3, 1);
904     *bits += coefbits8x8(data->b.y4, 1);
905     *bits += coefbits8x8(data->b.u, 1);
906     *bits += coefbits8x8(data->b.v, 1);
907     data->bits = *bits;
908     
909     /* -- reconstruction -- */
910     memcpy(&data->reconstruction,&data->b,sizeof(block_t));
911     dequantize(&data->reconstruction, 1, s->quant);
912     doidct(&data->reconstruction);
913     truncateblock(&data->reconstruction);
914 }
915
916 int writeIBlock(VIDEOSTREAM*s, TAG*tag, iblockdata_t*data)
917 {
918     int c = 0, y = 0;
919     int has_dc=1;
920     int bits = 0;
921     block_t b;
922
923     getblockpatterns(&data->b, &y, &c, has_dc);
924     if(!data->iframe) {
925         swf_SetBits(tag,0,1); bits += 1; // COD
926     }
927     bits += codehuffman(tag, data->ctable, c);
928     bits += codehuffman(tag, cbpy, y);
929
930     /* luminance */
931     bits += encode8x8(tag, data->b.y1, has_dc, y&8);
932     bits += encode8x8(tag, data->b.y2, has_dc, y&4);
933     bits += encode8x8(tag, data->b.y3, has_dc, y&2);
934     bits += encode8x8(tag, data->b.y4, has_dc, y&1);
935
936     /* chrominance */
937     bits += encode8x8(tag, data->b.u, has_dc, c&2);
938     bits += encode8x8(tag, data->b.v, has_dc, c&1);
939
940     copy_block_pic(s, s->current, &data->reconstruction, data->bx, data->by);
941     assert(data->bits == bits);
942     return bits;
943 }
944
945 int getmvdbits(VIDEOSTREAM*s,block_t*fb, int bx,int by,int hx,int hy)
946 {
947     block_t b;
948     block_t fbold;
949     block_t fbdiff;
950     int bits = 0;
951     memcpy(&fbdiff, fb, sizeof(block_t));
952     getmvdregion(&fbold, s->oldpic, bx, by, hx, hy, s->linex);
953     yuvdiff(&fbdiff, &fbold);
954     dodctandquant(&fbdiff, &b, 0, s->quant);
955     bits += coefbits8x8(b.y1, 0);
956     bits += coefbits8x8(b.y2, 0);
957     bits += coefbits8x8(b.y3, 0);
958     bits += coefbits8x8(b.y4, 0);
959     bits += coefbits8x8(b.u, 0);
960     bits += coefbits8x8(b.v, 0);
961     return bits;
962 }
963
964 void prepareMVDBlock(VIDEOSTREAM*s, mvdblockdata_t*data, int bx, int by, block_t* fb, int*bits)
965 { /* consider mvd(x,y)-block */
966
967     int t;
968     int y,c;
969     block_t fbdiff;
970     int predictmvdx;
971     int predictmvdy;
972
973     data->bx = bx;
974     data->by = by;
975     predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
976
977     data->bits = 65535;
978     data->movex=0;
979     data->movey=0;
980
981     if(s->do_motion) {
982         int hx,hy;
983         int bestx=0,besty=0,bestbits=65536;
984         int startx=-32,endx=31;
985         int starty=-32,endy=31;
986
987         if(!bx) startx=0;
988         if(!by) starty=0;
989         if(bx==s->bbx-1) endx=0;
990         if(by==s->bby-1) endy=0;
991
992         for(hx=startx;hx<=endx;hx+=4)
993         for(hy=starty;hy<=endy;hy+=4)
994         {
995             int bits = 0;
996             bits = getmvdbits(s,fb,bx,by,hx,hy);
997             if(bits<bestbits) {
998                 bestbits = bits;
999                 bestx = hx;
1000                 besty = hy;
1001             }
1002         }
1003         
1004         if(bestx-3 > startx) startx = bestx-3;
1005         if(besty-3 > starty) starty = besty-3;
1006         if(bestx+3 < endx) endx = bestx+3;
1007         if(besty+3 < endy) endy = besty+3;
1008
1009         for(hx=startx;hx<=endx;hx++)
1010         for(hy=starty;hy<=endy;hy++)
1011         {
1012             int bits = 0;
1013             bits = getmvdbits(s,fb,bx,by,hx,hy);
1014             if(bits<bestbits) {
1015                 bestbits = bits;
1016                 bestx = hx;
1017                 besty = hy;
1018             }
1019         }
1020         data->movex = bestx;
1021         data->movey = besty;
1022     }
1023
1024     memcpy(&fbdiff, fb, sizeof(block_t));
1025     getmvdregion(&data->fbold, s->oldpic, bx, by, data->movex, data->movey, s->linex);
1026     yuvdiff(&fbdiff, &data->fbold);
1027     dodctandquant(&fbdiff, &data->b, 0, s->quant);
1028     getblockpatterns(&data->b, &y, &c, 0);
1029
1030     data->xindex = mvd2index(predictmvdx, predictmvdy, data->movex, data->movey, 0);
1031     data->yindex = mvd2index(predictmvdx, predictmvdy, data->movex, data->movey, 1);
1032
1033     *bits = 1; //cod
1034     *bits += mcbpc_inter[0*4+c].len;
1035     *bits += cbpy[y^15].len;
1036     *bits += mvd[data->xindex].len; // (0,0)
1037     *bits += mvd[data->yindex].len;
1038     *bits += coefbits8x8(data->b.y1, 0);
1039     *bits += coefbits8x8(data->b.y2, 0);
1040     *bits += coefbits8x8(data->b.y3, 0);
1041     *bits += coefbits8x8(data->b.y4, 0);
1042     *bits += coefbits8x8(data->b.u, 0);
1043     *bits += coefbits8x8(data->b.v, 0);
1044     data->bits = *bits;
1045
1046     /* -- reconstruction -- */
1047     memcpy(&data->reconstruction, &data->b, sizeof(block_t));
1048     dequantize(&data->reconstruction, 0, s->quant);
1049     doidct(&data->reconstruction);
1050     for(t=0;t<64;t++) {
1051         data->reconstruction.y1[t] = 
1052             truncate256(data->reconstruction.y1[t] + (int)data->fbold.y1[t]);
1053         data->reconstruction.y2[t] = 
1054             truncate256(data->reconstruction.y2[t] + (int)data->fbold.y2[t]);
1055         data->reconstruction.y3[t] = 
1056             truncate256(data->reconstruction.y3[t] + (int)data->fbold.y3[t]);
1057         data->reconstruction.y4[t] = 
1058             truncate256(data->reconstruction.y4[t] + (int)data->fbold.y4[t]);
1059         data->reconstruction.u[t] = 
1060             truncate256(data->reconstruction.u[t] + (int)data->fbold.u[t]);
1061         data->reconstruction.v[t] = 
1062             truncate256(data->reconstruction.v[t] + (int)data->fbold.v[t]);
1063     }
1064 }
1065
1066 int writeMVDBlock(VIDEOSTREAM*s, TAG*tag, mvdblockdata_t*data)
1067 {
1068     int c = 0, y = 0;
1069     int t;
1070     int has_dc=0; // mvd w/o mvd24
1071     /* mvd (0,0) block (mode=0) */
1072     int mode = 0;
1073     int bx = data->bx;
1074     int by = data->by;
1075     int bits = 0;
1076
1077     getblockpatterns(&data->b, &y, &c, has_dc);
1078     swf_SetBits(tag,0,1); bits += 1; // COD
1079     bits += codehuffman(tag, mcbpc_inter, mode*4+c);
1080     bits += codehuffman(tag, cbpy, y^15);
1081
1082     /* vector */
1083     bits += codehuffman(tag, mvd, data->xindex);
1084     bits += codehuffman(tag, mvd, data->yindex);
1085
1086     /* luminance */
1087     bits += encode8x8(tag, data->b.y1, has_dc, y&8);
1088     bits += encode8x8(tag, data->b.y2, has_dc, y&4);
1089     bits += encode8x8(tag, data->b.y3, has_dc, y&2);
1090     bits += encode8x8(tag, data->b.y4, has_dc, y&1);
1091
1092     /* chrominance */
1093     bits += encode8x8(tag, data->b.u, has_dc, c&2);
1094     bits += encode8x8(tag, data->b.v, has_dc, c&1);
1095
1096     s->mvdx[by*s->bbx+bx] = data->movex;
1097     s->mvdy[by*s->bbx+bx] = data->movey;
1098
1099     copy_block_pic(s, s->current, &data->reconstruction, data->bx, data->by);
1100     assert(data->bits == bits);
1101     return bits;
1102 }
1103
1104 static int encode_PFrame_block(TAG*tag, VIDEOSTREAM*s, int bx, int by)
1105 {
1106     block_t fb;
1107     int diff1,diff2;
1108     int bits_i;
1109     int bits_vxy;
1110
1111     iblockdata_t iblock;
1112     mvdblockdata_t mvdblock;
1113     
1114     getregion(&fb, s->current, bx, by, s->linex);
1115     prepareIBlock(s, &iblock, bx, by, &fb, &bits_i, 0);
1116
1117     /* encoded last frame <=> original current block: */
1118     diff1 = compare_pic_pic(s, s->current, s->oldpic, bx, by);
1119     /* encoded current frame <=> original current block: */
1120     diff2 = compare_pic_block(s, &iblock.reconstruction, s->current, bx, by);
1121
1122     if(diff1 <= diff2) {
1123         swf_SetBits(tag, 1,1); /* cod=1, block skipped */
1124         /* copy the region from the last frame so that we have a complete reconstruction */
1125         copyregion(s, s->current, s->oldpic, bx, by);
1126         return 1;
1127     }
1128     prepareMVDBlock(s, &mvdblock, bx, by, &fb, &bits_vxy);
1129
1130     if(bits_i > bits_vxy) {
1131         return writeMVDBlock(s, tag, &mvdblock);
1132     } else {
1133         return writeIBlock(s, tag, &iblock);
1134     }
1135 }
1136
1137 /* should be called encode_IFrameBlock */
1138 static void encode_IFrame_block(TAG*tag, VIDEOSTREAM*s, int bx, int by)
1139 {
1140     block_t fb;
1141     iblockdata_t data;
1142     int bits;
1143
1144     getregion(&fb, s->current, bx, by, s->width);
1145     prepareIBlock(s, &data, bx, by, &fb, &bits, 1);
1146     writeIBlock(s, tag, &data);
1147 }
1148
1149 #ifdef MAIN
1150 static int bmid = 0;
1151
1152 void setdbgpic(TAG*tag, RGBA*pic, int width, int height)
1153 {
1154     MATRIX m;
1155     tag = tag->prev;
1156
1157     tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
1158     swf_SetU16(tag, 133);
1159
1160     tag = swf_InsertTag(tag, ST_DEFINEBITSLOSSLESS);
1161     swf_SetU16(tag, 1000+bmid);
1162     swf_SetLosslessBits(tag, width, height, (void*)pic, BMF_32BIT);
1163
1164     tag = swf_InsertTag(tag, ST_DEFINESHAPE);
1165     swf_SetU16(tag, 2000+bmid);
1166     swf_ShapeSetBitmapRect(tag, 1000+bmid, width, height);
1167
1168     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1169     swf_GetMatrix(0,&m);
1170     m.tx = width*20;
1171     swf_ObjectPlace(tag, 2000+bmid, 133, &m, 0, 0);
1172
1173     bmid++;
1174 }
1175 #endif
1176
1177 #define TYPE_IFRAME 0
1178 #define TYPE_PFRAME 1
1179
1180 static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
1181 {
1182     U32 i32;
1183     swf_SetU16(tag, frame);
1184     swf_SetBits(tag, 1, 17); /* picture start code*/
1185     swf_SetBits(tag, 0, 5); /* version=0, version 1 would optimize rle behaviour*/
1186     swf_SetBits(tag, frame, 8); /* time reference */
1187
1188     /* write dimensions, taking advantage of some predefined sizes
1189        if the opportunity presents itself */
1190     i32 = width<<16|height;
1191     switch(i32)
1192     {
1193         case 352<<16|288: swf_SetBits(tag, 2, 3);break;
1194         case 176<<16|144: swf_SetBits(tag, 3, 3);break;
1195         case 128<<16|96: swf_SetBits(tag, 4, 3);break;
1196         case 320<<16|240: swf_SetBits(tag, 5, 3);break;
1197         case 160<<16|120: swf_SetBits(tag, 6, 3);break;
1198         default:
1199             if(width>255 || height>255) {
1200                 swf_SetBits(tag, 1, 3);
1201                 swf_SetBits(tag, width, 16);
1202                 swf_SetBits(tag, height, 16);
1203             } else {
1204                 swf_SetBits(tag, 0, 3);
1205                 swf_SetBits(tag, width, 8);
1206                 swf_SetBits(tag, height, 8);
1207             }
1208     }
1209
1210     swf_SetBits(tag, type, 2); /* I-Frame or P-Frame */
1211     swf_SetBits(tag, 0, 1); /* No deblock filter */
1212     assert(quant>0);
1213     assert(quant<32);
1214     swf_SetBits(tag, quant, 5); /* quantizer (1-31), may be updated later on*/
1215     swf_SetBits(tag, 0, 1); /* No extra info */
1216 }
1217
1218 void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1219 {
1220     int bx, by;
1221
1222     if(quant<1) quant=1;
1223     if(quant>31) quant=31;
1224     s->quant = quant;
1225
1226     writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_IFRAME);
1227
1228     /* fixme: should fill with 0,128,128, not 0,0,0 */
1229     memset(s->current, 0, s->linex*s->height*sizeof(YUV));
1230
1231     rgb2yuv(s->current, pic, s->linex, s->olinex, s->owidth, s->oheight);
1232
1233     for(by=0;by<s->bby;by++)
1234     {
1235         for(bx=0;bx<s->bbx;bx++)
1236         {
1237             encode_IFrame_block(tag, s, bx, by);
1238         }
1239     }
1240     s->frame++;
1241     memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1242 }
1243 void swf_SetVideoStreamBlackFrame(TAG*tag, VIDEOSTREAM*s)
1244 {
1245     int bx, by;
1246     int quant = 31;
1247     int x,y;
1248     s->quant = quant;
1249
1250     writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_IFRAME);
1251
1252     for(y=0;y<s->height;y++)
1253     for(x=0;x<s->width;x++) {
1254         s->current[y*s->width+x].y = 0;
1255         s->current[y*s->width+x].u = 128;
1256         s->current[y*s->width+x].v = 128;
1257     }
1258     for(x=0;x<16;x++)
1259     for(y=0;y<16;y++) {
1260         s->current[y*s->width+x].y = 64; 
1261         s->current[y*s->width+x].u = 128; 
1262         s->current[y*s->width+x].v = 128;
1263     }
1264
1265     for(by=0;by<s->bby;by++)
1266     {
1267         for(bx=0;bx<s->bbx;bx++)
1268         {
1269             encode_IFrame_block(tag, s, bx, by);
1270         }
1271     }
1272     s->frame++;
1273     memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1274 }
1275
1276 void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1277 {
1278     int bx, by;
1279
1280     if(quant<1) quant=1;
1281     if(quant>31) quant=31;
1282     s->quant = quant;
1283
1284     writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1285
1286     /* fixme: should fill with 0,128,128, not 0,0,0 */
1287     memset(s->current, 0, s->linex*s->height*sizeof(YUV));
1288
1289     rgb2yuv(s->current, pic, s->linex, s->olinex, s->owidth, s->oheight);
1290     memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1291     memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1292
1293     for(by=0;by<s->bby;by++)
1294     {
1295         for(bx=0;bx<s->bbx;bx++)
1296         {
1297             encode_PFrame_block(tag, s, bx, by);
1298         }
1299     }
1300     s->frame++;
1301     memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1302
1303 #ifdef MAIN
1304 #ifdef PNG
1305     yuv2rgb(pic, s->current, s->linex, s->width, s->height);
1306     setdbgpic(tag, pic, s->width, s->height);
1307 #endif
1308 #endif
1309 }
1310
1311 void swf_SetVideoStreamMover(TAG*tag, VIDEOSTREAM*s, signed char* movex, signed char* movey, void**pictures, int quant)
1312 {
1313     int bx, by;
1314     YUV pic[16*16];
1315
1316     if(quant<1) quant=1;
1317     if(quant>31) quant=31;
1318     s->quant = quant;
1319
1320     writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1321
1322     memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1323     memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1324
1325     for(by=0;by<s->bby;by++)
1326     {
1327         for(bx=0;bx<s->bbx;bx++)
1328         {
1329             int predictmvdx=0, predictmvdy=0;
1330             int mvx=movex[by*s->bbx+bx];
1331             int mvy=movey[by*s->bbx+bx];
1332             void*picture = pictures?pictures[by*s->bbx+bx]:0;
1333     
1334             if(mvx<-32) mvx=-32;
1335             if(mvx>31) mvx=31;
1336             if(mvy<-32) mvy=-32;
1337             if(mvy>31) mvy=31;
1338
1339             if(mvx == 0 && mvy == 0 && picture == 0) {
1340                 swf_SetBits(tag,1,1); // COD skip
1341             } else {
1342                 int mode = 0;
1343                 int has_dc=0;
1344                 int y=0,c=0;
1345                 block_t b;
1346                 block_t b2;
1347                 
1348                 swf_SetBits(tag,0,1); // COD
1349
1350                 if(mvx==0 && mvy==0 && picture) { // only picture
1351                     mode = 3;
1352                     has_dc = 1;
1353                 }
1354
1355                 if(picture) {
1356                     RGBA* picblock = (RGBA*)picture;
1357                     rgb2yuv(pic, picblock,16,16,16,16);
1358                     /* TODO: if has_dc!=1, subtract 128 from rgb values */
1359                     getregion(&b, pic, 0,0,16);
1360                     dodctandquant(&b, &b2, 1, s->quant);
1361                     getblockpatterns(&b2, &y, &c, 1);
1362                 } else {
1363                     y=0;c=0;
1364                 }
1365
1366                 codehuffman(tag, mcbpc_inter, mode*4+c);
1367                 codehuffman(tag, cbpy, mode==3?y:y^15);
1368                 
1369                 if(mode < 3) {
1370                     /* has motion vector */
1371                     predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
1372                     codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, mvx, mvy, 0));
1373                     codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, mvx, mvy, 1));
1374                     s->mvdx[by*s->bbx+bx] = mvx;
1375                     s->mvdy[by*s->bbx+bx] = mvy;
1376                 }
1377
1378                 if(has_dc||y||c) {
1379                     encode8x8(tag, b2.y1, has_dc, y&8);
1380                     encode8x8(tag, b2.y2, has_dc, y&4);
1381                     encode8x8(tag, b2.y3, has_dc, y&2);
1382                     encode8x8(tag, b2.y4, has_dc, y&1);
1383                     encode8x8(tag, b2.u, has_dc, c&2);
1384                     encode8x8(tag, b2.v, has_dc, c&1);
1385                 }
1386             }
1387         }
1388     }
1389     s->frame++;
1390 }
1391
1392 #define TESTS
1393 #ifdef TESTS
1394 void test_copy_diff()
1395 {
1396     VIDEOSTREAM stream;
1397     VIDEOSTREAM* s = &stream;
1398     TAG*tag;
1399     RGBA*pic = malloc(256*256*sizeof(RGBA));
1400     block_t fb;
1401     int x,y;
1402     int bx,by;
1403     for(x=0;x<256;x++)
1404     for(y=0;y<256;y++) {
1405         pic[y*256+x].r = x*y;
1406         pic[y*256+x].g = x+y;
1407         pic[y*256+x].b = (x+1)%(y+1);
1408     }
1409     tag = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
1410     swf_SetU16(tag, 33);
1411     swf_SetVideoStreamDefine(tag, s, 10, 256, 256);
1412     
1413     rgb2yuv(s->current, pic, s->linex, s->olinex, s->owidth, s->oheight);
1414     for(by=0;by<16;by++)
1415     for(bx=0;bx<16;bx++) {
1416         int diff1,diff2;
1417         /* test1: does compare pic pic return zero for identical blocks? */
1418         diff1 = compare_pic_pic(s, s->current, s->current, bx, by);
1419         assert(!diff1);
1420         /* test2: do blocks which are copied back return zero diff? */
1421         getregion(&fb, s->current, bx, by, s->linex);
1422         copy_block_pic(s, s->oldpic, &fb, bx, by);
1423         diff1 = compare_pic_block(s, &fb, s->oldpic, bx, by);
1424         assert(!diff1);
1425         /* test3: does compare_pic_block return the same result as compare_pic_pic? */
1426         getregion(&fb, s->current, 15-bx, 15-by, s->linex);
1427         copy_block_pic(s, s->oldpic, &fb, bx, by);
1428         diff1 = compare_pic_block(s, &fb, s->current, bx, by);
1429         diff2 = compare_pic_pic(s, s->current, s->oldpic, bx, by);
1430         assert(diff1 == diff2);
1431     }
1432 }
1433
1434 #endif
1435
1436 #ifdef MAIN
1437 #include "png.h"
1438
1439 int compileSWFActionCode(const char *script, int version, void**data, int*len) {return 0;}
1440
1441 void mkblack()
1442 {
1443     SWF swf;
1444     SWFPLACEOBJECT obj;
1445     int frames = 88;
1446     int width = 160;
1447     int height = 112;
1448     int x,y;
1449     TAG*tag = 0;
1450     RGBA rgb;
1451     RGBA* pic = 0;
1452     VIDEOSTREAM stream;
1453    
1454     pic = malloc(width*height*4);
1455     memset(pic, 0, width*height*4);
1456
1457     memset(&swf,0,sizeof(SWF));
1458     memset(&obj,0,sizeof(obj));
1459
1460     swf.fileVersion    = 6;
1461     swf.frameRate      = 15*256;
1462     swf.movieSize.xmax = 20*width;
1463     swf.movieSize.ymax = 20*height;
1464
1465     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1466     tag = swf.firstTag;
1467     rgb.r = 0x00;rgb.g = 0x30;rgb.b = 0xff;
1468     swf_SetRGB(tag,&rgb);
1469
1470     tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
1471     swf_SetU16(tag, 1);
1472     swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
1473     stream.do_motion = 0;
1474
1475     for(y=0;y<height;y++)  {
1476         for(x=0;x<width;x++) {
1477             int dx = x/16;
1478             int dy = y/16;
1479
1480             pic[y*width+x].r = 0;
1481             pic[y*width+x].g = 0;
1482             pic[y*width+x].b = 0;
1483             pic[y*width+x].a = 0;
1484         }
1485     }
1486     tag = swf_InsertTag(tag, ST_VIDEOFRAME);
1487     swf_SetU16(tag, 1);
1488     
1489     swf_SetVideoStreamIFrame(tag, &stream, pic, 7);
1490
1491     tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
1492     swf_GetPlaceObject(0, &obj);
1493     
1494     obj.depth = 4;
1495     obj.id = 1;
1496     
1497     swf_SetPlaceObject(tag,&obj);
1498
1499     tag = swf_InsertTag(tag, ST_SHOWFRAME);
1500     
1501     swf_VideoStreamClear(&stream);
1502
1503     tag = swf_InsertTag(tag, ST_END);
1504
1505     int fi = open("black.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
1506     if(swf_WriteSWC(fi,&swf)<0) {
1507         fprintf(stderr,"WriteSWF() failed.\n");
1508     }
1509     close(fi);
1510     swf_FreeTags(&swf);
1511 }
1512
1513 int main(int argn, char*argv[])
1514 {
1515     int fi;
1516     int t;
1517     SWF swf;
1518     TAG * tag;
1519     RGBA* pic, *pic2, rgb;
1520     SWFPLACEOBJECT obj;
1521     int width = 0;
1522     int height = 0;
1523     int frames = 10;
1524     int framerate = 29;
1525     unsigned char*data;
1526     char* fname = "/home/kramm/pics/peppers_fromjpg.png";
1527     //char* fname = "/home/kramm/pics/baboon.png";
1528     VIDEOSTREAM stream;
1529     double d = 1.0;
1530
1531 #ifdef TESTS
1532     test_copy_diff();
1533 #endif
1534
1535     mkblack();
1536
1537     memset(&stream, 0, sizeof(stream));
1538
1539     getPNG(fname, &width, &height, &data);
1540     pic = (RGBA*)malloc(width*height*sizeof(RGBA));
1541     pic2 = (RGBA*)malloc(width*height*sizeof(RGBA));
1542     memcpy(pic, data, width*height*sizeof(RGBA));
1543     free(data);
1544
1545     printf("Compressing %s, size %dx%d\n", fname, width, height);
1546
1547     memset(&swf,0,sizeof(SWF));
1548     memset(&obj,0,sizeof(obj));
1549
1550     swf.fileVersion    = 6;
1551     swf.frameRate      = framerate*256;
1552     swf.movieSize.xmax = 20*width*2;
1553     swf.movieSize.ymax = 20*height;
1554
1555     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1556     tag = swf.firstTag;
1557     rgb.r = 0x00;rgb.g = 0x30;rgb.b = 0xff;
1558     swf_SetRGB(tag,&rgb);
1559
1560     tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
1561     swf_SetU16(tag, 33);
1562     swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
1563     stream.do_motion = 0;
1564
1565     //srand48(time(0));
1566
1567     for(t=0;t<frames;t++)
1568     {
1569         int x,y;
1570         double xx,yy;
1571         for(y=0,yy=0;y<height;y++,yy+=d)  {
1572             RGBA*line = &pic[((int)yy)*width];
1573             for(x=0,xx=0;x<width;x++,xx+=d) {
1574                 int dx = x/16;
1575                 int dy = y/16;
1576                 if(dx==0 && dy==0) {
1577                     pic2[y*width+x] = line[((int)xx)];
1578                     pic2[y*width+x].r+=2;
1579                     pic2[y*width+x].g+=2;
1580                     pic2[y*width+x].b+=2;
1581                 } else {
1582                     //pic2[y*width+x] = line[((int)xx)];
1583                     //pic2[y*width+x].r = lrand48();//line[((int)xx)];
1584                     //pic2[y*width+x].g = lrand48();//line[((int)xx)];
1585                     //pic2[y*width+x].b = lrand48();//line[((int)xx)];
1586                     pic2[y*width+x].r = 0;
1587                     pic2[y*width+x].g = 0;
1588                     pic2[y*width+x].b = 0;
1589                 }
1590                 /*if(dx==16 && dy==16) 
1591                     pic2[y*width+x] = pic[(y-16*16)*width+(x-16*16)];*/
1592                 /*if(dx<=0 && dy<=0) {
1593                     pic2[y*width+x] = line[((int)xx)];*/
1594                 /*if(x==0 && y==0) {
1595                     RGBA color;
1596                     memset(&color, 0, sizeof(RGBA));
1597                     pic2[y*width+x] = color;*/
1598                 /*} else  {
1599                     RGBA color;
1600                     color.r = lrand48();
1601                     color.g = lrand48();
1602                     color.b = lrand48();
1603                     color.a = 0;
1604                     pic2[y*width+x] = color;
1605                 }*/
1606             }
1607         }
1608         printf("frame:%d\n", t);fflush(stdout);
1609
1610         if(t==1)
1611             break;
1612
1613         tag = swf_InsertTag(tag, ST_VIDEOFRAME);
1614         swf_SetU16(tag, 33);
1615         if(t==0)
1616             swf_SetVideoStreamIFrame(tag, &stream, pic2, 7);
1617         else {
1618             swf_SetVideoStreamPFrame(tag, &stream, pic2, 7);
1619         }
1620
1621         tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
1622         swf_GetPlaceObject(0, &obj);
1623         if(t==0) {
1624             obj.depth = 1;
1625             obj.id = 33;
1626         } else {
1627             obj.move = 1;
1628             obj.depth = 1;
1629             obj.ratio = t;
1630         }
1631         swf_SetPlaceObject(tag,&obj);
1632
1633         tag = swf_InsertTag(tag, ST_SHOWFRAME);
1634         d-=0.015;
1635     }
1636     swf_VideoStreamClear(&stream);
1637
1638     tag = swf_InsertTag(tag, ST_END);
1639
1640     fi = open("video3.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
1641     if(swf_WriteSWC(fi,&swf)<0) {
1642         fprintf(stderr,"WriteSWF() failed.\n");
1643     }
1644     close(fi);
1645     swf_FreeTags(&swf);
1646     return 0;
1647 }
1648 #undef MAIN
1649 #endif