module.audio.ac3.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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.ac3.php //
  11. // module for analyzing AC-3 (aka Dolby Digital) audio files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_ac3
  16. {
  17. function getid3_ac3(&$fd, &$ThisFileInfo) {
  18. ///AH
  19. $ThisFileInfo['ac3']['raw']['bsi'] = array();
  20. $thisfile_ac3 = &$ThisFileInfo['ac3'];
  21. $thisfile_ac3_raw = &$thisfile_ac3['raw'];
  22. $thisfile_ac3_raw_bsi = &$thisfile_ac3_raw['bsi'];
  23. // http://www.atsc.org/standards/a_52a.pdf
  24. $ThisFileInfo['fileformat'] = 'ac3';
  25. $ThisFileInfo['audio']['dataformat'] = 'ac3';
  26. $ThisFileInfo['audio']['bitrate_mode'] = 'cbr';
  27. $ThisFileInfo['audio']['lossless'] = false;
  28. // An AC-3 serial coded audio bit stream is made up of a sequence of synchronization frames
  29. // Each synchronization frame contains 6 coded audio blocks (AB), each of which represent 256
  30. // new audio samples per channel. A synchronization information (SI) header at the beginning
  31. // of each frame contains information needed to acquire and maintain synchronization. A
  32. // bit stream information (BSI) header follows SI, and contains parameters describing the coded
  33. // audio service. The coded audio blocks may be followed by an auxiliary data (Aux) field. At the
  34. // end of each frame is an error check field that includes a CRC word for error detection. An
  35. // additional CRC word is located in the SI header, the use of which, by a decoder, is optional.
  36. //
  37. // syncinfo() | bsi() | AB0 | AB1 | AB2 | AB3 | AB4 | AB5 | Aux | CRC
  38. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  39. $AC3header['syncinfo'] = fread($fd, 5);
  40. $thisfile_ac3_raw['synchinfo']['synchword'] = substr($AC3header['syncinfo'], 0, 2);
  41. if ($thisfile_ac3_raw['synchinfo']['synchword'] != "\x0B\x77") {
  42. $ThisFileInfo['error'][] = 'Expecting "\x0B\x77" at offset '.$ThisFileInfo['avdataoffset'].', found \x'.strtoupper(dechex($AC3header['syncinfo']{0})).'\x'.strtoupper(dechex($AC3header['syncinfo']{1})).' instead';
  43. unset($thisfile_ac3);
  44. return false;
  45. } else {
  46. // syncinfo() {
  47. // syncword 16
  48. // crc1 16
  49. // fscod 2
  50. // frmsizecod 6
  51. // } /* end of syncinfo */
  52. $thisfile_ac3_raw['synchinfo']['crc1'] = getid3_lib::LittleEndian2Int(substr($AC3header['syncinfo'], 2, 2));
  53. $ac3_synchinfo_fscod_frmsizecod = getid3_lib::LittleEndian2Int(substr($AC3header['syncinfo'], 4, 1));
  54. $thisfile_ac3_raw['synchinfo']['fscod'] = ($ac3_synchinfo_fscod_frmsizecod & 0xC0) >> 6;
  55. $thisfile_ac3_raw['synchinfo']['frmsizecod'] = ($ac3_synchinfo_fscod_frmsizecod & 0x3F);
  56. $thisfile_ac3['sample_rate'] = $this->AC3sampleRateCodeLookup($thisfile_ac3_raw['synchinfo']['fscod']);
  57. if ($thisfile_ac3_raw['synchinfo']['fscod'] <= 3) {
  58. $ThisFileInfo['audio']['sample_rate'] = $thisfile_ac3['sample_rate'];
  59. }
  60. $thisfile_ac3['frame_length'] = $this->AC3frameSizeLookup($thisfile_ac3_raw['synchinfo']['frmsizecod'], $thisfile_ac3_raw['synchinfo']['fscod']);
  61. $thisfile_ac3['bitrate'] = $this->AC3bitrateLookup($thisfile_ac3_raw['synchinfo']['frmsizecod']);
  62. $ThisFileInfo['audio']['bitrate'] = $thisfile_ac3['bitrate'];
  63. $AC3header['bsi'] = getid3_lib::BigEndian2Bin(fread($fd, 15));
  64. $ac3_bsi_offset = 0;
  65. $thisfile_ac3_raw_bsi['bsid'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
  66. $ac3_bsi_offset += 5;
  67. if ($thisfile_ac3_raw_bsi['bsid'] > 8) {
  68. // Decoders which can decode version 8 will thus be able to decode version numbers less than 8.
  69. // If this standard is extended by the addition of additional elements or features, a value of bsid greater than 8 will be used.
  70. // Decoders built to this version of the standard will not be able to decode versions with bsid greater than 8.
  71. $ThisFileInfo['error'][] = 'Bit stream identification is version '.$thisfile_ac3_raw_bsi['bsid'].', but getID3() only understands up to version 8';
  72. unset($thisfile_ac3);
  73. return false;
  74. }
  75. $thisfile_ac3_raw_bsi['bsmod'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 3));
  76. $ac3_bsi_offset += 3;
  77. $thisfile_ac3_raw_bsi['acmod'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 3));
  78. $ac3_bsi_offset += 3;
  79. $thisfile_ac3['service_type'] = $this->AC3serviceTypeLookup($thisfile_ac3_raw_bsi['bsmod'], $thisfile_ac3_raw_bsi['acmod']);
  80. $ac3_coding_mode = $this->AC3audioCodingModeLookup($thisfile_ac3_raw_bsi['acmod']);
  81. foreach($ac3_coding_mode as $key => $value) {
  82. $thisfile_ac3[$key] = $value;
  83. }
  84. switch ($thisfile_ac3_raw_bsi['acmod']) {
  85. case 0:
  86. case 1:
  87. $ThisFileInfo['audio']['channelmode'] = 'mono';
  88. break;
  89. case 3:
  90. case 4:
  91. $ThisFileInfo['audio']['channelmode'] = 'stereo';
  92. break;
  93. default:
  94. $ThisFileInfo['audio']['channelmode'] = 'surround';
  95. break;
  96. }
  97. $ThisFileInfo['audio']['channels'] = $thisfile_ac3['num_channels'];
  98. if ($thisfile_ac3_raw_bsi['acmod'] & 0x01) {
  99. // If the lsb of acmod is a 1, center channel is in use and cmixlev follows in the bit stream.
  100. $thisfile_ac3_raw_bsi['cmixlev'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
  101. $ac3_bsi_offset += 2;
  102. $thisfile_ac3['center_mix_level'] = $this->AC3centerMixLevelLookup($thisfile_ac3_raw_bsi['cmixlev']);
  103. }
  104. if ($thisfile_ac3_raw_bsi['acmod'] & 0x04) {
  105. // If the msb of acmod is a 1, surround channels are in use and surmixlev follows in the bit stream.
  106. $thisfile_ac3_raw_bsi['surmixlev'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
  107. $ac3_bsi_offset += 2;
  108. $thisfile_ac3['surround_mix_level'] = $this->AC3surroundMixLevelLookup($thisfile_ac3_raw_bsi['surmixlev']);
  109. }
  110. if ($thisfile_ac3_raw_bsi['acmod'] == 0x02) {
  111. // When operating in the two channel mode, this 2-bit code indicates whether or not the program has been encoded in Dolby Surround.
  112. $thisfile_ac3_raw_bsi['dsurmod'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
  113. $ac3_bsi_offset += 2;
  114. $thisfile_ac3['dolby_surround_mode'] = $this->AC3dolbySurroundModeLookup($thisfile_ac3_raw_bsi['dsurmod']);
  115. }
  116. $thisfile_ac3_raw_bsi['lfeon'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  117. $ac3_bsi_offset += 1;
  118. $thisfile_ac3['lfe_enabled'] = $thisfile_ac3_raw_bsi['lfeon'];
  119. if ($thisfile_ac3_raw_bsi['lfeon']) {
  120. //$ThisFileInfo['audio']['channels']++;
  121. $ThisFileInfo['audio']['channels'] .= '.1';
  122. }
  123. $thisfile_ac3['channels_enabled'] = $this->AC3channelsEnabledLookup($thisfile_ac3_raw_bsi['acmod'], $thisfile_ac3_raw_bsi['lfeon']);
  124. // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1–31.
  125. // The value of 0 is reserved. The values of 1 to 31 are interpreted as -1 dB to -31 dB with respect to digital 100 percent.
  126. $thisfile_ac3_raw_bsi['dialnorm'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
  127. $ac3_bsi_offset += 5;
  128. $thisfile_ac3['dialogue_normalization'] = '-'.$thisfile_ac3_raw_bsi['dialnorm'].'dB';
  129. $thisfile_ac3_raw_bsi['compre_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  130. $ac3_bsi_offset += 1;
  131. if ($thisfile_ac3_raw_bsi['compre_flag']) {
  132. $thisfile_ac3_raw_bsi['compr'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 8));
  133. $ac3_bsi_offset += 8;
  134. $thisfile_ac3['heavy_compression'] = $this->AC3heavyCompression($thisfile_ac3_raw_bsi['compr']);
  135. }
  136. $thisfile_ac3_raw_bsi['langcode_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  137. $ac3_bsi_offset += 1;
  138. if ($thisfile_ac3_raw_bsi['langcode_flag']) {
  139. $thisfile_ac3_raw_bsi['langcod'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 8));
  140. $ac3_bsi_offset += 8;
  141. }
  142. $thisfile_ac3_raw_bsi['audprodie'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  143. $ac3_bsi_offset += 1;
  144. if ($thisfile_ac3_raw_bsi['audprodie']) {
  145. $thisfile_ac3_raw_bsi['mixlevel'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
  146. $ac3_bsi_offset += 5;
  147. $thisfile_ac3_raw_bsi['roomtyp'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
  148. $ac3_bsi_offset += 2;
  149. $thisfile_ac3['mixing_level'] = (80 + $thisfile_ac3_raw_bsi['mixlevel']).'dB';
  150. $thisfile_ac3['room_type'] = $this->AC3roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp']);
  151. }
  152. if ($thisfile_ac3_raw_bsi['acmod'] == 0x00) {
  153. // If acmod is 0, then two completely independent program channels (dual mono)
  154. // are encoded into the bit stream, and are referenced as Ch1, Ch2. In this case,
  155. // a number of additional items are present in BSI or audblk to fully describe Ch2.
  156. // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1–31.
  157. // The value of 0 is reserved. The values of 1 to 31 are interpreted as -1 dB to -31 dB with respect to digital 100 percent.
  158. $thisfile_ac3_raw_bsi['dialnorm2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
  159. $ac3_bsi_offset += 5;
  160. $thisfile_ac3['dialogue_normalization2'] = '-'.$thisfile_ac3_raw_bsi['dialnorm2'].'dB';
  161. $thisfile_ac3_raw_bsi['compre_flag2'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  162. $ac3_bsi_offset += 1;
  163. if ($thisfile_ac3_raw_bsi['compre_flag2']) {
  164. $thisfile_ac3_raw_bsi['compr2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 8));
  165. $ac3_bsi_offset += 8;
  166. $thisfile_ac3['heavy_compression2'] = $this->AC3heavyCompression($thisfile_ac3_raw_bsi['compr2']);
  167. }
  168. $thisfile_ac3_raw_bsi['langcode_flag2'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  169. $ac3_bsi_offset += 1;
  170. if ($thisfile_ac3_raw_bsi['langcode_flag2']) {
  171. $thisfile_ac3_raw_bsi['langcod2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 8));
  172. $ac3_bsi_offset += 8;
  173. }
  174. $thisfile_ac3_raw_bsi['audprodie2'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  175. $ac3_bsi_offset += 1;
  176. if ($thisfile_ac3_raw_bsi['audprodie2']) {
  177. $thisfile_ac3_raw_bsi['mixlevel2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
  178. $ac3_bsi_offset += 5;
  179. $thisfile_ac3_raw_bsi['roomtyp2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
  180. $ac3_bsi_offset += 2;
  181. $thisfile_ac3['mixing_level2'] = (80 + $thisfile_ac3_raw_bsi['mixlevel2']).'dB';
  182. $thisfile_ac3['room_type2'] = $this->AC3roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp2']);
  183. }
  184. }
  185. $thisfile_ac3_raw_bsi['copyright'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  186. $ac3_bsi_offset += 1;
  187. $thisfile_ac3_raw_bsi['original'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  188. $ac3_bsi_offset += 1;
  189. $thisfile_ac3_raw_bsi['timecode1_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  190. $ac3_bsi_offset += 1;
  191. if ($thisfile_ac3_raw_bsi['timecode1_flag']) {
  192. $thisfile_ac3_raw_bsi['timecode1'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 14));
  193. $ac3_bsi_offset += 14;
  194. }
  195. $thisfile_ac3_raw_bsi['timecode2_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  196. $ac3_bsi_offset += 1;
  197. if ($thisfile_ac3_raw_bsi['timecode2_flag']) {
  198. $thisfile_ac3_raw_bsi['timecode2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 14));
  199. $ac3_bsi_offset += 14;
  200. }
  201. $thisfile_ac3_raw_bsi['addbsi_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
  202. $ac3_bsi_offset += 1;
  203. if ($thisfile_ac3_raw_bsi['addbsi_flag']) {
  204. $thisfile_ac3_raw_bsi['addbsi_length'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 6));
  205. $ac3_bsi_offset += 6;
  206. $AC3header['bsi'] .= getid3_lib::BigEndian2Bin(fread($fd, $thisfile_ac3_raw_bsi['addbsi_length']));
  207. $thisfile_ac3_raw_bsi['addbsi_data'] = substr($AC3header['bsi'], $ac3_bsi_offset, $thisfile_ac3_raw_bsi['addbsi_length'] * 8);
  208. $ac3_bsi_offset += $thisfile_ac3_raw_bsi['addbsi_length'] * 8;
  209. }
  210. }
  211. return true;
  212. }
  213. function AC3sampleRateCodeLookup($fscod) {
  214. static $AC3sampleRateCodeLookup = array(
  215. 0 => 48000,
  216. 1 => 44100,
  217. 2 => 32000,
  218. 3 => 'reserved' // If the reserved code is indicated, the decoder should not attempt to decode audio and should mute.
  219. );
  220. return (isset($AC3sampleRateCodeLookup[$fscod]) ? $AC3sampleRateCodeLookup[$fscod] : false);
  221. }
  222. function AC3serviceTypeLookup($bsmod, $acmod) {
  223. static $AC3serviceTypeLookup = array();
  224. if (empty($AC3serviceTypeLookup)) {
  225. for ($i = 0; $i <= 7; $i++) {
  226. $AC3serviceTypeLookup[0][$i] = 'main audio service: complete main (CM)';
  227. $AC3serviceTypeLookup[1][$i] = 'main audio service: music and effects (ME)';
  228. $AC3serviceTypeLookup[2][$i] = 'associated service: visually impaired (VI)';
  229. $AC3serviceTypeLookup[3][$i] = 'associated service: hearing impaired (HI)';
  230. $AC3serviceTypeLookup[4][$i] = 'associated service: dialogue (D)';
  231. $AC3serviceTypeLookup[5][$i] = 'associated service: commentary (C)';
  232. $AC3serviceTypeLookup[6][$i] = 'associated service: emergency (E)';
  233. }
  234. $AC3serviceTypeLookup[7][1] = 'associated service: voice over (VO)';
  235. for ($i = 2; $i <= 7; $i++) {
  236. $AC3serviceTypeLookup[7][$i] = 'main audio service: karaoke';
  237. }
  238. }
  239. return (isset($AC3serviceTypeLookup[$bsmod][$acmod]) ? $AC3serviceTypeLookup[$bsmod][$acmod] : false);
  240. }
  241. function AC3audioCodingModeLookup($acmod) {
  242. static $AC3audioCodingModeLookup = array();
  243. if (empty($AC3audioCodingModeLookup)) {
  244. // array(channel configuration, # channels (not incl LFE), channel order)
  245. $AC3audioCodingModeLookup = array (
  246. 0 => array('channel_config'=>'1+1', 'num_channels'=>2, 'channel_order'=>'Ch1,Ch2'),
  247. 1 => array('channel_config'=>'1/0', 'num_channels'=>1, 'channel_order'=>'C'),
  248. 2 => array('channel_config'=>'2/0', 'num_channels'=>2, 'channel_order'=>'L,R'),
  249. 3 => array('channel_config'=>'3/0', 'num_channels'=>3, 'channel_order'=>'L,C,R'),
  250. 4 => array('channel_config'=>'2/1', 'num_channels'=>3, 'channel_order'=>'L,R,S'),
  251. 5 => array('channel_config'=>'3/1', 'num_channels'=>4, 'channel_order'=>'L,C,R,S'),
  252. 6 => array('channel_config'=>'2/2', 'num_channels'=>4, 'channel_order'=>'L,R,SL,SR'),
  253. 7 => array('channel_config'=>'3/2', 'num_channels'=>5, 'channel_order'=>'L,C,R,SL,SR')
  254. );
  255. }
  256. return (isset($AC3audioCodingModeLookup[$acmod]) ? $AC3audioCodingModeLookup[$acmod] : false);
  257. }
  258. function AC3centerMixLevelLookup($cmixlev) {
  259. static $AC3centerMixLevelLookup;
  260. if (empty($AC3centerMixLevelLookup)) {
  261. $AC3centerMixLevelLookup = array(
  262. 0 => pow(2, -3.0 / 6), // 0.707 (–3.0 dB)
  263. 1 => pow(2, -4.5 / 6), // 0.595 (–4.5 dB)
  264. 2 => pow(2, -6.0 / 6), // 0.500 (–6.0 dB)
  265. 3 => 'reserved'
  266. );
  267. }
  268. return (isset($AC3centerMixLevelLookup[$cmixlev]) ? $AC3centerMixLevelLookup[$cmixlev] : false);
  269. }
  270. function AC3surroundMixLevelLookup($surmixlev) {
  271. static $AC3surroundMixLevelLookup;
  272. if (empty($AC3surroundMixLevelLookup)) {
  273. $AC3surroundMixLevelLookup = array(
  274. 0 => pow(2, -3.0 / 6),
  275. 1 => pow(2, -6.0 / 6),
  276. 2 => 0,
  277. 3 => 'reserved'
  278. );
  279. }
  280. return (isset($AC3surroundMixLevelLookup[$surmixlev]) ? $AC3surroundMixLevelLookup[$surmixlev] : false);
  281. }
  282. function AC3dolbySurroundModeLookup($dsurmod) {
  283. static $AC3dolbySurroundModeLookup = array(
  284. 0 => 'not indicated',
  285. 1 => 'Not Dolby Surround encoded',
  286. 2 => 'Dolby Surround encoded',
  287. 3 => 'reserved'
  288. );
  289. return (isset($AC3dolbySurroundModeLookup[$dsurmod]) ? $AC3dolbySurroundModeLookup[$dsurmod] : false);
  290. }
  291. function AC3channelsEnabledLookup($acmod, $lfeon) {
  292. $AC3channelsEnabledLookup = array(
  293. 'ch1'=>(bool) ($acmod == 0),
  294. 'ch2'=>(bool) ($acmod == 0),
  295. 'left'=>(bool) ($acmod > 1),
  296. 'right'=>(bool) ($acmod > 1),
  297. 'center'=>(bool) ($acmod & 0x01),
  298. 'surround_mono'=>false,
  299. 'surround_left'=>false,
  300. 'surround_right'=>false,
  301. 'lfe'=>$lfeon);
  302. switch ($acmod) {
  303. case 4:
  304. case 5:
  305. $AC3channelsEnabledLookup['surround_mono'] = true;
  306. break;
  307. case 6:
  308. case 7:
  309. $AC3channelsEnabledLookup['surround_left'] = true;
  310. $AC3channelsEnabledLookup['surround_right'] = true;
  311. break;
  312. }
  313. return $AC3channelsEnabledLookup;
  314. }
  315. function AC3heavyCompression($compre) {
  316. // The first four bits indicate gain changes in 6.02dB increments which can be
  317. // implemented with an arithmetic shift operation. The following four bits
  318. // indicate linear gain changes, and require a 5-bit multiply.
  319. // We will represent the two 4-bit fields of compr as follows:
  320. // X0 X1 X2 X3 . Y4 Y5 Y6 Y7
  321. // The meaning of the X values is most simply described by considering X to represent a 4-bit
  322. // signed integer with values from –8 to +7. The gain indicated by X is then (X + 1) * 6.02 dB. The
  323. // following table shows this in detail.
  324. // Meaning of 4 msb of compr
  325. // 7 +48.16 dB
  326. // 6 +42.14 dB
  327. // 5 +36.12 dB
  328. // 4 +30.10 dB
  329. // 3 +24.08 dB
  330. // 2 +18.06 dB
  331. // 1 +12.04 dB
  332. // 0 +6.02 dB
  333. // -1 0 dB
  334. // -2 –6.02 dB
  335. // -3 –12.04 dB
  336. // -4 –18.06 dB
  337. // -5 –24.08 dB
  338. // -6 –30.10 dB
  339. // -7 –36.12 dB
  340. // -8 –42.14 dB
  341. $fourbit = str_pad(decbin(($compre & 0xF0) >> 4), 4, '0', STR_PAD_LEFT);
  342. if ($fourbit{0} == '1') {
  343. $log_gain = -8 + bindec(substr($fourbit, 1));
  344. } else {
  345. $log_gain = bindec(substr($fourbit, 1));
  346. }
  347. $log_gain = ($log_gain + 1) * getid3_lib::RGADamplitude2dB(2);
  348. // The value of Y is a linear representation of a gain change of up to –6 dB. Y is considered to
  349. // be an unsigned fractional integer, with a leading value of 1, or: 0.1 Y4 Y5 Y6 Y7 (base 2). Y can
  350. // represent values between 0.111112 (or 31/32) and 0.100002 (or 1/2). Thus, Y can represent gain
  351. // changes from –0.28 dB to –6.02 dB.
  352. $lin_gain = (16 + ($compre & 0x0F)) / 32;
  353. // The combination of X and Y values allows compr to indicate gain changes from
  354. // 48.16 – 0.28 = +47.89 dB, to
  355. // –42.14 – 6.02 = –48.16 dB.
  356. return $log_gain - $lin_gain;
  357. }
  358. function AC3roomTypeLookup($roomtyp) {
  359. static $AC3roomTypeLookup = array(
  360. 0 => 'not indicated',
  361. 1 => 'large room, X curve monitor',
  362. 2 => 'small room, flat monitor',
  363. 3 => 'reserved'
  364. );
  365. return (isset($AC3roomTypeLookup[$roomtyp]) ? $AC3roomTypeLookup[$roomtyp] : false);
  366. }
  367. function AC3frameSizeLookup($frmsizecod, $fscod) {
  368. $padding = (bool) ($frmsizecod % 2);
  369. $framesizeid = floor($frmsizecod / 2);
  370. static $AC3frameSizeLookup = array();
  371. if (empty($AC3frameSizeLookup)) {
  372. $AC3frameSizeLookup = array (
  373. 0 => array(128, 138, 192),
  374. 1 => array(40, 160, 174, 240),
  375. 2 => array(48, 192, 208, 288),
  376. 3 => array(56, 224, 242, 336),
  377. 4 => array(64, 256, 278, 384),
  378. 5 => array(80, 320, 348, 480),
  379. 6 => array(96, 384, 416, 576),
  380. 7 => array(112, 448, 486, 672),
  381. 8 => array(128, 512, 556, 768),
  382. 9 => array(160, 640, 696, 960),
  383. 10 => array(192, 768, 834, 1152),
  384. 11 => array(224, 896, 974, 1344),
  385. 12 => array(256, 1024, 1114, 1536),
  386. 13 => array(320, 1280, 1392, 1920),
  387. 14 => array(384, 1536, 1670, 2304),
  388. 15 => array(448, 1792, 1950, 2688),
  389. 16 => array(512, 2048, 2228, 3072),
  390. 17 => array(576, 2304, 2506, 3456),
  391. 18 => array(640, 2560, 2786, 3840)
  392. );
  393. }
  394. if (($fscod == 1) && $padding) {
  395. // frame lengths are padded by 1 word (16 bits) at 44100
  396. $AC3frameSizeLookup[$frmsizecod] += 2;
  397. }
  398. return (isset($AC3frameSizeLookup[$framesizeid][$fscod]) ? $AC3frameSizeLookup[$framesizeid][$fscod] : false);
  399. }
  400. function AC3bitrateLookup($frmsizecod) {
  401. $framesizeid = floor($frmsizecod / 2);
  402. static $AC3bitrateLookup = array(
  403. 0 => 32000,
  404. 1 => 40000,
  405. 2 => 48000,
  406. 3 => 56000,
  407. 4 => 64000,
  408. 5 => 80000,
  409. 6 => 96000,
  410. 7 => 112000,
  411. 8 => 128000,
  412. 9 => 160000,
  413. 10 => 192000,
  414. 11 => 224000,
  415. 12 => 256000,
  416. 13 => 320000,
  417. 14 => 384000,
  418. 15 => 448000,
  419. 16 => 512000,
  420. 17 => 576000,
  421. 18 => 640000
  422. );
  423. return (isset($AC3bitrateLookup[$framesizeid]) ? $AC3bitrateLookup[$framesizeid] : false);
  424. }
  425. }
  426. ?>