File Coverage

blib/lib/Image/ExifTool/FLAC.pm
Criterion Covered Total %
statement 79 82 96.3
branch 29 40 72.5
condition 20 32 62.5
subroutine 5 5 100.0
pod 0 2 0.0
total 133 161 82.6


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