module.audio.dts.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.dts.php //
  11. // module for analyzing DTS 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. /**
  19. * @tutorial http://wiki.multimedia.cx/index.php?title=DTS
  20. */
  21. class getid3_dts extends getid3_handler
  22. {
  23. /**
  24. * Default DTS syncword used in native .cpt or .dts formats.
  25. */
  26. const syncword = "\x7F\xFE\x80\x01";
  27. /**
  28. * @var int
  29. */
  30. private $readBinDataOffset = 0;
  31. /**
  32. * Possible syncwords indicating bitstream encoding.
  33. */
  34. public static $syncwords = array(
  35. 0 => "\x7F\xFE\x80\x01", // raw big-endian
  36. 1 => "\xFE\x7F\x01\x80", // raw little-endian
  37. 2 => "\x1F\xFF\xE8\x00", // 14-bit big-endian
  38. 3 => "\xFF\x1F\x00\xE8"); // 14-bit little-endian
  39. /**
  40. * @return bool
  41. */
  42. public function Analyze() {
  43. $info = &$this->getid3->info;
  44. $info['fileformat'] = 'dts';
  45. $this->fseek($info['avdataoffset']);
  46. $DTSheader = $this->fread(20); // we only need 2 words magic + 6 words frame header, but these words may be normal 16-bit words OR 14-bit words with 2 highest bits set to zero, so 8 words can be either 8*16/8 = 16 bytes OR 8*16*(16/14)/8 = 18.3 bytes
  47. // check syncword
  48. $sync = substr($DTSheader, 0, 4);
  49. if (($encoding = array_search($sync, self::$syncwords)) !== false) {
  50. $info['dts']['raw']['magic'] = $sync;
  51. $this->readBinDataOffset = 32;
  52. } elseif ($this->isDependencyFor('matroska')) {
  53. // Matroska contains DTS without syncword encoded as raw big-endian format
  54. $encoding = 0;
  55. $this->readBinDataOffset = 0;
  56. } else {
  57. unset($info['fileformat']);
  58. return $this->error('Expecting "'.implode('| ', array_map('getid3_lib::PrintHexBytes', self::$syncwords)).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($sync).'"');
  59. }
  60. // decode header
  61. $fhBS = '';
  62. for ($word_offset = 0; $word_offset <= strlen($DTSheader); $word_offset += 2) {
  63. switch ($encoding) {
  64. case 0: // raw big-endian
  65. $fhBS .= getid3_lib::BigEndian2Bin( substr($DTSheader, $word_offset, 2) );
  66. break;
  67. case 1: // raw little-endian
  68. $fhBS .= getid3_lib::BigEndian2Bin(strrev(substr($DTSheader, $word_offset, 2)));
  69. break;
  70. case 2: // 14-bit big-endian
  71. $fhBS .= substr(getid3_lib::BigEndian2Bin( substr($DTSheader, $word_offset, 2) ), 2, 14);
  72. break;
  73. case 3: // 14-bit little-endian
  74. $fhBS .= substr(getid3_lib::BigEndian2Bin(strrev(substr($DTSheader, $word_offset, 2))), 2, 14);
  75. break;
  76. }
  77. }
  78. $info['dts']['raw']['frame_type'] = $this->readBinData($fhBS, 1);
  79. $info['dts']['raw']['deficit_samples'] = $this->readBinData($fhBS, 5);
  80. $info['dts']['flags']['crc_present'] = (bool) $this->readBinData($fhBS, 1);
  81. $info['dts']['raw']['pcm_sample_blocks'] = $this->readBinData($fhBS, 7);
  82. $info['dts']['raw']['frame_byte_size'] = $this->readBinData($fhBS, 14);
  83. $info['dts']['raw']['channel_arrangement'] = $this->readBinData($fhBS, 6);
  84. $info['dts']['raw']['sample_frequency'] = $this->readBinData($fhBS, 4);
  85. $info['dts']['raw']['bitrate'] = $this->readBinData($fhBS, 5);
  86. $info['dts']['flags']['embedded_downmix'] = (bool) $this->readBinData($fhBS, 1);
  87. $info['dts']['flags']['dynamicrange'] = (bool) $this->readBinData($fhBS, 1);
  88. $info['dts']['flags']['timestamp'] = (bool) $this->readBinData($fhBS, 1);
  89. $info['dts']['flags']['auxdata'] = (bool) $this->readBinData($fhBS, 1);
  90. $info['dts']['flags']['hdcd'] = (bool) $this->readBinData($fhBS, 1);
  91. $info['dts']['raw']['extension_audio'] = $this->readBinData($fhBS, 3);
  92. $info['dts']['flags']['extended_coding'] = (bool) $this->readBinData($fhBS, 1);
  93. $info['dts']['flags']['audio_sync_insertion'] = (bool) $this->readBinData($fhBS, 1);
  94. $info['dts']['raw']['lfe_effects'] = $this->readBinData($fhBS, 2);
  95. $info['dts']['flags']['predictor_history'] = (bool) $this->readBinData($fhBS, 1);
  96. if ($info['dts']['flags']['crc_present']) {
  97. $info['dts']['raw']['crc16'] = $this->readBinData($fhBS, 16);
  98. }
  99. $info['dts']['flags']['mri_perfect_reconst'] = (bool) $this->readBinData($fhBS, 1);
  100. $info['dts']['raw']['encoder_soft_version'] = $this->readBinData($fhBS, 4);
  101. $info['dts']['raw']['copy_history'] = $this->readBinData($fhBS, 2);
  102. $info['dts']['raw']['bits_per_sample'] = $this->readBinData($fhBS, 2);
  103. $info['dts']['flags']['surround_es'] = (bool) $this->readBinData($fhBS, 1);
  104. $info['dts']['flags']['front_sum_diff'] = (bool) $this->readBinData($fhBS, 1);
  105. $info['dts']['flags']['surround_sum_diff'] = (bool) $this->readBinData($fhBS, 1);
  106. $info['dts']['raw']['dialog_normalization'] = $this->readBinData($fhBS, 4);
  107. $info['dts']['bitrate'] = self::bitrateLookup($info['dts']['raw']['bitrate']);
  108. $info['dts']['bits_per_sample'] = self::bitPerSampleLookup($info['dts']['raw']['bits_per_sample']);
  109. $info['dts']['sample_rate'] = self::sampleRateLookup($info['dts']['raw']['sample_frequency']);
  110. $info['dts']['dialog_normalization'] = self::dialogNormalization($info['dts']['raw']['dialog_normalization'], $info['dts']['raw']['encoder_soft_version']);
  111. $info['dts']['flags']['lossless'] = (($info['dts']['raw']['bitrate'] == 31) ? true : false);
  112. $info['dts']['bitrate_mode'] = (($info['dts']['raw']['bitrate'] == 30) ? 'vbr' : 'cbr');
  113. $info['dts']['channels'] = self::numChannelsLookup($info['dts']['raw']['channel_arrangement']);
  114. $info['dts']['channel_arrangement'] = self::channelArrangementLookup($info['dts']['raw']['channel_arrangement']);
  115. $info['audio']['dataformat'] = 'dts';
  116. $info['audio']['lossless'] = $info['dts']['flags']['lossless'];
  117. $info['audio']['bitrate_mode'] = $info['dts']['bitrate_mode'];
  118. $info['audio']['bits_per_sample'] = $info['dts']['bits_per_sample'];
  119. $info['audio']['sample_rate'] = $info['dts']['sample_rate'];
  120. $info['audio']['channels'] = $info['dts']['channels'];
  121. $info['audio']['bitrate'] = $info['dts']['bitrate'];
  122. if (isset($info['avdataend']) && !empty($info['dts']['bitrate']) && is_numeric($info['dts']['bitrate'])) {
  123. $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) / ($info['dts']['bitrate'] / 8);
  124. if (($encoding == 2) || ($encoding == 3)) {
  125. // 14-bit data packed into 16-bit words, so the playtime is wrong because only (14/16) of the bytes in the data portion of the file are used at the specified bitrate
  126. $info['playtime_seconds'] *= (14 / 16);
  127. }
  128. }
  129. return true;
  130. }
  131. /**
  132. * @param string $bin
  133. * @param int $length
  134. *
  135. * @return int
  136. */
  137. private function readBinData($bin, $length) {
  138. $data = substr($bin, $this->readBinDataOffset, $length);
  139. $this->readBinDataOffset += $length;
  140. return bindec($data);
  141. }
  142. /**
  143. * @param int $index
  144. *
  145. * @return int|string|false
  146. */
  147. public static function bitrateLookup($index) {
  148. static $lookup = array(
  149. 0 => 32000,
  150. 1 => 56000,
  151. 2 => 64000,
  152. 3 => 96000,
  153. 4 => 112000,
  154. 5 => 128000,
  155. 6 => 192000,
  156. 7 => 224000,
  157. 8 => 256000,
  158. 9 => 320000,
  159. 10 => 384000,
  160. 11 => 448000,
  161. 12 => 512000,
  162. 13 => 576000,
  163. 14 => 640000,
  164. 15 => 768000,
  165. 16 => 960000,
  166. 17 => 1024000,
  167. 18 => 1152000,
  168. 19 => 1280000,
  169. 20 => 1344000,
  170. 21 => 1408000,
  171. 22 => 1411200,
  172. 23 => 1472000,
  173. 24 => 1536000,
  174. 25 => 1920000,
  175. 26 => 2048000,
  176. 27 => 3072000,
  177. 28 => 3840000,
  178. 29 => 'open',
  179. 30 => 'variable',
  180. 31 => 'lossless',
  181. );
  182. return (isset($lookup[$index]) ? $lookup[$index] : false);
  183. }
  184. /**
  185. * @param int $index
  186. *
  187. * @return int|string|false
  188. */
  189. public static function sampleRateLookup($index) {
  190. static $lookup = array(
  191. 0 => 'invalid',
  192. 1 => 8000,
  193. 2 => 16000,
  194. 3 => 32000,
  195. 4 => 'invalid',
  196. 5 => 'invalid',
  197. 6 => 11025,
  198. 7 => 22050,
  199. 8 => 44100,
  200. 9 => 'invalid',
  201. 10 => 'invalid',
  202. 11 => 12000,
  203. 12 => 24000,
  204. 13 => 48000,
  205. 14 => 'invalid',
  206. 15 => 'invalid',
  207. );
  208. return (isset($lookup[$index]) ? $lookup[$index] : false);
  209. }
  210. /**
  211. * @param int $index
  212. *
  213. * @return int|false
  214. */
  215. public static function bitPerSampleLookup($index) {
  216. static $lookup = array(
  217. 0 => 16,
  218. 1 => 20,
  219. 2 => 24,
  220. 3 => 24,
  221. );
  222. return (isset($lookup[$index]) ? $lookup[$index] : false);
  223. }
  224. /**
  225. * @param int $index
  226. *
  227. * @return int|false
  228. */
  229. public static function numChannelsLookup($index) {
  230. switch ($index) {
  231. case 0:
  232. return 1;
  233. case 1:
  234. case 2:
  235. case 3:
  236. case 4:
  237. return 2;
  238. case 5:
  239. case 6:
  240. return 3;
  241. case 7:
  242. case 8:
  243. return 4;
  244. case 9:
  245. return 5;
  246. case 10:
  247. case 11:
  248. case 12:
  249. return 6;
  250. case 13:
  251. return 7;
  252. case 14:
  253. case 15:
  254. return 8;
  255. }
  256. return false;
  257. }
  258. /**
  259. * @param int $index
  260. *
  261. * @return string
  262. */
  263. public static function channelArrangementLookup($index) {
  264. static $lookup = array(
  265. 0 => 'A',
  266. 1 => 'A + B (dual mono)',
  267. 2 => 'L + R (stereo)',
  268. 3 => '(L+R) + (L-R) (sum-difference)',
  269. 4 => 'LT + RT (left and right total)',
  270. 5 => 'C + L + R',
  271. 6 => 'L + R + S',
  272. 7 => 'C + L + R + S',
  273. 8 => 'L + R + SL + SR',
  274. 9 => 'C + L + R + SL + SR',
  275. 10 => 'CL + CR + L + R + SL + SR',
  276. 11 => 'C + L + R+ LR + RR + OV',
  277. 12 => 'CF + CR + LF + RF + LR + RR',
  278. 13 => 'CL + C + CR + L + R + SL + SR',
  279. 14 => 'CL + CR + L + R + SL1 + SL2 + SR1 + SR2',
  280. 15 => 'CL + C+ CR + L + R + SL + S + SR',
  281. );
  282. return (isset($lookup[$index]) ? $lookup[$index] : 'user-defined');
  283. }
  284. /**
  285. * @param int $index
  286. * @param int $version
  287. *
  288. * @return int|false
  289. */
  290. public static function dialogNormalization($index, $version) {
  291. switch ($version) {
  292. case 7:
  293. return 0 - $index;
  294. case 6:
  295. return 0 - 16 - $index;
  296. }
  297. return false;
  298. }
  299. }