source: extensions/charlies_content/getid3/getid3/module.audio.optimfrog.php @ 3318

Last change on this file since 3318 was 3318, checked in by vdigital, 15 years ago

+ Add Charlies' content to depository

  • Property svn:keywords set to Author Date Id Revision
File size: 19.3 KB
RevLine 
[3318]1<?php
2// +----------------------------------------------------------------------+
3// | PHP version 5                                                        |
4// +----------------------------------------------------------------------+
5// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
6// +----------------------------------------------------------------------+
7// | This source file is subject to version 2 of the GPL license,         |
8// | that is bundled with this package in the file license.txt and is     |
9// | available through the world-wide-web at the following url:           |
10// | http://www.gnu.org/copyleft/gpl.html                                 |
11// +----------------------------------------------------------------------+
12// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
13// +----------------------------------------------------------------------+
14// | Authors: James Heinrich <infoØgetid3*org>                            |
15// |          Allan Hansen <ahØartemis*dk>                                |
16// +----------------------------------------------------------------------+
17// | module.audio.optimfrog.php                                           |
18// | Module for analyzing OptimFROG Audio files                           |
19// | dependencies: module.audio-video.riff.php                            |
20// +----------------------------------------------------------------------+
21//
22// $Id: module.audio.optimfrog.php 3318 2009-05-20 21:54:10Z vdigital $
23
24       
25       
26class getid3_optimfrog extends getid3_handler
27{
28
29    public function Analyze() {
30       
31        $getid3 = $this->getid3;
32       
33        $getid3->include_module('audio-video.riff');
34       
35        $getid3->info['audio']['dataformat']   = 'ofr';
36        $getid3->info['audio']['bitrate_mode'] = 'vbr';
37        $getid3->info['audio']['lossless']     = true;
38
39        fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
40        $ofr_header = fread($getid3->fp, 8);
41       
42        if (substr($ofr_header, 0, 5) == '*RIFF') {
43            return $this->ParseOptimFROGheader42($getid3->fp, $getid3->info);
44
45        } elseif (substr($ofr_header, 0, 3) == 'OFR') {
46            return $this->ParseOptimFROGheader45($getid3->fp, $getid3->info);
47        }
48       
49        throw new getid3_exception('Expecting "*RIFF" or "OFR " at offset '.$getid3->info['avdataoffset'].', found "'.$ofr_header.'"');
50    }
51
52
53
54    private function ParseOptimFROGheader42() {
55       
56        $getid3 = $this->getid3;
57       
58        // for fileformat of v4.21 and older
59
60        fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
61       
62        $ofr_header_data = fread($getid3->fp, 45);
63        $getid3->info['avdataoffset'] = 45;
64
65        $ofr_encoder_version_raw   = getid3_lib::LittleEndian2Int(substr($ofr_header_data, 0, 1));
66        $ofr_encoder_version_major = floor($ofr_encoder_version_raw / 10);
67        $ofr_encoder_version_minor = $ofr_encoder_version_raw - ($ofr_encoder_version_major * 10);
68        $riff_data                 = substr($ofr_header_data, 1, 44);
69        $origna_riff_header_size   = getid3_lib::LittleEndian2Int(substr($riff_data,  4, 4)) +  8;
70        $origna_riff_data_size     = getid3_lib::LittleEndian2Int(substr($riff_data, 40, 4)) + 44;
71
72        if ($origna_riff_header_size > $origna_riff_data_size) {
73            $getid3->info['avdataend'] -= ($origna_riff_header_size - $origna_riff_data_size);
74            fseek($getid3->fp, $getid3->info['avdataend'], SEEK_SET);
75            $riff_data .= fread($getid3->fp, $origna_riff_header_size - $origna_riff_data_size);
76        }
77
78        // move the data chunk after all other chunks (if any)
79        // so that the RIFF parser doesn't see EOF when trying
80        // to skip over the data chunk
81       
82        $riff_data = substr($riff_data, 0, 36).substr($riff_data, 44).substr($riff_data, 36, 8);
83       
84        // Save audio info key
85        $saved_info_audio = $getid3->info['audio'];
86
87        // Instantiate riff module and analyze string
88        $riff = new getid3_riff($getid3);
89        $riff->AnalyzeString($riff_data);
90       
91        // Restore info key
92        $getid3->info['audio'] = $saved_info_audio;
93       
94        $getid3->info['audio']['encoder']         = 'OptimFROG '.$ofr_encoder_version_major.'.'.$ofr_encoder_version_minor;
95        $getid3->info['audio']['channels']        = $getid3->info['riff']['audio'][0]['channels'];
96        $getid3->info['audio']['sample_rate']     = $getid3->info['riff']['audio'][0]['sample_rate'];
97        $getid3->info['audio']['bits_per_sample'] = $getid3->info['riff']['audio'][0]['bits_per_sample'];
98        $getid3->info['playtime_seconds']         = $origna_riff_data_size / ($getid3->info['audio']['channels'] * $getid3->info['audio']['sample_rate'] * ($getid3->info['audio']['bits_per_sample'] / 8));
99        $getid3->info['audio']['bitrate']         = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / $getid3->info['playtime_seconds'];
100       
101        $getid3->info['fileformat'] = 'ofr';
102
103        return true;
104    }
105
106
107
108    private function ParseOptimFROGheader45() {
109       
110        $getid3 = $this->getid3;
111       
112        // for fileformat of v4.50a and higher
113
114        $riff_data = '';
115        fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
116       
117        while (!feof($getid3->fp) && (ftell($getid3->fp) < $getid3->info['avdataend'])) {
118       
119            $block_offset = ftell($getid3->fp);
120            $block_data   = fread($getid3->fp, 8);
121            $offset       = 8;
122            $block_name   =                  substr($block_data, 0, 4);
123            $block_size   = getid3_lib::LittleEndian2Int(substr($block_data, 4, 4));
124
125            if ($block_name == 'OFRX') {
126                $block_name = 'OFR ';
127            }
128            if (!isset($getid3->info['ofr'][$block_name])) {
129                $getid3->info['ofr'][$block_name] = array ();
130            }
131            $info_ofr_this_block = &$getid3->info['ofr'][$block_name];
132
133            switch ($block_name) {
134                case 'OFR ':
135
136                    // shortcut
137                    $info_ofr_this_block['offset'] = $block_offset;
138                    $info_ofr_this_block['size']   = $block_size;
139
140                    $getid3->info['audio']['encoder'] = 'OptimFROG 4.50 alpha';
141                    switch ($block_size) {
142                        case 12:
143                        case 15:
144                            // good
145                            break;
146
147                        default:
148                            $getid3->warning('"'.$block_name.'" contains more data than expected (expected 12 or 15 bytes, found '.$block_size.' bytes)');
149                            break;
150                    }
151                    $block_data .= fread($getid3->fp, $block_size);
152
153                    $info_ofr_this_block['total_samples']      = getid3_lib::LittleEndian2Int(substr($block_data, $offset, 6));
154                    $offset += 6;
155                   
156                    $info_ofr_this_block['raw']['sample_type'] = getid3_lib::LittleEndian2Int($block_data{$offset++});
157                    $info_ofr_this_block['sample_type']        = $this->OptimFROGsampleTypeLookup($info_ofr_this_block['raw']['sample_type']);
158                   
159                    $info_ofr_this_block['channel_config']     = getid3_lib::LittleEndian2Int($block_data{$offset++});
160                    $info_ofr_this_block['channels']           = $info_ofr_this_block['channel_config'];
161
162                    $info_ofr_this_block['sample_rate']        = getid3_lib::LittleEndian2Int(substr($block_data, $offset, 4));
163                    $offset += 4;
164
165                    if ($block_size > 12) {
166
167                        // OFR 4.504b or higher
168                        $info_ofr_this_block['channels']           = $this->OptimFROGchannelConfigNumChannelsLookup($info_ofr_this_block['channel_config']);
169                        $info_ofr_this_block['raw']['encoder_id']  = getid3_lib::LittleEndian2Int(substr($block_data, $offset, 2));
170                        $info_ofr_this_block['encoder']            = $this->OptimFROGencoderNameLookup($info_ofr_this_block['raw']['encoder_id']);
171                        $offset += 2;
172
173                        $info_ofr_this_block['raw']['compression'] = getid3_lib::LittleEndian2Int($block_data{$offset++});
174                        $info_ofr_this_block['compression']        = $this->OptimFROGcompressionLookup($info_ofr_this_block['raw']['compression']);
175                        $info_ofr_this_block['speedup']            = $this->OptimFROGspeedupLookup($info_ofr_this_block['raw']['compression']);
176
177                        $getid3->info['audio']['encoder']         = 'OptimFROG '.$info_ofr_this_block['encoder'];
178                        $getid3->info['audio']['encoder_options'] = '--mode '.$info_ofr_this_block['compression'];
179
180                        if ((($info_ofr_this_block['raw']['encoder_id'] & 0xF0) >> 4) == 7) { // v4.507
181                            if (preg_match('/\.ofs$/i', $getid3->filename)) {
182                                // OptimFROG DualStream format is lossy, but as of v4.507 there is no way to tell the difference
183                                // between lossless and lossy other than the file extension.
184                                $getid3->info['audio']['dataformat']   = 'ofs';
185                                $getid3->info['audio']['lossless']     = true;
186                            }
187                        }
188                    }
189
190                    $getid3->info['audio']['channels']        = $info_ofr_this_block['channels'];
191                    $getid3->info['audio']['sample_rate']     = $info_ofr_this_block['sample_rate'];
192                    $getid3->info['audio']['bits_per_sample'] = $this->OptimFROGbitsPerSampleTypeLookup($info_ofr_this_block['raw']['sample_type']);
193                    break;
194
195
196                case 'COMP':
197                    // unlike other block types, there CAN be multiple COMP blocks
198
199                    $comp_data['offset'] = $block_offset;
200                    $comp_data['size']   = $block_size;
201
202                    if ($getid3->info['avdataoffset'] == 0) {
203                        $getid3->info['avdataoffset'] = $block_offset;
204                    }
205
206                    // Only interested in first 14 bytes (only first 12 needed for v4.50 alpha), not actual audio data
207                    $block_data .= fread($getid3->fp, 14);
208                    fseek($getid3->fp, $block_size - 14, SEEK_CUR);
209
210                    $comp_data['crc_32']                       = getid3_lib::LittleEndian2Int(substr($block_data, $offset, 4));
211                    $offset += 4;
212                   
213                    $comp_data['sample_count']                 = getid3_lib::LittleEndian2Int(substr($block_data, $offset, 4));
214                    $offset += 4;
215                   
216                    $comp_data['raw']['sample_type']           = getid3_lib::LittleEndian2Int($block_data{$offset++});
217                    $comp_data['sample_type']                  = $this->OptimFROGsampleTypeLookup($comp_data['raw']['sample_type']);
218                   
219                    $comp_data['raw']['channel_configuration'] = getid3_lib::LittleEndian2Int($block_data{$offset++});
220                    $comp_data['channel_configuration']        = $this->OptimFROGchannelConfigurationLookup($comp_data['raw']['channel_configuration']);
221
222                    $comp_data['raw']['algorithm_id']          = getid3_lib::LittleEndian2Int(substr($block_data, $offset, 2));
223                    $offset += 2;
224
225                    if ($getid3->info['ofr']['OFR ']['size'] > 12) {
226
227                        // OFR 4.504b or higher
228                        $comp_data['raw']['encoder_id']        = getid3_lib::LittleEndian2Int(substr($block_data, $offset, 2));
229                        $comp_data['encoder']                  = $this->OptimFROGencoderNameLookup($comp_data['raw']['encoder_id']);
230                        $offset += 2;
231                    }
232
233                    if ($comp_data['crc_32'] == 0x454E4F4E) {
234                        // ASCII value of 'NONE' - placeholder value in v4.50a
235                        $comp_data['crc_32'] = false;
236                    }
237
238                    $info_ofr_this_block[] = $comp_data;
239                    break;
240
241                case 'HEAD':
242                    $info_ofr_this_block['offset'] = $block_offset;
243                    $info_ofr_this_block['size']   = $block_size;
244
245                    $riff_data .= fread($getid3->fp, $block_size);
246                    break;
247
248                case 'TAIL':
249                    $info_ofr_this_block['offset'] = $block_offset;
250                    $info_ofr_this_block['size']   = $block_size;
251
252                    if ($block_size > 0) {
253                        $riff_data .= fread($getid3->fp, $block_size);
254                    }
255                    break;
256
257                case 'RECV':
258                    // block contains no useful meta data - simply note and skip
259
260                    $info_ofr_this_block['offset'] = $block_offset;
261                    $info_ofr_this_block['size']   = $block_size;
262
263                    fseek($getid3->fp, $block_size, SEEK_CUR);
264                    break;
265
266
267                case 'APET':
268                    // APEtag v2
269
270                    $info_ofr_this_block['offset'] = $block_offset;
271                    $info_ofr_this_block['size']   = $block_size;
272                    $getid3->warning('APEtag processing inside OptimFROG not supported in this version ('.GETID3_VERSION.') of getID3()');
273
274                    fseek($getid3->fp, $block_size, SEEK_CUR);
275                    break;
276
277
278                case 'MD5 ':
279                    // APEtag v2
280
281                    $info_ofr_this_block['offset'] = $block_offset;
282                    $info_ofr_this_block['size']   = $block_size;
283
284                    if ($block_size == 16) {
285
286                        $info_ofr_this_block['md5_binary'] = fread($getid3->fp, $block_size);
287                        $info_ofr_this_block['md5_string'] = getid3_lib::PrintHexBytes($info_ofr_this_block['md5_binary'], true, false, false);
288                        $getid3->info['md5_data_source']   = $info_ofr_this_block['md5_string'];
289
290                    } else {
291
292                        $getid3->warning('Expecting block size of 16 in "MD5 " chunk, found '.$block_size.' instead');
293                        fseek($getid3->fp, $block_size, SEEK_CUR);
294
295                    }
296                    break;
297
298
299                default:
300                    $info_ofr_this_block['offset'] = $block_offset;
301                    $info_ofr_this_block['size']   = $block_size;
302
303                    $getid3->warning('Unhandled OptimFROG block type "'.$block_name.'" at offset '.$info_ofr_this_block['offset']);
304                    fseek($getid3->fp, $block_size, SEEK_CUR);
305                    break;
306            }
307        }
308       
309        if (isset($getid3->info['ofr']['TAIL']['offset'])) {
310            $getid3->info['avdataend'] = $getid3->info['ofr']['TAIL']['offset'];
311        }
312
313        $getid3->info['playtime_seconds'] = (float)$getid3->info['ofr']['OFR ']['total_samples'] / ($getid3->info['audio']['channels'] * $getid3->info['audio']['sample_rate']);
314        $getid3->info['audio']['bitrate'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / $getid3->info['playtime_seconds'];
315
316        // move the data chunk after all other chunks (if any)
317        // so that the RIFF parser doesn't see EOF when trying
318        // to skip over the data chunk
319       
320        $riff_data = substr($riff_data, 0, 36).substr($riff_data, 44).substr($riff_data, 36, 8);
321       
322        // Save audio info key
323        $saved_info_audio = $getid3->info['audio'];
324
325        // Instantiate riff module and analyze string
326        $riff = new getid3_riff($getid3);
327        $riff->AnalyzeString($riff_data);
328       
329        // Restore info key
330        $getid3->info['audio'] = $saved_info_audio;
331       
332        $getid3->info['fileformat'] = 'ofr';
333
334        return true;
335    }
336
337
338
339    public static function OptimFROGsampleTypeLookup($sample_type) {
340       
341        static $lookup = array (
342            0  => 'unsigned int (8-bit)',
343            1  => 'signed int (8-bit)',
344            2  => 'unsigned int (16-bit)',
345            3  => 'signed int (16-bit)',
346            4  => 'unsigned int (24-bit)',
347            5  => 'signed int (24-bit)',
348            6  => 'unsigned int (32-bit)',
349            7  => 'signed int (32-bit)',
350            8  => 'float 0.24 (32-bit)',
351            9  => 'float 16.8 (32-bit)',
352            10 => 'float 24.0 (32-bit)'
353        );
354       
355        return @$lookup[$sample_type];
356    }
357
358
359
360    public static function OptimFROGbitsPerSampleTypeLookup($sample_type) {
361
362        static $lookup = array (
363            0  => 8,
364            1  => 8,
365            2  => 16,
366            3  => 16,
367            4  => 24,
368            5  => 24,
369            6  => 32,
370            7  => 32,
371            8  => 32,
372            9  => 32,
373            10 => 32
374        );
375       
376        return @$lookup[$sample_type];
377    }
378
379
380
381    public static function OptimFROGchannelConfigurationLookup($channel_configuration) {
382       
383        static $lookup = array (
384            0 => 'mono',
385            1 => 'stereo'
386        );
387       
388        return @$lookup[$channel_configuration];
389    }
390
391
392
393    public static function OptimFROGchannelConfigNumChannelsLookup($channel_configuration) {
394
395        static $lookup = array (
396            0 => 1,
397            1 => 2
398        );
399       
400        return @$lookup[$channel_configuration];
401    }
402
403
404
405    public static function OptimFROGencoderNameLookup($encoder_id) {
406
407        // version = (encoderID >> 4) + 4500
408        // system  =  encoderID & 0xF
409
410        $encoder_version   = number_format(((($encoder_id & 0xF0) >> 4) + 4500) / 1000, 3);
411        $encoder_system_id = ($encoder_id & 0x0F);
412
413        static $lookup = array (
414            0x00 => 'Windows console',
415            0x01 => 'Linux console',
416            0x0F => 'unknown'
417        );
418        return $encoder_version.' ('.(isset($lookup[$encoder_system_id]) ? $lookup[$encoder_system_id] : 'undefined encoder type (0x'.dechex($encoder_system_id).')').')';
419    }
420
421
422
423    public static function OptimFROGcompressionLookup($compression_id) {
424
425        // mode    = compression >> 3
426        // speedup = compression & 0x07
427
428        $compression_mode_id    = ($compression_id & 0xF8) >> 3;
429        //$compression_speed_up_id = ($compression_id & 0x07);
430
431        static $lookup = array (
432            0x00 => 'fast',
433            0x01 => 'normal',
434            0x02 => 'high',
435            0x03 => 'extra', // extranew (some versions)
436            0x04 => 'best',  // bestnew (some versions)
437            0x05 => 'ultra',
438            0x06 => 'insane',
439            0x07 => 'highnew',
440            0x08 => 'extranew',
441            0x09 => 'bestnew'
442        );
443        return (isset($lookup[$compression_mode_id]) ? $lookup[$compression_mode_id] : 'undefined mode (0x'.str_pad(dechex($compression_mode_id), 2, '0', STR_PAD_LEFT).')');
444    }
445
446
447
448    public static function OptimFROGspeedupLookup($compression_id) {
449
450        // mode    = compression >> 3
451        // speedup = compression & 0x07
452
453        //$compression_mode_id    = ($compression_id & 0xF8) >> 3;
454        $compression_speed_up_id = ($compression_id & 0x07);
455
456        static $lookup = array (
457            0x00 => '1x',
458            0x01 => '2x',
459            0x02 => '4x'
460        );
461
462        return (isset($lookup[$compression_speed_up_id]) ? $lookup[$compression_speed_up_id] : 'undefined mode (0x'.dechex($compression_speed_up_id));
463    }
464
465}
466
467
468?>
Note: See TracBrowser for help on using the repository browser.