Ejemplo n.º 1
0
static switch_status_t switch_adpcm_init(switch_codec_t *codec, switch_codec_flag_t flags, const switch_codec_settings_t *codec_settings)
{
	uint32_t encoding, decoding;
	struct ima_adpcm_context *context = NULL;

	encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
	decoding = (flags & SWITCH_CODEC_FLAG_DECODE);

	if (!(encoding || decoding) || (!(context = switch_core_alloc(codec->memory_pool, sizeof(*context))))) {
		return SWITCH_STATUS_FALSE;
	} else {
		if (encoding) {
			context->encoder_object = ima_adpcm_init(context->encoder_object, IMA_ADPCM_DVI4, 0);
		}
		if (decoding) {
			context->decoder_object = ima_adpcm_init(context->decoder_object, IMA_ADPCM_DVI4, 0);
		}

		codec->private_info = context;
		return SWITCH_STATUS_SUCCESS;
	}
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    int i;
    SNDFILE *inhandle;
    SNDFILE *outhandle;
    int frames;
    int dec_frames;
    int outframes;
    int ima_bytes;
    double pre_energy;
    double post_energy;
    double diff_energy;
    int16_t pre_amp[HIST_LEN];
    int16_t post_amp[HIST_LEN];
    uint8_t ima_data[HIST_LEN];
    int16_t history[HIST_LEN];
    int hist_in;
    int hist_out;
    ima_adpcm_state_t *ima_enc_state;
    ima_adpcm_state_t *ima_dec_state;
    int xx;
    int total_pre_samples;
    int total_compressed_bytes;
    int total_post_samples;
    const char *in_file_name;
    int variant;
    int chunk_size;
    int enc_chunk_size;
    int log_encoded_data;
    int opt;

    variant = IMA_ADPCM_DVI4;
    in_file_name = IN_FILE_NAME;
    chunk_size = 160;
    enc_chunk_size = 0;
    log_encoded_data = FALSE;
    while ((opt = getopt(argc, argv, "ac:i:lv")) != -1)
    {
        switch (opt)
        {
        case 'a':
            variant = IMA_ADPCM_IMA4;
            chunk_size = 505;
            break;
        case 'c':
            enc_chunk_size = atoi(optarg);
            break;
        case 'i':
            in_file_name = optarg;
            break;
        case 'l':
            log_encoded_data = TRUE;
            break;
        case 'v':
            variant = IMA_ADPCM_VDVI;
            break;
        default:
            //usage();
            exit(2);
            break;
        }
    }

    if ((inhandle = sf_open_telephony_read(in_file_name, 1)) == NULL)
    {
        fprintf(stderr, "    Cannot open audio file '%s'\n", in_file_name);
        exit(2);
    }

    if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL)
    {
        fprintf(stderr, "    Cannot create audio file '%s'\n", OUT_FILE_NAME);
        exit(2);
    }

    if ((ima_enc_state = ima_adpcm_init(NULL, variant, enc_chunk_size)) == NULL)
    {
        fprintf(stderr, "    Cannot create encoder\n");
        exit(2);
    }
        
    if ((ima_dec_state = ima_adpcm_init(NULL, variant, enc_chunk_size)) == NULL)
    {
        fprintf(stderr, "    Cannot create decoder\n");
        exit(2);
    }

    hist_in = 0;
    hist_out = 0;
    pre_energy = 0.0;
    post_energy = 0.0;
    diff_energy = 0.0;
    total_pre_samples = 0;
    total_compressed_bytes = 0;
    total_post_samples = 0;
    while ((frames = sf_readf_short(inhandle, pre_amp, chunk_size)))
    {
        total_pre_samples += frames;
        ima_bytes = ima_adpcm_encode(ima_enc_state, ima_data, pre_amp, frames);
        if (log_encoded_data)
            write(1, ima_data, ima_bytes);
        total_compressed_bytes += ima_bytes;
        dec_frames = ima_adpcm_decode(ima_dec_state, post_amp, ima_data, ima_bytes);
        total_post_samples += dec_frames;
        for (i = 0;  i < frames;  i++)
        {
            history[hist_in++] = pre_amp[i];
            if (hist_in >= HIST_LEN)
                hist_in = 0;
            pre_energy += (double) pre_amp[i] * (double) pre_amp[i];
        }
        for (i = 0;  i < dec_frames;  i++)
        {
            post_energy += (double) post_amp[i] * (double) post_amp[i];
            xx = post_amp[i] - history[hist_out++];
            if (hist_out >= HIST_LEN)
                hist_out = 0;
            diff_energy += (double) xx * (double) xx;
        }
        outframes = sf_writef_short(outhandle, post_amp, dec_frames);
    }
    if (sf_close_telephony(inhandle))
    {
        fprintf(stderr, "    Cannot close audio file '%s'\n", in_file_name);
        exit(2);
    }
    if (sf_close_telephony(outhandle))
    {
        fprintf(stderr, "    Cannot close audio file '%s'\n", OUT_FILE_NAME);
        exit(2);
    }
    ima_adpcm_release(ima_enc_state);
    ima_adpcm_release(ima_dec_state);

    printf("Pre samples: %d\n", total_pre_samples);
    printf("Compressed bytes: %d\n", total_compressed_bytes);
    printf("Post samples: %d\n", total_post_samples);

    printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
    printf("Residual energy is %f%% of the total.\n", 100.0*diff_energy/post_energy);
    if (fabs(1.0 - post_energy/pre_energy) > 0.05
        ||
        fabs(diff_energy/post_energy) > 0.03)
    {
        printf("Tests failed.\n");
        exit(2);
    }
    
    printf("Tests passed.\n");
    return 0;
}