module.audio.flac.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /////////////////////////////////////////////////////////////////
  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. class getid3_flac
  17. {
  18. function getid3_flac(&$fd, &$ThisFileInfo) {
  19. // http://flac.sourceforge.net/format.html
  20. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  21. $StreamMarker = fread($fd, 4);
  22. if ($StreamMarker != 'fLaC') {
  23. $ThisFileInfo['error'][] = 'Expecting "fLaC" at offset '.$ThisFileInfo['avdataoffset'].', found "'.$StreamMarker.'"';
  24. return false;
  25. }
  26. $ThisFileInfo['fileformat'] = 'flac';
  27. $ThisFileInfo['audio']['dataformat'] = 'flac';
  28. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  29. $ThisFileInfo['audio']['lossless'] = true;
  30. return getid3_flac::FLACparseMETAdata($fd, $ThisFileInfo);
  31. }
  32. function FLACparseMETAdata(&$fd, &$ThisFileInfo) {
  33. do {
  34. $METAdataBlockOffset = ftell($fd);
  35. $METAdataBlockHeader = fread($fd, 4);
  36. $METAdataLastBlockFlag = (bool) (getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 0, 1)) & 0x80);
  37. $METAdataBlockType = getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 0, 1)) & 0x7F;
  38. $METAdataBlockLength = getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 1, 3));
  39. $METAdataBlockTypeText = getid3_flac::FLACmetaBlockTypeLookup($METAdataBlockType);
  40. if ($METAdataBlockLength < 0) {
  41. $ThisFileInfo['error'][] = 'corrupt or invalid METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$METAdataBlockType.') at offset '.$METAdataBlockOffset;
  42. break;
  43. }
  44. $ThisFileInfo['flac'][$METAdataBlockTypeText]['raw'] = array();
  45. $ThisFileInfo_flac_METAdataBlockTypeText_raw = &$ThisFileInfo['flac'][$METAdataBlockTypeText]['raw'];
  46. $ThisFileInfo_flac_METAdataBlockTypeText_raw['offset'] = $METAdataBlockOffset;
  47. $ThisFileInfo_flac_METAdataBlockTypeText_raw['last_meta_block'] = $METAdataLastBlockFlag;
  48. $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_type'] = $METAdataBlockType;
  49. $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_type_text'] = $METAdataBlockTypeText;
  50. $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_length'] = $METAdataBlockLength;
  51. $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'] = @fread($fd, $METAdataBlockLength);
  52. $ThisFileInfo['avdataoffset'] = ftell($fd);
  53. switch ($METAdataBlockTypeText) {
  54. case 'STREAMINFO':
  55. if (!getid3_flac::FLACparseSTREAMINFO($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
  56. return false;
  57. }
  58. break;
  59. case 'PADDING':
  60. // ignore
  61. break;
  62. case 'APPLICATION':
  63. if (!getid3_flac::FLACparseAPPLICATION($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
  64. return false;
  65. }
  66. break;
  67. case 'SEEKTABLE':
  68. if (!getid3_flac::FLACparseSEEKTABLE($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
  69. return false;
  70. }
  71. break;
  72. case 'VORBIS_COMMENT':
  73. $OldOffset = ftell($fd);
  74. fseek($fd, 0 - $METAdataBlockLength, SEEK_CUR);
  75. getid3_ogg::ParseVorbisCommentsFilepointer($fd, $ThisFileInfo);
  76. fseek($fd, $OldOffset, SEEK_SET);
  77. break;
  78. case 'CUESHEET':
  79. if (!getid3_flac::FLACparseCUESHEET($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
  80. return false;
  81. }
  82. break;
  83. case 'PICTURE':
  84. if (!$this->FLACparsePICTURE($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
  85. return false;
  86. }
  87. break;
  88. default:
  89. $ThisFileInfo['warning'][] = 'Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$METAdataBlockType.') at offset '.$METAdataBlockOffset;
  90. break;
  91. }
  92. } while ($METAdataLastBlockFlag === false);
  93. if (isset($ThisFileInfo['flac']['STREAMINFO'])) {
  94. $ThisFileInfo['flac']['compressed_audio_bytes'] = $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'];
  95. $ThisFileInfo['flac']['uncompressed_audio_bytes'] = $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] * $ThisFileInfo['flac']['STREAMINFO']['channels'] * ($ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'] / 8);
  96. if ($ThisFileInfo['flac']['uncompressed_audio_bytes'] == 0) {
  97. $ThisFileInfo['error'][] = 'Corrupt FLAC file: uncompressed_audio_bytes == zero';
  98. return false;
  99. }
  100. $ThisFileInfo['flac']['compression_ratio'] = $ThisFileInfo['flac']['compressed_audio_bytes'] / $ThisFileInfo['flac']['uncompressed_audio_bytes'];
  101. }
  102. // set md5_data_source - built into flac 0.5+
  103. if (isset($ThisFileInfo['flac']['STREAMINFO']['audio_signature'])) {
  104. if ($ThisFileInfo['flac']['STREAMINFO']['audio_signature'] === str_repeat("\x00", 16)) {
  105. $ThisFileInfo['warning'][] = 'FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)';
  106. } else {
  107. $ThisFileInfo['md5_data_source'] = '';
  108. $md5 = $ThisFileInfo['flac']['STREAMINFO']['audio_signature'];
  109. for ($i = 0; $i < strlen($md5); $i++) {
  110. $ThisFileInfo['md5_data_source'] .= str_pad(dechex(ord($md5{$i})), 2, '00', STR_PAD_LEFT);
  111. }
  112. if (!preg_match('/^[0-9a-f]{32}$/', $ThisFileInfo['md5_data_source'])) {
  113. unset($ThisFileInfo['md5_data_source']);
  114. }
  115. }
  116. }
  117. $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'];
  118. if ($ThisFileInfo['audio']['bits_per_sample'] == 8) {
  119. // special case
  120. // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value
  121. // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed
  122. $ThisFileInfo['warning'][] = 'FLAC calculates MD5 data strangely on 8-bit audio, so the stored md5_data_source value will not match the decoded WAV file';
  123. }
  124. if (!empty($ThisFileInfo['ogg']['vendor'])) {
  125. $ThisFileInfo['audio']['encoder'] = $ThisFileInfo['ogg']['vendor'];
  126. }
  127. return true;
  128. }
  129. function FLACmetaBlockTypeLookup($blocktype) {
  130. static $FLACmetaBlockTypeLookup = array();
  131. if (empty($FLACmetaBlockTypeLookup)) {
  132. $FLACmetaBlockTypeLookup[0] = 'STREAMINFO';
  133. $FLACmetaBlockTypeLookup[1] = 'PADDING';
  134. $FLACmetaBlockTypeLookup[2] = 'APPLICATION';
  135. $FLACmetaBlockTypeLookup[3] = 'SEEKTABLE';
  136. $FLACmetaBlockTypeLookup[4] = 'VORBIS_COMMENT';
  137. $FLACmetaBlockTypeLookup[5] = 'CUESHEET';
  138. $FLACmetaBlockTypeLookup[6] = 'PICTURE';
  139. }
  140. return (isset($FLACmetaBlockTypeLookup[$blocktype]) ? $FLACmetaBlockTypeLookup[$blocktype] : 'reserved');
  141. }
  142. function FLACapplicationIDLookup($applicationid) {
  143. static $FLACapplicationIDLookup = array();
  144. if (empty($FLACapplicationIDLookup)) {
  145. // http://flac.sourceforge.net/id.html
  146. $FLACapplicationIDLookup[0x46746F6C] = 'flac-tools'; // 'Ftol'
  147. $FLACapplicationIDLookup[0x46746F6C] = 'Sound Font FLAC'; // 'SFFL'
  148. }
  149. return (isset($FLACapplicationIDLookup[$applicationid]) ? $FLACapplicationIDLookup[$applicationid] : 'reserved');
  150. }
  151. function FLACpictureTypeLookup($type_id) {
  152. static $lookup = array (
  153. 0 => 'Other',
  154. 1 => '32x32 pixels \'file icon\' (PNG only)',
  155. 2 => 'Other file icon',
  156. 3 => 'Cover (front)',
  157. 4 => 'Cover (back)',
  158. 5 => 'Leaflet page',
  159. 6 => 'Media (e.g. label side of CD)',
  160. 7 => 'Lead artist/lead performer/soloist',
  161. 8 => 'Artist/performer',
  162. 9 => 'Conductor',
  163. 10 => 'Band/Orchestra',
  164. 11 => 'Composer',
  165. 12 => 'Lyricist/text writer',
  166. 13 => 'Recording Location',
  167. 14 => 'During recording',
  168. 15 => 'During performance',
  169. 16 => 'Movie/video screen capture',
  170. 17 => 'A bright coloured fish',
  171. 18 => 'Illustration',
  172. 19 => 'Band/artist logotype',
  173. 20 => 'Publisher/Studio logotype',
  174. );
  175. return (isset($lookup[$type_id]) ? $lookup[$type_id] : 'reserved');
  176. }
  177. function FLACparseSTREAMINFO($METAdataBlockData, &$ThisFileInfo) {
  178. $offset = 0;
  179. $ThisFileInfo['flac']['STREAMINFO']['min_block_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
  180. $offset += 2;
  181. $ThisFileInfo['flac']['STREAMINFO']['max_block_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
  182. $offset += 2;
  183. $ThisFileInfo['flac']['STREAMINFO']['min_frame_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 3));
  184. $offset += 3;
  185. $ThisFileInfo['flac']['STREAMINFO']['max_frame_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 3));
  186. $offset += 3;
  187. $SampleRateChannelsSampleBitsStreamSamples = getid3_lib::BigEndian2Bin(substr($METAdataBlockData, $offset, 8));
  188. $ThisFileInfo['flac']['STREAMINFO']['sample_rate'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 0, 20));
  189. $ThisFileInfo['flac']['STREAMINFO']['channels'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 20, 3)) + 1;
  190. $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 23, 5)) + 1;
  191. $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 28, 36));
  192. $offset += 8;
  193. $ThisFileInfo['flac']['STREAMINFO']['audio_signature'] = substr($METAdataBlockData, $offset, 16);
  194. $offset += 16;
  195. if (!empty($ThisFileInfo['flac']['STREAMINFO']['sample_rate'])) {
  196. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  197. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['flac']['STREAMINFO']['sample_rate'];
  198. $ThisFileInfo['audio']['channels'] = $ThisFileInfo['flac']['STREAMINFO']['channels'];
  199. $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'];
  200. $ThisFileInfo['playtime_seconds'] = $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] / $ThisFileInfo['flac']['STREAMINFO']['sample_rate'];
  201. $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
  202. } else {
  203. $ThisFileInfo['error'][] = 'Corrupt METAdata block: STREAMINFO';
  204. return false;
  205. }
  206. unset($ThisFileInfo['flac']['STREAMINFO']['raw']);
  207. return true;
  208. }
  209. function FLACparseAPPLICATION($METAdataBlockData, &$ThisFileInfo) {
  210. $offset = 0;
  211. $ApplicationID = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 4));
  212. $offset += 4;
  213. $ThisFileInfo['flac']['APPLICATION'][$ApplicationID]['name'] = getid3_flac::FLACapplicationIDLookup($ApplicationID);
  214. $ThisFileInfo['flac']['APPLICATION'][$ApplicationID]['data'] = substr($METAdataBlockData, $offset);
  215. $offset = $METAdataBlockLength;
  216. unset($ThisFileInfo['flac']['APPLICATION']['raw']);
  217. return true;
  218. }
  219. function FLACparseSEEKTABLE($METAdataBlockData, &$ThisFileInfo) {
  220. $offset = 0;
  221. $METAdataBlockLength = strlen($METAdataBlockData);
  222. $placeholderpattern = str_repeat("\xFF", 8);
  223. while ($offset < $METAdataBlockLength) {
  224. $SampleNumberString = substr($METAdataBlockData, $offset, 8);
  225. $offset += 8;
  226. if ($SampleNumberString == $placeholderpattern) {
  227. // placeholder point
  228. @$ThisFileInfo['flac']['SEEKTABLE']['placeholders']++;
  229. $offset += 10;
  230. } else {
  231. $SampleNumber = getid3_lib::BigEndian2Int($SampleNumberString);
  232. $ThisFileInfo['flac']['SEEKTABLE'][$SampleNumber]['offset'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
  233. $offset += 8;
  234. $ThisFileInfo['flac']['SEEKTABLE'][$SampleNumber]['samples'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
  235. $offset += 2;
  236. }
  237. }
  238. unset($ThisFileInfo['flac']['SEEKTABLE']['raw']);
  239. return true;
  240. }
  241. function FLACparseCUESHEET($METAdataBlockData, &$ThisFileInfo) {
  242. $offset = 0;
  243. $ThisFileInfo['flac']['CUESHEET']['media_catalog_number'] = trim(substr($METAdataBlockData, $offset, 128), "\0");
  244. $offset += 128;
  245. $ThisFileInfo['flac']['CUESHEET']['lead_in_samples'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
  246. $offset += 8;
  247. $ThisFileInfo['flac']['CUESHEET']['flags']['is_cd'] = (bool) (getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1)) & 0x80);
  248. $offset += 1;
  249. $offset += 258; // reserved
  250. $ThisFileInfo['flac']['CUESHEET']['number_tracks'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  251. $offset += 1;
  252. for ($track = 0; $track < $ThisFileInfo['flac']['CUESHEET']['number_tracks']; $track++) {
  253. $TrackSampleOffset = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
  254. $offset += 8;
  255. $TrackNumber = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  256. $offset += 1;
  257. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['sample_offset'] = $TrackSampleOffset;
  258. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['isrc'] = substr($METAdataBlockData, $offset, 12);
  259. $offset += 12;
  260. $TrackFlagsRaw = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  261. $offset += 1;
  262. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['is_audio'] = (bool) ($TrackFlagsRaw & 0x80);
  263. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['pre_emphasis'] = (bool) ($TrackFlagsRaw & 0x40);
  264. $offset += 13; // reserved
  265. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  266. $offset += 1;
  267. for ($index = 0; $index < $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points']; $index++) {
  268. $IndexSampleOffset = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
  269. $offset += 8;
  270. $IndexNumber = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  271. $offset += 1;
  272. $offset += 3; // reserved
  273. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['indexes'][$IndexNumber] = $IndexSampleOffset;
  274. }
  275. }
  276. unset($ThisFileInfo['flac']['CUESHEET']['raw']);
  277. return true;
  278. }
  279. function FLACparsePICTURE($meta_data_block_data, &$ThisFileInfo) {
  280. $picture = &$ThisFileInfo['flac']['PICTURE'][sizeof($ThisFileInfo['flac']['PICTURE']) - 1];
  281. $offset = 0;
  282. $picture['type'] = $this->FLACpictureTypeLookup(getid3_lib::BigEndian2Int(substr($meta_data_block_data, $offset, 4)));
  283. $offset += 4;
  284. $length = getid3_lib::BigEndian2Int(substr($meta_data_block_data, $offset, 4));
  285. $offset += 4;
  286. $picture['mime_type'] = substr($meta_data_block_data, $offset, $length);
  287. $offset += $length;
  288. $length = getid3_lib::BigEndian2Int(substr($meta_data_block_data, $offset, 4));
  289. $offset += 4;
  290. $picture['description'] = substr($meta_data_block_data, $offset, $length);
  291. $offset += $length;
  292. $picture['width'] = getid3_lib::BigEndian2Int(substr($meta_data_block_data, $offset, 4));
  293. $offset += 4;
  294. $picture['height'] = getid3_lib::BigEndian2Int(substr($meta_data_block_data, $offset, 4));
  295. $offset += 4;
  296. $picture['color_depth'] = getid3_lib::BigEndian2Int(substr($meta_data_block_data, $offset, 4));
  297. $offset += 4;
  298. $picture['colors_indexed'] = getid3_lib::BigEndian2Int(substr($meta_data_block_data, $offset, 4));
  299. $offset += 4;
  300. $length = getid3_lib::BigEndian2Int(substr($meta_data_block_data, $offset, 4));
  301. $offset += 4;
  302. $picture['image_data'] = substr($meta_data_block_data, $offset, $length);
  303. $offset += $length;
  304. unset($ThisFileInfo['flac']['PICTURE']['raw']);
  305. return true;
  306. }
  307. }
  308. ?>