/
videobuffers.cpp
1087 lines (975 loc) · 34.2 KB
/
videobuffers.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, Daniel Thor Kristjansson
// based on earlier work in MythTV's videout_xvmc.cpp
// MythTV
#include "mythconfig.h"
#include "mythcontext.h"
#include "fourcc.h"
#include "compat.h"
#include "mythlogging.h"
#include "mythcodecid.h"
#include "videobuffers.h"
// FFmpeg
extern "C" {
#include "libavcodec/avcodec.h"
}
// Std
#include <chrono>
#include <thread>
#define TRY_LOCK_SPINS 2000
#define TRY_LOCK_SPINS_BEFORE_WARNING 9999
#define TRY_LOCK_SPIN_WAIT 1000 /* usec */
int next_dbg_str = 0;
YUVInfo::YUVInfo(uint Width, uint Height, uint Size, const int *Pitches,
const int *Offsets, int Alignment)
: m_width(Width),
m_height(Height),
m_size(Size)
{
// make sure all our pitches are a multiple of "aligned" bytes
// Needs to take into consideration that U and V channels are half
// the width of Y channel
uint alignedwidth;
if (!Alignment)
alignedwidth = m_width;
else
alignedwidth = (m_width + Alignment - 1) & ~(Alignment - 1);
if (Pitches)
{
memcpy(m_pitches, Pitches, 3 * sizeof(int));
}
else
{
m_pitches[0] = alignedwidth;
m_pitches[1] = m_pitches[2] = (alignedwidth+1) >> 1;
}
if (Offsets)
{
memcpy(m_offsets, Offsets, 3 * sizeof(int));
}
else
{
m_offsets[0] = 0;
m_offsets[1] = alignedwidth * m_height;
m_offsets[2] = m_offsets[1] + ((alignedwidth+1) >> 1) * ((m_height+1) >> 1);
}
}
/**
* \class VideoBuffers
* This class creates tracks the state of the buffers used by
* various VideoOutput derived classes.
*
* The states available for a buffer are: available, limbo, used,
* process, and displayed.
*
* The two most important states are available and used.
* Used is implemented as a FIFO, and is used to buffer
* frames ready for display. A decoder may decode frames
* out of order but must add them to the used queue in
* order. The available buffers are buffers that are ready
* to be used by the decoder.
*
* Generally a buffer leaves the available state via
* GetNextFreeFrame(bool,bool,BufferType) and enters the
* limbo state. It then leaves the limbo state via
* ReleaseFrame(VideoFrame*) and enters the used state.
* Then it leaves the used state via DoneDisplayingFrame()
* and enters the available state.
*
* At any point, DiscardFrame(VideoFrame*) can be called
* to remove the frame from the current state and add it
* to the available list.
*
* However, there are two additional states available, these
* are currently used by VideoOutputXv for XvMC support.
* These are the process and displayed state. The process
* state simply indicates that the frame has been picked up
* in the VideoOutput::ProcessFrame() call, but
* VideoOutput::Show() has not yet used the frame. This
* is needed because a frame other than a used frame
* is being held by VideoOutput and we don't want to lose
* it if the stream is reset. The displayed state indicates
* that DoneDisplayingFrame() has been called for the frame,
* but it cannot yet be added to available because it is
* still being displayed. VideoOutputXv calls
* DiscardFrame(VideoFrame*) on the frames no longer
* being displayed at the end of the next
* DoneDisplayingFrame(), finally adding them to available.
*
* The only method that returns with a lock held on the VideoBuffers
* object itself, preventing anyone else from using the VideoBuffers
* class, inluding to unlocking frames, is the begin_lock(BufferType).
* This method is to be used with extreme caution, in particular one
* should not attempt to acquire any locks before end_lock() is called.
*
* There are also frame inheritence tracking functions, these are
* used by VideoOutputXv to avoid throwing away displayed frames too
* early. See videoout_xv.cpp for their use.
*
* released = used + finished + displayed + pause
* total = available + limbo + released
* released_and_in_use_by_decoder = decode
*
* available - frames not in use by decoder or display
* limbo - frames in use by decoder but not released for display
* decode - frames in use by decoder and released for display
* used - frames released for display but not displayed or paused
* displayed - frames displayed but still used as a reference frame
* pause - frames used for pause
* finished - frames that are finished displaying but still in use by decoder
*
* NOTE: All queues are mutually exclusive except "decode" which tracks frames
* that have been released but still in use by the decoder. If a frame
* has finished being processed/displayed but is still in use by the
* decoder (in the decode queue) then it is placed in the finished queue
* until the decoder is no longer using it (not in the decode queue).
*
* \see VideoOutput
*/
uint VideoBuffers::GetNumBuffers(int PixelFormat, int MaxReferenceFrames, bool Decoder /*=false*/)
{
uint refs = static_cast<uint>(MaxReferenceFrames);
switch (PixelFormat)
{
case FMT_DXVA2: return 30;
case FMT_VTB: return 24;
// Max 16 ref frames, 12 headroom and allocate 2 extra in the VAAPI frames
// context for additional references held by the VPP deinterlacer (i.e.
// prevent buffer starvation in the decoder)
// This covers the 'worst case' samples.
case FMT_VAAPI: return Decoder ? (refs + 14) : (refs + 12);
case FMT_VDPAU: return refs + 12;
// Copyback of hardware frames. These decoders are buffering internally
// already - so no need for a large presentation buffer
case FMT_NONE: return 8;
// As for copyback, these decoders buffer internally
case FMT_NVDEC: return 8;
case FMT_MEDIACODEC: return 8;
case FMT_MMAL: return 8;
// the default number of output buffers in FFmpeg v4l2_m2m.h is 6
case FMT_DRMPRIME: return 6;
// Standard software decode
case FMT_YV12: return refs + 14;
default: break;
}
return 30;
}
VideoBuffers::~VideoBuffers()
{
DeleteBuffers();
}
/*! \brief Creates buffers and sets various buffer management parameters.
*
* This normally creates numdecode buffers, but it creates
* one more buffer if extra_for_pause is true. Only numdecode
* buffers are added to available and hence into the buffer
* management handled by VideoBuffers. The availability of
* any scratch frame must be managed by the video output
* class itself.
*
* \param NumDecode number of buffers to allocate for normal use
* \param ExtraForPause allocate an extra buffer, a scratch a frame for pause
* \param NeedFree maximum number of buffers needed in display
* and pause
* \param NeedPrebufferNormal number buffers you can put in used or limbo normally
* \param NeedPrebufferSmall number of buffers you can put in used or limbo
* after SetPrebuffering(false) has been called.
*/
void VideoBuffers::Init(uint NumDecode, bool ExtraForPause,
uint NeedFree, uint NeedPrebufferNormal,
uint NeedPrebufferSmall)
{
QMutexLocker locker(&m_globalLock);
Reset();
uint numcreate = NumDecode + ((ExtraForPause) ? 1 : 0);
// make a big reservation, so that things that depend on
// pointer to VideoFrames work even after a few push_backs
m_buffers.reserve(max(numcreate, (uint)128));
m_buffers.resize(numcreate);
for (uint i = 0; i < numcreate; i++)
{
memset(At(i), 0, sizeof(VideoFrame));
At(i)->codec = FMT_NONE;
At(i)->interlaced_frame = -1;
At(i)->top_field_first = 1;
m_vbufferMap[At(i)] = i;
}
m_needFreeFrames = NeedFree;
m_needPrebufferFrames = NeedPrebufferNormal;
m_needPrebufferFramesNormal = NeedPrebufferNormal;
m_needPrebufferFramesSmall = NeedPrebufferSmall;
m_createdPauseFrame = ExtraForPause;
if (m_createdPauseFrame)
Enqueue(kVideoBuffer_pause, At(numcreate - 1));
for (uint i = 0; i < NumDecode; i++)
Enqueue(kVideoBuffer_avail, At(i));
SetDeinterlacing(DEINT_NONE, DEINT_NONE, kCodec_NONE);
}
void VideoBuffers::SetDeinterlacing(MythDeintType Single, MythDeintType Double,
MythCodecID CodecID)
{
QMutexLocker locker(&m_globalLock);
frame_vector_t::iterator it = m_buffers.begin();
for ( ; it != m_buffers.end(); ++it)
SetDeinterlacingFlags(*it, Single, Double, CodecID);
}
/*! \brief Set the appropriate flags for single and double rate deinterlacing
* \note Double rate support is determined by the VideoOutput class and must be set appropriately
* \note Driver deinterlacers are only available for hardware frames with the exception of
* NVDEC and VTB which can use shaders.
* \note Shader and CPU deinterlacers are disabled for hardware frames (except for shaders with NVDEC and VTB)
* \todo Handling of decoder deinterlacing with NVDEC
*/
void VideoBuffers::SetDeinterlacingFlags(VideoFrame &Frame, MythDeintType Single,
MythDeintType Double, MythCodecID CodecID)
{
static const MythDeintType kDriver = DEINT_ALL & ~(DEINT_CPU | DEINT_SHADER);
static const MythDeintType kShader = DEINT_ALL & ~(DEINT_CPU | DEINT_DRIVER);
static const MythDeintType kSoftware = DEINT_ALL & ~(DEINT_SHADER | DEINT_DRIVER);
Frame.deinterlace_single = Single;
Frame.deinterlace_double = Double;
if (codec_is_copyback(CodecID))
{
if (codec_is_vaapi_dec(CodecID) || codec_is_nvdec_dec(CodecID))
Frame.deinterlace_allowed = kSoftware | kShader | kDriver;
else // VideoToolBox, MediaCodec and VDPAU copyback
Frame.deinterlace_allowed = kSoftware | kShader;
}
else if (FMT_DRMPRIME == Frame.codec)
Frame.deinterlace_allowed = kShader; // No driver deint - if RGBA frames are returned, shaders will be disabled
else if (FMT_MMAL == Frame.codec)
Frame.deinterlace_allowed = kShader; // No driver deint yet (TODO) and YUV frames returned
else if (FMT_VTB == Frame.codec)
Frame.deinterlace_allowed = kShader; // No driver deint and YUV frames returned
else if (FMT_NVDEC == Frame.codec)
Frame.deinterlace_allowed = kShader | kDriver; // YUV frames and decoder deint
else if (FMT_VDPAU == Frame.codec)
Frame.deinterlace_allowed = kDriver; // No YUV frames for shaders
else if (FMT_VAAPI == Frame.codec)
Frame.deinterlace_allowed = kDriver; // DRM will allow shader if no VPP
else
Frame.deinterlace_allowed = kSoftware | kShader;
}
/**
* \fn VideoBuffers::Reset()
* Resets the class so that Init may be called again.
*/
void VideoBuffers::Reset()
{
QMutexLocker locker(&m_globalLock);
m_available.clear();
m_used.clear();
m_limbo.clear();
m_finished.clear();
m_decode.clear();
m_pause.clear();
m_displayed.clear();
m_vbufferMap.clear();
}
/**
* \fn VideoBuffers::SetPrebuffering(bool normal)
* Sets prebuffering state to normal, or small.
*/
void VideoBuffers::SetPrebuffering(bool Normal)
{
QMutexLocker locker(&m_globalLock);
m_needPrebufferFrames = (Normal) ? m_needPrebufferFramesNormal : m_needPrebufferFramesSmall;
}
void VideoBuffers::ReleaseDecoderResources(VideoFrame *Frame)
{
if (format_is_hw(Frame->codec))
{
AVBufferRef* ref = reinterpret_cast<AVBufferRef*>(Frame->priv[0]);
if (ref != nullptr)
av_buffer_unref(&ref);
Frame->buf = Frame->priv[0] = nullptr;
if (format_is_hwframes(Frame->codec) || (Frame->codec == FMT_NVDEC))
{
ref = reinterpret_cast<AVBufferRef*>(Frame->priv[1]);
if (ref != nullptr)
av_buffer_unref(&ref);
Frame->priv[1] = nullptr;
}
}
(void)Frame;
}
VideoFrame *VideoBuffers::GetNextFreeFrameInternal(BufferType EnqueueTo)
{
QMutexLocker locker(&m_globalLock);
VideoFrame *frame = nullptr;
// Try to get a frame not being used by the decoder
for (size_t i = 0; i < m_available.size(); i++)
{
frame = m_available.dequeue();
if (m_decode.contains(frame))
m_available.enqueue(frame);
else
break;
}
while (frame && m_used.contains(frame))
{
LOG(VB_PLAYBACK, LOG_NOTICE,
QString("GetNextFreeFrame() served a busy frame %1. Dropping. %2")
.arg(DebugString(frame, true)).arg(GetStatus()));
frame = m_available.dequeue();
}
if (frame)
SafeEnqueue(EnqueueTo, frame);
return frame;
}
/*! \brief Gets a frame from available buffers list.
*
* \param EnqueueTo Put new frame in some state other than limbo.
*/
VideoFrame *VideoBuffers::GetNextFreeFrame(BufferType EnqueueTo)
{
for (uint tries = 1; true; tries++)
{
VideoFrame *frame = VideoBuffers::GetNextFreeFrameInternal(EnqueueTo);
if (frame)
return frame;
if (tries >= TRY_LOCK_SPINS)
{
LOG(VB_GENERAL, LOG_ERR, QString("GetNextFreeFrame: "
"available:%1 used:%2 limbo:%3 pause:%4 displayed:%5 decode:%6 finished:%7")
.arg(m_available.size()).arg(m_used.size()).arg(m_limbo.size())
.arg(m_pause.size()).arg(m_displayed.size()).arg(m_decode.size())
.arg(m_finished.size()));
LOG(VB_GENERAL, LOG_ERR,
QString("GetNextFreeFrame() unable to "
"lock frame %1 times. Discarding Frames.")
.arg(TRY_LOCK_SPINS));
DiscardFrames(true);
continue;
}
if (tries && !(tries % TRY_LOCK_SPINS_BEFORE_WARNING))
{
LOG(VB_PLAYBACK, LOG_NOTICE,
QString("GetNextFreeFrame() TryLock has "
"spun %1 times, this is a lot.").arg(tries));
}
std::this_thread::sleep_for(std::chrono::microseconds(TRY_LOCK_SPIN_WAIT));
}
return nullptr;
}
/**
* \fn VideoBuffers::ReleaseFrame(VideoFrame*)
* Frame is ready to be for filtering or OSD application.
* Removes frame from limbo and adds it to used queue.
* \param frame Frame to move to used.
*/
void VideoBuffers::ReleaseFrame(VideoFrame *Frame)
{
QMutexLocker locker(&m_globalLock);
m_vpos = m_vbufferMap[Frame];
m_limbo.remove(Frame);
//non directrendering frames are ffmpeg handled
if (Frame->directrendering != 0)
m_decode.enqueue(Frame);
m_used.enqueue(Frame);
}
/**
* \fn VideoBuffers::DeLimboFrame(VideoFrame*)
* If the frame is still in the limbo state it is added to the available queue.
* \param frame Frame to move to used.
*/
void VideoBuffers::DeLimboFrame(VideoFrame *Frame)
{
QMutexLocker locker(&m_globalLock);
if (m_limbo.contains(Frame))
m_limbo.remove(Frame);
// if decoder didn't release frame and the buffer is getting released by
// the decoder assume that the frame is lost and return to available
if (!m_decode.contains(Frame))
{
ReleaseDecoderResources(Frame);
SafeEnqueue(kVideoBuffer_avail, Frame);
}
// remove from decode queue since the decoder is finished
while (m_decode.contains(Frame))
m_decode.remove(Frame);
}
/**
* \fn VideoBuffers::StartDisplayingFrame(void)
* Sets rpos to index of videoframe at head of used queue.
*/
void VideoBuffers::StartDisplayingFrame(void)
{
QMutexLocker locker(&m_globalLock);
m_rpos = m_vbufferMap[m_used.head()];
}
/**
* \fn VideoBuffers::DoneDisplayingFrame(VideoFrame *Frame)
* Removes frame from used queue and adds it to the available list.
*/
void VideoBuffers::DoneDisplayingFrame(VideoFrame *Frame)
{
QMutexLocker locker(&m_globalLock);
if(m_used.contains(Frame))
Remove(kVideoBuffer_used, Frame);
Enqueue(kVideoBuffer_finished, Frame);
// check if any finished frames are no longer used by decoder and return to available
frame_queue_t ula(m_finished);
frame_queue_t::iterator it = ula.begin();
for (; it != ula.end(); ++it)
{
if (!m_decode.contains(*it))
{
Remove(kVideoBuffer_finished, *it);
ReleaseDecoderResources(*it);
Enqueue(kVideoBuffer_avail, *it);
}
}
}
/**
* \fn VideoBuffers::DiscardFrame(VideoFrame*)
* Frame is ready to be reused by decoder.
* Add frame to available list, remove from any other list.
*/
void VideoBuffers::DiscardFrame(VideoFrame *Frame)
{
QMutexLocker locker(&m_globalLock);
ReleaseDecoderResources(Frame);
SafeEnqueue(kVideoBuffer_avail, Frame);
}
frame_queue_t *VideoBuffers::Queue(BufferType Type)
{
QMutexLocker locker(&m_globalLock);
frame_queue_t *queue = nullptr;
if (Type == kVideoBuffer_avail)
queue = &m_available;
else if (Type == kVideoBuffer_used)
queue = &m_used;
else if (Type == kVideoBuffer_displayed)
queue = &m_displayed;
else if (Type == kVideoBuffer_limbo)
queue = &m_limbo;
else if (Type == kVideoBuffer_pause)
queue = &m_pause;
else if (Type == kVideoBuffer_decode)
queue = &m_decode;
else if (Type == kVideoBuffer_finished)
queue = &m_finished;
return queue;
}
const frame_queue_t *VideoBuffers::Queue(BufferType Type) const
{
QMutexLocker locker(&m_globalLock);
const frame_queue_t *queue = nullptr;
if (Type == kVideoBuffer_avail)
queue = &m_available;
else if (Type == kVideoBuffer_used)
queue = &m_used;
else if (Type == kVideoBuffer_displayed)
queue = &m_displayed;
else if (Type == kVideoBuffer_limbo)
queue = &m_limbo;
else if (Type == kVideoBuffer_pause)
queue = &m_pause;
else if (Type == kVideoBuffer_decode)
queue = &m_decode;
else if (Type == kVideoBuffer_finished)
queue = &m_finished;
return queue;
}
VideoFrame* VideoBuffers::At(uint FrameNum)
{
return &m_buffers[FrameNum];
}
const VideoFrame* VideoBuffers::At(uint FrameNum) const
{
return &m_buffers[FrameNum];
}
VideoFrame *VideoBuffers::Dequeue(BufferType Type)
{
QMutexLocker locker(&m_globalLock);
frame_queue_t *queue = Queue(Type);
if (!queue)
return nullptr;
return queue->dequeue();
}
VideoFrame *VideoBuffers::Head(BufferType Type)
{
QMutexLocker locker(&m_globalLock);
frame_queue_t *queue = Queue(Type);
if (!queue)
return nullptr;
if (!queue->empty())
return queue->head();
return nullptr;
}
VideoFrame *VideoBuffers::Tail(BufferType Type)
{
QMutexLocker locker(&m_globalLock);
frame_queue_t *queue = Queue(Type);
if (!queue)
return nullptr;
if (!queue->empty())
return queue->tail();
return nullptr;
}
void VideoBuffers::Enqueue(BufferType Type, VideoFrame *Frame)
{
if (!Frame)
return;
frame_queue_t *queue = Queue(Type);
if (!queue)
return;
m_globalLock.lock();
queue->remove(Frame);
queue->enqueue(Frame);
if (Type == kVideoBuffer_pause)
Frame->pause_frame = 1;
m_globalLock.unlock();
}
void VideoBuffers::Remove(BufferType Type, VideoFrame *Frame)
{
if (!Frame)
return;
QMutexLocker locker(&m_globalLock);
if ((Type & kVideoBuffer_avail) == kVideoBuffer_avail)
m_available.remove(Frame);
if ((Type & kVideoBuffer_used) == kVideoBuffer_used)
m_used.remove(Frame);
if ((Type & kVideoBuffer_displayed) == kVideoBuffer_displayed)
m_displayed.remove(Frame);
if ((Type & kVideoBuffer_limbo) == kVideoBuffer_limbo)
m_limbo.remove(Frame);
if ((Type & kVideoBuffer_pause) == kVideoBuffer_pause)
m_pause.remove(Frame);
if ((Type & kVideoBuffer_decode) == kVideoBuffer_decode)
m_decode.remove(Frame);
if ((Type & kVideoBuffer_finished) == kVideoBuffer_finished)
m_finished.remove(Frame);
}
void VideoBuffers::Requeue(BufferType Dest, BufferType Source, int Count)
{
QMutexLocker locker(&m_globalLock);
Count = (Count <= 0) ? Size(Source) : Count;
for (uint i=0; i<(uint)Count; i++)
{
VideoFrame *frame = Dequeue(Source);
if (frame)
Enqueue(Dest, frame);
}
}
void VideoBuffers::SafeEnqueue(BufferType Type, VideoFrame* Frame)
{
if (!Frame)
return;
QMutexLocker locker(&m_globalLock);
Remove(kVideoBuffer_all, Frame);
Enqueue(Type, Frame);
}
frame_queue_t::iterator VideoBuffers::BeginLock(BufferType Type)
{
m_globalLock.lock();
frame_queue_t *queue = Queue(Type);
if (queue)
return queue->begin();
return m_available.begin();
}
void VideoBuffers::EndLock(void)
{
m_globalLock.unlock();
}
frame_queue_t::iterator VideoBuffers::End(BufferType Type)
{
QMutexLocker locker(&m_globalLock);
frame_queue_t *queue = Queue(Type);
return (queue ? queue->end() : m_available.end());
}
uint VideoBuffers::Size(BufferType Type) const
{
QMutexLocker locker(&m_globalLock);
const frame_queue_t *queue = Queue(Type);
if (queue)
return queue->size();
return 0;
}
bool VideoBuffers::Contains(BufferType Type, VideoFrame *Frame) const
{
QMutexLocker locker(&m_globalLock);
const frame_queue_t *queue = Queue(Type);
if (queue)
return queue->contains(Frame);
return false;
}
VideoFrame *VideoBuffers::GetScratchFrame(void)
{
if (!m_createdPauseFrame || !Head(kVideoBuffer_pause))
{
LOG(VB_GENERAL, LOG_ERR, "GetScratchFrame() called, but not allocated");
return nullptr;
}
QMutexLocker locker(&m_globalLock);
return Head(kVideoBuffer_pause);
}
VideoFrame* VideoBuffers::GetLastDecodedFrame(void)
{
return At(m_vpos);
}
VideoFrame* VideoBuffers::GetLastShownFrame(void)
{
return At(m_rpos);
}
void VideoBuffers::SetLastShownFrameToScratch(void)
{
if (!m_createdPauseFrame || !Head(kVideoBuffer_pause))
{
LOG(VB_GENERAL, LOG_ERR,
"SetLastShownFrameToScratch() called but no pause frame");
return;
}
VideoFrame *pause = Head(kVideoBuffer_pause);
m_rpos = m_vbufferMap[pause];
}
uint VideoBuffers::ValidVideoFrames(void) const
{
return Size(kVideoBuffer_used);
}
uint VideoBuffers::FreeVideoFrames(void) const
{
return Size(kVideoBuffer_avail);
}
bool VideoBuffers::EnoughFreeFrames(void) const
{
return Size(kVideoBuffer_avail) >= m_needFreeFrames;
}
bool VideoBuffers::EnoughDecodedFrames(void) const
{
return Size(kVideoBuffer_used) >= m_needPrebufferFrames;
}
const VideoFrame* VideoBuffers::GetLastDecodedFrame(void) const
{
return At(m_vpos);
}
const VideoFrame* VideoBuffers::GetLastShownFrame(void) const
{
return At(m_rpos);
}
uint VideoBuffers::Size(void) const
{
return m_buffers.size();
}
/**
* \fn VideoBuffers::DiscardFrames(bool)
* Mark all used frames as ready to be reused, this is for seek.
*/
void VideoBuffers::DiscardFrames(bool NextFrameIsKeyFrame)
{
QMutexLocker locker(&m_globalLock);
LOG(VB_PLAYBACK, LOG_INFO, QString("VideoBuffers::DiscardFrames(%1): %2")
.arg(NextFrameIsKeyFrame).arg(GetStatus()));
if (!NextFrameIsKeyFrame)
{
frame_queue_t ula(m_used);
frame_queue_t::iterator it = ula.begin();
for (; it != ula.end(); ++it)
DiscardFrame(*it);
LOG(VB_PLAYBACK, LOG_INFO,
QString("VideoBuffers::DiscardFrames(%1): %2 -- done")
.arg(NextFrameIsKeyFrame).arg(GetStatus()));
return;
}
// Remove inheritence of all frames not in displayed or pause
frame_queue_t ula(m_used);
ula.insert(ula.end(), m_limbo.begin(), m_limbo.end());
ula.insert(ula.end(), m_available.begin(), m_available.end());
ula.insert(ula.end(), m_finished.begin(), m_finished.end());
frame_queue_t::iterator it;
// Discard frames
frame_queue_t discards(m_used);
discards.insert(discards.end(), m_limbo.begin(), m_limbo.end());
discards.insert(discards.end(), m_finished.begin(), m_finished.end());
for (it = discards.begin(); it != discards.end(); ++it)
DiscardFrame(*it);
// Verify that things are kosher
if (m_available.count() + m_pause.count() + m_displayed.count() != Size())
{
for (uint i=0; i < Size(); i++)
{
if (!m_available.contains(At(i)) &&
!m_pause.contains(At(i)) &&
!m_displayed.contains(At(i)))
{
// This message is DEBUG because it does occur
// after Reset is called.
// That happens when failing over from OpenGL
// to another method, if QT painter is selected.
LOG(VB_GENERAL, LOG_DEBUG,
QString("VideoBuffers::DiscardFrames(): %1 (%2) not "
"in available, pause, or displayed %3")
.arg(DebugString(At(i), true)).arg((long long)At(i))
.arg(GetStatus()));
DiscardFrame(At(i));
}
}
}
// Make sure frames used by decoder are last...
// This is for libmpeg2 which still uses the frames after a reset.
for (it = m_decode.begin(); it != m_decode.end(); ++it)
Remove(kVideoBuffer_all, *it);
for (it = m_decode.begin(); it != m_decode.end(); ++it)
m_available.enqueue(*it);
m_decode.clear();
LOG(VB_PLAYBACK, LOG_INFO,
QString("VideoBuffers::DiscardFrames(%1): %2 -- done")
.arg(NextFrameIsKeyFrame).arg(GetStatus()));
}
/*! \brief Clear used frames after seeking.
*
* \note The use of this function after seeking is a little
* dubious as the decoder is often not paused. In extreme cases
* every video buffer has already been requested by the decoder
* by the time this function is called. Should probably be
* repurposed as ClearBeforeSeek...
*/
void VideoBuffers::ClearAfterSeek(void)
{
{
QMutexLocker locker(&m_globalLock);
for (uint i = 0; i < Size(); i++)
At(i)->timecode = 0;
for (uint i = 0; (i < Size()) && (m_used.count() > 1); i++)
{
VideoFrame *buffer = At(i);
if (m_used.contains(buffer) && !m_decode.contains(buffer))
{
m_used.remove(buffer);
m_available.enqueue(buffer);
ReleaseDecoderResources(buffer);
}
}
if (m_used.count() > 0)
{
for (uint i = 0; i < Size(); i++)
{
VideoFrame *buffer = At(i);
if (m_used.contains(buffer) && !m_decode.contains(buffer))
{
m_used.remove(buffer);
m_available.enqueue(buffer);
ReleaseDecoderResources(buffer);
m_vpos = m_vbufferMap[buffer];
m_rpos = m_vpos;
break;
}
}
}
else
{
m_vpos = m_rpos = 0;
}
}
}
bool VideoBuffers::CreateBuffers(VideoFrameType Type, QSize Size, bool ExtraForPause,
uint NeedFree, uint NeedprebufferNormal,
uint NeedPrebufferSmall, int MaxReferenceFrames)
{
Init(GetNumBuffers(Type, MaxReferenceFrames), ExtraForPause, NeedFree, NeedprebufferNormal,
NeedPrebufferSmall);
return CreateBuffers(Type, Size.width(), Size.height());
}
bool VideoBuffers::CreateBuffers(VideoFrameType Type, int Width, int Height)
{
if (format_is_hw(Type))
{
bool success = true;
for (uint i = 0; i < Size(); i++)
success &= CreateBuffer(Width, Height, i, nullptr, Type);
LOG(VB_PLAYBACK, LOG_INFO, QString("Created %1 %2 (%3x%4) video buffers")
.arg(Size()).arg(format_description(Type)).arg(Width).arg(Height));
return success;
}
vector<YUVInfo> yuvinfo;
return CreateBuffers(Type, Width, Height, yuvinfo);
}
bool VideoBuffers::CreateBuffers(VideoFrameType Type, int Width, int Height, vector<YUVInfo> YUVInfos)
{
if ((FMT_YV12 != Type) && (FMT_YUY2 != Type))
return false;
uint bufsize = buffersize(Type, Width, Height);
while (YUVInfos.size() < Size())
YUVInfos.emplace_back(Width, Height, bufsize, nullptr, nullptr);
bool ok = true;
for (uint i = 0; i < Size(); i++)
{
uint size = std::max(bufsize, YUVInfos[i].m_size);
unsigned char *data = static_cast<unsigned char*>(av_malloc(size + 64));
if (!data)
LOG(VB_GENERAL, LOG_CRIT, "Failed to allocate video buffer memory");
init(&m_buffers[i], Type, data, YUVInfos[i].m_width, YUVInfos[i].m_height, size,
(const int*) YUVInfos[i].m_pitches, (const int*) YUVInfos[i].m_offsets);
ok &= (m_buffers[i].buf != nullptr);
}
Clear();
LOG(VB_PLAYBACK, LOG_INFO, QString("Created %1 %2 (%3x%4) video buffers")
.arg(Size()).arg(format_description(Type)).arg(Width).arg(Height));
return ok;
}
bool VideoBuffers::CreateBuffer(int Width, int Height, uint Number,
void* Data, VideoFrameType Format)
{
if (Number >= Size())
return false;
init(&m_buffers[Number], Format, (unsigned char*)Data, Width, Height, 0);
return true;
}
void VideoBuffers::DeleteBuffers(void)
{
next_dbg_str = 0;
for (uint i = 0; i < Size(); i++)
av_freep(&(m_buffers[i].buf));
}
bool VideoBuffers::ReinitBuffer(VideoFrame *Frame, VideoFrameType Type, MythCodecID CodecID,
int Width, int Height)
{
if (!Frame)
return false;
if (format_is_hw(Type) || format_is_hw(Frame->codec))
{
LOG(VB_GENERAL, LOG_ERR, "Cannot re-initialise a hardware buffer");
return false;
}
// Find the frame
VideoFrameType old = Frame->codec;
int size = static_cast<int>(buffersize(Type, Width, Height));
unsigned char *buf = Frame->buf;
bool newbuf = false;
if ((Frame->size != size) || !buf)
{
// Free existing buffer
av_freep(&buf);
Frame->buf = nullptr;
// Initialise new
buf = static_cast<unsigned char*>(av_malloc(static_cast<size_t>(size + 64)));
if (!buf)
{
LOG(VB_GENERAL, LOG_ERR, "Failed to reallocate frame buffer");
return false;
}
newbuf = true;
}
LOG(VB_PLAYBACK, LOG_INFO, QString("Reallocated frame %1 %2x%3->%4 %5x%6 (New buffer: %7)")
.arg(format_description(old)).arg(Frame->width).arg(Frame->height)
.arg(format_description(Type)).arg(Width).arg(Height)
.arg(newbuf));
MythDeintType singler = Frame->deinterlace_single;
MythDeintType doubler = Frame->deinterlace_double;
init(Frame, Type, buf, Width, Height, size);
// retain deinterlacer settings and update restrictions based on new frame type
SetDeinterlacingFlags(*Frame, singler, doubler, CodecID);
clear(Frame);
return true;
}
static unsigned long long to_bitmap(const frame_queue_t& Queue, int Num);
QString VideoBuffers::GetStatus(uint Num) const
{
if (Num == 0)
Num = Size();
QString str("");
if (m_globalLock.tryLock())
{
int count = Size();
unsigned long long a = to_bitmap(m_available, count);
unsigned long long u = to_bitmap(m_used, count);
unsigned long long d = to_bitmap(m_displayed, count);
unsigned long long l = to_bitmap(m_limbo, count);
unsigned long long p = to_bitmap(m_pause, count);
unsigned long long f = to_bitmap(m_finished, count);
unsigned long long x = to_bitmap(m_decode, count);
for (uint i = 0; i < Num; i++)
{
unsigned long long mask = 1ULL << i;
QString tmp("");
if (a & mask)
tmp += (x & mask) ? "a" : "A";
if (u & mask)
tmp += (x & mask) ? "u" : "U";
if (d & mask)
tmp += (x & mask) ? "d" : "D";
if (l & mask)
tmp += (x & mask) ? "l" : "L";
if (p & mask)
tmp += (x & mask) ? "p" : "P";
if (f & mask)
tmp += (x & mask) ? "f" : "F";
if (0 == tmp.length())