示例#1
0
文件: g726.c 项目: AmesianX/baresip
static int encode(struct auenc_state *st, uint8_t *buf,
		  size_t *len, const int16_t *sampv, size_t sampc)
{
	if (!buf || !len || !sampv)
		return EINVAL;

	if (*len < MAX_PACKET)
		return ENOMEM;

	*len = g726_encode(&st->st, buf, sampv, (int)sampc);

	return 0;
}
示例#2
0
static int encode(struct auenc_state *st, uint8_t *buf,
		  size_t *len, int fmt, const void *sampv, size_t sampc)
{
	if (!buf || !len || !sampv)
		return EINVAL;

	if (fmt != AUFMT_S16LE)
		return ENOTSUP;

	if (*len < MAX_PACKET)
		return ENOMEM;

	*len = g726_encode(&st->st, buf, sampv, (int)sampc);

	return 0;
}
示例#3
0
static switch_status_t switch_g726_encode(switch_codec_t *codec,
										  switch_codec_t *other_codec,
										  void *decoded_data,
										  uint32_t decoded_data_len,
										  uint32_t decoded_rate, void *encoded_data, uint32_t *encoded_data_len, uint32_t *encoded_rate,
										  unsigned int *flag)
{
	g726_state_t *context = codec->private_info;

	if (!context) {
		return SWITCH_STATUS_FALSE;
	}

	*encoded_data_len = g726_encode(context, (uint8_t *) encoded_data, (int16_t *) decoded_data, decoded_data_len / 2);

	return SWITCH_STATUS_SUCCESS;
}
示例#4
0
int internal_encode_g726(void* handle, const void* pAudioBuffer, 
                         unsigned cbAudioSamples, int* rSamplesConsumed, 
                         void* pCodedData, unsigned cbMaxCodedData, 
                         int* pcbCodedSize, unsigned* pbSendNow)
{
   int bytes;

   bytes = g726_encode((g726_state_t*)handle, pCodedData, pAudioBuffer, cbAudioSamples);
   assert(bytes <= cbMaxCodedData);
   *pcbCodedSize = bytes;

//   printf("G726 encoder: samples consumed: %d, bytes in a frame: %d, bytes wrote: %d\n",
//          cbAudioSamples, bytes, cbMaxCodedData);

   *pbSendNow = FALSE;
   *rSamplesConsumed = cbAudioSamples;

   return RPLG_SUCCESS;
}
示例#5
0
SPAN_DECLARE(void) codec_munge(codec_munge_state_t *s, int16_t amp[], int len)
{
    uint8_t law;
    uint8_t adpcmdata[160];
    int i;
    int adpcm;
    int x;

    switch (s->munging_codec)
    {
    case MUNGE_CODEC_NONE:
        /* Do nothing */
        break;
    case MUNGE_CODEC_ALAW:
        for (i = 0;  i < len;  i++)
        {
            law = linear_to_alaw(amp[i]);
            amp[i] = alaw_to_linear(law);
        }
        break;
    case MUNGE_CODEC_ULAW:
        for (i = 0;  i < len;  i++)
        {
            law = linear_to_ulaw(amp[i]);
            if (s->rbs_pattern & (1 << s->sequence))
            {
                /* Strip the bottom bit at the RBS rate */
                law &= 0xFE;
            }
            amp[i] = ulaw_to_linear(law);
        }
        break;
    case MUNGE_CODEC_G726_32K:
        /* This could actually be any of the G.726 rates */
        for (i = 0;  i < len;  i += x)
        {
            x = (len - i >= 160)  ?  160  :  (len - i);
            adpcm = g726_encode(&s->g726_enc_state, adpcmdata, amp + i, x);
            g726_decode(&s->g726_dec_state, amp + i, adpcmdata, adpcm);
        }
        break;
    }
}
示例#6
0
void codec_munge(codec_munge_state_t *s, int16_t amp[], int len)
{
    uint8_t law;
    uint8_t adpcmdata[160];
    int i;
    int adpcm;
    int x;

    switch (s->munging_codec)
    {
    case MUNGE_CODEC_NONE:
        /* Do nothing */
        break;
    case MUNGE_CODEC_ALAW:
        for (i = 0;  i < len;  i++)
        {
            law = linear_to_alaw(amp[i]);
            amp[i] = alaw_to_linear(law);
        }
        break;
    case MUNGE_CODEC_ULAW:
        for (i = 0;  i < len;  i++)
        {
            law = linear_to_ulaw(amp[i]);
            amp[i] = ulaw_to_linear(law);
        }
        break;
    case MUNGE_CODEC_G726_32K:
        /* This could actually be any of the G.726 rates */
        for (i = 0;  i < len;  i += x)
        {
            x = (len - i >= 160)  ?  160  :  (len - i);
            adpcm = g726_encode(&s->g726_enc_state, adpcmdata, amp + i, x);
            g726_decode(&s->g726_dec_state, amp + i, adpcmdata, adpcm);
        }
        break;
    }
}
示例#7
0
文件: g726_tests.c 项目: vir/spandsp
int main(int argc, char *argv[])
{
    g726_state_t *enc_state;
    g726_state_t *dec_state;
    int opt;
    int itutests;
    int bit_rate;
    SNDFILE *inhandle;
    SNDFILE *outhandle;
    int16_t amp[1024];
    int frames;
    int adpcm;
    int packing;

    bit_rate = 32000;
    itutests = true;
    packing = G726_PACKING_NONE;
    while ((opt = getopt(argc, argv, "b:LR")) != -1)
    {
        switch (opt)
        {
        case 'b':
            bit_rate = atoi(optarg);
            if (bit_rate != 16000  &&  bit_rate != 24000  &&  bit_rate != 32000  &&  bit_rate != 40000)
            {
                fprintf(stderr, "Invalid bit rate selected. Only 16000, 24000, 32000 and 40000 are valid.\n");
                exit(2);
            }
            itutests = false;
            break;
        case 'L':
            packing = G726_PACKING_LEFT;
            break;
        case 'R':
            packing = G726_PACKING_RIGHT;
            break;
        default:
            //usage();
            exit(2);
        }
    }

    if (itutests)
    {
        itu_compliance_tests();
    }
    else
    {
        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);
        }

        printf("ADPCM packing is %d\n", packing);
        enc_state = g726_init(NULL, bit_rate, G726_ENCODING_LINEAR, packing);
        dec_state = g726_init(NULL, bit_rate, G726_ENCODING_LINEAR, packing);

        while ((frames = sf_readf_short(inhandle, amp, 159)))
        {
            adpcm = g726_encode(enc_state, adpcmdata, amp, frames);
            frames = g726_decode(dec_state, amp, adpcmdata, adpcm);
            sf_writef_short(outhandle, amp, frames);
        }
        if (sf_close_telephony(inhandle))
        {
            printf("    Cannot close audio file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if (sf_close_telephony(outhandle))
        {
            printf("    Cannot close audio file '%s'\n", OUT_FILE_NAME);
            exit(2);
        }
        printf("'%s' transcoded to '%s' at %dbps.\n", IN_FILE_NAME, OUT_FILE_NAME, bit_rate);
        g726_free(enc_state);
        g726_free(dec_state);
    }
    return 0;
}
示例#8
0
文件: g726_tests.c 项目: vir/spandsp
static void itu_compliance_tests(void)
{
    g726_state_t enc_state;
    g726_state_t dec_state;
    int len2;
    int len3;
    int i;
    int test;
    int bad_samples;
    int conditioning_samples;
    int samples;
    int conditioning_adpcm;
    int adpcm;

    len2 = 0;
    conditioning_samples = 0;
    for (test = 0;  itu_test_sets[test].rate;  test++)
    {
        printf("Test %2d: '%s' + '%s'\n"
               "      -> '%s' + '%s'\n"
               "      -> '%s' [%d, %d, %d]\n",
               test,
               itu_test_sets[test].conditioning_pcm_file,
               itu_test_sets[test].pcm_file,
               itu_test_sets[test].conditioning_adpcm_file,
               itu_test_sets[test].adpcm_file,
               itu_test_sets[test].output_file,
               itu_test_sets[test].rate,
               itu_test_sets[test].compression_law,
               itu_test_sets[test].decompression_law);
        if (itu_test_sets[test].compression_law != G726_ENCODING_NONE)
        {
            /* Test the encode side */
            g726_init(&enc_state, itu_test_sets[test].rate, itu_test_sets[test].compression_law, G726_PACKING_NONE);
            if (itu_test_sets[test].conditioning_pcm_file[0])
            {
                conditioning_samples = get_test_vector(itu_test_sets[test].conditioning_pcm_file, xlaw, MAX_TEST_VECTOR_LEN);
                printf("Test %d: Homing %d samples at %dbps\n", test, conditioning_samples, itu_test_sets[test].rate);
            }
            else
            {
                conditioning_samples = 0;
            }
            samples = get_test_vector(itu_test_sets[test].pcm_file, xlaw + conditioning_samples, MAX_TEST_VECTOR_LEN);
            memcpy(itudata, xlaw, samples + conditioning_samples);
            printf("Test %d: Compressing %d samples at %dbps\n", test, samples, itu_test_sets[test].rate);
            len2 = g726_encode(&enc_state, adpcmdata, itudata, conditioning_samples + samples);
        }
        /* Test the decode side */
        g726_init(&dec_state, itu_test_sets[test].rate, itu_test_sets[test].decompression_law, G726_PACKING_NONE);
        if (itu_test_sets[test].conditioning_adpcm_file[0])
        {
            conditioning_adpcm = get_test_vector(itu_test_sets[test].conditioning_adpcm_file, unpacked, MAX_TEST_VECTOR_LEN);
            printf("Test %d: Homing %d octets at %dbps\n", test, conditioning_adpcm, itu_test_sets[test].rate);
        }
        else
        {
            conditioning_adpcm = 0;
        }
        adpcm = get_test_vector(itu_test_sets[test].adpcm_file, unpacked + conditioning_adpcm, MAX_TEST_VECTOR_LEN);
        if (itu_test_sets[test].compression_law != G726_ENCODING_NONE)
        {
            /* Test our compressed version against the reference compressed version */
            printf("Test %d: Compressed data check - %d/%d octets\n", test, conditioning_adpcm + adpcm, len2);
            if (conditioning_adpcm + adpcm == len2)
            {
                for (bad_samples = 0, i = conditioning_samples;  i < len2;  i++)
                {
                    if (adpcmdata[i] != unpacked[i])
                    {
                        bad_samples++;
                        printf("Test %d: Compressed mismatch %d %x %x\n", test, i, adpcmdata[i], unpacked[i]);
                    }
                }
                if (bad_samples > 0)
                {
                    printf("Test failed\n");
                    exit(2);
                }
                printf("Test passed\n");
            }
            else
            {
                printf("Test %d: Length mismatch - ref = %d, processed = %d\n", test, conditioning_adpcm + adpcm, len2);
                exit(2);
            }
        }

        len3 = g726_decode(&dec_state, outdata, unpacked, conditioning_adpcm + adpcm);

        /* Get the output reference data */
        samples = get_test_vector(itu_test_sets[test].output_file, xlaw, MAX_TEST_VECTOR_LEN);
        memcpy(itu_ref, xlaw, samples);
        /* Test our decompressed version against the reference decompressed version */
        printf("Test %d: Decompressed data check - %d/%d samples\n", test, samples, len3 - conditioning_adpcm);
        if (samples == len3 - conditioning_adpcm)
        {
            for (bad_samples = 0, i = 0;  i < len3;  i++)
            {
                if (itu_ref[i] != ((uint8_t *) outdata)[i + conditioning_adpcm])
                {
                    bad_samples++;
                    printf("Test %d: Decompressed mismatch %d %x %x\n", test, i, itu_ref[i], ((uint8_t *) outdata)[i + conditioning_adpcm]);
                }
            }
            if (bad_samples > 0)
            {
                printf("Test failed\n");
                exit(2);
            }
            printf("Test passed\n");
        }
        else
        {
            printf("Test %d: Length mismatch - ref = %d, processed = %d\n", test, samples, len3 - conditioning_adpcm);
            exit(2);
        }
    }

    printf("Tests passed.\n");
}
示例#9
0
int main(int argc, char *argv[])
{
    g726_state_t enc_state;
    g726_state_t dec_state;
    int len2;
    int len3;
    int i;
    int test;
    int bits_per_code;
    int itutests;
    int bit_rate;
    int bad_samples;
    AFfilehandle inhandle;
    AFfilehandle outhandle;
    AFfilesetup filesetup;
    int16_t amp[1024];
    int frames;
    int outframes;
    int conditioning_samples;
    int samples;
    int conditioning_adpcm;
    int adpcm;
    int packing;
    float x;

    i = 1;
    bit_rate = 32000;
    itutests = TRUE;
    packing = G726_PACKING_NONE;
    while (argc > i)
    {
        if (strcmp(argv[i], "-16") == 0)
        {
            bit_rate = 16000;
            itutests = FALSE;
            i++;
        }
        else if (strcmp(argv[i], "-24") == 0)
        {
            bit_rate = 24000;
            itutests = FALSE;
            i++;
        }
        else if (strcmp(argv[i], "-32") == 0)
        {
            bit_rate = 32000;
            itutests = FALSE;
            i++;
        }
        else if (strcmp(argv[i], "-40") == 0)
        {
            bit_rate = 40000;
            itutests = FALSE;
            i++;
        }
        else if (strcmp(argv[i], "-l") == 0)
        {
            packing = G726_PACKING_LEFT;
            i++;
        }
        else if (strcmp(argv[i], "-r") == 0)
        {
            packing = G726_PACKING_RIGHT;
            i++;
        }
        else
        {
            fprintf(stderr, "Unknown parameter %s specified.\n", argv[i]);
            exit(2);
        }
    }

    len2 = 0;
    conditioning_samples = 0;
    if (itutests)
    {
        for (test = 0;  itu_test_sets[test].rate;  test++)
        {
            printf("Test %2d: '%s' + '%s'\n"
                   "      -> '%s' + '%s'\n"
                   "      -> '%s' [%d, %d, %d]\n",
                   test,
                   itu_test_sets[test].conditioning_pcm_file,
                   itu_test_sets[test].pcm_file,
                   itu_test_sets[test].conditioning_adpcm_file,
                   itu_test_sets[test].adpcm_file,
                   itu_test_sets[test].output_file,
                   itu_test_sets[test].rate,
                   itu_test_sets[test].compression_law,
                   itu_test_sets[test].decompression_law);
            switch (itu_test_sets[test].rate)
            {
            case 16000:
                bits_per_code = 2;
                break;
            case 24000:
                bits_per_code = 3;
                break;
            case 32000:
            default:
                bits_per_code = 4;
                break;
            case 40000:
                bits_per_code = 5;
                break;
            }
            if (itu_test_sets[test].compression_law != G726_ENCODING_NONE)
            {
                /* Test the encode side */
                g726_init(&enc_state, itu_test_sets[test].rate, itu_test_sets[test].compression_law, G726_PACKING_NONE);
                if (itu_test_sets[test].conditioning_pcm_file[0])
                {
                    conditioning_samples = get_test_vector(itu_test_sets[test].conditioning_pcm_file, xlaw, MAX_TEST_VECTOR_LEN);
                    printf("Test %d: Homing %d samples at %dbps\n", test, conditioning_samples, itu_test_sets[test].rate);
                }
                else
                {
                    conditioning_samples = 0;
                }
                samples = get_test_vector(itu_test_sets[test].pcm_file, xlaw + conditioning_samples, MAX_TEST_VECTOR_LEN);
                memcpy(itudata, xlaw, samples + conditioning_samples);
                printf("Test %d: Compressing %d samples at %dbps\n", test, samples, itu_test_sets[test].rate);
                len2 = g726_encode(&enc_state, adpcmdata, itudata, conditioning_samples + samples);
            }
            /* Test the decode side */
            g726_init(&dec_state, itu_test_sets[test].rate, itu_test_sets[test].decompression_law, G726_PACKING_NONE);
            if (itu_test_sets[test].conditioning_adpcm_file[0])
            {
                conditioning_adpcm = get_test_vector(itu_test_sets[test].conditioning_adpcm_file, unpacked, MAX_TEST_VECTOR_LEN);
                printf("Test %d: Homing %d octets at %dbps\n", test, conditioning_adpcm, itu_test_sets[test].rate);
            }
            else
            {
                conditioning_adpcm = 0;
            }
            adpcm = get_test_vector(itu_test_sets[test].adpcm_file, unpacked + conditioning_adpcm, MAX_TEST_VECTOR_LEN);
            if (itu_test_sets[test].compression_law != G726_ENCODING_NONE)
            {
                /* Test our compressed version against the reference compressed version */
                printf("Test %d: Compressed data check - %d/%d octets\n", test, conditioning_adpcm + adpcm, len2);
                if (conditioning_adpcm + adpcm == len2)
                {
                    for (bad_samples = 0, i = conditioning_samples;  i < len2;  i++)
                    {
                        if (adpcmdata[i] != unpacked[i])
                        {
                            bad_samples++;
                            printf("Test %d: Compressed mismatch %d %x %x\n", test, i, adpcmdata[i], unpacked[i]);
                        }
                    }
                    if (bad_samples > 0)
                    {
                        printf("Test failed\n");
                        exit(2);
                    }
                    printf("Test passed\n");
                }
                else
                {
                    printf("Test %d: Length mismatch - ref = %d, processed = %d\n", test, conditioning_adpcm + adpcm, len2);
                    exit(2);
                }
            }

            len3 = g726_decode(&dec_state, outdata, unpacked, conditioning_adpcm + adpcm);

            /* Get the output reference data */
            samples = get_test_vector(itu_test_sets[test].output_file, xlaw, MAX_TEST_VECTOR_LEN);
            memcpy(itu_ref, xlaw, samples);
            /* Test our decompressed version against the reference decompressed version */
            printf("Test %d: Decompressed data check - %d/%d samples\n", test, samples, len3 - conditioning_adpcm);
            if (samples == len3 - conditioning_adpcm)
            {
                for (bad_samples = 0, i = 0;  i < len3;  i++)
                {
                    if (itu_ref[i] != ((uint8_t *) outdata)[i + conditioning_adpcm])
                    {
                        bad_samples++;
                        printf("Test %d: Decompressed mismatch %d %x %x\n", test, i, itu_ref[i], ((uint8_t *) outdata)[i + conditioning_adpcm]);
                    }
                }
                if (bad_samples > 0)
                {
                    printf("Test failed\n");
                    exit(2);
                }
                printf("Test passed\n");
            }
            else
            {
                printf("Test %d: Length mismatch - ref = %d, processed = %d\n", test, samples, len3 - conditioning_adpcm);
                exit(2);
            }
        }

        printf("Tests passed.\n");
    }
    else
    {
        if ((inhandle = afOpenFile(IN_FILE_NAME, "r", 0)) == AF_NULL_FILEHANDLE)
        {
            printf("    Cannot open wave file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
        {
            printf("    Unexpected frame size in wave file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
        {
            printf("    Unexpected sample rate in wave file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
        {
            printf("    Unexpected number of channels in wave file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
        {
            fprintf(stderr, "    Failed to create file setup\n");
            exit(2);
        }
        afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
        afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
        afInitFileFormat(filesetup, AF_FILE_WAVE);
        afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);

        outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup);
        if (outhandle == AF_NULL_FILEHANDLE)
        {
            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
            exit(2);
        }

        printf("ADPCM packing is %d\n", packing);
        g726_init(&enc_state, bit_rate, G726_ENCODING_LINEAR, packing);
        g726_init(&dec_state, bit_rate, G726_ENCODING_LINEAR, packing);

        while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, 159)))
        {
            adpcm = g726_encode(&enc_state, adpcmdata, amp, frames);
            frames = g726_decode(&dec_state, amp, adpcmdata, adpcm);
            outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, frames);
        }
        if (afCloseFile(inhandle) != 0)
        {
            printf("    Cannot close wave file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if (afCloseFile(outhandle) != 0)
        {
            printf("    Cannot close wave file '%s'\n", OUT_FILE_NAME);
            exit(2);
        }
        afFreeFileSetup(filesetup);
        printf("'%s' transcoded to '%s' at %dbps.\n", IN_FILE_NAME, OUT_FILE_NAME, bit_rate);
    }
    return 0;
}