Skip to contentSkip to conversation

ALSA: hda - Apply codec delay to wallclock.

PATCH Applied Dylan Reid 3 participants 10 messages Apr 9, 2013 – Apr 15, 2013 Sound

lore
Share & tools
lore

Dylan Reid

author
#0 lore raw

PATCH v2 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.

---
Changes since v1:
Calculate nsec from frames, subtract delay for playback as well as capture.

 sound/pci/hda/hda_intel.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
sound/pci/hda/hda_intel.c +18 -0
index 735567e..3e6f2c6 100644
@@ -1889,6 +1889,23 @@ static void azx_timecounter_init(struct snd_pcm_substream *substream,
 		tc->cycle_last = last;
 }
 
+static u64 azx_subtract_codec_delay(struct snd_pcm_substream *substream,
+				u64 nsec)
+{
+	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
+	struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
+	u64 codec_frames, codec_nsecs;
+
+	if (!hinfo->ops.get_delay)
+		return nsec;
+
+	codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
+	codec_nsecs = div_u64(codec_frames * 1000000000LL,
+			      substream->runtime->rate);
+
+	return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
+}
+
 static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
 				struct timespec *ts)
 {
@@ -1897,6 +1914,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_subtract_codec_delay(substream, nsec);
 
 	*ts = ns_to_timespec(nsec);
 
Signature · 1 line
1.8.1.3.605.g02339dd
Applied
#1 lore raw

At Mon, 8 Apr 2013 18:20:30 -0700,

Quoted 6 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.

Thanks, applied now.

Takashi

Quoted 47 lines

---
Changes since v1:
Calculate nsec from frames, subtract delay for playback as well as capture.

 sound/pci/hda/hda_intel.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
sound/pci/hda/hda_intel.c +18 -0
index 735567e..3e6f2c6 100644
@@ -1889,6 +1889,23 @@ static void azx_timecounter_init(struct snd_pcm_substream *substream,
 		tc->cycle_last = last;
 }
 
+static u64 azx_subtract_codec_delay(struct snd_pcm_substream *substream,
+				u64 nsec)
+{
+	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
+	struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
+	u64 codec_frames, codec_nsecs;
+
+	if (!hinfo->ops.get_delay)
+		return nsec;
+
+	codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
+	codec_nsecs = div_u64(codec_frames * 1000000000LL,
+			      substream->runtime->rate);
+
+	return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
+}
+
 static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
 				struct timespec *ts)
 {
@@ -1897,6 +1914,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_subtract_codec_delay(substream, nsec);
 
 	*ts = ns_to_timespec(nsec);
 
Signature · 1 line
1.8.1.3.605.g02339dd
#2 lore raw
In reply to Dylan Reid

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

Quoted 11 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.

---
Changes since v1:
Calculate nsec from frames, subtract delay for playback as well as capture.

sound/pci/hda/hda_intel.c +1 -0
+	return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;

Is this correct? I would think for capture you need to add the delay, and subtract it for playback? -Pierre

author
#3 lore raw
Quoted 13 lines · Pierre-Louis Bossart
Quoted 10 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.

---
Changes since v1:
Calculate nsec from frames, subtract delay for playback as well as capture.

Quoted 5 lines
Quoted 1 line

+ return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;

Is this correct? I would think for capture you need to add the delay, and subtract it for playback?

Should the capture timestamp represents the wall time when the sample is converted by the A-to-D? If so, would be the correct time be the codec delay nsec before what is reported here?

Quoted 2 lines

-Pierre

#4 lore raw
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

+ return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;

Is this correct? I would think for capture you need to add the delay, and subtract it for playback?

Should the capture timestamp represents the wall time when the sample is converted by the A-to-D? If so, would be the correct time be the codec delay nsec before what is reported here?

For playback audio_timestamp = wallclk - codec_delay_in_nsec for capture audio_timestamp = wallclk + codec_delay_in_nsec

#5 lore raw

At Wed, 10 Apr 2013 07:29:32 -0700,

Quoted 9 lines · Pierre-Louis Bossart
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

+ return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;

Is this correct? I would think for capture you need to add the delay, and subtract it for playback?

Quoted 6 lines
Quoted 3 lines

Should the capture timestamp represents the wall time when the sample is converted by the A-to-D? If so, would be the correct time be the codec delay nsec before what is reported here?

For playback audio_timestamp = wallclk - codec_delay_in_nsec for capture audio_timestamp = wallclk + codec_delay_in_nsec

Hmm... I'm confused, too. From a pretty generic view,

- hwptr = the samples (frames) transferred on the (memory) buffer
- hwptr + PCM delay = the point being currently captured

Which position does the wall clock correspond?

Takashi

#6 lore raw
Quoted 10 lines
Quoted 3 lines

For playback audio_timestamp = wallclk - codec_delay_in_nsec for capture audio_timestamp = wallclk + codec_delay_in_nsec

Hmm... I'm confused, too. From a pretty generic view,

- hwptr = the samples (frames) transferred on the (memory) buffer
- hwptr + PCM delay = the point being currently captured

Which position does the wall clock correspond?

you now have 3 positions:

For capture:
hwptr = sample in memory
hwptr+ delay = sample recorded on link (tracked by LPIB and wallclock) hwptr+ delay + codec_delay = sample recorded by A/D

For playback
hwptr = next sample in memory to be DMA'ed
hwptr - delay = sample pushed on link (tracked by LPIB and wallclock) hwptr - delay - codec_delay = sample played in A/D

the wallclock is only at the soc/chipset level, it doesn't know anything about the peripheral. Makes sense?
-Pierre

#7 lore raw

At Thu, 11 Apr 2013 19:17:10 -0600,

Quoted 19 lines · Pierre-Louis Bossart
Quoted 11 lines
Quoted 4 lines

For playback audio_timestamp = wallclk - codec_delay_in_nsec for capture audio_timestamp = wallclk + codec_delay_in_nsec

Hmm... I'm confused, too. From a pretty generic view,

- hwptr = the samples (frames) transferred on the (memory) buffer
- hwptr + PCM delay = the point being currently captured

Which position does the wall clock correspond?

you now have 3 positions:

For capture:
hwptr = sample in memory
hwptr+ delay = sample recorded on link (tracked by LPIB and wallclock) hwptr+ delay + codec_delay = sample recorded by A/D

Quoted 8 lines

For playback
hwptr = next sample in memory to be DMA'ed
hwptr - delay = sample pushed on link (tracked by LPIB and wallclock) hwptr - delay - codec_delay = sample played in A/D

the wallclock is only at the soc/chipset level, it doesn't know anything about the peripheral.
Makes sense?

Thanks, now it's clearer.

Takashi

#8 lore raw

At Fri, 12 Apr 2013 10:55:27 +0200,

Quoted 27 lines · Takashi Iwai

At Thu, 11 Apr 2013 19:17:10 -0600,

Quoted 24 lines · Pierre-Louis Bossart
Quoted 11 lines
Quoted 4 lines

For playback audio_timestamp = wallclk - codec_delay_in_nsec for capture audio_timestamp = wallclk + codec_delay_in_nsec

Hmm... I'm confused, too. From a pretty generic view,

- hwptr = the samples (frames) transferred on the (memory) buffer
- hwptr + PCM delay = the point being currently captured

Which position does the wall clock correspond?

you now have 3 positions:

For capture:
hwptr = sample in memory
hwptr+ delay = sample recorded on link (tracked by LPIB and wallclock) hwptr+ delay + codec_delay = sample recorded by A/D

For playback
hwptr = next sample in memory to be DMA'ed
hwptr - delay = sample pushed on link (tracked by LPIB and wallclock) hwptr - delay - codec_delay = sample played in A/D

Quoted 5 lines
Quoted 3 lines

the wallclock is only at the soc/chipset level, it doesn't know anything about the peripheral.
Makes sense?

Thanks, now it's clearer.

Dylan, could you care to submit the fix patch?

thanks,

Takashi

author
#9 lore raw
Quoted 31 lines · Takashi Iwai

At Fri, 12 Apr 2013 10:55:27 +0200,

Quoted 28 lines · Takashi Iwai

At Thu, 11 Apr 2013 19:17:10 -0600,

Quoted 25 lines · Pierre-Louis Bossart
Quoted 11 lines
Quoted 4 lines

For playback audio_timestamp = wallclk - codec_delay_in_nsec for capture audio_timestamp = wallclk + codec_delay_in_nsec

Hmm... I'm confused, too. From a pretty generic view,

- hwptr = the samples (frames) transferred on the (memory) buffer
- hwptr + PCM delay = the point being currently captured

Which position does the wall clock correspond?

you now have 3 positions:

For capture:
hwptr = sample in memory
hwptr+ delay = sample recorded on link (tracked by LPIB and wallclock) hwptr+ delay + codec_delay = sample recorded by A/D

For playback
hwptr = next sample in memory to be DMA'ed
hwptr - delay = sample pushed on link (tracked by LPIB and wallclock) hwptr - delay - codec_delay = sample played in A/D

the wallclock is only at the soc/chipset level, it doesn't know

anything

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

about the peripheral.
Makes sense?

Thanks, now it's clearer.

Dylan, could you care to submit the fix patch?

It will be the first thing I do when I get to the office tomorrow morning.

Thanks,

Dylan

Quoted 4 lines

thanks,

Takashi