module.audio.la.php 9.1 KB

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