FFmpeg AAC编码

通过FFmpeg进行AAC编码步骤如下:
1.通过名字libfdk_aac找到AAC编码器。
2.创建编码上下文环境,并设置上下文的参数(比特率、采样大小、采样率、声道数等信息)。
3.创建并打开输出文件,打开编码器,对帧数据进行编码处理(实际应用中原始帧数据是来自麦克风或解码后的音频,这里是人工添加的模拟数据)。
4.将帧数据编码后的包写进文件。

/*ACC编码*/

#include 
#include 
#include 

#include 

#include 
#include 
#include 
#include 

// 判断编码器是否支持某个采样格式(采样大小)
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
{
    const enum AVSampleFormat *p = codec->sample_fmts;

    while (*p != AV_SAMPLE_FMT_NONE) {
        if (*p == sample_fmt)
            return 1;
        p++;
    }
    return 0;
}

// 从编码器中获取采样率(从编码器所支持的采样率中获取与44100最接近的采样率)
static int select_sample_rate(const AVCodec *codec)
{
    const int *p;
    int best_samplerate = 0;

    if (!codec->supported_samplerates)
        return 44100;

    p = codec->supported_samplerates;
    while (*p) {
        if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))
            best_samplerate = *p;
        p++;
    }
    return best_samplerate;
}

/* select layout with the highest channel count */
static int select_channel_layout(const AVCodec *codec)
{
    const uint64_t *p;
    uint64_t best_ch_layout = 0;
    int best_nb_channels   = 0;

    if (!codec->channel_layouts)
        return AV_CH_LAYOUT_STEREO;

    p = codec->channel_layouts;
    while (*p) {
        int nb_channels = av_get_channel_layout_nb_channels(*p);

        if (nb_channels > best_nb_channels) {
            best_ch_layout    = *p;
            best_nb_channels = nb_channels;
        }
        p++;
    }
    return best_ch_layout;
}

int main(int argc, char **argv)
{
    const char *filename; // 输出的文件名,从参数中获取
    const AVCodec *codec; // 编码器
    AVCodecContext *c= NULL; // 编码上下文环境
    AVFrame *frame; // 原始帧
    AVPacket pkt; // 编码后的包数据
    int i, j, k, ret, got_output;
    FILE *f; // 输出文件 
    uint16_t *samples; 
    float t, tincr;

    if (argc <= 1) {
        fprintf(stderr, "Usage: %s \n", argv[0]);
        return 0;
    }
    filename = argv[1];


    // 找到aac的编码器
    // codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
    codec = avcodec_find_encoder_by_name("libfdk_aac");
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }

    // 创建编码器的上下文
    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate audio codec context\n");
        exit(1);
    }

    // 设置上下文格式
    // 比特率
    c->bit_rate = 64000;
    // 采样大小为16位
    c->sample_fmt = AV_SAMPLE_FMT_S16;
    if (!check_sample_fmt(codec, c->sample_fmt)) {
        fprintf(stderr, "Encoder does not support sample format %s",
                av_get_sample_fmt_name(c->sample_fmt));
        exit(1);
    }

    // 设置采样率,这里通过函数获取,也可以直接写具体值
    c->sample_rate    = select_sample_rate(codec);
    // channel_layout为各个通道存储顺序,可以据此算出声道数。设置声道数也可以直接写具体值
    c->channel_layout = select_channel_layout(codec);
    c->channels       = av_get_channel_layout_nb_channels(c->channel_layout);

    // 打开编码器
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }

    // 打开输出文件
    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }

    // 初始化原始帧
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate audio frame\n");
        exit(1);
    }

    // 设置帧的参数
    // 一个frame可能包含多个音频帧,nb_samples记录音频帧的数量
    frame->nb_samples     = c->frame_size;
    // frame的格式和声道信息
    frame->format         = c->sample_fmt;
    frame->channel_layout = c->channel_layout;

    /* allocate the data buffers */
    ret = av_frame_get_buffer(frame, 0);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate audio data buffers\n");
        exit(1);
    }

    // 实际应用中原始数据是来自麦克风或解码后的音频,这里是人工添加的模拟数据
    t = 0;
    tincr = 2 * M_PI * 440.0 / c->sample_rate;
    for (i = 0; i < 200; i++) {
        av_init_packet(&pkt);
        pkt.data = NULL; // packet data will be allocated by the encoder
        pkt.size = 0;

        /* make sure the frame is writable -- makes a copy if the encoder
         * kept a reference internally */
        ret = av_frame_make_writable(frame);
        if (ret < 0)
            exit(1);
        samples = (uint16_t*)frame->data[0];

        for (j = 0; j < c->frame_size; j++) {
            samples[2*j] = (int)(sin(t) * 10000);

            for (k = 1; k < c->channels; k++)
                samples[2*j + k] = samples[2*j];
            t += tincr;
        }

        // 音频编码
        ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding audio frame\n");
            exit(1);
        }

        // got_output用于标记是否参数了数据包(因为可能是多个frame压缩参数一个pkt,所以不是每一个frame编码时都会产生数据包)
        // 编码完后将编码后的数据写入文件
        if (got_output) {
            fwrite(pkt.data, 1, pkt.size, f);
            av_packet_unref(&pkt);
        }
    }

    /* get the delayed frames */
    for (got_output = 1; got_output; i++) {
        ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            exit(1);
        }

        if (got_output) {
            fwrite(pkt.data, 1, pkt.size, f);
            av_packet_unref(&pkt);
        }
    }
    fclose(f);

    av_frame_free(&frame);
    avcodec_free_context(&c);

    return 0;
}

你可能感兴趣的:(FFmpeg AAC编码)