From a7a60c127b70f891c5bb1ecf58b9e90dfbfb1e41 Mon Sep 17 00:00:00 2001 From: kramm Date: Sun, 13 Apr 2003 17:44:45 +0000 Subject: [PATCH] moved to lib/h.263, quant is now a parameter, added swf_VideoStreamClear. --- lib/h.263/mkvideo.c | 96 ++++++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/lib/h.263/mkvideo.c b/lib/h.263/mkvideo.c index 250f4c7..db8d527 100644 --- a/lib/h.263/mkvideo.c +++ b/lib/h.263/mkvideo.c @@ -9,24 +9,9 @@ #include #include #include -#include "../lib/rfxswf.h" -#include "png.h" +#include "../rfxswf.h" #include "h263tables.c" - -typedef struct _YUV -{ - unsigned char y,u,v; -} YUV; - -typedef struct _VIDEOSTREAM -{ - int width; - int height; - int frame; - int linex; - YUV*oldpic; - YUV*current; -} VIDEOSTREAM; +#include "swfvideo.h" void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width, U16 height) { @@ -38,15 +23,22 @@ void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width swf_SetU8(tag, 2); /* codec = h.263 sorenson spark */ memset(stream, 0, sizeof(VIDEOSTREAM)); + stream->olinex = width; + width+=15;width&=~15; + height+=15;height&=~15; stream->linex = width; - width&=~15; - height&=~15; stream->width = width; stream->height = height; stream->current = (YUV*)malloc(width*height*sizeof(YUV)); stream->oldpic = (YUV*)malloc(width*height*sizeof(YUV)); memset(stream->oldpic, 0, width*height*sizeof(YUV)); + memset(stream->current, 0, width*height*sizeof(YUV)); +} +void swf_VideoStreamClear(VIDEOSTREAM*stream) +{ + free(stream->oldpic);stream->oldpic = 0; + free(stream->current);stream->current = 0; } typedef struct _block_t @@ -186,18 +178,21 @@ static void getregion(fblock_t* bb, YUV*pic, int bx, int by, int linex) p2+=linex*2; } } -static void rgb2yuv(YUV*dest, RGBA*src, int linex, int width, int height) +static void rgb2yuv(YUV*dest, RGBA*src, int dlinex, int slinex, int width, int height) { int x,y; for(y=0;y>8; + dest[y*dlinex+x].u = (r*((int)(-0.169*256)) + g*((int)(-0.332*256)) + b*((int)( 0.500 *256))+ 128*256)>>8; + dest[y*dlinex+x].v = (r*((int)( 0.500*256)) + g*((int)(-0.419*256)) + b*((int)(-0.0813*256))+ 128*256)>>8; } } } @@ -275,7 +270,7 @@ static int compareregions(VIDEOSTREAM*s, int bx, int by) return diff/256; } -static int valtodc(int val) +static inline int valtodc(int val) { assert(val>=0); @@ -322,16 +317,21 @@ static int codehuffman(TAG*tag, struct huffcode*table, int index) static void quantize8x8(double*src, int*dest, int has_dc, int quant) { int t,pos=0; + double q = 1.0/(quant*2); if(has_dc) { dest[0] = valtodc((int)src[0]); /*DC*/ pos++; } for(t=pos;t<64;t++) { - dest[t] = (int)src[t]; + //dest[t] = (int)src[t]; /* exact: if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;} */ //if(quant&1){dest[t] = (dest[t]/quant - 1)/2;}else{dest[t] = ((dest[t]+1)/quant - 1)/2;} - dest[t] = dest[t]/(quant*2); + //dest[t] = dest[t]/(quant*2); + dest[t] = (int)(src[t]*q); + /* TODO: warn if this happens- the video will be buggy */ + if(dest[t]>127) dest[t]=127; + if(dest[t]<-127) dest[t]=-127; } } @@ -684,10 +684,10 @@ static int encode_blockP(TAG*tag, VIDEOSTREAM*s, int bx, int by, int*quant) int bits_v00; diff = compareregions(s, bx, by); - if(diff < 24 /*TODO: should be a parameter- good values are between 32 and 48 */) { + if(diff < 16 /*TODO: should be a parameter- good values are between 32 and 48 */) { swf_SetBits(tag, 1,1); /* cod=1, block skipped */ copyregion(s, s->current, s->oldpic, bx, by); - return; + return 1; } getregion(&fb, s->current, bx, by, s->width); @@ -884,17 +884,19 @@ static void writeHeader(TAG*tag, int width, int height, int frame, int quant, in swf_SetBits(tag, 0, 1); /* No extra info */ } -void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic) +void swf_SetVideoStreamIFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant) { int bx, by, bbx, bby; - int quant = 31; + + if(quant<1) quant=1; + if(quant>31) quant=31; writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_IFRAME); bbx = (s->width+15)/16; //TODO: move bbx,bby into VIDEOSTREAM bby = (s->height+15)/16; - rgb2yuv(s->current, pic, s->linex, s->width, s->height); + rgb2yuv(s->current, pic, s->linex, s->olinex, s->width, s->height); for(by=0;byoldpic, s->current, s->width*s->height*sizeof(YUV)); } -void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic) +void swf_SetVideoStreamPFrame(TAG*tag, VIDEOSTREAM*s, RGBA*pic, int quant) { int bx, by, bbx, bby; - int quant = 31; + + if(quant<1) quant=1; + if(quant>31) quant=31; writeHeader(tag, s->width, s->height, s->frame, quant, TYPE_PFRAME); bbx = (s->width+15)/16; bby = (s->height+15)/16; - rgb2yuv(s->current, pic, s->linex, s->width, s->height); + rgb2yuv(s->current, pic, s->linex, s->olinex, s->width, s->height); for(by=0;by