ï»¿<?php
/**
 * MP4Info
 * 
 * @author 		Tommy Lacroix <lacroix.tommy@gmail.com>
 * @copyright   Copyright (c) 2006-2009 Tommy Lacroix
 * @license		LGPL version 3, http://www.gnu.org/licenses/lgpl.html
 * @package 	php-mp4info
 * @link 		$HeadURL$
 *
 * Version allégée par Jean-Yves Giraud (décembre 2013) pour récupérer uniquement largeur et hauteur d'une video MP4
 *
 */

// ---

/**
 * 8.16 Sample Description Box
 * 
 * @author 		Tommy Lacroix <lacroix.tommy@gmail.com>
 * @version 	1.0.20090601	$Id$
 * @todo 		Factor this into a fullbox
 */
class MP4Info_Box_stsd extends MP4Info_Box {
	/**
	 * Values
	 *
	 * @var string{}
	 */
	protected $values = array();
	
	
	/**
	 * Constructor
	 *
	 * @author 	Tommy Lacroix <lacroix.tommy@gmail.com>
	 * @param	int					$totalSize
	 * @param	int					$boxType
	 * @param	file|string			$data
	 * @param	MP4Info_Box			$parent
	 * @return 	MP4Info_Box_stsd
	 * @access 	public
	 * @throws 	Exception
	 */		
	public function __construct($totalSize, $boxType, $data, $parent) {
		if (!self::isCompatible($boxType, $parent)) {
			throw new Exception('This box isn\'t "stsd"');
		}

		// Call ancestor
		parent::__construct($totalSize,$boxType,'',$parent);
		
		// Get data
		$data = self::getDataFrom3rd($data, $totalSize);

		// Unpack
		$ar = unpack('Cversion/C3flags/Ncount',$data);
		$this->version = $ar['version'];
		$this->flags = $ar['flags1']*65536+$ar['flags1']*256+$ar['flags1']*1;
		$this->count = $ar['count'];
		// Recurse to mdia...
		$cursor = $parent;
		while (($cursor !== false) && ($cursor->getBoxTypeStr() != 'mdia')) {
			$cursor = $cursor->getParent();
		}
		if ($cursor !== false) {
			// Then to hdlr
			$this->parent_mdia = $cursor;
			unset($cursor);
			foreach ($this->parent_mdia->children() as $cursor) {
				if ($cursor->getBoxTypeStr() == 'hdlr') {
					$this->parent_hdlr = $cursor;
					$this->type = $cursor->getHandlerType();
				}
			}
		}
		
		// Unpack SAMPLEDESCRIPTION
		$desc = substr($data,8);
		for ($i=0;$i<$this->count;$i++) {
			$ar = unpack('Nlen',$desc);
			$type = substr($desc,4,4);
			$rawinfo = substr($desc,8,$ar['len']-8);
			
			//print $type.PHP_EOL;
			switch ($type) {
				case 'avc1':
				case 'mp4v':
				case 'h264':
				case 'H264':
					$ar2 = unpack('N6dummy0/nwidth/nheight/Nhres/Nvres/Ndummy3/nframeCount/CstrLen/a31compressor/ndepth/ndummy4',$rawinfo);
					//print_r($ar2);
					$info = array(
						'width'=>$ar2['width'],
						'height'=>$ar2['height'],
						'compressor'=>$ar2['compressor'],
					);
					break;
				default:
					$info = $rawinfo;
			}
			
			$this->values[$type] = $info;
		}
	} // Constructor
	
	
	/**
	 * Check if block is compatible with class
	 *
	 * @author 	Tommy Lacroix <lacroix.tommy@gmail.com>
	 * @param 	int					$boxType
	 * @param 	MP4Info_Box			$parent
	 * @return 	bool
	 * @access 	public
	 * @static
	 */			
	static public function isCompatible($boxType, $parent) {
		return $boxType == 0x73747364;
	} // isCompatible method
	
	/**
	 * Values getter
	 *
	 * @author 	Tommy Lacroix <lacroix.tommy@gmail.com>
	 * @return string{}
	 * @access 	public
	 */
	public function getValues() {
		return $this->values;
	} // getValues method

}	