encoder: h264,h265,mpeg2,vp8: use gst_util_uint64_scale() for bitrate

Use gst_util_uint64_scale() to calculate bitrate instead of normal arithmetic
to avoid overflows, underflows and loss of precision.

https://bugzilla.gnome.org/show_bug.cgi?id=768458
This commit is contained in:
Víctor Manuel Jáquez Leal 2016-07-21 17:38:40 +02:00
parent 4259d1aec7
commit 68b9ab7818
4 changed files with 16 additions and 14 deletions

View file

@ -2382,10 +2382,10 @@ ensure_bitrate (GstVaapiEncoderH264 * encoder)
if (!encoder->use_dct8x8)
bits_per_mb += (bits_per_mb * 10) / 100;
guint64 factor = encoder->mb_width * encoder->mb_height * bits_per_mb;
base_encoder->bitrate =
encoder->mb_width * encoder->mb_height * bits_per_mb *
GST_VAAPI_ENCODER_FPS_N (encoder) /
GST_VAAPI_ENCODER_FPS_D (encoder) / 1000;
gst_util_uint64_scale (factor, GST_VAAPI_ENCODER_FPS_N (encoder),
GST_VAAPI_ENCODER_FPS_D (encoder)) / 1000;
GST_INFO ("target bitrate computed to %u kbps", base_encoder->bitrate);
}
break;

View file

@ -1947,10 +1947,10 @@ ensure_bitrate (GstVaapiEncoderH265 * encoder)
/* Fixme: Provide better estimation */
/* Using a 1/6 compression ratio */
/* 12 bits per pixel fro yuv420 */
guint64 factor = encoder->luma_width * encoder->luma_height * 12 / 6;
base_encoder->bitrate =
(encoder->luma_width * encoder->luma_height * 12 / 6) *
GST_VAAPI_ENCODER_FPS_N (encoder) /
GST_VAAPI_ENCODER_FPS_D (encoder) / 1000;
gst_util_uint64_scale (factor, GST_VAAPI_ENCODER_FPS_N (encoder),
GST_VAAPI_ENCODER_FPS_D (encoder)) / 1000;
GST_INFO ("target bitrate computed to %u kbps", base_encoder->bitrate);
}
break;

View file

@ -186,10 +186,11 @@ ensure_bitrate (GstVaapiEncoderMpeg2 * encoder)
switch (GST_VAAPI_ENCODER_RATE_CONTROL (encoder)) {
case GST_VAAPI_RATECONTROL_CBR:
if (!base_encoder->bitrate)
base_encoder->bitrate = GST_VAAPI_ENCODER_WIDTH (encoder) *
GST_VAAPI_ENCODER_HEIGHT (encoder) *
GST_VAAPI_ENCODER_FPS_N (encoder) /
GST_VAAPI_ENCODER_FPS_D (encoder) / 4 / 1000;
base_encoder->bitrate =
gst_util_uint64_scale (GST_VAAPI_ENCODER_WIDTH (encoder) *
GST_VAAPI_ENCODER_HEIGHT (encoder),
GST_VAAPI_ENCODER_FPS_N (encoder),
GST_VAAPI_ENCODER_FPS_D (encoder)) / 4 / 1000;
break;
default:
base_encoder->bitrate = 0;

View file

@ -125,10 +125,11 @@ ensure_bitrate (GstVaapiEncoderVP8 * encoder)
switch (GST_VAAPI_ENCODER_RATE_CONTROL (encoder)) {
case GST_VAAPI_RATECONTROL_CBR:
if (!base_encoder->bitrate) {
base_encoder->bitrate = GST_VAAPI_ENCODER_WIDTH (encoder) *
GST_VAAPI_ENCODER_HEIGHT (encoder) *
GST_VAAPI_ENCODER_FPS_N (encoder) /
GST_VAAPI_ENCODER_FPS_D (encoder) / 4 * 1000;
base_encoder->bitrate =
gst_util_uint64_scale (GST_VAAPI_ENCODER_WIDTH (encoder) *
GST_VAAPI_ENCODER_HEIGHT (encoder),
GST_VAAPI_ENCODER_FPS_N (encoder),
GST_VAAPI_ENCODER_FPS_D (encoder)) / 4 * 1000;
}
default:
base_encoder->bitrate = 0;