comparison snow.c @ 3313:be941215e8e6 libavcodec

Snow 1pass ratecontrol
author lorenm
date Sun, 28 May 2006 12:38:10 +0000
parents 68721b62a528
children aea2230e6033
comparison
equal deleted inserted replaced
3312:458b6162fa22 3313:be941215e8e6
442 int chroma_v_shift; 442 int chroma_v_shift;
443 int spatial_scalability; 443 int spatial_scalability;
444 int qlog; 444 int qlog;
445 int lambda; 445 int lambda;
446 int lambda2; 446 int lambda2;
447 int pass1_rc;
447 int mv_scale; 448 int mv_scale;
448 int qbias; 449 int qbias;
449 #define QBIAS_SHIFT 3 450 #define QBIAS_SHIFT 3
450 int b_width; 451 int b_width;
451 int b_height; 452 int b_height;
3850 s->avctx->get_buffer(s->avctx, &s->mconly_picture); 3851 s->avctx->get_buffer(s->avctx, &s->mconly_picture);
3851 3852
3852 return 0; 3853 return 0;
3853 } 3854 }
3854 3855
3856 static void ratecontrol_1pass(SnowContext *s, AVFrame *pict)
3857 {
3858 /* estimate the frame's complexity as a sum of weighted dwt coefs.
3859 * FIXME we know exact mv bits at this point,
3860 * but ratecontrol isn't set up to include them. */
3861 uint32_t coef_sum= 0;
3862 int level, orientation;
3863
3864 for(level=0; level<s->spatial_decomposition_count; level++){
3865 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3866 SubBand *b= &s->plane[0].band[level][orientation];
3867 DWTELEM *buf= b->buf;
3868 const int w= b->width;
3869 const int h= b->height;
3870 const int stride= b->stride;
3871 const int qlog= clip(2*QROOT + b->qlog, 0, QROOT*16);
3872 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
3873 const int qdiv= (1<<16)/qmul;
3874 int x, y;
3875 if(orientation==0)
3876 decorrelate(s, b, buf, stride, 1, 0);
3877 for(y=0; y<h; y++)
3878 for(x=0; x<w; x++)
3879 coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
3880 if(orientation==0)
3881 correlate(s, b, buf, stride, 1, 0);
3882 }
3883 }
3884
3885 /* ugly, ratecontrol just takes a sqrt again */
3886 coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
3887 assert(coef_sum < INT_MAX);
3888
3889 if(pict->pict_type == I_TYPE){
3890 s->m.current_picture.mb_var_sum= coef_sum;
3891 s->m.current_picture.mc_mb_var_sum= 0;
3892 }else{
3893 s->m.current_picture.mc_mb_var_sum= coef_sum;
3894 s->m.current_picture.mb_var_sum= 0;
3895 }
3896
3897 pict->quality= ff_rate_estimate_qscale(&s->m, 1);
3898 s->lambda= pict->quality * 3/2;
3899 }
3855 3900
3856 static void calculate_vissual_weight(SnowContext *s, Plane *p){ 3901 static void calculate_vissual_weight(SnowContext *s, Plane *p){
3857 int width = p->width; 3902 int width = p->width;
3858 int height= p->height; 3903 int height= p->height;
3859 int level, orientation, x, y; 3904 int level, orientation, x, y;
3908 3953
3909 if(avctx->flags&CODEC_FLAG_PASS1){ 3954 if(avctx->flags&CODEC_FLAG_PASS1){
3910 if(!avctx->stats_out) 3955 if(!avctx->stats_out)
3911 avctx->stats_out = av_mallocz(256); 3956 avctx->stats_out = av_mallocz(256);
3912 } 3957 }
3913 if(avctx->flags&CODEC_FLAG_PASS2){ 3958 if(!(avctx->flags&CODEC_FLAG_QSCALE)){
3914 if(ff_rate_control_init(&s->m) < 0) 3959 if(ff_rate_control_init(&s->m) < 0)
3915 return -1; 3960 return -1;
3916 } 3961 }
3962 s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
3917 3963
3918 for(plane_index=0; plane_index<3; plane_index++){ 3964 for(plane_index=0; plane_index<3; plane_index++){
3919 calculate_vissual_weight(s, &s->plane[plane_index]); 3965 calculate_vissual_weight(s, &s->plane[plane_index]);
3920 } 3966 }
3921 3967
3991 &pict->data[i][y * pict->linesize[i]], 4037 &pict->data[i][y * pict->linesize[i]],
3992 width>>shift); 4038 width>>shift);
3993 } 4039 }
3994 s->new_picture = *pict; 4040 s->new_picture = *pict;
3995 4041
4042 s->m.picture_number= avctx->frame_number;
3996 if(avctx->flags&CODEC_FLAG_PASS2){ 4043 if(avctx->flags&CODEC_FLAG_PASS2){
3997 s->m.pict_type = 4044 s->m.pict_type =
3998 pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type; 4045 pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
3999 s->keyframe= pict->pict_type==FF_I_TYPE; 4046 s->keyframe= pict->pict_type==FF_I_TYPE;
4000 s->m.picture_number= avctx->frame_number;
4001 if(!(avctx->flags&CODEC_FLAG_QSCALE)) 4047 if(!(avctx->flags&CODEC_FLAG_QSCALE))
4002 pict->quality= ff_rate_estimate_qscale(&s->m, 0); 4048 pict->quality= ff_rate_estimate_qscale(&s->m, 0);
4003 }else{ 4049 }else{
4004 s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0; 4050 s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
4051 s->m.pict_type=
4005 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE; 4052 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
4006 } 4053 }
4007 4054
4055 if(s->pass1_rc && avctx->frame_number == 0)
4056 pict->quality= 2*FF_QP2LAMBDA;
4008 if(pict->quality){ 4057 if(pict->quality){
4009 s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2)); 4058 s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
4010 //<64 >60 4059 //<64 >60
4011 s->qlog += 61*QROOT/8; 4060 s->qlog += 61*QROOT/8;
4012 }else{ 4061 s->lambda = pict->quality * 3/2;
4062 }
4063 if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
4013 s->qlog= LOSSLESS_QLOG; 4064 s->qlog= LOSSLESS_QLOG;
4014 } 4065 s->lambda = 0;
4066 }//else keep previous frame's qlog until after motion est
4015 4067
4016 frame_start(s); 4068 frame_start(s);
4017 s->current_picture.key_frame= s->keyframe; 4069 s->current_picture.key_frame= s->keyframe;
4018 4070
4019 s->m.current_picture_ptr= &s->m.current_picture; 4071 s->m.current_picture_ptr= &s->m.current_picture;
4048 s->m.flags= s->avctx->flags; 4100 s->m.flags= s->avctx->flags;
4049 s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0; 4101 s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
4050 s->m.out_format= FMT_H263; 4102 s->m.out_format= FMT_H263;
4051 s->m.unrestricted_mv= 1; 4103 s->m.unrestricted_mv= 1;
4052 4104
4053 s->lambda = s->m.lambda= pict->quality * 3/2; //FIXME bug somewhere else 4105 s->m.lambda = s->lambda;
4054 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7); 4106 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
4055 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT; 4107 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
4056 4108
4057 s->m.dsp= s->dsp; //move 4109 s->m.dsp= s->dsp; //move
4058 ff_init_me(&s->m); 4110 ff_init_me(&s->m);
4059 s->dsp= s->m.dsp; 4111 s->dsp= s->m.dsp;
4060 } 4112 }
4061 4113
4062 redo_frame: 4114 redo_frame:
4063 4115
4116 s->m.pict_type = pict->pict_type;
4064 s->qbias= pict->pict_type == P_TYPE ? 2 : 0; 4117 s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
4065 4118
4066 encode_header(s); 4119 encode_header(s);
4067 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start); 4120 s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
4068 encode_blocks(s); 4121 encode_blocks(s);
4084 } 4137 }
4085 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0); 4138 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
4086 4139
4087 if( plane_index==0 4140 if( plane_index==0
4088 && pict->pict_type == P_TYPE 4141 && pict->pict_type == P_TYPE
4142 && !(avctx->flags&CODEC_FLAG_PASS2)
4089 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){ 4143 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
4090 ff_init_range_encoder(c, buf, buf_size); 4144 ff_init_range_encoder(c, buf, buf_size);
4091 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8); 4145 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
4092 pict->pict_type= FF_I_TYPE; 4146 pict->pict_type= FF_I_TYPE;
4093 s->keyframe=1; 4147 s->keyframe=1;
4103 } 4157 }
4104 } 4158 }
4105 4159
4106 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count); 4160 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
4107 4161
4162 if(s->pass1_rc && plane_index==0)
4163 ratecontrol_1pass(s, pict);
4164
4108 for(level=0; level<s->spatial_decomposition_count; level++){ 4165 for(level=0; level<s->spatial_decomposition_count; level++){
4109 for(orientation=level ? 1 : 0; orientation<4; orientation++){ 4166 for(orientation=level ? 1 : 0; orientation<4; orientation++){
4110 SubBand *b= &p->band[level][orientation]; 4167 SubBand *b= &p->band[level][orientation];
4111 4168
4112 quantize(s, b, b->buf, b->stride, s->qbias); 4169 quantize(s, b, b->buf, b->stride, s->qbias);
4158 avctx->release_buffer(avctx, &s->last_picture); 4215 avctx->release_buffer(avctx, &s->last_picture);
4159 4216
4160 s->current_picture.coded_picture_number = avctx->frame_number; 4217 s->current_picture.coded_picture_number = avctx->frame_number;
4161 s->current_picture.pict_type = pict->pict_type; 4218 s->current_picture.pict_type = pict->pict_type;
4162 s->current_picture.quality = pict->quality; 4219 s->current_picture.quality = pict->quality;
4163 if(avctx->flags&CODEC_FLAG_PASS1){ 4220 s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
4164 s->m.p_tex_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits - s->m.mv_bits; 4221 s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
4165 s->m.current_picture.display_picture_number = 4222 s->m.current_picture.display_picture_number =
4166 s->m.current_picture.coded_picture_number = avctx->frame_number; 4223 s->m.current_picture.coded_picture_number = avctx->frame_number;
4167 s->m.pict_type = pict->pict_type; 4224 s->m.current_picture.quality = pict->quality;
4168 s->m.current_picture.quality = pict->quality; 4225 s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
4226 if(s->pass1_rc)
4227 ff_rate_estimate_qscale(&s->m, 0);
4228 if(avctx->flags&CODEC_FLAG_PASS1)
4169 ff_write_pass1_stats(&s->m); 4229 ff_write_pass1_stats(&s->m);
4170 } 4230 s->m.last_pict_type = s->m.pict_type;
4171 if(avctx->flags&CODEC_FLAG_PASS2){
4172 s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
4173 }
4174 4231
4175 emms_c(); 4232 emms_c();
4176 4233
4177 return ff_rac_terminate(c); 4234 return ff_rac_terminate(c);
4178 } 4235 }