diff e2gallerypro/e2upload/Backend/Assets/getid3/module.audio-video.mpeg.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/e2gallerypro/e2upload/Backend/Assets/getid3/module.audio-video.mpeg.php	Mon Feb 22 08:02:39 2010 -0500
     1.3 @@ -0,0 +1,324 @@
     1.4 +<?php
     1.5 +// +----------------------------------------------------------------------+
     1.6 +// | PHP version 5                                                        |
     1.7 +// +----------------------------------------------------------------------+
     1.8 +// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
     1.9 +// +----------------------------------------------------------------------+
    1.10 +// | This source file is subject to version 2 of the GPL license,         |
    1.11 +// | that is bundled with this package in the file license.txt and is     |
    1.12 +// | available through the world-wide-web at the following url:           |
    1.13 +// | http://www.gnu.org/copyleft/gpl.html                                 |
    1.14 +// +----------------------------------------------------------------------+
    1.15 +// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
    1.16 +// +----------------------------------------------------------------------+
    1.17 +// | Authors: James Heinrich <infoØgetid3*org>                            |
    1.18 +// |          Allan Hansen <ahØartemis*dk>                                |
    1.19 +// +----------------------------------------------------------------------+
    1.20 +// | module.audio-video.mpeg.php                                          |
    1.21 +// | Module for analyzing MPEG files                                      |
    1.22 +// | dependencies: module.audio.mp3.php                                   |
    1.23 +// +----------------------------------------------------------------------+
    1.24 +//
    1.25 +// $Id: module.audio-video.mpeg.php,v 1.3 2006/11/02 10:48:00 ah Exp $
    1.26 +
    1.27 +        
    1.28 +        
    1.29 +class getid3_mpeg extends getid3_handler
    1.30 +{
    1.31 +
    1.32 +    const VIDEO_PICTURE_START   = "\x00\x00\x01\x00";
    1.33 +    const VIDEO_USER_DATA_START = "\x00\x00\x01\xB2";
    1.34 +    const VIDEO_SEQUENCE_HEADER = "\x00\x00\x01\xB3";
    1.35 +    const VIDEO_SEQUENCE_ERROR  = "\x00\x00\x01\xB4";
    1.36 +    const VIDEO_EXTENSION_START = "\x00\x00\x01\xB5";
    1.37 +    const VIDEO_SEQUENCE_END    = "\x00\x00\x01\xB7";
    1.38 +    const VIDEO_GROUP_START     = "\x00\x00\x01\xB8";
    1.39 +    const AUDIO_START           = "\x00\x00\x01\xC0";
    1.40 +    
    1.41 +
    1.42 +    public function Analyze() {
    1.43 +        
    1.44 +        $getid3 = $this->getid3;
    1.45 +        
    1.46 +        $getid3->info['mpeg']['video']['raw'] = array ();
    1.47 +        $info_mpeg_video     = &$getid3->info['mpeg']['video'];
    1.48 +        $info_mpeg_video_raw = &$info_mpeg_video['raw'];
    1.49 +        
    1.50 +        $getid3->info['video'] = array ();
    1.51 +        $info_video = &$getid3->info['video'];
    1.52 +        
    1.53 +        $getid3->include_module('audio.mp3');
    1.54 +        
    1.55 +        if ($getid3->info['avdataend'] <= $getid3->info['avdataoffset']) {
    1.56 +            throw new getid3_exception('"avdataend" ('.$getid3->info['avdataend'].') is unexpectedly less-than-or-equal-to "avdataoffset" ('.$getid3->info['avdataoffset'].')');
    1.57 +        }
    1.58 +
    1.59 +        $getid3->info['fileformat'] = 'mpeg';
    1.60 +        fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
    1.61 +        $mpeg_stream_data        = fread($getid3->fp, min(100000, $getid3->info['avdataend'] - $getid3->info['avdataoffset']));
    1.62 +        $mpeg_stream_data_length = strlen($mpeg_stream_data);
    1.63 +
    1.64 +        $video_chunk_offset = 0;
    1.65 +        while (substr($mpeg_stream_data, $video_chunk_offset++, 4) !== getid3_mpeg::VIDEO_SEQUENCE_HEADER) {
    1.66 +            if ($video_chunk_offset >= $mpeg_stream_data_length) {
    1.67 +                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?');
    1.68 +            }
    1.69 +        }
    1.70 +
    1.71 +        // Start code                       32 bits
    1.72 +        // horizontal frame size            12 bits
    1.73 +        // vertical frame size              12 bits
    1.74 +        // pixel aspect ratio                4 bits
    1.75 +        // frame rate                        4 bits
    1.76 +        // bitrate                          18 bits
    1.77 +        // marker bit                        1 bit
    1.78 +        // VBV buffer size                  10 bits
    1.79 +        // constrained parameter flag        1 bit
    1.80 +        // intra quant. matrix flag          1 bit
    1.81 +        // intra quant. matrix values      512 bits (present if matrix flag == 1)
    1.82 +        // non-intra quant. matrix flag      1 bit
    1.83 +        // non-intra quant. matrix values  512 bits (present if matrix flag == 1)
    1.84 +
    1.85 +        $info_video['dataformat'] = 'mpeg';
    1.86 +
    1.87 +        $video_chunk_offset += (strlen(getid3_mpeg::VIDEO_SEQUENCE_HEADER) - 1);
    1.88 +
    1.89 +        $frame_size_dword = getid3_lib::BigEndian2Int(substr($mpeg_stream_data, $video_chunk_offset, 3));
    1.90 +        $video_chunk_offset += 3;
    1.91 +
    1.92 +        $aspect_ratio_frame_rate_dword = getid3_lib::BigEndian2Int(substr($mpeg_stream_data, $video_chunk_offset, 1));
    1.93 +        $video_chunk_offset += 1;
    1.94 +
    1.95 +        $assorted_information = getid3_lib::BigEndian2Bin(substr($mpeg_stream_data, $video_chunk_offset, 4));
    1.96 +        $video_chunk_offset += 4;
    1.97 +
    1.98 +        $info_mpeg_video_raw['framesize_horizontal']   = ($frame_size_dword & 0xFFF000) >> 12; // 12 bits for horizontal frame size
    1.99 +        $info_mpeg_video_raw['framesize_vertical']     = ($frame_size_dword & 0x000FFF);       // 12 bits for vertical frame size
   1.100 +        $info_mpeg_video_raw['pixel_aspect_ratio']     = ($aspect_ratio_frame_rate_dword & 0xF0) >> 4;
   1.101 +        $info_mpeg_video_raw['frame_rate']             = ($aspect_ratio_frame_rate_dword & 0x0F);
   1.102 +                                                                        
   1.103 +        $info_mpeg_video['framesize_horizontal']          = $info_mpeg_video_raw['framesize_horizontal'];
   1.104 +        $info_mpeg_video['framesize_vertical']            = $info_mpeg_video_raw['framesize_vertical'];
   1.105 +
   1.106 +        $info_mpeg_video['pixel_aspect_ratio']            = $this->MPEGvideoAspectRatioLookup($info_mpeg_video_raw['pixel_aspect_ratio']);
   1.107 +        $info_mpeg_video['pixel_aspect_ratio_text']       = $this->MPEGvideoAspectRatioTextLookup($info_mpeg_video_raw['pixel_aspect_ratio']);
   1.108 +        $info_mpeg_video['frame_rate']                    = $this->MPEGvideoFramerateLookup($info_mpeg_video_raw['frame_rate']);
   1.109 +
   1.110 +        $info_mpeg_video_raw['bitrate']                =       bindec(substr($assorted_information,  0, 18));
   1.111 +        $info_mpeg_video_raw['marker_bit']             = (bool)bindec($assorted_information{18});
   1.112 +        $info_mpeg_video_raw['vbv_buffer_size']        =       bindec(substr($assorted_information, 19, 10));
   1.113 +        $info_mpeg_video_raw['constrained_param_flag'] = (bool)bindec($assorted_information{29});
   1.114 +        $info_mpeg_video_raw['intra_quant_flag']       = (bool)bindec($assorted_information{30});
   1.115 +        
   1.116 +        if ($info_mpeg_video_raw['intra_quant_flag']) {
   1.117 +
   1.118 +            // read 512 bits
   1.119 +            $info_mpeg_video_raw['intra_quant']          = getid3_lib::BigEndian2Bin(substr($mpeg_stream_data, $video_chunk_offset, 64));
   1.120 +            $video_chunk_offset += 64;
   1.121 +
   1.122 +            $info_mpeg_video_raw['non_intra_quant_flag'] = (bool)bindec($info_mpeg_video_raw['intra_quant']{511});
   1.123 +            $info_mpeg_video_raw['intra_quant']          =       bindec($assorted_information{31}).substr(getid3_lib::BigEndian2Bin(substr($mpeg_stream_data, $video_chunk_offset, 64)), 0, 511);
   1.124 +
   1.125 +            if ($info_mpeg_video_raw['non_intra_quant_flag']) {
   1.126 +                $info_mpeg_video_raw['non_intra_quant'] = substr($mpeg_stream_data, $video_chunk_offset, 64);
   1.127 +                $video_chunk_offset += 64;
   1.128 +            }
   1.129 +
   1.130 +        } else {
   1.131 +
   1.132 +            $info_mpeg_video_raw['non_intra_quant_flag'] = (bool)bindec($assorted_information{31});
   1.133 +            if ($info_mpeg_video_raw['non_intra_quant_flag']) {
   1.134 +                $info_mpeg_video_raw['non_intra_quant'] = substr($mpeg_stream_data, $video_chunk_offset, 64);
   1.135 +                $video_chunk_offset += 64;
   1.136 +            }
   1.137 +        }
   1.138 +
   1.139 +        if ($info_mpeg_video_raw['bitrate'] == 0x3FFFF) { // 18 set bits
   1.140 +
   1.141 +            $getid3->warning('This version of getID3() cannot determine average bitrate of VBR MPEG video files');
   1.142 +            $info_mpeg_video['bitrate_mode'] = 'vbr';
   1.143 +
   1.144 +        } else {
   1.145 +
   1.146 +            $info_mpeg_video['bitrate']      = $info_mpeg_video_raw['bitrate'] * 400;
   1.147 +            $info_mpeg_video['bitrate_mode'] = 'cbr';
   1.148 +            $info_video['bitrate']              = $info_mpeg_video['bitrate'];
   1.149 +        }
   1.150 +
   1.151 +        $info_video['resolution_x']       = $info_mpeg_video['framesize_horizontal'];
   1.152 +        $info_video['resolution_y']       = $info_mpeg_video['framesize_vertical'];
   1.153 +        $info_video['frame_rate']         = $info_mpeg_video['frame_rate'];
   1.154 +        $info_video['bitrate_mode']       = $info_mpeg_video['bitrate_mode'];
   1.155 +        $info_video['pixel_aspect_ratio'] = $info_mpeg_video['pixel_aspect_ratio'];
   1.156 +        $info_video['lossless']           = false;
   1.157 +        $info_video['bits_per_sample']    = 24;
   1.158 +
   1.159 +
   1.160 +        //0x000001B3 begins the sequence_header of every MPEG video stream.
   1.161 +        //But in MPEG-2, this header must immediately be followed by an
   1.162 +        //extension_start_code (0x000001B5) with a sequence_extension ID (1).
   1.163 +        //(This extension contains all the additional MPEG-2 stuff.)
   1.164 +        //MPEG-1 doesn't have this extension, so that's a sure way to tell the
   1.165 +        //difference between MPEG-1 and MPEG-2 video streams.
   1.166 +
   1.167 +        $info_video['codec'] = substr($mpeg_stream_data, $video_chunk_offset, 4) == getid3_mpeg::VIDEO_EXTENSION_START ? 'MPEG-2' : 'MPEG-1';
   1.168 +
   1.169 +        $audio_chunk_offset = 0;
   1.170 +        while (true) {
   1.171 +            while (substr($mpeg_stream_data, $audio_chunk_offset++, 4) !== getid3_mpeg::AUDIO_START) {
   1.172 +                if ($audio_chunk_offset >= $mpeg_stream_data_length) {
   1.173 +                    break 2;
   1.174 +                }
   1.175 +            }
   1.176 +
   1.177 +            for ($i = 0; $i <= 7; $i++) {
   1.178 +                // 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
   1.179 +                // I have no idea why or what the difference is, so this is a stupid hack.
   1.180 +                // If anybody has any better idea of what's going on, please let me know - info@getid3.org
   1.181 +
   1.182 +                // make copy of info
   1.183 +                $dummy = $getid3->info;
   1.184 +
   1.185 +                // clone getid3 - better safe than sorry
   1.186 +                $clone = clone $this->getid3;
   1.187 +                
   1.188 +                // check
   1.189 +                $mp3 = new getid3_mp3($clone);
   1.190 +                if ($mp3->decodeMPEGaudioHeader($getid3->fp, ($audio_chunk_offset + 3) + 8 + $i, $dummy, false)) {
   1.191 +
   1.192 +                    $getid3->info = $dummy;
   1.193 +                    $getid3->info['audio']['bitrate_mode'] = 'cbr';
   1.194 +                    $getid3->info['audio']['lossless']     = false;
   1.195 +                    break 2;
   1.196 +                }
   1.197 +                
   1.198 +                // destroy copy
   1.199 +                unset($dummy);
   1.200 +            }
   1.201 +        }
   1.202 +
   1.203 +        // Temporary hack to account for interleaving overhead:
   1.204 +        if (!empty($info_video['bitrate']) && !empty($getid3->info['audio']['bitrate'])) {
   1.205 +            $getid3->info['playtime_seconds'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / ($info_video['bitrate'] + $getid3->info['audio']['bitrate']);
   1.206 +
   1.207 +            // Interleaved MPEG audio/video files have a certain amount of overhead that varies
   1.208 +            // by both video and audio bitrates, and not in any sensible, linear/logarithmic patter
   1.209 +            // Use interpolated lookup tables to approximately guess how much is overhead, because
   1.210 +            // playtime is calculated as filesize / total-bitrate
   1.211 +            $getid3->info['playtime_seconds'] *= $this->MPEGsystemNonOverheadPercentage($info_video['bitrate'], $getid3->info['audio']['bitrate']);
   1.212 +
   1.213 +            //switch ($info_video['bitrate']) {
   1.214 +            //    case('5000000'):
   1.215 +            //        $multiplier = 0.93292642112380355828048824319889;
   1.216 +            //        break;
   1.217 +            //    case('5500000'):
   1.218 +            //        $multiplier = 0.93582895375200989965359777343219;
   1.219 +            //        break;
   1.220 +            //    case('6000000'):
   1.221 +            //        $multiplier = 0.93796247714820932532911373859139;
   1.222 +            //        break;
   1.223 +            //    case('7000000'):
   1.224 +            //        $multiplier = 0.9413264083635103463010117778776;
   1.225 +            //        break;
   1.226 +            //    default:
   1.227 +            //        $multiplier = 1;
   1.228 +            //        break;
   1.229 +            //}
   1.230 +            //$getid3->info['playtime_seconds'] *= $multiplier;
   1.231 +            //$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.');
   1.232 +        
   1.233 +            if ($info_video['bitrate'] < 50000) {
   1.234 +                $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.');
   1.235 +            }
   1.236 +        }
   1.237 +
   1.238 +        return true;
   1.239 +    }
   1.240 +
   1.241 +
   1.242 +
   1.243 +    public static function MPEGsystemNonOverheadPercentage($video_bitrate, $audio_bitrate) {
   1.244 +        
   1.245 +        $overhead_percentage = 0;
   1.246 +
   1.247 +        $audio_bitrate = max(min($audio_bitrate / 1000,   384), 32); // limit to range of 32kbps - 384kbps (should be only legal bitrates, but maybe VBR?)
   1.248 +        $video_bitrate = max(min($video_bitrate / 1000, 10000), 10); // limit to range of 10kbps -  10Mbps (beyond that curves flatten anyways, no big loss)
   1.249 +
   1.250 +        //OMBB[audiobitrate]                 = array (   video-10kbps,       video-100kbps,      video-1000kbps,     video-10000kbps)
   1.251 +        static $overhead_multiplier_by_bitrate = array (
   1.252 +             32 => array (0, 0.9676287944368530, 0.9802276264360310, 0.9844916183244460, 0.9852821845179940),
   1.253 +             48 => array (0, 0.9779100089209830, 0.9787770035359320, 0.9846738664076130, 0.9852683013799960),
   1.254 +             56 => array (0, 0.9731249855367600, 0.9776624308938040, 0.9832606361852130, 0.9843922606633340),
   1.255 +             64 => array (0, 0.9755642683275760, 0.9795256705493390, 0.9836573009193170, 0.9851122539404470),
   1.256 +             96 => array (0, 0.9788025247497290, 0.9798553314148700, 0.9822956869792560, 0.9834815119124690),
   1.257 +            128 => array (0, 0.9816940050925480, 0.9821675936072120, 0.9829756927470870, 0.9839763420152050),
   1.258 +            160 => array (0, 0.9825894094561180, 0.9820913399073960, 0.9823907143253970, 0.9832821783651570),
   1.259 +            192 => array (0, 0.9832038474336260, 0.9825731694317960, 0.9821028622712400, 0.9828262076447620),
   1.260 +            224 => array (0, 0.9836516298538770, 0.9824718601823890, 0.9818302180625380, 0.9823735101626480),
   1.261 +            256 => array (0, 0.9845863022094920, 0.9837229411967540, 0.9824521662210830, 0.9828645172100790),
   1.262 +            320 => array (0, 0.9849565280263180, 0.9837683142805110, 0.9822885275960400, 0.9824424382727190),
   1.263 +            384 => array (0, 0.9856094774357600, 0.9844573394432720, 0.9825970399837330, 0.9824673808303890)
   1.264 +        );
   1.265 +
   1.266 +        $bitrate_to_use_min = $bitrate_to_use_max = $previous_bitrate = 32;
   1.267 +
   1.268 +        foreach ($overhead_multiplier_by_bitrate as $key => $value) {
   1.269 +        
   1.270 +            if ($audio_bitrate >= $previous_bitrate) {
   1.271 +                $bitrate_to_use_min = $previous_bitrate;
   1.272 +            }
   1.273 +            if ($audio_bitrate < $key) {
   1.274 +                $bitrate_to_use_max = $key;
   1.275 +                break;
   1.276 +            }
   1.277 +            $previous_bitrate = $key;
   1.278 +        }
   1.279 +        
   1.280 +        $factor_a = ($bitrate_to_use_max - $audio_bitrate) / ($bitrate_to_use_max - $bitrate_to_use_min);
   1.281 +
   1.282 +        $video_bitrate_log10 = log10($video_bitrate);
   1.283 +        $video_factor_min1   = $overhead_multiplier_by_bitrate[$bitrate_to_use_min][floor($video_bitrate_log10)];
   1.284 +        $video_factor_min2   = $overhead_multiplier_by_bitrate[$bitrate_to_use_max][floor($video_bitrate_log10)];
   1.285 +        $video_factor_max1   = $overhead_multiplier_by_bitrate[$bitrate_to_use_min][ceil($video_bitrate_log10)];
   1.286 +        $video_factor_max2   = $overhead_multiplier_by_bitrate[$bitrate_to_use_max][ceil($video_bitrate_log10)];
   1.287 +        $factor_v = $video_bitrate_log10 - floor($video_bitrate_log10);
   1.288 +
   1.289 +        $overhead_percentage  = $video_factor_min1 *      $factor_a  *      $factor_v;
   1.290 +        $overhead_percentage += $video_factor_min2 * (1 - $factor_a) *      $factor_v;
   1.291 +        $overhead_percentage += $video_factor_max1 *      $factor_a  * (1 - $factor_v);
   1.292 +        $overhead_percentage += $video_factor_max2 * (1 - $factor_a) * (1 - $factor_v);
   1.293 +
   1.294 +        return $overhead_percentage;
   1.295 +    }
   1.296 +
   1.297 +
   1.298 +
   1.299 +    public static function MPEGvideoFramerateLookup($raw_frame_rate) {
   1.300 +        
   1.301 +        $lookup = array (0, 23.976, 24, 25, 29.97, 30, 50, 59.94, 60);
   1.302 +        
   1.303 +        return (float)(isset($lookup[$raw_frame_rate]) ? $lookup[$raw_frame_rate] : 0);
   1.304 +    }
   1.305 +
   1.306 +
   1.307 +
   1.308 +    public static function MPEGvideoAspectRatioLookup($raw_aspect_ratio) {
   1.309 +        
   1.310 +        $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);
   1.311 +        
   1.312 +        return (float)(isset($lookup[$raw_aspect_ratio]) ? $lookup[$raw_aspect_ratio] : 0);
   1.313 +    }
   1.314 +
   1.315 +
   1.316 +
   1.317 +    public static function MPEGvideoAspectRatioTextLookup($raw_aspect_ratio) {
   1.318 +        
   1.319 +        $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');
   1.320 +        
   1.321 +        return (isset($lookup[$raw_aspect_ratio]) ? $lookup[$raw_aspect_ratio] : '');
   1.322 +    }
   1.323 +
   1.324 +}
   1.325 +
   1.326 +
   1.327 +?>
   1.328 \ No newline at end of file