Image/ExifTool/APE.pm

filter: Statementstmt 91.6% 88 / 96 Branchbran 65.0% 39 / 60 Conditioncond 44.4% 8 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 74.3% 142 / 191 CC 37 SCAR 39.1
CC
37
Cov
78%
ProcessAPE
58.3
MakeTag
3.0
BEGIN
1.0
CRAP 50.1
« Image/ExifTool/Google.pm ↑ Summary Image/ExifTool/MISB.pm »
1 #------------------------------------------------------------------------------
2 # File: APE.pm
3 #
4 # Description: Read Monkey's Audio meta information
5 #
6 # Revisions: 11/13/2006 - P. Harvey Created
7 #
8 # References: 1) http://www.monkeysaudio.com/
9 # 2) http://www.personal.uni-jena.de/~pfk/mpp/sv8/apetag.html
10 #------------------------------------------------------------------------------
11  
12 package Image::ExifTool::APE;
13  
14 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 $VERSION = '1.07';
19  
20 # APE metadata blocks
21 8 %Image::ExifTool::APE::Main = (
22 GROUPS => { 2 => 'Audio' },
23 NOTES => q{
24 Tags found in Monkey's Audio (APE) information. Only a few common tags are
25 listed below, but ExifTool will extract any tag found. ExifTool supports
26 APEv1 and APEv2 tags, as well as ID3 information in APE files, and will also
27 read APE metadata from MP3 and MPC files.
28 },
29 Album => { },
30 Artist => { },
31 Genre => { },
32 Title => { },
33 Track => { },
34 Year => { },
35 DURATION => {
36 Name => 'Duration',
37 ValueConv => '$val += 4294967296 if $val < 0 and $val >= -2147483648; $val * 1e-7',
38 PrintConv => 'ConvertDuration($val)',
39 },
40 'Tool Version' => { Name => 'ToolVersion' },
41 'Tool Name' => { Name => 'ToolName' },
42 );
43  
44 # APE MAC header version 3.97 or earlier
45 8 %Image::ExifTool::APE::OldHeader = (
46 PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
47 GROUPS => { 1 => 'MAC', 2 => 'Audio' },
48 FORMAT => 'int16u',
49 NOTES => 'APE MAC audio header for version 3.97 or earlier.',
50 0 => {
51 Name => 'APEVersion',
52 ValueConv => '$val / 1000',
53 },
54 1 => 'CompressionLevel',
55 # 2 => 'FormatFlags',
56 3 => 'Channels',
57 4 => { Name => 'SampleRate', Format => 'int32u' },
58 # 6 => { Name => 'HeaderBytes', Format => 'int32u' }, # WAV header bytes
59 # 8 => { Name => 'TerminatingBytes', Format => 'int32u' },
60 10 => { Name => 'TotalFrames', Format => 'int32u' },
61 12 => { Name => 'FinalFrameBlocks', Format => 'int32u' },
62 );
63  
64 # APE MAC header version 3.98 or later
65 8 %Image::ExifTool::APE::NewHeader = (
66 PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
67 GROUPS => { 1 => 'MAC', 2 => 'Audio' },
68 FORMAT => 'int16u',
69 NOTES => 'APE MAC audio header for version 3.98 or later.',
70 0 => 'CompressionLevel',
71 # 1 => 'FormatFlags',
72 2 => { Name => 'BlocksPerFrame', Format => 'int32u' },
73 4 => { Name => 'FinalFrameBlocks', Format => 'int32u' },
74 6 => { Name => 'TotalFrames', Format => 'int32u' },
75 8 => 'BitsPerSample',
76 9 => 'Channels',
77 10 => { Name => 'SampleRate', Format => 'int32u' },
78 );
79  
80 # APE Composite tags
81 8 %Image::ExifTool::APE::Composite = (
82 GROUPS => { 2 => 'Audio' },
83 Duration => {
84 Require => {
85 0 => 'APE:SampleRate',
86 1 => 'APE:TotalFrames',
87 2 => 'APE:BlocksPerFrame',
88 3 => 'APE:FinalFrameBlocks',
89 },
90 RawConv => '($val[0] && $val[1]) ? (($val[1] - 1) * $val[2] + $val[3]) / $val[0]: undef',
91 PrintConv => 'ConvertDuration($val)',
92 },
93 );
94  
95 # add our composite tags
96 8 Image::ExifTool::AddCompositeTags('Image::ExifTool::APE');
97  
98 #------------------------------------------------------------------------------
99 # Make tag info hash for specified tag
100 # Inputs: 0) tag name, 1) tag table ref
101 # - must only call if tag doesn't exist
102 sub MakeTag($$)
103 3 ▶{
Subroutine MakeTag: called CC=3 SCAR=11.0
Pod undocumented
104 3 my ($tag, $tagTablePtr) = @_;
105 3 my $name = ucfirst(lc($tag));
106 # remove invalid characters in tag name and capitalize following letters
107 3 $name =~ s/[^\w-]+(.?)/\U$1/sg;
108 3 $name =~ s/([a-z0-9])_([a-z])/$1\U$2/g;
109 3 my %tagInfo = ( Name => $name );
110 3 ▶ $tagInfo{Groups} = { 2 => 'Preview' } if $tag =~ /^Cover Art/ and $tag !~ /Desc$/;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^Cover Art/ and not $tag =~ /Desc$/ 1 2 3
Condition100%3/3
$tag =~ /^Cover Art/ and not $tag =~ /Desc$/
!ll&&!rl&&r
111
MC/DC100%2/2
$tag =~ /^Cover Art/ and not $tag =~ /Desc$/
$tag =~ /^Cover Art/ not $tag =~ /Desc$/
Truth table100%3/3
A and not(B)
A= $tag =~ /^Cover Art/B= not $tag =~ /Desc$/
ABresult
0X0
100
111
111 3 AddTagToTable($tagTablePtr, $tag, \%tagInfo);
112 }
113  
114 #------------------------------------------------------------------------------
115 # Extract information from an APE file
116 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
117 # - Just looks for APE trailer if FileType is already set
118 # Returns: 1 on success, 0 if this wasn't a valid APE file
119 sub ProcessAPE($$)
120 3 ▶{
Subroutine ProcessAPE: called CC=35 SCAR=40.7
Pod undocumented
121 3 my ($et, $dirInfo) = @_;
122  
123 # must first check for leading/trailing ID3 information
124 3 ▶ unless ($$et{DoneID3}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($et->{"DoneID3"}) 1 2 3
125 1 require Image::ExifTool::ID3;
126 1 ▶ Image::ExifTool::ID3::ProcessID3($et, $dirInfo) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
&Image::ExifTool::ID3::ProcessID3($et, $dirInfo) and return 1 0 1 1
127 }
128 3 my $raf = $$dirInfo{RAF};
129 3 my $verbose = $et->Options('Verbose');
130 3 my ($buff, $i, $header, $tagTablePtr, $dataPos, $oldIndent);
131  
132 3 $$et{DoneAPE} = 1;
133  
134 # check APE signature and process audio information
135 # unless this is some other type of file
136 3 ▶ unless ($$et{FileType}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($et->{"FileType"}) 1 2 3
137 1 ▶ $raf->Read($buff, 32) == 32 or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 32) == 32 or return 0 1 0 1
138 1 ▶ $buff =~ /^(MAC |APETAGEX)/ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$buff =~ /^(MAC |APETAGEX)/ or return 0 1 0 1
139 1 $et->SetFileType();
140 1 SetByteOrder('II');
141  
142 1 ▶ if ($buff =~ /^APETAGEX/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /^APETAGEX/) { } 0 1 1
143 # we already read the APE header
144 0 $header = 1;
145 } else {
146 # process the MAC header
147 1 my $vers = Get16u(\$buff, 4);
148 1 my $table;
149 1 ▶ if ($vers <= 3970) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vers <= 3970) { } 0 1 1
150 0 $buff = substr($buff, 4);
151 0 $table = GetTagTable('Image::ExifTool::APE::OldHeader');
152 } else {
153 1 my $dlen = Get32u(\$buff, 8);
154 1 my $hlen = Get32u(\$buff, 12);
155 1 ▶ unless ($dlen & 0x80000000 or $hlen & 0x80000000) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dlen & 2147483648 or $hlen & 2147483648) 1 0 1
Condition33%1/3
$dlen & 2147483648 or $hlen & 2147483648
l!l&&r!l&&!r
001
MC/DC0%0/2
$dlen & 2147483648 or $hlen & 2147483648
$dlen & 2147483648 $hlen & 2147483648
Truth table33%1/3
A or B
A= $dlen & 2147483648B= $hlen & 2147483648
ABresult
1X1
011
000
156 1 ▶ if ($raf->Seek($dlen, 0) and $raf->Read($buff, $hlen) == $hlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raf->Seek($dlen, 0) and $raf->Read($buff, $hlen) == $hlen) 1 0 1
Condition33%1/3
$raf->Seek($dlen, 0) and $raf->Read($buff, $hlen) == $hlen
!ll&&!rl&&r
001
MC/DC0%0/2
$raf->Seek($dlen, 0) and $raf->Read($buff, $hlen) == $hlen
$raf->Seek($dlen, 0) $raf->Read($buff, $hlen) == $hlen
Truth table33%1/3
A and B
A= $raf->Seek($dlen, 0)B= $raf->Read($buff, $hlen) == $hlen
ABresult
0X0
100
111
157 1 $table = GetTagTable('Image::ExifTool::APE::NewHeader');
158 }
159 }
160 }
161 1 ▶ $et->ProcessDirectory( { DataPt => \$buff }, $table) if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 1 0 1
162 }
163 }
164 # look for APE trailer unless we already found an APE header
165 3 ▶ unless ($header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header) 3 0 3
166 # look for the APE trailer footer...
167 3 my $footPos = -32;
168 # (...but before the ID3v1 trailer if it exists)
169 3 ▶ $footPos -= $$et{DoneID3} if $$et{DoneID3} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $et->{"DoneID3"} > 1 2 1 3
170 3 ▶ $raf->Seek($footPos, 2) or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($footPos, 2) or return 1 3 0 3
171 3 ▶ $raf->Read($buff, 32) == 32 or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 32) == 32 or return 1 3 0 3
172 3 ▶ $buff =~ /^APETAGEX/ or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$buff =~ /^APETAGEX/ or return 1 2 1 3
173 2 SetByteOrder('II');
174 }
175 #
176 # Read the APE data (we have just read the APE header or footer into $buff)
177 #
178 2 my ($version, $size, $count, $flags) = unpack('x8V4', $buff);
179 2 $version /= 1000;
180 2 $size -= 32; # get size of data only
181 2 ▶ if (($size & 0x80000000) == 0 and
Branch: 1 branch
BranchTrueFalseTotal
if (($size & 2147483648) == 0 and $header || $raf->Seek(-$size - 32, 1) and $raf->Read($buff, $size) == $size) { } 2 0 2
Condition33%3/9
($size & 2147483648) == 0 and $header || $raf->Seek(-$size - 32, 1) and $raf->Read($buff, $size) == $size
!ll&&!rl&&r
002
($size & 2147483648) == 0 and $header || $raf->Seek(-$size - 32, 1)
!ll&&!rl&&r
002
$header or $raf->Seek(-$size - 32, 1)
l!l&&r!l&&!r
020
MC/DC0%0/4
($size & 2147483648) == 0 and $header || $raf->Seek(-$size - 32, 1) and $raf->Read($buff, $size) == $size
($size & 2147483648) == 0 $header $raf->Seek(-$size - 32, 1) $raf->Read($buff, $size) == $size
Truth table16%1/6
A and B or C and D
A= ($size & 2147483648) == 0B= $headerC= $raf->Seek(-$size - 32, 1)D= $raf->Read($buff, $size) == $size
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
182 ($header or $raf->Seek(-$size-32, 1)) and
183 $raf->Read($buff, $size) == $size)
184 {
185 2 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 2 2
186 0 $oldIndent = $$et{INDENT};
187 0 $$et{INDENT} .= '| ';
188 0 $et->VerboseDir("APEv$version", $count, $size);
189 0 $et->VerboseDump(\$buff, DataPos => $raf->Tell() - $size);
190 }
191 2 $tagTablePtr = GetTagTable('Image::ExifTool::APE::Main');
192 2 $dataPos = $raf->Tell() - $size;
193 } else {
194 0 $count = -1;
195 }
196 #
197 # Process the APE tags
198 #
199 2 my $pos = 0;
200 2 ▶ for ($i=0; $i<$count; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 20 2 22
201 # read next APE tag
202 20 ▶ last if $pos + 8 > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + 8 > $size 0 20 20
203 20 my $len = Get32u(\$buff, $pos);
204 20 my $flags = Get32u(\$buff, $pos + 4);
205 20 pos($buff) = $pos + 8;
206 20 ▶ last unless $buff =~ /\G(.*?)\0/sg;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /\G(.*?)\0/gs 0 20 20
207 20 my $tag = $1;
208 # avoid conflicts with our special table entries
209 20 ▶ $tag .= '.' if $Image::ExifTool::specialTags{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $Image::ExifTool::specialTags{$tag} 0 20 20
210 20 $pos = pos($buff);
211 20 ▶ last if $pos + $len > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $len > $size 0 20 20
212 20 my $val = substr($buff, $pos, $len);
213 20 ▶ MakeTag($tag, $tagTablePtr) unless $$tagTablePtr{$tag};
Branch: 1 branch
BranchTrueFalseTotal
unless $tagTablePtr->{$tag} 2 18 20
214 # handle binary-value tags
215 20 ▶ if (($flags & 0x06) == 0x02) {
Branch: 1 branch
BranchTrueFalseTotal
if (($flags & 6) == 2) 2 18 20
216 2 my $buf2 = $val;
217 2 $val = \$buf2;
218 # extract cover art description separately (hackitty hack)
219 2 ▶ if ($tag =~ /^Cover Art/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^Cover Art/) 2 0 2
220 2 $buf2 =~ s/^([\x20-\x7e]*)\0//;
221 2 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 2 0 2
222 2 my $t = "$tag Desc";
223 2 my $v = $1;
224 2 ▶ MakeTag($t, $tagTablePtr) unless $$tagTablePtr{$t};
Branch: 1 branch
BranchTrueFalseTotal
unless $tagTablePtr->{$t} 1 1 2
225 2 $et->HandleTag($tagTablePtr, $t, $v);
226 }
227 }
228 }
229 20 $et->HandleTag($tagTablePtr, $tag, $val,
230 Index => $i,
231 DataPt => \$buff,
232 DataPos => $dataPos,
233 Start => $pos,
234 Size => $len,
235 );
236 20 $pos += $len;
237 }
238 2 ▶ $i == $count or $et->Warn('Bad APE trailer');
Branch: 1 branch
BranchTrueFalseTotal
$i == $count or $et->Warn("Bad APE trailer") 2 0 2
239 2 ▶ $$et{INDENT} = $oldIndent if defined $oldIndent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldIndent 0 2 2
240 2 return 1;
241 }
242  
243 8 1; # end
244  
245 __END__
« Image/ExifTool/Google.pm ↑ Summary Image/ExifTool/MISB.pm »