⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pmp3enc.c

📁 瑞星微公司RK27XX系列芯片的SDK开发包
💻 C
字号:
//#include "stdio.h"
#include "../include/audio_globals.h"

#ifdef MP3_ENC_INCLUDE

#include "MP3Enc.h"
#include "..\common\mTrackControl.h"
#include "..\common\mRECORD.H"

#include "pMP3Enc_lib.h"

#include "../include/file_access.h"

FT0 *InitMP3Enc = (FT0 *) RKInitMP3Encode;
FT1 *MP3Enc  = RKMP3Encode;
FT2 *EnCDSync = RKEnableCDSync;


extern  unsigned char ucBufFull;
extern  unsigned char gStartRec;

#ifdef PCM_ENC_INCLUDE
extern short  RecordWriteBuf[];
#endif

#if  defined(MP3_ENC_INCLUDE)||defined(ADPCM_ENC_INCLUDE)
extern char   RecordWriteBuf[];
#endif
extern unsigned long RecordWriteIndex;
extern unsigned char SendFirstBufSign;
extern unsigned char SendSecondBufSign;


int MP3ENC_SaveData(void *SourceBuffer, unsigned int uiSaveDataLength)
{
    unsigned char *ucSourceBuffer = (unsigned char*)SourceBuffer;
    //fwrite((void *)SourceBuffer,1,uiSaveDataLength,(MY_FILE*)TRACK_GetFileHandleOfRecord());
    RKFIO_FWrite((MY_FILE*)TRACK_GetFileHandleOfRecord(), (void *)SourceBuffer, uiSaveDataLength); //MPEG4_INDEX_BUFFERSIZE

    return 1;
}

