音視頻流媒體開發(fā)【十七】音頻編碼實戰(zhàn)

音視頻流媒體開發(fā)-目錄

FFmpeg流程

從本地?件讀取PCM數(shù)據(jù)進(jìn)?AAC格式編碼绝骚,然后將編碼后的AAC數(shù)據(jù)存儲到本地?件蛆橡。

示例的流程如下所示邪意。

鍵函數(shù)說明:
  • avcodec_find_encoder:根據(jù)指定的AVCodecID查找注冊的編碼器。
  • avcodec_alloc_context3:為AVCodecContext分配內(nèi)存丽蝎。
  • avcodec_open2:打開編碼器猎拨。
  • avcodec_send_frame:將AVFrame?壓縮數(shù)據(jù)給編碼器。
  • avcodec_receive_packet:獲取到編碼后的AVPacket數(shù)據(jù),收到的packet需要??釋放內(nèi)存迟几。
  • av_frame_get_buffer: 為?頻或視頻幀分配新的buffer消请。在調(diào)?這個函數(shù)之前,必須在AVFame上設(shè)置好以下屬性:format(視頻為像素格式类腮,?頻為樣本格式)臊泰、nb_samples(樣本個數(shù),針對?頻)蚜枢、channel_layout(通道類型缸逃,針對?頻)、width/height(寬?厂抽,針對視頻)需频。
  • av_frame_make_writable:確保AVFrame是可寫的,使?av_frame_make_writable()的問題是筷凤,在最壞的情況下昭殉,它會在您使?encode再次更改整個輸?frame之前復(fù)制它. 如果frame不可寫,av_frame_make_writable()將分配新的緩沖區(qū)藐守,并復(fù)制這個輸?input frame數(shù)據(jù)挪丢,避免和編碼器需要緩存該幀時造成沖突。
  • av_samples_fill_arrays 填充?頻幀

對于 flush encoder的操作:

編碼器通常的沖洗?法:調(diào)??次 avcodec_send_frame(NULL)(返回成功)卢厂,然后不停調(diào)?avcodec_receive_packet() 直到其返回 AVERROR_EOF乾蓬,取出所有緩存幀,avcodec_receive_packet() 返回 AVERROR_EOF 這?次是沒有有效數(shù)據(jù)的慎恒,僅僅獲取到?個結(jié)束標(biāo)志

PCM樣本格式

PCM(Pulse Code Modulation任内,脈沖編碼調(diào)制)?頻數(shù)據(jù)是未經(jīng)壓縮的?頻采樣數(shù)據(jù)裸流,它是由模擬信號經(jīng)過采樣融柬、量化死嗦、編碼轉(zhuǎn)換成的標(biāo)準(zhǔn)數(shù)字?頻數(shù)據(jù)。

描述PCM數(shù)據(jù)的6個參數(shù):

  1. Sample Rate : 采樣頻率丹鸿。8kHz(電話)越走、44.1kHz(CD)棚品、48kHz(DVD)靠欢。
  2. Sample Size : 量化位數(shù)。通常該值為16-bit铜跑。
  3. Number of Channels : 通道個數(shù)门怪。常?的?頻有?體聲(stereo)和單聲道(mono)兩種類型,?體聲包含左聲道和右聲道锅纺。另外還有環(huán)繞?體聲等其它不太常?的類型掷空。
  4. Sign : 表示樣本數(shù)據(jù)是否是有符號位,?如??字節(jié)表示的樣本數(shù)據(jù),有符號的話表示范圍為-128 ~ 127坦弟,?符號是0 ~ 255护锤。有符號位16bits數(shù)據(jù)取值范圍為-32768~32767。
  5. Byte Ordering : 字節(jié)序酿傍。字節(jié)序是little-endian還是big-endian烙懦。通常均為little-endian。字節(jié)序說明?第4節(jié)赤炒。
  6. Integer Or Floating Point : 整形或浮點型氯析。?多數(shù)格式的PCM樣本數(shù)據(jù)使?整形表示,?在?些對精度要求?的應(yīng)???莺褒,使?浮點類型表示PCM樣本數(shù)據(jù)(浮點數(shù) float值域為 [-1.0, 1.0])掩缓。
