From nobody Tue Feb 10 18:54:51 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1769538964; cv=none; d=zohomail.com; s=zohoarc; b=gD7G3cyhJ68VItN7n88zNjduPjTMrQfgk8H3SqnnK00GA/vTuUAid7qlopJJDVXCIPPyyj1dl08j6Pv8DvFG8GuRAVMwzFvjCJowqSTF+DXRyNfIZrUpbbuwIa67X+WaWWuOFueG+S8K+bQryCXmyWLXAY9SjTu6pFMIYLMP5m8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1769538964; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Nao6GR4PA+D5lNYLhp5P8F75FAU05Pi5/wDI5gnX4p8=; b=NjPQ54880xzLSQn/Dj+GOU0xREKQxcDee+fzOJb0adoVdk8eKeZz2BkqcwdOylIqzXp2iXwoT0a8eZWi1K9efvBf2pWtJroDv+hYZvlOgGoSTJvvoH5/wXjaZV5yFGU8kowhJI2EX5TKlU5Ld4NAKTs6omFme1uSrffXwz6Nlec= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1769538964640842.2841170566288; Tue, 27 Jan 2026 10:36:04 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vknq2-0006Mk-Kk; Tue, 27 Jan 2026 13:30:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vknnq-0001mV-OG for qemu-devel@nongnu.org; Tue, 27 Jan 2026 13:28:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vknnl-0003Qq-Bt for qemu-devel@nongnu.org; Tue, 27 Jan 2026 13:28:36 -0500 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-613-D_ikycIsPRuMpRdGJHRJhQ-1; Tue, 27 Jan 2026 13:28:27 -0500 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3A79218002F2; Tue, 27 Jan 2026 18:28:26 +0000 (UTC) Received: from localhost (unknown [10.45.242.23]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9C82218001D5; Tue, 27 Jan 2026 18:28:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769538511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Nao6GR4PA+D5lNYLhp5P8F75FAU05Pi5/wDI5gnX4p8=; b=bpgv6fbMIij+5TrZSAeqA5ctx6TmPX4od5wyPs3hvq5zgAud2QlVBxiqsgtdSMfKTqs9WU fmmlKDFjXPFk1t9fDxxIMJbrOZBzKfDurHsWfpGhl+yYFqMzGZEDINvplpEpJpO3GDwSik hp5OiIYs+RcFZhWTyTPF8s3gzzjpFAA= X-MC-Unique: D_ikycIsPRuMpRdGJHRJhQ-1 X-Mimecast-MFC-AGG-ID: D_ikycIsPRuMpRdGJHRJhQ_1769538506 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Christian Schoenebeck , Akihiko Odaki Subject: [PATCH 31/43] audio/mixeng: replace redundant pcm_info fields with AudioFormat Date: Tue, 27 Jan 2026 22:24:58 +0400 Message-ID: <20260127182516.289834-32-marcandre.lureau@redhat.com> In-Reply-To: <20260127182516.289834-1-marcandre.lureau@redhat.com> References: <20260127182516.289834-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1769538966650154100 From: Marc-Andr=C3=A9 Lureau The audio_pcm_info structure stored three fields (bits, is_signed, is_float) that were always derived from the AudioFormat enum. This redundancy meant the same information was represented twice, with no type-level guarantee that they stayed in sync. Replace these fields with a single AudioFormat field, and add helper functions to extract the derived properties when needed: - audio_format_bits() - audio_format_is_signed() - audio_format_is_float() This improves type safety by making AudioFormat the single source of truth, eliminating the possibility of inconsistent state between the format enum and its derived boolean/integer representations. Signed-off-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Akihiko Odaki --- audio/audio_int.h | 4 +- audio/audio_template.h | 12 +-- include/qemu/audio.h | 49 +++++++++ audio/audio-mixeng-be.c | 218 ++++++++++++---------------------------- audio/dbusaudio.c | 12 +-- audio/coreaudio.m | 2 +- 6 files changed, 126 insertions(+), 171 deletions(-) diff --git a/audio/audio_int.h b/audio/audio_int.h index 5334c4baad2..dd5f2220d75 100644 --- a/audio/audio_int.h +++ b/audio/audio_int.h @@ -45,9 +45,7 @@ struct audio_callback { }; =20 struct audio_pcm_info { - int bits; - bool is_signed; - bool is_float; + AudioFormat af; int freq; int nchannels; int bytes_per_frame; diff --git a/audio/audio_template.h b/audio/audio_template.h index 08d60422589..3da91a4782c 100644 --- a/audio/audio_template.h +++ b/audio/audio_template.h @@ -173,7 +173,7 @@ static int glue (audio_pcm_sw_init_, TYPE) ( sw->empty =3D true; #endif =20 - if (sw->info.is_float) { + if (audio_format_is_float(hw->info.af)) { #ifdef DAC sw->conv =3D mixeng_conv_float[sw->info.nchannels =3D=3D 2] [sw->info.swap_endianness]; @@ -188,9 +188,9 @@ static int glue (audio_pcm_sw_init_, TYPE) ( sw->clip =3D mixeng_clip #endif [sw->info.nchannels =3D=3D 2] - [sw->info.is_signed] + [audio_format_is_signed(hw->info.af)] [sw->info.swap_endianness] - [audio_bits_to_index(sw->info.bits)]; + [audio_format_to_index(hw->info.af)]; } =20 sw->name =3D g_strdup (name); @@ -300,7 +300,7 @@ static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioMixen= gBackend *s, goto err1; } =20 - if (hw->info.is_float) { + if (audio_format_is_float(hw->info.af)) { #ifdef DAC hw->clip =3D mixeng_clip_float[hw->info.nchannels =3D=3D 2] [hw->info.swap_endianness]; @@ -315,9 +315,9 @@ static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioMixen= gBackend *s, hw->conv =3D mixeng_conv #endif [hw->info.nchannels =3D=3D 2] - [hw->info.is_signed] + [audio_format_is_signed(hw->info.af)] [hw->info.swap_endianness] - [audio_bits_to_index(hw->info.bits)]; + [audio_format_to_index(hw->info.af)]; } =20 glue(audio_pcm_hw_alloc_resources_, TYPE)(hw); diff --git a/include/qemu/audio.h b/include/qemu/audio.h index b6b6ee9b560..a2fbc286eb1 100644 --- a/include/qemu/audio.h +++ b/include/qemu/audio.h @@ -185,6 +185,55 @@ bool audio_be_set_dbus_server(AudioBackend *be, =20 const char *audio_application_name(void); =20 +static inline int audio_format_bits(AudioFormat fmt) +{ + switch (fmt) { + case AUDIO_FORMAT_S8: + case AUDIO_FORMAT_U8: + return 8; + + case AUDIO_FORMAT_S16: + case AUDIO_FORMAT_U16: + return 16; + + case AUDIO_FORMAT_F32: + case AUDIO_FORMAT_S32: + case AUDIO_FORMAT_U32: + return 32; + + case AUDIO_FORMAT__MAX: + break; + } + + g_assert_not_reached(); +} + +static inline bool audio_format_is_float(AudioFormat fmt) +{ + return fmt =3D=3D AUDIO_FORMAT_F32; +} + +static inline bool audio_format_is_signed(AudioFormat fmt) +{ + switch (fmt) { + case AUDIO_FORMAT_S8: + case AUDIO_FORMAT_S16: + case AUDIO_FORMAT_S32: + case AUDIO_FORMAT_F32: + return true; + + case AUDIO_FORMAT_U8: + case AUDIO_FORMAT_U16: + case AUDIO_FORMAT_U32: + return false; + + case AUDIO_FORMAT__MAX: + break; + } + + g_assert_not_reached(); +} + #define DEFINE_AUDIO_PROPERTIES(_s, _f) \ DEFINE_PROP_AUDIODEV("audiodev", _s, _f) =20 diff --git a/audio/audio-mixeng-be.c b/audio/audio-mixeng-be.c index a0e542754e5..146026d0b39 100644 --- a/audio/audio-mixeng-be.c +++ b/audio/audio-mixeng-be.c @@ -62,23 +62,28 @@ int audio_bug (const char *funcname, int cond) return cond; } =20 -static inline int audio_bits_to_index (int bits) +/* + * Convert audio format to mixeng_clip index. Used by audio_pcm_sw_init_ a= nd + * audio_mixeng_backend_add_capture() + */ +static int audio_format_to_index(AudioFormat af) { - switch (bits) { - case 8: + switch (af) { + case AUDIO_FORMAT_U8: + case AUDIO_FORMAT_S8: return 0; - - case 16: + case AUDIO_FORMAT_U16: + case AUDIO_FORMAT_S16: return 1; - - case 32: + case AUDIO_FORMAT_U32: + case AUDIO_FORMAT_S32: return 2; - - default: - audio_bug ("bits_to_index", 1); - AUD_log (NULL, "invalid bits %d\n", bits); - return 0; + case AUDIO_FORMAT_F32: + case AUDIO_FORMAT__MAX: + break; } + + g_assert_not_reached(); } =20 void AUD_vlog (const char *cap, const char *fmt, va_list ap) @@ -172,141 +177,68 @@ static int audio_validate_settings (const struct aud= settings *as) =20 static int audio_pcm_info_eq (struct audio_pcm_info *info, const struct au= dsettings *as) { - int bits =3D 8; - bool is_signed =3D false, is_float =3D false; - - switch (as->fmt) { - case AUDIO_FORMAT_S8: - is_signed =3D true; - /* fall through */ - case AUDIO_FORMAT_U8: - break; - - case AUDIO_FORMAT_S16: - is_signed =3D true; - /* fall through */ - case AUDIO_FORMAT_U16: - bits =3D 16; - break; - - case AUDIO_FORMAT_F32: - is_float =3D true; - /* fall through */ - case AUDIO_FORMAT_S32: - is_signed =3D true; - /* fall through */ - case AUDIO_FORMAT_U32: - bits =3D 32; - break; - - default: - abort(); - } - return info->freq =3D=3D as->freq + return info->af =3D=3D as->fmt + && info->freq =3D=3D as->freq && info->nchannels =3D=3D as->nchannels - && info->is_signed =3D=3D is_signed - && info->is_float =3D=3D is_float - && info->bits =3D=3D bits && info->swap_endianness =3D=3D (as->endianness !=3D HOST_BIG_ENDI= AN); } =20 void audio_pcm_init_info (struct audio_pcm_info *info, const struct audset= tings *as) { - int bits =3D 8, mul; - bool is_signed =3D false, is_float =3D false; - - switch (as->fmt) { - case AUDIO_FORMAT_S8: - is_signed =3D true; - /* fall through */ - case AUDIO_FORMAT_U8: - mul =3D 1; - break; - - case AUDIO_FORMAT_S16: - is_signed =3D true; - /* fall through */ - case AUDIO_FORMAT_U16: - bits =3D 16; - mul =3D 2; - break; - - case AUDIO_FORMAT_F32: - is_float =3D true; - /* fall through */ - case AUDIO_FORMAT_S32: - is_signed =3D true; - /* fall through */ - case AUDIO_FORMAT_U32: - bits =3D 32; - mul =3D 4; - break; - - default: - abort(); - } - + info->af =3D as->fmt; info->freq =3D as->freq; - info->bits =3D bits; - info->is_signed =3D is_signed; - info->is_float =3D is_float; info->nchannels =3D as->nchannels; - info->bytes_per_frame =3D as->nchannels * mul; + info->bytes_per_frame =3D as->nchannels * audio_format_bits(as->fmt) /= 8; info->bytes_per_second =3D info->freq * info->bytes_per_frame; info->swap_endianness =3D (as->endianness !=3D HOST_BIG_ENDIAN); } =20 -void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int= len) +void audio_pcm_info_clear_buf(struct audio_pcm_info *info, void *buf, int = len) { if (!len) { return; } =20 - if (info->is_signed || info->is_float) { - memset(buf, 0x00, len * info->bytes_per_frame); - } else { - switch (info->bits) { - case 8: - memset(buf, 0x80, len * info->bytes_per_frame); - break; - - case 16: - { - int i; - uint16_t *p =3D buf; - short s =3D INT16_MAX; - - if (info->swap_endianness) { - s =3D bswap16 (s); - } - - for (i =3D 0; i < len * info->nchannels; i++) { - p[i] =3D s; - } - } - break; + switch (info->af) { + case AUDIO_FORMAT_U8: + memset(buf, 0x80, len * info->bytes_per_frame); + break; + case AUDIO_FORMAT_U16: { + int i; + uint16_t *p =3D buf; + short s =3D INT16_MAX; =20 - case 32: - { - int i; - uint32_t *p =3D buf; - int32_t s =3D INT32_MAX; + if (info->swap_endianness) { + s =3D bswap16(s); + } =20 - if (info->swap_endianness) { - s =3D bswap32 (s); - } + for (i =3D 0; i < len * info->nchannels; i++) { + p[i] =3D s; + } + break; + } + case AUDIO_FORMAT_U32: { + int i; + uint32_t *p =3D buf; + int32_t s =3D INT32_MAX; =20 - for (i =3D 0; i < len * info->nchannels; i++) { - p[i] =3D s; - } - } - break; + if (info->swap_endianness) { + s =3D bswap32(s); + } =20 - default: - AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n", - info->bits); - break; + for (i =3D 0; i < len * info->nchannels; i++) { + p[i] =3D s; } + break; + } + case AUDIO_FORMAT_S8: + case AUDIO_FORMAT_S16: + case AUDIO_FORMAT_S32: + case AUDIO_FORMAT_F32: + memset(buf, 0x00, len * info->bytes_per_frame); + break; + case AUDIO_FORMAT__MAX: + g_assert_not_reached(); } } =20 @@ -719,8 +651,8 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *= buf, size_t buf_len) #ifdef DEBUG_AUDIO static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *= info) { - dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n", - cap, info->bits, info->is_signed, info->is_float, info->freq, + dolog("%s: %s, freq %d, nchan %d\n", + cap, AudioFormat_str(info->af), info->freq, info->nchannels); } #endif @@ -1759,15 +1691,15 @@ static CaptureVoiceOut *audio_mixeng_backend_add_ca= pture( =20 cap->buf =3D g_malloc0_n(hw->mix_buf.size, hw->info.bytes_per_fram= e); =20 - if (hw->info.is_float) { + if (audio_format_is_float(hw->info.af)) { hw->clip =3D mixeng_clip_float[hw->info.nchannels =3D=3D 2] [hw->info.swap_endianness]; } else { hw->clip =3D mixeng_clip [hw->info.nchannels =3D=3D 2] - [hw->info.is_signed] + [audio_format_is_signed(hw->info.af)] [hw->info.swap_endianness] - [audio_bits_to_index(hw->info.bits)]; + [audio_format_to_index(hw->info.af)]; } =20 QLIST_INSERT_HEAD (&s->cap_head, cap, entries); @@ -1869,29 +1801,6 @@ audsettings audiodev_to_audsettings(AudiodevPerDirec= tionOptions *pdo) }; } =20 -int audioformat_bytes_per_sample(AudioFormat fmt) -{ - switch (fmt) { - case AUDIO_FORMAT_U8: - case AUDIO_FORMAT_S8: - return 1; - - case AUDIO_FORMAT_U16: - case AUDIO_FORMAT_S16: - return 2; - - case AUDIO_FORMAT_U32: - case AUDIO_FORMAT_S32: - case AUDIO_FORMAT_F32: - return 4; - - case AUDIO_FORMAT__MAX: - ; - } - abort(); -} - - /* frames =3D freq * usec / 1e6 */ int audio_buffer_frames(AudiodevPerDirectionOptions *pdo, audsettings *as, int def_usecs) @@ -1914,8 +1823,7 @@ int audio_buffer_samples(AudiodevPerDirectionOptions = *pdo, int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo, audsettings *as, int def_usecs) { - return audio_buffer_samples(pdo, as, def_usecs) * - audioformat_bytes_per_sample(as->fmt); + return audio_buffer_samples(pdo, as, def_usecs) * audio_format_bits(as= ->fmt) / 8; } =20 void audio_rate_start(RateCtl *rate) diff --git a/audio/dbusaudio.c b/audio/dbusaudio.c index e284542b2dd..72d6194033b 100644 --- a/audio/dbusaudio.c +++ b/audio/dbusaudio.c @@ -147,9 +147,9 @@ dbus_init_out_listener(QemuDBusDisplay1AudioOutListener= *listener, qemu_dbus_display1_audio_out_listener_call_init( listener, (uintptr_t)hw, - hw->info.bits, - hw->info.is_signed, - hw->info.is_float, + audio_format_bits(hw->info.af), + audio_format_is_signed(hw->info.af), + audio_format_is_float(hw->info.af), hw->info.freq, hw->info.nchannels, hw->info.bytes_per_frame, @@ -273,9 +273,9 @@ dbus_init_in_listener(QemuDBusDisplay1AudioInListener *= listener, HWVoiceIn *hw) qemu_dbus_display1_audio_in_listener_call_init( listener, (uintptr_t)hw, - hw->info.bits, - hw->info.is_signed, - hw->info.is_float, + audio_format_bits(hw->info.af), + audio_format_is_signed(hw->info.af), + audio_format_is_float(hw->info.af), hw->info.freq, hw->info.nchannels, hw->info.bytes_per_frame, diff --git a/audio/coreaudio.m b/audio/coreaudio.m index 40d7986b1d7..08bab353831 100644 --- a/audio/coreaudio.m +++ b/audio/coreaudio.m @@ -359,7 +359,7 @@ static OSStatus init_out_device(coreaudioVoiceOut *core) AudioValueRange frameRange; =20 AudioStreamBasicDescription streamBasicDescription =3D { - .mBitsPerChannel =3D core->hw.info.bits, + .mBitsPerChannel =3D audio_format_bits(core->hw.info.af), .mBytesPerFrame =3D core->hw.info.bytes_per_frame, .mBytesPerPacket =3D core->hw.info.bytes_per_frame, .mChannelsPerFrame =3D core->hw.info.nchannels, --=20 2.52.0