PageRenderTime 106ms CodeModel.GetById 41ms app.highlight 29ms RepoModel.GetById 31ms app.codeStats 0ms

/org/xiph/fvorbis/Info.hx

https://github.com/kenmcd/ffmp3
Haxe | 466 lines | 369 code | 21 blank | 76 comment | 114 complexity | b1a55019bb4713afea052822bfa46302 MD5 | raw file
  1package org.xiph.fvorbis;
  2
  3import org.xiph.system.Bytes;
  4
  5import flash.Vector;
  6
  7import org.xiph.fogg.Buffer;
  8import org.xiph.fogg.Packet;
  9
 10class Info {
 11    /*
 12     * generated source for Info
 13     */
 14    inline static private var OV_EBADPACKET : Int = -136;
 15    inline static private var OV_ENOTAUDIO : Int = -135;
 16    static private var _vorbis : Bytes = System.fromString("vorbis");
 17    inline static private var VI_TIMEB : Int = 1;
 18    inline static private var VI_FLOORB : Int = 2;
 19    inline static private var VI_RESB : Int = 3;
 20    inline static private var VI_MAPB : Int = 1;
 21    inline static private var VI_WINDOWB : Int = 1;
 22    public var version : Int;
 23    public var channels : Int;
 24    public var rate : Int;
 25    var bitrate_upper : Int;
 26    var bitrate_nominal : Int;
 27    var bitrate_lower : Int;
 28    // discarded initializer: 'new int[2]';
 29    public var blocksizes : Vector<Int>;
 30    public var modes : Int;
 31    var maps : Int;
 32    public var times : Int;
 33    public var floors : Int;
 34    public var residues : Int;
 35    public var books : Int;
 36    public var psys : Int;
 37    // discarded initializer: 'null';
 38    public var mode_param : Array<InfoMode>;
 39    // discarded initializer: 'null';
 40    public var map_type : Vector<Int>;
 41    // discarded initializer: 'null';
 42    public var map_param : Array<Dynamic>;
 43    // discarded initializer: 'null';
 44    public var time_type : Vector<Int>;
 45    // discarded initializer: 'null';
 46    public var time_param : Array<Dynamic>;
 47    // discarded initializer: 'null';
 48    public var floor_type : Vector<Int>;
 49    // discarded initializer: 'null';
 50    public var floor_param : Array<Dynamic>;
 51    // discarded initializer: 'null';
 52    public var residue_type : Vector<Int>;
 53    // discarded initializer: 'null';
 54    public var residue_param : Array<Dynamic>;
 55    // discarded initializer: 'null';
 56    public var book_param : Array<StaticCodeBook>;
 57    // discarded initializer: 'new PsyInfo[64]';
 58    // psy_param : Vector<PsyInfo>;
 59    var envelopesa : Int;
 60    var preecho_thresh : Float;
 61    var preecho_clamp : Float;
 62
 63    // modifiers: public
 64    public function init() : Void {
 65        rate = 0;
 66    }
 67
 68    // modifiers: public
 69    public function clear() : Void {
 70        // for-while;
 71        var i : Int = 0;
 72        while (i < modes) {
 73            mode_param[i] = null;
 74            i++;
 75        };
 76        mode_param = null;
 77        // for-while;
 78        var i : Int = 0;
 79        while (i < maps) {
 80            FuncMapping.mapping_P[map_type[i]].free_info(map_param[i]);
 81            i++;
 82        };
 83        map_param = null;
 84        // for-while;
 85        var i : Int = 0;
 86        while (i < times) {
 87            FuncTime.time_P[time_type[i]].free_info(time_param[i]);
 88            i++;
 89        };
 90        time_param = null;
 91        // for-while;
 92        var i : Int = 0;
 93        while (i < floors) {
 94            FuncFloor.floor_P[floor_type[i]].free_info(floor_param[i]);
 95            i++;
 96        };
 97        floor_param = null;
 98        // for-while;
 99        var i : Int = 0;
100        while (i < residues) {
101            FuncResidue.residue_P[residue_type[i]].free_info(residue_param[i]);
102            i++;
103        };
104        residue_param = null;
105        // for-while;
106        var i : Int = 0;
107        while (i < books) {
108            if (book_param[i] != null) {
109                book_param[i].clear();
110                book_param[i] = null;
111            };
112            i++;
113        };
114        book_param = null;
115        /*
116        // for-while;
117        var i : Int = 0;
118        while (i < psys) {
119            psy_param[i].free();
120            i++;
121        };
122        */
123    }
124
125    // modifiers: 
126    function unpack_info(opb : Buffer) : Int {
127        version = opb.read(32);
128        if (version != 0) {
129            return -1;
130        };
131        channels = opb.read(8);
132        rate = opb.read(32);
133        bitrate_upper = opb.read(32);
134        bitrate_nominal = opb.read(32);
135        bitrate_lower = opb.read(32);
136        blocksizes[0] = (1 << opb.read(4));
137        blocksizes[1] = (1 << opb.read(4));
138        if (((((rate < 1) || (channels < 1)) || (blocksizes[0] < 8)) || (blocksizes[1] < blocksizes[0])) || (opb.read(1) != 1)) {
139            clear();
140            return -1;
141        };
142        return 0;
143    }
144
145    // modifiers: 
146    function unpack_books(opb : Buffer) : Int {
147        books = (opb.read(8) + 1);
148        if ((book_param == null) || (book_param.length != books)) {
149            //book_param = new StaticCodeBook[books];
150            //book_param = new Vector(books, true);
151            book_param = ArrayTools.alloc(books);
152        };
153        // for-while;
154        var i : Int = 0;
155        while (i < books) {
156            book_param[i] = new StaticCodeBook();
157            if (book_param[i].unpack(opb) != 0) {
158                clear();
159                return -1;
160            };
161            i++;
162        };
163
164        times = (opb.read(6) + 1);
165        if ((time_type == null) || (time_type.length != cast(times,UInt))) {
166            //time_type = new int[times];
167            time_type = new Vector(times, true);
168        };
169        if ((time_param == null) || (time_param.length != times)) {
170            //time_param = new Object[times];
171            //time_param = new Vector(times, true);
172            time_param = ArrayTools.alloc(times);
173        };
174        // for-while;
175        var i : Int = 0;
176        while (i < times) {
177            time_type[i] = opb.read(16);
178            if ((time_type[i] < 0) || (time_type[i] >= Info.VI_TIMEB)) {
179                clear();
180                return -1;
181            };
182            time_param[i] = FuncTime.time_P[time_type[i]].unpack(this, opb);
183            if (time_param[i] == null) {
184                clear();
185                return -1;
186            };
187            i++;
188        };
189        floors = (opb.read(6) + 1);
190        if ((floor_type == null) || (floor_type.length != cast(floors,UInt))) {
191            //floor_type = new int[floors];
192            floor_type = new Vector(floors, true);
193        };
194        if ((floor_param == null) || (floor_param.length != floors)) {
195            //floor_param = new Object[floors];
196            //floor_param = new Vector(floors, true);
197            floor_param = ArrayTools.alloc(floors, true);
198        };
199        // for-while;
200        var i : Int = 0;
201        while (i < floors) {
202            floor_type[i] = opb.read(16);
203            if ((floor_type[i] < 0) || (floor_type[i] >= Info.VI_FLOORB)) {
204                clear();
205                return -1;
206            };
207            floor_param[i] = FuncFloor.floor_P[floor_type[i]].unpack(this, opb);
208            if (floor_param[i] == null) {
209                clear();
210                return -1;
211            };
212            i++;
213        };
214        residues = (opb.read(6) + 1);
215        if ((residue_type == null) || (residue_type.length != cast(residues,UInt))) {
216            //residue_type = new int[residues];
217            residue_type = new Vector(residues, true);
218        };
219        if ((residue_param == null) || (residue_param.length != residues)) {
220            //residue_param = new Object[residues];
221            //residue_param = new Vector(residues, true);
222            residue_param = ArrayTools.alloc(residues);
223        };
224        // for-while;
225        var i : Int = 0;
226        while (i < residues) {
227            residue_type[i] = opb.read(16);
228            if ((residue_type[i] < 0) || (residue_type[i] >= Info.VI_RESB)) {
229                clear();
230                return -1;
231            };
232            residue_param[i] = FuncResidue.residue_P[residue_type[i]].unpack(this, opb);
233            if (residue_param[i] == null) {
234                clear();
235                return -1;
236            };
237            i++;
238        };
239        maps = (opb.read(6) + 1);
240        if ((map_type == null) || (map_type.length != cast(maps,UInt))) {
241            //map_type = new int[maps];
242            map_type = new Vector(maps, true);
243        };
244        if ((map_param == null) || (map_param.length != maps)) {
245            //map_param = new Object[maps];
246            //map_param = new Vector(maps, true);
247            map_param = ArrayTools.alloc(maps);
248        };
249        // for-while;
250        var i : Int = 0;
251        while (i < maps) {
252            map_type[i] = opb.read(16);
253            if ((map_type[i] < 0) || (map_type[i] >= Info.VI_MAPB)) {
254                clear();
255                return -1;
256            };
257            map_param[i] = FuncMapping.mapping_P[map_type[i]].unpack(this, opb);
258            if (map_param[i] == null) {
259                clear();
260                return -1;
261            };
262            i++;
263        };
264        modes = (opb.read(6) + 1);
265        if ((mode_param == null) || (mode_param.length != modes)) {
266            //mode_param = new InfoMode[modes];
267            //mode_param = new Vector(modes, true);
268            mode_param = ArrayTools.alloc(modes);
269        };
270
271
272        // for-while;
273        var i : Int = 0;
274        while (i < modes) {
275            mode_param[i] = new InfoMode();
276            mode_param[i].blockflag = opb.read(1);
277            mode_param[i].windowtype = opb.read(16);
278            mode_param[i].transformtype = opb.read(16);
279            mode_param[i].mapping = opb.read(8);
280            if (((mode_param[i].windowtype >= Info.VI_WINDOWB) || (mode_param[i].transformtype >= Info.VI_WINDOWB)) || (mode_param[i].mapping >= maps)) {
281                clear();
282                return -1;
283            };
284            i++;
285        };
286        if (opb.read(1) != 1) {
287            clear();
288            return -1;
289        };
290        return 0;
291    }
292
293    // modifiers: public
294    public function synthesis_headerin(vc : Comment, op : Packet) : Int {
295        var opb : Buffer = new Buffer();
296        if (op != null) {
297            opb.readinit(op.packet_base, op.packet, op.bytes);
298            var buffer : Bytes = System.alloc(6);
299            var packtype : Int = opb.read(8);
300            opb.readBytes(buffer, 6);
301            /*
302            if (buffer[0] != 'v' || buffer[1] != 'o' || buffer[2] != 'r' ||
303                buffer[3] != 'b' || buffer[4] != 'i' || buffer[5] != 's') {
304                return -1;
305            };
306            */
307            if (buffer.toString() != "vorbis") {
308                return -1;
309            };
310
311            switch (packtype) {
312            case 0x01:
313                if (op.b_o_s == 0) {
314                    return -1;
315                };
316                if (rate != 0) {
317                    return -1;
318                };
319                return unpack_info(opb);
320            case 0x03:
321                if (rate == 0) {
322                    return -1;
323                };
324                return vc.unpack(opb);
325            case 0x05:
326                if ((rate == 0) || (vc.vendor == null)) {
327                    return -1;
328                };
329
330                return unpack_books(opb);
331            default:
332            };
333        };
334        return -1;
335    }
336
337    // modifiers: 
338    function pack_info(opb : Buffer) : Int {
339        opb.write(0x01, 8);
340        opb.writeBytes(Info._vorbis);
341        opb.write(0x00, 32);
342        opb.write(channels, 8);
343        opb.write(rate, 32);
344        opb.write(bitrate_upper, 32);
345        opb.write(bitrate_nominal, 32);
346        opb.write(bitrate_lower, 32);
347        opb.write(Info.ilog2(blocksizes[0]), 4);
348        opb.write(Info.ilog2(blocksizes[1]), 4);
349        opb.write(1, 1);
350        return 0;
351    }
352
353    // modifiers: 
354    function pack_books(opb : Buffer) : Int {
355        opb.write(0x05, 8);
356        opb.writeBytes(Info._vorbis);
357        opb.write(books - 1, 8);
358        // for-while;
359        var i : Int = 0;
360        while (i < books) {
361            if (book_param[i].pack(opb) != 0) {
362                return -1;
363            };
364            i++;
365        };
366        opb.write(times - 1, 6);
367        // for-while;
368        var i : Int = 0;
369        while (i < times) {
370            opb.write(time_type[i], 16);
371            FuncTime.time_P[time_type[i]].pack(this.time_param[i], opb);
372            i++;
373        };
374        opb.write(floors - 1, 6);
375        // for-while;
376        var i : Int = 0;
377        while (i < floors) {
378            opb.write(floor_type[i], 16);
379            FuncFloor.floor_P[floor_type[i]].pack(floor_param[i], opb);
380            i++;
381        };
382        opb.write(residues - 1, 6);
383        // for-while;
384        var i : Int = 0;
385        while (i < residues) {
386            opb.write(residue_type[i], 16);
387            FuncResidue.residue_P[residue_type[i]].pack(residue_param[i], opb);
388            i++;
389        };
390        opb.write(maps - 1, 6);
391        // for-while;
392        var i : Int = 0;
393        while (i < maps) {
394            opb.write(map_type[i], 16);
395            FuncMapping.mapping_P[map_type[i]].pack(this, map_param[i], opb);
396            i++;
397        };
398        opb.write(modes - 1, 6);
399        // for-while;
400        var i : Int = 0;
401        while (i < modes) {
402            opb.write(mode_param[i].blockflag, 1);
403            opb.write(mode_param[i].windowtype, 16);
404            opb.write(mode_param[i].transformtype, 16);
405            opb.write(mode_param[i].mapping, 8);
406            i++;
407        };
408        opb.write(1, 1);
409        return 0;
410    }
411
412    // modifiers: public
413    public function blocksize(op : Packet) : Int {
414        var opb : Buffer = new Buffer();
415        var mode : Int;
416        opb.readinit(op.packet_base, op.packet, op.bytes);
417        if (opb.read(1) != 0) {
418            return Info.OV_ENOTAUDIO;
419        };
420        var modebits : Int = 0;
421        var v : Int = modes;
422        while (v > 1) {
423            modebits++;
424            v >>>= 1;
425        };
426        mode = opb.read(modebits);
427        if (mode == -1) {
428            return Info.OV_EBADPACKET;
429        };
430        return blocksizes[mode_param[mode].blockflag];
431    }
432
433    // modifiers: static, private
434    static private function ilog2(v : Int) : Int {
435        var ret : Int = 0;
436        while (v > 1) {
437            ret++;
438            v >>>= 1;
439        };
440        return ret;
441    }
442
443    // modifiers: public
444    public function toString() : String {
445        return "version:" + version + ", channels:" + channels +
446            ", rate:" + rate + ", bitrate:" + bitrate_upper + "," +
447            bitrate_nominal + "," + bitrate_lower;
448    }
449
450    public function new() {
451        // collected discarded initializers:
452        blocksizes = new Vector(2, true);
453        mode_param = null;
454        map_type = null;
455        map_param = null;
456        time_type = null;
457        time_param = null;
458        floor_type = null;
459        floor_param = null;
460        residue_type = null;
461        residue_param = null;
462        book_param = null;
463        //psy_param = new Vector(64, true);
464    }
465}
466