fix memory issues

This commit is contained in:
Christian R. Helmrich 2020-01-26 20:00:25 +01:00
parent 7d115dcc3c
commit 63802e44f0
4 changed files with 77 additions and 17 deletions

View File

@ -31,6 +31,8 @@ struct ExhaleEncAPI
virtual unsigned encodeLookahead () = 0;
// frame encoder
virtual unsigned encodeFrame () = 0;
// destructor
virtual ~ExhaleEncAPI () { }
};
// C constructor

View File

@ -12,7 +12,7 @@
#include "basicMP4Writer.h"
#include "basicWavReader.h"
// #define USE_EXHALELIB_DLL (defined (_WIN32) || defined (WIN32) || defined (_WIN64) || defined (WIN64))
#ifdef USE_EXHALELIB_DLL
#if USE_EXHALELIB_DLL
#include "exhaleDecl.h"
#else
#include "../lib/exhaleEnc.h"
@ -362,7 +362,7 @@ int main (const int argc, char* argv[])
uint32_t byteCount = 0, bw = 0, bwMax = 0, br; // for bytes read and bit-rate
uint32_t headerRes = 0;
// open & prepare ExhaleEncoder object
#ifdef USE_EXHALELIB_DLL
#if USE_EXHALELIB_DLL
ExhaleEncAPI& exhaleEnc = *exhaleCreate (inPcmData, outAuData, sampleRate, numChannels, frameLength, indepPeriod, variableCoreBitRateMode +
#else
ExhaleEncoder exhaleEnc (inPcmData, outAuData, sampleRate, numChannels, frameLength, indepPeriod, variableCoreBitRateMode +
@ -383,7 +383,9 @@ int main (const int argc, char* argv[])
{
fprintf_s (stderr, " ERROR while trying to initialize xHE-AAC encoder: error value %d was returned!\n\n", i);
i <<= 2; // return value
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // coder init error
}
@ -410,7 +412,9 @@ int main (const int argc, char* argv[])
{
fprintf_s (stderr, "\n ERROR while trying to write MPEG-4 bit-stream header: stopped after %d bytes!\n\n", headerRes);
i = 3; // return value
# if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
# endif
goto mainFinish; // writeout error
}
}
@ -422,12 +426,20 @@ int main (const int argc, char* argv[])
{
fprintf_s (stderr, "\n ERROR while trying to create first xHE-AAC frame: error value %d was returned!\n\n", bw);
i = 2; // return value
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // coder-time error
}
if (bwMax < bw) bwMax = bw;
// write first AU, add frame to header
if (mp4Writer.addFrameAU (outAuData, byteCount, bw) != bw) goto mainFinish;
if (mp4Writer.addFrameAU (outAuData, byteCount, bw) != bw)
{
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // writeout error
}
byteCount += bw;
while (wavReader.read (inPcmData, frameLength) > 0) // read a new audio frame
@ -437,12 +449,20 @@ int main (const int argc, char* argv[])
{
fprintf_s (stderr, "\n ERROR while trying to create xHE-AAC frame: error value %d was returned!\n\n", bw);
i = 2; // return value
goto mainFinish;
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // encoding error
}
if (bwMax < bw) bwMax = bw;
// write new AU, add frame to header
if (mp4Writer.addFrameAU (outAuData, byteCount, bw) != bw) goto mainFinish;
if (mp4Writer.addFrameAU (outAuData, byteCount, bw) != bw)
{
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // writeout error
}
byteCount += bw;
if (!readStdin && (mod3Percent > 0) && !(mp4Writer.getFrameCount () % mod3Percent))
@ -459,12 +479,20 @@ int main (const int argc, char* argv[])
{
fprintf_s (stderr, "\n ERROR while trying to create xHE-AAC frame: error value %d was returned!\n\n", bw);
i = 2; // return value
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // coder-time error
}
if (bwMax < bw) bwMax = bw;
// write final AU, add frame to header
if (mp4Writer.addFrameAU (outAuData, byteCount, bw) != bw) goto mainFinish;
if (mp4Writer.addFrameAU (outAuData, byteCount, bw) != bw)
{
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // writeout error
}
byteCount += bw;
const int64_t actualLength = wavReader.getDataBytesRead () / int64_t (numChannels * inSampDepth >> 3);
@ -478,12 +506,20 @@ int main (const int argc, char* argv[])
{
fprintf_s (stderr, "\n ERROR while trying to create last xHE-AAC frame: error value %d was returned!\n\n", bw);
i = 2; // return value
goto mainFinish;
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // encoding error
}
if (bwMax < bw) bwMax = bw;
// the flush AU, add frame to header
if (mp4Writer.addFrameAU (outAuData, byteCount, bw) != bw) goto mainFinish;
if (mp4Writer.addFrameAU (outAuData, byteCount, bw) != bw)
{
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // writeout error
}
byteCount += bw;
} // trailing frame
@ -497,7 +533,9 @@ int main (const int argc, char* argv[])
{
fprintf_s (stderr, "\n ERROR while trying to write MPEG-4 bit-stream header: stopped after %d bytes!\n\n", headerRes);
i = 3; // return value
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
goto mainFinish; // writeout error
}
// move AU data forward to make room for actual MP4 header at start of file
@ -531,6 +569,9 @@ int main (const int argc, char* argv[])
if (bw != headerRes) fprintf_s (stderr, " The encoded MPEG-4 bit-stream is likely to be unreadable!\n");
fprintf_s (stderr, "\n");
}
#if USE_EXHALELIB_DLL
exhaleDelete (&exhaleEnc);
#endif
} // end coding loop and stats print-out
}

