日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

音频编码案例

發布時間:2024/4/11 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 音频编码案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

音頻編碼案例


目錄

  • FFmpeg流程
  • PCM樣本格式
  • PCM編碼成AAC案例

  • 1. FFmpeg流程

  • 從本地?件讀取PCM數據進?AAC格式編碼,然后將編碼后的AAC數據存儲到本地?件。
  • 示例的流程如下所示
  • 1. 關鍵函數說明:

  • avcodec_find_encoder:根據指定的AVCodecID查找注冊的編碼器。
  • avcodec_alloc_context3:為AVCodecContext分配內存。
  • avcodec_open2:打開編碼器。
  • avcodec_send_frame:將AVFrame?壓縮數據給編碼器。
  • avcodec_receive_packet:獲取到編碼后的AVPacket數據,收到的packet需要??釋放內存。
  • av_frame_get_buffer: 為?頻或視頻幀分配新的buffer。在調?這個函數之前,必須在AVFame上設置好以下屬性:format(視頻為像素格式,?頻為樣本格式)、nb_samples(樣本個數,針對?頻)、channel_layout(通道類型,針對?頻)、width/height(寬?,針對視頻)。
  • av_frame_make_writable:確保AVFrame是可寫的,使?av_frame_make_writable()的問題是,在最壞的情況下,它會在您使?encode再次更改整個輸?frame之前復制它. 如果frame不可寫,av_frame_make_writable()將分配新的緩沖區,并復制這個輸?input frame數據,避免和編碼器需要緩存該幀時造成沖突。
  • av_samples_fill_arrays 填充?頻幀
  • 2. 對于 flush encoder的操作:

  • 編碼器通常的沖洗?法:調??次 avcodec_send_frame(NULL)(返回成功),然后不停調?
    avcodec_receive_packet() 直到其返回 AVERROR_EOF,取出所有緩存幀, avcodec_receive_packet() 返回 AVERROR_EOF 這?次是沒有有效數據的,僅僅獲取到?個結束標志

  • 2. PCM樣本格式

  • PCM(Pulse Code Modulation,脈沖編碼調制)?頻數據是未經壓縮的?頻采樣數據裸流,它是由模擬信號經過采樣、量化、編碼轉換成的標準數字?頻數據。
    描述PCM數據的6個參數:
  • Sample Rate : 采樣頻率。8kHz(電話)、44.1kHz(CD)、48kHz(DVD)。
  • Sample Size : 量化位數。通常該值為16-bit。
  • Number of Channels : 通道個數。常?的?頻有?體聲(stereo)和單聲道(mono)兩種類型,?體聲包含左聲道和右聲道。另外還有環繞?體聲等其它不太常?的類型。
  • Sign : 表示樣本數據是否是有符號位,?如??字節表示的樣本數據,有符號的話表示范圍為-128 ~
    127,?符號是0 ~ 255。有符號位16bits數據取值范圍為-32768~32767。
  • Byte Ordering : 字節序。字節序是little-endian還是big-endian。通常均為little-endian。字節序說明?第4節。
  • Integer Or Floating Point : 整形或浮點型。?多數格式的PCM樣本數據使?整形表示,?在?些對精度要求?的應???,使?浮點類型表示PCM樣本數據(浮點數 float值域為 [-1.0, 1.0])
  • 推薦的PCM數據播放?具:
  • Audacity:?款免費開源的跨平臺?頻處理軟件。
  • Adobe Auditon。導?原始數據,打開的時候需要選擇采樣率、格式和字節序
  • ffplay, 使?示例如下:
  • //播放格式為f32le,雙聲道,采樣頻率48000Hz的PCM數據 ffplay -f f32le -ac 2 -ar 48000 pcm_audio

    1. FFmpeg?持的PCM數據格式

  • 使?ffmpeg -formats命令,獲取ffmpeg?持的?視頻格式,其中我們可以找到?持的PCM格式。
  • DE alaw PCM A-lawDE f32be PCM 32-bit floating-point big-endianDE f32le PCM 32-bit floating-point little-endianDE f64be PCM 64-bit floating-point big-endianDE f64le PCM 64-bit floating-point little-endianDE mulaw PCM mu-lawDE s16be PCM signed 16-bit big-endianDE s16le PCM signed 16-bit little-endianDE s24be PCM signed 24-bit big-endianDE s24le PCM signed 24-bit little-endianDE s32be PCM signed 32-bit big-endianDE s32le PCM signed 32-bit little-endianDE s8 PCM signed 8-bitDE u16be PCM unsigned 16-bit big-endianDE u16le PCM unsigned 16-bit little-endianDE u24be PCM unsigned 24-bit big-endianDE u24le PCM unsigned 24-bit little-endianDE u32be PCM unsigned 32-bit big-endianDE u32le PCM unsigned 32-bit little-endianDE u8 PCM unsigned 8-bitDE vidc PCM Archimedes VIDC
  • s是有符號,u是?符號,f是浮點數。
  • be是?端,le是?端
  • 2. FFmpeg中Packed和Planar的PCM數據區別

  • FFmpeg中?視頻數據基本上都有Packed和Planar兩種存儲?式,對于雙聲道?頻來說,Packed?式為兩個聲道的數據交錯存儲;Planar?式為兩個聲道分開存儲。假設?個L/R為?個采樣點,數據存儲的?式如下所示:
  • Packed: L R L R L R L R Planar: L L L L ... R R R R...
    1. packed格式
    AV_SAMPLE_FMT_U8, ///< unsigned 8 bits AV_SAMPLE_FMT_S16, ///< signed 16 bits AV_SAMPLE_FMT_S32, ///< signed 32 bits AV_SAMPLE_FMT_FLT, ///< float AV_SAMPLE_FMT_DBL, ///< double
  • 只能保存在AVFrame的uint8_t *data[0];
  • ?頻保持格式如下:
  • LRLRLR ...
    2. planar格式
  • planar為FFmpeg內部存儲?頻使?的采樣格式,所有的Planar格式后?都有字?P標識。
  • AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar AV_SAMPLE_FMT_FLTP, ///< float, planar AV_SAMPLE_FMT_DBLP, ///< double, planar AV_SAMPLE_FMT_S64, ///< signed 64 bits 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默認的AAC編碼器不?持AV_SAMPLE_FMT_S16格式的編碼,只?持AV_SAMPLE_FMT_FLTP,這種格式是按平?存儲,樣點是float類型,所謂平?也就是:每個聲道單獨存儲,?如左聲道存儲到data[0]中,右聲道存儲到data[1]中。
  • FFmpeg?頻解碼后和編碼前的數據是存放在AVFrame結構中的。
  • Packed格式,frame.data[0]或frame.extended_data[0]包含所有的?頻數據中。
  • Planar格式,frame.data[i]或者frame.extended_data[i]表示第i個聲道的數據(假設聲道0是第?個), AVFrame.data數組??固定為8,如果聲道數超過8,需要從frame.extended_data獲取聲道數據
  • 3. 補充說明
  • Planar模式是ffmpeg內部存儲模式,我們實際使?的?頻?件都是Packed模式的。
  • FFmpeg解碼不同格式的?頻輸出的?頻采樣格式不是?樣。測試發現,其中AAC解碼輸出的數據為浮點型的 AV_SAMPLE_FMT_FLTP 格式,MP3解碼輸出的數據為 AV_SAMPLE_FMT_S16P 格式(使?的mp3?件為16位深)。具體采樣格式可以查看解碼后的AVFrame中的format成員或編解碼器的AVCodecContext中的sample_fmt成員。
  • Planar或者Packed模式直接影響到保存?件時寫?件的操作,操作數據的時候?定要先檢測?頻采樣格式
  • 3. PCM字節序

  • big endian是指低地址存放最?有效字節(MSB,Most Significant Bit),?little endian則是低地址存放最低有效字節(LSB,Least Significant Bit)。
  • 下??圖像加以說明。?如數字0x12345678在兩種不同字節序CPU中的存儲順序如下所示:
  • Big Endian 低地址 ?地址 | 12 | 34 | 56 | 78 | Little Endian 低地址 ?地址 | 78 | 56 | 34 | 12 |
  • 所有?絡協議都是采?big endian的?式來傳輸數據的。所以也把big endian?式稱之為?絡字節序。當兩臺采?不同字節序的主機通信時,在發送數據之前都必須經過字節序的轉換成為?絡字節序后再進?傳輸。

  • 3. PCM編碼成AAC實戰

    /** * @projectName 08-01-encode_audio * @brief 音頻編碼 * 從本地讀取PCM數據進行AAC編碼 * 1. 輸入PCM格式問題,通過AVCodec的sample_fmts參數獲取具體的格式支持 * (1)默認的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作為結束符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; }/* 檢測該編碼器是否支持該采樣率, 該函數只是作參考 */ 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 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;}/* 是否需要釋放數據? avcodec_receive_packet第一個調用的就是 av_packet_unref* 所以我們不用手動去釋放,這里有個問題,不能將pkt直接插入到隊列,因為編碼器會釋放數據* 可以新分配一個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數據,在編碼時候如果輸入要為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) {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;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; // 強制使用 libfdk_aaccodec_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);elseprintf("default codec name: %s\n", "aac");if (force_codec == 0) { // 沒有強制設置編碼器codec = avcodec_find_encoder(codec_id); // 按ID查找則缺省的aac encode為aacenc.c} else {// 按名字查找指定的encode,對應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后進行關聯printf("1 frame_size:%d\n", codec_ctx->frame_size);/* 將編碼器上下文和編碼器進行關聯 */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);}/* 每次送多少數據給編碼器由:* (1)frame_size(每幀單個通道的采樣點數);* (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);}// 計算出每一幀的數據 單個采樣點的字節 * 通道數目 * 每幀采樣點數量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可寫, 如果編碼器內部保持了內存參考計數,則需要重新拷貝一個備份目的是新寫入的數據和編碼器保存的數據不能產生沖突*/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數據填充到frame去,但要注意格式的匹配, 是planar還是packed都要區分清楚ret = av_samples_fill_arrays(frame->data, frame->linesize,pcm_buf, frame->channels,frame->nb_samples, frame->format, 0);} else {// 將讀取到的PCM數據填充到frame去,但要注意格式的匹配, 是planar還是packed都要區分清楚// 將本地的f32le packed模式的數據轉為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);}// 設置ptspts += 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; }

    總結

    以上是生活随笔為你收集整理的音频编码案例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。