| 1 | package Ogg::Vorbis::Header::PurePerl; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # First four bytes of stream are always OggS | ||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use constant OGGHEADERFLAG => 'OggS'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '1.05'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 14 | 5 | my $file = shift; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | my %data = (); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | if (ref $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 1 | binmode $file; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | %data = ( | |||||||||||||||||||||||||||
| 22 | 'filesize' => -s $file, | ||||||||||||||||||||||||||||
| 23 | 'fileHandle' => $file, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | } else { | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | open my $fh, '<', $file or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 1 | warn "$class: File $file does not exist or cannot be read: $!"; | |||||||||||||||||||||||||||
| 30 | 1 | return undef; | |||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # make sure dos-type systems can handle it... | ||||||||||||||||||||||||||||
| 34 | 3 | binmode $fh; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | %data = ( | |||||||||||||||||||||||||||
| 37 | 'filename' => $file, | ||||||||||||||||||||||||||||
| 38 | 'filesize' => -s $file, | ||||||||||||||||||||||||||||
| 39 | 'fileHandle' => $fh, | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | if ( _init(\%data) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 3 | _load_info(\%data); | |||||||||||||||||||||||||||
| 45 | 3 | _load_comments(\%data); | |||||||||||||||||||||||||||
| 46 | 3 | _calculate_track_length(\%data); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | undef $data{'fileHandle'}; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | return bless \%data, $class; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | ▶ | sub info { | ||||||||||||||||||||||||||
|
Subroutine
info: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | 3 | my $key = shift; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # if the user did not supply a key, return the entire hash | ||||||||||||||||||||||||||||
| 59 | 3 | ▶ | return $self->{'INFO'} unless $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # otherwise, return the value for the given key | ||||||||||||||||||||||||||||
| 62 | 1 | return $self->{'INFO'}{lc $key}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | sub comment_tags { | ||||||||||||||||||||||||||
|
Subroutine
comment_tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | my %keys = (); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | return grep { !$keys{$_}++ } @{$self->{'COMMENT_KEYS'}}; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | sub comment { | ||||||||||||||||||||||||||
|
Subroutine
comment: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 4 | my $key = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # if the user supplied key does not exist, return undef | ||||||||||||||||||||||||||||
| 78 | 4 | ▶ | return undef unless($self->{'COMMENTS'}{lc $key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | return wantarray | ||||||||||||||||||||||||||||
| 81 | 1 | ? @{$self->{'COMMENTS'}{lc $key}} | |||||||||||||||||||||||||||
| 82 | 4 | ▶ | : $self->{'COMMENTS'}{lc $key}->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | sub path { | ||||||||||||||||||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | return $self->{'filename'}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # "private" methods | ||||||||||||||||||||||||||||
| 92 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | 4 | my $data = shift; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # check the header to make sure this is actually an Ogg-Vorbis file | ||||||||||||||||||||||||||||
| 96 | 4 | ▶ | $data->{'startInfoHeader'} = _check_header($data) || return undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
_check_header($data) || (return undef)
MC/DC100%1/1
_check_header($data) || (return undef)
_check_header($data)
Truth table100%2/2
A
A= _check_header($data)
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | return 1; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | ▶ | sub _skip_id3_header { | ||||||||||||||||||||||||||
|
Subroutine
_skip_id3_header: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 102 | 4 | my $fh = shift; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | read $fh, my $buffer, 3; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 4 | my $byte_count = 3; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 4 | ▶ | if ($buffer eq 'ID3') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | ▶ | while (read $fh, $buffer, 4096) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 13 | my $found; | |||||||||||||||||||||||||||
| 113 | 13 | ▶ | if (($found = index($buffer, OGGHEADERFLAG)) >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $byte_count += $found; | |||||||||||||||||||||||||||
| 115 | 0 | seek $fh, $byte_count, 0; | |||||||||||||||||||||||||||
| 116 | 0 | last; | |||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 13 | $byte_count += 4096; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||
| 123 | 3 | seek $fh, 0, 0; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 4 | return tell($fh); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 4 | ▶ | sub _check_header { | ||||||||||||||||||||||||||
|
Subroutine
_check_header: called CC=6 SCAR=19.0
| |||||||||||||||||||||||||||||
| 130 | 4 | my $data = shift; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | my $fh = $data->{'fileHandle'}; | |||||||||||||||||||||||||||
| 133 | 4 | my $buffer; | |||||||||||||||||||||||||||
| 134 | my $page_seg_count; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # stores how far into the file we've read, so later reads into the file can | ||||||||||||||||||||||||||||
| 137 | # skip right past all of the header stuff | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 4 | my $byte_count = _skip_id3_header($fh); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Remember the start of the Ogg data | ||||||||||||||||||||||||||||
| 142 | 4 | $data->{startHeader} = $byte_count; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # check that the first four bytes are 'OggS' | ||||||||||||||||||||||||||||
| 145 | 4 | read($fh, $buffer, 27); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | ▶ | if (substr($buffer, 0, 4) ne OGGHEADERFLAG) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 1 | warn "This is not an Ogg bitstream (no OggS header)."; | |||||||||||||||||||||||||||
| 149 | 1 | return undef; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 3 | $byte_count += 4; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # check the stream structure version (1 byte, should be 0x00) | ||||||||||||||||||||||||||||
| 155 | 3 | ▶ | if (ord(substr($buffer, 4, 1)) != 0x00) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | warn "This is not an Ogg bitstream (invalid structure version)."; | |||||||||||||||||||||||||||
| 157 | 0 | return undef; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 3 | $byte_count += 1; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # check the header type flag | ||||||||||||||||||||||||||||
| 163 | # This is a bitfield, so technically we should check all of the bits | ||||||||||||||||||||||||||||
| 164 | # that could potentially be set. However, the only value this should | ||||||||||||||||||||||||||||
| 165 | # possibly have at the beginning of a proper Ogg-Vorbis file is 0x02, | ||||||||||||||||||||||||||||
| 166 | # so we just check for that. If it's not that, we go on anyway, but | ||||||||||||||||||||||||||||
| 167 | # give a warning (this behavior may (should?) be modified in the future. | ||||||||||||||||||||||||||||
| 168 | 3 | ▶ | if (ord(substr($buffer, 5, 1)) != 0x02) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | warn "Invalid header type flag (trying to go ahead anyway)."; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 3 | $byte_count += 1; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # read the number of page segments | ||||||||||||||||||||||||||||
| 175 | 3 | $page_seg_count = ord(substr($buffer, 26, 1)); | |||||||||||||||||||||||||||
| 176 | 3 | $byte_count += 21; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # read $page_seg_count bytes, then throw 'em out | ||||||||||||||||||||||||||||
| 179 | 3 | seek($fh, $page_seg_count, 1); | |||||||||||||||||||||||||||
| 180 | 3 | $byte_count += $page_seg_count; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # check packet type. Should be 0x01 (for indentification header) | ||||||||||||||||||||||||||||
| 183 | 3 | read($fh, $buffer, 7); | |||||||||||||||||||||||||||
| 184 | 3 | ▶ | if (ord(substr($buffer, 0, 1)) != 0x01) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | warn "Wrong vorbis header type, giving up."; | |||||||||||||||||||||||||||
| 186 | 0 | return undef; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 3 | $byte_count += 1; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # check that the packet identifies itself as 'vorbis' | ||||||||||||||||||||||||||||
| 192 | 3 | ▶ | if (substr($buffer, 1, 6) ne 'vorbis') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | warn "This does not appear to be a vorbis stream, giving up."; | |||||||||||||||||||||||||||
| 194 | 0 | return undef; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 3 | $byte_count += 6; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # at this point, we assume the bitstream is valid | ||||||||||||||||||||||||||||
| 200 | 3 | return $byte_count; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 3 | ▶ | sub _load_info { | ||||||||||||||||||||||||||
|
Subroutine
_load_info: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 204 | 3 | my $data = shift; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 3 | my $start = $data->{'startInfoHeader'}; | |||||||||||||||||||||||||||
| 207 | 3 | my $fh = $data->{'fileHandle'}; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 3 | my $byte_count = $start + 23; | |||||||||||||||||||||||||||
| 210 | 3 | my %info = (); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 3 | seek($fh, $start, 0); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # read the vorbis version | ||||||||||||||||||||||||||||
| 215 | 3 | read($fh, my $buffer, 23); | |||||||||||||||||||||||||||
| 216 | 3 | $info{'version'} = _decode_int(substr($buffer, 0, 4, '')); | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # read the number of audio channels | ||||||||||||||||||||||||||||
| 219 | 3 | $info{'channels'} = ord(substr($buffer, 0, 1, '')); | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # read the sample rate | ||||||||||||||||||||||||||||
| 222 | 3 | $info{'rate'} = _decode_int(substr($buffer, 0, 4, '')); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # read the bitrate maximum | ||||||||||||||||||||||||||||
| 225 | 3 | $info{'bitrate_upper'} = _decode_int(substr($buffer, 0, 4, '')); | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # read the bitrate nominal | ||||||||||||||||||||||||||||
| 228 | 3 | $info{'bitrate_nominal'} = _decode_int(substr($buffer, 0, 4, '')); | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | # read the bitrate minimal | ||||||||||||||||||||||||||||
| 231 | 3 | $info{'bitrate_lower'} = _decode_int(substr($buffer, 0, 4, '')); | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # read the blocksize_0 and blocksize_1 | ||||||||||||||||||||||||||||
| 234 | # these are each 4 bit fields, whose actual value is 2 to the power | ||||||||||||||||||||||||||||
| 235 | # of the value of the field | ||||||||||||||||||||||||||||
| 236 | 3 | my $blocksize = substr($buffer, 0, 1, ''); | |||||||||||||||||||||||||||
| 237 | 3 | $info{'blocksize_0'} = 2 << ((ord($blocksize) & 0xF0) >> 4); | |||||||||||||||||||||||||||
| 238 | 3 | $info{'blocksize_1'} = 2 << (ord($blocksize) & 0x0F); | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # read the framing_flag | ||||||||||||||||||||||||||||
| 241 | 3 | $info{'framing_flag'} = ord(substr($buffer, 0, 1, '')); | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # bitrate_window is -1 in the current version of vorbisfile | ||||||||||||||||||||||||||||
| 244 | 3 | $info{'bitrate_window'} = -1; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 3 | $data->{'startCommentHeader'} = $byte_count; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 3 | $data->{'INFO'} = \%info; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 3 | ▶ | sub _load_comments { | ||||||||||||||||||||||||||
|
Subroutine
_load_comments: called CC=25 SCAR=39.2
| |||||||||||||||||||||||||||||
| 252 | 3 | my $data = shift; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 3 | my $fh = $data->{'fileHandle'}; | |||||||||||||||||||||||||||
| 255 | 3 | my $start = $data->{'startHeader'}; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 3 | $data->{COMMENT_KEYS} = []; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # Comment parsing code based on Image::ExifTool::Vorbis | ||||||||||||||||||||||||||||
| 260 | 3 | my $MAX_PACKETS = 2; | |||||||||||||||||||||||||||
| 261 | 3 | my $done; | |||||||||||||||||||||||||||
| 262 | 3 | my ($page, $packets, $streams) = (0,0,0,0); | |||||||||||||||||||||||||||
| 263 | 3 | my ($buff, $flag, $stream, %val); | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 3 | seek $fh, $start, 0; | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 3 | while (1) { | |||||||||||||||||||||||||||
| 268 | 9 | ▶ | if (!$done && read( $fh, $buff, 28 ) == 28) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $done and read($fh, $buff, 28) == 28
MC/DC0%0/2
not $done and read($fh, $buff, 28) == 28
not $done
read($fh, $buff, 28) == 28
Truth table33%1/3
not(A) and B
A= not $doneB= read($fh, $buff, 28) == 28
| |||||||||||||||||||||||||||||
| 269 | # validate magic number | ||||||||||||||||||||||||||||
| 270 | 9 | ▶ | unless ( $buff =~ /^OggS/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | warn "No comment header?"; | |||||||||||||||||||||||||||
| 272 | 0 | last; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 9 | $flag = get8u(\$buff, 5); # page flag | |||||||||||||||||||||||||||
| 276 | 9 | $stream = get32u(\$buff, 14); # stream serial number | |||||||||||||||||||||||||||
| 277 | 9 | ▶ | ++$streams if $flag & 0x02; # count start-of-stream pages | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 9 | ▶ | ++$packets unless $flag & 0x01; # keep track of packet count | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | else { | ||||||||||||||||||||||||||||
| 281 | # all done unless we have to process our last packet | ||||||||||||||||||||||||||||
| 282 | 0 | ▶ | last unless %val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 0 | ($stream) = sort keys %val; # take a stream | |||||||||||||||||||||||||||
| 284 | 0 | $flag = 0; # no continuation | |||||||||||||||||||||||||||
| 285 | 0 | $done = 1; # flag for done reading | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # can finally process previous packet from this stream | ||||||||||||||||||||||||||||
| 289 | # unless this is a continuation page | ||||||||||||||||||||||||||||
| 290 | 9 | ▶ | if (defined $val{$stream} and not $flag & 0x01) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $val{$stream} and not $flag & 1
MC/DC50%1/2
defined $val{$stream} and not $flag & 1
defined $val{$stream}
not $flag & 1
Truth table66%2/3
A and not(B)
A= defined $val{$stream}B= not $flag & 1
| |||||||||||||||||||||||||||||
| 291 | 3 | _process_comments( $data, \$val{$stream} ); | |||||||||||||||||||||||||||
| 292 | 3 | delete $val{$stream}; | |||||||||||||||||||||||||||
| 293 | # only read the first $MAX_PACKETS packets from each stream | ||||||||||||||||||||||||||||
| 294 | 3 | ▶ | if ($packets > $MAX_PACKETS * $streams) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | # all done (success!) | ||||||||||||||||||||||||||||
| 296 | 3 | ▶ | last unless %val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | # process remaining stream(s) | ||||||||||||||||||||||||||||
| 298 | 0 | next; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | # stop processing Ogg Vorbis if we have scanned enough packets | ||||||||||||||||||||||||||||
| 303 | 6 | ▶ | last if $packets > $MAX_PACKETS * $streams and not %val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$packets > $MAX_PACKETS * $streams and not %val
MC/DC0%0/2
$packets > $MAX_PACKETS * $streams and not %val
$packets > $MAX_PACKETS * $streams
not %val
Truth table33%1/3
A and not(B)
A= $packets > $MAX_PACKETS * $streamsB= not %val
| |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | # continue processing the current page | ||||||||||||||||||||||||||||
| 306 | # page sequence number | ||||||||||||||||||||||||||||
| 307 | 6 | my $page_num = get32u(\$buff, 18); | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | # number of segments | ||||||||||||||||||||||||||||
| 310 | 6 | my $nseg = get8u(\$buff, 26); | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | # calculate total data length | ||||||||||||||||||||||||||||
| 313 | 6 | my $data_len = get8u(\$buff, 27); | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 6 | ▶ | if ($nseg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 6 | ▶ | read( $fh, $buff, $nseg-1 ) == $nseg-1 or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 6 | my @segs = unpack('C*', $buff); | |||||||||||||||||||||||||||
| 318 | # could check that all these (but the last) are 255... | ||||||||||||||||||||||||||||
| 319 | 6 | foreach (@segs) { $data_len += $_ } | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 6 | ▶ | if (defined $page) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 6 | ▶ | if ($page == $page_num) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 6 | ++$page; | |||||||||||||||||||||||||||
| 325 | } else { | ||||||||||||||||||||||||||||
| 326 | 0 | warn "Missing page(s) in Ogg file\n"; | |||||||||||||||||||||||||||
| 327 | 0 | undef $page; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | # read page data | ||||||||||||||||||||||||||||
| 332 | 6 | ▶ | read($fh, $buff, $data_len) == $data_len or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 6 | ▶ | if (defined $val{$stream}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 335 | # add this continuation page | ||||||||||||||||||||||||||||
| 336 | 0 | $val{$stream} .= $buff; | |||||||||||||||||||||||||||
| 337 | } elsif (not $flag & 0x01) { | ||||||||||||||||||||||||||||
| 338 | # ignore remaining pages of a continued packet | ||||||||||||||||||||||||||||
| 339 | # ignore the first page of any packet we aren't parsing | ||||||||||||||||||||||||||||
| 340 | 6 | ▶ | if ($buff =~ /^(.)vorbis/s and ord($1) == 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$buff =~ /^(.)vorbis/s and ord $1 == 3
MC/DC50%1/2
$buff =~ /^(.)vorbis/s and ord $1 == 3
$buff =~ /^(.)vorbis/s
ord $1 == 3
Truth table66%2/3
A and B
A= $buff =~ /^(.)vorbis/sB= ord $1 == 3
| |||||||||||||||||||||||||||||
| 341 | # save this page, it has comments | ||||||||||||||||||||||||||||
| 342 | 3 | $val{$stream} = $buff; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 6 | ▶ | if (defined $val{$stream} and $flag & 0x04) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $val{$stream} and $flag & 4
MC/DC0%0/2
defined $val{$stream} and $flag & 4
defined $val{$stream}
$flag & 4
Truth table66%2/3
A and B
A= defined $val{$stream}B= $flag & 4
| |||||||||||||||||||||||||||||
| 347 | # process Ogg Vorbis packet now if end-of-stream bit is set | ||||||||||||||||||||||||||||
| 348 | 0 | _process_comments($data, \$val{$stream}); | |||||||||||||||||||||||||||
| 349 | 0 | delete $val{$stream}; | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 3 | $data->{'INFO'}{offset} = tell $fh; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 3 | ▶ | sub _process_comments { | ||||||||||||||||||||||||||
|
Subroutine
_process_comments: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 357 | 3 | my ( $data, $data_pt ) = @_; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 3 | my $pos = 7; | |||||||||||||||||||||||||||
| 360 | 3 | my $end = length $$data_pt; | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 3 | my $num; | |||||||||||||||||||||||||||
| 363 | my %comments; | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 3 | while (1) { | |||||||||||||||||||||||||||
| 366 | 9 | ▶ | last if $pos + 4 > $end; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 9 | my $len = get32u($data_pt, $pos); | |||||||||||||||||||||||||||
| 368 | 9 | ▶ | last if $pos + 4 + $len > $end; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 9 | my $start = $pos + 4; | |||||||||||||||||||||||||||
| 370 | 9 | my $buff = substr($$data_pt, $start, $len); | |||||||||||||||||||||||||||
| 371 | 9 | $pos = $start + $len; | |||||||||||||||||||||||||||
| 372 | 9 | my ($tag, $val); | |||||||||||||||||||||||||||
| 373 | 9 | ▶ | if (defined $num) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | 6 | ▶ | $buff =~ /(.*?)=(.*)/s or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 6 | ($tag, $val) = ($1, $2); | |||||||||||||||||||||||||||
| 376 | } else { | ||||||||||||||||||||||||||||
| 377 | 3 | $tag = 'vendor'; | |||||||||||||||||||||||||||
| 378 | 3 | $val = $buff; | |||||||||||||||||||||||||||
| 379 | 3 | ▶ | $num = ($pos + 4 < $end) ? get32u($data_pt, $pos) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | 3 | $pos += 4; | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 9 | my $lctag = lc $tag; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 9 | push @{$comments{$lctag}}, $val; | |||||||||||||||||||||||||||
| 386 | 9 | push @{$data->{COMMENT_KEYS}}, $lctag; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # all done if this was our last tag | ||||||||||||||||||||||||||||
| 389 | 9 | ▶ | if ( !$num-- ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 3 | $data->{COMMENTS} = \%comments; | |||||||||||||||||||||||||||
| 391 | 3 | return 1; | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 0 | warn "format error in Vorbis comments\n"; | |||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 0 | return 0; | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 21 | ▶ | sub get8u { | ||||||||||||||||||||||||||
|
Subroutine
get8u: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 401 | 21 | return unpack( "x$_[1] C", ${$_[0]} ); | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 27 | ▶ | sub get32u { | ||||||||||||||||||||||||||
|
Subroutine
get32u: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 405 | 27 | return unpack( "x$_[1] V", ${$_[0]} ); | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 3 | ▶ | sub _calculate_track_length { | ||||||||||||||||||||||||||
|
Subroutine
_calculate_track_length: called CC=15 SCAR=37.9
| |||||||||||||||||||||||||||||
| 409 | 3 | my $data = shift; | |||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 3 | my $fh = $data->{'fileHandle'}; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | # The original author was doing something pretty lame, and was walking the | ||||||||||||||||||||||||||||
| 414 | # entire file to find the last granule_position. Instead, let's seek to | ||||||||||||||||||||||||||||
| 415 | # the end of the file - blocksize_0, and read from there. | ||||||||||||||||||||||||||||
| 416 | 3 | my $len = 0; | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | # Bug 1155 - Seek further back to get the granule_position. | ||||||||||||||||||||||||||||
| 419 | # However, for short tracks, don't seek that far back. | ||||||||||||||||||||||||||||
| 420 | 3 | ▶ | if (($data->{'filesize'} - $data->{'INFO'}{'offset'}) > ($data->{'INFO'}{'blocksize_0'} * 2)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 0 | $len = $data->{'INFO'}{'blocksize_0'} * 2; | |||||||||||||||||||||||||||
| 423 | } elsif ($data->{'filesize'} < $data->{'INFO'}{'blocksize_0'}) { | ||||||||||||||||||||||||||||
| 424 | 3 | $len = $data->{'filesize'}; | |||||||||||||||||||||||||||
| 425 | } else { | ||||||||||||||||||||||||||||
| 426 | 0 | $len = $data->{'INFO'}{'blocksize_0'}; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 3 | ▶ | if ($data->{'INFO'}{'blocksize_0'} == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | 0 | print "Ogg::Vorbis::Header::PurePerl:\n"; | |||||||||||||||||||||||||||
| 431 | 0 | warn "blocksize_0 is 0! Should be a power of 2! http://www.xiph.org/ogg/vorbis/doc/vorbis-spec-ref.html\n"; | |||||||||||||||||||||||||||
| 432 | 0 | return; | |||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 3 | seek($fh, -$len, 2); | |||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 3 | my $buf = ''; | |||||||||||||||||||||||||||
| 438 | 3 | my $found_header = 0; | |||||||||||||||||||||||||||
| 439 | 3 | my $block = $len; | |||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | SEEK: | ||||||||||||||||||||||||||||
| 442 | 3 | ▶ | while ($found_header == 0 && read($fh, $buf, $len)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$found_header == 0 and read $fh, $buf, $len
MC/DC0%0/2
$found_header == 0 and read $fh, $buf, $len
$found_header == 0
read $fh, $buf, $len
Truth table33%1/3
A and B
A= $found_header == 0B= read $fh, $buf, $len
| |||||||||||||||||||||||||||||
| 443 | # search the last read $block bytes for Ogg header flag | ||||||||||||||||||||||||||||
| 444 | # the search is conducted backwards so that the last flag | ||||||||||||||||||||||||||||
| 445 | # is found first | ||||||||||||||||||||||||||||
| 446 | 3 | ▶ | for (my $i = $block; $i >= 0; $i--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 447 | 90 | ▶ | if (substr($buf, $i, 4) eq OGGHEADERFLAG) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 3 | substr($buf, 0, ($i+4), ''); | |||||||||||||||||||||||||||
| 449 | 3 | $found_header = 1; | |||||||||||||||||||||||||||
| 450 | 3 | last SEEK; | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | # already read the whole file? | ||||||||||||||||||||||||||||
| 455 | 0 | ▶ | last if $len == $data->{'filesize'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 0 | $len += $block; | |||||||||||||||||||||||||||
| 458 | 0 | ▶ | $len = $data->{'filesize'} if $len > $data->{'filesize'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 0 | seek($fh, -$len, 2); | |||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 3 | ▶ | unless ($found_header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 464 | 0 | warn "Ogg::Vorbis::Header::PurePerl: Didn't find an ogg header - invalid file?\n"; | |||||||||||||||||||||||||||
| 465 | 0 | return; | |||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | # stream structure version - must be 0x00 | ||||||||||||||||||||||||||||
| 469 | 3 | ▶ | if (ord(substr($buf, 0, 1, '')) != 0x00) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 470 | 0 | warn "Ogg::Vorbis::Header::PurePerl: Invalid stream structure version: " . sprintf("%x", ord($buf)); | |||||||||||||||||||||||||||
| 471 | 0 | return; | |||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | # absolute granule position - this is what we need! | ||||||||||||||||||||||||||||
| 475 | 3 | substr($buf, 0, 1, ''); | |||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | 3 | my $granule_position = _decode_int(substr($buf, 0, 8, '')); | |||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | 3 | ▶ | if ($granule_position && $data->{'INFO'}{'rate'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$granule_position and $data->{"INFO"}{"rate"}
MC/DC0%0/2
$granule_position and $data->{"INFO"}{"rate"}
$granule_position
$data->{"INFO"}{"rate"}
Truth table33%1/3
A and B
A= $granule_positionB= $data->{"INFO"}{"rate"}
| |||||||||||||||||||||||||||||
| 480 | 0 | $data->{'INFO'}{'length'} = int($granule_position / $data->{'INFO'}{'rate'}); | |||||||||||||||||||||||||||
| 481 | 0 | ▶ | if ($data->{'INFO'}{'length'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | 0 | $data->{'INFO'}{'bitrate_average'} = sprintf( "%d", ( $data->{'filesize'} * 8 ) / $data->{'INFO'}{'length'} ); | |||||||||||||||||||||||||||
| 483 | } else { | ||||||||||||||||||||||||||||
| 484 | 0 | $data->{'INFO'}{'bitrate_average'} = 0; | |||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | } else { | ||||||||||||||||||||||||||||
| 487 | 3 | $data->{'INFO'}{'length'} = 0; | |||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | 18 | ▶ | sub _decode_int { | ||||||||||||||||||||||||||
|
Subroutine
_decode_int: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 492 | 18 | my $bytes = shift; | |||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 18 | my $num_bytes = length($bytes); | |||||||||||||||||||||||||||
| 495 | 18 | my $num = 0; | |||||||||||||||||||||||||||
| 496 | 18 | my $mult = 1; | |||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 18 | ▶ | for (my $i = 0; $i < $num_bytes; $i ++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 84 | $num += ord(substr($bytes, 0, 1, '')) * $mult; | |||||||||||||||||||||||||||
| 501 | 84 | $mult *= 256; | |||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 18 | return $num; | |||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | 3 | 1; | |||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | __END__ | ||||||||||||||||||||||||||||