Word16 GSMInitEncode(void **state_data, Flag dtx, Word8 *id) { Speech_Encode_FrameState* s; OSCL_UNUSED_ARG(id); if (state_data == NULL) { /* fprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n"); */ return -1; } *state_data = NULL; /* allocate memory */ if ((s = (Speech_Encode_FrameState *) oscl_malloc(sizeof(Speech_Encode_FrameState))) == NULL) { /* fprintf(stderr, "Speech_Encode_Frame_init: can not malloc state " "structure\n"); */ return -1; } s->pre_state = NULL; s->cod_amr_state = NULL; s->dtx = dtx; if (Pre_Process_init(&s->pre_state) || cod_amr_init(&s->cod_amr_state, s->dtx)) { Speech_Encode_FrameState** temp = &s; GSMEncodeFrameExit((void**)temp); return -1; } Speech_Encode_Frame_reset(s); *state_data = (void *)s; return 0; }
/************************************************************************* * * Function: Speech_Encode_Frame_init * Purpose: Allocates memory for filter structure and initializes * state memory * ************************************************************************** */ int Speech_Encode_Frame_init (Speech_Encode_FrameState **state, Flag dtx, char *id) { Speech_Encode_FrameState* s; if (state == (Speech_Encode_FrameState **) NULL){ fprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n"); return -1; } *state = NULL; /* allocate memory */ if ((s= (Speech_Encode_FrameState *) malloc(sizeof(Speech_Encode_FrameState))) == NULL){ fprintf(stderr, "Speech_Encode_Frame_init: can not malloc state " "structure\n"); return -1; } s->complexityCounter = getCounterId(id); s->pre_state = NULL; s->cod_amr_state = NULL; s->dtx = dtx; if (Pre_Process_init(&s->pre_state) || cod_amr_init(&s->cod_amr_state, s->dtx)) { Speech_Encode_Frame_exit(&s); return -1; } Speech_Encode_Frame_reset(s); *state = s; return 0; }
/* ***************************************************************************** * MAIN PROGRAM ***************************************************************************** */ int main (int argc, char *argv[]) { char *progname = argv[0]; char *modeStr = NULL; char *usedModeStr = NULL; char *fileName = NULL; char *modefileName = NULL; char *serialFileName = NULL; FILE *file_speech = NULL; /* File of speech data */ FILE *file_serial = NULL; /* File of coded bits */ FILE *file_modes = NULL; /* File with mode information */ Word16 new_speech[L_FRAME]; /* Pointer to new speech data */ Word16 serial[SERIAL_FRAMESIZE]; /* Output bitstream buffer */ #ifdef MMS_IO UWord8 packed_bits[MAX_PACKED_SIZE]; Word16 packed_size; #endif Word32 frame; Word16 dtx = 0; /* enable encoder DTX */ /* changed eedodr */ Word16 reset_flag; int i; enum Mode mode; enum Mode used_mode; enum TXFrameType tx_type; int useModeFile = 0; Speech_Encode_FrameState *speech_encoder_state = NULL; sid_syncState *sid_state = NULL; proc_head ("Encoder"); fprintf(stderr, "Code compiled with VAD option: %s\n\n", get_vadname()); /*----------------------------------------------------------------------* * Process command line options * *----------------------------------------------------------------------*/ while (argc > 1) { if (strcmp(argv[1], "-dtx") == 0) { dtx = 1; } else if (strncmp(argv[1], "-modefile=", 10) == 0) { useModeFile = 1; modefileName = argv[1]+10; } else break; argc--; argv++; } /*----------------------------------------------------------------------* * Check number of arguments * *----------------------------------------------------------------------*/ if ( (argc != 4 && !useModeFile) || (argc != 3 && useModeFile)) { fprintf (stderr, " Usage:\n\n" " %s [-dtx] amr_mode speech_file bitstream_file\n\n" " or \n\n" " %s [-dtx] -modefile=mode_file speech_file bitstream_file\n\n" " -dtx enables DTX mode\n" " -modefile=mode_file reads AMR modes from text file (one line per frame)\n\n", progname, progname); exit (1); } /*----------------------------------------------------------------------* * Open mode file or convert mode string * *----------------------------------------------------------------------*/ if (useModeFile) { fileName = argv[1]; serialFileName = argv[2]; /* Open mode control file */ if (strcmp(modefileName, "-") == 0) { file_modes = stdin; } else if ((file_modes = fopen (modefileName, "rt")) == NULL) { fprintf (stderr, "Error opening mode control file %s !!\n", modefileName); exit (1); } fprintf (stderr, " Mode control file: %s\n", modefileName); } else { modeStr = argv[1]; fileName = argv[2]; serialFileName = argv[3]; /* check and convert mode string */ if (str2mode(modeStr, &mode) != 0 && mode != MRDTX) { fprintf(stderr, "Invalid amr_mode specified: '%s'\n", modeStr); exit(1); } } /*----------------------------------------------------------------------* * Open speech file and result file (output serial bit stream) * *----------------------------------------------------------------------*/ if (strcmp(fileName, "-") == 0) { file_speech = stdin; } else if ((file_speech = fopen (fileName, "rb")) == NULL) { fprintf (stderr, "Error opening input file %s !!\n", fileName); exit (1); } fprintf (stderr, " Input speech file: %s\n", fileName); if (strcmp(serialFileName, "-") == 0) { file_serial = stdout; } else if ((file_serial = fopen (serialFileName, "wb")) == NULL) { fprintf (stderr,"Error opening output bitstream file %s !!\n",serialFileName); exit (1); } fprintf (stderr, " Output bitstream file: %s\n", serialFileName); /*-----------------------------------------------------------------------* * Initialisation of the coder. * *-----------------------------------------------------------------------*/ if ( Speech_Encode_Frame_init(&speech_encoder_state, dtx, "encoder") || sid_sync_init (&sid_state)) exit(-1); #ifdef MMS_IO /* write magic number to indicate single channel AMR file storage format */ fwrite(AMR_MAGIC_NUMBER, sizeof(UWord8), strlen(AMR_MAGIC_NUMBER), file_serial); #endif /*-----------------------------------------------------------------------* * Process speech frame by frame * *-----------------------------------------------------------------------*/ frame = 0; while (fread (new_speech, sizeof (Word16), L_FRAME, file_speech) == L_FRAME) { /* read new mode string from file if required */ if (useModeFile) { int res; if ((res = read_mode(file_modes, &mode)) == EOF) { fprintf(stderr, "\nend of mode control file reached"); break; } else if (res == 1) exit(-1); } frame++; /* zero flags and parameter bits */ for (i = 0; i < SERIAL_FRAMESIZE; i++) serial[i] = 0; /* check for homing frame */ reset_flag = encoder_homing_frame_test(new_speech); /* encode speech */ Speech_Encode_Frame(speech_encoder_state, mode, new_speech, &serial[1], &used_mode); /* print frame number and mode information */ mode2str(mode, &modeStr); mode2str(used_mode, &usedModeStr); if ( (frame%50) == 0) { fprintf (stderr, "\rframe=%-8d mode=%-5s used_mode=%-5s", frame, modeStr, usedModeStr); } /* include frame type and mode information in serial bitstream */ sid_sync (sid_state, used_mode, &tx_type); #ifndef MMS_IO serial[0] = tx_type; if (tx_type != TX_NO_DATA) { serial[1+MAX_SERIAL_SIZE] = mode; } else { serial[1+MAX_SERIAL_SIZE] = -1; } /* write bitstream to output file */ if (fwrite (serial, sizeof (Word16), SERIAL_FRAMESIZE, file_serial) != SERIAL_FRAMESIZE) { fprintf(stderr, "\nerror writing output file: %s\n", strerror(errno)); exit(-1); } #else packed_size = PackBits(used_mode, mode, tx_type, &serial[1], packed_bits); /* write file storage format bitstream to output file */ if (fwrite (packed_bits, sizeof (UWord8), packed_size, file_serial) != packed_size) { fprintf(stderr, "\nerror writing output file: %s\n", strerror(errno)); exit(-1); } #endif fflush(file_serial); /* perform homing if homing frame was detected at encoder input */ if (reset_flag != 0) { Speech_Encode_Frame_reset(speech_encoder_state); sid_sync_reset(sid_state); } } fprintf (stderr, "\n%d frame(s) processed\n", frame); /*-----------------------------------------------------------------------* * Close down speech coder * *-----------------------------------------------------------------------*/ Speech_Encode_Frame_exit(&speech_encoder_state); sid_sync_exit (&sid_state); return (0); }
int amrcoder(char *src,ssize_t src_size,char *dst,ssize_t *dst_size,int armmode,int channels){ char *modeStr = NULL; char *usedModeStr = NULL; Word16 new_speech[L_FRAME]; /* Pointer to new speech data */ Word16 serial[SERIAL_FRAMESIZE]; /* Output bitstream buffer */ Word16 *psrc; UWord8 packed_bits[MAX_PACKED_SIZE]; Word16 packed_size; Word32 frame; /* changed eedodr */ Word16 reset_flag; int i; int y; enum Mode mode; enum Mode used_mode; enum TXFrameType tx_type; *dst_size=0; if(armmode<0||armmode>7) mode=DEFAULT_MODE; else mode=(enum Mode)armmode; /*-----------------------------------------------------------------------* * Initialisation of the coder. * *-----------------------------------------------------------------------*/ /* if ( Speech_Encode_Frame_init(&speech_encoder_state, dtx, "encoder") || sid_sync_init (&sid_state)) return -1; */ frame = 0; psrc=(Word16 *)src; /*16 bits 2channels each pcm frame occupy 4bytes*/ pthread_mutex_lock(&amr_encode_lock); while (src_size>=2*channels*L_FRAME) { /*use left channel */ for(y=0;y<L_FRAME;y++){ new_speech[y]=psrc[0]; psrc+=channels; } src_size-=L_FRAME*2*channels; frame++; /* zero flags and parameter bits */ for (i = 0; i < SERIAL_FRAMESIZE; i++) serial[i] = 0; /* check for homing frame */ reset_flag = encoder_homing_frame_test(new_speech); /* encode speech */ Speech_Encode_Frame(speech_encoder_state, mode, new_speech, &serial[1], &used_mode); /* print frame number and mode information */ mode2str(mode, &modeStr); mode2str(used_mode, &usedModeStr); if ( (frame%50) == 0) { fprintf (stderr, "\rframe=%-8d mode=%-5s used_mode=%-5s", frame, modeStr, usedModeStr); } /* include frame type and mode information in serial bitstream */ sid_sync (sid_state, used_mode, &tx_type); packed_size = PackBits(used_mode, mode, tx_type, &serial[1], packed_bits); /* write file storage format bitstream to output file */ memcpy(dst,packed_bits,sizeof(UWord8)*packed_size); dst+=sizeof(UWord8)*packed_size; *dst_size+=sizeof(UWord8)*packed_size; /* if (fwrite (packed_bits, sizeof (UWord8), packed_size, file_serial) != packed_size) { fprintf(stderr, "\nerror writing output file: %s\n", strerror(errno)); exit(-1); } fflush(file_serial); */ /* perform homing if homing frame was detected at encoder input */ if (reset_flag != 0) { Speech_Encode_Frame_reset(speech_encoder_state); sid_sync_reset(sid_state); } } pthread_mutex_unlock(&amr_encode_lock); /*-----------------------------------------------------------------------* * Close down speech coder * *-----------------------------------------------------------------------*/ /* Speech_Encode_Frame_exit(&speech_encoder_state); sid_sync_exit (&sid_state); */ return (0); }
struct OutputBuffer* encodePCMtoAMR(const char *buf, unsigned int len) { const char* curr_buf = buf; unsigned int curr_len = len; struct OutputBuffer* ob = NULL; Speech_Encode_FrameState *speech_encoder_state = NULL; sid_syncState *sid_state = NULL; //Word16 new_speech[L_FRAME]; /* Pointer to new speech data */ Word16 *new_speech = NULL; Word16 serial[SERIAL_FRAMESIZE]; /* Output bitstream buffer */ #ifdef MMS_IO UWord8 packed_bits[MAX_PACKED_SIZE]; Word16 packed_size; #endif int i; Word16 dtx = 0; const unsigned int frame_size = sizeof(Word16)*L_FRAME; enum Mode mode = MR475; enum Mode used_mode; enum TXFrameType tx_type; /* changed eedodr */ Word16 reset_flag; /*-----------------------------------------------------------------------* * Initialisation of the coder. * *-----------------------------------------------------------------------*/ if (Speech_Encode_Frame_init(&speech_encoder_state, dtx, "encoder") || sid_sync_init(&sid_state)) { return NULL; } ob = OB_New(); #ifdef MMS_IO /* write magic number to indicate single channel AMR file storage format */ OB_Append(ob, AMR_MAGIC_NUMBER, AMR_MAGIC_NUMBER_LEN); #endif /*-----------------------------------------------------------------------* * Process speech frame by frame * *-----------------------------------------------------------------------*/ while (curr_len >= frame_size) { // memcpy(new_speech, curr_buf, frame_size); new_speech = (Word16*)curr_buf; curr_buf += frame_size; curr_len -= frame_size; /* zero flags and parameter bits */ for (i = 0; i < SERIAL_FRAMESIZE; i++) serial[i] = 0; /* check for homing frame */ reset_flag = encoder_homing_frame_test(new_speech); /* encode speech */ Speech_Encode_Frame(speech_encoder_state, mode, new_speech, &serial[1], &used_mode); /* include frame type and mode information in serial bitstream */ sid_sync(sid_state, used_mode, &tx_type); #ifndef MMS_IO serial[0] = tx_type; if (tx_type != TX_NO_DATA) { serial[1+MAX_SERIAL_SIZE] = mode; } else { serial[1+MAX_SERIAL_SIZE] = -1; } /* write bitstream to output file */ if (fwrite (serial, sizeof (Word16), SERIAL_FRAMESIZE, file_serial) != SERIAL_FRAMESIZE) { fprintf(stderr, "\nerror writing output file: %s\n", strerror(errno)); exit(-1); } #else packed_size = PackBits(used_mode, mode, tx_type, &serial[1], packed_bits); /* write file storage format bitstream to output file */ OB_Append(ob, (const char *)packed_bits, packed_size); #endif /* perform homing if homing frame was detected at encoder input */ if (reset_flag != 0) { Speech_Encode_Frame_reset(speech_encoder_state); sid_sync_reset(sid_state); } } Speech_Encode_Frame_exit(&speech_encoder_state); sid_sync_exit(&sid_state); return ob; }