From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 4AE0C49F9D for ; Sun, 17 Mar 2024 19:57:48 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 275E968D1B7; Sun, 17 Mar 2024 21:57:42 +0200 (EET) Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4EC0B68D1A8 for ; Sun, 17 Mar 2024 21:57:35 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id F217CE9BD2; Sun, 17 Mar 2024 20:57:34 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HQhJ3NXGwL_S; Sun, 17 Mar 2024 20:57:33 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 842DAE9BA0; Sun, 17 Mar 2024 20:57:33 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sun, 17 Mar 2024 20:57:29 +0100 Message-Id: <20240317195729.11755-2-cus@passwd.hu> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240317195729.11755-1-cus@passwd.hu> References: <20240317195729.11755-1-cus@passwd.hu> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] avformat/riffdec: follow the MS docs more strictly for setting wav channel layouts X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: - Only parse the defined masks in dwChannelMask, unless strict_std_compliance is less than normal. This matches with the behaviour of the wav muxer. - Ignore additional bits in dwChannelMasks as the MS documentation suggests [1] - Assume UNKNOWN channels for missing bits as the MS documentation suggests [1] [1] https://learn.microsoft.com/en-us/previous-versions/windows/hardware/design/dn653308(v=vs.85)#details-about-dwchannelmask Signed-off-by: Marton Balint --- libavformat/riffdec.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/libavformat/riffdec.c b/libavformat/riffdec.c index 6cc912133c..0c7b2c6bdb 100644 --- a/libavformat/riffdec.c +++ b/libavformat/riffdec.c @@ -58,7 +58,7 @@ enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid) * an openended structure. */ -static void parse_waveformatex(AVFormatContext *ctx, AVIOContext *pb, AVCodecParameters *par) +static int parse_waveformatex(AVFormatContext *ctx, AVIOContext *pb, AVCodecParameters *par, int channels) { ff_asf_guid subformat; int bps; @@ -69,7 +69,26 @@ static void parse_waveformatex(AVFormatContext *ctx, AVIOContext *pb, AVCodecPar par->bits_per_coded_sample = bps; mask = avio_rl32(pb); /* dwChannelMask */ - av_channel_layout_from_mask(&par->ch_layout, mask); + if (ctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) + mask &= 0x3ffff; + + /* According to MS docs, most significant bits should be ignored if mask + * has more bits than the number of channels */ + while (av_popcount64(mask) > channels) + mask &= ~(1LL << av_log2(mask)); + + /* If the mask has less bits than channels, then we assign the remaining + * channels as UNKNOWN. */ + if (mask && av_popcount64(mask) < channels) { + int ret = av_channel_layout_custom_init(&par->ch_layout, channels); + if (ret < 0) + return ret; + for (int i = 0, idx = 0; mask; i++, mask >>= 1) + if (mask & 1) + par->ch_layout.u.map[idx++].id = i; + } else { + av_channel_layout_from_mask(&par->ch_layout, mask); + } ff_get_guid(pb, &subformat); if (!memcmp(subformat + 4, @@ -88,6 +107,7 @@ static void parse_waveformatex(AVFormatContext *ctx, AVIOContext *pb, AVCodecPar "unknown subformat:"FF_PRI_GUID"\n", FF_ARG_GUID(subformat)); } + return 0; } /* "big_endian" values are needed for RIFX file format */ @@ -145,7 +165,9 @@ int ff_get_wav_header(AVFormatContext *ctx, AVIOContext *pb, size -= 18; cbSize = FFMIN(size, cbSize); if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */ - parse_waveformatex(ctx, pb, par); + ret = parse_waveformatex(ctx, pb, par, channels); + if (ret < 0) + return ret; cbSize -= 22; size -= 22; } -- 2.35.3 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".