| 1 | package Peta::MP3::Encode; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: MP3 (MPEG-1 and MPEG-2 Layer III) encoder | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # An MPEG-1 Layer III encoder, and MPEG-2's at the lower sample rates: | ||||||||||||||||||||||||||||
| 5 | # samples go in, MP3 frames come out. The work is shared out like this: | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Filterbank the polyphase filterbank and the MDCT, 576 lines a granule | ||||||||||||||||||||||||||||
| 8 | # (::PDL does the same with PDL, faster on both perls) | ||||||||||||||||||||||||||||
| 9 | # Psy how much noise each scalefactor band can hide; attacks | ||||||||||||||||||||||||||||
| 10 | # Quantize the lines as integers and Huffman codes, within a budget | ||||||||||||||||||||||||||||
| 11 | # Tables the tables of ISO 11172-3 | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # This module builds the frames. A frame holds 1152 samples of each channel | ||||||||||||||||||||||||||||
| 14 | # in two granules (MPEG-2: 576, one granule): a header, the side | ||||||||||||||||||||||||||||
| 15 | # information, then the main data. The main data need not fit its own | ||||||||||||||||||||||||||||
| 16 | # frame: room a frame leaves unused is taken by the frames after it (the | ||||||||||||||||||||||||||||
| 17 | # bit reservoir, reaching 511 bytes back, MPEG-2 255), so that a granule | ||||||||||||||||||||||||||||
| 18 | # can have more bits than its share when it needs them. In joint stereo a | ||||||||||||||||||||||||||||
| 19 | # frame whose channels are much alike is coded as their sum and difference | ||||||||||||||||||||||||||||
| 20 | # (mid and side). | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # A granule with an attack is coded in short blocks, so that its noise does | ||||||||||||||||||||||||||||
| 23 | # not spread to before the attack (pre-echo). The granule before it ends in | ||||||||||||||||||||||||||||
| 24 | # a start window, the one after it begins with a stop window. Deciding that | ||||||||||||||||||||||||||||
| 25 | # needs one granule of samples ahead, which the encoder holds back. Both | ||||||||||||||||||||||||||||
| 26 | # channels always have the same block type. | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | # The bitrate is constant, or variable with vbr => 0 .. 9 (as LAME's -V, 0 | ||||||||||||||||||||||||||||
| 29 | # the finest): then every granule is quantized as finely as the model | ||||||||||||||||||||||||||||
| 30 | # allows, and each frame is the smallest whose room, with what the | ||||||||||||||||||||||||||||
| 31 | # reservoir holds, takes its main data. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use Peta::MP3::Encode::Tables; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use Peta::MP3::Encode::Filterbank; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use Peta::MP3::Encode::Psy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use Peta::MP3::Encode::Quantize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use constant SQRT_HALF => 0.7071067811865476; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | use constant DELAY => 528; # samples the encoder delays the sound by | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use constant DECODER => 529; # and a decoder (the filterbank's synthesis) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | use constant AHEAD => 576; # samples held back to see the next granule's attack | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Block types, as the side information has them. | ||||||||||||||||||||||||||||
| 47 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 1 | NORMAL => 0, | |||||||||||||||||||||||||||
| 49 | START => 1, | ||||||||||||||||||||||||||||
| 50 | SHORT => 2, | ||||||||||||||||||||||||||||
| 51 | STOP => 3, | ||||||||||||||||||||||||||||
| 52 | 1 | }; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # The noise each VBR level allows, in dB against what the model allows. | ||||||||||||||||||||||||||||
| 55 | # Chosen so that each level comes out near the bitrate of LAME's -V of the | ||||||||||||||||||||||||||||
| 56 | # same number. | ||||||||||||||||||||||||||||
| 57 | 1 | my @VBR_DB = (-14, -10, -6.5, -4, -2, 1, 3, 5, 7, 9); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | my %MODE = (stereo => 0, joint => 1, mono => 3); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 19 | ▶ | sub new ($class, %arg) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=27 SCAR=33.2
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 19 | ▶ | my $rate = $arg{rate} // 44100; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"rate"} // 44100
MC/DC100%1/1
$arg{"rate"} // 44100
$arg{"rate"}
Truth table100%2/2
A
A= $arg{"rate"}
| |||||||||||||||||||||||||||||
| 63 | 19 | ▶ | my $channels = $arg{channels} // 2; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"channels"} // 2
MC/DC100%1/1
$arg{"channels"} // 2
$arg{"channels"}
Truth table100%2/2
A
A= $arg{"channels"}
| |||||||||||||||||||||||||||||
| 64 | 19 | my $vbr = $arg{vbr}; | |||||||||||||||||||||||||||
| 65 | 19 | ▶ | my $lsf = exists $Peta::MP3::Encode::Tables::LSF_RATE_CODE{$rate} ? 1 : 0; # MPEG-2's low sample rates | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 19 | ▶ | my $bitrate = $arg{bitrate} // ($lsf ? 48 : 96) * $channels; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$arg{"bitrate"} // ($lsf ? 48 : 96) * $channels
MC/DC100%1/1
$arg{"bitrate"} // ($lsf ? 48 : 96) * $channels
$arg{"bitrate"}
Truth table100%2/2
A
A= $arg{"bitrate"}
| |||||||||||||||||||||||||||||
| 67 | 19 | ▶ | my $mode = $channels == 1 ? 'mono' : $arg{mode} // 'joint'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$arg{"mode"} // "joint"
MC/DC0%0/1
$arg{"mode"} // "joint"
$arg{"mode"}
Truth table50%1/2
A
A= $arg{"mode"}
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 19 | ▶ | my ($rate_codes, $bitrates) = $lsf | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | ? (\%Peta::MP3::Encode::Tables::LSF_RATE_CODE, \@Peta::MP3::Encode::Tables::LSF_BITRATES) | ||||||||||||||||||||||||||||
| 71 | : (\%Peta::MP3::Encode::Tables::RATE_CODE, \@Peta::MP3::Encode::Tables::BITRATES); | ||||||||||||||||||||||||||||
| 72 | 19 | my @rates = @$bitrates; | |||||||||||||||||||||||||||
| 73 | 19 | my $rate_code = $rate_codes->{$rate}; | |||||||||||||||||||||||||||
| 74 | 19 | my ($index) = grep { $rates[$_] == $bitrate } 1 .. $#rates; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 19 | ▶ | die "vbr: 0 to 9\n" if defined $vbr && $vbr !~ /\A[0-9]\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $vbr and not $vbr =~ /\A[0-9]\z/u
MC/DC100%2/2
defined $vbr and not $vbr =~ /\A[0-9]\z/u
defined $vbr
not $vbr =~ /\A[0-9]\z/u
Truth table100%3/3
A and not(B)
A= defined $vbrB= not $vbr =~ /\A[0-9]\z/u
| |||||||||||||||||||||||||||||
| 77 | 18 | ▶ | die "Layer III has 32000, 44100 and 48000 Hz (MPEG-1), 16000, 22050 and 24000 Hz (MPEG-2), not $rate\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | if !defined $rate_code; | ||||||||||||||||||||||||||||
| 79 | 17 | ▶ | die "no bitrate of $bitrate kbit/s (have: @rates[1 .. $#rates])\n" if !$index && !defined $vbr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$index or defined $vbr
MC/DC50%1/2
$index or defined $vbr
$index
defined $vbr
Truth table66%2/3
A or B
A= $indexB= defined $vbr
| |||||||||||||||||||||||||||||
| 80 | 15 | ▶ | die "channels: 1 or 2\n" if $channels != 1 && $channels != 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$channels != 1 and $channels != 2
MC/DC0%0/2
$channels != 1 and $channels != 2
$channels != 1
$channels != 2
Truth table66%2/3
A and B
A= $channels != 1B= $channels != 2
| |||||||||||||||||||||||||||||
| 81 | 15 | ▶ | die "mode: stereo, joint or mono\n" if !defined $MODE{$mode} || ($mode eq 'mono') != ($channels == 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $MODE{$mode} or ($mode eq "mono") != ($channels == 1)
MC/DC0%0/2
not defined $MODE{$mode} or ($mode eq "mono") != ($channels == 1)
not defined $MODE{$mode}
($mode eq "mono") != ($channels == 1)
Truth table33%1/3
not(A) or B
A= not defined $MODE{$mode}B= ($mode eq "mono") != ($channels == 1)
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 15 | ▶ | my $bank = _filterbank($arg{filterbank} // 'auto'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"filterbank"} // "auto"
MC/DC100%1/1
$arg{"filterbank"} // "auto"
$arg{"filterbank"}
Truth table100%2/2
A
A= $arg{"filterbank"}
| |||||||||||||||||||||||||||||
| 84 | 15 | ▶ | my $side = $lsf ? ($channels == 1 ? 9 : 17) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 85 | : ($channels == 1 ? 17 : 32); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | return bless { | ||||||||||||||||||||||||||||
| 88 | rate => $rate, | ||||||||||||||||||||||||||||
| 89 | rate_code => $rate_code, | ||||||||||||||||||||||||||||
| 90 | channels => $channels, | ||||||||||||||||||||||||||||
| 91 | mode => $mode, | ||||||||||||||||||||||||||||
| 92 | bitrate => $bitrate, | ||||||||||||||||||||||||||||
| 93 | bitrate_index => $index, | ||||||||||||||||||||||||||||
| 94 | rates => \@rates, | ||||||||||||||||||||||||||||
| 95 | vbr => $vbr, | ||||||||||||||||||||||||||||
| 96 | vbr_scale => defined $vbr ? 10**($VBR_DB[$vbr] / 10) : 1, | ||||||||||||||||||||||||||||
| 97 | name => $arg{ffmpeg_gapless} ? 'LavfPeta' : 'PetaMP3', # see "THE ENCODER'S NAME" | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | lsf => $lsf, | ||||||||||||||||||||||||||||
| 100 | granules => $lsf ? 1 : 2, | ||||||||||||||||||||||||||||
| 101 | frame_samples => $lsf ? 576 : 1152, | ||||||||||||||||||||||||||||
| 102 | side => $side, # bytes of side information | ||||||||||||||||||||||||||||
| 103 | most_back => $lsf ? 255 : 511, # bytes main_data_begin reaches back | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 25 | banks => [ map { $bank->new($rate) } 1 .. $channels ], | |||||||||||||||||||||||||||
| 106 | psy => Peta::MP3::Encode::Psy->new($rate), | ||||||||||||||||||||||||||||
| 107 | quant => Peta::MP3::Encode::Quantize->new($rate), | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 15 | ▶ | pcm => [ map { [] } 1 .. $channels ], # samples not yet coded | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 110 | wants => [], # whether the granules not yet coded have an attack | ||||||||||||||||||||||||||||
| 111 | seen => 0, # how many of those granules have been looked at | ||||||||||||||||||||||||||||
| 112 | type => 0, # the block type of the last granule coded | ||||||||||||||||||||||||||||
| 113 | rest => 0, # what the frames' sizes have left over, for padding | ||||||||||||||||||||||||||||
| 114 | pending => [], # frames whose room for main data is not yet full | ||||||||||||||||||||||||||||
| 115 | offsets => [], # VBR: where each frame given out begins | ||||||||||||||||||||||||||||
| 116 | counts => [], # VBR: the frames at each bitrate index | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | frames => 0, | ||||||||||||||||||||||||||||
| 119 | samples => 0, | ||||||||||||||||||||||||||||
| 120 | bytes => 0, # of the frames given out | ||||||||||||||||||||||||||||
| 121 | shorts => 0, # granules in short blocks | ||||||||||||||||||||||||||||
| 122 | }, $class; | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # The filterbank's class: 'pdl', 'perl', or 'auto' (PDL where it loads). | ||||||||||||||||||||||||||||
| 126 | 15 | ▶ | sub _filterbank ($name) { | ||||||||||||||||||||||||||
|
Subroutine
_filterbank: called CC=4 SCAR=15.9
| |||||||||||||||||||||||||||||
| 127 | 15 | my $fbank = 'Peta::MP3::Encode::Filterbank'; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 15 | ▶ | return $fbank if $name eq 'perl'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 14 | ▶ | return "${fbank}::PDL" if eval { require Peta::MP3::Encode::Filterbank::PDL; 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | return $fbank if $name ne 'pdl'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | die "the PDL filterbank needs PDL: $@"; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub filterbank ($self) { return ref($self->{banks}[0]) =~ /PDL\z/ ? 'pdl' : 'perl' } | ||||||||||||||||||||||||||
|
Subroutine
filterbank: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 8 | ▶ | sub frames ($self) { return $self->{frames} } | ||||||||||||||||||||||||||
|
Subroutine
frames: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub samples ($self) { return $self->{samples} } | ||||||||||||||||||||||||||
|
Subroutine
samples: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 3 | ▶ | sub shorts ($self) { return $self->{shorts} } | ||||||||||||||||||||||||||
|
Subroutine
shorts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub vbr ($self) { return $self->{vbr} } | ||||||||||||||||||||||||||
|
Subroutine
vbr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # The bitrate in kbit/s: the constant one, or the average of the frames so far. | ||||||||||||||||||||||||||||
| 144 | 1 | ▶ | sub bitrate ($self) { | ||||||||||||||||||||||||||
|
Subroutine
bitrate: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | return $self->{bitrate} if !defined $self->{vbr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 1 | ▶ | return 0 if !$self->{frames}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | return 8 * $self->{bytes} * $self->{rate} / ($self->{frame_samples} * $self->{frames}) / 1000; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # Takes samples (-1 .. 1), one array reference per channel. Returns the | ||||||||||||||||||||||||||||
| 152 | # frames they complete, as bytes. | ||||||||||||||||||||||||||||
| 153 | 14 | ▶ | sub encode ($self, @channels) { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 14 | ▶ | die "expected $self->{channels} channel(s)\n" if @channels != $self->{channels}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 14 | my $pcm = $self->{pcm}; | |||||||||||||||||||||||||||
| 157 | 14 | push @{ $pcm->[$_] }, @{ $channels[$_] } for 0 .. $#channels; | |||||||||||||||||||||||||||
| 158 | 14 | $self->{samples} += @{ $channels[0] }; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 14 | return $self->_frames; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # Returns the rest: the samples still held, followed by enough silence to | ||||||||||||||||||||||||||||
| 164 | # come out of a decoder whole, and every frame still waiting. | ||||||||||||||||||||||||||||
| 165 | 14 | ▶ | sub finish ($self) { | ||||||||||||||||||||||||||
|
Subroutine
finish: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 14 | my $pcm = $self->{pcm}; | |||||||||||||||||||||||||||
| 167 | 14 | my $n = $self->{frame_samples}; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # The frames must hold the samples and both delays, and each frame is | ||||||||||||||||||||||||||||
| 170 | # coded with the next granule seen. | ||||||||||||||||||||||||||||
| 171 | 14 | my $frames = int(($self->{samples} + DELAY + DECODER + $n - 1) / $n); | |||||||||||||||||||||||||||
| 172 | 14 | ▶ | $frames = 2 if $frames < 2; # FFmpeg knows MP3 by two frames in a row | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 14 | my $more = ($frames - $self->{frames}) * $n + AHEAD - @{ $pcm->[0] }; | |||||||||||||||||||||||||||
| 174 | 14 | ▶ | if ($more > 0) { push @$_, (0.0) x $more for @$pcm } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 14 | my $out = $self->_frames; | |||||||||||||||||||||||||||
| 177 | 14 | for my $frame (@{ $self->{pending} }) { | |||||||||||||||||||||||||||
| 178 | 24 | $frame->{data} .= "\0" x ($frame->{room} - length $frame->{data}); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 14 | return $out . $self->_flush; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # A frame's bytes at a bitrate index, without padding. | ||||||||||||||||||||||||||||
| 184 | 816 | ▶ | sub _frame_bytes ($self, $index) { | ||||||||||||||||||||||||||
|
Subroutine
_frame_bytes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 185 | 816 | return int($self->{frame_samples} * 125 * $self->{rates}[$index] / $self->{rate}); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # Codes every frame there are samples for (and a granule more to look | ||||||||||||||||||||||||||||
| 189 | # ahead at). Returns the frames completed. | ||||||||||||||||||||||||||||
| 190 | 28 | ▶ | sub _frames ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_frames: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 191 | 28 | my $pcm = $self->{pcm}; | |||||||||||||||||||||||||||
| 192 | 28 | my $n = $self->{frame_samples}; | |||||||||||||||||||||||||||
| 193 | 28 | my $out = ''; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 28 | ▶ | while (@{ $pcm->[0] } >= $n + AHEAD) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 481 | my @frame = map { [ splice @$_, 0, $n ] } @$pcm; | |||||||||||||||||||||||||||
| 197 | 481 | my @ahead = map { [ @$_[ 0 .. AHEAD - 1 ] ] } @$pcm; | |||||||||||||||||||||||||||
| 198 | 481 | $out .= $self->_frame(\@frame, \@ahead); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 28 | return $out; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Codes one frame: $pcm its samples by channel, $ahead the next granule's. | ||||||||||||||||||||||||||||
| 204 | # Returns the frames this one completes. | ||||||||||||||||||||||||||||
| 205 | 481 | ▶ | sub _frame ($self, $pcm, $ahead) { | ||||||||||||||||||||||||||
|
Subroutine
_frame: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 206 | 481 | $self->_find_attacks($pcm, $ahead); | |||||||||||||||||||||||||||
| 207 | 481 | my ($xr, $types) = $self->_spectra($pcm); | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 481 | ▶ | my $ms = $self->{mode} eq 'joint' && _alike($xr); | ||||||||||||||||||||||||||
|
Condition100%3/3
$self->{'mode'} eq 'joint' && _alike($xr)
MC/DC100%2/2
$self->{'mode'} eq 'joint' && _alike($xr)
$self->{'mode'} eq 'joint'
_alike($xr)
Truth table100%3/3
A && B
A= $self->{'mode'} eq 'joint'B= _alike($xr)
| |||||||||||||||||||||||||||||
| 210 | 481 | ▶ | _to_mid_side($xr) if $ms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 481 | my ($index, $padding, $room) = $self->_frame_size; | |||||||||||||||||||||||||||
| 213 | 481 | my $back = 0; # bytes the frames still waiting have free | |||||||||||||||||||||||||||
| 214 | 481 | $back += $_->{room} - length $_->{data} for @{ $self->{pending} }; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 481 | my $gi = $self->_quantize($xr, $types, $room, $back); | |||||||||||||||||||||||||||
| 217 | 481 | my $data = $self->_main_data($gi, $xr); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 481 | ▶ | ($index, $room) = $self->_smallest_frame(length($data) - $back) if defined $self->{vbr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # Room that would stay unused beyond the reservoir's reach is filled now. | ||||||||||||||||||||||||||||
| 222 | 481 | my $unused = $back + $room - length $data; | |||||||||||||||||||||||||||
| 223 | 481 | ▶ | $data .= "\0" x ($unused - $self->{most_back}) if $unused > $self->{most_back}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 481 | my $head = $self->_header($index, $padding, $ms, $back, $gi, $types); | |||||||||||||||||||||||||||
| 226 | 481 | push @{ $self->{pending} }, { head => $head, room => $room, data => '' }; | |||||||||||||||||||||||||||
| 227 | 481 | $self->{counts}[$index]++; | |||||||||||||||||||||||||||
| 228 | 481 | $self->{frames}++; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 481 | $self->_place($data); | |||||||||||||||||||||||||||
| 231 | 481 | return $self->_flush; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | # Whether this frame's granules, and the one after them, have attacks. The | ||||||||||||||||||||||||||||
| 235 | # samples of each granule tell for it and for the next. | ||||||||||||||||||||||||||||
| 236 | 481 | ▶ | sub _find_attacks ($self, $pcm, $ahead) { | ||||||||||||||||||||||||||
|
Subroutine
_find_attacks: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 237 | 481 | my $granules = $self->{granules}; | |||||||||||||||||||||||||||
| 238 | 481 | my $wants = $self->{wants}; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 481 | ▶ | while ($self->{seen} < $granules + 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 705 | my $g = $self->{seen}++; | |||||||||||||||||||||||||||
| 242 | 705 | ▶ | my @samples = $g < $granules ? map { _granule_of($_, $g) } @$pcm : @$ahead; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 705 | my ($early, $late) = $self->{psy}->attack(@samples); | |||||||||||||||||||||||||||
| 244 | 705 | ▶ | $wants->[$g] ||= $early; | ||||||||||||||||||||||||||
|
Condition100%3/3
$wants->[$g] ||= $early
MC/DC100%2/2
$wants->[$g] ||= $early
$wants->[$g]
$early
Truth table100%3/3
A ||= B
A= $wants->[$g]B= $early
| |||||||||||||||||||||||||||||
| 245 | 705 | ▶ | $wants->[$g + 1] ||= $late; | ||||||||||||||||||||||||||
|
Condition66%2/3
$$wants[$g + 1] ||= $late
MC/DC50%1/2
$$wants[$g + 1] ||= $late
$$wants[$g + 1]
$late
Truth table66%2/3
A ||= B
A= $$wants[$g + 1]B= $late
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 481 | return; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # The 576 samples of granule $g. | ||||||||||||||||||||||||||||
| 251 | 1619 | ▶ | sub _granule_of ($samples, $g) { return [ @$samples[ 576 * $g .. 576 * $g + 575 ] ] } | ||||||||||||||||||||||||||
|
Subroutine
_granule_of: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # The granules' block types, and their lines by [granule][channel]. | ||||||||||||||||||||||||||||
| 254 | 481 | ▶ | sub _spectra ($self, $pcm) { | ||||||||||||||||||||||||||
|
Subroutine
_spectra: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 255 | 481 | my $granules = $self->{granules}; | |||||||||||||||||||||||||||
| 256 | 481 | my $wants = $self->{wants}; | |||||||||||||||||||||||||||
| 257 | 481 | my (@xr, @types); | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 481 | for my $gr (0 .. $granules - 1) { | |||||||||||||||||||||||||||
| 260 | 691 | my $type = _block_type($self->{type}, $wants->[$gr], $wants->[$gr + 1]); | |||||||||||||||||||||||||||
| 261 | 691 | $self->{type} = $types[$gr] = $type; | |||||||||||||||||||||||||||
| 262 | 691 | ▶ | $self->{shorts}++ if $type == SHORT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 691 | for my $ch (0 .. $self->{channels} - 1) { | |||||||||||||||||||||||||||
| 265 | 1220 | $xr[$gr][$ch] = $self->{banks}[$ch]->granule(_granule_of($pcm->[$ch], $gr), $type); | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | 481 | splice @$wants, 0, $granules; | |||||||||||||||||||||||||||
| 269 | 481 | $self->{seen} -= $granules; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 481 | return (\@xr, \@types); | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # A granule's block type, from the last granule's and whether this one and | ||||||||||||||||||||||||||||
| 275 | # the next have attacks: short where there is one, a start window before | ||||||||||||||||||||||||||||
| 276 | # short blocks, a stop window after them. | ||||||||||||||||||||||||||||
| 277 | 691 | ▶ | sub _block_type ($last, $now, $next) { | ||||||||||||||||||||||||||
|
Subroutine
_block_type: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 278 | 691 | ▶ | return SHORT if $now; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 663 | ▶ | return SHORT if $last == SHORT && $next; # no window both stops and starts | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$last == 2 and $next
MC/DC0%0/2
$last == 2 and $next
$last == 2
$next
Truth table66%2/3
A and B
A= $last == 2B= $next
| |||||||||||||||||||||||||||||
| 280 | 663 | ▶ | return STOP if $last == SHORT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 635 | ▶ | return START if $next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 619 | return NORMAL; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # Whether the two channels of a frame are alike enough to code as mid and side. | ||||||||||||||||||||||||||||
| 286 | 363 | ▶ | sub _alike ($xr) { | ||||||||||||||||||||||||||
|
Subroutine
_alike: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 287 | 363 | my ($mid, $side) = (0.0, 0.0); | |||||||||||||||||||||||||||
| 288 | 363 | for my $granule (@$xr) { | |||||||||||||||||||||||||||
| 289 | 529 | my ($l, $r) = @$granule; | |||||||||||||||||||||||||||
| 290 | 529 | for my $i (0 .. 575) { | |||||||||||||||||||||||||||
| 291 | 304704 | my $s = $l->[$i] + $r->[$i]; | |||||||||||||||||||||||||||
| 292 | 304704 | my $d = $l->[$i] - $r->[$i]; | |||||||||||||||||||||||||||
| 293 | 304704 | $mid += $s * $s; | |||||||||||||||||||||||||||
| 294 | 304704 | $side += $d * $d; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | 363 | return $side < 0.5 * $mid; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # Left and right become mid and side, in place. | ||||||||||||||||||||||||||||
| 301 | 44 | ▶ | sub _to_mid_side ($xr) { | ||||||||||||||||||||||||||
|
Subroutine
_to_mid_side: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 302 | 44 | for my $granule (@$xr) { | |||||||||||||||||||||||||||
| 303 | 84 | my ($l, $r) = @$granule; | |||||||||||||||||||||||||||
| 304 | 84 | my @mid = map { ($l->[$_] + $r->[$_]) * SQRT_HALF } 0 .. 575; | |||||||||||||||||||||||||||
| 305 | 84 | my @side = map { ($l->[$_] - $r->[$_]) * SQRT_HALF } 0 .. 575; | |||||||||||||||||||||||||||
| 306 | 84 | @$granule = (\@mid, \@side); | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | 44 | return; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | # The frame's bitrate index, its padding (0 or 1), and the bytes of main | ||||||||||||||||||||||||||||
| 312 | # data it has room for. At a constant bitrate a frame is padded by a byte | ||||||||||||||||||||||||||||
| 313 | # now and then, so that the sizes come out right on average. At a variable | ||||||||||||||||||||||||||||
| 314 | # bitrate the size is chosen once the main data is made; until then the | ||||||||||||||||||||||||||||
| 315 | # largest frame's room is what the granules can spend. | ||||||||||||||||||||||||||||
| 316 | 481 | ▶ | sub _frame_size ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_frame_size: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 317 | 481 | my $overhead = 4 + $self->{side}; # the header and the side information | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 481 | ▶ | return (undef, 0, $self->_frame_bytes($#{ $self->{rates} }) - $overhead) if defined $self->{vbr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 341 | my $whole = $self->{frame_samples} * 125 * $self->{bitrate}; | |||||||||||||||||||||||||||
| 322 | 341 | my $padding = 0; | |||||||||||||||||||||||||||
| 323 | 341 | $self->{rest} += $whole % $self->{rate}; | |||||||||||||||||||||||||||
| 324 | 341 | ▶ | if ($self->{rest} >= $self->{rate}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 121 | $padding = 1; | |||||||||||||||||||||||||||
| 326 | 121 | $self->{rest} -= $self->{rate}; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | 341 | return ($self->{bitrate_index}, $padding, int($whole / $self->{rate}) + $padding - $overhead); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | # VBR: the smallest frame whose room takes $need bytes; its index and room. | ||||||||||||||||||||||||||||
| 332 | 140 | ▶ | sub _smallest_frame ($self, $need) { | ||||||||||||||||||||||||||
|
Subroutine
_smallest_frame: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 333 | 140 | my $overhead = 4 + $self->{side}; | |||||||||||||||||||||||||||
| 334 | 140 | my $last = $#{ $self->{rates} }; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 140 | my $index = 1; | |||||||||||||||||||||||||||
| 337 | 140 | ▶ | $index++ while $index < $last && $self->_frame_bytes($index) - $overhead < $need; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$index < $last and $self->_frame_bytes($index) - $overhead < $need
MC/DC100%2/2
$index < $last and $self->_frame_bytes($index) - $overhead < $need
$index < $last
$self->_frame_bytes($index) - $overhead < $need
Truth table100%3/3
A and B
A= $index < $lastB= $self->_frame_bytes($index) - $overhead < $need
| |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 140 | return ($index, $self->_frame_bytes($index) - $overhead); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | # Quantizes the granules: [granule][channel] of what Quantize returns. | ||||||||||||||||||||||||||||
| 343 | # | ||||||||||||||||||||||||||||
| 344 | # Each granule gets its share of the frame, and of what the reservoir holds; | ||||||||||||||||||||||||||||
| 345 | # what a granule leaves, those after it can use. At a constant bitrate, what | ||||||||||||||||||||||||||||
| 346 | # would otherwise be left beyond the reservoir's reach must be used: finer | ||||||||||||||||||||||||||||
| 347 | # quantization than the model asks for, rather than bits thrown away. (At a | ||||||||||||||||||||||||||||
| 348 | # variable bitrate a smaller frame is chosen instead.) | ||||||||||||||||||||||||||||
| 349 | 481 | ▶ | sub _quantize ($self, $xr, $types, $room, $back) { | ||||||||||||||||||||||||||
|
Subroutine
_quantize: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 350 | 481 | my $granules = $self->{granules}; | |||||||||||||||||||||||||||
| 351 | 481 | my $channels = $self->{channels}; | |||||||||||||||||||||||||||
| 352 | 481 | my $count = $granules * $channels; | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 481 | my $share = 8 * $room / $count; | |||||||||||||||||||||||||||
| 355 | 481 | my $left = 8 * ($room + $back); | |||||||||||||||||||||||||||
| 356 | 481 | ▶ | my $must = defined $self->{vbr} ? 0 : 8 * ($room + $back - $self->{most_back}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | 481 | my @gi; | |||||||||||||||||||||||||||
| 358 | 481 | my $n = 0; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 481 | for my $gr (0 .. $granules - 1) { | |||||||||||||||||||||||||||
| 361 | 691 | for my $ch (0 .. $channels - 1) { | |||||||||||||||||||||||||||
| 362 | 1220 | my $rest = $count - $n; # granules still to code, this one too | |||||||||||||||||||||||||||
| 363 | 1220 | my $spare = $left - $share * $rest; | |||||||||||||||||||||||||||
| 364 | 1220 | ▶ | my $budget = int($share + ($spare > 0 ? $spare / $rest : 0)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 1220 | ▶ | $budget = $left if $budget > $left; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 1220 | ▶ | my $least = $must > 0 ? int($must / $rest) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 1220 | my $allowed = $self->{psy}->allowed($xr->[$gr][$ch], $types->[$gr]); | |||||||||||||||||||||||||||
| 369 | 1220 | ▶ | if ($self->{vbr_scale} != 1) { $_ *= $self->{vbr_scale} for @$allowed } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 1220 | ▶ | my $first = $gr ? $gi[0][$ch] : undef; # for scfsi | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 1220 | my $g = $self->{quant}->granule($xr->[$gr][$ch], $allowed, $budget, $least, $types->[$gr], $first); | |||||||||||||||||||||||||||
| 373 | 1220 | $left -= $g->{part2_3_length}; | |||||||||||||||||||||||||||
| 374 | 1220 | $must -= $g->{part2_3_length}; | |||||||||||||||||||||||||||
| 375 | 1220 | $gi[$gr][$ch] = $g; | |||||||||||||||||||||||||||
| 376 | 1220 | $n++; | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | 481 | return \@gi; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # The frame's main data, as bytes. | ||||||||||||||||||||||||||||
| 383 | 481 | ▶ | sub _main_data ($self, $gi, $xr) { | ||||||||||||||||||||||||||
|
Subroutine
_main_data: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 384 | 481 | my $main = Peta::MP3::Encode::Bits->new; | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 481 | for my $gr (0 .. $self->{granules} - 1) { | |||||||||||||||||||||||||||
| 387 | 691 | for my $ch (0 .. $self->{channels} - 1) { | |||||||||||||||||||||||||||
| 388 | 1220 | my $before = $main->bits; | |||||||||||||||||||||||||||
| 389 | 1220 | $self->{quant}->write($gi->[$gr][$ch], $main, $xr->[$gr][$ch]); | |||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 1220 | my $wrote = $main->bits - $before; | |||||||||||||||||||||||||||
| 392 | 1220 | my $counted = $gi->[$gr][$ch]{part2_3_length}; | |||||||||||||||||||||||||||
| 393 | 1220 | ▶ | die "internal: granule $gr channel $ch wrote $wrote bits, counted $counted\n" if $wrote != $counted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | 481 | return $main->bytes; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | # The frame's header and side information, as bytes. | ||||||||||||||||||||||||||||
| 400 | 481 | ▶ | sub _header ($self, $index, $padding, $ms, $back, $gi, $types) { | ||||||||||||||||||||||||||
|
Subroutine
_header: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||
| 401 | 481 | my $lsf = $self->{lsf}; | |||||||||||||||||||||||||||
| 402 | 481 | my $channels = $self->{channels}; | |||||||||||||||||||||||||||
| 403 | 481 | my $head = Peta::MP3::Encode::Bits->new; | |||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 481 | ▶ | $head->put($lsf ? 0xFFF3 : 0xFFFB, 16); # sync, MPEG-1 or 2, Layer III, no CRC | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | 481 | $head->put($index, 4); | |||||||||||||||||||||||||||
| 407 | 481 | $head->put($self->{rate_code}, 2); | |||||||||||||||||||||||||||
| 408 | 481 | $head->put($padding, 1); | |||||||||||||||||||||||||||
| 409 | 481 | $head->put(0, 1); # private | |||||||||||||||||||||||||||
| 410 | 481 | $head->put($MODE{ $self->{mode} }, 2); | |||||||||||||||||||||||||||
| 411 | 481 | ▶ | $head->put($ms ? 2 : 0, 2); # mode extension: mid and side or not | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | 481 | $head->put(0, 1); # copyright | |||||||||||||||||||||||||||
| 413 | 481 | $head->put(1, 1); # original | |||||||||||||||||||||||||||
| 414 | 481 | $head->put(0, 2); # emphasis | |||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 481 | ▶ | if ($lsf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | 271 | $head->put($back, 8); # main_data_begin | |||||||||||||||||||||||||||
| 418 | 271 | $head->put(0, $channels); # private | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | else { | ||||||||||||||||||||||||||||
| 421 | 210 | $head->put($back, 9); # main_data_begin | |||||||||||||||||||||||||||
| 422 | 210 | ▶ | $head->put(0, $channels == 1 ? 5 : 3); # private | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | 210 | $head->put(oct('0b' . join '', @{ $gi->[1][$_]{scfsi} }), 4) for 0 .. $channels - 1; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 481 | for my $gr (0 .. $self->{granules} - 1) { | |||||||||||||||||||||||||||
| 427 | 691 | for my $ch (0 .. $channels - 1) { | |||||||||||||||||||||||||||
| 428 | 1220 | my $g = $gi->[$gr][$ch]; | |||||||||||||||||||||||||||
| 429 | 1220 | $head->put($g->{part2_3_length}, 12); | |||||||||||||||||||||||||||
| 430 | 1220 | $head->put($g->{big_values}, 9); | |||||||||||||||||||||||||||
| 431 | 1220 | $head->put($g->{global_gain}, 8); | |||||||||||||||||||||||||||
| 432 | 1220 | ▶ | $head->put($g->{scalefac_compress}, $lsf ? 9 : 4); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 1220 | ▶ | if ($types->[$gr] == NORMAL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | 1129 | $head->put(0, 1); # no window switching | |||||||||||||||||||||||||||
| 436 | 1129 | $head->put($_, 5) for @{ $g->{table_select} }; | |||||||||||||||||||||||||||
| 437 | 1129 | $head->put($g->{region0_count}, 4); | |||||||||||||||||||||||||||
| 438 | 1129 | $head->put($g->{region1_count}, 3); | |||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | else { | ||||||||||||||||||||||||||||
| 441 | 91 | $head->put(1, 1); # window switching | |||||||||||||||||||||||||||
| 442 | 91 | $head->put($types->[$gr], 2); | |||||||||||||||||||||||||||
| 443 | 91 | $head->put(0, 1); # not mixed | |||||||||||||||||||||||||||
| 444 | 91 | $head->put($_, 5) for @{ $g->{table_select} }[ 0, 1 ]; | |||||||||||||||||||||||||||
| 445 | 91 | $head->put($_, 3) for @{ $g->{subblock_gain} }; | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | 1220 | ▶ | $head->put($g->{preflag}, 1) if !$lsf; # MPEG-2 has it in scalefac_compress | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 449 | 1220 | $head->put($g->{scalefac_scale}, 1); | |||||||||||||||||||||||||||
| 450 | 1220 | $head->put($g->{count1table}, 1); | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | 481 | return $head->bytes; | |||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | # Puts the main data into the rooms still free, oldest frame first. | ||||||||||||||||||||||||||||
| 457 | 481 | ▶ | sub _place ($self, $data) { | ||||||||||||||||||||||||||
|
Subroutine
_place: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 458 | 481 | for my $frame (@{ $self->{pending} }) { | |||||||||||||||||||||||||||
| 459 | 1185 | ▶ | last if $data eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 920 | my $free = $frame->{room} - length $frame->{data}; | |||||||||||||||||||||||||||
| 462 | 920 | ▶ | next if $free <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | 920 | $frame->{data} .= substr $data, 0, $free, ''; | |||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | 481 | ▶ | die "internal: main data beyond the frame\n" if $data ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 467 | 481 | return; | |||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | # Returns the frames whose rooms are full, in order. | ||||||||||||||||||||||||||||
| 471 | 495 | ▶ | sub _flush ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_flush: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 472 | 495 | my $pending = $self->{pending}; | |||||||||||||||||||||||||||
| 473 | 495 | my $out = ''; | |||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 495 | ▶ | while (@$pending && length $pending->[0]{data} == $pending->[0]{room}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$pending and length $pending->[0]{"data"} == $pending->[0]{"room"}
MC/DC100%2/2
@$pending and length $pending->[0]{"data"} == $pending->[0]{"room"}
@$pending
length $pending->[0]{"data"} == $pending->[0]{"room"}
Truth table100%3/3
A and B
A= @$pendingB= length $pending->[0]{"data"} == $pending->[0]{"room"}
| |||||||||||||||||||||||||||||
| 476 | 481 | my $frame = shift @$pending; | |||||||||||||||||||||||||||
| 477 | 481 | ▶ | push @{ $self->{offsets} }, $self->{bytes} + length $out if defined $self->{vbr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 478 | 481 | $out .= $frame->{head} . $frame->{data}; | |||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | 495 | $self->{bytes} += length $out; | |||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 495 | return $out; | |||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | # The frame that goes before all the others: a frame of silence whose main | ||||||||||||||||||||||||||||
| 486 | # data is an Info tag (Xing at a variable bitrate) with the LAME extension. | ||||||||||||||||||||||||||||
| 487 | # The tag gives the frames, the bytes and a table for seeking; the extension | ||||||||||||||||||||||||||||
| 488 | # says how many samples the encoder put before and after the sound, so that | ||||||||||||||||||||||||||||
| 489 | # a decoder can give back exactly the samples encoded. | ||||||||||||||||||||||||||||
| 490 | # | ||||||||||||||||||||||||||||
| 491 | # Its numbers are those of the frames given out so far: write it once at | ||||||||||||||||||||||||||||
| 492 | # the start, to keep its place, and again when all is encoded. The frame | ||||||||||||||||||||||||||||
| 493 | # has the stream's bitrate, or at a variable bitrate the smallest that | ||||||||||||||||||||||||||||
| 494 | # holds the tag. | ||||||||||||||||||||||||||||
| 495 | 10 | ▶ | sub info_frame ($self) { | ||||||||||||||||||||||||||
|
Subroutine
info_frame: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 496 | 10 | my $vbr = defined $self->{vbr}; | |||||||||||||||||||||||||||
| 497 | 10 | my $side = $self->{side}; | |||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | 10 | ▶ | my $index = $vbr ? 1 : $self->{bitrate_index}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 500 | 10 | ▶ | $index++ while $index < $#{ $self->{rates} } && $self->_frame_bytes($index) < 4 + $side + 156; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$index < $#{$$self{"rates"};} and $self->_frame_bytes($index) < 4 + $side + 156
MC/DC50%1/2
$index < $#{$$self{"rates"};} and $self->_frame_bytes($index) < 4 + $side + 156
$index < $#{$$self{"rates"};}
$self->_frame_bytes($index) < 4 + $side + 156
Truth table66%2/3
A and B
A= $index < $#{$$self{"rates"};}B= $self->_frame_bytes($index) < 4 + $side + 156
| |||||||||||||||||||||||||||||
| 501 | 10 | my $bytes = $self->_frame_bytes($index); | |||||||||||||||||||||||||||
| 502 | 10 | my $total = $bytes + $self->{bytes}; | |||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | my $head = pack 'n C C', | ||||||||||||||||||||||||||||
| 505 | $self->{lsf} ? 0xFFF3 : 0xFFFB, # sync, MPEG-1 or 2, Layer III, no CRC | ||||||||||||||||||||||||||||
| 506 | ($index << 4) | ($self->{rate_code} << 2), # bitrate, sample rate, no padding | ||||||||||||||||||||||||||||
| 507 | 10 | ▶ | ($MODE{ $self->{mode} } << 6) | 4; # mode, no extension, original | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | my @toc = $vbr ? $self->_seek_table($bytes, $total) | ||||||||||||||||||||||||||||
| 510 | 10 | ▶ | : map { int($_ * 256 / 100) } 0 .. 99; # constant bitrate: a straight line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 511 | my $tag = join '', | ||||||||||||||||||||||||||||
| 512 | $vbr ? 'Xing' : 'Info', | ||||||||||||||||||||||||||||
| 513 | pack('N', 0x0F), # frames, bytes, seek table and quality follow | ||||||||||||||||||||||||||||
| 514 | pack('N', $self->{frames}), | ||||||||||||||||||||||||||||
| 515 | pack('N', $total), | ||||||||||||||||||||||||||||
| 516 | pack('C100', @toc), | ||||||||||||||||||||||||||||
| 517 | 10 | ▶ | pack('N', $vbr ? 100 - 10 * $self->{vbr} : 0); # quality, as LAME gives it | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 10 | my $padding = $self->{frames} * $self->{frame_samples} - DELAY - $self->{samples}; | |||||||||||||||||||||||||||
| 520 | 10 | ▶ | $padding = 0 if $padding < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | my $lame = join '', | ||||||||||||||||||||||||||||
| 523 | 10 | ▶ | sprintf('%-9.9s', $self->{name}), # the encoder | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 524 | chr($vbr ? 0x04 : 0x01), # tag revision 0; VBR (new) or constant bitrate | ||||||||||||||||||||||||||||
| 525 | chr(0), # lowpass: not given | ||||||||||||||||||||||||||||
| 526 | pack('N', 0), # peak amplitude: not given | ||||||||||||||||||||||||||||
| 527 | pack('n n', 0, 0), # replay gains: not given | ||||||||||||||||||||||||||||
| 528 | chr(0), # encoding flags, ATH type | ||||||||||||||||||||||||||||
| 529 | chr(_byte($self->_least_bitrate)), # the bitrate; at a variable one the least | ||||||||||||||||||||||||||||
| 530 | substr(pack('N', (DELAY << 12) | $padding), 1), # encoder delay and padding, 12 bits each | ||||||||||||||||||||||||||||
| 531 | chr(0), # misc | ||||||||||||||||||||||||||||
| 532 | chr(0), # MP3 gain | ||||||||||||||||||||||||||||
| 533 | pack('n', 0), # preset and surround | ||||||||||||||||||||||||||||
| 534 | pack('N', $total), # music length | ||||||||||||||||||||||||||||
| 535 | pack('n', 0); # music CRC | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | 10 | my $frame = $head . ("\0" x $side) . $tag . $lame; | |||||||||||||||||||||||||||
| 538 | 10 | my $need = length($frame) + 2; | |||||||||||||||||||||||||||
| 539 | 10 | ▶ | die "internal: the tag takes $need bytes, the frame has $bytes\n" if $need > $bytes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | 10 | $frame .= pack 'n', _crc16($frame); # of all before it: 190 bytes in stereo | |||||||||||||||||||||||||||
| 542 | 10 | return $frame . "\0" x ($bytes - length $frame); | |||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | # VBR: where each hundredth of the playing time begins, in 256ths of the | ||||||||||||||||||||||||||||
| 546 | # file. $before is the Info frame's size, $total the file's. | ||||||||||||||||||||||||||||
| 547 | 4 | ▶ | sub _seek_table ($self, $before, $total) { | ||||||||||||||||||||||||||
|
Subroutine
_seek_table: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 548 | 4 | my $offsets = $self->{offsets}; | |||||||||||||||||||||||||||
| 549 | 4 | my @toc; | |||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | 4 | for my $percent (0 .. 99) { | |||||||||||||||||||||||||||
| 552 | 400 | ▶ | my $at = @$offsets ? $before + $offsets->[ int($percent * @$offsets / 100) ] : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 553 | 400 | push @toc, _byte(int(256 * $at / $total)); | |||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | 4 | return @toc; | |||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | # The bitrate for the LAME tag: the constant one, or the least used. | ||||||||||||||||||||||||||||
| 559 | 10 | ▶ | sub _least_bitrate ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_least_bitrate: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 560 | 10 | ▶ | return $self->{bitrate} if !defined $self->{vbr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | 4 | my ($least) = grep { $self->{counts}[$_] } 1 .. $#{ $self->{rates} }; | |||||||||||||||||||||||||||
| 563 | 4 | ▶ | return $least ? $self->{rates}[$least] : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | 410 | ▶ | sub _byte ($n) { return $n > 255 ? 255 : $n } | ||||||||||||||||||||||||||
|
Subroutine
_byte: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | # CRC-16 as the LAME tag has it: polynomial 0x8005, bits reflected. | ||||||||||||||||||||||||||||
| 569 | 10 | ▶ | sub _crc16 ($bytes) { | ||||||||||||||||||||||||||
|
Subroutine
_crc16: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 570 | 10 | my $crc = 0; | |||||||||||||||||||||||||||
| 571 | 10 | for my $byte (unpack 'C*', $bytes) { | |||||||||||||||||||||||||||
| 572 | 1855 | $crc ^= $byte; | |||||||||||||||||||||||||||
| 573 | 1855 | ▶ | for (1 .. 8) { $crc = $crc & 1 ? ($crc >> 1) ^ 0xA001 : $crc >> 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | 10 | return $crc; | |||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | # Bits written most significant first, into bytes. | ||||||||||||||||||||||||||||
| 579 | 1 | package Peta::MP3::Encode::Bits { | |||||||||||||||||||||||||||
| 580 | 962 | ▶ | sub new ($class) { return bless { bytes => '', acc => 0, count => 0, bits => 0 }, $class } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 201663 | ▶ | sub put ($self, $value, $width) { | ||||||||||||||||||||||||||
|
Subroutine
put: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 583 | 201663 | ▶ | return if !$width; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | 190945 | $self->{acc} = ($self->{acc} << $width) | ($value & ((1 << $width) - 1)); | |||||||||||||||||||||||||||
| 586 | 190945 | $self->{count} += $width; | |||||||||||||||||||||||||||
| 587 | 190945 | $self->{bits} += $width; | |||||||||||||||||||||||||||
| 588 | 190945 | ▶ | while ($self->{count} >= 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 589 | 75445 | $self->{count} -= 8; | |||||||||||||||||||||||||||
| 590 | 75445 | $self->{bytes} .= chr(($self->{acc} >> $self->{count}) & 255); | |||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||
| 592 | 190945 | $self->{acc} &= (1 << $self->{count}) - 1; | |||||||||||||||||||||||||||
| 593 | 190945 | return; | |||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | 2440 | ▶ | sub bits ($self) { return $self->{bits} } | ||||||||||||||||||||||||||
|
Subroutine
bits: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | # Everything written, the last byte filled up with zeros. | ||||||||||||||||||||||||||||
| 599 | 962 | ▶ | sub bytes ($self) { | ||||||||||||||||||||||||||
|
Subroutine
bytes: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 600 | 962 | ▶ | return $self->{bytes} if !$self->{count}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 601 | 375 | return $self->{bytes} . chr(($self->{acc} << (8 - $self->{count})) & 255); | |||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||
| 603 | } | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | 1 | 1; | |||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | __END__ | ||||||||||||||||||||||||||||