~/bend-docscommunity

src/mp3_dec.bend checks

raw source on the hub · import emerging-ezaudio@0.6.0.0/src/mp3_dec.bend as Mp3_dec

MPEG-1 Layer III decoder: side info, Huffman big values and count1 quadruples, requantization, and one frame into the synthesis filterbank. Long blocks with no scale-factor bits and no bit reservoir. Layer I and II never reach here.

4 imports
import Base
import ./mp3_tab.bend as Tab
import ./mp3_enc.bend as Enc
import ./mp3_syn.bend as Syn

Types

type Tk source · line 36 · raw

Data

value plus the bits not consumed

type Tb source · line 95 · raw

Data

a read-only table as a balanced binary tree, so a lookup walks its depth, not the list

type Lib source · line 162 · raw

Data

tables shared by every frame: the Huffman codebooks, 4/3 powers, codebook offsets and linbits, the two count1 codebooks, and the scale-factor band widths of one rate

type Gd source · line 208 · raw

Data

one granule channel, long blocks only. block 0 is long.

type Op source · line 330 · raw

Data

main_data_begin and the bits after the side-info preamble

type Sd source · line 351 · raw

Data

channels read so far

type Gs source · line 372 · raw

Data

granule channels, in order, and the main-data bits

type Nd source · line 398 · raw

Data

Huffman leaf search: more while the leaf still points into the tree, the leaf, how wide the next peek is, the bits, and the bits used so far in this granule channel

type Vr source · line 423 · raw

Data

one line read: its signed value as a U32, the bits after it, the bits used

type Pq source · line 464 · raw

Data

two lines from one big-values code

type Bv source · line 490 · raw

Data

a granule channel's walk: more while count1 may go on, the bits, bits used, the next line, and the lines so far, newest first

type Rg source · line 494 · raw

Data

where regions 1 and 2 start, in lines, and each region's codebook offset and linbits

type Qd source · line 586 · raw

Data

four count1 lines: v, w, x, y, then the bits and bits used

type Gl source · line 672 · raw

Data

a granule channel's quantized lines and its global_gain

type Gp source · line 852 · raw

Data

a granule's spectrum, channel after channel, and the granule channels after it

type St source · line 893 · raw

Data

decoder state: rate, channels, overlap, polyphase delay, 1 while both are still zero, the tables, and the PCM words so far, newest first

Definitions

def u.nth source · line 9 · raw

@xs:List<&2, U32> -> @+ii:U32 -> U32

def f.word source · line 12 · raw

@x:F32 -> U32

def se.or source · line 15 · raw

@neg:Bool -> @+w:U32 -> U32

def se.u16 source · line 22 · raw

@+w:U32 -> U32

def asr.fill source · line 25 · raw

@neg:Bool -> @+v:U32 -> U32

def asr.u3 source · line 32 · raw

@+w:U32 -> U32

def tk.go source · line 40 · raw

@hop:Nat -> @xs:List<&2, U32> -> @+acc:U32 -> Tk

read hop bits, most significant first. Past the end the bits read as zero.

def tk.drop source · line 49 · raw

@hop:Nat -> @xs:List<&2, U32> -> List<&2, U32>

def tk.v source · line 58 · raw

@t:Tk -> U32

def peek.at source · line 63 · raw

@+bits:List<&2, U32> -> @+n:U32 -> U32

def bits.pref source · line 66 · raw

@hop:Nat -> @+w:U32 -> @rest:List<&2, U32> -> List<&2, U32>

def bits.of source · line 73 · raw

@xs:List<&2, U32> -> List<&2, U32>

def rd.u source · line 80 · raw

@t:Tk -> U32

def rd.xs source · line 83 · raw

@t:Tk -> List<&2, U32>

def rd.n source · line 88 · raw

@bits:List<&2, U32> -> @+n:U32 -> Tk

def side.skip source · line 91 · raw

@bits:List<&2, U32> -> @+n:U32 -> List<&2, U32>

