Skip to content

Commit

Permalink
Fixed bug 3886 - mp3_mad broken when upsampling needed
Browse files Browse the repository at this point in the history
Ozkan Sezer

Here is the fix: dynamically allocate mp3_mad->output_buffer,
so that upsampling doesn't segfault.
  • Loading branch information
slouken committed Oct 16, 2017
1 parent 81cb076 commit c69ff58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion music_mad.c
Expand Up @@ -44,6 +44,7 @@ mad_openFileRW(SDL_RWops *src, SDL_AudioSpec *mixer, int freesrc)
mp3_mad->output_begin = 0;
mp3_mad->output_end = 0;
mp3_mad->mixer = *mixer;
mp3_mad->output_buffer = NULL;
}
return mp3_mad;
}
Expand All @@ -58,6 +59,7 @@ mad_closeFile(mad_data *mp3_mad)
if (mp3_mad->freesrc) {
SDL_RWclose(mp3_mad->src);
}
SDL_free(mp3_mad->output_buffer);
SDL_free(mp3_mad);
}

Expand Down Expand Up @@ -177,7 +179,6 @@ decode_frame(mad_data *mp3_mad) {

mad_synth_frame(&mp3_mad->synth, &mp3_mad->frame);
pcm = &mp3_mad->synth.pcm;
out = mp3_mad->output_buffer + mp3_mad->output_end;

if ((mp3_mad->status & MS_cvt_decoded) == 0) {
mp3_mad->status |= MS_cvt_decoded;
Expand All @@ -188,6 +189,15 @@ decode_frame(mad_data *mp3_mad) {
SDL_BuildAudioCVT(&mp3_mad->cvt, AUDIO_S16, pcm->channels, mp3_mad->frame.header.samplerate, mp3_mad->mixer.format, mp3_mad->mixer.channels, mp3_mad->mixer.freq);
}

if (!mp3_mad->output_buffer) {
size_t sz = MAD_OUTPUT_BUFFER_SIZE;
if (mp3_mad->cvt.len_mult > 1) {
sz *= mp3_mad->cvt.len_mult;
}
mp3_mad->output_buffer = (unsigned char *) SDL_malloc(sz);
}
out = mp3_mad->output_buffer + mp3_mad->output_end;

/* pcm->samplerate contains the sampling frequency */

nchannels = pcm->channels;
Expand Down
2 changes: 1 addition & 1 deletion music_mad.h
Expand Up @@ -55,7 +55,7 @@ typedef struct {
SDL_AudioCVT cvt;

unsigned char input_buffer[MAD_INPUT_BUFFER_SIZE + MAD_BUFFER_GUARD];
unsigned char output_buffer[MAD_OUTPUT_BUFFER_SIZE];
unsigned char *output_buffer;
} mad_data;

mad_data *mad_openFileRW(SDL_RWops *src, SDL_AudioSpec *mixer, int freesrc);
Expand Down

0 comments on commit c69ff58

Please sign in to comment.