github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/raylib/external/stb_vorbis.h (about)

     1  // Ogg Vorbis audio decoder - v1.14 - public domain
     2  // http://nothings.org/stb_vorbis/
     3  //
     4  // Original version written by Sean Barrett in 2007.
     5  //
     6  // Originally sponsored by RAD Game Tools. Seeking implementation
     7  // sponsored by Phillip Bennefall, Marc Andersen, Aaron Baker,
     8  // Elias Software, Aras Pranckevicius, and Sean Barrett.
     9  //
    10  // LICENSE
    11  //
    12  //   See end of file for license information.
    13  //
    14  // Limitations:
    15  //
    16  //   - floor 0 not supported (used in old ogg vorbis files pre-2004)
    17  //   - lossless sample-truncation at beginning ignored
    18  //   - cannot concatenate multiple vorbis streams
    19  //   - sample positions are 32-bit, limiting seekable 192Khz
    20  //       files to around 6 hours (Ogg supports 64-bit)
    21  //
    22  // Feature contributors:
    23  //    Dougall Johnson (sample-exact seeking)
    24  //
    25  // Bugfix/warning contributors:
    26  //    Terje Mathisen     Niklas Frykholm     Andy Hill
    27  //    Casey Muratori     John Bolton         Gargaj
    28  //    Laurent Gomila     Marc LeBlanc        Ronny Chevalier
    29  //    Bernhard Wodo      Evan Balster        alxprd@github
    30  //    Tom Beaumont       Ingo Leitgeb        Nicolas Guillemot
    31  //    Phillip Bennefall  Rohit               Thiago Goulart
    32  //    manxorist@github   saga musix          github:infatum
    33  //    Timur Gagiev       BareRose
    34  //
    35  // Partial history:
    36  //    1.14    - 2018-02-11 - delete bogus dealloca usage
    37  //    1.13    - 2018-01-29 - fix truncation of last frame (hopefully)
    38  //    1.12    - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files
    39  //    1.11    - 2017-07-23 - fix MinGW compilation 
    40  //    1.10    - 2017-03-03 - more robust seeking; fix negative stbv_ilog(); clear error in open_memory
    41  //    1.09    - 2016-04-04 - back out 'truncation of last frame' fix from previous version
    42  //    1.08    - 2016-04-02 - warnings; setup memory leaks; truncation of last frame
    43  //    1.07    - 2015-01-16 - fixes for crashes on invalid files; warning fixes; const
    44  //    1.06    - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)
    45  //                           some crash fixes when out of memory or with corrupt files
    46  //                           fix some inappropriately signed shifts
    47  //    1.05    - 2015-04-19 - don't define __forceinline if it's redundant
    48  //    1.04    - 2014-08-27 - fix missing const-correct case in API
    49  //    1.03    - 2014-08-07 - warning fixes
    50  //    1.02    - 2014-07-09 - declare qsort comparison as explicitly _cdecl in Windows
    51  //    1.01    - 2014-06-18 - fix stb_vorbis_get_samples_float (interleaved was correct)
    52  //    1.0     - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in >2-channel;
    53  //                           (API change) report sample rate for decode-full-file funcs
    54  //
    55  // See end of file for full version history.
    56  
    57  
    58  //////////////////////////////////////////////////////////////////////////////
    59  //
    60  //  HEADER BEGINS HERE
    61  //
    62  
    63  #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
    64  #define STB_VORBIS_INCLUDE_STB_VORBIS_H
    65  
    66  #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
    67  #define STB_VORBIS_NO_STDIO
    68  #endif
    69  
    70  #ifndef STB_VORBIS_NO_STDIO
    71  #include <stdio.h>
    72  #endif
    73  
    74  // NOTE: Added to work with raylib on Android
    75  #if defined(PLATFORM_ANDROID)
    76      #include "utils.h"  // Android fopen function map
    77  #endif
    78  
    79  #ifdef __cplusplus
    80  extern "C" {
    81  #endif
    82  
    83  #ifdef STB_VORBIS_STATIC
    84  #define STBVDEF static
    85  #else
    86  #define STBVDEF extern
    87  #endif
    88  
    89  ///////////   THREAD SAFETY
    90  
    91  // Individual stb_vorbis* handles are not thread-safe; you cannot decode from
    92  // them from multiple threads at the same time. However, you can have multiple
    93  // stb_vorbis* handles and decode from them independently in multiple thrads.
    94  
    95  
    96  ///////////   MEMORY ALLOCATION
    97  
    98  // normally stb_vorbis uses malloc() to allocate memory at startup,
    99  // and alloca() to allocate temporary memory during a frame on the
   100  // stack. (Memory consumption will depend on the amount of setup
   101  // data in the file and how you set the compile flags for speed
   102  // vs. size. In my test files the maximal-size usage is ~150KB.)
   103  //
   104  // You can modify the wrapper functions in the source (stbv_setup_malloc,
   105  // stbv_setup_temp_malloc, temp_malloc) to change this behavior, or you
   106  // can use a simpler allocation model: you pass in a buffer from
   107  // which stb_vorbis will allocate _all_ its memory (including the
   108  // temp memory). "open" may fail with a VORBIS_outofmem if you
   109  // do not pass in enough data; there is no way to determine how
   110  // much you do need except to succeed (at which point you can
   111  // query get_info to find the exact amount required. yes I know
   112  // this is lame).
   113  //
   114  // If you pass in a non-NULL buffer of the type below, allocation
   115  // will occur from it as described above. Otherwise just pass NULL
   116  // to use malloc()/alloca()
   117  
   118  typedef struct
   119  {
   120     char *alloc_buffer;
   121     int   alloc_buffer_length_in_bytes;
   122  } stb_vorbis_alloc;
   123  
   124  
   125  ///////////   FUNCTIONS USEABLE WITH ALL INPUT MODES
   126  
   127  typedef struct stb_vorbis stb_vorbis;
   128  
   129  typedef struct
   130  {
   131     unsigned int sample_rate;
   132     int channels;
   133  
   134     unsigned int setup_memory_required;
   135     unsigned int setup_temp_memory_required;
   136     unsigned int temp_memory_required;
   137  
   138     int max_frame_size;
   139  } stb_vorbis_info;
   140  
   141  // get general information about the file
   142  STBVDEF stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);
   143  
   144  // get the last error detected (clears it, too)
   145  STBVDEF int stb_vorbis_get_error(stb_vorbis *f);
   146  
   147  // close an ogg vorbis file and free all memory in use
   148  STBVDEF void stb_vorbis_close(stb_vorbis *f);
   149  
   150  // this function returns the offset (in samples) from the beginning of the
   151  // file that will be returned by the next decode, if it is known, or -1
   152  // otherwise. after a flush_pushdata() call, this may take a while before
   153  // it becomes valid again.
   154  // NOT WORKING YET after a seek with PULLDATA API
   155  STBVDEF int stb_vorbis_get_sample_offset(stb_vorbis *f);
   156  
   157  // returns the current seek point within the file, or offset from the beginning
   158  // of the memory buffer. In pushdata mode it returns 0.
   159  STBVDEF unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
   160  
   161  ///////////   PUSHDATA API
   162  
   163  #ifndef STB_VORBIS_NO_PUSHDATA_API
   164  
   165  // this API allows you to get blocks of data from any source and hand
   166  // them to stb_vorbis. you have to buffer them; stb_vorbis will tell
   167  // you how much it used, and you have to give it the rest next time;
   168  // and stb_vorbis may not have enough data to work with and you will
   169  // need to give it the same data again PLUS more. Note that the Vorbis
   170  // specification does not bound the size of an individual frame.
   171  
   172  STBVDEF stb_vorbis *stb_vorbis_open_pushdata(
   173           const unsigned char * datablock, int datablock_length_in_bytes,
   174           int *datablock_memory_consumed_in_bytes,
   175           int *error,
   176           const stb_vorbis_alloc *alloc_buffer);
   177  // create a vorbis decoder by passing in the initial data block containing
   178  //    the ogg&vorbis headers (you don't need to do parse them, just provide
   179  //    the first N bytes of the file--you're told if it's not enough, see below)
   180  // on success, returns an stb_vorbis *, does not set error, returns the amount of
   181  //    data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;
   182  // on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed
   183  // if returns NULL and *error is VORBIS_need_more_data, then the input block was
   184  //       incomplete and you need to pass in a larger block from the start of the file
   185  
   186  STBVDEF int stb_vorbis_decode_frame_pushdata(
   187           stb_vorbis *f,
   188           const unsigned char *datablock, int datablock_length_in_bytes,
   189           int *channels,             // place to write number of float * buffers
   190           float ***output,           // place to write float ** array of float * buffers
   191           int *samples               // place to write number of output samples
   192       );
   193  // decode a frame of audio sample data if possible from the passed-in data block
   194  //
   195  // return value: number of bytes we used from datablock
   196  //
   197  // possible cases:
   198  //     0 bytes used, 0 samples output (need more data)
   199  //     N bytes used, 0 samples output (resynching the stream, keep going)
   200  //     N bytes used, M samples output (one frame of data)
   201  // note that after opening a file, you will ALWAYS get one N-bytes,0-sample
   202  // frame, because Vorbis always "discards" the first frame.
   203  //
   204  // Note that on resynch, stb_vorbis will rarely consume all of the buffer,
   205  // instead only datablock_length_in_bytes-3 or less. This is because it wants
   206  // to avoid missing parts of a page header if they cross a datablock boundary,
   207  // without writing state-machiney code to record a partial detection.
   208  //
   209  // The number of channels returned are stored in *channels (which can be
   210  // NULL--it is always the same as the number of channels reported by
   211  // get_info). *output will contain an array of float* buffers, one per
   212  // channel. In other words, (*output)[0][0] contains the first sample from
   213  // the first channel, and (*output)[1][0] contains the first sample from
   214  // the second channel.
   215  
   216  STBVDEF void stb_vorbis_flush_pushdata(stb_vorbis *f);
   217  // inform stb_vorbis that your next datablock will not be contiguous with
   218  // previous ones (e.g. you've seeked in the data); future attempts to decode
   219  // frames will cause stb_vorbis to resynchronize (as noted above), and
   220  // once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it
   221  // will begin decoding the _next_ frame.
   222  //
   223  // if you want to seek using pushdata, you need to seek in your file, then
   224  // call stb_vorbis_flush_pushdata(), then start calling decoding, then once
   225  // decoding is returning you data, call stb_vorbis_get_sample_offset, and
   226  // if you don't like the result, seek your file again and repeat.
   227  #endif
   228  
   229  
   230  //////////   PULLING INPUT API
   231  
   232  #ifndef STB_VORBIS_NO_PULLDATA_API
   233  // This API assumes stb_vorbis is allowed to pull data from a source--
   234  // either a block of memory containing the _entire_ vorbis stream, or a
   235  // FILE * that you or it create, or possibly some other reading mechanism
   236  // if you go modify the source to replace the FILE * case with some kind
   237  // of callback to your code. (But if you don't support seeking, you may
   238  // just want to go ahead and use pushdata.)
   239  
   240  #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
   241  STBVDEF int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);
   242  #endif
   243  #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
   244  STBVDEF int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);
   245  #endif
   246  // decode an entire file and output the data interleaved into a malloc()ed
   247  // buffer stored in *output. The return value is the number of samples
   248  // decoded, or -1 if the file could not be opened or was not an ogg vorbis file.
   249  // When you're done with it, just free() the pointer returned in *output.
   250  
   251  STBVDEF stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len,
   252                                    int *error, const stb_vorbis_alloc *alloc_buffer);
   253  // create an ogg vorbis decoder from an ogg vorbis stream in memory (note
   254  // this must be the entire stream!). on failure, returns NULL and sets *error
   255  
   256  #ifndef STB_VORBIS_NO_STDIO
   257  STBVDEF stb_vorbis * stb_vorbis_open_filename(const char *filename,
   258                                    int *error, const stb_vorbis_alloc *alloc_buffer);
   259  // create an ogg vorbis decoder from a filename via fopen(). on failure,
   260  // returns NULL and sets *error (possibly to VORBIS_file_open_failure).
   261  
   262  STBVDEF stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close,
   263                                    int *error, const stb_vorbis_alloc *alloc_buffer);
   264  // create an ogg vorbis decoder from an open FILE *, looking for a stream at
   265  // the _current_ seek point (ftell). on failure, returns NULL and sets *error.
   266  // note that stb_vorbis must "own" this stream; if you seek it in between
   267  // calls to stb_vorbis, it will become confused. Morever, if you attempt to
   268  // perform stb_vorbis_seek_*() operations on this file, it will assume it
   269  // owns the _entire_ rest of the file after the start point. Use the next
   270  // function, stb_vorbis_open_file_section(), to limit it.
   271  
   272  STBVDEF stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close,
   273                  int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len);
   274  // create an ogg vorbis decoder from an open FILE *, looking for a stream at
   275  // the _current_ seek point (ftell); the stream will be of length 'len' bytes.
   276  // on failure, returns NULL and sets *error. note that stb_vorbis must "own"
   277  // this stream; if you seek it in between calls to stb_vorbis, it will become
   278  // confused.
   279  #endif
   280  
   281  STBVDEF int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);
   282  STBVDEF int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number);
   283  // these functions seek in the Vorbis file to (approximately) 'sample_number'.
   284  // after calling seek_frame(), the next call to get_frame_*() will include
   285  // the specified sample. after calling stb_vorbis_seek(), the next call to
   286  // stb_vorbis_get_samples_* will start with the specified sample. If you
   287  // do not need to seek to EXACTLY the target sample when using get_samples_*,
   288  // you can also use seek_frame().
   289  
   290  STBVDEF int stb_vorbis_seek_start(stb_vorbis *f);
   291  // this function is equivalent to stb_vorbis_seek(f,0)
   292  
   293  STBVDEF unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f);
   294  STBVDEF float        stb_vorbis_stream_length_in_seconds(stb_vorbis *f);
   295  // these functions return the total length of the vorbis stream
   296  
   297  STBVDEF int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output);
   298  // decode the next frame and return the number of samples. the number of
   299  // channels returned are stored in *channels (which can be NULL--it is always
   300  // the same as the number of channels reported by get_info). *output will
   301  // contain an array of float* buffers, one per channel. These outputs will
   302  // be overwritten on the next call to stb_vorbis_get_frame_*.
   303  //
   304  // You generally should not intermix calls to stb_vorbis_get_frame_*()
   305  // and stb_vorbis_get_samples_*(), since the latter calls the former.
   306  
   307  #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
   308  STBVDEF int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts);
   309  STBVDEF int stb_vorbis_get_frame_short            (stb_vorbis *f, int num_c, short **buffer, int num_samples);
   310  #endif
   311  // decode the next frame and return the number of *samples* per channel.
   312  // Note that for interleaved data, you pass in the number of shorts (the
   313  // size of your array), but the return value is the number of samples per
   314  // channel, not the total number of samples.
   315  //
   316  // The data is coerced to the number of channels you request according to the
   317  // channel coercion rules (see below). You must pass in the size of your
   318  // buffer(s) so that stb_vorbis will not overwrite the end of the buffer.
   319  // The maximum buffer size needed can be gotten from get_info(); however,
   320  // the Vorbis I specification implies an absolute maximum of 4096 samples
   321  // per channel.
   322  
   323  // Channel coercion rules:
   324  //    Let M be the number of channels requested, and N the number of channels present,
   325  //    and Cn be the nth channel; let stereo L be the sum of all L and center channels,
   326  //    and stereo R be the sum of all R and center channels (channel assignment from the
   327  //    vorbis spec).
   328  //        M    N       output
   329  //        1    k      sum(Ck) for all k
   330  //        2    *      stereo L, stereo R
   331  //        k    l      k > l, the first l channels, then 0s
   332  //        k    l      k <= l, the first k channels
   333  //    Note that this is not _good_ surround etc. mixing at all! It's just so
   334  //    you get something useful.
   335  
   336  STBVDEF int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats);
   337  STBVDEF int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples);
   338  // gets num_samples samples, not necessarily on a frame boundary--this requires
   339  // buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.
   340  // Returns the number of samples stored per channel; it may be less than requested
   341  // at the end of the file. If there are no more samples in the file, returns 0.
   342  
   343  #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
   344  STBVDEF int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts);
   345  STBVDEF int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples);
   346  #endif
   347  // gets num_samples samples, not necessarily on a frame boundary--this requires
   348  // buffering so you have to supply the buffers. Applies the coercion rules above
   349  // to produce 'channels' channels. Returns the number of samples stored per channel;
   350  // it may be less than requested at the end of the file. If there are no more
   351  // samples in the file, returns 0.
   352  
   353  #endif
   354  
   355  ////////   ERROR CODES
   356  
   357  enum STBVorbisError
   358  {
   359     VORBIS__no_error,
   360  
   361     VORBIS_need_more_data=1,             // not a real error
   362  
   363     VORBIS_invalid_api_mixing,           // can't mix API modes
   364     VORBIS_outofmem,                     // not enough memory
   365     VORBIS_feature_not_supported,        // uses floor 0
   366     VORBIS_too_many_channels,            // STB_VORBIS_MAX_CHANNELS is too small
   367     VORBIS_file_open_failure,            // fopen() failed
   368     VORBIS_seek_without_length,          // can't seek in unknown-length file
   369  
   370     VORBIS_unexpected_eof=10,            // file is truncated?
   371     VORBIS_seek_invalid,                 // seek past EOF
   372  
   373     // decoding errors (corrupt/invalid stream) -- you probably
   374     // don't care about the exact details of these
   375  
   376     // vorbis errors:
   377     VORBIS_invalid_setup=20,
   378     VORBIS_invalid_stream,
   379  
   380     // ogg errors:
   381     VORBIS_missing_capture_pattern=30,
   382     VORBIS_invalid_stream_structure_version,
   383     VORBIS_continued_packet_flag_invalid,
   384     VORBIS_incorrect_stream_serial_number,
   385     VORBIS_invalid_first_page,
   386     VORBIS_bad_packet_type,
   387     VORBIS_cant_find_last_page,
   388     VORBIS_seek_failed
   389  };
   390  
   391  
   392  #ifdef __cplusplus
   393  }
   394  #endif
   395  
   396  #endif // STB_VORBIS_INCLUDE_STB_VORBIS_H
   397  //
   398  //  HEADER ENDS HERE
   399  //
   400  //////////////////////////////////////////////////////////////////////////////
   401  
   402  #ifdef STB_VORBIS_IMPLEMENTATION
   403  
   404  // global configuration settings (e.g. set these in the project/makefile),
   405  // or just set them in this file at the top (although ideally the first few
   406  // should be visible when the header file is compiled too, although it's not
   407  // crucial)
   408  
   409  // STB_VORBIS_NO_PUSHDATA_API
   410  //     does not compile the code for the various stb_vorbis_*_pushdata()
   411  //     functions
   412  // #define STB_VORBIS_NO_PUSHDATA_API
   413  
   414  // STB_VORBIS_NO_PULLDATA_API
   415  //     does not compile the code for the non-pushdata APIs
   416  // #define STB_VORBIS_NO_PULLDATA_API
   417  
   418  // STB_VORBIS_NO_STDIO
   419  //     does not compile the code for the APIs that use FILE *s internally
   420  //     or externally (implied by STB_VORBIS_NO_PULLDATA_API)
   421  // #define STB_VORBIS_NO_STDIO
   422  
   423  // STB_VORBIS_NO_INTEGER_CONVERSION
   424  //     does not compile the code for converting audio sample data from
   425  //     float to integer (implied by STB_VORBIS_NO_PULLDATA_API)
   426  // #define STB_VORBIS_NO_INTEGER_CONVERSION
   427  
   428  // STB_VORBIS_NO_FAST_SCALED_FLOAT
   429  //      does not use a fast float-to-int trick to accelerate float-to-int on
   430  //      most platforms which requires endianness be defined correctly.
   431  // #define STB_VORBIS_NO_FAST_SCALED_FLOAT
   432  
   433  
   434  // STB_VORBIS_MAX_CHANNELS [number]
   435  //     globally define this to the maximum number of channels you need.
   436  //     The spec does not put a restriction on channels except that
   437  //     the count is stored in a byte, so 255 is the hard limit.
   438  //     Reducing this saves about 16 bytes per value, so using 16 saves
   439  //     (255-16)*16 or around 4KB. Plus anything other memory usage
   440  //     I forgot to account for. Can probably go as low as 8 (7.1 audio),
   441  //     6 (5.1 audio), or 2 (stereo only).
   442  #ifndef STB_VORBIS_MAX_CHANNELS
   443  #define STB_VORBIS_MAX_CHANNELS    16  // enough for anyone?
   444  #endif
   445  
   446  // STB_VORBIS_PUSHDATA_CRC_COUNT [number]
   447  //     after a flush_pushdata(), stb_vorbis begins scanning for the
   448  //     next valid page, without backtracking. when it finds something
   449  //     that looks like a page, it streams through it and verifies its
   450  //     CRC32. Should that validation fail, it keeps scanning. But it's
   451  //     possible that _while_ streaming through to check the CRC32 of
   452  //     one candidate page, it sees another candidate page. This #define
   453  //     determines how many "overlapping" candidate pages it can search
   454  //     at once. Note that "real" pages are typically ~4KB to ~8KB, whereas
   455  //     garbage pages could be as big as 64KB, but probably average ~16KB.
   456  //     So don't hose ourselves by scanning an apparent 64KB page and
   457  //     missing a ton of real ones in the interim; so minimum of 2
   458  #ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
   459  #define STB_VORBIS_PUSHDATA_CRC_COUNT  4
   460  #endif
   461  
   462  // STB_VORBIS_FAST_HUFFMAN_LENGTH [number]
   463  //     sets the log size of the huffman-acceleration table.  Maximum
   464  //     supported value is 24. with larger numbers, more decodings are O(1),
   465  //     but the table size is larger so worse cache missing, so you'll have
   466  //     to probe (and try multiple ogg vorbis files) to find the sweet spot.
   467  #ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
   468  #define STB_VORBIS_FAST_HUFFMAN_LENGTH   10
   469  #endif
   470  
   471  // STB_VORBIS_FAST_BINARY_LENGTH [number]
   472  //     sets the log size of the binary-search acceleration table. this
   473  //     is used in similar fashion to the fast-huffman size to set initial
   474  //     parameters for the binary search
   475  
   476  // STB_VORBIS_FAST_HUFFMAN_INT
   477  //     The fast huffman tables are much more efficient if they can be
   478  //     stored as 16-bit results instead of 32-bit results. This restricts
   479  //     the codebooks to having only 65535 possible outcomes, though.
   480  //     (At least, accelerated by the huffman table.)
   481  #ifndef STB_VORBIS_FAST_HUFFMAN_INT
   482  #define STB_VORBIS_FAST_HUFFMAN_SHORT
   483  #endif
   484  
   485  // STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
   486  //     If the 'fast huffman' search doesn't succeed, then stb_vorbis falls
   487  //     back on binary searching for the correct one. This requires storing
   488  //     extra tables with the huffman codes in sorted order. Defining this
   489  //     symbol trades off space for speed by forcing a linear search in the
   490  //     non-fast case, except for "sparse" codebooks.
   491  // #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
   492  
   493  // STB_VORBIS_DIVIDES_IN_RESIDUE
   494  //     stb_vorbis precomputes the result of the scalar residue decoding
   495  //     that would otherwise require a divide per chunk. you can trade off
   496  //     space for time by defining this symbol.
   497  // #define STB_VORBIS_DIVIDES_IN_RESIDUE
   498  
   499  // STB_VORBIS_DIVIDES_IN_CODEBOOK
   500  //     vorbis VQ codebooks can be encoded two ways: with every case explicitly
   501  //     stored, or with all elements being chosen from a small range of values,
   502  //     and all values possible in all elements. By default, stb_vorbis expands
   503  //     this latter kind out to look like the former kind for ease of decoding,
   504  //     because otherwise an integer divide-per-vector-element is required to
   505  //     unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can
   506  //     trade off storage for speed.
   507  //#define STB_VORBIS_DIVIDES_IN_CODEBOOK
   508  
   509  #ifdef STB_VORBIS_CODEBOOK_SHORTS
   510  #error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"
   511  #endif
   512  
   513  // STB_VORBIS_DIVIDE_TABLE
   514  //     this replaces small integer divides in the floor decode loop with
   515  //     table lookups. made less than 1% difference, so disabled by default.
   516  
   517  // STB_VORBIS_NO_INLINE_DECODE
   518  //     disables the inlining of the scalar codebook fast-huffman decode.
   519  //     might save a little codespace; useful for debugging
   520  // #define STB_VORBIS_NO_INLINE_DECODE
   521  
   522  // STB_VORBIS_NO_DEFER_FLOOR
   523  //     Normally we only decode the floor without synthesizing the actual
   524  //     full curve. We can instead synthesize the curve immediately. This
   525  //     requires more memory and is very likely slower, so I don't think
   526  //     you'd ever want to do it except for debugging.
   527  // #define STB_VORBIS_NO_DEFER_FLOOR
   528  
   529  
   530  
   531  
   532  //////////////////////////////////////////////////////////////////////////////
   533  
   534  #ifdef STB_VORBIS_NO_PULLDATA_API
   535     #define STB_VORBIS_NO_INTEGER_CONVERSION
   536     #define STB_VORBIS_NO_STDIO
   537  #endif
   538  
   539  #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
   540     #define STB_VORBIS_NO_STDIO 1
   541  #endif
   542  
   543  #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
   544  #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
   545  
   546     // only need endianness for fast-float-to-int, which we don't
   547     // use for pushdata
   548  
   549     #ifndef STB_VORBIS_BIG_ENDIAN
   550       #define STB_VORBIS_ENDIAN  0
   551     #else
   552       #define STB_VORBIS_ENDIAN  1
   553     #endif
   554  
   555  #endif
   556  #endif
   557  
   558  
   559  #ifndef STB_VORBIS_NO_STDIO
   560  #include <stdio.h>
   561  #endif
   562  
   563  #ifndef STB_VORBIS_NO_CRT
   564     #include <stdlib.h>
   565     #include <string.h>
   566     #include <assert.h>
   567     #include <math.h>
   568  
   569     // find definition of alloca if it's not in stdlib.h:
   570     #if defined(_MSC_VER) || defined(__MINGW32__)
   571        #include <malloc.h>
   572     #endif
   573     #if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__) || defined(__APPLE__) || defined(__CYGWIN__)
   574        #include <alloca.h>
   575     #endif
   576  #else // STB_VORBIS_NO_CRT
   577     #define NULL 0
   578     #define malloc(s)   0
   579     #define free(s)     ((void) 0)
   580     #define realloc(s)  0
   581  #endif // STB_VORBIS_NO_CRT
   582  
   583  #include <limits.h>
   584  
   585  #ifdef __MINGW32__
   586     // eff you mingw:
   587     //     "fixed":
   588     //         http://sourceforge.net/p/mingw-w64/mailman/message/32882927/
   589     //     "no that broke the build, reverted, who cares about C":
   590     //         http://sourceforge.net/p/mingw-w64/mailman/message/32890381/
   591     #ifdef __forceinline
   592     #undef __forceinline
   593     #endif
   594     #define __forceinline
   595     #ifndef alloca
   596     #define alloca(s) __builtin_alloca(s)
   597     #endif
   598  #elif !defined(_MSC_VER)
   599     #if __GNUC__
   600        #define __forceinline inline
   601     #else
   602        #define __forceinline
   603     #endif
   604  #endif
   605  
   606  #if STB_VORBIS_MAX_CHANNELS > 256
   607  #error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"
   608  #endif
   609  
   610  #if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24
   611  #error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"
   612  #endif
   613  
   614  
   615  #if 0
   616  #include <crtdbg.h>
   617  #define STBV_CHECK(f)   _CrtIsValidHeapPointer(f->channel_buffers[1])
   618  #else
   619  #define STBV_CHECK(f)   ((void) 0)
   620  #endif
   621  
   622  #define STBV_MAX_BLOCKSIZE_LOG  13   // from specification
   623  #define STBV_MAX_BLOCKSIZE      (1 << STBV_MAX_BLOCKSIZE_LOG)
   624  
   625  
   626  typedef unsigned char  stbv_uint8;
   627  typedef   signed char  stbv_int8;
   628  typedef unsigned short stbv_uint16;
   629  typedef   signed short stbv_int16;
   630  typedef unsigned int   stbv_uint32;
   631  typedef   signed int   stbv_int32;
   632  
   633  #ifndef TRUE
   634  #define TRUE 1
   635  #define FALSE 0
   636  #endif
   637  
   638  typedef float stbv_codetype;
   639  
   640  // @NOTE
   641  //
   642  // Some arrays below are tagged "//varies", which means it's actually
   643  // a variable-sized piece of data, but rather than malloc I assume it's
   644  // small enough it's better to just allocate it all together with the
   645  // main thing
   646  //
   647  // Most of the variables are specified with the smallest size I could pack
   648  // them into. It might give better performance to make them all full-sized
   649  // integers. It should be safe to freely rearrange the structures or change
   650  // the sizes larger--nothing relies on silently truncating etc., nor the
   651  // order of variables.
   652  
   653  #define STBV_FAST_HUFFMAN_TABLE_SIZE   (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)
   654  #define STBV_FAST_HUFFMAN_TABLE_MASK   (STBV_FAST_HUFFMAN_TABLE_SIZE - 1)
   655  
   656  typedef struct
   657  {
   658     int dimensions, entries;
   659     stbv_uint8 *codeword_lengths;
   660     float  minimum_value;
   661     float  delta_value;
   662     stbv_uint8  value_bits;
   663     stbv_uint8  lookup_type;
   664     stbv_uint8  sequence_p;
   665     stbv_uint8  sparse;
   666     stbv_uint32 lookup_values;
   667     stbv_codetype *multiplicands;
   668     stbv_uint32 *codewords;
   669     #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
   670      stbv_int16  fast_huffman[STBV_FAST_HUFFMAN_TABLE_SIZE];
   671     #else
   672      stbv_int32  fast_huffman[STBV_FAST_HUFFMAN_TABLE_SIZE];
   673     #endif
   674     stbv_uint32 *sorted_codewords;
   675     int    *sorted_values;
   676     int     sorted_entries;
   677  } StbvCodebook;
   678  
   679  typedef struct
   680  {
   681     stbv_uint8 order;
   682     stbv_uint16 rate;
   683     stbv_uint16 bark_map_size;
   684     stbv_uint8 amplitude_bits;
   685     stbv_uint8 amplitude_offset;
   686     stbv_uint8 number_of_books;
   687     stbv_uint8 book_list[16]; // varies
   688  } StbvFloor0;
   689  
   690  typedef struct
   691  {
   692     stbv_uint8 partitions;
   693     stbv_uint8 partition_class_list[32]; // varies
   694     stbv_uint8 class_dimensions[16]; // varies
   695     stbv_uint8 class_subclasses[16]; // varies
   696     stbv_uint8 class_masterbooks[16]; // varies
   697     stbv_int16 subclass_books[16][8]; // varies
   698     stbv_uint16 Xlist[31*8+2]; // varies
   699     stbv_uint8 sorted_order[31*8+2];
   700     stbv_uint8 stbv_neighbors[31*8+2][2];
   701     stbv_uint8 floor1_multiplier;
   702     stbv_uint8 rangebits;
   703     int values;
   704  } StbvFloor1;
   705  
   706  typedef union
   707  {
   708     StbvFloor0 floor0;
   709     StbvFloor1 floor1;
   710  } StbvFloor;
   711  
   712  typedef struct
   713  {
   714     stbv_uint32 begin, end;
   715     stbv_uint32 part_size;
   716     stbv_uint8 classifications;
   717     stbv_uint8 classbook;
   718     stbv_uint8 **classdata;
   719     stbv_int16 (*residue_books)[8];
   720  } StbvResidue;
   721  
   722  typedef struct
   723  {
   724     stbv_uint8 magnitude;
   725     stbv_uint8 angle;
   726     stbv_uint8 mux;
   727  } StbvMappingChannel;
   728  
   729  typedef struct
   730  {
   731     stbv_uint16 coupling_steps;
   732     StbvMappingChannel *chan;
   733     stbv_uint8  submaps;
   734     stbv_uint8  submap_floor[15]; // varies
   735     stbv_uint8  submap_residue[15]; // varies
   736  } StbvMapping;
   737  
   738  typedef struct
   739  {
   740     stbv_uint8 blockflag;
   741     stbv_uint8 mapping;
   742     stbv_uint16 windowtype;
   743     stbv_uint16 transformtype;
   744  } StbvMode;
   745  
   746  typedef struct
   747  {
   748     stbv_uint32  goal_crc;    // expected crc if match
   749     int     bytes_left;  // bytes left in packet
   750     stbv_uint32  crc_so_far;  // running crc
   751     int     bytes_done;  // bytes processed in _current_ chunk
   752     stbv_uint32  sample_loc;  // granule pos encoded in page
   753  } StbvCRCscan;
   754  
   755  typedef struct
   756  {
   757     stbv_uint32 page_start, page_end;
   758     stbv_uint32 last_decoded_sample;
   759  } StbvProbedPage;
   760  
   761  struct stb_vorbis
   762  {
   763    // user-accessible info
   764     unsigned int sample_rate;
   765     int channels;
   766  
   767     unsigned int setup_memory_required;
   768     unsigned int temp_memory_required;
   769     unsigned int setup_temp_memory_required;
   770  
   771    // input config
   772  #ifndef STB_VORBIS_NO_STDIO
   773     FILE *f;
   774     stbv_uint32 f_start;
   775     int close_on_free;
   776  #endif
   777  
   778     stbv_uint8 *stream;
   779     stbv_uint8 *stream_start;
   780     stbv_uint8 *stream_end;
   781  
   782     stbv_uint32 stream_len;
   783  
   784     stbv_uint8  push_mode;
   785  
   786     stbv_uint32 first_audio_page_offset;
   787  
   788     StbvProbedPage p_first, p_last;
   789  
   790    // memory management
   791     stb_vorbis_alloc alloc;
   792     int setup_offset;
   793     int temp_offset;
   794  
   795    // run-time results
   796     int eof;
   797     enum STBVorbisError error;
   798  
   799    // user-useful data
   800  
   801    // header info
   802     int blocksize[2];
   803     int blocksize_0, blocksize_1;
   804     int codebook_count;
   805     StbvCodebook *codebooks;
   806     int floor_count;
   807     stbv_uint16 floor_types[64]; // varies
   808     StbvFloor *floor_config;
   809     int residue_count;
   810     stbv_uint16 residue_types[64]; // varies
   811     StbvResidue *residue_config;
   812     int mapping_count;
   813     StbvMapping *mapping;
   814     int mode_count;
   815     StbvMode mode_config[64];  // varies
   816  
   817     stbv_uint32 total_samples;
   818  
   819    // decode buffer
   820     float *channel_buffers[STB_VORBIS_MAX_CHANNELS];
   821     float *outputs        [STB_VORBIS_MAX_CHANNELS];
   822  
   823     float *previous_window[STB_VORBIS_MAX_CHANNELS];
   824     int previous_length;
   825  
   826     #ifndef STB_VORBIS_NO_DEFER_FLOOR
   827     stbv_int16 *finalY[STB_VORBIS_MAX_CHANNELS];
   828     #else
   829     float *floor_buffers[STB_VORBIS_MAX_CHANNELS];
   830     #endif
   831  
   832     stbv_uint32 current_loc; // sample location of next frame to decode
   833     int    current_loc_valid;
   834  
   835    // per-blocksize precomputed data
   836     
   837     // twiddle factors
   838     float *A[2],*B[2],*C[2];
   839     float *window[2];
   840     stbv_uint16 *stbv_bit_reverse[2];
   841  
   842    // current page/packet/segment streaming info
   843     stbv_uint32 serial; // stream serial number for verification
   844     int last_page;
   845     int segment_count;
   846     stbv_uint8 segments[255];
   847     stbv_uint8 page_flag;
   848     stbv_uint8 bytes_in_seg;
   849     stbv_uint8 first_decode;
   850     int next_seg;
   851     int last_seg;  // flag that we're on the last segment
   852     int last_seg_which; // what was the segment number of the last seg?
   853     stbv_uint32 acc;
   854     int valid_bits;
   855     int packet_bytes;
   856     int end_seg_with_known_loc;
   857     stbv_uint32 known_loc_for_packet;
   858     int discard_samples_deferred;
   859     stbv_uint32 samples_output;
   860  
   861    // push mode scanning
   862     int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching
   863  #ifndef STB_VORBIS_NO_PUSHDATA_API
   864     StbvCRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT];
   865  #endif
   866  
   867    // sample-access
   868     int channel_buffer_start;
   869     int channel_buffer_end;
   870  };
   871  
   872  #if defined(STB_VORBIS_NO_PUSHDATA_API)
   873     #define STBV_IS_PUSH_MODE(f)   FALSE
   874  #elif defined(STB_VORBIS_NO_PULLDATA_API)
   875     #define STBV_IS_PUSH_MODE(f)   TRUE
   876  #else
   877     #define STBV_IS_PUSH_MODE(f)   ((f)->push_mode)
   878  #endif
   879  
   880  typedef struct stb_vorbis stbv_vorb;
   881  
   882  static int stbv_error(stbv_vorb *f, enum STBVorbisError e)
   883  {
   884     f->error = e;
   885     if (!f->eof && e != VORBIS_need_more_data) {
   886        f->error=e; // breakpoint for debugging
   887     }
   888     return 0;
   889  }
   890  
   891  
   892  // these functions are used for allocating temporary memory
   893  // while decoding. if you can afford the stack space, use
   894  // alloca(); otherwise, provide a temp buffer and it will
   895  // allocate out of those.
   896  
   897  #define stbv_array_size_required(count,size)  (count*(sizeof(void *)+(size)))
   898  
   899  #define stbv_temp_alloc(f,size)              (f->alloc.alloc_buffer ? stbv_setup_temp_malloc(f,size) : alloca(size))
   900  #define stbv_temp_free(f,p)                  0
   901  #define stbv_temp_alloc_save(f)              ((f)->temp_offset)
   902  #define stbv_temp_alloc_restore(f,p)         ((f)->temp_offset = (p))
   903  
   904  #define stbv_temp_block_array(f,count,size)  stbv_make_block_array(stbv_temp_alloc(f,stbv_array_size_required(count,size)), count, size)
   905  
   906  // given a sufficiently large block of memory, make an array of pointers to subblocks of it
   907  static void *stbv_make_block_array(void *mem, int count, int size)
   908  {
   909     int i;
   910     void ** p = (void **) mem;
   911     char *q = (char *) (p + count);
   912     for (i=0; i < count; ++i) {
   913        p[i] = q;
   914        q += size;
   915     }
   916     return p;
   917  }
   918  
   919  static void *stbv_setup_malloc(stbv_vorb *f, int sz)
   920  {
   921     sz = (sz+3) & ~3;
   922     f->setup_memory_required += sz;
   923     if (f->alloc.alloc_buffer) {
   924        void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
   925        if (f->setup_offset + sz > f->temp_offset) return NULL;
   926        f->setup_offset += sz;
   927        return p;
   928     }
   929     return sz ? malloc(sz) : NULL;
   930  }
   931  
   932  static void stbv_setup_free(stbv_vorb *f, void *p)
   933  {
   934     if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack
   935     free(p);
   936  }
   937  
   938  static void *stbv_setup_temp_malloc(stbv_vorb *f, int sz)
   939  {
   940     sz = (sz+3) & ~3;
   941     if (f->alloc.alloc_buffer) {
   942        if (f->temp_offset - sz < f->setup_offset) return NULL;
   943        f->temp_offset -= sz;
   944        return (char *) f->alloc.alloc_buffer + f->temp_offset;
   945     }
   946     return malloc(sz);
   947  }
   948  
   949  static void stbv_setup_temp_free(stbv_vorb *f, void *p, int sz)
   950  {
   951     if (f->alloc.alloc_buffer) {
   952        f->temp_offset += (sz+3)&~3;
   953        return;
   954     }
   955     free(p);
   956  }
   957  
   958  #define STBV_CRC32_POLY    0x04c11db7   // from spec
   959  
   960  static stbv_uint32 stbv_crc_table[256];
   961  static void stbv_crc32_init(void)
   962  {
   963     int i,j;
   964     stbv_uint32 s;
   965     for(i=0; i < 256; i++) {
   966        for (s=(stbv_uint32) i << 24, j=0; j < 8; ++j)
   967           s = (s << 1) ^ (s >= (1U<<31) ? STBV_CRC32_POLY : 0);
   968        stbv_crc_table[i] = s;
   969     }
   970  }
   971  
   972  static __forceinline stbv_uint32 stbv_crc32_update(stbv_uint32 crc, stbv_uint8 byte)
   973  {
   974     return (crc << 8) ^ stbv_crc_table[byte ^ (crc >> 24)];
   975  }
   976  
   977  
   978  // used in setup, and for huffman that doesn't go fast path
   979  static unsigned int stbv_bit_reverse(unsigned int n)
   980  {
   981    n = ((n & 0xAAAAAAAA) >>  1) | ((n & 0x55555555) << 1);
   982    n = ((n & 0xCCCCCCCC) >>  2) | ((n & 0x33333333) << 2);
   983    n = ((n & 0xF0F0F0F0) >>  4) | ((n & 0x0F0F0F0F) << 4);
   984    n = ((n & 0xFF00FF00) >>  8) | ((n & 0x00FF00FF) << 8);
   985    return (n >> 16) | (n << 16);
   986  }
   987  
   988  static float stbv_square(float x)
   989  {
   990     return x*x;
   991  }
   992  
   993  // this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3
   994  // as required by the specification. fast(?) implementation from stb.h
   995  // @OPTIMIZE: called multiple times per-packet with "constants"; move to setup
   996  static int stbv_ilog(stbv_int32 n)
   997  {
   998     static signed char log2_4[16] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
   999  
  1000     if (n < 0) return 0; // signed n returns 0
  1001  
  1002     // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)
  1003     if (n < (1 << 14))
  1004          if (n < (1 <<  4))            return  0 + log2_4[n      ];
  1005          else if (n < (1 <<  9))       return  5 + log2_4[n >>  5];
  1006               else                     return 10 + log2_4[n >> 10];
  1007     else if (n < (1 << 24))
  1008               if (n < (1 << 19))       return 15 + log2_4[n >> 15];
  1009               else                     return 20 + log2_4[n >> 20];
  1010          else if (n < (1 << 29))       return 25 + log2_4[n >> 25];
  1011               else                     return 30 + log2_4[n >> 30];
  1012  }
  1013  
  1014  #ifndef M_PI
  1015    #define M_PI  3.14159265358979323846264f  // from CRC
  1016  #endif
  1017  
  1018  // code length assigned to a value with no huffman encoding
  1019  #define NO_CODE   255
  1020  
  1021  /////////////////////// LEAF SETUP FUNCTIONS //////////////////////////
  1022  //
  1023  // these functions are only called at setup, and only a few times
  1024  // per file
  1025  
  1026  static float stbv_float32_unpack(stbv_uint32 x)
  1027  {
  1028     // from the specification
  1029     stbv_uint32 mantissa = x & 0x1fffff;
  1030     stbv_uint32 sign = x & 0x80000000;
  1031     stbv_uint32 exp = (x & 0x7fe00000) >> 21;
  1032     double res = sign ? -(double)mantissa : (double)mantissa;
  1033     return (float) ldexp((float)res, exp-788);
  1034  }
  1035  
  1036  
  1037  // zlib & jpeg huffman tables assume that the output symbols
  1038  // can either be arbitrarily arranged, or have monotonically
  1039  // increasing frequencies--they rely on the lengths being sorted;
  1040  // this makes for a very simple generation algorithm.
  1041  // vorbis allows a huffman table with non-sorted lengths. This
  1042  // requires a more sophisticated construction, since symbols in
  1043  // order do not map to huffman codes "in order".
  1044  static void stbv_add_entry(StbvCodebook *c, stbv_uint32 huff_code, int symbol, int count, int len, stbv_uint32 *values)
  1045  {
  1046     if (!c->sparse) {
  1047        c->codewords      [symbol] = huff_code;
  1048     } else {
  1049        c->codewords       [count] = huff_code;
  1050        c->codeword_lengths[count] = len;
  1051        values             [count] = symbol;
  1052     }
  1053  }
  1054  
  1055  static int stbv_compute_codewords(StbvCodebook *c, stbv_uint8 *len, int n, stbv_uint32 *values)
  1056  {
  1057     int i,k,m=0;
  1058     stbv_uint32 available[32];
  1059  
  1060     memset(available, 0, sizeof(available));
  1061     // find the first entry
  1062     for (k=0; k < n; ++k) if (len[k] < NO_CODE) break;
  1063     if (k == n) { assert(c->sorted_entries == 0); return TRUE; }
  1064     // add to the list
  1065     stbv_add_entry(c, 0, k, m++, len[k], values);
  1066     // add all available leaves
  1067     for (i=1; i <= len[k]; ++i)
  1068        available[i] = 1U << (32-i);
  1069     // note that the above code treats the first case specially,
  1070     // but it's really the same as the following code, so they
  1071     // could probably be combined (except the initial code is 0,
  1072     // and I use 0 in available[] to mean 'empty')
  1073     for (i=k+1; i < n; ++i) {
  1074        stbv_uint32 res;
  1075        int z = len[i], y;
  1076        if (z == NO_CODE) continue;
  1077        // find lowest available leaf (should always be earliest,
  1078        // which is what the specification calls for)
  1079        // note that this property, and the fact we can never have
  1080        // more than one free leaf at a given level, isn't totally
  1081        // trivial to prove, but it seems true and the assert never
  1082        // fires, so!
  1083        while (z > 0 && !available[z]) --z;
  1084        if (z == 0) { return FALSE; }
  1085        res = available[z];
  1086        assert(z >= 0 && z < 32);
  1087        available[z] = 0;
  1088        stbv_add_entry(c, stbv_bit_reverse(res), i, m++, len[i], values);
  1089        // propogate availability up the tree
  1090        if (z != len[i]) {
  1091           assert(len[i] >= 0 && len[i] < 32);
  1092           for (y=len[i]; y > z; --y) {
  1093              assert(available[y] == 0);
  1094              available[y] = res + (1 << (32-y));
  1095           }
  1096        }
  1097     }
  1098     return TRUE;
  1099  }
  1100  
  1101  // accelerated huffman table allows fast O(1) match of all symbols
  1102  // of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH
  1103  static void stbv_compute_accelerated_huffman(StbvCodebook *c)
  1104  {
  1105     int i, len;
  1106     for (i=0; i < STBV_FAST_HUFFMAN_TABLE_SIZE; ++i)
  1107        c->fast_huffman[i] = -1;
  1108  
  1109     len = c->sparse ? c->sorted_entries : c->entries;
  1110     #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
  1111     if (len > 32767) len = 32767; // largest possible value we can encode!
  1112     #endif
  1113     for (i=0; i < len; ++i) {
  1114        if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {
  1115           stbv_uint32 z = c->sparse ? stbv_bit_reverse(c->sorted_codewords[i]) : c->codewords[i];
  1116           // set table entries for all bit combinations in the higher bits
  1117           while (z < STBV_FAST_HUFFMAN_TABLE_SIZE) {
  1118               c->fast_huffman[z] = i;
  1119               z += 1 << c->codeword_lengths[i];
  1120           }
  1121        }
  1122     }
  1123  }
  1124  
  1125  #ifdef _MSC_VER
  1126  #define STBV_CDECL __cdecl
  1127  #else
  1128  #define STBV_CDECL
  1129  #endif
  1130  
  1131  static int STBV_CDECL stbv_uint32_compare(const void *p, const void *q)
  1132  {
  1133     stbv_uint32 x = * (stbv_uint32 *) p;
  1134     stbv_uint32 y = * (stbv_uint32 *) q;
  1135     return x < y ? -1 : x > y;
  1136  }
  1137  
  1138  static int stbv_include_in_sort(StbvCodebook *c, stbv_uint8 len)
  1139  {
  1140     if (c->sparse) { assert(len != NO_CODE); return TRUE; }
  1141     if (len == NO_CODE) return FALSE;
  1142     if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE;
  1143     return FALSE;
  1144  }
  1145  
  1146  // if the fast table above doesn't work, we want to binary
  1147  // search them... need to reverse the bits
  1148  static void stbv_compute_sorted_huffman(StbvCodebook *c, stbv_uint8 *lengths, stbv_uint32 *values)
  1149  {
  1150     int i, len;
  1151     // build a list of all the entries
  1152     // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.
  1153     // this is kind of a frivolous optimization--I don't see any performance improvement,
  1154     // but it's like 4 extra lines of code, so.
  1155     if (!c->sparse) {
  1156        int k = 0;
  1157        for (i=0; i < c->entries; ++i)
  1158           if (stbv_include_in_sort(c, lengths[i])) 
  1159              c->sorted_codewords[k++] = stbv_bit_reverse(c->codewords[i]);
  1160        assert(k == c->sorted_entries);
  1161     } else {
  1162        for (i=0; i < c->sorted_entries; ++i)
  1163           c->sorted_codewords[i] = stbv_bit_reverse(c->codewords[i]);
  1164     }
  1165  
  1166     qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), stbv_uint32_compare);
  1167     c->sorted_codewords[c->sorted_entries] = 0xffffffff;
  1168  
  1169     len = c->sparse ? c->sorted_entries : c->entries;
  1170     // now we need to indicate how they correspond; we could either
  1171     //   #1: sort a different data structure that says who they correspond to
  1172     //   #2: for each sorted entry, search the original list to find who corresponds
  1173     //   #3: for each original entry, find the sorted entry
  1174     // #1 requires extra storage, #2 is slow, #3 can use binary search!
  1175     for (i=0; i < len; ++i) {
  1176        int huff_len = c->sparse ? lengths[values[i]] : lengths[i];
  1177        if (stbv_include_in_sort(c,huff_len)) {
  1178           stbv_uint32 code = stbv_bit_reverse(c->codewords[i]);
  1179           int x=0, n=c->sorted_entries;
  1180           while (n > 1) {
  1181              // invariant: sc[x] <= code < sc[x+n]
  1182              int m = x + (n >> 1);
  1183              if (c->sorted_codewords[m] <= code) {
  1184                 x = m;
  1185                 n -= (n>>1);
  1186              } else {
  1187                 n >>= 1;
  1188              }
  1189           }
  1190           assert(c->sorted_codewords[x] == code);
  1191           if (c->sparse) {
  1192              c->sorted_values[x] = values[i];
  1193              c->codeword_lengths[x] = huff_len;
  1194           } else {
  1195              c->sorted_values[x] = i;
  1196           }
  1197        }
  1198     }
  1199  }
  1200  
  1201  // only run while parsing the header (3 times)
  1202  static int stbv_vorbis_validate(stbv_uint8 *data)
  1203  {
  1204     static stbv_uint8 vorbis[6] = { 'v', 'o', 'r', 'b', 'i', 's' };
  1205     return memcmp(data, vorbis, 6) == 0;
  1206  }
  1207  
  1208  // called from setup only, once per code book
  1209  // (formula implied by specification)
  1210  static int stbv_lookup1_values(int entries, int dim)
  1211  {
  1212     int r = (int) floor(exp((float) log((float) entries) / dim));
  1213     if ((int) floor(pow((float) r+1, dim)) <= entries)   // (int) cast for MinGW warning;
  1214        ++r;                                              // floor() to avoid _ftol() when non-CRT
  1215     assert(pow((float) r+1, dim) > entries);
  1216     assert((int) floor(pow((float) r, dim)) <= entries); // (int),floor() as above
  1217     return r;
  1218  }
  1219  
  1220  // called twice per file
  1221  static void stbv_compute_twiddle_factors(int n, float *A, float *B, float *C)
  1222  {
  1223     int n4 = n >> 2, n8 = n >> 3;
  1224     int k,k2;
  1225  
  1226     for (k=k2=0; k < n4; ++k,k2+=2) {
  1227        A[k2  ] = (float)  cos(4*k*M_PI/n);
  1228        A[k2+1] = (float) -sin(4*k*M_PI/n);
  1229        B[k2  ] = (float)  cos((k2+1)*M_PI/n/2) * 0.5f;
  1230        B[k2+1] = (float)  sin((k2+1)*M_PI/n/2) * 0.5f;
  1231     }
  1232     for (k=k2=0; k < n8; ++k,k2+=2) {
  1233        C[k2  ] = (float)  cos(2*(k2+1)*M_PI/n);
  1234        C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);
  1235     }
  1236  }
  1237  
  1238  static void stbv_compute_window(int n, float *window)
  1239  {
  1240     int n2 = n >> 1, i;
  1241     for (i=0; i < n2; ++i)
  1242        window[i] = (float) sin(0.5 * M_PI * stbv_square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));
  1243  }
  1244  
  1245  static void stbv_compute_bitreverse(int n, stbv_uint16 *rev)
  1246  {
  1247     int ld = stbv_ilog(n) - 1; // stbv_ilog is off-by-one from normal definitions
  1248     int i, n8 = n >> 3;
  1249     for (i=0; i < n8; ++i)
  1250        rev[i] = (stbv_bit_reverse(i) >> (32-ld+3)) << 2;
  1251  }
  1252  
  1253  static int stbv_init_blocksize(stbv_vorb *f, int b, int n)
  1254  {
  1255     int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;
  1256     f->A[b] = (float *) stbv_setup_malloc(f, sizeof(float) * n2);
  1257     f->B[b] = (float *) stbv_setup_malloc(f, sizeof(float) * n2);
  1258     f->C[b] = (float *) stbv_setup_malloc(f, sizeof(float) * n4);
  1259     if (!f->A[b] || !f->B[b] || !f->C[b]) return stbv_error(f, VORBIS_outofmem);
  1260     stbv_compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);
  1261     f->window[b] = (float *) stbv_setup_malloc(f, sizeof(float) * n2);
  1262     if (!f->window[b]) return stbv_error(f, VORBIS_outofmem);
  1263     stbv_compute_window(n, f->window[b]);
  1264     f->stbv_bit_reverse[b] = (stbv_uint16 *) stbv_setup_malloc(f, sizeof(stbv_uint16) * n8);
  1265     if (!f->stbv_bit_reverse[b]) return stbv_error(f, VORBIS_outofmem);
  1266     stbv_compute_bitreverse(n, f->stbv_bit_reverse[b]);
  1267     return TRUE;
  1268  }
  1269  
  1270  static void stbv_neighbors(stbv_uint16 *x, int n, int *plow, int *phigh)
  1271  {
  1272     int low = -1;
  1273     int high = 65536;
  1274     int i;
  1275     for (i=0; i < n; ++i) {
  1276        if (x[i] > low  && x[i] < x[n]) { *plow  = i; low = x[i]; }
  1277        if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }
  1278     }
  1279  }
  1280  
  1281  // this has been repurposed so y is now the original index instead of y
  1282  typedef struct
  1283  {
  1284     stbv_uint16 x,id;
  1285  } stbv_floor_ordering;
  1286  
  1287  static int STBV_CDECL stbv_point_compare(const void *p, const void *q)
  1288  {
  1289     stbv_floor_ordering *a = (stbv_floor_ordering *) p;
  1290     stbv_floor_ordering *b = (stbv_floor_ordering *) q;
  1291     return a->x < b->x ? -1 : a->x > b->x;
  1292  }
  1293  
  1294  //
  1295  /////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////
  1296  
  1297  
  1298  #if defined(STB_VORBIS_NO_STDIO)
  1299     #define STBV_USE_MEMORY(z)    TRUE
  1300  #else
  1301     #define STBV_USE_MEMORY(z)    ((z)->stream)
  1302  #endif
  1303  
  1304  static stbv_uint8 stbv_get8(stbv_vorb *z)
  1305  {
  1306     if (STBV_USE_MEMORY(z)) {
  1307        if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
  1308        return *z->stream++;
  1309     }
  1310  
  1311     #ifndef STB_VORBIS_NO_STDIO
  1312     {
  1313     int c = fgetc(z->f);
  1314     if (c == EOF) { z->eof = TRUE; return 0; }
  1315     return c;
  1316     }
  1317     #endif
  1318  }
  1319  
  1320  static stbv_uint32 stbv_get32(stbv_vorb *f)
  1321  {
  1322     stbv_uint32 x;
  1323     x = stbv_get8(f);
  1324     x += stbv_get8(f) << 8;
  1325     x += stbv_get8(f) << 16;
  1326     x += (stbv_uint32) stbv_get8(f) << 24;
  1327     return x;
  1328  }
  1329  
  1330  static int stbv_getn(stbv_vorb *z, stbv_uint8 *data, int n)
  1331  {
  1332     if (STBV_USE_MEMORY(z)) {
  1333        if (z->stream+n > z->stream_end) { z->eof = 1; return 0; }
  1334        memcpy(data, z->stream, n);
  1335        z->stream += n;
  1336        return 1;
  1337     }
  1338  
  1339     #ifndef STB_VORBIS_NO_STDIO   
  1340     if (fread(data, n, 1, z->f) == 1)
  1341        return 1;
  1342     else {
  1343        z->eof = 1;
  1344        return 0;
  1345     }
  1346     #endif
  1347  }
  1348  
  1349  static void stbv_skip(stbv_vorb *z, int n)
  1350  {
  1351     if (STBV_USE_MEMORY(z)) {
  1352        z->stream += n;
  1353        if (z->stream >= z->stream_end) z->eof = 1;
  1354        return;
  1355     }
  1356     #ifndef STB_VORBIS_NO_STDIO
  1357     {
  1358        long x = ftell(z->f);
  1359        fseek(z->f, x+n, SEEK_SET);
  1360     }
  1361     #endif
  1362  }
  1363  
  1364  static int stbv_set_file_offset(stb_vorbis *f, unsigned int loc)
  1365  {
  1366     #ifndef STB_VORBIS_NO_PUSHDATA_API
  1367     if (f->push_mode) return 0;
  1368     #endif
  1369     f->eof = 0;
  1370     if (STBV_USE_MEMORY(f)) {
  1371        if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
  1372           f->stream = f->stream_end;
  1373           f->eof = 1;
  1374           return 0;
  1375        } else {
  1376           f->stream = f->stream_start + loc;
  1377           return 1;
  1378        }
  1379     }
  1380     #ifndef STB_VORBIS_NO_STDIO
  1381     if (loc + f->f_start < loc || loc >= 0x80000000) {
  1382        loc = 0x7fffffff;
  1383        f->eof = 1;
  1384     } else {
  1385        loc += f->f_start;
  1386     }
  1387     if (!fseek(f->f, loc, SEEK_SET))
  1388        return 1;
  1389     f->eof = 1;
  1390     fseek(f->f, f->f_start, SEEK_END);
  1391     return 0;
  1392     #endif
  1393  }
  1394  
  1395  
  1396  static stbv_uint8 stbv_ogg_page_header[4] = { 0x4f, 0x67, 0x67, 0x53 };
  1397  
  1398  static int stbv_capture_pattern(stbv_vorb *f)
  1399  {
  1400     if (0x4f != stbv_get8(f)) return FALSE;
  1401     if (0x67 != stbv_get8(f)) return FALSE;
  1402     if (0x67 != stbv_get8(f)) return FALSE;
  1403     if (0x53 != stbv_get8(f)) return FALSE;
  1404     return TRUE;
  1405  }
  1406  
  1407  #define STBV_PAGEFLAG_continued_packet   1
  1408  #define STBV_PAGEFLAG_first_page         2
  1409  #define STBV_PAGEFLAG_last_page          4
  1410  
  1411  static int stbv_start_page_no_capturepattern(stbv_vorb *f)
  1412  {
  1413     stbv_uint32 loc0,loc1,n;
  1414     // stream structure version
  1415     if (0 != stbv_get8(f)) return stbv_error(f, VORBIS_invalid_stream_structure_version);
  1416     // header flag
  1417     f->page_flag = stbv_get8(f);
  1418     // absolute granule position
  1419     loc0 = stbv_get32(f); 
  1420     loc1 = stbv_get32(f);
  1421     // @TODO: validate loc0,loc1 as valid positions?
  1422     // stream serial number -- vorbis doesn't interleave, so discard
  1423     stbv_get32(f);
  1424     //if (f->serial != stbv_get32(f)) return stbv_error(f, VORBIS_incorrect_stream_serial_number);
  1425     // page sequence number
  1426     n = stbv_get32(f);
  1427     f->last_page = n;
  1428     // CRC32
  1429     stbv_get32(f);
  1430     // page_segments
  1431     f->segment_count = stbv_get8(f);
  1432     if (!stbv_getn(f, f->segments, f->segment_count))
  1433        return stbv_error(f, VORBIS_unexpected_eof);
  1434     // assume we _don't_ know any the sample position of any segments
  1435     f->end_seg_with_known_loc = -2;
  1436     if (loc0 != ~0U || loc1 != ~0U) {
  1437        int i;
  1438        // determine which packet is the last one that will complete
  1439        for (i=f->segment_count-1; i >= 0; --i)
  1440           if (f->segments[i] < 255)
  1441              break;
  1442        // 'i' is now the index of the _last_ segment of a packet that ends
  1443        if (i >= 0) {
  1444           f->end_seg_with_known_loc = i;
  1445           f->known_loc_for_packet   = loc0;
  1446        }
  1447     }
  1448     if (f->first_decode) {
  1449        int i,len;
  1450        StbvProbedPage p;
  1451        len = 0;
  1452        for (i=0; i < f->segment_count; ++i)
  1453           len += f->segments[i];
  1454        len += 27 + f->segment_count;
  1455        p.page_start = f->first_audio_page_offset;
  1456        p.page_end = p.page_start + len;
  1457        p.last_decoded_sample = loc0;
  1458        f->p_first = p;
  1459     }
  1460     f->next_seg = 0;
  1461     return TRUE;
  1462  }
  1463  
  1464  static int stbv_start_page(stbv_vorb *f)
  1465  {
  1466     if (!stbv_capture_pattern(f)) return stbv_error(f, VORBIS_missing_capture_pattern);
  1467     return stbv_start_page_no_capturepattern(f);
  1468  }
  1469  
  1470  static int stbv_start_packet(stbv_vorb *f)
  1471  {
  1472     while (f->next_seg == -1) {
  1473        if (!stbv_start_page(f)) return FALSE;
  1474        if (f->page_flag & STBV_PAGEFLAG_continued_packet)
  1475           return stbv_error(f, VORBIS_continued_packet_flag_invalid);
  1476     }
  1477     f->last_seg = FALSE;
  1478     f->valid_bits = 0;
  1479     f->packet_bytes = 0;
  1480     f->bytes_in_seg = 0;
  1481     // f->next_seg is now valid
  1482     return TRUE;
  1483  }
  1484  
  1485  static int stbv_maybe_start_packet(stbv_vorb *f)
  1486  {
  1487     if (f->next_seg == -1) {
  1488        int x = stbv_get8(f);
  1489        if (f->eof) return FALSE; // EOF at page boundary is not an error!
  1490        if (0x4f != x      ) return stbv_error(f, VORBIS_missing_capture_pattern);
  1491        if (0x67 != stbv_get8(f)) return stbv_error(f, VORBIS_missing_capture_pattern);
  1492        if (0x67 != stbv_get8(f)) return stbv_error(f, VORBIS_missing_capture_pattern);
  1493        if (0x53 != stbv_get8(f)) return stbv_error(f, VORBIS_missing_capture_pattern);
  1494        if (!stbv_start_page_no_capturepattern(f)) return FALSE;
  1495        if (f->page_flag & STBV_PAGEFLAG_continued_packet) {
  1496           // set up enough state that we can read this packet if we want,
  1497           // e.g. during recovery
  1498           f->last_seg = FALSE;
  1499           f->bytes_in_seg = 0;
  1500           return stbv_error(f, VORBIS_continued_packet_flag_invalid);
  1501        }
  1502     }
  1503     return stbv_start_packet(f);
  1504  }
  1505  
  1506  static int stbv_next_segment(stbv_vorb *f)
  1507  {
  1508     int len;
  1509     if (f->last_seg) return 0;
  1510     if (f->next_seg == -1) {
  1511        f->last_seg_which = f->segment_count-1; // in case stbv_start_page fails
  1512        if (!stbv_start_page(f)) { f->last_seg = 1; return 0; }
  1513        if (!(f->page_flag & STBV_PAGEFLAG_continued_packet)) return stbv_error(f, VORBIS_continued_packet_flag_invalid);
  1514     }
  1515     len = f->segments[f->next_seg++];
  1516     if (len < 255) {
  1517        f->last_seg = TRUE;
  1518        f->last_seg_which = f->next_seg-1;
  1519     }
  1520     if (f->next_seg >= f->segment_count)
  1521        f->next_seg = -1;
  1522     assert(f->bytes_in_seg == 0);
  1523     f->bytes_in_seg = len;
  1524     return len;
  1525  }
  1526  
  1527  #define STBV_EOP    (-1)
  1528  #define STBV_INVALID_BITS  (-1)
  1529  
  1530  static int stbv_get8_packet_raw(stbv_vorb *f)
  1531  {
  1532     if (!f->bytes_in_seg) {  // CLANG!
  1533        if (f->last_seg) return STBV_EOP;
  1534        else if (!stbv_next_segment(f)) return STBV_EOP;
  1535     }
  1536     assert(f->bytes_in_seg > 0);
  1537     --f->bytes_in_seg;
  1538     ++f->packet_bytes;
  1539     return stbv_get8(f);
  1540  }
  1541  
  1542  static int stbv_get8_packet(stbv_vorb *f)
  1543  {
  1544     int x = stbv_get8_packet_raw(f);
  1545     f->valid_bits = 0;
  1546     return x;
  1547  }
  1548  
  1549  static void stbv_flush_packet(stbv_vorb *f)
  1550  {
  1551     while (stbv_get8_packet_raw(f) != STBV_EOP);
  1552  }
  1553  
  1554  // @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important
  1555  // as the huffman decoder?
  1556  static stbv_uint32 stbv_get_bits(stbv_vorb *f, int n)
  1557  {
  1558     stbv_uint32 z;
  1559  
  1560     if (f->valid_bits < 0) return 0;
  1561     if (f->valid_bits < n) {
  1562        if (n > 24) {
  1563           // the accumulator technique below would not work correctly in this case
  1564           z = stbv_get_bits(f, 24);
  1565           z += stbv_get_bits(f, n-24) << 24;
  1566           return z;
  1567        }
  1568        if (f->valid_bits == 0) f->acc = 0;
  1569        while (f->valid_bits < n) {
  1570           int z = stbv_get8_packet_raw(f);
  1571           if (z == STBV_EOP) {
  1572              f->valid_bits = STBV_INVALID_BITS;
  1573              return 0;
  1574           }
  1575           f->acc += z << f->valid_bits;
  1576           f->valid_bits += 8;
  1577        }
  1578     }
  1579     if (f->valid_bits < 0) return 0;
  1580     z = f->acc & ((1 << n)-1);
  1581     f->acc >>= n;
  1582     f->valid_bits -= n;
  1583     return z;
  1584  }
  1585  
  1586  // @OPTIMIZE: primary accumulator for huffman
  1587  // expand the buffer to as many bits as possible without reading off end of packet
  1588  // it might be nice to allow f->valid_bits and f->acc to be stored in registers,
  1589  // e.g. cache them locally and decode locally
  1590  static __forceinline void stbv_prep_huffman(stbv_vorb *f)
  1591  {
  1592     if (f->valid_bits <= 24) {
  1593        if (f->valid_bits == 0) f->acc = 0;
  1594        do {
  1595           int z;
  1596           if (f->last_seg && !f->bytes_in_seg) return;
  1597           z = stbv_get8_packet_raw(f);
  1598           if (z == STBV_EOP) return;
  1599           f->acc += (unsigned) z << f->valid_bits;
  1600           f->valid_bits += 8;
  1601        } while (f->valid_bits <= 24);
  1602     }
  1603  }
  1604  
  1605  enum
  1606  {
  1607     STBV_VORBIS_packet_id = 1,
  1608     STBV_VORBIS_packet_comment = 3,
  1609     STBV_VORBIS_packet_setup = 5
  1610  };
  1611  
  1612  static int stbv_codebook_decode_scalar_raw(stbv_vorb *f, StbvCodebook *c)
  1613  {
  1614     int i;
  1615     stbv_prep_huffman(f);
  1616  
  1617     if (c->codewords == NULL && c->sorted_codewords == NULL)
  1618        return -1;
  1619  
  1620     // cases to use binary search: sorted_codewords && !c->codewords
  1621     //                             sorted_codewords && c->entries > 8
  1622     if (c->entries > 8 ? c->sorted_codewords!=NULL : !c->codewords) {
  1623        // binary search
  1624        stbv_uint32 code = stbv_bit_reverse(f->acc);
  1625        int x=0, n=c->sorted_entries, len;
  1626  
  1627        while (n > 1) {
  1628           // invariant: sc[x] <= code < sc[x+n]
  1629           int m = x + (n >> 1);
  1630           if (c->sorted_codewords[m] <= code) {
  1631              x = m;
  1632              n -= (n>>1);
  1633           } else {
  1634              n >>= 1;
  1635           }
  1636        }
  1637        // x is now the sorted index
  1638        if (!c->sparse) x = c->sorted_values[x];
  1639        // x is now sorted index if sparse, or symbol otherwise
  1640        len = c->codeword_lengths[x];
  1641        if (f->valid_bits >= len) {
  1642           f->acc >>= len;
  1643           f->valid_bits -= len;
  1644           return x;
  1645        }
  1646  
  1647        f->valid_bits = 0;
  1648        return -1;
  1649     }
  1650  
  1651     // if small, linear search
  1652     assert(!c->sparse);
  1653     for (i=0; i < c->entries; ++i) {
  1654        if (c->codeword_lengths[i] == NO_CODE) continue;
  1655        if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i])-1))) {
  1656           if (f->valid_bits >= c->codeword_lengths[i]) {
  1657              f->acc >>= c->codeword_lengths[i];
  1658              f->valid_bits -= c->codeword_lengths[i];
  1659              return i;
  1660           }
  1661           f->valid_bits = 0;
  1662           return -1;
  1663        }
  1664     }
  1665  
  1666     stbv_error(f, VORBIS_invalid_stream);
  1667     f->valid_bits = 0;
  1668     return -1;
  1669  }
  1670  
  1671  #ifndef STB_VORBIS_NO_INLINE_DECODE
  1672  
  1673  #define STBV_DECODE_RAW(var, f,c)                                  \
  1674     if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)        \
  1675        stbv_prep_huffman(f);                                        \
  1676     var = f->acc & STBV_FAST_HUFFMAN_TABLE_MASK;                    \
  1677     var = c->fast_huffman[var];                                \
  1678     if (var >= 0) {                                            \
  1679        int n = c->codeword_lengths[var];                       \
  1680        f->acc >>= n;                                           \
  1681        f->valid_bits -= n;                                     \
  1682        if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
  1683     } else {                                                   \
  1684        var = stbv_codebook_decode_scalar_raw(f,c);                  \
  1685     }
  1686  
  1687  #else
  1688  
  1689  static int stbv_codebook_decode_scalar(stbv_vorb *f, StbvCodebook *c)
  1690  {
  1691     int i;
  1692     if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)
  1693        stbv_prep_huffman(f);
  1694     // fast huffman table lookup
  1695     i = f->acc & STBV_FAST_HUFFMAN_TABLE_MASK;
  1696     i = c->fast_huffman[i];
  1697     if (i >= 0) {
  1698        f->acc >>= c->codeword_lengths[i];
  1699        f->valid_bits -= c->codeword_lengths[i];
  1700        if (f->valid_bits < 0) { f->valid_bits = 0; return -1; }
  1701        return i;
  1702     }
  1703     return stbv_codebook_decode_scalar_raw(f,c);
  1704  }
  1705  
  1706  #define STBV_DECODE_RAW(var,f,c)    var = stbv_codebook_decode_scalar(f,c);
  1707  
  1708  #endif
  1709  
  1710  #define STBV_DECODE(var,f,c)                                       \
  1711     STBV_DECODE_RAW(var,f,c)                                        \
  1712     if (c->sparse) var = c->sorted_values[var];
  1713  
  1714  #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1715    #define DECODE_VQ(var,f,c)   STBV_DECODE_RAW(var,f,c)
  1716  #else
  1717    #define DECODE_VQ(var,f,c)   STBV_DECODE(var,f,c)
  1718  #endif
  1719  
  1720  
  1721  
  1722  
  1723  
  1724  
  1725  // STBV_CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case
  1726  // where we avoid one addition
  1727  #define STBV_CODEBOOK_ELEMENT(c,off)          (c->multiplicands[off])
  1728  #define STBV_CODEBOOK_ELEMENT_FAST(c,off)     (c->multiplicands[off])
  1729  #define STBV_CODEBOOK_ELEMENT_BASE(c)         (0)
  1730  
  1731  static int stbv_codebook_decode_start(stbv_vorb *f, StbvCodebook *c)
  1732  {
  1733     int z = -1;
  1734  
  1735     // type 0 is only legal in a scalar context
  1736     if (c->lookup_type == 0)
  1737        stbv_error(f, VORBIS_invalid_stream);
  1738     else {
  1739        DECODE_VQ(z,f,c);
  1740        if (c->sparse) assert(z < c->sorted_entries);
  1741        if (z < 0) {  // check for STBV_EOP
  1742           if (!f->bytes_in_seg)
  1743              if (f->last_seg)
  1744                 return z;
  1745           stbv_error(f, VORBIS_invalid_stream);
  1746        }
  1747     }
  1748     return z;
  1749  }
  1750  
  1751  static int stbv_codebook_decode(stbv_vorb *f, StbvCodebook *c, float *output, int len)
  1752  {
  1753     int i,z = stbv_codebook_decode_start(f,c);
  1754     if (z < 0) return FALSE;
  1755     if (len > c->dimensions) len = c->dimensions;
  1756  
  1757  #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1758     if (c->lookup_type == 1) {
  1759        float last = STBV_CODEBOOK_ELEMENT_BASE(c);
  1760        int div = 1;
  1761        for (i=0; i < len; ++i) {
  1762           int off = (z / div) % c->lookup_values;
  1763           float val = STBV_CODEBOOK_ELEMENT_FAST(c,off) + last;
  1764           output[i] += val;
  1765           if (c->sequence_p) last = val + c->minimum_value;
  1766           div *= c->lookup_values;
  1767        }
  1768        return TRUE;
  1769     }
  1770  #endif
  1771  
  1772     z *= c->dimensions;
  1773     if (c->sequence_p) {
  1774        float last = STBV_CODEBOOK_ELEMENT_BASE(c);
  1775        for (i=0; i < len; ++i) {
  1776           float val = STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1777           output[i] += val;
  1778           last = val + c->minimum_value;
  1779        }
  1780     } else {
  1781        float last = STBV_CODEBOOK_ELEMENT_BASE(c);
  1782        for (i=0; i < len; ++i) {
  1783           output[i] += STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1784        }
  1785     }
  1786  
  1787     return TRUE;
  1788  }
  1789  
  1790  static int stbv_codebook_decode_step(stbv_vorb *f, StbvCodebook *c, float *output, int len, int step)
  1791  {
  1792     int i,z = stbv_codebook_decode_start(f,c);
  1793     float last = STBV_CODEBOOK_ELEMENT_BASE(c);
  1794     if (z < 0) return FALSE;
  1795     if (len > c->dimensions) len = c->dimensions;
  1796  
  1797  #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1798     if (c->lookup_type == 1) {
  1799        int div = 1;
  1800        for (i=0; i < len; ++i) {
  1801           int off = (z / div) % c->lookup_values;
  1802           float val = STBV_CODEBOOK_ELEMENT_FAST(c,off) + last;
  1803           output[i*step] += val;
  1804           if (c->sequence_p) last = val;
  1805           div *= c->lookup_values;
  1806        }
  1807        return TRUE;
  1808     }
  1809  #endif
  1810  
  1811     z *= c->dimensions;
  1812     for (i=0; i < len; ++i) {
  1813        float val = STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1814        output[i*step] += val;
  1815        if (c->sequence_p) last = val;
  1816     }
  1817  
  1818     return TRUE;
  1819  }
  1820  
  1821  static int stbv_codebook_decode_deinterleave_repeat(stbv_vorb *f, StbvCodebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode)
  1822  {
  1823     int c_inter = *c_inter_p;
  1824     int p_inter = *p_inter_p;
  1825     int i,z, effective = c->dimensions;
  1826  
  1827     // type 0 is only legal in a scalar context
  1828     if (c->lookup_type == 0)   return stbv_error(f, VORBIS_invalid_stream);
  1829  
  1830     while (total_decode > 0) {
  1831        float last = STBV_CODEBOOK_ELEMENT_BASE(c);
  1832        DECODE_VQ(z,f,c);
  1833        #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1834        assert(!c->sparse || z < c->sorted_entries);
  1835        #endif
  1836        if (z < 0) {
  1837           if (!f->bytes_in_seg)
  1838              if (f->last_seg) return FALSE;
  1839           return stbv_error(f, VORBIS_invalid_stream);
  1840        }
  1841  
  1842        // if this will take us off the end of the buffers, stop short!
  1843        // we check by computing the length of the virtual interleaved
  1844        // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
  1845        // and the length we'll be using (effective)
  1846        if (c_inter + p_inter*ch + effective > len * ch) {
  1847           effective = len*ch - (p_inter*ch - c_inter);
  1848        }
  1849  
  1850     #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  1851        if (c->lookup_type == 1) {
  1852           int div = 1;
  1853           for (i=0; i < effective; ++i) {
  1854              int off = (z / div) % c->lookup_values;
  1855              float val = STBV_CODEBOOK_ELEMENT_FAST(c,off) + last;
  1856              if (outputs[c_inter])
  1857                 outputs[c_inter][p_inter] += val;
  1858              if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1859              if (c->sequence_p) last = val;
  1860              div *= c->lookup_values;
  1861           }
  1862        } else
  1863     #endif
  1864        {
  1865           z *= c->dimensions;
  1866           if (c->sequence_p) {
  1867              for (i=0; i < effective; ++i) {
  1868                 float val = STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1869                 if (outputs[c_inter])
  1870                    outputs[c_inter][p_inter] += val;
  1871                 if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1872                 last = val;
  1873              }
  1874           } else {
  1875              for (i=0; i < effective; ++i) {
  1876                 float val = STBV_CODEBOOK_ELEMENT_FAST(c,z+i) + last;
  1877                 if (outputs[c_inter])
  1878                    outputs[c_inter][p_inter] += val;
  1879                 if (++c_inter == ch) { c_inter = 0; ++p_inter; }
  1880              }
  1881           }
  1882        }
  1883  
  1884        total_decode -= effective;
  1885     }
  1886     *c_inter_p = c_inter;
  1887     *p_inter_p = p_inter;
  1888     return TRUE;
  1889  }
  1890  
  1891  static int stbv_predict_point(int x, int x0, int x1, int y0, int y1)
  1892  {
  1893     int dy = y1 - y0;
  1894     int adx = x1 - x0;
  1895     // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?
  1896     int err = abs(dy) * (x - x0);
  1897     int off = err / adx;
  1898     return dy < 0 ? y0 - off : y0 + off;
  1899  }
  1900  
  1901  // the following table is block-copied from the specification
  1902  static float stbv_inverse_db_table[256] =
  1903  {
  1904    1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f, 
  1905    1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f, 
  1906    1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f, 
  1907    2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f, 
  1908    2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f, 
  1909    3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f, 
  1910    4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f, 
  1911    6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f, 
  1912    7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f, 
  1913    1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f, 
  1914    1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f, 
  1915    1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f, 
  1916    2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f, 
  1917    2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f, 
  1918    3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f, 
  1919    4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f, 
  1920    5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f, 
  1921    7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f, 
  1922    9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f, 
  1923    1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f, 
  1924    1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f, 
  1925    2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f, 
  1926    2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f, 
  1927    3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f, 
  1928    4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f, 
  1929    5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f, 
  1930    7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f, 
  1931    9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f, 
  1932    0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f, 
  1933    0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f, 
  1934    0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f, 
  1935    0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f, 
  1936    0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f, 
  1937    0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f, 
  1938    0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f, 
  1939    0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f, 
  1940    0.00092223983f, 0.00098217216f, 0.0010459992f,  0.0011139742f, 
  1941    0.0011863665f,  0.0012634633f,  0.0013455702f,  0.0014330129f, 
  1942    0.0015261382f,  0.0016253153f,  0.0017309374f,  0.0018434235f, 
  1943    0.0019632195f,  0.0020908006f,  0.0022266726f,  0.0023713743f, 
  1944    0.0025254795f,  0.0026895994f,  0.0028643847f,  0.0030505286f, 
  1945    0.0032487691f,  0.0034598925f,  0.0036847358f,  0.0039241906f, 
  1946    0.0041792066f,  0.0044507950f,  0.0047400328f,  0.0050480668f, 
  1947    0.0053761186f,  0.0057254891f,  0.0060975636f,  0.0064938176f, 
  1948    0.0069158225f,  0.0073652516f,  0.0078438871f,  0.0083536271f, 
  1949    0.0088964928f,  0.009474637f,   0.010090352f,   0.010746080f, 
  1950    0.011444421f,   0.012188144f,   0.012980198f,   0.013823725f, 
  1951    0.014722068f,   0.015678791f,   0.016697687f,   0.017782797f, 
  1952    0.018938423f,   0.020169149f,   0.021479854f,   0.022875735f, 
  1953    0.024362330f,   0.025945531f,   0.027631618f,   0.029427276f, 
  1954    0.031339626f,   0.033376252f,   0.035545228f,   0.037855157f, 
  1955    0.040315199f,   0.042935108f,   0.045725273f,   0.048696758f, 
  1956    0.051861348f,   0.055231591f,   0.058820850f,   0.062643361f, 
  1957    0.066714279f,   0.071049749f,   0.075666962f,   0.080584227f, 
  1958    0.085821044f,   0.091398179f,   0.097337747f,   0.10366330f, 
  1959    0.11039993f,    0.11757434f,    0.12521498f,    0.13335215f, 
  1960    0.14201813f,    0.15124727f,    0.16107617f,    0.17154380f, 
  1961    0.18269168f,    0.19456402f,    0.20720788f,    0.22067342f, 
  1962    0.23501402f,    0.25028656f,    0.26655159f,    0.28387361f, 
  1963    0.30232132f,    0.32196786f,    0.34289114f,    0.36517414f, 
  1964    0.38890521f,    0.41417847f,    0.44109412f,    0.46975890f, 
  1965    0.50028648f,    0.53279791f,    0.56742212f,    0.60429640f, 
  1966    0.64356699f,    0.68538959f,    0.72993007f,    0.77736504f, 
  1967    0.82788260f,    0.88168307f,    0.9389798f,     1.0f
  1968  };
  1969  
  1970  
  1971  // @OPTIMIZE: if you want to replace this bresenham line-drawing routine,
  1972  // note that you must produce bit-identical output to decode correctly;
  1973  // this specific sequence of operations is specified in the spec (it's
  1974  // drawing integer-quantized frequency-space lines that the encoder
  1975  // expects to be exactly the same)
  1976  //     ... also, isn't the whole point of Bresenham's algorithm to NOT
  1977  // have to divide in the setup? sigh.
  1978  #ifndef STB_VORBIS_NO_DEFER_FLOOR
  1979  #define STBV_LINE_OP(a,b)   a *= b
  1980  #else
  1981  #define STBV_LINE_OP(a,b)   a = b
  1982  #endif
  1983  
  1984  #ifdef STB_VORBIS_DIVIDE_TABLE
  1985  #define STBV_DIVTAB_NUMER   32
  1986  #define STBV_DIVTAB_DENOM   64
  1987  stbv_int8 stbv_integer_divide_table[STBV_DIVTAB_NUMER][STBV_DIVTAB_DENOM]; // 2KB
  1988  #endif
  1989  
  1990  static __forceinline void stbv_draw_line(float *output, int x0, int y0, int x1, int y1, int n)
  1991  {
  1992     int dy = y1 - y0;
  1993     int adx = x1 - x0;
  1994     int ady = abs(dy);
  1995     int base;
  1996     int x=x0,y=y0;
  1997     int err = 0;
  1998     int sy;
  1999  
  2000  #ifdef STB_VORBIS_DIVIDE_TABLE
  2001     if (adx < STBV_DIVTAB_DENOM && ady < STBV_DIVTAB_NUMER) {
  2002        if (dy < 0) {
  2003           base = -stbv_integer_divide_table[ady][adx];
  2004           sy = base-1;
  2005        } else {
  2006           base =  stbv_integer_divide_table[ady][adx];
  2007           sy = base+1;
  2008        }
  2009     } else {
  2010        base = dy / adx;
  2011        if (dy < 0)
  2012           sy = base - 1;
  2013        else
  2014           sy = base+1;
  2015     }
  2016  #else
  2017     base = dy / adx;
  2018     if (dy < 0)
  2019        sy = base - 1;
  2020     else
  2021        sy = base+1;
  2022  #endif
  2023     ady -= abs(base) * adx;
  2024     if (x1 > n) x1 = n;
  2025     if (x < x1) {
  2026        STBV_LINE_OP(output[x], stbv_inverse_db_table[y]);
  2027        for (++x; x < x1; ++x) {
  2028           err += ady;
  2029           if (err >= adx) {
  2030              err -= adx;
  2031              y += sy;
  2032           } else
  2033              y += base;
  2034           STBV_LINE_OP(output[x], stbv_inverse_db_table[y]);
  2035        }
  2036     }
  2037  }
  2038  
  2039  static int stbv_residue_decode(stbv_vorb *f, StbvCodebook *book, float *target, int offset, int n, int rtype)
  2040  {
  2041     int k;
  2042     if (rtype == 0) {
  2043        int step = n / book->dimensions;
  2044        for (k=0; k < step; ++k)
  2045           if (!stbv_codebook_decode_step(f, book, target+offset+k, n-offset-k, step))
  2046              return FALSE;
  2047     } else {
  2048        for (k=0; k < n; ) {
  2049           if (!stbv_codebook_decode(f, book, target+offset, n-k))
  2050              return FALSE;
  2051           k += book->dimensions;
  2052           offset += book->dimensions;
  2053        }
  2054     }
  2055     return TRUE;
  2056  }
  2057  
  2058  // n is 1/2 of the blocksize --
  2059  // specification: "Correct per-vector decode length is [n]/2"
  2060  static void stbv_decode_residue(stbv_vorb *f, float *residue_buffers[], int ch, int n, int rn, stbv_uint8 *do_not_decode)
  2061  {
  2062     int i,j,pass;
  2063     StbvResidue *r = f->residue_config + rn;
  2064     int rtype = f->residue_types[rn];
  2065     int c = r->classbook;
  2066     int classwords = f->codebooks[c].dimensions;
  2067     unsigned int actual_size = rtype == 2 ? n*2 : n;
  2068     unsigned int limit_r_begin = (r->begin < actual_size ? r->begin : actual_size);
  2069     unsigned int limit_r_end   = (r->end   < actual_size ? r->end   : actual_size);
  2070     int n_read = limit_r_end - limit_r_begin;
  2071     int part_read = n_read / r->part_size;
  2072     int temp_alloc_point = stbv_temp_alloc_save(f);
  2073     #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2074     stbv_uint8 ***part_classdata = (stbv_uint8 ***) stbv_temp_block_array(f,f->channels, part_read * sizeof(**part_classdata));
  2075     #else
  2076     int **classifications = (int **) stbv_temp_block_array(f,f->channels, part_read * sizeof(**classifications));
  2077     #endif
  2078  
  2079     STBV_CHECK(f);
  2080  
  2081     for (i=0; i < ch; ++i)
  2082        if (!do_not_decode[i])
  2083           memset(residue_buffers[i], 0, sizeof(float) * n);
  2084  
  2085     if (rtype == 2 && ch != 1) {
  2086        for (j=0; j < ch; ++j)
  2087           if (!do_not_decode[j])
  2088              break;
  2089        if (j == ch)
  2090           goto done;
  2091  
  2092        for (pass=0; pass < 8; ++pass) {
  2093           int pcount = 0, class_set = 0;
  2094           if (ch == 2) {
  2095              while (pcount < part_read) {
  2096                 int z = r->begin + pcount*r->part_size;
  2097                 int c_inter = (z & 1), p_inter = z>>1;
  2098                 if (pass == 0) {
  2099                    StbvCodebook *c = f->codebooks+r->classbook;
  2100                    int q;
  2101                    STBV_DECODE(q,f,c);
  2102                    if (q == STBV_EOP) goto done;
  2103                    #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2104                    part_classdata[0][class_set] = r->classdata[q];
  2105                    #else
  2106                    for (i=classwords-1; i >= 0; --i) {
  2107                       classifications[0][i+pcount] = q % r->classifications;
  2108                       q /= r->classifications;
  2109                    }
  2110                    #endif
  2111                 }
  2112                 for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
  2113                    int z = r->begin + pcount*r->part_size;
  2114                    #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2115                    int c = part_classdata[0][class_set][i];
  2116                    #else
  2117                    int c = classifications[0][pcount];
  2118                    #endif
  2119                    int b = r->residue_books[c][pass];
  2120                    if (b >= 0) {
  2121                       StbvCodebook *book = f->codebooks + b;
  2122                       #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
  2123                       if (!stbv_codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
  2124                          goto done;
  2125                       #else
  2126                       // saves 1%
  2127                       if (!stbv_codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
  2128                          goto done;
  2129                       #endif
  2130                    } else {
  2131                       z += r->part_size;
  2132                       c_inter = z & 1;
  2133                       p_inter = z >> 1;
  2134                    }
  2135                 }
  2136                 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2137                 ++class_set;
  2138                 #endif
  2139              }
  2140           } else if (ch == 1) {
  2141              while (pcount < part_read) {
  2142                 int z = r->begin + pcount*r->part_size;
  2143                 int c_inter = 0, p_inter = z;
  2144                 if (pass == 0) {
  2145                    StbvCodebook *c = f->codebooks+r->classbook;
  2146                    int q;
  2147                    STBV_DECODE(q,f,c);
  2148                    if (q == STBV_EOP) goto done;
  2149                    #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2150                    part_classdata[0][class_set] = r->classdata[q];
  2151                    #else
  2152                    for (i=classwords-1; i >= 0; --i) {
  2153                       classifications[0][i+pcount] = q % r->classifications;
  2154                       q /= r->classifications;
  2155                    }
  2156                    #endif
  2157                 }
  2158                 for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
  2159                    int z = r->begin + pcount*r->part_size;
  2160                    #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2161                    int c = part_classdata[0][class_set][i];
  2162                    #else
  2163                    int c = classifications[0][pcount];
  2164                    #endif
  2165                    int b = r->residue_books[c][pass];
  2166                    if (b >= 0) {
  2167                       StbvCodebook *book = f->codebooks + b;
  2168                       if (!stbv_codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
  2169                          goto done;
  2170                    } else {
  2171                       z += r->part_size;
  2172                       c_inter = 0;
  2173                       p_inter = z;
  2174                    }
  2175                 }
  2176                 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2177                 ++class_set;
  2178                 #endif
  2179              }
  2180           } else {
  2181              while (pcount < part_read) {
  2182                 int z = r->begin + pcount*r->part_size;
  2183                 int c_inter = z % ch, p_inter = z/ch;
  2184                 if (pass == 0) {
  2185                    StbvCodebook *c = f->codebooks+r->classbook;
  2186                    int q;
  2187                    STBV_DECODE(q,f,c);
  2188                    if (q == STBV_EOP) goto done;
  2189                    #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2190                    part_classdata[0][class_set] = r->classdata[q];
  2191                    #else
  2192                    for (i=classwords-1; i >= 0; --i) {
  2193                       classifications[0][i+pcount] = q % r->classifications;
  2194                       q /= r->classifications;
  2195                    }
  2196                    #endif
  2197                 }
  2198                 for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
  2199                    int z = r->begin + pcount*r->part_size;
  2200                    #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2201                    int c = part_classdata[0][class_set][i];
  2202                    #else
  2203                    int c = classifications[0][pcount];
  2204                    #endif
  2205                    int b = r->residue_books[c][pass];
  2206                    if (b >= 0) {
  2207                       StbvCodebook *book = f->codebooks + b;
  2208                       if (!stbv_codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
  2209                          goto done;
  2210                    } else {
  2211                       z += r->part_size;
  2212                       c_inter = z % ch;
  2213                       p_inter = z / ch;
  2214                    }
  2215                 }
  2216                 #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2217                 ++class_set;
  2218                 #endif
  2219              }
  2220           }
  2221        }
  2222        goto done;
  2223     }
  2224     STBV_CHECK(f);
  2225  
  2226     for (pass=0; pass < 8; ++pass) {
  2227        int pcount = 0, class_set=0;
  2228        while (pcount < part_read) {
  2229           if (pass == 0) {
  2230              for (j=0; j < ch; ++j) {
  2231                 if (!do_not_decode[j]) {
  2232                    StbvCodebook *c = f->codebooks+r->classbook;
  2233                    int temp;
  2234                    STBV_DECODE(temp,f,c);
  2235                    if (temp == STBV_EOP) goto done;
  2236                    #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2237                    part_classdata[j][class_set] = r->classdata[temp];
  2238                    #else
  2239                    for (i=classwords-1; i >= 0; --i) {
  2240                       classifications[j][i+pcount] = temp % r->classifications;
  2241                       temp /= r->classifications;
  2242                    }
  2243                    #endif
  2244                 }
  2245              }
  2246           }
  2247           for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
  2248              for (j=0; j < ch; ++j) {
  2249                 if (!do_not_decode[j]) {
  2250                    #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2251                    int c = part_classdata[j][class_set][i];
  2252                    #else
  2253                    int c = classifications[j][pcount];
  2254                    #endif
  2255                    int b = r->residue_books[c][pass];
  2256                    if (b >= 0) {
  2257                       float *target = residue_buffers[j];
  2258                       int offset = r->begin + pcount * r->part_size;
  2259                       int n = r->part_size;
  2260                       StbvCodebook *book = f->codebooks + b;
  2261                       if (!stbv_residue_decode(f, book, target, offset, n, rtype))
  2262                          goto done;
  2263                    }
  2264                 }
  2265              }
  2266           }
  2267           #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2268           ++class_set;
  2269           #endif
  2270        }
  2271     }
  2272    done:
  2273     STBV_CHECK(f);
  2274     #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  2275     stbv_temp_free(f,part_classdata);
  2276     #else
  2277     stbv_temp_free(f,classifications);
  2278     #endif
  2279     stbv_temp_alloc_restore(f,temp_alloc_point);
  2280  }
  2281  
  2282  
  2283  #if 0
  2284  // slow way for debugging
  2285  void inverse_mdct_slow(float *buffer, int n)
  2286  {
  2287     int i,j;
  2288     int n2 = n >> 1;
  2289     float *x = (float *) malloc(sizeof(*x) * n2);
  2290     memcpy(x, buffer, sizeof(*x) * n2);
  2291     for (i=0; i < n; ++i) {
  2292        float acc = 0;
  2293        for (j=0; j < n2; ++j)
  2294           // formula from paper:
  2295           //acc += n/4.0f * x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1));
  2296           // formula from wikipedia
  2297           //acc += 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
  2298           // these are equivalent, except the formula from the paper inverts the multiplier!
  2299           // however, what actually works is NO MULTIPLIER!?!
  2300           //acc += 64 * 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
  2301           acc += x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1));
  2302        buffer[i] = acc;
  2303     }
  2304     free(x);
  2305  }
  2306  #elif 0
  2307  // same as above, but just barely able to run in real time on modern machines
  2308  void inverse_mdct_slow(float *buffer, int n, stbv_vorb *f, int blocktype)
  2309  {
  2310     float mcos[16384];
  2311     int i,j;
  2312     int n2 = n >> 1, nmask = (n << 2) -1;
  2313     float *x = (float *) malloc(sizeof(*x) * n2);
  2314     memcpy(x, buffer, sizeof(*x) * n2);
  2315     for (i=0; i < 4*n; ++i)
  2316        mcos[i] = (float) cos(M_PI / 2 * i / n);
  2317  
  2318     for (i=0; i < n; ++i) {
  2319        float acc = 0;
  2320        for (j=0; j < n2; ++j)
  2321           acc += x[j] * mcos[(2 * i + 1 + n2)*(2*j+1) & nmask];
  2322        buffer[i] = acc;
  2323     }
  2324     free(x);
  2325  }
  2326  #elif 0
  2327  // transform to use a slow dct-iv; this is STILL basically trivial,
  2328  // but only requires half as many ops
  2329  void dct_iv_slow(float *buffer, int n)
  2330  {
  2331     float mcos[16384];
  2332     float x[2048];
  2333     int i,j;
  2334     int n2 = n >> 1, nmask = (n << 3) - 1;
  2335     memcpy(x, buffer, sizeof(*x) * n);
  2336     for (i=0; i < 8*n; ++i)
  2337        mcos[i] = (float) cos(M_PI / 4 * i / n);
  2338     for (i=0; i < n; ++i) {
  2339        float acc = 0;
  2340        for (j=0; j < n; ++j)
  2341           acc += x[j] * mcos[((2 * i + 1)*(2*j+1)) & nmask];
  2342        buffer[i] = acc;
  2343     }
  2344  }
  2345  
  2346  void inverse_mdct_slow(float *buffer, int n, stbv_vorb *f, int blocktype)
  2347  {
  2348     int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4;
  2349     float temp[4096];
  2350  
  2351     memcpy(temp, buffer, n2 * sizeof(float));
  2352     dct_iv_slow(temp, n2);  // returns -c'-d, a-b'
  2353  
  2354     for (i=0; i < n4  ; ++i) buffer[i] = temp[i+n4];            // a-b'
  2355     for (   ; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1];   // b-a', c+d'
  2356     for (   ; i < n   ; ++i) buffer[i] = -temp[i - n3_4];       // c'+d
  2357  }
  2358  #endif
  2359  
  2360  #ifndef LIBVORBIS_MDCT
  2361  #define LIBVORBIS_MDCT 0
  2362  #endif
  2363  
  2364  #if LIBVORBIS_MDCT
  2365  // directly call the vorbis MDCT using an interface documented
  2366  // by Jeff Roberts... useful for performance comparison
  2367  typedef struct 
  2368  {
  2369    int n;
  2370    int log2n;
  2371    
  2372    float *trig;
  2373    int   *bitrev;
  2374  
  2375    float scale;
  2376  } mdct_lookup;
  2377  
  2378  extern void mdct_init(mdct_lookup *lookup, int n);
  2379  extern void mdct_clear(mdct_lookup *l);
  2380  extern void mdct_backward(mdct_lookup *init, float *in, float *out);
  2381  
  2382  mdct_lookup M1,M2;
  2383  
  2384  void stbv_inverse_mdct(float *buffer, int n, stbv_vorb *f, int blocktype)
  2385  {
  2386     mdct_lookup *M;
  2387     if (M1.n == n) M = &M1;
  2388     else if (M2.n == n) M = &M2;
  2389     else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; }
  2390     else { 
  2391        if (M2.n) __asm int 3;
  2392        mdct_init(&M2, n);
  2393        M = &M2;
  2394     }
  2395  
  2396     mdct_backward(M, buffer, buffer);
  2397  }
  2398  #endif
  2399  
  2400  
  2401  // the following were split out into separate functions while optimizing;
  2402  // they could be pushed back up but eh. __forceinline showed no change;
  2403  // they're probably already being inlined.
  2404  static void stbv_imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A)
  2405  {
  2406     float *ee0 = e + i_off;
  2407     float *ee2 = ee0 + k_off;
  2408     int i;
  2409  
  2410     assert((n & 3) == 0);
  2411     for (i=(n>>2); i > 0; --i) {
  2412        float k00_20, k01_21;
  2413        k00_20  = ee0[ 0] - ee2[ 0];
  2414        k01_21  = ee0[-1] - ee2[-1];
  2415        ee0[ 0] += ee2[ 0];//ee0[ 0] = ee0[ 0] + ee2[ 0];
  2416        ee0[-1] += ee2[-1];//ee0[-1] = ee0[-1] + ee2[-1];
  2417        ee2[ 0] = k00_20 * A[0] - k01_21 * A[1];
  2418        ee2[-1] = k01_21 * A[0] + k00_20 * A[1];
  2419        A += 8;
  2420  
  2421        k00_20  = ee0[-2] - ee2[-2];
  2422        k01_21  = ee0[-3] - ee2[-3];
  2423        ee0[-2] += ee2[-2];//ee0[-2] = ee0[-2] + ee2[-2];
  2424        ee0[-3] += ee2[-3];//ee0[-3] = ee0[-3] + ee2[-3];
  2425        ee2[-2] = k00_20 * A[0] - k01_21 * A[1];
  2426        ee2[-3] = k01_21 * A[0] + k00_20 * A[1];
  2427        A += 8;
  2428  
  2429        k00_20  = ee0[-4] - ee2[-4];
  2430        k01_21  = ee0[-5] - ee2[-5];
  2431        ee0[-4] += ee2[-4];//ee0[-4] = ee0[-4] + ee2[-4];
  2432        ee0[-5] += ee2[-5];//ee0[-5] = ee0[-5] + ee2[-5];
  2433        ee2[-4] = k00_20 * A[0] - k01_21 * A[1];
  2434        ee2[-5] = k01_21 * A[0] + k00_20 * A[1];
  2435        A += 8;
  2436  
  2437        k00_20  = ee0[-6] - ee2[-6];
  2438        k01_21  = ee0[-7] - ee2[-7];
  2439        ee0[-6] += ee2[-6];//ee0[-6] = ee0[-6] + ee2[-6];
  2440        ee0[-7] += ee2[-7];//ee0[-7] = ee0[-7] + ee2[-7];
  2441        ee2[-6] = k00_20 * A[0] - k01_21 * A[1];
  2442        ee2[-7] = k01_21 * A[0] + k00_20 * A[1];
  2443        A += 8;
  2444        ee0 -= 8;
  2445        ee2 -= 8;
  2446     }
  2447  }
  2448  
  2449  static void stbv_imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1)
  2450  {
  2451     int i;
  2452     float k00_20, k01_21;
  2453  
  2454     float *e0 = e + d0;
  2455     float *e2 = e0 + k_off;
  2456  
  2457     for (i=lim >> 2; i > 0; --i) {
  2458        k00_20 = e0[-0] - e2[-0];
  2459        k01_21 = e0[-1] - e2[-1];
  2460        e0[-0] += e2[-0];//e0[-0] = e0[-0] + e2[-0];
  2461        e0[-1] += e2[-1];//e0[-1] = e0[-1] + e2[-1];
  2462        e2[-0] = (k00_20)*A[0] - (k01_21) * A[1];
  2463        e2[-1] = (k01_21)*A[0] + (k00_20) * A[1];
  2464  
  2465        A += k1;
  2466  
  2467        k00_20 = e0[-2] - e2[-2];
  2468        k01_21 = e0[-3] - e2[-3];
  2469        e0[-2] += e2[-2];//e0[-2] = e0[-2] + e2[-2];
  2470        e0[-3] += e2[-3];//e0[-3] = e0[-3] + e2[-3];
  2471        e2[-2] = (k00_20)*A[0] - (k01_21) * A[1];
  2472        e2[-3] = (k01_21)*A[0] + (k00_20) * A[1];
  2473  
  2474        A += k1;
  2475  
  2476        k00_20 = e0[-4] - e2[-4];
  2477        k01_21 = e0[-5] - e2[-5];
  2478        e0[-4] += e2[-4];//e0[-4] = e0[-4] + e2[-4];
  2479        e0[-5] += e2[-5];//e0[-5] = e0[-5] + e2[-5];
  2480        e2[-4] = (k00_20)*A[0] - (k01_21) * A[1];
  2481        e2[-5] = (k01_21)*A[0] + (k00_20) * A[1];
  2482  
  2483        A += k1;
  2484  
  2485        k00_20 = e0[-6] - e2[-6];
  2486        k01_21 = e0[-7] - e2[-7];
  2487        e0[-6] += e2[-6];//e0[-6] = e0[-6] + e2[-6];
  2488        e0[-7] += e2[-7];//e0[-7] = e0[-7] + e2[-7];
  2489        e2[-6] = (k00_20)*A[0] - (k01_21) * A[1];
  2490        e2[-7] = (k01_21)*A[0] + (k00_20) * A[1];
  2491  
  2492        e0 -= 8;
  2493        e2 -= 8;
  2494  
  2495        A += k1;
  2496     }
  2497  }
  2498  
  2499  static void stbv_imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0)
  2500  {
  2501     int i;
  2502     float A0 = A[0];
  2503     float A1 = A[0+1];
  2504     float A2 = A[0+a_off];
  2505     float A3 = A[0+a_off+1];
  2506     float A4 = A[0+a_off*2+0];
  2507     float A5 = A[0+a_off*2+1];
  2508     float A6 = A[0+a_off*3+0];
  2509     float A7 = A[0+a_off*3+1];
  2510  
  2511     float k00,k11;
  2512  
  2513     float *ee0 = e  +i_off;
  2514     float *ee2 = ee0+k_off;
  2515  
  2516     for (i=n; i > 0; --i) {
  2517        k00     = ee0[ 0] - ee2[ 0];
  2518        k11     = ee0[-1] - ee2[-1];
  2519        ee0[ 0] =  ee0[ 0] + ee2[ 0];
  2520        ee0[-1] =  ee0[-1] + ee2[-1];
  2521        ee2[ 0] = (k00) * A0 - (k11) * A1;
  2522        ee2[-1] = (k11) * A0 + (k00) * A1;
  2523  
  2524        k00     = ee0[-2] - ee2[-2];
  2525        k11     = ee0[-3] - ee2[-3];
  2526        ee0[-2] =  ee0[-2] + ee2[-2];
  2527        ee0[-3] =  ee0[-3] + ee2[-3];
  2528        ee2[-2] = (k00) * A2 - (k11) * A3;
  2529        ee2[-3] = (k11) * A2 + (k00) * A3;
  2530  
  2531        k00     = ee0[-4] - ee2[-4];
  2532        k11     = ee0[-5] - ee2[-5];
  2533        ee0[-4] =  ee0[-4] + ee2[-4];
  2534        ee0[-5] =  ee0[-5] + ee2[-5];
  2535        ee2[-4] = (k00) * A4 - (k11) * A5;
  2536        ee2[-5] = (k11) * A4 + (k00) * A5;
  2537  
  2538        k00     = ee0[-6] - ee2[-6];
  2539        k11     = ee0[-7] - ee2[-7];
  2540        ee0[-6] =  ee0[-6] + ee2[-6];
  2541        ee0[-7] =  ee0[-7] + ee2[-7];
  2542        ee2[-6] = (k00) * A6 - (k11) * A7;
  2543        ee2[-7] = (k11) * A6 + (k00) * A7;
  2544  
  2545        ee0 -= k0;
  2546        ee2 -= k0;
  2547     }
  2548  }
  2549  
  2550  static __forceinline void stbv_iter_54(float *z)
  2551  {
  2552     float k00,k11,k22,k33;
  2553     float y0,y1,y2,y3;
  2554  
  2555     k00  = z[ 0] - z[-4];
  2556     y0   = z[ 0] + z[-4];
  2557     y2   = z[-2] + z[-6];
  2558     k22  = z[-2] - z[-6];
  2559  
  2560     z[-0] = y0 + y2;      // z0 + z4 + z2 + z6
  2561     z[-2] = y0 - y2;      // z0 + z4 - z2 - z6
  2562  
  2563     // done with y0,y2
  2564  
  2565     k33  = z[-3] - z[-7];
  2566  
  2567     z[-4] = k00 + k33;    // z0 - z4 + z3 - z7
  2568     z[-6] = k00 - k33;    // z0 - z4 - z3 + z7
  2569  
  2570     // done with k33
  2571  
  2572     k11  = z[-1] - z[-5];
  2573     y1   = z[-1] + z[-5];
  2574     y3   = z[-3] + z[-7];
  2575  
  2576     z[-1] = y1 + y3;      // z1 + z5 + z3 + z7
  2577     z[-3] = y1 - y3;      // z1 + z5 - z3 - z7
  2578     z[-5] = k11 - k22;    // z1 - z5 + z2 - z6
  2579     z[-7] = k11 + k22;    // z1 - z5 - z2 + z6
  2580  }
  2581  
  2582  static void stbv_imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n)
  2583  {
  2584     int a_off = base_n >> 3;
  2585     float A2 = A[0+a_off];
  2586     float *z = e + i_off;
  2587     float *base = z - 16 * n;
  2588  
  2589     while (z > base) {
  2590        float k00,k11;
  2591  
  2592        k00   = z[-0] - z[-8];
  2593        k11   = z[-1] - z[-9];
  2594        z[-0] = z[-0] + z[-8];
  2595        z[-1] = z[-1] + z[-9];
  2596        z[-8] =  k00;
  2597        z[-9] =  k11 ;
  2598  
  2599        k00    = z[ -2] - z[-10];
  2600        k11    = z[ -3] - z[-11];
  2601        z[ -2] = z[ -2] + z[-10];
  2602        z[ -3] = z[ -3] + z[-11];
  2603        z[-10] = (k00+k11) * A2;
  2604        z[-11] = (k11-k00) * A2;
  2605  
  2606        k00    = z[-12] - z[ -4];  // reverse to avoid a unary negation
  2607        k11    = z[ -5] - z[-13];
  2608        z[ -4] = z[ -4] + z[-12];
  2609        z[ -5] = z[ -5] + z[-13];
  2610        z[-12] = k11;
  2611        z[-13] = k00;
  2612  
  2613        k00    = z[-14] - z[ -6];  // reverse to avoid a unary negation
  2614        k11    = z[ -7] - z[-15];
  2615        z[ -6] = z[ -6] + z[-14];
  2616        z[ -7] = z[ -7] + z[-15];
  2617        z[-14] = (k00+k11) * A2;
  2618        z[-15] = (k00-k11) * A2;
  2619  
  2620        stbv_iter_54(z);
  2621        stbv_iter_54(z-8);
  2622        z -= 16;
  2623     }
  2624  }
  2625  
  2626  static void stbv_inverse_mdct(float *buffer, int n, stbv_vorb *f, int blocktype)
  2627  {
  2628     int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
  2629     int ld;
  2630     // @OPTIMIZE: reduce register pressure by using fewer variables?
  2631     int save_point = stbv_temp_alloc_save(f);
  2632     float *buf2 = (float *) stbv_temp_alloc(f, n2 * sizeof(*buf2));
  2633     float *u=NULL,*v=NULL;
  2634     // twiddle factors
  2635     float *A = f->A[blocktype];
  2636  
  2637     // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
  2638     // See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function.
  2639  
  2640     // kernel from paper
  2641  
  2642  
  2643     // merged:
  2644     //   copy and reflect spectral data
  2645     //   step 0
  2646  
  2647     // note that it turns out that the items added together during
  2648     // this step are, in fact, being added to themselves (as reflected
  2649     // by step 0). inexplicable inefficiency! this became obvious
  2650     // once I combined the passes.
  2651  
  2652     // so there's a missing 'times 2' here (for adding X to itself).
  2653     // this propogates through linearly to the end, where the numbers
  2654     // are 1/2 too small, and need to be compensated for.
  2655  
  2656     {
  2657        float *d,*e, *AA, *e_stop;
  2658        d = &buf2[n2-2];
  2659        AA = A;
  2660        e = &buffer[0];
  2661        e_stop = &buffer[n2];
  2662        while (e != e_stop) {
  2663           d[1] = (e[0] * AA[0] - e[2]*AA[1]);
  2664           d[0] = (e[0] * AA[1] + e[2]*AA[0]);
  2665           d -= 2;
  2666           AA += 2;
  2667           e += 4;
  2668        }
  2669  
  2670        e = &buffer[n2-3];
  2671        while (d >= buf2) {
  2672           d[1] = (-e[2] * AA[0] - -e[0]*AA[1]);
  2673           d[0] = (-e[2] * AA[1] + -e[0]*AA[0]);
  2674           d -= 2;
  2675           AA += 2;
  2676           e -= 4;
  2677        }
  2678     }
  2679  
  2680     // now we use symbolic names for these, so that we can
  2681     // possibly swap their meaning as we change which operations
  2682     // are in place
  2683  
  2684     u = buffer;
  2685     v = buf2;
  2686  
  2687     // step 2    (paper output is w, now u)
  2688     // this could be in place, but the data ends up in the wrong
  2689     // place... _somebody_'s got to swap it, so this is nominated
  2690     {
  2691        float *AA = &A[n2-8];
  2692        float *d0,*d1, *e0, *e1;
  2693  
  2694        e0 = &v[n4];
  2695        e1 = &v[0];
  2696  
  2697        d0 = &u[n4];
  2698        d1 = &u[0];
  2699  
  2700        while (AA >= A) {
  2701           float v40_20, v41_21;
  2702  
  2703           v41_21 = e0[1] - e1[1];
  2704           v40_20 = e0[0] - e1[0];
  2705           d0[1]  = e0[1] + e1[1];
  2706           d0[0]  = e0[0] + e1[0];
  2707           d1[1]  = v41_21*AA[4] - v40_20*AA[5];
  2708           d1[0]  = v40_20*AA[4] + v41_21*AA[5];
  2709  
  2710           v41_21 = e0[3] - e1[3];
  2711           v40_20 = e0[2] - e1[2];
  2712           d0[3]  = e0[3] + e1[3];
  2713           d0[2]  = e0[2] + e1[2];
  2714           d1[3]  = v41_21*AA[0] - v40_20*AA[1];
  2715           d1[2]  = v40_20*AA[0] + v41_21*AA[1];
  2716  
  2717           AA -= 8;
  2718  
  2719           d0 += 4;
  2720           d1 += 4;
  2721           e0 += 4;
  2722           e1 += 4;
  2723        }
  2724     }
  2725  
  2726     // step 3
  2727     ld = stbv_ilog(n) - 1; // stbv_ilog is off-by-one from normal definitions
  2728  
  2729     // optimized step 3:
  2730  
  2731     // the original step3 loop can be nested r inside s or s inside r;
  2732     // it's written originally as s inside r, but this is dumb when r
  2733     // iterates many times, and s few. So I have two copies of it and
  2734     // switch between them halfway.
  2735  
  2736     // this is iteration 0 of step 3
  2737     stbv_imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*0, -(n >> 3), A);
  2738     stbv_imdct_step3_iter0_loop(n >> 4, u, n2-1-n4*1, -(n >> 3), A);
  2739  
  2740     // this is iteration 1 of step 3
  2741     stbv_imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*0, -(n >> 4), A, 16);
  2742     stbv_imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*1, -(n >> 4), A, 16);
  2743     stbv_imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*2, -(n >> 4), A, 16);
  2744     stbv_imdct_step3_inner_r_loop(n >> 5, u, n2-1 - n8*3, -(n >> 4), A, 16);
  2745  
  2746     l=2;
  2747     for (; l < (ld-3)>>1; ++l) {
  2748        int k0 = n >> (l+2), k0_2 = k0>>1;
  2749        int lim = 1 << (l+1);
  2750        int i;
  2751        for (i=0; i < lim; ++i)
  2752           stbv_imdct_step3_inner_r_loop(n >> (l+4), u, n2-1 - k0*i, -k0_2, A, 1 << (l+3));
  2753     }
  2754  
  2755     for (; l < ld-6; ++l) {
  2756        int k0 = n >> (l+2), k1 = 1 << (l+3), k0_2 = k0>>1;
  2757        int rlim = n >> (l+6), r;
  2758        int lim = 1 << (l+1);
  2759        int i_off;
  2760        float *A0 = A;
  2761        i_off = n2-1;
  2762        for (r=rlim; r > 0; --r) {
  2763           stbv_imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0);
  2764           A0 += k1*4;
  2765           i_off -= 8;
  2766        }
  2767     }
  2768  
  2769     // iterations with count:
  2770     //   ld-6,-5,-4 all interleaved together
  2771     //       the big win comes from getting rid of needless flops
  2772     //         due to the constants on pass 5 & 4 being all 1 and 0;
  2773     //       combining them to be simultaneous to improve cache made little difference
  2774     stbv_imdct_step3_inner_s_loop_ld654(n >> 5, u, n2-1, A, n);
  2775  
  2776     // output is u
  2777  
  2778     // step 4, 5, and 6
  2779     // cannot be in-place because of step 5
  2780     {
  2781        stbv_uint16 *bitrev = f->stbv_bit_reverse[blocktype];
  2782        // weirdly, I'd have thought reading sequentially and writing
  2783        // erratically would have been better than vice-versa, but in
  2784        // fact that's not what my testing showed. (That is, with
  2785        // j = bitreverse(i), do you read i and write j, or read j and write i.)
  2786  
  2787        float *d0 = &v[n4-4];
  2788        float *d1 = &v[n2-4];
  2789        while (d0 >= v) {
  2790           int k4;
  2791  
  2792           k4 = bitrev[0];
  2793           d1[3] = u[k4+0];
  2794           d1[2] = u[k4+1];
  2795           d0[3] = u[k4+2];
  2796           d0[2] = u[k4+3];
  2797  
  2798           k4 = bitrev[1];
  2799           d1[1] = u[k4+0];
  2800           d1[0] = u[k4+1];
  2801           d0[1] = u[k4+2];
  2802           d0[0] = u[k4+3];
  2803           
  2804           d0 -= 4;
  2805           d1 -= 4;
  2806           bitrev += 2;
  2807        }
  2808     }
  2809     // (paper output is u, now v)
  2810  
  2811  
  2812     // data must be in buf2
  2813     assert(v == buf2);
  2814  
  2815     // step 7   (paper output is v, now v)
  2816     // this is now in place
  2817     {
  2818        float *C = f->C[blocktype];
  2819        float *d, *e;
  2820  
  2821        d = v;
  2822        e = v + n2 - 4;
  2823  
  2824        while (d < e) {
  2825           float a02,a11,b0,b1,b2,b3;
  2826  
  2827           a02 = d[0] - e[2];
  2828           a11 = d[1] + e[3];
  2829  
  2830           b0 = C[1]*a02 + C[0]*a11;
  2831           b1 = C[1]*a11 - C[0]*a02;
  2832  
  2833           b2 = d[0] + e[ 2];
  2834           b3 = d[1] - e[ 3];
  2835  
  2836           d[0] = b2 + b0;
  2837           d[1] = b3 + b1;
  2838           e[2] = b2 - b0;
  2839           e[3] = b1 - b3;
  2840  
  2841           a02 = d[2] - e[0];
  2842           a11 = d[3] + e[1];
  2843  
  2844           b0 = C[3]*a02 + C[2]*a11;
  2845           b1 = C[3]*a11 - C[2]*a02;
  2846  
  2847           b2 = d[2] + e[ 0];
  2848           b3 = d[3] - e[ 1];
  2849  
  2850           d[2] = b2 + b0;
  2851           d[3] = b3 + b1;
  2852           e[0] = b2 - b0;
  2853           e[1] = b1 - b3;
  2854  
  2855           C += 4;
  2856           d += 4;
  2857           e -= 4;
  2858        }
  2859     }
  2860  
  2861     // data must be in buf2
  2862  
  2863  
  2864     // step 8+decode   (paper output is X, now buffer)
  2865     // this generates pairs of data a la 8 and pushes them directly through
  2866     // the decode kernel (pushing rather than pulling) to avoid having
  2867     // to make another pass later
  2868  
  2869     // this cannot POSSIBLY be in place, so we refer to the buffers directly
  2870  
  2871     {
  2872        float *d0,*d1,*d2,*d3;
  2873  
  2874        float *B = f->B[blocktype] + n2 - 8;
  2875        float *e = buf2 + n2 - 8;
  2876        d0 = &buffer[0];
  2877        d1 = &buffer[n2-4];
  2878        d2 = &buffer[n2];
  2879        d3 = &buffer[n-4];
  2880        while (e >= v) {
  2881           float p0,p1,p2,p3;
  2882  
  2883           p3 =  e[6]*B[7] - e[7]*B[6];
  2884           p2 = -e[6]*B[6] - e[7]*B[7]; 
  2885  
  2886           d0[0] =   p3;
  2887           d1[3] = - p3;
  2888           d2[0] =   p2;
  2889           d3[3] =   p2;
  2890  
  2891           p1 =  e[4]*B[5] - e[5]*B[4];
  2892           p0 = -e[4]*B[4] - e[5]*B[5]; 
  2893  
  2894           d0[1] =   p1;
  2895           d1[2] = - p1;
  2896           d2[1] =   p0;
  2897           d3[2] =   p0;
  2898  
  2899           p3 =  e[2]*B[3] - e[3]*B[2];
  2900           p2 = -e[2]*B[2] - e[3]*B[3]; 
  2901  
  2902           d0[2] =   p3;
  2903           d1[1] = - p3;
  2904           d2[2] =   p2;
  2905           d3[1] =   p2;
  2906  
  2907           p1 =  e[0]*B[1] - e[1]*B[0];
  2908           p0 = -e[0]*B[0] - e[1]*B[1]; 
  2909  
  2910           d0[3] =   p1;
  2911           d1[0] = - p1;
  2912           d2[3] =   p0;
  2913           d3[0] =   p0;
  2914  
  2915           B -= 8;
  2916           e -= 8;
  2917           d0 += 4;
  2918           d2 += 4;
  2919           d1 -= 4;
  2920           d3 -= 4;
  2921        }
  2922     }
  2923  
  2924     stbv_temp_free(f,buf2);
  2925     stbv_temp_alloc_restore(f,save_point);
  2926  }
  2927  
  2928  #if 0
  2929  // this is the original version of the above code, if you want to optimize it from scratch
  2930  void inverse_mdct_naive(float *buffer, int n)
  2931  {
  2932     float s;
  2933     float A[1 << 12], B[1 << 12], C[1 << 11];
  2934     int i,k,k2,k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
  2935     int n3_4 = n - n4, ld;
  2936     // how can they claim this only uses N words?!
  2937     // oh, because they're only used sparsely, whoops
  2938     float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13];
  2939     // set up twiddle factors
  2940  
  2941     for (k=k2=0; k < n4; ++k,k2+=2) {
  2942        A[k2  ] = (float)  cos(4*k*M_PI/n);
  2943        A[k2+1] = (float) -sin(4*k*M_PI/n);
  2944        B[k2  ] = (float)  cos((k2+1)*M_PI/n/2);
  2945        B[k2+1] = (float)  sin((k2+1)*M_PI/n/2);
  2946     }
  2947     for (k=k2=0; k < n8; ++k,k2+=2) {
  2948        C[k2  ] = (float)  cos(2*(k2+1)*M_PI/n);
  2949        C[k2+1] = (float) -sin(2*(k2+1)*M_PI/n);
  2950     }
  2951  
  2952     // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
  2953     // Note there are bugs in that pseudocode, presumably due to them attempting
  2954     // to rename the arrays nicely rather than representing the way their actual
  2955     // implementation bounces buffers back and forth. As a result, even in the
  2956     // "some formulars corrected" version, a direct implementation fails. These
  2957     // are noted below as "paper bug".
  2958  
  2959     // copy and reflect spectral data
  2960     for (k=0; k < n2; ++k) u[k] = buffer[k];
  2961     for (   ; k < n ; ++k) u[k] = -buffer[n - k - 1];
  2962     // kernel from paper
  2963     // step 1
  2964     for (k=k2=k4=0; k < n4; k+=1, k2+=2, k4+=4) {
  2965        v[n-k4-1] = (u[k4] - u[n-k4-1]) * A[k2]   - (u[k4+2] - u[n-k4-3])*A[k2+1];
  2966        v[n-k4-3] = (u[k4] - u[n-k4-1]) * A[k2+1] + (u[k4+2] - u[n-k4-3])*A[k2];
  2967     }
  2968     // step 2
  2969     for (k=k4=0; k < n8; k+=1, k4+=4) {
  2970        w[n2+3+k4] = v[n2+3+k4] + v[k4+3];
  2971        w[n2+1+k4] = v[n2+1+k4] + v[k4+1];
  2972        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];
  2973        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];
  2974     }
  2975     // step 3
  2976     ld = stbv_ilog(n) - 1; // stbv_ilog is off-by-one from normal definitions
  2977     for (l=0; l < ld-3; ++l) {
  2978        int k0 = n >> (l+2), k1 = 1 << (l+3);
  2979        int rlim = n >> (l+4), r4, r;
  2980        int s2lim = 1 << (l+2), s2;
  2981        for (r=r4=0; r < rlim; r4+=4,++r) {
  2982           for (s2=0; s2 < s2lim; s2+=2) {
  2983              u[n-1-k0*s2-r4] = w[n-1-k0*s2-r4] + w[n-1-k0*(s2+1)-r4];
  2984              u[n-3-k0*s2-r4] = w[n-3-k0*s2-r4] + w[n-3-k0*(s2+1)-r4];
  2985              u[n-1-k0*(s2+1)-r4] = (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1]
  2986                                  - (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1+1];
  2987              u[n-3-k0*(s2+1)-r4] = (w[n-3-k0*s2-r4] - w[n-3-k0*(s2+1)-r4]) * A[r*k1]
  2988                                  + (w[n-1-k0*s2-r4] - w[n-1-k0*(s2+1)-r4]) * A[r*k1+1];
  2989           }
  2990        }
  2991        if (l+1 < ld-3) {
  2992           // paper bug: ping-ponging of u&w here is omitted
  2993           memcpy(w, u, sizeof(u));
  2994        }
  2995     }
  2996  
  2997     // step 4
  2998     for (i=0; i < n8; ++i) {
  2999        int j = stbv_bit_reverse(i) >> (32-ld+3);
  3000        assert(j < n8);
  3001        if (i == j) {
  3002           // paper bug: original code probably swapped in place; if copying,
  3003           //            need to directly copy in this case
  3004           int i8 = i << 3;
  3005           v[i8+1] = u[i8+1];
  3006           v[i8+3] = u[i8+3];
  3007           v[i8+5] = u[i8+5];
  3008           v[i8+7] = u[i8+7];
  3009        } else if (i < j) {
  3010           int i8 = i << 3, j8 = j << 3;
  3011           v[j8+1] = u[i8+1], v[i8+1] = u[j8 + 1];
  3012           v[j8+3] = u[i8+3], v[i8+3] = u[j8 + 3];
  3013           v[j8+5] = u[i8+5], v[i8+5] = u[j8 + 5];
  3014           v[j8+7] = u[i8+7], v[i8+7] = u[j8 + 7];
  3015        }
  3016     }
  3017     // step 5
  3018     for (k=0; k < n2; ++k) {
  3019        w[k] = v[k*2+1];
  3020     }
  3021     // step 6
  3022     for (k=k2=k4=0; k < n8; ++k, k2 += 2, k4 += 4) {
  3023        u[n-1-k2] = w[k4];
  3024        u[n-2-k2] = w[k4+1];
  3025        u[n3_4 - 1 - k2] = w[k4+2];
  3026        u[n3_4 - 2 - k2] = w[k4+3];
  3027     }
  3028     // step 7
  3029     for (k=k2=0; k < n8; ++k, k2 += 2) {
  3030        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;
  3031        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;
  3032        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;
  3033        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;
  3034     }
  3035     // step 8
  3036     for (k=k2=0; k < n4; ++k,k2 += 2) {
  3037        X[k]      = v[k2+n2]*B[k2  ] + v[k2+1+n2]*B[k2+1];
  3038        X[n2-1-k] = v[k2+n2]*B[k2+1] - v[k2+1+n2]*B[k2  ];
  3039     }
  3040  
  3041     // decode kernel to output
  3042     // determined the following value experimentally
  3043     // (by first figuring out what made inverse_mdct_slow work); then matching that here
  3044     // (probably vorbis encoder premultiplies by n or n/2, to save it on the decoder?)
  3045     s = 0.5; // theoretically would be n4
  3046  
  3047     // [[[ note! the s value of 0.5 is compensated for by the B[] in the current code,
  3048     //     so it needs to use the "old" B values to behave correctly, or else
  3049     //     set s to 1.0 ]]]
  3050     for (i=0; i < n4  ; ++i) buffer[i] = s * X[i+n4];
  3051     for (   ; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1];
  3052     for (   ; i < n   ; ++i) buffer[i] = -s * X[i - n3_4];
  3053  }
  3054  #endif
  3055  
  3056  static float *stbv_get_window(stbv_vorb *f, int len)
  3057  {
  3058     len <<= 1;
  3059     if (len == f->blocksize_0) return f->window[0];
  3060     if (len == f->blocksize_1) return f->window[1];
  3061     assert(0);
  3062     return NULL;
  3063  }
  3064  
  3065  #ifndef STB_VORBIS_NO_DEFER_FLOOR
  3066  typedef stbv_int16 STBV_YTYPE;
  3067  #else
  3068  typedef int STBV_YTYPE;
  3069  #endif
  3070  static int stbv_do_floor(stbv_vorb *f, StbvMapping *map, int i, int n, float *target, STBV_YTYPE *finalY, stbv_uint8 *step2_flag)
  3071  {
  3072     int n2 = n >> 1;
  3073     int s = map->chan[i].mux, floor;
  3074     floor = map->submap_floor[s];
  3075     if (f->floor_types[floor] == 0) {
  3076        return stbv_error(f, VORBIS_invalid_stream);
  3077     } else {
  3078        StbvFloor1 *g = &f->floor_config[floor].floor1;
  3079        int j,q;
  3080        int lx = 0, ly = finalY[0] * g->floor1_multiplier;
  3081        for (q=1; q < g->values; ++q) {
  3082           j = g->sorted_order[q];
  3083           #ifndef STB_VORBIS_NO_DEFER_FLOOR
  3084           if (finalY[j] >= 0)
  3085           #else
  3086           if (step2_flag[j])
  3087           #endif
  3088           {
  3089              int hy = finalY[j] * g->floor1_multiplier;
  3090              int hx = g->Xlist[j];
  3091              if (lx != hx)
  3092                 stbv_draw_line(target, lx,ly, hx,hy, n2);
  3093              STBV_CHECK(f);
  3094              lx = hx, ly = hy;
  3095           }
  3096        }
  3097        if (lx < n2) {
  3098           // optimization of: stbv_draw_line(target, lx,ly, n,ly, n2);
  3099           for (j=lx; j < n2; ++j)
  3100              STBV_LINE_OP(target[j], stbv_inverse_db_table[ly]);
  3101           STBV_CHECK(f);
  3102        }
  3103     }
  3104     return TRUE;
  3105  }
  3106  
  3107  // The meaning of "left" and "right"
  3108  //
  3109  // For a given frame:
  3110  //     we compute samples from 0..n
  3111  //     window_center is n/2
  3112  //     we'll window and mix the samples from left_start to left_end with data from the previous frame
  3113  //     all of the samples from left_end to right_start can be output without mixing; however,
  3114  //        this interval is 0-length except when transitioning between short and long frames
  3115  //     all of the samples from right_start to right_end need to be mixed with the next frame,
  3116  //        which we don't have, so those get saved in a buffer
  3117  //     frame N's right_end-right_start, the number of samples to mix with the next frame,
  3118  //        has to be the same as frame N+1's left_end-left_start (which they are by
  3119  //        construction)
  3120  
  3121  static int stbv_vorbis_decode_initial(stbv_vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode)
  3122  {
  3123     StbvMode *m;
  3124     int i, n, prev, next, window_center;
  3125     f->channel_buffer_start = f->channel_buffer_end = 0;
  3126  
  3127    retry:
  3128     if (f->eof) return FALSE;
  3129     if (!stbv_maybe_start_packet(f))
  3130        return FALSE;
  3131     // check packet type
  3132     if (stbv_get_bits(f,1) != 0) {
  3133        if (STBV_IS_PUSH_MODE(f))
  3134           return stbv_error(f,VORBIS_bad_packet_type);
  3135        while (STBV_EOP != stbv_get8_packet(f));
  3136        goto retry;
  3137     }
  3138  
  3139     if (f->alloc.alloc_buffer)
  3140        assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
  3141  
  3142     i = stbv_get_bits(f, stbv_ilog(f->mode_count-1));
  3143     if (i == STBV_EOP) return FALSE;
  3144     if (i >= f->mode_count) return FALSE;
  3145     *mode = i;
  3146     m = f->mode_config + i;
  3147     if (m->blockflag) {
  3148        n = f->blocksize_1;
  3149        prev = stbv_get_bits(f,1);
  3150        next = stbv_get_bits(f,1);
  3151     } else {
  3152        prev = next = 0;
  3153        n = f->blocksize_0;
  3154     }
  3155  
  3156  // WINDOWING
  3157  
  3158     window_center = n >> 1;
  3159     if (m->blockflag && !prev) {
  3160        *p_left_start = (n - f->blocksize_0) >> 2;
  3161        *p_left_end   = (n + f->blocksize_0) >> 2;
  3162     } else {
  3163        *p_left_start = 0;
  3164        *p_left_end   = window_center;
  3165     }
  3166     if (m->blockflag && !next) {
  3167        *p_right_start = (n*3 - f->blocksize_0) >> 2;
  3168        *p_right_end   = (n*3 + f->blocksize_0) >> 2;
  3169     } else {
  3170        *p_right_start = window_center;
  3171        *p_right_end   = n;
  3172     }
  3173  
  3174     return TRUE;
  3175  }
  3176  
  3177  static int stbv_vorbis_decode_packet_rest(stbv_vorb *f, int *len, StbvMode *m, int left_start, int left_end, int right_start, int right_end, int *p_left)
  3178  {
  3179     StbvMapping *map;
  3180     int i,j,k,n,n2;
  3181     int zero_channel[256];
  3182     int really_zero_channel[256];
  3183  
  3184  // WINDOWING
  3185  
  3186     n = f->blocksize[m->blockflag];
  3187     map = &f->mapping[m->mapping];
  3188  
  3189  // FLOORS
  3190     n2 = n >> 1;
  3191  
  3192     STBV_CHECK(f);
  3193  
  3194     for (i=0; i < f->channels; ++i) {
  3195        int s = map->chan[i].mux, floor;
  3196        zero_channel[i] = FALSE;
  3197        floor = map->submap_floor[s];
  3198        if (f->floor_types[floor] == 0) {
  3199           return stbv_error(f, VORBIS_invalid_stream);
  3200        } else {
  3201           StbvFloor1 *g = &f->floor_config[floor].floor1;
  3202           if (stbv_get_bits(f, 1)) {
  3203              short *finalY;
  3204              stbv_uint8 step2_flag[256];
  3205              static int range_list[4] = { 256, 128, 86, 64 };
  3206              int range = range_list[g->floor1_multiplier-1];
  3207              int offset = 2;
  3208              finalY = f->finalY[i];
  3209              finalY[0] = stbv_get_bits(f, stbv_ilog(range)-1);
  3210              finalY[1] = stbv_get_bits(f, stbv_ilog(range)-1);
  3211              for (j=0; j < g->partitions; ++j) {
  3212                 int pclass = g->partition_class_list[j];
  3213                 int cdim = g->class_dimensions[pclass];
  3214                 int cbits = g->class_subclasses[pclass];
  3215                 int csub = (1 << cbits)-1;
  3216                 int cval = 0;
  3217                 if (cbits) {
  3218                    StbvCodebook *c = f->codebooks + g->class_masterbooks[pclass];
  3219                    STBV_DECODE(cval,f,c);
  3220                 }
  3221                 for (k=0; k < cdim; ++k) {
  3222                    int book = g->subclass_books[pclass][cval & csub];
  3223                    cval = cval >> cbits;
  3224                    if (book >= 0) {
  3225                       int temp;
  3226                       StbvCodebook *c = f->codebooks + book;
  3227                       STBV_DECODE(temp,f,c);
  3228                       finalY[offset++] = temp;
  3229                    } else
  3230                       finalY[offset++] = 0;
  3231                 }
  3232              }
  3233              if (f->valid_bits == STBV_INVALID_BITS) goto error; // behavior according to spec
  3234              step2_flag[0] = step2_flag[1] = 1;
  3235              for (j=2; j < g->values; ++j) {
  3236                 int low, high, pred, highroom, lowroom, room, val;
  3237                 low = g->stbv_neighbors[j][0];
  3238                 high = g->stbv_neighbors[j][1];
  3239                 //stbv_neighbors(g->Xlist, j, &low, &high);
  3240                 pred = stbv_predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]);
  3241                 val = finalY[j];
  3242                 highroom = range - pred;
  3243                 lowroom = pred;
  3244                 if (highroom < lowroom)
  3245                    room = highroom * 2;
  3246                 else
  3247                    room = lowroom * 2;
  3248                 if (val) {
  3249                    step2_flag[low] = step2_flag[high] = 1;
  3250                    step2_flag[j] = 1;
  3251                    if (val >= room)
  3252                       if (highroom > lowroom)
  3253                          finalY[j] = val - lowroom + pred;
  3254                       else
  3255                          finalY[j] = pred - val + highroom - 1;
  3256                    else
  3257                       if (val & 1)
  3258                          finalY[j] = pred - ((val+1)>>1);
  3259                       else
  3260                          finalY[j] = pred + (val>>1);
  3261                 } else {
  3262                    step2_flag[j] = 0;
  3263                    finalY[j] = pred;
  3264                 }
  3265              }
  3266  
  3267  #ifdef STB_VORBIS_NO_DEFER_FLOOR
  3268              stbv_do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag);
  3269  #else
  3270              // defer final floor computation until _after_ residue
  3271              for (j=0; j < g->values; ++j) {
  3272                 if (!step2_flag[j])
  3273                    finalY[j] = -1;
  3274              }
  3275  #endif
  3276           } else {
  3277             error:
  3278              zero_channel[i] = TRUE;
  3279           }
  3280           // So we just defer everything else to later
  3281  
  3282           // at this point we've decoded the floor into buffer
  3283        }
  3284     }
  3285     STBV_CHECK(f);
  3286     // at this point we've decoded all floors
  3287  
  3288     if (f->alloc.alloc_buffer)
  3289        assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
  3290  
  3291     // re-enable coupled channels if necessary
  3292     memcpy(really_zero_channel, zero_channel, sizeof(really_zero_channel[0]) * f->channels);
  3293     for (i=0; i < map->coupling_steps; ++i)
  3294        if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) {
  3295           zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE;
  3296        }
  3297  
  3298     STBV_CHECK(f);
  3299  // RESIDUE STBV_DECODE
  3300     for (i=0; i < map->submaps; ++i) {
  3301        float *residue_buffers[STB_VORBIS_MAX_CHANNELS];
  3302        int r;
  3303        stbv_uint8 do_not_decode[256];
  3304        int ch = 0;
  3305        for (j=0; j < f->channels; ++j) {
  3306           if (map->chan[j].mux == i) {
  3307              if (zero_channel[j]) {
  3308                 do_not_decode[ch] = TRUE;
  3309                 residue_buffers[ch] = NULL;
  3310              } else {
  3311                 do_not_decode[ch] = FALSE;
  3312                 residue_buffers[ch] = f->channel_buffers[j];
  3313              }
  3314              ++ch;
  3315           }
  3316        }
  3317        r = map->submap_residue[i];
  3318        stbv_decode_residue(f, residue_buffers, ch, n2, r, do_not_decode);
  3319     }
  3320  
  3321     if (f->alloc.alloc_buffer)
  3322        assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
  3323     STBV_CHECK(f);
  3324  
  3325  // INVERSE COUPLING
  3326     for (i = map->coupling_steps-1; i >= 0; --i) {
  3327        int n2 = n >> 1;
  3328        float *m = f->channel_buffers[map->chan[i].magnitude];
  3329        float *a = f->channel_buffers[map->chan[i].angle    ];
  3330        for (j=0; j < n2; ++j) {
  3331           float a2,m2;
  3332           if (m[j] > 0)
  3333              if (a[j] > 0)
  3334                 m2 = m[j], a2 = m[j] - a[j];
  3335              else
  3336                 a2 = m[j], m2 = m[j] + a[j];
  3337           else
  3338              if (a[j] > 0)
  3339                 m2 = m[j], a2 = m[j] + a[j];
  3340              else
  3341                 a2 = m[j], m2 = m[j] - a[j];
  3342           m[j] = m2;
  3343           a[j] = a2;
  3344        }
  3345     }
  3346     STBV_CHECK(f);
  3347  
  3348     // finish decoding the floors
  3349  #ifndef STB_VORBIS_NO_DEFER_FLOOR
  3350     for (i=0; i < f->channels; ++i) {
  3351        if (really_zero_channel[i]) {
  3352           memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
  3353        } else {
  3354           stbv_do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i], NULL);
  3355        }
  3356     }
  3357  #else
  3358     for (i=0; i < f->channels; ++i) {
  3359        if (really_zero_channel[i]) {
  3360           memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
  3361        } else {
  3362           for (j=0; j < n2; ++j)
  3363              f->channel_buffers[i][j] *= f->floor_buffers[i][j];
  3364        }
  3365     }
  3366  #endif
  3367  
  3368  // INVERSE MDCT
  3369     STBV_CHECK(f);
  3370     for (i=0; i < f->channels; ++i)
  3371        stbv_inverse_mdct(f->channel_buffers[i], n, f, m->blockflag);
  3372     STBV_CHECK(f);
  3373  
  3374     // this shouldn't be necessary, unless we exited on an error
  3375     // and want to flush to get to the next packet
  3376     stbv_flush_packet(f);
  3377  
  3378     if (f->first_decode) {
  3379        // assume we start so first non-discarded sample is sample 0
  3380        // this isn't to spec, but spec would require us to read ahead
  3381        // and decode the size of all current frames--could be done,
  3382        // but presumably it's not a commonly used feature
  3383        f->current_loc = -n2; // start of first frame is positioned for discard
  3384        // we might have to discard samples "from" the next frame too,
  3385        // if we're lapping a large block then a small at the start?
  3386        f->discard_samples_deferred = n - right_end;
  3387        f->current_loc_valid = TRUE;
  3388        f->first_decode = FALSE;
  3389     } else if (f->discard_samples_deferred) {
  3390        if (f->discard_samples_deferred >= right_start - left_start) {
  3391           f->discard_samples_deferred -= (right_start - left_start);
  3392           left_start = right_start;
  3393           *p_left = left_start;
  3394        } else {
  3395           left_start += f->discard_samples_deferred;
  3396           *p_left = left_start;
  3397           f->discard_samples_deferred = 0;
  3398        }
  3399     } else if (f->previous_length == 0 && f->current_loc_valid) {
  3400        // we're recovering from a seek... that means we're going to discard
  3401        // the samples from this packet even though we know our position from
  3402        // the last page header, so we need to update the position based on
  3403        // the discarded samples here
  3404        // but wait, the code below is going to add this in itself even
  3405        // on a discard, so we don't need to do it here...
  3406     }
  3407  
  3408     // check if we have ogg information about the sample # for this packet
  3409     if (f->last_seg_which == f->end_seg_with_known_loc) {
  3410        // if we have a valid current loc, and this is final:
  3411        if (f->current_loc_valid && (f->page_flag & STBV_PAGEFLAG_last_page)) {
  3412           stbv_uint32 current_end = f->known_loc_for_packet;
  3413           // then let's infer the size of the (probably) short final frame
  3414           if (current_end < f->current_loc + (right_end-left_start)) {
  3415              if (current_end < f->current_loc) {
  3416                 // negative truncation, that's impossible!
  3417                 *len = 0;
  3418              } else {
  3419                 *len = current_end - f->current_loc;
  3420              }
  3421              *len += left_start; // this doesn't seem right, but has no ill effect on my test files
  3422              if (*len > right_end) *len = right_end; // this should never happen
  3423              f->current_loc += *len;
  3424              return TRUE;
  3425           }
  3426        }
  3427        // otherwise, just set our sample loc
  3428        // guess that the ogg granule pos refers to the _middle_ of the
  3429        // last frame?
  3430        // set f->current_loc to the position of left_start
  3431        f->current_loc = f->known_loc_for_packet - (n2-left_start);
  3432        f->current_loc_valid = TRUE;
  3433     }
  3434     if (f->current_loc_valid)
  3435        f->current_loc += (right_start - left_start);
  3436  
  3437     if (f->alloc.alloc_buffer)
  3438        assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
  3439     *len = right_end;  // ignore samples after the window goes to 0
  3440     STBV_CHECK(f);
  3441  
  3442     return TRUE;
  3443  }
  3444  
  3445  static int stbv_vorbis_decode_packet(stbv_vorb *f, int *len, int *p_left, int *p_right)
  3446  {
  3447     int mode, left_end, right_end;
  3448     if (!stbv_vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) return 0;
  3449     return stbv_vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left);
  3450  }
  3451  
  3452  static int stbv_vorbis_finish_frame(stb_vorbis *f, int len, int left, int right)
  3453  {
  3454     int prev,i,j;
  3455     // we use right&left (the start of the right- and left-window sin()-regions)
  3456     // to determine how much to return, rather than inferring from the rules
  3457     // (same result, clearer code); 'left' indicates where our sin() window
  3458     // starts, therefore where the previous window's right edge starts, and
  3459     // therefore where to start mixing from the previous buffer. 'right'
  3460     // indicates where our sin() ending-window starts, therefore that's where
  3461     // we start saving, and where our returned-data ends.
  3462  
  3463     // mixin from previous window
  3464     if (f->previous_length) {
  3465        int i,j, n = f->previous_length;
  3466        float *w = stbv_get_window(f, n);
  3467        for (i=0; i < f->channels; ++i) {
  3468           for (j=0; j < n; ++j)
  3469              f->channel_buffers[i][left+j] =
  3470                 f->channel_buffers[i][left+j]*w[    j] +
  3471                 f->previous_window[i][     j]*w[n-1-j];
  3472        }
  3473     }
  3474  
  3475     prev = f->previous_length;
  3476  
  3477     // last half of this data becomes previous window
  3478     f->previous_length = len - right;
  3479  
  3480     // @OPTIMIZE: could avoid this copy by double-buffering the
  3481     // output (flipping previous_window with channel_buffers), but
  3482     // then previous_window would have to be 2x as large, and
  3483     // channel_buffers couldn't be temp mem (although they're NOT
  3484     // currently temp mem, they could be (unless we want to level
  3485     // performance by spreading out the computation))
  3486     for (i=0; i < f->channels; ++i)
  3487        for (j=0; right+j < len; ++j)
  3488           f->previous_window[i][j] = f->channel_buffers[i][right+j];
  3489  
  3490     if (!prev)
  3491        // there was no previous packet, so this data isn't valid...
  3492        // this isn't entirely true, only the would-have-overlapped data
  3493        // isn't valid, but this seems to be what the spec requires
  3494        return 0;
  3495  
  3496     // truncate a short frame
  3497     if (len < right) right = len;
  3498  
  3499     f->samples_output += right-left;
  3500  
  3501     return right - left;
  3502  }
  3503  
  3504  static int stbv_vorbis_pump_first_frame(stb_vorbis *f)
  3505  {
  3506     int len, right, left, res;
  3507     res = stbv_vorbis_decode_packet(f, &len, &left, &right);
  3508     if (res)
  3509        stbv_vorbis_finish_frame(f, len, left, right);
  3510     return res;
  3511  }
  3512  
  3513  #ifndef STB_VORBIS_NO_PUSHDATA_API
  3514  static int stbv_is_whole_packet_present(stb_vorbis *f, int end_page)
  3515  {
  3516     // make sure that we have the packet available before continuing...
  3517     // this requires a full ogg parse, but we know we can fetch from f->stream
  3518  
  3519     // instead of coding this out explicitly, we could save the current read state,
  3520     // read the next packet with stbv_get8() until end-of-packet, check f->eof, then
  3521     // reset the state? but that would be slower, esp. since we'd have over 256 bytes
  3522     // of state to restore (primarily the page segment table)
  3523  
  3524     int s = f->next_seg, first = TRUE;
  3525     stbv_uint8 *p = f->stream;
  3526  
  3527     if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag
  3528        for (; s < f->segment_count; ++s) {
  3529           p += f->segments[s];
  3530           if (f->segments[s] < 255)               // stop at first short segment
  3531              break;
  3532        }
  3533        // either this continues, or it ends it...
  3534        if (end_page)
  3535           if (s < f->segment_count-1)             return stbv_error(f, VORBIS_invalid_stream);
  3536        if (s == f->segment_count)
  3537           s = -1; // set 'crosses page' flag
  3538        if (p > f->stream_end)                     return stbv_error(f, VORBIS_need_more_data);
  3539        first = FALSE;
  3540     }
  3541     for (; s == -1;) {
  3542        stbv_uint8 *q; 
  3543        int n;
  3544  
  3545        // check that we have the page header ready
  3546        if (p + 26 >= f->stream_end)               return stbv_error(f, VORBIS_need_more_data);
  3547        // validate the page
  3548        if (memcmp(p, stbv_ogg_page_header, 4))         return stbv_error(f, VORBIS_invalid_stream);
  3549        if (p[4] != 0)                             return stbv_error(f, VORBIS_invalid_stream);
  3550        if (first) { // the first segment must NOT have 'continued_packet', later ones MUST
  3551           if (f->previous_length)
  3552              if ((p[5] & STBV_PAGEFLAG_continued_packet))  return stbv_error(f, VORBIS_invalid_stream);
  3553           // if no previous length, we're resynching, so we can come in on a continued-packet,
  3554           // which we'll just drop
  3555        } else {
  3556           if (!(p[5] & STBV_PAGEFLAG_continued_packet)) return stbv_error(f, VORBIS_invalid_stream);
  3557        }
  3558        n = p[26]; // segment counts
  3559        q = p+27;  // q points to segment table
  3560        p = q + n; // advance past header
  3561        // make sure we've read the segment table
  3562        if (p > f->stream_end)                     return stbv_error(f, VORBIS_need_more_data);
  3563        for (s=0; s < n; ++s) {
  3564           p += q[s];
  3565           if (q[s] < 255)
  3566              break;
  3567        }
  3568        if (end_page)
  3569           if (s < n-1)                            return stbv_error(f, VORBIS_invalid_stream);
  3570        if (s == n)
  3571           s = -1; // set 'crosses page' flag
  3572        if (p > f->stream_end)                     return stbv_error(f, VORBIS_need_more_data);
  3573        first = FALSE;
  3574     }
  3575     return TRUE;
  3576  }
  3577  #endif // !STB_VORBIS_NO_PUSHDATA_API
  3578  
  3579  static int stbv_start_decoder(stbv_vorb *f)
  3580  {
  3581     stbv_uint8 header[6], x,y;
  3582     int len,i,j,k, max_submaps = 0;
  3583     int longest_floorlist=0;
  3584  
  3585     // first page, first packet
  3586  
  3587     if (!stbv_start_page(f))                              return FALSE;
  3588     // validate page flag
  3589     if (!(f->page_flag & STBV_PAGEFLAG_first_page))       return stbv_error(f, VORBIS_invalid_first_page);
  3590     if (f->page_flag & STBV_PAGEFLAG_last_page)           return stbv_error(f, VORBIS_invalid_first_page);
  3591     if (f->page_flag & STBV_PAGEFLAG_continued_packet)    return stbv_error(f, VORBIS_invalid_first_page);
  3592     // check for expected packet length
  3593     if (f->segment_count != 1)                       return stbv_error(f, VORBIS_invalid_first_page);
  3594     if (f->segments[0] != 30)                        return stbv_error(f, VORBIS_invalid_first_page);
  3595     // read packet
  3596     // check packet header
  3597     if (stbv_get8(f) != STBV_VORBIS_packet_id)                 return stbv_error(f, VORBIS_invalid_first_page);
  3598     if (!stbv_getn(f, header, 6))                         return stbv_error(f, VORBIS_unexpected_eof);
  3599     if (!stbv_vorbis_validate(header))                    return stbv_error(f, VORBIS_invalid_first_page);
  3600     // vorbis_version
  3601     if (stbv_get32(f) != 0)                               return stbv_error(f, VORBIS_invalid_first_page);
  3602     f->channels = stbv_get8(f); if (!f->channels)         return stbv_error(f, VORBIS_invalid_first_page);
  3603     if (f->channels > STB_VORBIS_MAX_CHANNELS)       return stbv_error(f, VORBIS_too_many_channels);
  3604     f->sample_rate = stbv_get32(f); if (!f->sample_rate)  return stbv_error(f, VORBIS_invalid_first_page);
  3605     stbv_get32(f); // bitrate_maximum
  3606     stbv_get32(f); // bitrate_nominal
  3607     stbv_get32(f); // bitrate_minimum
  3608     x = stbv_get8(f);
  3609     {
  3610        int log0,log1;
  3611        log0 = x & 15;
  3612        log1 = x >> 4;
  3613        f->blocksize_0 = 1 << log0;
  3614        f->blocksize_1 = 1 << log1;
  3615        if (log0 < 6 || log0 > 13)                       return stbv_error(f, VORBIS_invalid_setup);
  3616        if (log1 < 6 || log1 > 13)                       return stbv_error(f, VORBIS_invalid_setup);
  3617        if (log0 > log1)                                 return stbv_error(f, VORBIS_invalid_setup);
  3618     }
  3619  
  3620     // framing_flag
  3621     x = stbv_get8(f);
  3622     if (!(x & 1))                                    return stbv_error(f, VORBIS_invalid_first_page);
  3623  
  3624     // second packet!
  3625     if (!stbv_start_page(f))                              return FALSE;
  3626  
  3627     if (!stbv_start_packet(f))                            return FALSE;
  3628     do {
  3629        len = stbv_next_segment(f);
  3630        stbv_skip(f, len);
  3631        f->bytes_in_seg = 0;
  3632     } while (len);
  3633  
  3634     // third packet!
  3635     if (!stbv_start_packet(f))                            return FALSE;
  3636  
  3637     #ifndef STB_VORBIS_NO_PUSHDATA_API
  3638     if (STBV_IS_PUSH_MODE(f)) {
  3639        if (!stbv_is_whole_packet_present(f, TRUE)) {
  3640           // convert error in ogg header to write type
  3641           if (f->error == VORBIS_invalid_stream)
  3642              f->error = VORBIS_invalid_setup;
  3643           return FALSE;
  3644        }
  3645     }
  3646     #endif
  3647  
  3648     stbv_crc32_init(); // always init it, to avoid multithread race conditions
  3649  
  3650     if (stbv_get8_packet(f) != STBV_VORBIS_packet_setup)       return stbv_error(f, VORBIS_invalid_setup);
  3651     for (i=0; i < 6; ++i) header[i] = stbv_get8_packet(f);
  3652     if (!stbv_vorbis_validate(header))                    return stbv_error(f, VORBIS_invalid_setup);
  3653  
  3654     // codebooks
  3655  
  3656     f->codebook_count = stbv_get_bits(f,8) + 1;
  3657     f->codebooks = (StbvCodebook *) stbv_setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count);
  3658     if (f->codebooks == NULL)                        return stbv_error(f, VORBIS_outofmem);
  3659     memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count);
  3660     for (i=0; i < f->codebook_count; ++i) {
  3661        stbv_uint32 *values;
  3662        int ordered, sorted_count;
  3663        int total=0;
  3664        stbv_uint8 *lengths;
  3665        StbvCodebook *c = f->codebooks+i;
  3666        STBV_CHECK(f);
  3667        x = stbv_get_bits(f, 8); if (x != 0x42)            return stbv_error(f, VORBIS_invalid_setup);
  3668        x = stbv_get_bits(f, 8); if (x != 0x43)            return stbv_error(f, VORBIS_invalid_setup);
  3669        x = stbv_get_bits(f, 8); if (x != 0x56)            return stbv_error(f, VORBIS_invalid_setup);
  3670        x = stbv_get_bits(f, 8);
  3671        c->dimensions = (stbv_get_bits(f, 8)<<8) + x;
  3672        x = stbv_get_bits(f, 8);
  3673        y = stbv_get_bits(f, 8);
  3674        c->entries = (stbv_get_bits(f, 8)<<16) + (y<<8) + x;
  3675        ordered = stbv_get_bits(f,1);
  3676        c->sparse = ordered ? 0 : stbv_get_bits(f,1);
  3677  
  3678        if (c->dimensions == 0 && c->entries != 0)    return stbv_error(f, VORBIS_invalid_setup);
  3679  
  3680        if (c->sparse)
  3681           lengths = (stbv_uint8 *) stbv_setup_temp_malloc(f, c->entries);
  3682        else
  3683           lengths = c->codeword_lengths = (stbv_uint8 *) stbv_setup_malloc(f, c->entries);
  3684  
  3685        if (!lengths) return stbv_error(f, VORBIS_outofmem);
  3686  
  3687        if (ordered) {
  3688           int current_entry = 0;
  3689           int current_length = stbv_get_bits(f,5) + 1;
  3690           while (current_entry < c->entries) {
  3691              int limit = c->entries - current_entry;
  3692              int n = stbv_get_bits(f, stbv_ilog(limit));
  3693              if (current_entry + n > (int) c->entries) { return stbv_error(f, VORBIS_invalid_setup); }
  3694              memset(lengths + current_entry, current_length, n);
  3695              current_entry += n;
  3696              ++current_length;
  3697           }
  3698        } else {
  3699           for (j=0; j < c->entries; ++j) {
  3700              int present = c->sparse ? stbv_get_bits(f,1) : 1;
  3701              if (present) {
  3702                 lengths[j] = stbv_get_bits(f, 5) + 1;
  3703                 ++total;
  3704                 if (lengths[j] == 32)
  3705                    return stbv_error(f, VORBIS_invalid_setup);
  3706              } else {
  3707                 lengths[j] = NO_CODE;
  3708              }
  3709           }
  3710        }
  3711  
  3712        if (c->sparse && total >= c->entries >> 2) {
  3713           // convert sparse items to non-sparse!
  3714           if (c->entries > (int) f->setup_temp_memory_required)
  3715              f->setup_temp_memory_required = c->entries;
  3716  
  3717           c->codeword_lengths = (stbv_uint8 *) stbv_setup_malloc(f, c->entries);
  3718           if (c->codeword_lengths == NULL) return stbv_error(f, VORBIS_outofmem);
  3719           memcpy(c->codeword_lengths, lengths, c->entries);
  3720           stbv_setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
  3721           lengths = c->codeword_lengths;
  3722           c->sparse = 0;
  3723        }
  3724  
  3725        // compute the size of the sorted tables
  3726        if (c->sparse) {
  3727           sorted_count = total;
  3728        } else {
  3729           sorted_count = 0;
  3730           #ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
  3731           for (j=0; j < c->entries; ++j)
  3732              if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE)
  3733                 ++sorted_count;
  3734           #endif
  3735        }
  3736  
  3737        c->sorted_entries = sorted_count;
  3738        values = NULL;
  3739  
  3740        STBV_CHECK(f);
  3741        if (!c->sparse) {
  3742           c->codewords = (stbv_uint32 *) stbv_setup_malloc(f, sizeof(c->codewords[0]) * c->entries);
  3743           if (!c->codewords)                  return stbv_error(f, VORBIS_outofmem);
  3744        } else {
  3745           unsigned int size;
  3746           if (c->sorted_entries) {
  3747              c->codeword_lengths = (stbv_uint8 *) stbv_setup_malloc(f, c->sorted_entries);
  3748              if (!c->codeword_lengths)           return stbv_error(f, VORBIS_outofmem);
  3749              c->codewords = (stbv_uint32 *) stbv_setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries);
  3750              if (!c->codewords)                  return stbv_error(f, VORBIS_outofmem);
  3751              values = (stbv_uint32 *) stbv_setup_temp_malloc(f, sizeof(*values) * c->sorted_entries);
  3752              if (!values)                        return stbv_error(f, VORBIS_outofmem);
  3753           }
  3754           size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries;
  3755           if (size > f->setup_temp_memory_required)
  3756              f->setup_temp_memory_required = size;
  3757        }
  3758  
  3759        if (!stbv_compute_codewords(c, lengths, c->entries, values)) {
  3760           if (c->sparse) stbv_setup_temp_free(f, values, 0);
  3761           return stbv_error(f, VORBIS_invalid_setup);
  3762        }
  3763  
  3764        if (c->sorted_entries) {
  3765           // allocate an extra slot for sentinels
  3766           c->sorted_codewords = (stbv_uint32 *) stbv_setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries+1));
  3767           if (c->sorted_codewords == NULL) return stbv_error(f, VORBIS_outofmem);
  3768           // allocate an extra slot at the front so that c->sorted_values[-1] is defined
  3769           // so that we can catch that case without an extra if
  3770           c->sorted_values    = ( int   *) stbv_setup_malloc(f, sizeof(*c->sorted_values   ) * (c->sorted_entries+1));
  3771           if (c->sorted_values == NULL) return stbv_error(f, VORBIS_outofmem);
  3772           ++c->sorted_values;
  3773           c->sorted_values[-1] = -1;
  3774           stbv_compute_sorted_huffman(c, lengths, values);
  3775        }
  3776  
  3777        if (c->sparse) {
  3778           stbv_setup_temp_free(f, values, sizeof(*values)*c->sorted_entries);
  3779           stbv_setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries);
  3780           stbv_setup_temp_free(f, lengths, c->entries);
  3781           c->codewords = NULL;
  3782        }
  3783  
  3784        stbv_compute_accelerated_huffman(c);
  3785  
  3786        STBV_CHECK(f);
  3787        c->lookup_type = stbv_get_bits(f, 4);
  3788        if (c->lookup_type > 2) return stbv_error(f, VORBIS_invalid_setup);
  3789        if (c->lookup_type > 0) {
  3790           stbv_uint16 *mults;
  3791           c->minimum_value = stbv_float32_unpack(stbv_get_bits(f, 32));
  3792           c->delta_value = stbv_float32_unpack(stbv_get_bits(f, 32));
  3793           c->value_bits = stbv_get_bits(f, 4)+1;
  3794           c->sequence_p = stbv_get_bits(f,1);
  3795           if (c->lookup_type == 1) {
  3796              c->lookup_values = stbv_lookup1_values(c->entries, c->dimensions);
  3797           } else {
  3798              c->lookup_values = c->entries * c->dimensions;
  3799           }
  3800           if (c->lookup_values == 0) return stbv_error(f, VORBIS_invalid_setup);
  3801           mults = (stbv_uint16 *) stbv_setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values);
  3802           if (mults == NULL) return stbv_error(f, VORBIS_outofmem);
  3803           for (j=0; j < (int) c->lookup_values; ++j) {
  3804              int q = stbv_get_bits(f, c->value_bits);
  3805              if (q == STBV_EOP) { stbv_setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return stbv_error(f, VORBIS_invalid_setup); }
  3806              mults[j] = q;
  3807           }
  3808  
  3809  #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  3810           if (c->lookup_type == 1) {
  3811              int len, sparse = c->sparse;
  3812              float last=0;
  3813              // pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop
  3814              if (sparse) {
  3815                 if (c->sorted_entries == 0) goto stbv_skip;
  3816                 c->multiplicands = (stbv_codetype *) stbv_setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
  3817              } else
  3818                 c->multiplicands = (stbv_codetype *) stbv_setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries        * c->dimensions);
  3819              if (c->multiplicands == NULL) { stbv_setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values); return stbv_error(f, VORBIS_outofmem); }
  3820              len = sparse ? c->sorted_entries : c->entries;
  3821              for (j=0; j < len; ++j) {
  3822                 unsigned int z = sparse ? c->sorted_values[j] : j;
  3823                 unsigned int div=1;
  3824                 for (k=0; k < c->dimensions; ++k) {
  3825                    int off = (z / div) % c->lookup_values;
  3826                    float val = mults[off];
  3827                    val = mults[off]*c->delta_value + c->minimum_value + last;
  3828                    c->multiplicands[j*c->dimensions + k] = val;
  3829                    if (c->sequence_p)
  3830                       last = val;
  3831                    if (k+1 < c->dimensions) {
  3832                       if (div > UINT_MAX / (unsigned int) c->lookup_values) {
  3833                          stbv_setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
  3834                          return stbv_error(f, VORBIS_invalid_setup);
  3835                       }
  3836                       div *= c->lookup_values;
  3837                    }
  3838                 }
  3839              }
  3840              c->lookup_type = 2;
  3841           }
  3842           else
  3843  #endif
  3844           {
  3845              float last=0;
  3846              STBV_CHECK(f);
  3847              c->multiplicands = (stbv_codetype *) stbv_setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
  3848              if (c->multiplicands == NULL) { stbv_setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values); return stbv_error(f, VORBIS_outofmem); }
  3849              for (j=0; j < (int) c->lookup_values; ++j) {
  3850                 float val = mults[j] * c->delta_value + c->minimum_value + last;
  3851                 c->multiplicands[j] = val;
  3852                 if (c->sequence_p)
  3853                    last = val;
  3854              }
  3855           }
  3856  #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
  3857          stbv_skip:;
  3858  #endif
  3859           stbv_setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
  3860  
  3861           STBV_CHECK(f);
  3862        }
  3863        STBV_CHECK(f);
  3864     }
  3865  
  3866     // time domain transfers (notused)
  3867  
  3868     x = stbv_get_bits(f, 6) + 1;
  3869     for (i=0; i < x; ++i) {
  3870        stbv_uint32 z = stbv_get_bits(f, 16);
  3871        if (z != 0) return stbv_error(f, VORBIS_invalid_setup);
  3872     }
  3873  
  3874     // Floors
  3875     f->floor_count = stbv_get_bits(f, 6)+1;
  3876     f->floor_config = (StbvFloor *)  stbv_setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
  3877     if (f->floor_config == NULL) return stbv_error(f, VORBIS_outofmem);
  3878     for (i=0; i < f->floor_count; ++i) {
  3879        f->floor_types[i] = stbv_get_bits(f, 16);
  3880        if (f->floor_types[i] > 1) return stbv_error(f, VORBIS_invalid_setup);
  3881        if (f->floor_types[i] == 0) {
  3882           StbvFloor0 *g = &f->floor_config[i].floor0;
  3883           g->order = stbv_get_bits(f,8);
  3884           g->rate = stbv_get_bits(f,16);
  3885           g->bark_map_size = stbv_get_bits(f,16);
  3886           g->amplitude_bits = stbv_get_bits(f,6);
  3887           g->amplitude_offset = stbv_get_bits(f,8);
  3888           g->number_of_books = stbv_get_bits(f,4) + 1;
  3889           for (j=0; j < g->number_of_books; ++j)
  3890              g->book_list[j] = stbv_get_bits(f,8);
  3891           return stbv_error(f, VORBIS_feature_not_supported);
  3892        } else {
  3893           stbv_floor_ordering p[31*8+2];
  3894           StbvFloor1 *g = &f->floor_config[i].floor1;
  3895           int max_class = -1; 
  3896           g->partitions = stbv_get_bits(f, 5);
  3897           for (j=0; j < g->partitions; ++j) {
  3898              g->partition_class_list[j] = stbv_get_bits(f, 4);
  3899              if (g->partition_class_list[j] > max_class)
  3900                 max_class = g->partition_class_list[j];
  3901           }
  3902           for (j=0; j <= max_class; ++j) {
  3903              g->class_dimensions[j] = stbv_get_bits(f, 3)+1;
  3904              g->class_subclasses[j] = stbv_get_bits(f, 2);
  3905              if (g->class_subclasses[j]) {
  3906                 g->class_masterbooks[j] = stbv_get_bits(f, 8);
  3907                 if (g->class_masterbooks[j] >= f->codebook_count) return stbv_error(f, VORBIS_invalid_setup);
  3908              }
  3909              for (k=0; k < 1 << g->class_subclasses[j]; ++k) {
  3910                 g->subclass_books[j][k] = stbv_get_bits(f,8)-1;
  3911                 if (g->subclass_books[j][k] >= f->codebook_count) return stbv_error(f, VORBIS_invalid_setup);
  3912              }
  3913           }
  3914           g->floor1_multiplier = stbv_get_bits(f,2)+1;
  3915           g->rangebits = stbv_get_bits(f,4);
  3916           g->Xlist[0] = 0;
  3917           g->Xlist[1] = 1 << g->rangebits;
  3918           g->values = 2;
  3919           for (j=0; j < g->partitions; ++j) {
  3920              int c = g->partition_class_list[j];
  3921              for (k=0; k < g->class_dimensions[c]; ++k) {
  3922                 g->Xlist[g->values] = stbv_get_bits(f, g->rangebits);
  3923                 ++g->values;
  3924              }
  3925           }
  3926           // precompute the sorting
  3927           for (j=0; j < g->values; ++j) {
  3928              p[j].x = g->Xlist[j];
  3929              p[j].id = j;
  3930           }
  3931           qsort(p, g->values, sizeof(p[0]), stbv_point_compare);
  3932           for (j=0; j < g->values; ++j)
  3933              g->sorted_order[j] = (stbv_uint8) p[j].id;
  3934           // precompute the stbv_neighbors
  3935           for (j=2; j < g->values; ++j) {
  3936              int low,hi;
  3937              stbv_neighbors(g->Xlist, j, &low,&hi);
  3938              g->stbv_neighbors[j][0] = low;
  3939              g->stbv_neighbors[j][1] = hi;
  3940           }
  3941  
  3942           if (g->values > longest_floorlist)
  3943              longest_floorlist = g->values;
  3944        }
  3945     }
  3946  
  3947     // StbvResidue
  3948     f->residue_count = stbv_get_bits(f, 6)+1;
  3949     f->residue_config = (StbvResidue *) stbv_setup_malloc(f, f->residue_count * sizeof(f->residue_config[0]));
  3950     if (f->residue_config == NULL) return stbv_error(f, VORBIS_outofmem);
  3951     memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0]));
  3952     for (i=0; i < f->residue_count; ++i) {
  3953        stbv_uint8 residue_cascade[64];
  3954        StbvResidue *r = f->residue_config+i;
  3955        f->residue_types[i] = stbv_get_bits(f, 16);
  3956        if (f->residue_types[i] > 2) return stbv_error(f, VORBIS_invalid_setup);
  3957        r->begin = stbv_get_bits(f, 24);
  3958        r->end = stbv_get_bits(f, 24);
  3959        if (r->end < r->begin) return stbv_error(f, VORBIS_invalid_setup);
  3960        r->part_size = stbv_get_bits(f,24)+1;
  3961        r->classifications = stbv_get_bits(f,6)+1;
  3962        r->classbook = stbv_get_bits(f,8);
  3963        if (r->classbook >= f->codebook_count) return stbv_error(f, VORBIS_invalid_setup);
  3964        for (j=0; j < r->classifications; ++j) {
  3965           stbv_uint8 high_bits=0;
  3966           stbv_uint8 low_bits=stbv_get_bits(f,3);
  3967           if (stbv_get_bits(f,1))
  3968              high_bits = stbv_get_bits(f,5);
  3969           residue_cascade[j] = high_bits*8 + low_bits;
  3970        }
  3971        r->residue_books = (short (*)[8]) stbv_setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications);
  3972        if (r->residue_books == NULL) return stbv_error(f, VORBIS_outofmem);
  3973        for (j=0; j < r->classifications; ++j) {
  3974           for (k=0; k < 8; ++k) {
  3975              if (residue_cascade[j] & (1 << k)) {
  3976                 r->residue_books[j][k] = stbv_get_bits(f, 8);
  3977                 if (r->residue_books[j][k] >= f->codebook_count) return stbv_error(f, VORBIS_invalid_setup);
  3978              } else {
  3979                 r->residue_books[j][k] = -1;
  3980              }
  3981           }
  3982        }
  3983        // precompute the classifications[] array to avoid inner-loop mod/divide
  3984        // call it 'classdata' since we already have r->classifications
  3985        r->classdata = (stbv_uint8 **) stbv_setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
  3986        if (!r->classdata) return stbv_error(f, VORBIS_outofmem);
  3987        memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
  3988        for (j=0; j < f->codebooks[r->classbook].entries; ++j) {
  3989           int classwords = f->codebooks[r->classbook].dimensions;
  3990           int temp = j;
  3991           r->classdata[j] = (stbv_uint8 *) stbv_setup_malloc(f, sizeof(r->classdata[j][0]) * classwords);
  3992           if (r->classdata[j] == NULL) return stbv_error(f, VORBIS_outofmem);
  3993           for (k=classwords-1; k >= 0; --k) {
  3994              r->classdata[j][k] = temp % r->classifications;
  3995              temp /= r->classifications;
  3996           }
  3997        }
  3998     }
  3999  
  4000     f->mapping_count = stbv_get_bits(f,6)+1;
  4001     f->mapping = (StbvMapping *) stbv_setup_malloc(f, f->mapping_count * sizeof(*f->mapping));
  4002     if (f->mapping == NULL) return stbv_error(f, VORBIS_outofmem);
  4003     memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping));
  4004     for (i=0; i < f->mapping_count; ++i) {
  4005        StbvMapping *m = f->mapping + i;      
  4006        int mapping_type = stbv_get_bits(f,16);
  4007        if (mapping_type != 0) return stbv_error(f, VORBIS_invalid_setup);
  4008        m->chan = (StbvMappingChannel *) stbv_setup_malloc(f, f->channels * sizeof(*m->chan));
  4009        if (m->chan == NULL) return stbv_error(f, VORBIS_outofmem);
  4010        if (stbv_get_bits(f,1))
  4011           m->submaps = stbv_get_bits(f,4)+1;
  4012        else
  4013           m->submaps = 1;
  4014        if (m->submaps > max_submaps)
  4015           max_submaps = m->submaps;
  4016        if (stbv_get_bits(f,1)) {
  4017           m->coupling_steps = stbv_get_bits(f,8)+1;
  4018           for (k=0; k < m->coupling_steps; ++k) {
  4019              m->chan[k].magnitude = stbv_get_bits(f, stbv_ilog(f->channels-1));
  4020              m->chan[k].angle = stbv_get_bits(f, stbv_ilog(f->channels-1));
  4021              if (m->chan[k].magnitude >= f->channels)        return stbv_error(f, VORBIS_invalid_setup);
  4022              if (m->chan[k].angle     >= f->channels)        return stbv_error(f, VORBIS_invalid_setup);
  4023              if (m->chan[k].magnitude == m->chan[k].angle)   return stbv_error(f, VORBIS_invalid_setup);
  4024           }
  4025        } else
  4026           m->coupling_steps = 0;
  4027  
  4028        // reserved field
  4029        if (stbv_get_bits(f,2)) return stbv_error(f, VORBIS_invalid_setup);
  4030        if (m->submaps > 1) {
  4031           for (j=0; j < f->channels; ++j) {
  4032              m->chan[j].mux = stbv_get_bits(f, 4);
  4033              if (m->chan[j].mux >= m->submaps)                return stbv_error(f, VORBIS_invalid_setup);
  4034           }
  4035        } else
  4036           // @SPECIFICATION: this case is missing from the spec
  4037           for (j=0; j < f->channels; ++j)
  4038              m->chan[j].mux = 0;
  4039  
  4040        for (j=0; j < m->submaps; ++j) {
  4041           stbv_get_bits(f,8); // discard
  4042           m->submap_floor[j] = stbv_get_bits(f,8);
  4043           m->submap_residue[j] = stbv_get_bits(f,8);
  4044           if (m->submap_floor[j] >= f->floor_count)      return stbv_error(f, VORBIS_invalid_setup);
  4045           if (m->submap_residue[j] >= f->residue_count)  return stbv_error(f, VORBIS_invalid_setup);
  4046        }
  4047     }
  4048  
  4049     // Modes
  4050     f->mode_count = stbv_get_bits(f, 6)+1;
  4051     for (i=0; i < f->mode_count; ++i) {
  4052        StbvMode *m = f->mode_config+i;
  4053        m->blockflag = stbv_get_bits(f,1);
  4054        m->windowtype = stbv_get_bits(f,16);
  4055        m->transformtype = stbv_get_bits(f,16);
  4056        m->mapping = stbv_get_bits(f,8);
  4057        if (m->windowtype != 0)                 return stbv_error(f, VORBIS_invalid_setup);
  4058        if (m->transformtype != 0)              return stbv_error(f, VORBIS_invalid_setup);
  4059        if (m->mapping >= f->mapping_count)     return stbv_error(f, VORBIS_invalid_setup);
  4060     }
  4061  
  4062     stbv_flush_packet(f);
  4063  
  4064     f->previous_length = 0;
  4065  
  4066     for (i=0; i < f->channels; ++i) {
  4067        f->channel_buffers[i] = (float *) stbv_setup_malloc(f, sizeof(float) * f->blocksize_1);
  4068        f->previous_window[i] = (float *) stbv_setup_malloc(f, sizeof(float) * f->blocksize_1/2);
  4069        f->finalY[i]          = (stbv_int16 *) stbv_setup_malloc(f, sizeof(stbv_int16) * longest_floorlist);
  4070        if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) return stbv_error(f, VORBIS_outofmem);
  4071        memset(f->channel_buffers[i], 0, sizeof(float) * f->blocksize_1);
  4072        #ifdef STB_VORBIS_NO_DEFER_FLOOR
  4073        f->floor_buffers[i]   = (float *) stbv_setup_malloc(f, sizeof(float) * f->blocksize_1/2);
  4074        if (f->floor_buffers[i] == NULL) return stbv_error(f, VORBIS_outofmem);
  4075        #endif
  4076     }
  4077  
  4078     if (!stbv_init_blocksize(f, 0, f->blocksize_0)) return FALSE;
  4079     if (!stbv_init_blocksize(f, 1, f->blocksize_1)) return FALSE;
  4080     f->blocksize[0] = f->blocksize_0;
  4081     f->blocksize[1] = f->blocksize_1;
  4082  
  4083  #ifdef STB_VORBIS_DIVIDE_TABLE
  4084     if (stbv_integer_divide_table[1][1]==0)
  4085        for (i=0; i < STBV_DIVTAB_NUMER; ++i)
  4086           for (j=1; j < STBV_DIVTAB_DENOM; ++j)
  4087              stbv_integer_divide_table[i][j] = i / j;
  4088  #endif
  4089  
  4090     // compute how much temporary memory is needed
  4091  
  4092     // 1.
  4093     {
  4094        stbv_uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1);
  4095        stbv_uint32 classify_mem;
  4096        int i,max_part_read=0;
  4097        for (i=0; i < f->residue_count; ++i) {
  4098           StbvResidue *r = f->residue_config + i;
  4099           unsigned int actual_size = f->blocksize_1 / 2;
  4100           unsigned int limit_r_begin = r->begin < actual_size ? r->begin : actual_size;
  4101           unsigned int limit_r_end   = r->end   < actual_size ? r->end   : actual_size;
  4102           int n_read = limit_r_end - limit_r_begin;
  4103           int part_read = n_read / r->part_size;
  4104           if (part_read > max_part_read)
  4105              max_part_read = part_read;
  4106        }
  4107        #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
  4108        classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(stbv_uint8 *));
  4109        #else
  4110        classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *));
  4111        #endif
  4112  
  4113        // maximum reasonable partition size is f->blocksize_1
  4114  
  4115        f->temp_memory_required = classify_mem;
  4116        if (imdct_mem > f->temp_memory_required)
  4117           f->temp_memory_required = imdct_mem;
  4118     }
  4119  
  4120     f->first_decode = TRUE;
  4121  
  4122     if (f->alloc.alloc_buffer) {
  4123        assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);
  4124        // check if there's enough temp memory so we don't error later
  4125        if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset)
  4126           return stbv_error(f, VORBIS_outofmem);
  4127     }
  4128  
  4129     f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
  4130  
  4131     return TRUE;
  4132  }
  4133  
  4134  static void stbv_vorbis_deinit(stb_vorbis *p)
  4135  {
  4136     int i,j;
  4137     if (p->residue_config) {
  4138        for (i=0; i < p->residue_count; ++i) {
  4139           StbvResidue *r = p->residue_config+i;
  4140           if (r->classdata) {
  4141              for (j=0; j < p->codebooks[r->classbook].entries; ++j)
  4142                 stbv_setup_free(p, r->classdata[j]);
  4143              stbv_setup_free(p, r->classdata);
  4144           }
  4145           stbv_setup_free(p, r->residue_books);
  4146        }
  4147     }
  4148  
  4149     if (p->codebooks) {
  4150        STBV_CHECK(p);
  4151        for (i=0; i < p->codebook_count; ++i) {
  4152           StbvCodebook *c = p->codebooks + i;
  4153           stbv_setup_free(p, c->codeword_lengths);
  4154           stbv_setup_free(p, c->multiplicands);
  4155           stbv_setup_free(p, c->codewords);
  4156           stbv_setup_free(p, c->sorted_codewords);
  4157           // c->sorted_values[-1] is the first entry in the array
  4158           stbv_setup_free(p, c->sorted_values ? c->sorted_values-1 : NULL);
  4159        }
  4160        stbv_setup_free(p, p->codebooks);
  4161     }
  4162     stbv_setup_free(p, p->floor_config);
  4163     stbv_setup_free(p, p->residue_config);
  4164     if (p->mapping) {
  4165        for (i=0; i < p->mapping_count; ++i)
  4166           stbv_setup_free(p, p->mapping[i].chan);
  4167        stbv_setup_free(p, p->mapping);
  4168     }
  4169     STBV_CHECK(p);
  4170     for (i=0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) {
  4171        stbv_setup_free(p, p->channel_buffers[i]);
  4172        stbv_setup_free(p, p->previous_window[i]);
  4173        #ifdef STB_VORBIS_NO_DEFER_FLOOR
  4174        stbv_setup_free(p, p->floor_buffers[i]);
  4175        #endif
  4176        stbv_setup_free(p, p->finalY[i]);
  4177     }
  4178     for (i=0; i < 2; ++i) {
  4179        stbv_setup_free(p, p->A[i]);
  4180        stbv_setup_free(p, p->B[i]);
  4181        stbv_setup_free(p, p->C[i]);
  4182        stbv_setup_free(p, p->window[i]);
  4183        stbv_setup_free(p, p->stbv_bit_reverse[i]);
  4184     }
  4185     #ifndef STB_VORBIS_NO_STDIO
  4186     if (p->close_on_free) fclose(p->f);
  4187     #endif
  4188  }
  4189  
  4190  STBVDEF void stb_vorbis_close(stb_vorbis *p)
  4191  {
  4192     if (p == NULL) return;
  4193     stbv_vorbis_deinit(p);
  4194     stbv_setup_free(p,p);
  4195  }
  4196  
  4197  static void stbv_vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z)
  4198  {
  4199     memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start
  4200     if (z) {
  4201        p->alloc = *z;
  4202        p->alloc.alloc_buffer_length_in_bytes = (p->alloc.alloc_buffer_length_in_bytes+3) & ~3;
  4203        p->temp_offset = p->alloc.alloc_buffer_length_in_bytes;
  4204     }
  4205     p->eof = 0;
  4206     p->error = VORBIS__no_error;
  4207     p->stream = NULL;
  4208     p->codebooks = NULL;
  4209     p->page_crc_tests = -1;
  4210     #ifndef STB_VORBIS_NO_STDIO
  4211     p->close_on_free = FALSE;
  4212     p->f = NULL;
  4213     #endif
  4214  }
  4215  
  4216  STBVDEF int stb_vorbis_get_sample_offset(stb_vorbis *f)
  4217  {
  4218     if (f->current_loc_valid)
  4219        return f->current_loc;
  4220     else
  4221        return -1;
  4222  }
  4223  
  4224  STBVDEF stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f)
  4225  {
  4226     stb_vorbis_info d;
  4227     d.channels = f->channels;
  4228     d.sample_rate = f->sample_rate;
  4229     d.setup_memory_required = f->setup_memory_required;
  4230     d.setup_temp_memory_required = f->setup_temp_memory_required;
  4231     d.temp_memory_required = f->temp_memory_required;
  4232     d.max_frame_size = f->blocksize_1 >> 1;
  4233     return d;
  4234  }
  4235  
  4236  STBVDEF int stb_vorbis_get_error(stb_vorbis *f)
  4237  {
  4238     int e = f->error;
  4239     f->error = VORBIS__no_error;
  4240     return e;
  4241  }
  4242  
  4243  static stb_vorbis * stbv_vorbis_alloc(stb_vorbis *f)
  4244  {
  4245     stb_vorbis *p = (stb_vorbis *) stbv_setup_malloc(f, sizeof(*p));
  4246     return p;
  4247  }
  4248  
  4249  #ifndef STB_VORBIS_NO_PUSHDATA_API
  4250  
  4251  STBVDEF void stb_vorbis_flush_pushdata(stb_vorbis *f)
  4252  {
  4253     f->previous_length = 0;
  4254     f->page_crc_tests  = 0;
  4255     f->discard_samples_deferred = 0;
  4256     f->current_loc_valid = FALSE;
  4257     f->first_decode = FALSE;
  4258     f->samples_output = 0;
  4259     f->channel_buffer_start = 0;
  4260     f->channel_buffer_end = 0;
  4261  }
  4262  
  4263  static int stbv_vorbis_search_for_page_pushdata(stbv_vorb *f, stbv_uint8 *data, int data_len)
  4264  {
  4265     int i,n;
  4266     for (i=0; i < f->page_crc_tests; ++i)
  4267        f->scan[i].bytes_done = 0;
  4268  
  4269     // if we have room for more scans, search for them first, because
  4270     // they may cause us to stop early if their header is incomplete
  4271     if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) {
  4272        if (data_len < 4) return 0;
  4273        data_len -= 3; // need to look for 4-byte sequence, so don't miss
  4274                       // one that straddles a boundary
  4275        for (i=0; i < data_len; ++i) {
  4276           if (data[i] == 0x4f) {
  4277              if (0==memcmp(data+i, stbv_ogg_page_header, 4)) {
  4278                 int j,len;
  4279                 stbv_uint32 crc;
  4280                 // make sure we have the whole page header
  4281                 if (i+26 >= data_len || i+27+data[i+26] >= data_len) {
  4282                    // only read up to this page start, so hopefully we'll
  4283                    // have the whole page header start next time
  4284                    data_len = i;
  4285                    break;
  4286                 }
  4287                 // ok, we have it all; compute the length of the page
  4288                 len = 27 + data[i+26];
  4289                 for (j=0; j < data[i+26]; ++j)
  4290                    len += data[i+27+j];
  4291                 // scan everything up to the embedded crc (which we must 0)
  4292                 crc = 0;
  4293                 for (j=0; j < 22; ++j)
  4294                    crc = stbv_crc32_update(crc, data[i+j]);
  4295                 // now process 4 0-bytes
  4296                 for (   ; j < 26; ++j)
  4297                    crc = stbv_crc32_update(crc, 0);
  4298                 // len is the total number of bytes we need to scan
  4299                 n = f->page_crc_tests++;
  4300                 f->scan[n].bytes_left = len-j;
  4301                 f->scan[n].crc_so_far = crc;
  4302                 f->scan[n].goal_crc = data[i+22] + (data[i+23] << 8) + (data[i+24]<<16) + (data[i+25]<<24);
  4303                 // if the last frame on a page is continued to the next, then
  4304                 // we can't recover the sample_loc immediately
  4305                 if (data[i+27+data[i+26]-1] == 255)
  4306                    f->scan[n].sample_loc = ~0;
  4307                 else
  4308                    f->scan[n].sample_loc = data[i+6] + (data[i+7] << 8) + (data[i+ 8]<<16) + (data[i+ 9]<<24);
  4309                 f->scan[n].bytes_done = i+j;
  4310                 if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT)
  4311                    break;
  4312                 // keep going if we still have room for more
  4313              }
  4314           }
  4315        }
  4316     }
  4317  
  4318     for (i=0; i < f->page_crc_tests;) {
  4319        stbv_uint32 crc;
  4320        int j;
  4321        int n = f->scan[i].bytes_done;
  4322        int m = f->scan[i].bytes_left;
  4323        if (m > data_len - n) m = data_len - n;
  4324        // m is the bytes to scan in the current chunk
  4325        crc = f->scan[i].crc_so_far;
  4326        for (j=0; j < m; ++j)
  4327           crc = stbv_crc32_update(crc, data[n+j]);
  4328        f->scan[i].bytes_left -= m;
  4329        f->scan[i].crc_so_far = crc;
  4330        if (f->scan[i].bytes_left == 0) {
  4331           // does it match?
  4332           if (f->scan[i].crc_so_far == f->scan[i].goal_crc) {
  4333              // Houston, we have page
  4334              data_len = n+m; // consumption amount is wherever that scan ended
  4335              f->page_crc_tests = -1; // drop out of page scan mode
  4336              f->previous_length = 0; // decode-but-don't-output one frame
  4337              f->next_seg = -1;       // start a new page
  4338              f->current_loc = f->scan[i].sample_loc; // set the current sample location
  4339                                      // to the amount we'd have decoded had we decoded this page
  4340              f->current_loc_valid = f->current_loc != ~0U;
  4341              return data_len;
  4342           }
  4343           // delete entry
  4344           f->scan[i] = f->scan[--f->page_crc_tests];
  4345        } else {
  4346           ++i;
  4347        }
  4348     }
  4349  
  4350     return data_len;
  4351  }
  4352  
  4353  // return value: number of bytes we used
  4354  STBVDEF int stb_vorbis_decode_frame_pushdata(
  4355           stb_vorbis *f,                   // the file we're decoding
  4356           const stbv_uint8 *data, int data_len, // the memory available for decoding
  4357           int *channels,                   // place to write number of float * buffers
  4358           float ***output,                 // place to write float ** array of float * buffers
  4359           int *samples                     // place to write number of output samples
  4360       )
  4361  {
  4362     int i;
  4363     int len,right,left;
  4364  
  4365     if (!STBV_IS_PUSH_MODE(f)) return stbv_error(f, VORBIS_invalid_api_mixing);
  4366  
  4367     if (f->page_crc_tests >= 0) {
  4368        *samples = 0;
  4369        return stbv_vorbis_search_for_page_pushdata(f, (stbv_uint8 *) data, data_len);
  4370     }
  4371  
  4372     f->stream     = (stbv_uint8 *) data;
  4373     f->stream_end = (stbv_uint8 *) data + data_len;
  4374     f->error      = VORBIS__no_error;
  4375  
  4376     // check that we have the entire packet in memory
  4377     if (!stbv_is_whole_packet_present(f, FALSE)) {
  4378        *samples = 0;
  4379        return 0;
  4380     }
  4381  
  4382     if (!stbv_vorbis_decode_packet(f, &len, &left, &right)) {
  4383        // save the actual error we encountered
  4384        enum STBVorbisError error = f->error;
  4385        if (error == VORBIS_bad_packet_type) {
  4386           // flush and resynch
  4387           f->error = VORBIS__no_error;
  4388           while (stbv_get8_packet(f) != STBV_EOP)
  4389              if (f->eof) break;
  4390           *samples = 0;
  4391           return (int) (f->stream - data);
  4392        }
  4393        if (error == VORBIS_continued_packet_flag_invalid) {
  4394           if (f->previous_length == 0) {
  4395              // we may be resynching, in which case it's ok to hit one
  4396              // of these; just discard the packet
  4397              f->error = VORBIS__no_error;
  4398              while (stbv_get8_packet(f) != STBV_EOP)
  4399                 if (f->eof) break;
  4400              *samples = 0;
  4401              return (int) (f->stream - data);
  4402           }
  4403        }
  4404        // if we get an error while parsing, what to do?
  4405        // well, it DEFINITELY won't work to continue from where we are!
  4406        stb_vorbis_flush_pushdata(f);
  4407        // restore the error that actually made us bail
  4408        f->error = error;
  4409        *samples = 0;
  4410        return 1;
  4411     }
  4412  
  4413     // success!
  4414     len = stbv_vorbis_finish_frame(f, len, left, right);
  4415     for (i=0; i < f->channels; ++i)
  4416        f->outputs[i] = f->channel_buffers[i] + left;
  4417  
  4418     if (channels) *channels = f->channels;
  4419     *samples = len;
  4420     *output = f->outputs;
  4421     return (int) (f->stream - data);
  4422  }
  4423  
  4424  STBVDEF stb_vorbis *stb_vorbis_open_pushdata(
  4425           const unsigned char *data, int data_len, // the memory available for decoding
  4426           int *data_used,              // only defined if result is not NULL
  4427           int *error, const stb_vorbis_alloc *alloc)
  4428  {
  4429     stb_vorbis *f, p;
  4430     stbv_vorbis_init(&p, alloc);
  4431     p.stream     = (stbv_uint8 *) data;
  4432     p.stream_end = (stbv_uint8 *) data + data_len;
  4433     p.push_mode  = TRUE;
  4434     if (!stbv_start_decoder(&p)) {
  4435        if (p.eof)
  4436           *error = VORBIS_need_more_data;
  4437        else
  4438           *error = p.error;
  4439        return NULL;
  4440     }
  4441     f = stbv_vorbis_alloc(&p);
  4442     if (f) {
  4443        *f = p;
  4444        *data_used = (int) (f->stream - data);
  4445        *error = 0;
  4446        return f;
  4447     } else {
  4448        stbv_vorbis_deinit(&p);
  4449        return NULL;
  4450     }
  4451  }
  4452  #endif // STB_VORBIS_NO_PUSHDATA_API
  4453  
  4454  STBVDEF unsigned int stb_vorbis_get_file_offset(stb_vorbis *f)
  4455  {
  4456     #ifndef STB_VORBIS_NO_PUSHDATA_API
  4457     if (f->push_mode) return 0;
  4458     #endif
  4459     if (STBV_USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
  4460     #ifndef STB_VORBIS_NO_STDIO
  4461     return (unsigned int) (ftell(f->f) - f->f_start);
  4462     #endif
  4463  }
  4464  
  4465  #ifndef STB_VORBIS_NO_PULLDATA_API
  4466  //
  4467  // DATA-PULLING API
  4468  //
  4469  
  4470  static stbv_uint32 stbv_vorbis_find_page(stb_vorbis *f, stbv_uint32 *end, stbv_uint32 *last)
  4471  {
  4472     for(;;) {
  4473        int n;
  4474        if (f->eof) return 0;
  4475        n = stbv_get8(f);
  4476        if (n == 0x4f) { // page header candidate
  4477           unsigned int retry_loc = stb_vorbis_get_file_offset(f);
  4478           int i;
  4479           // check if we're off the end of a file_section stream
  4480           if (retry_loc - 25 > f->stream_len)
  4481              return 0;
  4482           // check the rest of the header
  4483           for (i=1; i < 4; ++i)
  4484              if (stbv_get8(f) != stbv_ogg_page_header[i])
  4485                 break;
  4486           if (f->eof) return 0;
  4487           if (i == 4) {
  4488              stbv_uint8 header[27];
  4489              stbv_uint32 i, crc, goal, len;
  4490              for (i=0; i < 4; ++i)
  4491                 header[i] = stbv_ogg_page_header[i];
  4492              for (; i < 27; ++i)
  4493                 header[i] = stbv_get8(f);
  4494              if (f->eof) return 0;
  4495              if (header[4] != 0) goto invalid;
  4496              goal = header[22] + (header[23] << 8) + (header[24]<<16) + (header[25]<<24);
  4497              for (i=22; i < 26; ++i)
  4498                 header[i] = 0;
  4499              crc = 0;
  4500              for (i=0; i < 27; ++i)
  4501                 crc = stbv_crc32_update(crc, header[i]);
  4502              len = 0;
  4503              for (i=0; i < header[26]; ++i) {
  4504                 int s = stbv_get8(f);
  4505                 crc = stbv_crc32_update(crc, s);
  4506                 len += s;
  4507              }
  4508              if (len && f->eof) return 0;
  4509              for (i=0; i < len; ++i)
  4510                 crc = stbv_crc32_update(crc, stbv_get8(f));
  4511              // finished parsing probable page
  4512              if (crc == goal) {
  4513                 // we could now check that it's either got the last
  4514                 // page flag set, OR it's followed by the capture
  4515                 // pattern, but I guess TECHNICALLY you could have
  4516                 // a file with garbage between each ogg page and recover
  4517                 // from it automatically? So even though that paranoia
  4518                 // might decrease the chance of an invalid decode by
  4519                 // another 2^32, not worth it since it would hose those
  4520                 // invalid-but-useful files?
  4521                 if (end)
  4522                    *end = stb_vorbis_get_file_offset(f);
  4523                 if (last) {
  4524                    if (header[5] & 0x04)
  4525                       *last = 1;
  4526                    else
  4527                       *last = 0;
  4528                 }
  4529                 stbv_set_file_offset(f, retry_loc-1);
  4530                 return 1;
  4531              }
  4532           }
  4533          invalid:
  4534           // not a valid page, so rewind and look for next one
  4535           stbv_set_file_offset(f, retry_loc);
  4536        }
  4537     }
  4538  }
  4539  
  4540  
  4541  #define STBV_SAMPLE_unknown  0xffffffff
  4542  
  4543  // seeking is implemented with a binary search, which narrows down the range to
  4544  // 64K, before using a linear search (because finding the synchronization
  4545  // pattern can be expensive, and the chance we'd find the end page again is
  4546  // relatively high for small ranges)
  4547  //
  4548  // two initial interpolation-style probes are used at the start of the search
  4549  // to try to bound either side of the binary search sensibly, while still
  4550  // working in O(log n) time if they fail.
  4551  
  4552  static int stbv_get_seek_page_info(stb_vorbis *f, StbvProbedPage *z)
  4553  {
  4554     stbv_uint8 header[27], lacing[255];
  4555     int i,len;
  4556  
  4557     // record where the page starts
  4558     z->page_start = stb_vorbis_get_file_offset(f);
  4559  
  4560     // parse the header
  4561     stbv_getn(f, header, 27);
  4562     if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S')
  4563        return 0;
  4564     stbv_getn(f, lacing, header[26]);
  4565  
  4566     // determine the length of the payload
  4567     len = 0;
  4568     for (i=0; i < header[26]; ++i)
  4569        len += lacing[i];
  4570  
  4571     // this implies where the page ends
  4572     z->page_end = z->page_start + 27 + header[26] + len;
  4573  
  4574     // read the last-decoded sample out of the data
  4575     z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24);
  4576  
  4577     // restore file state to where we were
  4578     stbv_set_file_offset(f, z->page_start);
  4579     return 1;
  4580  }
  4581  
  4582  // rarely used function to seek back to the preceeding page while finding the
  4583  // start of a packet
  4584  static int stbv_go_to_page_before(stb_vorbis *f, unsigned int limit_offset)
  4585  {
  4586     unsigned int previous_safe, end;
  4587  
  4588     // now we want to seek back 64K from the limit
  4589     if (limit_offset >= 65536 && limit_offset-65536 >= f->first_audio_page_offset)
  4590        previous_safe = limit_offset - 65536;
  4591     else
  4592        previous_safe = f->first_audio_page_offset;
  4593  
  4594     stbv_set_file_offset(f, previous_safe);
  4595  
  4596     while (stbv_vorbis_find_page(f, &end, NULL)) {
  4597        if (end >= limit_offset && stb_vorbis_get_file_offset(f) < limit_offset)
  4598           return 1;
  4599        stbv_set_file_offset(f, end);
  4600     }
  4601  
  4602     return 0;
  4603  }
  4604  
  4605  // implements the search logic for finding a page and starting decoding. if
  4606  // the function succeeds, current_loc_valid will be true and current_loc will
  4607  // be less than or equal to the provided sample number (the closer the
  4608  // better).
  4609  static int stbv_seek_to_sample_coarse(stb_vorbis *f, stbv_uint32 sample_number)
  4610  {
  4611     StbvProbedPage left, right, mid;
  4612     int i, start_seg_with_known_loc, end_pos, page_start;
  4613     stbv_uint32 delta, stream_length, padding;
  4614     double offset, bytes_per_sample;
  4615     int probe = 0;
  4616  
  4617     // find the last page and validate the target sample
  4618     stream_length = stb_vorbis_stream_length_in_samples(f);
  4619     if (stream_length == 0)            return stbv_error(f, VORBIS_seek_without_length);
  4620     if (sample_number > stream_length) return stbv_error(f, VORBIS_seek_invalid);
  4621  
  4622     // this is the maximum difference between the window-center (which is the
  4623     // actual granule position value), and the right-start (which the spec
  4624     // indicates should be the granule position (give or take one)).
  4625     padding = ((f->blocksize_1 - f->blocksize_0) >> 2);
  4626     if (sample_number < padding)
  4627        sample_number = 0;
  4628     else
  4629        sample_number -= padding;
  4630  
  4631     left = f->p_first;
  4632     while (left.last_decoded_sample == ~0U) {
  4633        // (untested) the first page does not have a 'last_decoded_sample'
  4634        stbv_set_file_offset(f, left.page_end);
  4635        if (!stbv_get_seek_page_info(f, &left)) goto error;
  4636     }
  4637  
  4638     right = f->p_last;
  4639     assert(right.last_decoded_sample != ~0U);
  4640  
  4641     // starting from the start is handled differently
  4642     if (sample_number <= left.last_decoded_sample) {
  4643        if (stb_vorbis_seek_start(f))
  4644           return 1;
  4645        return 0;
  4646     }
  4647  
  4648     while (left.page_end != right.page_start) {
  4649        assert(left.page_end < right.page_start);
  4650        // search range in bytes
  4651        delta = right.page_start - left.page_end;
  4652        if (delta <= 65536) {
  4653           // there's only 64K left to search - handle it linearly
  4654           stbv_set_file_offset(f, left.page_end);
  4655        } else {
  4656           if (probe < 2) {
  4657              if (probe == 0) {
  4658                 // first probe (interpolate)
  4659                 double data_bytes = right.page_end - left.page_start;
  4660                 bytes_per_sample = data_bytes / right.last_decoded_sample;
  4661                 offset = left.page_start + bytes_per_sample * (sample_number - left.last_decoded_sample);
  4662              } else {
  4663                 // second probe (try to bound the other side)
  4664                 double error = ((double) sample_number - mid.last_decoded_sample) * bytes_per_sample;
  4665                 if (error >= 0 && error <  8000) error =  8000;
  4666                 if (error <  0 && error > -8000) error = -8000;
  4667                 offset += error * 2;
  4668              }
  4669  
  4670              // ensure the offset is valid
  4671              if (offset < left.page_end)
  4672                 offset = left.page_end;
  4673              if (offset > right.page_start - 65536)
  4674                 offset = right.page_start - 65536;
  4675  
  4676              stbv_set_file_offset(f, (unsigned int) offset);
  4677           } else {
  4678              // binary search for large ranges (offset by 32K to ensure
  4679              // we don't hit the right page)
  4680              stbv_set_file_offset(f, left.page_end + (delta / 2) - 32768);
  4681           }
  4682  
  4683           if (!stbv_vorbis_find_page(f, NULL, NULL)) goto error;
  4684        }
  4685  
  4686        for (;;) {
  4687           if (!stbv_get_seek_page_info(f, &mid)) goto error;
  4688           if (mid.last_decoded_sample != ~0U) break;
  4689           // (untested) no frames end on this page
  4690           stbv_set_file_offset(f, mid.page_end);
  4691           assert(mid.page_start < right.page_start);
  4692        }
  4693  
  4694        // if we've just found the last page again then we're in a tricky file,
  4695        // and we're close enough.
  4696        if (mid.page_start == right.page_start)
  4697           break;
  4698  
  4699        if (sample_number < mid.last_decoded_sample)
  4700           right = mid;
  4701        else
  4702           left = mid;
  4703  
  4704        ++probe;
  4705     }
  4706  
  4707     // seek back to start of the last packet
  4708     page_start = left.page_start;
  4709     stbv_set_file_offset(f, page_start);
  4710     if (!stbv_start_page(f)) return stbv_error(f, VORBIS_seek_failed);
  4711     end_pos = f->end_seg_with_known_loc;
  4712     assert(end_pos >= 0);
  4713  
  4714     for (;;) {
  4715        for (i = end_pos; i > 0; --i)
  4716           if (f->segments[i-1] != 255)
  4717              break;
  4718  
  4719        start_seg_with_known_loc = i;
  4720  
  4721        if (start_seg_with_known_loc > 0 || !(f->page_flag & STBV_PAGEFLAG_continued_packet))
  4722           break;
  4723  
  4724        // (untested) the final packet begins on an earlier page
  4725        if (!stbv_go_to_page_before(f, page_start))
  4726           goto error;
  4727  
  4728        page_start = stb_vorbis_get_file_offset(f);
  4729        if (!stbv_start_page(f)) goto error;
  4730        end_pos = f->segment_count - 1;
  4731     }
  4732  
  4733     // prepare to start decoding
  4734     f->current_loc_valid = FALSE;
  4735     f->last_seg = FALSE;
  4736     f->valid_bits = 0;
  4737     f->packet_bytes = 0;
  4738     f->bytes_in_seg = 0;
  4739     f->previous_length = 0;
  4740     f->next_seg = start_seg_with_known_loc;
  4741  
  4742     for (i = 0; i < start_seg_with_known_loc; i++)
  4743        stbv_skip(f, f->segments[i]);
  4744  
  4745     // start decoding (optimizable - this frame is generally discarded)
  4746     if (!stbv_vorbis_pump_first_frame(f))
  4747        return 0;
  4748     if (f->current_loc > sample_number)
  4749        return stbv_error(f, VORBIS_seek_failed);
  4750     return 1;
  4751  
  4752  error:
  4753     // try to restore the file to a valid state
  4754     stb_vorbis_seek_start(f);
  4755     return stbv_error(f, VORBIS_seek_failed);
  4756  }
  4757  
  4758  // the same as stbv_vorbis_decode_initial, but without advancing
  4759  static int stbv_peek_decode_initial(stbv_vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode)
  4760  {
  4761     int bits_read, bytes_read;
  4762  
  4763     if (!stbv_vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode))
  4764        return 0;
  4765  
  4766     // either 1 or 2 bytes were read, figure out which so we can rewind
  4767     bits_read = 1 + stbv_ilog(f->mode_count-1);
  4768     if (f->mode_config[*mode].blockflag)
  4769        bits_read += 2;
  4770     bytes_read = (bits_read + 7) / 8;
  4771  
  4772     f->bytes_in_seg += bytes_read;
  4773     f->packet_bytes -= bytes_read;
  4774     stbv_skip(f, -bytes_read);
  4775     if (f->next_seg == -1)
  4776        f->next_seg = f->segment_count - 1;
  4777     else
  4778        f->next_seg--;
  4779     f->valid_bits = 0;
  4780  
  4781     return 1;
  4782  }
  4783  
  4784  STBVDEF int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
  4785  {
  4786     stbv_uint32 max_frame_samples;
  4787  
  4788     if (STBV_IS_PUSH_MODE(f)) return stbv_error(f, VORBIS_invalid_api_mixing);
  4789  
  4790     // fast page-level search
  4791     if (!stbv_seek_to_sample_coarse(f, sample_number))
  4792        return 0;
  4793  
  4794     assert(f->current_loc_valid);
  4795     assert(f->current_loc <= sample_number);
  4796  
  4797     // linear search for the relevant packet
  4798     max_frame_samples = (f->blocksize_1*3 - f->blocksize_0) >> 2;
  4799     while (f->current_loc < sample_number) {
  4800        int left_start, left_end, right_start, right_end, mode, frame_samples;
  4801        if (!stbv_peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode))
  4802           return stbv_error(f, VORBIS_seek_failed);
  4803        // calculate the number of samples returned by the next frame
  4804        frame_samples = right_start - left_start;
  4805        if (f->current_loc + frame_samples > sample_number) {
  4806           return 1; // the next frame will contain the sample
  4807        } else if (f->current_loc + frame_samples + max_frame_samples > sample_number) {
  4808           // there's a chance the frame after this could contain the sample
  4809           stbv_vorbis_pump_first_frame(f);
  4810        } else {
  4811           // this frame is too early to be relevant
  4812           f->current_loc += frame_samples;
  4813           f->previous_length = 0;
  4814           stbv_maybe_start_packet(f);
  4815           stbv_flush_packet(f);
  4816        }
  4817     }
  4818     // the next frame will start with the sample
  4819     assert(f->current_loc == sample_number);
  4820     return 1;
  4821  }
  4822  
  4823  STBVDEF int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
  4824  {
  4825     if (!stb_vorbis_seek_frame(f, sample_number))
  4826        return 0;
  4827  
  4828     if (sample_number != f->current_loc) {
  4829        int n;
  4830        stbv_uint32 frame_start = f->current_loc;
  4831        stb_vorbis_get_frame_float(f, &n, NULL);
  4832        assert(sample_number > frame_start);
  4833        assert(f->channel_buffer_start + (int) (sample_number-frame_start) <= f->channel_buffer_end);
  4834        f->channel_buffer_start += (sample_number - frame_start);
  4835     }
  4836  
  4837     return 1;
  4838  }
  4839  
  4840  STBVDEF int stb_vorbis_seek_start(stb_vorbis *f)
  4841  {
  4842     if (STBV_IS_PUSH_MODE(f)) { return stbv_error(f, VORBIS_invalid_api_mixing); }
  4843     stbv_set_file_offset(f, f->first_audio_page_offset);
  4844     f->previous_length = 0;
  4845     f->first_decode = TRUE;
  4846     f->next_seg = -1;
  4847     return stbv_vorbis_pump_first_frame(f);
  4848  }
  4849  
  4850  STBVDEF unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f)
  4851  {
  4852     unsigned int restore_offset, previous_safe;
  4853     unsigned int end, last_page_loc;
  4854  
  4855     if (STBV_IS_PUSH_MODE(f)) return stbv_error(f, VORBIS_invalid_api_mixing);
  4856     if (!f->total_samples) {
  4857        unsigned int last;
  4858        stbv_uint32 lo,hi;
  4859        char header[6];
  4860  
  4861        // first, store the current decode position so we can restore it
  4862        restore_offset = stb_vorbis_get_file_offset(f);
  4863  
  4864        // now we want to seek back 64K from the end (the last page must
  4865        // be at most a little less than 64K, but let's allow a little slop)
  4866        if (f->stream_len >= 65536 && f->stream_len-65536 >= f->first_audio_page_offset)
  4867           previous_safe = f->stream_len - 65536;
  4868        else
  4869           previous_safe = f->first_audio_page_offset;
  4870  
  4871        stbv_set_file_offset(f, previous_safe);
  4872        // previous_safe is now our candidate 'earliest known place that seeking
  4873        // to will lead to the final page'
  4874  
  4875        if (!stbv_vorbis_find_page(f, &end, &last)) {
  4876           // if we can't find a page, we're hosed!
  4877           f->error = VORBIS_cant_find_last_page;
  4878           f->total_samples = 0xffffffff;
  4879           goto done;
  4880        }
  4881  
  4882        // check if there are more pages
  4883        last_page_loc = stb_vorbis_get_file_offset(f);
  4884  
  4885        // stop when the last_page flag is set, not when we reach eof;
  4886        // this allows us to stop short of a 'file_section' end without
  4887        // explicitly checking the length of the section
  4888        while (!last) {
  4889           stbv_set_file_offset(f, end);
  4890           if (!stbv_vorbis_find_page(f, &end, &last)) {
  4891              // the last page we found didn't have the 'last page' flag
  4892              // set. whoops!
  4893              break;
  4894           }
  4895           previous_safe = last_page_loc+1;
  4896           last_page_loc = stb_vorbis_get_file_offset(f);
  4897        }
  4898  
  4899        stbv_set_file_offset(f, last_page_loc);
  4900  
  4901        // parse the header
  4902        stbv_getn(f, (unsigned char *)header, 6);
  4903        // extract the absolute granule position
  4904        lo = stbv_get32(f);
  4905        hi = stbv_get32(f);
  4906        if (lo == 0xffffffff && hi == 0xffffffff) {
  4907           f->error = VORBIS_cant_find_last_page;
  4908           f->total_samples = STBV_SAMPLE_unknown;
  4909           goto done;
  4910        }
  4911        if (hi)
  4912           lo = 0xfffffffe; // saturate
  4913        f->total_samples = lo;
  4914  
  4915        f->p_last.page_start = last_page_loc;
  4916        f->p_last.page_end   = end;
  4917        f->p_last.last_decoded_sample = lo;
  4918  
  4919       done:
  4920        stbv_set_file_offset(f, restore_offset);
  4921     }
  4922     return f->total_samples == STBV_SAMPLE_unknown ? 0 : f->total_samples;
  4923  }
  4924  
  4925  STBVDEF float stb_vorbis_stream_length_in_seconds(stb_vorbis *f)
  4926  {
  4927     return stb_vorbis_stream_length_in_samples(f) / (float) f->sample_rate;
  4928  }
  4929  
  4930  
  4931  
  4932  STBVDEF int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output)
  4933  {
  4934     int len, right,left,i;
  4935     if (STBV_IS_PUSH_MODE(f)) return stbv_error(f, VORBIS_invalid_api_mixing);
  4936  
  4937     if (!stbv_vorbis_decode_packet(f, &len, &left, &right)) {
  4938        f->channel_buffer_start = f->channel_buffer_end = 0;
  4939        return 0;
  4940     }
  4941  
  4942     len = stbv_vorbis_finish_frame(f, len, left, right);
  4943     for (i=0; i < f->channels; ++i)
  4944        f->outputs[i] = f->channel_buffers[i] + left;
  4945  
  4946     f->channel_buffer_start = left;
  4947     f->channel_buffer_end   = left+len;
  4948  
  4949     if (channels) *channels = f->channels;
  4950     if (output)   *output = f->outputs;
  4951     return len;
  4952  }
  4953  
  4954  #ifndef STB_VORBIS_NO_STDIO
  4955  
  4956  STBVDEF stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length)
  4957  {
  4958     stb_vorbis *f, p;
  4959     stbv_vorbis_init(&p, alloc);
  4960     p.f = file;
  4961     p.f_start = (stbv_uint32) ftell(file);
  4962     p.stream_len   = length;
  4963     p.close_on_free = close_on_free;
  4964     if (stbv_start_decoder(&p)) {
  4965        f = stbv_vorbis_alloc(&p);
  4966        if (f) {
  4967           *f = p;
  4968           stbv_vorbis_pump_first_frame(f);
  4969           return f;
  4970        }
  4971     }
  4972     if (error) *error = p.error;
  4973     stbv_vorbis_deinit(&p);
  4974     return NULL;
  4975  }
  4976  
  4977  STBVDEF stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc)
  4978  {
  4979     unsigned int len, start;
  4980     start = (unsigned int) ftell(file);
  4981     fseek(file, 0, SEEK_END);
  4982     len = (unsigned int) (ftell(file) - start);
  4983     fseek(file, start, SEEK_SET);
  4984     return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len);
  4985  }
  4986  
  4987  STBVDEF stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc)
  4988  {
  4989     FILE *f = fopen(filename, "rb");
  4990     if (f) 
  4991        return stb_vorbis_open_file(f, TRUE, error, alloc);
  4992     if (error) *error = VORBIS_file_open_failure;
  4993     return NULL;
  4994  }
  4995  #endif // STB_VORBIS_NO_STDIO
  4996  
  4997  STBVDEF stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc)
  4998  {
  4999     stb_vorbis *f, p;
  5000     if (data == NULL) return NULL;
  5001     stbv_vorbis_init(&p, alloc);
  5002     p.stream = (stbv_uint8 *) data;
  5003     p.stream_end = (stbv_uint8 *) data + len;
  5004     p.stream_start = (stbv_uint8 *) p.stream;
  5005     p.stream_len = len;
  5006     p.push_mode = FALSE;
  5007     if (stbv_start_decoder(&p)) {
  5008        f = stbv_vorbis_alloc(&p);
  5009        if (f) {
  5010           *f = p;
  5011           stbv_vorbis_pump_first_frame(f);
  5012           if (error) *error = VORBIS__no_error;
  5013           return f;
  5014        }
  5015     }
  5016     if (error) *error = p.error;
  5017     stbv_vorbis_deinit(&p);
  5018     return NULL;
  5019  }
  5020  
  5021  #ifndef STB_VORBIS_NO_INTEGER_CONVERSION
  5022  #define STBV_PLAYBACK_MONO 1
  5023  #define STBV_PLAYBACK_LEFT 2
  5024  #define STBV_PLAYBACK_RIGHT 4
  5025  
  5026  #define STBV_L  (STBV_PLAYBACK_LEFT  | STBV_PLAYBACK_MONO)
  5027  #define STBV_C  (STBV_PLAYBACK_LEFT  | STBV_PLAYBACK_RIGHT | STBV_PLAYBACK_MONO)
  5028  #define STBV_R  (STBV_PLAYBACK_RIGHT | STBV_PLAYBACK_MONO)
  5029  
  5030  static stbv_int8 stbv_channel_position[7][6] =
  5031  {
  5032     { 0 },
  5033     { STBV_C },
  5034     { STBV_L, STBV_R },
  5035     { STBV_L, STBV_C, STBV_R },
  5036     { STBV_L, STBV_R, STBV_L, STBV_R },
  5037     { STBV_L, STBV_C, STBV_R, STBV_L, STBV_R },
  5038     { STBV_L, STBV_C, STBV_R, STBV_L, STBV_R, STBV_C },
  5039  };
  5040  
  5041  
  5042  #ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
  5043     typedef union {
  5044        float f;
  5045        int i;
  5046     } stbv_float_conv;
  5047     typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4];
  5048     #define STBV_FASTDEF(x) stbv_float_conv x
  5049     // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round
  5050     #define STBV_MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
  5051     #define STBV_ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
  5052     #define STBV_FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + STBV_MAGIC(s), temp.i - STBV_ADDEND(s))
  5053     #define stbv_check_endianness()  
  5054  #else
  5055     #define STBV_FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
  5056     #define stbv_check_endianness()
  5057     #define STBV_FASTDEF(x)
  5058  #endif
  5059  
  5060  static void stbv_copy_samples(short *dest, float *src, int len)
  5061  {
  5062     int i;
  5063     stbv_check_endianness();
  5064     for (i=0; i < len; ++i) {
  5065        STBV_FASTDEF(temp);
  5066        int v = STBV_FAST_SCALED_FLOAT_TO_INT(temp, src[i],15);
  5067        if ((unsigned int) (v + 32768) > 65535)
  5068           v = v < 0 ? -32768 : 32767;
  5069        dest[i] = v;
  5070     }
  5071  }
  5072  
  5073  static void stbv_compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len)
  5074  {
  5075     #define BUFFER_SIZE  32
  5076     float buffer[BUFFER_SIZE];
  5077     int i,j,o,n = BUFFER_SIZE;
  5078     stbv_check_endianness();
  5079     for (o = 0; o < len; o += BUFFER_SIZE) {
  5080        memset(buffer, 0, sizeof(buffer));
  5081        if (o + n > len) n = len - o;
  5082        for (j=0; j < num_c; ++j) {
  5083           if (stbv_channel_position[num_c][j] & mask) {
  5084              for (i=0; i < n; ++i)
  5085                 buffer[i] += data[j][d_offset+o+i];
  5086           }
  5087        }
  5088        for (i=0; i < n; ++i) {
  5089           STBV_FASTDEF(temp);
  5090           int v = STBV_FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
  5091           if ((unsigned int) (v + 32768) > 65535)
  5092              v = v < 0 ? -32768 : 32767;
  5093           output[o+i] = v;
  5094        }
  5095     }
  5096  }
  5097  
  5098  static void stbv_compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len)
  5099  {
  5100     #define BUFFER_SIZE  32
  5101     float buffer[BUFFER_SIZE];
  5102     int i,j,o,n = BUFFER_SIZE >> 1;
  5103     // o is the offset in the source data
  5104     stbv_check_endianness();
  5105     for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
  5106        // o2 is the offset in the output data
  5107        int o2 = o << 1;
  5108        memset(buffer, 0, sizeof(buffer));
  5109        if (o + n > len) n = len - o;
  5110        for (j=0; j < num_c; ++j) {
  5111           int m = stbv_channel_position[num_c][j] & (STBV_PLAYBACK_LEFT | STBV_PLAYBACK_RIGHT);
  5112           if (m == (STBV_PLAYBACK_LEFT | STBV_PLAYBACK_RIGHT)) {
  5113              for (i=0; i < n; ++i) {
  5114                 buffer[i*2+0] += data[j][d_offset+o+i];
  5115                 buffer[i*2+1] += data[j][d_offset+o+i];
  5116              }
  5117           } else if (m == STBV_PLAYBACK_LEFT) {
  5118              for (i=0; i < n; ++i) {
  5119                 buffer[i*2+0] += data[j][d_offset+o+i];
  5120              }
  5121           } else if (m == STBV_PLAYBACK_RIGHT) {
  5122              for (i=0; i < n; ++i) {
  5123                 buffer[i*2+1] += data[j][d_offset+o+i];
  5124              }
  5125           }
  5126        }
  5127        for (i=0; i < (n<<1); ++i) {
  5128           STBV_FASTDEF(temp);
  5129           int v = STBV_FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
  5130           if ((unsigned int) (v + 32768) > 65535)
  5131              v = v < 0 ? -32768 : 32767;
  5132           output[o2+i] = v;
  5133        }
  5134     }
  5135  }
  5136  
  5137  static void stbv_convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples)
  5138  {
  5139     int i;
  5140     if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
  5141        static int channel_selector[3][2] = { {0}, {STBV_PLAYBACK_MONO}, {STBV_PLAYBACK_LEFT, STBV_PLAYBACK_RIGHT} };
  5142        for (i=0; i < buf_c; ++i)
  5143           stbv_compute_samples(channel_selector[buf_c][i], buffer[i]+b_offset, data_c, data, d_offset, samples);
  5144     } else {
  5145        int limit = buf_c < data_c ? buf_c : data_c;
  5146        for (i=0; i < limit; ++i)
  5147           stbv_copy_samples(buffer[i]+b_offset, data[i]+d_offset, samples);
  5148        for (   ; i < buf_c; ++i)
  5149           memset(buffer[i]+b_offset, 0, sizeof(short) * samples);
  5150     }
  5151  }
  5152  
  5153  STBVDEF int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples)
  5154  {
  5155     float **output;
  5156     int len = stb_vorbis_get_frame_float(f, NULL, &output);
  5157     if (len > num_samples) len = num_samples;
  5158     if (len)
  5159        stbv_convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len);
  5160     return len;
  5161  }
  5162  
  5163  static void stbv_convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len)
  5164  {
  5165     int i;
  5166     stbv_check_endianness();
  5167     if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
  5168        assert(buf_c == 2);
  5169        for (i=0; i < buf_c; ++i)
  5170           stbv_compute_stereo_samples(buffer, data_c, data, d_offset, len);
  5171     } else {
  5172        int limit = buf_c < data_c ? buf_c : data_c;
  5173        int j;
  5174        for (j=0; j < len; ++j) {
  5175           for (i=0; i < limit; ++i) {
  5176              STBV_FASTDEF(temp);
  5177              float f = data[i][d_offset+j];
  5178              int v = STBV_FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15);
  5179              if ((unsigned int) (v + 32768) > 65535)
  5180                 v = v < 0 ? -32768 : 32767;
  5181              *buffer++ = v;
  5182           }
  5183           for (   ; i < buf_c; ++i)
  5184              *buffer++ = 0;
  5185        }
  5186     }
  5187  }
  5188  
  5189  STBVDEF int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts)
  5190  {
  5191     float **output;
  5192     int len;
  5193     if (num_c == 1) return stb_vorbis_get_frame_short(f,num_c,&buffer, num_shorts);
  5194     len = stb_vorbis_get_frame_float(f, NULL, &output);
  5195     if (len) {
  5196        if (len*num_c > num_shorts) len = num_shorts / num_c;
  5197        stbv_convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len);
  5198     }
  5199     return len;
  5200  }
  5201  
  5202  STBVDEF int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts)
  5203  {
  5204     float **outputs;
  5205     int len = num_shorts / channels;
  5206     int n=0;
  5207     int z = f->channels;
  5208     if (z > channels) z = channels;
  5209     while (n < len) {
  5210        int k = f->channel_buffer_end - f->channel_buffer_start;
  5211        if (n+k >= len) k = len - n;
  5212        if (k)
  5213           stbv_convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k);
  5214        buffer += k*channels;
  5215        n += k;
  5216        f->channel_buffer_start += k;
  5217        if (n == len) break;
  5218        if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
  5219     }
  5220     return n;
  5221  }
  5222  
  5223  STBVDEF int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len)
  5224  {
  5225     float **outputs;
  5226     int n=0;
  5227     int z = f->channels;
  5228     if (z > channels) z = channels;
  5229     while (n < len) {
  5230        int k = f->channel_buffer_end - f->channel_buffer_start;
  5231        if (n+k >= len) k = len - n;
  5232        if (k)
  5233           stbv_convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k);
  5234        n += k;
  5235        f->channel_buffer_start += k;
  5236        if (n == len) break;
  5237        if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
  5238     }
  5239     return n;
  5240  }
  5241  
  5242  #ifndef STB_VORBIS_NO_STDIO
  5243  STBVDEF int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output)
  5244  {
  5245     int data_len, offset, total, limit, error;
  5246     short *data;
  5247     stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL);
  5248     if (v == NULL) return -1;
  5249     limit = v->channels * 4096;
  5250     *channels = v->channels;
  5251     if (sample_rate)
  5252        *sample_rate = v->sample_rate;
  5253     offset = data_len = 0;
  5254     total = limit;
  5255     data = (short *) malloc(total * sizeof(*data));
  5256     if (data == NULL) {
  5257        stb_vorbis_close(v);
  5258        return -2;
  5259     }
  5260     for (;;) {
  5261        int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset);
  5262        if (n == 0) break;
  5263        data_len += n;
  5264        offset += n * v->channels;
  5265        if (offset + limit > total) {
  5266           short *data2;
  5267           total *= 2;
  5268           data2 = (short *) realloc(data, total * sizeof(*data));
  5269           if (data2 == NULL) {
  5270              free(data);
  5271              stb_vorbis_close(v);
  5272              return -2;
  5273           }
  5274           data = data2;
  5275        }
  5276     }
  5277     *output = data;
  5278     stb_vorbis_close(v);
  5279     return data_len;
  5280  }
  5281  #endif // NO_STDIO
  5282  
  5283  STBVDEF int stb_vorbis_decode_memory(const stbv_uint8 *mem, int len, int *channels, int *sample_rate, short **output)
  5284  {
  5285     int data_len, offset, total, limit, error;
  5286     short *data;
  5287     stb_vorbis *v = stb_vorbis_open_memory(mem, len, &error, NULL);
  5288     if (v == NULL) return -1;
  5289     limit = v->channels * 4096;
  5290     *channels = v->channels;
  5291     if (sample_rate)
  5292        *sample_rate = v->sample_rate;
  5293     offset = data_len = 0;
  5294     total = limit;
  5295     data = (short *) malloc(total * sizeof(*data));
  5296     if (data == NULL) {
  5297        stb_vorbis_close(v);
  5298        return -2;
  5299     }
  5300     for (;;) {
  5301        int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset);
  5302        if (n == 0) break;
  5303        data_len += n;
  5304        offset += n * v->channels;
  5305        if (offset + limit > total) {
  5306           short *data2;
  5307           total *= 2;
  5308           data2 = (short *) realloc(data, total * sizeof(*data));
  5309           if (data2 == NULL) {
  5310              free(data);
  5311              stb_vorbis_close(v);
  5312              return -2;
  5313           }
  5314           data = data2;
  5315        }
  5316     }
  5317     *output = data;
  5318     stb_vorbis_close(v);
  5319     return data_len;
  5320  }
  5321  #endif // STB_VORBIS_NO_INTEGER_CONVERSION
  5322  
  5323  STBVDEF int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats)
  5324  {
  5325     float **outputs;
  5326     int len = num_floats / channels;
  5327     int n=0;
  5328     int z = f->channels;
  5329     if (z > channels) z = channels;
  5330     while (n < len) {
  5331        int i,j;
  5332        int k = f->channel_buffer_end - f->channel_buffer_start;
  5333        if (n+k >= len) k = len - n;
  5334        for (j=0; j < k; ++j) {
  5335           for (i=0; i < z; ++i)
  5336              *buffer++ = f->channel_buffers[i][f->channel_buffer_start+j];
  5337           for (   ; i < channels; ++i)
  5338              *buffer++ = 0;
  5339        }
  5340        n += k;
  5341        f->channel_buffer_start += k;
  5342        if (n == len)
  5343           break;
  5344        if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
  5345           break;
  5346     }
  5347     return n;
  5348  }
  5349  
  5350  STBVDEF int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples)
  5351  {
  5352     float **outputs;
  5353     int n=0;
  5354     int z = f->channels;
  5355     if (z > channels) z = channels;
  5356     while (n < num_samples) {
  5357        int i;
  5358        int k = f->channel_buffer_end - f->channel_buffer_start;
  5359        if (n+k >= num_samples) k = num_samples - n;
  5360        if (k) {
  5361           for (i=0; i < z; ++i)
  5362              memcpy(buffer[i]+n, f->channel_buffers[i]+f->channel_buffer_start, sizeof(float)*k);
  5363           for (   ; i < channels; ++i)
  5364              memset(buffer[i]+n, 0, sizeof(float) * k);
  5365        }
  5366        n += k;
  5367        f->channel_buffer_start += k;
  5368        if (n == num_samples)
  5369           break;
  5370        if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
  5371           break;
  5372     }
  5373     return n;
  5374  }
  5375  #endif // STB_VORBIS_NO_PULLDATA_API
  5376  
  5377  /* Version history
  5378      1.12    - 2017-11-21 - limit residue begin/end to blocksize/2 to avoid large temp allocs in bad/corrupt files
  5379      1.11    - 2017-07-23 - fix MinGW compilation 
  5380      1.10    - 2017-03-03 - more robust seeking; fix negative stbv_ilog(); clear error in open_memory
  5381      1.09    - 2016-04-04 - back out 'avoid discarding last frame' fix from previous version
  5382      1.08    - 2016-04-02 - fixed multiple warnings; fix setup memory leaks;
  5383                             avoid discarding last frame of audio data
  5384      1.07    - 2015-01-16 - fixed some warnings, fix mingw, const-correct API
  5385                             some more crash fixes when out of memory or with corrupt files 
  5386      1.06    - 2015-08-31 - full, correct support for seeking API (Dougall Johnson)
  5387                             some crash fixes when out of memory or with corrupt files
  5388      1.05    - 2015-04-19 - don't define __forceinline if it's redundant
  5389      1.04    - 2014-08-27 - fix missing const-correct case in API
  5390      1.03    - 2014-08-07 - Warning fixes
  5391      1.02    - 2014-07-09 - Declare qsort compare function _cdecl on windows
  5392      1.01    - 2014-06-18 - fix stb_vorbis_get_samples_float
  5393      1.0     - 2014-05-26 - fix memory leaks; fix warnings; fix bugs in multichannel
  5394                             (API change) report sample rate for decode-full-file funcs
  5395      0.99996 - bracket #include <malloc.h> for macintosh compilation by Laurent Gomila
  5396      0.99995 - use union instead of pointer-cast for fast-float-to-int to avoid alias-optimization problem
  5397      0.99994 - change fast-float-to-int to work in single-precision FPU mode, remove endian-dependence
  5398      0.99993 - remove assert that fired on legal files with empty tables
  5399      0.99992 - rewind-to-start
  5400      0.99991 - bugfix to stb_vorbis_get_samples_short by Bernhard Wodo
  5401      0.9999 - (should have been 0.99990) fix no-CRT support, compiling as C++
  5402      0.9998 - add a full-decode function with a memory source
  5403      0.9997 - fix a bug in the read-from-FILE case in 0.9996 addition
  5404      0.9996 - query length of vorbis stream in samples/seconds
  5405      0.9995 - bugfix to another optimization that only happened in certain files
  5406      0.9994 - bugfix to one of the optimizations that caused significant (but inaudible?) errors
  5407      0.9993 - performance improvements; runs in 99% to 104% of time of reference implementation
  5408      0.9992 - performance improvement of IMDCT; now performs close to reference implementation
  5409      0.9991 - performance improvement of IMDCT
  5410      0.999 - (should have been 0.9990) performance improvement of IMDCT
  5411      0.998 - no-CRT support from Casey Muratori
  5412      0.997 - bugfixes for bugs found by Terje Mathisen
  5413      0.996 - bugfix: fast-huffman decode initialized incorrectly for sparse codebooks; fixing gives 10% speedup - found by Terje Mathisen
  5414      0.995 - bugfix: fix to 'effective' overrun detection - found by Terje Mathisen
  5415      0.994 - bugfix: garbage decode on final VQ symbol of a non-multiple - found by Terje Mathisen
  5416      0.993 - bugfix: pushdata API required 1 extra byte for empty page (failed to consume final page if empty) - found by Terje Mathisen
  5417      0.992 - fixes for MinGW warning
  5418      0.991 - turn fast-float-conversion on by default
  5419      0.990 - fix push-mode seek recovery if you seek into the headers
  5420      0.98b - fix to bad release of 0.98
  5421      0.98 - fix push-mode seek recovery; robustify float-to-int and support non-fast mode
  5422      0.97 - builds under c++ (typecasting, don't use 'class' keyword)
  5423      0.96 - somehow MY 0.95 was right, but the web one was wrong, so here's my 0.95 rereleased as 0.96, fixes a typo in the clamping code
  5424      0.95 - clamping code for 16-bit functions
  5425      0.94 - not publically released
  5426      0.93 - fixed all-zero-floor case (was decoding garbage)
  5427      0.92 - fixed a memory leak
  5428      0.91 - conditional compiles to omit parts of the API and the infrastructure to support them: STB_VORBIS_NO_PULLDATA_API, STB_VORBIS_NO_PUSHDATA_API, STB_VORBIS_NO_STDIO, STB_VORBIS_NO_INTEGER_CONVERSION
  5429      0.90 - first public release
  5430  */
  5431  
  5432  #endif // STB_VORBIS_IMPLEMENTATION
  5433  
  5434  
  5435  /*
  5436  ------------------------------------------------------------------------------
  5437  This software is available under 2 licenses -- choose whichever you prefer.
  5438  ------------------------------------------------------------------------------
  5439  ALTERNATIVE A - MIT License
  5440  Copyright (c) 2017 Sean Barrett
  5441  Permission is hereby granted, free of charge, to any person obtaining a copy of 
  5442  this software and associated documentation files (the "Software"), to deal in 
  5443  the Software without restriction, including without limitation the rights to 
  5444  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
  5445  of the Software, and to permit persons to whom the Software is furnished to do 
  5446  so, subject to the following conditions:
  5447  The above copyright notice and this permission notice shall be included in all 
  5448  copies or substantial portions of the Software.
  5449  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  5450  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  5451  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
  5452  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  5453  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  5454  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  5455  SOFTWARE.
  5456  ------------------------------------------------------------------------------
  5457  ALTERNATIVE B - Public Domain (www.unlicense.org)
  5458  This is free and unencumbered software released into the public domain.
  5459  Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 
  5460  software, either in source code form or as a compiled binary, for any purpose, 
  5461  commercial or non-commercial, and by any means.
  5462  In jurisdictions that recognize copyright laws, the author or authors of this 
  5463  software dedicate any and all copyright interest in the software to the public 
  5464  domain. We make this dedication for the benefit of the public at large and to 
  5465  the detriment of our heirs and successors. We intend this dedication to be an 
  5466  overt act of relinquishment in perpetuity of all present and future rights to 
  5467  this software under copyright law.
  5468  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  5469  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  5470  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
  5471  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  5472  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
  5473  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  5474  ------------------------------------------------------------------------------
  5475  */