static int amr_codec_decoder(const struct PluginCodec_Definition * codec, void * context, const void * fromPtr, unsigned * fromLen, void * toPtr, unsigned * toLen, unsigned int * flag) { unsigned char * packet; if (*toLen < L_FRAME*sizeof(short)) return FALSE; if (fromLen == NULL || *fromLen == 0) { unsigned char buffer[32]; buffer[0] = (AMR_BITRATE_DTX << 3)|4; Decoder_Interface_Decode(context, buffer, (short *)toPtr, 0); // Handle missing data return TRUE; } packet = (unsigned char *)fromPtr; Decoder_Interface_Decode(context, packet+1, (short *)toPtr, 0); // Skip over CMR *fromLen = block_size[packet[1] >> 3] + 1; // Actual bytes consumed *toLen = L_FRAME*sizeof(short); return TRUE; }
int main(void) { FILE *fp; FILE *decoder_fp; int read_num = 0; int write_num = 0; char buffer[1024] = {0}; char decoder_buffer[1024] = {0}; int* fAudioCodec, *fAudioDecodec; unsigned char fTo[1024] = {0}; fAudioCodec = (int*)Encoder_Interface_init(0); fAudioDecodec = (int*)Decoder_Interface_init(); fp = fopen(AUDIO_FILE, "rb"); decoder_fp = fopen("temp.txt", "wb+"); if (fp < 0 || decoder_fp < 0) { } while((read_num = fread(buffer, 1, 320, fp))) { Encoder_Interface_Encode(fAudioCodec, MR475, (const short int*)buffer, fTo, 0); Decoder_Interface_Decode(fAudioDecodec, (const unsigned char *)fTo, (short int *)decoder_buffer, 0); fseek(decoder_fp, write_num, SEEK_SET); write_num += read_num; fwrite(decoder_buffer, 1, 320, decoder_fp); if(read_num < 320) break; } fclose(fp); fclose(decoder_fp); }
int main(int argc, char *argv[]) { FILE* in; char header[6]; int n; void *wav, *amr; if (argc < 3) { fprintf(stderr, "%s in.amr out.wav\n", argv[0]); return 1; } in = fopen(argv[1], "rb"); if (!in) { perror(argv[1]); return 1; } n = fread(header, 1, 6, in); if (n != 6 || memcmp(header, "#!AMR\n", 6)) { fprintf(stderr, "Bad header\n"); return 1; } wav = wav_write_open(argv[2], 8000, 16, 1); if (!wav) { fprintf(stderr, "Unable to open %s\n", argv[2]); return 1; } amr = Decoder_Interface_init(); while (1) { uint8_t buffer[500], littleendian[320], *ptr; int size, i; int16_t outbuffer[160]; /* Read the mode byte */ n = fread(buffer, 1, 1, in); if (n <= 0) break; /* Find the packet size */ size = sizes[(buffer[0] >> 3) & 0x0f]; n = fread(buffer + 1, 1, size, in); if (n != size) break; /* Decode the packet */ Decoder_Interface_Decode(amr, buffer, outbuffer, 0); /* Convert to little endian and write to wav */ ptr = littleendian; for (i = 0; i < 160; i++) { *ptr++ = (outbuffer[i] >> 0) & 0xff; *ptr++ = (outbuffer[i] >> 8) & 0xff; } wav_write_data(wav, littleendian, 320); } fclose(in); Decoder_Interface_exit(amr); wav_write_close(wav); return 0; }
/* * Decode frame. */ static pj_status_t amr_codec_decode( pjmedia_codec *codec, const struct pjmedia_frame *input, unsigned output_buf_len, struct pjmedia_frame *output) { struct amr_data *amr_data = (struct amr_data*) codec->codec_data; pjmedia_frame input_; pjmedia_codec_amr_bit_info *info; unsigned out_size; /* AMR decoding buffer: AMR max frame size + 1 byte header. */ unsigned char bitstream[61]; pj_assert(amr_data != NULL); PJ_ASSERT_RETURN(input && output, PJ_EINVAL); out_size = amr_data->clock_rate * FRAME_LENGTH_MS / 1000 * 2; if (output_buf_len < out_size) return PJMEDIA_CODEC_EPCMTOOSHORT; input_.buf = &bitstream[1]; /* AMR max frame size */ input_.size = (amr_data->dec_setting.amr_nb? 31: 60); pjmedia_codec_amr_predecode(input, &amr_data->dec_setting, &input_); info = (pjmedia_codec_amr_bit_info*)&input_.bit_info; /* VA AMR decoder requires frame info in the first byte. */ bitstream[0] = (info->frame_type << 3) | (info->good_quality << 2); TRACE_((THIS_FILE, "AMR decode(): mode=%d, ft=%d, size=%d", info->mode, info->frame_type, input_.size)); /* Decode */ if (amr_data->dec_setting.amr_nb) { #ifdef USE_AMRNB Decoder_Interface_Decode(amr_data->decoder, bitstream, (pj_int16_t*)output->buf, 0); #endif } else { #ifdef USE_AMRWB D_IF_decode(amr_data->decoder, bitstream, (pj_int16_t*)output->buf, 0); #endif } output->size = out_size; output->type = PJMEDIA_FRAME_TYPE_AUDIO; output->timestamp = input->timestamp; #if USE_PJMEDIA_PLC if (amr_data->plc_enabled) pjmedia_plc_save(amr_data->plc, (pj_int16_t*)output->buf); #endif return PJ_SUCCESS; }
static void dec_process(MSFilter *f) { static const int nsamples = 160; mblk_t *im, *om; uint8_t *tocs; int toclen; uint8_t tmp[32]; while ((im = ms_queue_get(f->inputs[0])) != NULL) { int sz = msgdsize(im); int i; if (sz < 2) { freemsg(im); continue; } /*skip payload header, ignore CMR */ im->b_rptr++; /*see the number of TOCs :*/ tocs = im->b_rptr; toclen = toc_list_check(tocs, sz); if (toclen == -1) { ms_warning("Bad AMR toc list"); freemsg(im); continue; } im->b_rptr += toclen; /*iterate through frames, following the toc list*/ for (i = 0; i < toclen; ++i) { int index = toc_get_index(tocs[i]); int framesz; if (index >= 9) { ms_warning("Bad amr toc, index=%i", index); break; } framesz = amr_frame_sizes[index]; if (im->b_rptr + framesz > im->b_wptr) { ms_warning("Truncated amr frame"); break; } tmp[0] = tocs[i]; memcpy(&tmp[1], im->b_rptr, framesz); om = allocb(nsamples * 2, 0); Decoder_Interface_Decode(f->data, tmp, (short*) om->b_wptr, 0); om->b_wptr += nsamples * 2; im->b_rptr += framesz; ms_queue_put(f->outputs[0], om); } freemsg(im); } }
JNIEXPORT void JNICALL Java_ac_robinson_mov_AMRtoPCMConverter_AmrDecoderDecode(JNIEnv* env, jobject obj, jint* nativePointer, jbyteArray in, jshortArray out, jint bfi) { jsize inLen = env->GetArrayLength(in); jbyte inBuf[inLen]; env->GetByteArrayRegion(in, 0, inLen, inBuf); jsize outLen = env->GetArrayLength(out); short outBuf[outLen]; Decoder_Interface_Decode(nativePointer, (const unsigned char*) inBuf, (short*) outBuf, bfi); // env->ReleaseByteArrayElements(in, inBuf, JNI_ABORT); // no need - GetByteArrayRegion handles this env->SetShortArrayRegion(out, 0, outLen, outBuf); }
int decode_amr(const char* infile, const char* outfile) { FILE* in = fopen(infile, "rb"); if (!in) { return 1; } char header[6]; ssize_t n = fread(header, 1, 6, in); if (n != 6 || memcmp(header, "#!AMR\n", 6)) { fprintf(stderr, "Bad header\n"); return 1; } void* wav = wav_write_open(outfile, 8000, 16, 1); void* amr = Decoder_Interface_init(); while (1) { uint8_t buffer[500]; /* Read the mode byte */ n = fread(buffer, 1, 1, in); if (n <= 0) break; /* Find the packet size */ int size = sizes[(buffer[0] >> 3) & 0x0f]; if (size <= 0) continue; n = fread(buffer + 1, 1, size, in); if (n != size) break; /* Decode the packet */ int16_t outbuffer[160]; Decoder_Interface_Decode(amr, buffer, outbuffer, 0); /* Convert to little endian and write to wav */ uint8_t littleendian[320]; uint8_t* ptr = littleendian; for (int i = 0; i < 160; i++) { *ptr++ = (outbuffer[i] >> 0) & 0xff; *ptr++ = (outbuffer[i] >> 8) & 0xff; } wav_write_data(wav, littleendian, 320); } fclose(in); Decoder_Interface_exit(amr); wav_write_close(wav); return 0; }
static int decode_nb(struct audec_state *st, int16_t *sampv, size_t *sampc, const uint8_t *buf, size_t len) { if (!st || !sampv || !sampc || !buf) return EINVAL; if (len > NB_SERIAL_MAX) return EPROTO; if (*sampc < L_FRAME16k) return ENOMEM; Decoder_Interface_Decode(st->dec, &buf[1], sampv, 0); *sampc = FRAMESIZE_NB; return 0; }
static switch_status_t switch_amr_decode(switch_codec_t *codec, switch_codec_t *other_codec, void *encoded_data, uint32_t encoded_data_len, uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate, unsigned int *flag) { #ifdef AMR_PASSTHROUGH switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "This codec is only usable in passthrough mode!\n"); return SWITCH_STATUS_FALSE; #else struct amr_context *context = codec->private_info; if (!context) { return SWITCH_STATUS_FALSE; } Decoder_Interface_Decode(context->decoder_state, (unsigned char *) encoded_data, (int16_t *) decoded_data, 0); *decoded_data_len = codec->implementation->decoded_bytes_per_packet; return SWITCH_STATUS_SUCCESS; #endif }
unsigned AMRDecoder::DecodeFrame( const void *inputBuffer, // input buffer with AMR frame data (max 32 bytes) void *outputBuffer // output buffer (160 16-bit words - 320 bytes) ) { if (inputBuffer == NULL || outputBuffer == NULL) return 0; memset(outputBuffer, 0, 160 * 2); #ifdef IF2 unsigned int frameType = *reinterpret_cast<const unsigned char*>(inputBuffer) & 0x000F; #else unsigned int frameType = (*reinterpret_cast<const unsigned char*>(inputBuffer) >> 3) & 0x000F; #endif Decoder_Interface_Decode(codecState, reinterpret_cast<unsigned char*>(const_cast<void*>(inputBuffer)), reinterpret_cast<short*>(outputBuffer), 0); return frameSize[frameType]; }
static int amr_codec_decoder(const struct PluginCodec_Definition * codec, void * context, const void * from, unsigned * fromLen, void * to, unsigned * toLen, unsigned int * flag) { // unsigned int mode; if( *fromLen < 1 ) return 0; /* // get the AMR mode from the first nibble of the frame mode = *(char *)from & 0xF; // check that the input is long enough for the decoder if( *fromLen != bytes_per_frame[mode] ) { fprintf(stderr,"AMR codec: packet size %u doesn't match expected %u for mode %u\n", *fromLen,bytes_per_frame[mode], mode ); return 0; } */ Decoder_Interface_Decode( context, (void *)from, (short *)to, 0 ); #if 0 fprintf(stderr,"Decoded AMR frame ["); hexprint(from,*fromLen); fprintf(stderr,"]\nResult: ["); hexprint(to,40); fprintf(stderr,"...]\n"); #endif // return the number of decoded bytes to the caller *toLen = 160*sizeof(short); return 1; }