/// <summary>Whether to collect timing information or not: false. Used as a compile
/// time directive.
/// </summary>
#ifDO_TIMING
/// <summary>The wall time for the initialization. </summary>
//private long initTime;
/// <summary>The wall time for the building of layers. </summary>
//private long buildTime;
/// <summary>The wall time for the writing of layers. </summary>
//private long writeTime;
#endif
/// <summary> 5D Array containing all the coded code-blocks:
///
/// <ul>
/// <li>1st index: tile index</li>
/// <li>2nd index: component index</li>
/// <li>3rd index: resolution level index</li>
/// <li>4th index: subband index</li>
/// <li>5th index: code-block index</li>
/// </ul>
///
/// </summary>
privateCBlkRateDistStats[][][][][]cblks;
/// <summary> 6D Array containing the indices of the truncation points. It actually
/// contains the index of the element in CBlkRateDistStats.truncIdxs that
/// gives the real truncation point index.
///
/// <ul>
/// <li>1st index: tile index</li>
/// <li>2nd index: layer index</li>
/// <li>3rd index: component index</li>
/// <li>4th index: resolution level index</li>
/// <li>5th index: subband index</li>
/// <li>6th index: code-block index</li>
/// </ul>
///
/// </summary>
privateint[][][][][][]truncIdxs;
/// <summary> Number of precincts in each resolution level:
///
/// <ul>
/// <li>1st dim: tile index.</li>
/// <li>2nd dim: component index.</li>
/// <li>3nd dim: resolution level index.</li>
/// </ul>
///
/// </summary>
privateCoord[][][]numPrec;
/// <summary>Array containing the layers information. </summary>
privateEBCOTLayer[]layers;
/// <summary>The log of 2, natural base </summary>
//UPGRADE_NOTE: Final was removed from the declaration of 'LOG2 '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
// If we are timing make sure that 'finalize' gets called.
//UPGRADE_ISSUE: Method 'java.lang.System.runFinalizersOnExit' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javalangSystem'"
// CONVERSION PROBLEM?
//System_Renamed.runFinalizersOnExit(true);
// The System.runFinalizersOnExit() method is deprecated in Java
// 1.2 since it can cause a deadlock in some cases. However, here
// we use it only for profiling purposes and is disabled in
// production code.
initTime=0L;
buildTime=0L;
writeTime=0L;
#endif
// Save the layer specs
lyrSpec=lyrs;
//Initialize the size of the RD slope rates array
RDSlopesRates=newint[RD_SUMMARY_SIZE];
//Get number of tiles, components
intnt=src.getNumTiles();
intnc=NumComps;
//Allocate the coded code-blocks and truncation points indexes arrays
cblks=newCBlkRateDistStats[nt][][][][];
for(inti2=0;i2<nt;i2++)
{
cblks[i2]=newCBlkRateDistStats[nc][][][];
}
truncIdxs=newint[nt][][][][][];
for(inti3=0;i3<nt;i3++)
{
truncIdxs[i3]=newint[num_Layers][][][][];
for(inti4=0;i4<num_Layers;i4++)
{
truncIdxs[i3][i4]=newint[nc][][][];
}
}
intcblkPerSubband;// Number of code-blocks per subband
intmrl;// Number of resolution levels
intl;// layer index
ints;//subband index
// Used to compute the maximum number of precincts for each resolution
// level
inttx0,ty0,tx1,ty1;// Current tile position in the reference grid
inttcx0,tcy0,tcx1,tcy1;// Current tile position in the domain of
// the image component
inttrx0,try0,trx1,try1;// Current tile position in the reduced
// Initialize maximum number of precincts per resolution array
if(numPrec==null)
{
Coord[][][]tmpArray=newCoord[nt][][];
for(inti5=0;i5<nt;i5++)
{
tmpArray[i5]=newCoord[nc][];
}
numPrec=tmpArray;
}
if(numPrec[t][c]==null)
{
numPrec[t][c]=newCoord[mrl];
}
// Subsampling factors
xrsiz=src.getCompSubsX(c);
yrsiz=src.getCompSubsY(c);
// Tile's coordinates in the image component domain
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
// Tile's coordinates in the reduced resolution image
// domain
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
// Calculate the maximum number of precincts for each
// resolution level taking into account tile specific
// options.
doubletwoppx=(double)encSpec.pss.getPPX(t,c,r);
doubletwoppy=(double)encSpec.pss.getPPY(t,c,r);
numPrec[t][c][r]=newCoord();
if(trx1>trx0)
{
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_NOTE: Call to 'super.finalize()' was removed. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1124'"
}
#endif
/// <summary> Runs the rate allocation algorithm and writes the data to the bit
/// stream writer object provided to the constructor.
///
/// </summary>
publicoverridevoidrunAndWrite()
{
//Now, run the rate allocation
buildAndWriteLayers();
}
/// <summary> Initializes the layers array. This must be called after the main header
/// has been entirely written or simulated, so as to take its overhead into
/// account. This method will get all the code-blocks and then initialize
/// the target bitrates for each layer, according to the specifications.
///
/// </summary>
publicoverridevoidinitialize()
{
intn,i,l;
intho;// The header overhead (in bytes)
floatnp;// The number of pixels divided by the number of bits per byte
doublels;// Step for log-scale
doublebasebytes;
intlastbytes,newbytes,nextbytes;
intloopnlyrs;
intminlsz;// The minimum allowable number of bytes in a layer
inttotenclength;
intmaxpkt;
intnumTiles=src.getNumTiles();
intnumComps=src.NumComps;
intnumLvls;
intavgPktLen;
#ifDO_TIMING
longstime=0L;
#endif
// Start by getting all the code-blocks, we need this in order to have
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
newbytes=(int)basebytes-lastbytes-ho;
if(newbytes<minlsz)
{
// Skip layer (too small)
basebytes*=ls;
num_Layers--;
continue;
}
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
lastbytes=(int)basebytes-ho;
layers[n].maxBytes=lastbytes;
basebytes*=ls;
n++;
}
i++;// Goto next optimization point
}
// Ensure minimum size of last layer (this one determines overall
// bitrate)
n=num_Layers-2;
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
nextbytes=(int)(lyrSpec.TotBitrate*np)-ho;
newbytes=nextbytes-((n>=0)?layers[n].maxBytes:0);
while(newbytes<minlsz)
{
if(num_Layers==1)
{
if(newbytes<=0)
{
thrownewSystem.ArgumentException("Overall target bitrate too "+"low, given the current "+"bit stream header overhead");
}
break;
}
// Delete last layer
num_Layers--;
n--;
newbytes=nextbytes-((n>=0)?layers[n].maxBytes:0);
}
// Set last layer to the overall target bitrate
n++;
layers[n].maxBytes=nextbytes;
layers[n].optimize=true;
// Re-initialize progression order changes if needed Default values
thrownewSystem.InvalidOperationException("JJ2000 bug: One precinct at least has "+"not been written for resolution level "+r+" of component "+c+" in tile "+t+".");
thrownewSystem.InvalidOperationException("JJ2000 bug: One precinct at least has "+"not been written for resolution level "+r+" of component "+c+" in tile "+t+".");
thrownewSystem.InvalidOperationException("JJ2000 bug: One precinct at least has "+"not been written for resolution level "+r+" of component "+c+" in tile "+t+".");
floatlog_sl1;// The log of the first slope used for interpolation
floatlog_sl2;// The log of the second slope used for interpolation
floatlog_len1;// The log of the first length used for interpolation
floatlog_len2;// The log of the second length used for interpolation
floatlog_isl;// The log of the interpolated slope
floatlog_ilen;// Log of the interpolated length
floatlog_ab;// Log of actual bytes in last layer
intsidx;// Index into the summary R-D info array
floatlog_off;// The log of the offset proportion
inttlen;// The corrected target layer length
floatlthresh;// The threshold of the last layer
floateth;// The estimated threshold
// In order to estimate the threshold we base ourselves in the summary
// R-D info in RDSlopesRates. In order to use it we must compensate
// for the overhead of the packet heads. The proportion of overhead is
// estimated using the last layer simulation results.
// NOTE: the model used in this method is that the slope varies
// linearly with the log of the rate (i.e. length).
// NOTE: the model used in this method is that the distortion is
// proprotional to a power of the rate. Thus, the slope is also
// proportional to another power of the rate. This translates as the
// log of the slope varies linearly with the log of the rate, which is
// what we use.
// 1) Find the offset of the length predicted from the summary R-D
// information, to the actual length by using the last layer.
// We ensure that the threshold we use for estimation actually
// includes some data.
lthresh=lastLayer.rdThreshold;
if(lthresh>maxSlope)
lthresh=maxSlope;
// If the slope of the last layer is too small then we just include
// all the rest (not possible to do better).
if(lthresh<FLOAT_ABS_PRECISION)
return0f;
sidx=getLimitedSIndexFromSlope(lthresh);
// If the index is outside of the summary info array use the last two,
// or first two, indexes, as appropriate
if(sidx>=RD_SUMMARY_SIZE-1)
sidx=RD_SUMMARY_SIZE-2;
// Get the logs of the lengths and the slopes
if(RDSlopesRates[sidx+1]==0)
{
// Pathological case, we can not use log of 0. Add
// RDSlopesRates[sidx]+1 bytes to the rates (just a crude simple
// solution to this rare case)
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
// Do not use negative offsets (i.e. offset proportion larger than 1)
// since that is probably a sign that our model is off. To be
// conservative use an offset of 0 (i.e. offset proportiojn 1).
if(log_off<0)
log_off=0f;
// 2) Correct the target layer length by the offset.
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
// 3) Find, from the summary R-D info, the thresholds that generate
// lengths just above and below our corrected target layer length.
// Look for the index in the summary info array that gives the largest
// length smaller than the target length
for(sidx=RD_SUMMARY_SIZE-1;sidx>=0;sidx--)
{
if(RDSlopesRates[sidx]>=tlen)
break;
}
sidx++;
// Correct if out of the array
if(sidx>=RD_SUMMARY_SIZE)
sidx=RD_SUMMARY_SIZE-1;
if(sidx<=0)
sidx=1;
// Get the log of the lengths and the slopes that are just above and
// below the target length.
if(RDSlopesRates[sidx]==0)
{
// Pathological case, we can not use log of 0. Add
// RDSlopesRates[sidx-1]+1 bytes to the rates (just a crude simple
// solution to this rare case)
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
log_ilen=(float)System.Math.Log(tlen);
}
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
eth=(float)System.Math.Exp(log_isl);
// Correct out of bounds results
if(eth>lthresh)
eth=lthresh;
if(eth<FLOAT_ABS_PRECISION)
eth=0f;
// Return the estimated threshold
returneth;
}
/// <summary> This function finds the new truncation points indices for a packet. It
/// does so by including the data from the code-blocks in the component,
/// resolution level and tile, associated with a R-D slope which is larger
/// than or equal to 'fthresh'.
///
/// </summary>
/// <param name="layerIdx">The index of the current layer
///
/// </param>
/// <param name="compIdx">The index of the current component
///
/// </param>
/// <param name="lvlIdx">The index of the current resolution level
///
/// </param>
/// <param name="tileIdx">The index of the current tile
///
/// </param>
/// <param name="subb">The LL subband in the resolution level lvlIdx, which is
/// parent of all the subbands in the packet. Except for resolution level 0
/// this subband is always a node.
///
/// </param>
/// <param name="fthresh">The value of the rate-distortion threshold
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
/// <summary> Returns the minimum slope value associated with a summary table
/// index. This minimum slope is just 2^(index-RD_SUMMARY_OFF).
///
/// </summary>
/// <param name="index">The summary index value.
///
/// </param>
/// <returns> The minimum slope value associated with a summary table index.
///
/// </returns>
privatestaticfloatgetSlopeFromSIndex(intindex)
{
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"