2.基于FFMPEG将PCM转为AAC

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011003120/article/details/81144889

继续ffmpeg的学习之路。。。

看了雷博的PCM转AAC代码,理解了一下大致的流程以及逻辑,然后迫不及待的手敲了一遍,然后编译运行,中间遇到了一些问题,便记录一下。

一、综述

PCM转AAC,上网查询了一些资料,了解了PCM和AAC相关的概念以及一些与转码相关的参数。

1).几个参数

下面几个参数是在转码过程中比较重要的:

1.sample_fmt:
音频的格式,有AV_SAMPLE_FMT_S16、AV_SAMPLE_FMT_FLT等不同的类型,这个要根据你的音频文件的具体格式来决定。
同时,在使用最新版的ffmpeg库的时候,发现只支持AV_SAMPLE_FMT_FLT这一种格式,这一点就是遇到一个坑,会在下面描述。。。

2.sample_rate
音频的采样率,有44K, 16K, 8K等不同的参数。

3.channel_layout
音频通道布局,可以通过这个参数来确定声道的个数。

4.channels
通道的个数。有单通道,双通道,。。。等,若是双通道,那么在通过avcodec_encode_audio2接口编码出来的AVPacket中的data[0]是左声道,data[1]是右声道。

2).几个函数

下面几个函数是于音频相关的函数,记录一下。

av_get_channel_layout_nb_channels():用于确定声道的个数
av_samples_get_buffer_size():根据提供的参数来获取buf的大小
avcodec_fill_audio_frame():将缓冲区赋值到AVFream中

3 )遇到的问题

由于我是在linux下编译运行的程序,使用的是最新的ffmpeg库,所以在使用雷博的代码的时候,有些小的地方做了些改动。
1.宏改动
CODEC_CAP_DELAY这个宏在最新的代码中已经不存在了,用AV_CODEC_CAP_DELAY来替代

2.格式问题
将雷博的代码运行后,会出现这个问题:

./test
[aac @ 0x17f5e40] Specified sample format s16 is invalid or not supported
can not open encoder!

提示不支持AV_SAMPLE_FMT_S16这个格式,这就尴尬了,然后便打印一下解码器支持的什么格式:

    pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    printf("---------------->%d\n",pCodec->sample_fmts[0]);

打印出来的值是8,即枚举AV_SAMPLE_FMT_FLTP。

上网百度了下,说是最新的ffmpeg,16年以后,就只支持这一种AAC音频格式,所以想对PCM进行编码需要确定PCM是AV_SAMPLE_FMT_FLTP类型的。

但是,我的测试音频不是AV_SAMPLE_FMT_FLTP格式的,于是便查找有没有别的替代方法,上网查了查,遇到同样问题的人很多,但是解决办法只找到一种,如下:

    pCodec = avcodec_find_encoder_by_name("libfdk_aac");
    //pCodec = avcodec_find_encoder(pCodecCtx->codec_id);

4)其他

根据前一篇YUV420转jpg的思路,主要采用两种方法,第一种就是标准的编码,然后写到AVFormatContext输出文件中去,第二种则是将编码过来的AVPacket里面的数据直接写到文件中去。

二、代码

1)方法1


int flush_encoder(AVFormatContext * fmt_ctx, unsigned int stream_index)
{
    int ret = 0;
    int got_frame = 0;
    AVPacket enc_pkt;

    if (!(fmt_ctx->streams[0]->codec->codec->capabilities & AV_CODEC_CAP_DELAY))
        return 0;

    while(1)
    {
        enc_pkt.data = NULL;
        enc_pkt.size = 0;
        av_init_packet(&enc_pkt);
        ret = avcodec_encode_audio2(fmt_ctx->streams[stream_index]->codec, &enc_pkt, NULL, &got_frame);
        if (ret < 0)
            break;
        if (!got_frame)
        {
            ret = 0;
            break;
        }

        printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);

        ret = av_write_frame(fmt_ctx, &enc_pkt);
        if (ret < 0)
            break;
    }

    return ret;
}

