Skip to content

Commit 5f1e1b3

Browse files
committed
mpg123: support for MPG123_API_VERSION 49, i.e. mpg123 >= 1.33.0
We now use the 64-bit-only api as of mpg123 >= 1.33.0 which makes us free of sizeof off_t annoyances. It is an xor, i.e. we do not try old api even in dynamic loading case if we're building agaist mpg123 >= 1.33.0, i.e. MPG123_API_VERSION >= 49. Fixes #699 Closes #698. (cherry picked from commit 05099c5) (cherry picked from commit 50f885d)
1 parent 371c723 commit 5f1e1b3

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

src/codecs/music_mpg123.c

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include "music_mpg123.h"
3030
#include "mp3utils.h"
3131

32+
/* Not defining MPG123_PORTABLE_API here, because mpg123 v1.32.x,
33+
* i.e. MPG123_API_VERSION 48, doesn't have mpg123_open_handle64()
34+
*/
35+
/* #define MPG123_PORTABLE_API */
3236
#include <stdio.h> /* For SEEK_SET */
3337
#ifdef MPG123_HEADER
3438
#include MPG123_HEADER
@@ -54,19 +58,27 @@ typedef struct {
5458
int (*mpg123_getformat)( mpg123_handle *mh, long *rate, int *channels, int *encoding );
5559
int (*mpg123_init)(void);
5660
mpg123_handle *(*mpg123_new)(const char* decoder, int *error);
57-
int (*mpg123_open_handle)(mpg123_handle *mh, void *iohandle);
61+
const char* (*mpg123_strerror)(mpg123_handle *mh);
5862
const char* (*mpg123_plain_strerror)(int errcode);
5963
void (*mpg123_rates)(const long **list, size_t *number);
6064
#if (MPG123_API_VERSION >= 45) /* api (but not abi) change as of mpg123-1.26.0 */
6165
int (*mpg123_read)(mpg123_handle *mh, void *outmemory, size_t outmemsize, size_t *done );
6266
#else
6367
int (*mpg123_read)(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done );
6468
#endif
69+
#if (MPG123_API_VERSION >= 49)
70+
int (*mpg123_open_handle64)(mpg123_handle *mh, void *iohandle);
71+
int (*mpg123_reader64)(mpg123_handle *mh, int (*r_read)(void*, void*, size_t, size_t*), int64_t (*r_lseek)(void*, int64_t, int), void (*cleanup)(void*));
72+
int64_t (*mpg123_seek64)(mpg123_handle *mh, int64_t sampleoff, int whence);
73+
int64_t (*mpg123_tell64)(mpg123_handle *mh);
74+
int64_t (*mpg123_length64)(mpg123_handle *mh);
75+
#else
76+
int (*mpg123_open_handle)(mpg123_handle *mh, void *iohandle);
6577
int (*mpg123_replace_reader_handle)( mpg123_handle *mh, MIX_SSIZE_T (*r_read) (void *, void *, size_t), off_t (*r_lseek)(void *, off_t, int), void (*cleanup)(void*) );
6678
off_t (*mpg123_seek)( mpg123_handle *mh, off_t sampleoff, int whence );
6779
off_t (*mpg123_tell)( mpg123_handle *mh);
6880
off_t (*mpg123_length)(mpg123_handle *mh);
69-
const char* (*mpg123_strerror)(mpg123_handle *mh);
81+
#endif
7082
} mpg123_loader;
7183

7284
static mpg123_loader mpg123;
@@ -102,19 +114,27 @@ static int MPG123_Load(void)
102114
FUNCTION_LOADER(mpg123_getformat, int (*)( mpg123_handle *mh, long *rate, int *channels, int *encoding ))
103115
FUNCTION_LOADER(mpg123_init, int (*)(void))
104116
FUNCTION_LOADER(mpg123_new, mpg123_handle *(*)(const char* decoder, int *error))
105-
FUNCTION_LOADER(mpg123_open_handle, int (*)(mpg123_handle *mh, void *iohandle))
117+
FUNCTION_LOADER(mpg123_strerror, const char* (*)(mpg123_handle *mh))
106118
FUNCTION_LOADER(mpg123_plain_strerror, const char* (*)(int errcode))
107119
FUNCTION_LOADER(mpg123_rates, void (*)(const long **list, size_t *number))
108120
#if (MPG123_API_VERSION >= 45) /* api (but not abi) change as of mpg123-1.26.0 */
109121
FUNCTION_LOADER(mpg123_read, int (*)(mpg123_handle *mh, void *outmemory, size_t outmemsize, size_t *done ))
110122
#else
111123
FUNCTION_LOADER(mpg123_read, int (*)(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done ))
112124
#endif
125+
#if (MPG123_API_VERSION >= 49)
126+
FUNCTION_LOADER(mpg123_open_handle64, int (*)(mpg123_handle *mh, void *iohandle))
127+
FUNCTION_LOADER(mpg123_reader64, int (*)(mpg123_handle *mh, int (*r_read)(void*, void*, size_t, size_t*), int64_t (*r_lseek)(void*, int64_t, int), void (*cleanup)(void*)))
128+
FUNCTION_LOADER(mpg123_seek64, int64_t (*)(mpg123_handle *mh, int64_t sampleoff, int whence))
129+
FUNCTION_LOADER(mpg123_tell64, int64_t (*)(mpg123_handle *mh))
130+
FUNCTION_LOADER(mpg123_length64, int64_t (*)(mpg123_handle *mh))
131+
#else
132+
FUNCTION_LOADER(mpg123_open_handle, int (*)(mpg123_handle *mh, void *iohandle))
113133
FUNCTION_LOADER(mpg123_replace_reader_handle, int (*)( mpg123_handle *mh, MIX_SSIZE_T (*r_read) (void *, void *, size_t), off_t (*r_lseek)(void *, off_t, int), void (*cleanup)(void*) ))
114134
FUNCTION_LOADER(mpg123_seek, off_t (*)( mpg123_handle *mh, off_t sampleoff, int whence ))
115135
FUNCTION_LOADER(mpg123_tell, off_t (*)( mpg123_handle *mh))
116136
FUNCTION_LOADER(mpg123_length, off_t (*)(mpg123_handle *mh))
117-
FUNCTION_LOADER(mpg123_strerror, const char* (*)(mpg123_handle *mh))
137+
#endif
118138
}
119139
++mpg123.loaded;
120140

