Skip to content

Commit 036c735

Browse files
author
Kevin Ahrendt
authored
Bugfix: Use internal memory for buffers if PSRAM allocation fails (#16)
1 parent e038c10 commit 036c735

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed

src/decode/flac_decoder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ FLACDecoderResult FLACDecoder::decode_frame(uint8_t *buffer, size_t buffer_lengt
257257
// freed in free_buffers()
258258
this->block_samples_ = (int32_t *) heap_caps_malloc(this->max_block_size_ * this->num_channels_ * sizeof(int32_t),
259259
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
260+
if (this->block_samples_ == nullptr) {
261+
this->block_samples_ =
262+
(int32_t *) heap_caps_malloc(this->max_block_size_ * this->num_channels_ * sizeof(int32_t), MALLOC_CAP_8BIT);
263+
}
260264
}
261265
if (!this->block_samples_) {
262266
return FLAC_DECODER_ERROR_MEMORY_ALLOCATION_ERROR;

src/decode/mp3_decoder.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8056,17 +8056,34 @@ MP3DecInfo *AllocateBuffers(void) {
80568056
SubbandInfo *sbi;
80578057

80588058
mp3DecInfo = (MP3DecInfo *) heap_caps_malloc(sizeof(MP3DecInfo), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
8059+
if (mp3DecInfo == nullptr) {
8060+
mp3DecInfo = (MP3DecInfo *) malloc(sizeof(MP3DecInfo));
8061+
}
80598062
if (!mp3DecInfo)
80608063
return 0;
80618064
ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo));
80628065

8063-
fh = (FrameHeader *) heap_caps_malloc(sizeof(FrameHeader), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
8064-
si = (SideInfo *) heap_caps_malloc(sizeof(SideInfo), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
8065-
sfi = (ScaleFactorInfo *) heap_caps_malloc(sizeof(ScaleFactorInfo), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
80668066
hi = (HuffmanInfo *) heap_caps_malloc(sizeof(HuffmanInfo), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
8067+
if (hi == nullptr) {
8068+
hi = (HuffmanInfo *) malloc(sizeof(HuffmanInfo));
8069+
}
80678070
di = (DequantInfo *) heap_caps_malloc(sizeof(DequantInfo), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
8071+
if (di == nullptr) {
8072+
di = (DequantInfo *) malloc(sizeof(DequantInfo));
8073+
}
80688074
mi = (IMDCTInfo *) heap_caps_malloc(sizeof(IMDCTInfo), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
8075+
if (mi == nullptr) {
8076+
mi = (IMDCTInfo *) malloc(sizeof(IMDCTInfo));
8077+
}
80698078
sbi = (SubbandInfo *) heap_caps_malloc(sizeof(SubbandInfo), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
8079+
if (sbi == nullptr) {
8080+
sbi = (SubbandInfo *) malloc(sizeof(SubbandInfo));
8081+
}
8082+
8083+
// Relatively small structures, fine to leave in internal memory
8084+
fh = (FrameHeader *) malloc(sizeof(FrameHeader));
8085+
si = (SideInfo *) malloc(sizeof(SideInfo));
8086+
sfi = (ScaleFactorInfo *) malloc(sizeof(ScaleFactorInfo));
80708087

80718088
mp3DecInfo->FrameHeaderPS = (void *) fh;
80728089
mp3DecInfo->SideInfoPS = (void *) si;

src/resample/art_resampler.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,22 @@ Resample *resampleInit(int numChannels, int numTaps, int numFilters, float lowpa
107107
cxt->filters = (float **) calloc(cxt->numFilters + 1, sizeof(float *));
108108

109109
cxt->tempFilter = (float *) heap_caps_malloc(numTaps * sizeof(float), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
110+
if (cxt->tempFilter == nullptr) {
111+
cxt->tempFilter = (float *) malloc(numTaps * sizeof(float));
112+
}
113+
114+
if (cxt->tempFilter == nullptr) {
115+
return NULL;
116+
}
110117

111118
for (i = 0; i <= cxt->numFilters; ++i) {
112119
cxt->filters[i] = (float *) heap_caps_malloc(cxt->numTaps * sizeof(float), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
120+
if (cxt->filters[i] == nullptr) {
121+
cxt->filters[i] = (float *) malloc(cxt->numTaps * sizeof(float));
122+
}
123+
if (cxt->filters[i] == nullptr) {
124+
return NULL;
125+
}
113126
memset(cxt->filters[i], 0, cxt->numTaps * sizeof(float));
114127
init_filter(cxt, cxt->filters[i], (float) i / cxt->numFilters, lowpassRatio);
115128
}
@@ -120,6 +133,12 @@ Resample *resampleInit(int numChannels, int numTaps, int numFilters, float lowpa
120133

121134
for (i = 0; i < numChannels; ++i) {
122135
cxt->buffers[i] = (float *) heap_caps_malloc(cxt->numSamples * sizeof(float), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
136+
if (cxt->buffers[i] == nullptr) {
137+
cxt->buffers[i] = (float *) malloc(cxt->numSamples * sizeof(float));
138+
}
139+
if (cxt->buffers[i] == nullptr) {
140+
return NULL;
141+
}
123142
memset(cxt->buffers[i], 0, cxt->numSamples * sizeof(float));
124143
}
125144

src/resample/resampler.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ bool Resampler::initialize(ResamplerConfiguration &config) {
2626

2727
this->float_input_buffer_ =
2828
(float *) heap_caps_malloc(this->input_buffer_samples_ * sizeof(float), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
29+
if (this->float_input_buffer_ == nullptr) {
30+
this->float_input_buffer_ = (float *) malloc(this->input_buffer_samples_ * sizeof(float));
31+
}
2932

3033
this->float_output_buffer_ =
3134
(float *) heap_caps_malloc(this->output_buffer_samples_ * sizeof(float), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
35+
if (this->float_output_buffer_ == nullptr) {
36+
this->float_output_buffer_ = (float *) malloc(this->output_buffer_samples_ * sizeof(float));
37+
}
3238

3339
if ((this->float_input_buffer_ == nullptr) || (this->float_output_buffer_ == nullptr)) {
3440
return false;
@@ -76,15 +82,20 @@ bool Resampler::initialize(ResamplerConfiguration &config) {
7682
}
7783

7884
if (this->sample_ratio_ < 1.0f) {
79-
this->resampler_ = art_resampler::resampleInit(this->channels_, this->number_of_taps_, this->number_of_filters_,
85+
this->resampler_ =
86+
art_resampler::resampleInit(this->channels_, this->number_of_taps_, this->number_of_filters_,
8087
this->sample_ratio_ * this->lowpass_ratio_, flags | INCLUDE_LOWPASS);
8188
} else if (this->lowpass_ratio_ < 1.0f) {
8289
this->resampler_ = art_resampler::resampleInit(this->channels_, this->number_of_taps_, this->number_of_filters_,
83-
this->lowpass_ratio_, flags | INCLUDE_LOWPASS);
90+
this->lowpass_ratio_, flags | INCLUDE_LOWPASS);
8491
} else {
85-
this->resampler_ = art_resampler::resampleInit(this->channels_, this->number_of_taps_, this->number_of_filters_, 1.0f, flags);
92+
this->resampler_ =
93+
art_resampler::resampleInit(this->channels_, this->number_of_taps_, this->number_of_filters_, 1.0f, flags);
8694
}
8795

96+
if (this->resampler_ == nullptr) {
97+
return false;
98+
}
8899
art_resampler::resampleAdvancePosition(this->resampler_, this->number_of_taps_ / 2.0f);
89100
}
90101

@@ -104,12 +115,12 @@ ResamplerResults Resampler::resample(const uint8_t *input_buffer, uint8_t *outpu
104115
}
105116
uint32_t conversion_time = 0;
106117
if (this->requires_resampling_) {
107-
quantization_utils::quantized_to_float(input_buffer, this->float_input_buffer_, frames_to_process * this->channels_, this->input_bits_,
108-
gain_db);
118+
quantization_utils::quantized_to_float(input_buffer, this->float_input_buffer_, frames_to_process * this->channels_,
119+
this->input_bits_, gain_db);
109120
} else {
110121
// Just converting the bits per sample
111-
quantization_utils::quantized_to_float(input_buffer, this->float_output_buffer_, frames_to_process * this->channels_, this->input_bits_,
112-
gain_db);
122+
quantization_utils::quantized_to_float(input_buffer, this->float_output_buffer_,
123+
frames_to_process * this->channels_, this->input_bits_, gain_db);
113124
}
114125

115126
size_t frames_used = frames_to_process;
@@ -119,28 +130,32 @@ ResamplerResults Resampler::resample(const uint8_t *input_buffer, uint8_t *outpu
119130
if (this->requires_resampling_) {
120131
if (this->pre_filter_) {
121132
for (int i = 0; i < this->channels_; ++i) {
122-
art_resampler::biquad_apply_buffer(&this->lowpass_[i][0], this->float_input_buffer_ + i, frames_to_process, this->channels_);
123-
art_resampler::biquad_apply_buffer(&this->lowpass_[i][1], this->float_input_buffer_ + i, frames_to_process, this->channels_);
133+
art_resampler::biquad_apply_buffer(&this->lowpass_[i][0], this->float_input_buffer_ + i, frames_to_process,
134+
this->channels_);
135+
art_resampler::biquad_apply_buffer(&this->lowpass_[i][1], this->float_input_buffer_ + i, frames_to_process,
136+
this->channels_);
124137
}
125138
}
126139

127140
art_resampler::ResampleResult res =
128141
art_resampler::resampleProcessInterleaved(this->resampler_, this->float_input_buffer_, frames_to_process,
129-
this->float_output_buffer_, output_frames_free, this->sample_ratio_);
142+
this->float_output_buffer_, output_frames_free, this->sample_ratio_);
130143

131144
frames_used = res.input_used;
132145
frames_generated = res.output_generated;
133146

134147
if (this->post_filter_) {
135148
for (int i = 0; i < this->channels_; ++i) {
136-
art_resampler::biquad_apply_buffer(&this->lowpass_[i][0], this->float_output_buffer_ + i, frames_generated, this->channels_);
137-
art_resampler::biquad_apply_buffer(&this->lowpass_[i][1], this->float_output_buffer_ + i, frames_generated, this->channels_);
149+
art_resampler::biquad_apply_buffer(&this->lowpass_[i][0], this->float_output_buffer_ + i, frames_generated,
150+
this->channels_);
151+
art_resampler::biquad_apply_buffer(&this->lowpass_[i][1], this->float_output_buffer_ + i, frames_generated,
152+
this->channels_);
138153
}
139154
}
140155
}
141156

142-
uint32_t clipped_samples = quantization_utils::float_to_quantized(this->float_output_buffer_, output_buffer,
143-
frames_generated * this->channels_, this->output_bits_);
157+
uint32_t clipped_samples = quantization_utils::float_to_quantized(
158+
this->float_output_buffer_, output_buffer, frames_generated * this->channels_, this->output_bits_);
144159

145160
ResamplerResults results = {.frames_used = frames_used,
146161
.frames_generated = frames_generated,

0 commit comments

Comments
 (0)