-
Notifications
You must be signed in to change notification settings - Fork 0
/
DVDPlayerVideo.cpp
1629 lines (1395 loc) · 49.9 KB
/
DVDPlayerVideo.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
/*
* Copyright (C) 2005-2012 Team XBMC
* http://www.xbmc.org
*
* 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 2, 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 XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "system.h"
#include "cores/VideoRenderers/RenderFlags.h"
#include "windowing/WindowingFactory.h"
#include "settings/AdvancedSettings.h"
#include "settings/GUISettings.h"
#include "settings/Settings.h"
#include "video/VideoReferenceClock.h"
#include "utils/MathUtils.h"
#include "DVDPlayer.h"
#include "DVDPlayerVideo.h"
#include "DVDCodecs/DVDFactoryCodec.h"
#include "DVDCodecs/DVDCodecUtils.h"
#include "DVDCodecs/Video/DVDVideoPPFFmpeg.h"
#include "DVDCodecs/Video/DVDVideoCodecFFmpeg.h"
#include "DVDDemuxers/DVDDemux.h"
#include "DVDDemuxers/DVDDemuxUtils.h"
#include "DVDOverlayRenderer.h"
#include "DVDPerformanceCounter.h"
#include "DVDCodecs/DVDCodecs.h"
#include "DVDCodecs/Overlay/DVDOverlayCodecCC.h"
#include "DVDCodecs/Overlay/DVDOverlaySSA.h"
#include <sstream>
#include <iomanip>
#include <numeric>
#include <iterator>
#include "utils/log.h"
using namespace std;
class CPulldownCorrection
{
public:
CPulldownCorrection()
{
m_duration = 0.0;
m_accum = 0;
m_total = 0;
m_next = m_pattern.end();
}
void init(double fps, int *begin, int *end)
{
std::copy(begin, end, std::back_inserter(m_pattern));
m_duration = DVD_TIME_BASE / fps;
m_accum = 0;
m_total = std::accumulate(m_pattern.begin(), m_pattern.end(), 0);
m_next = m_pattern.begin();
}
double pts()
{
double input = m_duration * std::distance(m_pattern.begin(), m_next);
double output = m_duration * m_accum / m_total;
return output - input;
}
double dur()
{
return m_duration * m_pattern.size() * *m_next / m_total;
}
void next()
{
m_accum += *m_next;
if(++m_next == m_pattern.end())
{
m_next = m_pattern.begin();
m_accum = 0;
}
}
bool enabled()
{
return m_pattern.size() > 0;
}
private:
double m_duration;
int m_total;
int m_accum;
std::vector<int> m_pattern;
std::vector<int>::iterator m_next;
};
class CDVDMsgVideoCodecChange : public CDVDMsg
{
public:
CDVDMsgVideoCodecChange(const CDVDStreamInfo &hints, CDVDVideoCodec* codec)
: CDVDMsg(GENERAL_STREAMCHANGE)
, m_codec(codec)
, m_hints(hints)
{}
~CDVDMsgVideoCodecChange()
{
delete m_codec;
}
CDVDVideoCodec* m_codec;
CDVDStreamInfo m_hints;
};
CDVDPlayerVideo::CDVDPlayerVideo( CDVDClock* pClock
, CDVDOverlayContainer* pOverlayContainer
, CDVDMessageQueue& parent)
: CThread("CDVDPlayerVideo")
, m_messageQueue("video")
, m_messageParent(parent)
{
m_pClock = pClock;
m_pOverlayContainer = pOverlayContainer;
m_pTempOverlayPicture = NULL;
m_pVideoCodec = NULL;
m_pOverlayCodecCC = NULL;
m_speed = DVD_PLAYSPEED_NORMAL;
m_bRenderSubs = false;
m_stalled = false;
m_started = false;
m_iVideoDelay = 0;
m_iSubtitleDelay = 0;
m_FlipTimeStamp = 0.0;
m_iLateFrames = 0;
m_iDroppedRequest = 0;
m_fForcedAspectRatio = 0;
m_iNrOfPicturesNotToSkip = 0;
m_messageQueue.SetMaxDataSize(40 * 1024 * 1024);
m_messageQueue.SetMaxTimeSize(12.0);
g_dvdPerformanceCounter.EnableVideoQueue(&m_messageQueue);
m_iCurrentPts = DVD_NOPTS_VALUE;
m_iDroppedFrames = 0;
m_fFrameRate = 25;
m_bCalcFrameRate = false;
m_fStableFrameRate = 0.0;
m_iFrameRateCount = 0;
m_bAllowDrop = false;
m_iFrameRateErr = 0;
m_iFrameRateLength = 0;
m_bFpsInvalid = false;
m_bAllowFullscreen = false;
m_droptime = 0.0;
m_dropbase = 0.0;
m_autosync = 1;
memset(&m_output, 0, sizeof(m_output));
}
CDVDPlayerVideo::~CDVDPlayerVideo()
{
StopThread();
g_dvdPerformanceCounter.DisableVideoQueue();
g_VideoReferenceClock.StopThread();
}
double CDVDPlayerVideo::GetOutputDelay()
{
double time = m_messageQueue.GetPacketCount(CDVDMsg::DEMUXER_PACKET);
if( m_fFrameRate )
time = (time * DVD_TIME_BASE) / m_fFrameRate;
else
time = 0.0;
if( m_speed != 0 )
time = time * DVD_PLAYSPEED_NORMAL / abs(m_speed);
return time;
}
bool CDVDPlayerVideo::OpenStream( CDVDStreamInfo &hint )
{
unsigned int surfaces = 0;
std::vector<ERenderFormat> formats;
#ifdef HAS_VIDEO_PLAYBACK
surfaces = g_renderManager.GetProcessorSize();
formats = g_renderManager.SupportedFormats();
#endif
CLog::Log(LOGNOTICE, "Creating video codec with codec id: %i", hint.codec);
CDVDVideoCodec* codec = CDVDFactoryCodec::CreateVideoCodec(hint, surfaces, formats);
if(!codec)
{
CLog::Log(LOGERROR, "Unsupported video codec");
return false;
}
if(g_guiSettings.GetBool("videoplayer.usedisplayasclock") && !g_VideoReferenceClock.IsRunning())
{
g_VideoReferenceClock.Create();
//we have to wait for the clock to start otherwise alsa can cause trouble
if (!g_VideoReferenceClock.WaitStarted(2000))
CLog::Log(LOGDEBUG, "g_VideoReferenceClock didn't start in time");
}
if(m_messageQueue.IsInited())
m_messageQueue.Put(new CDVDMsgVideoCodecChange(hint, codec), 0);
else
{
OpenStream(hint, codec);
CLog::Log(LOGNOTICE, "Creating video thread");
m_messageQueue.Init();
Create();
}
return true;
}
void CDVDPlayerVideo::OpenStream(CDVDStreamInfo &hint, CDVDVideoCodec* codec)
{
//reported fps is usually not completely correct
if (hint.fpsrate && hint.fpsscale)
m_fFrameRate = DVD_TIME_BASE / CDVDCodecUtils::NormalizeFrameduration((double)DVD_TIME_BASE * hint.fpsscale / hint.fpsrate);
else
m_fFrameRate = 25;
m_bFpsInvalid = (hint.fpsrate == 0 || hint.fpsscale == 0);
m_bCalcFrameRate = g_guiSettings.GetBool("videoplayer.usedisplayasclock") ||
g_guiSettings.GetInt("videoplayer.adjustrefreshrate") != ADJUST_REFRESHRATE_OFF;
ResetFrameRateCalc();
m_iDroppedRequest = 0;
m_iLateFrames = 0;
m_autosync = 1;
if( m_fFrameRate > 100 || m_fFrameRate < 5 )
{
CLog::Log(LOGERROR, "CDVDPlayerVideo::OpenStream - Invalid framerate %d, using forced 25fps and just trust timestamps", (int)m_fFrameRate);
m_fFrameRate = 25;
}
// use aspect in stream if available
if(hint.forced_aspect)
m_fForcedAspectRatio = hint.aspect;
else
m_fForcedAspectRatio = 0.0;
if (m_pVideoCodec)
delete m_pVideoCodec;
m_pVideoCodec = codec;
m_hints = hint;
m_stalled = m_messageQueue.GetPacketCount(CDVDMsg::DEMUXER_PACKET) == 0;
m_started = false;
m_codecname = m_pVideoCodec->GetName();
}
void CDVDPlayerVideo::CloseStream(bool bWaitForBuffers)
{
// wait until buffers are empty
if (bWaitForBuffers && m_speed > 0) m_messageQueue.WaitUntilEmpty();
m_messageQueue.Abort();
// wait for decode_video thread to end
CLog::Log(LOGNOTICE, "waiting for video thread to exit");
StopThread(); // will set this->m_bStop to true
m_messageQueue.End();
CLog::Log(LOGNOTICE, "deleting video codec");
if (m_pVideoCodec)
{
m_pVideoCodec->Dispose();
delete m_pVideoCodec;
m_pVideoCodec = NULL;
}
if (m_pTempOverlayPicture)
{
CDVDCodecUtils::FreePicture(m_pTempOverlayPicture);
m_pTempOverlayPicture = NULL;
}
//tell the clock we stopped playing video
m_pClock->UpdateFramerate(0.0);
}
void CDVDPlayerVideo::OnStartup()
{
m_iDroppedFrames = 0;
m_crop.x1 = m_crop.x2 = 0.0f;
m_crop.y1 = m_crop.y2 = 0.0f;
m_iCurrentPts = DVD_NOPTS_VALUE;
m_FlipTimeStamp = m_pClock->GetAbsoluteClock();
g_dvdPerformanceCounter.EnableVideoDecodePerformance(this);
}
void CDVDPlayerVideo::Process()
{
CLog::Log(LOGNOTICE, "running thread: video_thread");
DVDVideoPicture picture;
CPulldownCorrection pulldown;
CDVDVideoPPFFmpeg mPostProcess("");
CStdString sPostProcessType;
bool bPostProcessDeint = false;
memset(&picture, 0, sizeof(DVDVideoPicture));
double pts = 0;
double frametime = (double)DVD_TIME_BASE / m_fFrameRate;
int iDropped = 0; //frames dropped in a row
bool bRequestDrop = false;
m_videoStats.Start();
while (!m_bStop)
{
int iQueueTimeOut = (int)(m_stalled ? frametime / 4 : frametime * 10) / 1000;
int iPriority = (m_speed == DVD_PLAYSPEED_PAUSE && m_started) ? 1 : 0;
CDVDMsg* pMsg;
MsgQueueReturnCode ret = m_messageQueue.Get(&pMsg, iQueueTimeOut, iPriority);
if (MSGQ_IS_ERROR(ret) || ret == MSGQ_ABORT)
{
CLog::Log(LOGERROR, "Got MSGQ_ABORT or MSGO_IS_ERROR return true");
break;
}
else if (ret == MSGQ_TIMEOUT)
{
// if we only wanted priority messages, this isn't a stall
if( iPriority )
continue;
//Okey, start rendering at stream fps now instead, we are likely in a stillframe
if( !m_stalled )
{
if(m_started)
CLog::Log(LOGINFO, "CDVDPlayerVideo - Stillframe detected, switching to forced %f fps", m_fFrameRate);
m_stalled = true;
pts+= frametime*4;
}
//Waiting timed out, output last picture
if( picture.iFlags & DVP_FLAG_ALLOCATED )
{
//Remove interlaced flag before outputting
//no need to output this as if it was interlaced
picture.iFlags &= ~DVP_FLAG_INTERLACED;
picture.iFlags |= DVP_FLAG_NOSKIP;
OutputPicture(&picture, pts);
pts+= frametime;
}
continue;
}
if (pMsg->IsType(CDVDMsg::GENERAL_SYNCHRONIZE))
{
if(((CDVDMsgGeneralSynchronize*)pMsg)->Wait(100, SYNCSOURCE_VIDEO))
{
CLog::Log(LOGDEBUG, "CDVDPlayerVideo - CDVDMsg::GENERAL_SYNCHRONIZE");
/* we may be very much off correct pts here, but next picture may be a still*/
/* make sure it isn't dropped */
m_iNrOfPicturesNotToSkip = 5;
}
else
m_messageQueue.Put(pMsg->Acquire(), 1); /* push back as prio message, to process other prio messages */
pMsg->Release();
continue;
}
else if (pMsg->IsType(CDVDMsg::GENERAL_RESYNC))
{
CDVDMsgGeneralResync* pMsgGeneralResync = (CDVDMsgGeneralResync*)pMsg;
if(pMsgGeneralResync->m_timestamp != DVD_NOPTS_VALUE)
pts = pMsgGeneralResync->m_timestamp;
double delay = m_FlipTimeStamp - m_pClock->GetAbsoluteClock();
if( delay > frametime ) delay = frametime;
else if( delay < 0 ) delay = 0;
if(pMsgGeneralResync->m_clock)
{
CLog::Log(LOGDEBUG, "CDVDPlayerVideo - CDVDMsg::GENERAL_RESYNC(%f, 1)", pts);
m_pClock->Discontinuity(pts - delay);
}
else
CLog::Log(LOGDEBUG, "CDVDPlayerVideo - CDVDMsg::GENERAL_RESYNC(%f, 0)", pts);
pMsgGeneralResync->Release();
continue;
}
else if (pMsg->IsType(CDVDMsg::GENERAL_DELAY))
{
if (m_speed != DVD_PLAYSPEED_PAUSE)
{
double timeout = static_cast<CDVDMsgDouble*>(pMsg)->m_value;
CLog::Log(LOGDEBUG, "CDVDPlayerVideo - CDVDMsg::GENERAL_DELAY(%f)", timeout);
timeout *= (double)DVD_PLAYSPEED_NORMAL / abs(m_speed);
timeout += CDVDClock::GetAbsoluteClock();
while(!m_bStop && CDVDClock::GetAbsoluteClock() < timeout)
Sleep(1);
}
}
else if (pMsg->IsType(CDVDMsg::VIDEO_SET_ASPECT))
{
CLog::Log(LOGDEBUG, "CDVDPlayerVideo - CDVDMsg::VIDEO_SET_ASPECT");
m_fForcedAspectRatio = *((CDVDMsgDouble*)pMsg);
}
else if (pMsg->IsType(CDVDMsg::GENERAL_RESET))
{
if(m_pVideoCodec)
m_pVideoCodec->Reset();
picture.iFlags &= ~DVP_FLAG_ALLOCATED;
m_packets.clear();
m_started = false;
}
else if (pMsg->IsType(CDVDMsg::GENERAL_FLUSH)) // private message sent by (CDVDPlayerVideo::Flush())
{
if(m_pVideoCodec)
m_pVideoCodec->Reset();
picture.iFlags &= ~DVP_FLAG_ALLOCATED;
m_packets.clear();
m_pullupCorrection.Flush();
//we need to recalculate the framerate
//TODO: this needs to be set on a streamchange instead
ResetFrameRateCalc();
m_stalled = true;
m_started = false;
}
else if (pMsg->IsType(CDVDMsg::VIDEO_NOSKIP))
{
// libmpeg2 is also returning incomplete frames after a dvd cell change
// so the first few pictures are not the correct ones to display in some cases
// just display those together with the correct one.
// (setting it to 2 will skip some menu stills, 5 is working ok for me).
m_iNrOfPicturesNotToSkip = 5;
}
else if (pMsg->IsType(CDVDMsg::PLAYER_SETSPEED))
{
m_speed = static_cast<CDVDMsgInt*>(pMsg)->m_value;
if(m_speed == DVD_PLAYSPEED_PAUSE)
m_iNrOfPicturesNotToSkip = 0;
}
else if (pMsg->IsType(CDVDMsg::PLAYER_STARTED))
{
if(m_started)
m_messageParent.Put(new CDVDMsgInt(CDVDMsg::PLAYER_STARTED, DVDPLAYER_VIDEO));
}
else if (pMsg->IsType(CDVDMsg::GENERAL_STREAMCHANGE))
{
CDVDMsgVideoCodecChange* msg(static_cast<CDVDMsgVideoCodecChange*>(pMsg));
OpenStream(msg->m_hints, msg->m_codec);
msg->m_codec = NULL;
picture.iFlags &= ~DVP_FLAG_ALLOCATED;
}
if (pMsg->IsType(CDVDMsg::DEMUXER_PACKET))
{
DemuxPacket* pPacket = ((CDVDMsgDemuxerPacket*)pMsg)->GetPacket();
bool bPacketDrop = ((CDVDMsgDemuxerPacket*)pMsg)->GetPacketDrop();
if (m_stalled)
{
CLog::Log(LOGINFO, "CDVDPlayerVideo - Stillframe left, switching to normal playback");
m_stalled = false;
//don't allow the first frames after a still to be dropped
//sometimes we get multiple stills (long duration frames) after each other
//in normal mpegs
m_iNrOfPicturesNotToSkip = 5;
}
else if( iDropped*frametime > DVD_MSEC_TO_TIME(100) && m_iNrOfPicturesNotToSkip == 0 )
{ // if we dropped too many pictures in a row, insert a forced picture
m_iNrOfPicturesNotToSkip = 1;
}
#ifdef PROFILE
bRequestDrop = false;
#else
if (m_messageQueue.GetDataSize() == 0
|| m_speed < 0)
{
bRequestDrop = false;
m_iDroppedRequest = 0;
m_iLateFrames = 0;
}
#endif
// if player want's us to drop this packet, do so nomatter what
if(bPacketDrop)
bRequestDrop = true;
// tell codec if next frame should be dropped
// problem here, if one packet contains more than one frame
// both frames will be dropped in that case instead of just the first
// decoder still needs to provide an empty image structure, with correct flags
m_pVideoCodec->SetDropState(bRequestDrop);
// ask codec to do deinterlacing if possible
EDEINTERLACEMODE mDeintMode = g_settings.m_currentVideoSettings.m_DeinterlaceMode;
EINTERLACEMETHOD mInt = g_renderManager.AutoInterlaceMethod(g_settings.m_currentVideoSettings.m_InterlaceMethod);
unsigned int mFilters = 0;
if (mDeintMode != VS_DEINTERLACEMODE_OFF)
{
if (mInt == VS_INTERLACEMETHOD_DEINTERLACE)
mFilters = CDVDVideoCodec::FILTER_DEINTERLACE_ANY;
else if(mInt == VS_INTERLACEMETHOD_DEINTERLACE_HALF)
mFilters = CDVDVideoCodec::FILTER_DEINTERLACE_ANY | CDVDVideoCodec::FILTER_DEINTERLACE_HALFED;
if (mDeintMode == VS_DEINTERLACEMODE_AUTO && mFilters)
mFilters |= CDVDVideoCodec::FILTER_DEINTERLACE_FLAGGED;
}
if (!g_renderManager.Supports(RENDERFEATURE_ROTATION))
mFilters |= CDVDVideoCodec::FILTER_ROTATE;
mFilters = m_pVideoCodec->SetFilters(mFilters);
int iDecoderState = m_pVideoCodec->Decode(pPacket->pData, pPacket->iSize, pPacket->dts, pPacket->pts);
// buffer packets so we can recover should decoder flush for some reason
if(m_pVideoCodec->GetConvergeCount() > 0)
{
m_packets.push_back(DVDMessageListItem(pMsg, 0));
if(m_packets.size() > m_pVideoCodec->GetConvergeCount()
|| m_packets.size() * frametime > DVD_SEC_TO_TIME(10))
m_packets.pop_front();
}
m_videoStats.AddSampleBytes(pPacket->iSize);
// assume decoder dropped a picture if it didn't give us any
// picture from a demux packet, this should be reasonable
// for libavformat as a demuxer as it normally packetizes
// pictures when they come from demuxer
if(bRequestDrop && !bPacketDrop && (iDecoderState & VC_BUFFER) && !(iDecoderState & VC_PICTURE))
{
m_iDroppedFrames++;
iDropped++;
}
// loop while no error
while (!m_bStop)
{
// if decoder was flushed, we need to seek back again to resume rendering
if (iDecoderState & VC_FLUSHED)
{
CLog::Log(LOGDEBUG, "CDVDPlayerVideo - video decoder was flushed");
while(!m_packets.empty())
{
CDVDMsgDemuxerPacket* msg = (CDVDMsgDemuxerPacket*)m_packets.front().message->Acquire();
m_packets.pop_front();
// all packets except the last one should be dropped
// if prio packets and current packet should be dropped, this is likely a new reset
msg->m_drop = !m_packets.empty() || (iPriority > 0 && bPacketDrop);
m_messageQueue.Put(msg, iPriority + 10);
}
m_pVideoCodec->Reset();
m_packets.clear();
break;
}
// if decoder had an error, tell it to reset to avoid more problems
if (iDecoderState & VC_ERROR)
{
CLog::Log(LOGDEBUG, "CDVDPlayerVideo - video decoder returned error");
break;
}
// check for a new picture
if (iDecoderState & VC_PICTURE)
{
// try to retrieve the picture (should never fail!), unless there is a demuxer bug ofcours
m_pVideoCodec->ClearPicture(&picture);
if (m_pVideoCodec->GetPicture(&picture))
{
sPostProcessType.clear();
picture.iGroupId = pPacket->iGroupId;
if(picture.iDuration == 0.0)
picture.iDuration = frametime;
if(bPacketDrop)
picture.iFlags |= DVP_FLAG_DROPPED;
if (m_iNrOfPicturesNotToSkip > 0)
{
picture.iFlags |= DVP_FLAG_NOSKIP;
m_iNrOfPicturesNotToSkip--;
}
// validate picture timing,
// if both dts/pts invalid, use pts calulated from picture.iDuration
// if pts invalid use dts, else use picture.pts as passed
if (picture.dts == DVD_NOPTS_VALUE && picture.pts == DVD_NOPTS_VALUE)
picture.pts = pts;
else if (picture.pts == DVD_NOPTS_VALUE)
picture.pts = picture.dts;
/* use forced aspect if any */
if( m_fForcedAspectRatio != 0.0f )
picture.iDisplayWidth = (int) (picture.iDisplayHeight * m_fForcedAspectRatio);
//Deinterlace if codec said format was interlaced or if we have selected we want to deinterlace
//this video
if ((mDeintMode == VS_DEINTERLACEMODE_AUTO && (picture.iFlags & DVP_FLAG_INTERLACED)) || mDeintMode == VS_DEINTERLACEMODE_FORCE)
{
if(mInt == VS_INTERLACEMETHOD_SW_BLEND)
{
if (!sPostProcessType.empty())
sPostProcessType += ",";
sPostProcessType += g_advancedSettings.m_videoPPFFmpegDeint;
bPostProcessDeint = true;
}
}
if (g_settings.m_currentVideoSettings.m_PostProcess)
{
if (!sPostProcessType.empty())
sPostProcessType += ",";
// This is what mplayer uses for its "high-quality filter combination"
sPostProcessType += g_advancedSettings.m_videoPPFFmpegPostProc;
}
if (!sPostProcessType.empty())
{
mPostProcess.SetType(sPostProcessType, bPostProcessDeint);
if (mPostProcess.Process(&picture))
mPostProcess.GetPicture(&picture);
}
/* if frame has a pts (usually originiating from demux packet), use that */
if(picture.pts != DVD_NOPTS_VALUE)
{
if(pulldown.enabled())
picture.pts += pulldown.pts();
pts = picture.pts;
}
if(pulldown.enabled())
{
picture.iDuration = pulldown.dur();
pulldown.next();
}
if (picture.iRepeatPicture)
picture.iDuration *= picture.iRepeatPicture + 1;
#if 1
int iResult = OutputPicture(&picture, pts);
#elif 0
// testing NV12 rendering functions
DVDVideoPicture* pTempNV12Picture = CDVDCodecUtils::ConvertToNV12Picture(&picture);
int iResult = OutputPicture(pTempNV12Picture, pts);
CDVDCodecUtils::FreePicture(pTempNV12Picture);
#elif 0
// testing YUY2 or UYVY rendering functions
// WARNING: since this scales a full YV12 frame, weaving artifacts will show on interlaced content
// even with the deinterlacer on
DVDVideoPicture* pTempYUVPackedPicture = CDVDCodecUtils::ConvertToYUV422PackedPicture(&picture, RENDER_FMT_UYVY422);
//DVDVideoPicture* pTempYUVPackedPicture = CDVDCodecUtils::ConvertToYUV422PackedPicture(&picture, RENDER_FMT_YUYV422);
int iResult = OutputPicture(pTempYUVPackedPicture, pts);
CDVDCodecUtils::FreePicture(pTempYUVPackedPicture);
#endif
if(m_started == false)
{
m_codecname = m_pVideoCodec->GetName();
m_started = true;
m_messageParent.Put(new CDVDMsgInt(CDVDMsg::PLAYER_STARTED, DVDPLAYER_VIDEO));
}
// guess next frame pts. iDuration is always valid
if (m_speed != 0)
pts += picture.iDuration * m_speed / abs(m_speed);
if( iResult & EOS_ABORT )
{
//if we break here and we directly try to decode again wihout
//flushing the video codec things break for some reason
//i think the decoder (libmpeg2 atleast) still has a pointer
//to the data, and when the packet is freed that will fail.
iDecoderState = m_pVideoCodec->Decode(NULL, 0, DVD_NOPTS_VALUE, DVD_NOPTS_VALUE);
break;
}
if( (iResult & EOS_DROPPED) && !bPacketDrop )
{
m_iDroppedFrames++;
iDropped++;
}
else
iDropped = 0;
bRequestDrop = (iResult & EOS_VERYLATE) == EOS_VERYLATE;
}
else
{
CLog::Log(LOGWARNING, "Decoder Error getting videoPicture.");
m_pVideoCodec->Reset();
}
}
/*
if (iDecoderState & VC_USERDATA)
{
// found some userdata while decoding a frame
// could be closed captioning
DVDVideoUserData videoUserData;
if (m_pVideoCodec->GetUserData(&videoUserData))
{
ProcessVideoUserData(&videoUserData, pts);
}
}
*/
// if the decoder needs more data, we just break this loop
// and try to get more data from the videoQueue
if (iDecoderState & VC_BUFFER)
break;
// the decoder didn't need more data, flush the remaning buffer
iDecoderState = m_pVideoCodec->Decode(NULL, 0, DVD_NOPTS_VALUE, DVD_NOPTS_VALUE);
}
}
// all data is used by the decoder, we can safely free it now
pMsg->Release();
}
// we need to let decoder release any picture retained resources.
m_pVideoCodec->ClearPicture(&picture);
}
void CDVDPlayerVideo::OnExit()
{
g_dvdPerformanceCounter.DisableVideoDecodePerformance();
if (m_pOverlayCodecCC)
{
m_pOverlayCodecCC->Dispose();
m_pOverlayCodecCC = NULL;
}
CLog::Log(LOGNOTICE, "thread end: video_thread");
}
void CDVDPlayerVideo::ProcessVideoUserData(DVDVideoUserData* pVideoUserData, double pts)
{
// check userdata type
BYTE* data = pVideoUserData->data;
int size = pVideoUserData->size;
if (size >= 2)
{
if (data[0] == 'C' && data[1] == 'C')
{
data += 2;
size -= 2;
// closed captioning
if (!m_pOverlayCodecCC)
{
m_pOverlayCodecCC = new CDVDOverlayCodecCC();
CDVDCodecOptions options;
CDVDStreamInfo info;
if (!m_pOverlayCodecCC->Open(info, options))
{
delete m_pOverlayCodecCC;
m_pOverlayCodecCC = NULL;
}
}
if (m_pOverlayCodecCC)
{
m_pOverlayCodecCC->Decode(data, size, DVD_NOPTS_VALUE, DVD_NOPTS_VALUE);
CDVDOverlay* overlay;
while((overlay = m_pOverlayCodecCC->GetOverlay()) != NULL)
{
overlay->iGroupId = 0;
overlay->iPTSStartTime += pts;
if(overlay->iPTSStopTime != 0.0)
overlay->iPTSStopTime += pts;
m_pOverlayContainer->Add(overlay);
overlay->Release();
}
}
}
}
}
bool CDVDPlayerVideo::InitializedOutputDevice()
{
#ifdef HAS_VIDEO_PLAYBACK
return g_renderManager.IsStarted();
#else
return false;
#endif
}
void CDVDPlayerVideo::SetSpeed(int speed)
{
if(m_messageQueue.IsInited())
m_messageQueue.Put( new CDVDMsgInt(CDVDMsg::PLAYER_SETSPEED, speed), 1 );
else
m_speed = speed;
}
void CDVDPlayerVideo::StepFrame()
{
m_iNrOfPicturesNotToSkip++;
}
void CDVDPlayerVideo::Flush()
{
/* flush using message as this get's called from dvdplayer thread */
/* and any demux packet that has been taken out of queue need to */
/* be disposed of before we flush */
m_messageQueue.Flush();
m_messageQueue.Put(new CDVDMsg(CDVDMsg::GENERAL_FLUSH), 1);
}
int CDVDPlayerVideo::GetLevel()
{
int level = m_messageQueue.GetLevel();
// fast exit, if the message queue is full, we do not care about the codec queue.
if (level == 100)
return level;
// Now for the harder choices, the message queue could be time or size based.
// In order to return the proper summed level, we need to know which.
if (m_messageQueue.IsDataBased())
{
int datasize = m_messageQueue.GetDataSize();
if (m_pVideoCodec)
datasize += m_pVideoCodec->GetDataSize();
return min(100, (int)(100 * datasize / (m_messageQueue.GetMaxDataSize() * m_messageQueue.GetMaxTimeSize())));
}
else
{
double timesize = m_messageQueue.GetTimeSize();
if (m_pVideoCodec)
timesize += m_pVideoCodec->GetTimeSize();
return min(100, MathUtils::round_int(100.0 * m_messageQueue.GetMaxTimeSize() * timesize));
}
return level;
}
#ifdef HAS_VIDEO_PLAYBACK
void CDVDPlayerVideo::ProcessOverlays(DVDVideoPicture* pSource, double pts)
{
// remove any overlays that are out of time
if (m_started)
m_pOverlayContainer->CleanUp(pts - m_iSubtitleDelay);
enum EOverlay
{ OVERLAY_AUTO // select mode auto
, OVERLAY_GPU // render osd using gpu
, OVERLAY_BUF // render osd on buffer
} render = OVERLAY_AUTO;
if(pSource->format == RENDER_FMT_YUV420P)
{
if(g_Windowing.GetRenderQuirks() & RENDER_QUIRKS_MAJORMEMLEAK_OVERLAYRENDERER)
{
// for now use cpu for ssa overlays as it currently allocates and
// frees textures for each frame this causes a hugh memory leak
// on some mesa intel drivers
if(m_pOverlayContainer->ContainsOverlayType(DVDOVERLAY_TYPE_SPU)
|| m_pOverlayContainer->ContainsOverlayType(DVDOVERLAY_TYPE_IMAGE)
|| m_pOverlayContainer->ContainsOverlayType(DVDOVERLAY_TYPE_SSA) )
render = OVERLAY_BUF;
}
if(render == OVERLAY_BUF)
{
// rendering spu overlay types directly on video memory costs a lot of processing power.
// thus we allocate a temp picture, copy the original to it (needed because the same picture can be used more than once).
// then do all the rendering on that temp picture and finaly copy it to video memory.
// In almost all cases this is 5 or more times faster!.
if(m_pTempOverlayPicture && ( m_pTempOverlayPicture->iWidth != pSource->iWidth
|| m_pTempOverlayPicture->iHeight != pSource->iHeight))
{
CDVDCodecUtils::FreePicture(m_pTempOverlayPicture);
m_pTempOverlayPicture = NULL;
}
if(!m_pTempOverlayPicture)
m_pTempOverlayPicture = CDVDCodecUtils::AllocatePicture(pSource->iWidth, pSource->iHeight);
if(!m_pTempOverlayPicture)
return;
CDVDCodecUtils::CopyPicture(m_pTempOverlayPicture, pSource);
memcpy(pSource->data , m_pTempOverlayPicture->data , sizeof(pSource->data));
memcpy(pSource->iLineSize, m_pTempOverlayPicture->iLineSize, sizeof(pSource->iLineSize));
}
}
if(render == OVERLAY_AUTO)
render = OVERLAY_GPU;
VecOverlays overlays;
{
CSingleLock lock(*m_pOverlayContainer);
VecOverlays* pVecOverlays = m_pOverlayContainer->GetOverlays();
VecOverlaysIter it = pVecOverlays->begin();
//Check all overlays and render those that should be rendered, based on time and forced
//Both forced and subs should check timeing, pts == 0 in the stillframe case
while (it != pVecOverlays->end())
{
CDVDOverlay* pOverlay = *it++;
if(!pOverlay->bForced && !m_bRenderSubs)
continue;
if(pOverlay->iGroupId != pSource->iGroupId)
continue;
double pts2 = pOverlay->bForced ? pts : pts - m_iSubtitleDelay;
if((pOverlay->iPTSStartTime <= pts2 && (pOverlay->iPTSStopTime > pts2 || pOverlay->iPTSStopTime == 0LL)) || pts == 0)
{
if(pOverlay->IsOverlayType(DVDOVERLAY_TYPE_GROUP))
overlays.insert(overlays.end(), static_cast<CDVDOverlayGroup*>(pOverlay)->m_overlays.begin()
, static_cast<CDVDOverlayGroup*>(pOverlay)->m_overlays.end());
else
overlays.push_back(pOverlay);
}
}
for(it = overlays.begin(); it != overlays.end(); ++it)
{
double pts2 = (*it)->bForced ? pts : pts - m_iSubtitleDelay;
if (render == OVERLAY_GPU)
g_renderManager.AddOverlay(*it, pts2);
if (render == OVERLAY_BUF)
CDVDOverlayRenderer::Render(pSource, *it, pts2);
}
}
}
#endif
int CDVDPlayerVideo::OutputPicture(const DVDVideoPicture* src, double pts)
{
/* picture buffer is not allowed to be modified in this call */
DVDVideoPicture picture(*src);
DVDVideoPicture* pPicture = &picture;
#ifdef HAS_VIDEO_PLAYBACK
double config_framerate = m_bFpsInvalid ? 0.0 : m_fFrameRate;
/* check so that our format or aspect has changed. if it has, reconfigure renderer */
if (!g_renderManager.IsConfigured()
|| m_output.width != pPicture->iWidth
|| m_output.height != pPicture->iHeight
|| m_output.dwidth != pPicture->iDisplayWidth