J2KDecoderModule.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Reflection;
  29. using System.Threading;
  30. using System.Collections.Generic;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Imaging;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.Environment.Scenes;
  37. namespace OpenSim.Region.Environment.Modules.Agent.TextureSender
  38. {
  39. public class J2KDecoderModule : IRegionModule, IJ2KDecoder
  40. {
  41. #region IRegionModule Members
  42. private static readonly ILog m_log
  43. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. /// <summary>
  45. /// Cached Decoded Layers
  46. /// </summary>
  47. private readonly Dictionary<UUID, OpenJPEG.J2KLayerInfo[]> m_cacheddecode = new Dictionary<UUID, OpenJPEG.J2KLayerInfo[]>();
  48. /// <summary>
  49. /// List of client methods to notify of results of decode
  50. /// </summary>
  51. private readonly Dictionary<UUID, List<DecodedCallback>> m_notifyList = new Dictionary<UUID, List<DecodedCallback>>();
  52. public void Initialise(Scene scene, IConfigSource source)
  53. {
  54. scene.RegisterModuleInterface<IJ2KDecoder>(this);
  55. }
  56. public void PostInitialise()
  57. {
  58. }
  59. public void Close()
  60. {
  61. }
  62. public string Name
  63. {
  64. get { return "J2KDecoderModule"; }
  65. }
  66. public bool IsSharedModule
  67. {
  68. get { return true; }
  69. }
  70. #endregion
  71. #region IJ2KDecoder Members
  72. public void decode(UUID AssetId, byte[] assetData, DecodedCallback decodedReturn)
  73. {
  74. // Dummy for if decoding fails.
  75. OpenJPEG.J2KLayerInfo[] result = new OpenJPEG.J2KLayerInfo[0];
  76. // Check if it's cached
  77. bool cached = false;
  78. lock (m_cacheddecode)
  79. {
  80. if (m_cacheddecode.ContainsKey(AssetId))
  81. {
  82. cached = true;
  83. result = m_cacheddecode[AssetId];
  84. }
  85. }
  86. // If it's cached, return the cached results
  87. if (cached)
  88. {
  89. decodedReturn(AssetId, result);
  90. }
  91. else
  92. {
  93. // not cached, so we need to decode it
  94. // Add to notify list and start decoding.
  95. // Next request for this asset while it's decoding will only be added to the notify list
  96. // once this is decoded, requests will be served from the cache and all clients in the notifylist will be updated
  97. bool decode = false;
  98. lock (m_notifyList)
  99. {
  100. if (m_notifyList.ContainsKey(AssetId))
  101. {
  102. m_notifyList[AssetId].Add(decodedReturn);
  103. }
  104. else
  105. {
  106. List<DecodedCallback> notifylist = new List<DecodedCallback>();
  107. notifylist.Add(decodedReturn);
  108. m_notifyList.Add(AssetId, notifylist);
  109. decode = true;
  110. }
  111. }
  112. // Do Decode!
  113. if (decode)
  114. {
  115. doJ2kDecode(AssetId, assetData);
  116. }
  117. }
  118. }
  119. #endregion
  120. /// <summary>
  121. /// Decode Jpeg2000 Asset Data
  122. /// </summary>
  123. /// <param name="AssetId">UUID of Asset</param>
  124. /// <param name="j2kdata">Byte Array Asset Data </param>
  125. private void doJ2kDecode(UUID AssetId, byte[] j2kdata)
  126. {
  127. int DecodeTime = 0;
  128. DecodeTime = System.Environment.TickCount;
  129. OpenJPEG.J2KLayerInfo[] layers = new OpenJPEG.J2KLayerInfo[0]; // Dummy result for if it fails. Informs that there's only full quality
  130. try
  131. {
  132. AssetTexture texture = new AssetTexture(AssetId, j2kdata);
  133. if (texture.DecodeLayerBoundaries())
  134. {
  135. bool sane = true;
  136. // Sanity check all of the layers
  137. for (int i = 0; i < texture.LayerInfo.Length; i++)
  138. {
  139. if (texture.LayerInfo[i].End > texture.AssetData.Length)
  140. {
  141. sane = false;
  142. break;
  143. }
  144. }
  145. if (sane)
  146. {
  147. layers = texture.LayerInfo;
  148. }
  149. else
  150. {
  151. m_log.WarnFormat("[J2KDecoderModule]: JPEG2000 texture decoding succeeded, but sanity check failed for {0}",
  152. AssetId);
  153. }
  154. }
  155. else
  156. {
  157. m_log.WarnFormat("[J2KDecoderModule]: JPEG2000 texture decoding failed for {0}", AssetId);
  158. }
  159. texture = null; // dereference and dispose of ManagedImage
  160. }
  161. catch (Exception ex)
  162. {
  163. m_log.WarnFormat("[J2KDecoderModule]: JPEG2000 texture decoding threw an exception for {0}, {1}", AssetId, ex);
  164. }
  165. // Write out decode time
  166. m_log.InfoFormat("[J2KDecoderModule]: {0} Decode Time: {1}", System.Environment.TickCount - DecodeTime, AssetId);
  167. // Cache Decoded layers
  168. lock (m_cacheddecode)
  169. {
  170. m_cacheddecode.Add(AssetId, layers);
  171. }
  172. // Notify Interested Parties
  173. lock (m_notifyList)
  174. {
  175. if (m_notifyList.ContainsKey(AssetId))
  176. {
  177. foreach (DecodedCallback d in m_notifyList[AssetId])
  178. {
  179. if (d != null)
  180. d.DynamicInvoke(AssetId, layers);
  181. }
  182. m_notifyList.Remove(AssetId);
  183. }
  184. }
  185. }
  186. }
  187. }