Skip to contentSkip to conversation

Division by zero in snd_pcm_rate_sw_params()

PATCH Francois Gouget 3 participants 4 messages Jul 24, 2004

lore
Share & tools
lore
#0 lore raw

Hi,

I am working on Wine and lately I have been tracking various sound related issues. One issue I noticed is a crash in Wine's winmm wave test when using the Alsa sound backend.

More precisely:
* if Wine's winmm wave test is the only application using the sound
device, then it works
* but if another application is already using the sound device (e.g.
xmms), then Wine's winmm wave test crashes when trying to play the 96kHz test.

The crash occurs when calling snd_pcm_sw_params():

EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR,
"unable to set sw params for playback");

See this URL for the exact source:
http://source.winehq.org/source/dlls/winmm/winealsa/audio.c#L1602

Digging some more I found out that the division by zero happens in the snd_pcm_rate_sw_params() function pcm_rate.c. Here's why:

* Wine sets xfer_align to 1:
EXIT_ON_ERROR(snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1),

                 MMSYSERR_ERROR, "unable to set xfer align");

* then it calls snd_pcm_sw_params() as shown above

* eventually we get to snd_pcm_rate_sw_params() in src/pcm/pcm_rate.c.
(http://cvs.sourceforge.net/viewcvs.py/alsa/alsa-lib/src/pcm/pcm_rate.c?rev=1.83&view=markup)

* There we call recalc():

   recalc(pcm, &sparams->xfer_align);

* when more than one application uses the soundcard, recalc() ends up
doing:

   *val = muldiv_near(*val, slave->period_size, pcm->period_size);

* however in my case slave->period_size is less than half of
pcm->period_size so I end up with xfer_align == *val == 0.

* then, a few lines later in snd_pcm_rate_sw_params() we do:

    if (sparams->start_threshold > (slave->buffer_size /
            sparams->xfer_align) * sparams->xfer_align)
        sparams->start_threshold = (slave->buffer_size /
            sparams->xfer_align) * sparams->xfer_align;

And we crash with a division by zero because now sparams->xfer_align==0. (these lines were added in CVS revision 1.83 of this file)

Simply resetting xfer_align to 1 is it is zero after the call to recalc() (see attached patch) fixes the problem for me (no crash and the test tone plays correctly). However I don't know if this is the right fix, hence the following questions:

Does my patch look ok? What is the proper procedure to get it applied? Is there further information I can provide to resolve this issue?

My configuration:
* Up to date Debian testing
* Alsa 1.0.5a
* cat /proc/asound/cards
0 [V8235 ]: VIA8233 - VIA 8235

                     VIA 8235 at 0xbc00, irq 5
Signature · 2 lines
Francois Gouget
[email protected]
pcm_rate.c renamed from pcm_rate.c.orig +2 -0
@@ -630,6 +638,8 @@
 	recalc(pcm, &sparams->avail_min);
 	rate->orig_avail_min = sparams->avail_min;
 	recalc(pcm, &sparams->xfer_align);
+	if (sparams->xfer_align==0)
+		sparams->xfer_align=1;
 	recalc(pcm, &sparams->start_threshold);
 	if (sparams->start_threshold <= slave->buffer_size) {
 		if (sparams->start_threshold > (slave->buffer_size / sparams->avail_min) * sparams->avail_min)
#2 lore raw
In reply to James Courtier-Dutton
Quoted 3 lines · James Courtier-Dutton

Once I have tested my code more fully, I will post a patch to the list. I have been posting patches for pcm_rate.c already, but although they are better than before, they still do not work in all cases.

I appreciate your work.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click

#1 lore raw
In reply to Francois Gouget
Quoted 4 lines · Francois Gouget

Hi,

I am working on Wine and lately I have been tracking various sound related issues. One issue I noticed is a crash in Wine's winmm wave test when using the Alsa sound backend.

This and other problems are caused by the many bug/problems with the pcm_rate.c plugin. I have been doing some work on the pcm_rate.c plugin, and your patch contains something one of the bug fixes I already have here fixes. I could reproduce it without needing wine.

Once I have tested my code more fully, I will post a patch to the list.
I have been posting patches for pcm_rate.c already, but although they are better than before, they still do not work in all cases.

James

-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click

#3 lore raw

James Courtier-Dutton wrote:
[...]

Quoted 1 line

This and other problems are caused by the many bug/problems with the pcm_rate.c plugin. I have been doing some work on the pcm_rate.c plugin, and your patch contains something one of the bug fixes I already have here fixes. I could reproduce it without needing wine.

I just wanted to report the problem to make sure it was known and also to check Wine is not doing something wrong. Since you're obviously aware of the problem and even working on a solution everything's perfect!

Thanks for your work.

Signature · 10 lines
Francois Gouget
[email protected]



-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click