load_flac.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 Jun 2015 00:11:41 -0700
changeset 705 fe757163b8f7
parent 625 1d489d8ec2e0
child 711 f40c5ac95b12
permissions -rw-r--r--
Fixed bug 3018 - Loading MIDI music using FluidSynth leaks memory.

Philipp Wiesemann

There is a memory leak in fluidsynth.c and fluidsynth_loadsong_RW_internal(). The allocated temporary buffer is not deleted if fluid_player_add_mem() returns FLUID_OK.
slouken@382
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@601
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@382
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@382
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@382
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@382
    20
slouken@518
    21
  This is the source needed to decode a FLAC into a waveform.
slouken@617
    22
    ~ Austen Dicken (admin@cvpcs.org).
slouken@382
    23
*/
slouken@382
    24
slouken@382
    25
#ifdef FLAC_MUSIC
slouken@382
    26
slouken@382
    27
#include <stdio.h>
slouken@382
    28
#include <stdlib.h>
slouken@382
    29
#include <string.h>
slouken@382
    30
slouken@382
    31
#include "SDL_mutex.h"
slouken@382
    32
#include "SDL_endian.h"
slouken@382
    33
#include "SDL_timer.h"
slouken@382
    34
slouken@382
    35
#include "SDL_mixer.h"
slouken@382
    36
#include "dynamic_flac.h"
slouken@382
    37
#include "load_flac.h"
slouken@382
    38
slouken@382
    39
#include <FLAC/stream_decoder.h>
slouken@382
    40
slouken@382
    41
typedef struct {
slouken@617
    42
    SDL_RWops* sdl_src;
slouken@617
    43
    SDL_AudioSpec* sdl_spec;
slouken@617
    44
    Uint8** sdl_audio_buf;
slouken@617
    45
    Uint32* sdl_audio_len;
slouken@617
    46
    int sdl_audio_read;
slouken@617
    47
    FLAC__uint64 flac_total_samples;
slouken@617
    48
    unsigned flac_bps;
slouken@382
    49
} FLAC_SDL_Data;
slouken@382
    50
slouken@382
    51
static FLAC__StreamDecoderReadStatus flac_read_load_cb(
slouken@617
    52
                                    const FLAC__StreamDecoder *decoder,
slouken@617
    53
                                    FLAC__byte buffer[],
slouken@617
    54
                                    size_t *bytes,
slouken@617
    55
                                    void *client_data)
slouken@436
    56
{
slouken@617
    57
    // make sure there is something to be reading
slouken@617
    58
    if (*bytes > 0) {
slouken@617
    59
        FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
    60
slouken@617
    61
        *bytes = SDL_RWread (data->sdl_src, buffer, sizeof (FLAC__byte),
slouken@617
    62
                                *bytes);
slouken@382
    63
slouken@617
    64
        if (*bytes == 0) { // error or no data was read (EOF)
slouken@617
    65
            return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
slouken@621
    66
        } else { // data was read, continue
slouken@617
    67
            return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
slouken@617
    68
        }
slouken@621
    69
    } else {
slouken@617
    70
        return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
slouken@617
    71
    }
slouken@382
    72
}
slouken@382
    73
slouken@382
    74
static FLAC__StreamDecoderSeekStatus flac_seek_load_cb(
slouken@617
    75
                                    const FLAC__StreamDecoder *decoder,
slouken@617
    76
                                    FLAC__uint64 absolute_byte_offset,
slouken@617
    77
                                    void *client_data)
slouken@436
    78
{
slouken@617
    79
    FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
    80
slouken@617
    81
    if (SDL_RWseek (data->sdl_src, absolute_byte_offset, RW_SEEK_SET) < 0) {
slouken@617
    82
        return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
slouken@621
    83
    } else {
slouken@617
    84
        return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
slouken@617
    85
    }
slouken@382
    86
}
slouken@382
    87
slouken@382
    88
static FLAC__StreamDecoderTellStatus flac_tell_load_cb(
slouken@617
    89
                                    const FLAC__StreamDecoder *decoder,
slouken@617
    90
                                    FLAC__uint64 *absolute_byte_offset,
slouken@617
    91
                                    void *client_data)
