@@ -88,6 +88,39 @@ STATIC bool mp3file_update_inbuf(audiomp3_mp3file_obj_t* self) {
8888#define BYTES_LEFT (self ) (self->inbuf_length - self->inbuf_offset)
8989#define CONSUME (self , n ) (self->inbuf_offset += n)
9090
91+ // http://id3.org/d3v2.3.0
92+ // http://id3.org/id3v2.3.0
93+ STATIC void mp3file_skip_id3v2 (audiomp3_mp3file_obj_t * self ) {
94+ mp3file_update_inbuf (self );
95+ if (BYTES_LEFT (self ) < 10 ) {
96+ return ;
97+ }
98+ uint8_t * data = READ_PTR (self );
99+ if (!(
100+ data [0 ] == 'I' &&
101+ data [1 ] == 'D' &&
102+ data [2 ] == '3' &&
103+ data [3 ] != 0xff &&
104+ data [4 ] != 0xff &&
105+ (data [5 ] & 0x1f ) == 0 &&
106+ (data [6 ] & 0x80 ) == 0 &&
107+ (data [7 ] & 0x80 ) == 0 &&
108+ (data [8 ] & 0x80 ) == 0 &&
109+ (data [9 ] & 0x80 ) == 0 )) {
110+ return ;
111+ }
112+ uint32_t size = (data [6 ] << 21 ) | (data [7 ] << 14 ) | (data [8 ] << 7 ) | (data [9 ]);
113+ size += 10 ; // size excludes the "header" (but not the "extended header")
114+ // First, deduct from size whatever is left in buffer
115+ uint32_t to_consume = MIN (size , BYTES_LEFT (self ));
116+ CONSUME (self , to_consume );
117+ size -= to_consume ;
118+
119+ // Next, seek in the file after the header
120+ f_lseek (& self -> file -> fp , f_tell (& self -> file -> fp ) + size );
121+ return ;
122+ }
123+
91124/* If a sync word can be found, advance to it and return true. Otherwise,
92125 * return false.
93126 */
@@ -223,6 +256,7 @@ void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t* self,
223256 self -> eof = 0 ;
224257 self -> other_channel = -1 ;
225258 mp3file_update_inbuf (self );
259+ mp3file_skip_id3v2 (self );
226260 mp3file_find_sync_word (self );
227261}
228262
@@ -253,6 +287,7 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t*
253287 self -> buffer_index = !self -> buffer_index ;
254288 int16_t * buffer = (int16_t * )(void * )self -> buffers [self -> buffer_index ];
255289
290+ mp3file_skip_id3v2 (self );
256291 if (!mp3file_find_sync_word (self )) {
257292 return self -> eof ? GET_BUFFER_DONE : GET_BUFFER_ERROR ;
258293 }
0 commit comments