[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.audio.shorten.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.shorten.php //
12 // module for analyzing Shorten Audio files //
13 // dependencies: NONE //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17
18 class getid3_shorten extends getid3_handler
19 {
20 /**
21 * @return bool
22 */
23 public function Analyze() {
24 $info = &$this->getid3->info;
25
26 $this->fseek($info['avdataoffset']);
27
28 $ShortenHeader = $this->fread(8);
29 $magic = 'ajkg';
30 if (substr($ShortenHeader, 0, 4) != $magic) {
31 $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($ShortenHeader, 0, 4)).'"');
32 return false;
33 }
34 $info['fileformat'] = 'shn';
35 $info['audio']['dataformat'] = 'shn';
36 $info['audio']['lossless'] = true;
37 $info['audio']['bitrate_mode'] = 'vbr';
38
39 $info['shn']['version'] = getid3_lib::LittleEndian2Int(substr($ShortenHeader, 4, 1));
40
41 $this->fseek($info['avdataend'] - 12);
42 $SeekTableSignatureTest = $this->fread(12);
43 $info['shn']['seektable']['present'] = substr($SeekTableSignatureTest, 4, 8) == 'SHNAMPSK';
44 if ($info['shn']['seektable']['present']) {
45 $info['shn']['seektable']['length'] = getid3_lib::LittleEndian2Int(substr($SeekTableSignatureTest, 0, 4));
46 $info['shn']['seektable']['offset'] = $info['avdataend'] - $info['shn']['seektable']['length'];
47 $this->fseek($info['shn']['seektable']['offset']);
48 $SeekTableMagic = $this->fread(4);
49 $magic = 'SEEK';
50 if ($SeekTableMagic != $magic) {
51
52 $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['shn']['seektable']['offset'].', found "'.getid3_lib::PrintHexBytes($SeekTableMagic).'"');
53 return false;
54
55 } else {
56
57 // typedef struct tag_TSeekEntry
58 // {
59 // unsigned long SampleNumber;
60 // unsigned long SHNFileByteOffset;
61 // unsigned long SHNLastBufferReadPosition;
62 // unsigned short SHNByteGet;
63 // unsigned short SHNBufferOffset;
64 // unsigned short SHNFileBitOffset;
65 // unsigned long SHNGBuffer;
66 // unsigned short SHNBitShift;
67 // long CBuf0[3];
68 // long CBuf1[3];
69 // long Offset0[4];
70 // long Offset1[4];
71 // }TSeekEntry;
72
73 $SeekTableData = $this->fread($info['shn']['seektable']['length'] - 16);
74 $info['shn']['seektable']['entry_count'] = floor(strlen($SeekTableData) / 80);
75 //$info['shn']['seektable']['entries'] = array();
76 //$SeekTableOffset = 0;
77 //for ($i = 0; $i < $info['shn']['seektable']['entry_count']; $i++) {
78 // $SeekTableEntry['sample_number'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
79 // $SeekTableOffset += 4;
80 // $SeekTableEntry['shn_file_byte_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
81 // $SeekTableOffset += 4;
82 // $SeekTableEntry['shn_last_buffer_read_position'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
83 // $SeekTableOffset += 4;
84 // $SeekTableEntry['shn_byte_get'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
85 // $SeekTableOffset += 2;
86 // $SeekTableEntry['shn_buffer_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
87 // $SeekTableOffset += 2;
88 // $SeekTableEntry['shn_file_bit_offset'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
89 // $SeekTableOffset += 2;
90 // $SeekTableEntry['shn_gbuffer'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
91 // $SeekTableOffset += 4;
92 // $SeekTableEntry['shn_bit_shift'] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 2));
93 // $SeekTableOffset += 2;
94 // for ($j = 0; $j < 3; $j++) {
95 // $SeekTableEntry['cbuf0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
96 // $SeekTableOffset += 4;
97 // }
98 // for ($j = 0; $j < 3; $j++) {
99 // $SeekTableEntry['cbuf1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
100 // $SeekTableOffset += 4;
101 // }
102 // for ($j = 0; $j < 4; $j++) {
103 // $SeekTableEntry['offset0'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
104 // $SeekTableOffset += 4;
105 // }
106 // for ($j = 0; $j < 4; $j++) {
107 // $SeekTableEntry['offset1'][$j] = getid3_lib::LittleEndian2Int(substr($SeekTableData, $SeekTableOffset, 4));
108 // $SeekTableOffset += 4;
109 // }
110 //
111 // $info['shn']['seektable']['entries'][] = $SeekTableEntry;
112 //}
113
114 }
115
116 }
117
118 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
119 $this->error('PHP running in Safe Mode - backtick operator not available, cannot run shntool to analyze Shorten files');
120 return false;
121 }
122
123 if (GETID3_OS_ISWINDOWS) {
124
125 $RequiredFiles = array('shorten.exe', 'cygwin1.dll', 'head.exe');
126 foreach ($RequiredFiles as $required_file) {
127 if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
128 $this->error(GETID3_HELPERAPPSDIR.$required_file.' does not exist');
129 return false;
130 }
131 }
132 $commandline = GETID3_HELPERAPPSDIR.'shorten.exe -x "'.$info['filenamepath'].'" - | '.GETID3_HELPERAPPSDIR.'head.exe -c 64';
133 $commandline = str_replace('/', '\\', $commandline);
134
135 } else {
136
137 static $shorten_present;
138 if (!isset($shorten_present)) {
139 $shorten_present = file_exists('/usr/local/bin/shorten') || `which shorten`;
140 }
141 if (!$shorten_present) {
142 $this->error('shorten binary was not found in path or /usr/local/bin');
143 return false;
144 }
145 $commandline = (file_exists('/usr/local/bin/shorten') ? '/usr/local/bin/' : '' ) . 'shorten -x '.escapeshellarg($info['filenamepath']).' - | head -c 64';
146
147 }
148
149 $output = `$commandline`;
150
151 if (!empty($output) && (substr($output, 12, 4) == 'fmt ')) {
152
153 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
154
155 $fmt_size = getid3_lib::LittleEndian2Int(substr($output, 16, 4));
156 $DecodedWAVFORMATEX = getid3_riff::parseWAVEFORMATex(substr($output, 20, $fmt_size));
157 $info['audio']['channels'] = $DecodedWAVFORMATEX['channels'];
158 $info['audio']['bits_per_sample'] = $DecodedWAVFORMATEX['bits_per_sample'];
159 $info['audio']['sample_rate'] = $DecodedWAVFORMATEX['sample_rate'];
160
161 if (substr($output, 20 + $fmt_size, 4) == 'data') {
162
163 $info['playtime_seconds'] = getid3_lib::LittleEndian2Int(substr($output, 20 + 4 + $fmt_size, 4)) / $DecodedWAVFORMATEX['raw']['nAvgBytesPerSec'];
164
165 } else {
166
167 $this->error('shorten failed to decode DATA chunk to expected location, cannot determine playtime');
168 return false;
169
170 }
171
172 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) / $info['playtime_seconds']) * 8;
173
174 } else {
175
176 $this->error('shorten failed to decode file to WAV for parsing');
177 return false;
178
179 }
180
181 return true;
182 }
183
184 }