MythTV  master
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
VideoBuffers Class Reference

#include <videobuffers.h>

Collaboration diagram for VideoBuffers:
Collaboration graph
[legend]

Public Member Functions

 VideoBuffers ()=default
 
virtual ~VideoBuffers ()
 
void Init (uint NumDecode, bool ExtraForPause, uint NeedFree, uint NeedprebufferNormal, uint NeedPrebufferSmall)
 Creates buffers and sets various buffer management parameters. More...
 
bool CreateBuffers (VideoFrameType Type, QSize Size, bool ExtraForPause, uint NeedFree, uint NeedprebufferNormal, uint NeedPrebufferSmall, int MaxReferenceFrames=16)
 
bool CreateBuffers (VideoFrameType Type, int Width, int Height)
 
void DeleteBuffers (void)
 
void SetDeinterlacing (MythDeintType Single, MythDeintType Double, MythCodecID CodecID)
 
void Reset (void)
 
void DiscardFrames (bool NextFrameIsKeyFrame)
 
void ClearAfterSeek (void)
 Clear used frames after seeking. More...
 
void SetPrebuffering (bool Normal)
 
VideoFrameGetNextFreeFrame (BufferType EnqueueTo=kVideoBuffer_limbo)
 Gets a frame from available buffers list. More...
 
void ReleaseFrame (VideoFrame *Frame)
 
void DeLimboFrame (VideoFrame *Frame)
 
void StartDisplayingFrame (void)
 
void DoneDisplayingFrame (VideoFrame *Frame)
 
void DiscardFrame (VideoFrame *Frame)
 
void DiscardPauseFrames (void)
 
bool DiscardAndRecreate (MythCodecID CodecID, QSize VideoDim, int References)
 Discard all buffers and recreate. More...
 
VideoFrameAt (uint FrameNum)
 
VideoFrameDequeue (BufferType Type)
 
VideoFrameHead (BufferType Type)
 
VideoFrameTail (BufferType Type)
 
void Requeue (BufferType Dest, BufferType Source, int Count=1)
 
void Enqueue (BufferType Type, VideoFrame *Frame)
 
void SafeEnqueue (BufferType Type, VideoFrame *Frame)
 
void Remove (BufferType Type, VideoFrame *Frame)
 
frame_queue_t::iterator BeginLock (BufferType Type)
 Lock the video buffers. More...
 
frame_queue_t::iterator End (BufferType Type)
 
void EndLock ()
 
uint Size (BufferType Type) const
 
bool Contains (BufferType Type, VideoFrame *Frame) const
 
VideoFrameGetScratchFrame (void)
 
VideoFrameGetLastDecodedFrame (void)
 
VideoFrameGetLastShownFrame (void)
 
void SetLastShownFrameToScratch (void)
 
uint ValidVideoFrames (void) const
 
uint FreeVideoFrames (void) const
 
bool EnoughFreeFrames (void) const
 
bool EnoughDecodedFrames (void) const
 
const VideoFrameAt (uint FrameNum) const
 
const VideoFrameGetLastDecodedFrame (void) const
 
const VideoFrameGetLastShownFrame (void) const
 
uint Size (void) const
 
void Clear (uint FrameNum)
 
void Clear (void)
 
bool CreateBuffer (int Width, int Height, uint Number, void *Data, VideoFrameType Format)
 
QString GetStatus (uint Num=0) const
 

Static Public Member Functions

static uint GetNumBuffers (int PixelFormat, int MaxReferenceFrames=16, bool Decoder=false)
 
static bool ReinitBuffer (VideoFrame *Frame, VideoFrameType Type, MythCodecID CodecID, int Width, int Height)
 

Private Member Functions

frame_queue_tQueue (BufferType Type)
 
const frame_queue_tQueue (BufferType Type) const
 
VideoFrameGetNextFreeFrameInternal (BufferType EnqueueTo)
 

Static Private Member Functions

