-
Notifications
You must be signed in to change notification settings - Fork 361
math: auditory: guard mod_psy_get_mel_filterbank() against zero divisors #10916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
singalsu
wants to merge
1
commit into
thesofproject:main
Choose a base branch
from
singalsu:auditory_updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+30
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,10 +12,13 @@ | |
| #include <sof/math/fft.h> | ||
| #include <sof/math/log.h> | ||
| #include <sof/math/numbers.h> | ||
| #include <sof/trace/trace.h> | ||
| #include <ipc/topology.h> | ||
| #include <errno.h> | ||
| #include <stdint.h> | ||
|
|
||
| LOG_MODULE_REGISTER(math_auditory, CONFIG_SOF_LOG_LEVEL); | ||
|
|
||
| #define ONE_Q16 Q_CONVERT_FLOAT(1, 16) | ||
| #define ONE_Q20 Q_CONVERT_FLOAT(1, 20) | ||
| #define ONE_Q30 Q_CONVERT_FLOAT(1, 30) | ||
|
|
@@ -117,6 +120,16 @@ int mod_psy_get_mel_filterbank(struct processing_module *mod, struct psy_mel_fil | |
| if (!fb->scratch_data1 || !fb->scratch_data2) | ||
| return -ENOMEM; | ||
|
|
||
| /* fb->mel_bins is used both as the loop bound and as part of the | ||
| * (mel_bins + 1) divisor below. Reject non-positive values up front to | ||
| * avoid a divide by zero (mel_bins == -1) and to keep mel_step | ||
| * meaningful. | ||
| */ | ||
| if (fb->mel_bins <= 0 || fb->mel_bins > AUDITORY_MAX_MEL_BANDS) { | ||
| comp_cl_err(mod->dev, "Invalid mel_bins %d", fb->mel_bins); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| /* Log power can be log, or log10 or dB, get multiply coef to convert | ||
| * log to desired format. | ||
| */ | ||
|
|
@@ -149,6 +162,17 @@ int mod_psy_get_mel_filterbank(struct processing_module *mod, struct psy_mel_fil | |
| mel_start = psy_hz_to_mel(fb->start_freq); | ||
| mel_end = psy_hz_to_mel(fb->end_freq); | ||
| mel_step = (mel_end - mel_start) / (fb->mel_bins + 1); | ||
| /* delta_cl / delta_rc below are both equal to mel_step; guard against | ||
| * a non-positive step (start_freq >= end_freq, or so many bins that | ||
| * the integer division truncates to zero) before using them as | ||
| * divisors. | ||
| */ | ||
|
singalsu marked this conversation as resolved.
|
||
| if (mel_step <= 0) { | ||
| comp_cl_err(mod->dev, "Invalid mel_step %d (start_freq=%d end_freq=%d mel_bins=%d)", | ||
| mel_step, fb->start_freq, fb->end_freq, fb->mel_bins); | ||
| return -EINVAL; | ||
| } | ||
|
singalsu marked this conversation as resolved.
|
||
|
|
||
| for (i = 0; i < fb->mel_bins; i++) { | ||
| left_mel = mel_start + i * mel_step; | ||
| center_mel = mel_start + (i + 1) * mel_step; | ||
|
Comment on lines
176
to
178
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't do, not relevant. |
||
|
|
@@ -160,6 +184,11 @@ int mod_psy_get_mel_filterbank(struct processing_module *mod, struct psy_mel_fil | |
| if (fb->slaney_normalize) { | ||
| left_hz = psy_mel_to_hz(left_mel); | ||
| right_hz = psy_mel_to_hz(right_mel); | ||
| if (right_hz <= left_hz) { | ||
| comp_cl_err(mod->dev, "Invalid Hz range left=%d right=%d at mel bin %d", | ||
| left_hz, right_hz, i); | ||
| return -EINVAL; | ||
| } | ||
|
singalsu marked this conversation as resolved.
|
||
| scale = Q_SHIFT_RND(TWO_Q29 / (right_hz - left_hz), 29, 16); /* Q16.16*/ | ||
| if (i == 0) { | ||
| scale_inv = Q_SHIFT_LEFT(ONE_Q30 / scale, 14, 16); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.