推薦的PCM數(shù)據(jù)播放?具:

ffplay, 使?示例如下:

//播放格式為f32le,雙聲道遵岩,采樣頻率48000Hz的PCM數(shù)據(jù)
ffplay -f f32le -ac 2 -ar 48000 pcm_audio
  • Audacity:?款免費開源的跨平臺?頻處理軟件你辣。
  • Adobe Auditon。導(dǎo)?原始數(shù)據(jù)尘执,打開的時候需要選擇采樣率绢记、格式和字節(jié)序。

FFmpeg?持的PCM數(shù)據(jù)格式

使?ffmpeg -formats命令正卧,獲取ffmpeg?持的?視頻格式蠢熄,其中我們可以找到?持的PCM格式。

1 DE alaw PCM A-law
2 DE f32be PCM 32-bit floating-point big-endian
3 DE f32le PCM 32-bit floating-point little-endian
4 DE f64be PCM 64-bit floating-point big-endian
5 DE f64le PCM 64-bit floating-point little-endian
6 DE mulaw PCM mu-law
7 DE s16be PCM signed 16-bit big-endian
8 DE s16le PCM signed 16-bit little-endian
9 DE s24be PCM signed 24-bit big-endian
10 DE s24le PCM signed 24-bit little-endian
11 DE s32be PCM signed 32-bit big-endian
12 DE s32le PCM signed 32-bit little-endian
13 DE s8 PCM signed 8-bit
14 DE u16be PCM unsigned 16-bit big-endian
15 DE u16le PCM unsigned 16-bit little-endian
16 DE u24be PCM unsigned 24-bit big-endian
17 DE u24le PCM unsigned 24-bit little-endian
18 DE u32be PCM unsigned 32-bit big-endian
19 DE u32le PCM unsigned 32-bit little-endian
20 DE u8 PCM unsigned 8-bit

s是有符號炉旷,u是?符號签孔,f是浮點數(shù)。
be是?端窘行,le是?端饥追。

FFmpeg中Packed和Planar的PCM數(shù)據(jù)區(qū)別

FFmpeg中?視頻數(shù)據(jù)基本上都有Packed和Planar兩種存儲?式,對于雙聲道?頻來說罐盔,Packed?式為兩個聲道的數(shù)據(jù)交錯存儲但绕;Planar?式為兩個聲道分開存儲。假設(shè)?個L/R為?個采樣點惶看,數(shù)據(jù)存儲的?式如下所示:

  • Packed: L R L R L R L R
  • Planar: L L L L ... R R R R...

packed格式

1 AV_SAMPLE_FMT_U8, ///< unsigned 8 bits
2 AV_SAMPLE_FMT_S16, ///< signed 16 bits
3 AV_SAMPLE_FMT_S32, ///< signed 32 bits
4 AV_SAMPLE_FMT_FLT, ///< float
5 AV_SAMPLE_FMT_DBL, ///< double

只能保存在AVFrame的uint8_t *data[0];

?頻保持格式如下:
LRLRLR ...

planar格式

planar為FFmpeg內(nèi)部存儲?頻使?的采樣格式捏顺,所有的Planar格式后?都有字?P標(biāo)識。

1 AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar
2 AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar
3 AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar
4 AV_SAMPLE_FMT_FLTP, ///< float, planar
5 AV_SAMPLE_FMT_DBLP, ///< double, planar
6 AV_SAMPLE_FMT_S64, ///< signed 64 bits
7 AV_SAMPLE_FMT_S64P, ///< signed 64 bits, planar

