lib/Peta/MP3/Encode/Quantize.pm

filter: Statementstmt 97.8% 464 / 474 Branchbran 95.6% 283 / 296 Conditioncond 91.8% 68 / 74 MC/DCmcdc 84.4% 38 / 45 Subroutinesub 100% 41 / 41 Podpod 100% 3 / 3 totaltotal 96.1% 897 / 933 CC 214 SCAR 23.5
CC
214
Cov
97%
_best_regions
19.0
_part2
17.0
_plans
16.0
CRAP 10.5
« lib/Peta/Vorbis/Encode/Psy.pm ↑ Summary lib/Peta/Synth/MIDI.pm »
1 package Peta::MP3::Encode::Quantize;
2 # ABSTRACT: a granule's lines into Layer III's integers and codes
3  
4 # Turns a granule's 576 frequency lines into integers and Huffman codes,
5 # within a number of bits and, where the bits allow, within the noise each
6 # band may take.
7 #
8 # A line x is quantized with a step 2^((q - 210) / 4) as
9 #
10 # ix = int( (|x| / step)^(3/4) + 0.4054 )
11 #
12 # where q is the global gain, less 2 (or 4, with scalefac_scale) for each
13 # step of the band's scalefactor, and in a short block less 8 for each step
14 # of the window's subblock gain. The lines come in groups that share a q:
15 # the 22 bands of a long block, or the 13 bands times 3 windows of a short
16 # one. The steps:
17 #
18 # 1. Each group's coarsest q whose noise stays within what it may take.
19 # 2. The global gain the coarsest group needs, as far as the scalefactors
20 # (and subblock gains) can make the others finer: in a long block bands
21 # 0-10 by up to 15 steps, 11-20 by 7, 21 not at all; in a short one
22 # bands 0-5 by 15, 6-11 by 7, 12 not at all, and each window by up to 7
23 # steps of subblock gain. Where a band wants finer than its scalefactor
24 # reaches, all the others are made finer with it. Then steps of 4
25 # (scalefac_scale) and, in a long block, preflag (bands 11-20 finer by
26 # the decoder's table) are tried, and the plan that takes the fewest
27 # bits wins (1-2% fewer than the coarsest global gain alone).
28 # 3. If that takes more bits than there are, a coarser global gain until
29 # it fits: all groups' noise rises together. If it takes fewer than
30 # must be used, a finer one.
31 # 4. MPEG-1, in the second granule of a frame, long blocks after long
32 # blocks: the groups of scalefactors (bands 0-5, 6-10, 11-15, 16-20)
33 # the first granule already sent are not sent again (scfsi).
34 # 5. The Huffman coding: the big values in regions, each with the table
35 # that codes it shortest (a normal long block chooses where its three
36 # regions end; the others have two, split after 36 lines, or a start or
37 # stop block's at the 8th long band's end); then the quadruples of ones
38 # and zeros; then nothing for the zeros at the top.
39 #
40 # Written for pperl's JIT: flat lexical arrays, plain loops.
41  
42 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Peta::MP3::Encode::Tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 our $VERSION = '0.2610180';
47  
48 1 ▶use constant LARGEST => 8206; # the largest value: 15 plus what 13 linbits hold
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant ROUND => 0.4054;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant SHORT => 2; # the block type of short blocks
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # A group of lines: [ LO, HI ] its lines, SF its scalefactor (-1: none),
53 # MOST that scalefactor's largest, WINDOW its short window (-1: none).
54 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 LO => 0,
56 HI => 1,
57 SF => 2,
58 MOST => 3,
59 WINDOW => 4,
60 1 };
61  
62 1 my @POW43 = map { $_**(4 / 3) } 0 .. LARGEST;
63 1 my @HUFFMAN = @Peta::MP3::Encode::Tables::HUFFMAN;
64 1 my @SLEN1 = @Peta::MP3::Encode::Tables::SLEN1;
65 1 my @SLEN2 = @Peta::MP3::Encode::Tables::SLEN2;
66 1 my @PRETAB = @Peta::MP3::Encode::Tables::PRETAB;
67 1 my @LSF_PARTS = @Peta::MP3::Encode::Tables::LSF_PARTS;
68 1 my @LSF_SLEN_MOST = @Peta::MP3::Encode::Tables::LSF_SLEN_MOST;
69  
70 # The tables without linbits, by the largest value they hold.
71 1 my @SMALL = ([1], [1], [2, 3], [5, 6], [7, 8, 9], [7, 8, 9], [10, 11, 12], [10, 11, 12], map { [13, 15] } 8 .. 15);
72  
73 # The tables with linbits, in their two families, fewest linbits first.
74 1 my @ESCAPE = ([16 .. 23], [24 .. 31]);
75  
76 # The tables with code lengths of their own; where each table finds its
77 # lengths among them; and their lengths side by side, LENGTHS[256 c + 16 x + y].
78 1 my @CODED = (1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 24);
79 1 my @CODED_AT;
80 1 $CODED_AT[ $CODED[$_] ] = $_ for 0 .. $#CODED;
81 1 $CODED_AT[$_] = $CODED_AT[16] for 17 .. 23;
82 1 $CODED_AT[$_] = $CODED_AT[24] for 25 .. 31;
83 1 my @LENGTHS = map { @{ $HUFFMAN[$_][2] } } @CODED;
84  
85 # The scalefactors scfsi shares between granules, in four groups.
86 1 my @SCFSI = ([ 0, 6 ], [ 6, 11 ], [ 11, 16 ], [ 16, 21 ]);
87  
88 15 ▶sub new ($class, $rate) {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
89 15 ▶ 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
150
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
90 15 my @long = @{ $bands->{long} };
91 15 my @short = @{ $bands->{short} };
92  
93 # Long blocks: the 22 bands.
94 15 my @long_groups;
95 15 for my $b (0 .. 21) {
96 330 ▶ my $most = $b < 11 ? 15 : $b < 21 ? 7 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$b < 11 ? : 165 165 330
$b < 21 ? : 150 15 165
97 330 ▶ push @long_groups, [ $long[$b], $long[$b + 1], $b < 21 ? $b : -1, $most, -1 ];
Branch: 1 branch
BranchTrueFalseTotal
$b < 21 ? : 315 15 330
98 }
99  
100 # Short blocks: band b, window w is group 3 b + w, its lines in the
101 # bitstream's order.
102 15 my @short_groups;
103 15 for my $b (0 .. 12) {
104 195 my $width = $short[$b + 1] - $short[$b];
105 195 ▶ my $most = $b < 6 ? 15 : $b < 12 ? 7 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$b < 6 ? : 90 105 195
$b < 12 ? : 90 15 105
106 195 for my $w (0 .. 2) {
107 585 my $lo = 3 * $short[$b] + $w * $width;
108 585 ▶ push @short_groups, [ $lo, $lo + $width, $b < 12 ? 3 * $b + $w : -1, $most, $w ];
Branch: 1 branch
BranchTrueFalseTotal
$b < 12 ? : 540 45 585
109 }
110 }
111  
112 return bless {
113 long => \@long,
114 groups => { long => \@long_groups, short => \@short_groups },
115 15 ▶ lsf => exists $Peta::MP3::Encode::Tables::LSF_RATE_CODE{$rate} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
exists $Peta::MP3::Encode::Tables::LSF_RATE_CODE{$rate} ? : 7 8 15
116 }, $class;
117 }
118  
119 # Quantizes and codes a granule. $allowed is the noise each group may take;
120 # $budget the most bits it may take, $least the fewest where it would take
121 # fewer (the bits are there, and would otherwise go unused); $type its
122 # block type. $before is the first granule's result for this channel, when
123 # this is the second: its scalefactors may be reused.
124 #
125 # Returns { ix, scalefac (as sent), preflag, subblock_gain, global_gain,
126 # scalefac_scale, scalefac_compress, block_type, big_values, count1,
127 # table_select, region0_count, region1_count, count1table, part2_length,
128 # part2_3_length, scfsi }.
129 1220 ▶sub granule ($self, $xr, $allowed, $budget, $least, $type, $before = undef) {
Subroutine granule: called CC=9 SCAR=22.0
Pod documented
130 1220 ▶ $budget = 4095 if $budget > 4095;
Branch: 1 branch
BranchTrueFalseTotal
if $budget > 4095 254 966 1220
131  
132 1220 my @a34 = (0.0) x 576; # |x|^(3/4)
133 1220 my $loud = 0.0;
134 1220 for my $i (0 .. 575) {
135 702720 my $a = abs $xr->[$i];
136 702720 $a34[$i] = $a**0.75;
137 702720 ▶ $loud = $a if $a > $loud;
Branch: 1 branch
BranchTrueFalseTotal
if $a > $loud 14585 688135 702720
138 }
139 1220 ▶ return _silent($type) if $loud < 1e-12;
Branch: 1 branch
BranchTrueFalseTotal
if $loud < "1e-12" 197 1023 1220
140  
141 1023 ▶ my $reuse = $before && $type != SHORT && $before->{block_type} != SHORT ? $before->{scalefac} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$before && $type != 2 && $before->{'block_type'} != 2 ? : 264 759 1023
Condition100%6/6
$before and $type != 2 and $before->{"block_type"} != 2
!ll&&!rl&&r
74613264
$before and $type != 2
!ll&&!rl&&r
7388277
MC/DC100%3/3
$before and $type != 2 and $before->{"block_type"} != 2
$before $type != 2 $before->{"block_type"} != 2
Truth table100%4/4
A and B and C
A= $beforeB= $type != 2C= $before->{"block_type"} != 2
ABCresult
0XX0
10X0
1100
1111
142  
143 1023 my ($want, $finest) = $self->_wanted($xr, \@a34, $allowed, $type);
144 1023 my @plans = $self->_plans($want, $finest, $type);
145 1023 my ($plan, $priced) = $self->_cheapest(\@plans, \@a34, $type, $reuse);
146  
147 1023 return $self->_fit(\@a34, $type, @$plan, $budget, $least, $reuse, $priced);
148 }
149  
150 # Step 1: each group's coarsest q within its noise, and its finest possible
151 # (no line beyond LARGEST). A group of zeros wants none.
152 1023 ▶sub _wanted ($self, $xr, $a34, $allowed, $type) {
Subroutine _wanted: called CC=9 SCAR=22.0
153 1023 my $groups = $self->_groups($type);
154 1023 my (@want, @finest);
155  
156 1023 for my $g (0 .. $#$groups) {
157 23118 my ($lo, $hi) = @{ $groups->[$g] }[ LO, HI ];
158  
159 23118 my $top = 0.0;
160 23118 for my $i ($lo .. $hi - 1) {
161 589248 my $a = abs $xr->[$i];
162 589248 ▶ $top = $a if $a > $top;
Branch: 1 branch
BranchTrueFalseTotal
if $a > $top 99815 489433 589248
163 }
164 23118 ▶ if ($top == 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top == 0) 273 22845 23118
165 273 push @want, 255;
166 273 push @finest, 0;
167 273 next;
168 }
169  
170 22845 my $low = _finest($top);
171 22845 my $high = 255;
172 22845 push @finest, $low;
173  
174 22845 ▶ if (_noise($xr, $a34, $lo, $hi, $high) <= $allowed->[$g]) { push @want, $high; next }
Branch: 2 branches
BranchTrueFalseTotal
if (_noise($xr, $a34, $lo, $hi, $high) <= $allowed->[$g]) { } 16048 6797 22845
elsif (_noise($xr, $a34, $lo, $hi, $low) > $allowed->[$g]) { } 0 6797 6797
175 0 elsif (_noise($xr, $a34, $lo, $hi, $low) > $allowed->[$g]) { push @want, $low; next }
176  
177 6797 ▶ while ($high - $low > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high - $low > 1) 49473 6797 56270
178 49473 my $mid = int(($low + $high) / 2);
179 49473 ▶ if (_noise($xr, $a34, $lo, $hi, $mid) <= $allowed->[$g]) { $low = $mid } else { $high = $mid }
Branch: 1 branch
BranchTrueFalseTotal
if (_noise($xr, $a34, $lo, $hi, $mid) <= $allowed->[$g]) { } 27949 21524 49473
180 }
181 6797 push @want, $low;
182 }
183 1023 return (\@want, \@finest);
184 }
185  
186 # Step 2: the plans worth trying, each [ global gain, scalefactors,
187 # subblock gains, scalefac_scale, the finest global gain, preflag ]. The
188 # scalefactors here are what the decoder makes of them (preflag's table
189 # added). Steps of 2 without preflag come first; the others only where
190 # those do not reach as far as the groups want.
191 1023 ▶sub _plans ($self, $want, $finest, $type) {
Subroutine _plans: called CC=16 SCAR=27.7
192 1023 my $groups = $self->_groups($type);
193 1023 my $ideal = max(0, @$want);
194 1023 ▶ my @tries = $type == SHORT ? ([ 0, 0 ], [ 1, 0 ]) : ([ 0, 0 ], [ 1, 0 ], [ 0, 1 ], [ 1, 1 ]);
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 36 987 1023
195 1023 my @plans;
196  
197 1023 for my $try (@tries) {
198 3936 my ($scale, $pre) = @$try;
199 3936 ▶ my $unit = $scale ? 4 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$scale ? : 1965 1971 3936
200  
201 # The global gain: the coarsest that every group can be made fine
202 # enough from.
203 3936 my $gain = $ideal;
204 3936 for my $g (0 .. $#$groups) {
205 87816 my ($at, $most) = @{ $groups->[$g] }[ SF, MOST ];
206 87816 ▶ my $add = $pre && $at >= 0 ? $PRETAB[$at] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pre && $at >= 0 ? : 39816 48000 87816
Condition100%3/3
$pre and $at >= 0
!ll&&!rl&&r
46104189639816
MC/DC100%2/2
$pre and $at >= 0
$pre $at >= 0
Truth table100%3/3
A and B
A= $preB= $at >= 0
ABresult
0X0
100
111
207 87816 ▶ my $reach = $want->[$g] + ($type == SHORT ? 56 : 0) + $unit * ($most + $add);
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 2808 85008 87816
208 87816 ▶ $gain = $reach if $gain > $reach;
Branch: 1 branch
BranchTrueFalseTotal
if $gain > $reach 7958 79858 87816
209 }
210  
211 3936 ▶ my $sbg = $type == SHORT ? _subblock_gains($groups, $want, $gain, $unit) : [ 0, 0, 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 72 3864 3936
212 3936 my $sf = _scalefactors($groups, $want, $finest, $gain, $sbg, $unit, $pre, $type);
213  
214 # The global gain itself can be no finer than the groups allow.
215 3936 my $floor = 0;
216 3936 for my $g (0 .. $#$groups) {
217 87816 my ($at, $w) = @{ $groups->[$g] }[ SF, WINDOW ];
218 87816 ▶ my $q = $finest->[$g] + ($at >= 0 ? $unit * $sf->[$at] : 0) + ($w >= 0 ? 8 * $sbg->[$w] : 0);
Branch: 2 branches
BranchTrueFalseTotal
$at >= 0 ? : 83736 4080 87816
$w >= 0 ? : 2808 85008 87816
219 87816 ▶ $floor = $q if $q > $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $q > $floor 17381 70435 87816
220 }
221 3936 ▶ $gain = $floor if $gain < $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $gain < $floor 0 3936 3936
222  
223 3936 push @plans, [ $gain, $sf, $sbg, $scale, $floor, $pre ];
224 3936 ▶ last if $gain >= $ideal;
Branch: 1 branch
BranchTrueFalseTotal
if $gain >= $ideal 39 3897 3936
225 }
226 1023 return @plans;
227 }
228  
229 # A short block's subblock gains: for each window, as much as its hardest
230 # group needs beyond what the scalefactors reach, at most 7.
231 72 ▶sub _subblock_gains ($groups, $want, $gain, $unit) {
Subroutine _subblock_gains: called CC=4 SCAR=13.9
232 72 my @sbg = (0, 0, 0);
233 72 for my $g (0 .. $#$groups) {
234 2808 my ($most, $w) = @{ $groups->[$g] }[ MOST, WINDOW ];
235 2808 my $need = $gain - $want->[$g] - $unit * $most;
236 2808 ▶ next if $need <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $need <= 0 1240 1568 2808
237  
238 1568 my $steps = int(($need + 7) / 8);
239 1568 ▶ $sbg[$w] = $steps if $steps > $sbg[$w];
Branch: 1 branch
BranchTrueFalseTotal
if $steps > $sbg[$w] 573 995 1568
240 }
241 72 return [ map { min($_, 7) } @sbg ];
242 }
243  
244 # The scalefactors that bring each group from the global gain to what it
245 # wants, as far as they reach, and no finer than it can go.
246 3936 ▶sub _scalefactors ($groups, $want, $finest, $gain, $sbg, $unit, $pre, $type) {
Subroutine _scalefactors: called CC=11 SCAR=24.1
247 3936 ▶ my @sf = (0) x ($type == SHORT ? 36 : 21);
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 72 3864 3936
248 3936 for my $g (0 .. $#$groups) {
249 87816 my ($at, $most, $w) = @{ $groups->[$g] }[ SF, MOST, WINDOW ];
250 87816 ▶ next if $at < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $at < 0 4080 83736 87816
251  
252 83736 ▶ my $add = $pre ? $PRETAB[$at] : 0; # with preflag, at the least that
Branch: 1 branch
BranchTrueFalseTotal
$pre ? : 39816 43920 83736
253 83736 ▶ my $base = $gain - ($w >= 0 ? 8 * $sbg->[$w] : 0);
Branch: 1 branch
BranchTrueFalseTotal
$w >= 0 ? : 2592 81144 83736
254 83736 my $need = $base - $want->[$g];
255  
256 83736 ▶ my $sf = $need > 0 ? int(($need + $unit - 1) / $unit) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$need > 0 ? : 19220 64516 83736
257 83736 ▶ $sf = $most + $add if $sf > $most + $add;
Branch: 1 branch
BranchTrueFalseTotal
if $sf > $most + $add 0 83736 83736
258 83736 ▶ $sf-- while $sf > 0 && $base - $unit * $sf < $finest->[$g];
Branch: 1 branch
BranchTrueFalseTotal
$sf > 0 and $base - $unit * $sf < $finest->[$g] and do { --$sf } 0 83736 83736
Condition66%2/3
$sf > 0 and $base - $unit * $sf < $finest->[$g]
!ll&&!rl&&r
64516192200
MC/DC0%0/2
$sf > 0 and $base - $unit * $sf < $finest->[$g]
$sf > 0 $base - $unit * $sf < $finest->[$g]
Truth table66%2/3
A and B
A= $sf > 0B= $base - $unit * $sf < $finest->[$g]
ABresult
0X0
100
111
259 83736 ▶ $sf = $add if $sf < $add;
Branch: 1 branch
BranchTrueFalseTotal
if $sf < $add 16765 66971 83736
260 83736 $sf[$at] = $sf;
261 }
262 3936 return \@sf;
263 }
264  
265 # Of several plans, the one whose lines take the fewest bits, and that
266 # count with its lines ([ bits, ix ]) for the fit to start from. A single
267 # plan, or none that can be coded, leaves the first, unpriced.
268 1023 ▶sub _cheapest ($self, $plans, $a34, $type, $reuse) {
Subroutine _cheapest: called CC=6 SCAR=17.9
269 1023 ▶ return ($plans->[0]) if @$plans == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$plans == 1 6 1017 1023
270  
271 1017 my ($plan, $priced) = ($plans->[0]);
272 1017 for my $p (@$plans) {
273 3930 my ($gain, $sf, $sbg, $scale, undef, $pre) = @$p;
274 3930 my $sent = _sent($sf, $pre);
275 3930 my (undef, $part2) = $self->_scalefactor_bits($sent, $type, $pre, _scfsi($sent, $reuse));
276 3930 ▶ next if !defined $part2; # cannot be coded so (MPEG-2)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part2 724 3206 3930
277  
278 3206 my $ix = $self->_quantize($a34, $type, $gain, $sf, $sbg, $scale);
279 3206 my $bits = $part2 + $self->_huffman($ix, 0, $type)->{bits};
280 3206 ▶ ($plan, $priced) = ($p, [ $bits, $ix ]) if !$priced || $bits < $priced->[0];
Branch: 1 branch
BranchTrueFalseTotal
if not $priced or $bits < $priced->[0] 1383 1823 3206
Condition100%3/3
not $priced or $bits < $priced->[0]
l!l&&r!l&&!r
10173661823
MC/DC100%2/2
not $priced or $bits < $priced->[0]
not $priced $bits < $priced->[0]
Truth table100%3/3
not(A) or B
A= not $pricedB= $bits < $priced->[0]
ABresult
1X1
011
000
281 }
282 1017 return ($plan, $priced);
283 }
284  
285 # Step 3: within the budget. The finest global gain from $gain on that
286 # fits; or, where that takes fewer than $least bits, the coarsest from
287 # $finest on that takes them.
288 1023 ▶sub _fit ($self, $a34, $type, $gain, $sf, $sbg, $scale, $finest, $pre, $budget, $least, $reuse, $priced = undef) {
Subroutine _fit: called CC=12 SCAR=25.3
289 1023 my $scfsi = _scfsi(_sent($sf, $pre), $reuse);
290 1023 my ($compress, $part2) = $self->_scalefactor_bits(_sent($sf, $pre), $type, $pre, $scfsi);
291  
292 4031 ▶ my $cost = sub ($g) {
Subroutine __ANON__: called CC=1 SCAR=0.0
293 4031 my $ix = $self->_quantize($a34, $type, $g, $sf, $sbg, $scale);
294 4031 return ($part2 + $self->_huffman($ix, 0, $type)->{bits}, $ix);
295 1023 };
296 1023 ▶ my ($bits, $ix) = $priced ? @$priced : $cost->($gain);
Branch: 1 branch
BranchTrueFalseTotal
$priced ? : 1017 6 1023
297  
298 1023 ▶ if ($bits > $budget) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bits > $budget) { } 31 992 1023
elsif ($bits < $least and $gain > $finest) { } 622 740 1362
299 31 my ($low, $high) = ($gain, 255);
300 31 my ($coarsest) = $cost->($high);
301 31 ▶ if ($coarsest > $budget) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coarsest > $budget) 0 31 31
302 # Not even the coarsest gain fits with these scalefactors: none, then.
303 0 $sf = [ (0) x @$sf ];
304 0 $sbg = [ 0, 0, 0 ];
305 0 $pre = 0;
306 0 $scfsi = _scfsi($sf, $reuse);
307 0 ($compress, $part2) = $self->_scalefactor_bits($sf, $type, 0, $scfsi);
308 0 ($low, $high) = (0, 255);
309 }
310 31 ▶ while ($high - $low > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high - $low > 1) 188 31 219
311 188 my $mid = int(($low + $high) / 2);
312 188 my ($b) = $cost->($mid);
313 188 ▶ if ($b > $budget) { $low = $mid } else { $high = $mid }
Branch: 1 branch
BranchTrueFalseTotal
if ($b > $budget) { } 48 140 188
314 }
315 31 $gain = $high;
316 31 ($bits, $ix) = $cost->($gain);
317 }
318 ▶ elsif ($bits < $least && $gain > $finest) {
Condition66%2/3
$bits < $least and $gain > $finest
!ll&&!rl&&r
3700622
MC/DC50%1/2
$bits < $least and $gain > $finest
$bits < $least $gain > $finest
Truth table66%2/3
A and B
A= $bits < $leastB= $gain > $finest
ABresult
0X0
100
111
319 # bits(low) >= least > bits(high), or low is all there is.
320 622 my ($low, $high) = ($finest, $gain);
321 622 ▶ while ($high - $low > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high - $low > 1) 3153 622 3775
322 3153 my $mid = int(($low + $high) / 2);
323 3153 my ($b) = $cost->($mid);
324 3153 ▶ if ($b >= $least) { $low = $mid } else { $high = $mid }
Branch: 1 branch
BranchTrueFalseTotal
if ($b >= $least) { } 161 2992 3153
325 }
326 622 $gain = $low;
327 622 ($bits, $ix) = $cost->($gain);
328 622 ▶ ($bits, $ix) = $cost->(++$gain) while $bits > $budget;
Branch: 1 branch
BranchTrueFalseTotal
$bits > $budget and do { () = &$cost(++$gain) } 0 622 622
329 }
330  
331 1023 my $code = $self->_huffman($ix, 1, $type);
332 return {
333 ix => $ix,
334 scalefac => _sent($sf, $pre),
335 preflag => $pre,
336 subblock_gain => $sbg,
337 global_gain => $gain,
338 scalefac_scale => $scale,
339 scalefac_compress => $compress,
340 block_type => $type,
341 scfsi => $scfsi,
342 part2_length => $part2,
343 part2_3_length => $part2 + $code->{bits},
344 1023 %$code,
345 };
346 }
347  
348 # A granule of silence.
349 197 ▶sub _silent ($type) {
Subroutine _silent: called CC=2 SCAR=6.9
350 return {
351 197 ▶ ix => [ (0) x 576 ],
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 1 196 197
352 scalefac => [ (0) x ($type == SHORT ? 36 : 21) ],
353 preflag => 0,
354 subblock_gain => [ 0, 0, 0 ],
355 global_gain => 0,
356 scalefac_scale => 0,
357 scalefac_compress => 0,
358 block_type => $type,
359 scfsi => [ 0, 0, 0, 0 ],
360 part2_length => 0,
361 part2_3_length => 0,
362 bits => 0,
363 big_values => 0,
364 count1 => 0,
365 table_select => [ 0, 0, 0 ],
366 region0_count => 0,
367 region1_count => 0,
368 count1table => 0,
369 };
370 }
371  
372 9283 ▶sub _groups ($self, $type) { return $self->{groups}{ $type == SHORT ? 'short' : 'long' } }
Subroutine _groups: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 262 9021 9283
373  
374 # The finest q at which no line of a group, $top the largest, exceeds LARGEST.
375 22845 ▶sub _finest ($top) {
Subroutine _finest: called CC=4 SCAR=15.6
376 22845 my $q = int(210 + 4 * log($top / LARGEST**(4 / 3)) / log(2)) + 1;
377 22845 ▶ $q = 0 if $q < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $q < 0 0 22845 22845
378 22845 ▶ $q++ while $q < 255 && int($top**0.75 * 2**(-0.1875 * ($q - 210)) + ROUND) > LARGEST;
Branch: 1 branch
BranchTrueFalseTotal
$q < 255 and int $top ** "0.75" * 2 ** ("-0.1875" * ($q - 210)) + "0.4054" > 8206 and do { ++$q } 0 22845 22845
Condition33%1/3
$q < 255 and int $top ** "0.75" * 2 ** ("-0.1875" * ($q - 210)) + "0.4054" > 8206
!ll&&!rl&&r
0228450
MC/DC0%0/2
$q < 255 and int $top ** "0.75" * 2 ** ("-0.1875" * ($q - 210)) + "0.4054" > 8206
$q < 255 int $top ** "0.75" * 2 ** ("-0.1875" * ($q - 210)) + "0.4054" > 8206
Truth table33%1/3
A and B
A= $q < 255B= int $top ** "0.75" * 2 ** ("-0.1875" * ($q - 210)) + "0.4054" > 8206
ABresult
0X0
100
111
379 22845 return $q;
380 }
381  
382 # A group's quantization noise at step q.
383 79115 ▶sub _noise ($xr, $a34, $lo, $hi, $q) {
Subroutine _noise: called CC=3 SCAR=11.0
384 79115 my $step = 2**(0.25 * ($q - 210));
385 79115 my $istep = 2**(-0.1875 * ($q - 210));
386 79115 my $sum = 0.0;
387 79115 for my $i ($lo .. $hi - 1) {
388 1440098 my $ix = int($a34->[$i] * $istep + ROUND);
389 1440098 ▶ $ix = LARGEST if $ix > LARGEST;
Branch: 1 branch
BranchTrueFalseTotal
if $ix > 8206 0 1440098 1440098
390 1440098 my $d = abs($xr->[$i]) - $POW43[$ix] * $step;
391 1440098 $sum += $d * $d;
392 }
393 79115 return $sum;
394 }
395  
396 # The lines quantized, as a fresh array.
397 7237 ▶sub _quantize ($self, $a34, $type, $gain, $sf, $sbg, $scale) {
Subroutine _quantize: called CC=7 SCAR=19.5
398 7237 ▶ my $unit = $scale ? 4 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$scale ? : 2855 4382 7237
399 7237 my @ix = (0) x 576;
400  
401 7237 for my $group (@{ $self->_groups($type) }) {
402 162444 my ($lo, $hi, $at, undef, $w) = @$group;
403 162444 ▶ my $q = $gain - ($at >= 0 ? $unit * $sf->[$at] : 0) - ($w >= 0 ? 8 * $sbg->[$w] : 0);
Branch: 2 branches
BranchTrueFalseTotal
$at >= 0 ? : 154827 7617 162444
$w >= 0 ? : 7410 155034 162444
404 162444 my $istep = 2**(-0.1875 * ($q - 210));
405 162444 for my $i ($lo .. $hi - 1) {
406 4168512 my $ix = int($a34->[$i] * $istep + ROUND);
407 4168512 ▶ $ix = LARGEST if $ix > LARGEST;
Branch: 1 branch
BranchTrueFalseTotal
if $ix > 8206 0 4168512 4168512
408 4168512 $ix[$i] = $ix;
409 }
410 }
411 7237 return \@ix;
412 }
413  
414 # The scalefactors as sent: with preflag, less the decoder's table.
415 6999 ▶sub _sent ($sf, $pre) {
Subroutine _sent: called CC=2 SCAR=6.9
416 6999 ▶ return $sf if !$pre;
Branch: 1 branch
BranchTrueFalseTotal
unless $pre 5013 1986 6999
417 1986 return [ map { $sf->[$_] - $PRETAB[$_] } 0 .. $#$sf ];
418 }
419  
420 # Step 4: which of the four scfsi groups of a long block's scalefactors are
421 # the same as the first granule's, as [ 0 or 1 ] x 4. None without a first.
422 4953 ▶sub _scfsi ($sent, $reuse) {
Subroutine _scfsi: called CC=4 SCAR=13.9
423 4953 ▶ return [ 0, 0, 0, 0 ] if !$reuse;
Branch: 1 branch
BranchTrueFalseTotal
unless $reuse 3633 1320 4953
424  
425 1320 my @same;
426 1320 for my $group (@SCFSI) {
427 5280 my ($from, $to) = @$group;
428 5280 my $differs = grep { $sent->[$_] != $reuse->[$_] } $from .. $to - 1;
429 5280 ▶ push @same, $differs ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$differs ? : 2593 2687 5280
430 }
431 1320 return \@same;
432 }
433  
434 # The scfsi group of long block scalefactor $i.
435 73710 ▶sub _scfsi_group ($i) {
Subroutine _scfsi_group: called CC=4 SCAR=13.9
436 73710 ▶ return 0 if $i < 6;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 6 21060 52650 73710
437 52650 ▶ return 1 if $i < 11;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 11 17550 35100 52650
438 35100 ▶ return 2 if $i < 16;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 16 17550 17550 35100
439 17550 return 3;
440 }
441  
442 # The scalefactors' bits: (scalefac_compress, bits), or nothing where they
443 # cannot be coded so.
444 4953 ▶sub _scalefactor_bits ($self, $sf, $type, $pre, $scfsi) {
Subroutine _scalefactor_bits: called CC=2 SCAR=6.9
445 4953 ▶ return _part2_lsf($sf, $type, $pre) if $self->{lsf};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lsf"} 2110 2843 4953
446 2843 return _part2($sf, $type, $scfsi);
447 }
448  
449 # MPEG-1: the smallest scalefac_compress that holds the scalefactors. Long
450 # blocks: bands 0-10 with slen1 bits, 11-20 with slen2. Short: bands 0-5
451 # (scalefactors 0-17) with slen1, 6-11 (18-35) with slen2. Groups that
452 # scfsi reuses are not sent.
453 2843 ▶sub _part2 ($sf, $type, $scfsi = [ 0, 0, 0, 0 ]) {
Subroutine _part2: called CC=17 SCAR=28.3
454 2843 ▶ my ($split, $first, $second) = $type == SHORT ? (18, 18, 18) : (11, 11, 10);
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 63 2780 2843
455  
456 2843 for my $g (0 .. 3) {
457 11372 ▶ next if !$scfsi->[$g];
Branch: 1 branch
BranchTrueFalseTotal
unless $scfsi->[$g] 8685 2687 11372
458 2687 my ($from, $to) = @{ $SCFSI[$g] };
459 2687 ▶ if ($from < 11) { $first -= $to - $from } else { $second -= $to - $from }
Branch: 1 branch
BranchTrueFalseTotal
if ($from < 11) { } 561 2126 2687
460 }
461  
462 2843 my ($one, $two) = (0, 0); # the largest of each part
463 2843 for my $i (0 .. $#$sf) {
464 60648 ▶ next if $type != SHORT && $scfsi->[ _scfsi_group($i) ];
Branch: 1 branch
BranchTrueFalseTotal
if $type != 2 and $$scfsi[_scfsi_group($i)] 13609 47039 60648
Condition100%3/3
$type != 2 and $$scfsi[_scfsi_group($i)]
!ll&&!rl&&r
22684477113609
MC/DC100%2/2
$type != 2 and $$scfsi[_scfsi_group($i)]
$type != 2 $$scfsi[_scfsi_group($i)]
Truth table100%3/3
A and B
A= $type != 2B= $$scfsi[_scfsi_group($i)]
ABresult
0X0
100
111
465 47039 ▶ if ($i < $split) { $one = $sf->[$i] if $sf->[$i] > $one }
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $split) { } 28735 18304 47039
if $sf->[$i] > $one 4820 23915 28735
466 18304 ▶ else { $two = $sf->[$i] if $sf->[$i] > $two }
Branch: 1 branch
BranchTrueFalseTotal
if $sf->[$i] > $two 1327 16977 18304
467 }
468  
469 2843 my ($best, $bits);
470 2843 for my $c (0 .. 15) {
471 45488 ▶ next if (1 << $SLEN1[$c]) <= $one || (1 << $SLEN2[$c]) <= $two;
Branch: 1 branch
BranchTrueFalseTotal
if 1 << $SLEN1[$c] <= $one or 1 << $SLEN2[$c] <= $two 39313 6175 45488
Condition100%3/3
1 << $SLEN1[$c] <= $one or 1 << $SLEN2[$c] <= $two
l!l&&r!l&&!r
3812511886175
MC/DC100%2/2
1 << $SLEN1[$c] <= $one or 1 << $SLEN2[$c] <= $two
1 << $SLEN1[$c] <= $one 1 << $SLEN2[$c] <= $two
Truth table100%3/3
A or B
A= 1 << $SLEN1[$c] <= $oneB= 1 << $SLEN2[$c] <= $two
ABresult
1X1
011
000
472 6175 my $cost = $first * $SLEN1[$c] + $second * $SLEN2[$c];
473 6175 ▶ ($best, $bits) = ($c, $cost) if !defined $bits || $cost < $bits;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bits or $cost < $bits 2843 3332 6175
Condition66%2/3
not defined $bits or $cost < $bits
l!l&&r!l&&!r
284303332
MC/DC50%1/2
not defined $bits or $cost < $bits
not defined $bits $cost < $bits
Truth table66%2/3
not(A) or B
A= not defined $bitsB= $cost < $bits
ABresult
1X1
011
000
474 }
475 2843 return ($best, $bits);
476 }
477  
478 # MPEG-2: scalefac_compress chooses a partition of the scalefactors and each
479 # part's bits (ISO 13818-3, 2.4.3.2): the cheapest that holds them. Table 2
480 # means preflag; so with preflag only it, and for a long block without, not
481 # it.
482 2110 ▶sub _part2_lsf ($sf, $type, $pre) {
Subroutine _part2_lsf: called CC=11 SCAR=24.0
483 2110 ▶ my $row = $type == SHORT ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 45 2065 2110
484 2110 ▶ my @tables = $pre ? (2) : $type == SHORT ? (0, 1, 2) : (0, 1);
Branch: 2 branches
BranchTrueFalseTotal
$pre ? : 784 1326 2110
$type == 2 ? : 45 1281 1326
485 2110 my ($best, $bits);
486  
487 TABLE:
488 2110 for my $t (@tables) {
489 3481 my $parts = $LSF_PARTS[$t][$row];
490 3481 my ($at, $cost, @slen) = (0, 0);
491 3481 for my $p (0 .. 3) {
492 11687 my $top = max(0, @$sf[ $at .. $at + $parts->[$p] - 1 ]);
493 11687 $at += $parts->[$p];
494  
495 11687 my $len = 0;
496 11687 ▶ $len++ while (1 << $len) <= $top;
Branch: 1 branch
BranchTrueFalseTotal
1 << $len <= $top and do { ++$len } 14333 11687 26020
497 11687 ▶ next TABLE if $len > $LSF_SLEN_MOST[$t][$p];
Branch: 1 branch
BranchTrueFalseTotal
if $len > $LSF_SLEN_MOST[$t][$p] 839 10848 11687
498  
499 10848 push @slen, $len;
500 10848 $cost += $parts->[$p] * $len;
501 }
502 2642 my $compress = _lsf_compress($t, @slen);
503 2642 ▶ ($best, $bits) = ($compress, $cost) if !defined $bits || $cost < $bits;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bits or $cost < $bits 1391 1251 2642
Condition100%3/3
not defined $bits or $cost < $bits
l!l&&r!l&&!r
138651251
MC/DC100%2/2
not defined $bits or $cost < $bits
not defined $bits $cost < $bits
Truth table100%3/3
not(A) or B
A= not defined $bitsB= $cost < $bits
ABresult
1X1
011
000
504 }
505 2110 ▶ return defined $bits ? ($best, $bits) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $bits ? : 1386 724 2110
506 }
507  
508 # MPEG-2: scalefac_compress for a table and its parts' bits ...
509 2642 ▶sub _lsf_compress ($t, @slen) {
Subroutine _lsf_compress: called CC=3 SCAR=11.0
510 2642 ▶ return (($slen[0] * 5 + $slen[1]) << 4) + ($slen[2] << 2) + $slen[3] if $t == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t == 0 1326 1316 2642
511 1316 ▶ return 400 + (($slen[0] * 5 + $slen[1]) << 2) + $slen[2] if $t == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $t == 1 1250 66 1316
512 66 return 500 + $slen[0] * 3 + $slen[1];
513 }
514  
515 # ... and back: each part's bands and bits, as the decoder reads them.
516 468 ▶sub _lsf_slen ($compress, $type) {
Subroutine _lsf_slen: called CC=4 SCAR=14.2
517 468 ▶ my $row = $type == SHORT ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 ? : 15 453 468
518  
519 468 ▶ if ($compress < 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress < 400) 466 2 468
520 466 my $c = $compress;
521 466 return ($LSF_PARTS[0][$row], [ int(($c >> 4) / 5), ($c >> 4) % 5, ($c >> 2) % 4, $c % 4 ]);
522 }
523 2 ▶ if ($compress < 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compress < 500) 2 0 2
524 2 my $c = $compress - 400;
525 2 return ($LSF_PARTS[1][$row], [ int(($c >> 2) / 5), ($c >> 2) % 5, $c % 4, 0 ]);
526 }
527 0 my $c = $compress - 500;
528 0 return ($LSF_PARTS[2][$row], [ int($c / 3), $c % 3, 0, 0 ]);
529 }
530  
531 # Step 5: the Huffman coding of the lines. Returns { bits, big_values,
532 # count1 (the end of the quadruples), table_select, region0_count,
533 # region1_count, count1table }.
534 #
535 # With $exact, a normal long block gets the best regions of all there may
536 # be; that is done once a granule. Otherwise the regions are known before
537 # the values are looked at (fixed, or a quick guess).
538 8260 ▶sub _huffman ($self, $ix, $exact, $type) {
Subroutine _huffman: called CC=12 SCAR=24.8
539 8260 my @ix = @$ix;
540  
541 # The zeros at the top are not coded; below them, the quadruples of
542 # ones and zeros.
543 8260 my $end = 576;
544 8260 ▶ $end-- while $end > 0 && $ix[$end - 1] == 0;
Branch: 1 branch
BranchTrueFalseTotal
$end > 0 and $ix[$end - 1] == 0 and do { --$end } 3756979 8260 3765239
Condition100%3/3
$end > 0 and $ix[$end - 1] == 0
!ll&&!rl&&r
4882123756979
MC/DC100%2/2
$end > 0 and $ix[$end - 1] == 0
$end > 0 $ix[$end - 1] == 0
Truth table100%3/3
A and B
A= $end > 0B= $ix[$end - 1] == 0
ABresult
0X0
100
111
545 8260 ▶ $end++ if $end % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $end % 2 3229 5031 8260
546 8260 my $start = $end;
547 8260 ▶ while ($start >= 4 && $ix[$start - 1] <= 1 && $ix[$start - 2] <= 1 && $ix[$start - 3] <= 1 && $ix[$start - 4] <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start >= 4 and $ix[$start - 1] <= 1 and $ix[$start - 2] <= 1 and $ix[$start - 3] <= 1 and $ix[$start - 4] <= 1) 92571 8260 100831
Condition100%12/12
$start >= 4 and $ix[$start - 1] <= 1 and $ix[$start - 2] <= 1 and $ix[$start - 3] <= 1 and $ix[$start - 4] <= 1
!ll&&!rl&&r
6635162592571
$start >= 4 and $ix[$start - 1] <= 1 and $ix[$start - 2] <= 1 and $ix[$start - 3] <= 1
!ll&&!rl&&r
4327230894196
$start >= 4 and $ix[$start - 1] <= 1 and $ix[$start - 2] <= 1
!ll&&!rl&&r
2645168296504
$start >= 4 and $ix[$start - 1] <= 1
!ll&&!rl&&r
66257998186
MC/DC100%5/5
$start >= 4 and $ix[$start - 1] <= 1 and $ix[$start - 2] <= 1 and $ix[$start - 3] <= 1 and $ix[$start - 4] <= 1
$start >= 4 $ix[$start - 1] <= 1 $ix[$start - 2] <= 1 $ix[$start - 3] <= 1 $ix[$start - 4] <= 1
Truth table100%6/6
A and B and C and D and E
A= $start >= 4B= $ix[$start - 1] <= 1C= $ix[$start - 2] <= 1D= $ix[$start - 3] <= 1E= $ix[$start - 4] <= 1
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
548 92571 $start -= 4;
549 }
550 8260 my ($count1table, $bits) = _count1(\@ix, $start, $end);
551  
552 8260 my $pairs = $start / 2;
553 8260 my %code = (
554 big_values => $pairs,
555 count1 => $end,
556 count1table => $count1table,
557 region0_count => 0,
558 region1_count => 0,
559 table_select => [ 0, 0, 0 ],
560 );
561 8260 ▶ return { %code, bits => $bits } if $pairs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pairs == 0 56 8204 8260
562  
563 8204 ▶ my ($regions, $big_bits) = $exact && $type == 0
Branch: 1 branch
BranchTrueFalseTotal
$exact && $type == 0 ? : 935 7269 8204
Condition100%3/3
$exact and $type == 0
!ll&&!rl&&r
718584935
MC/DC100%2/2
$exact and $type == 0
$exact $type == 0
Truth table100%3/3
A and B
A= $exactB= $type == 0
ABresult
0X0
100
111
564 ? $self->_best_regions(\@ix, $pairs)
565 : $self->_guessed_regions(\@ix, $pairs, $type);
566 8204 @code{qw(table_select region0_count region1_count)} = @$regions;
567  
568 8204 return { %code, bits => $bits + $big_bits };
569 }
570  
571 # The quadruples from $start to $end: the table that codes them shorter (0:
572 # A, 1: B) and its bits, with the signs.
573 8260 ▶sub _count1 ($ix, $start, $end) {
Subroutine _count1: called CC=3 SCAR=11.0
574 8260 my @len_a = @{ $HUFFMAN[32][2] };
575 8260 my ($bits_a, $bits_b) = (0, 0);
576  
577 8260 ▶ for (my $i = $start; $i < $end; $i += 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 92571 8260 100831
578 92571 my $signs = $ix->[$i] + $ix->[$i + 1] + $ix->[$i + 2] + $ix->[$i + 3];
579 92571 $bits_a += $len_a[ $ix->[$i] * 8 + $ix->[$i + 1] * 4 + $ix->[$i + 2] * 2 + $ix->[$i + 3] ] + $signs;
580 92571 $bits_b += 4 + $signs;
581 }
582 8260 ▶ return $bits_b < $bits_a ? (1, $bits_b) : (0, $bits_a);
Branch: 1 branch
BranchTrueFalseTotal
$bits_b < $bits_a ? : 2260 6000 8260
583 }
584  
585 # Where the regions of a block's big values end, in pairs: a normal block's
586 # from its region counts, a start or stop block's region 0 at the 8th long
587 # band's end (36 lines at MPEG-1's rates, 54 at MPEG-2's), a short block's
588 # after 36 lines. Short, start and stop blocks have no third region.
589 8489 ▶sub _region_ends ($self, $type, $r0, $r1) {
Subroutine _region_ends: called CC=3 SCAR=11.0
590 8489 my $long = $self->{long};
591 8489 ▶ return ($long->[$r0 + 1] / 2, $long->[$r0 + $r1 + 2] / 2) if $type == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 0 7717 772 8489
592 772 ▶ return (18, 288) if $type == SHORT;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 2 259 513 772
593 513 return ($long->[8] / 2, 288);
594 }
595  
596 # The regions as a quick guess, each with the table that codes it shortest:
597 # ([ \@table_select, region0_count, region1_count ], bits). A normal
598 # block's regions end at the band edges nearest a third and two thirds of
599 # the big values.
600 7269 ▶sub _guessed_regions ($self, $ix, $pairs, $type) {
Subroutine _guessed_regions: called CC=9 SCAR=22.0
601 7269 my @long = @{ $self->{long} };
602 7269 my ($r0, $r1) = (0, 0);
603 7269 ▶ if ($type == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 0) 6588 681 7269
604 6588 for my $a (0 .. 15) {
605 105408 ▶ $r0 = $a if $long[$a + 1] / 2 <= $pairs / 3;
Branch: 1 branch
BranchTrueFalseTotal
if $long[$a + 1] / 2 <= $pairs / 3 20303 85105 105408
606 }
607 6588 for my $b (0 .. 7) {
608 52704 ▶ $r1 = $b if $r0 + $b + 2 <= $#long && $long[$r0 + $b + 2] / 2 <= 2 * $pairs / 3;
Branch: 1 branch
BranchTrueFalseTotal
if $r0 + $b + 2 <= $#long and $long[$r0 + $b + 2] / 2 <= 2 * $pairs / 3 16336 36368 52704
Condition100%3/3
$r0 + $b + 2 <= $#long and $long[$r0 + $b + 2] / 2 <= 2 * $pairs / 3
!ll&&!rl&&r
3113605716336
MC/DC100%2/2
$r0 + $b + 2 <= $#long and $long[$r0 + $b + 2] / 2 <= 2 * $pairs / 3
$r0 + $b + 2 <= $#long $long[$r0 + $b + 2] / 2 <= 2 * $pairs / 3
Truth table100%3/3
A and B
A= $r0 + $b + 2 <= $#longB= $long[$r0 + $b + 2] / 2 <= 2 * $pairs / 3
ABresult
0X0
100
111
609 }
610 }
611  
612 7269 my ($one, $two) = $self->_region_ends($type, $r0, $r1);
613 7269 $one = min($one, $pairs);
614 7269 ▶ $two = $type == 0 ? min($two, $pairs) : $pairs;
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 ? : 6588 681 7269
615  
616 7269 my ($t0, $b0) = _region($ix, 0, $one);
617 7269 my ($t1, $b1) = _region($ix, $one, $two);
618 7269 ▶ my ($t2, $b2) = $type == 0 ? _region($ix, $two, $pairs) : (0, 0);
Branch: 1 branch
BranchTrueFalseTotal
$type == 0 ? : 6588 681 7269
619  
620 7269 return ([ [ $t0, $t1, $t2 ], $r0, $r1 ], $b0 + $b1 + $b2);
621 }
622  
623 # The best regions of all a normal long block may have, as for
624 # _guessed_regions.
625 #
626 # The code lengths of the big values are summed once per table that has
627 # lengths of its own (16-23 share theirs, as do 24-31), pair by pair, so
628 # that what a region costs in a table is a difference of two sums.
629 935 ▶sub _best_regions ($self, $ix, $pairs) {
Subroutine _best_regions: called CC=19 SCAR=29.4
630 # Per pair: its place in the tables, its larger value; running sums of
631 # the signs and the escapes (values of 15 and more) before it.
632 935 my @place = (0) x $pairs;
633 935 my @big = (0) x $pairs;
634 935 my @signs = (0) x ($pairs + 1);
635 935 my @esc = (0) x ($pairs + 1);
636 935 my ($s, $e) = (0, 0);
637 935 for my $p (0 .. $pairs - 1) {
638 36851 my $x = $ix->[2 * $p];
639 36851 my $y = $ix->[2 * $p + 1];
640 36851 ▶ my $cx = $x > 15 ? 15 : $x;
Branch: 1 branch
BranchTrueFalseTotal
$x > 15 ? : 6332 30519 36851
641 36851 ▶ my $cy = $y > 15 ? 15 : $y;
Branch: 1 branch
BranchTrueFalseTotal
$y > 15 ? : 6332 30519 36851
642 36851 $place[$p] = 16 * $cx + $cy;
643 36851 ▶ $big[$p] = $x > $y ? $x : $y;
Branch: 1 branch
BranchTrueFalseTotal
$x > $y ? : 11898 24953 36851
644 36851 ▶ $s += ($x > 0 ? 1 : 0) + ($y > 0 ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$x > 0 ? : 26808 10043 36851
$y > 0 ? : 26925 9926 36851
645 36851 ▶ $e += ($x > 14 ? 1 : 0) + ($y > 14 ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$x > 14 ? : 6436 30415 36851
$y > 14 ? : 6424 30427 36851
646 36851 $signs[$p + 1] = $s;
647 36851 $esc[$p + 1] = $e;
648 }
649  
650 # Running sums of the code lengths: sums[(pairs + 1) c + p], c the
651 # table's place in @CODED.
652 935 my $stride = $pairs + 1;
653 935 my @sums = (0) x ($stride * @CODED);
654 935 my ($c, $p, $sum) = (0, 0, 0);
655 935 for my $n (0 .. @CODED * $pairs - 1) {
656 552765 $sum += $LENGTHS[ 256 * $c + $place[$p] ];
657 552765 $p++;
658 552765 $sums[ $stride * $c + $p ] = $sum;
659 552765 ▶ if ($p == $pairs) { $p = 0; $sum = 0; $c++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($p == $pairs) 14025 538740 552765
660 }
661  
662 # Where regions may end, in pairs, as far as the big values go: the long
663 # bands' edges; and the largest value between each edge and the next.
664 935 my @long = @{ $self->{long} };
665 935 my @edge = map { min($_ / 2, $pairs) } @long;
666 935 my @top = map { max(0, @big[ $edge[$_] .. $edge[$_ + 1] - 1 ]) } 0 .. $#edge - 1;
667  
668 # A region from edge $from to edge $to: its best table and bits.
669 80237 ▶ my $cost = sub ($from, $to) {
Subroutine __ANON__: called CC=7 SCAR=19.5
670 80237 my ($a, $b) = ($edge[$from], $edge[$to]);
671 80237 ▶ return (0, 0) if $b <= $a;
Branch: 1 branch
BranchTrueFalseTotal
if $b <= $a 7574 72663 80237
672  
673 72663 my $m = max(@top[ $from .. $to - 1 ]);
674 72663 ▶ return (0, 0) if $m == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $m == 0 4529 68134 72663
675  
676 68134 my ($best, $least);
677 68134 ▶ for my $t ($m < 16 ? @{ $SMALL[$m] } : _escape_tables($m)) {
Branch: 1 branch
BranchTrueFalseTotal
$m < 16 ? : 31988 36146 68134
678 149296 my $at = $stride * $CODED_AT[$t];
679 149296 my $sum = $sums[ $at + $b ] - $sums[ $at + $a ] + $HUFFMAN[$t][1] * ($esc[$b] - $esc[$a]);
680 149296 ▶ ($best, $least) = ($t, $sum) if !defined $least || $sum < $least;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $least or $sum < $least 139224 10072 149296
Condition100%3/3
not defined $least or $sum < $least
l!l&&r!l&&!r
681347109010072
MC/DC100%2/2
not defined $least or $sum < $least
not defined $least $sum < $least
Truth table100%3/3
not(A) or B
A= not defined $leastB= $sum < $least
ABresult
1X1
011
000
681 }
682 68134 return ($best, $least + $signs[$b] - $signs[$a]);
683 935 };
684  
685 # The edge where the big values end.
686 935 my $last = 1;
687 935 ▶ $last++ while $edge[$last] < $pairs;
Branch: 1 branch
BranchTrueFalseTotal
$edge[$last] < $pairs and do { ++$last } 7625 935 8560
688  
689 935 my $best;
690 935 for my $r0 (0 .. 15) {
691 8103 my $one = min($r0 + 1, $last);
692 8103 my ($t0, $b0) = $cost->(0, $one);
693 8103 for my $r1 (0 .. 7) {
694 36067 ▶ last if $r0 + $r1 + 2 > $#long;
Branch: 1 branch
BranchTrueFalseTotal
if $r0 + $r1 + 2 > $#long 0 36067 36067
695  
696 36067 my $two = min($r0 + $r1 + 2, $last);
697 36067 my ($t1, $b1) = $cost->($one, $two);
698 36067 my ($t2, $b2) = $cost->($two, $last);
699 36067 my $total = $b0 + $b1 + $b2;
700 36067 ▶ $best = [ $total, $r0, $r1, $t0, $t1, $t2 ] if !$best || $total < $best->[0];
Branch: 1 branch
BranchTrueFalseTotal
if not $best or $total < $best->[0] 4448 31619 36067
Condition100%3/3
not $best or $total < $best->[0]
l!l&&r!l&&!r
935351331619
MC/DC100%2/2
not $best or $total < $best->[0]
not $best $total < $best->[0]
Truth table100%3/3
not(A) or B
A= not $bestB= $total < $best->[0]
ABresult
1X1
011
000
701  
702 36067 ▶ last if $two >= $last;
Branch: 1 branch
BranchTrueFalseTotal
if $two >= $last 6739 29328 36067
703 }
704 8103 ▶ last if $one >= $last;
Branch: 1 branch
BranchTrueFalseTotal
if $one >= $last 835 7268 8103
705 }
706  
707 935 my ($total, $r0, $r1, @tables) = @$best;
708 935 return ([ \@tables, $r0, $r1 ], $total);
709 }
710  
711 # A region of big values, pairs $from to $to: the table that codes it
712 # shortest, and its bits with the signs and the escapes' linbits. One pass
713 # for its largest value, signs and escapes; one per table that holds the
714 # largest.
715 21126 ▶sub _region ($ix, $from, $to) {
Subroutine _region: called CC=14 SCAR=26.4
716 21126 ▶ return (0, 0) if $to <= $from;
Branch: 1 branch
BranchTrueFalseTotal
if $to <= $from 199 20927 21126
717  
718 20927 my ($m, $signs, $esc) = (0, 0, 0);
719 20927 for my $i (2 * $from .. 2 * $to - 1) {
720 560024 my $v = $ix->[$i];
721 560024 ▶ next if !$v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 173778 386246 560024
722 386246 $signs++;
723 386246 ▶ $m = $v if $v > $m;
Branch: 1 branch
BranchTrueFalseTotal
if $v > $m 54761 331485 386246
724 386246 ▶ $esc++ if $v > 14;
Branch: 1 branch
BranchTrueFalseTotal
if $v > 14 81389 304857 386246
725 }
726 20927 ▶ return (0, 0) if $m == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $m == 0 5282 15645 20927
727  
728 15645 my ($best, $least);
729 15645 ▶ for my $t ($m < 16 ? @{ $SMALL[$m] } : _escape_tables($m)) {
Branch: 1 branch
BranchTrueFalseTotal
$m < 16 ? : 6573 9072 15645
730 33037 my $base = 256 * $CODED_AT[$t];
731 33037 my $sum = $HUFFMAN[$t][1] * $esc;
732 33037 for my $p ($from .. $to - 1) {
733 553783 my $x = $ix->[2 * $p];
734 553783 my $y = $ix->[2 * $p + 1];
735 553783 ▶ $sum += $LENGTHS[ $base + 16 * ($x > 15 ? 15 : $x) + ($y > 15 ? 15 : $y) ];
Branch: 2 branches
BranchTrueFalseTotal
$x > 15 ? : 79792 473991 553783
$y > 15 ? : 79436 474347 553783
736 }
737 33037 ▶ ($best, $least) = ($t, $sum) if !defined $least || $sum < $least;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $least or $sum < $least 28335 4702 33037
Condition100%3/3
not defined $least or $sum < $least
l!l&&r!l&&!r
15645126904702
MC/DC100%2/2
not defined $least or $sum < $least
not defined $least $sum < $least
Truth table100%3/3
not(A) or B
A= not defined $leastB= $sum < $least
ABresult
1X1
011
000
738 }
739 15645 return ($best, $least + $signs);
740 }
741  
742 # The tables for a largest value above 15: of each family the smallest
743 # whose linbits hold it.
744 45218 ▶sub _escape_tables ($top) {
Subroutine _escape_tables: called CC=4 SCAR=13.9
745 45218 my $need = 0;
746 45218 ▶ $need++ while (1 << $need) <= $top - 15;
Branch: 1 branch
BranchTrueFalseTotal
1 << $need <= $top - 15 and do { ++$need } 426902 45218 472120
747  
748 45218 my @tables;
749 45218 for my $family (@ESCAPE) {
750 90436 my ($t) = grep { $HUFFMAN[$_][1] >= $need } @$family;
751 90436 ▶ push @tables, $t if defined $t;
Branch: 1 branch
BranchTrueFalseTotal
if defined $t 90436 0 90436
752 }
753 45218 return @tables;
754 }
755  
756 # Writes the granule's main data to a bit writer: the scalefactors (but
757 # those scfsi reuses), then the Huffman codes. $xr gives the signs.
758 1220 ▶sub write ($self, $gi, $out, $xr) {
Subroutine write: called CC=1 SCAR=0.0
Pod documented
759 1220 $self->_write_scalefactors($gi, $out);
760 1220 $self->_write_big_values($gi, $out, $xr);
761 1220 _write_count1($gi, $out, $xr);
762 1220 return;
763 }
764  
765 1220 ▶sub _write_scalefactors ($self, $gi, $out) {
Subroutine _write_scalefactors: called CC=9 SCAR=22.0
766 1220 my $sf = $gi->{scalefac};
767  
768 1220 ▶ if ($self->{lsf}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lsf"}) 468 752 1220
769 468 my ($parts, $slen) = _lsf_slen($gi->{scalefac_compress}, $gi->{block_type});
770 468 my $i = 0;
771 468 for my $p (0 .. 3) {
772 1872 for (1 .. $parts->[$p]) {
773 10053 $out->put($sf->[$i], $slen->[$p]);
774 10053 $i++;
775 }
776 }
777 468 return;
778 }
779  
780 752 my $s1 = $SLEN1[ $gi->{scalefac_compress} ];
781 752 my $s2 = $SLEN2[ $gi->{scalefac_compress} ];
782 752 my $short = $gi->{block_type} == SHORT;
783 752 ▶ my $split = $short ? 18 : 11;
Branch: 1 branch
BranchTrueFalseTotal
$short ? : 22 730 752
784 752 for my $i (0 .. $#$sf) {
785 16122 ▶ next if !$short && $gi->{scfsi}[ _scfsi_group($i) ];
Branch: 1 branch
BranchTrueFalseTotal
if not $short and $gi->{"scfsi"}[_scfsi_group($i)] 2958 13164 16122
Condition100%3/3
not $short and $gi->{"scfsi"}[_scfsi_group($i)]
!ll&&!rl&&r
792123722958
MC/DC100%2/2
not $short and $gi->{"scfsi"}[_scfsi_group($i)]
not $short $gi->{"scfsi"}[_scfsi_group($i)]
Truth table100%3/3
not(A) and B
A= not $shortB= $gi->{"scfsi"}[_scfsi_group($i)]
ABresult
0X0
100
111
786 13164 ▶ $out->put($sf->[$i], $i < $split ? $s1 : $s2);
Branch: 1 branch
BranchTrueFalseTotal
$i < $split ? : 7603 5561 13164
787 }
788 752 return;
789 }
790  
791 1220 ▶sub _write_big_values ($self, $gi, $out, $xr) {
Subroutine _write_big_values: called CC=15 SCAR=27.1
792 1220 my $ix = $gi->{ix};
793 1220 my ($one, $two) = $self->_region_ends($gi->{block_type}, $gi->{region0_count}, $gi->{region1_count});
794  
795 1220 for my $p (0 .. $gi->{big_values} - 1) {
796 46486 ▶ my $region = $p < $one ? 0 : $p < $two ? 1 : 2;
Branch: 2 branches
BranchTrueFalseTotal
$p < $one ? : 10160 36326 46486
$p < $two ? : 18806 17520 36326
797 46486 my $table = $gi->{table_select}[$region];
798 46486 ▶ next if $table == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $table == 0 4352 42134 46486
799  
800 42134 my (undef, $linbits, $len, $code) = @{ $HUFFMAN[$table] };
801 42134 my $x = $ix->[2 * $p];
802 42134 my $y = $ix->[2 * $p + 1];
803 42134 ▶ my $cx = $x > 15 ? 15 : $x;
Branch: 1 branch
BranchTrueFalseTotal
$x > 15 ? : 6791 35343 42134
804 42134 ▶ my $cy = $y > 15 ? 15 : $y;
Branch: 1 branch
BranchTrueFalseTotal
$y > 15 ? : 6720 35414 42134
805  
806 42134 $out->put($code->[ $cx * 16 + $cy ], $len->[ $cx * 16 + $cy ]);
807 42134 ▶ $out->put($x - 15, $linbits) if $cx == 15 && $linbits;
Branch: 1 branch
BranchTrueFalseTotal
if $cx == 15 and $linbits 6902 35232 42134
Condition100%3/3
$cx == 15 and $linbits
!ll&&!rl&&r
35214186902
MC/DC100%2/2
$cx == 15 and $linbits
$cx == 15 $linbits
Truth table100%3/3
A and B
A= $cx == 15B= $linbits
ABresult
0X0
100
111
808 42134 ▶ $out->put($xr->[2 * $p] < 0 ? 1 : 0, 1) if $x;
Branch: 2 branches
BranchTrueFalseTotal
if $x 33210 8924 42134
$$xr[2 * $p] < 0 ? : 16573 16637 33210
809 42134 ▶ $out->put($y - 15, $linbits) if $cy == 15 && $linbits;
Branch: 1 branch
BranchTrueFalseTotal
if $cy == 15 and $linbits 6823 35311 42134
Condition100%3/3
$cy == 15 and $linbits
!ll&&!rl&&r
35297146823
MC/DC100%2/2
$cy == 15 and $linbits
$cy == 15 $linbits
Truth table100%3/3
A and B
A= $cy == 15B= $linbits
ABresult
0X0
100
111
810 42134 ▶ $out->put($xr->[2 * $p + 1] < 0 ? 1 : 0, 1) if $y;
Branch: 2 branches
BranchTrueFalseTotal
if $y 33160 8974 42134
$$xr[2 * $p + 1] < 0 ? : 16581 16579 33160
811 }
812 1220 return;
813 }
814  
815 1220 ▶sub _write_count1 ($gi, $out, $xr) {
Subroutine _write_count1: called CC=6 SCAR=17.9
816 1220 my $ix = $gi->{ix};
817 1220 my ($len, $code) = @{ $HUFFMAN[32] }[ 2, 3 ];
818  
819 1220 ▶ for (my $i = 2 * $gi->{big_values}; $i < $gi->{count1}; $i += 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $gi->{"count1"}) 20849 1220 22069
820 20849 my $v = $ix->[$i] * 8 + $ix->[$i + 1] * 4 + $ix->[$i + 2] * 2 + $ix->[$i + 3];
821 20849 ▶ if ($gi->{count1table}) { $out->put(15 - $v, 4) }
Branch: 1 branch
BranchTrueFalseTotal
if ($gi->{'count1table'}) { } 3051 17798 20849
822 17798 else { $out->put($code->[$v], $len->[$v]) }
823  
824 20849 for my $k ($i .. $i + 3) {
825 83396 ▶ $out->put($xr->[$k] < 0 ? 1 : 0, 1) if $ix->[$k];
Branch: 2 branches
BranchTrueFalseTotal
if $ix->[$k] 13646 69750 83396
$xr->[$k] < 0 ? : 6766 6880 13646
826 }
827 }
828 1220 return;
829 }
830  
831 1 1;
832  
833 __END__
« lib/Peta/Vorbis/Encode/Psy.pm ↑ Summary lib/Peta/Synth/MIDI.pm »