rlm@3: <?php
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | PHP version 5                                                        |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | This source file is subject to version 2 of the GPL license,         |
rlm@3: // | that is bundled with this package in the file license.txt and is     |
rlm@3: // | available through the world-wide-web at the following url:           |
rlm@3: // | http://www.gnu.org/copyleft/gpl.html                                 |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | Authors: James Heinrich <infoØgetid3*org>                            |
rlm@3: // |          Allan Hansen <ahØartemis*dk>                                |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | module.audio.dts.php                                                 |
rlm@3: // | Module for analyzing DTS audio files                                 |
rlm@3: // | dependencies: NONE                                                   |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: //
rlm@3: // $Id: module.audio.dts.php,v 1.2 2006/11/16 13:14:26 ah Exp $
rlm@3: 
rlm@3:         
rlm@3:         
rlm@3: // Specs taken from "DTS Coherent Acoustics;Core and Extensions,  ETSI TS 102 114 V1.2.1 (2002-12)"
rlm@3: // (http://pda.etsi.org/pda/queryform.asp)
rlm@3: // With thanks to Gambit <macteam@users.sourceforge.net> http://mac.sourceforge.net/atl/
rlm@3:         
rlm@3: class getid3_dts extends getid3_handler
rlm@3: {
rlm@3: 
rlm@3:     public function Analyze() {
rlm@3:         
rlm@3:         $getid3 = $this->getid3;
rlm@3:         
rlm@3:         $getid3->info['dts'] = array ();
rlm@3:         $info_dts = &$getid3->info['dts'];
rlm@3: 
rlm@3:         $getid3->info['fileformat'] = 'dts';
rlm@3: 
rlm@3:         fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
rlm@3:         $header = fread($getid3->fp, 16);
rlm@3: 
rlm@3:         $fhBS = getid3_lib::BigEndian2Bin(substr($header, 4, 12));
rlm@3:         $bs_offset = 0;
rlm@3:         $info_dts['raw']['frame_type']             =        bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['raw']['deficit_samples']        =        bindec(substr($fhBS, $bs_offset,  5)); $bs_offset +=  5;
rlm@3:         $info_dts['flags']['crc_present']          = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['raw']['pcm_sample_blocks']      =        bindec(substr($fhBS, $bs_offset,  7)); $bs_offset +=  7;
rlm@3:         $info_dts['raw']['frame_byte_size']        =        bindec(substr($fhBS, $bs_offset, 14)); $bs_offset += 14;
rlm@3:         $info_dts['raw']['channel_arrangement']    =        bindec(substr($fhBS, $bs_offset,  6)); $bs_offset +=  6;
rlm@3:         $info_dts['raw']['sample_frequency']       =        bindec(substr($fhBS, $bs_offset,  4)); $bs_offset +=  4;
rlm@3:         $info_dts['raw']['bitrate']                =        bindec(substr($fhBS, $bs_offset,  5)); $bs_offset +=  5;
rlm@3:         $info_dts['flags']['embedded_downmix']     = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['flags']['dynamicrange']         = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['flags']['timestamp']            = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['flags']['auxdata']              = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['flags']['hdcd']                 = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['raw']['extension_audio']        =        bindec(substr($fhBS, $bs_offset,  3)); $bs_offset +=  3;
rlm@3:         $info_dts['flags']['extended_coding']      = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['flags']['audio_sync_insertion'] = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['raw']['lfe_effects']            =        bindec(substr($fhBS, $bs_offset,  2)); $bs_offset +=  2;
rlm@3:         $info_dts['flags']['predictor_history']    = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         if ($info_dts['flags']['crc_present']) {
rlm@3:             $info_dts['raw']['crc16']              =        bindec(substr($fhBS, $bs_offset, 16)); $bs_offset += 16;
rlm@3:         }
rlm@3:         $info_dts['flags']['mri_perfect_reconst']  = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['raw']['encoder_soft_version']   =        bindec(substr($fhBS, $bs_offset,  4)); $bs_offset +=  4;
rlm@3:         $info_dts['raw']['copy_history']           =        bindec(substr($fhBS, $bs_offset,  2)); $bs_offset +=  2;
rlm@3:         $info_dts['raw']['bits_per_sample']        =        bindec(substr($fhBS, $bs_offset,  2)); $bs_offset +=  2;
rlm@3:         $info_dts['flags']['surround_es']          = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['flags']['front_sum_diff']       = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['flags']['surround_sum_diff']    = (bool) bindec(substr($fhBS, $bs_offset,  1)); $bs_offset +=  1;
rlm@3:         $info_dts['raw']['dialog_normalization']   =        bindec(substr($fhBS, $bs_offset,  4)); $bs_offset +=  4;
rlm@3: 
rlm@3: 
rlm@3:         $info_dts['bitrate']              = $this->DTSbitrateLookup($info_dts['raw']['bitrate']);
rlm@3:         $info_dts['bits_per_sample']      = $this->DTSbitPerSampleLookup($info_dts['raw']['bits_per_sample']);
rlm@3:         $info_dts['sample_rate']          = $this->DTSsampleRateLookup($info_dts['raw']['sample_frequency']);
rlm@3:         $info_dts['dialog_normalization'] = $this->DTSdialogNormalization($info_dts['raw']['dialog_normalization'], $info_dts['raw']['encoder_soft_version']);
rlm@3:         $info_dts['flags']['lossless']    = (($info_dts['raw']['bitrate'] == 31) ? true  : false);
rlm@3:         $info_dts['bitrate_mode']         = (($info_dts['raw']['bitrate'] == 30) ? 'vbr' : 'cbr');
rlm@3:         $info_dts['channels']             = $this->DTSnumChannelsLookup($info_dts['raw']['channel_arrangement']);
rlm@3:         $info_dts['channel_arrangement']  = $this->DTSchannelArrangementLookup($info_dts['raw']['channel_arrangement']);
rlm@3: 
rlm@3:         $getid3->info['audio']['dataformat']      = 'dts';
rlm@3:         $getid3->info['audio']['lossless']        = $info_dts['flags']['lossless'];
rlm@3:         $getid3->info['audio']['bitrate_mode']    = $info_dts['bitrate_mode'];
rlm@3:         $getid3->info['audio']['bits_per_sample'] = $info_dts['bits_per_sample'];
rlm@3:         $getid3->info['audio']['sample_rate']     = $info_dts['sample_rate'];
rlm@3:         $getid3->info['audio']['channels']        = $info_dts['channels'];
rlm@3:         $getid3->info['audio']['bitrate']         = $info_dts['bitrate'];
rlm@3:         $getid3->info['playtime_seconds']         = ($getid3->info['avdataend'] - $getid3->info['avdataoffset']) / ($info_dts['bitrate'] / 8);
rlm@3: 
rlm@3:         return true;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3:     public static function DTSbitrateLookup($index) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0  => 32000,
rlm@3:             1  => 56000,
rlm@3:             2  => 64000,
rlm@3:             3  => 96000,
rlm@3:             4  => 112000,
rlm@3:             5  => 128000,
rlm@3:             6  => 192000,
rlm@3:             7  => 224000,
rlm@3:             8  => 256000,
rlm@3:             9  => 320000,
rlm@3:             10 => 384000,
rlm@3:             11 => 448000,
rlm@3:             12 => 512000,
rlm@3:             13 => 576000,
rlm@3:             14 => 640000,
rlm@3:             15 => 768000,
rlm@3:             16 => 960000,
rlm@3:             17 => 1024000,
rlm@3:             18 => 1152000,
rlm@3:             19 => 1280000,
rlm@3:             20 => 1344000,
rlm@3:             21 => 1408000,
rlm@3:             22 => 1411200,
rlm@3:             23 => 1472000,
rlm@3:             24 => 1536000,
rlm@3:             25 => 1920000,
rlm@3:             26 => 2048000,
rlm@3:             27 => 3072000,
rlm@3:             28 => 3840000,
rlm@3:             29 => 'open',
rlm@3:             30 => 'variable',
rlm@3:             31 => 'lossless'
rlm@3:         );
rlm@3:         return @$lookup[$index];
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     public static function DTSsampleRateLookup($index) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0  => 'invalid',
rlm@3:             1  => 8000,
rlm@3:             2  => 16000,
rlm@3:             3  => 32000,
rlm@3:             4  => 'invalid',
rlm@3:             5  => 'invalid',
rlm@3:             6  => 11025,
rlm@3:             7  => 22050,
rlm@3:             8  => 44100,
rlm@3:             9  => 'invalid',
rlm@3:             10 => 'invalid',
rlm@3:             11 => 12000,
rlm@3:             12 => 24000,
rlm@3:             13 => 48000,
rlm@3:             14 => 'invalid',
rlm@3:             15 => 'invalid'
rlm@3:         );
rlm@3:         return @$lookup[$index];
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     public static function DTSbitPerSampleLookup($index) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0  => 16,
rlm@3:             1  => 20,
rlm@3:             2  => 24,
rlm@3:             3  => 24,
rlm@3:         );
rlm@3:         return @$lookup[$index];
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     public static function DTSnumChannelsLookup($index) {
rlm@3:         
rlm@3:         switch ($index) {
rlm@3:             case 0:
rlm@3:                 return 1;
rlm@3:                 
rlm@3:             case 1:
rlm@3:             case 2:
rlm@3:             case 3:
rlm@3:             case 4:
rlm@3:                 return 2;
rlm@3:                 
rlm@3:             case 5:
rlm@3:             case 6:
rlm@3:                 return 3;
rlm@3:                 
rlm@3:             case 7:
rlm@3:             case 8:
rlm@3:                 return 4;
rlm@3:                 
rlm@3:             case 9:
rlm@3:                 return 5;
rlm@3:                 
rlm@3:             case 10:
rlm@3:             case 11:
rlm@3:             case 12:
rlm@3:                 return 6;
rlm@3:                 
rlm@3:             case 13:
rlm@3:                 return 7;
rlm@3:                 
rlm@3:             case 14:
rlm@3:             case 15:
rlm@3:                 return 8;
rlm@3:         }
rlm@3:         return false;
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     public static function DTSchannelArrangementLookup($index) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0  => 'A',
rlm@3:             1  => 'A + B (dual mono)',
rlm@3:             2  => 'L + R (stereo)',
rlm@3:             3  => '(L+R) + (L-R) (sum-difference)',
rlm@3:             4  => 'LT + RT (left and right total)',
rlm@3:             5  => 'C + L + R',
rlm@3:             6  => 'L + R + S',
rlm@3:             7  => 'C + L + R + S',
rlm@3:             8  => 'L + R + SL + SR',
rlm@3:             9  => 'C + L + R + SL + SR',
rlm@3:             10 => 'CL + CR + L + R + SL + SR',
rlm@3:             11 => 'C + L + R+ LR + RR + OV',
rlm@3:             12 => 'CF + CR + LF + RF + LR + RR',
rlm@3:             13 => 'CL + C + CR + L + R + SL + SR',
rlm@3:             14 => 'CL + CR + L + R + SL1 + SL2 + SR1 + SR2',
rlm@3:             15 => 'CL + C+ CR + L + R + SL + S + SR',
rlm@3:         );
rlm@3:         return (@$lookup[$index] ? @$lookup[$index] : 'user-defined');
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     public static function DTSdialogNormalization($index, $version) {
rlm@3:         
rlm@3:         switch ($version) {
rlm@3:             case 7:
rlm@3:                 return 0 - $index;
rlm@3:                 
rlm@3:             case 6:
rlm@3:                 return 0 - 16 - $index;
rlm@3:         }
rlm@3:         return false;
rlm@3:     }
rlm@3: 
rlm@3: }
rlm@3: 
rlm@3: 
rlm@3: ?>