Skip to contentSkip to conversation

audio/hda: improve windows guest audio quality.

PATCH Superseded Gerd Hoffmann 1 participant 5 messages Apr 27, 2018

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover audio/hda: improve windows guest audio quality.

Based on a patch by Martin Schrodt.

v2: drop underrun detection, handle overruns only.

Gerd Hoffmann (4):
audio/hda: create millisecond timers that handle IO audio/hda: turn some dprintfs into trace points audio/hda: tweak timer adjust logic audio/hda: detect output buffer overruns

 include/hw/compat.h   |   7 ++
 hw/audio/hda-codec.c  | 287 ++++++++++++++++++++++++++++++++++++++++++++------
 hw/audio/intel-hda.c  |   7 --
 hw/audio/trace-events |   6 ++
 4 files changed, 269 insertions(+), 38 deletions(-)
Signature · 1 line
2.9.3
#3 lore raw

PATCH v2 3/4 audio/hda: tweak timer adjust logic

We have some jitter in the audio timer call frequency and buffer sizes. So it is rather pointless trying to be very exact, effect is a constant up+down adjustment. So adjust only in case we are off too much.

---
 hw/audio/hda-codec.c  | 20 +++++++++++++-------
 hw/audio/trace-events |  1 +
 2 files changed, 14 insertions(+), 7 deletions(-)
hw/audio/hda-codec.c +13 -7
index 13d4473f18..f6fb1d0fa0 100644
@@ -129,7 +129,6 @@ static void hda_codec_parse_fmt(uint32_t format, struct audsettings *as)
 #include  "hda-codec-common.h"
 
 #define HDA_TIMER_TICKS (SCALE_MS)
-#define MAX_CORR (SCALE_US * 100)
 #define B_SIZE sizeof(st->buf)
 #define B_MASK (sizeof(st->buf) - 1)
 
