AVProWindowsMediaFormatConverter.cs 14.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
// Support for using externally created native textures, from Unity 4.2 upwards
#if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_5
	#define AVPROWINDOWSMEDIA_UNITYFEATURE_EXTERNALTEXTURES
#endif

using UnityEngine;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;

//-----------------------------------------------------------------------------
// Copyright 2012-2016 RenderHeads Ltd.  All rights reserved.
//-----------------------------------------------------------------------------

public class AVProWindowsMediaFormatConverter : System.IDisposable
{
	private int _movieHandle;
	
	// Format conversion and texture output
	private Texture2D _rawTexture;
	private bool _isExternalTexture;
	private RenderTexture _finalTexture;
	private Texture _outputTexture;
	private Material _conversionMaterial;
	private int _usedTextureWidth, _usedTextureHeight;
	private Vector4 _uv;	
	private int _lastFrameUploaded = -1;
	
	// Conversion params
	private int _width;
	private int _height;
	private bool _flipX;
	private bool _flipY;
	private AVProWindowsMediaPlugin.VideoFrameFormat _sourceVideoFormat;
	private bool _useBT709;
	private bool _requiresTextureCrop;
	private bool _requiresConversion;

	public bool RequiresConversion
	{
		get { return _requiresTextureCrop; } 
	}

	public Texture OutputTexture
	{
		get { return _outputTexture; }
	}
	
	public int DisplayFrame
	{
		get { return _lastFrameUploaded; }
	}

	public bool	ValidPicture { get; private set; }
	
	public void Reset()
	{
		ValidPicture = false;
		_lastFrameUploaded = -1;
	}

	public Material GetConversionMaterial()
	{
		if (!_requiresConversion)
			return _conversionMaterial;
		return null;
	}
	
	public bool Build(int movieHandle, int width, int height, AVProWindowsMediaPlugin.VideoFrameFormat format, bool useBT709, bool flipX, bool flipY, FilterMode filterMode, TextureWrapMode wrapMode)
	{
		Reset();

		_outputTexture = null;
		_movieHandle = movieHandle;

		_width = width;
		_height = height;
		_sourceVideoFormat = format;
		_flipX = flipX;
		_flipY = flipY;
		_useBT709 = useBT709;
		
#if AVPROWINDOWSMEDIA_UNITYFEATURE_EXTERNALTEXTURES
		if (AVProWindowsMediaManager.Instance._useExternalTextures)
			CreateExternalTexture();
		else
#endif
			CreateTexture();

		if (_rawTexture != null)
		{
			_requiresConversion = false;
			_requiresTextureCrop = (_usedTextureWidth != _rawTexture.width || _usedTextureHeight != _rawTexture.height);
			if (_requiresTextureCrop)
			{
				SetFlip(_flipX, _flipY);
				_requiresConversion = true;
			}

			if (!_isExternalTexture)
				AVProWindowsMediaPlugin.SetTexturePointer(_movieHandle, _rawTexture.GetNativeTexturePtr());

			if (!_requiresConversion)
			{
				bool isDX11 = SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 11");

				if (_flipX || _flipY)
				{
					_requiresConversion = true;
				}
				else if (_sourceVideoFormat == AVProWindowsMediaPlugin.VideoFrameFormat.RAW_BGRA32 && isDX11)
				{
#if UNITY_5
					// DX11 has red and blue channels swapped around
					if (!SystemInfo.SupportsTextureFormat(TextureFormat.BGRA32))
						_requiresConversion = true;
#else
                    _requiresConversion = true;
#endif
				}
				else if (_sourceVideoFormat != AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGB &&
					 	_sourceVideoFormat != AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGBA &&
						_sourceVideoFormat != AVProWindowsMediaPlugin.VideoFrameFormat.RAW_BGRA32)
				{
					_requiresConversion = true;
				}
			}

			if (_requiresConversion)
			{
				if (CreateMaterial())
				{
					CreateRenderTexture();
					_outputTexture = _finalTexture;

					_conversionMaterial.mainTexture = _rawTexture;
					SetFlip(_flipX, _flipY);
					bool formatIs422 = (_sourceVideoFormat != AVProWindowsMediaPlugin.VideoFrameFormat.RAW_BGRA32);
					if (formatIs422)
					{
						_conversionMaterial.SetFloat("_TextureWidth", _finalTexture.width);
					}
				}
			}
			else
			{

				bool formatIs422 = (_sourceVideoFormat != AVProWindowsMediaPlugin.VideoFrameFormat.RAW_BGRA32 &&
				                    _sourceVideoFormat != AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGB &&
				                    _sourceVideoFormat != AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGBA);
				if (formatIs422)
				{
					CreateMaterial();
					_conversionMaterial.SetFloat("_TextureWidth", _width);
					_rawTexture.filterMode = FilterMode.Point;
				}
				else
				{
					_rawTexture.filterMode = FilterMode.Bilinear;
				}
				//_rawTexture.wrapMode = TextureWrapMode.Repeat;
				_outputTexture = _rawTexture;
			}
		}

		if (_outputTexture != null)
		{
			_outputTexture.filterMode = filterMode;
			_outputTexture.wrapMode = wrapMode;
		}
		
		return (_outputTexture != null);
	}
	
