PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/getid3/module.audio-video.mpeg.php

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