Skip to content

Commit 1a44cb8

Browse files
bvibberStudioMaX
authored andcommitted
Fixes for MPEG-2 aspect ratio detection
* fix array lookup * calculate pixel aspect ratio from display aspect ratios * delay calc of aspect ratio until after extended size is known * mirror the updated pixel aspect ratio up to the 'video' section Fixes remaining issues with #148
1 parent 789f3a7 commit 1a44cb8

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/Module/AudioVideo/Mpeg.php

+19-10
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ public function Analyze() {
152152
case 0xB5: // extension_start_code
153153
$info['video']['codec'] = 'MPEG-2';
154154

155-
if (isset($info['mpeg']['video']['raw']['aspect_ratio_information'])) {
156-
// MPEG-2 defines the aspect ratio flag differently from MPEG-1, but the MPEG-2 extension start code may occur after we've already looked up the aspect ratio assuming it was MPEG-1, so re-lookup assuming MPEG-2
157-
$info['mpeg']['video']['pixel_aspect_ratio'] = self::videoAspectRatioLookup($info['mpeg']['video']['raw']['aspect_ratio_information'], 2);
158-
$info['mpeg']['video']['pixel_aspect_ratio_text'] = self::videoAspectRatioTextLookup($info['mpeg']['video']['raw']['aspect_ratio_information'], 2);
159-
}
160-
161155
$bitstream = getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $StartCodeOffset + 4, 8)); // 48 bits for Sequence Extension ID; 61 bits for Sequence Display Extension ID; 59 bits for Sequence Scalable Extension ID
162156
$bitstreamoffset = 0;
163157

@@ -182,6 +176,16 @@ public function Analyze() {
182176
$info['video']['interlaced'] = !$info['mpeg']['video']['raw']['progressive_sequence'];
183177
$info['mpeg']['video']['interlaced'] = !$info['mpeg']['video']['raw']['progressive_sequence'];
184178
$info['mpeg']['video']['chroma_format'] = self::chromaFormatTextLookup($info['mpeg']['video']['raw']['chroma_format']);
179+
180+
if (isset($info['mpeg']['video']['raw']['aspect_ratio_information'])) {
181+
// MPEG-2 defines the aspect ratio flag differently from MPEG-1, but the MPEG-2 extension start code may occur after we've already looked up the aspect ratio assuming it was MPEG-1, so re-lookup assuming MPEG-2
182+
// This must be done after the extended size is known, so the display aspect ratios can be converted to pixel aspect ratios.
183+
$info['mpeg']['video']['pixel_aspect_ratio'] = self::videoAspectRatioLookup($info['mpeg']['video']['raw']['aspect_ratio_information'], 2, $info['video']['resolution_x'], $info['video']['resolution_y']);
184+
$info['mpeg']['video']['pixel_aspect_ratio_text'] = self::videoAspectRatioTextLookup($info['mpeg']['video']['raw']['aspect_ratio_information'], 2);
185+
$info['video']['pixel_aspect_ratio'] = $info['mpeg']['video']['pixel_aspect_ratio'];
186+
$info['video']['pixel_aspect_ratio_text'] = $info['mpeg']['video']['pixel_aspect_ratio_text'];
187+
}
188+
185189
break;
186190

187191
case 2: // 0010 Sequence Display Extension ID
@@ -604,12 +608,17 @@ public static function videoFramerateLookup($rawframerate) {
604608
*
605609
* @return float
606610
*/
607-
public static function videoAspectRatioLookup($rawaspectratio, $mpeg_version=1) {
611+
public static function videoAspectRatioLookup($rawaspectratio, $mpeg_version=1, $width=0, $height=0) {
608612
$lookup = array(
609613
1 => array(0, 1, 0.6735, 0.7031, 0.7615, 0.8055, 0.8437, 0.8935, 0.9157, 0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2015, 0),
610614
2 => array(0, 1, 1.3333, 1.7778, 2.2100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
611615
);
612-
return (float) (isset($lookup[$rawaspectratio][$mpeg_version]) ? $lookup[$rawaspectratio][$mpeg_version] : 0);
616+
$ratio = (float) (isset($lookup[$mpeg_version][$rawaspectratio]) ? $lookup[$mpeg_version][$rawaspectratio] : 0);
617+
if ($mpeg_version == 2 && $ratio != 1) {
618+
// Calculate pixel aspect ratio from MPEG-2 display aspect ratio
619+
$ratio = $ratio * $height / $width;
620+
}
621+
return $ratio;
613622
}
614623

615624
/**
@@ -620,9 +629,9 @@ public static function videoAspectRatioLookup($rawaspectratio, $mpeg_version=1)
620629
public static function videoAspectRatioTextLookup($rawaspectratio, $mpeg_version=1) {
621630
$lookup = array(
622631
1 => array('forbidden', 'square pixels', '0.6735', '16:9, 625 line, PAL', '0.7615', '0.8055', '16:9, 525 line, NTSC', '0.8935', '4:3, 625 line, PAL, CCIR601', '0.9815', '1.0255', '1.0695', '4:3, 525 line, NTSC, CCIR601', '1.1575', '1.2015', 'reserved'),
623-
2 => array('forbidden', 'square pixels', '4:3', '16:9', '1.21:1', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved'), // http://dvd.sourceforge.net/dvdinfo/mpeghdrs.html
632+
2 => array('forbidden', 'square pixels', '4:3', '16:9', '2.21:1', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved', 'reserved'), // http://dvd.sourceforge.net/dvdinfo/mpeghdrs.html
624633
);
625-
return (isset($lookup[$rawaspectratio][$mpeg_version]) ? $lookup[$rawaspectratio][$mpeg_version] : '');
634+
return (isset($lookup[$mpeg_version][$rawaspectratio]) ? $lookup[$mpeg_version][$rawaspectratio] : '');
626635
}
627636

628637
/**

0 commit comments

Comments
 (0)