def tb.leaves source · line 99 · raw

@xs:List<&2, U32> -> List<&2, Tb>

def tb.pairs source · line 107 · raw

@xs:List<&2, Tb> -> List<&2, Tb>

join neighbours. An odd last subtree pairs with a zero leaf.

def tb.up source · line 116 · raw

@hop:Nat -> @xs:List<&2, Tb> -> List<&2, Tb>

def tb.root source · line 123 · raw

@xs:List<&2, Tb> -> Tb

def tb.of source · line 131 · raw

@+depth:Nat -> @xs:List<&2, U32> -> Tb

a table of at most 2^depth words, word i at the leaf its depth low bits of i name

def tb.pick source · line 134 · raw

@up:Bool -> @lo:Tb -> @hi:Tb -> Tb

def tb.val source · line 141 · raw

@node:Tb -> U32

def tb.at source · line 149 · raw

@hop:Nat -> @node:Tb -> @+ii:U32 -> U32

word ii of a depth-hop table. Past its words a lookup is 0.

def bd.drop source · line 169 · raw

@hop:Nat -> @xs:List<&2, U32> -> List<&2, U32>

drop a prefix of a byte list

def bd.take source · line 179 · raw

@hop:Nat -> @xs:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>

take a prefix of a byte list

def scf.off source · line 189 · raw

@+hz:U32 -> U32

scale-factor band row. 44100 is 0, 48000 is 22, 32000 is 44.

def scf.row source · line 199 · raw

@+hz:U32 -> List<&2, U32>

the 22 long-block scale-factor band widths, in lines, at one sampling frequency

def lib.of source · line 203 · raw

@+hz:U32 -> Lib

the tables for one sampling frequency

def side.tail source · line 214 · raw

@+part:U32 -> @+big:U32 -> @+gain:U32 -> @+comp:U32 -> @+t0:U32 -> @+t1:U32 -> @+t2:U32 -> @+r0:U32 -> @+r1:U32 -> @tail:Tk -> Pair(Gd, List<&2, U32>)

def side.r1 source · line 231 · raw

@+part:U32 -> @+big:U32 -> @+gain:U32 -> @+comp:U32 -> @+tabs:U32 -> @+r0:U32 -> @+r1:Tk -> Pair(Gd, List<&2, U32>)

def side.r0 source · line 243 · raw

@+part:U32 -> @+big:U32 -> @+gain:U32 -> @+comp:U32 -> @+tabs:U32 -> @+r0:Tk -> Pair(Gd, List<&2, U32>)

def side.long source · line 253 · raw

@+part:U32 -> @+big:U32 -> @+gain:U32 -> @+comp:U32 -> @+tabs:Tk -> Pair(Gd, List<&2, U32>)

def side.sw source · line 256 · raw

@longb:Bool -> @+part:U32 -> @+big:U32 -> @+gain:U32 -> @+comp:U32 -> @bits:List<&2, U32> -> Pair(Gd, List<&2, U32>)

def side.bit source · line 270 · raw

@+part:U32 -> @+big:U32 -> @+gain:U32 -> @+comp:U32 -> @+win:Tk -> Pair(Gd, List<&2, U32>)

def side.comp source · line 273 · raw

@+part:U32 -> @+big:U32 -> @+gain:U32 -> @+comp:Tk -> Pair(Gd, List<&2, U32>)

def side.gain source · line 276 · raw

@+part:U32 -> @+big:U32 -> @+gain:Tk -> Pair(Gd, List<&2, U32>)

def side.win source · line 279 · raw

@part:Tk -> @+big:Tk -> Pair(Gd, List<&2, U32>)

def side.ch source · line 283 · raw

@+bits:List<&2, U32> -> Pair(Gd, List<&2, U32>)

side info for one granule channel. Window switching sets block to 2.

def g.part source · line 287 · raw

@g:Gd -> U32

part2_3_length of one granule channel

def gd.both source · line 292 · raw

@aa:Bool -> @bb:Bool -> Bool

