Info/MPEG/Audio.pm

filter: Statementstmt 56.7% 76 / 134 Branchbran 28.7% 23 / 80 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 72.2% 13 / 18 Podpod 0.0% 0 / 13 totaltotal 43.8% 114 / 260 CC 44 SCAR 36.4
CC
44
Cov
45%
get_audio_mode
118.8
get_emphasis
30.0
get_frame_length
20.0
CRAP 38.1
↑ Summary Info/ASF.pm »
1 ##------------------------------------------------------------------------
2 ## Package: Video::Info::MPEG::Audio
3 ## Author: Benjamin R. Ginter
4 ## Notice: Copyright (c) 2001 Benjamin R. Ginter
5 ## Purpose: Parse audio streams
6 ## Comments: None
7 ## CVS: $Id: Audio.pm,v 1.3 2002/11/12 07:19:34 allenday Exp $
8 ##------------------------------------------------------------------------
9  
10 package Video::Info::MPEG::Audio;
11 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Video::Info::MPEG qw( $AUDIO_BITRATE );
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 5 our $AUDIO_BITRATE;
19 5 our $AUDIO_SAMPLING_RATE;
20  
21 ##------------------------------------------------------------------------
22 ## Preloaded methods go here.
23 ##------------------------------------------------------------------------
24 5 1;
25  
26 12 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
27 12 my $self = shift;
28 12 my %param = @_;
29 12 $self->handle($self->filename($param{-file}));
30 12 $self->init_attributes;
31 12 $self->version(0);
32 }
33  
34 ##------------------------------------------------------------------------
35 ## parse()
36 ##
37 ## Parse an audio packet. Since this is in the context of a video stream,
38 ## we only care about the MPEG version, layer, bitrate, sampling rate,
39 ## channels, and emphasis.
40 ##
41 ##------------------------------------------------------------------------
42 23 ▶sub parse {
Subroutine parse: called CC=7 SCAR=20.1
Pod undocumented
43 23 my($self,$offset) = @_;
44  
45 23 ▶ $offset = 0 if !defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 8 15 23
46 23 $self->{offset} = $offset;
47  
48 23 $self->{_bytes} = $self->get_header();
49 # printf "0x%08x\n", unpack( "N", pack( "C*", @{$self->{_bytes}} ) );
50  
51 23 print "Video::Info::MPEG::Audio::parse( $offset )\n" if DEBUG;
52  
53 #print "parse audio: $offset\n";
54 23 ▶ $self->is_audio() or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_audio or return 0 5 18 23
55  
56 5 ▶ $self->get_version && $self->get_layer or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_version and $self->get_layer or return 0 5 0 5
Condition33%1/3
$self->get_version and $self->get_layer
!ll&&!rl&&r
005
MC/DC0%0/2
$self->get_version and $self->get_layer
$self->get_version $self->get_layer
Truth table33%1/3
A and B
A= $self->get_versionB= $self->get_layer
ABresult
0X0
100
111
57 5 ▶ $self->get_bitrate && $self->get_sampling_freq or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_bitrate and $self->get_sampling_freq or return 0 5 0 5
Condition33%1/3
$self->get_bitrate and $self->get_sampling_freq
!ll&&!rl&&r
005
MC/DC0%0/2
$self->get_bitrate and $self->get_sampling_freq
$self->get_bitrate $self->get_sampling_freq
Truth table33%1/3
A and B
A= $self->get_bitrateB= $self->get_sampling_freq
ABresult
0X0
100
111
58 # $self->get_protect;
59 5 $self->get_audio_mode();
60 # $self->get_copyright();
61 # $self->get_padding();
62 # $self->get_emphasis();
63 #$self->get_frame_length();
64  
65 #if we made it this far, assume a bona fide MPEG
66 5 $self->type('MPEG');
67  
68 5 if ( DEBUG ) {
69 print '-' x 74, "\n", 'Parse Audio', "\n", '-' x 74, "\n";
70  
71 print "MPEG-$self->{version} Layer $self->{layer}\n";
72 print " MODE: $self->{mode}\n";
73 print " BITRATE: $self->{bitrate}\n";
74 print " BYTERATE: $self->{byterate}\n";
75 print "SAMPLING RATE: $self->{sampling}\n";
76 print " PADDING: $self->{padding}\n";
77 print " EMPHASIS: $self->{emphasis}\n";
78 print " COPYRIGHT: $self->{copyright}\n";
79 print " PROTECT: $self->{protect}\n";
80 # print " FRAME_LENGTH: $self->{frame_length}\n";
81 print "Audio : Mpeg $self->{version} layer $self->{layer}\n";
82 print "$self->{bitrate} kbps $self->{sampling} Hz\n";
83 print "$self->{mode}, $self->{emphasis}\n";
84 }
85
86 ## Save off some information to a format Video::Info expects.
87 ## The $self-> hash remains available for the user if needed.
88  
89 5 $self->arate ( $self->{byterate} * 8 );
90 5 $self->copyright( $self->{copyright} );
91  
92 5 return 1;
93 }
94  
95 ##------------------------------------------------------------------------
96 ## is_audio()
97 ##
98 ## Verify we have the proper MPEG audio packet start codes
99 ##------------------------------------------------------------------------
100 23 ▶sub is_audio {
Subroutine is_audio: called CC=4 SCAR=16.6
Pod undocumented
101 23 my $self = shift;
102 23 my $bytes = $self->{_bytes};
103  
104 ## ensure that the first two bytes are FFFx
105 23 ▶ return 0 if $bytes->[0] != 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes->[0] != 255 18 5 23
106  
107 5 ▶ if ( ( $bytes->[1] & 0xF0 ) != 0xF0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($bytes->[1] & 240) != 240) 0 5 5
108 ## Doesn't start with 12 bits set
109
110 0 ▶ if ( ( $bytes->[1] & 0xE0 ) != 0xE0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($bytes->[1] & 224) != 224) 0 0 0
111 ## Doesn't start with 11 bits set either -- give up
112 0 return 0;
113 }
114 # else {
115 ## starts with 11 bits set
116 0 $self->{version} = 2.5;
117 # }
118 }
119 5 return 1;
120  
121 }
122  
123 ##------------------------------------------------------------------------
124 ## get_version()
125 ##
126 ## Determine the MPEG Version
127 ##------------------------------------------------------------------------
128 5 ▶sub get_version {
Subroutine get_version: called CC=4 SCAR=19.1
Pod undocumented
129 5 my $self = shift;
130  
131 ## find mpeg version 1.0 or 2.0
132 5 ▶ if ( $self->{_bytes}->[1] & 0x08 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_bytes'}[1] & 8) { } 5 0 5
133 5 ▶ if ( $self->{version} != 2.5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'version'} != 2.5) { } 5 0 5
134 5 $self->{version} = 1;
135 5 $self->acodecraw(0x50);
136 }
137 else {
138 ## invalid 01 encountered
139 0 return 0;
140 }
141 } else {
142 0 ▶ if ( $self->{version} != 2.5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'version'} != 2.5) { } 0 0 0
143 0 $self->{version} = 2;
144 0 $self->acodecraw(0x50);
145 } else {
146 ## err, isn't this set?
147 0 $self->{version} = 3;
148 0 $self->acodecraw(0x55);
149 }
150 }
151 5 return 1;
152 }
153  
154 ##------------------------------------------------------------------------
155 ## get_layer()
156 ##
157 ## Determine the MPEG layer
158 ##------------------------------------------------------------------------
159 5 ▶sub get_layer {
Subroutine get_layer: called CC=5 SCAR=20.9
Pod undocumented
160 5 my $self = shift;
161  
162 ## Find layer
163 5 my $layer = ( $self->{_bytes}->[1] & 0x06 ) >> 1;
164 5 ▶ if ( $layer == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($layer == 0) { } 0 5 5
elsif ($layer == 1) { } 1 4 5
elsif ($layer == 2) { } 4 0 4
elsif ($layer == 3) { } 0 0 0
165 0 $self->{layer} = -1;
166 0 return 0;
167 }
168 elsif ( $layer == 1 ) {
169 1 $self->{layer} = 3;
170 }
171 elsif ( $layer == 2 ) {
172 4 $self->{layer} = 2;
173 }
174 elsif ( $layer == 3 ) {
175 0 $self->{layer} = 1;
176 }
177 else {
178 0 $self->{layer} = $layer;
179 0 print "Unknown audio layer index: $layer\n";
180 0 return 0;
181 }
182 # undef $layer;
183  
184 5 return 1;
185 }
186  
187 ##------------------------------------------------------------------------
188 ## get_audio_mode()
189 ##
190 ## Determine the audio mode (channels, etc.)
191 ##------------------------------------------------------------------------
192 5 ▶sub get_audio_mode {
Subroutine get_audio_mode: called CC=16 SCAR=47.8
Pod undocumented
193 5 my $self = shift;
194  
195 ## Get the raw audio mode
196 5 $self->{mode_raw} = $self->{_bytes}->[3] >> 6;
197 5 ▶ $self->{mode_raw} == 1 ? $self->{modext} = ( $self->{_bytes}->[3] >> 4 ) & 0x03 : $self->{modext} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mode_raw'} == 1 ? : 0 5 5
198  
199 5 $self->achans( 2 );
200  
201 ## Now decode it
202 5 ▶ if ( $self->{mode_raw} == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'mode_raw'} == 0) { } 3 2 5
elsif ($self->{'mode_raw'} == 1) { } 0 2 2
elsif ($self->{'mode_raw'} == 2) { } 0 2 2
elsif ($self->{'mode_raw'} == 3) { } 2 0 2
203 3 $self->{mode} = 'Stereo';
204 3 $self->achans(2);
205 }
206 elsif ( $self->{mode_raw} == 1 ) {
207 0 ▶ if ( $self->{layer} == 1 || $self->{layer} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'layer'} == 1 or $self->{'layer'} == 2) { } 0 0 0
Condition0%0/3
$self->{"layer"} == 1 or $self->{"layer"} == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"layer"} == 1 or $self->{"layer"} == 2
$self->{"layer"} == 1 $self->{"layer"} == 2
Truth table0%0/3
A or B
A= $self->{"layer"} == 1B= $self->{"layer"} == 2
ABresult
1X1
011
000
208 0 ▶ if ( $self->{modext} == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'modext'} == 0) { } 0 0 0
elsif ($self->{'modext'} == 1) { } 0 0 0
elsif ($self->{'modext'} == 2) { } 0 0 0
elsif ($self->{'modext'} == 3) { } 0 0 0
209 0 $self->{mode} = 'Intensity stereo on bands 4-31/32';
210 }
211 elsif ( $self->{modext} == 1 ) {
212 0 $self->{mode} = 'Intensity stereo on bands 8-31/32';
213 }
214 elsif ( $self->{modext} == 2 ) {
215 0 $self->{mode} = 'Intensity stereo on bands 12-31/32';
216 }
217 elsif ( $self->{modext} == 3 ) {
218 0 $self->{mode} = 'Intensity stereo on bands 16-31/32';
219 }
220 else {
221 0 $self->{mode} = "Unknown audio mode extension. Mode=$self->{mode_raw} Ext: $self->{modext}";
222 0 return 0;
223 }
224 }
225 else {
226 ## mp3
227 0 ▶ if ( $self->{modext} == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'modext'} == 0) { } 0 0 0
elsif ($self->{'modext'} == 1) { } 0 0 0
elsif ($self->{'modext'} == 2) { } 0 0 0
elsif ($self->{'modext'} == 3) { } 0 0 0
228 0 $self->{mode} = 'Intensity stereo off, M/S stereo off';
229 }
230 elsif ( $self->{modext} == 1 ) {
231 0 $self->{mode} = 'Intensity stereo on, M/S stereo off';
232 }
233 elsif ( $self->{modext} == 2 ) {
234 0 $self->{mode} = 'Intensity stereo off, M/S stereo on';
235 }
236 elsif ( $self->{modext} == 3 ) {
237 0 $self->{mode} = 'Intensity stereo on, M/S stereo on';
238 }
239 else {
240 0 $self->{mode} = "Unknown audio mode extension. Mode=$self->{mode_raw} Ext: $self->{modext}";
241 0 return 0;
242 }
243
244 }
245 }
246 elsif ( $self->{mode_raw} == 2 ) {
247 0 $self->{mode} = 'Dual Channel';
248 0 $self->achans(2); #not stereo, but still 2, right? brg: yes
249 }
250 elsif ( $self->{mode_raw} == 3 ) {
251 2 $self->{mode} = 'Mono';
252 2 $self->achans(1);
253 }
254 else {
255 0 $self->{mode} = "Unknown audio mode. Mode=$self->{mode_raw} Ext: $self->{modext}";
256 0 $self->achans(0);
257 0 return 0;
258 }
259  
260 5 return 1;
261 }
262  
263 ##------------------------------------------------------------------------
264 ## get_copyright()
265 ##------------------------------------------------------------------------
266 0 ▶sub get_copyright {
Subroutine get_copyright: not called CC=2 SCAR=17.9
Pod undocumented
267 0 my $self = shift;
268  
269 ## Set original/copyright bit
270 0 ▶ $self->{_bytes}->[3] & 0x04 ? $self->{copyright} = 1 : $self->{copyright} = 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_bytes'}[3] & 4 ? : 0 0 0
271 }
272  
273 ##------------------------------------------------------------------------
274 ## get_protect()
275 ##
276 ## Extract the protection bit
277 ##------------------------------------------------------------------------
278 0 ▶sub get_protect {
Subroutine get_protect: not called CC=2 SCAR=17.9
Pod undocumented
279 0 my $self = shift;
280  
281 ## Get protection bit
282 0 ▶ $self->{_bytes}->[1] & 0x01 ? $self->{protect} = 0 : $self->{protect} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_bytes'}[1] & 1 ? : 0 0 0
283  
284 }
285  
286 ##------------------------------------------------------------------------
287 ## get_bitrate()
288 ##------------------------------------------------------------------------
289 5 ▶sub get_bitrate {
Subroutine get_bitrate: called CC=2 SCAR=7.0
Pod undocumented
290 5 my $self = shift;
291  
292 ## Bitrate index and sampling index to pass through the array
293 5 my $bitrate_index = $self->{_bytes}->[2] >> 4;
294 5 ▶ return 0 if $bitrate_index == 15;
Branch: 1 branch
BranchTrueFalseTotal
if $bitrate_index == 15 0 5 5
295  
296 5 $self->{bitrate} = $AUDIO_BITRATE->{ $self->{version} }->{ $self->{layer} }->[ $bitrate_index ];
297 5 $self->{byterate} = ( $self->{bitrate} * 1000 ) / 8.0;
298  
299 5 return 1;
300 }
301  
302 ##------------------------------------------------------------------------
303 ## get_sampling_freq()
304 ##------------------------------------------------------------------------
305 5 ▶sub get_sampling_freq {
Subroutine get_sampling_freq: called CC=2 SCAR=7.0
Pod undocumented
306 5 my $self = shift;
307  
308 5 my $sampling_index = ( $self->{_bytes}->[2] & 0x0F ) >> 2;
309 # print "sampling_index: $sampling_index\n";
310  
311 5 ▶ return 0 if $sampling_index == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $sampling_index == 3 0 5 5
312  
313 5 $self->{sampling} = $AUDIO_SAMPLING_RATE->{ $self->{version} }->[ $sampling_index ];
314 5 return 1;
315 }
316  
317 ##------------------------------------------------------------------------
318 ## get_padding()
319 ##------------------------------------------------------------------------
320 0 ▶sub get_padding {
Subroutine get_padding: not called CC=2 SCAR=17.9
Pod undocumented
321 0 my $self = shift;
322  
323 ## Get padding bit
324 0 ▶ $self->{_bytes}->[2] & 0x02 ? $self->{padding} = 1 : $self->{padding} = 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_bytes'}[2] & 2 ? : 0 0 0
325 }
326  
327 ##------------------------------------------------------------------------
328 ## get_emphasis()
329 ##------------------------------------------------------------------------
330 0 ▶sub get_emphasis {
Subroutine get_emphasis: not called CC=5 SCAR=34.0
Pod undocumented
331 0 my $self = shift;
332  
333 ## Get emphasis
334 0 my $emphasis_index = $self->{_bytes}->[3] & 0x03;
335  
336 0 ▶ if ( $emphasis_index == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($emphasis_index == 0) { } 0 0 0
elsif ($emphasis_index == 1) { } 0 0 0
elsif ($emphasis_index == 2) { } 0 0 0
elsif ($emphasis_index == 3) { } 0 0 0
337 0 $self->{emphasis} = 'No Emphasis';
338 }
339 elsif ( $emphasis_index == 1 ) {
340 0 $self->{emphasis} = '50/15us';
341 }
342 elsif ( $emphasis_index == 2 ) {
343 0 $self->{emphasis} = 'Unknown';
344 }
345 elsif ( $emphasis_index == 3 ) {
346 0 $self->{emphasis} = 'CCITT J 17';
347 }
348 else {
349 0 $self->{emphasis} = 'Undefined';
350 }
351  
352 }
353  
354 ##------------------------------------------------------------------------
355 ## get_frame_length()
356 ##------------------------------------------------------------------------
357 0 ▶sub get_frame_length {
Subroutine get_frame_length: not called CC=4 SCAR=30.0
Pod undocumented
358 0 my $self = shift;
359  
360 ## Get frame-length
361 0 ▶ if ( $self->{version} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'version'} == 1) { } 0 0 0
362 0 ▶ if ( $self->{layer} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'layer'} == 1) { } 0 0 0
363 0 $self->{frame_length} = int( ( 48000 * $self->{bitrate} ) / $self->{sampling} ) + 4 * $self->{padding};
364 }
365 else {
366 0 $self->{frame_length} = int( ( 72000 * $self->{bitrate} ) / $self->{sampling} ) + $self->{padding};
367 }
368 }
369 else {
370 0 print "Audio layer invalid : should be 1 or 2\n";
371 0 return 0;
372 }
373  
374 0 ▶ if ( $self->{protect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"protect"}) 0 0 0
375 0 $self->{frame_length} += 2;
376 }
377 }
378  
379 5 1;
380  
381 __END__
↑ Summary Info/ASF.pm »