int PCM_2_AAC_1(char* pFile)
{
    if (NULL == pFile) return -1;

    AVFormatContext* pFormatCtx;
    AVStream* audio_st;
    AVCodecContext* pCodecCtx;
    AVCodec* pCodec;
    AVOutputFormat* fmt;
    AVPacket pkt;
    AVFrame * pFrame;

    int ret = 0;
    int size =0;
    unsigned char* frame_buff = NULL;

    FILE* in_file = fopen(pFile, "rb");
    char* out_file = "test_1.aac";

    av_register_all();

    pFormatCtx = avformat_alloc_context();
    fmt = av_guess_format(NULL, out_file, NULL);
    pFormatCtx->oformat = fmt;

    audio_st = avformat_new_stream(pFormatCtx, NULL);
    if (NULL == audio_st)
    {
        printf("init avstream error!\n");
        return -1;
    }

    if (avio_open(&pFormatCtx->pb,  out_file, AVIO_FLAG_READ_WRITE) < 0)
    {
        printf("open output file error!\n");
        return -1;
    }

    pCodecCtx = audio_st->codec;
    pCodecCtx->codec_id = fmt->audio_codec;
    pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
    pCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;
    pCodecCtx->sample_rate = 44100;
    pCodecCtx->channel_layout = AV_CH_LAYOUT_STEREO;
    pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);
    pCodecCtx->bit_rate = 64000;

    pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    //pCodec = avcodec_find_encoder_by_name("libfdk_aac");
    if (NULL == pCodec)
    {
        printf("can not find encoder!\n");
        return -1;
    }

    if (avcodec_open2(pCodecCtx,  pCodec, NULL) < 0)
    {
        printf("can not open encoder!\n");
        return -1;
    }

    pFrame = av_frame_alloc();
    pFrame->nb_samples = pCodecCtx->frame_size;
    pFrame->format = pCodecCtx->sample_fmt;

    size = av_samples_get_buffer_size(NULL, pCodecCtx->channels, pFrame->nb_samples, pFrame->format, 1);
    frame_buff = (char *)av_malloc(size);
    avcodec_fill_audio_frame(pFrame, pCodecCtx->channels, pCodecCtx->sample_fmt, frame_buff, size, 1);

    avformat_write_header(pFormatCtx, NULL);

    av_new_packet(&pkt,  size);

    int i = 0;
    int framenum = 1000;
    int got_frame = 0;

    for (i = 0; i < framenum; i++)
    {
        if (fread(frame_buff, 1, size, in_file) <= 0)
        {
            printf("failed to read raw data!\n");
            return -1;
        }
        else if (feof(in_file))
        {
            break;
        }

        pFrame->data[0] = frame_buff;

        pFrame->pts = i * 100;

        got_frame = 0;

        ret = avcodec_encode_audio2(pCodecCtx, &pkt, pFrame, &got_frame);
        if (ret < 0)
        {
            printf("failure to encode!\n");
            return -1;
        }
        if (got_frame == 1)
        {
            printf("success to encode 1 frame!\tsize:%5d\n", pkt.size);
            pkt.stream_index = audio_st->index;
            ret = av_write_frame(pFormatCtx, &pkt);
            av_free_packet(&pkt);
        }
    }

    ret = flush_encoder(pFormatCtx,0);
    if (ret < 0)
    {
        printf("flush encoder error!\n");
        return -1;
    }

    av_write_trailer(pFormatCtx);

    avcodec_close(audio_st->codec);
    av_free(pFrame);
    av_free(frame_buff);
    avio_close(pFormatCtx->pb);
    avformat_free_context(pFormatCtx);
    fclose(in_file);

    return 0;
}

2)方法2

int PCM_2_AAC_2(char* pFile)
{
    if (NULL == pFile)
        return -1;

    AVCodecContext* pCodecCtx = NULL;
    AVCodec * pCodec = NULL;
    AVPacket pkt;
    AVFrame* pFrame = NULL;

    int size = 0;
    char* frame_buff = NULL;
    //int cnt = 0;
    int got_frame = 0;
    int ret = -1;
    FILE* out_file = NULL;
    FILE* in_file = NULL;

    in_file = fopen(pFile, "rb");
    if (NULL == in_file)
    {
        printf("open in file error!\n");
        ret = -1;
        return ret;
    }

    out_file =  fopen("test.aac", "wb");
    if (NULL == out_file)
    {
        printf("open out file error!\n");
        goto ERR_3;
    }

    avcodec_register_all();

    pCodecCtx = avcodec_alloc_context3(NULL);
    if (NULL == pCodecCtx)
    {
        printf("alloc AVCodecContext failure!\n");
        ret = -1;
        goto ERR_2;
    }

    pCodecCtx->codec_id = AV_CODEC_ID_AAC;
    pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
    pCodecCtx->sample_fmt = AV_SAMPLE_FMT_FLT;//AV_SAMPLE_FMT_S16;
    pCodecCtx->sample_rate = 44100;
    pCodecCtx->channel_layout = AV_CH_LAYOUT_STEREO;
    pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);
    pCodecCtx->bit_rate = 64000;

    //pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    pCodec = avcodec_find_encoder_by_name("libfdk_aac");
    if (NULL == pCodec)
    {
        printf("can not find encoder!\n");
        ret = -1;
        goto ERR_1;
    }

    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
    {
        printf("open encoder error!\n");
        ret = -1;
        goto ERR_1;
    }

    pFrame = av_frame_alloc();
    pFrame->nb_samples = pCodecCtx->frame_size;
    pFrame->format = pCodecCtx->sample_fmt;

    size = av_samples_get_buffer_size(NULL, pCodecCtx->channels, pFrame->nb_samples, pFrame->format, 1);
    frame_buff = (char *)av_malloc(size);

    avcodec_fill_audio_frame(pFrame, pCodecCtx->channels, pCodecCtx->sample_fmt,(const uint8_t*)frame_buff, size, 1);

    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;
    memset(frame_buff, 0, size);
    int i =0;

    while((fread(frame_buff, 1, size, in_file)) > 0)
    {
        pFrame->pts = i++;
        if (avcodec_encode_audio2(pCodecCtx, &pkt, pFrame, &got_frame) >= 0)
        {
            if (got_frame)
            {
                fwrite(pkt.data, 1, pkt.size, out_file);
                av_free_packet(&pkt);
            }
            else
            {
                continue;
            }
        }
        else
        {
            printf("encode audio error!\n");
            ret = -1;
            goto ERR_1;
        }

        memset(frame_buff,0, size);
    }

    //fresh
    pkt.data = NULL;
    pkt.size = 0;
    while(1)
    {
            if (avcodec_encode_audio2(pCodecCtx, &pkt, NULL, &got_frame) >= 0)
            {
            if (got_frame)
            {
                fwrite(pkt.data, 1, pkt.size, out_file);
                av_free_packet(&pkt);
            }
            else
            {
                break;
            }           
        }
        else
        {
            printf("fresh encode audio error!\n");
            ret = -1;
            goto ERR_1;
        }
    }

    ERR_1:
    av_freep(&pFrame->data[0]);
    //av_freep(frame_buff);
    av_frame_free(pFrame);
    avcodec_close(pCodecCtx);
    avcodec_free_context(&pCodecCtx);
    ERR_2:
    fclose(out_file);
    ERR_3:
    fclose(in_file);

    return ret;
}

猜你喜欢

转载自blog.csdn.net/u011003120/article/details/81144889