From 63802e44f0cb06f61c0019dde34e32780d8081e7 Mon Sep 17 00:00:00 2001 From: "Christian R. Helmrich" Date: Sun, 26 Jan 2020 20:00:25 +0100 Subject: [PATCH] fix memory issues --- include/exhaleDecl.h | 2 ++ src/app/exhaleApp.cpp | 71 ++++++++++++++++++++++++++++++++++--------- src/lib/exhaleEnc.cpp | 19 +++++++++++- src/lib/exhaleEnc.h | 2 +- 4 files changed, 77 insertions(+), 17 deletions(-) diff --git a/include/exhaleDecl.h b/include/exhaleDecl.h index 409b457..a96f013 100644 --- a/include/exhaleDecl.h +++ b/include/exhaleDecl.h @@ -31,6 +31,8 @@ struct ExhaleEncAPI virtual unsigned encodeLookahead () = 0; // frame encoder virtual unsigned encodeFrame () = 0; + // destructor + virtual ~ExhaleEncAPI () { } }; // C constructor diff --git a/src/app/exhaleApp.cpp b/src/app/exhaleApp.cpp index 73d982a..5c57f19 100644 --- a/src/app/exhaleApp.cpp +++ b/src/app/exhaleApp.cpp @@ -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 } diff --git a/src/lib/exhaleEnc.cpp b/src/lib/exhaleEnc.cpp index 247b114..407022a 100644 --- a/src/lib/exhaleEnc.cpp +++ b/src/lib/exhaleEnc.cpp @@ -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; diff --git a/src/lib/exhaleEnc.h b/src/lib/exhaleEnc.h index f30b4ec..720b2e7 100644 --- a/src/lib/exhaleEnc.h +++ b/src/lib/exhaleEnc.h @@ -133,7 +133,7 @@ public: #endif ); // destructor - ~ExhaleEncoder (); + virtual ~ExhaleEncoder (); // public functions unsigned encodeLookahead (); unsigned encodeFrame ();