Attempting to improve the robustness of texture decoding by always ignoring LayerInfo.End values and creating guessed default layer boundaries on failed decodes Changed a noisy J2K decode log message from Info to Debug

Replacing openjpeg-dotnet decoding with managed CSJ2K decoding. Should be much more reliable, faster, and use less memory

* Re-added openjpeg-dotnet files since they are used elsewhere in OpenSim * Updated prebuild.xml with a reference to CSJ2K
prioritization
John Hurliman 2009-09-29 18:15:22 -07:00 committed by Melanie
parent 4bf47fa592
commit 22cc31135e
3 changed files with 70 additions and 169 deletions

View File

@ -34,8 +34,8 @@ using System.Threading;
using log4net;
using Nini.Config;
using OpenMetaverse;
using OpenMetaverse.Assets;
using OpenMetaverse.Imaging;
using CSJ2K;
using OpenSim.Framework;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;
@ -195,163 +195,47 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
{
int DecodeTime = 0;
DecodeTime = Environment.TickCount;
OpenJPEG.J2KLayerInfo[] layers = new OpenJPEG.J2KLayerInfo[0]; // Dummy result for if it fails. Informs that there's only full quality
OpenJPEG.J2KLayerInfo[] layers = null;
if (!OpenJpegFail)
{
if (!fCache.TryLoadCacheForAsset(AssetId, out layers))
{
try
{
List<int> layerStarts = CSJ2K.J2kImage.GetLayerBoundaries(new MemoryStream(j2kdata));
AssetTexture texture = new AssetTexture(AssetId, j2kdata);
if (texture.DecodeLayerBoundaries())
if (layerStarts != null && layerStarts.Count > 0)
{
bool sane = true;
layers = new OpenJPEG.J2KLayerInfo[layerStarts.Count];
// Sanity check all of the layers
for (int i = 0; i < texture.LayerInfo.Length; i++)
for (int i = 0; i < layerStarts.Count; i++)
{
if (texture.LayerInfo[i].End > texture.AssetData.Length)
{
sane = false;
break;
}
}
OpenJPEG.J2KLayerInfo layer = new OpenJPEG.J2KLayerInfo();
int start = layerStarts[i];
if (sane)
{
layers = texture.LayerInfo;
fCache.SaveFileCacheForAsset(AssetId, layers);
// Write out decode time
m_log.InfoFormat("[J2KDecoderModule]: {0} Decode Time: {1}", Environment.TickCount - DecodeTime,
AssetId);
}
else
{
m_log.WarnFormat(
"[J2KDecoderModule]: JPEG2000 texture decoding succeeded, but sanity check failed for {0}",
AssetId);
}
}
else
{
/*
Random rnd = new Random();
// scramble ends for test
for (int i = 0; i < texture.LayerInfo.Length; i++)
{
texture.LayerInfo[i].End = rnd.Next(999999);
}
*/
// Try to do some heuristics error correction! Yeah.
bool sane2Heuristics = true;
if (texture.Image == null)
sane2Heuristics = false;
if (texture.LayerInfo == null)
sane2Heuristics = false;
if (sane2Heuristics)
{
if (texture.LayerInfo.Length == 0)
sane2Heuristics = false;
}
if (sane2Heuristics)
{
// Last layer start is less then the end of the file and last layer start is greater then 0
if (texture.LayerInfo[texture.LayerInfo.Length - 1].Start < texture.AssetData.Length && texture.LayerInfo[texture.LayerInfo.Length - 1].Start > 0)
{
}
else
{
sane2Heuristics = false;
}
}
if (sane2Heuristics)
{
int start = 0;
// try to fix it by using consistant data in the start field
for (int i = 0; i < texture.LayerInfo.Length; i++)
{
if (i == 0)
start = 0;
if (i == texture.LayerInfo.Length - 1)
texture.LayerInfo[i].End = texture.AssetData.Length;
layer.Start = 0;
else
texture.LayerInfo[i].End = texture.LayerInfo[i + 1].Start - 1;
layer.Start = layerStarts[i];
// in this case, the end of the next packet is less then the start of the last packet
// after we've attempted to fix it which means the start of the last packet is borked
// there's no recovery from this
if (texture.LayerInfo[i].End < start)
{
sane2Heuristics = false;
break;
}
if (texture.LayerInfo[i].End < 0 || texture.LayerInfo[i].End > texture.AssetData.Length)
{
sane2Heuristics = false;
break;
}
if (texture.LayerInfo[i].Start < 0 || texture.LayerInfo[i].Start > texture.AssetData.Length)
{
sane2Heuristics = false;
break;
}
start = texture.LayerInfo[i].Start;
}
}
if (sane2Heuristics)
{
layers = texture.LayerInfo;
fCache.SaveFileCacheForAsset(AssetId, layers);
// Write out decode time
m_log.InfoFormat("[J2KDecoderModule]: HEURISTICS SUCCEEDED {0} Decode Time: {1}", Environment.TickCount - DecodeTime,
AssetId);
}
if (i == layerStarts.Count - 1)
layer.End = j2kdata.Length;
else
{
m_log.WarnFormat("[J2KDecoderModule]: JPEG2000 texture decoding failed for {0}. Is this a texture? is it J2K?", AssetId);
layer.End = layerStarts[i + 1] - 1;
layers[i] = layer;
}
}
texture = null; // dereference and dispose of ManagedImage
}
catch (DllNotFoundException)
{
m_log.Error(
"[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!");
OpenJpegFail = true;
}
catch (Exception ex)
{
m_log.WarnFormat(
"[J2KDecoderModule]: JPEG2000 texture decoding threw an exception for {0}, {1}",
AssetId, ex);
}
m_log.Warn("[J2KDecoderModule]: CSJ2K threw an exception decoding texture " + AssetId + ": " + ex.ToString());
}
if (layers.Length == 0)
{
m_log.Warn("[J2KDecoderModule]: OpenJPEG failed to decode any layer data for texture " + AssetId + ", guessing sane defaults");
// Layer decoding completely failed. Guess at sane defaults for the layer boundaries
layers = CreateDefaultLayers(j2kdata.Length);
}
// Cache Decoded layers
@ -360,7 +244,7 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
if (m_cacheddecode.ContainsKey(AssetId))
m_cacheddecode.Remove(AssetId);
m_cacheddecode.Add(AssetId, layers);
}
}
// Notify Interested Parties
@ -378,6 +262,31 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
}
}
private OpenJPEG.J2KLayerInfo[] CreateDefaultLayers(int j2kLength)
{
OpenJPEG.J2KLayerInfo[] layers = new OpenJPEG.J2KLayerInfo[5];
layers[0] = new OpenJPEG.J2KLayerInfo();
for (int i = 0; i < layers.Length; i++)
{
OpenJPEG.J2KLayerInfo layer = new OpenJPEG.J2KLayerInfo();
if (i == 0)
layer.Start = 0;
else
layer.Start = layers[i - 1].End + 1;
// These default layer sizes are based on a small sampling of real-world texture data
// with extra padding thrown in for good measure. This is a worst case fallback plan
// and will probably not gracefully handle all real world data
layer.End = (int)(160d * Math.Exp(1.3d * (double)(i + 1)));
layers[i] = layer;
}
return layers;
}
private void CleanCache()
{
m_log.Info("[J2KDecoderModule]: Cleaner thread started");
@ -418,11 +327,10 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
{
m_cacheDecodeFolder = pFolder;
m_cacheTimeout = timeout;
if (!Directory.Exists(pFolder))
{
Createj2KCacheFolder(pFolder);
}
}
/// <summary>
/// Save Layers to Disk Cache
@ -447,14 +355,15 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
stringResult.AppendFormat("{0}|{1}|{2}{3}", Layers[i].Start, Layers[i].End, Layers[i].End - Layers[i].Start, strEnd);
}
fsSWCache.Write(stringResult.ToString());
fsSWCache.Close();
fsSWCache.Dispose();
fsCache.Dispose();
return true;
}
return false;
}
@ -475,11 +384,9 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
return false;
if (!enabled)
{
return false;
}
string readResult = string.Empty;
string readResult = String.Empty;
try
{
@ -493,7 +400,6 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
sr.Close();
sr.Dispose();
fsCachefile.Dispose();
}
catch (IOException ioe)
{
@ -514,7 +420,6 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
"[J2KDecodeCache]: Cache Read failed. IO Exception.");
}
return false;
}
catch (UnauthorizedAccessException)
{
@ -541,7 +446,6 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
m_log.Error(
"[J2KDecodeCache]: Cache Read failed, not supported. Cache disabled!");
enabled = false;
return false;
}
catch (Exception e)
@ -581,7 +485,6 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
Layers[i] = new OpenJPEG.J2KLayerInfo();
Layers[i].Start = element1;
Layers[i].End = element2;
}
else
{
@ -592,9 +495,6 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender
}
}
return true;
}

BIN
bin/CSJ2K.dll Normal file

Binary file not shown.

View File

@ -1503,6 +1503,7 @@
<Reference name="OpenMetaverseTypes.dll"/>
<Reference name="OpenMetaverse.StructuredData.dll"/>
<Reference name="OpenMetaverse.dll"/>
<Reference name="CSJ2K.dll"/>
<Reference name="OpenSim.Framework"/>
<Reference name="OpenSim.Framework.Capabilities"/>
<Reference name="OpenSim.Framework.Communications"/>