From 4417183bae95ba18198bf68f4e0f7f4391c57598 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 26 May 2025 19:00:36 +0200 Subject: [PATCH] riff-media: fix MS and DVI ADPCM av_bps calculations Align the calculations for the number of samples per block with the calculations in adpcmdec. For MS ADPCM we have in adpcmdec: samples = (blocksize - 7 * dec->channels) * 2 + 2 * dec->channels; outsize = 2 * samples; outbuf = gst_buffer_new_and_alloc (outsize); This gives us the total output byte size in 16 bits samples. To get back to the samples, dividing by the channels and 2, we get the right samples per block as: int spb = ((strf->blockalign / strf->channels) - 7) * 2 + 2; Which we can then use to calculate the bitrate in riff-media. A similar calculation for DVI ADPCM is needed to get the right bitrate in all cases. Tested with the sample in https://bugzilla.gnome.org/show_bug.cgi?id=636245 and another (failing before this patch) sample. Part-of: --- .../gst-plugins-base/gst-libs/gst/riff/riff-media.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/subprojects/gst-plugins-base/gst-libs/gst/riff/riff-media.c b/subprojects/gst-plugins-base/gst-libs/gst/riff/riff-media.c index c349dcf70d..a0cee7d5dc 100644 --- a/subprojects/gst-plugins-base/gst-libs/gst/riff/riff-media.c +++ b/subprojects/gst-plugins-base/gst-libs/gst/riff/riff-media.c @@ -1342,8 +1342,9 @@ gst_riff_create_audio_caps (guint16 codec_id, * so either we calculate the bitrate or mark it as invalid as this * would probably confuse timing */ strf->av_bps = 0; - if (strf->channels != 0 && strf->rate != 0 && strf->blockalign != 0) { - int spb = ((strf->blockalign - strf->channels * 7) / 2) * 2; + if (strf->channels != 0 && strf->rate != 0 && strf->blockalign != 0 && + (strf->blockalign / strf->channels) >= 7) { + int spb = ((strf->blockalign / strf->channels) - 7) * 2 + 2; strf->av_bps = gst_util_uint64_scale_int (strf->rate, strf->blockalign, spb); GST_DEBUG ("fixing av_bps to calculated value %d of MS ADPCM", @@ -1464,8 +1465,9 @@ gst_riff_create_audio_caps (guint16 codec_id, * header, so either we calculate the bitrate or mark it as invalid * as this would probably confuse timing */ strf->av_bps = 0; - if (strf->channels != 0 && strf->rate != 0 && strf->blockalign != 0) { - int spb = ((strf->blockalign - strf->channels * 4) / 2) * 2; + if (strf->channels != 0 && strf->rate != 0 && strf->blockalign != 0 && + (strf->blockalign / strf->channels) >= 4) { + int spb = ((strf->blockalign / strf->channels) - 4) * 2 + 1; strf->av_bps = gst_util_uint64_scale_int (strf->rate, strf->blockalign, spb); GST_DEBUG ("fixing av_bps to calculated value %d of IMA DVI ADPCM",