annotate e2gallerypro/e2upload/Backend/Assets/getid3/module.audio-video.mpeg.php @ 28:118cb614cf18 judyates tip

judy bio.
author Robert McIntyre <rlm@mit.edu>
date Tue, 29 Dec 2015 16:26:18 -0800
parents 3f6b44aa6b35
children
rev   line source
rlm@3 1 <?php
rlm@3 2 // +----------------------------------------------------------------------+
rlm@3 3 // | PHP version 5 |
rlm@3 4 // +----------------------------------------------------------------------+
rlm@3 5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
rlm@3 6 // +----------------------------------------------------------------------+
rlm@3 7 // | This source file is subject to version 2 of the GPL license, |
rlm@3 8 // | that is bundled with this package in the file license.txt and is |
rlm@3 9 // | available through the world-wide-web at the following url: |
rlm@3 10 // | http://www.gnu.org/copyleft/gpl.html |
rlm@3 11 // +----------------------------------------------------------------------+
rlm@3 12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
rlm@3 13 // +----------------------------------------------------------------------+
rlm@3 14 // | Authors: James Heinrich <infoØgetid3*org> |
rlm@3 15 // | Allan Hansen <ahØartemis*dk> |
rlm@3 16 // +----------------------------------------------------------------------+
rlm@3 17 // | module.audio-video.mpeg.php |
rlm@3 18 // | Module for analyzing MPEG files |
rlm@3 19 // | dependencies: module.audio.mp3.php |
rlm@3 20 // +----------------------------------------------------------------------+
rlm@3 21 //
rlm@3 22 // $Id: module.audio-video.mpeg.php,v 1.3 2006/11/02 10:48:00 ah Exp $
rlm@3 23
rlm@3 24
rlm@3 25
rlm@3 26 class getid3_mpeg extends getid3_handler
rlm@3 27 {
rlm@3 28
rlm@3 29 const VIDEO_PICTURE_START = "\x00\x00\x01\x00";
rlm@3 30 const VIDEO_USER_DATA_START = "\x00\x00\x01\xB2";
rlm@3 31 const VIDEO_SEQUENCE_HEADER = "\x00\x00\x01\xB3";
rlm@3 32 const VIDEO_SEQUENCE_ERROR = "\x00\x00\x01\xB4";
rlm@3 33 const VIDEO_EXTENSION_START = "\x00\x00\x01\xB5";
rlm@3 34 const VIDEO_SEQUENCE_END = "\x00\x00\x01\xB7";
rlm@3 35 const VIDEO_GROUP_START = "\x00\x00\x01\xB8";
rlm@3 36 const AUDIO_START = "\x00\x00\x01\xC0";
rlm@3 37
rlm@3 38
rlm@3 39 public function Analyze() {
rlm@3 40
rlm@3 41 $getid3 = $this->getid3;
rlm@3 42
rlm@3 43 $getid3->info['mpeg']['video']['raw'] = array ();
rlm@3 44 $info_mpeg_video = &$getid3->info['mpeg']['video'];
rlm@3 45 $info_mpeg_video_raw = &$info_mpeg_video['raw'];
rlm@3 46
rlm@3 47 $getid3->info['video'] = array ();
rlm@3 48 $info_video = &$getid3->info['video'];
rlm@3 49
rlm@3 50 $getid3->include_module('audio.mp3');
rlm@3 51
rlm@3 52 if ($getid3->info['avdataend'] <= $getid3->info['avdataoffset']) {
rlm@3 53 throw new getid3_exception('"avdataend" ('.$getid3->info['avdataend'].') is unexpectedly less-than-or-equal-to "avdataoffset" ('.$getid3->info['avdataoffset'].')');
rlm@3 54 }
rlm@3 55
rlm@3 56 $getid3->info['fileformat'] = 'mpeg';
rlm@3 57 fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
rlm@3 58 $mpeg_stream_data = fread($getid3->fp, min(100000, $getid3->info['avdataend'] - $getid3->info['avdataoffset']));
rlm@3 59 $mpeg_stream_data_length = strlen($mpeg_stream_data);
rlm@3 60
rlm@3 61 $video_chunk_offset = 0;
rlm@3 62 while (substr($mpeg_stream_data, $video_chunk_offset++, 4) !== getid3_mpeg::VIDEO_SEQUENCE_HEADER) {
rlm@3 63 if ($video_chunk_offset >= $mpeg_stream_data_length) {
rlm@3 64 throw new getid3_exception('Could not find start of video block in the first 100,000 bytes (or before end of file) - this might not be an MPEG-video file?');
rlm@3 65 }
rlm@3 66 }
rlm@3 67
rlm@3 68 // Start code 32 bits
rlm@3 69 // horizontal frame size 12 bits
rlm@3 70 // vertical frame size 12 bits
rlm@3 71 // pixel aspect ratio 4 bits
rlm@3 72 // frame rate 4 bits
rlm@3 73 // bitrate 18 bits
rlm@3 74 // marker bit 1 bit
rlm@3 75 // VBV buffer size 10 bits
rlm@3 76 // constrained parameter flag 1 bit
rlm@3 77 // intra quant. matrix flag 1 bit
rlm@3 78 // intra quant. matrix values 512 bits (present if matrix flag == 1)
rlm@3 79 // non-intra quant. matrix flag 1 bit
rlm@3 80 // non-intra quant. matrix values 512 bits (present if matrix flag == 1)
rlm@3 81
rlm@3 82 $info_video['dataformat'] = 'mpeg';
rlm@3 83
rlm@3 84 $video_chunk_offset += (strlen(getid3_mpeg::VIDEO_SEQUENCE_HEADER) - 1);
rlm@3 85
rlm@3 86 $frame_size_dword = getid3_lib::BigEndian2Int(substr($mpeg_stream_data, $video_chunk_offset, 3));
rlm@3 87 $video_chunk_offset += 3;
rlm@3 88
rlm@3 89 $aspect_ratio_frame_rate_dword = getid3_lib::BigEndian2Int(substr($mpeg_stream_data, $video_chunk_offset, 1));
rlm@3 90 $video_chunk_offset += 1;
rlm@3 91
rlm@3 92 $assorted_information = getid3_lib::BigEndian2Bin(substr($mpeg_stream_data, $video_chunk_offset, 4));
rlm@3 93 $video_chunk_offset += 4;
rlm@3 94
rlm@3 95 $info_mpeg_video_raw['framesize_horizontal'] = ($frame_size_dword & 0xFFF000) >> 12; // 12 bits for horizontal frame size
rlm@3 96 $info_mpeg_video_raw['framesize_vertical'] = ($frame_size_dword & 0x000FFF); // 12 bits for vertical frame size
rlm@3 97 $info_mpeg_video_raw['pixel_aspect_ratio'] = ($aspect_ratio_frame_rate_dword & 0xF0) >> 4;
rlm@3 98 $info_mpeg_video_raw['frame_rate'] = ($aspect_ratio_frame_rate_dword & 0x0F);
rlm@3 99
rlm@3 100 $info_mpeg_video['framesize_horizontal'] = $info_mpeg_video_raw['framesize_horizontal'];
rlm@3 101 $info_mpeg_video['framesize_vertical'] = $info_mpeg_video_raw['framesize_vertical'];
rlm@3 102
rlm@3 103 $info_mpeg_video['pixel_aspect_ratio'] = $this->MPEGvideoAspectRatioLookup($info_mpeg_video_raw['pixel_aspect_ratio']);
rlm@3 104 $info_mpeg_video['pixel_aspect_ratio_text'] = $this->MPEGvideoAspectRatioTextLookup($info_mpeg_video_raw['pixel_aspect_ratio']);
rlm@3 105 $info_mpeg_video['frame_rate'] = $this->MPEGvideoFramerateLookup($info_mpeg_video_raw['frame_rate']);
rlm@3 106
rlm@3 107 $info_mpeg_video_raw['bitrate'] = bindec(substr($assorted_information, 0, 18));
rlm@3 108 $info_mpeg_video_raw['marker_bit'] = (bool)bindec($assorted_information{18});
rlm@3 109 $info_mpeg_video_raw['vbv_buffer_size'] = bindec(substr($assorted_information, 19, 10));
rlm@3 110 $info_mpeg_video_raw['constrained_param_flag'] = (bool)bindec($assorted_information{29});
rlm@3 111 $info_mpeg_video_raw['intra_quant_flag'] = (bool)bindec($assorted_information{30});
rlm@3 112
rlm@3 113 if ($info_mpeg_video_raw['intra_quant_flag']) {
rlm@3 114
rlm@3 115 // read 512 bits
rlm@3 116 $info_mpeg_video_raw['intra_quant'] = getid3_lib::BigEndian2Bin(substr($mpeg_stream_data, $video_chunk_offset, 64));
rlm@3 117 $video_chunk_offset += 64;
rlm@3 118
rlm@3 119 $info_mpeg_video_raw['non_intra_quant_flag'] = (bool)bindec($info_mpeg_video_raw['intra_quant']{511});
rlm@3 120 $info_mpeg_video_raw['intra_quant'] = bindec($assorted_information{31}).substr(getid3_lib::BigEndian2Bin(substr($mpeg_stream_data, $video_chunk_offset, 64)), 0, 511);
rlm@3 121
rlm@3 122 if ($info_mpeg_video_raw['non_intra_quant_flag']) {
rlm@3 123 $info_mpeg_video_raw['non_intra_quant'] = substr($mpeg_stream_data, $video_chunk_offset, 64);
rlm@3 124 $video_chunk_offset += 64;
rlm@3 125 }
rlm@3 126
rlm@3 127 } else {
rlm@3 128
rlm@3 129 $info_mpeg_video_raw['non_intra_quant_flag'] = (bool)bindec($assorted_information{31});
rlm@3 130 if ($info_mpeg_video_raw['non_intra_quant_flag']) {
rlm@3 131 $info_mpeg_video_raw['non_intra_quant'] = substr($mpeg_stream_data, $video_chunk_offset, 64);
rlm@3 132 $video_chunk_offset += 64;
rlm@3 133 }
rlm@3 134 }
rlm@3 135
rlm@3 136 if ($info_mpeg_video_raw['bitrate'] == 0x3FFFF) { // 18 set bits
rlm@3 137
rlm@3 138 $getid3->warning('This version of getID3() cannot determine average bitrate of VBR MPEG video files');
rlm@3 139 $info_mpeg_video['bitrate_mode'] = 'vbr';
rlm@3 140
rlm@3 141 } else {
rlm@3 142
rlm@3 143 $info_mpeg_video['bitrate'] = $info_mpeg_video_raw['bitrate'] * 400;
rlm@3 144 $info_mpeg_video['bitrate_mode'] = 'cbr';
rlm@3 145 $info_video['bitrate'] = $info_mpeg_video['bitrate'];
rlm@3 146 }
rlm@3 147
rlm@3 148 $info_video['resolution_x'] = $info_mpeg_video['framesize_horizontal'];
rlm@3 149 $info_video['resolution_y'] = $info_mpeg_video['framesize_vertical'];
rlm@3 150 $info_video['frame_rate'] = $info_mpeg_video['frame_rate'];
rlm@3 151 $info_video['bitrate_mode'] = $info_mpeg_video['bitrate_mode'];
rlm@3 152 $info_video['pixel_aspect_ratio'] = $info_mpeg_video['pixel_aspect_ratio'];
rlm@3 153 $info_video['lossless'] = false;
rlm@3 154 $info_video['bits_per_sample'] = 24;
rlm@3 155
rlm@3 156
rlm@3 157 //0x000001B3 begins the sequence_header of every MPEG video stream.
rlm@3 158 //But in MPEG-2, this header must immediately be followed by an
rlm@3 159 //extension_start_code (0x000001B5) with a sequence_extension ID (1).
rlm@3 160 //(This extension contains all the additional MPEG-2 stuff.)
rlm@3 161 //MPEG-1 doesn't have this extension, so that's a sure way to tell the
rlm@3 162 //difference between MPEG-1 and MPEG-2 video streams.
rlm@3 163
rlm@3 164 $info_video['codec'] = substr($mpeg_stream_data, $video_chunk_offset, 4) == getid3_mpeg::VIDEO_EXTENSION_START ? 'MPEG-2' : 'MPEG-1';
rlm@3 165
rlm@3 166 $audio_chunk_offset = 0;
rlm@3 167 while (true) {
rlm@3 168 while (substr($mpeg_stream_data, $audio_chunk_offset++, 4) !== getid3_mpeg::AUDIO_START) {
rlm@3 169 if ($audio_chunk_offset >= $mpeg_stream_data_length) {
rlm@3 170 break 2;
rlm@3 171 }
rlm@3 172 }
rlm@3 173
rlm@3 174 for ($i = 0; $i <= 7; $i++) {
rlm@3 175 // 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
rlm@3 176 // I have no idea why or what the difference is, so this is a stupid hack.
rlm@3 177 // If anybody has any better idea of what's going on, please let me know - info@getid3.org
rlm@3 178
rlm@3 179 // make copy of info
rlm@3 180 $dummy = $getid3->info;
rlm@3 181
rlm@3 182 // clone getid3 - better safe than sorry
rlm@3 183 $clone = clone $this->getid3;
rlm@3 184
rlm@3 185 // check
rlm@3 186 $mp3 = new getid3_mp3($clone);
rlm@3 187 if ($mp3->decodeMPEGaudioHeader($getid3->fp, ($audio_chunk_offset + 3) + 8 + $i, $dummy, false)) {
rlm@3 188
rlm@3 189 $getid3->info = $dummy;
rlm@3 190 $getid3->info['audio']['bitrate_mode'] = 'cbr';
rlm@3 191 $getid3->info['audio']['lossless'] = false;
rlm@3 192 break 2;
rlm@3 193 }
rlm@3 194
rlm@3 195 // destroy copy
rlm@3 196 unset($dummy);
rlm@3 197 }
rlm@3 198 }
rlm@3 199
rlm@3 200 // Temporary hack to account for interleaving overhead:
rlm@3 201 if (!empty($info_video['bitrate']) && !empty($getid3->info['audio']['bitrate'])) {
rlm@3 202 $getid3->info['playtime_seconds'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / ($info_video['bitrate'] + $getid3->info['audio']['bitrate']);
rlm@3 203
rlm@3 204 // Interleaved MPEG audio/video files have a certain amount of overhead that varies
rlm@3 205 // by both video and audio bitrates, and not in any sensible, linear/logarithmic patter
rlm@3 206 // Use interpolated lookup tables to approximately guess how much is overhead, because
rlm@3 207 // playtime is calculated as filesize / total-bitrate
rlm@3 208 $getid3->info['playtime_seconds'] *= $this->MPEGsystemNonOverheadPercentage($info_video['bitrate'], $getid3->info['audio']['bitrate']);
rlm@3 209
rlm@3 210 //switch ($info_video['bitrate']) {
rlm@3 211 // case('5000000'):
rlm@3 212 // $multiplier = 0.93292642112380355828048824319889;
rlm@3 213 // break;
rlm@3 214 // case('5500000'):
rlm@3 215 // $multiplier = 0.93582895375200989965359777343219;
rlm@3 216 // break;
rlm@3 217 // case('6000000'):
rlm@3 218 // $multiplier = 0.93796247714820932532911373859139;
rlm@3 219 // break;
rlm@3 220 // case('7000000'):
rlm@3 221 // $multiplier = 0.9413264083635103463010117778776;
rlm@3 222 // break;
rlm@3 223 // default:
rlm@3 224 // $multiplier = 1;
rlm@3 225 // break;
rlm@3 226 //}
rlm@3 227 //$getid3->info['playtime_seconds'] *= $multiplier;
rlm@3 228 //$getid3->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.');
rlm@3 229
rlm@3 230 if ($info_video['bitrate'] < 50000) {
rlm@3 231 $getid3->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.');
rlm@3 232 }
rlm@3 233 }
rlm@3 234
rlm@3 235 return true;
rlm@3 236 }
rlm@3 237
rlm@3 238
rlm@3 239
rlm@3 240 public static function MPEGsystemNonOverheadPercentage($video_bitrate, $audio_bitrate) {
rlm@3 241
rlm@3 242 $overhead_percentage = 0;
rlm@3 243
rlm@3 244 $audio_bitrate = max(min($audio_bitrate / 1000, 384), 32); // limit to range of 32kbps - 384kbps (should be only legal bitrates, but maybe VBR?)
rlm@3 245 $video_bitrate = max(min($video_bitrate / 1000, 10000), 10); // limit to range of 10kbps - 10Mbps (beyond that curves flatten anyways, no big loss)
rlm@3 246
rlm@3 247 //OMBB[audiobitrate] = array ( video-10kbps, video-100kbps, video-1000kbps, video-10000kbps)
rlm@3 248 static $overhead_multiplier_by_bitrate = array (
rlm@3 249 32 => array (0, 0.9676287944368530, 0.9802276264360310, 0.9844916183244460, 0.9852821845179940),
rlm@3 250 48 => array (0, 0.9779100089209830, 0.9787770035359320, 0.9846738664076130, 0.9852683013799960),
rlm@3 251 56 => array (0, 0.9731249855367600, 0.9776624308938040, 0.9832606361852130, 0.9843922606633340),
rlm@3 252 64 => array (0, 0.9755642683275760, 0.9795256705493390, 0.9836573009193170, 0.9851122539404470),
rlm@3 253 96 => array (0, 0.9788025247497290, 0.9798553314148700, 0.9822956869792560, 0.9834815119124690),
rlm@3 254 128 => array (0, 0.9816940050925480, 0.9821675936072120, 0.9829756927470870, 0.9839763420152050),
rlm@3 255 160 => array (0, 0.9825894094561180, 0.9820913399073960, 0.9823907143253970, 0.9832821783651570),
rlm@3 256 192 => array (0, 0.9832038474336260, 0.9825731694317960, 0.9821028622712400, 0.9828262076447620),
rlm@3 257 224 => array (0, 0.9836516298538770, 0.9824718601823890, 0.9818302180625380, 0.9823735101626480),
rlm@3 258 256 => array (0, 0.9845863022094920, 0.9837229411967540, 0.9824521662210830, 0.9828645172100790),
rlm@3 259 320 => array (0, 0.9849565280263180, 0.9837683142805110, 0.9822885275960400, 0.9824424382727190),
rlm@3 260 384 => array (0, 0.9856094774357600, 0.9844573394432720, 0.9825970399837330, 0.9824673808303890)
rlm@3 261 );
rlm@3 262
rlm@3 263 $bitrate_to_use_min = $bitrate_to_use_max = $previous_bitrate = 32;
rlm@3 264
rlm@3 265 foreach ($overhead_multiplier_by_bitrate as $key => $value) {
rlm@3 266
rlm@3 267 if ($audio_bitrate >= $previous_bitrate) {
rlm@3 268 $bitrate_to_use_min = $previous_bitrate;
rlm@3 269 }
rlm@3 270 if ($audio_bitrate < $key) {
rlm@3 271 $bitrate_to_use_max = $key;
rlm@3 272 break;
rlm@3 273 }
rlm@3 274 $previous_bitrate = $key;
rlm@3 275 }
rlm@3 276
rlm@3 277 $factor_a = ($bitrate_to_use_max - $audio_bitrate) / ($bitrate_to_use_max - $bitrate_to_use_min);
rlm@3 278
rlm@3 279 $video_bitrate_log10 = log10($video_bitrate);
rlm@3 280 $video_factor_min1 = $overhead_multiplier_by_bitrate[$bitrate_to_use_min][floor($video_bitrate_log10)];
rlm@3 281 $video_factor_min2 = $overhead_multiplier_by_bitrate[$bitrate_to_use_max][floor($video_bitrate_log10)];
rlm@3 282 $video_factor_max1 = $overhead_multiplier_by_bitrate[$bitrate_to_use_min][ceil($video_bitrate_log10)];
rlm@3 283 $video_factor_max2 = $overhead_multiplier_by_bitrate[$bitrate_to_use_max][ceil($video_bitrate_log10)];
rlm@3 284 $factor_v = $video_bitrate_log10 - floor($video_bitrate_log10);
rlm@3 285
rlm@3 286 $overhead_percentage = $video_factor_min1 * $factor_a * $factor_v;
rlm@3 287 $overhead_percentage += $video_factor_min2 * (1 - $factor_a) * $factor_v;
rlm@3 288 $overhead_percentage += $video_factor_max1 * $factor_a * (1 - $factor_v);
rlm@3 289 $overhead_percentage += $video_factor_max2 * (1 - $factor_a) * (1 - $factor_v);
rlm@3 290
rlm@3 291 return $overhead_percentage;
rlm@3 292 }
rlm@3 293
rlm@3 294
rlm@3 295
rlm@3 296 public static function MPEGvideoFramerateLookup($raw_frame_rate) {
rlm@3 297
rlm@3 298 $lookup = array (0, 23.976, 24, 25, 29.97, 30, 50, 59.94, 60);
rlm@3 299
rlm@3 300 return (float)(isset($lookup[$raw_frame_rate]) ? $lookup[$raw_frame_rate] : 0);
rlm@3 301 }
rlm@3 302
rlm@3 303
rlm@3 304
rlm@3 305 public static function MPEGvideoAspectRatioLookup($raw_aspect_ratio) {
rlm@3 306
rlm@3 307 $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);
rlm@3 308
rlm@3 309 return (float)(isset($lookup[$raw_aspect_ratio]) ? $lookup[$raw_aspect_ratio] : 0);
rlm@3 310 }
rlm@3 311
rlm@3 312
rlm@3 313
rlm@3 314 public static function MPEGvideoAspectRatioTextLookup($raw_aspect_ratio) {
rlm@3 315
rlm@3 316 $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');
rlm@3 317
rlm@3 318 return (isset($lookup[$raw_aspect_ratio]) ? $lookup[$raw_aspect_ratio] : '');
rlm@3 319 }
rlm@3 320
rlm@3 321 }
rlm@3 322
rlm@3 323
rlm@3 324 ?>