| 1 | package Peta::MP3::Encode::Filterbank::PDL; | ||||||||||
| 2 | # ABSTRACT: Layer III's filterbank with PDL | ||||||||||
| 3 | |||||||||||
| 4 | # The filterbank of Peta::MP3::Encode::Filterbank in PDL: the same | ||||||||||
| 5 | # arithmetic, as a handful of whole-array operations per granule. | ||||||||||
| 6 | # | ||||||||||
| 7 | # windowed sums the samples gathered by an index of (8, 64, 18), times | ||||||||||
| 8 | # the window, summed over the 8 | ||||||||||
| 9 | # matrixing a matrix product: (64, 18) times (32, 64) | ||||||||||
| 10 | # MDCT a matrix product per window: the 36 inputs of the 32 | ||||||||||
| 11 | # subbands times the window's (18, 36), or the three short | ||||||||||
| 12 | # windows' (6, 12) | ||||||||||
| 13 | # butterflies two slices of 8 lines by 31 subbands | ||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | # PDL exports dozens of functions into a package. They go to a package of | ||||||||||
| 18 | # their own, and this one calls PDL by name. | ||||||||||
| 19 | { | ||||||||||
| 20 | package Peta::MP3::Encode::Filterbank::PDL::Imports; | ||||||||||
| 21 | 1 | ▶ | use PDL::LiteF; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | use Peta::MP3::Encode::Filterbank; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 1 | ▶ | use Peta::MP3::Encode::Tables; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 1 | our $VERSION = '0.2610180'; | |||||||||
| 28 | |||||||||||
| 29 | 1 | ▶ | use constant SHORT => 2; # the block type of short blocks | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | # A piddle of doubles from a list, in the dimensions given. | ||||||||||
| 32 | 1191 | ▶ | sub _pdl ($list, @dims) { return PDL->pdl(PDL::double(), $list)->reshape(@dims) } | ||||||||
|
Subroutine
_pdl: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | # What every bank shares, made once, the first time a bank is made. | ||||||||||
| 35 | 1 | my %SHARED; | |||||||||
| 36 | |||||||||||
| 37 | 24 | ▶ | sub _shared () { | ||||||||
|
Subroutine
_shared: called CC=7 SCAR=19.5
| |||||||||||
| 38 | 24 | ▶ | return \%SHARED if %SHARED; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | # The windowed sums: which sample each term reads, (8, 64, 18), and the | ||||||||||
| 41 | # window it is weighed by, (8, 64). | ||||||||||
| 42 | 1 | my (@index, @window); | |||||||||
| 43 | 1 | for my $t (0 .. 17) { | |||||||||
| 44 | 18 | for my $i (0 .. 63) { | |||||||||
| 45 | 1152 | for my $j (0 .. 7) { | |||||||||
| 46 | 9216 | push @index, 511 + 32 * $t - $i - 64 * $j; | |||||||||
| 47 | 9216 | ▶ | push @window, $Peta::MP3::Encode::Tables::WINDOW[ $i + 64 * $j ] if $t == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | } | ||||||||||
| 51 | 1 | $SHARED{index} = PDL->pdl(PDL::long(), \@index)->reshape(8, 64, 18); | |||||||||
| 52 | 1 | $SHARED{window} = _pdl(\@window, 8, 64); | |||||||||
| 53 | |||||||||||
| 54 | # The matrixing (32, 64) and the subbands' signs (32, 18). | ||||||||||
| 55 | 1 | my @matrix = map { Peta::MP3::Encode::Filterbank::matrixing($_ % 32, int($_ / 32)) } 0 .. 2047; | |||||||||
| 56 | 1 | my @sign = map { Peta::MP3::Encode::Filterbank::sign(int($_ / 32), $_ % 32) } 0 .. 575; | |||||||||
| 57 | 1 | $SHARED{matrix} = _pdl(\@matrix, 32, 64); | |||||||||
| 58 | 1 | $SHARED{sign} = _pdl(\@sign, 32, 18); | |||||||||
| 59 | |||||||||||
| 60 | # The MDCTs: element (m, k) of each long window's (18, 36), and of the short (6, 12). | ||||||||||
| 61 | 1 | for my $type (0, 1, 3) { | |||||||||
| 62 | 3 | my @long = map { Peta::MP3::Encode::Filterbank::long_mdct($type, $_ % 18, int($_ / 18)) } 0 .. 647; | |||||||||
| 63 | 3 | $SHARED{long}{$type} = _pdl(\@long, 18, 36); | |||||||||
| 64 | } | ||||||||||
| 65 | 1 | my @short = map { Peta::MP3::Encode::Filterbank::short_mdct($_ % 6, int($_ / 6)) } 0 .. 71; | |||||||||
| 66 | 1 | $SHARED{short} = _pdl(\@short, 6, 12); | |||||||||
| 67 | |||||||||||
| 68 | 1 | my ($cs, $ca) = Peta::MP3::Encode::Filterbank::butterflies(); | |||||||||
| 69 | 1 | $SHARED{cs} = _pdl($cs, 8); | |||||||||
| 70 | 1 | $SHARED{ca} = _pdl($ca, 8); | |||||||||
| 71 | |||||||||||
| 72 | 1 | return \%SHARED; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 24 | ▶ | sub new ($class, $rate) { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 76 | 24 | return bless { | |||||||||
| 77 | shared => _shared(), | ||||||||||
| 78 | history => PDL->zeroes(PDL::double(), 480), | ||||||||||
| 79 | previous => PDL->zeroes(PDL::double(), 32, 18), | ||||||||||
| 80 | order => PDL->pdl(PDL::long(), Peta::MP3::Encode::Filterbank::short_order($rate)), | ||||||||||
| 81 | }, $class; | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | # The lines of the next granule of 576 samples, in a block type's window. | ||||||||||
| 85 | 1182 | ▶ | sub granule ($self, $samples, $type) { | ||||||||
|
Subroutine
granule: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 86 | 1182 | my $s = $self->{shared}; | |||||||||
| 87 | 1182 | my $buf = $self->{history}->append(_pdl($samples, 576)); | |||||||||
| 88 | 1182 | $self->{history} = $buf->slice('576:1055')->copy; | |||||||||
| 89 | |||||||||||
| 90 | # The subband samples, (32, 18): sub[sb, t]. The MDCT's inputs, (36, 32): in[k, sb]. | ||||||||||
| 91 | 1182 | my $y = ($buf->index($s->{index}) * $s->{window})->sumover; # (64, 18) | |||||||||
| 92 | 1182 | my $sub = ($y x $s->{matrix}) * $s->{sign}; | |||||||||
| 93 | 1182 | my $in = $self->{previous}->transpose->append($sub->transpose); | |||||||||
| 94 | 1182 | $self->{previous} = $sub; | |||||||||
| 95 | |||||||||||
| 96 | 1182 | ▶ | return [ $self->_short($in)->list ] if $type == SHORT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 1152 | return [ _long($in, $s, $type)->list ]; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | # A long block: the MDCT, (18, 32), line 18 sb + m; then the butterflies. | ||||||||||
| 101 | 1152 | ▶ | sub _long ($in, $s, $type) { | ||||||||
|
Subroutine
_long: called CC=1 SCAR=0.0
| |||||||||||
| 102 | 1152 | my $xr = $in x $s->{long}{$type}; | |||||||||
| 103 | 1152 | my $up = $xr->slice('17:10', '0:30'); | |||||||||
| 104 | 1152 | my $down = $xr->slice('0:7', '1:31'); | |||||||||
| 105 | 1152 | my $u = $up->copy; | |||||||||
| 106 | 1152 | my $d = $down->copy; | |||||||||
| 107 | 1152 | $up .= $u * $s->{cs} + $d * $s->{ca}; | |||||||||
| 108 | 1152 | $down .= $d * $s->{cs} - $u * $s->{ca}; | |||||||||
| 109 | 1152 | return $xr->reshape(576); | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | # A short block: three windows of 12 inputs, from 6, 12 and 18, (6, 32) | ||||||||||
| 113 | # each; side by side as (3, 6, 32), line 18 sb + 3 m + w; then in the | ||||||||||
| 114 | # bitstream's order. | ||||||||||
| 115 | 30 | ▶ | sub _short ($self, $in) { | ||||||||
|
Subroutine
_short: called CC=1 SCAR=0.0
| |||||||||||
| 116 | 30 | my $short = $self->{shared}{short}; | |||||||||
| 117 | 30 | my @windows = map { $in->slice((6 + 6 * $_) . ':' . (17 + 6 * $_)) x $short } 0 .. 2; | |||||||||
| 118 | 30 | my $mdct = PDL::cat(@windows)->mv(2, 0)->reshape(576); | |||||||||
| 119 | 30 | return $mdct->index($self->{order}); | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 1 | 1; | |||||||||
| 123 | |||||||||||
| 124 | __END__ | ||||||||||