-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathavdemuxthread.cpp
331 lines (276 loc) · 11.7 KB
/
avdemuxthread.cpp
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "avdemuxthread.h"
#include <QDebug>
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
AVDemuxThread::AVDemuxThread(QObject *parent):QThread (parent)
{
memset(m_szFilename, 0, sizeof(m_szFilename));
initAV();
}
AVDemuxThread::~AVDemuxThread()
{
avformat_network_deinit();
qDebug() << "thread quit";
}
void AVDemuxThread::initAV()
{
avformat_network_init();
}
void AVDemuxThread::openAV(char *szFileName)
{
strcpy(m_szFilename, szFileName);
start();
}
void AVDemuxThread::startWorkInAThread()
{
AudioPlayThread *workerThread = new AudioPlayThread(this);
connect(this, &AVDemuxThread::updateAudioData, workerThread, &AudioPlayThread::updateAudioData);
connect(workerThread, &AVDemuxThread::finished, workerThread, &QObject::deleteLater);
}
void AVDemuxThread::run()
{
AVFormatContext *avFormatContext = nullptr;
AVPacket packet;
AVFrame *pFrame = nullptr;
int audioStream= -1;
int videoStream = -1;
int err = -1;
avFormatContext = avformat_alloc_context();
if (!avFormatContext) {
av_log(nullptr, AV_LOG_FATAL, "Could not allocate context.\n");
}
if(avformat_open_input(&avFormatContext, m_szFilename, nullptr, nullptr) != 0){
}
// Retrieve stream information
if(avformat_find_stream_info(avFormatContext, nullptr)<0){
qDebug() << "Couldn't find stream information";
return;
}
for(unsigned int i=0; i < avFormatContext->nb_streams; i++){
if(avFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO){
audioStream = static_cast<int>(i);
}
else if(avFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){
videoStream = static_cast<int>(i);
}
}
if(audioStream == -1 && videoStream == -1){
qDebug() << "Didn't find a audio stream";
return;
}
pFrame = av_frame_alloc();
//音频
AVCodecContext *pCodecCtxAudio = nullptr;
AVCodec *pCodecAudio;
uint8_t *audioBuffer = nullptr;
struct SwrContext *au_convert_ctx = nullptr;
int out_buffer_size = 0;
if(audioStream != -1)
{
pCodecCtxAudio = avcodec_alloc_context3(nullptr);
if (!pCodecCtxAudio)
return;
//音频编码器设置
err = avcodec_parameters_to_context(pCodecCtxAudio, avFormatContext->streams[audioStream]->codecpar);
if(err < 0){
return;
}
// Find the decoder for the audio stream
pCodecAudio = avcodec_find_decoder(pCodecCtxAudio->codec_id);
if(pCodecAudio == nullptr){
qDebug() << "Codec not found";
return;
}
// Open codec
if(avcodec_open2(pCodecCtxAudio, pCodecAudio, nullptr)<0){
qDebug() << "Could not open codec";
return;
}
//Out Audio Param
uint64_t out_channel_layout=AV_CH_LAYOUT_STEREO;
//nb_samples: AAC-1024 MP3-1152
int out_nb_samples = pCodecCtxAudio->frame_size;
AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
int out_sample_rate = 44100;
int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);
//Out Buffer Size
out_buffer_size = av_samples_get_buffer_size(nullptr,
out_channels,
out_nb_samples,
out_sample_fmt,
1);
//FIX:Some Codec's Context Information is missing
int64_t in_channel_layout = av_get_default_channel_layout(pCodecCtxAudio->channels);
audioBuffer = (uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE * 2);
au_convert_ctx = swr_alloc();
au_convert_ctx = swr_alloc_set_opts(au_convert_ctx,
out_channel_layout,
out_sample_fmt,
out_sample_rate,
in_channel_layout,
pCodecCtxAudio->sample_fmt,
pCodecCtxAudio->sample_rate,
0,
nullptr);
swr_init(au_convert_ctx);
}
//视频
AVCodecContext *pVideoCodecContext = nullptr;
AVCodec *pCodecVideo;
AVCodecParameters *pVideoChannelCodecPara = nullptr;
int iWindowWidth = 0;
int iWindowHeight = 0;
struct SwsContext *img_convert_ctx = nullptr;
struct SwsContext *img_convert_ctx_rgb = nullptr;
AVFrame *pFrameYUV = nullptr;
AVFrame * pFrameRGB= nullptr;
unsigned char *rgbBuffer;
unsigned char *yuvBuffer;
if(videoStream != -1)
{
//开始视频
pVideoChannelCodecPara = avFormatContext->streams[videoStream]->codecpar;
pVideoCodecContext = avcodec_alloc_context3(nullptr);
if (!pVideoCodecContext){
qDebug() << "avcodec_alloc_context3";
return;
}
err = avcodec_parameters_to_context(pVideoCodecContext, pVideoChannelCodecPara);
if (err < 0){
qDebug() << "avcodec_parameters_to_context";
return;
}
pCodecVideo = avcodec_find_decoder(pVideoChannelCodecPara->codec_id);
if(pCodecVideo == nullptr){
qDebug() << "avcodec_find_decoder";
return;
}
qDebug() << "编解码器名:" << pCodecVideo->long_name;
err = avcodec_open2(pVideoCodecContext, pCodecVideo, nullptr);
if(err){
qDebug() << "avcodec_open2";
return;
}
iWindowWidth = pVideoCodecContext->width;
iWindowHeight = pVideoCodecContext->height;
img_convert_ctx = sws_getContext(pVideoCodecContext->width,
pVideoCodecContext->height,
pVideoCodecContext->pix_fmt,
pVideoCodecContext->width,
pVideoCodecContext->height,
AV_PIX_FMT_YUV420P,
SWS_BICUBIC,
nullptr,
nullptr,
nullptr);
img_convert_ctx_rgb = sws_getContext(pVideoCodecContext->width,
pVideoCodecContext->height,
AV_PIX_FMT_YUV420P,
pVideoCodecContext->width,
pVideoCodecContext->height,
AV_PIX_FMT_RGB32,
SWS_BICUBIC,
nullptr,
nullptr,
nullptr);
pFrameYUV = av_frame_alloc();
pFrameRGB = av_frame_alloc();
//计算一个标准画面的大小
yuvBuffer = static_cast<unsigned char *>(av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,
pVideoCodecContext->width,
pVideoCodecContext->height,
1)));
rgbBuffer = static_cast<unsigned char *>(av_malloc(av_image_get_buffer_size(AV_PIX_FMT_RGB32,
pVideoCodecContext->width,
pVideoCodecContext->height,
1
)));
av_image_fill_arrays(pFrameYUV->data,
pFrameYUV->linesize,
yuvBuffer,
AV_PIX_FMT_YUV420P,
pVideoCodecContext->width,
pVideoCodecContext->height,
1
);
av_image_fill_arrays(pFrameRGB->data,
pFrameRGB->linesize,
rgbBuffer,
AV_PIX_FMT_RGB32,
pVideoCodecContext->width,
pVideoCodecContext->height,
1
);
qDebug() << "视频宽度:" << iWindowWidth << "高度:" << iWindowHeight;
}
//前戏完毕
qDebug() << "--------------- File Information ----------------";
// Dump valid information onto standard error
av_dump_format(avFormatContext, 0, m_szFilename, false);
qDebug() << "-------------------------------------------------";
while(av_read_frame(avFormatContext, &packet) >= 0){
if(packet.stream_index == audioStream){
// msleep(10);
int got_frame = 0;
err = avcodec_decode_audio4( pCodecCtxAudio,
pFrame,
&got_frame,
&packet);
if (err < 0) {
qDebug() << "Error in decoding audio frame";
return;
}
if (got_frame > 0){
swr_convert(au_convert_ctx,
&audioBuffer,
MAX_AUDIO_FRAME_SIZE,
(const uint8_t **)pFrame->data,
pFrame->nb_samples);
//请注意,这里信号不能使用指针,否则会出错,这个害我排查了一天.....
QByteArray audio;
audio.append((char*)audioBuffer, out_buffer_size);
updateAudioData(audio);
}
else{
qDebug() << "no audio data";
}
}
else if(packet.stream_index == videoStream){
err = avcodec_send_packet(pVideoCodecContext, &packet);
if(err != 0){
if(AVERROR(EAGAIN) == err)
continue;
qDebug() << "发送视频帧失败!"<< err;
}
//这个延时会导致声音播放卡顿....
msleep(4);
//解码
while(avcodec_receive_frame(pVideoCodecContext, pFrame) == 0){
sws_scale(img_convert_ctx,
pFrame->data,
pFrame->linesize,
0,
pVideoCodecContext->height,
pFrameYUV->data,
pFrameYUV->linesize
);
if(img_convert_ctx_rgb){
sws_scale(img_convert_ctx_rgb,
pFrameYUV->data,
pFrameYUV->linesize,
0,
pVideoCodecContext->height,
pFrameRGB->data,
pFrameRGB->linesize);
//构造QImage,用于主页面显示
QImage image((uchar *)pFrameRGB->data[0], iWindowWidth, iWindowHeight,QImage::Format_ARGB32);
// QImage imagef = image.copy(); //把图像复制一份 传递给界面显
emit updateVideoPic(image);
}
}
}
}
// av_free(out_buffer);
// avcodec_close(pCodecCtx);
avformat_close_input(&avFormatContext);
qDebug() << "end of play";
}