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 "internal.h"
00026 #include "dsputil.h"
00027 #include "dwt.h"
00028 #include "snow.h"
00029
00030 #include "rangecoder.h"
00031 #include "mathops.h"
00032
00033 #include "mpegvideo.h"
00034 #include "h263.h"
00035
00036 #undef NDEBUG
00037 #include <assert.h>
00038
00039 #define QUANTIZE2 0
00040
00041 #if QUANTIZE2==1
00042 #define Q2_STEP 8
00043
00044 static void find_sse(SnowContext *s, Plane *p, int *score, int score_stride, IDWTELEM *r0, IDWTELEM *r1, int level, int orientation){
00045 SubBand *b= &p->band[level][orientation];
00046 int x, y;
00047 int xo=0;
00048 int yo=0;
00049 int step= 1 << (s->spatial_decomposition_count - level);
00050
00051 if(orientation&1)
00052 xo= step>>1;
00053 if(orientation&2)
00054 yo= step>>1;
00055
00056
00057
00058 memset(score, 0, sizeof(*score)*score_stride*((p->height + Q2_STEP-1)/Q2_STEP));
00059 for(y=0; y<p->height; y++){
00060 for(x=0; x<p->width; x++){
00061 int sx= (x-xo + step/2) / step / Q2_STEP;
00062 int sy= (y-yo + step/2) / step / Q2_STEP;
00063 int v= r0[x + y*p->width] - r1[x + y*p->width];
00064 assert(sx>=0 && sy>=0 && sx < score_stride);
00065 v= ((v+8)>>4)<<4;
00066 score[sx + sy*score_stride] += v*v;
00067 assert(score[sx + sy*score_stride] >= 0);
00068 }
00069 }
00070 }
00071
00072 static void dequantize_all(SnowContext *s, Plane *p, IDWTELEM *buffer, int width, int height){
00073 int level, orientation;
00074
00075 for(level=0; level<s->spatial_decomposition_count; level++){
00076 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00077 SubBand *b= &p->band[level][orientation];
00078 IDWTELEM *dst= buffer + (b->ibuf - s->spatial_idwt_buffer);
00079
00080 dequantize(s, b, dst, b->stride);
00081 }
00082 }
00083 }
00084
00085 static void dwt_quantize(SnowContext *s, Plane *p, DWTELEM *buffer, int width, int height, int stride, int type){
00086 int level, orientation, ys, xs, x, y, pass;
00087 IDWTELEM best_dequant[height * stride];
00088 IDWTELEM idwt2_buffer[height * stride];
00089 const int score_stride= (width + 10)/Q2_STEP;
00090 int best_score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP];
00091 int score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP];
00092 int threshold= (s->m.lambda * s->m.lambda) >> 6;
00093
00094
00095
00096
00097 ff_spatial_dwt(buffer, width, height, stride, type, s->spatial_decomposition_count);
00098
00099 for(level=0; level<s->spatial_decomposition_count; level++){
00100 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00101 SubBand *b= &p->band[level][orientation];
00102 IDWTELEM *dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
00103 DWTELEM *src= buffer + (b-> buf - s->spatial_dwt_buffer);
00104 assert(src == b->buf);
00105
00106 quantize(s, b, dst, src, b->stride, s->qbias);
00107 }
00108 }
00109 for(pass=0; pass<1; pass++){
00110 if(s->qbias == 0)
00111 continue;
00112 for(level=0; level<s->spatial_decomposition_count; level++){
00113 for(orientation=level ? 1 : 0; orientation<4; orientation++){
00114 SubBand *b= &p->band[level][orientation];
00115 IDWTELEM *dst= idwt2_buffer + (b->ibuf - s->spatial_idwt_buffer);
00116 IDWTELEM *best_dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
00117
00118 for(ys= 0; ys<Q2_STEP; ys++){
00119 for(xs= 0; xs<Q2_STEP; xs++){
00120 memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
00121 dequantize_all(s, p, idwt2_buffer, width, height);
00122 ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
00123 find_sse(s, p, best_score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
00124 memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
00125 for(y=ys; y<b->height; y+= Q2_STEP){
00126 for(x=xs; x<b->width; x+= Q2_STEP){
00127 if(dst[x + y*b->stride]<0) dst[x + y*b->stride]++;
00128 if(dst[x + y*b->stride]>0) dst[x + y*b->stride]--;
00129
00130 }
00131 }
00132 dequantize_all(s, p, idwt2_buffer, width, height);
00133 ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
00134 find_sse(s, p, score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
00135 for(y=ys; y<b->height; y+= Q2_STEP){
00136 for(x=xs; x<b->width; x+= Q2_STEP){
00137 int score_idx= x/Q2_STEP + (y/Q2_STEP)*score_stride;
00138 if(score[score_idx] <= best_score[score_idx] + threshold){
00139 best_score[score_idx]= score[score_idx];
00140 if(best_dst[x + y*b->stride]<0) best_dst[x + y*b->stride]++;
00141 if(best_dst[x + y*b->stride]>0) best_dst[x + y*b->stride]--;
00142
00143 }
00144 }
00145 }
00146 }
00147 }
00148 }
00149 }
00150 }
00151 memcpy(s->spatial_idwt_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
00152 }
00153
00154 #endif
00155
00156 static av_cold int encode_init(AVCodecContext *avctx)
00157 {
00158 SnowContext *s = avctx->priv_data;
00159 int plane_index;
00160
00161 if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
00162 av_log(avctx, AV_LOG_ERROR, "This codec is under development, files encoded with it may not be decodable with future versions!!!\n"
00163 "Use vstrict=-2 / -strict -2 to use it anyway.\n");
00164 return -1;
00165 }
00166
00167 if(avctx->prediction_method == DWT_97
00168 && (avctx->flags & CODEC_FLAG_QSCALE)
00169 && avctx->global_quality == 0){
00170 av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
00171 return -1;
00172 }
00173
00174 s->spatial_decomposition_type= avctx->prediction_method;
00175
00176 s->mv_scale = (avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
00177 s->block_max_depth= (avctx->flags & CODEC_FLAG_4MV ) ? 1 : 0;
00178
00179 for(plane_index=0; plane_index<3; plane_index++){
00180 s->plane[plane_index].diag_mc= 1;
00181 s->plane[plane_index].htaps= 6;
00182 s->plane[plane_index].hcoeff[0]= 40;
00183 s->plane[plane_index].hcoeff[1]= -10;
00184 s->plane[plane_index].hcoeff[2]= 2;
00185 s->plane[plane_index].fast_mc= 1;
00186 }
00187
00188 ff_snow_common_init(avctx);
00189 ff_snow_alloc_blocks(s);
00190
00191 s->version=0;
00192
00193 s->m.avctx = avctx;
00194 s->m.flags = avctx->flags;
00195 s->m.bit_rate= avctx->bit_rate;
00196
00197 s->m.me.temp =
00198 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
00199 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
00200 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
00201 s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
00202 ff_h263_encode_init(&s->m);
00203
00204 s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
00205
00206 if(avctx->flags&CODEC_FLAG_PASS1){
00207 if(!avctx->stats_out)
00208 avctx->stats_out = av_mallocz(256);
00209 }
00210 if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
00211 if(ff_rate_control_init(&s->m) < 0)
00212 return -1;
00213 }
00214 s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
00215
00216 avctx->coded_frame= &s->current_picture;
00217 switch(avctx->pix_fmt){
00218
00219
00220 case PIX_FMT_YUV420P:
00221
00222
00223
00224 s->colorspace_type= 0;
00225 break;
00226
00227
00228
00229 default:
00230 av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
00231 return -1;
00232 }
00233
00234 s->chroma_h_shift= 1;
00235 s->chroma_v_shift= 1;
00236
00237 ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
00238 ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
00239
00240 s->avctx->get_buffer(s->avctx, &s->input_picture);
00241
00242 if(s->avctx->me_method == ME_ITER){
00243 int i;
00244 int size= s->b_width * s->b_height << 2*s->block_max_depth;
00245 for(i=0; i<s->max_ref_frames; i++){
00246 s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
00247 s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
00248 }
00249 }
00250
00251 s->runs = av_malloc(avctx->width * avctx->height * sizeof(*s->runs));
00252
00253 return 0;
00254 }
00255
00256
00257 static int pix_sum(uint8_t * pix, int line_size, int w)
00258 {
00259 int s, i, j;
00260
00261 s = 0;
00262 for (i = 0; i < w; i++) {
00263 for (j = 0; j < w; j++) {
00264 s += pix[0];
00265 pix ++;
00266 }
00267 pix += line_size - w;
00268 }
00269 return s;
00270 }
00271
00272
00273 static int pix_norm1(uint8_t * pix, int line_size, int w)
00274 {
00275 int s, i, j;
00276 uint32_t *sq = ff_squareTbl + 256;
00277
00278 s = 0;
00279 for (i = 0; i < w; i++) {
00280 for (j = 0; j < w; j ++) {
00281 s += sq[pix[0]];
00282 pix ++;
00283 }
00284 pix += line_size - w;
00285 }
00286 return s;
00287 }
00288
00289
00290 #define P_LEFT P[1]
00291 #define P_TOP P[2]
00292 #define P_TOPRIGHT P[3]
00293 #define P_MEDIAN P[4]
00294 #define P_MV1 P[9]
00295 #define FLAG_QPEL 1 //must be 1
00296
00297 static int encode_q_branch(SnowContext *s, int level, int x, int y){
00298 uint8_t p_buffer[1024];
00299 uint8_t i_buffer[1024];
00300 uint8_t p_state[sizeof(s->block_state)];
00301 uint8_t i_state[sizeof(s->block_state)];
00302 RangeCoder pc, ic;
00303 uint8_t *pbbak= s->c.bytestream;
00304 uint8_t *pbbak_start= s->c.bytestream_start;
00305 int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
00306 const int w= s->b_width << s->block_max_depth;
00307 const int h= s->b_height << s->block_max_depth;
00308 const int rem_depth= s->block_max_depth - level;
00309 const int index= (x + y*w) << rem_depth;
00310 const int block_w= 1<<(LOG2_MB_SIZE - level);
00311 int trx= (x+1)<<rem_depth;
00312 int try= (y+1)<<rem_depth;
00313 const BlockNode *left = x ? &s->block[index-1] : &null_block;
00314 const BlockNode *top = y ? &s->block[index-w] : &null_block;
00315 const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
00316 const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
00317 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
00318 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl;
00319 int pl = left->color[0];
00320 int pcb= left->color[1];
00321 int pcr= left->color[2];
00322 int pmx, pmy;
00323 int mx=0, my=0;
00324 int l,cr,cb;
00325 const int stride= s->current_picture.linesize[0];
00326 const int uvstride= s->current_picture.linesize[1];
00327 uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y* stride)*block_w,
00328 s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
00329 s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
00330 int P[10][2];
00331 int16_t last_mv[3][2];
00332 int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL);
00333 const int shift= 1+qpel;
00334 MotionEstContext *c= &s->m.me;
00335 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
00336 int mx_context= av_log2(2*FFABS(left->mx - top->mx));
00337 int my_context= av_log2(2*FFABS(left->my - top->my));
00338 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
00339 int ref, best_ref, ref_score, ref_mx, ref_my;
00340
00341 assert(sizeof(s->block_state) >= 256);
00342 if(s->keyframe){
00343 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
00344 return 0;
00345 }
00346
00347
00348
00349 P_LEFT[0]= left->mx;
00350 P_LEFT[1]= left->my;
00351 P_TOP [0]= top->mx;
00352 P_TOP [1]= top->my;
00353 P_TOPRIGHT[0]= tr->mx;
00354 P_TOPRIGHT[1]= tr->my;
00355
00356 last_mv[0][0]= s->block[index].mx;
00357 last_mv[0][1]= s->block[index].my;
00358 last_mv[1][0]= right->mx;
00359 last_mv[1][1]= right->my;
00360 last_mv[2][0]= bottom->mx;
00361 last_mv[2][1]= bottom->my;
00362
00363 s->m.mb_stride=2;
00364 s->m.mb_x=
00365 s->m.mb_y= 0;
00366 c->skip= 0;
00367
00368 assert(c-> stride == stride);
00369 assert(c->uvstride == uvstride);
00370
00371 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
00372 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
00373 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
00374 c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
00375
00376 c->xmin = - x*block_w - 16+3;
00377 c->ymin = - y*block_w - 16+3;
00378 c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
00379 c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
00380
00381 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
00382 if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
00383 if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
00384 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
00385 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
00386 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
00387 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
00388
00389 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
00390 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
00391
00392 if (!y) {
00393 c->pred_x= P_LEFT[0];
00394 c->pred_y= P_LEFT[1];
00395 } else {
00396 c->pred_x = P_MEDIAN[0];
00397 c->pred_y = P_MEDIAN[1];
00398 }
00399
00400 score= INT_MAX;
00401 best_ref= 0;
00402 for(ref=0; ref<s->ref_frames; ref++){
00403 init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
00404
00405 ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, 0, last_mv,
00406 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
00407
00408 assert(ref_mx >= c->xmin);
00409 assert(ref_mx <= c->xmax);
00410 assert(ref_my >= c->ymin);
00411 assert(ref_my <= c->ymax);
00412
00413 ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
00414 ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
00415 ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
00416 if(s->ref_mvs[ref]){
00417 s->ref_mvs[ref][index][0]= ref_mx;
00418 s->ref_mvs[ref][index][1]= ref_my;
00419 s->ref_scores[ref][index]= ref_score;
00420 }
00421 if(score > ref_score){
00422 score= ref_score;
00423 best_ref= ref;
00424 mx= ref_mx;
00425 my= ref_my;
00426 }
00427 }
00428
00429
00430
00431 base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
00432 pc= s->c;
00433 pc.bytestream_start=
00434 pc.bytestream= p_buffer;
00435 memcpy(p_state, s->block_state, sizeof(s->block_state));
00436
00437 if(level!=s->block_max_depth)
00438 put_rac(&pc, &p_state[4 + s_context], 1);
00439 put_rac(&pc, &p_state[1 + left->type + top->type], 0);
00440 if(s->ref_frames > 1)
00441 put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
00442 pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
00443 put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
00444 put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
00445 p_len= pc.bytestream - pc.bytestream_start;
00446 score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
00447
00448 block_s= block_w*block_w;
00449 sum = pix_sum(current_data[0], stride, block_w);
00450 l= (sum + block_s/2)/block_s;
00451 iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
00452
00453 block_s= block_w*block_w>>2;
00454 sum = pix_sum(current_data[1], uvstride, block_w>>1);
00455 cb= (sum + block_s/2)/block_s;
00456
00457 sum = pix_sum(current_data[2], uvstride, block_w>>1);
00458 cr= (sum + block_s/2)/block_s;
00459
00460
00461 ic= s->c;
00462 ic.bytestream_start=
00463 ic.bytestream= i_buffer;
00464 memcpy(i_state, s->block_state, sizeof(s->block_state));
00465 if(level!=s->block_max_depth)
00466 put_rac(&ic, &i_state[4 + s_context], 1);
00467 put_rac(&ic, &i_state[1 + left->type + top->type], 1);
00468 put_symbol(&ic, &i_state[32], l-pl , 1);
00469 put_symbol(&ic, &i_state[64], cb-pcb, 1);
00470 put_symbol(&ic, &i_state[96], cr-pcr, 1);
00471 i_len= ic.bytestream - ic.bytestream_start;
00472 iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
00473
00474
00475 assert(iscore < 255*255*256 + s->lambda2*10);
00476 assert(iscore >= 0);
00477 assert(l>=0 && l<=255);
00478 assert(pl>=0 && pl<=255);
00479
00480 if(level==0){
00481 int varc= iscore >> 8;
00482 int vard= score >> 8;
00483 if (vard <= 64 || vard < varc)
00484 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
00485 else
00486 c->scene_change_score+= s->m.qscale;
00487 }
00488
00489 if(level!=s->block_max_depth){
00490 put_rac(&s->c, &s->block_state[4 + s_context], 0);
00491 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
00492 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
00493 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
00494 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
00495 score2+= s->lambda2>>FF_LAMBDA_SHIFT;
00496
00497 if(score2 < score && score2 < iscore)
00498 return score2;
00499 }
00500
00501 if(iscore < score){
00502 pred_mv(s, &pmx, &pmy, 0, left, top, tr);
00503 memcpy(pbbak, i_buffer, i_len);
00504 s->c= ic;
00505 s->c.bytestream_start= pbbak_start;
00506 s->c.bytestream= pbbak + i_len;
00507 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
00508 memcpy(s->block_state, i_state, sizeof(s->block_state));
00509 return iscore;
00510 }else{
00511 memcpy(pbbak, p_buffer, p_len);
00512 s->c= pc;
00513 s->c.bytestream_start= pbbak_start;
00514 s->c.bytestream= pbbak + p_len;
00515 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
00516 memcpy(s->block_state, p_state, sizeof(s->block_state));
00517 return score;
00518 }
00519 }
00520
00521 static void encode_q_branch2(SnowContext *s, int level, int x, int y){
00522 const int w= s->b_width << s->block_max_depth;
00523 const int rem_depth= s->block_max_depth - level;
00524 const int index= (x + y*w) << rem_depth;
00525 int trx= (x+1)<<rem_depth;
00526 BlockNode *b= &s->block[index];
00527 const BlockNode *left = x ? &s->block[index-1] : &null_block;
00528 const BlockNode *top = y ? &s->block[index-w] : &null_block;
00529 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
00530 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl;
00531 int pl = left->color[0];
00532 int pcb= left->color[1];
00533 int pcr= left->color[2];
00534 int pmx, pmy;
00535 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
00536 int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
00537 int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
00538 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
00539
00540 if(s->keyframe){
00541 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
00542 return;
00543 }
00544
00545 if(level!=s->block_max_depth){
00546 if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
00547 put_rac(&s->c, &s->block_state[4 + s_context], 1);
00548 }else{
00549 put_rac(&s->c, &s->block_state[4 + s_context], 0);
00550 encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
00551 encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
00552 encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
00553 encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
00554 return;
00555 }
00556 }
00557 if(b->type & BLOCK_INTRA){
00558 pred_mv(s, &pmx, &pmy, 0, left, top, tr);
00559 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
00560 put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
00561 put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
00562 put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
00563 set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
00564 }else{
00565 pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
00566 put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
00567 if(s->ref_frames > 1)
00568 put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
00569 put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
00570 put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
00571 set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
00572 }
00573 }
00574
00575 static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
00576 int i, x2, y2;
00577 Plane *p= &s->plane[plane_index];
00578 const int block_size = MB_SIZE >> s->block_max_depth;
00579 const int block_w = plane_index ? block_size/2 : block_size;
00580 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+1] : ff_obmc_tab[s->block_max_depth];
00581 const int obmc_stride= plane_index ? block_size : 2*block_size;
00582 const int ref_stride= s->current_picture.linesize[plane_index];
00583 uint8_t *src= s-> input_picture.data[plane_index];
00584 IDWTELEM *dst= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
00585 const int b_stride = s->b_width << s->block_max_depth;
00586 const int w= p->width;
00587 const int h= p->height;
00588 int index= mb_x + mb_y*b_stride;
00589 BlockNode *b= &s->block[index];
00590 BlockNode backup= *b;
00591 int ab=0;
00592 int aa=0;
00593
00594 b->type|= BLOCK_INTRA;
00595 b->color[plane_index]= 0;
00596 memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
00597
00598 for(i=0; i<4; i++){
00599 int mb_x2= mb_x + (i &1) - 1;
00600 int mb_y2= mb_y + (i>>1) - 1;
00601 int x= block_w*mb_x2 + block_w/2;
00602 int y= block_w*mb_y2 + block_w/2;
00603
00604 add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
00605 x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
00606
00607 for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
00608 for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
00609 int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
00610 int obmc_v= obmc[index];
00611 int d;
00612 if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
00613 if(x<0) obmc_v += obmc[index + block_w];
00614 if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
00615 if(x+block_w>w) obmc_v += obmc[index - block_w];
00616
00617
00618 d = -dst[index] + (1<<(FRAC_BITS-1));
00619 dst[index] = d;
00620 ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
00621 aa += obmc_v * obmc_v;
00622 }
00623 }
00624 }
00625 *b= backup;
00626
00627 return av_clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255);
00628 }
00629
00630 static inline int get_block_bits(SnowContext *s, int x, int y, int w){
00631 const int b_stride = s->b_width << s->block_max_depth;
00632 const int b_height = s->b_height<< s->block_max_depth;
00633 int index= x + y*b_stride;
00634 const BlockNode *b = &s->block[index];
00635 const BlockNode *left = x ? &s->block[index-1] : &null_block;
00636 const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
00637 const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
00638 const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
00639 int dmx, dmy;
00640
00641
00642
00643 if(x<0 || x>=b_stride || y>=b_height)
00644 return 0;
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654 if(b->type & BLOCK_INTRA){
00655 return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
00656 + av_log2(2*FFABS(left->color[1] - b->color[1]))
00657 + av_log2(2*FFABS(left->color[2] - b->color[2])));
00658 }else{
00659 pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
00660 dmx-= b->mx;
00661 dmy-= b->my;
00662 return 2*(1 + av_log2(2*FFABS(dmx))
00663 + av_log2(2*FFABS(dmy))
00664 + av_log2(2*b->ref));
00665 }
00666 }
00667
00668 static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
00669 Plane *p= &s->plane[plane_index];
00670 const int block_size = MB_SIZE >> s->block_max_depth;
00671 const int block_w = plane_index ? block_size/2 : block_size;
00672 const int obmc_stride= plane_index ? block_size : 2*block_size;
00673 const int ref_stride= s->current_picture.linesize[plane_index];
00674 uint8_t *dst= s->current_picture.data[plane_index];
00675 uint8_t *src= s-> input_picture.data[plane_index];
00676 IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
00677 uint8_t *cur = s->scratchbuf;
00678 uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];
00679 const int b_stride = s->b_width << s->block_max_depth;
00680 const int b_height = s->b_height<< s->block_max_depth;
00681 const int w= p->width;
00682 const int h= p->height;
00683 int distortion;
00684 int rate= 0;
00685 const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
00686 int sx= block_w*mb_x - block_w/2;
00687 int sy= block_w*mb_y - block_w/2;
00688 int x0= FFMAX(0,-sx);
00689 int y0= FFMAX(0,-sy);
00690 int x1= FFMIN(block_w*2, w-sx);
00691 int y1= FFMIN(block_w*2, h-sy);
00692 int i,x,y;
00693
00694 ff_snow_pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_w*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);
00695
00696 for(y=y0; y<y1; y++){
00697 const uint8_t *obmc1= obmc_edged + y*obmc_stride;
00698 const IDWTELEM *pred1 = pred + y*obmc_stride;
00699 uint8_t *cur1 = cur + y*ref_stride;
00700 uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
00701 for(x=x0; x<x1; x++){
00702 #if FRAC_BITS >= LOG2_OBMC_MAX
00703 int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
00704 #else
00705 int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
00706 #endif
00707 v = (v + pred1[x]) >> FRAC_BITS;
00708 if(v&(~255)) v= ~(v>>31);
00709 dst1[x] = v;
00710 }
00711 }
00712
00713
00714 if(LOG2_OBMC_MAX == 8
00715 && (mb_x == 0 || mb_x == b_stride-1)
00716 && (mb_y == 0 || mb_y == b_height-1)){
00717 if(mb_x == 0)
00718 x1 = block_w;
00719 else
00720 x0 = block_w;
00721 if(mb_y == 0)
00722 y1 = block_w;
00723 else
00724 y0 = block_w;
00725 for(y=y0; y<y1; y++)
00726 memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
00727 }
00728
00729 if(block_w==16){
00730
00731
00732
00733
00734
00735
00736 if(s->avctx->me_cmp == FF_CMP_W97)
00737 distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
00738 else if(s->avctx->me_cmp == FF_CMP_W53)
00739 distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
00740 else{
00741 distortion = 0;
00742 for(i=0; i<4; i++){
00743 int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
00744 distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
00745 }
00746 }
00747 }else{
00748 assert(block_w==8);
00749 distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
00750 }
00751
00752 if(plane_index==0){
00753 for(i=0; i<4; i++){
00754
00755
00756
00757
00758 rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
00759 }
00760 if(mb_x == b_stride-2)
00761 rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
00762 }
00763 return distortion + rate*penalty_factor;
00764 }
00765
00766 static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
00767 int i, y2;
00768 Plane *p= &s->plane[plane_index];
00769 const int block_size = MB_SIZE >> s->block_max_depth;
00770 const int block_w = plane_index ? block_size/2 : block_size;
00771 const uint8_t *obmc = plane_index ? ff_obmc_tab[s->block_max_depth+1] : ff_obmc_tab[s->block_max_depth];
00772 const int obmc_stride= plane_index ? block_size : 2*block_size;
00773 const int ref_stride= s->current_picture.linesize[plane_index];
00774 uint8_t *dst= s->current_picture.data[plane_index];
00775 uint8_t *src= s-> input_picture.data[plane_index];
00776
00777
00778 static IDWTELEM zero_dst[4096];
00779 const int b_stride = s->b_width << s->block_max_depth;
00780 const int w= p->width;
00781 const int h= p->height;
00782 int distortion= 0;
00783 int rate= 0;
00784 const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
00785
00786 for(i=0; i<9; i++){
00787 int mb_x2= mb_x + (i%3) - 1;
00788 int mb_y2= mb_y + (i/3) - 1;
00789 int x= block_w*mb_x2 + block_w/2;
00790 int y= block_w*mb_y2 + block_w/2;
00791
00792 add_yblock(s, 0, NULL, zero_dst, dst, obmc,
00793 x, y, block_w, block_w, w, h, 0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
00794
00795
00796 for(y2= y; y2<0; y2++)
00797 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
00798 for(y2= h; y2<y+block_w; y2++)
00799 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
00800 if(x<0){
00801 for(y2= y; y2<y+block_w; y2++)
00802 memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
00803 }
00804 if(x+block_w > w){
00805 for(y2= y; y2<y+block_w; y2++)
00806 memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
00807 }
00808
00809 assert(block_w== 8 || block_w==16);
00810 distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
00811 }
00812
00813 if(plane_index==0){
00814 BlockNode *b= &s->block[mb_x+mb_y*b_stride];
00815 int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
00816
00817
00818
00819
00820
00821
00822 if(merged)
00823 rate = get_block_bits(s, mb_x, mb_y, 2);
00824 for(i=merged?4:0; i<9; i++){
00825 static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
00826 rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
00827 }
00828 }
00829 return distortion + rate*penalty_factor;
00830 }
00831
00832 static int encode_subband_c0run(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
00833 const int w= b->width;
00834 const int h= b->height;
00835 int x, y;
00836
00837 if(1){
00838 int run=0;
00839 int run_index=0;
00840 int max_index;
00841
00842 for(y=0; y<h; y++){
00843 for(x=0; x<w; x++){
00844 int v, p=0;
00845 int l=0, lt=0, t=0, rt=0;
00846 v= src[x + y*stride];
00847
00848 if(y){
00849 t= src[x + (y-1)*stride];
00850 if(x){
00851 lt= src[x - 1 + (y-1)*stride];
00852 }
00853 if(x + 1 < w){
00854 rt= src[x + 1 + (y-1)*stride];
00855 }
00856 }
00857 if(x){
00858 l= src[x - 1 + y*stride];
00859
00860
00861
00862
00863 }
00864 if(parent){
00865 int px= x>>1;
00866 int py= y>>1;
00867 if(px<b->parent->width && py<b->parent->height)
00868 p= parent[px + py*2*stride];
00869 }
00870 if(!(l|lt|t|rt|p)){
00871 if(v){
00872 s->runs[run_index++]= run;
00873 run=0;
00874 }else{
00875 run++;
00876 }
00877 }
00878 }
00879 }
00880 max_index= run_index;
00881 s->runs[run_index++]= run;
00882 run_index=0;
00883 run= s->runs[run_index++];
00884
00885 put_symbol2(&s->c, b->state[30], max_index, 0);
00886 if(run_index <= max_index)
00887 put_symbol2(&s->c, b->state[1], run, 3);
00888
00889 for(y=0; y<h; y++){
00890 if(s->c.bytestream_end - s->c.bytestream < w*40){
00891 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
00892 return -1;
00893 }
00894 for(x=0; x<w; x++){
00895 int v, p=0;
00896 int l=0, lt=0, t=0, rt=0;
00897 v= src[x + y*stride];
00898
00899 if(y){
00900 t= src[x + (y-1)*stride];
00901 if(x){
00902 lt= src[x - 1 + (y-1)*stride];
00903 }
00904 if(x + 1 < w){
00905 rt= src[x + 1 + (y-1)*stride];
00906 }
00907 }
00908 if(x){
00909 l= src[x - 1 + y*stride];
00910
00911
00912
00913
00914 }
00915 if(parent){
00916 int px= x>>1;
00917 int py= y>>1;
00918 if(px<b->parent->width && py<b->parent->height)
00919 p= parent[px + py*2*stride];
00920 }
00921 if(l|lt|t|rt|p){
00922 int context= av_log2(3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
00923
00924 put_rac(&s->c, &b->state[0][context], !!v);
00925 }else{
00926 if(!run){
00927 run= s->runs[run_index++];
00928
00929 if(run_index <= max_index)
00930 put_symbol2(&s->c, b->state[1], run, 3);
00931 assert(v);
00932 }else{
00933 run--;
00934 assert(!v);
00935 }
00936 }
00937 if(v){
00938 int context= av_log2(3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
00939 int l2= 2*FFABS(l) + (l<0);
00940 int t2= 2*FFABS(t) + (t<0);
00941
00942 put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
00943 put_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l2&0xFF] + 3*ff_quant3bA[t2&0xFF]], v<0);
00944 }
00945 }
00946 }
00947 }
00948 return 0;
00949 }
00950
00951 static int encode_subband(SnowContext *s, SubBand *b, const IDWTELEM *src, const IDWTELEM *parent, int stride, int orientation){
00952
00953
00954 return encode_subband_c0run(s, b, src, parent, stride, orientation);
00955
00956 }
00957
00958 static av_always_inline int check_block(SnowContext *s, int mb_x, int mb_y, int p[3], int intra, const uint8_t *obmc_edged, int *best_rd){
00959 const int b_stride= s->b_width << s->block_max_depth;
00960 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
00961 BlockNode backup= *block;
00962 unsigned value;
00963 int rd, index;
00964
00965 assert(mb_x>=0 && mb_y>=0);
00966 assert(mb_x<b_stride);
00967
00968 if(intra){
00969 block->color[0] = p[0];
00970 block->color[1] = p[1];
00971 block->color[2] = p[2];
00972 block->type |= BLOCK_INTRA;
00973 }else{
00974 index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
00975 value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
00976 if(s->me_cache[index] == value)
00977 return 0;
00978 s->me_cache[index]= value;
00979
00980 block->mx= p[0];
00981 block->my= p[1];
00982 block->type &= ~BLOCK_INTRA;
00983 }
00984
00985 rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
00986
00987
00988 if(rd < *best_rd){
00989 *best_rd= rd;
00990 return 1;
00991 }else{
00992 *block= backup;
00993 return 0;
00994 }
00995 }
00996
00997
00998
00999 static av_always_inline int check_block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, const uint8_t *obmc_edged, int *best_rd){
01000 int p[2] = {p0, p1};
01001 return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
01002 }
01003
01004 static av_always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
01005 const int b_stride= s->b_width << s->block_max_depth;
01006 BlockNode *block= &s->block[mb_x + mb_y * b_stride];
01007 BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
01008 unsigned value;
01009 int rd, index;
01010
01011 assert(mb_x>=0 && mb_y>=0);
01012 assert(mb_x<b_stride);
01013 assert(((mb_x|mb_y)&1) == 0);
01014
01015 index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
01016 value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
01017 if(s->me_cache[index] == value)
01018 return 0;
01019 s->me_cache[index]= value;
01020
01021 block->mx= p0;
01022 block->my= p1;
01023 block->ref= ref;
01024 block->type &= ~BLOCK_INTRA;
01025 block[1]= block[b_stride]= block[b_stride+1]= *block;
01026
01027 rd= get_4block_rd(s, mb_x, mb_y, 0);
01028
01029
01030 if(rd < *best_rd){
01031 *best_rd= rd;
01032 return 1;
01033 }else{
01034 block[0]= backup[0];
01035 block[1]= backup[1];
01036 block[b_stride]= backup[2];
01037 block[b_stride+1]= backup[3];
01038 return 0;
01039 }
01040 }
01041
01042 static void iterative_me(SnowContext *s){
01043 int pass, mb_x, mb_y;
01044 const int b_width = s->b_width << s->block_max_depth;
01045 const int b_height= s->b_height << s->block_max_depth;
01046 const int b_stride= b_width;
01047 int color[3];
01048
01049 {
01050 RangeCoder r = s->c;
01051 uint8_t state[sizeof(s->block_state)];
01052 memcpy(state, s->block_state, sizeof(s->block_state));
01053 for(mb_y= 0; mb_y<s->b_height; mb_y++)
01054 for(mb_x= 0; mb_x<s->b_width; mb_x++)
01055 encode_q_branch(s, 0, mb_x, mb_y);
01056 s->c = r;
01057 memcpy(s->block_state, state, sizeof(s->block_state));
01058 }
01059
01060 for(pass=0; pass<25; pass++){
01061 int change= 0;
01062
01063 for(mb_y= 0; mb_y<b_height; mb_y++){
01064 for(mb_x= 0; mb_x<b_width; mb_x++){
01065 int dia_change, i, j, ref;
01066 int best_rd= INT_MAX, ref_rd;
01067 BlockNode backup, ref_b;
01068 const int index= mb_x + mb_y * b_stride;
01069 BlockNode *block= &s->block[index];
01070 BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
01071 BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
01072 BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
01073 BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
01074 BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
01075 BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
01076 BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
01077 BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
01078 const int b_w= (MB_SIZE >> s->block_max_depth);
01079 uint8_t obmc_edged[b_w*2][b_w*2];
01080
01081 if(pass && (block->type & BLOCK_OPT))
01082 continue;
01083 block->type |= BLOCK_OPT;
01084
01085 backup= *block;
01086
01087 if(!s->me_cache_generation)
01088 memset(s->me_cache, 0, sizeof(s->me_cache));
01089 s->me_cache_generation += 1<<22;
01090
01091
01092 {
01093 int x, y;
01094 memcpy(obmc_edged, ff_obmc_tab[s->block_max_depth], b_w*b_w*4);
01095 if(mb_x==0)
01096 for(y=0; y<b_w*2; y++)
01097 memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
01098 if(mb_x==b_stride-1)
01099 for(y=0; y<b_w*2; y++)
01100 memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
01101 if(mb_y==0){
01102 for(x=0; x<b_w*2; x++)
01103 obmc_edged[0][x] += obmc_edged[b_w-1][x];
01104 for(y=1; y<b_w; y++)
01105 memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
01106 }
01107 if(mb_y==b_height-1){
01108 for(x=0; x<b_w*2; x++)
01109 obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
01110 for(y=b_w; y<b_w*2-1; y++)
01111 memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
01112 }
01113 }
01114
01115
01116 if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
01117 uint8_t *src= s-> input_picture.data[0];
01118 uint8_t *dst= s->current_picture.data[0];
01119 const int stride= s->current_picture.linesize[0];
01120 const int block_w= MB_SIZE >> s->block_max_depth;
01121 const int sx= block_w*mb_x - block_w/2;
01122 const int sy= block_w*mb_y - block_w/2;
01123 const int w= s->plane[0].width;
01124 const int h= s->plane[0].height;
01125 int y;
01126
01127 for(y=sy; y<0; y++)
01128 memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
01129 for(y=h; y<sy+block_w*2; y++)
01130 memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
01131 if(sx<0){
01132 for(y=sy; y<sy+block_w*2; y++)
01133 memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
01134 }
01135 if(sx+block_w*2 > w){
01136 for(y=sy; y<sy+block_w*2; y++)
01137 memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
01138 }
01139 }
01140
01141
01142 for(i=0; i<3; i++)
01143 color[i]= get_dc(s, mb_x, mb_y, i);
01144
01145
01146 if(pass > 0 && (block->type&BLOCK_INTRA)){
01147 int color0[3]= {block->color[0], block->color[1], block->color[2]};
01148 check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
01149 }else
01150 check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
01151
01152 ref_b= *block;
01153 ref_rd= best_rd;
01154 for(ref=0; ref < s->ref_frames; ref++){
01155 int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
01156 if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2)
01157 continue;
01158 block->ref= ref;
01159 best_rd= INT_MAX;
01160
01161 check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
01162 check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
01163 if(tb)
01164 check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
01165 if(lb)
01166 check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
01167 if(rb)
01168 check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
01169 if(bb)
01170 check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
01171
01172
01173
01174 do{
01175 dia_change=0;
01176 for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
01177 for(j=0; j<i; j++){
01178 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
01179 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
01180 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
01181 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
01182 }
01183 }
01184 }while(dia_change);
01185
01186 do{
01187 static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
01188 dia_change=0;
01189 for(i=0; i<8; i++)
01190 dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
01191 }while(dia_change);
01192
01193
01194 mvr[0][0]= block->mx;
01195 mvr[0][1]= block->my;
01196 if(ref_rd > best_rd){
01197 ref_rd= best_rd;
01198 ref_b= *block;
01199 }
01200 }
01201 best_rd= ref_rd;
01202 *block= ref_b;
01203 check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
01204
01205 if(!same_block(block, &backup)){
01206 if(tb ) tb ->type &= ~BLOCK_OPT;
01207 if(lb ) lb ->type &= ~BLOCK_OPT;
01208 if(rb ) rb ->type &= ~BLOCK_OPT;
01209 if(bb ) bb ->type &= ~BLOCK_OPT;
01210 if(tlb) tlb->type &= ~BLOCK_OPT;
01211 if(trb) trb->type &= ~BLOCK_OPT;
01212 if(blb) blb->type &= ~BLOCK_OPT;
01213 if(brb) brb->type &= ~BLOCK_OPT;
01214 change ++;
01215 }
01216 }
01217 }
01218 av_log(s->avctx, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
01219 if(!change)
01220 break;
01221 }
01222
01223 if(s->block_max_depth == 1){
01224 int change= 0;
01225 for(mb_y= 0; mb_y<b_height; mb_y+=2){
01226 for(mb_x= 0; mb_x<b_width; mb_x+=2){
01227 int i;
01228 int best_rd, init_rd;
01229 const int index= mb_x + mb_y * b_stride;
01230 BlockNode *b[4];
01231
01232 b[0]= &s->block[index];
01233 b[1]= b[0]+1;
01234 b[2]= b[0]+b_stride;
01235 b[3]= b[2]+1;
01236 if(same_block(b[0], b[1]) &&
01237 same_block(b[0], b[2]) &&
01238 same_block(b[0], b[3]))
01239 continue;
01240
01241 if(!s->me_cache_generation)
01242 memset(s->me_cache, 0, sizeof(s->me_cache));
01243 s->me_cache_generation += 1<<22;
01244
01245 init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
01246
01247
01248 check_4block_inter(s, mb_x, mb_y,
01249 (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
01250 (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
01251
01252 for(i=0; i<4; i++)
01253 if(!(b[i]->type&BLOCK_INTRA))
01254 check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
01255
01256 if(init_rd != best_rd)
01257 change++;
01258 }
01259 }
01260 av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
01261 }
01262 }
01263
01264 static void encode_blocks(SnowContext *s, int search){
01265 int x, y;
01266 int w= s->b_width;
01267 int h= s->b_height;
01268
01269 if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
01270 iterative_me(s);
01271
01272 for(y=0; y<h; y++){
01273 if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){
01274 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
01275 return;
01276 }
01277 for(x=0; x<w; x++){
01278 if(s->avctx->me_method == ME_ITER || !search)
01279 encode_q_branch2(s, 0, x, y);
01280 else
01281 encode_q_branch (s, 0, x, y);
01282 }
01283 }
01284 }
01285
01286 static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
01287 const int w= b->width;
01288 const int h= b->height;
01289 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
01290 const int qmul= ff_qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
01291 int x,y, thres1, thres2;
01292
01293 if(s->qlog == LOSSLESS_QLOG){
01294 for(y=0; y<h; y++)
01295 for(x=0; x<w; x++)
01296 dst[x + y*stride]= src[x + y*stride];
01297 return;
01298 }
01299
01300 bias= bias ? 0 : (3*qmul)>>3;
01301 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
01302 thres2= 2*thres1;
01303
01304 if(!bias){
01305 for(y=0; y<h; y++){
01306 for(x=0; x<w; x++){
01307 int i= src[x + y*stride];
01308
01309 if((unsigned)(i+thres1) > thres2){
01310 if(i>=0){
01311 i<<= QEXPSHIFT;
01312 i/= qmul;
01313 dst[x + y*stride]= i;
01314 }else{
01315 i= -i;
01316 i<<= QEXPSHIFT;
01317 i/= qmul;
01318 dst[x + y*stride]= -i;
01319 }
01320 }else
01321 dst[x + y*stride]= 0;
01322 }
01323 }
01324 }else{
01325 for(y=0; y<h; y++){
01326 for(x=0; x<w; x++){
01327 int i= src[x + y*stride];
01328
01329 if((unsigned)(i+thres1) > thres2){
01330 if(i>=0){
01331 i<<= QEXPSHIFT;
01332 i= (i + bias) / qmul;
01333 dst[x + y*stride]= i;
01334 }else{
01335 i= -i;
01336 i<<= QEXPSHIFT;
01337 i= (i + bias) / qmul;
01338 dst[x + y*stride]= -i;
01339 }
01340 }else
01341 dst[x + y*stride]= 0;
01342 }
01343 }
01344 }
01345 }
01346
01347 static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
01348 const int w= b->width;
01349 const int h= b->height;
01350 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
01351 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
01352 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
01353 int x,y;
01354
01355 if(s->qlog == LOSSLESS_QLOG) return;
01356
01357 for(y=0; y<h; y++){
01358 for(x=0; x<w; x++){
01359 int i= src[x + y*stride];
01360 if(i<0){
01361 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT));
01362 }else if(i>0){
01363 src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
01364 }
01365 }
01366 }
01367 }
01368
01369 static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
01370 const int w= b->width;
01371 const int h= b->height;
01372 int x,y;
01373
01374 for(y=h-1; y>=0; y--){
01375 for(x=w-1; x>=0; x--){
01376 int i= x + y*stride;
01377
01378 if(x){
01379 if(use_median){
01380 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
01381 else src[i] -= src[i - 1];
01382 }else{
01383 if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
01384 else src[i] -= src[i - 1];
01385 }
01386 }else{
01387 if(y) src[i] -= src[i - stride];
01388 }
01389 }
01390 }
01391 }
01392
01393 static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
01394 const int w= b->width;
01395 const int h= b->height;
01396 int x,y;
01397
01398 for(y=0; y<h; y++){
01399 for(x=0; x<w; x++){
01400 int i= x + y*stride;
01401
01402 if(x){
01403 if(use_median){
01404 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
01405 else src[i] += src[i - 1];
01406 }else{
01407 if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
01408 else src[i] += src[i - 1];
01409 }
01410 }else{
01411 if(y) src[i] += src[i - stride];
01412 }
01413 }
01414 }
01415 }
01416
01417 static void encode_qlogs(SnowContext *s){
01418 int plane_index, level, orientation;
01419
01420 for(plane_index=0; plane_index<2; plane_index++){
01421 for(level=0; level<s->spatial_decomposition_count; level++){
01422 for(orientation=level ? 1:0; orientation<4; orientation++){
01423 if(orientation==2) continue;
01424 put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
01425 }
01426 }
01427 }
01428 }
01429
01430 static void encode_header(SnowContext *s){
01431 int plane_index, i;
01432 uint8_t kstate[32];
01433
01434 memset(kstate, MID_STATE, sizeof(kstate));
01435
01436 put_rac(&s->c, kstate, s->keyframe);
01437 if(s->keyframe || s->always_reset){
01438 ff_snow_reset_contexts(s);
01439 s->last_spatial_decomposition_type=
01440 s->last_qlog=
01441 s->last_qbias=
01442 s->last_mv_scale=
01443 s->last_block_max_depth= 0;
01444 for(plane_index=0; plane_index<2; plane_index++){
01445 Plane *p= &s->plane[plane_index];
01446 p->last_htaps=0;
01447 p->last_diag_mc=0;
01448 memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
01449 }
01450 }
01451 if(s->keyframe){
01452 put_symbol(&s->c, s->header_state, s->version, 0);
01453 put_rac(&s->c, s->header_state, s->always_reset);
01454 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
01455 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
01456 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
01457 put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
01458 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
01459 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
01460 put_rac(&s->c, s->header_state, s->spatial_scalability);
01461
01462 put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
01463
01464 encode_qlogs(s);
01465 }
01466
01467 if(!s->keyframe){
01468 int update_mc=0;
01469 for(plane_index=0; plane_index<2; plane_index++){
01470 Plane *p= &s->plane[plane_index];
01471 update_mc |= p->last_htaps != p->htaps;
01472 update_mc |= p->last_diag_mc != p->diag_mc;
01473 update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
01474 }
01475 put_rac(&s->c, s->header_state, update_mc);
01476 if(update_mc){
01477 for(plane_index=0; plane_index<2; plane_index++){
01478 Plane *p= &s->plane[plane_index];
01479 put_rac(&s->c, s->header_state, p->diag_mc);
01480 put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
01481 for(i= p->htaps/2; i; i--)
01482 put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
01483 }
01484 }
01485 if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
01486 put_rac(&s->c, s->header_state, 1);
01487 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
01488 encode_qlogs(s);
01489 }else
01490 put_rac(&s->c, s->header_state, 0);
01491 }
01492
01493 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
01494 put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
01495 put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
01496 put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
01497 put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
01498
01499 }
01500
01501 static void update_last_header_values(SnowContext *s){
01502 int plane_index;
01503
01504 if(!s->keyframe){
01505 for(plane_index=0; plane_index<2; plane_index++){
01506 Plane *p= &s->plane[plane_index];
01507 p->last_diag_mc= p->diag_mc;
01508 p->last_htaps = p->htaps;
01509 memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
01510 }
01511 }
01512
01513 s->last_spatial_decomposition_type = s->spatial_decomposition_type;
01514 s->last_qlog = s->qlog;
01515 s->last_qbias = s->qbias;
01516 s->last_mv_scale = s->mv_scale;
01517 s->last_block_max_depth = s->block_max_depth;
01518 s->last_spatial_decomposition_count = s->spatial_decomposition_count;
01519 }
01520
01521 static int qscale2qlog(int qscale){
01522 return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
01523 + 61*QROOT/8;
01524 }
01525
01526 static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
01527 {
01528
01529
01530
01531 uint32_t coef_sum= 0;
01532 int level, orientation, delta_qlog;
01533
01534 for(level=0; level<s->spatial_decomposition_count; level++){
01535 for(orientation=level ? 1 : 0; orientation<4; orientation++){
01536 SubBand *b= &s->plane[0].band[level][orientation];
01537 IDWTELEM *buf= b->ibuf;
01538 const int w= b->width;
01539 const int h= b->height;
01540 const int stride= b->stride;
01541 const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
01542 const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
01543 const int qdiv= (1<<16)/qmul;
01544 int x, y;
01545
01546 for(y=0; y<h; y++)
01547 for(x=0; x<w; x++)
01548 buf[x+y*stride]= b->buf[x+y*stride];
01549 if(orientation==0)
01550 decorrelate(s, b, buf, stride, 1, 0);
01551 for(y=0; y<h; y++)
01552 for(x=0; x<w; x++)
01553 coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
01554 }
01555 }
01556
01557
01558 coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
01559 assert(coef_sum < INT_MAX);
01560
01561 if(pict->pict_type == AV_PICTURE_TYPE_I){
01562 s->m.current_picture.mb_var_sum= coef_sum;
01563 s->m.current_picture.mc_mb_var_sum= 0;
01564 }else{
01565 s->m.current_picture.mc_mb_var_sum= coef_sum;
01566 s->m.current_picture.mb_var_sum= 0;
01567 }
01568
01569 pict->quality= ff_rate_estimate_qscale(&s->m, 1);
01570 if (pict->quality < 0)
01571 return INT_MIN;
01572 s->lambda= pict->quality * 3/2;
01573 delta_qlog= qscale2qlog(pict->quality) - s->qlog;
01574 s->qlog+= delta_qlog;
01575 return delta_qlog;
01576 }
01577
01578 static void calculate_visual_weight(SnowContext *s, Plane *p){
01579 int width = p->width;
01580 int height= p->height;
01581 int level, orientation, x, y;
01582
01583 for(level=0; level<s->spatial_decomposition_count; level++){
01584 for(orientation=level ? 1 : 0; orientation<4; orientation++){
01585 SubBand *b= &p->band[level][orientation];
01586 IDWTELEM *ibuf= b->ibuf;
01587 int64_t error=0;
01588
01589 memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
01590 ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
01591 ff_spatial_idwt(s->spatial_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
01592 for(y=0; y<height; y++){
01593 for(x=0; x<width; x++){
01594 int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
01595 error += d*d;
01596 }
01597 }
01598
01599 b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
01600 }
01601 }
01602 }
01603
01604 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
01605 const AVFrame *pict, int *got_packet)
01606 {
01607 SnowContext *s = avctx->priv_data;
01608 RangeCoder * const c= &s->c;
01609 AVFrame *pic = &s->new_picture;
01610 const int width= s->avctx->width;
01611 const int height= s->avctx->height;
01612 int level, orientation, plane_index, i, y, ret;
01613 uint8_t rc_header_bak[sizeof(s->header_state)];
01614 uint8_t rc_block_bak[sizeof(s->block_state)];
01615
01616 if ((ret = ff_alloc_packet2(avctx, pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + FF_MIN_BUFFER_SIZE)) < 0)
01617 return ret;
01618
01619 ff_init_range_encoder(c, pkt->data, pkt->size);
01620 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01621
01622 for(i=0; i<3; i++){
01623 int shift= !!i;
01624 for(y=0; y<(height>>shift); y++)
01625 memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
01626 &pict->data[i][y * pict->linesize[i]],
01627 width>>shift);
01628 }
01629 s->new_picture = *pict;
01630
01631 s->m.picture_number= avctx->frame_number;
01632 if(avctx->flags&CODEC_FLAG_PASS2){
01633 s->m.pict_type = pic->pict_type = s->m.rc_context.entry[avctx->frame_number].new_pict_type;
01634 s->keyframe = pic->pict_type == AV_PICTURE_TYPE_I;
01635 if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
01636 pic->quality = ff_rate_estimate_qscale(&s->m, 0);
01637 if (pic->quality < 0)
01638 return -1;
01639 }
01640 }else{
01641 s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
01642 s->m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
01643 }
01644
01645 if(s->pass1_rc && avctx->frame_number == 0)
01646 pic->quality = 2*FF_QP2LAMBDA;
01647 if (pic->quality) {
01648 s->qlog = qscale2qlog(pic->quality);
01649 s->lambda = pic->quality * 3/2;
01650 }
01651 if (s->qlog < 0 || (!pic->quality && (avctx->flags & CODEC_FLAG_QSCALE))) {
01652 s->qlog= LOSSLESS_QLOG;
01653 s->lambda = 0;
01654 }
01655
01656 ff_snow_frame_start(s);
01657
01658 s->m.current_picture_ptr= &s->m.current_picture;
01659 s->m.last_picture.f.pts = s->m.current_picture.f.pts;
01660 s->m.current_picture.f.pts = pict->pts;
01661 if(pic->pict_type == AV_PICTURE_TYPE_P){
01662 int block_width = (width +15)>>4;
01663 int block_height= (height+15)>>4;
01664 int stride= s->current_picture.linesize[0];
01665
01666 assert(s->current_picture.data[0]);
01667 assert(s->last_picture[0].data[0]);
01668
01669 s->m.avctx= s->avctx;
01670 s->m.current_picture.f.data[0] = s->current_picture.data[0];
01671 s->m. last_picture.f.data[0] = s->last_picture[0].data[0];
01672 s->m. new_picture.f.data[0] = s-> input_picture.data[0];
01673 s->m. last_picture_ptr= &s->m. last_picture;
01674 s->m.linesize=
01675 s->m. last_picture.f.linesize[0] =
01676 s->m. new_picture.f.linesize[0] =
01677 s->m.current_picture.f.linesize[0] = stride;
01678 s->m.uvlinesize= s->current_picture.linesize[1];
01679 s->m.width = width;
01680 s->m.height= height;
01681 s->m.mb_width = block_width;
01682 s->m.mb_height= block_height;
01683 s->m.mb_stride= s->m.mb_width+1;
01684 s->m.b8_stride= 2*s->m.mb_width+1;
01685 s->m.f_code=1;
01686 s->m.pict_type = pic->pict_type;
01687 s->m.me_method= s->avctx->me_method;
01688 s->m.me.scene_change_score=0;
01689 s->m.flags= s->avctx->flags;
01690 s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
01691 s->m.out_format= FMT_H263;
01692 s->m.unrestricted_mv= 1;
01693
01694 s->m.lambda = s->lambda;
01695 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
01696 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
01697
01698 s->m.dsp= s->dsp;
01699 ff_init_me(&s->m);
01700 s->dsp= s->m.dsp;
01701 }
01702
01703 if(s->pass1_rc){
01704 memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
01705 memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
01706 }
01707
01708 redo_frame:
01709
01710 if (pic->pict_type == AV_PICTURE_TYPE_I)
01711 s->spatial_decomposition_count= 5;
01712 else
01713 s->spatial_decomposition_count= 5;
01714
01715 while( !(width >>(s->chroma_h_shift + s->spatial_decomposition_count))
01716 || !(height>>(s->chroma_v_shift + s->spatial_decomposition_count)))
01717 s->spatial_decomposition_count--;
01718
01719 s->m.pict_type = pic->pict_type;
01720 s->qbias = pic->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
01721
01722 ff_snow_common_init_after_header(avctx);
01723
01724 if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
01725 for(plane_index=0; plane_index<3; plane_index++){
01726 calculate_visual_weight(s, &s->plane[plane_index]);
01727 }
01728 }
01729
01730 encode_header(s);
01731 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
01732 encode_blocks(s, 1);
01733 s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
01734
01735 for(plane_index=0; plane_index<3; plane_index++){
01736 Plane *p= &s->plane[plane_index];
01737 int w= p->width;
01738 int h= p->height;
01739 int x, y;
01740
01741
01742 if (!s->memc_only) {
01743
01744 if(pict->data[plane_index])
01745 for(y=0; y<h; y++){
01746 for(x=0; x<w; x++){
01747 s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
01748 }
01749 }
01750 predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
01751
01752 if( plane_index==0
01753 && pic->pict_type == AV_PICTURE_TYPE_P
01754 && !(avctx->flags&CODEC_FLAG_PASS2)
01755 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
01756 ff_init_range_encoder(c, pkt->data, pkt->size);
01757 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
01758 pic->pict_type= AV_PICTURE_TYPE_I;
01759 s->keyframe=1;
01760 s->current_picture.key_frame=1;
01761 goto redo_frame;
01762 }
01763
01764 if(s->qlog == LOSSLESS_QLOG){
01765 for(y=0; y<h; y++){
01766 for(x=0; x<w; x++){
01767 s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
01768 }
01769 }
01770 }else{
01771 for(y=0; y<h; y++){
01772 for(x=0; x<w; x++){
01773 s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
01774 }
01775 }
01776 }
01777
01778
01779
01780
01781 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
01782
01783 if(s->pass1_rc && plane_index==0){
01784 int delta_qlog = ratecontrol_1pass(s, pic);
01785 if (delta_qlog <= INT_MIN)
01786 return -1;
01787 if(delta_qlog){
01788
01789 ff_init_range_encoder(c, pkt->data, pkt->size);
01790 memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
01791 memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
01792 encode_header(s);
01793 encode_blocks(s, 0);
01794 }
01795 }
01796
01797 for(level=0; level<s->spatial_decomposition_count; level++){
01798 for(orientation=level ? 1 : 0; orientation<4; orientation++){
01799 SubBand *b= &p->band[level][orientation];
01800
01801 if(!QUANTIZE2)
01802 quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
01803 if(orientation==0)
01804 decorrelate(s, b, b->ibuf, b->stride, pic->pict_type == AV_PICTURE_TYPE_P, 0);
01805 if (!s->no_bitstream)
01806 encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
01807 assert(b->parent==NULL || b->parent->stride == b->stride*2);
01808 if(orientation==0)
01809 correlate(s, b, b->ibuf, b->stride, 1, 0);
01810 }
01811 }
01812
01813 for(level=0; level<s->spatial_decomposition_count; level++){
01814 for(orientation=level ? 1 : 0; orientation<4; orientation++){
01815 SubBand *b= &p->band[level][orientation];
01816
01817 dequantize(s, b, b->ibuf, b->stride);
01818 }
01819 }
01820
01821 ff_spatial_idwt(s->spatial_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
01822 if(s->qlog == LOSSLESS_QLOG){
01823 for(y=0; y<h; y++){
01824 for(x=0; x<w; x++){
01825 s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
01826 }
01827 }
01828 }
01829 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
01830 }else{
01831
01832 if(pic->pict_type == AV_PICTURE_TYPE_I){
01833 for(y=0; y<h; y++){
01834 for(x=0; x<w; x++){
01835 s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
01836 pict->data[plane_index][y*pict->linesize[plane_index] + x];
01837 }
01838 }
01839 }else{
01840 memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
01841 predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
01842 }
01843 }
01844 if(s->avctx->flags&CODEC_FLAG_PSNR){
01845 int64_t error= 0;
01846
01847 if(pict->data[plane_index])
01848 for(y=0; y<h; y++){
01849 for(x=0; x<w; x++){
01850 int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
01851 error += d*d;
01852 }
01853 }
01854 s->avctx->error[plane_index] += error;
01855 s->current_picture.error[plane_index] = error;
01856 }
01857
01858 }
01859
01860 update_last_header_values(s);
01861
01862 ff_snow_release_buffer(avctx);
01863
01864 s->current_picture.coded_picture_number = avctx->frame_number;
01865 s->current_picture.pict_type = pict->pict_type;
01866 s->current_picture.quality = pict->quality;
01867 s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
01868 s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
01869 s->m.current_picture.f.display_picture_number =
01870 s->m.current_picture.f.coded_picture_number = avctx->frame_number;
01871 s->m.current_picture.f.quality = pic->quality;
01872 s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
01873 if(s->pass1_rc)
01874 if (ff_rate_estimate_qscale(&s->m, 0) < 0)
01875 return -1;
01876 if(avctx->flags&CODEC_FLAG_PASS1)
01877 ff_write_pass1_stats(&s->m);
01878 s->m.last_pict_type = s->m.pict_type;
01879 avctx->frame_bits = s->m.frame_bits;
01880 avctx->mv_bits = s->m.mv_bits;
01881 avctx->misc_bits = s->m.misc_bits;
01882 avctx->p_tex_bits = s->m.p_tex_bits;
01883
01884 emms_c();
01885
01886 pkt->size = ff_rac_terminate(c);
01887 if (avctx->coded_frame->key_frame)
01888 pkt->flags |= AV_PKT_FLAG_KEY;
01889 *got_packet = 1;
01890
01891 return 0;
01892 }
01893
01894 static av_cold int encode_end(AVCodecContext *avctx)
01895 {
01896 SnowContext *s = avctx->priv_data;
01897
01898 ff_snow_common_end(s);
01899 if (s->input_picture.data[0])
01900 avctx->release_buffer(avctx, &s->input_picture);
01901 av_free(avctx->stats_out);
01902 av_freep(&s->runs);
01903
01904 return 0;
01905 }
01906
01907 #define OFFSET(x) offsetof(SnowContext, x)
01908 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
01909 static const AVOption options[] = {
01910 { "memc_only", "Only do ME/MC (I frames -> ref, P frame -> ME+MC).", OFFSET(memc_only), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
01911 { "no_bitstream", "Skip final bitstream writeout.", OFFSET(no_bitstream), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
01912 { NULL },
01913 };
01914
01915 static const AVClass snowenc_class = {
01916 .class_name = "snow encoder",
01917 .item_name = av_default_item_name,
01918 .option = options,
01919 .version = LIBAVUTIL_VERSION_INT,
01920 };
01921
01922 AVCodec ff_snow_encoder = {
01923 .name = "snow",
01924 .type = AVMEDIA_TYPE_VIDEO,
01925 .id = CODEC_ID_SNOW,
01926 .priv_data_size = sizeof(SnowContext),
01927 .init = encode_init,
01928 .encode2 = encode_frame,
01929 .close = encode_end,
01930 .long_name = NULL_IF_CONFIG_SMALL("Snow"),
01931 .priv_class = &snowenc_class,
01932 };
01933
01934
01935 #ifdef TEST
01936 #undef malloc
01937 #undef free
01938 #undef printf
01939
01940 #include "libavutil/lfg.h"
01941 #include "libavutil/mathematics.h"
01942
01943 int main(void){
01944 int width=256;
01945 int height=256;
01946 int buffer[2][width*height];
01947 SnowContext s;
01948 int i;
01949 AVLFG prng;
01950 s.spatial_decomposition_count=6;
01951 s.spatial_decomposition_type=1;
01952
01953 av_lfg_init(&prng, 1);
01954
01955 printf("testing 5/3 DWT\n");
01956 for(i=0; i<width*height; i++)
01957 buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
01958
01959 ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01960 ff_spatial_idwt((IDWTELEM*)buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01961
01962 for(i=0; i<width*height; i++)
01963 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
01964
01965 printf("testing 9/7 DWT\n");
01966 s.spatial_decomposition_type=0;
01967 for(i=0; i<width*height; i++)
01968 buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
01969
01970 ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01971 ff_spatial_idwt((IDWTELEM*)buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01972
01973 for(i=0; i<width*height; i++)
01974 if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
01975
01976 {
01977 int level, orientation, x, y;
01978 int64_t errors[8][4];
01979 int64_t g=0;
01980
01981 memset(errors, 0, sizeof(errors));
01982 s.spatial_decomposition_count=3;
01983 s.spatial_decomposition_type=0;
01984 for(level=0; level<s.spatial_decomposition_count; level++){
01985 for(orientation=level ? 1 : 0; orientation<4; orientation++){
01986 int w= width >> (s.spatial_decomposition_count-level);
01987 int h= height >> (s.spatial_decomposition_count-level);
01988 int stride= width << (s.spatial_decomposition_count-level);
01989 DWTELEM *buf= buffer[0];
01990 int64_t error=0;
01991
01992 if(orientation&1) buf+=w;
01993 if(orientation>1) buf+=stride>>1;
01994
01995 memset(buffer[0], 0, sizeof(int)*width*height);
01996 buf[w/2 + h/2*stride]= 256*256;
01997 ff_spatial_idwt((IDWTELEM*)buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
01998 for(y=0; y<height; y++){
01999 for(x=0; x<width; x++){
02000 int64_t d= buffer[0][x + y*width];
02001 error += d*d;
02002 if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
02003 }
02004 if(FFABS(height/2-y)<9 && level==2) printf("\n");
02005 }
02006 error= (int)(sqrt(error)+0.5);
02007 errors[level][orientation]= error;
02008 if(g) g=av_gcd(g, error);
02009 else g= error;
02010 }
02011 }
02012 printf("static int const visual_weight[][4]={\n");
02013 for(level=0; level<s.spatial_decomposition_count; level++){
02014 printf(" {");
02015 for(orientation=0; orientation<4; orientation++){
02016 printf("%8"PRId64",", errors[level][orientation]/g);
02017 }
02018 printf("},\n");
02019 }
02020 printf("};\n");
02021 {
02022 int level=2;
02023 int w= width >> (s.spatial_decomposition_count-level);
02024
02025 int stride= width << (s.spatial_decomposition_count-level);
02026 DWTELEM *buf= buffer[0];
02027 int64_t error=0;
02028
02029 buf+=w;
02030 buf+=stride>>1;
02031
02032 memset(buffer[0], 0, sizeof(int)*width*height);
02033 for(y=0; y<height; y++){
02034 for(x=0; x<width; x++){
02035 int tab[4]={0,2,3,1};
02036 buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
02037 }
02038 }
02039 ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
02040 for(y=0; y<height; y++){
02041 for(x=0; x<width; x++){
02042 int64_t d= buffer[0][x + y*width];
02043 error += d*d;
02044 if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
02045 }
02046 if(FFABS(height/2-y)<9) printf("\n");
02047 }
02048 }
02049
02050 }
02051 return 0;
02052 }
02053 #endif