<Website>

getID3

PHK Home

File: /lib/module.audio.ac3.php

Size:13085
Storage flags:strip

<?php
















class getid3_ac3 extends getid3_handler
{
private 
$AC3header = array();
private 
$BSIoffset 0;

const 
syncword "\x0B\x77";

public function 
Analyze() {
$info = &$this->getid3->info;


 
$info['ac3']['raw']['bsi'] = array();
$thisfile_ac3 = &$info['ac3'];
$thisfile_ac3_raw = &$thisfile_ac3['raw'];
$thisfile_ac3_raw_bsi = &$thisfile_ac3_raw['bsi'];




$info['fileformat'] = 'ac3';


 
 
 
 
 
 
 
 
 


 
 
 
 
 

$this->fseek($info['avdataoffset']);
$this->AC3header['syncinfo'] = $this->fread(5);

if (
strpos($this->AC3header['syncinfo'], self::syncword) === 0) {
$thisfile_ac3_raw['synchinfo']['synchword'] = self::syncword;
$offset 2;
} else {
if (!
$this->isDependencyFor('matroska')) {
unset(
$info['fileformat'], $info['ac3']);
return 
$this->error('Expecting "'.getid3_lib::PrintHexBytes(self::syncword).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($this->AC3header['syncinfo'], 02)).'"');
}
$offset 0;
$this->fseek(-2SEEK_CUR);
}

$info['audio']['dataformat'] = 'ac3';
$info['audio']['bitrate_mode'] = 'cbr';
$info['audio']['lossless'] = false;

$thisfile_ac3_raw['synchinfo']['crc1'] = getid3_lib::LittleEndian2Int(substr($this->AC3header['syncinfo'], $offset2));
$ac3_synchinfo_fscod_frmsizecod getid3_lib::LittleEndian2Int(substr($this->AC3header['syncinfo'], ($offset 2), 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'] = self::sampleRateCodeLookup($thisfile_ac3_raw['synchinfo']['fscod']);
if (
$thisfile_ac3_raw['synchinfo']['fscod'] <= 3) {
$info['audio']['sample_rate'] = $thisfile_ac3['sample_rate'];
}

$thisfile_ac3['frame_length'] = self::frameSizeLookup($thisfile_ac3_raw['synchinfo']['frmsizecod'], $thisfile_ac3_raw['synchinfo']['fscod']);
$thisfile_ac3['bitrate'] = self::bitrateLookup($thisfile_ac3_raw['synchinfo']['frmsizecod']);
$info['audio']['bitrate'] = $thisfile_ac3['bitrate'];

$this->AC3header['bsi'] = getid3_lib::BigEndian2Bin($this->fread(15));
$ac3_bsi_offset 0;

$thisfile_ac3_raw_bsi['bsid'] = $this->readHeaderBSI(5);
if (
$thisfile_ac3_raw_bsi['bsid'] > 8) {

 
 
 
$this->error('Bit stream identification is version '.$thisfile_ac3_raw_bsi['bsid'].', but getID3() only understands up to version 8');
unset(
$info['ac3']);
return 
false;
}

$thisfile_ac3_raw_bsi['bsmod'] = $this->readHeaderBSI(3);
$thisfile_ac3_raw_bsi['acmod'] = $this->readHeaderBSI(3);

$thisfile_ac3['service_type'] = self::serviceTypeLookup($thisfile_ac3_raw_bsi['bsmod'], $thisfile_ac3_raw_bsi['acmod']);
$ac3_coding_mode self::audioCodingModeLookup($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:
$info['audio']['channelmode'] = 'mono';
break;
case 
3:
case 
4:
$info['audio']['channelmode'] = 'stereo';
break;
default:
$info['audio']['channelmode'] = 'surround';
break;
}
$info['audio']['channels'] = $thisfile_ac3['num_channels'];

if (
$thisfile_ac3_raw_bsi['acmod'] & 0x01) {

 
$thisfile_ac3_raw_bsi['cmixlev'] = $this->readHeaderBSI(2);
$thisfile_ac3['center_mix_level'] = self::centerMixLevelLookup($thisfile_ac3_raw_bsi['cmixlev']);
}

if (
$thisfile_ac3_raw_bsi['acmod'] & 0x04) {

 
$thisfile_ac3_raw_bsi['surmixlev'] = $this->readHeaderBSI(2);
$thisfile_ac3['surround_mix_level'] = self::surroundMixLevelLookup($thisfile_ac3_raw_bsi['surmixlev']);
}

if (
$thisfile_ac3_raw_bsi['acmod'] == 0x02) {

 
$thisfile_ac3_raw_bsi['dsurmod'] = $this->readHeaderBSI(2);
$thisfile_ac3['dolby_surround_mode'] = self::dolbySurroundModeLookup($thisfile_ac3_raw_bsi['dsurmod']);
}

$thisfile_ac3_raw_bsi['lfeon'] = (bool) $this->readHeaderBSI(1);
$thisfile_ac3['lfe_enabled'] = $thisfile_ac3_raw_bsi['lfeon'];
if (
$thisfile_ac3_raw_bsi['lfeon']) {

 
$info['audio']['channels'] .= '.1';
}

$thisfile_ac3['channels_enabled'] = self::channelsEnabledLookup($thisfile_ac3_raw_bsi['acmod'], $thisfile_ac3_raw_bsi['lfeon']);


 
 
$thisfile_ac3_raw_bsi['dialnorm'] = $this->readHeaderBSI(5);
$thisfile_ac3['dialogue_normalization'] = '-'.$thisfile_ac3_raw_bsi['dialnorm'].'dB';

$thisfile_ac3_raw_bsi['compre_flag'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['compre_flag']) {
$thisfile_ac3_raw_bsi['compr'] = $this->readHeaderBSI(8);
$thisfile_ac3['heavy_compression'] = self::heavyCompression($thisfile_ac3_raw_bsi['compr']);
}

$thisfile_ac3_raw_bsi['langcode_flag'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['langcode_flag']) {
$thisfile_ac3_raw_bsi['langcod'] = $this->readHeaderBSI(8);
}

$thisfile_ac3_raw_bsi['audprodie'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['audprodie']) {
$thisfile_ac3_raw_bsi['mixlevel'] = $this->readHeaderBSI(5);
$thisfile_ac3_raw_bsi['roomtyp'] = $this->readHeaderBSI(2);

$thisfile_ac3['mixing_level'] = (80 $thisfile_ac3_raw_bsi['mixlevel']).'dB';
$thisfile_ac3['room_type'] = self::roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp']);
}

if (
$thisfile_ac3_raw_bsi['acmod'] == 0x00) {

 
 


 
 
$thisfile_ac3_raw_bsi['dialnorm2'] = $this->readHeaderBSI(5);
$thisfile_ac3['dialogue_normalization2'] = '-'.$thisfile_ac3_raw_bsi['dialnorm2'].'dB';

$thisfile_ac3_raw_bsi['compre_flag2'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['compre_flag2']) {
$thisfile_ac3_raw_bsi['compr2'] = $this->readHeaderBSI(8);
$thisfile_ac3['heavy_compression2'] = self::heavyCompression($thisfile_ac3_raw_bsi['compr2']);
}

$thisfile_ac3_raw_bsi['langcode_flag2'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['langcode_flag2']) {
$thisfile_ac3_raw_bsi['langcod2'] = $this->readHeaderBSI(8);
}

$thisfile_ac3_raw_bsi['audprodie2'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['audprodie2']) {
$thisfile_ac3_raw_bsi['mixlevel2'] = $this->readHeaderBSI(5);
$thisfile_ac3_raw_bsi['roomtyp2'] = $this->readHeaderBSI(2);

$thisfile_ac3['mixing_level2'] = (80 $thisfile_ac3_raw_bsi['mixlevel2']).'dB';
$thisfile_ac3['room_type2'] = self::roomTypeLookup($thisfile_ac3_raw_bsi['roomtyp2']);
}

}

$thisfile_ac3_raw_bsi['copyright'] = (bool) $this->readHeaderBSI(1);

$thisfile_ac3_raw_bsi['original'] = (bool) $this->readHeaderBSI(1);

$thisfile_ac3_raw_bsi['timecode1_flag'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['timecode1_flag']) {
$thisfile_ac3_raw_bsi['timecode1'] = $this->readHeaderBSI(14);
}

$thisfile_ac3_raw_bsi['timecode2_flag'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['timecode2_flag']) {
$thisfile_ac3_raw_bsi['timecode2'] = $this->readHeaderBSI(14);
}

$thisfile_ac3_raw_bsi['addbsi_flag'] = (bool) $this->readHeaderBSI(1);
if (
$thisfile_ac3_raw_bsi['addbsi_flag']) {
$thisfile_ac3_raw_bsi['addbsi_length'] = $this->readHeaderBSI(6);

$this->AC3header['bsi'] .= getid3_lib::BigEndian2Bin($this->fread($thisfile_ac3_raw_bsi['addbsi_length']));

$thisfile_ac3_raw_bsi['addbsi_data'] = substr($this->AC3header['bsi'], $this->BSIoffset$thisfile_ac3_raw_bsi['addbsi_length'] * 8);
$this->BSIoffset += $thisfile_ac3_raw_bsi['addbsi_length'] * 8;
}

return 
true;
}

private function 
readHeaderBSI($length) {
$data substr($this->AC3header['bsi'], $this->BSIoffset$length);
$this->BSIoffset += $length;

return 
bindec($data);
}

public static function 
sampleRateCodeLookup($fscod) {
static 
$sampleRateCodeLookup = array(
=> 48000,
=> 44100,
=> 32000,
=> 'reserved' 
 
);
return (isset(
$sampleRateCodeLookup[$fscod]) ? $sampleRateCodeLookup[$fscod] : false);
}

public static function 
serviceTypeLookup($bsmod$acmod) {
static 
$serviceTypeLookup = array();
if (empty(
$serviceTypeLookup)) {
for (
$i 0$i <= 7$i++) {
$serviceTypeLookup[0][$i] = 'main audio service: complete main (CM)';
$serviceTypeLookup[1][$i] = 'main audio service: music and effects (ME)';
$serviceTypeLookup[2][$i] = 'associated service: visually impaired (VI)';
$serviceTypeLookup[3][$i] = 'associated service: hearing impaired (HI)';
$serviceTypeLookup[4][$i] = 'associated service: dialogue (D)';
$serviceTypeLookup[5][$i] = 'associated service: commentary (C)';
$serviceTypeLookup[6][$i] = 'associated service: emergency (E)';
}

$serviceTypeLookup[7][1] = 'associated service: voice over (VO)';
for (
$i 2$i <= 7$i++) {
$serviceTypeLookup[7][$i] = 'main audio service: karaoke';
}
}
return (isset(
$serviceTypeLookup[$bsmod][$acmod]) ? $serviceTypeLookup[$bsmod][$acmod] : false);
}

public static function 
audioCodingModeLookup($acmod) {

 static 
$audioCodingModeLookup = array (
=> array('channel_config'=>'1+1''num_channels'=>2'channel_order'=>'Ch1,Ch2'),
=> array('channel_config'=>'1/0''num_channels'=>1'channel_order'=>'C'),
=> array('channel_config'=>'2/0''num_channels'=>2'channel_order'=>'L,R'),
=> array('channel_config'=>'3/0''num_channels'=>3'channel_order'=>'L,C,R'),
=> array('channel_config'=>'2/1''num_channels'=>3'channel_order'=>'L,R,S'),
=> array('channel_config'=>'3/1''num_channels'=>4'channel_order'=>'L,C,R,S'),
=> array('channel_config'=>'2/2''num_channels'=>4'channel_order'=>'L,R,SL,SR'),
=> array('channel_config'=>'3/2''num_channels'=>5'channel_order'=>'L,C,R,SL,SR'),
);
return (isset(
$audioCodingModeLookup[$acmod]) ? $audioCodingModeLookup[$acmod] : false);
}

public static function 
centerMixLevelLookup($cmixlev) {
static 
$centerMixLevelLookup;
if (empty(
$centerMixLevelLookup)) {
$centerMixLevelLookup = array(
=> pow(2, -3.0 6), 
 
=> pow(2, -4.5 6), 
 
=> pow(2, -6.0 6), 
 
=> 'reserved'
);
}
return (isset(
$centerMixLevelLookup[$cmixlev]) ? $centerMixLevelLookup[$cmixlev] : false);
}

public static function 
surroundMixLevelLookup($surmixlev) {
static 
$surroundMixLevelLookup;
if (empty(
$surroundMixLevelLookup)) {
$surroundMixLevelLookup = array(
=> pow(2, -3.0 6),
=> pow(2, -6.0 6),
=> 0,
=> 'reserved'
);
}
return (isset(
$surroundMixLevelLookup[$surmixlev]) ? $surroundMixLevelLookup[$surmixlev] : false);
}

public static function 
dolbySurroundModeLookup($dsurmod) {
static 
$dolbySurroundModeLookup = array(
=> 'not indicated',
=> 'Not Dolby Surround encoded',
=> 'Dolby Surround encoded',
=> 'reserved'
);
return (isset(
$dolbySurroundModeLookup[$dsurmod]) ? $dolbySurroundModeLookup[$dsurmod] : false);
}

public static function 
channelsEnabledLookup($acmod$lfeon) {
$lookup = 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:
$lookup['surround_mono'] = true;
break;
case 
6:
case 
7:
$lookup['surround_left'] = true;
$lookup['surround_right'] = true;
break;
}
return 
$lookup;
}

public static function 
heavyCompression($compre) {

 
 
 
 
 
 
 


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

$fourbit str_pad(decbin(($compre 0xF0) >> 4), 4'0'STR_PAD_LEFT);
if (
$fourbit{0} == '1') {
$log_gain = -bindec(substr($fourbit1));
} else {
$log_gain bindec(substr($fourbit1));
}
$log_gain = ($log_gain 1) * getid3_lib::RGADamplitude2dB(2);


 
 
 

$lin_gain = (16 + ($compre 0x0F)) / 32;


 
 

return 
$log_gain $lin_gain;
}

public static function 
roomTypeLookup($roomtyp) {
static 
$roomTypeLookup = array(
=> 'not indicated',
=> 'large room, X curve monitor',
=> 'small room, flat monitor',
=> 'reserved'
);
return (isset(
$roomTypeLookup[$roomtyp]) ? $roomTypeLookup[$roomtyp] : false);
}

public static function 
frameSizeLookup($frmsizecod$fscod) {
$padding = (bool) ($frmsizecod 2);
$framesizeid floor($frmsizecod 2);

static 
$frameSizeLookup = array();
if (empty(
$frameSizeLookup)) {
$frameSizeLookup = array (
=> array(128138192),
=> array(40160174240),
=> array(48192208288),
=> array(56224242336),
=> array(64256278384),
=> array(80320348480),
=> array(96384416576),
=> array(112448486672),
=> array(128512556768),
=> array(160640696960),
10 => array(1927688341152),
11 => array(2248969741344),
12 => array(256102411141536),
13 => array(320128013921920),
14 => array(384153616702304),
15 => array(448179219502688),
16 => array(512204822283072),
17 => array(576230425063456),
18 => array(640256027863840)
);
}
if ((
$fscod == 1) && $padding) {

 
$frameSizeLookup[$frmsizecod] += 2;
}
return (isset(
$frameSizeLookup[$framesizeid][$fscod]) ? $frameSizeLookup[$framesizeid][$fscod] : false);
}

public static function 
bitrateLookup($frmsizecod) {
$framesizeid floor($frmsizecod 2);

static 
$bitrateLookup = array(
=> 32000,
=> 40000,
=> 48000,
=> 56000,
=> 64000,
=> 80000,
=> 96000,
=> 112000,
=> 128000,
=> 160000,
10 => 192000,
11 => 224000,
12 => 256000,
13 => 320000,
14 => 384000,
15 => 448000,
16 => 512000,
17 => 576000,
18 => 640000
);
return (isset(
$bitrateLookup[$framesizeid]) ? $bitrateLookup[$framesizeid] : false);
}


}

For more information about the PHK package format: http://phk.tekwire.net