00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/intmath.h"
00022 #include "libavutil/log.h"
00023 #include "libavutil/opt.h"
00024 #include "avcodec.h"
00025 #include "dsputil.h"
00026 #include "dwt.h"
00027 #include "snow.h"
00028 #include "snowdata.h"
00029
00030 #include "rangecoder.h"
00031 #include "mathops.h"
00032 #include "h263.h"
00033
00034
00035 void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
00036 int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8){
00037 int y, x;
00038 IDWTELEM * dst;
00039 for(y=0; y<b_h; y++){
00040
00041 const uint8_t *obmc1= obmc + y*obmc_stride;
00042 const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
00043 const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
00044 const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
00045 dst = slice_buffer_get_line(sb, src_y + y);
00046 for(x=0; x<b_w; x++){
00047 int v= obmc1[x] * block[3][x + y*src_stride]
00048 +obmc2[x] * block[2][x + y*src_stride]
00049 +obmc3[x] * block[1][x + y*src_stride]
00050 +obmc4[x] * block[0][x + y*src_stride];
00051
00052 v <<= 8 - LOG2_OBMC_MAX;
00053 if(FRAC_BITS != 8){
00054 v >>= 8 - FRAC_BITS;
00055 }
00056 if(add){
00057 v += dst[x + src_x];
00058 v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
00059 if(v&(~255)) v= ~(v>>31);
00060 dst8[x + y*src_stride] = v;
00061 }else{
00062 dst[x + src_x] -= v;
00063 }
00064 }
00065 }
00066 }
00067
00068 void ff_snow_reset_contexts(SnowContext *s){
00069 int plane_index, level, orientation;
00070
00071 for(plane_index=0; plane_index<3; plane_index++){
00072 for(level=0; level<MAX_DECOMPOSITIONS; level++){
00073 for(orientation=level ? 1:0; orientation<4; orientation++){
00074 memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
00075 }
00076 }
00077 }
00078 memset(s->header_state, MID_STATE, sizeof(s->header_state));
00079 memset(s->block_state, MID_STATE, sizeof(s->block_state));
00080 }
00081
00082 int ff_snow_alloc_blocks(SnowContext *s){
00083 int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
00084 int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
00085
00086 s->b_width = w;
00087 s->b_height= h;
00088
00089 av_free(s->block);
00090 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
00091 return 0;
00092 }
00093
00094 static void init_qexp(void){
00095 int i;
00096 double v=128;
00097
00098 for(i=0; i<QROOT; i++){
00099 ff_qexp[i]= lrintf(v);
00100 v *= pow(2, 1.0 / QROOT);
00101 }
00102 }
00103 static void mc_block(Plane *p, uint8_t *dst, const uint8_t *src, int stride, int b_w, int b_h, int dx, int dy){
00104 static const uint8_t weight[64]={
00105 8,7,6,5,4,3,2,1,
00106 7,7,0,0,0,0,0,1,
00107 6,0,6,0,0,0,2,0,
00108 5,0,0,5,0,3,0,0,
00109 4,0,0,0,4,0,0,0,
00110 3,0,0,5,0,3,0,0,
00111 2,0,6,0,0,0,2,0,
00112 1,7,0,0,0,0,0,1,
00113 };
00114
00115 static const uint8_t brane[256]={
00116 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
00117 0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
00118 0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
00119 0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
00120 0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
00121 0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
00122 0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
00123 0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
00124 0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
00125 0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
00126 0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
00127 0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
00128 0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
00129 0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
00130 0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
00131 0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
00132 };
00133
00134 static const uint8_t needs[16]={
00135 0,1,0,0,
00136 2,4,2,0,
00137 0,1,0,0,
00138 15
00139 };
00140
00141 int x, y, b, r, l;
00142 int16_t tmpIt [64*(32+HTAPS_MAX)];
00143 uint8_t tmp2t[3][64*(32+HTAPS_MAX)];
00144 int16_t *tmpI= tmpIt;
00145 uint8_t *tmp2= tmp2t[0];
00146 const uint8_t *hpel[11];
00147 av_assert2(dx<16 && dy<16);
00148 r= brane[dx + 16*dy]&15;
00149 l= brane[dx + 16*dy]>>4;
00150
00151 b= needs[l] | needs[r];
00152 if(p && !p->diag_mc)
00153 b= 15;
00154
00155 if(b&5){
00156 for(y=0; y < b_h+HTAPS_MAX-1; y++){
00157 for(x=0; x < b_w; x++){
00158 int a_1=src[x + HTAPS_MAX/2-4];
00159 int a0= src[x + HTAPS_MAX/2-3];
00160 int a1= src[x + HTAPS_MAX/2-2];
00161 int a2= src[x + HTAPS_MAX/2-1];
00162 int a3= src[x + HTAPS_MAX/2+0];
00163 int a4= src[x + HTAPS_MAX/2+1];
00164 int a5= src[x + HTAPS_MAX/2+2];
00165 int a6= src[x + HTAPS_MAX/2+3];
00166 int am=0;
00167 if(!p || p->fast_mc){
00168 am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
00169 tmpI[x]= am;
00170 am= (am+16)>>5;
00171 }else{
00172 am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
00173 tmpI[x]= am;
00174 am= (am+32)>>6;
00175 }
00176
00177 if(am&(~255)) am= ~(am>>31);
00178 tmp2[x]= am;
00179 }
00180 tmpI+= 64;
00181 tmp2+= 64;
00182 src += stride;
00183 }
00184 src -= stride*y;
00185 }
00186 src += HTAPS_MAX/2 - 1;
00187 tmp2= tmp2t[1];
00188
00189 if(b&2){
00190 for(y=0; y < b_h; y++){
00191 for(x=0; x < b_w+1; x++){
00192 int a_1=src[x + (HTAPS_MAX/2-4)*stride];
00193 int a0= src[x + (HTAPS_MAX/2-3)*stride];
00194 int a1= src[x + (HTAPS_MAX/2-2)*stride];
00195 int a2= src[x + (HTAPS_MAX/2-1)*stride];
00196 int a3= src[x + (HTAPS_MAX/2+0)*stride];
00197 int a4= src[x + (HTAPS_MAX/2+1)*stride];
00198 int a5= src[x + (HTAPS_MAX/2+2)*stride];
00199 int a6= src[x + (HTAPS_MAX/2+3)*stride];
00200 int am=0;
00201 if(!p || p->fast_mc)
00202 am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
00203 else
00204 am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
00205
00206 if(am&(~255)) am= ~(am>>31);
00207 tmp2[x]= am;
00208 }
00209 src += stride;
00210 tmp2+= 64;
00211 }
00212 src -= stride*y;
00213 }
00214 src += stride*(HTAPS_MAX/2 - 1);
00215 tmp2= tmp2t[2];
00216 tmpI= tmpIt;
00217 if(b&4){
00218 for(y=0; y < b_h; y++){
00219 for(x=0; x < b_w; x++){
00220 int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
00221 int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
00222 int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
00223 int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
00224 int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
00225 int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
00226 int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
00227 int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
00228 int am=0;
00229 if(!p || p->fast_mc)
00230 am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
00231 else
00232 am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
00233 if(am&(~255)) am= ~(am>>31);
00234 tmp2[x]= am;
00235 }
00236 tmpI+= 64;
00237 tmp2+= 64;
00238 }
00239 }
00240
00241 hpel[ 0]= src;
00242 hpel[ 1]= tmp2t[0] + 64*(HTAPS_MAX/2-1);
00243 hpel[ 2]= src + 1;
00244
00245 hpel[ 4]= tmp2t[1];
00246 hpel[ 5]= tmp2t[2];
00247 hpel[ 6]= tmp2t[1] + 1;
00248
00249 hpel[ 8]= src + stride;
00250 hpel[ 9]= hpel[1] + 64;
00251 hpel[10]= hpel[8] + 1;
00252
00253 #define MC_STRIDE(x) (needs[x] ? 64 : stride)
00254
00255 if(b==15){
00256 int dxy = dx / 8 + dy / 8 * 4;
00257 const uint8_t *src1 = hpel[dxy ];
00258 const uint8_t *src2 = hpel[dxy + 1];
00259 const uint8_t *src3 = hpel[dxy + 4];
00260 const uint8_t *src4 = hpel[dxy + 5];
00261 int stride1 = MC_STRIDE(dxy);
00262 int stride2 = MC_STRIDE(dxy + 1);
00263 int stride3 = MC_STRIDE(dxy + 4);
00264 int stride4 = MC_STRIDE(dxy + 5);
00265 dx&=7;
00266 dy&=7;
00267 for(y=0; y < b_h; y++){
00268 for(x=0; x < b_w; x++){
00269 dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
00270 (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
00271 }
00272 src1+=stride1;
00273 src2+=stride2;
00274 src3+=stride3;
00275 src4+=stride4;
00276 dst +=stride;
00277 }
00278 }else{
00279 const uint8_t *src1= hpel[l];
00280 const uint8_t *src2= hpel[r];
00281 int stride1 = MC_STRIDE(l);
00282 int stride2 = MC_STRIDE(r);
00283 int a= weight[((dx&7) + (8*(dy&7)))];
00284 int b= 8-a;
00285 for(y=0; y < b_h; y++){
00286 for(x=0; x < b_w; x++){
00287 dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
00288 }
00289 src1+=stride1;
00290 src2+=stride2;
00291 dst +=stride;
00292 }
00293 }
00294 }
00295
00296 void ff_snow_pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
00297 if(block->type & BLOCK_INTRA){
00298 int x, y;
00299 const unsigned color = block->color[plane_index];
00300 const unsigned color4 = color*0x01010101;
00301 if(b_w==32){
00302 for(y=0; y < b_h; y++){
00303 *(uint32_t*)&dst[0 + y*stride]= color4;
00304 *(uint32_t*)&dst[4 + y*stride]= color4;
00305 *(uint32_t*)&dst[8 + y*stride]= color4;
00306 *(uint32_t*)&dst[12+ y*stride]= color4;
00307 *(uint32_t*)&dst[16+ y*stride]= color4;
00308 *(uint32_t*)&dst[20+ y*stride]= color4;
00309 *(uint32_t*)&dst[24+ y*stride]= color4;
00310 *(uint32_t*)&dst[28+ y*stride]= color4;
00311 }
00312 }else if(b_w==16){
00313 for(y=0; y < b_h; y++){
00314 *(uint32_t*)&dst[0 + y*stride]= color4;
00315 *(uint32_t*)&dst[4 + y*stride]= color4;
00316 *(uint32_t*)&dst[8 + y*stride]= color4;
00317 *(uint32_t*)&dst[12+ y*stride]= color4;
00318 }
00319 }else if(b_w==8){
00320 for(y=0; y < b_h; y++){
00321 *(uint32_t*)&dst[0 + y*stride]= color4;
00322 *(uint32_t*)&dst[4 + y*stride]= color4;
00323 }
00324 }else if(b_w==4){
00325 for(y=0; y < b_h; y++){
00326 *(uint32_t*)&dst[0 + y*stride]= color4;
00327 }
00328 }else{
00329 for(y=0; y < b_h; y++){
00330 for(x=0; x < b_w; x++){
00331 dst[x + y*stride]= color;
00332 }
00333 }
00334 }
00335 }else{
00336 uint8_t *src= s->last_picture[block->ref].data[plane_index];
00337 const int scale= plane_index ? (2*s->mv_scale)>>s->chroma_h_shift : 2*s->mv_scale;
00338 int mx= block->mx*scale;
00339 int my= block->my*scale;
00340 const int dx= mx&15;
00341 const int dy= my&15;
00342 const int tab_index= 3 - (b_w>>2) + (b_w>>4);
00343 sx += (mx>>4) - (HTAPS_MAX/2-1);
00344 sy += (my>>4) - (HTAPS_MAX/2-1);
00345 src += sx + sy*stride;
00346 if( (unsigned)sx >= w - b_w - (HTAPS_MAX-2)
00347 || (unsigned)sy >= h - b_h - (HTAPS_MAX-2)){
00348 s->dsp.emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1, sx, sy, w, h);
00349 src= tmp + MB_SIZE;
00350 }
00351
00352 av_assert2(s->chroma_h_shift == s->chroma_v_shift);
00353
00354
00355
00356 av_assert2(b_w>1 && b_h>1);
00357 av_assert2((tab_index>=0 && tab_index<4) || b_w==32);
00358 if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)) || !s->plane[plane_index].fast_mc )
00359 mc_block(&s->plane[plane_index], dst, src, stride, b_w, b_h, dx, dy);
00360 else if(b_w==32){
00361 int y;
00362 for(y=0; y<b_h; y+=16){
00363 s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
00364 s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
00365 }
00366 }else if(b_w==b_h)
00367 s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
00368 else if(b_w==2*b_h){
00369 s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
00370 s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
00371 }else{
00372 av_assert2(2*b_w==b_h);
00373 s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
00374 s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
00375 }
00376 }
00377 }
00378
00379 #define mca(dx,dy,b_w)\
00380 static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, int stride, int h){\
00381 av_assert2(h==b_w);\
00382 mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, stride, b_w, b_w, dx, dy);\
00383 }
00384
00385 mca( 0, 0,16)
00386 mca( 8, 0,16)
00387 mca( 0, 8,16)
00388 mca( 8, 8,16)
00389 mca( 0, 0,8)
00390 mca( 8, 0,8)
00391 mca( 0, 8,8)
00392 mca( 8, 8,8)
00393
00394 av_cold int ff_snow_common_init(AVCodecContext *avctx){
00395 SnowContext *s = avctx->priv_data;
00396 int width, height;
00397 int i, j;
00398
00399 s->avctx= avctx;
00400 s->max_ref_frames=1;
00401
00402 ff_dsputil_init(&s->dsp, avctx);
00403 ff_dwt_init(&s->dwt);
00404
00405 #define mcf(dx,dy)\
00406 s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
00407 s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
00408 s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
00409 s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
00410 s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
00411 s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
00412
00413 mcf( 0, 0)
00414 mcf( 4, 0)
00415 mcf( 8, 0)
00416 mcf(12, 0)
00417 mcf( 0, 4)
00418 mcf( 4, 4)
00419 mcf( 8, 4)
00420 mcf(12, 4)
00421 mcf( 0, 8)
00422 mcf( 4, 8)
00423 mcf( 8, 8)
00424 mcf(12, 8)
00425 mcf( 0,12)
00426 mcf( 4,12)
00427 mcf( 8,12)
00428 mcf(12,12)
00429
00430 #define mcfh(dx,dy)\
00431 s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
00432 s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
00433 mc_block_hpel ## dx ## dy ## 16;\
00434 s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
00435 s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
00436 mc_block_hpel ## dx ## dy ## 8;
00437
00438 mcfh(0, 0)
00439 mcfh(8, 0)
00440 mcfh(0, 8)
00441 mcfh(8, 8)
00442
00443 init_qexp();
00444
00445
00446
00447 width= s->avctx->width;
00448 height= s->avctx->height;
00449
00450 FF_ALLOCZ_OR_GOTO(avctx, s->spatial_idwt_buffer, width * height * sizeof(IDWTELEM), fail);
00451 FF_ALLOCZ_OR_GOTO(avctx, s->spatial_dwt_buffer, width * height * sizeof(DWTELEM), fail);
00452 FF_ALLOCZ_OR_GOTO(avctx, s->temp_dwt_buffer, width * sizeof(DWTELEM), fail);
00453 FF_ALLOCZ_OR_GOTO(avctx, s->temp_idwt_buffer, width * sizeof(IDWTELEM), fail);
00454 FF_ALLOC_OR_GOTO(avctx, s->run_buffer, ((width + 1) >> 1) * ((height + 1) >> 1) * sizeof(*s->run_buffer), fail);
00455
00456 for(i=0; i<MAX_REF_FRAMES; i++)
00457 for(j=0; j<MAX_REF_FRAMES; j++)
00458 ff_scale_mv_ref[i][j] = 256*(i+1)/(j+1);
00459
00460 return 0;
00461 fail:
00462 return AVERROR(ENOMEM);
00463 }
00464
00465 int ff_snow_common_init_after_header(AVCodecContext *avctx) {
00466 SnowContext *s = avctx->priv_data;
00467 int plane_index, level, orientation;
00468 int ret, emu_buf_size;
00469
00470 if(!s->scratchbuf) {
00471 if ((ret = s->avctx->get_buffer(s->avctx, &s->mconly_picture)) < 0) {
00472 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00473 return ret;
00474 }
00475 FF_ALLOCZ_OR_GOTO(avctx, s->scratchbuf, FFMAX(s->mconly_picture.linesize[0], 2*avctx->width+256)*7*MB_SIZE, fail);
00476 emu_buf_size = FFMAX(s->mconly_picture.linesize[0], 2*avctx->width+256) * (2 * MB_SIZE + HTAPS_MAX - 1);
00477 FF_ALLOC_OR_GOTO(avctx, s->emu_edge_buffer, emu_buf_size, fail);
00478 }
00479
00480 if(s->mconly_picture.format != avctx->pix_fmt) {
00481 av_log(avctx, AV_LOG_ERROR, "pixel format changed\n");
00482 return AVERROR_INVALIDDATA;
00483 }
00484
00485 for(plane_index=0; plane_index<3; plane_index++){
00486 int w= s->avctx->width;
00487 int h= s->avctx->height;
00488
00489 if(plane_index){
00490 w>>= s->chroma_h_shift;
00491 h>>= s->chroma_v_shift;
00492 }
00493 s->plane[plane_index].width = w;
00494 s->plane[plane_index].height= h;
00495
00496 for(level=s->spatial_decomposition_count-1; level>=0; level--){
00497 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00498 SubBand *b= &s->plane[plane_index].band[level][orientation];
00499
00500 b->buf= s->spatial_dwt_buffer;
00501 b->level= level;
00502 b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
00503 b->width = (w + !(orientation&1))>>1;
00504 b->height= (h + !(orientation>1))>>1;
00505
00506 b->stride_line = 1 << (s->spatial_decomposition_count - level);
00507 b->buf_x_offset = 0;
00508 b->buf_y_offset = 0;
00509
00510 if(orientation&1){
00511 b->buf += (w+1)>>1;
00512 b->buf_x_offset = (w+1)>>1;
00513 }
00514 if(orientation>1){
00515 b->buf += b->stride>>1;
00516 b->buf_y_offset = b->stride_line >> 1;
00517 }
00518 b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
00519
00520 if(level)
00521 b->parent= &s->plane[plane_index].band[level-1][orientation];
00522
00523 av_freep(&b->x_coeff);
00524 b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
00525 }
00526 w= (w+1)>>1;
00527 h= (h+1)>>1;
00528 }
00529 }
00530
00531 return 0;
00532 fail:
00533 return AVERROR(ENOMEM);
00534 }
00535
00536 #define USE_HALFPEL_PLANE 0
00537
00538 static void halfpel_interpol(SnowContext *s, uint8_t *halfpel[4][4], AVFrame *frame){
00539 int p,x,y;
00540
00541 for(p=0; p<3; p++){
00542 int is_chroma= !!p;
00543 int w= is_chroma ? s->avctx->width >>s->chroma_h_shift : s->avctx->width;
00544 int h= is_chroma ? s->avctx->height>>s->chroma_v_shift : s->avctx->height;
00545 int ls= frame->linesize[p];
00546 uint8_t *src= frame->data[p];
00547
00548 halfpel[1][p] = (uint8_t*) av_malloc(ls * (h + 2 * EDGE_WIDTH)) + EDGE_WIDTH * (1 + ls);
00549 halfpel[2][p] = (uint8_t*) av_malloc(ls * (h + 2 * EDGE_WIDTH)) + EDGE_WIDTH * (1 + ls);
00550 halfpel[3][p] = (uint8_t*) av_malloc(ls * (h + 2 * EDGE_WIDTH)) + EDGE_WIDTH * (1 + ls);
00551
00552 halfpel[0][p]= src;
00553 for(y=0; y<h; y++){
00554 for(x=0; x<w; x++){
00555 int i= y*ls + x;
00556
00557 halfpel[1][p][i]= (20*(src[i] + src[i+1]) - 5*(src[i-1] + src[i+2]) + (src[i-2] + src[i+3]) + 16 )>>5;
00558 }
00559 }
00560 for(y=0; y<h; y++){
00561 for(x=0; x<w; x++){
00562 int i= y*ls + x;
00563
00564 halfpel[2][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
00565 }
00566 }
00567 src= halfpel[1][p];
00568 for(y=0; y<h; y++){
00569 for(x=0; x<w; x++){
00570 int i= y*ls + x;
00571
00572 halfpel[3][p][i]= (20*(src[i] + src[i+ls]) - 5*(src[i-ls] + src[i+2*ls]) + (src[i-2*ls] + src[i+3*ls]) + 16 )>>5;
00573 }
00574 }
00575
00576
00577 }
00578 }
00579
00580 void ff_snow_release_buffer(AVCodecContext *avctx)
00581 {
00582 SnowContext *s = avctx->priv_data;
00583 int i;
00584
00585 if(s->last_picture[s->max_ref_frames-1].data[0]){
00586 avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
00587 for(i=0; i<9; i++)
00588 if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3])
00589 av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture.linesize[i%3]));
00590 }
00591 }
00592
00593 int ff_snow_frame_start(SnowContext *s){
00594 AVFrame tmp;
00595 int w= s->avctx->width;
00596 int h= s->avctx->height;
00597
00598 if (s->current_picture.data[0] && !(s->avctx->flags&CODEC_FLAG_EMU_EDGE)) {
00599 s->dsp.draw_edges(s->current_picture.data[0],
00600 s->current_picture.linesize[0], w , h ,
00601 EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
00602 s->dsp.draw_edges(s->current_picture.data[1],
00603 s->current_picture.linesize[1], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
00604 EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
00605 s->dsp.draw_edges(s->current_picture.data[2],
00606 s->current_picture.linesize[2], w>>s->chroma_h_shift, h>>s->chroma_v_shift,
00607 EDGE_WIDTH>>s->chroma_h_shift, EDGE_WIDTH>>s->chroma_v_shift, EDGE_TOP | EDGE_BOTTOM);
00608 }
00609
00610 ff_snow_release_buffer(s->avctx);
00611
00612 tmp= s->last_picture[s->max_ref_frames-1];
00613 memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
00614 memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
00615 if(USE_HALFPEL_PLANE && s->current_picture.data[0])
00616 halfpel_interpol(s, s->halfpel_plane[0], &s->current_picture);
00617 s->last_picture[0]= s->current_picture;
00618 s->current_picture= tmp;
00619
00620 if(s->keyframe){
00621 s->ref_frames= 0;
00622 }else{
00623 int i;
00624 for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
00625 if(i && s->last_picture[i-1].key_frame)
00626 break;
00627 s->ref_frames= i;
00628 if(s->ref_frames==0){
00629 av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
00630 return -1;
00631 }
00632 }
00633
00634 s->current_picture.reference= 3;
00635 if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
00636 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00637 return -1;
00638 }
00639
00640 s->current_picture.key_frame= s->keyframe;
00641
00642 return 0;
00643 }
00644
00645 av_cold void ff_snow_common_end(SnowContext *s)
00646 {
00647 int plane_index, level, orientation, i;
00648
00649 av_freep(&s->spatial_dwt_buffer);
00650 av_freep(&s->temp_dwt_buffer);
00651 av_freep(&s->spatial_idwt_buffer);
00652 av_freep(&s->temp_idwt_buffer);
00653 av_freep(&s->run_buffer);
00654
00655 s->m.me.temp= NULL;
00656 av_freep(&s->m.me.scratchpad);
00657 av_freep(&s->m.me.map);
00658 av_freep(&s->m.me.score_map);
00659 av_freep(&s->m.obmc_scratchpad);
00660
00661 av_freep(&s->block);
00662 av_freep(&s->scratchbuf);
00663 av_freep(&s->emu_edge_buffer);
00664
00665 for(i=0; i<MAX_REF_FRAMES; i++){
00666 av_freep(&s->ref_mvs[i]);
00667 av_freep(&s->ref_scores[i]);
00668 if(s->last_picture[i].data[0]) {
00669 av_assert0(s->last_picture[i].data[0] != s->current_picture.data[0]);
00670 s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
00671 }
00672 }
00673
00674 for(plane_index=0; plane_index<3; plane_index++){
00675 for(level=s->spatial_decomposition_count-1; level>=0; level--){
00676 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00677 SubBand *b= &s->plane[plane_index].band[level][orientation];
00678
00679 av_freep(&b->x_coeff);
00680 }
00681 }
00682 }
00683 if (s->mconly_picture.data[0])
00684 s->avctx->release_buffer(s->avctx, &s->mconly_picture);
00685 if (s->current_picture.data[0])
00686 s->avctx->release_buffer(s->avctx, &s->current_picture);
00687 }