PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/ID3/module.audio.dts.php

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