資源簡介
Windows環境下使用Ffmpeg+Qt Creator開發的簡單播放器:
(1)聲音輸出使用PortAudio;
(2)視頻顯示使用SDL2;
(3)具備音視頻同步;
(4)使用QT開發,便于Debug運行查看Ffmpeg各個結構體的變量是如何變化。
代碼片段和文件信息
#include?
#include?
#include?“audio.h“
#include?“avpackets_queue.h“
#include?“av_clock.h“
#include?“video.h“
#include?“log.h“
#define?AVCODE_MAX_AUDIO_frame_SIZE 192000??/*?1?second?of?48khz?32bit?audio?*/
const?double?kPortAudioLatency??=?0.3;??//?0.3秒,也就是說PortAudio庫需要累積0.3秒的數據后才真正播放
#define?ERR_STREAM stderr
typedef?struct?audio_param
{
????AVframe?wantframe; //?指定PCM輸出格式
????PaStreamParameters?*outputParameters;
????PaStream?*stream;
????int?sampleRate;
????int?format;
????PacketQueue?audioQueue;?????//?音頻隊列
????int?packetEof;??????????????//?數據包已經讀取到最后
????int?quit;???????????????????//?是否退出線程
????double?audiobaseTime;???????//?音頻base?time
????SwrContext *swrCtx;????????//?音頻PCM格式轉換
}T_AudioParam;
static?T_AudioParam?sAudioParam;
static?int?_AudioDecodeframe(?AVCodecContext?*pAudioCodecCtx?uint8_t?*audioBuf?int?bufSize??int?*packeEof);
void?AudioDecodeThread(?void?*userdata?)
{
????FunEntry();
????AVCodecContext *pAudioCodecCtx?=?(AVCodecContext?*)?userdata;
????int len1 =?0;
????int audio_size =?0;
????uint8_t?audioBuf[AVCODE_MAX_AUDIO_frame_SIZE];
????AVClockDisable();???????//?禁止時鐘
????while?(?sAudioParam.quit?!=?1?)
????{
????????if(sAudioParam.packetEof?&&?sAudioParam.audioQueue.size?==?0)
????????{
????????????sAudioParam.quit?=?1;
????????}
????????audio_size?=?0;
????????if(VideoGetFirstframe())????????????//?等圖像出來后再出聲音
????????{
????????????audio_size?=?_AudioDecodeframe(?pAudioCodecCtx?audioBuf?sizeof(audioBuf)?&sAudioParam.packetEof);
????????}
????????if?(?audio_size?>?0?)
????????{
????????????AudioPlay(audioBuf?audio_size?0);
????????}
????????else
????????{
????????????std::this_thread::sleep_for(std::chrono::milliseconds(20));?????//?沒有數據時先休眠20毫秒
????????}
????}
????FunExit();
}
//?對于音頻來說,一個packet里面,可能含有多幀(frame)數據
/**
?*?@brief?_AudioDecodeframe
?*?@param?pAudioCodecCtx
?*?@param?audioBuf
?*?@param?bufSize
?*?@param?packeEof
?*?@return
?*/
int?_AudioDecodeframe(?AVCodecContext?*pAudioCodecCtx
???????????????????????uint8_t?*audioBuf?int?bufSize?int?*packeEof)
{
????AVPacket packet;
????AVframe *pframe?=?NULL;
????int gotframe?=?0;
????int decodeLen??=?0;
????long? audioBufIndex?=?0;
????int convertLength =?0;
????int convertAll =?0;
????if?(?PacketQueueTake(?&sAudioParam.audioQueue?&packet?1?)?0?)
????{
????????std::this_thread::sleep_for(std::chrono::milliseconds(30));?????//?沒有取到數據休眠
????????return(-1);
????}
????pframe?=?av_frame_alloc();
????*packeEof?=?packet.size???0:?1;???????//?約定使用數據長度為0時來標記packet已經讀取完畢
????while?(?packet.size?>?0?)
????{
????????/*
?????????*?pAudioCodecCtx:解碼器信息
?????????*?pframe:輸出,存數據到frame
?????????*?gotframe:輸出。0代表有frame取了,不意味發生了錯誤。
?????????*?packet:輸入,取數據解碼。
?????????*/
????????decodeLen?=?avcodec_decode_audio4(?pAudioCodecCtx?pframe?&gotframe?&packet?);
????????if(decodeLen?0)
????????{
????????????LogError(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????814??2018-02-27?18:23??01-simple-pla
?????文件???????1470??2018-03-06?10:09??01-simple-pla
?????文件??????24050??2018-03-06?18:40??01-simple-pla
?????文件??????11046??2018-03-06?17:01??01-simple-pla
?????文件????????465??2018-03-06?16:31??01-simple-pla
?????文件???????1596??2018-03-06?17:01??01-simple-pla
?????文件????????888??2018-03-06?16:31??01-simple-pla
?????文件???????1426??2018-03-06?17:03??01-simple-pla
?????文件????????292??2018-03-06?10:09??01-simple-pla
?????文件???24510976??2018-02-26?09:14??01-simple-pla
?????文件????2011648??2018-02-26?09:14??01-simple-pla
?????文件????4290048??2018-02-26?09:14??01-simple-pla
?????文件????5145088??2018-02-26?09:14??01-simple-pla
?????文件?????669184??2018-02-26?09:14??01-simple-pla
?????文件?????120334??2017-10-09?11:53??01-simple-pla
?????文件????1540622??2015-12-29?06:25??01-simple-pla
?????文件??????79360??2015-12-29?06:25??01-simple-pla
?????文件?????667648??2018-02-28?11:56??01-simple-pla
?????文件?????116736??2018-02-26?09:14??01-simple-pla
?????文件????1005056??2018-02-26?09:14??01-simple-pla
?????文件????6636853??2018-02-26?09:14??01-simple-pla
?????文件?????293888??2018-02-26?09:14??01-simple-pla
?????文件?????509440??2018-02-26?09:14??01-simple-pla
?????文件???24510976??2018-02-26?09:14??01-simple-pla
?????文件????2011648??2018-02-26?09:14??01-simple-pla
?????文件????4290048??2018-02-26?09:14??01-simple-pla
?????文件????5145088??2018-02-26?09:14??01-simple-pla
?????文件?????669184??2018-02-26?09:14??01-simple-pla
?????文件?????281600??2018-02-26?09:14??01-simple-pla
?????文件????1258496??2018-02-26?09:14??01-simple-pla
............此處省略343個文件信息
評論
共有 條評論