/
mythvideooutopengl.cpp
935 lines (814 loc) · 31.8 KB
/
mythvideooutopengl.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
// C/C++
#include <utility>
// MythTV
#include "mythcontext.h"
#include "mythmainwindow.h"
#include "mythplayer.h"
#include "videodisplayprofile.h"
#include "osd.h"
#include "mythuihelper.h"
#include "opengl/mythopenglperf.h"
#include "opengl/mythrenderopengl.h"
#include "opengl/mythpainteropengl.h"
#include "mythcodeccontext.h"
#include "mythopenglinterop.h"
#include "mythvideooutopengl.h"
#define LOC QString("VidOutGL: ")
/*! \brief Generate the list of available OpenGL profiles
*
* \note This could be improved by eliminating unsupported profiles at run time -
* but it is currently called statically and hence options would be fixed and unable
* to reflect changes in UI render device.
*/
void MythVideoOutputOpenGL::GetRenderOptions(RenderOptions &Options)
{
QStringList safe;
safe << "opengl" << "opengl-yv12";
// all profiles can handle all software frames
(*Options.safe_renderers)["dummy"].append(safe);
(*Options.safe_renderers)["nuppel"].append(safe);
if (Options.decoders->contains("ffmpeg"))
(*Options.safe_renderers)["ffmpeg"].append(safe);
if (Options.decoders->contains("mediacodec-dec"))
(*Options.safe_renderers)["mediacodec-dec"].append(safe);
if (Options.decoders->contains("vaapi-dec"))
(*Options.safe_renderers)["vaapi-dec"].append(safe);
if (Options.decoders->contains("vdpau-dec"))
(*Options.safe_renderers)["vdpau-dec"].append(safe);
if (Options.decoders->contains("nvdec-dec"))
(*Options.safe_renderers)["nvdec-dec"].append(safe);
if (Options.decoders->contains("vtb-dec"))
(*Options.safe_renderers)["vtb-dec"].append(safe);
if (Options.decoders->contains("v4l2-dec"))
(*Options.safe_renderers)["v4l2-dec"].append(safe);
if (Options.decoders->contains("mmal-dec"))
(*Options.safe_renderers)["mmal-dec"].append(safe);
// OpenGL UYVY
Options.renderers->append("opengl");
Options.priorities->insert("opengl", 65);
// OpenGL YV12
Options.renderers->append("opengl-yv12");
Options.priorities->insert("opengl-yv12", 65);
#if defined(USING_VAAPI) || defined (USING_VTB) || defined (USING_MEDIACODEC) || defined (USING_VDPAU) || defined (USING_NVDEC) || defined (USING_MMAL) || defined (USING_V4L2PRIME) || defined (USING_EGL)
Options.renderers->append("opengl-hw");
(*Options.safe_renderers)["dummy"].append("opengl-hw");
(*Options.safe_renderers)["nuppel"].append("opengl-hw");
Options.priorities->insert("opengl-hw", 110);
#endif
#ifdef USING_VAAPI
if (Options.decoders->contains("vaapi"))
(*Options.safe_renderers)["vaapi"].append("opengl-hw");
#endif
#ifdef USING_VTB
if (Options.decoders->contains("vtb"))
(*Options.safe_renderers)["vtb"].append("opengl-hw");
#endif
#ifdef USING_MEDIACODEC
if (Options.decoders->contains("mediacodec"))
(*Options.safe_renderers)["mediacodec"].append("opengl-hw");
#endif
#ifdef USING_VDPAU
if (Options.decoders->contains("vdpau"))
(*Options.safe_renderers)["vdpau"].append("opengl-hw");
#endif
#ifdef USING_NVDEC
if (Options.decoders->contains("nvdec"))
(*Options.safe_renderers)["nvdec"].append("opengl-hw");
#endif
#ifdef USING_MMAL
if (Options.decoders->contains("mmal"))
(*Options.safe_renderers)["mmal"].append("opengl-hw");
#endif
#ifdef USING_V4L2PRIME
if (Options.decoders->contains("v4l2"))
(*Options.safe_renderers)["v4l2"].append("opengl-hw");
#endif
#ifdef USING_EGL
if (Options.decoders->contains("drmprime"))
(*Options.safe_renderers)["drmprime"].append("opengl-hw");
#endif
}
MythVideoOutputOpenGL::MythVideoOutputOpenGL(QString Profile)
: m_videoProfile(std::move(Profile))
{
// Setup display switching
if (gCoreContext->GetBoolSetting("UseVideoModes", false))
m_displayRes = DisplayRes::AcquireRelease();
// Retrieve render context
m_render = MythRenderOpenGL::GetOpenGLRender();
if (!m_render)
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to retrieve OpenGL context");
return;
}
// Retain and lock
m_render->IncrRef();
OpenGLLocker locker(m_render);
// enable performance monitoring if requested
// will report the execution time for the key GL code blocks
// N.B. 'Upload' should always be zero when using hardware decoding and direct
// rendering. Any copy cost for direct rendering will be included within 'Render'
if (VERBOSE_LEVEL_CHECK(VB_GPUVIDEO, LOG_INFO))
{
m_openGLPerf = new MythOpenGLPerf("GLVidPerf: ", { "Upload:", "Clear:", "Render:", "Flush:", "Swap:" });
if (!m_openGLPerf->isCreated())
{
delete m_openGLPerf;
m_openGLPerf = nullptr;
}
}
// Disallow unsupported video texturing on GLES2
if (m_render->isOpenGLES() && m_render->format().majorVersion() < 3)
{
LOG(VB_GENERAL, LOG_INFO, LOC + "Disabling unsupported texture formats for GLES2");
m_isGLES2 = true;
}
// Retrieve OpenGL painter
MythMainWindow *win = MythMainWindow::getMainWindow();
m_openGLPainter = dynamic_cast<MythOpenGLPainter*>(win->GetCurrentPainter());
if (!m_openGLPainter)
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to get painter");
return;
}
// we need to control buffer swapping
m_openGLPainter->SetSwapControl(false);
// Create OpenGLVideo
QRect dvr = GetDisplayVisibleRect();
m_openGLVideo = new MythOpenGLVideo(m_render, &m_videoColourSpace, m_window.GetVideoDim(),
m_window.GetVideoDispDim(), dvr, m_window.GetDisplayVideoRect(),
m_window.GetVideoRect(), true, m_videoProfile);
// Connect VideoOutWindow to OpenGLVideo
QObject::connect(&m_window, &VideoOutWindow::VideoSizeChanged, m_openGLVideo, &MythOpenGLVideo::SetVideoDimensions);
QObject::connect(&m_window, &VideoOutWindow::VideoRectsChanged, m_openGLVideo, &MythOpenGLVideo::SetVideoRects);
QObject::connect(&m_window, &VideoOutWindow::WindowRectChanged, m_openGLVideo, &MythOpenGLVideo::SetViewportRect);
}
MythVideoOutputOpenGL::~MythVideoOutputOpenGL()
{
DestroyBuffers();
while (!m_openGLVideoPiPs.empty())
{
delete *m_openGLVideoPiPs.begin();
m_openGLVideoPiPs.erase(m_openGLVideoPiPs.begin());
}
m_openGLVideoPiPsReady.clear();
if (m_openGLPainter)
m_openGLPainter->SetSwapControl(true);
delete m_openGLVideo;
if (m_render)
{
m_render->makeCurrent();
delete m_openGLPerf;
m_render->doneCurrent();
m_render->DecrRef();
}
m_render = nullptr;
}
void MythVideoOutputOpenGL::DestroyBuffers(void)
{
DiscardFrames(true, true);
m_videoBuffers.DeleteBuffers();
m_videoBuffers.Reset();
m_buffersCreated = false;
}
bool MythVideoOutputOpenGL::Init(const QSize &VideoDim, const QSize &VideoDispDim, float Aspect,
WId /*WinID*/, const QRect &DisplayVisibleRect, MythCodecID CodecId)
{
if (!m_render || !m_openGLPainter || !m_openGLVideo)
return false;
if (!gCoreContext->IsUIThread())
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Cannot initialise OpenGL video from this thread");
return false;
}
OpenGLLocker ctx_lock(m_render);
// if we are the main video player then free up as much video memory
// as possible at startup
PIPState pip = m_window.GetPIPState();
if ((kCodec_NONE == m_newCodecId) && ((kPIPOff == pip) || (kPBPLeft == pip)))
m_openGLPainter->FreeResources();
// Default initialisation - mainly VideoOutWindow
if (!MythVideoOutput::Init(VideoDim, VideoDispDim, Aspect, 0, DisplayVisibleRect, CodecId))
return false;
// Ensure any new profile preferences are handled after a stream change
if (m_dbDisplayProfile)
m_openGLVideo->SetProfile(m_dbDisplayProfile->GetVideoRenderer());
// Set default support for picture attributes
InitPictureAttributes();
// Setup display
QSize size = m_window.GetVideoDim();
// Set the display mode if required
if (m_displayRes && !m_window.IsEmbedding())
ResizeForVideo(size.width(), size.height());
InitDisplayMeasurements();
// Create buffers
if (!CreateBuffers(CodecId, m_window.GetVideoDim()))
return false;
// Adjust visible rect for embedding
QRect dvr = GetDisplayVisibleRect();
if (m_videoCodecID == kCodec_NONE)
{
m_render->SetViewPort(QRect(QPoint(), dvr.size()));
return true;
}
if (m_window.GetPIPState() >= kPIPStandAlone)
{
QRect tmprect = QRect(QPoint(0,0), dvr.size());
ResizeDisplayWindow(tmprect, true);
}
// Reset OpenGLVideo
if (m_openGLVideo->IsValid())
m_openGLVideo->ResetFrameFormat();
// This works around an issue with VDPAU direct rendering using legacy drivers
m_render->Flush();
return true;
}
bool MythVideoOutputOpenGL::InputChanged(const QSize &VideoDim, const QSize &VideoDispDim,
float Aspect, MythCodecID CodecId, bool &AspectOnly,
MythMultiLocker* /*Locks*/, int ReferenceFrames,
bool ForceChange)
{
QSize currentvideodim = m_window.GetVideoDim();
QSize currentvideodispdim = m_window.GetVideoDispDim();
MythCodecID currentcodec = m_videoCodecID;
float currentaspect = m_window.GetVideoAspect();
if (m_newCodecId != kCodec_NONE)
{
// InputChanged has been called twice in quick succession without a call to ProcessFrame
currentvideodim = m_newVideoDim;
currentvideodispdim = m_newVideoDispDim;
currentcodec = m_newCodecId;
currentaspect = m_newAspect;
}
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Video changed: %1x%2 (%3x%4) '%5' (Aspect %6 Refs %13)"
"-> %7x%8 (%9x%10) '%11' (Aspect %12 Refs %14)")
.arg(currentvideodispdim.width()).arg(currentvideodispdim.height())
.arg(currentvideodim.width()).arg(currentvideodim.height())
.arg(toString(currentcodec)).arg(static_cast<double>(currentaspect))
.arg(VideoDispDim.width()).arg(VideoDispDim.height())
.arg(VideoDim.width()).arg(VideoDim.height())
.arg(toString(CodecId)).arg(static_cast<double>(Aspect))
.arg(m_maxReferenceFrames).arg(ReferenceFrames));
bool cidchanged = (CodecId != currentcodec);
bool reschanged = (VideoDispDim != currentvideodispdim);
bool refschanged = m_maxReferenceFrames != ReferenceFrames;
// aspect ratio changes are a no-op as changes are handled at display time
if (!(cidchanged || reschanged || refschanged || ForceChange))
{
AspectOnly = true;
return true;
}
// N.B. We no longer check for interop support for the new codec as it is a
// poor substitute for a full check of decoder capabilities etc. Better to let
// hardware decoding fail if necessary - which should at least fallback to
// software decoding rather than bailing out here.
// delete and recreate the buffers and flag that the input has changed
m_maxReferenceFrames = ReferenceFrames;
m_videoBuffers.BeginLock(kVideoBuffer_all);
DestroyBuffers();
m_buffersCreated = CreateBuffers(CodecId, VideoDim);
m_videoBuffers.EndLock();
if (!m_buffersCreated)
return false;
m_newCodecId= CodecId;
m_newVideoDim = VideoDim;
m_newVideoDispDim = VideoDispDim;
m_newAspect = Aspect;
return true;
}
QRect MythVideoOutputOpenGL::GetDisplayVisibleRect(void)
{
QRect dvr = m_window.GetDisplayVisibleRect();
MythMainWindow *mainwin = GetMythMainWindow();
if (!mainwin)
return dvr;
QSize size = mainwin->size();
// may be called before m_window is initialised fully
if (dvr.isEmpty())
dvr = QRect(QPoint(0, 0), size);
// If the Video screen mode has vertically less pixels
// than the GUI screen mode - OpenGL coordinate adjustments
// must be made to put the video at the top of the display
// area instead of at the bottom.
if (dvr.height() < size.height())
dvr.setTop(dvr.top() - size.height() + dvr.height());
// If the Video screen mode has horizontally less pixels
// than the GUI screen mode - OpenGL width must be set
// as the higher GUI width so that the Program Guide
// invoked from playback is not cut off.
if (dvr.width() < size.width())
dvr.setWidth(size.width());
return dvr;
}
bool MythVideoOutputOpenGL::CreateBuffers(MythCodecID CodecID, QSize Size)
{
if (m_buffersCreated)
return true;
if (codec_is_copyback(CodecID))
{
m_videoBuffers.Init(VideoBuffers::GetNumBuffers(FMT_NONE), false, 1, 4, 2);
return m_videoBuffers.CreateBuffers(FMT_YV12, Size.width(), Size.height());
}
if (codec_is_mediacodec(CodecID))
return m_videoBuffers.CreateBuffers(FMT_MEDIACODEC, Size, false, 1, 2, 2);
if (codec_is_vaapi(CodecID))
return m_videoBuffers.CreateBuffers(FMT_VAAPI, Size, false, 2, 1, 4, m_maxReferenceFrames);
if (codec_is_vtb(CodecID))
return m_videoBuffers.CreateBuffers(FMT_VTB, Size, false, 1, 4, 2);
if (codec_is_vdpau(CodecID))
return m_videoBuffers.CreateBuffers(FMT_VDPAU, Size, false, 2, 1, 4, m_maxReferenceFrames);
if (codec_is_nvdec(CodecID))
return m_videoBuffers.CreateBuffers(FMT_NVDEC, Size, false, 2, 1, 4);
if (codec_is_mmal(CodecID))
return m_videoBuffers.CreateBuffers(FMT_MMAL, Size, false, 2, 1, 4);
if (codec_is_v4l2(CodecID) || codec_is_drmprime(CodecID))
return m_videoBuffers.CreateBuffers(FMT_DRMPRIME, Size, false, 2, 1, 4);
return m_videoBuffers.CreateBuffers(FMT_YV12, Size, false, 1, 8, 4, m_maxReferenceFrames);
}
void MythVideoOutputOpenGL::ProcessFrame(VideoFrame *Frame, OSD */*osd*/,
const PIPMap &PiPPlayers,
FrameScanType Scan)
{
if (!m_render)
return;
OpenGLLocker ctx_lock(m_render);
// start the first timer
if (m_openGLPerf)
m_openGLPerf->RecordSample();
// Process input changes
if (m_newCodecId != kCodec_NONE)
{
// Ensure we don't lose embedding through program changes.
bool wasembedding = m_window.IsEmbedding();
QRect oldrect;
if (wasembedding)
{
oldrect = m_window.GetEmbeddingRect();
StopEmbedding();
}
// Note - we don't call the default VideoOutput::InputChanged method as
// the OpenGL implementation is asynchronous.
// So we need to update the video display profile here. It is a little
// circular as we need to set the video dimensions first which are then
// reset in Init.
// All told needs a cleanup - not least because the use of codecName appears
// to be inconsistent.
m_window.InputChanged(m_newVideoDim, m_newVideoDispDim, m_newAspect);
AVCodecID avCodecId = myth2av_codecid(m_newCodecId);
AVCodec *codec = avcodec_find_decoder(avCodecId);
QString codecName;
if (codec)
codecName = codec->name;
if (m_dbDisplayProfile)
m_dbDisplayProfile->SetInput(m_window.GetVideoDispDim(), 0 , codecName);
bool ok = Init(m_newVideoDim, m_newVideoDispDim, m_newAspect,
0, m_window.GetDisplayVisibleRect(), m_newCodecId);
m_newCodecId = kCodec_NONE;
m_newVideoDim = QSize();
m_newVideoDispDim = QSize();
m_newAspect = 0.0F;
if (wasembedding && ok)
EmbedInWidget(oldrect);
if (!ok)
return;
}
if (Frame)
m_window.SetRotation(Frame->rotation);
if (VERBOSE_LEVEL_CHECK(VB_GPU, LOG_INFO))
m_render->logDebugMarker(LOC + "PROCESS_FRAME_START");
bool swframe = Frame ? !format_is_hw(Frame->codec) : false;
bool dummy = Frame ? Frame->dummy : false;
// software deinterlacing
if (!dummy && swframe)
m_deinterlacer.Filter(Frame, Scan);
if (!m_window.IsEmbedding())
{
m_openGLVideoPiPActive = nullptr;
ShowPIPs(Frame, PiPPlayers);
}
if (m_openGLVideo && swframe && !dummy)
m_openGLVideo->ProcessFrame(Frame, Scan);
// time texture update
if (m_openGLPerf)
m_openGLPerf->RecordSample();
if (VERBOSE_LEVEL_CHECK(VB_GPU, LOG_INFO))
m_render->logDebugMarker(LOC + "PROCESS_FRAME_END");
}
void MythVideoOutputOpenGL::PrepareFrame(VideoFrame *Frame, FrameScanType Scan, OSD *Osd)
{
if (!m_render)
return;
if (m_newCodecId != kCodec_NONE)
return; // input changes need to be handled in ProcessFrame
OpenGLLocker ctx_lock(m_render);
if (VERBOSE_LEVEL_CHECK(VB_GPU, LOG_INFO))
m_render->logDebugMarker(LOC + "PREPARE_FRAME_START");
bool dummy = false;
int topfieldfirst = 0;
if (Frame)
{
m_framesPlayed = Frame->frameNumber + 1;
topfieldfirst = Frame->interlaced_reversed ? !Frame->top_field_first : Frame->top_field_first;
dummy = Frame->dummy;
}
else
{
// see VideoOutputOpenGL::DoneDisplayingFrame
// we only retain pause frames for hardware formats
if (m_videoBuffers.Size(kVideoBuffer_pause))
Frame = m_videoBuffers.Tail(kVideoBuffer_pause);
}
// if process frame has not been called (double rate hardware deint), then
// we need to start the first 2 performance timers here
if (m_openGLPerf)
{
if (!m_openGLPerf->GetTimersRunning())
{
m_openGLPerf->RecordSample();
m_openGLPerf->RecordSample();
}
}
m_render->BindFramebuffer(nullptr);
// avoid clearing the framebuffer if it will be entirely overwritten by video
if (!m_window.VideoIsFullScreen() || (!Frame || dummy))
{
if (VERBOSE_LEVEL_CHECK(VB_GPU, LOG_INFO))
m_render->logDebugMarker(LOC + "CLEAR_START");
int gray = m_dbLetterboxColour == kLetterBoxColour_Gray25 ? 127 : 0;
#if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
// Qt < 5.8 uses a different QRegion API. Just clear and remove this code
// when 5.8 is standard
m_render->SetBackground(gray, gray, gray, 255);
m_render->ClearFramebuffer();
#else
if (!Frame || dummy)
{
m_render->ClearFramebuffer();
}
else if (m_window.IsEmbedding())
{
// use MythRenderOpenGL rendering as it will clear to the appropriate 'black level'
m_render->ClearRect(nullptr, m_window.GetWindowRect(), gray);
}
else
{
// in the vast majority of cases it is significantly quicker to just
// clear the unused portions of the screen
QRegion toclear = m_window.GetBoundingRegion();
for (QRegion::const_iterator it = toclear.begin() ; it != toclear.end(); ++it)
m_render->ClearRect(nullptr, *it, gray);
}
#endif
if (VERBOSE_LEVEL_CHECK(VB_GPU, LOG_INFO))
m_render->logDebugMarker(LOC + "CLEAR_END");
}
// time framebuffer clearing
if (m_openGLPerf)
m_openGLPerf->RecordSample();
// stereoscopic views
QRect main = m_render->GetViewPort();
QRect first = main;
QRect second = main;
bool twopass = (m_stereo == kStereoscopicModeSideBySide) || (m_stereo == kStereoscopicModeTopAndBottom);
if (kStereoscopicModeSideBySide == m_stereo)
{
first = QRect(main.left() / 2, main.top(), main.width() / 2, main.height());
second = first.translated(main.width() / 2, 0);
}
else if (kStereoscopicModeTopAndBottom == m_stereo)
{
first = QRect(main.left(), main.top() / 2, main.width(), main.height() / 2);
second = first.translated(0, main.height() / 2);
}
// main UI when embedded
if (m_window.IsEmbedding())
{
MythMainWindow *win = GetMythMainWindow();
if (win && win->GetPaintWindow())
{
if (twopass)
m_render->SetViewPort(first, true);
win->GetPaintWindow()->clearMask();
win->draw(m_openGLPainter);
if (twopass)
{
m_render->SetViewPort(second, true);
win->GetPaintWindow()->clearMask();
win->draw(m_openGLPainter);
m_render->SetViewPort(main, true);
}
}
}
// video
if (m_openGLVideo && !dummy)
m_openGLVideo->PrepareFrame(Frame, topfieldfirst, Scan, m_stereo);
// dummy streams need the viewport updated in case we have resized the window
else if (dummy)
m_render->SetViewPort(m_window.GetWindowRect());
// PiPs/PBPs
if (!m_openGLVideoPiPs.empty())
{
QMap<MythPlayer*,MythOpenGLVideo*>::iterator it = m_openGLVideoPiPs.begin();
for (; it != m_openGLVideoPiPs.end(); ++it)
{
if (m_openGLVideoPiPsReady[it.key()])
{
bool active = m_openGLVideoPiPActive == *it;
if (twopass)
m_render->SetViewPort(first, true);
(*it)->PrepareFrame(nullptr, topfieldfirst, Scan, kStereoscopicModeNone, active);
if (twopass)
{
m_render->SetViewPort(second, true);
(*it)->PrepareFrame(nullptr, topfieldfirst, Scan, kStereoscopicModeNone, active);
m_render->SetViewPort(main);
}
}
}
}
// visualisation
if (m_visual && m_openGLPainter && !m_window.IsEmbedding())
{
if (twopass)
m_render->SetViewPort(first, true);
m_visual->Draw(GetTotalOSDBounds(), m_openGLPainter, nullptr);
if (twopass)
{
m_render->SetViewPort(second, true);
m_visual->Draw(GetTotalOSDBounds(), m_openGLPainter, nullptr);
m_render->SetViewPort(main);
}
}
// OSD
if (Osd && m_openGLPainter && !m_window.IsEmbedding())
{
if (twopass)
m_render->SetViewPort(first, true);
Osd->Draw(m_openGLPainter, GetTotalOSDBounds().size(), true);
if (twopass)
{
m_render->SetViewPort(second, true);
Osd->Draw(m_openGLPainter, GetTotalOSDBounds().size(), true);
m_render->SetViewPort(main);
}
}
// time rendering
if (m_openGLPerf)
m_openGLPerf->RecordSample();
m_render->Flush();
// time flush
if (m_openGLPerf)
m_openGLPerf->RecordSample();
if (VERBOSE_LEVEL_CHECK(VB_GPU, LOG_INFO))
m_render->logDebugMarker(LOC + "PREPARE_FRAME_END");
}
/*! \brief Release a video frame back into the decoder pool.
*
* Software frames do not need a pause frame as OpenGLVideo
* holds a copy of the last frame in its input textures. So
* just release the frame.
*
* Hardware frames hold the underlying interop class and
* hence access to the video texture. We cannot access them
* without a frame so retain the most recent frame by removing
* it from the 'used' queue and adding it to the 'pause' queue.
*/
void MythVideoOutputOpenGL::DoneDisplayingFrame(VideoFrame *Frame)
{
if (!Frame)
return;
bool retain = format_is_hw(Frame->codec);
m_videoBuffers.BeginLock(kVideoBuffer_pause);
while (m_videoBuffers.Size(kVideoBuffer_pause))
{
VideoFrame* frame = m_videoBuffers.Dequeue(kVideoBuffer_pause);
if (!retain || (retain && (frame != Frame)))
MythVideoOutput::DoneDisplayingFrame(frame);
}
if (retain)
{
m_videoBuffers.Enqueue(kVideoBuffer_pause, Frame);
if (m_videoBuffers.Contains(kVideoBuffer_used, Frame))
m_videoBuffers.Remove(kVideoBuffer_used, Frame);
}
else
{
m_videoBuffers.DoneDisplayingFrame(Frame);
}
m_videoBuffers.EndLock();
}
/*! \brief Discard video frames
*
* If Flushed is true, the decoder will probably reset the hardware decoder in
* use and we need to release any hardware pause frames so the decoder is released
* before a new one is created.
*/
void MythVideoOutputOpenGL::DiscardFrames(bool KeyFrame, bool Flushed)
{
if (Flushed)
{
m_videoBuffers.BeginLock(kVideoBuffer_pause);
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("(%1): %2").arg(KeyFrame).arg(m_videoBuffers.GetStatus()));
while (m_videoBuffers.Size(kVideoBuffer_pause))
m_videoBuffers.DiscardFrame(m_videoBuffers.Tail(kVideoBuffer_pause));
m_videoBuffers.EndLock();
}
MythVideoOutput::DiscardFrames(KeyFrame, Flushed);
}
VideoFrameType* MythVideoOutputOpenGL::DirectRenderFormats(void)
{
static VideoFrameType s_openglFormats[] =
{ FMT_YV12, FMT_NV12, FMT_YUY2, FMT_YUV422P, FMT_YUV444P,
FMT_YUV420P9, FMT_YUV420P10, FMT_YUV420P12, FMT_YUV420P14, FMT_YUV420P16,
FMT_YUV422P9, FMT_YUV422P10, FMT_YUV422P12, FMT_YUV422P14, FMT_YUV422P16,
FMT_YUV444P9, FMT_YUV444P10, FMT_YUV444P12, FMT_YUV444P14, FMT_YUV444P16,
FMT_P010, FMT_P016,
FMT_NONE };
// OpenGLES2 only allows luminance textures - no RG etc
static VideoFrameType s_opengles2Formats[] =
{ FMT_YV12, FMT_YUY2, FMT_YUV422P, FMT_YUV444P, FMT_NONE };
return m_isGLES2 ? &s_opengles2Formats[0] : &s_openglFormats[0];
}
void MythVideoOutputOpenGL::WindowResized(const QSize &Size)
{
m_window.SetWindowSize(Size);
InitDisplayMeasurements();
}
void MythVideoOutputOpenGL::Show(FrameScanType /*scan*/)
{
if (m_render && !IsErrored())
{
m_render->makeCurrent();
if (VERBOSE_LEVEL_CHECK(VB_GPU, LOG_INFO))
m_render->logDebugMarker(LOC + "SHOW");
m_render->swapBuffers();
if (m_openGLPerf)
{
// time buffer swap and log
// Results will normally be available on the next pass - and we will typically
// test every other frame as a result to avoid blocking in the driver.
// With the default of averaging over 30 samples this should give a 30 sample
// average over 60 frames
m_openGLPerf->RecordSample();
m_openGLPerf->LogSamples();
}
m_render->doneCurrent();
}
}
void MythVideoOutputOpenGL::ClearAfterSeek(void)
{
if (m_openGLVideo)
m_openGLVideo->ResetTextures();
MythVideoOutput::ClearAfterSeek();
}
/*! \brief Generate a list of supported OpenGL profiles.
*
* \note This list could be filtered based upon current feature support. This
* would however assume an OpenGL render device (not currently a given) but more
* importantly, filtering out a selected profile encourages the display profile
* code to use a higher priority, non-OpenGL renderer (such as VDPAU). By not
* filtering, we allow the OpenGL video code to fallback to a supported, reasonable
* alternative.
*/
QStringList MythVideoOutputOpenGL::GetAllowedRenderers(MythCodecID CodecId, const QSize& /*VideoDim*/)
{
QStringList allowed;
if (getenv("NO_OPENGL"))
return allowed;
if (codec_sw_copy(CodecId))
{
allowed << "opengl" << "opengl-yv12";
return allowed;
}
VideoFrameType format = FMT_NONE;
if (codec_is_vaapi(CodecId))
format = FMT_VAAPI;
else if (codec_is_vdpau(CodecId))
format = FMT_VDPAU;
else if (codec_is_nvdec(CodecId))
format = FMT_NVDEC;
else if (codec_is_vtb(CodecId))
format = FMT_VTB;
else if (codec_is_mmal(CodecId))
format = FMT_MMAL;
else if (codec_is_v4l2(CodecId) || codec_is_drmprime(CodecId))
format = FMT_DRMPRIME;
else if (codec_is_mediacodec(CodecId))
format = FMT_MEDIACODEC;
if (FMT_NONE == format)
return allowed;
allowed += MythOpenGLInterop::GetAllowedRenderers(format);
return allowed;
}
void MythVideoOutputOpenGL::UpdatePauseFrame(int64_t &DisplayTimecode)
{
m_videoBuffers.BeginLock(kVideoBuffer_used);
VideoFrame *used = m_videoBuffers.Head(kVideoBuffer_used);
if (used)
{
if (format_is_hw(used->codec))
DoneDisplayingFrame(used);
else
m_openGLVideo->ProcessFrame(used);
DisplayTimecode = used->disp_timecode;
}
else
{
LOG(VB_PLAYBACK, LOG_WARNING, LOC + "Could not update pause frame");
}
m_videoBuffers.EndLock();
}
void MythVideoOutputOpenGL::InitPictureAttributes(void)
{
m_videoColourSpace.SetSupportedAttributes(ALL_PICTURE_ATTRIBUTES);
}
void MythVideoOutputOpenGL::ShowPIP(VideoFrame* /*Frame*/, MythPlayer *PiPPlayer, PIPLocation Location)
{
if (!PiPPlayer)
return;
int pipw, piph;
VideoFrame *pipimage = PiPPlayer->GetCurrentFrame(pipw, piph);
const QSize pipvideodim = PiPPlayer->GetVideoBufferSize();
QRect pipvideorect = QRect(QPoint(0, 0), pipvideodim);
if ((PiPPlayer->GetVideoAspect() <= 0.0F) || !pipimage || !pipimage->buf ||
(pipimage->codec != FMT_YV12) || !PiPPlayer->IsPIPVisible())
{
PiPPlayer->ReleaseCurrentFrame(pipimage);
return;
}
QRect position = GetPIPRect(Location, PiPPlayer);
QRect dvr = m_window.GetDisplayVisibleRect();
m_openGLVideoPiPsReady[PiPPlayer] = false;
MythOpenGLVideo *gl_pipchain = m_openGLVideoPiPs[PiPPlayer];
if (!gl_pipchain)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Initialise PiP");
VideoColourSpace *colourspace = new VideoColourSpace(&m_videoColourSpace);
m_openGLVideoPiPs[PiPPlayer] = gl_pipchain = new MythOpenGLVideo(m_render, colourspace,
pipvideodim, pipvideodim,
dvr, position, pipvideorect,
false, m_videoProfile);
colourspace->DecrRef();
if (!gl_pipchain->IsValid())
{
PiPPlayer->ReleaseCurrentFrame(pipimage);
return;
}
gl_pipchain->SetMasterViewport(dvr.size());
}
if (gl_pipchain->GetVideoSize() != pipvideodim)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Re-initialise PiP.");
delete gl_pipchain;
VideoColourSpace *colourspace = new VideoColourSpace(&m_videoColourSpace);
m_openGLVideoPiPs[PiPPlayer] = gl_pipchain = new MythOpenGLVideo(m_render, colourspace,
pipvideodim, pipvideodim,
dvr, position, pipvideorect,
false, m_videoProfile);
colourspace->DecrRef();
if (!gl_pipchain->IsValid())
{
PiPPlayer->ReleaseCurrentFrame(pipimage);
return;
}
gl_pipchain->SetMasterViewport(dvr.size());
}
if (gl_pipchain->IsValid())
{
gl_pipchain->SetVideoRects(position, pipvideorect);
gl_pipchain->ProcessFrame(pipimage);
}
m_openGLVideoPiPsReady[PiPPlayer] = true;
if (PiPPlayer->IsPIPActive())
m_openGLVideoPiPActive = gl_pipchain;
PiPPlayer->ReleaseCurrentFrame(pipimage);
}
void MythVideoOutputOpenGL::RemovePIP(MythPlayer *PiPPlayer)
{
if (m_openGLVideoPiPs.contains(PiPPlayer))
{
m_render->makeCurrent();
delete m_openGLVideoPiPs.take(PiPPlayer);
m_openGLVideoPiPsReady.remove(PiPPlayer);
m_openGLVideoPiPs.remove(PiPPlayer);
m_render->doneCurrent();
}
}
QStringList MythVideoOutputOpenGL::GetVisualiserList(void)
{
if (m_render)
return VideoVisual::GetVisualiserList(m_render->Type());
return MythVideoOutput::GetVisualiserList();
}
MythPainter *MythVideoOutputOpenGL::GetOSDPainter(void)
{
return m_openGLPainter;
}
bool MythVideoOutputOpenGL::CanVisualise(AudioPlayer *Audio, MythRender* /*Render*/)
{
return MythVideoOutput::CanVisualise(Audio, m_render);
}
bool MythVideoOutputOpenGL::SetupVisualisation(AudioPlayer *Audio, MythRender* /*Render*/, const QString &Name)
{
return MythVideoOutput::SetupVisualisation(Audio, m_render, Name);
}