module.audio.flac.php 19 KB

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