@@ -196,13 +195,20 @@ static inline int64_t hda_bytes_per_second(HDAAudioStream *st)
 
 static inline void hda_timer_sync_adjust(HDAAudioStream *st, int64_t target_pos)
 {
-    int64_t corr =
-        NANOSECONDS_PER_SECOND * target_pos / hda_bytes_per_second(st);
-    if (corr > MAX_CORR) {
-        corr = MAX_CORR;
-    } else if (corr < -MAX_CORR) {
-        corr = -MAX_CORR;
+    int64_t limit = B_SIZE / 8;
+    int64_t corr = 0;
+
+    if (target_pos > limit) {
+        corr = HDA_TIMER_TICKS;
     }
+    if (target_pos < -limit) {
+        corr = -HDA_TIMER_TICKS;
+    }
+    if (corr == 0) {
+        return;
+    }
+
+    trace_hda_audio_adjust(st->node->name, target_pos);
     atomic_fetch_add(&st->buft_start, corr);
 }
 
hw/audio/trace-events +1 -0
index 03340b9359..30112d97c4 100644
@@ -21,3 +21,4 @@ milkymist_ac97_out_cb_transferred(int transferred) "transferred %d"
 # hw/audio/hda-codec.c
 hda_audio_running(const char *stream, int nr, bool running) "st %s, nr %d, run %d"
 hda_audio_format(const char *stream, int chan, const char *fmt, int freq) "st %s, %d x %s @ %d Hz"
+hda_audio_adjust(const char *stream, int pos) "st %s, pos %d"
Signature · 1 line
2.9.3
#4 lore raw

PATCH v2 4/4 audio/hda: detect output buffer overruns

In reply to Gerd Hoffmann

If some event caused some larger playback hickup the fine-grained timer adjust isn't able to recover. Use a buffer overruns as indicator for that. Reset timer adjust logic in case we detected one.

---
 hw/audio/hda-codec.c  | 9 +++++++++
 hw/audio/trace-events | 1 +
 2 files changed, 10 insertions(+)
hw/audio/hda-codec.c +9 -0
index f6fb1d0fa0..fc4945086b 100644
@@ -325,6 +325,15 @@ static void hda_audio_output_cb(void *opaque, int avail)
 
     int64_t to_transfer = audio_MIN(wpos - rpos, avail);
 
+    if (wpos - rpos == B_SIZE) {
+        /* drop buffer, reset timer adjust */
+        st->rpos = 0;
+        st->wpos = 0;
+        st->buft_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+        trace_hda_audio_overrun(st->node->name);
+        return;
+    }
+
     hda_timer_sync_adjust(st, (wpos - rpos) - to_transfer - (B_SIZE >> 1));
 
     while (to_transfer) {
hw/audio/trace-events +1 -0
index 30112d97c4..5891b4e2b9 100644
@@ -22,3 +22,4 @@ milkymist_ac97_out_cb_transferred(int transferred) "transferred %d"
 hda_audio_running(const char *stream, int nr, bool running) "st %s, nr %d, run %d"
 hda_audio_format(const char *stream, int chan, const char *fmt, int freq) "st %s, %d x %s @ %d Hz"
 hda_audio_adjust(const char *stream, int pos) "st %s, pos %d"
+hda_audio_overrun(const char *stream) "st %s"
Signature · 1 line
2.9.3
#1 lore raw

PATCH v2 1/4 audio/hda: create millisecond timers that handle IO

In reply to Gerd Hoffmann

Currently, the HDA device tries to sync itself with the QEMU audio backend by waiting for the guest driver to handle buffer completion interrupts. This causes the backend to often read too much data from the device, as well as running out of data whenever the guest takes too long to handle the interrupt.

According to the HDA specification, the guest is also not required to use interrupts, but can also sync itself by polling the LPIB registers.

This patch will introduce high frequency (1000Hz) timers that interface with the device and allow for much smoother emulation of the LPIB registers. Since the timing is now provided by these timers, the need to wait for buffer completion interrupts also ceases.

Message-id: [email protected]

[ kraxel: keep old code for compatibility with older qemu versions,
add property to switch code paths at runtime ]

---
 include/hw/compat.h  |   7 ++
 hw/audio/hda-codec.c | 263 ++++++++++++++++++++++++++++++++++++++++++++++-----
 hw/audio/intel-hda.c |   7 --
 3 files changed, 244 insertions(+), 33 deletions(-)
include/hw/compat.h +7 -0
index 13242b831a..dced6d81bf 100644
@@ -1,6 +1,13 @@
 #ifndef HW_COMPAT_H
 #define HW_COMPAT_H
 
+#define HW_COMPAT_2_12 \
+    {\
+        .driver   = "hda-audio",\
+        .property = "use-timer",\
+        .value    = "false",\
+    },
+
 #define HW_COMPAT_2_11 \
     {\
         .driver   = "hpet",\
hw/audio/hda-codec.c +237 -26

371 lines not on this page · raw message

hw/audio/intel-hda.c +0 -7
index 948268afd8..23a2cf6484 100644
@@ -407,13 +407,6 @@ static bool intel_hda_xfer(HDACodecDevice *dev, uint32_t stnr, bool output,
     if (st->bpl == NULL) {
         return false;
     }
-    if (st->ctl & (1 << 26)) {
-        /*
-         * Wait with the next DMA xfer until the guest
-         * has acked the buffer completion interrupt
-         */
-        return false;
-    }
 
     left = len;
     s = st->bentries;
Signature · 1 line
2.9.3
#2 lore raw

PATCH v2 2/4 audio/hda: turn some dprintfs into trace points

In reply to Gerd Hoffmann
---
 hw/audio/hda-codec.c  | 9 ++++-----
 hw/audio/trace-events | 4 ++++
 2 files changed, 8 insertions(+), 5 deletions(-)
hw/audio/hda-codec.c +4 -5
index 9ee2f3d55f..13d4473f18 100644
@@ -24,6 +24,7 @@
 #include "intel-hda.h"
 #include "intel-hda-defs.h"
 #include "audio/audio.h"
+#include "trace.h"
 
 /* -------------------------------------------------------------------------- */
 
@@ -394,8 +395,7 @@ static void hda_audio_set_running(HDAAudioStream *st, bool running)
         return;
     }
     st->running = running;
-    dprint(st->state, 1, "%s: %s (stream %d)\n", st->node->name,
-           st->running ? "on" : "off", st->stream);
+    trace_hda_audio_running(st->node->name, st->stream, st->running);
     if (st->state->use_timer) {
         if (running) {
             int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -449,9 +449,8 @@ static void hda_audio_setup(HDAAudioStream *st)
         return;
     }
 
-    dprint(st->state, 1, "%s: format: %d x %s @ %d Hz\n",
-           st->node->name, st->as.nchannels,
-           fmt2name[st->as.fmt], st->as.freq);
+    trace_hda_audio_format(st->node->name, st->as.nchannels,
+                           fmt2name[st->as.fmt], st->as.freq);
 
     if (st->output) {
         if (use_timer) {
hw/audio/trace-events +4 -0
index fa1646d169..03340b9359 100644
@@ -17,3 +17,7 @@ milkymist_ac97_in_cb(int avail, uint32_t remaining) "avail %d remaining %u"
 milkymist_ac97_in_cb_transferred(int transferred) "transferred %d"
 milkymist_ac97_out_cb(int free, uint32_t remaining) "free %d remaining %u"
 milkymist_ac97_out_cb_transferred(int transferred) "transferred %d"
+
+# hw/audio/hda-codec.c
+hda_audio_running(const char *stream, int nr, bool running) "st %s, nr %d, run %d"
+hda_audio_format(const char *stream, int chan, const char *fmt, int freq) "st %s, %d x %s @ %d Hz"
Signature · 1 line
2.9.3