module.audio.vqf.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.vqf.php //
  11. // module for analyzing VQF audio files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. class getid3_vqf extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. // based loosely on code from TTwinVQ by Jurgen Faul <jfaulØgmx*de>
  26. // http://jfaul.de/atl or http://j-faul.virtualave.net/atl/atl.html
  27. $info['fileformat'] = 'vqf';
  28. $info['audio']['dataformat'] = 'vqf';
  29. $info['audio']['bitrate_mode'] = 'cbr';
  30. $info['audio']['lossless'] = false;
  31. // shortcut
  32. $info['vqf']['raw'] = array();
  33. $thisfile_vqf = &$info['vqf'];
  34. $thisfile_vqf_raw = &$thisfile_vqf['raw'];
  35. $this->fseek($info['avdataoffset']);
  36. $VQFheaderData = $this->fread(16);
  37. $offset = 0;
  38. $thisfile_vqf_raw['header_tag'] = substr($VQFheaderData, $offset, 4);
  39. $magic = 'TWIN';
  40. if ($thisfile_vqf_raw['header_tag'] != $magic) {
  41. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_vqf_raw['header_tag']).'"');
  42. unset($info['vqf']);
  43. unset($info['fileformat']);
  44. return false;
  45. }
  46. $offset += 4;
  47. $thisfile_vqf_raw['version'] = substr($VQFheaderData, $offset, 8);
  48. $offset += 8;
  49. $thisfile_vqf_raw['size'] = getid3_lib::BigEndian2Int(substr($VQFheaderData, $offset, 4));
  50. $offset += 4;
  51. while ($this->ftell() < $info['avdataend']) {
  52. $ChunkBaseOffset = $this->ftell();
  53. $chunkoffset = 0;
  54. $ChunkData = $this->fread(8);
  55. $ChunkName = substr($ChunkData, $chunkoffset, 4);
  56. if ($ChunkName == 'DATA') {
  57. $info['avdataoffset'] = $ChunkBaseOffset;
  58. break;
  59. }
  60. $chunkoffset += 4;
  61. $ChunkSize = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  62. $chunkoffset += 4;
  63. if ($ChunkSize > ($info['avdataend'] - $this->ftell())) {
  64. $this->error('Invalid chunk size ('.$ChunkSize.') for chunk "'.$ChunkName.'" at offset '.$ChunkBaseOffset);
  65. break;
  66. }
  67. if ($ChunkSize > 0) {
  68. $ChunkData .= $this->fread($ChunkSize);
  69. }
  70. switch ($ChunkName) {
  71. case 'COMM':
  72. // shortcut
  73. $thisfile_vqf['COMM'] = array();
  74. $thisfile_vqf_COMM = &$thisfile_vqf['COMM'];
  75. $thisfile_vqf_COMM['channel_mode'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  76. $chunkoffset += 4;
  77. $thisfile_vqf_COMM['bitrate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  78. $chunkoffset += 4;
  79. $thisfile_vqf_COMM['sample_rate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  80. $chunkoffset += 4;
  81. $thisfile_vqf_COMM['security_level'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  82. $chunkoffset += 4;
  83. $info['audio']['channels'] = $thisfile_vqf_COMM['channel_mode'] + 1;
  84. $info['audio']['sample_rate'] = $this->VQFchannelFrequencyLookup($thisfile_vqf_COMM['sample_rate']);
  85. $info['audio']['bitrate'] = $thisfile_vqf_COMM['bitrate'] * 1000;
  86. $info['audio']['encoder_options'] = 'CBR' . ceil($info['audio']['bitrate']/1000);
  87. if ($info['audio']['bitrate'] == 0) {
  88. $this->error('Corrupt VQF file: bitrate_audio == zero');
  89. return false;
  90. }
  91. break;
  92. case 'NAME':
  93. case 'AUTH':
  94. case '(c) ':
  95. case 'FILE':
  96. case 'COMT':
  97. case 'ALBM':
  98. $thisfile_vqf['comments'][$this->VQFcommentNiceNameLookup($ChunkName)][] = trim(substr($ChunkData, 8));
  99. break;
  100. case 'DSIZ':
  101. $thisfile_vqf['DSIZ'] = getid3_lib::BigEndian2Int(substr($ChunkData, 8, 4));
  102. break;
  103. default:
  104. $this->warning('Unhandled chunk type "'.$ChunkName.'" at offset '.$ChunkBaseOffset);
  105. break;
  106. }
  107. }
  108. $info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate'];
  109. if (isset($thisfile_vqf['DSIZ']) && (($thisfile_vqf['DSIZ'] != ($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'))))) {
  110. switch ($thisfile_vqf['DSIZ']) {
  111. case 0:
  112. case 1:
  113. $this->warning('Invalid DSIZ value "'.$thisfile_vqf['DSIZ'].'". This is known to happen with VQF files encoded by Ahead Nero, and seems to be its way of saying this is TwinVQF v'.($thisfile_vqf['DSIZ'] + 1).'.0');
  114. $info['audio']['encoder'] = 'Ahead Nero';
  115. break;
  116. default:
  117. $this->warning('Probable corrupted file - should be '.$thisfile_vqf['DSIZ'].' bytes, actually '.($info['avdataend'] - $info['avdataoffset'] - strlen('DATA')));
  118. break;
  119. }
  120. }
  121. return true;
  122. }
  123. /**
  124. * @param int $frequencyid
  125. *
  126. * @return int
  127. */
  128. public function VQFchannelFrequencyLookup($frequencyid) {
  129. static $VQFchannelFrequencyLookup = array(
  130. 11 => 11025,
  131. 22 => 22050,
  132. 44 => 44100
  133. );
  134. return (isset($VQFchannelFrequencyLookup[$frequencyid]) ? $VQFchannelFrequencyLookup[$frequencyid] : $frequencyid * 1000);
  135. }
  136. /**
  137. * @param string $shortname
  138. *
  139. * @return string
  140. */
  141. public function VQFcommentNiceNameLookup($shortname) {
  142. static $VQFcommentNiceNameLookup = array(
  143. 'NAME' => 'title',
  144. 'AUTH' => 'artist',
  145. '(c) ' => 'copyright',
  146. 'FILE' => 'filename',
  147. 'COMT' => 'comment',
  148. 'ALBM' => 'album'
  149. );
  150. return (isset($VQFcommentNiceNameLookup[$shortname]) ? $VQFcommentNiceNameLookup[$shortname] : $shortname);
  151. }
  152. }