g711/aac/pcm

1、faac

example

./configure --prefix=$(pwd)/_install

make

make install

 

/* aac_encode.c */
#include <stdio.h>
#include <faac.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char **argv)
{
    unsigned long sampleRate = 8000;
    unsigned int numChannels = 1;
    unsigned long inputSample = 0;
    unsigned long maxOutputBytes = 0;
    faacEncHandle encoder;
    faacEncConfigurationPtr config;
    FILE *rfile = NULL;
    FILE *wfile = NULL;
    int16_t *pcm_input = NULL;
    uint8_t *aac_output = NULL;
    int readcount = 0;
    int writecount = 0;

    encoder = faacEncOpen(sampleRate, numChannels, &inputSample, &maxOutputBytes);
    config = faacEncGetCurrentConfiguration(encoder);
    config->aacObjectType = MAIN;
    config->mpegVersion = MPEG4;
    config->useLfe = 0;
    config->useTns = 1;
    config->allowMidside = 1;
    /*RAW_STREAM=0, ADTS_STREAM=1*/
    config->outputFormat = 1;  
    config->bitRate = sampleRate;
    config->inputFormat = FAAC_INPUT_16BIT;
    faacEncSetConfiguration(encoder, config);

    printf("sampleRate:%ld, numChannels:%d, inputSample:%ld, maxOutputBytes:%ld\n", 
            sampleRate, numChannels, inputSample, maxOutputBytes);

    if (argv[1]) {
        rfile = fopen(argv[1], "rb");
    } else {
        printf("try to open input.pcm\n");
        rfile = fopen("input.pcm", "rb");
    }

    if (!rfile) {
        printf("open error\n");
        goto end;
    }

    if (argv[2]) {
        wfile = fopen(argv[2], "wb");
    } else {
        printf("try to open output.aac\n");
        wfile = fopen("output.aac", "wb");
    }

    if (!wfile) {
        printf("open error\n");
        goto end;
    }

    pcm_input = (int16_t *)malloc(inputSample * sizeof(int16_t));
    aac_output = (uint8_t *)malloc(maxOutputBytes * sizeof(uint8_t));

    /* encode */
    while (1) {
        int readlen = 0;
        int ret = 0;

        readlen = fread(pcm_input, sizeof(int16_t), inputSample, rfile);

        ret = faacEncEncode(encoder, (int32_t *)pcm_input, readlen, aac_output, maxOutputBytes);

        if (ret > 0) {
            fwrite(aac_output, sizeof(uint8_t), ret, wfile);
        } else if (ret < 0) {
            printf("encode error\n");
            break;
        }

        readcount += readlen * 2;
        writecount += ret;

        if (!readlen && !ret) {
            printf("encode complete, from %d bytes to %d bytes\n", readcount, writecount);
            break;
        }
    }

    free(pcm_input);
    free(aac_output);

  end:
    if (wfile) fclose(wfile);
    if (rfile) fclose(rfile);
    faacEncClose(encoder);
    return 0;
}

 

Makefile

 

APP = main

INCLUDE = \
-I ./faac/include

LIB = \
-L ./faac/lib/

SRC  = main.c

CFLAGS =

LDFLAGS = -lfaac

out:
    gcc $(SRC) -o $(APP) $(LIB) $(INCLUDE) $(CFLAGS) $(LDFLAGS)

clean:
    rm -rf *o *.out $(APP)

 

2、faac/faad <--- > pcm

https://www.audiocoding.com/downloads.html

 

3、g711 <---> pcm

https://github.com/phoenixZZZ/G711_EncodecAndDecodec

https://github.com/Wenstery/G711AudioStream

https://github.com/escrichov/G711

 

codec-for-audio-in-G72X-G711-G723-G726-G729

https://github.com/twstx1/codec-for-audio-in-G72X-G711-G723-G726-G729/blob/master/G711_G721_G723/encode.c

 

非常好的实例,已经用在产品里了,感谢作者!

An ANSI C library for encoding/decoding using the A-law and u-Law.

https://github.com/dystopiancode/pcm-g711

https://blog.csdn.net/szfhy/article/details/52448906

 

posted @ 2018-08-15 16:09  dong1  阅读(770)  评论(0编辑  收藏  举报