module.audio-video.nsv.php 8.8 KB

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