@samitouri / QOSamiQemu / commits / 42061a1435

audio/mixeng: replace redundant pcm_info fields with AudioFormat

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é Lureau <marcandre.lureau@redhat.com> Reviewed-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp> Reviewed-by: Mark Cave-Ayland <mark.caveayland@nutanix.com>

Marc-André Lureau committed Jan 20, 2026 at 23:23 UTC 42061a14358c4a26b2104c47ba1ff2adb14bbd63
6 files changed +126 -171
audio/audio-mixeng-be.c
+63 -155
@@ -62,23 +62,28 @@ int audio_bug (const char *funcname, int cond)
62 return cond;
63 }
64
65 -static inline int audio_bits_to_index (int bits)
65 +/*
66 + * Convert audio format to mixeng_clip index. Used by audio_pcm_sw_init_ and
67 + * audio_mixeng_backend_add_capture()
68 + */
69 +static int audio_format_to_index(AudioFormat af)
70 {
67 - switch (bits) {
68 - case 8:
71 + switch (af) {
72 + case AUDIO_FORMAT_U8:
73 + case AUDIO_FORMAT_S8:
74 return 0;
70 -
71 - case 16:
75 + case AUDIO_FORMAT_U16:
76 + case AUDIO_FORMAT_S16:
77 return 1;
73 -
74 - case 32:
78 + case AUDIO_FORMAT_U32:
79 + case AUDIO_FORMAT_S32:
80 return 2;
76 -
77 - default:
78 - audio_bug ("bits_to_index", 1);
79 - AUD_log (NULL, "invalid bits %d\n", bits);
80 - return 0;
81 + case AUDIO_FORMAT_F32:
82 + case AUDIO_FORMAT__MAX:
83 + break;
84 }
85 +
86 + g_assert_not_reached();
87 }
88
89 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
@@ -172,141 +177,68 @@ static int audio_validate_settings (const struct audsettings *as)
177
178 static int audio_pcm_info_eq (struct audio_pcm_info *info, const struct audsettings *as)
179 {
175 - int bits = 8;
176 - bool is_signed = false, is_float = false;
177 -
178 - switch (as->fmt) {
179 - case AUDIO_FORMAT_S8:
180 - is_signed = true;
181 - /* fall through */
182 - case AUDIO_FORMAT_U8:
183 - break;
184 -
185 - case AUDIO_FORMAT_S16:
186 - is_signed = true;
187 - /* fall through */
188 - case AUDIO_FORMAT_U16:
189 - bits = 16;
190 - break;
191 -
192 - case AUDIO_FORMAT_F32:
193 - is_float = true;
194 - /* fall through */
195 - case AUDIO_FORMAT_S32:
196 - is_signed = true;
197 - /* fall through */
198 - case AUDIO_FORMAT_U32:
199 - bits = 32;
200 - break;
201 -
202 - default:
203 - abort();
204 - }
205 - return info->freq == as->freq
180 + return info->af == as->fmt
181 + && info->freq == as->freq
182 && info->nchannels == as->nchannels
207 - && info->is_signed == is_signed
208 - && info->is_float == is_float
209 - && info->bits == bits
183 && info->swap_endianness == (as->endianness != HOST_BIG_ENDIAN);
184 }
185
186 void audio_pcm_init_info (struct audio_pcm_info *info, const struct audsettings *as)
187 {
215 - int bits = 8, mul;
216 - bool is_signed = false, is_float = false;
217 -
218 - switch (as->fmt) {
219 - case AUDIO_FORMAT_S8:
220 - is_signed = true;
221 - /* fall through */
222 - case AUDIO_FORMAT_U8:
223 - mul = 1;
224 - break;
225 -
226 - case AUDIO_FORMAT_S16:
227 - is_signed = true;
228 - /* fall through */
229 - case AUDIO_FORMAT_U16:
230 - bits = 16;
231 - mul = 2;
232 - break;
233 -
234 - case AUDIO_FORMAT_F32:
235 - is_float = true;
236 - /* fall through */
237 - case AUDIO_FORMAT_S32:
238 - is_signed = true;
239 - /* fall through */
240 - case AUDIO_FORMAT_U32:
241 - bits = 32;
242 - mul = 4;
243 - break;
244 -
245 - default:
246 - abort();
247 - }
248 -
188 + info->af = as->fmt;
189 info->freq = as->freq;
250 - info->bits = bits;
251 - info->is_signed = is_signed;
252 - info->is_float = is_float;
190 info->nchannels = as->nchannels;
254 - info->bytes_per_frame = as->nchannels * mul;
191 + info->bytes_per_frame = as->nchannels * audio_format_bits(as->fmt) / 8;
192 info->bytes_per_second = info->freq * info->bytes_per_frame;
193 info->swap_endianness = (as->endianness != HOST_BIG_ENDIAN);
194 }
195
259 -void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
196 +void audio_pcm_info_clear_buf(struct audio_pcm_info *info, void *buf, int len)
197 {
198 if (!len) {
199 return;
200 }
201
265 - if (info->is_signed || info->is_float) {
266 - memset(buf, 0x00, len * info->bytes_per_frame);
267 - } else {
268 - switch (info->bits) {
269 - case 8:
270 - memset(buf, 0x80, len * info->bytes_per_frame);
271 - break;
272 -
273 - case 16:
274 - {
275 - int i;
276 - uint16_t *p = buf;
277 - short s = INT16_MAX;
278 -
279 - if (info->swap_endianness) {
280 - s = bswap16 (s);
281 - }
282 -
283 - for (i = 0; i < len * info->nchannels; i++) {
284 - p[i] = s;
285 - }
286 - }
287 - break;
202 + switch (info->af) {
203 + case AUDIO_FORMAT_U8:
204 + memset(buf, 0x80, len * info->bytes_per_frame);
205 + break;
206 + case AUDIO_FORMAT_U16: {
207 + int i;
208 + uint16_t *p = buf;
209 + short s = INT16_MAX;
210
289 - case 32:
290 - {
291 - int i;
292 - uint32_t *p = buf;
293 - int32_t s = INT32_MAX;
211 + if (info->swap_endianness) {
212 + s = bswap16(s);
213 + }
214
295 - if (info->swap_endianness) {
296 - s = bswap32 (s);
297 - }
215 + for (i = 0; i < len * info->nchannels; i++) {
216 + p[i] = s;
217 + }
218 + break;
219 + }
220 + case AUDIO_FORMAT_U32: {
221 + int i;
222 + uint32_t *p = buf;
223 + int32_t s = INT32_MAX;
224
299 - for (i = 0; i < len * info->nchannels; i++) {
300 - p[i] = s;
301 - }
302 - }
303 - break;
225 + if (info->swap_endianness) {
226 + s = bswap32(s);
227 + }
228
305 - default:
306 - AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
307 - info->bits);
308 - break;
229 + for (i = 0; i < len * info->nchannels; i++) {
230 + p[i] = s;
231 }
232 + break;
233 + }
234 + case AUDIO_FORMAT_S8:
235 + case AUDIO_FORMAT_S16:
236 + case AUDIO_FORMAT_S32:
237 + case AUDIO_FORMAT_F32:
238 + memset(buf, 0x00, len * info->bytes_per_frame);
239 + break;
240 + case AUDIO_FORMAT__MAX:
241 + g_assert_not_reached();
242 }
243 }
244
@@ -719,8 +651,8 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t buf_len)
651 #ifdef DEBUG_AUDIO
652 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
653 {
722 - dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
723 - cap, info->bits, info->is_signed, info->is_float, info->freq,
654 + dolog("%s: %s, freq %d, nchan %d\n",
655 + cap, AudioFormat_str(info->af), info->freq,
656 info->nchannels);
657 }
658 #endif
@@ -1759,15 +1691,15 @@ static CaptureVoiceOut *audio_mixeng_backend_add_capture(
1691
1692 cap->buf = g_malloc0_n(hw->mix_buf.size, hw->info.bytes_per_frame);
1693
1762 - if (hw->info.is_float) {
1694 + if (audio_format_is_float(hw->info.af)) {
1695 hw->clip = mixeng_clip_float[hw->info.nchannels == 2]
1696 [hw->info.swap_endianness];
1697 } else {
1698 hw->clip = mixeng_clip
1699 [hw->info.nchannels == 2]
1768 - [hw->info.is_signed]
1700 + [audio_format_is_signed(hw->info.af)]
1701 [hw->info.swap_endianness]
1770 - [audio_bits_to_index(hw->info.bits)];
1702 + [audio_format_to_index(hw->info.af)];
1703 }
1704
1705 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
@@ -1869,29 +1801,6 @@ audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
1801 };
1802 }
1803
1872 -int audioformat_bytes_per_sample(AudioFormat fmt)
1873 -{
1874 - switch (fmt) {
1875 - case AUDIO_FORMAT_U8:
1876 - case AUDIO_FORMAT_S8:
1877 - return 1;
1878 -
1879 - case AUDIO_FORMAT_U16:
1880 - case AUDIO_FORMAT_S16:
1881 - return 2;
1882 -
1883 - case AUDIO_FORMAT_U32:
1884 - case AUDIO_FORMAT_S32:
1885 - case AUDIO_FORMAT_F32:
1886 - return 4;
1887 -
1888 - case AUDIO_FORMAT__MAX:
1889 - ;
1890 - }
1891 - abort();
1892 -}
1893 -
1894 -
1804 /* frames = freq * usec / 1e6 */
1805 int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
1806 audsettings *as, int def_usecs)
@@ -1914,8 +1823,7 @@ int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
1823 int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
1824 audsettings *as, int def_usecs)
1825 {
1917 - return audio_buffer_samples(pdo, as, def_usecs) *
1918 - audioformat_bytes_per_sample(as->fmt);
1826 + return audio_buffer_samples(pdo, as, def_usecs) * audio_format_bits(as->fmt) / 8;
1827 }
1828
1829 void audio_rate_start(RateCtl *rate)
audio/audio_int.h
+1 -3
@@ -45,9 +45,7 @@ struct audio_callback {
45 };
46
47 struct audio_pcm_info {
48 - int bits;
49 - bool is_signed;
50 - bool is_float;
48 + AudioFormat af;
49 int freq;
50 int nchannels;
51 int bytes_per_frame;
audio/audio_template.h
+6 -6
@@ -173,7 +173,7 @@ static int glue (audio_pcm_sw_init_, TYPE) (
173 sw->empty = true;
174 #endif
175
176 - if (sw->info.is_float) {
176 + if (audio_format_is_float(hw->info.af)) {
177 #ifdef DAC
178 sw->conv = mixeng_conv_float[sw->info.nchannels == 2]
179 [sw->info.swap_endianness];
@@ -188,9 +188,9 @@ static int glue (audio_pcm_sw_init_, TYPE) (
188 sw->clip = mixeng_clip
189 #endif
190 [sw->info.nchannels == 2]
191 - [sw->info.is_signed]
191 + [audio_format_is_signed(hw->info.af)]
192 [sw->info.swap_endianness]
193 - [audio_bits_to_index(sw->info.bits)];
193 + [audio_format_to_index(hw->info.af)];
194 }
195
196 sw->name = g_strdup (name);
@@ -300,7 +300,7 @@ static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioMixengBackend *s,
300 goto err1;
301 }
302
303 - if (hw->info.is_float) {
303 + if (audio_format_is_float(hw->info.af)) {
304 #ifdef DAC
305 hw->clip = mixeng_clip_float[hw->info.nchannels == 2]
306 [hw->info.swap_endianness];
@@ -315,9 +315,9 @@ static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioMixengBackend *s,
315 hw->conv = mixeng_conv
316 #endif
317 [hw->info.nchannels == 2]
318 - [hw->info.is_signed]
318 + [audio_format_is_signed(hw->info.af)]
319 [hw->info.swap_endianness]
320 - [audio_bits_to_index(hw->info.bits)];
320 + [audio_format_to_index(hw->info.af)];
321 }
322
323 glue(audio_pcm_hw_alloc_resources_, TYPE)(hw);
audio/coreaudio.m
+1 -1
@@ -359,7 +359,7 @@ static OSStatus init_out_device(coreaudioVoiceOut *core)
359 AudioValueRange frameRange;
360
361 AudioStreamBasicDescription streamBasicDescription = {
362 - .mBitsPerChannel = core->hw.info.bits,
362 + .mBitsPerChannel = audio_format_bits(core->hw.info.af),
363 .mBytesPerFrame = core->hw.info.bytes_per_frame,
364 .mBytesPerPacket = core->hw.info.bytes_per_frame,
365 .mChannelsPerFrame = core->hw.info.nchannels,
audio/dbusaudio.c
+6 -6
@@ -147,9 +147,9 @@ dbus_init_out_listener(QemuDBusDisplay1AudioOutListener *listener,
147 qemu_dbus_display1_audio_out_listener_call_init(
148 listener,
149 (uintptr_t)hw,
150 - hw->info.bits,
151 - hw->info.is_signed,
152 - hw->info.is_float,
150 + audio_format_bits(hw->info.af),
151 + audio_format_is_signed(hw->info.af),
152 + audio_format_is_float(hw->info.af),
153 hw->info.freq,
154 hw->info.nchannels,
155 hw->info.bytes_per_frame,
@@ -273,9 +273,9 @@ dbus_init_in_listener(QemuDBusDisplay1AudioInListener *listener, HWVoiceIn *hw)
273 qemu_dbus_display1_audio_in_listener_call_init(
274 listener,
275 (uintptr_t)hw,
276 - hw->info.bits,
277 - hw->info.is_signed,
278 - hw->info.is_float,
276 + audio_format_bits(hw->info.af),
277 + audio_format_is_signed(hw->info.af),
278 + audio_format_is_float(hw->info.af),
279 hw->info.freq,
280 hw->info.nchannels,
281 hw->info.bytes_per_frame,
include/qemu/audio.h
+49
@@ -185,6 +185,55 @@ bool audio_be_set_dbus_server(AudioBackend *be,
185
186 const char *audio_application_name(void);
187
188 +static inline int audio_format_bits(AudioFormat fmt)
189 +{
190 + switch (fmt) {
191 + case AUDIO_FORMAT_S8:
192 + case AUDIO_FORMAT_U8:
193 + return 8;
194 +
195 + case AUDIO_FORMAT_S16:
196 + case AUDIO_FORMAT_U16:
197 + return 16;
198 +
199 + case AUDIO_FORMAT_F32:
200 + case AUDIO_FORMAT_S32:
201 + case AUDIO_FORMAT_U32:
202 + return 32;
203 +
204 + case AUDIO_FORMAT__MAX:
205 + break;
206 + }
207 +
208 + g_assert_not_reached();
209 +}
210 +
211 +static inline bool audio_format_is_float(AudioFormat fmt)
212 +{
213 + return fmt == AUDIO_FORMAT_F32;
214 +}
215 +
216 +static inline bool audio_format_is_signed(AudioFormat fmt)
217 +{
218 + switch (fmt) {
219 + case AUDIO_FORMAT_S8:
220 + case AUDIO_FORMAT_S16:
221 + case AUDIO_FORMAT_S32:
222 + case AUDIO_FORMAT_F32:
223 + return true;
224 +
225 + case AUDIO_FORMAT_U8:
226 + case AUDIO_FORMAT_U16:
227 + case AUDIO_FORMAT_U32:
228 + return false;
229 +
230 + case AUDIO_FORMAT__MAX:
231 + break;
232 + }
233 +
234 + g_assert_not_reached();
235 +}
236 +
237 #define DEFINE_AUDIO_PROPERTIES(_s, _f) \
238 DEFINE_PROP_AUDIODEV("audiodev", _s, _f)
239