	public bool Update()
	{
		bool result = UpdateTexture();
		if (_requiresConversion)
		{
			if (result)
			{
				DoFormatConversion();
			}
			else
			{
				if (_finalTexture != null && !_finalTexture.IsCreated())
				{
					Reset();
				}
			}
		}
		else
		{
			if (result)
				ValidPicture = true;
		}
		return result;
	}
	
	private bool UpdateTexture()
	{
		bool result = false;
	
		// We update all the textures from AVProQuickTimeManager.Update()
		// so just check if the update was done
		int lastFrameUploaded = AVProWindowsMediaPlugin.GetLastFrameUploaded(_movieHandle);
		if (_lastFrameUploaded != lastFrameUploaded)
		{			
			_lastFrameUploaded = lastFrameUploaded;
			result = true;
		}

		return result;
	}
	
	public void Dispose()
	{
		ValidPicture = false;
		_width = _height = 0;
		
		if (_conversionMaterial != null)
		{
			_conversionMaterial.mainTexture = null;
#if UNITY_EDITOR
			Material.DestroyImmediate(_conversionMaterial);
#else
            Material.Destroy(_conversionMaterial);
#endif
			_conversionMaterial = null;
		}

		_outputTexture = null;
		
		if (_finalTexture != null)
		{
			RenderTexture.ReleaseTemporary(_finalTexture);
			_finalTexture = null;
		}
		
		if (_rawTexture != null)
		{			
			if (!_isExternalTexture)
			{
#if UNITY_EDITOR
				Texture2D.DestroyImmediate(_rawTexture);
#else
				Texture2D.Destroy(_rawTexture);
#endif
			}
			_rawTexture = null;
		}
	}

	private bool CreateMaterial()
	{	
		Shader shader = AVProWindowsMediaManager.Instance.GetPixelConversionShader(_sourceVideoFormat, _useBT709);
		if (shader)
		{
			if (_conversionMaterial != null)
			{
				if (_conversionMaterial.shader != shader)
				{
					Material.Destroy(_conversionMaterial);
					_conversionMaterial = null;
				}
			}
			
			if (_conversionMaterial == null)
			{
				_conversionMaterial = new Material(shader);
				_conversionMaterial.name = "AVProWindowsMedia-Material";
			}
		}
		
		return (_conversionMaterial != null);
	}	

#if AVPROWINDOWSMEDIA_UNITYFEATURE_EXTERNALTEXTURES
	private void CreateExternalTexture()
	{
		System.IntPtr texPtr = AVProWindowsMediaPlugin.GetTexturePointer(_movieHandle);
		if (texPtr != System.IntPtr.Zero)
		{
			int texWidth = _width;
			TextureFormat textureFormat = TextureFormat.ARGB32;
			switch (_sourceVideoFormat)
			{
			case AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGBA:
			case AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGB_HQ:
				textureFormat = TextureFormat.DXT5;
				break;
			case AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGB:
				textureFormat = TextureFormat.DXT1;
				break;
			case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_422_YUY2:
			case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_422_UYVY:
			case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_422_YVYU:
			case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_422_HDYC:
				texWidth = _width / 2;
				break;
			}
			
			_usedTextureWidth = texWidth;
			_usedTextureHeight = _height;
			_rawTexture = Texture2D.CreateExternalTexture(texWidth, _height, textureFormat, false, false, texPtr);

			_rawTexture.wrapMode = TextureWrapMode.Clamp;
			_rawTexture.filterMode = FilterMode.Point;
			_rawTexture.name = "AVProWindowsMedia-RawExternal";

			_isExternalTexture = true;
		}
	}
#endif
	
