2.基於FFMPEG將PCM轉為AAC
繼續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;
}