plane 0: LLLLLLLLLLLLLLLLLLLLLLLLLL...
plane 1: RRRRRRRRRRRRRRRRRRRR....

plane 0對于uint8_t *data[0];
plane 1對于uint8_t *data[1];

FFmpeg默認(rèn)的AAC編碼器不?持AV_SAMPLE_FMT_S16格式的編碼纬黎,只?持AV_SAMPLE_FMT_FLTP幅骄,這種格式是按平?存儲,樣點是float類型本今,所謂平?也就是:每個聲道單獨存儲拆座,?如左聲道存儲到data[0]中主巍,右聲道存儲到data[1]中。

FFmpeg?頻解碼后和編碼前的數(shù)據(jù)是存放在AVFrame結(jié)構(gòu)中的挪凑。

  • Packed格式孕索,frame.data[0]或frame.extended_data[0]包含所有的?頻數(shù)據(jù)中。
  • Planar格式躏碳,frame.data[i]或者frame.extended_data[i]表示第i個聲道的數(shù)據(jù)(假設(shè)聲道0是第?個), AVFrame.data數(shù)組??固定為8檬果,如果聲道數(shù)超過8,需要從frame.extended_data獲取聲道數(shù)據(jù)唐断。
補(bǔ)充說明
  • Planar模式是ffmpeg內(nèi)部存儲模式选脊,我們實際使?的?頻?件都是Packed模式的。
  • FFmpeg解碼不同格式的?頻輸出的?頻采樣格式不是?樣脸甘。測試發(fā)現(xiàn)恳啥,其中AAC解碼輸出的數(shù)據(jù)為浮點型的 AV_SAMPLE_FMT_FLTP 格式,MP3解碼輸出的數(shù)據(jù)為 AV_SAMPLE_FMT_S16P 格式(使?的mp3?件為16位深)丹诀。具體采樣格式可以查看解碼后的AVFrame中的format成員或編解碼器的AVCodecContext中的sample_fmt成員钝的。
  • Planar或者Packed模式直接影響到保存?件時寫?件的操作,操作數(shù)據(jù)的時候?定要先檢測?頻采樣格式铆遭。

PCM字節(jié)序

談到字節(jié)序的問題硝桩,必然牽涉到兩?CPU派系。那就是Motorola的PowerPC系列CPU和Intel的x86系列CPU枚荣。PowerPC系列采?big endian?式存儲數(shù)據(jù)碗脊,?x86系列則采?little endian?式存儲數(shù)據(jù)。那么究竟什么是big endian橄妆,什么?是little endian衙伶?

big endian是指低地址存放最?有效字節(jié)(MSB,Most Significant Bit)害碾,?little endian則是低地址存放最低有效字節(jié)(LSB矢劲,Least Significant Bit)。

下??圖像加以說明慌随。?如數(shù)字0x12345678在兩種不同字節(jié)序CPU中的存儲順序如下所示:

Big Endian
低地址 ?地址
----------------------------------------------------------------------------->
| 12 | 34 | 56 | 78 |
Little Endian
低地址 ?地址
----------------------------------------------------------------------------->
| 78 | 56 | 34 | 12 |

所有?絡(luò)協(xié)議都是采?big endian的?式來傳輸數(shù)據(jù)的芬沉。所以也把big endian?式稱之為?絡(luò)字節(jié)序。當(dāng)兩臺采?不同字節(jié)序的主機(jī)通信時阁猜,在發(fā)送數(shù)據(jù)之前都必須經(jīng)過字節(jié)序的轉(zhuǎn)換成為?絡(luò)字節(jié)序后再進(jìn)?傳輸丸逸。

問題:

avcodec_receive_packet 不同的返回值代表什么含義;讀取的packet如果要放到隊列??那應(yīng)該怎么放到隊列蹦漠?

