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 define('GETID3_FLV_TAG_AUDIO', 8);
57 define('GETID3_FLV_TAG_VIDEO', 9);
58 define('GETID3_FLV_TAG_META', 18);
60 define('GETID3_FLV_VIDEO_H263', 2);
61 define('GETID3_FLV_VIDEO_SCREEN', 3);
62 define('GETID3_FLV_VIDEO_VP6FLV', 4);
63 define('GETID3_FLV_VIDEO_VP6FLV_ALPHA', 5);
64 define('GETID3_FLV_VIDEO_SCREENV2', 6);
65 define('GETID3_FLV_VIDEO_H264', 7);
67 define('H264_AVC_SEQUENCE_HEADER', 0);
68 define('H264_PROFILE_BASELINE', 66);
69 define('H264_PROFILE_MAIN', 77);
70 define('H264_PROFILE_EXTENDED', 88);
71 define('H264_PROFILE_HIGH', 100);
72 define('H264_PROFILE_HIGH10', 110);
73 define('H264_PROFILE_HIGH422', 122);
74 define('H264_PROFILE_HIGH444', 144);
75 define('H264_PROFILE_HIGH444_PREDICTIVE', 244);
77 class getid3_flv
extends getid3_handler
82 * Break out of the loop if too many frames have been scanned; only scan this
83 * many if meta frame does not contain useful duration.
87 public $max_frames = 100000;
92 public function Analyze() {
93 $info = &$this->getid3
->info
;
95 $this->fseek($info['avdataoffset']);
97 $FLVdataLength = $info['avdataend'] - $info['avdataoffset'];
98 $FLVheader = $this->fread(5);
100 $info['fileformat'] = 'flv';
101 $info['flv']['header']['signature'] = substr($FLVheader, 0, 3);
102 $info['flv']['header']['version'] = getid3_lib
::BigEndian2Int(substr($FLVheader, 3, 1));
103 $TypeFlags = getid3_lib
::BigEndian2Int(substr($FLVheader, 4, 1));
105 if ($info['flv']['header']['signature'] != self
::magic
) {
106 $this->error('Expecting "'.getid3_lib
::PrintHexBytes(self
::magic
).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($info['flv']['header']['signature']).'"');
107 unset($info['flv'], $info['fileformat']);
111 $info['flv']['header']['hasAudio'] = (bool) ($TypeFlags & 0x04);
112 $info['flv']['header']['hasVideo'] = (bool) ($TypeFlags & 0x01);
114 $FrameSizeDataLength = getid3_lib
::BigEndian2Int($this->fread(4));
115 $FLVheaderFrameLength = 9;
116 if ($FrameSizeDataLength > $FLVheaderFrameLength) {
117 $this->fseek($FrameSizeDataLength - $FLVheaderFrameLength, SEEK_CUR
);
120 $found_video = false;
121 $found_audio = false;
123 $found_valid_meta_playtime = false;
125 $info['flv']['framecount'] = array('total'=>0, 'audio'=>0, 'video'=>0);
126 $flv_framecount = &$info['flv']['framecount'];
127 while ((($this->ftell() +
16) < $info['avdataend']) && (($tagParseCount++
<= $this->max_frames
) ||
!$found_valid_meta_playtime)) {
128 $ThisTagHeader = $this->fread(16);
130 $PreviousTagLength = getid3_lib
::BigEndian2Int(substr($ThisTagHeader, 0, 4));
131 $TagType = getid3_lib
::BigEndian2Int(substr($ThisTagHeader, 4, 1));
132 $DataLength = getid3_lib
::BigEndian2Int(substr($ThisTagHeader, 5, 3));
133 $Timestamp = getid3_lib
::BigEndian2Int(substr($ThisTagHeader, 8, 3));
134 $LastHeaderByte = getid3_lib
::BigEndian2Int(substr($ThisTagHeader, 15, 1));
135 $NextOffset = $this->ftell() - 1 +
$DataLength;
136 if ($Timestamp > $Duration) {
137 $Duration = $Timestamp;
140 $flv_framecount['total']++
;
142 case GETID3_FLV_TAG_AUDIO
:
143 $flv_framecount['audio']++
;
146 $info['flv']['audio']['audioFormat'] = ($LastHeaderByte >> 4) & 0x0F;
147 $info['flv']['audio']['audioRate'] = ($LastHeaderByte >> 2) & 0x03;
148 $info['flv']['audio']['audioSampleSize'] = ($LastHeaderByte >> 1) & 0x01;
149 $info['flv']['audio']['audioType'] = $LastHeaderByte & 0x01;
153 case GETID3_FLV_TAG_VIDEO
:
154 $flv_framecount['video']++
;
157 $info['flv']['video']['videoCodec'] = $LastHeaderByte & 0x07;
159 $FLVvideoHeader = $this->fread(11);
161 if ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H264
) {
162 // this code block contributed by: moysevichØgmail*com
164 $AVCPacketType = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 0, 1));
165 if ($AVCPacketType == H264_AVC_SEQUENCE_HEADER
) {
166 // read AVCDecoderConfigurationRecord
167 $configurationVersion = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 4, 1));
168 $AVCProfileIndication = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 5, 1));
169 $profile_compatibility = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 6, 1));
170 $lengthSizeMinusOne = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 7, 1));
171 $numOfSequenceParameterSets = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 8, 1));
173 if (($numOfSequenceParameterSets & 0x1F) != 0) {
174 // there is at least one SequenceParameterSet
175 // read size of the first SequenceParameterSet
176 //$spsSize = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 9, 2));
177 $spsSize = getid3_lib
::LittleEndian2Int(substr($FLVvideoHeader, 9, 2));
178 // read the first SequenceParameterSet
179 $sps = $this->fread($spsSize);
180 if (strlen($sps) == $spsSize) { // make sure that whole SequenceParameterSet was red
181 $spsReader = new AVCSequenceParameterSetReader($sps);
182 $spsReader->readData();
183 $info['video']['resolution_x'] = $spsReader->getWidth();
184 $info['video']['resolution_y'] = $spsReader->getHeight();
188 // end: moysevichØgmail*com
190 } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H263
) {
192 $PictureSizeType = (getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 3, 2))) >> 7;
193 $PictureSizeType = $PictureSizeType & 0x0007;
194 $info['flv']['header']['videoSizeType'] = $PictureSizeType;
195 switch ($PictureSizeType) {
197 //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2));
198 //$PictureSizeEnc <<= 1;
199 //$info['video']['resolution_x'] = ($PictureSizeEnc & 0xFF00) >> 8;
200 //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
201 //$PictureSizeEnc <<= 1;
202 //$info['video']['resolution_y'] = ($PictureSizeEnc & 0xFF00) >> 8;
204 $PictureSizeEnc['x'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 4, 2)) >> 7;
205 $PictureSizeEnc['y'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 5, 2)) >> 7;
206 $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFF;
207 $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFF;
211 $PictureSizeEnc['x'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 4, 3)) >> 7;
212 $PictureSizeEnc['y'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 6, 3)) >> 7;
213 $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFFFF;
214 $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFFFF;
218 $info['video']['resolution_x'] = 352;
219 $info['video']['resolution_y'] = 288;
223 $info['video']['resolution_x'] = 176;
224 $info['video']['resolution_y'] = 144;
228 $info['video']['resolution_x'] = 128;
229 $info['video']['resolution_y'] = 96;
233 $info['video']['resolution_x'] = 320;
234 $info['video']['resolution_y'] = 240;
238 $info['video']['resolution_x'] = 160;
239 $info['video']['resolution_y'] = 120;
243 $info['video']['resolution_x'] = 0;
244 $info['video']['resolution_y'] = 0;
249 } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_VP6FLV_ALPHA
) {
251 /* contributed by schouwerwouØgmail*com */
252 if (!isset($info['video']['resolution_x'])) { // only when meta data isn't set
253 $PictureSizeEnc['x'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
254 $PictureSizeEnc['y'] = getid3_lib
::BigEndian2Int(substr($FLVvideoHeader, 7, 2));
255 $info['video']['resolution_x'] = ($PictureSizeEnc['x'] & 0xFF) << 3;
256 $info['video']['resolution_y'] = ($PictureSizeEnc['y'] & 0xFF) << 3;
258 /* end schouwerwouØgmail*com */
261 if (!empty($info['video']['resolution_x']) && !empty($info['video']['resolution_y'])) {
262 $info['video']['pixel_aspect_ratio'] = $info['video']['resolution_x'] / $info['video']['resolution_y'];
268 case GETID3_FLV_TAG_META
:
271 $this->fseek(-1, SEEK_CUR
);
272 $datachunk = $this->fread($DataLength);
273 $AMFstream = new AMFStream($datachunk);
274 $reader = new AMFReader($AMFstream);
275 $eventName = $reader->readData();
276 $info['flv']['meta'][$eventName] = $reader->readData();
279 $copykeys = array('framerate'=>'frame_rate', 'width'=>'resolution_x', 'height'=>'resolution_y', 'audiodatarate'=>'bitrate', 'videodatarate'=>'bitrate');
280 foreach ($copykeys as $sourcekey => $destkey) {
281 if (isset($info['flv']['meta']['onMetaData'][$sourcekey])) {
282 switch ($sourcekey) {
285 $info['video'][$destkey] = intval(round($info['flv']['meta']['onMetaData'][$sourcekey]));
287 case 'audiodatarate':
288 $info['audio'][$destkey] = getid3_lib
::CastAsInt(round($info['flv']['meta']['onMetaData'][$sourcekey] * 1000));
290 case 'videodatarate':
293 $info['video'][$destkey] = $info['flv']['meta']['onMetaData'][$sourcekey];
298 if (!empty($info['flv']['meta']['onMetaData']['duration'])) {
299 $found_valid_meta_playtime = true;
308 $this->fseek($NextOffset);
311 $info['playtime_seconds'] = $Duration / 1000;
312 if ($info['playtime_seconds'] > 0) {
313 $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
316 if ($info['flv']['header']['hasAudio']) {
317 $info['audio']['codec'] = self
::audioFormatLookup($info['flv']['audio']['audioFormat']);
318 $info['audio']['sample_rate'] = self
::audioRateLookup($info['flv']['audio']['audioRate']);
319 $info['audio']['bits_per_sample'] = self
::audioBitDepthLookup($info['flv']['audio']['audioSampleSize']);
321 $info['audio']['channels'] = $info['flv']['audio']['audioType'] +
1; // 0=mono,1=stereo
322 $info['audio']['lossless'] = ($info['flv']['audio']['audioFormat'] ?
false : true); // 0=uncompressed
323 $info['audio']['dataformat'] = 'flv';
325 if (!empty($info['flv']['header']['hasVideo'])) {
326 $info['video']['codec'] = self
::videoCodecLookup($info['flv']['video']['videoCodec']);
327 $info['video']['dataformat'] = 'flv';
328 $info['video']['lossless'] = false;
331 // Set information from meta
332 if (!empty($info['flv']['meta']['onMetaData']['duration'])) {
333 $info['playtime_seconds'] = $info['flv']['meta']['onMetaData']['duration'];
334 $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
336 if (isset($info['flv']['meta']['onMetaData']['audiocodecid'])) {
337 $info['audio']['codec'] = self
::audioFormatLookup($info['flv']['meta']['onMetaData']['audiocodecid']);
339 if (isset($info['flv']['meta']['onMetaData']['videocodecid'])) {
340 $info['video']['codec'] = self
::videoCodecLookup($info['flv']['meta']['onMetaData']['videocodecid']);
348 * @return string|false
350 public static function audioFormatLookup($id) {
351 static $lookup = array(
352 0 => 'Linear PCM, platform endian',
355 3 => 'Linear PCM, little endian',
356 4 => 'Nellymoser 16kHz mono',
357 5 => 'Nellymoser 8kHz mono',
359 7 => 'G.711A-law logarithmic PCM',
360 8 => 'G.711 mu-law logarithmic PCM',
364 12 => false, // unknown?
365 13 => false, // unknown?
367 15 => 'Device-specific sound',
369 return (isset($lookup[$id]) ?
$lookup[$id] : false);
377 public static function audioRateLookup($id) {
378 static $lookup = array(
384 return (isset($lookup[$id]) ?
$lookup[$id] : false);
392 public static function audioBitDepthLookup($id) {
393 static $lookup = array(
397 return (isset($lookup[$id]) ?
$lookup[$id] : false);
403 * @return string|false
405 public static function videoCodecLookup($id) {
406 static $lookup = array(
407 GETID3_FLV_VIDEO_H263
=> 'Sorenson H.263',
408 GETID3_FLV_VIDEO_SCREEN
=> 'Screen video',
409 GETID3_FLV_VIDEO_VP6FLV
=> 'On2 VP6',
410 GETID3_FLV_VIDEO_VP6FLV_ALPHA
=> 'On2 VP6 with alpha channel',
411 GETID3_FLV_VIDEO_SCREENV2
=> 'Screen video v2',
412 GETID3_FLV_VIDEO_H264
=> 'Sorenson H.264',
414 return (isset($lookup[$id]) ?
$lookup[$id] : false);
431 * @param string $bytes
433 public function __construct(&$bytes) {
434 $this->bytes
=& $bytes;
441 public function readByte() { // 8-bit
442 return ord(substr($this->bytes
, $this->pos++
, 1));
448 public function readInt() { // 16-bit
449 return ($this->readByte() << 8) +
$this->readByte();
455 public function readLong() { // 32-bit
456 return ($this->readByte() << 24) +
($this->readByte() << 16) +
($this->readByte() << 8) +
$this->readByte();
460 * @return float|false
462 public function readDouble() {
463 return getid3_lib
::BigEndian2Float($this->read(8));
469 public function readUTF() {
470 $length = $this->readInt();
471 return $this->read($length);
477 public function readLongUTF() {
478 $length = $this->readLong();
479 return $this->read($length);
487 public function read($length) {
488 $val = substr($this->bytes
, $this->pos
, $length);
489 $this->pos +
= $length;
496 public function peekByte() {
498 $val = $this->readByte();
506 public function peekInt() {
508 $val = $this->readInt();
516 public function peekLong() {
518 $val = $this->readLong();
524 * @return float|false
526 public function peekDouble() {
528 $val = $this->readDouble();
536 public function peekUTF() {
538 $val = $this->readUTF();
546 public function peekLongUTF() {
548 $val = $this->readLongUTF();
562 * @param AMFStream $stream
564 public function __construct(AMFStream
$stream) {
565 $this->stream
= $stream;
571 public function readData() {
574 $type = $this->stream
->readByte();
579 $value = $this->readDouble();
584 $value = $this->readBoolean();
589 $value = $this->readString();
594 $value = $this->readObject();
604 $value = $this->readMixedArray();
609 $value = $this->readArray();
614 $value = $this->readDate();
619 $value = $this->readLongString();
622 // XML (handled as string)
624 $value = $this->readXML();
627 // Typed object (handled as object)
629 $value = $this->readTypedObject();
634 $value = '(unknown or unsupported data type)';
642 * @return float|false
644 public function readDouble() {
645 return $this->stream
->readDouble();
651 public function readBoolean() {
652 return $this->stream
->readByte() == 1;
658 public function readString() {
659 return $this->stream
->readUTF();
665 public function readObject() {
666 // Get highest numerical index - ignored
667 // $highestIndex = $this->stream->readLong();
672 while ($key = $this->stream
->readUTF()) {
673 $data[$key] = $this->readData();
675 // Mixed array record ends with empty string (0x00 0x00) and 0x09
676 if (($key == '') && ($this->stream
->peekByte() == 0x09)) {
678 $this->stream
->readByte();
686 public function readMixedArray() {
687 // Get highest numerical index - ignored
688 $highestIndex = $this->stream
->readLong();
693 while ($key = $this->stream
->readUTF()) {
694 if (is_numeric($key)) {
697 $data[$key] = $this->readData();
699 // Mixed array record ends with empty string (0x00 0x00) and 0x09
700 if (($key == '') && ($this->stream
->peekByte() == 0x09)) {
702 $this->stream
->readByte();
711 public function readArray() {
712 $length = $this->stream
->readLong();
715 for ($i = 0; $i < $length; $i++
) {
716 $data[] = $this->readData();
722 * @return float|false
724 public function readDate() {
725 $timestamp = $this->stream
->readDouble();
726 $timezone = $this->stream
->readInt();
733 public function readLongString() {
734 return $this->stream
->readLongUTF();
740 public function readXML() {
741 return $this->stream
->readLongUTF();
747 public function readTypedObject() {
748 $className = $this->stream
->readUTF();
749 return $this->readObject();
753 class AVCSequenceParameterSetReader
760 public $currentBytes = 0;
761 public $currentBits = 0;
776 public function __construct($sps) {
780 public function readData() {
783 $profile = $this->getBits(8); // read profile
786 $level_idc = $this->getBits(8); // level_idc
787 $this->expGolombUe(); // seq_parameter_set_id // sps
788 $this->expGolombUe(); // log2_max_frame_num_minus4
789 $picOrderType = $this->expGolombUe(); // pic_order_cnt_type
790 if ($picOrderType == 0) {
791 $this->expGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
792 } elseif ($picOrderType == 1) {
793 $this->skipBits(1); // delta_pic_order_always_zero_flag
794 $this->expGolombSe(); // offset_for_non_ref_pic
795 $this->expGolombSe(); // offset_for_top_to_bottom_field
796 $num_ref_frames_in_pic_order_cnt_cycle = $this->expGolombUe(); // num_ref_frames_in_pic_order_cnt_cycle
797 for ($i = 0; $i < $num_ref_frames_in_pic_order_cnt_cycle; $i++
) {
798 $this->expGolombSe(); // offset_for_ref_frame[ i ]
801 $this->expGolombUe(); // num_ref_frames
802 $this->skipBits(1); // gaps_in_frame_num_value_allowed_flag
803 $pic_width_in_mbs_minus1 = $this->expGolombUe(); // pic_width_in_mbs_minus1
804 $pic_height_in_map_units_minus1 = $this->expGolombUe(); // pic_height_in_map_units_minus1
806 $frame_mbs_only_flag = $this->getBits(1); // frame_mbs_only_flag
807 if ($frame_mbs_only_flag == 0) {
808 $this->skipBits(1); // mb_adaptive_frame_field_flag
810 $this->skipBits(1); // direct_8x8_inference_flag
811 $frame_cropping_flag = $this->getBits(1); // frame_cropping_flag
813 $frame_crop_left_offset = 0;
814 $frame_crop_right_offset = 0;
815 $frame_crop_top_offset = 0;
816 $frame_crop_bottom_offset = 0;
818 if ($frame_cropping_flag) {
819 $frame_crop_left_offset = $this->expGolombUe(); // frame_crop_left_offset
820 $frame_crop_right_offset = $this->expGolombUe(); // frame_crop_right_offset
821 $frame_crop_top_offset = $this->expGolombUe(); // frame_crop_top_offset
822 $frame_crop_bottom_offset = $this->expGolombUe(); // frame_crop_bottom_offset
824 $this->skipBits(1); // vui_parameters_present_flag
827 $this->width
= (($pic_width_in_mbs_minus1 +
1) * 16) - ($frame_crop_left_offset * 2) - ($frame_crop_right_offset * 2);
828 $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);
835 public function skipBits($bits) {
836 $newBits = $this->currentBits +
$bits;
837 $this->currentBytes +
= (int)floor($newBits / 8);
838 $this->currentBits
= $newBits %
8;
844 public function getBit() {
845 $result = (getid3_lib
::BigEndian2Int(substr($this->sps
, $this->currentBytes
, 1)) >> (7 - $this->currentBits
)) & 0x01;
855 public function getBits($bits) {
857 for ($i = 0; $i < $bits; $i++
) {
858 $result = ($result << 1) +
$this->getBit();
866 public function expGolombUe() {
867 $significantBits = 0;
868 $bit = $this->getBit();
871 $bit = $this->getBit();
873 if ($significantBits > 31) {
874 // something is broken, this is an emergency escape to prevent infinite loops
878 return (1 << $significantBits) +
$this->getBits($significantBits) - 1;
884 public function expGolombSe() {
885 $result = $this->expGolombUe();
886 if (($result & 0x01) == 0) {
887 return -($result >> 1);
889 return ($result +
1) >> 1;
896 public function getWidth() {
903 public function getHeight() {
904 return $this->height
;