PageRenderTime 43ms CodeModel.GetById 15ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/org/xiph/fvorbis/CodeBook.hx

https://github.com/kenmcd/ffmp3
Haxe | 634 lines | 503 code | 37 blank | 94 comment | 135 complexity | b6c37df0f0b65df9d097c50e39caa905 MD5 | raw file
  1package org.xiph.fvorbis;
  2
  3import org.xiph.system.Bytes;
  4
  5import flash.Vector;
  6
  7import org.xiph.fogg.Buffer;
  8
  9class CodeBook {
 10    /*
 11     * generated source for CodeBook
 12     */
 13    public var dim : Int;
 14    var entries : Int;
 15    // discarded initializer: 'StaticCodeBook()';
 16    var c : StaticCodeBook;
 17    var valuelist : Vector<Float>;
 18    var codelist : Vector<Int>;
 19    var decode_tree : DecodeAux;
 20
 21    // modifiers: 
 22    function encode(a : Int, b : Buffer) : Int {
 23        b.write(codelist[a], c.lengthlist[a]);
 24        return c.lengthlist[a];
 25    }
 26
 27    // modifiers: 
 28    function errorv(a : Vector<Float>) : Int {
 29        var best : Int = best(a, 1);
 30        // for-while;
 31        var k : Int = 0;
 32        while (k < dim) {
 33            a[k] = valuelist[(best * dim) + k];
 34            k++;
 35        };
 36        return best;
 37    }
 38
 39    // modifiers: 
 40    function encodev(best : Int, a : Vector<Float>, b : Buffer) : Int {
 41        // for-while;
 42        var k : Int = 0;
 43        while (k < dim) {
 44            a[k] = valuelist[(best * dim) + k];
 45            k++;
 46        };
 47        return encode(best, b);
 48    }
 49
 50    // modifiers: 
 51    function encodevs(a : Vector<Float>, b : Buffer, step : Int, addmul : Int) : Int {
 52        var best : Int = besterror(a, step, addmul);
 53        return encode(best, b);
 54    }
 55
 56    // discarded initializer: 'new int[15]';
 57    private var t : Vector<Int>;
 58
 59    // modifiers: synchronized
 60    public function decodevs_add(a : Vector<Float>, offset : Int, b : Buffer, n : Int) : Int {
 61        var step : UInt = Std.int(n / dim);
 62        var entry : Int;
 63        var i : UInt;
 64        var j : UInt;
 65        var o : Int;
 66        if (t.length < step) {
 67            //t = new int[step];
 68            t = new Vector(step);
 69        };
 70        // for-while;
 71        i = 0;
 72        while (i < step) {
 73            entry = decode(b);
 74            if (entry == -1) {
 75                return -1;
 76            };
 77            t[i] = (entry * dim);
 78            i++;
 79        };
 80        // for-while;
 81        i = 0;
 82        o = 0;
 83        while (i < cast(dim,UInt)) {
 84            // for-while;
 85            j = 0;
 86            while (j < step) {
 87                a[(offset + o) + j] += valuelist[t[j] + i];
 88                j++;
 89            };
 90            i++; o += step;
 91        };
 92        return 0;
 93    }
 94
 95    // modifiers: 
 96    public function decodev_add(a : Vector<Float>, offset : Int, b : Buffer, n : Int) : Int {
 97        var i : Int;
 98        var j : Int;
 99        var entry : Int;
100        var t : Int;
101        if (dim > 8) {
102            // for-while;
103            i = 0;
104            while (i < n) {
105                entry = decode(b);
106                if (entry == -1) {
107                    return -1;
108                };
109                t = (entry * dim);
110                // for-while;
111                j = 0;
112                while (j < dim) {
113                    // HAXE200BUG
114                    // a[offset + i++] += valuelist[t + j++];
115                    a[offset + i] += valuelist[t + j];
116                    i++; j++;
117                };
118            };
119        }
120        else {
121            // for-while;
122            i = 0;
123            while (i < n) {
124                entry = decode(b);
125                if (entry == -1) {
126                    return -1;
127                };
128                t = (entry * dim);
129                j = 0;
130
131                {
132                    // FIXME: why isn't this a loop, but some voodoo
133                    // sideeffects-and-oh-wait-a-switch?!
134                    var fall : Bool = false;
135                    if (dim == 8) {
136                        // HAXE200BUG
137                        // a[offset + i++] += valuelist[t + j++];
138                        a[offset + i] += valuelist[t + j];
139                        i++; j++;
140                        fall = true;
141                    };
142                    if (fall || dim == 7) {
143                        // HAXE200BUG
144                        // a[offset + i++] += valuelist[t + j++];
145                        a[offset + i] += valuelist[t + j];
146                        i++; j++;
147                        fall = true;
148                    };
149                    if (fall || dim == 6) {
150                        // HAXE200BUG
151                        // a[offset + i++] += valuelist[t + j++];
152                        a[offset + i] += valuelist[t + j];
153                        i++; j++;
154                        fall = true;
155                    };
156                    if (fall || dim == 5) {
157                        // HAXE200BUG
158                        // a[offset + i++] += valuelist[t + j++];
159                        a[offset + i] += valuelist[t + j];
160                        i++; j++;
161                        fall = true;
162                    };
163                    if (fall || dim == 4) {
164                        // HAXE200BUG
165                        // a[offset + i++] += valuelist[t + j++];
166                        a[offset + i] += valuelist[t + j];
167                        i++; j++;
168                        fall = true;
169                    };
170                    if (fall || dim == 3) {
171                        // HAXE200BUG
172                        // a[offset + i++] += valuelist[t + j++];
173                        a[offset + i] += valuelist[t + j];
174                        i++; j++;
175                        fall = true;
176                    };
177                    if (fall || dim == 2) {
178                        // HAXE200BUG
179                        // a[offset + i++] += valuelist[t + j++];
180                        a[offset + i] += valuelist[t + j];
181                        i++; j++;
182                        fall = true;
183                    };
184                    if (fall || dim == 1) {
185                        // HAXE200BUG
186                        // a[offset + i++] += valuelist[t + j++];
187                        a[offset + i] += valuelist[t + j];
188                        i++; j++;
189                    };
190                    /*
191                    if dim == 0 {
192                    };
193                    */
194                };
195            };
196        };
197        return 0;
198    }
199
200    // modifiers: 
201    public function decodev_set(a : Vector<Float>, offset : Int, b : Buffer, n : Int) : Int {
202        var i : Int;
203        var j : Int;
204        var entry : Int;
205        var t : Int;
206        // for-while;
207        i = 0;
208        while (i < n) {
209            entry = decode(b);
210            if (entry == -1) {
211                return -1;
212            };
213            t = (entry * dim);
214            // for-while;
215            j = 0;
216            while (j < dim) {
217                a[offset + i++] = valuelist[t + j++];
218            };
219        };
220        return 0;
221    }
222
223    // modifiers: 
224    public function decodevv_add(a : Array<Vector<Float>>, offset : Int, ch : Int, b : Buffer, n : Int) : Int {
225        var i : Int;
226        var j : Int;
227        var k : Int;
228        var entry : Int;
229        var chptr : Int = 0;
230        // for-while;
231        i = Std.int(offset / ch);
232        while (i < ((offset + n) / ch)) {
233            entry = decode(b);
234            if (entry == -1) {
235                return -1;
236            };
237            var t : Int = entry * dim;
238            // for-while;
239            j = 0;
240            while (j < dim) {
241                // HAXE200BUG
242                // haXe 2.00 has a bug causing the following code line
243                // to increment chptr twice (because of the "+=" operator?):
244                //   a[chptr++][i] += valuelist[t + j];
245                a[chptr][i] += valuelist[t + j];
246                chptr++;
247                if (chptr == ch) {
248                    chptr = 0;
249                    i++;
250                };
251                j++;
252            };
253        };
254        return 0;
255    }
256
257    // modifiers: 
258    public function decode(b : Buffer) : Int {
259        var ptr : Int = 0;
260        var t : DecodeAux = decode_tree;
261        var lok : Int = b.look(t.tabn);
262        if (lok >= 0) {
263            ptr = t.tab[lok];
264            b.adv(t.tabl[lok]);
265            if (ptr <= 0) {
266                return -ptr;
267            };
268        };
269        do {
270            {
271                var swval = b.read1();
272                if (swval == 0) {
273                    ptr = t.ptr0[ptr];
274                } else if (swval == 1) {
275                    ptr = t.ptr1[ptr];
276                } else {
277                    return -1;
278                };
279            };
280        } while (ptr > 0);
281
282        return -ptr;
283    }
284
285    // modifiers: 
286    public function decodevs(a : Vector<Float>, index : Int, b : Buffer, step : Int, addmul : Int) : Int {
287        var entry : Int = decode(b);
288        if (entry == -1) {
289            return -1;
290        };
291
292        switch (addmul) {
293        case -1:
294            // for-while;
295            var i : Int = 0;
296            var o : Int = 0;
297            while (i < dim) {
298                a[index + o] = valuelist[(entry * dim) + i];
299                i++; o += step;
300            };
301
302        case 0:
303            // for-while;
304            var i : Int = 0;
305            var o : Int = 0;
306            while (i < dim) {
307                a[index + o] += valuelist[(entry * dim) + i];
308                i++; o += step;
309            };
310
311        case 1:
312            // for-while;
313            var i : Int = 0;
314            var o : Int = 0;
315            while (i < dim) {
316                a[index + o] *= valuelist[(entry * dim) + i];
317                i++; o += step;
318            };
319
320        default:
321            trace("CodeBook.decodeves: addmul=" + addmul);
322        };
323
324        return entry;
325    }
326
327    // modifiers: 
328    function best(a : Vector<Float>, step : Int) : Int {
329        var nt : EncodeAuxNearestMatch = c.nearest_tree;
330        var tt : EncodeAuxThreshMatch = c.thresh_tree;
331        var ptr : Int = 0;
332        if (tt != null) {
333            var index : Int = 0;
334            // for-while;
335            var k : Int = 0;
336            var o : Int = step * (dim - 1);
337            while (k < dim) {
338                var i : Int;
339                // for-while;
340                i = 0;
341                while (i < (tt.threshvals - 1)) {
342                    if (a[o] < tt.quantthresh[i]) {
343                        break;
344                    };
345                    i++;
346                };
347                index = ((index * tt.quantvals) + tt.quantmap[i]);
348                k++; o -= step;
349            };
350            if (c.lengthlist[index] > 0) {
351                return index;
352            };
353        };
354        if (nt != null) {
355            while (true) {
356                var c : Float = 0.;
357                var p : Int = nt.p[ptr];
358                var q : Int = nt.q[ptr];
359                // for-while;
360                var k : Int = 0;
361                var o : Int = 0;
362                while (k < dim) {
363                    c += ((valuelist[p + k] - valuelist[q + k]) * (a[o] - ((valuelist[p + k] + valuelist[q + k]) * 0.5)));
364                    k++; o += step;
365                };
366                if (c > 0.) {
367                    ptr = -nt.ptr0[ptr];
368                }
369                else {
370                    ptr = -nt.ptr1[ptr];
371                };
372                if (ptr <= 0) {
373                    break;
374                };
375            };
376            return -ptr;
377        };
378        var besti : Int = -1;
379        var best : Float = 0.;
380        var e : Int = 0;
381        // for-while;
382        var i : Int = 0;
383        while (i < entries) {
384            if (c.lengthlist[i] > 0) {
385                var _this : Float = CodeBook.dist(dim, valuelist, e, a, step);
386                if ((besti == -1) || (_this < best)) {
387                    best = _this;
388                    besti = i;
389                };
390            };
391            e += dim;
392            i++;
393        };
394        return besti;
395    }
396
397    // modifiers: 
398    function besterror(a : Vector<Float>, step : Int, addmul : Int) : Int {
399        var best : Int = best(a, step);
400        switch (addmul) {
401        case 0:
402            // for-while;
403            var i : Int = 0;
404            var o : Int = 0;
405            while (i < dim) {
406                a[o] -= valuelist[(best * dim) + i];
407                i++; o += step;
408            };
409
410        case 1:
411            // for-while;
412            var i : Int = 0;
413            var o : Int = 0;
414            while (i < dim) {
415                var val : Float = valuelist[(best * dim) + i];
416                if (val == 0) {
417                    a[o] = 0;
418                }
419                else {
420                    a[o] /= val;
421                };
422                i++; o += step;
423            };
424        };
425        return best;
426    }
427
428    // modifiers: 
429    function clear() : Void {
430    }
431
432    // modifiers: static, private
433    static private function dist(el : Int, ref : Vector<Float>, index : Int, b : Vector<Float>, step : Int) : Float {
434        var acc : Float = 0.;
435        // for-while;
436        var i : Int = 0;
437        while (i < el) {
438            var val : Float = ref[index + i] - b[i * step];
439            acc += (val * val);
440            i++;
441        };
442        return acc;
443    }
444
445    // modifiers: 
446    public function init_decode(s : StaticCodeBook) : Int {
447        c = s;
448        entries = s.entries;
449        dim = s.dim;
450        valuelist = s.unquantize();
451        decode_tree = make_decode_tree();
452        if (decode_tree == null) {
453            clear();
454            return -1;
455        };
456        return 0;
457    }
458
459    // modifiers: static
460    static function make_words(l : Vector<Int>, n : Int) : Vector<Int> {
461        //var marker : Vector<Int> = new int[33];
462        var marker : Vector<Int> = new Vector(33, true);
463        //var r : Vector<Int> = new int[n];
464        var r : Vector<Int> = new Vector(n, true);
465
466        // for-while;
467        var i : Int = 0;
468        while (i < n) {
469            var length : Int = l[i];
470            if (length > 0) {
471                var entry : Int = marker[length];
472                if ((length < 32) && ((entry >>> length) != 0)) {
473                    return null;
474                };
475                r[i] = entry;
476                // for-while;
477                var j : Int = length;
478                while (j > 0) {
479                    if ((marker[j] & 1) != 0) {
480                        if (j == 1) {
481                            marker[1]++;
482                        }
483                        else {
484                            marker[j] = (marker[j - 1] << 1);
485                        };
486                        break;
487                    };
488                    marker[j]++;
489                    j--;
490                };
491                // for-while;
492                var j : Int = length + 1;
493                while (j < 33) {
494                    if ((marker[j] >>> 1) == entry) {
495                        entry = marker[j];
496                        marker[j] = (marker[j - 1] << 1);
497                    }
498                    else {
499                        break;
500                    };
501                    j++;
502                };
503            };
504            i++;
505        };
506        // for-while;
507        var i : Int = 0;
508        while (i < n) {
509            var temp : Int = 0;
510            // for-while;
511            var j : Int = 0;
512            while (j < l[i]) {
513                temp <<= 1;
514                temp |= ((r[i] >>> j) & 1);
515                j++;
516            };
517            r[i] = temp;
518            i++;
519        };
520        return r;
521    }
522
523    // modifiers: 
524    function make_decode_tree() : DecodeAux {
525        var top : Int = 0;
526        var t : DecodeAux = new DecodeAux();
527        //var ptr0 : Vector<Int> = t.ptr0 = new int[entries * 2];
528        var ptr0 : Vector<Int> = t.ptr0 = new Vector(entries * 2, true);
529        //var ptr1 : Vector<Int> = t.ptr1 = new int[entries * 2];
530        var ptr1 : Vector<Int> = t.ptr1 = new Vector(entries * 2, true);
531        var codelist : Vector<Int> = make_words(c.lengthlist, c.entries);
532        if (codelist == null) {
533            return null;
534        };
535        t.aux = (entries * 2);
536        // for-while;
537        var i : Int = 0;
538        while (i < entries) {
539            if (c.lengthlist[i] > 0) {
540                var ptr : Int = 0;
541                var j : Int;
542                // for-while;
543                j = 0;
544                while (j < (c.lengthlist[i] - 1)) {
545                    var bit : Int = (codelist[i] >>> j) & 1;
546                    if (bit == 0) {
547                        if (ptr0[ptr] == 0) {
548                            ptr0[ptr] = ++top;
549                        };
550                        ptr = ptr0[ptr];
551                    }
552                    else {
553                        if (ptr1[ptr] == 0) {
554                            ptr1[ptr] = ++top;
555                        };
556                        ptr = ptr1[ptr];
557                    };
558                    j++;
559                };
560                if (((codelist[i] >>> j) & 1) == 0) {
561                    ptr0[ptr] = -i;
562                }
563                else {
564                    ptr1[ptr] = -i;
565                };
566            };
567            i++;
568        };
569        t.tabn = (ilog(entries) - 4);
570        if (t.tabn < 5) {
571            t.tabn = 5;
572        };
573        var n : Int = 1 << t.tabn;
574        //t.tab = new int[n];
575        t.tab = new Vector(n, true);
576        //t.tabl = new int[n];
577        t.tabl = new Vector(n, true);
578
579        // for-while;
580        var i : Int = 0;
581        while (i < n) {
582            var p : Int = 0;
583            var j : Int = 0;
584            // for-while;
585            j = 0;
586            while ((j < t.tabn) && ((p > 0) || (j == 0))) {
587                if ((i & (1 << j)) != 0) {
588                    p = ptr1[p];
589                }
590                else {
591                    p = ptr0[p];
592                };
593                j++;
594            };
595            t.tab[i] = p;
596            t.tabl[i] = j;
597            i++;
598        };
599        return t;
600    }
601
602    // modifiers: static, private
603    static private function ilog(v : Int) : Int {
604        var ret : Int = 0;
605        while (v != 0) {
606            ret++;
607            v >>>= 1;
608        };
609        return ret;
610    }
611
612    public function new() {
613        // discarded initializer: 'StaticCodeBook()';
614        c = new StaticCodeBook();
615        // discarded initializer: 'new int[15]';
616        t = new Vector(15, true);
617    }
618
619}
620
621class DecodeAux {
622    /*
623     * generated source for DecodeAux
624     */
625    public var tab : Vector<Int>;
626    public var tabl : Vector<Int>;
627    public var tabn : Int;
628    public var ptr0 : Vector<Int>;
629    public var ptr1 : Vector<Int>;
630    public var aux : Int;
631
632    public function new() {
633    }
634}