[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.audio-video.swf.php
1 <?php
2
3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
10 // //
11 // module.audio-video.swf.php //
12 // module for analyzing Shockwave Flash files //
13 // dependencies: NONE //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17
18 class getid3_swf extends getid3_handler
19 {
20 public $ReturnAllTagData = false;
21
22 /**
23 * @return bool
24 */
25 public function Analyze() {
26 $info = &$this->getid3->info;
27
28 $info['fileformat'] = 'swf';
29 $info['video']['dataformat'] = 'swf';
30
31 // http://www.openswf.org/spec/SWFfileformat.html
32
33 $this->fseek($info['avdataoffset']);
34
35 $SWFfileData = $this->fread($info['avdataend'] - $info['avdataoffset']); // 8 + 2 + 2 + max(9) bytes NOT including Frame_Size RECT data
36
37 $info['swf']['header']['signature'] = substr($SWFfileData, 0, 3);
38 switch ($info['swf']['header']['signature']) {
39 case 'FWS':
40 $info['swf']['header']['compressed'] = false;
41 break;
42
43 case 'CWS':
44 $info['swf']['header']['compressed'] = true;
45 break;
46
47 default:
48 $this->error('Expecting "FWS" or "CWS" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['swf']['header']['signature']).'"');
49 unset($info['swf']);
50 unset($info['fileformat']);
51 return false;
52 break;
53 }
54 $info['swf']['header']['version'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 3, 1));
55 $info['swf']['header']['length'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 4, 4));
56
57 if ($info['swf']['header']['compressed']) {
58 $SWFHead = substr($SWFfileData, 0, 8);
59 $SWFfileData = substr($SWFfileData, 8);
60 if ($decompressed = @gzuncompress($SWFfileData)) {
61 $SWFfileData = $SWFHead.$decompressed;
62 } else {
63 $this->error('Error decompressing compressed SWF data ('.strlen($SWFfileData).' bytes compressed, should be '.($info['swf']['header']['length'] - 8).' bytes uncompressed)');
64 return false;
65 }
66 }
67
68 $FrameSizeBitsPerValue = (ord(substr($SWFfileData, 8, 1)) & 0xF8) >> 3;
69 $FrameSizeDataLength = ceil((5 + (4 * $FrameSizeBitsPerValue)) / 8);
70 $FrameSizeDataString = str_pad(decbin(ord(substr($SWFfileData, 8, 1)) & 0x07), 3, '0', STR_PAD_LEFT);
71 for ($i = 1; $i < $FrameSizeDataLength; $i++) {
72 $FrameSizeDataString .= str_pad(decbin(ord(substr($SWFfileData, 8 + $i, 1))), 8, '0', STR_PAD_LEFT);
73 }
74 list($X1, $X2, $Y1, $Y2) = explode("\n", wordwrap($FrameSizeDataString, $FrameSizeBitsPerValue, "\n", 1));
75 $info['swf']['header']['frame_width'] = getid3_lib::Bin2Dec($X2);
76 $info['swf']['header']['frame_height'] = getid3_lib::Bin2Dec($Y2);
77
78 // http://www-lehre.informatik.uni-osnabrueck.de/~fbstark/diplom/docs/swf/Flash_Uncovered.htm
79 // Next in the header is the frame rate, which is kind of weird.
80 // It is supposed to be stored as a 16bit integer, but the first byte
81 // (or last depending on how you look at it) is completely ignored.
82 // Example: 0x000C -> 0x0C -> 12 So the frame rate is 12 fps.
83
84 // Byte at (8 + $FrameSizeDataLength) is always zero and ignored
85 $info['swf']['header']['frame_rate'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 9 + $FrameSizeDataLength, 1));
86 $info['swf']['header']['frame_count'] = getid3_lib::LittleEndian2Int(substr($SWFfileData, 10 + $FrameSizeDataLength, 2));
87
88 $info['video']['frame_rate'] = $info['swf']['header']['frame_rate'];
89 $info['video']['resolution_x'] = intval(round($info['swf']['header']['frame_width'] / 20));
90 $info['video']['resolution_y'] = intval(round($info['swf']['header']['frame_height'] / 20));
91 $info['video']['pixel_aspect_ratio'] = (float) 1;
92
93 if (($info['swf']['header']['frame_count'] > 0) && ($info['swf']['header']['frame_rate'] > 0)) {
94 $info['playtime_seconds'] = $info['swf']['header']['frame_count'] / $info['swf']['header']['frame_rate'];
95 }
96 //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
97
98
99 // SWF tags
100
101 $CurrentOffset = 12 + $FrameSizeDataLength;
102 $SWFdataLength = strlen($SWFfileData);
103
104 while ($CurrentOffset < $SWFdataLength) {
105 //echo __LINE__.'='.number_format(microtime(true) - $start_time, 3).'<br>';
106
107 $TagIDTagLength = getid3_lib::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 2));
108 $TagID = ($TagIDTagLength & 0xFFFC) >> 6;
109 $TagLength = ($TagIDTagLength & 0x003F);
110 $CurrentOffset += 2;
111 if ($TagLength == 0x3F) {
112 $TagLength = getid3_lib::LittleEndian2Int(substr($SWFfileData, $CurrentOffset, 4));
113 $CurrentOffset += 4;
114 }
115
116 unset($TagData);
117 $TagData['offset'] = $CurrentOffset;
118 $TagData['size'] = $TagLength;
119 $TagData['id'] = $TagID;
120 $TagData['data'] = substr($SWFfileData, $CurrentOffset, $TagLength);
121 switch ($TagID) {
122 case 0: // end of movie
123 break 2;
124
125 case 9: // Set background color
126 //$info['swf']['tags'][] = $TagData;
127 $info['swf']['bgcolor'] = strtoupper(str_pad(dechex(getid3_lib::BigEndian2Int($TagData['data'])), 6, '0', STR_PAD_LEFT));
128 break;
129
130 default:
131 if ($this->ReturnAllTagData) {
132 $info['swf']['tags'][] = $TagData;
133 }
134 break;
135 }
136
137 $CurrentOffset += $TagLength;
138 }
139
140 return true;
141 }
142
143 }