123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
<?php
if (!defined('GETID3_INCLUDEPATH')) {
exit;
}
class getid3_gif extends getid3_handler
{
public function Analyze() {
$info = &$this->getid3->info;
$info['fileformat'] = 'gif';
$info['video']['dataformat'] = 'gif';
$info['video']['lossless'] = true;
$info['video']['pixel_aspect_ratio'] = (float) 1;
$this->fseek($info['avdataoffset']);
$GIFheader = $this->fread(13);
$offset = 0;
$info['gif']['header']['raw']['identifier'] = substr($GIFheader, $offset, 3);
$offset += 3;
$magic = 'GIF';
if ($info['gif']['header']['raw']['identifier'] != $magic) {
$this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['gif']['header']['raw']['identifier']).'"');
unset($info['fileformat']);
unset($info['gif']);
return false;
}
$info['gif']['header']['raw']['version'] = substr($GIFheader, $offset, 3);
$offset += 3;
$info['gif']['header']['raw']['width'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
$offset += 2;
$info['gif']['header']['raw']['height'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
$offset += 2;
$info['gif']['header']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
$offset += 1;
$info['gif']['header']['raw']['bg_color_index'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
$offset += 1;
$info['gif']['header']['raw']['aspect_ratio'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
$offset += 1;
$info['video']['resolution_x'] = $info['gif']['header']['raw']['width'];
$info['video']['resolution_y'] = $info['gif']['header']['raw']['height'];
$info['gif']['version'] = $info['gif']['header']['raw']['version'];
$info['gif']['header']['flags']['global_color_table'] = (bool) ($info['gif']['header']['raw']['flags'] & 0x80);
if ($info['gif']['header']['raw']['flags'] & 0x80) {
$info['gif']['header']['bits_per_pixel'] = 3 * ((($info['gif']['header']['raw']['flags'] & 0x70) >> 4) + 1);
} else {
$info['gif']['header']['bits_per_pixel'] = 0;
}
$info['gif']['header']['flags']['global_color_sorted'] = (bool) ($info['gif']['header']['raw']['flags'] & 0x40);
if ($info['gif']['header']['flags']['global_color_table']) {
$info['gif']['header']['global_color_size'] = pow(2, ($info['gif']['header']['raw']['flags'] & 0x07) + 1);
$info['video']['bits_per_sample'] = ($info['gif']['header']['raw']['flags'] & 0x07) + 1;
} else {
$info['gif']['header']['global_color_size'] = 0;
}
if ($info['gif']['header']['raw']['aspect_ratio'] != 0) {
$info['gif']['header']['aspect_ratio'] = ($info['gif']['header']['raw']['aspect_ratio'] + 15) / 64;
}
if ($info['gif']['header']['flags']['global_color_table']) {
$GIFcolorTable = $this->fread(3 * $info['gif']['header']['global_color_size']);
if ($this->getid3->option_extra_info) {
$offset = 0;
for ($i = 0; $i < $info['gif']['header']['global_color_size']; $i++) {
$red = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
$green = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
$blue = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
$info['gif']['global_color_table'][$i] = (($red << 16) | ($green << 8) | ($blue));
$info['gif']['global_color_table_rgb'][$i] = sprintf('%02X%02X%02X', $red, $green, $blue);
}
}
}
$info['gif']['animation']['animated'] = false;
while (!feof($this->getid3->fp)) {
$NextBlockTest = $this->fread(1);
switch ($NextBlockTest) {
case '!':
$ExtensionBlockData = $NextBlockTest.$this->fread(2);
$ExtensionBlock = array();
$ExtensionBlock['function_code'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlockData, 1, 1));
$ExtensionBlock['byte_length'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlockData, 2, 1));
$ExtensionBlock['data'] = (($ExtensionBlock['byte_length'] > 0) ? $this->fread($ExtensionBlock['byte_length']) : null);
switch ($ExtensionBlock['function_code']) {
case 0xFF:
if ($ExtensionBlock['byte_length'] != 11) {
$this->warning('Expected block size of the Application Extension is 11 bytes, found '.$ExtensionBlock['byte_length'].' at offset '.$this->ftell());
break;
}
if (substr($ExtensionBlock['data'], 0, 11) !== 'NETSCAPE2.0'
&& substr($ExtensionBlock['data'], 0, 11) !== 'ANIMEXTS1.0'
) {
$this->warning('Ignoring unsupported Application Extension '.substr($ExtensionBlock['data'], 0, 11));
break;
}
$ExtensionBlock['data'] .= $this->fread(4);
if (substr($ExtensionBlock['data'], 11, 2) == "\x03\x01") {
$info['gif']['animation']['animated'] = true;
$info['gif']['animation']['loop_count'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlock['data'], 13, 2));
} else {
$this->warning('Expecting 03 01 at offset '.($this->ftell() - 4).', found "'.getid3_lib::PrintHexBytes(substr($ExtensionBlock['data'], 11, 2)).'"');
}
break;
}
if ($this->getid3->option_extra_info) {
$info['gif']['extension_blocks'][] = $ExtensionBlock;
}
break;
case ';':
$info['gif']['terminator_offset'] = $this->ftell() - 1;
break;
default:
break;
}
}
return true;
}
public function GetLSBits($bits) {
static $bitbuffer = '';
while (strlen($bitbuffer) < $bits) {
$bitbuffer = str_pad(decbin(ord($this->fread(1))), 8, '0', STR_PAD_LEFT).$bitbuffer;
}
$value = bindec(substr($bitbuffer, 0 - $bits));
$bitbuffer = substr($bitbuffer, 0, 0 - $bits);
return $value;
}
}