diff e2gallerypro/e2upload/Backend/Assets/getid3/module.audio.aac_adif.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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/e2gallerypro/e2upload/Backend/Assets/getid3/module.audio.aac_adif.php	Mon Feb 22 08:02:39 2010 -0500
     1.3 @@ -0,0 +1,311 @@
     1.4 +<?php
     1.5 +// +----------------------------------------------------------------------+
     1.6 +// | PHP version 5                                                        |
     1.7 +// +----------------------------------------------------------------------+
     1.8 +// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
     1.9 +// +----------------------------------------------------------------------+
    1.10 +// | This source file is subject to version 2 of the GPL license,         |
    1.11 +// | that is bundled with this package in the file license.txt and is     |
    1.12 +// | available through the world-wide-web at the following url:           |
    1.13 +// | http://www.gnu.org/copyleft/gpl.html                                 |
    1.14 +// +----------------------------------------------------------------------+
    1.15 +// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
    1.16 +// +----------------------------------------------------------------------+
    1.17 +// | Authors: James Heinrich <infoØgetid3*org>                            |
    1.18 +// |          Allan Hansen <ahØartemis*dk>                                |
    1.19 +// +----------------------------------------------------------------------+
    1.20 +// | module.audio.aac_adif.php                                            |
    1.21 +// | Module for analyzing AAC files with ADIF header.                     |
    1.22 +// | dependencies: NONE                                                   |
    1.23 +// +----------------------------------------------------------------------+
    1.24 +//
    1.25 +// $Id: module.audio.aac_adif.php,v 1.3 2006/11/02 10:48:00 ah Exp $
    1.26 +
    1.27 +        
    1.28 +        
    1.29 +class getid3_aac_adif extends getid3_handler
    1.30 +{
    1.31 +
    1.32 +    public function Analyze() {
    1.33 +
    1.34 +        $getid3 = $this->getid3;    
    1.35 +
    1.36 +        // http://faac.sourceforge.net/wiki/index.php?page=ADIF
    1.37 +        // http://libmpeg.org/mpeg4/doc/w2203tfs.pdf
    1.38 +        // adif_header() {
    1.39 +        //     adif_id                                32
    1.40 +        //     copyright_id_present                    1
    1.41 +        //     if( copyright_id_present )
    1.42 +        //         copyright_id                       72
    1.43 +        //     original_copy                           1
    1.44 +        //     home                                    1
    1.45 +        //     bitstream_type                          1
    1.46 +        //     bitrate                                23
    1.47 +        //     num_program_config_elements             4
    1.48 +        //     for (i = 0; i < num_program_config_elements + 1; i++ ) {
    1.49 +        //         if( bitstream_type == '0' )
    1.50 +        //             adif_buffer_fullness           20
    1.51 +        //         program_config_element()
    1.52 +        //     }
    1.53 +        // }
    1.54 +        
    1.55 +
    1.56 +        $getid3->info['fileformat']          = 'aac';
    1.57 +        $getid3->info['audio']['dataformat'] = 'aac';
    1.58 +        $getid3->info['audio']['lossless']   = false;
    1.59 +        
    1.60 +        $getid3->info['aac']['header'] = array () ;
    1.61 +        $info_aac        = &$getid3->info['aac'];
    1.62 +        $info_aac_header = & $info_aac['header'];
    1.63 +
    1.64 +        fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
    1.65 +        $aac_header_bitstream = getid3_lib::BigEndian2Bin(fread($getid3->fp, 1024));
    1.66 +        
    1.67 +        $info_aac['header_type']            = 'ADIF';
    1.68 +        $info_aac_header['mpeg_version'] = 4;
    1.69 +        $bit_offset = 32;
    1.70 +
    1.71 +        $info_aac_header['copyright'] = $aac_header_bitstream{$bit_offset++} == '1';
    1.72 +        if ($info_aac_header['copyright']) {
    1.73 +            $info_aac_header['copyright_id'] = getid3_aac_adif::Bin2String(substr($aac_header_bitstream, $bit_offset, 72));
    1.74 +            $bit_offset += 72;
    1.75 +        }
    1.76 +        
    1.77 +        $info_aac_header['original_copy'] = $aac_header_bitstream{$bit_offset++} == '1';
    1.78 +        $info_aac_header['home']          = $aac_header_bitstream{$bit_offset++} == '1';
    1.79 +        $info_aac_header['is_vbr']        = $aac_header_bitstream{$bit_offset++} == '1';
    1.80 +
    1.81 +        if ($info_aac_header['is_vbr']) {
    1.82 +            $getid3->info['audio']['bitrate_mode'] = 'vbr';
    1.83 +            $info_aac_header['bitrate_max']     = bindec(substr($aac_header_bitstream, $bit_offset, 23));
    1.84 +            $bit_offset += 23;
    1.85 +        } 
    1.86 +        else {
    1.87 +            $getid3->info['audio']['bitrate_mode'] = 'cbr';
    1.88 +            $info_aac_header['bitrate']         = bindec(substr($aac_header_bitstream, $bit_offset, 23));
    1.89 +            $bit_offset += 23;
    1.90 +            $getid3->info['audio']['bitrate']      = $info_aac_header['bitrate'];
    1.91 +        }
    1.92 +        
    1.93 +        $info_aac_header['num_program_configs'] = 1 + bindec(substr($aac_header_bitstream, $bit_offset, 4));
    1.94 +        $bit_offset += 4;
    1.95 +
    1.96 +        for ($i = 0; $i < $info_aac_header['num_program_configs']; $i++) {
    1.97 +
    1.98 +            // http://www.audiocoding.com/wiki/index.php?page=program_config_element
    1.99 +
   1.100 +            // buffer_fullness                       20
   1.101 +
   1.102 +            // element_instance_tag                   4
   1.103 +            // object_type                            2
   1.104 +            // sampling_frequency_index               4
   1.105 +            // num_front_channel_elements             4
   1.106 +            // num_side_channel_elements              4
   1.107 +            // num_back_channel_elements              4
   1.108 +            // num_lfe_channel_elements               2
   1.109 +            // num_assoc_data_elements                3
   1.110 +            // num_valid_cc_elements                  4
   1.111 +            // mono_mixdown_present                   1
   1.112 +            // mono_mixdown_element_number            4   if mono_mixdown_present == 1
   1.113 +            // stereo_mixdown_present                 1
   1.114 +            // stereo_mixdown_element_number          4   if stereo_mixdown_present == 1
   1.115 +            // matrix_mixdown_idx_present             1
   1.116 +            // matrix_mixdown_idx                     2   if matrix_mixdown_idx_present == 1
   1.117 +            // pseudo_surround_enable                 1   if matrix_mixdown_idx_present == 1
   1.118 +            // for (i = 0; i < num_front_channel_elements; i++) {
   1.119 +            //     front_element_is_cpe[i]            1
   1.120 +            //     front_element_tag_select[i]        4
   1.121 +            // }
   1.122 +            // for (i = 0; i < num_side_channel_elements; i++) {
   1.123 +            //     side_element_is_cpe[i]             1
   1.124 +            //     side_element_tag_select[i]         4
   1.125 +            // }
   1.126 +            // for (i = 0; i < num_back_channel_elements; i++) {
   1.127 +            //     back_element_is_cpe[i]             1
   1.128 +            //     back_element_tag_select[i]         4
   1.129 +            // }
   1.130 +            // for (i = 0; i < num_lfe_channel_elements; i++) {
   1.131 +            //     lfe_element_tag_select[i]          4
   1.132 +            // }
   1.133 +            // for (i = 0; i < num_assoc_data_elements; i++) {
   1.134 +            //     assoc_data_element_tag_select[i]   4
   1.135 +            // }
   1.136 +            // for (i = 0; i < num_valid_cc_elements; i++) {
   1.137 +            //     cc_element_is_ind_sw[i]            1
   1.138 +            //     valid_cc_element_tag_select[i]     4
   1.139 +            // }
   1.140 +            // byte_alignment()                       VAR
   1.141 +            // comment_field_bytes                    8
   1.142 +            // for (i = 0; i < comment_field_bytes; i++) {
   1.143 +            //     comment_field_data[i]              8
   1.144 +            // }
   1.145 +            
   1.146 +            $info_aac['program_configs'][$i] = array ();
   1.147 +            $info_aac_program_configs_i = &$info_aac['program_configs'][$i];
   1.148 +
   1.149 +            if (!$info_aac_header['is_vbr']) {
   1.150 +                $info_aac_program_configs_i['buffer_fullness']        = bindec(substr($aac_header_bitstream, $bit_offset, 20));
   1.151 +                $bit_offset += 20;
   1.152 +            }
   1.153 +                
   1.154 +            $info_aac_program_configs_i['element_instance_tag']       = bindec(substr($aac_header_bitstream, $bit_offset,      4));
   1.155 +            $info_aac_program_configs_i['object_type']                = bindec(substr($aac_header_bitstream, $bit_offset +  4, 2));
   1.156 +            $info_aac_program_configs_i['sampling_frequency_index']   = bindec(substr($aac_header_bitstream, $bit_offset +  6, 4));
   1.157 +            $info_aac_program_configs_i['num_front_channel_elements'] = bindec(substr($aac_header_bitstream, $bit_offset + 10, 4));
   1.158 +            $info_aac_program_configs_i['num_side_channel_elements']  = bindec(substr($aac_header_bitstream, $bit_offset + 14, 4));
   1.159 +            $info_aac_program_configs_i['num_back_channel_elements']  = bindec(substr($aac_header_bitstream, $bit_offset + 18, 4));
   1.160 +            $info_aac_program_configs_i['num_lfe_channel_elements']   = bindec(substr($aac_header_bitstream, $bit_offset + 22, 2));
   1.161 +            $info_aac_program_configs_i['num_assoc_data_elements']    = bindec(substr($aac_header_bitstream, $bit_offset + 24, 3));
   1.162 +            $info_aac_program_configs_i['num_valid_cc_elements']      = bindec(substr($aac_header_bitstream, $bit_offset + 27, 4));
   1.163 +            $bit_offset += 31;
   1.164 +            
   1.165 +            $info_aac_program_configs_i['mono_mixdown_present'] = $aac_header_bitstream{$bit_offset++} == 1;
   1.166 +            if ($info_aac_program_configs_i['mono_mixdown_present']) {
   1.167 +                $info_aac_program_configs_i['mono_mixdown_element_number'] = bindec(substr($aac_header_bitstream, $bit_offset, 4));
   1.168 +                $bit_offset += 4;
   1.169 +            }
   1.170 +            
   1.171 +            $info_aac_program_configs_i['stereo_mixdown_present'] = $aac_header_bitstream{$bit_offset++} == 1;
   1.172 +            if ($info_aac_program_configs_i['stereo_mixdown_present']) {
   1.173 +                $info_aac_program_configs_i['stereo_mixdown_element_number'] = bindec(substr($aac_header_bitstream, $bit_offset, 4));
   1.174 +                $bit_offset += 4;
   1.175 +            }
   1.176 +            
   1.177 +            $info_aac_program_configs_i['matrix_mixdown_idx_present'] = $aac_header_bitstream{$bit_offset++} == 1;
   1.178 +            if ($info_aac_program_configs_i['matrix_mixdown_idx_present']) {
   1.179 +                $info_aac_program_configs_i['matrix_mixdown_idx']     = bindec(substr($aac_header_bitstream, $bit_offset, 2));
   1.180 +                $bit_offset += 2;
   1.181 +                $info_aac_program_configs_i['pseudo_surround_enable'] = $aac_header_bitstream{$bit_offset++} == 1;
   1.182 +            }
   1.183 +            
   1.184 +            for ($j = 0; $j < $info_aac_program_configs_i['num_front_channel_elements']; $j++) {
   1.185 +                $info_aac_program_configs_i['front_element_is_cpe'][$j]     = $aac_header_bitstream{$bit_offset++} == 1;
   1.186 +                $info_aac_program_configs_i['front_element_tag_select'][$j] = bindec(substr($aac_header_bitstream, $bit_offset, 4));
   1.187 +                $bit_offset += 4;
   1.188 +            }
   1.189 +            for ($j = 0; $j < $info_aac_program_configs_i['num_side_channel_elements']; $j++) {
   1.190 +                $info_aac_program_configs_i['side_element_is_cpe'][$j]     = $aac_header_bitstream{$bit_offset++} == 1;
   1.191 +                $info_aac_program_configs_i['side_element_tag_select'][$j] = bindec(substr($aac_header_bitstream, $bit_offset, 4));
   1.192 +                $bit_offset += 4;
   1.193 +            }
   1.194 +            for ($j = 0; $j < $info_aac_program_configs_i['num_back_channel_elements']; $j++) {
   1.195 +                $info_aac_program_configs_i['back_element_is_cpe'][$j]     = $aac_header_bitstream{$bit_offset++} == 1;
   1.196 +                $info_aac_program_configs_i['back_element_tag_select'][$j] = bindec(substr($aac_header_bitstream, $bit_offset, 4));
   1.197 +                $bit_offset += 4;
   1.198 +            }
   1.199 +            for ($j = 0; $j < $info_aac_program_configs_i['num_lfe_channel_elements']; $j++) {
   1.200 +                $info_aac_program_configs_i['lfe_element_tag_select'][$j] = bindec(substr($aac_header_bitstream, $bit_offset, 4));
   1.201 +                $bit_offset += 4;
   1.202 +            }
   1.203 +            for ($j = 0; $j < $info_aac_program_configs_i['num_assoc_data_elements']; $j++) {
   1.204 +                $info_aac_program_configs_i['assoc_data_element_tag_select'][$j] = bindec(substr($aac_header_bitstream, $bit_offset, 4));
   1.205 +                $bit_offset += 4;
   1.206 +            }
   1.207 +            for ($j = 0; $j < $info_aac_program_configs_i['num_valid_cc_elements']; $j++) {
   1.208 +                $info_aac_program_configs_i['cc_element_is_ind_sw'][$j]          = $aac_header_bitstream{$bit_offset++} == 1;
   1.209 +                $info_aac_program_configs_i['valid_cc_element_tag_select'][$j]   = bindec(substr($aac_header_bitstream, $bit_offset, 4));
   1.210 +                $bit_offset += 4;
   1.211 +            }
   1.212 +
   1.213 +            $bit_offset = ceil($bit_offset / 8) * 8;
   1.214 +
   1.215 +            $info_aac_program_configs_i['comment_field_bytes'] = bindec(substr($aac_header_bitstream, $bit_offset, 8));
   1.216 +            $bit_offset += 8;
   1.217 +            
   1.218 +            $info_aac_program_configs_i['comment_field'] = getid3_aac_adif::Bin2String(substr($aac_header_bitstream, $bit_offset, 8 * $info_aac_program_configs_i['comment_field_bytes']));
   1.219 +            $bit_offset += 8 * $info_aac_program_configs_i['comment_field_bytes'];
   1.220 +
   1.221 +            $info_aac_header['profile_text']                  = getid3_aac_adif::AACprofileLookup($info_aac_program_configs_i['object_type'], $info_aac_header['mpeg_version']);
   1.222 +            $info_aac_program_configs_i['sampling_frequency'] = $getid3->info['audio']['sample_rate'] = getid3_aac_adif::AACsampleRateLookup($info_aac_program_configs_i['sampling_frequency_index']);
   1.223 +            $getid3->info['audio']['channels']                = getid3_aac_adif::AACchannelCountCalculate($info_aac_program_configs_i);
   1.224 +            
   1.225 +            if ($info_aac_program_configs_i['comment_field']) {
   1.226 +                $info_aac['comments'][] = $info_aac_program_configs_i['comment_field'];
   1.227 +            }
   1.228 +        }
   1.229 +        
   1.230 +        $getid3->info['playtime_seconds'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / $getid3->info['audio']['bitrate'];
   1.231 +        $getid3->info['audio']['encoder_options'] = $info_aac['header_type'].' '.$info_aac_header['profile_text'];
   1.232 +
   1.233 +        return true;
   1.234 +    }
   1.235 +
   1.236 +
   1.237 +
   1.238 +    public static function Bin2String($bin_string) {
   1.239 +        // return 'hi' for input of '0110100001101001'
   1.240 +        $string = '';
   1.241 +        $bin_string_reversed = strrev($bin_string);
   1.242 +        for ($i = 0; $i < strlen($bin_string_reversed); $i += 8) {
   1.243 +            $string = chr(bindec(strrev(substr($bin_string_reversed, $i, 8)))).$string;
   1.244 +        }
   1.245 +        return $string;
   1.246 +    }
   1.247 +    
   1.248 +    
   1.249 +    
   1.250 +    public static function AACsampleRateLookup($samplerate_id) {
   1.251 +        
   1.252 +        static $lookup = array (
   1.253 +            0  => 96000,
   1.254 +            1  => 88200,
   1.255 +            2  => 64000,
   1.256 +            3  => 48000,
   1.257 +            4  => 44100,
   1.258 +            5  => 32000,
   1.259 +            6  => 24000,
   1.260 +            7  => 22050,
   1.261 +            8  => 16000,
   1.262 +            9  => 12000,
   1.263 +            10 => 11025,
   1.264 +            11 => 8000,
   1.265 +            12 => 0,
   1.266 +            13 => 0,
   1.267 +            14 => 0,
   1.268 +            15 => 0
   1.269 +        );
   1.270 +        return (isset($lookup[$samplerate_id]) ? $lookup[$samplerate_id] : 'invalid');
   1.271 +    }
   1.272 +
   1.273 +
   1.274 +
   1.275 +    public static function AACprofileLookup($profile_id, $mpeg_version) {
   1.276 +        
   1.277 +        static $lookup = array (
   1.278 +            2 => array ( 
   1.279 +                0 => 'Main profile',
   1.280 +                1 => 'Low Complexity profile (LC)',
   1.281 +                2 => 'Scalable Sample Rate profile (SSR)',
   1.282 +                3 => '(reserved)'
   1.283 +            ),            
   1.284 +            4 => array (
   1.285 +                0 => 'AAC_MAIN',
   1.286 +                1 => 'AAC_LC',
   1.287 +                2 => 'AAC_SSR',
   1.288 +                3 => 'AAC_LTP'
   1.289 +            )
   1.290 +        );
   1.291 +        return (isset($lookup[$mpeg_version][$profile_id]) ? $lookup[$mpeg_version][$profile_id] : 'invalid');
   1.292 +    }
   1.293 +
   1.294 +
   1.295 +
   1.296 +    public static function AACchannelCountCalculate($program_configs) {
   1.297 +        
   1.298 +        $channels = 0;
   1.299 +        
   1.300 +        foreach (array ('front', 'side', 'back') as $placement) {
   1.301 +            for ($i = 0; $i < $program_configs['num_'.$placement.'_channel_elements']; $i++) {
   1.302 +                
   1.303 +                // Each element is channel pair (CPE = Channel Pair Element)
   1.304 +                $channels += 1 + ($program_configs[$placement.'_element_is_cpe'][$i] ? 1 : 0);
   1.305 +            }
   1.306 +        }
   1.307 +        
   1.308 +        return $channels + $program_configs['num_lfe_channel_elements'];
   1.309 +    }
   1.310 +
   1.311 +}
   1.312 +
   1.313 +
   1.314 +?>
   1.315 \ No newline at end of file