hxcinst.cpp
Upload User: zhongxx05
Upload Date: 2007-06-06
Package Size: 33641k
Code Size: 10k
Category:

Symbian

Development Platform:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK ***** 
  2.  * Version: RCSL 1.0/RPSL 1.0 
  3.  *  
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
  5.  *      
  6.  * The contents of this file, and the files included with this file, are 
  7.  * subject to the current version of the RealNetworks Public Source License 
  8.  * Version 1.0 (the "RPSL") available at 
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
  10.  * the file under the RealNetworks Community Source License Version 1.0 
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
  12.  * in which case the RCSL will apply. You may also obtain the license terms 
  13.  * directly from RealNetworks.  You may not use this file except in 
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
  16.  * RCSL for the rights, obligations and limitations governing use of the 
  17.  * contents of the file.  
  18.  *  
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the 
  20.  * developer of the Original Code and owns the copyrights in the portions 
  21.  * it created. 
  22.  *  
  23.  * This file, and the files included with this file, is distributed and made 
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  28.  * 
  29.  * Technology Compatibility Kit Test Suite(s) Location: 
  30.  *    http://www.helixcommunity.org/content/tck 
  31.  * 
  32.  * Contributor(s): 
  33.  *  
  34.  * ***** END LICENSE BLOCK ***** */ 
  35. #include "hxvamain.h"
  36. #include "hvenc.h"
  37. #include "h261defs.h"
  38. #include "hvscodes.h"
  39. #include "hxcinst.h"
  40. #include "h263codecf.h"
  41. #include "memory.h"
  42. #ifdef _MACINTOSH
  43. #include <string.h> // for memcpy
  44. #endif
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /* Function Protos      */
  49. U32 VvOpenDecoder ( S16 format );
  50. U32 VvGetDecoderPtr ( S16 index );
  51. U32 VvDecode ( S16 index, U32 PBframeCap, PICTURE_DESCR* picdesc,
  52.                           U32 bsStart, U32 bsEnd, S16* nextGOB, PICTURE* newPic, 
  53.                           VvDecoderStats* status, U32 newBs );
  54.               
  55. U32 VvCloseDecoder (S16 index );
  56. U32 VvDecGetMBMap ( S16 index, U32 mapType, U32 outMap );
  57. }
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. inline DWORD WIDTHBYTES(DWORD bits) 
  60. {
  61.     return (((bits) + 31) / 32 * 4);
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. inline DWORD ROUNDTO16(DWORD a) 
  65. {
  66.     return ((a+15)>>4)<<4;
  67. }
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. inline BOOL IsOpenDecoder(S16 hDecoder)
  70. {
  71.     return 1<=hDecoder && hDecoder<=MAX_NUM_DECODERS;
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. inline void MarkDecoderClosed(S16& hDecoder)
  75. {
  76.     hDecoder = MAX_NUM_DECODERS+1;
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  79. inline U32 BITNUM(VvBitIndex q) 
  80. {
  81.     return ((((q).l >> 13) &0x7FFF8) + ((q).l&7));
  82. }
  83. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  84. inline U32 BITSDIFF(VvBitIndex start,VvBitIndex finish) 
  85. {
  86.     return (BITNUM(finish)-BITNUM(start));
  87. }
  88. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  89. inline U32 BYTESDIFF(VvBitIndex start,VvBitIndex finish)
  90. {
  91.     return (BITSDIFF(start,finish) + 7) >> 3;
  92. }
  93. const S16 HXH263CodecInstance::capsCIF(CIF);
  94. const U32 HXH263CodecInstance::effFactor(56);
  95. const U32 HXH263CodecInstance::h263MaxInterFrames(31);
  96. const U32 HXH263CodecInstance::bogusId(1);
  97. const U32 HXH263CodecInstance::cifHeight(288);
  98. const U32 HXH263CodecInstance::cifWidth(352);
  99. const U32 HXH263CodecInstance::qcifHeight(144);
  100. const U32 HXH263CodecInstance::qcifWidth(176);
  101. const U32 HXH263CodecInstance::sqcifHeight(96);
  102. const U32 HXH263CodecInstance::sqcifWidth(128);
  103. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. HXH263CodecInstance::HXH263CodecInstance() :
  105. m_fccType(0),
  106. m_openFlags(0),
  107. m_vcmVersion(0),
  108. m_lpDecoderPtr(NULL)
  109. {
  110.     MarkDecoderClosed(m_hDecoder);
  111. }
  112. BOOL HXH263CodecInstance::Initialize(void)
  113. {
  114.     // make sure we're being opened as a video compressor
  115.     m_fccType = 1;
  116.     m_openFlags = 0;
  117.     m_vcmVersion = 1;
  118.     return TRUE;
  119. }
  120.     
  121. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  122. HX_RESULT HXH263CodecInstance::DecompressStart( 
  123. const HXVA_Image_Format &srcFormat,
  124. HXVA_Image_Format &dstFormat
  125. )
  126. {
  127.     // Somebody called start without end
  128.     //if(IsOpenDecoder(m_hDecoder)) 
  129. // DecompressEnd();
  130.     
  131.     // open decoder with CIF capabilities
  132.     // FIX: must check for success here
  133.     m_hDecoder = (S16) VvOpenDecoder(capsCIF); 
  134.     
  135.     if(m_hDecoder > MAX_NUM_DECODERS)     {
  136.         MarkDecoderClosed(m_hDecoder);
  137.         return HXR_OUTOFMEMORY;
  138.     }
  139.     
  140.     // Initialize picture descriptor
  141.     m_PicDesc.rows = ROUNDTO16(srcFormat.dimensions.height);
  142.     m_PicDesc.cols = ROUNDTO16(srcFormat.dimensions.width);
  143.     // Initialize the output picture
  144.     m_PicOut.picLayout = VIVO_YUV12;
  145.     m_PicOut.color = 1;
  146.     // initialize remaining decoder state
  147.     m_nextGob = 0;
  148.     // Grab the decoder pointer
  149.     m_lpDecoderPtr = (LPBYTE) VvGetDecoderPtr(m_hDecoder);
  150.     if(m_lpDecoderPtr==NULL)    {
  151.         DecompressEnd();
  152.         return HXR_OUTOFMEMORY;
  153.     }
  154.     return HXR_OK;
  155. }
  156. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. HX_RESULT HXH263CodecInstance::DecompressEnd()
  158. {
  159.     if(IsOpenDecoder(m_hDecoder)) 
  160. VvCloseDecoder(m_hDecoder);
  161.     MarkDecoderClosed(m_hDecoder);
  162.     m_lpDecoderPtr = NULL;
  163.     return HXR_OK;
  164. }
  165. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  166. HX_RESULT HXH263CodecInstance::DecompressConvert(
  167. const HXVA_Image &src,
  168. HXVA_Image &dst
  169. )
  170. {
  171.     static U32 EOS(0x00FC0000);
  172.     // Cool, the NetShow player seems to send ICM_DECOMPRESS_CONVERT before
  173.     // sending ICM_DECOMPRESS_BEGIN (at least sometimes!)
  174.     if(!IsOpenDecoder(m_hDecoder)) 
  175.  DecompressStart(src.format, dst.format);
  176.     VvBitIndex begin,end,ret;
  177.     // if biSizeImage == 1 it means we have the second half of a PB frame; set it to 0 and decode
  178.     //if(lpDEX->lpbiSrc->biSizeImage == 1) 
  179. // lpDEX->lpbiSrc->biSizeImage = 0;
  180.     begin.l = 0;
  181.     end.ww.byte = (U16)(src.size) + 4; // make room for EOS
  182.     end.l = end.ww.byte * 8 + 1;
  183.     U32 result = 1;
  184.     
  185.     // copy the incoming data to the decoder buffer
  186.     LPBYTE lpData = GetDecoderPtr(); 
  187.     U32 *lpEOS = &EOS; // WIN16 days r over
  188. if(!lpData) {//decoder was obviously closed already
  189. return HXR_FAIL;
  190. }
  191.     memcpy(lpData, src.GetBasePointer(), src.size); /* Flawfinder: ignore */
  192.     memcpy(lpData+(int)src.size, lpEOS, 4); /* Flawfinder: ignore */
  193.     static const U32 PBFrameCap(1);
  194.     
  195.     m_PicDesc.decodingInProgress = FALSE;
  196.     PICTURE *lpPicOut = &m_PicOut;
  197.     PICTURE_DESCR *lpPicDesc = &m_PicDesc;
  198.     S16 *lpNextGob = &m_nextGob;
  199.     VvDecoderStats *lpDecoderStats = &m_DecoderStats;
  200.     VvBitIndex *lpRet = &ret;
  201.     do  {
  202.         result = 
  203.         VvDecode ( m_hDecoder,          // decoder index
  204.             PBFrameCap,                 // reconstruct B frames
  205.             lpPicDesc,            // frame data
  206.             (U32) begin.l,          // first bit to decode
  207.             (U32) end.l,                // last bit (+1) to decode
  208.             lpNextGob,            // next gob to process
  209.             lpPicOut,         // pointer to output pix
  210.             lpDecoderStats,   // pointer to stats block
  211.             (U32) lpRet );              // pos of first bit
  212.     }
  213.     while(m_PicDesc.decodingInProgress);
  214.     // copy decoded frame to the destination buffer
  215. // YUV Do something..
  216. unsigned char* pDst = (unsigned char*)dst.GetBasePointer();
  217. S32 lumaHeight = dst.format.dimensions.height;
  218.     S32 chromaHeight = (lumaHeight+1)>1;
  219.     // Now turn YUV pointers upside down before passing to DoRGB16
  220.     unsigned char* pY = m_PicOut.y.ptr;
  221.     unsigned char* pCr = m_PicOut.cr.ptr;
  222.     unsigned char* pCb = m_PicOut.cb.ptr;
  223.     int yOffset = m_PicOut.y.hoffset;
  224.     int crOffset = m_PicOut.cr.hoffset;
  225.     int cbOffset = m_PicOut.cb.hoffset;
  226. int chromaLines = m_PicOut.cb.nvert;
  227. int chromaBytes = m_PicOut.cb.nhor;
  228. int memsz = lumaHeight*yOffset;
  229. if(m_PicOut.picLayout == VVS_LAYOUT_YUV12) memsz += (memsz>>1);
  230. memcpy(pDst, pY, memsz); /* Flawfinder: ignore */
  231. if(m_PicOut.picLayout == VVS_LAYOUT_TEE) 
  232. {
  233. // optimised layout for color conversion.
  234. pDst += lumaHeight*yOffset;
  235. unsigned char * pDst2 = pDst+(chromaLines*chromaBytes);
  236. for(int i=0;i<chromaLines;i++)
  237. {
  238. memcpy(pDst2, pCr, chromaBytes); /* Flawfinder: ignore */
  239. memcpy(pDst, pCb, chromaBytes); /* Flawfinder: ignore */
  240. pCr+=crOffset;
  241. pCb+=cbOffset;
  242. pDst+=chromaBytes;
  243. pDst2+=chromaBytes;
  244. }
  245. }
  246.     return HXR_OK;
  247. }
  248. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  249. HX_RESULT HXH263CodecInstance::DecompressVerifyParams(void)
  250. {
  251.     return HXR_OK;
  252. }
  253.     
  254. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  255. HX_RESULT HXH263CodecInstance::DecompressGetOutputFormat( void)
  256. {
  257.     return HXR_OK;
  258. }