level: Fix integer overflow when filling LevelMeta

The level in GstAudioLevelMeta is represented as a signed 8bit value from 0 to
127 (with 127 meaning silence). When converting from double, make sure to clip
the value, this also prevent integer overflow in the conversion. This fixes an
issue where a lower then -127db is reported and random level with near silent
streams (due to integer overflow).

Fixes #4068

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/8012>
This commit is contained in:
Nicolas Dufresne 2024-11-29 13:41:54 -05:00 committed by GStreamer Marge Bot
parent 4507f92b0d
commit 85969fdaa7

View file

@ -731,8 +731,18 @@ gst_level_transform_ip (GstBaseTransform * trans, GstBuffer * in)
if (filter->audio_level_meta) {
gdouble RMS = sqrt (CS_tot / num_int_samples);
gdouble RMSdB = 20 * log10 (RMS + EPSILON);
guint8 level;
gst_level_rtp_audio_level_meta (filter, in, -RMSdB);
/* -127db is considered silent in audio level meta, clip anything below and
* avoid possible integer overflow */
if (RMSdB < -127.0)
level = 127;
else if (RMSdB > 0.0)
level = 0;
else
level = -RMSdB;
gst_level_rtp_audio_level_meta (filter, in, level);
}
GST_OBJECT_UNLOCK (filter);