music_mpg123.c: use tag handling from mp3utils to avoid frame error reports
authorOzkan Sezer
Wed, 11 Dec 2019 01:15:02 +0300
changeset 1072503ad6c93c50
parent 1071 322880726dd9
child 1073 8450c02199e4
music_mpg123.c: use tag handling from mp3utils to avoid frame error reports

thanks Vitaly Novichkov.
src/codecs/mp3utils.c
src/codecs/music_mpg123.c
     1.1 --- a/src/codecs/mp3utils.c	Wed Dec 11 00:50:02 2019 +0300
     1.2 +++ b/src/codecs/mp3utils.c	Wed Dec 11 01:15:02 2019 +0300
     1.3 @@ -24,7 +24,7 @@
     1.4  
     1.5  #include "mp3utils.h"
     1.6  
     1.7 -#if defined(MUSIC_MP3_MAD) /*|| defined(MUSIC_MP3_MPG123)*/
     1.8 +#if defined(MUSIC_MP3_MAD) || defined(MUSIC_MP3_MPG123)
     1.9  
    1.10  /*********************** SDL_RW WITH BOOKKEEPING ************************/
    1.11  
     2.1 --- a/src/codecs/music_mpg123.c	Wed Dec 11 00:50:02 2019 +0300
     2.2 +++ b/src/codecs/music_mpg123.c	Wed Dec 11 01:15:02 2019 +0300
     2.3 @@ -29,6 +29,7 @@
     2.4  #include "SDL_loadso.h"
     2.5  
     2.6  #include "music_mpg123.h"
     2.7 +#include "mp3utils.h"
     2.8  
     2.9  #include <mpg123.h>
    2.10  
    2.11 @@ -94,7 +95,7 @@
    2.12          FUNCTION_LOADER(mpg123_new, mpg123_handle *(*)(const char* decoder, int *error))
    2.13          FUNCTION_LOADER(mpg123_open_handle, int (*)(mpg123_handle *mh, void *iohandle))
    2.14          FUNCTION_LOADER(mpg123_plain_strerror, const char* (*)(int errcode))
    2.15 -        FUNCTION_LOADER(mpg123_rates, void (*)(const long **list, size_t *number));
    2.16 +        FUNCTION_LOADER(mpg123_rates, void (*)(const long **list, size_t *number))
    2.17          FUNCTION_LOADER(mpg123_read, int (*)(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done ))
    2.18          FUNCTION_LOADER(mpg123_replace_reader_handle, int (*)( mpg123_handle *mh, ssize_t (*r_read) (void *, void *, size_t), off_t (*r_lseek)(void *, off_t, int), void (*cleanup)(void*) ))
    2.19          FUNCTION_LOADER(mpg123_seek, off_t (*)( mpg123_handle *mh, off_t sampleoff, int whence ))
    2.20 @@ -121,8 +122,8 @@
    2.21  
    2.22  typedef struct
    2.23  {
    2.24 +    struct mp3file_t mp3file;
    2.25      int play_count;
    2.26 -    SDL_RWops* src;
    2.27      int freesrc;
    2.28      int volume;
    2.29  
    2.30 @@ -183,12 +184,12 @@
    2.31  /* we're gonna override mpg123's I/O with these wrappers for RWops */
    2.32  static ssize_t rwops_read(void* p, void* dst, size_t n)
    2.33  {
    2.34 -    return (ssize_t)SDL_RWread((SDL_RWops*)p, dst, 1, n);
    2.35 +    return (ssize_t)MP3_RWread((struct mp3file_t *)p, dst, 1, n);
    2.36  }
    2.37  
    2.38  static off_t rwops_seek(void* p, off_t offset, int whence)
    2.39  {
    2.40 -    return (off_t)SDL_RWseek((SDL_RWops*)p, (Sint64)offset, whence);
    2.41 +    return (off_t)MP3_RWseek((struct mp3file_t *)p, (Sint64)offset, whence);
    2.42  }
    2.43  
    2.44  static void rwops_cleanup(void* p)
    2.45 @@ -219,9 +220,17 @@
    2.46      if (!music) {
    2.47          return NULL;
    2.48      }
    2.49 -    music->src = src;
    2.50 +    music->mp3file.src = src;
    2.51      music->volume = MIX_MAX_VOLUME;
    2.52  
    2.53 +    music->mp3file.length = SDL_RWsize(src);
    2.54 +    if (mp3_skiptags(&music->mp3file) < 0) {
    2.55 +        SDL_free(music);
    2.56 +        Mix_SetError("music_mpg123: corrupt mp3 file (bad tags.)");
    2.57 +        return NULL;
    2.58 +    }
    2.59 +    MP3_RWseek(&music->mp3file, 0, RW_SEEK_SET);
    2.60 +
    2.61      /* Just assume 16-bit 2 channel audio for now */
    2.62      music->buffer_size = music_spec.samples * sizeof(Sint16) * 2;
    2.63      music->buffer = (unsigned char *)SDL_malloc(music->buffer_size);
    2.64 @@ -268,7 +277,7 @@
    2.65          mpg123.mpg123_format(music->handle, rates[i], channels, formats);
    2.66      }
    2.67  
    2.68 -    result = mpg123.mpg123_open_handle(music->handle, music->src);
    2.69 +    result = mpg123.mpg123_open_handle(music->handle, &music->mp3file);
    2.70      if (result != MPG123_OK) {
    2.71          MPG123_Delete(music);
    2.72          Mix_SetError("mpg123_open_handle: %s", mpg_err(music->handle, result));
    2.73 @@ -392,7 +401,7 @@
    2.74          SDL_free(music->buffer);
    2.75      }
    2.76      if (music->freesrc) {
    2.77 -        SDL_RWclose(music->src);
    2.78 +        SDL_RWclose(music->mp3file.src);
    2.79      }
    2.80      SDL_free(music);
    2.81  }