2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
8 // FLV module by Seth Kaufman <sethØwhirl-i-gig*com> //
10 // * version 0.1 (26 June 2005) //
13 // * version 0.1.1 (15 July 2005) //
14 // minor modifications by James Heinrich <info@getid3.org> //
16 // * version 0.2 (22 February 2006) //
17 // Support for On2 VP6 codec and meta information //
18 // by Steve Webster <steve.websterØfeaturecreep*com> //
20 // * version 0.3 (15 June 2006) //
21 // Modified to not read entire file into memory //
22 // by James Heinrich <info@getid3.org> //
24 // * version 0.4 (07 December 2007) //
25 // Bugfixes for incorrectly parsed FLV dimensions //
26 // and incorrect parsing of onMetaTag //
27 // by Evgeny Moysevich <moysevichØgmail*com> //
29 // * version 0.5 (21 May 2009) //
30 // Fixed parsing of audio tags and added additional codec //
31 // details. The duration is now read from onMetaTag (if //
32 // exists), rather than parsing whole file //
33 // by Nigel Barnes <ngbarnesØhotmail*com> //
35 // * version 0.6 (24 May 2009) //
36 // Better parsing of files with h264 video //
37 // by Evgeny Moysevich <moysevichØgmail*com> //
39 // * version 0.6.1 (30 May 2011) //
40 // prevent infinite loops in expGolombUe() //
42 // * version 0.7.0 (16 Jul 2013) //
43 // handle GETID3_FLV_VIDEO_VP6FLV_ALPHA //
44 // improved AVCSequenceParameterSetReader::readData() //
45 // by Xander Schouwerwou <schouwerwouØgmail*com> //
47 /////////////////////////////////////////////////////////////////
49 // module.audio-video.flv.php //
50 // module for analyzing Shockwave Flash Video files //
51 // dependencies: NONE //
53 /////////////////////////////////////////////////////////////////
55 define('GETID3_FLV_TAG_AUDIO', 8);
56 define('GETID3_FLV_TAG_VIDEO', 9);
57 define('GETID3_FLV_TAG_META', 18);
59 define('GETID3_FLV_VIDEO_H263', 2);
60 define('GETID3_FLV_VIDEO_SCREEN', 3);
61 define('GETID3_FLV_VIDEO_VP6FLV', 4);
62 define('GETID3_FLV_VIDEO_VP6FLV_ALPHA', 5);
63 define('GETID3_FLV_VIDEO_SCREENV2', 6);
64 define('GETID3_FLV_VIDEO_H264', 7);
66 define('H264_AVC_SEQUENCE_HEADER', 0);
67 define('H264_PROFILE_BASELINE', 66);
68 define('H264_PROFILE_MAIN', 77);
69 define('H264_PROFILE_EXTENDED', 88);
70 define('H264_PROFILE_HIGH', 100);
71 define('H264_PROFILE_HIGH10', 110);
72 define('H264_PROFILE_HIGH422', 122);
73 define('H264_PROFILE_HIGH444', 144);
74 define('H264_PROFILE_HIGH444_PREDICTIVE', 244);
76 class getid3_flv extends getid3_handler {
80 public $max_frames = 100000; // break out of the loop if too many frames have been scanned; only scan this many if meta frame does not contain useful duration
82 public function Analyze() {
83 $info = &$this->getid3->info;
85 $this->fseek($info['avdataoffset']);
87 $FLVdataLength = $info['avdataend'] - $info['avdataoffset'];
88 $FLVheader = $this->fread(5);
90 $info['fileformat'] = 'flv';
91 $info['flv']['header']['signature'] = substr($FLVheader, 0, 3);
92 $info['flv']['header']['version'] = getid3_lib::BigEndian2Int(substr($FLVheader, 3, 1));
93 $TypeFlags = getid3_lib::BigEndian2Int(substr($FLVheader, 4, 1));
95 if ($info['flv']['header']['signature'] != self::magic) {
96 $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes(self::magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['flv']['header']['signature']).'"';
97 unset($info['flv'], $info['fileformat']);
101 $info['flv']['header']['hasAudio'] = (bool) ($TypeFlags & 0x04);
102 $info['flv']['header']['hasVideo'] = (bool) ($TypeFlags & 0x01);
104 $FrameSizeDataLength = getid3_lib::BigEndian2Int($this->fread(4));
105 $FLVheaderFrameLength = 9;
106 if ($FrameSizeDataLength > $FLVheaderFrameLength) {
107 $this->fseek($FrameSizeDataLength - $FLVheaderFrameLength, SEEK_CUR);
110 $found_video = false;
111 $found_audio = false;
113 $found_valid_meta_playtime = false;
115 $info['flv']['framecount'] = array('total'=>0, 'audio'=>0, 'video'=>0);
116 $flv_framecount = &$info['flv']['framecount'];
117 while ((($this->ftell() + 16) < $info['avdataend']) && (($tagParseCount++ <= $this->max_frames) || !$found_valid_meta_playtime)) {
118 $ThisTagHeader = $this->fread(16);
120 $PreviousTagLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 0, 4));
121 $TagType = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 4, 1));
122 $DataLength = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 5, 3));
123 $Timestamp = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 8, 3));
124 $LastHeaderByte = getid3_lib::BigEndian2Int(substr($ThisTagHeader, 15, 1));
125 $NextOffset = $this->ftell() - 1 + $DataLength;
126 if ($Timestamp > $Duration) {
127 $Duration = $Timestamp;
130 $flv_framecount['total']++;
132 case GETID3_FLV_TAG_AUDIO:
133 $flv_framecount['audio']++;
136 $info['flv']['audio']['audioFormat'] = ($LastHeaderByte >> 4) & 0x0F;
137 $info['flv']['audio']['audioRate'] = ($LastHeaderByte >> 2) & 0x03;
138 $info['flv']['audio']['audioSampleSize'] = ($LastHeaderByte >> 1) & 0x01;
139 $info['flv']['audio']['audioType'] = $LastHeaderByte & 0x01;
143 case GETID3_FLV_TAG_VIDEO:
144 $flv_framecount['video']++;
147 $info['flv']['video']['videoCodec'] = $LastHeaderByte & 0x07;
149 $FLVvideoHeader = $this->fread(11);
151 if ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H264) {
152 // this code block contributed by: moysevichØgmail*com
154 $AVCPacketType = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 0, 1));
155 if ($AVCPacketType == H264_AVC_SEQUENCE_HEADER) {
156 // read AVCDecoderConfigurationRecord
157 $configurationVersion = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 1));
158 $AVCProfileIndication = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 1));
159 $profile_compatibility = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 1));
160 $lengthSizeMinusOne = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 1));
161 $numOfSequenceParameterSets = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 8, 1));
163 if (($numOfSequenceParameterSets & 0x1F) != 0) {
164 // there is at least one SequenceParameterSet
165 // read size of the first SequenceParameterSet
166 //$spsSize = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 9, 2));
167 $spsSize = getid3_lib::LittleEndian2Int(substr($FLVvideoHeader, 9, 2));
168 // read the first SequenceParameterSet
169 $sps = $this->fread($spsSize);
170 if (strlen($sps) == $spsSize) { // make sure that whole SequenceParameterSet was red
171 $spsReader = new AVCSequenceParameterSetReader($sps);
172 $spsReader->readData();
173 $info['video']['resolution_x'] = $spsReader->getWidth();
174 $info['video']['resolution_y'] = $spsReader->getHeight();
178 // end: moysevichØgmail*com
180 } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_H263) {
182 $PictureSizeType = (getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 3, 2))) >> 7;
183 $PictureSizeType = $PictureSizeType & 0x0007;
184 $info['flv']['header']['videoSizeType'] = $PictureSizeType;
185 switch ($PictureSizeType) {
187 //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2));
188 //$PictureSizeEnc <<= 1;
189 //$info['video']['resolution_x'] = ($PictureSizeEnc & 0xFF00) >> 8;
190 //$PictureSizeEnc = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
191 //$PictureSizeEnc <<= 1;
192 //$info['video']['resolution_y'] = ($PictureSizeEnc & 0xFF00) >> 8;
194 $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 2)) >> 7;
195 $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 5, 2)) >> 7;
196 $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFF;
197 $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFF;
201 $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 4, 3)) >> 7;
202 $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 3)) >> 7;
203 $info['video']['resolution_x'] = $PictureSizeEnc['x'] & 0xFFFF;
204 $info['video']['resolution_y'] = $PictureSizeEnc['y'] & 0xFFFF;
208 $info['video']['resolution_x'] = 352;
209 $info['video']['resolution_y'] = 288;
213 $info['video']['resolution_x'] = 176;
214 $info['video']['resolution_y'] = 144;
218 $info['video']['resolution_x'] = 128;
219 $info['video']['resolution_y'] = 96;
223 $info['video']['resolution_x'] = 320;
224 $info['video']['resolution_y'] = 240;
228 $info['video']['resolution_x'] = 160;
229 $info['video']['resolution_y'] = 120;
233 $info['video']['resolution_x'] = 0;
234 $info['video']['resolution_y'] = 0;
239 } elseif ($info['flv']['video']['videoCodec'] == GETID3_FLV_VIDEO_VP6FLV_ALPHA) {
241 /* contributed by schouwerwouØgmail*com */
242 if (!isset($info['video']['resolution_x'])) { // only when meta data isn't set
243 $PictureSizeEnc['x'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 6, 2));
244 $PictureSizeEnc['y'] = getid3_lib::BigEndian2Int(substr($FLVvideoHeader, 7, 2));
245 $info['video']['resolution_x'] = ($PictureSizeEnc['x'] & 0xFF) << 3;
246 $info['video']['resolution_y'] = ($PictureSizeEnc['y'] & 0xFF) << 3;
248 /* end schouwerwouØgmail*com */
251 if (!empty($info['video']['resolution_x']) && !empty($info['video']['resolution_y'])) {
252 $info['video']['pixel_aspect_ratio'] = $info['video']['resolution_x'] / $info['video']['resolution_y'];
258 case GETID3_FLV_TAG_META:
261 $this->fseek(-1, SEEK_CUR);
262 $datachunk = $this->fread($DataLength);
263 $AMFstream = new AMFStream($datachunk);
264 $reader = new AMFReader($AMFstream);
265 $eventName = $reader->readData();
266 $info['flv']['meta'][$eventName] = $reader->readData();
269 $copykeys = array('framerate'=>'frame_rate', 'width'=>'resolution_x', 'height'=>'resolution_y', 'audiodatarate'=>'bitrate', 'videodatarate'=>'bitrate');
270 foreach ($copykeys as $sourcekey => $destkey) {
271 if (isset($info['flv']['meta']['onMetaData'][$sourcekey])) {
272 switch ($sourcekey) {
275 $info['video'][$destkey] = intval(round($info['flv']['meta']['onMetaData'][$sourcekey]));
277 case 'audiodatarate':
278 $info['audio'][$destkey] = getid3_lib::CastAsInt(round($info['flv']['meta']['onMetaData'][$sourcekey] * 1000));
280 case 'videodatarate':
283 $info['video'][$destkey] = $info['flv']['meta']['onMetaData'][$sourcekey];
288 if (!empty($info['flv']['meta']['onMetaData']['duration'])) {
289 $found_valid_meta_playtime = true;
298 $this->fseek($NextOffset);
301 $info['playtime_seconds'] = $Duration / 1000;
302 if ($info['playtime_seconds'] > 0) {
303 $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
306 if ($info['flv']['header']['hasAudio']) {
307 $info['audio']['codec'] = self::audioFormatLookup($info['flv']['audio']['audioFormat']);
308 $info['audio']['sample_rate'] = self::audioRateLookup($info['flv']['audio']['audioRate']);
309 $info['audio']['bits_per_sample'] = self::audioBitDepthLookup($info['flv']['audio']['audioSampleSize']);
311 $info['audio']['channels'] = $info['flv']['audio']['audioType'] + 1; // 0=mono,1=stereo
312 $info['audio']['lossless'] = ($info['flv']['audio']['audioFormat'] ? false : true); // 0=uncompressed
313 $info['audio']['dataformat'] = 'flv';
315 if (!empty($info['flv']['header']['hasVideo'])) {
316 $info['video']['codec'] = self::videoCodecLookup($info['flv']['video']['videoCodec']);
317 $info['video']['dataformat'] = 'flv';
318 $info['video']['lossless'] = false;
321 // Set information from meta
322 if (!empty($info['flv']['meta']['onMetaData']['duration'])) {
323 $info['playtime_seconds'] = $info['flv']['meta']['onMetaData']['duration'];
324 $info['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
326 if (isset($info['flv']['meta']['onMetaData']['audiocodecid'])) {
327 $info['audio']['codec'] = self::audioFormatLookup($info['flv']['meta']['onMetaData']['audiocodecid']);
329 if (isset($info['flv']['meta']['onMetaData']['videocodecid'])) {
330 $info['video']['codec'] = self::videoCodecLookup($info['flv']['meta']['onMetaData']['videocodecid']);
336 public static function audioFormatLookup($id) {
337 static $lookup = array(
338 0 => 'Linear PCM, platform endian',
341 3 => 'Linear PCM, little endian',
342 4 => 'Nellymoser 16kHz mono',
343 5 => 'Nellymoser 8kHz mono',
345 7 => 'G.711A-law logarithmic PCM',
346 8 => 'G.711 mu-law logarithmic PCM',
350 12 => false, // unknown?
351 13 => false, // unknown?
353 15 => 'Device-specific sound',
355 return (isset($lookup[$id]) ? $lookup[$id] : false);
358 public static function audioRateLookup($id) {
359 static $lookup = array(
365 return (isset($lookup[$id]) ? $lookup[$id] : false);
368 public static function audioBitDepthLookup($id) {
369 static $lookup = array(
373 return (isset($lookup[$id]) ? $lookup[$id] : false);
376 public static function videoCodecLookup($id) {
377 static $lookup = array(
378 GETID3_FLV_VIDEO_H263 => 'Sorenson H.263',
379 GETID3_FLV_VIDEO_SCREEN => 'Screen video',
380 GETID3_FLV_VIDEO_VP6FLV => 'On2 VP6',
381 GETID3_FLV_VIDEO_VP6FLV_ALPHA => 'On2 VP6 with alpha channel',
382 GETID3_FLV_VIDEO_SCREENV2 => 'Screen video v2',
383 GETID3_FLV_VIDEO_H264 => 'Sorenson H.264',
385 return (isset($lookup[$id]) ? $lookup[$id] : false);
393 public function __construct(&$bytes) {
394 $this->bytes =& $bytes;
398 public function readByte() {
399 return getid3_lib::BigEndian2Int(substr($this->bytes, $this->pos++, 1));
402 public function readInt() {
403 return ($this->readByte() << 8) + $this->readByte();
406 public function readLong() {
407 return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte();
410 public function readDouble() {
411 return getid3_lib::BigEndian2Float($this->read(8));
414 public function readUTF() {
415 $length = $this->readInt();
416 return $this->read($length);
419 public function readLongUTF() {
420 $length = $this->readLong();
421 return $this->read($length);
424 public function read($length) {
425 $val = substr($this->bytes, $this->pos, $length);
426 $this->pos += $length;
430 public function peekByte() {
432 $val = $this->readByte();
437 public function peekInt() {
439 $val = $this->readInt();
444 public function peekLong() {
446 $val = $this->readLong();
451 public function peekDouble() {
453 $val = $this->readDouble();
458 public function peekUTF() {
460 $val = $this->readUTF();
465 public function peekLongUTF() {
467 $val = $this->readLongUTF();
476 public function __construct(&$stream) {
477 $this->stream =& $stream;
480 public function readData() {
483 $type = $this->stream->readByte();
488 $value = $this->readDouble();
493 $value = $this->readBoolean();
498 $value = $this->readString();
503 $value = $this->readObject();
513 $value = $this->readMixedArray();
518 $value = $this->readArray();
523 $value = $this->readDate();
528 $value = $this->readLongString();
531 // XML (handled as string)
533 $value = $this->readXML();
536 // Typed object (handled as object)
538 $value = $this->readTypedObject();
543 $value = '(unknown or unsupported data type)';
550 public function readDouble() {
551 return $this->stream->readDouble();
554 public function readBoolean() {
555 return $this->stream->readByte() == 1;
558 public function readString() {
559 return $this->stream->readUTF();
562 public function readObject() {
563 // Get highest numerical index - ignored
564 // $highestIndex = $this->stream->readLong();
568 while ($key = $this->stream->readUTF()) {
569 $data[$key] = $this->readData();
571 // Mixed array record ends with empty string (0x00 0x00) and 0x09
572 if (($key == '') && ($this->stream->peekByte() == 0x09)) {
574 $this->stream->readByte();
579 public function readMixedArray() {
580 // Get highest numerical index - ignored
581 $highestIndex = $this->stream->readLong();
585 while ($key = $this->stream->readUTF()) {
586 if (is_numeric($key)) {
589 $data[$key] = $this->readData();
591 // Mixed array record ends with empty string (0x00 0x00) and 0x09
592 if (($key == '') && ($this->stream->peekByte() == 0x09)) {
594 $this->stream->readByte();
600 public function readArray() {
601 $length = $this->stream->readLong();
604 for ($i = 0; $i < $length; $i++) {
605 $data[] = $this->readData();
610 public function readDate() {
611 $timestamp = $this->stream->readDouble();
612 $timezone = $this->stream->readInt();
616 public function readLongString() {
617 return $this->stream->readLongUTF();
620 public function readXML() {
621 return $this->stream->readLongUTF();
624 public function readTypedObject() {
625 $className = $this->stream->readUTF();
626 return $this->readObject();
630 class AVCSequenceParameterSetReader {
633 public $currentBytes = 0;
634 public $currentBits = 0;
638 public function __construct($sps) {
642 public function readData() {
645 $profile = $this->getBits(8); // read profile
648 $level_idc = $this->getBits(8); // level_idc
649 $this->expGolombUe(); // seq_parameter_set_id // sps
650 $this->expGolombUe(); // log2_max_frame_num_minus4
651 $picOrderType = $this->expGolombUe(); // pic_order_cnt_type
652 if ($picOrderType == 0) {
653 $this->expGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
654 } elseif ($picOrderType == 1) {
655 $this->skipBits(1); // delta_pic_order_always_zero_flag
656 $this->expGolombSe(); // offset_for_non_ref_pic
657 $this->expGolombSe(); // offset_for_top_to_bottom_field
658 $num_ref_frames_in_pic_order_cnt_cycle = $this->expGolombUe(); // num_ref_frames_in_pic_order_cnt_cycle
659 for ($i = 0; $i < $num_ref_frames_in_pic_order_cnt_cycle; $i++) {
660 $this->expGolombSe(); // offset_for_ref_frame[ i ]
663 $this->expGolombUe(); // num_ref_frames
664 $this->skipBits(1); // gaps_in_frame_num_value_allowed_flag
665 $pic_width_in_mbs_minus1 = $this->expGolombUe(); // pic_width_in_mbs_minus1
666 $pic_height_in_map_units_minus1 = $this->expGolombUe(); // pic_height_in_map_units_minus1
668 $frame_mbs_only_flag = $this->getBits(1); // frame_mbs_only_flag
669 if ($frame_mbs_only_flag == 0) {
670 $this->skipBits(1); // mb_adaptive_frame_field_flag
672 $this->skipBits(1); // direct_8x8_inference_flag
673 $frame_cropping_flag = $this->getBits(1); // frame_cropping_flag
675 $frame_crop_left_offset = 0;
676 $frame_crop_right_offset = 0;
677 $frame_crop_top_offset = 0;
678 $frame_crop_bottom_offset = 0;
680 if ($frame_cropping_flag) {
681 $frame_crop_left_offset = $this->expGolombUe(); // frame_crop_left_offset
682 $frame_crop_right_offset = $this->expGolombUe(); // frame_crop_right_offset
683 $frame_crop_top_offset = $this->expGolombUe(); // frame_crop_top_offset
684 $frame_crop_bottom_offset = $this->expGolombUe(); // frame_crop_bottom_offset
686 $this->skipBits(1); // vui_parameters_present_flag
689 $this->width = (($pic_width_in_mbs_minus1 + 1) * 16) - ($frame_crop_left_offset * 2) - ($frame_crop_right_offset * 2);
690 $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);
694 public function skipBits($bits) {
695 $newBits = $this->currentBits + $bits;
696 $this->currentBytes += (int)floor($newBits / 8);
697 $this->currentBits = $newBits % 8;
700 public function getBit() {
701 $result = (getid3_lib::BigEndian2Int(substr($this->sps, $this->currentBytes, 1)) >> (7 - $this->currentBits)) & 0x01;
706 public function getBits($bits) {
708 for ($i = 0; $i < $bits; $i++) {
709 $result = ($result << 1) + $this->getBit();
714 public function expGolombUe() {
715 $significantBits = 0;
716 $bit = $this->getBit();
719 $bit = $this->getBit();
721 if ($significantBits > 31) {
722 // something is broken, this is an emergency escape to prevent infinite loops
726 return (1 << $significantBits) + $this->getBits($significantBits) - 1;
729 public function expGolombSe() {
730 $result = $this->expGolombUe();
731 if (($result & 0x01) == 0) {
732 return -($result >> 1);
734 return ($result + 1) >> 1;
738 public function getWidth() {
742 public function getHeight() {
743 return $this->height;