pax_global_header00006660000000000000000000000064114705741030014514gustar00rootroot0000000000000052 comment=06f8139305291bfd1dbf85edde8cb7ac34be9e0e dokidoki-support/000077500000000000000000000000001147057410300143675ustar00rootroot00000000000000dokidoki-support/LICENSE000066400000000000000000000040261147057410300153760ustar00rootroot00000000000000lua copyright (C) 1994-2008 Lua.org, PUC-Rio memarray module copyright (C) 2005-2006 Varol Kaptan gl, glu, lua_stb_image, mixer modules copyright (C) 2009 Henk Boom Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---- luaglfw copyright (c) 2002-2005 Camilla Berglund This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. dokidoki-support/Makefile000066400000000000000000000025641147057410300160360ustar00rootroot00000000000000NAME=dokidoki-support #### General Settings TARGET=$(NAME) CFLAGS=-Wall -O2 -DMEMARRAY_USE_OPENGL \ $(PLATFORM_CFLAGS) $(EXTRA_CFLAGS) LDFLAGS=$(STATIC_LINK) -llua -lglfw \ $(DYNAMIC_LINK) $(PLATFORM_LDFLAGS) $(EXTRA_LDFLAGS) CC=gcc #### Platform Settings linux: make $(TARGET) PLATFORM=linux \ STATIC_LINK="-Wl,-Bstatic" \ DYNAMIC_LINK="-Wl,-Bdynamic" \ PLATFORM_CFLAGS="-DDOKIDOKI_LINUX -pthread" \ PLATFORM_LDFLAGS="-Wl,-E -pthread -lpthread -lGL -lGLU -lX11 -lXrandr -lm -lasound" macosx: make $(TARGET) PLATFORM=macosx \ STATIC_LINK="" \ DYNAMIC_LINK="" \ PLATFORM_CFLAGS="-DDOKIDOKI_MACOSX -I/System/Library/Frameworks/CoreFoundation.framework/Headers -I/opt/local/include" \ PLATFORM_LDFLAGS="-lportaudio -L/opt/local/lib -framework AGL -framework OpenGL -framework Carbon" mingw: make $(TARGET) PLATFORM=mingw \ STATIC_LINK="-Wl,-Bstatic" \ DYNAMIC_LINK="-Wl,-Bdynamic" \ PLATFORM_CFLAGS="-DDOKIDOKI_MINGW" \ PLATFORM_LDFLAGS="-Wl,-Bstatic -lportaudio -Wl,-Bdynamic -lopengl32 -lglu32 -lole32 -lwinmm" clean: rm -f *.o $(NAME) #### Actual Building OBJS= collision.o \ gl.o \ glu.o \ log.o \ lua_stb_image.o \ luaglfw.o \ memarray.o \ minlua.o \ mixer.o \ stb_vorbis.o \ $(EXTRA_OBJECTS) $(TARGET): $(OBJS) $(CC) -o $@ $^ $(LDFLAGS) log.o: log.h lua_stb_image.o: stb_image.c minlua.o: log.h mixer.o: stb_vorbis.h dokidoki-support/collision.c000066400000000000000000000145031147057410300165310ustar00rootroot00000000000000#include #include #include #include #define POLY_MT "collision.polygon" typedef struct { lua_Number x; lua_Number y; } vector_s; typedef struct { size_t vertex_count; lua_Number bounding_radius; vector_s vertices[1]; } polygon_s; typedef struct { vector_s pos; vector_s facing; polygon_s *poly; } body_s; static vector_s make_vector(lua_Number x, lua_Number y) { vector_s v; v.x = x; v.y = y; return v; } static lua_Number vector_magnitude_squared(vector_s v) { return v.x * v.x + v.y * v.y; } static lua_Number vector_magnitude(vector_s v) { return sqrt(v.x*v.x + v.y * v.y); } static vector_s vector_neg(vector_s v) { return make_vector(-v.x, -v.y); } static vector_s vector_mul(vector_s v, lua_Number s) { return make_vector(v.x * s, v.y * s); } static lua_Number vector_dot(vector_s v1, vector_s v2) { return v1.x * v2.x + v1.y * v2.y; } static vector_s vector_sub(vector_s v1, vector_s v2) { return make_vector(v1.x - v2.x, v1.y - v2.y); } static vector_s vector_rotate_to(vector_s v, vector_s facing) { return make_vector(v.x * facing.x + v.y * -facing.y, v.x * facing.y + v.y * facing.x); } static vector_s vector_rotate_from(vector_s v, vector_s facing) { return make_vector(v.x * facing.x + v.y * facing.y, v.x * -facing.y + v.y * facing.x); } static int collision_native__make_polygon(lua_State *L) { int vertex_count = lua_objlen(L, 1) / 2; polygon_s *poly = (polygon_s *)lua_newuserdata( L, sizeof(polygon_s) + (vertex_count - 1) * sizeof(vector_s)); luaL_getmetatable(L, POLY_MT); lua_setmetatable(L, -2); poly->vertex_count = vertex_count; poly->bounding_radius = 0; int i; for(i = 0; i < vertex_count; i++) { lua_rawgeti(L, 1, i*2+1); poly->vertices[i].x = lua_tonumber(L, -1); lua_rawgeti(L, 1, i*2+2); poly->vertices[i].y = lua_tonumber(L, -1); lua_pop(L, 2); lua_Number mag = vector_magnitude(poly->vertices[i]); if(poly->bounding_radius < mag) poly->bounding_radius = mag; } return 1; } static lua_Number halfwidth_along_axis(vector_s axis, const polygon_s *poly) { lua_Number hw = 0; int i; for(i = 0; i < poly->vertex_count; i++) { lua_Number new_hw = vector_dot(poly->vertices[i], axis); if(new_hw > hw) hw = new_hw; } return hw; } static int separate_by_axis(vector_s axis, lua_Number hw1, const body_s *body1, const body_s *body2, vector_s *out) { //printf("axiss = %lf, %lf\n", axis.x, axis.y); lua_Number overlap = vector_dot(body1->pos, axis) + hw1 + halfwidth_along_axis( vector_neg(vector_rotate_from(axis, body2->facing)), body2->poly) - vector_dot(body2->pos, axis); //printf("overlap = %lf - %lf + %lf - %lf = %lf\n", // vector_dot(body1->pos, axis), hw1, // halfwidth_along_axis( // vector_neg(vector_rotate_from(axis, body2->facing)), // body2->poly), // vector_dot(body2->pos, axis), overlap); if(overlap <= 0) return 0; vector_s correction = vector_mul(axis, overlap/vector_dot(axis, axis)); if(correction.x == 0 && correction.y == 0) return 0; //printf("correction = %lf, %lf\n", correction.x, correction.y); if(vector_dot(correction, correction) < vector_dot(*out, *out)) *out = correction; return 1; } static int separate_by_axes(const body_s *body1, const body_s *body2, vector_s *out) { polygon_s *poly1 = body1->poly; int i, j; for(i = 0, j = poly1->vertex_count-1; i < poly1->vertex_count; j = i, i++) { vector_s axis = vector_rotate_to(vector_sub(poly1->vertices[i], poly1->vertices[j]), make_vector(0, -1)); //printf("axis = %lf, %lf\n", axis.x, axis.y); lua_Number hw1 = vector_dot(poly1->vertices[i], axis); //printf("body1->facing = %lf, %lf\n", body1->facing.x, body1->facing.y); axis = vector_rotate_to(axis, body1->facing); if(!separate_by_axis(axis, hw1, body1, body2, out)) return 0; } return 1; } static int collision_native__collide(lua_State *L) { //printf("#### starting\n"); body_s body1, body2; body1.pos.x = luaL_checknumber(L, 1); body1.pos.y = luaL_checknumber(L, 2); body1.facing.x = luaL_checknumber(L, 3); body1.facing.y = luaL_checknumber(L, 4); //printf("1body1->facing = %lf, %lf\n", body1.facing.x, body1.facing.y); body1.poly = luaL_checkudata(L, 5, POLY_MT); body2.pos.x = luaL_checknumber(L, 6); body2.pos.y = luaL_checknumber(L, 7); body2.facing.x = luaL_checknumber(L, 8); body2.facing.y = luaL_checknumber(L, 9); body2.poly = luaL_checkudata(L, 10, POLY_MT); //printf("pos1 = %lf, %lf\n", body1.pos.x, body1.pos.y); //printf("pos2 = %lf, %lf\n", body2.pos.x, body2.pos.y); lua_Number bounding_distance = body1.poly->bounding_radius + body2.poly->bounding_radius; lua_Number distance_squared = vector_magnitude_squared(vector_sub(body2.pos, body1.pos)); if(bounding_distance * bounding_distance < distance_squared) { //printf("broad phase fail\n"); lua_pushboolean(L, 0); return 1; } else { vector_s correction = make_vector(1.0/0.0, 1.0/0.0); if(!separate_by_axes(&body1, &body2, &correction) || !separate_by_axes(&body2, &body1, &correction)) { //printf("fine phase fail\n"); lua_pushboolean(L, 0); return 1; } if(vector_dot(correction, vector_sub(body2.pos, body1.pos)) > 0) { correction.x = -correction.x; correction.y = -correction.y; } //printf("positive collision %lf, %lf\n", correction.x, correction.y); lua_pushboolean(L, 1); lua_pushnumber(L, correction.x); lua_pushnumber(L, correction.y); return 3; } } static const luaL_Reg collision_native_lib[] = { {"make_polygon", collision_native__make_polygon}, {"collide", collision_native__collide}, {NULL, NULL} }; int luaopen_collision_native(lua_State *L) { luaL_newmetatable(L, POLY_MT); lua_newtable(L); luaL_register(L, NULL, collision_native_lib); return 1; } dokidoki-support/gl.c000066400000000000000000002253371147057410300151510ustar00rootroot00000000000000#include "lua.h" #include "lualib.h" #include "lauxlib.h" #ifdef __APPLE__ #include "OpenGL/gl.h" #include "OpenGL/glu.h" #else #include "GL/gl.h" #include "GL/glu.h" #endif static void * checkpointer(lua_State *L, int num) { void *arg; if(lua_islightuserdata(L, num)) arg = lua_touserdata(L, num); else if(lua_isstring(L, num)) arg = (void *)lua_tostring(L, num); else luaL_argerror(L, num, "expected lightuserdata or string"); return arg; } struct constant_s {const char *name; int value;}; static struct constant_s constants [] = { {"GL_LUMINANCE12_ALPHA12", 0x8047}, {"GL_COMPILE_AND_EXECUTE", 0x1301}, {"GL_RGB16", 0x8054}, {"GL_CLAMP", 0x2900}, {"GL_MODULATE", 0x2100}, {"GL_ALPHA12", 0x803d}, {"GL_UNSIGNED_SHORT", 0x1403}, {"GL_STENCIL_FUNC", 0xb92}, {"GL_COLOR_INDEX", 0x1900}, {"GL_TEXTURE_ENV_COLOR", 0x2201}, {"GL_DECAL", 0x2101}, {"GL_VERSION", 0x1f02}, {"GL_BLEND", 0xbe2}, {"GL_SCISSOR_BIT", 0x80000}, {"GL_CLIENT_VERTEX_ARRAY_BIT", 0x2}, {"GL_UNPACK_SKIP_ROWS", 0xcf3}, {"GL_EYE_PLANE", 0x2502}, {"GL_RGBA8", 0x8058}, {"GL_OBJECT_PLANE", 0x2501}, {"GL_LIGHTING", 0xb50}, {"GL_MAP1_TEXTURE_COORD_4", 0xd96}, {"GL_LIST_MODE", 0xb30}, {"GL_DOUBLEBUFFER", 0xc32}, {"GL_LUMINANCE12_ALPHA4", 0x8046}, {"GL_ACCUM_BLUE_BITS", 0xd5a}, {"GL_ACCUM", 0x100}, {"GL_CLEAR", 0x1500}, {"GL_RIGHT", 0x407}, {"GL_3D_COLOR_TEXTURE", 0x603}, {"GL_NEAREST", 0x2600}, {"GL_FALSE", 0x0}, {"GL_CURRENT_RASTER_COLOR", 0xb04}, {"GL_COPY_INVERTED", 0x150c}, {"GL_TEXTURE_WRAP_S", 0x2802}, {"GL_2D", 0x600}, {"GL_HINT_BIT", 0x8000}, {"GL_ZOOM_X", 0xd16}, {"GL_T4F_V4F", 0x2a28}, {"GL_VERTEX_ARRAY_STRIDE", 0x807c}, {"GL_ALWAYS", 0x207}, {"GL_MODELVIEW", 0x1700}, {"GL_LIGHT6", 0x4006}, {"GL_LOAD", 0x101}, {"GL_INDEX_LOGIC_OP", 0xbf1}, {"GL_NAND", 0x150e}, {"GL_STENCIL_FAIL", 0xb94}, {"GL_ONE_MINUS_SRC_COLOR", 0x301}, {"GL_POLYGON_OFFSET_LINE", 0x2a02}, {"GL_KEEP", 0x1e00}, {"GL_PIXEL_MAP_I_TO_I_SIZE", 0xcb0}, {"GL_PROXY_TEXTURE_1D", 0x8063}, {"GL_PIXEL_MAP_I_TO_A", 0xc75}, {"GL_AND_INVERTED", 0x1504}, {"GL_NO_ERROR", 0x0}, {"GL_AND", 0x1501}, {"GL_TEXTURE_ENV_MODE", 0x2200}, {"GL_AND_REVERSE", 0x1502}, {"GL_ZERO", 0x0}, {"GL_FOG_COLOR", 0xb66}, {"GL_DOUBLE", 0x140a}, {"GL_LESS", 0x201}, {"GL_COLOR_CLEAR_VALUE", 0xc22}, {"GL_TEXTURE_MAG_FILTER", 0x2800}, {"GL_MAP_COLOR", 0xd10}, {"GL_C4UB_V2F", 0x2a22}, {"GL_VIEWPORT_BIT", 0x800}, {"GL_ALL_CLIENT_ATTRIB_BITS", 0xffffffff}, {"GL_RGBA12", 0x805a}, {"GL_POINT", 0x1b00}, {"GL_PACK_ROW_LENGTH", 0xd02}, {"GL_INTENSITY4", 0x804a}, {"GL_FOG_START", 0xb63}, {"GL_SMOOTH", 0x1d01}, {"GL_POLYGON_MODE", 0xb40}, {"GL_LINE_STIPPLE_PATTERN", 0xb25}, {"GL_ONE_MINUS_DST_ALPHA", 0x305}, {"GL_MAP1_TEXTURE_COORD_1", 0xd93}, {"GL_CURRENT_RASTER_TEXTURE_COORDS", 0xb06}, {"GL_NEAREST_MIPMAP_LINEAR", 0x2702}, {"GL_UNPACK_SKIP_PIXELS", 0xcf4}, {"GL_T2F_N3F_V3F", 0x2a2b}, {"GL_PACK_ALIGNMENT", 0xd05}, {"GL_C3F_V3F", 0x2a24}, {"GL_LEQUAL", 0x203}, {"GL_EXTENSIONS", 0x1f03}, {"GL_CURRENT_RASTER_INDEX", 0xb05}, {"GL_COLOR_MATERIAL_PARAMETER", 0xb56}, {"GL_ALPHA_BIAS", 0xd1d}, {"GL_LINE_WIDTH_RANGE", 0xb22}, {"GL_MAP1_INDEX", 0xd91}, {"GL_FOG_INDEX", 0xb61}, {"GL_FEEDBACK_BUFFER_SIZE", 0xdf1}, {"GL_CURRENT_COLOR", 0xb00}, {"GL_GREEN", 0x1904}, {"GL_POINT_SIZE_GRANULARITY", 0xb13}, {"GL_FEEDBACK_BUFFER_TYPE", 0xdf2}, {"GL_COLOR_MATERIAL", 0xb57}, {"GL_CLIP_PLANE1", 0x3001}, {"GL_SPOT_EXPONENT", 0x1205}, {"GL_LINEAR_MIPMAP_LINEAR", 0x2703}, {"GL_COLOR", 0x1800}, {"GL_LUMINANCE16_ALPHA16", 0x8048}, {"GL_TRIANGLE_STRIP", 0x5}, {"GL_MAX_PROJECTION_STACK_DEPTH", 0xd38}, {"GL_TEXTURE_MIN_FILTER", 0x2801}, {"GL_GREATER", 0x204}, {"GL_OR_INVERTED", 0x150d}, {"GL_INDEX_SHIFT", 0xd12}, {"GL_FOG", 0xb60}, {"GL_POINTS", 0x0}, {"GL_FRONT_LEFT", 0x400}, {"GL_DEPTH_WRITEMASK", 0xb72}, {"GL_PIXEL_MAP_I_TO_A_SIZE", 0xcb5}, {"GL_LUMINANCE8", 0x8040}, {"GL_ALPHA_BITS", 0xd55}, {"GL_PROJECTION", 0x1701}, {"GL_STENCIL_TEST", 0xb90}, {"GL_INDEX_CLEAR_VALUE", 0xc20}, {"GL_POINT_TOKEN", 0x701}, {"GL_PROJECTION_STACK_DEPTH", 0xba4}, {"GL_TEXTURE_BLUE_SIZE", 0x805e}, {"GL_VERSION_1_1", 0x1}, {"GL_STENCIL_PASS_DEPTH_PASS", 0xb96}, {"GL_DRAW_BUFFER", 0xc01}, {"GL_SHORT", 0x1402}, {"GL_LIGHT7", 0x4007}, {"GL_FLOAT", 0x1406}, {"GL_LUMINANCE8_ALPHA8", 0x8045}, {"GL_SCISSOR_BOX", 0xc10}, {"GL_INTENSITY12", 0x804c}, {"GL_TEXTURE_GREEN_SIZE", 0x805d}, {"GL_FOG_DENSITY", 0xb62}, {"GL_LINEAR", 0x2601}, {"GL_ONE_MINUS_DST_COLOR", 0x307}, {"GL_LEFT", 0x406}, {"GL_MAP_STENCIL", 0xd11}, {"GL_STENCIL_VALUE_MASK", 0xb93}, {"GL_PIXEL_MAP_R_TO_R_SIZE", 0xcb6}, {"GL_FOG_HINT", 0xc54}, {"GL_NONE", 0x0}, {"GL_NOR", 0x1508}, {"GL_LINE_SMOOTH", 0xb20}, {"GL_INDEX_BITS", 0xd51}, {"GL_LIGHT_MODEL_TWO_SIDE", 0xb52}, {"GL_PERSPECTIVE_CORRECTION_HINT", 0xc50}, {"GL_NORMAL_ARRAY_POINTER", 0x808f}, {"GL_TEXTURE_COORD_ARRAY_TYPE", 0x8089}, {"GL_POLYGON_SMOOTH", 0xb41}, {"GL_CLIP_PLANE5", 0x3005}, {"GL_NORMAL_ARRAY", 0x8075}, {"GL_POLYGON_OFFSET_FACTOR", 0x8038}, {"GL_RGB10_A2", 0x8059}, {"GL_SHININESS", 0x1601}, {"GL_ENABLE_BIT", 0x2000}, {"GL_POSITION", 0x1203}, {"GL_MULT", 0x103}, {"GL_DEPTH_TEST", 0xb71}, {"GL_SPOT_DIRECTION", 0x1204}, {"GL_RGBA_MODE", 0xc31}, {"GL_STENCIL_BITS", 0xd57}, {"GL_CLIP_PLANE3", 0x3003}, {"GL_LIST_BASE", 0xb32}, {"GL_PIXEL_MAP_I_TO_R", 0xc72}, {"GL_CCW", 0x901}, {"GL_MAX_MODELVIEW_STACK_DEPTH", 0xd36}, {"GL_T2F_C4F_N3F_V3F", 0x2a2c}, {"GL_OR_REVERSE", 0x150b}, {"GL_BACK_LEFT", 0x402}, {"GL_MAP1_COLOR_4", 0xd90}, {"GL_DST_ALPHA", 0x304}, {"GL_FRONT", 0x404}, {"GL_STENCIL_PASS_DEPTH_FAIL", 0xb95}, {"GL_BLUE_BIAS", 0xd1b}, {"GL_Q", 0x2003}, {"GL_STENCIL", 0x1802}, {"GL_CURRENT_RASTER_DISTANCE", 0xb09}, {"GL_MAX_EVAL_ORDER", 0xd30}, {"GL_MAP1_TEXTURE_COORD_3", 0xd95}, {"GL_MAP1_TEXTURE_COORD_2", 0xd94}, {"GL_V3F", 0x2a21}, {"GL_SRC_ALPHA", 0x302}, {"GL_PIXEL_MAP_I_TO_B", 0xc74}, {"GL_PIXEL_MAP_I_TO_G_SIZE", 0xcb3}, {"GL_POLYGON_OFFSET_UNITS", 0x2a00}, {"GL_UNSIGNED_INT", 0x1405}, {"GL_CURRENT_RASTER_POSITION_VALID", 0xb08}, {"GL_LIST_BIT", 0x20000}, {"GL_SELECTION_BUFFER_POINTER", 0xdf3}, {"GL_TEXTURE_GEN_Q", 0xc63}, {"GL_VIEWPORT", 0xba2}, {"GL_GREEN_SCALE", 0xd18}, {"GL_GREEN_BITS", 0xd53}, {"GL_INDEX_WRITEMASK", 0xc21}, {"GL_T2F_V3F", 0x2a27}, {"GL_RED_SCALE", 0xd14}, {"GL_POINT_SIZE_RANGE", 0xb12}, {"GL_SET", 0x150f}, {"GL_CLIENT_ALL_ATTRIB_BITS", 0xffffffff}, {"GL_ADD", 0x104}, {"GL_FASTEST", 0x1101}, {"GL_EDGE_FLAG_ARRAY_STRIDE", 0x808c}, {"GL_AMBIENT", 0x1200}, {"GL_C4UB_V3F", 0x2a23}, {"GL_EQUAL", 0x202}, {"GL_T4F_C4F_N3F_V4F", 0x2a2d}, {"GL_ONE_MINUS_SRC_ALPHA", 0x303}, {"GL_DITHER", 0xbd0}, {"GL_SPOT_CUTOFF", 0x1206}, {"GL_TEXTURE_COORD_ARRAY_STRIDE", 0x808a}, {"GL_TEXTURE_WIDTH", 0x1000}, {"GL_PIXEL_MAP_I_TO_R_SIZE", 0xcb2}, {"GL_MODELVIEW_MATRIX", 0xba6}, {"GL_LUMINANCE12", 0x8041}, {"GL_T2F_C4UB_V3F", 0x2a29}, {"GL_T", 0x2001}, {"GL_STACK_UNDERFLOW", 0x504}, {"GL_LINES", 0x1}, {"GL_ALPHA4", 0x803b}, {"GL_TEXTURE_MATRIX", 0xba8}, {"GL_CLIENT_PIXEL_STORE_BIT", 0x1}, {"GL_FRONT_RIGHT", 0x401}, {"GL_COMPILE", 0x1300}, {"GL_INDEX_ARRAY_STRIDE", 0x8086}, {"GL_COLOR_ARRAY_SIZE", 0x8081}, {"GL_MAX_NAME_STACK_DEPTH", 0xd37}, {"GL_AMBIENT_AND_DIFFUSE", 0x1602}, {"GL_MAX_TEXTURE_STACK_DEPTH", 0xd39}, {"GL_PIXEL_MAP_S_TO_S", 0xc71}, {"GL_COEFF", 0xa00}, {"GL_PACK_SWAP_BYTES", 0xd00}, {"GL_CURRENT_NORMAL", 0xb02}, {"GL_NEAREST_MIPMAP_NEAREST", 0x2700}, {"GL_TEXTURE_BORDER_COLOR", 0x1004}, {"GL_FLAT", 0x1d00}, {"GL_TEXTURE_BINDING_2D", 0x8069}, {"GL_LINE_LOOP", 0x2}, {"GL_QUAD_STRIP", 0x8}, {"GL_PROXY_TEXTURE_2D", 0x8064}, {"GL_FEEDBACK_BUFFER_POINTER", 0xdf0}, {"GL_ALL_ATTRIB_BITS", 0xfffff}, {"GL_NAME_STACK_DEPTH", 0xd70}, {"GL_VERTEX_ARRAY", 0x8074}, {"GL_DIFFUSE", 0x1201}, {"GL_PIXEL_MAP_R_TO_R", 0xc76}, {"GL_CLIP_PLANE0", 0x3000}, {"GL_MAX_VIEWPORT_DIMS", 0xd3a}, {"GL_N3F_V3F", 0x2a25}, {"GL_CLIP_PLANE2", 0x3002}, {"GL_EDGE_FLAG_ARRAY_POINTER", 0x8093}, {"GL_TEXTURE_GEN_MODE", 0x2500}, {"GL_ATTRIB_STACK_DEPTH", 0xbb0}, {"GL_MAP2_TEXTURE_COORD_2", 0xdb4}, {"GL_STENCIL_REF", 0xb97}, {"GL_BLEND_DST", 0xbe0}, {"GL_REPLACE", 0x1e01}, {"GL_REPEAT", 0x2901}, {"GL_V2F", 0x2a20}, {"GL_BLUE_SCALE", 0xd1a}, {"GL_MAP1_VERTEX_4", 0xd98}, {"GL_PIXEL_MAP_I_TO_G", 0xc73}, {"GL_TEXTURE_LUMINANCE_SIZE", 0x8060}, {"GL_PIXEL_MAP_G_TO_G", 0xc77}, {"GL_CW", 0x900}, {"GL_ALPHA8", 0x803c}, {"GL_TEXTURE_RESIDENT", 0x8067}, {"GL_COPY", 0x1503}, {"GL_DEPTH_CLEAR_VALUE", 0xb73}, {"GL_SPECULAR", 0x1202}, {"GL_VERTEX_ARRAY_POINTER", 0x808e}, {"GL_TEXTURE_GEN_S", 0xc60}, {"GL_POLYGON_STIPPLE", 0xb42}, {"GL_STENCIL_CLEAR_VALUE", 0xb91}, {"GL_3D_COLOR", 0x602}, {"GL_MAP2_GRID_DOMAIN", 0xdd2}, {"GL_FILL", 0x1b02}, {"GL_INDEX_ARRAY_TYPE", 0x8085}, {"GL_3D", 0x601}, {"GL_CULL_FACE_MODE", 0xb45}, {"GL_BYTE", 0x1400}, {"GL_OR", 0x1507}, {"GL_ACCUM_GREEN_BITS", 0xd59}, {"GL_PROJECTION_MATRIX", 0xba7}, {"GL_SRC_ALPHA_SATURATE", 0x308}, {"GL_EXP", 0x800}, {"GL_LINE_SMOOTH_HINT", 0xc52}, {"GL_POINT_BIT", 0x2}, {"GL_TRUE", 0x1}, {"GL_COPY_PIXEL_TOKEN", 0x706}, {"GL_4_BYTES", 0x1409}, {"GL_UNPACK_LSB_FIRST", 0xcf1}, {"GL_DEPTH", 0x1801}, {"GL_DST_COLOR", 0x306}, {"GL_RGBA", 0x1908}, {"GL_MAP2_VERTEX_4", 0xdb8}, {"GL_EMISSION", 0x1600}, {"GL_COLOR_ARRAY_POINTER", 0x8090}, {"GL_DEPTH_SCALE", 0xd1e}, {"GL_MAX_LIGHTS", 0xd31}, {"GL_OBJECT_LINEAR", 0x2401}, {"GL_ALPHA_SCALE", 0xd1c}, {"GL_INDEX_MODE", 0xc30}, {"GL_NOTEQUAL", 0x205}, {"GL_CURRENT_RASTER_POSITION", 0xb07}, {"GL_LIGHT4", 0x4004}, {"GL_MAP2_TEXTURE_COORD_1", 0xdb3}, {"GL_INDEX_ARRAY_POINTER", 0x8091}, {"GL_LIGHT0", 0x4000}, {"GL_COLOR_ARRAY_STRIDE", 0x8083}, {"GL_RGB10", 0x8052}, {"GL_MAP1_VERTEX_3", 0xd97}, {"GL_LINE_TOKEN", 0x702}, {"GL_UNPACK_ALIGNMENT", 0xcf5}, {"GL_RED_BITS", 0xd52}, {"GL_TEXTURE_HEIGHT", 0x1001}, {"GL_ACCUM_BUFFER_BIT", 0x200}, {"GL_MAP1_GRID_DOMAIN", 0xdd0}, {"GL_ALPHA16", 0x803e}, {"GL_POINT_SMOOTH_HINT", 0xc51}, {"GL_VENDOR", 0x1f00}, {"GL_MAX_PIXEL_MAP_TABLE", 0xd34}, {"GL_ACCUM_RED_BITS", 0xd58}, {"GL_CLIENT_ATTRIB_STACK_DEPTH", 0xbb1}, {"GL_RGBA16", 0x805b}, {"GL_RGB5_A1", 0x8057}, {"GL_LIGHT3", 0x4003}, {"GL_LIGHT1", 0x4001}, {"GL_RGBA4", 0x8056}, {"GL_DEPTH_BUFFER_BIT", 0x100}, {"GL_RGBA2", 0x8055}, {"GL_RGB", 0x1907}, {"GL_PIXEL_MAP_S_TO_S_SIZE", 0xcb1}, {"GL_PIXEL_MAP_B_TO_B", 0xc78}, {"GL_RGB8", 0x8051}, {"GL_RGB5", 0x8050}, {"GL_PACK_SKIP_PIXELS", 0xd04}, {"GL_BITMAP_TOKEN", 0x704}, {"GL_DEPTH_BIAS", 0xd1f}, {"GL_CULL_FACE", 0xb44}, {"GL_MODELVIEW_STACK_DEPTH", 0xba3}, {"GL_INTENSITY16", 0x804d}, {"GL_ALPHA_TEST", 0xbc0}, {"GL_LINE_WIDTH", 0xb21}, {"GL_INTENSITY", 0x8049}, {"GL_PIXEL_MAP_A_TO_A", 0xc79}, {"GL_BLUE", 0x1905}, {"GL_COLOR_ARRAY", 0x8076}, {"GL_DECR", 0x1e03}, {"GL_DEPTH_BITS", 0xd56}, {"GL_PIXEL_MAP_I_TO_I", 0xc70}, {"GL_POLYGON_STIPPLE_BIT", 0x10}, {"GL_ORDER", 0xa01}, {"GL_INCR", 0x1e02}, {"GL_LUMINANCE16", 0x8042}, {"GL_BACK_RIGHT", 0x403}, {"GL_READ_BUFFER", 0xc02}, {"GL_LUMINANCE6_ALPHA2", 0x8044}, {"GL_RED_BIAS", 0xd15}, {"GL_LUMINANCE4_ALPHA4", 0x8043}, {"GL_MAP1_NORMAL", 0xd92}, {"GL_EXP2", 0x801}, {"GL_LINE_STIPPLE_REPEAT", 0xb26}, {"GL_PIXEL_MAP_G_TO_G_SIZE", 0xcb7}, {"GL_BITMAP", 0x1a00}, {"GL_AUX3", 0x40c}, {"GL_LUMINANCE4", 0x803f}, {"GL_GEQUAL", 0x206}, {"GL_TEXTURE_INTERNAL_FORMAT", 0x1003}, {"GL_DEPTH_COMPONENT", 0x1902}, {"GL_POLYGON_BIT", 0x8}, {"GL_TEXTURE_BINDING_1D", 0x8068}, {"GL_FOG_BIT", 0x80}, {"GL_ONE", 0x1}, {"GL_LINE_STRIP", 0x3}, {"GL_LOGIC_OP_MODE", 0xbf0}, {"GL_FEEDBACK", 0x1c01}, {"GL_TEXTURE_PRIORITY", 0x8066}, {"GL_TEXTURE_COORD_ARRAY_POINTER", 0x8092}, {"GL_TEXTURE_ENV", 0x2300}, {"GL_TEXTURE_BIT", 0x40000}, {"GL_POINT_SIZE", 0xb11}, {"GL_UNPACK_ROW_LENGTH", 0xcf2}, {"GL_EVAL_BIT", 0x10000}, {"GL_STENCIL_INDEX", 0x1901}, {"GL_SCISSOR_TEST", 0xc11}, {"GL_BACK", 0x405}, {"GL_ALPHA_TEST_REF", 0xbc2}, {"GL_COLOR_BUFFER_BIT", 0x4000}, {"GL_STACK_OVERFLOW", 0x503}, {"GL_MAX_ATTRIB_STACK_DEPTH", 0xd35}, {"GL_RENDER", 0x1c00}, {"GL_TRANSFORM_BIT", 0x1000}, {"GL_TRIANGLE_FAN", 0x6}, {"GL_FRONT_FACE", 0xb46}, {"GL_STENCIL_BUFFER_BIT", 0x400}, {"GL_FOG_END", 0xb64}, {"GL_PIXEL_MODE_BIT", 0x20}, {"GL_AUX0", 0x409}, {"GL_UNSIGNED_BYTE", 0x1401}, {"GL_LINE_RESET_TOKEN", 0x707}, {"GL_LUMINANCE_ALPHA", 0x190a}, {"GL_DOMAIN", 0xa02}, {"GL_LINE_BIT", 0x4}, {"GL_NOOP", 0x1505}, {"GL_POINT_SMOOTH", 0xb10}, {"GL_4D_COLOR_TEXTURE", 0x604}, {"GL_ACCUM_ALPHA_BITS", 0xd5b}, {"GL_CURRENT_BIT", 0x1}, {"GL_NORMAL_ARRAY_STRIDE", 0x807f}, {"GL_OUT_OF_MEMORY", 0x505}, {"GL_LINE", 0x1b01}, {"GL_PIXEL_MAP_I_TO_B_SIZE", 0xcb4}, {"GL_CURRENT_INDEX", 0xb01}, {"GL_MAP2_INDEX", 0xdb1}, {"GL_2_BYTES", 0x1407}, {"GL_AUX_BUFFERS", 0xc00}, {"GL_INVERT", 0x150a}, {"GL_INVALID_OPERATION", 0x502}, {"GL_INVALID_VALUE", 0x501}, {"GL_INVALID_ENUM", 0x500}, {"GL_MAP1_GRID_SEGMENTS", 0xdd1}, {"GL_RGB12", 0x8053}, {"GL_NICEST", 0x1102}, {"GL_RENDERER", 0x1f01}, {"GL_RETURN", 0x102}, {"GL_LIGHT_MODEL_AMBIENT", 0xb53}, {"GL_TEXTURE", 0x1702}, {"GL_LUMINANCE", 0x1909}, {"GL_TEXTURE_GEN_T", 0xc61}, {"GL_INTENSITY8", 0x804b}, {"GL_DONT_CARE", 0x1100}, {"GL_INDEX_ARRAY", 0x8077}, {"GL_BLEND_SRC", 0xbe1}, {"GL_TEXTURE_GEN_R", 0xc62}, {"GL_AUX1", 0x40a}, {"GL_LIGHT_MODEL_LOCAL_VIEWER", 0xb51}, {"GL_R", 0x2002}, {"GL_RGB4", 0x804f}, {"GL_S", 0x2000}, {"GL_FRONT_AND_BACK", 0x408}, {"GL_NORMAL_ARRAY_TYPE", 0x807e}, {"GL_SPHERE_MAP", 0x2402}, {"GL_COLOR_INDEXES", 0x1603}, {"GL_QUADS", 0x7}, {"GL_LINE_STIPPLE", 0xb24}, {"GL_MAP2_COLOR_4", 0xdb0}, {"GL_EYE_LINEAR", 0x2400}, {"GL_TEXTURE_COORD_ARRAY", 0x8078}, {"GL_MAP2_VERTEX_3", 0xdb7}, {"GL_LINEAR_MIPMAP_NEAREST", 0x2701}, {"GL_DRAW_PIXEL_TOKEN", 0x705}, {"GL_T2F_C3F_V3F", 0x2a2a}, {"GL_EDGE_FLAG_ARRAY", 0x8079}, {"GL_TEXTURE_BORDER", 0x1005}, {"GL_MAP2_NORMAL", 0xdb2}, {"GL_SELECT", 0x1c02}, {"GL_EQUIV", 0x1509}, {"GL_TEXTURE_INTENSITY_SIZE", 0x8061}, {"GL_NEVER", 0x200}, {"GL_PIXEL_MAP_A_TO_A_SIZE", 0xcb9}, {"GL_MAP2_TEXTURE_COORD_4", 0xdb6}, {"GL_TEXTURE_RED_SIZE", 0x805c}, {"GL_TEXTURE_ALPHA_SIZE", 0x805f}, {"GL_TEXTURE_COORD_ARRAY_SIZE", 0x8088}, {"GL_COLOR_MATERIAL_FACE", 0xb55}, {"GL_SRC_COLOR", 0x300}, {"GL_TEXTURE_COMPONENTS", 0x1003}, {"GL_LIST_INDEX", 0xb33}, {"GL_ACCUM_CLEAR_VALUE", 0xb80}, {"GL_MAX_CLIENT_ATTRIB_STACK_DEPTH", 0xd3b}, {"GL_TEXTURE_2D", 0xde1}, {"GL_TEXTURE_1D", 0xde0}, {"GL_SHADE_MODEL", 0xb54}, {"GL_LIGHT2", 0x4002}, {"GL_POLYGON_OFFSET_POINT", 0x2a01}, {"GL_MAX_LIST_NESTING", 0xb31}, {"GL_ZOOM_Y", 0xd17}, {"GL_MATRIX_MODE", 0xba0}, {"GL_UNPACK_SWAP_BYTES", 0xcf0}, {"GL_MAP2_GRID_SEGMENTS", 0xdd3}, {"GL_PACK_SKIP_ROWS", 0xd03}, {"GL_PACK_LSB_FIRST", 0xd01}, {"GL_CLIP_PLANE4", 0x3004}, {"GL_INT", 0x1404}, {"GL_LIGHT5", 0x4005}, {"GL_MAX_TEXTURE_SIZE", 0xd33}, {"GL_ALPHA", 0x1906}, {"GL_COLOR_ARRAY_TYPE", 0x8082}, {"GL_EDGE_FLAG", 0xb43}, {"GL_XOR", 0x1506}, {"GL_ALPHA_TEST_FUNC", 0xbc1}, {"GL_SELECTION_BUFFER_SIZE", 0xdf4}, {"GL_PIXEL_MAP_B_TO_B_SIZE", 0xcb8}, {"GL_VERTEX_ARRAY_SIZE", 0x807a}, {"GL_AUTO_NORMAL", 0xd80}, {"GL_LINE_WIDTH_GRANULARITY", 0xb23}, {"GL_POLYGON_OFFSET_FILL", 0x8037}, {"GL_QUADRATIC_ATTENUATION", 0x1209}, {"GL_R3_G3_B2", 0x2a10}, {"GL_AUX2", 0x40b}, {"GL_GREEN_BIAS", 0xd19}, {"GL_VERTEX_ARRAY_TYPE", 0x807b}, {"GL_STENCIL_WRITEMASK", 0xb98}, {"GL_COLOR_WRITEMASK", 0xc23}, {"GL_INDEX_OFFSET", 0xd13}, {"GL_POLYGON_TOKEN", 0x703}, {"GL_LIGHTING_BIT", 0x40}, {"GL_POLYGON_SMOOTH_HINT", 0xc53}, {"GL_STEREO", 0xc33}, {"GL_CONSTANT_ATTENUATION", 0x1207}, {"GL_NORMALIZE", 0xba1}, {"GL_FOG_MODE", 0xb65}, {"GL_BLUE_BITS", 0xd54}, {"GL_MAP2_TEXTURE_COORD_3", 0xdb5}, {"GL_LOGIC_OP", 0xbf1}, {"GL_TEXTURE_STACK_DEPTH", 0xba5}, {"GL_DEPTH_FUNC", 0xb74}, {"GL_PASS_THROUGH_TOKEN", 0x700}, {"GL_RENDER_MODE", 0xc40}, {"GL_CURRENT_TEXTURE_COORDS", 0xb03}, {"GL_TEXTURE_WRAP_T", 0x2803}, {"GL_MAX_CLIP_PLANES", 0xd32}, {"GL_SUBPIXEL_BITS", 0xd50}, {"GL_3_BYTES", 0x1408}, {"GL_RED", 0x1903}, {"GL_TRIANGLES", 0x4}, {"GL_LINEAR_ATTENUATION", 0x1208}, {"GL_COLOR_LOGIC_OP", 0xbf2}, {"GL_POLYGON", 0x9}, {"GL_C4F_N3F_V3F", 0x2a26}, {"GL_DEPTH_RANGE", 0xb70}, {NULL, 0} }; static int gl__glColor4i(lua_State *L) { glColor4i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glIndexs(lua_State *L) { glIndexs( luaL_checknumber(L, 1)); return 0; } static int gl__glDrawArrays(lua_State *L) { glDrawArrays( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glMapGrid1d(lua_State *L) { glMapGrid1d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glColor4b(lua_State *L) { glColor4b( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glTexCoord1sv(lua_State *L) { glTexCoord1sv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord3f(lua_State *L) { glTexCoord3f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glColor3b(lua_State *L) { glColor3b( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glMapGrid1f(lua_State *L) { glMapGrid1f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glColor4dv(lua_State *L) { glColor4dv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord2d(lua_State *L) { glTexCoord2d( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glTexCoord3fv(lua_State *L) { glTexCoord3fv( checkpointer(L, 1)); return 0; } static int gl__glCopyTexImage1D(lua_State *L) { glCopyTexImage1D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7)); return 0; } static int gl__glNormal3bv(lua_State *L) { glNormal3bv( checkpointer(L, 1)); return 0; } static int gl__glColor3usv(lua_State *L) { glColor3usv( checkpointer(L, 1)); return 0; } static int gl__glTexGeniv(lua_State *L) { glTexGeniv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glTexCoord4d(lua_State *L) { glTexCoord4d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glMultMatrixf(lua_State *L) { glMultMatrixf( checkpointer(L, 1)); return 0; } static int gl__glNormal3sv(lua_State *L) { glNormal3sv( checkpointer(L, 1)); return 0; } static int gl__glFlush(lua_State *L) { glFlush(); return 0; } static int gl__glGetIntegerv(lua_State *L) { glGetIntegerv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glEvalCoord1dv(lua_State *L) { glEvalCoord1dv( checkpointer(L, 1)); return 0; } static int gl__glInterleavedArrays(lua_State *L) { glInterleavedArrays( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glColor3s(lua_State *L) { glColor3s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glEndList(lua_State *L) { glEndList(); return 0; } static int gl__glColor3d(lua_State *L) { glColor3d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glRasterPos3i(lua_State *L) { glRasterPos3i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glColor4sv(lua_State *L) { glColor4sv( checkpointer(L, 1)); return 0; } static int gl__glColor3ub(lua_State *L) { glColor3ub( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glPushName(lua_State *L) { glPushName( luaL_checknumber(L, 1)); return 0; } static int gl__glLightModelfv(lua_State *L) { glLightModelfv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glVertex3i(lua_State *L) { glVertex3i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glColorMaterial(lua_State *L) { glColorMaterial( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glFrontFace(lua_State *L) { glFrontFace( luaL_checknumber(L, 1)); return 0; } static int gl__glVertex4s(lua_State *L) { glVertex4s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glColor3ui(lua_State *L) { glColor3ui( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glVertex2sv(lua_State *L) { glVertex2sv( checkpointer(L, 1)); return 0; } static int gl__glEvalCoord1fv(lua_State *L) { glEvalCoord1fv( checkpointer(L, 1)); return 0; } static int gl__glTranslated(lua_State *L) { glTranslated( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glTexCoord1dv(lua_State *L) { glTexCoord1dv( checkpointer(L, 1)); return 0; } static int gl__glRectiv(lua_State *L) { glRectiv( checkpointer(L, 1), checkpointer(L, 2)); return 0; } static int gl__glVertex2fv(lua_State *L) { glVertex2fv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord4s(lua_State *L) { glTexCoord4s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glEvalCoord2dv(lua_State *L) { glEvalCoord2dv( checkpointer(L, 1)); return 0; } static int gl__glRects(lua_State *L) { glRects( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glTexCoord4f(lua_State *L) { glTexCoord4f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glVertex2f(lua_State *L) { glVertex2f( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glMap2f(lua_State *L) { glMap2f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), luaL_checknumber(L, 8), luaL_checknumber(L, 9), checkpointer(L, 10)); return 0; } static int gl__glGetMaterialfv(lua_State *L) { glGetMaterialfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glNormal3iv(lua_State *L) { glNormal3iv( checkpointer(L, 1)); return 0; } static int gl__glOrtho(lua_State *L) { glOrtho( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6)); return 0; } static int gl__glGetBooleanv(lua_State *L) { glGetBooleanv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glGetTexLevelParameteriv(lua_State *L) { glGetTexLevelParameteriv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), checkpointer(L, 4)); return 0; } static int gl__glPixelStorei(lua_State *L) { glPixelStorei( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glRasterPos2s(lua_State *L) { glRasterPos2s( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glNormal3f(lua_State *L) { glNormal3f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glEvalPoint1(lua_State *L) { glEvalPoint1( luaL_checknumber(L, 1)); return 0; } static int gl__glTexParameteriv(lua_State *L) { glTexParameteriv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glGetPixelMapfv(lua_State *L) { glGetPixelMapfv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glMatrixMode(lua_State *L) { glMatrixMode( luaL_checknumber(L, 1)); return 0; } static int gl__glScaled(lua_State *L) { glScaled( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glTexCoord2sv(lua_State *L) { glTexCoord2sv( checkpointer(L, 1)); return 0; } static int gl__glIndexdv(lua_State *L) { glIndexdv( checkpointer(L, 1)); return 0; } static int gl__glIndexf(lua_State *L) { glIndexf( luaL_checknumber(L, 1)); return 0; } static int gl__glEvalCoord2fv(lua_State *L) { glEvalCoord2fv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord1s(lua_State *L) { glTexCoord1s( luaL_checknumber(L, 1)); return 0; } static int gl__glDrawElements(lua_State *L) { glDrawElements( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), checkpointer(L, 4)); return 0; } static int gl__glVertex4fv(lua_State *L) { glVertex4fv( checkpointer(L, 1)); return 0; } static int gl__glRasterPos3dv(lua_State *L) { glRasterPos3dv( checkpointer(L, 1)); return 0; } static int gl__glClearDepth(lua_State *L) { glClearDepth( luaL_checknumber(L, 1)); return 0; } static int gl__glPolygonMode(lua_State *L) { glPolygonMode( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glVertex4d(lua_State *L) { glVertex4d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glTexEnvfv(lua_State *L) { glTexEnvfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glRasterPos2i(lua_State *L) { glRasterPos2i( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glColor4bv(lua_State *L) { glColor4bv( checkpointer(L, 1)); return 0; } static int gl__glRectsv(lua_State *L) { glRectsv( checkpointer(L, 1), checkpointer(L, 2)); return 0; } static int gl__glColor3ubv(lua_State *L) { glColor3ubv( checkpointer(L, 1)); return 0; } static int gl__glColor3f(lua_State *L) { glColor3f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glStencilMask(lua_State *L) { glStencilMask( luaL_checknumber(L, 1)); return 0; } static int gl__glColor4iv(lua_State *L) { glColor4iv( checkpointer(L, 1)); return 0; } static int gl__glRotated(lua_State *L) { glRotated( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glColorMask(lua_State *L) { glColorMask( lua_toboolean(L, 1), lua_toboolean(L, 2), lua_toboolean(L, 3), lua_toboolean(L, 4)); return 0; } static int gl__glTexParameterfv(lua_State *L) { glTexParameterfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glLightModelf(lua_State *L) { glLightModelf( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glScalef(lua_State *L) { glScalef( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glBegin(lua_State *L) { glBegin( luaL_checknumber(L, 1)); return 0; } static int gl__glRotatef(lua_State *L) { glRotatef( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glInitNames(lua_State *L) { glInitNames(); return 0; } static int gl__glRasterPos3sv(lua_State *L) { glRasterPos3sv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord3sv(lua_State *L) { glTexCoord3sv( checkpointer(L, 1)); return 0; } static int gl__glIndexubv(lua_State *L) { glIndexubv( checkpointer(L, 1)); return 0; } static int gl__glNormal3d(lua_State *L) { glNormal3d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glTexCoord2dv(lua_State *L) { glTexCoord2dv( checkpointer(L, 1)); return 0; } static int gl__glVertex3s(lua_State *L) { glVertex3s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glPolygonOffset(lua_State *L) { glPolygonOffset( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glClearColor(lua_State *L) { glClearColor( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glTexCoord1iv(lua_State *L) { glTexCoord1iv( checkpointer(L, 1)); return 0; } static int gl__glGetFloatv(lua_State *L) { glGetFloatv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glGetTexEnviv(lua_State *L) { glGetTexEnviv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glRecti(lua_State *L) { glRecti( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glNormal3dv(lua_State *L) { glNormal3dv( checkpointer(L, 1)); return 0; } static int gl__glEdgeFlag(lua_State *L) { glEdgeFlag( lua_toboolean(L, 1)); return 0; } static int gl__glColor3sv(lua_State *L) { glColor3sv( checkpointer(L, 1)); return 0; } static int gl__glMap2d(lua_State *L) { glMap2d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), luaL_checknumber(L, 8), luaL_checknumber(L, 9), checkpointer(L, 10)); return 0; } static int gl__glClearAccum(lua_State *L) { glClearAccum( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glTranslatef(lua_State *L) { glTranslatef( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glTexCoord4iv(lua_State *L) { glTexCoord4iv( checkpointer(L, 1)); return 0; } static int gl__glPassThrough(lua_State *L) { glPassThrough( luaL_checknumber(L, 1)); return 0; } static int gl__glNewList(lua_State *L) { glNewList( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glEnd(lua_State *L) { glEnd(); return 0; } static int gl__glGetMapdv(lua_State *L) { glGetMapdv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glLoadMatrixd(lua_State *L) { glLoadMatrixd( checkpointer(L, 1)); return 0; } static int gl__glGetPolygonStipple(lua_State *L) { glGetPolygonStipple( checkpointer(L, 1)); return 0; } static int gl__glGetString(lua_State *L) { lua_pushstring(L, glGetString( luaL_checknumber(L, 1))); return 1; } static int gl__glPixelTransferf(lua_State *L) { glPixelTransferf( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glCopyTexSubImage2D(lua_State *L) { glCopyTexSubImage2D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), luaL_checknumber(L, 8)); return 0; } static int gl__glVertex4dv(lua_State *L) { glVertex4dv( checkpointer(L, 1)); return 0; } static int gl__glRasterPos2sv(lua_State *L) { glRasterPos2sv( checkpointer(L, 1)); return 0; } static int gl__glVertex4sv(lua_State *L) { glVertex4sv( checkpointer(L, 1)); return 0; } static int gl__glFogi(lua_State *L) { glFogi( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glRectdv(lua_State *L) { glRectdv( checkpointer(L, 1), checkpointer(L, 2)); return 0; } static int gl__glIndexsv(lua_State *L) { glIndexsv( checkpointer(L, 1)); return 0; } static int gl__glRasterPos4f(lua_State *L) { glRasterPos4f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glDisable(lua_State *L) { glDisable( luaL_checknumber(L, 1)); return 0; } static int gl__glColor4uiv(lua_State *L) { glColor4uiv( checkpointer(L, 1)); return 0; } static int gl__glPixelMapfv(lua_State *L) { glPixelMapfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glIndexPointer(lua_State *L) { glIndexPointer( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glDrawBuffer(lua_State *L) { glDrawBuffer( luaL_checknumber(L, 1)); return 0; } static int gl__glGetMapfv(lua_State *L) { glGetMapfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glLineWidth(lua_State *L) { glLineWidth( luaL_checknumber(L, 1)); return 0; } static int gl__glReadBuffer(lua_State *L) { glReadBuffer( luaL_checknumber(L, 1)); return 0; } static int gl__glGetTexGendv(lua_State *L) { glGetTexGendv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glRasterPos2dv(lua_State *L) { glRasterPos2dv( checkpointer(L, 1)); return 0; } static int gl__glGenLists(lua_State *L) { lua_pushnumber(L, glGenLists( luaL_checknumber(L, 1))); return 1; } static int gl__glRasterPos4d(lua_State *L) { glRasterPos4d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glDepthFunc(lua_State *L) { glDepthFunc( luaL_checknumber(L, 1)); return 0; } static int gl__glHint(lua_State *L) { glHint( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glMaterialf(lua_State *L) { glMaterialf( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glPushAttrib(lua_State *L) { glPushAttrib( luaL_checknumber(L, 1)); return 0; } static int gl__glTexCoord3dv(lua_State *L) { glTexCoord3dv( checkpointer(L, 1)); return 0; } static int gl__glArrayElement(lua_State *L) { glArrayElement( luaL_checknumber(L, 1)); return 0; } static int gl__glLoadName(lua_State *L) { glLoadName( luaL_checknumber(L, 1)); return 0; } static int gl__glSelectBuffer(lua_State *L) { glSelectBuffer( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glFeedbackBuffer(lua_State *L) { glFeedbackBuffer( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glFogiv(lua_State *L) { glFogiv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glRasterPos4fv(lua_State *L) { glRasterPos4fv( checkpointer(L, 1)); return 0; } static int gl__glTexGend(lua_State *L) { glTexGend( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glLoadMatrixf(lua_State *L) { glLoadMatrixf( checkpointer(L, 1)); return 0; } static int gl__glTexCoord4i(lua_State *L) { glTexCoord4i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glVertex4iv(lua_State *L) { glVertex4iv( checkpointer(L, 1)); return 0; } static int gl__glGetTexGeniv(lua_State *L) { glGetTexGeniv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glFogf(lua_State *L) { glFogf( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glRectfv(lua_State *L) { glRectfv( checkpointer(L, 1), checkpointer(L, 2)); return 0; } static int gl__glShadeModel(lua_State *L) { glShadeModel( luaL_checknumber(L, 1)); return 0; } static int gl__glColor4usv(lua_State *L) { glColor4usv( checkpointer(L, 1)); return 0; } static int gl__glVertex2d(lua_State *L) { glVertex2d( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glMapGrid2f(lua_State *L) { glMapGrid2f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6)); return 0; } static int gl__glIndexMask(lua_State *L) { glIndexMask( luaL_checknumber(L, 1)); return 0; } static int gl__glDeleteLists(lua_State *L) { glDeleteLists( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glColorPointer(lua_State *L) { glColorPointer( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), checkpointer(L, 4)); return 0; } static int gl__glEnable(lua_State *L) { glEnable( luaL_checknumber(L, 1)); return 0; } static int gl__glNormal3i(lua_State *L) { glNormal3i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glEvalMesh2(lua_State *L) { glEvalMesh2( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5)); return 0; } static int gl__glRectf(lua_State *L) { glRectf( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glCopyPixels(lua_State *L) { glCopyPixels( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5)); return 0; } static int gl__glTexGenf(lua_State *L) { glTexGenf( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glEvalPoint2(lua_State *L) { glEvalPoint2( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glMapGrid2d(lua_State *L) { glMapGrid2d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6)); return 0; } static int gl__glPushMatrix(lua_State *L) { glPushMatrix(); return 0; } static int gl__glDeleteTextures(lua_State *L) { glDeleteTextures( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glGetTexEnvfv(lua_State *L) { glGetTexEnvfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glColor3bv(lua_State *L) { glColor3bv( checkpointer(L, 1)); return 0; } static int gl__glMap1f(lua_State *L) { glMap1f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), checkpointer(L, 6)); return 0; } static int gl__glTexCoord1i(lua_State *L) { glTexCoord1i( luaL_checknumber(L, 1)); return 0; } static int gl__glRasterPos4dv(lua_State *L) { glRasterPos4dv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord3s(lua_State *L) { glTexCoord3s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glDisableClientState(lua_State *L) { glDisableClientState( luaL_checknumber(L, 1)); return 0; } static int gl__glClipPlane(lua_State *L) { glClipPlane( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glEvalCoord2d(lua_State *L) { glEvalCoord2d( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glEvalCoord1f(lua_State *L) { glEvalCoord1f( luaL_checknumber(L, 1)); return 0; } static int gl__glEvalCoord1d(lua_State *L) { glEvalCoord1d( luaL_checknumber(L, 1)); return 0; } static int gl__glTexCoord2fv(lua_State *L) { glTexCoord2fv( checkpointer(L, 1)); return 0; } static int gl__glPointSize(lua_State *L) { glPointSize( luaL_checknumber(L, 1)); return 0; } static int gl__glGetMapiv(lua_State *L) { glGetMapiv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glMap1d(lua_State *L) { glMap1d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), checkpointer(L, 6)); return 0; } static int gl__glCopyTexSubImage1D(lua_State *L) { glCopyTexSubImage1D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6)); return 0; } static int gl__glVertex3fv(lua_State *L) { glVertex3fv( checkpointer(L, 1)); return 0; } static int gl__glCopyTexImage2D(lua_State *L) { glCopyTexImage2D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), luaL_checknumber(L, 8)); return 0; } static int gl__glTexSubImage2D(lua_State *L) { glTexSubImage2D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), luaL_checknumber(L, 8), checkpointer(L, 9)); return 0; } static int gl__glColor4us(lua_State *L) { glColor4us( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glPixelTransferi(lua_State *L) { glPixelTransferi( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glPopName(lua_State *L) { glPopName(); return 0; } static int gl__glIsTexture(lua_State *L) { lua_pushboolean(L, glIsTexture( luaL_checknumber(L, 1))); return 1; } static int gl__glAlphaFunc(lua_State *L) { glAlphaFunc( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glEdgeFlagv(lua_State *L) { glEdgeFlagv( checkpointer(L, 1)); return 0; } static int gl__glListBase(lua_State *L) { glListBase( luaL_checknumber(L, 1)); return 0; } static int gl__glRenderMode(lua_State *L) { lua_pushnumber(L, glRenderMode( luaL_checknumber(L, 1))); return 1; } static int gl__glGetMaterialiv(lua_State *L) { glGetMaterialiv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glVertex3d(lua_State *L) { glVertex3d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glLogicOp(lua_State *L) { glLogicOp( luaL_checknumber(L, 1)); return 0; } static int gl__glLineStipple(lua_State *L) { glLineStipple( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glPixelMapusv(lua_State *L) { glPixelMapusv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glBindTexture(lua_State *L) { glBindTexture( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glGenTextures(lua_State *L) { glGenTextures( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glGetTexImage(lua_State *L) { glGetTexImage( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), checkpointer(L, 5)); return 0; } static int gl__glCullFace(lua_State *L) { glCullFace( luaL_checknumber(L, 1)); return 0; } static int gl__glRasterPos2fv(lua_State *L) { glRasterPos2fv( checkpointer(L, 1)); return 0; } static int gl__glTexImage2D(lua_State *L) { glTexImage2D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), luaL_checknumber(L, 8), checkpointer(L, 9)); return 0; } static int gl__glEnableClientState(lua_State *L) { glEnableClientState( luaL_checknumber(L, 1)); return 0; } static int gl__glGetTexParameteriv(lua_State *L) { glGetTexParameteriv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glGetError(lua_State *L) { lua_pushnumber(L, glGetError()); return 1; } static int gl__glTexImage1D(lua_State *L) { glTexImage1D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), checkpointer(L, 8)); return 0; } static int gl__glGetTexLevelParameterfv(lua_State *L) { glGetTexLevelParameterfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), checkpointer(L, 4)); return 0; } static int gl__glGetTexParameterfv(lua_State *L) { glGetTexParameterfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glPixelStoref(lua_State *L) { glPixelStoref( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glTexParameterf(lua_State *L) { glTexParameterf( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glVertex2iv(lua_State *L) { glVertex2iv( checkpointer(L, 1)); return 0; } static int gl__glEvalCoord2f(lua_State *L) { glEvalCoord2f( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glTexEnvi(lua_State *L) { glTexEnvi( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glTexEnvf(lua_State *L) { glTexEnvf( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glLightModeli(lua_State *L) { glLightModeli( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glGetTexGenfv(lua_State *L) { glGetTexGenfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glTexGenfv(lua_State *L) { glTexGenfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glTexGendv(lua_State *L) { glTexGendv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glTexGeni(lua_State *L) { glTexGeni( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glTexCoordPointer(lua_State *L) { glTexCoordPointer( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), checkpointer(L, 4)); return 0; } static int gl__glFinish(lua_State *L) { glFinish(); return 0; } static int gl__glRasterPos2d(lua_State *L) { glRasterPos2d( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glVertex3f(lua_State *L) { glVertex3f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glIndexiv(lua_State *L) { glIndexiv( checkpointer(L, 1)); return 0; } static int gl__glStencilFunc(lua_State *L) { glStencilFunc( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glEvalMesh1(lua_State *L) { glEvalMesh1( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glDrawPixels(lua_State *L) { glDrawPixels( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), checkpointer(L, 5)); return 0; } static int gl__glReadPixels(lua_State *L) { glReadPixels( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), checkpointer(L, 7)); return 0; } static int gl__glBlendFunc(lua_State *L) { glBlendFunc( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glIndexfv(lua_State *L) { glIndexfv( checkpointer(L, 1)); return 0; } static int gl__glGetPixelMapuiv(lua_State *L) { glGetPixelMapuiv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glColor4f(lua_State *L) { glColor4f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glIndexi(lua_State *L) { glIndexi( luaL_checknumber(L, 1)); return 0; } static int gl__glRasterPos3fv(lua_State *L) { glRasterPos3fv( checkpointer(L, 1)); return 0; } static int gl__glPixelMapuiv(lua_State *L) { glPixelMapuiv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glStencilOp(lua_State *L) { glStencilOp( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glPopAttrib(lua_State *L) { glPopAttrib(); return 0; } static int gl__glBitmap(lua_State *L) { glBitmap( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), checkpointer(L, 7)); return 0; } static int gl__glPolygonStipple(lua_State *L) { glPolygonStipple( checkpointer(L, 1)); return 0; } static int gl__glVertex3dv(lua_State *L) { glVertex3dv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord2f(lua_State *L) { glTexCoord2f( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glColor3fv(lua_State *L) { glColor3fv( checkpointer(L, 1)); return 0; } static int gl__glCallList(lua_State *L) { glCallList( luaL_checknumber(L, 1)); return 0; } static int gl__glColor3i(lua_State *L) { glColor3i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glVertex2s(lua_State *L) { glVertex2s( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glTexParameteri(lua_State *L) { glTexParameteri( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glMaterialiv(lua_State *L) { glMaterialiv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glColor4fv(lua_State *L) { glColor4fv( checkpointer(L, 1)); return 0; } static int gl__glTexEnviv(lua_State *L) { glTexEnviv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glRasterPos3iv(lua_State *L) { glRasterPos3iv( checkpointer(L, 1)); return 0; } static int gl__glIsEnabled(lua_State *L) { lua_pushboolean(L, glIsEnabled( luaL_checknumber(L, 1))); return 1; } static int gl__glLoadIdentity(lua_State *L) { glLoadIdentity(); return 0; } static int gl__glTexCoord4sv(lua_State *L) { glTexCoord4sv( checkpointer(L, 1)); return 0; } static int gl__glMateriali(lua_State *L) { glMateriali( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glLightModeliv(lua_State *L) { glLightModeliv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glTexCoord1fv(lua_State *L) { glTexCoord1fv( checkpointer(L, 1)); return 0; } static int gl__glVertex2dv(lua_State *L) { glVertex2dv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord1d(lua_State *L) { glTexCoord1d( luaL_checknumber(L, 1)); return 0; } static int gl__glGetLightiv(lua_State *L) { glGetLightiv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glGetLightfv(lua_State *L) { glGetLightfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glLightiv(lua_State *L) { glLightiv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glEdgeFlagPointer(lua_State *L) { glEdgeFlagPointer( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glAccum(lua_State *L) { glAccum( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glLightfv(lua_State *L) { glLightfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glLighti(lua_State *L) { glLighti( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glColor4s(lua_State *L) { glColor4s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glGetDoublev(lua_State *L) { glGetDoublev( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glDepthRange(lua_State *L) { glDepthRange( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glPixelZoom(lua_State *L) { glPixelZoom( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glRasterPos4i(lua_State *L) { glRasterPos4i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glMaterialfv(lua_State *L) { glMaterialfv( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glVertex2i(lua_State *L) { glVertex2i( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glTexSubImage1D(lua_State *L) { glTexSubImage1D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), checkpointer(L, 7)); return 0; } static int gl__glPrioritizeTextures(lua_State *L) { glPrioritizeTextures( luaL_checknumber(L, 1), checkpointer(L, 2), checkpointer(L, 3)); return 0; } static int gl__glTexCoord3i(lua_State *L) { glTexCoord3i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glClearStencil(lua_State *L) { glClearStencil( luaL_checknumber(L, 1)); return 0; } static int gl__glViewport(lua_State *L) { glViewport( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glNormal3fv(lua_State *L) { glNormal3fv( checkpointer(L, 1)); return 0; } static int gl__glGetPointerv(lua_State *L) { glGetPointerv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glClear(lua_State *L) { glClear( luaL_checknumber(L, 1)); return 0; } static int gl__glColor4ubv(lua_State *L) { glColor4ubv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord2iv(lua_State *L) { glTexCoord2iv( checkpointer(L, 1)); return 0; } static int gl__glColor3us(lua_State *L) { glColor3us( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glColor4ub(lua_State *L) { glColor4ub( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glTexCoord4dv(lua_State *L) { glTexCoord4dv( checkpointer(L, 1)); return 0; } static int gl__glGetPixelMapusv(lua_State *L) { glGetPixelMapusv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glColor4d(lua_State *L) { glColor4d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glRasterPos3d(lua_State *L) { glRasterPos3d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glNormal3b(lua_State *L) { glNormal3b( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glNormalPointer(lua_State *L) { glNormalPointer( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glColor4ui(lua_State *L) { glColor4ui( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glMultMatrixd(lua_State *L) { glMultMatrixd( checkpointer(L, 1)); return 0; } static int gl__glColor3iv(lua_State *L) { glColor3iv( checkpointer(L, 1)); return 0; } static int gl__glIsList(lua_State *L) { lua_pushboolean(L, glIsList( luaL_checknumber(L, 1))); return 1; } static int gl__glVertex3sv(lua_State *L) { glVertex3sv( checkpointer(L, 1)); return 0; } static int gl__glRasterPos4sv(lua_State *L) { glRasterPos4sv( checkpointer(L, 1)); return 0; } static int gl__glVertexPointer(lua_State *L) { glVertexPointer( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), checkpointer(L, 4)); return 0; } static int gl__glPushClientAttrib(lua_State *L) { glPushClientAttrib( luaL_checknumber(L, 1)); return 0; } static int gl__glRectd(lua_State *L) { glRectd( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glPopClientAttrib(lua_State *L) { glPopClientAttrib(); return 0; } static int gl__glRasterPos2iv(lua_State *L) { glRasterPos2iv( checkpointer(L, 1)); return 0; } static int gl__glGetClipPlane(lua_State *L) { glGetClipPlane( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glRasterPos4s(lua_State *L) { glRasterPos4s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glVertex3iv(lua_State *L) { glVertex3iv( checkpointer(L, 1)); return 0; } static int gl__glScissor(lua_State *L) { glScissor( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glColor3dv(lua_State *L) { glColor3dv( checkpointer(L, 1)); return 0; } static int gl__glRasterPos3s(lua_State *L) { glRasterPos3s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glRasterPos3f(lua_State *L) { glRasterPos3f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glTexCoord2s(lua_State *L) { glTexCoord2s( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glClearIndex(lua_State *L) { glClearIndex( luaL_checknumber(L, 1)); return 0; } static int gl__glRasterPos2f(lua_State *L) { glRasterPos2f( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glLightf(lua_State *L) { glLightf( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glVertex4f(lua_State *L) { glVertex4f( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glTexCoord4fv(lua_State *L) { glTexCoord4fv( checkpointer(L, 1)); return 0; } static int gl__glTexCoord3iv(lua_State *L) { glTexCoord3iv( checkpointer(L, 1)); return 0; } static int gl__glNormal3s(lua_State *L) { glNormal3s( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glPopMatrix(lua_State *L) { glPopMatrix(); return 0; } static int gl__glTexCoord2i(lua_State *L) { glTexCoord2i( luaL_checknumber(L, 1), luaL_checknumber(L, 2)); return 0; } static int gl__glVertex4i(lua_State *L) { glVertex4i( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int gl__glFrustum(lua_State *L) { glFrustum( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6)); return 0; } static int gl__glFogfv(lua_State *L) { glFogfv( luaL_checknumber(L, 1), checkpointer(L, 2)); return 0; } static int gl__glTexCoord3d(lua_State *L) { glTexCoord3d( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int gl__glTexCoord1f(lua_State *L) { glTexCoord1f( luaL_checknumber(L, 1)); return 0; } static int gl__glColor3uiv(lua_State *L) { glColor3uiv( checkpointer(L, 1)); return 0; } static int gl__glIndexub(lua_State *L) { glIndexub( luaL_checknumber(L, 1)); return 0; } static int gl__glAreTexturesResident(lua_State *L) { lua_pushboolean(L, glAreTexturesResident( luaL_checknumber(L, 1), checkpointer(L, 2), checkpointer(L, 3))); return 1; } static int gl__glIndexd(lua_State *L) { glIndexd( luaL_checknumber(L, 1)); return 0; } static int gl__glDepthMask(lua_State *L) { glDepthMask( lua_toboolean(L, 1)); return 0; } static int gl__glCallLists(lua_State *L) { glCallLists( luaL_checknumber(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int gl__glRasterPos4iv(lua_State *L) { glRasterPos4iv( checkpointer(L, 1)); return 0; } static const luaL_Reg functions [] = { {"glColor4i", gl__glColor4i}, {"glIndexs", gl__glIndexs}, {"glDrawArrays", gl__glDrawArrays}, {"glMapGrid1d", gl__glMapGrid1d}, {"glColor4b", gl__glColor4b}, {"glTexCoord1sv", gl__glTexCoord1sv}, {"glTexCoord3f", gl__glTexCoord3f}, {"glColor3b", gl__glColor3b}, {"glMapGrid1f", gl__glMapGrid1f}, {"glColor4dv", gl__glColor4dv}, {"glTexCoord2d", gl__glTexCoord2d}, {"glTexCoord3fv", gl__glTexCoord3fv}, {"glCopyTexImage1D", gl__glCopyTexImage1D}, {"glNormal3bv", gl__glNormal3bv}, {"glColor3usv", gl__glColor3usv}, {"glTexGeniv", gl__glTexGeniv}, {"glTexCoord4d", gl__glTexCoord4d}, {"glMultMatrixf", gl__glMultMatrixf}, {"glNormal3sv", gl__glNormal3sv}, {"glFlush", gl__glFlush}, {"glGetIntegerv", gl__glGetIntegerv}, {"glEvalCoord1dv", gl__glEvalCoord1dv}, {"glInterleavedArrays", gl__glInterleavedArrays}, {"glColor3s", gl__glColor3s}, {"glEndList", gl__glEndList}, {"glColor3d", gl__glColor3d}, {"glRasterPos3i", gl__glRasterPos3i}, {"glColor4sv", gl__glColor4sv}, {"glColor3ub", gl__glColor3ub}, {"glPushName", gl__glPushName}, {"glLightModelfv", gl__glLightModelfv}, {"glVertex3i", gl__glVertex3i}, {"glColorMaterial", gl__glColorMaterial}, {"glFrontFace", gl__glFrontFace}, {"glVertex4s", gl__glVertex4s}, {"glColor3ui", gl__glColor3ui}, {"glVertex2sv", gl__glVertex2sv}, {"glEvalCoord1fv", gl__glEvalCoord1fv}, {"glTranslated", gl__glTranslated}, {"glTexCoord1dv", gl__glTexCoord1dv}, {"glRectiv", gl__glRectiv}, {"glVertex2fv", gl__glVertex2fv}, {"glTexCoord4s", gl__glTexCoord4s}, {"glEvalCoord2dv", gl__glEvalCoord2dv}, {"glRects", gl__glRects}, {"glTexCoord4f", gl__glTexCoord4f}, {"glVertex2f", gl__glVertex2f}, {"glMap2f", gl__glMap2f}, {"glGetMaterialfv", gl__glGetMaterialfv}, {"glNormal3iv", gl__glNormal3iv}, {"glOrtho", gl__glOrtho}, {"glGetBooleanv", gl__glGetBooleanv}, {"glGetTexLevelParameteriv", gl__glGetTexLevelParameteriv}, {"glPixelStorei", gl__glPixelStorei}, {"glRasterPos2s", gl__glRasterPos2s}, {"glNormal3f", gl__glNormal3f}, {"glEvalPoint1", gl__glEvalPoint1}, {"glTexParameteriv", gl__glTexParameteriv}, {"glGetPixelMapfv", gl__glGetPixelMapfv}, {"glMatrixMode", gl__glMatrixMode}, {"glScaled", gl__glScaled}, {"glTexCoord2sv", gl__glTexCoord2sv}, {"glIndexdv", gl__glIndexdv}, {"glIndexf", gl__glIndexf}, {"glEvalCoord2fv", gl__glEvalCoord2fv}, {"glTexCoord1s", gl__glTexCoord1s}, {"glDrawElements", gl__glDrawElements}, {"glVertex4fv", gl__glVertex4fv}, {"glRasterPos3dv", gl__glRasterPos3dv}, {"glClearDepth", gl__glClearDepth}, {"glPolygonMode", gl__glPolygonMode}, {"glVertex4d", gl__glVertex4d}, {"glTexEnvfv", gl__glTexEnvfv}, {"glRasterPos2i", gl__glRasterPos2i}, {"glColor4bv", gl__glColor4bv}, {"glRectsv", gl__glRectsv}, {"glColor3ubv", gl__glColor3ubv}, {"glColor3f", gl__glColor3f}, {"glStencilMask", gl__glStencilMask}, {"glColor4iv", gl__glColor4iv}, {"glRotated", gl__glRotated}, {"glColorMask", gl__glColorMask}, {"glTexParameterfv", gl__glTexParameterfv}, {"glLightModelf", gl__glLightModelf}, {"glScalef", gl__glScalef}, {"glBegin", gl__glBegin}, {"glRotatef", gl__glRotatef}, {"glInitNames", gl__glInitNames}, {"glRasterPos3sv", gl__glRasterPos3sv}, {"glTexCoord3sv", gl__glTexCoord3sv}, {"glIndexubv", gl__glIndexubv}, {"glNormal3d", gl__glNormal3d}, {"glTexCoord2dv", gl__glTexCoord2dv}, {"glVertex3s", gl__glVertex3s}, {"glPolygonOffset", gl__glPolygonOffset}, {"glClearColor", gl__glClearColor}, {"glTexCoord1iv", gl__glTexCoord1iv}, {"glGetFloatv", gl__glGetFloatv}, {"glGetTexEnviv", gl__glGetTexEnviv}, {"glRecti", gl__glRecti}, {"glNormal3dv", gl__glNormal3dv}, {"glEdgeFlag", gl__glEdgeFlag}, {"glColor3sv", gl__glColor3sv}, {"glMap2d", gl__glMap2d}, {"glClearAccum", gl__glClearAccum}, {"glTranslatef", gl__glTranslatef}, {"glTexCoord4iv", gl__glTexCoord4iv}, {"glPassThrough", gl__glPassThrough}, {"glNewList", gl__glNewList}, {"glEnd", gl__glEnd}, {"glGetMapdv", gl__glGetMapdv}, {"glLoadMatrixd", gl__glLoadMatrixd}, {"glGetPolygonStipple", gl__glGetPolygonStipple}, {"glGetString", gl__glGetString}, {"glPixelTransferf", gl__glPixelTransferf}, {"glCopyTexSubImage2D", gl__glCopyTexSubImage2D}, {"glVertex4dv", gl__glVertex4dv}, {"glRasterPos2sv", gl__glRasterPos2sv}, {"glVertex4sv", gl__glVertex4sv}, {"glFogi", gl__glFogi}, {"glRectdv", gl__glRectdv}, {"glIndexsv", gl__glIndexsv}, {"glRasterPos4f", gl__glRasterPos4f}, {"glDisable", gl__glDisable}, {"glColor4uiv", gl__glColor4uiv}, {"glPixelMapfv", gl__glPixelMapfv}, {"glIndexPointer", gl__glIndexPointer}, {"glDrawBuffer", gl__glDrawBuffer}, {"glGetMapfv", gl__glGetMapfv}, {"glLineWidth", gl__glLineWidth}, {"glReadBuffer", gl__glReadBuffer}, {"glGetTexGendv", gl__glGetTexGendv}, {"glRasterPos2dv", gl__glRasterPos2dv}, {"glGenLists", gl__glGenLists}, {"glRasterPos4d", gl__glRasterPos4d}, {"glDepthFunc", gl__glDepthFunc}, {"glHint", gl__glHint}, {"glMaterialf", gl__glMaterialf}, {"glPushAttrib", gl__glPushAttrib}, {"glTexCoord3dv", gl__glTexCoord3dv}, {"glArrayElement", gl__glArrayElement}, {"glLoadName", gl__glLoadName}, {"glSelectBuffer", gl__glSelectBuffer}, {"glFeedbackBuffer", gl__glFeedbackBuffer}, {"glFogiv", gl__glFogiv}, {"glRasterPos4fv", gl__glRasterPos4fv}, {"glTexGend", gl__glTexGend}, {"glLoadMatrixf", gl__glLoadMatrixf}, {"glTexCoord4i", gl__glTexCoord4i}, {"glVertex4iv", gl__glVertex4iv}, {"glGetTexGeniv", gl__glGetTexGeniv}, {"glFogf", gl__glFogf}, {"glRectfv", gl__glRectfv}, {"glShadeModel", gl__glShadeModel}, {"glColor4usv", gl__glColor4usv}, {"glVertex2d", gl__glVertex2d}, {"glMapGrid2f", gl__glMapGrid2f}, {"glIndexMask", gl__glIndexMask}, {"glDeleteLists", gl__glDeleteLists}, {"glColorPointer", gl__glColorPointer}, {"glEnable", gl__glEnable}, {"glNormal3i", gl__glNormal3i}, {"glEvalMesh2", gl__glEvalMesh2}, {"glRectf", gl__glRectf}, {"glCopyPixels", gl__glCopyPixels}, {"glTexGenf", gl__glTexGenf}, {"glEvalPoint2", gl__glEvalPoint2}, {"glMapGrid2d", gl__glMapGrid2d}, {"glPushMatrix", gl__glPushMatrix}, {"glDeleteTextures", gl__glDeleteTextures}, {"glGetTexEnvfv", gl__glGetTexEnvfv}, {"glColor3bv", gl__glColor3bv}, {"glMap1f", gl__glMap1f}, {"glTexCoord1i", gl__glTexCoord1i}, {"glRasterPos4dv", gl__glRasterPos4dv}, {"glTexCoord3s", gl__glTexCoord3s}, {"glDisableClientState", gl__glDisableClientState}, {"glClipPlane", gl__glClipPlane}, {"glEvalCoord2d", gl__glEvalCoord2d}, {"glEvalCoord1f", gl__glEvalCoord1f}, {"glEvalCoord1d", gl__glEvalCoord1d}, {"glTexCoord2fv", gl__glTexCoord2fv}, {"glPointSize", gl__glPointSize}, {"glGetMapiv", gl__glGetMapiv}, {"glMap1d", gl__glMap1d}, {"glCopyTexSubImage1D", gl__glCopyTexSubImage1D}, {"glVertex3fv", gl__glVertex3fv}, {"glCopyTexImage2D", gl__glCopyTexImage2D}, {"glTexSubImage2D", gl__glTexSubImage2D}, {"glColor4us", gl__glColor4us}, {"glPixelTransferi", gl__glPixelTransferi}, {"glPopName", gl__glPopName}, {"glIsTexture", gl__glIsTexture}, {"glAlphaFunc", gl__glAlphaFunc}, {"glEdgeFlagv", gl__glEdgeFlagv}, {"glListBase", gl__glListBase}, {"glRenderMode", gl__glRenderMode}, {"glGetMaterialiv", gl__glGetMaterialiv}, {"glVertex3d", gl__glVertex3d}, {"glLogicOp", gl__glLogicOp}, {"glLineStipple", gl__glLineStipple}, {"glPixelMapusv", gl__glPixelMapusv}, {"glBindTexture", gl__glBindTexture}, {"glGenTextures", gl__glGenTextures}, {"glGetTexImage", gl__glGetTexImage}, {"glCullFace", gl__glCullFace}, {"glRasterPos2fv", gl__glRasterPos2fv}, {"glTexImage2D", gl__glTexImage2D}, {"glEnableClientState", gl__glEnableClientState}, {"glGetTexParameteriv", gl__glGetTexParameteriv}, {"glGetError", gl__glGetError}, {"glTexImage1D", gl__glTexImage1D}, {"glGetTexLevelParameterfv", gl__glGetTexLevelParameterfv}, {"glGetTexParameterfv", gl__glGetTexParameterfv}, {"glPixelStoref", gl__glPixelStoref}, {"glTexParameterf", gl__glTexParameterf}, {"glVertex2iv", gl__glVertex2iv}, {"glEvalCoord2f", gl__glEvalCoord2f}, {"glTexEnvi", gl__glTexEnvi}, {"glTexEnvf", gl__glTexEnvf}, {"glLightModeli", gl__glLightModeli}, {"glGetTexGenfv", gl__glGetTexGenfv}, {"glTexGenfv", gl__glTexGenfv}, {"glTexGendv", gl__glTexGendv}, {"glTexGeni", gl__glTexGeni}, {"glTexCoordPointer", gl__glTexCoordPointer}, {"glFinish", gl__glFinish}, {"glRasterPos2d", gl__glRasterPos2d}, {"glVertex3f", gl__glVertex3f}, {"glIndexiv", gl__glIndexiv}, {"glStencilFunc", gl__glStencilFunc}, {"glEvalMesh1", gl__glEvalMesh1}, {"glDrawPixels", gl__glDrawPixels}, {"glReadPixels", gl__glReadPixels}, {"glBlendFunc", gl__glBlendFunc}, {"glIndexfv", gl__glIndexfv}, {"glGetPixelMapuiv", gl__glGetPixelMapuiv}, {"glColor4f", gl__glColor4f}, {"glIndexi", gl__glIndexi}, {"glRasterPos3fv", gl__glRasterPos3fv}, {"glPixelMapuiv", gl__glPixelMapuiv}, {"glStencilOp", gl__glStencilOp}, {"glPopAttrib", gl__glPopAttrib}, {"glBitmap", gl__glBitmap}, {"glPolygonStipple", gl__glPolygonStipple}, {"glVertex3dv", gl__glVertex3dv}, {"glTexCoord2f", gl__glTexCoord2f}, {"glColor3fv", gl__glColor3fv}, {"glCallList", gl__glCallList}, {"glColor3i", gl__glColor3i}, {"glVertex2s", gl__glVertex2s}, {"glTexParameteri", gl__glTexParameteri}, {"glMaterialiv", gl__glMaterialiv}, {"glColor4fv", gl__glColor4fv}, {"glTexEnviv", gl__glTexEnviv}, {"glRasterPos3iv", gl__glRasterPos3iv}, {"glIsEnabled", gl__glIsEnabled}, {"glLoadIdentity", gl__glLoadIdentity}, {"glTexCoord4sv", gl__glTexCoord4sv}, {"glMateriali", gl__glMateriali}, {"glLightModeliv", gl__glLightModeliv}, {"glTexCoord1fv", gl__glTexCoord1fv}, {"glVertex2dv", gl__glVertex2dv}, {"glTexCoord1d", gl__glTexCoord1d}, {"glGetLightiv", gl__glGetLightiv}, {"glGetLightfv", gl__glGetLightfv}, {"glLightiv", gl__glLightiv}, {"glEdgeFlagPointer", gl__glEdgeFlagPointer}, {"glAccum", gl__glAccum}, {"glLightfv", gl__glLightfv}, {"glLighti", gl__glLighti}, {"glColor4s", gl__glColor4s}, {"glGetDoublev", gl__glGetDoublev}, {"glDepthRange", gl__glDepthRange}, {"glPixelZoom", gl__glPixelZoom}, {"glRasterPos4i", gl__glRasterPos4i}, {"glMaterialfv", gl__glMaterialfv}, {"glVertex2i", gl__glVertex2i}, {"glTexSubImage1D", gl__glTexSubImage1D}, {"glPrioritizeTextures", gl__glPrioritizeTextures}, {"glTexCoord3i", gl__glTexCoord3i}, {"glClearStencil", gl__glClearStencil}, {"glViewport", gl__glViewport}, {"glNormal3fv", gl__glNormal3fv}, {"glGetPointerv", gl__glGetPointerv}, {"glClear", gl__glClear}, {"glColor4ubv", gl__glColor4ubv}, {"glTexCoord2iv", gl__glTexCoord2iv}, {"glColor3us", gl__glColor3us}, {"glColor4ub", gl__glColor4ub}, {"glTexCoord4dv", gl__glTexCoord4dv}, {"glGetPixelMapusv", gl__glGetPixelMapusv}, {"glColor4d", gl__glColor4d}, {"glRasterPos3d", gl__glRasterPos3d}, {"glNormal3b", gl__glNormal3b}, {"glNormalPointer", gl__glNormalPointer}, {"glColor4ui", gl__glColor4ui}, {"glMultMatrixd", gl__glMultMatrixd}, {"glColor3iv", gl__glColor3iv}, {"glIsList", gl__glIsList}, {"glVertex3sv", gl__glVertex3sv}, {"glRasterPos4sv", gl__glRasterPos4sv}, {"glVertexPointer", gl__glVertexPointer}, {"glPushClientAttrib", gl__glPushClientAttrib}, {"glRectd", gl__glRectd}, {"glPopClientAttrib", gl__glPopClientAttrib}, {"glRasterPos2iv", gl__glRasterPos2iv}, {"glGetClipPlane", gl__glGetClipPlane}, {"glRasterPos4s", gl__glRasterPos4s}, {"glVertex3iv", gl__glVertex3iv}, {"glScissor", gl__glScissor}, {"glColor3dv", gl__glColor3dv}, {"glRasterPos3s", gl__glRasterPos3s}, {"glRasterPos3f", gl__glRasterPos3f}, {"glTexCoord2s", gl__glTexCoord2s}, {"glClearIndex", gl__glClearIndex}, {"glRasterPos2f", gl__glRasterPos2f}, {"glLightf", gl__glLightf}, {"glVertex4f", gl__glVertex4f}, {"glTexCoord4fv", gl__glTexCoord4fv}, {"glTexCoord3iv", gl__glTexCoord3iv}, {"glNormal3s", gl__glNormal3s}, {"glPopMatrix", gl__glPopMatrix}, {"glTexCoord2i", gl__glTexCoord2i}, {"glVertex4i", gl__glVertex4i}, {"glFrustum", gl__glFrustum}, {"glFogfv", gl__glFogfv}, {"glTexCoord3d", gl__glTexCoord3d}, {"glTexCoord1f", gl__glTexCoord1f}, {"glColor3uiv", gl__glColor3uiv}, {"glIndexub", gl__glIndexub}, {"glAreTexturesResident", gl__glAreTexturesResident}, {"glIndexd", gl__glIndexd}, {"glDepthMask", gl__glDepthMask}, {"glCallLists", gl__glCallLists}, {"glRasterPos4iv", gl__glRasterPos4iv}, {NULL, NULL} }; int luaopen_gl(lua_State *L) { lua_newtable(L); struct constant_s *c = constants; while(c->name) { lua_pushnumber(L, c->value); lua_setfield(L, -2, c->name); c++; } luaL_register(L, NULL, functions); return 1; } dokidoki-support/glu.c000066400000000000000000000414721147057410300153320ustar00rootroot00000000000000#include "lua.h" #include "lualib.h" #include "lauxlib.h" #ifdef __APPLE__ #include "OpenGL/gl.h" #include "OpenGL/glu.h" #else #include "GL/gl.h" #include "GL/glu.h" #endif static void * checkpointer(lua_State *L, int num) { void *arg; if(lua_islightuserdata(L, num)) arg = lua_touserdata(L, num); else if(lua_isstring(L, num)) arg = (void *)lua_tostring(L, num); else luaL_argerror(L, num, "expected lightuserdata or string"); return arg; } struct constant_s {const char *name; int value;}; static struct constant_s constants [] = { {"GLU_TESS_COMBINE_DATA", 0x1870f}, {"GLU_TESS_VERTEX", 0x18705}, {"GLU_TESS_ERROR_DATA", 0x1870d}, {"GLU_NURBS_ERROR30", 0x187b8}, {"GLU_NURBS_BEGIN_EXT", 0x18744}, {"GLU_NURBS_VERTEX_DATA_EXT", 0x1874b}, {"GLU_NURBS_ERROR35", 0x187bd}, {"GLU_CCW", 0x18719}, {"GLU_INVALID_VALUE", 0x18a25}, {"GLU_VERTEX", 0x18705}, {"GLU_TESS_ERROR4", 0x1873a}, {"GLU_EXT_nurbs_tessellator", 0x1}, {"GLU_TESS_ERROR7", 0x1873d}, {"GLU_NURBS_ERROR27", 0x187b5}, {"GLU_TRUE", 0x1}, {"GLU_NURBS_COLOR", 0x18747}, {"GLU_V_STEP", 0x1876f}, {"GLU_NURBS_ERROR14", 0x187a8}, {"GLU_TESS_VERTEX_DATA", 0x1870b}, {"GLU_NURBS_ERROR5", 0x1879f}, {"GLU_TESS_WINDING_RULE", 0x1872c}, {"GLU_TESS_MISSING_BEGIN_POLYGON", 0x18737}, {"GLU_NURBS_ERROR29", 0x187b7}, {"GLU_TESS_TOLERANCE", 0x1872e}, {"GLU_NURBS_ERROR36", 0x187be}, {"GLU_FILL", 0x186ac}, {"GLU_NURBS_ERROR10", 0x187a4}, {"GLU_EXT_object_space_tess", 0x1}, {"GLU_NURBS_ERROR17", 0x187ab}, {"GLU_NURBS_VERTEX_EXT", 0x18745}, {"GLU_OBJECT_PARAMETRIC_ERROR_EXT", 0x18770}, {"GLU_TESS_WINDING_ODD", 0x18722}, {"GLU_VERSION_1_2", 0x1}, {"GLU_NURBS_ERROR16", 0x187aa}, {"GLU_NURBS_ERROR8", 0x187a2}, {"GLU_NURBS_ERROR23", 0x187b1}, {"GLU_NURBS_ERROR24", 0x187b2}, {"GLU_OBJECT_PARAMETRIC_ERROR", 0x18770}, {"GLU_OUTLINE_POLYGON", 0x18790}, {"GLU_NURBS_NORMAL_EXT", 0x18746}, {"GLU_NURBS_ERROR19", 0x187ad}, {"GLU_NURBS_ERROR", 0x18707}, {"GLU_NURBS_ERROR11", 0x187a5}, {"GLU_OUTSIDE", 0x186b4}, {"GLU_NURBS_COLOR_DATA", 0x1874d}, {"GLU_NURBS_ERROR13", 0x187a7}, {"GLU_NURBS_END_EXT", 0x18749}, {"GLU_DISPLAY_MODE", 0x1876c}, {"GLU_NONE", 0x186a2}, {"GLU_NURBS_TEXTURE_COORD_DATA", 0x1874e}, {"GLU_NURBS_ERROR22", 0x187b0}, {"GLU_TESS_ERROR2", 0x18738}, {"GLU_NURBS_ERROR20", 0x187ae}, {"GLU_NURBS_ERROR21", 0x187af}, {"GLU_NURBS_ERROR1", 0x1879b}, {"GLU_NURBS_RENDERER", 0x18742}, {"GLU_NURBS_BEGIN_DATA", 0x1874a}, {"GLU_PARAMETRIC_TOLERANCE", 0x1876a}, {"GLU_INCOMPATIBLE_GL_VERSION", 0x18a27}, {"GLU_NURBS_ERROR4", 0x1879e}, {"GLU_TESS_ERROR", 0x18707}, {"GLU_TESS_BOUNDARY_ONLY", 0x1872d}, {"GLU_FALSE", 0x0}, {"GLU_NURBS_ERROR28", 0x187b6}, {"GLU_TESS_END", 0x18706}, {"GLU_VERSION_1_1", 0x1}, {"GLU_UNKNOWN", 0x1871c}, {"GLU_LINE", 0x186ab}, {"GLU_TESS_MISSING_END_POLYGON", 0x18739}, {"GLU_SAMPLING_METHOD", 0x1876d}, {"GLU_INVALID_ENUM", 0x18a24}, {"GLU_NURBS_VERTEX_DATA", 0x1874b}, {"GLU_NURBS_ERROR3", 0x1879d}, {"GLU_NURBS_TEX_COORD_DATA_EXT", 0x1874e}, {"GLU_NURBS_ERROR6", 0x187a0}, {"GLU_NURBS_ERROR15", 0x187a9}, {"GLU_NURBS_END", 0x18749}, {"GLU_EXTENSIONS", 0x189c1}, {"GLU_NURBS_ERROR33", 0x187bb}, {"GLU_DOMAIN_DISTANCE", 0x18779}, {"GLU_NURBS_END_DATA_EXT", 0x1874f}, {"GLU_TESS_BEGIN", 0x18704}, {"GLU_NURBS_COLOR_EXT", 0x18747}, {"GLU_NURBS_TESSELLATOR", 0x18741}, {"GLU_NURBS_ERROR31", 0x187b9}, {"GLU_PATH_LENGTH", 0x18777}, {"GLU_OBJECT_PATH_LENGTH_EXT", 0x18771}, {"GLU_OBJECT_PATH_LENGTH", 0x18771}, {"GLU_SILHOUETTE", 0x186ad}, {"GLU_NURBS_NORMAL", 0x18746}, {"GLU_PARAMETRIC_ERROR", 0x18778}, {"GLU_NURBS_TEXTURE_COORD", 0x18748}, {"GLU_U_STEP", 0x1876e}, {"GLU_TESS_WINDING_NEGATIVE", 0x18725}, {"GLU_NURBS_ERROR32", 0x187ba}, {"GLU_NURBS_ERROR25", 0x187b3}, {"GLU_TESS_ERROR3", 0x18739}, {"GLU_TESS_WINDING_POSITIVE", 0x18724}, {"GLU_TESS_WINDING_NONZERO", 0x18723}, {"GLU_NURBS_TEX_COORD_EXT", 0x18748}, {"GLU_TESS_COORD_TOO_LARGE", 0x1873b}, {"GLU_TESS_MISSING_END_CONTOUR", 0x1873a}, {"GLU_NURBS_ERROR26", 0x187b4}, {"GLU_TESS_ERROR8", 0x1873e}, {"GLU_NURBS_TESSELLATOR_EXT", 0x18741}, {"GLU_TESS_ERROR6", 0x1873c}, {"GLU_BEGIN", 0x18704}, {"GLU_SMOOTH", 0x186a0}, {"GLU_TESS_ERROR5", 0x1873b}, {"GLU_TESS_ERROR1", 0x18737}, {"GLU_EXTERIOR", 0x1871b}, {"GLU_TESS_EDGE_FLAG_DATA", 0x1870e}, {"GLU_INTERIOR", 0x1871a}, {"GLU_CW", 0x18718}, {"GLU_NURBS_COLOR_DATA_EXT", 0x1874d}, {"GLU_MAP1_TRIM_2", 0x18772}, {"GLU_TESS_BEGIN_DATA", 0x1870a}, {"GLU_TESS_END_DATA", 0x1870c}, {"GLU_TESS_COMBINE", 0x18709}, {"GLU_NURBS_NORMAL_DATA", 0x1874c}, {"GLU_EDGE_FLAG", 0x18708}, {"GLU_INVALID_OPERATION", 0x18a28}, {"GLU_NURBS_ERROR9", 0x187a3}, {"GLU_CULLING", 0x18769}, {"GLU_NURBS_ERROR37", 0x187bf}, {"GLU_NURBS_ERROR7", 0x187a1}, {"GLU_NURBS_MODE", 0x18740}, {"GLU_END", 0x18706}, {"GLU_NURBS_ERROR12", 0x187a6}, {"GLU_ERROR", 0x18707}, {"GLU_OUTLINE_PATCH", 0x18791}, {"GLU_TESS_EDGE_FLAG", 0x18708}, {"GLU_INSIDE", 0x186b5}, {"GLU_TESS_MISSING_BEGIN_CONTOUR", 0x18738}, {"GLU_FLAT", 0x186a1}, {"GLU_NURBS_VERTEX", 0x18745}, {"GLU_VERSION_1_3", 0x1}, {"GLU_NURBS_BEGIN_DATA_EXT", 0x1874a}, {"GLU_NURBS_END_DATA", 0x1874f}, {"GLU_TESS_NEED_COMBINE_CALLBACK", 0x1873c}, {"GLU_POINT", 0x186aa}, {"GLU_MAP1_TRIM_3", 0x18773}, {"GLU_SAMPLING_TOLERANCE", 0x1876b}, {"GLU_NURBS_RENDERER_EXT", 0x18742}, {"GLU_NURBS_MODE_EXT", 0x18740}, {"GLU_NURBS_ERROR18", 0x187ac}, {"GLU_VERSION", 0x189c0}, {"GLU_NURBS_ERROR2", 0x1879c}, {"GLU_NURBS_BEGIN", 0x18744}, {"GLU_AUTO_LOAD_MATRIX", 0x18768}, {"GLU_NURBS_ERROR34", 0x187bc}, {"GLU_OUT_OF_MEMORY", 0x18a26}, {"GLU_TESS_WINDING_ABS_GEQ_TWO", 0x18726}, {"GLU_NURBS_NORMAL_DATA_EXT", 0x1874c}, {NULL, 0} }; static int glu__gluBuild1DMipmaps(lua_State *L) { lua_pushnumber(L, gluBuild1DMipmaps( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), checkpointer(L, 6))); return 1; } static int glu__gluQuadricDrawStyle(lua_State *L) { gluQuadricDrawStyle( checkpointer(L, 1), luaL_checknumber(L, 2)); return 0; } static int glu__gluScaleImage(lua_State *L) { lua_pushnumber(L, gluScaleImage( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), checkpointer(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), luaL_checknumber(L, 8), checkpointer(L, 9))); return 1; } static int glu__gluErrorString(lua_State *L) { lua_pushstring(L, gluErrorString( luaL_checknumber(L, 1))); return 1; } static int glu__gluNewNurbsRenderer(lua_State *L) { lua_pushlightuserdata(L, gluNewNurbsRenderer()); return 1; } static int glu__gluBeginTrim(lua_State *L) { gluBeginTrim( checkpointer(L, 1)); return 0; } static int glu__gluTessVertex(lua_State *L) { gluTessVertex( checkpointer(L, 1), checkpointer(L, 2), checkpointer(L, 3)); return 0; } static int glu__gluNurbsCallback(lua_State *L) { gluNurbsCallback( checkpointer(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int glu__gluGetTessProperty(lua_State *L) { gluGetTessProperty( checkpointer(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int glu__gluEndPolygon(lua_State *L) { gluEndPolygon( checkpointer(L, 1)); return 0; } static int glu__gluEndSurface(lua_State *L) { gluEndSurface( checkpointer(L, 1)); return 0; } static int glu__gluPartialDisk(lua_State *L) { gluPartialDisk( checkpointer(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7)); return 0; } static int glu__gluTessCallback(lua_State *L) { gluTessCallback( checkpointer(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int glu__gluNextContour(lua_State *L) { gluNextContour( checkpointer(L, 1), luaL_checknumber(L, 2)); return 0; } static int glu__gluDeleteNurbsRenderer(lua_State *L) { gluDeleteNurbsRenderer( checkpointer(L, 1)); return 0; } static int glu__gluQuadricNormals(lua_State *L) { gluQuadricNormals( checkpointer(L, 1), luaL_checknumber(L, 2)); return 0; } static int glu__gluBuild2DMipmaps(lua_State *L) { lua_pushnumber(L, gluBuild2DMipmaps( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), checkpointer(L, 7))); return 1; } static int glu__gluEndCurve(lua_State *L) { gluEndCurve( checkpointer(L, 1)); return 0; } static int glu__gluLookAt(lua_State *L) { gluLookAt( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), luaL_checknumber(L, 8), luaL_checknumber(L, 9)); return 0; } static int glu__gluTessBeginContour(lua_State *L) { gluTessBeginContour( checkpointer(L, 1)); return 0; } static int glu__gluBeginSurface(lua_State *L) { gluBeginSurface( checkpointer(L, 1)); return 0; } static int glu__gluQuadricTexture(lua_State *L) { gluQuadricTexture( checkpointer(L, 1), lua_toboolean(L, 2)); return 0; } static int glu__gluNurbsCurve(lua_State *L) { gluNurbsCurve( checkpointer(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3), luaL_checknumber(L, 4), checkpointer(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7)); return 0; } static int glu__gluBeginPolygon(lua_State *L) { gluBeginPolygon( checkpointer(L, 1)); return 0; } static int glu__gluTessProperty(lua_State *L) { gluTessProperty( checkpointer(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int glu__gluNewTess(lua_State *L) { lua_pushlightuserdata(L, gluNewTess()); return 1; } static int glu__gluTessNormal(lua_State *L) { gluTessNormal( checkpointer(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int glu__gluPickMatrix(lua_State *L) { gluPickMatrix( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), checkpointer(L, 5)); return 0; } static int glu__gluPerspective(lua_State *L) { gluPerspective( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int glu__gluEndTrim(lua_State *L) { gluEndTrim( checkpointer(L, 1)); return 0; } static int glu__gluPwlCurve(lua_State *L) { gluPwlCurve( checkpointer(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5)); return 0; } static int glu__gluTessEndPolygon(lua_State *L) { gluTessEndPolygon( checkpointer(L, 1)); return 0; } static int glu__gluTessEndContour(lua_State *L) { gluTessEndContour( checkpointer(L, 1)); return 0; } static int glu__gluNewQuadric(lua_State *L) { lua_pushlightuserdata(L, gluNewQuadric()); return 1; } static int glu__gluTessBeginPolygon(lua_State *L) { gluTessBeginPolygon( checkpointer(L, 1), checkpointer(L, 2)); return 0; } static int glu__gluSphere(lua_State *L) { gluSphere( checkpointer(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int glu__gluQuadricOrientation(lua_State *L) { gluQuadricOrientation( checkpointer(L, 1), luaL_checknumber(L, 2)); return 0; } static int glu__gluLoadSamplingMatrices(lua_State *L) { gluLoadSamplingMatrices( checkpointer(L, 1), checkpointer(L, 2), checkpointer(L, 3), checkpointer(L, 4)); return 0; } static int glu__gluBeginCurve(lua_State *L) { gluBeginCurve( checkpointer(L, 1)); return 0; } static int glu__gluProject(lua_State *L) { lua_pushnumber(L, gluProject( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), checkpointer(L, 4), checkpointer(L, 5), checkpointer(L, 6), checkpointer(L, 7), checkpointer(L, 8), checkpointer(L, 9))); return 1; } static int glu__gluUnProject(lua_State *L) { lua_pushnumber(L, gluUnProject( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), checkpointer(L, 4), checkpointer(L, 5), checkpointer(L, 6), checkpointer(L, 7), checkpointer(L, 8), checkpointer(L, 9))); return 1; } static int glu__gluOrtho2D(lua_State *L) { gluOrtho2D( luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); return 0; } static int glu__gluDeleteQuadric(lua_State *L) { gluDeleteQuadric( checkpointer(L, 1)); return 0; } static int glu__gluNurbsSurface(lua_State *L) { gluNurbsSurface( checkpointer(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3), luaL_checknumber(L, 4), checkpointer(L, 5), luaL_checknumber(L, 6), luaL_checknumber(L, 7), checkpointer(L, 8), luaL_checknumber(L, 9), luaL_checknumber(L, 10), luaL_checknumber(L, 11)); return 0; } static int glu__gluNurbsProperty(lua_State *L) { gluNurbsProperty( checkpointer(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3)); return 0; } static int glu__gluGetString(lua_State *L) { lua_pushstring(L, gluGetString( luaL_checknumber(L, 1))); return 1; } static int glu__gluDeleteTess(lua_State *L) { gluDeleteTess( checkpointer(L, 1)); return 0; } static int glu__gluDisk(lua_State *L) { gluDisk( checkpointer(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5)); return 0; } static int glu__gluCylinder(lua_State *L) { gluCylinder( checkpointer(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4), luaL_checknumber(L, 5), luaL_checknumber(L, 6)); return 0; } static int glu__gluQuadricCallback(lua_State *L) { gluQuadricCallback( checkpointer(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static int glu__gluGetNurbsProperty(lua_State *L) { gluGetNurbsProperty( checkpointer(L, 1), luaL_checknumber(L, 2), checkpointer(L, 3)); return 0; } static const luaL_Reg functions [] = { {"gluBuild1DMipmaps", glu__gluBuild1DMipmaps}, {"gluQuadricDrawStyle", glu__gluQuadricDrawStyle}, {"gluScaleImage", glu__gluScaleImage}, {"gluErrorString", glu__gluErrorString}, {"gluNewNurbsRenderer", glu__gluNewNurbsRenderer}, {"gluBeginTrim", glu__gluBeginTrim}, {"gluTessVertex", glu__gluTessVertex}, {"gluNurbsCallback", glu__gluNurbsCallback}, {"gluGetTessProperty", glu__gluGetTessProperty}, {"gluEndPolygon", glu__gluEndPolygon}, {"gluEndSurface", glu__gluEndSurface}, {"gluPartialDisk", glu__gluPartialDisk}, {"gluTessCallback", glu__gluTessCallback}, {"gluNextContour", glu__gluNextContour}, {"gluDeleteNurbsRenderer", glu__gluDeleteNurbsRenderer}, {"gluQuadricNormals", glu__gluQuadricNormals}, {"gluBuild2DMipmaps", glu__gluBuild2DMipmaps}, {"gluEndCurve", glu__gluEndCurve}, {"gluLookAt", glu__gluLookAt}, {"gluTessBeginContour", glu__gluTessBeginContour}, {"gluBeginSurface", glu__gluBeginSurface}, {"gluQuadricTexture", glu__gluQuadricTexture}, {"gluNurbsCurve", glu__gluNurbsCurve}, {"gluBeginPolygon", glu__gluBeginPolygon}, {"gluTessProperty", glu__gluTessProperty}, {"gluNewTess", glu__gluNewTess}, {"gluTessNormal", glu__gluTessNormal}, {"gluPickMatrix", glu__gluPickMatrix}, {"gluPerspective", glu__gluPerspective}, {"gluEndTrim", glu__gluEndTrim}, {"gluPwlCurve", glu__gluPwlCurve}, {"gluTessEndPolygon", glu__gluTessEndPolygon}, {"gluTessEndContour", glu__gluTessEndContour}, {"gluNewQuadric", glu__gluNewQuadric}, {"gluTessBeginPolygon", glu__gluTessBeginPolygon}, {"gluSphere", glu__gluSphere}, {"gluQuadricOrientation", glu__gluQuadricOrientation}, {"gluLoadSamplingMatrices", glu__gluLoadSamplingMatrices}, {"gluBeginCurve", glu__gluBeginCurve}, {"gluProject", glu__gluProject}, {"gluUnProject", glu__gluUnProject}, {"gluOrtho2D", glu__gluOrtho2D}, {"gluDeleteQuadric", glu__gluDeleteQuadric}, {"gluNurbsSurface", glu__gluNurbsSurface}, {"gluNurbsProperty", glu__gluNurbsProperty}, {"gluGetString", glu__gluGetString}, {"gluDeleteTess", glu__gluDeleteTess}, {"gluDisk", glu__gluDisk}, {"gluCylinder", glu__gluCylinder}, {"gluQuadricCallback", glu__gluQuadricCallback}, {"gluGetNurbsProperty", glu__gluGetNurbsProperty}, {NULL, NULL} }; int luaopen_glu(lua_State *L) { lua_newtable(L); struct constant_s *c = constants; while(c->name) { lua_pushnumber(L, c->value); lua_setfield(L, -2, c->name); c++; } luaL_register(L, NULL, functions); return 1; } dokidoki-support/init.lua000066400000000000000000000016111147057410300160340ustar00rootroot00000000000000require "dokidoki.module" [[]] require "glfw" import(require "gl") kernel = require "dokidoki.kernel" function make_uber_scene (coefficient_of_awesomeness) local time_until_change = 0 local color = {0, 0, 0, 0} local function handle_event (event) if event.type == 'quit' or event.type == 'key' and event.is_down and event.key == glfw.KEY_ESC then kernel.abort_main_loop() end end local function update (dt) time_until_change = time_until_change - dt if time_until_change <= 0 then time_until_change = time_until_change + 1 / coefficient_of_awesomeness color = {math.random(), math.random(), math.random(), 0} end end local function draw () glClearColor(unpack(color)) glClear(GL_COLOR_BUFFER_BIT) end return {handle_event = handle_event, update = update, draw = draw} end kernel.start_main_loop(make_uber_scene(math.acos(-1))) dokidoki-support/log.c000066400000000000000000000025521147057410300153200ustar00rootroot00000000000000#include "log.h" #include #include #include #include #include FILE *file = NULL; #if defined(DOKIDOKI_LINUX) || defined(DOKIDOKI_MACOSX) static void open_logfile() { const char *suffix = "/.dokidoki.log"; const char *home = getenv("HOME"); size_t len = strlen(suffix) + strlen(home) + 1; char *path = (char *)malloc(len * sizeof(char)); strcpy(path, home); strcat(path, suffix); file = fopen(path, "w"); free(path); } #else static void open_logfile() { file = fopen("dokidoki.log", "w"); } #endif void log_init(int use_logfile) { if(use_logfile) open_logfile(); if(!file) file = stderr; } void log_message(const char *message) { fputs(message, file); fputs("\n", file); fflush(file); } void log_messagef(const char *fmt, ...) { va_list args; va_start(args, fmt); vfprintf(file, fmt, args); fputs("\n", file); fflush(file); va_end(args); } //// lua binding ////////////////////////////////////////////////////////////// static int log__log_message(lua_State *L) { log_message(luaL_checkstring(L, 1)); return 0; } static const luaL_Reg log_lib[] = { {"log_message", log__log_message}, {NULL, NULL} }; int luaopen_log(lua_State *L) { lua_newtable(L); luaL_register(L, NULL, log_lib); return 1; } dokidoki-support/log.h000066400000000000000000000003071147057410300153210ustar00rootroot00000000000000#ifndef LOG_H #define LOG_H #include void log_init(int use_logfile); void log_message(const char *message); void log_messagef(const char *fmt, ...); int luaopen_log(lua_State *L); #endif dokidoki-support/lua_stb_image.c000066400000000000000000000034231147057410300173300ustar00rootroot00000000000000#include #include #include #define STBI_NO_WRITE #define STBI_NO_HDR #include "stb_image.c" static int check_req_comp(lua_State *L, int index) { int req_comp = luaL_optint(L, 2, 0); luaL_argcheck(L, 0 <= req_comp && req_comp <= 4, 2, "invalid requested component count"); return req_comp; } static int return_image(lua_State *L, unsigned char * image, int x, int y, int comp, int req_comp) { if(req_comp == 0) req_comp = comp; if(image) { lua_pushlstring(L, (char *)image, x * y * req_comp); lua_pushinteger(L, x); lua_pushinteger(L, y); lua_pushinteger(L, comp); stbi_image_free(image); return 4; } else { lua_pushnil(L); lua_pushstring(L, stbi_failure_reason()); return 2; } } static int stb_image_load(lua_State *L) { const char * filename = luaL_checkstring(L, 1); int req_comp = check_req_comp(L, 2); int x, y, comp; unsigned char * image = stbi_load(filename, &x, &y, &comp, req_comp); return return_image(L, image, x, y, comp, req_comp); } static int stb_image_load_from_string(lua_State *L) { size_t source_len; const char * source = luaL_checklstring(L, 1, &source_len); int req_comp = check_req_comp(L, 2); int x, y, comp; unsigned char * image = stbi_load_from_memory( (unsigned char *)source, source_len, &x, &y, &comp, req_comp); return return_image(L, image, x, y, comp, req_comp); } static const luaL_Reg stb_image_lib[] = { {"load", stb_image_load}, {"load_from_string", stb_image_load_from_string}, {NULL, NULL} }; int luaopen_stb_image(lua_State *L) { lua_newtable(L); luaL_register(L, NULL, stb_image_lib); return 1; } dokidoki-support/luaglfw.c000066400000000000000000000767161147057410300162150ustar00rootroot00000000000000//======================================================================== // GLFW - An OpenGL framework // File: luaglfw.c // Platform: Lua 5.0 // API version: 2.5 // WWW: http://glfw.sourceforge.net //------------------------------------------------------------------------ // Copyright (c) 2002-2005 Camilla Berglund // // This software is provided 'as-is', without any express or implied // warranty. In no event will the authors be held liable for any damages // arising from the use of this software. // // Permission is granted to anyone to use this software for any purpose, // including commercial applications, and to alter it and redistribute it // freely, subject to the following restrictions: // // 1. The origin of this software must not be misrepresented; you must not // claim that you wrote the original software. If you use this software // in a product, an acknowledgment in the product documentation would // be appreciated but is not required. // // 2. Altered source versions must be plainly marked as such, and must not // be misrepresented as being the original software. // // 3. This notice may not be removed or altered from any source // distribution. // //======================================================================== #include #include #include "luaglfw.h" //************************************************************************ //**** Internal type definitions **** //************************************************************************ struct lua_constant { char *str; int value; }; //************************************************************************ //**** Handy functions **** //************************************************************************ // Check the number of arguments on the Lua stack static int badArgs( lua_State *L, int n, char const *name ) { if( lua_gettop(L) != n ) { lua_settop( L, 0 ); lua_pushstring( L, "Bad arguments passed to function: " ); lua_pushstring( L, name ); lua_concat( L, 2 ); lua_error( L ); return 1; } return 0; } // Get a numeric value from a Lua table static void getNumber( lua_State *L, int index, char *key, lua_Number *num ) { lua_pushstring( L, key ); if( index < 0 ) index --; lua_rawget( L, index ); if( !lua_isnil( L, -1 ) ) { *num = lua_tonumber( L, -1 ); } lua_remove( L, -1 ); } // Add constants to the table on top of the stack static void addConstants( lua_State *L, struct lua_constant *cn ) { while( cn->str ) { lua_pushstring( L, cn->str ); lua_pushnumber( L, cn->value ); lua_rawset( L, -3 ); ++ cn; } } // Report error for unsupported functions static void unsupportedFunction( lua_State *L, char const *name ) { lua_settop( L, 0 ); lua_pushstring( L, "Unsupported function: " ); lua_pushstring( L, name ); lua_concat( L, 2 ); lua_error( L ); } static void pushVideoMode( lua_State *L, const GLFWvidmode *mode) { lua_newtable( L ); lua_pushstring( L, "Width" ); lua_pushnumber( L, mode->Width ); lua_rawset( L, -3 ); lua_pushstring( L, "Height" ); lua_pushnumber( L, mode->Height ); lua_rawset( L, -3 ); lua_pushstring( L, "RedBits" ); lua_pushnumber( L, mode->RedBits ); lua_rawset( L, -3 ); lua_pushstring( L, "GreenBits" ); lua_pushnumber( L, mode->GreenBits ); lua_rawset( L, -3 ); lua_pushstring( L, "BlueBits" ); lua_pushnumber( L, mode->BlueBits ); lua_rawset( L, -3 ); } //************************************************************************ //*** Callback wrapper functions **** //************************************************************************ static lua_State * callback_lua_state = (lua_State *) 0; static const char * windowsize_name; static const char * windowclose_name; static const char * windowrefresh_name; static const char * mousebutton_name; static const char * mousepos_name; static const char * mousewheel_name; static const char * key_name; static const char * char_name; void GLFWCALL luaglfw_windowsizefun( int w, int h ) { lua_State *L = callback_lua_state; if( L == NULL ) return; lua_getglobal( L, windowsize_name ); if( lua_isfunction( L, -1 ) ) { lua_pushnumber( L, (lua_Number)w ); lua_pushnumber( L, (lua_Number)h ); lua_pcall( L, 2, 0, 0 ); } } int GLFWCALL luaglfw_windowclosefun( void ) { lua_State *L = callback_lua_state; int do_close = 1; if( L == NULL ) return 1; lua_getglobal( L, windowclose_name ); if( lua_isfunction( L, -1 ) ) { lua_pcall( L, 0, 1, 0 ); do_close = (int) lua_tonumber( L, -1 ); lua_pop( L, 1 ); } return do_close; } void GLFWCALL luaglfw_windowrefreshfun( void ) { lua_State *L = callback_lua_state; if( L == NULL ) return; lua_getglobal( L, windowrefresh_name ); if( lua_isfunction( L, -1 ) ) { lua_pcall( L, 0, 0, 0 ); } } void GLFWCALL luaglfw_mousebuttonfun( int button, int action ) { lua_State *L = callback_lua_state; if( L == NULL ) return; lua_getglobal( L, mousebutton_name ); if( lua_isfunction( L, -1 ) ) { lua_pushnumber( L, (lua_Number)button ); lua_pushnumber( L, (lua_Number)action ); lua_pcall( L, 2, 0, 0 ); } } void GLFWCALL luaglfw_mouseposfun( int x, int y ) { lua_State *L = callback_lua_state; if( L == NULL ) return; lua_getglobal( L, mousepos_name ); if( lua_isfunction( L, -1 ) ) { lua_pushnumber( L, (lua_Number)x ); lua_pushnumber( L, (lua_Number)y ); lua_pcall( L, 2, 0, 0 ); } } void GLFWCALL luaglfw_mousewheelfun( int wheelpos ) { lua_State *L = callback_lua_state; if( L == NULL ) return; lua_getglobal( L, mousewheel_name ); if( lua_isfunction( L, -1 ) ) { lua_pushnumber( L, (lua_Number)wheelpos ); lua_pcall( L, 1, 0, 0 ); } } void GLFWCALL luaglfw_keyfun( int key, int action ) { lua_State *L = callback_lua_state; if( L == NULL ) return; lua_getglobal( L, key_name ); if( lua_isfunction( L, -1 ) ) { lua_pushnumber( L, (lua_Number)key ); lua_pushnumber( L, (lua_Number)action ); lua_pcall( L, 2, 0, 0 ); } } void GLFWCALL luaglfw_charfun( int key, int action ) { lua_State *L = callback_lua_state; if( L == NULL ) return; lua_getglobal( L, char_name ); if( lua_isfunction( L, -1 ) ) { lua_pushnumber( L, (lua_Number)key ); lua_pushnumber( L, (lua_Number)action ); lua_pcall( L, 2, 0, 0 ); } } //************************************************************************ //**** GLFW proxy functions **** //************************************************************************ //======================================================================== // Init/termination & version info //======================================================================== static int glfw_Init( lua_State *L ) { int res; lua_settop( L, 0 ); res = glfwInit(); lua_pushnumber( L, (lua_Number)res ); return 1; } static int glfw_Terminate( lua_State *L ) { glfwTerminate(); return 0; } static int glfw_GetVersion( lua_State *L ) { int major, minor, rev; glfwGetVersion( &major, &minor, &rev ); lua_pushnumber( L, major ); lua_pushnumber( L, minor ); lua_pushnumber( L, rev ); return 3; } //======================================================================== // Window handling //======================================================================== static int glfw_OpenWindow( lua_State *L ) { lua_Number w, h, r, g, b, a, depth, stencil, mode; int argc, res; // Check arguments if( badArgs( L, 9, "OpenWindow" ) ) return 0; // Get all arguments to glfwOpenWindow w = lua_tonumber( L, 1 ); h = lua_tonumber( L, 2 ); r = lua_tonumber( L, 3 ); g = lua_tonumber( L, 4 ); b = lua_tonumber( L, 5 ); a = lua_tonumber( L, 6 ); depth = lua_tonumber( L, 7 ); stencil = lua_tonumber( L, 8 ); mode = lua_tonumber( L, 9 ); // Call glfwOpenWindow lua_settop( L,0 ); res = glfwOpenWindow( (int)w, (int)h, (int)r, (int)g, (int)b, (int)a, (int)depth, (int)stencil, (int)mode ); // Return result lua_pushnumber( L, (lua_Number)res ); return 1; } static int glfw_OpenWindowHint( lua_State *L ) { lua_Number target, hint; if( badArgs( L, 2, "OpenWindowHint" ) ) return 0; target = lua_tonumber( L, 1 ); hint = lua_tonumber( L, 2 ); lua_settop( L, 0 ); glfwOpenWindowHint( (int)target, (int)hint ); return 0; } static int glfw_CloseWindow( lua_State *L ) { glfwCloseWindow(); return 0; } static int glfw_SetWindowTitle( lua_State *L ) { const char *str; if( badArgs( L, 1, "SetWindowTitle" ) ) return 0; str = lua_tostring( L, 1 ); lua_remove( L, 1 ); glfwSetWindowTitle( str ); return 0; } static int glfw_SetWindowSize( lua_State *L ) { lua_Number w, h; if( badArgs( L, 2, "SetWindowSize" ) ) return 0; w = lua_tonumber( L, 1 ); h = lua_tonumber( L, 2 ); lua_settop( L, 0 ); glfwSetWindowSize( (int)w, (int)h ); return 0; } static int glfw_GetWindowSize( lua_State *L ) { int w, h; glfwGetWindowSize( &w, &h ); lua_settop( L, 0 ); lua_pushnumber( L, (lua_Number)w ); lua_pushnumber( L, (lua_Number)h ); return 2; } static int glfw_SetWindowPos( lua_State *L ) { lua_Number x, y; if( badArgs( L, 2, "SetWindowPos" ) ) return 0; x = lua_tonumber( L, 1 ); y = lua_tonumber( L, 2 ); lua_settop( L, 0 ); glfwSetWindowPos( (int)x, (int)y ); return 0; } static int glfw_IconifyWindow( lua_State *L ) { glfwIconifyWindow(); return 0; } static int glfw_RestoreWindow( lua_State *L ) { glfwRestoreWindow(); return 0; } static int glfw_GetWindowParam( lua_State *L ) { lua_Number n; if( badArgs( L, 1, "GetWindowParam" ) ) return 0; n = lua_tonumber( L, 1 ); lua_settop( L, 0 ); n = glfwGetWindowParam( (int)n ); lua_pushnumber( L, n ); return 1; } //======================================================================== // Buffer swapping //======================================================================== static int glfw_SwapBuffers( lua_State *L ) { glfwSwapBuffers(); return 0; } static int glfw_SwapInterval( lua_State *L ) { lua_Number interval; if( badArgs( L, 1, "SwapInterval" ) ) return 0; interval = lua_tonumber( L, 1 ); lua_remove( L, 1 ); glfwSwapInterval( (int)interval ); return 0; } //======================================================================== // Video modes //======================================================================== #define LUAGLFW_MAX_NUM_MODES 256 static int glfw_GetVideoModes( lua_State *L ) { GLFWvidmode modes[ LUAGLFW_MAX_NUM_MODES ]; int modecount, i; modecount = glfwGetVideoModes( modes, LUAGLFW_MAX_NUM_MODES ); lua_newtable( L ); for( i = 0; i < modecount; i++ ) { pushVideoMode( L, &modes[i] ); lua_rawseti( L, -2, i + 1 ); } return 1; } static int glfw_GetDesktopMode( lua_State *L ) { GLFWvidmode mode; glfwGetDesktopMode( &mode ); pushVideoMode( L, &mode ); return 1; } //======================================================================== // Event polling/waiting //======================================================================== static int glfw_PollEvents( lua_State *L ) { glfwPollEvents(); return 0; } static int glfw_WaitEvents( lua_State *L ) { glfwWaitEvents(); return 0; } //======================================================================== // Key and mouse input //======================================================================== static int glfw_GetKey( lua_State *L ) { int n = 0; if( badArgs( L, 1, "GetKey" ) ) return 0; switch( lua_type( L, 1 ) ) { case LUA_TSTRING: n = (lua_tostring(L,1)[0]); break; case LUA_TNUMBER: n = lua_tonumber(L,1); break; } lua_settop( L, 0 ); n = glfwGetKey( n ); lua_pushnumber( L, n ); return 1; } static int glfw_GetMouseButton( lua_State *L ) { lua_Number n; if( badArgs( L, 1, "GetMouseButton" ) ) return 0; n = lua_tonumber( L, 1 ); lua_settop( L, 0 ); lua_pushnumber( L, glfwGetMouseButton( (int)n ) ); return 1; } static int glfw_GetMousePos( lua_State *L ) { int x, y; glfwGetMousePos( &x, &y ); lua_settop( L, 0 ); lua_pushnumber( L, (lua_Number)x ); lua_pushnumber( L, (lua_Number)y ); return 2; } static int glfw_SetMousePos( lua_State *L ) { lua_Number x, y; if( badArgs( L, 2, "SetMousePos" ) ) return 0; x = lua_tonumber( L, 1 ); y = lua_tonumber( L, 2 ); lua_settop( L, 0 ); glfwSetMousePos( (int)x, (int)y ); return 0; } static int glfw_GetMouseWheel( lua_State *L ) { int pos; pos = glfwGetMouseWheel(); lua_settop( L, 0 ); lua_pushnumber( L, (lua_Number)pos ); return 1; } static int glfw_SetMouseWheel( lua_State *L ) { lua_Number pos; if( badArgs( L, 1, "SetMouseWheel" ) ) return 0; pos = lua_tonumber( L, 1 ); lua_settop( L, 0 ); glfwSetMouseWheel( (int)pos ); return 0; } //======================================================================== // Joystick input //======================================================================== static int glfw_GetJoystickParam( lua_State *L ) { lua_Number joy, param; int res; if( badArgs( L, 2, "GetJoystickParam" ) ) return 0; joy = lua_tonumber( L, 1 ); param = lua_tonumber( L, 2 ); lua_settop( L, 0 ); res = glfwGetJoystickParam( (int)joy, (int)param ); lua_pushnumber( L, (lua_Number)res ); return 1; } #define LUAGLFW_MAX_JOY_AXES 256 static int glfw_GetJoystickPos( lua_State *L ) { int joy, numaxes, res, i; float pos[ LUAGLFW_MAX_JOY_AXES ]; // Get arguments if( badArgs( L, 2, "GetJoystickPos" ) ) return 0; joy = (int)lua_tonumber( L, 1 ); numaxes = (int)lua_tonumber( L, 2 ); lua_settop( L, 0 ); if( numaxes < 1 ) return 0; if( numaxes > LUAGLFW_MAX_JOY_AXES ) numaxes = LUAGLFW_MAX_JOY_AXES; // Call GLFW funciton res = glfwGetJoystickPos( joy, pos, numaxes ); // Create result array lua_newtable( L ); for( i = 0; i < res; ++ i ) { lua_pushnumber( L, (lua_Number)(i+1) ); lua_pushnumber( L, pos[ i ] ); lua_rawset( L, -3 ); } return 1; } #define LUAGLFW_MAX_JOY_BUTTONS 256 static int glfw_GetJoystickButtons( lua_State *L ) { int joy, numbuttons, res, i; unsigned char buttons[ LUAGLFW_MAX_JOY_AXES ]; // Get arguments if( badArgs( L, 2, "GetJoystickButtons" ) ) return 0; joy = (int)lua_tonumber( L, 1 ); numbuttons = (int)lua_tonumber( L, 2 ); lua_settop( L, 0 ); if( numbuttons < 1 ) return 0; if( numbuttons > LUAGLFW_MAX_JOY_BUTTONS ) numbuttons = LUAGLFW_MAX_JOY_BUTTONS; // Call GLFW funciton res = glfwGetJoystickButtons( joy, buttons, numbuttons ); // Create result array lua_newtable( L ); for( i = 0; i < res; ++ i ) { lua_pushnumber( L, (lua_Number)(i+1) ); lua_pushnumber( L, (lua_Number)buttons[ i ] ); lua_rawset( L, -3 ); } return 1; } //======================================================================== // Timing //======================================================================== static int glfw_GetTime( lua_State *L ) { lua_Number t; t = glfwGetTime(); lua_settop( L, 0 ); lua_pushnumber( L, t ); return 1; } static int glfw_SetTime( lua_State *L ) { lua_Number t; if ( badArgs( L, 1, "SetTime" ) ) return 0; t = lua_tonumber( L, 1 ); lua_settop( L, 0 ); glfwSetTime( (double)t ); return 0; } static int glfw_Sleep( lua_State *L ) { lua_Number t; if ( badArgs( L, 1, "Sleep" ) ) return 0; t = lua_tonumber( L, 1 ); lua_settop( L, 0 ); glfwSleep( (double)t ); return 0; } //======================================================================== // Threading support (not possible in Lua) //======================================================================== static int glfw_CreateThread( lua_State *L ) { unsupportedFunction( L, "CreateThread" ); return 0; } static int glfw_DestroyThread( lua_State *L ) { unsupportedFunction( L, "DestroyThread" ); return 0; } static int glfw_WaitThread( lua_State *L ) { unsupportedFunction( L, "WaitThread" ); return 0; } static int glfw_GetThreadID( lua_State *L ) { unsupportedFunction( L, "GetThreadID" ); return 0; } static int glfw_CreateMutex( lua_State *L ) { unsupportedFunction( L, "CreateMutex" ); return 0; } static int glfw_DestroyMutex( lua_State *L ) { unsupportedFunction( L, "DestroyMutex" ); return 0; } static int glfw_LockMutex( lua_State *L ) { unsupportedFunction( L, "LockMutex" ); return 0; } static int glfw_UnlockMutex( lua_State *L ) { unsupportedFunction( L, "UnlockMutex" ); return 0; } static int glfw_CreateCond( lua_State *L ) { unsupportedFunction( L, "CreateCond" ); return 0; } static int glfw_DestroyCond( lua_State *L ) { unsupportedFunction( L, "DestroyCond" ); return 0; } static int glfw_WaitCond( lua_State *L ) { unsupportedFunction( L, "WaitCond" ); return 0; } static int glfw_SignalCond( lua_State *L ) { unsupportedFunction( L, "SignalCond" ); return 0; } static int glfw_BroadcastCond( lua_State *L ) { unsupportedFunction( L, "BroadcastCond" ); return 0; } static int glfw_GetNumberOfProcessors( lua_State *L ) { int n; n = glfwGetNumberOfProcessors(); lua_settop( L,0 ); lua_pushnumber( L, (lua_Number)n ); return 1; } //======================================================================== // Extension handling //======================================================================== static int glfw_GetGLVersion( lua_State *L ) { int major, minor, rev; glfwGetGLVersion( &major, &minor, &rev ); lua_settop( L,0 ); lua_pushnumber( L, (lua_Number)major ); lua_pushnumber( L, (lua_Number)minor ); lua_pushnumber( L, (lua_Number)rev ); return 3; } static int glfw_ExtensionSupported( lua_State *L ) { const char *str; int res; if( badArgs( L, 1, "ExtensionSupported" ) ) return 0; str = lua_tostring( L, 1 ); lua_settop( L, 0 ); res = glfwExtensionSupported( str ); lua_pushnumber( L, (lua_Number)res ); return 1; } //======================================================================== // Enable/Disable //======================================================================== static int glfw_Enable( lua_State *L ) { lua_Number param; if ( badArgs( L, 1, "Enable" ) ) return 0; param = lua_tonumber( L, 1 ); lua_settop( L, 0 ); glfwEnable( (int)param ); return 0; } static int glfw_Disable( lua_State *L ) { lua_Number param; if ( badArgs( L, 1, "Disable" ) ) return 0; param = lua_tonumber( L, 1 ); lua_settop( L, 0 ); glfwDisable( (int)param ); return 0; } //======================================================================== // Image/texture I/O support //======================================================================== static int glfw_ReadImage( lua_State *L ) { unsupportedFunction( L, "ReadImage" ); return 0; } static int glfw_FreeImage( lua_State *L ) { unsupportedFunction( L, "FreeImage" ); return 0; } static int glfw_LoadTexture2D( lua_State *L ) { const char *name; lua_Number flags; int res; if( badArgs( L, 2, "LoadTexture2D" ) ) return 0; name = lua_tostring( L, 1 ); flags = lua_tonumber( L, 2 ); lua_settop( L, 0 ); res = glfwLoadTexture2D( name, (int)flags ); lua_pushnumber( L, (lua_Number)res ); return 1; } //======================================================================== // Callback function management //======================================================================== static int glfw_SetWindowSizeCallback( lua_State *L ) { GLFWwindowsizefun fun = NULL; if( lua_isstring( L, 1 ) ) { windowsize_name = lua_tostring( L, 1 ); fun = luaglfw_windowsizefun; } glfwSetWindowSizeCallback( fun ); return 0; } static int glfw_SetWindowCloseCallback( lua_State *L ) { GLFWwindowclosefun fun = NULL; if( lua_isstring( L, 1 ) ) { windowclose_name = lua_tostring( L, 1 ); fun = luaglfw_windowclosefun; } glfwSetWindowCloseCallback( fun ); return 0; } static int glfw_SetWindowRefreshCallback( lua_State *L ) { GLFWwindowrefreshfun fun = NULL; if( lua_isstring( L, 1 ) ) { windowrefresh_name = lua_tostring( L, 1 ); fun = luaglfw_windowrefreshfun; } glfwSetWindowRefreshCallback( fun ); return 0; } static int glfw_SetMouseButtonCallback( lua_State *L ) { GLFWmousebuttonfun fun = NULL; if( lua_isstring( L, 1 ) ) { mousebutton_name = lua_tostring( L, 1 ); fun = luaglfw_mousebuttonfun; } glfwSetMouseButtonCallback( fun ); return 0; } static int glfw_SetMousePosCallback( lua_State *L ) { GLFWmouseposfun fun = NULL; if( lua_isstring( L, 1 ) ) { mousepos_name = lua_tostring( L, 1 ); fun = luaglfw_mouseposfun; } glfwSetMousePosCallback( fun ); return 0; } static int glfw_SetMouseWheelCallback( lua_State *L ) { GLFWmousewheelfun fun = NULL; if( lua_isstring( L, 1 ) ) { mousewheel_name = lua_tostring( L, 1 ); fun = luaglfw_mousewheelfun; } glfwSetMouseWheelCallback( fun ); return 0; } static int glfw_SetKeyCallback( lua_State *L ) { GLFWkeyfun fun = NULL; if( lua_isstring( L, 1 ) ) { key_name = lua_tostring( L, 1 ); fun = luaglfw_keyfun; } glfwSetKeyCallback( fun ); return 0; } static int glfw_SetCharCallback( lua_State *L ) { GLFWcharfun fun = NULL; if( lua_isstring( L, 1 ) ) { char_name = lua_tostring( L, 1 ); fun = luaglfw_charfun; } glfwSetCharCallback( fun ); return 0; } //************************************************************************ //**** LUA Library Registration **** //************************************************************************ // GLFW constants are stored in the global Lua table "glfw" static struct lua_constant glfw_constants[] = { // GL constants (GL_TRUE/GL_FALSE) { "TRUE", GL_TRUE }, { "FALSE", GL_FALSE }, // GLFW version { "VERSION_MAJOR", GLFW_VERSION_MAJOR }, { "VERSION_MINOR", GLFW_VERSION_MINOR }, { "VERSION_REVISION", GLFW_VERSION_REVISION }, // Key and button state/action definitions { "PRESS", GLFW_PRESS }, { "RELEASE", GLFW_RELEASE }, // Keyboard key definitions { "KEY_SPACE", GLFW_KEY_SPACE }, { "KEY_SPECIAL", GLFW_KEY_SPECIAL }, { "KEY_ESC", GLFW_KEY_ESC }, { "KEY_F1", GLFW_KEY_F1 }, { "KEY_F2", GLFW_KEY_F2 }, { "KEY_F3", GLFW_KEY_F3 }, { "KEY_F4", GLFW_KEY_F4 }, { "KEY_F5", GLFW_KEY_F5 }, { "KEY_F6", GLFW_KEY_F6 }, { "KEY_F7", GLFW_KEY_F7 }, { "KEY_F8", GLFW_KEY_F8 }, { "KEY_F9", GLFW_KEY_F9 }, { "KEY_F10", GLFW_KEY_F10 }, { "KEY_F11", GLFW_KEY_F11 }, { "KEY_F12", GLFW_KEY_F12 }, { "KEY_F13", GLFW_KEY_F13 }, { "KEY_F14", GLFW_KEY_F14 }, { "KEY_F15", GLFW_KEY_F15 }, { "KEY_F16", GLFW_KEY_F16 }, { "KEY_F17", GLFW_KEY_F17 }, { "KEY_F18", GLFW_KEY_F18 }, { "KEY_F19", GLFW_KEY_F19 }, { "KEY_F20", GLFW_KEY_F20 }, { "KEY_F21", GLFW_KEY_F21 }, { "KEY_F22", GLFW_KEY_F22 }, { "KEY_F23", GLFW_KEY_F23 }, { "KEY_F24", GLFW_KEY_F24 }, { "KEY_F25", GLFW_KEY_F25 }, { "KEY_UP", GLFW_KEY_UP }, { "KEY_DOWN", GLFW_KEY_DOWN }, { "KEY_LEFT", GLFW_KEY_LEFT }, { "KEY_RIGHT", GLFW_KEY_RIGHT }, { "KEY_LSHIFT", GLFW_KEY_LSHIFT }, { "KEY_RSHIFT", GLFW_KEY_RSHIFT }, { "KEY_LCTRL", GLFW_KEY_LCTRL }, { "KEY_RCTRL", GLFW_KEY_RCTRL }, { "KEY_LALT", GLFW_KEY_LALT }, { "KEY_RALT", GLFW_KEY_RALT }, { "KEY_TAB", GLFW_KEY_TAB }, { "KEY_ENTER", GLFW_KEY_ENTER }, { "KEY_BACKSPACE", GLFW_KEY_BACKSPACE }, { "KEY_INSERT", GLFW_KEY_INSERT }, { "KEY_DEL", GLFW_KEY_DEL }, { "KEY_PAGEUP", GLFW_KEY_PAGEUP }, { "KEY_PAGEDOWN", GLFW_KEY_PAGEDOWN }, { "KEY_HOME", GLFW_KEY_HOME }, { "KEY_END", GLFW_KEY_END }, { "KEY_KP_0", GLFW_KEY_KP_0 }, { "KEY_KP_1", GLFW_KEY_KP_1 }, { "KEY_KP_2", GLFW_KEY_KP_2 }, { "KEY_KP_3", GLFW_KEY_KP_3 }, { "KEY_KP_4", GLFW_KEY_KP_4 }, { "KEY_KP_5", GLFW_KEY_KP_5 }, { "KEY_KP_6", GLFW_KEY_KP_6 }, { "KEY_KP_7", GLFW_KEY_KP_7 }, { "KEY_KP_8", GLFW_KEY_KP_8 }, { "KEY_KP_9", GLFW_KEY_KP_9 }, { "KEY_KP_DIVIDE", GLFW_KEY_KP_DIVIDE }, { "KEY_KP_MULTIPLY", GLFW_KEY_KP_MULTIPLY }, { "KEY_KP_SUBTRACT", GLFW_KEY_KP_SUBTRACT }, { "KEY_KP_ADD", GLFW_KEY_KP_ADD }, { "KEY_KP_DECIMAL", GLFW_KEY_KP_DECIMAL }, { "KEY_KP_EQUAL", GLFW_KEY_KP_EQUAL }, { "KEY_KP_ENTER", GLFW_KEY_KP_ENTER }, { "KEY_LAST", GLFW_KEY_LAST }, // Mouse button definitions { "MOUSE_BUTTON_1", GLFW_MOUSE_BUTTON_1 }, { "MOUSE_BUTTON_2", GLFW_MOUSE_BUTTON_2 }, { "MOUSE_BUTTON_3", GLFW_MOUSE_BUTTON_3 }, { "MOUSE_BUTTON_4", GLFW_MOUSE_BUTTON_4 }, { "MOUSE_BUTTON_5", GLFW_MOUSE_BUTTON_5 }, { "MOUSE_BUTTON_6", GLFW_MOUSE_BUTTON_6 }, { "MOUSE_BUTTON_7", GLFW_MOUSE_BUTTON_7 }, { "MOUSE_BUTTON_8", GLFW_MOUSE_BUTTON_8 }, { "MOUSE_BUTTON_LAST", GLFW_MOUSE_BUTTON_LAST }, // Mouse button aliases { "MOUSE_BUTTON_LEFT", GLFW_MOUSE_BUTTON_LEFT }, { "MOUSE_BUTTON_RIGHT", GLFW_MOUSE_BUTTON_RIGHT }, { "MOUSE_BUTTON_MIDDLE", GLFW_MOUSE_BUTTON_MIDDLE }, // Joystick identifiers { "JOYSTICK_1", GLFW_JOYSTICK_1 }, { "JOYSTICK_2", GLFW_JOYSTICK_2 }, { "JOYSTICK_3", GLFW_JOYSTICK_3 }, { "JOYSTICK_4", GLFW_JOYSTICK_4 }, { "JOYSTICK_5", GLFW_JOYSTICK_5 }, { "JOYSTICK_6", GLFW_JOYSTICK_6 }, { "JOYSTICK_7", GLFW_JOYSTICK_7 }, { "JOYSTICK_8", GLFW_JOYSTICK_8 }, { "JOYSTICK_9", GLFW_JOYSTICK_9 }, { "JOYSTICK_10", GLFW_JOYSTICK_10 }, { "JOYSTICK_11", GLFW_JOYSTICK_11 }, { "JOYSTICK_12", GLFW_JOYSTICK_12 }, { "JOYSTICK_13", GLFW_JOYSTICK_13 }, { "JOYSTICK_14", GLFW_JOYSTICK_14 }, { "JOYSTICK_15", GLFW_JOYSTICK_15 }, { "JOYSTICK_16", GLFW_JOYSTICK_16 }, { "JOYSTICK_LAST", GLFW_JOYSTICK_LAST }, // glfwOpenWindow modes { "WINDOW", GLFW_WINDOW }, { "FULLSCREEN", GLFW_FULLSCREEN }, // glfwGetWindowParam tokens { "OPENED", GLFW_OPENED }, { "ACTIVE", GLFW_ACTIVE }, { "ICONIFIED", GLFW_ICONIFIED }, { "ACCELERATED", GLFW_ACCELERATED }, { "RED_BITS", GLFW_RED_BITS }, { "GREEN_BITS", GLFW_GREEN_BITS }, { "BLUE_BITS", GLFW_BLUE_BITS }, { "ALPHA_BITS", GLFW_ALPHA_BITS }, { "DEPTH_BITS", GLFW_DEPTH_BITS }, { "STENCIL_BITS", GLFW_STENCIL_BITS }, // Constants for glfwGetWindowParam and glfwOpenWindowHint { "REFRESH_RATE", GLFW_REFRESH_RATE }, { "ACCUM_RED_BITS", GLFW_ACCUM_RED_BITS }, { "ACCUM_GREEN_BITS", GLFW_ACCUM_GREEN_BITS }, { "ACCUM_BLUE_BITS", GLFW_ACCUM_BLUE_BITS }, { "ACCUM_ALPHA_BITS", GLFW_ACCUM_ALPHA_BITS }, { "AUX_BUFFERS", GLFW_AUX_BUFFERS }, { "STEREO", GLFW_STEREO }, // glfwEnable/glfwDisable tokens { "MOUSE_CURSOR", GLFW_MOUSE_CURSOR }, { "STICKY_KEYS", GLFW_STICKY_KEYS }, { "STICKY_MOUSE_BUTTONS", GLFW_STICKY_MOUSE_BUTTONS }, { "SYSTEM_KEYS", GLFW_SYSTEM_KEYS }, { "KEY_REPEAT", GLFW_KEY_REPEAT }, { "AUTO_POLL_EVENTS", GLFW_AUTO_POLL_EVENTS }, // glfwWaitThread wait modes { "WAIT", GLFW_WAIT }, { "NOWAIT", GLFW_NOWAIT }, // glfwGetJoystickParam tokens { "PRESENT", GLFW_PRESENT }, { "AXES", GLFW_AXES }, { "BUTTONS", GLFW_BUTTONS }, // glfwReadImage/glfwLoadTexture2D flags { "NO_RESCALE_BIT", GLFW_NO_RESCALE_BIT }, { "ORIGIN_UL_BIT", GLFW_ORIGIN_UL_BIT }, { "BUILD_MIPMAPS_BIT", GLFW_BUILD_MIPMAPS_BIT }, { "ALPHA_MAP_BIT", GLFW_ALPHA_MAP_BIT }, // Time spans longer than this (seconds) are considered to be infinity { "INFINITY", GLFW_INFINITY }, { NULL, 0 } }; // Library functions to register static const luaL_reg glfwlib[] = { { "Init", glfw_Init }, { "Terminate", glfw_Terminate }, { "GetVersion", glfw_GetVersion }, { "OpenWindow", glfw_OpenWindow }, { "OpenWindowHint", glfw_OpenWindowHint }, { "CloseWindow", glfw_CloseWindow }, { "SetWindowTitle", glfw_SetWindowTitle }, { "SetWindowSize", glfw_SetWindowSize }, { "GetWindowSize", glfw_GetWindowSize }, { "SetWindowPos", glfw_SetWindowPos }, { "IconifyWindow", glfw_IconifyWindow }, { "RestoreWindow", glfw_RestoreWindow }, { "GetWindowParam", glfw_GetWindowParam }, { "SwapBuffers", glfw_SwapBuffers }, { "SwapInterval", glfw_SwapInterval }, { "GetVideoModes", glfw_GetVideoModes }, { "GetDesktopMode", glfw_GetDesktopMode }, { "PollEvents", glfw_PollEvents }, { "WaitEvents", glfw_WaitEvents }, { "GetKey", glfw_GetKey }, { "GetMouseButton", glfw_GetMouseButton }, { "GetMousePos", glfw_GetMousePos }, { "SetMousePos", glfw_SetMousePos }, { "GetMouseWheel", glfw_GetMouseWheel }, { "SetMouseWheel", glfw_SetMouseWheel }, { "GetJoystickParam", glfw_GetJoystickParam }, { "GetJoystickPos", glfw_GetJoystickPos }, { "GetJoystickButtons", glfw_GetJoystickButtons }, { "GetTime", glfw_GetTime }, { "SetTime", glfw_SetTime }, { "Sleep", glfw_Sleep }, { "GetGLVersion", glfw_GetGLVersion }, { "ExtensionSupported", glfw_ExtensionSupported }, { "CreateThread", glfw_CreateThread }, { "DestroyThread", glfw_DestroyThread }, { "WaitThread", glfw_WaitThread }, { "GetThreadID", glfw_GetThreadID }, { "CreateMutex", glfw_CreateMutex }, { "DestroyMutex", glfw_DestroyMutex }, { "LockMutex", glfw_LockMutex }, { "UnlockMutex", glfw_UnlockMutex }, { "CreateCond", glfw_CreateCond }, { "DestroyCond", glfw_DestroyCond }, { "WaitCond", glfw_WaitCond }, { "SignalCond", glfw_SignalCond }, { "BroadcastCond", glfw_BroadcastCond }, { "GetNumberOfProcessors", glfw_GetNumberOfProcessors }, { "Enable", glfw_Enable }, { "Disable", glfw_Disable }, { "ReadImage", glfw_ReadImage }, { "FreeImage", glfw_FreeImage }, { "LoadTexture2D", glfw_LoadTexture2D }, { "SetWindowSizeCallback", glfw_SetWindowSizeCallback }, { "SetWindowCloseCallback", glfw_SetWindowCloseCallback }, { "SetWindowRefreshCallback", glfw_SetWindowRefreshCallback }, { "SetMouseButtonCallback", glfw_SetMouseButtonCallback }, { "SetMousePosCallback", glfw_SetMousePosCallback }, { "SetMouseWheelCallback", glfw_SetMouseWheelCallback }, { "SetKeyCallback", glfw_SetKeyCallback }, { "SetCharCallback", glfw_SetCharCallback }, { NULL, NULL } }; int luaopen_glfw( lua_State *L ) { // Store GLFW functions in "glfw" table luaL_openlib( L, "glfw", glfwlib, 0 ); // Store GLFW constants in "glfw" table lua_pushstring( L, "glfw" ); lua_gettable( L, LUA_GLOBALSINDEX ); addConstants( L, glfw_constants ); lua_settop( L, 0 ); // Remember Lua state for callback functions callback_lua_state = L; return 0; } dokidoki-support/luaglfw.h000066400000000000000000000027141147057410300162050ustar00rootroot00000000000000//======================================================================== // GLFW - An OpenGL framework // File: luaglfw.h // Platform: Lua 5.0 // API version: 2.5 // WWW: http://glfw.sourceforge.net //------------------------------------------------------------------------ // Copyright (c) 2002-2005 Camilla Berglund // // This software is provided 'as-is', without any express or implied // warranty. In no event will the authors be held liable for any damages // arising from the use of this software. // // Permission is granted to anyone to use this software for any purpose, // including commercial applications, and to alter it and redistribute it // freely, subject to the following restrictions: // // 1. The origin of this software must not be misrepresented; you must not // claim that you wrote the original software. If you use this software // in a product, an acknowledgment in the product documentation would // be appreciated but is not required. // // 2. Altered source versions must be plainly marked as such, and must not // be misrepresented as being the original software. // // 3. This notice may not be removed or altered from any source // distribution. // //======================================================================== #ifndef __luaglfw_h_ #define __luaglfw_h_ #ifdef __cplusplus extern "C" { #endif #include int luaopen_glfw( lua_State * L ); #ifdef __cplusplus } #endif #endif // __luaglfw_h_ dokidoki-support/memarray.c000066400000000000000000000427551147057410300163650ustar00rootroot00000000000000/* ====================================================================== ** memarray.c - Copyright (C) 2005-2006 Varol Kaptan ** see LICENSE for more information ** ====================================================================== ** vim: set ts=3 et: */ #include #include #include #if defined(MEMARRAY_USE_OPENGL) #if defined(__APPLE__) && defined(__MACH__) #include #include #else #include #include #endif #endif #include #include #define MYNAME "memarray" #define MYTYPE "memarray*" #define VERSION "0.5" #define MEMARRAY_TCHAR 0 #define MEMARRAY_TUCHAR 1 #define MEMARRAY_TSHORT 2 #define MEMARRAY_TUSHORT 3 #define MEMARRAY_TINT 4 #define MEMARRAY_TUINT 5 #define MEMARRAY_TLONG 6 #define MEMARRAY_TULONG 7 #define MEMARRAY_TFLOAT 8 #define MEMARRAY_TDOUBLE 9 #if !defined(MEMARRAY_USE_OPENGL) #define MEMARRAY_NUM_TYPES 10 #else #define MEMARRAY_TGLBOOLEAN 10 #define MEMARRAY_TGLBYTE 11 #define MEMARRAY_TGLUBYTE 12 #define MEMARRAY_TGLSHORT 13 #define MEMARRAY_TGLUSHORT 14 #define MEMARRAY_TGLINT 15 #define MEMARRAY_TGLUINT 16 #define MEMARRAY_TGLSIZEI 17 #define MEMARRAY_TGLENUM 18 #define MEMARRAY_TGLBITFIELD 19 #define MEMARRAY_TGLFLOAT 20 #define MEMARRAY_TGLDOUBLE 21 #define MEMARRAY_NUM_TYPES 22 #endif #define MEMARRAY_TYPE_MASK 0x1F #define MEMARRAY_INHERITED 0x20 #define mem_type(m) ((m)->flags & MEMARRAY_TYPE_MASK) #define mem_inherited(m) ((m)->flags & MEMARRAY_INHERITED) typedef struct { char *name; int size; } typeinfo; const typeinfo m_types[MEMARRAY_NUM_TYPES] = { { "char", sizeof(char) }, { "uchar", sizeof(unsigned char) }, { "short", sizeof(short) }, { "ushort", sizeof(unsigned short) }, { "int", sizeof(int) }, { "uint", sizeof(unsigned int) }, { "long", sizeof(long) }, { "ulong", sizeof(unsigned long) }, { "float", sizeof(float) }, { "double", sizeof(double) }, #if defined(MEMARRAY_USE_OPENGL) { "GLboolean", sizeof(GLboolean) }, { "GLbyte", sizeof(GLbyte) }, { "GLubyte", sizeof(GLubyte) }, { "GLshort", sizeof(GLshort) }, { "GLushort", sizeof(GLushort) }, { "GLint", sizeof(GLint) }, { "GLuint", sizeof(GLuint) }, { "GLsizei", sizeof(GLsizei) }, { "GLenum", sizeof(GLenum) }, { "GLbitfield",sizeof(GLbitfield) }, { "GLfloat", sizeof(GLfloat) }, { "GLdouble", sizeof(GLdouble) }, #endif }; typedef struct memarray { size_t size; size_t length; unsigned int flags; void *data; } memarray_t; /* we keep discovered endianness here (1 = little endian, 0 = big endian) */ static int little_endian = 0; static int get_memtype(const char * typename) { int type; for (type = 0; type < MEMARRAY_NUM_TYPES; ++type) { if (strcmp(typename, m_types[type].name) == 0) return type; } return -1; } static const void * check_lightuserdata(lua_State *L, int n) { if (lua_type(L,n) != LUA_TLIGHTUSERDATA) luaL_typerror(L, n, "lightuserdata"); return (const void *) lua_touserdata(L, n); } static memarray_t *memarray_get(lua_State * L, int i) { if (luaL_checkudata(L, i, MYTYPE) == NULL) luaL_typerror(L, i, MYTYPE); return lua_touserdata(L, i); } static memarray_t *memarray_new(lua_State * L) { memarray_t *m = lua_newuserdata(L, sizeof(memarray_t)); m->data = NULL; m->size = 0; m->length = 0; m->flags = MEMARRAY_INHERITED; luaL_getmetatable(L, MYTYPE); lua_setmetatable(L, -2); /* fprintf(stderr, "memarray_new @ %p\n", (void *)m); */ return m; } static void memarray_delete(memarray_t *m) { if (!mem_inherited(m)) free(m->data); m->data = NULL; m->size = 0; m->length = 0; m->flags = MEMARRAY_INHERITED; } /* self, 'type', n, [address] */ static int Lrealloc(lua_State * L) { memarray_t *m; const char *typename; int type; size_t length; size_t size; void * data; char errmsg[128]; m = memarray_get(L, 1); typename = luaL_optstring(L, 2, "uchar"); if ( (type = get_memtype(typename)) < 0 ) { luaL_error(L, "memarray:realloc(): unknown type '%s'", typename); return 0; } length = (size_t) luaL_optnumber(L, 3, 0); size = length * m_types[type].size; if (lua_gettop(L) > 3) { /* do not allocate memory, inherit something else */ memarray_delete(m); m->size = size; m->length = length; m->flags = type | MEMARRAY_INHERITED; m->data = lua_touserdata(L, 4); } else { /* allocate memory */ memarray_delete(m); data = (void *) malloc(size); if ((size != 0) && (data == NULL)) { m->flags = type | MEMARRAY_INHERITED; sprintf(errmsg, "memarray:__call(): memory allocation failure " "for %zd bytes (%s x %zd)\n", size, typename, length); luaL_error(L, errmsg); } else { m->flags = type; m->size = size; m->length = length; m->data = data; } } lua_settop(L, 1); return 1; } /* memarray, 'type', n, [address] */ static int L__call(lua_State * L) { memarray_t *m; m = memarray_new(L); lua_replace(L, 1); return Lrealloc(L); } /* 'type' */ static int Lsizeof(lua_State * L) { const char *typename; int type; typename = luaL_checkstring(L, 1); if ( (type = get_memtype(typename)) < 0 ) { luaL_error(L, "memarray:sizeof(): unknown type '%s'", typename); return 0; } lua_pushnumber(L, m_types[type].size); return 1; } /* dst, src, size(in bytes) */ static int Lmemcpy(lua_State * L) { void * dst = (void *) check_lightuserdata(L, 1); const void * src = check_lightuserdata(L, 2); size_t size = (size_t) luaL_checknumber(L, 3); memcpy(dst, src, size); return 1; } /* self, index */ static int L__index(lua_State * L) { memarray_t *m; size_t index; m = memarray_get(L, 1); switch (lua_type(L, 2)) { case LUA_TNUMBER: index = (size_t) lua_tonumber(L, 2); if ( /*(index < 0) ||*/ (index >= m->length) ) { lua_pushnil(L); luaL_error(L, "index out of range"); } else switch (mem_type(m)) { case MEMARRAY_TCHAR: lua_pushnumber(L, ((char *)(m->data))[index]); break; case MEMARRAY_TUCHAR: lua_pushnumber(L, ((unsigned char *)(m->data))[index]); break; case MEMARRAY_TSHORT: lua_pushnumber(L, ((short *)(m->data))[index]); break; case MEMARRAY_TUSHORT: lua_pushnumber(L, ((unsigned short *)(m->data))[index]); break; case MEMARRAY_TINT: lua_pushnumber(L, ((int *)(m->data))[index]); break; case MEMARRAY_TUINT: lua_pushnumber(L, ((unsigned int *)(m->data))[index]); break; case MEMARRAY_TLONG: lua_pushnumber(L, ((long *)(m->data))[index]); break; case MEMARRAY_TULONG: lua_pushnumber(L, ((unsigned long *)(m->data))[index]); break; case MEMARRAY_TFLOAT: lua_pushnumber(L, ((float *)(m->data))[index]); break; case MEMARRAY_TDOUBLE: lua_pushnumber(L, ((double *)(m->data))[index]); break; #if defined(MEMARRAY_USE_OPENGL) case MEMARRAY_TGLBOOLEAN: lua_pushnumber(L, ((GLboolean *)(m->data))[index]); break; case MEMARRAY_TGLBYTE: lua_pushnumber(L, ((GLbyte *)(m->data))[index]); break; case MEMARRAY_TGLUBYTE: lua_pushnumber(L, ((GLubyte *)(m->data))[index]); break; case MEMARRAY_TGLSHORT: lua_pushnumber(L, ((GLshort *)(m->data))[index]); break; case MEMARRAY_TGLUSHORT: lua_pushnumber(L, ((GLushort *)(m->data))[index]); break; case MEMARRAY_TGLINT: lua_pushnumber(L, ((GLint *)(m->data))[index]); break; case MEMARRAY_TGLUINT: lua_pushnumber(L, ((GLuint *)(m->data))[index]); break; case MEMARRAY_TGLSIZEI: lua_pushnumber(L, ((GLsizei *)(m->data))[index]); break; case MEMARRAY_TGLENUM: lua_pushnumber(L, ((GLenum *)(m->data))[index]); break; case MEMARRAY_TGLBITFIELD: lua_pushnumber(L, ((GLbitfield*)(m->data))[index]); break; case MEMARRAY_TGLFLOAT: lua_pushnumber(L, ((GLfloat *)(m->data))[index]); break; case MEMARRAY_TGLDOUBLE: lua_pushnumber(L, ((GLdouble *)(m->data))[index]); break; #endif default: lua_pushnil(L); break; } break; default: lua_getmetatable(L, 1); lua_replace(L, 1); lua_rawget(L, 1); break; } return 1; } /* self, index, value */ static int L__newindex(lua_State * L) { memarray_t *m; size_t index; lua_Number value; m = memarray_get(L, 1); switch (lua_type(L, 2)) { case LUA_TNUMBER: index = (size_t) lua_tonumber(L, 2); value = lua_tonumber(L, 3); if ( /*(index < 0) ||*/ (index >= m->length) ) { luaL_error(L, "index out of range"); } else switch (mem_type(m)) { case MEMARRAY_TCHAR: ((char *)m->data)[index] = (char) value; break; case MEMARRAY_TUCHAR: ((unsigned char *)m->data)[index] = (unsigned char) value; break; case MEMARRAY_TSHORT: ((short *)m->data)[index] = (short) value; break; case MEMARRAY_TUSHORT: ((unsigned short *)m->data)[index] = (unsigned short) value; break; case MEMARRAY_TINT: ((int *)m->data)[index] = (int) value; break; case MEMARRAY_TUINT: ((unsigned int *)m->data)[index] = (unsigned int) value; break; case MEMARRAY_TLONG: ((long *)m->data)[index] = (long) value; break; case MEMARRAY_TULONG: ((unsigned long *)m->data)[index] = (unsigned long) value; break; case MEMARRAY_TFLOAT: ((float *)m->data)[index] = (float) value; break; case MEMARRAY_TDOUBLE: ((double *)m->data)[index] = (double) value; break; #if defined(MEMARRAY_USE_OPENGL) case MEMARRAY_TGLBOOLEAN: ((GLboolean *)(m->data))[index] = (GLboolean ) value; break; case MEMARRAY_TGLBYTE: ((GLbyte *)(m->data))[index] = (GLbyte ) value; break; case MEMARRAY_TGLUBYTE: ((GLubyte *)(m->data))[index] = (GLubyte ) value; break; case MEMARRAY_TGLSHORT: ((GLshort *)(m->data))[index] = (GLshort ) value; break; case MEMARRAY_TGLUSHORT: ((GLushort *)(m->data))[index] = (GLushort ) value; break; case MEMARRAY_TGLINT: ((GLint *)(m->data))[index] = (GLint ) value; break; case MEMARRAY_TGLUINT: ((GLuint *)(m->data))[index] = (GLuint ) value; break; case MEMARRAY_TGLSIZEI: ((GLsizei *)(m->data))[index] = (GLsizei ) value; break; case MEMARRAY_TGLENUM: ((GLenum *)(m->data))[index] = (GLenum ) value; break; case MEMARRAY_TGLBITFIELD: ((GLbitfield*)(m->data))[index] = (GLbitfield) value; break; case MEMARRAY_TGLFLOAT: ((GLfloat *)(m->data))[index] = (GLfloat ) value; break; case MEMARRAY_TGLDOUBLE: ((GLdouble *)(m->data))[index] = (GLdouble ) value; break; #endif default: lua_pushnil(L); break; } break; default: luaL_error(L, "memarray*:__newindex key must be number"); break; } return 0; } /* self */ static int L__tostring(lua_State * L) { memarray_t *m = memarray_get(L, 1); char s[256]; sprintf(s, mem_inherited(m) ? "memarray(%s,%zd,%p,inherited)@%p": "memarray(%s,%zd,%p)@%p" , m_types[mem_type(m)].name, m->length, (void *)m->data, (void *)m); lua_pushstring(L, s); return 1; } /* self */ static int Lsize(lua_State *L) { memarray_t *m = memarray_get(L, 1); lua_pushnumber(L, m->size); return 1; } /* self */ static int Llength(lua_State *L) { memarray_t *m = memarray_get(L, 1); lua_pushnumber(L, m->length); return 1; } /* self, [index] */ static int Lptr(lua_State *L) { memarray_t *m = memarray_get(L, 1); size_t index = (size_t) luaL_optnumber(L, 2, 0); if ( /*(index < 0) ||*/ (index >= m->length) ) { luaL_error(L, "index out of range"); return 0; } else { char *ptr = m->data; lua_pushlightuserdata(L, (void *)(ptr + index * m_types[mem_type(m)].size)); return 1; } } /* self */ static int L__gc(lua_State *L) { memarray_t *m = memarray_get(L, 1); /* fprintf(stderr, "invoking __gc for "); fprintf(stderr, mem_inherited(m) ? "memarray(%s,%zd,%p,inherited)@%p\n": "memarray(%s,%zd,%p)@%p\n" , m_types[mem_type(m)].name, m->length, (void *)m->data, (void *)m); */ memarray_delete(m); return 0; } /* self */ static int Lto_str(lua_State *L) { memarray_t *m = memarray_get(L, 1); luaL_Buffer b; luaL_buffinit(L, &b); luaL_addlstring(&b, m->data, m->size); luaL_pushresult(&b); return 1; } /* self, str */ static int Lfrom_str(lua_State *L) { size_t len; memarray_t *m = memarray_get(L, 1); const char *str = luaL_checklstring(L, 2, &len); if (m->size != len) { luaL_error(L, "memarray:to_str size mismatch"); return 0; } else { memcpy(m->data, str, len); } return 0; } #define PUT_SWAP_2(B, src) \ luaL_putchar((B), (src)[1]); \ luaL_putchar((B), (src)[0]); #define PUT_SWAP_4(B, src) \ luaL_putchar((B), (src)[3]); \ luaL_putchar((B), (src)[2]); \ luaL_putchar((B), (src)[1]); \ luaL_putchar((B), (src)[0]); #define PUT_SWAP_8(B, src) \ luaL_putchar((B), (src)[7]); \ luaL_putchar((B), (src)[6]); \ luaL_putchar((B), (src)[5]); \ luaL_putchar((B), (src)[4]); \ luaL_putchar((B), (src)[3]); \ luaL_putchar((B), (src)[2]); \ luaL_putchar((B), (src)[1]); \ luaL_putchar((B), (src)[0]); /* self */ static int Lto_netstr(lua_State *L) { memarray_t *m = memarray_get(L, 1); luaL_Buffer b; size_t item_size; unsigned char *src, *end; luaL_buffinit(L, &b); item_size = m->size / m->length; if (little_endian && (item_size != 1)) { src = (unsigned char *) m->data; end = src + m->size; switch (item_size) { case 2: while (src < end) { PUT_SWAP_2(&b, src); src += 2; } break; case 4: while (src < end) { PUT_SWAP_4(&b, src); src += 4; } break; case 8: while (src < end) { PUT_SWAP_8(&b, src); src += 8; } break; default: luaL_error(L, "memarray:to_netstr(): unsupported item_size"); break; } } else { luaL_addlstring(&b, m->data, m->size); } luaL_pushresult(&b); return 1; } #define COPY_SWAP_2(src,dst) \ dst[0] = src[1]; \ dst[1] = src[0]; #define COPY_SWAP_4(src, dst) \ dst[0] = src[3]; \ dst[1] = src[2]; \ dst[2] = src[1]; \ dst[3] = src[0]; #define COPY_SWAP_8(src, dst) \ dst[0] = src[7]; \ dst[1] = src[6]; \ dst[2] = src[5]; \ dst[3] = src[4]; \ dst[4] = src[3]; \ dst[5] = src[2]; \ dst[6] = src[1]; \ dst[7] = src[0]; /* self, str */ static int Lfrom_netstr(lua_State *L) { size_t len; memarray_t *m = memarray_get(L, 1); const char *str = luaL_checklstring(L, 2, &len); size_t item_size; unsigned char *src, *end, *dst; if (m->size != len) { luaL_error(L, "memarray:to_netstr size mismatch"); return 0; } else { item_size = m->size / m->length; if (little_endian && (item_size != 1)) { src = (unsigned char *) str; end = src + m->size; dst = (unsigned char *) m->data; switch (item_size) { case 2: while (src < end) { COPY_SWAP_2(src, dst); src += 2; dst += 2; } break; case 4: while (src < end) { COPY_SWAP_4(src, dst); src += 4; dst += 4; } break; case 8: while (src < end) { COPY_SWAP_8(src, dst); src += 8; dst += 8; } break; default: luaL_error(L, "memarray:from_netstr(): unsupported item_size"); break; } } else { memcpy(m->data, str, len); } } return 0; } static const luaL_reg reg_memarray[] = { { "__call", L__call }, { "sizeof", Lsizeof }, { "memcpy", Lmemcpy }, { NULL, NULL } }; static const luaL_reg reg_memarray_ptr[] = { { "__index", L__index }, { "__newindex", L__newindex }, { "__tostring", L__tostring }, { "__gc", L__gc }, { "size", Lsize }, { "length", Llength }, { "ptr", Lptr }, { "to_str", Lto_str }, { "from_str", Lfrom_str }, { "to_netstr", Lto_netstr }, { "from_netstr", Lfrom_netstr}, { "realloc", Lrealloc }, { NULL, NULL } }; LUALIB_API int luaopen_memarray(lua_State * L) { /* we discover endianness here */ int one = 1; little_endian = (*(char *)&one == 1); luaL_newmetatable(L, MYTYPE); luaL_openlib(L, NULL, reg_memarray_ptr, 0); lua_newtable(L); lua_pushliteral(L, "VERSION"); lua_pushliteral(L, VERSION); lua_settable(L, -3); lua_pushliteral(L, MYNAME); lua_pushvalue(L, -2); lua_settable(L, LUA_GLOBALSINDEX); lua_pushvalue(L, -1); luaL_openlib(L, NULL, reg_memarray, 0); lua_setmetatable(L, -2); return 1; } dokidoki-support/minlua.c000066400000000000000000000123221147057410300160200ustar00rootroot00000000000000#include #include #include #include #include #include #include #include #include "log.h" //// preloaders /////////////////////////////////////////////////////////////// // to statically link in binary lua modules: // // - use the right linker options (or just include the .a in the link line) // - add them to init_preloaders right below // // You don't need to manually declare them or include anything, the // REGISTER_LOADER macro handles that. #define REGISTER_LOADER(module_name, loader) \ int loader(lua_State *L); \ lua_pushcfunction(L, loader); \ lua_setfield(L, -2, module_name) void init_preloaders(lua_State *L) { lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); // add your custom loaders here, they look like this: REGISTER_LOADER("collision.native", luaopen_collision_native); REGISTER_LOADER("gl", luaopen_gl); REGISTER_LOADER("glfw", luaopen_glfw); REGISTER_LOADER("glu", luaopen_glu); REGISTER_LOADER("log", luaopen_log); REGISTER_LOADER("memarray", luaopen_memarray); REGISTER_LOADER("mixer", luaopen_mixer); REGISTER_LOADER("stb_image", luaopen_stb_image); // each night I pray that I might one day happen upon a less disgusting way // of doing this #ifdef EXTRA_LOADERS #include EXTRA_LOADERS #endif lua_pop(L, 2); } //// working directory handling /////////////////////////////////////////////// #if defined(DOKIDOKI_LINUX) int get_exe_path(char *buffer, int size) { int ret = readlink("/proc/self/exe", buffer, size); if(0 >= ret && ret < size) buffer[ret] = 0; return ret; } #elif defined(DOKIDOKI_MINGW) #include int get_exe_path(char *buffer, int size) { return GetModuleFileName(NULL, buffer, size); } #elif defined(DOKIDOKI_MACOSX) #include #include int switch_to_game_directory() { log_message("finding the bundle directory"); CFBundleRef bundle = CFBundleGetMainBundle(); CFURLRef url = NULL; if(bundle) url = CFBundleCopyBundleURL(bundle); if(!url) { log_message("no bundle found"); return 0; } CFStringRef str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle); CFRelease(url); int size = CFStringGetLength(str) + 1; char *dir = (char *)malloc(size); int success = CFStringGetCString(str, dir, size, kCFURLPOSIXPathStyle); CFRelease(str); if(success) { int success = chdir(dir); if(success == 0) log_messagef("changed directory to '%s'", dir); else log_messagef("failed changing directory to '%s'", dir); // there must be a better way to tell if we're bundled :/ if(chdir("Contents/Resources") == 0) log_message("running as an app bundle, changed to resources directory"); else log_message("running unbundled"); } free(dir); return success; } #else int switch_to_game_directory() { log_message("automatic game path detection not supported on this platform"); return 0; } #endif #if defined(DOKIDOKI_MINGW) || defined(DOKIDOKI_LINUX) int switch_to_game_directory() { log_message("finding the game's directory"); int size = 256; char *buffer = NULL; while(!buffer) { buffer = (char *)malloc(size); int ret = get_exe_path(buffer, size); // error if(ret <= 0) { free(buffer); buffer = NULL; log_message("failed locating the executable"); return 0; } // buffer too small else if(ret == size) { free(buffer); buffer = NULL; size *= 2; } } char *dir = dirname(buffer); int success = chdir(dir); if(success == 0) log_messagef("changed directory to '%s'", dir); else log_messagef("failed to change directory to '%s'", dir); free(buffer); return success; } #endif //// main program ///////////////////////////////////////////////////////////// int main(int argc, char ** argv) { int i; int use_logfile = 1; for(i = 0; i < argc; i++) if(strcmp(argv[i], "--stderr") == 0) use_logfile = 0; log_init(use_logfile); switch_to_game_directory(); // load lua and libraries lua_State *L = lua_open(); luaL_openlibs(L); init_preloaders(L); // load arguments (and pre-arguments) into a global 'arg' table lua_newtable(L); for(i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i); } lua_setglobal(L, "arg"); // open app, with error reporting lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); int error = luaL_dofile(L, "init.lua"); if(!error) { // load command-line arguments as function arguments lua_checkstack(L, argc - 1); for(i = 1; i <= argc - 1; i++) lua_pushstring(L, argv[i]); error = lua_pcall(L, argc - 1, 0, -2); // run the result } if(error) log_messagef("fatal error: %s", lua_tostring(L, -1)); else log_message("exiting normally"); lua_close(L); return error; } dokidoki-support/mixer.c000066400000000000000000000407061147057410300156660ustar00rootroot00000000000000#include #include #include #include #include "stb_vorbis.h" //// Types, Constants, Globals //////////////////////////////////////////////// typedef short sample_t; typedef int calc_t; #define SAMPLE_MIN (-0x8000) #define SAMPLE_MAX 0x7FFF #define PORTAUDIO_SAMPLE_TYPE paInt16 #define ALSA_SAMPLE_TYPE SND_PCM_FORMAT_S16 #define SAMPLE_RATE 44100 #define SAMPLE_TIME (1.0/SAMPLE_RATE) #define CHANNELS 64 #define BUFFER_SIZE 256 typedef struct { size_t frames; sample_t * samples; } sound_data_t; typedef struct { float volume[2]; } fade_t; typedef struct { // shared volatile int is_new; const sound_data_t * volatile data; int loop; // callback-only after init volatile int new_fade_duration; volatile fade_t new_fade; // for use by callback only size_t position; int fade_duration; int fade_remaining; fade_t fade_start; fade_t fade_end; } channel_t; static const char * error_string = NULL; static int calc_buffer[BUFFER_SIZE * 2]; #define CHECK(condition, message) \ do \ { \ if(!(condition)) \ { \ if(message) \ error_string = message; \ return 0; \ } \ } while(0) #define ERROR(message) CHECK(0, message) //// Fades //////////////////////////////////////////////////////////////////// static float interp(float t, float start, float end) { return (1-t) * start + t * end; } static void fade_interp(const channel_t * chan, fade_t *out) { const fade_t * start = &chan->fade_start; const fade_t * end = &chan->fade_end; float t = 1; if(chan->fade_duration > 0) t = 1 - (float)chan->fade_remaining / chan->fade_duration; out->volume[0] = interp(t, start->volume[0], end->volume[0]); out->volume[1] = interp(t, start->volume[1], end->volume[1]); } static void fade_init(fade_t * out) { out->volume[0] = 1; out->volume[1] = 1; } //// Channels ///////////////////////////////////////////////////////////////// static channel_t channels[CHANNELS]; static int channel_is_valid(int channel) { return 0 <= channel && channel < CHANNELS; } static int channel_is_active(int channel) { assert(channel_is_valid(channel)); return channels[channel].data != NULL; } static void channel_stop(int channel) { assert(channel_is_valid(channel)); channels[channel].data = NULL; } static void channel_mix_into(int channel, calc_t * output, size_t frame_count) { assert(channel_is_valid(channel)); assert(channel_is_active(channel)); int i; channel_t * chan = &channels[channel]; const sound_data_t * data = chan->data; if(chan->is_new) { chan->is_new = 0; chan->position = 0; chan->fade_duration = 0; chan->fade_remaining = 0; fade_init(&chan->fade_start); fade_init(&chan->fade_end); } if(chan->new_fade_duration >= 0) { fade_t current; fade_interp(chan, ¤t); chan->fade_start = current; chan->fade_end = chan->new_fade; chan->fade_duration = chan->new_fade_duration; chan->fade_remaining = chan->fade_duration; chan->new_fade_duration = -1; } size_t position = chan->position; size_t to_copy = data->frames - position; if(to_copy > frame_count) to_copy = frame_count; for(i = 0; i < to_copy; i++) { fade_t fade; fade_interp(chan, &fade); output[i*2] += data->samples[(position + i)*2] * fade.volume[0]; output[i*2+1] += data->samples[(position + i)*2+1] * fade.volume[1]; if(chan->fade_remaining > 0) chan->fade_remaining--; } chan->position = position + to_copy; if(chan->position == data->frames) { // imprecise looping for now if(chan->loop > 0) { chan->loop--; if(chan->loop == 0) channel_stop(channel); else chan->position = 0; } else { chan->position = 0; } } } //// General Audio //////////////////////////////////////////////////////////// static void mix_into(sample_t * output, size_t frame_count) { memset(calc_buffer, 0, frame_count * 2 * sizeof(calc_t)); int c; for(c = 0; c != CHANNELS; c++) if(channel_is_active(c)) channel_mix_into(c, calc_buffer, frame_count); int i; for(i = 0; i != BUFFER_SIZE * 2; i++) { if(calc_buffer[i] <= SAMPLE_MIN) output[i] = SAMPLE_MIN; else if(calc_buffer[i] <= SAMPLE_MAX) output[i] = calc_buffer[i]; else output[i] = SAMPLE_MAX; } } static int play_sound_effect(const sound_data_t * data, float left, float right, int loop) { int c = 0; while(c != CHANNELS && channel_is_active(c)) c++; if(c == CHANNELS) { return -1; } else { channels[c].is_new = 1; channels[c].loop = loop; channels[c].new_fade.volume[0] = left; channels[c].new_fade.volume[1] = right; channels[c].new_fade_duration = 0; // this marks the channel as ready, so after this the callback is free // to jump on it channels[c].data = data; return c; } } static void channel_fade_to(int channel, float duration, float left, float right) { assert(channel_is_valid(channel)); if(channel_is_active(channel)) { while(channels[channel].new_fade_duration >= 0) /* wait for previous fade to be acknowledged */; channels[channel].new_fade.volume[0] = left; channels[channel].new_fade.volume[1] = right; channels[channel].new_fade_duration = (int)(duration * SAMPLE_RATE); } } //// Sound Data /////////////////////////////////////////////////////////////// static sound_data_t * new_sound_data(size_t frames) { sound_data_t * data = (sound_data_t *)malloc(sizeof(sound_data_t)); data->frames = frames; data->samples = (sample_t *)malloc(frames * sizeof(sample_t) * 2); return data; } static void delete_sound_data(sound_data_t * data) { free(data->samples); free(data); } //// WAV Loading ////////////////////////////////////////////////////////////// static unsigned char get_u8(FILE * file) { int n = fgetc(file); return n == EOF ? 0 : (unsigned char)n; } static unsigned short get_u16(FILE * file) { unsigned n = get_u8(file); return n + (get_u8(file) << 8); } static unsigned short get_s16(FILE * file) { return (signed short)get_u16(file); } static unsigned int get_u32(FILE * file) { unsigned n = get_u16(file); return n + (get_u16(file) << 16); } static int get_chunk_id(const char * id, FILE * file) { size_t read; char chunk_id[5]; chunk_id[4] = 0; read = fread(chunk_id, 1, 4, file); CHECK(read == 4 && strcmp(chunk_id, id) == 0, NULL); return 1; } static sound_data_t * load_wav(const char * filename) { FILE * file = fopen(filename, "rb"); CHECK(file, "file not found"); char chunk_id[5]; chunk_id[4] = 0; // read RIFF header CHECK(get_chunk_id("RIFF", file), "didn't get expected \"RIFF\" chunk"); get_u32(file); CHECK(get_chunk_id("WAVE", file), "didn't get expected \"WAVE\" type"); // read "fmt " chunk CHECK(get_chunk_id("fmt ", file), "didn't get expected \"fmt \" chunk"); CHECK(get_u32(file) == 16, "unexpected \"fmt \" chunk size"); CHECK(get_u16(file) == 1, "unsupported compression type"); int channels = get_u16(file); CHECK(channels == 1 || channels == 2, "unsupported number of channels"); CHECK(get_u32(file) == SAMPLE_RATE, "unsupported sample rate"); get_u32(file); // skip bytes per second get_u16(file); // skip block align int bits_per_sample = get_u16(file); CHECK(bits_per_sample == 8 || bits_per_sample == 16, "unsupported bits per sample"); // read "data" chunk CHECK(get_chunk_id("data", file), "didn't get expected \"data\" chunk"); size_t size = get_u32(file); size_t frames = size * 8 / bits_per_sample / channels; // two channels sound_data_t * data = new_sound_data(frames); sample_t * dest = data->samples; sample_t * end = data->samples + frames * 2; if(bits_per_sample == 8) while(!feof(file) && !ferror(file) && dest != end) { sample_t sample = ((sample_t)get_u8(file) - 128) << 8; *(dest++) = sample; if(channels == 1) *(dest++) = sample; } else while(!feof(file) && !ferror(file) && dest != end) { sample_t sample = get_s16(file); *(dest++) = sample; if(channels == 1) *(dest++) = sample; } if(dest != end) { delete_sound_data(data); data = NULL; ERROR("error or end of file before finished reading samples"); } return data; } //// OGG Loading ////////////////////////////////////////////////////////////// static sound_data_t * load_ogg(const char * filename) { sound_data_t * data = new_sound_data(0); int channels; int ret = stb_vorbis_decode_filename( (char *)filename, &channels, &data->samples); if(channels != 2) { delete_sound_data(data); data = NULL; ERROR("only 2-channel ogg files are supported"); } if(ret < 0) { delete_sound_data(data); data = NULL; ERROR("some sort of error in stb_vorbis_decode_filename"); } data->frames = ret; return data; } #if defined(DOKIDOKI_MACOSX) || defined(DOKIDOKI_MINGW) //// Portaudio //////////////////////////////////////////////////////////////// #include #define PA_CHECK(error) \ do \ { \ PaError PA_CHECK__err = (error); \ if(PA_CHECK__err != paNoError) \ { \ error_string = Pa_GetErrorText(PA_CHECK__err); \ return 0; \ } \ } while(0) static PaStream * stream = NULL; static int callback(const void *input, void *output, unsigned long frame_count, const PaStreamCallbackTimeInfo *time_info, PaStreamCallbackFlags status_flags, void *unused) { mix_into((sample_t *)output, frame_count); return paContinue; } static int init() { int c; for(c = 0; c < CHANNELS; c++) channels[c].data = NULL; PA_CHECK(Pa_Initialize()); PaError err; err = Pa_OpenDefaultStream(&stream, 0, 2, PORTAUDIO_SAMPLE_TYPE, SAMPLE_RATE, BUFFER_SIZE, callback, NULL); if(err != paNoError) { Pa_Terminate(); PA_CHECK(err); } err = Pa_StartStream(stream); if(err != paNoError) { Pa_Terminate(); PA_CHECK(err); } return 1; } static int uninit() { PA_CHECK(Pa_StopStream(stream)); PA_CHECK(Pa_CloseStream(stream)); PA_CHECK(Pa_Terminate()); return 1; } //// ALSA ///////////////////////////////////////////////////////////////////// #elif defined(DOKIDOKI_LINUX) #define ALSA_CHECK(error) \ do \ { \ int ALSA_CHECK__err = (error); \ if(ALSA_CHECK__err < 0) \ { \ error_string = snd_strerror(ALSA_CHECK__err); \ return 0; \ } \ } while(0) #include #include static snd_pcm_t * sound_device = NULL; static GLFWthread audio_thread = 0; static volatile int running = 0; static GLFWmutex running_mutex = NULL; static GLFWCALL void audio_loop(void *data) { sample_t buffer[BUFFER_SIZE*2]; glfwLockMutex(running_mutex); while(running) { glfwUnlockMutex(running_mutex); mix_into(buffer, BUFFER_SIZE); int err = snd_pcm_writei(sound_device, buffer, BUFFER_SIZE); if(err < 0) err = snd_pcm_recover(sound_device, err, 0); glfwLockMutex(running_mutex); if(err < 0) break; } glfwUnlockMutex(running_mutex); } static int init() { ALSA_CHECK(snd_pcm_open(&sound_device, "default", SND_PCM_STREAM_PLAYBACK, 0)); ALSA_CHECK(snd_pcm_set_params(sound_device, ALSA_SAMPLE_TYPE, // format SND_PCM_ACCESS_RW_INTERLEAVED, // access type 2, // channels SAMPLE_RATE, // rate 1, // resample (unsigned)((float)BUFFER_SIZE/SAMPLE_RATE*1000000*20))); // latency in us ALSA_CHECK(snd_pcm_prepare(sound_device)); CHECK(running_mutex = glfwCreateMutex(), "audio mutex creation failed"); running = 1; audio_thread = glfwCreateThread(audio_loop, NULL); return 1; } static int uninit() { glfwLockMutex(running_mutex); running = 0; glfwUnlockMutex(running_mutex); glfwWaitThread(audio_thread, GLFW_WAIT); ALSA_CHECK(snd_pcm_close(sound_device)); sound_device = NULL; return 1; } //// No Audio ///////////////////////////////////////////////////////////////// #else static int init() { return 1; } static int uninit() { return 1; } #endif //// Lua ////////////////////////////////////////////////////////////////////// #include #include #include #define LUA_CHECK(L, condition, message) \ do \ { \ if(!(condition)) \ { \ lua_pushnil(L); \ lua_pushstring(L, message); \ return 2; \ } \ } while(0) #define LUA_ERROR(L, message) LUA_CHECK(L, 0, message) static int mixer__initted = 0; static void check_initted(lua_State *L) { if(!mixer__initted) luaL_error(L, "please call mixer.init() first"); } static int mixer__init(lua_State *L) { if(!mixer__initted) { mixer__initted = init(); LUA_CHECK(L, mixer__initted, error_string); } lua_pushboolean(L, 1); return 1; } static int mixer__uninit(lua_State *L) { if(mixer__initted) { mixer__initted = 0; LUA_CHECK(L, uninit(), error_string); } lua_pushboolean(L, 1); return 1; } static int generic_sound_loader( lua_State *L, sound_data_t * (*loader) (const char *filename)) { const char * filename = luaL_checkstring(L, 1); sound_data_t * sound = loader(filename); if(sound == NULL) { lua_pushnil(L); lua_pushstring(L, error_string); return 2; } else { *(sound_data_t **)lua_newuserdata(L, sizeof(sound)) = sound; luaL_getmetatable(L, "mixer.sound_effect"); lua_setmetatable(L, -2); return 1; } } static int mixer__load_wav(lua_State *L) { return generic_sound_loader(L, load_wav); } static int mixer__load_ogg(lua_State *L) { return generic_sound_loader(L, load_ogg); } static int mixer__channel_fade_to(lua_State *L) { check_initted(L); int channel = luaL_checkint(L, 1); float duration = luaL_checknumber(L, 2); float left = luaL_checknumber(L, 3); float right = luaL_optnumber(L, 4, left); luaL_argcheck(L, channel_is_valid(channel), 1, "invalid channel given"); luaL_argcheck(L, duration >= 0, 1, "invalid duration given"); channel_fade_to(channel, duration, left, right); return 0; } static int mixer__channel_stop(lua_State *L) { check_initted(L); int channel = luaL_checkint(L, 1); luaL_argcheck(L, channel_is_valid(channel), 1, "invalid channel given"); channel_stop(channel); return 0; } static sound_data_t * check_sound_effect(lua_State *L, int index) { return *(sound_data_t **)luaL_checkudata(L, index, "mixer.sound_effect"); } static int sound_effect__play(lua_State *L) { check_initted(L); sound_data_t * sound = check_sound_effect(L, 1); float left = luaL_optnumber(L, 2, 1.0); float right = luaL_optnumber(L, 3, left); int loop = luaL_optint(L, 4, 1); int channel = play_sound_effect(sound, left, right, loop); if(channel < 0) lua_pushnil(L); else lua_pushnumber(L, channel); return 1; } static const luaL_reg sound_effect_lib[] = { {"play", sound_effect__play}, {NULL, NULL} }; static const luaL_Reg mixer_lib[] = { {"init", mixer__init}, {"uninit", mixer__uninit}, {"load_wav", mixer__load_wav}, {"load_ogg", mixer__load_ogg}, {"channel_fade_to", mixer__channel_fade_to}, {"channel_stop", mixer__channel_stop}, {NULL, NULL} }; int luaopen_mixer(lua_State *L) { lua_newuserdata(L, 0); lua_newtable(L); lua_pushcfunction(L, mixer__uninit); lua_setfield(L, -2, "__gc"); lua_setmetatable(L, -2); lua_setfield(L, LUA_REGISTRYINDEX, "mixer.uninitializer"); luaL_newmetatable(L, "mixer.sound_effect"); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); luaL_register(L, NULL, sound_effect_lib); lua_newtable(L); luaL_register(L, NULL, mixer_lib); return 1; } dokidoki-support/module_to_c.lua000077500000000000000000000011451147057410300173670ustar00rootroot00000000000000#!/usr/bin/env lua modulename = ... assert(modulename) dash = modulename:find('%-') cname = modulename:sub((dash or 0)+1):gsub('%.', '_') functionname = 'luaopen_' .. cname io.write('#include \n') io.write('#include \n') io.write('int ' .. functionname .. '(lua_State *L){\n') size = 0 io.write(' static const char data[] = {') for c in function () return io.read(1) end do io.write(c:byte() .. ',') size = size + 1 end io.write('0};\n') io.write([[ luaL_loadbuffer(L, data, ]] .. size .. [[, "]] .. modulename .. [[") && lua_error(L); lua_call(L, 0, 1); return 1; }]]) dokidoki-support/stb_image.c000066400000000000000000003726701147057410300165040ustar00rootroot00000000000000/* stbi-1.18 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c when you control the images you're loading QUICK NOTES: Primarily of interest to game developers and other people who can avoid problematic images and only need the trivial interface JPEG baseline (no JPEG progressive, no oddball channel decimations) PNG 8-bit only BMP non-1bpp, non-RLE TGA (not sure what subset, if a subset) PSD (composited view only, no extra channels) HDR (radiance rgbE format) writes BMP,TGA (define STBI_NO_WRITE to remove code) decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code) supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD) TODO: stbi_info_* history: 1.18 fix a threading bug (local mutable static) 1.17 support interlaced PNG 1.16 major bugfix - convert_format converted one too many pixels 1.15 initialize some fields for thread safety 1.14 fix threadsafe conversion bug; header-file-only version (#define STBI_HEADER_FILE_ONLY before including) 1.13 threadsafe 1.12 const qualifiers in the API 1.11 Support installable IDCT, colorspace conversion routines 1.10 Fixes for 64-bit (don't use "unsigned long") optimized upsampling by Fabian "ryg" Giesen 1.09 Fix format-conversion for PSD code (bad global variables!) 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz 1.07 attempt to fix C++ warning/errors again 1.06 attempt to fix C++ warning/errors again 1.05 fix TGA loading to return correct *comp and use good luminance calc 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR 1.02 support for (subset of) HDR files, float interface for preferred access to them 1.01 fix bug: possible bug in handling right-side up bmps... not sure fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all 1.00 interface to zlib that skips zlib header 0.99 correct handling of alpha in palette 0.98 TGA loader by lonesock; dynamically add loaders (untested) 0.97 jpeg errors on too large a file; also catch another malloc failure 0.96 fix detection of invalid v value - particleman@mollyrocket forum 0.95 during header scan, seek to markers in case of padding 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same 0.93 handle jpegtran output; verbose errors 0.92 read 4,8,16,24,32-bit BMP files of several formats 0.91 output 24-bit Windows 3.0 BMP files 0.90 fix a few more warnings; bump version number to approach 1.0 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd 0.60 fix compiling as c++ 0.59 fix warnings: merge Dave Moore's -Wall fixes 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available 0.56 fix bug: zlib uncompressed mode len vs. nlen 0.55 fix bug: restart_interval not initialized to 0 0.54 allow NULL for 'int *comp' 0.53 fix bug in png 3->4; speedup png decoding 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments 0.51 obey req_comp requests, 1-component jpegs return as 1-component, on 'test' only check type, not whether we support this variant */ #ifndef STBI_INCLUDE_STB_IMAGE_H #define STBI_INCLUDE_STB_IMAGE_H //// begin header file //////////////////////////////////////////////////// // // Limitations: // - no progressive/interlaced support (jpeg, png) // - 8-bit samples only (jpeg, png) // - not threadsafe // - channel subsampling of at most 2 in each dimension (jpeg) // - no delayed line count (jpeg) -- IJG doesn't support either // // Basic usage (see HDR discussion below): // int x,y,n; // unsigned char *data = stbi_load(filename, &x, &y, &n, 0); // // ... process data if not NULL ... // // ... x = width, y = height, n = # 8-bit components per pixel ... // // ... replace '0' with '1'..'4' to force that many components per pixel // stbi_image_free(data) // // Standard parameters: // int *x -- outputs image width in pixels // int *y -- outputs image height in pixels // int *comp -- outputs # of image components in image file // int req_comp -- if non-zero, # of image components requested in result // // The return value from an image loader is an 'unsigned char *' which points // to the pixel data. The pixel data consists of *y scanlines of *x pixels, // with each pixel consisting of N interleaved 8-bit components; the first // pixel pointed to is top-left-most in the image. There is no padding between // image scanlines or between pixels, regardless of format. The number of // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise. // If req_comp is non-zero, *comp has the number of components that _would_ // have been output otherwise. E.g. if you set req_comp to 4, you will always // get RGBA output, but you can check *comp to easily see if it's opaque. // // An output image with N components has the following components interleaved // in this order in each pixel: // // N=#comp components // 1 grey // 2 grey, alpha // 3 red, green, blue // 4 red, green, blue, alpha // // If image loading fails for any reason, the return value will be NULL, // and *x, *y, *comp will be unchanged. The function stbi_failure_reason() // can be queried for an extremely brief, end-user unfriendly explanation // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly // more user-friendly ones. // // Paletted PNG and BMP images are automatically depalettized. // // // =========================================================================== // // HDR image support (disable by defining STBI_NO_HDR) // // stb_image now supports loading HDR images in general, and currently // the Radiance .HDR file format, although the support is provided // generically. You can still load any file through the existing interface; // if you attempt to load an HDR file, it will be automatically remapped to // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; // both of these constants can be reconfigured through this interface: // // stbi_hdr_to_ldr_gamma(2.2f); // stbi_hdr_to_ldr_scale(1.0f); // // (note, do not use _inverse_ constants; stbi_image will invert them // appropriately). // // Additionally, there is a new, parallel interface for loading files as // (linear) floats to preserve the full dynamic range: // // float *data = stbi_loadf(filename, &x, &y, &n, 0); // // If you load LDR images through this interface, those images will // be promoted to floating point values, run through the inverse of // constants corresponding to the above: // // stbi_ldr_to_hdr_scale(1.0f); // stbi_ldr_to_hdr_gamma(2.2f); // // Finally, given a filename (or an open file or memory block--see header // file for details) containing image data, you can query for the "most // appropriate" interface to use (that is, whether the image is HDR or // not), using: // // stbi_is_hdr(char *filename); #ifndef STBI_NO_STDIO #include #endif #define STBI_VERSION 1 enum { STBI_default = 0, // only used for req_comp STBI_grey = 1, STBI_grey_alpha = 2, STBI_rgb = 3, STBI_rgb_alpha = 4, }; typedef unsigned char stbi_uc; #ifdef __cplusplus extern "C" { #endif // WRITING API #if !defined(STBI_NO_WRITE) && !defined(STBI_NO_STDIO) // write a BMP/TGA file given tightly packed 'comp' channels (no padding, nor bmp-stride-padding) // (you must include the appropriate extension in the filename). // returns TRUE on success, FALSE if couldn't open file, error writing file extern int stbi_write_bmp (char const *filename, int x, int y, int comp, void *data); extern int stbi_write_tga (char const *filename, int x, int y, int comp, void *data); #endif // PRIMARY API - works on images of any type // load image by filename, open file, or memory buffer #ifndef STBI_NO_STDIO extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp); extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); #endif extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); // for stbi_load_from_file, file pointer is left pointing immediately after image #ifndef STBI_NO_HDR #ifndef STBI_NO_STDIO extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp); extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); #endif extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); extern void stbi_hdr_to_ldr_gamma(float gamma); extern void stbi_hdr_to_ldr_scale(float scale); extern void stbi_ldr_to_hdr_gamma(float gamma); extern void stbi_ldr_to_hdr_scale(float scale); #endif // STBI_NO_HDR // get a VERY brief reason for failure // NOT THREADSAFE extern char *stbi_failure_reason (void); // free the loaded image -- this is just free() extern void stbi_image_free (void *retval_from_stbi_load); // get image dimensions & components without fully decoding extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len); #ifndef STBI_NO_STDIO extern int stbi_info (char const *filename, int *x, int *y, int *comp); extern int stbi_is_hdr (char const *filename); extern int stbi_is_hdr_from_file(FILE *f); #endif // ZLIB client - used by PNG, available for other purposes extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen); extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen); extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen); extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); // TYPE-SPECIFIC ACCESS // is it a jpeg? extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len); extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); #ifndef STBI_NO_STDIO extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp); extern int stbi_jpeg_test_file (FILE *f); extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp); extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp); #endif // is it a png? extern int stbi_png_test_memory (stbi_uc const *buffer, int len); extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp); #ifndef STBI_NO_STDIO extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp); extern int stbi_png_info (char const *filename, int *x, int *y, int *comp); extern int stbi_png_test_file (FILE *f); extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp); #endif // is it a bmp? extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len); extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp); extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); #ifndef STBI_NO_STDIO extern int stbi_bmp_test_file (FILE *f); extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); #endif // is it a tga? extern int stbi_tga_test_memory (stbi_uc const *buffer, int len); extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp); extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); #ifndef STBI_NO_STDIO extern int stbi_tga_test_file (FILE *f); extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); #endif // is it a psd? extern int stbi_psd_test_memory (stbi_uc const *buffer, int len); extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp); extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); #ifndef STBI_NO_STDIO extern int stbi_psd_test_file (FILE *f); extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); #endif // is it an hdr? extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len); extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp); extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); #ifndef STBI_NO_STDIO extern int stbi_hdr_test_file (FILE *f); extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); #endif // define new loaders typedef struct { int (*test_memory)(stbi_uc const *buffer, int len); stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); #ifndef STBI_NO_STDIO int (*test_file)(FILE *f); stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp); #endif } stbi_loader; // register a loader by filling out the above structure (you must defined ALL functions) // returns 1 if added or already added, 0 if not added (too many loaders) // NOT THREADSAFE extern int stbi_register_loader(stbi_loader *loader); // define faster low-level operations (typically SIMD support) #if STBI_SIMD typedef void (*stbi_idct_8x8)(uint8 *out, int out_stride, short data[64], unsigned short *dequantize); // compute an integer IDCT on "input" // input[x] = data[x] * dequantize[x] // write results to 'out': 64 samples, each run of 8 spaced by 'out_stride' // CLAMP results to 0..255 typedef void (*stbi_YCbCr_to_RGB_run)(uint8 *output, uint8 const *y, uint8 const *cb, uint8 const *cr, int count, int step); // compute a conversion from YCbCr to RGB // 'count' pixels // write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B // y: Y input channel // cb: Cb input channel; scale/biased to be 0..255 // cr: Cr input channel; scale/biased to be 0..255 extern void stbi_install_idct(stbi_idct_8x8 func); extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func); #endif // STBI_SIMD #ifdef __cplusplus } #endif // // //// end header file ///////////////////////////////////////////////////// #endif // STBI_INCLUDE_STB_IMAGE_H #ifndef STBI_HEADER_FILE_ONLY #ifndef STBI_NO_HDR #include // ldexp #include // strcmp #endif #ifndef STBI_NO_STDIO #include #endif #include #include #include #include #ifndef _MSC_VER #ifdef __cplusplus #define __forceinline inline #else #define __forceinline #endif #endif // implementation: typedef unsigned char uint8; typedef unsigned short uint16; typedef signed short int16; typedef unsigned int uint32; typedef signed int int32; typedef unsigned int uint; // should produce compiler error if size is wrong typedef unsigned char validate_uint32[sizeof(uint32)==4]; #if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE) #define STBI_NO_WRITE #endif ////////////////////////////////////////////////////////////////////////////// // // Generic API that works on all image types // // this is not threadsafe static char *failure_reason; char *stbi_failure_reason(void) { return failure_reason; } static int e(char *str) { failure_reason = str; return 0; } #ifdef STBI_NO_FAILURE_STRINGS #define e(x,y) 0 #elif defined(STBI_FAILURE_USERMSG) #define e(x,y) e(y) #else #define e(x,y) e(x) #endif #define epf(x,y) ((float *) (e(x,y)?NULL:NULL)) #define epuc(x,y) ((unsigned char *) (e(x,y)?NULL:NULL)) void stbi_image_free(void *retval_from_stbi_load) { free(retval_from_stbi_load); } #define MAX_LOADERS 32 stbi_loader *loaders[MAX_LOADERS]; static int max_loaders = 0; int stbi_register_loader(stbi_loader *loader) { int i; for (i=0; i < MAX_LOADERS; ++i) { // already present? if (loaders[i] == loader) return 1; // end of the list? if (loaders[i] == NULL) { loaders[i] = loader; max_loaders = i+1; return 1; } } // no room for it return 0; } #ifndef STBI_NO_HDR static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp); static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp); #endif #ifndef STBI_NO_STDIO unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp) { FILE *f = fopen(filename, "rb"); unsigned char *result; if (!f) return epuc("can't fopen", "Unable to open file"); result = stbi_load_from_file(f,x,y,comp,req_comp); fclose(f); return result; } unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) { int i; if (stbi_jpeg_test_file(f)) return stbi_jpeg_load_from_file(f,x,y,comp,req_comp); if (stbi_png_test_file(f)) return stbi_png_load_from_file(f,x,y,comp,req_comp); if (stbi_bmp_test_file(f)) return stbi_bmp_load_from_file(f,x,y,comp,req_comp); if (stbi_psd_test_file(f)) return stbi_psd_load_from_file(f,x,y,comp,req_comp); #ifndef STBI_NO_HDR if (stbi_hdr_test_file(f)) { float *hdr = stbi_hdr_load_from_file(f, x,y,comp,req_comp); return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp); } #endif for (i=0; i < max_loaders; ++i) if (loaders[i]->test_file(f)) return loaders[i]->load_from_file(f,x,y,comp,req_comp); // test tga last because it's a crappy test! if (stbi_tga_test_file(f)) return stbi_tga_load_from_file(f,x,y,comp,req_comp); return epuc("unknown image type", "Image not of any known type, or corrupt"); } #endif unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { int i; if (stbi_jpeg_test_memory(buffer,len)) return stbi_jpeg_load_from_memory(buffer,len,x,y,comp,req_comp); if (stbi_png_test_memory(buffer,len)) return stbi_png_load_from_memory(buffer,len,x,y,comp,req_comp); if (stbi_bmp_test_memory(buffer,len)) return stbi_bmp_load_from_memory(buffer,len,x,y,comp,req_comp); if (stbi_psd_test_memory(buffer,len)) return stbi_psd_load_from_memory(buffer,len,x,y,comp,req_comp); #ifndef STBI_NO_HDR if (stbi_hdr_test_memory(buffer, len)) { float *hdr = stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp); return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp); } #endif for (i=0; i < max_loaders; ++i) if (loaders[i]->test_memory(buffer,len)) return loaders[i]->load_from_memory(buffer,len,x,y,comp,req_comp); // test tga last because it's a crappy test! if (stbi_tga_test_memory(buffer,len)) return stbi_tga_load_from_memory(buffer,len,x,y,comp,req_comp); return epuc("unknown image type", "Image not of any known type, or corrupt"); } #ifndef STBI_NO_HDR #ifndef STBI_NO_STDIO float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp) { FILE *f = fopen(filename, "rb"); float *result; if (!f) return epf("can't fopen", "Unable to open file"); result = stbi_loadf_from_file(f,x,y,comp,req_comp); fclose(f); return result; } float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) { unsigned char *data; #ifndef STBI_NO_HDR if (stbi_hdr_test_file(f)) return stbi_hdr_load_from_file(f,x,y,comp,req_comp); #endif data = stbi_load_from_file(f, x, y, comp, req_comp); if (data) return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp); return epf("unknown image type", "Image not of any known type, or corrupt"); } #endif float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { stbi_uc *data; #ifndef STBI_NO_HDR if (stbi_hdr_test_memory(buffer, len)) return stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp); #endif data = stbi_load_from_memory(buffer, len, x, y, comp, req_comp); if (data) return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp); return epf("unknown image type", "Image not of any known type, or corrupt"); } #endif // these is-hdr-or-not is defined independent of whether STBI_NO_HDR is // defined, for API simplicity; if STBI_NO_HDR is defined, it always // reports false! int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len) { #ifndef STBI_NO_HDR return stbi_hdr_test_memory(buffer, len); #else return 0; #endif } #ifndef STBI_NO_STDIO extern int stbi_is_hdr (char const *filename) { FILE *f = fopen(filename, "rb"); int result=0; if (f) { result = stbi_is_hdr_from_file(f); fclose(f); } return result; } extern int stbi_is_hdr_from_file(FILE *f) { #ifndef STBI_NO_HDR return stbi_hdr_test_file(f); #else return 0; #endif } #endif // @TODO: get image dimensions & components without fully decoding #ifndef STBI_NO_STDIO extern int stbi_info (char const *filename, int *x, int *y, int *comp); extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); #endif extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); #ifndef STBI_NO_HDR static float h2l_gamma_i=1.0f/2.2f, h2l_scale_i=1.0f; static float l2h_gamma=2.2f, l2h_scale=1.0f; void stbi_hdr_to_ldr_gamma(float gamma) { h2l_gamma_i = 1/gamma; } void stbi_hdr_to_ldr_scale(float scale) { h2l_scale_i = 1/scale; } void stbi_ldr_to_hdr_gamma(float gamma) { l2h_gamma = gamma; } void stbi_ldr_to_hdr_scale(float scale) { l2h_scale = scale; } #endif ////////////////////////////////////////////////////////////////////////////// // // Common code used by all image loaders // enum { SCAN_load=0, SCAN_type, SCAN_header, }; typedef struct { uint32 img_x, img_y; int img_n, img_out_n; #ifndef STBI_NO_STDIO FILE *img_file; #endif uint8 *img_buffer, *img_buffer_end; } stbi; #ifndef STBI_NO_STDIO static void start_file(stbi *s, FILE *f) { s->img_file = f; } #endif static void start_mem(stbi *s, uint8 const *buffer, int len) { #ifndef STBI_NO_STDIO s->img_file = NULL; #endif s->img_buffer = (uint8 *) buffer; s->img_buffer_end = (uint8 *) buffer+len; } __forceinline static int get8(stbi *s) { #ifndef STBI_NO_STDIO if (s->img_file) { int c = fgetc(s->img_file); return c == EOF ? 0 : c; } #endif if (s->img_buffer < s->img_buffer_end) return *s->img_buffer++; return 0; } __forceinline static int at_eof(stbi *s) { #ifndef STBI_NO_STDIO if (s->img_file) return feof(s->img_file); #endif return s->img_buffer >= s->img_buffer_end; } __forceinline static uint8 get8u(stbi *s) { return (uint8) get8(s); } static void skip(stbi *s, int n) { #ifndef STBI_NO_STDIO if (s->img_file) fseek(s->img_file, n, SEEK_CUR); else #endif s->img_buffer += n; } static int get16(stbi *s) { int z = get8(s); return (z << 8) + get8(s); } static uint32 get32(stbi *s) { uint32 z = get16(s); return (z << 16) + get16(s); } static int get16le(stbi *s) { int z = get8(s); return z + (get8(s) << 8); } static uint32 get32le(stbi *s) { uint32 z = get16le(s); return z + (get16le(s) << 16); } static void getn(stbi *s, stbi_uc *buffer, int n) { #ifndef STBI_NO_STDIO if (s->img_file) { fread(buffer, 1, n, s->img_file); return; } #endif memcpy(buffer, s->img_buffer, n); s->img_buffer += n; } ////////////////////////////////////////////////////////////////////////////// // // generic converter from built-in img_n to req_comp // individual types do this automatically as much as possible (e.g. jpeg // does all cases internally since it needs to colorspace convert anyway, // and it never has alpha, so very few cases ). png can automatically // interleave an alpha=255 channel, but falls back to this for other cases // // assume data buffer is malloced, so malloc a new one and free that one // only failure mode is malloc failing static uint8 compute_y(int r, int g, int b) { return (uint8) (((r*77) + (g*150) + (29*b)) >> 8); } static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y) { int i,j; unsigned char *good; if (req_comp == img_n) return data; assert(req_comp >= 1 && req_comp <= 4); good = (unsigned char *) malloc(req_comp * x * y); if (good == NULL) { free(data); return epuc("outofmem", "Out of memory"); } for (j=0; j < (int) y; ++j) { unsigned char *src = data + j * x * img_n ; unsigned char *dest = good + j * x * req_comp; #define COMBO(a,b) ((a)*8+(b)) #define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) // convert source image with img_n components to one with req_comp components; // avoid switch per pixel, so use switch per scanline and massive macros switch(COMBO(img_n, req_comp)) { CASE(1,2) dest[0]=src[0], dest[1]=255; break; CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break; CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break; CASE(2,1) dest[0]=src[0]; break; CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break; CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break; CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break; CASE(3,1) dest[0]=compute_y(src[0],src[1],src[2]); break; CASE(3,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = 255; break; CASE(4,1) dest[0]=compute_y(src[0],src[1],src[2]); break; CASE(4,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break; CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break; default: assert(0); } #undef CASE } free(data); return good; } #ifndef STBI_NO_HDR static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp) { int i,k,n; float *output = (float *) malloc(x * y * comp * sizeof(float)); if (output == NULL) { free(data); return epf("outofmem", "Out of memory"); } // compute number of non-alpha components if (comp & 1) n = comp; else n = comp-1; for (i=0; i < x*y; ++i) { for (k=0; k < n; ++k) { output[i*comp + k] = (float) pow(data[i*comp+k]/255.0f, l2h_gamma) * l2h_scale; } if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f; } free(data); return output; } #define float2int(x) ((int) (x)) static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp) { int i,k,n; stbi_uc *output = (stbi_uc *) malloc(x * y * comp); if (output == NULL) { free(data); return epuc("outofmem", "Out of memory"); } // compute number of non-alpha components if (comp & 1) n = comp; else n = comp-1; for (i=0; i < x*y; ++i) { for (k=0; k < n; ++k) { float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f; if (z < 0) z = 0; if (z > 255) z = 255; output[i*comp + k] = float2int(z); } if (k < comp) { float z = data[i*comp+k] * 255 + 0.5f; if (z < 0) z = 0; if (z > 255) z = 255; output[i*comp + k] = float2int(z); } } free(data); return output; } #endif ////////////////////////////////////////////////////////////////////////////// // // "baseline" JPEG/JFIF decoder (not actually fully baseline implementation) // // simple implementation // - channel subsampling of at most 2 in each dimension // - doesn't support delayed output of y-dimension // - simple interface (only one output format: 8-bit interleaved RGB) // - doesn't try to recover corrupt jpegs // - doesn't allow partial loading, loading multiple at once // - still fast on x86 (copying globals into locals doesn't help x86) // - allocates lots of intermediate memory (full size of all components) // - non-interleaved case requires this anyway // - allows good upsampling (see next) // high-quality // - upsampled channels are bilinearly interpolated, even across blocks // - quality integer IDCT derived from IJG's 'slow' // performance // - fast huffman; reasonable integer IDCT // - uses a lot of intermediate memory, could cache poorly // - load http://nothings.org/remote/anemones.jpg 3 times on 2.8Ghz P4 // stb_jpeg: 1.34 seconds (MSVC6, default release build) // stb_jpeg: 1.06 seconds (MSVC6, processor = Pentium Pro) // IJL11.dll: 1.08 seconds (compiled by intel) // IJG 1998: 0.98 seconds (MSVC6, makefile provided by IJG) // IJG 1998: 0.95 seconds (MSVC6, makefile + proc=PPro) // huffman decoding acceleration #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache typedef struct { uint8 fast[1 << FAST_BITS]; // weirdly, repacking this into AoS is a 10% speed loss, instead of a win uint16 code[256]; uint8 values[256]; uint8 size[257]; unsigned int maxcode[18]; int delta[17]; // old 'firstsymbol' - old 'firstcode' } huffman; typedef struct { #if STBI_SIMD unsigned short dequant2[4][64]; #endif stbi s; huffman huff_dc[4]; huffman huff_ac[4]; uint8 dequant[4][64]; // sizes for components, interleaved MCUs int img_h_max, img_v_max; int img_mcu_x, img_mcu_y; int img_mcu_w, img_mcu_h; // definition of jpeg image component struct { int id; int h,v; int tq; int hd,ha; int dc_pred; int x,y,w2,h2; uint8 *data; void *raw_data; uint8 *linebuf; } img_comp[4]; uint32 code_buffer; // jpeg entropy-coded buffer int code_bits; // number of valid bits unsigned char marker; // marker seen while filling entropy buffer int nomore; // flag if we saw a marker so must stop int scan_n, order[4]; int restart_interval, todo; } jpeg; static int build_huffman(huffman *h, int *count) { int i,j,k=0,code; // build size list for each symbol (from JPEG spec) for (i=0; i < 16; ++i) for (j=0; j < count[i]; ++j) h->size[k++] = (uint8) (i+1); h->size[k] = 0; // compute actual symbols (from jpeg spec) code = 0; k = 0; for(j=1; j <= 16; ++j) { // compute delta to add to code to compute symbol id h->delta[j] = k - code; if (h->size[k] == j) { while (h->size[k] == j) h->code[k++] = (uint16) (code++); if (code-1 >= (1 << j)) return e("bad code lengths","Corrupt JPEG"); } // compute largest code + 1 for this size, preshifted as needed later h->maxcode[j] = code << (16-j); code <<= 1; } h->maxcode[j] = 0xffffffff; // build non-spec acceleration table; 255 is flag for not-accelerated memset(h->fast, 255, 1 << FAST_BITS); for (i=0; i < k; ++i) { int s = h->size[i]; if (s <= FAST_BITS) { int c = h->code[i] << (FAST_BITS-s); int m = 1 << (FAST_BITS-s); for (j=0; j < m; ++j) { h->fast[c+j] = (uint8) i; } } } return 1; } static void grow_buffer_unsafe(jpeg *j) { do { int b = j->nomore ? 0 : get8(&j->s); if (b == 0xff) { int c = get8(&j->s); if (c != 0) { j->marker = (unsigned char) c; j->nomore = 1; return; } } j->code_buffer = (j->code_buffer << 8) | b; j->code_bits += 8; } while (j->code_bits <= 24); } // (1 << n) - 1 static uint32 bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535}; // decode a jpeg huffman value from the bitstream __forceinline static int decode(jpeg *j, huffman *h) { unsigned int temp; int c,k; if (j->code_bits < 16) grow_buffer_unsafe(j); // look at the top FAST_BITS and determine what symbol ID it is, // if the code is <= FAST_BITS c = (j->code_buffer >> (j->code_bits - FAST_BITS)) & ((1 << FAST_BITS)-1); k = h->fast[c]; if (k < 255) { if (h->size[k] > j->code_bits) return -1; j->code_bits -= h->size[k]; return h->values[k]; } // naive test is to shift the code_buffer down so k bits are // valid, then test against maxcode. To speed this up, we've // preshifted maxcode left so that it has (16-k) 0s at the // end; in other words, regardless of the number of bits, it // wants to be compared against something shifted to have 16; // that way we don't need to shift inside the loop. if (j->code_bits < 16) temp = (j->code_buffer << (16 - j->code_bits)) & 0xffff; else temp = (j->code_buffer >> (j->code_bits - 16)) & 0xffff; for (k=FAST_BITS+1 ; ; ++k) if (temp < h->maxcode[k]) break; if (k == 17) { // error! code not found j->code_bits -= 16; return -1; } if (k > j->code_bits) return -1; // convert the huffman code to the symbol id c = ((j->code_buffer >> (j->code_bits - k)) & bmask[k]) + h->delta[k]; assert((((j->code_buffer) >> (j->code_bits - h->size[c])) & bmask[h->size[c]]) == h->code[c]); // convert the id to a symbol j->code_bits -= k; return h->values[c]; } // combined JPEG 'receive' and JPEG 'extend', since baseline // always extends everything it receives. __forceinline static int extend_receive(jpeg *j, int n) { unsigned int m = 1 << (n-1); unsigned int k; if (j->code_bits < n) grow_buffer_unsafe(j); k = (j->code_buffer >> (j->code_bits - n)) & bmask[n]; j->code_bits -= n; // the following test is probably a random branch that won't // predict well. I tried to table accelerate it but failed. // maybe it's compiling as a conditional move? if (k < m) return (-1 << n) + k + 1; else return k; } // given a value that's at position X in the zigzag stream, // where does it appear in the 8x8 matrix coded as row-major? static uint8 dezigzag[64+15] = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, // let corrupt input sample past end 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }; // decode one 64-entry block-- static int decode_block(jpeg *j, short data[64], huffman *hdc, huffman *hac, int b) { int diff,dc,k; int t = decode(j, hdc); if (t < 0) return e("bad huffman code","Corrupt JPEG"); // 0 all the ac values now so we can do it 32-bits at a time memset(data,0,64*sizeof(data[0])); diff = t ? extend_receive(j, t) : 0; dc = j->img_comp[b].dc_pred + diff; j->img_comp[b].dc_pred = dc; data[0] = (short) dc; // decode AC components, see JPEG spec k = 1; do { int r,s; int rs = decode(j, hac); if (rs < 0) return e("bad huffman code","Corrupt JPEG"); s = rs & 15; r = rs >> 4; if (s == 0) { if (rs != 0xf0) break; // end block k += 16; } else { k += r; // decode into unzigzag'd location data[dezigzag[k++]] = (short) extend_receive(j,s); } } while (k < 64); return 1; } // take a -128..127 value and clamp it and convert to 0..255 __forceinline static uint8 clamp(int x) { x += 128; // trick to use a single test to catch both cases if ((unsigned int) x > 255) { if (x < 0) return 0; if (x > 255) return 255; } return (uint8) x; } #define f2f(x) (int) (((x) * 4096 + 0.5)) #define fsh(x) ((x) << 12) // derived from jidctint -- DCT_ISLOW #define IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \ int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \ p2 = s2; \ p3 = s6; \ p1 = (p2+p3) * f2f(0.5411961f); \ t2 = p1 + p3*f2f(-1.847759065f); \ t3 = p1 + p2*f2f( 0.765366865f); \ p2 = s0; \ p3 = s4; \ t0 = fsh(p2+p3); \ t1 = fsh(p2-p3); \ x0 = t0+t3; \ x3 = t0-t3; \ x1 = t1+t2; \ x2 = t1-t2; \ t0 = s7; \ t1 = s5; \ t2 = s3; \ t3 = s1; \ p3 = t0+t2; \ p4 = t1+t3; \ p1 = t0+t3; \ p2 = t1+t2; \ p5 = (p3+p4)*f2f( 1.175875602f); \ t0 = t0*f2f( 0.298631336f); \ t1 = t1*f2f( 2.053119869f); \ t2 = t2*f2f( 3.072711026f); \ t3 = t3*f2f( 1.501321110f); \ p1 = p5 + p1*f2f(-0.899976223f); \ p2 = p5 + p2*f2f(-2.562915447f); \ p3 = p3*f2f(-1.961570560f); \ p4 = p4*f2f(-0.390180644f); \ t3 += p1+p4; \ t2 += p2+p3; \ t1 += p2+p4; \ t0 += p1+p3; #if !STBI_SIMD // .344 seconds on 3*anemones.jpg static void idct_block(uint8 *out, int out_stride, short data[64], uint8 *dequantize) { int i,val[64],*v=val; uint8 *o,*dq = dequantize; short *d = data; // columns for (i=0; i < 8; ++i,++d,++dq, ++v) { // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0 && d[40]==0 && d[48]==0 && d[56]==0) { // no shortcut 0 seconds // (1|2|3|4|5|6|7)==0 0 seconds // all separate -0.047 seconds // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds int dcterm = d[0] * dq[0] << 2; v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm; } else { IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24], d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56]) // constants scaled things up by 1<<12; let's bring them back // down, but keep 2 extra bits of precision x0 += 512; x1 += 512; x2 += 512; x3 += 512; v[ 0] = (x0+t3) >> 10; v[56] = (x0-t3) >> 10; v[ 8] = (x1+t2) >> 10; v[48] = (x1-t2) >> 10; v[16] = (x2+t1) >> 10; v[40] = (x2-t1) >> 10; v[24] = (x3+t0) >> 10; v[32] = (x3-t0) >> 10; } } for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) { // no fast case since the first 1D IDCT spread components out IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]) // constants scaled things up by 1<<12, plus we had 1<<2 from first // loop, plus horizontal and vertical each scale by sqrt(8) so together // we've got an extra 1<<3, so 1<<17 total we need to remove. x0 += 65536; x1 += 65536; x2 += 65536; x3 += 65536; o[0] = clamp((x0+t3) >> 17); o[7] = clamp((x0-t3) >> 17); o[1] = clamp((x1+t2) >> 17); o[6] = clamp((x1-t2) >> 17); o[2] = clamp((x2+t1) >> 17); o[5] = clamp((x2-t1) >> 17); o[3] = clamp((x3+t0) >> 17); o[4] = clamp((x3-t0) >> 17); } } #else static void idct_block(uint8 *out, int out_stride, short data[64], unsigned short *dequantize) { int i,val[64],*v=val; uint8 *o; unsigned short *dq = dequantize; short *d = data; // columns for (i=0; i < 8; ++i,++d,++dq, ++v) { // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0 && d[40]==0 && d[48]==0 && d[56]==0) { // no shortcut 0 seconds // (1|2|3|4|5|6|7)==0 0 seconds // all separate -0.047 seconds // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds int dcterm = d[0] * dq[0] << 2; v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm; } else { IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24], d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56]) // constants scaled things up by 1<<12; let's bring them back // down, but keep 2 extra bits of precision x0 += 512; x1 += 512; x2 += 512; x3 += 512; v[ 0] = (x0+t3) >> 10; v[56] = (x0-t3) >> 10; v[ 8] = (x1+t2) >> 10; v[48] = (x1-t2) >> 10; v[16] = (x2+t1) >> 10; v[40] = (x2-t1) >> 10; v[24] = (x3+t0) >> 10; v[32] = (x3-t0) >> 10; } } for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) { // no fast case since the first 1D IDCT spread components out IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]) // constants scaled things up by 1<<12, plus we had 1<<2 from first // loop, plus horizontal and vertical each scale by sqrt(8) so together // we've got an extra 1<<3, so 1<<17 total we need to remove. x0 += 65536; x1 += 65536; x2 += 65536; x3 += 65536; o[0] = clamp((x0+t3) >> 17); o[7] = clamp((x0-t3) >> 17); o[1] = clamp((x1+t2) >> 17); o[6] = clamp((x1-t2) >> 17); o[2] = clamp((x2+t1) >> 17); o[5] = clamp((x2-t1) >> 17); o[3] = clamp((x3+t0) >> 17); o[4] = clamp((x3-t0) >> 17); } } static stbi_idct_8x8 stbi_idct_installed = idct_block; extern void stbi_install_idct(stbi_idct_8x8 func) { stbi_idct_installed = func; } #endif #define MARKER_none 0xff // if there's a pending marker from the entropy stream, return that // otherwise, fetch from the stream and get a marker. if there's no // marker, return 0xff, which is never a valid marker value static uint8 get_marker(jpeg *j) { uint8 x; if (j->marker != MARKER_none) { x = j->marker; j->marker = MARKER_none; return x; } x = get8u(&j->s); if (x != 0xff) return MARKER_none; while (x == 0xff) x = get8u(&j->s); return x; } // in each scan, we'll have scan_n components, and the order // of the components is specified by order[] #define RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7) // after a restart interval, reset the entropy decoder and // the dc prediction static void reset(jpeg *j) { j->code_bits = 0; j->code_buffer = 0; j->nomore = 0; j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0; j->marker = MARKER_none; j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff; // no more than 1<<31 MCUs if no restart_interal? that's plenty safe, // since we don't even allow 1<<30 pixels } static int parse_entropy_coded_data(jpeg *z) { reset(z); if (z->scan_n == 1) { int i,j; #if STBI_SIMD __declspec(align(16)) #endif short data[64]; int n = z->order[0]; // non-interleaved data, we just need to process one block at a time, // in trivial scanline order // number of blocks to do just depends on how many actual "pixels" this // component has, independent of interleaved MCU blocking and such int w = (z->img_comp[n].x+7) >> 3; int h = (z->img_comp[n].y+7) >> 3; for (j=0; j < h; ++j) { for (i=0; i < w; ++i) { if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0; #if STBI_SIMD stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]); #else idct_block(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]); #endif // every data block is an MCU, so countdown the restart interval if (--z->todo <= 0) { if (z->code_bits < 24) grow_buffer_unsafe(z); // if it's NOT a restart, then just bail, so we get corrupt data // rather than no data if (!RESTART(z->marker)) return 1; reset(z); } } } } else { // interleaved! int i,j,k,x,y; short data[64]; for (j=0; j < z->img_mcu_y; ++j) { for (i=0; i < z->img_mcu_x; ++i) { // scan an interleaved mcu... process scan_n components in order for (k=0; k < z->scan_n; ++k) { int n = z->order[k]; // scan out an mcu's worth of this component; that's just determined // by the basic H and V specified for the component for (y=0; y < z->img_comp[n].v; ++y) { for (x=0; x < z->img_comp[n].h; ++x) { int x2 = (i*z->img_comp[n].h + x)*8; int y2 = (j*z->img_comp[n].v + y)*8; if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0; #if STBI_SIMD stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]); #else idct_block(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]); #endif } } } // after all interleaved components, that's an interleaved MCU, // so now count down the restart interval if (--z->todo <= 0) { if (z->code_bits < 24) grow_buffer_unsafe(z); // if it's NOT a restart, then just bail, so we get corrupt data // rather than no data if (!RESTART(z->marker)) return 1; reset(z); } } } } return 1; } static int process_marker(jpeg *z, int m) { int L; switch (m) { case MARKER_none: // no marker found return e("expected marker","Corrupt JPEG"); case 0xC2: // SOF - progressive return e("progressive jpeg","JPEG format not supported (progressive)"); case 0xDD: // DRI - specify restart interval if (get16(&z->s) != 4) return e("bad DRI len","Corrupt JPEG"); z->restart_interval = get16(&z->s); return 1; case 0xDB: // DQT - define quantization table L = get16(&z->s)-2; while (L > 0) { int q = get8(&z->s); int p = q >> 4; int t = q & 15,i; if (p != 0) return e("bad DQT type","Corrupt JPEG"); if (t > 3) return e("bad DQT table","Corrupt JPEG"); for (i=0; i < 64; ++i) z->dequant[t][dezigzag[i]] = get8u(&z->s); #if STBI_SIMD for (i=0; i < 64; ++i) z->dequant2[t][i] = z->dequant[t][i]; #endif L -= 65; } return L==0; case 0xC4: // DHT - define huffman table L = get16(&z->s)-2; while (L > 0) { uint8 *v; int sizes[16],i,m=0; int q = get8(&z->s); int tc = q >> 4; int th = q & 15; if (tc > 1 || th > 3) return e("bad DHT header","Corrupt JPEG"); for (i=0; i < 16; ++i) { sizes[i] = get8(&z->s); m += sizes[i]; } L -= 17; if (tc == 0) { if (!build_huffman(z->huff_dc+th, sizes)) return 0; v = z->huff_dc[th].values; } else { if (!build_huffman(z->huff_ac+th, sizes)) return 0; v = z->huff_ac[th].values; } for (i=0; i < m; ++i) v[i] = get8u(&z->s); L -= m; } return L==0; } // check for comment block or APP blocks if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) { skip(&z->s, get16(&z->s)-2); return 1; } return 0; } // after we see SOS static int process_scan_header(jpeg *z) { int i; int Ls = get16(&z->s); z->scan_n = get8(&z->s); if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s.img_n) return e("bad SOS component count","Corrupt JPEG"); if (Ls != 6+2*z->scan_n) return e("bad SOS len","Corrupt JPEG"); for (i=0; i < z->scan_n; ++i) { int id = get8(&z->s), which; int q = get8(&z->s); for (which = 0; which < z->s.img_n; ++which) if (z->img_comp[which].id == id) break; if (which == z->s.img_n) return 0; z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return e("bad DC huff","Corrupt JPEG"); z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return e("bad AC huff","Corrupt JPEG"); z->order[i] = which; } if (get8(&z->s) != 0) return e("bad SOS","Corrupt JPEG"); get8(&z->s); // should be 63, but might be 0 if (get8(&z->s) != 0) return e("bad SOS","Corrupt JPEG"); return 1; } static int process_frame_header(jpeg *z, int scan) { stbi *s = &z->s; int Lf,p,i,q, h_max=1,v_max=1,c; Lf = get16(s); if (Lf < 11) return e("bad SOF len","Corrupt JPEG"); // JPEG p = get8(s); if (p != 8) return e("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline s->img_y = get16(s); if (s->img_y == 0) return e("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG s->img_x = get16(s); if (s->img_x == 0) return e("0 width","Corrupt JPEG"); // JPEG requires c = get8(s); if (c != 3 && c != 1) return e("bad component count","Corrupt JPEG"); // JFIF requires s->img_n = c; for (i=0; i < c; ++i) { z->img_comp[i].data = NULL; z->img_comp[i].linebuf = NULL; } if (Lf != 8+3*s->img_n) return e("bad SOF len","Corrupt JPEG"); for (i=0; i < s->img_n; ++i) { z->img_comp[i].id = get8(s); if (z->img_comp[i].id != i+1) // JFIF requires if (z->img_comp[i].id != i) // some version of jpegtran outputs non-JFIF-compliant files! return e("bad component ID","Corrupt JPEG"); q = get8(s); z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return e("bad H","Corrupt JPEG"); z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return e("bad V","Corrupt JPEG"); z->img_comp[i].tq = get8(s); if (z->img_comp[i].tq > 3) return e("bad TQ","Corrupt JPEG"); } if (scan != SCAN_load) return 1; if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode"); for (i=0; i < s->img_n; ++i) { if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h; if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v; } // compute interleaved mcu info z->img_h_max = h_max; z->img_v_max = v_max; z->img_mcu_w = h_max * 8; z->img_mcu_h = v_max * 8; z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w; z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h; for (i=0; i < s->img_n; ++i) { // number of effective pixels (e.g. for non-interleaved MCU) z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max; z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max; // to simplify generation, we'll allocate enough memory to decode // the bogus oversized data from using interleaved MCUs and their // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't // discard the extra data until colorspace conversion z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8; z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8; z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15); if (z->img_comp[i].raw_data == NULL) { for(--i; i >= 0; --i) { free(z->img_comp[i].raw_data); z->img_comp[i].data = NULL; } return e("outofmem", "Out of memory"); } // align blocks for installable-idct using mmx/sse z->img_comp[i].data = (uint8*) (((size_t) z->img_comp[i].raw_data + 15) & ~15); z->img_comp[i].linebuf = NULL; } return 1; } // use comparisons since in some cases we handle more than one case (e.g. SOF) #define DNL(x) ((x) == 0xdc) #define SOI(x) ((x) == 0xd8) #define EOI(x) ((x) == 0xd9) #define SOF(x) ((x) == 0xc0 || (x) == 0xc1) #define SOS(x) ((x) == 0xda) static int decode_jpeg_header(jpeg *z, int scan) { int m; z->marker = MARKER_none; // initialize cached marker to empty m = get_marker(z); if (!SOI(m)) return e("no SOI","Corrupt JPEG"); if (scan == SCAN_type) return 1; m = get_marker(z); while (!SOF(m)) { if (!process_marker(z,m)) return 0; m = get_marker(z); while (m == MARKER_none) { // some files have extra padding after their blocks, so ok, we'll scan if (at_eof(&z->s)) return e("no SOF", "Corrupt JPEG"); m = get_marker(z); } } if (!process_frame_header(z, scan)) return 0; return 1; } static int decode_jpeg_image(jpeg *j) { int m; j->restart_interval = 0; if (!decode_jpeg_header(j, SCAN_load)) return 0; m = get_marker(j); while (!EOI(m)) { if (SOS(m)) { if (!process_scan_header(j)) return 0; if (!parse_entropy_coded_data(j)) return 0; } else { if (!process_marker(j, m)) return 0; } m = get_marker(j); } return 1; } // static jfif-centered resampling (across block boundaries) typedef uint8 *(*resample_row_func)(uint8 *out, uint8 *in0, uint8 *in1, int w, int hs); #define div4(x) ((uint8) ((x) >> 2)) static uint8 *resample_row_1(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs) { return in_near; } static uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs) { // need to generate two samples vertically for every one in input int i; for (i=0; i < w; ++i) out[i] = div4(3*in_near[i] + in_far[i] + 2); return out; } static uint8* resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs) { // need to generate two samples horizontally for every one in input int i; uint8 *input = in_near; if (w == 1) { // if only one sample, can't do any interpolation out[0] = out[1] = input[0]; return out; } out[0] = input[0]; out[1] = div4(input[0]*3 + input[1] + 2); for (i=1; i < w-1; ++i) { int n = 3*input[i]+2; out[i*2+0] = div4(n+input[i-1]); out[i*2+1] = div4(n+input[i+1]); } out[i*2+0] = div4(input[w-2]*3 + input[w-1] + 2); out[i*2+1] = input[w-1]; return out; } #define div16(x) ((uint8) ((x) >> 4)) static uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs) { // need to generate 2x2 samples for every one in input int i,t0,t1; if (w == 1) { out[0] = out[1] = div4(3*in_near[0] + in_far[0] + 2); return out; } t1 = 3*in_near[0] + in_far[0]; out[0] = div4(t1+2); for (i=1; i < w; ++i) { t0 = t1; t1 = 3*in_near[i]+in_far[i]; out[i*2-1] = div16(3*t0 + t1 + 8); out[i*2 ] = div16(3*t1 + t0 + 8); } out[w*2-1] = div4(t1+2); return out; } static uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs) { // resample with nearest-neighbor int i,j; for (i=0; i < w; ++i) for (j=0; j < hs; ++j) out[i*hs+j] = in_near[i]; return out; } #define float2fixed(x) ((int) ((x) * 65536 + 0.5)) // 0.38 seconds on 3*anemones.jpg (0.25 with processor = Pro) // VC6 without processor=Pro is generating multiple LEAs per multiply! static void YCbCr_to_RGB_row(uint8 *out, const uint8 *y, const uint8 *pcb, const uint8 *pcr, int count, int step) { int i; for (i=0; i < count; ++i) { int y_fixed = (y[i] << 16) + 32768; // rounding int r,g,b; int cr = pcr[i] - 128; int cb = pcb[i] - 128; r = y_fixed + cr*float2fixed(1.40200f); g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f); b = y_fixed + cb*float2fixed(1.77200f); r >>= 16; g >>= 16; b >>= 16; if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } out[0] = (uint8)r; out[1] = (uint8)g; out[2] = (uint8)b; out[3] = 255; out += step; } } #if STBI_SIMD static stbi_YCbCr_to_RGB_run stbi_YCbCr_installed = YCbCr_to_RGB_row; void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func) { stbi_YCbCr_installed = func; } #endif // clean up the temporary component buffers static void cleanup_jpeg(jpeg *j) { int i; for (i=0; i < j->s.img_n; ++i) { if (j->img_comp[i].data) { free(j->img_comp[i].raw_data); j->img_comp[i].data = NULL; } if (j->img_comp[i].linebuf) { free(j->img_comp[i].linebuf); j->img_comp[i].linebuf = NULL; } } } typedef struct { resample_row_func resample; uint8 *line0,*line1; int hs,vs; // expansion factor in each axis int w_lores; // horizontal pixels pre-expansion int ystep; // how far through vertical expansion we are int ypos; // which pre-expansion row we're on } stbi_resample; static uint8 *load_jpeg_image(jpeg *z, int *out_x, int *out_y, int *comp, int req_comp) { int n, decode_n; // validate req_comp if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error"); z->s.img_n = 0; // load a jpeg image from whichever source if (!decode_jpeg_image(z)) { cleanup_jpeg(z); return NULL; } // determine actual number of components to generate n = req_comp ? req_comp : z->s.img_n; if (z->s.img_n == 3 && n < 3) decode_n = 1; else decode_n = z->s.img_n; // resample and color-convert { int k; uint i,j; uint8 *output; uint8 *coutput[4]; stbi_resample res_comp[4]; for (k=0; k < decode_n; ++k) { stbi_resample *r = &res_comp[k]; // allocate line buffer big enough for upsampling off the edges // with upsample factor of 4 z->img_comp[k].linebuf = (uint8 *) malloc(z->s.img_x + 3); if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); } r->hs = z->img_h_max / z->img_comp[k].h; r->vs = z->img_v_max / z->img_comp[k].v; r->ystep = r->vs >> 1; r->w_lores = (z->s.img_x + r->hs-1) / r->hs; r->ypos = 0; r->line0 = r->line1 = z->img_comp[k].data; if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1; else if (r->hs == 1 && r->vs == 2) r->resample = resample_row_v_2; else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2; else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2; else r->resample = resample_row_generic; } // can't error after this so, this is safe output = (uint8 *) malloc(n * z->s.img_x * z->s.img_y + 1); if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); } // now go ahead and resample for (j=0; j < z->s.img_y; ++j) { uint8 *out = output + n * z->s.img_x * j; for (k=0; k < decode_n; ++k) { stbi_resample *r = &res_comp[k]; int y_bot = r->ystep >= (r->vs >> 1); coutput[k] = r->resample(z->img_comp[k].linebuf, y_bot ? r->line1 : r->line0, y_bot ? r->line0 : r->line1, r->w_lores, r->hs); if (++r->ystep >= r->vs) { r->ystep = 0; r->line0 = r->line1; if (++r->ypos < z->img_comp[k].y) r->line1 += z->img_comp[k].w2; } } if (n >= 3) { uint8 *y = coutput[0]; if (z->s.img_n == 3) { #if STBI_SIMD stbi_YCbCr_installed(out, y, coutput[1], coutput[2], z->s.img_x, n); #else YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s.img_x, n); #endif } else for (i=0; i < z->s.img_x; ++i) { out[0] = out[1] = out[2] = y[i]; out[3] = 255; // not used if n==3 out += n; } } else { uint8 *y = coutput[0]; if (n == 1) for (i=0; i < z->s.img_x; ++i) out[i] = y[i]; else for (i=0; i < z->s.img_x; ++i) *out++ = y[i], *out++ = 255; } } cleanup_jpeg(z); *out_x = z->s.img_x; *out_y = z->s.img_y; if (comp) *comp = z->s.img_n; // report original components, not output return output; } } #ifndef STBI_NO_STDIO unsigned char *stbi_jpeg_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) { jpeg j; start_file(&j.s, f); return load_jpeg_image(&j, x,y,comp,req_comp); } unsigned char *stbi_jpeg_load(char const *filename, int *x, int *y, int *comp, int req_comp) { unsigned char *data; FILE *f = fopen(filename, "rb"); if (!f) return NULL; data = stbi_jpeg_load_from_file(f,x,y,comp,req_comp); fclose(f); return data; } #endif unsigned char *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { jpeg j; start_mem(&j.s, buffer,len); return load_jpeg_image(&j, x,y,comp,req_comp); } #ifndef STBI_NO_STDIO int stbi_jpeg_test_file(FILE *f) { int n,r; jpeg j; n = ftell(f); start_file(&j.s, f); r = decode_jpeg_header(&j, SCAN_type); fseek(f,n,SEEK_SET); return r; } #endif int stbi_jpeg_test_memory(stbi_uc const *buffer, int len) { jpeg j; start_mem(&j.s, buffer,len); return decode_jpeg_header(&j, SCAN_type); } // @TODO: #ifndef STBI_NO_STDIO extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp); extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp); #endif extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); // public domain zlib decode v0.2 Sean Barrett 2006-11-18 // simple implementation // - all input must be provided in an upfront buffer // - all output is written to a single output buffer (can malloc/realloc) // performance // - fast huffman // fast-way is faster to check than jpeg huffman, but slow way is slower #define ZFAST_BITS 9 // accelerate all cases in default tables #define ZFAST_MASK ((1 << ZFAST_BITS) - 1) // zlib-style huffman encoding // (jpegs packs from left, zlib from right, so can't share code) typedef struct { uint16 fast[1 << ZFAST_BITS]; uint16 firstcode[16]; int maxcode[17]; uint16 firstsymbol[16]; uint8 size[288]; uint16 value[288]; } zhuffman; __forceinline static int bitreverse16(int n) { n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); return n; } __forceinline static int bit_reverse(int v, int bits) { assert(bits <= 16); // to bit reverse n bits, reverse 16 and shift // e.g. 11 bits, bit reverse and shift away 5 return bitreverse16(v) >> (16-bits); } static int zbuild_huffman(zhuffman *z, uint8 *sizelist, int num) { int i,k=0; int code, next_code[16], sizes[17]; // DEFLATE spec for generating codes memset(sizes, 0, sizeof(sizes)); memset(z->fast, 255, sizeof(z->fast)); for (i=0; i < num; ++i) ++sizes[sizelist[i]]; sizes[0] = 0; for (i=1; i < 16; ++i) assert(sizes[i] <= (1 << i)); code = 0; for (i=1; i < 16; ++i) { next_code[i] = code; z->firstcode[i] = (uint16) code; z->firstsymbol[i] = (uint16) k; code = (code + sizes[i]); if (sizes[i]) if (code-1 >= (1 << i)) return e("bad codelengths","Corrupt JPEG"); z->maxcode[i] = code << (16-i); // preshift for inner loop code <<= 1; k += sizes[i]; } z->maxcode[16] = 0x10000; // sentinel for (i=0; i < num; ++i) { int s = sizelist[i]; if (s) { int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s]; z->size[c] = (uint8)s; z->value[c] = (uint16)i; if (s <= ZFAST_BITS) { int k = bit_reverse(next_code[s],s); while (k < (1 << ZFAST_BITS)) { z->fast[k] = (uint16) c; k += (1 << s); } } ++next_code[s]; } } return 1; } // zlib-from-memory implementation for PNG reading // because PNG allows splitting the zlib stream arbitrarily, // and it's annoying structurally to have PNG call ZLIB call PNG, // we require PNG read all the IDATs and combine them into a single // memory buffer typedef struct { uint8 *zbuffer, *zbuffer_end; int num_bits; uint32 code_buffer; char *zout; char *zout_start; char *zout_end; int z_expandable; zhuffman z_length, z_distance; } zbuf; __forceinline static int zget8(zbuf *z) { if (z->zbuffer >= z->zbuffer_end) return 0; return *z->zbuffer++; } static void fill_bits(zbuf *z) { do { assert(z->code_buffer < (1U << z->num_bits)); z->code_buffer |= zget8(z) << z->num_bits; z->num_bits += 8; } while (z->num_bits <= 24); } __forceinline static unsigned int zreceive(zbuf *z, int n) { unsigned int k; if (z->num_bits < n) fill_bits(z); k = z->code_buffer & ((1 << n) - 1); z->code_buffer >>= n; z->num_bits -= n; return k; } __forceinline static int zhuffman_decode(zbuf *a, zhuffman *z) { int b,s,k; if (a->num_bits < 16) fill_bits(a); b = z->fast[a->code_buffer & ZFAST_MASK]; if (b < 0xffff) { s = z->size[b]; a->code_buffer >>= s; a->num_bits -= s; return z->value[b]; } // not resolved by fast table, so compute it the slow way // use jpeg approach, which requires MSbits at top k = bit_reverse(a->code_buffer, 16); for (s=ZFAST_BITS+1; ; ++s) if (k < z->maxcode[s]) break; if (s == 16) return -1; // invalid code! // code size is s, so: b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s]; assert(z->size[b] == s); a->code_buffer >>= s; a->num_bits -= s; return z->value[b]; } static int expand(zbuf *z, int n) // need to make room for n bytes { char *q; int cur, limit; if (!z->z_expandable) return e("output buffer limit","Corrupt PNG"); cur = (int) (z->zout - z->zout_start); limit = (int) (z->zout_end - z->zout_start); while (cur + n > limit) limit *= 2; q = (char *) realloc(z->zout_start, limit); if (q == NULL) return e("outofmem", "Out of memory"); z->zout_start = q; z->zout = q + cur; z->zout_end = q + limit; return 1; } static int length_base[31] = { 3,4,5,6,7,8,9,10,11,13, 15,17,19,23,27,31,35,43,51,59, 67,83,99,115,131,163,195,227,258,0,0 }; static int length_extra[31]= { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 }; static int dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; static int dist_extra[32] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; static int parse_huffman_block(zbuf *a) { for(;;) { int z = zhuffman_decode(a, &a->z_length); if (z < 256) { if (z < 0) return e("bad huffman code","Corrupt PNG"); // error in huffman codes if (a->zout >= a->zout_end) if (!expand(a, 1)) return 0; *a->zout++ = (char) z; } else { uint8 *p; int len,dist; if (z == 256) return 1; z -= 257; len = length_base[z]; if (length_extra[z]) len += zreceive(a, length_extra[z]); z = zhuffman_decode(a, &a->z_distance); if (z < 0) return e("bad huffman code","Corrupt PNG"); dist = dist_base[z]; if (dist_extra[z]) dist += zreceive(a, dist_extra[z]); if (a->zout - a->zout_start < dist) return e("bad dist","Corrupt PNG"); if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0; p = (uint8 *) (a->zout - dist); while (len--) *a->zout++ = *p++; } } } static int compute_huffman_codes(zbuf *a) { static uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; zhuffman z_codelength; uint8 lencodes[286+32+137];//padding for maximum single op uint8 codelength_sizes[19]; int i,n; int hlit = zreceive(a,5) + 257; int hdist = zreceive(a,5) + 1; int hclen = zreceive(a,4) + 4; memset(codelength_sizes, 0, sizeof(codelength_sizes)); for (i=0; i < hclen; ++i) { int s = zreceive(a,3); codelength_sizes[length_dezigzag[i]] = (uint8) s; } if (!zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0; n = 0; while (n < hlit + hdist) { int c = zhuffman_decode(a, &z_codelength); assert(c >= 0 && c < 19); if (c < 16) lencodes[n++] = (uint8) c; else if (c == 16) { c = zreceive(a,2)+3; memset(lencodes+n, lencodes[n-1], c); n += c; } else if (c == 17) { c = zreceive(a,3)+3; memset(lencodes+n, 0, c); n += c; } else { assert(c == 18); c = zreceive(a,7)+11; memset(lencodes+n, 0, c); n += c; } } if (n != hlit+hdist) return e("bad codelengths","Corrupt PNG"); if (!zbuild_huffman(&a->z_length, lencodes, hlit)) return 0; if (!zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0; return 1; } static int parse_uncompressed_block(zbuf *a) { uint8 header[4]; int len,nlen,k; if (a->num_bits & 7) zreceive(a, a->num_bits & 7); // discard // drain the bit-packed data into header k = 0; while (a->num_bits > 0) { header[k++] = (uint8) (a->code_buffer & 255); // wtf this warns? a->code_buffer >>= 8; a->num_bits -= 8; } assert(a->num_bits == 0); // now fill header the normal way while (k < 4) header[k++] = (uint8) zget8(a); len = header[1] * 256 + header[0]; nlen = header[3] * 256 + header[2]; if (nlen != (len ^ 0xffff)) return e("zlib corrupt","Corrupt PNG"); if (a->zbuffer + len > a->zbuffer_end) return e("read past buffer","Corrupt PNG"); if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0; memcpy(a->zout, a->zbuffer, len); a->zbuffer += len; a->zout += len; return 1; } static int parse_zlib_header(zbuf *a) { int cmf = zget8(a); int cm = cmf & 15; /* int cinfo = cmf >> 4; */ int flg = zget8(a); if ((cmf*256+flg) % 31 != 0) return e("bad zlib header","Corrupt PNG"); // zlib spec if (flg & 32) return e("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png if (cm != 8) return e("bad compression","Corrupt PNG"); // DEFLATE required for png // window = 1 << (8 + cinfo)... but who cares, we fully buffer output return 1; } // @TODO: should statically initialize these for optimal thread safety static uint8 default_length[288], default_distance[32]; static void init_defaults(void) { int i; // use <= to match clearly with spec for (i=0; i <= 143; ++i) default_length[i] = 8; for ( ; i <= 255; ++i) default_length[i] = 9; for ( ; i <= 279; ++i) default_length[i] = 7; for ( ; i <= 287; ++i) default_length[i] = 8; for (i=0; i <= 31; ++i) default_distance[i] = 5; } int stbi_png_partial; // a quick hack to only allow decoding some of a PNG... I should implement real streaming support instead static int parse_zlib(zbuf *a, int parse_header) { int final, type; if (parse_header) if (!parse_zlib_header(a)) return 0; a->num_bits = 0; a->code_buffer = 0; do { final = zreceive(a,1); type = zreceive(a,2); if (type == 0) { if (!parse_uncompressed_block(a)) return 0; } else if (type == 3) { return 0; } else { if (type == 1) { // use fixed code lengths if (!default_distance[31]) init_defaults(); if (!zbuild_huffman(&a->z_length , default_length , 288)) return 0; if (!zbuild_huffman(&a->z_distance, default_distance, 32)) return 0; } else { if (!compute_huffman_codes(a)) return 0; } if (!parse_huffman_block(a)) return 0; } if (stbi_png_partial && a->zout - a->zout_start > 65536) break; } while (!final); return 1; } static int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header) { a->zout_start = obuf; a->zout = obuf; a->zout_end = obuf + olen; a->z_expandable = exp; return parse_zlib(a, parse_header); } char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen) { zbuf a; char *p = (char *) malloc(initial_size); if (p == NULL) return NULL; a.zbuffer = (uint8 *) buffer; a.zbuffer_end = (uint8 *) buffer + len; if (do_zlib(&a, p, initial_size, 1, 1)) { if (outlen) *outlen = (int) (a.zout - a.zout_start); return a.zout_start; } else { free(a.zout_start); return NULL; } } char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen) { return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); } int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen) { zbuf a; a.zbuffer = (uint8 *) ibuffer; a.zbuffer_end = (uint8 *) ibuffer + ilen; if (do_zlib(&a, obuffer, olen, 0, 1)) return (int) (a.zout - a.zout_start); else return -1; } char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen) { zbuf a; char *p = (char *) malloc(16384); if (p == NULL) return NULL; a.zbuffer = (uint8 *) buffer; a.zbuffer_end = (uint8 *) buffer+len; if (do_zlib(&a, p, 16384, 1, 0)) { if (outlen) *outlen = (int) (a.zout - a.zout_start); return a.zout_start; } else { free(a.zout_start); return NULL; } } int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen) { zbuf a; a.zbuffer = (uint8 *) ibuffer; a.zbuffer_end = (uint8 *) ibuffer + ilen; if (do_zlib(&a, obuffer, olen, 0, 0)) return (int) (a.zout - a.zout_start); else return -1; } // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18 // simple implementation // - only 8-bit samples // - no CRC checking // - allocates lots of intermediate memory // - avoids problem of streaming data between subsystems // - avoids explicit window management // performance // - uses stb_zlib, a PD zlib implementation with fast huffman decoding typedef struct { uint32 length; uint32 type; } chunk; #define PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d)) static chunk get_chunk_header(stbi *s) { chunk c; c.length = get32(s); c.type = get32(s); return c; } static int check_png_header(stbi *s) { static uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 }; int i; for (i=0; i < 8; ++i) if (get8(s) != png_sig[i]) return e("bad png sig","Not a PNG"); return 1; } typedef struct { stbi s; uint8 *idata, *expanded, *out; } png; enum { F_none=0, F_sub=1, F_up=2, F_avg=3, F_paeth=4, F_avg_first, F_paeth_first, }; static uint8 first_row_filter[5] = { F_none, F_sub, F_none, F_avg_first, F_paeth_first }; static int paeth(int a, int b, int c) { int p = a + b - c; int pa = abs(p-a); int pb = abs(p-b); int pc = abs(p-c); if (pa <= pb && pa <= pc) return a; if (pb <= pc) return b; return c; } // create the png data from post-deflated data static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, uint32 x, uint32 y) { stbi *s = &a->s; uint32 i,j,stride = x*out_n; int k; int img_n = s->img_n; // copy it into a local for later assert(out_n == s->img_n || out_n == s->img_n+1); if (stbi_png_partial) y = 1; a->out = (uint8 *) malloc(x * y * out_n); if (!a->out) return e("outofmem", "Out of memory"); if (!stbi_png_partial) { if (s->img_x == x && s->img_y == y) if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG"); else // interlaced: if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG"); } for (j=0; j < y; ++j) { uint8 *cur = a->out + stride*j; uint8 *prior = cur - stride; int filter = *raw++; if (filter > 4) return e("invalid filter","Corrupt PNG"); // if first row, use special filter that doesn't sample previous row if (j == 0) filter = first_row_filter[filter]; // handle first pixel explicitly for (k=0; k < img_n; ++k) { switch(filter) { case F_none : cur[k] = raw[k]; break; case F_sub : cur[k] = raw[k]; break; case F_up : cur[k] = raw[k] + prior[k]; break; case F_avg : cur[k] = raw[k] + (prior[k]>>1); break; case F_paeth : cur[k] = (uint8) (raw[k] + paeth(0,prior[k],0)); break; case F_avg_first : cur[k] = raw[k]; break; case F_paeth_first: cur[k] = raw[k]; break; } } if (img_n != out_n) cur[img_n] = 255; raw += img_n; cur += out_n; prior += out_n; // this is a little gross, so that we don't switch per-pixel or per-component if (img_n == out_n) { #define CASE(f) \ case f: \ for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \ for (k=0; k < img_n; ++k) switch(filter) { CASE(F_none) cur[k] = raw[k]; break; CASE(F_sub) cur[k] = raw[k] + cur[k-img_n]; break; CASE(F_up) cur[k] = raw[k] + prior[k]; break; CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-img_n])>>1); break; CASE(F_paeth) cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break; CASE(F_avg_first) cur[k] = raw[k] + (cur[k-img_n] >> 1); break; CASE(F_paeth_first) cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break; } #undef CASE } else { assert(img_n+1 == out_n); #define CASE(f) \ case f: \ for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \ for (k=0; k < img_n; ++k) switch(filter) { CASE(F_none) cur[k] = raw[k]; break; CASE(F_sub) cur[k] = raw[k] + cur[k-out_n]; break; CASE(F_up) cur[k] = raw[k] + prior[k]; break; CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-out_n])>>1); break; CASE(F_paeth) cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break; CASE(F_avg_first) cur[k] = raw[k] + (cur[k-out_n] >> 1); break; CASE(F_paeth_first) cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break; } #undef CASE } } return 1; } static int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n, int interlaced) { uint8 *final; int p; int save; if (!interlaced) return create_png_image_raw(a, raw, raw_len, out_n, a->s.img_x, a->s.img_y); save = stbi_png_partial; stbi_png_partial = 0; // de-interlacing final = (uint8 *) malloc(a->s.img_x * a->s.img_y * out_n); for (p=0; p < 7; ++p) { int xorig[] = { 0,4,0,2,0,1,0 }; int yorig[] = { 0,0,4,0,2,0,1 }; int xspc[] = { 8,8,4,4,2,2,1 }; int yspc[] = { 8,8,8,4,4,2,2 }; int i,j,x,y; // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1 x = (a->s.img_x - xorig[p] + xspc[p]-1) / xspc[p]; y = (a->s.img_y - yorig[p] + yspc[p]-1) / yspc[p]; if (x && y) { if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) { free(final); return 0; } for (j=0; j < y; ++j) for (i=0; i < x; ++i) memcpy(final + (j*yspc[p]+yorig[p])*a->s.img_x*out_n + (i*xspc[p]+xorig[p])*out_n, a->out + (j*x+i)*out_n, out_n); free(a->out); raw += (x*out_n+1)*y; raw_len -= (x*out_n+1)*y; } } a->out = final; stbi_png_partial = save; return 1; } static int compute_transparency(png *z, uint8 tc[3], int out_n) { stbi *s = &z->s; uint32 i, pixel_count = s->img_x * s->img_y; uint8 *p = z->out; // compute color-based transparency, assuming we've // already got 255 as the alpha value in the output assert(out_n == 2 || out_n == 4); if (out_n == 2) { for (i=0; i < pixel_count; ++i) { p[1] = (p[0] == tc[0] ? 0 : 255); p += 2; } } else { for (i=0; i < pixel_count; ++i) { if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) p[3] = 0; p += 4; } } return 1; } static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n) { uint32 i, pixel_count = a->s.img_x * a->s.img_y; uint8 *p, *temp_out, *orig = a->out; p = (uint8 *) malloc(pixel_count * pal_img_n); if (p == NULL) return e("outofmem", "Out of memory"); // between here and free(out) below, exitting would leak temp_out = p; if (pal_img_n == 3) { for (i=0; i < pixel_count; ++i) { int n = orig[i]*4; p[0] = palette[n ]; p[1] = palette[n+1]; p[2] = palette[n+2]; p += 3; } } else { for (i=0; i < pixel_count; ++i) { int n = orig[i]*4; p[0] = palette[n ]; p[1] = palette[n+1]; p[2] = palette[n+2]; p[3] = palette[n+3]; p += 4; } } free(a->out); a->out = temp_out; return 1; } static int parse_png_file(png *z, int scan, int req_comp) { uint8 palette[1024], pal_img_n=0; uint8 has_trans=0, tc[3]; uint32 ioff=0, idata_limit=0, i, pal_len=0; int first=1,k,interlace=0; stbi *s = &z->s; if (!check_png_header(s)) return 0; if (scan == SCAN_type) return 1; for(;;first=0) { chunk c = get_chunk_header(s); if (first && c.type != PNG_TYPE('I','H','D','R')) return e("first not IHDR","Corrupt PNG"); switch (c.type) { case PNG_TYPE('I','H','D','R'): { int depth,color,comp,filter; if (!first) return e("multiple IHDR","Corrupt PNG"); if (c.length != 13) return e("bad IHDR len","Corrupt PNG"); s->img_x = get32(s); if (s->img_x > (1 << 24)) return e("too large","Very large image (corrupt?)"); s->img_y = get32(s); if (s->img_y > (1 << 24)) return e("too large","Very large image (corrupt?)"); depth = get8(s); if (depth != 8) return e("8bit only","PNG not supported: 8-bit only"); color = get8(s); if (color > 6) return e("bad ctype","Corrupt PNG"); if (color == 3) pal_img_n = 3; else if (color & 1) return e("bad ctype","Corrupt PNG"); comp = get8(s); if (comp) return e("bad comp method","Corrupt PNG"); filter= get8(s); if (filter) return e("bad filter method","Corrupt PNG"); interlace = get8(s); if (interlace>1) return e("bad interlace method","Corrupt PNG"); if (!s->img_x || !s->img_y) return e("0-pixel image","Corrupt PNG"); if (!pal_img_n) { s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode"); if (scan == SCAN_header) return 1; } else { // if paletted, then pal_n is our final components, and // img_n is # components to decompress/filter. s->img_n = 1; if ((1 << 30) / s->img_x / 4 < s->img_y) return e("too large","Corrupt PNG"); // if SCAN_header, have to scan to see if we have a tRNS } break; } case PNG_TYPE('P','L','T','E'): { if (c.length > 256*3) return e("invalid PLTE","Corrupt PNG"); pal_len = c.length / 3; if (pal_len * 3 != c.length) return e("invalid PLTE","Corrupt PNG"); for (i=0; i < pal_len; ++i) { palette[i*4+0] = get8u(s); palette[i*4+1] = get8u(s); palette[i*4+2] = get8u(s); palette[i*4+3] = 255; } break; } case PNG_TYPE('t','R','N','S'): { if (z->idata) return e("tRNS after IDAT","Corrupt PNG"); if (pal_img_n) { if (scan == SCAN_header) { s->img_n = 4; return 1; } if (pal_len == 0) return e("tRNS before PLTE","Corrupt PNG"); if (c.length > pal_len) return e("bad tRNS len","Corrupt PNG"); pal_img_n = 4; for (i=0; i < c.length; ++i) palette[i*4+3] = get8u(s); } else { if (!(s->img_n & 1)) return e("tRNS with alpha","Corrupt PNG"); if (c.length != (uint32) s->img_n*2) return e("bad tRNS len","Corrupt PNG"); has_trans = 1; for (k=0; k < s->img_n; ++k) tc[k] = (uint8) get16(s); // non 8-bit images will be larger } break; } case PNG_TYPE('I','D','A','T'): { if (pal_img_n && !pal_len) return e("no PLTE","Corrupt PNG"); if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; } if (ioff + c.length > idata_limit) { uint8 *p; if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; while (ioff + c.length > idata_limit) idata_limit *= 2; p = (uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e("outofmem", "Out of memory"); z->idata = p; } #ifndef STBI_NO_STDIO if (s->img_file) { if (fread(z->idata+ioff,1,c.length,s->img_file) != c.length) return e("outofdata","Corrupt PNG"); } else #endif { memcpy(z->idata+ioff, s->img_buffer, c.length); s->img_buffer += c.length; } ioff += c.length; break; } case PNG_TYPE('I','E','N','D'): { uint32 raw_len; if (scan != SCAN_load) return 1; if (z->idata == NULL) return e("no IDAT","Corrupt PNG"); z->expanded = (uint8 *) stbi_zlib_decode_malloc((char *) z->idata, ioff, (int *) &raw_len); if (z->expanded == NULL) return 0; // zlib should set error free(z->idata); z->idata = NULL; if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans) s->img_out_n = s->img_n+1; else s->img_out_n = s->img_n; if (!create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0; if (has_trans) if (!compute_transparency(z, tc, s->img_out_n)) return 0; if (pal_img_n) { // pal_img_n == 3 or 4 s->img_n = pal_img_n; // record the actual colors we had s->img_out_n = pal_img_n; if (req_comp >= 3) s->img_out_n = req_comp; if (!expand_palette(z, palette, pal_len, s->img_out_n)) return 0; } free(z->expanded); z->expanded = NULL; return 1; } default: // if critical, fail if ((c.type & (1 << 29)) == 0) { #ifndef STBI_NO_FAILURE_STRINGS // not threadsafe static char invalid_chunk[] = "XXXX chunk not known"; invalid_chunk[0] = (uint8) (c.type >> 24); invalid_chunk[1] = (uint8) (c.type >> 16); invalid_chunk[2] = (uint8) (c.type >> 8); invalid_chunk[3] = (uint8) (c.type >> 0); #endif return e(invalid_chunk, "PNG not supported: unknown chunk type"); } skip(s, c.length); break; } // end of chunk, read and skip CRC get32(s); } } static unsigned char *do_png(png *p, int *x, int *y, int *n, int req_comp) { unsigned char *result=NULL; p->expanded = NULL; p->idata = NULL; p->out = NULL; if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error"); if (parse_png_file(p, SCAN_load, req_comp)) { result = p->out; p->out = NULL; if (req_comp && req_comp != p->s.img_out_n) { result = convert_format(result, p->s.img_out_n, req_comp, p->s.img_x, p->s.img_y); p->s.img_out_n = req_comp; if (result == NULL) return result; } *x = p->s.img_x; *y = p->s.img_y; if (n) *n = p->s.img_n; } free(p->out); p->out = NULL; free(p->expanded); p->expanded = NULL; free(p->idata); p->idata = NULL; return result; } #ifndef STBI_NO_STDIO unsigned char *stbi_png_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) { png p; start_file(&p.s, f); return do_png(&p, x,y,comp,req_comp); } unsigned char *stbi_png_load(char const *filename, int *x, int *y, int *comp, int req_comp) { unsigned char *data; FILE *f = fopen(filename, "rb"); if (!f) return NULL; data = stbi_png_load_from_file(f,x,y,comp,req_comp); fclose(f); return data; } #endif unsigned char *stbi_png_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { png p; start_mem(&p.s, buffer,len); return do_png(&p, x,y,comp,req_comp); } #ifndef STBI_NO_STDIO int stbi_png_test_file(FILE *f) { png p; int n,r; n = ftell(f); start_file(&p.s, f); r = parse_png_file(&p, SCAN_type,STBI_default); fseek(f,n,SEEK_SET); return r; } #endif int stbi_png_test_memory(stbi_uc const *buffer, int len) { png p; start_mem(&p.s, buffer, len); return parse_png_file(&p, SCAN_type,STBI_default); } // TODO: load header from png #ifndef STBI_NO_STDIO int stbi_png_info (char const *filename, int *x, int *y, int *comp) { png p; FILE *f = fopen(filename, "rb"); if (!f) return 0; start_file(&p.s, f); if (parse_png_file(&p, SCAN_header, 0)) { if(x) *x = p.s.img_x; if(y) *y = p.s.img_y; if (comp) *comp = p.s.img_n; fclose(f); return 1; } fclose(f); return 0; } extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp); #endif extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp); // Microsoft/Windows BMP image static int bmp_test(stbi *s) { int sz; if (get8(s) != 'B') return 0; if (get8(s) != 'M') return 0; get32le(s); // discard filesize get16le(s); // discard reserved get16le(s); // discard reserved get32le(s); // discard data offset sz = get32le(s); if (sz == 12 || sz == 40 || sz == 56 || sz == 108) return 1; return 0; } #ifndef STBI_NO_STDIO int stbi_bmp_test_file (FILE *f) { stbi s; int r,n = ftell(f); start_file(&s,f); r = bmp_test(&s); fseek(f,n,SEEK_SET); return r; } #endif int stbi_bmp_test_memory (stbi_uc const *buffer, int len) { stbi s; start_mem(&s, buffer, len); return bmp_test(&s); } // returns 0..31 for the highest set bit static int high_bit(unsigned int z) { int n=0; if (z == 0) return -1; if (z >= 0x10000) n += 16, z >>= 16; if (z >= 0x00100) n += 8, z >>= 8; if (z >= 0x00010) n += 4, z >>= 4; if (z >= 0x00004) n += 2, z >>= 2; if (z >= 0x00002) n += 1, z >>= 1; return n; } static int bitcount(unsigned int a) { a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2 a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4 a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits a = (a + (a >> 8)); // max 16 per 8 bits a = (a + (a >> 16)); // max 32 per 8 bits return a & 0xff; } static int shiftsigned(int v, int shift, int bits) { int result; int z=0; if (shift < 0) v <<= -shift; else v >>= shift; result = v; z = bits; while (z < 8) { result += v >> z; z += bits; } return result; } static stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp) { uint8 *out; unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0; stbi_uc pal[256][4]; int psize=0,i,j,compress=0,width; int bpp, flip_vertically, pad, target, offset, hsz; if (get8(s) != 'B' || get8(s) != 'M') return epuc("not BMP", "Corrupt BMP"); get32le(s); // discard filesize get16le(s); // discard reserved get16le(s); // discard reserved offset = get32le(s); hsz = get32le(s); if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) return epuc("unknown BMP", "BMP type not supported: unknown"); failure_reason = "bad BMP"; if (hsz == 12) { s->img_x = get16le(s); s->img_y = get16le(s); } else { s->img_x = get32le(s); s->img_y = get32le(s); } if (get16le(s) != 1) return 0; bpp = get16le(s); if (bpp == 1) return epuc("monochrome", "BMP type not supported: 1-bit"); flip_vertically = ((int) s->img_y) > 0; s->img_y = abs((int) s->img_y); if (hsz == 12) { if (bpp < 24) psize = (offset - 14 - 24) / 3; } else { compress = get32le(s); if (compress == 1 || compress == 2) return epuc("BMP RLE", "BMP type not supported: RLE"); get32le(s); // discard sizeof get32le(s); // discard hres get32le(s); // discard vres get32le(s); // discard colorsused get32le(s); // discard max important if (hsz == 40 || hsz == 56) { if (hsz == 56) { get32le(s); get32le(s); get32le(s); get32le(s); } if (bpp == 16 || bpp == 32) { mr = mg = mb = 0; if (compress == 0) { if (bpp == 32) { mr = 0xff << 16; mg = 0xff << 8; mb = 0xff << 0; ma = 0xff << 24; fake_a = 1; // @TODO: check for cases like alpha value is all 0 and switch it to 255 } else { mr = 31 << 10; mg = 31 << 5; mb = 31 << 0; } } else if (compress == 3) { mr = get32le(s); mg = get32le(s); mb = get32le(s); // not documented, but generated by photoshop and handled by mspaint if (mr == mg && mg == mb) { // ?!?!? return NULL; } } else return NULL; } } else { assert(hsz == 108); mr = get32le(s); mg = get32le(s); mb = get32le(s); ma = get32le(s); get32le(s); // discard color space for (i=0; i < 12; ++i) get32le(s); // discard color space parameters } if (bpp < 16) psize = (offset - 14 - hsz) >> 2; } s->img_n = ma ? 4 : 3; if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 target = req_comp; else target = s->img_n; // if they want monochrome, we'll post-convert out = (stbi_uc *) malloc(target * s->img_x * s->img_y); if (!out) return epuc("outofmem", "Out of memory"); if (bpp < 16) { int z=0; if (psize == 0 || psize > 256) { free(out); return epuc("invalid", "Corrupt BMP"); } for (i=0; i < psize; ++i) { pal[i][2] = get8(s); pal[i][1] = get8(s); pal[i][0] = get8(s); if (hsz != 12) get8(s); pal[i][3] = 255; } skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4)); if (bpp == 4) width = (s->img_x + 1) >> 1; else if (bpp == 8) width = s->img_x; else { free(out); return epuc("bad bpp", "Corrupt BMP"); } pad = (-width)&3; for (j=0; j < (int) s->img_y; ++j) { for (i=0; i < (int) s->img_x; i += 2) { int v=get8(s),v2=0; if (bpp == 4) { v2 = v & 15; v >>= 4; } out[z++] = pal[v][0]; out[z++] = pal[v][1]; out[z++] = pal[v][2]; if (target == 4) out[z++] = 255; if (i+1 == (int) s->img_x) break; v = (bpp == 8) ? get8(s) : v2; out[z++] = pal[v][0]; out[z++] = pal[v][1]; out[z++] = pal[v][2]; if (target == 4) out[z++] = 255; } skip(s, pad); } } else { int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0; int z = 0; int easy=0; skip(s, offset - 14 - hsz); if (bpp == 24) width = 3 * s->img_x; else if (bpp == 16) width = 2*s->img_x; else /* bpp = 32 and pad = 0 */ width=0; pad = (-width) & 3; if (bpp == 24) { easy = 1; } else if (bpp == 32) { if (mb == 0xff && mg == 0xff00 && mr == 0xff000000 && ma == 0xff000000) easy = 2; } if (!easy) { if (!mr || !mg || !mb) return epuc("bad masks", "Corrupt BMP"); // right shift amt to put high bit in position #7 rshift = high_bit(mr)-7; rcount = bitcount(mr); gshift = high_bit(mg)-7; gcount = bitcount(mr); bshift = high_bit(mb)-7; bcount = bitcount(mr); ashift = high_bit(ma)-7; acount = bitcount(mr); } for (j=0; j < (int) s->img_y; ++j) { if (easy) { for (i=0; i < (int) s->img_x; ++i) { int a; out[z+2] = get8(s); out[z+1] = get8(s); out[z+0] = get8(s); z += 3; a = (easy == 2 ? get8(s) : 255); if (target == 4) out[z++] = a; } } else { for (i=0; i < (int) s->img_x; ++i) { uint32 v = (bpp == 16 ? get16le(s) : get32le(s)); int a; out[z++] = shiftsigned(v & mr, rshift, rcount); out[z++] = shiftsigned(v & mg, gshift, gcount); out[z++] = shiftsigned(v & mb, bshift, bcount); a = (ma ? shiftsigned(v & ma, ashift, acount) : 255); if (target == 4) out[z++] = a; } } skip(s, pad); } } if (flip_vertically) { stbi_uc t; for (j=0; j < (int) s->img_y>>1; ++j) { stbi_uc *p1 = out + j *s->img_x*target; stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target; for (i=0; i < (int) s->img_x*target; ++i) { t = p1[i], p1[i] = p2[i], p2[i] = t; } } } if (req_comp && req_comp != target) { out = convert_format(out, target, req_comp, s->img_x, s->img_y); if (out == NULL) return out; // convert_format frees input on failure } *x = s->img_x; *y = s->img_y; if (comp) *comp = target; return out; } #ifndef STBI_NO_STDIO stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp) { stbi_uc *data; FILE *f = fopen(filename, "rb"); if (!f) return NULL; data = stbi_bmp_load_from_file(f, x,y,comp,req_comp); fclose(f); return data; } stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp) { stbi s; start_file(&s, f); return bmp_load(&s, x,y,comp,req_comp); } #endif stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { stbi s; start_mem(&s, buffer, len); return bmp_load(&s, x,y,comp,req_comp); } // Targa Truevision - TGA // by Jonathan Dummer static int tga_test(stbi *s) { int sz; get8u(s); // discard Offset sz = get8u(s); // color type if( sz > 1 ) return 0; // only RGB or indexed allowed sz = get8u(s); // image type if( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0; // only RGB or grey allowed, +/- RLE get16(s); // discard palette start get16(s); // discard palette length get8(s); // discard bits per palette color entry get16(s); // discard x origin get16(s); // discard y origin if( get16(s) < 1 ) return 0; // test width if( get16(s) < 1 ) return 0; // test height sz = get8(s); // bits per pixel if( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) ) return 0; // only RGB or RGBA or grey allowed return 1; // seems to have passed everything } #ifndef STBI_NO_STDIO int stbi_tga_test_file (FILE *f) { stbi s; int r,n = ftell(f); start_file(&s, f); r = tga_test(&s); fseek(f,n,SEEK_SET); return r; } #endif int stbi_tga_test_memory (stbi_uc const *buffer, int len) { stbi s; start_mem(&s, buffer, len); return tga_test(&s); } static stbi_uc *tga_load(stbi *s, int *x, int *y, int *comp, int req_comp) { // read in the TGA header stuff int tga_offset = get8u(s); int tga_indexed = get8u(s); int tga_image_type = get8u(s); int tga_is_RLE = 0; int tga_palette_start = get16le(s); int tga_palette_len = get16le(s); int tga_palette_bits = get8u(s); int tga_x_origin = get16le(s); int tga_y_origin = get16le(s); int tga_width = get16le(s); int tga_height = get16le(s); int tga_bits_per_pixel = get8u(s); int tga_inverted = get8u(s); // image data unsigned char *tga_data; unsigned char *tga_palette = NULL; int i, j; unsigned char raw_data[4]; unsigned char trans_data[4]; int RLE_count = 0; int RLE_repeating = 0; int read_next_pixel = 1; // do a tiny bit of precessing if( tga_image_type >= 8 ) { tga_image_type -= 8; tga_is_RLE = 1; } /* int tga_alpha_bits = tga_inverted & 15; */ tga_inverted = 1 - ((tga_inverted >> 5) & 1); // error check if( //(tga_indexed) || (tga_width < 1) || (tga_height < 1) || (tga_image_type < 1) || (tga_image_type > 3) || ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) && (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32)) ) { return NULL; } // If I'm paletted, then I'll use the number of bits from the palette if( tga_indexed ) { tga_bits_per_pixel = tga_palette_bits; } // tga info *x = tga_width; *y = tga_height; if( (req_comp < 1) || (req_comp > 4) ) { // just use whatever the file was req_comp = tga_bits_per_pixel / 8; *comp = req_comp; } else { // force a new number of components *comp = tga_bits_per_pixel/8; } tga_data = (unsigned char*)malloc( tga_width * tga_height * req_comp ); // skip to the data's starting position (offset usually = 0) skip(s, tga_offset ); // do I need to load a palette? if( tga_indexed ) { // any data to skip? (offset usually = 0) skip(s, tga_palette_start ); // load the palette tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 ); getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 ); } // load the data for( i = 0; i < tga_width * tga_height; ++i ) { // if I'm in RLE mode, do I need to get a RLE chunk? if( tga_is_RLE ) { if( RLE_count == 0 ) { // yep, get the next byte as a RLE command int RLE_cmd = get8u(s); RLE_count = 1 + (RLE_cmd & 127); RLE_repeating = RLE_cmd >> 7; read_next_pixel = 1; } else if( !RLE_repeating ) { read_next_pixel = 1; } } else { read_next_pixel = 1; } // OK, if I need to read a pixel, do it now if( read_next_pixel ) { // load however much data we did have if( tga_indexed ) { // read in 1 byte, then perform the lookup int pal_idx = get8u(s); if( pal_idx >= tga_palette_len ) { // invalid index pal_idx = 0; } pal_idx *= tga_bits_per_pixel / 8; for( j = 0; j*8 < tga_bits_per_pixel; ++j ) { raw_data[j] = tga_palette[pal_idx+j]; } } else { // read in the data raw for( j = 0; j*8 < tga_bits_per_pixel; ++j ) { raw_data[j] = get8u(s); } } // convert raw to the intermediate format switch( tga_bits_per_pixel ) { case 8: // Luminous => RGBA trans_data[0] = raw_data[0]; trans_data[1] = raw_data[0]; trans_data[2] = raw_data[0]; trans_data[3] = 255; break; case 16: // Luminous,Alpha => RGBA trans_data[0] = raw_data[0]; trans_data[1] = raw_data[0]; trans_data[2] = raw_data[0]; trans_data[3] = raw_data[1]; break; case 24: // BGR => RGBA trans_data[0] = raw_data[2]; trans_data[1] = raw_data[1]; trans_data[2] = raw_data[0]; trans_data[3] = 255; break; case 32: // BGRA => RGBA trans_data[0] = raw_data[2]; trans_data[1] = raw_data[1]; trans_data[2] = raw_data[0]; trans_data[3] = raw_data[3]; break; } // clear the reading flag for the next pixel read_next_pixel = 0; } // end of reading a pixel // convert to final format switch( req_comp ) { case 1: // RGBA => Luminance tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]); break; case 2: // RGBA => Luminance,Alpha tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]); tga_data[i*req_comp+1] = trans_data[3]; break; case 3: // RGBA => RGB tga_data[i*req_comp+0] = trans_data[0]; tga_data[i*req_comp+1] = trans_data[1]; tga_data[i*req_comp+2] = trans_data[2]; break; case 4: // RGBA => RGBA tga_data[i*req_comp+0] = trans_data[0]; tga_data[i*req_comp+1] = trans_data[1]; tga_data[i*req_comp+2] = trans_data[2]; tga_data[i*req_comp+3] = trans_data[3]; break; } // in case we're in RLE mode, keep counting down --RLE_count; } // do I need to invert the image? if( tga_inverted ) { for( j = 0; j*2 < tga_height; ++j ) { int index1 = j * tga_width * req_comp; int index2 = (tga_height - 1 - j) * tga_width * req_comp; for( i = tga_width * req_comp; i > 0; --i ) { unsigned char temp = tga_data[index1]; tga_data[index1] = tga_data[index2]; tga_data[index2] = temp; ++index1; ++index2; } } } // clear my palette, if I had one if( tga_palette != NULL ) { free( tga_palette ); } // the things I do to get rid of an error message, and yet keep // Microsoft's C compilers happy... [8^( tga_palette_start = tga_palette_len = tga_palette_bits = tga_x_origin = tga_y_origin = 0; // OK, done return tga_data; } #ifndef STBI_NO_STDIO stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp) { stbi_uc *data; FILE *f = fopen(filename, "rb"); if (!f) return NULL; data = stbi_tga_load_from_file(f, x,y,comp,req_comp); fclose(f); return data; } stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp) { stbi s; start_file(&s, f); return tga_load(&s, x,y,comp,req_comp); } #endif stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { stbi s; start_mem(&s, buffer, len); return tga_load(&s, x,y,comp,req_comp); } // ************************************************************************************************* // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicholas Schulz, tweaked by STB static int psd_test(stbi *s) { if (get32(s) != 0x38425053) return 0; // "8BPS" else return 1; } #ifndef STBI_NO_STDIO int stbi_psd_test_file(FILE *f) { stbi s; int r,n = ftell(f); start_file(&s, f); r = psd_test(&s); fseek(f,n,SEEK_SET); return r; } #endif int stbi_psd_test_memory(stbi_uc const *buffer, int len) { stbi s; start_mem(&s, buffer, len); return psd_test(&s); } static stbi_uc *psd_load(stbi *s, int *x, int *y, int *comp, int req_comp) { int pixelCount; int channelCount, compression; int channel, i, count, len; int w,h; uint8 *out; // Check identifier if (get32(s) != 0x38425053) // "8BPS" return epuc("not PSD", "Corrupt PSD image"); // Check file type version. if (get16(s) != 1) return epuc("wrong version", "Unsupported version of PSD image"); // Skip 6 reserved bytes. skip(s, 6 ); // Read the number of channels (R, G, B, A, etc). channelCount = get16(s); if (channelCount < 0 || channelCount > 16) return epuc("wrong channel count", "Unsupported number of channels in PSD image"); // Read the rows and columns of the image. h = get32(s); w = get32(s); // Make sure the depth is 8 bits. if (get16(s) != 8) return epuc("unsupported bit depth", "PSD bit depth is not 8 bit"); // Make sure the color mode is RGB. // Valid options are: // 0: Bitmap // 1: Grayscale // 2: Indexed color // 3: RGB color // 4: CMYK color // 7: Multichannel // 8: Duotone // 9: Lab color if (get16(s) != 3) return epuc("wrong color format", "PSD is not in RGB color format"); // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.) skip(s,get32(s) ); // Skip the image resources. (resolution, pen tool paths, etc) skip(s, get32(s) ); // Skip the reserved data. skip(s, get32(s) ); // Find out if the data is compressed. // Known values: // 0: no compression // 1: RLE compressed compression = get16(s); if (compression > 1) return epuc("bad compression", "PSD has an unknown compression format"); // Create the destination image. out = (stbi_uc *) malloc(4 * w*h); if (!out) return epuc("outofmem", "Out of memory"); pixelCount = w*h; // Initialize the data to zero. //memset( out, 0, pixelCount * 4 ); // Finally, the image data. if (compression) { // RLE as used by .PSD and .TIFF // Loop until you get the number of unpacked bytes you are expecting: // Read the next source byte into n. // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally. // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times. // Else if n is 128, noop. // Endloop // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data, // which we're going to just skip. skip(s, h * channelCount * 2 ); // Read the RLE data by channel. for (channel = 0; channel < 4; channel++) { uint8 *p; p = out+channel; if (channel >= channelCount) { // Fill this channel with default data. for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4; } else { // Read the RLE data. count = 0; while (count < pixelCount) { len = get8(s); if (len == 128) { // No-op. } else if (len < 128) { // Copy next len+1 bytes literally. len++; count += len; while (len) { *p = get8(s); p += 4; len--; } } else if (len > 128) { uint32 val; // Next -len+1 bytes in the dest are replicated from next source byte. // (Interpret len as a negative 8-bit int.) len ^= 0x0FF; len += 2; val = get8(s); count += len; while (len) { *p = val; p += 4; len--; } } } } } } else { // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...) // where each channel consists of an 8-bit value for each pixel in the image. // Read the data by channel. for (channel = 0; channel < 4; channel++) { uint8 *p; p = out + channel; if (channel > channelCount) { // Fill this channel with default data. for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4; } else { // Read the data. count = 0; for (i = 0; i < pixelCount; i++) *p = get8(s), p += 4; } } } if (req_comp && req_comp != 4) { out = convert_format(out, 4, req_comp, w, h); if (out == NULL) return out; // convert_format frees input on failure } if (comp) *comp = channelCount; *y = h; *x = w; return out; } #ifndef STBI_NO_STDIO stbi_uc *stbi_psd_load(char const *filename, int *x, int *y, int *comp, int req_comp) { stbi_uc *data; FILE *f = fopen(filename, "rb"); if (!f) return NULL; data = stbi_psd_load_from_file(f, x,y,comp,req_comp); fclose(f); return data; } stbi_uc *stbi_psd_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) { stbi s; start_file(&s, f); return psd_load(&s, x,y,comp,req_comp); } #endif stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { stbi s; start_mem(&s, buffer, len); return psd_load(&s, x,y,comp,req_comp); } // ************************************************************************************************* // Radiance RGBE HDR loader // originally by Nicolas Schulz #ifndef STBI_NO_HDR static int hdr_test(stbi *s) { char *signature = "#?RADIANCE\n"; int i; for (i=0; signature[i]; ++i) if (get8(s) != signature[i]) return 0; return 1; } int stbi_hdr_test_memory(stbi_uc const *buffer, int len) { stbi s; start_mem(&s, buffer, len); return hdr_test(&s); } #ifndef STBI_NO_STDIO int stbi_hdr_test_file(FILE *f) { stbi s; int r,n = ftell(f); start_file(&s, f); r = hdr_test(&s); fseek(f,n,SEEK_SET); return r; } #endif #define HDR_BUFLEN 1024 static char *hdr_gettoken(stbi *z, char *buffer) { int len=0; char *s = buffer, c = '\0'; c = get8(z); while (!at_eof(z) && c != '\n') { buffer[len++] = c; if (len == HDR_BUFLEN-1) { // flush to end of line while (!at_eof(z) && get8(z) != '\n') ; break; } c = get8(z); } buffer[len] = 0; return buffer; } static void hdr_convert(float *output, stbi_uc *input, int req_comp) { if( input[3] != 0 ) { float f1; // Exponent f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8)); if (req_comp <= 2) output[0] = (input[0] + input[1] + input[2]) * f1 / 3; else { output[0] = input[0] * f1; output[1] = input[1] * f1; output[2] = input[2] * f1; } if (req_comp == 2) output[1] = 1; if (req_comp == 4) output[3] = 1; } else { switch (req_comp) { case 4: output[3] = 1; /* fallthrough */ case 3: output[0] = output[1] = output[2] = 0; break; case 2: output[1] = 1; /* fallthrough */ case 1: output[0] = 0; break; } } } static float *hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp) { char buffer[HDR_BUFLEN]; char *token; int valid = 0; int width, height; stbi_uc *scanline; float *hdr_data; int len; unsigned char count, value; int i, j, k, c1,c2, z; // Check identifier if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0) return epf("not HDR", "Corrupt HDR image"); // Parse header while(1) { token = hdr_gettoken(s,buffer); if (token[0] == 0) break; if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; } if (!valid) return epf("unsupported format", "Unsupported HDR format"); // Parse width and height // can't use sscanf() if we're not using stdio! token = hdr_gettoken(s,buffer); if (strncmp(token, "-Y ", 3)) return epf("unsupported data layout", "Unsupported HDR format"); token += 3; height = strtol(token, &token, 10); while (*token == ' ') ++token; if (strncmp(token, "+X ", 3)) return epf("unsupported data layout", "Unsupported HDR format"); token += 3; width = strtol(token, NULL, 10); *x = width; *y = height; *comp = 3; if (req_comp == 0) req_comp = 3; // Read data hdr_data = (float *) malloc(height * width * req_comp * sizeof(float)); // Load image data // image data is stored as some number of sca if( width < 8 || width >= 32768) { // Read flat data for (j=0; j < height; ++j) { for (i=0; i < width; ++i) { stbi_uc rgbe[4]; main_decode_loop: getn(s, rgbe, 4); hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp); } } } else { // Read RLE-encoded data scanline = NULL; for (j = 0; j < height; ++j) { c1 = get8(s); c2 = get8(s); len = get8(s); if (c1 != 2 || c2 != 2 || (len & 0x80)) { // not run-length encoded, so we have to actually use THIS data as a decoded // pixel (note this can't be a valid pixel--one of RGB must be >= 128) stbi_uc rgbe[4] = { c1,c2,len, get8(s) }; hdr_convert(hdr_data, rgbe, req_comp); i = 1; j = 0; free(scanline); goto main_decode_loop; // yes, this is fucking insane; blame the fucking insane format } len <<= 8; len |= get8(s); if (len != width) { free(hdr_data); free(scanline); return epf("invalid decoded scanline length", "corrupt HDR"); } if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4); for (k = 0; k < 4; ++k) { i = 0; while (i < width) { count = get8(s); if (count > 128) { // Run value = get8(s); count -= 128; for (z = 0; z < count; ++z) scanline[i++ * 4 + k] = value; } else { // Dump for (z = 0; z < count; ++z) scanline[i++ * 4 + k] = get8(s); } } } for (i=0; i < width; ++i) hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp); } free(scanline); } return hdr_data; } #ifndef STBI_NO_STDIO float *stbi_hdr_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) { stbi s; start_file(&s,f); return hdr_load(&s,x,y,comp,req_comp); } #endif float *stbi_hdr_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) { stbi s; start_mem(&s,buffer, len); return hdr_load(&s,x,y,comp,req_comp); } #endif // STBI_NO_HDR /////////////////////// write image /////////////////////// #ifndef STBI_NO_WRITE static void write8(FILE *f, int x) { uint8 z = (uint8) x; fwrite(&z,1,1,f); } static void writefv(FILE *f, char *fmt, va_list v) { while (*fmt) { switch (*fmt++) { case ' ': break; case '1': { uint8 x = va_arg(v, int); write8(f,x); break; } case '2': { int16 x = va_arg(v, int); write8(f,x); write8(f,x>>8); break; } case '4': { int32 x = va_arg(v, int); write8(f,x); write8(f,x>>8); write8(f,x>>16); write8(f,x>>24); break; } default: assert(0); va_end(v); return; } } } static void writef(FILE *f, char *fmt, ...) { va_list v; va_start(v, fmt); writefv(f,fmt,v); va_end(v); } static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad) { uint8 bg[3] = { 255, 0, 255}, px[3]; uint32 zero = 0; int i,j,k, j_end; if (vdir < 0) j_end = -1, j = y-1; else j_end = y, j = 0; for (; j != j_end; j += vdir) { for (i=0; i < x; ++i) { uint8 *d = (uint8 *) data + (j*x+i)*comp; if (write_alpha < 0) fwrite(&d[comp-1], 1, 1, f); switch (comp) { case 1: case 2: writef(f, "111", d[0],d[0],d[0]); break; case 4: if (!write_alpha) { for (k=0; k < 3; ++k) px[k] = bg[k] + ((d[k] - bg[k]) * d[3])/255; writef(f, "111", px[1-rgb_dir],px[1],px[1+rgb_dir]); break; } /* FALLTHROUGH */ case 3: writef(f, "111", d[1-rgb_dir],d[1],d[1+rgb_dir]); break; } if (write_alpha > 0) fwrite(&d[comp-1], 1, 1, f); } fwrite(&zero,scanline_pad,1,f); } } static int outfile(char const *filename, int rgb_dir, int vdir, int x, int y, int comp, void *data, int alpha, int pad, char *fmt, ...) { FILE *f = fopen(filename, "wb"); if (f) { va_list v; va_start(v, fmt); writefv(f, fmt, v); va_end(v); write_pixels(f,rgb_dir,vdir,x,y,comp,data,alpha,pad); fclose(f); } return f != NULL; } int stbi_write_bmp(char const *filename, int x, int y, int comp, void *data) { int pad = (-x*3) & 3; return outfile(filename,-1,-1,x,y,comp,data,0,pad, "11 4 22 4" "4 44 22 444444", 'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header 40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header } int stbi_write_tga(char const *filename, int x, int y, int comp, void *data) { int has_alpha = !(comp & 1); return outfile(filename, -1,-1, x, y, comp, data, has_alpha, 0, "111 221 2222 11", 0,0,2, 0,0,0, 0,0,x,y, 24+8*has_alpha, 8*has_alpha); } // any other image formats that do interleaved rgb data? // PNG: requires adler32,crc32 -- significant amount of code // PSD: no, channels output separately // TIFF: no, stripwise-interleaved... i think #endif // STBI_NO_WRITE #endif // STBI_HEADER_FILE_ONLY dokidoki-support/stb_vorbis.c000066400000000000000000005553071147057410300167260ustar00rootroot00000000000000// Ogg Vorbis I audio decoder -- version 0.99996 // // Written in April 2007 by Sean Barrett, sponsored by RAD Game Tools. // // Placed in the public domain April 2007 by the author: no copyright is // claimed, and you may use it for any purpose you like. // // No warranty for any purpose is expressed or implied by the author (nor // by RAD Game Tools). Report bugs and send enhancements to the author. // // Get the latest version and other information at: // http://nothings.org/stb_vorbis/ // Todo: // // - seeking (note you can seek yourself using the pushdata API) // // Limitations: // // - floor 0 not supported (used in old ogg vorbis files) // - lossless sample-truncation at beginning ignored // - cannot concatenate multiple vorbis streams // - sample positions are 32-bit, limiting seekable 192Khz // files to around 6 hours (Ogg supports 64-bit) // // All of these limitations may be removed in future versions. ////////////////////////////////////////////////////////////////////////////// // // HEADER BEGINS HERE // #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H #define STB_VORBIS_INCLUDE_STB_VORBIS_H #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) #define STB_VORBIS_NO_STDIO 1 #endif #ifndef STB_VORBIS_NO_STDIO #include #endif #ifdef __cplusplus extern "C" { #endif /////////// 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. /////////// 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() 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; typedef struct { unsigned int sample_rate; int channels; unsigned int setup_memory_required; unsigned int setup_temp_memory_required; unsigned int temp_memory_required; int max_frame_size; } stb_vorbis_info; // 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); // close an ogg vorbis file and free all memory in use extern void stb_vorbis_close(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 #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. extern stb_vorbis *stb_vorbis_open_pushdata( unsigned char *datablock, int datablock_length_in_bytes, int *datablock_memory_consumed_in_bytes, int *error, 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, 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); // 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 // once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it // will begin decoding the _next_ frame. // // if you want to seek using pushdata, you need to seek in your file, then // call stb_vorbis_flush_pushdata(), then start calling decoding, then once // decoding is returning you data, call stb_vorbis_get_sample_offset, and // if you don't like the result, seek your file again and repeat. #endif ////////// 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.) #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION) extern int stb_vorbis_decode_filename(char *filename, int *channels, short **output); #endif extern int stb_vorbis_decode_memory(unsigned char *mem, int len, int *channels, short **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(unsigned char *data, int len, int *error, 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(char *filename, int *error, 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, 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. Morever, 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, 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" // this stream; if you seek it in between calls to stb_vorbis, it will become // 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); // NOT WORKING YET // 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 void stb_vorbis_seek_start(stb_vorbis *f); // this function is equivalent to stb_vorbis_seek(f,0), but it // actually works 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. #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); #endif // decode the next frame and return the number of samples per channel. 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. 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. // 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. #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); #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. #endif //////// ERROR CODES enum STBVorbisError { VORBIS__no_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_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 // 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, }; #ifdef __cplusplus } #endif #endif // STB_VORBIS_INCLUDE_STB_VORBIS_H // // HEADER ENDS HERE // ////////////////////////////////////////////////////////////////////////////// #ifndef STB_VORBIS_HEADER_ONLY // global configuration settings (e.g. set these in the project/makefile), // or just set them in this file at the top (although ideally the first few // should be visible when the header file is compiled too, although it's not // crucial) // STB_VORBIS_NO_PUSHDATA_API // does not compile the code for the various stb_vorbis_*_pushdata() // functions // #define STB_VORBIS_NO_PUSHDATA_API // STB_VORBIS_NO_PULLDATA_API // does not compile the code for the non-pushdata APIs // #define STB_VORBIS_NO_PULLDATA_API // STB_VORBIS_NO_STDIO // does not compile the code for the APIs that use FILE *s internally // or externally (implied by STB_VORBIS_NO_PULLDATA_API) // #define STB_VORBIS_NO_STDIO // STB_VORBIS_NO_INTEGER_CONVERSION // does not compile the code for converting audio sample data from // float to integer (implied by STB_VORBIS_NO_PULLDATA_API) // #define STB_VORBIS_NO_INTEGER_CONVERSION // 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 // STB_VORBIS_MAX_CHANNELS [number] // globally define this to the maximum number of channels you need. // The spec does not put a restriction on channels except that // the count is stored in a byte, so 255 is the hard limit. // Reducing this saves about 16 bytes per value, so using 16 saves // (255-16)*16 or around 4KB. Plus anything other memory usage // 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? #endif // STB_VORBIS_PUSHDATA_CRC_COUNT [number] // after a flush_pushdata(), stb_vorbis begins scanning for the // next valid page, without backtracking. when it finds something // that looks like a page, it streams through it and verifies its // CRC32. Should that validation fail, it keeps scanning. But it's // possible that _while_ streaming through to check the CRC32 of // one candidate page, it sees another candidate page. This #define // determines how many "overlapping" candidate pages it can search // at once. Note that "real" pages are typically ~4KB to ~8KB, whereas // garbage pages could be as big as 64KB, but probably average ~16KB. // 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 #endif // STB_VORBIS_FAST_HUFFMAN_LENGTH [number] // sets the log size of the huffman-acceleration table. Maximum // supported value is 24. with larger numbers, more decodings are O(1), // 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 #endif // STB_VORBIS_FAST_BINARY_LENGTH [number] // sets the log size of the binary-search acceleration table. this // is used in similar fashion to the fast-huffman size to set initial // parameters for the binary search // STB_VORBIS_FAST_HUFFMAN_INT // The fast huffman tables are much more efficient if they can be // stored as 16-bit results instead of 32-bit results. This restricts // the codebooks to having only 65535 possible outcomes, though. // (At least, accelerated by the huffman table.) #ifndef STB_VORBIS_FAST_HUFFMAN_INT #define STB_VORBIS_FAST_HUFFMAN_SHORT #endif // STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH // If the 'fast huffman' search doesn't succeed, then stb_vorbis falls // back on binary searching for the correct one. This requires storing // extra tables with the huffman codes in sorted order. Defining this // symbol trades off space for speed by forcing a linear search in the // non-fast case, except for "sparse" codebooks. // #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH // STB_VORBIS_DIVIDES_IN_RESIDUE // stb_vorbis precomputes the result of the scalar residue decoding // that would otherwise require a divide per chunk. you can trade off // space for time by defining this symbol. // #define STB_VORBIS_DIVIDES_IN_RESIDUE // STB_VORBIS_DIVIDES_IN_CODEBOOK // vorbis VQ codebooks can be encoded two ways: with every case explicitly // stored, or with all elements being chosen from a small range of values, // and all values possible in all elements. By default, stb_vorbis expands // this latter kind out to look like the former kind for ease of decoding, // 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 // STB_VORBIS_CODEBOOK_SHORTS // The vorbis file format encodes VQ codebook floats as ax+b where a and // b are floating point per-codebook constants, and x is a 16-bit int. // Normally, stb_vorbis decodes them to floats rather than leaving them // as 16-bit ints and computing ax+b while decoding. This is a speed/space // tradeoff; you can save space by defining this flag. #ifndef STB_VORBIS_CODEBOOK_SHORTS #define STB_VORBIS_CODEBOOK_FLOATS #endif // STB_VORBIS_DIVIDE_TABLE // this replaces small integer divides in the floor decode loop with // table lookups. made less than 1% difference, so disabled by default. // STB_VORBIS_NO_INLINE_DECODE // disables the inlining of the scalar codebook fast-huffman decode. // might save a little codespace; useful for debugging // #define STB_VORBIS_NO_INLINE_DECODE // STB_VORBIS_NO_DEFER_FLOOR // Normally we only decode the floor without synthesizing the actual // full curve. We can instead synthesize the curve immediately. This // requires more memory and is very likely slower, so I don't think // 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 #endif #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) #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 #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 #if !(defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh)) #include #endif #else #define NULL 0 #endif #ifndef _MSC_VER #if __GNUC__ #define __forceinline inline #else #define __forceinline #endif #endif #if STB_VORBIS_MAX_CHANNELS > 256 #error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range" #endif #if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24 #error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range" #endif #define MAX_BLOCKSIZE_LOG 13 // from specification #define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG) typedef unsigned char uint8; typedef signed char int8; typedef unsigned short uint16; typedef signed short int16; typedef unsigned int uint32; typedef signed int int32; #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif #ifdef STB_VORBIS_CODEBOOK_FLOATS typedef float codetype; #else typedef uint16 codetype; #endif // @NOTE // // Some arrays below are tagged "//varies", which means it's actually // a variable-sized piece of data, but rather than malloc I assume it's // small enough it's better to just allocate it all together with the // main thing // // Most of the variables are specified with the smallest size I could pack // them into. It might give better performance to make them all full-sized // integers. It should be safe to freely rearrange the structures or change // 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) 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; 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 uint32 *sorted_codewords; int *sorted_values; int sorted_entries; } Codebook; typedef struct { uint8 order; uint16 rate; uint16 bark_map_size; uint8 amplitude_bits; uint8 amplitude_offset; uint8 number_of_books; uint8 book_list[16]; // varies } Floor0; 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 floor1_multiplier; uint8 rangebits; int values; } Floor1; typedef union { Floor0 floor0; Floor1 floor1; } Floor; typedef struct { uint32 begin, end; uint32 part_size; uint8 classifications; uint8 classbook; uint8 **classdata; int16 (*residue_books)[8]; } Residue; typedef struct { uint8 magnitude; uint8 angle; uint8 mux; } MappingChannel; typedef struct { uint16 coupling_steps; MappingChannel *chan; uint8 submaps; uint8 submap_floor[15]; // varies uint8 submap_residue[15]; // varies } Mapping; typedef struct { uint8 blockflag; uint8 mapping; uint16 windowtype; uint16 transformtype; } Mode; 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 } CRCscan; typedef struct { uint32 page_start, page_end; uint32 after_previous_page_start; uint32 first_decoded_sample; uint32 last_decoded_sample; } ProbedPage; struct stb_vorbis { // user-accessible info unsigned int sample_rate; int channels; unsigned int setup_memory_required; unsigned int temp_memory_required; unsigned int setup_temp_memory_required; // input config #ifndef STB_VORBIS_NO_STDIO FILE *f; uint32 f_start; int close_on_free; #endif uint8 *stream; uint8 *stream_start; uint8 *stream_end; uint32 stream_len; uint8 push_mode; uint32 first_audio_page_offset; ProbedPage p_first, p_last; // memory management stb_vorbis_alloc alloc; int setup_offset; int temp_offset; // run-time results int eof; enum STBVorbisError error; // user-useful data // header info int blocksize[2]; int blocksize_0, blocksize_1; int codebook_count; Codebook *codebooks; int floor_count; uint16 floor_types[64]; // varies Floor *floor_config; int residue_count; uint16 residue_types[64]; // varies Residue *residue_config; int mapping_count; Mapping *mapping; int mode_count; Mode mode_config[64]; // varies uint32 total_samples; // decode buffer float *channel_buffers[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 int16 *finalY[STB_VORBIS_MAX_CHANNELS]; #else float *floor_buffers[STB_VORBIS_MAX_CHANNELS]; #endif uint32 current_loc; // sample location of next frame to decode int current_loc_valid; // per-blocksize precomputed data // twiddle factors float *A[2],*B[2],*C[2]; float *window[2]; uint16 *bit_reverse[2]; // current page/packet/segment streaming info uint32 serial; // stream serial number for verification int last_page; int segment_count; uint8 segments[255]; uint8 page_flag; uint8 bytes_in_seg; uint8 first_decode; int next_seg; 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; int packet_bytes; int end_seg_with_known_loc; uint32 known_loc_for_packet; int discard_samples_deferred; uint32 samples_output; // 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 int channel_buffer_start; int channel_buffer_end; }; extern int my_prof(int slot); //#define stb_prof my_prof #ifndef stb_prof #define stb_prof(x) 0 #endif #if defined(STB_VORBIS_NO_PUSHDATA_API) #define IS_PUSH_MODE(f) FALSE #elif defined(STB_VORBIS_NO_PULLDATA_API) #define IS_PUSH_MODE(f) TRUE #else #define IS_PUSH_MODE(f) ((f)->push_mode) #endif 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 } 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 temp_alloc(f,size) (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size)) #ifdef dealloca #define temp_free(f,p) (f->alloc.alloc_buffer ? 0 : dealloca(size)) #else #define temp_free(f,p) 0 #endif #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) // 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) { p[i] = q; q += size; } return p; } static void *setup_malloc(vorb *f, int sz) { sz = (sz+3) & ~3; 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; f->setup_offset += sz; return p; } return sz ? malloc(sz) : NULL; } static void setup_free(vorb *f, void *p) { if (f->alloc.alloc_buffer) return; // do nothing; setup mem is not a stack free(p); } static void *setup_temp_malloc(vorb *f, int sz) { sz = (sz+3) & ~3; 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 malloc(sz); } static void setup_temp_free(vorb *f, void *p, size_t sz) { if (f->alloc.alloc_buffer) { f->temp_offset += (sz+3)&~3; return; } free(p); } #define CRC32_POLY 0x04c11db7 // from spec static uint32 crc_table[256]; static void crc32_init(void) { int i,j; uint32 s; for(i=0; i < 256; i++) { for (s=i<<24, j=0; j < 8; ++j) s = (s << 1) ^ (s >= (1<<31) ? CRC32_POLY : 0); crc_table[i] = s; } } 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); } static float square(float x) { return x*x; } // this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3 // as required by the specification. fast(?) implementation from stb.h // @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 }; // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29) if (n < (1U << 14)) if (n < (1U << 4)) return 0 + log2_4[n ]; else if (n < (1U << 9)) return 5 + log2_4[n >> 5]; else return 10 + log2_4[n >> 10]; else if (n < (1U << 24)) if (n < (1U << 19)) return 15 + log2_4[n >> 15]; else return 20 + log2_4[n >> 20]; else if (n < (1U << 29)) return 25 + log2_4[n >> 25]; else if (n < (1U << 31)) return 30 + log2_4[n >> 30]; else return 0; // signed n returns 0 } #ifndef M_PI #define M_PI 3.14159265358979323846264f // from CRC #endif // code length assigned to a value with no huffman encoding #define NO_CODE 255 /////////////////////// LEAF SETUP FUNCTIONS ////////////////////////// // // these functions are only called at setup, and only a few times // per file static float float32_unpack(uint32 x) { // from the specification uint32 mantissa = x & 0x1fffff; uint32 sign = x & 0x80000000; uint32 exp = (x & 0x7fe00000) >> 21; double res = sign ? -(double)mantissa : (double)mantissa; 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; // this makes for a very simple generation algorithm. // vorbis allows a huffman table with non-sorted lengths. This // requires a more sophisticated construction, since symbols in // 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; c->codeword_lengths[count] = len; values [count] = symbol; } } static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values) { 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; } // 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] = 1 << (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) { uint32 res; int z = len[i], y; 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) { assert(0); return FALSE; } res = available[z]; available[z] = 0; add_entry(c, bit_reverse(res), i, m++, len[i], values); // propogate availability up the tree if (z != len[i]) { for (y=len[i]; y > z; --y) { assert(available[y] == 0); available[y] = res + (1 << (32-y)); } } } return TRUE; } // accelerated huffman table allows fast O(1) match of all symbols // of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH static void compute_accelerated_huffman(Codebook *c) { int i, len; 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) { 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]; } } } } static int uint32_compare(const void *p, const void *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; return FALSE; } // if the fast table above doesn't work, we want to binary // search them... need to reverse the bits static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) { int i, len; // build a list of all the entries // 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) { int k = 0; 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) c->sorted_codewords[i] = bit_reverse(c->codewords[i]); } qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare); c->sorted_codewords[c->sorted_entries] = 0xffffffff; len = c->sparse ? c->sorted_entries : c->entries; // now we need to indicate how they correspond; we could either // #1: sort a different data structure that says who they correspond to // #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) { int huff_len = c->sparse ? lengths[values[i]] : lengths[i]; if (include_in_sort(c,huff_len)) { uint32 code = bit_reverse(c->codewords[i]); 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) { x = m; n -= (n>>1); } else { n >>= 1; } } assert(c->sorted_codewords[x] == code); if (c->sparse) { c->sorted_values[x] = values[i]; c->codeword_lengths[x] = huff_len; } else { c->sorted_values[x] = i; } } } } // only run while parsing the header (3 times) static int vorbis_validate(uint8 *data) { static uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' }; return memcmp(data, vorbis, 6) == 0; } // called from setup only, once per code book // (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 assert(pow((float) r+1, dim) > entries); assert((int) floor(pow((float) r, dim)) <= entries); // (int),floor() as above return r; } // called twice per file static void compute_twiddle_factors(int n, float *A, float *B, float *C) { int n4 = n >> 2, n8 = n >> 3; 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 < 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))); } 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; } 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); 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); 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); compute_bitreverse(n, f->bit_reverse[b]); return TRUE; } 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]; } } } // this has been repurposed so y is now the original index instead of y typedef struct { uint16 x,y; } Point; int point_compare(const void *p, const void *q) { Point *a = (Point *) p; Point *b = (Point *) 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 #else #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; } return *z->stream++; } #ifndef STB_VORBIS_NO_STDIO { int c = fgetc(z->f); if (c == EOF) { z->eof = TRUE; return 0; } return c; } #endif } static uint32 get32(vorb *f) { uint32 x; x = get8(f); x += get8(f) << 8; x += get8(f) << 16; x += 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; } memcpy(data, z->stream, n); z->stream += n; return 1; } #ifndef STB_VORBIS_NO_STDIO if (fread(data, n, 1, z->f) == 1) return 1; else { z->eof = 1; return 0; } #endif } static void skip(vorb *z, int n) { if (USE_MEMORY(z)) { z->stream += n; if (z->stream >= z->stream_end) z->eof = 1; return; } #ifndef STB_VORBIS_NO_STDIO { long x = ftell(z->f); fseek(z->f, x+n, SEEK_SET); } #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 f->eof = 0; 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 { f->stream = f->stream_start + loc; return 1; } } #ifndef STB_VORBIS_NO_STDIO if (loc + f->f_start < loc || loc >= 0x80000000) { loc = 0x7fffffff; f->eof = 1; } else { loc += f->f_start; } if (!fseek(f->f, loc, SEEK_SET)) return 1; f->eof = 1; fseek(f->f, f->f_start, SEEK_END); return 0; #endif } 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; return TRUE; } #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,i; // 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 loc0 = get32(f); loc1 = get32(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 n = get32(f); f->last_page = n; // CRC32 get32(f); // page_segments f->segment_count = get8(f); if (!getn(f, f->segments, f->segment_count)) 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 != ~0 || loc1 != ~0) { // determine which packet is the last one that will complete 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) { f->end_seg_with_known_loc = i; f->known_loc_for_packet = loc0; } } if (f->first_decode) { int i,len; ProbedPage p; len = 0; for (i=0; i < f->segment_count; ++i) len += f->segments[i]; len += 27 + f->segment_count; p.page_start = f->first_audio_page_offset; p.page_end = p.page_start + len; p.after_previous_page_start = p.page_start; p.first_decoded_sample = 0; p.last_decoded_sample = loc0; f->p_first = p; } f->next_seg = 0; return TRUE; } static int start_page(vorb *f) { 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; if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_continued_packet_flag_invalid); } f->last_seg = FALSE; f->valid_bits = 0; f->packet_bytes = 0; f->bytes_in_seg = 0; // f->next_seg is now valid return TRUE; } static int maybe_start_packet(vorb *f) { 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) { // set up enough state that we can read this packet if we want, // e.g. during recovery f->last_seg = FALSE; f->bytes_in_seg = 0; return error(f, VORBIS_continued_packet_flag_invalid); } } return start_packet(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); } len = f->segments[f->next_seg++]; if (len < 255) { f->last_seg = TRUE; f->last_seg_which = f->next_seg-1; } if (f->next_seg >= f->segment_count) f->next_seg = -1; assert(f->bytes_in_seg == 0); f->bytes_in_seg = len; return len; } #define EOP (-1) #define INVALID_BITS (-1) static int get8_packet_raw(vorb *f) { if (!f->bytes_in_seg) if (f->last_seg) return EOP; else if (!next_segment(f)) return EOP; assert(f->bytes_in_seg > 0); --f->bytes_in_seg; ++f->packet_bytes; return get8(f); } static int get8_packet(vorb *f) { int x = get8_packet_raw(f); f->valid_bits = 0; return x; } static void flush_packet(vorb *f) { while (get8_packet_raw(f) != EOP); } // @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important // as the huffman decoder? 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) { // the accumulator technique below would not work correctly in this case z = get_bits(f, 24); z += get_bits(f, n-24) << 24; return z; } if (f->valid_bits == 0) f->acc = 0; while (f->valid_bits < n) { int z = get8_packet_raw(f); if (z == EOP) { f->valid_bits = INVALID_BITS; return 0; } f->acc += z << f->valid_bits; f->valid_bits += 8; } } if (f->valid_bits < 0) return 0; z = f->acc & ((1 << n)-1); f->acc >>= n; f->valid_bits -= n; return z; } static int32 get_bits_signed(vorb *f, int n) { uint32 z = get_bits(f, n); if (z & (1 << (n-1))) z += ~((1 << n) - 1); return (int32) z; } // @OPTIMIZE: primary accumulator for huffman // expand the buffer to as many bits as possible without reading off end of packet // it might be nice to allow f->valid_bits and f->acc to be stored in registers, // 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 { int z; if (f->last_seg && !f->bytes_in_seg) return; z = get8_packet_raw(f); if (z == EOP) return; f->acc += z << f->valid_bits; f->valid_bits += 8; } while (f->valid_bits <= 24); } } enum { VORBIS_packet_id = 1, VORBIS_packet_comment = 3, VORBIS_packet_setup = 5, }; static int codebook_decode_scalar_raw(vorb *f, Codebook *c) { int i; prep_huffman(f); assert(c->sorted_codewords || c->codewords); // cases to use binary search: sorted_codewords && !c->codewords // sorted_codewords && c->entries > 8 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; while (n > 1) { // invariant: sc[x] <= code < sc[x+n] int m = x + (n >> 1); if (c->sorted_codewords[m] <= code) { x = m; n -= (n>>1); } else { n >>= 1; } } // x is now the sorted index 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) { f->acc >>= len; f->valid_bits -= len; return x; } f->valid_bits = 0; return -1; } // 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]) { f->acc >>= c->codeword_lengths[i]; f->valid_bits -= c->codeword_lengths[i]; return i; } f->valid_bits = 0; return -1; } } error(f, VORBIS_invalid_stream); f->valid_bits = 0; return -1; } static int codebook_decode_scalar(vorb *f, Codebook *c) { int i; if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH) prep_huffman(f); // fast huffman table lookup i = f->acc & FAST_HUFFMAN_TABLE_MASK; i = c->fast_huffman[i]; 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; } return i; } return codebook_decode_scalar_raw(f,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); \ } #else #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]; #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK #define DECODE_VQ(var,f,c) DECODE_RAW(var,f,c) #else #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 #ifndef STB_VORBIS_CODEBOOK_FLOATS #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off] * c->delta_value + c->minimum_value) #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off] * c->delta_value) #define CODEBOOK_ELEMENT_BASE(c) (c->minimum_value) #else #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off]) #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off]) #define CODEBOOK_ELEMENT_BASE(c) (0) #endif static int codebook_decode_start(vorb *f, Codebook *c, int len) { int z = -1; // 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 if (!f->bytes_in_seg) if (f->last_seg) return z; error(f, VORBIS_invalid_stream); } } return z; } static int codebook_decode(vorb *f, Codebook *c, float *output, int len) { int i,z = codebook_decode_start(f,c,len); if (z < 0) return FALSE; if (len > c->dimensions) len = c->dimensions; #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK if (c->lookup_type == 1) { float last = CODEBOOK_ELEMENT_BASE(c); int div = 1; for (i=0; i < len; ++i) { int off = (z / div) % c->lookup_values; float val = CODEBOOK_ELEMENT_FAST(c,off) + last; output[i] += val; if (c->sequence_p) last = val + c->minimum_value; div *= c->lookup_values; } return TRUE; } #endif z *= c->dimensions; 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; output[i] += val; last = val + c->minimum_value; } } else { float last = CODEBOOK_ELEMENT_BASE(c); for (i=0; i < len; ++i) { output[i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last; } } return TRUE; } static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step) { int i,z = codebook_decode_start(f,c,len); float last = CODEBOOK_ELEMENT_BASE(c); if (z < 0) return FALSE; if (len > c->dimensions) len = c->dimensions; #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK if (c->lookup_type == 1) { int div = 1; 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; div *= c->lookup_values; } return TRUE; } #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; } return TRUE; } static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode) { int c_inter = *c_inter_p; int p_inter = *p_inter_p; 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); while (total_decode > 0) { float last = CODEBOOK_ELEMENT_BASE(c); DECODE_VQ(z,f,c); #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK assert(!c->sparse || z < c->sorted_entries); #endif if (z < 0) { if (!f->bytes_in_seg) if (f->last_seg) return FALSE; return error(f, VORBIS_invalid_stream); } // if this will take us off the end of the buffers, stop short! // 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); } #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK if (c->lookup_type == 1) { int div = 1; for (i=0; i < effective; ++i) { int off = (z / div) % c->lookup_values; float val = CODEBOOK_ELEMENT_FAST(c,off) + last; outputs[c_inter][p_inter] += val; if (++c_inter == ch) { c_inter = 0; ++p_inter; } if (c->sequence_p) last = val; div *= c->lookup_values; } } else #endif { z *= c->dimensions; if (c->sequence_p) { for (i=0; i < effective; ++i) { float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; outputs[c_inter][p_inter] += val; 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; outputs[c_inter][p_inter] += val; if (++c_inter == ch) { c_inter = 0; ++p_inter; } } } } total_decode -= effective; } *c_inter_p = c_inter; *p_inter_p = p_inter; return TRUE; } #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK static int codebook_decode_deinterleave_repeat_2(vorb *f, Codebook *c, float **outputs, int *c_inter_p, int *p_inter_p, int len, int total_decode) { int c_inter = *c_inter_p; int p_inter = *p_inter_p; 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); while (total_decode > 0) { float last = CODEBOOK_ELEMENT_BASE(c); DECODE_VQ(z,f,c); if (z < 0) { if (!f->bytes_in_seg) if (f->last_seg) return FALSE; return error(f, VORBIS_invalid_stream); } // if this will take us off the end of the buffers, stop short! // 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*2 + effective > len * 2) { effective = len*2 - (p_inter*2 - c_inter); } { z *= c->dimensions; stb_prof(11); if (c->sequence_p) { // haven't optimized this case because I don't have any examples for (i=0; i < effective; ++i) { float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; outputs[c_inter][p_inter] += val; if (++c_inter == 2) { c_inter = 0; ++p_inter; } last = val; } } else { i=0; if (c_inter == 1) { float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; outputs[c_inter][p_inter] += val; c_inter = 0; ++p_inter; ++i; } { float *z0 = outputs[0]; float *z1 = outputs[1]; for (; i+1 < effective;) { z0[p_inter] += CODEBOOK_ELEMENT_FAST(c,z+i) + last; z1[p_inter] += CODEBOOK_ELEMENT_FAST(c,z+i+1) + last; ++p_inter; i += 2; } } if (i < effective) { float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last; outputs[c_inter][p_inter] += val; if (++c_inter == 2) { c_inter = 0; ++p_inter; } } } } total_decode -= effective; } *c_inter_p = c_inter; *p_inter_p = p_inter; return TRUE; } #endif static int predict_point(int x, int x0, int x1, int y0, int y1) { int dy = y1 - y0; int adx = x1 - x0; // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86? int err = abs(dy) * (x - x0); int off = err / adx; return dy < 0 ? y0 - off : y0 + off; } // 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 }; // @OPTIMIZE: if you want to replace this bresenham line-drawing routine, // note that you must produce bit-identical output to decode correctly; // this specific sequence of operations is specified in the spec (it's // drawing integer-quantized frequency-space lines that the encoder // expects to be exactly the same) // ... 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 #else #define LINE_OP(a,b) a = b #endif #ifdef STB_VORBIS_DIVIDE_TABLE #define DIVTAB_NUMER 32 #define DIVTAB_DENOM 64 int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB #endif static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n) { int dy = y1 - y0; int adx = x1 - x0; int ady = abs(dy); int base; 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) { base = -integer_divide_table[ady][adx]; sy = base-1; } else { base = integer_divide_table[ady][adx]; sy = base+1; } } else { base = dy / adx; if (dy < 0) sy = base - 1; else sy = base+1; } #else base = dy / adx; if (dy < 0) sy = base - 1; else sy = base+1; #endif ady -= abs(base) * adx; if (x1 > n) x1 = n; LINE_OP(output[x], inverse_db_table[y]); for (++x; x < x1; ++x) { err += ady; if (err >= adx) { err -= adx; y += sy; } else y += base; LINE_OP(output[x], inverse_db_table[y]); } } static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype) { int k; 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)) return FALSE; } else { for (k=0; k < n; ) { if (!codebook_decode(f, book, target+offset, n-k)) return FALSE; k += book->dimensions; offset += book->dimensions; } } return TRUE; } static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode) { 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; int n_read = r->end - 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 stb_prof(2); for (i=0; i < ch; ++i) if (!do_not_decode[i]) memset(residue_buffers[i], 0, sizeof(float) * n); if (rtype == 2 && ch != 1) { int len = ch * n; for (j=0; j < ch; ++j) if (!do_not_decode[j]) break; if (j == ch) goto done; stb_prof(3); for (pass=0; pass < 8; ++pass) { int pcount = 0, class_set = 0; if (ch == 2) { stb_prof(13); 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 part_classdata[0][class_set] = r->classdata[q]; #else for (i=classwords-1; i >= 0; --i) { classifications[0][i+pcount] = q % r->classifications; q /= r->classifications; } #endif } stb_prof(5); 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 int c = classifications[0][pcount]; #endif int b = r->residue_books[c][pass]; if (b >= 0) { Codebook *book = f->codebooks + b; stb_prof(20); // accounts for X time #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 // saves 1% if (!codebook_decode_deinterleave_repeat_2(f, book, residue_buffers, &c_inter, &p_inter, n, r->part_size)) goto done; #endif stb_prof(7); } else { z += r->part_size; c_inter = z & 1; p_inter = z >> 1; } } stb_prof(8); #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE ++class_set; #endif } } else if (ch == 1) { while (pcount < part_read) { int z = r->begin + pcount*r->part_size; int c_inter = 0, p_inter = z; 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 part_classdata[0][class_set] = r->classdata[q]; #else for (i=classwords-1; i >= 0; --i) { classifications[0][i+pcount] = q % r->classifications; q /= r->classifications; } #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 int c = part_classdata[0][class_set][i]; #else int c = classifications[0][pcount]; #endif int b = r->residue_books[c][pass]; if (b >= 0) { Codebook *book = f->codebooks + b; stb_prof(22); if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) goto done; stb_prof(3); } else { z += r->part_size; c_inter = 0; p_inter = z; } } #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE ++class_set; #endif } } else { 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 part_classdata[0][class_set] = r->classdata[q]; #else for (i=classwords-1; i >= 0; --i) { classifications[0][i+pcount] = q % r->classifications; q /= r->classifications; } #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 int c = part_classdata[0][class_set][i]; #else int c = classifications[0][pcount]; #endif int b = r->residue_books[c][pass]; if (b >= 0) { Codebook *book = f->codebooks + b; stb_prof(22); if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size)) goto done; stb_prof(3); } else { z += r->part_size; c_inter = z % ch; p_inter = z / ch; } } #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE ++class_set; #endif } } } goto done; } stb_prof(9); 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 part_classdata[j][class_set] = r->classdata[temp]; #else for (i=classwords-1; i >= 0; --i) { classifications[j][i+pcount] = temp % r->classifications; temp /= r->classifications; } #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 int c = part_classdata[j][class_set][i]; #else int c = classifications[j][pcount]; #endif int b = r->residue_books[c][pass]; if (b >= 0) { float *target = residue_buffers[j]; int offset = r->begin + pcount * r->part_size; int n = r->part_size; Codebook *book = f->codebooks + b; if (!residue_decode(f, book, target, offset, n, rtype)) goto done; } } } } #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE ++class_set; #endif } } done: stb_prof(0); temp_alloc_restore(f,temp_alloc_point); } #if 0 // slow way for debugging void inverse_mdct_slow(float *buffer, int n) { int i,j; int n2 = n >> 1; float *x = (float *) malloc(sizeof(*x) * n2); memcpy(x, buffer, sizeof(*x) * n2); for (i=0; i < n; ++i) { float acc = 0; for (j=0; j < n2; ++j) // formula from paper: //acc += n/4.0f * x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1)); // formula from wikipedia //acc += 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5)); // these are equivalent, except the formula from the paper inverts the multiplier! // however, what actually works is NO MULTIPLIER!?! //acc += 64 * 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5)); acc += x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1)); buffer[i] = acc; } free(x); } #elif 0 // same as above, but just barely able to run in real time on modern machines 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); 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 < n; ++i) { float acc = 0; for (j=0; j < n2; ++j) acc += x[j] * mcos[(2 * i + 1 + n2)*(2*j+1) & nmask]; buffer[i] = acc; } free(x); } #else // transform to use a slow dct-iv; this is STILL basically trivial, // but only requires half as many ops void dct_iv_slow(float *buffer, int n) { float mcos[16384]; float x[2048]; 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) { float acc = 0; for (j=0; j < n; ++j) acc += x[j] * mcos[((2 * i + 1)*(2*j+1)) & nmask]; //acc += x[j] * cos(M_PI / n * (i + 0.5) * (j + 0.5)); buffer[i] = acc; } free(x); } void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) { int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4; float temp[4096]; memcpy(temp, buffer, n2 * sizeof(float)); 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 } #endif #ifndef LIBVORBIS_MDCT #define LIBVORBIS_MDCT 0 #endif #if LIBVORBIS_MDCT // directly call the vorbis MDCT using an interface documented // by Jeff Roberts... useful for performance comparison typedef struct { int n; int log2n; float *trig; int *bitrev; 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; 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; mdct_init(&M2, n); M = &M2; } mdct_backward(M, buffer, buffer); } #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. static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A) { float *ee0 = e + i_off; float *ee2 = ee0 + k_off; int i; assert((n & 3) == 0); 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]; 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]; 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]; 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]; ee2[-6] = k00_20 * A[0] - k01_21 * A[1]; ee2[-7] = k01_21 * A[0] + k00_20 * A[1]; A += 8; ee0 -= 8; ee2 -= 8; } } static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1) { int i; float k00_20, k01_21; float *e0 = e + d0; float *e2 = e0 + k_off; 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]; 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]; 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]; 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 -= 8; e2 -= 8; A += k1; } } static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0) { 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 k00,k11; 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; 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[-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; } } static __forceinline void iter_54(float *z) { 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]; 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]; 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]; 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 k_off = -8; int a_off = base_n >> 3; float A2 = A[0+a_off]; float *z = e + i_off; float *base = z - 16 * n; while (z > base) { float k00,k11; 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 ; 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]; 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; iter_54(z); iter_54(z-8); z -= 16; } } static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) { int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l; int n3_4 = n - n4, 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; // twiddle factors float *A = f->A[blocktype]; // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio" // See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function. // kernel from paper // merged: // copy and reflect spectral data // step 0 // note that it turns out that the items added together during // this step are, in fact, being added to themselves (as reflected // by step 0). inexplicable inefficiency! this became obvious // once I combined the passes. // so there's a missing 'times 2' here (for adding X to itself). // this propogates through linearly to the end, where the numbers // are 1/2 too small, and need to be compensated for. { 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]); 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]); d -= 2; AA += 2; e -= 4; } } // now we use symbolic names for these, so that we can // possibly swap their meaning as we change which operations // are in place u = buffer; v = buf2; // step 2 (paper output is w, now u) // 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; e0 = &v[n4]; e1 = &v[0]; d0 = &u[n4]; d1 = &u[0]; 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]; 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]; AA -= 8; d0 += 4; d1 += 4; e0 += 4; e1 += 4; } } // step 3 ld = ilog(n) - 1; // ilog is off-by-one from normal definitions // optimized step 3: // the original step3 loop can be nested r inside s or s inside r; // it's written originally as s inside r, but this is dumb when r // iterates many times, and s few. So I have two copies of it and // 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); // 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); 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 (; 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) { imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0); A0 += k1*4; i_off -= 8; } } // iterations with count: // ld-6,-5,-4 all interleaved together // 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); // output is u // step 4, 5, and 6 // cannot be in-place because of step 5 { uint16 *bitrev = f->bit_reverse[blocktype]; // weirdly, I'd have thought reading sequentially and writing // erratically would have been better than vice-versa, but in // 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) { 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]; k4 = bitrev[1]; d1[1] = u[k4+0]; d1[0] = u[k4+1]; d0[1] = u[k4+2]; d0[0] = u[k4+3]; d0 -= 4; d1 -= 4; bitrev += 2; } } // (paper output is u, now v) // data must be in buf2 assert(v == buf2); // step 7 (paper output is v, now v) // this is now in place { float *C = f->C[blocktype]; float *d, *e; d = v; e = v + n2 - 4; 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; b2 = d[0] + e[ 2]; b3 = d[1] - e[ 3]; d[0] = b2 + b0; d[1] = b3 + b1; e[2] = b2 - b0; e[3] = b1 - b3; a02 = d[2] - e[0]; a11 = d[3] + e[1]; b0 = C[3]*a02 + C[2]*a11; b1 = C[3]*a11 - C[2]*a02; b2 = d[2] + e[ 0]; b3 = d[3] - e[ 1]; d[2] = b2 + b0; d[3] = b3 + b1; e[0] = b2 - b0; e[1] = b1 - b3; C += 4; d += 4; e -= 4; } } // 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 // to make another pass later // this cannot POSSIBLY be in place, so we refer to the buffers directly { float *d0,*d1,*d2,*d3; float *B = f->B[blocktype] + n2 - 8; float *e = buf2 + n2 - 8; d0 = &buffer[0]; d1 = &buffer[n2-4]; d2 = &buffer[n2]; 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]; 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]; 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]; 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]; d0[3] = p1; d1[0] = - p1; d2[3] = p0; d3[0] = p0; B -= 8; e -= 8; d0 += 4; d2 += 4; d1 -= 4; d3 -= 4; } } temp_alloc_restore(f,save_point); } #if 0 // this is the original version of the above code, if you want to optimize it from scratch void inverse_mdct_naive(float *buffer, int n) { float s; float A[1 << 12], B[1 << 12], C[1 << 11]; int i,k,k2,k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l; int n3_4 = n - n4, ld; // how can they claim this only uses N words?! // oh, because they're only used sparsely, whoops float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13]; // set up twiddle factors 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); B[k2+1] = (float) sin((k2+1)*M_PI/n/2); } 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); } // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio" // Note there are bugs in that pseudocode, presumably due to them attempting // to rename the arrays nicely rather than representing the way their actual // implementation bounces buffers back and forth. As a result, even in the // "some formulars corrected" version, a direct implementation fails. These // are noted below as "paper bug". // copy and reflect spectral data for (k=0; k < n2; ++k) u[k] = buffer[k]; for ( ; k < n ; ++k) u[k] = -buffer[n - k - 1]; // kernel from paper // step 1 for (k=k2=k4=0; k < n4; k+=1, k2+=2, k4+=4) { v[n-k4-1] = (u[k4] - u[n-k4-1]) * A[k2] - (u[k4+2] - u[n-k4-3])*A[k2+1]; v[n-k4-3] = (u[k4] - u[n-k4-1]) * A[k2+1] + (u[k4+2] - u[n-k4-3])*A[k2]; } // step 2 for (k=k4=0; k < n8; k+=1, k4+=4) { w[n2+3+k4] = v[n2+3+k4] + v[k4+3]; w[n2+1+k4] = v[n2+1+k4] + v[k4+1]; w[k4+3] = (v[n2+3+k4] - v[k4+3])*A[n2-4-k4] - (v[n2+1+k4]-v[k4+1])*A[n2-3-k4]; w[k4+1] = (v[n2+1+k4] - v[k4+1])*A[n2-4-k4] + (v[n2+3+k4]-v[k4+3])*A[n2-3-k4]; } // step 3 ld = ilog(n) - 1; // ilog is off-by-one from normal definitions for (l=0; l < ld-3; ++l) { int k0 = n >> (l+2), k1 = 1 << (l+3); int rlim = n >> (l+4), r4, r; int s2lim = 1 << (l+2), s2; for (r=r4=0; r < rlim; r4+=4,++r) { for (s2=0; s2 < s2lim; s2+=2) { u[n-1-k0*s2-r4] = w[n-1-k0*s2-r4] + w[n-1-k0*(s2+1)-r4]; u[n-3-k0*s2-r4] = w[n-3-k0*s2-r4] + w[n-3-k0*(s2+1)-r4]; u[n-1-k0*(s2+1)-r4] = (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1] - (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1+1]; u[n-3-k0*(s2+1)-r4] = (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1] + (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1+1]; } } if (l+1 < ld-3) { // paper bug: ping-ponging of u&w here is omitted memcpy(w, u, sizeof(u)); } } // step 4 for (i=0; i < n8; ++i) { int j = bit_reverse(i) >> (32-ld+3); assert(j < n8); if (i == j) { // paper bug: original code probably swapped in place; if copying, // need to directly copy in this case int i8 = i << 3; v[i8+1] = u[i8+1]; v[i8+3] = u[i8+3]; v[i8+5] = u[i8+5]; v[i8+7] = u[i8+7]; } else if (i < j) { int i8 = i << 3, j8 = j << 3; v[j8+1] = u[i8+1], v[i8+1] = u[j8 + 1]; v[j8+3] = u[i8+3], v[i8+3] = u[j8 + 3]; v[j8+5] = u[i8+5], v[i8+5] = u[j8 + 5]; v[j8+7] = u[i8+7], v[i8+7] = u[j8 + 7]; } } // step 5 for (k=0; k < n2; ++k) { w[k] = v[k*2+1]; } // step 6 for (k=k2=k4=0; k < n8; ++k, k2 += 2, k4 += 4) { u[n-1-k2] = w[k4]; u[n-2-k2] = w[k4+1]; u[n3_4 - 1 - k2] = w[k4+2]; u[n3_4 - 2 - k2] = w[k4+3]; } // step 7 for (k=k2=0; k < n8; ++k, k2 += 2) { v[n2 + k2 ] = ( u[n2 + k2] + u[n-2-k2] + C[k2+1]*(u[n2+k2]-u[n-2-k2]) + C[k2]*(u[n2+k2+1]+u[n-2-k2+1]))/2; v[n-2 - k2] = ( u[n2 + k2] + u[n-2-k2] - C[k2+1]*(u[n2+k2]-u[n-2-k2]) - C[k2]*(u[n2+k2+1]+u[n-2-k2+1]))/2; v[n2+1+ k2] = ( u[n2+1+k2] - u[n-1-k2] + C[k2+1]*(u[n2+1+k2]+u[n-1-k2]) - C[k2]*(u[n2+k2]-u[n-2-k2]))/2; v[n-1 - k2] = (-u[n2+1+k2] + u[n-1-k2] + C[k2+1]*(u[n2+1+k2]+u[n-1-k2]) - C[k2]*(u[n2+k2]-u[n-2-k2]))/2; } // step 8 for (k=k2=0; k < n4; ++k,k2 += 2) { X[k] = v[k2+n2]*B[k2 ] + v[k2+1+n2]*B[k2+1]; X[n2-1-k] = v[k2+n2]*B[k2+1] - v[k2+1+n2]*B[k2 ]; } // decode kernel to output // determined the following value experimentally // (by first figuring out what made inverse_mdct_slow work); then matching that here // (probably vorbis encoder premultiplies by n or n/2, to save it on the decoder?) s = 0.5; // theoretically would be n4 // [[[ note! the s value of 0.5 is compensated for by the B[] in the current code, // so it needs to use the "old" B values to behave correctly, or else // set s to 1.0 ]]] for (i=0; i < n4 ; ++i) buffer[i] = s * X[i+n4]; for ( ; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1]; for ( ; i < n ; ++i) buffer[i] = -s * X[i - n3_4]; } #endif 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]; assert(0); return NULL; } #ifndef STB_VORBIS_NO_DEFER_FLOOR typedef int16 YTYPE; #else typedef int YTYPE; #endif static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *finalY, uint8 *step2_flag) { int n2 = n >> 1; int s = map->chan[i].mux, floor; floor = map->submap_floor[s]; if (f->floor_types[floor] == 0) { return error(f, VORBIS_invalid_stream); } else { Floor1 *g = &f->floor_config[floor].floor1; int j,q; int lx = 0, ly = finalY[0] * g->floor1_multiplier; for (q=1; q < g->values; ++q) { j = g->sorted_order[q]; #ifndef STB_VORBIS_NO_DEFER_FLOOR if (finalY[j] >= 0) #else if (step2_flag[j]) #endif { int hy = finalY[j] * g->floor1_multiplier; int hx = g->Xlist[j]; draw_line(target, lx,ly, hx,hy, n2); lx = hx, ly = hy; } } if (lx < n2) // optimization of: draw_line(target, lx,ly, n,ly, n2); for (j=lx; j < n2; ++j) LINE_OP(target[j], inverse_db_table[ly]); } return TRUE; } static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) { Mode *m; int i, n, prev, next, window_center; f->channel_buffer_start = f->channel_buffer_end = 0; retry: if (f->eof) return FALSE; if (!maybe_start_packet(f)) return FALSE; // check packet type if (get_bits(f,1) != 0) { if (IS_PUSH_MODE(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; *mode = i; m = f->mode_config + i; if (m->blockflag) { n = f->blocksize_1; prev = get_bits(f,1); next = get_bits(f,1); } else { prev = next = 0; n = f->blocksize_0; } // WINDOWING window_center = n >> 1; 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; } 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; } return TRUE; } 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 zero_channel[256]; int really_zero_channel[256]; int window_center; // WINDOWING n = f->blocksize[m->blockflag]; window_center = n >> 1; map = &f->mapping[m->mapping]; // FLOORS n2 = n >> 1; stb_prof(1); 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) { return error(f, VORBIS_invalid_stream); } else { Floor1 *g = &f->floor_config[floor].floor1; 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]; 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) { 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 cval = 0; if (cbits) { Codebook *c = f->codebooks + g->class_masterbooks[pclass]; DECODE(cval,f,c); } for (k=0; k < cdim; ++k) { int book = g->subclass_books[pclass][cval & csub]; cval = cval >> cbits; if (book >= 0) { int temp; Codebook *c = f->codebooks + book; DECODE(temp,f,c); finalY[offset++] = temp; } else finalY[offset++] = 0; } } 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) { int low, high, pred, highroom, lowroom, room, val; low = g->neighbors[j][0]; high = g->neighbors[j][1]; //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; lowroom = pred; if (highroom < lowroom) room = highroom * 2; else room = lowroom * 2; if (val) { step2_flag[low] = step2_flag[high] = 1; step2_flag[j] = 1; if (val >= room) if (highroom > lowroom) finalY[j] = val - lowroom + pred; else finalY[j] = pred - val + highroom - 1; else if (val & 1) finalY[j] = pred - ((val+1)>>1); else finalY[j] = pred + (val>>1); } else { step2_flag[j] = 0; finalY[j] = pred; } } #ifdef STB_VORBIS_NO_DEFER_FLOOR 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) { if (!step2_flag[j]) finalY[j] = -1; } #endif } else { error: zero_channel[i] = TRUE; } // So we just defer everything else to later // at this point we've decoded the floor into buffer } } stb_prof(0); // at this point we've decoded all floors if (f->alloc.alloc_buffer) assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); // 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]) { zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE; } // RESIDUE DECODE for (i=0; i < map->submaps; ++i) { float *residue_buffers[STB_VORBIS_MAX_CHANNELS]; int r,t; 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]) { do_not_decode[ch] = TRUE; residue_buffers[ch] = NULL; } else { do_not_decode[ch] = FALSE; residue_buffers[ch] = f->channel_buffers[j]; } ++ch; } } r = map->submap_residue[i]; t = f->residue_types[r]; decode_residue(f, residue_buffers, ch, n2, r, do_not_decode); } if (f->alloc.alloc_buffer) assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset); // INVERSE COUPLING stb_prof(14); 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; 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 a2 = m[j], m2 = m[j] - a[j]; m[j] = m2; a[j] = a2; } } // finish decoding the floors #ifndef STB_VORBIS_NO_DEFER_FLOOR stb_prof(15); 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 { 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]) { memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2); } else { for (j=0; j < n2; ++j) f->channel_buffers[i][j] *= f->floor_buffers[i][j]; } } #endif // INVERSE MDCT stb_prof(16); for (i=0; i < f->channels; ++i) inverse_mdct(f->channel_buffers[i], n, f, m->blockflag); stb_prof(0); // this shouldn't be necessary, unless we exited on an error // and want to flush to get to the next packet flush_packet(f); 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, // but presumably it's not a commonly used feature f->current_loc = -n2; // start of first frame is positioned for discard // we might have to discard samples "from" the next frame too, // if we're lapping a large block then a small at the start? f->discard_samples_deferred = n - right_end; f->current_loc_valid = TRUE; f->first_decode = FALSE; } else if (f->discard_samples_deferred) { 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) { // 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 // the discarded samples here // but wait, the code below is going to add this in itself even // on a discard, so we don't need to do it here... } // check if we have ogg information about the sample # for this packet 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)) { uint32 current_end = f->known_loc_for_packet - (n-right_end); // then let's infer the size of the (probably) short final frame if (current_end < f->current_loc + right_end) { if (current_end < f->current_loc) { // negative truncation, that's impossible! *len = 0; } else { *len = current_end - f->current_loc; } *len += left_start; f->current_loc += *len; return TRUE; } } // otherwise, just set our sample loc // 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_valid = TRUE; } if (f->current_loc_valid) f->current_loc += (right_start - 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 return TRUE; } 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; 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; // 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 // starts, therefore where the previous window's right edge starts, and // therefore where to start mixing from the previous buffer. 'right' // indicates where our sin() ending-window starts, therefore that's where // we start saving, and where our returned-data ends. // mixin from previous window if (f->previous_length) { int i,j, n = f->previous_length; float *w = get_window(f, n); 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]; } } prev = f->previous_length; // last half of this data becomes previous window f->previous_length = len - right; // @OPTIMIZE: could avoid this copy by double-buffering the // output (flipping previous_window with channel_buffers), but // then previous_window would have to be 2x as large, and // 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]; if (!prev) // there was no previous packet, so this data isn't valid... // this isn't entirely true, only the would-have-overlapped data // isn't valid, but this seems to be what the spec requires return 0; // truncate a short frame if (len < right) right = len; f->samples_output += right-left; return right - left; } static void vorbis_pump_first_frame(stb_vorbis *f) { int len, right, left; if (vorbis_decode_packet(f, &len, &left, &right)) vorbis_finish_frame(f, len, left, right); } #ifndef STB_VORBIS_NO_PUSHDATA_API static int is_whole_packet_present(stb_vorbis *f, int end_page) { // make sure that we have the packet available before continuing... // this requires a full ogg parse, but we know we can fetch from f->stream // instead of coding this out explicitly, we could save the current read state, // read the next packet with get8() until end-of-packet, check f->eof, then // reset the state? but that would be slower, esp. since we'd have over 256 bytes // of state to restore (primarily the page segment table) 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) { p += f->segments[s]; if (f->segments[s] < 255) // stop at first short segment break; } // either this continues, or it ends it... if (end_page) if (s < f->segment_count-1) return error(f, VORBIS_invalid_stream); if (s == f->segment_count) s = -1; // set 'crosses page' flag if (p > f->stream_end) return error(f, VORBIS_need_more_data); first = FALSE; } 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); // 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 (f->previous_length) 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 // 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) { p += q[s]; if (q[s] < 255) break; } if (end_page) if (s < n-1) return error(f, VORBIS_invalid_stream); if (s == f->segment_count) s = -1; // set 'crosses page' flag if (p > f->stream_end) return error(f, VORBIS_need_more_data); first = FALSE; } return TRUE; } #endif // !STB_VORBIS_NO_PUSHDATA_API static int start_decoder(vorb *f) { uint8 header[6], x,y; int len,i,j,k, max_submaps = 0; int longest_floorlist=0; // first page, first packet 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); // check for expected packet length if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page); if (f->segments[0] != 30) 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); // 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); get32(f); // bitrate_maximum get32(f); // bitrate_nominal get32(f); // bitrate_minimum x = get8(f); { 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); } // framing_flag x = get8(f); if (!(x & 1)) return error(f, VORBIS_invalid_first_page); // second packet! if (!start_page(f)) return FALSE; if (!start_packet(f)) return FALSE; do { len = next_segment(f); skip(f, len); f->bytes_in_seg = 0; } while (len); // third packet! if (!start_packet(f)) return FALSE; #ifndef STB_VORBIS_NO_PUSHDATA_API if (IS_PUSH_MODE(f)) { if (!is_whole_packet_present(f, TRUE)) { // convert error in ogg header to write type if (f->error == VORBIS_invalid_stream) f->error = VORBIS_invalid_setup; return FALSE; } } #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); // 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); memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count); for (i=0; i < f->codebook_count; ++i) { uint32 *values; int ordered, sorted_count; int total=0; uint8 *lengths; Codebook *c = f->codebooks+i; 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; 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); if (c->sparse) lengths = (uint8 *) setup_temp_malloc(f, c->entries); else lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries); if (!lengths) return error(f, VORBIS_outofmem); if (ordered) { int current_entry = 0; 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_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) { lengths[j] = get_bits(f, 5) + 1; ++total; } else { lengths[j] = NO_CODE; } } } if (c->sparse && total >= c->entries >> 2) { // convert sparse items to non-sparse! 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); 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; c->sparse = 0; } // compute the size of the sorted tables if (c->sparse) { sorted_count = total; //assert(total != 0); } else { sorted_count = 0; #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 } c->sorted_entries = sorted_count; values = NULL; 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); } 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); return error(f, VORBIS_invalid_setup); } 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)); // 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) { ++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); setup_temp_free(f, lengths, c->entries); c->codewords = NULL; } compute_accelerated_huffman(c); c->lookup_type = get_bits(f, 4); 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->lookup_values = lookup1_values(c->entries, c->dimensions); } else { c->lookup_values = c->entries * c->dimensions; } 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); } mults[j] = q; } #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK if (c->lookup_type == 1) { int len, sparse = c->sparse; // 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); } len = sparse ? c->sorted_entries : c->entries; for (j=0; j < len; ++j) { int z = sparse ? c->sorted_values[j] : j, div=1; for (k=0; k < c->dimensions; ++k) { int off = (z / div) % c->lookup_values; c->multiplicands[j*c->dimensions + k] = #ifndef STB_VORBIS_CODEBOOK_FLOATS mults[off]; #else mults[off]*c->delta_value + c->minimum_value; // in this case (and this case only) we could pre-expand c->sequence_p, // and throw away the decode logic for it; have to ALSO do // it in the case below, but it can only be done if // STB_VORBIS_CODEBOOK_FLOATS // !STB_VORBIS_DIVIDES_IN_CODEBOOK #endif div *= c->lookup_values; } } setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); c->lookup_type = 2; } else #endif { c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values); #ifndef STB_VORBIS_CODEBOOK_FLOATS memcpy(c->multiplicands, mults, sizeof(c->multiplicands[0]) * c->lookup_values); #else for (j=0; j < (int) c->lookup_values; ++j) c->multiplicands[j] = mults[j] * c->delta_value + c->minimum_value; setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); #endif } skip:; #ifdef STB_VORBIS_CODEBOOK_FLOATS if (c->lookup_type == 2 && c->sequence_p) { for (j=1; j < (int) c->lookup_values; ++j) c->multiplicands[j] = c->multiplicands[j-1]; c->sequence_p = 0; } #endif } } // time domain transfers (notused) x = get_bits(f, 6) + 1; for (i=0; i < x; ++i) { uint32 z = get_bits(f, 16); 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)); 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) { 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); return error(f, VORBIS_feature_not_supported); } else { Point 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) { 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; g->class_subclasses[j] = get_bits(f, 2); 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); } 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->Xlist[0] = 0; g->Xlist[1] = 1 << g->rangebits; g->values = 2; for (j=0; j < g->partitions; ++j) { int c = g->partition_class_list[j]; 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) { p[j].x = g->Xlist[j]; p[j].y = j; } qsort(p, g->values, sizeof(p[0]), point_compare); for (j=0; j < g->values; ++j) g->sorted_order[j] = (uint8) p[j].y; // precompute the neighbors for (j=2; j < g->values; ++j) { int low,hi; neighbors(g->Xlist, j, &low,&hi); g->neighbors[j][0] = low; g->neighbors[j][1] = hi; } if (g->values > longest_floorlist) longest_floorlist = g->values; } } // Residue f->residue_count = get_bits(f, 6)+1; f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(*f->residue_config)); for (i=0; i < f->residue_count; ++i) { uint8 residue_cascade[64]; 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); r->begin = get_bits(f, 24); r->end = get_bits(f, 24); r->part_size = get_bits(f,24)+1; r->classifications = get_bits(f,6)+1; r->classbook = get_bits(f,8); 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); 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 { 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); memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries); 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); 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)); 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 (get_bits(f,1)) m->submaps = get_bits(f,4); 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; 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 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) { m->chan[j].mux = get_bits(f, 4); if (m->chan[j].mux >= m->submaps) return error(f, VORBIS_invalid_setup); } } else // @SPECIFICATION: this case is missing from the spec 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); } } // 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); } 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); #ifdef STB_VORBIS_NO_DEFER_FLOOR f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2); #endif } 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) integer_divide_table[i][j] = i / j; #endif // compute how much temporary memory is needed // 1. { 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) { Residue *r = f->residue_config + i; int n_read = r->end - 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 f->temp_memory_required = classify_mem; if (imdct_mem > f->temp_memory_required) f->temp_memory_required = imdct_mem; } f->first_decode = TRUE; 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) return error(f, VORBIS_outofmem); } f->first_audio_page_offset = stb_vorbis_get_file_offset(f); return TRUE; } static void vorbis_deinit(stb_vorbis *p) { int i,j; 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); } setup_free(p, r->residue_books); } if (p->codebooks) { 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, p->codebooks); } setup_free(p, p->floor_config); setup_free(p, p->residue_config); for (i=0; i < p->mapping_count; ++i) setup_free(p, p->mapping[i].chan); setup_free(p, p->mapping); for (i=0; i < p->channels; ++i) { setup_free(p, p->channel_buffers[i]); setup_free(p, p->previous_window[i]); #ifdef STB_VORBIS_NO_DEFER_FLOOR setup_free(p, p->floor_buffers[i]); #endif setup_free(p, p->finalY[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]); } #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; vorbis_deinit(p); setup_free(p,p); } static void vorbis_init(stb_vorbis *p, stb_vorbis_alloc *z) { memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start if (z) { p->alloc = *z; p->alloc.alloc_buffer_length_in_bytes = (p->alloc.alloc_buffer_length_in_bytes+3) & ~3; p->temp_offset = p->alloc.alloc_buffer_length_in_bytes; } p->eof = 0; p->error = VORBIS__no_error; p->stream = NULL; p->codebooks = NULL; p->page_crc_tests = -1; #ifndef STB_VORBIS_NO_STDIO p->close_on_free = FALSE; p->f = NULL; #endif } int stb_vorbis_get_sample_offset(stb_vorbis *f) { if (f->current_loc_valid) return f->current_loc; else return -1; } stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f) { stb_vorbis_info d; d.channels = f->channels; d.sample_rate = f->sample_rate; d.setup_memory_required = f->setup_memory_required; d.setup_temp_memory_required = f->setup_temp_memory_required; d.temp_memory_required = f->temp_memory_required; d.max_frame_size = f->blocksize_1 >> 1; return d; } int stb_vorbis_get_error(stb_vorbis *f) { int e = f->error; f->error = VORBIS__no_error; return e; } static stb_vorbis * vorbis_alloc(stb_vorbis *f) { stb_vorbis *p = (stb_vorbis *) setup_malloc(f, sizeof(*p)); return p; } #ifndef STB_VORBIS_NO_PUSHDATA_API void stb_vorbis_flush_pushdata(stb_vorbis *f) { f->previous_length = 0; f->page_crc_tests = 0; f->discard_samples_deferred = 0; f->current_loc_valid = FALSE; f->first_decode = FALSE; f->samples_output = 0; f->channel_buffer_start = 0; f->channel_buffer_end = 0; } 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) 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; 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; uint32 crc; // make sure we have the whole page header 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]; // 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]); // now process 4 0-bytes 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].crc_so_far = crc; 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) 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; if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT) break; // keep going if we still have room for more } } } } 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; // 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]); f->scan[i].bytes_left -= m; f->scan[i].crc_so_far = crc; if (f->scan[i].bytes_left == 0) { // does it match? 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 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 f->current_loc_valid = f->current_loc != ~0; return data_len; } // delete entry f->scan[i] = f->scan[--f->page_crc_tests]; } else { ++i; } } return data_len; } // return value: number of bytes we used int stb_vorbis_decode_frame_pushdata( stb_vorbis *f, // the file we're decoding 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; if (!IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); if (f->page_crc_tests >= 0) { *samples = 0; return vorbis_search_for_page_pushdata(f, data, data_len); } f->stream = data; f->stream_end = data + data_len; f->error = VORBIS__no_error; // check that we have the entire packet in memory if (!is_whole_packet_present(f, FALSE)) { *samples = 0; return 0; } 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) { // flush and resynch f->error = VORBIS__no_error; while (get8_packet(f) != EOP) if (f->eof) break; *samples = 0; return f->stream - data; } 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; *samples = 0; return f->stream - data; } } // if we get an error while parsing, what to do? // well, it DEFINITELY won't work to continue from where we are! stb_vorbis_flush_pushdata(f); // restore the error that actually made us bail f->error = error; *samples = 0; return 1; } // success! len = vorbis_finish_frame(f, len, left, right); for (i=0; i < f->channels; ++i) f->outputs[i] = f->channel_buffers[i] + left; if (channels) *channels = f->channels; *samples = len; *output = f->outputs; return f->stream - data; } stb_vorbis *stb_vorbis_open_pushdata( unsigned char *data, int data_len, // the memory available for decoding int *data_used, // only defined if result is not NULL int *error, stb_vorbis_alloc *alloc) { stb_vorbis *f, p; vorbis_init(&p, alloc); p.stream = data; p.stream_end = data + data_len; p.push_mode = TRUE; if (!start_decoder(&p)) { if (p.eof) *error = VORBIS_need_more_data; else *error = p.error; return NULL; } f = vorbis_alloc(&p); if (f) { *f = p; *data_used = f->stream - data; *error = 0; return f; } else { vorbis_deinit(&p); return NULL; } } #endif // STB_VORBIS_NO_PUSHDATA_API 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 f->stream - f->stream_start; #ifndef STB_VORBIS_NO_STDIO return ftell(f->f) - f->f_start; #endif } #ifndef STB_VORBIS_NO_PULLDATA_API // // DATA-PULLING API // static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) { for(;;) { int n; if (f->eof) return 0; n = get8(f); if (n == 0x4f) { // page header 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) if (get8(f) != ogg_page_header[i]) break; if (f->eof) return 0; if (i == 4) { uint8 header[27]; uint32 i, crc, goal, len; 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) header[i] = 0; crc = 0; for (i=0; i < 27; ++i) crc = crc32_update(crc, header[i]); len = 0; 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) crc = crc32_update(crc, get8(f)); // finished parsing probable page 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 // a file with garbage between each ogg page and recover // from it automatically? So even though that paranoia // might decrease the chance of an invalid decode by // another 2^32, not worth it since it would hose those // invalid-but-useful files? if (end) *end = stb_vorbis_get_file_offset(f); if (last) if (header[5] & 0x04) *last = 1; else *last = 0; set_file_offset(f, retry_loc-1); return 1; } } invalid: // not a valid page, so rewind and look for next one set_file_offset(f, retry_loc); } } } // seek is implemented with 'interpolation search'--this is like // binary search, but we use the data values to estimate the likely // location of the data item (plus a bit of a bias so when the // estimation is wrong we don't waste overly much time) #define SAMPLE_unknown 0xffffffff // ogg vorbis, in its insane infinite wisdom, only provides // information about the sample at the END of the page. // therefore we COULD have the data we need in the current // page, and not know it. we could just use the end location // as our only knowledge for bounds, seek back, and eventually // the binary search finds it. or we can try to be smart and // not waste time trying to locate more pages. we try to be // smart, since this data is already in memory anyway, so // doing needless I/O would be crazy! static int vorbis_analyze_page(stb_vorbis *f, ProbedPage *z) { uint8 header[27], lacing[255]; uint8 packet_type[255]; int num_packet, packet_start, previous =0; int i,len; uint32 samples; // record where the page starts z->page_start = stb_vorbis_get_file_offset(f); // parse the header getn(f, header, 27); assert(header[0] == 'O' && header[1] == 'g' && header[2] == 'g' && header[3] == 'S'); getn(f, lacing, header[26]); // determine the length of the payload len = 0; for (i=0; i < header[26]; ++i) len += lacing[i]; // this implies where the page ends z->page_end = z->page_start + 27 + header[26] + len; // read the last-decoded sample out of the data z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 16); if (header[5] & 4) { // if this is the last page, it's not possible to work // backwards to figure out the first sample! whoops! fuck. z->first_decoded_sample = SAMPLE_unknown; set_file_offset(f, z->page_start); return 1; } // scan through the frames to determine the sample-count of each one... // our goal is the sample # of the first fully-decoded sample on the // page, which is the first decoded sample of the 2nd page num_packet=0; packet_start = ((header[5] & 1) == 0); for (i=0; i < header[26]; ++i) { if (packet_start) { uint8 n,b,m; if (lacing[i] == 0) goto bail; // trying to read from zero-length packet n = get8(f); // if bottom bit is non-zero, we've got corruption if (n & 1) goto bail; n >>= 1; b = ilog(f->mode_count-1); m = n >> b; n &= (1 << b)-1; if (n >= f->mode_count) goto bail; if (num_packet == 0 && f->mode_config[n].blockflag) previous = (m & 1); packet_type[num_packet++] = f->mode_config[n].blockflag; skip(f, lacing[i]-1); } else skip(f, lacing[i]); packet_start = (lacing[i] < 255); } // now that we know the sizes of all the pages, we can start determining // how much sample data there is. samples = 0; // for the last packet, we step by its whole length, because the definition // is that we encoded the end sample loc of the 'last packet completed', // where 'completed' refers to packets being split, and we are left to guess // what 'end sample loc' means. we assume it means ignoring the fact that // the last half of the data is useless without windowing against the next // packet... (so it's not REALLY complete in that sense) if (num_packet > 1) samples += f->blocksize[packet_type[num_packet-1]]; for (i=num_packet-2; i >= 1; --i) { // now, for this packet, how many samples do we have that // do not overlap the following packet? if (packet_type[i] == 1) if (packet_type[i+1] == 1) samples += f->blocksize_1 >> 1; else samples += ((f->blocksize_1 - f->blocksize_0) >> 2) + (f->blocksize_0 >> 1); else samples += f->blocksize_0 >> 1; } // now, at this point, we've rewound to the very beginning of the // _second_ packet. if we entirely discard the first packet after // a seek, this will be exactly the right sample number. HOWEVER! // we can't as easily compute this number for the LAST page. The // only way to get the sample offset of the LAST page is to use // the end loc from the previous page. But what that returns us // is _exactly_ the place where we get our first non-overlapped // sample. (I think. Stupid spec for being ambiguous.) So for // consistency it's better to do that here, too. However, that // will then require us to NOT discard all of the first frame we // decode, in some cases, which means an even weirder frame size // and extra code. what a fucking pain. // we're going to discard the first packet if we // start the seek here, so we don't care about it. (we could actually // do better; if the first packet is long, and the previous packet // is short, there's actually data in the first half of the first // packet that doesn't need discarding... but not worth paying the // effort of tracking that of that here and in the seeking logic) // except crap, if we infer it from the _previous_ packet's end // location, we DO need to use that definition... and we HAVE to // infer the start loc of the LAST packet from the previous packet's // end location. fuck you, ogg vorbis. z->first_decoded_sample = z->last_decoded_sample - samples; // restore file state to where we were set_file_offset(f, z->page_start); return 1; // restore file state to where we were bail: set_file_offset(f, z->page_start); return 0; } static int vorbis_seek_frame_from_page(stb_vorbis *f, uint32 page_start, uint32 first_sample, uint32 target_sample, int fine) { int left_start, left_end, right_start, right_end, mode,i; int frame=0; uint32 frame_start; int frames_to_skip, data_to_skip; // first_sample is the sample # of the first sample that doesn't // overlap the previous page... note that this requires us to // _partially_ discard the first packet! bleh. set_file_offset(f, page_start); f->next_seg = -1; // force page resync frame_start = first_sample; // frame start is where the previous packet's last decoded sample // was, which corresponds to left_end... EXCEPT if the previous // packet was long and this packet is short? Probably a bug here. // now, we can start decoding frames... we'll only FAKE decode them, // until we find the frame that contains our sample; then we'll rewind, // and try again for (;;) { int start; if (!vorbis_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode)) return error(f, VORBIS_seek_failed); if (frame == 0) start = left_end; else start = left_start; // the window starts at left_start; the last valid sample we generate // before the next frame's window start is right_start-1 if (target_sample < frame_start + right_start-start) break; flush_packet(f); if (f->eof) return error(f, VORBIS_seek_failed); frame_start += right_start - start; ++frame; } // ok, at this point, the sample we want is contained in frame #'frame' // to decode frame #'frame' normally, we have to decode the // previous frame first... but if it's the FIRST frame of the page // we can't. if it's the first frame, it means it falls in the part // of the first frame that doesn't overlap either of the other frames. // so, if we have to handle that case for the first frame, we might // as well handle it for all of them, so: if (target_sample > frame_start + (left_end - left_start)) { // so what we want to do is go ahead and just immediately decode // this frame, but then make it so the next get_frame_float() uses // this already-decoded data? or do we want to go ahead and rewind, // and leave a flag saying to skip the first N data? let's do that frames_to_skip = frame; // if this is frame #1, skip 1 frame (#0) data_to_skip = left_end - left_start; } else { // otherwise, we want to skip frames 0, 1, 2, ... frame-2 // (which means frame-2+1 total frames) then decode frame-1, // then leave frame pending frames_to_skip = frame - 1; assert(frames_to_skip >= 0); data_to_skip = -1; } set_file_offset(f, page_start); f->next_seg = - 1; // force page resync for (i=0; i < frames_to_skip; ++i) { maybe_start_packet(f); flush_packet(f); } if (data_to_skip >= 0) { int i,j,n = f->blocksize_0 >> 1; f->discard_samples_deferred = data_to_skip; for (i=0; i < f->channels; ++i) for (j=0; j < n; ++j) f->previous_window[i][j] = 0; f->previous_length = n; frame_start += data_to_skip; } else { f->previous_length = 0; vorbis_pump_first_frame(f); } // at this point, the NEXT decoded frame will generate the desired sample if (fine) { // so if we're doing sample accurate streaming, we want to go ahead and decode it! if (target_sample != frame_start) { int n; stb_vorbis_get_frame_float(f, &n, NULL); assert(target_sample > frame_start); assert(f->channel_buffer_start + (int) (target_sample-frame_start) < f->channel_buffer_end); f->channel_buffer_start += (target_sample - frame_start); } } return 0; } static int vorbis_seek_base(stb_vorbis *f, unsigned int sample_number, int fine) { ProbedPage p[2],q; if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing); // do we know the location of the last page? if (f->p_last.page_start == 0) { uint32 z = stb_vorbis_stream_length_in_samples(f); if (z == 0) return error(f, VORBIS_cant_find_last_page); } p[0] = f->p_first; p[1] = f->p_last; if (sample_number >= f->p_last.last_decoded_sample) sample_number = f->p_last.last_decoded_sample-1; if (sample_number < f->p_first.last_decoded_sample) { vorbis_seek_frame_from_page(f, p[0].page_start, 0, sample_number, fine); return 0; } else { int attempts=0; while (p[0].page_end < p[1].page_start) { uint32 probe; uint32 start_offset, end_offset; uint32 start_sample, end_sample; // copy these into local variables so we can tweak them // if any are unknown start_offset = p[0].page_end; end_offset = p[1].after_previous_page_start; // an address known to seek to page p[1] start_sample = p[0].last_decoded_sample; end_sample = p[1].last_decoded_sample; // currently there is no such tweaking logic needed/possible? if (start_sample == SAMPLE_unknown || end_sample == SAMPLE_unknown) return error(f, VORBIS_seek_failed); // now we want to lerp between these for the target samples... // step 1: we need to bias towards the page start... if (start_offset + 4000 < end_offset) end_offset -= 4000; // now compute an interpolated search loc probe = start_offset + (int) floor((float) (end_offset - start_offset) / (end_sample - start_sample) * (sample_number - start_sample)); // next we need to bias towards binary search... // code is a little wonky to allow for full 32-bit unsigned values if (attempts >= 4) { uint32 probe2 = start_offset + ((end_offset - start_offset) >> 1); if (attempts >= 8) probe = probe2; else if (probe < probe2) probe = probe + ((probe2 - probe) >> 1); else probe = probe2 + ((probe - probe2) >> 1); } ++attempts; set_file_offset(f, probe); if (!vorbis_find_page(f, NULL, NULL)) return error(f, VORBIS_seek_failed); if (!vorbis_analyze_page(f, &q)) return error(f, VORBIS_seek_failed); q.after_previous_page_start = probe; // it's possible we've just found the last page again if (q.page_start == p[1].page_start) { p[1] = q; continue; } if (sample_number < q.last_decoded_sample) p[1] = q; else p[0] = q; } if (p[0].last_decoded_sample <= sample_number && sample_number < p[1].last_decoded_sample) { vorbis_seek_frame_from_page(f, p[1].page_start, p[0].last_decoded_sample, sample_number, fine); return 0; } return error(f, VORBIS_seek_failed); } } int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) { return vorbis_seek_base(f, sample_number, FALSE); } int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) { return vorbis_seek_base(f, sample_number, TRUE); } void stb_vorbis_seek_start(stb_vorbis *f) { if (IS_PUSH_MODE(f)) { error(f, VORBIS_invalid_api_mixing); return; } set_file_offset(f, f->first_audio_page_offset); f->previous_length = 0; f->first_decode = TRUE; f->next_seg = -1; vorbis_pump_first_frame(f); } 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) { int last; uint32 lo,hi; char header[6]; // first, store the current decode position so we can restore it restore_offset = stb_vorbis_get_file_offset(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) previous_safe = f->stream_len - 65536; else previous_safe = f->first_audio_page_offset; set_file_offset(f, previous_safe); // previous_safe is now our candidate 'earliest known place that seeking // to will lead to the final page' if (!vorbis_find_page(f, &end, (int unsigned *)&last)) { // if we can't find a page, we're hosed! f->error = VORBIS_cant_find_last_page; f->total_samples = 0xffffffff; goto done; } // check if there are more pages last_page_loc = stb_vorbis_get_file_offset(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) { set_file_offset(f, end); if (!vorbis_find_page(f, &end, (int unsigned *)&last)) { // the last page we found didn't have the 'last page' flag // set. whoops! break; } previous_safe = last_page_loc+1; last_page_loc = stb_vorbis_get_file_offset(f); } set_file_offset(f, last_page_loc); // parse the header getn(f, (unsigned char *)header, 6); // extract the absolute granule position lo = get32(f); hi = get32(f); if (lo == 0xffffffff && hi == 0xffffffff) { f->error = VORBIS_cant_find_last_page; f->total_samples = SAMPLE_unknown; goto done; } if (hi) lo = 0xfffffffe; // saturate f->total_samples = lo; f->p_last.page_start = last_page_loc; f->p_last.page_end = end; f->p_last.last_decoded_sample = lo; f->p_last.first_decoded_sample = SAMPLE_unknown; f->p_last.after_previous_page_start = previous_safe; done: set_file_offset(f, restore_offset); } return f->total_samples == SAMPLE_unknown ? 0 : f->total_samples; } float stb_vorbis_stream_length_in_seconds(stb_vorbis *f) { 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); 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) f->outputs[i] = f->channel_buffers[i] + left; f->channel_buffer_start = left; f->channel_buffer_end = left+len; 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, stb_vorbis_alloc *alloc, unsigned int length) { stb_vorbis *f, p; vorbis_init(&p, alloc); p.f = file; p.f_start = ftell(file); p.stream_len = length; p.close_on_free = close_on_free; if (start_decoder(&p)) { f = vorbis_alloc(&p); if (f) { *f = p; vorbis_pump_first_frame(f); return f; } } if (error) *error = p.error; vorbis_deinit(&p); return NULL; } stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, stb_vorbis_alloc *alloc) { unsigned int len, start; start = ftell(file); fseek(file, 0, SEEK_END); len = 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(char *filename, int *error, stb_vorbis_alloc *alloc) { FILE *f = fopen(filename, "rb"); if (f) return stb_vorbis_open_file(f, TRUE, error, alloc); if (error) *error = VORBIS_file_open_failure; return NULL; } #endif // STB_VORBIS_NO_STDIO stb_vorbis * stb_vorbis_open_memory(unsigned char *data, int len, int *error, stb_vorbis_alloc *alloc) { stb_vorbis *f, p; if (data == NULL) return NULL; vorbis_init(&p, alloc); p.stream = data; p.stream_end = data + len; p.stream_start = p.stream; p.stream_len = len; p.push_mode = FALSE; if (start_decoder(&p)) { f = vorbis_alloc(&p); if (f) { *f = p; vorbis_pump_first_frame(f); return f; } } 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 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 }, }; #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() #else #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) { FASTDEF(temp); 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; } } static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len) { #define BUFFER_SIZE 32 float buffer[BUFFER_SIZE]; int i,j,o,n = BUFFER_SIZE; check_endianness(); 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]; } } 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) v = v < 0 ? -32768 : 32767; output[o+i] = v; } } } static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} }; static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len) { #define BUFFER_SIZE 32 float buffer[BUFFER_SIZE]; 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) { // 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) { 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]; } } 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]; } } } 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) v = v < 0 ? -32768 : 32767; output[o2+i] = v; } } } 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 { int limit = buf_c < data_c ? buf_c : data_c; for (i=0; i < limit; ++i) copy_samples(buffer[i]+b_offset, data[i], samples); for ( ; i < buf_c; ++i) memset(buffer[i]+b_offset, 0, sizeof(short) * samples); } } int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples) { float **output; int len = stb_vorbis_get_frame_float(f, NULL, &output); if (len > num_samples) len = num_samples; if (len) convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len); return len; } static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len) { int i; check_endianness(); if (buf_c != data_c && buf_c <= 2 && data_c <= 6) { assert(buf_c == 2); for (i=0; i < buf_c; ++i) compute_stereo_samples(buffer, data_c, data, d_offset, len); } 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) { 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) v = v < 0 ? -32768 : 32767; *buffer++ = v; } for ( ; i < buf_c; ++i) *buffer++ = 0; } } } int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts) { float **output; int len; 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; convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len); } return len; } int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts) { float **outputs; int len = num_shorts / channels; int n=0; int z = f->channels; 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 (k) convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k); buffer += k*channels; n += k; f->channel_buffer_start += k; if (n == len) break; if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; } return n; } int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len) { float **outputs; int n=0; int z = f->channels; 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 (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; } return n; } #ifndef STB_VORBIS_NO_STDIO int stb_vorbis_decode_filename(char *filename, int *channels, short **output) { int data_len, offset, total, limit, error; short *data; stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL); if (v == NULL) return -1; limit = v->channels * 4096; *channels = v->channels; offset = data_len = 0; total = limit; 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; data_len += n; offset += n * v->channels; if (offset + limit > total) { short *data2; total *= 2; data2 = (short *) realloc(data, total * sizeof(*data)); if (data2 == NULL) { free(data); stb_vorbis_close(v); return -2; } data = data2; } } *output = data; return data_len; } #endif // NO_STDIO int stb_vorbis_decode_memory(uint8 *mem, int len, int *channels, short **output) { 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; limit = v->channels * 4096; *channels = v->channels; offset = data_len = 0; total = limit; 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; data_len += n; offset += n * v->channels; if (offset + limit > total) { short *data2; total *= 2; data2 = (short *) realloc(data, total * sizeof(*data)); if (data2 == NULL) { free(data); stb_vorbis_close(v); return -2; } data = data2; } } *output = data; return data_len; } #endif int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats) { float **outputs; int len = num_floats / channels; int n=0; int z = f->channels; 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) *buffer++ = 0; } n += k; f->channel_buffer_start += k; if (n == len) break; if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; } return n; } int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples) { float **outputs; int n=0; int z = f->channels; 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+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; if (n == num_samples) break; if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; } return n; } #endif // STB_VORBIS_NO_PULLDATA_API #endif // STB_VORBIS_HEADER_ONLY dokidoki-support/stb_vorbis.h000066400000000000000000000352241147057410300167220ustar00rootroot00000000000000////////////////////////////////////////////////////////////////////////////// // // HEADER BEGINS HERE // #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H #define STB_VORBIS_INCLUDE_STB_VORBIS_H #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) #define STB_VORBIS_NO_STDIO 1 #endif #ifndef STB_VORBIS_NO_STDIO #include #endif #ifdef __cplusplus extern "C" { #endif /////////// 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. /////////// 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() 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; typedef struct { unsigned int sample_rate; int channels; unsigned int setup_memory_required; unsigned int setup_temp_memory_required; unsigned int temp_memory_required; int max_frame_size; } stb_vorbis_info; // 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); // close an ogg vorbis file and free all memory in use extern void stb_vorbis_close(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 #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. extern stb_vorbis *stb_vorbis_open_pushdata( unsigned char *datablock, int datablock_length_in_bytes, int *datablock_memory_consumed_in_bytes, int *error, 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, 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); // 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 // once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it // will begin decoding the _next_ frame. // // if you want to seek using pushdata, you need to seek in your file, then // call stb_vorbis_flush_pushdata(), then start calling decoding, then once // decoding is returning you data, call stb_vorbis_get_sample_offset, and // if you don't like the result, seek your file again and repeat. #endif ////////// 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.) #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION) extern int stb_vorbis_decode_filename(char *filename, int *channels, short **output); #endif extern int stb_vorbis_decode_memory(unsigned char *mem, int len, int *channels, short **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(unsigned char *data, int len, int *error, 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(char *filename, int *error, 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, 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. Morever, 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, 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" // this stream; if you seek it in between calls to stb_vorbis, it will become // 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); // NOT WORKING YET // 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 void stb_vorbis_seek_start(stb_vorbis *f); // this function is equivalent to stb_vorbis_seek(f,0), but it // actually works 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. #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); #endif // decode the next frame and return the number of samples per channel. 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. 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. // 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. #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); #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. #endif //////// ERROR CODES enum STBVorbisError { VORBIS__no_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_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 // 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, }; #ifdef __cplusplus } #endif #endif // STB_VORBIS_INCLUDE_STB_VORBIS_H // // HEADER ENDS HERE // //////////////////////////////////////////////////////////////////////////////