Ejemplo n.º 1
0
/*
 * Modify codec settings.
 */
static pj_status_t amr_codec_modify( pjmedia_codec *codec, 
				     const pjmedia_codec_param *attr )
{
    struct amr_data *amr_data = (struct amr_data*) codec->codec_data;
    pj_bool_t prev_vad_state;

    pj_assert(amr_data != NULL);
    pj_assert(amr_data->encoder != NULL && amr_data->decoder != NULL);

    prev_vad_state = amr_data->vad_enabled;
    amr_data->vad_enabled = (attr->setting.vad != 0);
    amr_data->plc_enabled = (attr->setting.plc != 0);

    if (amr_data->enc_setting.amr_nb &&
        prev_vad_state != amr_data->vad_enabled)
    {
	/* Reinit AMR encoder to update VAD setting */
	TRACE_((THIS_FILE, "Reiniting AMR encoder to update VAD setting."));
#ifdef USE_AMRNB
        Encoder_Interface_exit(amr_data->encoder);
        amr_data->encoder = Encoder_Interface_init(amr_data->vad_enabled);
#endif
        if (amr_data->encoder == NULL) {
	    TRACE_((THIS_FILE, "Encoder_Interface_init() failed"));
	    amr_codec_close(codec);
	    return PJMEDIA_CODEC_EFAILED;
	}
    }

    TRACE_((THIS_FILE, "AMR codec modified: vad=%d, plc=%d",
			amr_data->vad_enabled, amr_data->plc_enabled));
    return PJ_SUCCESS;
}
Ejemplo n.º 2
0
static GstStateChangeReturn
gst_amrnbenc_state_change (GstElement * element, GstStateChange transition)
{
  GstAmrnbEnc *amrnbenc;
  GstStateChangeReturn ret;

  amrnbenc = GST_AMRNBENC (element);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      if (!(amrnbenc->handle = Encoder_Interface_init (0)))
        return GST_STATE_CHANGE_FAILURE;
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      amrnbenc->rate = 0;
      amrnbenc->channels = 0;
      amrnbenc->ts = 0;
      gst_adapter_clear (amrnbenc->adapter);
      break;
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:
      Encoder_Interface_exit (amrnbenc->handle);
      break;
    default:
      break;
  }
  return ret;
}
Ejemplo n.º 3
0
int encoder_uninitialize(unsigned long id)
{
	if(!pestreams[id].firstwrite)
	{
		sys_file_close(pestreams[id].fhandle);
		Encoder_Interface_exit(pestreams[id].enstate);
		pestreams[id].firstwrite = 1;
	}
	return 1;
}
Ejemplo n.º 4
0
EncoderAudioSource::~EncoderAudioSource()
{
	if (fBuffer)
		delete [] fBuffer;
	if (fp)
		fclose(fp);
	if(fAudioCodec)
	{
		Encoder_Interface_exit(fAudioCodec);
	}
}
Ejemplo n.º 5
0
static gboolean
gst_amrnbenc_stop (GstAudioEncoder * enc)
{
  GstAmrnbEnc *amrnbenc = GST_AMRNBENC (enc);

  GST_DEBUG_OBJECT (amrnbenc, "stop");

  Encoder_Interface_exit (amrnbenc->handle);

  return TRUE;
}
Ejemplo n.º 6
0
EncoderAudioSource::~EncoderAudioSource()
{
	if (fBuffer)
		delete [] fBuffer;
	if (fp)
		fclose(fp);
	if(fAudioCodec)
	{
#ifndef __WIN32__
		Encoder_Interface_exit(fAudioCodec);
#endif
	}
}
Ejemplo n.º 7
0
static switch_status_t switch_amr_destroy(switch_codec_t *codec)
{
#ifndef AMR_PASSTHROUGH
	struct amr_context *context = codec->private_info;

	if (context->encoder_state) {
		Encoder_Interface_exit(context->encoder_state);
	}
	if (context->decoder_state) {
		Decoder_Interface_exit(context->decoder_state);
	}
	codec->private_info = NULL;
#endif
	return SWITCH_STATUS_SUCCESS;
}
Ejemplo n.º 8
0
EncoderAudioSource::~EncoderAudioSource()
{
	if (fBuffer)
		delete [] fBuffer;
#ifdef STARV_TEST
	if (fp)
		fclose(fp);
#endif

#ifdef ENC_SOURCE
	if(fAudioCodec)
	{
		Encoder_Interface_exit(fAudioCodec);
	}
#endif
}
Ejemplo n.º 9
0
int i51KitAmrPcmStream2AmrStream ( void* pcm_stream , iU32 stream_length , void* amr_buffer , iU32 buff_length ) {

	//	author : jelo
	//	since : 2012.3.21
	// 	(C) 2011 , PKIG Tech. Co., Ltd.	

	if ( 0 == pcm_stream || 0 == stream_length || 0 == amr_buffer || 0 == buff_length ) {
		iLog ( "AMR-Enc : Buffer is Empty" ) ;
		return 0 ;
	}

	Encoder_Interface_init () ;
	
   enstate = Encoder_Interface_init(dtx);

   while (fread( speech, sizeof (Word16), 160, file_speech ) > 0)
   {
      /* read mode */
      if (file_mode != NULL){
         req_mode = 8;
         if (fscanf(file_mode, "%9s\n", mode_string) != EOF) {
            mode_tmp = strtol(&mode_string[2], NULL, 0);
            for (req_mode = 0; req_mode < 8; req_mode++){
               if (mode_tmp == modeConv[req_mode]){
                  break;
               }
            }
         }
         if (req_mode == 8){
            break;
         }
      }

      frames ++;

      /* call encoder */
      byte_counter = Encoder_Interface_Encode(enstate, req_mode, speech, serial_data, 0);

      bytes += byte_counter;
      fwrite(serial_data, sizeof (UWord8), byte_counter, file_encoded );
      fflush(file_encoded);
   }
   Encoder_Interface_exit(enstate);


}
Ejemplo n.º 10
0
Archivo: amr.c Proyecto: GGGO/baresip
static void encode_destructor(void *arg)
{
	struct auenc_state *st = arg;

	switch (st->ac->srate) {

#ifdef AMR_NB
	case 8000:
		Encoder_Interface_exit(st->enc);
		break;
#endif

#ifdef AMR_WB
	case 16000:
		E_IF_exit(st->enc);
		break;
#endif
	}
}
Ejemplo n.º 11
0
/*
 * Close codec.
 */
