29
29
#include "music_mpg123.h"
30
30
#include "mp3utils.h"
31
31
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 */
32
36
#include <stdio.h> /* For SEEK_SET */
33
37
#ifdef MPG123_HEADER
34
38
#include MPG123_HEADER
@@ -54,19 +58,27 @@ typedef struct {
54
58
int (* mpg123_getformat )( mpg123_handle * mh , long * rate , int * channels , int * encoding );
55
59
int (* mpg123_init )(void );
56
60
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 );
58
62
const char * (* mpg123_plain_strerror )(int errcode );
59
63
void (* mpg123_rates )(const long * * list , size_t * number );
60
64
#if (MPG123_API_VERSION >= 45 ) /* api (but not abi) change as of mpg123-1.26.0 */
61
65
int (* mpg123_read )(mpg123_handle * mh , void * outmemory , size_t outmemsize , size_t * done );
62
66
#else
63
67
int (* mpg123_read )(mpg123_handle * mh , unsigned char * outmemory , size_t outmemsize , size_t * done );
64
68
#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 );
65
77
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 * ) );
66
78
off_t (* mpg123_seek )( mpg123_handle * mh , off_t sampleoff , int whence );
67
79
off_t (* mpg123_tell )( mpg123_handle * mh );
68
80
off_t (* mpg123_length )(mpg123_handle * mh );
69
- const char * ( * mpg123_strerror )( mpg123_handle * mh );
81
+ #endif
70
82
} mpg123_loader ;
71
83
72
84
static mpg123_loader mpg123 ;
@@ -102,19 +114,27 @@ static int MPG123_Load(void)
102
114
FUNCTION_LOADER (mpg123_getformat , int (* )( mpg123_handle * mh , long * rate , int * channels , int * encoding ))
103
115
FUNCTION_LOADER (mpg123_init , int (* )(void ))
104
116
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 ))
106
118
FUNCTION_LOADER (mpg123_plain_strerror , const char * (* )(int errcode ))
107
119
FUNCTION_LOADER (mpg123_rates , void (* )(const long * * list , size_t * number ))
108
120
#if (MPG123_API_VERSION >= 45 ) /* api (but not abi) change as of mpg123-1.26.0 */
109
121
FUNCTION_LOADER (mpg123_read , int (* )(mpg123_handle * mh , void * outmemory , size_t outmemsize , size_t * done ))
110
122
#else
111
123
FUNCTION_LOADER (mpg123_read , int (* )(mpg123_handle * mh , unsigned char * outmemory , size_t outmemsize , size_t * done ))
112
124
#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 ))
113
133
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 * ) ))
114
134
FUNCTION_LOADER (mpg123_seek , off_t (* )( mpg123_handle * mh , off_t sampleoff , int whence ))
115
135
FUNCTION_LOADER (mpg123_tell , off_t (* )( mpg123_handle * mh ))
116
136
FUNCTION_LOADER (mpg123_length , off_t (* )(mpg123_handle * mh ))
117
- FUNCTION_LOADER ( mpg123_strerror , const char * ( * )( mpg123_handle * mh ))
137
+ #endif
118
138
}
119
139
++ mpg123 .loaded ;
120
140
@@ -147,7 +167,7 @@ typedef struct
147
167
unsigned char * buffer ;
148
168
size_t buffer_size ;
149
169
long sample_rate ;
150
- off_t total_length ;
170
+ Sint64 total_length ;
151
171
Mix_MusicMetaTags tags ;
152
172
} MPG123_Music ;
153
173
@@ -200,6 +220,18 @@ static char const* mpg_err(mpg123_handle* mpg, int result)
200
220
}
201
221
202
222
/* 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
203
235
static MIX_SSIZE_T rwops_read (void * p , void * dst , size_t n )
204
236
{
205
237
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)
209
241
{
210
242
return (off_t )MP3_RWseek ((struct mp3file_t * )p , (Sint64 )offset , whence );
211
243
}
244
+ #endif
212
245
213
246
static void rwops_cleanup (void * p )
214
247
{
@@ -268,10 +301,17 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
268
301
return NULL ;
269
302
}
270
303
304
+ #if (MPG123_API_VERSION >= 49 )
305
+ result = mpg123 .mpg123_reader64 (
306
+ music -> handle ,
307
+ rwops_read , rwops_seek , rwops_cleanup
308
+ );
309
+ #else
271
310
result = mpg123 .mpg123_replace_reader_handle (
272
311
music -> handle ,
273
312
rwops_read , rwops_seek , rwops_cleanup
274
313
);
314
+ #endif
275
315
if (result != MPG123_OK ) {
276
316
Mix_SetError ("mpg123_replace_reader_handle: %s" , mpg_err (music -> handle , result ));
277
317
MPG123_Delete (music );
@@ -298,7 +338,11 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
298
338
mpg123 .mpg123_format (music -> handle , rates [i ], channels , formats );
299
339
}
300
340
341
+ #if (MPG123_API_VERSION >= 49 )
342
+ result = mpg123 .mpg123_open_handle64 (music -> handle , & music -> mp3file );
343
+ #else
301
344
result = mpg123 .mpg123_open_handle (music -> handle , & music -> mp3file );
345
+ #endif
302
346
if (result != MPG123_OK ) {
303
347
Mix_SetError ("mpg123_open_handle: %s" , mpg_err (music -> handle , result ));
304
348
MPG123_Delete (music );
@@ -327,7 +371,11 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
327
371
return NULL ;
328
372
}
329
373
374
+ #if (MPG123_API_VERSION >= 49 )
375
+ music -> total_length = mpg123 .mpg123_length64 (music -> handle );
376
+ #else
330
377
music -> total_length = mpg123 .mpg123_length (music -> handle );
378
+ #endif
331
379
332
380
music -> freesrc = freesrc ;
333
381
return music ;
@@ -447,9 +495,15 @@ static int MPG123_GetAudio(void *context, void *data, int bytes)
447
495
static int MPG123_Seek (void * context , double secs )
448
496
{
449
497
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
450
502
off_t offset = (off_t )(music -> sample_rate * secs );
503
+ offset = mpg123 .mpg123_seek (music -> handle , offset , SEEK_SET );
504
+ #endif
451
505
452
- if (( offset = mpg123 . mpg123_seek ( music -> handle , offset , SEEK_SET )) < 0 ) {
506
+ if (offset < 0 ) {
453
507
return Mix_SetError ("mpg123_seek: %s" , mpg_err (music -> handle , (int )- offset ));
454
508
}
455
509
return 0 ;
@@ -458,11 +512,16 @@ static int MPG123_Seek(void *context, double secs)
458
512
static double MPG123_Tell (void * context )
459
513
{
460
514
MPG123_Music * music = (MPG123_Music * )context ;
461
- off_t offset = 0 ;
515
+ Sint64 offset ;
462
516
if (!music -> sample_rate ) {
463
517
return 0.0 ;
464
518
}
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 ) {
466
525
return Mix_SetError ("mpg123_tell: %s" , mpg_err (music -> handle , (int )- offset ));
467
526
}
468
527
return (double )offset / music -> sample_rate ;
0 commit comments