PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/_grenier_/getid3/inc/getid3/module.audio.flac.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 309 lines | 223 code | 65 blank | 21 comment | 30 complexity | ecc4e5d4465105530fc3c60f55ad0f0b 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. /////////////////////////////////////////////////////////////////
  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. default:
  84. $ThisFileInfo['warning'][] = 'Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$METAdataBlockType.') at offset '.$METAdataBlockOffset;
  85. break;
  86. }
  87. } while ($METAdataLastBlockFlag === false);
  88. if (isset($ThisFileInfo['flac']['STREAMINFO'])) {
  89. $ThisFileInfo['flac']['compressed_audio_bytes'] = $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'];
  90. $ThisFileInfo['flac']['uncompressed_audio_bytes'] = $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] * $ThisFileInfo['flac']['STREAMINFO']['channels'] * ($ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'] / 8);
  91. if ($ThisFileInfo['flac']['uncompressed_audio_bytes'] == 0) {
  92. $ThisFileInfo['error'][] = 'Corrupt FLAC file: uncompressed_audio_bytes == zero';
  93. return false;
  94. }
  95. $ThisFileInfo['flac']['compression_ratio'] = $ThisFileInfo['flac']['compressed_audio_bytes'] / $ThisFileInfo['flac']['uncompressed_audio_bytes'];
  96. }
  97. // set md5_data_source - built into flac 0.5+
  98. if (isset($ThisFileInfo['flac']['STREAMINFO']['audio_signature'])) {
  99. if ($ThisFileInfo['flac']['STREAMINFO']['audio_signature'] === str_repeat("\x00", 16)) {
  100. $ThisFileInfo['warning'][] = 'FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)';
  101. } else {
  102. $ThisFileInfo['md5_data_source'] = '';
  103. $md5 = $ThisFileInfo['flac']['STREAMINFO']['audio_signature'];
  104. for ($i = 0; $i < strlen($md5); $i++) {
  105. $ThisFileInfo['md5_data_source'] .= str_pad(dechex(ord($md5{$i})), 2, '00', STR_PAD_LEFT);
  106. }
  107. if (!preg_match('/^[0-9a-f]{32}$/', $ThisFileInfo['md5_data_source'])) {
  108. unset($ThisFileInfo['md5_data_source']);
  109. }
  110. }
  111. }
  112. $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'];
  113. if ($ThisFileInfo['audio']['bits_per_sample'] == 8) {
  114. // special case
  115. // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value
  116. // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed
  117. $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';
  118. }
  119. if (!empty($ThisFileInfo['ogg']['vendor'])) {
  120. $ThisFileInfo['audio']['encoder'] = $ThisFileInfo['ogg']['vendor'];
  121. }
  122. return true;
  123. }
  124. function FLACmetaBlockTypeLookup($blocktype) {
  125. static $FLACmetaBlockTypeLookup = array();
  126. if (empty($FLACmetaBlockTypeLookup)) {
  127. $FLACmetaBlockTypeLookup[0] = 'STREAMINFO';
  128. $FLACmetaBlockTypeLookup[1] = 'PADDING';
  129. $FLACmetaBlockTypeLookup[2] = 'APPLICATION';
  130. $FLACmetaBlockTypeLookup[3] = 'SEEKTABLE';
  131. $FLACmetaBlockTypeLookup[4] = 'VORBIS_COMMENT';
  132. $FLACmetaBlockTypeLookup[5] = 'CUESHEET';
  133. }
  134. return (isset($FLACmetaBlockTypeLookup[$blocktype]) ? $FLACmetaBlockTypeLookup[$blocktype] : 'reserved');
  135. }
  136. function FLACapplicationIDLookup($applicationid) {
  137. static $FLACapplicationIDLookup = array();
  138. if (empty($FLACapplicationIDLookup)) {
  139. // http://flac.sourceforge.net/id.html
  140. $FLACapplicationIDLookup[0x46746F6C] = 'flac-tools'; // 'Ftol'
  141. $FLACapplicationIDLookup[0x46746F6C] = 'Sound Font FLAC'; // 'SFFL'
  142. }
  143. return (isset($FLACapplicationIDLookup[$applicationid]) ? $FLACapplicationIDLookup[$applicationid] : 'reserved');
  144. }
  145. function FLACparseSTREAMINFO($METAdataBlockData, &$ThisFileInfo) {
  146. $offset = 0;
  147. $ThisFileInfo['flac']['STREAMINFO']['min_block_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
  148. $offset += 2;
  149. $ThisFileInfo['flac']['STREAMINFO']['max_block_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
  150. $offset += 2;
  151. $ThisFileInfo['flac']['STREAMINFO']['min_frame_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 3));
  152. $offset += 3;
  153. $ThisFileInfo['flac']['STREAMINFO']['max_frame_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 3));
  154. $offset += 3;
  155. $SampleRateChannelsSampleBitsStreamSamples = getid3_lib::BigEndian2Bin(substr($METAdataBlockData, $offset, 8));
  156. $ThisFileInfo['flac']['STREAMINFO']['sample_rate'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 0, 20));
  157. $ThisFileInfo['flac']['STREAMINFO']['channels'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 20, 3)) + 1;
  158. $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 23, 5)) + 1;
  159. $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 28, 36));
  160. $offset += 8;
  161. $ThisFileInfo['flac']['STREAMINFO']['audio_signature'] = substr($METAdataBlockData, $offset, 16);
  162. $offset += 16;
  163. if (!empty($ThisFileInfo['flac']['STREAMINFO']['sample_rate'])) {
  164. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  165. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['flac']['STREAMINFO']['sample_rate'];
  166. $ThisFileInfo['audio']['channels'] = $ThisFileInfo['flac']['STREAMINFO']['channels'];
  167. $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'];
  168. $ThisFileInfo['playtime_seconds'] = $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] / $ThisFileInfo['flac']['STREAMINFO']['sample_rate'];
  169. $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
  170. } else {
  171. $ThisFileInfo['error'][] = 'Corrupt METAdata block: STREAMINFO';
  172. return false;
  173. }
  174. return true;
  175. }
  176. function FLACparseAPPLICATION($METAdataBlockData, &$ThisFileInfo) {
  177. $offset = 0;
  178. $ApplicationID = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 4));
  179. $offset += 4;
  180. $ThisFileInfo['flac']['APPLICATION'][$ApplicationID]['name'] = getid3_flac::FLACapplicationIDLookup($ApplicationID);
  181. $ThisFileInfo['flac']['APPLICATION'][$ApplicationID]['data'] = substr($METAdataBlockData, $offset);
  182. $offset = $METAdataBlockLength;
  183. return true;
  184. }
  185. function FLACparseSEEKTABLE($METAdataBlockData, &$ThisFileInfo) {
  186. $offset = 0;
  187. $METAdataBlockLength = strlen($METAdataBlockData);
  188. $placeholderpattern = str_repeat("\xFF", 8);
  189. while ($offset < $METAdataBlockLength) {
  190. $SampleNumberString = substr($METAdataBlockData, $offset, 8);
  191. $offset += 8;
  192. if ($SampleNumberString == $placeholderpattern) {
  193. // placeholder point
  194. @$ThisFileInfo['flac']['SEEKTABLE']['placeholders']++;
  195. $offset += 10;
  196. } else {
  197. $SampleNumber = getid3_lib::BigEndian2Int($SampleNumberString);
  198. $ThisFileInfo['flac']['SEEKTABLE'][$SampleNumber]['offset'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
  199. $offset += 8;
  200. $ThisFileInfo['flac']['SEEKTABLE'][$SampleNumber]['samples'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
  201. $offset += 2;
  202. }
  203. }
  204. return true;
  205. }
  206. function FLACparseCUESHEET($METAdataBlockData, &$ThisFileInfo) {
  207. $offset = 0;
  208. $ThisFileInfo['flac']['CUESHEET']['media_catalog_number'] = trim(substr($METAdataBlockData, $offset, 128), "\0");
  209. $offset += 128;
  210. $ThisFileInfo['flac']['CUESHEET']['lead_in_samples'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
  211. $offset += 8;
  212. $ThisFileInfo['flac']['CUESHEET']['flags']['is_cd'] = (bool) (getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1)) & 0x80);
  213. $offset += 1;
  214. $offset += 258; // reserved
  215. $ThisFileInfo['flac']['CUESHEET']['number_tracks'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  216. $offset += 1;
  217. for ($track = 0; $track < $ThisFileInfo['flac']['CUESHEET']['number_tracks']; $track++) {
  218. $TrackSampleOffset = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
  219. $offset += 8;
  220. $TrackNumber = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  221. $offset += 1;
  222. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['sample_offset'] = $TrackSampleOffset;
  223. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['isrc'] = substr($METAdataBlockData, $offset, 12);
  224. $offset += 12;
  225. $TrackFlagsRaw = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  226. $offset += 1;
  227. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['is_audio'] = (bool) ($TrackFlagsRaw & 0x80);
  228. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['pre_emphasis'] = (bool) ($TrackFlagsRaw & 0x40);
  229. $offset += 13; // reserved
  230. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  231. $offset += 1;
  232. for ($index = 0; $index < $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points']; $index++) {
  233. $IndexSampleOffset = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
  234. $offset += 8;
  235. $IndexNumber = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
  236. $offset += 1;
  237. $offset += 3; // reserved
  238. $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['indexes'][$IndexNumber] = $IndexSampleOffset;
  239. }
  240. }
  241. return true;
  242. }
  243. }
  244. ?>