Unified · Split
| @@ -40,34 +40,51 @@ · 39 unmodified lines | ||
| 40 | 40 | #endif |
| 41 | 41 | |
| 42 | 42 | static int init_pass2(MpegEncContext *s); |
| 43 | −static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num); | |
| 43 | +static double get_qscale(MpegEncContext *s, RateControlEntry *rce, | |
| 44 | + double rate_factor, int frame_num); | |
| 44 | 45 | |
| 45 | −void ff_write_pass1_stats(MpegEncContext *s){ | |
| 46 | − snprintf(s->avctx->stats_out, 256, "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d;\n", | |
| 47 | − s->current_picture_ptr->f.display_picture_number, s->current_picture_ptr->f.coded_picture_number, s->pict_type, | |
| 48 | − s->current_picture.f.quality, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits, | |
| 49 | − s->f_code, s->b_code, s->current_picture.mc_mb_var_sum, s->current_picture.mb_var_sum, s->i_count, s->skip_count, s->header_bits); | |
| 46 | +void ff_write_pass1_stats(MpegEncContext *s) | |
| 47 | +{ | |
| 48 | + snprintf(s->avctx->stats_out, 256, | |
| 49 | + "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d " | |
| 50 | + "fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d;\n", | |
| 51 | + s->current_picture_ptr->f.display_picture_number, | |
| 52 | + s->current_picture_ptr->f.coded_picture_number, | |
| 53 | + s->pict_type, | |
| 54 | + s->current_picture.f.quality, | |
| 55 | + s->i_tex_bits, | |
| 56 | + s->p_tex_bits, | |
| 57 | + s->mv_bits, | |
| 58 | + s->misc_bits, | |
| 59 | + s->f_code, | |
| 60 | + s->b_code, | |
| 61 | + s->current_picture.mc_mb_var_sum, | |
| 62 | + s->current_picture.mb_var_sum, | |
| 63 | + s->i_count, s->skip_count, | |
| 64 | + s->header_bits); | |
| 50 | 65 | } |
| 51 | 66 | |
| 52 | −static inline double qp2bits(RateControlEntry *rce, double qp){ | |
| 53 | − if(qp<=0.0){ | |
| 67 | +static inline double qp2bits(RateControlEntry *rce, double qp) | |
| 68 | +{ | |
| 69 | + if (qp <= 0.0) { | |
| 54 | 70 | av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n"); |
| 55 | 71 | } |
| 56 | − return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits+1)/ qp; | |
| 72 | + return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / qp; | |
| 57 | 73 | } |
| 58 | 74 | |
| 59 | −static inline double bits2qp(RateControlEntry *rce, double bits){ | |
| 60 | − if(bits<0.9){ | |
| 75 | +static inline double bits2qp(RateControlEntry *rce, double bits) | |
| 76 | +{ | |
| 77 | + if (bits < 0.9) { | |
| 61 | 78 | av_log(NULL, AV_LOG_ERROR, "bits<0.9\n"); |
| 62 | 79 | } |
| 63 | − return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits+1)/ bits; | |
| 80 | + return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits + 1) / bits; | |
| 64 | 81 | } |
| 65 | 82 | |
| 66 | 83 | int ff_rate_control_init(MpegEncContext *s) |
| 67 | 84 | { |
| 68 | − RateControlContext *rcc= &s->rc_context; | |
| 85 | + RateControlContext *rcc = &s->rc_context; | |
| 69 | 86 | int i, res; |
| 70 | − static const char * const const_names[]={ | |
| 87 | + static const char * const const_names[] = { | |
| 71 | 88 | "PI", |
| 72 | 89 | "E", |
| 73 | 90 | "iTex", |
| @@ -83,10 +100,12 @@ int ff_rate_control_init(MpegEncContext *s) · 9 unmodified lines | ||
| 83 | 100 | "isB", |
| 84 | 101 | "avgQP", |
| 85 | 102 | "qComp", |
| 86 | −/* "lastIQP", | |
| 103 | +#if 0 | |
| 104 | + "lastIQP", | |
| 87 | 105 | "lastPQP", |
| 88 | 106 | "lastBQP", |
| 89 | − "nextNonBQP",*/ | |
| 107 | + "nextNonBQP", | |
| 108 | +#endif | |
| 90 | 109 | "avgIITex", |
| 91 | 110 | "avgPITex", |
| 92 | 111 | "avgPPTex", |
| @@ -94,156 +113,172 @@ int ff_rate_control_init(MpegEncContext *s) · 1 unmodified line | ||
| 94 | 113 | "avgTex", |
| 95 | 114 | NULL |
| 96 | 115 | }; |
| 97 | − static double (* const func1[])(void *, double)={ | |
| 116 | + static double (* const func1[])(void *, double) = { | |
| 98 | 117 | (void *)bits2qp, |
| 99 | 118 | (void *)qp2bits, |
| 100 | 119 | NULL |
| 101 | 120 | }; |
| 102 | − static const char * const func1_names[]={ | |
| 121 | + static const char * const func1_names[] = { | |
| 103 | 122 | "bits2qp", |
| 104 | 123 | "qp2bits", |
| 105 | 124 | NULL |
| 106 | 125 | }; |
| 107 | 126 | emms_c(); |
| 108 | 127 | |
| 109 | − res = av_expr_parse(&rcc->rc_eq_eval, s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, 0, s->avctx); | |
| 128 | + res = av_expr_parse(&rcc->rc_eq_eval, | |
| 129 | + s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", | |
| 130 | + const_names, func1_names, func1, | |
| 131 | + NULL, NULL, 0, s->avctx); | |
| 110 | 132 | if (res < 0) { |
| 111 | 133 | av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->avctx->rc_eq); |
| 112 | 134 | return res; |
| 113 | 135 | } |
| 114 | 136 | |
| 115 | − for(i=0; i<5; i++){ | |
| 116 | − rcc->pred[i].coeff= FF_QP2LAMBDA * 7.0; | |
| 117 | − rcc->pred[i].count= 1.0; | |
| 118 | − | |
| 119 | − rcc->pred[i].decay= 0.4; | |
| 120 | − rcc->i_cplx_sum [i]= | |
| 121 | − rcc->p_cplx_sum [i]= | |
| 122 | − rcc->mv_bits_sum[i]= | |
| 123 | − rcc->qscale_sum [i]= | |
| 124 | − rcc->frame_count[i]= 1; // 1 is better because of 1/0 and such | |
| 125 | − rcc->last_qscale_for[i]=FF_QP2LAMBDA * 5; | |
| 137 | + for (i = 0; i < 5; i++) { | |
| 138 | + rcc->pred[i].coeff = FF_QP2LAMBDA * 7.0; | |
| 139 | + rcc->pred[i].count = 1.0; | |
| 140 | + rcc->pred[i].decay = 0.4; | |
| 141 | + | |
| 142 | + rcc->i_cplx_sum [i] = | |
| 143 | + rcc->p_cplx_sum [i] = | |
| 144 | + rcc->mv_bits_sum[i] = | |
| 145 | + rcc->qscale_sum [i] = | |
| 146 | + rcc->frame_count[i] = 1; // 1 is better because of 1/0 and such | |
| 147 | + | |
| 148 | + rcc->last_qscale_for[i] = FF_QP2LAMBDA * 5; | |
| 126 | 149 | } |
| 127 | − rcc->buffer_index= s->avctx->rc_initial_buffer_occupancy; | |
| 150 | + rcc->buffer_index = s->avctx->rc_initial_buffer_occupancy; | |
| 128 | 151 | |
| 129 | − if(s->flags&CODEC_FLAG_PASS2){ | |
| 152 | + if (s->flags & CODEC_FLAG_PASS2) { | |
| 130 | 153 | int i; |
| 131 | 154 | char *p; |
| 132 | 155 | |
| 133 | 156 | /* find number of pics */ |
| 134 | − p= s->avctx->stats_in; | |
| 135 | − for(i=-1; p; i++){ | |
| 136 | − p= strchr(p+1, ';'); | |
| 137 | − } | |
| 138 | − i+= s->max_b_frames; | |
| 139 | − if(i<=0 || i>=INT_MAX / sizeof(RateControlEntry)) | |
| 157 | + p = s->avctx->stats_in; | |
| 158 | + for (i = -1; p; i++) | |
| 159 | + p = strchr(p + 1, ';'); | |
| 160 | + i += s->max_b_frames; | |
| 161 | + if (i <= 0 || i >= INT_MAX / sizeof(RateControlEntry)) | |
| 140 | 162 | return -1; |
| 141 | − rcc->entry = av_mallocz(i*sizeof(RateControlEntry)); | |
| 142 | − rcc->num_entries= i; | |
| 143 | − | |
| 144 | − /* init all to skipped p frames (with b frames we might have a not encoded frame at the end FIXME) */ | |
| 145 | − for(i=0; i<rcc->num_entries; i++){ | |
| 146 | − RateControlEntry *rce= &rcc->entry[i]; | |
| 147 | − rce->pict_type= rce->new_pict_type=AV_PICTURE_TYPE_P; | |
| 148 | − rce->qscale= rce->new_qscale=FF_QP2LAMBDA * 2; | |
| 149 | − rce->misc_bits= s->mb_num + 10; | |
| 150 | − rce->mb_var_sum= s->mb_num*100; | |
| 163 | + rcc->entry = av_mallocz(i * sizeof(RateControlEntry)); | |
| 164 | + rcc->num_entries = i; | |
| 165 | + | |
| 166 | + /* init all to skipped p frames | |
| 167 | + * (with b frames we might have a not encoded frame at the end FIXME) */ | |
| 168 | + for (i = 0; i < rcc->num_entries; i++) { | |
| 169 | + RateControlEntry *rce = &rcc->entry[i]; | |
| 170 | + | |
| 171 | + rce->pict_type = rce->new_pict_type = AV_PICTURE_TYPE_P; | |
| 172 | + rce->qscale = rce->new_qscale = FF_QP2LAMBDA * 2; | |
| 173 | + rce->misc_bits = s->mb_num + 10; | |
| 174 | + rce->mb_var_sum = s->mb_num * 100; | |
| 151 | 175 | } |
| 152 | 176 | |
| 153 | 177 | /* read stats */ |
| 154 | − p= s->avctx->stats_in; | |
| 155 | − for(i=0; i<rcc->num_entries - s->max_b_frames; i++){ | |
| 178 | + p = s->avctx->stats_in; | |
| 179 | + for (i = 0; i < rcc->num_entries - s->max_b_frames; i++) { | |
| 156 | 180 | RateControlEntry *rce; |
| 157 | 181 | int picture_number; |
| 158 | 182 | int e; |
| 159 | 183 | char *next; |
| 160 | 184 | |
| 161 | − next= strchr(p, ';'); | |
| 162 | − if(next){ | |
| 163 | − (*next)=0; //sscanf in unbelievably slow on looong strings //FIXME copy / do not write | |
| 185 | + next = strchr(p, ';'); | |
| 186 | + if (next) { | |
| 187 | + (*next) = 0; // sscanf in unbelievably slow on looong strings // FIXME copy / do not write | |
| 164 | 188 | next++; |
| 165 | 189 | } |
| 166 | − e= sscanf(p, " in:%d ", &picture_number); | |
| 190 | + e = sscanf(p, " in:%d ", &picture_number); | |
| 167 | 191 | |
| 168 | 192 | assert(picture_number >= 0); |
| 169 | 193 | assert(picture_number < rcc->num_entries); |
| 170 | − rce= &rcc->entry[picture_number]; | |
| 171 | − | |
| 172 | − e+=sscanf(p, " in:%*d out:%*d type:%d q:%f itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d", | |
| 173 | − &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits, | |
| 174 | − &rce->f_code, &rce->b_code, &rce->mc_mb_var_sum, &rce->mb_var_sum, &rce->i_count, &rce->skip_count, &rce->header_bits); | |
| 175 | − if(e!=14){ | |
| 176 | − av_log(s->avctx, AV_LOG_ERROR, "statistics are damaged at line %d, parser out=%d\n", i, e); | |
| 194 | + rce = &rcc->entry[picture_number]; | |
| 195 | + | |
| 196 | + e += sscanf(p, " in:%*d out:%*d type:%d q:%f itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d", | |
| 197 | + &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, | |
| 198 | + &rce->mv_bits, &rce->misc_bits, | |
| 199 | + &rce->f_code, &rce->b_code, | |
| 200 | + &rce->mc_mb_var_sum, &rce->mb_var_sum, | |
| 201 | + &rce->i_count, &rce->skip_count, &rce->header_bits); | |
| 202 | + if (e != 14) { | |
| 203 | + av_log(s->avctx, AV_LOG_ERROR, | |
| 204 | + "statistics are damaged at line %d, parser out=%d\n", | |
| 205 | + i, e); | |
| 177 | 206 | return -1; |
| 178 | 207 | } |
| 179 | 208 | |
| 180 | − p= next; | |
| 209 | + p = next; | |
| 181 | 210 | } |
| 182 | 211 | |
| 183 | − if(init_pass2(s) < 0) return -1; | |
| 212 | + if (init_pass2(s) < 0) | |
| 213 | + return -1; | |
| 184 | 214 | |
| 185 | − //FIXME maybe move to end | |
| 186 | − if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) { | |
| 215 | + // FIXME maybe move to end | |
| 216 | + if ((s->flags & CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) { | |
| 187 | 217 | #if CONFIG_LIBXVID |
| 188 | 218 | return ff_xvid_rate_control_init(s); |
| 189 | 219 | #else |
| 190 | − av_log(s->avctx, AV_LOG_ERROR, "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n"); | |
| 220 | + av_log(s->avctx, AV_LOG_ERROR, | |
| 221 | + "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n"); | |
| 191 | 222 | return -1; |
| 192 | 223 | #endif |
| 193 | 224 | } |
| 194 | 225 | } |
| 195 | 226 | |
| 196 | − if(!(s->flags&CODEC_FLAG_PASS2)){ | |
| 227 | + if (!(s->flags & CODEC_FLAG_PASS2)) { | |
| 228 | + rcc->short_term_qsum = 0.001; | |
| 229 | + rcc->short_term_qcount = 0.001; | |
| 197 | 230 | |
| 198 | − rcc->short_term_qsum=0.001; | |
| 199 | − rcc->short_term_qcount=0.001; | |
| 231 | + rcc->pass1_rc_eq_output_sum = 0.001; | |
| 232 | + rcc->pass1_wanted_bits = 0.001; | |
| 200 | 233 | |
| 201 | − rcc->pass1_rc_eq_output_sum= 0.001; | |
| 202 | − rcc->pass1_wanted_bits=0.001; | |
| 203 | − | |
| 204 | − if(s->avctx->qblur > 1.0){ | |
| 234 | + if (s->avctx->qblur > 1.0) { | |
| 205 | 235 | av_log(s->avctx, AV_LOG_ERROR, "qblur too large\n"); |
| 206 | 236 | return -1; |
| 207 | 237 | } |
| 208 | 238 | /* init stuff with the user specified complexity */ |
| 209 | − if(s->avctx->rc_initial_cplx){ | |
| 210 | − for(i=0; i<60*30; i++){ | |
| 211 | − double bits= s->avctx->rc_initial_cplx * (i/10000.0 + 1.0)*s->mb_num; | |
| 239 | + if (s->avctx->rc_initial_cplx) { | |
| 240 | + for (i = 0; i < 60 * 30; i++) { | |
| 241 | + double bits = s->avctx->rc_initial_cplx * (i / 10000.0 + 1.0) * s->mb_num; | |
| 212 | 242 | RateControlEntry rce; |
| 213 | 243 | |
| 214 | − if (i%((s->gop_size+3)/4)==0) rce.pict_type= AV_PICTURE_TYPE_I; | |
| 215 | − else if(i%(s->max_b_frames+1)) rce.pict_type= AV_PICTURE_TYPE_B; | |
| 216 | − else rce.pict_type= AV_PICTURE_TYPE_P; | |
| 217 | − | |
| 218 | − rce.new_pict_type= rce.pict_type; | |
| 219 | − rce.mc_mb_var_sum= bits*s->mb_num/100000; | |
| 220 | − rce.mb_var_sum = s->mb_num; | |
| 221 | − rce.qscale = FF_QP2LAMBDA * 2; | |
| 222 | − rce.f_code = 2; | |
| 223 | − rce.b_code = 1; | |
| 224 | − rce.misc_bits= 1; | |
| 225 | − | |
| 226 | − if(s->pict_type== AV_PICTURE_TYPE_I){ | |
| 227 | − rce.i_count = s->mb_num; | |
| 228 | − rce.i_tex_bits= bits; | |
| 229 | − rce.p_tex_bits= 0; | |
| 230 | − rce.mv_bits= 0; | |
| 231 | − }else{ | |
| 232 | − rce.i_count = 0; //FIXME we do know this approx | |
| 233 | − rce.i_tex_bits= 0; | |
| 234 | − rce.p_tex_bits= bits*0.9; | |
| 235 | − rce.mv_bits= bits*0.1; | |
| 244 | + if (i % ((s->gop_size + 3) / 4) == 0) | |
| 245 | + rce.pict_type = AV_PICTURE_TYPE_I; | |
| 246 | + else if (i % (s->max_b_frames + 1)) | |
| 247 | + rce.pict_type = AV_PICTURE_TYPE_B; | |
| 248 | + else | |
| 249 | + rce.pict_type = AV_PICTURE_TYPE_P; | |
| 250 | + | |
| 251 | + rce.new_pict_type = rce.pict_type; | |
| 252 | + rce.mc_mb_var_sum = bits * s->mb_num / 100000; | |
| 253 | + rce.mb_var_sum = s->mb_num; | |
| 254 | + | |
| 255 | + rce.qscale = FF_QP2LAMBDA * 2; | |
| 256 | + rce.f_code = 2; | |
| 257 | + rce.b_code = 1; | |
| 258 | + rce.misc_bits = 1; | |
| 259 | + | |
| 260 | + if (s->pict_type == AV_PICTURE_TYPE_I) { | |
| 261 | + rce.i_count = s->mb_num; | |
| 262 | + rce.i_tex_bits = bits; | |
| 263 | + rce.p_tex_bits = 0; | |
| 264 | + rce.mv_bits = 0; | |
| 265 | + } else { | |
| 266 | + rce.i_count = 0; // FIXME we do know this approx | |
| 267 | + rce.i_tex_bits = 0; | |
| 268 | + rce.p_tex_bits = bits * 0.9; | |
| 269 | + rce.mv_bits = bits * 0.1; | |
| 236 | 270 | } |
| 237 | − rcc->i_cplx_sum [rce.pict_type] += rce.i_tex_bits*rce.qscale; | |
| 238 | − rcc->p_cplx_sum [rce.pict_type] += rce.p_tex_bits*rce.qscale; | |
| 271 | + rcc->i_cplx_sum[rce.pict_type] += rce.i_tex_bits * rce.qscale; | |
| 272 | + rcc->p_cplx_sum[rce.pict_type] += rce.p_tex_bits * rce.qscale; | |
| 239 | 273 | rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits; |
| 240 | − rcc->frame_count[rce.pict_type] ++; | |
| 274 | + rcc->frame_count[rce.pict_type]++; | |
| 275 | + | |
| 276 | + get_qscale(s, &rce, rcc->pass1_wanted_bits / rcc->pass1_rc_eq_output_sum, i); | |
| 241 | 277 | |
| 242 | − get_qscale(s, &rce, rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum, i); | |
| 243 | − rcc->pass1_wanted_bits+= s->bit_rate/(1/av_q2d(s->avctx->time_base)); //FIXME misbehaves a little for variable fps | |
| 278 | + // FIXME misbehaves a little for variable fps | |
| 279 | + rcc->pass1_wanted_bits += s->bit_rate / (1 / av_q2d(s->avctx->time_base)); | |
| 244 | 280 | } |
| 245 | 281 | } |
| 246 | − | |
| 247 | 282 | } |
| 248 | 283 | |
| 249 | 284 | return 0; |
| @@ -251,47 +286,49 @@ int ff_rate_control_init(MpegEncContext *s) · 1 unmodified line | ||
| 251 | 286 | |
| 252 | 287 | void ff_rate_control_uninit(MpegEncContext *s) |
| 253 | 288 | { |
| 254 | − RateControlContext *rcc= &s->rc_context; | |
| 289 | + RateControlContext *rcc = &s->rc_context; | |
| 255 | 290 | emms_c(); |
| 256 | 291 | |
| 257 | 292 | av_expr_free(rcc->rc_eq_eval); |
| 258 | 293 | av_freep(&rcc->entry); |
| 259 | 294 | |
| 260 | 295 | #if CONFIG_LIBXVID |
| 261 | − if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) | |
| 296 | + if ((s->flags & CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) | |
| 262 | 297 | ff_xvid_rate_control_uninit(s); |
| 263 | 298 | #endif |
| 264 | 299 | } |
| 265 | 300 | |
| 266 | −int ff_vbv_update(MpegEncContext *s, int frame_size){ | |
| 267 | − RateControlContext *rcc= &s->rc_context; | |
| 268 | − const double fps= 1/av_q2d(s->avctx->time_base); | |
| 269 | − const int buffer_size= s->avctx->rc_buffer_size; | |
| 270 | − const double min_rate= s->avctx->rc_min_rate/fps; | |
| 271 | − const double max_rate= s->avctx->rc_max_rate/fps; | |
| 301 | +int ff_vbv_update(MpegEncContext *s, int frame_size) | |
| 302 | +{ | |
| 303 | + RateControlContext *rcc = &s->rc_context; | |
| 304 | + const double fps = 1 / av_q2d(s->avctx->time_base); | |
| 305 | + const int buffer_size = s->avctx->rc_buffer_size; | |
| 306 | + const double min_rate = s->avctx->rc_min_rate / fps; | |
| 307 | + const double max_rate = s->avctx->rc_max_rate / fps; | |
| 272 | 308 | |
| 273 | 309 | av_dlog(s, "%d %f %d %f %f\n", |
| 274 | 310 | buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate); |
| 275 | − if(buffer_size){ | |
| 311 | + | |
| 312 | + if (buffer_size) { | |
| 276 | 313 | int left; |
| 277 | 314 | |
| 278 | − rcc->buffer_index-= frame_size; | |
| 279 | − if(rcc->buffer_index < 0){ | |
| 315 | + rcc->buffer_index -= frame_size; | |
| 316 | + if (rcc->buffer_index < 0) { | |
| 280 | 317 | av_log(s->avctx, AV_LOG_ERROR, "rc buffer underflow\n"); |
| 281 | − rcc->buffer_index= 0; | |
| 318 | + rcc->buffer_index = 0; | |
| 282 | 319 | } |
| 283 | 320 | |
| 284 | − left= buffer_size - rcc->buffer_index - 1; | |
| 321 | + left = buffer_size - rcc->buffer_index - 1; | |
| 285 | 322 | rcc->buffer_index += av_clip(left, min_rate, max_rate); |
| 286 | 323 | |
| 287 | − if(rcc->buffer_index > buffer_size){ | |
| 288 | − int stuffing= ceil((rcc->buffer_index - buffer_size)/8); | |
| 324 | + if (rcc->buffer_index > buffer_size) { | |
| 325 | + int stuffing = ceil((rcc->buffer_index - buffer_size) / 8); | |
| 289 | 326 | |
| 290 | − if(stuffing < 4 && s->codec_id == AV_CODEC_ID_MPEG4) | |
| 291 | − stuffing=4; | |
| 292 | − rcc->buffer_index -= 8*stuffing; | |
| 327 | + if (stuffing < 4 && s->codec_id == AV_CODEC_ID_MPEG4) | |
| 328 | + stuffing = 4; | |
| 329 | + rcc->buffer_index -= 8 * stuffing; | |
| 293 | 330 | |
| 294 | − if(s->avctx->debug & FF_DEBUG_RC) | |
| 331 | + if (s->avctx->debug & FF_DEBUG_RC) | |
| 295 | 332 | av_log(s->avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing); |
| 296 | 333 | |
| 297 | 334 | return stuffing; |
| @@ -303,34 +340,38 @@ int ff_vbv_update(MpegEncContext *s, int frame_size){ · 5 unmodified lines | ||
| 303 | 340 | /** |
| 304 | 341 | * Modify the bitrate curve from pass1 for one frame. |
| 305 | 342 | */ |
| 306 | −static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num){ | |
| 307 | − RateControlContext *rcc= &s->rc_context; | |
| 308 | − AVCodecContext *a= s->avctx; | |
| 343 | +static double get_qscale(MpegEncContext *s, RateControlEntry *rce, | |
| 344 | + double rate_factor, int frame_num) | |
| 345 | +{ | |
| 346 | + RateControlContext *rcc = &s->rc_context; | |
| 347 | + AVCodecContext *a = s->avctx; | |
| 348 | + const int pict_type = rce->new_pict_type; | |
| 349 | + const double mb_num = s->mb_num; | |
| 309 | 350 | double q, bits; |
| 310 | − const int pict_type= rce->new_pict_type; | |
| 311 | − const double mb_num= s->mb_num; | |
| 312 | 351 | int i; |
| 313 | 352 | |
| 314 | − double const_values[]={ | |
| 353 | + double const_values[] = { | |
| 315 | 354 | M_PI, |
| 316 | 355 | M_E, |
| 317 | − rce->i_tex_bits*rce->qscale, | |
| 318 | − rce->p_tex_bits*rce->qscale, | |
| 319 | − (rce->i_tex_bits + rce->p_tex_bits)*(double)rce->qscale, | |
| 320 | − rce->mv_bits/mb_num, | |
| 321 | − rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code)*0.5 : rce->f_code, | |
| 322 | − rce->i_count/mb_num, | |
| 323 | − rce->mc_mb_var_sum/mb_num, | |
| 324 | − rce->mb_var_sum/mb_num, | |
| 356 | + rce->i_tex_bits * rce->qscale, | |
| 357 | + rce->p_tex_bits * rce->qscale, | |
| 358 | + (rce->i_tex_bits + rce->p_tex_bits) * (double)rce->qscale, | |
| 359 | + rce->mv_bits / mb_num, | |
| 360 | + rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code) * 0.5 : rce->f_code, | |
| 361 | + rce->i_count / mb_num, | |
| 362 | + rce->mc_mb_var_sum / mb_num, | |
| 363 | + rce->mb_var_sum / mb_num, | |
| 325 | 364 | rce->pict_type == AV_PICTURE_TYPE_I, |
| 326 | 365 | rce->pict_type == AV_PICTURE_TYPE_P, |
| 327 | 366 | rce->pict_type == AV_PICTURE_TYPE_B, |
| 328 | 367 | rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type], |
| 329 | 368 | a->qcompress, |
| 330 | −/* rcc->last_qscale_for[AV_PICTURE_TYPE_I], | |
| 369 | +#if 0 | |
| 370 | + rcc->last_qscale_for[AV_PICTURE_TYPE_I], | |
| 331 | 371 | rcc->last_qscale_for[AV_PICTURE_TYPE_P], |
| 332 | 372 | rcc->last_qscale_for[AV_PICTURE_TYPE_B], |
| 333 | − rcc->next_non_b_qscale,*/ | |
| 373 | + rcc->next_non_b_qscale, | |
| 374 | +#endif | |
| 334 | 375 | rcc->i_cplx_sum[AV_PICTURE_TYPE_I] / (double)rcc->frame_count[AV_PICTURE_TYPE_I], |
| 335 | 376 | rcc->i_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P], |
| 336 | 377 | rcc->p_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P], |
| @@ -345,61 +386,71 @@ static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_f · 8 unmodified lines | ||
| 345 | 386 | return -1; |
| 346 | 387 | } |
| 347 | 388 | |
| 348 | − rcc->pass1_rc_eq_output_sum+= bits; | |
| 349 | − bits*=rate_factor; | |
| 350 | − if(bits<0.0) bits=0.0; | |
| 351 | − bits+= 1.0; //avoid 1/0 issues | |
| 389 | + rcc->pass1_rc_eq_output_sum += bits; | |
| 390 | + bits *= rate_factor; | |
| 391 | + if (bits < 0.0) | |
| 392 | + bits = 0.0; | |
| 393 | + bits += 1.0; // avoid 1/0 issues | |
| 352 | 394 | |
| 353 | 395 | /* user override */ |
| 354 | − for(i=0; i<s->avctx->rc_override_count; i++){ | |
| 355 | − RcOverride *rco= s->avctx->rc_override; | |
| 356 | − if(rco[i].start_frame > frame_num) continue; | |
| 357 | − if(rco[i].end_frame < frame_num) continue; | |
| 358 | − | |
| 359 | − if(rco[i].qscale) | |
| 360 | − bits= qp2bits(rce, rco[i].qscale); //FIXME move at end to really force it? | |
| 396 | + for (i = 0; i < s->avctx->rc_override_count; i++) { | |
| 397 | + RcOverride *rco = s->avctx->rc_override; | |
| 398 | + if (rco[i].start_frame > frame_num) | |
| 399 | + continue; | |
| 400 | + if (rco[i].end_frame < frame_num) | |
| 401 | + continue; | |
| 402 | + | |
| 403 | + if (rco[i].qscale) | |
| 404 | + bits = qp2bits(rce, rco[i].qscale); // FIXME move at end to really force it? | |
| 361 | 405 | else |
| 362 | − bits*= rco[i].quality_factor; | |
| 406 | + bits *= rco[i].quality_factor; | |
| 363 | 407 | } |
| 364 | 408 | |
| 365 | − q= bits2qp(rce, bits); | |
| 409 | + q = bits2qp(rce, bits); | |
| 366 | 410 | |
| 367 | 411 | /* I/B difference */ |
| 368 | − if (pict_type==AV_PICTURE_TYPE_I && s->avctx->i_quant_factor<0.0) | |
| 369 | − q= -q*s->avctx->i_quant_factor + s->avctx->i_quant_offset; | |
| 370 | − else if(pict_type==AV_PICTURE_TYPE_B && s->avctx->b_quant_factor<0.0) | |
| 371 | − q= -q*s->avctx->b_quant_factor + s->avctx->b_quant_offset; | |
| 372 | − if(q<1) q=1; | |
| 412 | + if (pict_type == AV_PICTURE_TYPE_I && s->avctx->i_quant_factor < 0.0) | |
| 413 | + q = -q * s->avctx->i_quant_factor + s->avctx->i_quant_offset; | |
| 414 | + else if (pict_type == AV_PICTURE_TYPE_B && s->avctx->b_quant_factor < 0.0) | |
| 415 | + q = -q * s->avctx->b_quant_factor + s->avctx->b_quant_offset; | |
| 416 | + if (q < 1) | |
| 417 | + q = 1; | |
| 373 | 418 | |
| 374 | 419 | return q; |
| 375 | 420 | } |
| 376 | 421 | |
| 377 | −static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q){ | |
| 378 | − RateControlContext *rcc= &s->rc_context; | |
| 379 | − AVCodecContext *a= s->avctx; | |
| 380 | − const int pict_type= rce->new_pict_type; | |
| 381 | − const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P]; | |
| 382 | − const double last_non_b_q= rcc->last_qscale_for[rcc->last_non_b_pict_type]; | |
| 383 | − | |
| 384 | − if (pict_type==AV_PICTURE_TYPE_I && (a->i_quant_factor>0.0 || rcc->last_non_b_pict_type==AV_PICTURE_TYPE_P)) | |
| 385 | − q= last_p_q *FFABS(a->i_quant_factor) + a->i_quant_offset; | |
| 386 | − else if(pict_type==AV_PICTURE_TYPE_B && a->b_quant_factor>0.0) | |
| 387 | − q= last_non_b_q* a->b_quant_factor + a->b_quant_offset; | |
| 388 | − if(q<1) q=1; | |
| 422 | +static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q) | |
| 423 | +{ | |
| 424 | + RateControlContext *rcc = &s->rc_context; | |
| 425 | + AVCodecContext *a = s->avctx; | |
| 426 | + const int pict_type = rce->new_pict_type; | |
| 427 | + const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P]; | |
| 428 | + const double last_non_b_q = rcc->last_qscale_for[rcc->last_non_b_pict_type]; | |
| 429 | + | |
| 430 | + if (pict_type == AV_PICTURE_TYPE_I && | |
| 431 | + (a->i_quant_factor > 0.0 || rcc->last_non_b_pict_type == AV_PICTURE_TYPE_P)) | |
| 432 | + q = last_p_q * FFABS(a->i_quant_factor) + a->i_quant_offset; | |
| 433 | + else if (pict_type == AV_PICTURE_TYPE_B && | |
| 434 | + a->b_quant_factor > 0.0) | |
| 435 | + q = last_non_b_q * a->b_quant_factor + a->b_quant_offset; | |
| 436 | + if (q < 1) | |
| 437 | + q = 1; | |
| 389 | 438 | |
| 390 | 439 | /* last qscale / qdiff stuff */ |
| 391 | − if(rcc->last_non_b_pict_type==pict_type || pict_type!=AV_PICTURE_TYPE_I){ | |
| 392 | − double last_q= rcc->last_qscale_for[pict_type]; | |
| 393 | − const int maxdiff= FF_QP2LAMBDA * a->max_qdiff; | |
| 394 | − | |
| 395 | − if (q > last_q + maxdiff) q= last_q + maxdiff; | |
| 396 | − else if(q < last_q - maxdiff) q= last_q - maxdiff; | |
| 440 | + if (rcc->last_non_b_pict_type == pict_type || pict_type != AV_PICTURE_TYPE_I) { | |
| 441 | + double last_q = rcc->last_qscale_for[pict_type]; | |
| 442 | + const int maxdiff = FF_QP2LAMBDA * a->max_qdiff; | |
| 443 | + | |
| 444 | + if (q > last_q + maxdiff) | |
| 445 | + q = last_q + maxdiff; | |
| 446 | + else if (q < last_q - maxdiff) | |
| 447 | + q = last_q - maxdiff; | |
| 397 | 448 | } |
| 398 | 449 | |
| 399 | − rcc->last_qscale_for[pict_type]= q; //Note we cannot do that after blurring | |
| 450 | + rcc->last_qscale_for[pict_type] = q; // Note we cannot do that after blurring | |
| 400 | 451 | |
| 401 | − if(pict_type!=AV_PICTURE_TYPE_B) | |
| 402 | − rcc->last_non_b_pict_type= pict_type; | |
| 452 | + if (pict_type != AV_PICTURE_TYPE_B) | |
| 453 | + rcc->last_non_b_pict_type = pict_type; | |
| 403 | 454 | |
| 404 | 455 | return q; |
| 405 | 456 | } |
| @@ -407,239 +458,269 @@ static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, doubl · 1 unmodified line | ||
| 407 | 458 | /** |
| 408 | 459 | * Get the qmin & qmax for pict_type. |
| 409 | 460 | */ |
| 410 | −static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type){ | |
| 411 | − int qmin= s->avctx->lmin; | |
| 412 | − int qmax= s->avctx->lmax; | |
| 461 | +static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type) | |
| 462 | +{ | |
| 463 | + int qmin = s->avctx->lmin; | |
| 464 | + int qmax = s->avctx->lmax; | |
| 413 | 465 | |
| 414 | 466 | assert(qmin <= qmax); |
| 415 | 467 | |
| 416 | − if(pict_type==AV_PICTURE_TYPE_B){ | |
| 417 | − qmin= (int)(qmin*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5); | |
| 418 | − qmax= (int)(qmax*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5); | |
| 419 | − }else if(pict_type==AV_PICTURE_TYPE_I){ | |
| 420 | − qmin= (int)(qmin*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5); | |
| 421 | − qmax= (int)(qmax*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5); | |
| 468 | + switch (pict_type) { | |
| 469 | + case AV_PICTURE_TYPE_B: | |
| 470 | + qmin = (int)(qmin * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset + 0.5); | |
| 471 | + qmax = (int)(qmax * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset + 0.5); | |
| 472 | + break; | |
| 473 | + case AV_PICTURE_TYPE_I: | |
| 474 | + qmin = (int)(qmin * FFABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset + 0.5); | |
| 475 | + qmax = (int)(qmax * FFABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset + 0.5); | |
| 476 | + break; | |
| 422 | 477 | } |
| 423 | 478 | |
| 424 | − qmin= av_clip(qmin, 1, FF_LAMBDA_MAX); | |
| 425 | − qmax= av_clip(qmax, 1, FF_LAMBDA_MAX); | |
| 479 | + qmin = av_clip(qmin, 1, FF_LAMBDA_MAX); | |
| 480 | + qmax = av_clip(qmax, 1, FF_LAMBDA_MAX); | |
| 426 | 481 | |
| 427 | − if(qmax<qmin) qmax= qmin; | |
| 482 | + if (qmax < qmin) | |
| 483 | + qmax = qmin; | |
| 428 | 484 | |
| 429 | − *qmin_ret= qmin; | |
| 430 | − *qmax_ret= qmax; | |
| 485 | + *qmin_ret = qmin; | |
| 486 | + *qmax_ret = qmax; | |
| 431 | 487 | } |
| 432 | 488 | |
| 433 | −static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num){ | |
| 434 | − RateControlContext *rcc= &s->rc_context; | |
| 489 | +static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, | |
| 490 | + double q, int frame_num) | |
| 491 | +{ | |
| 492 | + RateControlContext *rcc = &s->rc_context; | |
| 493 | + const double buffer_size = s->avctx->rc_buffer_size; | |
| 494 | + const double fps = 1 / av_q2d(s->avctx->time_base); | |
| 495 | + const double min_rate = s->avctx->rc_min_rate / fps; | |
| 496 | + const double max_rate = s->avctx->rc_max_rate / fps; | |
| 497 | + const int pict_type = rce->new_pict_type; | |
| 435 | 498 | int qmin, qmax; |
| 436 | − const int pict_type= rce->new_pict_type; | |
| 437 | − const double buffer_size= s->avctx->rc_buffer_size; | |
| 438 | − const double fps= 1/av_q2d(s->avctx->time_base); | |
| 439 | − const double min_rate= s->avctx->rc_min_rate / fps; | |
| 440 | − const double max_rate= s->avctx->rc_max_rate / fps; | |
| 441 | 499 | |
| 442 | 500 | get_qminmax(&qmin, &qmax, s, pict_type); |
| 443 | 501 | |
| 444 | 502 | /* modulation */ |
| 445 | − if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==AV_PICTURE_TYPE_P) | |
| 446 | − q*= s->avctx->rc_qmod_amp; | |
| 503 | + if (s->avctx->rc_qmod_freq && | |
| 504 | + frame_num % s->avctx->rc_qmod_freq == 0 && | |
| 505 | + pict_type == AV_PICTURE_TYPE_P) | |
| 506 | + q *= s->avctx->rc_qmod_amp; | |
| 447 | 507 | |
| 448 | 508 | /* buffer overflow/underflow protection */ |
| 449 | − if(buffer_size){ | |
| 450 | − double expected_size= rcc->buffer_index; | |
| 509 | + if (buffer_size) { | |
| 510 | + double expected_size = rcc->buffer_index; | |
| 451 | 511 | double q_limit; |
| 452 | 512 | |
| 453 | − if(min_rate){ | |
| 454 | − double d= 2*(buffer_size - expected_size)/buffer_size; | |
| 455 | − if(d>1.0) d=1.0; | |
| 456 | − else if(d<0.0001) d=0.0001; | |
| 457 | − q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); | |
| 458 | − | |
| 459 | − q_limit= bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index) * s->avctx->rc_min_vbv_overflow_use, 1)); | |
| 460 | − if(q > q_limit){ | |
| 461 | − if(s->avctx->debug&FF_DEBUG_RC){ | |
| 462 | − av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit); | |
| 463 | − } | |
| 464 | − q= q_limit; | |
| 513 | + if (min_rate) { | |
| 514 | + double d = 2 * (buffer_size - expected_size) / buffer_size; | |
| 515 | + if (d > 1.0) | |
| 516 | + d = 1.0; | |
| 517 | + else if (d < 0.0001) | |
| 518 | + d = 0.0001; | |
| 519 | + q *= pow(d, 1.0 / s->avctx->rc_buffer_aggressivity); | |
| 520 | + | |
| 521 | + q_limit = bits2qp(rce, | |
| 522 | + FFMAX((min_rate - buffer_size + rcc->buffer_index) * | |
| 523 | + s->avctx->rc_min_vbv_overflow_use, 1)); | |
| 524 | + | |
| 525 | + if (q > q_limit) { | |
| 526 | + if (s->avctx->debug & FF_DEBUG_RC) | |
| 527 | + av_log(s->avctx, AV_LOG_DEBUG, | |
| 528 | + "limiting QP %f -> %f\n", q, q_limit); | |
| 529 | + q = q_limit; | |
| 465 | 530 | } |
| 466 | 531 | } |
| 467 | 532 | |
| 468 | − if(max_rate){ | |
| 469 | − double d= 2*expected_size/buffer_size; | |
| 470 | − if(d>1.0) d=1.0; | |
| 471 | − else if(d<0.0001) d=0.0001; | |
| 472 | − q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); | |
| 473 | − | |
| 474 | − q_limit= bits2qp(rce, FFMAX(rcc->buffer_index * s->avctx->rc_max_available_vbv_use, 1)); | |
| 475 | − if(q < q_limit){ | |
| 476 | − if(s->avctx->debug&FF_DEBUG_RC){ | |
| 477 | − av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit); | |
| 478 | − } | |
| 479 | − q= q_limit; | |
| 533 | + if (max_rate) { | |
| 534 | + double d = 2 * expected_size / buffer_size; | |
| 535 | + if (d > 1.0) | |
| 536 | + d = 1.0; | |
| 537 | + else if (d < 0.0001) | |
| 538 | + d = 0.0001; | |
| 539 | + q /= pow(d, 1.0 / s->avctx->rc_buffer_aggressivity); | |
| 540 | + | |
| 541 | + q_limit = bits2qp(rce, | |
| 542 | + FFMAX(rcc->buffer_index * | |
| 543 | + s->avctx->rc_max_available_vbv_use, | |
| 544 | + 1)); | |
| 545 | + if (q < q_limit) { | |
| 546 | + if (s->avctx->debug & FF_DEBUG_RC) | |
| 547 | + av_log(s->avctx, AV_LOG_DEBUG, | |
| 548 | + "limiting QP %f -> %f\n", q, q_limit); | |
| 549 | + q = q_limit; | |
| 480 | 550 | } |
| 481 | 551 | } |
| 482 | 552 | } |
| 483 | 553 | av_dlog(s, "q:%f max:%f min:%f size:%f index:%f agr:%f\n", |
| 484 | 554 | q, max_rate, min_rate, buffer_size, rcc->buffer_index, |
| 485 | 555 | s->avctx->rc_buffer_aggressivity); |
| 486 | − if(s->avctx->rc_qsquish==0.0 || qmin==qmax){ | |
| 487 | − if (q<qmin) q=qmin; | |
| 488 | − else if(q>qmax) q=qmax; | |
| 489 | − }else{ | |
| 490 | − double min2= log(qmin); | |
| 491 | − double max2= log(qmax); | |
| 492 | − | |
| 493 | − q= log(q); | |
| 494 | − q= (q - min2)/(max2-min2) - 0.5; | |
| 495 | − q*= -4.0; | |
| 496 | − q= 1.0/(1.0 + exp(q)); | |
| 497 | − q= q*(max2-min2) + min2; | |
| 498 | − | |
| 499 | − q= exp(q); | |
| 556 | + if (s->avctx->rc_qsquish == 0.0 || qmin == qmax) { | |
| 557 | + if (q < qmin) | |
| 558 | + q = qmin; | |
| 559 | + else if (q > qmax) | |
| 560 | + q = qmax; | |
| 561 | + } else { | |
| 562 | + double min2 = log(qmin); | |
| 563 | + double max2 = log(qmax); | |
| 564 | + | |
| 565 | + q = log(q); | |
| 566 | + q = (q - min2) / (max2 - min2) - 0.5; | |
| 567 | + q *= -4.0; | |
| 568 | + q = 1.0 / (1.0 + exp(q)); | |
| 569 | + q = q * (max2 - min2) + min2; | |
| 570 | + | |
| 571 | + q = exp(q); | |
| 500 | 572 | } |
| 501 | 573 | |
| 502 | 574 | return q; |
| 503 | 575 | } |
| 504 | 576 | |
| 505 | −//---------------------------------- | |
| 577 | +// ---------------------------------- | |
| 506 | 578 | // 1 Pass Code |
| 507 | 579 | |
| 508 | 580 | static double predict_size(Predictor *p, double q, double var) |
| 509 | 581 | { |
| 510 | − return p->coeff*var / (q*p->count); | |
| 582 | + return p->coeff * var / (q * p->count); | |
| 511 | 583 | } |
| 512 | 584 | |
| 513 | 585 | static void update_predictor(Predictor *p, double q, double var, double size) |
| 514 | 586 | { |
| 515 | − double new_coeff= size*q / (var + 1); | |
| 516 | − if(var<10) return; | |
| 587 | + double new_coeff = size * q / (var + 1); | |
| 588 | + if (var < 10) | |
| 589 | + return; | |
| 517 | 590 | |
| 518 | − p->count*= p->decay; | |
| 519 | − p->coeff*= p->decay; | |
| 591 | + p->count *= p->decay; | |
| 592 | + p->coeff *= p->decay; | |
| 520 | 593 | p->count++; |
| 521 | − p->coeff+= new_coeff; | |
| 594 | + p->coeff += new_coeff; | |
| 522 | 595 | } |
| 523 | 596 | |
| 524 | −static void adaptive_quantization(MpegEncContext *s, double q){ | |
| 597 | +static void adaptive_quantization(MpegEncContext *s, double q) | |
| 598 | +{ | |
| 525 | 599 | int i; |
| 526 | − const float lumi_masking= s->avctx->lumi_masking / (128.0*128.0); | |
| 527 | − const float dark_masking= s->avctx->dark_masking / (128.0*128.0); | |
| 528 | − const float temp_cplx_masking= s->avctx->temporal_cplx_masking; | |
| 600 | + const float lumi_masking = s->avctx->lumi_masking / (128.0 * 128.0); | |
| 601 | + const float dark_masking = s->avctx->dark_masking / (128.0 * 128.0); | |
| 602 | + const float temp_cplx_masking = s->avctx->temporal_cplx_masking; | |
| 529 | 603 | const float spatial_cplx_masking = s->avctx->spatial_cplx_masking; |
| 530 | − const float p_masking = s->avctx->p_masking; | |
| 531 | − const float border_masking = s->avctx->border_masking; | |
| 532 | − float bits_sum= 0.0; | |
| 533 | − float cplx_sum= 0.0; | |
| 534 | − float *cplx_tab = s->cplx_tab; | |
| 535 | − float *bits_tab = s->bits_tab; | |
| 536 | − const int qmin= s->avctx->mb_lmin; | |
| 537 | − const int qmax= s->avctx->mb_lmax; | |
| 538 | − Picture * const pic= &s->current_picture; | |
| 539 | − const int mb_width = s->mb_width; | |
| 540 | − const int mb_height = s->mb_height; | |
| 541 | − | |
| 542 | − for(i=0; i<s->mb_num; i++){ | |
| 543 | − const int mb_xy= s->mb_index2xy[i]; | |
| 544 | − float temp_cplx= sqrt(pic->mc_mb_var[mb_xy]); //FIXME merge in pow() | |
| 545 | − float spat_cplx= sqrt(pic->mb_var[mb_xy]); | |
| 546 | − const int lumi= pic->mb_mean[mb_xy]; | |
| 604 | + const float p_masking = s->avctx->p_masking; | |
| 605 | + const float border_masking = s->avctx->border_masking; | |
| 606 | + float bits_sum = 0.0; | |
| 607 | + float cplx_sum = 0.0; | |
| 608 | + float *cplx_tab = s->cplx_tab; | |
| 609 | + float *bits_tab = s->bits_tab; | |
| 610 | + const int qmin = s->avctx->mb_lmin; | |
| 611 | + const int qmax = s->avctx->mb_lmax; | |
| 612 | + Picture *const pic = &s->current_picture; | |
| 613 | + const int mb_width = s->mb_width; | |
| 614 | + const int mb_height = s->mb_height; | |
| 615 | + | |
| 616 | + for (i = 0; i < s->mb_num; i++) { | |
| 617 | + const int mb_xy = s->mb_index2xy[i]; | |
| 618 | + float temp_cplx = sqrt(pic->mc_mb_var[mb_xy]); // FIXME merge in pow() | |
| 619 | + float spat_cplx = sqrt(pic->mb_var[mb_xy]); | |
| 620 | + const int lumi = pic->mb_mean[mb_xy]; | |
| 547 | 621 | float bits, cplx, factor; |
| 548 | 622 | int mb_x = mb_xy % s->mb_stride; |
| 549 | 623 | int mb_y = mb_xy / s->mb_stride; |
| 550 | 624 | int mb_distance; |
| 551 | 625 | float mb_factor = 0.0; |
| 552 | − if(spat_cplx < 4) spat_cplx= 4; //FIXME finetune | |
| 553 | − if(temp_cplx < 4) temp_cplx= 4; //FIXME finetune | |
| 554 | − | |
| 555 | − if((s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTRA)){//FIXME hq mode | |
| 556 | − cplx= spat_cplx; | |
| 557 | − factor= 1.0 + p_masking; | |
| 558 | − }else{ | |
| 559 | − cplx= temp_cplx; | |
| 560 | − factor= pow(temp_cplx, - temp_cplx_masking); | |
| 626 | + if (spat_cplx < 4) | |
| 627 | + spat_cplx = 4; // FIXME finetune | |
| 628 | + if (temp_cplx < 4) | |
| 629 | + temp_cplx = 4; // FIXME finetune | |
| 630 | + | |
| 631 | + if ((s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_INTRA)) { // FIXME hq mode | |
| 632 | + cplx = spat_cplx; | |
| 633 | + factor = 1.0 + p_masking; | |
| 634 | + } else { | |
| 635 | + cplx = temp_cplx; | |
| 636 | + factor = pow(temp_cplx, -temp_cplx_masking); | |
| 561 | 637 | } |
| 562 | − factor*=pow(spat_cplx, - spatial_cplx_masking); | |
| 638 | + factor *= pow(spat_cplx, -spatial_cplx_masking); | |
| 563 | 639 | |
| 564 | − if(lumi>127) | |
| 565 | − factor*= (1.0 - (lumi-128)*(lumi-128)*lumi_masking); | |
| 640 | + if (lumi > 127) | |
| 641 | + factor *= (1.0 - (lumi - 128) * (lumi - 128) * lumi_masking); | |
| 566 | 642 | else |
| 567 | − factor*= (1.0 - (lumi-128)*(lumi-128)*dark_masking); | |
| 568 | − | |
| 569 | − if(mb_x < mb_width/5){ | |
| 570 | − mb_distance = mb_width/5 - mb_x; | |
| 571 | − mb_factor = (float)mb_distance / (float)(mb_width/5); | |
| 572 | − }else if(mb_x > 4*mb_width/5){ | |
| 573 | − mb_distance = mb_x - 4*mb_width/5; | |
| 574 | − mb_factor = (float)mb_distance / (float)(mb_width/5); | |
| 643 | + factor *= (1.0 - (lumi - 128) * (lumi - 128) * dark_masking); | |
| 644 | + | |
| 645 | + if (mb_x < mb_width / 5) { | |
| 646 | + mb_distance = mb_width / 5 - mb_x; | |
| 647 | + mb_factor = (float)mb_distance / (float)(mb_width / 5); | |
| 648 | + } else if (mb_x > 4 * mb_width / 5) { | |
| 649 | + mb_distance = mb_x - 4 * mb_width / 5; | |
| 650 | + mb_factor = (float)mb_distance / (float)(mb_width / 5); | |
| 575 | 651 | } |
| 576 | − if(mb_y < mb_height/5){ | |
| 577 | − mb_distance = mb_height/5 - mb_y; | |
| 578 | − mb_factor = FFMAX(mb_factor, (float)mb_distance / (float)(mb_height/5)); | |
| 579 | − }else if(mb_y > 4*mb_height/5){ | |
| 580 | − mb_distance = mb_y - 4*mb_height/5; | |
| 581 | − mb_factor = FFMAX(mb_factor, (float)mb_distance / (float)(mb_height/5)); | |
| 652 | + if (mb_y < mb_height / 5) { | |
| 653 | + mb_distance = mb_height / 5 - mb_y; | |
| 654 | + mb_factor = FFMAX(mb_factor, | |
| 655 | + (float)mb_distance / (float)(mb_height / 5)); | |
| 656 | + } else if (mb_y > 4 * mb_height / 5) { | |
| 657 | + mb_distance = mb_y - 4 * mb_height / 5; | |
| 658 | + mb_factor = FFMAX(mb_factor, | |
| 659 | + (float)mb_distance / (float)(mb_height / 5)); | |
| 582 | 660 | } |
| 583 | 661 | |
| 584 | − factor*= 1.0 - border_masking*mb_factor; | |
| 662 | + factor *= 1.0 - border_masking * mb_factor; | |
| 585 | 663 | |
| 586 | − if(factor<0.00001) factor= 0.00001; | |
| 664 | + if (factor < 0.00001) | |
| 665 | + factor = 0.00001; | |
| 587 | 666 | |
| 588 | − bits= cplx*factor; | |
| 589 | − cplx_sum+= cplx; | |
| 590 | − bits_sum+= bits; | |
| 591 | − cplx_tab[i]= cplx; | |
| 592 | − bits_tab[i]= bits; | |
| 667 | + bits = cplx * factor; | |
| 668 | + cplx_sum += cplx; | |
| 669 | + bits_sum += bits; | |
| 670 | + cplx_tab[i] = cplx; | |
| 671 | + bits_tab[i] = bits; | |
| 593 | 672 | } |
| 594 | 673 | |
| 595 | 674 | /* handle qmin/qmax clipping */ |
| 596 | − if(s->flags&CODEC_FLAG_NORMALIZE_AQP){ | |
| 597 | − float factor= bits_sum/cplx_sum; | |
| 598 | − for(i=0; i<s->mb_num; i++){ | |
| 599 | − float newq= q*cplx_tab[i]/bits_tab[i]; | |
| 600 | − newq*= factor; | |
| 675 | + if (s->flags & CODEC_FLAG_NORMALIZE_AQP) { | |
| 676 | + float factor = bits_sum / cplx_sum; | |
| 677 | + for (i = 0; i < s->mb_num; i++) { | |
| 678 | + float newq = q * cplx_tab[i] / bits_tab[i]; | |
| 679 | + newq *= factor; | |
| 601 | 680 | |
| 602 | − if (newq > qmax){ | |
| 681 | + if (newq > qmax) { | |
| 603 | 682 | bits_sum -= bits_tab[i]; |
| 604 | − cplx_sum -= cplx_tab[i]*q/qmax; | |
| 605 | − } | |
| 606 | − else if(newq < qmin){ | |
| 683 | + cplx_sum -= cplx_tab[i] * q / qmax; | |
| 684 | + } else if (newq < qmin) { | |
| 607 | 685 | bits_sum -= bits_tab[i]; |
| 608 | − cplx_sum -= cplx_tab[i]*q/qmin; | |
| 686 | + cplx_sum -= cplx_tab[i] * q / qmin; | |
| 609 | 687 | } |
| 610 | 688 | } |
| 611 | − if(bits_sum < 0.001) bits_sum= 0.001; | |
| 612 | − if(cplx_sum < 0.001) cplx_sum= 0.001; | |
| 689 | + if (bits_sum < 0.001) | |
| 690 | + bits_sum = 0.001; | |
| 691 | + if (cplx_sum < 0.001) | |
| 692 | + cplx_sum = 0.001; | |
| 613 | 693 | } |
| 614 | 694 | |
| 615 | − for(i=0; i<s->mb_num; i++){ | |
| 616 | − const int mb_xy= s->mb_index2xy[i]; | |
| 617 | − float newq= q*cplx_tab[i]/bits_tab[i]; | |
| 695 | + for (i = 0; i < s->mb_num; i++) { | |
| 696 | + const int mb_xy = s->mb_index2xy[i]; | |
| 697 | + float newq = q * cplx_tab[i] / bits_tab[i]; | |
| 618 | 698 | int intq; |
| 619 | 699 | |
| 620 | − if(s->flags&CODEC_FLAG_NORMALIZE_AQP){ | |
| 621 | − newq*= bits_sum/cplx_sum; | |
| 700 | + if (s->flags & CODEC_FLAG_NORMALIZE_AQP) { | |
| 701 | + newq *= bits_sum / cplx_sum; | |
| 622 | 702 | } |
| 623 | 703 | |
| 624 | − intq= (int)(newq + 0.5); | |
| 704 | + intq = (int)(newq + 0.5); | |
| 625 | 705 | |
| 626 | − if (intq > qmax) intq= qmax; | |
| 627 | − else if(intq < qmin) intq= qmin; | |
| 628 | − s->lambda_table[mb_xy]= intq; | |
| 706 | + if (intq > qmax) | |
| 707 | + intq = qmax; | |
| 708 | + else if (intq < qmin) | |
| 709 | + intq = qmin; | |
| 710 | + s->lambda_table[mb_xy] = intq; | |
| 629 | 711 | } |
| 630 | 712 | } |
| 631 | 713 | |
| 632 | −void ff_get_2pass_fcode(MpegEncContext *s){ | |
| 633 | − RateControlContext *rcc= &s->rc_context; | |
| 634 | − int picture_number= s->picture_number; | |
| 635 | − RateControlEntry *rce; | |
| 714 | +void ff_get_2pass_fcode(MpegEncContext *s) | |
| 715 | +{ | |
| 716 | + RateControlContext *rcc = &s->rc_context; | |
| 717 | + RateControlEntry *rce = &rcc->entry[s->picture_number]; | |
| 636 | 718 | |
| 637 | − rce= &rcc->entry[picture_number]; | |
| 638 | − s->f_code= rce->f_code; | |
| 639 | − s->b_code= rce->b_code; | |
| 719 | + s->f_code = rce->f_code; | |
| 720 | + s->b_code = rce->b_code; | |
| 640 | 721 | } |
| 641 | 722 | |
| 642 | −//FIXME rd or at least approx for dquant | |
| 723 | +// FIXME rd or at least approx for dquant | |
| 643 | 724 | |
| 644 | 725 | float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run) |
| 645 | 726 | { |
| @@ -649,249 +730,272 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run) · 3 unmodified lines | ||
| 649 | 730 | double diff; |
| 650 | 731 | double short_term_q; |
| 651 | 732 | double fps; |
| 652 | − int picture_number= s->picture_number; | |
| 733 | + int picture_number = s->picture_number; | |
| 653 | 734 | int64_t wanted_bits; |
| 654 | − RateControlContext *rcc= &s->rc_context; | |
| 655 | − AVCodecContext *a= s->avctx; | |
| 735 | + RateControlContext *rcc = &s->rc_context; | |
| 736 | + AVCodecContext *a = s->avctx; | |
| 656 | 737 | RateControlEntry local_rce, *rce; |
| 657 | 738 | double bits; |
| 658 | 739 | double rate_factor; |
| 659 | 740 | int var; |
| 660 | − const int pict_type= s->pict_type; | |
| 661 | − Picture * const pic= &s->current_picture; | |
| 741 | + const int pict_type = s->pict_type; | |
| 742 | + Picture * const pic = &s->current_picture; | |
| 662 | 743 | emms_c(); |
| 663 | 744 | |
| 664 | 745 | #if CONFIG_LIBXVID |
| 665 | − if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) | |
| 746 | + if ((s->flags & CODEC_FLAG_PASS2) && | |
| 747 | + s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) | |
| 666 | 748 | return ff_xvid_rate_estimate_qscale(s, dry_run); |
| 667 | 749 | #endif |
| 668 | 750 | |
| 669 | 751 | get_qminmax(&qmin, &qmax, s, pict_type); |
| 670 | 752 | |
| 671 | − fps= 1/av_q2d(s->avctx->time_base); | |
| 672 | − /* update predictors */ | |
| 673 | − if(picture_number>2 && !dry_run){ | |
| 674 | − const int last_var= s->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum; | |
| 675 | − update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits); | |
| 753 | + fps = 1 / av_q2d(s->avctx->time_base); | |
| 754 | + /* update predictors */ | |
| 755 | + if (picture_number > 2 && !dry_run) { | |
| 756 | + const int last_var = s->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum | |
| 757 | + : rcc->last_mc_mb_var_sum; | |
| 758 | + update_predictor(&rcc->pred[s->last_pict_type], | |
| 759 | + rcc->last_qscale, | |
| 760 | + sqrt(last_var), s->frame_bits); | |
| 676 | 761 | } |
| 677 | 762 | |
| 678 | − if(s->flags&CODEC_FLAG_PASS2){ | |
| 679 | − assert(picture_number>=0); | |
| 680 | − assert(picture_number<rcc->num_entries); | |
| 681 | − rce= &rcc->entry[picture_number]; | |
| 682 | − wanted_bits= rce->expected_bits; | |
| 683 | − }else{ | |
| 763 | + if (s->flags & CODEC_FLAG_PASS2) { | |
| 764 | + assert(picture_number >= 0); | |
| 765 | + assert(picture_number < rcc->num_entries); | |
| 766 | + rce = &rcc->entry[picture_number]; | |
| 767 | + wanted_bits = rce->expected_bits; | |
| 768 | + } else { | |
| 684 | 769 | Picture *dts_pic; |
| 685 | − rce= &local_rce; | |
| 770 | + rce = &local_rce; | |
| 686 | 771 | |
| 687 | − //FIXME add a dts field to AVFrame and ensure its set and use it here instead of reordering | |
| 688 | − //but the reordering is simpler for now until h.264 b pyramid must be handeld | |
| 689 | − if(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) | |
| 690 | − dts_pic= s->current_picture_ptr; | |
| 772 | + /* FIXME add a dts field to AVFrame and ensure it is set and use it | |
| 773 | + * here instead of reordering but the reordering is simpler for now | |
| 774 | + * until H.264 B-pyramid must be handled. */ | |
| 775 | + if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) | |
| 776 | + dts_pic = s->current_picture_ptr; | |
| 691 | 777 | else |
| 692 | − dts_pic= s->last_picture_ptr; | |
| 778 | + dts_pic = s->last_picture_ptr; | |
| 693 | 779 | |
| 694 | 780 | if (!dts_pic || dts_pic->f.pts == AV_NOPTS_VALUE) |
| 695 | − wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps); | |
| 781 | + wanted_bits = (uint64_t)(s->bit_rate * (double)picture_number / fps); | |
| 696 | 782 | else |
| 697 | − wanted_bits = (uint64_t)(s->bit_rate*(double)dts_pic->f.pts / fps); | |
| 783 | + wanted_bits = (uint64_t)(s->bit_rate * (double)dts_pic->f.pts / fps); | |
| 698 | 784 | } |
| 699 | 785 | |
| 700 | − diff= s->total_bits - wanted_bits; | |
| 701 | − br_compensation= (a->bit_rate_tolerance - diff)/a->bit_rate_tolerance; | |
| 702 | − if(br_compensation<=0.0) br_compensation=0.001; | |
| 786 | + diff = s->total_bits - wanted_bits; | |
| 787 | + br_compensation = (a->bit_rate_tolerance - diff) / a->bit_rate_tolerance; | |
| 788 | + if (br_compensation <= 0.0) | |
| 789 | + br_compensation = 0.001; | |
| 703 | 790 | |
| 704 | − var= pict_type == AV_PICTURE_TYPE_I ? pic->mb_var_sum : pic->mc_mb_var_sum; | |
| 791 | + var = pict_type == AV_PICTURE_TYPE_I ? pic->mb_var_sum : pic->mc_mb_var_sum; | |
| 705 | 792 | |
| 706 | 793 | short_term_q = 0; /* avoid warning */ |
| 707 | − if(s->flags&CODEC_FLAG_PASS2){ | |
| 708 | − if(pict_type!=AV_PICTURE_TYPE_I) | |
| 794 | + if (s->flags & CODEC_FLAG_PASS2) { | |
| 795 | + if (pict_type != AV_PICTURE_TYPE_I) | |
| 709 | 796 | assert(pict_type == rce->new_pict_type); |
| 710 | 797 | |
| 711 | − q= rce->new_qscale / br_compensation; | |
| 798 | + q = rce->new_qscale / br_compensation; | |
| 712 | 799 | av_dlog(s, "%f %f %f last:%d var:%d type:%d//\n", q, rce->new_qscale, |
| 713 | 800 | br_compensation, s->frame_bits, var, pict_type); |
| 714 | − }else{ | |
| 715 | − rce->pict_type= | |
| 716 | − rce->new_pict_type= pict_type; | |
| 717 | − rce->mc_mb_var_sum= pic->mc_mb_var_sum; | |
| 718 | − rce->mb_var_sum = pic-> mb_var_sum; | |
| 719 | − rce->qscale = FF_QP2LAMBDA * 2; | |
| 720 | − rce->f_code = s->f_code; | |
| 721 | − rce->b_code = s->b_code; | |
| 722 | − rce->misc_bits= 1; | |
| 723 | − | |
| 724 | − bits= predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var)); | |
| 725 | − if(pict_type== AV_PICTURE_TYPE_I){ | |
| 726 | − rce->i_count = s->mb_num; | |
| 727 | − rce->i_tex_bits= bits; | |
| 728 | − rce->p_tex_bits= 0; | |
| 729 | − rce->mv_bits= 0; | |
| 730 | − }else{ | |
| 731 | − rce->i_count = 0; //FIXME we do know this approx | |
| 732 | − rce->i_tex_bits= 0; | |
| 733 | − rce->p_tex_bits= bits*0.9; | |
| 734 | − | |
| 735 | − rce->mv_bits= bits*0.1; | |
| 801 | + } else { | |
| 802 | + rce->pict_type = | |
| 803 | + rce->new_pict_type = pict_type; | |
| 804 | + rce->mc_mb_var_sum = pic->mc_mb_var_sum; | |
| 805 | + rce->mb_var_sum = pic->mb_var_sum; | |
| 806 | + rce->qscale = FF_QP2LAMBDA * 2; | |
| 807 | + rce->f_code = s->f_code; | |
| 808 | + rce->b_code = s->b_code; | |
| 809 | + rce->misc_bits = 1; | |
| 810 | + | |
| 811 | + bits = predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var)); | |
| 812 | + if (pict_type == AV_PICTURE_TYPE_I) { | |
| 813 | + rce->i_count = s->mb_num; | |
| 814 | + rce->i_tex_bits = bits; | |
| 815 | + rce->p_tex_bits = 0; | |
| 816 | + rce->mv_bits = 0; | |
| 817 | + } else { | |
| 818 | + rce->i_count = 0; // FIXME we do know this approx | |
| 819 | + rce->i_tex_bits = 0; | |
| 820 | + rce->p_tex_bits = bits * 0.9; | |
| 821 | + rce->mv_bits = bits * 0.1; | |
| 736 | 822 | } |
| 737 | − rcc->i_cplx_sum [pict_type] += rce->i_tex_bits*rce->qscale; | |
| 738 | − rcc->p_cplx_sum [pict_type] += rce->p_tex_bits*rce->qscale; | |
| 823 | + rcc->i_cplx_sum[pict_type] += rce->i_tex_bits * rce->qscale; | |
| 824 | + rcc->p_cplx_sum[pict_type] += rce->p_tex_bits * rce->qscale; | |
| 739 | 825 | rcc->mv_bits_sum[pict_type] += rce->mv_bits; |
| 740 | − rcc->frame_count[pict_type] ++; | |
| 826 | + rcc->frame_count[pict_type]++; | |
| 741 | 827 | |
| 742 | − bits= rce->i_tex_bits + rce->p_tex_bits; | |
| 743 | − rate_factor= rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum * br_compensation; | |
| 828 | + bits = rce->i_tex_bits + rce->p_tex_bits; | |
| 829 | + rate_factor = rcc->pass1_wanted_bits / | |
| 830 | + rcc->pass1_rc_eq_output_sum * br_compensation; | |
| 744 | 831 | |
| 745 | − q= get_qscale(s, rce, rate_factor, picture_number); | |
| 832 | + q = get_qscale(s, rce, rate_factor, picture_number); | |
| 746 | 833 | if (q < 0) |
| 747 | 834 | return -1; |
| 748 | 835 | |
| 749 | − assert(q>0.0); | |
| 750 | − q= get_diff_limited_q(s, rce, q); | |
| 751 | − assert(q>0.0); | |
| 836 | + assert(q > 0.0); | |
| 837 | + q = get_diff_limited_q(s, rce, q); | |
| 838 | + assert(q > 0.0); | |
| 752 | 839 | |
| 753 | − if(pict_type==AV_PICTURE_TYPE_P || s->intra_only){ //FIXME type dependent blur like in 2-pass | |
| 754 | − rcc->short_term_qsum*=a->qblur; | |
| 755 | − rcc->short_term_qcount*=a->qblur; | |
| 840 | + // FIXME type dependent blur like in 2-pass | |
| 841 | + if (pict_type == AV_PICTURE_TYPE_P || s->intra_only) { | |
| 842 | + rcc->short_term_qsum *= a->qblur; | |
| 843 | + rcc->short_term_qcount *= a->qblur; | |
| 756 | 844 | |
| 757 | − rcc->short_term_qsum+= q; | |
| 845 | + rcc->short_term_qsum += q; | |
| 758 | 846 | rcc->short_term_qcount++; |
| 759 | − q= short_term_q= rcc->short_term_qsum/rcc->short_term_qcount; | |
| 847 | + q = short_term_q = rcc->short_term_qsum / rcc->short_term_qcount; | |
| 760 | 848 | } |
| 761 | − assert(q>0.0); | |
| 849 | + assert(q > 0.0); | |
| 762 | 850 | |
| 763 | − q= modify_qscale(s, rce, q, picture_number); | |
| 851 | + q = modify_qscale(s, rce, q, picture_number); | |
| 764 | 852 | |
| 765 | − rcc->pass1_wanted_bits+= s->bit_rate/fps; | |
| 853 | + rcc->pass1_wanted_bits += s->bit_rate / fps; | |
| 766 | 854 | |
| 767 | − assert(q>0.0); | |
| 855 | + assert(q > 0.0); | |
| 768 | 856 | } |
| 769 | 857 | |
| 770 | − if(s->avctx->debug&FF_DEBUG_RC){ | |
| 771 | − av_log(s->avctx, AV_LOG_DEBUG, "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f size:%d var:%d/%d br:%d fps:%d\n", | |
| 772 | − av_get_picture_type_char(pict_type), qmin, q, qmax, picture_number, (int)wanted_bits/1000, (int)s->total_bits/1000, | |
| 773 | − br_compensation, short_term_q, s->frame_bits, pic->mb_var_sum, pic->mc_mb_var_sum, s->bit_rate/1000, (int)fps | |
| 774 | − ); | |
| 858 | + if (s->avctx->debug & FF_DEBUG_RC) { | |
| 859 | + av_log(s->avctx, AV_LOG_DEBUG, | |
| 860 | + "%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f " | |
| 861 | + "size:%d var:%d/%d br:%d fps:%d\n", | |
| 862 | + av_get_picture_type_char(pict_type), | |
| 863 | + qmin, q, qmax, picture_number, | |
| 864 | + (int)wanted_bits / 1000, (int)s->total_bits / 1000, | |
| 865 | + br_compensation, short_term_q, s->frame_bits, | |
| 866 | + pic->mb_var_sum, pic->mc_mb_var_sum, | |
| 867 | + s->bit_rate / 1000, (int)fps); | |
| 775 | 868 | } |
| 776 | 869 | |
| 777 | − if (q<qmin) q=qmin; | |
| 778 | − else if(q>qmax) q=qmax; | |
| 870 | + if (q < qmin) | |
| 871 | + q = qmin; | |
| 872 | + else if (q > qmax) | |
| 873 | + q = qmax; | |
| 779 | 874 | |
| 780 | − if(s->adaptive_quant) | |
| 875 | + if (s->adaptive_quant) | |
| 781 | 876 | adaptive_quantization(s, q); |
| 782 | 877 | else |
| 783 | − q= (int)(q + 0.5); | |
| 878 | + q = (int)(q + 0.5); | |
| 784 | 879 | |
| 785 | − if(!dry_run){ | |
| 786 | − rcc->last_qscale= q; | |
| 787 | − rcc->last_mc_mb_var_sum= pic->mc_mb_var_sum; | |
| 788 | − rcc->last_mb_var_sum= pic->mb_var_sum; | |
| 880 | + if (!dry_run) { | |
| 881 | + rcc->last_qscale = q; | |
| 882 | + rcc->last_mc_mb_var_sum = pic->mc_mb_var_sum; | |
| 883 | + rcc->last_mb_var_sum = pic->mb_var_sum; | |
| 789 | 884 | } |
| 790 | 885 | return q; |
| 791 | 886 | } |
| 792 | 887 | |
| 793 | −//---------------------------------------------- | |
| 888 | +// ---------------------------------------------- | |
| 794 | 889 | // 2-Pass code |
| 795 | 890 | |
| 796 | 891 | static int init_pass2(MpegEncContext *s) |
| 797 | 892 | { |
| 798 | − RateControlContext *rcc= &s->rc_context; | |
| 799 | − AVCodecContext *a= s->avctx; | |
| 893 | + RateControlContext *rcc = &s->rc_context; | |
| 894 | + AVCodecContext *a = s->avctx; | |
| 800 | 895 | int i, toobig; |
| 801 | − double fps= 1/av_q2d(s->avctx->time_base); | |
| 802 | − double complexity[5]={0,0,0,0,0}; // approximate bits at quant=1 | |
| 803 | − uint64_t const_bits[5]={0,0,0,0,0}; // quantizer independent bits | |
| 896 | + double fps = 1 / av_q2d(s->avctx->time_base); | |
| 897 | + double complexity[5] = { 0 }; // approximate bits at quant=1 | |
| 898 | + uint64_t const_bits[5] = { 0 }; // quantizer independent bits | |
| 804 | 899 | uint64_t all_const_bits; |
| 805 | − uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps); | |
| 806 | − double rate_factor=0; | |
| 900 | + uint64_t all_available_bits = (uint64_t)(s->bit_rate * | |
| 901 | + (double)rcc->num_entries / fps); | |
| 902 | + double rate_factor = 0; | |
| 807 | 903 | double step; |
| 808 | − //int last_i_frame=-10000000; | |
| 809 | − const int filter_size= (int)(a->qblur*4) | 1; | |
| 904 | + const int filter_size = (int)(a->qblur * 4) | 1; | |
| 810 | 905 | double expected_bits; |
| 811 | 906 | double *qscale, *blurred_qscale, qscale_sum; |
| 812 | 907 | |
| 813 | 908 | /* find complexity & const_bits & decide the pict_types */ |
| 814 | − for(i=0; i<rcc->num_entries; i++){ | |
| 815 | − RateControlEntry *rce= &rcc->entry[i]; | |
| 909 | + for (i = 0; i < rcc->num_entries; i++) { | |
| 910 | + RateControlEntry *rce = &rcc->entry[i]; | |
| 816 | 911 | |
| 817 | − rce->new_pict_type= rce->pict_type; | |
| 818 | − rcc->i_cplx_sum [rce->pict_type] += rce->i_tex_bits*rce->qscale; | |
| 819 | − rcc->p_cplx_sum [rce->pict_type] += rce->p_tex_bits*rce->qscale; | |
| 912 | + rce->new_pict_type = rce->pict_type; | |
| 913 | + rcc->i_cplx_sum[rce->pict_type] += rce->i_tex_bits * rce->qscale; | |
| 914 | + rcc->p_cplx_sum[rce->pict_type] += rce->p_tex_bits * rce->qscale; | |
| 820 | 915 | rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits; |
| 821 | − rcc->frame_count[rce->pict_type] ++; | |
| 916 | + rcc->frame_count[rce->pict_type]++; | |
| 822 | 917 | |
| 823 | − complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale; | |
| 824 | − const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits; | |
| 918 | + complexity[rce->new_pict_type] += (rce->i_tex_bits + rce->p_tex_bits) * | |
| 919 | + (double)rce->qscale; | |
| 920 | + const_bits[rce->new_pict_type] += rce->mv_bits + rce->misc_bits; | |
| 825 | 921 | } |
| 826 | − all_const_bits= const_bits[AV_PICTURE_TYPE_I] + const_bits[AV_PICTURE_TYPE_P] + const_bits[AV_PICTURE_TYPE_B]; | |
| 827 | 922 | |
| 828 | − if(all_available_bits < all_const_bits){ | |
| 923 | + all_const_bits = const_bits[AV_PICTURE_TYPE_I] + | |
| 924 | + const_bits[AV_PICTURE_TYPE_P] + | |
| 925 | + const_bits[AV_PICTURE_TYPE_B]; | |
| 926 | + | |
| 927 | + if (all_available_bits < all_const_bits) { | |
| 829 | 928 | av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is too low\n"); |
| 830 | 929 | return -1; |
| 831 | 930 | } |
| 832 | 931 | |
| 833 | − qscale= av_malloc(sizeof(double)*rcc->num_entries); | |
| 834 | − blurred_qscale= av_malloc(sizeof(double)*rcc->num_entries); | |
| 932 | + qscale = av_malloc(sizeof(double) * rcc->num_entries); | |
| 933 | + blurred_qscale = av_malloc(sizeof(double) * rcc->num_entries); | |
| 835 | 934 | toobig = 0; |
| 836 | 935 | |
| 837 | − for(step=256*256; step>0.0000001; step*=0.5){ | |
| 838 | − expected_bits=0; | |
| 839 | − rate_factor+= step; | |
| 936 | + for (step = 256 * 256; step > 0.0000001; step *= 0.5) { | |
| 937 | + expected_bits = 0; | |
| 938 | + rate_factor += step; | |
| 840 | 939 | |
| 841 | − rcc->buffer_index= s->avctx->rc_buffer_size/2; | |
| 940 | + rcc->buffer_index = s->avctx->rc_buffer_size / 2; | |
| 842 | 941 | |
| 843 | 942 | /* find qscale */ |
| 844 | − for(i=0; i<rcc->num_entries; i++){ | |
| 845 | − RateControlEntry *rce= &rcc->entry[i]; | |
| 846 | − qscale[i]= get_qscale(s, &rcc->entry[i], rate_factor, i); | |
| 943 | + for (i = 0; i < rcc->num_entries; i++) { | |
| 944 | + RateControlEntry *rce = &rcc->entry[i]; | |
| 945 | + | |
| 946 | + qscale[i] = get_qscale(s, &rcc->entry[i], rate_factor, i); | |
| 847 | 947 | rcc->last_qscale_for[rce->pict_type] = qscale[i]; |
| 848 | 948 | } |
| 849 | − assert(filter_size%2==1); | |
| 949 | + assert(filter_size % 2 == 1); | |
| 850 | 950 | |
| 851 | 951 | /* fixed I/B QP relative to P mode */ |
| 852 | − for(i=rcc->num_entries-1; i>=0; i--){ | |
| 853 | − RateControlEntry *rce= &rcc->entry[i]; | |
| 952 | + for (i = rcc->num_entries - 1; i >= 0; i--) { | |
| 953 | + RateControlEntry *rce = &rcc->entry[i]; | |
| 854 | 954 | |
| 855 | − qscale[i]= get_diff_limited_q(s, rce, qscale[i]); | |
| 955 | + qscale[i] = get_diff_limited_q(s, rce, qscale[i]); | |
| 856 | 956 | } |
| 857 | 957 | |
| 858 | 958 | /* smooth curve */ |
| 859 | − for(i=0; i<rcc->num_entries; i++){ | |
| 860 | − RateControlEntry *rce= &rcc->entry[i]; | |
| 861 | − const int pict_type= rce->new_pict_type; | |
| 959 | + for (i = 0; i < rcc->num_entries; i++) { | |
| 960 | + RateControlEntry *rce = &rcc->entry[i]; | |
| 961 | + const int pict_type = rce->new_pict_type; | |
| 862 | 962 | int j; |
| 863 | − double q=0.0, sum=0.0; | |
| 864 | − | |
| 865 | − for(j=0; j<filter_size; j++){ | |
| 866 | − int index= i+j-filter_size/2; | |
| 867 | − double d= index-i; | |
| 868 | − double coeff= a->qblur==0 ? 1.0 : exp(-d*d/(a->qblur * a->qblur)); | |
| 869 | − | |
| 870 | − if(index < 0 || index >= rcc->num_entries) continue; | |
| 871 | − if(pict_type != rcc->entry[index].new_pict_type) continue; | |
| 872 | − q+= qscale[index] * coeff; | |
| 873 | − sum+= coeff; | |
| 963 | + double q = 0.0, sum = 0.0; | |
| 964 | + | |
| 965 | + for (j = 0; j < filter_size; j++) { | |
| 966 | + int index = i + j - filter_size / 2; | |
| 967 | + double d = index - i; | |
| 968 | + double coeff = a->qblur == 0 ? 1.0 : exp(-d * d / (a->qblur * a->qblur)); | |
| 969 | + | |
| 970 | + if (index < 0 || index >= rcc->num_entries) | |
| 971 | + continue; | |
| 972 | + if (pict_type != rcc->entry[index].new_pict_type) | |
| 973 | + continue; | |
| 974 | + q += qscale[index] * coeff; | |
| 975 | + sum += coeff; | |
| 874 | 976 | } |
| 875 | − blurred_qscale[i]= q/sum; | |
| 977 | + blurred_qscale[i] = q / sum; | |
| 876 | 978 | } |
| 877 | 979 | |
| 878 | 980 | /* find expected bits */ |
| 879 | − for(i=0; i<rcc->num_entries; i++){ | |
| 880 | − RateControlEntry *rce= &rcc->entry[i]; | |
| 981 | + for (i = 0; i < rcc->num_entries; i++) { | |
| 982 | + RateControlEntry *rce = &rcc->entry[i]; | |
| 881 | 983 | double bits; |
| 882 | − rce->new_qscale= modify_qscale(s, rce, blurred_qscale[i], i); | |
| 883 | − bits= qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits; | |
| 884 | − bits += 8*ff_vbv_update(s, bits); | |
| 885 | 984 | |
| 886 | − rce->expected_bits= expected_bits; | |
| 887 | − expected_bits += bits; | |
| 985 | + rce->new_qscale = modify_qscale(s, rce, blurred_qscale[i], i); | |
| 986 | + | |
| 987 | + bits = qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits; | |
| 988 | + bits += 8 * ff_vbv_update(s, bits); | |
| 989 | + | |
| 990 | + rce->expected_bits = expected_bits; | |
| 991 | + expected_bits += bits; | |
| 888 | 992 | } |
| 889 | 993 | |
| 890 | 994 | av_dlog(s->avctx, |
| 891 | 995 | "expected_bits: %f all_available_bits: %d rate_factor: %f\n", |
| 892 | 996 | expected_bits, (int)all_available_bits, rate_factor); |
| 893 | − if(expected_bits > all_available_bits) { | |
| 894 | − rate_factor-= step; | |
| 997 | + if (expected_bits > all_available_bits) { | |
| 998 | + rate_factor -= step; | |
| 895 | 999 | ++toobig; |
| 896 | 1000 | } |
| 897 | 1001 | } |
| @@ -900,33 +1004,34 @@ static int init_pass2(MpegEncContext *s) · 2 unmodified lines | ||
| 900 | 1004 | |
| 901 | 1005 | /* check bitrate calculations and print info */ |
| 902 | 1006 | qscale_sum = 0.0; |
| 903 | − for(i=0; i<rcc->num_entries; i++){ | |
| 1007 | + for (i = 0; i < rcc->num_entries; i++) { | |
| 904 | 1008 | av_dlog(s, "[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n", |
| 905 | 1009 | i, |
| 906 | 1010 | rcc->entry[i].new_qscale, |
| 907 | 1011 | rcc->entry[i].new_qscale / FF_QP2LAMBDA); |
| 908 | − qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA, s->avctx->qmin, s->avctx->qmax); | |
| 1012 | + qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA, | |
| 1013 | + s->avctx->qmin, s->avctx->qmax); | |
| 909 | 1014 | } |
| 910 | 1015 | assert(toobig <= 40); |
| 911 | 1016 | av_log(s->avctx, AV_LOG_DEBUG, |
| 912 | − "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n", | |
| 913 | − s->bit_rate, | |
| 914 | − (int)(expected_bits / ((double)all_available_bits/s->bit_rate))); | |
| 1017 | + "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n", | |
| 1018 | + s->bit_rate, | |
| 1019 | + (int)(expected_bits / ((double)all_available_bits / s->bit_rate))); | |
| 915 | 1020 | av_log(s->avctx, AV_LOG_DEBUG, |
| 916 | − "[lavc rc] estimated target average qp: %.3f\n", | |
| 917 | − (float)qscale_sum / rcc->num_entries); | |
| 1021 | + "[lavc rc] estimated target average qp: %.3f\n", | |
| 1022 | + (float)qscale_sum / rcc->num_entries); | |
| 918 | 1023 | if (toobig == 0) { |
| 919 | 1024 | av_log(s->avctx, AV_LOG_INFO, |
| 920 | − "[lavc rc] Using all of requested bitrate is not " | |
| 921 | − "necessary for this video with these parameters.\n"); | |
| 1025 | + "[lavc rc] Using all of requested bitrate is not " | |
| 1026 | + "necessary for this video with these parameters.\n"); | |
| 922 | 1027 | } else if (toobig == 40) { |
| 923 | 1028 | av_log(s->avctx, AV_LOG_ERROR, |
| 924 | − "[lavc rc] Error: bitrate too low for this video " | |
| 925 | − "with these parameters.\n"); | |
| 1029 | + "[lavc rc] Error: bitrate too low for this video " | |
| 1030 | + "with these parameters.\n"); | |
| 926 | 1031 | return -1; |
| 927 | − } else if (fabs(expected_bits/all_available_bits - 1.0) > 0.01) { | |
| 1032 | + } else if (fabs(expected_bits / all_available_bits - 1.0) > 0.01) { | |
| 928 | 1033 | av_log(s->avctx, AV_LOG_ERROR, |
| 929 | − "[lavc rc] Error: 2pass curve failed to converge\n"); | |
| 1034 | + "[lavc rc] Error: 2pass curve failed to converge\n"); | |
| 930 | 1035 | return -1; |
| 931 | 1036 | } |
| 932 | 1037 | |