-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVideoDecoder.cpp
543 lines (482 loc) · 11.9 KB
/
VideoDecoder.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include "VideoDecoder.h"
using namespace std;
using namespace cocos2d;
using namespace video;
Decoder* Decoder::create(const std::string& path)
{
auto ret = new (std::nothrow) Decoder();
if (ret&&ret->open(path))
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
bool Decoder::open(const std::string& path)
{
if (opened)
return false;
filePath = FileUtils::getInstance()->fullPathForFilename(path);
if (filePath.empty())
{
VERRO("no file: %s", path.c_str());
return false;
}
bool ok = false;
#define BREAK_IF(cond) if(cond) { VINFO("failed: %s", #cond); break; }
do
{
pFormatCtx = avformat_alloc_context();
BREAK_IF(!pFormatCtx);
demuxer = ffmpeg::Demuxer::create(filePath, pFormatCtx);
BREAK_IF(!demuxer);
demuxer->retain();
idxAudio = demuxer->getBestAudioStream();
idxVideo = demuxer->getBestVideoStream();
BREAK_IF(idxVideo < 0);
videoQueue = ffmpeg::VideoFrameQueue::create();
BREAK_IF(!videoQueue);
videoQueue->retain();
videoQueue->setMaxSize(64);
BREAK_IF(!demuxer->setVideoReceiver(videoQueue, idxVideo));
if (idxAudio >= 0)
{
audioQueue = ffmpeg::AudioFrameQueue::create();
BREAK_IF(!audioQueue);
audioQueue->retain();
audioQueue->setMaxSize(64);
demuxer->setAudioReceiver(audioQueue, idxAudio);
}
demuxer->prepare();
BREAK_IF(!demuxer->getDecoder(idxVideo));
// get the codec context for the video stream
pCodecCtx = demuxer->getDecoder(idxVideo)->getCodecContext();
pCodecV = demuxer->getDecoder(idxVideo)->getCodec();
ok = true;
}
while (false);
if (!ok)
return false;
//av_dump_format(pFormatCtx, 0, filePath.c_str(), 0);
nFramesV = pFormatCtx->streams[idxVideo]->nb_frames;
rawWidth = pCodecCtx->width;
rawHeight = pCodecCtx->height;
timeBaseV = pFormatCtx->streams[idxVideo]->time_base;
if (idxAudio >= 0)
timeBaseA = pFormatCtx->streams[idxAudio]->time_base;
durationV = pFormatCtx->streams[idxVideo]->duration * av_q2d(timeBaseV);
videoInfo = demuxer->getInfo();
for (int i = 0; i < videoInfo.videoStreams.size(); ++i)
{
if (videoInfo.videoStreams.at(i).index == idxVideo)
idxVideoAtInfo = i;
}
for (int i = 0; i < videoInfo.audioStreams.size(); ++i)
{
if (videoInfo.audioStreams.at(i).index == idxAudio)
idxAudioAtInfo = i;
}
opened = true;
return true;
}
bool Decoder::open(VideoStream* stream_, double loopA, double loopB)
{
if (opened || !stream_)
return false;
//TODO:
return true;
}
bool Decoder::setup()
{
return setup(Size(rawWidth, rawHeight));
}
bool Decoder::setup(const Size& target_size)
{
if (img_convert_ctx)
{
// already setup
return false;
}
const int width = (int)target_size.width;
const int height = (int)target_size.height;
if (width <= 0 || height <= 0)
{
return false;
}
targetSize = Size(width, height);
// TODO: avoid sws
auto ret = av_image_alloc(sws_pointers, sws_linesizes,
width, height, swsFormat, 1);
if (ret < 0)
{
VERRO("can't alloc image");
return false;
}
auto dec = (ffmpeg::VideoDecoder*)demuxer->getDecoder(idxVideo);
auto srcFormat = pCodecCtx->pix_fmt;
if (dec->isHardware())
srcFormat = dec->getSoftwareFormat();
CC_ASSERT(srcFormat >= 0);
// scale/convert
img_convert_ctx = sws_getContext(
pCodecCtx->width,
pCodecCtx->height,
srcFormat,
width,
height,
swsFormat,
SWS_FAST_BILINEAR,
nullptr, nullptr, nullptr);
VINFO("image conversion: %s(%dx%d) -> %s(%dx%d)",
av_pix_fmt_desc_get(srcFormat)->name,
pCodecCtx->width,
pCodecCtx->height,
av_pix_fmt_desc_get(swsFormat)->name,
width,
height);
if (!img_convert_ctx)
{
VERRO("can't create sws context");
return false;
}
return true;
}
bool Decoder::isOpened() const
{
return opened;
}
void Decoder::close()
{
CC_SAFE_RELEASE_NULL(videoQueue);
CC_SAFE_RELEASE_NULL(audioQueue);
CC_SAFE_RELEASE_NULL(demuxer);
if(img_convert_ctx)
{
sws_freeContext(img_convert_ctx);
img_convert_ctx = nullptr;
}
if (sws_pointers[0])
{
av_freep(&sws_pointers[0]);
memset(sws_pointers, 0, sizeof(void*) * 4);
}
if (stream)
{
stream->release();
stream = nullptr;
}
if (pFormatCtx)
{
avformat_free_context(pFormatCtx);
pFormatCtx = nullptr;
}
opened = false;
}
uint32_t Decoder::read(uint8_t** vbuf)
{
*vbuf = nullptr;
if (currentFrame == (nFramesV - 1) || !opened)
return 0;
if (lastFrame == currentFrame)
{
if(vFrame)
*vbuf = sws_pointers[0];
return 0;
}
//bool ok = true;
while (videoQueue->empty())
{
if (!demuxer->step())
{
//ok = false;
break;
}
}
if (videoQueue->empty())
return 0;
vFrame = videoQueue->pop();
CC_ASSERT(vFrame);
sws_scale(img_convert_ctx,
vFrame->data, vFrame->linesize,
0, pCodecCtx->height,
sws_pointers, sws_linesizes);
lastFrame = currentFrame;
currentFrame++;
*vbuf = sws_pointers[0];
return 1;
}
AVPixelFormat Decoder::getReadFormat()
{
return swsFormat;
}
bool Decoder::seek(int64_t frameOffset)
{
if (!opened || frameOffset >= nFramesV || frameOffset < 0)
return false;
if (frameOffset == currentFrame)
{
lastFrame = currentFrame - 1;
return true;
}
if (frameOffset == currentFrame - 1)
{
lastFrame = currentFrame;
return true;
}
int64_t targetFrame = frameOffset == 0 ? 0 : frameOffset - 1;
const auto ok = demuxer->seekKeyFrame(idxVideo, targetFrame);
currentFrame = targetFrame;
return ok;
}
bool Decoder::playerSeekTime(double sec)
{
const auto target = sec / durationV * nFramesV;
const auto targetFrame = (int64_t)target;
if (!opened|| targetFrame >= nFramesV)
{
VINFO("failed");
return false;
}
do
{
const auto df = target - currentFrame;
if (targetFrame == currentFrame || (-0.5 < df && df < 0))
{
// no seek, will step to next frame
lastFrame = currentFrame - 1;
break;
}
if (targetFrame == currentFrame - 1)
{
// wait at current frame
lastFrame = currentFrame;
break;
}
if (df <= 0)
{
// wait at current frame until df > 0
lastFrame = currentFrame;
break;
}
else
{
// note: we need to seek forward here,
// but usually the game is not slower than video,
// and seek forward is not a good idea when game is slower,
// so we just step as usual
lastFrame = currentFrame - 1;
}
}
while (false);
return true;
}
int64_t Decoder::tell() const
{
return currentFrame;
}
int64_t Decoder::getTotalFrames() const
{
return nFramesV;
}
double Decoder::getVideoFrameRateForPlayer()
{
if (!videoInfo.videoStreams.empty())
return av_q2d(getVideoFrameRate());
return Director::getInstance()->getAnimationInterval();
}
Decoder::Decoder() : timeBaseV(), timeBaseA(), videoInfo()
{
}
Decoder::~Decoder()
{
close();
}
int64_t Decoder::getVideoFrameCount(int index) const
{
if (index < 0) index = idxVideoAtInfo;
if (index >= videoInfo.videoStreams.size())
return 0;
return videoInfo.videoStreams.at(index).numFrames;
}
AVRational Decoder::getVideoFrameRate(int index) const
{
if (index < 0) index = idxVideoAtInfo;
if (index >= videoInfo.videoStreams.size())
return av_make_q(0, 0);
return videoInfo.videoStreams.at(index).frameRate;
}
AVRational Decoder::getVideoTimeBase(int index) const
{
if (index < 0) index = idxVideoAtInfo;
if (index >= videoInfo.videoStreams.size())
return av_make_q(0, 0);
return videoInfo.videoStreams.at(index).timeBase;
}
int64_t Decoder::getVideoBitRate(int index) const {
if (index < 0) index = idxVideoAtInfo;
if(index >= videoInfo.videoStreams.size())
return 0;
return videoInfo.videoStreams.at(index).bitRate;
}
AVPixelFormat Decoder::getVideoFormat(int index) const
{
if (index < 0) index = idxVideoAtInfo;
if (index >= videoInfo.videoStreams.size())
return AV_PIX_FMT_NONE;
return videoInfo.videoStreams.at(index).format;
}
std::string Decoder::getVideoFormatName(int index) const
{
if (index < 0) index = idxVideoAtInfo;
if (index >= videoInfo.videoStreams.size())
return "";
const auto ret = videoInfo.videoStreams.at(index).formatName;
return ret ? ret : "";
}
Size Decoder::getVideoSize(int index) const
{
if (index < 0) index = idxVideoAtInfo;
if (index >= videoInfo.videoStreams.size())
return { 0,0 };
auto& v = videoInfo.videoStreams.at(index);
return { (float)v.width,(float)v.height };
}
int64_t Decoder::getAudioFrameCount(int index) const
{
if (index < 0) index = idxAudioAtInfo;
if (index >= videoInfo.audioStreams.size())
return 0;
return videoInfo.audioStreams.at(index).numFrames;
}
AVRational Decoder::getAudioTimeBase(int index) const
{
if (index < 0) index = idxAudioAtInfo;
if (index >= videoInfo.audioStreams.size())
return av_make_q(0, 0);
return videoInfo.audioStreams.at(index).timeBase;
}
int64_t Decoder::getAudioBitRate(int index) const
{
if (index < 0) index = idxAudioAtInfo;
if (index >= videoInfo.audioStreams.size())
return 0;
return videoInfo.audioStreams.at(index).bitRate;
}
int Decoder::getAudioSampleRate(int index) const
{
if (index < 0) index = idxAudioAtInfo;
if (index >= videoInfo.audioStreams.size())
return 0;
return videoInfo.audioStreams.at(index).sampleRate;
}
int Decoder::getAudioChannelCount(int index) const
{
if (index < 0) index = idxAudioAtInfo;
if (index >= videoInfo.audioStreams.size())
return 0;
return videoInfo.audioStreams.at(index).channels;
}
std::string Decoder::getAudioChannelLayoutName(int index) const
{
if (index < 0) index = idxAudioAtInfo;
if (index >= videoInfo.audioStreams.size())
return "";
auto& info = videoInfo.audioStreams.at(index);
const auto ret = info.channelLayoutName;
if (ret[sizeof(info.channelLayoutName) - 1] != 0) {
return { ret, sizeof(info.channelLayoutName) };
}
return ret;
}
double Decoder::getContainerDuration() const
{
return videoInfo.duration;
}
double Decoder::getContainerStartTime() const
{
return videoInfo.start;
}
int64_t Decoder::getContainerBitRate() const
{
return videoInfo.bitRate;
}
size_t Decoder::getVideoStreamCount() const
{
return videoInfo.videoStreams.size();
}
size_t Decoder::getAudioStreamCount() const
{
return videoInfo.audioStreams.size();
}
std::string Decoder::getVideoDecoderName() const
{
const auto ret = demuxer->getDecoder(idxVideo)->getCodec()->name;
return ret ? ret : "";
}
AVPixelFormat Decoder::getVideoDecoderHardwareFormat() const
{
const auto decoder = dynamic_cast<ffmpeg::VideoDecoder*>(demuxer->getDecoder(idxVideo));
return decoder ? decoder->getHardwareFormat() : AV_PIX_FMT_NONE;
}
AVPixelFormat Decoder::getVideoDecoderSoftwareFormat() const
{
const auto decoder = dynamic_cast<ffmpeg::VideoDecoder*>(demuxer->getDecoder(idxVideo));
return decoder ? decoder->getSoftwareFormat() : AV_PIX_FMT_NONE;
}
bool Decoder::isVideoDecoderHardware() const
{
const auto decoder = dynamic_cast<ffmpeg::VideoDecoder*>(demuxer->getDecoder(idxVideo));
return decoder ? decoder->isHardware() : false;
}
std::string Decoder::getAudioDecoderName() const
{
const auto decoder = demuxer->getDecoder(idxAudio);
if (!decoder)
return "";
const auto ret = decoder->getCodec()->name;
return ret ? ret : "";
}
std::string Decoder::getPixelFormatName(AVPixelFormat format)
{
const auto desc = av_pix_fmt_desc_get(format);
if (!desc)
return "";
return desc->name;
}
std::vector<std::string> Decoder::getAllDecoderNames()
{
std::vector<std::string> ret;
for (auto&& c : ffmpeg::Codec::getAllDecoders())
ret.emplace_back(c->name);
return ret;
}
std::string Decoder::queryDecoderLongName(const std::string& name)
{
const auto codec = avcodec_find_decoder_by_name(name.c_str());
if (!codec)
return "";
return codec->long_name ? codec->long_name : "";
}
std::string Decoder::queryDecoderType(const std::string& name)
{
const auto codec = avcodec_find_decoder_by_name(name.c_str());
if (!codec)
return "";
const auto ret = av_get_media_type_string(codec->type);
return ret ? ret : "";
}
std::string Decoder::queryDecoderID(const std::string& name)
{
const auto codec = avcodec_find_decoder_by_name(name.c_str());
if (!codec)
return "";
return avcodec_get_name(codec->id);
}
bool Decoder::queryDecoderSupportsHardware(const std::string& name)
{
const auto codec = avcodec_find_decoder_by_name(name.c_str());
if (!codec)
return false;
return avcodec_get_hw_config(codec, 0);
}