module.audio.la.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.la.php //
  12. // module for analyzing LA (LosslessAudio) audio files //
  13. // dependencies: module.audio.riff.php //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
  17. class getid3_la extends getid3_handler
  18. {
  19. public function Analyze() {
  20. $info = &$this->getid3->info;
  21. $offset = 0;
  22. $this->fseek($info['avdataoffset']);
  23. $rawdata = $this->fread($this->getid3->fread_buffer_size());
  24. switch (substr($rawdata, $offset, 4)) {
  25. case 'LA02':
  26. case 'LA03':
  27. case 'LA04':
  28. $info['fileformat'] = 'la';
  29. $info['audio']['dataformat'] = 'la';
  30. $info['audio']['lossless'] = true;
  31. $info['la']['version_major'] = (int) substr($rawdata, $offset + 2, 1);
  32. $info['la']['version_minor'] = (int) substr($rawdata, $offset + 3, 1);
  33. $info['la']['version'] = (float) $info['la']['version_major'] + ($info['la']['version_minor'] / 10);
  34. $offset += 4;
  35. $info['la']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  36. $offset += 4;
  37. if ($info['la']['uncompressed_size'] == 0) {
  38. $info['error'][] = 'Corrupt LA file: uncompressed_size == zero';
  39. return false;
  40. }
  41. $WAVEchunk = substr($rawdata, $offset, 4);
  42. if ($WAVEchunk !== 'WAVE') {
  43. $info['error'][] = 'Expected "WAVE" ('.getid3_lib::PrintHexBytes('WAVE').') at offset '.$offset.', found "'.$WAVEchunk.'" ('.getid3_lib::PrintHexBytes($WAVEchunk).') instead.';
  44. return false;
  45. }
  46. $offset += 4;
  47. $info['la']['fmt_size'] = 24;
  48. if ($info['la']['version'] >= 0.3) {
  49. $info['la']['fmt_size'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  50. $info['la']['header_size'] = 49 + $info['la']['fmt_size'] - 24;
  51. $offset += 4;
  52. } else {
  53. // version 0.2 didn't support additional data blocks
  54. $info['la']['header_size'] = 41;
  55. }
  56. $fmt_chunk = substr($rawdata, $offset, 4);
  57. if ($fmt_chunk !== 'fmt ') {
  58. $info['error'][] = 'Expected "fmt " ('.getid3_lib::PrintHexBytes('fmt ').') at offset '.$offset.', found "'.$fmt_chunk.'" ('.getid3_lib::PrintHexBytes($fmt_chunk).') instead.';
  59. return false;
  60. }
  61. $offset += 4;
  62. $fmt_size = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  63. $offset += 4;
  64. $info['la']['raw']['format'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
  65. $offset += 2;
  66. $info['la']['channels'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
  67. $offset += 2;
  68. if ($info['la']['channels'] == 0) {
  69. $info['error'][] = 'Corrupt LA file: channels == zero';
  70. return false;
  71. }
  72. $info['la']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  73. $offset += 4;
  74. if ($info['la']['sample_rate'] == 0) {
  75. $info['error'][] = 'Corrupt LA file: sample_rate == zero';
  76. return false;
  77. }
  78. $info['la']['bytes_per_second'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  79. $offset += 4;
  80. $info['la']['bytes_per_sample'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
  81. $offset += 2;
  82. $info['la']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
  83. $offset += 2;
  84. $info['la']['samples'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  85. $offset += 4;
  86. $info['la']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 1));
  87. $offset += 1;
  88. $info['la']['flags']['seekable'] = (bool) ($info['la']['raw']['flags'] & 0x01);
  89. if ($info['la']['version'] >= 0.4) {
  90. $info['la']['flags']['high_compression'] = (bool) ($info['la']['raw']['flags'] & 0x02);
  91. }
  92. $info['la']['original_crc'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  93. $offset += 4;
  94. // mikeØbevin*de
  95. // Basically, the blocksize/seekevery are 61440/19 in La0.4 and 73728/16
  96. // in earlier versions. A seekpoint is added every blocksize * seekevery
  97. // samples, so 4 * int(totalSamples / (blockSize * seekEvery)) should
  98. // give the number of bytes used for the seekpoints. Of course, if seeking
  99. // is disabled, there are no seekpoints stored.
  100. if ($info['la']['version'] >= 0.4) {
  101. $info['la']['blocksize'] = 61440;
  102. $info['la']['seekevery'] = 19;
  103. } else {
  104. $info['la']['blocksize'] = 73728;
  105. $info['la']['seekevery'] = 16;
  106. }
  107. $info['la']['seekpoint_count'] = 0;
  108. if ($info['la']['flags']['seekable']) {
  109. $info['la']['seekpoint_count'] = floor($info['la']['samples'] / ($info['la']['blocksize'] * $info['la']['seekevery']));
  110. for ($i = 0; $i < $info['la']['seekpoint_count']; $i++) {
  111. $info['la']['seekpoints'][] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  112. $offset += 4;
  113. }
  114. }
  115. if ($info['la']['version'] >= 0.3) {
  116. // Following the main header information, the program outputs all of the
  117. // seekpoints. Following these is what I called the 'footer start',
  118. // i.e. the position immediately after the La audio data is finished.
  119. $info['la']['footerstart'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
  120. $offset += 4;
  121. if ($info['la']['footerstart'] > $info['filesize']) {
  122. $info['warning'][] = 'FooterStart value points to offset '.$info['la']['footerstart'].' which is beyond end-of-file ('.$info['filesize'].')';
  123. $info['la']['footerstart'] = $info['filesize'];
  124. }
  125. } else {
  126. // La v0.2 didn't have FooterStart value
  127. $info['la']['footerstart'] = $info['avdataend'];
  128. }
  129. if ($info['la']['footerstart'] < $info['avdataend']) {
  130. if ($RIFFtempfilename = tempnam(GETID3_TEMP_DIR, 'id3')) {
  131. if ($RIFF_fp = fopen($RIFFtempfilename, 'w+b')) {
  132. $RIFFdata = 'WAVE';
  133. if ($info['la']['version'] == 0.2) {
  134. $RIFFdata .= substr($rawdata, 12, 24);
  135. } else {
  136. $RIFFdata .= substr($rawdata, 16, 24);
  137. }
  138. if ($info['la']['footerstart'] < $info['avdataend']) {
  139. $this->fseek($info['la']['footerstart']);
  140. $RIFFdata .= $this->fread($info['avdataend'] - $info['la']['footerstart']);
  141. }
  142. $RIFFdata = 'RIFF'.getid3_lib::LittleEndian2String(strlen($RIFFdata), 4, false).$RIFFdata;
  143. fwrite($RIFF_fp, $RIFFdata, strlen($RIFFdata));
  144. fclose($RIFF_fp);
  145. $getid3_temp = new getID3();
  146. $getid3_temp->openfile($RIFFtempfilename);
  147. $getid3_riff = new getid3_riff($getid3_temp);
  148. $getid3_riff->Analyze();
  149. if (empty($getid3_temp->info['error'])) {
  150. $info['riff'] = $getid3_temp->info['riff'];
  151. } else {
  152. $info['warning'][] = 'Error parsing RIFF portion of La file: '.implode($getid3_temp->info['error']);
  153. }
  154. unset($getid3_temp, $getid3_riff);
  155. }
  156. unlink($RIFFtempfilename);
  157. }
  158. }
  159. // $info['avdataoffset'] should be zero to begin with, but just in case it's not, include the addition anyway
  160. $info['avdataend'] = $info['avdataoffset'] + $info['la']['footerstart'];
  161. $info['avdataoffset'] = $info['avdataoffset'] + $offset;
  162. $info['la']['compression_ratio'] = (float) (($info['avdataend'] - $info['avdataoffset']) / $info['la']['uncompressed_size']);
  163. $info['playtime_seconds'] = (float) ($info['la']['samples'] / $info['la']['sample_rate']) / $info['la']['channels'];
  164. if ($info['playtime_seconds'] == 0) {
  165. $info['error'][] = 'Corrupt LA file: playtime_seconds == zero';
  166. return false;
  167. }
  168. $info['audio']['bitrate'] = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['playtime_seconds'];
  169. //$info['audio']['codec'] = $info['la']['codec'];
  170. $info['audio']['bits_per_sample'] = $info['la']['bits_per_sample'];
  171. break;
  172. default:
  173. if (substr($rawdata, $offset, 2) == 'LA') {
  174. $info['error'][] = 'This version of getID3() ['.$this->getid3->version().'] does not support LA version '.substr($rawdata, $offset + 2, 1).'.'.substr($rawdata, $offset + 3, 1).' which this appears to be - check http://getid3.sourceforge.net for updates.';
  175. } else {
  176. $info['error'][] = 'Not a LA (Lossless-Audio) file';
  177. }
  178. return false;
  179. break;
  180. }
  181. $info['audio']['channels'] = $info['la']['channels'];
  182. $info['audio']['sample_rate'] = (int) $info['la']['sample_rate'];
  183. $info['audio']['encoder'] = 'LA v'.$info['la']['version'];
  184. return true;
  185. }
  186. }