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 const QString DebugString(const frame_queue_t& list);
33 const 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 size, const int *p, const int *o,
51  int aligned = 64);
52 
53  public:
57  uint pitches[3];
58  uint offsets[3];
59 };
60 
62 {
63  public:
64  VideoBuffers();
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 &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() { global_lock.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(vpos); }
106  VideoFrame *GetLastShownFrame(void) { return At(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) >= needfreeframes; }
113  bool EnoughDecodedFrames(void) const
114  { return Size(kVideoBuffer_used) >= needprebufferframes; }
115  bool EnoughPrebufferedFrames(void) const
116  { return Size(kVideoBuffer_used) >= keepprebufferframes; }
117 
118  const VideoFrame *At(uint i) const { return &buffers[i]; }
119  const VideoFrame *GetLastDecodedFrame(void) const { return At(vpos); }
120  const VideoFrame *GetLastShownFrame(void) const { return At(rpos); }
121  uint Size() const { return 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 
137  frame_queue_t available, used, limbo, pause, displayed, decode, finished;
138  vbuffer_map_t vbufferMap; // videobuffers to buffer's index
140  uchar_vector_t allocated_arrays; // for DeleteBuffers
141 
146  uint keepprebufferframes;
148 
151 
152  mutable QMutex global_lock;
153 };
154 
155 #endif // __VIDEOBUFFERS_H__
BufferType
Definition: videobuffers.h:35
frame_queue_t used
Definition: videobuffers.h:137
static int decode(unsigned char *vbiline, int scale0, int scale1)
Definition: cc.cpp:68
vector< unsigned char * > uchar_vector_t
Definition: videobuffers.h:27
uint needprebufferframes_normal
Definition: videobuffers.h:144
VideoFrame * GetLastShownFrame(void)
Definition: videobuffers.h:106
VideoFrame * At(uint i)
Definition: videobuffers.h:90
bool createdpauseframe
Definition: videobuffers.h:147
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
uint width
Definition: videobuffers.h:54
enum FrameType_ VideoFrameType
const QString & DebugString(const VideoFrame *frame, bool short_str=false)
uint height
Definition: videobuffers.h:55
const VideoFrame * GetLastDecodedFrame(void) const
Definition: videobuffers.h:119
bool EnoughFreeFrames(void) const
Definition: videobuffers.h:111
uint Size() const
Definition: videobuffers.h:121
#define MTV_PUBLIC
Definition: mythtvexp.h:15
deque< VideoFrame * >::iterator iterator
Definition: mythdeque.h:44
QMutex global_lock
Definition: videobuffers.h:152
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
uint needprebufferframes_small
Definition: videobuffers.h:145
map< const VideoFrame *, uint > vbuffer_map_t
Definition: videobuffers.h:25
frame_vector_t buffers
Definition: videobuffers.h:139
uchar_vector_t allocated_arrays
Definition: videobuffers.h:140
const VideoFrame * GetLastShownFrame(void) const
Definition: videobuffers.h:120
#define Enqueue(a, b, c)
uint needprebufferframes
Definition: videobuffers.h:143
VideoFrame * GetLastDecodedFrame(void)
Definition: videobuffers.h:105
uint needfreeframes
Definition: videobuffers.h:142
vbuffer_map_t vbufferMap
Definition: videobuffers.h:138
map< const unsigned char *, void * > buffer_map_t
Definition: videobuffers.h:24
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)
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
uint size
Definition: videobuffers.h:56