1.播放多媒體文件步驟
通常情況下,我們下載的視頻文件如MP4,MKV、FLV等都屬於封裝格式,就是把音視頻數據按照相應的規范,打包成一個文本文件。我們可以使用MediaInfo這個工具查看媒體文件的相關信息。
所以當我們播放一個媒體文件時,通常需要經過以下幾個步驟
①解封裝(Demuxing):就是將輸入的封裝格式的數據,分離成為音頻流壓縮編碼數據和視頻流壓縮編碼數據。封裝格式種類很多,例如MP4,MKV,RMVB,TS,FLV,AVI等等,它的作用就是將已經壓縮編碼的視頻數據和音頻數據按照一定的格式放到一起。例如,FLV格式的數據,經過解封裝操作后,輸出H.264編碼的視頻碼流和AAC編碼的音頻碼流。
②解碼(Decode):就是將視頻/音頻壓縮編碼數據,解碼成為非壓縮的視頻/音頻原始數據。音頻的壓縮編碼標准包含AAC,MP3等,視頻的壓縮編碼標准則包含H.264,MPEG2等。解碼是整個系統中最重要也是最復雜的一個環節。通過解碼,壓縮編碼的視頻數據輸出成為非壓縮的顏色數據,例如YUV、RGB等等;壓縮編碼的音頻數據輸出成為非壓縮的音頻抽樣數據,例如PCM數據。
③音視頻同步:就是根據解封裝模塊處理過程中獲取到的參數信息,同步解碼出來的音頻和視頻數據,並將音視頻頻數據送至系統的顯卡和聲卡播放出來(Render)。
2.FFMPEG音視頻解碼
通過上面對媒體文件播放步驟的了解,我們在解碼多媒體文件的時候需要經過兩個步驟,即解封裝(Demuxing)和解碼(Decode)。下面就來看一下FFMPEG解碼媒體文件的時候是怎樣做這兩個步驟的。
在使用FFMPEG解碼媒體文件之前,我們首先需要注冊FFMPEG的各種組件,通過
av_register_all();
AVFormatContext *pFormatCtx = avformat_alloc_context(); avformat_open_input(&pFormatCtx,input_cstr,NULL,NULL); avformat_find_stream_info(pFormatCtx,NULL);
來打開一個媒體文件,並獲得媒體文件封裝格式的上下文。之后我們就可以通過遍歷定義在libavformat/avformat.h里保存着媒體文件中封裝的流數量的nb_streams在媒體文件中分離出音視頻流。
分離出音視頻流之后,就可以對音視頻流分別進行解碼了,這里先以視頻解碼為例,我們可以遍歷AVStream找到codec_type為AVMEDIA_TYPE_VIDEO的的AVStream即為視頻流的索引值。
//視頻解碼,需要找到視頻對應的AVStream所在pFormatCtx->streams的索引位置 int video_stream_idx = -1; int i = 0; for(; i < pFormatCtx->nb_streams;i++){ //根據類型判斷,是否是視頻流 if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){ video_stream_idx = i; break; } }
然后我們就可以通過AVStream來找到對應的AVCodecContext即編解碼器的上下文。之后就可以通過這個上下文,使用
avcodec_find_decoder()
來找到對應的解碼器,再通過
avcodec_open2()
來打開解碼器,AVFormatContext、AVStream、AVCodecContext、AVCodec四者之間的關系為
打開解碼器之后,就可以循環的將一幀待解碼的數據AVPacket送給
avcodec_decode_video2()
進行解碼,解碼之后的數據存放在AVFrame里面。
3.示例代碼
3.1.視頻解碼
#include <stdio.h> #include <stdlib.h> //編碼 #include "libavcodec/avcodec.h" //封裝格式處理 #include "libavformat/avformat.h" //像素處理 #include "libswscale/swscale.h" int main() { //獲取輸入輸出文件名 const char *input = "test.mp4"; const char *output = "test.yuv"; //1.注冊所有組件 av_register_all(); //封裝格式上下文,統領全局的結構體,保存了視頻文件封裝格式的相關信息 AVFormatContext *pFormatCtx = avformat_alloc_context(); //2.打開輸入視頻文件 if (avformat_open_input(&pFormatCtx, input, NULL, NULL) != 0) { printf("%s","無法打開輸入視頻文件"); return; } //3.獲取視頻文件信息 if (avformat_find_stream_info(pFormatCtx,NULL) < 0) { printf("%s","無法獲取視頻文件信息"); return; } //獲取視頻流的索引位置 //遍歷所有類型的流(音頻流、視頻流、字幕流),找到視頻流 int v_stream_idx = -1; int i = 0; //number of streams for (; i < pFormatCtx->nb_streams; i++) { //流的類型 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { v_stream_idx = i; break; } } if (v_stream_idx == -1) { printf("%s","找不到視頻流\n"); return; } //只有知道視頻的編碼方式,才能夠根據編碼方式去找到解碼器 //獲取視頻流中的編解碼上下文 AVCodecContext *pCodecCtx = pFormatCtx->streams[v_stream_idx]->codec; //4.根據編解碼上下文中的編碼id查找對應的解碼 AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL) { printf("%s","找不到解碼器\n"); return; } //5.打開解碼器 if (avcodec_open2(pCodecCtx,pCodec,NULL)<0) { printf("%s","解碼器無法打開\n"); return; } //輸出視頻信息 printf("視頻的文件格式:%s",pFormatCtx->iformat->name); printf("視頻時長:%d", (pFormatCtx->duration)/1000000); printf("視頻的寬高:%d,%d",pCodecCtx->width,pCodecCtx->height); printf("解碼器的名稱:%s",pCodec->name); //准備讀取 //AVPacket用於存儲一幀一幀的壓縮數據(H264) //緩沖區,開辟空間 AVPacket *packet = (AVPacket*)av_malloc(sizeof(AVPacket)); //AVFrame用於存儲解碼后的像素數據(YUV) //內存分配 AVFrame *pFrame = av_frame_alloc(); //YUV420 AVFrame *pFrameYUV = av_frame_alloc(); //只有指定了AVFrame的像素格式、畫面大小才能真正分配內存 //緩沖區分配內存 uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)); //初始化緩沖區 avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height); //用於轉碼(縮放)的參數,轉之前的寬高,轉之后的寬高,格式等 struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); int got_picture, ret; FILE *fp_yuv = fopen(output, "wb+"); int frame_count = 0; //6.一幀一幀的讀取壓縮數據 while (av_read_frame(pFormatCtx, packet) >= 0) { //只要視頻壓縮數據(根據流的索引位置判斷) if (packet->stream_index == v_stream_idx) { //7.解碼一幀視頻壓縮數據,得到視頻像素數據 ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); if (ret < 0) { printf("%s","解碼錯誤"); return; } //為0說明解碼完成,非0正在解碼 if (got_picture) { //AVFrame轉為像素格式YUV420,寬高 //2 6輸入、輸出數據 //3 7輸入、輸出畫面一行的數據的大小 AVFrame 轉換是一行一行轉換的 //4 輸入數據第一列要轉碼的位置 從0開始 //5 輸入畫面的高度 sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); //輸出到YUV文件 //AVFrame像素幀寫入文件 //data解碼后的圖像像素數據(音頻采樣數據) //Y 亮度 UV 色度(壓縮了) 人對亮度更加敏感 //U V 個數是Y的1/4 int y_size = pCodecCtx->width * pCodecCtx->height; fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); frame_count++; printf("解碼第%d幀\n",frame_count); } } //釋放資源 av_free_packet(packet); } fclose(fp_yuv); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_free_context(pFormatCtx); }
3.2.音頻解碼
#include <stdlib.h> #include <stdio.h> #include <unistd.h> //封裝格式 #include "libavformat/avformat.h" //解碼 #include "libavcodec/avcodec.h" //縮放 #include "libswscale/swscale.h" #include "libswresample/swresample.h" int main (void) { //1.注冊組件 av_register_all(); //封裝格式上下文 AVFormatContext *pFormatCtx = avformat_alloc_context(); //2.打開輸入音頻文件 if (avformat_open_input(&pFormatCtx, "test.mp3", NULL, NULL) != 0) { printf("%s", "打開輸入音頻文件失敗"); return; } //3.獲取音頻信息 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { printf("%s", "獲取音頻信息失敗"); return; } //音頻解碼,需要找到對應的AVStream所在的pFormatCtx->streams的索引位置 int audio_stream_idx = -1; int i = 0; for (; i < pFormatCtx->nb_streams; i++) { //根據類型判斷是否是音頻流 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { audio_stream_idx = i; break; } } //4.獲取解碼器 //根據索引拿到對應的流,根據流拿到解碼器上下文 AVCodecContext *pCodeCtx = pFormatCtx->streams[audio_stream_idx]->codec; //再根據上下文拿到編解碼id,通過該id拿到解碼器 AVCodec *pCodec = avcodec_find_decoder(pCodeCtx->codec_id); if (pCodec == NULL) { printf("%s", "無法解碼"); return; } //5.打開解碼器 if (avcodec_open2(pCodeCtx, pCodec, NULL) < 0) { printf("%s", "編碼器無法打開"); return; } //編碼數據 AVPacket *packet = av_malloc(sizeof(AVPacket)); //解壓縮數據 AVFrame *frame = av_frame_alloc(); //frame->16bit 44100 PCM 統一音頻采樣格式與采樣率 SwrContext *swrCtx = swr_alloc(); //重采樣設置選項-----------------------------------------------------------start //輸入的采樣格式 enum AVSampleFormat in_sample_fmt = pCodeCtx->sample_fmt; //輸出的采樣格式 16bit PCM enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16; //輸入的采樣率 int in_sample_rate = pCodeCtx->sample_rate; //輸出的采樣率 int out_sample_rate = 44100; //輸入的聲道布局 uint64_t in_ch_layout = pCodeCtx->channel_layout; //輸出的聲道布局 uint64_t out_ch_layout = AV_CH_LAYOUT_MONO; swr_alloc_set_opts(swrCtx, out_ch_layout, out_sample_fmt, out_sample_rate, in_ch_layout, in_sample_fmt, in_sample_rate, 0, NULL); swr_init(swrCtx); //重采樣設置選項-----------------------------------------------------------end //獲取輸出的聲道個數 int out_channel_nb = av_get_channel_layout_nb_channels(out_ch_layout); //存儲pcm數據 uint8_t *out_buffer = (uint8_t *) av_malloc(2 * 44100); FILE *fp_pcm = fopen("out.pcm", "wb"); int ret, got_frame, framecount = 0; //6.一幀一幀讀取壓縮的音頻數據AVPacket while (av_read_frame(pFormatCtx, packet) >= 0) { if (packet->stream_index == audio_stream_idx) { //解碼AVPacket->AVFrame ret = avcodec_decode_audio4(pCodeCtx, frame, &got_frame, packet); if (ret < 0) { printf("%s", "解碼完成"); } //非0,正在解碼 if (got_frame) { printf("解碼%d幀", framecount++); swr_convert(swrCtx, &out_buffer, 2 * 44100, frame->data, frame->nb_samples); //獲取sample的size int out_buffer_size = av_samples_get_buffer_size(NULL, out_channel_nb, frame->nb_samples, out_sample_fmt, 1); //寫入文件進行測試 fwrite(out_buffer, 1, out_buffer_size, fp_pcm); } } av_free_packet(packet); } fclose(fp_pcm); av_frame_free(&frame); av_free(out_buffer); swr_free(&swrCtx); avcodec_close(pCodeCtx); avformat_close_input(&pFormatCtx); return 0; }