AMRPlugin.cpp
资源名称:111.rar [点击查看]
上传用户:mony888
上传日期:2022-07-26
资源大小:1247k
文件大小:6k
源码类别:
Windows CE
开发平台:
Visual C++
- #include <windows.h>
- #include "stdafx.h"
- //#include "AMRPlugin.h"
- //#include "interf_enc.h"
- //#include "sp_enc.h"
- // #include "interf_rom.h"
- #define CODEC_ID_AMR 0x1234
- // #define OUTFRAMES 16
- #define FRAMMAXSIZ 32
- static INT32 read_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
- typedef struct AMREnc_stru
- {
- INT32 bytesLeft;
- INT32 outOfData; //readBuf 是否不足一帧的数据
- INT32 eofReached;
- INT32 nFrames;
- UINT8 *readBuf;
- UINT8 *readPtr;
- UINT8 *outBuf;
- INT32 outBufSize;
- INT32 BytesFilled;
- UINT16 framerate; //播放器的播放帧率(一个FRAMES_FILLED相当于播放器的一帧)
- AMR_Encode_State *encInterface;
- INT32 usedBytes;
- INT32 start_tag;
- } AMREnc_Stru;
- static AMREnc_Stru* AMR_EncStru_Init()
- {
- AMREnc_Stru *AMREncStru;
- AMREncStru = ( AMREnc_Stru * )new AMREnc_Stru;
- if ( AMREncStru == NULL )
- {
- return NULL;
- }
- AMREncStru->bytesLeft = 0;
- AMREncStru->outOfData = 0;
- AMREncStru->eofReached = 0;
- AMREncStru->nFrames = 0;
- AMREncStru->readBuf = NULL;
- AMREncStru->readPtr = NULL;
- AMREncStru->outBuf = NULL;
- AMREncStru->outBufSize = 0;
- AMREncStru->framerate = 50;
- AMREncStru->usedBytes = 0;
- AMREncStru->BytesFilled = 0;
- AMREncStru->start_tag = 1;
- AMREncStru->encInterface = (AMR_Encode_State*)Encoder_Interface_init(0);
- if ( AMREncStru->encInterface == NULL )
- {
- delete AMREncStru;
- AMREncStru = NULL;
- return NULL;
- }
- // AMREncStru->outBufSize = SPCH_BUF_SIZE * OUTFRAMES;
- // AMREncStru->outBuf = (UINT8 *)malloc( AMREncStru->outBufSize );
- // if ( AMREncStru->outBuf == NULL )
- // {
- // Encoder_Interface_exit(AMREncStru->encInterface);
- // AMREncStru->encInterface = NULL;
- // delete AMREncStru;
- // AMREncStru = NULL;
- // }
- return AMREncStru;
- }
- static void AMR_EncStru_Clos(AMREnc_Stru* AMREncStru)
- {
- if ( AMREncStru->encInterface )
- {
- Encoder_Interface_exit(AMREncStru->encInterface);
- }
- if ( AMREncStru->outBuf )
- {
- free(AMREncStru->outBuf);
- AMREncStru->outBuf = NULL;
- }
- delete AMREncStru;
- AMREncStru = NULL;
- return;
- }
- static INT32 AMR_EncStru_Enc( AMREnc_Stru *AMREncStru )
- {
- INT32 nRead;
- INT32 BytesCounter;
- // enum Mode dec_mode;
- AMR_Encode_State *encInterface = NULL;
- UINT8 *inbuf = NULL;
- UINT8 outbuf[FRAMMAXSIZ];
- if ( AMREncStru == NULL )
- {
- return -1;
- }
- nRead = SPCH_BUF_SIZE;
- AMREncStru->usedBytes = 0;
- encInterface = AMREncStru->encInterface;
- do
- {
- if ( AMREncStru->bytesLeft < 0 )
- {
- return -2;
- }
- else if ( 0 == AMREncStru->bytesLeft )
- {
- AMREncStru->outOfData = 1;
- memset(AMREncStru->outBuf + AMREncStru->BytesFilled, 0, AMREncStru->outBufSize - AMREncStru->BytesFilled);
- break;
- }
- inbuf = AMREncStru->readPtr;
- if ( nRead > AMREncStru->bytesLeft )
- {
- AMREncStru->outOfData = 1;
- memset(AMREncStru->outBuf + AMREncStru->BytesFilled, 0, AMREncStru->outBufSize - AMREncStru->BytesFilled);
- break;
- }
- BytesCounter = Encoder_Interface_Encode(encInterface, ENCMODE, (INT16*)inbuf, outbuf, 0);
- AMREncStru->readPtr += nRead;
- AMREncStru->bytesLeft -= nRead;
- memcpy(AMREncStru->outBuf + AMREncStru->BytesFilled, outbuf, BytesCounter);
- AMREncStru->nFrames++;
- AMREncStru->BytesFilled += BytesCounter;
- // if ( AMREncStru->BytesFilled == OUTFRAMES * SPCH_BUF_SIZE )
- // {
- // AMREncStru->usedBytes = AMREncStru->readPtr - AMREncStru->readBuf;
- // return 1;
- // }
- } while( !AMREncStru->outOfData );
- AMREncStru->usedBytes = AMREncStru->readPtr - AMREncStru->readBuf;
- return 2;
- }
- static INT32 amr_enc_init(CodecContext *Data)
- {
- AMREnc_Stru* AMREncStru;
- memset(Data, 0, sizeof( CodecContext ));
- Data->bits_per_sample = 16;
- Data->channels = 1;
- Data->codec_id = CODEC_ID_AMR;
- Data->codec_type = CODEC_TYPE_AUDIO;
- Data->sample_fmt = 0x01;
- Data->sample_rate = 8000;
- Data->magic_code = (('!'<<24) | ('R'<<16) | ('M'<<8) | 'A');
- AMREncStru = AMR_EncStru_Init();
- if ( NULL == AMREncStru )
- {
- return -1;
- }
- Data->priv_data = AMREncStru;
- return 0;
- }
- static INT32 amr_enc_close(CodecContext *Data)
- {
- AMREnc_Stru* AMREncStru;
- AMREncStru = (AMREnc_Stru*)Data->priv_data;
- if ( AMREncStru )
- {
- AMR_EncStru_Clos(AMREncStru);
- }
- Data->priv_data = NULL;
- return 0;
- }
- static INT32 amr_enc_encode(CodecContext *Data, void** outdata, INT32* outdata_size, UINT8* inbuf, INT32* inbuf_size)
- {
- INT32 iJudg;
- AMREnc_Stru* AMREncStru;
- UINT8* pTemp;
- if (NULL != *outdata)
- {
- free(*outdata);
- *outdata = NULL;
- }
- AMREncStru = ( AMREnc_Stru* )Data->priv_data;
- if ( NULL == AMREncStru )
- {
- return -1;
- }
- pTemp = (UINT8 *)malloc(*inbuf_size + AMREncStru->bytesLeft);
- if ( NULL == pTemp )
- {
- return -1;
- }
- memcpy(pTemp, AMREncStru->readPtr, AMREncStru->bytesLeft);
- memcpy(pTemp + AMREncStru->bytesLeft, inbuf, *inbuf_size);
- AMREncStru->readBuf = pTemp;
- AMREncStru->readPtr = AMREncStru->readBuf;
- AMREncStru->bytesLeft += *inbuf_size;
- AMREncStru->BytesFilled = 0;
- AMREncStru->outOfData = 0;
- AMREncStru->outBufSize = ((AMREncStru->bytesLeft) / SPCH_BUF_SIZE + 1) * FRAMMAXSIZ; //根据inbufsize决定最大outbufsize
- AMREncStru->outBuf = (UINT8 *)malloc( AMREncStru->outBufSize );
- if ( AMREncStru->outBuf == NULL )
- {
- return -1;
- }
- iJudg = AMR_EncStru_Enc(AMREncStru);
- if ( iJudg < 0 )
- {
- return -2;
- }
- *outdata = AMREncStru->outBuf;
- // memcpy(outdata, AMREncStru->outBuf, AMREncStru->BytesFilled);
- *outdata_size = AMREncStru->BytesFilled;
- return AMREncStru->usedBytes;
- }
- static CodecInterfaceType amr_codec =
- {
- ( CHAR* )"AMR",
- CODEC_TYPE_AUDIO,
- CODEC_ID_AMR,
- sizeof( AMREnc_Stru ),
- amr_enc_init,
- amr_enc_close,
- NULL,
- amr_enc_encode,
- NULL,
- NULL,
- };
- CodecInterfaceType* Get_AMR_EncInterface(void)
- {
- CodecInterfaceType* pInterface;
- pInterface = (CodecInterfaceType*)malloc(sizeof(CodecInterfaceType));
- if (NULL == pInterface)
- {
- return NULL;
- }
- memcpy(pInterface, &amr_codec, sizeof(CodecInterfaceType));
- return pInterface;
- }