案例:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.c
win32 {
INCLUDEPATH += $$PWD/ffmpeg-4.2.1-win32-dev/include
LIBS += $$PWD/ffmpeg-4.2.1-win32-dev/lib/avformat.lib   \
        $$PWD/ffmpeg-4.2.1-win32-dev/lib/avcodec.lib    \
        $$PWD/ffmpeg-4.2.1-win32-dev/lib/avdevice.lib   \
        $$PWD/ffmpeg-4.2.1-win32-dev/lib/avfilter.lib   \
        $$PWD/ffmpeg-4.2.1-win32-dev/lib/avutil.lib     \
        $$PWD/ffmpeg-4.2.1-win32-dev/lib/postproc.lib   \
        $$PWD/ffmpeg-4.2.1-win32-dev/lib/swresample.lib \
        $$PWD/ffmpeg-4.2.1-win32-dev/lib/swscale.lib
}
/**
* @projectName   encode_audio
* @brief         音頻編碼
*               從本地讀取PCM數(shù)據(jù)進(jìn)行AAC編碼
*           1. 輸入PCM格式問題椭员,通過AVCodec的sample_fmts參數(shù)獲取具體的格式支持
*           (1)默認(rèn)的aac編碼器輸入的PCM格式為:AV_SAMPLE_FMT_FLTP
*           (2)libfdk_aac編碼器輸入的PCM格式為AV_SAMPLE_FMT_S16.
*           2. 支持的采樣率,通過AVCodec的supported_samplerates可以獲取
*/

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

#include <libavcodec/avcodec.h>

#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/frame.h>
#include <libavutil/samplefmt.h>
#include <libavutil/opt.h>

/* 檢測該編碼器是否支持該采樣格式 */
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) { // 通過AV_SAMPLE_FMT_NONE作為結(jié)束符
        if (*p == sample_fmt)
            return 1;
        p++;
    }
    return 0;
}

/* 檢測該編碼器是否支持該采樣率 */
static int check_sample_rate(const AVCodec *codec, const int sample_rate)
{
    const int *p = codec->supported_samplerates;
    while (*p != 0)  {// 0作為退出條件笛园,比如libfdk-aacenc.c的aac_sample_rates
        printf("%s support %dhz\n", codec->name, *p);
        if (*p == sample_rate)
            return 1;
        p++;
    }
    return 0;
}

/* 檢測該編碼器是否支持該采樣率, 該函數(shù)只是作參考 */
static int check_channel_layout(const AVCodec *codec, const uint64_t channel_layout)
{
    // 不是每個codec都給出支持的channel_layout
    const uint64_t *p = codec->channel_layouts;
    if(!p) {
        printf("the codec %s no set channel_layouts\n", codec->name);
        return 1;
    }
    while (*p != 0) { // 0作為退出條件隘击,比如libfdk-aacenc.c的aac_channel_layout
        printf("%s support channel_layout %d\n", codec->name, *p);
        if (*p == channel_layout)
            return 1;
        p++;
    }
    return 0;
}


