added some debugging code.
[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
820     if((x<-32 && x>31) || (y<-32 && y>31)) 
821         fprintf(stderr, "(%d,%d)\n", x,y);
822     assert((x>=-32 && x<=31) && (y>=-32 && y<=31));
823     //assert((x&1)==0 && (y&1)==0);//for now
824     //assert((x&2)==0 && (y&2)==0);//for now(2)
825
826     x-=px;
827     y-=py;
828
829     if(xy)
830         x=y;
831     x+=32;
832
833     /* (x&63) */
834     if(x>63)
835         x-=64;
836     if(x<0)
837         x+=64;
838
839     assert(x>=0 && x<64);
840     return x;
841 }
842
843 typedef struct _iblockdata_t
844 {
845     block_t b; //transformed quantized coefficients
846     block_t reconstruction;
847     int bits;
848     int bx,by;
849     struct huffcode*ctable; //table to use for chrominance encoding (different for i-frames)
850     int iframe; // 1 if this is part of an iframe
851 } iblockdata_t;
852
853 typedef struct _mvdblockdata_t
854 {
855     block_t b;
856     block_t fbold;
857     block_t reconstruction;
858     int xindex;
859     int yindex;
860     int movex;
861     int movey;
862     int bits;
863     int bx,by;
864 } mvdblockdata_t;
865
866 void prepareIBlock(VIDEOSTREAM*s, iblockdata_t*data, int bx, int by, block_t* fb, int*bits, int iframe)
867 {
868     /* consider I-block */
869     block_t fb_i;
870     block_t b;
871     int y,c;
872     struct huffcode*ctable;
873
874     data->bx = bx;
875     data->by = by;
876
877     data->iframe = iframe;
878     if(!iframe) {
879         data->ctable = &mcbpc_inter[3*4];
880     } else {
881         data->ctable = &mcbpc_intra[0];
882     }
883
884     memcpy(&fb_i, fb, sizeof(block_t));
885     dodctandquant(&fb_i, &data->b, 1, s->quant);
886     getblockpatterns(&data->b, &y, &c, 1);
887     *bits = 0;
888     if(!data->iframe) {
889         *bits += 1; //cod
890     }
891     *bits += data->ctable[c].len;
892     *bits += cbpy[y].len;
893     *bits += coefbits8x8(data->b.y1, 1);
894     *bits += coefbits8x8(data->b.y2, 1);
895     *bits += coefbits8x8(data->b.y3, 1);
896     *bits += coefbits8x8(data->b.y4, 1);
897     *bits += coefbits8x8(data->b.u, 1);
898     *bits += coefbits8x8(data->b.v, 1);
899     data->bits = *bits;
900     
901     /* -- reconstruction -- */
902     memcpy(&data->reconstruction,&data->b,sizeof(block_t));
903     dequantize(&data->reconstruction, 1, s->quant);
904     doidct(&data->reconstruction);
905     truncateblock(&data->reconstruction);
906 }
907
908 int writeIBlock(VIDEOSTREAM*s, TAG*tag, iblockdata_t*data)
909 {
910     int c = 0, y = 0;
911     int has_dc=1;
912     int bits = 0;
913     block_t b;
914
915     getblockpatterns(&data->b, &y, &c, has_dc);
916     if(!data->iframe) {
917         swf_SetBits(tag,0,1); bits += 1; // COD
918     }
919     bits += codehuffman(tag, data->ctable, c);
920     bits += codehuffman(tag, cbpy, y);
921
922     /* luminance */
923     bits += encode8x8(tag, data->b.y1, has_dc, y&8);
924     bits += encode8x8(tag, data->b.y2, has_dc, y&4);
925     bits += encode8x8(tag, data->b.y3, has_dc, y&2);
926     bits += encode8x8(tag, data->b.y4, has_dc, y&1);
927
928     /* chrominance */
929     bits += encode8x8(tag, data->b.u, has_dc, c&2);
930     bits += encode8x8(tag, data->b.v, has_dc, c&1);
931
932     copy_block_pic(s, s->current, &data->reconstruction, data->bx, data->by);
933     assert(data->bits == bits);
934     return bits;
935 }
936
937 int getmvdbits(VIDEOSTREAM*s,block_t*fb, int bx,int by,int hx,int hy)
938 {
939     block_t b;
940     block_t fbold;
941     block_t fbdiff;
942     int bits = 0;
943     memcpy(&fbdiff, fb, sizeof(block_t));
944     getmvdregion(&fbold, s->oldpic, bx, by, hx, hy, s->linex);
945     yuvdiff(&fbdiff, &fbold);
946     dodctandquant(&fbdiff, &b, 0, s->quant);
947     bits += coefbits8x8(b.y1, 0);
948     bits += coefbits8x8(b.y2, 0);
949     bits += coefbits8x8(b.y3, 0);
950     bits += coefbits8x8(b.y4, 0);
951     bits += coefbits8x8(b.u, 0);
952     bits += coefbits8x8(b.v, 0);
953     return bits;
954 }
955
956 void prepareMVDBlock(VIDEOSTREAM*s, mvdblockdata_t*data, int bx, int by, block_t* fb, int*bits)
957 { /* consider mvd(x,y)-block */
958
959     int t;
960     int y,c;
961     block_t fbdiff;
962     int predictmvdx;
963     int predictmvdy;
964
965     data->bx = bx;
966     data->by = by;
967     predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
968
969     data->bits = 65535;
970     data->movex=0;
971     data->movey=0;
972
973     if(s->do_motion) {
974         int hx,hy;
975         int bestx=0,besty=0,bestbits=65536;
976         int startx=-32,endx=31;
977         int starty=-32,endy=31;
978
979         if(!bx) startx=0;
980         if(!by) starty=0;
981         if(bx==s->bbx-1) endx=0;
982         if(by==s->bby-1) endy=0;
983
984         for(hx=startx;hx<=endx;hx+=4)
985         for(hy=starty;hy<=endy;hy+=4)
986         {
987             int bits = 0;
988             bits = getmvdbits(s,fb,bx,by,hx,hy);
989             if(bits<bestbits) {
990                 bestbits = bits;
991                 bestx = hx;
992                 besty = hy;
993             }
994         }
995         
996         if(bestx-3 > startx) startx = bestx-3;
997         if(besty-3 > starty) starty = besty-3;
998         if(bestx+3 < endx) endx = bestx+3;
999         if(besty+3 < endy) endy = besty+3;
1000
1001         for(hx=startx;hx<=endx;hx++)
1002         for(hy=starty;hy<=endy;hy++)
1003         {
1004             int bits = 0;
1005             bits = getmvdbits(s,fb,bx,by,hx,hy);
1006             if(bits<bestbits) {
1007                 bestbits = bits;
1008                 bestx = hx;
1009                 besty = hy;
1010             }
1011         }
1012         data->movex = bestx;
1013         data->movey = besty;
1014     }
1015
1016     memcpy(&fbdiff, fb, sizeof(block_t));
1017     getmvdregion(&data->fbold, s->oldpic, bx, by, data->movex, data->movey, s->linex);
1018     yuvdiff(&fbdiff, &data->fbold);
1019     dodctandquant(&fbdiff, &data->b, 0, s->quant);
1020     getblockpatterns(&data->b, &y, &c, 0);
1021
1022     data->xindex = mvd2index(predictmvdx, predictmvdy, data->movex, data->movey, 0);
1023     data->yindex = mvd2index(predictmvdx, predictmvdy, data->movex, data->movey, 1);
1024
1025     *bits = 1; //cod
1026     *bits += mcbpc_inter[0*4+c].len;
1027     *bits += cbpy[y^15].len;
1028     *bits += mvd[data->xindex].len; // (0,0)
1029     *bits += mvd[data->yindex].len;
1030     *bits += coefbits8x8(data->b.y1, 0);
1031     *bits += coefbits8x8(data->b.y2, 0);
1032     *bits += coefbits8x8(data->b.y3, 0);
1033     *bits += coefbits8x8(data->b.y4, 0);
1034     *bits += coefbits8x8(data->b.u, 0);
1035     *bits += coefbits8x8(data->b.v, 0);
1036     data->bits = *bits;
1037
1038     /* -- reconstruction -- */
1039     memcpy(&data->reconstruction, &data->b, sizeof(block_t));
1040     dequantize(&data->reconstruction, 0, s->quant);
1041     doidct(&data->reconstruction);
1042     for(t=0;t<64;t++) {
1043         data->reconstruction.y1[t] = 
1044             truncate256(data->reconstruction.y1[t] + (int)data->fbold.y1[t]);
1045         data->reconstruction.y2[t] = 
1046             truncate256(data->reconstruction.y2[t] + (int)data->fbold.y2[t]);
1047         data->reconstruction.y3[t] = 
1048             truncate256(data->reconstruction.y3[t] + (int)data->fbold.y3[t]);
1049         data->reconstruction.y4[t] = 
1050             truncate256(data->reconstruction.y4[t] + (int)data->fbold.y4[t]);
1051         data->reconstruction.u[t] = 
1052             truncate256(data->reconstruction.u[t] + (int)data->fbold.u[t]);
1053         data->reconstruction.v[t] = 
1054             truncate256(data->reconstruction.v[t] + (int)data->fbold.v[t]);
1055     }
1056 }
1057
1058 int writeMVDBlock(VIDEOSTREAM*s, TAG*tag, mvdblockdata_t*data)
1059 {
1060     int c = 0, y = 0;
1061     /* mvd (0,0) block (mode=0) */
1062     int t;
1063     int has_dc=0; // mvd w/o mvd24
1064     int mode = 0;
1065     int bx = data->bx;
1066     int by = data->by;
1067     int bits = 0;
1068
1069     getblockpatterns(&data->b, &y, &c, has_dc);
1070     swf_SetBits(tag,0,1); bits += 1; // COD
1071     bits += codehuffman(tag, mcbpc_inter, mode*4+c);
1072     bits += codehuffman(tag, cbpy, y^15);
1073
1074     /* vector */
1075     bits += codehuffman(tag, mvd, data->xindex);
1076     bits += codehuffman(tag, mvd, data->yindex);
1077
1078     /* luminance */
1079     bits += encode8x8(tag, data->b.y1, has_dc, y&8);
1080     bits += encode8x8(tag, data->b.y2, has_dc, y&4);
1081     bits += encode8x8(tag, data->b.y3, has_dc, y&2);
1082     bits += encode8x8(tag, data->b.y4, has_dc, y&1);
1083
1084     /* chrominance */
1085     bits += encode8x8(tag, data->b.u, has_dc, c&2);
1086     bits += encode8x8(tag, data->b.v, has_dc, c&1);
1087
1088     s->mvdx[by*s->bbx+bx] = data->movex;
1089     s->mvdy[by*s->bbx+bx] = data->movey;
1090
1091     copy_block_pic(s, s->current, &data->reconstruction, data->bx, data->by);
1092     assert(data->bits == bits);
1093     return bits;
1094 }
1095
1096 static int encode_PFrame_block(TAG*tag, VIDEOSTREAM*s, int bx, int by)
1097 {
1098     block_t fb;
1099     int diff1,diff2;
1100     int bits_i;
1101     int bits_vxy;
1102
1103     iblockdata_t iblock;
1104     mvdblockdata_t mvdblock;
1105     
1106     getregion(&fb, s->current, bx, by, s->linex);
1107     prepareIBlock(s, &iblock, bx, by, &fb, &bits_i, 0);
1108
1109     /* encoded last frame <=> original current block: */
1110     diff1 = compare_pic_pic(s, s->current, s->oldpic, bx, by);
1111     /* encoded current frame <=> original current block: */
1112     diff2 = compare_pic_block(s, &iblock.reconstruction, s->current, bx, by);
1113
1114     if(diff1 <= diff2) {
1115         swf_SetBits(tag, 1,1); /* cod=1, block skipped */
1116         /* copy the region from the last frame so that we have a complete reconstruction */
1117         copyregion(s, s->current, s->oldpic, bx, by);
1118         return 1;
1119     }
1120     prepareMVDBlock(s, &mvdblock, bx, by, &fb, &bits_vxy);
1121
1122     if(bits_i > bits_vxy) {
1123         return writeMVDBlock(s, tag, &mvdblock);
1124     } else {
1125         return writeIBlock(s, tag, &iblock);
1126     }
1127 }
1128
1129 /* should be called encode_IFrameBlock */
1130 static void encode_IFrame_block(TAG*tag, VIDEOSTREAM*s, int bx, int by)
1131 {
1132     block_t fb;
1133     iblockdata_t data;
1134     int bits;
1135
1136     getregion(&fb, s->current, bx, by, s->width);
1137     prepareIBlock(s, &data, bx, by, &fb, &bits, 1);
1138     writeIBlock(s, tag, &data);
1139 }
1140
1141 #ifdef MAIN
1142 static int bmid = 0;
1143
1144 void setdbgpic(TAG*tag, RGBA*pic, int width, int height)
1145 {
1146     MATRIX m;
1147     tag = tag->prev;
1148
1149     tag = swf_InsertTag(tag,ST_REMOVEOBJECT2);
1150     swf_SetU16(tag, 133);
1151
1152     tag = swf_InsertTag(tag, ST_DEFINEBITSLOSSLESS);
1153     swf_SetU16(tag, 1000+bmid);
1154     swf_SetLosslessBits(tag, width, height, (void*)pic, BMF_32BIT);
1155
1156     tag = swf_InsertTag(tag, ST_DEFINESHAPE);
1157     swf_SetU16(tag, 2000+bmid);
1158     swf_ShapeSetBitmapRect(tag, 1000+bmid, width, height);
1159
1160     tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
1161     swf_GetMatrix(0,&m);
1162     m.tx = width*20;
1163     swf_ObjectPlace(tag, 2000+bmid, 133, &m, 0, 0);
1164
1165     bmid++;
1166 }
1167 #endif
1168
1169 #define TYPE_IFRAME 0
1170 #define TYPE_PFRAME 1
1171
1172 static void writeHeader(TAG*tag, int width, int height, int frame, int quant, int type)
1173 {
1174     U32 i32;
1175     swf_SetU16(tag, frame);
1176     swf_SetBits(tag, 1, 17); /* picture start code*/
1177     swf_SetBits(tag, 0, 5); /* version=0, version 1 would optimize rle behaviour*/
1178     swf_SetBits(tag, frame, 8); /* time reference */
1179
1180     /* write dimensions, taking advantage of some predefined sizes
1181        if the opportunity presents itself */
1182     i32 = width<<16|height;
1183     switch(i32)
1184     {
1185         case 352<<16|288: swf_SetBits(tag, 2, 3);break;
1186         case 176<<16|144: swf_SetBits(tag, 3, 3);break;
1187         case 128<<16|96: swf_SetBits(tag, 4, 3);break;
1188         case 320<<16|240: swf_SetBits(tag, 5, 3);break;
1189         case 160<<16|120: swf_SetBits(tag, 6, 3);break;
1190         default:
1191             if(width>255 || height>255) {
1192                 swf_SetBits(tag, 1, 3);
1193                 swf_SetBits(tag, width, 16);
1194                 swf_SetBits(tag, height, 16);
1195             } else {
1196                 swf_SetBits(tag, 0, 3);
1197                 swf_SetBits(tag, width, 8);
1198                 swf_SetBits(tag, height, 8);
1199             }
1200     }
1201
1202     swf_SetBits(tag, type, 2); /* I-Frame or P-Frame */
1203     swf_SetBits(tag, 0, 1); /* No deblock filter */
1204     assert(quant>0);
1205     assert(quant<32);
1206     swf_SetBits(tag, quant, 5); /* quantizer (1-31), may be updated later on*/
1207     swf_SetBits(tag, 0, 1); /* No extra info */
1208 }
1209
1210 void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1211 {
1212     int bx, by;
1213
1214     if(quant<1) quant=1;
1215     if(quant>31) quant=31;
1216     s->quant = quant;
1217
1218     writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_IFRAME);
1219
1220     /* fixme: should fill with 0,128,128, not 0,0,0 */
1221     memset(s->current, 0, s->linex*s->height*sizeof(YUV));
1222
1223     rgb2yuv(s->current, pic, s->linex, s->olinex, s->owidth, s->oheight);
1224
1225     for(by=0;by<s->bby;by++)
1226     {
1227         for(bx=0;bx<s->bbx;bx++)
1228         {
1229             encode_IFrame_block(tag, s, bx, by);
1230         }
1231     }
1232     s->frame++;
1233     memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1234 }
1235
1236 void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant)
1237 {
1238     int bx, by;
1239
1240     if(quant<1) quant=1;
1241     if(quant>31) quant=31;
1242     s->quant = quant;
1243
1244     writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1245
1246     /* fixme: should fill with 0,128,128, not 0,0,0 */
1247     memset(s->current, 0, s->linex*s->height*sizeof(YUV));
1248
1249     rgb2yuv(s->current, pic, s->linex, s->olinex, s->owidth, s->oheight);
1250     memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1251     memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1252
1253     for(by=0;by<s->bby;by++)
1254     {
1255         for(bx=0;bx<s->bbx;bx++)
1256         {
1257             encode_PFrame_block(tag, s, bx, by);
1258         }
1259     }
1260     s->frame++;
1261     memcpy(s->oldpic, s->current, s->width*s->height*sizeof(YUV));
1262
1263 #ifdef MAIN
1264 #ifdef PNG
1265     yuv2rgb(pic, s->current, s->linex, s->width, s->height);
1266     setdbgpic(tag, pic, s->width, s->height);
1267 #endif
1268 #endif
1269 }
1270
1271 void swf_SetVideoStreamMover(TAG*tag, VIDEOSTREAM*s, signed char* movex, signed char* movey, int quant)
1272 {
1273     int bx, by;
1274
1275     if(quant<1) quant=1;
1276     if(quant>31) quant=31;
1277
1278     writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME);
1279
1280     memset(s->mvdx, 0, s->bbx*s->bby*sizeof(int));
1281     memset(s->mvdy, 0, s->bbx*s->bby*sizeof(int));
1282
1283     for(by=0;by<s->bby;by++)
1284     {
1285         for(bx=0;bx<s->bbx;bx++)
1286         {
1287             /* mvd (0,0) block (mode=0) */
1288             int mode = 0;
1289             int has_dc = 0;
1290             int cbpybits=0,cbpcbits=0;
1291             int predictmvdx=0, predictmvdy=0;
1292             int mvx=movex[by*s->bbx+bx];
1293             int mvy=movey[by*s->bbx+bx];
1294     
1295             if(mvx<-32) mvx=-32;
1296             if(mvx>31) mvx=31;
1297             if(mvy<-32) mvy=-32;
1298             if(mvy>31) mvy=31;
1299
1300             if(mvx == 0 && mvy == 0) {
1301                 swf_SetBits(tag,1,1); // COD skip
1302             } else {
1303                 swf_SetBits(tag,0,1); // COD
1304                 codehuffman(tag, mcbpc_inter, mode*4+cbpcbits);
1305                 codehuffman(tag, cbpy, cbpybits^15);
1306         
1307                 /* vector */
1308                 predictmvd(s,bx,by,&predictmvdx,&predictmvdy);
1309                 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, mvx, mvy, 0));
1310                 codehuffman(tag, mvd, mvd2index(predictmvdx, predictmvdy, mvx, mvy, 1));
1311                 s->mvdx[by*s->bbx+bx] = mvx;
1312                 s->mvdy[by*s->bbx+bx] = mvy;
1313             }
1314         }
1315     }
1316     s->frame++;
1317 }
1318
1319 #define TESTS
1320 #ifdef TESTS
1321 void test_copy_diff()
1322 {
1323     VIDEOSTREAM stream;
1324     VIDEOSTREAM* s = &stream;
1325     TAG*tag;
1326     RGBA*pic = malloc(256*256*sizeof(RGBA));
1327     block_t fb;
1328     int x,y;
1329     int bx,by;
1330     for(x=0;x<256;x++)
1331     for(y=0;y<256;y++) {
1332         pic[y*256+x].r = x*y;
1333         pic[y*256+x].g = x+y;
1334         pic[y*256+x].b = (x+1)%(y+1);
1335     }
1336     tag = swf_InsertTag(0, ST_DEFINEVIDEOSTREAM);
1337     swf_SetU16(tag, 33);
1338     swf_SetVideoStreamDefine(tag, s, 10, 256, 256);
1339     
1340     rgb2yuv(s->current, pic, s->linex, s->olinex, s->owidth, s->oheight);
1341     for(by=0;by<16;by++)
1342     for(bx=0;bx<16;bx++) {
1343         int diff1,diff2;
1344         /* test1: does compare pic pic return zero for identical blocks? */
1345         diff1 = compare_pic_pic(s, s->current, s->current, bx, by);
1346         assert(!diff1);
1347         /* test2: do blocks which are copied back return zero diff? */
1348         getregion(&fb, s->current, bx, by, s->linex);
1349         copy_block_pic(s, s->oldpic, &fb, bx, by);
1350         diff1 = compare_pic_block(s, &fb, s->oldpic, bx, by);
1351         assert(!diff1);
1352         /* test3: does compare_pic_block return the same result as compare_pic_pic? */
1353         getregion(&fb, s->current, 15-bx, 15-by, s->linex);
1354         copy_block_pic(s, s->oldpic, &fb, bx, by);
1355         diff1 = compare_pic_block(s, &fb, s->current, bx, by);
1356         diff2 = compare_pic_pic(s, s->current, s->oldpic, bx, by);
1357         assert(diff1 == diff2);
1358     }
1359 }
1360
1361 #endif
1362
1363 #ifdef MAIN
1364 #include "png.h"
1365
1366 int compileSWFActionCode(const char *script, int version, void**data, int*len) {return 0;}
1367
1368 void mkblack()
1369 {
1370     SWF swf;
1371     SWFPLACEOBJECT obj;
1372     int frames = 88;
1373     int width = 160;
1374     int height = 112;
1375     int x,y;
1376     TAG*tag = 0;
1377     RGBA rgb;
1378     RGBA* pic = 0;
1379     VIDEOSTREAM stream;
1380    
1381     pic = malloc(width*height*4);
1382     memset(pic, 0, width*height*4);
1383
1384     memset(&swf,0,sizeof(SWF));
1385     memset(&obj,0,sizeof(obj));
1386
1387     swf.fileVersion    = 6;
1388     swf.frameRate      = 15*256;
1389     swf.movieSize.xmax = 20*width;
1390     swf.movieSize.ymax = 20*height;
1391
1392     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1393     tag = swf.firstTag;
1394     rgb.r = 0x00;rgb.g = 0x30;rgb.b = 0xff;
1395     swf_SetRGB(tag,&rgb);
1396
1397     tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
1398     swf_SetU16(tag, 1);
1399     swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
1400     stream.do_motion = 0;
1401
1402     for(y=0;y<height;y++)  {
1403         for(x=0;x<width;x++) {
1404             int dx = x/16;
1405             int dy = y/16;
1406
1407             pic[y*width+x].r = 0;
1408             pic[y*width+x].g = 0;
1409             pic[y*width+x].b = 0;
1410             pic[y*width+x].a = 0;
1411         }
1412     }
1413     tag = swf_InsertTag(tag, ST_VIDEOFRAME);
1414     swf_SetU16(tag, 1);
1415     
1416     swf_SetVideoStreamIFrame(tag, &stream, pic, 7);
1417
1418     tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
1419     swf_GetPlaceObject(0, &obj);
1420     
1421     obj.depth = 4;
1422     obj.id = 1;
1423     
1424     swf_SetPlaceObject(tag,&obj);
1425
1426     tag = swf_InsertTag(tag, ST_SHOWFRAME);
1427     
1428     swf_VideoStreamClear(&stream);
1429
1430     tag = swf_InsertTag(tag, ST_END);
1431
1432     int fi = open("black.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
1433     if(swf_WriteSWC(fi,&swf)<0) {
1434         fprintf(stderr,"WriteSWF() failed.\n");
1435     }
1436     close(fi);
1437     swf_FreeTags(&swf);
1438 }
1439
1440 int main(int argn, char*argv[])
1441 {
1442     int fi;
1443     int t;
1444     SWF swf;
1445     TAG * tag;
1446     RGBA* pic, *pic2, rgb;
1447     SWFPLACEOBJECT obj;
1448     int width = 0;
1449     int height = 0;
1450     int frames = 10;
1451     int framerate = 29;
1452     unsigned char*data;
1453     char* fname = "/home/kramm/pics/peppers_fromjpg.png";
1454     //char* fname = "/home/kramm/pics/baboon.png";
1455     VIDEOSTREAM stream;
1456     double d = 1.0;
1457
1458 #ifdef TESTS
1459     test_copy_diff();
1460 #endif
1461
1462     mkblack();
1463
1464     memset(&stream, 0, sizeof(stream));
1465
1466     getPNG(fname, &width, &height, &data);
1467     pic = (RGBA*)malloc(width*height*sizeof(RGBA));
1468     pic2 = (RGBA*)malloc(width*height*sizeof(RGBA));
1469     memcpy(pic, data, width*height*sizeof(RGBA));
1470     free(data);
1471
1472     printf("Compressing %s, size %dx%d\n", fname, width, height);
1473
1474     memset(&swf,0,sizeof(SWF));
1475     memset(&obj,0,sizeof(obj));
1476
1477     swf.fileVersion    = 6;
1478     swf.frameRate      = framerate*256;
1479     swf.movieSize.xmax = 20*width*2;
1480     swf.movieSize.ymax = 20*height;
1481
1482     swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
1483     tag = swf.firstTag;
1484     rgb.r = 0x00;rgb.g = 0x30;rgb.b = 0xff;
1485     swf_SetRGB(tag,&rgb);
1486
1487     tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
1488     swf_SetU16(tag, 33);
1489     swf_SetVideoStreamDefine(tag, &stream, frames, width, height);
1490     stream.do_motion = 0;
1491
1492     //srand48(time(0));
1493
1494     for(t=0;t<frames;t++)
1495     {
1496         int x,y;
1497         double xx,yy;
1498         for(y=0,yy=0;y<height;y++,yy+=d)  {
1499             RGBA*line = &pic[((int)yy)*width];
1500             for(x=0,xx=0;x<width;x++,xx+=d) {
1501                 int dx = x/16;
1502                 int dy = y/16;
1503                 if(dx==0 && dy==0) {
1504                     pic2[y*width+x] = line[((int)xx)];
1505                     pic2[y*width+x].r+=2;
1506                     pic2[y*width+x].g+=2;
1507                     pic2[y*width+x].b+=2;
1508                 } else {
1509                     //pic2[y*width+x] = line[((int)xx)];
1510                     //pic2[y*width+x].r = lrand48();//line[((int)xx)];
1511                     //pic2[y*width+x].g = lrand48();//line[((int)xx)];
1512                     //pic2[y*width+x].b = lrand48();//line[((int)xx)];
1513                     pic2[y*width+x].r = 0;
1514                     pic2[y*width+x].g = 0;
1515                     pic2[y*width+x].b = 0;
1516                 }
1517                 /*if(dx==16 && dy==16) 
1518                     pic2[y*width+x] = pic[(y-16*16)*width+(x-16*16)];*/
1519                 /*if(dx<=0 && dy<=0) {
1520                     pic2[y*width+x] = line[((int)xx)];*/
1521                 /*if(x==0 && y==0) {
1522                     RGBA color;
1523                     memset(&color, 0, sizeof(RGBA));
1524                     pic2[y*width+x] = color;*/
1525                 /*} else  {
1526                     RGBA color;
1527                     color.r = lrand48();
1528                     color.g = lrand48();
1529                     color.b = lrand48();
1530                     color.a = 0;
1531                     pic2[y*width+x] = color;
1532                 }*/
1533             }
1534         }
1535         printf("frame:%d\n", t);fflush(stdout);
1536
1537         if(t==1)
1538             break;
1539
1540         tag = swf_InsertTag(tag, ST_VIDEOFRAME);
1541         swf_SetU16(tag, 33);
1542         if(t==0)
1543             swf_SetVideoStreamIFrame(tag, &stream, pic2, 7);
1544         else {
1545             swf_SetVideoStreamPFrame(tag, &stream, pic2, 7);
1546         }
1547
1548         tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
1549         swf_GetPlaceObject(0, &obj);
1550         if(t==0) {
1551             obj.depth = 1;
1552             obj.id = 33;
1553         } else {
1554             obj.move = 1;
1555             obj.depth = 1;
1556             obj.ratio = t;
1557         }
1558         swf_SetPlaceObject(tag,&obj);
1559
1560         tag = swf_InsertTag(tag, ST_SHOWFRAME);
1561         d-=0.015;
1562     }
1563     swf_VideoStreamClear(&stream);
1564
1565     tag = swf_InsertTag(tag, ST_END);
1566
1567     fi = open("video3.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
1568     if(swf_WriteSWC(fi,&swf)<0) {
1569         fprintf(stderr,"WriteSWF() failed.\n");
1570     }
1571     close(fi);
1572     swf_FreeTags(&swf);
1573     return 0;
1574 }
1575 #undef MAIN
1576 #endif