Exemplo n.º 1
0
void Decoder_Interface_Decode(void* state, const unsigned char* in, short* out, int bfi) {
	unsigned char type = (in[0] >> 3) & 0x0f;
	in++;
	if (bfi) {
		type = AMR_NO_DATA;
	}
	AMRDecode(state, (enum Frame_Type_3GPP) type, (UWord8*) in, out, MIME_IETF);
}
OSCL_EXPORT_REF int32 CDecoder_AMR_NB::ExecuteL(tPVAmrDecoderExternal * pExt)
{


    if (pExt->input_format == WMF)
        pExt->input_format = MIME_IETF;

    return AMRDecode(iDecState,
                     (enum Frame_Type_3GPP)pExt->mode,
                     (uint8*) pExt->pInputBuffer,
                     (int16*) pExt->pOutputBuffer,
                     pExt->input_format);

}
Exemplo n.º 3
0
int CDecoderAudio::Decode(JNIEnv* env, jbyteArray iData, int iSizeData){
	jbyte *pbydatain  = env->GetByteArrayElements(iData, 0);

	unsigned char type = (pbydatain[0] >> 3) & 0x0f;
	pbydatain++;
	
	AMRDecode(_ptrAmr, (enum Frame_Type_3GPP) type, (UWord8*) pbydatain, _outbuffer, MIME_IETF);

	// Convert to little endian
	uint8_t* ptr = (uint8_t*)m_pbyDecodedAudio;
	for (int i = 0; i < 160; i++) {
		*ptr++ = (_outbuffer[i] >> 0) & 0xff;
		*ptr++ = (_outbuffer[i] >> 8) & 0xff;
	}

	env->ReleaseByteArrayElements(iData, pbydatain, 0);
	return 1;
}
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;
}