def gd.u4 source · line 299 · raw

@ok:Bool -> @+big:U32 -> Bool

def gd.use source · line 307 · raw

@g:Gd -> Bool

long block, no scale-factor bits, no preflag or scale, big_values inside the 576 lines

def gs.ok source · line 314 · raw

@gs:List<&2, Gd> -> @ok:Bool -> Bool

every granule channel is one this decoder reads

def gs.bits source · line 322 · raw

@gs:List<&2, Gd> -> @+acc:U32 -> U32

the main-data bits every granule channel claims

def side.priv source · line 334 · raw

@+nch:U32 -> U32

private bits plus scfsi. mono is 5+4, stereo is 3+8.

def side.op source · line 341 · raw

@t:Tk -> @+nch:U32 -> Op

def side.open source · line 347 · raw

@bits:List<&2, U32> -> @+nch:U32 -> Op

9-bit main_data_begin, then the private bits and scfsi

def side.step2 source · line 354 · raw

@got:Pair(Gd, List<&2, U32>) -> @acc:List<&2, Gd> -> Sd

def side.step source · line 359 · raw

@st:Sd -> Sd

def side.nch source · line 364 · raw

@hop:Nat -> @st:Sd -> Sd

def gs.of source · line 375 · raw

@st:Sd -> Gs

def fr.n source · line 381 · raw

@+nch:U32 -> Nat

how many granule channels a frame carries

def side.bytes source · line 389 · raw

@+nch:U32 -> U32

side-info bytes. MPEG-1 mono is 17, stereo is 32.

def nd.mk source · line 401 · raw

@+leaf:U32 -> @+w:U32 -> @bits:List<&2, U32> -> @+used:U32 -> Nd

def hf.down source · line 405 · raw

@+leaf:U32 -> @+bits:List<&2, U32> -> @+used:U32 -> @+base:U32 -> @+huff:Tb -> Nd

the peeked bits are flushed. Index the codebook with the next (leaf & 7) bits.

def hf.go source · line 409 · raw

@hop:Nat -> @st:Nd -> @+base:U32 -> @+huff:Tb -> Nd

def ln.signed source · line 427 · raw

@+mag:U32 -> @+sign:U32 -> U32

a line from its magnitude and sign bit, two's complement

def vr.sign source · line 434 · raw

@+mag:U32 -> @t:Tk -> @+used:U32 -> Vr

def vr.mag source · line 440 · raw

@zero:Bool -> @+mag:U32 -> @bits:List<&2, U32> -> @+used:U32 -> Vr

a zero magnitude has no sign bit

def vr.esc source · line 447 · raw

@+lsb:U32 -> @t:Tk -> @+lin:U32 -> @+used:U32 -> Vr

def vr.read source · line 452 · raw

@esc:Bool -> @+lsb:U32 -> @bits:List<&2, U32> -> @+lin:U32 -> @+used:U32 -> Vr

def vr.of source · line 460 · raw

@+lsb:U32 -> @bits:List<&2, U32> -> @+lin:U32 -> @+used:U32 -> Vr

a 15 in a table with linbits is followed by linbits more magnitude bits

def pq.both source · line 467 · raw

@+xv:U32 -> @got:Vr -> Pq

def pq.y source · line 472 · raw

@+leaf:U32 -> @got:Vr -> @+lin:U32 -> Pq

def pq.leaf source · line 478 · raw

@nd:Nd -> @+lin:U32 -> Pq

a terminal leaf: code length in bits 8..14, x in bits 0..3, y in bits 4..7

def hf.pair source · line 485 · raw

@+bits:List<&2, U32> -> @+used:U32 -> @+base:U32 -> @+lin:U32 -> @+huff:Tb -> Pq

one big-values pair. base is the codebook's offset in the Huffman table.

def wd.sum source · line 498 · raw

@hop:Nat -> @xs:List<&2, U32> -> @+acc:U32 -> U32

the widths of the first hop bands, summed

def rg.of source · line 508 · raw

