├── LICENSE
├── README.md
├── WaveBase64_Player.swf
├── big-buck-bunny_trailer.ogg
├── decoder.html
├── js
├── ajax.js
└── script.js
├── oggvorbis.js
└── sync_def_PF-0_AF-33-BG_FF-33.ogg
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2012-2017 Dominik Homberger
2 |
3 | Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
4 |
5 | CC BY-NC-SA 4.0
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | OGG Vorbis JavaScript decoder
2 | =============================
3 | Here you can get my hand ported source code from stb_vorbis.c (Sean Barrett) to javascript (Dominik Homberger). Source code is from 2012.
4 |
5 | Here you can see an example:
6 | http://libwebpjs.hohenlimburg.org/vp8/ogg-vorbis-javascript-decoder/
7 |
--------------------------------------------------------------------------------
/WaveBase64_Player.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dominikhlbg/ogg-vorbis-javascript-decoder/c0c374c5d89ebdbaf06fee25b89316d8894eb06b/WaveBase64_Player.swf
--------------------------------------------------------------------------------
/big-buck-bunny_trailer.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dominikhlbg/ogg-vorbis-javascript-decoder/c0c374c5d89ebdbaf06fee25b89316d8894eb06b/big-buck-bunny_trailer.ogg
--------------------------------------------------------------------------------
/decoder.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Ogg Vorbis Decoder
6 |
7 |
8 |
9 |
25 |
26 |
27 |
28 | OGG Vorbis Javascript Decoder - 04/26/2012
29 |
30 |
31 | buffersize/seconds
32 |
33 |
34 | audio
35 |
36 | embed
37 |
38 | flash
39 |
40 |
41 |
42 |
43 | Player
44 |
45 |
46 |
47 | Ogg Vorbis (later:WebM) JavaScript Decoder for every browser - This script works with Safari 4/5, Internet Explorer 6/7/8/9 & 10, Google Chrome, Mozilla Firefox, Opera - implemented by Dominik Homberger
48 |
49 |
50 |
--------------------------------------------------------------------------------
/js/ajax.js:
--------------------------------------------------------------------------------
1 | function createRequestObject() {
2 | // var ro;
3 | // var browser = navigator.appName;
4 | // if(browser == "Microsoft Internet Explorer"){
5 | // ro = new ActiveXObject("Microsoft.XMLHTTP");
6 | // }else{
7 | ro = new XMLHttpRequest();
8 | // }
9 | return ro;
10 | }
11 |
12 | var http = createRequestObject();
13 |
14 | function convertResponseBodyToText(IEByteArray) {
15 | var ByteMapping = {};
16 | for ( var i = 0; i < 256; i++ ) {
17 | for ( var j = 0; j < 256; j++ ) {
18 | ByteMapping[ String.fromCharCode( i + j * 256 ) ] =
19 | String.fromCharCode(i) + String.fromCharCode(j);
20 | }
21 | }
22 | var rawBytes = IEBinaryToArray_ByteStr(IEByteArray);
23 | var lastChr = IEBinaryToArray_ByteStr_Last(IEByteArray);
24 | return rawBytes.replace(/[\s\S]/g,
25 | function( match ) { return ByteMapping[match]; }) + lastChr;
26 | }
27 |
28 | var IEBinaryToArray_ByteStr_Script =
29 | "\r\n"+
30 | "\r\n";
44 | document.write(IEBinaryToArray_ByteStr_Script);
45 |
46 | function readfile(filename) {
47 | http.open('get', filename);
48 |
49 | if (http.overrideMimeType)
50 | http.overrideMimeType('text/plain; charset=x-user-defined');
51 | else
52 | http.setRequestHeader('Accept-Charset', 'x-user-defined');
53 |
54 | http.send(null);
55 | http.onreadystatechange = function() {
56 | if(http.readyState == 4){
57 | if (typeof http.responseBody=='undefined') {
58 | var response = http.responseText.split('').map(function(e){return e.charCodeAt(0) & 0xff});//String.fromCharCode().join('')
59 | //alert(response);
60 | oggvorbisdata(response);
61 | } else {
62 | var response = convertResponseBodyToText(http.responseBody).split('');//.map(function(e){return e.charCodeAt(0) & 0xff});
63 | for (var i=0,j=response.length;i 0) {
32 | evt.preventDefault();
33 | evt.stopPropagation();
34 | var file = files[0];
35 | if (typeof FileReader !== "undefined") {
36 | if(!isActive) {
37 | var freader = new FileReader();
38 | freader.onload = function (evt) {
39 | isActive=true;
40 | disabledbuttons(true);
41 | oggvorbisdata(evt.target.result.split('').map(function(e){return e.charCodeAt(0) & 0xff}));
42 | };
43 | freader.readAsBinaryString(file);
44 | } else alert('refresh your browser to add a new file');
45 | } else {
46 | alert('Your Browser don\'t support the Filereader API');
47 | }
48 | }
49 | }, false);
50 | }
51 |
52 | var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
53 |
54 | function base64encode(str) {
55 | var out, i, len;
56 | var c1, c2, c3;
57 |
58 | len = str.length;
59 | i = 0;
60 | out = "";
61 | while(i < len) {
62 | c1 = str.charCodeAt(i++) & 0xff;
63 | if(i == len)
64 | {
65 | out += base64EncodeChars.charAt(c1 >> 2);
66 | out += base64EncodeChars.charAt((c1 & 0x3) << 4);
67 | out += "==";
68 | break;
69 | }
70 | c2 = str.charCodeAt(i++);
71 | if(i == len)
72 | {
73 | out += base64EncodeChars.charAt(c1 >> 2);
74 | out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
75 | out += base64EncodeChars.charAt((c2 & 0xF) << 2);
76 | out += "=";
77 | break;
78 | }
79 | c3 = str.charCodeAt(i++);
80 | out += base64EncodeChars.charAt(c1 >> 2);
81 | out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
82 | out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
83 | out += base64EncodeChars.charAt(c3 & 0x3F);
84 | }
85 | return out;
86 | }
87 |
88 | if (!window.btoa) window.btoa = base64encode;
89 |
90 |
--------------------------------------------------------------------------------
/oggvorbis.js:
--------------------------------------------------------------------------------
1 | // Ogg Vorbis I audio decoder -- version 0.99996
2 | //
3 | // Written in April 2007 by Sean Barrett, sponsored by RAD Game Tools. http://nothings.org/stb_vorbis/
4 | //
5 | // This Version is a javascript version hand ported by Dominik Homberger (dominikhlbg@gmail.com)
6 | // License: CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/
7 |
8 |
9 | (function(){
10 |
11 | var buffer_time;
12 | var player_type_audio;
13 | var player_type_embed;
14 | var player_type;
15 | // JavaScript Document
16 |
17 | var _iobuf = function(){
18 | this._ptr=char_;//*
19 | this._ptr_off=0;//*
20 | this._cnt=int_;
21 | this._base=char_;//*
22 | this._base_off=0;//*
23 | this._flag=int_;
24 | this._file=int_;
25 | this._charbuf=int_;
26 | this._bufsiz=int_;
27 | this._tmpfname=char_;//*
28 | this._tmpfname_off=0;//*
29 | };
30 | var FILE=_iobuf;
31 |
32 | var EOF = (-1);
33 |
34 | //FILE * fopen ( const char * filename, const char * mode );
35 | function fopen ( filename, mode ) {
36 | var f = new FILE();
37 | f._ptr=filename;
38 | return f;
39 | }
40 |
41 | //int fgetc ( FILE * stream );
42 | function fgetc ( stream ) {
43 | if(stream._ptr.length<=stream._ptr_off) return EOF;
44 | return stream._ptr[stream._ptr_off++];
45 | }
46 |
47 | //fread
48 | //size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
49 | function fread ( ptr, size, count, stream ) {
50 | //ptr.val=new Array();
51 | //todo: size include
52 |
53 | if(stream._ptr.length
187 | player.innerHTML ='';
188 | }
189 | document.getElementById('player-container').appendChild(player);
190 | }
191 | function uint8ToString(buf) {
192 | var i, length, out = '';
193 | for (i = 0, length = buf.length; i < length; i += 1) {
194 | out += String.fromCharCode(buf[i]);
195 | }
196 | return out;
197 | }
198 |
199 | // Base 64 encoding function, for browsers that do not support btoa()
200 | // by Tyler Akins (http://rumkin.com), available in the public domain
201 | function btoa2(input) {
202 | var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
203 |
204 | var output = "";
205 | var chr1, chr2, chr3;
206 | var enc1, enc2, enc3, enc4;
207 | var i = 0;
208 |
209 | do {
210 | chr1 = input[i++];
211 | chr2 = input[i++];
212 | chr3 = input[i++];
213 |
214 | enc1 = chr1 >> 2;
215 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
216 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
217 | enc4 = chr3 & 63;
218 |
219 | if (isNaN(chr2)) {
220 | enc3 = enc4 = 64;
221 | } else if (isNaN(chr3)) {
222 | enc4 = 64;
223 | }
224 |
225 | output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
226 | keyStr.charAt(enc3) + keyStr.charAt(enc4);
227 | } while (i < input.length);
228 |
229 | return output;
230 | }
231 |
232 | // JavaScript Document
233 |
234 | //312
235 | //////// ERROR CODES
236 |
237 | //enum STBVorbisError
238 | //{
239 | var
240 | VORBIS__no_error=0,
241 |
242 | VORBIS_need_more_data=1, // not a real error
243 |
244 | VORBIS_invalid_api_mixing=2, // can't mix API modes
245 | VORBIS_outofmem=3, // not enough memory
246 | VORBIS_feature_not_supported=4, // uses floor 0
247 | VORBIS_too_many_channels=5, // STB_VORBIS_MAX_CHANNELS is too small
248 | VORBIS_file_open_failure=6, // fopen() failed
249 | VORBIS_seek_without_length=7, // can't seek in unknown-length file
250 |
251 | VORBIS_unexpected_eof=10, // file is truncated?
252 | VORBIS_seek_invalid=11, // seek past EOF
253 |
254 | // decoding errors (corrupt/invalid stream) -- you probably
255 | // don't care about the exact details of these
256 |
257 | // vorbis errors:
258 | VORBIS_invalid_setup=20,
259 | VORBIS_invalid_stream=21,
260 |
261 | // ogg errors:
262 | VORBIS_missing_capture_pattern=30,
263 | VORBIS_invalid_stream_structure_version=31,
264 | VORBIS_continued_packet_flag_invalid=32,
265 | VORBIS_incorrect_stream_serial_number=33,
266 | VORBIS_invalid_first_page=34,
267 | VORBIS_bad_packet_type=35,
268 | VORBIS_cant_find_last_page=36,
269 | VORBIS_seek_failed=37
270 | //}
271 | ;
272 |
273 |
274 | //391
275 | // STB_VORBIS_MAX_CHANNELS [number]
276 | // globally define this to the maximum number of channels you need.
277 | // The spec does not put a restriction on channels except that
278 | // the count is stored in a byte, so 255 is the hard limit.
279 | // Reducing this saves about 16 bytes per value, so using 16 saves
280 | // (255-16)*16 or around 4KB. Plus anything other memory usage
281 | // I forgot to account for. Can probably go as low as 8 (7.1 audio),
282 | // 6 (5.1 audio), or 2 (stereo only).
283 | //#ifndef STB_VORBIS_MAX_CHANNELS
284 | var STB_VORBIS_MAX_CHANNELS = 16; // enough for anyone?
285 | //#endif
286 |
287 | //403
288 | // STB_VORBIS_PUSHDATA_CRC_COUNT [number]
289 | // after a flush_pushdata(), stb_vorbis begins scanning for the
290 | // next valid page, without backtracking. when it finds something
291 | // that looks like a page, it streams through it and verifies its
292 | // CRC32. Should that validation fail, it keeps scanning. But it's
293 | // possible that _while_ streaming through to check the CRC32 of
294 | // one candidate page, it sees another candidate page. This #define
295 | // determines how many "overlapping" candidate pages it can search
296 | // at once. Note that "real" pages are typically ~4KB to ~8KB, whereas
297 | // garbage pages could be as big as 64KB, but probably average ~16KB.
298 | // So don't hose ourselves by scanning an apparent 64KB page and
299 | // missing a ton of real ones in the interim; so minimum of 2
300 | //#ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
301 | var STB_VORBIS_PUSHDATA_CRC_COUNT = 4;
302 | //#endif
303 |
304 | //419
305 | // STB_VORBIS_FAST_HUFFMAN_LENGTH [number]
306 | // sets the log size of the huffman-acceleration table. Maximum
307 | // supported value is 24. with larger numbers, more decodings are O(1),
308 | // but the table size is larger so worse cache missing, so you'll have
309 | // to probe (and try multiple ogg vorbis files) to find the sweet spot.
310 | //#ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
311 | var STB_VORBIS_FAST_HUFFMAN_LENGTH = 10;
312 | //#endif
313 |
314 | //571
315 | //#ifdef STB_VORBIS_CODEBOOK_FLOATS
316 | var codetype=float_;
317 | //#else
318 | //typedef uint16 codetype;
319 | //#endif
320 |
321 | // @NOTE
322 | //
323 | // Some arrays below are tagged "//varies", which means it's actually
324 | // a variable-sized piece of data, but rather than malloc I assume it's
325 | // small enough it's better to just allocate it all together with the
326 | // main thing
327 | //
328 | // Most of the variables are specified with the smallest size I could pack
329 | // them into. It might give better performance to make them all full-sized
330 | // integers. It should be safe to freely rearrange the structures or change
331 | // the sizes larger--nothing relies on silently truncating etc., nor the
332 | // order of variables.
333 |
334 | var FAST_HUFFMAN_TABLE_SIZE = (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH);
335 | var FAST_HUFFMAN_TABLE_MASK = (FAST_HUFFMAN_TABLE_SIZE - 1);
336 |
337 | //593
338 | var Codebook = function()
339 | {
340 | this.dimensions=int_, this.entries=int_;
341 | this.codeword_lengths=uint8;//*
342 | this.minimum_value=float_;
343 | this.delta_value=float_;
344 | this.value_bits=uint8;
345 | this.lookup_type=uint8;
346 | this.sequence_p=uint8;
347 | this.sparse=uint8;
348 | this.lookup_values=uint32;
349 | this.multiplicands=codetype;//*
350 | this.codewords=uint32;//*
351 | // #ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
352 | this.fast_huffman=new Array(FAST_HUFFMAN_TABLE_SIZE);//int16
353 | // #else
354 | // int32 fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
355 | // #endif
356 | this.sorted_codewords=uint32;//*
357 | this.sorted_values=int_;//*
358 | this.sorted_values_off=0;//*
359 | this.sorted_entries=int_;
360 | };
361 |
362 | //616
363 | var Floor0 = function()
364 | {
365 | this.order=uint8;
366 | this.rate=uint16;
367 | this.bark_map_size=uint16;
368 | this.amplitude_bits=uint8;
369 | this.amplitude_offset=uint8;
370 | this.number_of_books=uint8;
371 | this.book_list=new Array(16);//uint8 // varies
372 | };
373 |
374 | //627
375 | var Floor1 = function()
376 | {
377 | this.partitions=uint8;
378 | this.partition_class_list=new Array(32);//uint8 // varies
379 | this.class_dimensions=new Array(16);//uint8 // varies
380 | this.class_subclasses=new Array(16);//uint8 // varies
381 | this.class_masterbooks=new Array(16);//uint8 // varies
382 | this.subclass_books=new Array(16);for(var i=0;i<16;i++)this.subclass_books[i]=new Array(8);//int16 // varies
383 | this.Xlist=new Array(31*8+2);//uint16 // varies
384 | this.sorted_order=new Array(31*8+2);//uint8
385 | this.neighbors=new Array(31*8+2);for(var i=0;i<31*8+2;i++)this.neighbors[i]=new Array(2);//uint8
386 | this.floor1_multiplier=uint8;
387 | this.rangebits=uint8;
388 | this.values=int_;
389 | };
390 |
391 | //643
392 | var Floor = function() //union
393 | {
394 | this.floor0=new Floor0();
395 | this.floor1=new Floor1();
396 | };
397 |
398 | //649
399 | var Residue = function()
400 | {
401 | this.begin=uint32, this.end=uint32;
402 | this.part_size=uint32;
403 | this.classifications=uint8;
404 | this.classbook=uint8;
405 | this.classdata=uint8;//**
406 | this.residue_books=new Array(8);//int16 //(*)
407 | };
408 |
409 | //659
410 | var MappingChannel = function()
411 | {
412 | this.magnitude=uint8;
413 | this.angle=uint8;
414 | this.mux=uint8;
415 | };
416 |
417 | //666
418 | var Mapping = function()
419 | {
420 | this.coupling_steps=uint16;
421 | this.chan='MappingChannel';//*
422 | this.submaps=uint8;
423 | this.submap_floor=new Array(15);//uint8 // varies
424 | this.submap_residue=new Array(15);//uint8 // varies
425 | };
426 |
427 | //675
428 | var Mode = function()
429 | {
430 | this.blockflag=uint8;
431 | this.mapping=uint8;
432 | this.windowtype=uint16;
433 | this.transformtype=uint16;
434 | };
435 |
436 | //692
437 | var ProbedPage = function()
438 | {
439 | this.page_start, this.page_end=uint32;
440 | this.after_previous_page_start=uint32;
441 | this.first_decoded_sample=uint32;
442 | this.last_decoded_sample=uint32;
443 | };
444 |
445 | //700
446 | var stb_vorbis = function()
447 | {
448 | // user-accessible info
449 | this.sample_rate=int_;//unsigned
450 | this.channels=int_;
451 |
452 | this.setup_memory_required=int_;//unsigned
453 | this.temp_memory_required=int_;//unsigned
454 | this.setup_temp_memory_required=int_;//unsigned
455 |
456 | // input config
457 | //#ifndef STB_VORBIS_NO_STDIO
458 | this.f='FILE';//*
459 | this.f_start=uint32;
460 | this.close_on_free=int_;
461 | //#endif
462 |
463 | this.stream=uint8;//*
464 | this.stream_off=0;//*
465 | this.stream_start=uint8;//*
466 | this.stream_start_off=0;//*
467 | this.stream_end=uint8;//*
468 | this.stream_end_off=0;//*
469 |
470 | this.stream_len=uint32;
471 |
472 | this.push_mode=uint8;
473 |
474 | this.first_audio_page_offset=uint32;
475 |
476 | this.p_first='ProbedPage', this.p_last='ProbedPage';
477 |
478 | // memory management
479 | this.alloc='stb_vorbis_alloc';
480 | this.setup_offset=int_;
481 | this.temp_offset=int_;
482 |
483 | // run-time results
484 | this.eof=int_;
485 | this.error='enum STBVorbisError';
486 |
487 | // user-useful data
488 |
489 | // header info
490 | this.blocksize=new Array(2);//int
491 | this.blocksize_0=int_, this.blocksize_1=int_;
492 | this.codebook_count=int_;
493 | this.codebooks='Codebook';//*
494 | this.floor_count=int_;
495 | this.floor_types=new Array(64);//uint16 // varies
496 | this.floor_config='Floor';//*
497 | this.residue_count=int_;
498 | this.residue_types=new Array(64);//uint16 // varies
499 | this.residue_config='Residue';//*
500 | this.mapping_count=int_;
501 | this.mapping='Mapping';//*
502 | this.mode_count=int_;
503 | this.mode_config=Arr_new(64,Mode); // varies
504 |
505 | this.total_samples=uint32;
506 |
507 | // decode buffer
508 | this.channel_buffers=new Array(STB_VORBIS_MAX_CHANNELS);//float *
509 | this.outputs =new Array(STB_VORBIS_MAX_CHANNELS);//float *
510 |
511 | this.previous_window=new Array(STB_VORBIS_MAX_CHANNELS);//float *
512 | this.previous_length=int_;
513 |
514 | //#ifndef STB_VORBIS_NO_DEFER_FLOOR
515 | this.finalY=new Array(STB_VORBIS_MAX_CHANNELS);//int16 *
516 | //#else
517 | //float *floor_buffers[STB_VORBIS_MAX_CHANNELS];
518 | //#endif
519 |
520 | this.current_loc=uint32; // sample location of next frame to decode
521 | this.current_loc_valid=int_;
522 |
523 | // per-blocksize precomputed data
524 |
525 | // twiddle factors
526 | this.A=new Array(2),this.B=new Array(2),this.C=new Array(2);//float *,*,*
527 | this.window_=new Array(2);//float *
528 | this.bit_reverse=new Array(2);//uint16 *
529 |
530 | // current page/packet/segment streaming info
531 | this.serial=uint32; // stream serial number for verification
532 | this.last_page=int_;
533 | this.segment_count=int_;
534 | this.segments=Arr(255,uint8);//
535 | this.page_flag=uint8;
536 | this.bytes_in_seg=uint8;
537 | this.first_decode=uint8;
538 | this.next_seg=int_;
539 | this.last_seg=int_; // flag that we're on the last segment
540 | this.last_seg_which=int_; // what was the segment number of the last seg?
541 | this.acc=uint32;
542 | this.valid_bits=int_;
543 | this.packet_bytes=int_;
544 | this.end_seg_with_known_loc=int_;
545 | this.known_loc_for_packet=uint32;
546 | this.discard_samples_deferred=int_;
547 | this.samples_output=uint32;
548 |
549 | // push mode scanning
550 | this.page_crc_tests=int_; // only in push_mode: number of tests active; -1 if not searching
551 | //#ifndef STB_VORBIS_NO_PUSHDATA_API
552 | this.scan=new Array(STB_VORBIS_PUSHDATA_CRC_COUNT);//CRCscan
553 | //#endif
554 |
555 | // sample-access
556 | this.channel_buffer_start=int_;
557 | this.channel_buffer_end=int_;
558 | };
559 |
560 | //818
561 | //extern int my_prof(int slot);
562 | ////#define stb_prof my_prof
563 | //
564 | //#ifndef stb_prof
565 | function stb_prof(x) { return 0 }
566 | //#endif
567 |
568 | //818
569 | //#if defined(STB_VORBIS_NO_PUSHDATA_API)
570 | // #define IS_PUSH_MODE(f) FALSE
571 | //#elif defined(STB_VORBIS_NO_PULLDATA_API)
572 | // #define IS_PUSH_MODE(f) TRUE
573 | //#else
574 | function IS_PUSH_MODE(f) { return ((f).push_mode) }
575 | //#endif
576 |
577 | //851
578 | function temp_alloc_save(f) { return ((f).temp_offset) }
579 | function temp_alloc_restore(f,p) { return ((f).temp_offset = (p)) }
580 |
581 | var CRC32_POLY = 0x04c11db7; // from spec
582 |
583 | var crc_table=new Array(256);//static uint32
584 | //911
585 | function crc32_init()//void
586 | {
587 | var i=int_,j=int_;
588 | var s=uint32;
589 | for(i=0; i < 256; i++) {
590 | for (s=(i<<24)>>>0, j=0; j < 8; ++j)
591 | s = ((s << 1) ^ (s >= ((1<<31)>>>0) ? CRC32_POLY : 0))>>>0;
592 | crc_table[i] = s;
593 | }
594 | }
595 |
596 | // used in setup, and for huffman that doesn't go fast path
597 | //929
598 | function bit_reverse(n)
599 | {
600 | n = (((n & 0xAAAAAAAA) >>> 1) | ((n & 0x55555555) << 1))>>>0;
601 | n = (((n & 0xCCCCCCCC) >>> 2) | ((n & 0x33333333) << 2))>>>0;
602 | n = (((n & 0xF0F0F0F0) >>> 4) | ((n & 0x0F0F0F0F) << 4))>>>0;
603 | n = (((n & 0xFF00FF00) >>> 8) | ((n & 0x00FF00FF) << 8))>>>0;
604 | return ((n >>> 16) | (n << 16))>>>0;
605 | }
606 |
607 | //938
608 | function square(x)
609 | {
610 | return x*x;
611 | }
612 |
613 | // this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3
614 | // as required by the specification. fast(?) implementation from stb.h
615 | // @OPTIMIZE: called multiple times per-packet with "constants"; move to setup
616 | //946
617 | function ilog(n)
618 | {
619 | var log2_4 = new Array( 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 );//static signed char_
620 |
621 | // 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)
622 | if (n < (1 << 14))//U
623 | if (n < (1 << 4)) return 0 + log2_4[n ];//U
624 | else if (n < (1 << 9)) return 5 + log2_4[n >>> 5];//U
625 | else return 10 + log2_4[n >>> 10];
626 | else if (n < (1 << 24))//U
627 | if (n < (1 << 19)) return 15 + log2_4[n >>> 15];//U
628 | else return 20 + log2_4[n >>> 20];
629 | else if (n < (1 << 29)) return 25 + log2_4[n >>> 25];//U
630 | else if (n < (1 << 31)>>>0) return 30 + log2_4[n >>> 30];//U
631 | else return 0; // signed n returns 0
632 | }
633 |
634 | //#ifndef M_PI
635 | //964
636 | var M_PI = 3.14159265358979323846264;//f // from CRC
637 | //#endif
638 |
639 | //967
640 | // code length assigned to a value with no huffman encoding
641 | var NO_CODE = 255;
642 |
643 | /////////////////////// LEAF SETUP FUNCTIONS //////////////////////////
644 | //
645 | // these functions are only called at setup, and only a few times
646 | // per file
647 |
648 | //975
649 | function float32_unpack(x)
650 | {
651 | // from the specification
652 | var mantissa = (x & 0x1fffff)>>>0;//uint32
653 | var sign = (x & 0x80000000)>>>0;//uint32
654 | var exp_ = (x & 0x7fe00000) >>> 21;//uint32
655 | var res = sign ? -mantissa : mantissa;//double (double):(double)
656 | return res*Math.pow(2,exp_-788);//(float) ldexp((float), )
657 | }
658 |
659 |
660 | // zlib & jpeg huffman tables assume that the output symbols
661 | // can either be arbitrarily arranged, or have monotonically
662 | // increasing frequencies--they rely on the lengths being sorted;
663 | // this makes for a very simple generation algorithm.
664 | // vorbis allows a huffman table with non-sorted lengths. This
665 | // requires a more sophisticated construction, since symbols in
666 | // order do not map to huffman codes "in order".
667 | //993
668 | function add_entry(c, huff_code, symbol, count, len, values)
669 | {
670 | if (!c.sparse) {
671 | c.codewords [symbol] = huff_code;
672 | } else {
673 | c.codewords [count] = huff_code;
674 | c.codeword_lengths[count] = len;
675 | values [count] = symbol;
676 | }
677 | }
678 |
679 | //1004
680 | function compute_codewords(c, len, n, values)
681 | {
682 | var i=int_,k=int_,m=0;//int_
683 | var available=new Array(32);//uint32
684 |
685 | memset(available, 0, 0, 32);//sizeof(available)
686 | // find the first entry
687 | for (k=0; k < n; ++k) if (len[k] < NO_CODE) break;
688 | if (k == n) { assert(c.sorted_entries == 0); return true; }
689 | // add to the list
690 | add_entry(c, 0, k, m++, len[k], values);
691 | // add all available leaves
692 | for (i=1; i <= len[k]; ++i)
693 | available[i] = (1 << (32-i))>>>0;
694 | // note that the above code treats the first case specially,
695 | // but it's really the same as the following code, so they
696 | // could probably be combined (except the initial code is 0,
697 | // and I use 0 in available[] to mean 'empty')
698 | for (i=k+1; i < n; ++i) {
699 | var res=uint32;
700 | var z = len[i], y=int_;//int_
701 | if (z == NO_CODE) continue;
702 | // find lowest available leaf (should always be earliest,
703 | // which is what the specification calls for)
704 | // note that this property, and the fact we can never have
705 | // more than one free leaf at a given level, isn't totally
706 | // trivial to prove, but it seems true and the assert never
707 | // fires, so!
708 | while (z > 0 && !available[z]) --z;
709 | if (z == 0) { assert(0); return false; }
710 | res = available[z];
711 | available[z] = 0;
712 | add_entry(c, bit_reverse(res), i, m++, len[i], values);
713 | // propogate availability up the tree
714 | if (z != len[i]) {
715 | for (y=len[i]; y > z; --y) {
716 | assert(available[y] == 0);
717 | available[y] = res + ((1 << (32-y)))>>>0;
718 | }
719 | }
720 | }
721 | return true;
722 | }
723 |
724 | // accelerated huffman table allows fast O(1) match of all symbols
725 | // of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH
726 | //1050
727 | function compute_accelerated_huffman(c)
728 | {
729 | var i=int_, len=int_;
730 | for (i=0; i < FAST_HUFFMAN_TABLE_SIZE; ++i)
731 | c.fast_huffman[i] = -1;
732 |
733 | len = c.sparse ? c.sorted_entries : c.entries;
734 | //#ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
735 | if (len > 32767) len = 32767; // largest possible value we can encode!
736 | //#endif
737 | for (i=0; i < len; ++i) {
738 | if (c.codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {
739 | var z = c.sparse ? bit_reverse(c.sorted_codewords[i]) : c.codewords[i];//uint32
740 | // set table entries for all bit combinations in the higher bits
741 | while (z < FAST_HUFFMAN_TABLE_SIZE) {
742 | c.fast_huffman[z] = i;
743 | z += (1 << c.codeword_lengths[i])>>>0;
744 | }
745 | }
746 | }
747 | }
748 |
749 | //1072
750 | function uint32_compare(p, q)
751 | {
752 | var x = p;//uint32 = * (uint32 *)
753 | var y = q;//uint32 = * (uint32 *)
754 | return x < y ? -1 : x > y;
755 | }
756 |
757 | //1079
758 | function include_in_sort(c, len)
759 | {
760 | if (c.sparse) { assert(len != NO_CODE); return true; }
761 | if (len == NO_CODE) return false;
762 | if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return true;
763 | return false;
764 | }
765 |
766 | // if the fast table above doesn't work, we want to binary
767 | // search them... need to reverse the bits
768 | //1089
769 | function compute_sorted_huffman(c, lengths, values)
770 | {
771 | var i=int_, len=int_;
772 | // build a list of all the entries
773 | // OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.
774 | // this is kind of a frivolous optimization--I don't see any performance improvement,
775 | // but it's like 4 extra lines of code, so.
776 | if (!c.sparse) {
777 | var k = 0;//int_
778 | for (i=0; i < c.entries; ++i)
779 | if (include_in_sort(c, lengths[i]))
780 | c.sorted_codewords[k++] = bit_reverse(c.codewords[i]);
781 | assert(k == c.sorted_entries);
782 | } else {
783 | for (i=0; i < c.sorted_entries; ++i)
784 | c.sorted_codewords[i] = bit_reverse(c.codewords[i]);
785 | }
786 |
787 | c.sorted_codewords.sort(uint32_compare);//qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare);
788 | c.sorted_codewords[c.sorted_entries] = 0xffffffff;
789 |
790 | len = c.sparse ? c.sorted_entries : c.entries;
791 | // now we need to indicate how they correspond; we could either
792 | // #1: sort a different data structure that says who they correspond to
793 | // #2: for each sorted entry, search the original list to find who corresponds
794 | // #3: for each original entry, find the sorted entry
795 | // #1 requires extra storage, #2 is slow, #3 can use binary search!
796 | for (i=0; i < len; ++i) {
797 | var huff_len = c.sparse ? lengths[values[i]] : lengths[i];//int_
798 | if (include_in_sort(c,huff_len)) {
799 | var code = bit_reverse(c.codewords[i]);//uint32
800 | var x=0, n=c.sorted_entries;//int_
801 | while (n > 1) {
802 | // invariant: sc[x] <= code < sc[x+n]
803 | var m = x + (n >>> 1);//int_
804 | if (c.sorted_codewords[m] <= code) {
805 | x = m;
806 | n -= (n>>>1);
807 | } else {
808 | n >>>= 1;
809 | }
810 | }
811 | assert(c.sorted_codewords[x] == code);
812 | if (c.sparse) {
813 | c.sorted_values[x] = values[i];
814 | c.codeword_lengths[x] = huff_len;
815 | } else {
816 | c.sorted_values[x] = i;
817 | }
818 | }
819 | }
820 | }
821 |
822 | //1142
823 | // only run while parsing the header (3 times)
824 | function vorbis_validate(data)
825 | {
826 | var vorbis = new Array( 118, 111, 114, 98, 105, 115 );//new Array( 'v', 'o', 'r', 'b', 'i', 's' );//static uint8
827 | return memcmp(data, vorbis, 6) == 0;
828 | }
829 |
830 | // called from setup only, once per code book
831 | // (formula implied by specification)
832 | //1151
833 | function lookup1_values(entries, dim)
834 | {
835 | var r = Math.floor(Math.exp(Math.log(entries) / dim));//int_ = (int_) (float_) (float_)
836 | if (Math.floor(Math.pow(r+1, dim)) <= entries) //(int_) (float_) // (int) cast for MinGW warning;
837 | ++r; // floor() to avoid _ftol() when non-CRT
838 | assert(Math.pow( r+1, dim) > entries);//(float_)
839 | assert(Math.floor(Math.pow(r, dim)) <= entries); //(int_) (float) // (int),floor() as above
840 | return r;
841 | }
842 |
843 | // called twice per file
844 | //1162
845 | function compute_twiddle_factors(n, A, B, C)
846 | {
847 | var n4 = n >>> 2, n8 = n >>> 3;//int_
848 | var k=int_,k2=int_;
849 |
850 | for (k=k2=0; k < n4; ++k,k2+=2) {
851 | A[k2 ] = Math.cos(4*k*M_PI/n);//(float_)
852 | A[k2+1] = -Math.sin(4*k*M_PI/n);//(float_)
853 | B[k2 ] = Math.cos((k2+1)*M_PI/n/2) * 0.5;//f //(float_)
854 | B[k2+1] = Math.sin((k2+1)*M_PI/n/2) * 0.5;//f //(float_)
855 | }
856 | for (k=k2=0; k < n8; ++k,k2+=2) {
857 | C[k2 ] = Math.cos(2*(k2+1)*M_PI/n);//(float_)
858 | C[k2+1] = -Math.sin(2*(k2+1)*M_PI/n);//(float_)
859 | }
860 | }
861 |
862 | //1179
863 | function compute_window(n, window_)
864 | {
865 | var n2 = n >> 1, i=int_;//int_
866 | for (i=0; i < n2; ++i)
867 | window_[i] = Math.sin(0.5 * M_PI * square(Math.sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));//(float) (float)
868 | }
869 |
870 | //1186
871 | function compute_bitreverse(n, rev)
872 | {
873 | var ld = ilog(n) - 1;//int_ // ilog is off-by-one from normal definitions
874 | var i=int_, n8 = n >>> 3;//int_
875 | for (i=0; i < n8; ++i)
876 | rev[i] = (bit_reverse(i) >>> (32-ld+3)) << 2;
877 | }
878 |
879 | //1194
880 | function init_blocksize(f, b, n)
881 | {
882 | var n2 = n >>> 1, n4 = n >>> 2, n8 = n >>> 3;//int_
883 | f.A[b] = new Array(n2);//(float *) setup_malloc(f, sizeof(float) * n2);
884 | f.B[b] = new Array(n2);//(float *) setup_malloc(f, sizeof(float) * n2);
885 | f.C[b] = new Array(n4);//(float *) setup_malloc(f, sizeof(float) * n4);
886 | if (!f.A[b] || !f.B[b] || !f.C[b]) return error(f, VORBIS_outofmem);
887 | compute_twiddle_factors(n, f.A[b], f.B[b], f.C[b]);
888 | f.window_[b] = new Array(n2);//(float *) setup_malloc(f, sizeof(float) * n2);
889 | if (!f.window_[b]) return error(f, VORBIS_outofmem);
890 | compute_window(n, f.window_[b]);
891 | f.bit_reverse[b] = new Array(n8);//(uint16 *) setup_malloc(f, sizeof(uint16) * n8);
892 | if (!f.bit_reverse[b]) return error(f, VORBIS_outofmem);
893 | compute_bitreverse(n, f.bit_reverse[b]);
894 | return true;
895 | }
896 |
897 | //1211
898 | function neighbors(x, n, plow, phigh)
899 | {
900 | var low = -1;//int_
901 | var high = 65536;//int_
902 | var i;//int_
903 | for (i=0; i < n; ++i) {
904 | if (x[i] > low && x[i] < x[n]) { plow[0] = i; low = x[i]; }
905 | if (x[i] < high && x[i] > x[n]) { phigh[0] = i; high = x[i]; }
906 | }
907 | }
908 |
909 | // this has been repurposed so y is now the original index instead of y
910 | //1223
911 | var Point = function()
912 | {
913 | this.x=52428,this.y=52428;//uint16,uint16
914 | };
915 |
916 | //1228
917 | function point_compare(p, q)
918 | {
919 | var a = p;//Point * = (Point *)
920 | var b = q;//Point * = (Point *)
921 | return a.x < b.x ? -1 : a.x > b.x;
922 | }
923 |
924 | //
925 | /////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////
926 |
927 |
928 | //#if defined(STB_VORBIS_NO_STDIO)
929 | // #define USE_MEMORY(z) TRUE
930 | //#else
931 | function USE_MEMORY(z) { return ((z).stream) }
932 | //#endif
933 |
934 | //1245
935 | function get8(z)
936 | {
937 | if (USE_MEMORY(z)) {
938 | if (z.stream_off >= z.stream_end_off) { z.eof = true; return 0; }
939 | return z.stream_off++;//*
940 | }
941 |
942 | //#ifndef STB_VORBIS_NO_STDIO
943 | {
944 | var c = fgetc(z.f);//int_
945 | if (c == EOF) { z.eof = true; return 0; }
946 | return c;
947 | }
948 | //#endif
949 | }
950 |
951 | //1261
952 | function get32(f)
953 | {
954 | var x=uint32;
955 | x = get8(f);
956 | x += get8(f) << 8;
957 | x += get8(f) << 16;
958 | x += get8(f) << 24;
959 | return x;
960 | }
961 |
962 | //1271
963 | function getn(z, data, n)
964 | {
965 | if (USE_MEMORY(z)) {
966 | if (z.stream_off+n > z.stream_end_off) { z.eof = 1; return 0; }
967 | memcpy(data, 0, z.stream, z.stream_off, n);
968 | z.stream_off += n;
969 | return 1;
970 | }
971 |
972 | //#ifndef STB_VORBIS_NO_STDIO
973 | if (fread(data, n, 1, z.f) == 1)
974 | return 1;
975 | else {
976 | z.eof = 1;
977 | return 0;
978 | }
979 | //#endif
980 | }
981 |
982 | //1290
983 | function skip(z, n)
984 | {
985 | if (USE_MEMORY(z)) {
986 | z.stream_off += n;
987 | if (z.stream_off >= z.stream_end_off) z.eof = 1;
988 | return;
989 | }
990 | //#ifndef STB_VORBIS_NO_STDIO
991 | {
992 | var x = ftell(z.f);//long
993 | fseek(z.f, x+n, SEEK_SET);
994 | }
995 | //#endif
996 | }
997 |
998 | //1339
999 | function capture_pattern(f)
1000 | {
1001 | if (0x4f != get8(f)) return false;
1002 | if (0x67 != get8(f)) return false;
1003 | if (0x67 != get8(f)) return false;
1004 | if (0x53 != get8(f)) return false;
1005 | return true;
1006 | }
1007 |
1008 | var PAGEFLAG_continued_packet = 1;
1009 | var PAGEFLAG_first_page = 2;
1010 | var PAGEFLAG_last_page = 4;
1011 |
1012 | //1352
1013 | function start_page_no_capturepattern(f)
1014 | {
1015 | var loc0=uint32,loc1=uint32,n=uint32,i=uint32;
1016 | // stream structure version
1017 | if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);
1018 | // header flag
1019 | f.page_flag = get8(f);
1020 | // absolute granule position
1021 | loc0 = get32(f);
1022 | loc1 = get32(f);
1023 | // @TODO: validate loc0,loc1 as valid positions?
1024 | // stream serial number -- vorbis doesn't interleave, so discard
1025 | get32(f);
1026 | //if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number);
1027 | // page sequence number
1028 | n = get32(f);
1029 | f.last_page = n;
1030 | // CRC32
1031 | get32(f);
1032 | // page_segments
1033 | f.segment_count = get8(f);
1034 | if (!getn(f, f.segments, f.segment_count))
1035 | return error(f, VORBIS_unexpected_eof);
1036 | // assume we _don't_ know any the sample position of any segments
1037 | f.end_seg_with_known_loc = -2;
1038 | if (loc0 != ~0 || loc1 != ~0) {
1039 | // determine which packet is the last one that will complete
1040 | for (i=f.segment_count-1; i >= 0; --i)
1041 | if (f.segments[i] < 255)
1042 | break;
1043 | // 'i' is now the index of the _last_ segment of a packet that ends
1044 | if (i >= 0) {
1045 | f.end_seg_with_known_loc = i;
1046 | f.known_loc_for_packet = loc0;
1047 | }
1048 | }
1049 | if (f.first_decode) {
1050 | var i=int_,len=int_;
1051 | var p=new ProbedPage();
1052 | len = 0;
1053 | for (i=0; i < f.segment_count; ++i)
1054 | len += f.segments[i];
1055 | len += 27 + f.segment_count;
1056 | p.page_start = f.first_audio_page_offset;
1057 | p.page_end = p.page_start + len;
1058 | p.after_previous_page_start = p.page_start;
1059 | p.first_decoded_sample = 0;
1060 | p.last_decoded_sample = loc0;
1061 | f.p_first = p;
1062 | }
1063 | f.next_seg = 0;
1064 | return true;
1065 | }
1066 |
1067 | //1406
1068 | function start_page(f)
1069 | {
1070 | if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern);
1071 | return start_page_no_capturepattern(f);
1072 | }
1073 |
1074 | //1412
1075 | function start_packet(f)
1076 | {
1077 | while (f.next_seg == -1) {
1078 | if (!start_page(f)) return false;
1079 | if (f.page_flag & PAGEFLAG_continued_packet)
1080 | return error(f, VORBIS_continued_packet_flag_invalid);
1081 | }
1082 | f.last_seg = false;
1083 | f.valid_bits = 0;
1084 | f.packet_bytes = 0;
1085 | f.bytes_in_seg = 0;
1086 | // f->next_seg is now valid
1087 | return true;
1088 | }
1089 |
1090 | //1427
1091 | function maybe_start_packet(f)
1092 | {
1093 | if (f.next_seg == -1) {
1094 | var x = get8(f);//int_
1095 | if (f.eof) return false; // EOF at page boundary is not an error!
1096 | if (0x4f != x ) return error(f, VORBIS_missing_capture_pattern);
1097 | if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
1098 | if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
1099 | if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
1100 | if (!start_page_no_capturepattern(f)) return false;
1101 | if (f.page_flag & PAGEFLAG_continued_packet) {
1102 | // set up enough state that we can read this packet if we want,
1103 | // e.g. during recovery
1104 | f.last_seg = false;
1105 | f.bytes_in_seg = 0;
1106 | return error(f, VORBIS_continued_packet_flag_invalid);
1107 | }
1108 | }
1109 | return start_packet(f);
1110 | }
1111 |
1112 | //1448
1113 | function next_segment(f)
1114 | {
1115 | var len=int_;
1116 | if (f.last_seg) return 0;
1117 | if (f.next_seg == -1) {
1118 | f.last_seg_which = f.segment_count-1; // in case start_page fails
1119 | if (!start_page(f)) { f.last_seg = 1; return 0; }
1120 | if (!(f.page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid);
1121 | }
1122 | len = f.segments[f.next_seg++];
1123 | if (len < 255) {
1124 | f.last_seg = true;
1125 | f.last_seg_which = f.next_seg-1;
1126 | }
1127 | if (f.next_seg >= f.segment_count)
1128 | f.next_seg = -1;
1129 | assert(f.bytes_in_seg == 0);
1130 | f.bytes_in_seg = len;
1131 | return len;
1132 | }
1133 |
1134 | var EOP = (-1);
1135 | var INVALID_BITS = (-1);
1136 |
1137 | //1472
1138 | function get8_packet_raw(f)
1139 | {
1140 | if (!f.bytes_in_seg)
1141 | if (f.last_seg) return EOP;
1142 | else if (!next_segment(f)) return EOP;
1143 | assert(f.bytes_in_seg > 0);
1144 | --f.bytes_in_seg;
1145 | ++f.packet_bytes;
1146 | return get8(f);
1147 | }
1148 |
1149 | //1483
1150 | function get8_packet(f)
1151 | {
1152 | var x = get8_packet_raw(f);//int_
1153 | f.valid_bits = 0;
1154 | return x;
1155 | }
1156 |
1157 | //1490
1158 | function flush_packet(f)
1159 | {
1160 | while (get8_packet_raw(f) != EOP);
1161 | }
1162 |
1163 | // @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important
1164 | // as the huffman decoder?
1165 | //1497
1166 | function get_bits(f, n)
1167 | {
1168 | var z=uint32;
1169 |
1170 | if (f.valid_bits < 0) return 0;
1171 | if (f.valid_bits < n) {
1172 | if (n > 24) {
1173 | // the accumulator technique below would not work correctly in this case
1174 | z = get_bits(f, 24);
1175 | z += (get_bits(f, n-24) << 24)>>>0;
1176 | return z;
1177 | }
1178 | if (f.valid_bits == 0) f.acc = 0;
1179 | while (f.valid_bits < n) {
1180 | var z = get8_packet_raw(f);//int_
1181 | if (z == EOP) {
1182 | f.valid_bits = INVALID_BITS;
1183 | return 0;
1184 | }
1185 | f.acc += (z << f.valid_bits)>>>0;
1186 | f.valid_bits += 8;
1187 | }
1188 | }
1189 | if (f.valid_bits < 0) return 0;
1190 | z = f.acc & (((1 << n)>>>0)-1);
1191 | f.acc >>>= n;
1192 | f.valid_bits -= n;
1193 | return z;
1194 | }
1195 |
1196 | // @OPTIMIZE: primary accumulator for huffman
1197 | // expand the buffer to as many bits as possible without reading off end of packet
1198 | // it might be nice to allow f->valid_bits and f->acc to be stored in registers,
1199 | // e.g. cache them locally and decode locally
1200 | //1539
1201 | function prep_huffman(f)
1202 | {
1203 | if (f.valid_bits <= 24) {
1204 | if (f.valid_bits == 0) f.acc = 0;
1205 | do {
1206 | var z=int_;
1207 | if (f.last_seg && !f.bytes_in_seg) return;
1208 | z = get8_packet_raw(f);
1209 | if (z == EOP) return;
1210 | f.acc += (z << f.valid_bits)>>>0;
1211 | f.valid_bits += 8;
1212 | } while (f.valid_bits <= 24);
1213 | }
1214 | }
1215 |
1216 | //1554
1217 | //enum
1218 | //{
1219 | var
1220 | VORBIS_packet_id = 1,
1221 | VORBIS_packet_comment = 3,
1222 | VORBIS_packet_setup = 5//,
1223 | //}
1224 | ;
1225 |
1226 | //1561
1227 | function codebook_decode_scalar_raw(f, c)
1228 | {
1229 | var i=int_;
1230 | prep_huffman(f);
1231 |
1232 | assert(c.sorted_codewords || c.codewords);
1233 | // cases to use binary search: sorted_codewords && !c->codewords
1234 | // sorted_codewords && c->entries > 8
1235 | if (c.entries > 8 ? c.sorted_codewords!=null : !c.codewords) {
1236 | // binary search
1237 | var code = bit_reverse(f.acc);//uint32
1238 | var x=0, n=c.sorted_entries, len=int_;//int_
1239 |
1240 | while (n > 1) {
1241 | // invariant: sc[x] <= code < sc[x+n]
1242 | var m = x + (n >>> 1);//int_
1243 | if (c.sorted_codewords[m] <= code) {
1244 | x = m;
1245 | n -= (n>>>1);
1246 | } else {
1247 | n >>>= 1;
1248 | }
1249 | }
1250 | // x is now the sorted index
1251 | if (!c.sparse) x = c.sorted_values[x];
1252 | // x is now sorted index if sparse, or symbol otherwise
1253 | len = c.codeword_lengths[x];
1254 | if (f.valid_bits >= len) {
1255 | f.acc >>>= len;
1256 | f.valid_bits -= len;
1257 | return x;
1258 | }
1259 |
1260 | f.valid_bits = 0;
1261 | return -1;
1262 | }
1263 |
1264 | // if small, linear search
1265 | assert(!c.sparse);
1266 | for (i=0; i < c.entries; ++i) {
1267 | if (c.codeword_lengths[i] == NO_CODE) continue;
1268 | if (c.codewords[i] == (f.acc & (((1 << c.codeword_lengths[i])>>>0)-1))) {
1269 | if (f.valid_bits >= c.codeword_lengths[i]) {
1270 | f.acc >>>= c.codeword_lengths[i];
1271 | f.valid_bits -= c.codeword_lengths[i];
1272 | return i;
1273 | }
1274 | f.valid_bits = 0;
1275 | return -1;
1276 | }
1277 | }
1278 |
1279 | error(f, VORBIS_invalid_stream);
1280 | f.valid_bits = 0;
1281 | return -1;
1282 | }
1283 |
1284 | //1635
1285 | //#ifndef STB_VORBIS_NO_INLINE_DECODE
1286 |
1287 | //1637
1288 | function DECODE_RAW(var_, f,c) {
1289 | if (f.valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)
1290 | prep_huffman(f);
1291 | var_[0] = f.acc & FAST_HUFFMAN_TABLE_MASK;
1292 | var_[0] = c.fast_huffman[var_[0]];
1293 | if (var_[0] >= 0) {
1294 | var n = c.codeword_lengths[var_[0]];//int_
1295 | f.acc >>>= n;
1296 | f.valid_bits -= n;
1297 | if (f.valid_bits < 0) { f.valid_bits = 0; var_[0] = -1; }
1298 | } else {
1299 | var_[0] = codebook_decode_scalar_raw(f,c);
1300 | }
1301 | }
1302 |
1303 | //#else
1304 | //
1305 | //#define DECODE_RAW(var,f,c) var = codebook_decode_scalar(f,c);
1306 | //
1307 | //#endif
1308 |
1309 | //1657
1310 | function DECODE(var_,f,c) {
1311 | DECODE_RAW(var_,f,c);
1312 | if (c.sparse) var_[0] = c.sorted_values[var_[0]];
1313 | }
1314 |
1315 | //1661
1316 | //#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1317 | function DECODE_VQ(var_,f,c) { DECODE_RAW(var_,f,c) }
1318 | //#else
1319 | // #define DECODE_VQ(var,f,c) DECODE(var,f,c)
1320 | //#endif
1321 |
1322 |
1323 |
1324 |
1325 |
1326 |
1327 | //1672
1328 | // CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case
1329 | // where we avoid one addition
1330 | //#ifndef STB_VORBIS_CODEBOOK_FLOATS
1331 | // #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off] * c->delta_value + c->minimum_value)
1332 | // #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off] * c->delta_value)
1333 | // #define CODEBOOK_ELEMENT_BASE(c) (c->minimum_value)
1334 | //#else
1335 | function CODEBOOK_ELEMENT(c,off) { return (c.multiplicands[off]) }
1336 | function CODEBOOK_ELEMENT_FAST(c,off) { return (c.multiplicands[off]) }
1337 | function CODEBOOK_ELEMENT_BASE(c) { return (0) }
1338 | //#endif
1339 |
1340 | //1684
1341 | function codebook_decode_start(f, c, len)
1342 | {
1343 | var z = [-1];//int_
1344 |
1345 | // type 0 is only legal in a scalar context
1346 | if (c.lookup_type == 0)
1347 | error(f, VORBIS_invalid_stream);
1348 | else {
1349 | DECODE_VQ(z,f,c);
1350 | if (c.sparse) assert(z < c.sorted_entries);
1351 | if (z[0] < 0) { // check for EOP
1352 | if (!f.bytes_in_seg)
1353 | if (f.last_seg)
1354 | return z[0];
1355 | error(f, VORBIS_invalid_stream);
1356 | }
1357 | }
1358 | return z[0];
1359 | }
1360 |
1361 | //1704
1362 | function codebook_decode(f, c, output, output_off, len)
1363 | {
1364 | var i=int_,z = codebook_decode_start(f,c,len);//int_
1365 | if (z < 0) return false;
1366 | if (len > c.dimensions) len = c.dimensions;
1367 |
1368 | //#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1369 | // if (c->lookup_type == 1) {
1370 | // float last = CODEBOOK_ELEMENT_BASE(c);
1371 | // int div = 1;
1372 | // for (i=0; i < len; ++i) {
1373 | // int off = (z / div) % c->lookup_values;
1374 | // float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
1375 | // output[i] += val;
1376 | // if (c->sequence_p) last = val + c->minimum_value;
1377 | // div *= c->lookup_values;
1378 | // }
1379 | // return TRUE;
1380 | // }
1381 | //#endif
1382 |
1383 | z *= c.dimensions;
1384 | if (c.sequence_p) {
1385 | var last = CODEBOOK_ELEMENT_BASE(c);//float_
1386 | for (i=0; i < len; ++i) {
1387 | var val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;//float_
1388 | output[output_off+ i] += val;
1389 | last = val + c.minimum_value;
1390 | }
1391 | } else {
1392 | var last = CODEBOOK_ELEMENT_BASE(c);//float_
1393 | for (i=0; i < len; ++i) {
1394 | output[output_off+ i] += CODEBOOK_ELEMENT_FAST(c,z+i) + last;
1395 | }
1396 | }
1397 |
1398 | return true;
1399 | }
1400 |
1401 | //1743
1402 | function codebook_decode_step(f, c, output, output_off, len, step)
1403 | {
1404 | var i=int_,z = codebook_decode_start(f,c,len);//int_
1405 | var last = CODEBOOK_ELEMENT_BASE(c);//float_
1406 | if (z < 0) return false;
1407 | if (len > c.dimensions) len = c.dimensions;
1408 |
1409 | //#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1410 | // if (c->lookup_type == 1) {
1411 | // int div = 1;
1412 | // for (i=0; i < len; ++i) {
1413 | // int off = (z / div) % c->lookup_values;
1414 | // float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
1415 | // output[i*step] += val;
1416 | // if (c->sequence_p) last = val;
1417 | // div *= c->lookup_values;
1418 | // }
1419 | // return TRUE;
1420 | // }
1421 | //#endif
1422 |
1423 | z *= c.dimensions;
1424 | for (i=0; i < len; ++i) {
1425 | var val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;//float_
1426 | output[output_off+ i*step] += val;
1427 | if (c.sequence_p) last = val;
1428 | }
1429 |
1430 | return true;
1431 | }
1432 |
1433 | //1774
1434 | function codebook_decode_deinterleave_repeat(f, c, outputs, ch, c_inter_p, p_inter_p, len, total_decode)
1435 | {
1436 | var c_inter = c_inter_p[0];//int_ = *
1437 | var p_inter = p_inter_p[0];//int_ = *
1438 | var i=int_,z=[int_], effective = c.dimensions;//int_
1439 |
1440 | // type 0 is only legal in a scalar context
1441 | if (c.lookup_type == 0) return error(f, VORBIS_invalid_stream);
1442 |
1443 | while (total_decode > 0) {
1444 | var last = CODEBOOK_ELEMENT_BASE(c);//float_
1445 | DECODE_VQ(z,f,c);
1446 | // #ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1447 | assert(!c.sparse || z[0] < c.sorted_entries);
1448 | // #endif
1449 | if (z[0] < 0) {
1450 | if (!f.bytes_in_seg)
1451 | if (f.last_seg) return false;
1452 | return error(f, VORBIS_invalid_stream);
1453 | }
1454 |
1455 | // if this will take us off the end of the buffers, stop short!
1456 | // we check by computing the length of the virtual interleaved
1457 | // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
1458 | // and the length we'll be using (effective)
1459 | if (c_inter + p_inter*ch + effective > len * ch) {
1460 | effective = len*ch - (p_inter*ch - c_inter);
1461 | }
1462 |
1463 | // #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1464 | // if (c->lookup_type == 1) {
1465 | // int div = 1;
1466 | // for (i=0; i < effective; ++i) {
1467 | // int off = (z / div) % c->lookup_values;
1468 | // float val = CODEBOOK_ELEMENT_FAST(c,off) + last;
1469 | // outputs[c_inter][p_inter] += val;
1470 | // if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1471 | // if (c->sequence_p) last = val;
1472 | // div *= c->lookup_values;
1473 | // }
1474 | // } else
1475 | // #endif
1476 | {
1477 | z[0] *= c.dimensions;
1478 | if (c.sequence_p) {
1479 | for (i=0; i < effective; ++i) {
1480 | var val = CODEBOOK_ELEMENT_FAST(c,z[0]+i) + last;//float_
1481 | outputs[c_inter][p_inter] += val;
1482 | if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1483 | last = val;
1484 | }
1485 | } else {
1486 | for (i=0; i < effective; ++i) {
1487 | var val = CODEBOOK_ELEMENT_FAST(c,z[0]+i) + last;//float_
1488 | outputs[c_inter][p_inter] += val;
1489 | if (++c_inter == ch) { c_inter = 0; ++p_inter; }
1490 | }
1491 | }
1492 | }
1493 |
1494 | total_decode -= effective;
1495 | }
1496 | c_inter_p[0] = c_inter;//*
1497 | p_inter_p[0] = p_inter;//*
1498 | return true;
1499 | }
1500 |
1501 | //#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
1502 | //1842
1503 | function codebook_decode_deinterleave_repeat_2(f, c, outputs, c_inter_p, p_inter_p, len, total_decode)
1504 | {
1505 | var c_inter = c_inter_p[0];//int_ = *
1506 | var p_inter = p_inter_p[0];//int_ = *
1507 | var i=int_,z=[int_], effective = c.dimensions;//int_
1508 |
1509 | // type 0 is only legal in a scalar context
1510 | if (c.lookup_type == 0) return error(f, VORBIS_invalid_stream);
1511 |
1512 | while (total_decode > 0) {
1513 | var last = CODEBOOK_ELEMENT_BASE(c);//float_
1514 | DECODE_VQ(z,f,c);
1515 |
1516 | if (z[0] < 0) {
1517 | if (!f.bytes_in_seg)
1518 | if (f.last_seg) return false;
1519 | return error(f, VORBIS_invalid_stream);
1520 | }
1521 |
1522 | // if this will take us off the end of the buffers, stop short!
1523 | // we check by computing the length of the virtual interleaved
1524 | // buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
1525 | // and the length we'll be using (effective)
1526 | if (c_inter + p_inter*2 + effective > len * 2) {
1527 | effective = len*2 - (p_inter*2 - c_inter);
1528 | }
1529 |
1530 | {
1531 | z[0] *= c.dimensions;
1532 | stb_prof(11);
1533 | if (c.sequence_p) {
1534 | // haven't optimized this case because I don't have any examples
1535 | for (i=0; i < effective; ++i) {
1536 | var val = CODEBOOK_ELEMENT_FAST(c,z[0]+i) + last;//float_
1537 | outputs[c_inter][p_inter] += val;
1538 | if (++c_inter == 2) { c_inter = 0; ++p_inter; }
1539 | last = val;
1540 | }
1541 | } else {
1542 | i=0;
1543 | if (c_inter == 1) {
1544 | var val = CODEBOOK_ELEMENT_FAST(c,z[0]+i) + last;//float_
1545 | outputs[c_inter][p_inter] += val;
1546 | c_inter = 0; ++p_inter;
1547 | ++i;
1548 | }
1549 | {
1550 | var z0 = outputs[0];//float_ *
1551 | var z1 = outputs[1];//float_ *
1552 | for (; i+1 < effective;) {
1553 | z0[p_inter] += CODEBOOK_ELEMENT_FAST(c,z[0]+i) + last;
1554 | z1[p_inter] += CODEBOOK_ELEMENT_FAST(c,z[0]+i+1) + last;
1555 | ++p_inter;
1556 | i += 2;
1557 | }
1558 | }
1559 | if (i < effective) {
1560 | var val = CODEBOOK_ELEMENT_FAST(c,z[0]+i) + last;//float_
1561 | outputs[c_inter][p_inter] += val;
1562 | if (++c_inter == 2) { c_inter = 0; ++p_inter; }
1563 | }
1564 | }
1565 | }
1566 |
1567 | total_decode -= effective;
1568 | }
1569 | c_inter_p[0] = c_inter;//*
1570 | p_inter_p[0] = p_inter;//*
1571 | return true;
1572 | }
1573 | //#endif
1574 |
1575 | //1914
1576 | function predict_point(x, x0, x1, y0, y1)
1577 | {
1578 | var dy = y1 - y0;//int_
1579 | var adx = x1 - x0;//int_
1580 | // @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?
1581 | var err = Math.abs(dy) * (x - x0);//int_
1582 | var off = parseInt(err / adx,10);//int_
1583 | return dy < 0 ? y0 - off : y0 + off;
1584 | }
1585 |
1586 | //1924
1587 | // the following table is block-copied from the specification
1588 | var inverse_db_table =//static float_
1589 | new Array(
1590 | 1.0649863e-07, 1.1341951e-07, 1.2079015e-07, 1.2863978e-07,
1591 | 1.3699951e-07, 1.4590251e-07, 1.5538408e-07, 1.6548181e-07,
1592 | 1.7623575e-07, 1.8768855e-07, 1.9988561e-07, 2.1287530e-07,
1593 | 2.2670913e-07, 2.4144197e-07, 2.5713223e-07, 2.7384213e-07,
1594 | 2.9163793e-07, 3.1059021e-07, 3.3077411e-07, 3.5226968e-07,
1595 | 3.7516214e-07, 3.9954229e-07, 4.2550680e-07, 4.5315863e-07,
1596 | 4.8260743e-07, 5.1396998e-07, 5.4737065e-07, 5.8294187e-07,
1597 | 6.2082472e-07, 6.6116941e-07, 7.0413592e-07, 7.4989464e-07,
1598 | 7.9862701e-07, 8.5052630e-07, 9.0579828e-07, 9.6466216e-07,
1599 | 1.0273513e-06, 1.0941144e-06, 1.1652161e-06, 1.2409384e-06,
1600 | 1.3215816e-06, 1.4074654e-06, 1.4989305e-06, 1.5963394e-06,
1601 | 1.7000785e-06, 1.8105592e-06, 1.9282195e-06, 2.0535261e-06,
1602 | 2.1869758e-06, 2.3290978e-06, 2.4804557e-06, 2.6416497e-06,
1603 | 2.8133190e-06, 2.9961443e-06, 3.1908506e-06, 3.3982101e-06,
1604 | 3.6190449e-06, 3.8542308e-06, 4.1047004e-06, 4.3714470e-06,
1605 | 4.6555282e-06, 4.9580707e-06, 5.2802740e-06, 5.6234160e-06,
1606 | 5.9888572e-06, 6.3780469e-06, 6.7925283e-06, 7.2339451e-06,
1607 | 7.7040476e-06, 8.2047000e-06, 8.7378876e-06, 9.3057248e-06,
1608 | 9.9104632e-06, 1.0554501e-05, 1.1240392e-05, 1.1970856e-05,
1609 | 1.2748789e-05, 1.3577278e-05, 1.4459606e-05, 1.5399272e-05,
1610 | 1.6400004e-05, 1.7465768e-05, 1.8600792e-05, 1.9809576e-05,
1611 | 2.1096914e-05, 2.2467911e-05, 2.3928002e-05, 2.5482978e-05,
1612 | 2.7139006e-05, 2.8902651e-05, 3.0780908e-05, 3.2781225e-05,
1613 | 3.4911534e-05, 3.7180282e-05, 3.9596466e-05, 4.2169667e-05,
1614 | 4.4910090e-05, 4.7828601e-05, 5.0936773e-05, 5.4246931e-05,
1615 | 5.7772202e-05, 6.1526565e-05, 6.5524908e-05, 6.9783085e-05,
1616 | 7.4317983e-05, 7.9147585e-05, 8.4291040e-05, 8.9768747e-05,
1617 | 9.5602426e-05, 0.00010181521, 0.00010843174, 0.00011547824,
1618 | 0.00012298267, 0.00013097477, 0.00013948625, 0.00014855085,
1619 | 0.00015820453, 0.00016848555, 0.00017943469, 0.00019109536,
1620 | 0.00020351382, 0.00021673929, 0.00023082423, 0.00024582449,
1621 | 0.00026179955, 0.00027881276, 0.00029693158, 0.00031622787,
1622 | 0.00033677814, 0.00035866388, 0.00038197188, 0.00040679456,
1623 | 0.00043323036, 0.00046138411, 0.00049136745, 0.00052329927,
1624 | 0.00055730621, 0.00059352311, 0.00063209358, 0.00067317058,
1625 | 0.00071691700, 0.00076350630, 0.00081312324, 0.00086596457,
1626 | 0.00092223983, 0.00098217216, 0.0010459992, 0.0011139742,
1627 | 0.0011863665, 0.0012634633, 0.0013455702, 0.0014330129,
1628 | 0.0015261382, 0.0016253153, 0.0017309374, 0.0018434235,
1629 | 0.0019632195, 0.0020908006, 0.0022266726, 0.0023713743,
1630 | 0.0025254795, 0.0026895994, 0.0028643847, 0.0030505286,
1631 | 0.0032487691, 0.0034598925, 0.0036847358, 0.0039241906,
1632 | 0.0041792066, 0.0044507950, 0.0047400328, 0.0050480668,
1633 | 0.0053761186, 0.0057254891, 0.0060975636, 0.0064938176,
1634 | 0.0069158225, 0.0073652516, 0.0078438871, 0.0083536271,
1635 | 0.0088964928, 0.009474637, 0.010090352, 0.010746080,
1636 | 0.011444421, 0.012188144, 0.012980198, 0.013823725,
1637 | 0.014722068, 0.015678791, 0.016697687, 0.017782797,
1638 | 0.018938423, 0.020169149, 0.021479854, 0.022875735,
1639 | 0.024362330, 0.025945531, 0.027631618, 0.029427276,
1640 | 0.031339626, 0.033376252, 0.035545228, 0.037855157,
1641 | 0.040315199, 0.042935108, 0.045725273, 0.048696758,
1642 | 0.051861348, 0.055231591, 0.058820850, 0.062643361,
1643 | 0.066714279, 0.071049749, 0.075666962, 0.080584227,
1644 | 0.085821044, 0.091398179, 0.097337747, 0.10366330,
1645 | 0.11039993, 0.11757434, 0.12521498, 0.13335215,
1646 | 0.14201813, 0.15124727, 0.16107617, 0.17154380,
1647 | 0.18269168, 0.19456402, 0.20720788, 0.22067342,
1648 | 0.23501402, 0.25028656, 0.26655159, 0.28387361,
1649 | 0.30232132, 0.32196786, 0.34289114, 0.36517414,
1650 | 0.38890521, 0.41417847, 0.44109412, 0.46975890,
1651 | 0.50028648, 0.53279791, 0.56742212, 0.60429640,
1652 | 0.64356699, 0.68538959, 0.72993007, 0.77736504,
1653 | 0.82788260, 0.88168307, 0.9389798, 1.0
1654 | );
1655 |
1656 |
1657 | //2013
1658 | function draw_line(output, x0, y0, x1, y1, n)
1659 | {
1660 | var dy = y1 - y0;//int_
1661 | var adx = x1 - x0;//int_
1662 | var ady = Math.abs(dy);//int_
1663 | var base=int_;
1664 | var x=x0,y=y0;//int_
1665 | var err = 0;//int_
1666 | var sy=int_;
1667 |
1668 | //#ifdef STB_VORBIS_DIVIDE_TABLE
1669 | // if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) {
1670 | // if (dy < 0) {
1671 | // base = -integer_divide_table[ady][adx];
1672 | // sy = base-1;
1673 | // } else {
1674 | // base = integer_divide_table[ady][adx];
1675 | // sy = base+1;
1676 | // }
1677 | // } else {
1678 | // base = dy / adx;
1679 | // if (dy < 0)
1680 | // sy = base - 1;
1681 | // else
1682 | // sy = base+1;
1683 | // }
1684 | //#else
1685 | base = parseInt(dy / adx,10);
1686 | if (dy < 0)
1687 | sy = base - 1;
1688 | else
1689 | sy = base+1;
1690 | //#endif
1691 | ady -= Math.abs(base) * adx;
1692 | if (x1 > n) x1 = n;
1693 | output[x] *= inverse_db_table[y];//LINE_OP(output[x], inverse_db_table[y]);
1694 | for (++x; x < x1; ++x) {
1695 | err += ady;
1696 | if (err >= adx) {
1697 | err -= adx;
1698 | y += sy;
1699 | } else
1700 | y += base;
1701 | output[x] *= inverse_db_table[y];//LINE_OP(output[x], inverse_db_table[y]);
1702 | }
1703 | }
1704 |
1705 | //2060
1706 | function residue_decode(f, book, target, offset, n, rtype)
1707 | {
1708 | var k=int_;
1709 | if (rtype == 0) {
1710 | var step = parseInt(n / book.dimensions);//int_
1711 | for (k=0; k < step; ++k)
1712 | if (!codebook_decode_step(f, book, target,+offset+k, n-offset-k, step))
1713 | return false;
1714 | } else {
1715 | for (k=0; k < n; ) {
1716 | if (!codebook_decode(f, book, target,+offset, n-k))
1717 | return false;
1718 | k += book.dimensions;
1719 | offset += book.dimensions;
1720 | }
1721 | }
1722 | return true;
1723 | }
1724 |
1725 | //2079
1726 | function decode_residue(f, residue_buffers, ch, n, rn, do_not_decode)
1727 | {
1728 | var i=int_,j=int_,pass=int_;
1729 | var r = f.residue_config[ + rn];//Residue *
1730 | var rtype = f.residue_types[rn];//int_
1731 | var c = r.classbook;//int_
1732 | var classwords = f.codebooks[c].dimensions;//int_
1733 | var n_read = r.end - r.begin;//int_
1734 | var part_read = parseInt(n_read / r.part_size,10);//int_
1735 | var temp_alloc_point = temp_alloc_save(f);//int_
1736 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1737 | var part_classdata=new Array();for(i=0;ichannels, part_read * sizeof(**part_classdata));
1738 | // #else
1739 | // int **classifications = (int **) temp_block_array(f,f->channels, part_read * sizeof(**classifications));
1740 | // #endif
1741 |
1742 | stb_prof(2);
1743 | for (i=0; i < ch; ++i)
1744 | if (!do_not_decode[i])
1745 | memset(residue_buffers[i], 0, 0, n);//sizeof(float) *
1746 |
1747 | if (rtype == 2 && ch != 1) {
1748 | var len = ch * n;//int_
1749 | for (j=0; j < ch; ++j)
1750 | if (!do_not_decode[j])
1751 | break;
1752 | if (j == ch) {
1753 | //goto done;
1754 | stb_prof(0);
1755 | temp_alloc_restore(f,temp_alloc_point);
1756 | return;
1757 | }
1758 |
1759 | stb_prof(3);
1760 | for (pass=0; pass < 8; ++pass) {
1761 | var pcount = 0, class_set = 0;//int_
1762 | if (ch == 2) {
1763 | stb_prof(13);
1764 | while (pcount < part_read) {
1765 | var z = r.begin + pcount*r.part_size;//int_
1766 | var c_inter = [(z & 1)], p_inter = [z>>>1];//int_
1767 | if (pass == 0) {
1768 | var c = f.codebooks[+r.classbook];//Codebook *
1769 | var q=[int_];
1770 | DECODE(q,f,c);q=q[0];
1771 | if (q == EOP) {//goto done;
1772 | stb_prof(0);
1773 | temp_alloc_restore(f,temp_alloc_point);
1774 | return;
1775 | }
1776 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1777 | part_classdata[0][class_set] = r.classdata[q];
1778 | // #else
1779 | // for (i=classwords-1; i >= 0; --i) {
1780 | // classifications[0][i+pcount] = q % r->classifications;
1781 | // q /= r->classifications;
1782 | // }
1783 | // #endif
1784 | }
1785 | stb_prof(5);
1786 | for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
1787 | var z = r.begin + pcount*r.part_size;//int_
1788 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1789 | var c = part_classdata[0][class_set][i];//int_
1790 | // #else
1791 | // int c = classifications[0][pcount];
1792 | // #endif
1793 | var b = r.residue_books[c][pass];//int_
1794 | if (b >= 0) {
1795 | var book = f.codebooks[ + b];//Codebook *
1796 | stb_prof(20); // accounts for X time
1797 | // #ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
1798 | // if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
1799 | // goto done;
1800 | // #else
1801 | // saves 1%
1802 | if (!codebook_decode_deinterleave_repeat_2(f, book, residue_buffers, c_inter, p_inter, n, r.part_size)) {
1803 | //goto done;
1804 | stb_prof(0);
1805 | temp_alloc_restore(f,temp_alloc_point);
1806 | return;
1807 | }
1808 | // #endif
1809 | stb_prof(7);
1810 | } else {
1811 | z += r.part_size;
1812 | c_inter[0] = z & 1;
1813 | p_inter[0] = z >>> 1;
1814 | }
1815 | }
1816 | stb_prof(8);
1817 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1818 | ++class_set;
1819 | // #endif
1820 | }
1821 | } else if (ch == 1) {
1822 | while (pcount < part_read) {
1823 | var z = r.begin + pcount*r.part_size;//int_
1824 | var c_inter = [0], p_inter = [z];//int_
1825 | if (pass == 0) {
1826 | var c = f.codebooks[+r.classbook];//Codebook *
1827 | var q=[int_];
1828 | DECODE(q,f,c);q=q[0];
1829 | if (q == EOP) { //goto done;
1830 | stb_prof(0);
1831 | temp_alloc_restore(f,temp_alloc_point);
1832 | return;
1833 | }
1834 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1835 | part_classdata[0][class_set] = r.classdata[q];
1836 | // #else
1837 | // for (i=classwords-1; i >= 0; --i) {
1838 | // classifications[0][i+pcount] = q % r->classifications;
1839 | // q /= r->classifications;
1840 | // }
1841 | // #endif
1842 | }
1843 | for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
1844 | var z = r.begin + pcount*r.part_size;//int_
1845 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1846 | var c = part_classdata[0][class_set][i];//int_
1847 | // #else
1848 | // int c = classifications[0][pcount];
1849 | // #endif
1850 | var b = r.residue_books[c][pass];//int_
1851 | if (b >= 0) {
1852 | var book = f.codebooks[ + b];//Codebook *
1853 | stb_prof(22);
1854 | if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, c_inter, p_inter, n, r.part_size)) {
1855 | //goto done;
1856 | stb_prof(0);
1857 | temp_alloc_restore(f,temp_alloc_point);
1858 | return;
1859 | }
1860 | stb_prof(3);
1861 | } else {
1862 | z += r.part_size;
1863 | c_inter[0] = 0;
1864 | p_inter[0] = z;
1865 | }
1866 | }
1867 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1868 | ++class_set;
1869 | // #endif
1870 | }
1871 | } else {
1872 | while (pcount < part_read) {
1873 | var z = r.begin + pcount*r.part_size;//int_
1874 | var c_inter = [z % ch], p_inter = [parseInt(z/ch,10)];//int_
1875 | if (pass == 0) {
1876 | var c = f.codebooks[+r.classbook];//Codebook *
1877 | var q=[int_];
1878 | DECODE(q,f,c);q=q[0];
1879 | if (q == EOP) { //goto done;
1880 | stb_prof(0);
1881 | temp_alloc_restore(f,temp_alloc_point);
1882 | return;
1883 | }
1884 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1885 | part_classdata[0][class_set] = r.classdata[q];
1886 | // #else
1887 | // for (i=classwords-1; i >= 0; --i) {
1888 | // classifications[0][i+pcount] = q % r->classifications;
1889 | // q /= r->classifications;
1890 | // }
1891 | // #endif
1892 | }
1893 | for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
1894 | var z = r.begin + pcount*r.part_size;//int_
1895 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1896 | var c = part_classdata[0][class_set][i];//int_
1897 | // #else
1898 | // int c = classifications[0][pcount];
1899 | // #endif
1900 | var b = r.residue_books[c][pass];//int_
1901 | if (b >= 0) {
1902 | var book = f.codebooks[ + b];//Codebook *
1903 | stb_prof(22);
1904 | if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, c_inter, p_inter, n, r.part_size)) {
1905 | //goto done;
1906 | stb_prof(0);
1907 | temp_alloc_restore(f,temp_alloc_point);
1908 | return;
1909 | }
1910 | stb_prof(3);
1911 | } else {
1912 | z += r.part_size;
1913 | c_inter[0] = z % ch;
1914 | p_inter[0] = parseInt(z / ch,10);
1915 | }
1916 | }
1917 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1918 | ++class_set;
1919 | // #endif
1920 | }
1921 | }
1922 | }
1923 | //goto done;
1924 | stb_prof(0);
1925 | temp_alloc_restore(f,temp_alloc_point);
1926 | return;
1927 | }
1928 | stb_prof(9);
1929 |
1930 | for (pass=0; pass < 8; ++pass) {
1931 | var pcount = 0, class_set=0;//int_
1932 | while (pcount < part_read) {
1933 | if (pass == 0) {
1934 | for (j=0; j < ch; ++j) {
1935 | if (!do_not_decode[j]) {
1936 | var c = f.codebooks[+r.classbook];//Codebook *
1937 | var temp=[int_];
1938 | DECODE(temp,f,c);temp=temp[0];
1939 | if (temp == EOP) { //goto done;
1940 | stb_prof(0);
1941 | temp_alloc_restore(f,temp_alloc_point);
1942 | return;
1943 | }
1944 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1945 | part_classdata[j][class_set] = r.classdata[temp];
1946 | // #else
1947 | // for (i=classwords-1; i >= 0; --i) {
1948 | // classifications[j][i+pcount] = temp % r->classifications;
1949 | // temp /= r->classifications;
1950 | // }
1951 | // #endif
1952 | }
1953 | }
1954 | }
1955 | for (i=0; i < classwords && pcount < part_read; ++i, ++pcount) {
1956 | for (j=0; j < ch; ++j) {
1957 | if (!do_not_decode[j]) {
1958 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1959 | var c = part_classdata[j][class_set][i];//int_
1960 | // #else
1961 | // int c = classifications[j][pcount];
1962 | // #endif
1963 | var b = r.residue_books[c][pass];//int_
1964 | if (b >= 0) {
1965 | var target = residue_buffers[j];//float_ *
1966 | var offset = r.begin + pcount * r.part_size;//int_
1967 | var n = r.part_size;//int_
1968 | var book = f.codebooks[ + b];//Codebook *
1969 | if (!residue_decode(f, book, target, offset, n, rtype)) {
1970 | //goto done;
1971 | stb_prof(0);
1972 | temp_alloc_restore(f,temp_alloc_point);
1973 | return;
1974 | }
1975 | }
1976 | }
1977 | }
1978 | }
1979 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
1980 | ++class_set;
1981 | // #endif
1982 | }
1983 | }
1984 | // done:
1985 | stb_prof(0);
1986 | temp_alloc_restore(f,temp_alloc_point);
1987 | }
1988 |
1989 |
1990 | // the following were split out into separate functions while optimizing;
1991 | // they could be pushed back up but eh. __forceinline showed no change;
1992 | // they're probably already being inlined.
1993 | //2427
1994 | function imdct_step3_iter0_loop(n, e, i_off, k_off, A)
1995 | {
1996 | var ee0 = e; var ee0_off = + i_off;//float_ *
1997 | var ee2 = ee0; var ee2_off = ee0_off + k_off;//float_ *
1998 | var i=int_;var A_off=0;
1999 |
2000 | assert((n & 3) == 0);
2001 | for (i=(n>>>2); i > 0; --i) {
2002 | var k00_20=float_, k01_21=float_;
2003 | k00_20 = ee0[ee0_off+ 0] - ee2[ee2_off+ 0];
2004 | k01_21 = ee0[ee0_off -1] - ee2[ee2_off -1];
2005 | ee0[ee0_off+ 0] += ee2[ee2_off+ 0];//ee0[ 0] = ee0[ 0] + ee2[ 0];
2006 | ee0[ee0_off -1] += ee2[ee2_off -1];//ee0[-1] = ee0[-1] + ee2[-1];
2007 | ee2[ee2_off+ 0] = k00_20 * A[A_off+ 0] - k01_21 * A[A_off+ 1];
2008 | ee2[ee2_off -1] = k01_21 * A[A_off+ 0] + k00_20 * A[A_off+ 1];
2009 | A_off += 8;
2010 |
2011 | k00_20 = ee0[ee0_off -2] - ee2[ee2_off -2];
2012 | k01_21 = ee0[ee0_off -3] - ee2[ee2_off -3];
2013 | ee0[ee0_off -2] += ee2[ee2_off -2];//ee0[-2] = ee0[-2] + ee2[-2];
2014 | ee0[ee0_off -3] += ee2[ee2_off -3];//ee0[-3] = ee0[-3] + ee2[-3];
2015 | ee2[ee2_off -2] = k00_20 * A[A_off+ 0] - k01_21 * A[A_off+ 1];
2016 | ee2[ee2_off -3] = k01_21 * A[A_off+ 0] + k00_20 * A[A_off+ 1];
2017 | A_off += 8;
2018 |
2019 | k00_20 = ee0[ee0_off -4] - ee2[ee2_off -4];
2020 | k01_21 = ee0[ee0_off -5] - ee2[ee2_off -5];
2021 | ee0[ee0_off -4] += ee2[ee2_off -4];//ee0[-4] = ee0[-4] + ee2[-4];
2022 | ee0[ee0_off -5] += ee2[ee2_off -5];//ee0[-5] = ee0[-5] + ee2[-5];
2023 | ee2[ee2_off -4] = k00_20 * A[A_off+ 0] - k01_21 * A[A_off+ 1];
2024 | ee2[ee2_off -5] = k01_21 * A[A_off+ 0] + k00_20 * A[A_off+ 1];
2025 | A_off += 8;
2026 |
2027 | k00_20 = ee0[ee0_off -6] - ee2[ee2_off -6];
2028 | k01_21 = ee0[ee0_off -7] - ee2[ee2_off -7];
2029 | ee0[ee0_off -6] += ee2[ee2_off -6];//ee0[-6] = ee0[-6] + ee2[-6];
2030 | ee0[ee0_off -7] += ee2[ee2_off -7];//ee0[-7] = ee0[-7] + ee2[-7];
2031 | ee2[ee2_off -6] = k00_20 * A[A_off+ 0] - k01_21 * A[A_off+ 1];
2032 | ee2[ee2_off -7] = k01_21 * A[A_off+ 0] + k00_20 * A[A_off+ 1];
2033 | A_off += 8;
2034 | ee0_off -= 8;
2035 | ee2_off -= 8;
2036 | }
2037 | }
2038 |
2039 | //2472
2040 | function imdct_step3_inner_r_loop(lim, e, d0, k_off, A, k1)
2041 | {
2042 | var i=int_;var A_off=0;
2043 | var k00_20=float_, k01_21=float_;
2044 |
2045 | var e0 = e; var e0_off = + d0;//float_ *
2046 | var e2 = e0; var e2_off = e0_off + k_off;//float_ *
2047 |
2048 | for (i=lim >>> 2; i > 0; --i) {
2049 | k00_20 = e0[e0_off -0] - e2[e2_off -0];
2050 | k01_21 = e0[e0_off -1] - e2[e2_off -1];
2051 | e0[e0_off -0] += e2[e2_off -0];//e0[-0] = e0[-0] + e2[-0];
2052 | e0[e0_off -1] += e2[e2_off -1];//e0[-1] = e0[-1] + e2[-1];
2053 | e2[e2_off -0] = (k00_20)*A[A_off+ 0] - (k01_21) * A[A_off+ 1];
2054 | e2[e2_off -1] = (k01_21)*A[A_off+ 0] + (k00_20) * A[A_off+ 1];
2055 |
2056 | A_off += k1;
2057 |
2058 | k00_20 = e0[e0_off -2] - e2[e2_off -2];
2059 | k01_21 = e0[e0_off -3] - e2[e2_off -3];
2060 | e0[e0_off -2] += e2[e2_off -2];//e0[-2] = e0[-2] + e2[-2];
2061 | e0[e0_off -3] += e2[e2_off -3];//e0[-3] = e0[-3] + e2[-3];
2062 | e2[e2_off -2] = (k00_20)*A[A_off+ 0] - (k01_21) * A[A_off+ 1];
2063 | e2[e2_off -3] = (k01_21)*A[A_off+ 0] + (k00_20) * A[A_off+ 1];
2064 |
2065 | A_off += k1;
2066 |
2067 | k00_20 = e0[e0_off -4] - e2[e2_off -4];
2068 | k01_21 = e0[e0_off -5] - e2[e2_off -5];
2069 | e0[e0_off -4] += e2[e2_off -4];//e0[-4] = e0[-4] + e2[-4];
2070 | e0[e0_off -5] += e2[e2_off -5];//e0[-5] = e0[-5] + e2[-5];
2071 | e2[e2_off -4] = (k00_20)*A[A_off+ 0] - (k01_21) * A[A_off+ 1];
2072 | e2[e2_off -5] = (k01_21)*A[A_off+ 0] + (k00_20) * A[A_off+ 1];
2073 |
2074 | A_off += k1;
2075 |
2076 | k00_20 = e0[e0_off -6] - e2[e2_off -6];
2077 | k01_21 = e0[e0_off -7] - e2[e2_off -7];
2078 | e0[e0_off -6] += e2[e2_off -6];//e0[-6] = e0[-6] + e2[-6];
2079 | e0[e0_off -7] += e2[e2_off -7];//e0[-7] = e0[-7] + e2[-7];
2080 | e2[e2_off -6] = (k00_20)*A[A_off+ 0] - (k01_21) * A[A_off+ 1];
2081 | e2[e2_off -7] = (k01_21)*A[A_off+ 0] + (k00_20) * A[A_off+ 1];
2082 |
2083 | e0_off -= 8;
2084 | e2_off -= 8;
2085 |
2086 | A_off += k1;
2087 | }
2088 | }
2089 |
2090 | //2522
2091 | function imdct_step3_inner_s_loop(n, e, i_off, k_off, A, A_off, a_off, k0)
2092 | {
2093 | var i=int_;
2094 | var A0 = A[A_off+ 0];//float_
2095 | var A1 = A[A_off+ 0+1];//float_
2096 | var A2 = A[A_off+ 0+a_off];//float_
2097 | var A3 = A[A_off+ 0+a_off+1];//float_
2098 | var A4 = A[A_off+ 0+a_off*2+0];//float_
2099 | var A5 = A[A_off+ 0+a_off*2+1];//float_
2100 | var A6 = A[A_off+ 0+a_off*3+0];//float_
2101 | var A7 = A[A_off+ 0+a_off*3+1];//float_
2102 |
2103 | var k00=float_,k11=float_;
2104 |
2105 | var ee0 = e; var ee0_off = +i_off;//float_ *
2106 | var ee2 = ee0;var ee2_off = ee0_off+k_off;//float_ *
2107 |
2108 | for (i=n; i > 0; --i) {
2109 | k00 = ee0[ee0_off+ 0] - ee2[ee2_off+ 0];
2110 | k11 = ee0[ee0_off -1] - ee2[ee2_off -1];
2111 | ee0[ee0_off+ 0] = ee0[ee0_off+ 0] + ee2[ee2_off+ 0];
2112 | ee0[ee0_off -1] = ee0[ee0_off -1] + ee2[ee2_off -1];
2113 | ee2[ee2_off+ 0] = (k00) * A0 - (k11) * A1;
2114 | ee2[ee2_off -1] = (k11) * A0 + (k00) * A1;
2115 |
2116 | k00 = ee0[ee0_off -2] - ee2[ee2_off -2];
2117 | k11 = ee0[ee0_off -3] - ee2[ee2_off -3];
2118 | ee0[ee0_off -2] = ee0[ee0_off -2] + ee2[ee2_off -2];
2119 | ee0[ee0_off -3] = ee0[ee0_off -3] + ee2[ee2_off -3];
2120 | ee2[ee2_off -2] = (k00) * A2 - (k11) * A3;
2121 | ee2[ee2_off -3] = (k11) * A2 + (k00) * A3;
2122 |
2123 | k00 = ee0[ee0_off -4] - ee2[ee2_off -4];
2124 | k11 = ee0[ee0_off -5] - ee2[ee2_off -5];
2125 | ee0[ee0_off -4] = ee0[ee0_off -4] + ee2[ee2_off -4];
2126 | ee0[ee0_off -5] = ee0[ee0_off -5] + ee2[ee2_off -5];
2127 | ee2[ee2_off -4] = (k00) * A4 - (k11) * A5;
2128 | ee2[ee2_off -5] = (k11) * A4 + (k00) * A5;
2129 |
2130 | k00 = ee0[ee0_off -6] - ee2[ee2_off -6];
2131 | k11 = ee0[ee0_off -7] - ee2[ee2_off -7];
2132 | ee0[ee0_off -6] = ee0[ee0_off -6] + ee2[ee2_off -6];
2133 | ee0[ee0_off -7] = ee0[ee0_off -7] + ee2[ee2_off -7];
2134 | ee2[ee2_off -6] = (k00) * A6 - (k11) * A7;
2135 | ee2[ee2_off -7] = (k11) * A6 + (k00) * A7;
2136 |
2137 | ee0_off -= k0;
2138 | ee2_off -= k0;
2139 | }
2140 | }
2141 |
2142 | //2573
2143 | function iter_54(z,z_off)
2144 | {
2145 | var k00=float_,k11=float_,k22=float_,k33=float_;
2146 | var y0=float_,y1=float_,y2=float_,y3=float_;
2147 |
2148 | k00 = z[z_off+ 0] - z[z_off -4];
2149 | y0 = z[z_off+ 0] + z[z_off -4];
2150 | y2 = z[z_off -2] + z[z_off -6];
2151 | k22 = z[z_off -2] - z[z_off -6];
2152 |
2153 | z[z_off -0] = y0 + y2; // z0 + z4 + z2 + z6
2154 | z[z_off -2] = y0 - y2; // z0 + z4 - z2 - z6
2155 |
2156 | // done with y0,y2
2157 |
2158 | k33 = z[z_off -3] - z[z_off -7];
2159 |
2160 | z[z_off -4] = k00 + k33; // z0 - z4 + z3 - z7
2161 | z[z_off -6] = k00 - k33; // z0 - z4 - z3 + z7
2162 |
2163 | // done with k33
2164 |
2165 | k11 = z[z_off -1] - z[z_off -5];
2166 | y1 = z[z_off -1] + z[z_off -5];
2167 | y3 = z[z_off -3] + z[z_off -7];
2168 |
2169 | z[z_off -1] = y1 + y3; // z1 + z5 + z3 + z7
2170 | z[z_off -3] = y1 - y3; // z1 + z5 - z3 - z7
2171 | z[z_off -5] = k11 - k22; // z1 - z5 + z2 - z6
2172 | z[z_off -7] = k11 + k22; // z1 - z5 - z2 + z6
2173 | }
2174 |
2175 | //2605
2176 | function imdct_step3_inner_s_loop_ld654(n, e, i_off, A, base_n)
2177 | {
2178 | var k_off = -8;//int_
2179 | var a_off = base_n >>> 3;//int_
2180 | var A2 = A[0+a_off];//float_
2181 | var z = e; var z_off = + i_off;//float_ *
2182 | var base = z; var base_off = z_off - 16 * n;//float_ *
2183 |
2184 | while (z_off > base_off) {
2185 | var k00=float_,k11=float_;
2186 |
2187 | k00 = z[z_off -0] - z[z_off -8];
2188 | k11 = z[z_off -1] - z[z_off -9];
2189 | z[z_off -0] = z[z_off -0] + z[z_off -8];
2190 | z[z_off -1] = z[z_off -1] + z[z_off -9];
2191 | z[z_off -8] = k00;
2192 | z[z_off -9] = k11 ;
2193 |
2194 | k00 = z[z_off -2] - z[z_off -10];
2195 | k11 = z[z_off -3] - z[z_off -11];
2196 | z[z_off -2] = z[z_off -2] + z[z_off -10];
2197 | z[z_off -3] = z[z_off -3] + z[z_off -11];
2198 | z[z_off -10] = (k00+k11) * A2;
2199 | z[z_off -11] = (k11-k00) * A2;
2200 |
2201 | k00 = z[z_off -12] - z[z_off -4]; // reverse to avoid a unary negation
2202 | k11 = z[z_off -5] - z[z_off -13];
2203 | z[z_off -4] = z[z_off -4] + z[z_off -12];
2204 | z[z_off -5] = z[z_off -5] + z[z_off -13];
2205 | z[z_off -12] = k11;
2206 | z[z_off -13] = k00;
2207 |
2208 | k00 = z[z_off -14] - z[z_off -6]; // reverse to avoid a unary negation
2209 | k11 = z[z_off -7] - z[z_off -15];
2210 | z[z_off -6] = z[z_off -6] + z[z_off -14];
2211 | z[z_off -7] = z[z_off -7] + z[z_off -15];
2212 | z[z_off -14] = (k00+k11) * A2;
2213 | z[z_off -15] = (k00-k11) * A2;
2214 |
2215 | iter_54(z,z_off);
2216 | iter_54(z,z_off-8);
2217 | z_off -= 16;
2218 | }
2219 | }
2220 |
2221 | //2650
2222 | function inverse_mdct(buffer, n, f, blocktype)
2223 | {
2224 | var n2 = n >>> 1, n4 = n >>> 2, n8 = n >>> 3, l=int_;//int_
2225 | var n3_4 = n - n4, ld=int_;//int_
2226 | // @OPTIMIZE: reduce register pressure by using fewer variables?
2227 | var save_point = temp_alloc_save(f);//int_
2228 | var buf2 = new Array(n2);var buf2_off = 0;//(float *) temp_alloc(f, n2 * sizeof(*buf2));//float_ *
2229 | var u=null,v=null;//float_ //*,*
2230 | var u_off=0,v_off=0;//float_ //*,*
2231 | // twiddle factors
2232 | var A = f.A[blocktype];//float_ *
2233 | var A_off = 0;//float_ *
2234 |
2235 | // IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
2236 | // See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function.
2237 |
2238 | // kernel from paper
2239 |
2240 |
2241 | // merged:
2242 | // copy and reflect spectral data
2243 | // step 0
2244 |
2245 | // note that it turns out that the items added together during
2246 | // this step are, in fact, being added to themselves (as reflected
2247 | // by step 0). inexplicable inefficiency! this became obvious
2248 | // once I combined the passes.
2249 |
2250 | // so there's a missing 'times 2' here (for adding X to itself).
2251 | // this propogates through linearly to the end, where the numbers
2252 | // are 1/2 too small, and need to be compensated for.
2253 |
2254 | {
2255 | var d=float_,e=float_, AA=float_, e_stop=float_;//*d,*e, *AA, *e_stop
2256 | var d_off=0,e_off=0, AA_off=0, e_stop_off=0;//*d,*e, *AA, *e_stop
2257 | d = buf2;d_off = (n2-2);//&
2258 | AA = A;AA_off = A_off;
2259 | e = buffer;e_off=(0);//&
2260 | e_stop = buffer;e_stop_off = (n2);//&
2261 | while (e_off != e_stop_off) {
2262 | d[d_off+ 1] = (e[e_off+ 0] * AA[AA_off+ 0] - e[e_off+ 2]*AA[AA_off+ 1]);
2263 | d[d_off+ 0] = (e[e_off+ 0] * AA[AA_off+ 1] + e[e_off+ 2]*AA[AA_off+ 0]);
2264 | d_off -= 2;
2265 | AA_off += 2;
2266 | e_off += 4;
2267 | }
2268 |
2269 | e = buffer;e_off=(n2-3);//&
2270 | while (d_off >= buf2_off) {
2271 | d[d_off+ 1] = (-e[e_off+ 2] * AA[AA_off+ 0] - -e[e_off+ 0]*AA[AA_off+ 1]);
2272 | d[d_off+ 0] = (-e[e_off+ 2] * AA[AA_off+ 1] + -e[e_off+ 0]*AA[AA_off+ 0]);
2273 | d_off -= 2;
2274 | AA_off += 2;
2275 | e_off -= 4;
2276 | }
2277 | }
2278 |
2279 | // now we use symbolic names for these, so that we can
2280 | // possibly swap their meaning as we change which operations
2281 | // are in place
2282 |
2283 | u = buffer;u_off = 0;
2284 | v = buf2;v_off = buf2_off;
2285 |
2286 | // step 2 (paper output is w, now u)
2287 | // this could be in place, but the data ends up in the wrong
2288 | // place... _somebody_'s got to swap it, so this is nominated
2289 | {
2290 | var AA = A;var AA_off = (n2-8);//float_ * = &
2291 | var d0=float_,d1=float_, e0=float_, e1=float_;//*d0,*d1, *e0, *e1
2292 | var d0_off=0,d1_off=0, e0_off=0, e1_off=0;//*d0,*d1, *e0, *e1
2293 |
2294 | e0 = v;e0_off = (n4);//&
2295 | e1 = v;e1_off = (0);//&
2296 |
2297 | d0 = u;d0_off = (n4);//&
2298 | d1 = u;d1_off = (0);//&
2299 |
2300 | while (AA_off >= A_off) {
2301 | var v40_20=float_, v41_21=float_;
2302 |
2303 | v41_21 = e0[e0_off+ 1] - e1[e1_off+ 1];
2304 | v40_20 = e0[e0_off+ 0] - e1[e1_off+ 0];
2305 | d0[d0_off+ 1] = e0[e0_off+ 1] + e1[e1_off+ 1];
2306 | d0[d0_off+ 0] = e0[e0_off+ 0] + e1[e1_off+ 0];
2307 | d1[d1_off+ 1] = v41_21*AA[AA_off+ 4] - v40_20*AA[AA_off+ 5];
2308 | d1[d1_off+ 0] = v40_20*AA[AA_off+ 4] + v41_21*AA[AA_off+ 5];
2309 |
2310 | v41_21 = e0[e0_off+ 3] - e1[e1_off+ 3];
2311 | v40_20 = e0[e0_off+ 2] - e1[e1_off+ 2];
2312 | d0[d0_off+ 3] = e0[e0_off+ 3] + e1[e1_off+ 3];
2313 | d0[d0_off+ 2] = e0[e0_off+ 2] + e1[e1_off+ 2];
2314 | d1[d1_off+ 3] = v41_21*AA[AA_off+ 0] - v40_20*AA[AA_off+ 1];
2315 | d1[d1_off+ 2] = v40_20*AA[AA_off+ 0] + v41_21*AA[AA_off+ 1];
2316 |
2317 | AA_off -= 8;
2318 |
2319 | d0_off += 4;
2320 | d1_off += 4;
2321 | e0_off += 4;
2322 | e1_off += 4;
2323 | }
2324 | }
2325 |
2326 | // step 3
2327 | ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
2328 |
2329 | // optimized step 3:
2330 |
2331 | // the original step3 loop can be nested r inside s or s inside r;
2332 | // it's written originally as s inside r, but this is dumb when r
2333 | // iterates many times, and s few. So I have two copies of it and
2334 | // switch between them halfway.
2335 |
2336 | // this is iteration 0 of step 3
2337 | imdct_step3_iter0_loop(n >>> 4, u, n2-1-n4*0, -(n >>> 3), A);
2338 | imdct_step3_iter0_loop(n >>> 4, u, n2-1-n4*1, -(n >>> 3), A);
2339 |
2340 | // this is iteration 1 of step 3
2341 | imdct_step3_inner_r_loop(n >>> 5, u, n2-1 - n8*0, -(n >>> 4), A, 16);
2342 | imdct_step3_inner_r_loop(n >>> 5, u, n2-1 - n8*1, -(n >>> 4), A, 16);
2343 | imdct_step3_inner_r_loop(n >>> 5, u, n2-1 - n8*2, -(n >>> 4), A, 16);
2344 | imdct_step3_inner_r_loop(n >>> 5, u, n2-1 - n8*3, -(n >>> 4), A, 16);
2345 |
2346 | l=2;
2347 | for (; l < (ld-3)>>>1; ++l) {
2348 | var k0 = n >>> (l+2), k0_2 = k0>>>1;//int_
2349 | var lim = (1 << (l+1))>>>0;//int_
2350 | var i=int_;
2351 | for (i=0; i < lim; ++i)
2352 | imdct_step3_inner_r_loop(n >>> (l+4), u, n2-1 - k0*i, -k0_2, A, (1 << (l+3))>>>0);
2353 | }
2354 |
2355 | for (; l < ld-6; ++l) {
2356 | var k0 = n >>> (l+2), k1 = (1 << (l+3))>>>0, k0_2 = k0>>>1;//int_
2357 | var rlim = n >>> (l+6), r=int_;//int_
2358 | var lim = (1 << (l+1))>>>0;//int_
2359 | var i_off=int_;
2360 | var A0 = A;var A0_off = A_off;//float_ *
2361 | i_off = n2-1;
2362 | for (r=rlim; r > 0; --r) {
2363 | imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, A0_off, k1, k0);
2364 | A0_off += k1*4;
2365 | i_off -= 8;
2366 | }
2367 | }
2368 |
2369 | // iterations with count:
2370 | // ld-6,-5,-4 all interleaved together
2371 | // the big win comes from getting rid of needless flops
2372 | // due to the constants on pass 5 & 4 being all 1 and 0;
2373 | // combining them to be simultaneous to improve cache made little difference
2374 | imdct_step3_inner_s_loop_ld654(n >>> 5, u, n2-1, A, n);
2375 |
2376 | // output is u
2377 |
2378 | // step 4, 5, and 6
2379 | // cannot be in-place because of step 5
2380 | {
2381 | var bitrev = f.bit_reverse[blocktype];var bitrev_off = 0;//uint16 *
2382 | // weirdly, I'd have thought reading sequentially and writing
2383 | // erratically would have been better than vice-versa, but in
2384 | // fact that's not what my testing showed. (That is, with
2385 | // j = bitreverse(i), do you read i and write j, or read j and write i.)
2386 |
2387 | var d0 = v;var d0_off = (n4-4);//float_ * = &
2388 | var d1 = v;var d1_off = (n2-4);//float_ * = &
2389 | while (d0_off >= v_off) {
2390 | var k4=int_;
2391 |
2392 | k4 = bitrev[bitrev_off+ 0];
2393 | d1[d1_off+ 3] = u[k4+0];
2394 | d1[d1_off+ 2] = u[k4+1];
2395 | d0[d0_off+ 3] = u[k4+2];
2396 | d0[d0_off+ 2] = u[k4+3];
2397 |
2398 | k4 = bitrev[bitrev_off+ 1];
2399 | d1[d1_off+ 1] = u[k4+0];
2400 | d1[d1_off+ 0] = u[k4+1];
2401 | d0[d0_off+ 1] = u[k4+2];
2402 | d0[d0_off+ 0] = u[k4+3];
2403 |
2404 | d0_off -= 4;
2405 | d1_off -= 4;
2406 | bitrev_off += 2;
2407 | }
2408 | }
2409 | // (paper output is u, now v)
2410 |
2411 |
2412 | // data must be in buf2
2413 | assert(v_off == buf2_off);
2414 |
2415 | // step 7 (paper output is v, now v)
2416 | // this is now in place
2417 | {
2418 | var C = f.C[blocktype];var C_off = 0;//float *
2419 | var d=float_, e=float_;//*d, *e
2420 | var d_off=0, e_off=0;//*d, *e
2421 |
2422 | d = v;d_off = v_off;
2423 | e = v;e_off = v_off + n2 - 4;
2424 |
2425 | while (d_off < e_off) {
2426 | var a02=float_,a11=float_,b0=float_,b1=float_,b2=float_,b3=float_;
2427 |
2428 | a02 = d[d_off+ 0] - e[e_off+ 2];
2429 | a11 = d[d_off+ 1] + e[e_off+ 3];
2430 |
2431 | b0 = C[C_off+ 1]*a02 + C[C_off+ 0]*a11;
2432 | b1 = C[C_off+ 1]*a11 - C[C_off+ 0]*a02;
2433 |
2434 | b2 = d[d_off+ 0] + e[e_off+ 2];
2435 | b3 = d[d_off+ 1] - e[e_off+ 3];
2436 |
2437 | d[d_off+ 0] = b2 + b0;
2438 | d[d_off+ 1] = b3 + b1;
2439 | e[e_off+ 2] = b2 - b0;
2440 | e[e_off+ 3] = b1 - b3;
2441 |
2442 | a02 = d[d_off+ 2] - e[e_off+ 0];
2443 | a11 = d[d_off+ 3] + e[e_off+ 1];
2444 |
2445 | b0 = C[C_off+ 3]*a02 + C[C_off+ 2]*a11;
2446 | b1 = C[C_off+ 3]*a11 - C[C_off+ 2]*a02;
2447 |
2448 | b2 = d[d_off+ 2] + e[e_off+ 0];
2449 | b3 = d[d_off+ 3] - e[e_off+ 1];
2450 |
2451 | d[d_off+ 2] = b2 + b0;
2452 | d[d_off+ 3] = b3 + b1;
2453 | e[e_off+ 0] = b2 - b0;
2454 | e[e_off+ 1] = b1 - b3;
2455 |
2456 | C_off += 4;
2457 | d_off += 4;
2458 | e_off -= 4;
2459 | }
2460 | }
2461 |
2462 | // data must be in buf2
2463 |
2464 |
2465 | // step 8+decode (paper output is X, now buffer)
2466 | // this generates pairs of data a la 8 and pushes them directly through
2467 | // the decode kernel (pushing rather than pulling) to avoid having
2468 | // to make another pass later
2469 |
2470 | // this cannot POSSIBLY be in place, so we refer to the buffers directly
2471 |
2472 | {
2473 | var d0=float_,d1=float_,d2=float_,d3=float_;//*d0,*d1,*d2,*d3
2474 | var d0_off=0,d1_off=0,d2_off=0,d3_off=0;//*d0,*d1,*d2,*d3
2475 |
2476 | var B = f.B[blocktype];var B_off = + n2 - 8;//float *
2477 | var e = buf2;var e_off = buf2_off + n2 - 8;//float *
2478 | d0 = buffer;d0_off = (0);//&
2479 | d1 = buffer;d1_off = (n2-4);//&
2480 | d2 = buffer;d2_off = (n2);//&
2481 | d3 = buffer;d3_off = (n-4);//&
2482 | while (e_off >= v_off) {
2483 | var p0=float_,p1=float_,p2=float_,p3=float_;
2484 |
2485 | p3 = e[e_off+ 6]*B[B_off+ 7] - e[e_off+ 7]*B[B_off+ 6];
2486 | p2 = -e[e_off+ 6]*B[B_off+ 6] - e[e_off+ 7]*B[B_off+ 7];
2487 |
2488 | d0[d0_off+ 0] = p3;
2489 | d1[d1_off+ 3] = - p3;
2490 | d2[d2_off+ 0] = p2;
2491 | d3[d3_off+ 3] = p2;
2492 |
2493 | p1 = e[e_off+ 4]*B[B_off+ 5] - e[e_off+ 5]*B[B_off+ 4];
2494 | p0 = -e[e_off+ 4]*B[B_off+ 4] - e[e_off+ 5]*B[B_off+ 5];
2495 |
2496 | d0[d0_off+ 1] = p1;
2497 | d1[d1_off+ 2] = - p1;
2498 | d2[d2_off+ 1] = p0;
2499 | d3[d3_off+ 2] = p0;
2500 |
2501 | p3 = e[e_off+ 2]*B[B_off+ 3] - e[e_off+ 3]*B[B_off+ 2];
2502 | p2 = -e[e_off+ 2]*B[B_off+ 2] - e[e_off+ 3]*B[B_off+ 3];
2503 |
2504 | d0[d0_off+ 2] = p3;
2505 | d1[d1_off+ 1] = - p3;
2506 | d2[d2_off+ 2] = p2;
2507 | d3[d3_off+ 1] = p2;
2508 |
2509 | p1 = e[e_off+ 0]*B[B_off+ 1] - e[e_off+ 1]*B[B_off+ 0];
2510 | p0 = -e[e_off+ 0]*B[B_off+ 0] - e[e_off+ 1]*B[B_off+ 1];
2511 |
2512 | d0[d0_off+ 3] = p1;
2513 | d1[d1_off+ 0] = - p1;
2514 | d2[d2_off+ 3] = p0;
2515 | d3[d3_off+ 0] = p0;
2516 |
2517 | B_off -= 8;
2518 | e_off -= 8;
2519 | d0_off += 4;
2520 | d2_off += 4;
2521 | d1_off -= 4;
2522 | d3_off -= 4;
2523 | }
2524 | }
2525 |
2526 | temp_alloc_restore(f,save_point);
2527 | }
2528 |
2529 | //3079
2530 | function get_window(f, len)
2531 | {
2532 | len <<= 1;
2533 | if (len == f.blocksize_0) return f.window_[0];
2534 | if (len == f.blocksize_1) return f.window_[1];
2535 | assert(0);
2536 | return null;
2537 | }
2538 |
2539 | //3093
2540 | function do_floor(f, map, i, n, target, finalY, step2_flag)
2541 | {
2542 | var n2 = n >>> 1;//int_
2543 | var s = map.chan[i].mux, floor1=int_;//int_
2544 | floor1 = map.submap_floor[s];
2545 | if (f.floor_types[floor1] == 0) {
2546 | return error(f, VORBIS_invalid_stream);
2547 | } else {
2548 | var g = f.floor_config[floor1].floor1;//Floor1 * = &
2549 | var j=int_,q=int_;
2550 | var lx = 0, ly = finalY[0] * g.floor1_multiplier;//int_
2551 | for (q=1; q < g.values; ++q) {
2552 | j = g.sorted_order[q];
2553 | // #ifndef STB_VORBIS_NO_DEFER_FLOOR
2554 | if (finalY[j] >= 0)
2555 | // #else
2556 | // if (step2_flag[j])
2557 | // #endif
2558 | {
2559 | var hy = finalY[j] * g.floor1_multiplier;//int_
2560 | var hx = g.Xlist[j];//int_
2561 | draw_line(target, lx,ly, hx,hy, n2);
2562 | lx = hx, ly = hy;
2563 | }
2564 | }
2565 | if (lx < n2)
2566 | // optimization of: draw_line(target, lx,ly, n,ly, n2);
2567 | for (j=lx; j < n2; ++j)
2568 | target[j] *= inverse_db_table[ly];//LINE_OP(target[j], inverse_db_table[ly]);
2569 | }
2570 | return true;
2571 | }
2572 |
2573 | //3126
2574 | function vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode)
2575 | {
2576 | var m=new Mode();//*
2577 | var i=int_, n=int_, prev=int_, next=int_, window_center=int_;
2578 | f.channel_buffer_start = f.channel_buffer_end = 0;
2579 |
2580 | var goto_retry=true;//retry:
2581 | while(goto_retry) {
2582 | goto_retry=false;
2583 | if (f.eof) return false;
2584 | if (!maybe_start_packet(f))
2585 | return false;
2586 | // check packet type
2587 | if (get_bits(f,1) != 0) {
2588 | if (IS_PUSH_MODE(f))
2589 | return error(f,VORBIS_bad_packet_type);
2590 | while (EOP != get8_packet(f));
2591 | goto_retry=true;
2592 | }
2593 | }
2594 |
2595 | if (f.alloc.alloc_buffer)
2596 | assert(f.alloc.alloc_buffer_length_in_bytes == f.temp_offset);
2597 |
2598 | i = get_bits(f, ilog(f.mode_count-1));
2599 | if (i == EOP) return false;
2600 | if (i >= f.mode_count) return false;
2601 | mode[0] = i;//*
2602 | m = f.mode_config[ + i];
2603 | if (m.blockflag) {
2604 | n = f.blocksize_1;
2605 | prev = get_bits(f,1);
2606 | next = get_bits(f,1);
2607 | } else {
2608 | prev = next = 0;
2609 | n = f.blocksize_0;
2610 | }
2611 |
2612 | // WINDOWING
2613 |
2614 | window_center = n >>> 1;
2615 | if (m.blockflag && !prev) {
2616 | p_left_start[0] = (n - f.blocksize_0) >>> 2;//*
2617 | p_left_end[0] = (n + f.blocksize_0) >>> 2;//*
2618 | } else {
2619 | p_left_start[0] = 0;//*
2620 | p_left_end[0] = window_center;//*
2621 | }
2622 | if (m.blockflag && !next) {
2623 | p_right_start[0] = (n*3 - f.blocksize_0) >>> 2;//*
2624 | p_right_end[0] = (n*3 + f.blocksize_0) >>> 2;//*
2625 | } else {
2626 | p_right_start[0] = window_center;//*
2627 | p_right_end[0] = n;//*
2628 | }
2629 | return true;
2630 | }
2631 |
2632 | //3181
2633 | function vorbis_decode_packet_rest(f, len, m, left_start, left_end, right_start, right_end, p_left)
2634 | {
2635 | var map=new Mapping();//*
2636 | var i=int_,j=int_,k=int_,n=int_,n2=int_;
2637 | var zero_channel=new Array(256);//int_
2638 | var really_zero_channel=new Array(256);//int_
2639 | var window_center=int_;
2640 |
2641 | // WINDOWING
2642 |
2643 | n = f.blocksize[m.blockflag];
2644 | window_center = n >>> 1;
2645 |
2646 | map = f.mapping[m.mapping];//&
2647 |
2648 | // FLOORS
2649 | n2 = n >>> 1;
2650 |
2651 | stb_prof(1);
2652 | goto_channels:for (i=0; i < f.channels; ++i) {
2653 | var s = map.chan[i].mux, floor1=int_;//int_
2654 | zero_channel[i] = false;
2655 | floor1 = map.submap_floor[s];
2656 | if (f.floor_types[floor1] == 0) {
2657 | return error(f, VORBIS_invalid_stream);
2658 | } else {
2659 | var g = f.floor_config[floor1].floor1;//Floor1 * = &
2660 | if (get_bits(f, 1)) {
2661 | var finalY=short_;//*
2662 | var step2_flag=new Array(256);//uint8
2663 | var range_list = new Array( 256, 128, 86, 64 );//static int_
2664 | var range = range_list[g.floor1_multiplier-1];//int_
2665 | var offset = 2;//int_
2666 | finalY = f.finalY[i];
2667 | finalY[0] = get_bits(f, ilog(range)-1);
2668 | finalY[1] = get_bits(f, ilog(range)-1);
2669 | for (j=0; j < g.partitions; ++j) {
2670 | var pclass = g.partition_class_list[j];//int_
2671 | var cdim = g.class_dimensions[pclass];//int_
2672 | var cbits = g.class_subclasses[pclass];//int_
2673 | var csub = ((1 << cbits)>>>0)-1;//int_
2674 | var cval = [0];//int_
2675 | if (cbits) {
2676 | var c = f.codebooks[ + g.class_masterbooks[pclass]];//Codebook *
2677 | DECODE(cval,f,c);cval=cval[0];
2678 | }
2679 | for (k=0; k < cdim; ++k) {
2680 | var book = g.subclass_books[pclass][cval & csub];//int_
2681 | cval = cval >>> cbits;
2682 | if (book >= 0) {
2683 | var temp=[int_];
2684 | var c = f.codebooks[ + book];//Codebook *
2685 | DECODE(temp,f,c);
2686 | finalY[offset++] = temp[0];
2687 | } else
2688 | finalY[offset++] = 0;
2689 | }
2690 | }
2691 | if (f.valid_bits == INVALID_BITS) { zero_channel[i] = true; continue goto_channels;/*goto error*/}; // behavior according to spec
2692 | step2_flag[0] = step2_flag[1] = 1;
2693 | for (j=2; j < g.values; ++j) {
2694 | var low=int_, high=int_, pred=int_, highroom=int_, lowroom=int_, room=int_, val=int_;
2695 | low = g.neighbors[j][0];
2696 | high = g.neighbors[j][1];
2697 | //neighbors(g->Xlist, j, &low, &high);
2698 | pred = predict_point(g.Xlist[j], g.Xlist[low], g.Xlist[high], finalY[low], finalY[high]);
2699 | val = finalY[j];
2700 | highroom = range - pred;
2701 | lowroom = pred;
2702 | if (highroom < lowroom)
2703 | room = highroom * 2;
2704 | else
2705 | room = lowroom * 2;
2706 | if (val) {
2707 | step2_flag[low] = step2_flag[high] = 1;
2708 | step2_flag[j] = 1;
2709 | if (val >= room)
2710 | if (highroom > lowroom)
2711 | finalY[j] = val - lowroom + pred;
2712 | else
2713 | finalY[j] = pred - val + highroom - 1;
2714 | else
2715 | if (val & 1)
2716 | finalY[j] = pred - ((val+1)>>>1);
2717 | else
2718 | finalY[j] = pred + (val>>>1);
2719 | } else {
2720 | step2_flag[j] = 0;
2721 | finalY[j] = pred;
2722 | }
2723 | }
2724 |
2725 | //#ifdef STB_VORBIS_NO_DEFER_FLOOR
2726 | // do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag);
2727 | //#else
2728 | // defer final floor computation until _after_ residue
2729 | for (j=0; j < g.values; ++j) {
2730 | if (!step2_flag[j])
2731 | finalY[j] = -1;
2732 | }
2733 | //#endif
2734 | } else {
2735 | // error:
2736 | zero_channel[i] = true;
2737 | }
2738 | // So we just defer everything else to later
2739 |
2740 | // at this point we've decoded the floor into buffer
2741 | }
2742 | }
2743 | stb_prof(0);
2744 | // at this point we've decoded all floors
2745 |
2746 | if (f.alloc.alloc_buffer)
2747 | assert(f.alloc.alloc_buffer_length_in_bytes == f.temp_offset);
2748 |
2749 | // re-enable coupled channels if necessary
2750 | memcpy(really_zero_channel, 0, zero_channel, 0, f.channels);//sizeof(really_zero_channel[0]) *
2751 | for (i=0; i < map.coupling_steps; ++i)
2752 | if (!zero_channel[map.chan[i].magnitude] || !zero_channel[map.chan[i].angle]) {
2753 | zero_channel[map.chan[i].magnitude] = zero_channel[map.chan[i].angle] = false;
2754 | }
2755 |
2756 | // RESIDUE DECODE
2757 | for (i=0; i < map.submaps; ++i) {
2758 | var residue_buffers=new Array(STB_VORBIS_MAX_CHANNELS);//float *
2759 | var r=int_,t=int_;
2760 | var do_not_decode=new Array(256);//uint8
2761 | var ch = 0;//int_
2762 | for (j=0; j < f.channels; ++j) {
2763 | if (map.chan[j].mux == i) {
2764 | if (zero_channel[j]) {
2765 | do_not_decode[ch] = true;
2766 | residue_buffers[ch] = null;
2767 | } else {
2768 | do_not_decode[ch] = false;
2769 | residue_buffers[ch] = f.channel_buffers[j];
2770 | }
2771 | ++ch;
2772 | }
2773 | }
2774 | r = map.submap_residue[i];
2775 | t = f.residue_types[r];
2776 | decode_residue(f, residue_buffers, ch, n2, r, do_not_decode);
2777 | }
2778 |
2779 | if (f.alloc.alloc_buffer)
2780 | assert(f.alloc.alloc_buffer_length_in_bytes == f.temp_offset);
2781 |
2782 | // INVERSE COUPLING
2783 | stb_prof(14);
2784 | for (i = map.coupling_steps-1; i >= 0; --i) {
2785 | var n2 = n >>> 1;//int_
2786 | var m_ = f.channel_buffers[map.chan[i].magnitude];//float_ *
2787 | var a_ = f.channel_buffers[map.chan[i].angle ];//float_ *
2788 | for (j=0; j < n2; ++j) {
2789 | var a2=float_,m2=float_;
2790 | if (m_[j] > 0)
2791 | if (a_[j] > 0)
2792 | m2 = m_[j], a2 = m_[j] - a_[j];
2793 | else
2794 | a2 = m_[j], m2 = m_[j] + a_[j];
2795 | else
2796 | if (a_[j] > 0)
2797 | m2 = m_[j], a2 = m_[j] + a_[j];
2798 | else
2799 | a2 = m_[j], m2 = m_[j] - a_[j];
2800 | m_[j] = m2;
2801 | a_[j] = a2;
2802 | }
2803 | }
2804 |
2805 | // finish decoding the floors
2806 | //#ifndef STB_VORBIS_NO_DEFER_FLOOR
2807 | stb_prof(15);
2808 | for (i=0; i < f.channels; ++i) {
2809 | if (really_zero_channel[i]) {
2810 | memset(f.channel_buffers[i], 0, 0, n2);//sizeof(*f->channel_buffers[i]) *
2811 | } else {
2812 | do_floor(f, map, i, n, f.channel_buffers[i], f.finalY[i], null);
2813 | }
2814 | }
2815 | //#else
2816 | // for (i=0; i < f->channels; ++i) {
2817 | // if (really_zero_channel[i]) {
2818 | // memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
2819 | // } else {
2820 | // for (j=0; j < n2; ++j)
2821 | // f->channel_buffers[i][j] *= f->floor_buffers[i][j];
2822 | // }
2823 | // }
2824 | //#endif
2825 |
2826 | // INVERSE MDCT
2827 | stb_prof(16);
2828 | for (i=0; i < f.channels; ++i)
2829 | inverse_mdct(f.channel_buffers[i], n, f, m.blockflag);
2830 | stb_prof(0);
2831 |
2832 | // this shouldn't be necessary, unless we exited on an error
2833 | // and want to flush to get to the next packet
2834 | flush_packet(f);
2835 |
2836 | if (f.first_decode) {
2837 | // assume we start so first non-discarded sample is sample 0
2838 | // this isn't to spec, but spec would require us to read ahead
2839 | // and decode the size of all current frames--could be done,
2840 | // but presumably it's not a commonly used feature
2841 | f.current_loc = -n2; // start of first frame is positioned for discard
2842 | // we might have to discard samples "from" the next frame too,
2843 | // if we're lapping a large block then a small at the start?
2844 | f.discard_samples_deferred = n - right_end;
2845 | f.current_loc_valid = true;
2846 | f.first_decode = false;
2847 | } else if (f.discard_samples_deferred) {
2848 | left_start += f.discard_samples_deferred;
2849 | p_left[0] = left_start;//*
2850 | f.discard_samples_deferred = 0;
2851 | } else if (f.previous_length == 0 && f.current_loc_valid) {
2852 | // we're recovering from a seek... that means we're going to discard
2853 | // the samples from this packet even though we know our position from
2854 | // the last page header, so we need to update the position based on
2855 | // the discarded samples here
2856 | // but wait, the code below is going to add this in itself even
2857 | // on a discard, so we don't need to do it here...
2858 | }
2859 |
2860 | // check if we have ogg information about the sample # for this packet
2861 | if (f.last_seg_which == f.end_seg_with_known_loc) {
2862 | // if we have a valid current loc, and this is final:
2863 | if (f.current_loc_valid && (f.page_flag & PAGEFLAG_last_page)) {
2864 | var current_end = f.known_loc_for_packet - (n-right_end);//uint32
2865 | // then let's infer the size of the (probably) short final frame
2866 | if (current_end < f.current_loc + right_end) {
2867 | if (current_end < f.current_loc) {
2868 | // negative truncation, that's impossible!
2869 | len[0] = 0;//*
2870 | } else {
2871 | len[0] = current_end - f.current_loc;//*
2872 | }
2873 | len[0] += left_start;//*
2874 | f.current_loc += len[0];//*
2875 | return true;
2876 | }
2877 | }
2878 | // otherwise, just set our sample loc
2879 | // guess that the ogg granule pos refers to the _middle_ of the
2880 | // last frame?
2881 | // set f->current_loc to the position of left_start
2882 | f.current_loc = f.known_loc_for_packet - (n2-left_start);
2883 | f.current_loc_valid = true;
2884 | }
2885 | if (f.current_loc_valid)
2886 | f.current_loc += (right_start - left_start);
2887 |
2888 | if (f.alloc.alloc_buffer)
2889 | assert(f.alloc.alloc_buffer_length_in_bytes == f.temp_offset);
2890 | len[0] = right_end;//* // ignore samples after the window goes to 0
2891 | return true;
2892 | }
2893 |
2894 | //3442
2895 | function vorbis_decode_packet(f, len, p_left, p_right)
2896 | {
2897 | var mode=[int_], left_end=[int_], right_end=[int_];
2898 | if (!vorbis_decode_initial(f, p_left, left_end, p_right, right_end, mode)) return 0;
2899 | return vorbis_decode_packet_rest(f, len, f.mode_config[ + mode], p_left[0], left_end[0], p_right[0], right_end[0], p_left);// *p_left, left_end, *p_right, right_end, p_left);
2900 | }
2901 |
2902 | //3449
2903 | function vorbis_finish_frame(f, len, left, right)
2904 | {
2905 | var prev=int_,i=int_,j=int_;
2906 | // we use right&left (the start of the right- and left-window sin()-regions)
2907 | // to determine how much to return, rather than inferring from the rules
2908 | // (same result, clearer code); 'left' indicates where our sin() window
2909 | // starts, therefore where the previous window's right edge starts, and
2910 | // therefore where to start mixing from the previous buffer. 'right'
2911 | // indicates where our sin() ending-window starts, therefore that's where
2912 | // we start saving, and where our returned-data ends.
2913 |
2914 | // mixin from previous window
2915 | if (f.previous_length) {
2916 | var i=int_,j=int_, n = f.previous_length;//int_
2917 | var w = get_window(f, n);//float_ *
2918 | for (i=0; i < f.channels; ++i) {
2919 | for (j=0; j < n; ++j)
2920 | f.channel_buffers[i][left+j] =
2921 | f.channel_buffers[i][left+j]*w[ j] +
2922 | f.previous_window[i][ j]*w[n-1-j];
2923 | }
2924 | }
2925 |
2926 | prev = f.previous_length;
2927 |
2928 | // last half of this data becomes previous window
2929 | f.previous_length = len - right;
2930 |
2931 | // @OPTIMIZE: could avoid this copy by double-buffering the
2932 | // output (flipping previous_window with channel_buffers), but
2933 | // then previous_window would have to be 2x as large, and
2934 | // channel_buffers couldn't be temp mem (although they're NOT
2935 | // currently temp mem, they could be (unless we want to level
2936 | // performance by spreading out the computation))
2937 | for (i=0; i < f.channels; ++i)
2938 | for (j=0; right+j < len; ++j)
2939 | f.previous_window[i][j] = f.channel_buffers[i][right+j];
2940 |
2941 | if (!prev)
2942 | // there was no previous packet, so this data isn't valid...
2943 | // this isn't entirely true, only the would-have-overlapped data
2944 | // isn't valid, but this seems to be what the spec requires
2945 | return 0;
2946 |
2947 | // truncate a short frame
2948 | if (len < right) right = len;
2949 |
2950 | f.samples_output += right-left;
2951 |
2952 | return right - left;
2953 | }
2954 |
2955 | //3501
2956 | function vorbis_pump_first_frame(f)
2957 | {
2958 | var len=[int_], right=[int_], left=[int_];
2959 | if (vorbis_decode_packet(f, len, left, right))
2960 | vorbis_finish_frame(f, len[0], left[0], right[0]);
2961 | }
2962 |
2963 | //3574
2964 | function start_decoder(f)
2965 | {
2966 | var header=new Array(6), x=uint8,y=uint8;//uint8
2967 | var len=int_,i=int_,j=int_,k=int_, max_submaps = 0;
2968 | var longest_floorlist=0;//int_
2969 |
2970 | // first page, first packet
2971 |
2972 | if (!start_page(f)) return false;
2973 | // validate page flag
2974 | if (!(f.page_flag & PAGEFLAG_first_page)) return error(f, VORBIS_invalid_first_page);
2975 | if (f.page_flag & PAGEFLAG_last_page) return error(f, VORBIS_invalid_first_page);
2976 | if (f.page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page);
2977 | // check for expected packet length
2978 | if (f.segment_count != 1) return error(f, VORBIS_invalid_first_page);
2979 | if (f.segments[0] != 30) return error(f, VORBIS_invalid_first_page);
2980 | // read packet
2981 | // check packet header
2982 | if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page);
2983 | if (!getn(f, header, 6)) return error(f, VORBIS_unexpected_eof);
2984 | if (!vorbis_validate(header)) return error(f, VORBIS_invalid_first_page);
2985 | // vorbis_version
2986 | if (get32(f) != 0) return error(f, VORBIS_invalid_first_page);
2987 | f.channels = get8(f); if (!f.channels) return error(f, VORBIS_invalid_first_page);
2988 | if (f.channels > STB_VORBIS_MAX_CHANNELS) return error(f, VORBIS_too_many_channels);
2989 | f.sample_rate = get32(f); if (!f.sample_rate) return error(f, VORBIS_invalid_first_page);
2990 | get32(f); // bitrate_maximum
2991 | get32(f); // bitrate_nominal
2992 | get32(f); // bitrate_minimum
2993 | x = get8(f);
2994 | { var log0=int_,log1=int_;
2995 | log0 = x & 15;
2996 | log1 = x >> 4;
2997 | f.blocksize_0 = 1 << log0;
2998 | f.blocksize_1 = 1 << log1;
2999 | if (log0 < 6 || log0 > 13) return error(f, VORBIS_invalid_setup);
3000 | if (log1 < 6 || log1 > 13) return error(f, VORBIS_invalid_setup);
3001 | if (log0 > log1) return error(f, VORBIS_invalid_setup);
3002 | }
3003 |
3004 | // framing_flag
3005 | x = get8(f);
3006 | if (!(x & 1)) return error(f, VORBIS_invalid_first_page);
3007 |
3008 | // second packet!
3009 | if (!start_page(f)) return false;
3010 |
3011 | if (!start_packet(f)) return false;
3012 | do {
3013 | len = next_segment(f);
3014 | skip(f, len);
3015 | f.bytes_in_seg = 0;
3016 | } while (len);
3017 |
3018 | // third packet!
3019 | if (!start_packet(f)) return false;
3020 |
3021 | //#ifndef STB_VORBIS_NO_PUSHDATA_API
3022 | if (IS_PUSH_MODE(f)) {
3023 | if (!is_whole_packet_present(f, TRUE)) {
3024 | // convert error in ogg header to write type
3025 | if (f.error == VORBIS_invalid_stream)
3026 | f.error = VORBIS_invalid_setup;
3027 | return false;
3028 | }
3029 | }
3030 | //#endif
3031 |
3032 | crc32_init(); // always init it, to avoid multithread race conditions
3033 |
3034 | if (get8_packet(f) != VORBIS_packet_setup) return error(f, VORBIS_invalid_setup);
3035 | for (i=0; i < 6; ++i) header[i] = get8_packet(f);
3036 | if (!vorbis_validate(header)) return error(f, VORBIS_invalid_setup);
3037 |
3038 | // codebooks
3039 |
3040 | f.codebook_count = get_bits(f,8) + 1;
3041 | f.codebooks = Arr_new(f.codebook_count,Codebook);//(Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count);
3042 | if (f.codebooks == null) return error(f, VORBIS_outofmem);
3043 | //memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count);
3044 | for (i=0; i < f.codebook_count; ++i) {
3045 | var values=uint32;//*
3046 | var ordered=int_, sorted_count=int_;
3047 | var total=0;//int_
3048 | var lengths=uint8;//*
3049 | var c = f.codebooks[+i];//Codebook *
3050 | x = get_bits(f, 8); if (x != 0x42) return error(f, VORBIS_invalid_setup);
3051 | x = get_bits(f, 8); if (x != 0x43) return error(f, VORBIS_invalid_setup);
3052 | x = get_bits(f, 8); if (x != 0x56) return error(f, VORBIS_invalid_setup);
3053 | x = get_bits(f, 8);
3054 | c.dimensions = (get_bits(f, 8)<<8) + x;
3055 | x = get_bits(f, 8);
3056 | y = get_bits(f, 8);
3057 | c.entries = (get_bits(f, 8)<<16) + (y<<8) + x;
3058 | ordered = get_bits(f,1);
3059 | c.sparse = ordered ? 0 : get_bits(f,1);
3060 |
3061 | if (c.sparse)
3062 | lengths = new Array(c.entries);//(uint8 *) setup_temp_malloc(f, c->entries);
3063 | else
3064 | lengths = c.codeword_lengths = new Array(c.entries);//(uint8 *) setup_malloc(f, c->entries);
3065 |
3066 | if (!lengths) return error(f, VORBIS_outofmem);
3067 |
3068 | if (ordered) {
3069 | var current_entry = 0;//int_
3070 | var current_length = get_bits(f,5) + 1;//int_
3071 | while (current_entry < c.entries) {
3072 | var limit = c.entries - current_entry;//int_
3073 | var n = get_bits(f, ilog(limit));//int_
3074 | if (current_entry + n > c.entries) { return error(f, VORBIS_invalid_setup); }//(int)
3075 | memset(lengths, + current_entry, current_length, n);
3076 | current_entry += n;
3077 | ++current_length;
3078 | }
3079 | } else {
3080 | for (j=0; j < c.entries; ++j) {
3081 | var present = c.sparse ? get_bits(f,1) : 1;//int_
3082 | if (present) {
3083 | lengths[j] = get_bits(f, 5) + 1;
3084 | ++total;
3085 | } else {
3086 | lengths[j] = NO_CODE;
3087 | }
3088 | }
3089 | }
3090 |
3091 | if (c.sparse && total >= c.entries >> 2) {
3092 | // convert sparse items to non-sparse!
3093 | if (c.entries > f.setup_temp_memory_required)//(int)
3094 | f.setup_temp_memory_required = c.entries;
3095 |
3096 | c.codeword_lengths = new Array(c.entries);//(uint8 *) setup_malloc(f, c->entries);
3097 | memcpy(c.codeword_lengths, 0, lengths, 0, c.entries);
3098 | //setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
3099 | lengths = c.codeword_lengths;
3100 | c.sparse = 0;
3101 | }
3102 |
3103 | // compute the size of the sorted tables
3104 | if (c.sparse) {
3105 | sorted_count = total;
3106 | //assert(total != 0);
3107 | } else {
3108 | sorted_count = 0;
3109 | //#ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
3110 | for (j=0; j < c.entries; ++j)
3111 | if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE)
3112 | ++sorted_count;
3113 | //#endif
3114 | }
3115 |
3116 | c.sorted_entries = sorted_count;
3117 | values = null;
3118 |
3119 | if (!c.sparse) {
3120 | c.codewords = new Array(c.entries);//(uint32 *) setup_malloc(f, sizeof(c->codewords[0]) * c->entries);
3121 | if (!c.codewords) return error(f, VORBIS_outofmem);
3122 | } else {
3123 | var size=int_;//unsigned
3124 | if (c.sorted_entries) {
3125 | c.codeword_lengths = new Array(c.sorted_entries);//(uint8 *) setup_malloc(f, c->sorted_entries);
3126 | if (!c.codeword_lengths) return error(f, VORBIS_outofmem);
3127 | c.codewords = new Array(c.sorted_entries);//(uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries);
3128 | if (!c.codewords) return error(f, VORBIS_outofmem);
3129 | values = new Array(c.sorted_entries);//(uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries);
3130 | if (!values) return error(f, VORBIS_outofmem);
3131 | }
3132 | size = c.entries + /*(sizeof(*c->codewords) + sizeof(*values)) * */c.sorted_entries;
3133 | if (size > f.setup_temp_memory_required)
3134 | f.setup_temp_memory_required = size;
3135 | }
3136 |
3137 | if (!compute_codewords(c, lengths, c.entries, values)) {
3138 | if (c.sparse) setup_temp_free(f, values, 0);
3139 | return error(f, VORBIS_invalid_setup);
3140 | }
3141 |
3142 | if (c.sorted_entries) {
3143 | // allocate an extra slot for sentinels
3144 | c.sorted_codewords = new Array(c.sorted_entries+1);//(uint32 *) setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries+1));
3145 | // allocate an extra slot at the front so that c->sorted_values[-1] is defined
3146 | // so that we can catch that case without an extra if
3147 | c.sorted_values = new Array(c.sorted_entries+1);//( int *) setup_malloc(f, sizeof(*c->sorted_values ) * (c->sorted_entries+1));
3148 | if (c.sorted_values) { ++c.sorted_values_off; c.sorted_values[c.sorted_values_off -1] = -1; }
3149 | compute_sorted_huffman(c, lengths, values);
3150 | }
3151 |
3152 | if (c.sparse) {
3153 | //setup_temp_free(f, values, sizeof(*values)*c->sorted_entries);
3154 | //setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries);
3155 | //setup_temp_free(f, lengths, c->entries);
3156 | c.codewords = null;
3157 | }
3158 |
3159 | compute_accelerated_huffman(c);
3160 |
3161 | c.lookup_type = get_bits(f, 4);
3162 | if (c.lookup_type > 2) return error(f, VORBIS_invalid_setup);
3163 | if (c.lookup_type > 0) {
3164 | var mults=uint16;//*
3165 | c.minimum_value = float32_unpack(get_bits(f, 32));
3166 | c.delta_value = float32_unpack(get_bits(f, 32));
3167 | c.value_bits = get_bits(f, 4)+1;
3168 | c.sequence_p = get_bits(f,1);
3169 | if (c.lookup_type == 1) {
3170 | c.lookup_values = lookup1_values(c.entries, c.dimensions);
3171 | } else {
3172 | c.lookup_values = c.entries * c.dimensions;
3173 | }
3174 | mults = new Array(c.lookup_values);//(uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values);
3175 | if (mults == null) return error(f, VORBIS_outofmem);
3176 | for (j=0; j < c.lookup_values; ++j) {//(int_)
3177 | var q = get_bits(f, c.value_bits);//int_
3178 | if (q == EOP) { /*setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values);*/ return error(f, VORBIS_invalid_setup); }
3179 | mults[j] = q;
3180 | }
3181 |
3182 | //#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
3183 | if (c.lookup_type == 1) {
3184 | var len=int_, sparse = c.sparse;//int_
3185 | var goto_skip = false;
3186 | // pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop
3187 | if (sparse) {
3188 | if (c.sorted_entries == 0) goto_skip=true; else
3189 | c.multiplicands = new Array(c.sorted_entries * c.dimensions);//(codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
3190 | } else
3191 | c.multiplicands = new Array(c.entries * c.dimensions);//(codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries * c->dimensions);
3192 | if (!goto_skip) {
3193 | if (c.multiplicands == null) { /*setup_temp_free(f,mults,sizeof(mults[0])*c->lookup_values);*/ return error(f, VORBIS_outofmem); }
3194 | len = sparse ? c.sorted_entries : c.entries;
3195 | for (j=0; j < len; ++j) {
3196 | var z = sparse ? c.sorted_values[j] : j, div=1;//int_
3197 | for (k=0; k < c.dimensions; ++k) {
3198 | var off = parseInt(z / div,10) % c.lookup_values;//int_
3199 | c.multiplicands[j*c.dimensions + k] =
3200 | // #ifndef STB_VORBIS_CODEBOOK_FLOATS
3201 | // mults[off];
3202 | // #else
3203 | mults[off]*c.delta_value + c.minimum_value;
3204 | // in this case (and this case only) we could pre-expand c->sequence_p,
3205 | // and throw away the decode logic for it; have to ALSO do
3206 | // it in the case below, but it can only be done if
3207 | // STB_VORBIS_CODEBOOK_FLOATS
3208 | // !STB_VORBIS_DIVIDES_IN_CODEBOOK
3209 | // #endif
3210 | div *= c.lookup_values;
3211 | }
3212 | }
3213 | //setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
3214 | c.lookup_type = 2;
3215 | }
3216 | }
3217 | else
3218 | //#endif
3219 | {
3220 | c.multiplicands = new Array(c.lookup_values);//(codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
3221 | // #ifndef STB_VORBIS_CODEBOOK_FLOATS
3222 | // memcpy(c->multiplicands, mults, sizeof(c->multiplicands[0]) * c->lookup_values);
3223 | // #else
3224 | for (j=0; j < c.lookup_values; ++j)//(int_)
3225 | c.multiplicands[j] = mults[j] * c.delta_value + c.minimum_value;
3226 | //setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
3227 | // #endif
3228 | }
3229 | // skip:;
3230 |
3231 | //#ifdef STB_VORBIS_CODEBOOK_FLOATS
3232 | if (c.lookup_type == 2 && c.sequence_p) {
3233 | for (j=1; j < c.lookup_values; ++j)//(int_)
3234 | c.multiplicands[j] = c.multiplicands[j-1];
3235 | c.sequence_p = 0;
3236 | }
3237 | //#endif
3238 | }
3239 | }
3240 |
3241 | // time domain transfers (notused)
3242 |
3243 | x = get_bits(f, 6) + 1;
3244 | for (i=0; i < x; ++i) {
3245 | var z = get_bits(f, 16);//uint32
3246 | if (z != 0) return error(f, VORBIS_invalid_setup);
3247 | }
3248 |
3249 | // Floors
3250 | f.floor_count = get_bits(f, 6)+1;
3251 | f.floor_config = Arr_new(f.floor_count,Floor);//(Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
3252 | for (i=0; i < f.floor_count; ++i) {
3253 | f.floor_types[i] = get_bits(f, 16);
3254 | if (f.floor_types[i] > 1) return error(f, VORBIS_invalid_setup);
3255 | if (f.floor_types[i] == 0) {
3256 | var g = f.floor_config[i].floor0;//Floor0 * = &
3257 | g.order = get_bits(f,8);
3258 | g.rate = get_bits(f,16);
3259 | g.bark_map_size = get_bits(f,16);
3260 | g.amplitude_bits = get_bits(f,6);
3261 | g.amplitude_offset = get_bits(f,8);
3262 | g.number_of_books = get_bits(f,4) + 1;
3263 | for (j=0; j < g.number_of_books; ++j)
3264 | g.book_list[j] = get_bits(f,8);
3265 | return error(f, VORBIS_feature_not_supported);
3266 | } else {
3267 | var p=Arr_new(31*8+2,Point);
3268 | var g = f.floor_config[i].floor1;//Floor1 * = &
3269 | var max_class = -1; //int_
3270 | g.partitions = get_bits(f, 5);
3271 | for (j=0; j < g.partitions; ++j) {
3272 | g.partition_class_list[j] = get_bits(f, 4);
3273 | if (g.partition_class_list[j] > max_class)
3274 | max_class = g.partition_class_list[j];
3275 | }
3276 | for (j=0; j <= max_class; ++j) {
3277 | g.class_dimensions[j] = get_bits(f, 3)+1;
3278 | g.class_subclasses[j] = get_bits(f, 2);
3279 | if (g.class_subclasses[j]) {
3280 | g.class_masterbooks[j] = get_bits(f, 8);
3281 | if (g.class_masterbooks[j] >= f.codebook_count) return error(f, VORBIS_invalid_setup);
3282 | }
3283 | for (k=0; k < (1 << g.class_subclasses[j])>>>0; ++k) {
3284 | g.subclass_books[j][k] = get_bits(f,8)-1;
3285 | if (g.subclass_books[j][k] >= f.codebook_count) return error(f, VORBIS_invalid_setup);
3286 | }
3287 | }
3288 | g.floor1_multiplier = get_bits(f,2)+1;
3289 | g.rangebits = get_bits(f,4);
3290 | g.Xlist[0] = 0;
3291 | g.Xlist[1] = (1 << g.rangebits)>>>0;
3292 | g.values = 2;
3293 | for (j=0; j < g.partitions; ++j) {
3294 | var c = g.partition_class_list[j];//int_
3295 | for (k=0; k < g.class_dimensions[c]; ++k) {
3296 | g.Xlist[g.values] = get_bits(f, g.rangebits);
3297 | ++g.values;
3298 | }
3299 | }
3300 | // precompute the sorting
3301 | for (j=0; j < g.values; ++j) {
3302 | p[j].x = g.Xlist[j];
3303 | p[j].y = j;
3304 | }
3305 | p.sort(point_compare);//qsort(p, g->values, sizeof(p[0]), point_compare);
3306 | for (j=0; j < g.values; ++j)
3307 | g.sorted_order[j] = p[j].y;//(uint8)
3308 | // precompute the neighbors
3309 | for (j=2; j < g.values; ++j) {
3310 | var low=[int_],hi=[int_];
3311 | neighbors(g.Xlist, j, low,hi);
3312 | g.neighbors[j][0] = low[0];
3313 | g.neighbors[j][1] = hi[0];
3314 | }
3315 |
3316 | if (g.values > longest_floorlist)
3317 | longest_floorlist = g.values;
3318 | }
3319 | }
3320 |
3321 | // Residue
3322 | f.residue_count = get_bits(f, 6)+1;
3323 | f.residue_config = Arr_new(f.residue_count,Residue);//(Residue *) setup_malloc(f, f->residue_count * sizeof(*f->residue_config));
3324 | for (i=0; i < f.residue_count; ++i) {
3325 | var residue_cascade=new Array(64);//uint8
3326 | var r = f.residue_config[+i];//Residue *
3327 | f.residue_types[i] = get_bits(f, 16);
3328 | if (f.residue_types[i] > 2) return error(f, VORBIS_invalid_setup);
3329 | r.begin = get_bits(f, 24);
3330 | r.end = get_bits(f, 24);
3331 | r.part_size = get_bits(f,24)+1;
3332 | r.classifications = get_bits(f,6)+1;
3333 | r.classbook = get_bits(f,8);
3334 | for (j=0; j < r.classifications; ++j) {
3335 | var high_bits=0;//uint8
3336 | var low_bits=get_bits(f,3);//uint8
3337 | if (get_bits(f,1))
3338 | high_bits = get_bits(f,5);
3339 | residue_cascade[j] = high_bits*8 + low_bits;
3340 | }
3341 | r.residue_books = new Array(r.classifications);for(var a=0;aresidue_books[0]) * r->classifications);
3342 | for (j=0; j < r.classifications; ++j) {
3343 | for (k=0; k < 8; ++k) {
3344 | if (residue_cascade[j] & (1 << k)) {
3345 | r.residue_books[j][k] = get_bits(f, 8);
3346 | if (r.residue_books[j][k] >= f.codebook_count) return error(f, VORBIS_invalid_setup);
3347 | } else {
3348 | r.residue_books[j][k] = -1;
3349 | }
3350 | }
3351 | }
3352 | // precompute the classifications[] array to avoid inner-loop mod/divide
3353 | // call it 'classdata' since we already have r->classifications
3354 | r.classdata = new Array(f.codebooks[r.classbook].entries);//(uint8 **) setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
3355 | if (!r.classdata) return error(f, VORBIS_outofmem);
3356 | memset(r.classdata, 0, 0, f.codebooks[r.classbook].entries);//sizeof(*r->classdata) *
3357 | for (j=0; j < f.codebooks[r.classbook].entries; ++j) {
3358 | var classwords = f.codebooks[r.classbook].dimensions;//int_
3359 | var temp = j;//int_
3360 | r.classdata[j] = new Array(classwords);//(uint8 *) setup_malloc(f, sizeof(r->classdata[j][0]) * classwords);
3361 | for (k=classwords-1; k >= 0; --k) {
3362 | r.classdata[j][k] = temp % r.classifications;
3363 | temp /= r.classifications; temp=parseInt(temp,10);
3364 | }
3365 | }
3366 | }
3367 |
3368 | f.mapping_count = get_bits(f,6)+1;
3369 | f.mapping = Arr_new(f.mapping_count,Mapping);//(Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping));
3370 | for (i=0; i < f.mapping_count; ++i) {
3371 | var m = f.mapping[ + i]; //Mapping *
3372 | var mapping_type = get_bits(f,16);//int_
3373 | if (mapping_type != 0) return error(f, VORBIS_invalid_setup);
3374 | m.chan = Arr_new(f.channels,MappingChannel);//(MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan));
3375 | if (get_bits(f,1))
3376 | m.submaps = get_bits(f,4);
3377 | else
3378 | m.submaps = 1;
3379 | if (m.submaps > max_submaps)
3380 | max_submaps = m.submaps;
3381 | if (get_bits(f,1)) {
3382 | m.coupling_steps = get_bits(f,8)+1;
3383 | for (k=0; k < m.coupling_steps; ++k) {
3384 | m.chan[k].magnitude = get_bits(f, ilog(f.channels)-1);
3385 | m.chan[k].angle = get_bits(f, ilog(f.channels)-1);
3386 | if (m.chan[k].magnitude >= f.channels) return error(f, VORBIS_invalid_setup);
3387 | if (m.chan[k].angle >= f.channels) return error(f, VORBIS_invalid_setup);
3388 | if (m.chan[k].magnitude == m.chan[k].angle) return error(f, VORBIS_invalid_setup);
3389 | }
3390 | } else
3391 | m.coupling_steps = 0;
3392 |
3393 | // reserved field
3394 | if (get_bits(f,2)) return error(f, VORBIS_invalid_setup);
3395 | if (m.submaps > 1) {
3396 | for (j=0; j < f.channels; ++j) {
3397 | m.chan[j].mux = get_bits(f, 4);
3398 | if (m.chan[j].mux >= m.submaps) return error(f, VORBIS_invalid_setup);
3399 | }
3400 | } else
3401 | // @SPECIFICATION: this case is missing from the spec
3402 | for (j=0; j < f.channels; ++j)
3403 | m.chan[j].mux = 0;
3404 |
3405 | for (j=0; j < m.submaps; ++j) {
3406 | get_bits(f,8); // discard
3407 | m.submap_floor[j] = get_bits(f,8);
3408 | m.submap_residue[j] = get_bits(f,8);
3409 | if (m.submap_floor[j] >= f.floor_count) return error(f, VORBIS_invalid_setup);
3410 | if (m.submap_residue[j] >= f.residue_count) return error(f, VORBIS_invalid_setup);
3411 | }
3412 | }
3413 |
3414 | // Modes
3415 | f.mode_count = get_bits(f, 6)+1;
3416 | for (i=0; i < f.mode_count; ++i) {
3417 | var m = f.mode_config[+i];//Mode *
3418 | m.blockflag = get_bits(f,1);
3419 | m.windowtype = get_bits(f,16);
3420 | m.transformtype = get_bits(f,16);
3421 | m.mapping = get_bits(f,8);
3422 | if (m.windowtype != 0) return error(f, VORBIS_invalid_setup);
3423 | if (m.transformtype != 0) return error(f, VORBIS_invalid_setup);
3424 | if (m.mapping >= f.mapping_count) return error(f, VORBIS_invalid_setup);
3425 | }
3426 |
3427 | flush_packet(f);
3428 |
3429 | f.previous_length = 0;
3430 |
3431 | for (i=0; i < f.channels; ++i) {
3432 | f.channel_buffers[i] = new Array(f.blocksize_1);//(float *) setup_malloc(f, sizeof(float) * f->blocksize_1);
3433 | f.previous_window[i] = new Array(parseInt(f.blocksize_1/2,10));//(float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2);
3434 | f.finalY[i] = new Array(longest_floorlist);//(int16 *) setup_malloc(f, sizeof(int16) * longest_floorlist);
3435 | // #ifdef STB_VORBIS_NO_DEFER_FLOOR
3436 | // f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2);
3437 | // #endif
3438 | }
3439 |
3440 | if (!init_blocksize(f, 0, f.blocksize_0)) return false;
3441 | if (!init_blocksize(f, 1, f.blocksize_1)) return false;
3442 | f.blocksize[0] = f.blocksize_0;
3443 | f.blocksize[1] = f.blocksize_1;
3444 |
3445 | //#ifdef STB_VORBIS_DIVIDE_TABLE
3446 | // if (integer_divide_table[1][1]==0)
3447 | // for (i=0; i < DIVTAB_NUMER; ++i)
3448 | // for (j=1; j < DIVTAB_DENOM; ++j)
3449 | // integer_divide_table[i][j] = i / j;
3450 | //#endif
3451 |
3452 | // compute how much temporary memory is needed
3453 |
3454 | // 1.
3455 | {
3456 | var imdct_mem = (f.blocksize_1 >>> 1);//uint32 // * sizeof(float)
3457 | var classify_mem=uint32;
3458 | var i=int_,max_part_read=0;//int_
3459 | for (i=0; i < f.residue_count; ++i) {
3460 | var r = f.residue_config[ + i];//Residue *
3461 | var n_read = r.end - r.begin;//int_
3462 | var part_read = parseInt(n_read / r.part_size,10);//int_
3463 | if (part_read > max_part_read)
3464 | max_part_read = part_read;
3465 | }
3466 | // #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
3467 | classify_mem = f.channels + max_part_read;// * (sizeof(void*) * sizeof(uint8 *))
3468 | // #else
3469 | // classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *));
3470 | // #endif
3471 |
3472 | f.temp_memory_required = classify_mem;
3473 | if (imdct_mem > f.temp_memory_required)
3474 | f.temp_memory_required = imdct_mem;
3475 | }
3476 |
3477 | f.first_decode = true;
3478 |
3479 | if (f.alloc.alloc_buffer) {
3480 | assert(f.temp_offset == f.alloc.alloc_buffer_length_in_bytes);
3481 | // check if there's enough temp memory so we don't error later
3482 | if (f.setup_offset + f.temp_memory_required > f.temp_offset)//sizeof(*f) + > (unsigned)
3483 | return error(f, VORBIS_outofmem);
3484 | }
3485 |
3486 | f.first_audio_page_offset = stb_vorbis_get_file_offset(f);
3487 |
3488 | return true;
3489 | }
3490 |
3491 | //4154
3492 | function vorbis_init(p, z)
3493 | {
3494 | //memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start
3495 | if (z) {
3496 | p.alloc = z;//*
3497 | p.alloc.alloc_buffer_length_in_bytes = (p.alloc.alloc_buffer_length_in_bytes+3) & ~3;
3498 | p.temp_offset = p.alloc.alloc_buffer_length_in_bytes;
3499 | }
3500 | p.eof = 0;
3501 | p.error = VORBIS__no_error;
3502 | p.stream = null;
3503 | p.codebooks = null;
3504 | p.page_crc_tests = -1;
3505 | //#ifndef STB_VORBIS_NO_STDIO
3506 | p.close_on_free = false;
3507 | p.f = null;
3508 | //#endif
3509 | }
3510 |
3511 | //4200
3512 | function vorbis_alloc(f)
3513 | {
3514 | var p = new stb_vorbis();//(stb_vorbis *) setup_malloc(f, sizeof(*p));//stb_vorbis *
3515 | return p;
3516 | }
3517 |
3518 | //4411
3519 | function stb_vorbis_get_file_offset(f)
3520 | {
3521 | //#ifndef STB_VORBIS_NO_PUSHDATA_API
3522 | if (f.push_mode) return 0;
3523 | //#endif
3524 | if (USE_MEMORY(f)) return f.stream - f.stream_start;
3525 | //#ifndef STB_VORBIS_NO_STDIO
3526 | return ftell(f.f) - f.f_start;
3527 | //#endif
3528 | }
3529 |
3530 | //4935
3531 | function stb_vorbis_get_frame_float(f, channels, output)
3532 | {
3533 | var len=[int_], right=[int_],left=[int_],i=int_;
3534 | if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
3535 |
3536 | if (!vorbis_decode_packet(f, len, left, right)) {//,&,&,&
3537 | f.channel_buffer_start = f.channel_buffer_end = 0;
3538 | return 0;
3539 | }
3540 |
3541 | len = vorbis_finish_frame(f, len[0], left[0], right[0]);
3542 | for (i=0; i < f.channels; ++i)
3543 | f.outputs[i] = f.channel_buffers[i].slice( + left);
3544 |
3545 | f.channel_buffer_start = left;
3546 | f.channel_buffer_end = left+len;
3547 |
3548 | if (channels) channels[0] = f.channels;//*
3549 | if (output) output[0] = f.outputs;//*
3550 | return len;
3551 | }
3552 |
3553 | //#ifndef STB_VORBIS_NO_STDIO
3554 |
3555 | //4959
3556 | function stb_vorbis_open_file_section(file, close_on_free, error, alloc, length)
3557 | {
3558 | var f=new stb_vorbis(), p=new stb_vorbis();//*
3559 | vorbis_init(p, alloc);
3560 | p.f = file;
3561 | p.f_start = ftell(file);
3562 | p.stream_len = length;
3563 | p.close_on_free = close_on_free;
3564 | if (start_decoder(p)) {
3565 | f = vorbis_alloc(p);
3566 | if (f) {
3567 | f = p;//* =
3568 | vorbis_pump_first_frame(f);
3569 | return f;
3570 | }
3571 | }
3572 | if (error) error[0] = p.error;//*
3573 | //vorbis_deinit(p);//&
3574 | return null;
3575 | }
3576 |
3577 | //4980
3578 | function stb_vorbis_open_file(file, close_on_free, error, alloc)
3579 | {
3580 | var len=int_, start=int_;//unsigned
3581 | //start = ftell(file);
3582 | //fseek(file, 0, SEEK_END);
3583 | len = file._ptr.length;//ftell(file) - start;
3584 | //fseek(file, start, SEEK_SET);
3585 | return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len);
3586 | }
3587 |
3588 | //4990
3589 | function stb_vorbis_open_filename(filename, error, alloc)
3590 | {
3591 | var f = fopen(filename, "rb");//FILE *
3592 | if (f)
3593 | return stb_vorbis_open_file(f, true, error, alloc);
3594 | if (error) error[0] = VORBIS_file_open_failure;//*
3595 | return null;
3596 | }
3597 | //#endif // STB_VORBIS_NO_STDIO
3598 |
3599 | //5062
3600 | function copy_samples(dest, dest_off, src, len)
3601 | {
3602 | var i=int_;
3603 | //check_endianness();
3604 | for (i=0; i < len; ++i) {
3605 | //FASTDEF(temp);
3606 | var v = Math.round(src[i]*32768);//int_ //FAST_SCALED_FLOAT_TO_INT(temp, src[i],15);
3607 | if ((v + 32768) > 65535)//(unsigned int_)
3608 | v = v < 0 ? -32768 : 32767;
3609 | dest[dest_off++] = v&0xff;//+i
3610 | dest[dest_off++] = v>>>8&0xff;//+i
3611 | }
3612 | }
3613 |
3614 | //5075
3615 | /*static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len)
3616 | {
3617 | #define BUFFER_SIZE 32
3618 | float buffer[BUFFER_SIZE];
3619 | int i,j,o,n = BUFFER_SIZE;
3620 | check_endianness();
3621 | for (o = 0; o < len; o += BUFFER_SIZE) {
3622 | memset(buffer, 0, sizeof(buffer));
3623 | if (o + n > len) n = len - o;
3624 | for (j=0; j < num_c; ++j) {
3625 | if (channel_position[num_c][j] & mask) {
3626 | for (i=0; i < n; ++i)
3627 | buffer[i] += data[j][d_offset+o+i];
3628 | }
3629 | }
3630 | for (i=0; i < n; ++i) {
3631 | FASTDEF(temp);
3632 | int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
3633 | if ((unsigned int) (v + 32768) > 65535)
3634 | v = v < 0 ? -32768 : 32767;
3635 | output[o+i] = v;
3636 | }
3637 | }
3638 | }*/
3639 |
3640 | //5100
3641 | /*static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} };*/
3642 | //5101
3643 | /*static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len)
3644 | {
3645 | #define BUFFER_SIZE 32
3646 | float buffer[BUFFER_SIZE];
3647 | int i,j,o,n = BUFFER_SIZE >> 1;
3648 | // o is the offset in the source data
3649 | check_endianness();
3650 | for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
3651 | // o2 is the offset in the output data
3652 | int o2 = o << 1;
3653 | memset(buffer, 0, sizeof(buffer));
3654 | if (o + n > len) n = len - o;
3655 | for (j=0; j < num_c; ++j) {
3656 | int m = channel_position[num_c][j] & (PLAYBACK_LEFT | PLAYBACK_RIGHT);
3657 | if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) {
3658 | for (i=0; i < n; ++i) {
3659 | buffer[i*2+0] += data[j][d_offset+o+i];
3660 | buffer[i*2+1] += data[j][d_offset+o+i];
3661 | }
3662 | } else if (m == PLAYBACK_LEFT) {
3663 | for (i=0; i < n; ++i) {
3664 | buffer[i*2+0] += data[j][d_offset+o+i];
3665 | }
3666 | } else if (m == PLAYBACK_RIGHT) {
3667 | for (i=0; i < n; ++i) {
3668 | buffer[i*2+1] += data[j][d_offset+o+i];
3669 | }
3670 | }
3671 | }
3672 | for (i=0; i < (n<<1); ++i) {
3673 | FASTDEF(temp);
3674 | int v = FAST_SCALED_FLOAT_TO_INT(temp,buffer[i],15);
3675 | if ((unsigned int) (v + 32768) > 65535)
3676 | v = v < 0 ? -32768 : 32767;
3677 | output[o2+i] = v;
3678 | }
3679 | }
3680 | }*/
3681 |
3682 | //5140
3683 | function convert_samples_short(buf_c, buffer, buffer_off, b_offset, data_c, data, d_offset, samples)
3684 | {
3685 | var i=int_;
3686 | if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {alert('Not implemented #1');
3687 | // static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} };
3688 | // for (i=0; i < buf_c; ++i)
3689 | // compute_samples(channel_selector[buf_c][i], buffer[i]+b_offset, data_c, data, d_offset, samples);
3690 | } else {
3691 | var limit = buf_c < data_c ? buf_c : data_c;//int_
3692 | for (i=0; i < limit; ++i)
3693 | copy_samples(buffer,buffer_off+(i)+b_offset, data[i], samples);
3694 | for ( ; i < buf_c; ++i)
3695 | memset(buffer,buffer_off+(i)+b_offset, 0, samples);//sizeof(short_) *
3696 | }
3697 | }
3698 |
3699 | //5156
3700 | function stb_vorbis_get_frame_short(f, num_c, buffer, buffer_off, num_samples)
3701 | {
3702 |
3703 | var output=[float_];//**
3704 | var len = stb_vorbis_get_frame_float(f, null, output);//int_,&
3705 | if (len > num_samples) len = num_samples;
3706 | if (len)
3707 | convert_samples_short(num_c, buffer, buffer_off, 0, f.channels, output[0], 0, len);
3708 | return len;
3709 | }
3710 |
3711 | //5166
3712 | function convert_channels_short_interleaved(buf_c, buffer, buffer_off, data_c, data, d_offset, len)
3713 | {
3714 | var i=int_;
3715 | //check_endianness();
3716 | if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {alert('Not implemented #2');
3717 | // assert(buf_c == 2);
3718 | // for (i=0; i < buf_c; ++i)
3719 | // compute_stereo_samples(buffer, data_c, data, d_offset, len);
3720 | } else {
3721 | var limit = buf_c < data_c ? buf_c : data_c;//int_
3722 | var j=int_;
3723 | for (j=0; j < len; ++j) {
3724 | for (i=0; i < limit; ++i) {
3725 | //FASTDEF(temp);
3726 | var f = data[i][d_offset+j];//float_
3727 | var v = Math.round(f*32768);//int_ //FAST_SCALED_FLOAT_TO_INT(temp, f,15);//data[i][d_offset+j],15);
3728 | if ((v + 32768) > 65535)//(unsigned int_)
3729 | v = v < 0 ? -32768 : 32767;
3730 | buffer[buffer_off++] = v&0xff;//*
3731 | buffer[buffer_off++] = v>>>8&0xff;//*
3732 | }
3733 | for ( ; i < buf_c; ++i) {
3734 | buffer[buffer_off++] = 0;//*
3735 | buffer[buffer_off++] = 0;//*
3736 | }
3737 | }
3738 | }
3739 | }
3740 |
3741 | //5192
3742 | function stb_vorbis_get_frame_short_interleaved(f, num_c, buffer, buffer_off, num_shorts)
3743 | {
3744 | var output=[float_];//**
3745 | var len=int_;
3746 | if (num_c == 1) return stb_vorbis_get_frame_short(f,num_c,buffer,buffer_off, num_shorts);//&
3747 | len = stb_vorbis_get_frame_float(f, null, output);
3748 | if (len) {
3749 | if (len*num_c > num_shorts) len = parseInt(num_shorts / num_c, 10);
3750 | convert_channels_short_interleaved(num_c, buffer, buffer_off, f.channels, output[0], 0, len);
3751 | }
3752 | return len;
3753 | }
3754 |
3755 | //5245
3756 | //#ifndef STB_VORBIS_NO_STDIO
3757 | function stb_vorbis_decode_filename(filename, channels, output)
3758 | {
3759 | var data_len=int_, offset=int_, total=int_, limit=int_, error=[int_];
3760 | var data=short_;//*
3761 | var v = stb_vorbis_open_filename(filename, error, null);//stb_vorbis *
3762 | if (v == null) return -1;
3763 | //var buffer_time = 4; //seconds
3764 | limit = v.channels * 4096;
3765 | channels[0] = v.channels;//*
3766 | function sound_Chunk(rest, data) {
3767 | if (data)
3768 | var dataURI = "data:audio/wav;base64,"+(btoa(uint8ToString(data)));//escape
3769 | offset = data_len = 0;
3770 | data_len = rest.length/v.channels/2;//0;
3771 | offset = 44+rest.length; //add by d (WAVE-HEADER)
3772 | total = limit;
3773 | data = (new Array(44)).concat(rest);//new Array(total);//(short *) malloc(total * sizeof(*data));
3774 | if (data == null) {
3775 | //stb_vorbis_close(v);
3776 | return -2;
3777 | }
3778 | for (;;) {
3779 | var n = stb_vorbis_get_frame_short_interleaved(v, v.channels, data,+offset, total-offset);//int_
3780 | //document.writeln(n+"\n");
3781 | if (n == 0) break;
3782 | data_len += n;
3783 | offset += n * v.channels*2;
3784 | if (data_len>v.sample_rate*buffer_time) break;
3785 | if (offset + limit > total) {
3786 | var data2=short_;//*
3787 | total *= 2*2;
3788 | data2 = data;data.length=total;//(short *) realloc(data, total * sizeof(*data));
3789 | if (data2 == null) {
3790 | free(data);
3791 | stb_vorbis_close(v);
3792 | return -2;
3793 | }
3794 | data = data2;
3795 | }
3796 | }
3797 | data.length = offset;
3798 | var rest = data.splice(v.channels*2 * v.sample_rate*buffer_time+44); data_len = data_len0 && setTimeout(function(){
3802 | sound_Chunk(rest, data);},dataURI?buffer_time*1000:0);
3803 | }
3804 | sound_Chunk([]);
3805 | output[0] = data;//*
3806 | return data_len;
3807 | }
3808 | //#endif // NO_STDIO
3809 |
3810 | var bits = 16;//static int_
3811 | function WRITE_U32(buf, buf_off, x) { buf[buf_off] = x&0xff;
3812 | buf[buf_off+1] = x>>>8&0xff;
3813 | buf[buf_off+2] = x>>>16&0xff;
3814 | buf[buf_off+3] = x>>>24&0xff;
3815 | }
3816 |
3817 | function WRITE_U16(buf, buf_off, x) { buf[buf_off] = x&0xff;
3818 | buf[buf_off+1] = x>>>8&0xff;
3819 | }
3820 |
3821 | /* Some of this based on ao/src/ao_wav.c */
3822 | function write_prelim_header(v, headbuf, knownlength) {
3823 | var size = 0x7fffffff;//unsigned int_
3824 | var channels = v.channels;//ov_info(vf,0)->channels;//int_
3825 | var samplerate = v.sample_rate;//ov_info(vf,0)->rate;//int_
3826 | var bytespersec = channels*samplerate*bits/8;//int_
3827 | var align = channels*bits/8;//int_
3828 | var samplesize = bits;//int_
3829 |
3830 | if(knownlength && knownlength*bits/8*channels < size)
3831 | size = (knownlength*bits/8*channels+44) ;//(unsigned int_)
3832 |
3833 | memcpy(headbuf, 0, new Array(82,73,70,70), 0, 4);//"RIFF"
3834 | WRITE_U32(headbuf,+4, size-8);
3835 | memcpy(headbuf,+8, new Array(87,65,86,69), 0, 4);//"WAVE"
3836 | memcpy(headbuf,+12, new Array(102,109,116,32), 0, 4);//"fmt "
3837 | WRITE_U32(headbuf,+16, 16);
3838 | WRITE_U16(headbuf,+20, 1); /* format */
3839 | WRITE_U16(headbuf,+22, channels);
3840 | WRITE_U32(headbuf,+24, samplerate);
3841 | WRITE_U32(headbuf,+28, bytespersec);
3842 | WRITE_U16(headbuf,+32, align);
3843 | WRITE_U16(headbuf,+34, samplesize);
3844 | memcpy(headbuf,+36, new Array(100,97,116,97), 0, 4);//"data"
3845 | WRITE_U32(headbuf,+40, size - 44);
3846 | headbuf.length=size;
3847 |
3848 | // if(fwrite(headbuf, 1, 44, out) != 44) {
3849 | // fprintf(stderr, _("ERROR: Failed to write Wave header: %s\n"), strerror(errno));
3850 | // return 1;
3851 | // }
3852 |
3853 | return 0;
3854 | }
3855 |
3856 | })();
--------------------------------------------------------------------------------
/sync_def_PF-0_AF-33-BG_FF-33.ogg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dominikhlbg/ogg-vorbis-javascript-decoder/c0c374c5d89ebdbaf06fee25b89316d8894eb06b/sync_def_PF-0_AF-33-BG_FF-33.ogg
--------------------------------------------------------------------------------