module.audio-video.nsv.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.nsv.php //
  11. // module for analyzing Nullsoft NSV 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_nsv extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. $this->fseek($info['avdataoffset']);
  26. $NSVheader = $this->fread(4);
  27. switch ($NSVheader) {
  28. case 'NSVs':
  29. if ($this->getNSVsHeaderFilepointer(0)) {
  30. $info['fileformat'] = 'nsv';
  31. $info['audio']['dataformat'] = 'nsv';
  32. $info['video']['dataformat'] = 'nsv';
  33. $info['audio']['lossless'] = false;
  34. $info['video']['lossless'] = false;
  35. }
  36. break;
  37. case 'NSVf':
  38. if ($this->getNSVfHeaderFilepointer(0)) {
  39. $info['fileformat'] = 'nsv';
  40. $info['audio']['dataformat'] = 'nsv';
  41. $info['video']['dataformat'] = 'nsv';
  42. $info['audio']['lossless'] = false;
  43. $info['video']['lossless'] = false;
  44. $this->getNSVsHeaderFilepointer($info['nsv']['NSVf']['header_length']);
  45. }
  46. break;
  47. default:
  48. $this->error('Expecting "NSVs" or "NSVf" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($NSVheader).'"');
  49. return false;
  50. }
  51. if (!isset($info['nsv']['NSVf'])) {
  52. $this->warning('NSVf header not present - cannot calculate playtime or bitrate');
  53. }
  54. return true;
  55. }
  56. /**
  57. * @param int $fileoffset
  58. *
  59. * @return bool
  60. */
  61. public function getNSVsHeaderFilepointer($fileoffset) {
  62. $info = &$this->getid3->info;
  63. $this->fseek($fileoffset);
  64. $NSVsheader = $this->fread(28);
  65. $offset = 0;
  66. $info['nsv']['NSVs']['identifier'] = substr($NSVsheader, $offset, 4);
  67. $offset += 4;
  68. if ($info['nsv']['NSVs']['identifier'] != 'NSVs') {
  69. $this->error('expected "NSVs" at offset ('.$fileoffset.'), found "'.$info['nsv']['NSVs']['identifier'].'" instead');
  70. unset($info['nsv']['NSVs']);
  71. return false;
  72. }
  73. $info['nsv']['NSVs']['offset'] = $fileoffset;
  74. $info['nsv']['NSVs']['video_codec'] = substr($NSVsheader, $offset, 4);
  75. $offset += 4;
  76. $info['nsv']['NSVs']['audio_codec'] = substr($NSVsheader, $offset, 4);
  77. $offset += 4;
  78. $info['nsv']['NSVs']['resolution_x'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 2));
  79. $offset += 2;
  80. $info['nsv']['NSVs']['resolution_y'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 2));
  81. $offset += 2;
  82. $info['nsv']['NSVs']['framerate_index'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  83. $offset += 1;
  84. //$info['nsv']['NSVs']['unknown1b'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  85. $offset += 1;
  86. //$info['nsv']['NSVs']['unknown1c'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  87. $offset += 1;
  88. //$info['nsv']['NSVs']['unknown1d'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  89. $offset += 1;
  90. //$info['nsv']['NSVs']['unknown2a'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  91. $offset += 1;
  92. //$info['nsv']['NSVs']['unknown2b'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  93. $offset += 1;
  94. //$info['nsv']['NSVs']['unknown2c'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  95. $offset += 1;
  96. //$info['nsv']['NSVs']['unknown2d'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  97. $offset += 1;
  98. switch ($info['nsv']['NSVs']['audio_codec']) {
  99. case 'PCM ':
  100. $info['nsv']['NSVs']['bits_channel'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  101. $offset += 1;
  102. $info['nsv']['NSVs']['channels'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
  103. $offset += 1;
  104. $info['nsv']['NSVs']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 2));
  105. $offset += 2;
  106. $info['audio']['sample_rate'] = $info['nsv']['NSVs']['sample_rate'];
  107. break;
  108. case 'MP3 ':
  109. case 'NONE':
  110. default:
  111. //$info['nsv']['NSVs']['unknown3'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 4));
  112. $offset += 4;
  113. break;
  114. }
  115. $info['video']['resolution_x'] = $info['nsv']['NSVs']['resolution_x'];
  116. $info['video']['resolution_y'] = $info['nsv']['NSVs']['resolution_y'];
  117. $info['nsv']['NSVs']['frame_rate'] = $this->NSVframerateLookup($info['nsv']['NSVs']['framerate_index']);
  118. $info['video']['frame_rate'] = $info['nsv']['NSVs']['frame_rate'];
  119. $info['video']['bits_per_sample'] = 24;
  120. $info['video']['pixel_aspect_ratio'] = (float) 1;
  121. return true;
  122. }
  123. /**
  124. * @param int $fileoffset
  125. * @param bool $getTOCoffsets
  126. *
  127. * @return bool
  128. */
  129. public function getNSVfHeaderFilepointer($fileoffset, $getTOCoffsets=false) {
  130. $info = &$this->getid3->info;
  131. $this->fseek($fileoffset);
  132. $NSVfheader = $this->fread(28);
  133. $offset = 0;
  134. $info['nsv']['NSVf']['identifier'] = substr($NSVfheader, $offset, 4);
  135. $offset += 4;
  136. if ($info['nsv']['NSVf']['identifier'] != 'NSVf') {
  137. $this->error('expected "NSVf" at offset ('.$fileoffset.'), found "'.$info['nsv']['NSVf']['identifier'].'" instead');
  138. unset($info['nsv']['NSVf']);
  139. return false;
  140. }
  141. $info['nsv']['NSVs']['offset'] = $fileoffset;
  142. $info['nsv']['NSVf']['header_length'] = getid3_lib::LittleEndian2Int(substr($NSVfheader, $offset, 4));
  143. $offset += 4;
  144. $info['nsv']['NSVf']['file_size'] = getid3_lib::LittleEndian2Int(substr($NSVfheader, $offset, 4));
  145. $offset += 4;
  146. if ($info['nsv']['NSVf']['file_size'] > $info['avdataend']) {
  147. $this->warning('truncated file - NSVf header indicates '.$info['nsv']['NSVf']['file_size'].' bytes, file actually '.$info['avdataend'].' bytes');
  148. }
  149. $info['nsv']['NSVf']['playtime_ms'] = getid3_lib::LittleEndian2Int(substr($NSVfheader, $offset, 4));
  150. $offset += 4;
  151. $info['nsv']['NSVf']['meta_size'] = getid3_lib::LittleEndian2Int(substr($NSVfheader, $offset, 4));
  152. $offset += 4;
  153. $info['nsv']['NSVf']['TOC_entries_1'] = getid3_lib::LittleEndian2Int(substr($NSVfheader, $offset, 4));
  154. $offset += 4;
  155. $info['nsv']['NSVf']['TOC_entries_2'] = getid3_lib::LittleEndian2Int(substr($NSVfheader, $offset, 4));
  156. $offset += 4;
  157. if ($info['nsv']['NSVf']['playtime_ms'] == 0) {
  158. $this->error('Corrupt NSV file: NSVf.playtime_ms == zero');
  159. return false;
  160. }
  161. $NSVfheader .= $this->fread($info['nsv']['NSVf']['meta_size'] + (4 * $info['nsv']['NSVf']['TOC_entries_1']) + (4 * $info['nsv']['NSVf']['TOC_entries_2']));
  162. $NSVfheaderlength = strlen($NSVfheader);
  163. $info['nsv']['NSVf']['metadata'] = substr($NSVfheader, $offset, $info['nsv']['NSVf']['meta_size']);
  164. $offset += $info['nsv']['NSVf']['meta_size'];
  165. if ($getTOCoffsets) {
  166. $TOCcounter = 0;
  167. while ($TOCcounter < $info['nsv']['NSVf']['TOC_entries_1']) {
  168. if ($TOCcounter < $info['nsv']['NSVf']['TOC_entries_1']) {
  169. $info['nsv']['NSVf']['TOC_1'][$TOCcounter] = getid3_lib::LittleEndian2Int(substr($NSVfheader, $offset, 4));
  170. $offset += 4;
  171. $TOCcounter++;
  172. }
  173. }
  174. }
  175. if (trim($info['nsv']['NSVf']['metadata']) != '') {
  176. $info['nsv']['NSVf']['metadata'] = str_replace('`', "\x01", $info['nsv']['NSVf']['metadata']);
  177. $CommentPairArray = explode("\x01".' ', $info['nsv']['NSVf']['metadata']);
  178. foreach ($CommentPairArray as $CommentPair) {
  179. if (strstr($CommentPair, '='."\x01")) {
  180. list($key, $value) = explode('='."\x01", $CommentPair, 2);
  181. $info['nsv']['comments'][strtolower($key)][] = trim(str_replace("\x01", '', $value));
  182. }
  183. }
  184. }
  185. $info['playtime_seconds'] = $info['nsv']['NSVf']['playtime_ms'] / 1000;
  186. $info['bitrate'] = ($info['nsv']['NSVf']['file_size'] * 8) / $info['playtime_seconds'];
  187. return true;
  188. }
  189. /**
  190. * @param int $framerateindex
  191. *
  192. * @return float|false
  193. */
  194. public static function NSVframerateLookup($framerateindex) {
  195. if ($framerateindex <= 127) {
  196. return (float) $framerateindex;
  197. }
  198. static $NSVframerateLookup = array();
  199. if (empty($NSVframerateLookup)) {
  200. $NSVframerateLookup[129] = 29.970;
  201. $NSVframerateLookup[131] = 23.976;
  202. $NSVframerateLookup[133] = 14.985;
  203. $NSVframerateLookup[197] = 59.940;
  204. $NSVframerateLookup[199] = 47.952;
  205. }
  206. return (isset($NSVframerateLookup[$framerateindex]) ? $NSVframerateLookup[$framerateindex] : false);
  207. }
  208. }