@+t0:U32 -> @+t1:U32 -> @+t2:U32 -> @+r0:U32 -> @+r1:U32 -> @+lib:Lib -> Rg

region 0 is r0 + 1 bands, region 1 is r1 + 1 more, region 2 the rest

def rg.reg source · line 514 · raw

@lt1:Bool -> @lt2:Bool -> U32

def rg.base source · line 525 · raw

@rg:Rg -> @+reg:U32 -> U32

def rg.lin source · line 536 · raw

@rg:Rg -> @+reg:U32 -> U32

def rg.b1 source · line 547 · raw

@rg:Rg -> U32

def rg.b2 source · line 552 · raw

@rg:Rg -> U32

def bv.put source · line 557 · raw

@got:Pq -> @+at:U32 -> @acc:List<&2, U32> -> Bv

def bv.pair source · line 562 · raw

@+reg:U32 -> @bits:List<&2, U32> -> @+used:U32 -> @+at:U32 -> @acc:List<&2, U32> -> @+rg:Rg -> @+huff:Tb -> Bv

def bv.step source · line 573 · raw

@st:Bv -> @+rg:Rg -> @+huff:Tb -> Bv

def bv.go source · line 578 · raw

@hop:Nat -> @st:Bv -> @+rg:Rg -> @+huff:Tb -> Bv

def c1.val source · line 590 · raw

@on:Bool -> @bits:List<&2, U32> -> @+used:U32 -> Vr

a count1 value is 0, or 1 with a sign bit

def qd.d source · line 597 · raw

@+va:U32 -> @+vb:U32 -> @+vc:U32 -> @got:Vr -> Qd

def qd.c source · line 602 · raw

@+leaf:U32 -> @+va:U32 -> @+vb:U32 -> @got:Vr -> Qd

def qd.b source · line 607 · raw

@+leaf:U32 -> @+va:U32 -> @got:Vr -> Qd

def qd.a source · line 612 · raw

@+leaf:U32 -> @got:Vr -> Qd

def qd.of source · line 618 · raw

@+leaf:U32 -> @bits:List<&2, U32> -> @+used:U32 -> Qd

a count1 leaf: code length in bits 0..2, which of v w x y are 1 in bits 7..4

def c1.second source · line 623 · raw

@done:Bool -> @+leaf:U32 -> @+bits:List<&2, U32> -> @+tab:List<&2, U32> -> U32

a leaf without bit 3 points to a second level indexed by (leaf & 3) more bits

def c1.leaf source · line 630 · raw

@+leaf:U32 -> @+bits:List<&2, U32> -> @+tab:List<&2, U32> -> U32

def qd.read source · line 634 · raw

@+bits:List<&2, U32> -> @+used:U32 -> @+tab:List<&2, U32> -> Qd

one count1 quadruple, Huffman table A (minimp3 tab32) or B (tab33)

def c1.more source · line 637 · raw

@+used:U32 -> @+part:U32 -> @+at:U32 -> Bool

def c1.keep source · line 641 · raw

@over:Bool -> @got:Qd -> @bits:List<&2, U32> -> @+used:U32 -> @+at:U32 -> @acc:List<&2, U32> -> @+part:U32 -> Bv

a quadruple that runs past part2_3_length is discarded and ends the region

def c1.used source · line 650 · raw

@got:Qd -> U32

def c1.step source · line 655 · raw

@+got:Qd -> @bits:List<&2, U32> -> @+used:U32 -> @+at:U32 -> @acc:List<&2, U32> -> @+part:U32 -> Bv

def c1.go source · line 658 · raw

@hop:Nat -> @st:Bv -> @+part:U32 -> @+tab:List<&2, U32> -> Bv

def ln.onto source · line 676 · raw

@xs:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>

reverse the lines read onto the zeros that fill the granule to 576

def gc.lines source · line 683 · raw

@st:Bv -> List<&2, U32>

def gc.tab source · line 688 · raw

