getid3_flac

The WordPress Core getid3 flac class.

Defined (1)

The class is defined in the following location(s).

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