123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- <?php
- class getid3_ac3
- {
- function getid3_ac3(&$fd, &$ThisFileInfo) {
-
- $ThisFileInfo['ac3']['raw']['bsi'] = array();
- $thisfile_ac3 = &$ThisFileInfo['ac3'];
- $thisfile_ac3_raw = &$thisfile_ac3['raw'];
- $thisfile_ac3_raw_bsi = &$thisfile_ac3_raw['bsi'];
-
- $ThisFileInfo['fileformat'] = 'ac3';
- $ThisFileInfo['audio']['dataformat'] = 'ac3';
- $ThisFileInfo['audio']['bitrate_mode'] = 'cbr';
- $ThisFileInfo['audio']['lossless'] = false;
-
-
-
-
-
-
-
-
-
-
- fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
- $AC3header['syncinfo'] = fread($fd, 5);
- $thisfile_ac3_raw['synchinfo']['synchword'] = substr($AC3header['syncinfo'], 0, 2);
- if ($thisfile_ac3_raw['synchinfo']['synchword'] != "\x0B\x77") {
- $ThisFileInfo['error'][] = 'Expecting "\x0B\x77" at offset '.$ThisFileInfo['avdataoffset'].', found \x'.strtoupper(dechex($AC3header['syncinfo']{0})).'\x'.strtoupper(dechex($AC3header['syncinfo']{1})).' instead';
- unset($thisfile_ac3);
- return false;
- } else {
-
-
-
-
-
-
- $thisfile_ac3_raw['synchinfo']['crc1'] = getid3_lib::LittleEndian2Int(substr($AC3header['syncinfo'], 2, 2));
- $ac3_synchinfo_fscod_frmsizecod = getid3_lib::LittleEndian2Int(substr($AC3header['syncinfo'], 4, 1));
- $thisfile_ac3_raw['synchinfo']['fscod'] = ($ac3_synchinfo_fscod_frmsizecod & 0xC0) >> 6;
- $thisfile_ac3_raw['synchinfo']['frmsizecod'] = ($ac3_synchinfo_fscod_frmsizecod & 0x3F);
- $thisfile_ac3['sample_rate'] = $this->AC3sampleRateCodeLookup($thisfile_ac3_raw['synchinfo']['fscod']);
- if ($thisfile_ac3_raw['synchinfo']['fscod'] <= 3) {
- $ThisFileInfo['audio']['sample_rate'] = $thisfile_ac3['sample_rate'];
- }
- $thisfile_ac3['frame_length'] = $this->AC3frameSizeLookup($thisfile_ac3_raw['synchinfo']['frmsizecod'], $thisfile_ac3_raw['synchinfo']['fscod']);
- $thisfile_ac3['bitrate'] = $this->AC3bitrateLookup($thisfile_ac3_raw['synchinfo']['frmsizecod']);
- $ThisFileInfo['audio']['bitrate'] = $thisfile_ac3['bitrate'];
- $AC3header['bsi'] = getid3_lib::BigEndian2Bin(fread($fd, 15));
- $ac3_bsi_offset = 0;
- $thisfile_ac3_raw_bsi['bsid'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
- $ac3_bsi_offset += 5;
- if ($thisfile_ac3_raw_bsi['bsid'] > 8) {
-
-
-
- $ThisFileInfo['error'][] = 'Bit stream identification is version '.$thisfile_ac3_raw_bsi['bsid'].', but getID3() only understands up to version 8';
- unset($thisfile_ac3);
- return false;
- }
- $thisfile_ac3_raw_bsi['bsmod'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 3));
- $ac3_bsi_offset += 3;
- $thisfile_ac3_raw_bsi['acmod'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 3));
- $ac3_bsi_offset += 3;
- $thisfile_ac3['service_type'] = $this->AC3serviceTypeLookup($thisfile_ac3_raw_bsi['bsmod'], $thisfile_ac3_raw_bsi['acmod']);
- $ac3_coding_mode = $this->AC3audioCodingModeLookup($thisfile_ac3_raw_bsi['acmod']);
- foreach($ac3_coding_mode as $key => $value) {
- $thisfile_ac3[$key] = $value;
- }
- switch ($thisfile_ac3_raw_bsi['acmod']) {
- case 0:
- case 1:
- $ThisFileInfo['audio']['channelmode'] = 'mono';
- break;
- case 3:
- case 4:
- $ThisFileInfo['audio']['channelmode'] = 'stereo';
- break;
- default:
- $ThisFileInfo['audio']['channelmode'] = 'surround';
- break;
- }
- $ThisFileInfo['audio']['channels'] = $thisfile_ac3['num_channels'];
- if ($thisfile_ac3_raw_bsi['acmod'] & 0x01) {
-
- $thisfile_ac3_raw_bsi['cmixlev'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
- $ac3_bsi_offset += 2;
- $thisfile_ac3['center_mix_level'] = $this->AC3centerMixLevelLookup($thisfile_ac3_raw_bsi['cmixlev']);
- }
- if ($thisfile_ac3_raw_bsi['acmod'] & 0x04) {
-
- $thisfile_ac3_raw_bsi['surmixlev'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
- $ac3_bsi_offset += 2;
- $thisfile_ac3['surround_mix_level'] = $this->AC3surroundMixLevelLookup($thisfile_ac3_raw_bsi['surmixlev']);
- }
- if ($thisfile_ac3_raw_bsi['acmod'] == 0x02) {
-
- $thisfile_ac3_raw_bsi['dsurmod'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
- $ac3_bsi_offset += 2;
- $thisfile_ac3['dolby_surround_mode'] = $this->AC3dolbySurroundModeLookup($thisfile_ac3_raw_bsi['dsurmod']);
- }
- $thisfile_ac3_raw_bsi['lfeon'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- $thisfile_ac3['lfe_enabled'] = $thisfile_ac3_raw_bsi['lfeon'];
- if ($thisfile_ac3_raw_bsi['lfeon']) {
-
- $ThisFileInfo['audio']['channels'] .= '.1';
- }
- $thisfile_ac3['channels_enabled'] = $this->AC3channelsEnabledLookup($thisfile_ac3_raw_bsi['acmod'], $thisfile_ac3_raw_bsi['lfeon']);
-
-
- $thisfile_ac3_raw_bsi['dialnorm'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
- $ac3_bsi_offset += 5;
- $thisfile_ac3['dialogue_normalization'] = '-'.$thisfile_ac3_raw_bsi['dialnorm'].'dB';
- $thisfile_ac3_raw_bsi['compre_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['compre_flag']) {
- $thisfile_ac3_raw_bsi['compr'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 8));
- $ac3_bsi_offset += 8;
- $thisfile_ac3['heavy_compression'] = $this->AC3heavyCompression($thisfile_ac3_raw_bsi['compr']);
- }
- $thisfile_ac3_raw_bsi['langcode_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['langcode_flag']) {
- $thisfile_ac3_raw_bsi['langcod'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 8));
- $ac3_bsi_offset += 8;
- }
- $thisfile_ac3_raw_bsi['audprodie'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['audprodie']) {
- $thisfile_ac3_raw_bsi['mixlevel'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
- $ac3_bsi_offset += 5;
- $thisfile_ac3_raw_bsi['roomtyp'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
- $ac3_bsi_offset += 2;
- $thisfile_ac3['mixing_level'] = (80 + $thisfile_ac3_raw_bsi['mixlevel']).'dB';
- $thisfile_ac3['room_type'] = $this->AC3roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp']);
- }
- if ($thisfile_ac3_raw_bsi['acmod'] == 0x00) {
-
-
-
-
-
- $thisfile_ac3_raw_bsi['dialnorm2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
- $ac3_bsi_offset += 5;
- $thisfile_ac3['dialogue_normalization2'] = '-'.$thisfile_ac3_raw_bsi['dialnorm2'].'dB';
- $thisfile_ac3_raw_bsi['compre_flag2'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['compre_flag2']) {
- $thisfile_ac3_raw_bsi['compr2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 8));
- $ac3_bsi_offset += 8;
- $thisfile_ac3['heavy_compression2'] = $this->AC3heavyCompression($thisfile_ac3_raw_bsi['compr2']);
- }
- $thisfile_ac3_raw_bsi['langcode_flag2'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['langcode_flag2']) {
- $thisfile_ac3_raw_bsi['langcod2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 8));
- $ac3_bsi_offset += 8;
- }
- $thisfile_ac3_raw_bsi['audprodie2'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['audprodie2']) {
- $thisfile_ac3_raw_bsi['mixlevel2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 5));
- $ac3_bsi_offset += 5;
- $thisfile_ac3_raw_bsi['roomtyp2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 2));
- $ac3_bsi_offset += 2;
- $thisfile_ac3['mixing_level2'] = (80 + $thisfile_ac3_raw_bsi['mixlevel2']).'dB';
- $thisfile_ac3['room_type2'] = $this->AC3roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp2']);
- }
- }
- $thisfile_ac3_raw_bsi['copyright'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- $thisfile_ac3_raw_bsi['original'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- $thisfile_ac3_raw_bsi['timecode1_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['timecode1_flag']) {
- $thisfile_ac3_raw_bsi['timecode1'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 14));
- $ac3_bsi_offset += 14;
- }
- $thisfile_ac3_raw_bsi['timecode2_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['timecode2_flag']) {
- $thisfile_ac3_raw_bsi['timecode2'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 14));
- $ac3_bsi_offset += 14;
- }
- $thisfile_ac3_raw_bsi['addbsi_flag'] = (bool) bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 1));
- $ac3_bsi_offset += 1;
- if ($thisfile_ac3_raw_bsi['addbsi_flag']) {
- $thisfile_ac3_raw_bsi['addbsi_length'] = bindec(substr($AC3header['bsi'], $ac3_bsi_offset, 6));
- $ac3_bsi_offset += 6;
- $AC3header['bsi'] .= getid3_lib::BigEndian2Bin(fread($fd, $thisfile_ac3_raw_bsi['addbsi_length']));
- $thisfile_ac3_raw_bsi['addbsi_data'] = substr($AC3header['bsi'], $ac3_bsi_offset, $thisfile_ac3_raw_bsi['addbsi_length'] * 8);
- $ac3_bsi_offset += $thisfile_ac3_raw_bsi['addbsi_length'] * 8;
- }
- }
- return true;
- }
- function AC3sampleRateCodeLookup($fscod) {
- static $AC3sampleRateCodeLookup = array(
- 0 => 48000,
- 1 => 44100,
- 2 => 32000,
- 3 => 'reserved'
- );
- return (isset($AC3sampleRateCodeLookup[$fscod]) ? $AC3sampleRateCodeLookup[$fscod] : false);
- }
- function AC3serviceTypeLookup($bsmod, $acmod) {
- static $AC3serviceTypeLookup = array();
- if (empty($AC3serviceTypeLookup)) {
- for ($i = 0; $i <= 7; $i++) {
- $AC3serviceTypeLookup[0][$i] = 'main audio service: complete main (CM)';
- $AC3serviceTypeLookup[1][$i] = 'main audio service: music and effects (ME)';
- $AC3serviceTypeLookup[2][$i] = 'associated service: visually impaired (VI)';
- $AC3serviceTypeLookup[3][$i] = 'associated service: hearing impaired (HI)';
- $AC3serviceTypeLookup[4][$i] = 'associated service: dialogue (D)';
- $AC3serviceTypeLookup[5][$i] = 'associated service: commentary (C)';
- $AC3serviceTypeLookup[6][$i] = 'associated service: emergency (E)';
- }
- $AC3serviceTypeLookup[7][1] = 'associated service: voice over (VO)';
- for ($i = 2; $i <= 7; $i++) {
- $AC3serviceTypeLookup[7][$i] = 'main audio service: karaoke';
- }
- }
- return (isset($AC3serviceTypeLookup[$bsmod][$acmod]) ? $AC3serviceTypeLookup[$bsmod][$acmod] : false);
- }
- function AC3audioCodingModeLookup($acmod) {
- static $AC3audioCodingModeLookup = array();
- if (empty($AC3audioCodingModeLookup)) {
-
- $AC3audioCodingModeLookup = array (
- 0 => array('channel_config'=>'1+1', 'num_channels'=>2, 'channel_order'=>'Ch1,Ch2'),
- 1 => array('channel_config'=>'1/0', 'num_channels'=>1, 'channel_order'=>'C'),
- 2 => array('channel_config'=>'2/0', 'num_channels'=>2, 'channel_order'=>'L,R'),
- 3 => array('channel_config'=>'3/0', 'num_channels'=>3, 'channel_order'=>'L,C,R'),
- 4 => array('channel_config'=>'2/1', 'num_channels'=>3, 'channel_order'=>'L,R,S'),
- 5 => array('channel_config'=>'3/1', 'num_channels'=>4, 'channel_order'=>'L,C,R,S'),
- 6 => array('channel_config'=>'2/2', 'num_channels'=>4, 'channel_order'=>'L,R,SL,SR'),
- 7 => array('channel_config'=>'3/2', 'num_channels'=>5, 'channel_order'=>'L,C,R,SL,SR')
- );
- }
- return (isset($AC3audioCodingModeLookup[$acmod]) ? $AC3audioCodingModeLookup[$acmod] : false);
- }
- function AC3centerMixLevelLookup($cmixlev) {
- static $AC3centerMixLevelLookup;
- if (empty($AC3centerMixLevelLookup)) {
- $AC3centerMixLevelLookup = array(
- 0 => pow(2, -3.0 / 6),
- 1 => pow(2, -4.5 / 6),
- 2 => pow(2, -6.0 / 6),
- 3 => 'reserved'
- );
- }
- return (isset($AC3centerMixLevelLookup[$cmixlev]) ? $AC3centerMixLevelLookup[$cmixlev] : false);
- }
- function AC3surroundMixLevelLookup($surmixlev) {
- static $AC3surroundMixLevelLookup;
- if (empty($AC3surroundMixLevelLookup)) {
- $AC3surroundMixLevelLookup = array(
- 0 => pow(2, -3.0 / 6),
- 1 => pow(2, -6.0 / 6),
- 2 => 0,
- 3 => 'reserved'
- );
- }
- return (isset($AC3surroundMixLevelLookup[$surmixlev]) ? $AC3surroundMixLevelLookup[$surmixlev] : false);
- }
- function AC3dolbySurroundModeLookup($dsurmod) {
- static $AC3dolbySurroundModeLookup = array(
- 0 => 'not indicated',
- 1 => 'Not Dolby Surround encoded',
- 2 => 'Dolby Surround encoded',
- 3 => 'reserved'
- );
- return (isset($AC3dolbySurroundModeLookup[$dsurmod]) ? $AC3dolbySurroundModeLookup[$dsurmod] : false);
- }
- function AC3channelsEnabledLookup($acmod, $lfeon) {
- $AC3channelsEnabledLookup = array(
- 'ch1'=>(bool) ($acmod == 0),
- 'ch2'=>(bool) ($acmod == 0),
- 'left'=>(bool) ($acmod > 1),
- 'right'=>(bool) ($acmod > 1),
- 'center'=>(bool) ($acmod & 0x01),
- 'surround_mono'=>false,
- 'surround_left'=>false,
- 'surround_right'=>false,
- 'lfe'=>$lfeon);
- switch ($acmod) {
- case 4:
- case 5:
- $AC3channelsEnabledLookup['surround_mono'] = true;
- break;
- case 6:
- case 7:
- $AC3channelsEnabledLookup['surround_left'] = true;
- $AC3channelsEnabledLookup['surround_right'] = true;
- break;
- }
- return $AC3channelsEnabledLookup;
- }
- function AC3heavyCompression($compre) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $fourbit = str_pad(decbin(($compre & 0xF0) >> 4), 4, '0', STR_PAD_LEFT);
- if ($fourbit{0} == '1') {
- $log_gain = -8 + bindec(substr($fourbit, 1));
- } else {
- $log_gain = bindec(substr($fourbit, 1));
- }
- $log_gain = ($log_gain + 1) * getid3_lib::RGADamplitude2dB(2);
-
-
-
-
- $lin_gain = (16 + ($compre & 0x0F)) / 32;
-
-
-
- return $log_gain - $lin_gain;
- }
- function AC3roomTypeLookup($roomtyp) {
- static $AC3roomTypeLookup = array(
- 0 => 'not indicated',
- 1 => 'large room, X curve monitor',
- 2 => 'small room, flat monitor',
- 3 => 'reserved'
- );
- return (isset($AC3roomTypeLookup[$roomtyp]) ? $AC3roomTypeLookup[$roomtyp] : false);
- }
- function AC3frameSizeLookup($frmsizecod, $fscod) {
- $padding = (bool) ($frmsizecod % 2);
- $framesizeid = floor($frmsizecod / 2);
- static $AC3frameSizeLookup = array();
- if (empty($AC3frameSizeLookup)) {
- $AC3frameSizeLookup = array (
- 0 => array(128, 138, 192),
- 1 => array(40, 160, 174, 240),
- 2 => array(48, 192, 208, 288),
- 3 => array(56, 224, 242, 336),
- 4 => array(64, 256, 278, 384),
- 5 => array(80, 320, 348, 480),
- 6 => array(96, 384, 416, 576),
- 7 => array(112, 448, 486, 672),
- 8 => array(128, 512, 556, 768),
- 9 => array(160, 640, 696, 960),
- 10 => array(192, 768, 834, 1152),
- 11 => array(224, 896, 974, 1344),
- 12 => array(256, 1024, 1114, 1536),
- 13 => array(320, 1280, 1392, 1920),
- 14 => array(384, 1536, 1670, 2304),
- 15 => array(448, 1792, 1950, 2688),
- 16 => array(512, 2048, 2228, 3072),
- 17 => array(576, 2304, 2506, 3456),
- 18 => array(640, 2560, 2786, 3840)
- );
- }
- if (($fscod == 1) && $padding) {
-
- $AC3frameSizeLookup[$frmsizecod] += 2;
- }
- return (isset($AC3frameSizeLookup[$framesizeid][$fscod]) ? $AC3frameSizeLookup[$framesizeid][$fscod] : false);
- }
- function AC3bitrateLookup($frmsizecod) {
- $framesizeid = floor($frmsizecod / 2);
- static $AC3bitrateLookup = array(
- 0 => 32000,
- 1 => 40000,
- 2 => 48000,
- 3 => 56000,
- 4 => 64000,
- 5 => 80000,
- 6 => 96000,
- 7 => 112000,
- 8 => 128000,
- 9 => 160000,
- 10 => 192000,
- 11 => 224000,
- 12 => 256000,
- 13 => 320000,
- 14 => 384000,
- 15 => 448000,
- 16 => 512000,
- 17 => 576000,
- 18 => 640000
- );
- return (isset($AC3bitrateLookup[$framesizeid]) ? $AC3bitrateLookup[$framesizeid] : false);
- }
- }
- ?>
|