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