static void SetDeinterlacingFlags (VideoFrame &Frame, MythDeintType Single, MythDeintType Double, MythCodecID CodecID)
 Set the appropriate flags for single and double rate deinterlacing. More...
 

Private Attributes

frame_queue_t m_available
 
frame_queue_t m_used
 
frame_queue_t m_limbo
 
frame_queue_t m_pause
 
frame_queue_t m_displayed
 
frame_queue_t m_decode
 
frame_queue_t m_finished
 
vbuffer_map_t m_vbufferMap
 
frame_vector_t m_buffers
 
uint m_needFreeFrames { 0 }
 
uint m_needPrebufferFrames { 0 }
 
uint m_needPrebufferFramesNormal { 0 }
 
uint m_needPrebufferFramesSmall { 0 }
 
bool m_createdPauseFrame { false }
 
uint m_rpos { 0 }
 
uint m_vpos { 0 }
 
QMutex m_globalLock { QMutex::Recursive }
 

Detailed Description

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 also
VideoOutput

Definition at line 39 of file videobuffers.h.

Constructor & Destructor Documentation

◆ VideoBuffers()

VideoBuffers::VideoBuffers ( )
default

◆ ~VideoBuffers()

VideoBuffers::~VideoBuffers ( )
virtual

Definition at line 166 of file videobuffers.cpp.

Member Function Documentation

◆ GetNumBuffers()

uint VideoBuffers::GetNumBuffers ( int  PixelFormat,
int  MaxReferenceFrames = 16,
bool  Decoder = false 
)
static

◆ Init()

void VideoBuffers::Init ( uint  NumDecode,
bool  ExtraForPause,
uint  NeedFree,
uint  NeedPrebufferNormal,
uint  NeedPrebufferSmall 
)

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.

Parameters
NumDecodenumber of buffers to allocate for normal use
ExtraForPauseallocate an extra buffer, a scratch a frame for pause
NeedFreemaximum number of buffers needed in display and pause
NeedPrebufferNormalnumber buffers you can put in used or limbo normally
NeedPrebufferSmallnumber of buffers you can put in used or limbo after SetPrebuffering(false) has been called.

Definition at line 188 of file videobuffers.cpp.

Referenced by MythVideoOutputOpenGL::CreateBuffers(), CreateBuffers(), VideoOutputD3D::CreateBuffers(), and DiscardAndRecreate().

◆ CreateBuffers() [1/2]

bool VideoBuffers::CreateBuffers ( VideoFrameType  Type,
QSize  Size,
bool  ExtraForPause,
uint  NeedFree,
uint  NeedprebufferNormal,
uint  NeedPrebufferSmall,
int  MaxReferenceFrames = 16 
)

◆ CreateBuffers() [2/2]

bool VideoBuffers::CreateBuffers ( VideoFrameType  Type,
int  Width,
int  Height 
)

Definition at line 1014 of file videobuffers.cpp.

◆ ReinitBuffer()

bool VideoBuffers::ReinitBuffer ( VideoFrame Frame,
VideoFrameType  Type,
MythCodecID  CodecID,
int  Width,
int  Height 
)
static

◆ DeleteBuffers()

void VideoBuffers::DeleteBuffers ( void  )

◆ SetDeinterlacing()

void VideoBuffers::SetDeinterlacing ( MythDeintType  Single,
MythDeintType  Double,
MythCodecID  CodecID 
)

Definition at line 226 of file videobuffers.cpp.

Referenced by Init(), and MythVideoOutput::SetDeinterlacing().

◆ Reset()

void VideoBuffers::Reset ( void  )

Resets the class so that Init may be called again.

Definition at line 291 of file videobuffers.cpp.

Referenced by MythVideoOutputOpenGL::DestroyBuffers(), DiscardAndRecreate(), Init(), and VideoOutputD3D::TearDown().

◆ DiscardFrames()

void VideoBuffers::DiscardFrames ( bool  NextFrameIsKeyFrame)

Mark all used frames as ready to be reused, this is for seek.

Definition at line 873 of file videobuffers.cpp.

