author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
///////////////////////////////////////////////////////////////// |
|
3 |
/// getID3() by James Heinrich <info@getid3.org> // |
|
16 | 4 |
// available at https://github.com/JamesHeinrich/getID3 // |
5 |
// or https://www.getid3.org // |
|
6 |
// or http://getid3.sourceforge.net // |
|
7 |
// see readme.txt for more details // |
|
8 |
///////////////////////////////////////////////////////////////// |
|
9 |
// // |
|
10 |
// module.audio-video.flv.php // |
|
11 |
// module for analyzing Shockwave Flash Video files // |
|
12 |
// dependencies: NONE // |
|
13 |
// // |
|
14 |
///////////////////////////////////////////////////////////////// |
|
0 | 15 |
// // |
16 |
// FLV module by Seth Kaufman <sethØwhirl-i-gig*com> // |
|
17 |
// // |
|
18 |
// * version 0.1 (26 June 2005) // |
|
19 |
// // |
|
20 |
// * version 0.1.1 (15 July 2005) // |
|
21 |
// minor modifications by James Heinrich <info@getid3.org> // |
|
22 |
// // |
|
23 |
// * version 0.2 (22 February 2006) // |
|
24 |
// Support for On2 VP6 codec and meta information // |
|
25 |
// by Steve Webster <steve.websterØfeaturecreep*com> // |
|
26 |
// // |
|
27 |
// * version 0.3 (15 June 2006) // |
|
28 |
// Modified to not read entire file into memory // |
|
29 |
// by James Heinrich <info@getid3.org> // |
|
30 |
// // |
|
31 |
// * version 0.4 (07 December 2007) // |
|
32 |
// Bugfixes for incorrectly parsed FLV dimensions // |
|
33 |
// and incorrect parsing of onMetaTag // |
|
34 |
// by Evgeny Moysevich <moysevichØgmail*com> // |
|
35 |
// // |
|
36 |
// * version 0.5 (21 May 2009) // |
|
37 |
// Fixed parsing of audio tags and added additional codec // |
|
38 |
// details. The duration is now read from onMetaTag (if // |
|
39 |
// exists), rather than parsing whole file // |
|
40 |
// by Nigel Barnes <ngbarnesØhotmail*com> // |
|
41 |
// // |
|
42 |
// * version 0.6 (24 May 2009) // |
|
43 |
// Better parsing of files with h264 video // |
|
44 |
// by Evgeny Moysevich <moysevichØgmail*com> // |
|
45 |
// // |
|
46 |
// * version 0.6.1 (30 May 2011) // |
|
47 |
// prevent infinite loops in expGolombUe() // |
|
48 |
// // |
|
5 | 49 |
// * version 0.7.0 (16 Jul 2013) // |
50 |
// handle GETID3_FLV_VIDEO_VP6FLV_ALPHA // |
|
51 |
// improved AVCSequenceParameterSetReader::readData() // |
|
52 |
// by Xander Schouwerwou <schouwerwouØgmail*com> // |
|
0 | 53 |
// /// |
54 |
///////////////////////////////////////////////////////////////// |
|
55 |
||
16 | 56 |
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers |
57 |
exit; |
|
58 |
} |
|
59 |
||
0 | 60 |
define('GETID3_FLV_TAG_AUDIO', 8); |
61 |
define('GETID3_FLV_TAG_VIDEO', 9); |
|
62 |
define('GETID3_FLV_TAG_META', 18); |
|
63 |
||
64 |
define('GETID3_FLV_VIDEO_H263', 2); |
|
65 |
define('GETID3_FLV_VIDEO_SCREEN', 3); |
|
66 |
define('GETID3_FLV_VIDEO_VP6FLV', 4); |
|
67 |
define('GETID3_FLV_VIDEO_VP6FLV_ALPHA', 5); |
|
68 |
define('GETID3_FLV_VIDEO_SCREENV2', 6); |
|
69 |
define('GETID3_FLV_VIDEO_H264', 7); |
|
70 |
||
71 |
define('H264_AVC_SEQUENCE_HEADER', 0); |
|
72 |
define('H264_PROFILE_BASELINE', 66); |
|
73 |
define('H264_PROFILE_MAIN', 77); |
|
74 |
define('H264_PROFILE_EXTENDED', 88); |
|
75 |
define('H264_PROFILE_HIGH', 100); |
|
76 |
define('H264_PROFILE_HIGH10', 110); |
|
77 |
define('H264_PROFILE_HIGH422', 122); |
|
78 |
define('H264_PROFILE_HIGH444', 144); |
|
79 |
define('H264_PROFILE_HIGH444_PREDICTIVE', 244); |
|
80 |
||
16 | 81 |
class getid3_flv extends getid3_handler |
82 |
{ |
|
5 | 83 |
const magic = 'FLV'; |
84 |
||
16 | 85 |
/** |
86 |
* Break out of the loop if too many frames have been scanned; only scan this |
|
87 |
* many if meta frame does not contain useful duration. |
|
88 |
* |
|
89 |
* @var int |
|
90 |
*/ |
|
91 |
public $max_frames = 100000; |
|
0 | 92 |
|
16 | 93 |
/** |
94 |
* @return bool |
|
95 |
*/ |
|
0 | 96 |
public function Analyze() { |
97 |
$info = &$this->getid3->info; |
|
98 |
||
5 | 99 |
$this->fseek($info['avdataoffset']); |
0 | 100 |
|
101 |
$FLVdataLength = $info['avdataend'] - $info['avdataoffset']; |
|
5 | 102 |
$FLVheader = $this->fread(5); |
0 | 103 |
|
104 |
$info['fileformat'] = 'flv'; |
|
105 |
$info['flv']['header']['signature'] = substr($FLVheader, 0, 3); |
|
106 |
$info['flv']['header']['version'] = getid3_lib::BigEndian2Int(substr($FLVheader, 3, 1)); |
|
107 |
$TypeFlags = getid3_lib::BigEndian2Int(substr($FLVheader, 4, 1)); |
|
108 |
||
5 | 109 |
if ($info['flv']['header']['signature'] != self::magic) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
$this->error('Expecting "'.getid3_lib::PrintHexBytes(self::magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['flv']['header']['signature']).'"'); |
5 | 111 |
unset($info['flv'], $info['fileformat']); |
0 | 112 |
return false; |
113 |
} |
|
114 |
||
115 |
$info['flv']['header']['hasAudio'] = (bool) ($TypeFlags & 0x04); |
|
116 |
$info['flv']['header']['hasVideo'] = (bool) ($TypeFlags & 0x01); |
|
117 |
||
5 | 118 |
$FrameSizeDataLength = getid3_lib::BigEndian2Int($this->fread(4)); |
0 | 119 |
$FLVheaderFrameLength = 9; |
120 |
if ($FrameSizeDataLength > $FLVheaderFrameLength) { |
|
5 | 121 |
$this->fseek($FrameSizeDataLength - $FLVheaderFrameLength, SEEK_CUR); |
0 | 122 |
} |
123 |
$Duration = 0; |
|
124 |
$found_video = false; |
|
125 |
$found_audio = false; |
|
126 |
$found_meta = false; |
|
127 |
$found_valid_meta_playtime = false; |
|
128 |
$tagParseCount = 0; |
|
129 |
$info['flv']['framecount'] = array('total'=>0, 'audio'=>0, 'video'=>0); |
|
130 |
$flv_framecount = &$info['flv']['framecount']; |
|
5 | 131 |
while ((($this->ftell() + 16) < $info['avdataend']) && (($tagParseCount++ <= $this->max_frames) || !$found_valid_meta_playtime)) { |
132 |
$ThisTagHeader = $this->fread(16); |
|
0 | 133 |
|
134 |
$PreviousTagLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 0, 4)); |
|
135 |
$TagType = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 4, 1)); |
|
136 |
$DataLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 5, 3)); |
|
137 |
$Timestamp = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 8, 3)); |
|
138 |
$LastHeaderByte = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 15, 1)); |
|
5 | 139 |
$NextOffset = $this->ftell() - 1 + $DataLength; |
0 | 140 |
if ($Timestamp > $Duration) { |
141 |
$Duration = $Timestamp; |
|
142 |
} |
|
143 |
||
144 |
$flv_framecount['total']++; |
|
145 |
switch ($TagType) { |
|
146 |
case GETID3_FLV_TAG_AUDIO: |
|
147 |
$flv_framecount['audio']++; |
|
148 |
if (!$found_audio) { |
|
149 |
$found_audio = true; |
|
150 |
$info['flv']['audio']['audioFormat'] = ($LastHeaderByte >> 4) & 0x0F; |
|
151 |
$info['flv']['audio']['audioRate'] = ($LastHeaderByte >> 2) & 0x03; |
|
152 |
$info['flv']['audio']['audioSampleSize'] = ($LastHeaderByte >> 1) & 0x01; |
|
153 |
$info['flv']['audio']['audioType'] = $LastHeaderByte & 0x01; |
|
154 |
} |
|
155 |
break; |
|
156 |
||
157 |
case GETID3_FLV_TAG_VIDEO: |
|
158 |
$flv_framecount['video']++; |
|
159 |
if (!$found_video) { |
|
160 |
$found_video = true; |
|
161 |
$info['flv']['video']['videoCodec'] = $LastHeaderByte & 0x07; |
|
162 |
||
5 | 163 |
$FLVvideoHeader = $this->fread(11); |
19 | 164 |
$PictureSizeEnc = array(); |
0 | 165 |
|
166 |
if ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H264) { |
|
167 |
// this code block contributed by: moysevichØgmail*com |
|
168 |
||
169 |
$AVCPacketType = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 0, 1)); |
|
170 |
if ($AVCPacketType == H264_AVC_SEQUENCE_HEADER) { |
|
171 |
// read AVCDecoderConfigurationRecord |
|
172 |
$configurationVersion = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 1)); |
|
173 |
$AVCProfileIndication = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 1)); |
|
174 |
$profile_compatibility = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 1)); |
|
175 |
$lengthSizeMinusOne = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 1)); |
|
176 |
$numOfSequenceParameterSets = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 8, 1)); |
|
177 |
||
178 |
if (($numOfSequenceParameterSets & 0x1F) != 0) { |
|
179 |
// there is at least one SequenceParameterSet |
|
180 |
// read size of the first SequenceParameterSet |
|
181 |
//$spsSize = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 9, 2)); |
|
182 |
$spsSize = getid3_lib::LittleEndian2Int(substr($FLVvideoHeader, 9, 2)); |
|
183 |
// read the first SequenceParameterSet |
|
5 | 184 |
$sps = $this->fread($spsSize); |
0 | 185 |
if (strlen($sps) == $spsSize) { // make sure that whole SequenceParameterSet was red |
186 |
$spsReader = new AVCSequenceParameterSetReader($sps); |
|
187 |
$spsReader->readData(); |
|
188 |
$info['video']['resolution_x'] = $spsReader->getWidth(); |
|
189 |
$info['video']['resolution_y'] = $spsReader->getHeight(); |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
// end: moysevichØgmail*com |
|
194 |
||
195 |
} elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H263) { |
|
196 |
||
197 |
$PictureSizeType = (getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 3, 2))) >> 7; |
|
198 |
$PictureSizeType = $PictureSizeType & 0x0007; |
|
199 |
$info['flv']['header']['videoSizeType'] = $PictureSizeType; |
|
200 |
switch ($PictureSizeType) { |
|
201 |
case 0: |
|
202 |
//$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2)); |
|
203 |
//$PictureSizeEnc <<= 1; |
|
204 |
//$info['video']['resolution_x'] = ($PictureSizeEnc & 0xFF00) >> 8; |
|
205 |
//$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2)); |
|
206 |
//$PictureSizeEnc <<= 1; |
|
207 |
//$info['video']['resolution_y'] = ($PictureSizeEnc & 0xFF00) >> 8; |
|
208 |
||
5 | 209 |
$PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 2)) >> 7; |
210 |
$PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2)) >> 7; |
|
0 | 211 |
$info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFF; |
212 |
$info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFF; |
|
213 |
break; |
|
214 |
||
215 |
case 1: |
|
5 | 216 |
$PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 3)) >> 7; |
217 |
$PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 3)) >> 7; |
|
0 | 218 |
$info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFFFF; |
219 |
$info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFFFF; |
|
220 |
break; |
|
221 |
||
222 |
case 2: |
|
223 |
$info['video']['resolution_x'] = 352; |
|
224 |
$info['video']['resolution_y'] = 288; |
|
225 |
break; |
|
226 |
||
227 |
case 3: |
|
228 |
$info['video']['resolution_x'] = 176; |
|
229 |
$info['video']['resolution_y'] = 144; |
|
230 |
break; |
|
231 |
||
232 |
case 4: |
|
233 |
$info['video']['resolution_x'] = 128; |
|
234 |
$info['video']['resolution_y'] = 96; |
|
235 |
break; |
|
236 |
||
237 |
case 5: |
|
238 |
$info['video']['resolution_x'] = 320; |
|
239 |
$info['video']['resolution_y'] = 240; |
|
240 |
break; |
|
241 |
||
242 |
case 6: |
|
243 |
$info['video']['resolution_x'] = 160; |
|
244 |
$info['video']['resolution_y'] = 120; |
|
245 |
break; |
|
246 |
||
247 |
default: |
|
248 |
$info['video']['resolution_x'] = 0; |
|
249 |
$info['video']['resolution_y'] = 0; |
|
250 |
break; |
|
251 |
||
252 |
} |
|
5 | 253 |
|
254 |
} elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_VP6FLV_ALPHA) { |
|
255 |
||
256 |
/* contributed by schouwerwouØgmail*com */ |
|
257 |
if (!isset($info['video']['resolution_x'])) { // only when meta data isn't set |
|
258 |
$PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2)); |
|
259 |
$PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 2)); |
|
260 |
$info['video']['resolution_x'] = ($PictureSizeEnc['x'] & 0xFF) << 3; |
|
261 |
$info['video']['resolution_y'] = ($PictureSizeEnc['y'] & 0xFF) << 3; |
|
262 |
} |
|
263 |
/* end schouwerwouØgmail*com */ |
|
264 |
||
0 | 265 |
} |
5 | 266 |
if (!empty($info['video']['resolution_x']) && !empty($info['video']['resolution_y'])) { |
267 |
$info['video']['pixel_aspect_ratio'] = $info['video']['resolution_x'] / $info['video']['resolution_y']; |
|
268 |
} |
|
0 | 269 |
} |
270 |
break; |
|
271 |
||
272 |
// Meta tag |
|
273 |
case GETID3_FLV_TAG_META: |
|
274 |
if (!$found_meta) { |
|
275 |
$found_meta = true; |
|
5 | 276 |
$this->fseek(-1, SEEK_CUR); |
277 |
$datachunk = $this->fread($DataLength); |
|
0 | 278 |
$AMFstream = new AMFStream($datachunk); |
279 |
$reader = new AMFReader($AMFstream); |
|
280 |
$eventName = $reader->readData(); |
|
281 |
$info['flv']['meta'][$eventName] = $reader->readData(); |
|
282 |
unset($reader); |
|
283 |
||
284 |
$copykeys = array('framerate'=>'frame_rate', 'width'=>'resolution_x', 'height'=>'resolution_y', 'audiodatarate'=>'bitrate', 'videodatarate'=>'bitrate'); |
|
285 |
foreach ($copykeys as $sourcekey => $destkey) { |
|
286 |
if (isset($info['flv']['meta']['onMetaData'][$sourcekey])) { |
|
287 |
switch ($sourcekey) { |
|
288 |
case 'width': |
|
289 |
case 'height': |
|
290 |
$info['video'][$destkey] = intval(round($info['flv']['meta']['onMetaData'][$sourcekey])); |
|
291 |
break; |
|
292 |
case 'audiodatarate': |
|
293 |
$info['audio'][$destkey] = getid3_lib::CastAsInt(round($info['flv']['meta']['onMetaData'][$sourcekey] * 1000)); |
|
294 |
break; |
|
295 |
case 'videodatarate': |
|
296 |
case 'frame_rate': |
|
297 |
default: |
|
298 |
$info['video'][$destkey] = $info['flv']['meta']['onMetaData'][$sourcekey]; |
|
299 |
break; |
|
300 |
} |
|
301 |
} |
|
302 |
} |
|
303 |
if (!empty($info['flv']['meta']['onMetaData']['duration'])) { |
|
304 |
$found_valid_meta_playtime = true; |
|
305 |
} |
|
306 |
} |
|
307 |
break; |
|
308 |
||
309 |
default: |
|
310 |
// noop |
|
311 |
break; |
|
312 |
} |
|
5 | 313 |
$this->fseek($NextOffset); |
0 | 314 |
} |
315 |
||
316 |
$info['playtime_seconds'] = $Duration / 1000; |
|
317 |
if ($info['playtime_seconds'] > 0) { |
|
318 |
$info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds']; |
|
319 |
} |
|
320 |
||
321 |
if ($info['flv']['header']['hasAudio']) { |
|
5 | 322 |
$info['audio']['codec'] = self::audioFormatLookup($info['flv']['audio']['audioFormat']); |
323 |
$info['audio']['sample_rate'] = self::audioRateLookup($info['flv']['audio']['audioRate']); |
|
324 |
$info['audio']['bits_per_sample'] = self::audioBitDepthLookup($info['flv']['audio']['audioSampleSize']); |
|
0 | 325 |
|
326 |
$info['audio']['channels'] = $info['flv']['audio']['audioType'] + 1; // 0=mono,1=stereo |
|
327 |
$info['audio']['lossless'] = ($info['flv']['audio']['audioFormat'] ? false : true); // 0=uncompressed |
|
328 |
$info['audio']['dataformat'] = 'flv'; |
|
329 |
} |
|
330 |
if (!empty($info['flv']['header']['hasVideo'])) { |
|
5 | 331 |
$info['video']['codec'] = self::videoCodecLookup($info['flv']['video']['videoCodec']); |
0 | 332 |
$info['video']['dataformat'] = 'flv'; |
333 |
$info['video']['lossless'] = false; |
|
334 |
} |
|
335 |
||
336 |
// Set information from meta |
|
337 |
if (!empty($info['flv']['meta']['onMetaData']['duration'])) { |
|
338 |
$info['playtime_seconds'] = $info['flv']['meta']['onMetaData']['duration']; |
|
339 |
$info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds']; |
|
340 |
} |
|
341 |
if (isset($info['flv']['meta']['onMetaData']['audiocodecid'])) { |
|
5 | 342 |
$info['audio']['codec'] = self::audioFormatLookup($info['flv']['meta']['onMetaData']['audiocodecid']); |
0 | 343 |
} |
344 |
if (isset($info['flv']['meta']['onMetaData']['videocodecid'])) { |
|
5 | 345 |
$info['video']['codec'] = self::videoCodecLookup($info['flv']['meta']['onMetaData']['videocodecid']); |
0 | 346 |
} |
347 |
return true; |
|
348 |
} |
|
349 |
||
16 | 350 |
/** |
351 |
* @param int $id |
|
352 |
* |
|
353 |
* @return string|false |
|
354 |
*/ |
|
5 | 355 |
public static function audioFormatLookup($id) { |
356 |
static $lookup = array( |
|
0 | 357 |
0 => 'Linear PCM, platform endian', |
358 |
1 => 'ADPCM', |
|
359 |
2 => 'mp3', |
|
360 |
3 => 'Linear PCM, little endian', |
|
361 |
4 => 'Nellymoser 16kHz mono', |
|
362 |
5 => 'Nellymoser 8kHz mono', |
|
363 |
6 => 'Nellymoser', |
|
364 |
7 => 'G.711A-law logarithmic PCM', |
|
365 |
8 => 'G.711 mu-law logarithmic PCM', |
|
366 |
9 => 'reserved', |
|
367 |
10 => 'AAC', |
|
5 | 368 |
11 => 'Speex', |
0 | 369 |
12 => false, // unknown? |
370 |
13 => false, // unknown? |
|
371 |
14 => 'mp3 8kHz', |
|
372 |
15 => 'Device-specific sound', |
|
373 |
); |
|
5 | 374 |
return (isset($lookup[$id]) ? $lookup[$id] : false); |
0 | 375 |
} |
376 |
||
16 | 377 |
/** |
378 |
* @param int $id |
|
379 |
* |
|
380 |
* @return int|false |
|
381 |
*/ |
|
5 | 382 |
public static function audioRateLookup($id) { |
383 |
static $lookup = array( |
|
0 | 384 |
0 => 5500, |
385 |
1 => 11025, |
|
386 |
2 => 22050, |
|
387 |
3 => 44100, |
|
388 |
); |
|
5 | 389 |
return (isset($lookup[$id]) ? $lookup[$id] : false); |
0 | 390 |
} |
391 |
||
16 | 392 |
/** |
393 |
* @param int $id |
|
394 |
* |
|
395 |
* @return int|false |
|
396 |
*/ |
|
5 | 397 |
public static function audioBitDepthLookup($id) { |
398 |
static $lookup = array( |
|
0 | 399 |
0 => 8, |
400 |
1 => 16, |
|
401 |
); |
|
5 | 402 |
return (isset($lookup[$id]) ? $lookup[$id] : false); |
0 | 403 |
} |
404 |
||
16 | 405 |
/** |
406 |
* @param int $id |
|
407 |
* |
|
408 |
* @return string|false |
|
409 |
*/ |
|
5 | 410 |
public static function videoCodecLookup($id) { |
411 |
static $lookup = array( |
|
0 | 412 |
GETID3_FLV_VIDEO_H263 => 'Sorenson H.263', |
413 |
GETID3_FLV_VIDEO_SCREEN => 'Screen video', |
|
414 |
GETID3_FLV_VIDEO_VP6FLV => 'On2 VP6', |
|
415 |
GETID3_FLV_VIDEO_VP6FLV_ALPHA => 'On2 VP6 with alpha channel', |
|
416 |
GETID3_FLV_VIDEO_SCREENV2 => 'Screen video v2', |
|
417 |
GETID3_FLV_VIDEO_H264 => 'Sorenson H.264', |
|
418 |
); |
|
5 | 419 |
return (isset($lookup[$id]) ? $lookup[$id] : false); |
0 | 420 |
} |
421 |
} |
|
422 |
||
16 | 423 |
class AMFStream |
424 |
{ |
|
425 |
/** |
|
426 |
* @var string |
|
427 |
*/ |
|
0 | 428 |
public $bytes; |
16 | 429 |
|
430 |
/** |
|
431 |
* @var int |
|
432 |
*/ |
|
0 | 433 |
public $pos; |
434 |
||
16 | 435 |
/** |
436 |
* @param string $bytes |
|
437 |
*/ |
|
5 | 438 |
public function __construct(&$bytes) { |
0 | 439 |
$this->bytes =& $bytes; |
440 |
$this->pos = 0; |
|
441 |
} |
|
442 |
||
16 | 443 |
/** |
444 |
* @return int |
|
445 |
*/ |
|
446 |
public function readByte() { // 8-bit |
|
447 |
return ord(substr($this->bytes, $this->pos++, 1)); |
|
0 | 448 |
} |
449 |
||
16 | 450 |
/** |
451 |
* @return int |
|
452 |
*/ |
|
453 |
public function readInt() { // 16-bit |
|
0 | 454 |
return ($this->readByte() << 8) + $this->readByte(); |
455 |
} |
|
456 |
||
16 | 457 |
/** |
458 |
* @return int |
|
459 |
*/ |
|
460 |
public function readLong() { // 32-bit |
|
0 | 461 |
return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte(); |
462 |
} |
|
463 |
||
16 | 464 |
/** |
465 |
* @return float|false |
|
466 |
*/ |
|
0 | 467 |
public function readDouble() { |
468 |
return getid3_lib::BigEndian2Float($this->read(8)); |
|
469 |
} |
|
470 |
||
16 | 471 |
/** |
472 |
* @return string |
|
473 |
*/ |
|
0 | 474 |
public function readUTF() { |
475 |
$length = $this->readInt(); |
|
476 |
return $this->read($length); |
|
477 |
} |
|
478 |
||
16 | 479 |
/** |
480 |
* @return string |
|
481 |
*/ |
|
0 | 482 |
public function readLongUTF() { |
483 |
$length = $this->readLong(); |
|
484 |
return $this->read($length); |
|
485 |
} |
|
486 |
||
16 | 487 |
/** |
488 |
* @param int $length |
|
489 |
* |
|
490 |
* @return string |
|
491 |
*/ |
|
0 | 492 |
public function read($length) { |
493 |
$val = substr($this->bytes, $this->pos, $length); |
|
494 |
$this->pos += $length; |
|
495 |
return $val; |
|
496 |
} |
|
497 |
||
16 | 498 |
/** |
499 |
* @return int |
|
500 |
*/ |
|
0 | 501 |
public function peekByte() { |
502 |
$pos = $this->pos; |
|
503 |
$val = $this->readByte(); |
|
504 |
$this->pos = $pos; |
|
505 |
return $val; |
|
506 |
} |
|
507 |
||
16 | 508 |
/** |
509 |
* @return int |
|
510 |
*/ |
|
0 | 511 |
public function peekInt() { |
512 |
$pos = $this->pos; |
|
513 |
$val = $this->readInt(); |
|
514 |
$this->pos = $pos; |
|
515 |
return $val; |
|
516 |
} |
|
517 |
||
16 | 518 |
/** |
519 |
* @return int |
|
520 |
*/ |
|
0 | 521 |
public function peekLong() { |
522 |
$pos = $this->pos; |
|
523 |
$val = $this->readLong(); |
|
524 |
$this->pos = $pos; |
|
525 |
return $val; |
|
526 |
} |
|
527 |
||
16 | 528 |
/** |
529 |
* @return float|false |
|
530 |
*/ |
|
0 | 531 |
public function peekDouble() { |
532 |
$pos = $this->pos; |
|
533 |
$val = $this->readDouble(); |
|
534 |
$this->pos = $pos; |
|
535 |
return $val; |
|
536 |
} |
|
537 |
||
16 | 538 |
/** |
539 |
* @return string |
|
540 |
*/ |
|
0 | 541 |
public function peekUTF() { |
542 |
$pos = $this->pos; |
|
543 |
$val = $this->readUTF(); |
|
544 |
$this->pos = $pos; |
|
545 |
return $val; |
|
546 |
} |
|
547 |
||
16 | 548 |
/** |
549 |
* @return string |
|
550 |
*/ |
|
0 | 551 |
public function peekLongUTF() { |
552 |
$pos = $this->pos; |
|
553 |
$val = $this->readLongUTF(); |
|
554 |
$this->pos = $pos; |
|
555 |
return $val; |
|
556 |
} |
|
557 |
} |
|
558 |
||
16 | 559 |
class AMFReader |
560 |
{ |
|
561 |
/** |
|
562 |
* @var AMFStream |
|
563 |
*/ |
|
0 | 564 |
public $stream; |
565 |
||
16 | 566 |
/** |
567 |
* @param AMFStream $stream |
|
568 |
*/ |
|
569 |
public function __construct(AMFStream $stream) { |
|
570 |
$this->stream = $stream; |
|
0 | 571 |
} |
572 |
||
16 | 573 |
/** |
574 |
* @return mixed |
|
575 |
*/ |
|
0 | 576 |
public function readData() { |
577 |
$value = null; |
|
578 |
||
579 |
$type = $this->stream->readByte(); |
|
580 |
switch ($type) { |
|
581 |
||
582 |
// Double |
|
583 |
case 0: |
|
584 |
$value = $this->readDouble(); |
|
585 |
break; |
|
586 |
||
587 |
// Boolean |
|
588 |
case 1: |
|
589 |
$value = $this->readBoolean(); |
|
590 |
break; |
|
591 |
||
592 |
// String |
|
593 |
case 2: |
|
594 |
$value = $this->readString(); |
|
595 |
break; |
|
596 |
||
597 |
// Object |
|
598 |
case 3: |
|
599 |
$value = $this->readObject(); |
|
600 |
break; |
|
601 |
||
602 |
// null |
|
603 |
case 6: |
|
604 |
return null; |
|
605 |
||
606 |
// Mixed array |
|
607 |
case 8: |
|
608 |
$value = $this->readMixedArray(); |
|
609 |
break; |
|
610 |
||
611 |
// Array |
|
612 |
case 10: |
|
613 |
$value = $this->readArray(); |
|
614 |
break; |
|
615 |
||
616 |
// Date |
|
617 |
case 11: |
|
618 |
$value = $this->readDate(); |
|
619 |
break; |
|
620 |
||
621 |
// Long string |
|
622 |
case 13: |
|
623 |
$value = $this->readLongString(); |
|
624 |
break; |
|
625 |
||
626 |
// XML (handled as string) |
|
627 |
case 15: |
|
628 |
$value = $this->readXML(); |
|
629 |
break; |
|
630 |
||
631 |
// Typed object (handled as object) |
|
632 |
case 16: |
|
633 |
$value = $this->readTypedObject(); |
|
634 |
break; |
|
635 |
||
636 |
// Long string |
|
637 |
default: |
|
638 |
$value = '(unknown or unsupported data type)'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
break; |
0 | 640 |
} |
641 |
||
642 |
return $value; |
|
643 |
} |
|
644 |
||
16 | 645 |
/** |
646 |
* @return float|false |
|
647 |
*/ |
|
0 | 648 |
public function readDouble() { |
649 |
return $this->stream->readDouble(); |
|
650 |
} |
|
651 |
||
16 | 652 |
/** |
653 |
* @return bool |
|
654 |
*/ |
|
0 | 655 |
public function readBoolean() { |
656 |
return $this->stream->readByte() == 1; |
|
657 |
} |
|
658 |
||
16 | 659 |
/** |
660 |
* @return string |
|
661 |
*/ |
|
0 | 662 |
public function readString() { |
663 |
return $this->stream->readUTF(); |
|
664 |
} |
|
665 |
||
16 | 666 |
/** |
667 |
* @return array |
|
668 |
*/ |
|
0 | 669 |
public function readObject() { |
670 |
// Get highest numerical index - ignored |
|
671 |
// $highestIndex = $this->stream->readLong(); |
|
672 |
||
673 |
$data = array(); |
|
16 | 674 |
$key = null; |
0 | 675 |
|
676 |
while ($key = $this->stream->readUTF()) { |
|
677 |
$data[$key] = $this->readData(); |
|
678 |
} |
|
679 |
// Mixed array record ends with empty string (0x00 0x00) and 0x09 |
|
680 |
if (($key == '') && ($this->stream->peekByte() == 0x09)) { |
|
681 |
// Consume byte |
|
682 |
$this->stream->readByte(); |
|
683 |
} |
|
684 |
return $data; |
|
685 |
} |
|
686 |
||
16 | 687 |
/** |
688 |
* @return array |
|
689 |
*/ |
|
0 | 690 |
public function readMixedArray() { |
691 |
// Get highest numerical index - ignored |
|
692 |
$highestIndex = $this->stream->readLong(); |
|
693 |
||
694 |
$data = array(); |
|
16 | 695 |
$key = null; |
0 | 696 |
|
697 |
while ($key = $this->stream->readUTF()) { |
|
698 |
if (is_numeric($key)) { |
|
16 | 699 |
$key = (int) $key; |
0 | 700 |
} |
701 |
$data[$key] = $this->readData(); |
|
702 |
} |
|
703 |
// Mixed array record ends with empty string (0x00 0x00) and 0x09 |
|
704 |
if (($key == '') && ($this->stream->peekByte() == 0x09)) { |
|
705 |
// Consume byte |
|
706 |
$this->stream->readByte(); |
|
707 |
} |
|
708 |
||
709 |
return $data; |
|
710 |
} |
|
711 |
||
16 | 712 |
/** |
713 |
* @return array |
|
714 |
*/ |
|
0 | 715 |
public function readArray() { |
716 |
$length = $this->stream->readLong(); |
|
717 |
$data = array(); |
|
718 |
||
719 |
for ($i = 0; $i < $length; $i++) { |
|
720 |
$data[] = $this->readData(); |
|
721 |
} |
|
722 |
return $data; |
|
723 |
} |
|
724 |
||
16 | 725 |
/** |
726 |
* @return float|false |
|
727 |
*/ |
|
0 | 728 |
public function readDate() { |
729 |
$timestamp = $this->stream->readDouble(); |
|
730 |
$timezone = $this->stream->readInt(); |
|
731 |
return $timestamp; |
|
732 |
} |
|
733 |
||
16 | 734 |
/** |
735 |
* @return string |
|
736 |
*/ |
|
0 | 737 |
public function readLongString() { |
738 |
return $this->stream->readLongUTF(); |
|
739 |
} |
|
740 |
||
16 | 741 |
/** |
742 |
* @return string |
|
743 |
*/ |
|
0 | 744 |
public function readXML() { |
745 |
return $this->stream->readLongUTF(); |
|
746 |
} |
|
747 |
||
16 | 748 |
/** |
749 |
* @return array |
|
750 |
*/ |
|
0 | 751 |
public function readTypedObject() { |
752 |
$className = $this->stream->readUTF(); |
|
753 |
return $this->readObject(); |
|
754 |
} |
|
755 |
} |
|
756 |
||
16 | 757 |
class AVCSequenceParameterSetReader |
758 |
{ |
|
759 |
/** |
|
760 |
* @var string |
|
761 |
*/ |
|
0 | 762 |
public $sps; |
763 |
public $start = 0; |
|
764 |
public $currentBytes = 0; |
|
765 |
public $currentBits = 0; |
|
16 | 766 |
|
767 |
/** |
|
768 |
* @var int |
|
769 |
*/ |
|
0 | 770 |
public $width; |
16 | 771 |
|
772 |
/** |
|
773 |
* @var int |
|
774 |
*/ |
|
0 | 775 |
public $height; |
776 |
||
16 | 777 |
/** |
778 |
* @param string $sps |
|
779 |
*/ |
|
5 | 780 |
public function __construct($sps) { |
0 | 781 |
$this->sps = $sps; |
782 |
} |
|
783 |
||
784 |
public function readData() { |
|
785 |
$this->skipBits(8); |
|
786 |
$this->skipBits(8); |
|
5 | 787 |
$profile = $this->getBits(8); // read profile |
788 |
if ($profile > 0) { |
|
789 |
$this->skipBits(8); |
|
790 |
$level_idc = $this->getBits(8); // level_idc |
|
791 |
$this->expGolombUe(); // seq_parameter_set_id // sps |
|
792 |
$this->expGolombUe(); // log2_max_frame_num_minus4 |
|
793 |
$picOrderType = $this->expGolombUe(); // pic_order_cnt_type |
|
794 |
if ($picOrderType == 0) { |
|
795 |
$this->expGolombUe(); // log2_max_pic_order_cnt_lsb_minus4 |
|
796 |
} elseif ($picOrderType == 1) { |
|
797 |
$this->skipBits(1); // delta_pic_order_always_zero_flag |
|
798 |
$this->expGolombSe(); // offset_for_non_ref_pic |
|
799 |
$this->expGolombSe(); // offset_for_top_to_bottom_field |
|
800 |
$num_ref_frames_in_pic_order_cnt_cycle = $this->expGolombUe(); // num_ref_frames_in_pic_order_cnt_cycle |
|
801 |
for ($i = 0; $i < $num_ref_frames_in_pic_order_cnt_cycle; $i++) { |
|
802 |
$this->expGolombSe(); // offset_for_ref_frame[ i ] |
|
0 | 803 |
} |
804 |
} |
|
5 | 805 |
$this->expGolombUe(); // num_ref_frames |
806 |
$this->skipBits(1); // gaps_in_frame_num_value_allowed_flag |
|
807 |
$pic_width_in_mbs_minus1 = $this->expGolombUe(); // pic_width_in_mbs_minus1 |
|
808 |
$pic_height_in_map_units_minus1 = $this->expGolombUe(); // pic_height_in_map_units_minus1 |
|
809 |
||
810 |
$frame_mbs_only_flag = $this->getBits(1); // frame_mbs_only_flag |
|
811 |
if ($frame_mbs_only_flag == 0) { |
|
812 |
$this->skipBits(1); // mb_adaptive_frame_field_flag |
|
813 |
} |
|
814 |
$this->skipBits(1); // direct_8x8_inference_flag |
|
815 |
$frame_cropping_flag = $this->getBits(1); // frame_cropping_flag |
|
816 |
||
817 |
$frame_crop_left_offset = 0; |
|
818 |
$frame_crop_right_offset = 0; |
|
819 |
$frame_crop_top_offset = 0; |
|
820 |
$frame_crop_bottom_offset = 0; |
|
821 |
||
822 |
if ($frame_cropping_flag) { |
|
823 |
$frame_crop_left_offset = $this->expGolombUe(); // frame_crop_left_offset |
|
824 |
$frame_crop_right_offset = $this->expGolombUe(); // frame_crop_right_offset |
|
825 |
$frame_crop_top_offset = $this->expGolombUe(); // frame_crop_top_offset |
|
826 |
$frame_crop_bottom_offset = $this->expGolombUe(); // frame_crop_bottom_offset |
|
827 |
} |
|
828 |
$this->skipBits(1); // vui_parameters_present_flag |
|
829 |
// etc |
|
830 |
||
831 |
$this->width = (($pic_width_in_mbs_minus1 + 1) * 16) - ($frame_crop_left_offset * 2) - ($frame_crop_right_offset * 2); |
|
832 |
$this->height = ((2 - $frame_mbs_only_flag) * ($pic_height_in_map_units_minus1 + 1) * 16) - ($frame_crop_top_offset * 2) - ($frame_crop_bottom_offset * 2); |
|
0 | 833 |
} |
834 |
} |
|
835 |
||
16 | 836 |
/** |
837 |
* @param int $bits |
|
838 |
*/ |
|
0 | 839 |
public function skipBits($bits) { |
840 |
$newBits = $this->currentBits + $bits; |
|
841 |
$this->currentBytes += (int)floor($newBits / 8); |
|
842 |
$this->currentBits = $newBits % 8; |
|
843 |
} |
|
844 |
||
16 | 845 |
/** |
846 |
* @return int |
|
847 |
*/ |
|
0 | 848 |
public function getBit() { |
849 |
$result = (getid3_lib::BigEndian2Int(substr($this->sps, $this->currentBytes, 1)) >> (7 - $this->currentBits)) & 0x01; |
|
850 |
$this->skipBits(1); |
|
851 |
return $result; |
|
852 |
} |
|
853 |
||
16 | 854 |
/** |
855 |
* @param int $bits |
|
856 |
* |
|
857 |
* @return int |
|
858 |
*/ |
|
0 | 859 |
public function getBits($bits) { |
860 |
$result = 0; |
|
861 |
for ($i = 0; $i < $bits; $i++) { |
|
862 |
$result = ($result << 1) + $this->getBit(); |
|
863 |
} |
|
864 |
return $result; |
|
865 |
} |
|
866 |
||
16 | 867 |
/** |
868 |
* @return int |
|
869 |
*/ |
|
0 | 870 |
public function expGolombUe() { |
871 |
$significantBits = 0; |
|
872 |
$bit = $this->getBit(); |
|
873 |
while ($bit == 0) { |
|
874 |
$significantBits++; |
|
875 |
$bit = $this->getBit(); |
|
876 |
||
877 |
if ($significantBits > 31) { |
|
878 |
// something is broken, this is an emergency escape to prevent infinite loops |
|
879 |
return 0; |
|
880 |
} |
|
881 |
} |
|
882 |
return (1 << $significantBits) + $this->getBits($significantBits) - 1; |
|
883 |
} |
|
884 |
||
16 | 885 |
/** |
886 |
* @return int |
|
887 |
*/ |
|
0 | 888 |
public function expGolombSe() { |
889 |
$result = $this->expGolombUe(); |
|
890 |
if (($result & 0x01) == 0) { |
|
891 |
return -($result >> 1); |
|
892 |
} else { |
|
893 |
return ($result + 1) >> 1; |
|
894 |
} |
|
895 |
} |
|
896 |
||
16 | 897 |
/** |
898 |
* @return int |
|
899 |
*/ |
|
0 | 900 |
public function getWidth() { |
901 |
return $this->width; |
|
902 |
} |
|
903 |
||
16 | 904 |
/** |
905 |
* @return int |
|
906 |
*/ |
|
0 | 907 |
public function getHeight() { |
908 |
return $this->height; |
|
909 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
910 |
} |