Newer
Older
monitord / lame-3.97 / ACM / .svn / text-base / DecodeStream.cpp.svn-base
  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: DecodeStream.cpp,v 1.3 2002/01/25 17:51:42 robux4 Exp $
  25. */
  26.  
  27. #if !defined(STRICT)
  28. #define STRICT
  29. #endif // STRICT
  30.  
  31. #include <assert.h>
  32. #include <windows.h>
  33.  
  34. #ifdef ENABLE_DECODING
  35.  
  36. #include "adebug.h"
  37.  
  38. #include "DecodeStream.h"
  39.  
  40. // static methods
  41.  
  42. DecodeStream * DecodeStream::Create()
  43. {
  44. DecodeStream * Result;
  45.  
  46. Result = new DecodeStream;
  47.  
  48. return Result;
  49. }
  50.  
  51. const bool DecodeStream::Erase(const DecodeStream * a_ACMStream)
  52. {
  53. delete a_ACMStream;
  54. return true;
  55. }
  56.  
  57. // class methods
  58.  
  59. DecodeStream::DecodeStream() :
  60. m_WorkingBufferUseSize(0),
  61. gfp(NULL)
  62. {
  63. /// \todo get the debug level from the registry
  64. my_debug = new ADbg(DEBUG_LEVEL_CREATION);
  65. if (my_debug != NULL) {
  66. unsigned char DebugFileName[512];
  67.  
  68. my_debug->setPrefix("MPG123stream"); /// \todo get it from the registry
  69. my_debug->setIncludeTime(true); /// \todo get it from the registry
  70.  
  71. // Check in the registry if we have to Output Debug information
  72. DebugFileName[0] = '\0';
  73.  
  74. HKEY OssKey;
  75. if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\MUKOLI", 0, KEY_READ , &OssKey ) == ERROR_SUCCESS) {
  76. DWORD DataType;
  77. DWORD DebugFileNameSize = 512;
  78. if (RegQueryValueEx( OssKey, "DebugFile", NULL, &DataType, DebugFileName, &DebugFileNameSize ) == ERROR_SUCCESS) {
  79. if (DataType == REG_SZ) {
  80. my_debug->setUseFile(true);
  81. my_debug->setDebugFile((char *)DebugFileName);
  82. my_debug->OutPut("Debug file is %s",(char *)DebugFileName);
  83. }
  84. }
  85. }
  86. my_debug->OutPut(DEBUG_LEVEL_FUNC_START, "DecodeStream Creation (0X%08X)",this);
  87. }
  88. else {
  89. ADbg debug;
  90. debug.OutPut("DecodeStream::ACMACMStream : Impossible to create my_debug");
  91. }
  92.  
  93. }
  94.  
  95. DecodeStream::~DecodeStream()
  96. {
  97. // lame_close( gfp );
  98.  
  99. if (my_debug != NULL)
  100. {
  101. my_debug->OutPut(DEBUG_LEVEL_FUNC_START, "DecodeStream Deletion (0X%08X)",this);
  102. delete my_debug;
  103. }
  104. }
  105.  
  106. bool DecodeStream::init(const int nSamplesPerSec, const int nChannels, const int nAvgBytesPerSec, const int nSourceBitrate)
  107. {
  108. bool bResult = false;
  109.  
  110. my_SamplesPerSec = nSamplesPerSec;
  111. my_Channels = nChannels;
  112. my_AvgBytesPerSec = nAvgBytesPerSec;
  113. my_SourceBitrate = nSourceBitrate;
  114.  
  115. bResult = true;
  116.  
  117. return bResult;
  118. }
  119.  
  120. bool DecodeStream::open()
  121. {
  122. bool bResult = false;
  123.  
  124. bResult = bool(InitMP3(&my_DecodeData) != 0);
  125.  
  126. return bResult;
  127. }
  128.  
  129. bool DecodeStream::close(LPBYTE pOutputBuffer, DWORD *pOutputSize)
  130. {
  131.  
  132. bool bResult = false;
  133. /*
  134. int nOutputSamples = 0;
  135.  
  136. nOutputSamples = lame_encode_flush( gfp, pOutputBuffer, 0 );
  137.  
  138. if ( nOutputSamples < 0 )
  139. {
  140. // BUFFER_TOO_SMALL
  141. *pOutputSize = 0;
  142. }
  143. else
  144. {
  145. *pOutputSize = nOutputSamples;
  146.  
  147. bResult = true;
  148. }
  149. /*
  150. // lame will be close in VbrWriteTag function
  151. if ( !lame_get_bWriteVbrTag( gfp ) )
  152. {
  153. // clean up of allocated memory
  154. lame_close( gfp );
  155. }
  156. */
  157. ExitMP3(&my_DecodeData);
  158.  
  159. bResult = true;
  160. return bResult;
  161. }
  162.  
  163. DWORD DecodeStream::GetOutputSizeForInput(const DWORD the_SrcLength) const
  164. {
  165. DWORD Result;
  166.  
  167. double OutputInputRatio = double(my_SamplesPerSec * 2 * my_Channels) / double(my_SourceBitrate);
  168.  
  169. OutputInputRatio *= 1.15; // allow 15% more
  170.  
  171. Result = DWORD(double(the_SrcLength) * OutputInputRatio);
  172.  
  173. my_debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "Result = %d (OutputInputRatio = %f)",Result,OutputInputRatio);
  174.  
  175. return Result;
  176. }
  177.  
  178. bool DecodeStream::ConvertBuffer(LPACMDRVSTREAMHEADER a_StreamHeader)
  179. {
  180. bool result = false;
  181.  
  182. if (my_debug != NULL)
  183. {
  184. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "enter DecodeStream::ConvertBuffer");
  185. }
  186.  
  187. int ProcessedBytes;
  188.  
  189. int ret = decodeMP3(&my_DecodeData, a_StreamHeader->pbSrc, a_StreamHeader->cbSrcLength, (char *)a_StreamHeader->pbDst, a_StreamHeader->cbDstLength, &ProcessedBytes);
  190.  
  191. switch (ret)
  192. {
  193. case MP3_OK:
  194. a_StreamHeader->cbSrcLengthUsed = a_StreamHeader->cbSrcLength;
  195. a_StreamHeader->cbDstLengthUsed = ProcessedBytes;
  196. result = true;
  197. break;
  198. case MP3_NEED_MORE:
  199. a_StreamHeader->cbSrcLengthUsed = 0;
  200. a_StreamHeader->cbDstLengthUsed = 0;
  201. break;
  202. case MP3_ERR:
  203. break;
  204. }
  205.  
  206. /*
  207. DWORD InSize = a_StreamHeader->cbSrcLength / 2, OutSize = a_StreamHeader->cbDstLength; // 2 for 8<->16 bits
  208.  
  209. // Encode it
  210. int dwSamples;
  211. int nOutputSamples = 0;
  212.  
  213. dwSamples = InSize / lame_get_num_channels( gfp );
  214.  
  215. if ( 1 == lame_get_num_channels( gfp ) )
  216. {
  217. nOutputSamples = lame_encode_buffer(gfp,(PSHORT)a_StreamHeader->pbSrc,(PSHORT)a_StreamHeader->pbSrc,dwSamples,a_StreamHeader->pbDst,a_StreamHeader->cbDstLength);
  218. }
  219. else
  220. {
  221. nOutputSamples = lame_encode_buffer_interleaved(gfp,(PSHORT)a_StreamHeader->pbSrc,dwSamples,a_StreamHeader->pbDst,a_StreamHeader->cbDstLength);
  222. }
  223.  
  224. a_StreamHeader->cbSrcLengthUsed = a_StreamHeader->cbSrcLength;
  225. a_StreamHeader->cbDstLengthUsed = nOutputSamples;
  226.  
  227. result = a_StreamHeader->cbDstLengthUsed <= a_StreamHeader->cbDstLength;
  228. */
  229. my_debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "UsedSize = %d / EncodedSize = %d, result = %d, ret = %s", a_StreamHeader->cbSrcLengthUsed, a_StreamHeader->cbDstLengthUsed, result,
  230. (ret == MP3_OK)?"MP3_OK":(ret == MP3_NEED_MORE)?"MP3_NEED_MORE":"error");
  231.  
  232. if (my_debug != NULL)
  233. {
  234. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "DecodeStream::ConvertBuffer result = %d",result);
  235. }
  236.  
  237. return result;
  238. }
  239. #endif // ENABLE_DECODING