-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathffmpeg.cpp
More file actions
302 lines (257 loc) · 7.5 KB
/
ffmpeg.cpp
File metadata and controls
302 lines (257 loc) · 7.5 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
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
#include "ffmpeg.h"
#include "faudio.h"
#include <iostream>
#include <QDebug>
using namespace std;
static double r2d(AVRational r)
{
return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
}
FFMpeg::FFMpeg()
{
av_register_all();//ffmpeg 编程第一句 注册
mIsPlay=false;
}
FFMpeg::~FFMpeg()
{
}
int FFMpeg::openVideo(const char *path)
{
qDebug()<<"openVideo:"<<path;
closeForeVideo();
mtx.lock();
int nRet = avformat_open_input(&mAfc,path,0,0);
if(nRet!=0){
mtx.unlock();
char buf[1024]={0};
av_strerror(nRet,buf,sizeof(buf));
qDebug()<<"av_strerror:"<<buf<<nRet;
return 0;
}
//视频时间,毫秒
mTotalMs= (mAfc->duration/AV_TIME_BASE)*1000;
qDebug()<<"mTotalMs:"<<mTotalMs;
//打开解码器
for(int i=0;i<mAfc->nb_streams;i++){//nb_streams打开的视频文件中流的数量,一般nb_streams = 2,音频流和视频流
AVCodecContext *acc = mAfc->streams[i]->codec;//分别获取音频流和视频流的解码器
if(acc->codec_type==AVMEDIA_TYPE_VIDEO)//如果是视频
{
mVideoStream=i;
AVCodec *codec=avcodec_find_decoder(acc->codec_id);//解码器
//找不到 解码器
if(!codec){
mtx.unlock();
qDebug()<<"找不到视频编码器";
return 0;
}
//解码器打开失败
int err = avcodec_open2(acc,codec,NULL);
if(err!=0){
mtx.unlock();
char buf[1024]={0};
av_strerror(err,buf,sizeof(buf));
qDebug()<<"视频编码器打开失败";
return 0;
}
}else if(acc->codec_type==AVMEDIA_TYPE_AUDIO)//如果是 音频
{
mAudioStream =i;
AVCodec *codec = avcodec_find_decoder(acc->codec_id);
int aRet = avcodec_open2(acc,codec,NULL);
if(aRet<0){
mtx.unlock();
qDebug()<<"音频编码器打开失败";
return 0;
}
//设置音频参数
this->mSampleRate = acc->sample_rate;//采样率
this->mChannel = acc->channels;//通道数
switch (acc->sample_fmt) {
case AV_SAMPLE_FMT_S16:
this->mSampleSize=16;
break;
case AV_SAMPLE_FMT_S32:
this->mSampleSize=32;
break;
default:
break;
}
qDebug()<<"采样率,通道数:"<<acc->sample_rate<<acc->channels<<acc->sample_fmt;
}
}
mtx.unlock();
return mTotalMs;
}
void FFMpeg::closeForeVideo()
{
mtx.lock();
if(mAfc)
avformat_close_input(&mAfc);
if(mYUV)
av_frame_free(&mYUV);
if(mcCtx){
sws_freeContext(mcCtx);
mcCtx=nullptr;
}
if(maCtx)
swr_free(&maCtx);
mtx.unlock();
}
bool FFMpeg::seek(float pos)
{
mtx.lock();
if (!mAfc)
{
mtx.unlock();
return false;
}
int64_t stamp = 0;
stamp = pos * mAfc->streams[mVideoStream]->duration;
int re = av_seek_frame(mAfc, mVideoStream, stamp, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME); //向后|关键帧
//清除之前的解码缓冲
avcodec_flush_buffers(mAfc->streams[mVideoStream]->codec);
mtx.unlock();
if (re >= 0)
{
return true;
}
//关键帧,P帧, B帧
return false;
}
int FFMpeg::getPts(const AVPacket *pkt)
{
mtx.lock();
if (!mAfc)
{
mtx.unlock();
return -1;
}
int pts = (pkt->pts * r2d(mAfc->streams[pkt->stream_index]->time_base)) * 1000; //毫秒数
mtx.unlock();
return pts;
}
int FFMpeg::decodeFrame(const AVPacket *pkt)
{
mtx.lock();
if(!mAfc){
mtx.unlock();
return 0;
}
if(mYUV==nullptr)
mYUV=av_frame_alloc();
if(mPCM==nullptr)
mPCM=av_frame_alloc();
AVFrame *frame = mYUV;//解码后 YUV 会改变
if(pkt->stream_index==mAudioStream)
frame=mPCM;
//根据索引 stream_index 来判断是音频还是视频
int re = avcodec_send_packet(mAfc->streams[pkt->stream_index]->codec,pkt);
if(re!=0){
mtx.unlock();
return 0;
}
re = avcodec_receive_frame(mAfc->streams[pkt->stream_index]->codec,frame);
if(re!=0)
{
//失败
mtx.unlock();
return 0;
}
mtx.unlock();
int p=(frame->pts*r2d(mAfc->streams[pkt->stream_index]->time_base))*1000;
if(pkt->stream_index==mAudioStream)
this->mPts=p;
return mPts;
}
AVPacket FFMpeg::readFrame()
{
AVPacket pkt;
memset(&pkt, 0, sizeof(AVPacket));
mtx.lock();
if (!mAfc)
{
mtx.unlock();
return pkt;
}
int err = av_read_frame(mAfc, &pkt);
if (err != 0)
{
//失败
}
mtx.unlock();
// qDebug()<<"readFrame:"<<pkt.pts;
return pkt;
}
bool FFMpeg::yuvToRGB(char *out, int outWidth, int outHeight)
{
mtx.lock();
if (!mAfc || !mYUV) //像素转换的前提是视频已经打开
{
mtx.unlock();
return false;
}
AVCodecContext *videoCtx = mAfc->streams[this->mVideoStream]->codec;
mcCtx = sws_getCachedContext(mcCtx, videoCtx->width, videoCtx->height,
videoCtx->pix_fmt, //像素点的格式
outWidth, outHeight, //目标宽度与高度
AV_PIX_FMT_BGRA, //输出的格式
SWS_BICUBIC, //算法标记
NULL, NULL, NULL
);
if (mcCtx)
{
//sws_getCachedContext 成功"
}
else
{
mtx.unlock();
//"sws_getCachedContext 失败"
return false;
}
uint8_t *data[AV_NUM_DATA_POINTERS] = { 0 };
data[0] = (uint8_t *)out; //指针传值,形参的值会被改变,out的值一直在变,所以QImage每次的画面都不一样,画面就这样显示出来了,这应该是整个开发过程最难的点
int linesize[AV_NUM_DATA_POINTERS] = { 0 };
linesize[0] = outWidth * 4; //每一行转码的宽度
//返回转码后的高度
int h = sws_scale(mcCtx, mYUV->data, mYUV->linesize, 0, videoCtx->height,
data,
linesize
);
//return true;
//已返回,何来解锁,永远死锁。
//曾经画面一直无法播放,无法找到原因。一句代码,影响整个系统,越简单的错误,越容易忽视。
mtx.unlock();
}
int FFMpeg::toPCM(char *out)
{
mtx.lock();
if (!mAfc || !mPCM || !out)
{
mtx.unlock();
return 0;
}
AVCodecContext *ctx = mAfc->streams[mAudioStream]->codec;
if (maCtx == NULL)
{
maCtx = swr_alloc();
swr_alloc_set_opts(maCtx, ctx->channel_layout,
AV_SAMPLE_FMT_S16,
ctx->sample_rate,
ctx->channels,
ctx->sample_fmt,
ctx->sample_rate,
0, 0);
swr_init(maCtx);
}
uint8_t *data[1];
data[0] = (uint8_t *)out;
int len = swr_convert(maCtx, data, 10000, (const uint8_t **)mPCM->data, mPCM->nb_samples);
if (len <= 0)
{
mtx.unlock();
return 0;
}
int outsize = av_samples_get_buffer_size(NULL, ctx->channels, mPCM->nb_samples, AV_SAMPLE_FMT_S16, 0);
mtx.unlock();
return outsize;
}