static pj_status_t amr_codec_close( pjmedia_codec *codec )
{
    struct amr_data *amr_data;

    PJ_ASSERT_RETURN(codec, PJ_EINVAL);

    amr_data = (struct amr_data*) codec->codec_data;
    PJ_ASSERT_RETURN(amr_data != NULL, PJ_EINVALIDOP);

    if (amr_data->encoder) {
        Encoder_Interface_exit(amr_data, amr_data->encoder);
        amr_data->encoder = NULL;
    }

    if (amr_data->decoder) {
        Decoder_Interface_exit(amr_data, amr_data->decoder);
        amr_data->decoder = NULL;
    }

    TRACE_((THIS_FILE, "AMR-NB codec closed"));
    return PJ_SUCCESS;
}
Ejemplo n.º 12
0
/*
 * Close codec.
 */
static pj_status_t amr_codec_close( pjmedia_codec *codec )
{
    struct amr_data *amr_data;

    PJ_ASSERT_RETURN(codec, PJ_EINVAL);

    amr_data = (struct amr_data*) codec->codec_data;
    PJ_ASSERT_RETURN(amr_data != NULL, PJ_EINVALIDOP);

    if (amr_data->encoder) {
        if (amr_data->enc_setting.amr_nb) {
#ifdef USE_AMRNB
            Encoder_Interface_exit(amr_data->encoder);
#endif
        } else {
#ifdef USE_AMRWB
            E_IF_exit(amr_data->encoder);
#endif
        }
        amr_data->encoder = NULL;
    }

    if (amr_data->decoder) {
        if (amr_data->dec_setting.amr_nb) {
#ifdef USE_AMRNB
            Decoder_Interface_exit(amr_data->decoder);
#endif
        } else {
#ifdef USE_AMRWB
            D_IF_exit(amr_data->decoder);
#endif
        }
        amr_data->decoder = NULL;
    }
    
    TRACE_((THIS_FILE, "AMR codec closed"));
    return PJ_SUCCESS;
}
static void amr_destroy_encoder(const struct PluginCodec_Definition * codec, void * context)
{
    AmrEncoderContext *ctx = (AmrEncoderContext *)context;
    Encoder_Interface_exit(ctx->encoder_state);
    free(ctx);
}
Ejemplo n.º 14
0
int main(int argc, char *argv[]) {
	enum Mode mode = MR122;
	int ch, dtx = 0;
	const char *infile, *outfile;
	FILE *out;
	void *wav, *amr;
	int format, sampleRate, channels, bitsPerSample;
	int inputSize;
	uint8_t* inputBuf;
	while ((ch = getopt(argc, argv, "r:d")) != -1) {
		switch (ch) {
		case 'r':
			mode = findMode(optarg);
			break;
		case 'd':
			dtx = 1;
			break;
		case '?':
		default:
			usage(argv[0]);
			return 1;
		}
	}
	if (argc - optind < 2) {
		usage(argv[0]);
		return 1;
	}
	infile = argv[optind];
	outfile = argv[optind + 1];

	wav = wav_read_open(infile);
	if (!wav) {
		fprintf(stderr, "Unable to open wav file %s\n", infile);
		return 1;
	}
	if (!wav_get_header(wav, &format, &channels, &sampleRate, &bitsPerSample, NULL)) {
		fprintf(stderr, "Bad wav file %s\n", infile);
		return 1;
	}
	if (format != 1) {
		fprintf(stderr, "Unsupported WAV format %d\n", format);
		return 1;
	}
	if (bitsPerSample != 16) {
		fprintf(stderr, "Unsupported WAV sample depth %d\n", bitsPerSample);
		return 1;
	}
	if (channels != 1)
		fprintf(stderr, "Warning, only compressing one audio channel\n");
	if (sampleRate != 8000)
		fprintf(stderr, "Warning, AMR-NB uses 8000 Hz sample rate (WAV file has %d Hz)\n", sampleRate);
	inputSize = channels*2*160;
	inputBuf = (uint8_t*) malloc(inputSize);

	amr = Encoder_Interface_init(dtx);
	out = fopen(outfile, "wb");
	if (!out) {
		perror(outfile);
		return 1;
	}

	fwrite("#!AMR\n", 1, 6, out);
	while (1) {
		short buf[160];
		uint8_t outbuf[500];
		int read, i, n;
		read = wav_read_data(wav, inputBuf, inputSize);
		read /= channels;
		read /= 2;
		if (read < 160)
			break;
		for (i = 0; i < 160; i++) {
			const uint8_t* in = &inputBuf[2*channels*i];
			buf[i] = in[0] | (in[1] << 8);
		}
		n = Encoder_Interface_Encode(amr, mode, buf, outbuf, 0);
		fwrite(outbuf, 1, n, out);
	}
	free(inputBuf);
	fclose(out);
	Encoder_Interface_exit(amr);
	wav_read_close(wav);

	return 0;
}
Ejemplo n.º 15
0
int encode_amr(const char* infile, const char* outfile) {
    enum Mode mode = MR122;
    FILE *out;
    void *wav, *amr;
    int format, sampleRate, channels, bitsPerSample;
    int inputSize;
    uint8_t* inputBuf;

    wav = wav_read_open(infile);
    if (!wav) {
        fprintf(stderr, "Unable to open wav file %s\n", infile);
        return 1;
    }
    if (!wav_get_header(wav, &format, &channels, &sampleRate, &bitsPerSample, NULL)) {
        fprintf(stderr, "Bad wav file %s\n", infile);
        return 1;
    }
    if (format != 1) {
        fprintf(stderr, "Unsupported WAV format %d\n", format);
        return 1;
    }
    if (bitsPerSample != 16) {
        fprintf(stderr, "Unsupported WAV sample depth %d\n", bitsPerSample);
        return 1;
    }
    if (channels != 1)
        fprintf(stderr, "Warning, only compressing one audio channel\n");
    if (sampleRate != 8000)
        fprintf(stderr, "Warning, AMR-NB uses 8000 Hz sample rate (WAV file has %d Hz)\n", sampleRate);
    
    inputSize = channels*2*160;
    inputBuf = (uint8_t*) malloc(inputSize);
    
    amr = Encoder_Interface_init(0);
    out = fopen(outfile, "wb");
    if (!out) {
        perror(outfile);
        return 1;
    }
    
    fwrite("#!AMR\n", 1, 6, out);
    while (1) {
        short buf[160];
        uint8_t outbuf[500];
        int read, i, n;
        read = wav_read_data(wav, inputBuf, inputSize);
        read /= channels;
        read /= 2;
        if (read < 160)
            break;
        for (i = 0; i < 160; i++) {
            const uint8_t* in = &inputBuf[2*channels*i];
            buf[i] = in[0] | (in[1] << 8);
        }
        n = Encoder_Interface_Encode(amr, mode, buf, outbuf, 0);
        fwrite(outbuf, 1, n, out);
    }
    free(inputBuf);
    fclose(out);
    Encoder_Interface_exit(amr);
    wav_read_close(wav);
    
    return 0;
}
Ejemplo n.º 16
0
static void enc_postprocess(MSFilter *f) {
    EncState *s = (EncState*) f->data;
    Encoder_Interface_exit(s->enc);
    s->enc = NULL;
    ms_bufferizer_flush(s->mb);
}
Ejemplo n.º 17
0
/*
 * main
 *
 *
 * Function:
 *    Speech encoder main program
 *
 *    Usage: encoder speech_file bitstream_file mode dtx mode_file
 *
 *    Format for speech_file:
 *       Speech is read from a binary file of 16 bits data.
 *
 *    Format for ETSI bitstream file:
 *       1 word (2-byte) for the TX frame type
 *       244 words (2-byte) containing 244 bits.
 *          Bit 0 = 0x0000 and Bit 1 = 0x0001
 *       1 word (2-byte) for the mode indication
 *       4 words for future use, currently written as zero
 *
 *    Format for 3GPP bitstream file:
 *       Holds mode information and bits packed to octets.
 *       Size is from 1 byte to 31 bytes.
 *
 *    ETSI bitstream file format is defined using ETSI as preprocessor
 *    definition
 *
 *    mode        : MR475, MR515, MR59, MR67, MR74, MR795, MR102, MR122
 *    mode_file   : reads mode information from a file
 * Returns:
 *    0
 */
