source: branches/MootoolsFileManager-Update/plugins/MootoolsFileManager/mootools-filemanager/Assets/Connector/Assets/getid3/module.audio-video.mpeg.php @ 1300

Last change on this file since 1300 was 1300, checked in by gogo, 8 years ago

Update the MootoolsFileManager? to the latest cpojer with some modifications.
Add a demo for the MFM examples/mootools-file-manager.php
Change the default config for ImageManager? and ExtendedFileManager? for added security.

File size: 15.4 KB
Line 
1<?php
2/////////////////////////////////////////////////////////////////
3/// getID3() by James Heinrich <info@getid3.org>               //
4//  available at http://getid3.sourceforge.net                 //
5//            or http://www.getid3.org                         //
6/////////////////////////////////////////////////////////////////
7// See readme.txt for more details                             //
8/////////////////////////////////////////////////////////////////
9//                                                             //
10// module.audio-video.mpeg.php                                 //
11// module for analyzing MPEG files                             //
12// dependencies: module.audio.mp3.php                          //
13//                                                            ///
14/////////////////////////////////////////////////////////////////
15
16getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.mp3.php', __FILE__, true);
17
18define('GETID3_MPEG_VIDEO_PICTURE_START',   "\x00\x00\x01\x00");
19define('GETID3_MPEG_VIDEO_USER_DATA_START', "\x00\x00\x01\xB2");
20define('GETID3_MPEG_VIDEO_SEQUENCE_HEADER', "\x00\x00\x01\xB3");
21define('GETID3_MPEG_VIDEO_SEQUENCE_ERROR',  "\x00\x00\x01\xB4");
22define('GETID3_MPEG_VIDEO_EXTENSION_START', "\x00\x00\x01\xB5");
23define('GETID3_MPEG_VIDEO_SEQUENCE_END',    "\x00\x00\x01\xB7");
24define('GETID3_MPEG_VIDEO_GROUP_START',     "\x00\x00\x01\xB8");
25define('GETID3_MPEG_AUDIO_START',           "\x00\x00\x01\xC0");
26
27
28class getid3_mpeg
29{
30
31        function getid3_mpeg(&$fd, &$ThisFileInfo) {
32                if ($ThisFileInfo['avdataend'] <= $ThisFileInfo['avdataoffset']) {
33                        $ThisFileInfo['error'][] = '"avdataend" ('.$ThisFileInfo['avdataend'].') is unexpectedly less-than-or-equal-to "avdataoffset" ('.$ThisFileInfo['avdataoffset'].')';
34                        return false;
35                }
36                $ThisFileInfo['fileformat'] = 'mpeg';
37                fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
38                $MPEGstreamData       = fread($fd, min(100000, $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']));
39                $MPEGstreamDataLength = strlen($MPEGstreamData);
40
41                $foundVideo = true;
42                $VideoChunkOffset = 0;
43                while (substr($MPEGstreamData, $VideoChunkOffset++, 4) !== GETID3_MPEG_VIDEO_SEQUENCE_HEADER) {
44                        if ($VideoChunkOffset >= $MPEGstreamDataLength) {
45                                $foundVideo = false;
46                                break;
47                        }
48                }
49                if ($foundVideo) {
50
51                        // Start code                       32 bits
52                        // horizontal frame size            12 bits
53                        // vertical frame size              12 bits
54                        // pixel aspect ratio                4 bits
55                        // frame rate                        4 bits
56                        // bitrate                          18 bits
57                        // marker bit                        1 bit
58                        // VBV buffer size                  10 bits
59                        // constrained parameter flag        1 bit
60                        // intra quant. matrix flag          1 bit
61                        // intra quant. matrix values      512 bits (present if matrix flag == 1)
62                        // non-intra quant. matrix flag      1 bit
63                        // non-intra quant. matrix values  512 bits (present if matrix flag == 1)
64
65                        $ThisFileInfo['video']['dataformat'] = 'mpeg';
66
67                        $VideoChunkOffset += (strlen(GETID3_MPEG_VIDEO_SEQUENCE_HEADER) - 1);
68
69                        $FrameSizeDWORD = getid3_lib::BigEndian2Int(substr($MPEGstreamData, $VideoChunkOffset, 3));
70                        $VideoChunkOffset += 3;
71
72                        $AspectRatioFrameRateDWORD = getid3_lib::BigEndian2Int(substr($MPEGstreamData, $VideoChunkOffset, 1));
73                        $VideoChunkOffset += 1;
74
75                        $assortedinformation = getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $VideoChunkOffset, 4));
76                        $VideoChunkOffset += 4;
77
78                        $ThisFileInfo['mpeg']['video']['raw']['framesize_horizontal'] = ($FrameSizeDWORD & 0xFFF000) >> 12; // 12 bits for horizontal frame size
79                        $ThisFileInfo['mpeg']['video']['raw']['framesize_vertical']   = ($FrameSizeDWORD & 0x000FFF);       // 12 bits for vertical frame size
80                        $ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']   = ($AspectRatioFrameRateDWORD & 0xF0) >> 4;
81                        $ThisFileInfo['mpeg']['video']['raw']['frame_rate']           = ($AspectRatioFrameRateDWORD & 0x0F);
82
83                        $ThisFileInfo['mpeg']['video']['framesize_horizontal'] = $ThisFileInfo['mpeg']['video']['raw']['framesize_horizontal'];
84                        $ThisFileInfo['mpeg']['video']['framesize_vertical']   = $ThisFileInfo['mpeg']['video']['raw']['framesize_vertical'];
85
86                        $ThisFileInfo['mpeg']['video']['pixel_aspect_ratio']      = $this->MPEGvideoAspectRatioLookup($ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']);
87                        $ThisFileInfo['mpeg']['video']['pixel_aspect_ratio_text'] = $this->MPEGvideoAspectRatioTextLookup($ThisFileInfo['mpeg']['video']['raw']['pixel_aspect_ratio']);
88                        $ThisFileInfo['mpeg']['video']['frame_rate']              = $this->MPEGvideoFramerateLookup($ThisFileInfo['mpeg']['video']['raw']['frame_rate']);
89
90                        $ThisFileInfo['mpeg']['video']['raw']['bitrate']                =        getid3_lib::Bin2Dec(substr($assortedinformation,  0, 18));
91                        $ThisFileInfo['mpeg']['video']['raw']['marker_bit']             = (bool) getid3_lib::Bin2Dec(substr($assortedinformation, 18,  1));
92                        $ThisFileInfo['mpeg']['video']['raw']['vbv_buffer_size']        =        getid3_lib::Bin2Dec(substr($assortedinformation, 19, 10));
93                        $ThisFileInfo['mpeg']['video']['raw']['constrained_param_flag'] = (bool) getid3_lib::Bin2Dec(substr($assortedinformation, 29,  1));
94                        $ThisFileInfo['mpeg']['video']['raw']['intra_quant_flag']       = (bool) getid3_lib::Bin2Dec(substr($assortedinformation, 30,  1));
95                        if ($ThisFileInfo['mpeg']['video']['raw']['intra_quant_flag']) {
96
97                                // read 512 bits
98                                $ThisFileInfo['mpeg']['video']['raw']['intra_quant']          = getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $VideoChunkOffset, 64));
99                                $VideoChunkOffset += 64;
100
101                                $ThisFileInfo['mpeg']['video']['raw']['non_intra_quant_flag'] = (bool) getid3_lib::Bin2Dec(substr($ThisFileInfo['mpeg']['video']['raw']['intra_quant'], 511,  1));
102                                $ThisFileInfo['mpeg']['video']['raw']['intra_quant']          =        getid3_lib::Bin2Dec(substr($assortedinformation, 31,  1)).substr(getid3_lib::BigEndian2Bin(substr($MPEGstreamData, $VideoChunkOffset, 64)), 0, 511);
103
104                                if ($ThisFileInfo['mpeg']['video']['raw']['non_intra_quant_flag']) {
105                                        $ThisFileInfo['mpeg']['video']['raw']['non_intra_quant'] = substr($MPEGstreamData, $VideoChunkOffset, 64);
106                                        $VideoChunkOffset += 64;
107                                }
108
109                        } else {
110
111                                $ThisFileInfo['mpeg']['video']['raw']['non_intra_quant_flag'] = (bool) getid3_lib::Bin2Dec(substr($assortedinformation, 31,  1));
112                                if ($ThisFileInfo['mpeg']['video']['raw']['non_intra_quant_flag']) {
113                                        $ThisFileInfo['mpeg']['video']['raw']['non_intra_quant'] = substr($MPEGstreamData, $VideoChunkOffset, 64);
114                                        $VideoChunkOffset += 64;
115                                }
116
117                        }
118
119                        if ($ThisFileInfo['mpeg']['video']['raw']['bitrate'] == 0x3FFFF) { // 18 set bits
120
121                                $ThisFileInfo['warning'][] = 'This version of getID3() ['.GETID3_VERSION.'] cannot determine average bitrate of VBR MPEG video files';
122                                $ThisFileInfo['mpeg']['video']['bitrate_mode'] = 'vbr';
123
124                        } else {
125
126                                $ThisFileInfo['mpeg']['video']['bitrate']      = $ThisFileInfo['mpeg']['video']['raw']['bitrate'] * 400;
127                                $ThisFileInfo['mpeg']['video']['bitrate_mode'] = 'cbr';
128                                $ThisFileInfo['video']['bitrate']              = $ThisFileInfo['mpeg']['video']['bitrate'];
129
130                        }
131
132                        $ThisFileInfo['video']['resolution_x']       = $ThisFileInfo['mpeg']['video']['framesize_horizontal'];
133                        $ThisFileInfo['video']['resolution_y']       = $ThisFileInfo['mpeg']['video']['framesize_vertical'];
134                        $ThisFileInfo['video']['frame_rate']         = $ThisFileInfo['mpeg']['video']['frame_rate'];
135                        $ThisFileInfo['video']['bitrate_mode']       = $ThisFileInfo['mpeg']['video']['bitrate_mode'];
136                        $ThisFileInfo['video']['pixel_aspect_ratio'] = $ThisFileInfo['mpeg']['video']['pixel_aspect_ratio'];
137                        $ThisFileInfo['video']['lossless']           = false;
138                        $ThisFileInfo['video']['bits_per_sample']    = 24;
139
140                } else {
141
142                        $ThisFileInfo['error'][] = '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?';
143
144                }
145
146                //0x000001B3 begins the sequence_header of every MPEG video stream.
147                //But in MPEG-2, this header must immediately be followed by an
148                //extension_start_code (0x000001B5) with a sequence_extension ID (1).
149                //(This extension contains all the additional MPEG-2 stuff.)
150                //MPEG-1 doesn't have this extension, so that's a sure way to tell the
151                //difference between MPEG-1 and MPEG-2 video streams.
152
153                if (substr($MPEGstreamData, $VideoChunkOffset, 4) == GETID3_MPEG_VIDEO_EXTENSION_START) {
154                        $ThisFileInfo['video']['codec'] = 'MPEG-2';
155                } else {
156                        $ThisFileInfo['video']['codec'] = 'MPEG-1';
157                }
158
159
160                $AudioChunkOffset = 0;
161                while (true) {
162                        while (substr($MPEGstreamData, $AudioChunkOffset++, 4) !== GETID3_MPEG_AUDIO_START) {
163                                if ($AudioChunkOffset >= $MPEGstreamDataLength) {
164                                        break 2;
165                                }
166                        }
167
168                        for ($i = 0; $i <= 7; $i++) {
169                                // 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
170                                // I have no idea why or what the difference is, so this is a stupid hack.
171                                // If anybody has any better idea of what's going on, please let me know - info@getid3.org
172
173                                $dummy = $ThisFileInfo;
174                                if (getid3_mp3::decodeMPEGaudioHeader($fd, ($AudioChunkOffset + 3) + 8 + $i, $dummy, false)) {
175                                        $ThisFileInfo = $dummy;
176                                        $ThisFileInfo['audio']['bitrate_mode']    = 'cbr';
177                                        $ThisFileInfo['audio']['lossless']        = false;
178                                        break 2;
179
180                                }
181                        }
182                }
183
184                // Temporary hack to account for interleaving overhead:
185                if (!empty($ThisFileInfo['video']['bitrate']) && !empty($ThisFileInfo['audio']['bitrate'])) {
186                        $ThisFileInfo['playtime_seconds'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / ($ThisFileInfo['video']['bitrate'] + $ThisFileInfo['audio']['bitrate']);
187
188                        // Interleaved MPEG audio/video files have a certain amount of overhead that varies
189                        // by both video and audio bitrates, and not in any sensible, linear/logarithmic patter
190                        // Use interpolated lookup tables to approximately guess how much is overhead, because
191                        // playtime is calculated as filesize / total-bitrate
192                        $ThisFileInfo['playtime_seconds'] *= $this->MPEGsystemNonOverheadPercentage($ThisFileInfo['video']['bitrate'], $ThisFileInfo['audio']['bitrate']);
193
194                        //switch ($ThisFileInfo['video']['bitrate']) {
195                        //      case('5000000'):
196                        //              $multiplier = 0.93292642112380355828048824319889;
197                        //              break;
198                        //      case('5500000'):
199                        //              $multiplier = 0.93582895375200989965359777343219;
200                        //              break;
201                        //      case('6000000'):
202                        //              $multiplier = 0.93796247714820932532911373859139;
203                        //              break;
204                        //      case('7000000'):
205                        //              $multiplier = 0.9413264083635103463010117778776;
206                        //              break;
207                        //      default:
208                        //              $multiplier = 1;
209                        //              break;
210                        //}
211                        //$ThisFileInfo['playtime_seconds'] *= $multiplier;
212                        //$ThisFileInfo['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.';
213                        if ($ThisFileInfo['video']['bitrate'] < 50000) {
214                                $ThisFileInfo['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.';
215                        }
216                }
217
218                return true;
219        }
220
221
222        function MPEGsystemNonOverheadPercentage($VideoBitrate, $AudioBitrate) {
223                $OverheadPercentage = 0;
224
225                $AudioBitrate = max(min($AudioBitrate / 1000,   384), 32); // limit to range of 32kbps - 384kbps (should be only legal bitrates, but maybe VBR?)
226                $VideoBitrate = max(min($VideoBitrate / 1000, 10000), 10); // limit to range of 10kbps -  10Mbps (beyond that curves flatten anyways, no big loss)
227
228
229                //OMBB[audiobitrate]              = array(video-10kbps,       video-100kbps,      video-1000kbps,     video-10000kbps)
230                $OverheadMultiplierByBitrate[32]  = array(0, 0.9676287944368530, 0.9802276264360310, 0.9844916183244460, 0.9852821845179940);
231                $OverheadMultiplierByBitrate[48]  = array(0, 0.9779100089209830, 0.9787770035359320, 0.9846738664076130, 0.9852683013799960);
232                $OverheadMultiplierByBitrate[56]  = array(0, 0.9731249855367600, 0.9776624308938040, 0.9832606361852130, 0.9843922606633340);
233                $OverheadMultiplierByBitrate[64]  = array(0, 0.9755642683275760, 0.9795256705493390, 0.9836573009193170, 0.9851122539404470);
234                $OverheadMultiplierByBitrate[96]  = array(0, 0.9788025247497290, 0.9798553314148700, 0.9822956869792560, 0.9834815119124690);
235                $OverheadMultiplierByBitrate[128] = array(0, 0.9816940050925480, 0.9821675936072120, 0.9829756927470870, 0.9839763420152050);
236                $OverheadMultiplierByBitrate[160] = array(0, 0.9825894094561180, 0.9820913399073960, 0.9823907143253970, 0.9832821783651570);
237                $OverheadMultiplierByBitrate[192] = array(0, 0.9832038474336260, 0.9825731694317960, 0.9821028622712400, 0.9828262076447620);
238                $OverheadMultiplierByBitrate[224] = array(0, 0.9836516298538770, 0.9824718601823890, 0.9818302180625380, 0.9823735101626480);
239                $OverheadMultiplierByBitrate[256] = array(0, 0.9845863022094920, 0.9837229411967540, 0.9824521662210830, 0.9828645172100790);
240                $OverheadMultiplierByBitrate[320] = array(0, 0.9849565280263180, 0.9837683142805110, 0.9822885275960400, 0.9824424382727190);
241                $OverheadMultiplierByBitrate[384] = array(0, 0.9856094774357600, 0.9844573394432720, 0.9825970399837330, 0.9824673808303890);
242
243                $BitrateToUseMin = 32;
244                $BitrateToUseMax = 32;
245                $previousBitrate = 32;
246                foreach ($OverheadMultiplierByBitrate as $key => $value) {
247                        if ($AudioBitrate >= $previousBitrate) {
248                                $BitrateToUseMin = $previousBitrate;
249                        }
250                        if ($AudioBitrate < $key) {
251                                $BitrateToUseMax = $key;
252                                break;
253                        }
254                        $previousBitrate = $key;
255                }
256                $FactorA = ($BitrateToUseMax - $AudioBitrate) / ($BitrateToUseMax - $BitrateToUseMin);
257
258                $VideoBitrateLog10 = log10($VideoBitrate);
259                $VideoFactorMin1 = $OverheadMultiplierByBitrate[$BitrateToUseMin][floor($VideoBitrateLog10)];
260                $VideoFactorMin2 = $OverheadMultiplierByBitrate[$BitrateToUseMax][floor($VideoBitrateLog10)];
261                $VideoFactorMax1 = $OverheadMultiplierByBitrate[$BitrateToUseMin][ceil($VideoBitrateLog10)];
262                $VideoFactorMax2 = $OverheadMultiplierByBitrate[$BitrateToUseMax][ceil($VideoBitrateLog10)];
263                $FactorV = $VideoBitrateLog10 - floor($VideoBitrateLog10);
264
265                $OverheadPercentage  = $VideoFactorMin1 *      $FactorA  *      $FactorV;
266                $OverheadPercentage += $VideoFactorMin2 * (1 - $FactorA) *      $FactorV;
267                $OverheadPercentage += $VideoFactorMax1 *      $FactorA  * (1 - $FactorV);
268                $OverheadPercentage += $VideoFactorMax2 * (1 - $FactorA) * (1 - $FactorV);
269
270                return $OverheadPercentage;
271        }
272
273
274        function MPEGvideoFramerateLookup($rawframerate) {
275                $MPEGvideoFramerateLookup = array(0, 23.976, 24, 25, 29.97, 30, 50, 59.94, 60);
276                return (isset($MPEGvideoFramerateLookup[$rawframerate]) ? (float) $MPEGvideoFramerateLookup[$rawframerate] : (float) 0);
277        }
278
279        function MPEGvideoAspectRatioLookup($rawaspectratio) {
280                $MPEGvideoAspectRatioLookup = 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);
281                return (isset($MPEGvideoAspectRatioLookup[$rawaspectratio]) ? (float) $MPEGvideoAspectRatioLookup[$rawaspectratio] : (float) 0);
282        }
283
284        function MPEGvideoAspectRatioTextLookup($rawaspectratio) {
285                $MPEGvideoAspectRatioTextLookup = 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');
286                return (isset($MPEGvideoAspectRatioTextLookup[$rawaspectratio]) ? $MPEGvideoAspectRatioTextLookup[$rawaspectratio] : '');
287        }
288
289}
290
291
292?>
Note: See TracBrowser for help on using the repository browser.