~/bend-docscommunity

src/mp3_dec.bend checks

raw source on the hub · import emerging-ezaudio@0.7.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 329 · raw

Data

main_data_begin and the bits after the side-info preamble

type Sd source · line 350 · raw

Data

channels read so far

type Gs source · line 371 · raw

Data

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

type Nd source · line 397 · 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 420 · raw

Data

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

type Pq source · line 461 · raw

Data

two lines from one big-values code

type Bv source · line 487 · 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 491 · raw

Data

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

type Qd source · line 583 · raw

Data

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

type Gl source · line 667 · raw

Data

a granule channel's quantized lines and its global_gain

type Gp source · line 847 · raw

Data

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

type St source · line 888 · 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 216 · raw

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

the side info's last three bits (preflag, scalefac_scale, count1 table) close a long granule; tabs packs the three 5-bit table selects

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 242 · raw

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

def side.long source · line 252 · raw

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

def side.sw source · line 255 · 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 269 · raw

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

def side.comp source · line 272 · raw

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

def side.gain source · line 275 · raw

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

def side.win source · line 278 · raw

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

def side.ch source · line 282 · 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 286 · raw

@g:Gd -> U32

part2_3_length of one granule channel

def gd.both source · line 291 · raw

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

def gd.u4 source · line 298 · raw

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

def gd.use source · line 306 · 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 313 · raw

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

every granule channel is one this decoder reads

def gs.bits source · line 321 · raw

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

the main-data bits every granule channel claims

def side.priv source · line 333 · raw

@+nch:U32 -> U32

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

def side.op source · line 340 · raw

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

def side.open source · line 346 · raw

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

9-bit main_data_begin, then the private bits and scfsi

def side.step2 source · line 353 · raw

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

def side.step source · line 358 · raw

@st:Sd -> Sd

def side.nch source · line 363 · raw

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

def gs.of source · line 374 · raw

@st:Sd -> Gs

def fr.n source · line 380 · raw

@+nch:U32 -> Nat

how many granule channels a frame carries

def side.bytes source · line 388 · raw

@+nch:U32 -> U32

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

def nd.mk source · line 400 · raw

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

def hf.down source · line 404 · 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 408 · raw

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

def ln.signed source · line 424 · raw

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

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

def vr.sign source · line 431 · raw

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

def vr.mag source · line 437 · raw

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

a zero magnitude has no sign bit

def vr.esc source · line 444 · raw

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

def vr.read source · line 449 · raw

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

def vr.of source · line 457 · 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 464 · raw

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

def pq.y source · line 469 · raw

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

def pq.leaf source · line 475 · 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 482 · 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 495 · raw

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

the widths of the first hop bands, summed

def rg.of source · line 505 · 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 511 · raw

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

def rg.base source · line 522 · raw

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

def rg.lin source · line 533 · raw

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

def rg.b1 source · line 544 · raw

@rg:Rg -> U32

def rg.b2 source · line 549 · raw

@rg:Rg -> U32

def bv.put source · line 554 · raw

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

def bv.pair source · line 559 · 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 570 · raw

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

def bv.go source · line 575 · raw

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

def c1.val source · line 587 · 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 594 · raw

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

def qd.c source · line 599 · raw

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

def qd.b source · line 604 · raw

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

def qd.a source · line 609 · raw

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

def qd.of source · line 615 · 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 620 · 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 627 · raw

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

def qd.read source · line 631 · 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 634 · raw

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

def c1.keep source · line 638 · 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 647 · raw

@got:Qd -> U32

def c1.step source · line 652 · raw

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

def c1.go source · line 655 · raw

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

def ln.onto source · line 671 · 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 678 · raw

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

def gc.tab source · line 683 · raw

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

def gc.out source · line 692 · raw

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

def gc.c1 source · line 700 · 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 706 · raw

@lib:Lib -> Tb

def lib.pow source · line 711 · raw

@lib:Lib -> Tb

def gc.read source · line 717 · 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 723 · raw

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

def gl.rev source · line 734 · raw

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

def gl.go source · line 742 · 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 749 · raw

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

def gl.side source · line 756 · raw

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

def gl.open source · line 762 · raw

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

def gl.avail source · line 768 · 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 775 · 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 779 · raw

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

def pw.poly source · line 783 · raw

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

def pw.at source · line 786 · raw

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

def pw.out source · line 789 · raw

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

def pw.apply source · line 792 · raw

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

def pw.shift source · line 796 · raw

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

def pw.mult source · line 803 · raw

@mid:Bool -> F32

def pw.mul source · line 810 · raw

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

def pw.pick source · line 813 · raw

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

def pw.of source · line 821 · raw

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

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

def rq.neg source · line 824 · raw

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

def rq.zero source · line 831 · raw

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

def rq.push source · line 839 · 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 850 · raw

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

def gp.go source · line 855 · raw

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

def ls.hd source · line 864 · raw

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

def ls.tl source · line 871 · raw

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

def pcm.push source · line 879 · 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 895 · raw

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

zero overlap for one or two channels

def st.fresh source · line 903 · raw

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

a decoder before its first frame

def pc.pcm source · line 906 · raw

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

def pc.ov source · line 911 · raw

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

def pc.qmf source · line 916 · raw

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

def st.gran source · line 922 · raw

@+pc:0x17b1d81789c1d8452301453a7acd4c81/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 932 · 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 939 · raw

@st:St -> Lib

def st.nch source · line 944 · raw

@st:St -> U32

def st.one source · line 949 · raw

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

def st.first source · line 952 · raw

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

def st.two source · line 958 · raw

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

granule 1 after granule 0

def st.live source · line 963 · raw

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

def pay.z source · line 971 · raw

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

payload after the 4-byte header is entirely zero

def zz.push source · line 980 · raw

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

def st.quiet source · line 988 · 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 993 · raw

@st:St -> U32

def dec.alt source · line 998 · raw

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

def dec.frame source · line 1007 · 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.