View File

@ -755,7 +755,7 @@ unsigned ExhaleEncoder::quantizationCoding () // apply MDCT quantization and en
const uint16_t sfbM1Width = grpOff[b] - sfbM1Start;
const uint16_t swbM1Size = (sfbM1Width * oneTwentyEightOver[grpLength]) >> 7; // sfbM1Width / grpLength
grpScaleFacs[b - 1] = grpScaleFacs[b] - INDEX_OFFSET; // reset prev. SFB to zero
grpScaleFacs[b - 1] = grpScaleFacs[b] - (b > 1 ? INDEX_OFFSET : 0); // zero-out
memset (&m_mdctQuantMag[ci][sfbM1Start], 0, sfbM1Width * sizeof (uint8_t));
// correct SFB statistics with some bit count estimate
@ -800,6 +800,23 @@ unsigned ExhaleEncoder::quantizationCoding () // apply MDCT quantization and en
#if EC_TRELLIS_OPT_CODING
estimBitCount = m_sfbQuantizer.quantizeSpecRDOC (entrCoder, grpScaleFacs, m_bitRateMode, // __min (estimBitCount, targetBitCountX2),
grpOff, grpRms, grpData.sfbsPerGroup, m_mdctQuantMag[ci]);
for (b = 1; b < grpData.sfbsPerGroup; b++)
{
// correct previous scale factor if delta exceeds 60
if (grpScaleFacs[b] > grpScaleFacs[b - 1] + INDEX_OFFSET)
{
const uint16_t sfbM1Start = grpOff[b - 1];
const uint16_t sfbM1Width = grpOff[b] - sfbM1Start;
grpScaleFacs[b - 1] = grpScaleFacs[b] - (b > 1 ? INDEX_OFFSET : 0); // 0-out
memset (&m_mdctQuantMag[ci][sfbM1Start], 0, sfbM1Width * sizeof (uint8_t));
// correct statistics with some bit count estimate
grpRms[b - 1] = 1 + (sfbM1Width >> 3) + entrCoder.indexGetBitCount (b > 1 ? (int) grpScaleFacs[b - 1] - grpScaleFacs[b - 2] : 0);
// correct entropy coding 2-tuples for next window
memset (&arithTuples[(sfbM1Start - grpOff[0]) >> 1], 1, (sfbM1Width >> 1) * sizeof (char));
}
}
#endif
}
b = lastSfb;

View File

@ -133,7 +133,7 @@ public:
#endif
);
// destructor
~ExhaleEncoder ();
virtual ~ExhaleEncoder ();
// public functions
unsigned encodeLookahead ();
unsigned encodeFrame ();