module.audio.dts.php 11 KB

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