module.audio-video.flv.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio-video.flv.php //
  11. // module for analyzing Shockwave Flash Video files //
  12. // dependencies: NONE //
  13. // //
  14. /////////////////////////////////////////////////////////////////
  15. // //
  16. // FLV module by Seth Kaufman <sethØwhirl-i-gig*com> //
  17. // //
  18. // * version 0.1 (26 June 2005) //
  19. // //
  20. // * version 0.1.1 (15 July 2005) //
  21. // minor modifications by James Heinrich <info@getid3.org> //
  22. // //
  23. // * version 0.2 (22 February 2006) //
  24. // Support for On2 VP6 codec and meta information //
  25. // by Steve Webster <steve.websterØfeaturecreep*com> //
  26. // //
  27. // * version 0.3 (15 June 2006) //
  28. // Modified to not read entire file into memory //
  29. // by James Heinrich <info@getid3.org> //
  30. // //
  31. // * version 0.4 (07 December 2007) //
  32. // Bugfixes for incorrectly parsed FLV dimensions //
  33. // and incorrect parsing of onMetaTag //
  34. // by Evgeny Moysevich <moysevichØgmail*com> //
  35. // //
  36. // * version 0.5 (21 May 2009) //
  37. // Fixed parsing of audio tags and added additional codec //
  38. // details. The duration is now read from onMetaTag (if //
  39. // exists), rather than parsing whole file //
  40. // by Nigel Barnes <ngbarnesØhotmail*com> //
  41. // //
  42. // * version 0.6 (24 May 2009) //
  43. // Better parsing of files with h264 video //
  44. // by Evgeny Moysevich <moysevichØgmail*com> //
  45. // //
  46. // * version 0.6.1 (30 May 2011) //
  47. // prevent infinite loops in expGolombUe() //
  48. // //
  49. // * version 0.7.0 (16 Jul 2013) //
  50. // handle GETID3_FLV_VIDEO_VP6FLV_ALPHA //
  51. // improved AVCSequenceParameterSetReader::readData() //
  52. // by Xander Schouwerwou <schouwerwouØgmail*com> //
  53. // ///
  54. /////////////////////////////////////////////////////////////////
  55. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  56. exit;
  57. }
  58. define('GETID3_FLV_TAG_AUDIO', 8);
  59. define('GETID3_FLV_TAG_VIDEO', 9);
  60. define('GETID3_FLV_TAG_META', 18);
  61. define('GETID3_FLV_VIDEO_H263', 2);
  62. define('GETID3_FLV_VIDEO_SCREEN', 3);
  63. define('GETID3_FLV_VIDEO_VP6FLV', 4);
  64. define('GETID3_FLV_VIDEO_VP6FLV_ALPHA', 5);
  65. define('GETID3_FLV_VIDEO_SCREENV2', 6);
  66. define('GETID3_FLV_VIDEO_H264', 7);
  67. define('H264_AVC_SEQUENCE_HEADER', 0);
  68. define('H264_PROFILE_BASELINE', 66);
  69. define('H264_PROFILE_MAIN', 77);
  70. define('H264_PROFILE_EXTENDED', 88);
  71. define('H264_PROFILE_HIGH', 100);
  72. define('H264_PROFILE_HIGH10', 110);
  73. define('H264_PROFILE_HIGH422', 122);
  74. define('H264_PROFILE_HIGH444', 144);
  75. define('H264_PROFILE_HIGH444_PREDICTIVE', 244);
  76. class getid3_flv extends getid3_handler
  77. {
  78. const magic = 'FLV';
  79. /**
  80. * Break out of the loop if too many frames have been scanned; only scan this
  81. * many if meta frame does not contain useful duration.
  82. *
  83. * @var int
  84. */
  85. public $max_frames = 100000;
  86. /**
  87. * @return bool
  88. */
  89. public function Analyze() {
  90. $info = &$this->getid3->info;
  91. $this->fseek($info['avdataoffset']);
  92. $FLVdataLength = $info['avdataend'] - $info['avdataoffset'];
  93. $FLVheader = $this->fread(5);
  94. $info['fileformat'] = 'flv';
  95. $info['flv']['header']['signature'] = substr($FLVheader, 0, 3);
  96. $info['flv']['header']['version'] = getid3_lib::BigEndian2Int(substr($FLVheader, 3, 1));
  97. $TypeFlags = getid3_lib::BigEndian2Int(substr($FLVheader, 4, 1));
  98. if ($info['flv']['header']['signature'] != self::magic) {
  99. $this->error('Expecting "'.getid3_lib::PrintHexBytes(self::magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['flv']['header']['signature']).'"');
  100. unset($info['flv'], $info['fileformat']);
  101. return false;
  102. }
  103. $info['flv']['header']['hasAudio'] = (bool) ($TypeFlags & 0x04);
  104. $info['flv']['header']['hasVideo'] = (bool) ($TypeFlags & 0x01);
  105. $FrameSizeDataLength = getid3_lib::BigEndian2Int($this->fread(4));
  106. $FLVheaderFrameLength = 9;
  107. if ($FrameSizeDataLength > $FLVheaderFrameLength) {
  108. $this->fseek($FrameSizeDataLength - $FLVheaderFrameLength, SEEK_CUR);
  109. }
  110. $Duration = 0;
  111. $found_video = false;
  112. $found_audio = false;
  113. $found_meta = false;
  114. $found_valid_meta_playtime = false;
  115. $tagParseCount = 0;
  116. $info['flv']['framecount'] = array('total'=>0, 'audio'=>0, 'video'=>0);
  117. $flv_framecount = &$info['flv']['framecount'];
  118. while ((($this->ftell() + 16) < $info['avdataend']) && (($tagParseCount++ <= $this->max_frames) || !$found_valid_meta_playtime)) {
  119. $ThisTagHeader = $this->fread(16);
  120. $PreviousTagLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 0, 4));
  121. $TagType = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 4, 1));
  122. $DataLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 5, 3));
  123. $Timestamp = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 8, 3));
  124. $LastHeaderByte = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 15, 1));
  125. $NextOffset = $this->ftell() - 1 + $DataLength;
  126. if ($Timestamp > $Duration) {
  127. $Duration = $Timestamp;
  128. }
  129. $flv_framecount['total']++;
  130. switch ($TagType) {
  131. case GETID3_FLV_TAG_AUDIO:
  132. $flv_framecount['audio']++;
  133. if (!$found_audio) {
  134. $found_audio = true;
  135. $info['flv']['audio']['audioFormat'] = ($LastHeaderByte >> 4) & 0x0F;
  136. $info['flv']['audio']['audioRate'] = ($LastHeaderByte >> 2) & 0x03;
  137. $info['flv']['audio']['audioSampleSize'] = ($LastHeaderByte >> 1) & 0x01;
  138. $info['flv']['audio']['audioType'] = $LastHeaderByte & 0x01;
  139. }
  140. break;
  141. case GETID3_FLV_TAG_VIDEO:
  142. $flv_framecount['video']++;
  143. if (!$found_video) {
  144. $found_video = true;
  145. $info['flv']['video']['videoCodec'] = $LastHeaderByte & 0x07;
  146. $FLVvideoHeader = $this->fread(11);
  147. if ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H264) {
  148. // this code block contributed by: moysevichØgmail*com
  149. $AVCPacketType = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 0, 1));
  150. if ($AVCPacketType == H264_AVC_SEQUENCE_HEADER) {
  151. // read AVCDecoderConfigurationRecord
  152. $configurationVersion = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 1));
  153. $AVCProfileIndication = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 1));
  154. $profile_compatibility = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 1));
  155. $lengthSizeMinusOne = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 1));
  156. $numOfSequenceParameterSets = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 8, 1));
  157. if (($numOfSequenceParameterSets & 0x1F) != 0) {
  158. // there is at least one SequenceParameterSet
  159. // read size of the first SequenceParameterSet
  160. //$spsSize = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 9, 2));
  161. $spsSize = getid3_lib::LittleEndian2Int(substr($FLVvideoHeader, 9, 2));
  162. // read the first SequenceParameterSet
  163. $sps = $this->fread($spsSize);
  164. if (strlen($sps) == $spsSize) { // make sure that whole SequenceParameterSet was red
  165. $spsReader = new AVCSequenceParameterSetReader($sps);
  166. $spsReader->readData();
  167. $info['video']['resolution_x'] = $spsReader->getWidth();
  168. $info['video']['resolution_y'] = $spsReader->getHeight();
  169. }
  170. }
  171. }
  172. // end: moysevichØgmail*com
  173. } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H263) {
  174. $PictureSizeType = (getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 3, 2))) >> 7;
  175. $PictureSizeType = $PictureSizeType & 0x0007;
  176. $info['flv']['header']['videoSizeType'] = $PictureSizeType;
  177. switch ($PictureSizeType) {
  178. case 0:
  179. //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2));
  180. //$PictureSizeEnc <<= 1;
  181. //$info['video']['resolution_x'] = ($PictureSizeEnc & 0xFF00) >> 8;
  182. //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
  183. //$PictureSizeEnc <<= 1;
  184. //$info['video']['resolution_y'] = ($PictureSizeEnc & 0xFF00) >> 8;
  185. $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 2)) >> 7;
  186. $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2)) >> 7;
  187. $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFF;
  188. $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFF;
  189. break;
  190. case 1:
  191. $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 3)) >> 7;
  192. $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 3)) >> 7;
  193. $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFFFF;
  194. $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFFFF;
  195. break;
  196. case 2:
  197. $info['video']['resolution_x'] = 352;
  198. $info['video']['resolution_y'] = 288;
  199. break;
  200. case 3:
  201. $info['video']['resolution_x'] = 176;
  202. $info['video']['resolution_y'] = 144;
  203. break;
  204. case 4:
  205. $info['video']['resolution_x'] = 128;
  206. $info['video']['resolution_y'] = 96;
  207. break;
  208. case 5:
  209. $info['video']['resolution_x'] = 320;
  210. $info['video']['resolution_y'] = 240;
  211. break;
  212. case 6:
  213. $info['video']['resolution_x'] = 160;
  214. $info['video']['resolution_y'] = 120;
  215. break;
  216. default:
  217. $info['video']['resolution_x'] = 0;
  218. $info['video']['resolution_y'] = 0;
  219. break;
  220. }
  221. } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_VP6FLV_ALPHA) {
  222. /* contributed by schouwerwouØgmail*com */
  223. if (!isset($info['video']['resolution_x'])) { // only when meta data isn't set
  224. $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
  225. $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 2));
  226. $info['video']['resolution_x'] = ($PictureSizeEnc['x'] & 0xFF) << 3;
  227. $info['video']['resolution_y'] = ($PictureSizeEnc['y'] & 0xFF) << 3;
  228. }
  229. /* end schouwerwouØgmail*com */
  230. }
  231. if (!empty($info['video']['resolution_x']) && !empty($info['video']['resolution_y'])) {
  232. $info['video']['pixel_aspect_ratio'] = $info['video']['resolution_x'] / $info['video']['resolution_y'];
  233. }
  234. }
  235. break;
  236. // Meta tag
  237. case GETID3_FLV_TAG_META:
  238. if (!$found_meta) {
  239. $found_meta = true;
  240. $this->fseek(-1, SEEK_CUR);
  241. $datachunk = $this->fread($DataLength);
  242. $AMFstream = new AMFStream($datachunk);
  243. $reader = new AMFReader($AMFstream);
  244. $eventName = $reader->readData();
  245. $info['flv']['meta'][$eventName] = $reader->readData();
  246. unset($reader);
  247. $copykeys = array('framerate'=>'frame_rate', 'width'=>'resolution_x', 'height'=>'resolution_y', 'audiodatarate'=>'bitrate', 'videodatarate'=>'bitrate');
  248. foreach ($copykeys as $sourcekey => $destkey) {
  249. if (isset($info['flv']['meta']['onMetaData'][$sourcekey])) {
  250. switch ($sourcekey) {
  251. case 'width':
  252. case 'height':
  253. $info['video'][$destkey] = intval(round($info['flv']['meta']['onMetaData'][$sourcekey]));
  254. break;
  255. case 'audiodatarate':
  256. $info['audio'][$destkey] = getid3_lib::CastAsInt(round($info['flv']['meta']['onMetaData'][$sourcekey] * 1000));
  257. break;
  258. case 'videodatarate':
  259. case 'frame_rate':
  260. default:
  261. $info['video'][$destkey] = $info['flv']['meta']['onMetaData'][$sourcekey];
  262. break;
  263. }
  264. }
  265. }
  266. if (!empty($info['flv']['meta']['onMetaData']['duration'])) {
  267. $found_valid_meta_playtime = true;
  268. }
  269. }
  270. break;
  271. default:
  272. // noop
  273. break;
  274. }
  275. $this->fseek($NextOffset);
  276. }
  277. $info['playtime_seconds'] = $Duration / 1000;
  278. if ($info['playtime_seconds'] > 0) {
  279. $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  280. }
  281. if ($info['flv']['header']['hasAudio']) {
  282. $info['audio']['codec'] = self::audioFormatLookup($info['flv']['audio']['audioFormat']);
  283. $info['audio']['sample_rate'] = self::audioRateLookup($info['flv']['audio']['audioRate']);
  284. $info['audio']['bits_per_sample'] = self::audioBitDepthLookup($info['flv']['audio']['audioSampleSize']);
  285. $info['audio']['channels'] = $info['flv']['audio']['audioType'] + 1; // 0=mono,1=stereo
  286. $info['audio']['lossless'] = ($info['flv']['audio']['audioFormat'] ? false : true); // 0=uncompressed
  287. $info['audio']['dataformat'] = 'flv';
  288. }
  289. if (!empty($info['flv']['header']['hasVideo'])) {
  290. $info['video']['codec'] = self::videoCodecLookup($info['flv']['video']['videoCodec']);
  291. $info['video']['dataformat'] = 'flv';
  292. $info['video']['lossless'] = false;
  293. }
  294. // Set information from meta
  295. if (!empty($info['flv']['meta']['onMetaData']['duration'])) {
  296. $info['playtime_seconds'] = $info['flv']['meta']['onMetaData']['duration'];
  297. $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  298. }
  299. if (isset($info['flv']['meta']['onMetaData']['audiocodecid'])) {
  300. $info['audio']['codec'] = self::audioFormatLookup($info['flv']['meta']['onMetaData']['audiocodecid']);
  301. }
  302. if (isset($info['flv']['meta']['onMetaData']['videocodecid'])) {
  303. $info['video']['codec'] = self::videoCodecLookup($info['flv']['meta']['onMetaData']['videocodecid']);
  304. }
  305. return true;
  306. }
  307. /**
  308. * @param int $id
  309. *
  310. * @return string|false
  311. */
  312. public static function audioFormatLookup($id) {
  313. static $lookup = array(
  314. 0 => 'Linear PCM, platform endian',
  315. 1 => 'ADPCM',
  316. 2 => 'mp3',
  317. 3 => 'Linear PCM, little endian',
  318. 4 => 'Nellymoser 16kHz mono',
  319. 5 => 'Nellymoser 8kHz mono',
  320. 6 => 'Nellymoser',
  321. 7 => 'G.711A-law logarithmic PCM',
  322. 8 => 'G.711 mu-law logarithmic PCM',
  323. 9 => 'reserved',
  324. 10 => 'AAC',
  325. 11 => 'Speex',
  326. 12 => false, // unknown?
  327. 13 => false, // unknown?
  328. 14 => 'mp3 8kHz',
  329. 15 => 'Device-specific sound',
  330. );
  331. return (isset($lookup[$id]) ? $lookup[$id] : false);
  332. }
  333. /**
  334. * @param int $id
  335. *
  336. * @return int|false
  337. */
  338. public static function audioRateLookup($id) {
  339. static $lookup = array(
  340. 0 => 5500,
  341. 1 => 11025,
  342. 2 => 22050,
  343. 3 => 44100,
  344. );
  345. return (isset($lookup[$id]) ? $lookup[$id] : false);
  346. }
  347. /**
  348. * @param int $id
  349. *
  350. * @return int|false
  351. */
  352. public static function audioBitDepthLookup($id) {
  353. static $lookup = array(
  354. 0 => 8,
  355. 1 => 16,
  356. );
  357. return (isset($lookup[$id]) ? $lookup[$id] : false);
  358. }
  359. /**
  360. * @param int $id
  361. *
  362. * @return string|false
  363. */
  364. public static function videoCodecLookup($id) {
  365. static $lookup = array(
  366. GETID3_FLV_VIDEO_H263 => 'Sorenson H.263',
  367. GETID3_FLV_VIDEO_SCREEN => 'Screen video',
  368. GETID3_FLV_VIDEO_VP6FLV => 'On2 VP6',
  369. GETID3_FLV_VIDEO_VP6FLV_ALPHA => 'On2 VP6 with alpha channel',
  370. GETID3_FLV_VIDEO_SCREENV2 => 'Screen video v2',
  371. GETID3_FLV_VIDEO_H264 => 'Sorenson H.264',
  372. );
  373. return (isset($lookup[$id]) ? $lookup[$id] : false);
  374. }
  375. }
  376. class AMFStream
  377. {
  378. /**
  379. * @var string
  380. */
  381. public $bytes;
  382. /**
  383. * @var int
  384. */
  385. public $pos;
  386. /**
  387. * @param string $bytes
  388. */
  389. public function __construct(&$bytes) {
  390. $this->bytes =& $bytes;
  391. $this->pos = 0;
  392. }
  393. /**
  394. * @return int
  395. */
  396. public function readByte() { // 8-bit
  397. return ord(substr($this->bytes, $this->pos++, 1));
  398. }
  399. /**
  400. * @return int
  401. */
  402. public function readInt() { // 16-bit
  403. return ($this->readByte() << 8) + $this->readByte();
  404. }
  405. /**
  406. * @return int
  407. */
  408. public function readLong() { // 32-bit
  409. return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte();
  410. }
  411. /**
  412. * @return float|false
  413. */
  414. public function readDouble() {
  415. return getid3_lib::BigEndian2Float($this->read(8));
  416. }
  417. /**
  418. * @return string
  419. */
  420. public function readUTF() {
  421. $length = $this->readInt();
  422. return $this->read($length);
  423. }
  424. /**
  425. * @return string
  426. */
  427. public function readLongUTF() {
  428. $length = $this->readLong();
  429. return $this->read($length);
  430. }
  431. /**
  432. * @param int $length
  433. *
  434. * @return string
  435. */
  436. public function read($length) {
  437. $val = substr($this->bytes, $this->pos, $length);
  438. $this->pos += $length;
  439. return $val;
  440. }
  441. /**
  442. * @return int
  443. */
  444. public function peekByte() {
  445. $pos = $this->pos;
  446. $val = $this->readByte();
  447. $this->pos = $pos;
  448. return $val;
  449. }
  450. /**
  451. * @return int
  452. */
  453. public function peekInt() {
  454. $pos = $this->pos;
  455. $val = $this->readInt();
  456. $this->pos = $pos;
  457. return $val;
  458. }
  459. /**
  460. * @return int
  461. */
  462. public function peekLong() {
  463. $pos = $this->pos;
  464. $val = $this->readLong();
  465. $this->pos = $pos;
  466. return $val;
  467. }
  468. /**
  469. * @return float|false
  470. */
  471. public function peekDouble() {
  472. $pos = $this->pos;
  473. $val = $this->readDouble();
  474. $this->pos = $pos;
  475. return $val;
  476. }
  477. /**
  478. * @return string
  479. */
  480. public function peekUTF() {
  481. $pos = $this->pos;
  482. $val = $this->readUTF();
  483. $this->pos = $pos;
  484. return $val;
  485. }
  486. /**
  487. * @return string
  488. */
  489. public function peekLongUTF() {
  490. $pos = $this->pos;
  491. $val = $this->readLongUTF();
  492. $this->pos = $pos;
  493. return $val;
  494. }
  495. }
  496. class AMFReader
  497. {
  498. /**
  499. * @var AMFStream
  500. */
  501. public $stream;
  502. /**
  503. * @param AMFStream $stream
  504. */
  505. public function __construct(AMFStream $stream) {
  506. $this->stream = $stream;
  507. }
  508. /**
  509. * @return mixed
  510. */
  511. public function readData() {
  512. $value = null;
  513. $type = $this->stream->readByte();
  514. switch ($type) {
  515. // Double
  516. case 0:
  517. $value = $this->readDouble();
  518. break;
  519. // Boolean
  520. case 1:
  521. $value = $this->readBoolean();
  522. break;
  523. // String
  524. case 2:
  525. $value = $this->readString();
  526. break;
  527. // Object
  528. case 3:
  529. $value = $this->readObject();
  530. break;
  531. // null
  532. case 6:
  533. return null;
  534. // Mixed array
  535. case 8:
  536. $value = $this->readMixedArray();
  537. break;
  538. // Array
  539. case 10:
  540. $value = $this->readArray();
  541. break;
  542. // Date
  543. case 11:
  544. $value = $this->readDate();
  545. break;
  546. // Long string
  547. case 13:
  548. $value = $this->readLongString();
  549. break;
  550. // XML (handled as string)
  551. case 15:
  552. $value = $this->readXML();
  553. break;
  554. // Typed object (handled as object)
  555. case 16:
  556. $value = $this->readTypedObject();
  557. break;
  558. // Long string
  559. default:
  560. $value = '(unknown or unsupported data type)';
  561. break;
  562. }
  563. return $value;
  564. }
  565. /**
  566. * @return float|false
  567. */
  568. public function readDouble() {
  569. return $this->stream->readDouble();
  570. }
  571. /**
  572. * @return bool
  573. */
  574. public function readBoolean() {
  575. return $this->stream->readByte() == 1;
  576. }
  577. /**
  578. * @return string
  579. */
  580. public function readString() {
  581. return $this->stream->readUTF();
  582. }
  583. /**
  584. * @return array
  585. */
  586. public function readObject() {
  587. // Get highest numerical index - ignored
  588. // $highestIndex = $this->stream->readLong();
  589. $data = array();
  590. $key = null;
  591. while ($key = $this->stream->readUTF()) {
  592. $data[$key] = $this->readData();
  593. }
  594. // Mixed array record ends with empty string (0x00 0x00) and 0x09
  595. if (($key == '') && ($this->stream->peekByte() == 0x09)) {
  596. // Consume byte
  597. $this->stream->readByte();
  598. }
  599. return $data;
  600. }
  601. /**
  602. * @return array
  603. */
  604. public function readMixedArray() {
  605. // Get highest numerical index - ignored
  606. $highestIndex = $this->stream->readLong();
  607. $data = array();
  608. $key = null;
  609. while ($key = $this->stream->readUTF()) {
  610. if (is_numeric($key)) {
  611. $key = (int) $key;
  612. }
  613. $data[$key] = $this->readData();
  614. }
  615. // Mixed array record ends with empty string (0x00 0x00) and 0x09
  616. if (($key == '') && ($this->stream->peekByte() == 0x09)) {
  617. // Consume byte
  618. $this->stream->readByte();
  619. }
  620. return $data;
  621. }
  622. /**
  623. * @return array
  624. */
  625. public function readArray() {
  626. $length = $this->stream->readLong();
  627. $data = array();
  628. for ($i = 0; $i < $length; $i++) {
  629. $data[] = $this->readData();
  630. }
  631. return $data;
  632. }
  633. /**
  634. * @return float|false
  635. */
  636. public function readDate() {
  637. $timestamp = $this->stream->readDouble();
  638. $timezone = $this->stream->readInt();
  639. return $timestamp;
  640. }
  641. /**
  642. * @return string
  643. */
  644. public function readLongString() {
  645. return $this->stream->readLongUTF();
  646. }
  647. /**
  648. * @return string
  649. */
  650. public function readXML() {
  651. return $this->stream->readLongUTF();
  652. }
  653. /**
  654. * @return array
  655. */
  656. public function readTypedObject() {
  657. $className = $this->stream->readUTF();
  658. return $this->readObject();
  659. }
  660. }
  661. class AVCSequenceParameterSetReader
  662. {
  663. /**
  664. * @var string
  665. */
  666. public $sps;
  667. public $start = 0;
  668. public $currentBytes = 0;
  669. public $currentBits = 0;
  670. /**
  671. * @var int
  672. */
  673. public $width;
  674. /**
  675. * @var int
  676. */
  677. public $height;
  678. /**
  679. * @param string $sps
  680. */
  681. public function __construct($sps) {
  682. $this->sps = $sps;
  683. }
  684. public function readData() {
  685. $this->skipBits(8);
  686. $this->skipBits(8);
  687. $profile = $this->getBits(8); // read profile
  688. if ($profile > 0) {
  689. $this->skipBits(8);
  690. $level_idc = $this->getBits(8); // level_idc
  691. $this->expGolombUe(); // seq_parameter_set_id // sps
  692. $this->expGolombUe(); // log2_max_frame_num_minus4
  693. $picOrderType = $this->expGolombUe(); // pic_order_cnt_type
  694. if ($picOrderType == 0) {
  695. $this->expGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
  696. } elseif ($picOrderType == 1) {
  697. $this->skipBits(1); // delta_pic_order_always_zero_flag
  698. $this->expGolombSe(); // offset_for_non_ref_pic
  699. $this->expGolombSe(); // offset_for_top_to_bottom_field
  700. $num_ref_frames_in_pic_order_cnt_cycle = $this->expGolombUe(); // num_ref_frames_in_pic_order_cnt_cycle
  701. for ($i = 0; $i < $num_ref_frames_in_pic_order_cnt_cycle; $i++) {
  702. $this->expGolombSe(); // offset_for_ref_frame[ i ]
  703. }
  704. }
  705. $this->expGolombUe(); // num_ref_frames
  706. $this->skipBits(1); // gaps_in_frame_num_value_allowed_flag
  707. $pic_width_in_mbs_minus1 = $this->expGolombUe(); // pic_width_in_mbs_minus1
  708. $pic_height_in_map_units_minus1 = $this->expGolombUe(); // pic_height_in_map_units_minus1
  709. $frame_mbs_only_flag = $this->getBits(1); // frame_mbs_only_flag
  710. if ($frame_mbs_only_flag == 0) {
  711. $this->skipBits(1); // mb_adaptive_frame_field_flag
  712. }
  713. $this->skipBits(1); // direct_8x8_inference_flag
  714. $frame_cropping_flag = $this->getBits(1); // frame_cropping_flag
  715. $frame_crop_left_offset = 0;
  716. $frame_crop_right_offset = 0;
  717. $frame_crop_top_offset = 0;
  718. $frame_crop_bottom_offset = 0;
  719. if ($frame_cropping_flag) {
  720. $frame_crop_left_offset = $this->expGolombUe(); // frame_crop_left_offset
  721. $frame_crop_right_offset = $this->expGolombUe(); // frame_crop_right_offset
  722. $frame_crop_top_offset = $this->expGolombUe(); // frame_crop_top_offset
  723. $frame_crop_bottom_offset = $this->expGolombUe(); // frame_crop_bottom_offset
  724. }
  725. $this->skipBits(1); // vui_parameters_present_flag
  726. // etc
  727. $this->width = (($pic_width_in_mbs_minus1 + 1) * 16) - ($frame_crop_left_offset * 2) - ($frame_crop_right_offset * 2);
  728. $this->height = ((2 - $frame_mbs_only_flag) * ($pic_height_in_map_units_minus1 + 1) * 16) - ($frame_crop_top_offset * 2) - ($frame_crop_bottom_offset * 2);
  729. }
  730. }
  731. /**
  732. * @param int $bits
  733. */
  734. public function skipBits($bits) {
  735. $newBits = $this->currentBits + $bits;
  736. $this->currentBytes += (int)floor($newBits / 8);
  737. $this->currentBits = $newBits % 8;
  738. }
  739. /**
  740. * @return int
  741. */
  742. public function getBit() {
  743. $result = (getid3_lib::BigEndian2Int(substr($this->sps, $this->currentBytes, 1)) >> (7 - $this->currentBits)) & 0x01;
  744. $this->skipBits(1);
  745. return $result;
  746. }
  747. /**
  748. * @param int $bits
  749. *
  750. * @return int
  751. */
  752. public function getBits($bits) {
  753. $result = 0;
  754. for ($i = 0; $i < $bits; $i++) {
  755. $result = ($result << 1) + $this->getBit();
  756. }
  757. return $result;
  758. }
  759. /**
  760. * @return int
  761. */
  762. public function expGolombUe() {
  763. $significantBits = 0;
  764. $bit = $this->getBit();
  765. while ($bit == 0) {
  766. $significantBits++;
  767. $bit = $this->getBit();
  768. if ($significantBits > 31) {
  769. // something is broken, this is an emergency escape to prevent infinite loops
  770. return 0;
  771. }
  772. }
  773. return (1 << $significantBits) + $this->getBits($significantBits) - 1;
  774. }
  775. /**
  776. * @return int
  777. */
  778. public function expGolombSe() {
  779. $result = $this->expGolombUe();
  780. if (($result & 0x01) == 0) {
  781. return -($result >> 1);
  782. } else {
  783. return ($result + 1) >> 1;
  784. }
  785. }
  786. /**
  787. * @return int
  788. */
  789. public function getWidth() {
  790. return $this->width;
  791. }
  792. /**
  793. * @return int
  794. */
  795. public function getHeight() {
  796. return $this->height;
  797. }
  798. }