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