MythTV  master
videobuffers.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #ifndef __VIDEOBUFFERS_H__
4 #define __VIDEOBUFFERS_H__
5 
6 #include <vector>
7 #include <map>
8 using namespace std;
9 
10 #include <QMutex>
11 #include <QString>
12 #include <QWaitCondition>
13 
14 #include "mythtvexp.h"
15 #include "mythframe.h"
16 #include "mythdeque.h"
17 
18 #ifdef USING_X11
20 #endif
21 
23 typedef vector<VideoFrame> frame_vector_t;
24 typedef map<const unsigned char*, void*> buffer_map_t;
25 typedef map<const VideoFrame*, uint> vbuffer_map_t;
26 typedef map<const VideoFrame*, QMutex*> frame_lock_map_t;
27 typedef vector<unsigned char*> uchar_vector_t;
28 
29 
30 const QString& DebugString(const VideoFrame *frame, bool short_str=false);
31 const QString& DebugString(uint str_num, bool short_str=false);
32 QString DebugString(const frame_queue_t& list);
33 QString DebugString(const vector<const VideoFrame*>& list);
34 
36 {
37  kVideoBuffer_avail = 0x00000001,
38  kVideoBuffer_limbo = 0x00000002,
39  kVideoBuffer_used = 0x00000004,
40  kVideoBuffer_pause = 0x00000008,
41  kVideoBuffer_displayed = 0x00000010,
42  kVideoBuffer_finished = 0x00000020,
43  kVideoBuffer_decode = 0x00000040,
44  kVideoBuffer_all = 0x0000003F,
45 };
46 
47 class YUVInfo
48 {
49  public:
50  YUVInfo(uint w, uint h, uint sz, const int *p, const int *o,
51  int aligned = 64);
52 
53  public:
57  uint m_pitches[3];
58  uint m_offsets[3];
59 };
60 
62 {
63  public:
64  VideoBuffers() = default;
65  virtual ~VideoBuffers();
66 
67  void Init(uint numdecode, bool extra_for_pause,
68  uint need_free, uint needprebuffer_normal,
69  uint needprebuffer_small, uint keepprebuffer);
70 
71  bool CreateBuffers(VideoFrameType type, int width, int height,
72  vector<unsigned char*> bufs,
73  vector<YUVInfo> yuvinfo);
74  bool CreateBuffers(VideoFrameType type, int width, int height);
75  void DeleteBuffers(void);
76 
77  void Reset(void);
78  void DiscardFrames(bool next_frame_keyframe);
79  void ClearAfterSeek(void);
80 
81  void SetPrebuffering(bool normal);
82 
83  VideoFrame *GetNextFreeFrame(BufferType enqueue_to = kVideoBuffer_limbo);
84  void ReleaseFrame(VideoFrame *frame);
85  void DeLimboFrame(VideoFrame *frame);
86  void StartDisplayingFrame(void);
87  void DoneDisplayingFrame(VideoFrame *frame);
88  void DiscardFrame(VideoFrame *frame);
89 
90  VideoFrame *At(uint i) { return &m_buffers[i]; }
91  VideoFrame *Dequeue(BufferType);
92  VideoFrame *Head(BufferType); // peek at next dequeue
93  VideoFrame *Tail(BufferType); // peek at last enqueue
94  void Requeue(BufferType dst, BufferType src, int num = 1);
96  void SafeEnqueue(BufferType, VideoFrame* frame);
97  void Remove(BufferType, VideoFrame *); // multiple buffer types ok
98  frame_queue_t::iterator begin_lock(BufferType); // this locks VideoBuffer
100  void end_lock() { m_globalLock.unlock(); } // this unlocks VideoBuffer
101  uint Size(BufferType type) const;
102  bool Contains(BufferType type, VideoFrame*) const;
103 
104  VideoFrame *GetScratchFrame(void);
105  VideoFrame *GetLastDecodedFrame(void) { return At(m_vpos); }
106  VideoFrame *GetLastShownFrame(void) { return At(m_rpos); }
107  void SetLastShownFrameToScratch(void);
108 
109  uint ValidVideoFrames(void) const { return Size(kVideoBuffer_used); }
110  uint FreeVideoFrames(void) const { return Size(kVideoBuffer_avail); }
111  bool EnoughFreeFrames(void) const
112  { return Size(kVideoBuffer_avail) >= m_needFreeFrames; }
113  bool EnoughDecodedFrames(void) const
114  { return Size(kVideoBuffer_used) >= m_needPrebufferFrames; }
115  bool EnoughPrebufferedFrames(void) const
116  { return Size(kVideoBuffer_used) >= m_keepPrebufferFrames; }
117 
118  const VideoFrame *At(uint i) const { return &m_buffers[i]; }
119  const VideoFrame *GetLastDecodedFrame(void) const { return At(m_vpos); }
120  const VideoFrame *GetLastShownFrame(void) const { return At(m_rpos); }
121  uint Size() const { return m_buffers.size(); }
122 
123  void Clear(uint i);
124  void Clear(void);
125 
126  bool CreateBuffer(int width, int height, uint num, void *data,
127  VideoFrameType fmt);
128  uint AddBuffer(int width, int height, void* data,
129  VideoFrameType fmt);
130 
131  QString GetStatus(int n=-1) const; // debugging method
132  private:
133  frame_queue_t *Queue(BufferType type);
134  const frame_queue_t *Queue(BufferType type) const;
135  VideoFrame *GetNextFreeFrameInternal(BufferType enqueue_to);
136 
144  vbuffer_map_t m_vbufferMap; // videobuffers to buffer's index
146  uchar_vector_t m_allocatedArrays; // for DeleteBuffers
147 
148  uint m_needFreeFrames {0};
149  uint m_needPrebufferFrames {0};
150  uint m_needPrebufferFramesNormal {0};
151  uint m_needPrebufferFramesSmall {0};
152  uint m_keepPrebufferFrames {0};
153  bool m_createdPauseFrame {false};
154  uint m_rpos {0};
155  uint m_vpos {0};
156 
157  mutable QMutex m_globalLock {QMutex::Recursive};
158 };
159 
160 #endif // __VIDEOBUFFERS_H__
BufferType
Definition: videobuffers.h:35
vbuffer_map_t m_vbufferMap
Definition: videobuffers.h:144
vector< unsigned char * > uchar_vector_t
Definition: videobuffers.h:27
VideoFrame * GetLastShownFrame(void)
Definition: videobuffers.h:106
frame_queue_t m_limbo
Definition: videobuffers.h:139
frame_queue_t m_used
Definition: videobuffers.h:138
VideoFrame * At(uint i)
Definition: videobuffers.h:90
uint m_height
Definition: videobuffers.h:55
uint ValidVideoFrames(void) const
Definition: videobuffers.h:109
This file is intended to hold X11 specific utility functions.
Definition: mythxdisplay.h:16
unsigned int uint
Definition: compat.h:140
frame_queue_t m_decode
Definition: videobuffers.h:142
enum FrameType_ VideoFrameType
const QString & DebugString(const VideoFrame *frame, bool short_str=false)
const VideoFrame * GetLastDecodedFrame(void) const
Definition: videobuffers.h:119
frame_queue_t m_available
Definition: videobuffers.h:137
bool EnoughFreeFrames(void) const
Definition: videobuffers.h:111
frame_queue_t m_displayed
Definition: videobuffers.h:141
frame_vector_t m_buffers
Definition: videobuffers.h:145
uint Size() const
Definition: videobuffers.h:121
#define MTV_PUBLIC
Definition: mythtvexp.h:15
deque< VideoFrame * >::iterator iterator
Definition: mythdeque.h:44
bool EnoughPrebufferedFrames(void) const
Definition: videobuffers.h:115
void end_lock()
Definition: videobuffers.h:100
map< const VideoFrame *, QMutex * > frame_lock_map_t
Definition: videobuffers.h:26
map< const VideoFrame *, uint > vbuffer_map_t
Definition: videobuffers.h:25
uint m_size
Definition: videobuffers.h:56
frame_queue_t m_finished
Definition: videobuffers.h:143
const VideoFrame * GetLastShownFrame(void) const
Definition: videobuffers.h:120
uint m_width
Definition: videobuffers.h:54
#define Enqueue(a, b, c)
VideoFrame * GetLastDecodedFrame(void)
Definition: videobuffers.h:105
map< const unsigned char *, void * > buffer_map_t
Definition: videobuffers.h:24
frame_queue_t m_pause
Definition: videobuffers.h:140
bool EnoughDecodedFrames(void) const
Definition: videobuffers.h:113
MythDeque< VideoFrame * > frame_queue_t
Definition: videobuffers.h:19
This class creates tracks the state of the buffers used by various VideoOutput derived classes.
Definition: videobuffers.h:61
#define Clear(a)
uchar_vector_t m_allocatedArrays
Definition: videobuffers.h:146
const VideoFrame * At(uint i) const
Definition: videobuffers.h:118
uint FreeVideoFrames(void) const
Definition: videobuffers.h:110
vector< VideoFrame > frame_vector_t
Definition: videobuffers.h:23