Skip to contentSkip to conversation

ALSA: hda - Apply codec delay to wallclock.

PATCH Superseded Dylan Reid 3 participants 8 messages Apr 6, 2013 – Apr 9, 2013 Sound

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dylan Reid

author
#0 lore raw

PATCH 1/1 ALSA: hda - Apply codec delay to wallclock.

For playback add the codec-side delay to the timestamp, for capture subtract it. This brings the timestamps in line with the time that was recently added to the delay reporting.

---
 sound/pci/hda/hda_intel.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
sound/pci/hda/hda_intel.c +22 -0
index 735567e..ec8ac71 100644
@@ -1889,6 +1889,27 @@ static void azx_timecounter_init(struct snd_pcm_substream *substream,
 		tc->cycle_last = last;
 }
 
+static u64 azx_add_codec_delay_nsec(struct snd_pcm_substream *substream,
+				u64 nsec)
+{
+	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
+	int stream = substream->stream;
+	struct hda_pcm_stream *hinfo = apcm->hinfo[stream];
+	struct hda_codec *codec = apcm->codec;
+	u64 codec_nsec;
+
+	if (hinfo->ops.get_delay) {
+		codec_nsec =
+			hinfo->ops.get_delay(hinfo, codec, substream) * 1000000;
+		if (stream == SNDRV_PCM_STREAM_CAPTURE)
+			nsec = (nsec > codec_nsec) ? nsec - codec_nsec : 0;
+		else if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+			nsec += codec_nsec;
+	}
+
+	return nsec;
+}
+
 static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
 				struct timespec *ts)
 {
@@ -1897,6 +1918,7 @@ static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
 
 	nsec = timecounter_read(&azx_dev->azx_tc);
 	nsec = div_u64(nsec, 3); /* can be optimized */
+	nsec = azx_add_codec_delay_nsec(substream, nsec);
 
 	*ts = ns_to_timespec(nsec);
 
Signature · 1 line
1.8.1.3.605.g02339dd
#1 lore raw

At Fri, 5 Apr 2013 17:22:04 -0700,

Quoted 24 lines · Dylan Reid

For playback add the codec-side delay to the timestamp, for capture subtract it. This brings the timestamps in line with the time that was recently added to the delay reporting.

---
 sound/pci/hda/hda_intel.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
sound/pci/hda/hda_intel.c +6 -0
index 735567e..ec8ac71 100644
@@ -1889,6 +1889,27 @@ static void azx_timecounter_init(struct snd_pcm_substream *substream,
 		tc->cycle_last = last;
 }
 
+static u64 azx_add_codec_delay_nsec(struct snd_pcm_substream *substream,
+				u64 nsec)
+{
+	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
+	int stream = substream->stream;
+	struct hda_pcm_stream *hinfo = apcm->hinfo[stream];

sound/pci/hda/hda_intel.c static void azx_timecounter_init(struct snd_pcm_substream *substream, · line 1907

Quoted 10 lines
sound/pci/hda/hda_intel.c +10 -0
+	struct hda_codec *codec = apcm->codec;
+	u64 codec_nsec;
+
+	if (hinfo->ops.get_delay) {
+		codec_nsec =
+			hinfo->ops.get_delay(hinfo, codec, substream) * 1000000;
+		if (stream == SNDRV_PCM_STREAM_CAPTURE)
+			nsec = (nsec > codec_nsec) ? nsec - codec_nsec : 0;
+		else if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+			nsec += codec_nsec;

I think the compensation is applied in a wrong direction.

For the playback, the wallclock indicates the timestamp corresponding to the position currently being played. With the codec delay, the position is back more, i.e. the timestamp has to be subtracted.

Takashi

Quoted 19 lines
sound/pci/hda/hda_intel.c +6 -0
+	}
+
+	return nsec;
+}
+
 static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
 				struct timespec *ts)
 {
@@ -1897,6 +1918,7 @@ static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
 
 	nsec = timecounter_read(&azx_dev->azx_tc);
 	nsec = div_u64(nsec, 3); /* can be optimized */
+	nsec = azx_add_codec_delay_nsec(substream, nsec);
 
 	*ts = ns_to_timespec(nsec);
 
Signature · 1 line
1.8.1.3.605.g02339dd
author
#2 lore raw
Quoted 39 lines · Takashi Iwai

At Fri, 5 Apr 2013 17:22:04 -0700,

Quoted 24 lines · Dylan Reid

For playback add the codec-side delay to the timestamp, for capture subtract it. This brings the timestamps in line with the time that was recently added to the delay reporting.

---
 sound/pci/hda/hda_intel.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
sound/pci/hda/hda_intel.c +6 -0
index 735567e..ec8ac71 100644
@@ -1889,6 +1889,27 @@ static void azx_timecounter_init(struct snd_pcm_substream *substream,
              tc->cycle_last = last;
 }
 
+static u64 azx_add_codec_delay_nsec(struct snd_pcm_substream *substream,
+                             u64 nsec)
+{
+     struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
+     int stream = substream->stream;
+     struct hda_pcm_stream *hinfo = apcm->hinfo[stream];

sound/pci/hda/hda_intel.c static void azx_timecounter_init(struct snd_pcm_substream *substream, · line 1907

Quoted 10 lines
sound/pci/hda/hda_intel.c +10 -0
+     struct hda_codec *codec = apcm->codec;
+     u64 codec_nsec;
+
+     if (hinfo->ops.get_delay) {
+             codec_nsec =
+                     hinfo->ops.get_delay(hinfo, codec, substream) * 1000000;
+             if (stream == SNDRV_PCM_STREAM_CAPTURE)
+                     nsec = (nsec > codec_nsec) ? nsec - codec_nsec : 0;
+             else if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+                     nsec += codec_nsec;

I think the compensation is applied in a wrong direction.

Quoted 4 lines

For the playback, the wallclock indicates the timestamp corresponding to the position currently being played. With the codec delay, the position is back more, i.e. the timestamp has to be subtracted.

OK, I was thinking of the timestamp as the wall time when the next sample will be played. If it represents the time the currently rendered sample was fed into the pipeline, this makes sense, I'll change it to subtract for output. In this case, what does it mean for input? I was thinking of it as the time the sample was converted by the A-to-D.

Thanks,

-dg

Quoted 22 lines

Takashi

Quoted 19 lines
sound/pci/hda/hda_intel.c +6 -1
+     }
+
+     return nsec;
+}
+
 static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
                              struct timespec *ts)
 {
@@ -1897,6 +1918,7 @@ static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
 
      nsec = timecounter_read(&azx_dev->azx_tc);
      nsec = div_u64(nsec, 3); /* can be optimized */
+     nsec = azx_add_codec_delay_nsec(substream, nsec);
 
      *ts = ns_to_timespec(nsec);
 
--

1.8.1.3.605.g02339dd

#3 lore raw
In reply to Dylan Reid

sound/pci/hda/hda_intel.c static void azx_timecounter_init(struct snd_pcm_substream *substream, · line 1907

Quoted 8 lines
sound/pci/hda/hda_intel.c +7 -0
+	if (hinfo->ops.get_delay) {
+		codec_nsec =
+			hinfo->ops.get_delay(hinfo, codec, substream) * 1000000;
+		if (stream == SNDRV_PCM_STREAM_CAPTURE)
+			nsec = (nsec > codec_nsec) ? nsec - codec_nsec : 0;
+		else if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+			nsec += codec_nsec;

Can the .get_delay be modified to provide a better resolution than a ms? If you already convert to time, microseconds would seem like a better fit? Your codec seems to report frames (ie 20.83 us). -Pierre

#4 lore raw

At Mon, 08 Apr 2013 11:31:12 -0500,

Quoted 13 lines · Pierre-Louis Bossart

sound/pci/hda/hda_intel.c static void azx_timecounter_init(struct snd_pcm_substream *substream, · line 1907

Quoted 9 lines
sound/pci/hda/hda_intel.c +7 -0
+	if (hinfo->ops.get_delay) {
+		codec_nsec =
+			hinfo->ops.get_delay(hinfo, codec, substream) * 1000000;
+		if (stream == SNDRV_PCM_STREAM_CAPTURE)
+			nsec = (nsec > codec_nsec) ? nsec - codec_nsec : 0;
+		else if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+			nsec += codec_nsec;

Can the .get_delay be modified to provide a better resolution than a ms? If you already convert to time, microseconds would seem like a better fit? Your codec seems to report frames (ie 20.83 us).

Actually the patch must be wrong -- get_delay returns the delay in frame (sample) unit.

Takashi

author
#5 lore raw
Quoted 12 lines · Takashi Iwai

At Mon, 08 Apr 2013 11:31:12 -0500,

Quoted 10 lines · Pierre-Louis Bossart
Quoted 9 lines
sound/pci/hda/hda_intel.c +7 -0
+   if (hinfo->ops.get_delay) {
+           codec_nsec =
+                   hinfo->ops.get_delay(hinfo, codec, substream) * 1000000;
+           if (stream == SNDRV_PCM_STREAM_CAPTURE)
+                   nsec = (nsec > codec_nsec) ? nsec - codec_nsec : 0;
+           else if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+                   nsec += codec_nsec;
Quoted 6 lines
Quoted 3 lines

Can the .get_delay be modified to provide a better resolution than a ms? If you already convert to time, microseconds would seem like a better fit? Your codec seems to report frames (ie 20.83 us).

Actually the patch must be wrong -- get_delay returns the delay in frame (sample) unit.

Of course you're right. That's annoying, it will have to convert from ms to frames (in get_delay) then to ns here. Converting from frames to ns here is a better idea, but for this particular codec, all I have is ms resolution.

Pierre,
What should the capture timestamp represent? When the sample hits the A-to-D or when it is read out of the buffer?

Thanks,

-dg

Quoted 3 lines

Takashi

#6 lore raw
Quoted 5 lines

Of course you're right. That's annoying, it will have to convert from ms to frames (in get_delay) then to ns here. Converting from frames to ns here is a better idea, but for this particular codec, all I have is ms resolution.

This is odd, looks completely arbitrary...

Quoted 3 lines

Pierre,
What should the capture timestamp represent? When the sample hits the A-to-D or when it is read out of the buffer?

When the samples hit A-to-D, as close as possible to the input (or the serial link if the codec doesn't report delay) -Pierre

author
#7 lore raw
Quoted 8 lines · Pierre-Louis Bossart
Quoted 5 lines

Of course you're right. That's annoying, it will have to convert from ms to frames (in get_delay) then to ns here. Converting from frames to ns here is a better idea, but for this particular codec, all I have is ms resolution.

This is odd, looks completely arbitrary...

It does, I'll change this patch to convert from frames to ns, and check to see if the codec vendor can supply more accurate DSP latency numbers.

Quoted 9 lines
Quoted 5 lines

Pierre,
What should the capture timestamp represent? When the sample hits the A-to-D or when it is read out of the buffer?

When the samples hit A-to-D, as close as possible to the input (or the serial link if the codec doesn't report delay)

OK, the codec latency will be subtracted from both the playback and capture times.

Thanks for the clarifications!

-dg

Quoted 2 lines

-Pierre