annotate e2gallerypro/e2upload/Backend/Assets/getid3/module.audio.ac3.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
rev   line source
rlm@3 1 <?php
rlm@3 2 // +----------------------------------------------------------------------+
rlm@3 3 // | PHP version 5 |
rlm@3 4 // +----------------------------------------------------------------------+
rlm@3 5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
rlm@3 6 // +----------------------------------------------------------------------+
rlm@3 7 // | This source file is subject to version 2 of the GPL license, |
rlm@3 8 // | that is bundled with this package in the file license.txt and is |
rlm@3 9 // | available through the world-wide-web at the following url: |
rlm@3 10 // | http://www.gnu.org/copyleft/gpl.html |
rlm@3 11 // +----------------------------------------------------------------------+
rlm@3 12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
rlm@3 13 // +----------------------------------------------------------------------+
rlm@3 14 // | Authors: James Heinrich <infoŘgetid3*org> |
rlm@3 15 // | Allan Hansen <ahŘartemis*dk> |
rlm@3 16 // +----------------------------------------------------------------------+
rlm@3 17 // | module.audio.ac3.php |
rlm@3 18 // | Module for analyzing AC-3 (aka Dolby Digital) audio files |
rlm@3 19 // | dependencies: NONE |
rlm@3 20 // +----------------------------------------------------------------------+
rlm@3 21 //
rlm@3 22 // $Id: module.audio.ac3.php,v 1.3 2006/11/02 10:48:01 ah Exp $
rlm@3 23
rlm@3 24
rlm@3 25
rlm@3 26 class getid3_ac3 extends getid3_handler
rlm@3 27 {
rlm@3 28
rlm@3 29 public function Analyze() {
rlm@3 30
rlm@3 31 $getid3 = $this->getid3;
rlm@3 32
rlm@3 33 // http://www.atsc.org/standards/a_52a.pdf
rlm@3 34
rlm@3 35 $getid3->info['fileformat'] = 'ac3';
rlm@3 36 $getid3->info['audio']['dataformat'] = 'ac3';
rlm@3 37 $getid3->info['audio']['bitrate_mode'] = 'cbr';
rlm@3 38 $getid3->info['audio']['lossless'] = false;
rlm@3 39
rlm@3 40 $getid3->info['ac3']['raw']['bsi'] = array ();
rlm@3 41 $info_ac3 = &$getid3->info['ac3'];
rlm@3 42 $info_ac3_raw = &$info_ac3['raw'];
rlm@3 43 $info_ac3_raw_bsi = &$info_ac3_raw['bsi'];
rlm@3 44
rlm@3 45
rlm@3 46 // An AC-3 serial coded audio bit stream is made up of a sequence of synchronization frames
rlm@3 47 // Each synchronization frame contains 6 coded audio blocks (AB), each of which represent 256
rlm@3 48 // new audio samples per channel. A synchronization information (SI) header at the beginning
rlm@3 49 // of each frame contains information needed to acquire and maintain synchronization. A
rlm@3 50 // bit stream information (BSI) header follows SI, and contains parameters describing the coded
rlm@3 51 // audio service. The coded audio blocks may be followed by an auxiliary data (Aux) field. At the
rlm@3 52 // end of each frame is an error check field that includes a CRC word for error detection. An
rlm@3 53 // additional CRC word is located in the SI header, the use of which, by a decoder, is optional.
rlm@3 54 //
rlm@3 55 // syncinfo() | bsi() | AB0 | AB1 | AB2 | AB3 | AB4 | AB5 | Aux | CRC
rlm@3 56
rlm@3 57 $this->fseek($getid3->info['avdataoffset'], SEEK_SET);
rlm@3 58 $ac3_header['syncinfo'] = $this->fread(5);
rlm@3 59 $info_ac3_raw['synchinfo']['synchword'] = substr($ac3_header['syncinfo'], 0, 2);
rlm@3 60
rlm@3 61 if ($info_ac3_raw['synchinfo']['synchword'] != "\x0B\x77") {
rlm@3 62 throw new getid3_exception('Expecting "\x0B\x77" at offset '.$getid3->info['avdataoffset'].', found \x'.strtoupper(dechex($ac3_header['syncinfo']{0})).'\x'.strtoupper(dechex($ac3_header['syncinfo']{1})).' instead');
rlm@3 63 }
rlm@3 64
rlm@3 65
rlm@3 66 // syncinfo() {
rlm@3 67 // syncword 16
rlm@3 68 // crc1 16
rlm@3 69 // fscod 2
rlm@3 70 // frmsizecod 6
rlm@3 71 // } /* end of syncinfo */
rlm@3 72
rlm@3 73 $info_ac3_raw['synchinfo']['crc1'] = getid3_lib::LittleEndian2Int(substr($ac3_header['syncinfo'], 2, 2));
rlm@3 74 $ac3_synchinfo_fscod_frmsizecod = getid3_lib::LittleEndian2Int(substr($ac3_header['syncinfo'], 4, 1));
rlm@3 75 $info_ac3_raw['synchinfo']['fscod'] = ($ac3_synchinfo_fscod_frmsizecod & 0xC0) >> 6;
rlm@3 76 $info_ac3_raw['synchinfo']['frmsizecod'] = ($ac3_synchinfo_fscod_frmsizecod & 0x3F);
rlm@3 77
rlm@3 78 $info_ac3['sample_rate'] = getid3_ac3::AC3sampleRateCodeLookup($info_ac3_raw['synchinfo']['fscod']);
rlm@3 79 if ($info_ac3_raw['synchinfo']['fscod'] <= 3) {
rlm@3 80 $getid3->info['audio']['sample_rate'] = $info_ac3['sample_rate'];
rlm@3 81 }
rlm@3 82
rlm@3 83 $info_ac3['frame_length'] = getid3_ac3::AC3frameSizeLookup($info_ac3_raw['synchinfo']['frmsizecod'], $info_ac3_raw['synchinfo']['fscod']);
rlm@3 84 $info_ac3['bitrate'] = getid3_ac3::AC3bitrateLookup($info_ac3_raw['synchinfo']['frmsizecod']);
rlm@3 85 $getid3->info['audio']['bitrate'] = $info_ac3['bitrate'];
rlm@3 86
rlm@3 87 $ac3_header['bsi'] = getid3_lib::BigEndian2Bin($this->fread(15));
rlm@3 88
rlm@3 89 $info_ac3_raw_bsi['bsid'] = bindec(substr($ac3_header['bsi'], 0, 5));
rlm@3 90 if ($info_ac3_raw_bsi['bsid'] > 8) {
rlm@3 91 // Decoders which can decode version 8 will thus be able to decode version numbers less than 8.
rlm@3 92 // If this standard is extended by the addition of additional elements or features, a value of bsid greater than 8 will be used.
rlm@3 93 // Decoders built to this version of the standard will not be able to decode versions with bsid greater than 8.
rlm@3 94 throw new getid3_exception('Bit stream identification is version '.$info_ac3_raw_bsi['bsid'].', but getID3() only understands up to version 8');
rlm@3 95 }
rlm@3 96
rlm@3 97 $info_ac3_raw_bsi['bsmod'] = bindec(substr($ac3_header['bsi'], 5, 3));
rlm@3 98 $info_ac3_raw_bsi['acmod'] = bindec(substr($ac3_header['bsi'], 8, 3));
rlm@3 99
rlm@3 100 $info_ac3['service_type'] = getid3_ac3::AC3serviceTypeLookup($info_ac3_raw_bsi['bsmod'], $info_ac3_raw_bsi['acmod']);
rlm@3 101 $ac3_coding_mode = getid3_ac3::AC3audioCodingModeLookup($info_ac3_raw_bsi['acmod']);
rlm@3 102 foreach($ac3_coding_mode as $key => $value) {
rlm@3 103 $info_ac3[$key] = $value;
rlm@3 104 }
rlm@3 105 switch ($info_ac3_raw_bsi['acmod']) {
rlm@3 106 case 0:
rlm@3 107 case 1:
rlm@3 108 $getid3->info['audio']['channelmode'] = 'mono';
rlm@3 109 break;
rlm@3 110 case 3:
rlm@3 111 case 4:
rlm@3 112 $getid3->info['audio']['channelmode'] = 'stereo';
rlm@3 113 break;
rlm@3 114 default:
rlm@3 115 $getid3->info['audio']['channelmode'] = 'surround';
rlm@3 116 break;
rlm@3 117 }
rlm@3 118 $getid3->info['audio']['channels'] = $info_ac3['num_channels'];
rlm@3 119
rlm@3 120 $offset = 11;
rlm@3 121
rlm@3 122 if ($info_ac3_raw_bsi['acmod'] & 0x01) {
rlm@3 123 // If the lsb of acmod is a 1, center channel is in use and cmixlev follows in the bit stream.
rlm@3 124 $info_ac3_raw_bsi['cmixlev'] = bindec(substr($ac3_header['bsi'], $offset, 2));
rlm@3 125 $info_ac3['center_mix_level'] = getid3_ac3::AC3centerMixLevelLookup($info_ac3_raw_bsi['cmixlev']);
rlm@3 126 $offset += 2;
rlm@3 127 }
rlm@3 128
rlm@3 129 if ($info_ac3_raw_bsi['acmod'] & 0x04) {
rlm@3 130 // If the msb of acmod is a 1, surround channels are in use and surmixlev follows in the bit stream.
rlm@3 131 $info_ac3_raw_bsi['surmixlev'] = bindec(substr($ac3_header['bsi'], $offset, 2));
rlm@3 132 $info_ac3['surround_mix_level'] = getid3_ac3::AC3surroundMixLevelLookup($info_ac3_raw_bsi['surmixlev']);
rlm@3 133 $offset += 2;
rlm@3 134 }
rlm@3 135
rlm@3 136 if ($info_ac3_raw_bsi['acmod'] == 0x02) {
rlm@3 137 // When operating in the two channel mode, this 2-bit code indicates whether or not the program has been encoded in Dolby Surround.
rlm@3 138 $info_ac3_raw_bsi['dsurmod'] = bindec(substr($ac3_header['bsi'], $offset, 2));
rlm@3 139 $info_ac3['dolby_surround_mode'] = getid3_ac3::AC3dolbySurroundModeLookup($info_ac3_raw_bsi['dsurmod']);
rlm@3 140 $offset += 2;
rlm@3 141 }
rlm@3 142
rlm@3 143 $info_ac3_raw_bsi['lfeon'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 144 $info_ac3['lfe_enabled'] = $info_ac3_raw_bsi['lfeon'];
rlm@3 145 if ($info_ac3_raw_bsi['lfeon']) {
rlm@3 146 $getid3->info['audio']['channels'] .= '.1';
rlm@3 147 }
rlm@3 148
rlm@3 149 $info_ac3['channels_enabled'] = getid3_ac3::AC3channelsEnabledLookup($info_ac3_raw_bsi['acmod'], $info_ac3_raw_bsi['lfeon']);
rlm@3 150
rlm@3 151 // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1–31.
rlm@3 152 // 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.
rlm@3 153 $info_ac3_raw_bsi['dialnorm'] = bindec(substr($ac3_header['bsi'], $offset, 5));
rlm@3 154 $offset += 5;
rlm@3 155 $info_ac3['dialogue_normalization'] = '-'.$info_ac3_raw_bsi['dialnorm'].'dB';
rlm@3 156
rlm@3 157 $info_ac3_raw_bsi['compre_flag'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 158 if ($info_ac3_raw_bsi['compre_flag']) {
rlm@3 159 $info_ac3_raw_bsi['compr'] = bindec(substr($ac3_header['bsi'], $offset, 8));
rlm@3 160 $offset += 8;
rlm@3 161
rlm@3 162 $info_ac3['heavy_compression'] = getid3_ac3::AC3heavyCompression($info_ac3_raw_bsi['compr']);
rlm@3 163 }
rlm@3 164
rlm@3 165 $info_ac3_raw_bsi['langcode_flag'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 166 if ($info_ac3_raw_bsi['langcode_flag']) {
rlm@3 167 $info_ac3_raw_bsi['langcod'] = bindec(substr($ac3_header['bsi'], $offset, 8));
rlm@3 168 $offset += 8;
rlm@3 169 }
rlm@3 170
rlm@3 171 $info_ac3_raw_bsi['audprodie'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 172 if ($info_ac3_raw_bsi['audprodie']) {
rlm@3 173 $info_ac3_raw_bsi['mixlevel'] = bindec(substr($ac3_header['bsi'], $offset, 5));
rlm@3 174 $offset += 5;
rlm@3 175
rlm@3 176 $info_ac3_raw_bsi['roomtyp'] = bindec(substr($ac3_header['bsi'], $offset, 2));
rlm@3 177 $offset += 2;
rlm@3 178
rlm@3 179 $info_ac3['mixing_level'] = (80 + $info_ac3_raw_bsi['mixlevel']).'dB';
rlm@3 180 $info_ac3['room_type'] = getid3_ac3::AC3roomTypeLookup($info_ac3_raw_bsi['roomtyp']);
rlm@3 181 }
rlm@3 182
rlm@3 183 if ($info_ac3_raw_bsi['acmod'] == 0x00) {
rlm@3 184 // If acmod is 0, then two completely independent program channels (dual mono)
rlm@3 185 // are encoded into the bit stream, and are referenced as Ch1, Ch2. In this case,
rlm@3 186 // a number of additional items are present in BSI or audblk to fully describe Ch2.
rlm@3 187
rlm@3 188
rlm@3 189 // This indicates how far the average dialogue level is below digital 100 percent. Valid values are 1–31.
rlm@3 190 // 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.
rlm@3 191 $info_ac3_raw_bsi['dialnorm2'] = bindec(substr($ac3_header['bsi'], $offset, 5));
rlm@3 192 $offset += 5;
rlm@3 193
rlm@3 194 $info_ac3['dialogue_normalization2'] = '-'.$info_ac3_raw_bsi['dialnorm2'].'dB';
rlm@3 195
rlm@3 196 $info_ac3_raw_bsi['compre_flag2'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 197 if ($info_ac3_raw_bsi['compre_flag2']) {
rlm@3 198 $info_ac3_raw_bsi['compr2'] = bindec(substr($ac3_header['bsi'], $offset, 8));
rlm@3 199 $offset += 8;
rlm@3 200
rlm@3 201 $info_ac3['heavy_compression2'] = getid3_ac3::AC3heavyCompression($info_ac3_raw_bsi['compr2']);
rlm@3 202 }
rlm@3 203
rlm@3 204 $info_ac3_raw_bsi['langcode_flag2'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 205 if ($info_ac3_raw_bsi['langcode_flag2']) {
rlm@3 206 $info_ac3_raw_bsi['langcod2'] = bindec(substr($ac3_header['bsi'], $offset, 8));
rlm@3 207 $offset += 8;
rlm@3 208 }
rlm@3 209
rlm@3 210 $info_ac3_raw_bsi['audprodie2'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 211 if ($info_ac3_raw_bsi['audprodie2']) {
rlm@3 212 $info_ac3_raw_bsi['mixlevel2'] = bindec(substr($ac3_header['bsi'], $offset, 5));
rlm@3 213 $offset += 5;
rlm@3 214
rlm@3 215 $info_ac3_raw_bsi['roomtyp2'] = bindec(substr($ac3_header['bsi'], $offset, 2));
rlm@3 216 $offset += 2;
rlm@3 217
rlm@3 218 $info_ac3['mixing_level2'] = (80 + $info_ac3_raw_bsi['mixlevel2']).'dB';
rlm@3 219 $info_ac3['room_type2'] = getid3_ac3::AC3roomTypeLookup($info_ac3_raw_bsi['roomtyp2']);
rlm@3 220 }
rlm@3 221
rlm@3 222 }
rlm@3 223
rlm@3 224 $info_ac3_raw_bsi['copyright'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 225
rlm@3 226 $info_ac3_raw_bsi['original'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 227
rlm@3 228 $info_ac3_raw_bsi['timecode1_flag'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 229 if ($info_ac3_raw_bsi['timecode1_flag']) {
rlm@3 230 $info_ac3_raw_bsi['timecode1'] = bindec(substr($ac3_header['bsi'], $offset, 14));
rlm@3 231 $offset += 14;
rlm@3 232 }
rlm@3 233
rlm@3 234 $info_ac3_raw_bsi['timecode2_flag'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 235 if ($info_ac3_raw_bsi['timecode2_flag']) {
rlm@3 236 $info_ac3_raw_bsi['timecode2'] = bindec(substr($ac3_header['bsi'], $offset, 14));
rlm@3 237 $offset += 14;
rlm@3 238 }
rlm@3 239
rlm@3 240 $info_ac3_raw_bsi['addbsi_flag'] = $ac3_header['bsi']{$offset++} == '1';
rlm@3 241 if ($info_ac3_raw_bsi['addbsi_flag']) {
rlm@3 242 $info_ac3_raw_bsi['addbsi_length'] = bindec(substr($ac3_header['bsi'], $offset, 6));
rlm@3 243 $offset += 6;
rlm@3 244
rlm@3 245 $ac3_header['bsi'] .= getid3_lib::BigEndian2Bin($this->fread($info_ac3_raw_bsi['addbsi_length']));
rlm@3 246
rlm@3 247 $info_ac3_raw_bsi['addbsi_data'] = substr($ac3_header['bsi'], 119, $info_ac3_raw_bsi['addbsi_length'] * 8);
rlm@3 248 }
rlm@3 249
rlm@3 250 return true;
rlm@3 251 }
rlm@3 252
rlm@3 253
rlm@3 254
rlm@3 255 public static function AC3sampleRateCodeLookup($fscod) {
rlm@3 256
rlm@3 257 static $lookup = array (
rlm@3 258 0 => 48000,
rlm@3 259 1 => 44100,
rlm@3 260 2 => 32000,
rlm@3 261 3 => 'reserved' // If the reserved code is indicated, the decoder should not attempt to decode audio and should mute.
rlm@3 262 );
rlm@3 263 return (isset($lookup[$fscod]) ? $lookup[$fscod] : false);
rlm@3 264 }
rlm@3 265
rlm@3 266
rlm@3 267
rlm@3 268 public static function AC3serviceTypeLookup($bsmod, $acmod) {
rlm@3 269
rlm@3 270 static $lookup = array (
rlm@3 271 0 => 'main audio service: complete main (CM)',
rlm@3 272 1 => 'main audio service: music and effects (ME)',
rlm@3 273 2 => 'associated service: visually impaired (VI)',
rlm@3 274 3 => 'associated service: hearing impaired (HI)',
rlm@3 275 4 => 'associated service: dialogue (D)',
rlm@3 276 5 => 'associated service: commentary (C)',
rlm@3 277 6 => 'associated service: emergency (E)',
rlm@3 278 7 => 'main audio service: karaoke'
rlm@3 279 );
rlm@3 280
rlm@3 281 if ($bsmod == 7 && $acmod == 1) {
rlm@3 282 return 'associated service: voice over (VO)';
rlm@3 283 }
rlm@3 284
rlm@3 285 return (isset($lookup[$bsmod]) ? $lookup[$bsmod] : false);
rlm@3 286 }
rlm@3 287
rlm@3 288
rlm@3 289
rlm@3 290 public static function AC3audioCodingModeLookup($acmod) {
rlm@3 291
rlm@3 292 // array (channel configuration, # channels (not incl LFE), channel order)
rlm@3 293 static $lookup = array (
rlm@3 294 0 => array ('channel_config'=>'1+1', 'num_channels'=>2, 'channel_order'=>'Ch1,Ch2'),
rlm@3 295 1 => array ('channel_config'=>'1/0', 'num_channels'=>1, 'channel_order'=>'C'),
rlm@3 296 2 => array ('channel_config'=>'2/0', 'num_channels'=>2, 'channel_order'=>'L,R'),
rlm@3 297 3 => array ('channel_config'=>'3/0', 'num_channels'=>3, 'channel_order'=>'L,C,R'),
rlm@3 298 4 => array ('channel_config'=>'2/1', 'num_channels'=>3, 'channel_order'=>'L,R,S'),
rlm@3 299 5 => array ('channel_config'=>'3/1', 'num_channels'=>4, 'channel_order'=>'L,C,R,S'),
rlm@3 300 6 => array ('channel_config'=>'2/2', 'num_channels'=>4, 'channel_order'=>'L,R,SL,SR'),
rlm@3 301 7 => array ('channel_config'=>'3/2', 'num_channels'=>5, 'channel_order'=>'L,C,R,SL,SR')
rlm@3 302 );
rlm@3 303 return (isset($lookup[$acmod]) ? $lookup[$acmod] : false);
rlm@3 304 }
rlm@3 305
rlm@3 306
rlm@3 307
rlm@3 308 public static function AC3centerMixLevelLookup($cmixlev) {
rlm@3 309
rlm@3 310 static $lookup;
rlm@3 311 if (!@$lookup) {
rlm@3 312 $lookup = array (
rlm@3 313 0 => pow(2, -3.0 / 6), // 0.707 (–3.0 dB)
rlm@3 314 1 => pow(2, -4.5 / 6), // 0.595 (–4.5 dB)
rlm@3 315 2 => pow(2, -6.0 / 6), // 0.500 (–6.0 dB)
rlm@3 316 3 => 'reserved'
rlm@3 317 );
rlm@3 318 }
rlm@3 319 return (isset($lookup[$cmixlev]) ? $lookup[$cmixlev] : false);
rlm@3 320 }
rlm@3 321
rlm@3 322
rlm@3 323
rlm@3 324 public static function AC3surroundMixLevelLookup($surmixlev) {
rlm@3 325
rlm@3 326 static $lookup;
rlm@3 327 if (!@$lookup) {
rlm@3 328 $lookup = array (
rlm@3 329 0 => pow(2, -3.0 / 6),
rlm@3 330 1 => pow(2, -6.0 / 6),
rlm@3 331 2 => 0,
rlm@3 332 3 => 'reserved'
rlm@3 333 );
rlm@3 334 }
rlm@3 335 return (isset($lookup[$surmixlev]) ? $lookup[$surmixlev] : false);
rlm@3 336 }
rlm@3 337
rlm@3 338
rlm@3 339
rlm@3 340 public static function AC3dolbySurroundModeLookup($dsurmod) {
rlm@3 341
rlm@3 342 static $lookup = array (
rlm@3 343 0 => 'not indicated',
rlm@3 344 1 => 'Not Dolby Surround encoded',
rlm@3 345 2 => 'Dolby Surround encoded',
rlm@3 346 3 => 'reserved'
rlm@3 347 );
rlm@3 348 return (isset($lookup[$dsurmod]) ? $lookup[$dsurmod] : false);
rlm@3 349 }
rlm@3 350
rlm@3 351
rlm@3 352
rlm@3 353 public static function AC3channelsEnabledLookup($acmod, $lfeon) {
rlm@3 354
rlm@3 355 return array (
rlm@3 356 'ch1' => $acmod == 0,
rlm@3 357 'ch2' => $acmod == 0,
rlm@3 358 'left' => $acmod > 1,
rlm@3 359 'right' => $acmod > 1,
rlm@3 360 'center' => (bool)($acmod & 0x01),
rlm@3 361 'surround_mono' => $acmod == 4 || $acmod == 5,
rlm@3 362 'surround_left' => $acmod == 6 || $acmod == 7,
rlm@3 363 'surround_right' => $acmod == 6 || $acmod == 7,
rlm@3 364 'lfe' => $lfeon
rlm@3 365 );
rlm@3 366 }
rlm@3 367
rlm@3 368
rlm@3 369
rlm@3 370 public static function AC3heavyCompression($compre) {
rlm@3 371
rlm@3 372 // The first four bits indicate gain changes in 6.02dB increments which can be
rlm@3 373 // implemented with an arithmetic shift operation. The following four bits
rlm@3 374 // indicate linear gain changes, and require a 5-bit multiply.
rlm@3 375 // We will represent the two 4-bit fields of compr as follows:
rlm@3 376 // X0 X1 X2 X3 . Y4 Y5 Y6 Y7
rlm@3 377 // The meaning of the X values is most simply described by considering X to represent a 4-bit
rlm@3 378 // signed integer with values from –8 to +7. The gain indicated by X is then (X + 1) * 6.02 dB. The
rlm@3 379 // following table shows this in detail.
rlm@3 380
rlm@3 381 // Meaning of 4 msb of compr
rlm@3 382 // 7 +48.16 dB
rlm@3 383 // 6 +42.14 dB
rlm@3 384 // 5 +36.12 dB
rlm@3 385 // 4 +30.10 dB
rlm@3 386 // 3 +24.08 dB
rlm@3 387 // 2 +18.06 dB
rlm@3 388 // 1 +12.04 dB
rlm@3 389 // 0 +6.02 dB
rlm@3 390 // -1 0 dB
rlm@3 391 // -2 –6.02 dB
rlm@3 392 // -3 –12.04 dB
rlm@3 393 // -4 –18.06 dB
rlm@3 394 // -5 –24.08 dB
rlm@3 395 // -6 –30.10 dB
rlm@3 396 // -7 –36.12 dB
rlm@3 397 // -8 –42.14 dB
rlm@3 398
rlm@3 399 $fourbit = str_pad(decbin(($compre & 0xF0) >> 4), 4, '0', STR_PAD_LEFT);
rlm@3 400 if ($fourbit{0} == '1') {
rlm@3 401 $log_gain = -8 + bindec(substr($fourbit, 1));
rlm@3 402 } else {
rlm@3 403 $log_gain = bindec(substr($fourbit, 1));
rlm@3 404 }
rlm@3 405 $log_gain = ($log_gain + 1) * (20 * log10(2));
rlm@3 406
rlm@3 407 // The value of Y is a linear representation of a gain change of up to –6 dB. Y is considered to
rlm@3 408 // be an unsigned fractional integer, with a leading value of 1, or: 0.1 Y4 Y5 Y6 Y7 (base 2). Y can
rlm@3 409 // represent values between 0.111112 (or 31/32) and 0.100002 (or 1/2). Thus, Y can represent gain
rlm@3 410 // changes from –0.28 dB to –6.02 dB.
rlm@3 411
rlm@3 412 $lin_gain = (16 + ($compre & 0x0F)) / 32;
rlm@3 413
rlm@3 414 // The combination of X and Y values allows compr to indicate gain changes from
rlm@3 415 // 48.16 – 0.28 = +47.89 dB, to
rlm@3 416 // –42.14 – 6.02 = –48.16 dB.
rlm@3 417
rlm@3 418 return $log_gain - $lin_gain;
rlm@3 419 }
rlm@3 420
rlm@3 421
rlm@3 422
rlm@3 423 public static function AC3roomTypeLookup($roomtyp) {
rlm@3 424
rlm@3 425 static $lookup = array (
rlm@3 426 0 => 'not indicated',
rlm@3 427 1 => 'large room, X curve monitor',
rlm@3 428 2 => 'small room, flat monitor',
rlm@3 429 3 => 'reserved'
rlm@3 430 );
rlm@3 431 return (isset($lookup[$roomtyp]) ? $lookup[$roomtyp] : false);
rlm@3 432 }
rlm@3 433
rlm@3 434
rlm@3 435
rlm@3 436 public static function AC3frameSizeLookup($frmsizecod, $fscod) {
rlm@3 437
rlm@3 438 $padding = (bool)($frmsizecod % 2);
rlm@3 439 $frame_size_id = floor($frmsizecod / 2);
rlm@3 440
rlm@3 441 static $lookup = array (
rlm@3 442 0 => array (128, 138, 192),
rlm@3 443 1 => array (40, 160, 174, 240),
rlm@3 444 2 => array (48, 192, 208, 288),
rlm@3 445 3 => array (56, 224, 242, 336),
rlm@3 446 4 => array (64, 256, 278, 384),
rlm@3 447 5 => array (80, 320, 348, 480),
rlm@3 448 6 => array (96, 384, 416, 576),
rlm@3 449 7 => array (112, 448, 486, 672),
rlm@3 450 8 => array (128, 512, 556, 768),
rlm@3 451 9 => array (160, 640, 696, 960),
rlm@3 452 10 => array (192, 768, 834, 1152),
rlm@3 453 11 => array (224, 896, 974, 1344),
rlm@3 454 12 => array (256, 1024, 1114, 1536),
rlm@3 455 13 => array (320, 1280, 1392, 1920),
rlm@3 456 14 => array (384, 1536, 1670, 2304),
rlm@3 457 15 => array (448, 1792, 1950, 2688),
rlm@3 458 16 => array (512, 2048, 2228, 3072),
rlm@3 459 17 => array (576, 2304, 2506, 3456),
rlm@3 460 18 => array (640, 2560, 2786, 3840)
rlm@3 461 );
rlm@3 462 if (($fscod == 1) && $padding) {
rlm@3 463 // frame lengths are padded by 1 word (16 bits) at 44100
rlm@3 464 $lookup[$frmsizecod] += 2;
rlm@3 465 }
rlm@3 466 return (isset($lookup[$frame_size_id][$fscod]) ? $lookup[$frame_size_id][$fscod] : false);
rlm@3 467 }
rlm@3 468
rlm@3 469
rlm@3 470
rlm@3 471 public static function AC3bitrateLookup($frmsizecod) {
rlm@3 472
rlm@3 473 static $lookup = array (
rlm@3 474 0 => 32000,
rlm@3 475 1 => 40000,
rlm@3 476 2 => 48000,
rlm@3 477 3 => 56000,
rlm@3 478 4 => 64000,
rlm@3 479 5 => 80000,
rlm@3 480 6 => 96000,
rlm@3 481 7 => 112000,
rlm@3 482 8 => 128000,
rlm@3 483 9 => 160000,
rlm@3 484 10 => 192000,
rlm@3 485 11 => 224000,
rlm@3 486 12 => 256000,
rlm@3 487 13 => 320000,
rlm@3 488 14 => 384000,
rlm@3 489 15 => 448000,
rlm@3 490 16 => 512000,
rlm@3 491 17 => 576000,
rlm@3 492 18 => 640000
rlm@3 493 );
rlm@3 494 $frame_size_id = floor($frmsizecod / 2);
rlm@3 495 return (isset($lookup[$frame_size_id]) ? $lookup[$frame_size_id] : false);
rlm@3 496 }
rlm@3 497
rlm@3 498 }
rlm@3 499
rlm@3 500 ?>