lib/Peta/Vorbis/Encode/Ogg.pm

filter: Statementstmt 94.5% 69 / 73 Branchbran 76.6% 23 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 89.8% 106 / 118 CC 18 SCAR 16.0
CC
18
Cov
89%
packet
9.8
_page
4.0
put
4.0
CRAP 4.9
« lib/Peta/MP3/Encode/Filterbank.pm ↑ Summary lib/Peta/Synth/EQ.pm »
1 package Peta::Vorbis::Encode::Ogg;
2 # ABSTRACT: Ogg pages around Vorbis packets
3  
4 # Ogg pages around packets (RFC 3533): a header of 27 bytes ("OggS",
5 # version, flags, granule position, stream serial, page number, CRC, the
6 # number of segments), the segment table (each packet as segments of 255
7 # bytes and a last shorter one), the packets' bytes. The CRC is the
8 # CRC-32 of the polynomial 0x04C11DB7, not reflected, over the page with
9 # its own field zero.
10 #
11 # A page's granule position is that of the last packet finished on it
12 # (-1 when none is); for Vorbis, the number of samples decoded through it.
13  
14 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = '0.2610180';
17  
18 # The header type flags; and how full a page gets before it goes out.
19 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 CONTINUED => 1, # the page begins with the rest of a packet
21 FIRST => 2, # the stream's first page
22 LAST => 4, # its last
23 PAGE_BYTES => 4096,
24 1 };
25  
26 # The CRC of each byte value, for a byte at a time.
27 1 my @CRC;
28 1 for my $i (0 .. 255) {
29 256 my $r = $i << 24;
30 256 for (1 .. 8) {
31 2048 ▶ $r = $r & 0x80000000 ? ($r << 1) ^ 0x04C11DB7 : $r << 1;
Branch: 1 branch
BranchTrueFalseTotal
$r & 2147483648 ? : 1024 1024 2048
32 2048 $r &= 0xFFFFFFFF;
33 }
34 256 $CRC[$i] = $r;
35 }
36  
37 12 ▶sub crc ($bytes) {
Subroutine crc: called CC=2 SCAR=6.9
Pod documented
38 12 my $crc = 0;
39 12 for my $byte (unpack 'C*', $bytes) {
40 14448 $crc = (($crc << 8) & 0xFFFFFFFF) ^ $CRC[ (($crc >> 24) ^ $byte) & 0xFF ];
41 }
42 12 return $crc;
43 }
44  
45 2 ▶sub new ($class, $serial) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
46 2 return bless {
47 serial => $serial,
48 page => 0, # the next page's number
49 segments => [], # the lacing values of the page being made
50 body => '', # and its bytes
51 granule => -1, # its granule position
52 first => 1, # whether it is the stream's first
53 }, $class;
54 }
55  
56 # A packet onto the page being made: its segments, and the granule position
57 # once it is finished. Returns the pages it filled; with $last, the stream's
58 # last page too, which ends with this packet.
59 70 ▶sub packet ($self, $bytes, $granule, $last = 0) {
Subroutine packet: called CC=9 SCAR=22.8
Pod documented
60 70 my $out = '';
61 70 my @lacing = ((255) x int(length($bytes) / 255), length($bytes) % 255);
62 70 ▶ while (@lacing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lacing) 70 70 140
63 70 my $room = 255 - @{ $self->{segments} };
64 0 ▶ if ($room == 0) { $out .= $self->_page(0); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($room == 0) 0 70 70
65 70 my @take = splice @lacing, 0, $room;
66 70 my $len = 0;
67 70 $len += $_ for @take;
68 70 push @{ $self->{segments} }, @take;
69 70 $self->{body} .= substr $bytes, 0, $len, '';
70 70 ▶ $self->{granule} = $granule if !@lacing; # the packet ends on this page
Branch: 1 branch
BranchTrueFalseTotal
unless @lacing 70 0 70
71 70 ▶ $self->{continued_next} = @lacing ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@lacing ? : 0 70 70
72 }
73 70 ▶ if ($last) { $out .= $self->_page(1) }
Branch: 2 branches
BranchTrueFalseTotal
if ($last) { } 2 68 70
elsif (length $self->{'body'} >= 4096) { } 0 68 68
74 0 elsif (length $self->{body} >= PAGE_BYTES) { $out .= $self->_page(0) }
75 70 return $out;
76 }
77  
78 # The page being made, out now (the headers end their pages).
79 6 ▶sub flush ($self) {
Subroutine flush: called CC=2 SCAR=7.1
Pod documented
80 6 ▶ return '' if !@{ $self->{segments} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"segments"};} 0 6 6
81 6 return $self->_page(0);
82 }
83  
84 8 ▶sub _page ($self, $last) {
Subroutine _page: called CC=4 SCAR=13.9
85 my $flags = ($self->{continuing} ? CONTINUED : 0)
86 8 ▶ | ($self->{first} ? FIRST : 0)
Branch: 3 branches
BranchTrueFalseTotal
$self->{'continuing'} ? : 0 8 8
$self->{'first'} ? : 2 6 8
$last ? : 2 6 8
87 | ($last ? LAST : 0);
88 8 my @segments = @{ $self->{segments} };
89  
90 my $page = join '',
91 'OggS',
92 pack('C C', 0, $flags), # version, flags
93 pack('q<', $self->{granule}),
94 pack('V V V', $self->{serial}, $self->{page}++, 0), # the CRC, 0 while it is computed
95 pack('C', scalar @segments),
96 pack('C*', @segments),
97 8 $self->{body};
98 8 substr($page, 22, 4) = pack 'V', crc($page);
99  
100 8 $self->{continuing} = $self->{continued_next};
101 8 $self->{continued_next} = 0;
102 8 $self->{first} = 0;
103 8 $self->{segments} = [];
104 8 $self->{body} = '';
105 8 $self->{granule} = -1;
106 8 return $page;
107 }
108  
109 # Bits packed as Vorbis packs them: from the least significant bit of each
110 # byte up.
111 1 package Peta::Vorbis::Encode::Bits {
112 70 ▶ sub new ($class) { return bless { bytes => '', acc => 0, count => 0, bits => 0 }, $class }
Subroutine new: called CC=1 SCAR=0.0
113  
114 17988 ▶ sub put ($self, $value, $width) {
Subroutine put: called CC=4 SCAR=13.9
115 17988 ▶ return if !$width;
Branch: 1 branch
BranchTrueFalseTotal
unless $width 0 17988 17988
116 17988 ▶ while ($width > 24) { # wide values in two pieces
Branch: 1 branch
BranchTrueFalseTotal
if ($width > 24) 42 17988 18030
117 42 $self->put($value & 0xFFFF, 16);
118 42 $value >>= 16;
119 42 $width -= 16;
120 }
121 17988 $self->{acc} |= ($value & ((1 << $width) - 1)) << $self->{count};
122 17988 $self->{count} += $width;
123 17988 $self->{bits} += $width;
124 17988 ▶ while ($self->{count} >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"count"} >= 8) 9274 17988 27262
125 9274 $self->{bytes} .= chr($self->{acc} & 0xFF);
126 9274 $self->{acc} >>= 8;
127 9274 $self->{count} -= 8;
128 }
129 17988 return;
130 }
131  
132 0 ▶ sub bits ($self) { return $self->{bits} }
Subroutine bits: not called CC=1 SCAR=6.9
133  
134 # Everything written, the last byte filled up with zeros.
135 70 ▶ sub bytes ($self) {
Subroutine bytes: called CC=2 SCAR=6.9
136 70 ▶ return $self->{bytes} if !$self->{count};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"count"} 8 62 70
137 62 return $self->{bytes} . chr($self->{acc} & 0xFF);
138 }
139 }
140  
141 1 1;
142  
143 __END__
« lib/Peta/MP3/Encode/Filterbank.pm ↑ Summary lib/Peta/Synth/EQ.pm »