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