2424#include "snd_codeci.h"
2525#include "snd_mp3.h"
2626#include <errno.h>
27+ #include <limits.h>
2728
2829#define MPG123_DEF_SSIZE_T /* we do define ssize_t in our stdinc.h */
2930#include <mpg123.h>
@@ -40,6 +41,25 @@ typedef struct _mp3_priv_t
4041} mp3_priv_t ;
4142
4243/* CALLBACKS: libmpg123 expects POSIX read/lseek() behavior! */
44+ #if (MPG123_API_VERSION >= 49 )
45+ static int mp3_read (void * f , void * buf , size_t size , size_t * bytes )
46+ {
47+ * bytes = FS_fread (buf , 1 , size , (fshandle_t * )f );
48+ if (* bytes == 0 && errno != 0 )
49+ return -1 ;
50+ return 0 ;
51+ }
52+ static int64_t mp3_seek (void * f , int64_t offset , int whence )
53+ {
54+ #if (LONG_MAX <= 2147483647L )
55+ if (offset > LONG_MAX ) return -1 ;
56+ #endif
57+ if (f == NULL ) return -1 ;
58+ if (FS_fseek ((fshandle_t * )f , (long ) offset , whence ) < 0 )
59+ return -1 ;
60+ return (int64_t ) FS_ftell ((fshandle_t * )f );
61+ }
62+ #else
4363static ssize_t mp3_read (void * f , void * buf , size_t size )
4464{
4565 ssize_t ret = (ssize_t ) FS_fread (buf , 1 , size , (fshandle_t * )f );
@@ -49,11 +69,19 @@ static ssize_t mp3_read (void *f, void *buf, size_t size)
4969}
5070static off_t mp3_seek (void * f , off_t offset , int whence )
5171{
72+ #if (LONG_MAX <= 2147483647L )
73+ if (offset > LONG_MAX ) return -1 ;
74+ #endif
5275 if (f == NULL ) return -1 ;
5376 if (FS_fseek ((fshandle_t * )f , (long ) offset , whence ) < 0 )
5477 return (off_t )- 1 ;
5578 return (off_t ) FS_ftell ((fshandle_t * )f );
5679}
80+ #endif
81+ static void mp3_close (void * f )
82+ {
83+ /* we close elsewhere. */
84+ }
5785
5886static qboolean S_MP3_CodecInitialize (void )
5987{
@@ -99,12 +127,21 @@ static qboolean S_MP3_CodecOpenStream (snd_stream_t *stream)
99127 goto _fail ;
100128 }
101129
102- if (mpg123_replace_reader_handle (priv -> handle , mp3_read , mp3_seek , NULL ) != MPG123_OK ||
130+ #if (MPG123_API_VERSION >= 49 )
131+ if (mpg123_reader64 (priv -> handle , mp3_read , mp3_seek , mp3_close ) != MPG123_OK ||
132+ mpg123_open_handle64 (priv -> handle , & stream -> fh ) != MPG123_OK )
133+ {
134+ Con_Printf ("Unable to open mpg123 handle\n" );
135+ goto _fail ;
136+ }
137+ #else
138+ if (mpg123_replace_reader_handle (priv -> handle , mp3_read , mp3_seek , mp3_close ) != MPG123_OK ||
103139 mpg123_open_handle (priv -> handle , & stream -> fh ) != MPG123_OK )
104140 {
105141 Con_Printf ("Unable to open mpg123 handle\n" );
106142 goto _fail ;
107143 }
144+ #endif
108145 priv -> handle_open = 1 ;
109146
110147 if (mpg123_getformat (priv -> handle , & rate , & channels , & encoding ) != MPG123_OK )
@@ -199,9 +236,13 @@ static void S_MP3_CodecCloseStream (snd_stream_t *stream)
199236static int S_MP3_CodecRewindStream (snd_stream_t * stream )
200237{
201238 mp3_priv_t * priv = (mp3_priv_t * ) stream -> priv ;
239+ #if (MPG123_API_VERSION >= 49 )
240+ int64_t res = mpg123_seek64 (priv -> handle , 0 , SEEK_SET );
241+ #else
202242 off_t res = mpg123_seek (priv -> handle , 0 , SEEK_SET );
243+ #endif
203244 if (res >= 0 ) return 0 ;
204- return res ;
245+ return ( int ) res ;
205246}
206247
207248snd_codec_t mp3_codec =
0 commit comments