@@ -147,7 +167,7 @@ typedef struct
147167
unsigned char *buffer;
148168
size_t buffer_size;
149169
long sample_rate;
150-
off_t total_length;
170+
Sint64 total_length;
151171
Mix_MusicMetaTags tags;
152172
} MPG123_Music;
153173

@@ -200,6 +220,18 @@ static char const* mpg_err(mpg123_handle* mpg, int result)
200220
}
201221

202222
/* we're gonna override mpg123's I/O with these wrappers for RWops */
223+
#if (MPG123_API_VERSION >= 49)
224+
static int rwops_read(void* p, void* dst, size_t n, size_t *b)
225+
{
226+
*b = MP3_RWread((struct mp3file_t *)p, dst, 1, n);
227+
return 0;
228+
}
229+
230+
static int64_t rwops_seek(void* p, int64_t offset, int whence)
231+
{
232+
return MP3_RWseek((struct mp3file_t *)p, offset, whence);
233+
}
234+
#else
203235
static MIX_SSIZE_T rwops_read(void* p, void* dst, size_t n)
204236
{
205237
return (MIX_SSIZE_T)MP3_RWread((struct mp3file_t *)p, dst, 1, n);
@@ -209,6 +241,7 @@ static off_t rwops_seek(void* p, off_t offset, int whence)
209241
{
210242
return (off_t)MP3_RWseek((struct mp3file_t *)p, (Sint64)offset, whence);
211243
}
244+
#endif
212245

213246
static void rwops_cleanup(void* p)
214247
{
@@ -268,10 +301,17 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
268301
return NULL;
269302
}
270303

304+
#if (MPG123_API_VERSION >= 49)
305+
result = mpg123.mpg123_reader64(
306+
music->handle,
307+
rwops_read, rwops_seek, rwops_cleanup
308+
);
309+
#else
271310
result = mpg123.mpg123_replace_reader_handle(
272311
music->handle,
273312
rwops_read, rwops_seek, rwops_cleanup
274313
);
314+
#endif
275315
if (result != MPG123_OK) {
276316
Mix_SetError("mpg123_replace_reader_handle: %s", mpg_err(music->handle, result));
277317
MPG123_Delete(music);
@@ -298,7 +338,11 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
298338
mpg123.mpg123_format(music->handle, rates[i], channels, formats);
299339
}
300340

341+
#if (MPG123_API_VERSION >= 49)
342+
result = mpg123.mpg123_open_handle64(music->handle, &music->mp3file);
343+
#else
301344
result = mpg123.mpg123_open_handle(music->handle, &music->mp3file);
345+
#endif
302346
if (result != MPG123_OK) {
303347
Mix_SetError("mpg123_open_handle: %s", mpg_err(music->handle, result));
304348
MPG123_Delete(music);
@@ -327,7 +371,11 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
327371
return NULL;
328372
}
329373

374+
#if (MPG123_API_VERSION >= 49)
375+
music->total_length = mpg123.mpg123_length64(music->handle);
376+
#else
330377
music->total_length = mpg123.mpg123_length(music->handle);
378+
#endif
331379

332380
music->freesrc = freesrc;
333381
return music;
@@ -447,9 +495,15 @@ static int MPG123_GetAudio(void *context, void *data, int bytes)
447495
static int MPG123_Seek(void *context, double secs)
448496
{
449497
MPG123_Music *music = (MPG123_Music *)context;
498+
#if (MPG123_API_VERSION >= 49)
499+
int64_t offset = (int64_t)(music->sample_rate * secs);
500+
offset = mpg123.mpg123_seek64(music->handle, offset, SEEK_SET);
501+
#else
450502
off_t offset = (off_t)(music->sample_rate * secs);
503+
offset = mpg123.mpg123_seek(music->handle, offset, SEEK_SET);
504+
#endif
451505

452-
if ((offset = mpg123.mpg123_seek(music->handle, offset, SEEK_SET)) < 0) {
506+
if (offset < 0) {
453507
return Mix_SetError("mpg123_seek: %s", mpg_err(music->handle, (int)-offset));
454508
}
455509
return 0;
@@ -458,11 +512,16 @@ static int MPG123_Seek(void *context, double secs)
458512
static double MPG123_Tell(void *context)
459513
{
460514
MPG123_Music *music = (MPG123_Music *)context;
461-
off_t offset = 0;
515+
Sint64 offset;
462516
if (!music->sample_rate) {
463517
return 0.0;
464518
}
465-
if ((offset = mpg123.mpg123_tell(music->handle)) < 0) {
519+
#if (MPG123_API_VERSION >= 49)
520+
offset = mpg123.mpg123_tell64(music->handle);
521+
#else
522+
offset = mpg123.mpg123_tell(music->handle);
523+
#endif
524+
if (offset < 0) {
466525
return Mix_SetError("mpg123_tell: %s", mpg_err(music->handle, (int)-offset));
467526
}
468527
return (double)offset / music->sample_rate;

0 commit comments

Comments
 (0)