1 /*
2   SDL_mixer:  An audio mixer library based on the SDL library
3   Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifdef MUSIC_MP3_MAD
23 
24 #include "music_mad.h"
25 
26 #include "mad.h"
27 
28 
29 /* NOTE: The dithering functions are GPL, which should be fine if your
30          application is GPL (which would need to be true if you enabled
31          libmad support in SDL_mixer). If you're using libmad under the
32          commercial license, you need to disable this code.
33 */
34 /************************ dithering functions ***************************/
35 
36 #ifdef MUSIC_MP3_MAD_GPL_DITHERING
37 
38 /* All dithering done here is taken from the GPL'ed xmms-mad plugin. */
39 
40 /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.       */
41 /* Any feedback is very welcome. For any question, comments,       */
42 /* see http://www.math.keio.ac.jp/matumoto/emt.html or email       */
43 /* matumoto@math.keio.ac.jp                                        */
44 
45 /* Period parameters */
46 #define MP3_DITH_N 624
47 #define MP3_DITH_M 397
48 #define MATRIX_A 0x9908b0df   /* constant vector a */
49 #define UPPER_MASK 0x80000000 /* most significant w-r bits */
50 #define LOWER_MASK 0x7fffffff /* least significant r bits */
51 
52 /* Tempering parameters */
53 #define TEMPERING_MASK_B 0x9d2c5680
54 #define TEMPERING_MASK_C 0xefc60000
55 #define TEMPERING_SHIFT_U(y)  (y >> 11)
56 #define TEMPERING_SHIFT_S(y)  (y << 7)
57 #define TEMPERING_SHIFT_T(y)  (y << 15)
58 #define TEMPERING_SHIFT_L(y)  (y >> 18)
59 
60 static unsigned long mt[MP3_DITH_N]; /* the array for the state vector  */
61 static int mti=MP3_DITH_N+1; /* mti==MP3_DITH_N+1 means mt[MP3_DITH_N] is not initialized */
62 
63 /* initializing the array with a NONZERO seed */
sgenrand(unsigned long seed)64 static void sgenrand(unsigned long seed)
65 {
66     /* setting initial seeds to mt[MP3_DITH_N] using         */
67     /* the generator Line 25 of Table 1 in          */
68     /* [KNUTH 1981, The Art of Computer Programming */
69     /*    Vol. 2 (2nd Ed.), pp102]                  */
70     mt[0]= seed & 0xffffffff;
71     for (mti=1; mti<MP3_DITH_N; mti++)
72         mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
73 }
74 
genrand(void)75 static unsigned long genrand(void)
76 {
77     unsigned long y;
78     static unsigned long mag01[2]={0x0, MATRIX_A};
79     /* mag01[x] = x * MATRIX_A  for x=0,1 */
80 
81     if (mti >= MP3_DITH_N) { /* generate MP3_DITH_N words at one time */
82         int kk;
83 
84         if (mti == MP3_DITH_N+1)   /* if sgenrand() has not been called, */
85             sgenrand(4357); /* a default initial seed is used   */
86 
87         for (kk=0;kk<MP3_DITH_N-MP3_DITH_M;kk++) {
88             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
89             mt[kk] = mt[kk+MP3_DITH_M] ^ (y >> 1) ^ mag01[y & 0x1];
90         }
91         for (;kk<MP3_DITH_N-1;kk++) {
92             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
93             mt[kk] = mt[kk+(MP3_DITH_M-MP3_DITH_N)] ^ (y >> 1) ^ mag01[y & 0x1];
94         }
95         y = (mt[MP3_DITH_N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
96         mt[MP3_DITH_N-1] = mt[MP3_DITH_M-1] ^ (y >> 1) ^ mag01[y & 0x1];
97 
98         mti = 0;
99     }
100 
101     y = mt[mti++];
102     y ^= TEMPERING_SHIFT_U(y);
103     y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
104     y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
105     y ^= TEMPERING_SHIFT_L(y);
106 
107     return y;
108 }
109 
triangular_dither_noise(int nbits)110 static long triangular_dither_noise(int nbits) {
111     /* parameter nbits : the peak-to-peak amplitude desired (in bits)
112      *  use with nbits set to    2 + nber of bits to be trimmed.
113      * (because triangular is made from two uniformly distributed processes,
114      * it starts at 2 bits peak-to-peak amplitude)
115      * see The Theory of Dithered Quantization by Robert Alexander Wannamaker
116      * for complete proof of why that's optimal
117      */
118     long v = (genrand()/2 - genrand()/2); /* in ]-2^31, 2^31[ */
119     long P = 1 << (32 - nbits); /* the power of 2 */
120     v /= P;
121     /* now v in ]-2^(nbits-1), 2^(nbits-1) [ */
122 
123     return v;
124 }
125 
126 #endif /* MUSIC_MP3_MAD_GPL_DITHERING */
127 
128 
129 #define MAD_INPUT_BUFFER_SIZE   (5*8192)
130 
131 enum {
132     MS_input_eof      = 0x0001,
133     MS_input_error    = 0x0001,
134     MS_decode_error   = 0x0002,
135     MS_error_flags    = 0x000f,
136 };
137 
138 typedef struct {
139     int play_count;
140     SDL_RWops *src;
141     int freesrc;
142     struct mad_stream stream;
143     struct mad_frame frame;
144     struct mad_synth synth;
145     mad_timer_t next_frame_start;
146     int volume;
147     int status;
148     SDL_AudioStream *audiostream;
149 
150     unsigned char input_buffer[MAD_INPUT_BUFFER_SIZE + MAD_BUFFER_GUARD];
151 } MAD_Music;
152 
153 
154 static int MAD_Seek(void *context, double position);
155 
MAD_CreateFromRW(SDL_RWops * src,int freesrc)156 static void *MAD_CreateFromRW(SDL_RWops *src, int freesrc)
157 {
158     MAD_Music *music;
159 
160     music = (MAD_Music *)SDL_calloc(1, sizeof(MAD_Music));
161     if (!music) {
162         SDL_OutOfMemory();
163         return NULL;
164     }
165     music->src = src;
166     music->volume = MIX_MAX_VOLUME;
167 
168     mad_stream_init(&music->stream);
169     mad_frame_init(&music->frame);
170     mad_synth_init(&music->synth);
171     mad_timer_reset(&music->next_frame_start);
172 
173     music->freesrc = freesrc;
174     return music;
175 }
176 
MAD_SetVolume(void * context,int volume)177 static void MAD_SetVolume(void *context, int volume)
178 {
179     MAD_Music *music = (MAD_Music *)context;
180     music->volume = volume;
181 }
182 
183 /* Starts the playback. */
MAD_Play(void * context,int play_count)184 static int MAD_Play(void *context, int play_count)
185 {
186     MAD_Music *music = (MAD_Music *)context;
187     music->play_count = play_count;
188     return MAD_Seek(music, 0.0);
189 }
190 
191 
192 /*************************** TAG HANDLING: ******************************/
193 
is_id3v1(const Uint8 * data,size_t length)194 static SDL_INLINE SDL_bool is_id3v1(const Uint8 *data, size_t length)
195 {
196     /* http://id3.org/ID3v1 :  3 bytes "TAG" identifier and 125 bytes tag data */
197     if (length < 3 || SDL_memcmp(data,"TAG",3) != 0) {
198         return SDL_FALSE;
199     }
200     return SDL_TRUE;
201 }
is_id3v2(const Uint8 * data,size_t length)202 static SDL_INLINE SDL_bool is_id3v2(const Uint8 *data, size_t length)
203 {
204     /* ID3v2 header is 10 bytes:  http://id3.org/id3v2.4.0-structure */
205     /* bytes 0-2: "ID3" identifier */
206     if (length < 10 || SDL_memcmp(data,"ID3",3) != 0) {
207         return SDL_FALSE;
208     }
209     /* bytes 3-4: version num (major,revision), each byte always less than 0xff. */
210     if (data[3] == 0xff || data[4] == 0xff) {
211         return SDL_FALSE;
212     }
213     /* bytes 6-9 are the ID3v2 tag size: a 32 bit 'synchsafe' integer, i.e. the
214      * highest bit 7 in each byte zeroed.  i.e.: 7 bit information in each byte ->
215      * effectively a 28 bit value.  */
216     if (data[6] >= 0x80 || data[7] >= 0x80 || data[8] >= 0x80 || data[9] >= 0x80) {
217         return SDL_FALSE;
218     }
219     return SDL_TRUE;
220 }
is_apetag(const Uint8 * data,size_t length)221 static SDL_INLINE SDL_bool is_apetag(const Uint8 *data, size_t length)
222 {
223    /* http://wiki.hydrogenaud.io/index.php?title=APEv2_specification
224     * APEv2 header is 32 bytes: bytes 0-7 ident, bytes 8-11 version,
225     * bytes 12-17 size.  bytes 24-31 are reserved: must be all zeroes.
226     * APEv1 has no header, so no luck.  */
227     Uint32 v;
228 
229     if (length < 32 || SDL_memcmp(data,"APETAGEX",8) != 0) {
230         return SDL_FALSE;
231     }
232     v = (data[11]<<24) | (data[10]<<16) | (data[9]<<8) | data[8]; /* version */
233     if (v != 2000U) {
234         return SDL_FALSE;
235     }
236     v = 0; /* reserved bits : */
237     if (SDL_memcmp(&data[24],&v,4) != 0 || SDL_memcmp(&data[28],&v,4) != 0) {
238         return SDL_FALSE;
239     }
240     return SDL_TRUE;
241 }
242 
get_tagsize(const Uint8 * data,size_t length)243 static size_t get_tagsize(const Uint8 *data, size_t length)
244 {
245     size_t size;
246 
247     if (is_id3v1(data, length)) {
248         return 128; /* fixed length */
249     }
250     if (is_id3v2(data, length)) {
251         /* size is a 'synchsafe' integer (see above) */
252         size = (data[6]<<21) + (data[7]<<14) + (data[8]<<7) + data[9];
253         size += 10; /* header size */
254         /* ID3v2 header[5] is flags (bits 4-7 only, 0-3 are zero).
255          * bit 4 set: footer is present (a copy of the header but
256          * with "3DI" as ident.)  */
257         if (data[5] & 0x10) {
258             size += 10; /* footer size */
259         }
260         /* optional padding (always zeroes) */
261         while (size < length && data[size] == 0) {
262             ++size;
263         }
264         return size;
265     }
266     if (is_apetag(data, length)) {
267         size = (data[15]<<24) | (data[14]<<16) | (data[13]<<8) | data[12];
268         size += 32; /* header size */
269         return size;
270     }
271     return 0;
272 }
273 
consume_tag(struct mad_stream * stream)274 static int consume_tag(struct mad_stream *stream)
275 {
276     /* FIXME: what if the buffer doesn't have the full tag ??? */
277     size_t remaining = stream->bufend - stream->next_frame;
278     size_t tagsize = get_tagsize(stream->this_frame, remaining);
279     if (tagsize != 0) {
280         mad_stream_skip(stream, tagsize);
281         return 0;
282     }
283     return -1;
284 }
285 
286 /* Reads the next frame from the file.
287    Returns true on success or false on failure.
288  */
read_next_frame(MAD_Music * music)289 static SDL_bool read_next_frame(MAD_Music *music)
290 {
291     if (music->stream.buffer == NULL ||
292         music->stream.error == MAD_ERROR_BUFLEN) {
293         size_t read_size;
294         size_t remaining;
295         unsigned char *read_start;
296 
297         /* There might be some bytes in the buffer left over from last
298            time.    If so, move them down and read more bytes following
299            them. */
300         if (music->stream.next_frame != NULL) {
301             remaining = music->stream.bufend - music->stream.next_frame;
302             memmove(music->input_buffer, music->stream.next_frame, remaining);
303             read_start = music->input_buffer + remaining;
304             read_size = MAD_INPUT_BUFFER_SIZE - remaining;
305 
306         } else {
307             read_size = MAD_INPUT_BUFFER_SIZE;
308             read_start = music->input_buffer;
309             remaining = 0;
310         }
311 
312         /* Now read additional bytes from the input file. */
313         read_size = SDL_RWread(music->src, read_start, 1, read_size);
314 
315         if (read_size == 0) {
316             if ((music->status & (MS_input_eof | MS_input_error)) == 0) {
317                 /* FIXME: how to detect error? */
318                 music->status |= MS_input_eof;
319 
320                 /* At the end of the file, we must stuff MAD_BUFFER_GUARD
321                    number of 0 bytes. */
322                 SDL_memset(read_start + read_size, 0, MAD_BUFFER_GUARD);
323                 read_size += MAD_BUFFER_GUARD;
324             }
325         }
326 
327         /* Now feed those bytes into the libmad stream. */
328         mad_stream_buffer(&music->stream, music->input_buffer,
329                                             read_size + remaining);
330         music->stream.error = MAD_ERROR_NONE;
331     }
332 
333     /* Now ask libmad to extract a frame from the data we just put in
334        its buffer. */
335     if (mad_frame_decode(&music->frame, &music->stream)) {
336         if (MAD_RECOVERABLE(music->stream.error)) {
337             consume_tag(&music->stream); /* consume any ID3 tags */
338             mad_stream_sync(&music->stream); /* to frame seek mode */
339             return SDL_FALSE;
340 
341         } else if (music->stream.error == MAD_ERROR_BUFLEN) {
342             return SDL_FALSE;
343 
344         } else {
345             Mix_SetError("mad_frame_decode() failed, corrupt stream?");
346             music->status |= MS_decode_error;
347             return SDL_FALSE;
348         }
349     }
350 
351     mad_timer_add(&music->next_frame_start, music->frame.header.duration);
352 
353     return SDL_TRUE;
354 }
355 
356 /* Scale a MAD sample to 16 bits for output. */
scale(mad_fixed_t sample)357 static Sint16 scale(mad_fixed_t sample)
358 {
359     const int n_bits_to_loose = MAD_F_FRACBITS + 1 - 16;
360 
361     /* round */
362     sample += (1L << (n_bits_to_loose - 1));
363 
364 #ifdef MUSIC_MP3_MAD_GPL_DITHERING
365     sample += triangular_dither_noise(n_bits_to_loose + 1);
366 #endif
367 
368     /* clip */
369     if (sample >= MAD_F_ONE)
370         sample = MAD_F_ONE - 1;
371     else if (sample < -MAD_F_ONE)
372         sample = -MAD_F_ONE;
373 
374     /* quantize */
375     return (Sint16)(sample >> n_bits_to_loose);
376 }
377 
378 /* Once the frame has been read, copies its samples into the output buffer. */
decode_frame(MAD_Music * music)379 static SDL_bool decode_frame(MAD_Music *music)
380 {
381     struct mad_pcm *pcm;
382     unsigned int i, nchannels, nsamples;
383     mad_fixed_t const *left_ch, *right_ch;
384     Sint16 *buffer, *dst;
385     int result;
386 
387     mad_synth_frame(&music->synth, &music->frame);
388     pcm = &music->synth.pcm;
389 
390     if (!music->audiostream) {
391         music->audiostream = SDL_NewAudioStream(AUDIO_S16, pcm->channels, pcm->samplerate, music_spec.format, music_spec.channels, music_spec.freq);
392         if (!music->audiostream) {
393             return SDL_FALSE;
394         }
395     }
396 
397     nchannels = pcm->channels;
398     nsamples = pcm->length;
399     left_ch = pcm->samples[0];
400     right_ch = pcm->samples[1];
401     buffer = SDL_stack_alloc(Sint16, nsamples*nchannels);
402     if (!buffer) {
403         SDL_OutOfMemory();
404         return SDL_FALSE;
405     }
406 
407     dst = buffer;
408     if (nchannels == 1) {
409         for (i = nsamples; i--;) {
410             *dst++ = scale(*left_ch++);
411         }
412     } else {
413         for (i = nsamples; i--;) {
414             *dst++ = scale(*left_ch++);
415             *dst++ = scale(*right_ch++);
416         }
417     }
418 
419     result = SDL_AudioStreamPut(music->audiostream, buffer, (nsamples * nchannels * sizeof(Sint16)));
420     SDL_stack_free(buffer);
421 
422     if (result < 0) {
423         return SDL_FALSE;
424     }
425     return SDL_TRUE;
426 }
427 
MAD_GetSome(void * context,void * data,int bytes,SDL_bool * done)428 static int MAD_GetSome(void *context, void *data, int bytes, SDL_bool *done)
429 {
430     MAD_Music *music = (MAD_Music *)context;
431     int filled;
432 
433     if (music->audiostream) {
434         filled = SDL_AudioStreamGet(music->audiostream, data, bytes);
435         if (filled != 0) {
436             return filled;
437         }
438     }
439 
440     if (!music->play_count) {
441         /* All done */
442         *done = SDL_TRUE;
443         return 0;
444     }
445 
446     if (read_next_frame(music)) {
447         if (!decode_frame(music)) {
448             return -1;
449         }
450     } else if (music->status & MS_input_eof) {
451         int play_count = -1;
452         if (music->play_count > 0) {
453             play_count = (music->play_count - 1);
454         }
455         if (MAD_Play(music, play_count) < 0) {
456             return -1;
457         }
458     } else if (music->status & MS_decode_error) {
459         return -1;
460     }
461     return 0;
462 }
MAD_GetAudio(void * context,void * data,int bytes)463 static int MAD_GetAudio(void *context, void *data, int bytes)
464 {
465     MAD_Music *music = (MAD_Music *)context;
466     return music_pcm_getaudio(context, data, bytes, music->volume, MAD_GetSome);
467 }
468 
MAD_Seek(void * context,double position)469 static int MAD_Seek(void *context, double position)
470 {
471     MAD_Music *music = (MAD_Music *)context;
472     mad_timer_t target;
473     int int_part;
474 
475     int_part = (int)position;
476     mad_timer_set(&target, int_part, (int)((position - int_part) * 1000000), 1000000);
477 
478     if (mad_timer_compare(music->next_frame_start, target) > 0) {
479         /* In order to seek backwards in a VBR file, we have to rewind and
480            start again from the beginning.    This isn't necessary if the
481            file happens to be CBR, of course; in that case we could seek
482            directly to the frame we want.    But I leave that little
483            optimization for the future developer who discovers she really
484            needs it. */
485         mad_timer_reset(&music->next_frame_start);
486         music->status &= ~MS_error_flags;
487 
488         SDL_RWseek(music->src, 0, RW_SEEK_SET);
489     }
490 
491     /* Now we have to skip frames until we come to the right one.
492        Again, only truly necessary if the file is VBR. */
493     while (mad_timer_compare(music->next_frame_start, target) < 0) {
494         if (!read_next_frame(music)) {
495             if ((music->status & MS_error_flags) != 0) {
496                 /* Couldn't read a frame; either an error condition or
497                      end-of-file.    Stop. */
498                 return Mix_SetError("Seek position out of range");
499             }
500         }
501     }
502 
503     /* Here we are, at the beginning of the frame that contains the
504        target time.    Ehh, I say that's close enough.    If we wanted to,
505        we could get more precise by decoding the frame now and counting
506        the appropriate number of samples out of it. */
507     return 0;
508 }
509 
MAD_Delete(void * context)510 static void MAD_Delete(void *context)
511 {
512     MAD_Music *music = (MAD_Music *)context;
513 
514     mad_stream_finish(&music->stream);
515     mad_frame_finish(&music->frame);
516     mad_synth_finish(&music->synth);
517 
518     if (music->audiostream) {
519         SDL_FreeAudioStream(music->audiostream);
520     }
521     if (music->freesrc) {
522         SDL_RWclose(music->src);
523     }
524     SDL_free(music);
525 }
526 
527 Mix_MusicInterface Mix_MusicInterface_MAD =
528 {
529     "MAD",
530     MIX_MUSIC_MAD,
531     MUS_MP3,
532     SDL_FALSE,
533     SDL_FALSE,
534 
535     NULL,   /* Load */
536     NULL,   /* Open */
537     MAD_CreateFromRW,
538     NULL,   /* CreateFromFile */
539     MAD_SetVolume,
540     MAD_Play,
541     NULL,   /* IsPlaying */
542     MAD_GetAudio,
543     MAD_Seek,
544     NULL,   /* Pause */
545     NULL,   /* Resume */
546     NULL,   /* Stop */
547     MAD_Delete,
548     NULL,   /* Close */
549     NULL,   /* Unload */
550 };
551 
552 #endif /* MUSIC_MP3_MAD */
553 
554 /* vi: set ts=4 sw=4 expandtab: */
555