@+lib:Lib -> @+c1:U32 -> List<&2, U32>

def gc.out source · line 697 · raw

@over:Bool -> @+gain:U32 -> @st:Bv -> Maybe<&2, Gl>

def gc.c1 source · line 705 · raw

@st:Bv -> @+gain:U32 -> @+part:U32 -> @+tab:List<&2, U32> -> Maybe<&2, Gl>

count1 runs from the end of big values while bits are left and line 572 is not passed

def lib.huff source · line 711 · raw

@lib:Lib -> Tb

def lib.pow source · line 716 · raw

@lib:Lib -> Tb

def gc.read source · line 722 · raw

@g:Gd -> @bits:List<&2, U32> -> @+lib:Lib -> Maybe<&2, Gl>

one granule channel: big values, then count1. none when big values read past part2_3_length.

def gl.add source · line 728 · raw

@acc:Maybe<&2, List<&2, Gl>> -> @got:Maybe<&2, Gl> -> Maybe<&2, List<&2, Gl>>

def gl.rev source · line 739 · raw

@got:Maybe<&2, List<&2, Gl>> -> Maybe<&2, List<&2, Gl>>

def gl.go source · line 747 · raw

@gs:List<&2, Gd> -> @+bits:List<&2, U32> -> @+lib:Lib -> @acc:Maybe<&2, List<&2, Gl>> -> Maybe<&2, List<&2, Gl>>

each granule channel starts part2_3_length bits after the one before

def gl.when source · line 754 · raw

@ok:Bool -> @+gs:List<&2, Gd> -> @bits:List<&2, U32> -> @+lib:Lib -> Maybe<&2, List<&2, Gl>>

def gl.side source · line 761 · raw

@gg:Gs -> @+begin:U32 -> @+avail:U32 -> @+lib:Lib -> Maybe<&2, List<&2, Gl>>

def gl.open source · line 767 · raw

@op:Op -> @+nch:U32 -> @+avail:U32 -> @+lib:Lib -> Maybe<&2, List<&2, Gl>>

def gl.avail source · line 773 · raw

@+size:U32 -> @+nch:U32 -> @+crc:U32 -> U32

main-data bits after the header, the CRC, and the side info

def dec.gls source · line 780 · raw

@+frame:List<&2, U32> -> @+nch:U32 -> @+crc:U32 -> @+lib:Lib -> Maybe<&2, List<&2, Gl>>

every granule channel's lines, in bitstream order, or none when the frame holds what this decoder does not read: a bit reservoir, window switching, scale-factor bits, preflag, scale-factor scale, more than 288 big values, main data past the frame, or big values past part2_3_length. crc 1 means 16 CRC bits follow the header.

def pw.f source · line 784 · raw

@+low:U32 -> @+sign:U32 -> @+x:U32 -> F32

def pw.poly source · line 788 · raw

@+low:U32 -> @+sign:U32 -> @+x:U32 -> F32

def pw.at source · line 791 · raw

@+pows:Tb -> @+ii:U32 -> F32

def pw.out source · line 794 · raw

@+x:U32 -> @+low:U32 -> @+idx:U32 -> @+sign:U32 -> @+pows:Tb -> @+mult:F32 -> F32

def pw.apply source · line 797 · raw

@+x:U32 -> @+pows:Tb -> @+mult:F32 -> F32

def pw.shift source · line 801 · raw

@mid:Bool -> @+x:U32 -> U32

def pw.mult source · line 808 · raw

@mid:Bool -> F32

def pw.mul source · line 815 · raw

@+mid:Bool -> @+x:U32 -> @+pows:Tb -> F32

def pw.pick source · line 818 · raw

@small:Bool -> @+x:U32 -> @+pows:Tb -> F32

def pw.of source · line 826 · raw

@+x:U32 -> @+pows:Tb -> F32

|x| ^ (4/3), minimp3's table below 129 and its polynomial above

def rq.neg source · line 829 · raw

@neg:Bool -> @+line:U32 -> @+gain:F32 -> @+pows:Tb -> F32

