PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/ID3/module.audio.flac.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 516 lines | 359 code | 74 blank | 83 comment | 53 complexity | 5892c3a5453e0825861d75b70758fe9f MD5 | raw file
  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.flac.php //
  11. // module for analyzing FLAC and OggFLAC audio files //
  12. // dependencies: module.audio.ogg.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ogg.php', __FILE__, true);
  16. /**
  17. * @tutorial http://flac.sourceforge.net/format.html
  18. */
  19. class getid3_flac extends getid3_handler
  20. {
  21. const syncword = 'fLaC';
  22. /**
  23. * @return bool
  24. */
  25. public function Analyze() {
  26. $info = &$this->getid3->info;
  27. $this->fseek($info['avdataoffset']);
  28. $StreamMarker = $this->fread(4);
  29. if ($StreamMarker != self::syncword) {
  30. return $this->error('Expecting "'.getid3_lib::PrintHexBytes(self::syncword).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($StreamMarker).'"');
  31. }
  32. $info['fileformat'] = 'flac';
  33. $info['audio']['dataformat'] = 'flac';
  34. $info['audio']['bitrate_mode'] = 'vbr';
  35. $info['audio']['lossless'] = true;
  36. // parse flac container
  37. return $this->parseMETAdata();
  38. }
  39. /**
  40. * @return bool
  41. */
  42. public function parseMETAdata() {
  43. $info = &$this->getid3->info;
  44. do {
  45. $BlockOffset = $this->ftell();
  46. $BlockHeader = $this->fread(4);
  47. $LBFBT = getid3_lib::BigEndian2Int(substr($BlockHeader, 0, 1)); // LBFBT = LastBlockFlag + BlockType
  48. $LastBlockFlag = (bool) ($LBFBT & 0x80);
  49. $BlockType = ($LBFBT & 0x7F);
  50. $BlockLength = getid3_lib::BigEndian2Int(substr($BlockHeader, 1, 3));
  51. $BlockTypeText = self::metaBlockTypeLookup($BlockType);
  52. if (($BlockOffset + 4 + $BlockLength) > $info['avdataend']) {
  53. $this->warning('METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$BlockTypeText.') at offset '.$BlockOffset.' extends beyond end of file');
  54. break;
  55. }
  56. if ($BlockLength < 1) {
  57. if ($BlockTypeText != 'reserved') {
  58. // probably supposed to be zero-length
  59. $this->warning('METADATA_BLOCK_HEADER.BLOCK_LENGTH ('.$BlockTypeText.') at offset '.$BlockOffset.' is zero bytes');
  60. continue;
  61. }
  62. $this->error('METADATA_BLOCK_HEADER.BLOCK_LENGTH ('.$BlockLength.') at offset '.$BlockOffset.' is invalid');
  63. break;
  64. }
  65. $info['flac'][$BlockTypeText]['raw'] = array();
  66. $BlockTypeText_raw = &$info['flac'][$BlockTypeText]['raw'];
  67. $BlockTypeText_raw['offset'] = $BlockOffset;
  68. $BlockTypeText_raw['last_meta_block'] = $LastBlockFlag;
  69. $BlockTypeText_raw['block_type'] = $BlockType;
  70. $BlockTypeText_raw['block_type_text'] = $BlockTypeText;
  71. $BlockTypeText_raw['block_length'] = $BlockLength;
  72. if ($BlockTypeText_raw['block_type'] != 0x06) { // do not read attachment data automatically
  73. $BlockTypeText_raw['block_data'] = $this->fread($BlockLength);
  74. }
  75. switch ($BlockTypeText) {
  76. case 'STREAMINFO': // 0x00
  77. if (!$this->parseSTREAMINFO($BlockTypeText_raw['block_data'])) {
  78. return false;
  79. }
  80. break;
  81. case 'PADDING': // 0x01
  82. unset($info['flac']['PADDING']); // ignore
  83. break;
  84. case 'APPLICATION': // 0x02
  85. if (!$this->parseAPPLICATION($BlockTypeText_raw['block_data'])) {
  86. return false;
  87. }
  88. break;
  89. case 'SEEKTABLE': // 0x03
  90. if (!$this->parseSEEKTABLE($BlockTypeText_raw['block_data'])) {
  91. return false;
  92. }
  93. break;
  94. case 'VORBIS_COMMENT': // 0x04
  95. if (!$this->parseVORBIS_COMMENT($BlockTypeText_raw['block_data'])) {
  96. return false;
  97. }
  98. break;
  99. case 'CUESHEET': // 0x05
  100. if (!$this->parseCUESHEET($BlockTypeText_raw['block_data'])) {
  101. return false;
  102. }
  103. break;
  104. case 'PICTURE': // 0x06
  105. if (!$this->parsePICTURE()) {
  106. return false;
  107. }
  108. break;
  109. default:
  110. $this->warning('Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$BlockType.') at offset '.$BlockOffset);
  111. }
  112. unset($info['flac'][$BlockTypeText]['raw']);
  113. $info['avdataoffset'] = $this->ftell();
  114. }
  115. while ($LastBlockFlag === false);
  116. // handle tags
  117. if (!empty($info['flac']['VORBIS_COMMENT']['comments'])) {
  118. $info['flac']['comments'] = $info['flac']['VORBIS_COMMENT']['comments'];
  119. }
  120. if (!empty($info['flac']['VORBIS_COMMENT']['vendor'])) {
  121. $info['audio']['encoder'] = str_replace('reference ', '', $info['flac']['VORBIS_COMMENT']['vendor']);
  122. }
  123. // copy attachments to 'comments' array if nesesary
  124. if (isset($info['flac']['PICTURE']) && ($this->getid3->option_save_attachments !== getID3::ATTACHMENTS_NONE)) {
  125. foreach ($info['flac']['PICTURE'] as $entry) {
  126. if (!empty($entry['data'])) {
  127. if (!isset($info['flac']['comments']['picture'])) {
  128. $info['flac']['comments']['picture'] = array();
  129. }
  130. $comments_picture_data = array();
  131. foreach (array('data', 'image_mime', 'image_width', 'image_height', 'imagetype', 'picturetype', 'description', 'datalength') as $picture_key) {
  132. if (isset($entry[$picture_key])) {
  133. $comments_picture_data[$picture_key] = $entry[$picture_key];
  134. }
  135. }
  136. $info['flac']['comments']['picture'][] = $comments_picture_data;
  137. unset($comments_picture_data);
  138. }
  139. }
  140. }
  141. if (isset($info['flac']['STREAMINFO'])) {
  142. if (!$this->isDependencyFor('matroska')) {
  143. $info['flac']['compressed_audio_bytes'] = $info['avdataend'] - $info['avdataoffset'];
  144. }
  145. $info['flac']['uncompressed_audio_bytes'] = $info['flac']['STREAMINFO']['samples_stream'] * $info['flac']['STREAMINFO']['channels'] * ($info['flac']['STREAMINFO']['bits_per_sample'] / 8);
  146. if ($info['flac']['uncompressed_audio_bytes'] == 0) {
  147. return $this->error('Corrupt FLAC file: uncompressed_audio_bytes == zero');
  148. }
  149. if (!empty($info['flac']['compressed_audio_bytes'])) {
  150. $info['flac']['compression_ratio'] = $info['flac']['compressed_audio_bytes'] / $info['flac']['uncompressed_audio_bytes'];
  151. }
  152. }
  153. // set md5_data_source - built into flac 0.5+
  154. if (isset($info['flac']['STREAMINFO']['audio_signature'])) {
  155. if ($info['flac']['STREAMINFO']['audio_signature'] === str_repeat("\x00", 16)) {
  156. $this->warning('FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)');
  157. }
  158. else {
  159. $info['md5_data_source'] = '';
  160. $md5 = $info['flac']['STREAMINFO']['audio_signature'];
  161. for ($i = 0; $i < strlen($md5); $i++) {
  162. $info['md5_data_source'] .= str_pad(dechex(ord($md5[$i])), 2, '00', STR_PAD_LEFT);
  163. }
  164. if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) {
  165. unset($info['md5_data_source']);
  166. }
  167. }
  168. }
  169. if (isset($info['flac']['STREAMINFO']['bits_per_sample'])) {
  170. $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample'];
  171. if ($info['audio']['bits_per_sample'] == 8) {
  172. // special case
  173. // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value
  174. // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed
  175. $this->warning('FLAC calculates MD5 data strangely on 8-bit audio, so the stored md5_data_source value will not match the decoded WAV file');
  176. }
  177. }
  178. return true;
  179. }
  180. /**
  181. * @param string $BlockData
  182. *
  183. * @return array
  184. */
  185. public static function parseSTREAMINFOdata($BlockData) {
  186. $streaminfo = array();
  187. $streaminfo['min_block_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 0, 2));
  188. $streaminfo['max_block_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 2, 2));
  189. $streaminfo['min_frame_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 4, 3));
  190. $streaminfo['max_frame_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 7, 3));
  191. $SRCSBSS = getid3_lib::BigEndian2Bin(substr($BlockData, 10, 8));
  192. $streaminfo['sample_rate'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 0, 20));
  193. $streaminfo['channels'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 20, 3)) + 1;
  194. $streaminfo['bits_per_sample'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 23, 5)) + 1;
  195. $streaminfo['samples_stream'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 28, 36));
  196. $streaminfo['audio_signature'] = substr($BlockData, 18, 16);
  197. return $streaminfo;
  198. }
  199. /**
  200. * @param string $BlockData
  201. *
  202. * @return bool
  203. */
  204. private function parseSTREAMINFO($BlockData) {
  205. $info = &$this->getid3->info;
  206. $info['flac']['STREAMINFO'] = self::parseSTREAMINFOdata($BlockData);
  207. if (!empty($info['flac']['STREAMINFO']['sample_rate'])) {
  208. $info['audio']['bitrate_mode'] = 'vbr';
  209. $info['audio']['sample_rate'] = $info['flac']['STREAMINFO']['sample_rate'];
  210. $info['audio']['channels'] = $info['flac']['STREAMINFO']['channels'];
  211. $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample'];
  212. $info['playtime_seconds'] = $info['flac']['STREAMINFO']['samples_stream'] / $info['flac']['STREAMINFO']['sample_rate'];
  213. if ($info['playtime_seconds'] > 0) {
  214. if (!$this->isDependencyFor('matroska')) {
  215. $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  216. }
  217. else {
  218. $this->warning('Cannot determine audio bitrate because total stream size is unknown');
  219. }
  220. }
  221. } else {
  222. return $this->error('Corrupt METAdata block: STREAMINFO');
  223. }
  224. return true;
  225. }
  226. /**
  227. * @param string $BlockData
  228. *
  229. * @return bool
  230. */
  231. private function parseAPPLICATION($BlockData) {
  232. $info = &$this->getid3->info;
  233. $ApplicationID = getid3_lib::BigEndian2Int(substr($BlockData, 0, 4));
  234. $info['flac']['APPLICATION'][$ApplicationID]['name'] = self::applicationIDLookup($ApplicationID);
  235. $info['flac']['APPLICATION'][$ApplicationID]['data'] = substr($BlockData, 4);
  236. return true;
  237. }
  238. /**
  239. * @param string $BlockData
  240. *
  241. * @return bool
  242. */
  243. private function parseSEEKTABLE($BlockData) {
  244. $info = &$this->getid3->info;
  245. $offset = 0;
  246. $BlockLength = strlen($BlockData);
  247. $placeholderpattern = str_repeat("\xFF", 8);
  248. while ($offset < $BlockLength) {
  249. $SampleNumberString = substr($BlockData, $offset, 8);
  250. $offset += 8;
  251. if ($SampleNumberString == $placeholderpattern) {
  252. // placeholder point
  253. getid3_lib::safe_inc($info['flac']['SEEKTABLE']['placeholders'], 1);
  254. $offset += 10;
  255. } else {
  256. $SampleNumber = getid3_lib::BigEndian2Int($SampleNumberString);
  257. $info['flac']['SEEKTABLE'][$SampleNumber]['offset'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8));
  258. $offset += 8;
  259. $info['flac']['SEEKTABLE'][$SampleNumber]['samples'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 2));
  260. $offset += 2;
  261. }
  262. }
  263. return true;
  264. }
  265. /**
  266. * @param string $BlockData
  267. *
  268. * @return bool
  269. */
  270. private function parseVORBIS_COMMENT($BlockData) {
  271. $info = &$this->getid3->info;
  272. $getid3_ogg = new getid3_ogg($this->getid3);
  273. if ($this->isDependencyFor('matroska')) {
  274. $getid3_ogg->setStringMode($this->data_string);
  275. }
  276. $getid3_ogg->ParseVorbisComments();
  277. if (isset($info['ogg'])) {
  278. unset($info['ogg']['comments_raw']);
  279. $info['flac']['VORBIS_COMMENT'] = $info['ogg'];
  280. unset($info['ogg']);
  281. }
  282. unset($getid3_ogg);
  283. return true;
  284. }
  285. /**
  286. * @param string $BlockData
  287. *
  288. * @return bool
  289. */
  290. private function parseCUESHEET($BlockData) {
  291. $info = &$this->getid3->info;
  292. $offset = 0;
  293. $info['flac']['CUESHEET']['media_catalog_number'] = trim(substr($BlockData, $offset, 128), "\0");
  294. $offset += 128;
  295. $info['flac']['CUESHEET']['lead_in_samples'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8));
  296. $offset += 8;
  297. $info['flac']['CUESHEET']['flags']['is_cd'] = (bool) (getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)) & 0x80);
  298. $offset += 1;
  299. $offset += 258; // reserved
  300. $info['flac']['CUESHEET']['number_tracks'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1));
  301. $offset += 1;
  302. for ($track = 0; $track < $info['flac']['CUESHEET']['number_tracks']; $track++) {
  303. $TrackSampleOffset = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8));
  304. $offset += 8;
  305. $TrackNumber = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1));
  306. $offset += 1;
  307. $info['flac']['CUESHEET']['tracks'][$TrackNumber]['sample_offset'] = $TrackSampleOffset;
  308. $info['flac']['CUESHEET']['tracks'][$TrackNumber]['isrc'] = substr($BlockData, $offset, 12);
  309. $offset += 12;
  310. $TrackFlagsRaw = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1));
  311. $offset += 1;
  312. $info['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['is_audio'] = (bool) ($TrackFlagsRaw & 0x80);
  313. $info['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['pre_emphasis'] = (bool) ($TrackFlagsRaw & 0x40);
  314. $offset += 13; // reserved
  315. $info['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1));
  316. $offset += 1;
  317. for ($index = 0; $index < $info['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points']; $index++) {
  318. $IndexSampleOffset = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8));
  319. $offset += 8;
  320. $IndexNumber = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1));
  321. $offset += 1;
  322. $offset += 3; // reserved
  323. $info['flac']['CUESHEET']['tracks'][$TrackNumber]['indexes'][$IndexNumber] = $IndexSampleOffset;
  324. }
  325. }
  326. return true;
  327. }
  328. /**
  329. * Parse METADATA_BLOCK_PICTURE flac structure and extract attachment
  330. * External usage: audio.ogg
  331. *
  332. * @return bool
  333. */
  334. public function parsePICTURE() {
  335. $info = &$this->getid3->info;
  336. $picture['typeid'] = getid3_lib::BigEndian2Int($this->fread(4));
  337. $picture['picturetype'] = self::pictureTypeLookup($picture['typeid']);
  338. $picture['image_mime'] = $this->fread(getid3_lib::BigEndian2Int($this->fread(4)));
  339. $descr_length = getid3_lib::BigEndian2Int($this->fread(4));
  340. if ($descr_length) {
  341. $picture['description'] = $this->fread($descr_length);
  342. }
  343. $picture['image_width'] = getid3_lib::BigEndian2Int($this->fread(4));
  344. $picture['image_height'] = getid3_lib::BigEndian2Int($this->fread(4));
  345. $picture['color_depth'] = getid3_lib::BigEndian2Int($this->fread(4));
  346. $picture['colors_indexed'] = getid3_lib::BigEndian2Int($this->fread(4));
  347. $picture['datalength'] = getid3_lib::BigEndian2Int($this->fread(4));
  348. if ($picture['image_mime'] == '-->') {
  349. $picture['data'] = $this->fread($picture['datalength']);
  350. } else {
  351. $picture['data'] = $this->saveAttachment(
  352. str_replace('/', '_', $picture['picturetype']).'_'.$this->ftell(),
  353. $this->ftell(),
  354. $picture['datalength'],
  355. $picture['image_mime']);
  356. }
  357. $info['flac']['PICTURE'][] = $picture;
  358. return true;
  359. }
  360. /**
  361. * @param int $blocktype
  362. *
  363. * @return string
  364. */
  365. public static function metaBlockTypeLookup($blocktype) {
  366. static $lookup = array(
  367. 0 => 'STREAMINFO',
  368. 1 => 'PADDING',
  369. 2 => 'APPLICATION',
  370. 3 => 'SEEKTABLE',
  371. 4 => 'VORBIS_COMMENT',
  372. 5 => 'CUESHEET',
  373. 6 => 'PICTURE',
  374. );
  375. return (isset($lookup[$blocktype]) ? $lookup[$blocktype] : 'reserved');
  376. }
  377. /**
  378. * @param int $applicationid
  379. *
  380. * @return string
  381. */
  382. public static function applicationIDLookup($applicationid) {
  383. // http://flac.sourceforge.net/id.html
  384. static $lookup = array(
  385. 0x41544348 => 'FlacFile', // "ATCH"
  386. 0x42534F4C => 'beSolo', // "BSOL"
  387. 0x42554753 => 'Bugs Player', // "BUGS"
  388. 0x43756573 => 'GoldWave cue points (specification)', // "Cues"
  389. 0x46696361 => 'CUE Splitter', // "Fica"
  390. 0x46746F6C => 'flac-tools', // "Ftol"
  391. 0x4D4F5442 => 'MOTB MetaCzar', // "MOTB"
  392. 0x4D505345 => 'MP3 Stream Editor', // "MPSE"
  393. 0x4D754D4C => 'MusicML: Music Metadata Language', // "MuML"
  394. 0x52494646 => 'Sound Devices RIFF chunk storage', // "RIFF"
  395. 0x5346464C => 'Sound Font FLAC', // "SFFL"
  396. 0x534F4E59 => 'Sony Creative Software', // "SONY"
  397. 0x5351455A => 'flacsqueeze', // "SQEZ"
  398. 0x54745776 => 'TwistedWave', // "TtWv"
  399. 0x55495453 => 'UITS Embedding tools', // "UITS"
  400. 0x61696666 => 'FLAC AIFF chunk storage', // "aiff"
  401. 0x696D6167 => 'flac-image application for storing arbitrary files in APPLICATION metadata blocks', // "imag"
  402. 0x7065656D => 'Parseable Embedded Extensible Metadata (specification)', // "peem"
  403. 0x71667374 => 'QFLAC Studio', // "qfst"
  404. 0x72696666 => 'FLAC RIFF chunk storage', // "riff"
  405. 0x74756E65 => 'TagTuner', // "tune"
  406. 0x78626174 => 'XBAT', // "xbat"
  407. 0x786D6364 => 'xmcd', // "xmcd"
  408. );
  409. return (isset($lookup[$applicationid]) ? $lookup[$applicationid] : 'reserved');
  410. }
  411. /**
  412. * @param int $type_id
  413. *
  414. * @return string
  415. */
  416. public static function pictureTypeLookup($type_id) {
  417. static $lookup = array (
  418. 0 => 'Other',
  419. 1 => '32x32 pixels \'file icon\' (PNG only)',
  420. 2 => 'Other file icon',
  421. 3 => 'Cover (front)',
  422. 4 => 'Cover (back)',
  423. 5 => 'Leaflet page',
  424. 6 => 'Media (e.g. label side of CD)',
  425. 7 => 'Lead artist/lead performer/soloist',
  426. 8 => 'Artist/performer',
  427. 9 => 'Conductor',
  428. 10 => 'Band/Orchestra',
  429. 11 => 'Composer',
  430. 12 => 'Lyricist/text writer',
  431. 13 => 'Recording Location',
  432. 14 => 'During recording',
  433. 15 => 'During performance',
  434. 16 => 'Movie/video screen capture',
  435. 17 => 'A bright coloured fish',
  436. 18 => 'Illustration',
  437. 19 => 'Band/artist logotype',
  438. 20 => 'Publisher/Studio logotype',
  439. );
  440. return (isset($lookup[$type_id]) ? $lookup[$type_id] : 'reserved');
  441. }
  442. }