123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146(* Claude Code
*
* Copyright (C) 2026 Yoann Padioleau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* (LGPL) as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*)(* See Layer2.mli *)(*****************************************************************************)(* The bit allocation tables (Table B.2) *)(*****************************************************************************)(* the levels allowed, by allocation code; its length is 2^(bits of
* the code) *)letab_0_2=[|0;3;7;15;31;63;127;255;511;1023;2047;4095;8191;16383;32767;65535|]letab_3_10=[|0;3;5;7;9;15;31;63;127;255;511;1023;2047;4095;8191;65535|]letab_11_22=[|0;3;5;7;9;15;31;65535|]letab_23_29=[|0;3;5;65535|]letcd_0_1=[|0;3;5;9;15;31;63;127;255;511;1023;2047;4095;8191;16383;32767|]letcd_2_11=[|0;3;5;9;15;31;63;127|](* ISO/IEC 13818-3's, for MPEG-2's lower sample rates *)letlsf_0_3=[|0;3;5;7;9;15;31;63;127;255;511;1023;2047;4095;8191;16383|]letlsf_4_10=[|0;3;5;9;15;31;63;127|]letlsf_11_29=[|0;3;5;9|]letbands(sblimit:int)(f:int->intarray):intarrayarray=Array.initsblimitflettable_a=bands27(funsb->ifsb<3thenab_0_2elseifsb<11thenab_3_10elseifsb<23thenab_11_22elseab_23_29)lettable_b=bands30(funsb->ifsb<3thenab_0_2elseifsb<11thenab_3_10elseifsb<23thenab_11_22elseab_23_29)lettable_c=bands8(funsb->ifsb<2thencd_0_1elsecd_2_11)lettable_d=bands12(funsb->ifsb<2thencd_0_1elsecd_2_11)lettable_lsf=bands30(funsb->ifsb<4thenlsf_0_3elseifsb<11thenlsf_4_10elselsf_11_29)(* which table: by the bitrate each channel gets (2.4.3.3.1, and Table
* B.2's headings) *)lettable(h:Mpeg_audio_header.t):intarrayarray=ifh.version<>Mpeg_audio_header.Mpeg1thentable_lsfelseletper_channel=h.bitrate/h.channels/1000inifper_channel<=48thenifh.sample_rate=32000thentable_delsetable_celseifper_channel<=80thentable_aelseifh.sample_rate=48000thentable_aelsetable_b(*****************************************************************************)(* Samples *)(*****************************************************************************)letlog2(n:int):int=letrecgok=if1lslk>=nthenkelsego(k+1)ingo0(* 3 samples of [levels] levels, as codes: one grouped number for 3, 5
* and 9 levels (5, 7 and 10 bits), or 3 numbers *)letread_three(b:Bits.t)(levels:int):int*int*int=matchlevelswith|3|5|9->letc=Bits.readb(matchlevelswith3->5|5->7|_->10)in(cmodlevels,c/levelsmodlevels,c/levels/levelsmodlevels)|_->letbits=log2(levels+1)inlets0=Bits.readbbitsinlets1=Bits.readbbitsin(s0,s1,Bits.readbbits)(* [c] of [n] levels, evenly spread in ]-1, 1[ *)letfraction(n:int)(c:int):float=float_of_int((2*c)+1-n)/.float_of_intnletscalefactor(i:int):float=2.**(1.-.(float_of_inti/.3.))letdecode(h:Mpeg_audio_header.t)(b:Bits.t):floatarrayarray=letnch=h.channelsinlettable=tablehinletsblimit=Array.lengthtableinletbound=ifh.mode=Mpeg_audio_header.Joint_stereothenminsblimit(4+(4*h.mode_extension))elsesblimitinletnbalsb=log2(Array.lengthtable.(sb))in(* bit allocation: below the bound for each channel, above it shared *)letalloc=Array.make_matrixnch320inforsb=0tosblimit-1doifsb<boundthenforch=0tonch-1doalloc.(ch).(sb)<-Bits.readb(nbalsb)doneelseleta=Bits.readb(nbalsb)inforch=0tonch-1doalloc.(ch).(sb)<-adonedone;letscfsi=Array.make_matrixnch320inforsb=0tosblimit-1doforch=0tonch-1doifalloc.(ch).(sb)<>0thenscfsi.(ch).(sb)<-Bits.readb2donedone;(* the 3 parts' scalefactors, per scfsi: 3 of them, or shared *)letsf=Array.initnch(fun_->Array.make_matrix3230.)inforsb=0tosblimit-1doforch=0tonch-1doifalloc.(ch).(sb)<>0then(letread()=scalefactor(Bits.readb6)inlets=sf.(ch).(sb)inmatchscfsi.(ch).(sb)with|0->s.(0)<-read();s.(1)<-read();s.(2)<-read()|1->s.(0)<-read();s.(1)<-s.(0);s.(2)<-read()|2->s.(0)<-read();s.(1)<-s.(0);s.(2)<-s.(0)|_->s.(0)<-read();s.(1)<-read();s.(2)<-s.(1))donedone;letout=Array.initnch(fun_->Array.make(36*32)0.)inforgr=0to11doforsb=0tosblimit-1doforch=0to(ifsb<boundthennch-1else0)doleta=alloc.(ch).(sb)inifa<>0then(letlevels=table.(sb).(a)inletc0,c1,c2=read_threeblevelsin(* below the bound, this channel's; above, both channels',
* each with its scalefactor *)lettargets=ifsb<boundthen[ch]elseList.initnchFun.idinList.iter(func->letscale=sf.(c).(sb).(gr/4)inList.iteri(funkcode->out.(c).((((3*gr)+k)*32)+sb)<-fractionlevelscode*.scale)[c0;c1;c2])targets)donedonedone;out