DecodeStream.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. *
  3. * Lame ACM wrapper, encode/decode MP3 based RIFF/AVI files in MS Windows
  4. *
  5. * Copyright (c) 2002 Steve Lhomme <steve.lhomme at free.fr>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /*!
  23. \author Steve Lhomme
  24. \version \$Id$
  25. */
  26. #if !defined(STRICT)
  27. #define STRICT
  28. #endif // STRICT
  29. #include <assert.h>
  30. #include <windows.h>
  31. #ifdef ENABLE_DECODING
  32. #include "adebug.h"
  33. #include "DecodeStream.h"
  34. // static methods
  35. DecodeStream * DecodeStream::Create()
  36. {
  37. DecodeStream * Result;
  38. Result = new DecodeStream;
  39. return Result;
  40. }
  41. const bool DecodeStream::Erase(const DecodeStream * a_ACMStream)
  42. {
  43. delete a_ACMStream;
  44. return true;
  45. }
  46. // class methods
  47. DecodeStream::DecodeStream() :
  48. m_WorkingBufferUseSize(0),
  49. gfp(NULL)
  50. {
  51. /// \todo get the debug level from the registry
  52. my_debug = new ADbg(DEBUG_LEVEL_CREATION);
  53. if (my_debug != NULL) {
  54. unsigned char DebugFileName[512];
  55. my_debug->setPrefix("MPG123stream"); /// \todo get it from the registry
  56. my_debug->setIncludeTime(true); /// \todo get it from the registry
  57. // Check in the registry if we have to Output Debug information
  58. DebugFileName[0] = '\0';
  59. HKEY OssKey;
  60. if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\MUKOLI", 0, KEY_READ , &OssKey ) == ERROR_SUCCESS) {
  61. DWORD DataType;
  62. DWORD DebugFileNameSize = 512;
  63. if (RegQueryValueEx( OssKey, "DebugFile", NULL, &DataType, DebugFileName, &DebugFileNameSize ) == ERROR_SUCCESS) {
  64. if (DataType == REG_SZ) {
  65. my_debug->setUseFile(true);
  66. my_debug->setDebugFile((char *)DebugFileName);
  67. my_debug->OutPut("Debug file is %s",(char *)DebugFileName);
  68. }
  69. }
  70. }
  71. my_debug->OutPut(DEBUG_LEVEL_FUNC_START, "DecodeStream Creation (0X%08X)",this);
  72. }
  73. else {
  74. ADbg debug;
  75. debug.OutPut("DecodeStream::ACMACMStream : Impossible to create my_debug");
  76. }
  77. }
  78. DecodeStream::~DecodeStream()
  79. {
  80. // lame_close( gfp );
  81. if (my_debug != NULL)
  82. {
  83. my_debug->OutPut(DEBUG_LEVEL_FUNC_START, "DecodeStream Deletion (0X%08X)",this);
  84. delete my_debug;
  85. }
  86. }
  87. bool DecodeStream::init(const int nSamplesPerSec, const int nChannels, const int nAvgBytesPerSec, const int nSourceBitrate)
  88. {
  89. bool bResult = false;
  90. my_SamplesPerSec = nSamplesPerSec;
  91. my_Channels = nChannels;
  92. my_AvgBytesPerSec = nAvgBytesPerSec;
  93. my_SourceBitrate = nSourceBitrate;
  94. bResult = true;
  95. return bResult;
  96. }
  97. bool DecodeStream::open()
  98. {
  99. bool bResult = false;
  100. bResult = bool(InitMP3(&my_DecodeData) != 0);
  101. return bResult;
  102. }
  103. bool DecodeStream::close(LPBYTE pOutputBuffer, DWORD *pOutputSize)
  104. {
  105. bool bResult = false;
  106. /*
  107. int nOutputSamples = 0;
  108. nOutputSamples = lame_encode_flush( gfp, pOutputBuffer, 0 );
  109. if ( nOutputSamples < 0 )
  110. {
  111. // BUFFER_TOO_SMALL
  112. *pOutputSize = 0;
  113. }
  114. else
  115. {
  116. *pOutputSize = nOutputSamples;
  117. bResult = true;
  118. }
  119. /*
  120. // lame will be close in VbrWriteTag function
  121. if ( !lame_get_bWriteVbrTag( gfp ) )
  122. {
  123. // clean up of allocated memory
  124. lame_close( gfp );
  125. }
  126. */
  127. ExitMP3(&my_DecodeData);
  128. bResult = true;
  129. return bResult;
  130. }
  131. DWORD DecodeStream::GetOutputSizeForInput(const DWORD the_SrcLength) const
  132. {
  133. DWORD Result;
  134. double OutputInputRatio = double(my_SamplesPerSec * 2 * my_Channels) / double(my_SourceBitrate);
  135. OutputInputRatio *= 1.15; // allow 15% more
  136. Result = DWORD(double(the_SrcLength) * OutputInputRatio);
  137. my_debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "Result = %d (OutputInputRatio = %f)",Result,OutputInputRatio);
  138. return Result;
  139. }
  140. bool DecodeStream::ConvertBuffer(LPACMDRVSTREAMHEADER a_StreamHeader)
  141. {
  142. bool result = false;
  143. if (my_debug != NULL)
  144. {
  145. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "enter DecodeStream::ConvertBuffer");
  146. }
  147. int ProcessedBytes;
  148. int ret = decodeMP3(&my_DecodeData, a_StreamHeader->pbSrc, a_StreamHeader->cbSrcLength, (char *)a_StreamHeader->pbDst, a_StreamHeader->cbDstLength, &ProcessedBytes);
  149. switch (ret)
  150. {
  151. case MP3_OK:
  152. a_StreamHeader->cbSrcLengthUsed = a_StreamHeader->cbSrcLength;
  153. a_StreamHeader->cbDstLengthUsed = ProcessedBytes;
  154. result = true;
  155. break;
  156. case MP3_NEED_MORE:
  157. a_StreamHeader->cbSrcLengthUsed = 0;
  158. a_StreamHeader->cbDstLengthUsed = 0;
  159. break;
  160. case MP3_ERR:
  161. break;
  162. }
  163. /*
  164. DWORD InSize = a_StreamHeader->cbSrcLength / 2, OutSize = a_StreamHeader->cbDstLength; // 2 for 8<->16 bits
  165. // Encode it
  166. int dwSamples;
  167. int nOutputSamples = 0;
  168. dwSamples = InSize / lame_get_num_channels( gfp );
  169. if ( 1 == lame_get_num_channels( gfp ) )
  170. {
  171. nOutputSamples = lame_encode_buffer(gfp,(PSHORT)a_StreamHeader->pbSrc,(PSHORT)a_StreamHeader->pbSrc,dwSamples,a_StreamHeader->pbDst,a_StreamHeader->cbDstLength);
  172. }
  173. else
  174. {
  175. nOutputSamples = lame_encode_buffer_interleaved(gfp,(PSHORT)a_StreamHeader->pbSrc,dwSamples,a_StreamHeader->pbDst,a_StreamHeader->cbDstLength);
  176. }
  177. a_StreamHeader->cbSrcLengthUsed = a_StreamHeader->cbSrcLength;
  178. a_StreamHeader->cbDstLengthUsed = nOutputSamples;
  179. result = a_StreamHeader->cbDstLengthUsed <= a_StreamHeader->cbDstLength;
  180. */
  181. my_debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "UsedSize = %d / EncodedSize = %d, result = %d, ret = %s", a_StreamHeader->cbSrcLengthUsed, a_StreamHeader->cbDstLengthUsed, result,
  182. (ret == MP3_OK)?"MP3_OK":(ret == MP3_NEED_MORE)?"MP3_NEED_MORE":"error");
  183. if (my_debug != NULL)
  184. {
  185. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "DecodeStream::ConvertBuffer result = %d",result);
  186. }
  187. return result;
  188. }
  189. #endif // ENABLE_DECODING