//****************************************************************************
//
// The codec plug-in entry point for the MP3 decoder.
//
//****************************************************************************
unsigned long
MP3EncFunction(unsigned long ulSubFn, unsigned long ulParam1, unsigned long ulParam2,
               unsigned long ulParam3, unsigned long ulParam4)
{
    switch (ulSubFn)
    {


            // Return the current position (in milliseconds) within the file.
        case SUBFN_CODEC_GETTIME:
            {
                unsigned long *pulTime;
                tMP3ENC *pMP3ENC;

                // The first parameter is a pointer to the MP3 persistent data.
                pMP3ENC = (tMP3ENC *)ulParam1;

                // The second parameter is a pointer for the number of seconds.
                pulTime = (unsigned long *)ulParam2;

                // Determine the time based on the sample rate.
                *pulTime = ((pMP3ENC->ulTimePos / pMP3ENC->usSampleRate) * 1000) +
                           (((pMP3ENC->ulTimePos % pMP3ENC->usSampleRate) * 1000) /
                            pMP3ENC->usSampleRate);

                // *pulTime = (pMP3ENC->storedata/pMP3ENC->ulBitRate)<<3;
                // Success.
                return(1);
            }

            // Return the sample rate at which this file is encoded.
        case SUBFN_CODEC_GETSAMPLERATE:
            {
                unsigned long *pulSampleRate;
                tMP3ENC *pMP3ENC;

                // The first parameter is a pointer to the MP3 persistent data.
                pMP3ENC = (tMP3ENC *)ulParam1;

                pulSampleRate = (unsigned long *)ulParam2;

                // Return the sample rate of the file.
                *pulSampleRate = pMP3ENC->usSampleRate;

                // Success.
                return(1);

            }

            // Return the number of channels in the file.
        case SUBFN_CODEC_GETCHANNELS:
            {
                unsigned long *pulChannels;
                tMP3ENC *pMP3ENC;

                // The first parameter is a pointer to the MP3 persistent data.
                pMP3ENC = (tMP3ENC *)ulParam1;

                pulChannels = (unsigned long *)ulParam2;

                // Return the number of channels in the file.
                *pulChannels = pMP3ENC->ucChannels;

                // Success.
                return(1);
            }

            // Return the bitrate at which this file is encoded.
        case SUBFN_CODEC_GETBITRATE:
            {
                unsigned long *pulBitRate;
                tMP3ENC *pMP3ENC;

                // The first parameter is a pointer to the MP3 persistent data.
                pMP3ENC = (tMP3ENC *)ulParam1;

                pulBitRate = (unsigned long *)ulParam2;

                // Return the number of channels in the file.
                *pulBitRate = pMP3ENC->ulBitRate;

                // Success.
                return(1);

            }

            // Return the length (in milliseconds) of the file.
        case SUBFN_CODEC_GETLENGTH:
            {
                unsigned long *pulLength;
                tMP3ENC *pMP3ENC;

                // The first parameter is a pointer to the MP3 persistent data.
                pMP3ENC = (tMP3ENC *)ulParam1;

                pulLength = (unsigned long *)ulParam2;

                // Return the length of the file.
                *pulLength = pMP3ENC->ulTimeLength;

                // Success.
                return(1);
            }

            // Set the output buffer for the decoder.
        case SUBFN_CODEC_SETBUFFER:
            {
                tMP3ENC *pMP3ENC;

                // The first parameter is a pointer to the MP3 persistent state.
                pMP3ENC = (tMP3ENC *)ulParam1;

                // The second parameter is a pointer to the output buffer.
                pMP3ENC->pOutput = (BufferState *)ulParam2;

                // Provide the output buffer with our data buffers.
                if (pMP3ENC->ucChannels == 2)
                {
                    //BufferSetBuffer(pMP3ENC->pOutput, pMP3ENC->psLeft, pMP3ENC->psRight,
                    BufferSetBuffer(pMP3ENC->pOutput, LEFT_PCM_BUFFER, RIGHT_PCM_BUFFER, PCM_BUFFER_SIZE);
                }
                else
                {
                    //BufferSetBuffer(pMP3ENC->pOutput, pMP3ENC->psLeft, pMP3ENC->psLeft,
                    BufferSetBuffer(pMP3ENC->pOutput, LEFT_PCM_BUFFER, LEFT_PCM_BUFFER, PCM_BUFFER_SIZE);
                }

                ucBufFull = 0;

                // Success.
                return(1);
            }

        case SUBFN_CODEC_GETCAPTUREBUFFER:
            {
                tMP3ENC *pMP3ENC;
                int lLen;

                pMP3ENC = (tMP3ENC *) ulParam1;

                *(unsigned long *)ulParam2 = (unsigned long)pMP3ENC->psLeft;//pPCM->FrameOutputLeft;
                *(unsigned long *)ulParam3 = (unsigned long)pMP3ENC->psRight;//pPCM->FrameOutputRight;

                if (REC_GetMpegMode() == 1) lLen = 1152;
                else    lLen = 576;
                *(unsigned long *)ulParam4 = (unsigned long)lLen;

                //success
                return(1);
            }

            // Prepare the codec to encode a file.
        case SUBFN_CODEC_OPEN_ENC:
            {
                tMP3ENC *pMP3ENC;
                unsigned long *buf;
                unsigned long test;
                unsigned long bitrate;

                gStartRec = 0;

                // The first parameter is a pointer to the MP3 persistent state.
                pMP3ENC = (tMP3ENC *)ulParam1;

                pMP3ENC->ulTimePos = 0;

                pMP3ENC->ulBitRate = REC_GetgBitrate() * 1000;


                pMP3ENC->pOutput = 0;

                pMP3ENC->pMPEGInstance = (tMP3ENCInstance *)(((unsigned long)pMP3ENC + sizeof(tMP3ENC) + 2047) & 0xfffff800);

                buf = (unsigned long*) pMP3ENC->pMPEGInstance;


                test = (((unsigned long)pMP3ENC->pMPEGInstance + sizeof(tMP3ENCInstance)) & 0xFFFF);


                gBitStreamBuff = (unsigned char *) pENCODED_DATA;

                for (test = 0; test < ENCODED_DATA_SIZE; test++)
                    gBitStreamBuff[test] = 0;

                gBuffIdx = 0;

                bitrate = REC_GetgBitrate();

                if (bitrate == 256)
                {
                    bitrate = 0;
                    gMPEGEnc.version = 1; // '1' MPEG1, '0'-MPEG2
                    gMPEGEnc.sampling_frequency = 0; // '0' - 44.1 or 22.05 ,  '1' - 48 or 24 ,  '2' - 32 or 16
                    gMPEGEnc.ms = 1; // '0' - stereo '1' - Joint Stereo
                    gMPEGEnc.stereo = 2; // '1' - mono '2' - stereo
                    gMPEGEnc.bitrate_index = 13; // refer to MP3 Spec.
                    gMPEGEnc.Bitrate = 256; // refer to MP3 Spec. , Unit : bps/1000.
                    gMPEGEnc.SF_freq = 44100; // sampling rate
                }


                if (InitMP3Enc((unsigned long *)pMP3ENC->pMPEGInstance, (short *)LEFT_PCM_BUFFER, (short *)RIGHT_PCM_BUFFER, (unsigned long)bitrate) == 0)
                    return (0);


                if (bitrate == 256)
                {
                    gMPEGEnc.clip_enable = 1 ; // ‘1’ - enable  ‘0’ - disable
                    gMPEGEnc.cutoff_freq = 43; //limit to 4.3khz
                }

                //pMP3->usSampleRate = 44100; //gMPEGEnc.sampling_frequency;
                pMP3ENC->usSampleRate = REC_GetSamplingFreq();

                //pMP3->ucChannels = 2;//gMPEGEnc.stereo;
                pMP3ENC->ucChannels = REC_GetChannel();
                pMP3ENC->ulBitRate = REC_GetgBitrate();   // add by huangzf
                // pMP3ENC->storedata = 0;

                return(1);
            }

            // Encode a frame of data.
        case SUBFN_CODEC_ENCODE:
            {
                tMP3ENC *pMP3ENC;
                short *psLeft, *psRight;
                long  lLength;
                long  lLen;
                unsigned uCPSR, uBSize;
                int i;
                char *encodetemp;

                pMP3ENC = (tMP3ENC *) ulParam1;

                if (!gStartRec)
                {

                    pMP3ENC->pOutput->lReadPtr = 0;
                    pMP3ENC->pOutput->lWritePtr = 0;
                    gStartRec = 1;
                    ucBufFull = 0;
                }

                if (REC_GetMpegMode() == 1) lLen = 1152;
                else    lLen = 576;


                psLeft = pMP3ENC->psLeft;
                psRight = pMP3ENC->psRight;


                MP3Enc(psLeft, psRight);



                if (gCDSync.detect == 0)
                    //if(REC_Flag)
                {
                    //if(gBuffIdx >= ENCODED_DATA_WRITE_SIZE)
                    //{

                    /*MP3ENC_SaveData(pENCODED_DATA,ENCODED_DATA_WRITE_SIZE);
                    gBuffIdx -= ENCODED_DATA_WRITE_SIZE;
                        for(i=0;i<gBuffIdx;i++)
                                pENCODED_DATA[i]=pENCODED_DATA[i+ENCODED_DATA_WRITE_SIZE];
                    */
                    encodetemp = pENCODED_DATA;
                    if ((2*REC_WRITE_BUF_LEN - RecordWriteIndex) <= (gBuffIdx))
                    {
                        memcpy(&RecordWriteBuf[RecordWriteIndex], &encodetemp[0], (2*REC_WRITE_BUF_LEN - RecordWriteIndex));
                        memcpy(&RecordWriteBuf[0], &encodetemp[2*REC_WRITE_BUF_LEN - RecordWriteIndex], (gBuffIdx + RecordWriteIndex - 2*REC_WRITE_BUF_LEN));
                        RecordWriteIndex = gBuffIdx + RecordWriteIndex - 2 * REC_WRITE_BUF_LEN;
                        SendSecondBufSign = 1;
                    }
                    else
                    {
                        memcpy(&RecordWriteBuf[RecordWriteIndex], &encodetemp[0], (gBuffIdx));
                        RecordWriteIndex += gBuffIdx;
                    }
                    if ((RecordWriteIndex >= REC_WRITE_BUF_LEN) && (RecordWriteIndex < (REC_WRITE_BUF_LEN + gBuffIdx)))
                    {
                        SendFirstBufSign = 1;
                    }
                    //pMP3ENC->storedata += gBuffIdx;
                    gBuffIdx = 0;



                    pMP3ENC->ulTimePos += lLen;
                }
                else
                {
                    gBuffIdx = 0;

                }

                return(1);

            }

            // Cleanup after the codec.
        case SUBFN_CODEC_CLOSE:
            {
#if 0
                tMP3ENC *pMP3ENC;

                // If we are being asked to encode an MP3 file, then return a
                // failure since we can only decode MP3.
                if (ulParam2 & CODEC_OPEN_ENCODE)
                {
                    pMP3ENC = (tMP3ENC *)ulParam1;
                    FS_fwrite(pENCODED_DATA, 0, 1, fd_test);
                }
#endif
                // There's nothing to do, so return success.
                gStartRec = 0;
                return(1);
            }

        default:
            {
                // Return a failure.
                return(0);
            }
    }
}

#endif //MP3_ENC_INCLUDE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -