break;
}
- s->lambda *= avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
+ /* rate control stuff
+ * allow between the nominal bitrate, and what psy's bit reservoir says to target
+ * but drift towards the nominal bitrate always
+ */
+ frame_bits = put_bits_count(&s->pb);
+ rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate;
+ rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3);
+ too_many_bits = FFMAX(target_bits, rate_bits);
+ too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3);
+ too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
+
+ /* When using ABR, be strict (but only for increasing) */
+ too_few_bits = too_few_bits - too_few_bits/8;
+ too_many_bits = too_many_bits + too_many_bits/2;
+
+ if ( its == 0 /* for steady-state Q-scale tracking */
+ || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
+ || frame_bits >= 6144 * s->channels - 3 )
+ {
+ float ratio = ((float)rate_bits) / frame_bits;
+
+ if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
+ /*
+ * This path is for steady-state Q-scale tracking
+ * When frame bits fall within the stable range, we still need to adjust
+ * lambda to maintain it like so in a stable fashion (large jumps in lambda
+ * create artifacts and should be avoided), but slowly
+ */
+ ratio = sqrtf(sqrtf(ratio));
+ ratio = av_clipf(ratio, 0.9f, 1.1f);
+ } else {
+ /* Not so fast though */
+ ratio = sqrtf(ratio);
+ }
+ s->lambda = FFMIN(s->lambda * ratio, 65536.f);
+ /* Keep iterating if we must reduce and lambda is in the sky */
+ if (ratio > 0.9f && ratio < 1.1f) {
+ break;
+ } else {
+ if (is_mode || ms_mode || tns_mode || pred_mode) {
+ for (i = 0; i < s->chan_map[0]; i++) {
+ // Must restore coeffs
+ chans = tag == TYPE_CPE ? 2 : 1;
+ cpe = &s->cpe[i];
+ for (ch = 0; ch < chans; ch++)
+ memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs));
+ }
+ }
+ its++;
+ }
+ } else {
+ break;
+ }
} while (1);
+ if (s->options.ltp && s->coder->ltp_insert_new_frame)
+ s->coder->ltp_insert_new_frame(s);
+
put_bits(&s->pb, 3, TYPE_END);
flush_put_bits(&s->pb);
- frame_bits = put_bits_count(&s->pb);
++
+ #if FF_API_STAT_BITS
+ FF_DISABLE_DEPRECATION_WARNINGS
- avctx->frame_bits = frame_bits;
+ avctx->frame_bits = put_bits_count(&s->pb);
+ FF_ENABLE_DEPRECATION_WARNINGS
+ #endif
+
- // rate control stuff
- if (!(avctx->flags & AV_CODEC_FLAG_QSCALE)) {
- float ratio = avctx->bit_rate * 1024.0f / avctx->sample_rate / frame_bits;
- s->lambda *= ratio;
- s->lambda = FFMIN(s->lambda, 65536.f);
- }
+ s->lambda_sum += s->lambda;
+ s->lambda_count++;
if (!frame)
s->last_frame++;
if (ret < 0)
return ret;
vbv_retry:
- if (encode_picture(s, s->picture_number) < 0)
+ ret = encode_picture(s, s->picture_number);
+ if (growing_buffer) {
+ av_assert0(s->pb.buf == avctx->internal->byte_buffer);
+ pkt->data = s->pb.buf;
+ pkt->size = avctx->internal->byte_buffer_size;
+ }
+ if (ret < 0)
return -1;
+ #if FF_API_STAT_BITS
+ FF_DISABLE_DEPRECATION_WARNINGS
avctx->header_bits = s->header_bits;
avctx->mv_bits = s->mv_bits;
avctx->misc_bits = s->misc_bits;
s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
avctx->error[i] += s->current_picture_ptr->encoding_error[i];
}
+ ff_side_data_set_encoder_stats(pkt, s->current_picture.f->quality,
+ s->current_picture_ptr->encoding_error,
+ (s->avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
+ s->pict_type);
if (s->avctx->flags & AV_CODEC_FLAG_PASS1)
- assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
- avctx->i_tex_bits + avctx->p_tex_bits ==
- put_bits_count(&s->pb));
+ assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
+ s->misc_bits + s->i_tex_bits +
+ s->p_tex_bits);
flush_put_bits(&s->pb);
s->frame_bits = put_bits_count(&s->pb);