Commit d7f5dd97 authored by Jaroslav Kysela's avatar Jaroslav Kysela Committed by Takashi Iwai

ALSA: pcm: Revert "ALSA: pcm: rewrite snd_pcm_playback_silence()"

This reverts commit 9f656705.

There was a regression (in the top-up mode). Unfortunately, the patch
provided from the author of this commit is not easy to review.

Keep the updated and new comments in headers.
Also add a new comment that documents the missed API constraint which
led to the regression.
Reported-by: default avatarJeff Chua <jeff.chua.linux@gmail.com>
Link: https://lore.kernel.org/r/CAAJw_ZsbTVd3Es373x_wTNDF7RknGhCD0r+NKUSwAO7HpLAkYA@mail.gmail.comSigned-off-by: default avatarJaroslav Kysela <perex@perex.cz>
Signed-off-by: default avatarOswald Buddenhagen <oswald.buddenhagen@gmx.de>
Link: https://lore.kernel.org/r/20230505155244.2312199-1-oswald.buddenhagen@gmx.deSigned-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 56fc217f
...@@ -42,56 +42,74 @@ static int fill_silence_frames(struct snd_pcm_substream *substream, ...@@ -42,56 +42,74 @@ static int fill_silence_frames(struct snd_pcm_substream *substream,
* *
* when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
*/ */
void snd_pcm_playback_silence(struct snd_pcm_substream *substream) void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
{ {
struct snd_pcm_runtime *runtime = substream->runtime; struct snd_pcm_runtime *runtime = substream->runtime;
snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr); snd_pcm_uframes_t frames, ofs, transfer;
snd_pcm_sframes_t added, hw_avail, frames;
snd_pcm_uframes_t noise_dist, ofs, transfer;
int err; int err;
added = appl_ptr - runtime->silence_start; if (runtime->silence_size < runtime->boundary) {
if (added) { snd_pcm_sframes_t noise_dist, n;
if (added < 0) snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
added += runtime->boundary; if (runtime->silence_start != appl_ptr) {
if (added < runtime->silence_filled) n = appl_ptr - runtime->silence_start;
runtime->silence_filled -= added; if (n < 0)
n += runtime->boundary;
if ((snd_pcm_uframes_t)n < runtime->silence_filled)
runtime->silence_filled -= n;
else else
runtime->silence_filled = 0; runtime->silence_filled = 0;
runtime->silence_start = appl_ptr; runtime->silence_start = appl_ptr;
} }
if (runtime->silence_filled >= runtime->buffer_size)
// This will "legitimately" turn negative on underrun, and will be mangled
// into a huge number by the boundary crossing handling. The initial state
// might also be not quite sane. The code below MUST account for these cases.
hw_avail = appl_ptr - runtime->status->hw_ptr;
if (hw_avail < 0)
hw_avail += runtime->boundary;
noise_dist = hw_avail + runtime->silence_filled;
if (runtime->silence_size < runtime->boundary) {
frames = runtime->silence_threshold - noise_dist;
if (frames <= 0)
return; return;
noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
return;
frames = runtime->silence_threshold - noise_dist;
if (frames > runtime->silence_size) if (frames > runtime->silence_size)
frames = runtime->silence_size; frames = runtime->silence_size;
} else { } else {
frames = runtime->buffer_size - noise_dist; /*
if (frames <= 0) * This filling mode aims at free-running mode (used for example by dmix),
return; * which doesn't update the application pointer.
*/
if (new_hw_ptr == ULONG_MAX) { /* initialization */
snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
if (avail > runtime->buffer_size)
avail = runtime->buffer_size;
runtime->silence_filled = avail > 0 ? avail : 0;
runtime->silence_start = (runtime->status->hw_ptr +
runtime->silence_filled) %
runtime->boundary;
} else {
ofs = runtime->status->hw_ptr;
frames = new_hw_ptr - ofs;
if ((snd_pcm_sframes_t)frames < 0)
frames += runtime->boundary;
runtime->silence_filled -= frames;
if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
runtime->silence_filled = 0;
runtime->silence_start = new_hw_ptr;
} else {
runtime->silence_start = ofs;
}
}
frames = runtime->buffer_size - runtime->silence_filled;
} }
if (snd_BUG_ON(frames > runtime->buffer_size)) if (snd_BUG_ON(frames > runtime->buffer_size))
return; return;
ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size; if (frames == 0)
do { return;
ofs = runtime->silence_start % runtime->buffer_size;
while (frames > 0) {
transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
err = fill_silence_frames(substream, ofs, transfer); err = fill_silence_frames(substream, ofs, transfer);
snd_BUG_ON(err < 0); snd_BUG_ON(err < 0);
runtime->silence_filled += transfer; runtime->silence_filled += transfer;
frames -= transfer; frames -= transfer;
ofs = 0; ofs = 0;
} while (frames > 0); }
snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
} }
...@@ -425,6 +443,10 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, ...@@ -425,6 +443,10 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
return 0; return 0;
} }
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
runtime->silence_size > 0)
snd_pcm_playback_silence(substream, new_hw_ptr);
if (in_interrupt) { if (in_interrupt) {
delta = new_hw_ptr - runtime->hw_ptr_interrupt; delta = new_hw_ptr - runtime->hw_ptr_interrupt;
if (delta < 0) if (delta < 0)
...@@ -442,10 +464,6 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, ...@@ -442,10 +464,6 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
runtime->hw_ptr_wrap += runtime->boundary; runtime->hw_ptr_wrap += runtime->boundary;
} }
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
runtime->silence_size > 0)
snd_pcm_playback_silence(substream);
update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
return snd_pcm_update_state(substream, runtime); return snd_pcm_update_state(substream, runtime);
......
...@@ -29,7 +29,8 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream, ...@@ -29,7 +29,8 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream,
struct snd_pcm_runtime *runtime); struct snd_pcm_runtime *runtime);
int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream); int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream);
void snd_pcm_playback_silence(struct snd_pcm_substream *substream); void snd_pcm_playback_silence(struct snd_pcm_substream *substream,
snd_pcm_uframes_t new_hw_ptr);
static inline snd_pcm_uframes_t static inline snd_pcm_uframes_t
snd_pcm_avail(struct snd_pcm_substream *substream) snd_pcm_avail(struct snd_pcm_substream *substream)
......
...@@ -958,7 +958,7 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream, ...@@ -958,7 +958,7 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
if (snd_pcm_running(substream)) { if (snd_pcm_running(substream)) {
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
runtime->silence_size > 0) runtime->silence_size > 0)
snd_pcm_playback_silence(substream); snd_pcm_playback_silence(substream, ULONG_MAX);
err = snd_pcm_update_state(substream, runtime); err = snd_pcm_update_state(substream, runtime);
} }
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
...@@ -1455,7 +1455,7 @@ static void snd_pcm_post_start(struct snd_pcm_substream *substream, ...@@ -1455,7 +1455,7 @@ static void snd_pcm_post_start(struct snd_pcm_substream *substream,
__snd_pcm_set_state(runtime, state); __snd_pcm_set_state(runtime, state);
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
runtime->silence_size > 0) runtime->silence_size > 0)
snd_pcm_playback_silence(substream); snd_pcm_playback_silence(substream, ULONG_MAX);
snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART); snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
} }
...@@ -1916,7 +1916,7 @@ static void snd_pcm_post_reset(struct snd_pcm_substream *substream, ...@@ -1916,7 +1916,7 @@ static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
runtime->control->appl_ptr = runtime->status->hw_ptr; runtime->control->appl_ptr = runtime->status->hw_ptr;
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
runtime->silence_size > 0) runtime->silence_size > 0)
snd_pcm_playback_silence(substream); snd_pcm_playback_silence(substream, ULONG_MAX);
snd_pcm_stream_unlock_irq(substream); snd_pcm_stream_unlock_irq(substream);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment