module.audio-video.mpeg.php 15 KB

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