Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

This reverts commit 9f656705c5faa18afb26d922cfc64f9fd103c38d.

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: Jeff Chua <jeff.chua.linux@gmail.com>
Link: https://lore.kernel.org/r/CAAJw_ZsbTVd3Es373x_wTNDF7RknGhCD0r+NKUSwAO7HpLAkYA@mail.gmail.com
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
Signed-off-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Link: https://lore.kernel.org/r/20230505155244.2312199-1-oswald.buddenhagen@gmx.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>

authored by

Jaroslav Kysela and committed by
Takashi Iwai
d7f5dd97 56fc217f

+59 -40
+54 -36
sound/core/pcm_lib.c
··· 42 42 * 43 43 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately 44 44 */ 45 - void snd_pcm_playback_silence(struct snd_pcm_substream *substream) 45 + void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr) 46 46 { 47 47 struct snd_pcm_runtime *runtime = substream->runtime; 48 - snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr); 49 - snd_pcm_sframes_t added, hw_avail, frames; 50 - snd_pcm_uframes_t noise_dist, ofs, transfer; 48 + snd_pcm_uframes_t frames, ofs, transfer; 51 49 int err; 52 50 53 - added = appl_ptr - runtime->silence_start; 54 - if (added) { 55 - if (added < 0) 56 - added += runtime->boundary; 57 - if (added < runtime->silence_filled) 58 - runtime->silence_filled -= added; 59 - else 60 - runtime->silence_filled = 0; 61 - runtime->silence_start = appl_ptr; 62 - } 63 - 64 - // This will "legitimately" turn negative on underrun, and will be mangled 65 - // into a huge number by the boundary crossing handling. The initial state 66 - // might also be not quite sane. The code below MUST account for these cases. 67 - hw_avail = appl_ptr - runtime->status->hw_ptr; 68 - if (hw_avail < 0) 69 - hw_avail += runtime->boundary; 70 - 71 - noise_dist = hw_avail + runtime->silence_filled; 72 51 if (runtime->silence_size < runtime->boundary) { 73 - frames = runtime->silence_threshold - noise_dist; 74 - if (frames <= 0) 52 + snd_pcm_sframes_t noise_dist, n; 53 + snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr); 54 + if (runtime->silence_start != appl_ptr) { 55 + n = appl_ptr - runtime->silence_start; 56 + if (n < 0) 57 + n += runtime->boundary; 58 + if ((snd_pcm_uframes_t)n < runtime->silence_filled) 59 + runtime->silence_filled -= n; 60 + else 61 + runtime->silence_filled = 0; 62 + runtime->silence_start = appl_ptr; 63 + } 64 + if (runtime->silence_filled >= runtime->buffer_size) 75 65 return; 66 + noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; 67 + if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) 68 + return; 69 + frames = runtime->silence_threshold - noise_dist; 76 70 if (frames > runtime->silence_size) 77 71 frames = runtime->silence_size; 78 72 } else { 79 - frames = runtime->buffer_size - noise_dist; 80 - if (frames <= 0) 81 - return; 73 + /* 74 + * This filling mode aims at free-running mode (used for example by dmix), 75 + * which doesn't update the application pointer. 76 + */ 77 + if (new_hw_ptr == ULONG_MAX) { /* initialization */ 78 + snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); 79 + if (avail > runtime->buffer_size) 80 + avail = runtime->buffer_size; 81 + runtime->silence_filled = avail > 0 ? avail : 0; 82 + runtime->silence_start = (runtime->status->hw_ptr + 83 + runtime->silence_filled) % 84 + runtime->boundary; 85 + } else { 86 + ofs = runtime->status->hw_ptr; 87 + frames = new_hw_ptr - ofs; 88 + if ((snd_pcm_sframes_t)frames < 0) 89 + frames += runtime->boundary; 90 + runtime->silence_filled -= frames; 91 + if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { 92 + runtime->silence_filled = 0; 93 + runtime->silence_start = new_hw_ptr; 94 + } else { 95 + runtime->silence_start = ofs; 96 + } 97 + } 98 + frames = runtime->buffer_size - runtime->silence_filled; 82 99 } 83 - 84 100 if (snd_BUG_ON(frames > runtime->buffer_size)) 85 101 return; 86 - ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size; 87 - do { 102 + if (frames == 0) 103 + return; 104 + ofs = runtime->silence_start % runtime->buffer_size; 105 + while (frames > 0) { 88 106 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; 89 107 err = fill_silence_frames(substream, ofs, transfer); 90 108 snd_BUG_ON(err < 0); 91 109 runtime->silence_filled += transfer; 92 110 frames -= transfer; 93 111 ofs = 0; 94 - } while (frames > 0); 112 + } 95 113 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 96 114 } 97 115 ··· 443 425 return 0; 444 426 } 445 427 428 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 429 + runtime->silence_size > 0) 430 + snd_pcm_playback_silence(substream, new_hw_ptr); 431 + 446 432 if (in_interrupt) { 447 433 delta = new_hw_ptr - runtime->hw_ptr_interrupt; 448 434 if (delta < 0) ··· 463 441 snd_BUG_ON(crossed_boundary != 1); 464 442 runtime->hw_ptr_wrap += runtime->boundary; 465 443 } 466 - 467 - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 468 - runtime->silence_size > 0) 469 - snd_pcm_playback_silence(substream); 470 444 471 445 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); 472 446
+2 -1
sound/core/pcm_local.h
··· 29 29 struct snd_pcm_runtime *runtime); 30 30 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream); 31 31 32 - void snd_pcm_playback_silence(struct snd_pcm_substream *substream); 32 + void snd_pcm_playback_silence(struct snd_pcm_substream *substream, 33 + snd_pcm_uframes_t new_hw_ptr); 33 34 34 35 static inline snd_pcm_uframes_t 35 36 snd_pcm_avail(struct snd_pcm_substream *substream)
+3 -3
sound/core/pcm_native.c
··· 958 958 if (snd_pcm_running(substream)) { 959 959 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 960 960 runtime->silence_size > 0) 961 - snd_pcm_playback_silence(substream); 961 + snd_pcm_playback_silence(substream, ULONG_MAX); 962 962 err = snd_pcm_update_state(substream, runtime); 963 963 } 964 964 snd_pcm_stream_unlock_irq(substream); ··· 1455 1455 __snd_pcm_set_state(runtime, state); 1456 1456 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1457 1457 runtime->silence_size > 0) 1458 - snd_pcm_playback_silence(substream); 1458 + snd_pcm_playback_silence(substream, ULONG_MAX); 1459 1459 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART); 1460 1460 } 1461 1461 ··· 1916 1916 runtime->control->appl_ptr = runtime->status->hw_ptr; 1917 1917 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1918 1918 runtime->silence_size > 0) 1919 - snd_pcm_playback_silence(substream); 1919 + snd_pcm_playback_silence(substream, ULONG_MAX); 1920 1920 snd_pcm_stream_unlock_irq(substream); 1921 1921 } 1922 1922