VideoTag.pm

filter: Statementstmt 75.7% 75 / 99 Branchbran 50.0% 23 / 46 Conditioncond 40.0% 2 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 84.2% 16 / 19 Podpod 100% 6 / 6 totaltotal 69.1% 123 / 178 CC 27 SCAR 20.1
CC
27
Cov
67%
parse
13.3
_parse_h263
12.8
_parse_on2vp6_alpha
12.0
CRAP 7.5
« Body.pm ↑ Summary ToSWF.pm »
1 package FLV::VideoTag;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use base 'FLV::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use FLV::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use FLV::Tag;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our $VERSION = '0.24';
15  
16 =for stopwords codec
17  
18 =head1 NAME
19  
20 FLV::VideoTag - Flash video file data structure
21  
22 =head1 LICENSE
23  
24 See L<FLV::Info>
25  
26 =head1 METHODS
27  
28 This is a subclass of L<FLV::Base>.
29  
30 =over
31  
32 =item $self->parse($fileinst)
33  
34 Takes a FLV::File instance and extracts an FLV video tag from the file
35 stream. This method throws exceptions if the
36 stream is not a valid FLV v1.0 or v1.1 file.
37  
38 There is no return value.
39  
40 Note: this method needs more work to extract the codec specific data.
41  
42 =cut
43  
44 sub parse
45 5662 ▶{
Subroutine parse: called CC=9 SCAR=25.9
Pod documented
46 5662 my $self = shift;
47 5662 my $file = shift;
48 5662 my $datasize = shift;
49  
50 5662 my $flags = unpack 'C', $file->get_bytes(1);
51  
52 # The spec PDF is wrong -- type comes first, then codec
53 5662 my $type = ($flags >> 4) & 0x0f;
54 5662 my $codec = $flags & 0x0f;
55  
56 5662 ▶ if (!exists $VIDEO_CODEC_IDS{$codec})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $FLV::VideoTag::VIDEO_CODEC_IDS{$codec}) 0 5662 5662
57 {
58 0 die 'Unknown video codec ' . $codec . ' at byte ' . $file->get_pos(-1);
59 }
60 5662 ▶ if (!exists $VIDEO_FRAME_TYPES{$type})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $FLV::VideoTag::VIDEO_FRAME_TYPES{$type}) 0 5662 5662
61 {
62 0 die 'Unknown video frame type at byte ' . $file->get_pos(-1);
63 }
64  
65 5662 $self->{codec} = $codec;
66 5662 $self->{type} = $type;
67  
68 5662 my $pos = $file->get_pos();
69  
70 5662 $self->{data} = $file->get_bytes($datasize - 1);
71  
72 my $result
73 = 2 == $self->{codec} ? $self->_parse_h263($pos)
74 : 3 == $self->{codec} ? $self->_parse_screen_video($pos)
75 : 4 == $self->{codec} ? $self->_parse_on2vp6($pos)
76 : 5 == $self->{codec} ? $self->_parse_on2vp6_alpha($pos)
77 : 6 == $self->{codec} ? $self->_parse_screen_video($pos)
78 5662 ▶ : 7 == $self->{codec} ? $self->_parse_avc($pos)
Branch: 6 branches
BranchTrueFalseTotal
2 == $self->{'codec'} ? : 4172 1490 5662
3 == $self->{'codec'} ? : 0 1490 1490
4 == $self->{'codec'} ? : 1490 0 1490
5 == $self->{'codec'} ? : 0 0 0
6 == $self->{'codec'} ? : 0 0 0
7 == $self->{'codec'} ? : 0 0 0
79 : die 'Unknown video type';
80  
81 5662 return;
82 }
83  
84 sub _parse_h263
85 4470 ▶{
Subroutine _parse_h263: called CC=10 SCAR=25.5
86 4470 my $self = shift;
87 4470 my $pos = shift;
88  
89 # Surely there's a better way than this....
90 4470 my $bits = unpack 'B67', $self->{data};
91 4470 my $sizecode = substr $bits, 30, 3;
92 4470 my @d = (
93 (ord pack 'B8', substr $bits, 33, 8),
94 (ord pack 'B8', substr $bits, 41, 8),
95 (ord pack 'B8', substr $bits, 49, 8),
96 (ord pack 'B8', substr $bits, 57, 8),
97 );
98 4470 ▶ my ($width, $height, $offset)
Branch: 7 branches
BranchTrueFalseTotal
'000' eq $sizecode ? : 0 4470 4470
'001' eq $sizecode ? : 0 4470 4470
'010' eq $sizecode ? : 0 4470 4470
'011' eq $sizecode ? : 0 4470 4470
'100' eq $sizecode ? : 0 4470 4470
'101' eq $sizecode ? : 4470 0 4470
'110' eq $sizecode ? : 0 0 0
99 = '000' eq $sizecode ? ($d[0], $d[1], 16)
100 : '001' eq $sizecode ? ($d[0] * 256 + $d[1], $d[2] * 256 + $d[3], 32)
101 : '010' eq $sizecode ? (352, 288, 0)
102 : '011' eq $sizecode ? (176, 144, 0)
103 : '100' eq $sizecode ? (128, 96, 0)
104 : '101' eq $sizecode ? (320, 240, 0)
105 : '110' eq $sizecode ? (160, 120, 0)
106 : die 'Illegal value for H.263 size code at byte ' . $pos;
107  
108 4470 $self->{width} = $width;
109 4470 $self->{height} = $height;
110  
111 4470 my $typebits = substr $bits, 33 + $offset, 2;
112 4470 my @typebits = split m//xms, $typebits;
113 4470 my $type = 1 + $typebits[0] * 2 + $typebits[1];
114 4470 ▶ if (!defined $self->{type})
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'type'}) { } 298 4172 4470
elsif ($type != $self->{'type'}) { } 0 4172 4172
115 {
116 298 $self->{type} = $type;
117 }
118 elsif ($type != $self->{type})
119 {
120 0 warn "Type mismatch: header says $VIDEO_FRAME_TYPES{$self->{type}}, "
121 . "data says $VIDEO_FRAME_TYPES{$type}";
122 }
123  
124 4470 return;
125 }
126  
127 sub _parse_screen_video
128 0 ▶{
Subroutine _parse_screen_video: not called CC=2 SCAR=17.9
129 0 my $self = shift;
130 0 my $pos = shift;
131  
132 # Extract 4 bytes, big-endian
133 0 my ($width, $height) = unpack 'nn', $self->{data};
134  
135 # Only use the lower 12 bits of each
136 0 $width &= 0x3fff;
137 0 $height &= 0x3fff;
138  
139 0 $self->{width} = $width;
140 0 $self->{height} = $height;
141  
142 0 ▶ $self->{type} ||= 1;
Condition0%0/2
$self->{"type"} ||= 1
l!l
00
MC/DC0%0/1
$self->{"type"} ||= 1
$self->{"type"}
Truth table0%0/2
A
A= $self->{"type"}
Aresult
11
00
143  
144 0 return;
145 }
146  
147 sub _parse_on2vp6
148 1788 ▶{
Subroutine _parse_on2vp6: called CC=3 SCAR=11.0
149 1788 my $self = shift;
150 1788 my $pos = shift;
151  
152 1788 ▶ if (!$self->{type})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"type"}) 298 1490 1788
153 {
154  
155 # Bit 7 of the header (after 8 bits of offset) distinguishes
156 # keyframe from interframe
157 # See: http://use.perl.org/~ChrisDolan/journal/30427
158 298 my @bytes = unpack 'CC', $self->{data};
159 298 ▶ $self->{type} = 0 == ($bytes[1] & 0x80) ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
0 == ($bytes[1] & 128) ? : 10 288 298
160 }
161  
162 1788 return;
163 }
164  
165 sub _parse_on2vp6_alpha
166 0 ▶{
Subroutine _parse_on2vp6_alpha: not called CC=3 SCAR=24.8
167 0 my $self = shift;
168 0 my $pos = shift;
169  
170 0 ▶ if (!$self->{type})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"type"}) 0 0 0
171 {
172  
173 # Bit 7 of the header (after 32 bits of offset) distinguishes
174 # keyframe from interframe
175 0 my @bytes = unpack 'CCCCC', $self->{data};
176 0 ▶ $self->{type} = 0 == ($bytes[4] & 0x80) ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
0 == ($bytes[4] & 128) ? : 0 0 0
177 }
178  
179 0 return;
180 }
181  
182 sub _parse_avc
183 0 ▶{
Subroutine _parse_avc: not called CC=1 SCAR=6.9
184 0 my $self = shift;
185 0 my $pos = shift;
186  
187 0 my @time;
188 0 ($self->{avc_packet_type}, $time[0], $time[1], $time[2]) = unpack 'CCCC', $self->{data};
189 0 $self->{composition_time} = ($time[0] * 256 + $time[1]) * 256 + $time[2];
190  
191 0 return;
192 }
193  
194 =item $self->clone()
195  
196 Create an independent copy of this instance.
197  
198 =cut
199  
200 sub clone
201 1192 ▶{
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
202 1192 my $self = shift;
203  
204 1192 my $copy = FLV::VideoTag->new;
205 1192 FLV::Tag->copy_tag($self, $copy);
206 1192 for my $key (qw( codec type width height data avc_packet_type composition_time )) {
207 8344 ▶ if (exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$key}) 5960 2384 8344
208 5960 $copy->{$key} = $self->{$key};
209 }
210 }
211 1192 return $copy;
212 }
213  
214 =item $self->serialize()
215  
216 Returns a byte string representation of the tag data. Throws an
217 exception via croak() on error.
218  
219 =cut
220  
221 sub serialize
222 3237 ▶{
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
223 3237 my $self = shift;
224  
225 3237 my $flags = pack 'C', ($self->{type} << 4) | $self->{codec};
226 3237 return $flags . $self->{data};
227 }
228  
229 =item $self->get_info()
230  
231 Returns a hash of FLV metadata. See FLV::Info for more details.
232  
233 =cut
234  
235 sub get_info
236 4 ▶{
Subroutine get_info: called CC=1 SCAR=0.0
Pod documented
237 4 my ($pkg, @args) = @_;
238  
239 4 return $pkg->_get_info(
240 'video',
241 {
242 type => \%VIDEO_FRAME_TYPES,
243 codec => \%VIDEO_CODEC_IDS,
244 width => undef,
245 height => undef,
246 },
247 \@args
248 );
249 }
250  
251 =item $self->is_keyframe()
252  
253 Returns a boolean.
254  
255 =cut
256  
257 sub is_keyframe
258 6176 ▶{
Subroutine is_keyframe: called CC=3 SCAR=11.1
Pod documented
259 6176 my $self = shift;
260 6176 ▶ return $self->{type} && 1 == $self->{type} ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'type'} && 1 == $self->{'type'} ? : 171 6005 6176
Condition66%2/3
$self->{"type"} and 1 == $self->{"type"}
!ll&&!rl&&r
06005171
MC/DC50%1/2
$self->{"type"} and 1 == $self->{"type"}
$self->{"type"} 1 == $self->{"type"}
Truth table66%2/3
A and B
A= $self->{"type"}B= 1 == $self->{"type"}
ABresult
0X0
100
111
261 }
262  
263 =item $self->get_time()
264  
265 Returns the time in milliseconds for this tag.
266  
267 =cut
268  
269 sub get_time
270 3386 ▶{
Subroutine get_time: called CC=1 SCAR=0.0
Pod documented
271 3386 my $self = shift;
272 3386 return $self->{start};
273 }
274  
275 6 1;
276  
277 __END__
« Body.pm ↑ Summary ToSWF.pm »