module.audio-video.mpeg.php 15 KB

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