FFmpeg音频编码 ---- pcm转aac(使用新版ffmpeg API,亲测可用)
生活随笔
收集整理的這篇文章主要介紹了
FFmpeg音频编码 ---- pcm转aac(使用新版ffmpeg API,亲测可用)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/**
* @projectName 08-01-encode_audio
* @brief 音頻編碼
* 從本地讀取PCM數(shù)據進行AAC編碼
* 1. 輸入PCM格式問題,通過AVCodec的sample_fmts參數(shù)獲取具體的格式支持
* (1)默認的aac編碼器輸入的PCM格式為:AV_SAMPLE_FMT_FLTP
* (2)libfdk_aac編碼器輸入的PCM格式為AV_SAMPLE_FMT_S16.
* 2. 支持的采樣率,通過AVCodec的supported_samplerates可以獲取
* @author Liao Qingfu
* @date 2020-04-15
*/#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作為結束符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_ratesprintf("%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_layoutconst 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_layoutprintf("%s support channel_layout %d\n", codec->name, *p);if (*p == channel_layout)return 1;p++;}return 0;
}static int check_codec( AVCodec *codec, AVCodecContext *codec_ctx)
{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));return 0;}if (!check_sample_rate(codec, codec_ctx->sample_rate)) {fprintf(stderr, "Encoder does not support sample rate %d", codec_ctx->sample_rate);return 0;}if (!check_channel_layout(codec, codec_ctx->channel_layout)) {fprintf(stderr, "Encoder does not support channel layout %lu", codec_ctx->channel_layout);return 0;}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后進行關聯(lián)printf("1 frame_size:%d\n", codec_ctx->frame_size);return 1;
}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 Hzswitch (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_EOFwhile (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;}uint8_t aac_header[7];get_adts_header(ctx, aac_header, pkt->size);size_t len = 0;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ù)據? avcodec_receive_packet第一個調用的就是 av_packet_unref* 所以我們不用手動去釋放,這里有個問題,不能將pkt直接插入到隊列,因為編碼器會釋放數(shù)據* 可以新分配一個pkt, 然后使用av_packet_move_ref轉移pkt對應的buffer*/// av_packet_unref(pkt);}return -1;
}/** 這里只支持2通道的轉換
*/
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 3fltp_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ù)據,在編碼時候如果輸入要為fltp則需要進行轉換* 測試范例:* (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)
{const char* in_pcm_file = "48000_2_f32le.pcm"; // 輸入PCM文件const char* out_aac_file = "f32.aac"; // 輸出的AAC文件enum AVCodecID codec_id = AV_CODEC_ID_AAC;// 1.查找編碼器AVCodec *codec = avcodec_find_encoder(codec_id); // 按ID查找則缺省的aac encode為aacenc.cif (!codec) {fprintf(stderr, "Codec not found\n");exit(1);}// 2.分配內存AVCodecContext *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; //codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;// 3.檢測支持采樣格式支持情況if (!check_codec(codec, codec_ctx)) {exit(1);}// 4.將編碼器上下文和編碼器進行關聯(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); // 決定每次到底送多少個采樣點// 5.打開輸入和輸出文件FILE *infile = fopen(in_pcm_file, "rb");if (!infile) {fprintf(stderr, "Could not open %s\n", in_pcm_file);exit(1);}FILE *outfile = fopen(out_aac_file, "wb");if (!outfile) {fprintf(stderr, "Could not open %s\n", out_aac_file);exit(1);}// 6.分配packetAVPacket *pkt = av_packet_alloc();if (!pkt) {fprintf(stderr, "could not allocate the packet\n");exit(1);}// 7.分配frameAVFrame *frame = av_frame_alloc();if (!frame) {fprintf(stderr, "Could not allocate audio frame\n");exit(1);}/* 每次送多少數(shù)據給編碼器由:* (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);// 8.為frame分配bufferint ret = av_frame_get_buffer(frame, 0);if (ret < 0) {fprintf(stderr, "Could not allocate audio data buffers\n");exit(1);}// 9.循環(huán)讀取數(shù)據// 計算出每一幀的數(shù)據 單個采樣點的字節(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;}// 10.確保該frame可寫, 如果編碼器內部保持了內存參考計數(shù),則需要重新拷貝一個備份 目的是新寫入的數(shù)據和編碼器保存的數(shù)據不能產生沖突ret = av_frame_make_writable(frame);if(ret != 0)printf("av_frame_make_writable failed, ret = %d\n", ret);// 11.填充音頻幀if(AV_SAMPLE_FMT_S16 == frame->format) {// 將讀取到的PCM數(shù)據填充到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ù)據填充到frame去,但要注意格式的匹配, 是planar還是packed都要區(qū)分清楚// 將本地的f32le packed模式的數(shù)據轉為float palanarmemset(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);}// 12.編碼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;}}// 13.沖刷編碼器encode(codec_ctx, NULL, pkt, outfile);// 14.關閉文件fclose(infile);fclose(outfile);// 15.釋放內存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;
}
?https://www.cnblogs.com/vczf/p/13599573.html
?
總結
以上是生活随笔為你收集整理的FFmpeg音频编码 ---- pcm转aac(使用新版ffmpeg API,亲测可用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ffmpeg 从mp4上提取H264的n
- 下一篇: nginx实现,一推多用(rtmp推流,