forked from c12121234/MyFFmpegPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpegdemux.cpp
More file actions
266 lines (243 loc) · 6.85 KB
/
ffmpegdemux.cpp
File metadata and controls
266 lines (243 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "ffmpegdemux.h"
extern "C"{
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
}
#include <QDebug>
double static r2d(AVRational r)
{
return r.den == 0 ? 0 :static_cast<double>(r.num)/static_cast<double>(r.den);
}
FFmpegDemux::FFmpegDemux():
m_pFormatcontext(nullptr)
,m_nVideoStreamIdx(0)
,m_nAudioStreamIdx(0)
,m_nTotalTimeMs(0)
,m_nWidth(0)
,m_nHeight(0)
,m_nSampleRate(0)
,m_nChannels(0)
{
static bool s_bFirst = true;
static QMutex s_mutex;
s_mutex.lock();
if(s_bFirst)
{
av_register_all();
avformat_network_init();
s_bFirst = false;
}
s_mutex.unlock();
}
FFmpegDemux::~FFmpegDemux()
{
}
bool FFmpegDemux::Open(QString strURL)
{
Close();
//for open local file
av_register_all();
//for rtsp rtmp http stream video protocol.
avformat_network_init();
//decode encode codec
avcodec_register_all();
//parameter setting
AVDictionary* opt = nullptr;
av_dict_set(&opt,"rtsp_transport","tcp",0); //rtsp以tcp協議開啟
av_dict_set(&opt,"max_delay","500",0); //網路延遲時間
m_Mutex.lock();
int ret = avformat_open_input(
&m_pFormatcontext,
strURL.toStdString().c_str(),
0,
&opt //參數設定 如rtsp delay time
);
if(ret!=0)
{
m_Mutex.unlock();
char buff[1024] = {0};
av_strerror(ret,buff,sizeof(buff)-1);
qInfo()<<"open "<<strURL<<"failed, "<<buff;
return false;
}
else
qInfo()<<"open "<<strURL<<"sucess";
//獲取stream info
ret = avformat_find_stream_info(m_pFormatcontext,nullptr);
//時長
m_nTotalTimeMs = m_pFormatcontext->duration/(AV_TIME_BASE/1000);
qInfo()<<"total time ms = "<<m_nTotalTimeMs;
//dump 詳細訊息
av_dump_format(m_pFormatcontext,0,strURL.toStdString().c_str(),0);
m_nVideoStreamIdx = av_find_best_stream(m_pFormatcontext,AVMEDIA_TYPE_VIDEO,-1,-1,nullptr,0);
AVStream* as = m_pFormatcontext->streams[m_nVideoStreamIdx];
m_nWidth = as->codecpar->width;
m_nHeight = as->codecpar->height;
qDebug()<<"duration:"<<m_pFormatcontext->streams[m_nVideoStreamIdx]->duration;
qDebug()<<"=====================================================";
qDebug()<<"codec_id: "<<as->codecpar->codec_id;
//AVSampleFormat
qDebug()<<"format: "<<as->codecpar->format;
qDebug()<<m_nVideoStreamIdx<<": "<<"video info";
qDebug()<<"width:"<<as->codecpar->width; //此屬性未必有資訊
qDebug()<<"height:"<<as->codecpar->height;//此屬性未必有資訊
qDebug()<<"video fps = "<<r2d(as->avg_frame_rate); //avg_frame_rate為一數組 紀錄分子和分母以確保精確度
//獲取音訊index
m_nAudioStreamIdx = av_find_best_stream(m_pFormatcontext,AVMEDIA_TYPE_AUDIO,-1,-1,nullptr,0);
as = m_pFormatcontext->streams[m_nAudioStreamIdx];
m_nSampleRate = as->codecpar->sample_rate;
m_nChannels = as->codecpar->channels;
qDebug()<<"duration:"<<m_pFormatcontext->streams[m_nAudioStreamIdx]->duration;
qDebug()<<"=====================================================";
qDebug()<<"codec_id: "<<as->codecpar->codec_id;
qDebug()<<"format: "<<as->codecpar->format;
qDebug()<<m_nAudioStreamIdx<<": "<<"audio info";
qDebug()<<"sample rate: "<<as->codecpar->sample_rate;
qDebug()<<"channels: "<<as->codecpar->channels;
qDebug()<<"frame size:"<<as->codecpar->frame_size; //單通道定量樣本數量
//1024(frame size) * 2(雙通道) * format(以8bit為基準) 4 = 8192
m_Mutex.unlock();
return true;
}
AVPacket *FFmpegDemux::Read()
{
m_Mutex.lock();
if(!m_pFormatcontext)
{
m_Mutex.unlock();
return nullptr;
}
AVPacket* pPkt = av_packet_alloc();
//讀取1 frame 並分配空間
int nRet = av_read_frame(m_pFormatcontext,pPkt);
if(nRet!=0)
{
m_Mutex.unlock();
av_packet_free(&pPkt);
return nullptr;
}
//pts/dts turn to ms
pPkt->pts = pPkt->pts*(1000 * (r2d(m_pFormatcontext->streams[pPkt->stream_index]->time_base)));
pPkt->dts = pPkt->dts*(1000 * (r2d(m_pFormatcontext->streams[pPkt->stream_index]->time_base)));
//qDebug()<<"pts:"<<pPkt->pts<<" "<<flush;
m_Mutex.unlock();
return pPkt;
}
AVPacket *FFmpegDemux::ReadVideo()
{
m_Mutex.lock();
if(!m_pFormatcontext)
{
m_Mutex.unlock();
return nullptr;
}
m_Mutex.unlock();
AVPacket* pPkt = nullptr;
for(int i = 0;i<20;++i) //magic number 20 ,prevent block.
{
pPkt = Read();
if(!pPkt)
break;
if(pPkt->stream_index == m_nVideoStreamIdx)
{
break;
}
av_packet_free(&pPkt);
}
return pPkt;
}
bool FFmpegDemux::Seek(double dPos)
{
m_Mutex.lock();
if(!m_pFormatcontext)
{
m_Mutex.unlock();
return false;
}
//清理讀取緩衝
avformat_flush(m_pFormatcontext);
long long nSeekPos = 0;
nSeekPos = m_pFormatcontext->streams[m_nVideoStreamIdx]->duration * dPos;
int nRet = av_seek_frame(m_pFormatcontext,m_nVideoStreamIdx,nSeekPos,AVSEEK_FLAG_BACKWARD|AVSEEK_FLAG_FRAME);
m_Mutex.unlock();
return (nRet >= 0)?true:false;
//注意 此seek功能只會跳到關鍵frame 若要準確seek到位置必須和解碼模組關聯 故不在此作業
}
AVCodecParameters *FFmpegDemux::CopyVPara()
{
m_Mutex.lock();
if(!m_pFormatcontext)
{
m_Mutex.unlock();
return nullptr;
}
AVCodecParameters* pA = avcodec_parameters_alloc();
avcodec_parameters_copy(pA,m_pFormatcontext->streams[m_nVideoStreamIdx]->codecpar);
m_Mutex.unlock();
return pA;
}
AVCodecParameters *FFmpegDemux::CopyAPara()
{
m_Mutex.lock();
if(!m_pFormatcontext)
{
m_Mutex.unlock();
return nullptr;
}
AVCodecParameters* pA = avcodec_parameters_alloc();
avcodec_parameters_copy(pA,m_pFormatcontext->streams[m_nAudioStreamIdx]->codecpar);
m_Mutex.unlock();
return pA;
}
void FFmpegDemux::Clear()
{
m_Mutex.lock();
if(!m_pFormatcontext)
{
m_Mutex.unlock();
return;
}
avformat_flush(m_pFormatcontext);
m_Mutex.unlock();
}
void FFmpegDemux::Close()
{
m_Mutex.lock();
if(!m_pFormatcontext)
{
m_Mutex.unlock();
return;
}
avformat_close_input(&m_pFormatcontext);
//媒體總時長(毫秒)
m_nTotalTimeMs = 0;
m_Mutex.unlock();
}
bool FFmpegDemux::IsAudio(AVPacket *pPkt)
{
if(!pPkt)
return false;
if(pPkt->stream_index == m_nVideoStreamIdx)
return false;
return true;
}
int FFmpegDemux::GetTotalTimeMs()
{
return m_nTotalTimeMs;
}
int FFmpegDemux::GetWidth()
{
return m_nWidth;
}
int FFmpegDemux::GetHeight()
{
return m_nHeight;
}
int FFmpegDemux::GetSampleRate()
{
return m_nSampleRate;
}
int FFmpegDemux::GetChannels()
{
return m_nChannels;
}