lib/Peta/MP3/Encode/Filterbank.pm

filter: Statementstmt 100% 154 / 154 Branchbran 100% 42 / 42 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 17 / 17 Podpod 100% 11 / 11 totaltotal 99.1% 230 / 232 CC 33 SCAR 16.3
CC
33
Cov
100%
long_window
9.0
subbands
7.0
_long
6.0
CRAP 5.1
« lib/Peta/Vorbis/Encode/MDCT.pm ↑ Summary lib/Peta/Vorbis/Encode/Ogg.pm »
1 package Peta::MP3::Encode::Filterbank;
2 # ABSTRACT: samples into Layer III's frequency lines
3  
4 # Turns a channel's samples into the lines Layer III quantizes, a granule
5 # (576 samples) at a time:
6 #
7 # subbands the polyphase filterbank of ISO 11172-3: a 512-tap window and
8 # a matrixing into 32 subbands, 18 samples each
9 # mdct per subband, over the previous granule's 18 samples and these
10 # 18, in the window of the block type:
11 # 0 normal 36 samples, 18 lines
12 # 1 start long, then half of a short window
13 # 2 short three MDCTs of 12 samples, 6 lines each
14 # 3 stop the reverse of start
15 # then, in long blocks, the butterflies that reduce the
16 # aliasing between neighbouring subbands
17 #
18 # Long blocks come out in frequency order, line 18 sb + k. Short blocks
19 # come out in the order the bitstream has them: band by band, in each band
20 # window 0, 1, 2, each window's lines in frequency order.
21 #
22 # The functions after new() define the filterbank's arithmetic; the PDL
23 # filterbank builds its matrices from them too.
24 #
25 # Written for pperl's JIT. Each stage is one long loop that keeps its own
26 # counters (the JIT leaves short nested loops to the interpreter), reads
27 # lexical copies of the tables, and has no list assignments, bit operations
28 # or calls inside.
29  
30 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Peta::MP3::Encode::Tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 our $VERSION = '0.2610180';
34  
35 1 ▶use constant PI => 3.141592653589793;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant SHORT => 2; # the block type of short blocks
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 my @C = @Peta::MP3::Encode::Tables::WINDOW;
39 1 my @M = map { matrixing(int($_ / 64), $_ % 64) } 0 .. 2047; # M[64 sb + k]
40 1 my @SIGN = map { sign(int($_ / 32), $_ % 32) } 0 .. 575; # SIGN[32 t + sb]
41  
42 # Each long window's MDCT, LONG{type}[36 m + k]; the short one's, SHORT[12 m + k].
43 1 my %LONG = map { my $type = $_; ($type => [ map { long_mdct($type, int($_ / 36), $_ % 36) } 0 .. 647 ]) } 0, 1, 3;
44 1 my @SHORT = map { short_mdct(int($_ / 12), $_ % 12) } 0 .. 71;
45  
46 1 my ($CS, $CA) = butterflies();
47  
48 1 ▶sub new ($class, $rate) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
49 1 return bless {
50 history => [ (0.0) x 480 ], # the samples the window still reaches back to
51 previous => [ (0.0) x 576 ], # the last granule's subband samples
52 order => short_order($rate),
53 }, $class;
54 }
55  
56 # The matrixing of 64 windowed sums into subband $sb, at sum $k.
57 4096 ▶sub matrixing ($sb, $k) { return cos((2 * $sb + 1) * ($k - 16) * PI / 64) }
Subroutine matrixing: called CC=1 SCAR=0.0
Pod documented
58  
59 # The filterbank inverts every other subband in every other step: the sign
60 # that undoes it, for step $t of subband $sb.
61 1152 ▶sub sign ($t, $sb) { return ($sb % 2 && $t % 2) ? -1.0 : 1.0 }
Subroutine sign: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$sb % 2 && $t % 2 ? : 288 864 1152
Condition100%3/3
$sb % 2 and $t % 2
!ll&&!rl&&r
576288288
MC/DC100%2/2
$sb % 2 and $t % 2
$sb % 2 $t % 2
Truth table100%3/3
A and B
A= $sb % 2B= $t % 2
ABresult
0X0
100
111
62  
63 # A long block's window, at input $k of 36. The start and stop windows join
64 # a long half and a short one.
65 3888 ▶sub long_window ($type, $k) {
Subroutine long_window: called CC=9 SCAR=22.0
Pod documented
66 3888 my $long = sin(PI / 36 * ($k + 0.5));
67  
68 3888 ▶ return $long if $type == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 0 1296 2592 3888
69  
70 2592 ▶ if ($type == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) 1296 1296 2592
71 1296 ▶ return $long if $k < 18;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 18 648 648 1296
72 648 ▶ return 1.0 if $k < 24;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 24 216 432 648
73 432 ▶ return sin(PI / 12 * ($k + 0.5 - 18)) if $k < 30;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 30 216 216 432
74 216 return 0.0;
75 }
76  
77 1296 ▶ return 0.0 if $k < 6;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 6 216 1080 1296
78 1080 ▶ return sin(PI / 12 * ($k + 0.5 - 6)) if $k < 12;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 12 216 864 1080
79 864 ▶ return 1.0 if $k < 18;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 18 216 648 864
80 648 return $long;
81 }
82  
83 # The long MDCT through a block type's window: input $k's share of line $m.
84 3888 ▶sub long_mdct ($type, $m, $k) {
Subroutine long_mdct: called CC=1 SCAR=0.0
Pod documented
85 3888 return long_window($type, $k) * cos(PI / 72 * (2 * $k + 1 + 18) * (2 * $m + 1)) / 9;
86 }
87  
88 # The short MDCT through its window: input $k's share of line $m (of 6).
89 144 ▶sub short_mdct ($m, $k) {
Subroutine short_mdct: called CC=1 SCAR=0.0
Pod documented
90 144 return sin(PI / 12 * ($k + 0.5)) * cos(PI / 24 * (2 * $k + 1 + 6) * (2 * $m + 1)) / 3;
91 }
92  
93 # The aliasing butterflies' coefficients (Table B.9): (\@cs, \@ca).
94 2 ▶sub butterflies () {
Subroutine butterflies: called CC=1 SCAR=0.0
Pod documented
95 2 my @c = (-0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037);
96 2 return ([ map { 1 / sqrt(1 + $_ * $_) } @c ], [ map { $_ / sqrt(1 + $_ * $_) } @c ]);
97 }
98  
99 # Where each line of a short block, in the bitstream's order, is found among
100 # the MDCT's lines: line 6 sb + m of window w is MDCT line 18 sb + 3 m + w.
101 25 ▶sub short_order ($rate) {
Subroutine short_order: called CC=5 SCAR=16.1
Pod documented
102 25 ▶ my $bands = $Peta::MP3::Encode::Tables::BANDS{$rate} // die "no scalefactor bands for $rate Hz\n";
Condition50%1/2
$Peta::MP3::Encode::Tables::BANDS{$rate} // die("no scalefactor bands for $rate Hz\n")
l!l
250
MC/DC0%0/1
$Peta::MP3::Encode::Tables::BANDS{$rate} // die("no scalefactor bands for $rate Hz\n")
$Peta::MP3::Encode::Tables::BANDS{$rate}
Truth table50%1/2
A
A= $Peta::MP3::Encode::Tables::BANDS{$rate}
Aresult
11
00
103 25 my @short = @{ $bands->{short} };
104 25 my @order;
105  
106 25 for my $b (0 .. $#short - 1) {
107 325 for my $w (0 .. 2) {
108 975 for my $f ($short[$b] .. $short[$b + 1] - 1) {
109 14400 push @order, 18 * int($f / 6) + 3 * ($f % 6) + $w;
110 }
111 }
112 }
113 25 return \@order;
114 }
115  
116 # The lines of the next granule of 576 samples, in a block type's window.
117 38 ▶sub granule ($self, $samples, $type) {
Subroutine granule: called CC=1 SCAR=0.0
Pod documented
118 38 my $sub = $self->subbands($samples);
119 38 my $xr = $self->mdct($self->{previous}, $sub, $type);
120 38 $self->{previous} = $sub;
121 38 return $xr;
122 }
123  
124 # A granule of 576 samples into 18 samples of each of the 32 subbands:
125 # sub[32 t + sb].
126 38 ▶sub subbands ($self, $new) {
Subroutine subbands: called CC=7 SCAR=19.5
Pod documented
127 38 my @buf = (@{ $self->{history} }, @$new); # 1056 samples, oldest first
128 38 my @C = @C;
129 38 my @M = @M;
130 38 my @SIGN = @SIGN;
131  
132 # The windowed sums, 64 for each of the 18 steps: y[64 t + i].
133 38 my @y = (0.0) x 1152;
134 38 my ($t, $i, $j, $sum) = (0, 0, 0, 0.0);
135 38 for my $n (0 .. 9215) {
136 350208 $sum += $C[ $i + 64 * $j ] * $buf[ 511 + 32 * $t - $i - 64 * $j ];
137 350208 $j++;
138 350208 ▶ if ($j == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 8) 43776 306432 350208
139 43776 $y[ 64 * $t + $i ] = $sum;
140 43776 $j = 0;
141 43776 $sum = 0.0;
142 43776 $i++;
143 43776 ▶ if ($i == 64) { $i = 0; $t++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 64) 684 43092 43776
144 }
145 }
146  
147 # The matrixing.
148 38 my @sub = (0.0) x 576;
149 38 my ($sb, $k) = (0, 0);
150 38 $t = 0;
151 38 for my $n (0 .. 36863) {
152 1400832 $sum += $M[ 64 * $sb + $k ] * $y[ 64 * $t + $k ];
153 1400832 $k++;
154 1400832 ▶ if ($k == 64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 64) 21888 1378944 1400832
155 21888 $sub[ 32 * $t + $sb ] = $sum * $SIGN[ 32 * $t + $sb ];
156 21888 $k = 0;
157 21888 $sum = 0.0;
158 21888 $sb++;
159 21888 ▶ if ($sb == 32) { $sb = 0; $t++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($sb == 32) 684 21204 21888
160 }
161 }
162  
163 38 $self->{history} = [ @buf[ 576 .. 1055 ] ];
164 38 return \@sub;
165 }
166  
167 # The lines of a granule, from its subband samples and the previous
168 # granule's, in a block type's window.
169 38 ▶sub mdct ($self, $previous, $current, $type) {
Subroutine mdct: called CC=4 SCAR=13.9
Pod documented
170 # The 36 inputs of every subband side by side: in[36 sb + k].
171 38 my @in = (0.0) x 1152;
172 38 my ($sb, $k) = (0, 0);
173 38 for my $n (0 .. 575) {
174 21888 $in[ 36 * $sb + $k ] = $previous->[ 32 * $k + $sb ];
175 21888 $in[ 36 * $sb + $k + 18 ] = $current->[ 32 * $k + $sb ];
176 21888 $k++;
177 21888 ▶ if ($k == 18) { $k = 0; $sb++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 18) 1216 20672 21888
178 }
179  
180 38 ▶ return $self->_short(\@in) if $type == SHORT;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 2 7 31 38
181 31 return _long(\@in, $LONG{$type});
182 }
183  
184 # A long block: the MDCT of every subband, then the butterflies.
185 31 ▶sub _long ($inref, $wref) {
Subroutine _long: called CC=6 SCAR=17.9
186 31 my @in = @$inref;
187 31 my @W = @$wref;
188 31 my @xr = (0.0) x 576;
189 31 my ($sb, $m, $k, $sum) = (0, 0, 0, 0.0);
190 31 for my $n (0 .. 20735) {
191 642816 $sum += $W[ 36 * $m + $k ] * $in[ 36 * $sb + $k ];
192 642816 $k++;
193 642816 ▶ if ($k == 36) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 36) 17856 624960 642816
194 17856 $xr[ 18 * $sb + $m ] = $sum;
195 17856 $k = 0;
196 17856 $sum = 0.0;
197 17856 $m++;
198 17856 ▶ if ($m == 18) { $m = 0; $sb++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($m == 18) 992 16864 17856
199 }
200 }
201  
202 # The butterflies between each subband and the next, 8 pairs of lines
203 # about each boundary.
204 31 my @CS = @$CS;
205 31 my @CA = @$CA;
206 31 my $i = 0;
207 31 $sb = 1;
208 31 for my $n (0 .. 247) {
209 7688 my $up = 18 * $sb - 1 - $i;
210 7688 my $down = 18 * $sb + $i;
211 7688 my $bu = $xr[$up];
212 7688 my $bd = $xr[$down];
213 7688 $xr[$up] = $bu * $CS[$i] + $bd * $CA[$i];
214 7688 $xr[$down] = $bd * $CS[$i] - $bu * $CA[$i];
215 7688 $i++;
216 7688 ▶ if ($i == 8) { $i = 0; $sb++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 8) 961 6727 7688
217 }
218 31 return \@xr;
219 }
220  
221 # A short block: three MDCTs per subband, over inputs 6 .. 17, 12 .. 23 and
222 # 18 .. 29; then the lines in the bitstream's order.
223 7 ▶sub _short ($self, $inref) {
Subroutine _short: called CC=5 SCAR=16.1
224 7 my @in = @$inref;
225 7 my @S = @SHORT;
226 7 my @mdct = (0.0) x 576;
227 7 my ($sb, $w, $m, $k, $sum) = (0, 0, 0, 0, 0.0);
228 7 for my $n (0 .. 6911) {
229 48384 $sum += $S[ 12 * $m + $k ] * $in[ 36 * $sb + 6 + 6 * $w + $k ];
230 48384 $k++;
231 48384 ▶ if ($k == 12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 12) 4032 44352 48384
232 4032 $mdct[ 18 * $sb + 3 * $m + $w ] = $sum;
233 4032 $k = 0;
234 4032 $sum = 0.0;
235 4032 $m++;
236 4032 ▶ if ($m == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m == 6) 672 3360 4032
237 672 $m = 0;
238 672 $w++;
239 672 ▶ if ($w == 3) { $w = 0; $sb++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($w == 3) 224 448 672
240 }
241 }
242 }
243 7 return [ @mdct[ @{ $self->{order} } ] ];
244 }
245  
246 1 1;
247  
248 __END__
« lib/Peta/Vorbis/Encode/MDCT.pm ↑ Summary lib/Peta/Vorbis/Encode/Ogg.pm »