module.audio.ac3.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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.ac3.php //
  11. // module for analyzing AC-3 (aka Dolby Digital) 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. class getid3_ac3 extends getid3_handler
  19. {
  20. /**
  21. * @var array
  22. */
  23. private $AC3header = array();
  24. /**
  25. * @var int
  26. */
  27. private $BSIoffset = 0;
  28. const syncword = 0x0B77;
  29. /**
  30. * @return bool
  31. */
  32. public function Analyze() {
  33. $info = &$this->getid3->info;
  34. ///AH
  35. $info['ac3']['raw']['bsi'] = array();
  36. $thisfile_ac3 = &$info['ac3'];
  37. $thisfile_ac3_raw = &$thisfile_ac3['raw'];
  38. $thisfile_ac3_raw_bsi = &$thisfile_ac3_raw['bsi'];
  39. // http://www.atsc.org/standards/a_52a.pdf
  40. $info['fileformat'] = 'ac3';
  41. // An AC-3 serial coded audio bit stream is made up of a sequence of synchronization frames
  42. // Each synchronization frame contains 6 coded audio blocks (AB), each of which represent 256
  43. // new audio samples per channel. A synchronization information (SI) header at the beginning
  44. // of each frame contains information needed to acquire and maintain synchronization. A
  45. // bit stream information (BSI) header follows SI, and contains parameters describing the coded
  46. // audio service. The coded audio blocks may be followed by an auxiliary data (Aux) field. At the
  47. // end of each frame is an error check field that includes a CRC word for error detection. An
  48. // additional CRC word is located in the SI header, the use of which, by a decoder, is optional.
  49. //
  50. // syncinfo() | bsi() | AB0 | AB1 | AB2 | AB3 | AB4 | AB5 | Aux | CRC
  51. // syncinfo() {
  52. // syncword 16
  53. // crc1 16
  54. // fscod 2
  55. // frmsizecod 6
  56. // } /* end of syncinfo */
  57. $this->fseek($info['avdataoffset']);
  58. $tempAC3header = $this->fread(100); // should be enough to cover all data, there are some variable-length fields...?
  59. $this->AC3header['syncinfo'] = getid3_lib::BigEndian2Int(substr($tempAC3header, 0, 2));
  60. $this->AC3header['bsi'] = getid3_lib::BigEndian2Bin(substr($tempAC3header, 2));
  61. $thisfile_ac3_raw_bsi['bsid'] = (getid3_lib::LittleEndian2Int(substr($tempAC3header, 5, 1)) & 0xF8) >> 3; // AC3 and E-AC3 put the "bsid" version identifier in the same place, but unfortnately the 4 bytes between the syncword and the version identifier are interpreted differently, so grab it here so the following code structure can make sense
  62. unset($tempAC3header);
  63. if ($this->AC3header['syncinfo'] !== self::syncword) {
  64. if (!$this->isDependencyFor('matroska')) {
  65. unset($info['fileformat'], $info['ac3']);
  66. return $this->error('Expecting "'.dechex(self::syncword).'" at offset '.$info['avdataoffset'].', found "'.dechex($this->AC3header['syncinfo']).'"');
  67. }
  68. }
  69. $info['audio']['dataformat'] = 'ac3';
  70. $info['audio']['bitrate_mode'] = 'cbr';
  71. $info['audio']['lossless'] = false;
  72. if ($thisfile_ac3_raw_bsi['bsid'] <= 8) {
  73. $thisfile_ac3_raw_bsi['crc1'] = getid3_lib::Bin2Dec($this->readHeaderBSI(16));
  74. $thisfile_ac3_raw_bsi['fscod'] = $this->readHeaderBSI(2); // 5.4.1.3
  75. $thisfile_ac3_raw_bsi['frmsizecod'] = $this->readHeaderBSI(6); // 5.4.1.4
  76. if ($thisfile_ac3_raw_bsi['frmsizecod'] > 37) { // binary: 100101 - see Table 5.18 Frame Size Code Table (1 word = 16 bits)
  77. $this->warning('Unexpected ac3.bsi.frmsizecod value: '.$thisfile_ac3_raw_bsi['frmsizecod'].', bitrate not set correctly');
  78. }
  79. $thisfile_ac3_raw_bsi['bsid'] = $this->readHeaderBSI(5); // we already know this from pre-parsing the version identifier, but re-read it to let the bitstream flow as intended
  80. $thisfile_ac3_raw_bsi['bsmod'] = $this->readHeaderBSI(3);
  81. $thisfile_ac3_raw_bsi['acmod'] = $this->readHeaderBSI(3);
  82. if ($thisfile_ac3_raw_bsi['acmod'] & 0x01) {
  83. // If the lsb of acmod is a 1, center channel is in use and cmixlev follows in the bit stream.
  84. $thisfile_ac3_raw_bsi['cmixlev'] = $this->readHeaderBSI(2);
  85. $thisfile_ac3['center_mix_level'] = self::centerMixLevelLookup($thisfile_ac3_raw_bsi['cmixlev']);
  86. }
  87. if ($thisfile_ac3_raw_bsi['acmod'] & 0x04) {
  88. // If the msb of acmod is a 1, surround channels are in use and surmixlev follows in the bit stream.
  89. $thisfile_ac3_raw_bsi['surmixlev'] = $this->readHeaderBSI(2);
  90. $thisfile_ac3['surround_mix_level'] = self::surroundMixLevelLookup($thisfile_ac3_raw_bsi['surmixlev']);
  91. }
  92. if ($thisfile_ac3_raw_bsi['acmod'] == 0x02) {
  93. // When operating in the two channel mode, this 2-bit code indicates whether or not the program has been encoded in Dolby Surround.
  94. $thisfile_ac3_raw_bsi['dsurmod'] = $this->readHeaderBSI(2);
  95. $thisfile_ac3['dolby_surround_mode'] = self::dolbySurroundModeLookup($thisfile_ac3_raw_bsi['dsurmod']);
  96. }
  97. $thisfile_ac3_raw_bsi['flags']['lfeon'] = (bool) $this->readHeaderBSI(1);
  98. // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1-31.
  99. // 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.
  100. $thisfile_ac3_raw_bsi['dialnorm'] = $this->readHeaderBSI(5); // 5.4.2.8 dialnorm: Dialogue Normalization, 5 Bits
  101. $thisfile_ac3_raw_bsi['flags']['compr'] = (bool) $this->readHeaderBSI(1); // 5.4.2.9 compre: Compression Gain Word Exists, 1 Bit
  102. if ($thisfile_ac3_raw_bsi['flags']['compr']) {
  103. $thisfile_ac3_raw_bsi['compr'] = $this->readHeaderBSI(8); // 5.4.2.10 compr: Compression Gain Word, 8 Bits
  104. $thisfile_ac3['heavy_compression'] = self::heavyCompression($thisfile_ac3_raw_bsi['compr']);
  105. }
  106. $thisfile_ac3_raw_bsi['flags']['langcod'] = (bool) $this->readHeaderBSI(1); // 5.4.2.11 langcode: Language Code Exists, 1 Bit
  107. if ($thisfile_ac3_raw_bsi['flags']['langcod']) {
  108. $thisfile_ac3_raw_bsi['langcod'] = $this->readHeaderBSI(8); // 5.4.2.12 langcod: Language Code, 8 Bits
  109. }
  110. $thisfile_ac3_raw_bsi['flags']['audprodinfo'] = (bool) $this->readHeaderBSI(1); // 5.4.2.13 audprodie: Audio Production Information Exists, 1 Bit
  111. if ($thisfile_ac3_raw_bsi['flags']['audprodinfo']) {
  112. $thisfile_ac3_raw_bsi['mixlevel'] = $this->readHeaderBSI(5); // 5.4.2.14 mixlevel: Mixing Level, 5 Bits
  113. $thisfile_ac3_raw_bsi['roomtyp'] = $this->readHeaderBSI(2); // 5.4.2.15 roomtyp: Room Type, 2 Bits
  114. $thisfile_ac3['mixing_level'] = (80 + $thisfile_ac3_raw_bsi['mixlevel']).'dB';
  115. $thisfile_ac3['room_type'] = self::roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp']);
  116. }
  117. $thisfile_ac3_raw_bsi['dialnorm2'] = $this->readHeaderBSI(5); // 5.4.2.16 dialnorm2: Dialogue Normalization, ch2, 5 Bits
  118. $thisfile_ac3['dialogue_normalization2'] = '-'.$thisfile_ac3_raw_bsi['dialnorm2'].'dB'; // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1-31. 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.
  119. $thisfile_ac3_raw_bsi['flags']['compr2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.17 compr2e: Compression Gain Word Exists, ch2, 1 Bit
  120. if ($thisfile_ac3_raw_bsi['flags']['compr2']) {
  121. $thisfile_ac3_raw_bsi['compr2'] = $this->readHeaderBSI(8); // 5.4.2.18 compr2: Compression Gain Word, ch2, 8 Bits
  122. $thisfile_ac3['heavy_compression2'] = self::heavyCompression($thisfile_ac3_raw_bsi['compr2']);
  123. }
  124. $thisfile_ac3_raw_bsi['flags']['langcod2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.19 langcod2e: Language Code Exists, ch2, 1 Bit
  125. if ($thisfile_ac3_raw_bsi['flags']['langcod2']) {
  126. $thisfile_ac3_raw_bsi['langcod2'] = $this->readHeaderBSI(8); // 5.4.2.20 langcod2: Language Code, ch2, 8 Bits
  127. }
  128. $thisfile_ac3_raw_bsi['flags']['audprodinfo2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.21 audprodi2e: Audio Production Information Exists, ch2, 1 Bit
  129. if ($thisfile_ac3_raw_bsi['flags']['audprodinfo2']) {
  130. $thisfile_ac3_raw_bsi['mixlevel2'] = $this->readHeaderBSI(5); // 5.4.2.22 mixlevel2: Mixing Level, ch2, 5 Bits
  131. $thisfile_ac3_raw_bsi['roomtyp2'] = $this->readHeaderBSI(2); // 5.4.2.23 roomtyp2: Room Type, ch2, 2 Bits
  132. $thisfile_ac3['mixing_level2'] = (80 + $thisfile_ac3_raw_bsi['mixlevel2']).'dB';
  133. $thisfile_ac3['room_type2'] = self::roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp2']);
  134. }
  135. $thisfile_ac3_raw_bsi['copyright'] = (bool) $this->readHeaderBSI(1); // 5.4.2.24 copyrightb: Copyright Bit, 1 Bit
  136. $thisfile_ac3_raw_bsi['original'] = (bool) $this->readHeaderBSI(1); // 5.4.2.25 origbs: Original Bit Stream, 1 Bit
  137. $thisfile_ac3_raw_bsi['flags']['timecod1'] = $this->readHeaderBSI(2); // 5.4.2.26 timecod1e, timcode2e: Time Code (first and second) Halves Exist, 2 Bits
  138. if ($thisfile_ac3_raw_bsi['flags']['timecod1'] & 0x01) {
  139. $thisfile_ac3_raw_bsi['timecod1'] = $this->readHeaderBSI(14); // 5.4.2.27 timecod1: Time code first half, 14 bits
  140. $thisfile_ac3['timecode1'] = 0;
  141. $thisfile_ac3['timecode1'] += (($thisfile_ac3_raw_bsi['timecod1'] & 0x3E00) >> 9) * 3600; // The first 5 bits of this 14-bit field represent the time in hours, with valid values of 0�23
  142. $thisfile_ac3['timecode1'] += (($thisfile_ac3_raw_bsi['timecod1'] & 0x01F8) >> 3) * 60; // The next 6 bits represent the time in minutes, with valid values of 0�59
  143. $thisfile_ac3['timecode1'] += (($thisfile_ac3_raw_bsi['timecod1'] & 0x0003) >> 0) * 8; // The final 3 bits represents the time in 8 second increments, with valid values of 0�7 (representing 0, 8, 16, ... 56 seconds)
  144. }
  145. if ($thisfile_ac3_raw_bsi['flags']['timecod1'] & 0x02) {
  146. $thisfile_ac3_raw_bsi['timecod2'] = $this->readHeaderBSI(14); // 5.4.2.28 timecod2: Time code second half, 14 bits
  147. $thisfile_ac3['timecode2'] = 0;
  148. $thisfile_ac3['timecode2'] += (($thisfile_ac3_raw_bsi['timecod2'] & 0x3800) >> 11) * 1; // The first 3 bits of this 14-bit field represent the time in seconds, with valid values from 0�7 (representing 0-7 seconds)
  149. $thisfile_ac3['timecode2'] += (($thisfile_ac3_raw_bsi['timecod2'] & 0x07C0) >> 6) * (1 / 30); // The next 5 bits represents the time in frames, with valid values from 0�29 (one frame = 1/30th of a second)
  150. $thisfile_ac3['timecode2'] += (($thisfile_ac3_raw_bsi['timecod2'] & 0x003F) >> 0) * ((1 / 30) / 60); // The final 6 bits represents fractions of 1/64 of a frame, with valid values from 0�63
  151. }
  152. $thisfile_ac3_raw_bsi['flags']['addbsi'] = (bool) $this->readHeaderBSI(1);
  153. if ($thisfile_ac3_raw_bsi['flags']['addbsi']) {
  154. $thisfile_ac3_raw_bsi['addbsi_length'] = $this->readHeaderBSI(6) + 1; // This 6-bit code, which exists only if addbside is a 1, indicates the length in bytes of additional bit stream information. The valid range of addbsil is 0�63, indicating 1�64 additional bytes, respectively.
  155. $this->AC3header['bsi'] .= getid3_lib::BigEndian2Bin($this->fread($thisfile_ac3_raw_bsi['addbsi_length']));
  156. $thisfile_ac3_raw_bsi['addbsi_data'] = substr($this->AC3header['bsi'], $this->BSIoffset, $thisfile_ac3_raw_bsi['addbsi_length'] * 8);
  157. $this->BSIoffset += $thisfile_ac3_raw_bsi['addbsi_length'] * 8;
  158. }
  159. } elseif ($thisfile_ac3_raw_bsi['bsid'] <= 16) { // E-AC3
  160. $this->error('E-AC3 parsing is incomplete and experimental in this version of getID3 ('.$this->getid3->version().'). Notably the bitrate calculations are wrong -- value might (or not) be correct, but it is not calculated correctly. Email info@getid3.org if you know how to calculate EAC3 bitrate correctly.');
  161. $info['audio']['dataformat'] = 'eac3';
  162. $thisfile_ac3_raw_bsi['strmtyp'] = $this->readHeaderBSI(2);
  163. $thisfile_ac3_raw_bsi['substreamid'] = $this->readHeaderBSI(3);
  164. $thisfile_ac3_raw_bsi['frmsiz'] = $this->readHeaderBSI(11);
  165. $thisfile_ac3_raw_bsi['fscod'] = $this->readHeaderBSI(2);
  166. if ($thisfile_ac3_raw_bsi['fscod'] == 3) {
  167. $thisfile_ac3_raw_bsi['fscod2'] = $this->readHeaderBSI(2);
  168. $thisfile_ac3_raw_bsi['numblkscod'] = 3; // six blocks per syncframe
  169. } else {
  170. $thisfile_ac3_raw_bsi['numblkscod'] = $this->readHeaderBSI(2);
  171. }
  172. $thisfile_ac3['bsi']['blocks_per_sync_frame'] = self::blocksPerSyncFrame($thisfile_ac3_raw_bsi['numblkscod']);
  173. $thisfile_ac3_raw_bsi['acmod'] = $this->readHeaderBSI(3);
  174. $thisfile_ac3_raw_bsi['flags']['lfeon'] = (bool) $this->readHeaderBSI(1);
  175. $thisfile_ac3_raw_bsi['bsid'] = $this->readHeaderBSI(5); // we already know this from pre-parsing the version identifier, but re-read it to let the bitstream flow as intended
  176. $thisfile_ac3_raw_bsi['dialnorm'] = $this->readHeaderBSI(5);
  177. $thisfile_ac3_raw_bsi['flags']['compr'] = (bool) $this->readHeaderBSI(1);
  178. if ($thisfile_ac3_raw_bsi['flags']['compr']) {
  179. $thisfile_ac3_raw_bsi['compr'] = $this->readHeaderBSI(8);
  180. }
  181. if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value)
  182. $thisfile_ac3_raw_bsi['dialnorm2'] = $this->readHeaderBSI(5);
  183. $thisfile_ac3_raw_bsi['flags']['compr2'] = (bool) $this->readHeaderBSI(1);
  184. if ($thisfile_ac3_raw_bsi['flags']['compr2']) {
  185. $thisfile_ac3_raw_bsi['compr2'] = $this->readHeaderBSI(8);
  186. }
  187. }
  188. if ($thisfile_ac3_raw_bsi['strmtyp'] == 1) { // if dependent stream
  189. $thisfile_ac3_raw_bsi['flags']['chanmap'] = (bool) $this->readHeaderBSI(1);
  190. if ($thisfile_ac3_raw_bsi['flags']['chanmap']) {
  191. $thisfile_ac3_raw_bsi['chanmap'] = $this->readHeaderBSI(8);
  192. }
  193. }
  194. $thisfile_ac3_raw_bsi['flags']['mixmdat'] = (bool) $this->readHeaderBSI(1);
  195. if ($thisfile_ac3_raw_bsi['flags']['mixmdat']) { // Mixing metadata
  196. if ($thisfile_ac3_raw_bsi['acmod'] > 2) { // if more than 2 channels
  197. $thisfile_ac3_raw_bsi['dmixmod'] = $this->readHeaderBSI(2);
  198. }
  199. if (($thisfile_ac3_raw_bsi['acmod'] & 0x01) && ($thisfile_ac3_raw_bsi['acmod'] > 2)) { // if three front channels exist
  200. $thisfile_ac3_raw_bsi['ltrtcmixlev'] = $this->readHeaderBSI(3);
  201. $thisfile_ac3_raw_bsi['lorocmixlev'] = $this->readHeaderBSI(3);
  202. }
  203. if ($thisfile_ac3_raw_bsi['acmod'] & 0x04) { // if a surround channel exists
  204. $thisfile_ac3_raw_bsi['ltrtsurmixlev'] = $this->readHeaderBSI(3);
  205. $thisfile_ac3_raw_bsi['lorosurmixlev'] = $this->readHeaderBSI(3);
  206. }
  207. if ($thisfile_ac3_raw_bsi['flags']['lfeon']) { // if the LFE channel exists
  208. $thisfile_ac3_raw_bsi['flags']['lfemixlevcod'] = (bool) $this->readHeaderBSI(1);
  209. if ($thisfile_ac3_raw_bsi['flags']['lfemixlevcod']) {
  210. $thisfile_ac3_raw_bsi['lfemixlevcod'] = $this->readHeaderBSI(5);
  211. }
  212. }
  213. if ($thisfile_ac3_raw_bsi['strmtyp'] == 0) { // if independent stream
  214. $thisfile_ac3_raw_bsi['flags']['pgmscl'] = (bool) $this->readHeaderBSI(1);
  215. if ($thisfile_ac3_raw_bsi['flags']['pgmscl']) {
  216. $thisfile_ac3_raw_bsi['pgmscl'] = $this->readHeaderBSI(6);
  217. }
  218. if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value)
  219. $thisfile_ac3_raw_bsi['flags']['pgmscl2'] = (bool) $this->readHeaderBSI(1);
  220. if ($thisfile_ac3_raw_bsi['flags']['pgmscl2']) {
  221. $thisfile_ac3_raw_bsi['pgmscl2'] = $this->readHeaderBSI(6);
  222. }
  223. }
  224. $thisfile_ac3_raw_bsi['flags']['extpgmscl'] = (bool) $this->readHeaderBSI(1);
  225. if ($thisfile_ac3_raw_bsi['flags']['extpgmscl']) {
  226. $thisfile_ac3_raw_bsi['extpgmscl'] = $this->readHeaderBSI(6);
  227. }
  228. $thisfile_ac3_raw_bsi['mixdef'] = $this->readHeaderBSI(2);
  229. if ($thisfile_ac3_raw_bsi['mixdef'] == 1) { // mixing option 2
  230. $thisfile_ac3_raw_bsi['premixcmpsel'] = (bool) $this->readHeaderBSI(1);
  231. $thisfile_ac3_raw_bsi['drcsrc'] = (bool) $this->readHeaderBSI(1);
  232. $thisfile_ac3_raw_bsi['premixcmpscl'] = $this->readHeaderBSI(3);
  233. } elseif ($thisfile_ac3_raw_bsi['mixdef'] == 2) { // mixing option 3
  234. $thisfile_ac3_raw_bsi['mixdata'] = $this->readHeaderBSI(12);
  235. } elseif ($thisfile_ac3_raw_bsi['mixdef'] == 3) { // mixing option 4
  236. $mixdefbitsread = 0;
  237. $thisfile_ac3_raw_bsi['mixdeflen'] = $this->readHeaderBSI(5); $mixdefbitsread += 5;
  238. $thisfile_ac3_raw_bsi['flags']['mixdata2'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  239. if ($thisfile_ac3_raw_bsi['flags']['mixdata2']) {
  240. $thisfile_ac3_raw_bsi['premixcmpsel'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  241. $thisfile_ac3_raw_bsi['drcsrc'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  242. $thisfile_ac3_raw_bsi['premixcmpscl'] = $this->readHeaderBSI(3); $mixdefbitsread += 3;
  243. $thisfile_ac3_raw_bsi['flags']['extpgmlscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  244. if ($thisfile_ac3_raw_bsi['flags']['extpgmlscl']) {
  245. $thisfile_ac3_raw_bsi['extpgmlscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4;
  246. }
  247. $thisfile_ac3_raw_bsi['flags']['extpgmcscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  248. if ($thisfile_ac3_raw_bsi['flags']['extpgmcscl']) {
  249. $thisfile_ac3_raw_bsi['extpgmcscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4;
  250. }
  251. $thisfile_ac3_raw_bsi['flags']['extpgmrscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  252. if ($thisfile_ac3_raw_bsi['flags']['extpgmrscl']) {
  253. $thisfile_ac3_raw_bsi['extpgmrscl'] = $this->readHeaderBSI(4);
  254. }
  255. $thisfile_ac3_raw_bsi['flags']['extpgmlsscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  256. if ($thisfile_ac3_raw_bsi['flags']['extpgmlsscl']) {
  257. $thisfile_ac3_raw_bsi['extpgmlsscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4;
  258. }
  259. $thisfile_ac3_raw_bsi['flags']['extpgmrsscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  260. if ($thisfile_ac3_raw_bsi['flags']['extpgmrsscl']) {
  261. $thisfile_ac3_raw_bsi['extpgmrsscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4;
  262. }
  263. $thisfile_ac3_raw_bsi['flags']['extpgmlfescl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  264. if ($thisfile_ac3_raw_bsi['flags']['extpgmlfescl']) {
  265. $thisfile_ac3_raw_bsi['extpgmlfescl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4;
  266. }
  267. $thisfile_ac3_raw_bsi['flags']['dmixscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  268. if ($thisfile_ac3_raw_bsi['flags']['dmixscl']) {
  269. $thisfile_ac3_raw_bsi['dmixscl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4;
  270. }
  271. $thisfile_ac3_raw_bsi['flags']['addch'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  272. if ($thisfile_ac3_raw_bsi['flags']['addch']) {
  273. $thisfile_ac3_raw_bsi['flags']['extpgmaux1scl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  274. if ($thisfile_ac3_raw_bsi['flags']['extpgmaux1scl']) {
  275. $thisfile_ac3_raw_bsi['extpgmaux1scl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4;
  276. }
  277. $thisfile_ac3_raw_bsi['flags']['extpgmaux2scl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  278. if ($thisfile_ac3_raw_bsi['flags']['extpgmaux2scl']) {
  279. $thisfile_ac3_raw_bsi['extpgmaux2scl'] = $this->readHeaderBSI(4); $mixdefbitsread += 4;
  280. }
  281. }
  282. }
  283. $thisfile_ac3_raw_bsi['flags']['mixdata3'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  284. if ($thisfile_ac3_raw_bsi['flags']['mixdata3']) {
  285. $thisfile_ac3_raw_bsi['spchdat'] = $this->readHeaderBSI(5); $mixdefbitsread += 5;
  286. $thisfile_ac3_raw_bsi['flags']['addspchdat'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  287. if ($thisfile_ac3_raw_bsi['flags']['addspchdat']) {
  288. $thisfile_ac3_raw_bsi['spchdat1'] = $this->readHeaderBSI(5); $mixdefbitsread += 5;
  289. $thisfile_ac3_raw_bsi['spchan1att'] = $this->readHeaderBSI(2); $mixdefbitsread += 2;
  290. $thisfile_ac3_raw_bsi['flags']['addspchdat1'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread += 1;
  291. if ($thisfile_ac3_raw_bsi['flags']['addspchdat1']) {
  292. $thisfile_ac3_raw_bsi['spchdat2'] = $this->readHeaderBSI(5); $mixdefbitsread += 5;
  293. $thisfile_ac3_raw_bsi['spchan2att'] = $this->readHeaderBSI(3); $mixdefbitsread += 3;
  294. }
  295. }
  296. }
  297. $mixdata_bits = (8 * ($thisfile_ac3_raw_bsi['mixdeflen'] + 2)) - $mixdefbitsread;
  298. $mixdata_fill = (($mixdata_bits % 8) ? 8 - ($mixdata_bits % 8) : 0);
  299. $thisfile_ac3_raw_bsi['mixdata'] = $this->readHeaderBSI($mixdata_bits);
  300. $thisfile_ac3_raw_bsi['mixdatafill'] = $this->readHeaderBSI($mixdata_fill);
  301. unset($mixdefbitsread, $mixdata_bits, $mixdata_fill);
  302. }
  303. if ($thisfile_ac3_raw_bsi['acmod'] < 2) { // if mono or dual mono source
  304. $thisfile_ac3_raw_bsi['flags']['paninfo'] = (bool) $this->readHeaderBSI(1);
  305. if ($thisfile_ac3_raw_bsi['flags']['paninfo']) {
  306. $thisfile_ac3_raw_bsi['panmean'] = $this->readHeaderBSI(8);
  307. $thisfile_ac3_raw_bsi['paninfo'] = $this->readHeaderBSI(6);
  308. }
  309. if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value)
  310. $thisfile_ac3_raw_bsi['flags']['paninfo2'] = (bool) $this->readHeaderBSI(1);
  311. if ($thisfile_ac3_raw_bsi['flags']['paninfo2']) {
  312. $thisfile_ac3_raw_bsi['panmean2'] = $this->readHeaderBSI(8);
  313. $thisfile_ac3_raw_bsi['paninfo2'] = $this->readHeaderBSI(6);
  314. }
  315. }
  316. }
  317. $thisfile_ac3_raw_bsi['flags']['frmmixcfginfo'] = (bool) $this->readHeaderBSI(1);
  318. if ($thisfile_ac3_raw_bsi['flags']['frmmixcfginfo']) { // mixing configuration information
  319. if ($thisfile_ac3_raw_bsi['numblkscod'] == 0) {
  320. $thisfile_ac3_raw_bsi['blkmixcfginfo'][0] = $this->readHeaderBSI(5);
  321. } else {
  322. for ($blk = 0; $blk < $thisfile_ac3_raw_bsi['numblkscod']; $blk++) {
  323. $thisfile_ac3_raw_bsi['flags']['blkmixcfginfo'.$blk] = (bool) $this->readHeaderBSI(1);
  324. if ($thisfile_ac3_raw_bsi['flags']['blkmixcfginfo'.$blk]) { // mixing configuration information
  325. $thisfile_ac3_raw_bsi['blkmixcfginfo'][$blk] = $this->readHeaderBSI(5);
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. $thisfile_ac3_raw_bsi['flags']['infomdat'] = (bool) $this->readHeaderBSI(1);
  333. if ($thisfile_ac3_raw_bsi['flags']['infomdat']) { // Informational metadata
  334. $thisfile_ac3_raw_bsi['bsmod'] = $this->readHeaderBSI(3);
  335. $thisfile_ac3_raw_bsi['flags']['copyrightb'] = (bool) $this->readHeaderBSI(1);
  336. $thisfile_ac3_raw_bsi['flags']['origbs'] = (bool) $this->readHeaderBSI(1);
  337. if ($thisfile_ac3_raw_bsi['acmod'] == 2) { // if in 2/0 mode
  338. $thisfile_ac3_raw_bsi['dsurmod'] = $this->readHeaderBSI(2);
  339. $thisfile_ac3_raw_bsi['dheadphonmod'] = $this->readHeaderBSI(2);
  340. }
  341. if ($thisfile_ac3_raw_bsi['acmod'] >= 6) { // if both surround channels exist
  342. $thisfile_ac3_raw_bsi['dsurexmod'] = $this->readHeaderBSI(2);
  343. }
  344. $thisfile_ac3_raw_bsi['flags']['audprodi'] = (bool) $this->readHeaderBSI(1);
  345. if ($thisfile_ac3_raw_bsi['flags']['audprodi']) {
  346. $thisfile_ac3_raw_bsi['mixlevel'] = $this->readHeaderBSI(5);
  347. $thisfile_ac3_raw_bsi['roomtyp'] = $this->readHeaderBSI(2);
  348. $thisfile_ac3_raw_bsi['flags']['adconvtyp'] = (bool) $this->readHeaderBSI(1);
  349. }
  350. if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value)
  351. $thisfile_ac3_raw_bsi['flags']['audprodi2'] = (bool) $this->readHeaderBSI(1);
  352. if ($thisfile_ac3_raw_bsi['flags']['audprodi2']) {
  353. $thisfile_ac3_raw_bsi['mixlevel2'] = $this->readHeaderBSI(5);
  354. $thisfile_ac3_raw_bsi['roomtyp2'] = $this->readHeaderBSI(2);
  355. $thisfile_ac3_raw_bsi['flags']['adconvtyp2'] = (bool) $this->readHeaderBSI(1);
  356. }
  357. }
  358. if ($thisfile_ac3_raw_bsi['fscod'] < 3) { // if not half sample rate
  359. $thisfile_ac3_raw_bsi['flags']['sourcefscod'] = (bool) $this->readHeaderBSI(1);
  360. }
  361. }
  362. if (($thisfile_ac3_raw_bsi['strmtyp'] == 0) && ($thisfile_ac3_raw_bsi['numblkscod'] != 3)) { // if both surround channels exist
  363. $thisfile_ac3_raw_bsi['flags']['convsync'] = (bool) $this->readHeaderBSI(1);
  364. }
  365. if ($thisfile_ac3_raw_bsi['strmtyp'] == 2) { // if bit stream converted from AC-3
  366. if ($thisfile_ac3_raw_bsi['numblkscod'] != 3) { // 6 blocks per syncframe
  367. $thisfile_ac3_raw_bsi['flags']['blkid'] = 1;
  368. } else {
  369. $thisfile_ac3_raw_bsi['flags']['blkid'] = (bool) $this->readHeaderBSI(1);
  370. }
  371. if ($thisfile_ac3_raw_bsi['flags']['blkid']) {
  372. $thisfile_ac3_raw_bsi['frmsizecod'] = $this->readHeaderBSI(6);
  373. }
  374. }
  375. $thisfile_ac3_raw_bsi['flags']['addbsi'] = (bool) $this->readHeaderBSI(1);
  376. if ($thisfile_ac3_raw_bsi['flags']['addbsi']) {
  377. $thisfile_ac3_raw_bsi['addbsil'] = $this->readHeaderBSI(6);
  378. $thisfile_ac3_raw_bsi['addbsi'] = $this->readHeaderBSI(($thisfile_ac3_raw_bsi['addbsil'] + 1) * 8);
  379. }
  380. } else {
  381. $this->error('Bit stream identification is version '.$thisfile_ac3_raw_bsi['bsid'].', but getID3() only understands up to version 16. Please submit a support ticket with a sample file.');
  382. unset($info['ac3']);
  383. return false;
  384. }
  385. if (isset($thisfile_ac3_raw_bsi['fscod2'])) {
  386. $thisfile_ac3['sample_rate'] = self::sampleRateCodeLookup2($thisfile_ac3_raw_bsi['fscod2']);
  387. } else {
  388. $thisfile_ac3['sample_rate'] = self::sampleRateCodeLookup($thisfile_ac3_raw_bsi['fscod']);
  389. }
  390. if ($thisfile_ac3_raw_bsi['fscod'] <= 3) {
  391. $info['audio']['sample_rate'] = $thisfile_ac3['sample_rate'];
  392. } else {
  393. $this->warning('Unexpected ac3.bsi.fscod value: '.$thisfile_ac3_raw_bsi['fscod']);
  394. }
  395. if (isset($thisfile_ac3_raw_bsi['frmsizecod'])) {
  396. $thisfile_ac3['frame_length'] = self::frameSizeLookup($thisfile_ac3_raw_bsi['frmsizecod'], $thisfile_ac3_raw_bsi['fscod']);
  397. $thisfile_ac3['bitrate'] = self::bitrateLookup($thisfile_ac3_raw_bsi['frmsizecod']);
  398. } elseif (!empty($thisfile_ac3_raw_bsi['frmsiz'])) {
  399. // this isn't right, but it's (usually) close, roughly 5% less than it should be.
  400. // but WHERE is the actual bitrate value stored in EAC3?? email info@getid3.org if you know!
  401. $thisfile_ac3['bitrate'] = ($thisfile_ac3_raw_bsi['frmsiz'] + 1) * 16 * 30; // The frmsiz field shall contain a value one less than the overall size of the coded syncframe in 16-bit words. That is, this field may assume a value ranging from 0 to 2047, and these values correspond to syncframe sizes ranging from 1 to 2048.
  402. // kludge-fix to make it approximately the expected value, still not "right":
  403. $thisfile_ac3['bitrate'] = round(($thisfile_ac3['bitrate'] * 1.05) / 16000) * 16000;
  404. }
  405. $info['audio']['bitrate'] = $thisfile_ac3['bitrate'];
  406. if (isset($thisfile_ac3_raw_bsi['bsmod']) && isset($thisfile_ac3_raw_bsi['acmod'])) {
  407. $thisfile_ac3['service_type'] = self::serviceTypeLookup($thisfile_ac3_raw_bsi['bsmod'], $thisfile_ac3_raw_bsi['acmod']);
  408. }
  409. $ac3_coding_mode = self::audioCodingModeLookup($thisfile_ac3_raw_bsi['acmod']);
  410. foreach($ac3_coding_mode as $key => $value) {
  411. $thisfile_ac3[$key] = $value;
  412. }
  413. switch ($thisfile_ac3_raw_bsi['acmod']) {
  414. case 0:
  415. case 1:
  416. $info['audio']['channelmode'] = 'mono';
  417. break;
  418. case 3:
  419. case 4:
  420. $info['audio']['channelmode'] = 'stereo';
  421. break;
  422. default:
  423. $info['audio']['channelmode'] = 'surround';
  424. break;
  425. }
  426. $info['audio']['channels'] = $thisfile_ac3['num_channels'];
  427. $thisfile_ac3['lfe_enabled'] = $thisfile_ac3_raw_bsi['flags']['lfeon'];
  428. if ($thisfile_ac3_raw_bsi['flags']['lfeon']) {
  429. $info['audio']['channels'] .= '.1';
  430. }
  431. $thisfile_ac3['channels_enabled'] = self::channelsEnabledLookup($thisfile_ac3_raw_bsi['acmod'], $thisfile_ac3_raw_bsi['flags']['lfeon']);
  432. $thisfile_ac3['dialogue_normalization'] = '-'.$thisfile_ac3_raw_bsi['dialnorm'].'dB';
  433. return true;
  434. }
  435. /**
  436. * @param int $length
  437. *
  438. * @return int
  439. */
  440. private function readHeaderBSI($length) {
  441. $data = substr($this->AC3header['bsi'], $this->BSIoffset, $length);
  442. $this->BSIoffset += $length;
  443. return bindec($data);
  444. }
  445. /**
  446. * @param int $fscod
  447. *
  448. * @return int|string|false
  449. */
  450. public static function sampleRateCodeLookup($fscod) {
  451. static $sampleRateCodeLookup = array(
  452. 0 => 48000,
  453. 1 => 44100,
  454. 2 => 32000,
  455. 3 => 'reserved' // If the reserved code is indicated, the decoder should not attempt to decode audio and should mute.
  456. );
  457. return (isset($sampleRateCodeLookup[$fscod]) ? $sampleRateCodeLookup[$fscod] : false);
  458. }
  459. /**
  460. * @param int $fscod2
  461. *
  462. * @return int|string|false
  463. */
  464. public static function sampleRateCodeLookup2($fscod2) {
  465. static $sampleRateCodeLookup2 = array(
  466. 0 => 24000,
  467. 1 => 22050,
  468. 2 => 16000,
  469. 3 => 'reserved' // If the reserved code is indicated, the decoder should not attempt to decode audio and should mute.
  470. );
  471. return (isset($sampleRateCodeLookup2[$fscod2]) ? $sampleRateCodeLookup2[$fscod2] : false);
  472. }
  473. /**
  474. * @param int $bsmod
  475. * @param int $acmod
  476. *
  477. * @return string|false
  478. */
  479. public static function serviceTypeLookup($bsmod, $acmod) {
  480. static $serviceTypeLookup = array();
  481. if (empty($serviceTypeLookup)) {
  482. for ($i = 0; $i <= 7; $i++) {
  483. $serviceTypeLookup[0][$i] = 'main audio service: complete main (CM)';
  484. $serviceTypeLookup[1][$i] = 'main audio service: music and effects (ME)';
  485. $serviceTypeLookup[2][$i] = 'associated service: visually impaired (VI)';
  486. $serviceTypeLookup[3][$i] = 'associated service: hearing impaired (HI)';
  487. $serviceTypeLookup[4][$i] = 'associated service: dialogue (D)';
  488. $serviceTypeLookup[5][$i] = 'associated service: commentary (C)';
  489. $serviceTypeLookup[6][$i] = 'associated service: emergency (E)';
  490. }
  491. $serviceTypeLookup[7][1] = 'associated service: voice over (VO)';
  492. for ($i = 2; $i <= 7; $i++) {
  493. $serviceTypeLookup[7][$i] = 'main audio service: karaoke';
  494. }
  495. }
  496. return (isset($serviceTypeLookup[$bsmod][$acmod]) ? $serviceTypeLookup[$bsmod][$acmod] : false);
  497. }
  498. /**
  499. * @param int $acmod
  500. *
  501. * @return array|false
  502. */
  503. public static function audioCodingModeLookup($acmod) {
  504. // array(channel configuration, # channels (not incl LFE), channel order)
  505. static $audioCodingModeLookup = array (
  506. 0 => array('channel_config'=>'1+1', 'num_channels'=>2, 'channel_order'=>'Ch1,Ch2'),
  507. 1 => array('channel_config'=>'1/0', 'num_channels'=>1, 'channel_order'=>'C'),
  508. 2 => array('channel_config'=>'2/0', 'num_channels'=>2, 'channel_order'=>'L,R'),
  509. 3 => array('channel_config'=>'3/0', 'num_channels'=>3, 'channel_order'=>'L,C,R'),
  510. 4 => array('channel_config'=>'2/1', 'num_channels'=>3, 'channel_order'=>'L,R,S'),
  511. 5 => array('channel_config'=>'3/1', 'num_channels'=>4, 'channel_order'=>'L,C,R,S'),
  512. 6 => array('channel_config'=>'2/2', 'num_channels'=>4, 'channel_order'=>'L,R,SL,SR'),
  513. 7 => array('channel_config'=>'3/2', 'num_channels'=>5, 'channel_order'=>'L,C,R,SL,SR'),
  514. );
  515. return (isset($audioCodingModeLookup[$acmod]) ? $audioCodingModeLookup[$acmod] : false);
  516. }
  517. /**
  518. * @param int $cmixlev
  519. *
  520. * @return int|float|string|false
  521. */
  522. public static function centerMixLevelLookup($cmixlev) {
  523. static $centerMixLevelLookup;
  524. if (empty($centerMixLevelLookup)) {
  525. $centerMixLevelLookup = array(
  526. 0 => pow(2, -3.0 / 6), // 0.707 (-3.0 dB)
  527. 1 => pow(2, -4.5 / 6), // 0.595 (-4.5 dB)
  528. 2 => pow(2, -6.0 / 6), // 0.500 (-6.0 dB)
  529. 3 => 'reserved'
  530. );
  531. }
  532. return (isset($centerMixLevelLookup[$cmixlev]) ? $centerMixLevelLookup[$cmixlev] : false);
  533. }
  534. /**
  535. * @param int $surmixlev
  536. *
  537. * @return int|float|string|false
  538. */
  539. public static function surroundMixLevelLookup($surmixlev) {
  540. static $surroundMixLevelLookup;
  541. if (empty($surroundMixLevelLookup)) {
  542. $surroundMixLevelLookup = array(
  543. 0 => pow(2, -3.0 / 6),
  544. 1 => pow(2, -6.0 / 6),
  545. 2 => 0,
  546. 3 => 'reserved'
  547. );
  548. }
  549. return (isset($surroundMixLevelLookup[$surmixlev]) ? $surroundMixLevelLookup[$surmixlev] : false);
  550. }
  551. /**
  552. * @param int $dsurmod
  553. *
  554. * @return string|false
  555. */
  556. public static function dolbySurroundModeLookup($dsurmod) {
  557. static $dolbySurroundModeLookup = array(
  558. 0 => 'not indicated',
  559. 1 => 'Not Dolby Surround encoded',
  560. 2 => 'Dolby Surround encoded',
  561. 3 => 'reserved'
  562. );
  563. return (isset($dolbySurroundModeLookup[$dsurmod]) ? $dolbySurroundModeLookup[$dsurmod] : false);
  564. }
  565. /**
  566. * @param int $acmod
  567. * @param bool $lfeon
  568. *
  569. * @return array
  570. */
  571. public static function channelsEnabledLookup($acmod, $lfeon) {
  572. $lookup = array(
  573. 'ch1'=>($acmod == 0),
  574. 'ch2'=>($acmod == 0),
  575. 'left'=>($acmod > 1),
  576. 'right'=>($acmod > 1),
  577. 'center'=>(bool) ($acmod & 0x01),
  578. 'surround_mono'=>false,
  579. 'surround_left'=>false,
  580. 'surround_right'=>false,
  581. 'lfe'=>$lfeon);
  582. switch ($acmod) {
  583. case 4:
  584. case 5:
  585. $lookup['surround_mono'] = true;
  586. break;
  587. case 6:
  588. case 7:
  589. $lookup['surround_left'] = true;
  590. $lookup['surround_right'] = true;
  591. break;
  592. }
  593. return $lookup;
  594. }
  595. /**
  596. * @param int $compre
  597. *
  598. * @return float|int
  599. */
  600. public static function heavyCompression($compre) {
  601. // The first four bits indicate gain changes in 6.02dB increments which can be
  602. // implemented with an arithmetic shift operation. The following four bits
  603. // indicate linear gain changes, and require a 5-bit multiply.
  604. // We will represent the two 4-bit fields of compr as follows:
  605. // X0 X1 X2 X3 . Y4 Y5 Y6 Y7
  606. // The meaning of the X values is most simply described by considering X to represent a 4-bit
  607. // signed integer with values from -8 to +7. The gain indicated by X is then (X + 1) * 6.02 dB. The
  608. // following table shows this in detail.
  609. // Meaning of 4 msb of compr
  610. // 7 +48.16 dB
  611. // 6 +42.14 dB
  612. // 5 +36.12 dB
  613. // 4 +30.10 dB
  614. // 3 +24.08 dB
  615. // 2 +18.06 dB
  616. // 1 +12.04 dB
  617. // 0 +6.02 dB
  618. // -1 0 dB
  619. // -2 -6.02 dB
  620. // -3 -12.04 dB
  621. // -4 -18.06 dB
  622. // -5 -24.08 dB
  623. // -6 -30.10 dB
  624. // -7 -36.12 dB
  625. // -8 -42.14 dB
  626. $fourbit = str_pad(decbin(($compre & 0xF0) >> 4), 4, '0', STR_PAD_LEFT);
  627. if ($fourbit[0] == '1') {
  628. $log_gain = -8 + bindec(substr($fourbit, 1));
  629. } else {
  630. $log_gain = bindec(substr($fourbit, 1));
  631. }
  632. $log_gain = ($log_gain + 1) * getid3_lib::RGADamplitude2dB(2);
  633. // The value of Y is a linear representation of a gain change of up to -6 dB. Y is considered to
  634. // be an unsigned fractional integer, with a leading value of 1, or: 0.1 Y4 Y5 Y6 Y7 (base 2). Y can
  635. // represent values between 0.111112 (or 31/32) and 0.100002 (or 1/2). Thus, Y can represent gain
  636. // changes from -0.28 dB to -6.02 dB.
  637. $lin_gain = (16 + ($compre & 0x0F)) / 32;
  638. // The combination of X and Y values allows compr to indicate gain changes from
  639. // 48.16 - 0.28 = +47.89 dB, to
  640. // -42.14 - 6.02 = -48.16 dB.
  641. return $log_gain - $lin_gain;
  642. }
  643. /**
  644. * @param int $roomtyp
  645. *
  646. * @return string|false
  647. */
  648. public static function roomTypeLookup($roomtyp) {
  649. static $roomTypeLookup = array(
  650. 0 => 'not indicated',
  651. 1 => 'large room, X curve monitor',
  652. 2 => 'small room, flat monitor',
  653. 3 => 'reserved'
  654. );
  655. return (isset($roomTypeLookup[$roomtyp]) ? $roomTypeLookup[$roomtyp] : false);
  656. }
  657. /**
  658. * @param int $frmsizecod
  659. * @param int $fscod
  660. *
  661. * @return int|false
  662. */
  663. public static function frameSizeLookup($frmsizecod, $fscod) {
  664. // LSB is whether padding is used or not
  665. $padding = (bool) ($frmsizecod & 0x01);
  666. $framesizeid = ($frmsizecod & 0x3E) >> 1;
  667. static $frameSizeLookup = array();
  668. if (empty($frameSizeLookup)) {
  669. $frameSizeLookup = array (
  670. 0 => array( 128, 138, 192), // 32 kbps
  671. 1 => array( 160, 174, 240), // 40 kbps
  672. 2 => array( 192, 208, 288), // 48 kbps
  673. 3 => array( 224, 242, 336), // 56 kbps
  674. 4 => array( 256, 278, 384), // 64 kbps
  675. 5 => array( 320, 348, 480), // 80 kbps
  676. 6 => array( 384, 416, 576), // 96 kbps
  677. 7 => array( 448, 486, 672), // 112 kbps
  678. 8 => array( 512, 556, 768), // 128 kbps
  679. 9 => array( 640, 696, 960), // 160 kbps
  680. 10 => array( 768, 834, 1152), // 192 kbps
  681. 11 => array( 896, 974, 1344), // 224 kbps
  682. 12 => array(1024, 1114, 1536), // 256 kbps
  683. 13 => array(1280, 1392, 1920), // 320 kbps
  684. 14 => array(1536, 1670, 2304), // 384 kbps
  685. 15 => array(1792, 1950, 2688), // 448 kbps
  686. 16 => array(2048, 2228, 3072), // 512 kbps
  687. 17 => array(2304, 2506, 3456), // 576 kbps
  688. 18 => array(2560, 2786, 3840) // 640 kbps
  689. );
  690. }
  691. $paddingBytes = 0;
  692. if (($fscod == 1) && $padding) {
  693. // frame lengths are padded by 1 word (16 bits) at 44100
  694. // (fscode==1) means 44100Hz (see sampleRateCodeLookup)
  695. $paddingBytes = 2;
  696. }
  697. return (isset($frameSizeLookup[$framesizeid][$fscod]) ? $frameSizeLookup[$framesizeid][$fscod] + $paddingBytes : false);
  698. }
  699. /**
  700. * @param int $frmsizecod
  701. *
  702. * @return int|false
  703. */
  704. public static function bitrateLookup($frmsizecod) {
  705. // LSB is whether padding is used or not
  706. $padding = (bool) ($frmsizecod & 0x01);
  707. $framesizeid = ($frmsizecod & 0x3E) >> 1;
  708. static $bitrateLookup = array(
  709. 0 => 32000,
  710. 1 => 40000,
  711. 2 => 48000,
  712. 3 => 56000,
  713. 4 => 64000,
  714. 5 => 80000,
  715. 6 => 96000,
  716. 7 => 112000,
  717. 8 => 128000,
  718. 9 => 160000,
  719. 10 => 192000,
  720. 11 => 224000,
  721. 12 => 256000,
  722. 13 => 320000,
  723. 14 => 384000,
  724. 15 => 448000,
  725. 16 => 512000,
  726. 17 => 576000,
  727. 18 => 640000,
  728. );
  729. return (isset($bitrateLookup[$framesizeid]) ? $bitrateLookup[$framesizeid] : false);
  730. }
  731. /**
  732. * @param int $numblkscod
  733. *
  734. * @return int|false
  735. */
  736. public static function blocksPerSyncFrame($numblkscod) {
  737. static $blocksPerSyncFrameLookup = array(
  738. 0 => 1,
  739. 1 => 2,
  740. 2 => 3,
  741. 3 => 6,
  742. );
  743. return (isset($blocksPerSyncFrameLookup[$numblkscod]) ? $blocksPerSyncFrameLookup[$numblkscod] : false);
  744. }
  745. }