static int Process(amrwb* p, const packet* Packet, const flowstate* State) { int Size; if (Packet) { if (Packet->RefTime >= 0) p->Codec.Packet.RefTime = Packet->RefTime; // add new packet to buffer BufferPack(&p->Buffer,0); BufferWrite(&p->Buffer,Packet->Data[0],Packet->Length,256); } else p->Codec.Packet.RefTime = TIME_UNKNOWN; if (p->Buffer.WritePos - p->Buffer.ReadPos < 1) return ERR_NEED_MORE_DATA; if (p->Buffer.Data[p->Buffer.ReadPos] == '#' && p->Buffer.WritePos - p->Buffer.ReadPos > 9 && memcmp(p->Buffer.Data+p->Buffer.ReadPos,"#!AMR-WB\n",9)==0) p->Buffer.ReadPos += 9; Size = block_size[(p->Buffer.Data[p->Buffer.ReadPos] >> 3) & 0xF]; if (p->Buffer.WritePos - p->Buffer.ReadPos < Size) return ERR_NEED_MORE_DATA; D_IF_decode(p->Decoder, p->Buffer.Data+p->Buffer.ReadPos, p->Synth, _good_frame); p->Buffer.ReadPos += Size; p->Codec.Packet.Length = sizeof(p->Synth); p->Codec.Packet.Data[0] = p->Synth; return ERR_NONE; }
/* * 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 switch_status_t switch_amrwb_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 AMRWB_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 amrwb_context *context = codec->private_info; if (!context) { return SWITCH_STATUS_FALSE; } D_IF_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 }
int main(int argc, char *argv[]) { FILE *f_serial; /* File of serial bits for transmission */ FILE *f_synth; /* File of speech data */ Word16 synth[L_FRAME16k]; /* Buffer for speech @ 16kHz */ UWord8 serial[NB_SERIAL_MAX]; Word16 mode; Word32 frame; #ifndef IF2 char magic[16]; #endif void *st; fprintf(stderr, "\n"); fprintf(stderr, "===================================================================\n"); fprintf(stderr, " 3GPP AMR-WB Floating-point Speech Decoder, v7.0.0, Mar 20, 2007\n"); fprintf(stderr, "===================================================================\n"); fprintf(stderr, "\n"); /* * Read passed arguments and open in/out files */ if (argc != 3) { fprintf(stderr, "Usage : decoder bitstream_file synth_file\n"); fprintf(stderr, "\n"); fprintf(stderr, "Format for bitstream_file:\n"); #ifdef IF2 fprintf(stderr, " Described in TS26.201.\n"); #else fprintf(stderr, " Described in RFC 3267 (Sections 5.1 and 5.3).\n"); #endif fprintf(stderr, "\n"); fprintf(stderr, "Format for synth_file:\n"); fprintf(stderr, " Synthesis is written to a binary file of 16 bits data.\n"); fprintf(stderr, "\n"); exit(0); } /* Open file for synthesis and packed serial stream */ if ((f_serial = fopen(argv[1], "rb")) == NULL) { fprintf(stderr, "Input file '%s' does not exist !!\n", argv[1]); exit(0); } else { fprintf(stderr, "Input bitstream file: %s\n", argv[1]); } if ((f_synth = fopen(argv[2], "wb")) == NULL) { fprintf(stderr, "Cannot open file '%s' !!\n", argv[2]); exit(0); } else { fprintf(stderr, "Synthesis speech file: %s\n", argv[2]); } /* * Initialization of decoder */ st = D_IF_init(); #ifndef IF2 /* read magic number */ fread(magic, sizeof(char), strlen(AMRWB_MAGIC_NUMBER), f_serial); /* verify magic number */ if (strncmp(magic, AMRWB_MAGIC_NUMBER, strlen(AMRWB_MAGIC_NUMBER))) { fprintf(stderr, "%s%s\n", "Invalid magic number: ", magic); fclose(f_serial); fclose(f_synth); exit(0); } #endif /* * Loop for each "L_FRAME" speech data */ fprintf(stderr, "\n --- Running ---\n"); frame = 0; while (fread(serial, sizeof (UWord8), 1, f_serial ) > 0) { #ifdef IF2 mode = (Word16)(serial[0] >> 4); #else mode = (Word16)((serial[0] >> 3) & 0x0F); #endif fread(&serial[1], sizeof (UWord8), block_size[mode] - 1, f_serial ); frame++; fprintf(stderr, " Decoding frame: %ld\r", frame); D_IF_decode( st, serial, synth, _good_frame); fwrite(synth, sizeof(Word16), L_FRAME16k, f_synth); fflush(f_synth); } D_IF_exit(st); fclose(f_serial); fclose(f_synth); return 0; }
int main(int argc, char *argv[]) { FILE *f_serial; /* File of serial bits for transmission */ FILE *f_synth; /* File of speech data */ Word16 synth[L_FRAME16k]; /* Buffer for speech @ 16kHz */ UWord8 serial[NB_SERIAL_MAX]; Word16 mode; Word32 frame; void *st; fprintf(stderr, "\n"); fprintf(stderr, "===================================================================\n"); fprintf(stderr, " 3GPP AMR-WB Floating-point Speech Decoder, v5.0.0, Mar 05, 2002\n"); fprintf(stderr, "===================================================================\n"); fprintf(stderr, "\n"); /* * Read passed arguments and open in/out files */ if (argc != 3) { fprintf(stderr, "Usage : decoder bitstream_file synth_file\n"); fprintf(stderr, "\n"); fprintf(stderr, "Format for bitstream_file:\n"); fprintf(stderr, " Described in TS26.201.\n"); fprintf(stderr, "\n"); fprintf(stderr, "Format for synth_file:\n"); fprintf(stderr, " Synthesis is written to a binary file of 16 bits data.\n"); fprintf(stderr, "\n"); exit(0); } /* Open file for synthesis and packed serial stream */ if ((f_serial = fopen(argv[1], "rb")) == NULL) { fprintf(stderr, "Input file '%s' does not exist !!\n", argv[1]); exit(0); } else { fprintf(stderr, "Input bitstream file: %s\n", argv[1]); } if ((f_synth = fopen(argv[2], "wb")) == NULL) { fprintf(stderr, "Cannot open file '%s' !!\n", argv[2]); exit(0); } else { fprintf(stderr, "Synthesis speech file: %s\n", argv[2]); } /* * Initialization of decoder */ st = D_IF_init(); /* * Loop for each "L_FRAME" speech data */ fprintf(stderr, "\n --- Running ---\n"); frame = 0; while (fread(serial, sizeof (UWord8), 1, f_serial ) > 0) { mode = (Word16)(serial[0] >> 4); fread(&serial[1], sizeof (UWord8), block_size[mode] - 1, f_serial ); frame++; fprintf(stderr, " Decoding frame: %ld\r", frame); D_IF_decode( st, serial, synth, _good_frame); fwrite(synth, sizeof(Word16), L_FRAME16k, f_synth); fflush(f_synth); } D_IF_exit(st); fclose(f_serial); fclose(f_synth); return 0; }