int main (int argc, char * argv[]){

   /* file strucrures */
   FILE * file_speech = NULL;
   FILE * file_encoded = NULL;
   FILE * file_mode = NULL;

   /* input speech vector */
   short speech[160];

   /* counters */
   int byte_counter, frames = 0, bytes = 0;

   /* pointer to encoder state structure */
   int *enstate;

   /* requested mode */
   enum Mode req_mode = MR122;
   int dtx = 0;

   /* temporary variables */
   char mode_string[9];
   long mode_tmp;

   /* bitstream filetype */
#ifndef ETSI
   unsigned char serial_data[32];
#else
   short serial_data[250] = {0};
#endif

   /* Process command line options */

   if ((argc == 5) || (argc == 4)){
      file_encoded = fopen(argv[argc - 1], "wb");
      if (file_encoded == NULL){
         Usage(argv);
         return 1;
      }
      file_speech = fopen(argv[argc - 2], "rb");
      if (file_speech == NULL){
         fclose(file_encoded);
         Usage(argv);
         return 1;
      }
      if (strncmp(argv[argc - 3], "-modefile=", 10) == 0){
         file_mode = fopen(&argv[argc - 3][10], "rt");
         if (file_mode == NULL){
            Usage(argv);
            fclose(file_speech);
            fclose(file_encoded);
            return 1;
         }
      }
      else {
         mode_tmp = strtol(&argv[argc - 3][2], NULL, 0);
         for (req_mode = 0; req_mode < 8; req_mode++){
            if (mode_tmp == modeConv[req_mode])
               break;
         }
         if (req_mode == 8){
            Usage(argv);
            fclose(file_speech);
            fclose(file_encoded);
            if (file_mode != NULL)
               fclose(file_mode);
            return 1;
         }
      }
      if (argc == 5){
         if ((strcmp(argv[1], "-dtx") != 0)){
            Usage(argv);
            fclose(file_speech);
            fclose(file_encoded);
            if (file_mode != NULL){
               fclose(file_mode);
            }
            return 1;
         }
         else {
            dtx = 1;
         }
      }
   }
   else {
      Usage(argv);
      return 1;
   }


   enstate = Encoder_Interface_init(dtx);

   Copyright();
#ifndef VAD2
   fprintf( stderr, "%s\n", "Code compiled with VAD option: VAD1");
#else
   fprintf( stderr, "%s\n", "Code compiled with VAD option: VAD2");
#endif

#ifndef ETSI
#ifndef IF2
   /* write magic number to indicate single channel AMR file storage format */
   	bytes = fwrite(AMR_MAGIC_NUMBER, sizeof(char), strlen(AMR_MAGIC_NUMBER), file_encoded);
#endif
#endif

   /* read file */
   while (fread( speech, sizeof (Word16), 160, file_speech ) > 0)
   {
      /* read mode */
      if (file_mode != NULL){
         req_mode = 8;
         if (fscanf(file_mode, "%9s\n", mode_string) != EOF) {
            mode_tmp = strtol(&mode_string[2], NULL, 0);
            for (req_mode = 0; req_mode < 8; req_mode++){
               if (mode_tmp == modeConv[req_mode]){
                  break;
               }
            }
         }
         if (req_mode == 8){
            break;
         }
      }

      frames ++;

      /* call encoder */
      byte_counter = Encoder_Interface_Encode(enstate, req_mode, speech, serial_data, 0);

      bytes += byte_counter;
      fwrite(serial_data, sizeof (UWord8), byte_counter, file_encoded );
      fflush(file_encoded);
   }
   Encoder_Interface_exit(enstate);

#ifndef ETSI
#ifdef IF2
   fprintf ( stderr, "\n%s%i%s%i%s\n", "Frame structure AMR IF2: ", frames, " frames, ", bytes, " bytes.");
#else
   fprintf ( stderr, "\n%s%i%s%i%s\n", "Frame structure AMR MIME file storage format: ", frames, " frames, ", bytes, " bytes.");
#endif
#else
   fprintf ( stderr, "\n%s%i%s\n", "Frame structure AMR ETSI: ", frames, " frames. ");
#endif

   fclose(file_speech);
   fclose(file_encoded);
   if (file_mode != NULL)
      fclose(file_mode);

   return 0;
}
Ejemplo n.º 18
0
AMRAudioEncoder::~AMRAudioEncoder() {
  Encoder_Interface_exit(fEncoderState);
  delete[] fInputSampleBuffer;
}
Ejemplo n.º 19
0
static void enc_postprocess(AmrEncState *s) {
	Encoder_Interface_exit(s->amr_enc);
	s->amr_enc = NULL;	
}