	private void CreateTexture()
	{
		_usedTextureWidth = _width;
		_usedTextureHeight = _height;
		
		// Calculate texture size and format
		int textureWidth = _usedTextureWidth;
		int textureHeight = _usedTextureHeight;
		TextureFormat textureFormat = TextureFormat.RGBA32;


		switch (_sourceVideoFormat)
		{
		case AVProWindowsMediaPlugin.VideoFrameFormat.RAW_BGRA32:
			textureFormat = TextureFormat.RGBA32;
            if (SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 11"))
            {
#if UNITY_5
			    // DX11 has red and blue channels swapped around
			    if (SystemInfo.SupportsTextureFormat(TextureFormat.BGRA32))
				    textureFormat = TextureFormat.BGRA32;
#endif                
            }
			break;
		case AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGBA:
		case AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGB_HQ:
			textureFormat = TextureFormat.DXT5;
			break;
		case AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGB:
			textureFormat = TextureFormat.DXT1;
			break;
		case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_422_YUY2:
		case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_422_UYVY:
		case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_422_HDYC:
		case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_422_YVYU:
		case AVProWindowsMediaPlugin.VideoFrameFormat.YUV_420_NV12:
			textureFormat = TextureFormat.RGBA32;
            if (SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 11"))
            {
#if UNITY_5
			    // DX11 has red and blue channels swapped around
			    if (SystemInfo.SupportsTextureFormat(TextureFormat.BGRA32))
				    textureFormat = TextureFormat.BGRA32;
#endif
            }
            _usedTextureWidth /= 2;	// YCbCr422 modes need half width
			textureWidth = _usedTextureWidth;
			break;
		}

		bool requiresPOT = (SystemInfo.npotSupport == NPOTSupport.None);

		// If the texture isn't a power of 2
		if (requiresPOT)
		{
			// We use a power-of-2 texture as Unity makes these internally anyway and not doing it seems to break things for texture updates
			if (!Mathf.IsPowerOfTwo(_width) || !Mathf.IsPowerOfTwo(_height))
			{
				textureWidth = Mathf.NextPowerOfTwo(textureWidth);
				textureHeight = Mathf.NextPowerOfTwo(textureHeight);
			}
		}
				
		// Create texture that stores the initial raw frame
		// If there is already a texture, only destroy it if it's not the same requirements
		if (_rawTexture != null)
		{
			if (_rawTexture.width != textureWidth || 
			    _rawTexture.height != textureHeight ||
			    _rawTexture.format != textureFormat)
			{
				Texture2D.Destroy(_rawTexture);
				_rawTexture = null;
			}
		}

		if (_rawTexture == null)
		{
			bool isLinear = true;
			if (!_requiresConversion)
			{
				if (AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGBA == _sourceVideoFormat ||
					AVProWindowsMediaPlugin.VideoFrameFormat.Hap_RGB == _sourceVideoFormat ||
					AVProWindowsMediaPlugin.VideoFrameFormat.RAW_BGRA32 == _sourceVideoFormat)
				{
					isLinear = false;
				}
			}

			_rawTexture = new Texture2D(textureWidth, textureHeight, textureFormat, false, isLinear);
			_rawTexture.wrapMode = TextureWrapMode.Clamp;
			_rawTexture.filterMode = FilterMode.Point;
			_rawTexture.name = "AVProWindowsMedia-RawTexture";
			_rawTexture.Apply(false, true);
			_isExternalTexture = false;
		}
	}
	
	private void CreateRenderTexture()
	{	
		// Create RenderTexture for post transformed frames
		// If there is already a renderTexture, only destroy it smaller than desired size
		if (_finalTexture != null)
		{
			if (_finalTexture.width != _width || _finalTexture.height != _height)
			{
				RenderTexture.ReleaseTemporary(_finalTexture);
				_finalTexture = null;
			}
		}

		if (_finalTexture == null)
		{
			ValidPicture = false;
			_finalTexture = RenderTexture.GetTemporary(_width, _height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
			_finalTexture.wrapMode = TextureWrapMode.Clamp;
			_finalTexture.filterMode = FilterMode.Bilinear;
			_finalTexture.name = "AVProWindowsMedia-FinalTexture";
			_finalTexture.Create();
		}
	}
	
	private void DoFormatConversion()
	{
		if (_finalTexture == null)
			return;

		_finalTexture.DiscardContents();

		RenderTexture prev = RenderTexture.active;
		if (!_requiresTextureCrop)
		{
			Graphics.Blit(_rawTexture, _finalTexture, _conversionMaterial, 0);
		}
		else
		{
			RenderTexture.active = _finalTexture;

			_conversionMaterial.SetPass(0);

			GL.PushMatrix();
			GL.LoadOrtho();
			DrawQuad(_uv);
			GL.PopMatrix();

		}
		RenderTexture.active = prev;

		ValidPicture = true;
	}

	private void SetFlip(bool flipX, bool flipY)
	{
		_flipX = flipX;
		_flipY = flipY;

		if (_requiresTextureCrop)
		{
			CreateUVs(_flipX, _flipY);
		}
		else
		{
			if (_conversionMaterial != null)
			{
				// Flip and then offset back to get back to normalised range
				Vector2 scale = new Vector2(1f, 1f);
				Vector2 offset = new Vector2(0f, 0f);
				if (_flipX)
				{
					scale = new Vector2(-1f, scale.y);
					offset = new Vector2(1f, offset.y);
				}
				if (_flipY)
				{
					scale = new Vector2(scale.x, -1f);
					offset = new Vector2(offset.x, 1f);
				}

				_conversionMaterial.mainTextureScale = scale;
				_conversionMaterial.mainTextureOffset = offset;
				// Since Unity 5.3 Graphics.Blit ignores mainTextureOffset/Scale
#if UNITY_5 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2
				_conversionMaterial.SetVector("_MainTex_ST2", new Vector4(scale.x, scale.y, offset.x, offset.y));
#endif
			}
		}
	}
	
	private void CreateUVs(bool invertX, bool invertY)
	{				
		float x1, x2;
		float y1, y2;
		if (invertX)
		{
			x1 = 1.0f; x2 = 0.0f;
		}
		else
		{
			x1 = 0.0f; x2 = 1.0f;
		}
		if (invertY)
		{
			y1 = 1.0f; y2 = 0.0f;
		}
		else
		{
			y1 = 0.0f; y2 = 1.0f;
		}
		
		// Alter UVs if we're only using a portion of the texture
		if (_usedTextureWidth != _rawTexture.width)
		{
			float xd = _usedTextureWidth / (float)_rawTexture.width;
			x1 *= xd; x2 *= xd;
		}
		if (_usedTextureHeight != _rawTexture.height)
		{
			float yd = _usedTextureHeight / (float)_rawTexture.height;
			y1 *= yd; y2 *= yd;
		}
			
		_uv = new Vector4(x1, y1, x2, y2);
	}
	
	private static void DrawQuad(Vector4 uv)
	{
		GL.Begin(GL.QUADS);
		
		GL.TexCoord2(uv.x, uv.y);
		GL.Vertex3(0.0f, 0.0f, 0.1f);
		
		GL.TexCoord2(uv.z, uv.y);
		GL.Vertex3(1.0f, 0.0f, 0.1f);
		
		GL.TexCoord2(uv.z, uv.w);		
		GL.Vertex3(1.0f, 1.0f, 0.1f);
		
		GL.TexCoord2(uv.x, uv.w);
		GL.Vertex3(0.0f, 1.0f, 0.1f);
		
		GL.End();
	}	
}