module.audio.dts.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.dts.php //
  11. // module for analyzing DTS Audio files //
  12. // dependencies: NONE //
  13. // //
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_dts extends getid3_handler
  16. {
  17. public function Analyze() {
  18. $info = &$this->getid3->info;
  19. // Specs taken from "DTS Coherent Acoustics;Core and Extensions, ETSI TS 102 114 V1.2.1 (2002-12)"
  20. // (http://pda.etsi.org/pda/queryform.asp)
  21. // With thanks to Gambit <macteam@users.sourceforge.net> http://mac.sourceforge.net/atl/
  22. $info['fileformat'] = 'dts';
  23. fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
  24. $DTSheader = fread($this->getid3->fp, 16);
  25. $info['dts']['raw']['magic'] = substr($DTSheader, 0, 4);
  26. $magic = "\x7F\xFE\x80\x01";
  27. if ($info['dts']['raw']['magic'] != $magic) {
  28. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['dts']['raw']['magic']).'"';
  29. unset($info['fileformat'], $info['dts']);
  30. return false;
  31. }
  32. $fhBS = getid3_lib::BigEndian2Bin(substr($DTSheader, 4, 12));
  33. $bsOffset = 0;
  34. $info['dts']['raw']['frame_type'] = $this->readBinData($fhBS, $bsOffset, 1);
  35. $info['dts']['raw']['deficit_samples'] = $this->readBinData($fhBS, $bsOffset, 5);
  36. $info['dts']['flags']['crc_present'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  37. $info['dts']['raw']['pcm_sample_blocks'] = $this->readBinData($fhBS, $bsOffset, 7);
  38. $info['dts']['raw']['frame_byte_size'] = $this->readBinData($fhBS, $bsOffset, 14);
  39. $info['dts']['raw']['channel_arrangement'] = $this->readBinData($fhBS, $bsOffset, 6);
  40. $info['dts']['raw']['sample_frequency'] = $this->readBinData($fhBS, $bsOffset, 4);
  41. $info['dts']['raw']['bitrate'] = $this->readBinData($fhBS, $bsOffset, 5);
  42. $info['dts']['flags']['embedded_downmix'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  43. $info['dts']['flags']['dynamicrange'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  44. $info['dts']['flags']['timestamp'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  45. $info['dts']['flags']['auxdata'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  46. $info['dts']['flags']['hdcd'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  47. $info['dts']['raw']['extension_audio'] = $this->readBinData($fhBS, $bsOffset, 3);
  48. $info['dts']['flags']['extended_coding'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  49. $info['dts']['flags']['audio_sync_insertion'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  50. $info['dts']['raw']['lfe_effects'] = $this->readBinData($fhBS, $bsOffset, 2);
  51. $info['dts']['flags']['predictor_history'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  52. if ($info['dts']['flags']['crc_present']) {
  53. $info['dts']['raw']['crc16'] = $this->readBinData($fhBS, $bsOffset, 16);
  54. }
  55. $info['dts']['flags']['mri_perfect_reconst'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  56. $info['dts']['raw']['encoder_soft_version'] = $this->readBinData($fhBS, $bsOffset, 4);
  57. $info['dts']['raw']['copy_history'] = $this->readBinData($fhBS, $bsOffset, 2);
  58. $info['dts']['raw']['bits_per_sample'] = $this->readBinData($fhBS, $bsOffset, 2);
  59. $info['dts']['flags']['surround_es'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  60. $info['dts']['flags']['front_sum_diff'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  61. $info['dts']['flags']['surround_sum_diff'] = (bool) $this->readBinData($fhBS, $bsOffset, 1);
  62. $info['dts']['raw']['dialog_normalization'] = $this->readBinData($fhBS, $bsOffset, 4);
  63. $info['dts']['bitrate'] = self::DTSbitrateLookup($info['dts']['raw']['bitrate']);
  64. $info['dts']['bits_per_sample'] = self::DTSbitPerSampleLookup($info['dts']['raw']['bits_per_sample']);
  65. $info['dts']['sample_rate'] = self::DTSsampleRateLookup($info['dts']['raw']['sample_frequency']);
  66. $info['dts']['dialog_normalization'] = self::DTSdialogNormalization($info['dts']['raw']['dialog_normalization'], $info['dts']['raw']['encoder_soft_version']);
  67. $info['dts']['flags']['lossless'] = (($info['dts']['raw']['bitrate'] == 31) ? true : false);
  68. $info['dts']['bitrate_mode'] = (($info['dts']['raw']['bitrate'] == 30) ? 'vbr' : 'cbr');
  69. $info['dts']['channels'] = self::DTSnumChannelsLookup($info['dts']['raw']['channel_arrangement']);
  70. $info['dts']['channel_arrangement'] = self::DTSchannelArrangementLookup($info['dts']['raw']['channel_arrangement']);
  71. $info['audio']['dataformat'] = 'dts';
  72. $info['audio']['lossless'] = $info['dts']['flags']['lossless'];
  73. $info['audio']['bitrate_mode'] = $info['dts']['bitrate_mode'];
  74. $info['audio']['bits_per_sample'] = $info['dts']['bits_per_sample'];
  75. $info['audio']['sample_rate'] = $info['dts']['sample_rate'];
  76. $info['audio']['channels'] = $info['dts']['channels'];
  77. $info['audio']['bitrate'] = $info['dts']['bitrate'];
  78. if (isset($info['avdataend'])) {
  79. $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) / ($info['dts']['bitrate'] / 8);
  80. }
  81. return true;
  82. }
  83. private function readBinData($bin, &$offset, $length) {
  84. $data = substr($bin, $offset, $length);
  85. $offset += $length;
  86. return bindec($data);
  87. }
  88. private static function DTSbitrateLookup($index) {
  89. $DTSbitrateLookup = array(
  90. 0 => 32000,
  91. 1 => 56000,
  92. 2 => 64000,
  93. 3 => 96000,
  94. 4 => 112000,
  95. 5 => 128000,
  96. 6 => 192000,
  97. 7 => 224000,
  98. 8 => 256000,
  99. 9 => 320000,
  100. 10 => 384000,
  101. 11 => 448000,
  102. 12 => 512000,
  103. 13 => 576000,
  104. 14 => 640000,
  105. 15 => 768000,
  106. 16 => 960000,
  107. 17 => 1024000,
  108. 18 => 1152000,
  109. 19 => 1280000,
  110. 20 => 1344000,
  111. 21 => 1408000,
  112. 22 => 1411200,
  113. 23 => 1472000,
  114. 24 => 1536000,
  115. 25 => 1920000,
  116. 26 => 2048000,
  117. 27 => 3072000,
  118. 28 => 3840000,
  119. 29 => 'open',
  120. 30 => 'variable',
  121. 31 => 'lossless'
  122. );
  123. return (isset($DTSbitrateLookup[$index]) ? $DTSbitrateLookup[$index] : false);
  124. }
  125. private static function DTSsampleRateLookup($index) {
  126. $DTSsampleRateLookup = array(
  127. 0 => 'invalid',
  128. 1 => 8000,
  129. 2 => 16000,
  130. 3 => 32000,
  131. 4 => 'invalid',
  132. 5 => 'invalid',
  133. 6 => 11025,
  134. 7 => 22050,
  135. 8 => 44100,
  136. 9 => 'invalid',
  137. 10 => 'invalid',
  138. 11 => 12000,
  139. 12 => 24000,
  140. 13 => 48000,
  141. 14 => 'invalid',
  142. 15 => 'invalid'
  143. );
  144. return (isset($DTSsampleRateLookup[$index]) ? $DTSsampleRateLookup[$index] : false);
  145. }
  146. private static function DTSbitPerSampleLookup($index) {
  147. $DTSbitPerSampleLookup = array(
  148. 0 => 16,
  149. 1 => 20,
  150. 2 => 24,
  151. 3 => 24,
  152. );
  153. return (isset($DTSbitPerSampleLookup[$index]) ? $DTSbitPerSampleLookup[$index] : false);
  154. }
  155. private static function DTSnumChannelsLookup($index) {
  156. switch ($index) {
  157. case 0:
  158. return 1;
  159. break;
  160. case 1:
  161. case 2:
  162. case 3:
  163. case 4:
  164. return 2;
  165. break;
  166. case 5:
  167. case 6:
  168. return 3;
  169. break;
  170. case 7:
  171. case 8:
  172. return 4;
  173. break;
  174. case 9:
  175. return 5;
  176. break;
  177. case 10:
  178. case 11:
  179. case 12:
  180. return 6;
  181. break;
  182. case 13:
  183. return 7;
  184. break;
  185. case 14:
  186. case 15:
  187. return 8;
  188. break;
  189. }
  190. return false;
  191. }
  192. private static function DTSchannelArrangementLookup($index) {
  193. $DTSchannelArrangementLookup = array(
  194. 0 => 'A',
  195. 1 => 'A + B (dual mono)',
  196. 2 => 'L + R (stereo)',
  197. 3 => '(L+R) + (L-R) (sum-difference)',
  198. 4 => 'LT + RT (left and right total)',
  199. 5 => 'C + L + R',
  200. 6 => 'L + R + S',
  201. 7 => 'C + L + R + S',
  202. 8 => 'L + R + SL + SR',
  203. 9 => 'C + L + R + SL + SR',
  204. 10 => 'CL + CR + L + R + SL + SR',
  205. 11 => 'C + L + R+ LR + RR + OV',
  206. 12 => 'CF + CR + LF + RF + LR + RR',
  207. 13 => 'CL + C + CR + L + R + SL + SR',
  208. 14 => 'CL + CR + L + R + SL1 + SL2 + SR1 + SR2',
  209. 15 => 'CL + C+ CR + L + R + SL + S + SR',
  210. );
  211. return (isset($DTSchannelArrangementLookup[$index]) ? $DTSchannelArrangementLookup[$index] : 'user-defined');
  212. }
  213. private static function DTSdialogNormalization($index, $version) {
  214. switch ($version) {
  215. case 7:
  216. return 0 - $index;
  217. break;
  218. case 6:
  219. return 0 - 16 - $index;
  220. break;
  221. }
  222. return false;
  223. }
  224. }
  225. ?>