[go: up one dir, main page]

Menu

[r4]: / ffmpegdecoder.cpp  Maximize  Restore  History

Download this file

396 lines (308 with data), 12.1 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
Copyright (C) 2011 Arnaud Champenois arthelion92@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* FFMPEG C++ Wrapper
Thanks to http://dranger.com/ and QTFFmpegWrapper
*/
#include "ffmpegdecoder.h"
#include "utils.h"
#include <QFileInfo>
#define TS_PACKET_SIZE 188
const int64_t theOffsetValue = 1LL<<33;
int64_t global_pos = 0;
extern "C" {
int our_get_buffer(struct AVCodecContext *c, AVFrame *pic) {
int ret = avcodec_default_get_buffer(c, pic);
int64_t *pts = (int64_t*)av_malloc(sizeof(int64_t));
*pts = global_pos;
pic->opaque = pts;
return ret;
}
void our_release_buffer(struct AVCodecContext *c, AVFrame *pic) {
if(pic) av_freep(&pic->opaque);
avcodec_default_release_buffer(c, pic);
}
int64_t frame_get_pos(AVFrame *pic)
{
return *((int64_t*)pic->opaque);
}
}
const AVRational FFMPEGDecoder::millisecondbase = {1, 1000};
FFMPEGDecoder::FFMPEGDecoder()
{
InitValues();
}
void FFMPEGDecoder::InitValues()
{
mFormatCtx = NULL;
mAudioCodecCtx = NULL;
mVideoCodecCtx = NULL;
mVideoStream=-1;
mAudioStream=-1;
mFrame = NULL;
mFrameRGB = NULL;
mImgConvertCtx = NULL;
mLastFrame=-1;
mFirstFrame=-1;
mFilePosition = 0;
mBytesPerSec = 0;
}
FFMPEGDecoder::~FFMPEGDecoder()
{
Close();
}
int64_t FFMPEGDecoder::MSToFilePos(int64_t aMsValue) const
{
int64_t aBitCount = (aMsValue*mBytesPerSec)/1000;
int64_t aPacketNb = aBitCount/TS_PACKET_SIZE;
return (aPacketNb*TS_PACKET_SIZE);
}
bool FFMPEGDecoder::Open(const QString &aFileName,int aVideoStream, int anAudioStream)
{
if(avformat_open_input(&mFormatCtx, aFileName.toStdString().c_str(), NULL, NULL)!=0)
return false;
// Retrieve stream information
if(av_find_stream_info(mFormatCtx)<0)
return false; // Couldn't find stream information
// Find the first video stream
mVideoStream=aVideoStream;
mAudioStream=anAudioStream;
for(unsigned i=0; i < mFormatCtx->nb_streams; i++) {
if(mFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO
&&
mVideoStream < 0) {
mVideoStream=i;
}
if(mFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
mAudioStream < 0) {
mAudioStream=i;
}
if(mVideoStream != -1 && mAudioStream != -1) break;
}
if(mVideoStream==-1 || mAudioStream==-1)
return false;
mVideoCodecCtx=mFormatCtx->streams[mVideoStream]->codec;
mAudioCodecCtx=mFormatCtx->streams[mAudioStream]->codec;
print_log(QString("TimeBase = %1,%2").arg(mFormatCtx->streams[mVideoStream]->time_base.num)
.arg(mFormatCtx->streams[mVideoStream]->time_base.den));
print_log(QString("Offset=%1").arg(theOffsetValue));
AVCodec *aCodec;
// Find the decoder for the video stream
aCodec=avcodec_find_decoder(mVideoCodecCtx->codec_id);
if(aCodec==NULL) {
return false; // Codec not found
}
if(avcodec_open(mVideoCodecCtx, aCodec)<0)
return false; // Could not open codec
// Find the decoder for the audio stream
aCodec=avcodec_find_decoder(mAudioCodecCtx->codec_id);
if(aCodec==NULL) {
return false; // Codec not found
}
if(avcodec_open(mAudioCodecCtx, aCodec)<0)
return false; // Could not open codec
// Allocate video frame
mFrame=avcodec_alloc_frame();
mFrameRGB=avcodec_alloc_frame();
// Hack to correct wrong frame rates that seem to be generated by some
// codecs
if(mVideoCodecCtx->time_base.num>1000 && mVideoCodecCtx->time_base.den==1)
mVideoCodecCtx->time_base.den=1000;
if(mFormatCtx->duration > 0 && mFormatCtx->file_size > 0) // && mVideoCodecCtx->codec_id == CODEC_ID_H264)
{
mBytesPerSec = (1000*mFormatCtx->file_size)/TickToMS(mFormatCtx->streams[mVideoStream]->duration);
}
else
mBytesPerSec = ((105*mFormatCtx->bit_rate)/800);
print_log(QString("Rate B/s : %1").arg(mBytesPerSec));
// Determine required buffer size and allocate buffer
unsigned numBytes=avpicture_get_size(PIX_FMT_RGB24, mVideoCodecCtx->width,mVideoCodecCtx->height);
uint8_t *buffer=new uint8_t[numBytes];
// Assign appropriate parts of buffer to image planes in pFrameRGB
avpicture_fill((AVPicture *)mFrameRGB, buffer, PIX_FMT_RGB24,
mVideoCodecCtx->width, mVideoCodecCtx->height);
mFilePosition = 0;
mVideoCodecCtx->get_buffer = our_get_buffer;
mVideoCodecCtx->release_buffer = our_release_buffer;
return true;
}
void FFMPEGDecoder::Close()
{
if(mAudioCodecCtx)
avcodec_close(mAudioCodecCtx);
if(mVideoCodecCtx)
avcodec_close(mVideoCodecCtx);
if(mVideoCodecCtx)
av_close_input_file(mFormatCtx);
if(mFrame)
av_free(mFrame);
if(mFrameRGB)
av_free(mFrameRGB);
InitValues();
}
bool
FFMPEGDecoder::ReadNextPacket(FFMPEGPacket&aPacket, bool aNeedKeyFrame)
{
aPacket.Invalid();
while(!aPacket.IsValid())
{
if(av_read_frame(mFormatCtx, &aPacket.GetPacket())<0)
return false;
else if(aPacket.GetStreamIndex()==mVideoStream &&
(!aNeedKeyFrame || aPacket.IsKeyFrame()))
aPacket.SetValidity(FFMPEGPacket::VIDEO_PACKET);
else if(!aNeedKeyFrame && aPacket.GetStreamIndex()==mAudioStream)
aPacket.SetValidity(FFMPEGPacket::AUDIO_PACKET);
else
av_free_packet(&aPacket.GetPacket());
}
print_log(QString("Packet : dts=%1 pts=%2 key=%3").arg(aPacket.GetPacket().dts).arg(aPacket.GetPacket().pts).arg(aPacket.GetPacket().flags));
return true;
}
int64_t
FFMPEGDecoder::GetMSPosition() const
{
if(mLastFrame<0 || mFirstFrame<0) return 0;
return PosToMS(mLastFrame);
}
bool
FFMPEGDecoder::ReadNextImage(QImage &anImage, bool aNeedKeyFrame)
{
FFMPEGPacket aPacket;
int frameFinished = 0;
bool aSetFrame = false;
print_log(QString("\nRead Frame"));
while(!frameFinished)
{
while(ReadNextPacket(aPacket,(!aSetFrame)?aNeedKeyFrame:false) && aPacket.GetType() != FFMPEGPacket::VIDEO_PACKET);
if(!aSetFrame)
aSetFrame = true;
if(!aPacket.IsValid())
return false;
mLastFrame = aPacket.GetDTS();
global_pos = aPacket.GetPos();
avcodec_decode_video2(mVideoCodecCtx,mFrame,&frameFinished,&aPacket.GetPacket());
if(aNeedKeyFrame && frameFinished && !mFrame->key_frame) frameFinished = false;
}
mFilePosition = frame_get_pos(mFrame);
if(mFirstFrame<0)
mFirstFrame = mLastFrame;
else if(mLastFrame<mFirstFrame)
mLastFrame += theOffsetValue;
print_log(QString("Frame pos=%1 pts=%2 ms=%3").arg(mFilePosition).arg(mLastFrame).arg(GetMSPosition()));
int w = mVideoCodecCtx->width;
int h = mVideoCodecCtx->height;
if(mFrame->interlaced_frame)
avpicture_deinterlace ((AVPicture*)mFrame,
(AVPicture*)mFrame,
mVideoCodecCtx->pix_fmt,w,h);
mImgConvertCtx = sws_getCachedContext(mImgConvertCtx,w, h,
mVideoCodecCtx->pix_fmt,
w, h,
PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
if(mImgConvertCtx == NULL)
return false;
sws_scale(mImgConvertCtx, mFrame->data, mFrame->linesize, 0, mVideoCodecCtx->height,
mFrameRGB->data, mFrameRGB->linesize);
// Convert the frame to QImage
anImage=QImage(w,h,QImage::Format_RGB888);
for(int y=0;y<h;y++)
memcpy(anImage.scanLine(y),
mFrameRGB->data[0]+y*mFrameRGB->linesize[0],w*3);
return true;
}
bool FFMPEGDecoder::SeekFrame(VPosition aPos,SeekMode aMode)
{
if(aMode == FRAME_SEEK_CUR)
aPos.mFilePos += mFilePosition;
print_log(QString("\nSeek Frame : %1").arg(aPos.mFilePos));
if(av_seek_frame(mFormatCtx,mVideoStream,aPos.mFilePos, AVSEEK_FLAG_BYTE)<0)
return false;
avcodec_flush_buffers(mVideoCodecCtx);
avcodec_flush_buffers(mAudioCodecCtx);
return true;
}
int64_t FFMPEGDecoder::MSToPos(int64_t aMsValue) const
{
int64_t aPos = mFirstFrame+av_rescale_q(aMsValue,millisecondbase,mFormatCtx->streams[mVideoStream]->time_base);
if(aPos>=theOffsetValue)
aPos -= theOffsetValue;
return aPos;
}
bool FFMPEGDecoder::SeekFrame(int64_t aMsPos,SeekMode aMode)
{
int64_t aPos = (aMode == FRAME_SEEK_SET)?MSToPos(aMsPos):MSToTick(aMsPos);
if(aMode == FRAME_SEEK_CUR)
{
aPos += mLastFrame;
if(aPos>=theOffsetValue) aPos -= theOffsetValue;
}
print_log(QString("\nSeek : %1, ms=%2 (Pos:%3)").arg(aPos).arg(aMsPos).arg(mLastFrame));
if(av_seek_frame(mFormatCtx,mVideoStream,aPos, (aPos>mLastFrame)?AVSEEK_FLAG_BACKWARD:0)<0)
return SeekFrame(GetVPosition(aMsPos),aMode);
avcodec_flush_buffers(mVideoCodecCtx);
avcodec_flush_buffers(mAudioCodecCtx);
return true;
}
bool FFMPEGDecoder::SeekPosition(int64_t aPos)
{
if(av_seek_frame(mFormatCtx,mVideoStream,aPos, AVSEEK_FLAG_BYTE)<0)
return false;
mFilePosition = aPos;
mLastFrame = MSToPos(GetMSPosition());
avcodec_flush_buffers(mVideoCodecCtx);
avcodec_flush_buffers(mAudioCodecCtx);
return true;
}
void FFMPEGDecoder::Rewind()
{
if(av_seek_frame(mFormatCtx,mVideoStream,0, AVSEEK_FLAG_BYTE)<0)
return;
avcodec_flush_buffers(mVideoCodecCtx);
avcodec_flush_buffers(mAudioCodecCtx);
mLastFrame = mFirstFrame;
}
bool FFMPEGDecoder::GetFileInfo(FFMPEGFileInfo &aFileInfo)
{
aFileInfo.mLength = av_rescale_q(mFormatCtx->streams[mVideoStream]->duration,
mFormatCtx->streams[mVideoStream]->time_base,millisecondbase);
aFileInfo.mFrameRate = av_q2d(mFormatCtx->streams[mVideoStream]->avg_frame_rate);
aFileInfo.mFrameNb = mFormatCtx->streams[mVideoStream]->nb_frames;
if(aFileInfo.mFrameNb == 0 && aFileInfo.mLength != 0 && aFileInfo.mFrameRate!=0)
aFileInfo.mFrameNb = (int64_t)floor(aFileInfo.mLength*aFileInfo.mFrameRate);
else if(aFileInfo.mLength==0 && aFileInfo.mFrameRate!=0 && aFileInfo.mFrameNb!=0)
aFileInfo.mLength = (int64_t)floor(aFileInfo.mFrameNb/aFileInfo.mFrameRate);
print_log(QString("Length=%1").arg(aFileInfo.mLength));
GetAspectRatio(aFileInfo.mAspectRatio[0],aFileInfo.mAspectRatio[1]);
aFileInfo.mWidth = mVideoCodecCtx->width;
aFileInfo.mHeight = mVideoCodecCtx->height;
if(aFileInfo.mLength == 0)
return false;
else return true;
}
void FFMPEGDecoder::GetAspectRatio(int64_t&aNum,int64_t&aDen)
{
aNum = mVideoCodecCtx->sample_aspect_ratio.num*mVideoCodecCtx->width;
aDen = mVideoCodecCtx->sample_aspect_ratio.den*mVideoCodecCtx->height;
int64_t aMax = (aNum>aDen) ?aNum:aDen;
for(int64_t i=2;i<aMax;i++)
{
if(aNum%i==0 && aDen%i==0)
{
aNum /= i;
aDen /= i;
i=1;
}
}
}