static void get_adts_header(AVCodecContext *ctx, uint8_t *adts_header, int aac_length)
{
    uint8_t freq_idx = 0;    //0: 96000 Hz  3: 48000 Hz 4: 44100 Hz
    switch (ctx->sample_rate) {
        case 96000: freq_idx = 0; break;
        case 88200: freq_idx = 1; break;
        case 64000: freq_idx = 2; break;
        case 48000: freq_idx = 3; break;
        case 44100: freq_idx = 4; break;
        case 32000: freq_idx = 5; break;
        case 24000: freq_idx = 6; break;
        case 22050: freq_idx = 7; break;
        case 16000: freq_idx = 8; break;
        case 12000: freq_idx = 9; break;
        case 11025: freq_idx = 10; break;
        case 8000: freq_idx = 11; break;
        case 7350: freq_idx = 12; break;
        default: freq_idx = 4; break;
    }
    uint8_t chanCfg = ctx->channels;
    uint32_t frame_length = aac_length + 7;
    adts_header[0] = 0xFF;
    adts_header[1] = 0xF1;
    adts_header[2] = ((ctx->profile) << 6) + (freq_idx << 2) + (chanCfg >> 2);
    adts_header[3] = (((chanCfg & 3) << 6) + (frame_length  >> 11));
    adts_header[4] = ((frame_length & 0x7FF) >> 3);
    adts_header[5] = (((frame_length & 7) << 5) + 0x1F);
    adts_header[6] = 0xFC;
}
/*
*
*/
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;
    }

    /* read all the available output packets (in general there may be any number of them */
    // 編碼和解碼都是一樣的,都是send 1次研铆,然后receive多次, 直到AVERROR(EAGAIN)或者AVERROR_EOF
    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;
        printf("ctx->flags:0x%x & AV_CODEC_FLAG_GLOBAL_HEADER:0x%x, name:%s\n",ctx->flags, ctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER, ctx->codec->name);
        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;
        }
        /* 是否需要釋放數(shù)據(jù)? avcodec_receive_packet第一個調(diào)用的就是 av_packet_unref
        * 所以我們不用手動去釋放埋同,這里有個問題,不能將pkt直接插入到隊列棵红,因為編碼器會釋放數(shù)據(jù)
        * 可以新分配一個pkt, 然后使用av_packet_move_ref轉(zhuǎn)移pkt對應(yīng)的buffer
        */
        // av_packet_unref(pkt);
    }
    return -1;
}

