[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.audio.lpac.php
1 <?php
2
3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
10 // //
11 // module.audio.lpac.php //
12 // module for analyzing LPAC Audio files //
13 // dependencies: module.audio-video.riff.php //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
18
19 class getid3_lpac extends getid3_handler
20 {
21 /**
22 * @return bool
23 */
24 public function Analyze() {
25 $info = &$this->getid3->info;
26
27 $this->fseek($info['avdataoffset']);
28 $LPACheader = $this->fread(14);
29 $StreamMarker = substr($LPACheader, 0, 4);
30 if ($StreamMarker != 'LPAC') {
31 $this->error('Expected "LPAC" at offset '.$info['avdataoffset'].', found "'.$StreamMarker.'"');
32 return false;
33 }
34 $info['avdataoffset'] += 14;
35
36 $info['fileformat'] = 'lpac';
37 $info['audio']['dataformat'] = 'lpac';
38 $info['audio']['lossless'] = true;
39 $info['audio']['bitrate_mode'] = 'vbr';
40
41 $info['lpac']['file_version'] = getid3_lib::BigEndian2Int(substr($LPACheader, 4, 1));
42 $flags['audio_type'] = getid3_lib::BigEndian2Int(substr($LPACheader, 5, 1));
43 $info['lpac']['total_samples']= getid3_lib::BigEndian2Int(substr($LPACheader, 6, 4));
44 $flags['parameters'] = getid3_lib::BigEndian2Int(substr($LPACheader, 10, 4));
45
46 $info['lpac']['flags']['is_wave'] = (bool) ($flags['audio_type'] & 0x40);
47 $info['lpac']['flags']['stereo'] = (bool) ($flags['audio_type'] & 0x04);
48 $info['lpac']['flags']['24_bit'] = (bool) ($flags['audio_type'] & 0x02);
49 $info['lpac']['flags']['16_bit'] = (bool) ($flags['audio_type'] & 0x01);
50
51 if ($info['lpac']['flags']['24_bit'] && $info['lpac']['flags']['16_bit']) {
52 $this->warning('24-bit and 16-bit flags cannot both be set');
53 }
54
55 $info['lpac']['flags']['fast_compress'] = (bool) ($flags['parameters'] & 0x40000000);
56 $info['lpac']['flags']['random_access'] = (bool) ($flags['parameters'] & 0x08000000);
57 $info['lpac']['block_length'] = pow(2, (($flags['parameters'] & 0x07000000) >> 24)) * 256;
58 $info['lpac']['flags']['adaptive_prediction_order'] = (bool) ($flags['parameters'] & 0x00800000);
59 $info['lpac']['flags']['adaptive_quantization'] = (bool) ($flags['parameters'] & 0x00400000);
60 $info['lpac']['flags']['joint_stereo'] = (bool) ($flags['parameters'] & 0x00040000);
61 $info['lpac']['quantization'] = ($flags['parameters'] & 0x00001F00) >> 8;
62 $info['lpac']['max_prediction_order'] = ($flags['parameters'] & 0x0000003F);
63
64 if ($info['lpac']['flags']['fast_compress'] && ($info['lpac']['max_prediction_order'] != 3)) {
65 $this->warning('max_prediction_order expected to be "3" if fast_compress is true, actual value is "'.$info['lpac']['max_prediction_order'].'"');
66 }
67 switch ($info['lpac']['file_version']) {
68 case 6:
69 if ($info['lpac']['flags']['adaptive_quantization']) {
70 $this->warning('adaptive_quantization expected to be false in LPAC file stucture v6, actually true');
71 }
72 if ($info['lpac']['quantization'] != 20) {
73 $this->warning('Quantization expected to be 20 in LPAC file stucture v6, actually '.$info['lpac']['flags']['Q']);
74 }
75 break;
76
77 default:
78 //$this->warning('This version of getID3() ['.$this->getid3->version().'] only supports LPAC file format version 6, this file is version '.$info['lpac']['file_version'].' - please report to info@getid3.org');
79 break;
80 }
81
82 $getid3_temp = new getID3();
83 $getid3_temp->openfile($this->getid3->filename);
84 $getid3_temp->info = $info;
85 $getid3_riff = new getid3_riff($getid3_temp);
86 $getid3_riff->Analyze();
87 $info['avdataoffset'] = $getid3_temp->info['avdataoffset'];
88 $info['riff'] = $getid3_temp->info['riff'];
89 $info['error'] = $getid3_temp->info['error'];
90 $info['warning'] = $getid3_temp->info['warning'];
91 $info['lpac']['comments']['comment'] = $getid3_temp->info['comments'];
92 $info['audio']['sample_rate'] = $getid3_temp->info['audio']['sample_rate'];
93 unset($getid3_temp, $getid3_riff);
94
95 $info['audio']['channels'] = ($info['lpac']['flags']['stereo'] ? 2 : 1);
96
97 if ($info['lpac']['flags']['24_bit']) {
98 $info['audio']['bits_per_sample'] = $info['riff']['audio'][0]['bits_per_sample'];
99 } elseif ($info['lpac']['flags']['16_bit']) {
100 $info['audio']['bits_per_sample'] = 16;
101 } else {
102 $info['audio']['bits_per_sample'] = 8;
103 }
104
105 if ($info['lpac']['flags']['fast_compress']) {
106 // fast
107 $info['audio']['encoder_options'] = '-1';
108 } else {
109 switch ($info['lpac']['max_prediction_order']) {
110 case 20: // simple
111 $info['audio']['encoder_options'] = '-2';
112 break;
113 case 30: // medium
114 $info['audio']['encoder_options'] = '-3';
115 break;
116 case 40: // high
117 $info['audio']['encoder_options'] = '-4';
118 break;
119 case 60: // extrahigh
120 $info['audio']['encoder_options'] = '-5';
121 break;
122 }
123 }
124
125 $info['playtime_seconds'] = $info['lpac']['total_samples'] / $info['audio']['sample_rate'];
126 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
127
128 return true;
129 }
130
131 }