Re: [PATCH 1/2] ALSA: pcm: rewrite snd_pcm_playback_silence()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 05. 04. 23 22:12, Oswald Buddenhagen wrote:
The auto-silencer supports two modes: "thresholded" to fill up "just
enough", and "top-up" to fill up "as much as possible". The two modes
used rather distinct code paths, which this patch unifies. The only
remaining distinction is how much we actually want to fill.

This fixes a bug in thresholded mode, where we failed to use new_hw_ptr,
resulting in under-fill.

I don't follow what you refer here. The old code uses snd_pcm_playback_hw_avail() thus new hw_ptr for the threshold mode, too.

Top-up mode is now more well-behaved and much easier to understand in
corner cases.

This also updates comments in the proximity of silencing-related data
structures.

Signed-off-by: Oswald Buddenhagen <oswald.buddenhagen@xxxxxx>
---
  .../kernel-api/writing-an-alsa-driver.rst     | 14 +--
  include/sound/pcm.h                           | 14 +--
  include/uapi/sound/asound.h                   |  9 +-
  sound/core/pcm_lib.c                          | 86 ++++++++-----------
  sound/core/pcm_local.h                        |  3 +-
  sound/core/pcm_native.c                       |  6 +-
  6 files changed, 62 insertions(+), 70 deletions(-)

diff --git a/Documentation/sound/kernel-api/writing-an-alsa-driver.rst b/Documentation/sound/kernel-api/writing-an-alsa-driver.rst
index a368529e8ed3..f2834a016473 100644
--- a/Documentation/sound/kernel-api/writing-an-alsa-driver.rst
+++ b/Documentation/sound/kernel-api/writing-an-alsa-driver.rst
@@ -1577,14 +1577,16 @@ are the contents of this file:
            unsigned int period_step;
            unsigned int sleep_min;		/* min ticks to sleep */
            snd_pcm_uframes_t start_threshold;
-          snd_pcm_uframes_t stop_threshold;
-          snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
-                                                  noise is nearest than this */
-          snd_pcm_uframes_t silence_size;	/* Silence filling size */
+          // The following two thresholds alleviate playback buffer underruns; when
+          // hw_avail drops below the threshold, the respective action is triggered:
+          snd_pcm_uframes_t stop_threshold;	/* stop playback */
+          snd_pcm_uframes_t silence_threshold;	/* pre-fill buffer with silence */
+          snd_pcm_uframes_t silence_size;		/* msx size of silence pre-fill */
            snd_pcm_uframes_t boundary;	/* pointers wrap point */