/*
 * 這里只支持2通道的轉(zhuǎn)換
*/
void f32le_convert_to_fltp(float *f32le, float *fltp, int nb_samples) {
    float *fltp_l = fltp;   // 左通道
    float *fltp_r = fltp + nb_samples;   // 右通道
    for(int i = 0; i < nb_samples; i++) {
        fltp_l[i] = f32le[i*2];     // 0 1   - 2 3
        fltp_r[i] = f32le[i*2+1];   // 可以嘗試注釋左聲道或者右聲道聽聽聲音
    }
}
/*
 * 提取測試文件:
 * (1)s16格式:ffmpeg -i buweishui.aac -ar 48000 -ac 2 -f s16le 48000_2_s16le.pcm
 * (2)flt格式:ffmpeg -i buweishui.aac -ar 48000 -ac 2 -f f32le 48000_2_f32le.pcm
 *      ffmpeg只能提取packed格式的PCM數(shù)據(jù)凶赁,在編碼時候如果輸入要為fltp則需要進(jìn)行轉(zhuǎn)換
 * 測試范例:
 * (1)48000_2_s16le.pcm libfdk_aac.aac libfdk_aac  // 如果編譯的時候沒有支持fdk aac則提示找不到編碼器
 * (2)48000_2_f32le.pcm aac.aac aac // 我們這里只測試aac編碼器,不測試fdkaac
*/
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;     // 強(qiáng)制使用指定的編碼
    char *codec_name = NULL;

    if (argc < 3) {
        fprintf(stderr, "Usage: %s <input_file out_file [codec_name]>, argc:%d\n",
                argv[0], argc);
        return 0;
    }
    in_pcm_file = argv[1];      // 輸入PCM文件
    out_aac_file = argv[2];     // 輸出的AAC文件

    enum AVCodecID codec_id = AV_CODEC_ID_AAC;

    if(4 == argc) {
        if(strcmp(argv[3], "libfdk_aac") == 0) {
            force_codec = 1;     // 強(qiáng)制使用 libfdk_aac
            codec_name = "libfdk_aac";
        } else if(strcmp(argv[3], "aac") == 0) {
            force_codec = 1;
            codec_name = "aac";
        }
    }
    if(force_codec)
        printf("force codec name: %s\n", codec_name);
    else
        printf("default codec name: %s\n", "aac");

    if(force_codec == 0) { // 沒有強(qiáng)制設(shè)置編碼器
        codec = avcodec_find_encoder(codec_id); // 按ID查找則缺省的aac encode為aacenc.c
    } else {
        // 按名字查找指定的encode,對應(yīng)AVCodec的name字段
        codec = avcodec_find_encoder_by_name(codec_name);
    }
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }

    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;
    }

    /* 檢測支持采樣格式支持情況 */
    if (!check_sample_fmt(codec, codec_ctx->sample_fmt)) {
        fprintf(stderr, "Encoder does not support sample format %s",
                av_get_sample_fmt_name(codec_ctx->sample_fmt));
        exit(1);
    }
    if (!check_sample_rate(codec, codec_ctx->sample_rate)) {
        fprintf(stderr, "Encoder does not support sample rate %d", codec_ctx->sample_rate);
        exit(1);
    }
    if (!check_channel_layout(codec, codec_ctx->channel_layout)) {
        fprintf(stderr, "Encoder does not support channel layout %lu", codec_ctx->channel_layout);
        exit(1);
    }

    printf("\n\nAudio encode config\n");
    printf("bit_rate:%ldkbps\n", codec_ctx->bit_rate/1024);
    printf("sample_rate:%d\n", codec_ctx->sample_rate);
    printf("sample_fmt:%s\n", av_get_sample_fmt_name(codec_ctx->sample_fmt));
    printf("channels:%d\n", codec_ctx->channels);
    // frame_size是在avcodec_open2后進(jìn)行關(guān)聯(lián)
    printf("1 frame_size:%d\n", codec_ctx->frame_size);
    codec_ctx->flags = AV_CODEC_FLAG_GLOBAL_HEADER;  //ffmpeg默認(rèn)的aac是不帶adts逆甜,而fdk_aac默認(rèn)帶adts虱肄,這里我們強(qiáng)制不帶
    /* 將編碼器上下文和編碼器進(jìn)行關(guān)聯(lián) */
    if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }
    printf("2 frame_size:%d\n\n", codec_ctx->frame_size); // 決定每次到底送多少個采樣點
    // 打開輸入和輸出文件
    infile = fopen(in_pcm_file, "rb");
    if (!infile) {
        fprintf(stderr, "Could not open %s\n", in_pcm_file);
        exit(1);
    }
    outfile = fopen(out_aac_file, "wb");
    if (!outfile) {
        fprintf(stderr, "Could not open %s\n", out_aac_file);
        exit(1);
    }

    /* packet for holding encoded output */
    pkt = av_packet_alloc();
    if (!pkt)
    {
        fprintf(stderr, "could not allocate the packet\n");
        exit(1);
    }

    /* frame containing input raw audio */
    frame = av_frame_alloc();
    if (!frame)
    {
        fprintf(stderr, "Could not allocate audio frame\n");
        exit(1);
    }
    /* 每次送多少數(shù)據(jù)給編碼器由:
     *  (1)frame_size(每幀單個通道的采樣點數(shù));
     *  (2)sample_fmt(采樣點格式);
     *  (3)channel_layout(通道布局情況);
     * 3要素決定
     */
    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);
    printf("frame nb_samples:%d\n", frame->nb_samples);
    printf("frame sample_fmt:%d\n", frame->format);
    printf("frame channel_layout:%lu\n\n", frame->channel_layout);
    /* 為frame分配buffer */
    ret = av_frame_get_buffer(frame, 0);
    if (ret < 0)
    {
        fprintf(stderr, "Could not allocate audio data buffers\n");
        exit(1);
    }
    // 計算出每一幀的數(shù)據(jù) 單個采樣點的字節(jié) * 通道數(shù)目 * 每幀采樣點數(shù)量
    int frame_bytes = av_get_bytes_per_sample(frame->format) \
            * frame->channels \
            * frame->nb_samples;
    printf("frame_bytes %d\n", frame_bytes);
    uint8_t *pcm_buf = (uint8_t *)malloc(frame_bytes);
    if(!pcm_buf) {
        printf("pcm_buf malloc failed\n");
        return 1;
    }
    uint8_t *pcm_temp_buf = (uint8_t *)malloc(frame_bytes);
    if(!pcm_temp_buf) {
        printf("pcm_temp_buf malloc failed\n");
        return 1;
    }
    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;
