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