Referenced by MythVideoOutput::DiscardFrames(), GetNextFreeFrame(), and VideoOutputD3D::TearDown().

◆ ClearAfterSeek()

void VideoBuffers::ClearAfterSeek ( void  )

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...

Definition at line 960 of file videobuffers.cpp.

Referenced by MythVideoOutput::ClearAfterSeek().

◆ SetPrebuffering()

void VideoBuffers::SetPrebuffering ( bool  normal)

Sets prebuffering state to normal, or small.

Definition at line 308 of file videobuffers.cpp.

Referenced by MythVideoOutput::SetPrebuffering().

◆ GetNextFreeFrame()

VideoFrame * VideoBuffers::GetNextFreeFrame ( BufferType  EnqueueTo = kVideoBuffer_limbo)

Gets a frame from available buffers list.

Parameters
EnqueueToPut new frame in some state other than limbo.

Definition at line 346 of file videobuffers.cpp.

Referenced by MythVideoOutput::GetNextFreeFrame().

◆ ReleaseFrame()

void VideoBuffers::ReleaseFrame ( VideoFrame Frame)

Frame is ready to be for filtering or OSD application. Removes frame from limbo and adds it to used queue.

Parameters
frameFrame to move to used.

Definition at line 387 of file videobuffers.cpp.

Referenced by MythVideoOutput::ReleaseFrame().

◆ DeLimboFrame()

void VideoBuffers::DeLimboFrame ( VideoFrame Frame)

If the frame is still in the limbo state it is added to the available queue.

Parameters
frameFrame to move to used.

Definition at line 404 of file videobuffers.cpp.

Referenced by MythVideoOutput::DeLimboFrame().

◆ StartDisplayingFrame()

void VideoBuffers::StartDisplayingFrame ( void  )

Sets rpos to index of videoframe at head of used queue.

Definition at line 434 of file videobuffers.cpp.

Referenced by MythVideoOutput::StartDisplayingFrame().

◆ DoneDisplayingFrame()

void VideoBuffers::DoneDisplayingFrame ( VideoFrame Frame)

Removes frame from used queue and adds it to the available list.

Definition at line 444 of file videobuffers.cpp.

Referenced by MythVideoOutput::DoneDisplayingFrame(), and MythVideoOutputOpenGL::DoneDisplayingFrame().

◆ DiscardFrame()

void VideoBuffers::DiscardFrame ( VideoFrame Frame)

Frame is ready to be reused by decoder. Add frame to available list, remove from any other list.

Definition at line 477 of file videobuffers.cpp.

Referenced by MythVideoOutput::DiscardFrame().

◆ DiscardPauseFrames()

void VideoBuffers::DiscardPauseFrames ( void  )

Definition at line 487 of file videobuffers.cpp.

Referenced by MythVideoOutputOpenGL::DiscardFrames().

◆ DiscardAndRecreate()

bool VideoBuffers::DiscardAndRecreate ( MythCodecID  CodecID,
QSize  VideoDim,
int  References 
)

Discard all buffers and recreate.

This is used to 'atomically' recreate VideoBuffers that may use hardware buffer references. It is only used by MythVideoOutputOpenGL (other MythVideoOutput classes currently do not support hardware frames).

Definition at line 509 of file videobuffers.cpp.

Referenced by MythVideoOutputOpenGL::InputChanged().

◆ At() [1/2]

VideoFrame * VideoBuffers::At ( uint  FrameNum)

◆ Dequeue()

VideoFrame * VideoBuffers::Dequeue ( BufferType  Type)

◆ Head()

VideoFrame * VideoBuffers::Head ( BufferType  Type)

◆ Tail()

VideoFrame * VideoBuffers::Tail ( BufferType  Type)

◆ Requeue()

void VideoBuffers::Requeue ( BufferType  Dest,
BufferType  Source,
int  Count = 1 
)

Definition at line 734 of file videobuffers.cpp.

◆ Enqueue()

