comparison snow.c @ 3661:b4425339894b libavcodec

fix snow 1pass rc: the qlog used and the one written to the bitstream weren't always the same.
author lorenm
date Fri, 01 Sep 2006 21:51:35 +0000
parents 6805c1e5507e
children fc714e9a5419
comparison
equal deleted inserted replaced
3660:7e1ee254a3ee 3661:b4425339894b
2315 decode_q_branch(s, level+1, 2*x+0, 2*y+1); 2315 decode_q_branch(s, level+1, 2*x+0, 2*y+1);
2316 decode_q_branch(s, level+1, 2*x+1, 2*y+1); 2316 decode_q_branch(s, level+1, 2*x+1, 2*y+1);
2317 } 2317 }
2318 } 2318 }
2319 2319
2320 static void encode_blocks(SnowContext *s){ 2320 static void encode_blocks(SnowContext *s, int search){
2321 int x, y; 2321 int x, y;
2322 int w= s->b_width; 2322 int w= s->b_width;
2323 int h= s->b_height; 2323 int h= s->b_height;
2324 2324
2325 if(s->avctx->me_method == ME_ITER && !s->keyframe) 2325 if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
2326 iterative_me(s); 2326 iterative_me(s);
2327 2327
2328 for(y=0; y<h; y++){ 2328 for(y=0; y<h; y++){
2329 if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit 2329 if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
2330 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); 2330 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2331 return; 2331 return;
2332 } 2332 }
2333 for(x=0; x<w; x++){ 2333 for(x=0; x<w; x++){
2334 if(s->avctx->me_method == ME_ITER) 2334 if(s->avctx->me_method == ME_ITER || !search)
2335 encode_q_branch2(s, 0, x, y); 2335 encode_q_branch2(s, 0, x, y);
2336 else 2336 else
2337 encode_q_branch (s, 0, x, y); 2337 encode_q_branch (s, 0, x, y);
2338 } 2338 }
2339 } 2339 }
3955 static int qscale2qlog(int qscale){ 3955 static int qscale2qlog(int qscale){
3956 return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2)) 3956 return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
3957 + 61*QROOT/8; //<64 >60 3957 + 61*QROOT/8; //<64 >60
3958 } 3958 }
3959 3959
3960 static void ratecontrol_1pass(SnowContext *s, AVFrame *pict) 3960 static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
3961 { 3961 {
3962 /* estimate the frame's complexity as a sum of weighted dwt coefs. 3962 /* estimate the frame's complexity as a sum of weighted dwt coefs.
3963 * FIXME we know exact mv bits at this point, 3963 * FIXME we know exact mv bits at this point,
3964 * but ratecontrol isn't set up to include them. */ 3964 * but ratecontrol isn't set up to include them. */
3965 uint32_t coef_sum= 0; 3965 uint32_t coef_sum= 0;
3966 int level, orientation; 3966 int level, orientation, delta_qlog;
3967 3967
3968 for(level=0; level<s->spatial_decomposition_count; level++){ 3968 for(level=0; level<s->spatial_decomposition_count; level++){
3969 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 3969 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3970 SubBand *b= &s->plane[0].band[level][orientation]; 3970 SubBand *b= &s->plane[0].band[level][orientation];
3971 DWTELEM *buf= b->buf; 3971 DWTELEM *buf= b->buf;
3998 s->m.current_picture.mb_var_sum= 0; 3998 s->m.current_picture.mb_var_sum= 0;
3999 } 3999 }
4000 4000
4001 pict->quality= ff_rate_estimate_qscale(&s->m, 1); 4001 pict->quality= ff_rate_estimate_qscale(&s->m, 1);
4002 s->lambda= pict->quality * 3/2; 4002 s->lambda= pict->quality * 3/2;
4003 s->qlog= qscale2qlog(pict->quality); 4003 delta_qlog= qscale2qlog(pict->quality) - s->qlog;
4004 s->qlog+= delta_qlog;
4005 return delta_qlog;
4004 } 4006 }
4005 4007
4006 static void calculate_vissual_weight(SnowContext *s, Plane *p){ 4008 static void calculate_vissual_weight(SnowContext *s, Plane *p){
4007 int width = p->width; 4009 int width = p->width;
4008 int height= p->height; 4010 int height= p->height;
4160 RangeCoder * const c= &s->c; 4162 RangeCoder * const c= &s->c;
4161 AVFrame *pict = data; 4163 AVFrame *pict = data;
4162 const int width= s->avctx->width; 4164 const int width= s->avctx->width;
4163 const int height= s->avctx->height; 4165 const int height= s->avctx->height;
4164 int level, orientation, plane_index, i, y; 4166 int level, orientation, plane_index, i, y;
4167 uint8_t rc_header_bak[sizeof(s->header_state)];
4168 uint8_t rc_block_bak[sizeof(s->block_state)];
4165 4169
4166 ff_init_range_encoder(c, buf, buf_size); 4170 ff_init_range_encoder(c, buf, buf_size);
4167 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); 4171 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
4168 4172
4169 for(i=0; i<3; i++){ 4173 for(i=0; i<3; i++){
4242 s->m.dsp= s->dsp; //move 4246 s->m.dsp= s->dsp; //move
4243 ff_init_me(&s->m); 4247 ff_init_me(&s->m);
4244 s->dsp= s->m.dsp; 4248 s->dsp= s->m.dsp;
4245 } 4249 }
4246 4250
4251 if(s->pass1_rc){
4252 memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
4253 memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
4254 }
4255
4247 redo_frame: 4256 redo_frame:
4248 4257
4249 s->m.pict_type = pict->pict_type; 4258 s->m.pict_type = pict->pict_type;
4250 s->qbias= pict->pict_type == P_TYPE ? 2 : 0; 4259 s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
4251 4260
4252 encode_header(s); 4261 encode_header(s);
4253 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start); 4262 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
4254 encode_blocks(s); 4263 encode_blocks(s, 1);
4255 s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits; 4264 s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
4256 4265
4257 for(plane_index=0; plane_index<3; plane_index++){ 4266 for(plane_index=0; plane_index<3; plane_index++){
4258 Plane *p= &s->plane[plane_index]; 4267 Plane *p= &s->plane[plane_index];
4259 int w= p->width; 4268 int w= p->width;
4292 } 4301 }
4293 } 4302 }
4294 4303
4295 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count); 4304 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
4296 4305
4297 if(s->pass1_rc && plane_index==0) 4306 if(s->pass1_rc && plane_index==0){
4298 ratecontrol_1pass(s, pict); 4307 int delta_qlog = ratecontrol_1pass(s, pict);
4308 if(delta_qlog){
4309 //reordering qlog in the bitstream would eliminate this reset
4310 ff_init_range_encoder(c, buf, buf_size);
4311 memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
4312 memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
4313 encode_header(s);
4314 encode_blocks(s, 0);
4315 }
4316 }
4299 4317
4300 for(level=0; level<s->spatial_decomposition_count; level++){ 4318 for(level=0; level<s->spatial_decomposition_count; level++){
4301 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 4319 for(orientation=level ? 1 : 0; orientation<4; orientation++){
4302 SubBand *b= &p->band[level][orientation]; 4320 SubBand *b= &p->band[level][orientation];
4303 4321