重采樣:將音頻三元組【采樣率 采樣格式 通道數】之中的任何一個或者多個值改變。
一.為什么要進行重采樣?
1.原始音頻數據和編碼器的數據格式不一致
2.播放器要求的和獲取的數據不一致
3.方便運算
二.本次編碼流程
1.了解自己本機麥克風參數,我的切換為44100/16/2;包括麥克風錄音的size可能不一樣,本機windows下錄音的size為88200;
1.ffmpeg獲取麥克風數據
2.ffmpeg對數據進行重采樣(本次三元組無需變換)
3.使用AAC編碼器對重采樣后的數據進行AAC編碼,然后存入.aac文件
4.使用ffplay播放測試
三.整體代碼
#include "customcodex.hpp"int add_samples_to_fifo(AVAudioFifo *fifo,uint8_t **input_data,const int frame_size)
{int ret = 0;int size = 0;printf("add_samples_to_fifo size:%d \n", frame_size);size = av_audio_fifo_size(fifo) + frame_size;ret = av_audio_fifo_realloc(fifo, size);if (ret < 0){printf("Error, Failed to reallocate fifo!\n");return ret;}ret = av_audio_fifo_write(fifo, reinterpret_cast<void **>(input_data), frame_size);if (ret < frame_size){printf("Error, Failed to write data to fifo!\n");return AVERROR_EXIT;}return 0;
}
int read_fifo_and_encode(AVAudioFifo *fifo,AVFormatContext *fmt_ctx,AVCodecContext *c_ctx,AVFrame *frame)
{int ret = 0;const int frame_size = FFMIN(av_audio_fifo_size(fifo),c_ctx->frame_size);printf("read fifo - size : %d ,c_ctx->frame_size : %d\n", av_audio_fifo_size(fifo), c_ctx->frame_size);ret = av_audio_fifo_read(fifo, reinterpret_cast<void **>(frame->data), frame_size);if (ret < frame_size){printf("Error, Failed to read data from fifo!\n");return AVERROR_EXIT;}return 0;
}
int open_coder(AVCodecContext **codec_ctx)
{// 編碼器const AVCodec *codex = avcodec_find_encoder_by_name("libfdk_aac");// codex->capabilities = AV_CODEC_CAP_VARIABLE_FRAME_SIZE;if (!codex){fprintf(stderr, "Codec not found\n");return -1;}// 編碼器上下文*codec_ctx = avcodec_alloc_context3(codex);(*codec_ctx)->sample_fmt = AV_SAMPLE_FMT_S16; // 采樣大小(*codec_ctx)->channel_layout = AV_CH_LAYOUT_STEREO; //(*codec_ctx)->channels = 2; // 聲道數(*codec_ctx)->sample_rate = 44100; // 采樣率(*codec_ctx)->bit_rate = 0; // AAC 128k;AAC HE 64k; AAC_HE V2:32K// codec_ctx->profile = FF_PROFILE_AAC_HE_V2; // 用哪個AACif (avcodec_open2(*codec_ctx, codex, NULL) < 0){fprintf(stderr, "failed avcodec_open2 \n");return -1;}return 0;
}
int encode(AVCodecContext *codec_ctx, AVFrame *avframe, AVPacket *outpkt, FILE *outfile)
{printf("open_coder - codec_ctx->frame_size: %d ,avframe-size:%d\n", codec_ctx->frame_size, avframe->nb_samples);int ret = avcodec_send_frame(codec_ctx, avframe);printf("avcodec_send_frame:%d\n", ret);if (ret < 0){fprintf(stderr, "Error sending frame to encoder\n");return -1;}while (ret >= 0){// 獲取編碼后的音頻數據ret = avcodec_receive_packet(codec_ctx, outpkt);printf("avcodec_receive_packet:%d\n", ret);if (ret < 0){printf("encode - ret: %d \n", ret);// 有數據但是不夠了生成一幀了 沒有數據了if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)return 0;exit(-1);}printf("fwrite - outpkt.size: %d \n", outpkt->size);fwrite(outpkt->data, 1, outpkt->size, outfile);fflush(outfile);}return 0;
}int read_audio()
{int ret = 0;char errors[1024];AVFormatContext *fmt_ctx = NULL;AVDictionary *options = NULL;AVAudioFifo *fifo = nullptr;FILE *outfile = fopen("./out.pcm", "wb+");FILE *outfile_aac = fopen("./out.aac", "wb+");if (outfile == nullptr){printf("filed open out file\n");}AVPacket pkt;av_init_packet(&pkt);int frame_count = 0;const char *devicename = "audio=麥克風 (Realtek(R) Audio)";// 找到采集工具const AVInputFormat *iformat = av_find_input_format("dshow");if (iformat == NULL){printf("AVInputFormat find failed \n");return -1;}// 打開音頻設備ret = avformat_open_input(&fmt_ctx, devicename, iformat, &options);if (ret < 0){av_strerror(ret, errors, 1024);av_log(NULL, AV_LOG_ERROR, "error:%s", errors);return -1;}// 重采樣緩沖區uint8_t **src_data = NULL;int src_linesize = 0;uint8_t **dst_data = NULL;int dst_linesize = 0;// 初始化重采樣上下文SwrContext *swr_ctx = initSwr();if (!swr_ctx){printf("failed init swr\n");return -1;}// 編碼器上下文AVCodecContext *codec_ctx;ret = open_coder(&codec_ctx);if (ret != 0){return -1;}// 音頻輸入數據AVFrame *avframe = initInAvframe();//AVPacket *outpkt = av_packet_alloc();av_init_packet(outpkt);// Create the FIFO buffer for the audio samples to be encoded.fifo = av_audio_fifo_alloc(codec_ctx->sample_fmt, codec_ctx->channels, 1);if (!fifo){printf("Error, Failed to alloc fifo!\n");return -1;}// 88200/2=44100/2=22050// 每次讀取數據大小是88200,16位2個字節,雙聲道// 創建輸入緩沖區initBuffer(&src_data, src_linesize, &dst_data, dst_linesize);av_log(NULL, AV_LOG_DEBUG, "src-size:%d , dst-size:%d\n", src_linesize, dst_linesize);int count = 0;while (1){int frame_size = codec_ctx->frame_size;static bool finished = false;while (av_audio_fifo_size(fifo) < frame_size){printf("av_audio_fifo_size(fifo) :%d , frame_size :%d\n", av_audio_fifo_size(fifo), frame_size);ret = av_read_frame(fmt_ctx, &pkt);printf("av_read_frame-ret : %d\n", ret);if (ret == 0){printf("pkt-size:%d\n", pkt.size);memcpy((void *)src_data[0], (void *)pkt.data, pkt.size);ret = swr_convert(swr_ctx, // 重采樣的上下文dst_data, // 輸出結果緩沖區22050, // 每個通道的采樣數(const uint8_t **)src_data, // 輸入緩沖區22050); // 輸入單個通道的采樣數printf("swr_convert-ret:%d\n", ret);ret = add_samples_to_fifo(fifo, dst_data, 22050);printf("add_samples_to_fifo-ret:%d\n", ret);}if (count >= 20){finished = true;break;}count++;printf("##################### count:%d\n", count);}while (av_audio_fifo_size(fifo) > frame_size || (finished && av_audio_fifo_size(fifo) > 0)){ret = read_fifo_and_encode(fifo, fmt_ctx, codec_ctx, avframe);encode(codec_ctx, avframe, outpkt, outfile_aac);}if (finished){// 強制將編碼器緩沖區中的音頻進行編碼輸出encode(codec_ctx, nullptr, outpkt, outfile_aac);break;}}freePtr(src_data, dst_data, swr_ctx, fmt_ctx, outfile, outfile_aac);return 0;
}
void freePtr(uint8_t **src_data, uint8_t **dst_data, SwrContext *swr_ctx, AVFormatContext *fmt_ctx,FILE *outfile, FILE *outfile_aac)
{// 釋放輸入輸出緩沖區if (src_data){av_freep(&src_data[0]);}av_freep(src_data);if (dst_data){av_freep(&dst_data[0]);}av_freep(dst_data);// 釋放重采樣的上下文swr_free(&swr_ctx);avformat_close_input(&fmt_ctx);fclose(outfile);fclose(outfile_aac);av_log(NULL, AV_LOG_DEBUG, "end");
}
SwrContext *initSwr()
{SwrContext *swr_ctx = swr_alloc();// 設置重采樣參數av_opt_set_int(swr_ctx, "in_channel_count", 2, 0);av_opt_set_int(swr_ctx, "in_sample_rate", 44100, 0); // 輸入采樣率av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);av_opt_set_int(swr_ctx, "out_channel_count", 2, 0);av_opt_set_int(swr_ctx, "out_sample_rate", 44100, 0); // 輸出采樣率av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);swr_init(swr_ctx);return swr_ctx;
}
AVFrame *initInAvframe()
{AVFrame *avframe = av_frame_alloc();avframe->nb_samples = 1024; // 單通道一個音頻的采樣數avframe->format = AV_SAMPLE_FMT_S16;avframe->channel_layout = AV_CH_LAYOUT_STEREO; // AV_CH_LAYOUT_STEREOav_frame_get_buffer(avframe, 0); // 22050*2*2=88200if (!avframe || !avframe->buf){printf("failed get frame buffer\n");return nullptr;}return avframe;
}
void initBuffer(uint8_t ***src_data, int &src_linesize, uint8_t ***dst_data, int &dst_linesize)
{av_samples_alloc_array_and_samples(src_data, // 輸出緩沖區地址&src_linesize, // 緩沖區的大小2, // 通道個數22050, // 單通道采樣個數AV_SAMPLE_FMT_S16, // 采樣格式0);// 創建輸出緩沖區av_samples_alloc_array_and_samples(dst_data, // 輸出緩沖區地址&dst_linesize, // 緩沖區的大小2, // 通道個數22050, // 單通道采樣個數AV_SAMPLE_FMT_S16, // 采樣格式0);
}
四.遇到的問題
1.采樣格式和采樣個數不明確
解決辦法:查看系統聲音設置中,相應設備的輸出格式,一般包含位深和采樣率
采樣個數的話通過打開并讀取音頻數據,可以通過pkt.size打印出來。