CodecMgr.cpp
上传用户:oldpeter23
上传日期:2013-01-09
资源大小:1111k
文件大小:5k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: CodecMgr.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include "WndX.h"
  13. #include "g729a.h"
  14. #include "UDPSocket.h"
  15. #include "AVIOMgr.h"
  16. #include "CodecMgr.h"
  17. /*------------------------------------------------------------------------------*/
  18. CCodecMgr::CCodecMgr()
  19. {
  20. //VoiceAge g729a codec初始化
  21. va_g729a_init_encoder();
  22. va_g729a_init_decoder();
  23. m_hIC=NULL;
  24. m_cv.cbSize=sizeof(m_cv);
  25. m_cv.dwFlags=ICMF_COMPVARS_VALID ;
  26. m_cv.hic=m_hIC;
  27. m_cv.fccType=ICTYPE_VIDEO ;
  28. m_cv.fccHandler=859189837;
  29. m_cv.lpbiOut=0;
  30. m_cv.lKey=10;
  31. m_cv.lDataRate=6;
  32. m_cv.lQ=10000;
  33. //将视频格式固定(这是非常常用的格式,一般的视频捕捉设备应该会支持)
  34. m_BmpU.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  35. m_BmpU.bmiHeader.biWidth=176;
  36. m_BmpU.bmiHeader.biHeight=144;
  37. m_BmpU.bmiHeader.biPlanes=1;
  38. m_BmpU.bmiHeader.biBitCount=24;
  39. m_BmpU.bmiHeader.biSizeImage=76032;
  40. }
  41. /*------------------------------------------------------------------------------*/
  42. CCodecMgr::~CCodecMgr()
  43. {
  44. DestroyCodecA();
  45. DestroyCodecV();
  46. }
  47. /*------------------------------------------------------------------------------*/
  48. //初始化视频编码解码器
  49. BOOL CCodecMgr::InitCodecV()
  50. {
  51. DestroyCodecV();
  52. BOOL bRet=FALSE;
  53. //
  54. m_hIC=ICOpen(ICTYPE_VIDEO,859189837,ICMODE_COMPRESS|ICMODE_DECOMPRESS);
  55. if(!m_hIC)
  56. goto RET;
  57. ICCompressGetFormat(m_hIC,&m_BmpU,&m_BmpC);
  58. //微软的H.263 codec需要消息确认,得到这个不容易
  59. ICSendMessage(m_hIC,0x60c9,0xf7329ace,0xacdeaea2);
  60. m_cv.hic=m_hIC;
  61. m_cv.dwFlags=ICMF_COMPVARS_VALID;
  62. //star sequence of frames compression
  63. if(!ICSeqCompressFrameStart(&m_cv,&m_BmpU))
  64. goto RET;
  65. //star decompression;
  66. if(ICDecompressBegin(m_hIC,&m_BmpC,&m_BmpU)!=ICERR_OK)
  67. goto RET;
  68. bRet=TRUE;
  69. RET:
  70. if(!bRet)
  71. DestroyCodecV();
  72. return bRet;
  73. }
  74. /*------------------------------------------------------------------------------*/
  75. void CCodecMgr::DestroyCodecV()
  76. {
  77. if(m_hIC)
  78. {
  79. ICDecompressEnd(m_hIC);
  80. ICSeqCompressFrameEnd(&m_cv);
  81. ICCompressorFree(&m_cv);
  82. ICClose(m_hIC);
  83. m_hIC=NULL;
  84. }
  85. }
  86. /*------------------------------------------------------------------------------*/
  87. BOOL CCodecMgr::InitCodecA()
  88. {
  89. return TRUE;
  90. }
  91. /*------------------------------------------------------------------------------*/
  92. void CCodecMgr::DestroyCodecA()
  93. {
  94. }
  95. /*------------------------------------------------------------------------------*/
  96. //压缩音频帧
  97. BOOL CCodecMgr::EncodeAudioData(char *pin,int len,char* pout,int* lenr)
  98. {
  99. BOOL bRet=FALSE;
  100. if(!pin||len!=SIZE_AUDIO_FRAME||!pout)
  101. goto RET;
  102. //encode the audio frames
  103. va_g729a_encoder((short*)pin,(BYTE*)pout);
  104. va_g729a_encoder((short*)(pin+160),(BYTE*)pout+10);
  105. va_g729a_encoder((short*)(pin+320),(BYTE*)pout+20);
  106. va_g729a_encoder((short*)(pin+480),(BYTE*)pout+30);
  107. va_g729a_encoder((short*)(pin+640),(BYTE*)pout+40);
  108. va_g729a_encoder((short*)(pin+800),(BYTE*)pout+50);
  109. if(lenr)
  110. *lenr=SIZE_AUDIO_PACKED;
  111. bRet=TRUE;
  112. RET:
  113. return bRet;
  114. }
  115. /*------------------------------------------------------------------------------*/
  116. //解压缩音频帧
  117. BOOL CCodecMgr::DecodeAudioData(char *pin,int len,char* pout,int* lenr)
  118. {
  119. BOOL bRet=FALSE;
  120. if(!pin||len!=SIZE_AUDIO_PACKED||!pout)
  121. goto RET;
  122. //decode the audio frames
  123. va_g729a_decoder((BYTE*)pin,(short*)(pout),0);
  124. va_g729a_decoder((BYTE*)pin+10,(short*)(pout+160),0);
  125. va_g729a_decoder((BYTE*)pin+20,(short*)(pout+320),0);
  126. va_g729a_decoder((BYTE*)pin+30,(short*)(pout+480),0);
  127. va_g729a_decoder((BYTE*)pin+40,(short*)(pout+640),0);
  128. va_g729a_decoder((BYTE*)pin+50,(short*)(pout+800),0);
  129. if(lenr)
  130. *lenr=SIZE_AUDIO_FRAME;
  131. bRet=TRUE;
  132. RET:
  133. return bRet;
  134. }
  135. /*------------------------------------------------------------------------------*/
  136. //解压缩视频帧
  137. BOOL CCodecMgr::DecodeVideoData(char *pin,int len,char* pout,int *lenr,DWORD flag)
  138. {
  139. BOOL bRet=FALSE;
  140. if(!pin||!pout||!m_hIC)
  141. goto RET;
  142. //
  143. if(ICDecompress(m_hIC,flag,&m_BmpC.bmiHeader,pin,&m_BmpU.bmiHeader,pout)!=ICERR_OK)
  144. goto RET;
  145. if(lenr)
  146. *lenr=m_BmpU.bmiHeader.biSizeImage;
  147. bRet=TRUE;
  148. RET:
  149. return bRet;
  150. }
  151. /*------------------------------------------------------------------------------*/
  152. //压缩视频帧
  153. BOOL CCodecMgr::EncodeVideoData(char* pin,int len,char* pout,int* lenr,BOOL* pKey)
  154. {
  155. BOOL bRet=FALSE;
  156. char*p;
  157. long s=2000;
  158. BOOL k;
  159. if(!pin||!pout||len!=(int)m_BmpU.bmiHeader.biSizeImage||!m_hIC)
  160. goto RET;
  161. p=(char*)ICSeqCompressFrame(&m_cv,0,pin,&k,&s);
  162. if(!p)
  163. goto RET;
  164. if(lenr)
  165. *lenr=s;
  166. if(pKey)
  167. *pKey=k;
  168. if(s>MAX_UDP_SIZE-sizeof(PACK_VIDEO))
  169. goto RET;
  170. CopyMemory(pout,p,s);
  171. bRet=TRUE;
  172. RET:
  173. return bRet;
  174. }