Info/MPEG/Video.pm

filter: Statementstmt 94.1% 112 / 119 Branchbran 86.1% 31 / 36 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 11 totaltotal 86.5% 173 / 200 CC 23 SCAR 13.6
CC
23
Cov
92%
is_video
8.0
get_extensions
5.2
parse
4.0
CRAP 3.9
« Info.pm ↑ Summary Info/Magic.pm »
1 ##------------------------------------------------------------------------
2 ## Package: Video::Info::MPEG::Video
3 ## Author: Benjamin R. Ginter
4 ## Notice: Copyright (c) 2001 Benjamin R. Ginter
5 ## Purpose: Parse video streams
6 ## Comments: None
7 ## CVS: $Id: Video.pm,v 1.4 2003/07/08 07:35:33 allenday Exp $
8 ##------------------------------------------------------------------------
9  
10 package Video::Info::MPEG::Video;
11 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Video::Info::MPEG;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Video::Info::MPEG::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use base qw(Video::Info::MPEG);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
19 12 my $self = shift;
20 12 my %param = @_;
21 12 $self->init_attributes(@_);
22 12 $self->handle($self->filename);
23 12 ▶ $self->context($param{-context} || 'video');
Condition100%2/2
$param{'-context'} || 'video'
l!l
66
MC/DC100%1/1
$param{'-context'} || 'video'
$param{'-context'}
Truth table100%2/2
A
A= $param{'-context'}
Aresult
11
00
24 }
25  
26 ##------------------------------------------------------------------------
27 ## parse()
28 ##
29 ## Parse a video stream
30 ##------------------------------------------------------------------------
31 9 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
Pod undocumented
32 9 my $self = shift;
33 9 my $offset = shift;
34  
35 9 ▶ $offset = 0 if !defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 6 3 9
36  
37 9 ▶ $self->{offset} = $self->{last_offset} || $offset;
Condition100%3/3
$self->{'last_offset'} || $offset
l!l&&r!l&&!r
225
MC/DC100%2/2
$self->{'last_offset'} || $offset
$self->{'last_offset'} $offset
Truth table100%3/3
A || B
A= $self->{'last_offset'}B= $offset
ABresult
1X1
011
000
38  
39 9 print "Video::Info::MPEG::Video::parse( $offset )\n" if DEBUG;
40 # print "\n", '-' x 74, "\n", "Parse Video: $offset\n", '-' x 74, "\n";
41  
42 ## Make sure we have video
43 9 ▶ $self->is_video() or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_video or return 0 6 3 9
44 #if we made it this far, assume a bona fide MPEG
45 6 $self->type('MPEG');
46 6 $self->get_size();
47 6 $self->get_frame_rate();
48 6 $self->get_aspect_ratio();
49 6 $self->get_bitrate();
50 6 $self->get_duration();
51 6 $self->get_extensions();
52 6 $self->get_gop();
53 6 $self->get_header_size();
54  
55 6 if ( DEBUG ) {
56 print " DIMENSIONS: ", $self->width, 'x', $self->height, "\n";
57 printf " FRAME RATE: %0.2f fps\n", $self->fps;
58 printf " ASPECT RATIO: %s ( %d )\n", $self->aspect, $self->aspect_raw;
59 print " BITRATE: ", $self->vrate, "\n";
60 print " DURATION: ", $self->duration, "\n";
61 print " HEADER SIZE: $self->{video_header_size}\n";
62 }
63  
64 6 return 1;
65 }
66  
67 ##------------------------------------------------------------------------
68 ## get_size()
69 ##
70 ## Get the width and height
71 ##------------------------------------------------------------------------
72 6 ▶sub get_size {
Subroutine get_size: called CC=3 SCAR=12.0
Pod undocumented
73 6 my $self = shift;
74  
75 6 $self->{offset} += 4;
76  
77 6 $self->width( $self->grab( 2, $self->{offset} ) >> 4 );
78 6 $self->height( $self->grab( 2, $self->{offset} + 1 ) & 0x0FFF );
79 6 ▶ if ( !defined $self->width || !defined $self->height ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->width and defined $self->height) 0 6 6
Condition33%1/3
defined $self->width and defined $self->height
!ll&&!rl&&r
006
MC/DC0%0/2
defined $self->width and defined $self->height
defined $self->width defined $self->height
Truth table33%1/3
A and B
A= defined $self->widthB= defined $self->height
ABresult
0X0
100
111
80 0 return 0;
81 }
82 6 return 1;
83 }
84  
85 ##------------------------------------------------------------------------
86 ## is_video()
87 ##
88 ## Verify we're really dealing with a video packet
89 ##
90 ## This method searches up to eof for the start code in case there is
91 ## junk at the beginning of the file. Should we limit this somehow?
92 ##------------------------------------------------------------------------
93 9 ▶sub is_video {
Subroutine is_video: called CC=8 SCAR=20.8
Pod undocumented
94 9 my $self = shift;
95  
96 9 print "is_video: offset $self->{offset}\n" if DEBUG;
97  
98 # return 0 if !$self->next_start_code( SEQ_START_CODE, $self->{offset} );
99  
100 9 ▶ while ( $self->{offset} <= $self->filesize - 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"offset"} <= $self->filesize - 4) 145032 1 145033
101 145032 my $a = $self->get_byte( $self->{offset} );
102 145032 ▶ if ( $a != 0x00 ) { $self->{offset}++; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a != 0) 140296 4736 145032
103
104 4736 my $b = $self->get_byte( $self->{offset} + 1 );
105 4736 ▶ if ( $b != 0x00 ) { $self->{offset} += 2; next; };
Branch: 1 branch
BranchTrueFalseTotal
if ($b != 0) 3568 1168 4736
106  
107 1168 my $c = $self->get_byte( $self->{offset} + 2 );
108 1168 ▶ if ( $c != 0x01 ) { $self->{offset} += 3; next; };
Branch: 1 branch
BranchTrueFalseTotal
if ($c != 1) 409 759 1168
109  
110 759 my $d = $self->get_byte( $self->{offset} + 3 );
111  
112 759 printf "Found 0x%02x @ %d\n", $d, $self->{offset} + 3 if DEBUG;
113 # sleep 1;
114  
115 759 ▶ if ( $d == SEQ_START_CODE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d == 179) { } 5 754 759
elsif ($self->{'context'} eq 'video' and $d == 187) { } 3 1070 1073
116 5 return 1;
117 }
118 ▶ elsif ( $self->{context} eq 'video' && $d == SYS_PKT ) {
Condition100%3/3
$self->{"context"} eq "video" and $d == 187
!ll&&!rl&&r
3194323
MC/DC100%2/2
$self->{"context"} eq "video" and $d == 187
$self->{"context"} eq "video" $d == 187
Truth table100%3/3
A and B
A= $self->{"context"} eq "video"B= $d == 187
ABresult
0X0
100
111
119 3 print "Returning because video context\n" if DEBUG;
120 3 return 0;
121 }
122 751 $self->{offset}++;
123 }
124  
125 1 $self->{offset} = $self->{last_offset};
126  
127 1 return 1;
128 }
129  
130 ##------------------------------------------------------------------------
131 ## get_frame_rate()
132 ##
133 ## Extract the frame_rate index and do the lookup
134 ##------------------------------------------------------------------------
135 6 ▶sub get_frame_rate {
Subroutine get_frame_rate: called CC=2 SCAR=7.5
Pod undocumented
136 6 my $self = shift;
137  
138 6 $self->{offset} += 3;
139  
140 6 my $frame_rate_index = $self->grab( 1, $self->{offset} ) & 0x0f;
141  
142 6 ▶ if ( $frame_rate_index > 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame_rate_index > 8) 0 6 6
143 0 print "Invalid frame rate index: $frame_rate_index\n" if DEBUG;
144 ## $self->fps( 0.0 );
145 0 return 0;
146 }
147  
148 6 $self->fps( $FRAME_RATE->[ $frame_rate_index ] );
149
150 6 return 1;
151 }
152  
153 ##------------------------------------------------------------------------
154 ## get_aspect_ratio()
155 ##
156 ## Extract the aspect ratio index and do the lookup.
157 ##
158 ## NOTE: Don't die() on invalid aspect ratios as they are fairly common
159 ## For example, 320x240 is invalid. :)
160 ##------------------------------------------------------------------------
161 6 ▶sub get_aspect_ratio {
Subroutine get_aspect_ratio: called CC=3 SCAR=11.1
Pod undocumented
162 6 my $self = shift;
163  
164 6 my $aspect = ( $self->grab( 1, $self->{offset} ) & 0xF0 ) >> 4;
165 6 ▶ if ( !$aspect ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aspect) 0 6 6
166 # print "Invalid aspect ratio: $aspect\n";
167 0 return 0;
168 }
169 6 ▶ if ( $aspect > $#{ $ASPECT_RATIO } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aspect > $#{$Video::Info::MPEG::Video::ASPECT_RATIO;}) { } 3 3 6
170 # print "Reserved aspect ratio: $aspect\n";
171 3 $self->aspect( 'Reserved' );
172 }
173 else {
174 # print "Aspect Ratio: ", $ASPECT_RATIO->[ $aspect ], "\n";
175 3 $self->aspect( $ASPECT_RATIO->[ $aspect ] );
176 }
177  
178 6 $self->aspect_raw( $aspect );
179  
180 6 return 1;
181 }
182  
183 ##------------------------------------------------------------------------
184 ## get_bitrate()
185 ##
186 ## From the MPEG-2.2 spec:
187 ##
188 ## bit_rate -- This is a 30-bit integer. The lower 18 bits of the
189 ## integer are in bit_rate_value and the upper 12 bits are in
190 ## bit_rate_extension. The 30-bit integer specifies the bitrate of the
191 ## bitstream measured in units of 400 bits/second, rounded upwards.
192 ## The value zero is forbidden.
193 ##
194 ## So ignoring all the variable bitrate stuff for now, this 30 bit integer
195 ## multiplied times 400 bits/sec should give the rate in bits/sec.
196 ##
197 ## TODO: Variable bitrates? I need one that implements this.
198 ##
199 ## Continued from the MPEG-2.2 spec:
200 ##
201 ## If the bitstream is a constant bitrate stream, the bitrate specified
202 ## is the actual rate of operation of the VBV specified in annex C. If
203 ## the bitstream is a variable bitrate stream, the STD specifications in
204 ## ISO/IEC 13818-1 supersede the VBV, and the bitrate specified here is
205 ## used to dimension the transport stream STD (2.4.2 in ITU-T Rec. xxx |
206 ## ISO/IEC 13818-1), or the program stream STD (2.4.5 in ITU-T Rec. xxx |
207 ## ISO/IEC 13818-1).
208 ##
209 ## If the bitstream is not a constant rate bitstream the vbv_delay
210 ## field shall have the value FFFF in hexadecimal.
211 ##
212 ## Given the value encoded in the bitrate field, the bitstream shall be
213 ## generated so that the video encoding and the worst case multiplex
214 ## jitter do not cause STD buffer overflow or underflow.
215 ##
216 ##
217 ##------------------------------------------------------------------------
218 6 ▶sub get_bitrate {
Subroutine get_bitrate: called CC=2 SCAR=6.9
Pod undocumented
219 6 my $self = shift;
220  
221 6 $self->{offset}++;
222  
223 ## grab a short
224 6 my $bitrate = $self->grab( 2, $self->{offset} ) << 2;
225 6 my $lasttwo = $self->get_byte( $self->{offset} + 2 ) >> 6;
226  
227 6 ▶ if(!$self->vrate){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->vrate) { } 4 2 6
228 4 $self->vrate( ( $bitrate | $lasttwo ) * 400);
229 } else {
230 }
231 }
232  
233 ##------------------------------------------------------------------------
234 ## get_duration()
235 ##
236 ##
237 ##------------------------------------------------------------------------
238 6 ▶sub get_duration {
Subroutine get_duration: called CC=1 SCAR=0.0
Pod undocumented
239 6 my $self = shift;
240 6 $self->duration ( ( $self->filesize * 8 ) / ( $self->vrate * 400 ) );
241 }
242  
243 ##------------------------------------------------------------------------
244 ## get_extensions()
245 ##
246 ## TODO: make the $START_CODE->{$code} description the actual method name
247 ## for the extension handler.
248 ##------------------------------------------------------------------------
249 6 ▶sub get_extensions {
Subroutine get_extensions: called CC=5 SCAR=16.5
Pod undocumented
250 6 my $self = shift;
251
252 6 while (1) {
253 6 my $code = $self->next_start_code( undef, $self->{offset}, 1 );
254 6 ▶ last if $code == 0xB8;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 184 3 3 6
255 3 $self->{offset} = $self->{last_offset};
256
257 3 $code = $self->get_byte( $self->{offset} + 3 );
258 3 my $descr = $START_CODE->{$code};
259
260 3 ▶ if ( defined $descr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $descr) 3 0 3
261 ## printf "EXTENSION: %s\n", $START_CODE->{$code};
262
263 3 ▶ if ( $descr eq 'extension_start_code' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($descr eq 'extension_start_code') { } 0 3 3
elsif ($descr eq 'user_data_start_code') { } 2 1 3
264 0 $self->parse_extension( $self->{offset} );
265 0 next;
266 }
267 elsif ( $descr eq 'user_data_start_code' ) {
268 2 $self->parse_user_data( $self->{offset} );
269 2 last;
270 }
271 else {
272 1 print "No methods to handle $descr\n" if DEBUG;
273 1 last;
274 }
275 }
276
277 0 $self->{offset}++;
278 }
279 }
280  
281 ##------------------------------------------------------------------------
282 ## get_gop()
283 ##
284 ## Find first GOP header after video sequence header
285 ##------------------------------------------------------------------------
286 6 ▶sub get_gop {
Subroutine get_gop: called CC=2 SCAR=6.9
Pod undocumented
287 6 my $self = shift;
288  
289 6 ▶ if ( !$self->next_start_code( 0xb8, $self->{offset} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->next_start_code(184, $self->{"offset"})) 1 5 6
290 ##Ben: should we return 0 here?
291 ##Allen: yes, i suppose so.
292 1 return 0;
293 ##Allen: let's not do this: die "Couldn't find first GOP after Video Sequence start!\n";
294 }
295 5 print "Found GOP Header (0xB8) at $self->{last_offset} $self->{offset}\n" if DEBUG;
296 }
297  
298 ##------------------------------------------------------------------------
299 ## get_header_size()
300 ##
301 ## Video header size
302 ##------------------------------------------------------------------------
303 6 ▶sub get_header_size {
Subroutine get_header_size: called CC=1 SCAR=0.0
Pod undocumented
304 6 my $self = shift;
305  
306 6 print "OFFSETS: $self->{last_offset} $self->{offset}\n" if DEBUG;
307  
308 6 $self->header_size( $self->{last_offset} - $self->{offset} );
309 6 print "HEADER_SIZE: ", $self->header_size, "\n" if DEBUG;
310 }
311  
312 5 1;
313  
314 __END__
« Info.pm ↑ Summary Info/Magic.pm »