slouken@436
    92
{
slouken@617
    93
    FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
    94
slouken@621
    95
    Sint64 pos = SDL_RWtell (data->sdl_src);
slouken@382
    96
slouken@617
    97
    if (pos < 0) {
slouken@617
    98
        return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
slouken@621
    99
    } else {
slouken@617
   100
        *absolute_byte_offset = (FLAC__uint64)pos;
slouken@617
   101
        return FLAC__STREAM_DECODER_TELL_STATUS_OK;
slouken@617
   102
    }
slouken@382
   103
}
slouken@382
   104
slouken@382
   105
static FLAC__StreamDecoderLengthStatus flac_length_load_cb(
slouken@617
   106
                                    const FLAC__StreamDecoder *decoder,
slouken@617
   107
                                    FLAC__uint64 *stream_length,
slouken@617
   108
                                    void *client_data)
slouken@436
   109
{
slouken@617
   110
    FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
   111
slouken@621
   112
    Sint64 pos = SDL_RWtell (data->sdl_src);
slouken@621
   113
    Sint64 length = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END);
slouken@382
   114
slouken@617
   115
    if (SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET) != pos || length < 0) {
slouken@617
   116
        /* there was an error attempting to return the stream to the original
slouken@617
   117
         * position, or the length was invalid. */
slouken@617
   118
        return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
slouken@621
   119
    } else {
slouken@617
   120
        *stream_length = (FLAC__uint64)length;
slouken@617
   121
        return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
slouken@617
   122
    }
slouken@382
   123
}
slouken@382
   124
slouken@382
   125
static FLAC__bool flac_eof_load_cb(const FLAC__StreamDecoder *decoder,
slouken@617
   126
                                    void *client_data)
slouken@436
   127
{
slouken@617
   128
    FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
   129
slouken@621
   130
    Sint64 pos = SDL_RWtell (data->sdl_src);
slouken@621
   131
    Sint64 end = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END);
slouken@382
   132
slouken@617
   133
    // was the original position equal to the end (a.k.a. the seek didn't move)?
slouken@617
   134
    if (pos == end) {
slouken@617
   135
        // must be EOF
slouken@617
   136
        return true;
slouken@621
   137
    } else {
slouken@617
   138
        // not EOF, return to the original position
slouken@617
   139
        SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET);
slouken@617
   140
        return false;
slouken@617
   141
    }
slouken@382
   142
}
slouken@382
   143
slouken@382
   144
static FLAC__StreamDecoderWriteStatus flac_write_load_cb(
slouken@617
   145
                                    const FLAC__StreamDecoder *decoder,
slouken@617
   146
                                    const FLAC__Frame *frame,
slouken@617
   147
                                    const FLAC__int32 *const buffer[],
slouken@617
   148
                                    void *client_data)
