module.audio-video.nsv.php 8.8 KB

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