Example #1
0
int CDecoderAudio::Init(JNIEnv* env){
	m_byDecodedAudio = env->NewByteArray(BUFFER_DECODE_SIZE_AUDIO);
	m_pbyDecodedAudio = env->GetByteArrayElements(m_byDecodedAudio, 0);
	_ptrAmr = NULL;
	GSMInitDecode(&_ptrAmr, (int8*)"Decoder");
	m_iSizeDecoded = 320;
	__android_log_write(ANDROID_LOG_DEBUG, "CDecoderAudio::Init", "Initialized");		
	return 1;
}
status_t SoftAMR::initDecoder() {
    if (mMode == MODE_NARROW) {
        Word16 err = GSMInitDecode(&mState, (Word8 *)"AMRNBDecoder");

        if (err != 0) {
            return UNKNOWN_ERROR;
        }
    } else {
        int32_t memReq = pvDecoder_AmrWbMemRequirements();
        mDecoderBuf = malloc(memReq);

        pvDecoder_AmrWb_Init(&mState, mDecoderBuf, &mDecoderCookie);
    }

    return OK;
}
/*
-----------------------------------------------------------------------------

    CDecoder_AMR_NB

    StartL

    Start decoder object. Initialize codec status.

    Parameters:     none

    Return Values:  status

-----------------------------------------------------------------------------
*/
OSCL_EXPORT_REF int32 CDecoder_AMR_NB::StartL(tPVAmrDecoderExternal * pExt,
        bool aAllocateInputBuffer,
        bool aAllocateOutputBuffer)
{

    if (aAllocateInputBuffer)
    {
        iInputBuf = OSCL_ARRAY_NEW(int16, MAX_NUM_PACKED_INPUT_BYTES);
        if (iInputBuf == NULL)
        {
            return KCAI_CODEC_INIT_FAILURE;
        }
    }
    else
    {
        iInputBuf = NULL;
    }
    pExt->pInputBuffer = (uint8 *)iInputBuf;

    if (aAllocateOutputBuffer)
    {
        iOutputBuf = OSCL_ARRAY_NEW(int16, L_FRAME);

        if (iOutputBuf == NULL)
        {
            return KCAI_CODEC_INIT_FAILURE;
        }
    }
    else
    {
        iOutputBuf = NULL;
    }
    pExt->pOutputBuffer = iOutputBuf;

    pExt->samplingRate = 8000;
    pExt->desiredChannels = 1;

    pExt->reset_flag = 0;
    pExt->reset_flag_old = 1;
    pExt->mode_old = 0;

    return GSMInitDecode(&iDecState, (int8*)"Decoder");
}
int main(int argc, char *argv[]) {

    if(argc != 3) {
        fprintf(stderr, "Usage %s <input file> <output file>\n", argv[0]);
        return 1;
    }

    // Open the input file
    FILE* fpInput = fopen(argv[1], "rb");
    if (!fpInput) {
        fprintf(stderr, "Could not open %s\n", argv[1]);
        return 1;
    }

    // Validate the input AMR file
    char header[kFileHeaderSize];
    int bytesRead = fread(header, 1, kFileHeaderSize, fpInput);
    if (bytesRead != kFileHeaderSize || memcmp(header, "#!AMR\n", kFileHeaderSize)) {
        fprintf(stderr, "Invalid AMR-NB file\n");
        return 1;
    }

    // Open the output file
    SF_INFO sfInfo;
    memset(&sfInfo, 0, sizeof(SF_INFO));
    sfInfo.channels = kChannels;
    sfInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
    sfInfo.samplerate = kSampleRate;
    SNDFILE *handle = sf_open(argv[2], SFM_WRITE, &sfInfo);
    if(!handle){
        fprintf(stderr, "Could not create %s\n", argv[2]);
        return 1;
    }

    // Create AMR-NB decoder instance
    void* amrHandle;
    int err = GSMInitDecode(&amrHandle, (Word8*)"AMRNBDecoder");
    if(err != 0){
        fprintf(stderr, "Error creating AMR-NB decoder instance\n");
        return 1;
    }

    //Allocate input buffer
    void *inputBuf = malloc(kInputBufferSize);
    assert(inputBuf != NULL);

    //Allocate output buffer
    void *outputBuf = malloc(kOutputBufferSize);
    assert(outputBuf != NULL);


    // Decode loop
    uint32_t retVal = 0;
    while (1) {
        // Read mode
        uint8_t mode;
        bytesRead = fread(&mode, 1, 1, fpInput);
        if (bytesRead != 1) break;

        // Find frame type
        Frame_Type_3GPP frameType = (Frame_Type_3GPP)((mode >> 3) & 0x0f);
        if (frameType >= AMR_SID){
            fprintf(stderr, "Frame type %d not supported\n",frameType);
            retVal = 1;
            break;
        }

        // Find frame type
        int32_t frameSize = kFrameSizes[frameType];
        bytesRead = fread(inputBuf, 1, frameSize, fpInput);
        if (bytesRead != frameSize) break;

        //Decode frame
        int32_t decodeStatus;
        decodeStatus = AMRDecode(amrHandle, frameType, (uint8_t*)inputBuf,
                                 (int16_t*)outputBuf, MIME_IETF);
        if(decodeStatus == -1) {
            fprintf(stderr, "Decoder encountered error\n");
            retVal = 1;
            break;
        }

        //Write output to wav
        sf_writef_short(handle, (int16_t*)outputBuf, kSamplesPerFrame);

    }

    // Close input and output file
    fclose(fpInput);
    sf_close(handle);

    //Free allocated memory
    free(inputBuf);
    free(outputBuf);

    // Close decoder instance
    GSMDecodeFrameExit(&amrHandle);

    return retVal;
}
Example #5
0
void* Decoder_Interface_init(void) {
	void* ptr = NULL;
	GSMInitDecode(&ptr, (int8*)"Decoder");
	return ptr;
}