void VideoBuffers::Enqueue ( BufferType  Type,
VideoFrame Frame 
)

◆ SafeEnqueue()

void VideoBuffers::SafeEnqueue ( BufferType  Type,
VideoFrame Frame 
)

◆ Remove()

void VideoBuffers::Remove ( BufferType  Type,
VideoFrame Frame 
)

◆ BeginLock()

frame_queue_t::iterator VideoBuffers::BeginLock ( BufferType  Type)

Lock the video buffers.

Note
Use with caution. Holding this lock while releasing hardware buffers will almost certainly lead to deadlocks.

Definition at line 760 of file videobuffers.cpp.

Referenced by MythVideoOutputOpenGL::DoneDisplayingFrame(), and MythVideoOutputOpenGL::UpdatePauseFrame().

◆ End()

frame_queue_t::iterator VideoBuffers::End ( BufferType  Type)

Definition at line 774 of file videobuffers.cpp.

◆ EndLock()

void VideoBuffers::EndLock ( )

◆ Size() [1/2]

uint VideoBuffers::Size ( BufferType  Type) const

◆ Contains()

bool VideoBuffers::Contains ( BufferType  Type,
VideoFrame Frame 
) const

Definition at line 790 of file videobuffers.cpp.

Referenced by MythVideoOutputOpenGL::DoneDisplayingFrame().

◆ GetScratchFrame()

VideoFrame * VideoBuffers::GetScratchFrame ( void  )

◆ GetLastDecodedFrame() [1/2]

VideoFrame * VideoBuffers::GetLastDecodedFrame ( void  )

Definition at line 811 of file videobuffers.cpp.

Referenced by MythVideoOutput::GetLastDecodedFrame().

◆ GetLastShownFrame() [1/2]

VideoFrame * VideoBuffers::GetLastShownFrame ( void  )

Definition at line 816 of file videobuffers.cpp.

Referenced by MythVideoOutput::GetLastShownFrame().

◆ SetLastShownFrameToScratch()

void VideoBuffers::SetLastShownFrameToScratch ( void  )

Definition at line 821 of file videobuffers.cpp.

◆ ValidVideoFrames()

uint VideoBuffers::ValidVideoFrames ( void  ) const

Definition at line 834 of file videobuffers.cpp.

Referenced by MythVideoOutput::ValidVideoFrames().

◆ FreeVideoFrames()

uint VideoBuffers::FreeVideoFrames ( void  ) const

Definition at line 839 of file videobuffers.cpp.

Referenced by MythVideoOutput::FreeVideoFrames().

◆ EnoughFreeFrames()

bool VideoBuffers::EnoughFreeFrames ( void  ) const

Definition at line 844 of file videobuffers.cpp.

Referenced by MythVideoOutput::EnoughFreeFrames().

◆ EnoughDecodedFrames()

bool VideoBuffers::EnoughDecodedFrames ( void  ) const

Definition at line 849 of file videobuffers.cpp.

Referenced by MythVideoOutput::EnoughDecodedFrames().

◆ At() [2/2]

const VideoFrame * VideoBuffers::At ( uint  FrameNum) const

Definition at line 661 of file videobuffers.cpp.

◆ GetLastDecodedFrame() [2/2]

const VideoFrame * VideoBuffers::GetLastDecodedFrame ( void  ) const

Definition at line 854 of file videobuffers.cpp.

◆ GetLastShownFrame() [2/2]

const VideoFrame * VideoBuffers::GetLastShownFrame ( void  ) const

Definition at line 859 of file videobuffers.cpp.

◆ Size() [2/2]

uint VideoBuffers::Size ( void  ) const

◆ Clear() [1/2]

void VideoBuffers::Clear ( uint  FrameNum)

Definition at line 1157 of file videobuffers.cpp.

◆ Clear() [2/2]

void VideoBuffers::Clear ( void  )

Definition at line 1162 of file videobuffers.cpp.

Referenced by CreateBuffers().

◆ CreateBuffer()

