• FFmepg--音频编码流程--pcm编码为aac


    基本概念

    从本地⽂件读取PCM数据进⾏AAC格式编码,然后将编码后的AAC数据存储到本地⽂件。

    PCM样本格式:未经压缩的⾳频采样数据裸流
    参数:

    • Sample Rate : 采样频率
    • Sample Size : 量化位数
    • Number of Channels : 通道个数
    • Sign : 表示样本数据是否是有符号位
    • Byte Ordering : 字节序
    • Integer Or Floating Point : 整形或浮点型

    流程

    请添加图片描述

    api

    • avcodec_find_encoder:根据指定的AVCodecID查找注册的编码器
    • avcodec_alloc_context3:为AVCodecContext分配内存
    • avcodec_open2:打开编码器
    • avcodec_send_frame:将AVFrame⾮压缩数据给编码器
    • avcodec_receive_packet:获取到编码后的AVPacket数据
    • 设置AVFrame参数: format 、nb_samples、channel_layout、width/height
    • av_frame_get_buffer: 为⾳频或视频帧分配新的buffer
    • av_frame_make_writable:确保AVFrame是可写的
    • av_samples_fill_arrays 填充⾳频帧

    code(核心部分)

    static int encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
    {
        int ret;
    
        /* send the frame for encoding */
        ret = avcodec_send_frame(ctx, frame);
        if (ret < 0) {
            fprintf(stderr, "Error sending the frame to the encoder\n");
            return -1;
        }
        while (ret >= 0) {
            ret = avcodec_receive_packet(ctx, pkt);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                return 0;
            } else if (ret < 0) {
                fprintf(stderr, "Error encoding audio frame\n");
                return -1;
            }
    
            size_t len = 0;
            if((ctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
                // 需要额外的adts header写入
                uint8_t aac_header[7];
                get_adts_header(ctx, aac_header, pkt->size);
                len = fwrite(aac_header, 1, 7, output);
                if(len != 7) {
                    fprintf(stderr, "fwrite aac_header failed\n");
                    return -1;
                }
            }
            len = fwrite(pkt->data, 1, pkt->size, output);
            if(len != pkt->size) {
                fprintf(stderr, "fwrite aac data failed\n");
                return -1;
            }
           
        }
        return -1;
    }
    
    int main(int argc, char **argv)
    {
        char *in_pcm_file = NULL;
        char *out_aac_file = NULL;
        FILE *infile = NULL;
        FILE *outfile = NULL;
        const AVCodec *codec = NULL;
        AVCodecContext *codec_ctx= NULL;
        AVFrame *frame = NULL;
        AVPacket *pkt = NULL;
        int ret = 0;
        int force_codec = 0;     // 强制使用指定的编码
        char *codec_name = NULL;
    
        in_pcm_file = argv[1];      // 输入PCM文件
        out_aac_file = argv[2];     // 输出的AAC文件
    
        enum AVCodecID codec_id = AV_CODEC_ID_AAC;
    
        codec_ctx = avcodec_alloc_context3(codec);
        if (!codec_ctx) {
            fprintf(stderr, "Could not allocate audio codec context\n");
            exit(1);
        }
        codec_ctx->codec_id = codec_id;
        codec_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
        codec_ctx->bit_rate = 128*1024;
        codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
        codec_ctx->sample_rate    = 48000; //48000;
        codec_ctx->channels       = av_get_channel_layout_nb_channels(codec_ctx->channel_layout);
        codec_ctx->profile = FF_PROFILE_AAC_LOW;    //
    
        if(strcmp(codec->name, "aac") == 0) {
            codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
        } else if(strcmp(codec->name, "libfdk_aac") == 0) {
            codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;
        } else {
            codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
        }
        /* 检测支持采样格式支持情况 */
        check_sample_fmt(codec, codec_ctx->sample_fmt);
        check_sample_rate(codec, codec_ctx->sample_rate);
        check_channel_layout(codec, codec_ctx->channel_layout);
        
        codec_ctx->flags = AV_CODEC_FLAG_GLOBAL_HEADER;  //ffmpeg默认的aac是不带adts,而fdk_aac默认带adts,这里我们强制不带
        /* 将编码器上下文和编码器进行关联 */
        avcodec_open2(codec_ctx, codec, NULL);
        
        // 打开输入和输出文件
        infile = fopen(in_pcm_file, "rb");
        outfile = fopen(out_aac_file, "wb");
    
        /* packet for holding encoded output */
        pkt = av_packet_alloc();
    
        /* frame containing input raw audio */
        frame = av_frame_alloc();
      
        // 设置frame参数
        frame->nb_samples     = codec_ctx->frame_size;
        frame->format         = codec_ctx->sample_fmt;
        frame->channel_layout = codec_ctx->channel_layout;
        frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
       
        
        /* 为frame分配buffer */
        av_frame_get_buffer(frame, 0);
       
        // 计算出每一帧的数据 单个采样点的字节 * 通道数目 * 每帧采样点数量
        int frame_bytes = av_get_bytes_per_sample(frame->format) \
                * frame->channels \
                * frame->nb_samples;
        
        uint8_t *pcm_buf = (uint8_t *)malloc(frame_bytes);
        uint8_t *pcm_temp_buf = (uint8_t *)malloc(frame_bytes);
        int64_t pts = 0;
        
        printf("start enode\n");
        for (;;) {
            memset(pcm_buf, 0, frame_bytes);
            size_t read_bytes = fread(pcm_buf, 1, frame_bytes, infile);
            if(read_bytes <= 0) {
                printf("read file finish\n");
                break;
            }
    
            /* 确保该frame可写, 如果编码器内部保持了内存参考计数,则需要重新拷贝一个备份
                目的是新写入的数据和编码器保存的数据不能产生冲突
            */
            av_frame_make_writable(frame);
          
            if(AV_SAMPLE_FMT_S16 == frame->format) {
                // 将读取到的PCM数据填充到frame去
                ret = av_samples_fill_arrays(frame->data, frame->linesize,
                                       pcm_buf, frame->channels,
                                       frame->nb_samples, frame->format, 0);
            } else {
                // 将读取到的PCM数据填充到frame去
                // 将本地的f32le packed模式的数据转为float palanar
                memset(pcm_temp_buf, 0, frame_bytes);
                f32le_convert_to_fltp((float *)pcm_buf, (float *)pcm_temp_buf, frame->nb_samples);
                ret = av_samples_fill_arrays(frame->data, frame->linesize,
                                       pcm_temp_buf, frame->channels,
                                       frame->nb_samples, frame->format, 0);
            }
            // 设置pts
            pts += frame->nb_samples;
            frame->pts = pts;       // 使用采样率作为pts的单位,具体换算成秒 pts*1/采样率
            ret = encode(codec_ctx, frame, pkt, outfile);
            if(ret < 0) {
                printf("encode failed\n");
                break;
            }
        }
    
        /* 冲刷编码器 */
        encode(codec_ctx, NULL, pkt, outfile);
    
        // 关闭文件
        fclose(infile);
        fclose(outfile);
    
        // 释放内存
        if(pcm_buf) {
            free(pcm_buf);
        }
        if (pcm_temp_buf) {
            free(pcm_temp_buf);
        }
        av_frame_free(&frame);
        av_packet_free(&pkt);
        avcodec_free_context(&codec_ctx);
        printf("main finish, please enter Enter and exit\n");
        getchar();
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
  • 相关阅读:
    virtualBox虚拟机安装多个+主机访问虚拟机+虚拟机访问外网配置
    2021年下半年软件设计师下午真题答案及解析(二)
    读书笔记:《所有权的终结:数字时代的财产保护》
    Windows10中使用VS2022和Cmake编译构建C++开源日志库-spdlog
    神州三层交换机DHCPv6中继服务配置
    Bug是如何产生的?
    手撕IP核系列——Xilinx FIFO IP核-异步FIFO
    Java中什么是异常?
    【go零基础】go-zero从零基础学习到实战教程 - 1项目表设计
    【UE5】 ListView使用DataTable数据的蓝图方法
  • 原文地址:https://blog.csdn.net/qq_43538607/article/details/136685532