diff --git a/source/animated_sprite.cpp b/source/animated_sprite.cpp index b35e7a2..17019db 100644 --- a/source/animated_sprite.cpp +++ b/source/animated_sprite.cpp @@ -1,11 +1,11 @@ #include "animated_sprite.h" -#include // Para SDL_LogWarn, SDL_LogCategory, SDL_LogError -#include // Para size_t -#include // Para basic_istream, basic_ifstream, basic_ios -#include // Para basic_stringstream -#include // Para runtime_error -#include "texture.h" // Para Texture -#include "utils.h" // Para printWithDots +#include // Para SDL_LogWarn, SDL_LogCategory, SDL_LogError +#include // Para size_t +#include // Para basic_istream, basic_ifstream, basic_ios +#include // Para basic_stringstream +#include // Para runtime_error +#include "texture.h" // Para Texture +#include "utils.h" // Para printWithDots // Carga las animaciones en un vector(Animations) desde un fichero AnimationsFileBuffer loadAnimationsFromFile(const std::string &file_path) @@ -55,19 +55,18 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr texture, const Animation // Obtiene el índice de la animación a partir del nombre int AnimatedSprite::getIndex(const std::string &name) { - auto it = animation_indices_.find(name); + auto it = animation_indices_.find(name); if (it != animation_indices_.end()) { // Si se encuentra la animación en el mapa, devuelve su índice return it->second; } - // Si no se encuentra, muestra una advertencia y devuelve -1 + // Si no se encuentra, muestra una advertencia y devuelve -1 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "** Warning: could not find \"%s\" animation", name.c_str()); return -1; } - // Calcula el frame correspondiente a la animación void AnimatedSprite::animate() { @@ -258,7 +257,7 @@ void AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so animations_.emplace_back(animation); // Rellena el mapa con el nombre y el nuevo índice - animation_indices_[animation.name] = animations_.size() - 1; + animation_indices_[animation.name] = animations_.size() - 1; } // Una vez procesada la línea, aumenta el índice para pasar a la siguiente diff --git a/source/animated_sprite.h b/source/animated_sprite.h index 3d54418..351a492 100644 --- a/source/animated_sprite.h +++ b/source/animated_sprite.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include #include diff --git a/source/asset.cpp b/source/asset.cpp index 6472b5a..1cff5dc 100644 --- a/source/asset.cpp +++ b/source/asset.cpp @@ -1,9 +1,9 @@ #include "asset.h" -#include // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError -#include // Para find_if, max -#include // Para basic_ifstream, ifstream -#include // Para allocator, string, char_traits, operator+ -#include "utils.h" // Para getFileName +#include // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError +#include // Para find_if, max +#include // Para basic_ifstream, ifstream +#include // Para allocator, string, char_traits, operator+ +#include "utils.h" // Para getFileName // Singleton Asset *Asset::instance_ = nullptr; @@ -14,7 +14,7 @@ void Asset::init(const std::string &executable_path) { Asset::instance_ = new As // Libera la instancia void Asset::destroy() { delete Asset::instance_; } - // Obtiene la instancia +// Obtiene la instancia Asset *Asset::get() { return Asset::instance_; } // Añade un elemento a la lista diff --git a/source/audio.cpp b/source/audio.cpp index c957b6f..e6f07a5 100644 --- a/source/audio.cpp +++ b/source/audio.cpp @@ -60,7 +60,7 @@ void Audio::playSound(const std::string &name, Group group) { if (sound_enabled_) { - JA_PlaySound(Resource::get()->getSound(name), 0 , static_cast(group)); + JA_PlaySound(Resource::get()->getSound(name), 0, static_cast(group)); } } diff --git a/source/audio.h b/source/audio.h index 719172e..f970c16 100644 --- a/source/audio.h +++ b/source/audio.h @@ -26,7 +26,7 @@ public: // --- Control de Sonidos --- void playSound(const std::string &name, Group group = Group::GAME); // Reproducir sonido puntual - void stopAllSounds(); // Detener todos los sonidos + void stopAllSounds(); // Detener todos los sonidos // --- Configuración General --- void enable(bool value); // Establecer estado general @@ -47,7 +47,7 @@ public: // --- Control de Volumen --- void setSoundVolume(int volume, Group group = Group::ALL); // Ajustar volumen de efectos - void setMusicVolume(int volume); // Ajustar volumen de música + void setMusicVolume(int volume); // Ajustar volumen de música private: enum class MusicState diff --git a/source/background.cpp b/source/background.cpp index c4963ef..750080c 100644 --- a/source/background.cpp +++ b/source/background.cpp @@ -1,8 +1,8 @@ #define _USE_MATH_DEFINES #include "background.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_PIXELFORMAT_RGBA8888 -#include // Para clamp, max +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_PIXELFORMAT_RGBA8888 +#include // Para clamp, max #include #include "moving_sprite.h" // Para MovingSprite #include "param.h" // Para Param, ParamBackground, param diff --git a/source/background.h b/source/background.h index a229499..3ba92f0 100644 --- a/source/background.h +++ b/source/background.h @@ -1,11 +1,11 @@ #pragma once -#include // Para SDL_FRect, SDL_FPoint -#include // Para SDL_Texture, SDL_Renderer -#include // Para size_t -#include // Para unique_ptr, shared_ptr -#include // Para vector -#include "utils.h" // Para Color +#include // Para SDL_FRect, SDL_FPoint +#include // Para SDL_Texture, SDL_Renderer +#include // Para size_t +#include // Para unique_ptr, shared_ptr +#include // Para vector +#include "utils.h" // Para Color class MovingSprite; class Sprite; diff --git a/source/balloon.h b/source/balloon.h index a94e42a..3dc0a26 100644 --- a/source/balloon.h +++ b/source/balloon.h @@ -1,7 +1,7 @@ #pragma once -#include // Para SDL_FRect -#include // Para Uint8, Uint16, Uint32 +#include // Para SDL_FRect +#include // Para Uint8, Uint16, Uint32 #include // Para shared_ptr, unique_ptr #include // Para string #include // Para vector diff --git a/source/balloon_manager.cpp b/source/balloon_manager.cpp index 92e825a..eb09fc8 100644 --- a/source/balloon_manager.cpp +++ b/source/balloon_manager.cpp @@ -203,14 +203,14 @@ void BalloonManager::createChildBalloon(const std::shared_ptr &balloon, const int CHILD_HEIGHT = BALLOON_SIZE[static_cast(balloon->getSize()) - 1]; const int Y = balloon->getPosY() + (PARENT_HEIGHT - CHILD_HEIGHT) / 2; const int X = direction == "LEFT" ? balloon->getPosX() + (balloon->getWidth() / 3) : balloon->getPosX() + 2 * (balloon->getWidth() / 3); - + // Crea el globo auto b = createBalloon(0, Y, balloon->getType(), SIZE, VX, balloon_speed_, 0); - + // Establece parametros b->alignTo(X); b->setVelY(b->getType() == BalloonType::BALLOON ? -2.50f : BALLOON_VELX_NEGATIVE * 2.0f); - + // Herencia de estados if (balloon->isStopped()) { diff --git a/source/balloon_manager.h b/source/balloon_manager.h index 8205371..19e16e8 100644 --- a/source/balloon_manager.h +++ b/source/balloon_manager.h @@ -1,6 +1,6 @@ #pragma once -#include // Para SDL_FRect +#include // Para SDL_FRect #include // Para shared_ptr, unique_ptr #include // Para string #include // Para vector @@ -60,9 +60,9 @@ public: void normalColorsToAllBalloons(); // Restaura los colores originales // Configuración de sonido - void setSounds(bool value); // Activa o desactiva los sonidos de los globos + void setSounds(bool value); // Activa o desactiva los sonidos de los globos void setBouncingSounds(bool value); // Activa o desactiva los sonidos de rebote los globos - void setPoppingSounds(bool value); // Activa o desactiva los sonidos de los globos al explotar + void setPoppingSounds(bool value); // Activa o desactiva los sonidos de los globos al explotar // Configuración de juego void setPlayArea(SDL_FRect play_area) { play_area_ = play_area; }; // Define el área de juego @@ -97,8 +97,8 @@ private: bool creation_time_enabled_ = true; bool can_deploy_balloons_ = true; bool bouncing_sound_enabled_ = false; // Si debe sonar el globo al rebotar - bool poping_sound_enabled_ = true; // Si debe sonar el globo al explotar - bool sound_enabled_ = true; // Indica si los globos deben hacer algun sonido + bool poping_sound_enabled_ = true; // Si debe sonar el globo al explotar + bool sound_enabled_ = true; // Indica si los globos deben hacer algun sonido // Metodos privados void init(); diff --git a/source/bullet.cpp b/source/bullet.cpp index 433e600..2fad48b 100644 --- a/source/bullet.cpp +++ b/source/bullet.cpp @@ -1,8 +1,8 @@ #include "bullet.h" -#include // Para SDL_FRect -#include // Para unique_ptr, make_unique, shared_ptr -#include "param.h" // Para Param, ParamGame, param -#include "sprite.h" // Para Sprite +#include // Para SDL_FRect +#include // Para unique_ptr, make_unique, shared_ptr +#include "param.h" // Para Param, ParamGame, param +#include "sprite.h" // Para Sprite #include "resource.h" class Texture; // lines 5-5 diff --git a/source/bullet.h b/source/bullet.h index 08b2506..1fd00fd 100644 --- a/source/bullet.h +++ b/source/bullet.h @@ -1,6 +1,6 @@ #pragma once -#include // Para Uint8 +#include // Para Uint8 #include // Para shared_ptr, unique_ptr #include "animated_sprite.h" // Para AnimatedSprite #include "utils.h" // Para Circle diff --git a/source/define_buttons.h b/source/define_buttons.h index 8e47b9b..2e9f04e 100644 --- a/source/define_buttons.h +++ b/source/define_buttons.h @@ -1,11 +1,11 @@ #pragma once -#include // Para SDL_Event, SDL_GamepadButtonEvent -#include // Para SDL_GamepadButton -#include // Para std::shared_ptr -#include // Para size_t -#include // Para std::string -#include // Para std::vector +#include // Para SDL_Event, SDL_GamepadButtonEvent +#include // Para SDL_GamepadButton +#include // Para std::shared_ptr +#include // Para size_t +#include // Para std::string +#include // Para std::vector // Declaraciones adelantadas class Input; diff --git a/source/director.cpp b/source/director.cpp index 5256a5b..3e41cca 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -1,11 +1,11 @@ // IWYU pragma: no_include #include "director.h" -#include // Para SDL_AudioFormat -#include // Para SDL_GetError -#include // Para SDL_GamepadButton -#include // Para SDL_Init, SDL_Quit, SDL_INIT_AUDIO +#include // Para SDL_AudioFormat +#include // Para SDL_GetError +#include // Para SDL_GamepadButton +#include // Para SDL_Init, SDL_Quit, SDL_INIT_AUDIO #include // Para SDL_Log, SDL_LogCategory, SDL_LogE... -#include // Para SDL_Scancode +#include // Para SDL_Scancode #include // Para errno, EEXIST, EACCES, ENAMETOOLONG #include // Para printf, perror #include // Para mkdir, stat, S_IRWXU @@ -562,7 +562,7 @@ void Director::runTitle() // Ejecuta la sección donde se juega al juego void Director::runGame() { - int player_id = 1; + int player_id = 1; switch (Section::options) { @@ -579,7 +579,7 @@ void Director::runGame() break; default: - player_id = 1; + player_id = 1; break; } @@ -635,7 +635,7 @@ void Director::reset() Resource::get()->reload(); } Input::get()->discoverGameControllers(); - bindInputs(); + bindInputs(); ServiceMenu::get()->reset(); Section::name = Section::Name::LOGO; } diff --git a/source/enter_name.cpp b/source/enter_name.cpp index b212275..660b684 100644 --- a/source/enter_name.cpp +++ b/source/enter_name.cpp @@ -84,7 +84,7 @@ void EnterName::decPosition() { // Si position_ es 0, aseguramos que no vaya a ser negativo y limpiamos el carácter actual. position_ = 0; - //character_index_[position_] = 0; + // character_index_[position_] = 0; } // Si position_ es menor que NAME_LENGHT, aseguramos que el overflow esté desactivado. diff --git a/source/external/gif.cpp b/source/external/gif.cpp index e42ebab..221b193 100644 --- a/source/external/gif.cpp +++ b/source/external/gif.cpp @@ -1,8 +1,8 @@ #include "gif.h" -#include // Para SDL_LogError, SDL_LogCategory, SDL_LogInfo -#include // Para memcpy, size_t -#include // Para runtime_error -#include // Para char_traits, operator==, basic_string, string +#include // Para SDL_LogError, SDL_LogCategory, SDL_LogInfo +#include // Para memcpy, size_t +#include // Para runtime_error +#include // Para char_traits, operator==, basic_string, string namespace GIF { diff --git a/source/external/jail_audio.cpp b/source/external/jail_audio.cpp index 25da986..b35ab45 100644 --- a/source/external/jail_audio.cpp +++ b/source/external/jail_audio.cpp @@ -9,99 +9,107 @@ struct JA_Sound_t { - SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 }; - Uint32 length { 0 }; - Uint8 *buffer { NULL }; + SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000}; + Uint32 length{0}; + Uint8 *buffer{NULL}; }; struct JA_Channel_t { - JA_Sound_t *sound { nullptr }; - int pos { 0 }; - int times { 0 }; - int group { 0 }; - SDL_AudioStream *stream { nullptr }; - JA_Channel_state state { JA_CHANNEL_FREE }; + JA_Sound_t *sound{nullptr}; + int pos{0}; + int times{0}; + int group{0}; + SDL_AudioStream *stream{nullptr}; + JA_Channel_state state{JA_CHANNEL_FREE}; }; struct JA_Music_t { - SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 }; - Uint32 length { 0 }; - Uint8 *buffer { nullptr }; - char *filename { nullptr }; + SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000}; + Uint32 length{0}; + Uint8 *buffer{nullptr}; + char *filename{nullptr}; - int pos { 0 }; - int times { 0 }; - SDL_AudioStream *stream { nullptr }; - JA_Music_state state { JA_MUSIC_INVALID }; + int pos{0}; + int times{0}; + SDL_AudioStream *stream{nullptr}; + JA_Music_state state{JA_MUSIC_INVALID}; }; -JA_Music_t *current_music { nullptr }; -JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; +JA_Music_t *current_music{nullptr}; +JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS]; -SDL_AudioSpec JA_audioSpec { SDL_AUDIO_S16, 2, 48000 }; -float JA_musicVolume { 1.0f }; -float JA_soundVolume[JA_MAX_GROUPS]; -bool JA_musicEnabled { true }; -bool JA_soundEnabled { true }; -SDL_AudioDeviceID sdlAudioDevice { 0 }; -SDL_TimerID JA_timerID { 0 }; +SDL_AudioSpec JA_audioSpec{SDL_AUDIO_S16, 2, 48000}; +float JA_musicVolume{1.0f}; +float JA_soundVolume[JA_MAX_GROUPS]; +bool JA_musicEnabled{true}; +bool JA_soundEnabled{true}; +SDL_AudioDeviceID sdlAudioDevice{0}; +SDL_TimerID JA_timerID{0}; bool fading = false; int fade_start_time; int fade_duration; int fade_initial_volume; - Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) { if (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING) { - if (fading) { + if (fading) + { int time = SDL_GetTicks(); - if (time > (fade_start_time+fade_duration)) { + if (time > (fade_start_time + fade_duration)) + { fading = false; JA_StopMusic(); return 30; - } else { + } + else + { const int time_passed = time - fade_start_time; const float percent = (float)time_passed / (float)fade_duration; - SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume*(1.0 - percent)); + SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume * (1.0 - percent)); } } if (current_music->times != 0) { - if ((Uint32)SDL_GetAudioStreamAvailable(current_music->stream) < (current_music->length/2)) { + if ((Uint32)SDL_GetAudioStreamAvailable(current_music->stream) < (current_music->length / 2)) + { SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length); } - if (current_music->times>0) current_music->times--; + if (current_music->times > 0) + current_music->times--; } else { - if (SDL_GetAudioStreamAvailable(current_music->stream) == 0) JA_StopMusic(); + if (SDL_GetAudioStreamAvailable(current_music->stream) == 0) + JA_StopMusic(); } } if (JA_soundEnabled) { - for (int i=0; i < JA_MAX_SIMULTANEOUS_CHANNELS; ++i) + for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; ++i) if (channels[i].state == JA_CHANNEL_PLAYING) { if (channels[i].times != 0) { - if ((Uint32)SDL_GetAudioStreamAvailable(channels[i].stream) < (channels[i].sound->length/2)) { + if ((Uint32)SDL_GetAudioStreamAvailable(channels[i].stream) < (channels[i].sound->length / 2)) + { SDL_PutAudioStreamData(channels[i].stream, channels[i].sound->buffer, channels[i].sound->length); - if (channels[i].times>0) channels[i].times--; + if (channels[i].times > 0) + channels[i].times--; } } else { - if (SDL_GetAudioStreamAvailable(channels[i].stream) == 0) JA_StopChannel(i); + if (SDL_GetAudioStreamAvailable(channels[i].stream) == 0) + JA_StopChannel(i); } } - } return 30; @@ -109,30 +117,35 @@ Uint32 JA_UpdateCallback(void *userdata, SDL_TimerID timerID, Uint32 interval) void JA_Init(const int freq, const SDL_AudioFormat format, const int num_channels) { - #ifdef DEBUG +#ifdef DEBUG SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG); - #endif - +#endif + SDL_Log("Iniciant JailAudio..."); - JA_audioSpec = {format, num_channels, freq }; - if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); + JA_audioSpec = {format, num_channels, freq}; + if (!sdlAudioDevice) + SDL_CloseAudioDevice(sdlAudioDevice); sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec); - SDL_Log( (sdlAudioDevice==0) ? "Failed to initialize SDL audio!\n" : "OK!\n"); - for (int i=0; ispec.channels = chan; music->spec.freq = samplerate; music->spec.format = SDL_AUDIO_S16; - music->buffer = (Uint8*)SDL_malloc(music->length); + music->buffer = (Uint8 *)SDL_malloc(music->length); SDL_memcpy(music->buffer, output, music->length); free(output); music->pos = 0; @@ -152,19 +165,20 @@ JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length) return music; } -JA_Music_t *JA_LoadMusic(const char* filename) +JA_Music_t *JA_LoadMusic(const char *filename) { // [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid. FILE *f = fopen(filename, "rb"); fseek(f, 0, SEEK_END); long fsize = ftell(f); fseek(f, 0, SEEK_SET); - Uint8 *buffer = (Uint8*)malloc(fsize + 1); - if (fread(buffer, fsize, 1, f)!=1) return NULL; + Uint8 *buffer = (Uint8 *)malloc(fsize + 1); + if (fread(buffer, fsize, 1, f) != 1) + return NULL; fclose(f); JA_Music_t *music = JA_LoadMusic(buffer, fsize); - music->filename = (char*)malloc(strlen(filename)+1); + music->filename = (char *)malloc(strlen(filename) + 1); strcpy(music->filename, filename); free(buffer); @@ -174,7 +188,8 @@ JA_Music_t *JA_LoadMusic(const char* filename) void JA_PlayMusic(JA_Music_t *music, const int loop) { - if (!JA_musicEnabled) return; + if (!JA_musicEnabled) + return; JA_StopMusic(); @@ -184,46 +199,55 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) current_music->times = loop; current_music->stream = SDL_CreateAudioStream(¤t_music->spec, &JA_audioSpec); - if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n"); + if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) + printf("[ERROR] SDL_PutAudioStreamData failed!\n"); SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); - if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); - //SDL_ResumeAudioStreamDevice(current_music->stream); + if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) + printf("[ERROR] SDL_BindAudioStream failed!\n"); + // SDL_ResumeAudioStreamDevice(current_music->stream); } char *JA_GetMusicFilename(JA_Music_t *music) { - if (!music) music = current_music; + if (!music) + music = current_music; return music->filename; } void JA_PauseMusic() { - if (!JA_musicEnabled) return; - if (!current_music || current_music->state == JA_MUSIC_INVALID) return; + if (!JA_musicEnabled) + return; + if (!current_music || current_music->state == JA_MUSIC_INVALID) + return; current_music->state = JA_MUSIC_PAUSED; - //SDL_PauseAudioStreamDevice(current_music->stream); + // SDL_PauseAudioStreamDevice(current_music->stream); SDL_UnbindAudioStream(current_music->stream); } void JA_ResumeMusic() { - if (!JA_musicEnabled) return; - if (!current_music || current_music->state == JA_MUSIC_INVALID) return; + if (!JA_musicEnabled) + return; + if (!current_music || current_music->state == JA_MUSIC_INVALID) + return; current_music->state = JA_MUSIC_PLAYING; - //SDL_ResumeAudioStreamDevice(current_music->stream); + // SDL_ResumeAudioStreamDevice(current_music->stream); SDL_BindAudioStream(sdlAudioDevice, current_music->stream); } void JA_StopMusic() { - if (!JA_musicEnabled) return; - if (!current_music || current_music->state == JA_MUSIC_INVALID) return; + if (!JA_musicEnabled) + return; + if (!current_music || current_music->state == JA_MUSIC_INVALID) + return; current_music->pos = 0; current_music->state = JA_MUSIC_STOPPED; - //SDL_PauseAudioStreamDevice(current_music->stream); + // SDL_PauseAudioStreamDevice(current_music->stream); SDL_DestroyAudioStream(current_music->stream); current_music->stream = nullptr; free(current_music->filename); @@ -232,8 +256,10 @@ void JA_StopMusic() void JA_FadeOutMusic(const int milliseconds) { - if (!JA_musicEnabled) return; - if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return; + if (!JA_musicEnabled) + return; + if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) + return; fading = true; fade_start_time = SDL_GetTicks(); @@ -243,51 +269,55 @@ void JA_FadeOutMusic(const int milliseconds) JA_Music_state JA_GetMusicState() { - if (!JA_musicEnabled) return JA_MUSIC_DISABLED; - if (!current_music) return JA_MUSIC_INVALID; + if (!JA_musicEnabled) + return JA_MUSIC_DISABLED; + if (!current_music) + return JA_MUSIC_INVALID; return current_music->state; } void JA_DeleteMusic(JA_Music_t *music) { - if (current_music == music) current_music = nullptr; + if (current_music == music) + current_music = nullptr; SDL_free(music->buffer); - if (music->stream) SDL_DestroyAudioStream(music->stream); + if (music->stream) + SDL_DestroyAudioStream(music->stream); delete music; } float JA_SetMusicVolume(float volume) { - JA_musicVolume = SDL_clamp( volume, 0.0f, 1.0f ); - if (current_music) SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); + JA_musicVolume = SDL_clamp(volume, 0.0f, 1.0f); + if (current_music) + SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); return JA_musicVolume; } void JA_SetMusicPosition(float value) { - if (!current_music) return; + if (!current_music) + return; current_music->pos = value * current_music->spec.freq; } float JA_GetMusicPosition() { - if (!current_music) return 0; - return float(current_music->pos)/float(current_music->spec.freq); + if (!current_music) + return 0; + return float(current_music->pos) / float(current_music->spec.freq); } void JA_EnableMusic(const bool value) { - if ( !value && current_music && (current_music->state==JA_MUSIC_PLAYING) ) JA_StopMusic(); + if (!value && current_music && (current_music->state == JA_MUSIC_PLAYING)) + JA_StopMusic(); JA_musicEnabled = value; } - - - - -JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) +JA_Sound_t *JA_NewSound(Uint8 *buffer, Uint32 length) { JA_Sound_t *sound = new JA_Sound_t(); sound->buffer = buffer; @@ -295,15 +325,15 @@ JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) return sound; } -JA_Sound_t *JA_LoadSound(uint8_t* buffer, uint32_t size) +JA_Sound_t *JA_LoadSound(uint8_t *buffer, uint32_t size) { JA_Sound_t *sound = new JA_Sound_t(); - SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size),1, &sound->spec, &sound->buffer, &sound->length); + SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size), 1, &sound->spec, &sound->buffer, &sound->length); return sound; } -JA_Sound_t *JA_LoadSound(const char* filename) +JA_Sound_t *JA_LoadSound(const char *filename) { JA_Sound_t *sound = new JA_Sound_t(); SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length); @@ -313,11 +343,16 @@ JA_Sound_t *JA_LoadSound(const char* filename) int JA_PlaySound(JA_Sound_t *sound, const int loop, const int group) { - if (!JA_soundEnabled) return -1; + if (!JA_soundEnabled) + return -1; int channel = 0; - while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; } - if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0; + while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) + { + channel++; + } + if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) + channel = 0; JA_StopChannel(channel); channels[channel].sound = sound; @@ -334,9 +369,11 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop, const int group) int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop, const int group) { - if (!JA_soundEnabled) return -1; + if (!JA_soundEnabled) + return -1; - if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1; + if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) + return -1; JA_StopChannel(channel); channels[channel].sound = sound; @@ -353,8 +390,10 @@ int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop, void JA_DeleteSound(JA_Sound_t *sound) { - for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { - if (channels[i].sound == sound) JA_StopChannel(i); + for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) + { + if (channels[i].sound == sound) + JA_StopChannel(i); } SDL_free(sound->buffer); delete sound; @@ -362,7 +401,8 @@ void JA_DeleteSound(JA_Sound_t *sound) void JA_PauseChannel(const int channel) { - if (!JA_soundEnabled) return; + if (!JA_soundEnabled) + return; if (channel == -1) { @@ -370,7 +410,7 @@ void JA_PauseChannel(const int channel) if (channels[i].state == JA_CHANNEL_PLAYING) { channels[i].state = JA_CHANNEL_PAUSED; - //SDL_PauseAudioStreamDevice(channels[i].stream); + // SDL_PauseAudioStreamDevice(channels[i].stream); SDL_UnbindAudioStream(channels[i].stream); } } @@ -379,7 +419,7 @@ void JA_PauseChannel(const int channel) if (channels[channel].state == JA_CHANNEL_PLAYING) { channels[channel].state = JA_CHANNEL_PAUSED; - //SDL_PauseAudioStreamDevice(channels[channel].stream); + // SDL_PauseAudioStreamDevice(channels[channel].stream); SDL_UnbindAudioStream(channels[channel].stream); } } @@ -387,7 +427,8 @@ void JA_PauseChannel(const int channel) void JA_ResumeChannel(const int channel) { - if (!JA_soundEnabled) return; + if (!JA_soundEnabled) + return; if (channel == -1) { @@ -395,7 +436,7 @@ void JA_ResumeChannel(const int channel) if (channels[i].state == JA_CHANNEL_PAUSED) { channels[i].state = JA_CHANNEL_PLAYING; - //SDL_ResumeAudioStreamDevice(channels[i].stream); + // SDL_ResumeAudioStreamDevice(channels[i].stream); SDL_BindAudioStream(sdlAudioDevice, channels[i].stream); } } @@ -404,7 +445,7 @@ void JA_ResumeChannel(const int channel) if (channels[channel].state == JA_CHANNEL_PAUSED) { channels[channel].state = JA_CHANNEL_PLAYING; - //SDL_ResumeAudioStreamDevice(channels[channel].stream); + // SDL_ResumeAudioStreamDevice(channels[channel].stream); SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream); } } @@ -412,12 +453,15 @@ void JA_ResumeChannel(const int channel) void JA_StopChannel(const int channel) { - if (!JA_soundEnabled) return; + if (!JA_soundEnabled) + return; if (channel == -1) { - for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { - if (channels[i].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[i].stream); + for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) + { + if (channels[i].state != JA_CHANNEL_FREE) + SDL_DestroyAudioStream(channels[i].stream); channels[i].stream = nullptr; channels[i].state = JA_CHANNEL_FREE; channels[i].pos = 0; @@ -426,7 +470,8 @@ void JA_StopChannel(const int channel) } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) { - if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream); + if (channels[channel].state != JA_CHANNEL_FREE) + SDL_DestroyAudioStream(channels[channel].stream); channels[channel].stream = nullptr; channels[channel].state = JA_CHANNEL_FREE; channels[channel].pos = 0; @@ -436,23 +481,27 @@ void JA_StopChannel(const int channel) JA_Channel_state JA_GetChannelState(const int channel) { - if (!JA_soundEnabled) return JA_SOUND_DISABLED; + if (!JA_soundEnabled) + return JA_SOUND_DISABLED; - if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID; + if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) + return JA_CHANNEL_INVALID; return channels[channel].state; } float JA_SetSoundVolume(float volume, const int group) { - const float v = SDL_clamp( volume, 0.0f, 1.0f ); - for (int i = 0; i < JA_MAX_GROUPS; ++i) { - if (group==-1 || group==i) JA_soundVolume[i]=v; + const float v = SDL_clamp(volume, 0.0f, 1.0f); + for (int i = 0; i < JA_MAX_GROUPS; ++i) + { + if (group == -1 || group == i) + JA_soundVolume[i] = v; } for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) - if ( ((channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED)) && - ((group==-1) || (channels[i].group==group)) ) + if (((channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED)) && + ((group == -1) || (channels[i].group == group))) SDL_SetAudioStreamGain(channels[i].stream, JA_soundVolume[i]); return v; @@ -462,7 +511,8 @@ void JA_EnableSound(const bool value) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { - if (channels[i].state == JA_CHANNEL_PLAYING) JA_StopChannel(i); + if (channels[i].state == JA_CHANNEL_PLAYING) + JA_StopChannel(i); } JA_soundEnabled = value; } diff --git a/source/external/jail_audio.h b/source/external/jail_audio.h index 035b5cb..7bbb82f 100644 --- a/source/external/jail_audio.h +++ b/source/external/jail_audio.h @@ -1,8 +1,22 @@ #pragma once #include -enum JA_Channel_state { JA_CHANNEL_INVALID, JA_CHANNEL_FREE, JA_CHANNEL_PLAYING, JA_CHANNEL_PAUSED, JA_SOUND_DISABLED }; -enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED, JA_MUSIC_DISABLED }; +enum JA_Channel_state +{ + JA_CHANNEL_INVALID, + JA_CHANNEL_FREE, + JA_CHANNEL_PLAYING, + JA_CHANNEL_PAUSED, + JA_SOUND_DISABLED +}; +enum JA_Music_state +{ + JA_MUSIC_INVALID, + JA_MUSIC_PLAYING, + JA_MUSIC_PAUSED, + JA_MUSIC_STOPPED, + JA_MUSIC_DISABLED +}; struct JA_Sound_t; struct JA_Music_t; @@ -10,8 +24,8 @@ struct JA_Music_t; void JA_Init(const int freq, const SDL_AudioFormat format, const int num_channels); void JA_Quit(); -JA_Music_t *JA_LoadMusic(const char* filename); -JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length); +JA_Music_t *JA_LoadMusic(const char *filename); +JA_Music_t *JA_LoadMusic(Uint8 *buffer, Uint32 length); void JA_PlayMusic(JA_Music_t *music, const int loop = -1); char *JA_GetMusicFilename(JA_Music_t *music = nullptr); void JA_PauseMusic(); @@ -25,17 +39,17 @@ void JA_SetMusicPosition(float value); float JA_GetMusicPosition(); void JA_EnableMusic(const bool value); -JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length); -JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length); -JA_Sound_t *JA_LoadSound(const char* filename); -int JA_PlaySound(JA_Sound_t *sound, const int loop = 0, const int group=0); -int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0, const int group=0); +JA_Sound_t *JA_NewSound(Uint8 *buffer, Uint32 length); +JA_Sound_t *JA_LoadSound(Uint8 *buffer, Uint32 length); +JA_Sound_t *JA_LoadSound(const char *filename); +int JA_PlaySound(JA_Sound_t *sound, const int loop = 0, const int group = 0); +int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0, const int group = 0); void JA_PauseChannel(const int channel); void JA_ResumeChannel(const int channel); void JA_StopChannel(const int channel); JA_Channel_state JA_GetChannelState(const int channel); void JA_DeleteSound(JA_Sound_t *sound); -float JA_SetSoundVolume(float volume, const int group=0); +float JA_SetSoundVolume(float volume, const int group = 0); void JA_EnableSound(const bool value); float JA_SetVolume(float volume); diff --git a/source/external/jail_shader.cpp b/source/external/jail_shader.cpp index 0ef0998..41527e8 100644 --- a/source/external/jail_shader.cpp +++ b/source/external/jail_shader.cpp @@ -1,9 +1,9 @@ #include "jail_shader.h" -#include // Para SDL_LogCategory, SDL_LogError, SDL_LogWarn -#include // Para SDL_FPoint, SDL_Point -#include // Para strncmp -#include // Para runtime_error -#include // Para vector +#include // Para SDL_LogCategory, SDL_LogError, SDL_LogWarn +#include // Para SDL_FPoint, SDL_Point +#include // Para strncmp +#include // Para runtime_error +#include // Para vector #ifdef __APPLE__ #include "CoreFoundation/CoreFoundation.h" // Para Core Foundation en macOS @@ -75,13 +75,13 @@ namespace shader #endif // Función para verificar errores de OpenGL - void checkGLError(const char* operation) + void checkGLError(const char *operation) { GLenum error = glGetError(); if (error != GL_NO_ERROR) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Error OpenGL en %s: 0x%x", operation, error); + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Error OpenGL en %s: 0x%x", operation, error); } } @@ -160,7 +160,7 @@ namespace shader checkGLError("glAttachShader vertex"); glAttachShader(program_id, fragment_shader_id); checkGLError("glAttachShader fragment"); - + glLinkProgram(program_id); checkGLError("glLinkProgram"); @@ -218,34 +218,38 @@ namespace shader } // Función para obtener el ID de textura OpenGL desde SDL3 - GLuint getTextureID(SDL_Texture* texture) + GLuint getTextureID(SDL_Texture *texture) { - if (!texture) return DEFAULT_TEXTURE_ID; - + if (!texture) + return DEFAULT_TEXTURE_ID; + // Intentar obtener el ID de textura OpenGL desde las propiedades de SDL3 SDL_PropertiesID props = SDL_GetTextureProperties(texture); GLuint textureId = 0; - + // Intentar diferentes nombres de propiedades según la versión de SDL3 textureId = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "SDL.texture.opengl.texture", nullptr); - + // Si la primera no funciona, intentar con el nombre alternativo - if (textureId == 0) { + if (textureId == 0) + { textureId = (GLuint)(uintptr_t)SDL_GetPointerProperty(props, "texture.opengl.texture", nullptr); } - + // Si aún no funciona, intentar obtener como número - if (textureId == 0) { + if (textureId == 0) + { textureId = (GLuint)SDL_GetNumberProperty(props, "SDL.texture.opengl.texture", DEFAULT_TEXTURE_ID); } - + // Si ninguna funciona, usar el método manual de bindeo de textura - if (textureId == 0) { - SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, - "No se pudo obtener el ID de textura OpenGL, usando ID por defecto (%d)", DEFAULT_TEXTURE_ID); + if (textureId == 0) + { + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "No se pudo obtener el ID de textura OpenGL, usando ID por defecto (%d)", DEFAULT_TEXTURE_ID); textureId = DEFAULT_TEXTURE_ID; } - + return textureId; } @@ -254,7 +258,7 @@ namespace shader shader::win = window; shader::renderer = SDL_GetRenderer(window); shader::backBuffer = back_buffer_texture; - + if (!shader::renderer) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo obtener el renderer de la ventana."); @@ -297,7 +301,7 @@ namespace shader usingOpenGL = false; return false; } - + usingOpenGL = true; SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Shader system initialized successfully."); return true; @@ -315,10 +319,10 @@ namespace shader // Guardar estados de OpenGL GLint oldProgramId; glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgramId); - + GLint oldViewport[4]; glGetIntegerv(GL_VIEWPORT, oldViewport); - + GLboolean wasTextureEnabled = glIsEnabled(GL_TEXTURE_2D); GLint oldTextureId; glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTextureId); @@ -435,7 +439,7 @@ namespace shader programId = INVALID_PROGRAM_ID; SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Programa de shaders liberado."); } - + // Reinicializar variables win = nullptr; renderer = nullptr; diff --git a/source/external/jail_shader.h b/source/external/jail_shader.h index 8182f42..e725cb1 100644 --- a/source/external/jail_shader.h +++ b/source/external/jail_shader.h @@ -1,7 +1,7 @@ #pragma once -#include // Para SDL_Texture -#include // Para SDL_Window +#include // Para SDL_Texture +#include // Para SDL_Window #include namespace shader diff --git a/source/external/stb_vorbis.h b/source/external/stb_vorbis.h index 49df433..f372cc9 100644 --- a/source/external/stb_vorbis.h +++ b/source/external/stb_vorbis.h @@ -62,7 +62,6 @@ // // See end of file for full version history. - ////////////////////////////////////////////////////////////////////////////// // // HEADER BEGINS HERE @@ -80,148 +79,147 @@ #endif #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -/////////// THREAD SAFETY + /////////// THREAD SAFETY -// Individual stb_vorbis* handles are not thread-safe; you cannot decode from -// them from multiple threads at the same time. However, you can have multiple -// stb_vorbis* handles and decode from them independently in multiple thrads. + // Individual stb_vorbis* handles are not thread-safe; you cannot decode from + // them from multiple threads at the same time. However, you can have multiple + // stb_vorbis* handles and decode from them independently in multiple thrads. + /////////// MEMORY ALLOCATION -/////////// MEMORY ALLOCATION + // normally stb_vorbis uses malloc() to allocate memory at startup, + // and alloca() to allocate temporary memory during a frame on the + // stack. (Memory consumption will depend on the amount of setup + // data in the file and how you set the compile flags for speed + // vs. size. In my test files the maximal-size usage is ~150KB.) + // + // You can modify the wrapper functions in the source (setup_malloc, + // setup_temp_malloc, temp_malloc) to change this behavior, or you + // can use a simpler allocation model: you pass in a buffer from + // which stb_vorbis will allocate _all_ its memory (including the + // temp memory). "open" may fail with a VORBIS_outofmem if you + // do not pass in enough data; there is no way to determine how + // much you do need except to succeed (at which point you can + // query get_info to find the exact amount required. yes I know + // this is lame). + // + // If you pass in a non-NULL buffer of the type below, allocation + // will occur from it as described above. Otherwise just pass NULL + // to use malloc()/alloca() -// normally stb_vorbis uses malloc() to allocate memory at startup, -// and alloca() to allocate temporary memory during a frame on the -// stack. (Memory consumption will depend on the amount of setup -// data in the file and how you set the compile flags for speed -// vs. size. In my test files the maximal-size usage is ~150KB.) -// -// You can modify the wrapper functions in the source (setup_malloc, -// setup_temp_malloc, temp_malloc) to change this behavior, or you -// can use a simpler allocation model: you pass in a buffer from -// which stb_vorbis will allocate _all_ its memory (including the -// temp memory). "open" may fail with a VORBIS_outofmem if you -// do not pass in enough data; there is no way to determine how -// much you do need except to succeed (at which point you can -// query get_info to find the exact amount required. yes I know -// this is lame). -// -// If you pass in a non-NULL buffer of the type below, allocation -// will occur from it as described above. Otherwise just pass NULL -// to use malloc()/alloca() + typedef struct + { + char *alloc_buffer; + int alloc_buffer_length_in_bytes; + } stb_vorbis_alloc; -typedef struct -{ - char *alloc_buffer; - int alloc_buffer_length_in_bytes; -} stb_vorbis_alloc; + /////////// FUNCTIONS USEABLE WITH ALL INPUT MODES + typedef struct stb_vorbis stb_vorbis; -/////////// FUNCTIONS USEABLE WITH ALL INPUT MODES + typedef struct + { + unsigned int sample_rate; + int channels; -typedef struct stb_vorbis stb_vorbis; + unsigned int setup_memory_required; + unsigned int setup_temp_memory_required; + unsigned int temp_memory_required; -typedef struct -{ - unsigned int sample_rate; - int channels; + int max_frame_size; + } stb_vorbis_info; - unsigned int setup_memory_required; - unsigned int setup_temp_memory_required; - unsigned int temp_memory_required; + typedef struct + { + char *vendor; - int max_frame_size; -} stb_vorbis_info; + int comment_list_length; + char **comment_list; + } stb_vorbis_comment; -typedef struct -{ - char *vendor; + // get general information about the file + extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f); - int comment_list_length; - char **comment_list; -} stb_vorbis_comment; + // get ogg comments + extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f); -// get general information about the file -extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f); + // get the last error detected (clears it, too) + extern int stb_vorbis_get_error(stb_vorbis *f); -// get ogg comments -extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f); + // close an ogg vorbis file and free all memory in use + extern void stb_vorbis_close(stb_vorbis *f); -// get the last error detected (clears it, too) -extern int stb_vorbis_get_error(stb_vorbis *f); + // this function returns the offset (in samples) from the beginning of the + // file that will be returned by the next decode, if it is known, or -1 + // otherwise. after a flush_pushdata() call, this may take a while before + // it becomes valid again. + // NOT WORKING YET after a seek with PULLDATA API + extern int stb_vorbis_get_sample_offset(stb_vorbis *f); -// close an ogg vorbis file and free all memory in use -extern void stb_vorbis_close(stb_vorbis *f); + // returns the current seek point within the file, or offset from the beginning + // of the memory buffer. In pushdata mode it returns 0. + extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f); -// this function returns the offset (in samples) from the beginning of the -// file that will be returned by the next decode, if it is known, or -1 -// otherwise. after a flush_pushdata() call, this may take a while before -// it becomes valid again. -// NOT WORKING YET after a seek with PULLDATA API -extern int stb_vorbis_get_sample_offset(stb_vorbis *f); - -// returns the current seek point within the file, or offset from the beginning -// of the memory buffer. In pushdata mode it returns 0. -extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f); - -/////////// PUSHDATA API + /////////// PUSHDATA API #ifndef STB_VORBIS_NO_PUSHDATA_API -// this API allows you to get blocks of data from any source and hand -// them to stb_vorbis. you have to buffer them; stb_vorbis will tell -// you how much it used, and you have to give it the rest next time; -// and stb_vorbis may not have enough data to work with and you will -// need to give it the same data again PLUS more. Note that the Vorbis -// specification does not bound the size of an individual frame. + // this API allows you to get blocks of data from any source and hand + // them to stb_vorbis. you have to buffer them; stb_vorbis will tell + // you how much it used, and you have to give it the rest next time; + // and stb_vorbis may not have enough data to work with and you will + // need to give it the same data again PLUS more. Note that the Vorbis + // specification does not bound the size of an individual frame. -extern stb_vorbis *stb_vorbis_open_pushdata( - const unsigned char * datablock, int datablock_length_in_bytes, - int *datablock_memory_consumed_in_bytes, - int *error, - const stb_vorbis_alloc *alloc_buffer); -// create a vorbis decoder by passing in the initial data block containing -// the ogg&vorbis headers (you don't need to do parse them, just provide -// the first N bytes of the file--you're told if it's not enough, see below) -// on success, returns an stb_vorbis *, does not set error, returns the amount of -// data parsed/consumed on this call in *datablock_memory_consumed_in_bytes; -// on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed -// if returns NULL and *error is VORBIS_need_more_data, then the input block was -// incomplete and you need to pass in a larger block from the start of the file + extern stb_vorbis *stb_vorbis_open_pushdata( + const unsigned char *datablock, int datablock_length_in_bytes, + int *datablock_memory_consumed_in_bytes, + int *error, + const stb_vorbis_alloc *alloc_buffer); + // create a vorbis decoder by passing in the initial data block containing + // the ogg&vorbis headers (you don't need to do parse them, just provide + // the first N bytes of the file--you're told if it's not enough, see below) + // on success, returns an stb_vorbis *, does not set error, returns the amount of + // data parsed/consumed on this call in *datablock_memory_consumed_in_bytes; + // on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed + // if returns NULL and *error is VORBIS_need_more_data, then the input block was + // incomplete and you need to pass in a larger block from the start of the file -extern int stb_vorbis_decode_frame_pushdata( - stb_vorbis *f, - const unsigned char *datablock, int datablock_length_in_bytes, - int *channels, // place to write number of float * buffers - float ***output, // place to write float ** array of float * buffers - int *samples // place to write number of output samples - ); -// decode a frame of audio sample data if possible from the passed-in data block -// -// return value: number of bytes we used from datablock -// -// possible cases: -// 0 bytes used, 0 samples output (need more data) -// N bytes used, 0 samples output (resynching the stream, keep going) -// N bytes used, M samples output (one frame of data) -// note that after opening a file, you will ALWAYS get one N-bytes,0-sample -// frame, because Vorbis always "discards" the first frame. -// -// Note that on resynch, stb_vorbis will rarely consume all of the buffer, -// instead only datablock_length_in_bytes-3 or less. This is because it wants -// to avoid missing parts of a page header if they cross a datablock boundary, -// without writing state-machiney code to record a partial detection. -// -// The number of channels returned are stored in *channels (which can be -// NULL--it is always the same as the number of channels reported by -// get_info). *output will contain an array of float* buffers, one per -// channel. In other words, (*output)[0][0] contains the first sample from -// the first channel, and (*output)[1][0] contains the first sample from -// the second channel. + extern int stb_vorbis_decode_frame_pushdata( + stb_vorbis *f, + const unsigned char *datablock, int datablock_length_in_bytes, + int *channels, // place to write number of float * buffers + float ***output, // place to write float ** array of float * buffers + int *samples // place to write number of output samples + ); + // decode a frame of audio sample data if possible from the passed-in data block + // + // return value: number of bytes we used from datablock + // + // possible cases: + // 0 bytes used, 0 samples output (need more data) + // N bytes used, 0 samples output (resynching the stream, keep going) + // N bytes used, M samples output (one frame of data) + // note that after opening a file, you will ALWAYS get one N-bytes,0-sample + // frame, because Vorbis always "discards" the first frame. + // + // Note that on resynch, stb_vorbis will rarely consume all of the buffer, + // instead only datablock_length_in_bytes-3 or less. This is because it wants + // to avoid missing parts of a page header if they cross a datablock boundary, + // without writing state-machiney code to record a partial detection. + // + // The number of channels returned are stored in *channels (which can be + // NULL--it is always the same as the number of channels reported by + // get_info). *output will contain an array of float* buffers, one per + // channel. In other words, (*output)[0][0] contains the first sample from + // the first channel, and (*output)[1][0] contains the first sample from + // the second channel. -extern void stb_vorbis_flush_pushdata(stb_vorbis *f); + extern void stb_vorbis_flush_pushdata(stb_vorbis *f); // inform stb_vorbis that your next datablock will not be contiguous with // previous ones (e.g. you've seeked in the data); future attempts to decode // frames will cause stb_vorbis to resynchronize (as noted above), and @@ -234,51 +232,50 @@ extern void stb_vorbis_flush_pushdata(stb_vorbis *f); // if you don't like the result, seek your file again and repeat. #endif - -////////// PULLING INPUT API + ////////// PULLING INPUT API #ifndef STB_VORBIS_NO_PULLDATA_API -// This API assumes stb_vorbis is allowed to pull data from a source-- -// either a block of memory containing the _entire_ vorbis stream, or a -// FILE * that you or it create, or possibly some other reading mechanism -// if you go modify the source to replace the FILE * case with some kind -// of callback to your code. (But if you don't support seeking, you may -// just want to go ahead and use pushdata.) + // This API assumes stb_vorbis is allowed to pull data from a source-- + // either a block of memory containing the _entire_ vorbis stream, or a + // FILE * that you or it create, or possibly some other reading mechanism + // if you go modify the source to replace the FILE * case with some kind + // of callback to your code. (But if you don't support seeking, you may + // just want to go ahead and use pushdata.) #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION) -extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output); + extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output); #endif #if !defined(STB_VORBIS_NO_INTEGER_CONVERSION) -extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output); + extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output); #endif -// decode an entire file and output the data interleaved into a malloc()ed -// buffer stored in *output. The return value is the number of samples -// decoded, or -1 if the file could not be opened or was not an ogg vorbis file. -// When you're done with it, just free() the pointer returned in *output. + // decode an entire file and output the data interleaved into a malloc()ed + // buffer stored in *output. The return value is the number of samples + // decoded, or -1 if the file could not be opened or was not an ogg vorbis file. + // When you're done with it, just free() the pointer returned in *output. -extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, - int *error, const stb_vorbis_alloc *alloc_buffer); -// create an ogg vorbis decoder from an ogg vorbis stream in memory (note -// this must be the entire stream!). on failure, returns NULL and sets *error + extern stb_vorbis *stb_vorbis_open_memory(const unsigned char *data, int len, + int *error, const stb_vorbis_alloc *alloc_buffer); + // create an ogg vorbis decoder from an ogg vorbis stream in memory (note + // this must be the entire stream!). on failure, returns NULL and sets *error #ifndef STB_VORBIS_NO_STDIO -extern stb_vorbis * stb_vorbis_open_filename(const char *filename, - int *error, const stb_vorbis_alloc *alloc_buffer); -// create an ogg vorbis decoder from a filename via fopen(). on failure, -// returns NULL and sets *error (possibly to VORBIS_file_open_failure). + extern stb_vorbis *stb_vorbis_open_filename(const char *filename, + int *error, const stb_vorbis_alloc *alloc_buffer); + // create an ogg vorbis decoder from a filename via fopen(). on failure, + // returns NULL and sets *error (possibly to VORBIS_file_open_failure). -extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close, - int *error, const stb_vorbis_alloc *alloc_buffer); -// create an ogg vorbis decoder from an open FILE *, looking for a stream at -// the _current_ seek point (ftell). on failure, returns NULL and sets *error. -// note that stb_vorbis must "own" this stream; if you seek it in between -// calls to stb_vorbis, it will become confused. Moreover, if you attempt to -// perform stb_vorbis_seek_*() operations on this file, it will assume it -// owns the _entire_ rest of the file after the start point. Use the next -// function, stb_vorbis_open_file_section(), to limit it. + extern stb_vorbis *stb_vorbis_open_file(FILE *f, int close_handle_on_close, + int *error, const stb_vorbis_alloc *alloc_buffer); + // create an ogg vorbis decoder from an open FILE *, looking for a stream at + // the _current_ seek point (ftell). on failure, returns NULL and sets *error. + // note that stb_vorbis must "own" this stream; if you seek it in between + // calls to stb_vorbis, it will become confused. Moreover, if you attempt to + // perform stb_vorbis_seek_*() operations on this file, it will assume it + // owns the _entire_ rest of the file after the start point. Use the next + // function, stb_vorbis_open_file_section(), to limit it. -extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close, - int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len); + extern stb_vorbis *stb_vorbis_open_file_section(FILE *f, int close_handle_on_close, + int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len); // create an ogg vorbis decoder from an open FILE *, looking for a stream at // the _current_ seek point (ftell); the stream will be of length 'len' bytes. // on failure, returns NULL and sets *error. note that stb_vorbis must "own" @@ -286,117 +283,116 @@ extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_cl // confused. #endif -extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number); -extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number); -// these functions seek in the Vorbis file to (approximately) 'sample_number'. -// after calling seek_frame(), the next call to get_frame_*() will include -// the specified sample. after calling stb_vorbis_seek(), the next call to -// stb_vorbis_get_samples_* will start with the specified sample. If you -// do not need to seek to EXACTLY the target sample when using get_samples_*, -// you can also use seek_frame(). + extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number); + extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number); + // these functions seek in the Vorbis file to (approximately) 'sample_number'. + // after calling seek_frame(), the next call to get_frame_*() will include + // the specified sample. after calling stb_vorbis_seek(), the next call to + // stb_vorbis_get_samples_* will start with the specified sample. If you + // do not need to seek to EXACTLY the target sample when using get_samples_*, + // you can also use seek_frame(). -extern int stb_vorbis_seek_start(stb_vorbis *f); -// this function is equivalent to stb_vorbis_seek(f,0) + extern int stb_vorbis_seek_start(stb_vorbis *f); + // this function is equivalent to stb_vorbis_seek(f,0) -extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f); -extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f); -// these functions return the total length of the vorbis stream + extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f); + extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f); + // these functions return the total length of the vorbis stream -extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output); -// decode the next frame and return the number of samples. the number of -// channels returned are stored in *channels (which can be NULL--it is always -// the same as the number of channels reported by get_info). *output will -// contain an array of float* buffers, one per channel. These outputs will -// be overwritten on the next call to stb_vorbis_get_frame_*. -// -// You generally should not intermix calls to stb_vorbis_get_frame_*() -// and stb_vorbis_get_samples_*(), since the latter calls the former. + extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output); + // decode the next frame and return the number of samples. the number of + // channels returned are stored in *channels (which can be NULL--it is always + // the same as the number of channels reported by get_info). *output will + // contain an array of float* buffers, one per channel. These outputs will + // be overwritten on the next call to stb_vorbis_get_frame_*. + // + // You generally should not intermix calls to stb_vorbis_get_frame_*() + // and stb_vorbis_get_samples_*(), since the latter calls the former. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION -extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts); -extern int stb_vorbis_get_frame_short (stb_vorbis *f, int num_c, short **buffer, int num_samples); + extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts); + extern int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples); #endif -// decode the next frame and return the number of *samples* per channel. -// Note that for interleaved data, you pass in the number of shorts (the -// size of your array), but the return value is the number of samples per -// channel, not the total number of samples. -// -// The data is coerced to the number of channels you request according to the -// channel coercion rules (see below). You must pass in the size of your -// buffer(s) so that stb_vorbis will not overwrite the end of the buffer. -// The maximum buffer size needed can be gotten from get_info(); however, -// the Vorbis I specification implies an absolute maximum of 4096 samples -// per channel. + // decode the next frame and return the number of *samples* per channel. + // Note that for interleaved data, you pass in the number of shorts (the + // size of your array), but the return value is the number of samples per + // channel, not the total number of samples. + // + // The data is coerced to the number of channels you request according to the + // channel coercion rules (see below). You must pass in the size of your + // buffer(s) so that stb_vorbis will not overwrite the end of the buffer. + // The maximum buffer size needed can be gotten from get_info(); however, + // the Vorbis I specification implies an absolute maximum of 4096 samples + // per channel. -// Channel coercion rules: -// Let M be the number of channels requested, and N the number of channels present, -// and Cn be the nth channel; let stereo L be the sum of all L and center channels, -// and stereo R be the sum of all R and center channels (channel assignment from the -// vorbis spec). -// M N output -// 1 k sum(Ck) for all k -// 2 * stereo L, stereo R -// k l k > l, the first l channels, then 0s -// k l k <= l, the first k channels -// Note that this is not _good_ surround etc. mixing at all! It's just so -// you get something useful. + // Channel coercion rules: + // Let M be the number of channels requested, and N the number of channels present, + // and Cn be the nth channel; let stereo L be the sum of all L and center channels, + // and stereo R be the sum of all R and center channels (channel assignment from the + // vorbis spec). + // M N output + // 1 k sum(Ck) for all k + // 2 * stereo L, stereo R + // k l k > l, the first l channels, then 0s + // k l k <= l, the first k channels + // Note that this is not _good_ surround etc. mixing at all! It's just so + // you get something useful. -extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats); -extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples); -// gets num_samples samples, not necessarily on a frame boundary--this requires -// buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES. -// Returns the number of samples stored per channel; it may be less than requested -// at the end of the file. If there are no more samples in the file, returns 0. + extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats); + extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples); + // gets num_samples samples, not necessarily on a frame boundary--this requires + // buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES. + // Returns the number of samples stored per channel; it may be less than requested + // at the end of the file. If there are no more samples in the file, returns 0. #ifndef STB_VORBIS_NO_INTEGER_CONVERSION -extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts); -extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples); + extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts); + extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples); #endif -// gets num_samples samples, not necessarily on a frame boundary--this requires -// buffering so you have to supply the buffers. Applies the coercion rules above -// to produce 'channels' channels. Returns the number of samples stored per channel; -// it may be less than requested at the end of the file. If there are no more -// samples in the file, returns 0. + // gets num_samples samples, not necessarily on a frame boundary--this requires + // buffering so you have to supply the buffers. Applies the coercion rules above + // to produce 'channels' channels. Returns the number of samples stored per channel; + // it may be less than requested at the end of the file. If there are no more + // samples in the file, returns 0. #endif -//////// ERROR CODES + //////// ERROR CODES -enum STBVorbisError -{ - VORBIS__no_error, + enum STBVorbisError + { + VORBIS__no_error, - VORBIS_need_more_data=1, // not a real error + VORBIS_need_more_data = 1, // not a real error - VORBIS_invalid_api_mixing, // can't mix API modes - VORBIS_outofmem, // not enough memory - VORBIS_feature_not_supported, // uses floor 0 - VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small - VORBIS_file_open_failure, // fopen() failed - VORBIS_seek_without_length, // can't seek in unknown-length file + VORBIS_invalid_api_mixing, // can't mix API modes + VORBIS_outofmem, // not enough memory + VORBIS_feature_not_supported, // uses floor 0 + VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small + VORBIS_file_open_failure, // fopen() failed + VORBIS_seek_without_length, // can't seek in unknown-length file - VORBIS_unexpected_eof=10, // file is truncated? - VORBIS_seek_invalid, // seek past EOF + VORBIS_unexpected_eof = 10, // file is truncated? + VORBIS_seek_invalid, // seek past EOF - // decoding errors (corrupt/invalid stream) -- you probably - // don't care about the exact details of these + // decoding errors (corrupt/invalid stream) -- you probably + // don't care about the exact details of these - // vorbis errors: - VORBIS_invalid_setup=20, - VORBIS_invalid_stream, - - // ogg errors: - VORBIS_missing_capture_pattern=30, - VORBIS_invalid_stream_structure_version, - VORBIS_continued_packet_flag_invalid, - VORBIS_incorrect_stream_serial_number, - VORBIS_invalid_first_page, - VORBIS_bad_packet_type, - VORBIS_cant_find_last_page, - VORBIS_seek_failed, - VORBIS_ogg_skeleton_not_supported -}; + // vorbis errors: + VORBIS_invalid_setup = 20, + VORBIS_invalid_stream, + // ogg errors: + VORBIS_missing_capture_pattern = 30, + VORBIS_invalid_stream_structure_version, + VORBIS_continued_packet_flag_invalid, + VORBIS_incorrect_stream_serial_number, + VORBIS_invalid_first_page, + VORBIS_bad_packet_type, + VORBIS_cant_find_last_page, + VORBIS_seek_failed, + VORBIS_ogg_skeleton_not_supported + }; #ifdef __cplusplus } @@ -437,8 +433,7 @@ enum STBVorbisError // STB_VORBIS_NO_FAST_SCALED_FLOAT // does not use a fast float-to-int trick to accelerate float-to-int on // most platforms which requires endianness be defined correctly. -//#define STB_VORBIS_NO_FAST_SCALED_FLOAT - +// #define STB_VORBIS_NO_FAST_SCALED_FLOAT // STB_VORBIS_MAX_CHANNELS [number] // globally define this to the maximum number of channels you need. @@ -449,7 +444,7 @@ enum STBVorbisError // I forgot to account for. Can probably go as low as 8 (7.1 audio), // 6 (5.1 audio), or 2 (stereo only). #ifndef STB_VORBIS_MAX_CHANNELS -#define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone? +#define STB_VORBIS_MAX_CHANNELS 16 // enough for anyone? #endif // STB_VORBIS_PUSHDATA_CRC_COUNT [number] @@ -465,7 +460,7 @@ enum STBVorbisError // So don't hose ourselves by scanning an apparent 64KB page and // missing a ton of real ones in the interim; so minimum of 2 #ifndef STB_VORBIS_PUSHDATA_CRC_COUNT -#define STB_VORBIS_PUSHDATA_CRC_COUNT 4 +#define STB_VORBIS_PUSHDATA_CRC_COUNT 4 #endif // STB_VORBIS_FAST_HUFFMAN_LENGTH [number] @@ -474,7 +469,7 @@ enum STBVorbisError // but the table size is larger so worse cache missing, so you'll have // to probe (and try multiple ogg vorbis files) to find the sweet spot. #ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH -#define STB_VORBIS_FAST_HUFFMAN_LENGTH 10 +#define STB_VORBIS_FAST_HUFFMAN_LENGTH 10 #endif // STB_VORBIS_FAST_BINARY_LENGTH [number] @@ -513,7 +508,7 @@ enum STBVorbisError // because otherwise an integer divide-per-vector-element is required to // unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can // trade off storage for speed. -//#define STB_VORBIS_DIVIDES_IN_CODEBOOK +// #define STB_VORBIS_DIVIDES_IN_CODEBOOK #ifdef STB_VORBIS_CODEBOOK_SHORTS #error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats" @@ -535,81 +530,77 @@ enum STBVorbisError // you'd ever want to do it except for debugging. // #define STB_VORBIS_NO_DEFER_FLOOR - - - ////////////////////////////////////////////////////////////////////////////// #ifdef STB_VORBIS_NO_PULLDATA_API - #define STB_VORBIS_NO_INTEGER_CONVERSION - #define STB_VORBIS_NO_STDIO +#define STB_VORBIS_NO_INTEGER_CONVERSION +#define STB_VORBIS_NO_STDIO #endif #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) - #define STB_VORBIS_NO_STDIO 1 +#define STB_VORBIS_NO_STDIO 1 #endif #ifndef STB_VORBIS_NO_INTEGER_CONVERSION #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT - // only need endianness for fast-float-to-int, which we don't - // use for pushdata +// only need endianness for fast-float-to-int, which we don't +// use for pushdata - #ifndef STB_VORBIS_BIG_ENDIAN - #define STB_VORBIS_ENDIAN 0 - #else - #define STB_VORBIS_ENDIAN 1 - #endif +#ifndef STB_VORBIS_BIG_ENDIAN +#define STB_VORBIS_ENDIAN 0 +#else +#define STB_VORBIS_ENDIAN 1 +#endif #endif #endif - #ifndef STB_VORBIS_NO_STDIO #include #endif #ifndef STB_VORBIS_NO_CRT - #include - #include - #include - #include +#include +#include +#include +#include - // find definition of alloca if it's not in stdlib.h: - #if defined(_MSC_VER) || defined(__MINGW32__) - #include - #endif - #if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__) || defined(__NEWLIB__) - #include - #endif +// find definition of alloca if it's not in stdlib.h: +#if defined(_MSC_VER) || defined(__MINGW32__) +#include +#endif +#if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__) || defined(__NEWLIB__) +#include +#endif #else // STB_VORBIS_NO_CRT - #define NULL 0 - #define malloc(s) 0 - #define free(s) ((void) 0) - #define realloc(s) 0 +#define NULL 0 +#define malloc(s) 0 +#define free(s) ((void)0) +#define realloc(s) 0 #endif // STB_VORBIS_NO_CRT #include #ifdef __MINGW32__ - // eff you mingw: - // "fixed": - // http://sourceforge.net/p/mingw-w64/mailman/message/32882927/ - // "no that broke the build, reverted, who cares about C": - // http://sourceforge.net/p/mingw-w64/mailman/message/32890381/ - #ifdef __forceinline - #undef __forceinline - #endif - #define __forceinline - #ifndef alloca - #define alloca __builtin_alloca - #endif +// eff you mingw: +// "fixed": +// http://sourceforge.net/p/mingw-w64/mailman/message/32882927/ +// "no that broke the build, reverted, who cares about C": +// http://sourceforge.net/p/mingw-w64/mailman/message/32890381/ +#ifdef __forceinline +#undef __forceinline +#endif +#define __forceinline +#ifndef alloca +#define alloca __builtin_alloca +#endif #elif !defined(_MSC_VER) - #if __GNUC__ - #define __forceinline inline - #else - #define __forceinline - #endif +#if __GNUC__ +#define __forceinline inline +#else +#define __forceinline +#endif #endif #if STB_VORBIS_MAX_CHANNELS > 256 @@ -620,24 +611,22 @@ enum STBVorbisError #error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range" #endif - #if 0 #include -#define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1]) +#define CHECK(f) _CrtIsValidHeapPointer(f->channel_buffers[1]) #else -#define CHECK(f) ((void) 0) +#define CHECK(f) ((void)0) #endif -#define MAX_BLOCKSIZE_LOG 13 // from specification -#define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG) +#define MAX_BLOCKSIZE_LOG 13 // from specification +#define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG) - -typedef unsigned char uint8; -typedef signed char int8; +typedef unsigned char uint8; +typedef signed char int8; typedef unsigned short uint16; -typedef signed short int16; -typedef unsigned int uint32; -typedef signed int int32; +typedef signed short int16; +typedef unsigned int uint32; +typedef signed int int32; #ifndef TRUE #define TRUE 1 @@ -659,30 +648,30 @@ typedef float codetype; // the sizes larger--nothing relies on silently truncating etc., nor the // order of variables. -#define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH) -#define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1) +#define FAST_HUFFMAN_TABLE_SIZE (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH) +#define FAST_HUFFMAN_TABLE_MASK (FAST_HUFFMAN_TABLE_SIZE - 1) typedef struct { int dimensions, entries; uint8 *codeword_lengths; - float minimum_value; - float delta_value; - uint8 value_bits; - uint8 lookup_type; - uint8 sequence_p; - uint8 sparse; + float minimum_value; + float delta_value; + uint8 value_bits; + uint8 lookup_type; + uint8 sequence_p; + uint8 sparse; uint32 lookup_values; codetype *multiplicands; uint32 *codewords; - #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT - int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]; - #else - int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]; - #endif +#ifdef STB_VORBIS_FAST_HUFFMAN_SHORT + int16 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]; +#else + int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE]; +#endif uint32 *sorted_codewords; - int *sorted_values; - int sorted_entries; + int *sorted_values; + int sorted_entries; } Codebook; typedef struct @@ -700,13 +689,13 @@ typedef struct { uint8 partitions; uint8 partition_class_list[32]; // varies - uint8 class_dimensions[16]; // varies - uint8 class_subclasses[16]; // varies - uint8 class_masterbooks[16]; // varies - int16 subclass_books[16][8]; // varies - uint16 Xlist[31*8+2]; // varies - uint8 sorted_order[31*8+2]; - uint8 neighbors[31*8+2][2]; + uint8 class_dimensions[16]; // varies + uint8 class_subclasses[16]; // varies + uint8 class_masterbooks[16]; // varies + int16 subclass_books[16][8]; // varies + uint16 Xlist[31 * 8 + 2]; // varies + uint8 sorted_order[31 * 8 + 2]; + uint8 neighbors[31 * 8 + 2][2]; uint8 floor1_multiplier; uint8 rangebits; int values; @@ -739,9 +728,9 @@ typedef struct { uint16 coupling_steps; MappingChannel *chan; - uint8 submaps; - uint8 submap_floor[15]; // varies - uint8 submap_residue[15]; // varies + uint8 submaps; + uint8 submap_floor[15]; // varies + uint8 submap_residue[15]; // varies } Mapping; typedef struct @@ -754,11 +743,11 @@ typedef struct typedef struct { - uint32 goal_crc; // expected crc if match - int bytes_left; // bytes left in packet - uint32 crc_so_far; // running crc - int bytes_done; // bytes processed in _current_ chunk - uint32 sample_loc; // granule pos encoded in page + uint32 goal_crc; // expected crc if match + int bytes_left; // bytes left in packet + uint32 crc_so_far; // running crc + int bytes_done; // bytes processed in _current_ chunk + uint32 sample_loc; // granule pos encoded in page } CRCscan; typedef struct @@ -769,7 +758,7 @@ typedef struct struct stb_vorbis { - // user-accessible info + // user-accessible info unsigned int sample_rate; int channels; @@ -781,7 +770,7 @@ struct stb_vorbis int comment_list_length; char **comment_list; - // input config + // input config #ifndef STB_VORBIS_NO_STDIO FILE *f; uint32 f_start; @@ -794,7 +783,7 @@ struct stb_vorbis uint32 stream_len; - uint8 push_mode; + uint8 push_mode; // the page to seek to when seeking to start, may be zero uint32 first_audio_page_offset; @@ -803,18 +792,18 @@ struct stb_vorbis // (but not necessarily the page on which it starts) ProbedPage p_first, p_last; - // memory management + // memory management stb_vorbis_alloc alloc; int setup_offset; int temp_offset; - // run-time results + // run-time results int eof; enum STBVorbisError error; - // user-useful data + // user-useful data - // header info + // header info int blocksize[2]; int blocksize_0, blocksize_1; int codebook_count; @@ -828,34 +817,34 @@ struct stb_vorbis int mapping_count; Mapping *mapping; int mode_count; - Mode mode_config[64]; // varies + Mode mode_config[64]; // varies uint32 total_samples; - // decode buffer + // decode buffer float *channel_buffers[STB_VORBIS_MAX_CHANNELS]; - float *outputs [STB_VORBIS_MAX_CHANNELS]; + float *outputs[STB_VORBIS_MAX_CHANNELS]; float *previous_window[STB_VORBIS_MAX_CHANNELS]; int previous_length; - #ifndef STB_VORBIS_NO_DEFER_FLOOR +#ifndef STB_VORBIS_NO_DEFER_FLOOR int16 *finalY[STB_VORBIS_MAX_CHANNELS]; - #else +#else float *floor_buffers[STB_VORBIS_MAX_CHANNELS]; - #endif +#endif uint32 current_loc; // sample location of next frame to decode - int current_loc_valid; + int current_loc_valid; - // per-blocksize precomputed data + // per-blocksize precomputed data // twiddle factors - float *A[2],*B[2],*C[2]; + float *A[2], *B[2], *C[2]; float *window[2]; uint16 *bit_reverse[2]; - // current page/packet/segment streaming info + // current page/packet/segment streaming info uint32 serial; // stream serial number for verification int last_page; int segment_count; @@ -864,7 +853,7 @@ struct stb_vorbis uint8 bytes_in_seg; uint8 first_decode; int next_seg; - int last_seg; // flag that we're on the last segment + int last_seg; // flag that we're on the last segment int last_seg_which; // what was the segment number of the last seg? uint32 acc; int valid_bits; @@ -874,23 +863,23 @@ struct stb_vorbis int discard_samples_deferred; uint32 samples_output; - // push mode scanning + // push mode scanning int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching #ifndef STB_VORBIS_NO_PUSHDATA_API CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT]; #endif - // sample-access + // sample-access int channel_buffer_start; int channel_buffer_end; }; #if defined(STB_VORBIS_NO_PUSHDATA_API) - #define IS_PUSH_MODE(f) FALSE +#define IS_PUSH_MODE(f) FALSE #elif defined(STB_VORBIS_NO_PULLDATA_API) - #define IS_PUSH_MODE(f) TRUE +#define IS_PUSH_MODE(f) TRUE #else - #define IS_PUSH_MODE(f) ((f)->push_mode) +#define IS_PUSH_MODE(f) ((f)->push_mode) #endif typedef struct stb_vorbis vorb; @@ -898,34 +887,35 @@ typedef struct stb_vorbis vorb; static int error(vorb *f, enum STBVorbisError e) { f->error = e; - if (!f->eof && e != VORBIS_need_more_data) { - f->error=e; // breakpoint for debugging + if (!f->eof && e != VORBIS_need_more_data) + { + f->error = e; // breakpoint for debugging } return 0; } - // these functions are used for allocating temporary memory // while decoding. if you can afford the stack space, use // alloca(); otherwise, provide a temp buffer and it will // allocate out of those. -#define array_size_required(count,size) (count*(sizeof(void *)+(size))) +#define array_size_required(count, size) (count * (sizeof(void *) + (size))) -#define temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size)) -#define temp_free(f,p) (void)0 -#define temp_alloc_save(f) ((f)->temp_offset) -#define temp_alloc_restore(f,p) ((f)->temp_offset = (p)) +#define temp_alloc(f, size) (f->alloc.alloc_buffer ? setup_temp_malloc(f, size) : alloca(size)) +#define temp_free(f, p) (void)0 +#define temp_alloc_save(f) ((f)->temp_offset) +#define temp_alloc_restore(f, p) ((f)->temp_offset = (p)) -#define temp_block_array(f,count,size) make_block_array(temp_alloc(f,array_size_required(count,size)), count, size) +#define temp_block_array(f, count, size) make_block_array(temp_alloc(f, array_size_required(count, size)), count, size) // given a sufficiently large block of memory, make an array of pointers to subblocks of it static void *make_block_array(void *mem, int count, int size) { int i; - void ** p = (void **) mem; - char *q = (char *) (p + count); - for (i=0; i < count; ++i) { + void **p = (void **)mem; + char *q = (char *)(p + count); + for (i = 0; i < count; ++i) + { p[i] = q; q += size; } @@ -934,11 +924,13 @@ static void *make_block_array(void *mem, int count, int size) static void *setup_malloc(vorb *f, int sz) { - sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs. + sz = (sz + 7) & ~7; // round up to nearest 8 for alignment of future allocs. f->setup_memory_required += sz; - if (f->alloc.alloc_buffer) { - void *p = (char *) f->alloc.alloc_buffer + f->setup_offset; - if (f->setup_offset + sz > f->temp_offset) return NULL; + if (f->alloc.alloc_buffer) + { + void *p = (char *)f->alloc.alloc_buffer + f->setup_offset; + if (f->setup_offset + sz > f->temp_offset) + return NULL; f->setup_offset += sz; return p; } @@ -947,40 +939,45 @@ static void *setup_malloc(vorb *f, int sz) static void setup_free(vorb *f, void *p) { - if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack + if (f->alloc.alloc_buffer) + return; // do nothing; setup mem is a stack free(p); } static void *setup_temp_malloc(vorb *f, int sz) { - sz = (sz+7) & ~7; // round up to nearest 8 for alignment of future allocs. - if (f->alloc.alloc_buffer) { - if (f->temp_offset - sz < f->setup_offset) return NULL; + sz = (sz + 7) & ~7; // round up to nearest 8 for alignment of future allocs. + if (f->alloc.alloc_buffer) + { + if (f->temp_offset - sz < f->setup_offset) + return NULL; f->temp_offset -= sz; - return (char *) f->alloc.alloc_buffer + f->temp_offset; + return (char *)f->alloc.alloc_buffer + f->temp_offset; } return malloc(sz); } static void setup_temp_free(vorb *f, void *p, int sz) { - if (f->alloc.alloc_buffer) { - f->temp_offset += (sz+7)&~7; + if (f->alloc.alloc_buffer) + { + f->temp_offset += (sz + 7) & ~7; return; } free(p); } -#define CRC32_POLY 0x04c11db7 // from spec +#define CRC32_POLY 0x04c11db7 // from spec static uint32 crc_table[256]; static void crc32_init(void) { - int i,j; + int i, j; uint32 s; - for(i=0; i < 256; i++) { - for (s=(uint32) i << 24, j=0; j < 8; ++j) - s = (s << 1) ^ (s >= (1U<<31) ? CRC32_POLY : 0); + for (i = 0; i < 256; i++) + { + for (s = (uint32)i << 24, j = 0; j < 8; ++j) + s = (s << 1) ^ (s >= (1U << 31) ? CRC32_POLY : 0); crc_table[i] = s; } } @@ -990,20 +987,19 @@ static __forceinline uint32 crc32_update(uint32 crc, uint8 byte) return (crc << 8) ^ crc_table[byte ^ (crc >> 24)]; } - // used in setup, and for huffman that doesn't go fast path static unsigned int bit_reverse(unsigned int n) { - n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1); - n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2); - n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4); - n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8); - return (n >> 16) | (n << 16); + n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1); + n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2); + n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4); + n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8); + return (n >> 16) | (n << 16); } static float square(float x) { - return x*x; + return x * x; } // this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3 @@ -1011,28 +1007,36 @@ static float square(float x) // @OPTIMIZE: called multiple times per-packet with "constants"; move to setup static int ilog(int32 n) { - static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 }; + static signed char log2_4[16] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4}; - if (n < 0) return 0; // signed n returns 0 + if (n < 0) + return 0; // signed n returns 0 // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29) if (n < (1 << 14)) - if (n < (1 << 4)) return 0 + log2_4[n ]; - else if (n < (1 << 9)) return 5 + log2_4[n >> 5]; - else return 10 + log2_4[n >> 10]; + if (n < (1 << 4)) + return 0 + log2_4[n]; + else if (n < (1 << 9)) + return 5 + log2_4[n >> 5]; + else + return 10 + log2_4[n >> 10]; else if (n < (1 << 24)) - if (n < (1 << 19)) return 15 + log2_4[n >> 15]; - else return 20 + log2_4[n >> 20]; - else if (n < (1 << 29)) return 25 + log2_4[n >> 25]; - else return 30 + log2_4[n >> 30]; + if (n < (1 << 19)) + return 15 + log2_4[n >> 15]; + else + return 20 + log2_4[n >> 20]; + else if (n < (1 << 29)) + return 25 + log2_4[n >> 25]; + else + return 30 + log2_4[n >> 30]; } #ifndef M_PI - #define M_PI 3.14159265358979323846264f // from CRC +#define M_PI 3.14159265358979323846264f // from CRC #endif // code length assigned to a value with no huffman encoding -#define NO_CODE 255 +#define NO_CODE 255 /////////////////////// LEAF SETUP FUNCTIONS ////////////////////////// // @@ -1046,10 +1050,9 @@ static float float32_unpack(uint32 x) uint32 sign = x & 0x80000000; uint32 exp = (x & 0x7fe00000) >> 21; double res = sign ? -(double)mantissa : (double)mantissa; - return (float) ldexp((float)res, exp-788); + return (float)ldexp((float)res, exp - 788); } - // zlib & jpeg huffman tables assume that the output symbols // can either be arbitrarily arranged, or have monotonically // increasing frequencies--they rely on the lengths being sorted; @@ -1059,55 +1062,72 @@ static float float32_unpack(uint32 x) // order do not map to huffman codes "in order". static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values) { - if (!c->sparse) { - c->codewords [symbol] = huff_code; - } else { - c->codewords [count] = huff_code; + if (!c->sparse) + { + c->codewords[symbol] = huff_code; + } + else + { + c->codewords[count] = huff_code; c->codeword_lengths[count] = len; - values [count] = symbol; + values[count] = symbol; } } static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values) { - int i,k,m=0; + int i, k, m = 0; uint32 available[32]; memset(available, 0, sizeof(available)); // find the first entry - for (k=0; k < n; ++k) if (len[k] < NO_CODE) break; - if (k == n) { assert(c->sorted_entries == 0); return TRUE; } + for (k = 0; k < n; ++k) + if (len[k] < NO_CODE) + break; + if (k == n) + { + assert(c->sorted_entries == 0); + return TRUE; + } // add to the list add_entry(c, 0, k, m++, len[k], values); // add all available leaves - for (i=1; i <= len[k]; ++i) - available[i] = 1U << (32-i); + for (i = 1; i <= len[k]; ++i) + available[i] = 1U << (32 - i); // note that the above code treats the first case specially, // but it's really the same as the following code, so they // could probably be combined (except the initial code is 0, // and I use 0 in available[] to mean 'empty') - for (i=k+1; i < n; ++i) { + for (i = k + 1; i < n; ++i) + { uint32 res; int z = len[i], y; - if (z == NO_CODE) continue; + if (z == NO_CODE) + continue; // find lowest available leaf (should always be earliest, // which is what the specification calls for) // note that this property, and the fact we can never have // more than one free leaf at a given level, isn't totally // trivial to prove, but it seems true and the assert never // fires, so! - while (z > 0 && !available[z]) --z; - if (z == 0) { return FALSE; } + while (z > 0 && !available[z]) + --z; + if (z == 0) + { + return FALSE; + } res = available[z]; assert(z >= 0 && z < 32); available[z] = 0; add_entry(c, bit_reverse(res), i, m++, len[i], values); // propagate availability up the tree - if (z != len[i]) { + if (z != len[i]) + { assert(len[i] >= 0 && len[i] < 32); - for (y=len[i]; y > z; --y) { + for (y = len[i]; y > z; --y) + { assert(available[y] == 0); - available[y] = res + (1 << (32-y)); + available[y] = res + (1 << (32 - y)); } } } @@ -1119,20 +1139,24 @@ static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values) static void compute_accelerated_huffman(Codebook *c) { int i, len; - for (i=0; i < FAST_HUFFMAN_TABLE_SIZE; ++i) + for (i = 0; i < FAST_HUFFMAN_TABLE_SIZE; ++i) c->fast_huffman[i] = -1; len = c->sparse ? c->sorted_entries : c->entries; - #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT - if (len > 32767) len = 32767; // largest possible value we can encode! - #endif - for (i=0; i < len; ++i) { - if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) { +#ifdef STB_VORBIS_FAST_HUFFMAN_SHORT + if (len > 32767) + len = 32767; // largest possible value we can encode! +#endif + for (i = 0; i < len; ++i) + { + if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) + { uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i]; // set table entries for all bit combinations in the higher bits - while (z < FAST_HUFFMAN_TABLE_SIZE) { - c->fast_huffman[z] = i; - z += 1 << c->codeword_lengths[i]; + while (z < FAST_HUFFMAN_TABLE_SIZE) + { + c->fast_huffman[z] = i; + z += 1 << c->codeword_lengths[i]; } } } @@ -1146,16 +1170,22 @@ static void compute_accelerated_huffman(Codebook *c) static int STBV_CDECL uint32_compare(const void *p, const void *q) { - uint32 x = * (uint32 *) p; - uint32 y = * (uint32 *) q; + uint32 x = *(uint32 *)p; + uint32 y = *(uint32 *)q; return x < y ? -1 : x > y; } static int include_in_sort(Codebook *c, uint8 len) { - if (c->sparse) { assert(len != NO_CODE); return TRUE; } - if (len == NO_CODE) return FALSE; - if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE; + if (c->sparse) + { + assert(len != NO_CODE); + return TRUE; + } + if (len == NO_CODE) + return FALSE; + if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) + return TRUE; return FALSE; } @@ -1168,14 +1198,17 @@ static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN. // this is kind of a frivolous optimization--I don't see any performance improvement, // but it's like 4 extra lines of code, so. - if (!c->sparse) { + if (!c->sparse) + { int k = 0; - for (i=0; i < c->entries; ++i) + for (i = 0; i < c->entries; ++i) if (include_in_sort(c, lengths[i])) c->sorted_codewords[k++] = bit_reverse(c->codewords[i]); assert(k == c->sorted_entries); - } else { - for (i=0; i < c->sorted_entries; ++i) + } + else + { + for (i = 0; i < c->sorted_entries; ++i) c->sorted_codewords[i] = bit_reverse(c->codewords[i]); } @@ -1188,26 +1221,35 @@ static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) // #2: for each sorted entry, search the original list to find who corresponds // #3: for each original entry, find the sorted entry // #1 requires extra storage, #2 is slow, #3 can use binary search! - for (i=0; i < len; ++i) { + for (i = 0; i < len; ++i) + { int huff_len = c->sparse ? lengths[values[i]] : lengths[i]; - if (include_in_sort(c,huff_len)) { + if (include_in_sort(c, huff_len)) + { uint32 code = bit_reverse(c->codewords[i]); - int x=0, n=c->sorted_entries; - while (n > 1) { + int x = 0, n = c->sorted_entries; + while (n > 1) + { // invariant: sc[x] <= code < sc[x+n] int m = x + (n >> 1); - if (c->sorted_codewords[m] <= code) { + if (c->sorted_codewords[m] <= code) + { x = m; - n -= (n>>1); - } else { + n -= (n >> 1); + } + else + { n >>= 1; } } assert(c->sorted_codewords[x] == code); - if (c->sparse) { + if (c->sparse) + { c->sorted_values[x] = values[i]; c->codeword_lengths[x] = huff_len; - } else { + } + else + { c->sorted_values[x] = i; } } @@ -1217,7 +1259,7 @@ static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) // only run while parsing the header (3 times) static int vorbis_validate(uint8 *data) { - static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' }; + static uint8 vorbis[6] = {'v', 'o', 'r', 'b', 'i', 's'}; return memcmp(data, vorbis, 6) == 0; } @@ -1225,12 +1267,12 @@ static int vorbis_validate(uint8 *data) // (formula implied by specification) static int lookup1_values(int entries, int dim) { - int r = (int) floor(exp((float) log((float) entries) / dim)); - if ((int) floor(pow((float) r+1, dim)) <= entries) // (int) cast for MinGW warning; - ++r; // floor() to avoid _ftol() when non-CRT - if (pow((float) r+1, dim) <= entries) + int r = (int)floor(exp((float)log((float)entries) / dim)); + if ((int)floor(pow((float)r + 1, dim)) <= entries) // (int) cast for MinGW warning; + ++r; // floor() to avoid _ftol() when non-CRT + if (pow((float)r + 1, dim) <= entries) return -1; - if ((int) floor(pow((float) r, dim)) > entries) + if ((int)floor(pow((float)r, dim)) > entries) return -1; return r; } @@ -1239,48 +1281,53 @@ static int lookup1_values(int entries, int dim) static void compute_twiddle_factors(int n, float *A, float *B, float *C) { int n4 = n >> 2, n8 = n >> 3; - int k,k2; + int k, k2; - for (k=k2=0; k < n4; ++k,k2+=2) { - A[k2 ] = (float) cos(4*k*M_PI/n); - A[k2+1] = (float) -sin(4*k*M_PI/n); - B[k2 ] = (float) cos((k2+1)*M_PI/n/2) * 0.5f; - B[k2+1] = (float) sin((k2+1)*M_PI/n/2) * 0.5f; + for (k = k2 = 0; k < n4; ++k, k2 += 2) + { + A[k2] = (float)cos(4 * k * M_PI / n); + A[k2 + 1] = (float)-sin(4 * k * M_PI / n); + B[k2] = (float)cos((k2 + 1) * M_PI / n / 2) * 0.5f; + B[k2 + 1] = (float)sin((k2 + 1) * M_PI / n / 2) * 0.5f; } - for (k=k2=0; k < n8; ++k,k2+=2) { - C[k2 ] = (float) cos(2*(k2+1)*M_PI/n); - C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n); + for (k = k2 = 0; k < n8; ++k, k2 += 2) + { + C[k2] = (float)cos(2 * (k2 + 1) * M_PI / n); + C[k2 + 1] = (float)-sin(2 * (k2 + 1) * M_PI / n); } } static void compute_window(int n, float *window) { int n2 = n >> 1, i; - for (i=0; i < n2; ++i) - window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI))); + for (i = 0; i < n2; ++i) + window[i] = (float)sin(0.5 * M_PI * square((float)sin((i - 0 + 0.5) / n2 * 0.5 * M_PI))); } static void compute_bitreverse(int n, uint16 *rev) { int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions int i, n8 = n >> 3; - for (i=0; i < n8; ++i) - rev[i] = (bit_reverse(i) >> (32-ld+3)) << 2; + for (i = 0; i < n8; ++i) + rev[i] = (bit_reverse(i) >> (32 - ld + 3)) << 2; } static int init_blocksize(vorb *f, int b, int n) { int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3; - f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2); - f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2); - f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4); - if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem); + f->A[b] = (float *)setup_malloc(f, sizeof(float) * n2); + f->B[b] = (float *)setup_malloc(f, sizeof(float) * n2); + f->C[b] = (float *)setup_malloc(f, sizeof(float) * n4); + if (!f->A[b] || !f->B[b] || !f->C[b]) + return error(f, VORBIS_outofmem); compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]); - f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2); - if (!f->window[b]) return error(f, VORBIS_outofmem); + f->window[b] = (float *)setup_malloc(f, sizeof(float) * n2); + if (!f->window[b]) + return error(f, VORBIS_outofmem); compute_window(n, f->window[b]); - f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8); - if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem); + f->bit_reverse[b] = (uint16 *)setup_malloc(f, sizeof(uint16) * n8); + if (!f->bit_reverse[b]) + return error(f, VORBIS_outofmem); compute_bitreverse(n, f->bit_reverse[b]); return TRUE; } @@ -1290,49 +1337,66 @@ static void neighbors(uint16 *x, int n, int *plow, int *phigh) int low = -1; int high = 65536; int i; - for (i=0; i < n; ++i) { - if (x[i] > low && x[i] < x[n]) { *plow = i; low = x[i]; } - if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; } + for (i = 0; i < n; ++i) + { + if (x[i] > low && x[i] < x[n]) + { + *plow = i; + low = x[i]; + } + if (x[i] < high && x[i] > x[n]) + { + *phigh = i; + high = x[i]; + } } } // this has been repurposed so y is now the original index instead of y typedef struct { - uint16 x,id; + uint16 x, id; } stbv__floor_ordering; static int STBV_CDECL point_compare(const void *p, const void *q) { - stbv__floor_ordering *a = (stbv__floor_ordering *) p; - stbv__floor_ordering *b = (stbv__floor_ordering *) q; + stbv__floor_ordering *a = (stbv__floor_ordering *)p; + stbv__floor_ordering *b = (stbv__floor_ordering *)q; return a->x < b->x ? -1 : a->x > b->x; } // /////////////////////// END LEAF SETUP FUNCTIONS ////////////////////////// - #if defined(STB_VORBIS_NO_STDIO) - #define USE_MEMORY(z) TRUE +#define USE_MEMORY(z) TRUE #else - #define USE_MEMORY(z) ((z)->stream) +#define USE_MEMORY(z) ((z)->stream) #endif static uint8 get8(vorb *z) { - if (USE_MEMORY(z)) { - if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; } + if (USE_MEMORY(z)) + { + if (z->stream >= z->stream_end) + { + z->eof = TRUE; + return 0; + } return *z->stream++; } - #ifndef STB_VORBIS_NO_STDIO +#ifndef STB_VORBIS_NO_STDIO { - int c = fgetc(z->f); - if (c == EOF) { z->eof = TRUE; return 0; } - return c; + int c = fgetc(z->f); + if (c == EOF) + { + z->eof = TRUE; + return 0; + } + return c; } - #endif +#endif } static uint32 get32(vorb *f) @@ -1341,65 +1405,81 @@ static uint32 get32(vorb *f) x = get8(f); x += get8(f) << 8; x += get8(f) << 16; - x += (uint32) get8(f) << 24; + x += (uint32)get8(f) << 24; return x; } static int getn(vorb *z, uint8 *data, int n) { - if (USE_MEMORY(z)) { - if (z->stream+n > z->stream_end) { z->eof = 1; return 0; } + if (USE_MEMORY(z)) + { + if (z->stream + n > z->stream_end) + { + z->eof = 1; + return 0; + } memcpy(data, z->stream, n); z->stream += n; return 1; } - #ifndef STB_VORBIS_NO_STDIO +#ifndef STB_VORBIS_NO_STDIO if (fread(data, n, 1, z->f) == 1) return 1; - else { + else + { z->eof = 1; return 0; } - #endif +#endif } static void skip(vorb *z, int n) { - if (USE_MEMORY(z)) { + if (USE_MEMORY(z)) + { z->stream += n; - if (z->stream >= z->stream_end) z->eof = 1; + if (z->stream >= z->stream_end) + z->eof = 1; return; } - #ifndef STB_VORBIS_NO_STDIO +#ifndef STB_VORBIS_NO_STDIO { long x = ftell(z->f); - fseek(z->f, x+n, SEEK_SET); + fseek(z->f, x + n, SEEK_SET); } - #endif +#endif } static int set_file_offset(stb_vorbis *f, unsigned int loc) { - #ifndef STB_VORBIS_NO_PUSHDATA_API - if (f->push_mode) return 0; - #endif +#ifndef STB_VORBIS_NO_PUSHDATA_API + if (f->push_mode) + return 0; +#endif f->eof = 0; - if (USE_MEMORY(f)) { - if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) { + if (USE_MEMORY(f)) + { + if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) + { f->stream = f->stream_end; f->eof = 1; return 0; - } else { + } + else + { f->stream = f->stream_start + loc; return 1; } } - #ifndef STB_VORBIS_NO_STDIO - if (loc + f->f_start < loc || loc >= 0x80000000) { +#ifndef STB_VORBIS_NO_STDIO + if (loc + f->f_start < loc || loc >= 0x80000000) + { loc = 0x7fffffff; f->eof = 1; - } else { + } + else + { loc += f->f_start; } if (!fseek(f->f, loc, SEEK_SET)) @@ -1407,33 +1487,38 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc) f->eof = 1; fseek(f->f, f->f_start, SEEK_END); return 0; - #endif +#endif } - -static uint8 ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 }; +static uint8 ogg_page_header[4] = {0x4f, 0x67, 0x67, 0x53}; static int capture_pattern(vorb *f) { - if (0x4f != get8(f)) return FALSE; - if (0x67 != get8(f)) return FALSE; - if (0x67 != get8(f)) return FALSE; - if (0x53 != get8(f)) return FALSE; + if (0x4f != get8(f)) + return FALSE; + if (0x67 != get8(f)) + return FALSE; + if (0x67 != get8(f)) + return FALSE; + if (0x53 != get8(f)) + return FALSE; return TRUE; } -#define PAGEFLAG_continued_packet 1 -#define PAGEFLAG_first_page 2 -#define PAGEFLAG_last_page 4 +#define PAGEFLAG_continued_packet 1 +#define PAGEFLAG_first_page 2 +#define PAGEFLAG_last_page 4 static int start_page_no_capturepattern(vorb *f) { - uint32 loc0,loc1,n; - if (f->first_decode && !IS_PUSH_MODE(f)) { + uint32 loc0, loc1, n; + if (f->first_decode && !IS_PUSH_MODE(f)) + { f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4; } // stream structure version - if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version); + if (0 != get8(f)) + return error(f, VORBIS_invalid_stream_structure_version); // header flag f->page_flag = get8(f); // absolute granule position @@ -1442,8 +1527,8 @@ static int start_page_no_capturepattern(vorb *f) // @TODO: validate loc0,loc1 as valid positions? // stream serial number -- vorbis doesn't interleave, so discard get32(f); - //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number); - // page sequence number + // if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number); + // page sequence number n = get32(f); f->last_page = n; // CRC32 @@ -1454,22 +1539,25 @@ static int start_page_no_capturepattern(vorb *f) return error(f, VORBIS_unexpected_eof); // assume we _don't_ know any the sample position of any segments f->end_seg_with_known_loc = -2; - if (loc0 != ~0U || loc1 != ~0U) { + if (loc0 != ~0U || loc1 != ~0U) + { int i; // determine which packet is the last one that will complete - for (i=f->segment_count-1; i >= 0; --i) + for (i = f->segment_count - 1; i >= 0; --i) if (f->segments[i] < 255) break; // 'i' is now the index of the _last_ segment of a packet that ends - if (i >= 0) { + if (i >= 0) + { f->end_seg_with_known_loc = i; - f->known_loc_for_packet = loc0; + f->known_loc_for_packet = loc0; } } - if (f->first_decode) { - int i,len; + if (f->first_decode) + { + int i, len; len = 0; - for (i=0; i < f->segment_count; ++i) + for (i = 0; i < f->segment_count; ++i) len += f->segments[i]; len += 27 + f->segment_count; f->p_first.page_end = f->p_first.page_start + len; @@ -1481,14 +1569,17 @@ static int start_page_no_capturepattern(vorb *f) static int start_page(vorb *f) { - if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern); + if (!capture_pattern(f)) + return error(f, VORBIS_missing_capture_pattern); return start_page_no_capturepattern(f); } static int start_packet(vorb *f) { - while (f->next_seg == -1) { - if (!start_page(f)) return FALSE; + while (f->next_seg == -1) + { + if (!start_page(f)) + return FALSE; if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_continued_packet_flag_invalid); } @@ -1502,15 +1593,23 @@ static int start_packet(vorb *f) static int maybe_start_packet(vorb *f) { - if (f->next_seg == -1) { + if (f->next_seg == -1) + { int x = get8(f); - if (f->eof) return FALSE; // EOF at page boundary is not an error! - if (0x4f != x ) return error(f, VORBIS_missing_capture_pattern); - if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern); - if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern); - if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern); - if (!start_page_no_capturepattern(f)) return FALSE; - if (f->page_flag & PAGEFLAG_continued_packet) { + if (f->eof) + return FALSE; // EOF at page boundary is not an error! + if (0x4f != x) + return error(f, VORBIS_missing_capture_pattern); + if (0x67 != get8(f)) + return error(f, VORBIS_missing_capture_pattern); + if (0x67 != get8(f)) + return error(f, VORBIS_missing_capture_pattern); + if (0x53 != get8(f)) + return error(f, VORBIS_missing_capture_pattern); + if (!start_page_no_capturepattern(f)) + return FALSE; + if (f->page_flag & PAGEFLAG_continued_packet) + { // set up enough state that we can read this packet if we want, // e.g. during recovery f->last_seg = FALSE; @@ -1524,16 +1623,24 @@ static int maybe_start_packet(vorb *f) static int next_segment(vorb *f) { int len; - if (f->last_seg) return 0; - if (f->next_seg == -1) { - f->last_seg_which = f->segment_count-1; // in case start_page fails - if (!start_page(f)) { f->last_seg = 1; return 0; } - if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid); + if (f->last_seg) + return 0; + if (f->next_seg == -1) + { + f->last_seg_which = f->segment_count - 1; // in case start_page fails + if (!start_page(f)) + { + f->last_seg = 1; + return 0; + } + if (!(f->page_flag & PAGEFLAG_continued_packet)) + return error(f, VORBIS_continued_packet_flag_invalid); } len = f->segments[f->next_seg++]; - if (len < 255) { + if (len < 255) + { f->last_seg = TRUE; - f->last_seg_which = f->next_seg-1; + f->last_seg_which = f->next_seg - 1; } if (f->next_seg >= f->segment_count) f->next_seg = -1; @@ -1542,14 +1649,17 @@ static int next_segment(vorb *f) return len; } -#define EOP (-1) -#define INVALID_BITS (-1) +#define EOP (-1) +#define INVALID_BITS (-1) static int get8_packet_raw(vorb *f) { - if (!f->bytes_in_seg) { // CLANG! - if (f->last_seg) return EOP; - else if (!next_segment(f)) return EOP; + if (!f->bytes_in_seg) + { // CLANG! + if (f->last_seg) + return EOP; + else if (!next_segment(f)) + return EOP; } assert(f->bytes_in_seg > 0); --f->bytes_in_seg; @@ -1570,13 +1680,14 @@ static int get32_packet(vorb *f) x = get8_packet(f); x += get8_packet(f) << 8; x += get8_packet(f) << 16; - x += (uint32) get8_packet(f) << 24; + x += (uint32)get8_packet(f) << 24; return x; } static void flush_packet(vorb *f) { - while (get8_packet_raw(f) != EOP); + while (get8_packet_raw(f) != EOP) + ; } // @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important @@ -1585,18 +1696,24 @@ static uint32 get_bits(vorb *f, int n) { uint32 z; - if (f->valid_bits < 0) return 0; - if (f->valid_bits < n) { - if (n > 24) { + if (f->valid_bits < 0) + return 0; + if (f->valid_bits < n) + { + if (n > 24) + { // the accumulator technique below would not work correctly in this case z = get_bits(f, 24); - z += get_bits(f, n-24) << 24; + z += get_bits(f, n - 24) << 24; return z; } - if (f->valid_bits == 0) f->acc = 0; - while (f->valid_bits < n) { + if (f->valid_bits == 0) + f->acc = 0; + while (f->valid_bits < n) + { int z = get8_packet_raw(f); - if (z == EOP) { + if (z == EOP) + { f->valid_bits = INVALID_BITS; return 0; } @@ -1606,7 +1723,7 @@ static uint32 get_bits(vorb *f, int n) } assert(f->valid_bits >= n); - z = f->acc & ((1 << n)-1); + z = f->acc & ((1 << n) - 1); f->acc >>= n; f->valid_bits -= n; return z; @@ -1618,14 +1735,19 @@ static uint32 get_bits(vorb *f, int n) // e.g. cache them locally and decode locally static __forceinline void prep_huffman(vorb *f) { - if (f->valid_bits <= 24) { - if (f->valid_bits == 0) f->acc = 0; - do { + if (f->valid_bits <= 24) + { + if (f->valid_bits == 0) + f->acc = 0; + do + { int z; - if (f->last_seg && !f->bytes_in_seg) return; + if (f->last_seg && !f->bytes_in_seg) + return; z = get8_packet_raw(f); - if (z == EOP) return; - f->acc += (unsigned) z << f->valid_bits; + if (z == EOP) + return; + f->acc += (unsigned)z << f->valid_bits; f->valid_bits += 8; } while (f->valid_bits <= 24); } @@ -1648,26 +1770,33 @@ static int codebook_decode_scalar_raw(vorb *f, Codebook *c) // cases to use binary search: sorted_codewords && !c->codewords // sorted_codewords && c->entries > 8 - if (c->entries > 8 ? c->sorted_codewords!=NULL : !c->codewords) { + if (c->entries > 8 ? c->sorted_codewords != NULL : !c->codewords) + { // binary search uint32 code = bit_reverse(f->acc); - int x=0, n=c->sorted_entries, len; + int x = 0, n = c->sorted_entries, len; - while (n > 1) { + while (n > 1) + { // invariant: sc[x] <= code < sc[x+n] int m = x + (n >> 1); - if (c->sorted_codewords[m] <= code) { + if (c->sorted_codewords[m] <= code) + { x = m; - n -= (n>>1); - } else { + n -= (n >> 1); + } + else + { n >>= 1; } } // x is now the sorted index - if (!c->sparse) x = c->sorted_values[x]; + if (!c->sparse) + x = c->sorted_values[x]; // x is now sorted index if sparse, or symbol otherwise len = c->codeword_lengths[x]; - if (f->valid_bits >= len) { + if (f->valid_bits >= len) + { f->acc >>= len; f->valid_bits -= len; return x; @@ -1679,10 +1808,14 @@ static int codebook_decode_scalar_raw(vorb *f, Codebook *c) // if small, linear search assert(!c->sparse); - for (i=0; i < c->entries; ++i) { - if (c->codeword_lengths[i] == NO_CODE) continue; - if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) { - if (f->valid_bits >= c->codeword_lengths[i]) { + for (i = 0; i < c->entries; ++i) + { + if (c->codeword_lengths[i] == NO_CODE) + continue; + if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i]) - 1))) + { + if (f->valid_bits >= c->codeword_lengths[i]) + { f->acc >>= c->codeword_lengths[i]; f->valid_bits -= c->codeword_lengths[i]; return i; @@ -1699,18 +1832,25 @@ static int codebook_decode_scalar_raw(vorb *f, Codebook *c) #ifndef STB_VORBIS_NO_INLINE_DECODE -#define DECODE_RAW(var, f,c) \ - if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \ - prep_huffman(f); \ - var = f->acc & FAST_HUFFMAN_TABLE_MASK; \ - var = c->fast_huffman[var]; \ - if (var >= 0) { \ - int n = c->codeword_lengths[var]; \ - f->acc >>= n; \ - f->valid_bits -= n; \ - if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \ - } else { \ - var = codebook_decode_scalar_raw(f,c); \ +#define DECODE_RAW(var, f, c) \ + if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) \ + prep_huffman(f); \ + var = f->acc & FAST_HUFFMAN_TABLE_MASK; \ + var = c->fast_huffman[var]; \ + if (var >= 0) \ + { \ + int n = c->codeword_lengths[var]; \ + f->acc >>= n; \ + f->valid_bits -= n; \ + if (f->valid_bits < 0) \ + { \ + f->valid_bits = 0; \ + var = -1; \ + } \ + } \ + else \ + { \ + var = codebook_decode_scalar_raw(f, c); \ } #else @@ -1723,39 +1863,40 @@ static int codebook_decode_scalar(vorb *f, Codebook *c) // fast huffman table lookup i = f->acc & FAST_HUFFMAN_TABLE_MASK; i = c->fast_huffman[i]; - if (i >= 0) { + if (i >= 0) + { f->acc >>= c->codeword_lengths[i]; f->valid_bits -= c->codeword_lengths[i]; - if (f->valid_bits < 0) { f->valid_bits = 0; return -1; } + if (f->valid_bits < 0) + { + f->valid_bits = 0; + return -1; + } return i; } - return codebook_decode_scalar_raw(f,c); + return codebook_decode_scalar_raw(f, c); } -#define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c); +#define DECODE_RAW(var, f, c) var = codebook_decode_scalar(f, c); #endif -#define DECODE(var,f,c) \ - DECODE_RAW(var,f,c) \ - if (c->sparse) var = c->sorted_values[var]; +#define DECODE(var, f, c) \ + DECODE_RAW(var, f, c) \ + if (c->sparse) \ + var = c->sorted_values[var]; #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK - #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c) +#define DECODE_VQ(var, f, c) DECODE_RAW(var, f, c) #else - #define DECODE_VQ(var,f,c) DECODE(var,f,c) +#define DECODE_VQ(var, f, c) DECODE(var, f, c) #endif - - - - - // CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case // where we avoid one addition -#define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off]) -#define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off]) -#define CODEBOOK_ELEMENT_BASE(c) (0) +#define CODEBOOK_ELEMENT(c, off) (c->multiplicands[off]) +#define CODEBOOK_ELEMENT_FAST(c, off) (c->multiplicands[off]) +#define CODEBOOK_ELEMENT_BASE(c) (0) static int codebook_decode_start(vorb *f, Codebook *c) { @@ -1764,10 +1905,13 @@ static int codebook_decode_start(vorb *f, Codebook *c) // type 0 is only legal in a scalar context if (c->lookup_type == 0) error(f, VORBIS_invalid_stream); - else { - DECODE_VQ(z,f,c); - if (c->sparse) assert(z < c->sorted_entries); - if (z < 0) { // check for EOP + else + { + DECODE_VQ(z, f, c); + if (c->sparse) + assert(z < c->sorted_entries); + if (z < 0) + { // check for EOP if (!f->bytes_in_seg) if (f->last_seg) return z; @@ -1779,19 +1923,24 @@ static int codebook_decode_start(vorb *f, Codebook *c) static int codebook_decode(vorb *f, Codebook *c, float *output, int len) { - int i,z = codebook_decode_start(f,c); - if (z < 0) return FALSE; - if (len > c->dimensions) len = c->dimensions; + int i, z = codebook_decode_start(f, c); + if (z < 0) + return FALSE; + if (len > c->dimensions) + len = c->dimensions; #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK - if (c->lookup_type == 1) { + if (c->lookup_type == 1) + { float last = CODEBOOK_ELEMENT_BASE(c); int div = 1; - for (i=0; i < len; ++i) { + for (i = 0; i < len; ++i) + { int off = (z / div) % c->lookup_values; - float val = CODEBOOK_ELEMENT_FAST(c,off) + last; + float val = CODEBOOK_ELEMENT_FAST(c, off) + last; output[i] += val; - if (c->sequence_p) last = val + c->minimum_value; + if (c->sequence_p) + last = val + c->minimum_value; div *= c->lookup_values; } return TRUE; @@ -1799,17 +1948,22 @@ static int codebook_decode(vorb *f, Codebook *c, float *output, int len) #endif z *= c->dimensions; - if (c->sequence_p) { + if (c->sequence_p) + { float last = CODEBOOK_ELEMENT_BASE(c); - for (i=0; i < len; ++i) { - float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + for (i = 0; i < len; ++i) + { + float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last; output[i] += val; last = val + c->minimum_value; } - } else { + } + else + { float last = CODEBOOK_ELEMENT_BASE(c); - for (i=0; i < len; ++i) { - output[i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last; + for (i = 0; i < len; ++i) + { + output[i] += CODEBOOK_ELEMENT_FAST(c, z + i) + last; } } @@ -1818,19 +1972,24 @@ static int codebook_decode(vorb *f, Codebook *c, float *output, int len) static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step) { - int i,z = codebook_decode_start(f,c); + int i, z = codebook_decode_start(f, c); float last = CODEBOOK_ELEMENT_BASE(c); - if (z < 0) return FALSE; - if (len > c->dimensions) len = c->dimensions; + if (z < 0) + return FALSE; + if (len > c->dimensions) + len = c->dimensions; #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK - if (c->lookup_type == 1) { + if (c->lookup_type == 1) + { int div = 1; - for (i=0; i < len; ++i) { + for (i = 0; i < len; ++i) + { int off = (z / div) % c->lookup_values; - float val = CODEBOOK_ELEMENT_FAST(c,off) + last; - output[i*step] += val; - if (c->sequence_p) last = val; + float val = CODEBOOK_ELEMENT_FAST(c, off) + last; + output[i * step] += val; + if (c->sequence_p) + last = val; div *= c->lookup_values; } return TRUE; @@ -1838,10 +1997,12 @@ static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, in #endif z *= c->dimensions; - for (i=0; i < len; ++i) { - float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; - output[i*step] += val; - if (c->sequence_p) last = val; + for (i = 0; i < len; ++i) + { + float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last; + output[i * step] += val; + if (c->sequence_p) + last = val; } return TRUE; @@ -1851,20 +2012,24 @@ static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **out { int c_inter = *c_inter_p; int p_inter = *p_inter_p; - int i,z, effective = c->dimensions; + int i, z, effective = c->dimensions; // type 0 is only legal in a scalar context - if (c->lookup_type == 0) return error(f, VORBIS_invalid_stream); + if (c->lookup_type == 0) + return error(f, VORBIS_invalid_stream); - while (total_decode > 0) { + while (total_decode > 0) + { float last = CODEBOOK_ELEMENT_BASE(c); - DECODE_VQ(z,f,c); - #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK + DECODE_VQ(z, f, c); +#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK assert(!c->sparse || z < c->sorted_entries); - #endif - if (z < 0) { +#endif + if (z < 0) + { if (!f->bytes_in_seg) - if (f->last_seg) return FALSE; + if (f->last_seg) + return FALSE; return error(f, VORBIS_invalid_stream); } @@ -1872,40 +2037,62 @@ static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **out // we check by computing the length of the virtual interleaved // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter), // and the length we'll be using (effective) - if (c_inter + p_inter*ch + effective > len * ch) { - effective = len*ch - (p_inter*ch - c_inter); + if (c_inter + p_inter * ch + effective > len * ch) + { + effective = len * ch - (p_inter * ch - c_inter); } - #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK - if (c->lookup_type == 1) { +#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK + if (c->lookup_type == 1) + { int div = 1; - for (i=0; i < effective; ++i) { + for (i = 0; i < effective; ++i) + { int off = (z / div) % c->lookup_values; - float val = CODEBOOK_ELEMENT_FAST(c,off) + last; + float val = CODEBOOK_ELEMENT_FAST(c, off) + last; if (outputs[c_inter]) outputs[c_inter][p_inter] += val; - if (++c_inter == ch) { c_inter = 0; ++p_inter; } - if (c->sequence_p) last = val; + if (++c_inter == ch) + { + c_inter = 0; + ++p_inter; + } + if (c->sequence_p) + last = val; div *= c->lookup_values; } - } else - #endif + } + else +#endif { z *= c->dimensions; - if (c->sequence_p) { - for (i=0; i < effective; ++i) { - float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + if (c->sequence_p) + { + for (i = 0; i < effective; ++i) + { + float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last; if (outputs[c_inter]) outputs[c_inter][p_inter] += val; - if (++c_inter == ch) { c_inter = 0; ++p_inter; } + if (++c_inter == ch) + { + c_inter = 0; + ++p_inter; + } last = val; } - } else { - for (i=0; i < effective; ++i) { - float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; + } + else + { + for (i = 0; i < effective; ++i) + { + float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last; if (outputs[c_inter]) outputs[c_inter][p_inter] += val; - if (++c_inter == ch) { c_inter = 0; ++p_inter; } + if (++c_inter == ch) + { + c_inter = 0; + ++p_inter; + } } } } @@ -1929,73 +2116,71 @@ static int predict_point(int x, int x0, int x1, int y0, int y1) // the following table is block-copied from the specification static float inverse_db_table[256] = -{ - 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f, - 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f, - 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f, - 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f, - 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f, - 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f, - 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f, - 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f, - 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f, - 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f, - 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f, - 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f, - 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f, - 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f, - 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f, - 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f, - 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f, - 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f, - 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f, - 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f, - 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f, - 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f, - 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f, - 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f, - 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f, - 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f, - 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f, - 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f, - 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f, - 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f, - 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f, - 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f, - 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f, - 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f, - 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f, - 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f, - 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f, - 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f, - 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f, - 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f, - 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f, - 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f, - 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f, - 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f, - 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f, - 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f, - 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f, - 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f, - 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f, - 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f, - 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f, - 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f, - 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f, - 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f, - 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f, - 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f, - 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f, - 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f, - 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f, - 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f, - 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f, - 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f, - 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f, - 0.82788260f, 0.88168307f, 0.9389798f, 1.0f -}; - + { + 1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f, + 1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f, + 1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f, + 2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f, + 2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f, + 3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f, + 4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f, + 6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f, + 7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f, + 1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f, + 1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f, + 1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f, + 2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f, + 2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f, + 3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f, + 4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f, + 5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f, + 7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f, + 9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f, + 1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f, + 1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f, + 2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f, + 2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f, + 3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f, + 4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f, + 5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f, + 7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f, + 9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f, + 0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f, + 0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f, + 0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f, + 0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f, + 0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f, + 0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f, + 0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f, + 0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f, + 0.00092223983f, 0.00098217216f, 0.0010459992f, 0.0011139742f, + 0.0011863665f, 0.0012634633f, 0.0013455702f, 0.0014330129f, + 0.0015261382f, 0.0016253153f, 0.0017309374f, 0.0018434235f, + 0.0019632195f, 0.0020908006f, 0.0022266726f, 0.0023713743f, + 0.0025254795f, 0.0026895994f, 0.0028643847f, 0.0030505286f, + 0.0032487691f, 0.0034598925f, 0.0036847358f, 0.0039241906f, + 0.0041792066f, 0.0044507950f, 0.0047400328f, 0.0050480668f, + 0.0053761186f, 0.0057254891f, 0.0060975636f, 0.0064938176f, + 0.0069158225f, 0.0073652516f, 0.0078438871f, 0.0083536271f, + 0.0088964928f, 0.009474637f, 0.010090352f, 0.010746080f, + 0.011444421f, 0.012188144f, 0.012980198f, 0.013823725f, + 0.014722068f, 0.015678791f, 0.016697687f, 0.017782797f, + 0.018938423f, 0.020169149f, 0.021479854f, 0.022875735f, + 0.024362330f, 0.025945531f, 0.027631618f, 0.029427276f, + 0.031339626f, 0.033376252f, 0.035545228f, 0.037855157f, + 0.040315199f, 0.042935108f, 0.045725273f, 0.048696758f, + 0.051861348f, 0.055231591f, 0.058820850f, 0.062643361f, + 0.066714279f, 0.071049749f, 0.075666962f, 0.080584227f, + 0.085821044f, 0.091398179f, 0.097337747f, 0.10366330f, + 0.11039993f, 0.11757434f, 0.12521498f, 0.13335215f, + 0.14201813f, 0.15124727f, 0.16107617f, 0.17154380f, + 0.18269168f, 0.19456402f, 0.20720788f, 0.22067342f, + 0.23501402f, 0.25028656f, 0.26655159f, 0.28387361f, + 0.30232132f, 0.32196786f, 0.34289114f, 0.36517414f, + 0.38890521f, 0.41417847f, 0.44109412f, 0.46975890f, + 0.50028648f, 0.53279791f, 0.56742212f, 0.60429640f, + 0.64356699f, 0.68538959f, 0.72993007f, 0.77736504f, + 0.82788260f, 0.88168307f, 0.9389798f, 1.0f}; // @OPTIMIZE: if you want to replace this bresenham line-drawing routine, // note that you must produce bit-identical output to decode correctly; @@ -2005,14 +2190,14 @@ static float inverse_db_table[256] = // ... also, isn't the whole point of Bresenham's algorithm to NOT // have to divide in the setup? sigh. #ifndef STB_VORBIS_NO_DEFER_FLOOR -#define LINE_OP(a,b) a *= b +#define LINE_OP(a, b) a *= b #else -#define LINE_OP(a,b) a = b +#define LINE_OP(a, b) a = b #endif #ifdef STB_VORBIS_DIVIDE_TABLE -#define DIVTAB_NUMER 32 -#define DIVTAB_DENOM 64 +#define DIVTAB_NUMER 32 +#define DIVTAB_DENOM 64 int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB #endif @@ -2022,45 +2207,56 @@ static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y int adx = x1 - x0; int ady = abs(dy); int base; - int x=x0,y=y0; + int x = x0, y = y0; int err = 0; int sy; #ifdef STB_VORBIS_DIVIDE_TABLE - if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) { - if (dy < 0) { + if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) + { + if (dy < 0) + { base = -integer_divide_table[ady][adx]; - sy = base-1; - } else { - base = integer_divide_table[ady][adx]; - sy = base+1; + sy = base - 1; } - } else { + else + { + base = integer_divide_table[ady][adx]; + sy = base + 1; + } + } + else + { base = dy / adx; if (dy < 0) sy = base - 1; else - sy = base+1; + sy = base + 1; } #else base = dy / adx; if (dy < 0) sy = base - 1; else - sy = base+1; + sy = base + 1; #endif ady -= abs(base) * adx; - if (x1 > n) x1 = n; - if (x < x1) { - LINE_OP(output[x], inverse_db_table[y&255]); - for (++x; x < x1; ++x) { + if (x1 > n) + x1 = n; + if (x < x1) + { + LINE_OP(output[x], inverse_db_table[y & 255]); + for (++x; x < x1; ++x) + { err += ady; - if (err >= adx) { + if (err >= adx) + { err -= adx; y += sy; - } else + } + else y += base; - LINE_OP(output[x], inverse_db_table[y&255]); + LINE_OP(output[x], inverse_db_table[y & 255]); } } } @@ -2068,14 +2264,18 @@ static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype) { int k; - if (rtype == 0) { + if (rtype == 0) + { int step = n / book->dimensions; - for (k=0; k < step; ++k) - if (!codebook_decode_step(f, book, target+offset+k, n-offset-k, step)) + for (k = 0; k < step; ++k) + if (!codebook_decode_step(f, book, target + offset + k, n - offset - k, step)) return FALSE; - } else { - for (k=0; k < n; ) { - if (!codebook_decode(f, book, target+offset, n-k)) + } + else + { + for (k = 0; k < n;) + { + if (!codebook_decode(f, book, target + offset, n - k)) return FALSE; k += book->dimensions; offset += book->dimensions; @@ -2088,123 +2288,144 @@ static int residue_decode(vorb *f, Codebook *book, float *target, int offset, in // specification: "Correct per-vector decode length is [n]/2" static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode) { - int i,j,pass; + int i, j, pass; Residue *r = f->residue_config + rn; int rtype = f->residue_types[rn]; int c = r->classbook; int classwords = f->codebooks[c].dimensions; - unsigned int actual_size = rtype == 2 ? n*2 : n; + unsigned int actual_size = rtype == 2 ? n * 2 : n; unsigned int limit_r_begin = (r->begin < actual_size ? r->begin : actual_size); - unsigned int limit_r_end = (r->end < actual_size ? r->end : actual_size); + unsigned int limit_r_end = (r->end < actual_size ? r->end : actual_size); int n_read = limit_r_end - limit_r_begin; int part_read = n_read / r->part_size; int temp_alloc_point = temp_alloc_save(f); - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE - uint8 ***part_classdata = (uint8 ***) temp_block_array(f,f->channels, part_read * sizeof(**part_classdata)); - #else - int **classifications = (int **) temp_block_array(f,f->channels, part_read * sizeof(**classifications)); - #endif +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + uint8 ***part_classdata = (uint8 ***)temp_block_array(f, f->channels, part_read * sizeof(**part_classdata)); +#else + int **classifications = (int **)temp_block_array(f, f->channels, part_read * sizeof(**classifications)); +#endif CHECK(f); - for (i=0; i < ch; ++i) + for (i = 0; i < ch; ++i) if (!do_not_decode[i]) memset(residue_buffers[i], 0, sizeof(float) * n); - if (rtype == 2 && ch != 1) { - for (j=0; j < ch; ++j) + if (rtype == 2 && ch != 1) + { + for (j = 0; j < ch; ++j) if (!do_not_decode[j]) break; if (j == ch) goto done; - for (pass=0; pass < 8; ++pass) { + for (pass = 0; pass < 8; ++pass) + { int pcount = 0, class_set = 0; - if (ch == 2) { - while (pcount < part_read) { - int z = r->begin + pcount*r->part_size; - int c_inter = (z & 1), p_inter = z>>1; - if (pass == 0) { - Codebook *c = f->codebooks+r->classbook; + if (ch == 2) + { + while (pcount < part_read) + { + int z = r->begin + pcount * r->part_size; + int c_inter = (z & 1), p_inter = z >> 1; + if (pass == 0) + { + Codebook *c = f->codebooks + r->classbook; int q; - DECODE(q,f,c); - if (q == EOP) goto done; - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + DECODE(q, f, c); + if (q == EOP) + goto done; +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE part_classdata[0][class_set] = r->classdata[q]; - #else - for (i=classwords-1; i >= 0; --i) { - classifications[0][i+pcount] = q % r->classifications; +#else + for (i = classwords - 1; i >= 0; --i) + { + classifications[0][i + pcount] = q % r->classifications; q /= r->classifications; } - #endif +#endif } - for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { - int z = r->begin + pcount*r->part_size; - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) + { + int z = r->begin + pcount * r->part_size; +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE int c = part_classdata[0][class_set][i]; - #else +#else int c = classifications[0][pcount]; - #endif +#endif int b = r->residue_books[c][pass]; - if (b >= 0) { + if (b >= 0) + { Codebook *book = f->codebooks + b; - #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK +#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) goto done; - #else +#else // saves 1% if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) goto done; - #endif - } else { +#endif + } + else + { z += r->part_size; c_inter = z & 1; p_inter = z >> 1; } } - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE ++class_set; - #endif +#endif } - } else if (ch > 2) { - while (pcount < part_read) { - int z = r->begin + pcount*r->part_size; - int c_inter = z % ch, p_inter = z/ch; - if (pass == 0) { - Codebook *c = f->codebooks+r->classbook; + } + else if (ch > 2) + { + while (pcount < part_read) + { + int z = r->begin + pcount * r->part_size; + int c_inter = z % ch, p_inter = z / ch; + if (pass == 0) + { + Codebook *c = f->codebooks + r->classbook; int q; - DECODE(q,f,c); - if (q == EOP) goto done; - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + DECODE(q, f, c); + if (q == EOP) + goto done; +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE part_classdata[0][class_set] = r->classdata[q]; - #else - for (i=classwords-1; i >= 0; --i) { - classifications[0][i+pcount] = q % r->classifications; +#else + for (i = classwords - 1; i >= 0; --i) + { + classifications[0][i + pcount] = q % r->classifications; q /= r->classifications; } - #endif +#endif } - for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { - int z = r->begin + pcount*r->part_size; - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) + { + int z = r->begin + pcount * r->part_size; +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE int c = part_classdata[0][class_set][i]; - #else +#else int c = classifications[0][pcount]; - #endif +#endif int b = r->residue_books[c][pass]; - if (b >= 0) { + if (b >= 0) + { Codebook *book = f->codebooks + b; if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) goto done; - } else { + } + else + { z += r->part_size; c_inter = z % ch; p_inter = z / ch; } } - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE ++class_set; - #endif +#endif } } } @@ -2212,37 +2433,48 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int } CHECK(f); - for (pass=0; pass < 8; ++pass) { - int pcount = 0, class_set=0; - while (pcount < part_read) { - if (pass == 0) { - for (j=0; j < ch; ++j) { - if (!do_not_decode[j]) { - Codebook *c = f->codebooks+r->classbook; + for (pass = 0; pass < 8; ++pass) + { + int pcount = 0, class_set = 0; + while (pcount < part_read) + { + if (pass == 0) + { + for (j = 0; j < ch; ++j) + { + if (!do_not_decode[j]) + { + Codebook *c = f->codebooks + r->classbook; int temp; - DECODE(temp,f,c); - if (temp == EOP) goto done; - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + DECODE(temp, f, c); + if (temp == EOP) + goto done; +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE part_classdata[j][class_set] = r->classdata[temp]; - #else - for (i=classwords-1; i >= 0; --i) { - classifications[j][i+pcount] = temp % r->classifications; +#else + for (i = classwords - 1; i >= 0; --i) + { + classifications[j][i + pcount] = temp % r->classifications; temp /= r->classifications; } - #endif +#endif } } } - for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) { - for (j=0; j < ch; ++j) { - if (!do_not_decode[j]) { - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) + { + for (j = 0; j < ch; ++j) + { + if (!do_not_decode[j]) + { +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE int c = part_classdata[j][class_set][i]; - #else +#else int c = classifications[j][pcount]; - #endif +#endif int b = r->residue_books[c][pass]; - if (b >= 0) { + if (b >= 0) + { float *target = residue_buffers[j]; int offset = r->begin + pcount * r->part_size; int n = r->part_size; @@ -2253,22 +2485,21 @@ static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int } } } - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE ++class_set; - #endif +#endif } } - done: +done: CHECK(f); - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE - temp_free(f,part_classdata); - #else - temp_free(f,classifications); - #endif - temp_alloc_restore(f,temp_alloc_point); +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + temp_free(f, part_classdata); +#else + temp_free(f, classifications); +#endif + temp_alloc_restore(f, temp_alloc_point); } - #if 0 // slow way for debugging void inverse_mdct_slow(float *buffer, int n) @@ -2297,17 +2528,18 @@ void inverse_mdct_slow(float *buffer, int n) void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) { float mcos[16384]; - int i,j; - int n2 = n >> 1, nmask = (n << 2) -1; - float *x = (float *) malloc(sizeof(*x) * n2); + int i, j; + int n2 = n >> 1, nmask = (n << 2) - 1; + float *x = (float *)malloc(sizeof(*x) * n2); memcpy(x, buffer, sizeof(*x) * n2); - for (i=0; i < 4*n; ++i) - mcos[i] = (float) cos(M_PI / 2 * i / n); + for (i = 0; i < 4 * n; ++i) + mcos[i] = (float)cos(M_PI / 2 * i / n); - for (i=0; i < n; ++i) { + for (i = 0; i < n; ++i) + { float acc = 0; - for (j=0; j < n2; ++j) - acc += x[j] * mcos[(2 * i + 1 + n2)*(2*j+1) & nmask]; + for (j = 0; j < n2; ++j) + acc += x[j] * mcos[(2 * i + 1 + n2) * (2 * j + 1) & nmask]; buffer[i] = acc; } free(x); @@ -2319,15 +2551,16 @@ void dct_iv_slow(float *buffer, int n) { float mcos[16384]; float x[2048]; - int i,j; + int i, j; int n2 = n >> 1, nmask = (n << 3) - 1; memcpy(x, buffer, sizeof(*x) * n); - for (i=0; i < 8*n; ++i) - mcos[i] = (float) cos(M_PI / 4 * i / n); - for (i=0; i < n; ++i) { + for (i = 0; i < 8 * n; ++i) + mcos[i] = (float)cos(M_PI / 4 * i / n); + for (i = 0; i < n; ++i) + { float acc = 0; - for (j=0; j < n; ++j) - acc += x[j] * mcos[((2 * i + 1)*(2*j+1)) & nmask]; + for (j = 0; j < n; ++j) + acc += x[j] * mcos[((2 * i + 1) * (2 * j + 1)) & nmask]; buffer[i] = acc; } } @@ -2338,11 +2571,14 @@ void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) float temp[4096]; memcpy(temp, buffer, n2 * sizeof(float)); - dct_iv_slow(temp, n2); // returns -c'-d, a-b' + dct_iv_slow(temp, n2); // returns -c'-d, a-b' - for (i=0; i < n4 ; ++i) buffer[i] = temp[i+n4]; // a-b' - for ( ; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1]; // b-a', c+d' - for ( ; i < n ; ++i) buffer[i] = -temp[i - n3_4]; // c'+d + for (i = 0; i < n4; ++i) + buffer[i] = temp[i + n4]; // a-b' + for (; i < n3_4; ++i) + buffer[i] = -temp[n3_4 - i - 1]; // b-a', c+d' + for (; i < n; ++i) + buffer[i] = -temp[i - n3_4]; // c'+d } #endif @@ -2355,29 +2591,37 @@ void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) // by Jeff Roberts... useful for performance comparison typedef struct { - int n; - int log2n; + int n; + int log2n; - float *trig; - int *bitrev; + float *trig; + int *bitrev; - float scale; + float scale; } mdct_lookup; extern void mdct_init(mdct_lookup *lookup, int n); extern void mdct_clear(mdct_lookup *l); extern void mdct_backward(mdct_lookup *init, float *in, float *out); -mdct_lookup M1,M2; +mdct_lookup M1, M2; void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) { mdct_lookup *M; - if (M1.n == n) M = &M1; - else if (M2.n == n) M = &M2; - else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; } - else { - if (M2.n) __asm int 3; + if (M1.n == n) + M = &M1; + else if (M2.n == n) + M = &M2; + else if (M1.n == 0) + { + mdct_init(&M1, n); + M = &M1; + } + else + { + if (M2.n) + __asm int 3; mdct_init(&M2, n); M = &M2; } @@ -2386,7 +2630,6 @@ void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) } #endif - // the following were split out into separate functions while optimizing; // they could be pushed back up but eh. __forceinline showed no change; // they're probably already being inlined. @@ -2397,36 +2640,37 @@ static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float int i; assert((n & 3) == 0); - for (i=(n>>2); i > 0; --i) { + for (i = (n >> 2); i > 0; --i) + { float k00_20, k01_21; - k00_20 = ee0[ 0] - ee2[ 0]; - k01_21 = ee0[-1] - ee2[-1]; - ee0[ 0] += ee2[ 0];//ee0[ 0] = ee0[ 0] + ee2[ 0]; - ee0[-1] += ee2[-1];//ee0[-1] = ee0[-1] + ee2[-1]; - ee2[ 0] = k00_20 * A[0] - k01_21 * A[1]; + k00_20 = ee0[0] - ee2[0]; + k01_21 = ee0[-1] - ee2[-1]; + ee0[0] += ee2[0]; // ee0[ 0] = ee0[ 0] + ee2[ 0]; + ee0[-1] += ee2[-1]; // ee0[-1] = ee0[-1] + ee2[-1]; + ee2[0] = k00_20 * A[0] - k01_21 * A[1]; ee2[-1] = k01_21 * A[0] + k00_20 * A[1]; A += 8; - k00_20 = ee0[-2] - ee2[-2]; - k01_21 = ee0[-3] - ee2[-3]; - ee0[-2] += ee2[-2];//ee0[-2] = ee0[-2] + ee2[-2]; - ee0[-3] += ee2[-3];//ee0[-3] = ee0[-3] + ee2[-3]; + k00_20 = ee0[-2] - ee2[-2]; + k01_21 = ee0[-3] - ee2[-3]; + ee0[-2] += ee2[-2]; // ee0[-2] = ee0[-2] + ee2[-2]; + ee0[-3] += ee2[-3]; // ee0[-3] = ee0[-3] + ee2[-3]; ee2[-2] = k00_20 * A[0] - k01_21 * A[1]; ee2[-3] = k01_21 * A[0] + k00_20 * A[1]; A += 8; - k00_20 = ee0[-4] - ee2[-4]; - k01_21 = ee0[-5] - ee2[-5]; - ee0[-4] += ee2[-4];//ee0[-4] = ee0[-4] + ee2[-4]; - ee0[-5] += ee2[-5];//ee0[-5] = ee0[-5] + ee2[-5]; + k00_20 = ee0[-4] - ee2[-4]; + k01_21 = ee0[-5] - ee2[-5]; + ee0[-4] += ee2[-4]; // ee0[-4] = ee0[-4] + ee2[-4]; + ee0[-5] += ee2[-5]; // ee0[-5] = ee0[-5] + ee2[-5]; ee2[-4] = k00_20 * A[0] - k01_21 * A[1]; ee2[-5] = k01_21 * A[0] + k00_20 * A[1]; A += 8; - k00_20 = ee0[-6] - ee2[-6]; - k01_21 = ee0[-7] - ee2[-7]; - ee0[-6] += ee2[-6];//ee0[-6] = ee0[-6] + ee2[-6]; - ee0[-7] += ee2[-7];//ee0[-7] = ee0[-7] + ee2[-7]; + k00_20 = ee0[-6] - ee2[-6]; + k01_21 = ee0[-7] - ee2[-7]; + ee0[-6] += ee2[-6]; // ee0[-6] = ee0[-6] + ee2[-6]; + ee0[-7] += ee2[-7]; // ee0[-7] = ee0[-7] + ee2[-7]; ee2[-6] = k00_20 * A[0] - k01_21 * A[1]; ee2[-7] = k01_21 * A[0] + k00_20 * A[1]; A += 8; @@ -2443,40 +2687,41 @@ static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float float *e0 = e + d0; float *e2 = e0 + k_off; - for (i=lim >> 2; i > 0; --i) { + for (i = lim >> 2; i > 0; --i) + { k00_20 = e0[-0] - e2[-0]; k01_21 = e0[-1] - e2[-1]; - e0[-0] += e2[-0];//e0[-0] = e0[-0] + e2[-0]; - e0[-1] += e2[-1];//e0[-1] = e0[-1] + e2[-1]; - e2[-0] = (k00_20)*A[0] - (k01_21) * A[1]; - e2[-1] = (k01_21)*A[0] + (k00_20) * A[1]; + e0[-0] += e2[-0]; // e0[-0] = e0[-0] + e2[-0]; + e0[-1] += e2[-1]; // e0[-1] = e0[-1] + e2[-1]; + e2[-0] = (k00_20)*A[0] - (k01_21)*A[1]; + e2[-1] = (k01_21)*A[0] + (k00_20)*A[1]; A += k1; k00_20 = e0[-2] - e2[-2]; k01_21 = e0[-3] - e2[-3]; - e0[-2] += e2[-2];//e0[-2] = e0[-2] + e2[-2]; - e0[-3] += e2[-3];//e0[-3] = e0[-3] + e2[-3]; - e2[-2] = (k00_20)*A[0] - (k01_21) * A[1]; - e2[-3] = (k01_21)*A[0] + (k00_20) * A[1]; + e0[-2] += e2[-2]; // e0[-2] = e0[-2] + e2[-2]; + e0[-3] += e2[-3]; // e0[-3] = e0[-3] + e2[-3]; + e2[-2] = (k00_20)*A[0] - (k01_21)*A[1]; + e2[-3] = (k01_21)*A[0] + (k00_20)*A[1]; A += k1; k00_20 = e0[-4] - e2[-4]; k01_21 = e0[-5] - e2[-5]; - e0[-4] += e2[-4];//e0[-4] = e0[-4] + e2[-4]; - e0[-5] += e2[-5];//e0[-5] = e0[-5] + e2[-5]; - e2[-4] = (k00_20)*A[0] - (k01_21) * A[1]; - e2[-5] = (k01_21)*A[0] + (k00_20) * A[1]; + e0[-4] += e2[-4]; // e0[-4] = e0[-4] + e2[-4]; + e0[-5] += e2[-5]; // e0[-5] = e0[-5] + e2[-5]; + e2[-4] = (k00_20)*A[0] - (k01_21)*A[1]; + e2[-5] = (k01_21)*A[0] + (k00_20)*A[1]; A += k1; k00_20 = e0[-6] - e2[-6]; k01_21 = e0[-7] - e2[-7]; - e0[-6] += e2[-6];//e0[-6] = e0[-6] + e2[-6]; - e0[-7] += e2[-7];//e0[-7] = e0[-7] + e2[-7]; - e2[-6] = (k00_20)*A[0] - (k01_21) * A[1]; - e2[-7] = (k01_21)*A[0] + (k00_20) * A[1]; + e0[-6] += e2[-6]; // e0[-6] = e0[-6] + e2[-6]; + e0[-7] += e2[-7]; // e0[-7] = e0[-7] + e2[-7]; + e2[-6] = (k00_20)*A[0] - (k01_21)*A[1]; + e2[-7] = (k01_21)*A[0] + (k00_20)*A[1]; e0 -= 8; e2 -= 8; @@ -2489,47 +2734,48 @@ static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, floa { int i; float A0 = A[0]; - float A1 = A[0+1]; - float A2 = A[0+a_off]; - float A3 = A[0+a_off+1]; - float A4 = A[0+a_off*2+0]; - float A5 = A[0+a_off*2+1]; - float A6 = A[0+a_off*3+0]; - float A7 = A[0+a_off*3+1]; + float A1 = A[0 + 1]; + float A2 = A[0 + a_off]; + float A3 = A[0 + a_off + 1]; + float A4 = A[0 + a_off * 2 + 0]; + float A5 = A[0 + a_off * 2 + 1]; + float A6 = A[0 + a_off * 3 + 0]; + float A7 = A[0 + a_off * 3 + 1]; - float k00,k11; + float k00, k11; - float *ee0 = e +i_off; - float *ee2 = ee0+k_off; + float *ee0 = e + i_off; + float *ee2 = ee0 + k_off; - for (i=n; i > 0; --i) { - k00 = ee0[ 0] - ee2[ 0]; - k11 = ee0[-1] - ee2[-1]; - ee0[ 0] = ee0[ 0] + ee2[ 0]; - ee0[-1] = ee0[-1] + ee2[-1]; - ee2[ 0] = (k00) * A0 - (k11) * A1; - ee2[-1] = (k11) * A0 + (k00) * A1; + for (i = n; i > 0; --i) + { + k00 = ee0[0] - ee2[0]; + k11 = ee0[-1] - ee2[-1]; + ee0[0] = ee0[0] + ee2[0]; + ee0[-1] = ee0[-1] + ee2[-1]; + ee2[0] = (k00)*A0 - (k11)*A1; + ee2[-1] = (k11)*A0 + (k00)*A1; - k00 = ee0[-2] - ee2[-2]; - k11 = ee0[-3] - ee2[-3]; - ee0[-2] = ee0[-2] + ee2[-2]; - ee0[-3] = ee0[-3] + ee2[-3]; - ee2[-2] = (k00) * A2 - (k11) * A3; - ee2[-3] = (k11) * A2 + (k00) * A3; + k00 = ee0[-2] - ee2[-2]; + k11 = ee0[-3] - ee2[-3]; + ee0[-2] = ee0[-2] + ee2[-2]; + ee0[-3] = ee0[-3] + ee2[-3]; + ee2[-2] = (k00)*A2 - (k11)*A3; + ee2[-3] = (k11)*A2 + (k00)*A3; - k00 = ee0[-4] - ee2[-4]; - k11 = ee0[-5] - ee2[-5]; - ee0[-4] = ee0[-4] + ee2[-4]; - ee0[-5] = ee0[-5] + ee2[-5]; - ee2[-4] = (k00) * A4 - (k11) * A5; - ee2[-5] = (k11) * A4 + (k00) * A5; + k00 = ee0[-4] - ee2[-4]; + k11 = ee0[-5] - ee2[-5]; + ee0[-4] = ee0[-4] + ee2[-4]; + ee0[-5] = ee0[-5] + ee2[-5]; + ee2[-4] = (k00)*A4 - (k11)*A5; + ee2[-5] = (k11)*A4 + (k00)*A5; - k00 = ee0[-6] - ee2[-6]; - k11 = ee0[-7] - ee2[-7]; - ee0[-6] = ee0[-6] + ee2[-6]; - ee0[-7] = ee0[-7] + ee2[-7]; - ee2[-6] = (k00) * A6 - (k11) * A7; - ee2[-7] = (k11) * A6 + (k00) * A7; + k00 = ee0[-6] - ee2[-6]; + k11 = ee0[-7] - ee2[-7]; + ee0[-6] = ee0[-6] + ee2[-6]; + ee0[-7] = ee0[-7] + ee2[-7]; + ee2[-6] = (k00)*A6 - (k11)*A7; + ee2[-7] = (k11)*A6 + (k00)*A7; ee0 -= k0; ee2 -= k0; @@ -2538,76 +2784,77 @@ static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, floa static __forceinline void iter_54(float *z) { - float k00,k11,k22,k33; - float y0,y1,y2,y3; + float k00, k11, k22, k33; + float y0, y1, y2, y3; - k00 = z[ 0] - z[-4]; - y0 = z[ 0] + z[-4]; - y2 = z[-2] + z[-6]; - k22 = z[-2] - z[-6]; + k00 = z[0] - z[-4]; + y0 = z[0] + z[-4]; + y2 = z[-2] + z[-6]; + k22 = z[-2] - z[-6]; - z[-0] = y0 + y2; // z0 + z4 + z2 + z6 - z[-2] = y0 - y2; // z0 + z4 - z2 - z6 + z[-0] = y0 + y2; // z0 + z4 + z2 + z6 + z[-2] = y0 - y2; // z0 + z4 - z2 - z6 // done with y0,y2 - k33 = z[-3] - z[-7]; + k33 = z[-3] - z[-7]; - z[-4] = k00 + k33; // z0 - z4 + z3 - z7 - z[-6] = k00 - k33; // z0 - z4 - z3 + z7 + z[-4] = k00 + k33; // z0 - z4 + z3 - z7 + z[-6] = k00 - k33; // z0 - z4 - z3 + z7 // done with k33 - k11 = z[-1] - z[-5]; - y1 = z[-1] + z[-5]; - y3 = z[-3] + z[-7]; + k11 = z[-1] - z[-5]; + y1 = z[-1] + z[-5]; + y3 = z[-3] + z[-7]; - z[-1] = y1 + y3; // z1 + z5 + z3 + z7 - z[-3] = y1 - y3; // z1 + z5 - z3 - z7 - z[-5] = k11 - k22; // z1 - z5 + z2 - z6 - z[-7] = k11 + k22; // z1 - z5 - z2 + z6 + z[-1] = y1 + y3; // z1 + z5 + z3 + z7 + z[-3] = y1 - y3; // z1 + z5 - z3 - z7 + z[-5] = k11 - k22; // z1 - z5 + z2 - z6 + z[-7] = k11 + k22; // z1 - z5 - z2 + z6 } static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n) { int a_off = base_n >> 3; - float A2 = A[0+a_off]; + float A2 = A[0 + a_off]; float *z = e + i_off; float *base = z - 16 * n; - while (z > base) { - float k00,k11; + while (z > base) + { + float k00, k11; - k00 = z[-0] - z[-8]; - k11 = z[-1] - z[-9]; + k00 = z[-0] - z[-8]; + k11 = z[-1] - z[-9]; z[-0] = z[-0] + z[-8]; z[-1] = z[-1] + z[-9]; - z[-8] = k00; - z[-9] = k11 ; + z[-8] = k00; + z[-9] = k11; - k00 = z[ -2] - z[-10]; - k11 = z[ -3] - z[-11]; - z[ -2] = z[ -2] + z[-10]; - z[ -3] = z[ -3] + z[-11]; - z[-10] = (k00+k11) * A2; - z[-11] = (k11-k00) * A2; + k00 = z[-2] - z[-10]; + k11 = z[-3] - z[-11]; + z[-2] = z[-2] + z[-10]; + z[-3] = z[-3] + z[-11]; + z[-10] = (k00 + k11) * A2; + z[-11] = (k11 - k00) * A2; - k00 = z[-12] - z[ -4]; // reverse to avoid a unary negation - k11 = z[ -5] - z[-13]; - z[ -4] = z[ -4] + z[-12]; - z[ -5] = z[ -5] + z[-13]; + k00 = z[-12] - z[-4]; // reverse to avoid a unary negation + k11 = z[-5] - z[-13]; + z[-4] = z[-4] + z[-12]; + z[-5] = z[-5] + z[-13]; z[-12] = k11; z[-13] = k00; - k00 = z[-14] - z[ -6]; // reverse to avoid a unary negation - k11 = z[ -7] - z[-15]; - z[ -6] = z[ -6] + z[-14]; - z[ -7] = z[ -7] + z[-15]; - z[-14] = (k00+k11) * A2; - z[-15] = (k00-k11) * A2; + k00 = z[-14] - z[-6]; // reverse to avoid a unary negation + k11 = z[-7] - z[-15]; + z[-6] = z[-6] + z[-14]; + z[-7] = z[-7] + z[-15]; + z[-14] = (k00 + k11) * A2; + z[-15] = (k00 - k11) * A2; iter_54(z); - iter_54(z-8); + iter_54(z - 8); z -= 16; } } @@ -2618,8 +2865,8 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) int ld; // @OPTIMIZE: reduce register pressure by using fewer variables? int save_point = temp_alloc_save(f); - float *buf2 = (float *) temp_alloc(f, n2 * sizeof(*buf2)); - float *u=NULL,*v=NULL; + float *buf2 = (float *)temp_alloc(f, n2 * sizeof(*buf2)); + float *u = NULL, *v = NULL; // twiddle factors float *A = f->A[blocktype]; @@ -2628,7 +2875,6 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) // kernel from paper - // merged: // copy and reflect spectral data // step 0 @@ -2643,23 +2889,25 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) // are 1/2 too small, and need to be compensated for. { - float *d,*e, *AA, *e_stop; - d = &buf2[n2-2]; + float *d, *e, *AA, *e_stop; + d = &buf2[n2 - 2]; AA = A; e = &buffer[0]; e_stop = &buffer[n2]; - while (e != e_stop) { - d[1] = (e[0] * AA[0] - e[2]*AA[1]); - d[0] = (e[0] * AA[1] + e[2]*AA[0]); + while (e != e_stop) + { + d[1] = (e[0] * AA[0] - e[2] * AA[1]); + d[0] = (e[0] * AA[1] + e[2] * AA[0]); d -= 2; AA += 2; e += 4; } - e = &buffer[n2-3]; - while (d >= buf2) { - d[1] = (-e[2] * AA[0] - -e[0]*AA[1]); - d[0] = (-e[2] * AA[1] + -e[0]*AA[0]); + e = &buffer[n2 - 3]; + while (d >= buf2) + { + d[1] = (-e[2] * AA[0] - -e[0] * AA[1]); + d[0] = (-e[2] * AA[1] + -e[0] * AA[0]); d -= 2; AA += 2; e -= 4; @@ -2677,8 +2925,8 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) // this could be in place, but the data ends up in the wrong // place... _somebody_'s got to swap it, so this is nominated { - float *AA = &A[n2-8]; - float *d0,*d1, *e0, *e1; + float *AA = &A[n2 - 8]; + float *d0, *d1, *e0, *e1; e0 = &v[n4]; e1 = &v[0]; @@ -2686,22 +2934,23 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) d0 = &u[n4]; d1 = &u[0]; - while (AA >= A) { + while (AA >= A) + { float v40_20, v41_21; v41_21 = e0[1] - e1[1]; v40_20 = e0[0] - e1[0]; - d0[1] = e0[1] + e1[1]; - d0[0] = e0[0] + e1[0]; - d1[1] = v41_21*AA[4] - v40_20*AA[5]; - d1[0] = v40_20*AA[4] + v41_21*AA[5]; + d0[1] = e0[1] + e1[1]; + d0[0] = e0[0] + e1[0]; + d1[1] = v41_21 * AA[4] - v40_20 * AA[5]; + d1[0] = v40_20 * AA[4] + v41_21 * AA[5]; v41_21 = e0[3] - e1[3]; v40_20 = e0[2] - e1[2]; - d0[3] = e0[3] + e1[3]; - d0[2] = e0[2] + e1[2]; - d1[3] = v41_21*AA[0] - v40_20*AA[1]; - d1[2] = v40_20*AA[0] + v41_21*AA[1]; + d0[3] = e0[3] + e1[3]; + d0[2] = e0[2] + e1[2]; + d1[3] = v41_21 * AA[0] - v40_20 * AA[1]; + d1[2] = v40_20 * AA[0] + v41_21 * AA[1]; AA -= 8; @@ -2723,34 +2972,37 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) // switch between them halfway. // this is iteration 0 of step 3 - imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*0, -(n >> 3), A); - imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*1, -(n >> 3), A); + imdct_step3_iter0_loop(n >> 4, u, n2 - 1 - n4 * 0, -(n >> 3), A); + imdct_step3_iter0_loop(n >> 4, u, n2 - 1 - n4 * 1, -(n >> 3), A); // this is iteration 1 of step 3 - imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*0, -(n >> 4), A, 16); - imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*1, -(n >> 4), A, 16); - imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*2, -(n >> 4), A, 16); - imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*3, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 0, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 1, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 2, -(n >> 4), A, 16); + imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 3, -(n >> 4), A, 16); - l=2; - for (; l < (ld-3)>>1; ++l) { - int k0 = n >> (l+2), k0_2 = k0>>1; - int lim = 1 << (l+1); + l = 2; + for (; l < (ld - 3) >> 1; ++l) + { + int k0 = n >> (l + 2), k0_2 = k0 >> 1; + int lim = 1 << (l + 1); int i; - for (i=0; i < lim; ++i) - imdct_step3_inner_r_loop(n >> (l+4), u, n2-1 - k0*i, -k0_2, A, 1 << (l+3)); + for (i = 0; i < lim; ++i) + imdct_step3_inner_r_loop(n >> (l + 4), u, n2 - 1 - k0 * i, -k0_2, A, 1 << (l + 3)); } - for (; l < ld-6; ++l) { - int k0 = n >> (l+2), k1 = 1 << (l+3), k0_2 = k0>>1; - int rlim = n >> (l+6), r; - int lim = 1 << (l+1); + for (; l < ld - 6; ++l) + { + int k0 = n >> (l + 2), k1 = 1 << (l + 3), k0_2 = k0 >> 1; + int rlim = n >> (l + 6), r; + int lim = 1 << (l + 1); int i_off; float *A0 = A; - i_off = n2-1; - for (r=rlim; r > 0; --r) { + i_off = n2 - 1; + for (r = rlim; r > 0; --r) + { imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0); - A0 += k1*4; + A0 += k1 * 4; i_off -= 8; } } @@ -2760,7 +3012,7 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) // the big win comes from getting rid of needless flops // due to the constants on pass 5 & 4 being all 1 and 0; // combining them to be simultaneous to improve cache made little difference - imdct_step3_inner_s_loop_ld654(n >> 5, u, n2-1, A, n); + imdct_step3_inner_s_loop_ld654(n >> 5, u, n2 - 1, A, n); // output is u @@ -2773,22 +3025,23 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) // fact that's not what my testing showed. (That is, with // j = bitreverse(i), do you read i and write j, or read j and write i.) - float *d0 = &v[n4-4]; - float *d1 = &v[n2-4]; - while (d0 >= v) { + float *d0 = &v[n4 - 4]; + float *d1 = &v[n2 - 4]; + while (d0 >= v) + { int k4; k4 = bitrev[0]; - d1[3] = u[k4+0]; - d1[2] = u[k4+1]; - d0[3] = u[k4+2]; - d0[2] = u[k4+3]; + d1[3] = u[k4 + 0]; + d1[2] = u[k4 + 1]; + d0[3] = u[k4 + 2]; + d0[2] = u[k4 + 3]; k4 = bitrev[1]; - d1[1] = u[k4+0]; - d1[0] = u[k4+1]; - d0[1] = u[k4+2]; - d0[0] = u[k4+3]; + d1[1] = u[k4 + 0]; + d1[0] = u[k4 + 1]; + d0[1] = u[k4 + 2]; + d0[0] = u[k4 + 3]; d0 -= 4; d1 -= 4; @@ -2797,7 +3050,6 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) } // (paper output is u, now v) - // data must be in buf2 assert(v == buf2); @@ -2810,17 +3062,18 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) d = v; e = v + n2 - 4; - while (d < e) { - float a02,a11,b0,b1,b2,b3; + while (d < e) + { + float a02, a11, b0, b1, b2, b3; a02 = d[0] - e[2]; a11 = d[1] + e[3]; - b0 = C[1]*a02 + C[0]*a11; - b1 = C[1]*a11 - C[0]*a02; + b0 = C[1] * a02 + C[0] * a11; + b1 = C[1] * a11 - C[0] * a02; - b2 = d[0] + e[ 2]; - b3 = d[1] - e[ 3]; + b2 = d[0] + e[2]; + b3 = d[1] - e[3]; d[0] = b2 + b0; d[1] = b3 + b1; @@ -2830,11 +3083,11 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) a02 = d[2] - e[0]; a11 = d[3] + e[1]; - b0 = C[3]*a02 + C[2]*a11; - b1 = C[3]*a11 - C[2]*a02; + b0 = C[3] * a02 + C[2] * a11; + b1 = C[3] * a11 - C[2] * a02; - b2 = d[2] + e[ 0]; - b3 = d[3] - e[ 1]; + b2 = d[2] + e[0]; + b3 = d[3] - e[1]; d[2] = b2 + b0; d[3] = b3 + b1; @@ -2849,7 +3102,6 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) // data must be in buf2 - // step 8+decode (paper output is X, now buffer) // this generates pairs of data a la 8 and pushes them directly through // the decode kernel (pushing rather than pulling) to avoid having @@ -2858,48 +3110,49 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) // this cannot POSSIBLY be in place, so we refer to the buffers directly { - float *d0,*d1,*d2,*d3; + float *d0, *d1, *d2, *d3; float *B = f->B[blocktype] + n2 - 8; float *e = buf2 + n2 - 8; d0 = &buffer[0]; - d1 = &buffer[n2-4]; + d1 = &buffer[n2 - 4]; d2 = &buffer[n2]; - d3 = &buffer[n-4]; - while (e >= v) { - float p0,p1,p2,p3; + d3 = &buffer[n - 4]; + while (e >= v) + { + float p0, p1, p2, p3; - p3 = e[6]*B[7] - e[7]*B[6]; - p2 = -e[6]*B[6] - e[7]*B[7]; + p3 = e[6] * B[7] - e[7] * B[6]; + p2 = -e[6] * B[6] - e[7] * B[7]; - d0[0] = p3; - d1[3] = - p3; - d2[0] = p2; - d3[3] = p2; + d0[0] = p3; + d1[3] = -p3; + d2[0] = p2; + d3[3] = p2; - p1 = e[4]*B[5] - e[5]*B[4]; - p0 = -e[4]*B[4] - e[5]*B[5]; + p1 = e[4] * B[5] - e[5] * B[4]; + p0 = -e[4] * B[4] - e[5] * B[5]; - d0[1] = p1; - d1[2] = - p1; - d2[1] = p0; - d3[2] = p0; + d0[1] = p1; + d1[2] = -p1; + d2[1] = p0; + d3[2] = p0; - p3 = e[2]*B[3] - e[3]*B[2]; - p2 = -e[2]*B[2] - e[3]*B[3]; + p3 = e[2] * B[3] - e[3] * B[2]; + p2 = -e[2] * B[2] - e[3] * B[3]; - d0[2] = p3; - d1[1] = - p3; - d2[2] = p2; - d3[1] = p2; + d0[2] = p3; + d1[1] = -p3; + d2[2] = p2; + d3[1] = p2; - p1 = e[0]*B[1] - e[1]*B[0]; - p0 = -e[0]*B[0] - e[1]*B[1]; + p1 = e[0] * B[1] - e[1] * B[0]; + p0 = -e[0] * B[0] - e[1] * B[1]; - d0[3] = p1; - d1[0] = - p1; - d2[3] = p0; - d3[0] = p0; + d0[3] = p1; + d1[0] = -p1; + d2[3] = p0; + d3[0] = p0; B -= 8; e -= 8; @@ -2910,8 +3163,8 @@ static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) } } - temp_free(f,buf2); - temp_alloc_restore(f,save_point); + temp_free(f, buf2); + temp_alloc_restore(f, save_point); } #if 0 @@ -3045,8 +3298,10 @@ void inverse_mdct_naive(float *buffer, int n) static float *get_window(vorb *f, int len) { len <<= 1; - if (len == f->blocksize_0) return f->window[0]; - if (len == f->blocksize_1) return f->window[1]; + if (len == f->blocksize_0) + return f->window[0]; + if (len == f->blocksize_1) + return f->window[1]; return NULL; } @@ -3060,31 +3315,36 @@ static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *f int n2 = n >> 1; int s = map->chan[i].mux, floor; floor = map->submap_floor[s]; - if (f->floor_types[floor] == 0) { + if (f->floor_types[floor] == 0) + { return error(f, VORBIS_invalid_stream); - } else { + } + else + { Floor1 *g = &f->floor_config[floor].floor1; - int j,q; + int j, q; int lx = 0, ly = finalY[0] * g->floor1_multiplier; - for (q=1; q < g->values; ++q) { + for (q = 1; q < g->values; ++q) + { j = g->sorted_order[q]; - #ifndef STB_VORBIS_NO_DEFER_FLOOR +#ifndef STB_VORBIS_NO_DEFER_FLOOR if (finalY[j] >= 0) - #else +#else if (step2_flag[j]) - #endif +#endif { int hy = finalY[j] * g->floor1_multiplier; int hx = g->Xlist[j]; if (lx != hx) - draw_line(target, lx,ly, hx,hy, n2); + draw_line(target, lx, ly, hx, hy, n2); CHECK(f); lx = hx, ly = hy; } } - if (lx < n2) { + if (lx < n2) + { // optimization of: draw_line(target, lx,ly, n,ly, n2); - for (j=lx; j < n2; ++j) + for (j = lx; j < n2; ++j) LINE_OP(target[j], inverse_db_table[ly]); CHECK(f); } @@ -3112,51 +3372,65 @@ static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, in int i, n, prev, next, window_center; f->channel_buffer_start = f->channel_buffer_end = 0; - retry: - if (f->eof) return FALSE; +retry: + if (f->eof) + return FALSE; if (!maybe_start_packet(f)) return FALSE; // check packet type - if (get_bits(f,1) != 0) { + if (get_bits(f, 1) != 0) + { if (IS_PUSH_MODE(f)) - return error(f,VORBIS_bad_packet_type); - while (EOP != get8_packet(f)); + return error(f, VORBIS_bad_packet_type); + while (EOP != get8_packet(f)) + ; goto retry; } if (f->alloc.alloc_buffer) assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); - i = get_bits(f, ilog(f->mode_count-1)); - if (i == EOP) return FALSE; - if (i >= f->mode_count) return FALSE; + i = get_bits(f, ilog(f->mode_count - 1)); + if (i == EOP) + return FALSE; + if (i >= f->mode_count) + return FALSE; *mode = i; m = f->mode_config + i; - if (m->blockflag) { + if (m->blockflag) + { n = f->blocksize_1; - prev = get_bits(f,1); - next = get_bits(f,1); - } else { + prev = get_bits(f, 1); + next = get_bits(f, 1); + } + else + { prev = next = 0; n = f->blocksize_0; } -// WINDOWING + // WINDOWING window_center = n >> 1; - if (m->blockflag && !prev) { + if (m->blockflag && !prev) + { *p_left_start = (n - f->blocksize_0) >> 2; - *p_left_end = (n + f->blocksize_0) >> 2; - } else { - *p_left_start = 0; - *p_left_end = window_center; + *p_left_end = (n + f->blocksize_0) >> 2; } - if (m->blockflag && !next) { - *p_right_start = (n*3 - f->blocksize_0) >> 2; - *p_right_end = (n*3 + f->blocksize_0) >> 2; - } else { + else + { + *p_left_start = 0; + *p_left_end = window_center; + } + if (m->blockflag && !next) + { + *p_right_start = (n * 3 - f->blocksize_0) >> 2; + *p_right_end = (n * 3 + f->blocksize_0) >> 2; + } + else + { *p_right_start = window_center; - *p_right_end = n; + *p_right_end = n; } return TRUE; @@ -3165,66 +3439,78 @@ static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, in static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, int left_end, int right_start, int right_end, int *p_left) { Mapping *map; - int i,j,k,n,n2; + int i, j, k, n, n2; int zero_channel[256]; int really_zero_channel[256]; -// WINDOWING + // WINDOWING n = f->blocksize[m->blockflag]; map = &f->mapping[m->mapping]; -// FLOORS + // FLOORS n2 = n >> 1; CHECK(f); - for (i=0; i < f->channels; ++i) { + for (i = 0; i < f->channels; ++i) + { int s = map->chan[i].mux, floor; zero_channel[i] = FALSE; floor = map->submap_floor[s]; - if (f->floor_types[floor] == 0) { + if (f->floor_types[floor] == 0) + { return error(f, VORBIS_invalid_stream); - } else { + } + else + { Floor1 *g = &f->floor_config[floor].floor1; - if (get_bits(f, 1)) { + if (get_bits(f, 1)) + { short *finalY; uint8 step2_flag[256]; - static int range_list[4] = { 256, 128, 86, 64 }; - int range = range_list[g->floor1_multiplier-1]; + static int range_list[4] = {256, 128, 86, 64}; + int range = range_list[g->floor1_multiplier - 1]; int offset = 2; finalY = f->finalY[i]; - finalY[0] = get_bits(f, ilog(range)-1); - finalY[1] = get_bits(f, ilog(range)-1); - for (j=0; j < g->partitions; ++j) { + finalY[0] = get_bits(f, ilog(range) - 1); + finalY[1] = get_bits(f, ilog(range) - 1); + for (j = 0; j < g->partitions; ++j) + { int pclass = g->partition_class_list[j]; int cdim = g->class_dimensions[pclass]; int cbits = g->class_subclasses[pclass]; - int csub = (1 << cbits)-1; + int csub = (1 << cbits) - 1; int cval = 0; - if (cbits) { + if (cbits) + { Codebook *c = f->codebooks + g->class_masterbooks[pclass]; - DECODE(cval,f,c); + DECODE(cval, f, c); } - for (k=0; k < cdim; ++k) { + for (k = 0; k < cdim; ++k) + { int book = g->subclass_books[pclass][cval & csub]; cval = cval >> cbits; - if (book >= 0) { + if (book >= 0) + { int temp; Codebook *c = f->codebooks + book; - DECODE(temp,f,c); + DECODE(temp, f, c); finalY[offset++] = temp; - } else + } + else finalY[offset++] = 0; } } - if (f->valid_bits == INVALID_BITS) goto error; // behavior according to spec + if (f->valid_bits == INVALID_BITS) + goto error; // behavior according to spec step2_flag[0] = step2_flag[1] = 1; - for (j=2; j < g->values; ++j) { + for (j = 2; j < g->values; ++j) + { int low, high, pred, highroom, lowroom, room, val; low = g->neighbors[j][0]; high = g->neighbors[j][1]; - //neighbors(g->Xlist, j, &low, &high); + // neighbors(g->Xlist, j, &low, &high); pred = predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]); val = finalY[j]; highroom = range - pred; @@ -3233,7 +3519,8 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, room = highroom * 2; else room = lowroom * 2; - if (val) { + if (val) + { step2_flag[low] = step2_flag[high] = 1; step2_flag[j] = 1; if (val >= room) @@ -3241,12 +3528,13 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, finalY[j] = val - lowroom + pred; else finalY[j] = pred - val + highroom - 1; + else if (val & 1) + finalY[j] = pred - ((val + 1) >> 1); else - if (val & 1) - finalY[j] = pred - ((val+1)>>1); - else - finalY[j] = pred + (val>>1); - } else { + finalY[j] = pred + (val >> 1); + } + else + { step2_flag[j] = 0; finalY[j] = pred; } @@ -3256,13 +3544,16 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag); #else // defer final floor computation until _after_ residue - for (j=0; j < g->values; ++j) { + for (j = 0; j < g->values; ++j) + { if (!step2_flag[j]) finalY[j] = -1; } #endif - } else { - error: + } + else + { + error: zero_channel[i] = TRUE; } // So we just defer everything else to later @@ -3278,24 +3569,31 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, // re-enable coupled channels if necessary memcpy(really_zero_channel, zero_channel, sizeof(really_zero_channel[0]) * f->channels); - for (i=0; i < map->coupling_steps; ++i) - if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) { + for (i = 0; i < map->coupling_steps; ++i) + if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) + { zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE; } CHECK(f); -// RESIDUE DECODE - for (i=0; i < map->submaps; ++i) { + // RESIDUE DECODE + for (i = 0; i < map->submaps; ++i) + { float *residue_buffers[STB_VORBIS_MAX_CHANNELS]; int r; uint8 do_not_decode[256]; int ch = 0; - for (j=0; j < f->channels; ++j) { - if (map->chan[j].mux == i) { - if (zero_channel[j]) { + for (j = 0; j < f->channels; ++j) + { + if (map->chan[j].mux == i) + { + if (zero_channel[j]) + { do_not_decode[ch] = TRUE; residue_buffers[ch] = NULL; - } else { + } + else + { do_not_decode[ch] = FALSE; residue_buffers[ch] = f->channel_buffers[j]; } @@ -3310,23 +3608,24 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); CHECK(f); -// INVERSE COUPLING - for (i = map->coupling_steps-1; i >= 0; --i) { + // INVERSE COUPLING + for (i = map->coupling_steps - 1; i >= 0; --i) + { int n2 = n >> 1; float *m = f->channel_buffers[map->chan[i].magnitude]; - float *a = f->channel_buffers[map->chan[i].angle ]; - for (j=0; j < n2; ++j) { - float a2,m2; + float *a = f->channel_buffers[map->chan[i].angle]; + for (j = 0; j < n2; ++j) + { + float a2, m2; if (m[j] > 0) if (a[j] > 0) m2 = m[j], a2 = m[j] - a[j]; else a2 = m[j], m2 = m[j] + a[j]; + else if (a[j] > 0) + m2 = m[j], a2 = m[j] + a[j]; else - if (a[j] > 0) - m2 = m[j], a2 = m[j] + a[j]; - else - a2 = m[j], m2 = m[j] - a[j]; + a2 = m[j], m2 = m[j] - a[j]; m[j] = m2; a[j] = a2; } @@ -3335,27 +3634,35 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, // finish decoding the floors #ifndef STB_VORBIS_NO_DEFER_FLOOR - for (i=0; i < f->channels; ++i) { - if (really_zero_channel[i]) { + for (i = 0; i < f->channels; ++i) + { + if (really_zero_channel[i]) + { memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2); - } else { + } + else + { do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i], NULL); } } #else - for (i=0; i < f->channels; ++i) { - if (really_zero_channel[i]) { + for (i = 0; i < f->channels; ++i) + { + if (really_zero_channel[i]) + { memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2); - } else { - for (j=0; j < n2; ++j) + } + else + { + for (j = 0; j < n2; ++j) f->channel_buffers[i][j] *= f->floor_buffers[i][j]; } } #endif -// INVERSE MDCT + // INVERSE MDCT CHECK(f); - for (i=0; i < f->channels; ++i) + for (i = 0; i < f->channels; ++i) inverse_mdct(f->channel_buffers[i], n, f, m->blockflag); CHECK(f); @@ -3363,7 +3670,8 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, // and want to flush to get to the next packet flush_packet(f); - if (f->first_decode) { + if (f->first_decode) + { // assume we start so first non-discarded sample is sample 0 // this isn't to spec, but spec would require us to read ahead // and decode the size of all current frames--could be done, @@ -3374,17 +3682,24 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, f->discard_samples_deferred = n - right_end; f->current_loc_valid = TRUE; f->first_decode = FALSE; - } else if (f->discard_samples_deferred) { - if (f->discard_samples_deferred >= right_start - left_start) { + } + else if (f->discard_samples_deferred) + { + if (f->discard_samples_deferred >= right_start - left_start) + { f->discard_samples_deferred -= (right_start - left_start); left_start = right_start; *p_left = left_start; - } else { + } + else + { left_start += f->discard_samples_deferred; *p_left = left_start; f->discard_samples_deferred = 0; } - } else if (f->previous_length == 0 && f->current_loc_valid) { + } + else if (f->previous_length == 0 && f->current_loc_valid) + { // we're recovering from a seek... that means we're going to discard // the samples from this packet even though we know our position from // the last page header, so we need to update the position based on @@ -3394,20 +3709,27 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, } // check if we have ogg information about the sample # for this packet - if (f->last_seg_which == f->end_seg_with_known_loc) { + if (f->last_seg_which == f->end_seg_with_known_loc) + { // if we have a valid current loc, and this is final: - if (f->current_loc_valid && (f->page_flag & PAGEFLAG_last_page)) { + if (f->current_loc_valid && (f->page_flag & PAGEFLAG_last_page)) + { uint32 current_end = f->known_loc_for_packet; // then let's infer the size of the (probably) short final frame - if (current_end < f->current_loc + (right_end-left_start)) { - if (current_end < f->current_loc) { + if (current_end < f->current_loc + (right_end - left_start)) + { + if (current_end < f->current_loc) + { // negative truncation, that's impossible! *len = 0; - } else { + } + else + { *len = current_end - f->current_loc; } *len += left_start; // this doesn't seem right, but has no ill effect on my test files - if (*len > right_end) *len = right_end; // this should never happen + if (*len > right_end) + *len = right_end; // this should never happen f->current_loc += *len; return TRUE; } @@ -3416,7 +3738,7 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, // guess that the ogg granule pos refers to the _middle_ of the // last frame? // set f->current_loc to the position of left_start - f->current_loc = f->known_loc_for_packet - (n2-left_start); + f->current_loc = f->known_loc_for_packet - (n2 - left_start); f->current_loc_valid = TRUE; } if (f->current_loc_valid) @@ -3424,7 +3746,7 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, if (f->alloc.alloc_buffer) assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); - *len = right_end; // ignore samples after the window goes to 0 + *len = right_end; // ignore samples after the window goes to 0 CHECK(f); return TRUE; @@ -3433,13 +3755,14 @@ static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, static int vorbis_decode_packet(vorb *f, int *len, int *p_left, int *p_right) { int mode, left_end, right_end; - if (!vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) return 0; + if (!vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) + return 0; return vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left); } static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) { - int prev,i,j; + int prev, i, j; // we use right&left (the start of the right- and left-window sin()-regions) // to determine how much to return, rather than inferring from the rules // (same result, clearer code); 'left' indicates where our sin() window @@ -3449,15 +3772,18 @@ static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) // we start saving, and where our returned-data ends. // mixin from previous window - if (f->previous_length) { - int i,j, n = f->previous_length; + if (f->previous_length) + { + int i, j, n = f->previous_length; float *w = get_window(f, n); - if (w == NULL) return 0; - for (i=0; i < f->channels; ++i) { - for (j=0; j < n; ++j) - f->channel_buffers[i][left+j] = - f->channel_buffers[i][left+j]*w[ j] + - f->previous_window[i][ j]*w[n-1-j]; + if (w == NULL) + return 0; + for (i = 0; i < f->channels; ++i) + { + for (j = 0; j < n; ++j) + f->channel_buffers[i][left + j] = + f->channel_buffers[i][left + j] * w[j] + + f->previous_window[i][j] * w[n - 1 - j]; } } @@ -3472,9 +3798,9 @@ static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) // channel_buffers couldn't be temp mem (although they're NOT // currently temp mem, they could be (unless we want to level // performance by spreading out the computation)) - for (i=0; i < f->channels; ++i) - for (j=0; right+j < len; ++j) - f->previous_window[i][j] = f->channel_buffers[i][right+j]; + for (i = 0; i < f->channels; ++i) + for (j = 0; right + j < len; ++j) + f->previous_window[i][j] = f->channel_buffers[i][right + j]; if (!prev) // there was no previous packet, so this data isn't valid... @@ -3483,9 +3809,10 @@ static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) return 0; // truncate a short frame - if (len < right) right = len; + if (len < right) + right = len; - f->samples_output += right-left; + f->samples_output += right - left; return right - left; } @@ -3513,48 +3840,63 @@ static int is_whole_packet_present(stb_vorbis *f) int s = f->next_seg, first = TRUE; uint8 *p = f->stream; - if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag - for (; s < f->segment_count; ++s) { + if (s != -1) + { // if we're not starting the packet with a 'continue on next page' flag + for (; s < f->segment_count; ++s) + { p += f->segments[s]; - if (f->segments[s] < 255) // stop at first short segment + if (f->segments[s] < 255) // stop at first short segment break; } // either this continues, or it ends it... if (s == f->segment_count) s = -1; // set 'crosses page' flag - if (p > f->stream_end) return error(f, VORBIS_need_more_data); + if (p > f->stream_end) + return error(f, VORBIS_need_more_data); first = FALSE; } - for (; s == -1;) { + for (; s == -1;) + { uint8 *q; int n; // check that we have the page header ready - if (p + 26 >= f->stream_end) return error(f, VORBIS_need_more_data); + if (p + 26 >= f->stream_end) + return error(f, VORBIS_need_more_data); // validate the page - if (memcmp(p, ogg_page_header, 4)) return error(f, VORBIS_invalid_stream); - if (p[4] != 0) return error(f, VORBIS_invalid_stream); - if (first) { // the first segment must NOT have 'continued_packet', later ones MUST + if (memcmp(p, ogg_page_header, 4)) + return error(f, VORBIS_invalid_stream); + if (p[4] != 0) + return error(f, VORBIS_invalid_stream); + if (first) + { // the first segment must NOT have 'continued_packet', later ones MUST if (f->previous_length) - if ((p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream); + if ((p[5] & PAGEFLAG_continued_packet)) + return error(f, VORBIS_invalid_stream); // if no previous length, we're resynching, so we can come in on a continued-packet, // which we'll just drop - } else { - if (!(p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream); } - n = p[26]; // segment counts - q = p+27; // q points to segment table - p = q + n; // advance past header + else + { + if (!(p[5] & PAGEFLAG_continued_packet)) + return error(f, VORBIS_invalid_stream); + } + n = p[26]; // segment counts + q = p + 27; // q points to segment table + p = q + n; // advance past header // make sure we've read the segment table - if (p > f->stream_end) return error(f, VORBIS_need_more_data); - for (s=0; s < n; ++s) { + if (p > f->stream_end) + return error(f, VORBIS_need_more_data); + for (s = 0; s < n; ++s) + { p += q[s]; if (q[s] < 255) break; } if (s == n) s = -1; // set 'crosses page' flag - if (p > f->stream_end) return error(f, VORBIS_need_more_data); + if (p > f->stream_end) + return error(f, VORBIS_need_more_data); first = FALSE; } return TRUE; @@ -3563,21 +3905,27 @@ static int is_whole_packet_present(stb_vorbis *f) static int start_decoder(vorb *f) { - uint8 header[6], x,y; - int len,i,j,k, max_submaps = 0; - int longest_floorlist=0; + uint8 header[6], x, y; + int len, i, j, k, max_submaps = 0; + int longest_floorlist = 0; // first page, first packet f->first_decode = TRUE; - if (!start_page(f)) return FALSE; + if (!start_page(f)) + return FALSE; // validate page flag - if (!(f->page_flag & PAGEFLAG_first_page)) return error(f, VORBIS_invalid_first_page); - if (f->page_flag & PAGEFLAG_last_page) return error(f, VORBIS_invalid_first_page); - if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page); + if (!(f->page_flag & PAGEFLAG_first_page)) + return error(f, VORBIS_invalid_first_page); + if (f->page_flag & PAGEFLAG_last_page) + return error(f, VORBIS_invalid_first_page); + if (f->page_flag & PAGEFLAG_continued_packet) + return error(f, VORBIS_invalid_first_page); // check for expected packet length - if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page); - if (f->segments[0] != 30) { + if (f->segment_count != 1) + return error(f, VORBIS_invalid_first_page); + if (f->segments[0] != 30) + { // check for the Ogg skeleton fishead identifying header to refine our error if (f->segments[0] == 64 && getn(f, header, 6) && @@ -3587,72 +3935,99 @@ static int start_decoder(vorb *f) header[3] == 'h' && header[4] == 'e' && header[5] == 'a' && - get8(f) == 'd' && - get8(f) == '\0') return error(f, VORBIS_ogg_skeleton_not_supported); + get8(f) == 'd' && + get8(f) == '\0') + return error(f, VORBIS_ogg_skeleton_not_supported); else - return error(f, VORBIS_invalid_first_page); + return error(f, VORBIS_invalid_first_page); } // read packet // check packet header - if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page); - if (!getn(f, header, 6)) return error(f, VORBIS_unexpected_eof); - if (!vorbis_validate(header)) return error(f, VORBIS_invalid_first_page); + if (get8(f) != VORBIS_packet_id) + return error(f, VORBIS_invalid_first_page); + if (!getn(f, header, 6)) + return error(f, VORBIS_unexpected_eof); + if (!vorbis_validate(header)) + return error(f, VORBIS_invalid_first_page); // vorbis_version - if (get32(f) != 0) return error(f, VORBIS_invalid_first_page); - f->channels = get8(f); if (!f->channels) return error(f, VORBIS_invalid_first_page); - if (f->channels > STB_VORBIS_MAX_CHANNELS) return error(f, VORBIS_too_many_channels); - f->sample_rate = get32(f); if (!f->sample_rate) return error(f, VORBIS_invalid_first_page); + if (get32(f) != 0) + return error(f, VORBIS_invalid_first_page); + f->channels = get8(f); + if (!f->channels) + return error(f, VORBIS_invalid_first_page); + if (f->channels > STB_VORBIS_MAX_CHANNELS) + return error(f, VORBIS_too_many_channels); + f->sample_rate = get32(f); + if (!f->sample_rate) + return error(f, VORBIS_invalid_first_page); get32(f); // bitrate_maximum get32(f); // bitrate_nominal get32(f); // bitrate_minimum x = get8(f); { - int log0,log1; + int log0, log1; log0 = x & 15; log1 = x >> 4; f->blocksize_0 = 1 << log0; f->blocksize_1 = 1 << log1; - if (log0 < 6 || log0 > 13) return error(f, VORBIS_invalid_setup); - if (log1 < 6 || log1 > 13) return error(f, VORBIS_invalid_setup); - if (log0 > log1) return error(f, VORBIS_invalid_setup); + if (log0 < 6 || log0 > 13) + return error(f, VORBIS_invalid_setup); + if (log1 < 6 || log1 > 13) + return error(f, VORBIS_invalid_setup); + if (log0 > log1) + return error(f, VORBIS_invalid_setup); } // framing_flag x = get8(f); - if (!(x & 1)) return error(f, VORBIS_invalid_first_page); + if (!(x & 1)) + return error(f, VORBIS_invalid_first_page); // second packet! - if (!start_page(f)) return FALSE; + if (!start_page(f)) + return FALSE; - if (!start_packet(f)) return FALSE; + if (!start_packet(f)) + return FALSE; - if (!next_segment(f)) return FALSE; + if (!next_segment(f)) + return FALSE; - if (get8_packet(f) != VORBIS_packet_comment) return error(f, VORBIS_invalid_setup); - for (i=0; i < 6; ++i) header[i] = get8_packet(f); - if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup); - //file vendor + if (get8_packet(f) != VORBIS_packet_comment) + return error(f, VORBIS_invalid_setup); + for (i = 0; i < 6; ++i) + header[i] = get8_packet(f); + if (!vorbis_validate(header)) + return error(f, VORBIS_invalid_setup); + // file vendor len = get32_packet(f); - f->vendor = (char*)setup_malloc(f, sizeof(char) * (len+1)); - if (f->vendor == NULL) return error(f, VORBIS_outofmem); - for(i=0; i < len; ++i) { + f->vendor = (char *)setup_malloc(f, sizeof(char) * (len + 1)); + if (f->vendor == NULL) + return error(f, VORBIS_outofmem); + for (i = 0; i < len; ++i) + { f->vendor[i] = get8_packet(f); } f->vendor[len] = (char)'\0'; - //user comments + // user comments f->comment_list_length = get32_packet(f); - if (f->comment_list_length > 0) { - f->comment_list = (char**)setup_malloc(f, sizeof(char*) * (f->comment_list_length)); - if (f->comment_list == NULL) return error(f, VORBIS_outofmem); + if (f->comment_list_length > 0) + { + f->comment_list = (char **)setup_malloc(f, sizeof(char *) * (f->comment_list_length)); + if (f->comment_list == NULL) + return error(f, VORBIS_outofmem); } - for(i=0; i < f->comment_list_length; ++i) { + for (i = 0; i < f->comment_list_length; ++i) + { len = get32_packet(f); - f->comment_list[i] = (char*)setup_malloc(f, sizeof(char) * (len+1)); - if (f->comment_list[i] == NULL) return error(f, VORBIS_outofmem); + f->comment_list[i] = (char *)setup_malloc(f, sizeof(char) * (len + 1)); + if (f->comment_list[i] == NULL) + return error(f, VORBIS_outofmem); - for(j=0; j < len; ++j) { + for (j = 0; j < len; ++j) + { f->comment_list[i][j] = get8_packet(f); } f->comment_list[i][len] = (char)'\0'; @@ -3660,104 +4035,135 @@ static int start_decoder(vorb *f) // framing_flag x = get8_packet(f); - if (!(x & 1)) return error(f, VORBIS_invalid_setup); - + if (!(x & 1)) + return error(f, VORBIS_invalid_setup); skip(f, f->bytes_in_seg); f->bytes_in_seg = 0; - do { + do + { len = next_segment(f); skip(f, len); f->bytes_in_seg = 0; } while (len); // third packet! - if (!start_packet(f)) return FALSE; + if (!start_packet(f)) + return FALSE; - #ifndef STB_VORBIS_NO_PUSHDATA_API - if (IS_PUSH_MODE(f)) { - if (!is_whole_packet_present(f)) { +#ifndef STB_VORBIS_NO_PUSHDATA_API + if (IS_PUSH_MODE(f)) + { + if (!is_whole_packet_present(f)) + { // convert error in ogg header to write type if (f->error == VORBIS_invalid_stream) f->error = VORBIS_invalid_setup; return FALSE; } } - #endif +#endif crc32_init(); // always init it, to avoid multithread race conditions - if (get8_packet(f) != VORBIS_packet_setup) return error(f, VORBIS_invalid_setup); - for (i=0; i < 6; ++i) header[i] = get8_packet(f); - if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup); + if (get8_packet(f) != VORBIS_packet_setup) + return error(f, VORBIS_invalid_setup); + for (i = 0; i < 6; ++i) + header[i] = get8_packet(f); + if (!vorbis_validate(header)) + return error(f, VORBIS_invalid_setup); // codebooks - f->codebook_count = get_bits(f,8) + 1; - f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count); - if (f->codebooks == NULL) return error(f, VORBIS_outofmem); + f->codebook_count = get_bits(f, 8) + 1; + f->codebooks = (Codebook *)setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count); + if (f->codebooks == NULL) + return error(f, VORBIS_outofmem); memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count); - for (i=0; i < f->codebook_count; ++i) { + for (i = 0; i < f->codebook_count; ++i) + { uint32 *values; int ordered, sorted_count; - int total=0; + int total = 0; uint8 *lengths; - Codebook *c = f->codebooks+i; + Codebook *c = f->codebooks + i; CHECK(f); - x = get_bits(f, 8); if (x != 0x42) return error(f, VORBIS_invalid_setup); - x = get_bits(f, 8); if (x != 0x43) return error(f, VORBIS_invalid_setup); - x = get_bits(f, 8); if (x != 0x56) return error(f, VORBIS_invalid_setup); x = get_bits(f, 8); - c->dimensions = (get_bits(f, 8)<<8) + x; + if (x != 0x42) + return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); + if (x != 0x43) + return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); + if (x != 0x56) + return error(f, VORBIS_invalid_setup); + x = get_bits(f, 8); + c->dimensions = (get_bits(f, 8) << 8) + x; x = get_bits(f, 8); y = get_bits(f, 8); - c->entries = (get_bits(f, 8)<<16) + (y<<8) + x; - ordered = get_bits(f,1); - c->sparse = ordered ? 0 : get_bits(f,1); + c->entries = (get_bits(f, 8) << 16) + (y << 8) + x; + ordered = get_bits(f, 1); + c->sparse = ordered ? 0 : get_bits(f, 1); - if (c->dimensions == 0 && c->entries != 0) return error(f, VORBIS_invalid_setup); + if (c->dimensions == 0 && c->entries != 0) + return error(f, VORBIS_invalid_setup); if (c->sparse) - lengths = (uint8 *) setup_temp_malloc(f, c->entries); + lengths = (uint8 *)setup_temp_malloc(f, c->entries); else - lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries); + lengths = c->codeword_lengths = (uint8 *)setup_malloc(f, c->entries); - if (!lengths) return error(f, VORBIS_outofmem); + if (!lengths) + return error(f, VORBIS_outofmem); - if (ordered) { + if (ordered) + { int current_entry = 0; - int current_length = get_bits(f,5) + 1; - while (current_entry < c->entries) { + int current_length = get_bits(f, 5) + 1; + while (current_entry < c->entries) + { int limit = c->entries - current_entry; int n = get_bits(f, ilog(limit)); - if (current_length >= 32) return error(f, VORBIS_invalid_setup); - if (current_entry + n > (int) c->entries) { return error(f, VORBIS_invalid_setup); } + if (current_length >= 32) + return error(f, VORBIS_invalid_setup); + if (current_entry + n > (int)c->entries) + { + return error(f, VORBIS_invalid_setup); + } memset(lengths + current_entry, current_length, n); current_entry += n; ++current_length; } - } else { - for (j=0; j < c->entries; ++j) { - int present = c->sparse ? get_bits(f,1) : 1; - if (present) { + } + else + { + for (j = 0; j < c->entries; ++j) + { + int present = c->sparse ? get_bits(f, 1) : 1; + if (present) + { lengths[j] = get_bits(f, 5) + 1; ++total; if (lengths[j] == 32) return error(f, VORBIS_invalid_setup); - } else { + } + else + { lengths[j] = NO_CODE; } } } - if (c->sparse && total >= c->entries >> 2) { + if (c->sparse && total >= c->entries >> 2) + { // convert sparse items to non-sparse! - if (c->entries > (int) f->setup_temp_memory_required) + if (c->entries > (int)f->setup_temp_memory_required) f->setup_temp_memory_required = c->entries; - c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries); - if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem); + c->codeword_lengths = (uint8 *)setup_malloc(f, c->entries); + if (c->codeword_lengths == NULL) + return error(f, VORBIS_outofmem); memcpy(c->codeword_lengths, lengths, c->entries); setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs! lengths = c->codeword_lengths; @@ -3765,60 +4171,77 @@ static int start_decoder(vorb *f) } // compute the size of the sorted tables - if (c->sparse) { + if (c->sparse) + { sorted_count = total; - } else { + } + else + { sorted_count = 0; - #ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH - for (j=0; j < c->entries; ++j) +#ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH + for (j = 0; j < c->entries; ++j) if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE) ++sorted_count; - #endif +#endif } c->sorted_entries = sorted_count; values = NULL; CHECK(f); - if (!c->sparse) { - c->codewords = (uint32 *) setup_malloc(f, sizeof(c->codewords[0]) * c->entries); - if (!c->codewords) return error(f, VORBIS_outofmem); - } else { + if (!c->sparse) + { + c->codewords = (uint32 *)setup_malloc(f, sizeof(c->codewords[0]) * c->entries); + if (!c->codewords) + return error(f, VORBIS_outofmem); + } + else + { unsigned int size; - if (c->sorted_entries) { - c->codeword_lengths = (uint8 *) setup_malloc(f, c->sorted_entries); - if (!c->codeword_lengths) return error(f, VORBIS_outofmem); - c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries); - if (!c->codewords) return error(f, VORBIS_outofmem); - values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries); - if (!values) return error(f, VORBIS_outofmem); + if (c->sorted_entries) + { + c->codeword_lengths = (uint8 *)setup_malloc(f, c->sorted_entries); + if (!c->codeword_lengths) + return error(f, VORBIS_outofmem); + c->codewords = (uint32 *)setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries); + if (!c->codewords) + return error(f, VORBIS_outofmem); + values = (uint32 *)setup_temp_malloc(f, sizeof(*values) * c->sorted_entries); + if (!values) + return error(f, VORBIS_outofmem); } size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries; if (size > f->setup_temp_memory_required) f->setup_temp_memory_required = size; } - if (!compute_codewords(c, lengths, c->entries, values)) { - if (c->sparse) setup_temp_free(f, values, 0); + if (!compute_codewords(c, lengths, c->entries, values)) + { + if (c->sparse) + setup_temp_free(f, values, 0); return error(f, VORBIS_invalid_setup); } - if (c->sorted_entries) { + if (c->sorted_entries) + { // allocate an extra slot for sentinels - c->sorted_codewords = (uint32 *) setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries+1)); - if (c->sorted_codewords == NULL) return error(f, VORBIS_outofmem); + c->sorted_codewords = (uint32 *)setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries + 1)); + if (c->sorted_codewords == NULL) + return error(f, VORBIS_outofmem); // allocate an extra slot at the front so that c->sorted_values[-1] is defined // so that we can catch that case without an extra if - c->sorted_values = ( int *) setup_malloc(f, sizeof(*c->sorted_values ) * (c->sorted_entries+1)); - if (c->sorted_values == NULL) return error(f, VORBIS_outofmem); + c->sorted_values = (int *)setup_malloc(f, sizeof(*c->sorted_values) * (c->sorted_entries + 1)); + if (c->sorted_values == NULL) + return error(f, VORBIS_outofmem); ++c->sorted_values; c->sorted_values[-1] = -1; compute_sorted_huffman(c, lengths, values); } - if (c->sparse) { - setup_temp_free(f, values, sizeof(*values)*c->sorted_entries); - setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries); + if (c->sparse) + { + setup_temp_free(f, values, sizeof(*values) * c->sorted_entries); + setup_temp_free(f, c->codewords, sizeof(*c->codewords) * c->sorted_entries); setup_temp_free(f, lengths, c->entries); c->codewords = NULL; } @@ -3827,54 +4250,79 @@ static int start_decoder(vorb *f) CHECK(f); c->lookup_type = get_bits(f, 4); - if (c->lookup_type > 2) return error(f, VORBIS_invalid_setup); - if (c->lookup_type > 0) { + if (c->lookup_type > 2) + return error(f, VORBIS_invalid_setup); + if (c->lookup_type > 0) + { uint16 *mults; c->minimum_value = float32_unpack(get_bits(f, 32)); c->delta_value = float32_unpack(get_bits(f, 32)); - c->value_bits = get_bits(f, 4)+1; - c->sequence_p = get_bits(f,1); - if (c->lookup_type == 1) { + c->value_bits = get_bits(f, 4) + 1; + c->sequence_p = get_bits(f, 1); + if (c->lookup_type == 1) + { int values = lookup1_values(c->entries, c->dimensions); - if (values < 0) return error(f, VORBIS_invalid_setup); - c->lookup_values = (uint32) values; - } else { + if (values < 0) + return error(f, VORBIS_invalid_setup); + c->lookup_values = (uint32)values; + } + else + { c->lookup_values = c->entries * c->dimensions; } - if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup); - mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values); - if (mults == NULL) return error(f, VORBIS_outofmem); - for (j=0; j < (int) c->lookup_values; ++j) { + if (c->lookup_values == 0) + return error(f, VORBIS_invalid_setup); + mults = (uint16 *)setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values); + if (mults == NULL) + return error(f, VORBIS_outofmem); + for (j = 0; j < (int)c->lookup_values; ++j) + { int q = get_bits(f, c->value_bits); - if (q == EOP) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_invalid_setup); } + if (q == EOP) + { + setup_temp_free(f, mults, sizeof(mults[0]) * c->lookup_values); + return error(f, VORBIS_invalid_setup); + } mults[j] = q; } #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK - if (c->lookup_type == 1) { + if (c->lookup_type == 1) + { int len, sparse = c->sparse; - float last=0; + float last = 0; // pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop - if (sparse) { - if (c->sorted_entries == 0) goto skip; - c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions); - } else - c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions); - if (c->multiplicands == NULL) { setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); } + if (sparse) + { + if (c->sorted_entries == 0) + goto skip; + c->multiplicands = (codetype *)setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions); + } + else + c->multiplicands = (codetype *)setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions); + if (c->multiplicands == NULL) + { + setup_temp_free(f, mults, sizeof(mults[0]) * c->lookup_values); + return error(f, VORBIS_outofmem); + } len = sparse ? c->sorted_entries : c->entries; - for (j=0; j < len; ++j) { + for (j = 0; j < len; ++j) + { unsigned int z = sparse ? c->sorted_values[j] : j; - unsigned int div=1; - for (k=0; k < c->dimensions; ++k) { + unsigned int div = 1; + for (k = 0; k < c->dimensions; ++k) + { int off = (z / div) % c->lookup_values; float val = mults[off]; - val = mults[off]*c->delta_value + c->minimum_value + last; - c->multiplicands[j*c->dimensions + k] = val; + val = mults[off] * c->delta_value + c->minimum_value + last; + c->multiplicands[j * c->dimensions + k] = val; if (c->sequence_p) last = val; - if (k+1 < c->dimensions) { - if (div > UINT_MAX / (unsigned int) c->lookup_values) { - setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); + if (k + 1 < c->dimensions) + { + if (div > UINT_MAX / (unsigned int)c->lookup_values) + { + setup_temp_free(f, mults, sizeof(mults[0]) * c->lookup_values); return error(f, VORBIS_invalid_setup); } div *= c->lookup_values; @@ -3886,11 +4334,16 @@ static int start_decoder(vorb *f) else #endif { - float last=0; + float last = 0; CHECK(f); - c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values); - if (c->multiplicands == NULL) { setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); } - for (j=0; j < (int) c->lookup_values; ++j) { + c->multiplicands = (codetype *)setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values); + if (c->multiplicands == NULL) + { + setup_temp_free(f, mults, sizeof(mults[0]) * c->lookup_values); + return error(f, VORBIS_outofmem); + } + for (j = 0; j < (int)c->lookup_values; ++j) + { float val = mults[j] * c->delta_value + c->minimum_value + last; c->multiplicands[j] = val; if (c->sequence_p) @@ -3898,9 +4351,9 @@ static int start_decoder(vorb *f) } } #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK - skip:; + skip:; #endif - setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); + setup_temp_free(f, mults, sizeof(mults[0]) * c->lookup_values); CHECK(f); } @@ -3910,78 +4363,96 @@ static int start_decoder(vorb *f) // time domain transfers (notused) x = get_bits(f, 6) + 1; - for (i=0; i < x; ++i) { + for (i = 0; i < x; ++i) + { uint32 z = get_bits(f, 16); - if (z != 0) return error(f, VORBIS_invalid_setup); + if (z != 0) + return error(f, VORBIS_invalid_setup); } // Floors - f->floor_count = get_bits(f, 6)+1; - f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config)); - if (f->floor_config == NULL) return error(f, VORBIS_outofmem); - for (i=0; i < f->floor_count; ++i) { + f->floor_count = get_bits(f, 6) + 1; + f->floor_config = (Floor *)setup_malloc(f, f->floor_count * sizeof(*f->floor_config)); + if (f->floor_config == NULL) + return error(f, VORBIS_outofmem); + for (i = 0; i < f->floor_count; ++i) + { f->floor_types[i] = get_bits(f, 16); - if (f->floor_types[i] > 1) return error(f, VORBIS_invalid_setup); - if (f->floor_types[i] == 0) { + if (f->floor_types[i] > 1) + return error(f, VORBIS_invalid_setup); + if (f->floor_types[i] == 0) + { Floor0 *g = &f->floor_config[i].floor0; - g->order = get_bits(f,8); - g->rate = get_bits(f,16); - g->bark_map_size = get_bits(f,16); - g->amplitude_bits = get_bits(f,6); - g->amplitude_offset = get_bits(f,8); - g->number_of_books = get_bits(f,4) + 1; - for (j=0; j < g->number_of_books; ++j) - g->book_list[j] = get_bits(f,8); + g->order = get_bits(f, 8); + g->rate = get_bits(f, 16); + g->bark_map_size = get_bits(f, 16); + g->amplitude_bits = get_bits(f, 6); + g->amplitude_offset = get_bits(f, 8); + g->number_of_books = get_bits(f, 4) + 1; + for (j = 0; j < g->number_of_books; ++j) + g->book_list[j] = get_bits(f, 8); return error(f, VORBIS_feature_not_supported); - } else { - stbv__floor_ordering p[31*8+2]; + } + else + { + stbv__floor_ordering p[31 * 8 + 2]; Floor1 *g = &f->floor_config[i].floor1; int max_class = -1; g->partitions = get_bits(f, 5); - for (j=0; j < g->partitions; ++j) { + for (j = 0; j < g->partitions; ++j) + { g->partition_class_list[j] = get_bits(f, 4); if (g->partition_class_list[j] > max_class) max_class = g->partition_class_list[j]; } - for (j=0; j <= max_class; ++j) { - g->class_dimensions[j] = get_bits(f, 3)+1; + for (j = 0; j <= max_class; ++j) + { + g->class_dimensions[j] = get_bits(f, 3) + 1; g->class_subclasses[j] = get_bits(f, 2); - if (g->class_subclasses[j]) { + if (g->class_subclasses[j]) + { g->class_masterbooks[j] = get_bits(f, 8); - if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup); + if (g->class_masterbooks[j] >= f->codebook_count) + return error(f, VORBIS_invalid_setup); } - for (k=0; k < 1 << g->class_subclasses[j]; ++k) { - g->subclass_books[j][k] = get_bits(f,8)-1; - if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup); + for (k = 0; k < 1 << g->class_subclasses[j]; ++k) + { + g->subclass_books[j][k] = get_bits(f, 8) - 1; + if (g->subclass_books[j][k] >= f->codebook_count) + return error(f, VORBIS_invalid_setup); } } - g->floor1_multiplier = get_bits(f,2)+1; - g->rangebits = get_bits(f,4); + g->floor1_multiplier = get_bits(f, 2) + 1; + g->rangebits = get_bits(f, 4); g->Xlist[0] = 0; g->Xlist[1] = 1 << g->rangebits; g->values = 2; - for (j=0; j < g->partitions; ++j) { + for (j = 0; j < g->partitions; ++j) + { int c = g->partition_class_list[j]; - for (k=0; k < g->class_dimensions[c]; ++k) { + for (k = 0; k < g->class_dimensions[c]; ++k) + { g->Xlist[g->values] = get_bits(f, g->rangebits); ++g->values; } } // precompute the sorting - for (j=0; j < g->values; ++j) { + for (j = 0; j < g->values; ++j) + { p[j].x = g->Xlist[j]; p[j].id = j; } qsort(p, g->values, sizeof(p[0]), point_compare); - for (j=0; j < g->values-1; ++j) - if (p[j].x == p[j+1].x) + for (j = 0; j < g->values - 1; ++j) + if (p[j].x == p[j + 1].x) return error(f, VORBIS_invalid_setup); - for (j=0; j < g->values; ++j) - g->sorted_order[j] = (uint8) p[j].id; + for (j = 0; j < g->values; ++j) + g->sorted_order[j] = (uint8)p[j].id; // precompute the neighbors - for (j=2; j < g->values; ++j) { - int low = 0,hi = 0; - neighbors(g->Xlist, j, &low,&hi); + for (j = 2; j < g->values; ++j) + { + int low = 0, hi = 0; + neighbors(g->Xlist, j, &low, &hi); g->neighbors[j][0] = low; g->neighbors[j][1] = hi; } @@ -3992,146 +4463,191 @@ static int start_decoder(vorb *f) } // Residue - f->residue_count = get_bits(f, 6)+1; - f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0])); - if (f->residue_config == NULL) return error(f, VORBIS_outofmem); + f->residue_count = get_bits(f, 6) + 1; + f->residue_config = (Residue *)setup_malloc(f, f->residue_count * sizeof(f->residue_config[0])); + if (f->residue_config == NULL) + return error(f, VORBIS_outofmem); memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0])); - for (i=0; i < f->residue_count; ++i) { + for (i = 0; i < f->residue_count; ++i) + { uint8 residue_cascade[64]; - Residue *r = f->residue_config+i; + Residue *r = f->residue_config + i; f->residue_types[i] = get_bits(f, 16); - if (f->residue_types[i] > 2) return error(f, VORBIS_invalid_setup); + if (f->residue_types[i] > 2) + return error(f, VORBIS_invalid_setup); r->begin = get_bits(f, 24); r->end = get_bits(f, 24); - if (r->end < r->begin) return error(f, VORBIS_invalid_setup); - r->part_size = get_bits(f,24)+1; - r->classifications = get_bits(f,6)+1; - r->classbook = get_bits(f,8); - if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup); - for (j=0; j < r->classifications; ++j) { - uint8 high_bits=0; - uint8 low_bits=get_bits(f,3); - if (get_bits(f,1)) - high_bits = get_bits(f,5); - residue_cascade[j] = high_bits*8 + low_bits; + if (r->end < r->begin) + return error(f, VORBIS_invalid_setup); + r->part_size = get_bits(f, 24) + 1; + r->classifications = get_bits(f, 6) + 1; + r->classbook = get_bits(f, 8); + if (r->classbook >= f->codebook_count) + return error(f, VORBIS_invalid_setup); + for (j = 0; j < r->classifications; ++j) + { + uint8 high_bits = 0; + uint8 low_bits = get_bits(f, 3); + if (get_bits(f, 1)) + high_bits = get_bits(f, 5); + residue_cascade[j] = high_bits * 8 + low_bits; } - r->residue_books = (short (*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications); - if (r->residue_books == NULL) return error(f, VORBIS_outofmem); - for (j=0; j < r->classifications; ++j) { - for (k=0; k < 8; ++k) { - if (residue_cascade[j] & (1 << k)) { + r->residue_books = (short (*)[8])setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications); + if (r->residue_books == NULL) + return error(f, VORBIS_outofmem); + for (j = 0; j < r->classifications; ++j) + { + for (k = 0; k < 8; ++k) + { + if (residue_cascade[j] & (1 << k)) + { r->residue_books[j][k] = get_bits(f, 8); - if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup); - } else { + if (r->residue_books[j][k] >= f->codebook_count) + return error(f, VORBIS_invalid_setup); + } + else + { r->residue_books[j][k] = -1; } } } // precompute the classifications[] array to avoid inner-loop mod/divide // call it 'classdata' since we already have r->classifications - r->classdata = (uint8 **) setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries); - if (!r->classdata) return error(f, VORBIS_outofmem); + r->classdata = (uint8 **)setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries); + if (!r->classdata) + return error(f, VORBIS_outofmem); memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries); - for (j=0; j < f->codebooks[r->classbook].entries; ++j) { + for (j = 0; j < f->codebooks[r->classbook].entries; ++j) + { int classwords = f->codebooks[r->classbook].dimensions; int temp = j; - r->classdata[j] = (uint8 *) setup_malloc(f, sizeof(r->classdata[j][0]) * classwords); - if (r->classdata[j] == NULL) return error(f, VORBIS_outofmem); - for (k=classwords-1; k >= 0; --k) { + r->classdata[j] = (uint8 *)setup_malloc(f, sizeof(r->classdata[j][0]) * classwords); + if (r->classdata[j] == NULL) + return error(f, VORBIS_outofmem); + for (k = classwords - 1; k >= 0; --k) + { r->classdata[j][k] = temp % r->classifications; temp /= r->classifications; } } } - f->mapping_count = get_bits(f,6)+1; - f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping)); - if (f->mapping == NULL) return error(f, VORBIS_outofmem); + f->mapping_count = get_bits(f, 6) + 1; + f->mapping = (Mapping *)setup_malloc(f, f->mapping_count * sizeof(*f->mapping)); + if (f->mapping == NULL) + return error(f, VORBIS_outofmem); memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping)); - for (i=0; i < f->mapping_count; ++i) { + for (i = 0; i < f->mapping_count; ++i) + { Mapping *m = f->mapping + i; - int mapping_type = get_bits(f,16); - if (mapping_type != 0) return error(f, VORBIS_invalid_setup); - m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan)); - if (m->chan == NULL) return error(f, VORBIS_outofmem); - if (get_bits(f,1)) - m->submaps = get_bits(f,4)+1; + int mapping_type = get_bits(f, 16); + if (mapping_type != 0) + return error(f, VORBIS_invalid_setup); + m->chan = (MappingChannel *)setup_malloc(f, f->channels * sizeof(*m->chan)); + if (m->chan == NULL) + return error(f, VORBIS_outofmem); + if (get_bits(f, 1)) + m->submaps = get_bits(f, 4) + 1; else m->submaps = 1; if (m->submaps > max_submaps) max_submaps = m->submaps; - if (get_bits(f,1)) { - m->coupling_steps = get_bits(f,8)+1; - if (m->coupling_steps > f->channels) return error(f, VORBIS_invalid_setup); - for (k=0; k < m->coupling_steps; ++k) { - m->chan[k].magnitude = get_bits(f, ilog(f->channels-1)); - m->chan[k].angle = get_bits(f, ilog(f->channels-1)); - if (m->chan[k].magnitude >= f->channels) return error(f, VORBIS_invalid_setup); - if (m->chan[k].angle >= f->channels) return error(f, VORBIS_invalid_setup); - if (m->chan[k].magnitude == m->chan[k].angle) return error(f, VORBIS_invalid_setup); + if (get_bits(f, 1)) + { + m->coupling_steps = get_bits(f, 8) + 1; + if (m->coupling_steps > f->channels) + return error(f, VORBIS_invalid_setup); + for (k = 0; k < m->coupling_steps; ++k) + { + m->chan[k].magnitude = get_bits(f, ilog(f->channels - 1)); + m->chan[k].angle = get_bits(f, ilog(f->channels - 1)); + if (m->chan[k].magnitude >= f->channels) + return error(f, VORBIS_invalid_setup); + if (m->chan[k].angle >= f->channels) + return error(f, VORBIS_invalid_setup); + if (m->chan[k].magnitude == m->chan[k].angle) + return error(f, VORBIS_invalid_setup); } - } else + } + else m->coupling_steps = 0; // reserved field - if (get_bits(f,2)) return error(f, VORBIS_invalid_setup); - if (m->submaps > 1) { - for (j=0; j < f->channels; ++j) { + if (get_bits(f, 2)) + return error(f, VORBIS_invalid_setup); + if (m->submaps > 1) + { + for (j = 0; j < f->channels; ++j) + { m->chan[j].mux = get_bits(f, 4); - if (m->chan[j].mux >= m->submaps) return error(f, VORBIS_invalid_setup); + if (m->chan[j].mux >= m->submaps) + return error(f, VORBIS_invalid_setup); } - } else + } + else // @SPECIFICATION: this case is missing from the spec - for (j=0; j < f->channels; ++j) + for (j = 0; j < f->channels; ++j) m->chan[j].mux = 0; - for (j=0; j < m->submaps; ++j) { - get_bits(f,8); // discard - m->submap_floor[j] = get_bits(f,8); - m->submap_residue[j] = get_bits(f,8); - if (m->submap_floor[j] >= f->floor_count) return error(f, VORBIS_invalid_setup); - if (m->submap_residue[j] >= f->residue_count) return error(f, VORBIS_invalid_setup); + for (j = 0; j < m->submaps; ++j) + { + get_bits(f, 8); // discard + m->submap_floor[j] = get_bits(f, 8); + m->submap_residue[j] = get_bits(f, 8); + if (m->submap_floor[j] >= f->floor_count) + return error(f, VORBIS_invalid_setup); + if (m->submap_residue[j] >= f->residue_count) + return error(f, VORBIS_invalid_setup); } } // Modes - f->mode_count = get_bits(f, 6)+1; - for (i=0; i < f->mode_count; ++i) { - Mode *m = f->mode_config+i; - m->blockflag = get_bits(f,1); - m->windowtype = get_bits(f,16); - m->transformtype = get_bits(f,16); - m->mapping = get_bits(f,8); - if (m->windowtype != 0) return error(f, VORBIS_invalid_setup); - if (m->transformtype != 0) return error(f, VORBIS_invalid_setup); - if (m->mapping >= f->mapping_count) return error(f, VORBIS_invalid_setup); + f->mode_count = get_bits(f, 6) + 1; + for (i = 0; i < f->mode_count; ++i) + { + Mode *m = f->mode_config + i; + m->blockflag = get_bits(f, 1); + m->windowtype = get_bits(f, 16); + m->transformtype = get_bits(f, 16); + m->mapping = get_bits(f, 8); + if (m->windowtype != 0) + return error(f, VORBIS_invalid_setup); + if (m->transformtype != 0) + return error(f, VORBIS_invalid_setup); + if (m->mapping >= f->mapping_count) + return error(f, VORBIS_invalid_setup); } flush_packet(f); f->previous_length = 0; - for (i=0; i < f->channels; ++i) { - f->channel_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1); - f->previous_window[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2); - f->finalY[i] = (int16 *) setup_malloc(f, sizeof(int16) * longest_floorlist); - if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) return error(f, VORBIS_outofmem); + for (i = 0; i < f->channels; ++i) + { + f->channel_buffers[i] = (float *)setup_malloc(f, sizeof(float) * f->blocksize_1); + f->previous_window[i] = (float *)setup_malloc(f, sizeof(float) * f->blocksize_1 / 2); + f->finalY[i] = (int16 *)setup_malloc(f, sizeof(int16) * longest_floorlist); + if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) + return error(f, VORBIS_outofmem); memset(f->channel_buffers[i], 0, sizeof(float) * f->blocksize_1); - #ifdef STB_VORBIS_NO_DEFER_FLOOR - f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2); - if (f->floor_buffers[i] == NULL) return error(f, VORBIS_outofmem); - #endif +#ifdef STB_VORBIS_NO_DEFER_FLOOR + f->floor_buffers[i] = (float *)setup_malloc(f, sizeof(float) * f->blocksize_1 / 2); + if (f->floor_buffers[i] == NULL) + return error(f, VORBIS_outofmem); +#endif } - if (!init_blocksize(f, 0, f->blocksize_0)) return FALSE; - if (!init_blocksize(f, 1, f->blocksize_1)) return FALSE; + if (!init_blocksize(f, 0, f->blocksize_0)) + return FALSE; + if (!init_blocksize(f, 1, f->blocksize_1)) + return FALSE; f->blocksize[0] = f->blocksize_0; f->blocksize[1] = f->blocksize_1; #ifdef STB_VORBIS_DIVIDE_TABLE - if (integer_divide_table[1][1]==0) - for (i=0; i < DIVTAB_NUMER; ++i) - for (j=1; j < DIVTAB_DENOM; ++j) + if (integer_divide_table[1][1] == 0) + for (i = 0; i < DIVTAB_NUMER; ++i) + for (j = 1; j < DIVTAB_DENOM; ++j) integer_divide_table[i][j] = i / j; #endif @@ -4141,22 +4657,23 @@ static int start_decoder(vorb *f) { uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1); uint32 classify_mem; - int i,max_part_read=0; - for (i=0; i < f->residue_count; ++i) { + int i, max_part_read = 0; + for (i = 0; i < f->residue_count; ++i) + { Residue *r = f->residue_config + i; unsigned int actual_size = f->blocksize_1 / 2; unsigned int limit_r_begin = r->begin < actual_size ? r->begin : actual_size; - unsigned int limit_r_end = r->end < actual_size ? r->end : actual_size; + unsigned int limit_r_end = r->end < actual_size ? r->end : actual_size; int n_read = limit_r_end - limit_r_begin; int part_read = n_read / r->part_size; if (part_read > max_part_read) max_part_read = part_read; } - #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE - classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(uint8 *)); - #else - classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *)); - #endif +#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE + classify_mem = f->channels * (sizeof(void *) + max_part_read * sizeof(uint8 *)); +#else + classify_mem = f->channels * (sizeof(void *) + max_part_read * sizeof(int *)); +#endif // maximum reasonable partition size is f->blocksize_1 @@ -4165,11 +4682,11 @@ static int start_decoder(vorb *f) f->temp_memory_required = imdct_mem; } - - if (f->alloc.alloc_buffer) { + if (f->alloc.alloc_buffer) + { assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes); // check if there's enough temp memory so we don't error later - if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset) + if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned)f->temp_offset) return error(f, VORBIS_outofmem); } @@ -4179,9 +4696,12 @@ static int start_decoder(vorb *f) // in the middle of the headers, when that's the page where the first audio packet // starts, but we'd have to also correctly skip the end of any continued packet in // stb_vorbis_seek_start. - if (f->next_seg == -1) { + if (f->next_seg == -1) + { f->first_audio_page_offset = stb_vorbis_get_file_offset(f); - } else { + } + else + { f->first_audio_page_offset = 0; } @@ -4190,19 +4710,23 @@ static int start_decoder(vorb *f) static void vorbis_deinit(stb_vorbis *p) { - int i,j; + int i, j; setup_free(p, p->vendor); - for (i=0; i < p->comment_list_length; ++i) { + for (i = 0; i < p->comment_list_length; ++i) + { setup_free(p, p->comment_list[i]); } setup_free(p, p->comment_list); - if (p->residue_config) { - for (i=0; i < p->residue_count; ++i) { - Residue *r = p->residue_config+i; - if (r->classdata) { - for (j=0; j < p->codebooks[r->classbook].entries; ++j) + if (p->residue_config) + { + for (i = 0; i < p->residue_count; ++i) + { + Residue *r = p->residue_config + i; + if (r->classdata) + { + for (j = 0; j < p->codebooks[r->classbook].entries; ++j) setup_free(p, r->classdata[j]); setup_free(p, r->classdata); } @@ -4210,58 +4734,66 @@ static void vorbis_deinit(stb_vorbis *p) } } - if (p->codebooks) { + if (p->codebooks) + { CHECK(p); - for (i=0; i < p->codebook_count; ++i) { + for (i = 0; i < p->codebook_count; ++i) + { Codebook *c = p->codebooks + i; setup_free(p, c->codeword_lengths); setup_free(p, c->multiplicands); setup_free(p, c->codewords); setup_free(p, c->sorted_codewords); // c->sorted_values[-1] is the first entry in the array - setup_free(p, c->sorted_values ? c->sorted_values-1 : NULL); + setup_free(p, c->sorted_values ? c->sorted_values - 1 : NULL); } setup_free(p, p->codebooks); } setup_free(p, p->floor_config); setup_free(p, p->residue_config); - if (p->mapping) { - for (i=0; i < p->mapping_count; ++i) + if (p->mapping) + { + for (i = 0; i < p->mapping_count; ++i) setup_free(p, p->mapping[i].chan); setup_free(p, p->mapping); } CHECK(p); - for (i=0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) { + for (i = 0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) + { setup_free(p, p->channel_buffers[i]); setup_free(p, p->previous_window[i]); - #ifdef STB_VORBIS_NO_DEFER_FLOOR +#ifdef STB_VORBIS_NO_DEFER_FLOOR setup_free(p, p->floor_buffers[i]); - #endif +#endif setup_free(p, p->finalY[i]); } - for (i=0; i < 2; ++i) { + for (i = 0; i < 2; ++i) + { setup_free(p, p->A[i]); setup_free(p, p->B[i]); setup_free(p, p->C[i]); setup_free(p, p->window[i]); setup_free(p, p->bit_reverse[i]); } - #ifndef STB_VORBIS_NO_STDIO - if (p->close_on_free) fclose(p->f); - #endif +#ifndef STB_VORBIS_NO_STDIO + if (p->close_on_free) + fclose(p->f); +#endif } void stb_vorbis_close(stb_vorbis *p) { - if (p == NULL) return; + if (p == NULL) + return; vorbis_deinit(p); - setup_free(p,p); + setup_free(p, p); } static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z) { memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start - if (z) { + if (z) + { p->alloc = *z; p->alloc.alloc_buffer_length_in_bytes &= ~7; p->temp_offset = p->alloc.alloc_buffer_length_in_bytes; @@ -4271,10 +4803,10 @@ static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z) p->stream = NULL; p->codebooks = NULL; p->page_crc_tests = -1; - #ifndef STB_VORBIS_NO_STDIO +#ifndef STB_VORBIS_NO_STDIO p->close_on_free = FALSE; p->f = NULL; - #endif +#endif } int stb_vorbis_get_sample_offset(stb_vorbis *f) @@ -4313,9 +4845,9 @@ int stb_vorbis_get_error(stb_vorbis *f) return e; } -static stb_vorbis * vorbis_alloc(stb_vorbis *f) +static stb_vorbis *vorbis_alloc(stb_vorbis *f) { - stb_vorbis *p = (stb_vorbis *) setup_malloc(f, sizeof(*p)); + stb_vorbis *p = (stb_vorbis *)setup_malloc(f, sizeof(*p)); return p; } @@ -4324,7 +4856,7 @@ static stb_vorbis * vorbis_alloc(stb_vorbis *f) void stb_vorbis_flush_pushdata(stb_vorbis *f) { f->previous_length = 0; - f->page_crc_tests = 0; + f->page_crc_tests = 0; f->discard_samples_deferred = 0; f->current_loc_valid = FALSE; f->first_decode = FALSE; @@ -4335,51 +4867,57 @@ void stb_vorbis_flush_pushdata(stb_vorbis *f) static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len) { - int i,n; - for (i=0; i < f->page_crc_tests; ++i) + int i, n; + for (i = 0; i < f->page_crc_tests; ++i) f->scan[i].bytes_done = 0; // if we have room for more scans, search for them first, because // they may cause us to stop early if their header is incomplete - if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) { - if (data_len < 4) return 0; + if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) + { + if (data_len < 4) + return 0; data_len -= 3; // need to look for 4-byte sequence, so don't miss // one that straddles a boundary - for (i=0; i < data_len; ++i) { - if (data[i] == 0x4f) { - if (0==memcmp(data+i, ogg_page_header, 4)) { - int j,len; + for (i = 0; i < data_len; ++i) + { + if (data[i] == 0x4f) + { + if (0 == memcmp(data + i, ogg_page_header, 4)) + { + int j, len; uint32 crc; // make sure we have the whole page header - if (i+26 >= data_len || i+27+data[i+26] >= data_len) { + if (i + 26 >= data_len || i + 27 + data[i + 26] >= data_len) + { // only read up to this page start, so hopefully we'll // have the whole page header start next time data_len = i; break; } // ok, we have it all; compute the length of the page - len = 27 + data[i+26]; - for (j=0; j < data[i+26]; ++j) - len += data[i+27+j]; + len = 27 + data[i + 26]; + for (j = 0; j < data[i + 26]; ++j) + len += data[i + 27 + j]; // scan everything up to the embedded crc (which we must 0) crc = 0; - for (j=0; j < 22; ++j) - crc = crc32_update(crc, data[i+j]); + for (j = 0; j < 22; ++j) + crc = crc32_update(crc, data[i + j]); // now process 4 0-bytes - for ( ; j < 26; ++j) + for (; j < 26; ++j) crc = crc32_update(crc, 0); // len is the total number of bytes we need to scan n = f->page_crc_tests++; - f->scan[n].bytes_left = len-j; + f->scan[n].bytes_left = len - j; f->scan[n].crc_so_far = crc; - f->scan[n].goal_crc = data[i+22] + (data[i+23] << 8) + (data[i+24]<<16) + (data[i+25]<<24); + f->scan[n].goal_crc = data[i + 22] + (data[i + 23] << 8) + (data[i + 24] << 16) + (data[i + 25] << 24); // if the last frame on a page is continued to the next, then // we can't recover the sample_loc immediately - if (data[i+27+data[i+26]-1] == 255) + if (data[i + 27 + data[i + 26] - 1] == 255) f->scan[n].sample_loc = ~0; else - f->scan[n].sample_loc = data[i+6] + (data[i+7] << 8) + (data[i+ 8]<<16) + (data[i+ 9]<<24); - f->scan[n].bytes_done = i+j; + f->scan[n].sample_loc = data[i + 6] + (data[i + 7] << 8) + (data[i + 8] << 16) + (data[i + 9] << 24); + f->scan[n].bytes_done = i + j; if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT) break; // keep going if we still have room for more @@ -4388,34 +4926,40 @@ static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len) } } - for (i=0; i < f->page_crc_tests;) { + for (i = 0; i < f->page_crc_tests;) + { uint32 crc; int j; int n = f->scan[i].bytes_done; int m = f->scan[i].bytes_left; - if (m > data_len - n) m = data_len - n; + if (m > data_len - n) + m = data_len - n; // m is the bytes to scan in the current chunk crc = f->scan[i].crc_so_far; - for (j=0; j < m; ++j) - crc = crc32_update(crc, data[n+j]); + for (j = 0; j < m; ++j) + crc = crc32_update(crc, data[n + j]); f->scan[i].bytes_left -= m; f->scan[i].crc_so_far = crc; - if (f->scan[i].bytes_left == 0) { + if (f->scan[i].bytes_left == 0) + { // does it match? - if (f->scan[i].crc_so_far == f->scan[i].goal_crc) { + if (f->scan[i].crc_so_far == f->scan[i].goal_crc) + { // Houston, we have page - data_len = n+m; // consumption amount is wherever that scan ended - f->page_crc_tests = -1; // drop out of page scan mode - f->previous_length = 0; // decode-but-don't-output one frame - f->next_seg = -1; // start a new page + data_len = n + m; // consumption amount is wherever that scan ended + f->page_crc_tests = -1; // drop out of page scan mode + f->previous_length = 0; // decode-but-don't-output one frame + f->next_seg = -1; // start a new page f->current_loc = f->scan[i].sample_loc; // set the current sample location - // to the amount we'd have decoded had we decoded this page + // to the amount we'd have decoded had we decoded this page f->current_loc_valid = f->current_loc != ~0U; return data_len; } // delete entry f->scan[i] = f->scan[--f->page_crc_tests]; - } else { + } + else + { ++i; } } @@ -4425,53 +4969,62 @@ static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len) // return value: number of bytes we used int stb_vorbis_decode_frame_pushdata( - stb_vorbis *f, // the file we're decoding - const uint8 *data, int data_len, // the memory available for decoding - int *channels, // place to write number of float * buffers - float ***output, // place to write float ** array of float * buffers - int *samples // place to write number of output samples - ) + stb_vorbis *f, // the file we're decoding + const uint8 *data, int data_len, // the memory available for decoding + int *channels, // place to write number of float * buffers + float ***output, // place to write float ** array of float * buffers + int *samples // place to write number of output samples +) { int i; - int len,right,left; + int len, right, left; - if (!IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + if (!IS_PUSH_MODE(f)) + return error(f, VORBIS_invalid_api_mixing); - if (f->page_crc_tests >= 0) { + if (f->page_crc_tests >= 0) + { *samples = 0; - return vorbis_search_for_page_pushdata(f, (uint8 *) data, data_len); + return vorbis_search_for_page_pushdata(f, (uint8 *)data, data_len); } - f->stream = (uint8 *) data; - f->stream_end = (uint8 *) data + data_len; - f->error = VORBIS__no_error; + f->stream = (uint8 *)data; + f->stream_end = (uint8 *)data + data_len; + f->error = VORBIS__no_error; // check that we have the entire packet in memory - if (!is_whole_packet_present(f)) { + if (!is_whole_packet_present(f)) + { *samples = 0; return 0; } - if (!vorbis_decode_packet(f, &len, &left, &right)) { + if (!vorbis_decode_packet(f, &len, &left, &right)) + { // save the actual error we encountered enum STBVorbisError error = f->error; - if (error == VORBIS_bad_packet_type) { + if (error == VORBIS_bad_packet_type) + { // flush and resynch f->error = VORBIS__no_error; while (get8_packet(f) != EOP) - if (f->eof) break; + if (f->eof) + break; *samples = 0; - return (int) (f->stream - data); + return (int)(f->stream - data); } - if (error == VORBIS_continued_packet_flag_invalid) { - if (f->previous_length == 0) { + if (error == VORBIS_continued_packet_flag_invalid) + { + if (f->previous_length == 0) + { // we may be resynching, in which case it's ok to hit one // of these; just discard the packet f->error = VORBIS__no_error; while (get8_packet(f) != EOP) - if (f->eof) break; + if (f->eof) + break; *samples = 0; - return (int) (f->stream - data); + return (int)(f->stream - data); } } // if we get an error while parsing, what to do? @@ -4485,26 +5038,28 @@ int stb_vorbis_decode_frame_pushdata( // success! len = vorbis_finish_frame(f, len, left, right); - for (i=0; i < f->channels; ++i) + for (i = 0; i < f->channels; ++i) f->outputs[i] = f->channel_buffers[i] + left; - if (channels) *channels = f->channels; + if (channels) + *channels = f->channels; *samples = len; *output = f->outputs; - return (int) (f->stream - data); + return (int)(f->stream - data); } stb_vorbis *stb_vorbis_open_pushdata( - const unsigned char *data, int data_len, // the memory available for decoding - int *data_used, // only defined if result is not NULL - int *error, const stb_vorbis_alloc *alloc) + const unsigned char *data, int data_len, // the memory available for decoding + int *data_used, // only defined if result is not NULL + int *error, const stb_vorbis_alloc *alloc) { stb_vorbis *f, p; vorbis_init(&p, alloc); - p.stream = (uint8 *) data; - p.stream_end = (uint8 *) data + data_len; - p.push_mode = TRUE; - if (!start_decoder(&p)) { + p.stream = (uint8 *)data; + p.stream_end = (uint8 *)data + data_len; + p.push_mode = TRUE; + if (!start_decoder(&p)) + { if (p.eof) *error = VORBIS_need_more_data; else @@ -4512,12 +5067,15 @@ stb_vorbis *stb_vorbis_open_pushdata( return NULL; } f = vorbis_alloc(&p); - if (f) { + if (f) + { *f = p; - *data_used = (int) (f->stream - data); + *data_used = (int)(f->stream - data); *error = 0; return f; - } else { + } + else + { vorbis_deinit(&p); return NULL; } @@ -4526,13 +5084,15 @@ stb_vorbis *stb_vorbis_open_pushdata( unsigned int stb_vorbis_get_file_offset(stb_vorbis *f) { - #ifndef STB_VORBIS_NO_PUSHDATA_API - if (f->push_mode) return 0; - #endif - if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start); - #ifndef STB_VORBIS_NO_STDIO - return (unsigned int) (ftell(f->f) - f->f_start); - #endif +#ifndef STB_VORBIS_NO_PUSHDATA_API + if (f->push_mode) + return 0; +#endif + if (USE_MEMORY(f)) + return (unsigned int)(f->stream - f->stream_start); +#ifndef STB_VORBIS_NO_STDIO + return (unsigned int)(ftell(f->f) - f->f_start); +#endif } #ifndef STB_VORBIS_NO_PULLDATA_API @@ -4542,47 +5102,57 @@ unsigned int stb_vorbis_get_file_offset(stb_vorbis *f) static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) { - for(;;) { + for (;;) + { int n; - if (f->eof) return 0; + if (f->eof) + return 0; n = get8(f); - if (n == 0x4f) { // page header candidate + if (n == 0x4f) + { // page header candidate unsigned int retry_loc = stb_vorbis_get_file_offset(f); int i; // check if we're off the end of a file_section stream if (retry_loc - 25 > f->stream_len) return 0; // check the rest of the header - for (i=1; i < 4; ++i) + for (i = 1; i < 4; ++i) if (get8(f) != ogg_page_header[i]) break; - if (f->eof) return 0; - if (i == 4) { + if (f->eof) + return 0; + if (i == 4) + { uint8 header[27]; uint32 i, crc, goal, len; - for (i=0; i < 4; ++i) + for (i = 0; i < 4; ++i) header[i] = ogg_page_header[i]; for (; i < 27; ++i) header[i] = get8(f); - if (f->eof) return 0; - if (header[4] != 0) goto invalid; - goal = header[22] + (header[23] << 8) + (header[24]<<16) + (header[25]<<24); - for (i=22; i < 26; ++i) + if (f->eof) + return 0; + if (header[4] != 0) + goto invalid; + goal = header[22] + (header[23] << 8) + (header[24] << 16) + (header[25] << 24); + for (i = 22; i < 26; ++i) header[i] = 0; crc = 0; - for (i=0; i < 27; ++i) + for (i = 0; i < 27; ++i) crc = crc32_update(crc, header[i]); len = 0; - for (i=0; i < header[26]; ++i) { + for (i = 0; i < header[26]; ++i) + { int s = get8(f); crc = crc32_update(crc, s); len += s; } - if (len && f->eof) return 0; - for (i=0; i < len; ++i) + if (len && f->eof) + return 0; + for (i = 0; i < len; ++i) crc = crc32_update(crc, get8(f)); // finished parsing probable page - if (crc == goal) { + if (crc == goal) + { // we could now check that it's either got the last // page flag set, OR it's followed by the capture // pattern, but I guess TECHNICALLY you could have @@ -4593,25 +5163,25 @@ static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) // invalid-but-useful files? if (end) *end = stb_vorbis_get_file_offset(f); - if (last) { + if (last) + { if (header[5] & 0x04) *last = 1; else *last = 0; } - set_file_offset(f, retry_loc-1); + set_file_offset(f, retry_loc - 1); return 1; } } - invalid: + invalid: // not a valid page, so rewind and look for next one set_file_offset(f, retry_loc); } } } - -#define SAMPLE_unknown 0xffffffff +#define SAMPLE_unknown 0xffffffff // seeking is implemented with a binary search, which narrows down the range to // 64K, before using a linear search (because finding the synchronization @@ -4625,7 +5195,7 @@ static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) static int get_seek_page_info(stb_vorbis *f, ProbedPage *z) { uint8 header[27], lacing[255]; - int i,len; + int i, len; // record where the page starts z->page_start = stb_vorbis_get_file_offset(f); @@ -4638,7 +5208,7 @@ static int get_seek_page_info(stb_vorbis *f, ProbedPage *z) // determine the length of the payload len = 0; - for (i=0; i < header[26]; ++i) + for (i = 0; i < header[26]; ++i) len += lacing[i]; // this implies where the page ends @@ -4659,14 +5229,15 @@ static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset) unsigned int previous_safe, end; // now we want to seek back 64K from the limit - if (limit_offset >= 65536 && limit_offset-65536 >= f->first_audio_page_offset) + if (limit_offset >= 65536 && limit_offset - 65536 >= f->first_audio_page_offset) previous_safe = limit_offset - 65536; else previous_safe = f->first_audio_page_offset; set_file_offset(f, previous_safe); - while (vorbis_find_page(f, &end, NULL)) { + while (vorbis_find_page(f, &end, NULL)) + { if (end >= limit_offset && stb_vorbis_get_file_offset(f) < limit_offset) return 1; set_file_offset(f, end); @@ -4689,8 +5260,10 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) // find the last page and validate the target sample stream_length = stb_vorbis_stream_length_in_samples(f); - if (stream_length == 0) return error(f, VORBIS_seek_without_length); - if (sample_number > stream_length) return error(f, VORBIS_seek_invalid); + if (stream_length == 0) + return error(f, VORBIS_seek_without_length); + if (sample_number > stream_length) + return error(f, VORBIS_seek_invalid); // this is the maximum difference between the window-center (which is the // actual granule position value), and the right-start (which the spec @@ -4702,18 +5275,22 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) last_sample_limit = sample_number - padding; left = f->p_first; - while (left.last_decoded_sample == ~0U) { + while (left.last_decoded_sample == ~0U) + { // (untested) the first page does not have a 'last_decoded_sample' set_file_offset(f, left.page_end); - if (!get_seek_page_info(f, &left)) goto error; + if (!get_seek_page_info(f, &left)) + goto error; } right = f->p_last; assert(right.last_decoded_sample != ~0U); // starting from the start is handled differently - if (last_sample_limit <= left.last_decoded_sample) { - if (stb_vorbis_seek_start(f)) { + if (last_sample_limit <= left.last_decoded_sample) + { + if (stb_vorbis_seek_start(f)) + { if (f->current_loc > sample_number) return error(f, VORBIS_seek_failed); return 1; @@ -4721,25 +5298,35 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) return 0; } - while (left.page_end != right.page_start) { + while (left.page_end != right.page_start) + { assert(left.page_end < right.page_start); // search range in bytes delta = right.page_start - left.page_end; - if (delta <= 65536) { + if (delta <= 65536) + { // there's only 64K left to search - handle it linearly set_file_offset(f, left.page_end); - } else { - if (probe < 2) { - if (probe == 0) { + } + else + { + if (probe < 2) + { + if (probe == 0) + { // first probe (interpolate) double data_bytes = right.page_end - left.page_start; bytes_per_sample = data_bytes / right.last_decoded_sample; offset = left.page_start + bytes_per_sample * (last_sample_limit - left.last_decoded_sample); - } else { + } + else + { // second probe (try to bound the other side) - double error = ((double) last_sample_limit - mid.last_decoded_sample) * bytes_per_sample; - if (error >= 0 && error < 8000) error = 8000; - if (error < 0 && error > -8000) error = -8000; + double error = ((double)last_sample_limit - mid.last_decoded_sample) * bytes_per_sample; + if (error >= 0 && error < 8000) + error = 8000; + if (error < 0 && error > -8000) + error = -8000; offset += error * 2; } @@ -4749,19 +5336,25 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) if (offset > right.page_start - 65536) offset = right.page_start - 65536; - set_file_offset(f, (unsigned int) offset); - } else { + set_file_offset(f, (unsigned int)offset); + } + else + { // binary search for large ranges (offset by 32K to ensure // we don't hit the right page) set_file_offset(f, left.page_end + (delta / 2) - 32768); } - if (!vorbis_find_page(f, NULL, NULL)) goto error; + if (!vorbis_find_page(f, NULL, NULL)) + goto error; } - for (;;) { - if (!get_seek_page_info(f, &mid)) goto error; - if (mid.last_decoded_sample != ~0U) break; + for (;;) + { + if (!get_seek_page_info(f, &mid)) + goto error; + if (mid.last_decoded_sample != ~0U) + break; // (untested) no frames end on this page set_file_offset(f, mid.page_end); assert(mid.page_start < right.page_start); @@ -4769,10 +5362,13 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) // if we've just found the last page again then we're in a tricky file, // and we're close enough (if it wasn't an interpolation probe). - if (mid.page_start == right.page_start) { + if (mid.page_start == right.page_start) + { if (probe >= 2 || delta <= 65536) break; - } else { + } + else + { if (last_sample_limit < mid.last_decoded_sample) right = mid; else @@ -4785,13 +5381,15 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) // seek back to start of the last packet page_start = left.page_start; set_file_offset(f, page_start); - if (!start_page(f)) return error(f, VORBIS_seek_failed); + if (!start_page(f)) + return error(f, VORBIS_seek_failed); end_pos = f->end_seg_with_known_loc; assert(end_pos >= 0); - for (;;) { + for (;;) + { for (i = end_pos; i > 0; --i) - if (f->segments[i-1] != 255) + if (f->segments[i - 1] != 255) break; start_seg_with_known_loc = i; @@ -4804,7 +5402,8 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) goto error; page_start = stb_vorbis_get_file_offset(f); - if (!start_page(f)) goto error; + if (!start_page(f)) + goto error; end_pos = f->segment_count - 1; } @@ -4842,7 +5441,7 @@ static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int return 0; // either 1 or 2 bytes were read, figure out which so we can rewind - bits_read = 1 + ilog(f->mode_count-1); + bits_read = 1 + ilog(f->mode_count - 1); if (f->mode_config[*mode].blockflag) bits_read += 2; bytes_read = (bits_read + 7) / 8; @@ -4863,7 +5462,8 @@ int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) { uint32 max_frame_samples; - if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + if (IS_PUSH_MODE(f)) + return error(f, VORBIS_invalid_api_mixing); // fast page-level search if (!seek_to_sample_coarse(f, sample_number)) @@ -4873,19 +5473,25 @@ int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) assert(f->current_loc <= sample_number); // linear search for the relevant packet - max_frame_samples = (f->blocksize_1*3 - f->blocksize_0) >> 2; - while (f->current_loc < sample_number) { + max_frame_samples = (f->blocksize_1 * 3 - f->blocksize_0) >> 2; + while (f->current_loc < sample_number) + { int left_start, left_end, right_start, right_end, mode, frame_samples; if (!peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode)) return error(f, VORBIS_seek_failed); // calculate the number of samples returned by the next frame frame_samples = right_start - left_start; - if (f->current_loc + frame_samples > sample_number) { + if (f->current_loc + frame_samples > sample_number) + { return 1; // the next frame will contain the sample - } else if (f->current_loc + frame_samples + max_frame_samples > sample_number) { + } + else if (f->current_loc + frame_samples + max_frame_samples > sample_number) + { // there's a chance the frame after this could contain the sample vorbis_pump_first_frame(f); - } else { + } + else + { // this frame is too early to be relevant f->current_loc += frame_samples; f->previous_length = 0; @@ -4894,7 +5500,8 @@ int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) } } // the next frame should start with the sample - if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed); + if (f->current_loc != sample_number) + return error(f, VORBIS_seek_failed); return 1; } @@ -4903,12 +5510,13 @@ int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) if (!stb_vorbis_seek_frame(f, sample_number)) return 0; - if (sample_number != f->current_loc) { + if (sample_number != f->current_loc) + { int n; uint32 frame_start = f->current_loc; stb_vorbis_get_frame_float(f, &n, NULL); assert(sample_number > frame_start); - assert(f->channel_buffer_start + (int) (sample_number-frame_start) <= f->channel_buffer_end); + assert(f->channel_buffer_start + (int)(sample_number - frame_start) <= f->channel_buffer_end); f->channel_buffer_start += (sample_number - frame_start); } @@ -4917,7 +5525,10 @@ int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) int stb_vorbis_seek_start(stb_vorbis *f) { - if (IS_PUSH_MODE(f)) { return error(f, VORBIS_invalid_api_mixing); } + if (IS_PUSH_MODE(f)) + { + return error(f, VORBIS_invalid_api_mixing); + } set_file_offset(f, f->first_audio_page_offset); f->previous_length = 0; f->first_decode = TRUE; @@ -4930,10 +5541,12 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) unsigned int restore_offset, previous_safe; unsigned int end, last_page_loc; - if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); - if (!f->total_samples) { + if (IS_PUSH_MODE(f)) + return error(f, VORBIS_invalid_api_mixing); + if (!f->total_samples) + { unsigned int last; - uint32 lo,hi; + uint32 lo, hi; char header[6]; // first, store the current decode position so we can restore it @@ -4941,7 +5554,7 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) // now we want to seek back 64K from the end (the last page must // be at most a little less than 64K, but let's allow a little slop) - if (f->stream_len >= 65536 && f->stream_len-65536 >= f->first_audio_page_offset) + if (f->stream_len >= 65536 && f->stream_len - 65536 >= f->first_audio_page_offset) previous_safe = f->stream_len - 65536; else previous_safe = f->first_audio_page_offset; @@ -4950,7 +5563,8 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) // previous_safe is now our candidate 'earliest known place that seeking // to will lead to the final page' - if (!vorbis_find_page(f, &end, &last)) { + if (!vorbis_find_page(f, &end, &last)) + { // if we can't find a page, we're hosed! f->error = VORBIS_cant_find_last_page; f->total_samples = 0xffffffff; @@ -4963,14 +5577,16 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) // stop when the last_page flag is set, not when we reach eof; // this allows us to stop short of a 'file_section' end without // explicitly checking the length of the section - while (!last) { + while (!last) + { set_file_offset(f, end); - if (!vorbis_find_page(f, &end, &last)) { + if (!vorbis_find_page(f, &end, &last)) + { // the last page we found didn't have the 'last page' flag // set. whoops! break; } - previous_safe = last_page_loc+1; + previous_safe = last_page_loc + 1; last_page_loc = stb_vorbis_get_file_offset(f); } @@ -4981,7 +5597,8 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) // extract the absolute granule position lo = get32(f); hi = get32(f); - if (lo == 0xffffffff && hi == 0xffffffff) { + if (lo == 0xffffffff && hi == 0xffffffff) + { f->error = VORBIS_cant_find_last_page; f->total_samples = SAMPLE_unknown; goto done; @@ -4991,10 +5608,10 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) f->total_samples = lo; f->p_last.page_start = last_page_loc; - f->p_last.page_end = end; + f->p_last.page_end = end; f->p_last.last_decoded_sample = lo; - done: + done: set_file_offset(f, restore_offset); } return f->total_samples == SAMPLE_unknown ? 0 : f->total_samples; @@ -5002,67 +5619,72 @@ unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) float stb_vorbis_stream_length_in_seconds(stb_vorbis *f) { - return stb_vorbis_stream_length_in_samples(f) / (float) f->sample_rate; + return stb_vorbis_stream_length_in_samples(f) / (float)f->sample_rate; } - - int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output) { - int len, right,left,i; - if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); + int len, right, left, i; + if (IS_PUSH_MODE(f)) + return error(f, VORBIS_invalid_api_mixing); - if (!vorbis_decode_packet(f, &len, &left, &right)) { + if (!vorbis_decode_packet(f, &len, &left, &right)) + { f->channel_buffer_start = f->channel_buffer_end = 0; return 0; } len = vorbis_finish_frame(f, len, left, right); - for (i=0; i < f->channels; ++i) + for (i = 0; i < f->channels; ++i) f->outputs[i] = f->channel_buffers[i] + left; f->channel_buffer_start = left; - f->channel_buffer_end = left+len; + f->channel_buffer_end = left + len; - if (channels) *channels = f->channels; - if (output) *output = f->outputs; + if (channels) + *channels = f->channels; + if (output) + *output = f->outputs; return len; } #ifndef STB_VORBIS_NO_STDIO -stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length) +stb_vorbis *stb_vorbis_open_file_section(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length) { stb_vorbis *f, p; vorbis_init(&p, alloc); p.f = file; - p.f_start = (uint32) ftell(file); - p.stream_len = length; + p.f_start = (uint32)ftell(file); + p.stream_len = length; p.close_on_free = close_on_free; - if (start_decoder(&p)) { + if (start_decoder(&p)) + { f = vorbis_alloc(&p); - if (f) { + if (f) + { *f = p; vorbis_pump_first_frame(f); return f; } } - if (error) *error = p.error; + if (error) + *error = p.error; vorbis_deinit(&p); return NULL; } -stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc) +stb_vorbis *stb_vorbis_open_file(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc) { unsigned int len, start; - start = (unsigned int) ftell(file); + start = (unsigned int)ftell(file); fseek(file, 0, SEEK_END); - len = (unsigned int) (ftell(file) - start); + len = (unsigned int)(ftell(file) - start); fseek(file, start, SEEK_SET); return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len); } -stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc) +stb_vorbis *stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc) { FILE *f; #if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__) @@ -5073,82 +5695,89 @@ stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const st #endif if (f) return stb_vorbis_open_file(f, TRUE, error, alloc); - if (error) *error = VORBIS_file_open_failure; + if (error) + *error = VORBIS_file_open_failure; return NULL; } #endif // STB_VORBIS_NO_STDIO -stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc) +stb_vorbis *stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc) { stb_vorbis *f, p; - if (data == NULL) return NULL; + if (data == NULL) + return NULL; vorbis_init(&p, alloc); - p.stream = (uint8 *) data; - p.stream_end = (uint8 *) data + len; - p.stream_start = (uint8 *) p.stream; + p.stream = (uint8 *)data; + p.stream_end = (uint8 *)data + len; + p.stream_start = (uint8 *)p.stream; p.stream_len = len; p.push_mode = FALSE; - if (start_decoder(&p)) { + if (start_decoder(&p)) + { f = vorbis_alloc(&p); - if (f) { + if (f) + { *f = p; vorbis_pump_first_frame(f); - if (error) *error = VORBIS__no_error; + if (error) + *error = VORBIS__no_error; return f; } } - if (error) *error = p.error; + if (error) + *error = p.error; vorbis_deinit(&p); return NULL; } #ifndef STB_VORBIS_NO_INTEGER_CONVERSION -#define PLAYBACK_MONO 1 -#define PLAYBACK_LEFT 2 -#define PLAYBACK_RIGHT 4 +#define PLAYBACK_MONO 1 +#define PLAYBACK_LEFT 2 +#define PLAYBACK_RIGHT 4 -#define L (PLAYBACK_LEFT | PLAYBACK_MONO) -#define C (PLAYBACK_LEFT | PLAYBACK_RIGHT | PLAYBACK_MONO) -#define R (PLAYBACK_RIGHT | PLAYBACK_MONO) +#define L (PLAYBACK_LEFT | PLAYBACK_MONO) +#define C (PLAYBACK_LEFT | PLAYBACK_RIGHT | PLAYBACK_MONO) +#define R (PLAYBACK_RIGHT | PLAYBACK_MONO) static int8 channel_position[7][6] = -{ - { 0 }, - { C }, - { L, R }, - { L, C, R }, - { L, R, L, R }, - { L, C, R, L, R }, - { L, C, R, L, R, C }, + { + {0}, + {C}, + {L, R}, + {L, C, R}, + {L, R, L, R}, + {L, C, R, L, R}, + {L, C, R, L, R, C}, }; - #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT - typedef union { - float f; - int i; - } float_conv; - typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4]; - #define FASTDEF(x) float_conv x - // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round - #define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT)) - #define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22)) - #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s)) - #define check_endianness() +typedef union +{ + float f; + int i; +} float_conv; +typedef char stb_vorbis_float_size_test[sizeof(float) == 4 && sizeof(int) == 4]; +#define FASTDEF(x) float_conv x +// add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round +#define MAGIC(SHIFT) (1.5f * (1 << (23 - SHIFT)) + 0.5f / (1 << SHIFT)) +#define ADDEND(SHIFT) (((150 - SHIFT) << 23) + (1 << 22)) +#define FAST_SCALED_FLOAT_TO_INT(temp, x, s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s)) +#define check_endianness() #else - #define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s)))) - #define check_endianness() - #define FASTDEF(x) +#define FAST_SCALED_FLOAT_TO_INT(temp, x, s) ((int)((x) * (1 << (s)))) +#define check_endianness() +#define FASTDEF(x) #endif static void copy_samples(short *dest, float *src, int len) { int i; check_endianness(); - for (i=0; i < len; ++i) { + for (i = 0; i < len; ++i) + { FASTDEF(temp); - int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i],15); - if ((unsigned int) (v + 32768) > 65535) + int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i], 15); + if ((unsigned int)(v + 32768) > 65535) v = v < 0 ? -32768 : 32767; dest[i] = v; } @@ -5156,64 +5785,81 @@ static void copy_samples(short *dest, float *src, int len) static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len) { - #define BUFFER_SIZE 32 +#define BUFFER_SIZE 32 float buffer[BUFFER_SIZE]; - int i,j,o,n = BUFFER_SIZE; + int i, j, o, n = BUFFER_SIZE; check_endianness(); - for (o = 0; o < len; o += BUFFER_SIZE) { + for (o = 0; o < len; o += BUFFER_SIZE) + { memset(buffer, 0, sizeof(buffer)); - if (o + n > len) n = len - o; - for (j=0; j < num_c; ++j) { - if (channel_position[num_c][j] & mask) { - for (i=0; i < n; ++i) - buffer[i] += data[j][d_offset+o+i]; + if (o + n > len) + n = len - o; + for (j = 0; j < num_c; ++j) + { + if (channel_position[num_c][j] & mask) + { + for (i = 0; i < n; ++i) + buffer[i] += data[j][d_offset + o + i]; } } - for (i=0; i < n; ++i) { + for (i = 0; i < n; ++i) + { FASTDEF(temp); - int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15); - if ((unsigned int) (v + 32768) > 65535) + int v = FAST_SCALED_FLOAT_TO_INT(temp, buffer[i], 15); + if ((unsigned int)(v + 32768) > 65535) v = v < 0 ? -32768 : 32767; - output[o+i] = v; + output[o + i] = v; } } } static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len) { - #define BUFFER_SIZE 32 +#define BUFFER_SIZE 32 float buffer[BUFFER_SIZE]; - int i,j,o,n = BUFFER_SIZE >> 1; + int i, j, o, n = BUFFER_SIZE >> 1; // o is the offset in the source data check_endianness(); - for (o = 0; o < len; o += BUFFER_SIZE >> 1) { + for (o = 0; o < len; o += BUFFER_SIZE >> 1) + { // o2 is the offset in the output data int o2 = o << 1; memset(buffer, 0, sizeof(buffer)); - if (o + n > len) n = len - o; - for (j=0; j < num_c; ++j) { + if (o + n > len) + n = len - o; + for (j = 0; j < num_c; ++j) + { int m = channel_position[num_c][j] & (PLAYBACK_LEFT | PLAYBACK_RIGHT); - if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) { - for (i=0; i < n; ++i) { - buffer[i*2+0] += data[j][d_offset+o+i]; - buffer[i*2+1] += data[j][d_offset+o+i]; + if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) + { + for (i = 0; i < n; ++i) + { + buffer[i * 2 + 0] += data[j][d_offset + o + i]; + buffer[i * 2 + 1] += data[j][d_offset + o + i]; } - } else if (m == PLAYBACK_LEFT) { - for (i=0; i < n; ++i) { - buffer[i*2+0] += data[j][d_offset+o+i]; + } + else if (m == PLAYBACK_LEFT) + { + for (i = 0; i < n; ++i) + { + buffer[i * 2 + 0] += data[j][d_offset + o + i]; } - } else if (m == PLAYBACK_RIGHT) { - for (i=0; i < n; ++i) { - buffer[i*2+1] += data[j][d_offset+o+i]; + } + else if (m == PLAYBACK_RIGHT) + { + for (i = 0; i < n; ++i) + { + buffer[i * 2 + 1] += data[j][d_offset + o + i]; } } } - for (i=0; i < (n<<1); ++i) { + for (i = 0; i < (n << 1); ++i) + { FASTDEF(temp); - int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15); - if ((unsigned int) (v + 32768) > 65535) + int v = FAST_SCALED_FLOAT_TO_INT(temp, buffer[i], 15); + if ((unsigned int)(v + 32768) > 65535) v = v < 0 ? -32768 : 32767; - output[o2+i] = v; + output[o2 + i] = v; } } } @@ -5221,16 +5867,19 @@ static void compute_stereo_samples(short *output, int num_c, float **data, int d static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples) { int i; - if (buf_c != data_c && buf_c <= 2 && data_c <= 6) { - static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} }; - for (i=0; i < buf_c; ++i) - compute_samples(channel_selector[buf_c][i], buffer[i]+b_offset, data_c, data, d_offset, samples); - } else { + if (buf_c != data_c && buf_c <= 2 && data_c <= 6) + { + static int channel_selector[3][2] = {{0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT}}; + for (i = 0; i < buf_c; ++i) + compute_samples(channel_selector[buf_c][i], buffer[i] + b_offset, data_c, data, d_offset, samples); + } + else + { int limit = buf_c < data_c ? buf_c : data_c; - for (i=0; i < limit; ++i) - copy_samples(buffer[i]+b_offset, data[i]+d_offset, samples); - for ( ; i < buf_c; ++i) - memset(buffer[i]+b_offset, 0, sizeof(short) * samples); + for (i = 0; i < limit; ++i) + copy_samples(buffer[i] + b_offset, data[i] + d_offset, samples); + for (; i < buf_c; ++i) + memset(buffer[i] + b_offset, 0, sizeof(short) * samples); } } @@ -5238,7 +5887,8 @@ int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num { float **output = NULL; int len = stb_vorbis_get_frame_float(f, NULL, &output); - if (len > num_samples) len = num_samples; + if (len > num_samples) + len = num_samples; if (len) convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len); return len; @@ -5248,23 +5898,28 @@ static void convert_channels_short_interleaved(int buf_c, short *buffer, int dat { int i; check_endianness(); - if (buf_c != data_c && buf_c <= 2 && data_c <= 6) { + if (buf_c != data_c && buf_c <= 2 && data_c <= 6) + { assert(buf_c == 2); - for (i=0; i < buf_c; ++i) + for (i = 0; i < buf_c; ++i) compute_stereo_samples(buffer, data_c, data, d_offset, len); - } else { + } + else + { int limit = buf_c < data_c ? buf_c : data_c; int j; - for (j=0; j < len; ++j) { - for (i=0; i < limit; ++i) { + for (j = 0; j < len; ++j) + { + for (i = 0; i < limit; ++i) + { FASTDEF(temp); - float f = data[i][d_offset+j]; - int v = FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15); - if ((unsigned int) (v + 32768) > 65535) + float f = data[i][d_offset + j]; + int v = FAST_SCALED_FLOAT_TO_INT(temp, f, 15); // data[i][d_offset+j],15); + if ((unsigned int)(v + 32768) > 65535) v = v < 0 ? -32768 : 32767; *buffer++ = v; } - for ( ; i < buf_c; ++i) + for (; i < buf_c; ++i) *buffer++ = 0; } } @@ -5274,10 +5929,13 @@ int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buff { float **output; int len; - if (num_c == 1) return stb_vorbis_get_frame_short(f,num_c,&buffer, num_shorts); + if (num_c == 1) + return stb_vorbis_get_frame_short(f, num_c, &buffer, num_shorts); len = stb_vorbis_get_frame_float(f, NULL, &output); - if (len) { - if (len*num_c > num_shorts) len = num_shorts / num_c; + if (len) + { + if (len * num_c > num_shorts) + len = num_shorts / num_c; convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len); } return len; @@ -5287,19 +5945,24 @@ int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short { float **outputs; int len = num_shorts / channels; - int n=0; + int n = 0; int z = f->channels; - if (z > channels) z = channels; - while (n < len) { + if (z > channels) + z = channels; + while (n < len) + { int k = f->channel_buffer_end - f->channel_buffer_start; - if (n+k >= len) k = len - n; + if (n + k >= len) + k = len - n; if (k) convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k); - buffer += k*channels; + buffer += k * channels; n += k; f->channel_buffer_start += k; - if (n == len) break; - if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; + if (n == len) + break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) + break; } return n; } @@ -5307,18 +5970,23 @@ int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len) { float **outputs; - int n=0; + int n = 0; int z = f->channels; - if (z > channels) z = channels; - while (n < len) { + if (z > channels) + z = channels; + while (n < len) + { int k = f->channel_buffer_end - f->channel_buffer_start; - if (n+k >= len) k = len - n; + if (n + k >= len) + k = len - n; if (k) convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k); n += k; f->channel_buffer_start += k; - if (n == len) break; - if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; + if (n == len) + break; + if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) + break; } return n; } @@ -5329,28 +5997,34 @@ int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_ int data_len, offset, total, limit, error; short *data; stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL); - if (v == NULL) return -1; + if (v == NULL) + return -1; limit = v->channels * 4096; *channels = v->channels; if (sample_rate) *sample_rate = v->sample_rate; offset = data_len = 0; total = limit; - data = (short *) malloc(total * sizeof(*data)); - if (data == NULL) { + data = (short *)malloc(total * sizeof(*data)); + if (data == NULL) + { stb_vorbis_close(v); return -2; } - for (;;) { - int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset); - if (n == 0) break; + for (;;) + { + int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data + offset, total - offset); + if (n == 0) + break; data_len += n; offset += n * v->channels; - if (offset + limit > total) { + if (offset + limit > total) + { short *data2; total *= 2; - data2 = (short *) realloc(data, total * sizeof(*data)); - if (data2 == NULL) { + data2 = (short *)realloc(data, total * sizeof(*data)); + if (data2 == NULL) + { free(data); stb_vorbis_close(v); return -2; @@ -5369,28 +6043,34 @@ int stb_vorbis_decode_memory(const uint8 *mem, int len, int *channels, int *samp int data_len, offset, total, limit, error; short *data; stb_vorbis *v = stb_vorbis_open_memory(mem, len, &error, NULL); - if (v == NULL) return -1; + if (v == NULL) + return -1; limit = v->channels * 4096; *channels = v->channels; if (sample_rate) *sample_rate = v->sample_rate; offset = data_len = 0; total = limit; - data = (short *) malloc(total * sizeof(*data)); - if (data == NULL) { + data = (short *)malloc(total * sizeof(*data)); + if (data == NULL) + { stb_vorbis_close(v); return -2; } - for (;;) { - int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset); - if (n == 0) break; + for (;;) + { + int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data + offset, total - offset); + if (n == 0) + break; data_len += n; offset += n * v->channels; - if (offset + limit > total) { + if (offset + limit > total) + { short *data2; total *= 2; - data2 = (short *) realloc(data, total * sizeof(*data)); - if (data2 == NULL) { + data2 = (short *)realloc(data, total * sizeof(*data)); + if (data2 == NULL) + { free(data); stb_vorbis_close(v); return -2; @@ -5408,17 +6088,21 @@ int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float { float **outputs; int len = num_floats / channels; - int n=0; + int n = 0; int z = f->channels; - if (z > channels) z = channels; - while (n < len) { - int i,j; + if (z > channels) + z = channels; + while (n < len) + { + int i, j; int k = f->channel_buffer_end - f->channel_buffer_start; - if (n+k >= len) k = len - n; - for (j=0; j < k; ++j) { - for (i=0; i < z; ++i) - *buffer++ = f->channel_buffers[i][f->channel_buffer_start+j]; - for ( ; i < channels; ++i) + if (n + k >= len) + k = len - n; + for (j = 0; j < k; ++j) + { + for (i = 0; i < z; ++i) + *buffer++ = f->channel_buffers[i][f->channel_buffer_start + j]; + for (; i < channels; ++i) *buffer++ = 0; } n += k; @@ -5434,18 +6118,22 @@ int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples) { float **outputs; - int n=0; + int n = 0; int z = f->channels; - if (z > channels) z = channels; - while (n < num_samples) { + if (z > channels) + z = channels; + while (n < num_samples) + { int i; int k = f->channel_buffer_end - f->channel_buffer_start; - if (n+k >= num_samples) k = num_samples - n; - if (k) { - for (i=0; i < z; ++i) - memcpy(buffer[i]+n, f->channel_buffers[i]+f->channel_buffer_start, sizeof(float)*k); - for ( ; i < channels; ++i) - memset(buffer[i]+n, 0, sizeof(float) * k); + if (n + k >= num_samples) + k = num_samples - n; + if (k) + { + for (i = 0; i < z; ++i) + memcpy(buffer[i] + n, f->channel_buffers[i] + f->channel_buffer_start, sizeof(float) * k); + for (; i < channels; ++i) + memset(buffer[i] + n, 0, sizeof(float) * k); } n += k; f->channel_buffer_start += k; @@ -5521,7 +6209,6 @@ int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, in #endif // STB_VORBIS_HEADER_ONLY - /* ------------------------------------------------------------------------------ This software is available under 2 licenses -- choose whichever you prefer. diff --git a/source/fade.cpp b/source/fade.cpp index 0431e9d..1aca11c 100644 --- a/source/fade.cpp +++ b/source/fade.cpp @@ -1,10 +1,10 @@ #include "fade.h" -#include // Para SDL_BLENDMODE_BLEND, SDL_BLENDMODE_NONE -#include // Para SDL_PIXELFORMAT_RGBA8888 -#include // Para rand -#include // Para min, max -#include "param.h" // Para Param, param, ParamGame, ParamFade -#include "screen.h" // Para Screen +#include // Para SDL_BLENDMODE_BLEND, SDL_BLENDMODE_NONE +#include // Para SDL_PIXELFORMAT_RGBA8888 +#include // Para rand +#include // Para min, max +#include "param.h" // Para Param, param, ParamGame, ParamFade +#include "screen.h" // Para Screen // Constructor Fade::Fade() diff --git a/source/fade.h b/source/fade.h index 2e997e5..d1dd40d 100644 --- a/source/fade.h +++ b/source/fade.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include "utils.h" diff --git a/source/game_logo.cpp b/source/game_logo.cpp index 923f9cf..3b370bd 100644 --- a/source/game_logo.cpp +++ b/source/game_logo.cpp @@ -1,15 +1,15 @@ #include "game_logo.h" -#include // Para SDL_FlipMode -#include // Para max -#include "animated_sprite.h" // Para AnimatedSprite -#include "audio.h" // Para JA_PlaySound -#include "param.h" // Para Param, param, ParamGame, ParamTitle -#include "resource.h" // Para Resource -#include "screen.h" // Para Screen -#include "smart_sprite.h" // Para SmartSprite -#include "sprite.h" // Para Sprite -#include "texture.h" // Para Texture -#include "utils.h" // Para Color +#include // Para SDL_FlipMode +#include // Para max +#include "animated_sprite.h" // Para AnimatedSprite +#include "audio.h" // Para JA_PlaySound +#include "param.h" // Para Param, param, ParamGame, ParamTitle +#include "resource.h" // Para Resource +#include "screen.h" // Para Screen +#include "smart_sprite.h" // Para SmartSprite +#include "sprite.h" // Para Sprite +#include "texture.h" // Para Texture +#include "utils.h" // Para Color constexpr int ZOOM_FACTOR = 5; constexpr int FLASH_DELAY = 3; diff --git a/source/global_events.cpp b/source/global_events.cpp index 04ffd50..6c52445 100644 --- a/source/global_events.cpp +++ b/source/global_events.cpp @@ -1,7 +1,7 @@ #include "global_events.h" -#include // Para SDL_LogInfo, SDL_LogCategory -#include "mouse.h" // Para handleEvent -#include "section.h" // Para Name, Options, name, options +#include // Para SDL_LogInfo, SDL_LogCategory +#include "mouse.h" // Para handleEvent +#include "section.h" // Para Name, Options, name, options #include "screen.h" namespace GlobalEvents diff --git a/source/global_events.h b/source/global_events.h index c0849ae..247e529 100644 --- a/source/global_events.h +++ b/source/global_events.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace GlobalEvents { diff --git a/source/global_inputs.cpp b/source/global_inputs.cpp index 7443d54..b0add5e 100644 --- a/source/global_inputs.cpp +++ b/source/global_inputs.cpp @@ -1,5 +1,5 @@ #include "global_inputs.h" -#include // Para SDL_RendererLogicalPresentation, SDL_Se... +#include // Para SDL_RendererLogicalPresentation, SDL_Se... #include // Para operator+, allocator, char_traits, string #include // Para vector #include "asset.h" // Para Asset diff --git a/source/input.cpp b/source/input.cpp index eea194e..dd7f7c4 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -1,12 +1,12 @@ #include "input.h" -#include // Para SDL_GetError -#include // Para SDL_INIT_GAMEPAD, SDL_InitSubSystem -#include // Para SDL_GetKeyboardState -#include // Para SDL_LogInfo, SDL_LogCategory, SDL_Log... -#include // Para find -#include // Para distance -#include // Para unordered_map, operator==, _Node_cons... -#include // Para pair +#include // Para SDL_GetError +#include // Para SDL_INIT_GAMEPAD, SDL_InitSubSystem +#include // Para SDL_GetKeyboardState +#include // Para SDL_LogInfo, SDL_LogCategory, SDL_Log... +#include // Para find +#include // Para distance +#include // Para unordered_map, operator==, _Node_cons... +#include // Para pair // Singleton Input *Input::instance_ = nullptr; diff --git a/source/input.h b/source/input.h index 33651d9..4ba0c19 100644 --- a/source/input.h +++ b/source/input.h @@ -1,11 +1,11 @@ #pragma once -#include // Para SDL_GamepadButton, SDL_Gamepad -#include // Para SDL_Joystick, SDL_JoystickID -#include // Para SDL_Scancode -#include // Para Uint8 -#include // Para string -#include // Para vector +#include // Para SDL_GamepadButton, SDL_Gamepad +#include // Para SDL_Joystick, SDL_JoystickID +#include // Para SDL_Scancode +#include // Para Uint8 +#include // Para string +#include // Para vector /* connectedControllers es un vector donde están todos los mandos encontrados [0 .. n] diff --git a/source/item.h b/source/item.h index 890119b..2fb902f 100644 --- a/source/item.h +++ b/source/item.h @@ -1,7 +1,7 @@ #pragma once -#include // Para SDL_FRect -#include // Para Uint16 +#include // Para SDL_FRect +#include // Para Uint16 #include // Para shared_ptr, unique_ptr #include // Para string #include // Para vector diff --git a/source/lang.cpp b/source/lang.cpp index 79e71c6..e1964d9 100644 --- a/source/lang.cpp +++ b/source/lang.cpp @@ -82,9 +82,9 @@ namespace Lang } // Devuelve el código de un idioma a partir de un nombre - Code getCodeFromName(const std::string& name) + Code getCodeFromName(const std::string &name) { - for (const auto& lang : languages) + for (const auto &lang : languages) { if (lang.name == name) return lang.code; @@ -96,7 +96,7 @@ namespace Lang // Devuelve el nombre de un idioma a partir de un código std::string getNameFromCode(Code code) { - for (const auto& lang : languages) + for (const auto &lang : languages) { if (lang.code == code) return lang.name; @@ -151,7 +151,7 @@ namespace Lang } } - // Obtiene una fichero a partir de un lang::Code + // Obtiene una fichero a partir de un lang::Code std::string getLanguageFileName(Lang::Code code) { for (const auto &lang : languages) diff --git a/source/lang.h b/source/lang.h index 6515817..cb84a5a 100644 --- a/source/lang.h +++ b/source/lang.h @@ -37,7 +37,7 @@ namespace Lang Language getLanguage(Code code); // Devuelve el código de un idioma a partir de un nombre - Code getCodeFromName(const std::string& name); + Code getCodeFromName(const std::string &name); // Devuelve el nombre de un idioma a partir de un código std::string getNameFromCode(Code code); diff --git a/source/manage_hiscore_table.cpp b/source/manage_hiscore_table.cpp index c8a9c24..6466436 100644 --- a/source/manage_hiscore_table.cpp +++ b/source/manage_hiscore_table.cpp @@ -1,10 +1,10 @@ #include "manage_hiscore_table.h" -#include // Para SDL_GetError -#include // Para SDL_ReadIO, SDL_WriteIO, SDL_CloseIO -#include // Para SDL_LogCategory, SDL_LogError, SDL_Lo... -#include // Para find_if, sort -#include // Para distance -#include "utils.h" // Para getFileName +#include // Para SDL_GetError +#include // Para SDL_ReadIO, SDL_WriteIO, SDL_CloseIO +#include // Para SDL_LogCategory, SDL_LogError, SDL_Lo... +#include // Para find_if, sort +#include // Para distance +#include "utils.h" // Para getFileName // Resetea la tabla a los valores por defecto void ManageHiScoreTable::clear() diff --git a/source/mouse.cpp b/source/mouse.cpp index a466859..3f5386a 100644 --- a/source/mouse.cpp +++ b/source/mouse.cpp @@ -1,6 +1,6 @@ #include "mouse.h" -#include // Para SDL_ShowCursor -#include // Para SDL_GetTicks +#include // Para SDL_ShowCursor +#include // Para SDL_GetTicks namespace Mouse { diff --git a/source/mouse.h b/source/mouse.h index 598f361..cbb5b5e 100644 --- a/source/mouse.h +++ b/source/mouse.h @@ -1,7 +1,7 @@ #pragma once -#include // Para SDL_Event -#include // Para Uint32 +#include // Para SDL_Event +#include // Para Uint32 namespace Mouse { diff --git a/source/moving_sprite.h b/source/moving_sprite.h index 9ab175e..7cebe69 100644 --- a/source/moving_sprite.h +++ b/source/moving_sprite.h @@ -1,10 +1,10 @@ #pragma once -#include // Para SDL_FPoint, SDL_FRect -#include // Para SDL_FlipMode -#include // Para std::max -#include // Para shared_ptr -#include "sprite.h" // Para Sprite +#include // Para SDL_FPoint, SDL_FRect +#include // Para SDL_FlipMode +#include // Para std::max +#include // Para shared_ptr +#include "sprite.h" // Para Sprite class Texture; diff --git a/source/notifier.cpp b/source/notifier.cpp index 26ad4fa..6ca6932 100644 --- a/source/notifier.cpp +++ b/source/notifier.cpp @@ -1,7 +1,7 @@ #include "notifier.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_PIXELFORMAT_RGBA8888 -#include // Para string +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_PIXELFORMAT_RGBA8888 +#include // Para string #include #include #include "audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla... diff --git a/source/notifier.h b/source/notifier.h index e9c2719..0cf45e1 100644 --- a/source/notifier.h +++ b/source/notifier.h @@ -1,11 +1,11 @@ #pragma once -#include // Para SDL_FRect -#include // Para SDL_Renderer -#include // Para shared_ptr -#include // Para string -#include // Para vector -#include "utils.h" // Para Color +#include // Para SDL_FRect +#include // Para SDL_Renderer +#include // Para shared_ptr +#include // Para string +#include // Para vector +#include "utils.h" // Para Color class Sprite; class Text; diff --git a/source/options.cpp b/source/options.cpp index b33beb6..fcf3d75 100644 --- a/source/options.cpp +++ b/source/options.cpp @@ -1,12 +1,12 @@ #include "options.h" -#include // Para SDL_LogCategory, SDL_LogInfo, SDL_LogError -#include // Para clamp -#include // Para basic_ostream, operator<<, basic_ostream::... -#include // Para swap -#include // Para vector -#include "input.h" // Para InputDeviceToUse -#include "lang.h" // Para Code -#include "utils.h" // Para boolToString, stringToBool, getFileName +#include // Para SDL_LogCategory, SDL_LogInfo, SDL_LogError +#include // Para clamp +#include // Para basic_ostream, operator<<, basic_ostream::... +#include // Para swap +#include // Para vector +#include "input.h" // Para InputDeviceToUse +#include "lang.h" // Para Code +#include "utils.h" // Para boolToString, stringToBool, getFileName #include "asset.h" namespace Options @@ -33,7 +33,7 @@ namespace Options { // Settings settings.config_file = Asset::get()->get("config.txt"); - + // Opciones de control controllers.clear(); controllers.resize(2); diff --git a/source/options.h b/source/options.h index 9fed00c..7288803 100644 --- a/source/options.h +++ b/source/options.h @@ -1,7 +1,7 @@ #pragma once -#include // Para SDL_GamepadButton -#include // Para SDL_ScaleMode +#include // Para SDL_GamepadButton +#include // Para SDL_ScaleMode #include // Para std::string #include // Para std::vector #include "input.h" // Para InputAction, InputDeviceToUse @@ -143,7 +143,7 @@ namespace Options { int index; // Índice en el vector de mandos int player_id; // Jugador asociado al mando - InputDevice type; // Indica si se usará teclado, mando o ambos + InputDevice type; // Indica si se usará teclado, mando o ambos std::string name; // Nombre del dispositivo bool plugged; // Indica si el mando está conectado std::vector inputs; // Listado de acciones asignadas diff --git a/source/param.cpp b/source/param.cpp index 89f3621..837d715 100644 --- a/source/param.cpp +++ b/source/param.cpp @@ -1,10 +1,10 @@ #include "param.h" -#include // Para SDL_LogCategory, SDL_LogError, SDL_LogInfo -#include // Para basic_istream, basic_ifstream, ifstream -#include // Para basic_istringstream -#include // Para runtime_error -#include // Para operator==, stoi, char_traits, string, ope... -#include "utils.h" // Para Zone, Color, NotifyPosition, getFileName +#include // Para SDL_LogCategory, SDL_LogError, SDL_LogInfo +#include // Para basic_istream, basic_ifstream, ifstream +#include // Para basic_istringstream +#include // Para runtime_error +#include // Para operator==, stoi, char_traits, string, ope... +#include "utils.h" // Para Zone, Color, NotifyPosition, getFileName Param param; @@ -80,9 +80,9 @@ void initParam() param.notification.color = Color(48, 48, 48); // INTRO - param.intro.bg_color = Color::fromHex("543149"); - param.intro.card_color = Color::fromHex("CBDBFC"); - param.intro.shadow_color = Color::fromHex("00000080"); + param.intro.bg_color = Color::fromHex("543149"); + param.intro.card_color = Color::fromHex("CBDBFC"); + param.intro.shadow_color = Color::fromHex("00000080"); param.intro.text_distance_from_bottom = 48; } @@ -315,7 +315,7 @@ bool setParams(const std::string &var, const std::string &value) param.title.title_c_c_position = std::stoi(value); } - else if (var == "title.bg_color") + else if (var == "title.bg_color") { param.title.bg_color = Color::fromHex(value); } @@ -461,12 +461,12 @@ bool setParams(const std::string &var, const std::string &value) param.intro.card_color = Color::fromHex(value); } - else if (var == "intro.shadow_color") + else if (var == "intro.shadow_color") { param.intro.shadow_color = Color::fromHex(value); } - else if (var == "intro.text_distance_from_bottom") + else if (var == "intro.text_distance_from_bottom") { param.intro.text_distance_from_bottom = std::stoi(value); } diff --git a/source/param.h b/source/param.h index d9b8eda..74ecd7e 100644 --- a/source/param.h +++ b/source/param.h @@ -1,10 +1,10 @@ #pragma once -#include // Para SDL_FRect -#include // Para Uint32 -#include // Para std::string -#include // Para std::vector -#include "utils.h" // Para Color, Zone, NotifyPosition +#include // Para SDL_FRect +#include // Para Uint32 +#include // Para std::string +#include // Para std::vector +#include "utils.h" // Para Color, Zone, NotifyPosition // --- Parámetros del juego --- struct ParamGame diff --git a/source/path_sprite.h b/source/path_sprite.h index 548e726..05fe000 100644 --- a/source/path_sprite.h +++ b/source/path_sprite.h @@ -1,10 +1,10 @@ #pragma once -#include // Para SDL_FPoint -#include // Para std::function -#include // Para shared_ptr -#include // Para vector -#include "sprite.h" // Para Sprite +#include // Para SDL_FPoint +#include // Para std::function +#include // Para shared_ptr +#include // Para vector +#include "sprite.h" // Para Sprite class Texture; diff --git a/source/player.cpp b/source/player.cpp index 6440804..c19c683 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -1,6 +1,6 @@ #include "player.h" -#include // Para SDL_FlipMode -#include // Para SDL_GetTicks +#include // Para SDL_FlipMode +#include // Para SDL_GetTicks #include // Para rand #include // Para clamp, max, min #include // Para array diff --git a/source/player.h b/source/player.h index 2ae94ee..96b709a 100644 --- a/source/player.h +++ b/source/player.h @@ -1,7 +1,7 @@ #pragma once -#include // Para SDL_FRect -#include // Para Uint32 +#include // Para SDL_FRect +#include // Para Uint32 #include // Para unique_ptr, shared_ptr #include // Para string #include // Para vector diff --git a/source/resource.cpp b/source/resource.cpp index b91920b..68a333d 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -1,5 +1,5 @@ #include "resource.h" -#include // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError +#include // Para SDL_LogInfo, SDL_LogCategory, SDL_LogError #include #include // Para find_if #include diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index f784b9f..3c8b901 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -1,16 +1,16 @@ #include "scoreboard.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_PIXELFORMAT_RGBA8888 -#include // Para SDL_GetTicks -#include // Para roundf -#include // Para operator<<, setfill, setw -#include // Para basic_ostream, basic_ostringstream -#include "enter_name.h" // Para MAX_NAME_LENGHT -#include "lang.h" // Para getText -#include "resource.h" // Para Resource -#include "screen.h" // Para Screen -#include "sprite.h" // Para Sprite -#include "text.h" // Para Text, TEXT_CENTER, TEXT_COLOR +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_PIXELFORMAT_RGBA8888 +#include // Para SDL_GetTicks +#include // Para roundf +#include // Para operator<<, setfill, setw +#include // Para basic_ostream, basic_ostringstream +#include "enter_name.h" // Para MAX_NAME_LENGHT +#include "lang.h" // Para getText +#include "resource.h" // Para Resource +#include "screen.h" // Para Screen +#include "sprite.h" // Para Sprite +#include "text.h" // Para Text, TEXT_CENTER, TEXT_COLOR #include "param.h" // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado @@ -135,7 +135,7 @@ void Scoreboard::setColor(Color color) color_ = color; text_color1_ = param.scoreboard.text_autocolor ? color_.lighten(100) : param.scoreboard.text_color1; text_color2_ = param.scoreboard.text_autocolor ? color_.lighten(150) : param.scoreboard.text_color2; - + // Aplica los colores power_meter_sprite_->getTexture()->setColor(text_color2_); fillBackgroundTexture(); @@ -300,12 +300,12 @@ void Scoreboard::fillPanelTextures() // NAME text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_3_.x, slot4_3_.y, Lang::getText("[SCOREBOARD] 11"), 1, text_color1_); - + /* TEXTO CENTRADO */ text_scoreboard_->writeDX(TEXT_CENTER | TEXT_COLOR, slot4_4_.x, slot4_4_.y, record_name_[i], 1, getColorLikeKnightRider(name_colors_, loop_counter_ / 5)); /* TEXTO A LA IZQUIERDA */ - //text_scoreboard_->writeColored(enter_name_pos_.x, enter_name_pos_.y, record_name_[i], getColorLikeKnightRider(name_colors_, loop_counter_ / 5)); + // text_scoreboard_->writeColored(enter_name_pos_.x, enter_name_pos_.y, record_name_[i], getColorLikeKnightRider(name_colors_, loop_counter_ / 5)); break; } case ScoreboardMode::GAME_COMPLETED: diff --git a/source/scoreboard.h b/source/scoreboard.h index cfff36e..f7d5cd5 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -1,14 +1,14 @@ #pragma once -#include // Para SDL_FPoint, SDL_FRect -#include // Para SDL_Texture, SDL_Renderer -#include // Para Uint32 -#include // Para SDL_GetTicks -#include // Para size_t -#include // Para unique_ptr, shared_ptr -#include // Para string -#include // Para vector -#include "utils.h" // Para Color +#include // Para SDL_FPoint, SDL_FRect +#include // Para SDL_Texture, SDL_Renderer +#include // Para Uint32 +#include // Para SDL_GetTicks +#include // Para size_t +#include // Para unique_ptr, shared_ptr +#include // Para string +#include // Para vector +#include "utils.h" // Para Color class Sprite; class Text; diff --git a/source/screen.cpp b/source/screen.cpp index 33a3391..fe074bf 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -1,12 +1,12 @@ #include "screen.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_GetError -#include // Para SDL_SetHint, SDL_HINT_RENDER_DRIVER -#include // Para SDL_Init, SDL_INIT_VIDEO -#include // Para SDL_LogCategory, SDL_LogError, SDL_L... -#include // Para SDL_PixelFormat -#include // Para SDL_ScaleMode -#include // Para SDL_GetTicks +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_GetError +#include // Para SDL_SetHint, SDL_HINT_RENDER_DRIVER +#include // Para SDL_Init, SDL_INIT_VIDEO +#include // Para SDL_LogCategory, SDL_LogError, SDL_L... +#include // Para SDL_PixelFormat +#include // Para SDL_ScaleMode +#include // Para SDL_GetTicks #include // Para min, max #include // Para basic_ifstream, ifstream #include // Para istreambuf_iterator, operator== diff --git a/source/screen.h b/source/screen.h index dffce5c..05ff6d1 100644 --- a/source/screen.h +++ b/source/screen.h @@ -1,15 +1,15 @@ #pragma once -#include // Para SDL_FRect -#include // Para SDL_Renderer, SDL_SetRenderLogicalPresentation -#include // Para Uint32 -#include // Para SDL_Window, SDL_HideWindow, SDL_ShowWindow -#include // Para SDL_LogCategory, SDL_LogError, SDL_Log -#include // Para string -#include // Para shared_ptr -#include "options.h" // Para Options, VideoOptions, options -#include "param.h" // Para Param, ParamGame, param -#include "utils.h" // Para Color +#include // Para SDL_FRect +#include // Para SDL_Renderer, SDL_SetRenderLogicalPresentation +#include // Para Uint32 +#include // Para SDL_Window, SDL_HideWindow, SDL_ShowWindow +#include // Para SDL_LogCategory, SDL_LogError, SDL_Log +#include // Para string +#include // Para shared_ptr +#include "options.h" // Para Options, VideoOptions, options +#include "param.h" // Para Param, ParamGame, param +#include "utils.h" // Para Color #include "text.h" class Notifier; diff --git a/source/sections/credits.cpp b/source/sections/credits.cpp index a429cf4..f1b5c1d 100644 --- a/source/sections/credits.cpp +++ b/source/sections/credits.cpp @@ -1,31 +1,31 @@ // IWYU pragma: no_include #include "credits.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_PollEvent, SDL_Event -#include // Para SDL_PixelFormat -#include // Para SDL_GetTicks -#include // Para max, min, clamp -#include // Para array -#include // Para runtime_error -#include // Para basic_string, string -#include // Para vector -#include "balloon_manager.h" // Para BalloonManager -#include "fade.h" // Para Fade, FadeType, FadeMode -#include "global_events.h" // Para check -#include "global_inputs.h" // Para check, update -#include "input.h" // Para Input, INPUT_ALLOW_REPEAT -#include "audio.h" // Para JA_GetMusicState, JA_SetMusicVolume -#include "lang.h" // Para getText -#include "param.h" // Para Param, ParamGame, param -#include "player.h" // Para Player, PlayerState -#include "resource.h" // Para Resource -#include "screen.h" // Para Screen -#include "section.h" // Para Name, name -#include "sprite.h" // Para Sprite -#include "text.h" // Para Text, TEXT_CENTER, TEXT_SHADOW -#include "texture.h" // Para Texture -#include "tiled_bg.h" // Para TiledBG, TiledBGMode -#include "utils.h" // Para Color, Zone, shdw_txt_color, no_color +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_PollEvent, SDL_Event +#include // Para SDL_PixelFormat +#include // Para SDL_GetTicks +#include // Para max, min, clamp +#include // Para array +#include // Para runtime_error +#include // Para basic_string, string +#include // Para vector +#include "balloon_manager.h" // Para BalloonManager +#include "fade.h" // Para Fade, FadeType, FadeMode +#include "global_events.h" // Para check +#include "global_inputs.h" // Para check, update +#include "input.h" // Para Input, INPUT_ALLOW_REPEAT +#include "audio.h" // Para JA_GetMusicState, JA_SetMusicVolume +#include "lang.h" // Para getText +#include "param.h" // Para Param, ParamGame, param +#include "player.h" // Para Player, PlayerState +#include "resource.h" // Para Resource +#include "screen.h" // Para Screen +#include "section.h" // Para Name, name +#include "sprite.h" // Para Sprite +#include "text.h" // Para Text, TEXT_CENTER, TEXT_SHADOW +#include "texture.h" // Para Texture +#include "tiled_bg.h" // Para TiledBG, TiledBGMode +#include "utils.h" // Para Color, Zone, shdw_txt_color, no_color #include "ui/service_menu.h" #include "input.h" diff --git a/source/sections/credits.h b/source/sections/credits.h index 19e42d0..f1108ed 100644 --- a/source/sections/credits.h +++ b/source/sections/credits.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include diff --git a/source/sections/game.cpp b/source/sections/game.cpp index 6ce25ac..2083ec9 100644 --- a/source/sections/game.cpp +++ b/source/sections/game.cpp @@ -1,9 +1,9 @@ #include "game.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_EventType, SDL_Event, SDL_Poll... -#include // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4 -#include // Para SDL_PixelFormat -#include // Para SDL_GetTicks +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_EventType, SDL_Event, SDL_Poll... +#include // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4 +#include // Para SDL_PixelFormat +#include // Para SDL_GetTicks #include // Para rand, size_t #include // Para find_if, clamp, min #include // Para function diff --git a/source/sections/game.h b/source/sections/game.h index 91a20a5..2fc04ca 100644 --- a/source/sections/game.h +++ b/source/sections/game.h @@ -1,8 +1,8 @@ #pragma once -#include // Para SDL_Event -#include // Para SDL_Renderer, SDL_Texture -#include // Para Uint64, Uint8 +#include // Para SDL_Event +#include // Para SDL_Renderer, SDL_Texture +#include // Para Uint64, Uint8 #include // Para shared_ptr, unique_ptr #include // Para string #include // Para vector diff --git a/source/sections/hiscore_table.cpp b/source/sections/hiscore_table.cpp index 5a99d60..1a9b06b 100644 --- a/source/sections/hiscore_table.cpp +++ b/source/sections/hiscore_table.cpp @@ -1,9 +1,9 @@ #include "hiscore_table.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_PollEvent, SDL_Event -#include // Para SDL_PixelFormat -#include // Para SDL_SetRenderTarget, SDL_CreateTex... -#include // Para SDL_GetTicks +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_PollEvent, SDL_Event +#include // Para SDL_PixelFormat +#include // Para SDL_SetRenderTarget, SDL_CreateTex... +#include // Para SDL_GetTicks #include // Para rand, size_t #include // Para max #include // Para function diff --git a/source/sections/hiscore_table.h b/source/sections/hiscore_table.h index 78f3dcd..ed64140 100644 --- a/source/sections/hiscore_table.h +++ b/source/sections/hiscore_table.h @@ -1,11 +1,11 @@ #pragma once -#include // Para SDL_FRect -#include // Para SDL_Renderer, SDL_Texture -#include // Para Uint16, Uint32, Uint8 -#include // Para unique_ptr, shared_ptr -#include // Para string -#include // Para vector +#include // Para SDL_FRect +#include // Para SDL_Renderer, SDL_Texture +#include // Para Uint16, Uint32, Uint8 +#include // Para unique_ptr, shared_ptr +#include // Para string +#include // Para vector #include "utils.h" class Background; diff --git a/source/sections/instructions.cpp b/source/sections/instructions.cpp index 71aefe5..3eabb44 100644 --- a/source/sections/instructions.cpp +++ b/source/sections/instructions.cpp @@ -1,25 +1,25 @@ #include "instructions.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_PollEvent, SDL_Event -#include // Para SDL_PIXELFORMAT_RGBA8888 -#include // Para SDL_GetTicks -#include // Para max -#include // Para array -#include // Para move -#include // Para vector -#include "fade.h" // Para Fade, FadeMode, FadeType -#include "global_events.h" // Para check -#include "global_inputs.h" // Para check, update -#include "audio.h" // Para JA_GetMusicState, JA_Music_state -#include "lang.h" // Para getText -#include "param.h" // Para Param, param, ParamGame, ParamFade -#include "resource.h" // Para Resource -#include "screen.h" // Para Screen -#include "section.h" // Para Name, name, Options, options -#include "sprite.h" // Para Sprite -#include "text.h" // Para Text, TEXT_CENTER, TEXT_COLOR, TEXT_... -#include "tiled_bg.h" // Para TiledBG, TiledBGMode -#include "utils.h" // Para Color, shdw_txt_color, Zone, no_color +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_PollEvent, SDL_Event +#include // Para SDL_PIXELFORMAT_RGBA8888 +#include // Para SDL_GetTicks +#include // Para max +#include // Para array +#include // Para move +#include // Para vector +#include "fade.h" // Para Fade, FadeMode, FadeType +#include "global_events.h" // Para check +#include "global_inputs.h" // Para check, update +#include "audio.h" // Para JA_GetMusicState, JA_Music_state +#include "lang.h" // Para getText +#include "param.h" // Para Param, param, ParamGame, ParamFade +#include "resource.h" // Para Resource +#include "screen.h" // Para Screen +#include "section.h" // Para Name, name, Options, options +#include "sprite.h" // Para Sprite +#include "text.h" // Para Text, TEXT_CENTER, TEXT_COLOR, TEXT_... +#include "tiled_bg.h" // Para TiledBG, TiledBGMode +#include "utils.h" // Para Color, shdw_txt_color, Zone, no_color #include "input.h" // Constructor diff --git a/source/sections/instructions.h b/source/sections/instructions.h index ea70dcb..26513db 100644 --- a/source/sections/instructions.h +++ b/source/sections/instructions.h @@ -1,10 +1,10 @@ #pragma once -#include // Para SDL_FPoint, SDL_FRect -#include // Para SDL_Texture, SDL_Renderer -#include // Para Uint32 -#include // Para unique_ptr, shared_ptr -#include // Para vector +#include // Para SDL_FPoint, SDL_FRect +#include // Para SDL_Texture, SDL_Renderer +#include // Para Uint32 +#include // Para unique_ptr, shared_ptr +#include // Para vector class Fade; class Sprite; diff --git a/source/sections/intro.cpp b/source/sections/intro.cpp index 8fed741..4047e52 100644 --- a/source/sections/intro.cpp +++ b/source/sections/intro.cpp @@ -1,27 +1,27 @@ #include "intro.h" -#include // Para SDL_BLENDMODE_BLEND, SDL_BLENDMODE_MOD -#include // Para SDL_PollEvent, SDL_Event -#include // Para SDL_PIXELFORMAT_RGBA8888 -#include // Para SDL_FRect -#include // Para SDL_SetTextureBlendMode, SDL_GetRend... -#include // Para SDL_GetTicks -#include // Para array -#include // Para function -#include // Para basic_string, string -#include // Para move -#include "global_events.h" // Para check -#include "global_inputs.h" // Para check, update -#include "audio.h" // Para JA_PlayMusic, JA_StopMusic -#include "lang.h" // Para getText -#include "param.h" // Para Param, ParamGame, param -#include "path_sprite.h" // Para PathSprite, PathType -#include "resource.h" // Para Resource -#include "screen.h" // Para Screen -#include "section.h" // Para Name, name, Options, options -#include "texture.h" // Para Texture -#include "tiled_bg.h" // Para TiledBG, TiledBGMode -#include "utils.h" // Para Color, Zone, easeOutQuint, BLOCK -#include "writer.h" // Para Writer +#include // Para SDL_BLENDMODE_BLEND, SDL_BLENDMODE_MOD +#include // Para SDL_PollEvent, SDL_Event +#include // Para SDL_PIXELFORMAT_RGBA8888 +#include // Para SDL_FRect +#include // Para SDL_SetTextureBlendMode, SDL_GetRend... +#include // Para SDL_GetTicks +#include // Para array +#include // Para function +#include // Para basic_string, string +#include // Para move +#include "global_events.h" // Para check +#include "global_inputs.h" // Para check, update +#include "audio.h" // Para JA_PlayMusic, JA_StopMusic +#include "lang.h" // Para getText +#include "param.h" // Para Param, ParamGame, param +#include "path_sprite.h" // Para PathSprite, PathType +#include "resource.h" // Para Resource +#include "screen.h" // Para Screen +#include "section.h" // Para Name, name, Options, options +#include "texture.h" // Para Texture +#include "tiled_bg.h" // Para TiledBG, TiledBGMode +#include "utils.h" // Para Color, Zone, easeOutQuint, BLOCK +#include "writer.h" // Para Writer #include "input.h" #ifdef DEBUG diff --git a/source/sections/intro.h b/source/sections/intro.h index 35db874..ebc58bc 100644 --- a/source/sections/intro.h +++ b/source/sections/intro.h @@ -1,11 +1,11 @@ #pragma once -#include // Para Uint32, Uint8 -#include // Para unique_ptr -#include // Para vector -#include "path_sprite.h" // Para PathSprite -#include "tiled_bg.h" // Para TiledBG -#include "writer.h" // Para Writer +#include // Para Uint32, Uint8 +#include // Para unique_ptr +#include // Para vector +#include "path_sprite.h" // Para PathSprite +#include "tiled_bg.h" // Para TiledBG +#include "writer.h" // Para Writer #include "param.h" /* diff --git a/source/sections/logo.cpp b/source/sections/logo.cpp index f514653..23da4a1 100644 --- a/source/sections/logo.cpp +++ b/source/sections/logo.cpp @@ -1,17 +1,17 @@ #include "logo.h" -#include // Para SDL_PollEvent, SDL_Event -#include // Para SDL_GetTicks -#include // Para move -#include "global_events.h" // Para check -#include "global_inputs.h" // Para check, update -#include "audio.h" // Para JA_FadeOutMusic, JA_PlaySound, JA_StopC... -#include "param.h" // Para Param, ParamGame, param -#include "resource.h" // Para Resource -#include "screen.h" // Para Screen -#include "section.h" // Para Name, name -#include "sprite.h" // Para Sprite -#include "texture.h" // Para Texture -#include "utils.h" // Para Color, Zone +#include // Para SDL_PollEvent, SDL_Event +#include // Para SDL_GetTicks +#include // Para move +#include "global_events.h" // Para check +#include "global_inputs.h" // Para check, update +#include "audio.h" // Para JA_FadeOutMusic, JA_PlaySound, JA_StopC... +#include "param.h" // Para Param, ParamGame, param +#include "resource.h" // Para Resource +#include "screen.h" // Para Screen +#include "section.h" // Para Name, name +#include "sprite.h" // Para Sprite +#include "texture.h" // Para Texture +#include "utils.h" // Para Color, Zone #include "input.h" // Constructor diff --git a/source/sections/logo.h b/source/sections/logo.h index f599b91..2a86824 100644 --- a/source/sections/logo.h +++ b/source/sections/logo.h @@ -1,9 +1,9 @@ #pragma once -#include // Para SDL_FPoint -#include // Para Uint64 -#include // Para shared_ptr, unique_ptr -#include // Para vector +#include // Para SDL_FPoint +#include // Para Uint64 +#include // Para shared_ptr, unique_ptr +#include // Para vector class Sprite; class Texture; diff --git a/source/sections/title.cpp b/source/sections/title.cpp index 4d32934..087c327 100644 --- a/source/sections/title.cpp +++ b/source/sections/title.cpp @@ -1,30 +1,30 @@ #include "title.h" -#include // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN -#include // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5 -#include // Para SDL_GetTicks -#include // Para size_t -#include // Para char_traits, operator+, basic_string -#include // Para vector -#include "define_buttons.h" // Para DefineButtons -#include "fade.h" // Para Fade, FadeType -#include "game_logo.h" // Para GameLogo -#include "global_inputs.h" // Para check, update -#include "input.h" // Para Input, InputAction, INPUT_DO_NOT_ALLOW_R... -#include "audio.h" // Para JA_GetMusicState, JA_FadeOutMusic, JA_... -#include "lang.h" // Para getText -#include "global_events.h" // Para handleEvent -#include "notifier.h" // Para Notifier -#include "options.h" // Para OptionsController, Options, options -#include "param.h" // Para Param, param, ParamGame, ParamTitle -#include "player.h" // Para Player, PlayerState -#include "resource.h" // Para Resource -#include "screen.h" // Para Screen -#include "section.h" // Para Options, Name, name, AttractMode, options -#include "sprite.h" // Para Sprite -#include "text.h" // Para TEXT_CENTER, TEXT_SHADOW, Text -#include "texture.h" // Para Texture -#include "tiled_bg.h" // Para TiledBG, TiledBGMode -#include "utils.h" // Para Color, Zone, fade_color, no_color, BLOCK +#include // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN +#include // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5 +#include // Para SDL_GetTicks +#include // Para size_t +#include // Para char_traits, operator+, basic_string +#include // Para vector +#include "define_buttons.h" // Para DefineButtons +#include "fade.h" // Para Fade, FadeType +#include "game_logo.h" // Para GameLogo +#include "global_inputs.h" // Para check, update +#include "input.h" // Para Input, InputAction, INPUT_DO_NOT_ALLOW_R... +#include "audio.h" // Para JA_GetMusicState, JA_FadeOutMusic, JA_... +#include "lang.h" // Para getText +#include "global_events.h" // Para handleEvent +#include "notifier.h" // Para Notifier +#include "options.h" // Para OptionsController, Options, options +#include "param.h" // Para Param, param, ParamGame, ParamTitle +#include "player.h" // Para Player, PlayerState +#include "resource.h" // Para Resource +#include "screen.h" // Para Screen +#include "section.h" // Para Options, Name, name, AttractMode, options +#include "sprite.h" // Para Sprite +#include "text.h" // Para TEXT_CENTER, TEXT_SHADOW, Text +#include "texture.h" // Para Texture +#include "tiled_bg.h" // Para TiledBG, TiledBGMode +#include "utils.h" // Para Color, Zone, fade_color, no_color, BLOCK #include "ui/service_menu.h" #ifdef DEBUG diff --git a/source/sections/title.h b/source/sections/title.h index a3bcac2..a414023 100644 --- a/source/sections/title.h +++ b/source/sections/title.h @@ -1,7 +1,7 @@ #pragma once -#include // Para Uint32 -#include // Para unique_ptr, shared_ptr +#include // Para Uint32 +#include // Para unique_ptr, shared_ptr #include #include "section.h" // Para Options diff --git a/source/sprite.h b/source/sprite.h index b587fa6..472ef52 100644 --- a/source/sprite.h +++ b/source/sprite.h @@ -1,7 +1,7 @@ #pragma once -#include // Para SDL_FRect, SDL_FPoint -#include // Para shared_ptr +#include // Para SDL_FRect, SDL_FPoint +#include // Para shared_ptr class Texture; diff --git a/source/tabe.cpp b/source/tabe.cpp index 8f4331a..43e0d9d 100644 --- a/source/tabe.cpp +++ b/source/tabe.cpp @@ -1,12 +1,12 @@ // IWYU pragma: no_include #include "tabe.h" -#include // Para SDL_FlipMode -#include // Para rand, abs -#include // Para max -#include "audio.h" // Para JA_PlaySound -#include "param.h" // Para Param, ParamGame, param -#include "resource.h" // Para Resource -#include "utils.h" // Para Zone +#include // Para SDL_FlipMode +#include // Para rand, abs +#include // Para max +#include "audio.h" // Para JA_PlaySound +#include "param.h" // Para Param, ParamGame, param +#include "resource.h" // Para Resource +#include "utils.h" // Para Zone // Constructor Tabe::Tabe() diff --git a/source/tabe.h b/source/tabe.h index a317da7..8fd2a0f 100644 --- a/source/tabe.h +++ b/source/tabe.h @@ -1,8 +1,8 @@ #pragma once -#include // Para SDL_FRect -#include // Para Uint32 -#include // Para SDL_GetTicks +#include // Para SDL_FRect +#include // Para Uint32 +#include // Para SDL_GetTicks #include // Para rand #include // Para unique_ptr #include "animated_sprite.h" // Para AnimatedSprite diff --git a/source/text.cpp b/source/text.cpp index d4169c8..00b19dc 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -1,16 +1,16 @@ #include "text.h" -#include // Para SDL_BLENDMODE_BLEND -#include // Para SDL_PixelFormat -#include // Para SDL_FRect -#include // Para SDL_SetRenderTarget, SDL_GetRenderTa... -#include // Para size_t -#include // Para basic_ifstream, basic_istream, basic... -#include // Para cerr -#include // Para runtime_error -#include "screen.h" // Para Screen -#include "sprite.h" // Para Sprite -#include "texture.h" // Para Texture -#include "utils.h" // Para Color, getFileName, printWithDots +#include // Para SDL_BLENDMODE_BLEND +#include // Para SDL_PixelFormat +#include // Para SDL_FRect +#include // Para SDL_SetRenderTarget, SDL_GetRenderTa... +#include // Para size_t +#include // Para basic_ifstream, basic_istream, basic... +#include // Para cerr +#include // Para runtime_error +#include "screen.h" // Para Screen +#include "sprite.h" // Para Sprite +#include "texture.h" // Para Texture +#include "utils.h" // Para Color, getFileName, printWithDots // Llena una estructuta TextFile desde un fichero std::shared_ptr loadTextFile(const std::string &file_path) diff --git a/source/text.h b/source/text.h index 960e107..7d55aed 100644 --- a/source/text.h +++ b/source/text.h @@ -1,10 +1,10 @@ #pragma once -#include // Para Uint8 -#include // Para unique_ptr, shared_ptr -#include // Para string -#include "sprite.h" // Para Sprite -#include "utils.h" // Para Color +#include // Para Uint8 +#include // Para unique_ptr, shared_ptr +#include // Para string +#include "sprite.h" // Para Sprite +#include "utils.h" // Para Color class Texture; diff --git a/source/texture.cpp b/source/texture.cpp index 83caafc..4d52f0d 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -1,20 +1,20 @@ #define STB_IMAGE_IMPLEMENTATION #include "texture.h" -#include // Para SDL_GetError -#include // Para SDL_LogError, SDL_LogCategory, SDL_Log... -#include // Para SDL_CreateSurfaceFrom, SDL_DestroySurface -#include // Para uint32_t -#include // Para memcpy, size_t -#include // Para basic_ifstream, basic_ios, ios, operator| -#include // Para runtime_error -#include // Para operator+, char_traits, string, operat... -#include // Para basic_ifstream, basic_ostream, basic_ist... -#include // Para cerr -#include // Para basic_istringstream -#include // Para vector -#include "external/gif.h" // Para Gif -#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a... -#include "utils.h" // Para getFileName, Color, printWithDots +#include // Para SDL_GetError +#include // Para SDL_LogError, SDL_LogCategory, SDL_Log... +#include // Para SDL_CreateSurfaceFrom, SDL_DestroySurface +#include // Para uint32_t +#include // Para memcpy, size_t +#include // Para basic_ifstream, basic_ios, ios, operator| +#include // Para runtime_error +#include // Para operator+, char_traits, string, operat... +#include // Para basic_ifstream, basic_ostream, basic_ist... +#include // Para cerr +#include // Para basic_istringstream +#include // Para vector +#include "external/gif.h" // Para Gif +#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a... +#include "utils.h" // Para getFileName, Color, printWithDots // Constructor Texture::Texture(SDL_Renderer *renderer, const std::string &path) diff --git a/source/texture.h b/source/texture.h index 6a2cc4a..5fd77c7 100644 --- a/source/texture.h +++ b/source/texture.h @@ -1,14 +1,14 @@ #pragma once -#include // Para SDL_BlendMode -#include // Para SDL_PixelFormat -#include // Para SDL_FPoint, SDL_FRect -#include // Para SDL_Renderer, SDL_TextureAccess, SDL_Texture -#include // Para Uint8, Uint16, Uint32 -#include // Para SDL_FlipMode -#include // Para shared_ptr -#include // Para string -#include // Para vector +#include // Para SDL_BlendMode +#include // Para SDL_PixelFormat +#include // Para SDL_FPoint, SDL_FRect +#include // Para SDL_Renderer, SDL_TextureAccess, SDL_Texture +#include // Para Uint8, Uint16, Uint32 +#include // Para SDL_FlipMode +#include // Para shared_ptr +#include // Para string +#include // Para vector struct Color; diff --git a/source/tiled_bg.cpp b/source/tiled_bg.cpp index 2d97d3b..27b9387 100644 --- a/source/tiled_bg.cpp +++ b/source/tiled_bg.cpp @@ -1,11 +1,11 @@ #include "tiled_bg.h" -#include // Para SDL_PIXELFORMAT_RGBA8888 -#include // Para rand -#include // Para sin -#include // Para unique_ptr, make_unique -#include "resource.h" // Para Resource -#include "screen.h" // Para Screen -#include "sprite.h" // Para Sprite +#include // Para SDL_PIXELFORMAT_RGBA8888 +#include // Para rand +#include // Para sin +#include // Para unique_ptr, make_unique +#include "resource.h" // Para Resource +#include "screen.h" // Para Screen +#include "sprite.h" // Para Sprite // Constructor TiledBG::TiledBG(SDL_FRect pos, TiledBGMode mode) diff --git a/source/tiled_bg.h b/source/tiled_bg.h index 3a7b629..fcd6f12 100644 --- a/source/tiled_bg.h +++ b/source/tiled_bg.h @@ -1,7 +1,7 @@ #pragma once -#include // Para SDL_FRect -#include // Para SDL_Renderer, SDL_Texture +#include // Para SDL_FRect +#include // Para SDL_Renderer, SDL_Texture #include "utils.h" // Modos de funcionamiento para el tileado de fondo diff --git a/source/ui/menu_option.h b/source/ui/menu_option.h index f5fb919..1cddc9b 100644 --- a/source/ui/menu_option.h +++ b/source/ui/menu_option.h @@ -4,12 +4,12 @@ #include #include #include -#include // para std::clamp +#include // para std::clamp #include "ui/service_menu.h" // Necesitamos las enums como SettingsGroup -#include "options.h" // Para acceder a las variables de configuración -#include "lang.h" // Para las traducciones -#include "section.h" // Para las acciones como Quit o Reset -#include "text.h" // Para poder calcular el ancho del texto +#include "options.h" // Para acceder a las variables de configuración +#include "lang.h" // Para las traducciones +#include "section.h" // Para las acciones como Quit o Reset +#include "text.h" // Para poder calcular el ancho del texto // --- Interfaz Base para todas las Opciones del Menú --- @@ -159,6 +159,7 @@ public: } return max_w; } + private: std::vector value_list_; std::function getter_; diff --git a/source/ui/menu_renderer.h b/source/ui/menu_renderer.h index 445edc6..edced69 100644 --- a/source/ui/menu_renderer.h +++ b/source/ui/menu_renderer.h @@ -7,7 +7,6 @@ #include "ui/service_menu.h" // Necesario para las enums y para acceder al estado del menú #include "utils.h" // Para Color - // Forward declarations class Text; class MenuOption; diff --git a/source/utils.cpp b/source/utils.cpp index 33e8eb5..2dcddd8 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -1,16 +1,16 @@ #define _USE_MATH_DEFINES #include "utils.h" -#include // Para SDL_CloseIO, SDL_IOFromFile, SDL_ReadIO -#include // Para SDL_LogCategory, SDL_LogError, SDL_Lo... -#include // Para size_t -#include // Para min, clamp, find_if_not, find, transform -#include // Para tolower, isspace -#include // Para pow, sin, M_PI, cos -#include // Para operator< -#include // Para path -#include // Para runtime_error -#include // Para basic_string, string, operator==, ope... -#include "lang.h" // Para getText +#include // Para SDL_CloseIO, SDL_IOFromFile, SDL_ReadIO +#include // Para SDL_LogCategory, SDL_LogError, SDL_Lo... +#include // Para size_t +#include // Para min, clamp, find_if_not, find, transform +#include // Para tolower, isspace +#include // Para pow, sin, M_PI, cos +#include // Para operator< +#include // Para path +#include // Para runtime_error +#include // Para basic_string, string, operator==, ope... +#include "lang.h" // Para getText // Variables Overrides overrides = Overrides(); @@ -481,7 +481,7 @@ ColorCycle generateMirroredCycle(Color base, ColorCycleStyle style) for (size_t i = 0; i < COLOR_CYCLE_SIZE; ++i) { - float t = static_cast(i) / (COLOR_CYCLE_SIZE - 1); // 0 → 1 + float t = static_cast(i) / (COLOR_CYCLE_SIZE - 1); // 0 → 1 float hueShift = 0.0f; float satShift = 0.0f; float valShift = 0.0f; @@ -520,8 +520,7 @@ ColorCycle generateMirroredCycle(Color base, ColorCycleStyle style) HSV adjusted = { fmodf(baseHSV.h + hueShift + 360.0f, 360.0f), fminf(1.0f, fmaxf(0.0f, baseHSV.s + satShift)), - fminf(1.0f, fmaxf(0.0f, baseHSV.v + valShift)) - }; + fminf(1.0f, fmaxf(0.0f, baseHSV.v + valShift))}; Color c = hsvToRgb(adjusted); result[i] = c; diff --git a/source/utils.h b/source/utils.h index c5e1a06..70b226f 100644 --- a/source/utils.h +++ b/source/utils.h @@ -1,13 +1,13 @@ #pragma once -#include // Para SDL_FRect, SDL_FPoint -#include // Para SDL_Renderer -#include // Para Uint8 -#include // Para int32_t -#include // Para max, min -#include // Para string -#include // Para vector -#include // Para array +#include // Para SDL_FRect, SDL_FPoint +#include // Para SDL_Renderer +#include // Para Uint8 +#include // Para int32_t +#include // Para max, min +#include // Para string +#include // Para vector +#include // Para array #include // --- Constantes ---