Info/Video.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 177 CC 13 SCAR 23.0
CC
13
Cov
0%
get_extensions
30.0
parse
12.0
get_aspect_ratio
12.0
CRAP 10.0
1 ##------------------------------------------------------------------------
2 ## Package: MPEG::Info::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 2002/02/13 08:27:51 synaptic Exp $
8 ##------------------------------------------------------------------------
9  
10 package MPEG::Info::Video;
11 use 5.006;
12 use strict;
13 use warnings;
14  
15 require Exporter;
16  
17 our @ISA = qw( MPEG::Info Exporter );
18  
19 use MPEG::Info;
20 use MPEG::Info::Constants;
21  
22 ##------------------------------------------------------------------------
23 ## Preloaded methods go here.
24 ##------------------------------------------------------------------------
25 1;
26  
27 ##------------------------------------------------------------------------
28 ## new()
29 ##
30 ## override superclass constructor
31 ##------------------------------------------------------------------------
32 sub new {
33 my $proto = shift;
34 my $class = ref( $proto ) || $proto;
35 my $self = {};
36 bless( $self, $class );
37  
38 return $self;
39 }
40  
41 ##------------------------------------------------------------------------
42 ## parse()
43 ##
44 ## Parse a video stream
45 ##------------------------------------------------------------------------
46 sub parse {
47 my $self = shift;
48 my $offset = shift;
49  
50 $offset = 0 if !defined $offset;
51  
52 $self->{offset} = $self->{last_offset} || $offset;
53  
54 # print "\n", '-' x 74, "\n", "Parse Video: $offset\n", '-' x 74, "\n";
55  
56 ## Make sure we have video
57 $self->is_video() or return 0;
58  
59 #if we made it this far, assume a bona fide MPEG
60 $self->type('MPEG');
61  
62 $self->get_size();
63 $self->get_frame_rate();
64 $self->get_aspect_ratio();
65 $self->get_bitrate();
66 $self->get_duration();
67 $self->get_extensions();
68 # $self->get_gop();
69 # $self->get_header_size();
70  
71 if ( 0 ) {
72 print " DIMENSIONS: ", $self->width, 'x', $self->height, "\n";
73 printf " FRAME RATE: %0.2f fps\n", $self->fps;
74 printf " ASPECT RATIO: %s ( %d )\n", $self->aspect, $self->aspect_raw;
75 print " BITRATE: ", $self->vrate, "\n";
76 print " DURATION: ", $self->duration, "\n";
77 print " HEADER SIZE: $self->{video_header_size}\n";
78 }
79  
80 return 1;
81 }
82  
83 ##------------------------------------------------------------------------
84 ## get_size()
85 ##
86 ## Get the width and height
87 ##------------------------------------------------------------------------
88 sub get_size {
89 my $self = shift;
90  
91 $self->{offset} += 4;
92  
93 $self->width( $self->grab( 2, $self->{offset} ) >> 4 );
94 $self->height( $self->grab( 2, $self->{offset} + 1 ) & 0x0FFF );
95 if ( !defined $self->width || !defined $self->height ) {
96 return 0;
97 }
98 return 1;
99 }
100  
101 ##------------------------------------------------------------------------
102 ## is_video()
103 ##
104 ## Verify we're really dealing with a video packet
105 ##
106 ## This method searches up to eof for the start code in case there is
107 ## junk at the beginning of the file. Should we limit this somehow?
108 ##------------------------------------------------------------------------
109 sub is_video {
110 my $self = shift;
111  
112 if ( !$self->next_start_code( SEQ_START_CODE, $self->{offset} ) ) {
113 print "Not an MPEG Video stream\n";
114 return 0;
115 }
116  
117 # print "\tFound start code at ", $self->{last_offset}, "\n";
118 $self->{offset} = $self->{last_offset};
119  
120 return 1;
121 }
122  
123 ##------------------------------------------------------------------------
124 ## get_frame_rate()
125 ##
126 ## Extract the frame_rate index and do the lookup
127 ##------------------------------------------------------------------------
128 sub get_frame_rate {
129 my $self = shift;
130  
131 $self->{offset} += 3;
132  
133 my $frame_rate_index = $self->grab( 1, $self->{offset} ) & 0x0f;
134  
135 if ( $frame_rate_index > 8 ) {
136 print "Invalid frame rate index: $frame_rate_index\n";
137 ## $self->fps( 0.0 );
138 return 0;
139 }
140  
141 $self->fps( $FRAME_RATE->[ $frame_rate_index ] );
142
143 return 1;
144 }
145  
146 ##------------------------------------------------------------------------
147 ## get_aspect_ratio()
148 ##
149 ## Extract the aspect ratio index and do the lookup.
150 ##
151 ## NOTE: Don't die() on invalid aspect ratios as they are fairly common
152 ## For example, 320x240 is invalid. :)
153 ##------------------------------------------------------------------------
154 sub get_aspect_ratio {
155 my $self = shift;
156  
157 my $aspect = ( $self->grab( 1, $self->{offset} ) & 0xF0 ) >> 4;
158 if ( !$aspect ) {
159 # print "Invalid aspect ratio: $aspect\n";
160 return 0;
161 }
162 if ( $aspect > $#{ $ASPECT_RATIO } ) {
163 # print "Reserved aspect ratio: $aspect\n";
164 $self->aspect( 'Reserved' );
165 }
166 else {
167 # print "Aspect Ratio: ", $ASPECT_RATIO->[ $aspect ], "\n";
168 $self->aspect( $ASPECT_RATIO->[ $aspect ] );
169 }
170  
171 $self->aspect_raw( $aspect );
172  
173 return 1;
174 }
175  
176 ##------------------------------------------------------------------------
177 ## get_bitrate()
178 ##
179 ## From the MPEG-2.2 spec:
180 ##
181 ## bit_rate -- This is a 30-bit integer. The lower 18 bits of the
182 ## integer are in bit_rate_value and the upper 12 bits are in
183 ## bit_rate_extension. The 30-bit integer specifies the bitrate of the
184 ## bitstream measured in units of 400 bits/second, rounded upwards.
185 ## The value zero is forbidden.
186 ##
187 ## So ignoring all the variable bitrate stuff for now, this 30 bit integer
188 ## multiplied times 400 bits/sec should give the rate in bits/sec.
189 ##
190 ## TODO: Variable bitrates? I need one that implements this.
191 ##
192 ## Continued from the MPEG-2.2 spec:
193 ##
194 ## If the bitstream is a constant bitrate stream, the bitrate specified
195 ## is the actual rate of operation of the VBV specified in annex C. If
196 ## the bitstream is a variable bitrate stream, the STD specifications in
197 ## ISO/IEC 13818-1 supersede the VBV, and the bitrate specified here is
198 ## used to dimension the transport stream STD (2.4.2 in ITU-T Rec. xxx |
199 ## ISO/IEC 13818-1), or the program stream STD (2.4.5 in ITU-T Rec. xxx |
200 ## ISO/IEC 13818-1).
201 ##
202 ## If the bitstream is not a constant rate bitstream the vbv_delay
203 ## field shall have the value FFFF in hexadecimal.
204 ##
205 ## Given the value encoded in the bitrate field, the bitstream shall be
206 ## generated so that the video encoding and the worst case multiplex
207 ## jitter do not cause STD buffer overflow or underflow.
208 ##
209 ##
210 ##------------------------------------------------------------------------
211 sub get_bitrate {
212 my $self = shift;
213  
214 $self->{offset}++;
215  
216 ## grab a short
217 my $bitrate = $self->grab( 2, $self->{offset} ) << 2;
218 my $lasttwo = $self->get_byte( $self->{offset} + 2 ) >> 6;
219  
220 $self->vrate( ( $bitrate | $lasttwo ) * 400);
221 }
222  
223 ##------------------------------------------------------------------------
224 ## get_duration()
225 ##
226 ##
227 ##------------------------------------------------------------------------
228 sub get_duration {
229 my $self = shift;
230  
231 $self->duration ( ( $self->size * 8 ) / ( $self->vrate * 400 ) );
232 }
233  
234 ##------------------------------------------------------------------------
235 ## get_extensions()
236 ##
237 ## TODO: make the $START_CODE->{$code} description the actual method name
238 ## for the extension handler.
239 ##------------------------------------------------------------------------
240 sub get_extensions {
241 my $self = shift;
242  
243 while (1) {
244 my $code = $self->next_start_code( undef, $self->{offset}, 1 );
245 last if $code == 0xB8;
246 $self->{offset} = $self->{last_offset};
247  
248 $code = $self->get_byte( $self->{offset} + 3 );
249 my $descr = $START_CODE->{$code};
250  
251 if ( defined $descr ) {
252 ## printf "EXTENSION: %s\n", $START_CODE->{$code};
253  
254 if ( $descr eq 'extension_start_code' ) {
255 $self->parse_extension( $self->{offset} );
256 next;
257 }
258 elsif ( $descr eq 'user_data_start_code' ) {
259 $self->parse_user_data( $self->{offset} );
260 last;
261 }
262 else {
263 print "No methods to handle $descr\n";
264 last;
265 }
266 }
267  
268 $self->{offset}++;
269 }
270 }
271  
272 ##------------------------------------------------------------------------
273 ## get_gop()
274 ##
275 ## Find first GOP header after video sequence header
276 ##------------------------------------------------------------------------
277 sub get_gop {
278 my $self = shift;
279  
280 if ( !$self->next_start_code( 0xb8, $self->{offset} ) ) {
281 ## should we return 0 here?
282 die "Couldn't find first GOP after Video Sequence start!\n";
283 }
284 else {
285 print "Found GOP Header (0xB8) at $self->{last_offset} $self->{offset}\n";
286 }
287  
288 }
289  
290 ##------------------------------------------------------------------------
291 ## get_header_size()
292 ##
293 ## Video header size
294 ##------------------------------------------------------------------------
295 sub get_header_size {
296 my $self = shift;
297  
298 $self->{header_size} = $self->{last_offset} - $self->{offset};
299 }