slouken@436
   149
{
slouken@617
   150
    FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@617
   151
    size_t i;
slouken@617
   152
    Uint8 *buf;
slouken@382
   153
slouken@617
   154
    if (data->flac_total_samples == 0) {
slouken@617
   155
        SDL_SetError ("Given FLAC file does not specify its sample count.");
slouken@617
   156
        return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@617
   157
    }
slouken@382
   158
slouken@617
   159
    if (data->sdl_spec->channels != 2 || data->flac_bps != 16) {
slouken@617
   160
        SDL_SetError ("Current FLAC support is only for 16 bit Stereo files.");
slouken@617
   161
        return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@617
   162
    }
slouken@382
   163
slouken@617
   164
    // check if it is the first audio frame so we can initialize the output
slouken@617
   165
    // buffer
slouken@617
   166
    if (frame->header.number.sample_number == 0) {
slouken@617
   167
        *(data->sdl_audio_len) = data->sdl_spec->size;
slouken@617
   168
        data->sdl_audio_read = 0;
slouken@617
   169
        *(data->sdl_audio_buf) = SDL_malloc (*(data->sdl_audio_len));
slouken@382
   170
slouken@617
   171
        if (*(data->sdl_audio_buf) == NULL) {
slouken@617
   172
            SDL_SetError
slouken@617
   173
                    ("Unable to allocate memory to store the FLAC stream.");
slouken@617
   174
            return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@617
   175
        }
slouken@617
   176
    }
slouken@382
   177
slouken@617
   178
    buf = *(data->sdl_audio_buf);
slouken@382
   179
slouken@617
   180
    for (i = 0; i < frame->header.blocksize; i++) {
slouken@617
   181
        FLAC__int16 i16;
slouken@617
   182
        FLAC__uint16 ui16;
slouken@382
   183
slouken@617
   184
        i16 = (FLAC__int16)buffer[0][i];
slouken@617
   185
        ui16 = (FLAC__uint16)i16;
slouken@382
   186
slouken@617
   187
        *(buf + (data->sdl_audio_read++)) = (char)(ui16);
slouken@617
   188
        *(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8);
slouken@382
   189
slouken@617
   190
        i16 = (FLAC__int16)buffer[1][i];
slouken@617
   191
        ui16 = (FLAC__uint16)i16;
slouken@382
   192
slouken@617
   193
        *(buf + (data->sdl_audio_read++)) = (char)(ui16);
slouken@617
   194
        *(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8);
slouken@617
   195
    }
slouken@382
   196
slouken@617
   197
    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
slouken@382
   198
}
slouken@382
   199
slouken@382
   200
static void flac_metadata_load_cb(
slouken@617
   201
                    const FLAC__StreamDecoder *decoder,
slouken@617
   202
                    const FLAC__StreamMetadata *metadata,
slouken@617
   203
                    void *client_data)
slouken@470
   204
{
slouken@617
   205
    FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@617
   206
    FLAC__uint64 total_samples;
slouken@617
   207
    unsigned bps;
slouken@382
   208
slouken@617
   209
    if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
slouken@617
   210
        // save the metadata right now for use later on
slouken@617
   211
        *(data->sdl_audio_buf) = NULL;
slouken@617
   212
        *(data->sdl_audio_len) = 0;
slouken@617
   213
        memset (data->sdl_spec, '\0', sizeof (SDL_AudioSpec));
slouken@382
   214
slouken@617
   215
        data->sdl_spec->format = AUDIO_S16;
slouken@617
   216
        data->sdl_spec->freq = (int)(metadata->data.stream_info.sample_rate);
slouken@617
   217
        data->sdl_spec->channels = (Uint8)(metadata->data.stream_info.channels);
slouken@617
   218
        data->sdl_spec->samples = 8192; /* buffer size */
slouken@382
   219
slouken@617
   220
        total_samples = metadata->data.stream_info.total_samples;
slouken@617
   221
        bps = metadata->data.stream_info.bits_per_sample;
slouken@382
   222
slouken@621
   223
        data->sdl_spec->size = (Uint32)(total_samples * data->sdl_spec->channels * (bps / 8));
slouken@617
   224
        data->flac_total_samples = total_samples;
slouken@617
   225
        data->flac_bps = bps;
slouken@617
   226
    }
slouken@382
   227
}
slouken@382
   228
slouken@382
   229
static void flac_error_load_cb(
slouken@617
   230
                const FLAC__StreamDecoder *decoder,
slouken@617
   231
                FLAC__StreamDecoderErrorStatus status,
slouken@617
   232
                void *client_data)
slouken@470
   233
{
slouken@617
   234
    // print an SDL error based on the error status
slouken@617
   235
    switch (status) {
slouken@617
   236
        case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
slouken@617
   237
            SDL_SetError ("Error processing the FLAC file [LOST_SYNC].");
slouken@617
   238
        break;
slouken@617
   239
        case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
slouken@617
   240
            SDL_SetError ("Error processing the FLAC file [BAD_HEADER].");
slouken@617
   241
        break;
slouken@617
   242
        case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
slouken@617
   243
            SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH].");
slouken@617
   244
        break;
slouken@617
   245
        case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM:
slouken@617
   246
            SDL_SetError ("Error processing the FLAC file [UNPARSEABLE].");
slouken@617
   247
        break;
slouken@617
   248
        default:
slouken@617
   249
            SDL_SetError ("Error processing the FLAC file [UNKNOWN].");
slouken@617
   250
        break;
slouken@617
   251
    }
