2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
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 /////////////////////////////////////////////////////////////////
10 // module.audio-video.flv.php //
11 // module for analyzing Shockwave Flash Video files //
12 // dependencies: NONE //
14 /////////////////////////////////////////////////////////////////
16 // FLV module by Seth Kaufman <sethØwhirl-i-gig*com> //
18 // * version 0.1 (26 June 2005) //
20 // * version 0.1.1 (15 July 2005) //
21 // minor modifications by James Heinrich <info@getid3.org> //
23 // * version 0.2 (22 February 2006) //
24 // Support for On2 VP6 codec and meta information //
25 // by Steve Webster <steve.websterØfeaturecreep*com> //
27 // * version 0.3 (15 June 2006) //
28 // Modified to not read entire file into memory //
29 // by James Heinrich <info@getid3.org> //
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> //
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> //
42 // * version 0.6 (24 May 2009) //
43 // Better parsing of files with h264 video //
44 // by Evgeny Moysevich <moysevichØgmail*com> //
46 // * version 0.6.1 (30 May 2011) //
47 // prevent infinite loops in expGolombUe() //
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> //
54 /////////////////////////////////////////////////////////////////
56 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
60 define('GETID3_FLV_TAG_AUDIO', 8);
61 define('GETID3_FLV_TAG_VIDEO', 9);
62 define('GETID3_FLV_TAG_META', 18);
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);
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);
81 class getid3_flv
extends getid3_handler
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.
91 public $max_frames = 100000;
96 public function Analyze() {
97 $info = &$this->getid3
->info
;
99 $this->fseek($info['avdataoffset']);
101 $FLVdataLength = $info['avdataend'] - $info['avdataoffset'];
102 $FLVheader = $this->fread(5);
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));
109 if ($info['flv']['header']['signature'] != self
::magic
) {
110 $this->error('Expecting "'.getid3_lib
::PrintHexBytes(self
::magic
).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($info['flv']['header']['signature']).'"');
111 unset($info['flv'], $info['fileformat']);
115 $info['flv']['header']['hasAudio'] = (bool) ($TypeFlags & 0x04);
116 $info['flv']['header']['hasVideo'] = (bool) ($TypeFlags & 0x01);
118 $FrameSizeDataLength = getid3_lib
::BigEndian2Int($this->fread(4));
119 $FLVheaderFrameLength = 9;
120 if ($FrameSizeDataLength > $FLVheaderFrameLength) {
121 $this->fseek($FrameSizeDataLength - $FLVheaderFrameLength, SEEK_CUR
);
124 $found_video = false;
125 $found_audio = false;
127 $found_valid_meta_playtime = false;
129 $info['flv']['framecount'] = array('total'=>0, 'audio'=>0, 'video'=>0);
130 $flv_framecount = &$info['flv']['framecount'];
131 while ((($this->ftell() +
16) < $info['avdataend']) && (($tagParseCount++
<= $this->max_frames
) ||
!$found_valid_meta_playtime)) {
132 $ThisTagHeader = $this->fread(16);
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));
139 $NextOffset = $this->ftell() - 1 +
$DataLength;
140 if ($Timestamp > $Duration) {
141 $Duration = $Timestamp;
144 $flv_framecount['total']++
;
146 case GETID3_FLV_TAG_AUDIO
:
147 $flv_framecount['audio']++
;
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;
157 case GETID3_FLV_TAG_VIDEO
:
158 $flv_framecount['video']++
;
161 $info['flv']['video']['videoCodec'] = $LastHeaderByte & 0x07;
163 $FLVvideoHeader = $this->fread(11);
165 if ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H264
) {
166 // this code block contributed by: moysevichØgmail*com
168 $AVCPacketType = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 0, 1));
169 if ($AVCPacketType == H264_AVC_SEQUENCE_HEADER
) {
170 // read AVCDecoderConfigurationRecord
171 $configurationVersion = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 4, 1));
172 $AVCProfileIndication = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 5, 1));
173 $profile_compatibility = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 6, 1));
174 $lengthSizeMinusOne = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 7, 1));
175 $numOfSequenceParameterSets = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 8, 1));
177 if (($numOfSequenceParameterSets & 0x1F) != 0) {
178 // there is at least one SequenceParameterSet
179 // read size of the first SequenceParameterSet
180 //$spsSize = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 9, 2));
181 $spsSize = getid3_lib
::LittleEndian2Int(substr($FLVvideoHeader, 9, 2));
182 // read the first SequenceParameterSet
183 $sps = $this->fread($spsSize);
184 if (strlen($sps) == $spsSize) { // make sure that whole SequenceParameterSet was red
185 $spsReader = new AVCSequenceParameterSetReader($sps);
186 $spsReader->readData();
187 $info['video']['resolution_x'] = $spsReader->getWidth();
188 $info['video']['resolution_y'] = $spsReader->getHeight();
192 // end: moysevichØgmail*com
194 } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H263
) {
196 $PictureSizeType = (getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 3, 2))) >> 7;
197 $PictureSizeType = $PictureSizeType & 0x0007;
198 $info['flv']['header']['videoSizeType'] = $PictureSizeType;
199 switch ($PictureSizeType) {
201 //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2));
202 //$PictureSizeEnc <<= 1;
203 //$info['video']['resolution_x'] = ($PictureSizeEnc & 0xFF00) >> 8;
204 //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
205 //$PictureSizeEnc <<= 1;
206 //$info['video']['resolution_y'] = ($PictureSizeEnc & 0xFF00) >> 8;
208 $PictureSizeEnc['x'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 4, 2)) >> 7;
209 $PictureSizeEnc['y'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 5, 2)) >> 7;
210 $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFF;
211 $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFF;
215 $PictureSizeEnc['x'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 4, 3)) >> 7;
216 $PictureSizeEnc['y'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 6, 3)) >> 7;
217 $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFFFF;
218 $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFFFF;
222 $info['video']['resolution_x'] = 352;
223 $info['video']['resolution_y'] = 288;
227 $info['video']['resolution_x'] = 176;
228 $info['video']['resolution_y'] = 144;
232 $info['video']['resolution_x'] = 128;
233 $info['video']['resolution_y'] = 96;
237 $info['video']['resolution_x'] = 320;
238 $info['video']['resolution_y'] = 240;
242 $info['video']['resolution_x'] = 160;
243 $info['video']['resolution_y'] = 120;
247 $info['video']['resolution_x'] = 0;
248 $info['video']['resolution_y'] = 0;
253 } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_VP6FLV_ALPHA
) {
255 /* contributed by schouwerwouØgmail*com */
256 if (!isset($info['video']['resolution_x'])) { // only when meta data isn't set
257 $PictureSizeEnc['x'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
258 $PictureSizeEnc['y'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 7, 2));
259 $info['video']['resolution_x'] = ($PictureSizeEnc['x'] & 0xFF) << 3;
260 $info['video']['resolution_y'] = ($PictureSizeEnc['y'] & 0xFF) << 3;
262 /* end schouwerwouØgmail*com */
265 if (!empty($info['video']['resolution_x']) && !empty($info['video']['resolution_y'])) {
266 $info['video']['pixel_aspect_ratio'] = $info['video']['resolution_x'] / $info['video']['resolution_y'];
272 case GETID3_FLV_TAG_META
:
275 $this->fseek(-1, SEEK_CUR
);
276 $datachunk = $this->fread($DataLength);
277 $AMFstream = new AMFStream($datachunk);
278 $reader = new AMFReader($AMFstream);
279 $eventName = $reader->readData();
280 $info['flv']['meta'][$eventName] = $reader->readData();
283 $copykeys = array('framerate'=>'frame_rate', 'width'=>'resolution_x', 'height'=>'resolution_y', 'audiodatarate'=>'bitrate', 'videodatarate'=>'bitrate');
284 foreach ($copykeys as $sourcekey => $destkey) {
285 if (isset($info['flv']['meta']['onMetaData'][$sourcekey])) {
286 switch ($sourcekey) {
289 $info['video'][$destkey] = intval(round($info['flv']['meta']['onMetaData'][$sourcekey]));
291 case 'audiodatarate':
292 $info['audio'][$destkey] = getid3_lib
::CastAsInt(round($info['flv']['meta']['onMetaData'][$sourcekey] * 1000));
294 case 'videodatarate':
297 $info['video'][$destkey] = $info['flv']['meta']['onMetaData'][$sourcekey];
302 if (!empty($info['flv']['meta']['onMetaData']['duration'])) {
303 $found_valid_meta_playtime = true;
312 $this->fseek($NextOffset);
315 $info['playtime_seconds'] = $Duration / 1000;
316 if ($info['playtime_seconds'] > 0) {
317 $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
320 if ($info['flv']['header']['hasAudio']) {
321 $info['audio']['codec'] = self
::audioFormatLookup($info['flv']['audio']['audioFormat']);
322 $info['audio']['sample_rate'] = self
::audioRateLookup($info['flv']['audio']['audioRate']);
323 $info['audio']['bits_per_sample'] = self
::audioBitDepthLookup($info['flv']['audio']['audioSampleSize']);
325 $info['audio']['channels'] = $info['flv']['audio']['audioType'] +
1; // 0=mono,1=stereo
326 $info['audio']['lossless'] = ($info['flv']['audio']['audioFormat'] ?
false : true); // 0=uncompressed
327 $info['audio']['dataformat'] = 'flv';
329 if (!empty($info['flv']['header']['hasVideo'])) {
330 $info['video']['codec'] = self
::videoCodecLookup($info['flv']['video']['videoCodec']);
331 $info['video']['dataformat'] = 'flv';
332 $info['video']['lossless'] = false;
335 // Set information from meta
336 if (!empty($info['flv']['meta']['onMetaData']['duration'])) {
337 $info['playtime_seconds'] = $info['flv']['meta']['onMetaData']['duration'];
338 $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
340 if (isset($info['flv']['meta']['onMetaData']['audiocodecid'])) {
341 $info['audio']['codec'] = self
::audioFormatLookup($info['flv']['meta']['onMetaData']['audiocodecid']);
343 if (isset($info['flv']['meta']['onMetaData']['videocodecid'])) {
344 $info['video']['codec'] = self
::videoCodecLookup($info['flv']['meta']['onMetaData']['videocodecid']);
352 * @return string|false
354 public static function audioFormatLookup($id) {
355 static $lookup = array(
356 0 => 'Linear PCM, platform endian',
359 3 => 'Linear PCM, little endian',
360 4 => 'Nellymoser 16kHz mono',
361 5 => 'Nellymoser 8kHz mono',
363 7 => 'G.711A-law logarithmic PCM',
364 8 => 'G.711 mu-law logarithmic PCM',
368 12 => false, // unknown?
369 13 => false, // unknown?
371 15 => 'Device-specific sound',
373 return (isset($lookup[$id]) ?
$lookup[$id] : false);
381 public static function audioRateLookup($id) {
382 static $lookup = array(
388 return (isset($lookup[$id]) ?
$lookup[$id] : false);
396 public static function audioBitDepthLookup($id) {
397 static $lookup = array(
401 return (isset($lookup[$id]) ?
$lookup[$id] : false);
407 * @return string|false
409 public static function videoCodecLookup($id) {
410 static $lookup = array(
411 GETID3_FLV_VIDEO_H263
=> 'Sorenson H.263',
412 GETID3_FLV_VIDEO_SCREEN
=> 'Screen video',
413 GETID3_FLV_VIDEO_VP6FLV
=> 'On2 VP6',
414 GETID3_FLV_VIDEO_VP6FLV_ALPHA
=> 'On2 VP6 with alpha channel',
415 GETID3_FLV_VIDEO_SCREENV2
=> 'Screen video v2',
416 GETID3_FLV_VIDEO_H264
=> 'Sorenson H.264',
418 return (isset($lookup[$id]) ?
$lookup[$id] : false);
435 * @param string $bytes
437 public function __construct(&$bytes) {
438 $this->bytes
=& $bytes;
445 public function readByte() { // 8-bit
446 return ord(substr($this->bytes
, $this->pos++
, 1));
452 public function readInt() { // 16-bit
453 return ($this->readByte() << 8) +
$this->readByte();
459 public function readLong() { // 32-bit
460 return ($this->readByte() << 24) +
($this->readByte() << 16) +
($this->readByte() << 8) +
$this->readByte();
464 * @return float|false
466 public function readDouble() {
467 return getid3_lib
::BigEndian2Float($this->read(8));
473 public function readUTF() {
474 $length = $this->readInt();
475 return $this->read($length);
481 public function readLongUTF() {
482 $length = $this->readLong();
483 return $this->read($length);
491 public function read($length) {
492 $val = substr($this->bytes
, $this->pos
, $length);
493 $this->pos +
= $length;
500 public function peekByte() {
502 $val = $this->readByte();
510 public function peekInt() {
512 $val = $this->readInt();
520 public function peekLong() {
522 $val = $this->readLong();
528 * @return float|false
530 public function peekDouble() {
532 $val = $this->readDouble();
540 public function peekUTF() {
542 $val = $this->readUTF();
550 public function peekLongUTF() {
552 $val = $this->readLongUTF();
566 * @param AMFStream $stream
568 public function __construct(AMFStream
$stream) {
569 $this->stream
= $stream;
575 public function readData() {
578 $type = $this->stream
->readByte();
583 $value = $this->readDouble();
588 $value = $this->readBoolean();
593 $value = $this->readString();
598 $value = $this->readObject();
607 $value = $this->readMixedArray();
612 $value = $this->readArray();
617 $value = $this->readDate();
622 $value = $this->readLongString();
625 // XML (handled as string)
627 $value = $this->readXML();
630 // Typed object (handled as object)
632 $value = $this->readTypedObject();
637 $value = '(unknown or unsupported data type)';
645 * @return float|false
647 public function readDouble() {
648 return $this->stream
->readDouble();
654 public function readBoolean() {
655 return $this->stream
->readByte() == 1;
661 public function readString() {
662 return $this->stream
->readUTF();
668 public function readObject() {
669 // Get highest numerical index - ignored
670 // $highestIndex = $this->stream->readLong();
675 while ($key = $this->stream
->readUTF()) {
676 $data[$key] = $this->readData();
678 // Mixed array record ends with empty string (0x00 0x00) and 0x09
679 if (($key == '') && ($this->stream
->peekByte() == 0x09)) {
681 $this->stream
->readByte();
689 public function readMixedArray() {
690 // Get highest numerical index - ignored
691 $highestIndex = $this->stream
->readLong();
696 while ($key = $this->stream
->readUTF()) {
697 if (is_numeric($key)) {
700 $data[$key] = $this->readData();
702 // Mixed array record ends with empty string (0x00 0x00) and 0x09
703 if (($key == '') && ($this->stream
->peekByte() == 0x09)) {
705 $this->stream
->readByte();
714 public function readArray() {
715 $length = $this->stream
->readLong();
718 for ($i = 0; $i < $length; $i++
) {
719 $data[] = $this->readData();
725 * @return float|false
727 public function readDate() {
728 $timestamp = $this->stream
->readDouble();
729 $timezone = $this->stream
->readInt();
736 public function readLongString() {
737 return $this->stream
->readLongUTF();
743 public function readXML() {
744 return $this->stream
->readLongUTF();
750 public function readTypedObject() {
751 $className = $this->stream
->readUTF();
752 return $this->readObject();
756 class AVCSequenceParameterSetReader
763 public $currentBytes = 0;
764 public $currentBits = 0;
779 public function __construct($sps) {
783 public function readData() {
786 $profile = $this->getBits(8); // read profile
789 $level_idc = $this->getBits(8); // level_idc
790 $this->expGolombUe(); // seq_parameter_set_id // sps
791 $this->expGolombUe(); // log2_max_frame_num_minus4
792 $picOrderType = $this->expGolombUe(); // pic_order_cnt_type
793 if ($picOrderType == 0) {
794 $this->expGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
795 } elseif ($picOrderType == 1) {
796 $this->skipBits(1); // delta_pic_order_always_zero_flag
797 $this->expGolombSe(); // offset_for_non_ref_pic
798 $this->expGolombSe(); // offset_for_top_to_bottom_field
799 $num_ref_frames_in_pic_order_cnt_cycle = $this->expGolombUe(); // num_ref_frames_in_pic_order_cnt_cycle
800 for ($i = 0; $i < $num_ref_frames_in_pic_order_cnt_cycle; $i++
) {
801 $this->expGolombSe(); // offset_for_ref_frame[ i ]
804 $this->expGolombUe(); // num_ref_frames
805 $this->skipBits(1); // gaps_in_frame_num_value_allowed_flag
806 $pic_width_in_mbs_minus1 = $this->expGolombUe(); // pic_width_in_mbs_minus1
807 $pic_height_in_map_units_minus1 = $this->expGolombUe(); // pic_height_in_map_units_minus1
809 $frame_mbs_only_flag = $this->getBits(1); // frame_mbs_only_flag
810 if ($frame_mbs_only_flag == 0) {
811 $this->skipBits(1); // mb_adaptive_frame_field_flag
813 $this->skipBits(1); // direct_8x8_inference_flag
814 $frame_cropping_flag = $this->getBits(1); // frame_cropping_flag
816 $frame_crop_left_offset = 0;
817 $frame_crop_right_offset = 0;
818 $frame_crop_top_offset = 0;
819 $frame_crop_bottom_offset = 0;
821 if ($frame_cropping_flag) {
822 $frame_crop_left_offset = $this->expGolombUe(); // frame_crop_left_offset
823 $frame_crop_right_offset = $this->expGolombUe(); // frame_crop_right_offset
824 $frame_crop_top_offset = $this->expGolombUe(); // frame_crop_top_offset
825 $frame_crop_bottom_offset = $this->expGolombUe(); // frame_crop_bottom_offset
827 $this->skipBits(1); // vui_parameters_present_flag
830 $this->width
= (($pic_width_in_mbs_minus1 +
1) * 16) - ($frame_crop_left_offset * 2) - ($frame_crop_right_offset * 2);
831 $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);
838 public function skipBits($bits) {
839 $newBits = $this->currentBits +
$bits;
840 $this->currentBytes +
= (int)floor($newBits / 8);
841 $this->currentBits
= $newBits %
8;
847 public function getBit() {
848 $result = (getid3_lib
::BigEndian2Int(substr($this->sps
, $this->currentBytes
, 1)) >> (7 - $this->currentBits
)) & 0x01;
858 public function getBits($bits) {
860 for ($i = 0; $i < $bits; $i++
) {
861 $result = ($result << 1) +
$this->getBit();
869 public function expGolombUe() {
870 $significantBits = 0;
871 $bit = $this->getBit();
874 $bit = $this->getBit();
876 if ($significantBits > 31) {
877 // something is broken, this is an emergency escape to prevent infinite loops
881 return (1 << $significantBits) +
$this->getBits($significantBits) - 1;
887 public function expGolombSe() {
888 $result = $this->expGolombUe();
889 if (($result & 0x01) == 0) {
890 return -($result >> 1);
892 return ($result +
1) >> 1;
899 public function getWidth() {
906 public function getHeight() {
907 return $this->height
;