Image/ExifTool/FLAC.pm

filter: Statementstmt 96.6% 86 / 89 Branchbran 78.2% 36 / 46 Conditioncond 65.6% 21 / 32 MC/DCmcdc 45.0% 9 / 20 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 80.9% 157 / 194 CC 40 SCAR 32.7
CC
40
Cov
86%
ProcessBitStream
33.7
ProcessFLAC
10.8
BEGIN
1.0
CRAP 26.3
« Image/ExifTool/Palm.pm ↑ Summary Image/ExifTool/Scalado.pm »
1 #------------------------------------------------------------------------------
2 # File: FLAC.pm
3 #
4 # Description: Read Free Lossless Audio Codec information
5 #
6 # Revisions: 11/13/2006 - P. Harvey Created
7 #
8 # References: 1) http://flac.sourceforge.net/
9 #------------------------------------------------------------------------------
10  
11 package Image::ExifTool::FLAC;
12  
13 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 $VERSION = '1.09';
18  
19 sub ProcessBitStream($$$);
20  
21 # FLAC metadata blocks
22 5 %Image::ExifTool::FLAC::Main = (
23 NOTES => q{
24 Free Lossless Audio Codec (FLAC) meta information. ExifTool also extracts
25 ID3 information from these files.
26 },
27 0 => {
28 Name => 'StreamInfo',
29 SubDirectory => { TagTable => 'Image::ExifTool::FLAC::StreamInfo' },
30 },
31 1 => { Name => 'Padding', Binary => 1, Unknown => 1 },
32 2 => [{ # (see forum14064)
33 Name => 'Application_riff',
34 Condition => '$$valPt =~ /^riff(?!RIFF)/', # (all "riff" blocks but header)
35 SubDirectory => {
36 TagTable => 'Image::ExifTool::RIFF::Main',
37 ByteOrder => 'LittleEndian',
38 Start => 4,
39 },
40 },{
41 Name => 'ApplicationUnknown',
42 Binary => 1,
43 Unknown => 1,
44 }],
45 3 => { Name => 'SeekTable', Binary => 1, Unknown => 1 },
46 4 => {
47 Name => 'VorbisComment',
48 SubDirectory => { TagTable => 'Image::ExifTool::Vorbis::Comments' },
49 },
50 5 => { Name => 'CueSheet', Binary => 1, Unknown => 1 },
51 6 => {
52 Name => 'Picture',
53 SubDirectory => { TagTable => 'Image::ExifTool::FLAC::Picture' },
54 },
55 # 7-126 - Reserved
56 # 127 - Invalid
57 );
58  
59 5 %Image::ExifTool::FLAC::StreamInfo = (
60 PROCESS_PROC => \&ProcessBitStream,
61 NOTES => 'FLAC is big-endian, so bit 0 is the high-order bit in this table.',
62 GROUPS => { 2 => 'Audio' },
63 'Bit000-015' => 'BlockSizeMin',
64 'Bit016-031' => 'BlockSizeMax',
65 'Bit032-055' => 'FrameSizeMin',
66 'Bit056-079' => 'FrameSizeMax',
67 'Bit080-099' => 'SampleRate',
68 'Bit100-102' => {
69 Name => 'Channels',
70 ValueConv => '$val + 1',
71 },
72 'Bit103-107' => {
73 Name => 'BitsPerSample',
74 ValueConv => '$val + 1',
75 },
76 'Bit108-143' => 'TotalSamples',
77 'Bit144-271' => { #Tim Eliseo
78 Name => 'MD5Signature',
79 Format => 'undef',
80 ValueConv => 'unpack("H*",$val)',
81 },
82 );
83  
84 5 %Image::ExifTool::FLAC::Picture = (
85 PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
86 GROUPS => { 2 => 'Image' },
87 FORMAT => 'int32u',
88 0 => {
89 Name => 'PictureType',
90 PrintConv => { # (Note: Duplicated in ID3, ASF and FLAC modules!)
91 0 => 'Other',
92 1 => '32x32 PNG Icon',
93 2 => 'Other Icon',
94 3 => 'Front Cover',
95 4 => 'Back Cover',
96 5 => 'Leaflet',
97 6 => 'Media',
98 7 => 'Lead Artist',
99 8 => 'Artist',
100 9 => 'Conductor',
101 10 => 'Band',
102 11 => 'Composer',
103 12 => 'Lyricist',
104 13 => 'Recording Studio or Location',
105 14 => 'Recording Session',
106 15 => 'Performance',
107 16 => 'Capture from Movie or Video',
108 17 => 'Bright(ly) Colored Fish',
109 18 => 'Illustration',
110 19 => 'Band Logo',
111 20 => 'Publisher Logo',
112 },
113 },
114 1 => {
115 Name => 'PictureMIMEType',
116 Format => 'var_pstr32',
117 },
118 2 => {
119 Name => 'PictureDescription',
120 Format => 'var_pstr32',
121 ValueConv => '$self->Decode($val, "UTF8")',
122 },
123 3 => 'PictureWidth',
124 4 => 'PictureHeight',
125 5 => 'PictureBitsPerPixel',
126 6 => 'PictureIndexedColors',
127 7 => 'PictureLength',
128 8 => {
129 Name => 'Picture',
130 Groups => { 2 => 'Preview' },
131 Format => 'undef[$val{7}]',
132 Binary => 1,
133 },
134 );
135  
136 # FLAC composite tags
137 5 %Image::ExifTool::FLAC::Composite = (
138 Duration => {
139 Require => {
140 0 => 'FLAC:SampleRate',
141 1 => 'FLAC:TotalSamples',
142 },
143 ValueConv => '($val[0] and $val[1]) ? $val[1] / $val[0] : undef',
144 PrintConv => 'ConvertDuration($val)',
145 },
146 );
147  
148 # add our composite tags
149 5 Image::ExifTool::AddCompositeTags('Image::ExifTool::FLAC');
150  
151  
152 #------------------------------------------------------------------------------
153 # Process information in a bit stream
154 # Inputs: 0) ExifTool object ref, 1) dirInfo ref, 2) tag table ref
155 # Notes: Byte order is used to determine the ordering of bits in the stream:
156 # 'MM' = bit 0 is most significant, 'II' = bit 0 is least significant
157 # - can handle arbitrarily wide values (eg. 8-byte or larger integers)
158 sub ProcessBitStream($$$)
159 6 ▶{
Subroutine ProcessBitStream: called CC=31 SCAR=35.2
Pod undocumented
160 6 my ($et, $dirInfo, $tagTablePtr) = @_;
161 6 my $dataPt = $$dirInfo{DataPt};
162 6 my $dataPos = $$dirInfo{DataPos};
163 6 ▶ my $dirStart = $$dirInfo{DirStart} || 0;
Condition50%1/2
$dirInfo->{'DirStart'} || 0
l!l
06
MC/DC0%0/1
$dirInfo->{'DirStart'} || 0
$dirInfo->{'DirStart'}
Truth table50%1/2
A
A= $dirInfo->{'DirStart'}
Aresult
11
00
164 6 ▶ my $dirLen = $$dirInfo{DirLen} || (length($$dataPt) - $dirStart);
Condition66%2/3
$dirInfo->{'DirLen'} || length($$dataPt) - $dirStart
l!l&&r!l&&!r
240
MC/DC0%0/2
$dirInfo->{'DirLen'} || length($$dataPt) - $dirStart
$dirInfo->{'DirLen'} length($$dataPt) - $dirStart
Truth table66%2/3
A || B
A= $dirInfo->{'DirLen'}B= length($$dataPt) - $dirStart
ABresult
1X1
011
000
165 6 my $verbose = $et->Options('Verbose');
166 6 my $byteOrder = GetByteOrder();
167 6 my $tag;
168  
169 6 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 6 6
170 0 $et->VPrint(0, " + [BitStream directory, $dirLen bytes, '${byteOrder}' order]\n");
171 }
172 6 foreach $tag (sort keys %$tagTablePtr) {
173 74 ▶ next unless $tag =~ /^Bit(\d+)-?(\d+)?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /^Bit(\d+)-?(\d+)?/ 37 37 74
174 37 ▶ my ($b1, $b2) = ($1, $2 || $1); # start/end bit numbers in stream
Condition66%2/3
$2 || $1
l!l&&r!l&&!r
3340
MC/DC0%0/2
$2 || $1
$2 $1
Truth table66%2/3
A || B
A= $2B= $1
ABresult
1X1
011
000
175 37 my ($i1, $i2) = (int($b1 / 8), int($b2 / 8)); # start/end byte numbers
176 37 my ($f1, $f2) = ($b1 % 8, $b2 % 8); # start/end bit numbers within each byte
177 37 ▶ last if $i2 >= $dirLen;
Branch: 1 branch
BranchTrueFalseTotal
if $i2 >= $dirLen 0 37 37
178 37 my ($val, $extra);
179 # if Format is unspecified, convert the specified number of bits to an unsigned integer,
180 # otherwise allow HandleTag to convert whole bytes the normal way (via undefined $val)
181 37 ▶ if (ref $$tagTablePtr{$tag} ne 'HASH' or not $$tagTablePtr{$tag}{Format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tagTablePtr->{$tag} ne "HASH" or not $tagTablePtr->{$tag}{"Format"}) 35 2 37
Condition66%2/3
ref $tagTablePtr->{$tag} ne "HASH" or not $tagTablePtr->{$tag}{"Format"}
l!l&&r!l&&!r
0352
MC/DC50%1/2
ref $tagTablePtr->{$tag} ne "HASH" or not $tagTablePtr->{$tag}{"Format"}
ref $tagTablePtr->{$tag} ne "HASH" not $tagTablePtr->{$tag}{"Format"}
Truth table66%2/3
A or not(B)
A= ref $tagTablePtr->{$tag} ne "HASH"B= not $tagTablePtr->{$tag}{"Format"}
ABresult
1X1
011
000
182 35 my ($i, $mask);
183 35 $val = 0;
184 35 ▶ $extra = ', Mask=0x' if $verbose and ($f1 != 0 or $f2 != 7);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose and $f1 != 0 || $f2 != 7 0 35 35
Condition16%1/6
$verbose and $f1 != 0 || $f2 != 7
!ll&&!rl&&r
3500
$f1 != 0 or $f2 != 7
l!l&&r!l&&!r
000
MC/DC0%0/3
$verbose and $f1 != 0 || $f2 != 7
$verbose $f1 != 0 $f2 != 7
Truth table25%1/4
A and B or C
A= $verboseB= $f1 != 0C= $f2 != 7
ABCresult
0XX0
1000
11X1
1011
185 35 ▶ if ($byteOrder eq 'MM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($byteOrder eq 'MM') { } 24 11 35
186 # loop from high byte to low byte
187 24 ▶ for ($i=$i1; $i<=$i2; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $i2) 51 24 75
188 51 $mask = 0xff;
189 51 ▶ if ($i == $i1 and $f1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $i1 and $f1) 12 39 51
Condition100%3/3
$i == $i1 and $f1
!ll&&!rl&&r
271212
MC/DC100%2/2
$i == $i1 and $f1
$i == $i1 $f1
Truth table100%3/3
A and B
A= $i == $i1B= $f1
ABresult
0X0
100
111
190 # mask off high bits in first word (0 is high bit)
191 12 foreach ((8-$f1) .. 7) { $mask ^= (1 << $_) }
192 }
193 51 ▶ if ($i == $i2 and $f2 < 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $i2 and $f2 < 7) 12 39 51
Condition100%3/3
$i == $i2 and $f2 < 7
!ll&&!rl&&r
271212
MC/DC100%2/2
$i == $i2 and $f2 < 7
$i == $i2 $f2 < 7
Truth table100%3/3
A and B
A= $i == $i2B= $f2 < 7
ABresult
0X0
100
111
194 # mask off low bits in last word (7 is low bit)
195 12 foreach (0 .. (6-$f2)) { $mask ^= (1 << $_) }
196 }
197 51 $val = $val * 256 + ($mask & Get8u($dataPt, $i + $dirStart));
198 51 ▶ $extra .= sprintf('%.2x', $mask) if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 0 51 51
199 }
200 } else {
201 # (FLAC is big-endian, but support little-endian bit streams
202 # so this routine can be used by other modules)
203 # loop from high byte to low byte
204 11 ▶ for ($i=$i2; $i>=$i1; --$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $i1) 18 11 29
205 18 $mask = 0xff;
206 18 ▶ if ($i == $i1 and $f1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $i1 and $f1) 3 15 18
Condition100%3/3
$i == $i1 and $f1
!ll&&!rl&&r
783
MC/DC100%2/2
$i == $i1 and $f1
$i == $i1 $f1
Truth table100%3/3
A and B
A= $i == $i1B= $f1
ABresult
0X0
100
111
207 # mask off low bits in first word (0 is low bit)
208 3 foreach (0 .. ($f1-1)) { $mask ^= (1 << $_) }
209 }
210 18 ▶ if ($i == $i2 and $f2 < 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $i2 and $f2 < 7) 3 15 18
Condition100%3/3
$i == $i2 and $f2 < 7
!ll&&!rl&&r
783
MC/DC100%2/2
$i == $i2 and $f2 < 7
$i == $i2 $f2 < 7
Truth table100%3/3
A and B
A= $i == $i2B= $f2 < 7
ABresult
0X0
100
111
211 # mask off high bits in last word (7 is high bit)
212 3 foreach (($f2+1) .. 7) { $mask ^= (1 << $_) }
213 }
214 18 $val = $val * 256 + ($mask & Get8u($dataPt, $i + $dirStart));
215 18 ▶ $extra .= sprintf('%.2x', $mask) if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 0 18 18
216 }
217 }
218 # shift word down until low bit is in position 0
219 35 ▶ until ($mask & 0x01) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mask & 1) 53 35 88
220 53 $val /= 2;
221 53 $mask >>= 1;
222 }
223 }
224 37 $et->HandleTag($tagTablePtr, $tag, $val,
225 DataPt => $dataPt,
226 DataPos => $dataPos,
227 Start => $dirStart + $i1,
228 Size => $i2 - $i1 + 1,
229 Extra => $extra,
230 );
231 }
232 6 return 1;
233 }
234  
235 #------------------------------------------------------------------------------
236 # Extract information from an Ogg FLAC file
237 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
238 # Returns: 1 on success, 0 if this wasn't a valid Ogg FLAC file
239 sub ProcessFLAC($$)
240 2 ▶{
Subroutine ProcessFLAC: called CC=10 SCAR=23.8
Pod undocumented
241 2 my ($et, $dirInfo) = @_;
242  
243 # must first check for leading/trailing ID3 information
244 2 ▶ unless ($$et{DoneID3}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($et->{"DoneID3"}) 1 1 2
245 1 require Image::ExifTool::ID3;
246 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
247 }
248 2 my $raf = $$dirInfo{RAF};
249 2 my $verbose = $et->Options('Verbose');
250 2 my $out = $et->Options('TextOut');
251 2 my ($buff, $err);
252  
253 # check FLAC signature
254 2 ▶ $raf->Read($buff, 4) == 4 and $buff eq 'fLaC' or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 4) == 4 and $buff eq "fLaC" or return 0 2 0 2
Condition33%1/3
$raf->Read($buff, 4) == 4 and $buff eq "fLaC"
!ll&&!rl&&r
002
MC/DC0%0/2
$raf->Read($buff, 4) == 4 and $buff eq "fLaC"
$raf->Read($buff, 4) == 4 $buff eq "fLaC"
Truth table33%1/3
A and B
A= $raf->Read($buff, 4) == 4B= $buff eq "fLaC"
ABresult
0X0
100
111
255 2 $et->SetFileType();
256 2 SetByteOrder('MM');
257 2 my $tagTablePtr = GetTagTable('Image::ExifTool::FLAC::Main');
258 2 for (;;) {
259 # read next metadata block header
260 5 ▶ $raf->Read($buff, 4) == 4 or last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 4) == 4 or last 4 1 5
261 4 my $flag = unpack('C', $buff);
262 4 my $size = unpack('N', $buff) & 0x00ffffff;
263 4 ▶ $raf->Read($buff, $size) == $size or $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $size) == $size or $err = 1, last 4 0 4
264 4 my $last = $flag & 0x80; # last-metadata-block flag
265 4 my $tag = $flag & 0x7f; # tag bits
266 4 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 4 4
267 0 print $out "FLAC metadata block, type $tag:\n";
268 0 $et->VerboseDump(\$buff, DataPos => $raf->Tell() - $size);
269 }
270 4 $et->HandleTag($tagTablePtr, $tag, $buff,
271 DataPt => \$buff,
272 DataPos => $raf->Tell() - $size,
273 Start => 0,
274 Size => $size,
275 );
276 4 ▶ last if $last; # all done if is set
Branch: 1 branch
BranchTrueFalseTotal
if $last 1 3 4
277 }
278 2 ▶ $err and $et->Warn('Format error in FLAC file');
Branch: 1 branch
BranchTrueFalseTotal
$err and $et->Warn("Format error in FLAC file") 0 2 2
279 2 return 1;
280 }
281  
282 5 1; # end
283  
284 __END__
« Image/ExifTool/Palm.pm ↑ Summary Image/ExifTool/Scalado.pm »