Image/ExifTool/Ogg.pm

filter: Statementstmt 91.2% 94 / 103 Branchbran 67.5% 50 / 74 Conditioncond 54.5% 18 / 33 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 72.3% 173 / 239 CC 50 SCAR 38.5
CC
50
Cov
77%
ProcessOGG
60.5
ProcessPacket
14.2
BEGIN
1.0
CRAP 46.9
« Image/ExifTool/SonyIDC.pm ↑ Summary Image/ExifTool/HTML.pm »
1 #------------------------------------------------------------------------------
2 # File: Ogg.pm
3 #
4 # Description: Read Ogg meta information
5 #
6 # Revisions: 2011/07/13 - P. Harvey Created (split from Vorbis.pm)
7 # 2016/07/14 - PH Added Ogg Opus support
8 #
9 # References: 1) http://www.xiph.org/vorbis/doc/
10 # 2) http://flac.sourceforge.net/ogg_mapping.html
11 # 3) http://www.theora.org/doc/Theora.pdf
12 #------------------------------------------------------------------------------
13  
14 package Image::ExifTool::Ogg;
15  
16 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 $VERSION = '1.04';
21  
22 3 my $MAX_PACKETS = 2; # maximum packets to scan from each stream at start of file
23  
24 # Information types recognized in Ogg files
25 3 %Image::ExifTool::Ogg::Main = (
26 NOTES => q{
27 ExifTool extracts the following types of information from Ogg files. See
28 L<http://www.xiph.org/vorbis/doc/> for the Ogg specification.
29 },
30 # (these are for documentation purposes only, and aren't used by the code below)
31 vorbis => { SubDirectory => { TagTable => 'Image::ExifTool::Vorbis::Main' } },
32 theora => { SubDirectory => { TagTable => 'Image::ExifTool::Theora::Main' } },
33 Opus => { SubDirectory => { TagTable => 'Image::ExifTool::Opus::Main' } },
34 FLAC => { SubDirectory => { TagTable => 'Image::ExifTool::FLAC::Main' } },
35 ID3 => { SubDirectory => { TagTable => 'Image::ExifTool::ID3::Main' } },
36 );
37  
38 #------------------------------------------------------------------------------
39 # Process Ogg packet
40 # Inputs: 0) ExifTool object ref, 1) data ref
41 # Returns: 1 on success
42 sub ProcessPacket($$)
43 4 ▶{
Subroutine ProcessPacket: called CC=12 SCAR=26.6
Pod undocumented
44 4 my ($et, $dataPt) = @_;
45 4 my $rtnVal = 0;
46 4 ▶ if ($$dataPt =~ /^(.)(vorbis|theora)/s or $$dataPt =~ /^(OpusHead|OpusTags)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$dataPt =~ /^(.)(vorbis|theora)/s or $$dataPt =~ /^(OpusHead|OpusTags)/) 4 0 4
Condition66%2/3
$$dataPt =~ /^(.)(vorbis|theora)/s or $$dataPt =~ /^(OpusHead|OpusTags)/
l!l&&r!l&&!r
220
MC/DC0%0/2
$$dataPt =~ /^(.)(vorbis|theora)/s or $$dataPt =~ /^(OpusHead|OpusTags)/
$$dataPt =~ /^(.)(vorbis|theora)/s $$dataPt =~ /^(OpusHead|OpusTags)/
Truth table66%2/3
A or B
A= $$dataPt =~ /^(.)(vorbis|theora)/sB= $$dataPt =~ /^(OpusHead|OpusTags)/
ABresult
1X1
011
000
47 4 ▶ my ($tag, $type, $pos) = $2 ? (ord($1), ucfirst($2), 7) : ($1, 'Opus', 8);
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 2 2 4
48 # this is an OGV file if it contains Theora video
49 4 ▶ $et->OverrideFileType('OGV') if $type eq 'Theora' and $$et{FILE_TYPE} eq 'OGG';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "Theora" and $et->{"FILE_TYPE"} eq "OGG" 0 4 4
Condition33%1/3
$type eq "Theora" and $et->{"FILE_TYPE"} eq "OGG"
!ll&&!rl&&r
400
MC/DC0%0/2
$type eq "Theora" and $et->{"FILE_TYPE"} eq "OGG"
$type eq "Theora" $et->{"FILE_TYPE"} eq "OGG"
Truth table33%1/3
A and B
A= $type eq "Theora"B= $et->{"FILE_TYPE"} eq "OGG"
ABresult
0X0
100
111
50 4 ▶ $et->OverrideFileType('OPUS') if $type eq 'Opus' and $$et{FILE_TYPE} eq 'OGG';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "Opus" and $et->{"FILE_TYPE"} eq "OGG" 2 2 4
Condition66%2/3
$type eq "Opus" and $et->{"FILE_TYPE"} eq "OGG"
!ll&&!rl&&r
202
MC/DC50%1/2
$type eq "Opus" and $et->{"FILE_TYPE"} eq "OGG"
$type eq "Opus" $et->{"FILE_TYPE"} eq "OGG"
Truth table66%2/3
A and B
A= $type eq "Opus"B= $et->{"FILE_TYPE"} eq "OGG"
ABresult
0X0
100
111
51 4 my $tagTablePtr = GetTagTable("Image::ExifTool::${type}::Main");
52 4 my $tagInfo = $et->GetTagInfo($tagTablePtr, $tag);
53 4 ▶ return 0 unless $tagInfo and $$tagInfo{SubDirectory};
Branch: 1 branch
BranchTrueFalseTotal
unless $tagInfo and $tagInfo->{"SubDirectory"} 0 4 4
Condition33%1/3
$tagInfo and $tagInfo->{"SubDirectory"}
!ll&&!rl&&r
004
MC/DC0%0/2
$tagInfo and $tagInfo->{"SubDirectory"}
$tagInfo $tagInfo->{"SubDirectory"}
Truth table33%1/3
A and B
A= $tagInfoB= $tagInfo->{"SubDirectory"}
ABresult
0X0
100
111
54 4 my $subdir = $$tagInfo{SubDirectory};
55 my %dirInfo = (
56 DataPt => $dataPt,
57 DirName => $$tagInfo{Name},
58 4 DirStart => $pos,
59 );
60 4 my $table = GetTagTable($$subdir{TagTable});
61 # set group1 so Theoris comments can be distinguised from Vorbis comments
62 4 ▶ $$et{SET_GROUP1} = $type if $type eq 'Theora';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "Theora" 0 4 4
63 4 ▶ SetByteOrder($$subdir{ByteOrder}) if $$subdir{ByteOrder};
Branch: 1 branch
BranchTrueFalseTotal
if $subdir->{"ByteOrder"} 0 4 4
64 4 $rtnVal = $et->ProcessDirectory(\%dirInfo, $table);
65 4 SetByteOrder('II');
66 4 delete $$et{SET_GROUP1};
67 }
68 4 return $rtnVal;
69 }
70  
71 #------------------------------------------------------------------------------
72 # Extract information from an Ogg file
73 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
74 # Returns: 1 on success, 0 if this wasn't a valid Ogg file
75 sub ProcessOGG($$)
76 3 ▶{
Subroutine ProcessOGG: called CC=39 SCAR=41.0
Pod undocumented
77 3 my ($et, $dirInfo) = @_;
78  
79 # must first check for leading/trailing ID3 information
80 3 ▶ unless ($$et{DoneID3}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($et->{"DoneID3"}) 3 0 3
81 3 require Image::ExifTool::ID3;
82 3 ▶ Image::ExifTool::ID3::ProcessID3($et, $dirInfo) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
&Image::ExifTool::ID3::ProcessID3($et, $dirInfo) and return 1 0 3 3
83 }
84 3 my $raf = $$dirInfo{RAF};
85 3 my $verbose = $et->Options('Verbose');
86 3 my $out = $et->Options('TextOut');
87 3 my ($success, $page, $packets, $streams, $stream) = (0,0,0,0,'');
88 3 my ($buff, $flag, %val, $numFlac, %streamPage);
89  
90 3 for (;;) {
91 # must read ahead to next page to see if it is a continuation
92 # (this code would be a lot simpler if the continuation flag
93 # was on the leading instead of the trailing page!)
94 10 ▶ if ($raf and $raf->Read($buff, 28) == 28) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raf and $raf->Read($buff, 28) == 28) { } 9 1 10
Condition66%2/3
$raf and $raf->Read($buff, 28) == 28
!ll&&!rl&&r
019
MC/DC50%1/2
$raf and $raf->Read($buff, 28) == 28
$raf $raf->Read($buff, 28) == 28
Truth table66%2/3
A and B
A= $rafB= $raf->Read($buff, 28) == 28
ABresult
0X0
100
111
95 # validate magic number
96 9 ▶ unless ($buff =~ /^OggS/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buff =~ /^OggS/) 0 9 9
97 0 ▶ $success and $et->Warn('Lost synchronization');
Branch: 1 branch
BranchTrueFalseTotal
$success and $et->Warn("Lost synchronization") 0 0 0
98 0 last;
99 }
100 9 ▶ unless ($success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 3 6 9
101 # set file type and initialize on first page
102 3 $success = 1;
103 3 $et->SetFileType();
104 3 SetByteOrder('II');
105 }
106 9 $flag = Get8u(\$buff, 5); # page flag
107 9 $stream = Get32u(\$buff, 14); # stream serial number
108 9 ▶ if ($flag & 0x02) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag & 2) { } 3 6 9
109 3 ++$streams; # count start-of-stream pages
110 3 $streamPage{$stream} = $page = 0;
111 } else {
112 6 $page = $streamPage{$stream};
113 }
114 9 ▶ ++$packets unless $flag & 0x01; # keep track of packet count
Branch: 1 branch
BranchTrueFalseTotal
unless $flag & 1 8 1 9
115 } else {
116 # all done unless we have to process our last packet
117 1 ▶ last unless %val;
Branch: 1 branch
BranchTrueFalseTotal
unless %val 0 1 1
118 1 ($stream) = sort keys %val; # take a stream
119 1 $flag = 0; # no continuation
120 1 undef $raf; # flag for done reading
121 }
122  
123 10 ▶ if (defined $numFlac) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $numFlac) { } 2 8 10
124 # stop to process FLAC headers if we hit the end of file
125 2 ▶ last unless $raf;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf 1 1 2
126 1 --$numFlac; # one less header packet to read
127 } else {
128 # can finally process previous packet from this stream
129 # unless this is a continuation page
130 8 ▶ if (defined $val{$stream} and not $flag & 0x01) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val{$stream} and not $flag & 1) 4 4 8
Condition100%3/3
defined $val{$stream} and not $flag & 1
!ll&&!rl&&r
314
MC/DC100%2/2
defined $val{$stream} and not $flag & 1
defined $val{$stream} not $flag & 1
Truth table100%3/3
A and not(B)
A= defined $val{$stream}B= not $flag & 1
ABresult
0X0
100
111
131 4 ProcessPacket($et, \$val{$stream});
132 4 delete $val{$stream};
133 # only read the first $MAX_PACKETS packets from each stream
134 4 ▶ if ($packets > $MAX_PACKETS * $streams or not defined $raf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packets > $MAX_PACKETS * $streams or not defined $raf) 2 2 4
Condition66%2/3
$packets > $MAX_PACKETS * $streams or not defined $raf
l!l&&r!l&&!r
202
MC/DC50%1/2
$packets > $MAX_PACKETS * $streams or not defined $raf
$packets > $MAX_PACKETS * $streams not defined $raf
Truth table66%2/3
A or not(B)
A= $packets > $MAX_PACKETS * $streamsB= not defined $raf
ABresult
1X1
011
000
135 2 ▶ last unless %val; # all done (success!)
Branch: 1 branch
BranchTrueFalseTotal
unless %val 2 0 2
136 }
137 }
138 # stop processing Ogg if we have scanned enough packets
139 6 ▶ last if $packets > $MAX_PACKETS * $streams and not %val;
Branch: 1 branch
BranchTrueFalseTotal
if $packets > $MAX_PACKETS * $streams and not %val 0 6 6
Condition33%1/3
$packets > $MAX_PACKETS * $streams and not %val
!ll&&!rl&&r
600
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
ABresult
0X0
100
111
140 }
141  
142 # continue processing the current page
143 7 my $pageNum = Get32u(\$buff, 18); # page sequence number
144 7 my $nseg = Get8u(\$buff, 26); # number of segments
145 # calculate total data length
146 7 my $dataLen = Get8u(\$buff, 27);
147 7 ▶ if ($nseg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nseg) 7 0 7
148 7 ▶ last unless $raf;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf 0 7 7
149 7 ▶ $raf->Read($buff, $nseg-1) == $nseg-1 or last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $nseg - 1) == $nseg - 1 or last 7 0 7
150 7 my @segs = unpack('C*', $buff);
151 # could check that all these (but the last) are 255...
152 7 foreach (@segs) { $dataLen += $_ }
153 }
154 7 ▶ if (defined $page) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page) 7 0 7
155 7 ▶ if ($page == $pageNum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page == $pageNum) { } 7 0 7
156 7 $streamPage{$stream} = ++$page;
157 } else {
158 0 $et->Warn('Missing page(s) in Ogg file');
159 0 undef $page;
160 0 delete $streamPage{$stream};
161 }
162 }
163 # read page data
164 7 ▶ last unless $raf and $raf->Read($buff, $dataLen) == $dataLen;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf and $raf->Read($buff, $dataLen) == $dataLen 0 7 7
Condition33%1/3
$raf and $raf->Read($buff, $dataLen) == $dataLen
!ll&&!rl&&r
007
MC/DC0%0/2
$raf and $raf->Read($buff, $dataLen) == $dataLen
$raf $raf->Read($buff, $dataLen) == $dataLen
Truth table33%1/3
A and B
A= $rafB= $raf->Read($buff, $dataLen) == $dataLen
ABresult
0X0
100
111
165 7 ▶ if ($verbose > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 1) 0 7 7
166 0 printf $out "Page %d, stream 0x%x, flag 0x%x (%d bytes)\n",
167 $pageNum, $stream, $flag, $dataLen;
168 0 $et->VerboseDump(\$buff, DataPos => $raf->Tell() - $dataLen);
169 }
170 7 ▶ if (defined $val{$stream}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val{$stream}) { } 2 5 7
elsif (not $flag & 1) { } 5 0 5
171 2 $val{$stream} .= $buff; # add this continuation page
172 } elsif (not $flag & 0x01) { # ignore remaining pages of a continued packet
173 # ignore the first page of any packet we aren't parsing
174 5 ▶ if ($buff =~ /^(.(vorbis|theora)|Opus(Head|Tags))/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buff =~ /^(.(vorbis|theora)|Opus(Head|Tags))/s) { } 4 1 5
elsif ($buff =~ /^\x7fFLAC..(..)/s) { } 1 0 1
175 4 $val{$stream} = $buff; # save this page
176 } elsif ($buff =~ /^\x7fFLAC..(..)/s) {
177 1 $numFlac = unpack('n',$1);
178 1 $val{$stream} = substr($buff, 9);
179 }
180 }
181 7 ▶ if (defined $numFlac) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $numFlac) { } 2 5 7
elsif (defined $val{$stream} and $flag & 4) { } 0 5 5
182 # stop to process FLAC headers if we have them all
183 2 ▶ last if $numFlac <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $numFlac <= 0 0 2 2
184 ▶ } elsif (defined $val{$stream} and $flag & 0x04) {
Condition33%1/3
defined $val{$stream} and $flag & 4
!ll&&!rl&&r
050
MC/DC0%0/2
defined $val{$stream} and $flag & 4
defined $val{$stream} $flag & 4
Truth table33%1/3
A and B
A= defined $val{$stream}B= $flag & 4
ABresult
0X0
100
111
185 # process Ogg packet now if end-of-stream bit is set
186 0 ProcessPacket($et, \$val{$stream});
187 0 delete $val{$stream};
188 }
189 }
190 3 ▶ if (defined $numFlac and defined $val{$stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $numFlac and defined $val{$stream}) 1 2 3
Condition66%2/3
defined $numFlac and defined $val{$stream}
!ll&&!rl&&r
201
MC/DC50%1/2
defined $numFlac and defined $val{$stream}
defined $numFlac defined $val{$stream}
Truth table66%2/3
A and B
A= defined $numFlacB= defined $val{$stream}
ABresult
0X0
100
111
191 # process FLAC headers as if it was a complete FLAC file
192 1 require Image::ExifTool::FLAC;
193 1 my %dirInfo = ( RAF => File::RandomAccess->new(\$val{$stream}) );
194 1 Image::ExifTool::FLAC::ProcessFLAC($et, \%dirInfo);
195 }
196 3 return $success;
197 }
198  
199 3 1; # end
200  
201 __END__
« Image/ExifTool/SonyIDC.pm ↑ Summary Image/ExifTool/HTML.pm »