PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Vendor/getid3/module.audio.flac.php

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