/
videoout_d3d.cpp
756 lines (649 loc) · 20 KB
/
videoout_d3d.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
// -*- Mode: c++ -*-
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#include "mythcontext.h"
#include "videoout_d3d.h"
#include "osd.h"
#include "filtermanager.h"
#include "fourcc.h"
#include "videodisplayprofile.h"
#include "mythmainwindow.h"
#include "myth_imgconvert.h"
#include "mythplayer.h"
#include "mmsystem.h"
#include "tv.h"
#undef UNICODE
extern "C" {
#include "libavcodec/avcodec.h"
}
const int kNumBuffers = 31;
const int kNeedFreeFrames = 1;
const int kPrebufferFramesNormal = 10;
const int kPrebufferFramesSmall = 4;
const int kKeepPrebuffer = 2;
#define NUM_DXVA2_BUFS 30
#define LOC QString("VideoOutputD3D: ")
void VideoOutputD3D::GetRenderOptions(render_opts &opts,
QStringList &cpudeints)
{
opts.renderers->append("direct3d");
opts.deints->insert("direct3d", cpudeints);
(*opts.osds)["direct3d"].append("direct3d");
(*opts.safe_renderers)["dummy"].append("direct3d");
(*opts.safe_renderers)["nuppel"].append("direct3d");
if (opts.decoders->contains("ffmpeg"))
(*opts.safe_renderers)["ffmpeg"].append("direct3d");
if (opts.decoders->contains("crystalhd"))
(*opts.safe_renderers)["crystalhd"].append("direct3d");
opts.priorities->insert("direct3d", 70);
#ifdef USING_DXVA2
if (opts.decoders->contains("dxva2"))
(*opts.safe_renderers)["dxva2"].append("direct3d");
#endif
}
VideoOutputD3D::VideoOutputD3D(void)
: VideoOutput(), m_lock(QMutex::Recursive),
m_hWnd(NULL), m_render(NULL),
m_video(NULL),
m_render_valid(false), m_render_reset(false), m_pip_active(NULL),
m_osd_painter(NULL)
{
m_pauseFrame.buf = NULL;
#ifdef USING_DXVA2
m_decoder = NULL;
#endif
m_pause_surface = NULL;
}
VideoOutputD3D::~VideoOutputD3D()
{
TearDown();
}
void VideoOutputD3D::TearDown(void)
{
QMutexLocker locker(&m_lock);
vbuffers.DiscardFrames(true);
vbuffers.Reset();
vbuffers.DeleteBuffers();
if (m_pauseFrame.buf)
{
delete [] m_pauseFrame.buf;
m_pauseFrame.buf = NULL;
}
delete m_osd_painter;
m_osd_painter = NULL;
DeleteDecoder();
DestroyContext();
}
void VideoOutputD3D::DestroyContext(void)
{
QMutexLocker locker(&m_lock);
m_render_valid = false;
m_render_reset = false;
while (!m_pips.empty())
{
delete *m_pips.begin();
m_pips.erase(m_pips.begin());
}
m_pip_ready.clear();
if (m_video)
{
delete m_video;
m_video = NULL;
}
if (m_render)
{
m_render->DecrRef();
m_render = NULL;
}
}
void VideoOutputD3D::WindowResized(const QSize &new_size)
{
// FIXME this now requires the context to be re-created
/*
QMutexLocker locker(&m_lock);
window.SetDisplayVisibleRect(QRect(QPoint(0, 0), new_size));
window.SetDisplayAspect(
((float)new_size.width()) / new_size.height());
MoveResize();
*/
}
bool VideoOutputD3D::InputChanged(const QSize &input_size,
float aspect,
MythCodecID av_codec_id,
void *codec_private,
bool &aspect_only)
{
QMutexLocker locker(&m_lock);
QSize cursize = window.GetActualVideoDim();
LOG(VB_PLAYBACK, LOG_INFO, LOC +
QString("InputChanged from %1: %2x%3 aspect %4 to %5: %6x%7 aspect %9")
.arg(toString(video_codec_id)).arg(cursize.width())
.arg(cursize.height()).arg(window.GetVideoAspect())
.arg(toString(av_codec_id)).arg(input_size.width())
.arg(input_size.height()).arg(aspect));
bool cid_changed = (video_codec_id != av_codec_id);
bool res_changed = input_size != cursize;
bool asp_changed = aspect != window.GetVideoAspect();
if (!res_changed && !cid_changed)
{
if (asp_changed)
{
aspect_only = true;
VideoAspectRatioChanged(aspect);
MoveResize();
}
return true;
}
TearDown();
QRect disp = window.GetDisplayVisibleRect();
if (Init(input_size.width(), input_size.height(),
aspect, m_hWnd, disp, av_codec_id))
{
BestDeint();
return true;
}
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to re-initialise video output.");
errorState = kError_Unknown;
return false;
}
bool VideoOutputD3D::SetupContext()
{
QMutexLocker locker(&m_lock);
DestroyContext();
QSize size = window.GetVideoDim();
m_render = new MythRenderD3D9();
if (!(m_render && m_render->Create(window.GetDisplayVisibleRect().size(),
m_hWnd)))
return false;
m_video = new D3D9Image(m_render, size, true);
if (!(m_video && m_video->IsValid()))
return false;
LOG(VB_PLAYBACK, LOG_INFO, LOC +
"Direct3D device successfully initialized.");
m_render_valid = true;
return true;
}
bool VideoOutputD3D::Init(int width, int height, float aspect, WId winid,
const QRect &win_rect,MythCodecID codec_id)
{
MythPainter *painter = GetMythPainter();
if (painter)
painter->FreeResources();
QMutexLocker locker(&m_lock);
m_hWnd = winid;
window.SetAllowPreviewEPG(true);
VideoOutput::Init(width, height, aspect, winid, win_rect, codec_id);
LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Init with codec: %1")
.arg(toString(codec_id)));
SetProfile();
bool success = true;
success &= SetupContext();
InitDisplayMeasurements(width, height, false);
if (codec_is_dxva2(video_codec_id))
{
if (!CreateDecoder())
return false;
}
success &= CreateBuffers();
success &= InitBuffers();
success &= CreatePauseFrame();
MoveResize();
if (!success)
TearDown();
else
{
m_osd_painter = new MythD3D9Painter(m_render);
if (m_osd_painter)
{
m_osd_painter->SetSwapControl(false);
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Created D3D9 osd painter.");
}
else
LOG(VB_GENERAL, LOG_ERR, LOC +
"Failed to create D3D9 osd painter.");
}
return success;
}
void VideoOutputD3D::SetProfile(void)
{
if (db_vdisp_profile)
db_vdisp_profile->SetVideoRenderer("direct3d");
}
bool VideoOutputD3D::CreateBuffers(void)
{
if (codec_is_dxva2(video_codec_id))
{
vbuffers.Init(NUM_DXVA2_BUFS, false, 2, 1, 4, 1);
LOG(VB_PLAYBACK, LOG_INFO, LOC +
QString("Created %1 empty DXVA2 buffers.") .arg(NUM_DXVA2_BUFS));
return true;
}
vbuffers.Init(kNumBuffers, true, kNeedFreeFrames,
kPrebufferFramesNormal, kPrebufferFramesSmall,
kKeepPrebuffer);
return true;
}
bool VideoOutputD3D::InitBuffers(void)
{
#ifdef USING_DXVA2
if ((codec_is_dxva2(video_codec_id)) && m_decoder)
{
QMutexLocker locker(&m_lock);
const QSize video_dim = window.GetVideoDim();
bool ok = true;
for (int i = 0; i < NUM_DXVA2_BUFS; i++)
{
ok &= vbuffers.CreateBuffer(video_dim.width(),
video_dim.height(), i,
m_decoder->GetSurface(i), FMT_DXVA2);
}
if (ok)
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Initialised DXVA2 buffers.");
return ok;
}
#endif
return vbuffers.CreateBuffers(FMT_YV12,
window.GetVideoDim().width(),
window.GetVideoDim().height());
}
bool VideoOutputD3D::CreatePauseFrame(void)
{
if (codec_is_dxva2(video_codec_id))
return true;
init(&m_pauseFrame, FMT_YV12,
new unsigned char[vbuffers.GetScratchFrame()->size + 128],
vbuffers.GetScratchFrame()->width,
vbuffers.GetScratchFrame()->height,
vbuffers.GetScratchFrame()->size);
m_pauseFrame.frameNumber = vbuffers.GetScratchFrame()->frameNumber;
return true;
}
void VideoOutputD3D::PrepareFrame(VideoFrame *buffer, FrameScanType t,
OSD *osd)
{
(void)osd;
if (IsErrored())
{
LOG(VB_GENERAL, LOG_ERR, LOC +
"PrepareFrame() called while IsErrored is true.");
return;
}
if (!buffer && codec_is_std(video_codec_id))
buffer = vbuffers.GetScratchFrame();
bool dummy = false;
if (buffer)
{
dummy = buffer->dummy;
framesPlayed = buffer->frameNumber + 1;
}
if (!m_render || !m_video)
return;
m_render_valid = m_render->Test(m_render_reset);
if (m_render_valid)
{
QRect dvr = vsz_enabled ? vsz_desired_display_rect :
window.GetDisplayVideoRect();
bool ok = m_render->ClearBuffer();
if (ok && !dummy)
ok = m_video->UpdateVertices(dvr, window.GetVideoRect(),
255, true);
if (ok)
{
ok = m_render->Begin();
if (ok)
{
if (!dummy)
m_video->Draw();
QMap<MythPlayer*,D3D9Image*>::iterator it = m_pips.begin();
for (; it != m_pips.end(); ++it)
{
if (m_pip_ready[it.key()])
{
if (m_pip_active == *it)
{
QRect rect = (*it)->GetRect();
if (!rect.isNull())
{
rect.adjust(-10, -10, 10, 10);
m_render->DrawRect(rect, QColor(128,0,0,255), 255);
}
}
(*it)->Draw();
}
}
if (m_visual)
m_visual->Draw(GetTotalOSDBounds(), m_osd_painter, NULL);
if (osd && m_osd_painter && !window.IsEmbedding())
osd->DrawDirect(m_osd_painter, GetTotalOSDBounds().size(),
true);
m_render->End();
}
}
}
}
void VideoOutputD3D::Show(FrameScanType )
{
if (IsErrored())
{
LOG(VB_GENERAL, LOG_ERR, LOC +
"Show() called while IsErrored is true.");
return;
}
if (!m_render)
return;
m_render_valid = m_render->Test(m_render_reset);
if (m_render_valid)
m_render->Present(window.IsEmbedding() ? m_hEmbedWnd : NULL);
}
void VideoOutputD3D::EmbedInWidget(const QRect &rect)
{
if (window.IsEmbedding())
return;
VideoOutput::EmbedInWidget(rect);
// TODO: Initialise m_hEmbedWnd?
}
void VideoOutputD3D::StopEmbedding(void)
{
if (!window.IsEmbedding())
return;
VideoOutput::StopEmbedding();
}
void VideoOutputD3D::Zoom(ZoomDirection direction)
{
QMutexLocker locker(&m_lock);
VideoOutput::Zoom(direction);
MoveResize();
}
void VideoOutputD3D::UpdatePauseFrame(int64_t &disp_timecode)
{
QMutexLocker locker(&m_lock);
VideoFrame *used_frame = vbuffers.Head(kVideoBuffer_used);
if (codec_is_std(video_codec_id))
{
if (!used_frame)
used_frame = vbuffers.GetScratchFrame();
CopyFrame(&m_pauseFrame, used_frame);
disp_timecode = m_pauseFrame.disp_timecode;
}
else if (codec_is_dxva2(video_codec_id))
{
if (used_frame)
{
m_pause_surface = used_frame->buf;
disp_timecode = used_frame->disp_timecode;
}
else
LOG(VB_PLAYBACK, LOG_WARNING, LOC + "Failed to update pause frame");
}
}
void VideoOutputD3D::UpdateFrame(VideoFrame *frame, D3D9Image *img)
{
if (codec_is_dxva2(video_codec_id))
return;
// TODO - add a size check
bool hardware_conv = false;
uint pitch = 0;
uint8_t *buf = img->GetBuffer(hardware_conv, pitch);
if (buf && hardware_conv)
{
int i;
uint8_t *dst = buf;
uint8_t *src = frame->buf;
int chroma_width = frame->width >> 1;
int chroma_height = frame->height >> 1;
int chroma_pitch = pitch >> 1;
for (i = 0; i < frame->height; i++)
{
memcpy(dst, src, frame->width);
dst += pitch;
src += frame->width;
}
dst = buf + (frame->height * pitch);
src = frame->buf + (frame->height * frame->width * 5/4);
for (i = 0; i < chroma_height; i++)
{
memcpy(dst, src, chroma_width);
dst += chroma_pitch;
src += chroma_width;
}
dst = buf + (frame->height * pitch * 5/4);
src = frame->buf + (frame->height * frame->width);
for (i = 0; i < chroma_height; i++)
{
memcpy(dst, src, chroma_width);
dst += chroma_pitch;
src += chroma_width;
}
}
else if (buf && !hardware_conv)
{
AVPicture image_in, image_out;
avpicture_fill(&image_out, (uint8_t*)buf,
PIX_FMT_RGB32, frame->width, frame->height);
image_out.linesize[0] = pitch;
avpicture_fill(&image_in, frame->buf,
PIX_FMT_YUV420P, frame->width, frame->height);
myth_sws_img_convert(&image_out, PIX_FMT_RGB32, &image_in,
PIX_FMT_YUV420P, frame->width, frame->height);
}
img->ReleaseBuffer();
}
void VideoOutputD3D::ProcessFrame(VideoFrame *frame, OSD *osd,
FilterChain *filterList,
const PIPMap &pipPlayers,
FrameScanType scan)
{
if (!m_render || !m_video)
return;
QMutexLocker locker(&m_lock);
if (IsErrored())
{
LOG(VB_GENERAL, LOG_ERR, LOC +
"ProcessFrame() called while IsErrored is true.");
return;
}
bool gpu = codec_is_dxva2(video_codec_id);
if (gpu && frame && frame->codec != FMT_DXVA2)
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Wrong frame format");
return;
}
bool dummy = false;
bool pauseframe = false;
if (!frame)
{
if (!gpu)
{
frame = vbuffers.GetScratchFrame();
CopyFrame(vbuffers.GetScratchFrame(), &m_pauseFrame);
}
pauseframe = true;
}
if (frame)
dummy = frame->dummy;
bool deint_proc = m_deinterlacing && (m_deintFilter != NULL) &&
!dummy;
if (filterList && !gpu && !dummy)
filterList->ProcessFrame(frame);
bool safepauseframe = pauseframe && !IsBobDeint() && !gpu;
if (deint_proc && m_deinterlaceBeforeOSD &&
(!pauseframe || safepauseframe))
{
m_deintFilter->ProcessFrame(frame, scan);
}
if (!window.IsEmbedding())
ShowPIPs(frame, pipPlayers);
if ((!pauseframe || safepauseframe) &&
deint_proc && !m_deinterlaceBeforeOSD)
{
m_deintFilter->ProcessFrame(frame, scan);
}
// Test the device
m_render_valid |= m_render->Test(m_render_reset);
if (m_render_reset)
SetupContext();
// Update a software decoded frame
if (m_render_valid && !gpu && !dummy)
UpdateFrame(frame, m_video);
// Update a GPU decoded frame
if (m_render_valid && gpu && !dummy)
{
m_render_valid = m_render->Test(m_render_reset);
if (m_render_reset)
CreateDecoder();
if (m_render_valid && frame)
{
m_render->CopyFrame(frame->buf, m_video);
}
else if (m_render_valid && pauseframe)
{
m_render->CopyFrame(m_pause_surface, m_video);
}
}
}
void VideoOutputD3D::ShowPIP(VideoFrame *frame,
MythPlayer *pipplayer,
PIPLocation loc)
{
if (!pipplayer)
return;
int pipw, piph;
VideoFrame *pipimage = pipplayer->GetCurrentFrame(pipw, piph);
const float pipVideoAspect = pipplayer->GetVideoAspect();
const QSize pipVideoDim = pipplayer->GetVideoBufferSize();
const bool pipActive = pipplayer->IsPIPActive();
const bool pipVisible = pipplayer->IsPIPVisible();
const uint pipVideoWidth = pipVideoDim.width();
const uint pipVideoHeight = pipVideoDim.height();
if ((pipVideoAspect <= 0) || !pipimage ||
!pipimage->buf || (pipimage->codec != FMT_YV12) || !pipVisible)
{
pipplayer->ReleaseCurrentFrame(pipimage);
return;
}
QRect position = GetPIPRect(loc, pipplayer);
QRect dvr = window.GetDisplayVisibleRect();
m_pip_ready[pipplayer] = false;
D3D9Image *m_pip = m_pips[pipplayer];
if (!m_pip)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Initialise PiP.");
m_pip = new D3D9Image(m_render, QSize(pipVideoWidth, pipVideoHeight),
true);
m_pips[pipplayer] = m_pip;
if (!m_pip->IsValid())
{
pipplayer->ReleaseCurrentFrame(pipimage);
return;
}
}
QSize current = m_pip->GetSize();
if ((uint)current.width() != pipVideoWidth ||
(uint)current.height() != pipVideoHeight)
{
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Re-initialise PiP.");
delete m_pip;
m_pip = new D3D9Image(m_render, QSize(pipVideoWidth, pipVideoHeight),
true);
m_pips[pipplayer] = m_pip;
if (!m_pip->IsValid())
{
pipplayer->ReleaseCurrentFrame(pipimage);
return;
}
}
m_pip->UpdateVertices(position, QRect(0, 0, pipVideoWidth, pipVideoHeight),
255, true);
UpdateFrame(pipimage, m_pip);
m_pip_ready[pipplayer] = true;
if (pipActive)
m_pip_active = m_pip;
pipplayer->ReleaseCurrentFrame(pipimage);
}
void VideoOutputD3D::RemovePIP(MythPlayer *pipplayer)
{
if (!m_pips.contains(pipplayer))
return;
QMutexLocker locker(&m_lock);
D3D9Image *m_pip = m_pips[pipplayer];
if (m_pip)
delete m_pip;
m_pip_ready.remove(pipplayer);
m_pips.remove(pipplayer);
}
QStringList VideoOutputD3D::GetAllowedRenderers(
MythCodecID myth_codec_id, const QSize &video_dim)
{
QStringList list;
if (codec_is_std(myth_codec_id) || (codec_is_dxva2_hw(myth_codec_id) &&
!getenv("NO_DXVA2")))
{
list += "direct3d";
}
return list;
}
MythPainter *VideoOutputD3D::GetOSDPainter(void)
{
return m_osd_painter;
}
bool VideoOutputD3D::ApproveDeintFilter(const QString& filtername) const
{
if (codec_is_std(video_codec_id))
{
return !filtername.contains("bobdeint") &&
!filtername.contains("opengl") &&
!filtername.contains("vdpau");
}
return false;
}
MythCodecID VideoOutputD3D::GetBestSupportedCodec(
uint width, uint height, const QString &decoder,
uint stream_type, bool no_acceleration,
PixelFormat &pix_fmt)
{
#ifdef USING_DXVA2
QSize size(width, height);
bool use_cpu = no_acceleration;
MythCodecID test_cid = (MythCodecID)(kCodec_MPEG1_DXVA2 + (stream_type - 1));
use_cpu |= !codec_is_dxva2_hw(test_cid);
pix_fmt = PIX_FMT_DXVA2_VLD;
if ((decoder == "dxva2") && !getenv("NO_DXVA2") && !use_cpu)
return test_cid;
#endif
return (MythCodecID)(kCodec_MPEG1 + (stream_type - 1));
}
void* VideoOutputD3D::GetDecoderContext(unsigned char* buf, uint8_t*& id)
{
(void)buf;
(void)id;
#ifdef USING_DXVA2
if (m_decoder)
return (void*)&m_decoder->m_context;
#endif
return NULL;
}
bool VideoOutputD3D::CreateDecoder(void)
{
#ifdef USING_DXVA2
QMutexLocker locker(&m_lock);
if (m_decoder)
DeleteDecoder();
QSize video_dim = window.GetVideoDim();
m_decoder = new DXVA2Decoder(NUM_DXVA2_BUFS, video_codec_id,
video_dim.width(), video_dim.height());
return (m_decoder && m_decoder->Init(m_render));
#else
return false;
#endif
}
void VideoOutputD3D::DeleteDecoder(void)
{
#ifdef USING_DXVA2
QMutexLocker locker(&m_lock);
delete m_decoder;
m_decoder = NULL;
#endif
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */