1<?php
2
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/////////////////////////////////////////////////////////////////
10//                                                             //
11// module.audio.aac.php                                        //
12// module for analyzing AAC Audio files                        //
13// dependencies: NONE                                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20
21class getid3_aac extends getid3_handler
22{
23	/**
24	 * @return bool
25	 */
26	public function Analyze() {
27		$info = &$this->getid3->info;
28		$this->fseek($info['avdataoffset']);
29		if ($this->fread(4) == 'ADIF') {
30			$this->getAACADIFheaderFilepointer();
31		} else {
32			$this->getAACADTSheaderFilepointer();
33		}
34		return true;
35	}
36
37	/**
38	 * @return bool
39	 */
40	public function getAACADIFheaderFilepointer() {
41		$info = &$this->getid3->info;
42		$info['fileformat']          = 'aac';
43		$info['audio']['dataformat'] = 'aac';
44		$info['audio']['lossless']   = false;
45
46		$this->fseek($info['avdataoffset']);
47		$AACheader = $this->fread(1024);
48		$offset    = 0;
49
50		if (substr($AACheader, 0, 4) == 'ADIF') {
51
52			// http://faac.sourceforge.net/wiki/index.php?page=ADIF
53
54			// http://libmpeg.org/mpeg4/doc/w2203tfs.pdf
55			// adif_header() {
56			//     adif_id                                32
57			//     copyright_id_present                    1
58			//     if( copyright_id_present )
59			//         copyright_id                       72
60			//     original_copy                           1
61			//     home                                    1
62			//     bitstream_type                          1
63			//     bitrate                                23
64			//     num_program_config_elements             4
65			//     for (i = 0; i < num_program_config_elements + 1; i++ ) {
66			//         if( bitstream_type == '0' )
67			//             adif_buffer_fullness           20
68			//         program_config_element()
69			//     }
70			// }
71
72			$AACheaderBitstream = getid3_lib::BigEndian2Bin($AACheader);
73			$bitoffset          = 0;
74
75			$info['aac']['header_type']                   = 'ADIF';
76			$bitoffset += 32;
77			$info['aac']['header']['mpeg_version']        = 4;
78
79			$info['aac']['header']['copyright']           = substr($AACheaderBitstream, $bitoffset, 1) == '1';
80			$bitoffset += 1;
81			if ($info['aac']['header']['copyright']) {
82				$info['aac']['header']['copyright_id']    = getid3_lib::Bin2String(substr($AACheaderBitstream, $bitoffset, 72));
83				$bitoffset += 72;
84			}
85			$info['aac']['header']['original_copy']       = substr($AACheaderBitstream, $bitoffset, 1) == '1';
86			$bitoffset += 1;
87			$info['aac']['header']['home']                = substr($AACheaderBitstream, $bitoffset, 1) == '1';
88			$bitoffset += 1;
89			$info['aac']['header']['is_vbr']              = substr($AACheaderBitstream, $bitoffset, 1) == '1';
90			$bitoffset += 1;
91			if ($info['aac']['header']['is_vbr']) {
92				$info['audio']['bitrate_mode']            = 'vbr';
93				$info['aac']['header']['bitrate_max']     = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 23));
94				$bitoffset += 23;
95			} else {
96				$info['audio']['bitrate_mode']            = 'cbr';
97				$info['aac']['header']['bitrate']         = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 23));
98				$bitoffset += 23;
99				$info['audio']['bitrate']                 = $info['aac']['header']['bitrate'];
100			}
101			if ($info['audio']['bitrate'] == 0) {
102				$this->error('Corrupt AAC file: bitrate_audio == zero');
103				return false;
104			}
105			$info['aac']['header']['num_program_configs'] = 1 + getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
106			$bitoffset += 4;
107
108			for ($i = 0; $i < $info['aac']['header']['num_program_configs']; $i++) {
109				// http://www.audiocoding.com/wiki/index.php?page=program_config_element
110
111				// buffer_fullness                       20
112
113				// element_instance_tag                   4
114				// object_type                            2
115				// sampling_frequency_index               4
116				// num_front_channel_elements             4
117				// num_side_channel_elements              4
118				// num_back_channel_elements              4
119				// num_lfe_channel_elements               2
120				// num_assoc_data_elements                3
121				// num_valid_cc_elements                  4
122				// mono_mixdown_present                   1
123				// mono_mixdown_element_number            4   if mono_mixdown_present == 1
124				// stereo_mixdown_present                 1
125				// stereo_mixdown_element_number          4   if stereo_mixdown_present == 1
126				// matrix_mixdown_idx_present             1
127				// matrix_mixdown_idx                     2   if matrix_mixdown_idx_present == 1
128				// pseudo_surround_enable                 1   if matrix_mixdown_idx_present == 1
129				// for (i = 0; i < num_front_channel_elements; i++) {
130				//     front_element_is_cpe[i]            1
131				//     front_element_tag_select[i]        4
132				// }
133				// for (i = 0; i < num_side_channel_elements; i++) {
134				//     side_element_is_cpe[i]             1
135				//     side_element_tag_select[i]         4
136				// }
137				// for (i = 0; i < num_back_channel_elements; i++) {
138				//     back_element_is_cpe[i]             1
139				//     back_element_tag_select[i]         4
140				// }
141				// for (i = 0; i < num_lfe_channel_elements; i++) {
142				//     lfe_element_tag_select[i]          4
143				// }
144				// for (i = 0; i < num_assoc_data_elements; i++) {
145				//     assoc_data_element_tag_select[i]   4
146				// }
147				// for (i = 0; i < num_valid_cc_elements; i++) {
148				//     cc_element_is_ind_sw[i]            1
149				//     valid_cc_element_tag_select[i]     4
150				// }
151				// byte_alignment()                       VAR
152				// comment_field_bytes                    8
153				// for (i = 0; i < comment_field_bytes; i++) {
154				//     comment_field_data[i]              8
155				// }
156
157				if (!$info['aac']['header']['is_vbr']) {
158					$info['aac']['program_configs'][$i]['buffer_fullness']        = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 20));
159					$bitoffset += 20;
160				}
161				$info['aac']['program_configs'][$i]['element_instance_tag']       = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
162				$bitoffset += 4;
163				$info['aac']['program_configs'][$i]['object_type']                = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
164				$bitoffset += 2;
165				$info['aac']['program_configs'][$i]['sampling_frequency_index']   = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
166				$bitoffset += 4;
167				$info['aac']['program_configs'][$i]['num_front_channel_elements'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
168				$bitoffset += 4;
169				$info['aac']['program_configs'][$i]['num_side_channel_elements']  = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
170				$bitoffset += 4;
171				$info['aac']['program_configs'][$i]['num_back_channel_elements']  = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
172				$bitoffset += 4;
173				$info['aac']['program_configs'][$i]['num_lfe_channel_elements']   = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
174				$bitoffset += 2;
175				$info['aac']['program_configs'][$i]['num_assoc_data_elements']    = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 3));
176				$bitoffset += 3;
177				$info['aac']['program_configs'][$i]['num_valid_cc_elements']      = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
178				$bitoffset += 4;
179				$info['aac']['program_configs'][$i]['mono_mixdown_present']       = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
180				$bitoffset += 1;
181				if ($info['aac']['program_configs'][$i]['mono_mixdown_present']) {
182					$info['aac']['program_configs'][$i]['mono_mixdown_element_number']    = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
183					$bitoffset += 4;
184				}
185				$info['aac']['program_configs'][$i]['stereo_mixdown_present']             = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
186				$bitoffset += 1;
187				if ($info['aac']['program_configs'][$i]['stereo_mixdown_present']) {
188					$info['aac']['program_configs'][$i]['stereo_mixdown_element_number']  = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
189					$bitoffset += 4;
190				}
191				$info['aac']['program_configs'][$i]['matrix_mixdown_idx_present']         = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
192				$bitoffset += 1;
193				if ($info['aac']['program_configs'][$i]['matrix_mixdown_idx_present']) {
194					$info['aac']['program_configs'][$i]['matrix_mixdown_idx']             = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
195					$bitoffset += 2;
196					$info['aac']['program_configs'][$i]['pseudo_surround_enable']         = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
197					$bitoffset += 1;
198				}
199				for ($j = 0; $j < $info['aac']['program_configs'][$i]['num_front_channel_elements']; $j++) {
200					$info['aac']['program_configs'][$i]['front_element_is_cpe'][$j]     = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
201					$bitoffset += 1;
202					$info['aac']['program_configs'][$i]['front_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
203					$bitoffset += 4;
204				}
205				for ($j = 0; $j < $info['aac']['program_configs'][$i]['num_side_channel_elements']; $j++) {
206					$info['aac']['program_configs'][$i]['side_element_is_cpe'][$j]     = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
207					$bitoffset += 1;
208					$info['aac']['program_configs'][$i]['side_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
209					$bitoffset += 4;
210				}
211				for ($j = 0; $j < $info['aac']['program_configs'][$i]['num_back_channel_elements']; $j++) {
212					$info['aac']['program_configs'][$i]['back_element_is_cpe'][$j]     = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
213					$bitoffset += 1;
214					$info['aac']['program_configs'][$i]['back_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
215					$bitoffset += 4;
216				}
217				for ($j = 0; $j < $info['aac']['program_configs'][$i]['num_lfe_channel_elements']; $j++) {
218					$info['aac']['program_configs'][$i]['lfe_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
219					$bitoffset += 4;
220				}
221				for ($j = 0; $j < $info['aac']['program_configs'][$i]['num_assoc_data_elements']; $j++) {
222					$info['aac']['program_configs'][$i]['assoc_data_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
223					$bitoffset += 4;
224				}
225				for ($j = 0; $j < $info['aac']['program_configs'][$i]['num_valid_cc_elements']; $j++) {
226					$info['aac']['program_configs'][$i]['cc_element_is_ind_sw'][$j]          = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
227					$bitoffset += 1;
228					$info['aac']['program_configs'][$i]['valid_cc_element_tag_select'][$j]   = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
229					$bitoffset += 4;
230				}
231
232				$bitoffset = ceil($bitoffset / 8) * 8;
233
234				$info['aac']['program_configs'][$i]['comment_field_bytes'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 8));
235				$bitoffset += 8;
236				$info['aac']['program_configs'][$i]['comment_field']       = getid3_lib::Bin2String(substr($AACheaderBitstream, $bitoffset, 8 * $info['aac']['program_configs'][$i]['comment_field_bytes']));
237				$bitoffset += 8 * $info['aac']['program_configs'][$i]['comment_field_bytes'];
238
239
240				$info['aac']['header']['profile']                           = self::AACprofileLookup($info['aac']['program_configs'][$i]['object_type'], $info['aac']['header']['mpeg_version']);
241				$info['aac']['program_configs'][$i]['sampling_frequency']   = self::AACsampleRateLookup($info['aac']['program_configs'][$i]['sampling_frequency_index']);
242				$info['audio']['sample_rate']                               = $info['aac']['program_configs'][$i]['sampling_frequency'];
243				$info['audio']['channels']                                  = self::AACchannelCountCalculate($info['aac']['program_configs'][$i]);
244				if ($info['aac']['program_configs'][$i]['comment_field']) {
245					$info['aac']['comments'][]                          = $info['aac']['program_configs'][$i]['comment_field'];
246				}
247			}
248			$info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate'];
249
250			$info['audio']['encoder_options'] = $info['aac']['header_type'].' '.$info['aac']['header']['profile'];
251
252
253
254			return true;
255
256		} else {
257
258			unset($info['fileformat']);
259			unset($info['aac']);
260			$this->error('AAC-ADIF synch not found at offset '.$info['avdataoffset'].' (expected "ADIF", found "'.substr($AACheader, 0, 4).'" instead)');
261			return false;
262
263		}
264
265	}
266
267	/**
268	 * @param int  $MaxFramesToScan
269	 * @param bool $ReturnExtendedInfo
270	 *
271	 * @return bool
272	 */
273	public function getAACADTSheaderFilepointer($MaxFramesToScan=1000000, $ReturnExtendedInfo=false) {
274		$info = &$this->getid3->info;
275
276		// based loosely on code from AACfile by Jurgen Faul  <jfaulØgmx.de>
277		// http://jfaul.de/atl  or  http://j-faul.virtualave.net/atl/atl.html
278
279
280		// http://faac.sourceforge.net/wiki/index.php?page=ADTS // dead link
281		// http://wiki.multimedia.cx/index.php?title=ADTS
282
283		// * ADTS Fixed Header: these don't change from frame to frame
284		// syncword                                       12    always: '111111111111'
285		// ID                                              1    0: MPEG-4, 1: MPEG-2
286		// MPEG layer                                      2    If you send AAC in MPEG-TS, set to 0
287		// protection_absent                               1    0: CRC present; 1: no CRC
288		// profile                                         2    0: AAC Main; 1: AAC LC (Low Complexity); 2: AAC SSR (Scalable Sample Rate); 3: AAC LTP (Long Term Prediction)
289		// sampling_frequency_index                        4    15 not allowed
290		// private_bit                                     1    usually 0
291		// channel_configuration                           3
292		// original/copy                                   1    0: original; 1: copy
293		// home                                            1    usually 0
294		// emphasis                                        2    only if ID == 0 (ie MPEG-4)  // not present in some documentation?
295
296		// * ADTS Variable Header: these can change from frame to frame
297		// copyright_identification_bit                    1
298		// copyright_identification_start                  1
299		// aac_frame_length                               13    length of the frame including header (in bytes)
300		// adts_buffer_fullness                           11    0x7FF indicates VBR
301		// no_raw_data_blocks_in_frame                     2
302
303		// * ADTS Error check
304		// crc_check                                      16    only if protection_absent == 0
305
306		$byteoffset  = $info['avdataoffset'];
307		$framenumber = 0;
308
309		// Init bit pattern array
310		static $decbin = array();
311
312		// Populate $bindec
313		for ($i = 0; $i < 256; $i++) {
314			$decbin[chr($i)] = str_pad(decbin($i), 8, '0', STR_PAD_LEFT);
315		}
316
317		// used to calculate bitrate below
318		$BitrateCache = array();
319
320
321		while (true) {
322			// breaks out when end-of-file encountered, or invalid data found,
323			// or MaxFramesToScan frames have been scanned
324
325			if (!getid3_lib::intValueSupported($byteoffset)) {
326				$this->warning('Unable to parse AAC file beyond '.$this->ftell().' (PHP does not support file operations beyond '.round(PHP_INT_MAX / 1073741824).'GB)');
327				return false;
328			}
329			$this->fseek($byteoffset);
330
331			// First get substring
332			$substring = $this->fread(9); // header is 7 bytes (or 9 if CRC is present)
333			$substringlength = strlen($substring);
334			if ($substringlength != 9) {
335				$this->error('Failed to read 7 bytes at offset '.($this->ftell() - $substringlength).' (only read '.$substringlength.' bytes)');
336				return false;
337			}
338			// this would be easier with 64-bit math, but split it up to allow for 32-bit:
339			$header1 = getid3_lib::BigEndian2Int(substr($substring, 0, 2));
340			$header2 = getid3_lib::BigEndian2Int(substr($substring, 2, 4));
341			$header3 = getid3_lib::BigEndian2Int(substr($substring, 6, 1));
342
343			$info['aac']['header']['raw']['syncword']          = ($header1 & 0xFFF0) >> 4;
344			if ($info['aac']['header']['raw']['syncword'] != 0x0FFF) {
345				$this->error('Synch pattern (0x0FFF) not found at offset '.($this->ftell() - $substringlength).' (found 0x0'.strtoupper(dechex($info['aac']['header']['raw']['syncword'])).' instead)');
346				//if ($info['fileformat'] == 'aac') {
347				//	return true;
348				//}
349				unset($info['aac']);
350				return false;
351			}
352
353			// Gather info for first frame only - this takes time to do 1000 times!
354			if ($framenumber == 0) {
355				$info['aac']['header_type']                      = 'ADTS';
356				$info['fileformat']                              = 'aac';
357				$info['audio']['dataformat']                     = 'aac';
358
359				$info['aac']['header']['raw']['mpeg_version']      = ($header1 & 0x0008) >> 3;
360				$info['aac']['header']['raw']['mpeg_layer']        = ($header1 & 0x0006) >> 1;
361				$info['aac']['header']['raw']['protection_absent'] = ($header1 & 0x0001) >> 0;
362
363				$info['aac']['header']['raw']['profile_code']      = ($header2 & 0xC0000000) >> 30;
364				$info['aac']['header']['raw']['sample_rate_code']  = ($header2 & 0x3C000000) >> 26;
365				$info['aac']['header']['raw']['private_stream']    = ($header2 & 0x02000000) >> 25;
366				$info['aac']['header']['raw']['channels_code']     = ($header2 & 0x01C00000) >> 22;
367				$info['aac']['header']['raw']['original']          = ($header2 & 0x00200000) >> 21;
368				$info['aac']['header']['raw']['home']              = ($header2 & 0x00100000) >> 20;
369				$info['aac']['header']['raw']['copyright_stream']  = ($header2 & 0x00080000) >> 19;
370				$info['aac']['header']['raw']['copyright_start']   = ($header2 & 0x00040000) >> 18;
371				$info['aac']['header']['raw']['frame_length']      = ($header2 & 0x0003FFE0) >>  5;
372
373				$info['aac']['header']['mpeg_version']     = ($info['aac']['header']['raw']['mpeg_version']      ? 2    : 4);
374				$info['aac']['header']['crc_present']      = ($info['aac']['header']['raw']['protection_absent'] ? false: true);
375				$info['aac']['header']['profile']          = self::AACprofileLookup($info['aac']['header']['raw']['profile_code'], $info['aac']['header']['mpeg_version']);
376				$info['aac']['header']['sample_frequency'] = self::AACsampleRateLookup($info['aac']['header']['raw']['sample_rate_code']);
377				$info['aac']['header']['private']          = (bool) $info['aac']['header']['raw']['private_stream'];
378				$info['aac']['header']['original']         = (bool) $info['aac']['header']['raw']['original'];
379				$info['aac']['header']['home']             = (bool) $info['aac']['header']['raw']['home'];
380				$info['aac']['header']['channels']         = (($info['aac']['header']['raw']['channels_code'] == 7) ? 8 : $info['aac']['header']['raw']['channels_code']);
381				if ($ReturnExtendedInfo) {
382					$info['aac'][$framenumber]['copyright_id_bit']   = (bool) $info['aac']['header']['raw']['copyright_stream'];
383					$info['aac'][$framenumber]['copyright_id_start'] = (bool) $info['aac']['header']['raw']['copyright_start'];
384				}
385
386				if ($info['aac']['header']['raw']['mpeg_layer'] != 0) {
387					$this->warning('Layer error - expected "0", found "'.$info['aac']['header']['raw']['mpeg_layer'].'" instead');
388				}
389				if ($info['aac']['header']['sample_frequency'] == 0) {
390					$this->error('Corrupt AAC file: sample_frequency == zero');
391					return false;
392				}
393
394				$info['audio']['sample_rate'] = $info['aac']['header']['sample_frequency'];
395				$info['audio']['channels']    = $info['aac']['header']['channels'];
396			}
397
398			$FrameLength = ($header2 & 0x0003FFE0) >>  5;
399
400			if (!isset($BitrateCache[$FrameLength])) {
401				$BitrateCache[$FrameLength] = ($info['aac']['header']['sample_frequency'] / 1024) * $FrameLength * 8;
402			}
403			getid3_lib::safe_inc($info['aac']['bitrate_distribution'][$BitrateCache[$FrameLength]], 1);
404
405			$info['aac'][$framenumber]['aac_frame_length']     = $FrameLength;
406
407			$info['aac'][$framenumber]['adts_buffer_fullness'] = (($header2 & 0x0000001F) << 6) & (($header3 & 0xFC) >> 2);
408			if ($info['aac'][$framenumber]['adts_buffer_fullness'] == 0x07FF) {
409				$info['audio']['bitrate_mode'] = 'vbr';
410			} else {
411				$info['audio']['bitrate_mode'] = 'cbr';
412			}
413			$info['aac'][$framenumber]['num_raw_data_blocks']  = (($header3 & 0x03) >> 0);
414
415			if ($info['aac']['header']['crc_present']) {
416				//$info['aac'][$framenumber]['crc'] = getid3_lib::BigEndian2Int(substr($substring, 7, 2);
417			}
418
419			if (!$ReturnExtendedInfo) {
420				unset($info['aac'][$framenumber]);
421			}
422
423			/*
424			$rounded_precision = 5000;
425			$info['aac']['bitrate_distribution_rounded'] = array();
426			foreach ($info['aac']['bitrate_distribution'] as $bitrate => $count) {
427				$rounded_bitrate = round($bitrate / $rounded_precision) * $rounded_precision;
428				getid3_lib::safe_inc($info['aac']['bitrate_distribution_rounded'][$rounded_bitrate], $count);
429			}
430			ksort($info['aac']['bitrate_distribution_rounded']);
431			*/
432
433			$byteoffset += $FrameLength;
434			if ((++$framenumber < $MaxFramesToScan) && (($byteoffset + 10) < $info['avdataend'])) {
435
436				// keep scanning
437
438			} else {
439
440				$info['aac']['frames']    = $framenumber;
441				$info['playtime_seconds'] = ($info['avdataend'] / $byteoffset) * (($framenumber * 1024) / $info['aac']['header']['sample_frequency']);  // (1 / % of file scanned) * (samples / (samples/sec)) = seconds
442				if ($info['playtime_seconds'] == 0) {
443					$this->error('Corrupt AAC file: playtime_seconds == zero');
444					return false;
445				}
446				$info['audio']['bitrate']    = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
447				ksort($info['aac']['bitrate_distribution']);
448
449				$info['audio']['encoder_options'] = $info['aac']['header_type'].' '.$info['aac']['header']['profile'];
450
451				return true;
452
453			}
454		}
455		// should never get here.
456	}
457
458	/**
459	 * @param int $samplerateid
460	 *
461	 * @return int|string
462	 */
463	public static function AACsampleRateLookup($samplerateid) {
464		static $AACsampleRateLookup = array();
465		if (empty($AACsampleRateLookup)) {
466			$AACsampleRateLookup[0]  = 96000;
467			$AACsampleRateLookup[1]  = 88200;
468			$AACsampleRateLookup[2]  = 64000;
469			$AACsampleRateLookup[3]  = 48000;
470			$AACsampleRateLookup[4]  = 44100;
471			$AACsampleRateLookup[5]  = 32000;
472			$AACsampleRateLookup[6]  = 24000;
473			$AACsampleRateLookup[7]  = 22050;
474			$AACsampleRateLookup[8]  = 16000;
475			$AACsampleRateLookup[9]  = 12000;
476			$AACsampleRateLookup[10] = 11025;
477			$AACsampleRateLookup[11] = 8000;
478			$AACsampleRateLookup[12] = 0;
479			$AACsampleRateLookup[13] = 0;
480			$AACsampleRateLookup[14] = 0;
481			$AACsampleRateLookup[15] = 0;
482		}
483		return (isset($AACsampleRateLookup[$samplerateid]) ? $AACsampleRateLookup[$samplerateid] : 'invalid');
484	}
485
486	/**
487	 * @param int $profileid
488	 * @param int $mpegversion
489	 *
490	 * @return string
491	 */
492	public static function AACprofileLookup($profileid, $mpegversion) {
493		static $AACprofileLookup = array();
494		if (empty($AACprofileLookup)) {
495			$AACprofileLookup[2][0]  = 'Main profile';
496			$AACprofileLookup[2][1]  = 'Low Complexity profile (LC)';
497			$AACprofileLookup[2][2]  = 'Scalable Sample Rate profile (SSR)';
498			$AACprofileLookup[2][3]  = '(reserved)';
499			$AACprofileLookup[4][0]  = 'AAC_MAIN';
500			$AACprofileLookup[4][1]  = 'AAC_LC';
501			$AACprofileLookup[4][2]  = 'AAC_SSR';
502			$AACprofileLookup[4][3]  = 'AAC_LTP';
503		}
504		return (isset($AACprofileLookup[$mpegversion][$profileid]) ? $AACprofileLookup[$mpegversion][$profileid] : 'invalid');
505	}
506
507	/**
508	 * @param array $program_configs
509	 *
510	 * @return int
511	 */
512	public static function AACchannelCountCalculate($program_configs) {
513		$channels = 0;
514		for ($i = 0; $i < $program_configs['num_front_channel_elements']; $i++) {
515			$channels++;
516			if ($program_configs['front_element_is_cpe'][$i]) {
517				// each front element is channel pair (CPE = Channel Pair Element)
518				$channels++;
519			}
520		}
521		for ($i = 0; $i < $program_configs['num_side_channel_elements']; $i++) {
522			$channels++;
523			if ($program_configs['side_element_is_cpe'][$i]) {
524				// each side element is channel pair (CPE = Channel Pair Element)
525				$channels++;
526			}
527		}
528		for ($i = 0; $i < $program_configs['num_back_channel_elements']; $i++) {
529			$channels++;
530			if ($program_configs['back_element_is_cpe'][$i]) {
531				// each back element is channel pair (CPE = Channel Pair Element)
532				$channels++;
533			}
534		}
535		for ($i = 0; $i < $program_configs['num_lfe_channel_elements']; $i++) {
536			$channels++;
537		}
538		return $channels;
539	}
540
541}
542