shithub: openh264

Download patch

ref: 691e8379b596afc52dfc3784bf032d79bce7248b
parent: 99565beb817c81a1f4febd0638215fb1afc38f6b
parent: 043575ffb0309be318776673551e0abfa3089c55
author: Ethan Hugg <ethanhugg@gmail.com>
date: Wed Jan 22 03:11:40 EST 2014

Merge pull request #184 from mstorsjo/c-interface-vtbl

Add a public C API to the library by mimicking the C++ ABI with a C struct

--- a/codec/api/svc/codec_api.h
+++ b/codec/api/svc/codec_api.h
@@ -36,36 +36,43 @@
 #include "codec_app_def.h"
 #include "codec_def.h"
 
+#if defined(_WIN32) || defined(__cdecl)
+#define EXTAPI __cdecl
+#else
+#define EXTAPI
+#endif
+
+#ifdef __cplusplus
 class ISVCEncoder {
  public:
   /*
    * return: CM_RETURN: 0 - success; otherwise - failed;
    */
-  virtual int Initialize (SVCEncodingParam* pParam, const INIT_TYPE kiInitType = INIT_TYPE_PARAMETER_BASED) = 0;
-  virtual int Initialize (void* pParam, const INIT_TYPE kiInitType = INIT_TYPE_CONFIG_BASED) = 0;
+  virtual int EXTAPI Initialize (SVCEncodingParam* pParam, const INIT_TYPE kiInitType = INIT_TYPE_PARAMETER_BASED) = 0;
+  virtual int EXTAPI Initialize2 (void* pParam, const INIT_TYPE kiInitType = INIT_TYPE_CONFIG_BASED) = 0;
 
-  virtual int Uninitialize() = 0;
+  virtual int EXTAPI Uninitialize() = 0;
 
   /*
    * return: EVideoFrameType [IDR: videoFrameTypeIDR; P: videoFrameTypeP; ERROR: videoFrameTypeInvalid]
    */
-  virtual int EncodeFrame (const unsigned char* kpSrc, SFrameBSInfo* pBsInfo) = 0;
-  virtual int EncodeFrame (const SSourcePicture**   kppSrcPicList, int nSrcPicNum, SFrameBSInfo* pBsInfo) = 0;
+  virtual int EXTAPI EncodeFrame (const unsigned char* kpSrc, SFrameBSInfo* pBsInfo) = 0;
+  virtual int EXTAPI EncodeFrame2 (const SSourcePicture**   kppSrcPicList, int nSrcPicNum, SFrameBSInfo* pBsInfo) = 0;
 
   /*
    * return: 0 - success; otherwise - failed;
    */
-  virtual int EncodeParameterSets (SFrameBSInfo* pBsInfo) = 0;
+  virtual int EXTAPI EncodeParameterSets (SFrameBSInfo* pBsInfo) = 0;
 
   /*
    * return: 0 - success; otherwise - failed;
    */
-  virtual int PauseFrame (const unsigned char* kpSrc, SFrameBSInfo* pBsInfo) = 0;
+  virtual int EXTAPI PauseFrame (const unsigned char* kpSrc, SFrameBSInfo* pBsInfo) = 0;
 
   /*
    * return: 0 - success; otherwise - failed;
    */
-  virtual int ForceIntraFrame (bool bIDR) = 0;
+  virtual int EXTAPI ForceIntraFrame (bool bIDR) = 0;
 
   /************************************************************************
    * InDataFormat, IDRInterval, SVC Encode Param, Frame Rate, Bitrate,..
@@ -73,58 +80,118 @@
   /*
    * return: CM_RETURN: 0 - success; otherwise - failed;
    */
-  virtual int SetOption (ENCODER_OPTION eOptionId, void* pOption) = 0;
-  virtual int GetOption (ENCODER_OPTION eOptionId, void* pOption) = 0;
+  virtual int EXTAPI SetOption (ENCODER_OPTION eOptionId, void* pOption) = 0;
+  virtual int EXTAPI GetOption (ENCODER_OPTION eOptionId, void* pOption) = 0;
 };
 
 class ISVCDecoder {
  public:
-  virtual long Initialize (void* pParam, const INIT_TYPE iInitType) = 0;
-  virtual long Uninitialize() = 0;
+  virtual long EXTAPI Initialize (void* pParam, const INIT_TYPE iInitType) = 0;
+  virtual long EXTAPI Uninitialize() = 0;
 
-  virtual DECODING_STATE DecodeFrame (const unsigned char* pSrc,
-                                      const int iSrcLen,
-                                      unsigned char** ppDst,
-                                      int* pStride,
-                                      int& iWidth,
-                                      int& iHeight) = 0;
+  virtual DECODING_STATE EXTAPI DecodeFrame (const unsigned char* pSrc,
+                                             const int iSrcLen,
+                                             unsigned char** ppDst,
+                                             int* pStride,
+                                             int& iWidth,
+                                             int& iHeight) = 0;
 
   /*
    *  src must be 4 byte aligned,   recommend 16 byte aligned.    the available src size must be multiple of 4.
    */
-  virtual DECODING_STATE DecodeFrame (const unsigned char* pSrc,
-                                      const int iSrcLen,
-                                      void** ppDst,
-                                      SBufferInfo* pDstInfo) = 0;
+  virtual DECODING_STATE EXTAPI DecodeFrame2 (const unsigned char* pSrc,
+                                              const int iSrcLen,
+                                              void** ppDst,
+                                              SBufferInfo* pDstInfo) = 0;
 
   /*
    *  src must be 4 byte aligned,   recommend 16 byte aligned.    the available src size must be multiple of 4.
    *  this API does not work for now!! This is for future use to support non-I420 color format output.
    */
-  virtual DECODING_STATE DecodeFrameEx (const unsigned char* pSrc,
-                                        const int iSrcLen,
-                                        unsigned char* pDst,
-                                        int iDstStride,
-                                        int& iDstLen,
-                                        int& iWidth,
-                                        int& iHeight,
-                                        int& iColorFormat) = 0;
+  virtual DECODING_STATE EXTAPI DecodeFrameEx (const unsigned char* pSrc,
+                                               const int iSrcLen,
+                                               unsigned char* pDst,
+                                               int iDstStride,
+                                               int& iDstLen,
+                                               int& iWidth,
+                                               int& iHeight,
+                                               int& iColorFormat) = 0;
 
   /*************************************************************************
    * OutDataFormat
    *************************************************************************/
-  virtual long SetOption (DECODER_OPTION eOptionId, void* pOption) = 0;
-  virtual long GetOption (DECODER_OPTION eOptionId, void* pOption) = 0;
+  virtual long EXTAPI SetOption (DECODER_OPTION eOptionId, void* pOption) = 0;
+  virtual long EXTAPI GetOption (DECODER_OPTION eOptionId, void* pOption) = 0;
 };
 
 
 extern "C"
 {
+#else
+
+typedef struct ISVCEncoderVtbl ISVCEncoderVtbl;
+typedef const ISVCEncoderVtbl* ISVCEncoder;
+struct ISVCEncoderVtbl {
+
+  int (*Initialize) (ISVCEncoder*, SVCEncodingParam* pParam, const INIT_TYPE kiInitType);
+  int (*Initialize2) (ISVCEncoder*, void* pParam, const INIT_TYPE kiInitType);
+
+  int (*Uninitialize) (ISVCEncoder*);
+
+  int (*EncodeFrame) (ISVCEncoder*, const unsigned char* kpSrc, SFrameBSInfo* pBsInfo);
+  int (*EncodeFrame2) (ISVCEncoder*, const SSourcePicture**   kppSrcPicList, int nSrcPicNum, SFrameBSInfo* pBsInfo);
+
+  int (*EncodeParameterSets) (ISVCEncoder*, SFrameBSInfo* pBsInfo);
+
+  int (*PauseFrame) (ISVCEncoder*, const unsigned char* kpSrc, SFrameBSInfo* pBsInfo);
+
+  int (*ForceIntraFrame) (ISVCEncoder*, bool bIDR);
+
+  int (*SetOption) (ISVCEncoder*, ENCODER_OPTION eOptionId, void* pOption);
+  int (*GetOption) (ISVCEncoder*, ENCODER_OPTION eOptionId, void* pOption);
+};
+
+typedef struct ISVCDecoderVtbl ISVCDecoderVtbl;
+typedef const ISVCDecoderVtbl* ISVCDecoder;
+struct ISVCDecoderVtbl {
+  long (*Initialize) (ISVCDecoder*, void* pParam, const INIT_TYPE iInitType);
+  long (*Uninitialize) (ISVCDecoder*);
+
+  DECODING_STATE (*DecodeFrame) (ISVCDecoder*, const unsigned char* pSrc,
+                                 const int iSrcLen,
+                                 unsigned char** ppDst,
+                                 int* pStride,
+                                 int* iWidth,
+                                 int* iHeight);
+
+  DECODING_STATE (*DecodeFrame2) (ISVCDecoder*, const unsigned char* pSrc,
+                                  const int iSrcLen,
+                                  void** ppDst,
+                                  SBufferInfo* pDstInfo);
+
+  DECODING_STATE (*DecodeFrameEx) (ISVCDecoder*, const unsigned char* pSrc,
+                                   const int iSrcLen,
+                                   unsigned char* pDst,
+                                   int iDstStride,
+                                   int* iDstLen,
+                                   int* iWidth,
+                                   int* iHeight,
+                                   int* iColorFormat);
+
+  long (*SetOption) (ISVCDecoder*, DECODER_OPTION eOptionId, void* pOption);
+  long (*GetOption) (ISVCDecoder*, DECODER_OPTION eOptionId, void* pOption);
+};
+#endif
+
+
   int  CreateSVCEncoder (ISVCEncoder** ppEncoder);
   void DestroySVCEncoder (ISVCEncoder* pEncoder);
 
   long CreateDecoder (ISVCDecoder** ppDecoder);
   void DestroyDecoder (ISVCDecoder* pDecoder);
+
+#ifdef __cplusplus
 }
