forked from yuan631111976/qtavplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVDecoder.cpp
2073 lines (1838 loc) · 71.5 KB
/
AVDecoder.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "AVDecoder.h"
#include <QtMath>
#include <QDebug>
int readPacket(void *opaque, uint8_t *buf, int size){
//qDebug() << "readSize : " << size;
return 0;
}
int writePacket(void *opaque, uint8_t *buf, int size){
//qDebug() << "writeSize : " << size;
return 0;
}
int64_t seekPacket(void *opaque, int64_t offset, int whence){
//qDebug() << "seek : ";
return 0;
}
/******************************************
*
* Convert 32 bit float to a 4 byte array
*/
static unsigned char * FloatToByteArray(float f)
{
static unsigned char p[4];
unsigned char * x = reinterpret_cast<unsigned char *>(&f);
for (int i = 0; i < 4; ++i)
{
p[i] = x[i];
}
return p;
}
static void FloatArrayToByteArray(float f[], int float_count, unsigned char byte_array[])
{
for(int i = 0; i < float_count; i++)
{
unsigned char * x = reinterpret_cast<unsigned char *>(&f[i]);
for (int j = 0; j < 4; ++j)
{
byte_array[4 * i + j] = x[j];
}
}
}
static float ByteArrayToFloat(unsigned char foo[])
{
union test
{
unsigned char buf[4];
float number;
}test;
test.buf[0] = foo[0];
test.buf[1] = foo[1];
test.buf[2] = foo[2];
test.buf[3] = foo[3];
return test.number;
}
static void ByteArrayToFloatArray(unsigned char byte_array[], int byte_count, float float_array[])
{
union test
{
unsigned char buf[4];
float number;
}test;
for(int i=0;i < (byte_count / 4); i++)
{
test.buf[0] = byte_array[ i * 4 + 0];
test.buf[1] = byte_array[ i * 4 + 1];
test.buf[2] = byte_array[ i * 4 + 2];
test.buf[3] = byte_array[ i * 4 + 3];
float_array[i] = test.number;
}
}
static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
const enum AVPixelFormat *pix_fmts)
{
AVDecoder *opaque = (AVDecoder *) ctx->opaque;
const enum AVPixelFormat *p;
for (p = pix_fmts; *p != -1; p++) {
if (*p == opaque->mHWPixFormat)
{
// qDebug() << (*p) << ";" << opaque->mHWPixFormat;
// qDebug() << "Successed to get HW surface format.";
return *p;
}
}
// qDebug() << "Failed to get HW surface format.";
return AV_PIX_FMT_NONE;
}
static int lockmgr(void **mtx, enum AVLockOp op)
{
switch(op) {
case AV_LOCK_CREATE:{
QMutex *mutex = new QMutex;
*mtx = mutex;
return 0;
}
case AV_LOCK_OBTAIN:{
QMutex *mutex = (QMutex*)*mtx;
mutex->lock();
return 0;
}
case AV_LOCK_RELEASE:{
QMutex *mutex = (QMutex*)*mtx;
mutex->unlock();
return 0;
}
case AV_LOCK_DESTROY:{
QMutex *mutex = (QMutex*)*mtx;
delete mutex;
return 0;
}
}
return 1;
}
AVDecoder::AVDecoder()
: mAudioIndex(-1)
, mVideoIndex(-1)
, mFormatCtx(NULL)
, mAudioCodecCtx(NULL)
, mVideoCodecCtx(NULL)
, mAudioCodec(NULL)
, mVideoCodec(NULL)
, mHWFrame(NULL)
, mIsOpenAudioCodec(false)
, mIsOpenVideoCodec(false)
, mRotate(0)
, mIsReadFinish(false)
, mminimumBufferSize(3000)
, mmaximumBufferSize(1000 * 10)
, mIsDestroy(false)
, mIsVideoBuffered(false)
, mIsAudioBuffered(false)
, mIsSubtitleBuffered(false)
, mStatus(AVDefine::AVMediaStatus_UnknownStatus)
, mVideoSwsCtx(NULL)
, mIsSeek(false)
, mSeekTime(-1)
, mIsSeekd(true)
, mIsAudioSeeked(true)
, mIsVideoSeeked(true)
, mIsSubtitleSeeked(true)
, mIsAudioPlayed(true)
, mIsVideoPlayed(true)
, mIsSubtitlePlayed(true)
, mAvioCtx(NULL)
, mAvioBuffer(NULL)
, mLastTime(0)
, mLastPos(0)
, mPlayRate(AVDefine::AVPlaySpeedRate_Normal)
, mCallback(NULL)
, mAudioFrame(NULL)
, reciveFrame(NULL)
, mIsInit(false)
, mUseHw(false)
, mHWDeviceCtx(NULL)
, mFrameIndex(0)
, mIsVideoLoadedCompleted(true)
, mIsAudioLoadedCompleted(true)
, maxRenderListSize(10)
, mLastRenderItem(NULL)
, mIsNeedCallRenderFirstFrame(true)
, mIsAccompany(false)
, mAudioBufferSinkCtx(NULL)
, mAudioBufferSrcCtx(NULL)
, mAudioFilterGraph(NULL)
, mAudioChannelLayout(AVDefine::AVChannelLayout_Auto)
, mSourceSampleRate(8000)
, mRealPlayRate(1.0f)
, mdecodecMode(AVDefine::AVDecodeMode_Soft)
, mIsLiving(false)
, mAudioOutputs(NULL)
, mAudioInputs(NULL)
, mIsAudioFilterInited(false)
, mAudioFilterFrame(NULL)
{
#ifdef LIBAVUTIL_VERSION_MAJOR
#if (LIBAVUTIL_VERSION_MAJOR < 56)
avcodec_register_all();
avfilter_register_all();
av_register_all();
avformat_network_init();
#endif
#endif
av_log_set_callback(NULL);//不打印日志
av_lockmgr_register(lockmgr);
audioq.release();
videoq.release();
subtitleq.release();
initRenderList();
}
AVDecoder::~AVDecoder(){
mIsDestroy = true;
mProcessThread.stop();
int i = 0;
while(!mProcessThread.isRunning() && i++ < 200){
QThread::msleep(1);
}
release(true);
}
void AVDecoder::init(){
if(mIsInit)
return;
statusChanged(AVDefine::AVMediaStatus_Loading);
if(avformat_open_input(&mFormatCtx, mFilename.toStdString().c_str(), NULL, NULL) != 0)
{
qDebug() << "media open error : " << mFilename.toStdString().data();
statusChanged(AVDefine::AVMediaStatus_NoMedia);
return;
}
if(avformat_find_stream_info(mFormatCtx, NULL) < 0)
{
qDebug() << "media find stream error : " << mFilename.toStdString().data();
statusChanged(AVDefine::AVMediaStatus_InvalidMedia);
return;
}
mHasSubtitle = false;
/* 寻找视频流 */
int ret = av_find_best_stream(mFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &mVideoCodec, 0);
if (ret < 0) {
//qDebug() << "Cannot find a video stream in the input file";
}else{
mVideoIndex = ret;
mVideoCodecCtx = avcodec_alloc_context3(mVideoCodec);
if (!mVideoCodecCtx){
//qDebug() << "create video context fail!";
// qDebug() << "avformat_open_input 3";
statusChanged(AVDefine::AVMediaStatus_InvalidMedia);
}else{
avcodec_parameters_to_context(mVideoCodecCtx, mFormatCtx->streams[mVideoIndex]->codecpar);
mVideoCodecCtx->thread_count = 0;
#ifdef SUPPORT_HW
mHWConfigListMutex.lock();
mHWConfigList.clear();
for (int i = 0;; i++) {
const AVCodecHWConfig *config = avcodec_get_hw_config(mVideoCodec, i);
if (!config) {
break;
}
if ((config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) && config->device_type != AV_HWDEVICE_TYPE_NONE) {
mHWConfigList.push_back((AVCodecHWConfig *)config);
break;
}
}
mHWConfigListMutex.unlock();
#endif
initVideoContext();
if(mIsOpenVideoCodec){
if(mCallback){
mCallback->mediaHasVideoChanged();
}
videoq.setTimeBase(mFormatCtx->streams[mVideoIndex]->time_base);
videoq.init();
mIsVideoLoadedCompleted = false;
}else{
statusChanged(AVDefine::AVMediaStatus_InvalidMedia);
}
}
}
if(mCallback){
mCallback->mediaDurationChanged(mFormatCtx->duration / 1000);
}
// qDebug() << "DURATION time : " <<mFormatCtx->duration / 1000;
/* 寻找音频流 */
if(!getpreview()){
ret = av_find_best_stream(mFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &mAudioCodec, 0);
if (ret < 0) {
//qDebug() << "Cannot find a audio stream in the input file";
// statusChanged(AVDefine::MediaStatus_InvalidMedia);
}else{
mAudioIndex = ret;
/* create decoding context */
mAudioCodecCtx = avcodec_alloc_context3(mAudioCodec);
if (!mAudioCodecCtx){
statusChanged(AVDefine::AVMediaStatus_InvalidMedia);
//qDebug() << "create audio context fail!";
}else{
avcodec_parameters_to_context(mAudioCodecCtx, mFormatCtx->streams[mAudioIndex]->codecpar);
mIsOpenAudioCodec = true;
mAudioCodecCtx->thread_count = 0;
if(avcodec_open2(mAudioCodecCtx, mAudioCodec, NULL) < 0)
{
//qDebug() << "can't open audio codec";
statusChanged(AVDefine::AVMediaStatus_InvalidMedia);
mIsOpenAudioCodec = false;
}
if(mIsOpenAudioCodec){
if(mCallback){
mCallback->mediaHasAudioChanged();
}
}
}
}
}
// mIsOpenAudioCodec = false;
// mIsOpenVideoCodec = false;
if(!mIsOpenVideoCodec && !mIsOpenAudioCodec){ //如果即没有音频,也没有视频,则通知状态无效
statusChanged(AVDefine::AVMediaStatus_InvalidMedia);
release();//释放所有资源
return;
}
if(mIsOpenAudioCodec){
mAudioFrame = av_frame_alloc();
int sampleRate = mAudioCodecCtx->sample_rate;
mSourceSampleRate = sampleRate;
mSourceAudioFormat.setCodec("audio/pcm");
mSourceAudioFormat.setSampleRate(sampleRate * mRealPlayRate);
mSourceAudioFormat.setSampleType(QAudioFormat::SignedInt);
mSourceAudioFormat.setSampleSize(16);
mSourceAudioFormat.setByteOrder(QAudioFormat::LittleEndian);
mSourceAudioFormat.setChannelCount(mAudioCodecCtx->channels);
// if(!setAudioChannel(mAudioChannelLayout)){
// statusChanged(AVDefine::AVMediaStatus_InvalidMedia);
// }
initAudioFilter();
mIsAudioLoadedCompleted = false;
}else{
mSourceSampleRate = 8000;
mSourceAudioFormat.setCodec("audio/pcm");
mSourceAudioFormat.setSampleRate(8000 * mRealPlayRate);
mSourceAudioFormat.setChannelCount(1);
mSourceAudioFormat.setSampleType(QAudioFormat::SignedInt);
mSourceAudioFormat.setSampleSize(16);
mSourceAudioFormat.setByteOrder(QAudioFormat::LittleEndian);
if(mCallback){
mCallback->mediaUpdateAudioFormat(mSourceAudioFormat);
}
}
audioq.setTimeBase(mFormatCtx->streams[mAudioIndex]->time_base);
audioq.init();
mIsInit = true;
statusChanged(AVDefine::AVMediaStatus_Buffering);
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Decodec));
// mIsLiving = true;
// qDebug() << "----------------- : " << avio_find_protocol_name(mFilename.toStdString().c_str());
}
void AVDecoder::release(bool isDeleted){
// qDebug() << "-----------------1";
if(mFormatCtx != NULL){
avformat_close_input(&mFormatCtx);
avformat_free_context(mFormatCtx);
mFormatCtx = NULL;
}
// qDebug() << "-----------------2";
if(mAudioCodecCtx != NULL){
avcodec_close(mAudioCodecCtx);
avcodec_free_context(&mAudioCodecCtx);
mAudioCodecCtx = NULL;
}
// qDebug() << "-----------------3";
if(mVideoCodecCtx != NULL){
avcodec_close(mVideoCodecCtx);
avcodec_free_context(&mVideoCodecCtx);
mVideoCodecCtx = NULL;
}
// qDebug() << "-----------------4";
if(mAudioCodec != NULL){
av_free(mAudioCodec);
mAudioCodec = NULL;
}
// qDebug() << "-----------------5";
if(mVideoCodec != NULL){
av_free(mVideoCodec);
mVideoCodec = NULL;
}
// qDebug() << "-----------------8";
if(mAudioFrame != NULL){
av_frame_unref(mAudioFrame);
av_frame_free(&mAudioFrame);
mAudioFrame = NULL;
}
// qDebug() << "-----------------9";
if(reciveFrame != NULL){
av_frame_unref(reciveFrame);
av_frame_free(&reciveFrame);
reciveFrame = NULL;
}
if(mHWFrame != NULL){
av_frame_unref(mHWFrame);
av_frame_free(&mHWFrame);
mHWFrame = NULL;
}
if(mHWDeviceCtx != NULL){
av_buffer_unref(&mHWDeviceCtx);
av_free(mHWDeviceCtx);
mHWDeviceCtx = NULL;
}
// qDebug() << "-----------------10";
mIsOpenAudioCodec = false;
mIsOpenVideoCodec = false;
mRotate = 0;
mIsReadFinish = false;
if(!isDeleted){
mIsDestroy = false;
}
mIsVideoBuffered = false;
mIsAudioBuffered = false;
mIsSubtitleBuffered = false;
mStatus = AVDefine::AVMediaStatus_UnknownStatus;
if(mVideoSwsCtx != NULL){
sws_freeContext(mVideoSwsCtx);
mVideoSwsCtx = NULL;
}
releaseAudioFilter();
mIsSeek = false;
mSeekTime = -1;
mIsSeekd = true;
mIsAudioSeeked = true;
mIsVideoSeeked = true;
mIsSubtitleSeeked = true;
mIsAudioPlayed = true;
mIsVideoPlayed = true;
mIsSubtitlePlayed = true;
mIsAudioLoadedCompleted = true;
mIsVideoLoadedCompleted = true;
if(mAvioCtx != NULL){
av_free(mAvioCtx);
mAvioCtx = NULL;
}
if(mAvioBuffer != NULL){
delete mAvioBuffer;
mAvioBuffer = NULL;
}
mLastTime = 0;
mLastPos = 0;
audioq.release();
videoq.release();
subtitleq.release();
clearRenderList(isDeleted);
}
void AVDecoder::showFrameByPositionImpl(int time){
AVFormatContext *formatCtx = NULL;
AVCodec *videoCodec = NULL;
AVCodecContext *videoCodecCtx = NULL;
int videoIndex = -1;
int rotate = 0;
struct SwsContext *videoSwsCtx = NULL; //视频参数转换上下文
if(avformat_open_input(&formatCtx, mFilename.toStdString().c_str(), NULL, NULL) != 0)
{
return;
}
if(avformat_find_stream_info(formatCtx, NULL) < 0)
{
return;
}
/* 寻找视频流 */
int ret = av_find_best_stream(formatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &videoCodec, 0);
if (ret >= 0){
videoIndex = ret;
videoCodecCtx = avcodec_alloc_context3(videoCodec);
if (!videoCodecCtx){
}else{
avcodec_parameters_to_context(videoCodecCtx, formatCtx->streams[videoIndex]->codecpar);
videoCodecCtx->thread_count = 0;
if(avcodec_open2(videoCodecCtx, videoCodec, NULL) < 0)
{
AVDictionaryEntry *tag = NULL;
tag = av_dict_get(formatCtx->streams[videoIndex]->metadata, "rotate", tag, 0);
if(tag != NULL)
rotate = QString(tag->value).toInt();
switch (videoCodecCtx->pix_fmt) {
case AV_PIX_FMT_YUV420P :
case AV_PIX_FMT_YUVJ420P :
case AV_PIX_FMT_YUV422P :
case AV_PIX_FMT_YUVJ422P :
case AV_PIX_FMT_YUV444P :
case AV_PIX_FMT_YUVJ444P :
case AV_PIX_FMT_GRAY8 :
case AV_PIX_FMT_UYVY422 :
case AV_PIX_FMT_YUYV422 :
case AV_PIX_FMT_YUV420P10LE :
case AV_PIX_FMT_BGR24 :
case AV_PIX_FMT_RGB24 :
case AV_PIX_FMT_YUV410P :
case AV_PIX_FMT_YUV411P :
case AV_PIX_FMT_MONOWHITE :
case AV_PIX_FMT_MONOBLACK :
case AV_PIX_FMT_PAL8 :
case AV_PIX_FMT_UYYVYY411 :
case AV_PIX_FMT_BGR8 :
case AV_PIX_FMT_RGB8 :
case AV_PIX_FMT_NV12 :
case AV_PIX_FMT_NV21 :
case AV_PIX_FMT_ARGB :
case AV_PIX_FMT_RGBA :
case AV_PIX_FMT_ABGR :
case AV_PIX_FMT_BGRA :
case AV_PIX_FMT_GRAY16LE :
case AV_PIX_FMT_YUV440P :
case AV_PIX_FMT_YUVJ440P :
case AV_PIX_FMT_YUVA420P :
case AV_PIX_FMT_YUV420P16LE :
case AV_PIX_FMT_YUV422P16LE :
case AV_PIX_FMT_YUV444P16LE :
case AV_PIX_FMT_YUVA420P16LE :
case AV_PIX_FMT_YUVA422P16LE :
case AV_PIX_FMT_YUVA444P16LE :
case AV_PIX_FMT_YUV444P10LE :
break;
default: //AV_PIX_FMT_YUV420P 如果上面的格式不支持直接渲染,则转换成通用AV_PIX_FMT_YUV420P格式
videoSwsCtx = sws_getContext(
videoCodecCtx->width,
videoCodecCtx->height,
videoCodecCtx->pix_fmt,
videoCodecCtx->width,
videoCodecCtx->height,
AV_PIX_FMT_YUV420P,
SWS_BICUBIC,NULL,NULL,NULL);
break;
}
av_seek_frame(formatCtx,-1,time * 1000,AVSEEK_FLAG_BACKWARD);
AVPacket *pkt = av_packet_alloc();
while(av_read_frame(formatCtx, pkt) >= 0){
if(pkt->stream_index == videoIndex){ //视频
AVFrame *tempFrame = av_frame_alloc();
while(avcodec_receive_frame(mVideoCodecCtx, tempFrame) == 0){
av_frame_unref(tempFrame);
}
av_frame_free(&tempFrame);
av_packet_unref(pkt);
break;
}
av_packet_unref(pkt);
}
av_packet_free(&pkt);
}
}
}else{
}
}
void AVDecoder::decodec(){
if((!mIsOpenVideoCodec && !mIsOpenAudioCodec) || !mIsInit){//必须同时存在音频和视频才能播放
statusChanged(AVDefine::AVMediaStatus_InvalidMedia);
return;
}
if(mIsDestroy || mIsReadFinish)
{
return;
}
if(mIsSeek){
// qDebug() <<"------------- seek";
statusChanged(AVDefine::AVMediaStatus_Seeking);
av_seek_frame(mFormatCtx,-1,mSeekTime * 1000,AVSEEK_FLAG_BACKWARD);
// qDebug() << "------------------- av_seek_frame:" << mVideoCodecCtx->pts_correction_last_pts;
mIsSeek = false;
// mIsVideoSeeked = false;
// mIsAudioSeeked = false;
if(mHasSubtitle)
mIsSubtitleSeeked = false;
mIsSeekd = false;
}
AVPacket *pkt = av_packet_alloc();
int ret = av_read_frame(mFormatCtx, pkt);
if ((ret == AVERROR_EOF || avio_feof(mFormatCtx->pb))) { //读取完成
mIsReadFinish = true;
if(!mIsSeekd){
mIsSeekd = true;
statusChanged(AVDefine::AVMediaStatus_Buffered);
statusChanged(AVDefine::AVMediaStatus_Seeked);
statusChanged(AVDefine::AVMediaStatus_EndOfMedia);
}else{
if(mStatus == AVDefine::AVMediaStatus_Buffering){
statusChanged(AVDefine::AVMediaStatus_Buffered);
}
statusChanged(AVDefine::AVMediaStatus_EndOfMedia);
}
av_packet_unref(pkt);
av_freep(pkt);
return;
}
if (mFormatCtx->pb && mFormatCtx->pb->error)
{
av_packet_unref(pkt);
av_freep(pkt);
return;
}
if(ret < 0)
{
// qDebug() <<"------------------ :" << getError(ret);
av_packet_unref(pkt);
av_freep(pkt);
return;
}
pkt->pts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts;
mIsVideoSeeked = true;
mIsAudioSeeked = true;
// qDebug() << "------------------------------- decodec:" << pkt.stream_index << ":" << mFormatCtx->streams[pkt.stream_index]->codec->codec_type;
if (pkt->stream_index == mVideoIndex && mIsOpenVideoCodec){
// qDebug() <<"------------- video";
mIsVideoLoadedCompleted = false;
int currentTime = av_q2d(mFormatCtx->streams[mVideoIndex]->time_base ) * pkt->pts * 1000;
if(!mIsSeekd){
if(currentTime < mSeekTime){
mVideoCodecCtxMutex.lock();
ret = avcodec_send_packet(mVideoCodecCtx, pkt);
if(ret != 0){
}else{
AVFrame *tempFrame = av_frame_alloc();
while(avcodec_receive_frame(mVideoCodecCtx, tempFrame) == 0){
av_frame_unref(tempFrame);
}
av_frame_free(&tempFrame);
}
av_packet_unref(pkt);
av_freep(pkt);
pkt = NULL;
mVideoCodecCtxMutex.unlock();
mIsVideoSeeked = false;
}else{
videoq.put(pkt);
mIsVideoSeeked = true;
mIsVideoPlayed = false;
}
}else{
videoq.put(pkt);
mIsVideoPlayed = false;
}
if(pkt != NULL && pkt->pts == AV_NOPTS_VALUE){
mIsVideoLoadedCompleted = true;
}else{
mIsVideoLoadedCompleted = false;
}
if(mCallback && !hasAudio())
mCallback->mediaUpdateBufferSize(currentTime);
}else if(pkt->stream_index == mAudioIndex && mIsOpenAudioCodec && !getpreview()){ //预览时,不处理音频
mIsAudioLoadedCompleted = false;
qint64 audioTime = av_q2d(mFormatCtx->streams[mAudioIndex]->time_base ) * pkt->pts * 1000;
if(!mIsSeekd){
if(audioTime < mSeekTime){ //如果音频时间小于拖动的时间,则丢掉音频包
av_packet_unref(pkt);
av_freep(pkt);
pkt = NULL;
mIsAudioSeeked = false;
}else{
mIsAudioSeeked = true;
mIsAudioPlayed = false;
audioq.put(pkt);
}
}else{
mIsAudioPlayed = false;
audioq.put(pkt);
}
if(pkt != NULL && pkt->pts == AV_NOPTS_VALUE){
mIsAudioLoadedCompleted = true;
}else{
mIsAudioLoadedCompleted = false;
}
if(mCallback)
mCallback->mediaUpdateBufferSize(audioTime);
}
else if(mFormatCtx->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
// subtitleq.put(&pkt);
/*
if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_DVD_SUBTITLE");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_DVB_SUBTITLE");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_TEXT) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_TEXT");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_XSUB) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_XSUB");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_SSA) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_SSA");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_MOV_TEXT) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_MOV_TEXT");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_HDMV_PGS_SUBTITLE");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_DVB_TELETEXT");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_SRT) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_SRT");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_MICRODVD) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_MICRODVD");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_EIA_608) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_EIA_608");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_JACOSUB) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_JACOSUB");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_SAMI) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_SAMI");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_REALTEXT) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_REALTEXT");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_SUBVIEWER1) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_SUBVIEWER1");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_SUBVIEWER) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_SUBVIEWER");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_SUBRIP) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_SUBRIP");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_WEBVTT) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_WEBVTT");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_MPL2) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_MPL2");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_VPLAYER) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_VPLAYER");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_PJS) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_PJS");
} else if (mFormatCtx->streams[pkt->stream_index]->codec->codec_id == AV_CODEC_ID_ASS) {
qDebug("[nativeGetContentInfo] AVMEDIA_TYPE_SUBTITLE = AV_CODEC_ID_ASS");
}else{
qDebug("AVMEDIA_TYPE_SUBTITLE");
}
qDebug("AVMEDIA_TYPE_SUBTITLE");
qDebug() << "AVMEDIA_TYPE_SUBTITLE 222";*/
av_packet_unref(pkt);
av_freep(pkt);
pkt = NULL;
mIsSubtitleSeeked = true;
// mIsSubtitlePlayed = false;
// qDebug() << "------------------------------- AVMEDIA_TYPE_SUBTITLE";
// qDebug() << "------------------------------- AVMEDIA_TYPE_SUBTITLE:" << pkt.stream_index << ":" << AVMEDIA_TYPE_SUBTITLE;
}
else if(mFormatCtx->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_DATA){
// qDebug() << "------------------------------- AVMEDIA_TYPE_DATA";
av_packet_unref(pkt);
av_freep(pkt);
pkt = NULL;
mIsAudioLoadedCompleted = false;
mIsVideoLoadedCompleted = false;
}
else{
av_packet_unref(pkt);
av_freep(pkt);
pkt = NULL;
mIsAudioLoadedCompleted = false;
mIsVideoLoadedCompleted = false;
}
if(!mIsLiving){
if((!mIsVideoSeeked && hasVideo()) || (!mIsAudioSeeked && hasAudio()) || (!mIsSubtitleSeeked && mHasSubtitle)){ //如果其中某一个元素未完成拖动,则继续解码s
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Decodec));
}else{
//判断是否将设定的缓存装满,如果未装满的话,一直循环
int videoDiffTime = videoq.diffTime(),audioDiffTime = audioq.diffTime();
if(((videoDiffTime < mminimumBufferSize && mIsOpenVideoCodec && !mIsVideoLoadedCompleted) ||
(audioDiffTime < mminimumBufferSize && mIsOpenAudioCodec && !mIsAudioLoadedCompleted))
&& videoDiffTime < mminimumBufferSize && audioDiffTime < mminimumBufferSize
){//只要有一种流装满,则不继续处理
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Decodec));
}else{
if(videoq.size() == 0){
mIsVideoLoadedCompleted = true;
}
if(audioq.size() == 0){
mIsAudioLoadedCompleted = true;
}
if(!mIsSeekd){
mIsSeekd = true;
statusChanged(AVDefine::AVMediaStatus_Buffered);
statusChanged(AVDefine::AVMediaStatus_Seeked);
}
if(mStatus == AVDefine::AVMediaStatus_Buffering){
statusChanged(AVDefine::AVMediaStatus_Buffered);
}
}
if((videoDiffTime < mmaximumBufferSize && !mIsVideoLoadedCompleted) || (audioDiffTime < mmaximumBufferSize && !mIsAudioLoadedCompleted)){
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Decodec));
}
}
}else{
// qDebug() << "-------------------------- AVCodecTaskCommand_Decodec";
statusChanged(AVDefine::AVMediaStatus_Buffered);
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Decodec));
}
if(!mTime.isValid())
mTime.start();
int elapsed = mTime.elapsed();
if(elapsed - mLastTime >= 1000){ //1秒钟
// qDebug() << "bytes_read : " << mFormatCtx->pb->bytes_read
// << " pos : " << mFormatCtx->pb->pos
// << " max size : " << mFormatCtx->pb->maxsize
// << " written : " << mFormatCtx->pb->written
// << " download speed : " << (mFormatCtx->pb->pos - mLastPos) / 1024 << "kb/s";
//emit updateInternetSpeed(mFormatCtx->pb->pos - mLastPos > 0 ? mFormatCtx->pb->pos - mLastPos : 0);
mLastTime = 0;
mTime.restart();
if(mFormatCtx->pb != NULL)
mLastPos = mFormatCtx->pb->pos;
}
}
void AVDecoder::setFilename(const QString &source){
mProcessThread.clearAllTask(); //清除所有任务
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_SetFileName,0,source));
}
void AVDecoder::setFilenameImpl(const QString &source){
mProcessThread.clearAllTask(); //清除所有任务
if(mFilename.size() > 0){
clearRenderList();
release();//释放所有资源
}
mFilename = source;
mIsInit = false;
mIsNeedCallRenderFirstFrame = true;
load();
}
void AVDecoder::stop(){
mAudioBufferMutex.lock();
//清除音频buffer
mAudioBuffer.clear();
mAudioBufferMutex.unlock();
}
void AVDecoder::setMediaCallback(AVMediaCallback *callback){
this->mCallback = callback;
}
void AVDecoder::load(){
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Init));
}
/** 设置播放速率,最大为8,最小为1.0 / 8 */
void AVDecoder::setPlayRate(int rate){
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_SetPlayRate,rate));
}
int AVDecoder::getPlayRate(){
return this->mPlayRate;
}
bool AVDecoder::getAccompany()const{
return mIsAccompany;
}
void AVDecoder::setAccompany(bool flag){
mIsAccompany = flag;
}
void AVDecoder::seek(int ms){
if(isLiving())
return;
mProcessThread.clearAllTask();
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Seek,ms));
}
void AVDecoder::checkBuffer(){
if(mIsLiving)
return;
if(!mIsReadFinish){
//判断是否将设定的缓存装满,如果未装满的话,一直循环
//if(mIsOpenVideoCodec && !mIsVideoLoadedCompleted){
if(mIsOpenVideoCodec){
if(videoq.diffTime() < mmaximumBufferSize){
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Decodec));
}
}
//if(mIsOpenAudioCodec && !mIsAudioLoadedCompleted){
if(mIsOpenAudioCodec){
if(audioq.diffTime() < mmaximumBufferSize ){
mProcessThread.addTask(new AVCodecTask(this,AVCodecTask::AVCodecTaskCommand_Decodec));
}
}
}
}
void AVDecoder::releseCurrentRenderItem(){
if(mLastRenderItem != NULL && mLastRenderItem->isRendered && mLastRenderItem->valid){
mLastRenderItem->mutex.lock();
mVideoSwsCtxMutex.lock();
mLastRenderItem->release();
mVideoSwsCtxMutex.unlock();
mLastRenderItem->mutex.unlock();
}
}
void AVDecoder::resetVideoCodecContext(){
mVideoCodecCtxMutex.lock();
if(mVideoCodecCtx != NULL){
if(avcodec_is_open(mVideoCodecCtx)){
avcodec_close(mVideoCodecCtx);
if(avcodec_open2(mVideoCodecCtx, mVideoCodec, NULL) < 0)
{
mIsOpenVideoCodec = false;
}
}
}
mVideoCodecCtxMutex.unlock();
}
/** 渲染第一帧 */
void AVDecoder::renderFirstFrame(){
mRenderListMutex.lock();
int minTime = -1;
RenderItem *render = NULL;
for(int i = 0,len = mRenderList.size();i < len;i++){
RenderItem *item = mRenderList[i];
item->mutex.lock();
if(item->valid){
if(minTime == -1){
minTime = item->pts;
}
if(item->pts <= minTime){
minTime = item->pts;
render = item;
}
}
item->mutex.unlock();
}
if(render != NULL){
if(mCallback){
releseCurrentRenderItem();
vFormat.renderFrame = render->frame;
vFormat.renderFrameMutex = &render->mutex;
render->isRendered = true;
mCallback->mediaUpdateVideoFrame((void *)&vFormat);
mLastRenderItem = render;
}
}
mRenderListMutex.unlock();
}
/** 渲染下一帧 */
void AVDecoder::checkRenderList(){
if(mIsDestroy)
return;