def rq.zero source · line 836 · raw

@zero:Bool -> @+line:U32 -> @+gain:F32 -> @+pows:Tb -> F32

def rq.push source · line 844 · raw

@xs:List<&2, U32> -> @+gain:F32 -> @+pows:Tb -> @acc:List<&2, F32> -> List<&2, F32>

requantize lines with one gain, pushed onto acc, newest first

def gl.push source · line 855 · raw

@gl:Gl -> @+pows:Tb -> @acc:List<&2, F32> -> List<&2, F32>

def gp.go source · line 860 · raw

@hop:Nat -> @gls:List<&2, Gl> -> @+pows:Tb -> @acc:List<&2, F32> -> Gp

def ls.hd source · line 869 · raw

@xs:List<&2, F32> -> F32

def ls.tl source · line 876 · raw

@xs:List<&2, F32> -> List<&2, F32>

def pcm.push source · line 884 · raw

@hop:Nat -> @+xs:List<&2, F32> -> @acc:List<&2, U32> -> List<&2, U32>

push exactly hop PCM words, newest first. The count, not the list, sets the length.

def dec.ov source · line 900 · raw

@+nch:U32 -> List<&2, F32>

zero overlap for one or two channels

def st.fresh source · line 908 · raw

@+hz:U32 -> @+nch:U32 -> St

a decoder before its first frame

def pc.pcm source · line 911 · raw

@pc:0xd7eee36bef55d6453644b418e1a0daff/src/mp3_syn.Pc -> List<&2, F32>

def pc.ov source · line 916 · raw

@pc:0xd7eee36bef55d6453644b418e1a0daff/src/mp3_syn.Pc -> List<&2, F32>

def pc.qmf source · line 921 · raw

@pc:0xd7eee36bef55d6453644b418e1a0daff/src/mp3_syn.Pc -> List<&2, F32>

def st.gran source · line 927 · raw

@+pc:0xd7eee36bef55d6453644b418e1a0daff/src/mp3_syn.Pc -> @+hz:U32 -> @+nch:U32 -> @+lib:Lib -> @acc:List<&2, U32> -> St

the granule's PCM is read through projections, so its count never waits on the filterbank

def st.syn source · line 937 · raw

@gp:Gp -> @st:St -> Pair(St, List<&2, Gl>)

one granule through the filterbank: the granule channels it takes, then synthesis

def st.lib source · line 944 · raw

@st:St -> Lib

def st.nch source · line 949 · raw

@st:St -> U32

def st.one source · line 954 · raw

@+st:St -> @gls:List<&2, Gl> -> Pair(St, List<&2, Gl>)

def st.first source · line 957 · raw

@got:Pair(St, List<&2, Gl>) -> St

def st.two source · line 963 · raw

@got:Pair(St, List<&2, Gl>) -> St

granule 1 after granule 0

def st.live source · line 968 · raw

@got:Maybe<&2, List<&2, Gl>> -> @+st:St -> Maybe<&2, St>

def pay.z source · line 976 · raw

@xs:List<&2, U32> -> Bool

payload after the 4-byte header is entirely zero

def zz.push source · line 985 · raw

@hop:Nat -> @acc:List<&2, U32> -> List<&2, U32>

def st.quiet source · line 993 · raw

@st:St -> St

a silent frame on a decoder whose overlap and delay are still zero is 1152 zeros a channel

def st.fr source · line 998 · raw

@st:St -> U32

def dec.alt source · line 1003 · raw

@silent:Bool -> @+frame:List<&2, U32> -> @+crc:U32 -> @+st:St -> Maybe<&2, St>

def dec.frame source · line 1012 · raw

@ok:Bool -> @+frame:List<&2, U32> -> @+crc:U32 -> @+st:St -> Maybe<&2, St>

one MPEG-1 Layer III frame whose header the caller has checked. A silent frame on a fresh decoder skips the filterbank: zero spectra on zero state synthesize to zeros.