+#endif
 
 #endif//WELS_VIDEO_CODEC_SVC_API_H__
--- a/codec/console/dec/src/h264dec.cpp
+++ b/codec/console/dec/src/h264dec.cpp
@@ -217,7 +217,7 @@
     pData[2] = NULL;
     memset (&sDstBufInfo, 0, sizeof (SBufferInfo));
 
-    pDecoder->DecodeFrame (pBuf + iBufPos, iSliceSize, pData, &sDstBufInfo);
+    pDecoder->DecodeFrame2 (pBuf + iBufPos, iSliceSize, pData, &sDstBufInfo);
 
     if (sDstBufInfo.iBufferStatus == 1) {
       pDst[0] = (uint8_t*)pData[0];
@@ -259,7 +259,7 @@
   pData[2] = NULL;
   memset (&sDstBufInfo, 0, sizeof (SBufferInfo));
 
-  pDecoder->DecodeFrame (NULL, 0, pData, &sDstBufInfo);
+  pDecoder->DecodeFrame2 (NULL, 0, pData, &sDstBufInfo);
   if (sDstBufInfo.iBufferStatus == 1) {
     pDst[0] = (uint8_t*)pData[0];
     pDst[1] = (uint8_t*)pData[1];
--- a/codec/console/enc/src/welsenc.cpp
+++ b/codec/console/enc/src/welsenc.cpp
@@ -964,7 +964,7 @@
   sSvcParam.sDependencyLayers[sSvcParam.iNumDependencyLayer - 1].iFrameHeight =
     WELS_ALIGN(sSvcParam.sDependencyLayers[sSvcParam.iNumDependencyLayer - 1].iActualHeight, MB_HEIGHT_LUMA);
 
-  if (cmResultSuccess != pPtrEnc->Initialize ((void*)&sSvcParam, INIT_TYPE_CONFIG_BASED)) {	// SVC encoder initialization
+  if (cmResultSuccess != pPtrEnc->Initialize2 ((void*)&sSvcParam, INIT_TYPE_CONFIG_BASED)) {	// SVC encoder initialization
     fprintf (stderr, "SVC encoder Initialize failed\n");
     iRet = 1;
     goto INSIDE_MEM_FREE;
@@ -1094,7 +1094,7 @@
 
     // To encoder this frame
     iStart	= WelsTime();
-    int iEncFrames = pPtrEnc->EncodeFrame (const_cast<const SSourcePicture**> (pSrcPicList), nSpatialLayerNum, &sFbi);
+    int iEncFrames = pPtrEnc->EncodeFrame2 (const_cast<const SSourcePicture**> (pSrcPicList), nSpatialLayerNum, &sFbi);
     iTotal += WelsTime() - iStart;
 
     // fixed issue in case dismatch source picture introduced by frame skipped, 1/12/2010
--- a/codec/decoder/plus/inc/welsDecoderExt.h
+++ b/codec/decoder/plus/inc/welsDecoderExt.h
@@ -59,8 +59,8 @@
 CWelsDecoder (void_t);
 virtual ~CWelsDecoder();
 
-virtual long Initialize (void_t* pParam, const INIT_TYPE keInitType);
-virtual long Uninitialize();
+virtual long EXTAPI Initialize (void_t* pParam, const INIT_TYPE keInitType);
+virtual long EXTAPI Uninitialize();
 
 /***************************************************************************
 *	Description:
@@ -74,28 +74,28 @@
 *
 *	return: if decode frame success return 0, otherwise corresponding error returned.
 ***************************************************************************/
-virtual DECODING_STATE DecodeFrame (const unsigned char* kpSrc,
-                                    const int kiSrcLen,
-                                    unsigned char** ppDst,
-                                    int* pStride,
-                                    int& iWidth,
-                                    int& iHeight);
+virtual DECODING_STATE EXTAPI DecodeFrame (const unsigned char* kpSrc,
+                                           const int kiSrcLen,
+                                           unsigned char** ppDst,
+                                           int* pStride,
+                                           int& iWidth,
+                                           int& iHeight);
 
-virtual DECODING_STATE DecodeFrame (const unsigned char* kpSrc,
-                                    const int kiSrcLen,
-                                    void_t** ppDst,
-                                    SBufferInfo* pDstInfo);
-virtual DECODING_STATE DecodeFrameEx (const unsigned char* kpSrc,
-                                      const int kiSrcLen,
-                                      unsigned char* pDst,
-                                      int iDstStride,
-                                      int& iDstLen,
-                                      int& iWidth,
-                                      int& iHeight,
-                                      int& color_format);
+virtual DECODING_STATE EXTAPI DecodeFrame2 (const unsigned char* kpSrc,
+                                            const int kiSrcLen,
+                                            void_t** ppDst,
+                                            SBufferInfo* pDstInfo);
+virtual DECODING_STATE EXTAPI DecodeFrameEx (const unsigned char* kpSrc,
+                                             const int kiSrcLen,
+                                             unsigned char* pDst,
+                                             int iDstStride,
+                                             int& iDstLen,
+                                             int& iWidth,
+                                             int& iHeight,
+                                             int& color_format);
 
-virtual long SetOption (DECODER_OPTION eOptID, void_t* pOption);
-virtual long GetOption (DECODER_OPTION eOptID, void_t* pOption);
+virtual long EXTAPI SetOption (DECODER_OPTION eOptID, void_t* pOption);
+virtual long EXTAPI GetOption (DECODER_OPTION eOptID, void_t* pOption);
 
  private:
 PWelsDecoderContext 				m_pDecContext;
--- a/codec/decoder/plus/src/welsDecoderExt.cpp
+++ b/codec/decoder/plus/src/welsDecoderExt.cpp
@@ -340,7 +340,7 @@
   return cmInitParaError;
 }
 
-DECODING_STATE CWelsDecoder::DecodeFrame (const unsigned char* kpSrc,
+DECODING_STATE CWelsDecoder::DecodeFrame2 (const unsigned char* kpSrc,
     const int kiSrcLen,
     void_t** ppDst,
     SBufferInfo* pDstInfo) {
@@ -430,7 +430,7 @@
   DstInfo.UsrData.sSystemBuffer.iHeight = iHeight;
   DstInfo.eBufferProperty = BUFFER_HOST;
 
-  eDecState = DecodeFrame (kpSrc, kiSrcLen, (void_t**)ppDst, &DstInfo);
+  eDecState = DecodeFrame2 (kpSrc, kiSrcLen, (void_t**)ppDst, &DstInfo);
   if (eDecState == dsErrorFree) {
     pStride[0] = DstInfo.UsrData.sSystemBuffer.iStride[0];
     pStride[1] = DstInfo.UsrData.sSystemBuffer.iStride[1];
--- a/codec/encoder/plus/inc/welsEncoderExt.h
+++ b/codec/encoder/plus/inc/welsEncoderExt.h
@@ -65,31 +65,31 @@
   /*
    * return: CM_RETURN: 0 - success; otherwise - failed;
    */
-  virtual int Initialize (SVCEncodingParam* argv, const INIT_TYPE init_type);
-  virtual int Initialize (void* argv, const INIT_TYPE init_type);
+  virtual int EXTAPI Initialize (SVCEncodingParam* argv, const INIT_TYPE init_type);
+  virtual int EXTAPI Initialize2 (void* argv, const INIT_TYPE init_type);
 
-  virtual int Uninitialize();
+  virtual int EXTAPI Uninitialize();
 
   /*
    * return: EVideoFrameType [IDR: videoFrameTypeIDR; P: videoFrameTypeP; ERROR: videoFrameTypeInvalid]
    */
-  virtual int EncodeFrame (const unsigned char* kpSrc, SFrameBSInfo* pBsInfo);
-  virtual int EncodeFrame (const SSourcePicture** kppSrcPicList, int nSrcPicNum, SFrameBSInfo* pBsInfo);
+  virtual int EXTAPI EncodeFrame (const unsigned char* kpSrc, SFrameBSInfo* pBsInfo);
+  virtual int EXTAPI EncodeFrame2 (const SSourcePicture** kppSrcPicList, int nSrcPicNum, SFrameBSInfo* pBsInfo);
 
   /*
    * return: 0 - success; otherwise - failed;
    */
-  virtual int EncodeParameterSets (SFrameBSInfo* pBsInfo);
+  virtual int EXTAPI EncodeParameterSets (SFrameBSInfo* pBsInfo);
 
   /*
    * return: 0 - success; otherwise - failed;
    */
-  virtual int PauseFrame (const unsigned char* pSrc, SFrameBSInfo* pBsInfo);
+  virtual int EXTAPI PauseFrame (const unsigned char* pSrc, SFrameBSInfo* pBsInfo);
 
   /*
    * return: 0 - success; otherwise - failed;
    */
-  virtual int ForceIntraFrame (bool bIDR);
+  virtual int EXTAPI ForceIntraFrame (bool bIDR);
 
   /************************************************************************
    * InDataFormat, IDRInterval, SVC Encode Param, Frame Rate, Bitrate,..
@@ -97,8 +97,8 @@
   /*
    * return: CM_RETURN: 0 - success; otherwise - failed;
    */
-  virtual int SetOption (ENCODER_OPTION opt_id, void* option);
-  virtual int GetOption (ENCODER_OPTION opt_id, void* option);
+  virtual int EXTAPI SetOption (ENCODER_OPTION opt_id, void* option);
+  virtual int EXTAPI GetOption (ENCODER_OPTION opt_id, void* option);
 
  private:
   sWelsEncCtx*	m_pEncContext;
--- a/codec/encoder/plus/src/welsEncoderExt.cpp
+++ b/codec/encoder/plus/src/welsEncoderExt.cpp
@@ -353,10 +353,10 @@
 
   m_iSrcListSize  = 1;
 
-  return Initialize ((void*)&sConfig, INIT_TYPE_CONFIG_BASED);
+  return Initialize2 ((void*)&sConfig, INIT_TYPE_CONFIG_BASED);
 }
 
-int CWelsH264SVCEncoder::Initialize (void* argv, const INIT_TYPE iInitType) {
+int CWelsH264SVCEncoder::Initialize2 (void* argv, const INIT_TYPE iInitType) {
   if (INIT_TYPE_CONFIG_BASED != iInitType || NULL == argv) {
     WelsLog (m_pEncContext, WELS_LOG_ERROR, "CWelsH264SVCEncoder::Initialize(), invalid iInitType= %d, argv= 0x%p.\n",
              iInitType, (void*)argv);
@@ -602,7 +602,7 @@
   int32_t uiFrameType = videoFrameTypeInvalid;
 
   if (RawData2SrcPic ((uint8_t*)pSrc) == 0) {
-    uiFrameType = EncodeFrame (const_cast<const SSourcePicture**> (m_pSrcPicList), 1, pBsInfo);
+    uiFrameType = EncodeFrame2 (const_cast<const SSourcePicture**> (m_pSrcPicList), 1, pBsInfo);
   }
 
 #ifdef REC_FRAME_COUNT
@@ -619,7 +619,7 @@
 }
 
 
-int CWelsH264SVCEncoder::EncodeFrame (const SSourcePicture**   pSrcPicList, int nSrcPicNum, SFrameBSInfo* pBsInfo) {
+int CWelsH264SVCEncoder::EncodeFrame2 (const SSourcePicture**   pSrcPicList, int nSrcPicNum, SFrameBSInfo* pBsInfo) {
   if (! (pSrcPicList && m_pEncContext && m_bInitialFlag)) {
     return videoFrameTypeInvalid;
   }
--- a/test/decoder_test.cpp
+++ b/test/decoder_test.cpp
@@ -67,7 +67,7 @@
   memset(data, 0, sizeof(data));
   memset(&bufInfo, 0, sizeof(SBufferInfo));
 
-  DECODING_STATE rv = decoder->DecodeFrame(src, sliceSize, data, &bufInfo);
+  DECODING_STATE rv = decoder->DecodeFrame2(src, sliceSize, data, &bufInfo);
   if (rv != dsErrorFree) {
     return false;
   }