3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.audio.ac3.php //
12 // module for analyzing AC-3 (aka Dolby Digital) audio files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
18 class getid3_ac3
extends getid3_handler
23 private $AC3header = array();
28 private $BSIoffset = 0;
30 const syncword
= 0x0B77;
35 public function Analyze() {
36 $info = &$this->getid3
->info
;
39 $info['ac3']['raw']['bsi'] = array();
40 $thisfile_ac3 = &$info['ac3'];
41 $thisfile_ac3_raw = &$thisfile_ac3['raw'];
42 $thisfile_ac3_raw_bsi = &$thisfile_ac3_raw['bsi'];
45 // http://www.atsc.org/standards/a_52a.pdf
47 $info['fileformat'] = 'ac3';
49 // An AC-3 serial coded audio bit stream is made up of a sequence of synchronization frames
50 // Each synchronization frame contains 6 coded audio blocks (AB), each of which represent 256
51 // new audio samples per channel. A synchronization information (SI) header at the beginning
52 // of each frame contains information needed to acquire and maintain synchronization. A
53 // bit stream information (BSI) header follows SI, and contains parameters describing the coded
54 // audio service. The coded audio blocks may be followed by an auxiliary data (Aux) field. At the
55 // end of each frame is an error check field that includes a CRC word for error detection. An
56 // additional CRC word is located in the SI header, the use of which, by a decoder, is optional.
58 // syncinfo() | bsi() | AB0 | AB1 | AB2 | AB3 | AB4 | AB5 | Aux | CRC
65 // } /* end of syncinfo */
67 $this->fseek($info['avdataoffset']);
68 $tempAC3header = $this->fread(100); // should be enough to cover all data, there are some variable-length fields...?
69 $this->AC3header
['syncinfo'] = getid3_lib
::BigEndian2Int(substr($tempAC3header, 0, 2));
70 $this->AC3header
['bsi'] = getid3_lib
::BigEndian2Bin(substr($tempAC3header, 2));
71 $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
72 unset($tempAC3header);
74 if ($this->AC3header
['syncinfo'] !== self
::syncword
) {
75 if (!$this->isDependencyFor('matroska')) {
76 unset($info['fileformat'], $info['ac3']);
77 return $this->error('Expecting "'.dechex(self
::syncword
).'" at offset '.$info['avdataoffset'].', found "'.dechex($this->AC3header
['syncinfo']).'"');
81 $info['audio']['dataformat'] = 'ac3';
82 $info['audio']['bitrate_mode'] = 'cbr';
83 $info['audio']['lossless'] = false;
85 if ($thisfile_ac3_raw_bsi['bsid'] <= 8) {
87 $thisfile_ac3_raw_bsi['crc1'] = getid3_lib
::Bin2Dec($this->readHeaderBSI(16));
88 $thisfile_ac3_raw_bsi['fscod'] = $this->readHeaderBSI(2); // 5.4.1.3
89 $thisfile_ac3_raw_bsi['frmsizecod'] = $this->readHeaderBSI(6); // 5.4.1.4
90 if ($thisfile_ac3_raw_bsi['frmsizecod'] > 37) { // binary: 100101 - see Table 5.18 Frame Size Code Table (1 word = 16 bits)
91 $this->warning('Unexpected ac3.bsi.frmsizecod value: '.$thisfile_ac3_raw_bsi['frmsizecod'].', bitrate not set correctly');
94 $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
95 $thisfile_ac3_raw_bsi['bsmod'] = $this->readHeaderBSI(3);
96 $thisfile_ac3_raw_bsi['acmod'] = $this->readHeaderBSI(3);
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'] = $this->readHeaderBSI(2);
101 $thisfile_ac3['center_mix_level'] = self
::centerMixLevelLookup($thisfile_ac3_raw_bsi['cmixlev']);
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'] = $this->readHeaderBSI(2);
107 $thisfile_ac3['surround_mix_level'] = self
::surroundMixLevelLookup($thisfile_ac3_raw_bsi['surmixlev']);
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'] = $this->readHeaderBSI(2);
113 $thisfile_ac3['dolby_surround_mode'] = self
::dolbySurroundModeLookup($thisfile_ac3_raw_bsi['dsurmod']);
116 $thisfile_ac3_raw_bsi['flags']['lfeon'] = (bool) $this->readHeaderBSI(1);
118 // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1-31.
119 // 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.
120 $thisfile_ac3_raw_bsi['dialnorm'] = $this->readHeaderBSI(5); // 5.4.2.8 dialnorm: Dialogue Normalization, 5 Bits
122 $thisfile_ac3_raw_bsi['flags']['compr'] = (bool) $this->readHeaderBSI(1); // 5.4.2.9 compre: Compression Gain Word Exists, 1 Bit
123 if ($thisfile_ac3_raw_bsi['flags']['compr']) {
124 $thisfile_ac3_raw_bsi['compr'] = $this->readHeaderBSI(8); // 5.4.2.10 compr: Compression Gain Word, 8 Bits
125 $thisfile_ac3['heavy_compression'] = self
::heavyCompression($thisfile_ac3_raw_bsi['compr']);
128 $thisfile_ac3_raw_bsi['flags']['langcod'] = (bool) $this->readHeaderBSI(1); // 5.4.2.11 langcode: Language Code Exists, 1 Bit
129 if ($thisfile_ac3_raw_bsi['flags']['langcod']) {
130 $thisfile_ac3_raw_bsi['langcod'] = $this->readHeaderBSI(8); // 5.4.2.12 langcod: Language Code, 8 Bits
133 $thisfile_ac3_raw_bsi['flags']['audprodinfo'] = (bool) $this->readHeaderBSI(1); // 5.4.2.13 audprodie: Audio Production Information Exists, 1 Bit
134 if ($thisfile_ac3_raw_bsi['flags']['audprodinfo']) {
135 $thisfile_ac3_raw_bsi['mixlevel'] = $this->readHeaderBSI(5); // 5.4.2.14 mixlevel: Mixing Level, 5 Bits
136 $thisfile_ac3_raw_bsi['roomtyp'] = $this->readHeaderBSI(2); // 5.4.2.15 roomtyp: Room Type, 2 Bits
138 $thisfile_ac3['mixing_level'] = (80 +
$thisfile_ac3_raw_bsi['mixlevel']).'dB';
139 $thisfile_ac3['room_type'] = self
::roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp']);
143 $thisfile_ac3_raw_bsi['dialnorm2'] = $this->readHeaderBSI(5); // 5.4.2.16 dialnorm2: Dialogue Normalization, ch2, 5 Bits
144 $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.
146 $thisfile_ac3_raw_bsi['flags']['compr2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.17 compr2e: Compression Gain Word Exists, ch2, 1 Bit
147 if ($thisfile_ac3_raw_bsi['flags']['compr2']) {
148 $thisfile_ac3_raw_bsi['compr2'] = $this->readHeaderBSI(8); // 5.4.2.18 compr2: Compression Gain Word, ch2, 8 Bits
149 $thisfile_ac3['heavy_compression2'] = self
::heavyCompression($thisfile_ac3_raw_bsi['compr2']);
152 $thisfile_ac3_raw_bsi['flags']['langcod2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.19 langcod2e: Language Code Exists, ch2, 1 Bit
153 if ($thisfile_ac3_raw_bsi['flags']['langcod2']) {
154 $thisfile_ac3_raw_bsi['langcod2'] = $this->readHeaderBSI(8); // 5.4.2.20 langcod2: Language Code, ch2, 8 Bits
157 $thisfile_ac3_raw_bsi['flags']['audprodinfo2'] = (bool) $this->readHeaderBSI(1); // 5.4.2.21 audprodi2e: Audio Production Information Exists, ch2, 1 Bit
158 if ($thisfile_ac3_raw_bsi['flags']['audprodinfo2']) {
159 $thisfile_ac3_raw_bsi['mixlevel2'] = $this->readHeaderBSI(5); // 5.4.2.22 mixlevel2: Mixing Level, ch2, 5 Bits
160 $thisfile_ac3_raw_bsi['roomtyp2'] = $this->readHeaderBSI(2); // 5.4.2.23 roomtyp2: Room Type, ch2, 2 Bits
162 $thisfile_ac3['mixing_level2'] = (80 +
$thisfile_ac3_raw_bsi['mixlevel2']).'dB';
163 $thisfile_ac3['room_type2'] = self
::roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp2']);
166 $thisfile_ac3_raw_bsi['copyright'] = (bool) $this->readHeaderBSI(1); // 5.4.2.24 copyrightb: Copyright Bit, 1 Bit
168 $thisfile_ac3_raw_bsi['original'] = (bool) $this->readHeaderBSI(1); // 5.4.2.25 origbs: Original Bit Stream, 1 Bit
170 $thisfile_ac3_raw_bsi['flags']['timecod1'] = $this->readHeaderBSI(2); // 5.4.2.26 timecod1e, timcode2e: Time Code (first and second) Halves Exist, 2 Bits
171 if ($thisfile_ac3_raw_bsi['flags']['timecod1'] & 0x01) {
172 $thisfile_ac3_raw_bsi['timecod1'] = $this->readHeaderBSI(14); // 5.4.2.27 timecod1: Time code first half, 14 bits
173 $thisfile_ac3['timecode1'] = 0;
174 $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
175 $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
176 $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)
178 if ($thisfile_ac3_raw_bsi['flags']['timecod1'] & 0x02) {
179 $thisfile_ac3_raw_bsi['timecod2'] = $this->readHeaderBSI(14); // 5.4.2.28 timecod2: Time code second half, 14 bits
180 $thisfile_ac3['timecode2'] = 0;
181 $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)
182 $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)
183 $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
186 $thisfile_ac3_raw_bsi['flags']['addbsi'] = (bool) $this->readHeaderBSI(1);
187 if ($thisfile_ac3_raw_bsi['flags']['addbsi']) {
188 $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.
190 $this->AC3header
['bsi'] .= getid3_lib
::BigEndian2Bin($this->fread($thisfile_ac3_raw_bsi['addbsi_length']));
192 $thisfile_ac3_raw_bsi['addbsi_data'] = substr($this->AC3header
['bsi'], $this->BSIoffset
, $thisfile_ac3_raw_bsi['addbsi_length'] * 8);
193 $this->BSIoffset +
= $thisfile_ac3_raw_bsi['addbsi_length'] * 8;
197 } elseif ($thisfile_ac3_raw_bsi['bsid'] <= 16) { // E-AC3
200 $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.');
201 $info['audio']['dataformat'] = 'eac3';
203 $thisfile_ac3_raw_bsi['strmtyp'] = $this->readHeaderBSI(2);
204 $thisfile_ac3_raw_bsi['substreamid'] = $this->readHeaderBSI(3);
205 $thisfile_ac3_raw_bsi['frmsiz'] = $this->readHeaderBSI(11);
206 $thisfile_ac3_raw_bsi['fscod'] = $this->readHeaderBSI(2);
207 if ($thisfile_ac3_raw_bsi['fscod'] == 3) {
208 $thisfile_ac3_raw_bsi['fscod2'] = $this->readHeaderBSI(2);
209 $thisfile_ac3_raw_bsi['numblkscod'] = 3; // six blocks per syncframe
211 $thisfile_ac3_raw_bsi['numblkscod'] = $this->readHeaderBSI(2);
213 $thisfile_ac3['bsi']['blocks_per_sync_frame'] = self
::blocksPerSyncFrame($thisfile_ac3_raw_bsi['numblkscod']);
214 $thisfile_ac3_raw_bsi['acmod'] = $this->readHeaderBSI(3);
215 $thisfile_ac3_raw_bsi['flags']['lfeon'] = (bool) $this->readHeaderBSI(1);
216 $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
217 $thisfile_ac3_raw_bsi['dialnorm'] = $this->readHeaderBSI(5);
218 $thisfile_ac3_raw_bsi['flags']['compr'] = (bool) $this->readHeaderBSI(1);
219 if ($thisfile_ac3_raw_bsi['flags']['compr']) {
220 $thisfile_ac3_raw_bsi['compr'] = $this->readHeaderBSI(8);
222 if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value)
223 $thisfile_ac3_raw_bsi['dialnorm2'] = $this->readHeaderBSI(5);
224 $thisfile_ac3_raw_bsi['flags']['compr2'] = (bool) $this->readHeaderBSI(1);
225 if ($thisfile_ac3_raw_bsi['flags']['compr2']) {
226 $thisfile_ac3_raw_bsi['compr2'] = $this->readHeaderBSI(8);
229 if ($thisfile_ac3_raw_bsi['strmtyp'] == 1) { // if dependent stream
230 $thisfile_ac3_raw_bsi['flags']['chanmap'] = (bool) $this->readHeaderBSI(1);
231 if ($thisfile_ac3_raw_bsi['flags']['chanmap']) {
232 $thisfile_ac3_raw_bsi['chanmap'] = $this->readHeaderBSI(8);
235 $thisfile_ac3_raw_bsi['flags']['mixmdat'] = (bool) $this->readHeaderBSI(1);
236 if ($thisfile_ac3_raw_bsi['flags']['mixmdat']) { // Mixing metadata
237 if ($thisfile_ac3_raw_bsi['acmod'] > 2) { // if more than 2 channels
238 $thisfile_ac3_raw_bsi['dmixmod'] = $this->readHeaderBSI(2);
240 if (($thisfile_ac3_raw_bsi['acmod'] & 0x01) && ($thisfile_ac3_raw_bsi['acmod'] > 2)) { // if three front channels exist
241 $thisfile_ac3_raw_bsi['ltrtcmixlev'] = $this->readHeaderBSI(3);
242 $thisfile_ac3_raw_bsi['lorocmixlev'] = $this->readHeaderBSI(3);
244 if ($thisfile_ac3_raw_bsi['acmod'] & 0x04) { // if a surround channel exists
245 $thisfile_ac3_raw_bsi['ltrtsurmixlev'] = $this->readHeaderBSI(3);
246 $thisfile_ac3_raw_bsi['lorosurmixlev'] = $this->readHeaderBSI(3);
248 if ($thisfile_ac3_raw_bsi['flags']['lfeon']) { // if the LFE channel exists
249 $thisfile_ac3_raw_bsi['flags']['lfemixlevcod'] = (bool) $this->readHeaderBSI(1);
250 if ($thisfile_ac3_raw_bsi['flags']['lfemixlevcod']) {
251 $thisfile_ac3_raw_bsi['lfemixlevcod'] = $this->readHeaderBSI(5);
254 if ($thisfile_ac3_raw_bsi['strmtyp'] == 0) { // if independent stream
255 $thisfile_ac3_raw_bsi['flags']['pgmscl'] = (bool) $this->readHeaderBSI(1);
256 if ($thisfile_ac3_raw_bsi['flags']['pgmscl']) {
257 $thisfile_ac3_raw_bsi['pgmscl'] = $this->readHeaderBSI(6);
259 if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value)
260 $thisfile_ac3_raw_bsi['flags']['pgmscl2'] = (bool) $this->readHeaderBSI(1);
261 if ($thisfile_ac3_raw_bsi['flags']['pgmscl2']) {
262 $thisfile_ac3_raw_bsi['pgmscl2'] = $this->readHeaderBSI(6);
265 $thisfile_ac3_raw_bsi['flags']['extpgmscl'] = (bool) $this->readHeaderBSI(1);
266 if ($thisfile_ac3_raw_bsi['flags']['extpgmscl']) {
267 $thisfile_ac3_raw_bsi['extpgmscl'] = $this->readHeaderBSI(6);
269 $thisfile_ac3_raw_bsi['mixdef'] = $this->readHeaderBSI(2);
270 if ($thisfile_ac3_raw_bsi['mixdef'] == 1) { // mixing option 2
271 $thisfile_ac3_raw_bsi['premixcmpsel'] = (bool) $this->readHeaderBSI(1);
272 $thisfile_ac3_raw_bsi['drcsrc'] = (bool) $this->readHeaderBSI(1);
273 $thisfile_ac3_raw_bsi['premixcmpscl'] = $this->readHeaderBSI(3);
274 } elseif ($thisfile_ac3_raw_bsi['mixdef'] == 2) { // mixing option 3
275 $thisfile_ac3_raw_bsi['mixdata'] = $this->readHeaderBSI(12);
276 } elseif ($thisfile_ac3_raw_bsi['mixdef'] == 3) { // mixing option 4
278 $thisfile_ac3_raw_bsi['mixdeflen'] = $this->readHeaderBSI(5); $mixdefbitsread +
= 5;
279 $thisfile_ac3_raw_bsi['flags']['mixdata2'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
280 if ($thisfile_ac3_raw_bsi['flags']['mixdata2']) {
281 $thisfile_ac3_raw_bsi['premixcmpsel'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
282 $thisfile_ac3_raw_bsi['drcsrc'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
283 $thisfile_ac3_raw_bsi['premixcmpscl'] = $this->readHeaderBSI(3); $mixdefbitsread +
= 3;
284 $thisfile_ac3_raw_bsi['flags']['extpgmlscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
285 if ($thisfile_ac3_raw_bsi['flags']['extpgmlscl']) {
286 $thisfile_ac3_raw_bsi['extpgmlscl'] = $this->readHeaderBSI(4); $mixdefbitsread +
= 4;
288 $thisfile_ac3_raw_bsi['flags']['extpgmcscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
289 if ($thisfile_ac3_raw_bsi['flags']['extpgmcscl']) {
290 $thisfile_ac3_raw_bsi['extpgmcscl'] = $this->readHeaderBSI(4); $mixdefbitsread +
= 4;
292 $thisfile_ac3_raw_bsi['flags']['extpgmrscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
293 if ($thisfile_ac3_raw_bsi['flags']['extpgmrscl']) {
294 $thisfile_ac3_raw_bsi['extpgmrscl'] = $this->readHeaderBSI(4);
296 $thisfile_ac3_raw_bsi['flags']['extpgmlsscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
297 if ($thisfile_ac3_raw_bsi['flags']['extpgmlsscl']) {
298 $thisfile_ac3_raw_bsi['extpgmlsscl'] = $this->readHeaderBSI(4); $mixdefbitsread +
= 4;
300 $thisfile_ac3_raw_bsi['flags']['extpgmrsscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
301 if ($thisfile_ac3_raw_bsi['flags']['extpgmrsscl']) {
302 $thisfile_ac3_raw_bsi['extpgmrsscl'] = $this->readHeaderBSI(4); $mixdefbitsread +
= 4;
304 $thisfile_ac3_raw_bsi['flags']['extpgmlfescl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
305 if ($thisfile_ac3_raw_bsi['flags']['extpgmlfescl']) {
306 $thisfile_ac3_raw_bsi['extpgmlfescl'] = $this->readHeaderBSI(4); $mixdefbitsread +
= 4;
308 $thisfile_ac3_raw_bsi['flags']['dmixscl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
309 if ($thisfile_ac3_raw_bsi['flags']['dmixscl']) {
310 $thisfile_ac3_raw_bsi['dmixscl'] = $this->readHeaderBSI(4); $mixdefbitsread +
= 4;
312 $thisfile_ac3_raw_bsi['flags']['addch'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
313 if ($thisfile_ac3_raw_bsi['flags']['addch']) {
314 $thisfile_ac3_raw_bsi['flags']['extpgmaux1scl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
315 if ($thisfile_ac3_raw_bsi['flags']['extpgmaux1scl']) {
316 $thisfile_ac3_raw_bsi['extpgmaux1scl'] = $this->readHeaderBSI(4); $mixdefbitsread +
= 4;
318 $thisfile_ac3_raw_bsi['flags']['extpgmaux2scl'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
319 if ($thisfile_ac3_raw_bsi['flags']['extpgmaux2scl']) {
320 $thisfile_ac3_raw_bsi['extpgmaux2scl'] = $this->readHeaderBSI(4); $mixdefbitsread +
= 4;
324 $thisfile_ac3_raw_bsi['flags']['mixdata3'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
325 if ($thisfile_ac3_raw_bsi['flags']['mixdata3']) {
326 $thisfile_ac3_raw_bsi['spchdat'] = $this->readHeaderBSI(5); $mixdefbitsread +
= 5;
327 $thisfile_ac3_raw_bsi['flags']['addspchdat'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
328 if ($thisfile_ac3_raw_bsi['flags']['addspchdat']) {
329 $thisfile_ac3_raw_bsi['spchdat1'] = $this->readHeaderBSI(5); $mixdefbitsread +
= 5;
330 $thisfile_ac3_raw_bsi['spchan1att'] = $this->readHeaderBSI(2); $mixdefbitsread +
= 2;
331 $thisfile_ac3_raw_bsi['flags']['addspchdat1'] = (bool) $this->readHeaderBSI(1); $mixdefbitsread +
= 1;
332 if ($thisfile_ac3_raw_bsi['flags']['addspchdat1']) {
333 $thisfile_ac3_raw_bsi['spchdat2'] = $this->readHeaderBSI(5); $mixdefbitsread +
= 5;
334 $thisfile_ac3_raw_bsi['spchan2att'] = $this->readHeaderBSI(3); $mixdefbitsread +
= 3;
338 $mixdata_bits = (8 * ($thisfile_ac3_raw_bsi['mixdeflen'] +
2)) - $mixdefbitsread;
339 $mixdata_fill = (($mixdata_bits %
8) ?
8 - ($mixdata_bits %
8) : 0);
340 $thisfile_ac3_raw_bsi['mixdata'] = $this->readHeaderBSI($mixdata_bits);
341 $thisfile_ac3_raw_bsi['mixdatafill'] = $this->readHeaderBSI($mixdata_fill);
342 unset($mixdefbitsread, $mixdata_bits, $mixdata_fill);
344 if ($thisfile_ac3_raw_bsi['acmod'] < 2) { // if mono or dual mono source
345 $thisfile_ac3_raw_bsi['flags']['paninfo'] = (bool) $this->readHeaderBSI(1);
346 if ($thisfile_ac3_raw_bsi['flags']['paninfo']) {
347 $thisfile_ac3_raw_bsi['panmean'] = $this->readHeaderBSI(8);
348 $thisfile_ac3_raw_bsi['paninfo'] = $this->readHeaderBSI(6);
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']['paninfo2'] = (bool) $this->readHeaderBSI(1);
352 if ($thisfile_ac3_raw_bsi['flags']['paninfo2']) {
353 $thisfile_ac3_raw_bsi['panmean2'] = $this->readHeaderBSI(8);
354 $thisfile_ac3_raw_bsi['paninfo2'] = $this->readHeaderBSI(6);
358 $thisfile_ac3_raw_bsi['flags']['frmmixcfginfo'] = (bool) $this->readHeaderBSI(1);
359 if ($thisfile_ac3_raw_bsi['flags']['frmmixcfginfo']) { // mixing configuration information
360 if ($thisfile_ac3_raw_bsi['numblkscod'] == 0) {
361 $thisfile_ac3_raw_bsi['blkmixcfginfo'][0] = $this->readHeaderBSI(5);
363 for ($blk = 0; $blk < $thisfile_ac3_raw_bsi['numblkscod']; $blk++
) {
364 $thisfile_ac3_raw_bsi['flags']['blkmixcfginfo'.$blk] = (bool) $this->readHeaderBSI(1);
365 if ($thisfile_ac3_raw_bsi['flags']['blkmixcfginfo'.$blk]) { // mixing configuration information
366 $thisfile_ac3_raw_bsi['blkmixcfginfo'][$blk] = $this->readHeaderBSI(5);
373 $thisfile_ac3_raw_bsi['flags']['infomdat'] = (bool) $this->readHeaderBSI(1);
374 if ($thisfile_ac3_raw_bsi['flags']['infomdat']) { // Informational metadata
375 $thisfile_ac3_raw_bsi['bsmod'] = $this->readHeaderBSI(3);
376 $thisfile_ac3_raw_bsi['flags']['copyrightb'] = (bool) $this->readHeaderBSI(1);
377 $thisfile_ac3_raw_bsi['flags']['origbs'] = (bool) $this->readHeaderBSI(1);
378 if ($thisfile_ac3_raw_bsi['acmod'] == 2) { // if in 2/0 mode
379 $thisfile_ac3_raw_bsi['dsurmod'] = $this->readHeaderBSI(2);
380 $thisfile_ac3_raw_bsi['dheadphonmod'] = $this->readHeaderBSI(2);
382 if ($thisfile_ac3_raw_bsi['acmod'] >= 6) { // if both surround channels exist
383 $thisfile_ac3_raw_bsi['dsurexmod'] = $this->readHeaderBSI(2);
385 $thisfile_ac3_raw_bsi['flags']['audprodi'] = (bool) $this->readHeaderBSI(1);
386 if ($thisfile_ac3_raw_bsi['flags']['audprodi']) {
387 $thisfile_ac3_raw_bsi['mixlevel'] = $this->readHeaderBSI(5);
388 $thisfile_ac3_raw_bsi['roomtyp'] = $this->readHeaderBSI(2);
389 $thisfile_ac3_raw_bsi['flags']['adconvtyp'] = (bool) $this->readHeaderBSI(1);
391 if ($thisfile_ac3_raw_bsi['acmod'] == 0) { // if 1+1 mode (dual mono, so some items need a second value)
392 $thisfile_ac3_raw_bsi['flags']['audprodi2'] = (bool) $this->readHeaderBSI(1);
393 if ($thisfile_ac3_raw_bsi['flags']['audprodi2']) {
394 $thisfile_ac3_raw_bsi['mixlevel2'] = $this->readHeaderBSI(5);
395 $thisfile_ac3_raw_bsi['roomtyp2'] = $this->readHeaderBSI(2);
396 $thisfile_ac3_raw_bsi['flags']['adconvtyp2'] = (bool) $this->readHeaderBSI(1);
399 if ($thisfile_ac3_raw_bsi['fscod'] < 3) { // if not half sample rate
400 $thisfile_ac3_raw_bsi['flags']['sourcefscod'] = (bool) $this->readHeaderBSI(1);
403 if (($thisfile_ac3_raw_bsi['strmtyp'] == 0) && ($thisfile_ac3_raw_bsi['numblkscod'] != 3)) { // if both surround channels exist
404 $thisfile_ac3_raw_bsi['flags']['convsync'] = (bool) $this->readHeaderBSI(1);
406 if ($thisfile_ac3_raw_bsi['strmtyp'] == 2) { // if bit stream converted from AC-3
407 if ($thisfile_ac3_raw_bsi['numblkscod'] != 3) { // 6 blocks per syncframe
408 $thisfile_ac3_raw_bsi['flags']['blkid'] = 1;
410 $thisfile_ac3_raw_bsi['flags']['blkid'] = (bool) $this->readHeaderBSI(1);
412 if ($thisfile_ac3_raw_bsi['flags']['blkid']) {
413 $thisfile_ac3_raw_bsi['frmsizecod'] = $this->readHeaderBSI(6);
416 $thisfile_ac3_raw_bsi['flags']['addbsi'] = (bool) $this->readHeaderBSI(1);
417 if ($thisfile_ac3_raw_bsi['flags']['addbsi']) {
418 $thisfile_ac3_raw_bsi['addbsil'] = $this->readHeaderBSI(6);
419 $thisfile_ac3_raw_bsi['addbsi'] = $this->readHeaderBSI(($thisfile_ac3_raw_bsi['addbsil'] +
1) * 8);
424 $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.');
430 if (isset($thisfile_ac3_raw_bsi['fscod2'])) {
431 $thisfile_ac3['sample_rate'] = self
::sampleRateCodeLookup2($thisfile_ac3_raw_bsi['fscod2']);
433 $thisfile_ac3['sample_rate'] = self
::sampleRateCodeLookup($thisfile_ac3_raw_bsi['fscod']);
435 if ($thisfile_ac3_raw_bsi['fscod'] <= 3) {
436 $info['audio']['sample_rate'] = $thisfile_ac3['sample_rate'];
438 $this->warning('Unexpected ac3.bsi.fscod value: '.$thisfile_ac3_raw_bsi['fscod']);
440 if (isset($thisfile_ac3_raw_bsi['frmsizecod'])) {
441 $thisfile_ac3['frame_length'] = self
::frameSizeLookup($thisfile_ac3_raw_bsi['frmsizecod'], $thisfile_ac3_raw_bsi['fscod']);
442 $thisfile_ac3['bitrate'] = self
::bitrateLookup($thisfile_ac3_raw_bsi['frmsizecod']);
443 } elseif (!empty($thisfile_ac3_raw_bsi['frmsiz'])) {
444 // this isn't right, but it's (usually) close, roughly 5% less than it should be.
445 // but WHERE is the actual bitrate value stored in EAC3?? email info@getid3.org if you know!
446 $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.
447 // kludge-fix to make it approximately the expected value, still not "right":
448 $thisfile_ac3['bitrate'] = round(($thisfile_ac3['bitrate'] * 1.05) / 16000) * 16000;
450 $info['audio']['bitrate'] = $thisfile_ac3['bitrate'];
452 if (isset($thisfile_ac3_raw_bsi['bsmod']) && isset($thisfile_ac3_raw_bsi['acmod'])) {
453 $thisfile_ac3['service_type'] = self
::serviceTypeLookup($thisfile_ac3_raw_bsi['bsmod'], $thisfile_ac3_raw_bsi['acmod']);
455 $ac3_coding_mode = self
::audioCodingModeLookup($thisfile_ac3_raw_bsi['acmod']);
456 foreach($ac3_coding_mode as $key => $value) {
457 $thisfile_ac3[$key] = $value;
459 switch ($thisfile_ac3_raw_bsi['acmod']) {
462 $info['audio']['channelmode'] = 'mono';
466 $info['audio']['channelmode'] = 'stereo';
469 $info['audio']['channelmode'] = 'surround';
472 $info['audio']['channels'] = $thisfile_ac3['num_channels'];
474 $thisfile_ac3['lfe_enabled'] = $thisfile_ac3_raw_bsi['flags']['lfeon'];
475 if ($thisfile_ac3_raw_bsi['flags']['lfeon']) {
476 $info['audio']['channels'] .= '.1';
479 $thisfile_ac3['channels_enabled'] = self
::channelsEnabledLookup($thisfile_ac3_raw_bsi['acmod'], $thisfile_ac3_raw_bsi['flags']['lfeon']);
480 $thisfile_ac3['dialogue_normalization'] = '-'.$thisfile_ac3_raw_bsi['dialnorm'].'dB';
490 private function readHeaderBSI($length) {
491 $data = substr($this->AC3header
['bsi'], $this->BSIoffset
, $length);
492 $this->BSIoffset +
= $length;
494 return bindec($data);
500 * @return int|string|false
502 public static function sampleRateCodeLookup($fscod) {
503 static $sampleRateCodeLookup = array(
507 3 => 'reserved' // If the reserved code is indicated, the decoder should not attempt to decode audio and should mute.
509 return (isset($sampleRateCodeLookup[$fscod]) ?
$sampleRateCodeLookup[$fscod] : false);
515 * @return int|string|false
517 public static function sampleRateCodeLookup2($fscod2) {
518 static $sampleRateCodeLookup2 = array(
522 3 => 'reserved' // If the reserved code is indicated, the decoder should not attempt to decode audio and should mute.
524 return (isset($sampleRateCodeLookup2[$fscod2]) ?
$sampleRateCodeLookup2[$fscod2] : false);
531 * @return string|false
533 public static function serviceTypeLookup($bsmod, $acmod) {
534 static $serviceTypeLookup = array();
535 if (empty($serviceTypeLookup)) {
536 for ($i = 0; $i <= 7; $i++
) {
537 $serviceTypeLookup[0][$i] = 'main audio service: complete main (CM)';
538 $serviceTypeLookup[1][$i] = 'main audio service: music and effects (ME)';
539 $serviceTypeLookup[2][$i] = 'associated service: visually impaired (VI)';
540 $serviceTypeLookup[3][$i] = 'associated service: hearing impaired (HI)';
541 $serviceTypeLookup[4][$i] = 'associated service: dialogue (D)';
542 $serviceTypeLookup[5][$i] = 'associated service: commentary (C)';
543 $serviceTypeLookup[6][$i] = 'associated service: emergency (E)';
546 $serviceTypeLookup[7][1] = 'associated service: voice over (VO)';
547 for ($i = 2; $i <= 7; $i++
) {
548 $serviceTypeLookup[7][$i] = 'main audio service: karaoke';
551 return (isset($serviceTypeLookup[$bsmod][$acmod]) ?
$serviceTypeLookup[$bsmod][$acmod] : false);
557 * @return array|false
559 public static function audioCodingModeLookup($acmod) {
560 // array(channel configuration, # channels (not incl LFE), channel order)
561 static $audioCodingModeLookup = array (
562 0 => array('channel_config'=>'1+1', 'num_channels'=>2, 'channel_order'=>'Ch1,Ch2'),
563 1 => array('channel_config'=>'1/0', 'num_channels'=>1, 'channel_order'=>'C'),
564 2 => array('channel_config'=>'2/0', 'num_channels'=>2, 'channel_order'=>'L,R'),
565 3 => array('channel_config'=>'3/0', 'num_channels'=>3, 'channel_order'=>'L,C,R'),
566 4 => array('channel_config'=>'2/1', 'num_channels'=>3, 'channel_order'=>'L,R,S'),
567 5 => array('channel_config'=>'3/1', 'num_channels'=>4, 'channel_order'=>'L,C,R,S'),
568 6 => array('channel_config'=>'2/2', 'num_channels'=>4, 'channel_order'=>'L,R,SL,SR'),
569 7 => array('channel_config'=>'3/2', 'num_channels'=>5, 'channel_order'=>'L,C,R,SL,SR'),
571 return (isset($audioCodingModeLookup[$acmod]) ?
$audioCodingModeLookup[$acmod] : false);
575 * @param int $cmixlev
577 * @return int|float|string|false
579 public static function centerMixLevelLookup($cmixlev) {
580 static $centerMixLevelLookup;
581 if (empty($centerMixLevelLookup)) {
582 $centerMixLevelLookup = array(
583 0 => pow(2, -3.0 / 6), // 0.707 (-3.0 dB)
584 1 => pow(2, -4.5 / 6), // 0.595 (-4.5 dB)
585 2 => pow(2, -6.0 / 6), // 0.500 (-6.0 dB)
589 return (isset($centerMixLevelLookup[$cmixlev]) ?
$centerMixLevelLookup[$cmixlev] : false);
593 * @param int $surmixlev
595 * @return int|float|string|false
597 public static function surroundMixLevelLookup($surmixlev) {
598 static $surroundMixLevelLookup;
599 if (empty($surroundMixLevelLookup)) {
600 $surroundMixLevelLookup = array(
601 0 => pow(2, -3.0 / 6),
602 1 => pow(2, -6.0 / 6),
607 return (isset($surroundMixLevelLookup[$surmixlev]) ?
$surroundMixLevelLookup[$surmixlev] : false);
611 * @param int $dsurmod
613 * @return string|false
615 public static function dolbySurroundModeLookup($dsurmod) {
616 static $dolbySurroundModeLookup = array(
617 0 => 'not indicated',
618 1 => 'Not Dolby Surround encoded',
619 2 => 'Dolby Surround encoded',
622 return (isset($dolbySurroundModeLookup[$dsurmod]) ?
$dolbySurroundModeLookup[$dsurmod] : false);
631 public static function channelsEnabledLookup($acmod, $lfeon) {
633 'ch1'=>($acmod == 0),
634 'ch2'=>($acmod == 0),
635 'left'=>($acmod > 1),
636 'right'=>($acmod > 1),
637 'center'=>(bool) ($acmod & 0x01),
638 'surround_mono'=>false,
639 'surround_left'=>false,
640 'surround_right'=>false,
645 $lookup['surround_mono'] = true;
649 $lookup['surround_left'] = true;
650 $lookup['surround_right'] = true;
661 public static function heavyCompression($compre) {
662 // The first four bits indicate gain changes in 6.02dB increments which can be
663 // implemented with an arithmetic shift operation. The following four bits
664 // indicate linear gain changes, and require a 5-bit multiply.
665 // We will represent the two 4-bit fields of compr as follows:
666 // X0 X1 X2 X3 . Y4 Y5 Y6 Y7
667 // The meaning of the X values is most simply described by considering X to represent a 4-bit
668 // signed integer with values from -8 to +7. The gain indicated by X is then (X + 1) * 6.02 dB. The
669 // following table shows this in detail.
671 // Meaning of 4 msb of compr
689 $fourbit = str_pad(decbin(($compre & 0xF0) >> 4), 4, '0', STR_PAD_LEFT
);
690 if ($fourbit{0} == '1') {
691 $log_gain = -8 +
bindec(substr($fourbit, 1));
693 $log_gain = bindec(substr($fourbit, 1));
695 $log_gain = ($log_gain +
1) * getid3_lib
::RGADamplitude2dB(2);
697 // The value of Y is a linear representation of a gain change of up to -6 dB. Y is considered to
698 // be an unsigned fractional integer, with a leading value of 1, or: 0.1 Y4 Y5 Y6 Y7 (base 2). Y can
699 // represent values between 0.111112 (or 31/32) and 0.100002 (or 1/2). Thus, Y can represent gain
700 // changes from -0.28 dB to -6.02 dB.
702 $lin_gain = (16 +
($compre & 0x0F)) / 32;
704 // The combination of X and Y values allows compr to indicate gain changes from
705 // 48.16 - 0.28 = +47.89 dB, to
706 // -42.14 - 6.02 = -48.16 dB.
708 return $log_gain - $lin_gain;
712 * @param int $roomtyp
714 * @return string|false
716 public static function roomTypeLookup($roomtyp) {
717 static $roomTypeLookup = array(
718 0 => 'not indicated',
719 1 => 'large room, X curve monitor',
720 2 => 'small room, flat monitor',
723 return (isset($roomTypeLookup[$roomtyp]) ?
$roomTypeLookup[$roomtyp] : false);
727 * @param int $frmsizecod
732 public static function frameSizeLookup($frmsizecod, $fscod) {
733 // LSB is whether padding is used or not
734 $padding = (bool) ($frmsizecod & 0x01);
735 $framesizeid = ($frmsizecod & 0x3E) >> 1;
737 static $frameSizeLookup = array();
738 if (empty($frameSizeLookup)) {
739 $frameSizeLookup = array (
740 0 => array( 128, 138, 192), // 32 kbps
741 1 => array( 160, 174, 240), // 40 kbps
742 2 => array( 192, 208, 288), // 48 kbps
743 3 => array( 224, 242, 336), // 56 kbps
744 4 => array( 256, 278, 384), // 64 kbps
745 5 => array( 320, 348, 480), // 80 kbps
746 6 => array( 384, 416, 576), // 96 kbps
747 7 => array( 448, 486, 672), // 112 kbps
748 8 => array( 512, 556, 768), // 128 kbps
749 9 => array( 640, 696, 960), // 160 kbps
750 10 => array( 768, 834, 1152), // 192 kbps
751 11 => array( 896, 974, 1344), // 224 kbps
752 12 => array(1024, 1114, 1536), // 256 kbps
753 13 => array(1280, 1392, 1920), // 320 kbps
754 14 => array(1536, 1670, 2304), // 384 kbps
755 15 => array(1792, 1950, 2688), // 448 kbps
756 16 => array(2048, 2228, 3072), // 512 kbps
757 17 => array(2304, 2506, 3456), // 576 kbps
758 18 => array(2560, 2786, 3840) // 640 kbps
761 if (($fscod == 1) && $padding) {
762 // frame lengths are padded by 1 word (16 bits) at 44100
763 $frameSizeLookup[$frmsizecod] +
= 2;
765 return (isset($frameSizeLookup[$framesizeid][$fscod]) ?
$frameSizeLookup[$framesizeid][$fscod] : false);
769 * @param int $frmsizecod
773 public static function bitrateLookup($frmsizecod) {
774 // LSB is whether padding is used or not
775 $padding = (bool) ($frmsizecod & 0x01);
776 $framesizeid = ($frmsizecod & 0x3E) >> 1;
778 static $bitrateLookup = array(
799 return (isset($bitrateLookup[$framesizeid]) ?
$bitrateLookup[$framesizeid] : false);
803 * @param int $numblkscod
807 public static function blocksPerSyncFrame($numblkscod) {
808 static $blocksPerSyncFrameLookup = array(
814 return (isset($blocksPerSyncFrameLookup[$numblkscod]) ?
$blocksPerSyncFrameLookup[$numblkscod] : false);