dynamic_flac.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 22:12:31 -0800
changeset 712 7e59d684b070
parent 711 f40c5ac95b12
child 725 bdf7b8d20566
permissions -rw-r--r--
Updated to version 2.0.1
     1 /*
     2   SDL_mixer:  An audio mixer library based on the SDL library
     3   Copyright (C) 1997-2016 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   Implementation of the dynamic loading functionality for libFLAC.
    22     ~ Austen Dicken (admin@cvpcs.org)
    23 */
    24 
    25 #ifdef FLAC_MUSIC
    26 
    27 #include "SDL_loadso.h"
    28 #include "SDL_mixer.h"
    29 #include "dynamic_flac.h"
    30 
    31 flac_loader flac = {
    32     0, NULL
    33 };
    34 
    35 #ifdef FLAC_DYNAMIC
    36 int Mix_InitFLAC()
    37 {
    38     if ( flac.loaded == 0 ) {
    39         flac.handle = SDL_LoadObject(FLAC_DYNAMIC);
    40         if ( flac.handle == NULL ) {
    41             return -1;
    42         }
    43         flac.FLAC__stream_decoder_new =
    44             (FLAC__StreamDecoder *(*)())
    45             SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_new");
    46         if ( flac.FLAC__stream_decoder_new == NULL ) {
    47             SDL_UnloadObject(flac.handle);
    48             return -1;
    49         }
    50         flac.FLAC__stream_decoder_delete =
    51             (void (*)(FLAC__StreamDecoder *))
    52             SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_delete");
    53         if ( flac.FLAC__stream_decoder_delete == NULL ) {
    54             SDL_UnloadObject(flac.handle);
    55             return -1;
    56         }
    57         flac.FLAC__stream_decoder_init_stream =
    58             (FLAC__StreamDecoderInitStatus (*)(
    59                         FLAC__StreamDecoder *,
    60                         FLAC__StreamDecoderReadCallback,
    61                         FLAC__StreamDecoderSeekCallback,
    62                         FLAC__StreamDecoderTellCallback,
    63                         FLAC__StreamDecoderLengthCallback,
    64                         FLAC__StreamDecoderEofCallback,
    65                         FLAC__StreamDecoderWriteCallback,
    66                         FLAC__StreamDecoderMetadataCallback,
    67                         FLAC__StreamDecoderErrorCallback,
    68                         void *))
    69             SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_init_stream");
    70         if ( flac.FLAC__stream_decoder_init_stream == NULL ) {
    71             SDL_UnloadObject(flac.handle);
    72             return -1;
    73         }
    74         flac.FLAC__stream_decoder_finish =
    75             (FLAC__bool (*)(FLAC__StreamDecoder *))
    76             SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_finish");
    77         if ( flac.FLAC__stream_decoder_finish == NULL ) {
    78             SDL_UnloadObject(flac.handle);
    79             return -1;
    80         }
    81         flac.FLAC__stream_decoder_flush =
    82             (FLAC__bool (*)(FLAC__StreamDecoder *))
    83             SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_flush");
    84         if ( flac.FLAC__stream_decoder_flush == NULL ) {
    85             SDL_UnloadObject(flac.handle);
    86             return -1;
    87         }
    88         flac.FLAC__stream_decoder_process_single =
    89             (FLAC__bool (*)(FLAC__StreamDecoder *))
    90             SDL_LoadFunction(flac.handle,
    91                         "FLAC__stream_decoder_process_single");
    92         if ( flac.FLAC__stream_decoder_process_single == NULL ) {
    93             SDL_UnloadObject(flac.handle);
    94             return -1;
    95         }
    96         flac.FLAC__stream_decoder_process_until_end_of_metadata =
    97             (FLAC__bool (*)(FLAC__StreamDecoder *))
    98             SDL_LoadFunction(flac.handle,
    99                         "FLAC__stream_decoder_process_until_end_of_metadata");
   100         if ( flac.FLAC__stream_decoder_process_until_end_of_metadata == NULL ) {
   101             SDL_UnloadObject(flac.handle);
   102             return -1;
   103         }
   104         flac.FLAC__stream_decoder_process_until_end_of_stream =
   105             (FLAC__bool (*)(FLAC__StreamDecoder *))
   106             SDL_LoadFunction(flac.handle,
   107                         "FLAC__stream_decoder_process_until_end_of_stream");
   108         if ( flac.FLAC__stream_decoder_process_until_end_of_stream == NULL ) {
   109             SDL_UnloadObject(flac.handle);
   110             return -1;
   111         }
   112         flac.FLAC__stream_decoder_seek_absolute =
   113             (FLAC__bool (*)(FLAC__StreamDecoder *, FLAC__uint64))
   114             SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_seek_absolute");
   115         if ( flac.FLAC__stream_decoder_seek_absolute == NULL ) {
   116             SDL_UnloadObject(flac.handle);
   117             return -1;
   118         }
   119         flac.FLAC__stream_decoder_get_state =
   120             (FLAC__StreamDecoderState (*)(const FLAC__StreamDecoder *decoder))
   121             SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_get_state");
   122         if ( flac.FLAC__stream_decoder_get_state == NULL ) {
   123             SDL_UnloadObject(flac.handle);
   124             return -1;
   125         }
   126     }
   127     ++flac.loaded;
   128 
   129     return 0;
   130 }
   131 void Mix_QuitFLAC()
   132 {
   133     if ( flac.loaded == 0 ) {
   134         return;
   135     }
   136     if ( flac.loaded == 1 ) {
   137         SDL_UnloadObject(flac.handle);
   138     }
   139     --flac.loaded;
   140 }
   141 #else
   142 int Mix_InitFLAC()
   143 {
   144     if ( flac.loaded == 0 ) {
   145 #ifdef __MACOSX__
   146         extern FLAC__StreamDecoder *FLAC__stream_decoder_new(void) __attribute__((weak_import));
   147         if ( FLAC__stream_decoder_new == NULL )
   148         {
   149             /* Missing weakly linked framework */
   150             Mix_SetError("Missing FLAC.framework");
   151             return -1;
   152         }
   153 #endif // __MACOSX__
   154 
   155         flac.FLAC__stream_decoder_new = FLAC__stream_decoder_new;
   156         flac.FLAC__stream_decoder_delete = FLAC__stream_decoder_delete;
   157         flac.FLAC__stream_decoder_init_stream =
   158                             FLAC__stream_decoder_init_stream;
   159         flac.FLAC__stream_decoder_finish = FLAC__stream_decoder_finish;
   160         flac.FLAC__stream_decoder_flush = FLAC__stream_decoder_flush;
   161         flac.FLAC__stream_decoder_process_single =
   162                             FLAC__stream_decoder_process_single;
   163         flac.FLAC__stream_decoder_process_until_end_of_metadata =
   164                             FLAC__stream_decoder_process_until_end_of_metadata;
   165         flac.FLAC__stream_decoder_process_until_end_of_stream =
   166                             FLAC__stream_decoder_process_until_end_of_stream;
   167         flac.FLAC__stream_decoder_seek_absolute =
   168                             FLAC__stream_decoder_seek_absolute;
   169         flac.FLAC__stream_decoder_get_state =
   170                             FLAC__stream_decoder_get_state;
   171     }
   172     ++flac.loaded;
   173 
   174     return 0;
   175 }
   176 void Mix_QuitFLAC()
   177 {
   178     if ( flac.loaded == 0 ) {
   179         return;
   180     }
   181     if ( flac.loaded == 1 ) {
   182     }
   183     --flac.loaded;
   184 }
   185 #endif /* FLAC_DYNAMIC */
   186 
   187 #endif /* FLAC_MUSIC */