//            fseek(infile,0,SEEK_SET);
//            fflush(outfile);
//            continue;
        }

        /* 確保該frame可寫, 如果編碼器內(nèi)部保持了內(nèi)存參考計數(shù),則需要重新拷貝一個備份
            目的是新寫入的數(shù)據(jù)和編碼器保存的數(shù)據(jù)不能產(chǎn)生沖突
        */
        ret = av_frame_make_writable(frame);
        if(ret != 0)
            printf("av_frame_make_writable failed, ret = %d\n", ret);

        if(AV_SAMPLE_FMT_S16 == frame->format) {
            // 將讀取到的PCM數(shù)據(jù)填充到frame去交煞,但要注意格式的匹配, 是planar還是packed都要區(qū)分清楚
            ret = av_samples_fill_arrays(frame->data, frame->linesize,
                                   pcm_buf, frame->channels,
                                   frame->nb_samples, frame->format, 0);
        } else {
            // 將讀取到的PCM數(shù)據(jù)填充到frame去咏窿,但要注意格式的匹配, 是planar還是packed都要區(qū)分清楚
            // 將本地的f32le packed模式的數(shù)據(jù)轉(zhuǎn)為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);
        }

        // 設(shè)置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);

    // 關(guān)閉文件
    fclose(infile);
    fclose(outfile);

    // 釋放內(nèi)存
    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;
}

設(shè)置輸入輸出文件


運行
直接雙擊生成的acc文件素征,看是否能播放

ffmpeg默認(rèn)的aac編碼器集嵌,默認(rèn)編譯出來的每幀數(shù)據(jù)都不帶adts,但lib_fdk aac默認(rèn)是帶了adts header御毅,而且此時codec_ctx->flags的值都為0根欧。這樣我們沒法判斷是否需要自己額外寫入adts,因此我們在設(shè)置編碼的時候可以直接將odec_ctx->flags = AV_CODEC_FLAG_GLOBAL_HEADER; 大家都不帶adts

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末端蛆,一起剝皮案震驚了整個濱河市凤粗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌今豆,老刑警劉巖侈沪,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異晚凿,居然都是意外死亡亭罪,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門歼秽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來应役,“玉大人,你說我怎么就攤上這事燥筷÷嵯椋” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵肆氓,是天一觀的道長袍祖。 經(jīng)常有香客問我,道長谢揪,這世上最難降的妖魔是什么蕉陋? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任捐凭,我火速辦了婚禮,結(jié)果婚禮上凳鬓,老公的妹妹穿的比我還像新娘茁肠。我一直安慰自己,他們只是感情好缩举,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布垦梆。 她就那樣靜靜地躺著,像睡著了一般仅孩。 火紅的嫁衣襯著肌膚如雪托猩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天辽慕,我揣著相機(jī)與錄音京腥,去河邊找鬼。 笑死鼻百,一個胖子當(dāng)著我的面吹牛绞旅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播温艇,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼因悲,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了勺爱?” 一聲冷哼從身側(cè)響起晃琳,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎琐鲁,沒想到半個月后卫旱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡围段,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年顾翼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奈泪。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡适贸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出涝桅,到底是詐尸還是另有隱情拜姿,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布冯遂,位于F島的核電站蕊肥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏蛤肌。R本人自食惡果不足惜壁却,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一批狱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧儒洛,春花似錦精耐、人聲如沸狼速。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽向胡。三九已至恼蓬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間僵芹,已是汗流浹背处硬。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留拇派,地道東北人荷辕。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像件豌,于是被迫代替她去往敵國和親疮方。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容