- snd_pcm_uframes_t silenced_start;
-          snd_pcm_uframes_t silenced_size;
+          // internal data of auto-silencer
+          snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
+          snd_pcm_uframes_t silence_filled; /* size filled with silence */
snd_pcm_sync_id_t sync; /* hardware synchronization ID */ diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index 27040b472a4f..f20400bb7032 100644
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -378,18 +378,18 @@ struct snd_pcm_runtime {
  	unsigned int rate_den;
  	unsigned int no_period_wakeup: 1;
- /* -- SW params -- */
-	int tstamp_mode;		/* mmap timestamp is updated */
+	/* -- SW params; see struct snd_pcm_sw_params for comments -- */
+	int tstamp_mode;
    	unsigned int period_step;
  	snd_pcm_uframes_t start_threshold;
  	snd_pcm_uframes_t stop_threshold;
-	snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
-						noise is nearest than this */
-	snd_pcm_uframes_t silence_size;	/* Silence filling size */
-	snd_pcm_uframes_t boundary;	/* pointers wrap point */
+	snd_pcm_uframes_t silence_threshold;
+	snd_pcm_uframes_t silence_size;
+	snd_pcm_uframes_t boundary;
+ // internal data of auto-silencer

I would use traditional C-style comment style here (to match other descriptions).

  	snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
-	snd_pcm_uframes_t silence_filled; /* size filled with silence */
+	snd_pcm_uframes_t silence_filled; /* already filled part of silence area */
union snd_pcm_sync_id sync; /* hardware synchronization ID */ diff --git a/include/uapi/sound/asound.h b/include/uapi/sound/asound.h
index de6810e94abe..50aa1d98873f 100644
--- a/include/uapi/sound/asound.h
+++ b/include/uapi/sound/asound.h
@@ -429,9 +429,12 @@ struct snd_pcm_sw_params {
  	snd_pcm_uframes_t avail_min;		/* min avail frames for wakeup */
  	snd_pcm_uframes_t xfer_align;		/* obsolete: xfer size need to be a multiple */
  	snd_pcm_uframes_t start_threshold;	/* min hw_avail frames for automatic start */
-	snd_pcm_uframes_t stop_threshold;	/* min avail frames for automatic stop */
-	snd_pcm_uframes_t silence_threshold;	/* min distance from noise for silence filling */
-	snd_pcm_uframes_t silence_size;		/* silence block size */
+	// The following two thresholds alleviate playback buffer underruns; when
+	// hw_avail drops below the threshold, the respective action is triggered:

Traditional C-style comments.

+	snd_pcm_uframes_t stop_threshold;	/* stop playback */
+	snd_pcm_uframes_t silence_threshold;	/* pre-fill buffer with silence */
+	snd_pcm_uframes_t silence_size;		/* max size of silence pre-fill; when >= boundary,
+						 * fill played area with silence immediately */
  	snd_pcm_uframes_t boundary;		/* pointers wrap point */
  	unsigned int proto;			/* protocol version */
  	unsigned int tstamp_type;		/* timestamp type (req. proto >= 2.0.12) */
diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
index 02fd65993e7e..b074c5b926db 100644
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -42,70 +42,58 @@ static int fill_silence_frames(struct snd_pcm_substream *substream,
   *
   * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
   */
-void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
+void snd_pcm_playback_silence(struct snd_pcm_substream *substream)
  {
  	struct snd_pcm_runtime *runtime = substream->runtime;
+	snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
+	snd_pcm_sframes_t hw_avail, added, noise_dist;
  	snd_pcm_uframes_t frames, ofs, transfer;
  	int err;
+ // 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;
+	else if ((snd_pcm_uframes_t) hw_avail >= runtime->boundary)
+		hw_avail -= runtime->boundary;

If hw_avail is above runtime->boundary then the initial condition is totaly bogus. I would use snd_BUG_ON() and direct return here.

+
+	added = appl_ptr - runtime->silence_start;
+	if (added) {
+		if (added < 0)
+			added += runtime->boundary;
+		if ((snd_pcm_uframes_t)added < runtime->silence_filled)
+			runtime->silence_filled -= added;
+		else
+			runtime->silence_filled = 0;
+		runtime->silence_start = appl_ptr;
+	}
+
+	noise_dist = hw_avail + runtime->silence_filled;
  	if (runtime->silence_size < runtime->boundary) {
-		snd_pcm_sframes_t noise_dist, n;

...

-		if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
-			return;
  		frames = runtime->silence_threshold - noise_dist;
+		if ((snd_pcm_sframes_t) frames <= 0)
+			return;

The retyping does not look good here. Could we move the if before frames assignment like:

  if (runtime->silence_threshold <= noise_dist)
    return;
  frames = runtime->silence_threshold - noise_dist;

  		if (frames > runtime->silence_size)
  			frames = runtime->silence_size;
  	} else {
-		if (new_hw_ptr == ULONG_MAX) {	/* initialization */

...

-		frames = runtime->buffer_size - runtime->silence_filled;
+		frames = runtime->buffer_size - noise_dist;
+		if ((snd_pcm_sframes_t) frames <= 0)
+			return;

Similar thing, move the check before the frames assignment.

						Jaroslav

--
Jaroslav Kysela <perex@xxxxxxxx>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.




[Index of Archives]     [ALSA User]     [Linux Audio Users]     [Pulse Audio]     [Kernel Archive]     [Asterisk PBX]     [Photo Sharing]     [Linux Sound]     [Video 4 Linux]     [Gimp]     [Yosemite News]

  Powered by Linux