slouken@382
   252
}
slouken@382
   253
slouken@382
   254
/* don't call this directly; use Mix_LoadWAV_RW() for now. */
slouken@382
   255
SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,
slouken@470
   256
        SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
slouken@470
   257
{
slouken@617
   258
    FLAC__StreamDecoder *decoder = 0;
slouken@617
   259
    FLAC__StreamDecoderInitStatus init_status;
slouken@617
   260
    int was_error = 1;
slouken@617
   261
    int was_init = 0;
slouken@617
   262
    Uint32 samplesize;
slouken@382
   263
slouken@617
   264
    // create the client data passing information
slouken@617
   265
    FLAC_SDL_Data* client_data;
slouken@617
   266
    client_data = (FLAC_SDL_Data *)SDL_malloc (sizeof (FLAC_SDL_Data));
slouken@382
   267
slouken@617
   268
    if ((!src) || (!audio_buf) || (!audio_len))   /* sanity checks. */
slouken@617
   269
        goto done;
slouken@382
   270
slouken@617
   271
    if (!Mix_Init(MIX_INIT_FLAC))
slouken@617
   272
        goto done;
slouken@382
   273
slouken@617
   274
    if ((decoder = flac.FLAC__stream_decoder_new ()) == NULL) {
slouken@617
   275
        SDL_SetError ("Unable to allocate FLAC decoder.");
slouken@617
   276
        goto done;
slouken@617
   277
    }
slouken@382
   278
slouken@617
   279
    init_status = flac.FLAC__stream_decoder_init_stream (decoder,
slouken@617
   280
                                flac_read_load_cb, flac_seek_load_cb,
slouken@617
   281
                                flac_tell_load_cb, flac_length_load_cb,
slouken@617
   282
                                flac_eof_load_cb, flac_write_load_cb,
slouken@617
   283
                                flac_metadata_load_cb, flac_error_load_cb,
slouken@617
   284
                                client_data);
slouken@382
   285
slouken@617
   286
    if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
slouken@617
   287
        SDL_SetError ("Unable to initialize FLAC stream decoder.");
slouken@617
   288
        goto done;
slouken@617
   289
    }
slouken@382
   290
slouken@617
   291
    was_init = 1;
slouken@382
   292
slouken@617
   293
    client_data->sdl_src = src;
slouken@617
   294
    client_data->sdl_spec = spec;
slouken@617
   295
    client_data->sdl_audio_buf = audio_buf;
slouken@617
   296
    client_data->sdl_audio_len = audio_len;
slouken@382
   297
slouken@617
   298
    if (!flac.FLAC__stream_decoder_process_until_end_of_stream (decoder)) {
slouken@617
   299
        SDL_SetError ("Unable to process FLAC file.");
slouken@617
   300
        goto done;
slouken@617
   301
    }
slouken@382
   302
slouken@617
   303
    was_error = 0;
slouken@382
   304
slouken@617
   305
    /* Don't return a buffer that isn't a multiple of samplesize */
slouken@617
   306
    samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
slouken@617
   307
    *audio_len &= ~(samplesize - 1);
slouken@382
   308
slouken@382
   309
done:
slouken@617
   310
    if (was_init && decoder) {
slouken@617
   311
        flac.FLAC__stream_decoder_finish (decoder);
slouken@617
   312
    }
slouken@382
   313
slouken@617
   314
    if (decoder) {
slouken@617
   315
        flac.FLAC__stream_decoder_delete (decoder);
slouken@617
   316
    }
slouken@382
   317
slouken@625
   318
    if (freesrc && src) {
slouken@625
   319
        SDL_RWclose (src);
slouken@617
   320
    }
slouken@382
   321
slouken@625
   322
    if (was_error) {
slouken@617
   323
        spec = NULL;
slouken@625
   324
    }
slouken@382
   325
slouken@382
   326
    return spec;
slouken@382
   327
}
slouken@382
   328
slouken@382
   329
#endif // FLAC_MUSIC