J2KDecoderModule.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 OpenSimulator 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.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenMetaverse.Imaging;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. namespace OpenSim.Region.CoreModules.Agent.TextureSender
  40. {
  41. public class J2KDecoderModule : IRegionModule, IJ2KDecoder
  42. {
  43. #region IRegionModule Members
  44. private static readonly ILog m_log
  45. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// Cached Decoded Layers
  48. /// </summary>
  49. private readonly Dictionary<UUID, OpenJPEG.J2KLayerInfo[]> m_cacheddecode = new Dictionary<UUID, OpenJPEG.J2KLayerInfo[]>();
  50. private bool OpenJpegFail = false;
  51. private readonly string CacheFolder = Util.dataDir() + "/j2kDecodeCache";
  52. private readonly J2KDecodeFileCache fCache;
  53. /// <summary>
  54. /// List of client methods to notify of results of decode
  55. /// </summary>
  56. private readonly Dictionary<UUID, List<DecodedCallback>> m_notifyList = new Dictionary<UUID, List<DecodedCallback>>();
  57. public J2KDecoderModule()
  58. {
  59. fCache = new J2KDecodeFileCache(CacheFolder);
  60. }
  61. public void Initialise(Scene scene, IConfigSource source)
  62. {
  63. scene.RegisterModuleInterface<IJ2KDecoder>(this);
  64. }
  65. public void PostInitialise()
  66. {
  67. }
  68. public void Close()
  69. {
  70. }
  71. public string Name
  72. {
  73. get { return "J2KDecoderModule"; }
  74. }
  75. public bool IsSharedModule
  76. {
  77. get { return true; }
  78. }
  79. #endregion
  80. #region IJ2KDecoder Members
  81. public void decode(UUID AssetId, byte[] assetData, DecodedCallback decodedReturn)
  82. {
  83. // Dummy for if decoding fails.
  84. OpenJPEG.J2KLayerInfo[] result = new OpenJPEG.J2KLayerInfo[0];
  85. // Check if it's cached
  86. bool cached = false;
  87. lock (m_cacheddecode)
  88. {
  89. if (m_cacheddecode.ContainsKey(AssetId))
  90. {
  91. cached = true;
  92. result = m_cacheddecode[AssetId];
  93. }
  94. }
  95. // If it's cached, return the cached results
  96. if (cached)
  97. {
  98. decodedReturn(AssetId, result);
  99. }
  100. else
  101. {
  102. // not cached, so we need to decode it
  103. // Add to notify list and start decoding.
  104. // Next request for this asset while it's decoding will only be added to the notify list
  105. // once this is decoded, requests will be served from the cache and all clients in the notifylist will be updated
  106. bool decode = false;
  107. lock (m_notifyList)
  108. {
  109. if (m_notifyList.ContainsKey(AssetId))
  110. {
  111. m_notifyList[AssetId].Add(decodedReturn);
  112. }
  113. else
  114. {
  115. List<DecodedCallback> notifylist = new List<DecodedCallback>();
  116. notifylist.Add(decodedReturn);
  117. m_notifyList.Add(AssetId, notifylist);
  118. decode = true;
  119. }
  120. }
  121. // Do Decode!
  122. if (decode)
  123. {
  124. doJ2kDecode(AssetId, assetData);
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// Provides a synchronous decode so that caller can be assured that this executes before the next line
  130. /// </summary>
  131. /// <param name="AssetId"></param>
  132. /// <param name="j2kdata"></param>
  133. public void syncdecode(UUID AssetId, byte[] j2kdata)
  134. {
  135. doJ2kDecode(AssetId, j2kdata);
  136. }
  137. #endregion
  138. /// <summary>
  139. /// Decode Jpeg2000 Asset Data
  140. /// </summary>
  141. /// <param name="AssetId">UUID of Asset</param>
  142. /// <param name="j2kdata">Byte Array Asset Data </param>
  143. private void doJ2kDecode(UUID AssetId, byte[] j2kdata)
  144. {
  145. int DecodeTime = 0;
  146. DecodeTime = Environment.TickCount;
  147. OpenJPEG.J2KLayerInfo[] layers = new OpenJPEG.J2KLayerInfo[0]; // Dummy result for if it fails. Informs that there's only full quality
  148. if (!OpenJpegFail)
  149. {
  150. if (!fCache.TryLoadCacheForAsset(AssetId, out layers))
  151. {
  152. try
  153. {
  154. AssetTexture texture = new AssetTexture(AssetId, j2kdata);
  155. if (texture.DecodeLayerBoundaries())
  156. {
  157. bool sane = true;
  158. // Sanity check all of the layers
  159. for (int i = 0; i < texture.LayerInfo.Length; i++)
  160. {
  161. if (texture.LayerInfo[i].End > texture.AssetData.Length)
  162. {
  163. sane = false;
  164. break;
  165. }
  166. }
  167. if (sane)
  168. {
  169. layers = texture.LayerInfo;
  170. fCache.SaveFileCacheForAsset(AssetId, layers);
  171. // Write out decode time
  172. m_log.InfoFormat("[J2KDecoderModule]: {0} Decode Time: {1}", Environment.TickCount - DecodeTime,
  173. AssetId);
  174. }
  175. else
  176. {
  177. m_log.WarnFormat(
  178. "[J2KDecoderModule]: JPEG2000 texture decoding succeeded, but sanity check failed for {0}",
  179. AssetId);
  180. }
  181. }
  182. else
  183. {
  184. /*
  185. Random rnd = new Random();
  186. // scramble ends for test
  187. for (int i = 0; i < texture.LayerInfo.Length; i++)
  188. {
  189. texture.LayerInfo[i].End = rnd.Next(999999);
  190. }
  191. */
  192. // Try to do some heuristics error correction! Yeah.
  193. bool sane2Heuristics = true;
  194. if (texture.Image == null)
  195. sane2Heuristics = false;
  196. if (texture.LayerInfo == null)
  197. sane2Heuristics = false;
  198. if (sane2Heuristics)
  199. {
  200. if (texture.LayerInfo.Length == 0)
  201. sane2Heuristics = false;
  202. }
  203. if (sane2Heuristics)
  204. {
  205. // Last layer start is less then the end of the file and last layer start is greater then 0
  206. if (texture.LayerInfo[texture.LayerInfo.Length - 1].Start < texture.AssetData.Length && texture.LayerInfo[texture.LayerInfo.Length - 1].Start > 0)
  207. {
  208. }
  209. else
  210. {
  211. sane2Heuristics = false;
  212. }
  213. }
  214. if (sane2Heuristics)
  215. {
  216. int start = 0;
  217. // try to fix it by using consistant data in the start field
  218. for (int i = 0; i < texture.LayerInfo.Length; i++)
  219. {
  220. if (i == 0)
  221. start = 0;
  222. if (i == texture.LayerInfo.Length - 1)
  223. texture.LayerInfo[i].End = texture.AssetData.Length;
  224. else
  225. texture.LayerInfo[i].End = texture.LayerInfo[i + 1].Start - 1;
  226. // in this case, the end of the next packet is less then the start of the last packet
  227. // after we've attempted to fix it which means the start of the last packet is borked
  228. // there's no recovery from this
  229. if (texture.LayerInfo[i].End < start)
  230. {
  231. sane2Heuristics = false;
  232. break;
  233. }
  234. if (texture.LayerInfo[i].End < 0 || texture.LayerInfo[i].End > texture.AssetData.Length)
  235. {
  236. sane2Heuristics = false;
  237. break;
  238. }
  239. if (texture.LayerInfo[i].Start < 0 || texture.LayerInfo[i].Start > texture.AssetData.Length)
  240. {
  241. sane2Heuristics = false;
  242. break;
  243. }
  244. start = texture.LayerInfo[i].Start;
  245. }
  246. }
  247. if (sane2Heuristics)
  248. {
  249. layers = texture.LayerInfo;
  250. fCache.SaveFileCacheForAsset(AssetId, layers);
  251. // Write out decode time
  252. m_log.InfoFormat("[J2KDecoderModule]: HEURISTICS SUCCEEDED {0} Decode Time: {1}", Environment.TickCount - DecodeTime,
  253. AssetId);
  254. }
  255. else
  256. {
  257. m_log.WarnFormat("[J2KDecoderModule]: JPEG2000 texture decoding failed for {0}. Is this a texture? is it J2K?", AssetId);
  258. }
  259. }
  260. texture = null; // dereference and dispose of ManagedImage
  261. }
  262. catch (DllNotFoundException)
  263. {
  264. m_log.Error(
  265. "[J2KDecoderModule]: OpenJpeg is not installed properly. Decoding disabled! This will slow down texture performance! Often times this is because of an old version of GLIBC. You must have version 2.4 or above!");
  266. OpenJpegFail = true;
  267. }
  268. catch (Exception ex)
  269. {
  270. m_log.WarnFormat(
  271. "[J2KDecoderModule]: JPEG2000 texture decoding threw an exception for {0}, {1}",
  272. AssetId, ex);
  273. }
  274. }
  275. }
  276. // Cache Decoded layers
  277. lock (m_cacheddecode)
  278. {
  279. if (!m_cacheddecode.ContainsKey(AssetId))
  280. m_cacheddecode.Add(AssetId, layers);
  281. }
  282. // Notify Interested Parties
  283. lock (m_notifyList)
  284. {
  285. if (m_notifyList.ContainsKey(AssetId))
  286. {
  287. foreach (DecodedCallback d in m_notifyList[AssetId])
  288. {
  289. if (d != null)
  290. d.DynamicInvoke(AssetId, layers);
  291. }
  292. m_notifyList.Remove(AssetId);
  293. }
  294. }
  295. }
  296. }
  297. public class J2KDecodeFileCache
  298. {
  299. private readonly string m_cacheDecodeFolder;
  300. private bool enabled = true;
  301. private static readonly ILog m_log
  302. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  303. /// <summary>
  304. /// Creates a new instance of a file cache
  305. /// </summary>
  306. /// <param name="pFolder">base folder for the cache. Will be created if it doesn't exist</param>
  307. public J2KDecodeFileCache(string pFolder)
  308. {
  309. m_cacheDecodeFolder = pFolder;
  310. if (!Directory.Exists(pFolder))
  311. {
  312. Createj2KCacheFolder(pFolder);
  313. }
  314. }
  315. /// <summary>
  316. /// Save Layers to Disk Cache
  317. /// </summary>
  318. /// <param name="AssetId">Asset to Save the layers. Used int he file name by default</param>
  319. /// <param name="Layers">The Layer Data from OpenJpeg</param>
  320. /// <returns></returns>
  321. public bool SaveFileCacheForAsset(UUID AssetId, OpenJPEG.J2KLayerInfo[] Layers)
  322. {
  323. if (Layers.Length > 0 && enabled)
  324. {
  325. FileStream fsCache =
  326. new FileStream(String.Format("{0}/{1}", m_cacheDecodeFolder, FileNameFromAssetId(AssetId)),
  327. FileMode.Create);
  328. StreamWriter fsSWCache = new StreamWriter(fsCache);
  329. StringBuilder stringResult = new StringBuilder();
  330. string strEnd = "\n";
  331. for (int i = 0; i < Layers.Length; i++)
  332. {
  333. if (i == (Layers.Length - 1))
  334. strEnd = "";
  335. stringResult.AppendFormat("{0}|{1}|{2}{3}", Layers[i].Start, Layers[i].End, Layers[i].Size, strEnd);
  336. }
  337. fsSWCache.Write(stringResult.ToString());
  338. fsSWCache.Close();
  339. fsSWCache.Dispose();
  340. fsCache.Dispose();
  341. return true;
  342. }
  343. return false;
  344. }
  345. /// <summary>
  346. /// Loads the Layer data from the disk cache
  347. /// Returns true if load succeeded
  348. /// </summary>
  349. /// <param name="AssetId">AssetId that we're checking the cache for</param>
  350. /// <param name="Layers">out layers to save to</param>
  351. /// <returns>true if load succeeded</returns>
  352. public bool TryLoadCacheForAsset(UUID AssetId, out OpenJPEG.J2KLayerInfo[] Layers)
  353. {
  354. string filename = String.Format("{0}/{1}", m_cacheDecodeFolder, FileNameFromAssetId(AssetId));
  355. Layers = new OpenJPEG.J2KLayerInfo[0];
  356. if (!File.Exists(filename))
  357. return false;
  358. if (!enabled)
  359. {
  360. return false;
  361. }
  362. string readResult = string.Empty;
  363. try
  364. {
  365. FileStream fsCachefile =
  366. new FileStream(filename,
  367. FileMode.Open);
  368. StreamReader sr = new StreamReader(fsCachefile);
  369. readResult = sr.ReadToEnd();
  370. sr.Close();
  371. sr.Dispose();
  372. fsCachefile.Dispose();
  373. }
  374. catch (IOException ioe)
  375. {
  376. if (ioe is PathTooLongException)
  377. {
  378. m_log.Error(
  379. "[J2KDecodeCache]: Cache Read failed. Path is too long.");
  380. }
  381. else if (ioe is DirectoryNotFoundException)
  382. {
  383. m_log.Error(
  384. "[J2KDecodeCache]: Cache Read failed. Cache Directory does not exist!");
  385. enabled = false;
  386. }
  387. else
  388. {
  389. m_log.Error(
  390. "[J2KDecodeCache]: Cache Read failed. IO Exception.");
  391. }
  392. return false;
  393. }
  394. catch (UnauthorizedAccessException)
  395. {
  396. m_log.Error(
  397. "[J2KDecodeCache]: Cache Read failed. UnauthorizedAccessException Exception. Do you have the proper permissions on this file?");
  398. return false;
  399. }
  400. catch (ArgumentException ae)
  401. {
  402. if (ae is ArgumentNullException)
  403. {
  404. m_log.Error(
  405. "[J2KDecodeCache]: Cache Read failed. No Filename provided");
  406. }
  407. else
  408. {
  409. m_log.Error(
  410. "[J2KDecodeCache]: Cache Read failed. Filname was invalid");
  411. }
  412. return false;
  413. }
  414. catch (NotSupportedException)
  415. {
  416. m_log.Error(
  417. "[J2KDecodeCache]: Cache Read failed, not supported. Cache disabled!");
  418. enabled = false;
  419. return false;
  420. }
  421. catch (Exception e)
  422. {
  423. m_log.ErrorFormat(
  424. "[J2KDecodeCache]: Cache Read failed, unknown exception. Error: {0}",
  425. e.ToString());
  426. return false;
  427. }
  428. string[] lines = readResult.Split('\n');
  429. if (lines.Length <= 0)
  430. return false;
  431. Layers = new OpenJPEG.J2KLayerInfo[lines.Length];
  432. for (int i = 0; i < lines.Length; i++)
  433. {
  434. string[] elements = lines[i].Split('|');
  435. if (elements.Length == 3)
  436. {
  437. int element1, element2;
  438. try
  439. {
  440. element1 = Convert.ToInt32(elements[0]);
  441. element2 = Convert.ToInt32(elements[1]);
  442. }
  443. catch (FormatException)
  444. {
  445. m_log.WarnFormat("[J2KDecodeCache]: Cache Read failed with ErrorConvert for {0}", AssetId);
  446. Layers = new OpenJPEG.J2KLayerInfo[0];
  447. return false;
  448. }
  449. Layers[i] = new OpenJPEG.J2KLayerInfo();
  450. Layers[i].Start = element1;
  451. Layers[i].End = element2;
  452. }
  453. else
  454. {
  455. // reading failed
  456. m_log.WarnFormat("[J2KDecodeCache]: Cache Read failed for {0}", AssetId);
  457. Layers = new OpenJPEG.J2KLayerInfo[0];
  458. return false;
  459. }
  460. }
  461. return true;
  462. }
  463. /// <summary>
  464. /// Routine which converts assetid to file name
  465. /// </summary>
  466. /// <param name="AssetId">asset id of the image</param>
  467. /// <returns>string filename</returns>
  468. public string FileNameFromAssetId(UUID AssetId)
  469. {
  470. return String.Format("j2kCache_{0}.cache", AssetId);
  471. }
  472. /// <summary>
  473. /// Creates the Cache Folder
  474. /// </summary>
  475. /// <param name="pFolder">Folder to Create</param>
  476. public void Createj2KCacheFolder(string pFolder)
  477. {
  478. try
  479. {
  480. Directory.CreateDirectory(pFolder);
  481. }
  482. catch (IOException ioe)
  483. {
  484. if (ioe is PathTooLongException)
  485. {
  486. m_log.Error(
  487. "[J2KDecodeCache]: Cache Directory does not exist and create failed because the path to the cache folder is too long. Cache disabled!");
  488. }
  489. else if (ioe is DirectoryNotFoundException)
  490. {
  491. m_log.Error(
  492. "[J2KDecodeCache]: Cache Directory does not exist and create failed because the supplied base of the directory folder does not exist. Cache disabled!");
  493. }
  494. else
  495. {
  496. m_log.Error(
  497. "[J2KDecodeCache]: Cache Directory does not exist and create failed because of an IO Exception. Cache disabled!");
  498. }
  499. enabled = false;
  500. }
  501. catch (UnauthorizedAccessException)
  502. {
  503. m_log.Error(
  504. "[J2KDecodeCache]: Cache Directory does not exist and create failed because of an UnauthorizedAccessException Exception. Cache disabled!");
  505. enabled = false;
  506. }
  507. catch (ArgumentException ae)
  508. {
  509. if (ae is ArgumentNullException)
  510. {
  511. m_log.Error(
  512. "[J2KDecodeCache]: Cache Directory does not exist and create failed because the folder provided is invalid! Cache disabled!");
  513. }
  514. else
  515. {
  516. m_log.Error(
  517. "[J2KDecodeCache]: Cache Directory does not exist and create failed because no cache folder was provided! Cache disabled!");
  518. }
  519. enabled = false;
  520. }
  521. catch (NotSupportedException)
  522. {
  523. m_log.Error(
  524. "[J2KDecodeCache]: Cache Directory does not exist and create failed because it's not supported. Cache disabled!");
  525. enabled = false;
  526. }
  527. catch (Exception e)
  528. {
  529. m_log.ErrorFormat(
  530. "[J2KDecodeCache]: Cache Directory does not exist and create failed because of an unknown exception. Cache disabled! Error: {0}",
  531. e.ToString());
  532. enabled = false;
  533. }
  534. }
  535. }
  536. }