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