File Coverage

blib/lib/Image/ExifTool/FLAC.pm
Criterion Covered Total %
statement 79 82 96.3
branch 29 40 72.5
condition 21 32 65.6
subroutine 5 5 100.0
pod 0 2 0.0
total 134 161 83.2


line stmt bran cond sub pod time code
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 4     4   4482 use strict;
  4         7  
  4         131  
14 4     4   23 use vars qw($VERSION);
  4         7  
  4         159  
15 4     4   23 use Image::ExifTool qw(:DataAccess :Utils);
  4         8  
  4         5600  
16              
17             $VERSION = '1.09';
18              
19             sub ProcessBitStream($$$);
20              
21             # FLAC metadata blocks
22             %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             %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             %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             %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             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             {
160 5     5 0 14 my ($et, $dirInfo, $tagTablePtr) = @_;
161 5         17 my $dataPt = $$dirInfo{DataPt};
162 5         13 my $dataPos = $$dirInfo{DataPos};
163 5   50     24 my $dirStart = $$dirInfo{DirStart} || 0;
164 5   66     20 my $dirLen = $$dirInfo{DirLen} || (length($$dataPt) - $dirStart);
165 5         19 my $verbose = $et->Options('Verbose');
166 5         33 my $byteOrder = GetByteOrder();
167 5         8 my $tag;
168              
169 5 50       19 if ($verbose) {
170 0         0 $et->VPrint(0, " + [BitStream directory, $dirLen bytes, '${byteOrder}' order]\n");
171             }
172 5         41 foreach $tag (sort keys %$tagTablePtr) {
173 64 100       247 next unless $tag =~ /^Bit(\d+)-?(\d+)?/;
174 34   66     144 my ($b1, $b2) = ($1, $2 || $1); # start/end bit numbers in stream
175 34         113 my ($i1, $i2) = (int($b1 / 8), int($b2 / 8)); # start/end byte numbers
176 34         69 my ($f1, $f2) = ($b1 % 8, $b2 % 8); # start/end bit numbers within each byte
177 34 50       68 last if $i2 >= $dirLen;
178 34         45 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 34 100 66     162 if (ref $$tagTablePtr{$tag} ne 'HASH' or not $$tagTablePtr{$tag}{Format}) {
182 32         52 my ($i, $mask);
183 32         47 $val = 0;
184 32 0 0     71 $extra = ', Mask=0x' if $verbose and ($f1 != 0 or $f2 != 7);
      33        
185 32 100       65 if ($byteOrder eq 'MM') {
186             # loop from high byte to low byte
187 21         53 for ($i=$i1; $i<=$i2; ++$i) {
188 47         68 $mask = 0xff;
189 47 100 100     122 if ($i == $i1 and $f1) {
190             # mask off high bits in first word (0 is high bit)
191 10         31 foreach ((8-$f1) .. 7) { $mask ^= (1 << $_) }
  51         79  
192             }
193 47 100 100     114 if ($i == $i2 and $f2 < 7) {
194             # mask off low bits in last word (7 is low bit)
195 9         22 foreach (0 .. (6-$f2)) { $mask ^= (1 << $_) }
  25         43  
196             }
197 47         114 $val = $val * 256 + ($mask & Get8u($dataPt, $i + $dirStart));
198 47 50       126 $extra .= sprintf('%.2x', $mask) if $extra;
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         22 for ($i=$i2; $i>=$i1; --$i) {
205 18         25 $mask = 0xff;
206 18 100 100     46 if ($i == $i1 and $f1) {
207             # mask off low bits in first word (0 is low bit)
208 3         8 foreach (0 .. ($f1-1)) { $mask ^= (1 << $_) }
  14         22  
209             }
210 18 100 100     59 if ($i == $i2 and $f2 < 7) {
211             # mask off high bits in last word (7 is high bit)
212 3         15 foreach (($f2+1) .. 7) { $mask ^= (1 << $_) }
  12         19  
213             }
214 18         46 $val = $val * 256 + ($mask & Get8u($dataPt, $i + $dirStart));
215 18 50       53 $extra .= sprintf('%.2x', $mask) if $extra;
216             }
217             }
218             # shift word down until low bit is in position 0
219 32         74 until ($mask & 0x01) {
220 39         54 $val /= 2;
221 39         69 $mask >>= 1;
222             }
223             }
224 34         119 $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 5         19 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             {
241 2     2 0 7 my ($et, $dirInfo) = @_;
242              
243             # must first check for leading/trailing ID3 information
244 2 100       10 unless ($$et{DoneID3}) {
245 1         808 require Image::ExifTool::ID3;
246 1 50       7 Image::ExifTool::ID3::ProcessID3($et, $dirInfo) and return 1;
247             }
248 2         7 my $raf = $$dirInfo{RAF};
249 2         10 my $verbose = $et->Options('Verbose');
250 2         5 my $out = $et->Options('TextOut');
251 2         5 my ($buff, $err);
252              
253             # check FLAC signature
254 2 50 33     6 $raf->Read($buff, 4) == 4 and $buff eq 'fLaC' or return 0;
255 2         13 $et->SetFileType();
256 2         11 SetByteOrder('MM');
257 2         7 my $tagTablePtr = GetTagTable('Image::ExifTool::FLAC::Main');
258 2         7 for (;;) {
259             # read next metadata block header
260 5 100       21 $raf->Read($buff, 4) == 4 or last;
261 4         11 my $flag = unpack('C', $buff);
262 4         11 my $size = unpack('N', $buff) & 0x00ffffff;
263 4 50       12 $raf->Read($buff, $size) == $size or $err = 1, last;
264 4         9 my $last = $flag & 0x80; # last-metadata-block flag
265 4         7 my $tag = $flag & 0x7f; # tag bits
266 4 50       11 if ($verbose) {
267 0         0 print $out "FLAC metadata block, type $tag:\n";
268 0         0 $et->VerboseDump(\$buff, DataPos => $raf->Tell() - $size);
269             }
270 4         16 $et->HandleTag($tagTablePtr, $tag, $buff,
271             DataPt => \$buff,
272             DataPos => $raf->Tell() - $size,
273             Start => 0,
274             Size => $size,
275             );
276 4 100       11 last if $last; # all done if is set
277             }
278 2 50       8 $err and $et->Warn('Format error in FLAC file');
279 2         7 return 1;
280             }
281              
282             1; # end
283              
284             __END__