Skip to content

Commit

Permalink
fixed bug #4410: respect original mp3 file offset.
Browse files Browse the repository at this point in the history
  • Loading branch information
sezero committed Jan 28, 2021
1 parent 2b59e81 commit 1e03adf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
16 changes: 15 additions & 1 deletion src/codecs/mp3utils.c
Expand Up @@ -20,6 +20,7 @@
*/

#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "SDL_rwops.h"

#include "mp3utils.h"
Expand All @@ -28,6 +29,19 @@

/*********************** SDL_RW WITH BOOKKEEPING ************************/

int MP3_RWinit(struct mp3file_t *fil, SDL_RWops *src) {
/* Don't use SDL_RWsize() here -- see SDL bug #5509 */
fil->src = src;
fil->start = SDL_RWtell(src);
fil->length = SDL_RWseek(src, 0, RW_SEEK_END) - fil->start;
fil->pos = 0;
if (fil->start < 0 || fil->length < 0) {
return SDL_Error(SDL_EFSEEK);
}
SDL_RWseek(src, fil->start, RW_SEEK_SET);
return 0;
}

size_t MP3_RWread(struct mp3file_t *fil, void *ptr, size_t size, size_t maxnum) {
size_t remaining = (size_t)(fil->length - fil->pos);
size_t ret;
Expand All @@ -54,7 +68,7 @@ Sint64 MP3_RWseek(struct mp3file_t *fil, Sint64 offset, int whence) {
ret = SDL_RWseek(fil->src, fil->start + offset, RW_SEEK_SET);
if (ret < 0) return ret;
fil->pos = offset;
return (fil->pos - fil->start);
return offset;
}


Expand Down
3 changes: 2 additions & 1 deletion src/codecs/mp3utils.h
Expand Up @@ -27,7 +27,8 @@ struct mp3file_t {
Sint64 start, length, pos;
};

extern int mp3_skiptags(struct mp3file_t *fil, SDL_bool keep_id3v2);
extern int mp3_skiptags(struct mp3file_t *fil, SDL_bool keep_id3v2);
extern int MP3_RWinit(struct mp3file_t *fil, SDL_RWops *src);
extern size_t MP3_RWread(struct mp3file_t *fil, void *ptr, size_t size, size_t maxnum);
extern Sint64 MP3_RWseek(struct mp3file_t *fil, Sint64 offset, int whence);

Expand Down
6 changes: 4 additions & 2 deletions src/codecs/music_mad.c
Expand Up @@ -165,10 +165,12 @@ static void *MAD_CreateFromRW(SDL_RWops *src, int freesrc)
SDL_OutOfMemory();
return NULL;
}
music->mp3file.src = src;
music->volume = MIX_MAX_VOLUME;

music->mp3file.length = SDL_RWsize(src);
if (MP3_RWinit(&music->mp3file, src) < 0) {
SDL_free(music);
return NULL;
}
if (mp3_skiptags(&music->mp3file, SDL_FALSE) < 0) {
SDL_free(music);
Mix_SetError("music_mad: corrupt mp3 file (bad tags.)");
Expand Down
11 changes: 7 additions & 4 deletions src/codecs/music_mpg123.c
Expand Up @@ -226,12 +226,15 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)

music = (MPG123_Music*)SDL_calloc(1, sizeof(*music));
if (!music) {
SDL_OutOfMemory();
return NULL;
}
music->mp3file.src = src;
music->volume = MIX_MAX_VOLUME;

music->mp3file.length = SDL_RWsize(src);
if (MP3_RWinit(&music->mp3file, src) < 0) {
SDL_free(music);
return NULL;
}
if (mp3_skiptags(&music->mp3file, SDL_TRUE) < 0) {
SDL_free(music);
Mix_SetError("music_mpg123: corrupt mp3 file (bad tags.)");
Expand Down Expand Up @@ -298,8 +301,8 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
return NULL;
}
#ifdef DEBUG_MPG123
printf("MPG123 format: %s, channels: %d, rate: %ld\n",
mpg123_format_str(encoding), channels, rate);
printf("MPG123 format: %s, channels: %d, rate: %ld\n",
mpg123_format_str(encoding), channels, rate);
#endif

format = mpg123_format_to_sdl(encoding);
Expand Down

0 comments on commit 1e03adf

Please sign in to comment.