faac在嵌入式环境下的交叉编译

1,下载源码:wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz

版本根据自己需要查询

2,./configure CXX=arm-linux-gnueabihf-xxxxx-g++ CC=arm-linux-gnueabihf-xxxxx-gcc --prefix=./faac-1.28/_install --host=arm-linux --with-mp4v2=no

3,make && make install

示例代码:

#include <faac.h>
#include <stdio.h>
#include <stdlib.h>

#define SAMPLE_RATE 16000  // 16kHz采样率
#define CHANNELS 1         // 单声道
#define BIT_RATE 32000     // 32kbps比特率

int main() {
    // 打开PCM输入文件
    FILE* pcmFile = fopen("./test_16k.pcm", "rb");
    if (!pcmFile) {
        fprintf(stderr, "无法打开PCM文件\n");
        return 1;
    }

    // 创建AAC输出文件
    FILE* aacFile = fopen("./output.aac", "wb");
    if (!aacFile) {
        fprintf(stderr, "无法创建AAC文件\n");
        fclose(pcmFile);
        return 1;
    }

    // 初始化FAAC编码器
    unsigned long inputSamples = 0;
    unsigned long maxOutputBytes = 0;
    faacEncHandle encoder = faacEncOpen(SAMPLE_RATE, CHANNELS, &inputSamples, &maxOutputBytes);
    if (!encoder) {
        fprintf(stderr, "FAAC编码器初始化失败\n");
        fclose(pcmFile);
        fclose(aacFile);
        return 1;
    }

    // 配置编码器参数
    faacEncConfigurationPtr config = faacEncGetCurrentConfiguration(encoder);
    config->inputFormat = FAAC_INPUT_16BIT;
    config->mpegVersion = MPEG4;
    config->aacObjectType = LOW;
    config->bitRate = BIT_RATE;
    if (!faacEncSetConfiguration(encoder, config)) {
        fprintf(stderr, "FAAC编码器配置失败\n");
        faacEncClose(encoder);
        fclose(pcmFile);
        fclose(aacFile);
        return 1;
    }

    // 分配缓冲区
    short* inputBuffer = (short*)malloc(inputSamples * sizeof(short));
    unsigned char* outputBuffer = (unsigned char*)malloc(maxOutputBytes);

    // 编码循环
    size_t totalEncoded = 0;
    while (1) {
        size_t read = fread(inputBuffer, sizeof(short), inputSamples, pcmFile);
        if (read == 0) break;

        int bytesEncoded = faacEncEncode(encoder, inputBuffer, read, outputBuffer, maxOutputBytes);
        if (bytesEncoded > 0) {
            fwrite(outputBuffer, 1, bytesEncoded, aacFile);
            totalEncoded += bytesEncoded;
        }
    }

    // 释放资源
    free(inputBuffer);
    free(outputBuffer);
    faacEncClose(encoder);
    fclose(pcmFile);
    fclose(aacFile);

    printf("转换完成,共编码 %zu 字节数据\n", totalEncoded);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值