File indexing completed on 2024-12-22 05:33:11
0001 <?php 0002 ///////////////////////////////////////////////////////////////// 0003 /// getID3() by James Heinrich <info@getid3.org> // 0004 // available at http://getid3.sourceforge.net // 0005 // or http://www.getid3.org // 0006 // also https://github.com/JamesHeinrich/getID3 // 0007 ///////////////////////////////////////////////////////////////// 0008 // See readme.txt for more details // 0009 ///////////////////////////////////////////////////////////////// 0010 // // 0011 // module.audio-video.mpeg.php // 0012 // module for analyzing MPEG files // 0013 // dependencies: module.audio.mp3.php // 0014 // /// 0015 ///////////////////////////////////////////////////////////////// 0016 0017 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.mp3.php', __FILE__, true); 0018 0019 class getid3_mpeg extends getid3_handler { 0020 0021 const START_CODE_BASE = "\x00\x00\x01"; 0022 const VIDEO_PICTURE_START = "\x00\x00\x01\x00"; 0023 const VIDEO_USER_DATA_START = "\x00\x00\x01\xB2"; 0024 const VIDEO_SEQUENCE_HEADER = "\x00\x00\x01\xB3"; 0025 const VIDEO_SEQUENCE_ERROR = "\x00\x00\x01\xB4"; 0026 const VIDEO_EXTENSION_START = "\x00\x00\x01\xB5"; 0027 const VIDEO_SEQUENCE_END = "\x00\x00\x01\xB7"; 0028 const VIDEO_GROUP_START = "\x00\x00\x01\xB8"; 0029 const AUDIO_START = "\x00\x00\x01\xC0"; 0030 0031 0032 public function Analyze() { 0033 $info = &$this->getid3->info; 0034 0035 $info['fileformat'] = 'mpeg'; 0036 $this->fseek($info['avdataoffset']); 0037 0038 $MPEGstreamData = $this->fread($this->getid3->option_fread_buffer_size); 0039 $MPEGstreamBaseOffset = 0; // how far are we from the beginning of the file data ($info['avdataoffset']) 0040 $MPEGstreamDataOffset = 0; // how far are we from the beginning of the buffer data (~32kB) 0041 0042 $StartCodeValue = false; 0043 $prevStartCodeValue = false; 0044 0045 $GOPcounter = -1; 0046 $FramesByGOP = array(); 0047 $ParsedAVchannels = array(); 0048 0049 do { 0050 //echo $MPEGstreamDataOffset.' vs '.(strlen($MPEGstreamData) - 1024).'<Br>'; 0051 if ($MPEGstreamDataOffset > (strlen($MPEGstreamData) - 16384)) { 0052 // buffer running low, get more data 0053 //echo 'reading more data<br>'; 0054 $MPEGstreamData .= $this->fread($this->getid3->option_fread_buffer_size); 0055 if (strlen($MPEGstreamData) > $this->getid3->option_fread_buffer_size) { 0056 $MPEGstreamData = substr($MPEGstreamData, $MPEGstreamDataOffset); 0057 $MPEGstreamBaseOffset += $MPEGstreamDataOffset; 0058 $MPEGstreamDataOffset = 0; 0059 } 0060 } 0061 if (($StartCodeOffset = strpos($MPEGstreamData, self::START_CODE_BASE, $MPEGstreamDataOffset)) === false) { 0062 //echo 'no more start codes found.<br>'; 0063 break; 0064 } else { 0065 $MPEGstreamDataOffset = $StartCodeOffset; 0066 $prevStartCodeValue = $StartCodeValue; 0067 $StartCodeValue = ord(substr($MPEGstreamData, $StartCodeOffset + 3, 1)); 0068 //echo 'Found "'.strtoupper(dechex($StartCodeValue)).'" at offset '.($MPEGstreamBaseOffset + $StartCodeOffset).' ($MPEGstreamDataOffset = '.$MPEGstreamDataOffset.')<br>'; 0069 } 0070 $MPEGstreamDataOffset += 4; 0071 switch ($StartCodeValue) { 0072 0073 case 0x00: // picture_start_code 0074 if (!empty($info['mpeg']['video']['bitrate_mode']) && ($info['mpeg']['video']['bitrate_mode'] == 'vbr')) { 0075 $bitstream = getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $StartCodeOffset + 4, 4)); 0076 $bitstreamoffset = 0; 0077 0078 $PictureHeader = array(); 0079 0080 $PictureHeader['temporal_reference'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 10); // 10-bit unsigned integer associated with each input picture. It is incremented by one, modulo 1024, for each input frame. When a frame is coded as two fields the temporal reference in the picture header of both fields is the same. Following a group start header the temporal reference of the earliest picture (in display order) shall be reset to zero. 0081 $PictureHeader['picture_coding_type'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 3); // 3 bits for picture_coding_type 0082 $PictureHeader['vbv_delay'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 16); // 16 bits for vbv_delay 0083 //... etc 0084 0085 $FramesByGOP[$GOPcounter][] = $PictureHeader; 0086 } 0087 break; 0088 0089 case 0xB3: // sequence_header_code 0090 /* 0091 Note: purposely doing the less-pretty (and probably a bit slower) method of using string of bits rather than bitwise operations. 0092 Mostly because PHP 32-bit doesn't handle unsigned integers well for bitwise operation. 0093 Also the MPEG stream is designed as a bitstream and often doesn't align nicely with byte boundaries. 0094 */ 0095 $info['video']['codec'] = 'MPEG-1'; // will be updated if extension_start_code found 0096 0097 $bitstream = getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $StartCodeOffset + 4, 8)); 0098 $bitstreamoffset = 0; 0099 0100 $info['mpeg']['video']['raw']['horizontal_size_value'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 12); // 12 bits for horizontal frame size. Note: horizontal_size_extension, if present, will add 2 most-significant bits to this value 0101 $info['mpeg']['video']['raw']['vertical_size_value'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 12); // 12 bits for vertical frame size. Note: vertical_size_extension, if present, will add 2 most-significant bits to this value 0102 $info['mpeg']['video']['raw']['aspect_ratio_information'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 4); // 4 bits for aspect_ratio_information 0103 $info['mpeg']['video']['raw']['frame_rate_code'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 4); // 4 bits for Frame Rate id code 0104 $info['mpeg']['video']['raw']['bitrate'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 18); // 18 bits for bit_rate_value (18 set bits = VBR, otherwise bitrate = this value * 400) 0105 $marker_bit = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // The term "marker_bit" indicates a one bit field in which the value zero is forbidden. These marker bits are introduced at several points in the syntax to avoid start code emulation. 0106 $info['mpeg']['video']['raw']['vbv_buffer_size'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 10); // 10 bits vbv_buffer_size_value 0107 $info['mpeg']['video']['raw']['constrained_param_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: constrained_param_flag 0108 $info['mpeg']['video']['raw']['load_intra_quantiser_matrix'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: load_intra_quantiser_matrix 0109 0110 if ($info['mpeg']['video']['raw']['load_intra_quantiser_matrix']) { 0111 $bitstream .= getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $StartCodeOffset + 12, 64)); 0112 for ($i = 0; $i < 64; $i++) { 0113 $info['mpeg']['video']['raw']['intra_quantiser_matrix'][$i] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); 0114 } 0115 } 0116 $info['mpeg']['video']['raw']['load_non_intra_quantiser_matrix'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); 0117 0118 if ($info['mpeg']['video']['raw']['load_non_intra_quantiser_matrix']) { 0119 $bitstream .= getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $StartCodeOffset + 12 + ($info['mpeg']['video']['raw']['load_intra_quantiser_matrix'] ? 64 : 0), 64)); 0120 for ($i = 0; $i < 64; $i++) { 0121 $info['mpeg']['video']['raw']['non_intra_quantiser_matrix'][$i] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); 0122 } 0123 } 0124 0125 $info['mpeg']['video']['pixel_aspect_ratio'] = self::videoAspectRatioLookup($info['mpeg']['video']['raw']['aspect_ratio_information']); 0126 $info['mpeg']['video']['pixel_aspect_ratio_text'] = self::videoAspectRatioTextLookup($info['mpeg']['video']['raw']['aspect_ratio_information']); 0127 $info['mpeg']['video']['frame_rate'] = self::videoFramerateLookup($info['mpeg']['video']['raw']['frame_rate_code']); 0128 if ($info['mpeg']['video']['raw']['bitrate'] == 0x3FFFF) { // 18 set bits = VBR 0129 //$this->warning('This version of getID3() ['.$this->getid3->version().'] cannot determine average bitrate of VBR MPEG video files'); 0130 $info['mpeg']['video']['bitrate_mode'] = 'vbr'; 0131 } else { 0132 $info['mpeg']['video']['bitrate'] = $info['mpeg']['video']['raw']['bitrate'] * 400; 0133 $info['mpeg']['video']['bitrate_mode'] = 'cbr'; 0134 $info['video']['bitrate'] = $info['mpeg']['video']['bitrate']; 0135 } 0136 $info['video']['resolution_x'] = $info['mpeg']['video']['raw']['horizontal_size_value']; 0137 $info['video']['resolution_y'] = $info['mpeg']['video']['raw']['vertical_size_value']; 0138 $info['video']['frame_rate'] = $info['mpeg']['video']['frame_rate']; 0139 $info['video']['bitrate_mode'] = $info['mpeg']['video']['bitrate_mode']; 0140 $info['video']['pixel_aspect_ratio'] = $info['mpeg']['video']['pixel_aspect_ratio']; 0141 $info['video']['lossless'] = false; 0142 $info['video']['bits_per_sample'] = 24; 0143 break; 0144 0145 case 0xB5: // extension_start_code 0146 $info['video']['codec'] = 'MPEG-2'; 0147 0148 $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 0149 $bitstreamoffset = 0; 0150 0151 $info['mpeg']['video']['raw']['extension_start_code_identifier'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 4); // 4 bits for extension_start_code_identifier 0152 //echo $info['mpeg']['video']['raw']['extension_start_code_identifier'].'<br>'; 0153 switch ($info['mpeg']['video']['raw']['extension_start_code_identifier']) { 0154 case 1: // 0001 Sequence Extension ID 0155 $info['mpeg']['video']['raw']['profile_and_level_indication'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); // 8 bits for profile_and_level_indication 0156 $info['mpeg']['video']['raw']['progressive_sequence'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: progressive_sequence 0157 $info['mpeg']['video']['raw']['chroma_format'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for chroma_format 0158 $info['mpeg']['video']['raw']['horizontal_size_extension'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for horizontal_size_extension 0159 $info['mpeg']['video']['raw']['vertical_size_extension'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for vertical_size_extension 0160 $info['mpeg']['video']['raw']['bit_rate_extension'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 12); // 12 bits for bit_rate_extension 0161 $marker_bit = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // The term "marker_bit" indicates a one bit field in which the value zero is forbidden. These marker bits are introduced at several points in the syntax to avoid start code emulation. 0162 $info['mpeg']['video']['raw']['vbv_buffer_size_extension'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); // 8 bits for vbv_buffer_size_extension 0163 $info['mpeg']['video']['raw']['low_delay'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: low_delay 0164 $info['mpeg']['video']['raw']['frame_rate_extension_n'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for frame_rate_extension_n 0165 $info['mpeg']['video']['raw']['frame_rate_extension_d'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 5); // 5 bits for frame_rate_extension_d 0166 0167 $info['video']['resolution_x'] = ($info['mpeg']['video']['raw']['horizontal_size_extension'] << 12) | $info['mpeg']['video']['raw']['horizontal_size_value']; 0168 $info['video']['resolution_y'] = ($info['mpeg']['video']['raw']['vertical_size_extension'] << 12) | $info['mpeg']['video']['raw']['vertical_size_value']; 0169 $info['video']['interlaced'] = !$info['mpeg']['video']['raw']['progressive_sequence']; 0170 $info['mpeg']['video']['interlaced'] = !$info['mpeg']['video']['raw']['progressive_sequence']; 0171 $info['mpeg']['video']['chroma_format'] = self::chromaFormatTextLookup($info['mpeg']['video']['raw']['chroma_format']); 0172 break; 0173 0174 case 2: // 0010 Sequence Display Extension ID 0175 $info['mpeg']['video']['raw']['video_format'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 3); // 3 bits for video_format 0176 $info['mpeg']['video']['raw']['colour_description'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: colour_description 0177 if ($info['mpeg']['video']['raw']['colour_description']) { 0178 $info['mpeg']['video']['raw']['colour_primaries'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); // 8 bits for colour_primaries 0179 $info['mpeg']['video']['raw']['transfer_characteristics'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); // 8 bits for transfer_characteristics 0180 $info['mpeg']['video']['raw']['matrix_coefficients'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); // 8 bits for matrix_coefficients 0181 } 0182 $info['mpeg']['video']['raw']['display_horizontal_size'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 14); // 14 bits for display_horizontal_size 0183 $marker_bit = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // The term "marker_bit" indicates a one bit field in which the value zero is forbidden. These marker bits are introduced at several points in the syntax to avoid start code emulation. 0184 $info['mpeg']['video']['raw']['display_vertical_size'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 14); // 14 bits for display_vertical_size 0185 0186 $info['mpeg']['video']['video_format'] = self::videoFormatTextLookup($info['mpeg']['video']['raw']['video_format']); 0187 break; 0188 0189 case 3: // 0011 Quant Matrix Extension ID 0190 break; 0191 0192 case 5: // 0101 Sequence Scalable Extension ID 0193 $info['mpeg']['video']['raw']['scalable_mode'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for scalable_mode 0194 $info['mpeg']['video']['raw']['layer_id'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 4); // 4 bits for layer_id 0195 if ($info['mpeg']['video']['raw']['scalable_mode'] == 1) { // "spatial scalability" 0196 $info['mpeg']['video']['raw']['lower_layer_prediction_horizontal_size'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 14); // 14 bits for lower_layer_prediction_horizontal_size 0197 $marker_bit = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // The term "marker_bit" indicates a one bit field in which the value zero is forbidden. These marker bits are introduced at several points in the syntax to avoid start code emulation. 0198 $info['mpeg']['video']['raw']['lower_layer_prediction_vertical_size'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 14); // 14 bits for lower_layer_prediction_vertical_size 0199 $info['mpeg']['video']['raw']['horizontal_subsampling_factor_m'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 5); // 5 bits for horizontal_subsampling_factor_m 0200 $info['mpeg']['video']['raw']['horizontal_subsampling_factor_n'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 5); // 5 bits for horizontal_subsampling_factor_n 0201 $info['mpeg']['video']['raw']['vertical_subsampling_factor_m'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 5); // 5 bits for vertical_subsampling_factor_m 0202 $info['mpeg']['video']['raw']['vertical_subsampling_factor_n'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 5); // 5 bits for vertical_subsampling_factor_n 0203 } elseif ($info['mpeg']['video']['raw']['scalable_mode'] == 3) { // "temporal scalability" 0204 $info['mpeg']['video']['raw']['picture_mux_enable'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: picture_mux_enable 0205 if ($info['mpeg']['video']['raw']['picture_mux_enable']) { 0206 $info['mpeg']['video']['raw']['mux_to_progressive_sequence'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: mux_to_progressive_sequence 0207 } 0208 $info['mpeg']['video']['raw']['picture_mux_order'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 3); // 3 bits for picture_mux_order 0209 $info['mpeg']['video']['raw']['picture_mux_factor'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 3); // 3 bits for picture_mux_factor 0210 } 0211 0212 $info['mpeg']['video']['scalable_mode'] = self::scalableModeTextLookup($info['mpeg']['video']['raw']['scalable_mode']); 0213 break; 0214 0215 case 7: // 0111 Picture Display Extension ID 0216 break; 0217 0218 case 8: // 1000 Picture Coding Extension ID 0219 $info['mpeg']['video']['raw']['f_code_00'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 4); // 4 bits for f_code[0][0] (forward horizontal) 0220 $info['mpeg']['video']['raw']['f_code_01'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 4); // 4 bits for f_code[0][1] (forward vertical) 0221 $info['mpeg']['video']['raw']['f_code_10'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 4); // 4 bits for f_code[1][0] (backward horizontal) 0222 $info['mpeg']['video']['raw']['f_code_11'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 4); // 4 bits for f_code[1][1] (backward vertical) 0223 $info['mpeg']['video']['raw']['intra_dc_precision'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for intra_dc_precision 0224 $info['mpeg']['video']['raw']['picture_structure'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for picture_structure 0225 $info['mpeg']['video']['raw']['top_field_first'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: top_field_first 0226 $info['mpeg']['video']['raw']['frame_pred_frame_dct'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: frame_pred_frame_dct 0227 $info['mpeg']['video']['raw']['concealment_motion_vectors'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: concealment_motion_vectors 0228 $info['mpeg']['video']['raw']['q_scale_type'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: q_scale_type 0229 $info['mpeg']['video']['raw']['intra_vlc_format'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: intra_vlc_format 0230 $info['mpeg']['video']['raw']['alternate_scan'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: alternate_scan 0231 $info['mpeg']['video']['raw']['repeat_first_field'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: repeat_first_field 0232 $info['mpeg']['video']['raw']['chroma_420_type'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: chroma_420_type 0233 $info['mpeg']['video']['raw']['progressive_frame'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: progressive_frame 0234 $info['mpeg']['video']['raw']['composite_display_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: composite_display_flag 0235 if ($info['mpeg']['video']['raw']['composite_display_flag']) { 0236 $info['mpeg']['video']['raw']['v_axis'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: v_axis 0237 $info['mpeg']['video']['raw']['field_sequence'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 3); // 3 bits for field_sequence 0238 $info['mpeg']['video']['raw']['sub_carrier'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: sub_carrier 0239 $info['mpeg']['video']['raw']['burst_amplitude'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 7); // 7 bits for burst_amplitude 0240 $info['mpeg']['video']['raw']['sub_carrier_phase'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); // 8 bits for sub_carrier_phase 0241 } 0242 0243 $info['mpeg']['video']['intra_dc_precision_bits'] = $info['mpeg']['video']['raw']['intra_dc_precision'] + 8; 0244 $info['mpeg']['video']['picture_structure'] = self::pictureStructureTextLookup($info['mpeg']['video']['raw']['picture_structure']); 0245 break; 0246 0247 case 9: // 1001 Picture Spatial Scalable Extension ID 0248 break; 0249 case 10: // 1010 Picture Temporal Scalable Extension ID 0250 break; 0251 0252 default: 0253 $this->warning('Unexpected $info[mpeg][video][raw][extension_start_code_identifier] value of '.$info['mpeg']['video']['raw']['extension_start_code_identifier']); 0254 break; 0255 } 0256 break; 0257 0258 0259 case 0xB8: // group_of_pictures_header 0260 $GOPcounter++; 0261 if ($info['mpeg']['video']['bitrate_mode'] == 'vbr') { 0262 $bitstream = getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $StartCodeOffset + 4, 4)); // 27 bits needed for group_of_pictures_header 0263 $bitstreamoffset = 0; 0264 0265 $GOPheader = array(); 0266 0267 $GOPheader['byte_offset'] = $MPEGstreamBaseOffset + $StartCodeOffset; 0268 $GOPheader['drop_frame_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: drop_frame_flag 0269 $GOPheader['time_code_hours'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 5); // 5 bits for time_code_hours 0270 $GOPheader['time_code_minutes'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 6); // 6 bits for time_code_minutes 0271 $marker_bit = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // The term "marker_bit" indicates a one bit field in which the value zero is forbidden. These marker bits are introduced at several points in the syntax to avoid start code emulation. 0272 $GOPheader['time_code_seconds'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 6); // 6 bits for time_code_seconds 0273 $GOPheader['time_code_pictures'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 6); // 6 bits for time_code_pictures 0274 $GOPheader['closed_gop'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: closed_gop 0275 $GOPheader['broken_link'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: broken_link 0276 0277 $time_code_separator = ($GOPheader['drop_frame_flag'] ? ';' : ':'); // While non-drop time code is displayed with colons separating the digit pairs—"HH:MM:SS:FF"—drop frame is usually represented with a semi-colon (;) or period (.) as the divider between all the digit pairs—"HH;MM;SS;FF", "HH.MM.SS.FF" 0278 $GOPheader['time_code'] = sprintf('%02d'.$time_code_separator.'%02d'.$time_code_separator.'%02d'.$time_code_separator.'%02d', $GOPheader['time_code_hours'], $GOPheader['time_code_minutes'], $GOPheader['time_code_seconds'], $GOPheader['time_code_pictures']); 0279 0280 $info['mpeg']['group_of_pictures'][] = $GOPheader; 0281 } 0282 break; 0283 0284 case 0xC0: // audio stream 0285 case 0xC1: // audio stream 0286 case 0xC2: // audio stream 0287 case 0xC3: // audio stream 0288 case 0xC4: // audio stream 0289 case 0xC5: // audio stream 0290 case 0xC6: // audio stream 0291 case 0xC7: // audio stream 0292 case 0xC8: // audio stream 0293 case 0xC9: // audio stream 0294 case 0xCA: // audio stream 0295 case 0xCB: // audio stream 0296 case 0xCC: // audio stream 0297 case 0xCD: // audio stream 0298 case 0xCE: // audio stream 0299 case 0xCF: // audio stream 0300 case 0xD0: // audio stream 0301 case 0xD1: // audio stream 0302 case 0xD2: // audio stream 0303 case 0xD3: // audio stream 0304 case 0xD4: // audio stream 0305 case 0xD5: // audio stream 0306 case 0xD6: // audio stream 0307 case 0xD7: // audio stream 0308 case 0xD8: // audio stream 0309 case 0xD9: // audio stream 0310 case 0xDA: // audio stream 0311 case 0xDB: // audio stream 0312 case 0xDC: // audio stream 0313 case 0xDD: // audio stream 0314 case 0xDE: // audio stream 0315 case 0xDF: // audio stream 0316 //case 0xE0: // video stream 0317 //case 0xE1: // video stream 0318 //case 0xE2: // video stream 0319 //case 0xE3: // video stream 0320 //case 0xE4: // video stream 0321 //case 0xE5: // video stream 0322 //case 0xE6: // video stream 0323 //case 0xE7: // video stream 0324 //case 0xE8: // video stream 0325 //case 0xE9: // video stream 0326 //case 0xEA: // video stream 0327 //case 0xEB: // video stream 0328 //case 0xEC: // video stream 0329 //case 0xED: // video stream 0330 //case 0xEE: // video stream 0331 //case 0xEF: // video stream 0332 if (isset($ParsedAVchannels[$StartCodeValue])) { 0333 break; 0334 } 0335 $ParsedAVchannels[$StartCodeValue] = $StartCodeValue; 0336 // http://en.wikipedia.org/wiki/Packetized_elementary_stream 0337 // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html 0338 /* 0339 $PackedElementaryStream = array(); 0340 if ($StartCodeValue >= 0xE0) { 0341 $PackedElementaryStream['stream_type'] = 'video'; 0342 $PackedElementaryStream['stream_id'] = $StartCodeValue - 0xE0; 0343 } else { 0344 $PackedElementaryStream['stream_type'] = 'audio'; 0345 $PackedElementaryStream['stream_id'] = $StartCodeValue - 0xC0; 0346 } 0347 $PackedElementaryStream['packet_length'] = getid3_lib::BigEndian2Int(substr($MPEGstreamData, $StartCodeOffset + 4, 2)); 0348 0349 $bitstream = getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $StartCodeOffset + 6, 3)); // more may be needed below 0350 $bitstreamoffset = 0; 0351 0352 $PackedElementaryStream['marker_bits'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for marker_bits -- should be "10" = 2 0353 echo 'marker_bits = '.$PackedElementaryStream['marker_bits'].'<br>'; 0354 $PackedElementaryStream['scrambling_control'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 2); // 2 bits for scrambling_control -- 00 implies not scrambled 0355 $PackedElementaryStream['priority'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: priority 0356 $PackedElementaryStream['data_alignment_indicator'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: data_alignment_indicator -- 1 indicates that the PES packet header is immediately followed by the video start code or audio syncword 0357 $PackedElementaryStream['copyright'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: copyright -- 1 implies copyrighted 0358 $PackedElementaryStream['original_or_copy'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: original_or_copy -- 1 implies original 0359 $PackedElementaryStream['pts_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: pts_flag -- Presentation Time Stamp 0360 $PackedElementaryStream['dts_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: dts_flag -- Decode Time Stamp 0361 $PackedElementaryStream['escr_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: escr_flag -- Elementary Stream Clock Reference 0362 $PackedElementaryStream['es_rate_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: es_rate_flag -- Elementary Stream [data] Rate 0363 $PackedElementaryStream['dsm_trick_mode_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: dsm_trick_mode_flag -- DSM trick mode - not used by DVD 0364 $PackedElementaryStream['additional_copy_info_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: additional_copy_info_flag 0365 $PackedElementaryStream['crc_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: crc_flag 0366 $PackedElementaryStream['extension_flag'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 1); // 1 bit flag: extension_flag 0367 $PackedElementaryStream['pes_remain_header_length'] = self::readBitsFromStream($bitstream, $bitstreamoffset, 8); // 1 bit flag: priority 0368 0369 $additional_header_bytes = 0; 0370 $additional_header_bytes += ($PackedElementaryStream['pts_flag'] ? 5 : 0); 0371 $additional_header_bytes += ($PackedElementaryStream['dts_flag'] ? 5 : 0); 0372 $additional_header_bytes += ($PackedElementaryStream['escr_flag'] ? 6 : 0); 0373 $additional_header_bytes += ($PackedElementaryStream['es_rate_flag'] ? 3 : 0); 0374 $additional_header_bytes += ($PackedElementaryStream['additional_copy_info_flag'] ? 1 : 0); 0375 $additional_header_bytes += ($PackedElementaryStream['crc_flag'] ? 2 : 0); 0376 $additional_header_bytes += ($PackedElementaryStream['extension_flag'] ? 1 : 0); 0377 $PackedElementaryStream['additional_header_bytes'] = $additional_header_bytes; 0378 $bitstream .= getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $StartCodeOffset + 9, $additional_header_bytes)); 0379 0380 $info['mpeg']['packed_elementary_streams'][$PackedElementaryStream['stream_type']][$PackedElementaryStream['stream_id']][] = $PackedElementaryStream; 0381 */ 0382 $getid3_temp = new getID3(); 0383 $getid3_temp->openfile($this->getid3->filename); 0384 $getid3_temp->info = $info; 0385 $getid3_mp3 = new getid3_mp3($getid3_temp); 0386 for ($i = 0; $i <= 7; $i++) { 0387 // some files have the MPEG-audio header 8 bytes after the end of the $00 $00 $01 $C0 signature, some have it up to 13 bytes (or more?) after 0388 // I have no idea why or what the difference is, so this is a stupid hack. 0389 // If anybody has any better idea of what's going on, please let me know - info@getid3.org 0390 $getid3_temp->info = $info; // only overwrite real data if valid header found 0391 //echo 'audio at? '.($MPEGstreamBaseOffset + $StartCodeOffset + 4 + 8 + $i).'<br>'; 0392 if ($getid3_mp3->decodeMPEGaudioHeader($MPEGstreamBaseOffset + $StartCodeOffset + 4 + 8 + $i, $getid3_temp->info, false)) { 0393 //echo 'yes!<br>'; 0394 $info = $getid3_temp->info; 0395 $info['audio']['bitrate_mode'] = 'cbr'; 0396 $info['audio']['lossless'] = false; 0397 break; 0398 } 0399 } 0400 unset($getid3_temp, $getid3_mp3); 0401 break; 0402 0403 case 0xBC: // Program Stream Map 0404 case 0xBD: // Private stream 1 (non MPEG audio, subpictures) 0405 case 0xBE: // Padding stream 0406 case 0xBF: // Private stream 2 (navigation data) 0407 case 0xF0: // ECM stream 0408 case 0xF1: // EMM stream 0409 case 0xF2: // DSM-CC stream 0410 case 0xF3: // ISO/IEC_13522_stream 0411 case 0xF4: // ITU-I Rec. H.222.1 type A 0412 case 0xF5: // ITU-I Rec. H.222.1 type B 0413 case 0xF6: // ITU-I Rec. H.222.1 type C 0414 case 0xF7: // ITU-I Rec. H.222.1 type D 0415 case 0xF8: // ITU-I Rec. H.222.1 type E 0416 case 0xF9: // ancilliary stream 0417 case 0xFA: // ISO/IEC 14496-1 SL-packtized stream 0418 case 0xFB: // ISO/IEC 14496-1 FlexMux stream 0419 case 0xFC: // metadata stream 0420 case 0xFD: // extended stream ID 0421 case 0xFE: // reserved data stream 0422 case 0xFF: // program stream directory 0423 // ignore 0424 break; 0425 0426 default: 0427 // ignore 0428 break; 0429 } 0430 } while (true); 0431 0432 0433 0434 // // Temporary hack to account for interleaving overhead: 0435 // if (!empty($info['video']['bitrate']) && !empty($info['audio']['bitrate'])) { 0436 // $info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / ($info['video']['bitrate'] + $info['audio']['bitrate']); 0437 // 0438 // // Interleaved MPEG audio/video files have a certain amount of overhead that varies 0439 // // by both video and audio bitrates, and not in any sensible, linear/logarithmic pattern 0440 // // Use interpolated lookup tables to approximately guess how much is overhead, because 0441 // // playtime is calculated as filesize / total-bitrate 0442 // $info['playtime_seconds'] *= self::systemNonOverheadPercentage($info['video']['bitrate'], $info['audio']['bitrate']); 0443 // 0444 // //switch ($info['video']['bitrate']) { 0445 // // case('5000000'): 0446 // // $multiplier = 0.93292642112380355828048824319889; 0447 // // break; 0448 // // case('5500000'): 0449 // // $multiplier = 0.93582895375200989965359777343219; 0450 // // break; 0451 // // case('6000000'): 0452 // // $multiplier = 0.93796247714820932532911373859139; 0453 // // break; 0454 // // case('7000000'): 0455 // // $multiplier = 0.9413264083635103463010117778776; 0456 // // break; 0457 // // default: 0458 // // $multiplier = 1; 0459 // // break; 0460 // //} 0461 // //$info['playtime_seconds'] *= $multiplier; 0462 // //$info['warning'][] = 'Interleaved MPEG audio/video playtime may be inaccurate. With current hack should be within a few seconds of accurate. Report to info@getid3.org if off by more than 10 seconds.'; 0463 // if ($info['video']['bitrate'] < 50000) { 0464 // $this->warning('Interleaved MPEG audio/video playtime may be slightly inaccurate for video bitrates below 100kbps. Except in extreme low-bitrate situations, error should be less than 1%. Report to info@getid3.org if greater than this.'); 0465 // } 0466 // } 0467 // 0468 /* 0469 $time_prev = 0; 0470 $byte_prev = 0; 0471 $vbr_bitrates = array(); 0472 foreach ($info['mpeg']['group_of_pictures'] as $gopkey => $gopdata) { 0473 $time_this = ($gopdata['time_code_hours'] * 3600) + ($gopdata['time_code_minutes'] * 60) + $gopdata['time_code_seconds'] + ($gopdata['time_code_seconds'] / 30); 0474 $byte_this = $gopdata['byte_offset']; 0475 if ($gopkey > 0) { 0476 if ($time_this > $time_prev) { 0477 $bytedelta = $byte_this - $byte_prev; 0478 $timedelta = $time_this - $time_prev; 0479 $this_bitrate = ($bytedelta * 8) / $timedelta; 0480 echo $gopkey.': ('.number_format($time_prev, 2).'-'.number_format($time_this, 2).') '.number_format($bytedelta).' bytes over '.number_format($timedelta, 3).' seconds = '.number_format($this_bitrate / 1000, 2).'kbps<br>'; 0481 $time_prev = $time_this; 0482 $byte_prev = $byte_this; 0483 $vbr_bitrates[] = $this_bitrate; 0484 } 0485 } 0486 } 0487 echo 'average_File_bitrate = '.number_format(array_sum($vbr_bitrates) / count($vbr_bitrates), 1).'<br>'; 0488 */ 0489 //echo '<pre>'.print_r($FramesByGOP, true).'</pre>'; 0490 if (!empty($info['mpeg']['video']['bitrate_mode']) && ($info['mpeg']['video']['bitrate_mode'] == 'vbr')) { 0491 $last_GOP_id = max(array_keys($FramesByGOP)); 0492 $frames_in_last_GOP = count($FramesByGOP[$last_GOP_id]); 0493 $gopdata = &$info['mpeg']['group_of_pictures'][$last_GOP_id]; 0494 $info['playtime_seconds'] = ($gopdata['time_code_hours'] * 3600) + ($gopdata['time_code_minutes'] * 60) + $gopdata['time_code_seconds'] + (($gopdata['time_code_pictures'] + $frames_in_last_GOP + 1) / $info['mpeg']['video']['frame_rate']); 0495 if (!isset($info['video']['bitrate'])) { 0496 $overall_bitrate = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['playtime_seconds']; 0497 $info['video']['bitrate'] = $overall_bitrate - (isset($info['audio']['bitrate']) ? $info['audio']['bitrate'] : 0); 0498 } 0499 unset($info['mpeg']['group_of_pictures']); 0500 } 0501 0502 return true; 0503 } 0504 0505 private function readBitsFromStream(&$bitstream, &$bitstreamoffset, $bits_to_read, $return_singlebit_as_boolean=true) { 0506 $return = bindec(substr($bitstream, $bitstreamoffset, $bits_to_read)); 0507 $bitstreamoffset += $bits_to_read; 0508 if (($bits_to_read == 1) && $return_singlebit_as_boolean) { 0509 $return = (bool) $return; 0510 } 0511 return $return; 0512 } 0513 0514 0515 public static function systemNonOverheadPercentage($VideoBitrate, $AudioBitrate) { 0516 $OverheadPercentage = 0; 0517 0518 $AudioBitrate = max(min($AudioBitrate / 1000, 384), 32); // limit to range of 32kbps - 384kbps (should be only legal bitrates, but maybe VBR?) 0519 $VideoBitrate = max(min($VideoBitrate / 1000, 10000), 10); // limit to range of 10kbps - 10Mbps (beyond that curves flatten anyways, no big loss) 0520 0521 0522 //OMBB[audiobitrate] = array(video-10kbps, video-100kbps, video-1000kbps, video-10000kbps) 0523 $OverheadMultiplierByBitrate[32] = array(0, 0.9676287944368530, 0.9802276264360310, 0.9844916183244460, 0.9852821845179940); 0524 $OverheadMultiplierByBitrate[48] = array(0, 0.9779100089209830, 0.9787770035359320, 0.9846738664076130, 0.9852683013799960); 0525 $OverheadMultiplierByBitrate[56] = array(0, 0.9731249855367600, 0.9776624308938040, 0.9832606361852130, 0.9843922606633340); 0526 $OverheadMultiplierByBitrate[64] = array(0, 0.9755642683275760, 0.9795256705493390, 0.9836573009193170, 0.9851122539404470); 0527 $OverheadMultiplierByBitrate[96] = array(0, 0.9788025247497290, 0.9798553314148700, 0.9822956869792560, 0.9834815119124690); 0528 $OverheadMultiplierByBitrate[128] = array(0, 0.9816940050925480, 0.9821675936072120, 0.9829756927470870, 0.9839763420152050); 0529 $OverheadMultiplierByBitrate[160] = array(0, 0.9825894094561180, 0.9820913399073960, 0.9823907143253970, 0.9832821783651570); 0530 $OverheadMultiplierByBitrate[192] = array(0, 0.9832038474336260, 0.9825731694317960, 0.9821028622712400, 0.9828262076447620); 0531 $OverheadMultiplierByBitrate[224] = array(0, 0.9836516298538770, 0.9824718601823890, 0.9818302180625380, 0.9823735101626480); 0532 $OverheadMultiplierByBitrate[256] = array(0, 0.9845863022094920, 0.9837229411967540, 0.9824521662210830, 0.9828645172100790); 0533 $OverheadMultiplierByBitrate[320] = array(0, 0.9849565280263180, 0.9837683142805110, 0.9822885275960400, 0.9824424382727190); 0534 $OverheadMultiplierByBitrate[384] = array(0, 0.9856094774357600, 0.9844573394432720, 0.9825970399837330, 0.9824673808303890); 0535 0536 $BitrateToUseMin = 32; 0537 $BitrateToUseMax = 32; 0538 $previousBitrate = 32; 0539 foreach ($OverheadMultiplierByBitrate as $key => $value) { 0540 if ($AudioBitrate >= $previousBitrate) { 0541 $BitrateToUseMin = $previousBitrate; 0542 } 0543 if ($AudioBitrate < $key) { 0544 $BitrateToUseMax = $key; 0545 break; 0546 } 0547 $previousBitrate = $key; 0548 } 0549 $FactorA = ($BitrateToUseMax - $AudioBitrate) / ($BitrateToUseMax - $BitrateToUseMin); 0550 0551 $VideoBitrateLog10 = log10($VideoBitrate); 0552 $VideoFactorMin1 = $OverheadMultiplierByBitrate[$BitrateToUseMin][floor($VideoBitrateLog10)]; 0553 $VideoFactorMin2 = $OverheadMultiplierByBitrate[$BitrateToUseMax][floor($VideoBitrateLog10)]; 0554 $VideoFactorMax1 = $OverheadMultiplierByBitrate[$BitrateToUseMin][ceil($VideoBitrateLog10)]; 0555 $VideoFactorMax2 = $OverheadMultiplierByBitrate[$BitrateToUseMax][ceil($VideoBitrateLog10)]; 0556 $FactorV = $VideoBitrateLog10 - floor($VideoBitrateLog10); 0557 0558 $OverheadPercentage = $VideoFactorMin1 * $FactorA * $FactorV; 0559 $OverheadPercentage += $VideoFactorMin2 * (1 - $FactorA) * $FactorV; 0560 $OverheadPercentage += $VideoFactorMax1 * $FactorA * (1 - $FactorV); 0561 $OverheadPercentage += $VideoFactorMax2 * (1 - $FactorA) * (1 - $FactorV); 0562 0563 return $OverheadPercentage; 0564 } 0565 0566 0567 public static function videoFramerateLookup($rawframerate) { 0568 $lookup = array(0, 23.976, 24, 25, 29.97, 30, 50, 59.94, 60); 0569 return (isset($lookup[$rawframerate]) ? (float) $lookup[$rawframerate] : (float) 0); 0570 } 0571 0572 public static function videoAspectRatioLookup($rawaspectratio) { 0573 $lookup = 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); 0574 return (isset($lookup[$rawaspectratio]) ? (float) $lookup[$rawaspectratio] : (float) 0); 0575 } 0576 0577 public static function videoAspectRatioTextLookup($rawaspectratio) { 0578 $lookup = 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'); 0579 return (isset($lookup[$rawaspectratio]) ? $lookup[$rawaspectratio] : ''); 0580 } 0581 0582 public static function videoFormatTextLookup($video_format) { 0583 // ISO/IEC 13818-2, section 6.3.6, Table 6-6. Meaning of video_format 0584 $lookup = array('component', 'PAL', 'NTSC', 'SECAM', 'MAC', 'Unspecified video format', 'reserved(6)', 'reserved(7)'); 0585 return (isset($lookup[$video_format]) ? $lookup[$video_format] : ''); 0586 } 0587 0588 public static function scalableModeTextLookup($scalable_mode) { 0589 // ISO/IEC 13818-2, section 6.3.8, Table 6-10. Definition of scalable_mode 0590 $lookup = array('data partitioning', 'spatial scalability', 'SNR scalability', 'temporal scalability'); 0591 return (isset($lookup[$scalable_mode]) ? $lookup[$scalable_mode] : ''); 0592 } 0593 0594 public static function pictureStructureTextLookup($picture_structure) { 0595 // ISO/IEC 13818-2, section 6.3.11, Table 6-14 Meaning of picture_structure 0596 $lookup = array('reserved', 'Top Field', 'Bottom Field', 'Frame picture'); 0597 return (isset($lookup[$picture_structure]) ? $lookup[$picture_structure] : ''); 0598 } 0599 0600 public static function chromaFormatTextLookup($chroma_format) { 0601 // ISO/IEC 13818-2, section 6.3.11, Table 6-14 Meaning of picture_structure 0602 $lookup = array('reserved', '4:2:0', '4:2:2', '4:4:4'); 0603 return (isset($lookup[$chroma_format]) ? $lookup[$chroma_format] : ''); 0604 } 0605 0606 }