bool VideoBuffers::CreateBuffer ( int  Width,
int  Height,
uint  Number,
void *  Data,
VideoFrameType  Format 
)

Definition at line 1045 of file videobuffers.cpp.

Referenced by CreateBuffers().

◆ GetStatus()

QString VideoBuffers::GetStatus ( uint  Num = 0) const

◆ Queue() [1/2]

frame_queue_t * VideoBuffers::Queue ( BufferType  Type)
private

Definition at line 614 of file videobuffers.cpp.

Referenced by BeginLock(), Contains(), Dequeue(), End(), Enqueue(), Head(), Size(), and Tail().

◆ Queue() [2/2]

const frame_queue_t * VideoBuffers::Queue ( BufferType  Type) const
private

Definition at line 635 of file videobuffers.cpp.

◆ GetNextFreeFrameInternal()

VideoFrame * VideoBuffers::GetNextFreeFrameInternal ( BufferType  EnqueueTo)
private

Definition at line 314 of file videobuffers.cpp.

Referenced by GetNextFreeFrame().

◆ SetDeinterlacingFlags()

void VideoBuffers::SetDeinterlacingFlags ( VideoFrame Frame,
MythDeintType  Single,
MythDeintType  Double,
MythCodecID  CodecID 
)
staticprivate

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
Driver deinterlacers are only available for hardware frames with the exception of NVDEC and VTB which can use shaders.
Shader and CPU deinterlacers are disabled for hardware frames (except for shaders with NVDEC and VTB)
Todo:
Handling of decoder deinterlacing with NVDEC

Definition at line 241 of file videobuffers.cpp.

Referenced by ReinitBuffer(), and SetDeinterlacing().

Member Data Documentation

◆ m_available

frame_queue_t VideoBuffers::m_available
private

◆ m_used

frame_queue_t VideoBuffers::m_used
private

◆ m_limbo

frame_queue_t VideoBuffers::m_limbo
private

◆ m_pause

frame_queue_t VideoBuffers::m_pause
private

◆ m_displayed

frame_queue_t VideoBuffers::m_displayed
private

◆ m_decode

frame_queue_t VideoBuffers::m_decode
private

◆ m_finished

frame_queue_t VideoBuffers::m_finished
private

◆ m_vbufferMap

vbuffer_map_t VideoBuffers::m_vbufferMap
private

◆ m_buffers

frame_vector_t VideoBuffers::m_buffers
private

◆ m_needFreeFrames

uint VideoBuffers::m_needFreeFrames { 0 }
private

Definition at line 123 of file videobuffers.h.

Referenced by EnoughFreeFrames(), and Init().

◆ m_needPrebufferFrames

uint VideoBuffers::m_needPrebufferFrames { 0 }
private

Definition at line 124 of file videobuffers.h.

Referenced by EnoughDecodedFrames(), Init(), and SetPrebuffering().

◆ m_needPrebufferFramesNormal

uint VideoBuffers::m_needPrebufferFramesNormal { 0 }
private

Definition at line 125 of file videobuffers.h.

Referenced by Init(), and SetPrebuffering().

◆ m_needPrebufferFramesSmall

uint VideoBuffers::m_needPrebufferFramesSmall { 0 }
private

Definition at line 126 of file videobuffers.h.

Referenced by Init(), and SetPrebuffering().

◆ m_createdPauseFrame

bool VideoBuffers::m_createdPauseFrame { false }
private

Definition at line 127 of file videobuffers.h.

Referenced by GetScratchFrame(), Init(), and SetLastShownFrameToScratch().

◆ m_rpos

uint VideoBuffers::m_rpos { 0 }
private

◆ m_vpos

uint VideoBuffers::m_vpos { 0 }
private

Definition at line 129 of file videobuffers.h.

Referenced by ClearAfterSeek(), GetLastDecodedFrame(), and ReleaseFrame().

◆ m_globalLock

QMutex VideoBuffers::m_globalLock { QMutex::Recursive }
mutableprivate

The documentation for this class was generated from the following files: